Change detected CPU flags line to MSGL_INFO
[mplayer/kovensky.git] / mplayer.c
bloba703cc4b9f3605a1833b129489d7ec9f585537c0
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=0;
91 float start_volume = -1;
93 #include "osdep/priority.h"
95 char *heartbeat_cmd;
97 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
99 #ifdef HAVE_RTC
100 #ifdef __linux__
101 #include <linux/rtc.h>
102 #else
103 #include <rtc.h>
104 #define RTC_IRQP_SET RTCIO_IRQP_SET
105 #define RTC_PIE_ON RTCIO_PIE_ON
106 #endif /* __linux__ */
107 #endif /* HAVE_RTC */
109 #include "stream/tv.h"
110 #include "stream/stream_radio.h"
111 #ifdef CONFIG_DVBIN
112 #include "stream/dvbin.h"
113 #endif
114 #include "stream/cache2.h"
116 //**************************************************************************//
117 // Playtree
118 //**************************************************************************//
119 #include "playtree.h"
120 #include "playtreeparser.h"
122 //**************************************************************************//
123 // Config
124 //**************************************************************************//
125 #include "parser-cfg.h"
126 #include "parser-mpcmd.h"
128 //**************************************************************************//
129 // Config file
130 //**************************************************************************//
132 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
134 #include "get_path.h"
136 //**************************************************************************//
137 //**************************************************************************//
138 // Input media streaming & demultiplexer:
139 //**************************************************************************//
141 static int max_framesize=0;
143 #include "stream/stream.h"
144 #include "libmpdemux/demuxer.h"
145 #include "libmpdemux/stheader.h"
147 #ifdef CONFIG_DVDREAD
148 #include "stream/stream_dvd.h"
149 #endif
150 #include "stream/stream_dvdnav.h"
152 #include "libmpcodecs/dec_audio.h"
153 #include "libmpcodecs/dec_video.h"
154 #include "libmpcodecs/mp_image.h"
155 #include "libmpcodecs/vf.h"
156 #include "libmpcodecs/vd.h"
158 #include "mixer.h"
160 #include "mp_core.h"
161 #include "options.h"
162 #include "defaultopts.h"
164 #define Exit_SIGILL_RTCpuSel _(\
165 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
166 " It may be a bug in our new runtime CPU-detection code...\n"\
167 " Please read DOCS/HTML/en/bugreports.html.\n")
169 #define Exit_SIGILL _(\
170 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
171 " It usually happens when you run it on a CPU different than the one it was\n"\
172 " compiled/optimized for.\n"\
173 " Verify this!\n")
175 #define Exit_SIGSEGV_SIGFPE _(\
176 "- MPlayer crashed by bad usage of CPU/FPU/RAM.\n"\
177 " Recompile MPlayer with --enable-debug and make a 'gdb' backtrace and\n"\
178 " disassembly. Details in DOCS/HTML/en/bugreports_what.html#bugreports_crash.\n")
180 #define Exit_SIGCRASH _(\
181 "- MPlayer crashed. This shouldn't happen.\n"\
182 " It can be a bug in the MPlayer code _or_ in your drivers _or_ in your\n"\
183 " gcc version. If you think it's MPlayer's fault, please read\n"\
184 " DOCS/HTML/en/bugreports.html and follow the instructions there. We can't and\n"\
185 " won't help unless you provide this information when reporting a possible bug.\n")
187 #define SystemTooSlow _("\n\n"\
188 " ************************************************\n"\
189 " **** Your system is too SLOW to play this! ****\n"\
190 " ************************************************\n\n"\
191 "Possible reasons, problems, workarounds:\n"\
192 "- Most common: broken/buggy _audio_ driver\n"\
193 " - Try -ao sdl or use the OSS emulation of ALSA.\n"\
194 " - Experiment with different values for -autosync, 30 is a good start.\n"\
195 "- Slow video output\n"\
196 " - Try a different -vo driver (-vo help for a list) or try -framedrop!\n"\
197 "- Slow CPU\n"\
198 " - Don't try to play a big DVD/DivX on a slow CPU! Try some of the lavdopts,\n"\
199 " e.g. -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all.\n"\
200 "- Broken file\n"\
201 " - Try various combinations of -nobps -ni -forceidx -mc 0.\n"\
202 "- Slow media (NFS/SMB mounts, DVD, VCD etc)\n"\
203 " - Try -cache 8192.\n"\
204 "- Are you using -cache to play a non-interleaved AVI file?\n"\
205 " - Try -nocache.\n"\
206 "Read DOCS/HTML/en/video.html for tuning/speedup tips.\n"\
207 "If none of this helps you, read DOCS/HTML/en/bugreports.html.\n\n")
210 //**************************************************************************//
211 //**************************************************************************//
213 // Common FIFO functions, and keyboard/event FIFO code
214 #include "mp_fifo.h"
215 int noconsolecontrols=0;
216 //**************************************************************************//
218 // benchmark:
219 double video_time_usage=0;
220 double vout_time_usage=0;
221 static double audio_time_usage=0;
222 static int total_time_usage_start=0;
223 static int total_frame_cnt=0;
224 static int drop_frame_cnt=0; // total number of dropped frames
225 int benchmark=0;
227 // options:
228 int auto_quality=0;
229 static int output_quality=0;
231 static int list_properties = 0;
233 int term_osd = 1;
234 static char* term_osd_esc = "\x1b[A\r\x1b[K";
235 static char* playing_msg = NULL;
236 // seek:
237 static double seek_to_sec;
238 static off_t seek_to_byte=0;
239 static off_t step_sec=0;
241 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
243 // A/V sync:
244 int autosync=0; // 30 might be a good default value.
246 // codecs:
247 char **audio_codec_list=NULL; // override audio codec
248 char **video_codec_list=NULL; // override video codec
249 char **audio_fm_list=NULL; // override audio codec family
250 char **video_fm_list=NULL; // override video codec family
252 // demuxer:
253 extern char *demuxer_name; // override demuxer
254 extern char *audio_demuxer_name; // override audio demuxer
255 extern char *sub_demuxer_name; // override sub demuxer
257 // this dvdsub_id was selected via slang
258 // use this to allow dvdnav to follow -slang across stream resets,
259 // in particular the subtitle ID for a language changes
260 int dvdsub_lang_id;
261 int vobsub_id=-1;
262 char* audio_lang=NULL;
263 char* dvdsub_lang=NULL;
264 static char* spudec_ifo=NULL;
265 int forced_subs_only=0;
266 int file_filter=1;
268 // cache2:
269 int stream_cache_size=-1;
270 #ifdef CONFIG_STREAM_CACHE
271 extern int cache_fill_status;
273 float stream_cache_min_percent=20.0;
274 float stream_cache_seek_min_percent=50.0;
275 #else
276 #define cache_fill_status 0
277 #endif
279 // dump:
280 static char *stream_dump_name="stream.dump";
281 int stream_dump_type=0;
283 // A-V sync:
284 static float default_max_pts_correction=-1;//0.01f;
285 float audio_delay=0;
286 static int ignore_start=0;
288 static int softsleep=0;
290 double force_fps=0;
291 static int force_srate=0;
292 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
293 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
294 static int play_n_frames=-1;
295 static int play_n_frames_mf=-1;
297 // sub:
298 char *font_name=NULL;
299 char *sub_font_name=NULL;
300 extern int font_fontconfig;
301 float font_factor=0.75;
302 char **sub_name=NULL;
303 float sub_delay=0;
304 float sub_fps=0;
305 int sub_auto = 1;
306 char *vobsub_name=NULL;
307 /*DSP!!char *dsp=NULL;*/
308 int subcc_enabled=0;
309 int suboverlap_enabled = 1;
311 #include "ass_mp.h"
313 char* current_module=NULL; // for debugging
316 // ---
318 #ifdef CONFIG_MENU
319 #include "m_struct.h"
320 #include "libmenu/menu.h"
321 void vf_menu_pause_update(struct vf_instance* vf);
322 extern vf_info_t vf_info_menu;
323 static const vf_info_t* const libmenu_vfs[] = {
324 &vf_info_menu,
325 NULL
327 static vf_instance_t* vf_menu = NULL;
328 int use_menu = 0;
329 static char* menu_cfg = NULL;
330 static char* menu_root = "main";
331 #endif
334 #ifdef HAVE_RTC
335 static int nortc = 1;
336 static char* rtc_device;
337 #endif
339 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
340 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
341 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
342 int use_filedir_conf;
343 int use_filename_title;
345 #include "mpcommon.h"
346 #include "command.h"
348 #include "metadata.h"
350 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
352 const void *mpctx_get_video_out(MPContext *mpctx)
354 return mpctx->video_out;
357 const void *mpctx_get_audio_out(MPContext *mpctx)
359 return mpctx->audio_out;
362 void *mpctx_get_demuxer(MPContext *mpctx)
364 return mpctx->demuxer;
367 void *mpctx_get_playtree_iter(MPContext *mpctx)
369 return mpctx->playtree_iter;
372 void *mpctx_get_mixer(MPContext *mpctx)
374 return &mpctx->mixer;
377 int mpctx_get_global_sub_size(MPContext *mpctx)
379 return mpctx->global_sub_size;
382 int mpctx_get_osd_function(MPContext *mpctx)
384 return mpctx->osd_function;
387 static float get_relative_time(struct MPContext *mpctx)
389 unsigned int new_time = GetTimer();
390 unsigned int delta = new_time - mpctx->last_time;
391 mpctx->last_time = new_time;
392 return delta * 0.000001;
395 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
396 switch (type)
398 /* check for valid video stream */
399 case META_VIDEO_CODEC:
400 case META_VIDEO_BITRATE:
401 case META_VIDEO_RESOLUTION:
403 if (!mpctx->sh_video)
404 return 0;
405 break;
408 /* check for valid audio stream */
409 case META_AUDIO_CODEC:
410 case META_AUDIO_BITRATE:
411 case META_AUDIO_SAMPLES:
413 if (!mpctx->sh_audio)
414 return 0;
415 break;
418 /* check for valid demuxer */
419 case META_INFO_TITLE:
420 case META_INFO_ARTIST:
421 case META_INFO_ALBUM:
422 case META_INFO_YEAR:
423 case META_INFO_COMMENT:
424 case META_INFO_TRACK:
425 case META_INFO_GENRE:
427 if (!mpctx->demuxer)
428 return 0;
429 break;
432 default:
433 break;
436 return 1;
439 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
440 char **info = mpctx->demuxer->info;
441 int n;
443 if (!info || !tag)
444 return NULL;
446 for (n = 0; info[2*n] != NULL ; n++)
447 if (!strcasecmp (info[2*n], tag))
448 break;
450 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
453 char *get_metadata(struct MPContext *mpctx, metadata_t type)
455 char *meta = NULL;
456 sh_audio_t * const sh_audio = mpctx->sh_audio;
457 sh_video_t * const sh_video = mpctx->sh_video;
459 if (!is_valid_metadata_type(mpctx, type))
460 return NULL;
462 switch (type)
464 case META_NAME:
466 return strdup (mp_basename2 (mpctx->filename));
469 case META_VIDEO_CODEC:
471 if (sh_video->format == 0x10000001)
472 meta = strdup ("mpeg1");
473 else if (sh_video->format == 0x10000002)
474 meta = strdup ("mpeg2");
475 else if (sh_video->format == 0x10000004)
476 meta = strdup ("mpeg4");
477 else if (sh_video->format == 0x10000005)
478 meta = strdup ("h264");
479 else if (sh_video->format >= 0x20202020)
481 meta = malloc (8);
482 sprintf (meta, "%.4s", (char *) &sh_video->format);
484 else
486 meta = malloc (8);
487 sprintf (meta, "0x%08X", sh_video->format);
489 return meta;
492 case META_VIDEO_BITRATE:
494 meta = malloc (16);
495 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
496 return meta;
499 case META_VIDEO_RESOLUTION:
501 meta = malloc (16);
502 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
503 return meta;
506 case META_AUDIO_CODEC:
508 if (sh_audio->codec && sh_audio->codec->name)
509 meta = strdup (sh_audio->codec->name);
510 return meta;
513 case META_AUDIO_BITRATE:
515 meta = malloc (16);
516 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
517 return meta;
520 case META_AUDIO_SAMPLES:
522 meta = malloc (16);
523 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
524 return meta;
527 /* check for valid demuxer */
528 case META_INFO_TITLE:
529 return get_demuxer_info(mpctx, "Title");
531 case META_INFO_ARTIST:
532 return get_demuxer_info(mpctx, "Artist");
534 case META_INFO_ALBUM:
535 return get_demuxer_info(mpctx, "Album");
537 case META_INFO_YEAR:
538 return get_demuxer_info(mpctx, "Year");
540 case META_INFO_COMMENT:
541 return get_demuxer_info(mpctx, "Comment");
543 case META_INFO_TRACK:
544 return get_demuxer_info(mpctx, "Track");
546 case META_INFO_GENRE:
547 return get_demuxer_info(mpctx, "Genre");
549 default:
550 break;
553 return meta;
556 /// step size of mixer changes
557 int volstep = 3;
559 #ifdef CONFIG_DVDNAV
560 static void mp_dvdnav_context_free(MPContext *ctx){
561 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
562 ctx->nav_smpi = NULL;
563 if (ctx->nav_buffer) free(ctx->nav_buffer);
564 ctx->nav_buffer = NULL;
565 ctx->nav_start = NULL;
566 ctx->nav_in_size = 0;
568 #endif
570 void uninit_player(struct MPContext *mpctx, unsigned int mask){
571 mask &= mpctx->initialized_flags;
573 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
575 if(mask&INITIALIZED_ACODEC){
576 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
577 current_module="uninit_acodec";
578 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
579 mpctx->sh_audio=NULL;
580 mpctx->mixer.afilter = NULL;
583 if(mask&INITIALIZED_VCODEC){
584 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
585 current_module="uninit_vcodec";
586 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
587 mpctx->sh_video=NULL;
588 #ifdef CONFIG_MENU
589 vf_menu=NULL;
590 #endif
593 if(mask&INITIALIZED_DEMUXER){
594 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
595 current_module="free_demuxer";
596 if (mpctx->num_sources) {
597 mpctx->demuxer = mpctx->sources[0].demuxer;
598 for (int i = 1; i < mpctx->num_sources; i++) {
599 free_stream(mpctx->sources[i].stream);
600 free_demuxer(mpctx->sources[i].demuxer);
603 talloc_free(mpctx->sources);
604 mpctx->sources = NULL;
605 mpctx->num_sources = 0;
606 talloc_free(mpctx->timeline);
607 mpctx->timeline = NULL;
608 mpctx->num_timeline_parts = 0;
609 talloc_free(mpctx->chapters);
610 mpctx->chapters = NULL;
611 mpctx->num_chapters = 0;
612 mpctx->video_offset = 0;
613 if(mpctx->demuxer){
614 mpctx->stream=mpctx->demuxer->stream;
615 free_demuxer(mpctx->demuxer);
617 mpctx->demuxer=NULL;
620 // kill the cache process:
621 if(mask&INITIALIZED_STREAM){
622 mpctx->initialized_flags&=~INITIALIZED_STREAM;
623 current_module="uninit_stream";
624 if(mpctx->stream) free_stream(mpctx->stream);
625 mpctx->stream=NULL;
628 if(mask&INITIALIZED_VO){
629 mpctx->initialized_flags&=~INITIALIZED_VO;
630 current_module="uninit_vo";
631 vo_destroy(mpctx->video_out);
632 mpctx->video_out=NULL;
633 #ifdef CONFIG_DVDNAV
634 mp_dvdnav_context_free(mpctx);
635 #endif
638 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
639 if(mask&INITIALIZED_GETCH2){
640 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
641 current_module="uninit_getch2";
642 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
643 // restore terminal:
644 getch2_disable();
647 if(mask&INITIALIZED_VOBSUB){
648 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
649 current_module="uninit_vobsub";
650 if(vo_vobsub) vobsub_close(vo_vobsub);
651 vo_vobsub=NULL;
654 if (mask&INITIALIZED_SPUDEC){
655 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
656 current_module="uninit_spudec";
657 spudec_free(vo_spudec);
658 vo_spudec=NULL;
661 if(mask&INITIALIZED_AO){
662 mpctx->initialized_flags&=~INITIALIZED_AO;
663 current_module="uninit_ao";
664 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
665 if (mpctx->audio_out)
666 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
667 mpctx->audio_out=NULL;
670 current_module=NULL;
673 void exit_player_with_rc(struct MPContext *mpctx, exit_reason_t how, int rc)
675 uninit_player(mpctx, INITIALIZED_ALL);
676 #if defined(__MINGW32__) || defined(__CYGWIN__)
677 timeEndPeriod(1);
678 #endif
679 #ifdef CONFIG_X11
680 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
681 #endif
683 current_module="uninit_input";
684 mp_input_uninit(mpctx->input);
685 #ifdef CONFIG_MENU
686 if (use_menu)
687 menu_uninit();
688 #endif
690 #ifdef CONFIG_FREETYPE
691 current_module="uninit_font";
692 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
693 free_font_desc(mpctx->osd->sub_font);
694 free_font_desc(vo_font);
695 vo_font = NULL;
696 done_freetype();
697 #endif
698 osd_free(mpctx->osd);
700 #ifdef CONFIG_ASS
701 ass_library_done(ass_library);
702 ass_library = NULL;
703 #endif
705 current_module="exit_player";
707 // free mplayer config
708 if(mpctx->mconfig)
709 m_config_free(mpctx->mconfig);
710 mpctx->mconfig = NULL;
712 if(mpctx->playtree_iter)
713 play_tree_iter_free(mpctx->playtree_iter);
714 mpctx->playtree_iter = NULL;
715 if(mpctx->playtree)
716 play_tree_free(mpctx->playtree, 1);
717 mpctx->playtree = NULL;
719 talloc_free(mpctx->key_fifo);
721 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
722 edl_records = NULL;
723 switch(how) {
724 case EXIT_QUIT:
725 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Quit");
726 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
727 break;
728 case EXIT_EOF:
729 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","End of file");
730 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
731 break;
732 case EXIT_ERROR:
733 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Fatal error");
734 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
735 break;
736 default:
737 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
739 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
741 exit(rc);
744 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
746 exit_player_with_rc(mpctx, how, 1);
749 #ifndef __MINGW32__
750 static void child_sighandler(int x){
751 pid_t pid;
752 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
754 #endif
756 #ifdef CONFIG_CRASH_DEBUG
757 static char *prog_path;
758 static int crash_debug = 0;
759 #endif
761 static void exit_sighandler(int x){
762 static int sig_count=0;
763 #ifdef CONFIG_CRASH_DEBUG
764 if (!crash_debug || x != SIGTRAP)
765 #endif
766 ++sig_count;
767 if(sig_count==5)
769 /* We're crashing bad and can't uninit cleanly :(
770 * by popular request, we make one last (dirty)
771 * effort to restore the user's
772 * terminal. */
773 getch2_disable();
774 exit(1);
776 if(sig_count==6) exit(1);
777 if(sig_count>6){
778 // can't stop :(
779 #ifndef __MINGW32__
780 kill(getpid(),SIGKILL);
781 #endif
783 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"\n" "\nMPlayer interrupted by signal %d in module: %s\n",x,
784 current_module?current_module:"unknown"
786 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
787 if(sig_count<=1)
788 switch(x){
789 case SIGINT:
790 case SIGQUIT:
791 case SIGTERM:
792 case SIGKILL:
793 async_quit_request = 1;
794 return; // killed from keyboard (^C) or killed [-9]
795 case SIGILL:
796 #if CONFIG_RUNTIME_CPUDETECT
797 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL_RTCpuSel);
798 #else
799 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL);
800 #endif
801 case SIGFPE:
802 case SIGSEGV:
803 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGSEGV_SIGFPE);
804 default:
805 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGCRASH);
806 #ifdef CONFIG_CRASH_DEBUG
807 if (crash_debug) {
808 int gdb_pid;
809 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
810 gdb_pid = fork();
811 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
812 if (gdb_pid == 0) { // We are the child
813 char spid[20];
814 snprintf(spid, sizeof(spid), "%i", getppid());
815 getch2_disable(); // allow terminal to work properly with gdb
816 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
817 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
818 } else if (gdb_pid < 0)
819 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
820 else {
821 waitpid(gdb_pid, NULL, 0);
823 if (x == SIGTRAP) return;
825 #endif
827 getch2_disable();
828 exit(1);
831 #include "cfg-mplayer.h"
833 static int cfg_include(m_option_t *conf, char *filename)
835 return m_config_parse_config_file(conf->priv, filename);
838 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
840 char *conffile;
841 int conffile_fd;
842 if (!disable_system_conf &&
843 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
844 exit_player(mpctx, EXIT_NONE);
845 if ((conffile = get_path("")) == NULL) {
846 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"Cannot find HOME directory.\n");
847 } else {
848 #ifdef __MINGW32__
849 mkdir(conffile);
850 #else
851 mkdir(conffile, 0777);
852 #endif
853 free(conffile);
854 if ((conffile = get_path("config")) == NULL) {
855 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
856 } else {
857 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
858 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Creating config file: %s\n", conffile);
859 write(conffile_fd, default_config, strlen(default_config));
860 close(conffile_fd);
862 if (!disable_user_conf &&
863 m_config_parse_config_file(conf, conffile) < 0)
864 exit_player(mpctx, EXIT_NONE);
865 free(conffile);
870 #define PROFILE_CFG_PROTOCOL "protocol."
872 static void load_per_protocol_config (m_config_t* conf, const char *const file)
874 char *str;
875 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
876 m_profile_t *p;
878 /* does filename actually uses a protocol ? */
879 str = strstr (file, "://");
880 if (!str)
881 return;
883 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
884 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
885 p = m_config_get_profile (conf, protocol);
886 if (p)
888 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading protocol-related profile '%s'\n", protocol);
889 m_config_set_profile(conf,p);
893 #define PROFILE_CFG_EXTENSION "extension."
895 static void load_per_extension_config (m_config_t* conf, const char *const file)
897 char *str;
898 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
899 m_profile_t *p;
901 /* does filename actually have an extension ? */
902 str = strrchr (file, '.');
903 if (!str)
904 return;
906 sprintf (extension, PROFILE_CFG_EXTENSION);
907 strncat (extension, ++str, 7);
908 p = m_config_get_profile (conf, extension);
909 if (p)
911 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", extension);
912 m_config_set_profile(conf,p);
916 #define PROFILE_CFG_VO "vo."
917 #define PROFILE_CFG_AO "ao."
919 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
921 char profile[strlen (cfg) + strlen (out) + 1];
922 m_profile_t *p;
924 sprintf (profile, "%s%s", cfg, out);
925 p = m_config_get_profile (conf, profile);
926 if (p)
928 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", profile);
929 m_config_set_profile(conf,p);
934 * Tries to load a config file
935 * @return 0 if file was not found, 1 otherwise
937 static int try_load_config(m_config_t *conf, const char *file)
939 struct stat st;
940 if (stat(file, &st))
941 return 0;
942 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Loading config '%s'\n", file);
943 m_config_parse_config_file (conf, file);
944 return 1;
947 static void load_per_file_config (m_config_t* conf, const char *const file)
949 char *confpath;
950 char cfg[PATH_MAX];
951 char *name;
953 if (strlen(file) > PATH_MAX - 14) {
954 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Filename is too long, can not load file or directory specific config files\n");
955 return;
957 sprintf (cfg, "%s.conf", file);
959 name = strrchr(cfg, '/');
960 if (HAVE_DOS_PATHS) {
961 char *tmp = strrchr(cfg, '\\');
962 if (!name || tmp > name)
963 name = tmp;
964 tmp = strrchr(cfg, ':');
965 if (!name || tmp > name)
966 name = tmp;
968 if (!name)
969 name = cfg;
970 else
971 name++;
973 if (use_filedir_conf) {
974 char dircfg[PATH_MAX];
975 strcpy(dircfg, cfg);
976 strcpy(dircfg + (name - cfg), "mplayer.conf");
977 try_load_config(conf, dircfg);
979 if (try_load_config(conf, cfg))
980 return;
983 if ((confpath = get_path (name)) != NULL)
985 try_load_config(conf, confpath);
987 free (confpath);
991 /* When libmpdemux performs a blocking operation (network connection or
992 * cache filling) if the operation fails we use this function to check
993 * if it was interrupted by the user.
994 * The function returns a new value for eof. */
995 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
997 mp_cmd_t* cmd;
998 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
999 switch(cmd->id) {
1000 case MP_CMD_QUIT:
1001 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
1002 case MP_CMD_PLAY_TREE_STEP: {
1003 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
1004 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
1005 } break;
1006 case MP_CMD_PLAY_TREE_UP_STEP: {
1007 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
1008 } break;
1009 case MP_CMD_PLAY_ALT_SRC_STEP: {
1010 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
1011 } break;
1013 mp_cmd_free(cmd);
1015 return stop_play;
1018 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
1020 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
1022 play_tree_add_bpf(entry,mpctx->filename);
1025 if(!entry) {
1026 entry = mpctx->playtree_iter->tree;
1027 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1028 return PT_NEXT_ENTRY;
1030 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
1031 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1032 return PT_NEXT_ENTRY;
1035 play_tree_remove(entry,1,1);
1036 return PT_NEXT_SRC;
1038 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
1039 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1040 entry = mpctx->playtree_iter->tree;
1041 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1042 return PT_NEXT_ENTRY;
1044 play_tree_remove(entry,1,1);
1046 return PT_NEXT_SRC;
1049 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1051 struct MPOpts *opts = &mpctx->opts;
1052 sub_data *subd = NULL;
1053 struct ass_track *asst = NULL;
1055 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1056 return;
1059 #ifdef CONFIG_ASS
1060 if (opts->ass_enabled) {
1061 #ifdef CONFIG_ICONV
1062 asst = ass_read_file(ass_library, filename, sub_cp);
1063 #else
1064 asst = ass_read_file(ass_library, filename, 0);
1065 #endif
1066 if (!asst) {
1067 subd = sub_read_file(filename, fps);
1068 if (subd) {
1069 asst = ass_read_subdata(ass_library, subd, fps);
1070 if (asst) {
1071 sub_free(subd);
1072 subd = NULL;
1076 } else
1077 #endif
1078 subd = sub_read_file(filename, fps);
1081 if (!asst && !subd) {
1082 mp_tmsg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR,
1083 "Cannot load subtitles: %s\n", filename_recode(filename));
1084 return;
1087 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1088 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1089 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1090 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1091 filename_recode(filename));
1092 ++mpctx->set_of_sub_size;
1093 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "SUB: Added subtitle file (%d): %s\n", mpctx->set_of_sub_size,
1094 filename_recode(filename));
1097 void init_vo_spudec(struct MPContext *mpctx)
1099 if (vo_spudec)
1100 spudec_free(vo_spudec);
1101 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1102 vo_spudec = NULL;
1103 if (spudec_ifo) {
1104 unsigned int palette[16], width, height;
1105 current_module="spudec_init_vobsub";
1106 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1107 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1110 #ifdef CONFIG_DVDREAD
1111 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1112 current_module="spudec_init_dvdread";
1113 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1114 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1115 NULL, 0);
1117 #endif
1119 #ifdef CONFIG_DVDNAV
1120 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1121 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1122 current_module="spudec_init_dvdnav";
1123 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1125 #endif
1127 if (vo_spudec==NULL) {
1128 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1129 current_module="spudec_init_normal";
1130 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1131 spudec_set_font_factor(vo_spudec,font_factor);
1134 if (vo_spudec!=NULL) {
1135 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1136 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1141 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1142 * make it all work is to use the builtin SDL-bootstrap code, which
1143 * will be done automatically by replacing our main() if we include SDL.h.
1145 #if defined(__APPLE__) && defined(CONFIG_SDL)
1146 #ifdef CONFIG_SDL_SDL_H
1147 #include <SDL/SDL.h>
1148 #else
1149 #include <SDL.h>
1150 #endif
1151 #endif
1154 * \brief append a formatted string
1155 * \param buf buffer to print into
1156 * \param pos position of terminating 0 in buf
1157 * \param len maximum number of characters in buf, not including terminating 0
1158 * \param format printf format string
1160 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1162 va_list va;
1163 va_start(va, format);
1164 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1165 va_end(va);
1166 if (*pos >= len ) {
1167 buf[len] = 0;
1168 *pos = len;
1173 * \brief append time in the hh:mm:ss.f format
1174 * \param buf buffer to print into
1175 * \param pos position of terminating 0 in buf
1176 * \param len maximum number of characters in buf, not including terminating 0
1177 * \param time time value to convert/append
1179 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1180 long tenths = 10 * time;
1181 int f1 = tenths % 10;
1182 int ss = (tenths / 10) % 60;
1183 int mm = (tenths / 600) % 60;
1184 int hh = tenths / 36000;
1185 if (time <= 0) {
1186 saddf(buf, pos, len, "unknown");
1187 return;
1189 if (hh > 0)
1190 saddf(buf, pos, len, "%2d:", hh);
1191 if (hh > 0 || mm > 0)
1192 saddf(buf, pos, len, "%02d:", mm);
1193 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1196 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1198 struct MPOpts *opts = &mpctx->opts;
1199 sh_video_t * const sh_video = mpctx->sh_video;
1201 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1202 a_pos = playing_audio_pts(mpctx);
1203 if (mpctx->sh_audio && sh_video && at_frame) {
1204 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1205 if (mpctx->time_frame > 0)
1206 mpctx->last_av_difference += mpctx->time_frame * opts->playback_speed;
1207 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1208 && !mpctx->drop_message_shown) {
1209 mp_tmsg(MSGT_AVSYNC,MSGL_WARN,SystemTooSlow);
1210 mpctx->drop_message_shown = true;
1213 if (quiet)
1214 return;
1217 int width;
1218 char *line;
1219 unsigned pos = 0;
1220 get_screen_size();
1221 if (screen_width > 0)
1222 width = screen_width;
1223 else
1224 width = 80;
1225 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1226 /* Windows command line is broken (MinGW's rxvt works, but we
1227 * should not depend on that). */
1228 width--;
1229 #endif
1230 line = malloc(width + 1); // one additional char for the terminating null
1232 // Audio time
1233 if (mpctx->sh_audio) {
1234 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1235 if (!sh_video) {
1236 float len = demuxer_get_time_length(mpctx->demuxer);
1237 saddf(line, &pos, width, "(");
1238 sadd_hhmmssf(line, &pos, width, a_pos);
1239 saddf(line, &pos, width, ") of %.1f (", len);
1240 sadd_hhmmssf(line, &pos, width, len);
1241 saddf(line, &pos, width, ") ");
1245 // Video time
1246 if (sh_video)
1247 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1249 // A-V sync
1250 if (mpctx->sh_audio && sh_video)
1251 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1252 mpctx->last_av_difference, mpctx->total_avsync_change);
1254 // Video stats
1255 if (sh_video)
1256 saddf(line, &pos, width, "%3d/%3d ",
1257 (int)sh_video->num_frames,
1258 (int)sh_video->num_frames_decoded);
1260 // CPU usage
1261 if (sh_video) {
1262 if (sh_video->timer > 0.5)
1263 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1264 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1265 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1266 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1267 else
1268 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1269 } else if (mpctx->sh_audio) {
1270 if (mpctx->delay > 0.5)
1271 saddf(line, &pos, width, "%4.1f%% ",
1272 100.0*audio_time_usage/(double)mpctx->delay);
1273 else
1274 saddf(line, &pos, width, "??,?%% ");
1277 // VO stats
1278 if (sh_video)
1279 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1281 #ifdef CONFIG_STREAM_CACHE
1282 // cache stats
1283 if (stream_cache_size > 0)
1284 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1285 #endif
1287 // other
1288 if (opts->playback_speed != 1)
1289 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1291 // end
1292 if (erase_to_end_of_line) {
1293 line[pos] = 0;
1294 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1295 } else {
1296 memset(&line[pos], ' ', width - pos);
1297 line[width] = 0;
1298 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1300 free(line);
1304 * \brief build a chain of audio filters that converts the input format
1305 * to the ao's format, taking into account the current playback_speed.
1306 * \param sh_audio describes the requested input format of the chain.
1307 * \param ao_data describes the requested output format of the chain.
1309 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1311 struct MPOpts *opts = &mpctx->opts;
1312 int new_srate;
1313 int result;
1314 if (!sh_audio)
1316 mpctx->mixer.afilter = NULL;
1317 return 0;
1319 if(af_control_any_rev(sh_audio->afilter,
1320 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1321 &opts->playback_speed)) {
1322 new_srate = sh_audio->samplerate;
1323 } else {
1324 new_srate = sh_audio->samplerate * opts->playback_speed;
1325 if (new_srate != ao_data->samplerate) {
1326 // limits are taken from libaf/af_resample.c
1327 if (new_srate < 8000)
1328 new_srate = 8000;
1329 if (new_srate > 192000)
1330 new_srate = 192000;
1331 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1334 result = init_audio_filters(sh_audio, new_srate,
1335 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1336 mpctx->mixer.afilter = sh_audio->afilter;
1337 return result;
1341 typedef struct mp_osd_msg mp_osd_msg_t;
1342 struct mp_osd_msg {
1343 /// Previous message on the stack.
1344 mp_osd_msg_t* prev;
1345 /// Message text.
1346 char msg[128];
1347 int id,level,started;
1348 /// Display duration in ms.
1349 unsigned time;
1352 /// OSD message stack.
1353 static mp_osd_msg_t* osd_msg_stack = NULL;
1356 * \brief Add a message on the OSD message stack
1358 * If a message with the same id is already present in the stack
1359 * it is pulled on top of the stack, otherwise a new message is created.
1362 static void set_osd_msg_va(int id, int level, int time, const char *fmt,
1363 va_list ap)
1365 mp_osd_msg_t *msg,*last=NULL;
1366 int r;
1368 // look if the id is already in the stack
1369 for(msg = osd_msg_stack ; msg && msg->id != id ;
1370 last = msg, msg = msg->prev);
1371 // not found: alloc it
1372 if(!msg) {
1373 msg = calloc(1,sizeof(mp_osd_msg_t));
1374 msg->prev = osd_msg_stack;
1375 osd_msg_stack = msg;
1376 } else if(last) { // found, but it's not on top of the stack
1377 last->prev = msg->prev;
1378 msg->prev = osd_msg_stack;
1379 osd_msg_stack = msg;
1381 // write the msg
1382 r = vsnprintf(msg->msg, 128, fmt, ap);
1383 if(r >= 128) msg->msg[127] = 0;
1384 // set id and time
1385 msg->id = id;
1386 msg->level = level;
1387 msg->time = time;
1391 void set_osd_msg(int id, int level, int time, const char *fmt, ...)
1393 va_list ap;
1394 va_start(ap, fmt);
1395 set_osd_msg_va(id, level, time, fmt, ap);
1396 va_end(ap);
1399 void set_osd_tmsg(int id, int level, int time, const char *fmt, ...)
1401 va_list ap;
1402 va_start(ap, fmt);
1403 set_osd_msg_va(id, level, time, mp_gtext(fmt), ap);
1404 va_end(ap);
1409 * \brief Remove a message from the OSD stack
1411 * This function can be used to get rid of a message right away.
1415 void rm_osd_msg(int id) {
1416 mp_osd_msg_t *msg,*last=NULL;
1418 // Search for the msg
1419 for(msg = osd_msg_stack ; msg && msg->id != id ;
1420 last = msg, msg = msg->prev);
1421 if(!msg) return;
1423 // Detach it from the stack and free it
1424 if(last)
1425 last->prev = msg->prev;
1426 else
1427 osd_msg_stack = msg->prev;
1428 free(msg);
1432 * \brief Remove all messages from the OSD stack
1436 static void clear_osd_msgs(void) {
1437 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1438 while(msg) {
1439 prev = msg->prev;
1440 free(msg);
1441 msg = prev;
1443 osd_msg_stack = NULL;
1447 * \brief Get the current message from the OSD stack.
1449 * This function decrements the message timer and destroys the old ones.
1450 * The message that should be displayed is returned (if any).
1454 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1456 struct MPOpts *opts = &mpctx->opts;
1457 mp_osd_msg_t *msg,*prev,*last = NULL;
1458 static unsigned last_update = 0;
1459 unsigned now = GetTimerMS();
1460 unsigned diff;
1461 char hidden_dec_done = 0;
1463 if (mpctx->osd_visible) {
1464 // 36000000 means max timed visibility is 1 hour into the future, if
1465 // the difference is greater assume it's wrapped around from below 0
1466 if (mpctx->osd_visible - now > 36000000) {
1467 mpctx->osd_visible = 0;
1468 vo_osd_progbar_type = -1; // disable
1469 vo_osd_changed(OSDTYPE_PROGBAR);
1470 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1473 if (mpctx->osd_show_percentage_until - now > 36000000)
1474 mpctx->osd_show_percentage_until = 0;
1476 if(!last_update) last_update = now;
1477 diff = now >= last_update ? now - last_update : 0;
1479 last_update = now;
1481 // Look for the first message in the stack with high enough level.
1482 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1483 prev = msg->prev;
1484 if (msg->level > opts->osd_level && hidden_dec_done)
1485 continue;
1486 // The message has a high enough level or it is the first hidden one
1487 // in both cases we decrement the timer or kill it.
1488 if(!msg->started || msg->time > diff) {
1489 if(msg->started) msg->time -= diff;
1490 else msg->started = 1;
1491 // display it
1492 if (msg->level <= opts->osd_level)
1493 return msg;
1494 hidden_dec_done = 1;
1495 continue;
1497 // kill the message
1498 free(msg);
1499 if(last) {
1500 last->prev = prev;
1501 msg = last;
1502 } else {
1503 osd_msg_stack = prev;
1504 msg = NULL;
1507 // Nothing found
1508 return NULL;
1512 * \brief Display the OSD bar.
1514 * Display the OSD bar or fall back on a simple message.
1518 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1519 struct MPOpts *opts = &mpctx->opts;
1520 if (opts->osd_level < 1)
1521 return;
1523 if(mpctx->sh_video) {
1524 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1525 vo_osd_progbar_type = type;
1526 vo_osd_progbar_value = 256*(val-min)/(max-min);
1527 vo_osd_changed(OSDTYPE_PROGBAR);
1528 return;
1531 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1532 name, ROUND(100*(val-min)/(max-min)));
1536 * \brief Display text subtitles on the OSD
1538 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs)
1540 int i;
1541 vo_sub = subs;
1542 vo_osd_changed(OSDTYPE_SUBTITLE);
1543 if (!mpctx->sh_video) {
1544 // reverse order, since newest set_osd_msg is displayed first
1545 for (i = SUB_MAX_TEXT - 1; i >= 0; i--) {
1546 if (!subs || i >= subs->lines || !subs->text[i])
1547 rm_osd_msg(OSD_MSG_SUB_BASE + i);
1548 else {
1549 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1550 int display_time = i == subs->lines - 1 ? 180000 : 2000;
1551 set_osd_msg(OSD_MSG_SUB_BASE + i, 1, display_time, "%s", subs->text[i]);
1558 * \brief Update the OSD message line.
1560 * This function displays the current message on the vo OSD or on the term.
1561 * If the stack is empty and the OSD level is high enough the timer
1562 * is displayed (only on the vo OSD).
1566 static void update_osd_msg(struct MPContext *mpctx)
1568 struct MPOpts *opts = &mpctx->opts;
1569 mp_osd_msg_t *msg;
1570 struct osd_state *osd = mpctx->osd;
1571 char osd_text_timer[128];
1573 if (mpctx->add_osd_seek_info) {
1574 double percentage;
1575 if (mpctx->timeline && mpctx->sh_video)
1576 percentage = mpctx->sh_video->pts * 100 /
1577 mpctx->timeline[mpctx->num_timeline_parts].start;
1578 else
1579 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1580 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1581 if (mpctx->sh_video)
1582 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1583 mpctx->add_osd_seek_info = false;
1586 // Look if we have a msg
1587 if((msg = get_osd_msg(mpctx))) {
1588 if (strcmp(osd->osd_text, msg->msg)) {
1589 strncpy(osd->osd_text, msg->msg, 127);
1590 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1591 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1593 return;
1596 if(mpctx->sh_video) {
1597 // fallback on the timer
1598 if (opts->osd_level >= 2) {
1599 int len;
1600 if (mpctx->timeline)
1601 len = mpctx->timeline[mpctx->num_timeline_parts].start;
1602 else
1603 len = demuxer_get_time_length(mpctx->demuxer);
1604 int percentage = -1;
1605 char percentage_text[10];
1606 int pts = demuxer_get_current_time(mpctx->demuxer);
1608 if (mpctx->osd_show_percentage_until)
1609 if (mpctx->timeline)
1610 percentage = mpctx->sh_video->pts * 100 /
1611 mpctx->timeline[mpctx->num_timeline_parts].start;
1612 else
1613 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1615 if (percentage >= 0)
1616 snprintf(percentage_text, 9, " (%d%%)", percentage);
1617 else
1618 percentage_text[0] = 0;
1620 if (opts->osd_level == 3)
1621 snprintf(osd_text_timer, 63,
1622 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1623 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1624 len/3600,(len/60)%60,len%60,percentage_text);
1625 else
1626 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1627 mpctx->osd_function,pts/3600,(pts/60)%60,
1628 pts%60,percentage_text);
1629 } else
1630 osd_text_timer[0]=0;
1632 if (strcmp(osd->osd_text, osd_text_timer)) {
1633 strncpy(osd->osd_text, osd_text_timer, 63);
1634 vo_osd_changed(OSDTYPE_OSD);
1636 return;
1639 // Clear the term osd line
1640 if (term_osd && osd->osd_text[0]) {
1641 osd->osd_text[0] = 0;
1642 printf("%s\n",term_osd_esc);
1646 ///@}
1647 // OSDMsgStack
1650 void reinit_audio_chain(struct MPContext *mpctx)
1652 struct MPOpts *opts = &mpctx->opts;
1653 if (!mpctx->sh_audio)
1654 return;
1655 current_module="init_audio_codec";
1656 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1657 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1658 goto init_error;
1660 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1661 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1664 current_module="af_preinit";
1665 ao_data.samplerate=force_srate;
1666 ao_data.channels=0;
1667 ao_data.format=audio_output_format;
1668 #if 1
1669 // first init to detect best values
1670 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1671 // input:
1672 mpctx->sh_audio->samplerate,
1673 // output:
1674 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1675 mp_tmsg(MSGT_CPLAYER,MSGL_ERR, "Error at audio filter chain "
1676 "pre-init!\n");
1677 exit_player(mpctx, EXIT_ERROR);
1679 #endif
1680 current_module="ao2_init";
1681 mpctx->audio_out = init_best_audio_out(opts->audio_driver_list,
1682 0, // plugin flag
1683 ao_data.samplerate,
1684 ao_data.channels,
1685 ao_data.format, 0);
1686 if(!mpctx->audio_out){
1687 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Could not open/initialize audio device -> no sound.\n");
1688 goto init_error;
1690 mpctx->initialized_flags|=INITIALIZED_AO;
1691 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1692 mpctx->audio_out->info->short_name,
1693 ao_data.samplerate, ao_data.channels,
1694 af_fmt2str_short(ao_data.format),
1695 af_fmt2bits(ao_data.format)/8 );
1696 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1697 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1698 if(strlen(mpctx->audio_out->info->comment) > 0)
1699 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1700 // init audio filters:
1701 #if 1
1702 current_module="af_init";
1703 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1704 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter/ao format!\n");
1705 goto init_error;
1707 #endif
1708 mpctx->mixer.audio_out = mpctx->audio_out;
1709 mpctx->mixer.volstep = volstep;
1710 return;
1712 init_error:
1713 uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec and possibly AO
1714 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1715 mpctx->d_audio->id = -2;
1719 ///@}
1720 // Command2Property
1723 // Return pts value corresponding to the end point of audio written to the
1724 // ao so far.
1725 static double written_audio_pts(struct MPContext *mpctx)
1727 sh_audio_t *sh_audio = mpctx->sh_audio;
1728 demux_stream_t *d_audio = mpctx->d_audio;
1729 double buffered_output;
1730 // first calculate the end pts of audio that has been output by decoder
1731 double a_pts = sh_audio->pts;
1732 if (a_pts != MP_NOPTS_VALUE)
1733 // Good, decoder supports new way of calculating audio pts.
1734 // sh_audio->pts is the timestamp of the latest input packet with
1735 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1736 // the amount of bytes the decoder has written after that timestamp.
1737 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1738 else {
1739 // Decoder doesn't support new way of calculating pts (or we're
1740 // being called before it has decoded anything with known timestamp).
1741 // Use the old method of audio pts calculation: take the timestamp
1742 // of last packet with known pts the decoder has read data from,
1743 // and add amount of bytes read after the beginning of that packet
1744 // divided by input bps. This will be inaccurate if the input/output
1745 // ratio is not constant for every audio packet or if it is constant
1746 // but not accurately known in sh_audio->i_bps.
1748 a_pts = d_audio->pts;
1749 // ds_tell_pts returns bytes read after last timestamp from
1750 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1751 // it has read but not decoded
1752 if (sh_audio->i_bps)
1753 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1754 (double)sh_audio->i_bps;
1756 // Now a_pts hopefully holds the pts for end of audio from decoder.
1757 // Substract data in buffers between decoder and audio out.
1759 // Decoded but not filtered
1760 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1762 // Data buffered in audio filters, measured in bytes of "missing" output
1763 buffered_output = af_calc_delay(sh_audio->afilter);
1765 // Data that was ready for ao but was buffered because ao didn't fully
1766 // accept everything to internal buffers yet
1767 buffered_output += sh_audio->a_out_buffer_len;
1769 // Filters divide audio length by playback_speed, so multiply by it
1770 // to get the length in original units without speedup or slowdown
1771 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1773 return a_pts + mpctx->video_offset;
1776 // Return pts value corresponding to currently playing audio.
1777 double playing_audio_pts(struct MPContext *mpctx)
1779 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1780 mpctx->audio_out->get_delay();
1783 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1784 struct MPOpts *opts = &mpctx->opts;
1785 // check for frame-drop:
1786 current_module = "check_framedrop";
1787 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1788 static int dropped_frames;
1789 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1790 float d = delay-mpctx->delay;
1791 ++total_frame_cnt;
1792 // we should avoid dropping too many frames in sequence unless we
1793 // are too late. and we allow 100ms A-V delay here:
1794 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1795 && !mpctx->update_video_immediately) {
1796 ++drop_frame_cnt;
1797 ++dropped_frames;
1798 return frame_dropping;
1799 } else
1800 dropped_frames = 0;
1802 return 0;
1806 #ifdef HAVE_RTC
1807 int rtc_fd = -1;
1808 #endif
1810 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1812 #ifdef HAVE_RTC
1813 if (rtc_fd >= 0){
1814 // -------- RTC -----------
1815 current_module="sleep_rtc";
1816 while (time_frame > 0.000) {
1817 unsigned long rtc_ts;
1818 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1819 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC read error: %s\n", strerror(errno));
1820 time_frame -= get_relative_time(mpctx);
1822 } else
1823 #endif
1825 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1826 // unnecessarily high CPU usage
1827 float margin = softsleep ? 0.011 : 0;
1828 current_module = "sleep_timer";
1829 while (time_frame > margin) {
1830 usec_sleep(1000000 * (time_frame - margin));
1831 time_frame -= get_relative_time(mpctx);
1833 if (softsleep){
1834 current_module = "sleep_soft";
1835 if (time_frame < 0)
1836 mp_tmsg(MSGT_AVSYNC, MSGL_WARN, "Warning! Softsleep underflow!\n");
1837 while (time_frame > 0)
1838 time_frame -= get_relative_time(mpctx); // burn the CPU
1841 return time_frame;
1844 static void select_subtitle(MPContext *mpctx)
1846 struct MPOpts *opts = &mpctx->opts;
1847 // find the best sub to use
1848 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
1849 mpctx->global_sub_pos = -1; // no subs by default
1850 if (vobsub_index_id >= 0) {
1851 // if user asks for a vobsub id, use that first.
1852 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
1853 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1854 // if user asks for a dvd sub id, use that next.
1855 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1856 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
1857 // if there are text subs to use, use those. (autosubs come last here)
1858 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
1859 } else if (opts->sub_id == -1 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1860 // finally select subs by language and container hints
1861 if (opts->sub_id == -1 && dvdsub_lang)
1862 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
1863 if (opts->sub_id == -1)
1864 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
1865 if (opts->sub_id >= 0)
1866 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1868 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
1869 mpctx->global_sub_pos--;
1870 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
1873 #ifdef CONFIG_DVDNAV
1874 #ifndef FF_B_TYPE
1875 #define FF_B_TYPE 3
1876 #endif
1877 /// store decoded video image
1878 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1879 mp_image_t *from_mpi) {
1880 mp_image_t *mpi;
1882 /// Do not store B-frames
1883 if (from_mpi->pict_type == FF_B_TYPE)
1884 return to_mpi;
1886 if (to_mpi &&
1887 to_mpi->w == from_mpi->w &&
1888 to_mpi->h == from_mpi->h &&
1889 to_mpi->imgfmt == from_mpi->imgfmt)
1890 mpi = to_mpi;
1891 else {
1892 if (to_mpi)
1893 free_mp_image(to_mpi);
1894 if (from_mpi->w == 0 || from_mpi->h == 0)
1895 return NULL;
1896 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1899 copy_mpi(mpi,from_mpi);
1900 return mpi;
1903 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1904 struct MPOpts *opts = &ctx->opts;
1905 if (ctx->sh_video) {
1906 /// clear video pts
1907 ctx->d_video->pts = 0.0f;
1908 ctx->sh_video->pts = 0.0f;
1909 ctx->sh_video->i_pts = 0.0f;
1910 ctx->sh_video->last_pts = 0.0f;
1911 ctx->sh_video->num_buffered_pts = 0;
1912 ctx->sh_video->num_frames = 0;
1913 ctx->sh_video->num_frames_decoded = 0;
1914 ctx->sh_video->timer = 0.0f;
1915 ctx->sh_video->stream_delay = 0.0f;
1916 ctx->sh_video->timer = 0;
1917 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1920 if (ctx->sh_audio) {
1921 /// free audio packets and reset
1922 ds_free_packs(ctx->d_audio);
1923 audio_delay -= ctx->sh_audio->stream_delay;
1924 ctx->delay =- audio_delay;
1925 ctx->audio_out->reset();
1926 resync_audio_stream(ctx->sh_audio);
1929 audio_delay = 0.0f;
1930 ctx->global_sub_size = ctx->global_sub_indices[SUB_SOURCE_DEMUX] + mp_dvdnav_number_of_subs(ctx->stream);
1931 if (dvdsub_lang && opts->sub_id == dvdsub_lang_id) {
1932 dvdsub_lang_id = mp_dvdnav_sid_from_lang(ctx->stream, dvdsub_lang);
1933 if (dvdsub_lang_id != opts->sub_id) {
1934 opts->sub_id = dvdsub_lang_id;
1935 select_subtitle(ctx);
1939 /// clear all EOF related flags
1940 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1943 /// Restore last decoded DVDNAV (still frame)
1944 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1945 int *in_size,
1946 unsigned char **start,
1947 mp_image_t *decoded_frame)
1949 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1950 return decoded_frame;
1952 /// a change occured in dvdnav stream
1953 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1954 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1955 mp_dvdnav_context_free(mpctx);
1956 mp_dvdnav_reset_stream(mpctx);
1957 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1958 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1961 if (*in_size < 0) {
1962 float len;
1964 /// Display still frame, if any
1965 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1966 decoded_frame = mpctx->nav_smpi;
1968 /// increment video frame : continue playing after still frame
1969 len = demuxer_get_time_length(mpctx->demuxer);
1970 if (mpctx->sh_video->pts >= len &&
1971 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1972 mp_dvdnav_skip_still(mpctx->stream);
1973 mp_dvdnav_skip_wait(mpctx->stream);
1975 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1977 if (mpctx->nav_buffer) {
1978 *start = mpctx->nav_start;
1979 *in_size = mpctx->nav_in_size;
1980 if (mpctx->nav_start)
1981 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1985 return decoded_frame;
1988 /// Save last decoded DVDNAV (still frame)
1989 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1990 unsigned char *start,
1991 mp_image_t *decoded_frame)
1993 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1994 return;
1996 if (mpctx->nav_buffer)
1997 free(mpctx->nav_buffer);
1999 mpctx->nav_buffer = malloc(in_size);
2000 mpctx->nav_start = start;
2001 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
2002 if (mpctx->nav_buffer)
2003 memcpy(mpctx->nav_buffer,start,in_size);
2005 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
2006 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
2008 #endif /* CONFIG_DVDNAV */
2010 /* Modify video timing to match the audio timeline. There are two main
2011 * reasons this is needed. First, video and audio can start from different
2012 * positions at beginning of file or after a seek (MPlayer starts both
2013 * immediately even if they have different pts). Second, the file can have
2014 * audio timestamps that are inconsistent with the duration of the audio
2015 * packets, for example two consecutive timestamp values differing by
2016 * one second but only a packet with enough samples for half a second
2017 * of playback between them.
2019 static void adjust_sync(struct MPContext *mpctx, double frame_time)
2021 current_module = "av_sync";
2023 if (!mpctx->sh_audio)
2024 return;
2026 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
2027 double v_pts = mpctx->sh_video->pts;
2028 double av_delay = a_pts - v_pts;
2029 // Try to sync vo_flip() so it will *finish* at given time
2030 av_delay += mpctx->last_vo_flip_duration;
2031 av_delay -= audio_delay; // This much pts difference is desired
2033 double change = av_delay * 0.1;
2034 double max_change = default_max_pts_correction >= 0 ?
2035 default_max_pts_correction : frame_time * 0.1;
2036 if (change < -max_change)
2037 change = -max_change;
2038 else if (change > max_change)
2039 change = max_change;
2040 mpctx->delay += change;
2041 mpctx->total_avsync_change += change;
2044 static int fill_audio_out_buffers(struct MPContext *mpctx)
2046 struct MPOpts *opts = &mpctx->opts;
2047 unsigned int t;
2048 double tt;
2049 int playsize;
2050 int playflags=0;
2051 int audio_eof=0;
2052 sh_audio_t * const sh_audio = mpctx->sh_audio;
2054 current_module="play_audio";
2056 while (1) {
2057 int sleep_time;
2058 // all the current uses of ao_data.pts seem to be in aos that handle
2059 // sync completely wrong; there should be no need to use ao_data.pts
2060 // in get_space()
2061 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2062 playsize = mpctx->audio_out->get_space();
2063 if (mpctx->sh_video || playsize >= ao_data.outburst)
2064 break;
2066 // handle audio-only case:
2067 // this is where mplayer sleeps during audio-only playback
2068 // to avoid 100% CPU use
2069 sleep_time = (ao_data.outburst - playsize) * 1000 / ao_data.bps;
2070 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
2071 usec_sleep(sleep_time * 1000);
2074 // Fill buffer if needed:
2075 current_module="decode_audio";
2076 t = GetTimer();
2077 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2078 if (mpctx->d_audio->eof) {
2079 audio_eof = 1;
2080 if (sh_audio->a_out_buffer_len == 0)
2081 return 0;
2083 t = GetTimer() - t;
2084 tt = t*0.000001f; audio_time_usage+=tt;
2085 if (playsize > sh_audio->a_out_buffer_len) {
2086 playsize = sh_audio->a_out_buffer_len;
2087 if (audio_eof)
2088 playflags |= AOPLAY_FINAL_CHUNK;
2090 if (!playsize)
2091 return 1;
2093 // play audio:
2094 current_module="play_audio";
2096 // Is this pts value actually useful for the aos that access it?
2097 // They're obviously badly broken in the way they handle av sync;
2098 // would not having access to this make them more broken?
2099 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2100 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2102 if (playsize > 0) {
2103 sh_audio->a_out_buffer_len -= playsize;
2104 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2105 sh_audio->a_out_buffer_len);
2106 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2108 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2109 // Sanity check to avoid hanging in case current ao doesn't output
2110 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2111 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2112 sh_audio->a_out_buffer_len = 0;
2115 return 1;
2118 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2119 float *aq_sleep_time)
2121 struct MPOpts *opts = &mpctx->opts;
2122 int frame_time_remaining = 0;
2123 current_module="calc_sleep_time";
2125 *time_frame -= get_relative_time(mpctx); // reset timer
2127 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2128 float delay = mpctx->audio_out->get_delay();
2129 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2131 if (autosync) {
2133 * Adjust this raw delay value by calculating the expected
2134 * delay for this frame and generating a new value which is
2135 * weighted between the two. The higher autosync is, the
2136 * closer to the delay value gets to that which "-nosound"
2137 * would have used, and the longer it will take for A/V
2138 * sync to settle at the right value (but it eventually will.)
2139 * This settling time is very short for values below 100.
2141 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2142 float difference = delay - predicted;
2143 delay = predicted + difference / (float)autosync;
2146 *time_frame = delay - mpctx->delay / opts->playback_speed;
2148 // delay = amount of audio buffered in soundcard/driver
2149 if (delay > 0.25) delay=0.25; else
2150 if (delay < 0.10) delay=0.10;
2151 if (*time_frame > delay*0.6) {
2152 // sleep time too big - may cause audio drops (buffer underrun)
2153 frame_time_remaining = 1;
2154 *time_frame = delay*0.5;
2156 } else {
2157 // If we're lagging more than 200 ms behind the right playback rate,
2158 // don't try to "catch up".
2159 // If benchmark is set always output frames as fast as possible
2160 // without sleeping.
2161 if (*time_frame < -0.2 || benchmark)
2162 *time_frame = 0;
2165 *aq_sleep_time += *time_frame;
2168 //============================== SLEEP: ===================================
2170 *time_frame -= mpctx->video_out->flip_queue_offset;
2171 // flag 256 means: libvo driver does its timing (dvb card)
2172 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2173 *time_frame = timing_sleep(mpctx, *time_frame);
2174 *time_frame += mpctx->video_out->flip_queue_offset;
2175 return frame_time_remaining;
2178 int reinit_video_chain(struct MPContext *mpctx)
2180 struct MPOpts *opts = &mpctx->opts;
2181 sh_video_t * const sh_video = mpctx->sh_video;
2182 double ar=-1.0;
2183 //================== Init VIDEO (codec & libvo) ==========================
2184 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2185 current_module="preinit_libvo";
2187 //shouldn't we set dvideo->id=-2 when we fail?
2188 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2189 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2190 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Error opening/initializing the selected video_out (-vo) device.\n");
2191 goto err_out;
2193 mpctx->initialized_flags|=INITIALIZED_VO;
2196 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2197 mpctx->sh_video->stream_aspect = ar;
2198 current_module="init_video_filters";
2200 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2201 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2203 #ifdef CONFIG_MENU
2204 if(use_menu) {
2205 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2206 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2207 if(!vf_menu) {
2208 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Can't open libmenu video filter with root menu %s.\n",menu_root);
2209 use_menu = 0;
2212 if(vf_menu)
2213 sh_video->vfilter = vf_menu;
2214 #endif
2216 #ifdef CONFIG_ASS
2217 if(opts->ass_enabled) {
2218 int i;
2219 int insert = 1;
2220 if (opts->vf_settings)
2221 for (i = 0; opts->vf_settings[i].name; ++i)
2222 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2223 insert = 0;
2224 break;
2226 if (insert) {
2227 extern vf_info_t vf_info_ass;
2228 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2229 char* vf_arg[] = {"auto", "1", NULL};
2230 int retcode = 0;
2231 struct vf_instance *vf_ass = vf_open_plugin_noerr(opts, libass_vfs,
2232 sh_video->vfilter,
2233 "ass", vf_arg,
2234 &retcode);
2235 if (vf_ass)
2236 sh_video->vfilter = vf_ass;
2237 else if (retcode == -1) // vf_ass open() returns -1 if there's VO EOSD
2238 mp_msg(MSGT_CPLAYER, MSGL_V, "[ass] vf_ass not needed\n");
2239 else
2240 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2243 #endif
2245 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2247 #ifdef CONFIG_ASS
2248 if (opts->ass_enabled)
2249 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2250 #endif
2252 current_module="init_video_codec";
2254 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2255 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2256 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2258 if(!sh_video->initialized){
2259 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2260 goto err_out;
2263 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2265 if (sh_video->codec)
2266 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2268 sh_video->last_pts = MP_NOPTS_VALUE;
2269 sh_video->num_buffered_pts = 0;
2270 sh_video->next_frame_time = 0;
2272 if(auto_quality>0){
2273 // Auto quality option enabled
2274 output_quality=get_video_quality_max(sh_video);
2275 if(auto_quality>output_quality) auto_quality=output_quality;
2276 else output_quality=auto_quality;
2277 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2278 set_video_quality(sh_video,output_quality);
2281 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2283 current_module="init_vo";
2285 return 1;
2287 err_out:
2288 mpctx->sh_video = mpctx->d_video->sh = NULL;
2289 return 0;
2292 static double update_video_nocorrect_pts(struct MPContext *mpctx)
2294 struct sh_video *sh_video = mpctx->sh_video;
2295 double frame_time = 0;
2296 struct vo *video_out = mpctx->video_out;
2297 while (!video_out->frame_loaded) {
2298 current_module = "filter_video";
2299 // In nocorrect-pts mode there is no way to properly time these frames
2300 if (vo_get_buffered_frame(video_out, 0) >= 0)
2301 break;
2302 if (vf_output_queued_frame(sh_video->vfilter))
2303 continue;
2304 unsigned char *packet = NULL;
2305 frame_time = sh_video->next_frame_time;
2306 if (mpctx->update_video_immediately)
2307 frame_time = 0;
2308 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2309 &packet, force_fps);
2310 if (in_size < 0) {
2311 #ifdef CONFIG_DVDNAV
2312 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2313 if (mp_dvdnav_is_eof(mpctx->stream))
2314 return -1;
2315 if (mpctx->d_video)
2316 mpctx->d_video->eof = 0;
2317 if (mpctx->d_audio)
2318 mpctx->d_audio->eof = 0;
2319 mpctx->stream->eof = 0;
2320 } else
2321 #endif
2322 return -1;
2324 if (in_size > max_framesize)
2325 max_framesize = in_size;
2326 sh_video->timer += frame_time;
2327 if (mpctx->sh_audio)
2328 mpctx->delay -= frame_time;
2329 // video_read_frame can change fps (e.g. for ASF video)
2330 vo_fps = sh_video->fps;
2331 int framedrop_type = check_framedrop(mpctx, frame_time);
2332 current_module = "decode video";
2334 void *decoded_frame;
2335 #ifdef CONFIG_DVDNAV
2336 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2337 if (in_size >= 0 && !decoded_frame)
2338 #endif
2339 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2340 sh_video->pts);
2341 #ifdef CONFIG_DVDNAV
2342 // Save last still frame for future display
2343 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2344 #endif
2345 if (decoded_frame) {
2346 current_module = "filter video";
2347 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2348 if (!video_out->config_ok)
2349 break;
2352 return frame_time;
2355 static void determine_frame_pts(struct MPContext *mpctx)
2357 struct sh_video *sh_video = mpctx->sh_video;
2358 struct MPOpts *opts = &mpctx->opts;
2360 if (opts->user_pts_assoc_mode) {
2361 sh_video->pts_assoc_mode = opts->user_pts_assoc_mode;
2362 } else if (sh_video->pts_assoc_mode == 0) {
2363 if (sh_video->codec_reordered_pts != MP_NOPTS_VALUE)
2364 sh_video->pts_assoc_mode = 1;
2365 else
2366 sh_video->pts_assoc_mode = 2;
2367 } else {
2368 int probcount1 = sh_video->num_reordered_pts_problems;
2369 int probcount2 = sh_video->num_sorted_pts_problems;
2370 if (sh_video->pts_assoc_mode == 2) {
2371 int tmp = probcount1;
2372 probcount1 = probcount2;
2373 probcount2 = tmp;
2375 if (probcount1 >= probcount2 * 1.5 + 2) {
2376 sh_video->pts_assoc_mode = 3 - sh_video->pts_assoc_mode;
2377 mp_msg(MSGT_CPLAYER, MSGL_V, "Switching to pts association mode "
2378 "%d.\n", sh_video->pts_assoc_mode);
2381 sh_video->pts = sh_video->pts_assoc_mode == 1 ?
2382 sh_video->codec_reordered_pts : sh_video->sorted_pts;
2385 static double update_video(struct MPContext *mpctx)
2387 struct sh_video *sh_video = mpctx->sh_video;
2388 struct vo *video_out = mpctx->video_out;
2389 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2390 mpctx->osd); // hack for vf_expand
2391 if (!mpctx->opts.correct_pts)
2392 return update_video_nocorrect_pts(mpctx);
2394 double pts;
2396 bool hit_eof = false;
2397 while (!video_out->frame_loaded) {
2398 current_module = "filter_video";
2399 if (vo_get_buffered_frame(video_out, hit_eof) >= 0)
2400 break;
2401 // XXX Time used in this call is not counted in any performance
2402 // timer now, OSD time is not updated correctly for filter-added frames
2403 if (vf_output_queued_frame(sh_video->vfilter))
2404 continue;
2405 if (hit_eof)
2406 return -1;
2407 unsigned char *packet = NULL;
2408 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2409 if (pts != MP_NOPTS_VALUE)
2410 pts += mpctx->video_offset;
2411 if (in_size < 0) {
2412 // try to extract last frames in case of decoder lag
2413 in_size = 0;
2414 pts = 1e300;
2415 hit_eof = true;
2417 if (in_size > max_framesize)
2418 max_framesize = in_size;
2419 current_module = "decode video";
2420 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2421 void *decoded_frame = decode_video(sh_video, packet, in_size,
2422 framedrop_type, pts);
2423 if (decoded_frame) {
2424 determine_frame_pts(mpctx);
2425 current_module = "filter video";
2426 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2427 if (!video_out->config_ok)
2428 break; // We'd likely hang in this loop otherwise
2432 pts = video_out->next_pts;
2433 if (pts == MP_NOPTS_VALUE) {
2434 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2435 // Try to use decoder pts from before filters
2436 pts = sh_video->pts;
2437 if (pts == MP_NOPTS_VALUE)
2438 pts = sh_video->last_pts;
2440 sh_video->pts = pts;
2441 if (sh_video->last_pts == MP_NOPTS_VALUE)
2442 sh_video->last_pts = sh_video->pts;
2443 else if (sh_video->last_pts > sh_video->pts) {
2444 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2445 sh_video->pts, sh_video->last_pts);
2446 /* If the difference in pts is small treat it as jitter around the
2447 * right value (possibly caused by incorrect timestamp ordering) and
2448 * just show this frame immediately after the last one.
2449 * Treat bigger differences as timestamp resets and start counting
2450 * timing of later frames from the position of this one. */
2451 if (sh_video->last_pts - sh_video->pts > 0.5)
2452 sh_video->last_pts = sh_video->pts;
2453 else
2454 sh_video->pts = sh_video->last_pts;
2456 double frame_time = sh_video->pts - sh_video->last_pts;
2457 sh_video->last_pts = sh_video->pts;
2458 sh_video->timer += frame_time;
2459 if (mpctx->sh_audio)
2460 mpctx->delay -= frame_time;
2461 return frame_time;
2464 void pause_player(struct MPContext *mpctx)
2466 if (mpctx->paused)
2467 return;
2468 mpctx->paused = 1;
2469 mpctx->step_frames = 0;
2470 mpctx->time_frame -= get_relative_time(mpctx);
2472 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2473 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2475 if (mpctx->audio_out && mpctx->sh_audio)
2476 mpctx->audio_out->pause(); // pause audio, keep data if possible
2479 void unpause_player(struct MPContext *mpctx)
2481 if (!mpctx->paused)
2482 return;
2483 mpctx->paused = 0;
2485 if (mpctx->audio_out && mpctx->sh_audio)
2486 mpctx->audio_out->resume(); // resume audio
2487 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2488 && !mpctx->step_frames)
2489 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2490 (void)get_relative_time(mpctx); // ignore time that passed during pause
2493 void add_step_frame(struct MPContext *mpctx)
2495 mpctx->step_frames++;
2496 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2497 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2498 unpause_player(mpctx);
2501 static void pause_loop(struct MPContext *mpctx)
2503 mp_cmd_t* cmd;
2504 if (!quiet) {
2505 // Small hack to display the pause message on the OSD line.
2506 // The pause string is: "\n == PAUSE == \r" so we need to
2507 // take the first and the last char out
2508 if (term_osd && !mpctx->sh_video) {
2509 char msg[128] = _("\n ===== PAUSE =====\r");
2510 int mlen = strlen(msg);
2511 msg[mlen-1] = '\0';
2512 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2513 update_osd_msg(mpctx);
2514 } else
2515 mp_tmsg(MSGT_CPLAYER,MSGL_STATUS,"\n ===== PAUSE =====\r");
2516 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2519 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2520 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2521 if (cmd) {
2522 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2523 run_command(mpctx, cmd);
2524 mp_cmd_free(cmd);
2525 continue;
2527 if (mpctx->sh_video && mpctx->video_out)
2528 vo_check_events(mpctx->video_out);
2529 #ifdef CONFIG_MENU
2530 if (vf_menu)
2531 vf_menu_pause_update(vf_menu);
2532 #endif
2533 usec_sleep(20000);
2534 update_osd_msg(mpctx);
2535 int hack = vo_osd_changed(0);
2536 vo_osd_changed(hack);
2537 if (hack)
2538 break;
2543 // Find the right mute status and record position for new file position
2544 static void edl_seek_reset(MPContext *mpctx)
2546 mpctx->edl_muted = 0;
2547 next_edl_record = edl_records;
2549 while (next_edl_record) {
2550 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2551 break;
2553 if (next_edl_record->action == EDL_MUTE)
2554 mpctx->edl_muted = !mpctx->edl_muted;
2555 next_edl_record = next_edl_record->next;
2557 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2558 mixer_mute(&mpctx->mixer);
2562 // Execute EDL command for the current position if one exists
2563 static void edl_update(MPContext *mpctx)
2565 if (!next_edl_record)
2566 return;
2568 if (!mpctx->sh_video) {
2569 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot use EDL without video, disabling.\n");
2570 free_edl(edl_records);
2571 next_edl_record = NULL;
2572 edl_records = NULL;
2573 return;
2576 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2577 if (next_edl_record->action == EDL_SKIP) {
2578 mpctx->osd_function = OSD_FFW;
2579 mpctx->abs_seek_pos = 0;
2580 mpctx->rel_seek_secs = next_edl_record->length_sec;
2581 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2582 "[%f], length [%f]\n", next_edl_record->start_sec,
2583 next_edl_record->stop_sec, next_edl_record->length_sec);
2585 else if (next_edl_record->action == EDL_MUTE) {
2586 mpctx->edl_muted = !mpctx->edl_muted;
2587 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2588 mixer_mute(&mpctx->mixer);
2589 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2590 next_edl_record->start_sec );
2592 next_edl_record = next_edl_record->next;
2596 static void reinit_decoders(struct MPContext *mpctx)
2598 reinit_video_chain(mpctx);
2599 reinit_audio_chain(mpctx);
2600 mp_property_do("sub", M_PROPERTY_SET, &mpctx->global_sub_pos, mpctx);
2603 static bool timeline_set_part(struct MPContext *mpctx, int i)
2605 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
2606 struct timeline_part *n = mpctx->timeline + i;
2607 mpctx->timeline_part = i;
2608 mpctx->video_offset = n->start - n->source_start;
2609 if (n->source == p->source)
2610 return false;
2611 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo && mpctx->opts.video_id != -2 ? 0 : INITIALIZED_VO) | INITIALIZED_AO | INITIALIZED_ACODEC);
2612 mpctx->demuxer = n->source->demuxer;
2613 mpctx->d_video = mpctx->demuxer->video;
2614 mpctx->d_audio = mpctx->demuxer->audio;
2615 mpctx->d_sub = mpctx->demuxer->sub;
2616 mpctx->sh_video = mpctx->d_video->sh;
2617 mpctx->sh_audio = mpctx->d_audio->sh;
2618 return true;
2621 // Given pts, switch playback to the corresponding part.
2622 // Return offset within that part.
2623 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
2624 bool *need_reset)
2626 if (pts < 0)
2627 pts = 0;
2628 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
2629 struct timeline_part *p = mpctx->timeline + i;
2630 if (pts < (p+1)->start) {
2631 *need_reset = timeline_set_part(mpctx, i);
2632 return pts - p->start + p->source_start;
2635 return -1;
2639 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2640 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2641 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2642 static int seek(MPContext *mpctx, double amount, int style)
2644 current_module = "seek";
2645 if (mpctx->stop_play == AT_END_OF_FILE)
2646 mpctx->stop_play = KEEP_PLAYING;
2647 if (mpctx->timeline && style & SEEK_FACTOR) {
2648 amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
2649 style &= ~SEEK_FACTOR;
2651 if ((mpctx->demuxer->accurate_seek || mpctx->timeline) && mpctx->sh_video
2652 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2653 style |= SEEK_ABSOLUTE;
2654 if (amount > 0)
2655 style |= SEEK_FORWARD;
2656 else
2657 style |= SEEK_BACKWARD;
2658 amount += mpctx->sh_video->pts;
2661 /* At least the liba52 decoder wants to read from the input stream
2662 * during initialization, so reinit must be done after the demux_seek()
2663 * call that clears possible stream EOF. */
2664 bool need_reset = false;
2665 if (mpctx->timeline) {
2666 amount = timeline_set_from_time(mpctx, amount, &need_reset);
2667 if (amount == -1) {
2668 mpctx->stop_play = AT_END_OF_FILE;
2669 // Clear audio from current position
2670 if (mpctx->sh_audio) {
2671 mpctx->audio_out->reset();
2672 mpctx->sh_audio->a_buffer_len = 0;
2673 mpctx->sh_audio->a_out_buffer_len = 0;
2675 return -1;
2678 int seekresult = demux_seek(mpctx->demuxer, amount, audio_delay, style);
2679 if (need_reset)
2680 reinit_decoders(mpctx);
2681 if (seekresult == 0)
2682 return -1;
2684 if (mpctx->sh_video) {
2685 current_module = "seek_video_reset";
2686 resync_video_stream(mpctx->sh_video);
2687 vo_seek_reset(mpctx->video_out);
2688 mpctx->sh_video->num_buffered_pts = 0;
2689 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2690 mpctx->delay = 0;
2691 mpctx->time_frame = 0;
2692 mpctx->update_video_immediately = true;
2693 // Not all demuxers set d_video->pts during seek, so this value
2694 // (which is used by at least vobsub and edl code below) may
2695 // be completely wrong (probably 0).
2696 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
2697 update_subtitles(mpctx, &mpctx->opts, mpctx->sh_video,
2698 mpctx->sh_video->pts, mpctx->video_offset,
2699 mpctx->d_sub, 1);
2700 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2703 if (mpctx->sh_audio) {
2704 current_module = "seek_audio_reset";
2705 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2706 mpctx->sh_audio->a_buffer_len = 0;
2707 mpctx->sh_audio->a_out_buffer_len = 0;
2708 if (!mpctx->sh_video)
2709 update_subtitles(mpctx, &mpctx->opts, NULL, mpctx->sh_audio->pts,
2710 mpctx->video_offset, mpctx->d_sub, 1);
2713 if (vo_vobsub && mpctx->sh_video) {
2714 current_module = "seek_vobsub_reset";
2715 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2718 edl_seek_reset(mpctx);
2720 mpctx->total_avsync_change = 0;
2721 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2722 drop_frame_cnt = 0;
2724 current_module = NULL;
2725 return 0;
2728 int get_current_chapter(struct MPContext *mpctx)
2730 if (!mpctx->chapters || !mpctx->sh_video)
2731 return demuxer_get_current_chapter(mpctx->demuxer);
2733 int i;
2734 double current_pts = mpctx->sh_video->pts;
2735 for (i = 1; i < mpctx->num_chapters; i++)
2736 if (current_pts < mpctx->chapters[i].start)
2737 break;
2738 return i - 1;
2741 // currently returns a string allocated with malloc, not talloc
2742 char *chapter_display_name(struct MPContext *mpctx, int chapter)
2744 if (!mpctx->chapters || !mpctx->sh_video)
2745 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
2746 return strdup(mpctx->chapters[chapter].name);
2749 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
2750 char **chapter_name)
2752 if (!mpctx->chapters || !mpctx->sh_video)
2753 return demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
2754 chapter_name);
2755 if (chapter >= mpctx->num_chapters)
2756 return -1;
2757 if (chapter < 0)
2758 chapter = 0;
2759 *seek_pts = mpctx->chapters[chapter].start;
2760 if (chapter_name)
2761 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
2762 return chapter;
2765 static int find_ordered_chapter_sources(struct MPContext *mpctx,
2766 struct content_source *sources,
2767 int num_sources,
2768 unsigned char uid_map[][16])
2770 int num_filenames = 0;
2771 char **filenames = NULL;
2772 if (num_sources > 1) {
2773 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
2774 "other sources.\n");
2775 if (mpctx->stream->type != STREAMTYPE_FILE) {
2776 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
2777 "normal disk file. Will not search for related files.\n");
2778 } else {
2779 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
2780 "same directory to find referenced sources.\n");
2781 filenames = find_files(mpctx->demuxer->filename, ".mkv",
2782 &num_filenames);
2786 int num_left = num_sources - 1;
2787 for (int i = 0; i < num_filenames && num_left > 0; i++) {
2788 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
2789 filename_recode(filenames[i]));
2790 int format;
2791 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
2792 if (!s)
2793 continue;
2794 struct demuxer *d = demux_open(&mpctx->opts, s, DEMUXER_TYPE_MATROSKA,
2795 mpctx->opts.audio_id,
2796 mpctx->opts.video_id,
2797 mpctx->opts.sub_id, filenames[i]);
2798 if (!d) {
2799 free_stream(s);
2800 continue;
2802 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
2803 for (int i = 1; i < num_sources; i++) {
2804 if (sources[i].demuxer)
2805 continue;
2806 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
2807 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
2808 i, filename_recode(d->filename));
2809 sources[i].stream = s;
2810 sources[i].demuxer = d;
2811 num_left--;
2812 goto match;
2816 free_demuxer(d);
2817 free_stream(s);
2818 continue;
2819 match:
2822 talloc_free(filenames);
2823 if (num_left) {
2824 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
2825 "There will be parts MISSING from the video!\n");
2826 for (int i = 1, j = 1; i < num_sources; i++)
2827 if (sources[i].demuxer) {
2828 sources[j] = sources[i];
2829 memcpy(uid_map[j], uid_map[i], 16);
2830 j++;
2833 return num_sources - num_left;
2836 static void build_ordered_chapter_timeline(struct MPContext *mpctx)
2838 if (!mpctx->opts.ordered_chapters) {
2839 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
2840 "you have disabled support for them. Ignoring.\n");
2841 return;
2844 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
2845 "edit timeline.\n");
2847 struct demuxer *demuxer = mpctx->demuxer;
2848 struct matroska_data *m = &demuxer->matroska_data;
2850 // +1 because sources/uid_map[0] is original file even if all chapters
2851 // actually use other sources and need separate entries
2852 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
2853 m->num_ordered_chapters+1);
2854 sources[0].stream = mpctx->stream;
2855 sources[0].demuxer = mpctx->demuxer;
2856 unsigned char uid_map[m->num_ordered_chapters+1][16];
2857 int num_sources = 1;
2858 memcpy(uid_map[0], m->segment_uid, 16);
2860 for (int i = 0; i < m->num_ordered_chapters; i++) {
2861 struct matroska_chapter *c = m->ordered_chapters + i;
2862 if (!c->has_segment_uid)
2863 memcpy(c->segment_uid, m->segment_uid, 16);
2865 for (int j = 0; j < num_sources; j++)
2866 if (!memcmp(c->segment_uid, uid_map[j], 16))
2867 goto found1;
2868 memcpy(uid_map[num_sources], c->segment_uid, 16);
2869 sources[num_sources] = (struct content_source){};
2870 num_sources++;
2871 found1:
2875 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
2876 uid_map);
2879 // +1 for terminating chapter with start time marking end of last real one
2880 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
2881 m->num_ordered_chapters + 1);
2882 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
2883 m->num_ordered_chapters);
2884 uint64_t starttime = 0;
2885 uint64_t missing_time = 0;
2886 int part_count = 0;
2887 int num_chapters = 0;
2888 uint64_t prev_part_offset;
2889 for (int i = 0; i < m->num_ordered_chapters; i++) {
2890 struct matroska_chapter *c = m->ordered_chapters + i;
2892 int j;
2893 for (j = 0; j < num_sources; j++) {
2894 if (!memcmp(c->segment_uid, uid_map[j], 16))
2895 goto found2;
2897 missing_time += c->end - c->start;
2898 continue;
2899 found2:;
2900 chapters[num_chapters].start = starttime / 1000.;
2901 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
2902 /* Only add a separate part if the time or file actually changes.
2903 * Matroska files have chapter divisions that are redundant from
2904 * timeline point of view because the same chapter structure is used
2905 * both to specify the timeline and for normal chapter information.
2906 * Removing a missing inserted external chapter can also cause this. */
2907 if (part_count == 0 || c->start != starttime + prev_part_offset
2908 || sources + j != timeline[part_count - 1].source) {
2909 timeline[part_count].source = sources + j;
2910 timeline[part_count].start = chapters[num_chapters].start;
2911 timeline[part_count].source_start = c->start / 1000.;
2912 prev_part_offset = c->start - starttime;
2913 part_count++;
2915 starttime += c->end - c->start;
2916 num_chapters++;
2918 timeline[part_count].start = starttime / 1000.;
2920 if (!part_count) {
2921 // None of the parts come from the file itself???
2922 talloc_free(sources);
2923 talloc_free(timeline);
2924 talloc_free(chapters);
2925 return;
2928 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
2929 "sources. Total length %.3f seconds.\n", part_count, num_sources,
2930 timeline[part_count].start);
2931 if (missing_time)
2932 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
2933 "from the timeline!\n", missing_time / 1000.);
2934 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
2935 for (int i = 0; i < num_sources; i++)
2936 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
2937 filename_recode(sources[i].demuxer->filename));
2938 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
2939 "source_start, source):\n");
2940 for (int i = 0; i < part_count; i++) {
2941 struct timeline_part *p = timeline + i;
2942 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
2943 p->source_start, p->source - sources);
2945 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start);
2946 mpctx->sources = sources;
2947 mpctx->num_sources = num_sources;
2948 mpctx->timeline = timeline;
2949 mpctx->num_timeline_parts = part_count;
2950 mpctx->num_chapters = num_chapters;
2951 mpctx->chapters = chapters;
2953 mpctx->timeline_part = 0;
2954 mpctx->demuxer = timeline[0].source->demuxer;
2958 static int read_keys(void *ctx, int fd)
2960 getch2(ctx);
2961 return mplayer_get_key(ctx, 0);
2965 #ifdef PTW32_STATIC_LIB
2966 static void detach_ptw32(void)
2968 pthread_win32_thread_detach_np();
2969 pthread_win32_process_detach_np();
2971 #endif
2973 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2974 * file for some tools to link against. */
2975 #ifndef DISABLE_MAIN
2976 int main(int argc,char* argv[]){
2979 char * mem_ptr;
2981 // movie info:
2983 /* Flag indicating whether MPlayer should exit without playing anything. */
2984 int opt_exit = 0;
2986 //float a_frame=0; // Audio
2988 int i;
2990 struct MPContext *mpctx = &(struct MPContext){
2991 .osd_function = OSD_PLAY,
2992 .begin_skip = MP_NOPTS_VALUE,
2993 .play_tree_step = 1,
2994 .global_sub_pos = -1,
2995 .set_of_sub_pos = -1,
2996 .file_format = DEMUXER_TYPE_UNKNOWN,
2997 .last_dvb_step = 1,
3000 #ifdef PTW32_STATIC_LIB
3001 pthread_win32_process_attach_np();
3002 pthread_win32_thread_attach_np();
3003 atexit(detach_ptw32);
3004 #endif
3006 InitTimer();
3007 srand(GetTimerMS());
3009 mp_msg_init();
3010 set_av_log_callback();
3012 #ifdef CONFIG_X11
3013 mpctx->x11_state = vo_x11_init_state();
3014 #endif
3015 struct MPOpts *opts = &mpctx->opts;
3016 set_default_mplayer_options(opts);
3017 // Create the config context and register the options
3018 mpctx->mconfig = m_config_new(opts, cfg_include);
3019 m_config_register_options(mpctx->mconfig,mplayer_opts);
3020 mp_input_register_options(mpctx->mconfig);
3022 // Preparse the command line
3023 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
3025 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
3026 set_path_env();
3027 #endif
3029 #ifdef CONFIG_TV
3030 stream_tv_defaults.immediate = 1;
3031 #endif
3033 parse_cfgfiles(mpctx, mpctx->mconfig);
3035 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
3036 if(mpctx->playtree == NULL)
3037 opt_exit = 1;
3038 else {
3039 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
3040 if(mpctx->playtree) {
3041 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3042 if(mpctx->playtree_iter) {
3043 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3044 play_tree_iter_free(mpctx->playtree_iter);
3045 mpctx->playtree_iter = NULL;
3047 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3051 mpctx->key_fifo = mp_fifo_create(opts);
3053 print_version("MPlayer");
3055 #if defined(__MINGW32__) || defined(__CYGWIN__)
3057 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
3058 BOOL WINAPI (*setDEP)(DWORD) = NULL;
3059 if (kernel32)
3060 setDEP = GetProcAddress(kernel32, "SetProcessDEPPolicy");
3061 if (setDEP) setDEP(3);
3063 // stop Windows from showing all kinds of annoying error dialogs
3064 SetErrorMode(0x8003);
3065 // request 1ms timer resolution
3066 timeBeginPeriod(1);
3067 #endif
3069 #ifdef CONFIG_PRIORITY
3070 set_priority();
3071 #endif
3073 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
3074 list_video_out();
3075 opt_exit = 1;
3078 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
3079 list_audio_out();
3080 opt_exit = 1;
3083 /* Check codecs.conf. */
3084 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3085 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3086 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3087 if(!parse_codec_cfg(NULL)){
3088 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3090 mp_tmsg(MSGT_CPLAYER,MSGL_V,"Using built-in default codecs.conf.\n");
3093 free( mem_ptr ); // release the buffer created by get_path()
3096 #if 0
3097 if(video_codec_list){
3098 int i;
3099 video_codec=video_codec_list[0];
3100 for(i=0;video_codec_list[i];i++)
3101 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
3103 #endif
3104 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3105 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available audio codecs:\n");
3106 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3107 list_codecs(1);
3108 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3109 opt_exit = 1;
3111 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3112 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video codecs:\n");
3113 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3114 list_codecs(0);
3115 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3116 opt_exit = 1;
3118 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3119 vfm_help();
3120 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3121 opt_exit = 1;
3123 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3124 afm_help();
3125 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3126 opt_exit = 1;
3128 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3129 af_help();
3130 printf("\n");
3131 opt_exit = 1;
3133 #ifdef CONFIG_X11
3134 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3135 fstype_help();
3136 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3137 opt_exit = 1;
3139 #endif
3140 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3141 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3142 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3143 demuxer_help();
3144 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3145 opt_exit = 1;
3147 if(list_properties) {
3148 property_print_help();
3149 opt_exit = 1;
3152 if(opt_exit)
3153 exit_player(mpctx, EXIT_NONE);
3155 if(!mpctx->filename && !player_idle_mode){
3156 // no file/vcd/dvd -> show HELP:
3157 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
3158 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3161 /* Display what configure line was used */
3162 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3164 // Many users forget to include command line in bugreports...
3165 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3166 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "CommandLine:");
3167 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3168 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3171 //------ load global data first ------
3173 mpctx->osd = osd_create();
3175 // check font
3176 #ifdef CONFIG_FREETYPE
3177 init_freetype();
3178 #endif
3179 #ifdef CONFIG_FONTCONFIG
3180 if(font_fontconfig <= 0)
3182 #endif
3183 #ifdef CONFIG_BITMAP_FONT
3184 if(font_name){
3185 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3186 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",
3187 filename_recode(font_name));
3188 } else {
3189 // try default:
3190 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3191 free(mem_ptr); // release the buffer created by get_path()
3192 if(!vo_font)
3193 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3195 if (sub_font_name)
3196 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3197 else
3198 mpctx->osd->sub_font = vo_font;
3199 #endif
3200 #ifdef CONFIG_FONTCONFIG
3202 #endif
3204 #ifdef CONFIG_ASS
3205 ass_library = ass_init();
3206 #endif
3208 #ifdef HAVE_RTC
3209 if(!nortc)
3211 // seteuid(0); /* Can't hurt to try to get root here */
3212 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3213 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Failed to open %s: %s (it should be readable by the user.)\n",
3214 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3215 else {
3216 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3218 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3219 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp, strerror(errno));
3220 mp_tmsg(MSGT_CPLAYER, MSGL_HINT, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp);
3221 close (rtc_fd);
3222 rtc_fd = -1;
3223 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3224 /* variable only by the root */
3225 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno));
3226 close (rtc_fd);
3227 rtc_fd = -1;
3228 } else
3229 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Using Linux hardware RTC timing (%ldHz).\n", irqp);
3232 if(rtc_fd<0)
3233 #endif /* HAVE_RTC */
3234 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3235 softsleep?"software":timer_name);
3237 #ifdef HAVE_TERMCAP
3238 load_termcap(NULL); // load key-codes
3239 #endif
3241 // ========== Init keyboard FIFO (connection to libvo) ============
3243 // Init input system
3244 current_module = "init_input";
3245 mpctx->input = mp_input_init(&opts->input);
3246 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3247 if(slave_mode)
3248 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
3249 else if(!noconsolecontrols)
3250 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
3251 // Set the libstream interrupt callback
3252 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
3254 #ifdef CONFIG_MENU
3255 if(use_menu) {
3256 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3257 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3258 else {
3259 menu_cfg = get_path("menu.conf");
3260 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3261 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3262 else {
3263 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
3264 MPLAYER_CONFDIR "/menu.conf"))
3265 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", MPLAYER_CONFDIR"/menu.conf");
3266 else {
3267 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Menu init failed.\n");
3268 use_menu = 0;
3273 #endif
3275 current_module = NULL;
3277 /// Catch signals
3278 #ifndef __MINGW32__
3279 signal(SIGCHLD,child_sighandler);
3280 #endif
3282 #ifdef CONFIG_CRASH_DEBUG
3283 prog_path = argv[0];
3284 #endif
3285 //========= Catch terminate signals: ================
3286 // terminate requests:
3287 signal(SIGTERM,exit_sighandler); // kill
3288 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3290 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3292 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3293 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3294 #ifdef CONFIG_SIGHANDLER
3295 // fatal errors:
3296 signal(SIGBUS,exit_sighandler); // bus error
3297 signal(SIGSEGV,exit_sighandler); // segfault
3298 signal(SIGILL,exit_sighandler); // illegal instruction
3299 signal(SIGFPE,exit_sighandler); // floating point exc.
3300 signal(SIGABRT,exit_sighandler); // abort()
3301 #ifdef CONFIG_CRASH_DEBUG
3302 if (crash_debug)
3303 signal(SIGTRAP,exit_sighandler);
3304 #endif
3305 #endif
3307 // ******************* Now, let's see the per-file stuff ********************
3309 play_next_file:
3311 // init global sub numbers
3312 mpctx->global_sub_size = 0;
3313 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3315 if (mpctx->filename) {
3316 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
3317 load_per_extension_config (mpctx->mconfig, mpctx->filename);
3318 load_per_file_config (mpctx->mconfig, mpctx->filename);
3321 if (opts->video_driver_list)
3322 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
3323 if (opts->audio_driver_list)
3324 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
3326 // We must enable getch2 here to be able to interrupt network connection
3327 // or cache filling
3328 if(!noconsolecontrols && !slave_mode){
3329 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
3330 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"WARNING: getch2_init called twice!\n");
3331 else
3332 getch2_enable(); // prepare stdin for hotkeys...
3333 mpctx->initialized_flags|=INITIALIZED_GETCH2;
3334 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3337 // =================== GUI idle loop (STOP state) ===========================
3338 while (player_idle_mode && !mpctx->filename) {
3339 play_tree_t * entry = NULL;
3340 mp_cmd_t * cmd;
3341 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3342 if (mpctx->video_out)
3343 vo_check_events(mpctx->video_out);
3344 usec_sleep(20000);
3346 switch (cmd->id) {
3347 case MP_CMD_LOADFILE:
3348 // prepare a tree entry with the new filename
3349 entry = play_tree_new();
3350 play_tree_add_file(entry, cmd->args[0].v.s);
3351 // The entry is added to the main playtree after the switch().
3352 break;
3353 case MP_CMD_LOADLIST:
3354 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3355 break;
3356 case MP_CMD_QUIT:
3357 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3358 break;
3359 case MP_CMD_GET_PROPERTY:
3360 case MP_CMD_SET_PROPERTY:
3361 case MP_CMD_STEP_PROPERTY:
3362 run_command(mpctx, cmd);
3363 break;
3366 mp_cmd_free(cmd);
3368 if (entry) { // user entered a command that gave a valid entry
3369 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3370 play_tree_free_list(mpctx->playtree->child, 1);
3371 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3373 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3375 play_tree_set_child(mpctx->playtree, entry);
3377 /* Make iterator start at the top the of tree. */
3378 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3379 if (!mpctx->playtree_iter) continue;
3381 // find the first real item in the tree
3382 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3383 // no items!
3384 play_tree_iter_free(mpctx->playtree_iter);
3385 mpctx->playtree_iter = NULL;
3386 continue; // wait for next command
3388 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3391 //---------------------------------------------------------------------------
3393 if (mpctx->filename) {
3394 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nPlaying %s.\n",
3395 filename_recode(mpctx->filename));
3396 if(use_filename_title && vo_wintitle == NULL)
3397 vo_wintitle = strdup(mp_basename2(mpctx->filename));
3400 if (edl_filename) {
3401 if (edl_records) free_edl(edl_records);
3402 next_edl_record = edl_records = edl_parse_file();
3404 if (edl_output_filename) {
3405 if (edl_fd) fclose(edl_fd);
3406 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3408 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Can't open EDL file [%s] for writing.\n",
3409 filename_recode(edl_output_filename));
3413 //==================== Open VOB-Sub ============================
3415 current_module="vobsub";
3416 if (vobsub_name){
3417 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3418 if(vo_vobsub==NULL)
3419 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
3420 filename_recode(vobsub_name));
3421 } else if (sub_auto && mpctx->filename){
3422 /* try to autodetect vobsub from movie filename ::atmos */
3423 char *buf = strdup(mpctx->filename), *psub;
3424 char *pdot = strrchr(buf, '.');
3425 char *pslash = strrchr(buf, '/');
3426 #if defined(__MINGW32__) || defined(__CYGWIN__)
3427 if (!pslash) pslash = strrchr(buf, '\\');
3428 #endif
3429 if (pdot && (!pslash || pdot > pslash))
3430 *pdot = '\0';
3431 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3432 /* try from ~/.mplayer/sub */
3433 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3434 char *bname;
3435 int l;
3436 bname = strrchr(buf,'/');
3437 #if defined(__MINGW32__) || defined(__CYGWIN__)
3438 if(!bname) bname = strrchr(buf,'\\');
3439 #endif
3440 if(bname) bname++;
3441 else bname = buf;
3442 l = strlen(psub) + strlen(bname) + 1;
3443 psub = realloc(psub,l);
3444 strcat(psub,bname);
3445 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3446 free(psub);
3448 free(buf);
3450 if(vo_vobsub){
3451 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3452 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3453 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3455 // setup global sub numbering
3456 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3457 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3460 //============ Open & Sync STREAM --- fork cache2 ====================
3462 mpctx->stream=NULL;
3463 mpctx->demuxer=NULL;
3464 if (mpctx->d_audio) {
3465 //free_demuxer_stream(mpctx->d_audio);
3466 mpctx->d_audio=NULL;
3468 if (mpctx->d_video) {
3469 //free_demuxer_stream(d_video);
3470 mpctx->d_video=NULL;
3472 mpctx->sh_audio=NULL;
3473 mpctx->sh_video=NULL;
3475 current_module="open_stream";
3476 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3477 if(!mpctx->stream) { // error...
3478 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3479 goto goto_next_file;
3481 mpctx->initialized_flags|=INITIALIZED_STREAM;
3483 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3484 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3485 "playlist support will not be used automatically.\n"
3486 "MPlayer's playlist code is unsafe and should only be used with "
3487 "trusted sources.\nPlayback will probably fail.\n\n");
3488 #if 0
3489 play_tree_t* entry;
3490 // Handle playlist
3491 current_module="handle_playlist";
3492 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3493 filename_recode(mpctx->filename));
3494 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3495 mpctx->eof=playtree_add_playlist(mpctx, entry);
3496 goto goto_next_file;
3497 #endif
3499 mpctx->stream->start_pos+=seek_to_byte;
3501 if(stream_dump_type==5){
3502 unsigned char buf[4096];
3503 int len;
3504 FILE *f;
3505 current_module="dumpstream";
3506 stream_reset(mpctx->stream);
3507 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3508 f=fopen(stream_dump_name,"wb");
3509 if(!f){
3510 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3511 exit_player(mpctx, EXIT_ERROR);
3513 if (dvd_chapter > 1) {
3514 int chapter = dvd_chapter - 1;
3515 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3517 while(!mpctx->stream->eof && !async_quit_request){
3518 len=stream_read(mpctx->stream,buf,4096);
3519 if(len>0) {
3520 if(fwrite(buf,len,1,f) != 1) {
3521 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3522 exit_player(mpctx, EXIT_ERROR);
3525 if(dvd_last_chapter > 0) {
3526 int chapter = -1;
3527 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3528 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3529 break;
3532 if(fclose(f)) {
3533 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3534 exit_player(mpctx, EXIT_ERROR);
3536 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3537 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3540 #ifdef CONFIG_DVDREAD
3541 if(mpctx->stream->type==STREAMTYPE_DVD){
3542 current_module="dvd lang->id";
3543 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3544 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3545 // setup global sub numbering
3546 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3547 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3548 current_module=NULL;
3550 #endif
3552 #ifdef CONFIG_DVDNAV
3553 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3554 current_module="dvdnav lang->id";
3555 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3556 dvdsub_lang_id = -3;
3557 if(dvdsub_lang && opts->sub_id==-1)
3558 dvdsub_lang_id = opts->sub_id = mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3559 // setup global sub numbering
3560 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3561 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3562 current_module=NULL;
3564 #endif
3566 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3567 goto_enable_cache:
3568 if(stream_cache_size>0){
3569 int res;
3570 current_module="enable_cache";
3571 res = stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3572 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3573 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0));
3574 if(res == 0)
3575 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3578 //============ Open DEMUXERS --- DETECT file type =======================
3579 current_module="demux_open";
3581 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3583 // HACK to get MOV Reference Files working
3585 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3587 unsigned char* playlist_entry;
3588 play_tree_t *list = NULL, *entry = NULL;
3590 current_module="handle_demux_playlist";
3591 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3593 char *temp, *bname;
3595 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3596 filename_recode(playlist_entry));
3598 bname=mp_basename(playlist_entry);
3599 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3600 continue;
3602 if (!strcmp(playlist_entry, mpctx->filename)) // ignoring self-reference
3603 continue;
3605 entry = play_tree_new();
3607 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3609 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3610 if (temp)
3612 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3613 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3614 strcat(temp, playlist_entry);
3615 if (!strcmp(temp, mpctx->filename)) {
3616 free(temp);
3617 continue;
3619 play_tree_add_file(entry,temp);
3620 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3621 free(temp);
3624 else
3625 play_tree_add_file(entry,playlist_entry);
3627 if(!list)
3628 list = entry;
3629 else
3630 play_tree_append_entry(list,entry);
3632 free_demuxer(mpctx->demuxer);
3633 mpctx->demuxer = NULL;
3635 if (list)
3637 entry = play_tree_new();
3638 play_tree_set_child(entry,list);
3639 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3640 goto goto_next_file;
3644 if(!mpctx->demuxer)
3645 goto goto_next_file;
3647 if (mpctx->demuxer->matroska_data.ordered_chapters)
3648 build_ordered_chapter_timeline(mpctx);
3650 if (!mpctx->sources) {
3651 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
3652 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
3653 .demuxer = mpctx->demuxer};
3654 mpctx->num_sources = 1;
3657 if(dvd_chapter>1) {
3658 double pts;
3659 if (seek_chapter(mpctx, dvd_chapter-1, &pts, NULL) >= 0 && pts > -1.0)
3660 seek(mpctx, pts, SEEK_ABSOLUTE);
3663 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3665 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3666 int i;
3667 int maxid = -1;
3668 // setup global sub numbering
3669 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3670 for (i = 0; i < MAX_S_STREAMS; i++)
3671 if (mpctx->demuxer->s_streams[i])
3672 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3673 mpctx->global_sub_size += maxid + 1;
3675 // Make opts->sub_id always selectable if set.
3676 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3677 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3679 #ifdef CONFIG_ASS
3680 if (opts->ass_enabled && ass_library && use_embedded_fonts) {
3681 for (int j = 0; j < mpctx->num_sources; j++) {
3682 struct demuxer *d = mpctx->sources[j].demuxer;
3683 for (int i = 0; i < d->num_attachments; i++) {
3684 struct demux_attachment *att = d->attachments + i;
3685 if (att->name && att->type && att->data && att->data_size
3686 && (strcmp(att->type, "application/x-truetype-font") == 0
3687 || strcmp(att->type, "application/x-font") == 0))
3688 ass_add_font(ass_library, att->name, att->data, att->data_size);
3692 #endif
3694 current_module="demux_open2";
3696 //file_format=demuxer->file_format;
3698 mpctx->d_audio=mpctx->demuxer->audio;
3699 mpctx->d_video=mpctx->demuxer->video;
3700 mpctx->d_sub=mpctx->demuxer->sub;
3702 if (ts_prog) {
3703 int tmp = ts_prog;
3704 mp_property_do("switch_program", M_PROPERTY_SET, &tmp, mpctx);
3706 // select audio stream
3707 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3709 // DUMP STREAMS:
3710 if((stream_dump_type)&&(stream_dump_type<4)){
3711 FILE *f;
3712 demux_stream_t *ds=NULL;
3713 current_module="dump";
3714 // select stream to dump
3715 switch(stream_dump_type){
3716 case 1: ds=mpctx->d_audio;break;
3717 case 2: ds=mpctx->d_video;break;
3718 case 3: ds=mpctx->d_sub;break;
3720 if(!ds){
3721 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"dump: FATAL: Selected stream missing!\n");
3722 exit_player(mpctx, EXIT_ERROR);
3724 // disable other streams:
3725 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3726 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3727 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3728 // let's dump it!
3729 f=fopen(stream_dump_name,"wb");
3730 if(!f){
3731 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3732 exit_player(mpctx, EXIT_ERROR);
3734 while(!ds->eof){
3735 unsigned char* start;
3736 int in_size=ds_get_packet(ds,&start);
3737 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3738 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3739 if(in_size>0) fwrite(start,in_size,1,f);
3740 if(dvd_last_chapter>0) {
3741 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3742 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3743 break;
3746 fclose(f);
3747 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3748 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3751 mpctx->sh_audio=mpctx->d_audio->sh;
3752 mpctx->sh_video=mpctx->d_video->sh;
3754 if(mpctx->sh_video){
3756 current_module="video_read_properties";
3757 if(!video_read_properties(mpctx->sh_video)) {
3758 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Video: Cannot read properties.\n");
3759 mpctx->sh_video=mpctx->d_video->sh=NULL;
3760 } else {
3761 mp_tmsg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
3762 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3763 mpctx->sh_video->fps,mpctx->sh_video->frametime
3766 /* need to set fps here for output encoders to pick it up in their init */
3767 if(force_fps){
3768 mpctx->sh_video->fps=force_fps;
3769 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3771 vo_fps = mpctx->sh_video->fps;
3773 if(!mpctx->sh_video->fps && !force_fps){
3774 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"FPS not specified in the header or invalid, use the -fps option.\n");
3775 mpctx->sh_video=mpctx->d_video->sh=NULL;
3781 if(!mpctx->sh_video && !mpctx->sh_audio){
3782 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "No stream found.\n");
3783 #ifdef CONFIG_DVBIN
3784 if(mpctx->stream->type == STREAMTYPE_DVB)
3786 int dir;
3787 int v = mpctx->last_dvb_step;
3788 if(v > 0)
3789 dir = DVB_CHANNEL_HIGHER;
3790 else
3791 dir = DVB_CHANNEL_LOWER;
3793 if(dvb_step_channel(mpctx->stream, dir)) {
3794 mpctx->stop_play = PT_NEXT_ENTRY;
3795 mpctx->dvbin_reopen = 1;
3798 #endif
3799 goto goto_next_file; // exit_player(_("Fatal error"));
3802 /* display clip info */
3803 demux_info_print(mpctx->demuxer);
3805 //================== Read SUBTITLES (DVD & TEXT) ==========================
3806 if(vo_spudec==NULL && mpctx->sh_video &&
3807 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3808 init_vo_spudec(mpctx);
3811 // after reading video params we should load subtitles because
3812 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3813 // check .sub
3814 current_module="read_subtitles_file";
3815 double sub_fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
3816 if(sub_name){
3817 for (i = 0; sub_name[i] != NULL; ++i)
3818 add_subtitles(mpctx, sub_name[i], sub_fps, 0);
3820 if(sub_auto) { // auto load sub file ...
3821 char *psub = get_path( "sub/" );
3822 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3823 int i = 0;
3824 free(psub); // release the buffer created by get_path() above
3825 while (tmp[i]) {
3826 add_subtitles(mpctx, tmp[i], sub_fps, 1);
3827 free(tmp[i++]);
3829 free(tmp);
3831 if (mpctx->set_of_sub_size > 0) {
3832 // setup global sub numbering
3833 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3834 mpctx->global_sub_size += mpctx->set_of_sub_size;
3838 if (mpctx->global_sub_size) {
3839 select_subtitle(mpctx);
3840 if(subdata)
3841 switch (stream_dump_type) {
3842 case 3: list_sub_file(subdata); break;
3843 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3844 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3845 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3846 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3847 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3851 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3852 filename_recode(mpctx->filename));
3853 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3854 if (mpctx->sh_video) {
3855 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3856 if (mpctx->sh_video->format >= 0x20202020)
3857 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3858 else
3859 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3860 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3861 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3862 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3863 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3864 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3866 if (mpctx->sh_audio) {
3867 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3868 if (mpctx->sh_audio->format >= 0x20202020)
3869 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3870 else
3871 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3872 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3873 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3874 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3876 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2f\n", mpctx->timeline ?
3877 mpctx->timeline[mpctx->num_timeline_parts].start :
3878 demuxer_get_time_length(mpctx->demuxer));
3879 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n",
3880 mpctx->stream->seek && (!mpctx->demuxer || mpctx->demuxer->seekable));
3881 if (mpctx->demuxer) {
3882 if (mpctx->demuxer->num_chapters == 0)
3883 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3884 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3887 if(!mpctx->sh_video) goto main; // audio-only
3889 if(!reinit_video_chain(mpctx)) {
3890 if(!mpctx->sh_video){
3891 if(!mpctx->sh_audio) goto goto_next_file;
3892 goto main; // exit_player(_("Fatal error"));
3896 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3897 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3899 #ifdef CONFIG_FREETYPE
3900 force_load_font = 1;
3901 #endif
3903 //================== MAIN: ==========================
3904 main:
3905 current_module="main";
3907 if(playing_msg) {
3908 char* msg = property_expand_string(mpctx, playing_msg);
3909 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3910 free(msg);
3914 // Disable the term OSD in verbose mode
3915 if(verbose) term_osd = 0;
3918 //int frame_corr_num=0; //
3919 //float v_frame=0; // Video
3920 //float num_frames=0; // number of frames played
3922 int frame_time_remaining=0; // flag
3923 int blit_frame=0;
3925 // Make sure old OSD does not stay around,
3926 // e.g. with -fixed-vo and same-resolution files
3927 clear_osd_msgs();
3928 update_osd_msg(mpctx);
3930 //================ SETUP AUDIO ==========================
3932 if(mpctx->sh_audio){
3933 reinit_audio_chain(mpctx);
3934 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3935 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3938 current_module="av_init";
3940 if(mpctx->sh_video){
3941 mpctx->sh_video->timer=0;
3942 if (! ignore_start)
3943 audio_delay += mpctx->sh_video->stream_delay;
3945 if(mpctx->sh_audio){
3946 if (start_volume >= 0)
3947 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3948 if (! ignore_start)
3949 audio_delay -= mpctx->sh_audio->stream_delay;
3950 mpctx->delay=-audio_delay;
3953 if(!mpctx->sh_audio){
3954 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Audio: no sound\n");
3955 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3956 ds_free_packs(mpctx->d_audio); // free buffered chunks
3957 //mpctx->d_audio->id=-2; // do not read audio chunks
3958 //uninit_player(mpctx, INITIALIZED_AO); // close device
3960 if(!mpctx->sh_video){
3961 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Video: no video\n");
3962 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3963 ds_free_packs(mpctx->d_video);
3964 mpctx->d_video->id=-2;
3965 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3968 if (!mpctx->sh_video && !mpctx->sh_audio)
3969 goto goto_next_file;
3971 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3972 if(force_fps && mpctx->sh_video){
3973 vo_fps = mpctx->sh_video->fps=force_fps;
3974 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3975 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx->sh_video->fps,mpctx->sh_video->frametime);
3978 mp_input_set_section(mpctx->input, NULL);
3979 //TODO: add desired (stream-based) sections here
3980 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3981 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3983 //==================== START PLAYING =======================
3985 if(opts->loop_times>1) opts->loop_times--; else
3986 if(opts->loop_times==1) opts->loop_times = -1;
3988 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Starting playback...\n");
3990 total_time_usage_start=GetTimer();
3991 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3992 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3993 play_n_frames=play_n_frames_mf;
3995 if(play_n_frames==0){
3996 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
3999 // If there's a timeline force an absolute seek to initialize state
4000 if (seek_to_sec || mpctx->timeline) {
4001 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
4002 end_at.pos += seek_to_sec;
4005 if (end_at.type == END_AT_SIZE) {
4006 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Option -endpos in MPlayer does not yet support size units.\n");
4007 end_at.type = END_AT_NONE;
4010 #ifdef CONFIG_DVDNAV
4011 mp_dvdnav_context_free(mpctx);
4012 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4013 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
4014 mp_dvdnav_cell_has_changed(mpctx->stream,1);
4016 #endif
4018 get_relative_time(mpctx); // reset current delta
4019 mpctx->time_frame = 0;
4020 mpctx->drop_message_shown = 0;
4021 mpctx->update_video_immediately = true;
4022 mpctx->total_avsync_change = 0;
4023 // Make sure VO knows current pause state
4024 if (mpctx->sh_video)
4025 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
4026 NULL);
4028 while(!mpctx->stop_play){
4029 float aq_sleep_time=0;
4031 if(dvd_last_chapter>0) {
4032 int cur_chapter = get_current_chapter(mpctx);
4033 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
4034 goto goto_next_file;
4037 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
4038 mpctx->sh_audio = mpctx->d_audio->sh;
4039 mpctx->sh_audio->ds = mpctx->d_audio;
4040 reinit_audio_chain(mpctx);
4043 /*========================== PLAY AUDIO ============================*/
4045 if (mpctx->sh_audio && !mpctx->paused)
4046 if (!fill_audio_out_buffers(mpctx))
4047 // at eof, all audio at least written to ao
4048 if (!mpctx->sh_video)
4049 mpctx->stop_play = AT_END_OF_FILE;
4052 if(!mpctx->sh_video) {
4053 // handle audio-only case:
4054 double a_pos=0;
4055 // sh_audio can be NULL due to video stream switching
4056 // TODO: handle this better
4057 if (mpctx->sh_audio)
4058 a_pos = playing_audio_pts(mpctx);
4060 print_status(mpctx, a_pos, false);
4062 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
4063 mpctx->stop_play = PT_NEXT_ENTRY;
4064 update_subtitles(mpctx, &mpctx->opts, NULL, a_pos, mpctx->video_offset,
4065 mpctx->d_sub, 0);
4066 update_osd_msg(mpctx);
4068 } else {
4070 /*========================== PLAY VIDEO ============================*/
4072 vo_pts=mpctx->sh_video->timer*90000.0;
4073 vo_fps=mpctx->sh_video->fps;
4075 blit_frame = mpctx->video_out->frame_loaded;
4076 if (!blit_frame) {
4077 double frame_time = update_video(mpctx);
4078 blit_frame = mpctx->video_out->frame_loaded;
4079 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
4080 if (mpctx->sh_video->vf_initialized < 0) {
4081 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "\nFATAL: Could not initialize video filters (-vf) or video output (-vo).\n");
4082 mpctx->stop_play = PT_NEXT_ENTRY;
4083 goto goto_next_file;
4085 if (blit_frame) {
4086 struct sh_video *sh_video = mpctx->sh_video;
4087 update_subtitles(mpctx, &mpctx->opts, sh_video, sh_video->pts,
4088 mpctx->video_offset, mpctx->d_sub, 0);
4089 update_teletext(sh_video, mpctx->demuxer, 0);
4090 update_osd_msg(mpctx);
4091 struct vf_instance *vf = mpctx->sh_video->vfilter;
4092 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
4093 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
4094 vo_osd_changed(0);
4096 if (frame_time < 0)
4097 mpctx->stop_play = AT_END_OF_FILE;
4098 else {
4099 if (mpctx->update_video_immediately) {
4100 // Show this frame immediately, rest normally
4101 mpctx->update_video_immediately = false;
4102 } else {
4103 mpctx->time_frame += frame_time / opts->playback_speed;
4104 adjust_sync(mpctx, frame_time);
4108 if (mpctx->timeline) {
4109 struct timeline_part *next = mpctx->timeline + mpctx->timeline_part + 1;
4110 if (mpctx->sh_video->pts >= next->start
4111 || mpctx->stop_play == AT_END_OF_FILE
4112 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
4113 seek(mpctx, next->start, SEEK_ABSOLUTE);
4114 continue;
4118 // ==========================================================================
4120 // current_module="draw_osd";
4121 // if(vo_config_count) mpctx->video_out->draw_osd();
4123 current_module="vo_check_events";
4124 vo_check_events(mpctx->video_out);
4126 #ifdef CONFIG_X11
4127 if (stop_xscreensaver) {
4128 current_module = "stop_xscreensaver";
4129 xscreensaver_heartbeat(mpctx->x11_state);
4131 #elif _WIN32
4132 current_module = "stop_screensaver";
4133 SetThreadExecutionState(ES_DISPLAY_REQUIRED);
4134 #endif
4135 if (heartbeat_cmd) {
4136 static unsigned last_heartbeat;
4137 unsigned now = GetTimerMS();
4138 if (now - last_heartbeat > 30000) {
4139 last_heartbeat = now;
4140 system(heartbeat_cmd);
4144 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
4146 //====================== FLIP PAGE (VIDEO BLT): =========================
4148 current_module="flip_page";
4149 if (!frame_time_remaining && blit_frame) {
4150 unsigned int t2=GetTimer();
4151 unsigned int pts_us = mpctx->last_time + mpctx->time_frame * 1e6;
4152 int duration = -1;
4153 double pts2 = mpctx->video_out->next_pts2;
4154 if (pts2 != MP_NOPTS_VALUE && opts->correct_pts) {
4155 // expected A/V sync correction is ignored
4156 double diff = (pts2 - mpctx->sh_video->pts);
4157 diff /= opts->playback_speed;
4158 if (mpctx->time_frame < 0)
4159 diff += mpctx->time_frame;
4160 if (diff < 0)
4161 diff = 0;
4162 if (diff > 10)
4163 diff = 10;
4164 duration = diff * 1e6;
4166 vo_flip_page(mpctx->video_out, pts_us|1, duration);
4168 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
4169 vout_time_usage += mpctx->last_vo_flip_duration;
4170 if (mpctx->video_out->driver->flip_page_timed) {
4171 // No need to adjust sync based on flip speed
4172 mpctx->last_vo_flip_duration = 0;
4173 // For print_status - VO call finishing early is OK for sync
4174 mpctx->time_frame -= get_relative_time(mpctx);
4176 print_status(mpctx, MP_NOPTS_VALUE, true);
4178 else
4179 print_status(mpctx, MP_NOPTS_VALUE, false);
4181 //============================ Auto QUALITY ============================
4183 /*Output quality adjustments:*/
4184 if(auto_quality>0){
4185 current_module="autoq";
4186 // float total=0.000001f * (GetTimer()-aq_total_time);
4187 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4188 if(output_quality<auto_quality && aq_sleep_time>0)
4189 ++output_quality;
4190 else
4191 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4192 if(output_quality>1 && aq_sleep_time<0)
4193 --output_quality;
4194 else
4195 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4196 output_quality=0;
4197 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4198 set_video_quality(mpctx->sh_video,output_quality);
4201 if (!frame_time_remaining && blit_frame) {
4202 if (play_n_frames >= 0) {
4203 --play_n_frames;
4204 if (play_n_frames <= 0)
4205 mpctx->stop_play = PT_NEXT_ENTRY;
4207 if (mpctx->step_frames > 0) {
4208 mpctx->step_frames--;
4209 if (mpctx->step_frames == 0)
4210 pause_player(mpctx);
4215 // FIXME: add size based support for -endpos
4216 if (end_at.type == END_AT_TIME &&
4217 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
4218 mpctx->stop_play = PT_NEXT_ENTRY;
4220 } // end if(mpctx->sh_video)
4222 #ifdef CONFIG_DVDNAV
4223 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4224 nav_highlight_t hl;
4225 mp_dvdnav_get_highlight (mpctx->stream, &hl);
4226 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4227 vo_osd_changed (OSDTYPE_DVDNAV);
4229 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
4230 double ar = -1.0;
4231 if (stream_control (mpctx->demuxer->stream,
4232 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
4233 != STREAM_UNSUPPORTED)
4234 mpctx->sh_video->stream_aspect = ar;
4237 #endif
4239 //================= Keyboard events, SEEKing ====================
4241 current_module="key_events";
4244 while (1) {
4245 mp_cmd_t* cmd;
4246 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
4247 run_command(mpctx, cmd);
4248 mp_cmd_free(cmd);
4249 if (mpctx->stop_play)
4250 break;
4252 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
4253 || mpctx->abs_seek_pos)
4254 break;
4255 if (mpctx->sh_video) {
4256 update_osd_msg(mpctx);
4257 int hack = vo_osd_changed(0);
4258 vo_osd_changed(hack);
4259 if (hack)
4260 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
4261 add_step_frame(mpctx);
4262 break;
4264 else
4265 vo_osd_changed(0);
4267 pause_loop(mpctx);
4271 // handle -sstep
4272 if (step_sec > 0 && !mpctx->paused) {
4273 mpctx->osd_function=OSD_FFW;
4274 mpctx->rel_seek_secs+=step_sec;
4277 edl_update(mpctx);
4279 /* Looping. */
4280 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
4281 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
4283 if(opts->loop_times>1) opts->loop_times--; else
4284 if(opts->loop_times==1) opts->loop_times=-1;
4285 play_n_frames=play_n_frames_mf;
4286 mpctx->stop_play=0;
4287 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
4290 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
4291 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
4293 mpctx->rel_seek_secs=0;
4294 mpctx->abs_seek_pos=0;
4297 } // while(!mpctx->stop_play)
4299 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4301 #ifdef CONFIG_DVBIN
4302 if(mpctx->dvbin_reopen)
4304 mpctx->stop_play = 0;
4305 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4306 cache_uninit(mpctx->stream);
4307 mpctx->dvbin_reopen = 0;
4308 goto goto_enable_cache;
4310 #endif
4313 goto_next_file: // don't jump here after ao/vo/getch initialization!
4315 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4317 if(benchmark){
4318 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4319 double total_time_usage;
4320 total_time_usage_start=GetTimer()-total_time_usage_start;
4321 total_time_usage = (float)total_time_usage_start*0.000001;
4322 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4323 video_time_usage,vout_time_usage,audio_time_usage,
4324 total_time_usage-tot,total_time_usage);
4325 if(total_time_usage>0.0)
4326 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4327 100.0*video_time_usage/total_time_usage,
4328 100.0*vout_time_usage/total_time_usage,
4329 100.0*audio_time_usage/total_time_usage,
4330 100.0*(total_time_usage-tot)/total_time_usage,
4331 100.0);
4332 if(total_frame_cnt && frame_dropping)
4333 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4334 total_frame_cnt-drop_frame_cnt,
4335 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4336 drop_frame_cnt,
4337 100*drop_frame_cnt/total_frame_cnt,
4338 total_frame_cnt,
4339 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4342 // time to uninit all, except global stuff:
4343 uninit_player(mpctx, INITIALIZED_ALL-(opts->fixed_vo?INITIALIZED_VO:0));
4345 if(mpctx->set_of_sub_size > 0) {
4346 current_module="sub_free";
4347 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4348 sub_free(mpctx->set_of_subtitles[i]);
4349 #ifdef CONFIG_ASS
4350 if(mpctx->set_of_ass_tracks[i])
4351 ass_free_track( mpctx->set_of_ass_tracks[i] );
4352 #endif
4354 mpctx->set_of_sub_size = 0;
4356 vo_sub_last = vo_sub=NULL;
4357 subdata=NULL;
4358 #ifdef CONFIG_ASS
4359 ass_track = NULL;
4360 if(ass_library)
4361 ass_clear_fonts(ass_library);
4362 #endif
4364 if (!mpctx->stop_play) // In case some goto jumped here...
4365 mpctx->stop_play = PT_NEXT_ENTRY;
4367 int playtree_direction = 1;
4369 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4370 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4371 play_tree_iter_free(mpctx->playtree_iter);
4372 mpctx->playtree_iter = NULL;
4374 mpctx->play_tree_step = 1;
4375 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4376 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4377 if(mpctx->playtree_iter) {
4378 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4379 play_tree_iter_free(mpctx->playtree_iter);
4380 mpctx->playtree_iter = NULL;
4383 } else if (mpctx->stop_play == PT_STOP) {
4384 play_tree_iter_free(mpctx->playtree_iter);
4385 mpctx->playtree_iter = NULL;
4386 } else { // NEXT PREV SRC
4387 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4390 while(mpctx->playtree_iter != NULL) {
4391 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4392 if(mpctx->filename == NULL) {
4393 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4394 play_tree_iter_free(mpctx->playtree_iter);
4395 mpctx->playtree_iter = NULL;
4397 } else
4398 break;
4401 if(mpctx->playtree_iter != NULL || player_idle_mode){
4402 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4403 mpctx->stop_play = 0;
4404 goto play_next_file;
4408 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4410 return 1;
4412 #endif /* DISABLE_MAIN */