Support for multiple editions in Matroska
[mplayer.git] / mplayer.c
blob671e8c503940e5e9e0bf25ff76ecb50c09a89444
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 #endif
704 current_module="exit_player";
706 // free mplayer config
707 if(mpctx->mconfig)
708 m_config_free(mpctx->mconfig);
710 if(mpctx->playtree)
711 play_tree_free(mpctx->playtree, 1);
713 talloc_free(mpctx->key_fifo);
715 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
716 switch(how) {
717 case EXIT_QUIT:
718 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Quit");
719 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
720 break;
721 case EXIT_EOF:
722 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","End of file");
723 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
724 break;
725 case EXIT_ERROR:
726 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Fatal error");
727 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
728 break;
729 default:
730 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
732 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
734 exit(rc);
737 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
739 exit_player_with_rc(mpctx, how, 1);
742 #ifndef __MINGW32__
743 static void child_sighandler(int x){
744 pid_t pid;
745 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
747 #endif
749 #ifdef CONFIG_CRASH_DEBUG
750 static char *prog_path;
751 static int crash_debug = 0;
752 #endif
754 static void exit_sighandler(int x){
755 static int sig_count=0;
756 #ifdef CONFIG_CRASH_DEBUG
757 if (!crash_debug || x != SIGTRAP)
758 #endif
759 ++sig_count;
760 if(sig_count==5)
762 /* We're crashing bad and can't uninit cleanly :(
763 * by popular request, we make one last (dirty)
764 * effort to restore the user's
765 * terminal. */
766 getch2_disable();
767 exit(1);
769 if(sig_count==6) exit(1);
770 if(sig_count>6){
771 // can't stop :(
772 #ifndef __MINGW32__
773 kill(getpid(),SIGKILL);
774 #endif
776 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"\n" "\nMPlayer interrupted by signal %d in module: %s\n",x,
777 current_module?current_module:"unknown"
779 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
780 if(sig_count<=1)
781 switch(x){
782 case SIGINT:
783 case SIGQUIT:
784 case SIGTERM:
785 case SIGKILL:
786 async_quit_request = 1;
787 return; // killed from keyboard (^C) or killed [-9]
788 case SIGILL:
789 #if CONFIG_RUNTIME_CPUDETECT
790 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL_RTCpuSel);
791 #else
792 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL);
793 #endif
794 case SIGFPE:
795 case SIGSEGV:
796 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGSEGV_SIGFPE);
797 default:
798 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGCRASH);
799 #ifdef CONFIG_CRASH_DEBUG
800 if (crash_debug) {
801 int gdb_pid;
802 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
803 gdb_pid = fork();
804 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
805 if (gdb_pid == 0) { // We are the child
806 char spid[20];
807 snprintf(spid, sizeof(spid), "%i", getppid());
808 getch2_disable(); // allow terminal to work properly with gdb
809 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
810 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
811 } else if (gdb_pid < 0)
812 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
813 else {
814 waitpid(gdb_pid, NULL, 0);
816 if (x == SIGTRAP) return;
818 #endif
820 getch2_disable();
821 exit(1);
824 #include "cfg-mplayer.h"
826 static int cfg_include(m_option_t *conf, char *filename)
828 return m_config_parse_config_file(conf->priv, filename);
831 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
833 char *conffile;
834 int conffile_fd;
835 if (!disable_system_conf &&
836 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
837 exit_player(mpctx, EXIT_NONE);
838 if ((conffile = get_path("")) == NULL) {
839 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"Cannot find HOME directory.\n");
840 } else {
841 #ifdef __MINGW32__
842 mkdir(conffile);
843 #else
844 mkdir(conffile, 0777);
845 #endif
846 free(conffile);
847 if ((conffile = get_path("config")) == NULL) {
848 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
849 } else {
850 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
851 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Creating config file: %s\n", conffile);
852 write(conffile_fd, default_config, strlen(default_config));
853 close(conffile_fd);
855 if (!disable_user_conf &&
856 m_config_parse_config_file(conf, conffile) < 0)
857 exit_player(mpctx, EXIT_NONE);
858 free(conffile);
863 #define PROFILE_CFG_PROTOCOL "protocol."
865 static void load_per_protocol_config (m_config_t* conf, const char *const file)
867 char *str;
868 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
869 m_profile_t *p;
871 /* does filename actually uses a protocol ? */
872 str = strstr (file, "://");
873 if (!str)
874 return;
876 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
877 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
878 p = m_config_get_profile (conf, protocol);
879 if (p)
881 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading protocol-related profile '%s'\n", protocol);
882 m_config_set_profile(conf,p);
886 #define PROFILE_CFG_EXTENSION "extension."
888 static void load_per_extension_config (m_config_t* conf, const char *const file)
890 char *str;
891 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
892 m_profile_t *p;
894 /* does filename actually have an extension ? */
895 str = strrchr (file, '.');
896 if (!str)
897 return;
899 sprintf (extension, PROFILE_CFG_EXTENSION);
900 strncat (extension, ++str, 7);
901 p = m_config_get_profile (conf, extension);
902 if (p)
904 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", extension);
905 m_config_set_profile(conf,p);
909 #define PROFILE_CFG_VO "vo."
910 #define PROFILE_CFG_AO "ao."
912 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
914 char profile[strlen (cfg) + strlen (out) + 1];
915 m_profile_t *p;
917 sprintf (profile, "%s%s", cfg, out);
918 p = m_config_get_profile (conf, profile);
919 if (p)
921 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", profile);
922 m_config_set_profile(conf,p);
927 * Tries to load a config file
928 * @return 0 if file was not found, 1 otherwise
930 static int try_load_config(m_config_t *conf, const char *file)
932 struct stat st;
933 if (stat(file, &st))
934 return 0;
935 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Loading config '%s'\n", file);
936 m_config_parse_config_file (conf, file);
937 return 1;
940 static void load_per_file_config (m_config_t* conf, const char *const file)
942 char *confpath;
943 char cfg[PATH_MAX];
944 char *name;
946 if (strlen(file) > PATH_MAX - 14) {
947 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Filename is too long, can not load file or directory specific config files\n");
948 return;
950 sprintf (cfg, "%s.conf", file);
952 name = strrchr(cfg, '/');
953 if (HAVE_DOS_PATHS) {
954 char *tmp = strrchr(cfg, '\\');
955 if (!name || tmp > name)
956 name = tmp;
957 tmp = strrchr(cfg, ':');
958 if (!name || tmp > name)
959 name = tmp;
961 if (!name)
962 name = cfg;
963 else
964 name++;
966 if (use_filedir_conf) {
967 char dircfg[PATH_MAX];
968 strcpy(dircfg, cfg);
969 strcpy(dircfg + (name - cfg), "mplayer.conf");
970 try_load_config(conf, dircfg);
972 if (try_load_config(conf, cfg))
973 return;
976 if ((confpath = get_path (name)) != NULL)
978 try_load_config(conf, confpath);
980 free (confpath);
984 /* When libmpdemux performs a blocking operation (network connection or
985 * cache filling) if the operation fails we use this function to check
986 * if it was interrupted by the user.
987 * The function returns a new value for eof. */
988 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
990 mp_cmd_t* cmd;
991 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
992 switch(cmd->id) {
993 case MP_CMD_QUIT:
994 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
995 case MP_CMD_PLAY_TREE_STEP: {
996 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
997 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
998 } break;
999 case MP_CMD_PLAY_TREE_UP_STEP: {
1000 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
1001 } break;
1002 case MP_CMD_PLAY_ALT_SRC_STEP: {
1003 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
1004 } break;
1006 mp_cmd_free(cmd);
1008 return stop_play;
1011 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
1013 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
1015 play_tree_add_bpf(entry,mpctx->filename);
1018 if(!entry) {
1019 entry = mpctx->playtree_iter->tree;
1020 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1021 return PT_NEXT_ENTRY;
1023 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
1024 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1025 return PT_NEXT_ENTRY;
1028 play_tree_remove(entry,1,1);
1029 return PT_NEXT_SRC;
1031 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
1032 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1033 entry = mpctx->playtree_iter->tree;
1034 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1035 return PT_NEXT_ENTRY;
1037 play_tree_remove(entry,1,1);
1039 return PT_NEXT_SRC;
1042 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1044 struct MPOpts *opts = &mpctx->opts;
1045 sub_data *subd;
1046 #ifdef CONFIG_ASS
1047 ASS_Track *asst = 0;
1048 #endif
1050 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1051 return;
1054 subd = sub_read_file(filename, fps);
1055 #ifdef CONFIG_ASS
1056 if (opts->ass_enabled)
1057 #ifdef CONFIG_ICONV
1058 asst = ass_read_file(ass_library, filename, sub_cp);
1059 #else
1060 asst = ass_read_file(ass_library, filename, 0);
1061 #endif
1062 if (opts->ass_enabled && subd && !asst)
1063 asst = ass_read_subdata(ass_library, subd, fps);
1065 if (!asst && !subd)
1066 #else
1067 if(!subd)
1068 #endif
1069 mp_tmsg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, "Cannot load subtitles: %s\n",
1070 filename_recode(filename));
1072 #ifdef CONFIG_ASS
1073 if (!asst && !subd) return;
1074 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1075 #else
1076 if (!subd) return;
1077 #endif
1078 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1079 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1080 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1081 filename_recode(filename));
1082 ++mpctx->set_of_sub_size;
1083 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "SUB: Added subtitle file (%d): %s\n", mpctx->set_of_sub_size,
1084 filename_recode(filename));
1087 void init_vo_spudec(struct MPContext *mpctx)
1089 if (vo_spudec)
1090 spudec_free(vo_spudec);
1091 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1092 vo_spudec = NULL;
1093 if (spudec_ifo) {
1094 unsigned int palette[16], width, height;
1095 current_module="spudec_init_vobsub";
1096 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1097 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1100 #ifdef CONFIG_DVDREAD
1101 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1102 current_module="spudec_init_dvdread";
1103 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1104 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1105 NULL, 0);
1107 #endif
1109 #ifdef CONFIG_DVDNAV
1110 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1111 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1112 current_module="spudec_init_dvdnav";
1113 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1115 #endif
1117 if (vo_spudec==NULL) {
1118 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1119 current_module="spudec_init_normal";
1120 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1121 spudec_set_font_factor(vo_spudec,font_factor);
1124 if (vo_spudec!=NULL) {
1125 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1126 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1131 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1132 * make it all work is to use the builtin SDL-bootstrap code, which
1133 * will be done automatically by replacing our main() if we include SDL.h.
1135 #if defined(__APPLE__) && defined(CONFIG_SDL)
1136 #include <SDL.h>
1137 #endif
1140 * \brief append a formatted string
1141 * \param buf buffer to print into
1142 * \param pos position of terminating 0 in buf
1143 * \param len maximum number of characters in buf, not including terminating 0
1144 * \param format printf format string
1146 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1148 va_list va;
1149 va_start(va, format);
1150 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1151 va_end(va);
1152 if (*pos >= len ) {
1153 buf[len] = 0;
1154 *pos = len;
1159 * \brief append time in the hh:mm:ss.f format
1160 * \param buf buffer to print into
1161 * \param pos position of terminating 0 in buf
1162 * \param len maximum number of characters in buf, not including terminating 0
1163 * \param time time value to convert/append
1165 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1166 long tenths = 10 * time;
1167 int f1 = tenths % 10;
1168 int ss = (tenths / 10) % 60;
1169 int mm = (tenths / 600) % 60;
1170 int hh = tenths / 36000;
1171 if (time <= 0) {
1172 saddf(buf, pos, len, "unknown");
1173 return;
1175 if (hh > 0)
1176 saddf(buf, pos, len, "%2d:", hh);
1177 if (hh > 0 || mm > 0)
1178 saddf(buf, pos, len, "%02d:", mm);
1179 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1182 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1184 struct MPOpts *opts = &mpctx->opts;
1185 sh_video_t * const sh_video = mpctx->sh_video;
1187 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1188 a_pos = playing_audio_pts(mpctx);
1189 if (mpctx->sh_audio && sh_video && at_frame) {
1190 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1191 if (mpctx->time_frame > 0)
1192 mpctx->last_av_difference += mpctx->time_frame * opts->playback_speed;
1193 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1194 && !mpctx->drop_message_shown) {
1195 mp_tmsg(MSGT_AVSYNC,MSGL_WARN,SystemTooSlow);
1196 mpctx->drop_message_shown = true;
1199 if (quiet)
1200 return;
1203 int width;
1204 char *line;
1205 unsigned pos = 0;
1206 get_screen_size();
1207 if (screen_width > 0)
1208 width = screen_width;
1209 else
1210 width = 80;
1211 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1212 /* Windows command line is broken (MinGW's rxvt works, but we
1213 * should not depend on that). */
1214 width--;
1215 #endif
1216 line = malloc(width + 1); // one additional char for the terminating null
1218 // Audio time
1219 if (mpctx->sh_audio) {
1220 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1221 if (!sh_video) {
1222 float len = demuxer_get_time_length(mpctx->demuxer);
1223 saddf(line, &pos, width, "(");
1224 sadd_hhmmssf(line, &pos, width, a_pos);
1225 saddf(line, &pos, width, ") of %.1f (", len);
1226 sadd_hhmmssf(line, &pos, width, len);
1227 saddf(line, &pos, width, ") ");
1231 // Video time
1232 if (sh_video)
1233 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1235 // A-V sync
1236 if (mpctx->sh_audio && sh_video)
1237 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1238 mpctx->last_av_difference, mpctx->total_avsync_change);
1240 // Video stats
1241 if (sh_video)
1242 saddf(line, &pos, width, "%3d/%3d ",
1243 (int)sh_video->num_frames,
1244 (int)sh_video->num_frames_decoded);
1246 // CPU usage
1247 if (sh_video) {
1248 if (sh_video->timer > 0.5)
1249 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1250 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1251 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1252 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1253 else
1254 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1255 } else if (mpctx->sh_audio) {
1256 if (mpctx->delay > 0.5)
1257 saddf(line, &pos, width, "%4.1f%% ",
1258 100.0*audio_time_usage/(double)mpctx->delay);
1259 else
1260 saddf(line, &pos, width, "??,?%% ");
1263 // VO stats
1264 if (sh_video)
1265 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1267 #ifdef CONFIG_STREAM_CACHE
1268 // cache stats
1269 if (stream_cache_size > 0)
1270 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1271 #endif
1273 // other
1274 if (opts->playback_speed != 1)
1275 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1277 // end
1278 if (erase_to_end_of_line) {
1279 line[pos] = 0;
1280 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1281 } else {
1282 memset(&line[pos], ' ', width - pos);
1283 line[width] = 0;
1284 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1286 free(line);
1290 * \brief build a chain of audio filters that converts the input format
1291 * to the ao's format, taking into account the current playback_speed.
1292 * \param sh_audio describes the requested input format of the chain.
1293 * \param ao_data describes the requested output format of the chain.
1295 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1297 struct MPOpts *opts = &mpctx->opts;
1298 int new_srate;
1299 int result;
1300 if (!sh_audio)
1302 mpctx->mixer.afilter = NULL;
1303 return 0;
1305 if(af_control_any_rev(sh_audio->afilter,
1306 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1307 &opts->playback_speed)) {
1308 new_srate = sh_audio->samplerate;
1309 } else {
1310 new_srate = sh_audio->samplerate * opts->playback_speed;
1311 if (new_srate != ao_data->samplerate) {
1312 // limits are taken from libaf/af_resample.c
1313 if (new_srate < 8000)
1314 new_srate = 8000;
1315 if (new_srate > 192000)
1316 new_srate = 192000;
1317 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1320 result = init_audio_filters(sh_audio, new_srate,
1321 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1322 mpctx->mixer.afilter = sh_audio->afilter;
1323 return result;
1327 typedef struct mp_osd_msg mp_osd_msg_t;
1328 struct mp_osd_msg {
1329 /// Previous message on the stack.
1330 mp_osd_msg_t* prev;
1331 /// Message text.
1332 char msg[128];
1333 int id,level,started;
1334 /// Display duration in ms.
1335 unsigned time;
1338 /// OSD message stack.
1339 static mp_osd_msg_t* osd_msg_stack = NULL;
1342 * \brief Add a message on the OSD message stack
1344 * If a message with the same id is already present in the stack
1345 * it is pulled on top of the stack, otherwise a new message is created.
1349 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1350 mp_osd_msg_t *msg,*last=NULL;
1351 va_list va;
1352 int r;
1354 // look if the id is already in the stack
1355 for(msg = osd_msg_stack ; msg && msg->id != id ;
1356 last = msg, msg = msg->prev);
1357 // not found: alloc it
1358 if(!msg) {
1359 msg = calloc(1,sizeof(mp_osd_msg_t));
1360 msg->prev = osd_msg_stack;
1361 osd_msg_stack = msg;
1362 } else if(last) { // found, but it's not on top of the stack
1363 last->prev = msg->prev;
1364 msg->prev = osd_msg_stack;
1365 osd_msg_stack = msg;
1367 // write the msg
1368 va_start(va,fmt);
1369 r = vsnprintf(msg->msg, 128, fmt, va);
1370 va_end(va);
1371 if(r >= 128) msg->msg[127] = 0;
1372 // set id and time
1373 msg->id = id;
1374 msg->level = level;
1375 msg->time = time;
1380 * \brief Remove a message from the OSD stack
1382 * This function can be used to get rid of a message right away.
1386 void rm_osd_msg(int id) {
1387 mp_osd_msg_t *msg,*last=NULL;
1389 // Search for the msg
1390 for(msg = osd_msg_stack ; msg && msg->id != id ;
1391 last = msg, msg = msg->prev);
1392 if(!msg) return;
1394 // Detach it from the stack and free it
1395 if(last)
1396 last->prev = msg->prev;
1397 else
1398 osd_msg_stack = msg->prev;
1399 free(msg);
1403 * \brief Remove all messages from the OSD stack
1407 static void clear_osd_msgs(void) {
1408 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1409 while(msg) {
1410 prev = msg->prev;
1411 free(msg);
1412 msg = prev;
1414 osd_msg_stack = NULL;
1418 * \brief Get the current message from the OSD stack.
1420 * This function decrements the message timer and destroys the old ones.
1421 * The message that should be displayed is returned (if any).
1425 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1427 struct MPOpts *opts = &mpctx->opts;
1428 mp_osd_msg_t *msg,*prev,*last = NULL;
1429 static unsigned last_update = 0;
1430 unsigned now = GetTimerMS();
1431 unsigned diff;
1432 char hidden_dec_done = 0;
1434 if (mpctx->osd_visible) {
1435 // 36000000 means max timed visibility is 1 hour into the future, if
1436 // the difference is greater assume it's wrapped around from below 0
1437 if (mpctx->osd_visible - now > 36000000) {
1438 mpctx->osd_visible = 0;
1439 vo_osd_progbar_type = -1; // disable
1440 vo_osd_changed(OSDTYPE_PROGBAR);
1441 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1444 if (mpctx->osd_show_percentage_until - now > 36000000)
1445 mpctx->osd_show_percentage_until = 0;
1447 if(!last_update) last_update = now;
1448 diff = now >= last_update ? now - last_update : 0;
1450 last_update = now;
1452 // Look for the first message in the stack with high enough level.
1453 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1454 prev = msg->prev;
1455 if (msg->level > opts->osd_level && hidden_dec_done)
1456 continue;
1457 // The message has a high enough level or it is the first hidden one
1458 // in both cases we decrement the timer or kill it.
1459 if(!msg->started || msg->time > diff) {
1460 if(msg->started) msg->time -= diff;
1461 else msg->started = 1;
1462 // display it
1463 if (msg->level <= opts->osd_level)
1464 return msg;
1465 hidden_dec_done = 1;
1466 continue;
1468 // kill the message
1469 free(msg);
1470 if(last) {
1471 last->prev = prev;
1472 msg = last;
1473 } else {
1474 osd_msg_stack = prev;
1475 msg = NULL;
1478 // Nothing found
1479 return NULL;
1483 * \brief Display the OSD bar.
1485 * Display the OSD bar or fall back on a simple message.
1489 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1490 struct MPOpts *opts = &mpctx->opts;
1491 if (opts->osd_level < 1)
1492 return;
1494 if(mpctx->sh_video) {
1495 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1496 vo_osd_progbar_type = type;
1497 vo_osd_progbar_value = 256*(val-min)/(max-min);
1498 vo_osd_changed(OSDTYPE_PROGBAR);
1499 return;
1502 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1503 name, ROUND(100*(val-min)/(max-min)));
1507 * \brief Display text subtitles on the OSD
1509 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs)
1511 int i;
1512 vo_sub = subs;
1513 vo_osd_changed(OSDTYPE_SUBTITLE);
1514 if (!mpctx->sh_video) {
1515 // reverse order, since newest set_osd_msg is displayed first
1516 for (i = SUB_MAX_TEXT - 1; i >= 0; i--) {
1517 if (!subs || i >= subs->lines || !subs->text[i])
1518 rm_osd_msg(OSD_MSG_SUB_BASE + i);
1519 else {
1520 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1521 int display_time = i == subs->lines - 1 ? 180000 : 2000;
1522 set_osd_msg(OSD_MSG_SUB_BASE + i, 1, display_time, "%s", subs->text[i]);
1529 * \brief Update the OSD message line.
1531 * This function displays the current message on the vo OSD or on the term.
1532 * If the stack is empty and the OSD level is high enough the timer
1533 * is displayed (only on the vo OSD).
1537 static void update_osd_msg(struct MPContext *mpctx)
1539 struct MPOpts *opts = &mpctx->opts;
1540 mp_osd_msg_t *msg;
1541 struct osd_state *osd = mpctx->osd;
1542 char osd_text_timer[128];
1544 if (mpctx->add_osd_seek_info) {
1545 double percentage;
1546 if (mpctx->timeline && mpctx->sh_video)
1547 percentage = mpctx->sh_video->pts * 100 /
1548 mpctx->timeline[mpctx->num_timeline_parts].start;
1549 else
1550 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1551 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1552 if (mpctx->sh_video)
1553 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1554 mpctx->add_osd_seek_info = false;
1557 // Look if we have a msg
1558 if((msg = get_osd_msg(mpctx))) {
1559 if (strcmp(osd->osd_text, msg->msg)) {
1560 strncpy(osd->osd_text, msg->msg, 127);
1561 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1562 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1564 return;
1567 if(mpctx->sh_video) {
1568 // fallback on the timer
1569 if (opts->osd_level >= 2) {
1570 int len;
1571 if (mpctx->timeline)
1572 len = mpctx->timeline[mpctx->num_timeline_parts].start;
1573 else
1574 len = demuxer_get_time_length(mpctx->demuxer);
1575 int percentage = -1;
1576 char percentage_text[10];
1577 int pts = demuxer_get_current_time(mpctx->demuxer);
1579 if (mpctx->osd_show_percentage_until)
1580 if (mpctx->timeline)
1581 percentage = mpctx->sh_video->pts * 100 /
1582 mpctx->timeline[mpctx->num_timeline_parts].start;
1583 else
1584 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1586 if (percentage >= 0)
1587 snprintf(percentage_text, 9, " (%d%%)", percentage);
1588 else
1589 percentage_text[0] = 0;
1591 if (opts->osd_level == 3)
1592 snprintf(osd_text_timer, 63,
1593 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1594 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1595 len/3600,(len/60)%60,len%60,percentage_text);
1596 else
1597 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1598 mpctx->osd_function,pts/3600,(pts/60)%60,
1599 pts%60,percentage_text);
1600 } else
1601 osd_text_timer[0]=0;
1603 if (strcmp(osd->osd_text, osd_text_timer)) {
1604 strncpy(osd->osd_text, osd_text_timer, 63);
1605 vo_osd_changed(OSDTYPE_OSD);
1607 return;
1610 // Clear the term osd line
1611 if (term_osd && osd->osd_text[0]) {
1612 osd->osd_text[0] = 0;
1613 printf("%s\n",term_osd_esc);
1617 ///@}
1618 // OSDMsgStack
1621 void reinit_audio_chain(struct MPContext *mpctx)
1623 struct MPOpts *opts = &mpctx->opts;
1624 if (!mpctx->sh_audio)
1625 return;
1626 current_module="init_audio_codec";
1627 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1628 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1629 goto init_error;
1631 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1632 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1635 current_module="af_preinit";
1636 ao_data.samplerate=force_srate;
1637 ao_data.channels=0;
1638 ao_data.format=audio_output_format;
1639 #if 1
1640 // first init to detect best values
1641 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1642 // input:
1643 mpctx->sh_audio->samplerate,
1644 // output:
1645 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1646 mp_tmsg(MSGT_CPLAYER,MSGL_ERR, "Error at audio filter chain "
1647 "pre-init!\n");
1648 exit_player(mpctx, EXIT_ERROR);
1650 #endif
1651 current_module="ao2_init";
1652 mpctx->audio_out = init_best_audio_out(opts->audio_driver_list,
1653 0, // plugin flag
1654 ao_data.samplerate,
1655 ao_data.channels,
1656 ao_data.format, 0);
1657 if(!mpctx->audio_out){
1658 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Could not open/initialize audio device -> no sound.\n");
1659 goto init_error;
1661 mpctx->initialized_flags|=INITIALIZED_AO;
1662 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1663 mpctx->audio_out->info->short_name,
1664 ao_data.samplerate, ao_data.channels,
1665 af_fmt2str_short(ao_data.format),
1666 af_fmt2bits(ao_data.format)/8 );
1667 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1668 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1669 if(strlen(mpctx->audio_out->info->comment) > 0)
1670 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1671 // init audio filters:
1672 #if 1
1673 current_module="af_init";
1674 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1675 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter/ao format!\n");
1676 goto init_error;
1678 #endif
1679 mpctx->mixer.audio_out = mpctx->audio_out;
1680 mpctx->mixer.volstep = volstep;
1681 return;
1683 init_error:
1684 uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec and possibly AO
1685 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1686 mpctx->d_audio->id = -2;
1690 ///@}
1691 // Command2Property
1694 // Return pts value corresponding to the end point of audio written to the
1695 // ao so far.
1696 static double written_audio_pts(struct MPContext *mpctx)
1698 sh_audio_t *sh_audio = mpctx->sh_audio;
1699 demux_stream_t *d_audio = mpctx->d_audio;
1700 double buffered_output;
1701 // first calculate the end pts of audio that has been output by decoder
1702 double a_pts = sh_audio->pts;
1703 if (a_pts != MP_NOPTS_VALUE)
1704 // Good, decoder supports new way of calculating audio pts.
1705 // sh_audio->pts is the timestamp of the latest input packet with
1706 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1707 // the amount of bytes the decoder has written after that timestamp.
1708 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1709 else {
1710 // Decoder doesn't support new way of calculating pts (or we're
1711 // being called before it has decoded anything with known timestamp).
1712 // Use the old method of audio pts calculation: take the timestamp
1713 // of last packet with known pts the decoder has read data from,
1714 // and add amount of bytes read after the beginning of that packet
1715 // divided by input bps. This will be inaccurate if the input/output
1716 // ratio is not constant for every audio packet or if it is constant
1717 // but not accurately known in sh_audio->i_bps.
1719 a_pts = d_audio->pts;
1720 // ds_tell_pts returns bytes read after last timestamp from
1721 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1722 // it has read but not decoded
1723 if (sh_audio->i_bps)
1724 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1725 (double)sh_audio->i_bps;
1727 // Now a_pts hopefully holds the pts for end of audio from decoder.
1728 // Substract data in buffers between decoder and audio out.
1730 // Decoded but not filtered
1731 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1733 // Data buffered in audio filters, measured in bytes of "missing" output
1734 buffered_output = af_calc_delay(sh_audio->afilter);
1736 // Data that was ready for ao but was buffered because ao didn't fully
1737 // accept everything to internal buffers yet
1738 buffered_output += sh_audio->a_out_buffer_len;
1740 // Filters divide audio length by playback_speed, so multiply by it
1741 // to get the length in original units without speedup or slowdown
1742 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1744 return a_pts + mpctx->video_offset;
1747 // Return pts value corresponding to currently playing audio.
1748 double playing_audio_pts(struct MPContext *mpctx)
1750 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1751 mpctx->audio_out->get_delay();
1754 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1755 struct MPOpts *opts = &mpctx->opts;
1756 // check for frame-drop:
1757 current_module = "check_framedrop";
1758 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1759 static int dropped_frames;
1760 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1761 float d = delay-mpctx->delay;
1762 ++total_frame_cnt;
1763 // we should avoid dropping too many frames in sequence unless we
1764 // are too late. and we allow 100ms A-V delay here:
1765 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1766 && !mpctx->update_video_immediately) {
1767 ++drop_frame_cnt;
1768 ++dropped_frames;
1769 return frame_dropping;
1770 } else
1771 dropped_frames = 0;
1773 return 0;
1777 #ifdef HAVE_RTC
1778 int rtc_fd = -1;
1779 #endif
1781 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1783 #ifdef HAVE_RTC
1784 if (rtc_fd >= 0){
1785 // -------- RTC -----------
1786 current_module="sleep_rtc";
1787 while (time_frame > 0.000) {
1788 unsigned long rtc_ts;
1789 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1790 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC read error: %s\n", strerror(errno));
1791 time_frame -= get_relative_time(mpctx);
1793 } else
1794 #endif
1796 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1797 // unnecessarily high CPU usage
1798 float margin = softsleep ? 0.011 : 0;
1799 current_module = "sleep_timer";
1800 while (time_frame > margin) {
1801 usec_sleep(1000000 * (time_frame - margin));
1802 time_frame -= get_relative_time(mpctx);
1804 if (softsleep){
1805 current_module = "sleep_soft";
1806 if (time_frame < 0)
1807 mp_tmsg(MSGT_AVSYNC, MSGL_WARN, "Warning! Softsleep underflow!\n");
1808 while (time_frame > 0)
1809 time_frame -= get_relative_time(mpctx); // burn the CPU
1812 return time_frame;
1815 static void select_subtitle(MPContext *mpctx)
1817 struct MPOpts *opts = &mpctx->opts;
1818 // find the best sub to use
1819 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
1820 mpctx->global_sub_pos = -1; // no subs by default
1821 if (vobsub_index_id >= 0) {
1822 // if user asks for a vobsub id, use that first.
1823 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
1824 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1825 // if user asks for a dvd sub id, use that next.
1826 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1827 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
1828 // if there are text subs to use, use those. (autosubs come last here)
1829 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
1830 } else if (opts->sub_id == -1 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1831 // finally select subs by language and container hints
1832 if (opts->sub_id == -1 && dvdsub_lang)
1833 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
1834 if (opts->sub_id == -1)
1835 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
1836 if (opts->sub_id >= 0)
1837 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1839 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
1840 mpctx->global_sub_pos--;
1841 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
1844 #ifdef CONFIG_DVDNAV
1845 #ifndef FF_B_TYPE
1846 #define FF_B_TYPE 3
1847 #endif
1848 /// store decoded video image
1849 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1850 mp_image_t *from_mpi) {
1851 mp_image_t *mpi;
1853 /// Do not store B-frames
1854 if (from_mpi->pict_type == FF_B_TYPE)
1855 return to_mpi;
1857 if (to_mpi &&
1858 to_mpi->w == from_mpi->w &&
1859 to_mpi->h == from_mpi->h &&
1860 to_mpi->imgfmt == from_mpi->imgfmt)
1861 mpi = to_mpi;
1862 else {
1863 if (to_mpi)
1864 free_mp_image(to_mpi);
1865 if (from_mpi->w == 0 || from_mpi->h == 0)
1866 return NULL;
1867 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1870 copy_mpi(mpi,from_mpi);
1871 return mpi;
1874 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1875 struct MPOpts *opts = &ctx->opts;
1876 if (ctx->sh_video) {
1877 /// clear video pts
1878 ctx->d_video->pts = 0.0f;
1879 ctx->sh_video->pts = 0.0f;
1880 ctx->sh_video->i_pts = 0.0f;
1881 ctx->sh_video->last_pts = 0.0f;
1882 ctx->sh_video->num_buffered_pts = 0;
1883 ctx->sh_video->num_frames = 0;
1884 ctx->sh_video->num_frames_decoded = 0;
1885 ctx->sh_video->timer = 0.0f;
1886 ctx->sh_video->stream_delay = 0.0f;
1887 ctx->sh_video->timer = 0;
1888 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1891 if (ctx->sh_audio) {
1892 /// free audio packets and reset
1893 ds_free_packs(ctx->d_audio);
1894 audio_delay -= ctx->sh_audio->stream_delay;
1895 ctx->delay =- audio_delay;
1896 ctx->audio_out->reset();
1897 resync_audio_stream(ctx->sh_audio);
1900 audio_delay = 0.0f;
1901 ctx->global_sub_size = ctx->global_sub_indices[SUB_SOURCE_DEMUX] + mp_dvdnav_number_of_subs(ctx->stream);
1902 if (dvdsub_lang && opts->sub_id == dvdsub_lang_id) {
1903 dvdsub_lang_id = mp_dvdnav_sid_from_lang(ctx->stream, dvdsub_lang);
1904 if (dvdsub_lang_id != opts->sub_id) {
1905 opts->sub_id = dvdsub_lang_id;
1906 select_subtitle(ctx);
1910 /// clear all EOF related flags
1911 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1914 /// Restore last decoded DVDNAV (still frame)
1915 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1916 int *in_size,
1917 unsigned char **start,
1918 mp_image_t *decoded_frame)
1920 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1921 return decoded_frame;
1923 /// a change occured in dvdnav stream
1924 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1925 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1926 mp_dvdnav_context_free(mpctx);
1927 mp_dvdnav_reset_stream(mpctx);
1928 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1929 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1932 if (*in_size < 0) {
1933 float len;
1935 /// Display still frame, if any
1936 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1937 decoded_frame = mpctx->nav_smpi;
1939 /// increment video frame : continue playing after still frame
1940 len = demuxer_get_time_length(mpctx->demuxer);
1941 if (mpctx->sh_video->pts >= len &&
1942 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1943 mp_dvdnav_skip_still(mpctx->stream);
1944 mp_dvdnav_skip_wait(mpctx->stream);
1946 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1948 if (mpctx->nav_buffer) {
1949 *start = mpctx->nav_start;
1950 *in_size = mpctx->nav_in_size;
1951 if (mpctx->nav_start)
1952 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1956 return decoded_frame;
1959 /// Save last decoded DVDNAV (still frame)
1960 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1961 unsigned char *start,
1962 mp_image_t *decoded_frame)
1964 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1965 return;
1967 if (mpctx->nav_buffer)
1968 free(mpctx->nav_buffer);
1970 mpctx->nav_buffer = malloc(in_size);
1971 mpctx->nav_start = start;
1972 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1973 if (mpctx->nav_buffer)
1974 memcpy(mpctx->nav_buffer,start,in_size);
1976 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1977 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1979 #endif /* CONFIG_DVDNAV */
1981 /* Modify video timing to match the audio timeline. There are two main
1982 * reasons this is needed. First, video and audio can start from different
1983 * positions at beginning of file or after a seek (MPlayer starts both
1984 * immediately even if they have different pts). Second, the file can have
1985 * audio timestamps that are inconsistent with the duration of the audio
1986 * packets, for example two consecutive timestamp values differing by
1987 * one second but only a packet with enough samples for half a second
1988 * of playback between them.
1990 static void adjust_sync(struct MPContext *mpctx, double frame_time)
1992 current_module = "av_sync";
1994 if (!mpctx->sh_audio)
1995 return;
1997 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
1998 double v_pts = mpctx->sh_video->pts;
1999 double av_delay = a_pts - v_pts;
2000 // Try to sync vo_flip() so it will *finish* at given time
2001 av_delay += mpctx->last_vo_flip_duration;
2002 av_delay -= audio_delay; // This much pts difference is desired
2004 double change = av_delay * 0.1;
2005 double max_change = default_max_pts_correction >= 0 ?
2006 default_max_pts_correction : frame_time * 0.1;
2007 if (change < -max_change)
2008 change = -max_change;
2009 else if (change > max_change)
2010 change = max_change;
2011 mpctx->delay += change;
2012 mpctx->total_avsync_change += change;
2015 static int fill_audio_out_buffers(struct MPContext *mpctx)
2017 struct MPOpts *opts = &mpctx->opts;
2018 unsigned int t;
2019 double tt;
2020 int playsize;
2021 int playflags=0;
2022 int audio_eof=0;
2023 int bytes_to_write;
2024 sh_audio_t * const sh_audio = mpctx->sh_audio;
2026 current_module="play_audio";
2028 while (1) {
2029 int sleep_time;
2030 // all the current uses of ao_data.pts seem to be in aos that handle
2031 // sync completely wrong; there should be no need to use ao_data.pts
2032 // in get_space()
2033 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2034 bytes_to_write = mpctx->audio_out->get_space();
2035 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
2036 break;
2038 // handle audio-only case:
2039 // this is where mplayer sleeps during audio-only playback
2040 // to avoid 100% CPU use
2041 sleep_time = (ao_data.outburst - bytes_to_write) * 1000 / ao_data.bps;
2042 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
2043 usec_sleep(sleep_time * 1000);
2046 while (bytes_to_write) {
2047 playsize = bytes_to_write;
2048 if (playsize > MAX_OUTBURST)
2049 playsize = MAX_OUTBURST;
2050 bytes_to_write -= playsize;
2052 // Fill buffer if needed:
2053 current_module="decode_audio";
2054 t = GetTimer();
2055 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2056 if (mpctx->d_audio->eof) {
2057 audio_eof = 1;
2058 if (sh_audio->a_out_buffer_len == 0)
2059 return 0;
2061 t = GetTimer() - t;
2062 tt = t*0.000001f; audio_time_usage+=tt;
2063 if (playsize > sh_audio->a_out_buffer_len) {
2064 playsize = sh_audio->a_out_buffer_len;
2065 if (audio_eof)
2066 playflags |= AOPLAY_FINAL_CHUNK;
2068 if (!playsize)
2069 break;
2071 // play audio:
2072 current_module="play_audio";
2074 // Is this pts value actually useful for the aos that access it?
2075 // They're obviously badly broken in the way they handle av sync;
2076 // would not having access to this make them more broken?
2077 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2078 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2080 if (playsize > 0) {
2081 sh_audio->a_out_buffer_len -= playsize;
2082 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2083 sh_audio->a_out_buffer_len);
2084 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2086 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2087 // Sanity check to avoid hanging in case current ao doesn't output
2088 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2089 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2090 sh_audio->a_out_buffer_len = 0;
2093 return 1;
2096 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2097 float *aq_sleep_time)
2099 struct MPOpts *opts = &mpctx->opts;
2100 int frame_time_remaining = 0;
2101 current_module="calc_sleep_time";
2103 *time_frame -= get_relative_time(mpctx); // reset timer
2105 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2106 float delay = mpctx->audio_out->get_delay();
2107 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2109 if (autosync) {
2111 * Adjust this raw delay value by calculating the expected
2112 * delay for this frame and generating a new value which is
2113 * weighted between the two. The higher autosync is, the
2114 * closer to the delay value gets to that which "-nosound"
2115 * would have used, and the longer it will take for A/V
2116 * sync to settle at the right value (but it eventually will.)
2117 * This settling time is very short for values below 100.
2119 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2120 float difference = delay - predicted;
2121 delay = predicted + difference / (float)autosync;
2124 *time_frame = delay - mpctx->delay / opts->playback_speed;
2126 // delay = amount of audio buffered in soundcard/driver
2127 if (delay > 0.25) delay=0.25; else
2128 if (delay < 0.10) delay=0.10;
2129 if (*time_frame > delay*0.6) {
2130 // sleep time too big - may cause audio drops (buffer underrun)
2131 frame_time_remaining = 1;
2132 *time_frame = delay*0.5;
2134 } else {
2135 // If we're lagging more than 200 ms behind the right playback rate,
2136 // don't try to "catch up".
2137 // If benchmark is set always output frames as fast as possible
2138 // without sleeping.
2139 if (*time_frame < -0.2 || benchmark)
2140 *time_frame = 0;
2143 *aq_sleep_time += *time_frame;
2146 //============================== SLEEP: ===================================
2148 if (mpctx->video_out->driver->flip_page_timed)
2149 *time_frame -= 0.05;
2150 // flag 256 means: libvo driver does its timing (dvb card)
2151 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2152 *time_frame = timing_sleep(mpctx, *time_frame);
2153 if (mpctx->video_out->driver->flip_page_timed)
2154 *time_frame += 0.05;
2155 return frame_time_remaining;
2158 int reinit_video_chain(struct MPContext *mpctx)
2160 struct MPOpts *opts = &mpctx->opts;
2161 sh_video_t * const sh_video = mpctx->sh_video;
2162 double ar=-1.0;
2163 //================== Init VIDEO (codec & libvo) ==========================
2164 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2165 current_module="preinit_libvo";
2167 //shouldn't we set dvideo->id=-2 when we fail?
2168 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2169 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2170 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Error opening/initializing the selected video_out (-vo) device.\n");
2171 goto err_out;
2173 mpctx->initialized_flags|=INITIALIZED_VO;
2176 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2177 mpctx->sh_video->stream_aspect = ar;
2178 current_module="init_video_filters";
2180 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2181 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2183 #ifdef CONFIG_MENU
2184 if(use_menu) {
2185 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2186 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2187 if(!vf_menu) {
2188 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Can't open libmenu video filter with root menu %s.\n",menu_root);
2189 use_menu = 0;
2192 if(vf_menu)
2193 sh_video->vfilter = vf_menu;
2194 #endif
2196 #ifdef CONFIG_ASS
2197 if(opts->ass_enabled) {
2198 int i;
2199 int insert = 1;
2200 if (opts->vf_settings)
2201 for (i = 0; opts->vf_settings[i].name; ++i)
2202 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2203 insert = 0;
2204 break;
2206 if (insert) {
2207 extern vf_info_t vf_info_ass;
2208 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2209 char* vf_arg[] = {"auto", "1", NULL};
2210 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2211 if (vf_ass)
2212 sh_video->vfilter = vf_ass;
2213 else
2214 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2217 #endif
2219 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2221 #ifdef CONFIG_ASS
2222 if (opts->ass_enabled)
2223 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2224 #endif
2226 current_module="init_video_codec";
2228 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2229 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2230 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2232 if(!sh_video->initialized){
2233 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2234 goto err_out;
2237 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2239 if (sh_video->codec)
2240 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2242 sh_video->last_pts = MP_NOPTS_VALUE;
2243 sh_video->num_buffered_pts = 0;
2244 sh_video->next_frame_time = 0;
2246 if(auto_quality>0){
2247 // Auto quality option enabled
2248 output_quality=get_video_quality_max(sh_video);
2249 if(auto_quality>output_quality) auto_quality=output_quality;
2250 else output_quality=auto_quality;
2251 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2252 set_video_quality(sh_video,output_quality);
2255 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2257 current_module="init_vo";
2259 return 1;
2261 err_out:
2262 mpctx->sh_video = mpctx->d_video->sh = NULL;
2263 return 0;
2266 static double update_video_nocorrect_pts(struct MPContext *mpctx)
2268 struct sh_video *sh_video = mpctx->sh_video;
2269 double frame_time = 0;
2270 struct vo *video_out = mpctx->video_out;
2271 while (!video_out->frame_loaded) {
2272 current_module = "filter_video";
2273 // In nocorrect-pts mode there is no way to properly time these frames
2274 if (vo_get_buffered_frame(video_out, 0) >= 0)
2275 break;
2276 if (vf_output_queued_frame(sh_video->vfilter))
2277 continue;
2278 unsigned char *packet = NULL;
2279 frame_time = sh_video->next_frame_time;
2280 if (mpctx->update_video_immediately)
2281 frame_time = 0;
2282 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2283 &packet, force_fps);
2284 if (in_size < 0) {
2285 #ifdef CONFIG_DVDNAV
2286 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2287 if (mp_dvdnav_is_eof(mpctx->stream))
2288 return -1;
2289 if (mpctx->d_video)
2290 mpctx->d_video->eof = 0;
2291 if (mpctx->d_audio)
2292 mpctx->d_audio->eof = 0;
2293 mpctx->stream->eof = 0;
2294 } else
2295 #endif
2296 return -1;
2298 if (in_size > max_framesize)
2299 max_framesize = in_size;
2300 sh_video->timer += frame_time;
2301 if (mpctx->sh_audio)
2302 mpctx->delay -= frame_time;
2303 // video_read_frame can change fps (e.g. for ASF video)
2304 vo_fps = sh_video->fps;
2305 int framedrop_type = check_framedrop(mpctx, frame_time);
2306 current_module = "decode video";
2308 void *decoded_frame;
2309 #ifdef CONFIG_DVDNAV
2310 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2311 if (in_size >= 0 && !decoded_frame)
2312 #endif
2313 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2314 sh_video->pts);
2315 #ifdef CONFIG_DVDNAV
2316 // Save last still frame for future display
2317 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2318 #endif
2319 if (decoded_frame) {
2320 current_module = "filter video";
2321 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2322 if (!video_out->config_ok)
2323 break;
2326 return frame_time;
2329 static void determine_frame_pts(struct MPContext *mpctx)
2331 struct sh_video *sh_video = mpctx->sh_video;
2332 struct MPOpts *opts = &mpctx->opts;
2334 if (opts->user_pts_assoc_mode) {
2335 sh_video->pts_assoc_mode = opts->user_pts_assoc_mode;
2336 } else if (sh_video->pts_assoc_mode == 0) {
2337 if (sh_video->codec_reordered_pts != MP_NOPTS_VALUE)
2338 sh_video->pts_assoc_mode = 1;
2339 else
2340 sh_video->pts_assoc_mode = 2;
2341 } else {
2342 int probcount1 = sh_video->num_reordered_pts_problems;
2343 int probcount2 = sh_video->num_sorted_pts_problems;
2344 if (sh_video->pts_assoc_mode == 2) {
2345 int tmp = probcount1;
2346 probcount1 = probcount2;
2347 probcount2 = tmp;
2349 if (probcount1 >= probcount2 * 1.5 + 2) {
2350 sh_video->pts_assoc_mode = 3 - sh_video->pts_assoc_mode;
2351 mp_msg(MSGT_CPLAYER, MSGL_V, "Switching to pts association mode "
2352 "%d.\n", sh_video->pts_assoc_mode);
2355 sh_video->pts = sh_video->pts_assoc_mode == 1 ?
2356 sh_video->codec_reordered_pts : sh_video->sorted_pts;
2359 static double update_video(struct MPContext *mpctx)
2361 struct sh_video *sh_video = mpctx->sh_video;
2362 struct vo *video_out = mpctx->video_out;
2363 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2364 mpctx->osd); // hack for vf_expand
2365 if (!mpctx->opts.correct_pts)
2366 return update_video_nocorrect_pts(mpctx);
2368 double pts;
2370 bool hit_eof = false;
2371 while (!video_out->frame_loaded) {
2372 current_module = "filter_video";
2373 if (vo_get_buffered_frame(video_out, hit_eof) >= 0)
2374 break;
2375 // XXX Time used in this call is not counted in any performance
2376 // timer now, OSD time is not updated correctly for filter-added frames
2377 if (vf_output_queued_frame(sh_video->vfilter))
2378 continue;
2379 if (hit_eof)
2380 return -1;
2381 unsigned char *packet = NULL;
2382 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2383 if (pts != MP_NOPTS_VALUE)
2384 pts += mpctx->video_offset;
2385 if (in_size < 0) {
2386 // try to extract last frames in case of decoder lag
2387 in_size = 0;
2388 pts = 1e300;
2389 hit_eof = true;
2391 if (in_size > max_framesize)
2392 max_framesize = in_size;
2393 current_module = "decode video";
2394 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2395 void *decoded_frame = decode_video(sh_video, packet, in_size,
2396 framedrop_type, pts);
2397 if (decoded_frame) {
2398 determine_frame_pts(mpctx);
2399 current_module = "filter video";
2400 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2401 if (!video_out->config_ok)
2402 break; // We'd likely hang in this loop otherwise
2406 pts = video_out->next_pts;
2407 if (pts == MP_NOPTS_VALUE) {
2408 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2409 // Try to use decoder pts from before filters
2410 pts = sh_video->pts;
2411 if (pts == MP_NOPTS_VALUE)
2412 pts = sh_video->last_pts;
2414 sh_video->pts = pts;
2415 if (sh_video->last_pts == MP_NOPTS_VALUE)
2416 sh_video->last_pts = sh_video->pts;
2417 else if (sh_video->last_pts > sh_video->pts) {
2418 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2419 sh_video->pts, sh_video->last_pts);
2420 /* If the difference in pts is small treat it as jitter around the
2421 * right value (possibly caused by incorrect timestamp ordering) and
2422 * just show this frame immediately after the last one.
2423 * Treat bigger differences as timestamp resets and start counting
2424 * timing of later frames from the position of this one. */
2425 if (sh_video->last_pts - sh_video->pts > 0.5)
2426 sh_video->last_pts = sh_video->pts;
2427 else
2428 sh_video->pts = sh_video->last_pts;
2430 double frame_time = sh_video->pts - sh_video->last_pts;
2431 sh_video->last_pts = sh_video->pts;
2432 sh_video->timer += frame_time;
2433 if (mpctx->sh_audio)
2434 mpctx->delay -= frame_time;
2435 return frame_time;
2438 void pause_player(struct MPContext *mpctx)
2440 if (mpctx->paused)
2441 return;
2442 mpctx->paused = 1;
2443 mpctx->step_frames = 0;
2444 mpctx->time_frame -= get_relative_time(mpctx);
2446 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2447 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2449 if (mpctx->audio_out && mpctx->sh_audio)
2450 mpctx->audio_out->pause(); // pause audio, keep data if possible
2453 void unpause_player(struct MPContext *mpctx)
2455 if (!mpctx->paused)
2456 return;
2457 mpctx->paused = 0;
2459 if (mpctx->audio_out && mpctx->sh_audio)
2460 mpctx->audio_out->resume(); // resume audio
2461 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2462 && !mpctx->step_frames)
2463 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2464 (void)get_relative_time(mpctx); // ignore time that passed during pause
2467 void add_step_frame(struct MPContext *mpctx)
2469 mpctx->step_frames++;
2470 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2471 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2472 unpause_player(mpctx);
2475 static void pause_loop(struct MPContext *mpctx)
2477 mp_cmd_t* cmd;
2478 if (!quiet) {
2479 // Small hack to display the pause message on the OSD line.
2480 // The pause string is: "\n == PAUSE == \r" so we need to
2481 // take the first and the last char out
2482 if (term_osd && !mpctx->sh_video) {
2483 char msg[128] = _("\n ===== PAUSE =====\r");
2484 int mlen = strlen(msg);
2485 msg[mlen-1] = '\0';
2486 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2487 update_osd_msg(mpctx);
2488 } else
2489 mp_tmsg(MSGT_CPLAYER,MSGL_STATUS,"\n ===== PAUSE =====\r");
2490 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2493 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2494 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2495 if (cmd) {
2496 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2497 run_command(mpctx, cmd);
2498 mp_cmd_free(cmd);
2499 continue;
2501 if (mpctx->sh_video && mpctx->video_out)
2502 vo_check_events(mpctx->video_out);
2503 #ifdef CONFIG_MENU
2504 if (vf_menu)
2505 vf_menu_pause_update(vf_menu);
2506 #endif
2507 usec_sleep(20000);
2508 update_osd_msg(mpctx);
2509 int hack = vo_osd_changed(0);
2510 vo_osd_changed(hack);
2511 if (hack)
2512 break;
2517 // Find the right mute status and record position for new file position
2518 static void edl_seek_reset(MPContext *mpctx)
2520 mpctx->edl_muted = 0;
2521 next_edl_record = edl_records;
2523 while (next_edl_record) {
2524 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2525 break;
2527 if (next_edl_record->action == EDL_MUTE)
2528 mpctx->edl_muted = !mpctx->edl_muted;
2529 next_edl_record = next_edl_record->next;
2531 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2532 mixer_mute(&mpctx->mixer);
2536 // Execute EDL command for the current position if one exists
2537 static void edl_update(MPContext *mpctx)
2539 if (!next_edl_record)
2540 return;
2542 if (!mpctx->sh_video) {
2543 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot use EDL without video, disabling.\n");
2544 free_edl(edl_records);
2545 next_edl_record = NULL;
2546 edl_records = NULL;
2547 return;
2550 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2551 if (next_edl_record->action == EDL_SKIP) {
2552 mpctx->osd_function = OSD_FFW;
2553 mpctx->abs_seek_pos = 0;
2554 mpctx->rel_seek_secs = next_edl_record->length_sec;
2555 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2556 "[%f], length [%f]\n", next_edl_record->start_sec,
2557 next_edl_record->stop_sec, next_edl_record->length_sec);
2559 else if (next_edl_record->action == EDL_MUTE) {
2560 mpctx->edl_muted = !mpctx->edl_muted;
2561 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2562 mixer_mute(&mpctx->mixer);
2563 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2564 next_edl_record->start_sec );
2566 next_edl_record = next_edl_record->next;
2570 static void reinit_decoders(struct MPContext *mpctx)
2572 reinit_video_chain(mpctx);
2573 reinit_audio_chain(mpctx);
2574 mp_property_do("sub", M_PROPERTY_SET, &mpctx->global_sub_pos, mpctx);
2577 static bool timeline_set_part(struct MPContext *mpctx, int i)
2579 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
2580 struct timeline_part *n = mpctx->timeline + i;
2581 mpctx->timeline_part = i;
2582 mpctx->video_offset = n->start - n->source_start;
2583 if (n->source == p->source)
2584 return false;
2585 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo && mpctx->opts.video_id != -2 ? 0 : INITIALIZED_VO) | INITIALIZED_AO | INITIALIZED_ACODEC);
2586 mpctx->demuxer = n->source->demuxer;
2587 mpctx->d_video = mpctx->demuxer->video;
2588 mpctx->d_audio = mpctx->demuxer->audio;
2589 mpctx->d_sub = mpctx->demuxer->sub;
2590 mpctx->sh_video = mpctx->d_video->sh;
2591 mpctx->sh_audio = mpctx->d_audio->sh;
2592 return true;
2595 // Given pts, switch playback to the corresponding part.
2596 // Return offset within that part.
2597 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
2598 bool *need_reset)
2600 if (pts < 0)
2601 pts = 0;
2602 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
2603 struct timeline_part *p = mpctx->timeline + i;
2604 if (pts < (p+1)->start) {
2605 *need_reset = timeline_set_part(mpctx, i);
2606 return pts - p->start + p->source_start;
2609 return -1;
2613 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2614 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2615 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2616 static int seek(MPContext *mpctx, double amount, int style)
2618 current_module = "seek";
2619 if (mpctx->stop_play == AT_END_OF_FILE)
2620 mpctx->stop_play = KEEP_PLAYING;
2621 if (mpctx->timeline && style & SEEK_FACTOR) {
2622 amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
2623 style &= ~SEEK_FACTOR;
2625 if ((mpctx->demuxer->accurate_seek || mpctx->timeline) && mpctx->sh_video
2626 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2627 style |= SEEK_ABSOLUTE;
2628 if (amount > 0)
2629 style |= SEEK_FORWARD;
2630 else
2631 style |= SEEK_BACKWARD;
2632 amount += mpctx->sh_video->pts;
2635 /* At least the liba52 decoder wants to read from the input stream
2636 * during initialization, so reinit must be done after the demux_seek()
2637 * call that clears possible stream EOF. */
2638 bool need_reset = false;
2639 if (mpctx->timeline) {
2640 amount = timeline_set_from_time(mpctx, amount, &need_reset);
2641 if (amount == -1) {
2642 mpctx->stop_play = AT_END_OF_FILE;
2643 // Clear audio from current position
2644 if (mpctx->sh_audio) {
2645 mpctx->audio_out->reset();
2646 mpctx->sh_audio->a_buffer_len = 0;
2647 mpctx->sh_audio->a_out_buffer_len = 0;
2649 return -1;
2652 int seekresult = demux_seek(mpctx->demuxer, amount, audio_delay, style);
2653 if (need_reset)
2654 reinit_decoders(mpctx);
2655 if (seekresult == 0)
2656 return -1;
2658 if (mpctx->sh_video) {
2659 current_module = "seek_video_reset";
2660 resync_video_stream(mpctx->sh_video);
2661 vo_seek_reset(mpctx->video_out);
2662 mpctx->sh_video->num_buffered_pts = 0;
2663 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2664 mpctx->delay = 0;
2665 mpctx->time_frame = 0;
2666 mpctx->update_video_immediately = true;
2667 // Not all demuxers set d_video->pts during seek, so this value
2668 // (which is used by at least vobsub and edl code below) may
2669 // be completely wrong (probably 0).
2670 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
2671 update_subtitles(mpctx, &mpctx->opts, mpctx->sh_video,
2672 mpctx->sh_video->pts, mpctx->video_offset,
2673 mpctx->d_sub, 1);
2674 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2677 if (mpctx->sh_audio) {
2678 current_module = "seek_audio_reset";
2679 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2680 mpctx->sh_audio->a_buffer_len = 0;
2681 mpctx->sh_audio->a_out_buffer_len = 0;
2682 if (!mpctx->sh_video)
2683 update_subtitles(mpctx, &mpctx->opts, NULL, mpctx->sh_audio->pts,
2684 mpctx->video_offset, mpctx->d_sub, 1);
2687 if (vo_vobsub && mpctx->sh_video) {
2688 current_module = "seek_vobsub_reset";
2689 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2692 edl_seek_reset(mpctx);
2694 mpctx->total_avsync_change = 0;
2695 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2696 drop_frame_cnt = 0;
2698 current_module = NULL;
2699 return 0;
2702 int get_current_chapter(struct MPContext *mpctx)
2704 if (!mpctx->chapters || !mpctx->sh_video)
2705 return demuxer_get_current_chapter(mpctx->demuxer);
2707 int i;
2708 double current_pts = mpctx->sh_video->pts;
2709 for (i = 1; i < mpctx->num_chapters; i++)
2710 if (current_pts < mpctx->chapters[i].start)
2711 break;
2712 return i - 1;
2715 // currently returns a string allocated with malloc, not talloc
2716 char *chapter_display_name(struct MPContext *mpctx, int chapter)
2718 if (!mpctx->chapters || !mpctx->sh_video)
2719 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
2720 return strdup(mpctx->chapters[chapter].name);
2723 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
2724 char **chapter_name)
2726 if (!mpctx->chapters || !mpctx->sh_video)
2727 return demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
2728 chapter_name);
2729 if (chapter >= mpctx->num_chapters)
2730 return -1;
2731 if (chapter < 0)
2732 chapter = 0;
2733 *seek_pts = mpctx->chapters[chapter].start;
2734 if (chapter_name)
2735 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
2736 return chapter;
2739 static int find_ordered_chapter_sources(struct MPContext *mpctx,
2740 struct content_source *sources,
2741 int num_sources,
2742 unsigned char uid_map[][16])
2744 int num_filenames = 0;
2745 char **filenames = NULL;
2746 if (num_sources > 1) {
2747 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
2748 "other sources.\n");
2749 if (mpctx->stream->type != STREAMTYPE_FILE) {
2750 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
2751 "normal disk file. Will not search for related files.\n");
2752 } else {
2753 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
2754 "same directory to find referenced sources.\n");
2755 filenames = find_files(mpctx->demuxer->filename, ".mkv",
2756 &num_filenames);
2760 int num_left = num_sources - 1;
2761 for (int i = 0; i < num_filenames && num_left > 0; i++) {
2762 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
2763 filename_recode(filenames[i]));
2764 int format;
2765 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
2766 if (!s)
2767 continue;
2768 struct demuxer *d = demux_open(&mpctx->opts, s, DEMUXER_TYPE_MATROSKA,
2769 mpctx->opts.audio_id,
2770 mpctx->opts.video_id,
2771 mpctx->opts.sub_id, filenames[i]);
2772 if (!d) {
2773 free_stream(s);
2774 continue;
2776 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
2777 for (int i = 1; i < num_sources; i++) {
2778 if (sources[i].demuxer)
2779 continue;
2780 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
2781 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
2782 i, filename_recode(d->filename));
2783 sources[i].stream = s;
2784 sources[i].demuxer = d;
2785 num_left--;
2786 goto match;
2790 free_demuxer(d);
2791 free_stream(s);
2792 continue;
2793 match:
2796 talloc_free(filenames);
2797 if (num_left) {
2798 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
2799 "There will be parts MISSING from the video!\n");
2800 for (int i = 1, j = 1; i < num_sources; i++)
2801 if (sources[i].demuxer) {
2802 sources[j] = sources[i];
2803 memcpy(uid_map[j], uid_map[i], 16);
2804 j++;
2807 return num_sources - num_left;
2810 static void build_ordered_chapter_timeline(struct MPContext *mpctx)
2812 if (!mpctx->opts.ordered_chapters) {
2813 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
2814 "you have disabled support for them. Ignoring.\n");
2815 return;
2818 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
2819 "edit timeline.\n");
2821 struct demuxer *demuxer = mpctx->demuxer;
2822 struct matroska_data *m = &demuxer->matroska_data;
2824 // +1 because sources/uid_map[0] is original file even if all chapters
2825 // actually use other sources and need separate entries
2826 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
2827 m->num_ordered_chapters+1);
2828 sources[0].stream = mpctx->stream;
2829 sources[0].demuxer = mpctx->demuxer;
2830 unsigned char uid_map[m->num_ordered_chapters+1][16];
2831 int num_sources = 1;
2832 memcpy(uid_map[0], m->segment_uid, 16);
2834 for (int i = 0; i < m->num_ordered_chapters; i++) {
2835 struct matroska_chapter *c = m->ordered_chapters + i;
2836 if (!c->has_segment_uid)
2837 memcpy(c->segment_uid, m->segment_uid, 16);
2839 for (int j = 0; j < num_sources; j++)
2840 if (!memcmp(c->segment_uid, uid_map[j], 16))
2841 goto found1;
2842 memcpy(uid_map[num_sources], c->segment_uid, 16);
2843 sources[num_sources] = (struct content_source){};
2844 num_sources++;
2845 found1:
2849 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
2850 uid_map);
2853 // +1 for terminating chapter with start time marking end of last real one
2854 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
2855 m->num_ordered_chapters + 1);
2856 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
2857 m->num_ordered_chapters);
2858 uint64_t starttime = 0;
2859 uint64_t missing_time = 0;
2860 int part_count = 0;
2861 int num_chapters = 0;
2862 uint64_t prev_part_offset;
2863 for (int i = 0; i < m->num_ordered_chapters; i++) {
2864 struct matroska_chapter *c = m->ordered_chapters + i;
2866 int j;
2867 for (j = 0; j < num_sources; j++) {
2868 if (!memcmp(c->segment_uid, uid_map[j], 16))
2869 goto found2;
2871 missing_time += c->end - c->start;
2872 continue;
2873 found2:;
2874 chapters[num_chapters].start = starttime / 1000.;
2875 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
2876 /* Only add a separate part if the time or file actually changes.
2877 * Matroska files have chapter divisions that are redundant from
2878 * timeline point of view because the same chapter structure is used
2879 * both to specify the timeline and for normal chapter information.
2880 * Removing a missing inserted external chapter can also cause this. */
2881 if (part_count == 0 || c->start != starttime + prev_part_offset
2882 || sources + j != timeline[part_count - 1].source) {
2883 timeline[part_count].source = sources + j;
2884 timeline[part_count].start = chapters[num_chapters].start;
2885 timeline[part_count].source_start = c->start / 1000.;
2886 prev_part_offset = c->start - starttime;
2887 part_count++;
2889 starttime += c->end - c->start;
2890 num_chapters++;
2892 timeline[part_count].start = starttime / 1000.;
2894 if (!part_count) {
2895 // None of the parts come from the file itself???
2896 talloc_free(sources);
2897 talloc_free(timeline);
2898 talloc_free(chapters);
2899 return;
2902 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
2903 "sources. Total length %.3f seconds.\n", part_count, num_sources,
2904 timeline[part_count].start);
2905 if (missing_time)
2906 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
2907 "from the timeline!\n", missing_time / 1000.);
2908 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
2909 for (int i = 0; i < num_sources; i++)
2910 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
2911 filename_recode(sources[i].demuxer->filename));
2912 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
2913 "source_start, source):\n");
2914 for (int i = 0; i < part_count; i++) {
2915 struct timeline_part *p = timeline + i;
2916 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
2917 p->source_start, p->source - sources);
2919 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start);
2920 mpctx->sources = sources;
2921 mpctx->num_sources = num_sources;
2922 mpctx->timeline = timeline;
2923 mpctx->num_timeline_parts = part_count;
2924 mpctx->num_chapters = num_chapters;
2925 mpctx->chapters = chapters;
2927 mpctx->timeline_part = 0;
2928 mpctx->demuxer = timeline[0].source->demuxer;
2932 static int read_keys(void *ctx, int fd)
2934 getch2(ctx);
2935 return mplayer_get_key(ctx, 0);
2939 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2940 * file for some tools to link against. */
2941 #ifndef DISABLE_MAIN
2942 int main(int argc,char* argv[]){
2945 char * mem_ptr;
2947 // movie info:
2949 /* Flag indicating whether MPlayer should exit without playing anything. */
2950 int opt_exit = 0;
2952 //float a_frame=0; // Audio
2954 int i;
2956 struct MPContext *mpctx = &(struct MPContext){
2957 .osd_function = OSD_PLAY,
2958 .begin_skip = MP_NOPTS_VALUE,
2959 .play_tree_step = 1,
2960 .global_sub_pos = -1,
2961 .set_of_sub_pos = -1,
2962 .file_format = DEMUXER_TYPE_UNKNOWN,
2963 .last_dvb_step = 1,
2966 InitTimer();
2967 srand(GetTimerMS());
2969 mp_msg_init();
2970 set_av_log_callback();
2972 #ifdef CONFIG_X11
2973 mpctx->x11_state = vo_x11_init_state();
2974 #endif
2975 struct MPOpts *opts = &mpctx->opts;
2976 set_default_mplayer_options(opts);
2977 // Create the config context and register the options
2978 mpctx->mconfig = m_config_new(opts, cfg_include);
2979 m_config_register_options(mpctx->mconfig,mplayer_opts);
2980 mp_input_register_options(mpctx->mconfig);
2982 // Preparse the command line
2983 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2985 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2986 set_path_env();
2987 #endif
2989 #ifdef CONFIG_TV
2990 stream_tv_defaults.immediate = 1;
2991 #endif
2993 parse_cfgfiles(mpctx, mpctx->mconfig);
2995 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2996 if(mpctx->playtree == NULL)
2997 opt_exit = 1;
2998 else {
2999 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
3000 if(mpctx->playtree) {
3001 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3002 if(mpctx->playtree_iter) {
3003 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3004 play_tree_iter_free(mpctx->playtree_iter);
3005 mpctx->playtree_iter = NULL;
3007 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3011 mpctx->key_fifo = mp_fifo_create(opts);
3013 print_version("MPlayer");
3015 #if defined(__MINGW32__) || defined(__CYGWIN__)
3017 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
3018 BOOL WINAPI (*setDEP)(DWORD) = NULL;
3019 if (kernel32)
3020 setDEP = GetProcAddress(kernel32, "SetProcessDEPPolicy");
3021 if (setDEP) setDEP(3);
3023 // stop Windows from showing all kinds of annoying error dialogs
3024 SetErrorMode(0x8003);
3025 // request 1ms timer resolution
3026 timeBeginPeriod(1);
3027 #endif
3029 #ifdef CONFIG_PRIORITY
3030 set_priority();
3031 #endif
3033 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
3034 list_video_out();
3035 opt_exit = 1;
3038 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
3039 list_audio_out();
3040 opt_exit = 1;
3043 /* Check codecs.conf. */
3044 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3045 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3046 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3047 if(!parse_codec_cfg(NULL)){
3048 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3050 mp_tmsg(MSGT_CPLAYER,MSGL_V,"Using built-in default codecs.conf.\n");
3053 free( mem_ptr ); // release the buffer created by get_path()
3056 #if 0
3057 if(video_codec_list){
3058 int i;
3059 video_codec=video_codec_list[0];
3060 for(i=0;video_codec_list[i];i++)
3061 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
3063 #endif
3064 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3065 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available audio codecs:\n");
3066 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3067 list_codecs(1);
3068 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3069 opt_exit = 1;
3071 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3072 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video codecs:\n");
3073 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3074 list_codecs(0);
3075 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3076 opt_exit = 1;
3078 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3079 vfm_help();
3080 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3081 opt_exit = 1;
3083 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3084 afm_help();
3085 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3086 opt_exit = 1;
3088 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3089 af_help();
3090 printf("\n");
3091 opt_exit = 1;
3093 #ifdef CONFIG_X11
3094 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3095 fstype_help();
3096 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3097 opt_exit = 1;
3099 #endif
3100 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3101 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3102 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3103 demuxer_help();
3104 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3105 opt_exit = 1;
3107 if(list_properties) {
3108 property_print_help();
3109 opt_exit = 1;
3112 if(opt_exit)
3113 exit_player(mpctx, EXIT_NONE);
3115 if(!mpctx->filename && !player_idle_mode){
3116 // no file/vcd/dvd -> show HELP:
3117 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
3118 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3121 /* Display what configure line was used */
3122 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3124 // Many users forget to include command line in bugreports...
3125 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3126 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "CommandLine:");
3127 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3128 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3131 //------ load global data first ------
3133 mpctx->osd = osd_create();
3135 // check font
3136 #ifdef CONFIG_FREETYPE
3137 init_freetype();
3138 #endif
3139 #ifdef CONFIG_FONTCONFIG
3140 if(font_fontconfig <= 0)
3142 #endif
3143 #ifdef CONFIG_BITMAP_FONT
3144 if(font_name){
3145 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3146 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",
3147 filename_recode(font_name));
3148 } else {
3149 // try default:
3150 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3151 free(mem_ptr); // release the buffer created by get_path()
3152 if(!vo_font)
3153 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3155 if (sub_font_name)
3156 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3157 else
3158 mpctx->osd->sub_font = vo_font;
3159 #endif
3160 #ifdef CONFIG_FONTCONFIG
3162 #endif
3164 #ifdef CONFIG_ASS
3165 ass_library = ass_init();
3166 #endif
3168 #ifdef HAVE_RTC
3169 if(!nortc)
3171 // seteuid(0); /* Can't hurt to try to get root here */
3172 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3173 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Failed to open %s: %s (it should be readable by the user.)\n",
3174 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3175 else {
3176 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3178 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3179 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp, strerror(errno));
3180 mp_tmsg(MSGT_CPLAYER, MSGL_HINT, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp);
3181 close (rtc_fd);
3182 rtc_fd = -1;
3183 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3184 /* variable only by the root */
3185 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno));
3186 close (rtc_fd);
3187 rtc_fd = -1;
3188 } else
3189 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Using Linux hardware RTC timing (%ldHz).\n", irqp);
3192 if(rtc_fd<0)
3193 #endif /* HAVE_RTC */
3194 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3195 softsleep?"software":timer_name);
3197 #ifdef HAVE_TERMCAP
3198 load_termcap(NULL); // load key-codes
3199 #endif
3201 // ========== Init keyboard FIFO (connection to libvo) ============
3203 // Init input system
3204 current_module = "init_input";
3205 mpctx->input = mp_input_init(&opts->input);
3206 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3207 if(slave_mode)
3208 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
3209 else if(!noconsolecontrols)
3210 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
3211 // Set the libstream interrupt callback
3212 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
3214 #ifdef CONFIG_MENU
3215 if(use_menu) {
3216 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3217 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3218 else {
3219 menu_cfg = get_path("menu.conf");
3220 if(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 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
3224 MPLAYER_CONFDIR "/menu.conf"))
3225 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", MPLAYER_CONFDIR"/menu.conf");
3226 else {
3227 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Menu init failed.\n");
3228 use_menu = 0;
3233 #endif
3235 current_module = NULL;
3237 /// Catch signals
3238 #ifndef __MINGW32__
3239 signal(SIGCHLD,child_sighandler);
3240 #endif
3242 #ifdef CONFIG_CRASH_DEBUG
3243 prog_path = argv[0];
3244 #endif
3245 //========= Catch terminate signals: ================
3246 // terminate requests:
3247 signal(SIGTERM,exit_sighandler); // kill
3248 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3250 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3252 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3253 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3254 #ifdef CONFIG_SIGHANDLER
3255 // fatal errors:
3256 signal(SIGBUS,exit_sighandler); // bus error
3257 signal(SIGSEGV,exit_sighandler); // segfault
3258 signal(SIGILL,exit_sighandler); // illegal instruction
3259 signal(SIGFPE,exit_sighandler); // floating point exc.
3260 signal(SIGABRT,exit_sighandler); // abort()
3261 #ifdef CONFIG_CRASH_DEBUG
3262 if (crash_debug)
3263 signal(SIGTRAP,exit_sighandler);
3264 #endif
3265 #endif
3267 // ******************* Now, let's see the per-file stuff ********************
3269 play_next_file:
3271 // init global sub numbers
3272 mpctx->global_sub_size = 0;
3273 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3275 if (mpctx->filename) {
3276 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
3277 load_per_extension_config (mpctx->mconfig, mpctx->filename);
3278 load_per_file_config (mpctx->mconfig, mpctx->filename);
3281 if (opts->video_driver_list)
3282 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
3283 if (opts->audio_driver_list)
3284 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
3286 // We must enable getch2 here to be able to interrupt network connection
3287 // or cache filling
3288 if(!noconsolecontrols && !slave_mode){
3289 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
3290 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"WARNING: getch2_init called twice!\n");
3291 else
3292 getch2_enable(); // prepare stdin for hotkeys...
3293 mpctx->initialized_flags|=INITIALIZED_GETCH2;
3294 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3297 // =================== GUI idle loop (STOP state) ===========================
3298 while (player_idle_mode && !mpctx->filename) {
3299 play_tree_t * entry = NULL;
3300 mp_cmd_t * cmd;
3301 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3302 if (mpctx->video_out)
3303 vo_check_events(mpctx->video_out);
3304 usec_sleep(20000);
3306 switch (cmd->id) {
3307 case MP_CMD_LOADFILE:
3308 // prepare a tree entry with the new filename
3309 entry = play_tree_new();
3310 play_tree_add_file(entry, cmd->args[0].v.s);
3311 // The entry is added to the main playtree after the switch().
3312 break;
3313 case MP_CMD_LOADLIST:
3314 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3315 break;
3316 case MP_CMD_QUIT:
3317 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3318 break;
3319 case MP_CMD_GET_PROPERTY:
3320 case MP_CMD_SET_PROPERTY:
3321 case MP_CMD_STEP_PROPERTY:
3322 run_command(mpctx, cmd);
3323 break;
3326 mp_cmd_free(cmd);
3328 if (entry) { // user entered a command that gave a valid entry
3329 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3330 play_tree_free_list(mpctx->playtree->child, 1);
3331 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3333 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3335 play_tree_set_child(mpctx->playtree, entry);
3337 /* Make iterator start at the top the of tree. */
3338 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3339 if (!mpctx->playtree_iter) continue;
3341 // find the first real item in the tree
3342 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3343 // no items!
3344 play_tree_iter_free(mpctx->playtree_iter);
3345 mpctx->playtree_iter = NULL;
3346 continue; // wait for next command
3348 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3351 //---------------------------------------------------------------------------
3353 if (mpctx->filename) {
3354 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nPlaying %s.\n",
3355 filename_recode(mpctx->filename));
3356 if(use_filename_title && vo_wintitle == NULL)
3357 vo_wintitle = strdup(mp_basename2(mpctx->filename));
3360 if (edl_filename) {
3361 if (edl_records) free_edl(edl_records);
3362 next_edl_record = edl_records = edl_parse_file();
3364 if (edl_output_filename) {
3365 if (edl_fd) fclose(edl_fd);
3366 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3368 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Can't open EDL file [%s] for writing.\n",
3369 filename_recode(edl_output_filename));
3373 //==================== Open VOB-Sub ============================
3375 current_module="vobsub";
3376 if (vobsub_name){
3377 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3378 if(vo_vobsub==NULL)
3379 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
3380 filename_recode(vobsub_name));
3381 } else if (sub_auto && mpctx->filename){
3382 /* try to autodetect vobsub from movie filename ::atmos */
3383 char *buf = strdup(mpctx->filename), *psub;
3384 char *pdot = strrchr(buf, '.');
3385 char *pslash = strrchr(buf, '/');
3386 #if defined(__MINGW32__) || defined(__CYGWIN__)
3387 if (!pslash) pslash = strrchr(buf, '\\');
3388 #endif
3389 if (pdot && (!pslash || pdot > pslash))
3390 *pdot = '\0';
3391 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3392 /* try from ~/.mplayer/sub */
3393 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3394 char *bname;
3395 int l;
3396 bname = strrchr(buf,'/');
3397 #if defined(__MINGW32__) || defined(__CYGWIN__)
3398 if(!bname) bname = strrchr(buf,'\\');
3399 #endif
3400 if(bname) bname++;
3401 else bname = buf;
3402 l = strlen(psub) + strlen(bname) + 1;
3403 psub = realloc(psub,l);
3404 strcat(psub,bname);
3405 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3406 free(psub);
3408 free(buf);
3410 if(vo_vobsub){
3411 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3412 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3413 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3415 // setup global sub numbering
3416 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3417 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3420 //============ Open & Sync STREAM --- fork cache2 ====================
3422 mpctx->stream=NULL;
3423 mpctx->demuxer=NULL;
3424 if (mpctx->d_audio) {
3425 //free_demuxer_stream(mpctx->d_audio);
3426 mpctx->d_audio=NULL;
3428 if (mpctx->d_video) {
3429 //free_demuxer_stream(d_video);
3430 mpctx->d_video=NULL;
3432 mpctx->sh_audio=NULL;
3433 mpctx->sh_video=NULL;
3435 current_module="open_stream";
3436 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3437 if(!mpctx->stream) { // error...
3438 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3439 goto goto_next_file;
3441 mpctx->initialized_flags|=INITIALIZED_STREAM;
3443 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3444 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3445 "playlist support will not be used automatically.\n"
3446 "MPlayer's playlist code is unsafe and should only be used with "
3447 "trusted sources.\nPlayback will probably fail.\n\n");
3448 #if 0
3449 play_tree_t* entry;
3450 // Handle playlist
3451 current_module="handle_playlist";
3452 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3453 filename_recode(mpctx->filename));
3454 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3455 mpctx->eof=playtree_add_playlist(mpctx, entry);
3456 goto goto_next_file;
3457 #endif
3459 mpctx->stream->start_pos+=seek_to_byte;
3461 if(stream_dump_type==5){
3462 unsigned char buf[4096];
3463 int len;
3464 FILE *f;
3465 current_module="dumpstream";
3466 stream_reset(mpctx->stream);
3467 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3468 f=fopen(stream_dump_name,"wb");
3469 if(!f){
3470 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3471 exit_player(mpctx, EXIT_ERROR);
3473 if (dvd_chapter > 1) {
3474 int chapter = dvd_chapter - 1;
3475 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3477 while(!mpctx->stream->eof && !async_quit_request){
3478 len=stream_read(mpctx->stream,buf,4096);
3479 if(len>0) {
3480 if(fwrite(buf,len,1,f) != 1) {
3481 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3482 exit_player(mpctx, EXIT_ERROR);
3485 if(dvd_last_chapter > 0) {
3486 int chapter = -1;
3487 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3488 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3489 break;
3492 if(fclose(f)) {
3493 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3494 exit_player(mpctx, EXIT_ERROR);
3496 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3497 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3500 #ifdef CONFIG_DVDREAD
3501 if(mpctx->stream->type==STREAMTYPE_DVD){
3502 current_module="dvd lang->id";
3503 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3504 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3505 // setup global sub numbering
3506 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3507 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3508 current_module=NULL;
3510 #endif
3512 #ifdef CONFIG_DVDNAV
3513 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3514 current_module="dvdnav lang->id";
3515 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3516 dvdsub_lang_id = -3;
3517 if(dvdsub_lang && opts->sub_id==-1)
3518 dvdsub_lang_id = opts->sub_id = mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3519 // setup global sub numbering
3520 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3521 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3522 current_module=NULL;
3524 #endif
3526 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3527 goto_enable_cache:
3528 if(stream_cache_size>0){
3529 current_module="enable_cache";
3530 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3531 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3532 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3533 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3536 //============ Open DEMUXERS --- DETECT file type =======================
3537 current_module="demux_open";
3539 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3541 // HACK to get MOV Reference Files working
3543 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3545 unsigned char* playlist_entry;
3546 play_tree_t *list = NULL, *entry = NULL;
3548 current_module="handle_demux_playlist";
3549 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3551 char *temp, *bname;
3553 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3554 filename_recode(playlist_entry));
3556 bname=mp_basename(playlist_entry);
3557 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3558 continue;
3560 if (!strcmp(playlist_entry, mpctx->filename)) // ignoring self-reference
3561 continue;
3563 entry = play_tree_new();
3565 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3567 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3568 if (temp)
3570 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3571 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3572 strcat(temp, playlist_entry);
3573 if (!strcmp(temp, mpctx->filename)) {
3574 free(temp);
3575 continue;
3577 play_tree_add_file(entry,temp);
3578 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3579 free(temp);
3582 else
3583 play_tree_add_file(entry,playlist_entry);
3585 if(!list)
3586 list = entry;
3587 else
3588 play_tree_append_entry(list,entry);
3590 free_demuxer(mpctx->demuxer);
3591 mpctx->demuxer = NULL;
3593 if (list)
3595 entry = play_tree_new();
3596 play_tree_set_child(entry,list);
3597 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3598 goto goto_next_file;
3602 if(!mpctx->demuxer)
3603 goto goto_next_file;
3605 if (mpctx->demuxer->matroska_data.ordered_chapters)
3606 build_ordered_chapter_timeline(mpctx);
3608 if (!mpctx->sources) {
3609 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
3610 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
3611 .demuxer = mpctx->demuxer};
3612 mpctx->num_sources = 1;
3615 if(dvd_chapter>1) {
3616 double pts;
3617 if (seek_chapter(mpctx, dvd_chapter-1, &pts, NULL) >= 0 && pts > -1.0)
3618 seek(mpctx, pts, SEEK_ABSOLUTE);
3621 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3623 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3624 int i;
3625 int maxid = -1;
3626 // setup global sub numbering
3627 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3628 for (i = 0; i < MAX_S_STREAMS; i++)
3629 if (mpctx->demuxer->s_streams[i])
3630 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3631 mpctx->global_sub_size += maxid + 1;
3633 // Make opts->sub_id always selectable if set.
3634 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3635 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3637 #ifdef CONFIG_ASS
3638 if (opts->ass_enabled && ass_library) {
3639 for (int j = 0; j < mpctx->num_sources; j++) {
3640 struct demuxer *d = mpctx->sources[j].demuxer;
3641 for (int i = 0; i < d->num_attachments; i++) {
3642 struct demux_attachment *att = d->attachments + i;
3643 if (extract_embedded_fonts
3644 && att->name && att->type && att->data && att->data_size
3645 && (strcmp(att->type, "application/x-truetype-font") == 0
3646 || strcmp(att->type, "application/x-font") == 0))
3647 ass_add_font(ass_library, att->name, att->data, att->data_size);
3651 #endif
3653 current_module="demux_open2";
3655 //file_format=demuxer->file_format;
3657 mpctx->d_audio=mpctx->demuxer->audio;
3658 mpctx->d_video=mpctx->demuxer->video;
3659 mpctx->d_sub=mpctx->demuxer->sub;
3661 if (ts_prog) {
3662 int tmp = ts_prog;
3663 mp_property_do("switch_program", M_PROPERTY_SET, &tmp, mpctx);
3665 // select audio stream
3666 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3668 // DUMP STREAMS:
3669 if((stream_dump_type)&&(stream_dump_type<4)){
3670 FILE *f;
3671 demux_stream_t *ds=NULL;
3672 current_module="dump";
3673 // select stream to dump
3674 switch(stream_dump_type){
3675 case 1: ds=mpctx->d_audio;break;
3676 case 2: ds=mpctx->d_video;break;
3677 case 3: ds=mpctx->d_sub;break;
3679 if(!ds){
3680 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"dump: FATAL: Selected stream missing!\n");
3681 exit_player(mpctx, EXIT_ERROR);
3683 // disable other streams:
3684 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3685 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3686 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3687 // let's dump it!
3688 f=fopen(stream_dump_name,"wb");
3689 if(!f){
3690 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3691 exit_player(mpctx, EXIT_ERROR);
3693 while(!ds->eof){
3694 unsigned char* start;
3695 int in_size=ds_get_packet(ds,&start);
3696 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3697 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3698 if(in_size>0) fwrite(start,in_size,1,f);
3699 if(dvd_last_chapter>0) {
3700 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3701 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3702 break;
3705 fclose(f);
3706 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3707 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3710 mpctx->sh_audio=mpctx->d_audio->sh;
3711 mpctx->sh_video=mpctx->d_video->sh;
3713 if(mpctx->sh_video){
3715 current_module="video_read_properties";
3716 if(!video_read_properties(mpctx->sh_video)) {
3717 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Video: Cannot read properties.\n");
3718 mpctx->sh_video=mpctx->d_video->sh=NULL;
3719 } else {
3720 mp_tmsg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
3721 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3722 mpctx->sh_video->fps,mpctx->sh_video->frametime
3725 /* need to set fps here for output encoders to pick it up in their init */
3726 if(force_fps){
3727 mpctx->sh_video->fps=force_fps;
3728 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3730 vo_fps = mpctx->sh_video->fps;
3732 if(!mpctx->sh_video->fps && !force_fps){
3733 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"FPS not specified in the header or invalid, use the -fps option.\n");
3734 mpctx->sh_video=mpctx->d_video->sh=NULL;
3740 if(!mpctx->sh_video && !mpctx->sh_audio){
3741 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "No stream found.\n");
3742 #ifdef CONFIG_DVBIN
3743 if(mpctx->stream->type == STREAMTYPE_DVB)
3745 int dir;
3746 int v = mpctx->last_dvb_step;
3747 if(v > 0)
3748 dir = DVB_CHANNEL_HIGHER;
3749 else
3750 dir = DVB_CHANNEL_LOWER;
3752 if(dvb_step_channel(mpctx->stream, dir)) {
3753 mpctx->stop_play = PT_NEXT_ENTRY;
3754 mpctx->dvbin_reopen = 1;
3757 #endif
3758 goto goto_next_file; // exit_player(_("Fatal error"));
3761 /* display clip info */
3762 demux_info_print(mpctx->demuxer);
3764 //================== Read SUBTITLES (DVD & TEXT) ==========================
3765 if(vo_spudec==NULL && mpctx->sh_video &&
3766 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3767 init_vo_spudec(mpctx);
3770 // after reading video params we should load subtitles because
3771 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3772 // check .sub
3773 current_module="read_subtitles_file";
3774 double sub_fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
3775 if(sub_name){
3776 for (i = 0; sub_name[i] != NULL; ++i)
3777 add_subtitles(mpctx, sub_name[i], sub_fps, 0);
3779 if(sub_auto) { // auto load sub file ...
3780 char *psub = get_path( "sub/" );
3781 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3782 int i = 0;
3783 free(psub); // release the buffer created by get_path() above
3784 while (tmp[i]) {
3785 add_subtitles(mpctx, tmp[i], sub_fps, 1);
3786 free(tmp[i++]);
3788 free(tmp);
3790 if (mpctx->set_of_sub_size > 0) {
3791 // setup global sub numbering
3792 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3793 mpctx->global_sub_size += mpctx->set_of_sub_size;
3797 if (mpctx->global_sub_size) {
3798 select_subtitle(mpctx);
3799 if(subdata)
3800 switch (stream_dump_type) {
3801 case 3: list_sub_file(subdata); break;
3802 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3803 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3804 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3805 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3806 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3810 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3811 filename_recode(mpctx->filename));
3812 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3813 if (mpctx->sh_video) {
3814 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3815 if (mpctx->sh_video->format >= 0x20202020)
3816 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3817 else
3818 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3819 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3820 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3821 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3822 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3823 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3825 if (mpctx->sh_audio) {
3826 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3827 if (mpctx->sh_audio->format >= 0x20202020)
3828 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3829 else
3830 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3831 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3832 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3833 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3835 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3836 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n",
3837 mpctx->stream->seek && (!mpctx->demuxer || mpctx->demuxer->seekable));
3838 if (mpctx->demuxer) {
3839 if (mpctx->demuxer->num_chapters == 0)
3840 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3841 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3844 if(!mpctx->sh_video) goto main; // audio-only
3846 if(!reinit_video_chain(mpctx)) {
3847 if(!mpctx->sh_video){
3848 if(!mpctx->sh_audio) goto goto_next_file;
3849 goto main; // exit_player(_("Fatal error"));
3853 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3854 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3856 #ifdef CONFIG_FREETYPE
3857 force_load_font = 1;
3858 #endif
3860 //================== MAIN: ==========================
3861 main:
3862 current_module="main";
3864 if(playing_msg) {
3865 char* msg = property_expand_string(mpctx, playing_msg);
3866 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3867 free(msg);
3871 // Disable the term OSD in verbose mode
3872 if(verbose) term_osd = 0;
3875 //int frame_corr_num=0; //
3876 //float v_frame=0; // Video
3877 //float num_frames=0; // number of frames played
3879 int frame_time_remaining=0; // flag
3880 int blit_frame=0;
3882 // Make sure old OSD does not stay around,
3883 // e.g. with -fixed-vo and same-resolution files
3884 clear_osd_msgs();
3885 update_osd_msg(mpctx);
3887 //================ SETUP AUDIO ==========================
3889 if(mpctx->sh_audio){
3890 reinit_audio_chain(mpctx);
3891 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3892 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3895 current_module="av_init";
3897 if(mpctx->sh_video){
3898 mpctx->sh_video->timer=0;
3899 if (! ignore_start)
3900 audio_delay += mpctx->sh_video->stream_delay;
3902 if(mpctx->sh_audio){
3903 if (start_volume >= 0)
3904 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3905 if (! ignore_start)
3906 audio_delay -= mpctx->sh_audio->stream_delay;
3907 mpctx->delay=-audio_delay;
3910 if(!mpctx->sh_audio){
3911 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Audio: no sound\n");
3912 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3913 ds_free_packs(mpctx->d_audio); // free buffered chunks
3914 //mpctx->d_audio->id=-2; // do not read audio chunks
3915 //uninit_player(mpctx, INITIALIZED_AO); // close device
3917 if(!mpctx->sh_video){
3918 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Video: no video\n");
3919 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3920 ds_free_packs(mpctx->d_video);
3921 mpctx->d_video->id=-2;
3922 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3925 if (!mpctx->sh_video && !mpctx->sh_audio)
3926 goto goto_next_file;
3928 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3929 if(force_fps && mpctx->sh_video){
3930 vo_fps = mpctx->sh_video->fps=force_fps;
3931 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3932 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx->sh_video->fps,mpctx->sh_video->frametime);
3935 mp_input_set_section(mpctx->input, NULL);
3936 //TODO: add desired (stream-based) sections here
3937 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3938 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3940 //==================== START PLAYING =======================
3942 if(opts->loop_times>1) opts->loop_times--; else
3943 if(opts->loop_times==1) opts->loop_times = -1;
3945 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Starting playback...\n");
3947 total_time_usage_start=GetTimer();
3948 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3949 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3950 play_n_frames=play_n_frames_mf;
3952 if(play_n_frames==0){
3953 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
3956 // If there's a timeline force an absolute seek to initialize state
3957 if (seek_to_sec || mpctx->timeline) {
3958 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3959 end_at.pos += seek_to_sec;
3962 if (end_at.type == END_AT_SIZE) {
3963 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Option -endpos in MPlayer does not yet support size units.\n");
3964 end_at.type = END_AT_NONE;
3967 #ifdef CONFIG_DVDNAV
3968 mp_dvdnav_context_free(mpctx);
3969 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3970 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3971 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3973 #endif
3975 get_relative_time(mpctx); // reset current delta
3976 mpctx->time_frame = 0;
3977 mpctx->drop_message_shown = 0;
3978 mpctx->update_video_immediately = true;
3979 mpctx->total_avsync_change = 0;
3980 // Make sure VO knows current pause state
3981 if (mpctx->sh_video)
3982 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
3983 NULL);
3985 while(!mpctx->stop_play){
3986 float aq_sleep_time=0;
3988 if(dvd_last_chapter>0) {
3989 int cur_chapter = get_current_chapter(mpctx);
3990 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3991 goto goto_next_file;
3994 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3995 mpctx->sh_audio = mpctx->d_audio->sh;
3996 mpctx->sh_audio->ds = mpctx->d_audio;
3997 reinit_audio_chain(mpctx);
4000 /*========================== PLAY AUDIO ============================*/
4002 if (mpctx->sh_audio && !mpctx->paused)
4003 if (!fill_audio_out_buffers(mpctx))
4004 // at eof, all audio at least written to ao
4005 if (!mpctx->sh_video)
4006 mpctx->stop_play = AT_END_OF_FILE;
4009 if(!mpctx->sh_video) {
4010 // handle audio-only case:
4011 double a_pos=0;
4012 // sh_audio can be NULL due to video stream switching
4013 // TODO: handle this better
4014 if (mpctx->sh_audio)
4015 a_pos = playing_audio_pts(mpctx);
4017 print_status(mpctx, a_pos, false);
4019 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
4020 mpctx->stop_play = PT_NEXT_ENTRY;
4021 update_subtitles(mpctx, &mpctx->opts, NULL, a_pos, mpctx->video_offset,
4022 mpctx->d_sub, 0);
4023 update_osd_msg(mpctx);
4025 } else {
4027 /*========================== PLAY VIDEO ============================*/
4029 vo_pts=mpctx->sh_video->timer*90000.0;
4030 vo_fps=mpctx->sh_video->fps;
4032 blit_frame = mpctx->video_out->frame_loaded;
4033 if (!blit_frame) {
4034 double frame_time = update_video(mpctx);
4035 blit_frame = mpctx->video_out->frame_loaded;
4036 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
4037 if (mpctx->sh_video->vf_initialized < 0) {
4038 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "\nFATAL: Could not initialize video filters (-vf) or video output (-vo).\n");
4039 mpctx->stop_play = PT_NEXT_ENTRY;
4040 goto goto_next_file;
4042 if (blit_frame) {
4043 struct sh_video *sh_video = mpctx->sh_video;
4044 update_subtitles(mpctx, &mpctx->opts, sh_video, sh_video->pts,
4045 mpctx->video_offset, mpctx->d_sub, 0);
4046 update_teletext(sh_video, mpctx->demuxer, 0);
4047 update_osd_msg(mpctx);
4048 struct vf_instance *vf = mpctx->sh_video->vfilter;
4049 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
4050 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
4051 vo_osd_changed(0);
4053 if (frame_time < 0)
4054 mpctx->stop_play = AT_END_OF_FILE;
4055 else {
4056 if (mpctx->update_video_immediately) {
4057 // Show this frame immediately, rest normally
4058 mpctx->update_video_immediately = false;
4059 } else {
4060 mpctx->time_frame += frame_time / opts->playback_speed;
4061 adjust_sync(mpctx, frame_time);
4065 if (mpctx->timeline) {
4066 struct timeline_part *next = mpctx->timeline + mpctx->timeline_part + 1;
4067 if (mpctx->sh_video->pts >= next->start
4068 || mpctx->stop_play == AT_END_OF_FILE
4069 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
4070 seek(mpctx, next->start, SEEK_ABSOLUTE);
4071 continue;
4075 // ==========================================================================
4077 // current_module="draw_osd";
4078 // if(vo_config_count) mpctx->video_out->draw_osd();
4080 current_module="vo_check_events";
4081 vo_check_events(mpctx->video_out);
4083 #ifdef CONFIG_X11
4084 if (stop_xscreensaver) {
4085 current_module = "stop_xscreensaver";
4086 xscreensaver_heartbeat(mpctx->x11_state);
4088 #endif
4089 if (heartbeat_cmd) {
4090 static unsigned last_heartbeat;
4091 unsigned now = GetTimerMS();
4092 if (now - last_heartbeat > 30000) {
4093 last_heartbeat = now;
4094 system(heartbeat_cmd);
4098 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
4100 //====================== FLIP PAGE (VIDEO BLT): =========================
4102 current_module="flip_page";
4103 if (!frame_time_remaining && blit_frame) {
4104 unsigned int t2=GetTimer();
4105 unsigned int pts_us = mpctx->last_time + mpctx->time_frame * 1e6;
4106 int duration = -1;
4107 double pts2 = mpctx->video_out->next_pts2;
4108 if (pts2 != MP_NOPTS_VALUE && opts->correct_pts) {
4109 // expected A/V sync correction is ignored
4110 double diff = (pts2 - mpctx->sh_video->pts);
4111 diff /= opts->playback_speed;
4112 if (mpctx->time_frame < 0)
4113 diff += mpctx->time_frame;
4114 if (diff < 0)
4115 diff = 0;
4116 if (diff > 10)
4117 diff = 10;
4118 duration = diff * 1e6;
4120 vo_flip_page(mpctx->video_out, pts_us|1, duration);
4122 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
4123 vout_time_usage += mpctx->last_vo_flip_duration;
4124 if (mpctx->video_out->driver->flip_page_timed) {
4125 // No need to adjust sync based on flip speed
4126 mpctx->last_vo_flip_duration = 0;
4127 // For print_status - VO call finishing early is OK for sync
4128 mpctx->time_frame -= get_relative_time(mpctx);
4130 print_status(mpctx, MP_NOPTS_VALUE, true);
4132 else
4133 print_status(mpctx, MP_NOPTS_VALUE, false);
4135 //============================ Auto QUALITY ============================
4137 /*Output quality adjustments:*/
4138 if(auto_quality>0){
4139 current_module="autoq";
4140 // float total=0.000001f * (GetTimer()-aq_total_time);
4141 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4142 if(output_quality<auto_quality && aq_sleep_time>0)
4143 ++output_quality;
4144 else
4145 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4146 if(output_quality>1 && aq_sleep_time<0)
4147 --output_quality;
4148 else
4149 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4150 output_quality=0;
4151 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4152 set_video_quality(mpctx->sh_video,output_quality);
4155 if (!frame_time_remaining && blit_frame) {
4156 if (play_n_frames >= 0) {
4157 --play_n_frames;
4158 if (play_n_frames <= 0)
4159 mpctx->stop_play = PT_NEXT_ENTRY;
4161 if (mpctx->step_frames > 0) {
4162 mpctx->step_frames--;
4163 if (mpctx->step_frames == 0)
4164 pause_player(mpctx);
4169 // FIXME: add size based support for -endpos
4170 if (end_at.type == END_AT_TIME &&
4171 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
4172 mpctx->stop_play = PT_NEXT_ENTRY;
4174 } // end if(mpctx->sh_video)
4176 #ifdef CONFIG_DVDNAV
4177 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4178 nav_highlight_t hl;
4179 mp_dvdnav_get_highlight (mpctx->stream, &hl);
4180 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4181 vo_osd_changed (OSDTYPE_DVDNAV);
4183 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
4184 double ar = -1.0;
4185 if (stream_control (mpctx->demuxer->stream,
4186 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
4187 != STREAM_UNSUPPORTED)
4188 mpctx->sh_video->stream_aspect = ar;
4191 #endif
4193 //================= Keyboard events, SEEKing ====================
4195 current_module="key_events";
4198 while (1) {
4199 mp_cmd_t* cmd;
4200 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
4201 run_command(mpctx, cmd);
4202 mp_cmd_free(cmd);
4203 if (mpctx->stop_play)
4204 break;
4206 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
4207 || mpctx->abs_seek_pos)
4208 break;
4209 if (mpctx->sh_video) {
4210 update_osd_msg(mpctx);
4211 int hack = vo_osd_changed(0);
4212 vo_osd_changed(hack);
4213 if (hack)
4214 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
4215 add_step_frame(mpctx);
4216 break;
4218 else
4219 vo_osd_changed(0);
4221 pause_loop(mpctx);
4225 // handle -sstep
4226 if (step_sec > 0 && !mpctx->paused) {
4227 mpctx->osd_function=OSD_FFW;
4228 mpctx->rel_seek_secs+=step_sec;
4231 edl_update(mpctx);
4233 /* Looping. */
4234 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
4235 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
4237 if(opts->loop_times>1) opts->loop_times--; else
4238 if(opts->loop_times==1) opts->loop_times=-1;
4239 play_n_frames=play_n_frames_mf;
4240 mpctx->stop_play=0;
4241 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
4244 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
4245 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
4247 mpctx->rel_seek_secs=0;
4248 mpctx->abs_seek_pos=0;
4251 } // while(!mpctx->stop_play)
4253 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4255 #ifdef CONFIG_DVBIN
4256 if(mpctx->dvbin_reopen)
4258 mpctx->stop_play = 0;
4259 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4260 cache_uninit(mpctx->stream);
4261 mpctx->dvbin_reopen = 0;
4262 goto goto_enable_cache;
4264 #endif
4267 goto_next_file: // don't jump here after ao/vo/getch initialization!
4269 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4271 if(benchmark){
4272 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4273 double total_time_usage;
4274 total_time_usage_start=GetTimer()-total_time_usage_start;
4275 total_time_usage = (float)total_time_usage_start*0.000001;
4276 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4277 video_time_usage,vout_time_usage,audio_time_usage,
4278 total_time_usage-tot,total_time_usage);
4279 if(total_time_usage>0.0)
4280 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4281 100.0*video_time_usage/total_time_usage,
4282 100.0*vout_time_usage/total_time_usage,
4283 100.0*audio_time_usage/total_time_usage,
4284 100.0*(total_time_usage-tot)/total_time_usage,
4285 100.0);
4286 if(total_frame_cnt && frame_dropping)
4287 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4288 total_frame_cnt-drop_frame_cnt,
4289 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4290 drop_frame_cnt,
4291 100*drop_frame_cnt/total_frame_cnt,
4292 total_frame_cnt,
4293 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4296 // time to uninit all, except global stuff:
4297 uninit_player(mpctx, INITIALIZED_ALL-(opts->fixed_vo?INITIALIZED_VO:0));
4299 if(mpctx->set_of_sub_size > 0) {
4300 current_module="sub_free";
4301 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4302 sub_free(mpctx->set_of_subtitles[i]);
4303 #ifdef CONFIG_ASS
4304 if(mpctx->set_of_ass_tracks[i])
4305 ass_free_track( mpctx->set_of_ass_tracks[i] );
4306 #endif
4308 mpctx->set_of_sub_size = 0;
4310 vo_sub_last = vo_sub=NULL;
4311 subdata=NULL;
4312 #ifdef CONFIG_ASS
4313 ass_track = NULL;
4314 if(ass_library)
4315 ass_clear_fonts(ass_library);
4316 #endif
4318 if (!mpctx->stop_play) // In case some goto jumped here...
4319 mpctx->stop_play = PT_NEXT_ENTRY;
4321 int playtree_direction = 1;
4323 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4324 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4325 play_tree_iter_free(mpctx->playtree_iter);
4326 mpctx->playtree_iter = NULL;
4328 mpctx->play_tree_step = 1;
4329 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4330 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4331 if(mpctx->playtree_iter) {
4332 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4333 play_tree_iter_free(mpctx->playtree_iter);
4334 mpctx->playtree_iter = NULL;
4337 } else if (mpctx->stop_play == PT_STOP) {
4338 play_tree_iter_free(mpctx->playtree_iter);
4339 mpctx->playtree_iter = NULL;
4340 } else { // NEXT PREV SRC
4341 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4344 while(mpctx->playtree_iter != NULL) {
4345 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4346 if(mpctx->filename == NULL) {
4347 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4348 play_tree_iter_free(mpctx->playtree_iter);
4349 mpctx->playtree_iter = NULL;
4351 } else
4352 break;
4355 if(mpctx->playtree_iter != NULL || player_idle_mode){
4356 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4357 mpctx->stop_play = 0;
4358 goto play_next_file;
4362 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4364 return 1;
4366 #endif /* DISABLE_MAIN */