Ignore svn changes r30217, r30218
[mplayer/glamo.git] / mplayer.c
blobac97aed389e0ea73a6c52b7df5ce91b855ffc5d2
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdbool.h>
8 #include "config.h"
9 #include "talloc.h"
11 #if defined(__MINGW32__) || defined(__CYGWIN__)
12 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
13 #include <windows.h>
14 #endif
15 #include <string.h>
16 #include <unistd.h>
18 // #include <sys/mman.h>
19 #include <sys/types.h>
20 #ifndef __MINGW32__
21 #include <sys/ioctl.h>
22 #include <sys/wait.h>
23 #else
24 #define SIGHUP 1 /* hangup */
25 #define SIGQUIT 3 /* quit */
26 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
27 #define SIGBUS 10 /* bus error */
28 #define SIGPIPE 13 /* broken pipe */
29 #endif
31 #include <sys/time.h>
32 #include <sys/stat.h>
34 #include <signal.h>
35 #include <time.h>
36 #include <fcntl.h>
37 #include <limits.h>
39 #include <errno.h>
41 #include "mp_msg.h"
42 #include "av_log.h"
44 #define HELP_MP_DEFINE_STATIC
45 #include "help_mp.h"
47 #include "m_option.h"
48 #include "m_config.h"
49 #include "mplayer.h"
50 #include "access_mpcontext.h"
51 #include "m_property.h"
53 #include "cfg-mplayer-def.h"
55 #include "ffmpeg_files/intreadwrite.h"
56 #include "libavutil/avstring.h"
58 #include "subreader.h"
60 #include "mp_osd.h"
61 #include "libvo/video_out.h"
63 #include "libvo/font_load.h"
64 #include "libvo/sub.h"
66 #ifdef CONFIG_X11
67 #include "libvo/x11_common.h"
68 #endif
70 #include "libao2/audio_out.h"
72 #include "codec-cfg.h"
74 #include "edl.h"
76 #include "spudec.h"
77 #include "vobsub.h"
79 #include "osdep/getch2.h"
80 #include "osdep/timer.h"
81 #include "osdep/findfiles.h"
83 #include "input/input.h"
85 const int under_mencoder = 0;
86 int slave_mode=0;
87 int player_idle_mode=0;
88 int quiet=0;
89 int enable_mouse_movements=0;
90 float start_volume = -1;
92 #include "osdep/priority.h"
94 char *heartbeat_cmd;
96 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
98 #ifdef HAVE_RTC
99 #ifdef __linux__
100 #include <linux/rtc.h>
101 #else
102 #include <rtc.h>
103 #define RTC_IRQP_SET RTCIO_IRQP_SET
104 #define RTC_PIE_ON RTCIO_PIE_ON
105 #endif /* __linux__ */
106 #endif /* HAVE_RTC */
108 #include "stream/tv.h"
109 #include "stream/stream_radio.h"
110 #ifdef CONFIG_DVBIN
111 #include "stream/dvbin.h"
112 #endif
113 #include "stream/cache2.h"
115 //**************************************************************************//
116 // Playtree
117 //**************************************************************************//
118 #include "playtree.h"
119 #include "playtreeparser.h"
121 //**************************************************************************//
122 // Config
123 //**************************************************************************//
124 #include "parser-cfg.h"
125 #include "parser-mpcmd.h"
127 //**************************************************************************//
128 // Config file
129 //**************************************************************************//
131 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
133 #include "get_path.h"
135 //**************************************************************************//
136 //**************************************************************************//
137 // Input media streaming & demultiplexer:
138 //**************************************************************************//
140 static int max_framesize=0;
142 #include "stream/stream.h"
143 #include "libmpdemux/demuxer.h"
144 #include "libmpdemux/stheader.h"
146 #ifdef CONFIG_DVDREAD
147 #include "stream/stream_dvd.h"
148 #endif
149 #include "stream/stream_dvdnav.h"
151 #include "libmpcodecs/dec_audio.h"
152 #include "libmpcodecs/dec_video.h"
153 #include "libmpcodecs/mp_image.h"
154 #include "libmpcodecs/vf.h"
155 #include "libmpcodecs/vd.h"
157 #include "mixer.h"
159 #include "mp_core.h"
160 #include "options.h"
161 #include "defaultopts.h"
163 #define Exit_SIGILL_RTCpuSel _(\
164 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
165 " It may be a bug in our new runtime CPU-detection code...\n"\
166 " Please read DOCS/HTML/en/bugreports.html.\n")
168 #define Exit_SIGILL _(\
169 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
170 " It usually happens when you run it on a CPU different than the one it was\n"\
171 " compiled/optimized for.\n"\
172 " Verify this!\n")
174 #define Exit_SIGSEGV_SIGFPE _(\
175 "- MPlayer crashed by bad usage of CPU/FPU/RAM.\n"\
176 " Recompile MPlayer with --enable-debug and make a 'gdb' backtrace and\n"\
177 " disassembly. Details in DOCS/HTML/en/bugreports_what.html#bugreports_crash.\n")
179 #define Exit_SIGCRASH _(\
180 "- MPlayer crashed. This shouldn't happen.\n"\
181 " It can be a bug in the MPlayer code _or_ in your drivers _or_ in your\n"\
182 " gcc version. If you think it's MPlayer's fault, please read\n"\
183 " DOCS/HTML/en/bugreports.html and follow the instructions there. We can't and\n"\
184 " won't help unless you provide this information when reporting a possible bug.\n")
186 #define SystemTooSlow _("\n\n"\
187 " ************************************************\n"\
188 " **** Your system is too SLOW to play this! ****\n"\
189 " ************************************************\n\n"\
190 "Possible reasons, problems, workarounds:\n"\
191 "- Most common: broken/buggy _audio_ driver\n"\
192 " - Try -ao sdl or use the OSS emulation of ALSA.\n"\
193 " - Experiment with different values for -autosync, 30 is a good start.\n"\
194 "- Slow video output\n"\
195 " - Try a different -vo driver (-vo help for a list) or try -framedrop!\n"\
196 "- Slow CPU\n"\
197 " - Don't try to play a big DVD/DivX on a slow CPU! Try some of the lavdopts,\n"\
198 " e.g. -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all.\n"\
199 "- Broken file\n"\
200 " - Try various combinations of -nobps -ni -forceidx -mc 0.\n"\
201 "- Slow media (NFS/SMB mounts, DVD, VCD etc)\n"\
202 " - Try -cache 8192.\n"\
203 "- Are you using -cache to play a non-interleaved AVI file?\n"\
204 " - Try -nocache.\n"\
205 "Read DOCS/HTML/en/video.html for tuning/speedup tips.\n"\
206 "If none of this helps you, read DOCS/HTML/en/bugreports.html.\n\n")
209 //**************************************************************************//
210 //**************************************************************************//
212 // Common FIFO functions, and keyboard/event FIFO code
213 #include "mp_fifo.h"
214 int noconsolecontrols=0;
215 //**************************************************************************//
217 // benchmark:
218 double video_time_usage=0;
219 double vout_time_usage=0;
220 static double audio_time_usage=0;
221 static int total_time_usage_start=0;
222 static int total_frame_cnt=0;
223 static int drop_frame_cnt=0; // total number of dropped frames
224 int benchmark=0;
226 // options:
227 int auto_quality=0;
228 static int output_quality=0;
230 static int list_properties = 0;
232 int term_osd = 1;
233 static char* term_osd_esc = "\x1b[A\r\x1b[K";
234 static char* playing_msg = NULL;
235 // seek:
236 static double seek_to_sec;
237 static off_t seek_to_byte=0;
238 static off_t step_sec=0;
240 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
242 // A/V sync:
243 int autosync=0; // 30 might be a good default value.
245 // codecs:
246 char **audio_codec_list=NULL; // override audio codec
247 char **video_codec_list=NULL; // override video codec
248 char **audio_fm_list=NULL; // override audio codec family
249 char **video_fm_list=NULL; // override video codec family
251 // demuxer:
252 extern char *demuxer_name; // override demuxer
253 extern char *audio_demuxer_name; // override audio demuxer
254 extern char *sub_demuxer_name; // override sub demuxer
256 // this dvdsub_id was selected via slang
257 // use this to allow dvdnav to follow -slang across stream resets,
258 // in particular the subtitle ID for a language changes
259 int dvdsub_lang_id;
260 int vobsub_id=-1;
261 char* audio_lang=NULL;
262 char* dvdsub_lang=NULL;
263 static char* spudec_ifo=NULL;
264 int forced_subs_only=0;
265 int file_filter=1;
267 // cache2:
268 int stream_cache_size=-1;
269 #ifdef CONFIG_STREAM_CACHE
270 extern int cache_fill_status;
272 float stream_cache_min_percent=20.0;
273 float stream_cache_seek_min_percent=50.0;
274 #else
275 #define cache_fill_status 0
276 #endif
278 // dump:
279 static char *stream_dump_name="stream.dump";
280 int stream_dump_type=0;
282 // A-V sync:
283 static float default_max_pts_correction=-1;//0.01f;
284 float audio_delay=0;
285 static int ignore_start=0;
287 static int softsleep=0;
289 double force_fps=0;
290 static int force_srate=0;
291 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
292 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
293 static int play_n_frames=-1;
294 static int play_n_frames_mf=-1;
296 // sub:
297 char *font_name=NULL;
298 char *sub_font_name=NULL;
299 extern int font_fontconfig;
300 float font_factor=0.75;
301 char **sub_name=NULL;
302 float sub_delay=0;
303 float sub_fps=0;
304 int sub_auto = 1;
305 char *vobsub_name=NULL;
306 /*DSP!!char *dsp=NULL;*/
307 int subcc_enabled=0;
308 int suboverlap_enabled = 1;
310 #include "ass_mp.h"
312 char* current_module=NULL; // for debugging
315 // ---
317 #ifdef CONFIG_MENU
318 #include "m_struct.h"
319 #include "libmenu/menu.h"
320 void vf_menu_pause_update(struct vf_instance* vf);
321 extern vf_info_t vf_info_menu;
322 static const vf_info_t* const libmenu_vfs[] = {
323 &vf_info_menu,
324 NULL
326 static vf_instance_t* vf_menu = NULL;
327 int use_menu = 0;
328 static char* menu_cfg = NULL;
329 static char* menu_root = "main";
330 #endif
333 #ifdef HAVE_RTC
334 static int nortc = 1;
335 static char* rtc_device;
336 #endif
338 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
339 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
340 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
341 int use_filedir_conf;
342 int use_filename_title;
344 #include "mpcommon.h"
345 #include "command.h"
347 #include "metadata.h"
349 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
351 const void *mpctx_get_video_out(MPContext *mpctx)
353 return mpctx->video_out;
356 const void *mpctx_get_audio_out(MPContext *mpctx)
358 return mpctx->audio_out;
361 void *mpctx_get_demuxer(MPContext *mpctx)
363 return mpctx->demuxer;
366 void *mpctx_get_playtree_iter(MPContext *mpctx)
368 return mpctx->playtree_iter;
371 void *mpctx_get_mixer(MPContext *mpctx)
373 return &mpctx->mixer;
376 int mpctx_get_global_sub_size(MPContext *mpctx)
378 return mpctx->global_sub_size;
381 int mpctx_get_osd_function(MPContext *mpctx)
383 return mpctx->osd_function;
386 static float get_relative_time(struct MPContext *mpctx)
388 unsigned int new_time = GetTimer();
389 unsigned int delta = new_time - mpctx->last_time;
390 mpctx->last_time = new_time;
391 return delta * 0.000001;
394 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
395 switch (type)
397 /* check for valid video stream */
398 case META_VIDEO_CODEC:
399 case META_VIDEO_BITRATE:
400 case META_VIDEO_RESOLUTION:
402 if (!mpctx->sh_video)
403 return 0;
404 break;
407 /* check for valid audio stream */
408 case META_AUDIO_CODEC:
409 case META_AUDIO_BITRATE:
410 case META_AUDIO_SAMPLES:
412 if (!mpctx->sh_audio)
413 return 0;
414 break;
417 /* check for valid demuxer */
418 case META_INFO_TITLE:
419 case META_INFO_ARTIST:
420 case META_INFO_ALBUM:
421 case META_INFO_YEAR:
422 case META_INFO_COMMENT:
423 case META_INFO_TRACK:
424 case META_INFO_GENRE:
426 if (!mpctx->demuxer)
427 return 0;
428 break;
431 default:
432 break;
435 return 1;
438 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
439 char **info = mpctx->demuxer->info;
440 int n;
442 if (!info || !tag)
443 return NULL;
445 for (n = 0; info[2*n] != NULL ; n++)
446 if (!strcasecmp (info[2*n], tag))
447 break;
449 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
452 char *get_metadata(struct MPContext *mpctx, metadata_t type)
454 char *meta = NULL;
455 sh_audio_t * const sh_audio = mpctx->sh_audio;
456 sh_video_t * const sh_video = mpctx->sh_video;
458 if (!is_valid_metadata_type(mpctx, type))
459 return NULL;
461 switch (type)
463 case META_NAME:
465 return strdup (mp_basename2 (mpctx->filename));
468 case META_VIDEO_CODEC:
470 if (sh_video->format == 0x10000001)
471 meta = strdup ("mpeg1");
472 else if (sh_video->format == 0x10000002)
473 meta = strdup ("mpeg2");
474 else if (sh_video->format == 0x10000004)
475 meta = strdup ("mpeg4");
476 else if (sh_video->format == 0x10000005)
477 meta = strdup ("h264");
478 else if (sh_video->format >= 0x20202020)
480 meta = malloc (8);
481 sprintf (meta, "%.4s", (char *) &sh_video->format);
483 else
485 meta = malloc (8);
486 sprintf (meta, "0x%08X", sh_video->format);
488 return meta;
491 case META_VIDEO_BITRATE:
493 meta = malloc (16);
494 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
495 return meta;
498 case META_VIDEO_RESOLUTION:
500 meta = malloc (16);
501 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
502 return meta;
505 case META_AUDIO_CODEC:
507 if (sh_audio->codec && sh_audio->codec->name)
508 meta = strdup (sh_audio->codec->name);
509 return meta;
512 case META_AUDIO_BITRATE:
514 meta = malloc (16);
515 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
516 return meta;
519 case META_AUDIO_SAMPLES:
521 meta = malloc (16);
522 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
523 return meta;
526 /* check for valid demuxer */
527 case META_INFO_TITLE:
528 return get_demuxer_info(mpctx, "Title");
530 case META_INFO_ARTIST:
531 return get_demuxer_info(mpctx, "Artist");
533 case META_INFO_ALBUM:
534 return get_demuxer_info(mpctx, "Album");
536 case META_INFO_YEAR:
537 return get_demuxer_info(mpctx, "Year");
539 case META_INFO_COMMENT:
540 return get_demuxer_info(mpctx, "Comment");
542 case META_INFO_TRACK:
543 return get_demuxer_info(mpctx, "Track");
545 case META_INFO_GENRE:
546 return get_demuxer_info(mpctx, "Genre");
548 default:
549 break;
552 return meta;
555 /// step size of mixer changes
556 int volstep = 3;
558 #ifdef CONFIG_DVDNAV
559 static void mp_dvdnav_context_free(MPContext *ctx){
560 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
561 ctx->nav_smpi = NULL;
562 if (ctx->nav_buffer) free(ctx->nav_buffer);
563 ctx->nav_buffer = NULL;
564 ctx->nav_start = NULL;
565 ctx->nav_in_size = 0;
567 #endif
569 void uninit_player(struct MPContext *mpctx, unsigned int mask){
570 mask &= mpctx->initialized_flags;
572 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
574 if(mask&INITIALIZED_ACODEC){
575 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
576 current_module="uninit_acodec";
577 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
578 mpctx->sh_audio=NULL;
579 mpctx->mixer.afilter = NULL;
582 if(mask&INITIALIZED_VCODEC){
583 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
584 current_module="uninit_vcodec";
585 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
586 mpctx->sh_video=NULL;
587 #ifdef CONFIG_MENU
588 vf_menu=NULL;
589 #endif
592 if(mask&INITIALIZED_DEMUXER){
593 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
594 current_module="free_demuxer";
595 if (mpctx->num_sources) {
596 mpctx->demuxer = mpctx->sources[0].demuxer;
597 for (int i = 1; i < mpctx->num_sources; i++) {
598 free_stream(mpctx->sources[i].stream);
599 free_demuxer(mpctx->sources[i].demuxer);
602 talloc_free(mpctx->sources);
603 mpctx->sources = NULL;
604 mpctx->num_sources = 0;
605 talloc_free(mpctx->timeline);
606 mpctx->timeline = NULL;
607 mpctx->num_timeline_parts = 0;
608 talloc_free(mpctx->chapters);
609 mpctx->chapters = NULL;
610 mpctx->num_chapters = 0;
611 mpctx->video_offset = 0;
612 if(mpctx->demuxer){
613 mpctx->stream=mpctx->demuxer->stream;
614 free_demuxer(mpctx->demuxer);
616 mpctx->demuxer=NULL;
619 // kill the cache process:
620 if(mask&INITIALIZED_STREAM){
621 mpctx->initialized_flags&=~INITIALIZED_STREAM;
622 current_module="uninit_stream";
623 if(mpctx->stream) free_stream(mpctx->stream);
624 mpctx->stream=NULL;
627 if(mask&INITIALIZED_VO){
628 mpctx->initialized_flags&=~INITIALIZED_VO;
629 current_module="uninit_vo";
630 vo_destroy(mpctx->video_out);
631 mpctx->video_out=NULL;
632 #ifdef CONFIG_DVDNAV
633 mp_dvdnav_context_free(mpctx);
634 #endif
637 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
638 if(mask&INITIALIZED_GETCH2){
639 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
640 current_module="uninit_getch2";
641 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
642 // restore terminal:
643 getch2_disable();
646 if(mask&INITIALIZED_VOBSUB){
647 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
648 current_module="uninit_vobsub";
649 if(vo_vobsub) vobsub_close(vo_vobsub);
650 vo_vobsub=NULL;
653 if (mask&INITIALIZED_SPUDEC){
654 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
655 current_module="uninit_spudec";
656 spudec_free(vo_spudec);
657 vo_spudec=NULL;
660 if(mask&INITIALIZED_AO){
661 mpctx->initialized_flags&=~INITIALIZED_AO;
662 current_module="uninit_ao";
663 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
664 if (mpctx->audio_out)
665 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
666 mpctx->audio_out=NULL;
669 current_module=NULL;
672 void exit_player_with_rc(struct MPContext *mpctx, exit_reason_t how, int rc)
674 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
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;
1053 #ifdef CONFIG_ASS
1054 ASS_Track *asst = 0;
1055 #endif
1057 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1058 return;
1061 subd = sub_read_file(filename, fps);
1062 #ifdef CONFIG_ASS
1063 if (opts->ass_enabled)
1064 #ifdef CONFIG_ICONV
1065 asst = ass_read_file(ass_library, filename, sub_cp);
1066 #else
1067 asst = ass_read_file(ass_library, filename, 0);
1068 #endif
1069 if (opts->ass_enabled && subd && !asst)
1070 asst = ass_read_subdata(ass_library, subd, fps);
1072 if (!asst && !subd)
1073 #else
1074 if(!subd)
1075 #endif
1076 mp_tmsg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, "Cannot load subtitles: %s\n",
1077 filename_recode(filename));
1079 #ifdef CONFIG_ASS
1080 if (!asst && !subd) return;
1081 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1082 #else
1083 if (!subd) return;
1084 #endif
1085 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1086 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1087 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1088 filename_recode(filename));
1089 ++mpctx->set_of_sub_size;
1090 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "SUB: Added subtitle file (%d): %s\n", mpctx->set_of_sub_size,
1091 filename_recode(filename));
1094 void init_vo_spudec(struct MPContext *mpctx)
1096 if (vo_spudec)
1097 spudec_free(vo_spudec);
1098 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1099 vo_spudec = NULL;
1100 if (spudec_ifo) {
1101 unsigned int palette[16], width, height;
1102 current_module="spudec_init_vobsub";
1103 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1104 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1107 #ifdef CONFIG_DVDREAD
1108 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1109 current_module="spudec_init_dvdread";
1110 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1111 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1112 NULL, 0);
1114 #endif
1116 #ifdef CONFIG_DVDNAV
1117 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1118 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1119 current_module="spudec_init_dvdnav";
1120 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1122 #endif
1124 if (vo_spudec==NULL) {
1125 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1126 current_module="spudec_init_normal";
1127 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1128 spudec_set_font_factor(vo_spudec,font_factor);
1131 if (vo_spudec!=NULL) {
1132 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1133 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1138 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1139 * make it all work is to use the builtin SDL-bootstrap code, which
1140 * will be done automatically by replacing our main() if we include SDL.h.
1142 #if defined(__APPLE__) && defined(CONFIG_SDL)
1143 #ifdef CONFIG_SDL_SDL_H
1144 #include <SDL/SDL.h>
1145 #else
1146 #include <SDL.h>
1147 #endif
1148 #endif
1151 * \brief append a formatted string
1152 * \param buf buffer to print into
1153 * \param pos position of terminating 0 in buf
1154 * \param len maximum number of characters in buf, not including terminating 0
1155 * \param format printf format string
1157 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1159 va_list va;
1160 va_start(va, format);
1161 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1162 va_end(va);
1163 if (*pos >= len ) {
1164 buf[len] = 0;
1165 *pos = len;
1170 * \brief append time in the hh:mm:ss.f format
1171 * \param buf buffer to print into
1172 * \param pos position of terminating 0 in buf
1173 * \param len maximum number of characters in buf, not including terminating 0
1174 * \param time time value to convert/append
1176 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1177 long tenths = 10 * time;
1178 int f1 = tenths % 10;
1179 int ss = (tenths / 10) % 60;
1180 int mm = (tenths / 600) % 60;
1181 int hh = tenths / 36000;
1182 if (time <= 0) {
1183 saddf(buf, pos, len, "unknown");
1184 return;
1186 if (hh > 0)
1187 saddf(buf, pos, len, "%2d:", hh);
1188 if (hh > 0 || mm > 0)
1189 saddf(buf, pos, len, "%02d:", mm);
1190 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1193 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1195 struct MPOpts *opts = &mpctx->opts;
1196 sh_video_t * const sh_video = mpctx->sh_video;
1198 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1199 a_pos = playing_audio_pts(mpctx);
1200 if (mpctx->sh_audio && sh_video && at_frame) {
1201 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1202 if (mpctx->time_frame > 0)
1203 mpctx->last_av_difference += mpctx->time_frame * opts->playback_speed;
1204 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1205 && !mpctx->drop_message_shown) {
1206 mp_tmsg(MSGT_AVSYNC,MSGL_WARN,SystemTooSlow);
1207 mpctx->drop_message_shown = true;
1210 if (quiet)
1211 return;
1214 int width;
1215 char *line;
1216 unsigned pos = 0;
1217 get_screen_size();
1218 if (screen_width > 0)
1219 width = screen_width;
1220 else
1221 width = 80;
1222 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1223 /* Windows command line is broken (MinGW's rxvt works, but we
1224 * should not depend on that). */
1225 width--;
1226 #endif
1227 line = malloc(width + 1); // one additional char for the terminating null
1229 // Audio time
1230 if (mpctx->sh_audio) {
1231 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1232 if (!sh_video) {
1233 float len = demuxer_get_time_length(mpctx->demuxer);
1234 saddf(line, &pos, width, "(");
1235 sadd_hhmmssf(line, &pos, width, a_pos);
1236 saddf(line, &pos, width, ") of %.1f (", len);
1237 sadd_hhmmssf(line, &pos, width, len);
1238 saddf(line, &pos, width, ") ");
1242 // Video time
1243 if (sh_video)
1244 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1246 // A-V sync
1247 if (mpctx->sh_audio && sh_video)
1248 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1249 mpctx->last_av_difference, mpctx->total_avsync_change);
1251 // Video stats
1252 if (sh_video)
1253 saddf(line, &pos, width, "%3d/%3d ",
1254 (int)sh_video->num_frames,
1255 (int)sh_video->num_frames_decoded);
1257 // CPU usage
1258 if (sh_video) {
1259 if (sh_video->timer > 0.5)
1260 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1261 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1262 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1263 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1264 else
1265 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1266 } else if (mpctx->sh_audio) {
1267 if (mpctx->delay > 0.5)
1268 saddf(line, &pos, width, "%4.1f%% ",
1269 100.0*audio_time_usage/(double)mpctx->delay);
1270 else
1271 saddf(line, &pos, width, "??,?%% ");
1274 // VO stats
1275 if (sh_video)
1276 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1278 #ifdef CONFIG_STREAM_CACHE
1279 // cache stats
1280 if (stream_cache_size > 0)
1281 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1282 #endif
1284 // other
1285 if (opts->playback_speed != 1)
1286 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1288 // end
1289 if (erase_to_end_of_line) {
1290 line[pos] = 0;
1291 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1292 } else {
1293 memset(&line[pos], ' ', width - pos);
1294 line[width] = 0;
1295 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1297 free(line);
1301 * \brief build a chain of audio filters that converts the input format
1302 * to the ao's format, taking into account the current playback_speed.
1303 * \param sh_audio describes the requested input format of the chain.
1304 * \param ao_data describes the requested output format of the chain.
1306 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1308 struct MPOpts *opts = &mpctx->opts;
1309 int new_srate;
1310 int result;
1311 if (!sh_audio)
1313 mpctx->mixer.afilter = NULL;
1314 return 0;
1316 if(af_control_any_rev(sh_audio->afilter,
1317 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1318 &opts->playback_speed)) {
1319 new_srate = sh_audio->samplerate;
1320 } else {
1321 new_srate = sh_audio->samplerate * opts->playback_speed;
1322 if (new_srate != ao_data->samplerate) {
1323 // limits are taken from libaf/af_resample.c
1324 if (new_srate < 8000)
1325 new_srate = 8000;
1326 if (new_srate > 192000)
1327 new_srate = 192000;
1328 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1331 result = init_audio_filters(sh_audio, new_srate,
1332 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1333 mpctx->mixer.afilter = sh_audio->afilter;
1334 return result;
1338 typedef struct mp_osd_msg mp_osd_msg_t;
1339 struct mp_osd_msg {
1340 /// Previous message on the stack.
1341 mp_osd_msg_t* prev;
1342 /// Message text.
1343 char msg[128];
1344 int id,level,started;
1345 /// Display duration in ms.
1346 unsigned time;
1349 /// OSD message stack.
1350 static mp_osd_msg_t* osd_msg_stack = NULL;
1353 * \brief Add a message on the OSD message stack
1355 * If a message with the same id is already present in the stack
1356 * it is pulled on top of the stack, otherwise a new message is created.
1360 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1361 mp_osd_msg_t *msg,*last=NULL;
1362 va_list va;
1363 int r;
1365 // look if the id is already in the stack
1366 for(msg = osd_msg_stack ; msg && msg->id != id ;
1367 last = msg, msg = msg->prev);
1368 // not found: alloc it
1369 if(!msg) {
1370 msg = calloc(1,sizeof(mp_osd_msg_t));
1371 msg->prev = osd_msg_stack;
1372 osd_msg_stack = msg;
1373 } else if(last) { // found, but it's not on top of the stack
1374 last->prev = msg->prev;
1375 msg->prev = osd_msg_stack;
1376 osd_msg_stack = msg;
1378 // write the msg
1379 va_start(va,fmt);
1380 r = vsnprintf(msg->msg, 128, fmt, va);
1381 va_end(va);
1382 if(r >= 128) msg->msg[127] = 0;
1383 // set id and time
1384 msg->id = id;
1385 msg->level = level;
1386 msg->time = time;
1391 * \brief Remove a message from the OSD stack
1393 * This function can be used to get rid of a message right away.
1397 void rm_osd_msg(int id) {
1398 mp_osd_msg_t *msg,*last=NULL;
1400 // Search for the msg
1401 for(msg = osd_msg_stack ; msg && msg->id != id ;
1402 last = msg, msg = msg->prev);
1403 if(!msg) return;
1405 // Detach it from the stack and free it
1406 if(last)
1407 last->prev = msg->prev;
1408 else
1409 osd_msg_stack = msg->prev;
1410 free(msg);
1414 * \brief Remove all messages from the OSD stack
1418 static void clear_osd_msgs(void) {
1419 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1420 while(msg) {
1421 prev = msg->prev;
1422 free(msg);
1423 msg = prev;
1425 osd_msg_stack = NULL;
1429 * \brief Get the current message from the OSD stack.
1431 * This function decrements the message timer and destroys the old ones.
1432 * The message that should be displayed is returned (if any).
1436 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1438 struct MPOpts *opts = &mpctx->opts;
1439 mp_osd_msg_t *msg,*prev,*last = NULL;
1440 static unsigned last_update = 0;
1441 unsigned now = GetTimerMS();
1442 unsigned diff;
1443 char hidden_dec_done = 0;
1445 if (mpctx->osd_visible) {
1446 // 36000000 means max timed visibility is 1 hour into the future, if
1447 // the difference is greater assume it's wrapped around from below 0
1448 if (mpctx->osd_visible - now > 36000000) {
1449 mpctx->osd_visible = 0;
1450 vo_osd_progbar_type = -1; // disable
1451 vo_osd_changed(OSDTYPE_PROGBAR);
1452 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1455 if (mpctx->osd_show_percentage_until - now > 36000000)
1456 mpctx->osd_show_percentage_until = 0;
1458 if(!last_update) last_update = now;
1459 diff = now >= last_update ? now - last_update : 0;
1461 last_update = now;
1463 // Look for the first message in the stack with high enough level.
1464 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1465 prev = msg->prev;
1466 if (msg->level > opts->osd_level && hidden_dec_done)
1467 continue;
1468 // The message has a high enough level or it is the first hidden one
1469 // in both cases we decrement the timer or kill it.
1470 if(!msg->started || msg->time > diff) {
1471 if(msg->started) msg->time -= diff;
1472 else msg->started = 1;
1473 // display it
1474 if (msg->level <= opts->osd_level)
1475 return msg;
1476 hidden_dec_done = 1;
1477 continue;
1479 // kill the message
1480 free(msg);
1481 if(last) {
1482 last->prev = prev;
1483 msg = last;
1484 } else {
1485 osd_msg_stack = prev;
1486 msg = NULL;
1489 // Nothing found
1490 return NULL;
1494 * \brief Display the OSD bar.
1496 * Display the OSD bar or fall back on a simple message.
1500 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1501 struct MPOpts *opts = &mpctx->opts;
1502 if (opts->osd_level < 1)
1503 return;
1505 if(mpctx->sh_video) {
1506 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1507 vo_osd_progbar_type = type;
1508 vo_osd_progbar_value = 256*(val-min)/(max-min);
1509 vo_osd_changed(OSDTYPE_PROGBAR);
1510 return;
1513 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1514 name, ROUND(100*(val-min)/(max-min)));
1518 * \brief Display text subtitles on the OSD
1520 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs)
1522 int i;
1523 vo_sub = subs;
1524 vo_osd_changed(OSDTYPE_SUBTITLE);
1525 if (!mpctx->sh_video) {
1526 // reverse order, since newest set_osd_msg is displayed first
1527 for (i = SUB_MAX_TEXT - 1; i >= 0; i--) {
1528 if (!subs || i >= subs->lines || !subs->text[i])
1529 rm_osd_msg(OSD_MSG_SUB_BASE + i);
1530 else {
1531 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1532 int display_time = i == subs->lines - 1 ? 180000 : 2000;
1533 set_osd_msg(OSD_MSG_SUB_BASE + i, 1, display_time, "%s", subs->text[i]);
1540 * \brief Update the OSD message line.
1542 * This function displays the current message on the vo OSD or on the term.
1543 * If the stack is empty and the OSD level is high enough the timer
1544 * is displayed (only on the vo OSD).
1548 static void update_osd_msg(struct MPContext *mpctx)
1550 struct MPOpts *opts = &mpctx->opts;
1551 mp_osd_msg_t *msg;
1552 struct osd_state *osd = mpctx->osd;
1553 char osd_text_timer[128];
1555 if (mpctx->add_osd_seek_info) {
1556 double percentage;
1557 if (mpctx->timeline && mpctx->sh_video)
1558 percentage = mpctx->sh_video->pts * 100 /
1559 mpctx->timeline[mpctx->num_timeline_parts].start;
1560 else
1561 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1562 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1563 if (mpctx->sh_video)
1564 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1565 mpctx->add_osd_seek_info = false;
1568 // Look if we have a msg
1569 if((msg = get_osd_msg(mpctx))) {
1570 if (strcmp(osd->osd_text, msg->msg)) {
1571 strncpy(osd->osd_text, msg->msg, 127);
1572 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1573 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1575 return;
1578 if(mpctx->sh_video) {
1579 // fallback on the timer
1580 if (opts->osd_level >= 2) {
1581 int len;
1582 if (mpctx->timeline)
1583 len = mpctx->timeline[mpctx->num_timeline_parts].start;
1584 else
1585 len = demuxer_get_time_length(mpctx->demuxer);
1586 int percentage = -1;
1587 char percentage_text[10];
1588 int pts = demuxer_get_current_time(mpctx->demuxer);
1590 if (mpctx->osd_show_percentage_until)
1591 if (mpctx->timeline)
1592 percentage = mpctx->sh_video->pts * 100 /
1593 mpctx->timeline[mpctx->num_timeline_parts].start;
1594 else
1595 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1597 if (percentage >= 0)
1598 snprintf(percentage_text, 9, " (%d%%)", percentage);
1599 else
1600 percentage_text[0] = 0;
1602 if (opts->osd_level == 3)
1603 snprintf(osd_text_timer, 63,
1604 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1605 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1606 len/3600,(len/60)%60,len%60,percentage_text);
1607 else
1608 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1609 mpctx->osd_function,pts/3600,(pts/60)%60,
1610 pts%60,percentage_text);
1611 } else
1612 osd_text_timer[0]=0;
1614 if (strcmp(osd->osd_text, osd_text_timer)) {
1615 strncpy(osd->osd_text, osd_text_timer, 63);
1616 vo_osd_changed(OSDTYPE_OSD);
1618 return;
1621 // Clear the term osd line
1622 if (term_osd && osd->osd_text[0]) {
1623 osd->osd_text[0] = 0;
1624 printf("%s\n",term_osd_esc);
1628 ///@}
1629 // OSDMsgStack
1632 void reinit_audio_chain(struct MPContext *mpctx)
1634 struct MPOpts *opts = &mpctx->opts;
1635 if (!mpctx->sh_audio)
1636 return;
1637 current_module="init_audio_codec";
1638 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1639 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1640 goto init_error;
1642 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1643 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1646 current_module="af_preinit";
1647 ao_data.samplerate=force_srate;
1648 ao_data.channels=0;
1649 ao_data.format=audio_output_format;
1650 #if 1
1651 // first init to detect best values
1652 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1653 // input:
1654 mpctx->sh_audio->samplerate,
1655 // output:
1656 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1657 mp_tmsg(MSGT_CPLAYER,MSGL_ERR, "Error at audio filter chain "
1658 "pre-init!\n");
1659 exit_player(mpctx, EXIT_ERROR);
1661 #endif
1662 current_module="ao2_init";
1663 mpctx->audio_out = init_best_audio_out(opts->audio_driver_list,
1664 0, // plugin flag
1665 ao_data.samplerate,
1666 ao_data.channels,
1667 ao_data.format, 0);
1668 if(!mpctx->audio_out){
1669 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Could not open/initialize audio device -> no sound.\n");
1670 goto init_error;
1672 mpctx->initialized_flags|=INITIALIZED_AO;
1673 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1674 mpctx->audio_out->info->short_name,
1675 ao_data.samplerate, ao_data.channels,
1676 af_fmt2str_short(ao_data.format),
1677 af_fmt2bits(ao_data.format)/8 );
1678 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1679 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1680 if(strlen(mpctx->audio_out->info->comment) > 0)
1681 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1682 // init audio filters:
1683 #if 1
1684 current_module="af_init";
1685 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1686 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter/ao format!\n");
1687 goto init_error;
1689 #endif
1690 mpctx->mixer.audio_out = mpctx->audio_out;
1691 mpctx->mixer.volstep = volstep;
1692 return;
1694 init_error:
1695 uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec and possibly AO
1696 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1697 mpctx->d_audio->id = -2;
1701 ///@}
1702 // Command2Property
1705 // Return pts value corresponding to the end point of audio written to the
1706 // ao so far.
1707 static double written_audio_pts(struct MPContext *mpctx)
1709 sh_audio_t *sh_audio = mpctx->sh_audio;
1710 demux_stream_t *d_audio = mpctx->d_audio;
1711 double buffered_output;
1712 // first calculate the end pts of audio that has been output by decoder
1713 double a_pts = sh_audio->pts;
1714 if (a_pts != MP_NOPTS_VALUE)
1715 // Good, decoder supports new way of calculating audio pts.
1716 // sh_audio->pts is the timestamp of the latest input packet with
1717 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1718 // the amount of bytes the decoder has written after that timestamp.
1719 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1720 else {
1721 // Decoder doesn't support new way of calculating pts (or we're
1722 // being called before it has decoded anything with known timestamp).
1723 // Use the old method of audio pts calculation: take the timestamp
1724 // of last packet with known pts the decoder has read data from,
1725 // and add amount of bytes read after the beginning of that packet
1726 // divided by input bps. This will be inaccurate if the input/output
1727 // ratio is not constant for every audio packet or if it is constant
1728 // but not accurately known in sh_audio->i_bps.
1730 a_pts = d_audio->pts;
1731 // ds_tell_pts returns bytes read after last timestamp from
1732 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1733 // it has read but not decoded
1734 if (sh_audio->i_bps)
1735 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1736 (double)sh_audio->i_bps;
1738 // Now a_pts hopefully holds the pts for end of audio from decoder.
1739 // Substract data in buffers between decoder and audio out.
1741 // Decoded but not filtered
1742 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1744 // Data buffered in audio filters, measured in bytes of "missing" output
1745 buffered_output = af_calc_delay(sh_audio->afilter);
1747 // Data that was ready for ao but was buffered because ao didn't fully
1748 // accept everything to internal buffers yet
1749 buffered_output += sh_audio->a_out_buffer_len;
1751 // Filters divide audio length by playback_speed, so multiply by it
1752 // to get the length in original units without speedup or slowdown
1753 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1755 return a_pts + mpctx->video_offset;
1758 // Return pts value corresponding to currently playing audio.
1759 double playing_audio_pts(struct MPContext *mpctx)
1761 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1762 mpctx->audio_out->get_delay();
1765 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1766 struct MPOpts *opts = &mpctx->opts;
1767 // check for frame-drop:
1768 current_module = "check_framedrop";
1769 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1770 static int dropped_frames;
1771 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1772 float d = delay-mpctx->delay;
1773 ++total_frame_cnt;
1774 // we should avoid dropping too many frames in sequence unless we
1775 // are too late. and we allow 100ms A-V delay here:
1776 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1777 && !mpctx->update_video_immediately) {
1778 ++drop_frame_cnt;
1779 ++dropped_frames;
1780 return frame_dropping;
1781 } else
1782 dropped_frames = 0;
1784 return 0;
1788 #ifdef HAVE_RTC
1789 int rtc_fd = -1;
1790 #endif
1792 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1794 #ifdef HAVE_RTC
1795 if (rtc_fd >= 0){
1796 // -------- RTC -----------
1797 current_module="sleep_rtc";
1798 while (time_frame > 0.000) {
1799 unsigned long rtc_ts;
1800 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1801 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC read error: %s\n", strerror(errno));
1802 time_frame -= get_relative_time(mpctx);
1804 } else
1805 #endif
1807 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1808 // unnecessarily high CPU usage
1809 float margin = softsleep ? 0.011 : 0;
1810 current_module = "sleep_timer";
1811 while (time_frame > margin) {
1812 usec_sleep(1000000 * (time_frame - margin));
1813 time_frame -= get_relative_time(mpctx);
1815 if (softsleep){
1816 current_module = "sleep_soft";
1817 if (time_frame < 0)
1818 mp_tmsg(MSGT_AVSYNC, MSGL_WARN, "Warning! Softsleep underflow!\n");
1819 while (time_frame > 0)
1820 time_frame -= get_relative_time(mpctx); // burn the CPU
1823 return time_frame;
1826 static void select_subtitle(MPContext *mpctx)
1828 struct MPOpts *opts = &mpctx->opts;
1829 // find the best sub to use
1830 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
1831 mpctx->global_sub_pos = -1; // no subs by default
1832 if (vobsub_index_id >= 0) {
1833 // if user asks for a vobsub id, use that first.
1834 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
1835 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1836 // if user asks for a dvd sub id, use that next.
1837 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1838 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
1839 // if there are text subs to use, use those. (autosubs come last here)
1840 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
1841 } else if (opts->sub_id == -1 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1842 // finally select subs by language and container hints
1843 if (opts->sub_id == -1 && dvdsub_lang)
1844 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
1845 if (opts->sub_id == -1)
1846 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
1847 if (opts->sub_id >= 0)
1848 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1850 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
1851 mpctx->global_sub_pos--;
1852 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
1855 #ifdef CONFIG_DVDNAV
1856 #ifndef FF_B_TYPE
1857 #define FF_B_TYPE 3
1858 #endif
1859 /// store decoded video image
1860 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1861 mp_image_t *from_mpi) {
1862 mp_image_t *mpi;
1864 /// Do not store B-frames
1865 if (from_mpi->pict_type == FF_B_TYPE)
1866 return to_mpi;
1868 if (to_mpi &&
1869 to_mpi->w == from_mpi->w &&
1870 to_mpi->h == from_mpi->h &&
1871 to_mpi->imgfmt == from_mpi->imgfmt)
1872 mpi = to_mpi;
1873 else {
1874 if (to_mpi)
1875 free_mp_image(to_mpi);
1876 if (from_mpi->w == 0 || from_mpi->h == 0)
1877 return NULL;
1878 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1881 copy_mpi(mpi,from_mpi);
1882 return mpi;
1885 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1886 struct MPOpts *opts = &ctx->opts;
1887 if (ctx->sh_video) {
1888 /// clear video pts
1889 ctx->d_video->pts = 0.0f;
1890 ctx->sh_video->pts = 0.0f;
1891 ctx->sh_video->i_pts = 0.0f;
1892 ctx->sh_video->last_pts = 0.0f;
1893 ctx->sh_video->num_buffered_pts = 0;
1894 ctx->sh_video->num_frames = 0;
1895 ctx->sh_video->num_frames_decoded = 0;
1896 ctx->sh_video->timer = 0.0f;
1897 ctx->sh_video->stream_delay = 0.0f;
1898 ctx->sh_video->timer = 0;
1899 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1902 if (ctx->sh_audio) {
1903 /// free audio packets and reset
1904 ds_free_packs(ctx->d_audio);
1905 audio_delay -= ctx->sh_audio->stream_delay;
1906 ctx->delay =- audio_delay;
1907 ctx->audio_out->reset();
1908 resync_audio_stream(ctx->sh_audio);
1911 audio_delay = 0.0f;
1912 ctx->global_sub_size = ctx->global_sub_indices[SUB_SOURCE_DEMUX] + mp_dvdnav_number_of_subs(ctx->stream);
1913 if (dvdsub_lang && opts->sub_id == dvdsub_lang_id) {
1914 dvdsub_lang_id = mp_dvdnav_sid_from_lang(ctx->stream, dvdsub_lang);
1915 if (dvdsub_lang_id != opts->sub_id) {
1916 opts->sub_id = dvdsub_lang_id;
1917 select_subtitle(ctx);
1921 /// clear all EOF related flags
1922 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1925 /// Restore last decoded DVDNAV (still frame)
1926 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1927 int *in_size,
1928 unsigned char **start,
1929 mp_image_t *decoded_frame)
1931 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1932 return decoded_frame;
1934 /// a change occured in dvdnav stream
1935 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1936 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1937 mp_dvdnav_context_free(mpctx);
1938 mp_dvdnav_reset_stream(mpctx);
1939 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1940 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1943 if (*in_size < 0) {
1944 float len;
1946 /// Display still frame, if any
1947 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1948 decoded_frame = mpctx->nav_smpi;
1950 /// increment video frame : continue playing after still frame
1951 len = demuxer_get_time_length(mpctx->demuxer);
1952 if (mpctx->sh_video->pts >= len &&
1953 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1954 mp_dvdnav_skip_still(mpctx->stream);
1955 mp_dvdnav_skip_wait(mpctx->stream);
1957 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1959 if (mpctx->nav_buffer) {
1960 *start = mpctx->nav_start;
1961 *in_size = mpctx->nav_in_size;
1962 if (mpctx->nav_start)
1963 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1967 return decoded_frame;
1970 /// Save last decoded DVDNAV (still frame)
1971 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1972 unsigned char *start,
1973 mp_image_t *decoded_frame)
1975 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1976 return;
1978 if (mpctx->nav_buffer)
1979 free(mpctx->nav_buffer);
1981 mpctx->nav_buffer = malloc(in_size);
1982 mpctx->nav_start = start;
1983 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1984 if (mpctx->nav_buffer)
1985 memcpy(mpctx->nav_buffer,start,in_size);
1987 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1988 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1990 #endif /* CONFIG_DVDNAV */
1992 /* Modify video timing to match the audio timeline. There are two main
1993 * reasons this is needed. First, video and audio can start from different
1994 * positions at beginning of file or after a seek (MPlayer starts both
1995 * immediately even if they have different pts). Second, the file can have
1996 * audio timestamps that are inconsistent with the duration of the audio
1997 * packets, for example two consecutive timestamp values differing by
1998 * one second but only a packet with enough samples for half a second
1999 * of playback between them.
2001 static void adjust_sync(struct MPContext *mpctx, double frame_time)
2003 current_module = "av_sync";
2005 if (!mpctx->sh_audio)
2006 return;
2008 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
2009 double v_pts = mpctx->sh_video->pts;
2010 double av_delay = a_pts - v_pts;
2011 // Try to sync vo_flip() so it will *finish* at given time
2012 av_delay += mpctx->last_vo_flip_duration;
2013 av_delay -= audio_delay; // This much pts difference is desired
2015 double change = av_delay * 0.1;
2016 double max_change = default_max_pts_correction >= 0 ?
2017 default_max_pts_correction : frame_time * 0.1;
2018 if (change < -max_change)
2019 change = -max_change;
2020 else if (change > max_change)
2021 change = max_change;
2022 mpctx->delay += change;
2023 mpctx->total_avsync_change += change;
2026 static int fill_audio_out_buffers(struct MPContext *mpctx)
2028 struct MPOpts *opts = &mpctx->opts;
2029 unsigned int t;
2030 double tt;
2031 int playsize;
2032 int playflags=0;
2033 int audio_eof=0;
2034 sh_audio_t * const sh_audio = mpctx->sh_audio;
2036 current_module="play_audio";
2038 while (1) {
2039 int sleep_time;
2040 // all the current uses of ao_data.pts seem to be in aos that handle
2041 // sync completely wrong; there should be no need to use ao_data.pts
2042 // in get_space()
2043 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2044 playsize = mpctx->audio_out->get_space();
2045 if (mpctx->sh_video || playsize >= ao_data.outburst)
2046 break;
2048 // handle audio-only case:
2049 // this is where mplayer sleeps during audio-only playback
2050 // to avoid 100% CPU use
2051 sleep_time = (ao_data.outburst - playsize) * 1000 / ao_data.bps;
2052 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
2053 usec_sleep(sleep_time * 1000);
2056 // Fill buffer if needed:
2057 current_module="decode_audio";
2058 t = GetTimer();
2059 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2060 if (mpctx->d_audio->eof) {
2061 audio_eof = 1;
2062 if (sh_audio->a_out_buffer_len == 0)
2063 return 0;
2065 t = GetTimer() - t;
2066 tt = t*0.000001f; audio_time_usage+=tt;
2067 if (playsize > sh_audio->a_out_buffer_len) {
2068 playsize = sh_audio->a_out_buffer_len;
2069 if (audio_eof)
2070 playflags |= AOPLAY_FINAL_CHUNK;
2072 if (!playsize)
2073 return 1;
2075 // play audio:
2076 current_module="play_audio";
2078 // Is this pts value actually useful for the aos that access it?
2079 // They're obviously badly broken in the way they handle av sync;
2080 // would not having access to this make them more broken?
2081 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2082 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2084 if (playsize > 0) {
2085 sh_audio->a_out_buffer_len -= playsize;
2086 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2087 sh_audio->a_out_buffer_len);
2088 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2090 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2091 // Sanity check to avoid hanging in case current ao doesn't output
2092 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2093 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2094 sh_audio->a_out_buffer_len = 0;
2097 return 1;
2100 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2101 float *aq_sleep_time)
2103 struct MPOpts *opts = &mpctx->opts;
2104 int frame_time_remaining = 0;
2105 current_module="calc_sleep_time";
2107 *time_frame -= get_relative_time(mpctx); // reset timer
2109 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2110 float delay = mpctx->audio_out->get_delay();
2111 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2113 if (autosync) {
2115 * Adjust this raw delay value by calculating the expected
2116 * delay for this frame and generating a new value which is
2117 * weighted between the two. The higher autosync is, the
2118 * closer to the delay value gets to that which "-nosound"
2119 * would have used, and the longer it will take for A/V
2120 * sync to settle at the right value (but it eventually will.)
2121 * This settling time is very short for values below 100.
2123 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2124 float difference = delay - predicted;
2125 delay = predicted + difference / (float)autosync;
2128 *time_frame = delay - mpctx->delay / opts->playback_speed;
2130 // delay = amount of audio buffered in soundcard/driver
2131 if (delay > 0.25) delay=0.25; else
2132 if (delay < 0.10) delay=0.10;
2133 if (*time_frame > delay*0.6) {
2134 // sleep time too big - may cause audio drops (buffer underrun)
2135 frame_time_remaining = 1;
2136 *time_frame = delay*0.5;
2138 } else {
2139 // If we're lagging more than 200 ms behind the right playback rate,
2140 // don't try to "catch up".
2141 // If benchmark is set always output frames as fast as possible
2142 // without sleeping.
2143 if (*time_frame < -0.2 || benchmark)
2144 *time_frame = 0;
2147 *aq_sleep_time += *time_frame;
2150 //============================== SLEEP: ===================================
2152 if (mpctx->video_out->driver->flip_page_timed)
2153 *time_frame -= 0.05;
2154 // flag 256 means: libvo driver does its timing (dvb card)
2155 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2156 *time_frame = timing_sleep(mpctx, *time_frame);
2157 if (mpctx->video_out->driver->flip_page_timed)
2158 *time_frame += 0.05;
2159 return frame_time_remaining;
2162 int reinit_video_chain(struct MPContext *mpctx)
2164 struct MPOpts *opts = &mpctx->opts;
2165 sh_video_t * const sh_video = mpctx->sh_video;
2166 double ar=-1.0;
2167 //================== Init VIDEO (codec & libvo) ==========================
2168 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2169 current_module="preinit_libvo";
2171 //shouldn't we set dvideo->id=-2 when we fail?
2172 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2173 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2174 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Error opening/initializing the selected video_out (-vo) device.\n");
2175 goto err_out;
2177 mpctx->initialized_flags|=INITIALIZED_VO;
2180 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2181 mpctx->sh_video->stream_aspect = ar;
2182 current_module="init_video_filters";
2184 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2185 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2187 #ifdef CONFIG_MENU
2188 if(use_menu) {
2189 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2190 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2191 if(!vf_menu) {
2192 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Can't open libmenu video filter with root menu %s.\n",menu_root);
2193 use_menu = 0;
2196 if(vf_menu)
2197 sh_video->vfilter = vf_menu;
2198 #endif
2200 #ifdef CONFIG_ASS
2201 if(opts->ass_enabled) {
2202 int i;
2203 int insert = 1;
2204 if (opts->vf_settings)
2205 for (i = 0; opts->vf_settings[i].name; ++i)
2206 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2207 insert = 0;
2208 break;
2210 if (insert) {
2211 extern vf_info_t vf_info_ass;
2212 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2213 char* vf_arg[] = {"auto", "1", NULL};
2214 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2215 if (vf_ass)
2216 sh_video->vfilter = vf_ass;
2217 else
2218 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2221 #endif
2223 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2225 #ifdef CONFIG_ASS
2226 if (opts->ass_enabled)
2227 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2228 #endif
2230 current_module="init_video_codec";
2232 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2233 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2234 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2236 if(!sh_video->initialized){
2237 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2238 goto err_out;
2241 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2243 if (sh_video->codec)
2244 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2246 sh_video->last_pts = MP_NOPTS_VALUE;
2247 sh_video->num_buffered_pts = 0;
2248 sh_video->next_frame_time = 0;
2250 if(auto_quality>0){
2251 // Auto quality option enabled
2252 output_quality=get_video_quality_max(sh_video);
2253 if(auto_quality>output_quality) auto_quality=output_quality;
2254 else output_quality=auto_quality;
2255 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2256 set_video_quality(sh_video,output_quality);
2259 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2261 current_module="init_vo";
2263 return 1;
2265 err_out:
2266 mpctx->sh_video = mpctx->d_video->sh = NULL;
2267 return 0;
2270 static double update_video_nocorrect_pts(struct MPContext *mpctx)
2272 struct sh_video *sh_video = mpctx->sh_video;
2273 double frame_time = 0;
2274 struct vo *video_out = mpctx->video_out;
2275 while (!video_out->frame_loaded) {
2276 current_module = "filter_video";
2277 // In nocorrect-pts mode there is no way to properly time these frames
2278 if (vo_get_buffered_frame(video_out, 0) >= 0)
2279 break;
2280 if (vf_output_queued_frame(sh_video->vfilter))
2281 continue;
2282 unsigned char *packet = NULL;
2283 frame_time = sh_video->next_frame_time;
2284 if (mpctx->update_video_immediately)
2285 frame_time = 0;
2286 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2287 &packet, force_fps);
2288 if (in_size < 0) {
2289 #ifdef CONFIG_DVDNAV
2290 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2291 if (mp_dvdnav_is_eof(mpctx->stream))
2292 return -1;
2293 if (mpctx->d_video)
2294 mpctx->d_video->eof = 0;
2295 if (mpctx->d_audio)
2296 mpctx->d_audio->eof = 0;
2297 mpctx->stream->eof = 0;
2298 } else
2299 #endif
2300 return -1;
2302 if (in_size > max_framesize)
2303 max_framesize = in_size;
2304 sh_video->timer += frame_time;
2305 if (mpctx->sh_audio)
2306 mpctx->delay -= frame_time;
2307 // video_read_frame can change fps (e.g. for ASF video)
2308 vo_fps = sh_video->fps;
2309 int framedrop_type = check_framedrop(mpctx, frame_time);
2310 current_module = "decode video";
2312 void *decoded_frame;
2313 #ifdef CONFIG_DVDNAV
2314 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2315 if (in_size >= 0 && !decoded_frame)
2316 #endif
2317 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2318 sh_video->pts);
2319 #ifdef CONFIG_DVDNAV
2320 // Save last still frame for future display
2321 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2322 #endif
2323 if (decoded_frame) {
2324 current_module = "filter video";
2325 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2326 if (!video_out->config_ok)
2327 break;
2330 return frame_time;
2333 static void determine_frame_pts(struct MPContext *mpctx)
2335 struct sh_video *sh_video = mpctx->sh_video;
2336 struct MPOpts *opts = &mpctx->opts;
2338 if (opts->user_pts_assoc_mode) {
2339 sh_video->pts_assoc_mode = opts->user_pts_assoc_mode;
2340 } else if (sh_video->pts_assoc_mode == 0) {
2341 if (sh_video->codec_reordered_pts != MP_NOPTS_VALUE)
2342 sh_video->pts_assoc_mode = 1;
2343 else
2344 sh_video->pts_assoc_mode = 2;
2345 } else {
2346 int probcount1 = sh_video->num_reordered_pts_problems;
2347 int probcount2 = sh_video->num_sorted_pts_problems;
2348 if (sh_video->pts_assoc_mode == 2) {
2349 int tmp = probcount1;
2350 probcount1 = probcount2;
2351 probcount2 = tmp;
2353 if (probcount1 >= probcount2 * 1.5 + 2) {
2354 sh_video->pts_assoc_mode = 3 - sh_video->pts_assoc_mode;
2355 mp_msg(MSGT_CPLAYER, MSGL_V, "Switching to pts association mode "
2356 "%d.\n", sh_video->pts_assoc_mode);
2359 sh_video->pts = sh_video->pts_assoc_mode == 1 ?
2360 sh_video->codec_reordered_pts : sh_video->sorted_pts;
2363 static double update_video(struct MPContext *mpctx)
2365 struct sh_video *sh_video = mpctx->sh_video;
2366 struct vo *video_out = mpctx->video_out;
2367 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2368 mpctx->osd); // hack for vf_expand
2369 if (!mpctx->opts.correct_pts)
2370 return update_video_nocorrect_pts(mpctx);
2372 double pts;
2374 bool hit_eof = false;
2375 while (!video_out->frame_loaded) {
2376 current_module = "filter_video";
2377 if (vo_get_buffered_frame(video_out, hit_eof) >= 0)
2378 break;
2379 // XXX Time used in this call is not counted in any performance
2380 // timer now, OSD time is not updated correctly for filter-added frames
2381 if (vf_output_queued_frame(sh_video->vfilter))
2382 continue;
2383 if (hit_eof)
2384 return -1;
2385 unsigned char *packet = NULL;
2386 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2387 if (pts != MP_NOPTS_VALUE)
2388 pts += mpctx->video_offset;
2389 if (in_size < 0) {
2390 // try to extract last frames in case of decoder lag
2391 in_size = 0;
2392 pts = 1e300;
2393 hit_eof = true;
2395 if (in_size > max_framesize)
2396 max_framesize = in_size;
2397 current_module = "decode video";
2398 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2399 void *decoded_frame = decode_video(sh_video, packet, in_size,
2400 framedrop_type, pts);
2401 if (decoded_frame) {
2402 determine_frame_pts(mpctx);
2403 current_module = "filter video";
2404 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2405 if (!video_out->config_ok)
2406 break; // We'd likely hang in this loop otherwise
2410 pts = video_out->next_pts;
2411 if (pts == MP_NOPTS_VALUE) {
2412 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2413 // Try to use decoder pts from before filters
2414 pts = sh_video->pts;
2415 if (pts == MP_NOPTS_VALUE)
2416 pts = sh_video->last_pts;
2418 sh_video->pts = pts;
2419 if (sh_video->last_pts == MP_NOPTS_VALUE)
2420 sh_video->last_pts = sh_video->pts;
2421 else if (sh_video->last_pts > sh_video->pts) {
2422 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2423 sh_video->pts, sh_video->last_pts);
2424 /* If the difference in pts is small treat it as jitter around the
2425 * right value (possibly caused by incorrect timestamp ordering) and
2426 * just show this frame immediately after the last one.
2427 * Treat bigger differences as timestamp resets and start counting
2428 * timing of later frames from the position of this one. */
2429 if (sh_video->last_pts - sh_video->pts > 0.5)
2430 sh_video->last_pts = sh_video->pts;
2431 else
2432 sh_video->pts = sh_video->last_pts;
2434 double frame_time = sh_video->pts - sh_video->last_pts;
2435 sh_video->last_pts = sh_video->pts;
2436 sh_video->timer += frame_time;
2437 if (mpctx->sh_audio)
2438 mpctx->delay -= frame_time;
2439 return frame_time;
2442 void pause_player(struct MPContext *mpctx)
2444 if (mpctx->paused)
2445 return;
2446 mpctx->paused = 1;
2447 mpctx->step_frames = 0;
2448 mpctx->time_frame -= get_relative_time(mpctx);
2450 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2451 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2453 if (mpctx->audio_out && mpctx->sh_audio)
2454 mpctx->audio_out->pause(); // pause audio, keep data if possible
2457 void unpause_player(struct MPContext *mpctx)
2459 if (!mpctx->paused)
2460 return;
2461 mpctx->paused = 0;
2463 if (mpctx->audio_out && mpctx->sh_audio)
2464 mpctx->audio_out->resume(); // resume audio
2465 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2466 && !mpctx->step_frames)
2467 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2468 (void)get_relative_time(mpctx); // ignore time that passed during pause
2471 void add_step_frame(struct MPContext *mpctx)
2473 mpctx->step_frames++;
2474 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2475 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2476 unpause_player(mpctx);
2479 static void pause_loop(struct MPContext *mpctx)
2481 mp_cmd_t* cmd;
2482 if (!quiet) {
2483 // Small hack to display the pause message on the OSD line.
2484 // The pause string is: "\n == PAUSE == \r" so we need to
2485 // take the first and the last char out
2486 if (term_osd && !mpctx->sh_video) {
2487 char msg[128] = _("\n ===== PAUSE =====\r");
2488 int mlen = strlen(msg);
2489 msg[mlen-1] = '\0';
2490 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2491 update_osd_msg(mpctx);
2492 } else
2493 mp_tmsg(MSGT_CPLAYER,MSGL_STATUS,"\n ===== PAUSE =====\r");
2494 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2497 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2498 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2499 if (cmd) {
2500 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2501 run_command(mpctx, cmd);
2502 mp_cmd_free(cmd);
2503 continue;
2505 if (mpctx->sh_video && mpctx->video_out)
2506 vo_check_events(mpctx->video_out);
2507 #ifdef CONFIG_MENU
2508 if (vf_menu)
2509 vf_menu_pause_update(vf_menu);
2510 #endif
2511 usec_sleep(20000);
2512 update_osd_msg(mpctx);
2513 int hack = vo_osd_changed(0);
2514 vo_osd_changed(hack);
2515 if (hack)
2516 break;
2521 // Find the right mute status and record position for new file position
2522 static void edl_seek_reset(MPContext *mpctx)
2524 mpctx->edl_muted = 0;
2525 next_edl_record = edl_records;
2527 while (next_edl_record) {
2528 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2529 break;
2531 if (next_edl_record->action == EDL_MUTE)
2532 mpctx->edl_muted = !mpctx->edl_muted;
2533 next_edl_record = next_edl_record->next;
2535 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2536 mixer_mute(&mpctx->mixer);
2540 // Execute EDL command for the current position if one exists
2541 static void edl_update(MPContext *mpctx)
2543 if (!next_edl_record)
2544 return;
2546 if (!mpctx->sh_video) {
2547 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot use EDL without video, disabling.\n");
2548 free_edl(edl_records);
2549 next_edl_record = NULL;
2550 edl_records = NULL;
2551 return;
2554 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2555 if (next_edl_record->action == EDL_SKIP) {
2556 mpctx->osd_function = OSD_FFW;
2557 mpctx->abs_seek_pos = 0;
2558 mpctx->rel_seek_secs = next_edl_record->length_sec;
2559 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2560 "[%f], length [%f]\n", next_edl_record->start_sec,
2561 next_edl_record->stop_sec, next_edl_record->length_sec);
2563 else if (next_edl_record->action == EDL_MUTE) {
2564 mpctx->edl_muted = !mpctx->edl_muted;
2565 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2566 mixer_mute(&mpctx->mixer);
2567 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2568 next_edl_record->start_sec );
2570 next_edl_record = next_edl_record->next;
2574 static void reinit_decoders(struct MPContext *mpctx)
2576 reinit_video_chain(mpctx);
2577 reinit_audio_chain(mpctx);
2578 mp_property_do("sub", M_PROPERTY_SET, &mpctx->global_sub_pos, mpctx);
2581 static bool timeline_set_part(struct MPContext *mpctx, int i)
2583 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
2584 struct timeline_part *n = mpctx->timeline + i;
2585 mpctx->timeline_part = i;
2586 mpctx->video_offset = n->start - n->source_start;
2587 if (n->source == p->source)
2588 return false;
2589 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo && mpctx->opts.video_id != -2 ? 0 : INITIALIZED_VO) | INITIALIZED_AO | INITIALIZED_ACODEC);
2590 mpctx->demuxer = n->source->demuxer;
2591 mpctx->d_video = mpctx->demuxer->video;
2592 mpctx->d_audio = mpctx->demuxer->audio;
2593 mpctx->d_sub = mpctx->demuxer->sub;
2594 mpctx->sh_video = mpctx->d_video->sh;
2595 mpctx->sh_audio = mpctx->d_audio->sh;
2596 return true;
2599 // Given pts, switch playback to the corresponding part.
2600 // Return offset within that part.
2601 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
2602 bool *need_reset)
2604 if (pts < 0)
2605 pts = 0;
2606 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
2607 struct timeline_part *p = mpctx->timeline + i;
2608 if (pts < (p+1)->start) {
2609 *need_reset = timeline_set_part(mpctx, i);
2610 return pts - p->start + p->source_start;
2613 return -1;
2617 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2618 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2619 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2620 static int seek(MPContext *mpctx, double amount, int style)
2622 current_module = "seek";
2623 if (mpctx->stop_play == AT_END_OF_FILE)
2624 mpctx->stop_play = KEEP_PLAYING;
2625 if (mpctx->timeline && style & SEEK_FACTOR) {
2626 amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
2627 style &= ~SEEK_FACTOR;
2629 if ((mpctx->demuxer->accurate_seek || mpctx->timeline) && mpctx->sh_video
2630 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2631 style |= SEEK_ABSOLUTE;
2632 if (amount > 0)
2633 style |= SEEK_FORWARD;
2634 else
2635 style |= SEEK_BACKWARD;
2636 amount += mpctx->sh_video->pts;
2639 /* At least the liba52 decoder wants to read from the input stream
2640 * during initialization, so reinit must be done after the demux_seek()
2641 * call that clears possible stream EOF. */
2642 bool need_reset = false;
2643 if (mpctx->timeline) {
2644 amount = timeline_set_from_time(mpctx, amount, &need_reset);
2645 if (amount == -1) {
2646 mpctx->stop_play = AT_END_OF_FILE;
2647 // Clear audio from current position
2648 if (mpctx->sh_audio) {
2649 mpctx->audio_out->reset();
2650 mpctx->sh_audio->a_buffer_len = 0;
2651 mpctx->sh_audio->a_out_buffer_len = 0;
2653 return -1;
2656 int seekresult = demux_seek(mpctx->demuxer, amount, audio_delay, style);
2657 if (need_reset)
2658 reinit_decoders(mpctx);
2659 if (seekresult == 0)
2660 return -1;
2662 if (mpctx->sh_video) {
2663 current_module = "seek_video_reset";
2664 resync_video_stream(mpctx->sh_video);
2665 vo_seek_reset(mpctx->video_out);
2666 mpctx->sh_video->num_buffered_pts = 0;
2667 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2668 mpctx->delay = 0;
2669 mpctx->time_frame = 0;
2670 mpctx->update_video_immediately = true;
2671 // Not all demuxers set d_video->pts during seek, so this value
2672 // (which is used by at least vobsub and edl code below) may
2673 // be completely wrong (probably 0).
2674 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
2675 update_subtitles(mpctx, &mpctx->opts, mpctx->sh_video,
2676 mpctx->sh_video->pts, mpctx->video_offset,
2677 mpctx->d_sub, 1);
2678 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2681 if (mpctx->sh_audio) {
2682 current_module = "seek_audio_reset";
2683 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2684 mpctx->sh_audio->a_buffer_len = 0;
2685 mpctx->sh_audio->a_out_buffer_len = 0;
2686 if (!mpctx->sh_video)
2687 update_subtitles(mpctx, &mpctx->opts, NULL, mpctx->sh_audio->pts,
2688 mpctx->video_offset, mpctx->d_sub, 1);
2691 if (vo_vobsub && mpctx->sh_video) {
2692 current_module = "seek_vobsub_reset";
2693 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2696 edl_seek_reset(mpctx);
2698 mpctx->total_avsync_change = 0;
2699 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2700 drop_frame_cnt = 0;
2702 current_module = NULL;
2703 return 0;
2706 int get_current_chapter(struct MPContext *mpctx)
2708 if (!mpctx->chapters || !mpctx->sh_video)
2709 return demuxer_get_current_chapter(mpctx->demuxer);
2711 int i;
2712 double current_pts = mpctx->sh_video->pts;
2713 for (i = 1; i < mpctx->num_chapters; i++)
2714 if (current_pts < mpctx->chapters[i].start)
2715 break;
2716 return i - 1;
2719 // currently returns a string allocated with malloc, not talloc
2720 char *chapter_display_name(struct MPContext *mpctx, int chapter)
2722 if (!mpctx->chapters || !mpctx->sh_video)
2723 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
2724 return strdup(mpctx->chapters[chapter].name);
2727 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
2728 char **chapter_name)
2730 if (!mpctx->chapters || !mpctx->sh_video)
2731 return demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
2732 chapter_name);
2733 if (chapter >= mpctx->num_chapters)
2734 return -1;
2735 if (chapter < 0)
2736 chapter = 0;
2737 *seek_pts = mpctx->chapters[chapter].start;
2738 if (chapter_name)
2739 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
2740 return chapter;
2743 static int find_ordered_chapter_sources(struct MPContext *mpctx,
2744 struct content_source *sources,
2745 int num_sources,
2746 unsigned char uid_map[][16])
2748 int num_filenames = 0;
2749 char **filenames = NULL;
2750 if (num_sources > 1) {
2751 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
2752 "other sources.\n");
2753 if (mpctx->stream->type != STREAMTYPE_FILE) {
2754 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
2755 "normal disk file. Will not search for related files.\n");
2756 } else {
2757 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
2758 "same directory to find referenced sources.\n");
2759 filenames = find_files(mpctx->demuxer->filename, ".mkv",
2760 &num_filenames);
2764 int num_left = num_sources - 1;
2765 for (int i = 0; i < num_filenames && num_left > 0; i++) {
2766 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
2767 filename_recode(filenames[i]));
2768 int format;
2769 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
2770 if (!s)
2771 continue;
2772 struct demuxer *d = demux_open(&mpctx->opts, s, DEMUXER_TYPE_MATROSKA,
2773 mpctx->opts.audio_id,
2774 mpctx->opts.video_id,
2775 mpctx->opts.sub_id, filenames[i]);
2776 if (!d) {
2777 free_stream(s);
2778 continue;
2780 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
2781 for (int i = 1; i < num_sources; i++) {
2782 if (sources[i].demuxer)
2783 continue;
2784 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
2785 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
2786 i, filename_recode(d->filename));
2787 sources[i].stream = s;
2788 sources[i].demuxer = d;
2789 num_left--;
2790 goto match;
2794 free_demuxer(d);
2795 free_stream(s);
2796 continue;
2797 match:
2800 talloc_free(filenames);
2801 if (num_left) {
2802 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
2803 "There will be parts MISSING from the video!\n");
2804 for (int i = 1, j = 1; i < num_sources; i++)
2805 if (sources[i].demuxer) {
2806 sources[j] = sources[i];
2807 memcpy(uid_map[j], uid_map[i], 16);
2808 j++;
2811 return num_sources - num_left;
2814 static void build_ordered_chapter_timeline(struct MPContext *mpctx)
2816 if (!mpctx->opts.ordered_chapters) {
2817 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
2818 "you have disabled support for them. Ignoring.\n");
2819 return;
2822 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
2823 "edit timeline.\n");
2825 struct demuxer *demuxer = mpctx->demuxer;
2826 struct matroska_data *m = &demuxer->matroska_data;
2828 // +1 because sources/uid_map[0] is original file even if all chapters
2829 // actually use other sources and need separate entries
2830 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
2831 m->num_ordered_chapters+1);
2832 sources[0].stream = mpctx->stream;
2833 sources[0].demuxer = mpctx->demuxer;
2834 unsigned char uid_map[m->num_ordered_chapters+1][16];
2835 int num_sources = 1;
2836 memcpy(uid_map[0], m->segment_uid, 16);
2838 for (int i = 0; i < m->num_ordered_chapters; i++) {
2839 struct matroska_chapter *c = m->ordered_chapters + i;
2840 if (!c->has_segment_uid)
2841 memcpy(c->segment_uid, m->segment_uid, 16);
2843 for (int j = 0; j < num_sources; j++)
2844 if (!memcmp(c->segment_uid, uid_map[j], 16))
2845 goto found1;
2846 memcpy(uid_map[num_sources], c->segment_uid, 16);
2847 sources[num_sources] = (struct content_source){};
2848 num_sources++;
2849 found1:
2853 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
2854 uid_map);
2857 // +1 for terminating chapter with start time marking end of last real one
2858 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
2859 m->num_ordered_chapters + 1);
2860 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
2861 m->num_ordered_chapters);
2862 uint64_t starttime = 0;
2863 uint64_t missing_time = 0;
2864 int part_count = 0;
2865 int num_chapters = 0;
2866 uint64_t prev_part_offset;
2867 for (int i = 0; i < m->num_ordered_chapters; i++) {
2868 struct matroska_chapter *c = m->ordered_chapters + i;
2870 int j;
2871 for (j = 0; j < num_sources; j++) {
2872 if (!memcmp(c->segment_uid, uid_map[j], 16))
2873 goto found2;
2875 missing_time += c->end - c->start;
2876 continue;
2877 found2:;
2878 chapters[num_chapters].start = starttime / 1000.;
2879 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
2880 /* Only add a separate part if the time or file actually changes.
2881 * Matroska files have chapter divisions that are redundant from
2882 * timeline point of view because the same chapter structure is used
2883 * both to specify the timeline and for normal chapter information.
2884 * Removing a missing inserted external chapter can also cause this. */
2885 if (part_count == 0 || c->start != starttime + prev_part_offset
2886 || sources + j != timeline[part_count - 1].source) {
2887 timeline[part_count].source = sources + j;
2888 timeline[part_count].start = chapters[num_chapters].start;
2889 timeline[part_count].source_start = c->start / 1000.;
2890 prev_part_offset = c->start - starttime;
2891 part_count++;
2893 starttime += c->end - c->start;
2894 num_chapters++;
2896 timeline[part_count].start = starttime / 1000.;
2898 if (!part_count) {
2899 // None of the parts come from the file itself???
2900 talloc_free(sources);
2901 talloc_free(timeline);
2902 talloc_free(chapters);
2903 return;
2906 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
2907 "sources. Total length %.3f seconds.\n", part_count, num_sources,
2908 timeline[part_count].start);
2909 if (missing_time)
2910 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
2911 "from the timeline!\n", missing_time / 1000.);
2912 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
2913 for (int i = 0; i < num_sources; i++)
2914 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
2915 filename_recode(sources[i].demuxer->filename));
2916 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
2917 "source_start, source):\n");
2918 for (int i = 0; i < part_count; i++) {
2919 struct timeline_part *p = timeline + i;
2920 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
2921 p->source_start, p->source - sources);
2923 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start);
2924 mpctx->sources = sources;
2925 mpctx->num_sources = num_sources;
2926 mpctx->timeline = timeline;
2927 mpctx->num_timeline_parts = part_count;
2928 mpctx->num_chapters = num_chapters;
2929 mpctx->chapters = chapters;
2931 mpctx->timeline_part = 0;
2932 mpctx->demuxer = timeline[0].source->demuxer;
2936 static int read_keys(void *ctx, int fd)
2938 getch2(ctx);
2939 return mplayer_get_key(ctx, 0);
2943 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2944 * file for some tools to link against. */
2945 #ifndef DISABLE_MAIN
2946 int main(int argc,char* argv[]){
2949 char * mem_ptr;
2951 // movie info:
2953 /* Flag indicating whether MPlayer should exit without playing anything. */
2954 int opt_exit = 0;
2956 //float a_frame=0; // Audio
2958 int i;
2960 struct MPContext *mpctx = &(struct MPContext){
2961 .osd_function = OSD_PLAY,
2962 .begin_skip = MP_NOPTS_VALUE,
2963 .play_tree_step = 1,
2964 .global_sub_pos = -1,
2965 .set_of_sub_pos = -1,
2966 .file_format = DEMUXER_TYPE_UNKNOWN,
2967 .last_dvb_step = 1,
2970 InitTimer();
2971 srand(GetTimerMS());
2973 mp_msg_init();
2974 set_av_log_callback();
2976 #ifdef CONFIG_X11
2977 mpctx->x11_state = vo_x11_init_state();
2978 #endif
2979 struct MPOpts *opts = &mpctx->opts;
2980 set_default_mplayer_options(opts);
2981 // Create the config context and register the options
2982 mpctx->mconfig = m_config_new(opts, cfg_include);
2983 m_config_register_options(mpctx->mconfig,mplayer_opts);
2984 mp_input_register_options(mpctx->mconfig);
2986 // Preparse the command line
2987 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2989 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2990 set_path_env();
2991 #endif
2993 #ifdef CONFIG_TV
2994 stream_tv_defaults.immediate = 1;
2995 #endif
2997 parse_cfgfiles(mpctx, mpctx->mconfig);
2999 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
3000 if(mpctx->playtree == NULL)
3001 opt_exit = 1;
3002 else {
3003 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
3004 if(mpctx->playtree) {
3005 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3006 if(mpctx->playtree_iter) {
3007 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3008 play_tree_iter_free(mpctx->playtree_iter);
3009 mpctx->playtree_iter = NULL;
3011 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3015 mpctx->key_fifo = mp_fifo_create(opts);
3017 print_version("MPlayer");
3019 #if defined(__MINGW32__) || defined(__CYGWIN__)
3021 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
3022 BOOL WINAPI (*setDEP)(DWORD) = NULL;
3023 if (kernel32)
3024 setDEP = GetProcAddress(kernel32, "SetProcessDEPPolicy");
3025 if (setDEP) setDEP(3);
3027 // stop Windows from showing all kinds of annoying error dialogs
3028 SetErrorMode(0x8003);
3029 // request 1ms timer resolution
3030 timeBeginPeriod(1);
3031 #endif
3033 #ifdef CONFIG_PRIORITY
3034 set_priority();
3035 #endif
3037 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
3038 list_video_out();
3039 opt_exit = 1;
3042 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
3043 list_audio_out();
3044 opt_exit = 1;
3047 /* Check codecs.conf. */
3048 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3049 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3050 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3051 if(!parse_codec_cfg(NULL)){
3052 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3054 mp_tmsg(MSGT_CPLAYER,MSGL_V,"Using built-in default codecs.conf.\n");
3057 free( mem_ptr ); // release the buffer created by get_path()
3060 #if 0
3061 if(video_codec_list){
3062 int i;
3063 video_codec=video_codec_list[0];
3064 for(i=0;video_codec_list[i];i++)
3065 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
3067 #endif
3068 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3069 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available audio codecs:\n");
3070 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3071 list_codecs(1);
3072 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3073 opt_exit = 1;
3075 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3076 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video codecs:\n");
3077 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3078 list_codecs(0);
3079 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3080 opt_exit = 1;
3082 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3083 vfm_help();
3084 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3085 opt_exit = 1;
3087 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3088 afm_help();
3089 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3090 opt_exit = 1;
3092 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3093 af_help();
3094 printf("\n");
3095 opt_exit = 1;
3097 #ifdef CONFIG_X11
3098 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3099 fstype_help();
3100 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3101 opt_exit = 1;
3103 #endif
3104 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3105 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3106 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3107 demuxer_help();
3108 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3109 opt_exit = 1;
3111 if(list_properties) {
3112 property_print_help();
3113 opt_exit = 1;
3116 if(opt_exit)
3117 exit_player(mpctx, EXIT_NONE);
3119 if(!mpctx->filename && !player_idle_mode){
3120 // no file/vcd/dvd -> show HELP:
3121 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
3122 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3125 /* Display what configure line was used */
3126 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3128 // Many users forget to include command line in bugreports...
3129 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3130 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "CommandLine:");
3131 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3132 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3135 //------ load global data first ------
3137 mpctx->osd = osd_create();
3139 // check font
3140 #ifdef CONFIG_FREETYPE
3141 init_freetype();
3142 #endif
3143 #ifdef CONFIG_FONTCONFIG
3144 if(font_fontconfig <= 0)
3146 #endif
3147 #ifdef CONFIG_BITMAP_FONT
3148 if(font_name){
3149 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3150 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",
3151 filename_recode(font_name));
3152 } else {
3153 // try default:
3154 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3155 free(mem_ptr); // release the buffer created by get_path()
3156 if(!vo_font)
3157 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3159 if (sub_font_name)
3160 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3161 else
3162 mpctx->osd->sub_font = vo_font;
3163 #endif
3164 #ifdef CONFIG_FONTCONFIG
3166 #endif
3168 #ifdef CONFIG_ASS
3169 ass_library = ass_init();
3170 #endif
3172 #ifdef HAVE_RTC
3173 if(!nortc)
3175 // seteuid(0); /* Can't hurt to try to get root here */
3176 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3177 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Failed to open %s: %s (it should be readable by the user.)\n",
3178 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3179 else {
3180 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3182 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3183 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp, strerror(errno));
3184 mp_tmsg(MSGT_CPLAYER, MSGL_HINT, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp);
3185 close (rtc_fd);
3186 rtc_fd = -1;
3187 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3188 /* variable only by the root */
3189 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno));
3190 close (rtc_fd);
3191 rtc_fd = -1;
3192 } else
3193 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Using Linux hardware RTC timing (%ldHz).\n", irqp);
3196 if(rtc_fd<0)
3197 #endif /* HAVE_RTC */
3198 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3199 softsleep?"software":timer_name);
3201 #ifdef HAVE_TERMCAP
3202 load_termcap(NULL); // load key-codes
3203 #endif
3205 // ========== Init keyboard FIFO (connection to libvo) ============
3207 // Init input system
3208 current_module = "init_input";
3209 mpctx->input = mp_input_init(&opts->input);
3210 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3211 if(slave_mode)
3212 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
3213 else if(!noconsolecontrols)
3214 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
3215 // Set the libstream interrupt callback
3216 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
3218 #ifdef CONFIG_MENU
3219 if(use_menu) {
3220 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3221 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3222 else {
3223 menu_cfg = get_path("menu.conf");
3224 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3225 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3226 else {
3227 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
3228 MPLAYER_CONFDIR "/menu.conf"))
3229 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", MPLAYER_CONFDIR"/menu.conf");
3230 else {
3231 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Menu init failed.\n");
3232 use_menu = 0;
3237 #endif
3239 current_module = NULL;
3241 /// Catch signals
3242 #ifndef __MINGW32__
3243 signal(SIGCHLD,child_sighandler);
3244 #endif
3246 #ifdef CONFIG_CRASH_DEBUG
3247 prog_path = argv[0];
3248 #endif
3249 //========= Catch terminate signals: ================
3250 // terminate requests:
3251 signal(SIGTERM,exit_sighandler); // kill
3252 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3254 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3256 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3257 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3258 #ifdef CONFIG_SIGHANDLER
3259 // fatal errors:
3260 signal(SIGBUS,exit_sighandler); // bus error
3261 signal(SIGSEGV,exit_sighandler); // segfault
3262 signal(SIGILL,exit_sighandler); // illegal instruction
3263 signal(SIGFPE,exit_sighandler); // floating point exc.
3264 signal(SIGABRT,exit_sighandler); // abort()
3265 #ifdef CONFIG_CRASH_DEBUG
3266 if (crash_debug)
3267 signal(SIGTRAP,exit_sighandler);
3268 #endif
3269 #endif
3271 // ******************* Now, let's see the per-file stuff ********************
3273 play_next_file:
3275 // init global sub numbers
3276 mpctx->global_sub_size = 0;
3277 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3279 if (mpctx->filename) {
3280 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
3281 load_per_extension_config (mpctx->mconfig, mpctx->filename);
3282 load_per_file_config (mpctx->mconfig, mpctx->filename);
3285 if (opts->video_driver_list)
3286 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
3287 if (opts->audio_driver_list)
3288 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
3290 // We must enable getch2 here to be able to interrupt network connection
3291 // or cache filling
3292 if(!noconsolecontrols && !slave_mode){
3293 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
3294 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"WARNING: getch2_init called twice!\n");
3295 else
3296 getch2_enable(); // prepare stdin for hotkeys...
3297 mpctx->initialized_flags|=INITIALIZED_GETCH2;
3298 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3301 // =================== GUI idle loop (STOP state) ===========================
3302 while (player_idle_mode && !mpctx->filename) {
3303 play_tree_t * entry = NULL;
3304 mp_cmd_t * cmd;
3305 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3306 if (mpctx->video_out)
3307 vo_check_events(mpctx->video_out);
3308 usec_sleep(20000);
3310 switch (cmd->id) {
3311 case MP_CMD_LOADFILE:
3312 // prepare a tree entry with the new filename
3313 entry = play_tree_new();
3314 play_tree_add_file(entry, cmd->args[0].v.s);
3315 // The entry is added to the main playtree after the switch().
3316 break;
3317 case MP_CMD_LOADLIST:
3318 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3319 break;
3320 case MP_CMD_QUIT:
3321 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3322 break;
3323 case MP_CMD_GET_PROPERTY:
3324 case MP_CMD_SET_PROPERTY:
3325 case MP_CMD_STEP_PROPERTY:
3326 run_command(mpctx, cmd);
3327 break;
3330 mp_cmd_free(cmd);
3332 if (entry) { // user entered a command that gave a valid entry
3333 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3334 play_tree_free_list(mpctx->playtree->child, 1);
3335 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3337 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3339 play_tree_set_child(mpctx->playtree, entry);
3341 /* Make iterator start at the top the of tree. */
3342 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3343 if (!mpctx->playtree_iter) continue;
3345 // find the first real item in the tree
3346 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3347 // no items!
3348 play_tree_iter_free(mpctx->playtree_iter);
3349 mpctx->playtree_iter = NULL;
3350 continue; // wait for next command
3352 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3355 //---------------------------------------------------------------------------
3357 if (mpctx->filename) {
3358 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nPlaying %s.\n",
3359 filename_recode(mpctx->filename));
3360 if(use_filename_title && vo_wintitle == NULL)
3361 vo_wintitle = strdup(mp_basename2(mpctx->filename));
3364 if (edl_filename) {
3365 if (edl_records) free_edl(edl_records);
3366 next_edl_record = edl_records = edl_parse_file();
3368 if (edl_output_filename) {
3369 if (edl_fd) fclose(edl_fd);
3370 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3372 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Can't open EDL file [%s] for writing.\n",
3373 filename_recode(edl_output_filename));
3377 //==================== Open VOB-Sub ============================
3379 current_module="vobsub";
3380 if (vobsub_name){
3381 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3382 if(vo_vobsub==NULL)
3383 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
3384 filename_recode(vobsub_name));
3385 } else if (sub_auto && mpctx->filename){
3386 /* try to autodetect vobsub from movie filename ::atmos */
3387 char *buf = strdup(mpctx->filename), *psub;
3388 char *pdot = strrchr(buf, '.');
3389 char *pslash = strrchr(buf, '/');
3390 #if defined(__MINGW32__) || defined(__CYGWIN__)
3391 if (!pslash) pslash = strrchr(buf, '\\');
3392 #endif
3393 if (pdot && (!pslash || pdot > pslash))
3394 *pdot = '\0';
3395 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3396 /* try from ~/.mplayer/sub */
3397 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3398 char *bname;
3399 int l;
3400 bname = strrchr(buf,'/');
3401 #if defined(__MINGW32__) || defined(__CYGWIN__)
3402 if(!bname) bname = strrchr(buf,'\\');
3403 #endif
3404 if(bname) bname++;
3405 else bname = buf;
3406 l = strlen(psub) + strlen(bname) + 1;
3407 psub = realloc(psub,l);
3408 strcat(psub,bname);
3409 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3410 free(psub);
3412 free(buf);
3414 if(vo_vobsub){
3415 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3416 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3417 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3419 // setup global sub numbering
3420 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3421 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3424 //============ Open & Sync STREAM --- fork cache2 ====================
3426 mpctx->stream=NULL;
3427 mpctx->demuxer=NULL;
3428 if (mpctx->d_audio) {
3429 //free_demuxer_stream(mpctx->d_audio);
3430 mpctx->d_audio=NULL;
3432 if (mpctx->d_video) {
3433 //free_demuxer_stream(d_video);
3434 mpctx->d_video=NULL;
3436 mpctx->sh_audio=NULL;
3437 mpctx->sh_video=NULL;
3439 current_module="open_stream";
3440 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3441 if(!mpctx->stream) { // error...
3442 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3443 goto goto_next_file;
3445 mpctx->initialized_flags|=INITIALIZED_STREAM;
3447 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3448 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3449 "playlist support will not be used automatically.\n"
3450 "MPlayer's playlist code is unsafe and should only be used with "
3451 "trusted sources.\nPlayback will probably fail.\n\n");
3452 #if 0
3453 play_tree_t* entry;
3454 // Handle playlist
3455 current_module="handle_playlist";
3456 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3457 filename_recode(mpctx->filename));
3458 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3459 mpctx->eof=playtree_add_playlist(mpctx, entry);
3460 goto goto_next_file;
3461 #endif
3463 mpctx->stream->start_pos+=seek_to_byte;
3465 if(stream_dump_type==5){
3466 unsigned char buf[4096];
3467 int len;
3468 FILE *f;
3469 current_module="dumpstream";
3470 stream_reset(mpctx->stream);
3471 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3472 f=fopen(stream_dump_name,"wb");
3473 if(!f){
3474 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3475 exit_player(mpctx, EXIT_ERROR);
3477 if (dvd_chapter > 1) {
3478 int chapter = dvd_chapter - 1;
3479 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3481 while(!mpctx->stream->eof && !async_quit_request){
3482 len=stream_read(mpctx->stream,buf,4096);
3483 if(len>0) {
3484 if(fwrite(buf,len,1,f) != 1) {
3485 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3486 exit_player(mpctx, EXIT_ERROR);
3489 if(dvd_last_chapter > 0) {
3490 int chapter = -1;
3491 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3492 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3493 break;
3496 if(fclose(f)) {
3497 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3498 exit_player(mpctx, EXIT_ERROR);
3500 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3501 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3504 #ifdef CONFIG_DVDREAD
3505 if(mpctx->stream->type==STREAMTYPE_DVD){
3506 current_module="dvd lang->id";
3507 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3508 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3509 // setup global sub numbering
3510 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3511 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3512 current_module=NULL;
3514 #endif
3516 #ifdef CONFIG_DVDNAV
3517 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3518 current_module="dvdnav lang->id";
3519 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3520 dvdsub_lang_id = -3;
3521 if(dvdsub_lang && opts->sub_id==-1)
3522 dvdsub_lang_id = opts->sub_id = mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3523 // setup global sub numbering
3524 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3525 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3526 current_module=NULL;
3528 #endif
3530 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3531 goto_enable_cache:
3532 if(stream_cache_size>0){
3533 current_module="enable_cache";
3534 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3535 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3536 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3537 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3540 //============ Open DEMUXERS --- DETECT file type =======================
3541 current_module="demux_open";
3543 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3545 // HACK to get MOV Reference Files working
3547 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3549 unsigned char* playlist_entry;
3550 play_tree_t *list = NULL, *entry = NULL;
3552 current_module="handle_demux_playlist";
3553 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3555 char *temp, *bname;
3557 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3558 filename_recode(playlist_entry));
3560 bname=mp_basename(playlist_entry);
3561 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3562 continue;
3564 if (!strcmp(playlist_entry, mpctx->filename)) // ignoring self-reference
3565 continue;
3567 entry = play_tree_new();
3569 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3571 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3572 if (temp)
3574 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3575 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3576 strcat(temp, playlist_entry);
3577 if (!strcmp(temp, mpctx->filename)) {
3578 free(temp);
3579 continue;
3581 play_tree_add_file(entry,temp);
3582 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3583 free(temp);
3586 else
3587 play_tree_add_file(entry,playlist_entry);
3589 if(!list)
3590 list = entry;
3591 else
3592 play_tree_append_entry(list,entry);
3594 free_demuxer(mpctx->demuxer);
3595 mpctx->demuxer = NULL;
3597 if (list)
3599 entry = play_tree_new();
3600 play_tree_set_child(entry,list);
3601 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3602 goto goto_next_file;
3606 if(!mpctx->demuxer)
3607 goto goto_next_file;
3609 if (mpctx->demuxer->matroska_data.ordered_chapters)
3610 build_ordered_chapter_timeline(mpctx);
3612 if (!mpctx->sources) {
3613 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
3614 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
3615 .demuxer = mpctx->demuxer};
3616 mpctx->num_sources = 1;
3619 if(dvd_chapter>1) {
3620 double pts;
3621 if (seek_chapter(mpctx, dvd_chapter-1, &pts, NULL) >= 0 && pts > -1.0)
3622 seek(mpctx, pts, SEEK_ABSOLUTE);
3625 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3627 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3628 int i;
3629 int maxid = -1;
3630 // setup global sub numbering
3631 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3632 for (i = 0; i < MAX_S_STREAMS; i++)
3633 if (mpctx->demuxer->s_streams[i])
3634 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3635 mpctx->global_sub_size += maxid + 1;
3637 // Make opts->sub_id always selectable if set.
3638 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3639 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3641 #ifdef CONFIG_ASS
3642 if (opts->ass_enabled && ass_library) {
3643 for (int j = 0; j < mpctx->num_sources; j++) {
3644 struct demuxer *d = mpctx->sources[j].demuxer;
3645 for (int i = 0; i < d->num_attachments; i++) {
3646 struct demux_attachment *att = d->attachments + i;
3647 if (extract_embedded_fonts
3648 && att->name && att->type && att->data && att->data_size
3649 && (strcmp(att->type, "application/x-truetype-font") == 0
3650 || strcmp(att->type, "application/x-font") == 0))
3651 ass_add_font(ass_library, att->name, att->data, att->data_size);
3655 #endif
3657 current_module="demux_open2";
3659 //file_format=demuxer->file_format;
3661 mpctx->d_audio=mpctx->demuxer->audio;
3662 mpctx->d_video=mpctx->demuxer->video;
3663 mpctx->d_sub=mpctx->demuxer->sub;
3665 if (ts_prog) {
3666 int tmp = ts_prog;
3667 mp_property_do("switch_program", M_PROPERTY_SET, &tmp, mpctx);
3669 // select audio stream
3670 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3672 // DUMP STREAMS:
3673 if((stream_dump_type)&&(stream_dump_type<4)){
3674 FILE *f;
3675 demux_stream_t *ds=NULL;
3676 current_module="dump";
3677 // select stream to dump
3678 switch(stream_dump_type){
3679 case 1: ds=mpctx->d_audio;break;
3680 case 2: ds=mpctx->d_video;break;
3681 case 3: ds=mpctx->d_sub;break;
3683 if(!ds){
3684 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"dump: FATAL: Selected stream missing!\n");
3685 exit_player(mpctx, EXIT_ERROR);
3687 // disable other streams:
3688 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3689 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3690 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3691 // let's dump it!
3692 f=fopen(stream_dump_name,"wb");
3693 if(!f){
3694 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3695 exit_player(mpctx, EXIT_ERROR);
3697 while(!ds->eof){
3698 unsigned char* start;
3699 int in_size=ds_get_packet(ds,&start);
3700 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3701 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3702 if(in_size>0) fwrite(start,in_size,1,f);
3703 if(dvd_last_chapter>0) {
3704 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3705 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3706 break;
3709 fclose(f);
3710 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3711 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3714 mpctx->sh_audio=mpctx->d_audio->sh;
3715 mpctx->sh_video=mpctx->d_video->sh;
3717 if(mpctx->sh_video){
3719 current_module="video_read_properties";
3720 if(!video_read_properties(mpctx->sh_video)) {
3721 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Video: Cannot read properties.\n");
3722 mpctx->sh_video=mpctx->d_video->sh=NULL;
3723 } else {
3724 mp_tmsg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
3725 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3726 mpctx->sh_video->fps,mpctx->sh_video->frametime
3729 /* need to set fps here for output encoders to pick it up in their init */
3730 if(force_fps){
3731 mpctx->sh_video->fps=force_fps;
3732 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3734 vo_fps = mpctx->sh_video->fps;
3736 if(!mpctx->sh_video->fps && !force_fps){
3737 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"FPS not specified in the header or invalid, use the -fps option.\n");
3738 mpctx->sh_video=mpctx->d_video->sh=NULL;
3744 if(!mpctx->sh_video && !mpctx->sh_audio){
3745 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "No stream found.\n");
3746 #ifdef CONFIG_DVBIN
3747 if(mpctx->stream->type == STREAMTYPE_DVB)
3749 int dir;
3750 int v = mpctx->last_dvb_step;
3751 if(v > 0)
3752 dir = DVB_CHANNEL_HIGHER;
3753 else
3754 dir = DVB_CHANNEL_LOWER;
3756 if(dvb_step_channel(mpctx->stream, dir)) {
3757 mpctx->stop_play = PT_NEXT_ENTRY;
3758 mpctx->dvbin_reopen = 1;
3761 #endif
3762 goto goto_next_file; // exit_player(_("Fatal error"));
3765 /* display clip info */
3766 demux_info_print(mpctx->demuxer);
3768 //================== Read SUBTITLES (DVD & TEXT) ==========================
3769 if(vo_spudec==NULL && mpctx->sh_video &&
3770 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3771 init_vo_spudec(mpctx);
3774 // after reading video params we should load subtitles because
3775 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3776 // check .sub
3777 current_module="read_subtitles_file";
3778 double sub_fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
3779 if(sub_name){
3780 for (i = 0; sub_name[i] != NULL; ++i)
3781 add_subtitles(mpctx, sub_name[i], sub_fps, 0);
3783 if(sub_auto) { // auto load sub file ...
3784 char *psub = get_path( "sub/" );
3785 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3786 int i = 0;
3787 free(psub); // release the buffer created by get_path() above
3788 while (tmp[i]) {
3789 add_subtitles(mpctx, tmp[i], sub_fps, 1);
3790 free(tmp[i++]);
3792 free(tmp);
3794 if (mpctx->set_of_sub_size > 0) {
3795 // setup global sub numbering
3796 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3797 mpctx->global_sub_size += mpctx->set_of_sub_size;
3801 if (mpctx->global_sub_size) {
3802 select_subtitle(mpctx);
3803 if(subdata)
3804 switch (stream_dump_type) {
3805 case 3: list_sub_file(subdata); break;
3806 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3807 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3808 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3809 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3810 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3814 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3815 filename_recode(mpctx->filename));
3816 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3817 if (mpctx->sh_video) {
3818 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3819 if (mpctx->sh_video->format >= 0x20202020)
3820 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3821 else
3822 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3823 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3824 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3825 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3826 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3827 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3829 if (mpctx->sh_audio) {
3830 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3831 if (mpctx->sh_audio->format >= 0x20202020)
3832 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3833 else
3834 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3835 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3836 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3837 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3839 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2f\n", mpctx->timeline ?
3840 mpctx->timeline[mpctx->num_timeline_parts].start :
3841 demuxer_get_time_length(mpctx->demuxer));
3842 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n",
3843 mpctx->stream->seek && (!mpctx->demuxer || mpctx->demuxer->seekable));
3844 if (mpctx->demuxer) {
3845 if (mpctx->demuxer->num_chapters == 0)
3846 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3847 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3850 if(!mpctx->sh_video) goto main; // audio-only
3852 if(!reinit_video_chain(mpctx)) {
3853 if(!mpctx->sh_video){
3854 if(!mpctx->sh_audio) goto goto_next_file;
3855 goto main; // exit_player(_("Fatal error"));
3859 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3860 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3862 #ifdef CONFIG_FREETYPE
3863 force_load_font = 1;
3864 #endif
3866 //================== MAIN: ==========================
3867 main:
3868 current_module="main";
3870 if(playing_msg) {
3871 char* msg = property_expand_string(mpctx, playing_msg);
3872 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3873 free(msg);
3877 // Disable the term OSD in verbose mode
3878 if(verbose) term_osd = 0;
3881 //int frame_corr_num=0; //
3882 //float v_frame=0; // Video
3883 //float num_frames=0; // number of frames played
3885 int frame_time_remaining=0; // flag
3886 int blit_frame=0;
3888 // Make sure old OSD does not stay around,
3889 // e.g. with -fixed-vo and same-resolution files
3890 clear_osd_msgs();
3891 update_osd_msg(mpctx);
3893 //================ SETUP AUDIO ==========================
3895 if(mpctx->sh_audio){
3896 reinit_audio_chain(mpctx);
3897 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3898 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3901 current_module="av_init";
3903 if(mpctx->sh_video){
3904 mpctx->sh_video->timer=0;
3905 if (! ignore_start)
3906 audio_delay += mpctx->sh_video->stream_delay;
3908 if(mpctx->sh_audio){
3909 if (start_volume >= 0)
3910 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3911 if (! ignore_start)
3912 audio_delay -= mpctx->sh_audio->stream_delay;
3913 mpctx->delay=-audio_delay;
3916 if(!mpctx->sh_audio){
3917 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Audio: no sound\n");
3918 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3919 ds_free_packs(mpctx->d_audio); // free buffered chunks
3920 //mpctx->d_audio->id=-2; // do not read audio chunks
3921 //uninit_player(mpctx, INITIALIZED_AO); // close device
3923 if(!mpctx->sh_video){
3924 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Video: no video\n");
3925 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3926 ds_free_packs(mpctx->d_video);
3927 mpctx->d_video->id=-2;
3928 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3931 if (!mpctx->sh_video && !mpctx->sh_audio)
3932 goto goto_next_file;
3934 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3935 if(force_fps && mpctx->sh_video){
3936 vo_fps = mpctx->sh_video->fps=force_fps;
3937 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3938 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx->sh_video->fps,mpctx->sh_video->frametime);
3941 mp_input_set_section(mpctx->input, NULL);
3942 //TODO: add desired (stream-based) sections here
3943 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3944 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3946 //==================== START PLAYING =======================
3948 if(opts->loop_times>1) opts->loop_times--; else
3949 if(opts->loop_times==1) opts->loop_times = -1;
3951 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Starting playback...\n");
3953 total_time_usage_start=GetTimer();
3954 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3955 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3956 play_n_frames=play_n_frames_mf;
3958 if(play_n_frames==0){
3959 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
3962 // If there's a timeline force an absolute seek to initialize state
3963 if (seek_to_sec || mpctx->timeline) {
3964 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3965 end_at.pos += seek_to_sec;
3968 if (end_at.type == END_AT_SIZE) {
3969 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Option -endpos in MPlayer does not yet support size units.\n");
3970 end_at.type = END_AT_NONE;
3973 #ifdef CONFIG_DVDNAV
3974 mp_dvdnav_context_free(mpctx);
3975 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3976 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3977 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3979 #endif
3981 get_relative_time(mpctx); // reset current delta
3982 mpctx->time_frame = 0;
3983 mpctx->drop_message_shown = 0;
3984 mpctx->update_video_immediately = true;
3985 mpctx->total_avsync_change = 0;
3986 // Make sure VO knows current pause state
3987 if (mpctx->sh_video)
3988 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
3989 NULL);
3991 while(!mpctx->stop_play){
3992 float aq_sleep_time=0;
3994 if(dvd_last_chapter>0) {
3995 int cur_chapter = get_current_chapter(mpctx);
3996 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3997 goto goto_next_file;
4000 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
4001 mpctx->sh_audio = mpctx->d_audio->sh;
4002 mpctx->sh_audio->ds = mpctx->d_audio;
4003 reinit_audio_chain(mpctx);
4006 /*========================== PLAY AUDIO ============================*/
4008 if (mpctx->sh_audio && !mpctx->paused)
4009 if (!fill_audio_out_buffers(mpctx))
4010 // at eof, all audio at least written to ao
4011 if (!mpctx->sh_video)
4012 mpctx->stop_play = AT_END_OF_FILE;
4015 if(!mpctx->sh_video) {
4016 // handle audio-only case:
4017 double a_pos=0;
4018 // sh_audio can be NULL due to video stream switching
4019 // TODO: handle this better
4020 if (mpctx->sh_audio)
4021 a_pos = playing_audio_pts(mpctx);
4023 print_status(mpctx, a_pos, false);
4025 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
4026 mpctx->stop_play = PT_NEXT_ENTRY;
4027 update_subtitles(mpctx, &mpctx->opts, NULL, a_pos, mpctx->video_offset,
4028 mpctx->d_sub, 0);
4029 update_osd_msg(mpctx);
4031 } else {
4033 /*========================== PLAY VIDEO ============================*/
4035 vo_pts=mpctx->sh_video->timer*90000.0;
4036 vo_fps=mpctx->sh_video->fps;
4038 blit_frame = mpctx->video_out->frame_loaded;
4039 if (!blit_frame) {
4040 double frame_time = update_video(mpctx);
4041 blit_frame = mpctx->video_out->frame_loaded;
4042 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
4043 if (mpctx->sh_video->vf_initialized < 0) {
4044 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "\nFATAL: Could not initialize video filters (-vf) or video output (-vo).\n");
4045 mpctx->stop_play = PT_NEXT_ENTRY;
4046 goto goto_next_file;
4048 if (blit_frame) {
4049 struct sh_video *sh_video = mpctx->sh_video;
4050 update_subtitles(mpctx, &mpctx->opts, sh_video, sh_video->pts,
4051 mpctx->video_offset, mpctx->d_sub, 0);
4052 update_teletext(sh_video, mpctx->demuxer, 0);
4053 update_osd_msg(mpctx);
4054 struct vf_instance *vf = mpctx->sh_video->vfilter;
4055 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
4056 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
4057 vo_osd_changed(0);
4059 if (frame_time < 0)
4060 mpctx->stop_play = AT_END_OF_FILE;
4061 else {
4062 if (mpctx->update_video_immediately) {
4063 // Show this frame immediately, rest normally
4064 mpctx->update_video_immediately = false;
4065 } else {
4066 mpctx->time_frame += frame_time / opts->playback_speed;
4067 adjust_sync(mpctx, frame_time);
4071 if (mpctx->timeline) {
4072 struct timeline_part *next = mpctx->timeline + mpctx->timeline_part + 1;
4073 if (mpctx->sh_video->pts >= next->start
4074 || mpctx->stop_play == AT_END_OF_FILE
4075 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
4076 seek(mpctx, next->start, SEEK_ABSOLUTE);
4077 continue;
4081 // ==========================================================================
4083 // current_module="draw_osd";
4084 // if(vo_config_count) mpctx->video_out->draw_osd();
4086 current_module="vo_check_events";
4087 vo_check_events(mpctx->video_out);
4089 #ifdef CONFIG_X11
4090 if (stop_xscreensaver) {
4091 current_module = "stop_xscreensaver";
4092 xscreensaver_heartbeat(mpctx->x11_state);
4094 #endif
4095 if (heartbeat_cmd) {
4096 static unsigned last_heartbeat;
4097 unsigned now = GetTimerMS();
4098 if (now - last_heartbeat > 30000) {
4099 last_heartbeat = now;
4100 system(heartbeat_cmd);
4104 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
4106 //====================== FLIP PAGE (VIDEO BLT): =========================
4108 current_module="flip_page";
4109 if (!frame_time_remaining && blit_frame) {
4110 unsigned int t2=GetTimer();
4111 unsigned int pts_us = mpctx->last_time + mpctx->time_frame * 1e6;
4112 int duration = -1;
4113 double pts2 = mpctx->video_out->next_pts2;
4114 if (pts2 != MP_NOPTS_VALUE && opts->correct_pts) {
4115 // expected A/V sync correction is ignored
4116 double diff = (pts2 - mpctx->sh_video->pts);
4117 diff /= opts->playback_speed;
4118 if (mpctx->time_frame < 0)
4119 diff += mpctx->time_frame;
4120 if (diff < 0)
4121 diff = 0;
4122 if (diff > 10)
4123 diff = 10;
4124 duration = diff * 1e6;
4126 vo_flip_page(mpctx->video_out, pts_us|1, duration);
4128 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
4129 vout_time_usage += mpctx->last_vo_flip_duration;
4130 if (mpctx->video_out->driver->flip_page_timed) {
4131 // No need to adjust sync based on flip speed
4132 mpctx->last_vo_flip_duration = 0;
4133 // For print_status - VO call finishing early is OK for sync
4134 mpctx->time_frame -= get_relative_time(mpctx);
4136 print_status(mpctx, MP_NOPTS_VALUE, true);
4138 else
4139 print_status(mpctx, MP_NOPTS_VALUE, false);
4141 //============================ Auto QUALITY ============================
4143 /*Output quality adjustments:*/
4144 if(auto_quality>0){
4145 current_module="autoq";
4146 // float total=0.000001f * (GetTimer()-aq_total_time);
4147 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4148 if(output_quality<auto_quality && aq_sleep_time>0)
4149 ++output_quality;
4150 else
4151 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4152 if(output_quality>1 && aq_sleep_time<0)
4153 --output_quality;
4154 else
4155 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4156 output_quality=0;
4157 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4158 set_video_quality(mpctx->sh_video,output_quality);
4161 if (!frame_time_remaining && blit_frame) {
4162 if (play_n_frames >= 0) {
4163 --play_n_frames;
4164 if (play_n_frames <= 0)
4165 mpctx->stop_play = PT_NEXT_ENTRY;
4167 if (mpctx->step_frames > 0) {
4168 mpctx->step_frames--;
4169 if (mpctx->step_frames == 0)
4170 pause_player(mpctx);
4175 // FIXME: add size based support for -endpos
4176 if (end_at.type == END_AT_TIME &&
4177 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
4178 mpctx->stop_play = PT_NEXT_ENTRY;
4180 } // end if(mpctx->sh_video)
4182 #ifdef CONFIG_DVDNAV
4183 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4184 nav_highlight_t hl;
4185 mp_dvdnav_get_highlight (mpctx->stream, &hl);
4186 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4187 vo_osd_changed (OSDTYPE_DVDNAV);
4189 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
4190 double ar = -1.0;
4191 if (stream_control (mpctx->demuxer->stream,
4192 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
4193 != STREAM_UNSUPPORTED)
4194 mpctx->sh_video->stream_aspect = ar;
4197 #endif
4199 //================= Keyboard events, SEEKing ====================
4201 current_module="key_events";
4204 while (1) {
4205 mp_cmd_t* cmd;
4206 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
4207 run_command(mpctx, cmd);
4208 mp_cmd_free(cmd);
4209 if (mpctx->stop_play)
4210 break;
4212 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
4213 || mpctx->abs_seek_pos)
4214 break;
4215 if (mpctx->sh_video) {
4216 update_osd_msg(mpctx);
4217 int hack = vo_osd_changed(0);
4218 vo_osd_changed(hack);
4219 if (hack)
4220 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
4221 add_step_frame(mpctx);
4222 break;
4224 else
4225 vo_osd_changed(0);
4227 pause_loop(mpctx);
4231 // handle -sstep
4232 if (step_sec > 0 && !mpctx->paused) {
4233 mpctx->osd_function=OSD_FFW;
4234 mpctx->rel_seek_secs+=step_sec;
4237 edl_update(mpctx);
4239 /* Looping. */
4240 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
4241 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
4243 if(opts->loop_times>1) opts->loop_times--; else
4244 if(opts->loop_times==1) opts->loop_times=-1;
4245 play_n_frames=play_n_frames_mf;
4246 mpctx->stop_play=0;
4247 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
4250 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
4251 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
4253 mpctx->rel_seek_secs=0;
4254 mpctx->abs_seek_pos=0;
4257 } // while(!mpctx->stop_play)
4259 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4261 #ifdef CONFIG_DVBIN
4262 if(mpctx->dvbin_reopen)
4264 mpctx->stop_play = 0;
4265 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4266 cache_uninit(mpctx->stream);
4267 mpctx->dvbin_reopen = 0;
4268 goto goto_enable_cache;
4270 #endif
4273 goto_next_file: // don't jump here after ao/vo/getch initialization!
4275 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4277 if(benchmark){
4278 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4279 double total_time_usage;
4280 total_time_usage_start=GetTimer()-total_time_usage_start;
4281 total_time_usage = (float)total_time_usage_start*0.000001;
4282 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4283 video_time_usage,vout_time_usage,audio_time_usage,
4284 total_time_usage-tot,total_time_usage);
4285 if(total_time_usage>0.0)
4286 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4287 100.0*video_time_usage/total_time_usage,
4288 100.0*vout_time_usage/total_time_usage,
4289 100.0*audio_time_usage/total_time_usage,
4290 100.0*(total_time_usage-tot)/total_time_usage,
4291 100.0);
4292 if(total_frame_cnt && frame_dropping)
4293 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4294 total_frame_cnt-drop_frame_cnt,
4295 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4296 drop_frame_cnt,
4297 100*drop_frame_cnt/total_frame_cnt,
4298 total_frame_cnt,
4299 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4302 // time to uninit all, except global stuff:
4303 uninit_player(mpctx, INITIALIZED_ALL-(opts->fixed_vo?INITIALIZED_VO:0));
4305 if(mpctx->set_of_sub_size > 0) {
4306 current_module="sub_free";
4307 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4308 sub_free(mpctx->set_of_subtitles[i]);
4309 #ifdef CONFIG_ASS
4310 if(mpctx->set_of_ass_tracks[i])
4311 ass_free_track( mpctx->set_of_ass_tracks[i] );
4312 #endif
4314 mpctx->set_of_sub_size = 0;
4316 vo_sub_last = vo_sub=NULL;
4317 subdata=NULL;
4318 #ifdef CONFIG_ASS
4319 ass_track = NULL;
4320 if(ass_library)
4321 ass_clear_fonts(ass_library);
4322 #endif
4324 if (!mpctx->stop_play) // In case some goto jumped here...
4325 mpctx->stop_play = PT_NEXT_ENTRY;
4327 int playtree_direction = 1;
4329 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4330 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4331 play_tree_iter_free(mpctx->playtree_iter);
4332 mpctx->playtree_iter = NULL;
4334 mpctx->play_tree_step = 1;
4335 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4336 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4337 if(mpctx->playtree_iter) {
4338 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4339 play_tree_iter_free(mpctx->playtree_iter);
4340 mpctx->playtree_iter = NULL;
4343 } else if (mpctx->stop_play == PT_STOP) {
4344 play_tree_iter_free(mpctx->playtree_iter);
4345 mpctx->playtree_iter = NULL;
4346 } else { // NEXT PREV SRC
4347 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4350 while(mpctx->playtree_iter != NULL) {
4351 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4352 if(mpctx->filename == NULL) {
4353 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4354 play_tree_iter_free(mpctx->playtree_iter);
4355 mpctx->playtree_iter = NULL;
4357 } else
4358 break;
4361 if(mpctx->playtree_iter != NULL || player_idle_mode){
4362 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4363 mpctx->stop_play = 0;
4364 goto play_next_file;
4368 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4370 return 1;
4372 #endif /* DISABLE_MAIN */