Update mplayer sources
[mplayer/kovensky.git] / mplayer.c
blobe701d55bee56b07cfc7c4065e914b2c1ca111974
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 uninit_player(mpctx, INITIALIZED_ALL);
675 #if defined(__MINGW32__) || defined(__CYGWIN__)
676 timeEndPeriod(1);
677 #endif
678 #ifdef CONFIG_X11
679 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
680 #endif
682 current_module="uninit_input";
683 mp_input_uninit(mpctx->input);
684 #ifdef CONFIG_MENU
685 if (use_menu)
686 menu_uninit();
687 #endif
689 #ifdef CONFIG_FREETYPE
690 current_module="uninit_font";
691 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
692 free_font_desc(mpctx->osd->sub_font);
693 free_font_desc(vo_font);
694 vo_font = NULL;
695 done_freetype();
696 #endif
697 osd_free(mpctx->osd);
699 #ifdef CONFIG_ASS
700 ass_library_done(ass_library);
701 ass_library = NULL;
702 #endif
704 current_module="exit_player";
706 // free mplayer config
707 if(mpctx->mconfig)
708 m_config_free(mpctx->mconfig);
709 mpctx->mconfig = NULL;
711 if(mpctx->playtree_iter)
712 play_tree_iter_free(mpctx->playtree_iter);
713 mpctx->playtree_iter = NULL;
714 if(mpctx->playtree)
715 play_tree_free(mpctx->playtree, 1);
716 mpctx->playtree = NULL;
718 talloc_free(mpctx->key_fifo);
720 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
721 edl_records = NULL;
722 switch(how) {
723 case EXIT_QUIT:
724 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Quit");
725 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
726 break;
727 case EXIT_EOF:
728 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","End of file");
729 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
730 break;
731 case EXIT_ERROR:
732 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Fatal error");
733 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
734 break;
735 default:
736 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
738 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
740 exit(rc);
743 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
745 exit_player_with_rc(mpctx, how, 1);
748 #ifndef __MINGW32__
749 static void child_sighandler(int x){
750 pid_t pid;
751 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
753 #endif
755 #ifdef CONFIG_CRASH_DEBUG
756 static char *prog_path;
757 static int crash_debug = 0;
758 #endif
760 static void exit_sighandler(int x){
761 static int sig_count=0;
762 #ifdef CONFIG_CRASH_DEBUG
763 if (!crash_debug || x != SIGTRAP)
764 #endif
765 ++sig_count;
766 if(sig_count==5)
768 /* We're crashing bad and can't uninit cleanly :(
769 * by popular request, we make one last (dirty)
770 * effort to restore the user's
771 * terminal. */
772 getch2_disable();
773 exit(1);
775 if(sig_count==6) exit(1);
776 if(sig_count>6){
777 // can't stop :(
778 #ifndef __MINGW32__
779 kill(getpid(),SIGKILL);
780 #endif
782 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"\n" "\nMPlayer interrupted by signal %d in module: %s\n",x,
783 current_module?current_module:"unknown"
785 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
786 if(sig_count<=1)
787 switch(x){
788 case SIGINT:
789 case SIGQUIT:
790 case SIGTERM:
791 case SIGKILL:
792 async_quit_request = 1;
793 return; // killed from keyboard (^C) or killed [-9]
794 case SIGILL:
795 #if CONFIG_RUNTIME_CPUDETECT
796 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL_RTCpuSel);
797 #else
798 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL);
799 #endif
800 case SIGFPE:
801 case SIGSEGV:
802 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGSEGV_SIGFPE);
803 default:
804 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGCRASH);
805 #ifdef CONFIG_CRASH_DEBUG
806 if (crash_debug) {
807 int gdb_pid;
808 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
809 gdb_pid = fork();
810 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
811 if (gdb_pid == 0) { // We are the child
812 char spid[20];
813 snprintf(spid, sizeof(spid), "%i", getppid());
814 getch2_disable(); // allow terminal to work properly with gdb
815 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
816 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
817 } else if (gdb_pid < 0)
818 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
819 else {
820 waitpid(gdb_pid, NULL, 0);
822 if (x == SIGTRAP) return;
824 #endif
826 getch2_disable();
827 exit(1);
830 #include "cfg-mplayer.h"
832 static int cfg_include(m_option_t *conf, char *filename)
834 return m_config_parse_config_file(conf->priv, filename);
837 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
839 char *conffile;
840 int conffile_fd;
841 if (!disable_system_conf &&
842 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
843 exit_player(mpctx, EXIT_NONE);
844 if ((conffile = get_path("")) == NULL) {
845 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"Cannot find HOME directory.\n");
846 } else {
847 #ifdef __MINGW32__
848 mkdir(conffile);
849 #else
850 mkdir(conffile, 0777);
851 #endif
852 free(conffile);
853 if ((conffile = get_path("config")) == NULL) {
854 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
855 } else {
856 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
857 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Creating config file: %s\n", conffile);
858 write(conffile_fd, default_config, strlen(default_config));
859 close(conffile_fd);
861 if (!disable_user_conf &&
862 m_config_parse_config_file(conf, conffile) < 0)
863 exit_player(mpctx, EXIT_NONE);
864 free(conffile);
869 #define PROFILE_CFG_PROTOCOL "protocol."
871 static void load_per_protocol_config (m_config_t* conf, const char *const file)
873 char *str;
874 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
875 m_profile_t *p;
877 /* does filename actually uses a protocol ? */
878 str = strstr (file, "://");
879 if (!str)
880 return;
882 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
883 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
884 p = m_config_get_profile (conf, protocol);
885 if (p)
887 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading protocol-related profile '%s'\n", protocol);
888 m_config_set_profile(conf,p);
892 #define PROFILE_CFG_EXTENSION "extension."
894 static void load_per_extension_config (m_config_t* conf, const char *const file)
896 char *str;
897 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
898 m_profile_t *p;
900 /* does filename actually have an extension ? */
901 str = strrchr (file, '.');
902 if (!str)
903 return;
905 sprintf (extension, PROFILE_CFG_EXTENSION);
906 strncat (extension, ++str, 7);
907 p = m_config_get_profile (conf, extension);
908 if (p)
910 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", extension);
911 m_config_set_profile(conf,p);
915 #define PROFILE_CFG_VO "vo."
916 #define PROFILE_CFG_AO "ao."
918 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
920 char profile[strlen (cfg) + strlen (out) + 1];
921 m_profile_t *p;
923 sprintf (profile, "%s%s", cfg, out);
924 p = m_config_get_profile (conf, profile);
925 if (p)
927 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", profile);
928 m_config_set_profile(conf,p);
933 * Tries to load a config file
934 * @return 0 if file was not found, 1 otherwise
936 static int try_load_config(m_config_t *conf, const char *file)
938 struct stat st;
939 if (stat(file, &st))
940 return 0;
941 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Loading config '%s'\n", file);
942 m_config_parse_config_file (conf, file);
943 return 1;
946 static void load_per_file_config (m_config_t* conf, const char *const file)
948 char *confpath;
949 char cfg[PATH_MAX];
950 char *name;
952 if (strlen(file) > PATH_MAX - 14) {
953 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Filename is too long, can not load file or directory specific config files\n");
954 return;
956 sprintf (cfg, "%s.conf", file);
958 name = strrchr(cfg, '/');
959 if (HAVE_DOS_PATHS) {
960 char *tmp = strrchr(cfg, '\\');
961 if (!name || tmp > name)
962 name = tmp;
963 tmp = strrchr(cfg, ':');
964 if (!name || tmp > name)
965 name = tmp;
967 if (!name)
968 name = cfg;
969 else
970 name++;
972 if (use_filedir_conf) {
973 char dircfg[PATH_MAX];
974 strcpy(dircfg, cfg);
975 strcpy(dircfg + (name - cfg), "mplayer.conf");
976 try_load_config(conf, dircfg);
978 if (try_load_config(conf, cfg))
979 return;
982 if ((confpath = get_path (name)) != NULL)
984 try_load_config(conf, confpath);
986 free (confpath);
990 /* When libmpdemux performs a blocking operation (network connection or
991 * cache filling) if the operation fails we use this function to check
992 * if it was interrupted by the user.
993 * The function returns a new value for eof. */
994 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
996 mp_cmd_t* cmd;
997 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
998 switch(cmd->id) {
999 case MP_CMD_QUIT:
1000 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
1001 case MP_CMD_PLAY_TREE_STEP: {
1002 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
1003 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
1004 } break;
1005 case MP_CMD_PLAY_TREE_UP_STEP: {
1006 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
1007 } break;
1008 case MP_CMD_PLAY_ALT_SRC_STEP: {
1009 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
1010 } break;
1012 mp_cmd_free(cmd);
1014 return stop_play;
1017 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
1019 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
1021 play_tree_add_bpf(entry,mpctx->filename);
1024 if(!entry) {
1025 entry = mpctx->playtree_iter->tree;
1026 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1027 return PT_NEXT_ENTRY;
1029 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
1030 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1031 return PT_NEXT_ENTRY;
1034 play_tree_remove(entry,1,1);
1035 return PT_NEXT_SRC;
1037 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
1038 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1039 entry = mpctx->playtree_iter->tree;
1040 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1041 return PT_NEXT_ENTRY;
1043 play_tree_remove(entry,1,1);
1045 return PT_NEXT_SRC;
1048 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1050 struct MPOpts *opts = &mpctx->opts;
1051 sub_data *subd = NULL;
1052 struct ass_track *asst = NULL;
1054 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1055 return;
1058 #ifdef CONFIG_ASS
1059 if (opts->ass_enabled) {
1060 #ifdef CONFIG_ICONV
1061 asst = ass_read_file(ass_library, filename, sub_cp);
1062 #else
1063 asst = ass_read_file(ass_library, filename, 0);
1064 #endif
1065 if (!asst) {
1066 subd = sub_read_file(filename, fps);
1067 if (subd) {
1068 asst = ass_read_subdata(ass_library, subd, fps);
1069 if (asst) {
1070 sub_free(subd);
1071 subd = NULL;
1075 } else
1076 #endif
1077 subd = sub_read_file(filename, fps);
1080 if (!asst && !subd) {
1081 mp_tmsg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR,
1082 "Cannot load subtitles: %s\n", filename_recode(filename));
1083 return;
1086 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1087 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1088 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1089 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1090 filename_recode(filename));
1091 ++mpctx->set_of_sub_size;
1092 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "SUB: Added subtitle file (%d): %s\n", mpctx->set_of_sub_size,
1093 filename_recode(filename));
1096 void init_vo_spudec(struct MPContext *mpctx)
1098 if (vo_spudec)
1099 spudec_free(vo_spudec);
1100 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1101 vo_spudec = NULL;
1102 if (spudec_ifo) {
1103 unsigned int palette[16], width, height;
1104 current_module="spudec_init_vobsub";
1105 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1106 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1109 #ifdef CONFIG_DVDREAD
1110 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1111 current_module="spudec_init_dvdread";
1112 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1113 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1114 NULL, 0);
1116 #endif
1118 #ifdef CONFIG_DVDNAV
1119 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1120 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1121 current_module="spudec_init_dvdnav";
1122 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1124 #endif
1126 if (vo_spudec==NULL) {
1127 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1128 current_module="spudec_init_normal";
1129 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1130 spudec_set_font_factor(vo_spudec,font_factor);
1133 if (vo_spudec!=NULL) {
1134 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1135 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1140 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1141 * make it all work is to use the builtin SDL-bootstrap code, which
1142 * will be done automatically by replacing our main() if we include SDL.h.
1144 #if defined(__APPLE__) && defined(CONFIG_SDL)
1145 #ifdef CONFIG_SDL_SDL_H
1146 #include <SDL/SDL.h>
1147 #else
1148 #include <SDL.h>
1149 #endif
1150 #endif
1153 * \brief append a formatted string
1154 * \param buf buffer to print into
1155 * \param pos position of terminating 0 in buf
1156 * \param len maximum number of characters in buf, not including terminating 0
1157 * \param format printf format string
1159 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1161 va_list va;
1162 va_start(va, format);
1163 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1164 va_end(va);
1165 if (*pos >= len ) {
1166 buf[len] = 0;
1167 *pos = len;
1172 * \brief append time in the hh:mm:ss.f format
1173 * \param buf buffer to print into
1174 * \param pos position of terminating 0 in buf
1175 * \param len maximum number of characters in buf, not including terminating 0
1176 * \param time time value to convert/append
1178 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1179 long tenths = 10 * time;
1180 int f1 = tenths % 10;
1181 int ss = (tenths / 10) % 60;
1182 int mm = (tenths / 600) % 60;
1183 int hh = tenths / 36000;
1184 if (time <= 0) {
1185 saddf(buf, pos, len, "unknown");
1186 return;
1188 if (hh > 0)
1189 saddf(buf, pos, len, "%2d:", hh);
1190 if (hh > 0 || mm > 0)
1191 saddf(buf, pos, len, "%02d:", mm);
1192 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1195 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1197 struct MPOpts *opts = &mpctx->opts;
1198 sh_video_t * const sh_video = mpctx->sh_video;
1200 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1201 a_pos = playing_audio_pts(mpctx);
1202 if (mpctx->sh_audio && sh_video && at_frame) {
1203 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1204 if (mpctx->time_frame > 0)
1205 mpctx->last_av_difference += mpctx->time_frame * opts->playback_speed;
1206 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1207 && !mpctx->drop_message_shown) {
1208 mp_tmsg(MSGT_AVSYNC,MSGL_WARN,SystemTooSlow);
1209 mpctx->drop_message_shown = true;
1212 if (quiet)
1213 return;
1216 int width;
1217 char *line;
1218 unsigned pos = 0;
1219 get_screen_size();
1220 if (screen_width > 0)
1221 width = screen_width;
1222 else
1223 width = 80;
1224 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1225 /* Windows command line is broken (MinGW's rxvt works, but we
1226 * should not depend on that). */
1227 width--;
1228 #endif
1229 line = malloc(width + 1); // one additional char for the terminating null
1231 // Audio time
1232 if (mpctx->sh_audio) {
1233 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1234 if (!sh_video) {
1235 float len = demuxer_get_time_length(mpctx->demuxer);
1236 saddf(line, &pos, width, "(");
1237 sadd_hhmmssf(line, &pos, width, a_pos);
1238 saddf(line, &pos, width, ") of %.1f (", len);
1239 sadd_hhmmssf(line, &pos, width, len);
1240 saddf(line, &pos, width, ") ");
1244 // Video time
1245 if (sh_video)
1246 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1248 // A-V sync
1249 if (mpctx->sh_audio && sh_video)
1250 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1251 mpctx->last_av_difference, mpctx->total_avsync_change);
1253 // Video stats
1254 if (sh_video)
1255 saddf(line, &pos, width, "%3d/%3d ",
1256 (int)sh_video->num_frames,
1257 (int)sh_video->num_frames_decoded);
1259 // CPU usage
1260 if (sh_video) {
1261 if (sh_video->timer > 0.5)
1262 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1263 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1264 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1265 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1266 else
1267 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1268 } else if (mpctx->sh_audio) {
1269 if (mpctx->delay > 0.5)
1270 saddf(line, &pos, width, "%4.1f%% ",
1271 100.0*audio_time_usage/(double)mpctx->delay);
1272 else
1273 saddf(line, &pos, width, "??,?%% ");
1276 // VO stats
1277 if (sh_video)
1278 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1280 #ifdef CONFIG_STREAM_CACHE
1281 // cache stats
1282 if (stream_cache_size > 0)
1283 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1284 #endif
1286 // other
1287 if (opts->playback_speed != 1)
1288 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1290 // end
1291 if (erase_to_end_of_line) {
1292 line[pos] = 0;
1293 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1294 } else {
1295 memset(&line[pos], ' ', width - pos);
1296 line[width] = 0;
1297 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1299 free(line);
1303 * \brief build a chain of audio filters that converts the input format
1304 * to the ao's format, taking into account the current playback_speed.
1305 * \param sh_audio describes the requested input format of the chain.
1306 * \param ao_data describes the requested output format of the chain.
1308 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1310 struct MPOpts *opts = &mpctx->opts;
1311 int new_srate;
1312 int result;
1313 if (!sh_audio)
1315 mpctx->mixer.afilter = NULL;
1316 return 0;
1318 if(af_control_any_rev(sh_audio->afilter,
1319 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1320 &opts->playback_speed)) {
1321 new_srate = sh_audio->samplerate;
1322 } else {
1323 new_srate = sh_audio->samplerate * opts->playback_speed;
1324 if (new_srate != ao_data->samplerate) {
1325 // limits are taken from libaf/af_resample.c
1326 if (new_srate < 8000)
1327 new_srate = 8000;
1328 if (new_srate > 192000)
1329 new_srate = 192000;
1330 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1333 result = init_audio_filters(sh_audio, new_srate,
1334 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1335 mpctx->mixer.afilter = sh_audio->afilter;
1336 return result;
1340 typedef struct mp_osd_msg mp_osd_msg_t;
1341 struct mp_osd_msg {
1342 /// Previous message on the stack.
1343 mp_osd_msg_t* prev;
1344 /// Message text.
1345 char msg[128];
1346 int id,level,started;
1347 /// Display duration in ms.
1348 unsigned time;
1351 /// OSD message stack.
1352 static mp_osd_msg_t* osd_msg_stack = NULL;
1355 * \brief Add a message on the OSD message stack
1357 * If a message with the same id is already present in the stack
1358 * it is pulled on top of the stack, otherwise a new message is created.
1361 static void set_osd_msg_va(int id, int level, int time, const char *fmt,
1362 va_list ap)
1364 mp_osd_msg_t *msg,*last=NULL;
1365 int r;
1367 // look if the id is already in the stack
1368 for(msg = osd_msg_stack ; msg && msg->id != id ;
1369 last = msg, msg = msg->prev);
1370 // not found: alloc it
1371 if(!msg) {
1372 msg = calloc(1,sizeof(mp_osd_msg_t));
1373 msg->prev = osd_msg_stack;
1374 osd_msg_stack = msg;
1375 } else if(last) { // found, but it's not on top of the stack
1376 last->prev = msg->prev;
1377 msg->prev = osd_msg_stack;
1378 osd_msg_stack = msg;
1380 // write the msg
1381 r = vsnprintf(msg->msg, 128, fmt, ap);
1382 if(r >= 128) msg->msg[127] = 0;
1383 // set id and time
1384 msg->id = id;
1385 msg->level = level;
1386 msg->time = time;
1390 void set_osd_msg(int id, int level, int time, const char *fmt, ...)
1392 va_list ap;
1393 va_start(ap, fmt);
1394 set_osd_msg_va(id, level, time, fmt, ap);
1395 va_end(ap);
1398 void set_osd_tmsg(int id, int level, int time, const char *fmt, ...)
1400 va_list ap;
1401 va_start(ap, fmt);
1402 set_osd_msg_va(id, level, time, mp_gtext(fmt), ap);
1403 va_end(ap);
1408 * \brief Remove a message from the OSD stack
1410 * This function can be used to get rid of a message right away.
1414 void rm_osd_msg(int id) {
1415 mp_osd_msg_t *msg,*last=NULL;
1417 // Search for the msg
1418 for(msg = osd_msg_stack ; msg && msg->id != id ;
1419 last = msg, msg = msg->prev);
1420 if(!msg) return;
1422 // Detach it from the stack and free it
1423 if(last)
1424 last->prev = msg->prev;
1425 else
1426 osd_msg_stack = msg->prev;
1427 free(msg);
1431 * \brief Remove all messages from the OSD stack
1435 static void clear_osd_msgs(void) {
1436 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1437 while(msg) {
1438 prev = msg->prev;
1439 free(msg);
1440 msg = prev;
1442 osd_msg_stack = NULL;
1446 * \brief Get the current message from the OSD stack.
1448 * This function decrements the message timer and destroys the old ones.
1449 * The message that should be displayed is returned (if any).
1453 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1455 struct MPOpts *opts = &mpctx->opts;
1456 mp_osd_msg_t *msg,*prev,*last = NULL;
1457 static unsigned last_update = 0;
1458 unsigned now = GetTimerMS();
1459 unsigned diff;
1460 char hidden_dec_done = 0;
1462 if (mpctx->osd_visible) {
1463 // 36000000 means max timed visibility is 1 hour into the future, if
1464 // the difference is greater assume it's wrapped around from below 0
1465 if (mpctx->osd_visible - now > 36000000) {
1466 mpctx->osd_visible = 0;
1467 vo_osd_progbar_type = -1; // disable
1468 vo_osd_changed(OSDTYPE_PROGBAR);
1469 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1472 if (mpctx->osd_show_percentage_until - now > 36000000)
1473 mpctx->osd_show_percentage_until = 0;
1475 if(!last_update) last_update = now;
1476 diff = now >= last_update ? now - last_update : 0;
1478 last_update = now;
1480 // Look for the first message in the stack with high enough level.
1481 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1482 prev = msg->prev;
1483 if (msg->level > opts->osd_level && hidden_dec_done)
1484 continue;
1485 // The message has a high enough level or it is the first hidden one
1486 // in both cases we decrement the timer or kill it.
1487 if(!msg->started || msg->time > diff) {
1488 if(msg->started) msg->time -= diff;
1489 else msg->started = 1;
1490 // display it
1491 if (msg->level <= opts->osd_level)
1492 return msg;
1493 hidden_dec_done = 1;
1494 continue;
1496 // kill the message
1497 free(msg);
1498 if(last) {
1499 last->prev = prev;
1500 msg = last;
1501 } else {
1502 osd_msg_stack = prev;
1503 msg = NULL;
1506 // Nothing found
1507 return NULL;
1511 * \brief Display the OSD bar.
1513 * Display the OSD bar or fall back on a simple message.
1517 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1518 struct MPOpts *opts = &mpctx->opts;
1519 if (opts->osd_level < 1)
1520 return;
1522 if(mpctx->sh_video) {
1523 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1524 vo_osd_progbar_type = type;
1525 vo_osd_progbar_value = 256*(val-min)/(max-min);
1526 vo_osd_changed(OSDTYPE_PROGBAR);
1527 return;
1530 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1531 name, ROUND(100*(val-min)/(max-min)));
1535 * \brief Display text subtitles on the OSD
1537 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs)
1539 int i;
1540 vo_sub = subs;
1541 vo_osd_changed(OSDTYPE_SUBTITLE);
1542 if (!mpctx->sh_video) {
1543 // reverse order, since newest set_osd_msg is displayed first
1544 for (i = SUB_MAX_TEXT - 1; i >= 0; i--) {
1545 if (!subs || i >= subs->lines || !subs->text[i])
1546 rm_osd_msg(OSD_MSG_SUB_BASE + i);
1547 else {
1548 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1549 int display_time = i == subs->lines - 1 ? 180000 : 2000;
1550 set_osd_msg(OSD_MSG_SUB_BASE + i, 1, display_time, "%s", subs->text[i]);
1557 * \brief Update the OSD message line.
1559 * This function displays the current message on the vo OSD or on the term.
1560 * If the stack is empty and the OSD level is high enough the timer
1561 * is displayed (only on the vo OSD).
1565 static void update_osd_msg(struct MPContext *mpctx)
1567 struct MPOpts *opts = &mpctx->opts;
1568 mp_osd_msg_t *msg;
1569 struct osd_state *osd = mpctx->osd;
1570 char osd_text_timer[128];
1572 if (mpctx->add_osd_seek_info) {
1573 double percentage;
1574 if (mpctx->timeline && mpctx->sh_video)
1575 percentage = mpctx->sh_video->pts * 100 /
1576 mpctx->timeline[mpctx->num_timeline_parts].start;
1577 else
1578 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1579 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1580 if (mpctx->sh_video)
1581 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1582 mpctx->add_osd_seek_info = false;
1585 // Look if we have a msg
1586 if((msg = get_osd_msg(mpctx))) {
1587 if (strcmp(osd->osd_text, msg->msg)) {
1588 strncpy(osd->osd_text, msg->msg, 127);
1589 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1590 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1592 return;
1595 if(mpctx->sh_video) {
1596 // fallback on the timer
1597 if (opts->osd_level >= 2) {
1598 int len;
1599 if (mpctx->timeline)
1600 len = mpctx->timeline[mpctx->num_timeline_parts].start;
1601 else
1602 len = demuxer_get_time_length(mpctx->demuxer);
1603 int percentage = -1;
1604 char percentage_text[10];
1605 int pts = demuxer_get_current_time(mpctx->demuxer);
1607 if (mpctx->osd_show_percentage_until)
1608 if (mpctx->timeline)
1609 percentage = mpctx->sh_video->pts * 100 /
1610 mpctx->timeline[mpctx->num_timeline_parts].start;
1611 else
1612 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1614 if (percentage >= 0)
1615 snprintf(percentage_text, 9, " (%d%%)", percentage);
1616 else
1617 percentage_text[0] = 0;
1619 if (opts->osd_level == 3)
1620 snprintf(osd_text_timer, 63,
1621 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1622 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1623 len/3600,(len/60)%60,len%60,percentage_text);
1624 else
1625 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1626 mpctx->osd_function,pts/3600,(pts/60)%60,
1627 pts%60,percentage_text);
1628 } else
1629 osd_text_timer[0]=0;
1631 if (strcmp(osd->osd_text, osd_text_timer)) {
1632 strncpy(osd->osd_text, osd_text_timer, 63);
1633 vo_osd_changed(OSDTYPE_OSD);
1635 return;
1638 // Clear the term osd line
1639 if (term_osd && osd->osd_text[0]) {
1640 osd->osd_text[0] = 0;
1641 printf("%s\n",term_osd_esc);
1645 ///@}
1646 // OSDMsgStack
1649 void reinit_audio_chain(struct MPContext *mpctx)
1651 struct MPOpts *opts = &mpctx->opts;
1652 if (!mpctx->sh_audio)
1653 return;
1654 current_module="init_audio_codec";
1655 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1656 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1657 goto init_error;
1659 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1660 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1663 current_module="af_preinit";
1664 ao_data.samplerate=force_srate;
1665 ao_data.channels=0;
1666 ao_data.format=audio_output_format;
1667 #if 1
1668 // first init to detect best values
1669 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1670 // input:
1671 mpctx->sh_audio->samplerate,
1672 // output:
1673 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1674 mp_tmsg(MSGT_CPLAYER,MSGL_ERR, "Error at audio filter chain "
1675 "pre-init!\n");
1676 exit_player(mpctx, EXIT_ERROR);
1678 #endif
1679 current_module="ao2_init";
1680 mpctx->audio_out = init_best_audio_out(opts->audio_driver_list,
1681 0, // plugin flag
1682 ao_data.samplerate,
1683 ao_data.channels,
1684 ao_data.format, 0);
1685 if(!mpctx->audio_out){
1686 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Could not open/initialize audio device -> no sound.\n");
1687 goto init_error;
1689 mpctx->initialized_flags|=INITIALIZED_AO;
1690 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1691 mpctx->audio_out->info->short_name,
1692 ao_data.samplerate, ao_data.channels,
1693 af_fmt2str_short(ao_data.format),
1694 af_fmt2bits(ao_data.format)/8 );
1695 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1696 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1697 if(strlen(mpctx->audio_out->info->comment) > 0)
1698 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1699 // init audio filters:
1700 #if 1
1701 current_module="af_init";
1702 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1703 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter/ao format!\n");
1704 goto init_error;
1706 #endif
1707 mpctx->mixer.audio_out = mpctx->audio_out;
1708 mpctx->mixer.volstep = volstep;
1709 return;
1711 init_error:
1712 uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec and possibly AO
1713 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1714 mpctx->d_audio->id = -2;
1718 ///@}
1719 // Command2Property
1722 // Return pts value corresponding to the end point of audio written to the
1723 // ao so far.
1724 static double written_audio_pts(struct MPContext *mpctx)
1726 sh_audio_t *sh_audio = mpctx->sh_audio;
1727 demux_stream_t *d_audio = mpctx->d_audio;
1728 double buffered_output;
1729 // first calculate the end pts of audio that has been output by decoder
1730 double a_pts = sh_audio->pts;
1731 if (a_pts != MP_NOPTS_VALUE)
1732 // Good, decoder supports new way of calculating audio pts.
1733 // sh_audio->pts is the timestamp of the latest input packet with
1734 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1735 // the amount of bytes the decoder has written after that timestamp.
1736 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1737 else {
1738 // Decoder doesn't support new way of calculating pts (or we're
1739 // being called before it has decoded anything with known timestamp).
1740 // Use the old method of audio pts calculation: take the timestamp
1741 // of last packet with known pts the decoder has read data from,
1742 // and add amount of bytes read after the beginning of that packet
1743 // divided by input bps. This will be inaccurate if the input/output
1744 // ratio is not constant for every audio packet or if it is constant
1745 // but not accurately known in sh_audio->i_bps.
1747 a_pts = d_audio->pts;
1748 // ds_tell_pts returns bytes read after last timestamp from
1749 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1750 // it has read but not decoded
1751 if (sh_audio->i_bps)
1752 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1753 (double)sh_audio->i_bps;
1755 // Now a_pts hopefully holds the pts for end of audio from decoder.
1756 // Substract data in buffers between decoder and audio out.
1758 // Decoded but not filtered
1759 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1761 // Data buffered in audio filters, measured in bytes of "missing" output
1762 buffered_output = af_calc_delay(sh_audio->afilter);
1764 // Data that was ready for ao but was buffered because ao didn't fully
1765 // accept everything to internal buffers yet
1766 buffered_output += sh_audio->a_out_buffer_len;
1768 // Filters divide audio length by playback_speed, so multiply by it
1769 // to get the length in original units without speedup or slowdown
1770 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1772 return a_pts + mpctx->video_offset;
1775 // Return pts value corresponding to currently playing audio.
1776 double playing_audio_pts(struct MPContext *mpctx)
1778 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1779 mpctx->audio_out->get_delay();
1782 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1783 struct MPOpts *opts = &mpctx->opts;
1784 // check for frame-drop:
1785 current_module = "check_framedrop";
1786 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1787 static int dropped_frames;
1788 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1789 float d = delay-mpctx->delay;
1790 ++total_frame_cnt;
1791 // we should avoid dropping too many frames in sequence unless we
1792 // are too late. and we allow 100ms A-V delay here:
1793 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1794 && !mpctx->update_video_immediately) {
1795 ++drop_frame_cnt;
1796 ++dropped_frames;
1797 return frame_dropping;
1798 } else
1799 dropped_frames = 0;
1801 return 0;
1805 #ifdef HAVE_RTC
1806 int rtc_fd = -1;
1807 #endif
1809 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1811 #ifdef HAVE_RTC
1812 if (rtc_fd >= 0){
1813 // -------- RTC -----------
1814 current_module="sleep_rtc";
1815 while (time_frame > 0.000) {
1816 unsigned long rtc_ts;
1817 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1818 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC read error: %s\n", strerror(errno));
1819 time_frame -= get_relative_time(mpctx);
1821 } else
1822 #endif
1824 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1825 // unnecessarily high CPU usage
1826 float margin = softsleep ? 0.011 : 0;
1827 current_module = "sleep_timer";
1828 while (time_frame > margin) {
1829 usec_sleep(1000000 * (time_frame - margin));
1830 time_frame -= get_relative_time(mpctx);
1832 if (softsleep){
1833 current_module = "sleep_soft";
1834 if (time_frame < 0)
1835 mp_tmsg(MSGT_AVSYNC, MSGL_WARN, "Warning! Softsleep underflow!\n");
1836 while (time_frame > 0)
1837 time_frame -= get_relative_time(mpctx); // burn the CPU
1840 return time_frame;
1843 static void select_subtitle(MPContext *mpctx)
1845 struct MPOpts *opts = &mpctx->opts;
1846 // find the best sub to use
1847 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
1848 mpctx->global_sub_pos = -1; // no subs by default
1849 if (vobsub_index_id >= 0) {
1850 // if user asks for a vobsub id, use that first.
1851 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
1852 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1853 // if user asks for a dvd sub id, use that next.
1854 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1855 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
1856 // if there are text subs to use, use those. (autosubs come last here)
1857 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
1858 } else if (opts->sub_id == -1 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1859 // finally select subs by language and container hints
1860 if (opts->sub_id == -1 && dvdsub_lang)
1861 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
1862 if (opts->sub_id == -1)
1863 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
1864 if (opts->sub_id >= 0)
1865 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1867 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
1868 mpctx->global_sub_pos--;
1869 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
1872 #ifdef CONFIG_DVDNAV
1873 #ifndef FF_B_TYPE
1874 #define FF_B_TYPE 3
1875 #endif
1876 /// store decoded video image
1877 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1878 mp_image_t *from_mpi) {
1879 mp_image_t *mpi;
1881 /// Do not store B-frames
1882 if (from_mpi->pict_type == FF_B_TYPE)
1883 return to_mpi;
1885 if (to_mpi &&
1886 to_mpi->w == from_mpi->w &&
1887 to_mpi->h == from_mpi->h &&
1888 to_mpi->imgfmt == from_mpi->imgfmt)
1889 mpi = to_mpi;
1890 else {
1891 if (to_mpi)
1892 free_mp_image(to_mpi);
1893 if (from_mpi->w == 0 || from_mpi->h == 0)
1894 return NULL;
1895 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1898 copy_mpi(mpi,from_mpi);
1899 return mpi;
1902 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1903 struct MPOpts *opts = &ctx->opts;
1904 if (ctx->sh_video) {
1905 /// clear video pts
1906 ctx->d_video->pts = 0.0f;
1907 ctx->sh_video->pts = 0.0f;
1908 ctx->sh_video->i_pts = 0.0f;
1909 ctx->sh_video->last_pts = 0.0f;
1910 ctx->sh_video->num_buffered_pts = 0;
1911 ctx->sh_video->num_frames = 0;
1912 ctx->sh_video->num_frames_decoded = 0;
1913 ctx->sh_video->timer = 0.0f;
1914 ctx->sh_video->stream_delay = 0.0f;
1915 ctx->sh_video->timer = 0;
1916 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1919 if (ctx->sh_audio) {
1920 /// free audio packets and reset
1921 ds_free_packs(ctx->d_audio);
1922 audio_delay -= ctx->sh_audio->stream_delay;
1923 ctx->delay =- audio_delay;
1924 ctx->audio_out->reset();
1925 resync_audio_stream(ctx->sh_audio);
1928 audio_delay = 0.0f;
1929 ctx->global_sub_size = ctx->global_sub_indices[SUB_SOURCE_DEMUX] + mp_dvdnav_number_of_subs(ctx->stream);
1930 if (dvdsub_lang && opts->sub_id == dvdsub_lang_id) {
1931 dvdsub_lang_id = mp_dvdnav_sid_from_lang(ctx->stream, dvdsub_lang);
1932 if (dvdsub_lang_id != opts->sub_id) {
1933 opts->sub_id = dvdsub_lang_id;
1934 select_subtitle(ctx);
1938 /// clear all EOF related flags
1939 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1942 /// Restore last decoded DVDNAV (still frame)
1943 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1944 int *in_size,
1945 unsigned char **start,
1946 mp_image_t *decoded_frame)
1948 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1949 return decoded_frame;
1951 /// a change occured in dvdnav stream
1952 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1953 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1954 mp_dvdnav_context_free(mpctx);
1955 mp_dvdnav_reset_stream(mpctx);
1956 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1957 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1960 if (*in_size < 0) {
1961 float len;
1963 /// Display still frame, if any
1964 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1965 decoded_frame = mpctx->nav_smpi;
1967 /// increment video frame : continue playing after still frame
1968 len = demuxer_get_time_length(mpctx->demuxer);
1969 if (mpctx->sh_video->pts >= len &&
1970 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1971 mp_dvdnav_skip_still(mpctx->stream);
1972 mp_dvdnav_skip_wait(mpctx->stream);
1974 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1976 if (mpctx->nav_buffer) {
1977 *start = mpctx->nav_start;
1978 *in_size = mpctx->nav_in_size;
1979 if (mpctx->nav_start)
1980 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1984 return decoded_frame;
1987 /// Save last decoded DVDNAV (still frame)
1988 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1989 unsigned char *start,
1990 mp_image_t *decoded_frame)
1992 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1993 return;
1995 if (mpctx->nav_buffer)
1996 free(mpctx->nav_buffer);
1998 mpctx->nav_buffer = malloc(in_size);
1999 mpctx->nav_start = start;
2000 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
2001 if (mpctx->nav_buffer)
2002 memcpy(mpctx->nav_buffer,start,in_size);
2004 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
2005 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
2007 #endif /* CONFIG_DVDNAV */
2009 /* Modify video timing to match the audio timeline. There are two main
2010 * reasons this is needed. First, video and audio can start from different
2011 * positions at beginning of file or after a seek (MPlayer starts both
2012 * immediately even if they have different pts). Second, the file can have
2013 * audio timestamps that are inconsistent with the duration of the audio
2014 * packets, for example two consecutive timestamp values differing by
2015 * one second but only a packet with enough samples for half a second
2016 * of playback between them.
2018 static void adjust_sync(struct MPContext *mpctx, double frame_time)
2020 current_module = "av_sync";
2022 if (!mpctx->sh_audio)
2023 return;
2025 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
2026 double v_pts = mpctx->sh_video->pts;
2027 double av_delay = a_pts - v_pts;
2028 // Try to sync vo_flip() so it will *finish* at given time
2029 av_delay += mpctx->last_vo_flip_duration;
2030 av_delay -= audio_delay; // This much pts difference is desired
2032 double change = av_delay * 0.1;
2033 double max_change = default_max_pts_correction >= 0 ?
2034 default_max_pts_correction : frame_time * 0.1;
2035 if (change < -max_change)
2036 change = -max_change;
2037 else if (change > max_change)
2038 change = max_change;
2039 mpctx->delay += change;
2040 mpctx->total_avsync_change += change;
2043 static int fill_audio_out_buffers(struct MPContext *mpctx)
2045 struct MPOpts *opts = &mpctx->opts;
2046 unsigned int t;
2047 double tt;
2048 int playsize;
2049 int playflags=0;
2050 int audio_eof=0;
2051 sh_audio_t * const sh_audio = mpctx->sh_audio;
2053 current_module="play_audio";
2055 while (1) {
2056 int sleep_time;
2057 // all the current uses of ao_data.pts seem to be in aos that handle
2058 // sync completely wrong; there should be no need to use ao_data.pts
2059 // in get_space()
2060 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2061 playsize = mpctx->audio_out->get_space();
2062 if (mpctx->sh_video || playsize >= ao_data.outburst)
2063 break;
2065 // handle audio-only case:
2066 // this is where mplayer sleeps during audio-only playback
2067 // to avoid 100% CPU use
2068 sleep_time = (ao_data.outburst - playsize) * 1000 / ao_data.bps;
2069 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
2070 usec_sleep(sleep_time * 1000);
2073 // Fill buffer if needed:
2074 current_module="decode_audio";
2075 t = GetTimer();
2076 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2077 if (mpctx->d_audio->eof) {
2078 audio_eof = 1;
2079 if (sh_audio->a_out_buffer_len == 0)
2080 return 0;
2082 t = GetTimer() - t;
2083 tt = t*0.000001f; audio_time_usage+=tt;
2084 if (playsize > sh_audio->a_out_buffer_len) {
2085 playsize = sh_audio->a_out_buffer_len;
2086 if (audio_eof)
2087 playflags |= AOPLAY_FINAL_CHUNK;
2089 if (!playsize)
2090 return 1;
2092 // play audio:
2093 current_module="play_audio";
2095 // Is this pts value actually useful for the aos that access it?
2096 // They're obviously badly broken in the way they handle av sync;
2097 // would not having access to this make them more broken?
2098 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2099 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2101 if (playsize > 0) {
2102 sh_audio->a_out_buffer_len -= playsize;
2103 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2104 sh_audio->a_out_buffer_len);
2105 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2107 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2108 // Sanity check to avoid hanging in case current ao doesn't output
2109 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2110 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2111 sh_audio->a_out_buffer_len = 0;
2114 return 1;
2117 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2118 float *aq_sleep_time)
2120 struct MPOpts *opts = &mpctx->opts;
2121 int frame_time_remaining = 0;
2122 current_module="calc_sleep_time";
2124 *time_frame -= get_relative_time(mpctx); // reset timer
2126 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2127 float delay = mpctx->audio_out->get_delay();
2128 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2130 if (autosync) {
2132 * Adjust this raw delay value by calculating the expected
2133 * delay for this frame and generating a new value which is
2134 * weighted between the two. The higher autosync is, the
2135 * closer to the delay value gets to that which "-nosound"
2136 * would have used, and the longer it will take for A/V
2137 * sync to settle at the right value (but it eventually will.)
2138 * This settling time is very short for values below 100.
2140 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2141 float difference = delay - predicted;
2142 delay = predicted + difference / (float)autosync;
2145 *time_frame = delay - mpctx->delay / opts->playback_speed;
2147 // delay = amount of audio buffered in soundcard/driver
2148 if (delay > 0.25) delay=0.25; else
2149 if (delay < 0.10) delay=0.10;
2150 if (*time_frame > delay*0.6) {
2151 // sleep time too big - may cause audio drops (buffer underrun)
2152 frame_time_remaining = 1;
2153 *time_frame = delay*0.5;
2155 } else {
2156 // If we're lagging more than 200 ms behind the right playback rate,
2157 // don't try to "catch up".
2158 // If benchmark is set always output frames as fast as possible
2159 // without sleeping.
2160 if (*time_frame < -0.2 || benchmark)
2161 *time_frame = 0;
2164 *aq_sleep_time += *time_frame;
2167 //============================== SLEEP: ===================================
2169 *time_frame -= mpctx->video_out->flip_queue_offset;
2170 // flag 256 means: libvo driver does its timing (dvb card)
2171 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2172 *time_frame = timing_sleep(mpctx, *time_frame);
2173 *time_frame += mpctx->video_out->flip_queue_offset;
2174 return frame_time_remaining;
2177 int reinit_video_chain(struct MPContext *mpctx)
2179 struct MPOpts *opts = &mpctx->opts;
2180 sh_video_t * const sh_video = mpctx->sh_video;
2181 double ar=-1.0;
2182 //================== Init VIDEO (codec & libvo) ==========================
2183 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2184 current_module="preinit_libvo";
2186 //shouldn't we set dvideo->id=-2 when we fail?
2187 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2188 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2189 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Error opening/initializing the selected video_out (-vo) device.\n");
2190 goto err_out;
2192 mpctx->initialized_flags|=INITIALIZED_VO;
2195 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2196 mpctx->sh_video->stream_aspect = ar;
2197 current_module="init_video_filters";
2199 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2200 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2202 #ifdef CONFIG_MENU
2203 if(use_menu) {
2204 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2205 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2206 if(!vf_menu) {
2207 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Can't open libmenu video filter with root menu %s.\n",menu_root);
2208 use_menu = 0;
2211 if(vf_menu)
2212 sh_video->vfilter = vf_menu;
2213 #endif
2215 #ifdef CONFIG_ASS
2216 if(opts->ass_enabled) {
2217 int i;
2218 int insert = 1;
2219 if (opts->vf_settings)
2220 for (i = 0; opts->vf_settings[i].name; ++i)
2221 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2222 insert = 0;
2223 break;
2225 if (insert) {
2226 extern vf_info_t vf_info_ass;
2227 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2228 char* vf_arg[] = {"auto", "1", NULL};
2229 int retcode = 0;
2230 struct vf_instance *vf_ass = vf_open_plugin_noerr(opts, libass_vfs,
2231 sh_video->vfilter,
2232 "ass", vf_arg,
2233 &retcode);
2234 if (vf_ass)
2235 sh_video->vfilter = vf_ass;
2236 else if (retcode == -1) // vf_ass open() returns -1 if there's VO EOSD
2237 mp_msg(MSGT_CPLAYER, MSGL_V, "[ass] vf_ass not needed\n");
2238 else
2239 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2242 #endif
2244 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2246 #ifdef CONFIG_ASS
2247 if (opts->ass_enabled)
2248 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2249 #endif
2251 current_module="init_video_codec";
2253 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2254 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2255 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2257 if(!sh_video->initialized){
2258 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2259 goto err_out;
2262 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2264 if (sh_video->codec)
2265 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2267 sh_video->last_pts = MP_NOPTS_VALUE;
2268 sh_video->num_buffered_pts = 0;
2269 sh_video->next_frame_time = 0;
2271 if(auto_quality>0){
2272 // Auto quality option enabled
2273 output_quality=get_video_quality_max(sh_video);
2274 if(auto_quality>output_quality) auto_quality=output_quality;
2275 else output_quality=auto_quality;
2276 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2277 set_video_quality(sh_video,output_quality);
2280 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2282 current_module="init_vo";
2284 return 1;
2286 err_out:
2287 mpctx->sh_video = mpctx->d_video->sh = NULL;
2288 return 0;
2291 static double update_video_nocorrect_pts(struct MPContext *mpctx)
2293 struct sh_video *sh_video = mpctx->sh_video;
2294 double frame_time = 0;
2295 struct vo *video_out = mpctx->video_out;
2296 while (!video_out->frame_loaded) {
2297 current_module = "filter_video";
2298 // In nocorrect-pts mode there is no way to properly time these frames
2299 if (vo_get_buffered_frame(video_out, 0) >= 0)
2300 break;
2301 if (vf_output_queued_frame(sh_video->vfilter))
2302 continue;
2303 unsigned char *packet = NULL;
2304 frame_time = sh_video->next_frame_time;
2305 if (mpctx->update_video_immediately)
2306 frame_time = 0;
2307 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2308 &packet, force_fps);
2309 if (in_size < 0) {
2310 #ifdef CONFIG_DVDNAV
2311 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2312 if (mp_dvdnav_is_eof(mpctx->stream))
2313 return -1;
2314 if (mpctx->d_video)
2315 mpctx->d_video->eof = 0;
2316 if (mpctx->d_audio)
2317 mpctx->d_audio->eof = 0;
2318 mpctx->stream->eof = 0;
2319 } else
2320 #endif
2321 return -1;
2323 if (in_size > max_framesize)
2324 max_framesize = in_size;
2325 sh_video->timer += frame_time;
2326 if (mpctx->sh_audio)
2327 mpctx->delay -= frame_time;
2328 // video_read_frame can change fps (e.g. for ASF video)
2329 vo_fps = sh_video->fps;
2330 int framedrop_type = check_framedrop(mpctx, frame_time);
2331 current_module = "decode video";
2333 void *decoded_frame;
2334 #ifdef CONFIG_DVDNAV
2335 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2336 if (in_size >= 0 && !decoded_frame)
2337 #endif
2338 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2339 sh_video->pts);
2340 #ifdef CONFIG_DVDNAV
2341 // Save last still frame for future display
2342 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2343 #endif
2344 if (decoded_frame) {
2345 current_module = "filter video";
2346 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2347 if (!video_out->config_ok)
2348 break;
2351 return frame_time;
2354 static void determine_frame_pts(struct MPContext *mpctx)
2356 struct sh_video *sh_video = mpctx->sh_video;
2357 struct MPOpts *opts = &mpctx->opts;
2359 if (opts->user_pts_assoc_mode) {
2360 sh_video->pts_assoc_mode = opts->user_pts_assoc_mode;
2361 } else if (sh_video->pts_assoc_mode == 0) {
2362 if (sh_video->codec_reordered_pts != MP_NOPTS_VALUE)
2363 sh_video->pts_assoc_mode = 1;
2364 else
2365 sh_video->pts_assoc_mode = 2;
2366 } else {
2367 int probcount1 = sh_video->num_reordered_pts_problems;
2368 int probcount2 = sh_video->num_sorted_pts_problems;
2369 if (sh_video->pts_assoc_mode == 2) {
2370 int tmp = probcount1;
2371 probcount1 = probcount2;
2372 probcount2 = tmp;
2374 if (probcount1 >= probcount2 * 1.5 + 2) {
2375 sh_video->pts_assoc_mode = 3 - sh_video->pts_assoc_mode;
2376 mp_msg(MSGT_CPLAYER, MSGL_V, "Switching to pts association mode "
2377 "%d.\n", sh_video->pts_assoc_mode);
2380 sh_video->pts = sh_video->pts_assoc_mode == 1 ?
2381 sh_video->codec_reordered_pts : sh_video->sorted_pts;
2384 static double update_video(struct MPContext *mpctx)
2386 struct sh_video *sh_video = mpctx->sh_video;
2387 struct vo *video_out = mpctx->video_out;
2388 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2389 mpctx->osd); // hack for vf_expand
2390 if (!mpctx->opts.correct_pts)
2391 return update_video_nocorrect_pts(mpctx);
2393 double pts;
2395 bool hit_eof = false;
2396 while (!video_out->frame_loaded) {
2397 current_module = "filter_video";
2398 if (vo_get_buffered_frame(video_out, hit_eof) >= 0)
2399 break;
2400 // XXX Time used in this call is not counted in any performance
2401 // timer now, OSD time is not updated correctly for filter-added frames
2402 if (vf_output_queued_frame(sh_video->vfilter))
2403 continue;
2404 if (hit_eof)
2405 return -1;
2406 unsigned char *packet = NULL;
2407 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2408 if (pts != MP_NOPTS_VALUE)
2409 pts += mpctx->video_offset;
2410 if (in_size < 0) {
2411 // try to extract last frames in case of decoder lag
2412 in_size = 0;
2413 pts = 1e300;
2414 hit_eof = true;
2416 if (in_size > max_framesize)
2417 max_framesize = in_size;
2418 current_module = "decode video";
2419 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2420 void *decoded_frame = decode_video(sh_video, packet, in_size,
2421 framedrop_type, pts);
2422 if (decoded_frame) {
2423 determine_frame_pts(mpctx);
2424 current_module = "filter video";
2425 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2426 if (!video_out->config_ok)
2427 break; // We'd likely hang in this loop otherwise
2431 pts = video_out->next_pts;
2432 if (pts == MP_NOPTS_VALUE) {
2433 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2434 // Try to use decoder pts from before filters
2435 pts = sh_video->pts;
2436 if (pts == MP_NOPTS_VALUE)
2437 pts = sh_video->last_pts;
2439 sh_video->pts = pts;
2440 if (sh_video->last_pts == MP_NOPTS_VALUE)
2441 sh_video->last_pts = sh_video->pts;
2442 else if (sh_video->last_pts > sh_video->pts) {
2443 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2444 sh_video->pts, sh_video->last_pts);
2445 /* If the difference in pts is small treat it as jitter around the
2446 * right value (possibly caused by incorrect timestamp ordering) and
2447 * just show this frame immediately after the last one.
2448 * Treat bigger differences as timestamp resets and start counting
2449 * timing of later frames from the position of this one. */
2450 if (sh_video->last_pts - sh_video->pts > 0.5)
2451 sh_video->last_pts = sh_video->pts;
2452 else
2453 sh_video->pts = sh_video->last_pts;
2455 double frame_time = sh_video->pts - sh_video->last_pts;
2456 sh_video->last_pts = sh_video->pts;
2457 sh_video->timer += frame_time;
2458 if (mpctx->sh_audio)
2459 mpctx->delay -= frame_time;
2460 return frame_time;
2463 void pause_player(struct MPContext *mpctx)
2465 if (mpctx->paused)
2466 return;
2467 mpctx->paused = 1;
2468 mpctx->step_frames = 0;
2469 mpctx->time_frame -= get_relative_time(mpctx);
2471 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2472 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2474 if (mpctx->audio_out && mpctx->sh_audio)
2475 mpctx->audio_out->pause(); // pause audio, keep data if possible
2478 void unpause_player(struct MPContext *mpctx)
2480 if (!mpctx->paused)
2481 return;
2482 mpctx->paused = 0;
2484 if (mpctx->audio_out && mpctx->sh_audio)
2485 mpctx->audio_out->resume(); // resume audio
2486 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2487 && !mpctx->step_frames)
2488 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2489 (void)get_relative_time(mpctx); // ignore time that passed during pause
2492 void add_step_frame(struct MPContext *mpctx)
2494 mpctx->step_frames++;
2495 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2496 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2497 unpause_player(mpctx);
2500 static void pause_loop(struct MPContext *mpctx)
2502 mp_cmd_t* cmd;
2503 if (!quiet) {
2504 // Small hack to display the pause message on the OSD line.
2505 // The pause string is: "\n == PAUSE == \r" so we need to
2506 // take the first and the last char out
2507 if (term_osd && !mpctx->sh_video) {
2508 char msg[128] = _("\n ===== PAUSE =====\r");
2509 int mlen = strlen(msg);
2510 msg[mlen-1] = '\0';
2511 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2512 update_osd_msg(mpctx);
2513 } else
2514 mp_tmsg(MSGT_CPLAYER,MSGL_STATUS,"\n ===== PAUSE =====\r");
2515 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2518 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2519 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2520 if (cmd) {
2521 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2522 run_command(mpctx, cmd);
2523 mp_cmd_free(cmd);
2524 continue;
2526 if (mpctx->sh_video && mpctx->video_out)
2527 vo_check_events(mpctx->video_out);
2528 #ifdef CONFIG_MENU
2529 if (vf_menu)
2530 vf_menu_pause_update(vf_menu);
2531 #endif
2532 usec_sleep(20000);
2533 update_osd_msg(mpctx);
2534 int hack = vo_osd_changed(0);
2535 vo_osd_changed(hack);
2536 if (hack)
2537 break;
2542 // Find the right mute status and record position for new file position
2543 static void edl_seek_reset(MPContext *mpctx)
2545 mpctx->edl_muted = 0;
2546 next_edl_record = edl_records;
2548 while (next_edl_record) {
2549 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2550 break;
2552 if (next_edl_record->action == EDL_MUTE)
2553 mpctx->edl_muted = !mpctx->edl_muted;
2554 next_edl_record = next_edl_record->next;
2556 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2557 mixer_mute(&mpctx->mixer);
2561 // Execute EDL command for the current position if one exists
2562 static void edl_update(MPContext *mpctx)
2564 if (!next_edl_record)
2565 return;
2567 if (!mpctx->sh_video) {
2568 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot use EDL without video, disabling.\n");
2569 free_edl(edl_records);
2570 next_edl_record = NULL;
2571 edl_records = NULL;
2572 return;
2575 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2576 if (next_edl_record->action == EDL_SKIP) {
2577 mpctx->osd_function = OSD_FFW;
2578 mpctx->abs_seek_pos = 0;
2579 mpctx->rel_seek_secs = next_edl_record->length_sec;
2580 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2581 "[%f], length [%f]\n", next_edl_record->start_sec,
2582 next_edl_record->stop_sec, next_edl_record->length_sec);
2584 else if (next_edl_record->action == EDL_MUTE) {
2585 mpctx->edl_muted = !mpctx->edl_muted;
2586 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2587 mixer_mute(&mpctx->mixer);
2588 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2589 next_edl_record->start_sec );
2591 next_edl_record = next_edl_record->next;
2595 static void reinit_decoders(struct MPContext *mpctx)
2597 reinit_video_chain(mpctx);
2598 reinit_audio_chain(mpctx);
2599 mp_property_do("sub", M_PROPERTY_SET, &mpctx->global_sub_pos, mpctx);
2602 static bool timeline_set_part(struct MPContext *mpctx, int i)
2604 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
2605 struct timeline_part *n = mpctx->timeline + i;
2606 mpctx->timeline_part = i;
2607 mpctx->video_offset = n->start - n->source_start;
2608 if (n->source == p->source)
2609 return false;
2610 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo && mpctx->opts.video_id != -2 ? 0 : INITIALIZED_VO) | INITIALIZED_AO | INITIALIZED_ACODEC);
2611 mpctx->demuxer = n->source->demuxer;
2612 mpctx->d_video = mpctx->demuxer->video;
2613 mpctx->d_audio = mpctx->demuxer->audio;
2614 mpctx->d_sub = mpctx->demuxer->sub;
2615 mpctx->sh_video = mpctx->d_video->sh;
2616 mpctx->sh_audio = mpctx->d_audio->sh;
2617 return true;
2620 // Given pts, switch playback to the corresponding part.
2621 // Return offset within that part.
2622 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
2623 bool *need_reset)
2625 if (pts < 0)
2626 pts = 0;
2627 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
2628 struct timeline_part *p = mpctx->timeline + i;
2629 if (pts < (p+1)->start) {
2630 *need_reset = timeline_set_part(mpctx, i);
2631 return pts - p->start + p->source_start;
2634 return -1;
2638 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2639 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2640 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2641 static int seek(MPContext *mpctx, double amount, int style)
2643 current_module = "seek";
2644 if (mpctx->stop_play == AT_END_OF_FILE)
2645 mpctx->stop_play = KEEP_PLAYING;
2646 if (mpctx->timeline && style & SEEK_FACTOR) {
2647 amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
2648 style &= ~SEEK_FACTOR;
2650 if ((mpctx->demuxer->accurate_seek || mpctx->timeline) && mpctx->sh_video
2651 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2652 style |= SEEK_ABSOLUTE;
2653 if (amount > 0)
2654 style |= SEEK_FORWARD;
2655 else
2656 style |= SEEK_BACKWARD;
2657 amount += mpctx->sh_video->pts;
2660 /* At least the liba52 decoder wants to read from the input stream
2661 * during initialization, so reinit must be done after the demux_seek()
2662 * call that clears possible stream EOF. */
2663 bool need_reset = false;
2664 if (mpctx->timeline) {
2665 amount = timeline_set_from_time(mpctx, amount, &need_reset);
2666 if (amount == -1) {
2667 mpctx->stop_play = AT_END_OF_FILE;
2668 // Clear audio from current position
2669 if (mpctx->sh_audio) {
2670 mpctx->audio_out->reset();
2671 mpctx->sh_audio->a_buffer_len = 0;
2672 mpctx->sh_audio->a_out_buffer_len = 0;
2674 return -1;
2677 int seekresult = demux_seek(mpctx->demuxer, amount, audio_delay, style);
2678 if (need_reset)
2679 reinit_decoders(mpctx);
2680 if (seekresult == 0)
2681 return -1;
2683 if (mpctx->sh_video) {
2684 current_module = "seek_video_reset";
2685 resync_video_stream(mpctx->sh_video);
2686 vo_seek_reset(mpctx->video_out);
2687 mpctx->sh_video->num_buffered_pts = 0;
2688 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2689 mpctx->delay = 0;
2690 mpctx->time_frame = 0;
2691 mpctx->update_video_immediately = true;
2692 // Not all demuxers set d_video->pts during seek, so this value
2693 // (which is used by at least vobsub and edl code below) may
2694 // be completely wrong (probably 0).
2695 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
2696 update_subtitles(mpctx, &mpctx->opts, mpctx->sh_video,
2697 mpctx->sh_video->pts, mpctx->video_offset,
2698 mpctx->d_sub, 1);
2699 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2702 if (mpctx->sh_audio) {
2703 current_module = "seek_audio_reset";
2704 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2705 mpctx->sh_audio->a_buffer_len = 0;
2706 mpctx->sh_audio->a_out_buffer_len = 0;
2707 if (!mpctx->sh_video)
2708 update_subtitles(mpctx, &mpctx->opts, NULL, mpctx->sh_audio->pts,
2709 mpctx->video_offset, mpctx->d_sub, 1);
2712 if (vo_vobsub && mpctx->sh_video) {
2713 current_module = "seek_vobsub_reset";
2714 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2717 edl_seek_reset(mpctx);
2719 mpctx->total_avsync_change = 0;
2720 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2721 drop_frame_cnt = 0;
2723 current_module = NULL;
2724 return 0;
2727 int get_current_chapter(struct MPContext *mpctx)
2729 if (!mpctx->chapters || !mpctx->sh_video)
2730 return demuxer_get_current_chapter(mpctx->demuxer);
2732 int i;
2733 double current_pts = mpctx->sh_video->pts;
2734 for (i = 1; i < mpctx->num_chapters; i++)
2735 if (current_pts < mpctx->chapters[i].start)
2736 break;
2737 return i - 1;
2740 // currently returns a string allocated with malloc, not talloc
2741 char *chapter_display_name(struct MPContext *mpctx, int chapter)
2743 if (!mpctx->chapters || !mpctx->sh_video)
2744 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
2745 return strdup(mpctx->chapters[chapter].name);
2748 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
2749 char **chapter_name)
2751 if (!mpctx->chapters || !mpctx->sh_video)
2752 return demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
2753 chapter_name);
2754 if (chapter >= mpctx->num_chapters)
2755 return -1;
2756 if (chapter < 0)
2757 chapter = 0;
2758 *seek_pts = mpctx->chapters[chapter].start;
2759 if (chapter_name)
2760 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
2761 return chapter;
2764 static int find_ordered_chapter_sources(struct MPContext *mpctx,
2765 struct content_source *sources,
2766 int num_sources,
2767 unsigned char uid_map[][16])
2769 int num_filenames = 0;
2770 char **filenames = NULL;
2771 if (num_sources > 1) {
2772 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
2773 "other sources.\n");
2774 if (mpctx->stream->type != STREAMTYPE_FILE) {
2775 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
2776 "normal disk file. Will not search for related files.\n");
2777 } else {
2778 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
2779 "same directory to find referenced sources.\n");
2780 filenames = find_files(mpctx->demuxer->filename, ".mkv",
2781 &num_filenames);
2785 int num_left = num_sources - 1;
2786 for (int i = 0; i < num_filenames && num_left > 0; i++) {
2787 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
2788 filename_recode(filenames[i]));
2789 int format;
2790 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
2791 if (!s)
2792 continue;
2793 struct demuxer *d = demux_open(&mpctx->opts, s, DEMUXER_TYPE_MATROSKA,
2794 mpctx->opts.audio_id,
2795 mpctx->opts.video_id,
2796 mpctx->opts.sub_id, filenames[i]);
2797 if (!d) {
2798 free_stream(s);
2799 continue;
2801 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
2802 for (int i = 1; i < num_sources; i++) {
2803 if (sources[i].demuxer)
2804 continue;
2805 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
2806 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
2807 i, filename_recode(d->filename));
2808 sources[i].stream = s;
2809 sources[i].demuxer = d;
2810 num_left--;
2811 goto match;
2815 free_demuxer(d);
2816 free_stream(s);
2817 continue;
2818 match:
2821 talloc_free(filenames);
2822 if (num_left) {
2823 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
2824 "There will be parts MISSING from the video!\n");
2825 for (int i = 1, j = 1; i < num_sources; i++)
2826 if (sources[i].demuxer) {
2827 sources[j] = sources[i];
2828 memcpy(uid_map[j], uid_map[i], 16);
2829 j++;
2832 return num_sources - num_left;
2835 static void build_ordered_chapter_timeline(struct MPContext *mpctx)
2837 if (!mpctx->opts.ordered_chapters) {
2838 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
2839 "you have disabled support for them. Ignoring.\n");
2840 return;
2843 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
2844 "edit timeline.\n");
2846 struct demuxer *demuxer = mpctx->demuxer;
2847 struct matroska_data *m = &demuxer->matroska_data;
2849 // +1 because sources/uid_map[0] is original file even if all chapters
2850 // actually use other sources and need separate entries
2851 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
2852 m->num_ordered_chapters+1);
2853 sources[0].stream = mpctx->stream;
2854 sources[0].demuxer = mpctx->demuxer;
2855 unsigned char uid_map[m->num_ordered_chapters+1][16];
2856 int num_sources = 1;
2857 memcpy(uid_map[0], m->segment_uid, 16);
2859 for (int i = 0; i < m->num_ordered_chapters; i++) {
2860 struct matroska_chapter *c = m->ordered_chapters + i;
2861 if (!c->has_segment_uid)
2862 memcpy(c->segment_uid, m->segment_uid, 16);
2864 for (int j = 0; j < num_sources; j++)
2865 if (!memcmp(c->segment_uid, uid_map[j], 16))
2866 goto found1;
2867 memcpy(uid_map[num_sources], c->segment_uid, 16);
2868 sources[num_sources] = (struct content_source){};
2869 num_sources++;
2870 found1:
2874 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
2875 uid_map);
2878 // +1 for terminating chapter with start time marking end of last real one
2879 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
2880 m->num_ordered_chapters + 1);
2881 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
2882 m->num_ordered_chapters);
2883 uint64_t starttime = 0;
2884 uint64_t missing_time = 0;
2885 int part_count = 0;
2886 int num_chapters = 0;
2887 uint64_t prev_part_offset;
2888 for (int i = 0; i < m->num_ordered_chapters; i++) {
2889 struct matroska_chapter *c = m->ordered_chapters + i;
2891 int j;
2892 for (j = 0; j < num_sources; j++) {
2893 if (!memcmp(c->segment_uid, uid_map[j], 16))
2894 goto found2;
2896 missing_time += c->end - c->start;
2897 continue;
2898 found2:;
2899 chapters[num_chapters].start = starttime / 1000.;
2900 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
2901 /* Only add a separate part if the time or file actually changes.
2902 * Matroska files have chapter divisions that are redundant from
2903 * timeline point of view because the same chapter structure is used
2904 * both to specify the timeline and for normal chapter information.
2905 * Removing a missing inserted external chapter can also cause this. */
2906 if (part_count == 0 || c->start != starttime + prev_part_offset
2907 || sources + j != timeline[part_count - 1].source) {
2908 timeline[part_count].source = sources + j;
2909 timeline[part_count].start = chapters[num_chapters].start;
2910 timeline[part_count].source_start = c->start / 1000.;
2911 prev_part_offset = c->start - starttime;
2912 part_count++;
2914 starttime += c->end - c->start;
2915 num_chapters++;
2917 timeline[part_count].start = starttime / 1000.;
2919 if (!part_count) {
2920 // None of the parts come from the file itself???
2921 talloc_free(sources);
2922 talloc_free(timeline);
2923 talloc_free(chapters);
2924 return;
2927 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
2928 "sources. Total length %.3f seconds.\n", part_count, num_sources,
2929 timeline[part_count].start);
2930 if (missing_time)
2931 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
2932 "from the timeline!\n", missing_time / 1000.);
2933 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
2934 for (int i = 0; i < num_sources; i++)
2935 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
2936 filename_recode(sources[i].demuxer->filename));
2937 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
2938 "source_start, source):\n");
2939 for (int i = 0; i < part_count; i++) {
2940 struct timeline_part *p = timeline + i;
2941 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
2942 p->source_start, p->source - sources);
2944 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start);
2945 mpctx->sources = sources;
2946 mpctx->num_sources = num_sources;
2947 mpctx->timeline = timeline;
2948 mpctx->num_timeline_parts = part_count;
2949 mpctx->num_chapters = num_chapters;
2950 mpctx->chapters = chapters;
2952 mpctx->timeline_part = 0;
2953 mpctx->demuxer = timeline[0].source->demuxer;
2957 static int read_keys(void *ctx, int fd)
2959 getch2(ctx);
2960 return mplayer_get_key(ctx, 0);
2964 #ifdef PTW32_STATIC_LIB
2965 static void detach_ptw32(void)
2967 pthread_win32_thread_detach_np();
2968 pthread_win32_process_detach_np();
2970 #endif
2972 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2973 * file for some tools to link against. */
2974 #ifndef DISABLE_MAIN
2975 int main(int argc,char* argv[]){
2978 char * mem_ptr;
2980 // movie info:
2982 /* Flag indicating whether MPlayer should exit without playing anything. */
2983 int opt_exit = 0;
2985 //float a_frame=0; // Audio
2987 int i;
2989 struct MPContext *mpctx = &(struct MPContext){
2990 .osd_function = OSD_PLAY,
2991 .begin_skip = MP_NOPTS_VALUE,
2992 .play_tree_step = 1,
2993 .global_sub_pos = -1,
2994 .set_of_sub_pos = -1,
2995 .file_format = DEMUXER_TYPE_UNKNOWN,
2996 .last_dvb_step = 1,
2999 #ifdef PTW32_STATIC_LIB
3000 pthread_win32_process_attach_np();
3001 pthread_win32_thread_attach_np();
3002 atexit(detach_ptw32);
3003 #endif
3005 InitTimer();
3006 srand(GetTimerMS());
3008 mp_msg_init();
3009 set_av_log_callback();
3011 #ifdef CONFIG_X11
3012 mpctx->x11_state = vo_x11_init_state();
3013 #endif
3014 struct MPOpts *opts = &mpctx->opts;
3015 set_default_mplayer_options(opts);
3016 // Create the config context and register the options
3017 mpctx->mconfig = m_config_new(opts, cfg_include);
3018 m_config_register_options(mpctx->mconfig,mplayer_opts);
3019 mp_input_register_options(mpctx->mconfig);
3021 // Preparse the command line
3022 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
3024 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
3025 set_path_env();
3026 #endif
3028 #ifdef CONFIG_TV
3029 stream_tv_defaults.immediate = 1;
3030 #endif
3032 parse_cfgfiles(mpctx, mpctx->mconfig);
3034 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
3035 if(mpctx->playtree == NULL)
3036 opt_exit = 1;
3037 else {
3038 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
3039 if(mpctx->playtree) {
3040 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3041 if(mpctx->playtree_iter) {
3042 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3043 play_tree_iter_free(mpctx->playtree_iter);
3044 mpctx->playtree_iter = NULL;
3046 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3050 mpctx->key_fifo = mp_fifo_create(opts);
3052 print_version("MPlayer");
3054 #if defined(__MINGW32__) || defined(__CYGWIN__)
3056 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
3057 BOOL WINAPI (*setDEP)(DWORD) = NULL;
3058 if (kernel32)
3059 setDEP = GetProcAddress(kernel32, "SetProcessDEPPolicy");
3060 if (setDEP) setDEP(3);
3062 // stop Windows from showing all kinds of annoying error dialogs
3063 SetErrorMode(0x8003);
3064 // request 1ms timer resolution
3065 timeBeginPeriod(1);
3066 #endif
3068 #ifdef CONFIG_PRIORITY
3069 set_priority();
3070 #endif
3072 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
3073 list_video_out();
3074 opt_exit = 1;
3077 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
3078 list_audio_out();
3079 opt_exit = 1;
3082 /* Check codecs.conf. */
3083 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3084 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3085 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3086 if(!parse_codec_cfg(NULL)){
3087 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3089 mp_tmsg(MSGT_CPLAYER,MSGL_V,"Using built-in default codecs.conf.\n");
3092 free( mem_ptr ); // release the buffer created by get_path()
3095 #if 0
3096 if(video_codec_list){
3097 int i;
3098 video_codec=video_codec_list[0];
3099 for(i=0;video_codec_list[i];i++)
3100 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
3102 #endif
3103 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3104 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available audio codecs:\n");
3105 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3106 list_codecs(1);
3107 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3108 opt_exit = 1;
3110 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3111 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video codecs:\n");
3112 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3113 list_codecs(0);
3114 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3115 opt_exit = 1;
3117 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3118 vfm_help();
3119 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3120 opt_exit = 1;
3122 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3123 afm_help();
3124 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3125 opt_exit = 1;
3127 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3128 af_help();
3129 printf("\n");
3130 opt_exit = 1;
3132 #ifdef CONFIG_X11
3133 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3134 fstype_help();
3135 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3136 opt_exit = 1;
3138 #endif
3139 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3140 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3141 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3142 demuxer_help();
3143 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3144 opt_exit = 1;
3146 if(list_properties) {
3147 property_print_help();
3148 opt_exit = 1;
3151 if(opt_exit)
3152 exit_player(mpctx, EXIT_NONE);
3154 if(!mpctx->filename && !player_idle_mode){
3155 // no file/vcd/dvd -> show HELP:
3156 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
3157 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3160 /* Display what configure line was used */
3161 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3163 // Many users forget to include command line in bugreports...
3164 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3165 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "CommandLine:");
3166 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3167 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3170 //------ load global data first ------
3172 mpctx->osd = osd_create();
3174 // check font
3175 #ifdef CONFIG_FREETYPE
3176 init_freetype();
3177 #endif
3178 #ifdef CONFIG_FONTCONFIG
3179 if(font_fontconfig <= 0)
3181 #endif
3182 #ifdef CONFIG_BITMAP_FONT
3183 if(font_name){
3184 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3185 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",
3186 filename_recode(font_name));
3187 } else {
3188 // try default:
3189 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3190 free(mem_ptr); // release the buffer created by get_path()
3191 if(!vo_font)
3192 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3194 if (sub_font_name)
3195 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3196 else
3197 mpctx->osd->sub_font = vo_font;
3198 #endif
3199 #ifdef CONFIG_FONTCONFIG
3201 #endif
3203 #ifdef CONFIG_ASS
3204 ass_library = ass_init();
3205 #endif
3207 #ifdef HAVE_RTC
3208 if(!nortc)
3210 // seteuid(0); /* Can't hurt to try to get root here */
3211 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3212 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Failed to open %s: %s (it should be readable by the user.)\n",
3213 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3214 else {
3215 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3217 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3218 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp, strerror(errno));
3219 mp_tmsg(MSGT_CPLAYER, MSGL_HINT, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp);
3220 close (rtc_fd);
3221 rtc_fd = -1;
3222 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3223 /* variable only by the root */
3224 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno));
3225 close (rtc_fd);
3226 rtc_fd = -1;
3227 } else
3228 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Using Linux hardware RTC timing (%ldHz).\n", irqp);
3231 if(rtc_fd<0)
3232 #endif /* HAVE_RTC */
3233 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3234 softsleep?"software":timer_name);
3236 #ifdef HAVE_TERMCAP
3237 load_termcap(NULL); // load key-codes
3238 #endif
3240 // ========== Init keyboard FIFO (connection to libvo) ============
3242 // Init input system
3243 current_module = "init_input";
3244 mpctx->input = mp_input_init(&opts->input);
3245 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3246 if(slave_mode)
3247 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
3248 else if(!noconsolecontrols)
3249 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
3250 // Set the libstream interrupt callback
3251 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
3253 #ifdef CONFIG_MENU
3254 if(use_menu) {
3255 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3256 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3257 else {
3258 menu_cfg = get_path("menu.conf");
3259 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3260 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3261 else {
3262 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
3263 MPLAYER_CONFDIR "/menu.conf"))
3264 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", MPLAYER_CONFDIR"/menu.conf");
3265 else {
3266 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Menu init failed.\n");
3267 use_menu = 0;
3272 #endif
3274 current_module = NULL;
3276 /// Catch signals
3277 #ifndef __MINGW32__
3278 signal(SIGCHLD,child_sighandler);
3279 #endif
3281 #ifdef CONFIG_CRASH_DEBUG
3282 prog_path = argv[0];
3283 #endif
3284 //========= Catch terminate signals: ================
3285 // terminate requests:
3286 signal(SIGTERM,exit_sighandler); // kill
3287 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3289 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3291 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3292 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3293 #ifdef CONFIG_SIGHANDLER
3294 // fatal errors:
3295 signal(SIGBUS,exit_sighandler); // bus error
3296 signal(SIGSEGV,exit_sighandler); // segfault
3297 signal(SIGILL,exit_sighandler); // illegal instruction
3298 signal(SIGFPE,exit_sighandler); // floating point exc.
3299 signal(SIGABRT,exit_sighandler); // abort()
3300 #ifdef CONFIG_CRASH_DEBUG
3301 if (crash_debug)
3302 signal(SIGTRAP,exit_sighandler);
3303 #endif
3304 #endif
3306 // ******************* Now, let's see the per-file stuff ********************
3308 play_next_file:
3310 // init global sub numbers
3311 mpctx->global_sub_size = 0;
3312 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3314 if (mpctx->filename) {
3315 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
3316 load_per_extension_config (mpctx->mconfig, mpctx->filename);
3317 load_per_file_config (mpctx->mconfig, mpctx->filename);
3320 if (opts->video_driver_list)
3321 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
3322 if (opts->audio_driver_list)
3323 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
3325 // We must enable getch2 here to be able to interrupt network connection
3326 // or cache filling
3327 if(!noconsolecontrols && !slave_mode){
3328 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
3329 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"WARNING: getch2_init called twice!\n");
3330 else
3331 getch2_enable(); // prepare stdin for hotkeys...
3332 mpctx->initialized_flags|=INITIALIZED_GETCH2;
3333 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3336 // =================== GUI idle loop (STOP state) ===========================
3337 while (player_idle_mode && !mpctx->filename) {
3338 play_tree_t * entry = NULL;
3339 mp_cmd_t * cmd;
3340 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3341 if (mpctx->video_out)
3342 vo_check_events(mpctx->video_out);
3343 usec_sleep(20000);
3345 switch (cmd->id) {
3346 case MP_CMD_LOADFILE:
3347 // prepare a tree entry with the new filename
3348 entry = play_tree_new();
3349 play_tree_add_file(entry, cmd->args[0].v.s);
3350 // The entry is added to the main playtree after the switch().
3351 break;
3352 case MP_CMD_LOADLIST:
3353 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3354 break;
3355 case MP_CMD_QUIT:
3356 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3357 break;
3358 case MP_CMD_GET_PROPERTY:
3359 case MP_CMD_SET_PROPERTY:
3360 case MP_CMD_STEP_PROPERTY:
3361 run_command(mpctx, cmd);
3362 break;
3365 mp_cmd_free(cmd);
3367 if (entry) { // user entered a command that gave a valid entry
3368 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3369 play_tree_free_list(mpctx->playtree->child, 1);
3370 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3372 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3374 play_tree_set_child(mpctx->playtree, entry);
3376 /* Make iterator start at the top the of tree. */
3377 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3378 if (!mpctx->playtree_iter) continue;
3380 // find the first real item in the tree
3381 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3382 // no items!
3383 play_tree_iter_free(mpctx->playtree_iter);
3384 mpctx->playtree_iter = NULL;
3385 continue; // wait for next command
3387 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3390 //---------------------------------------------------------------------------
3392 if (mpctx->filename) {
3393 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nPlaying %s.\n",
3394 filename_recode(mpctx->filename));
3395 if(use_filename_title && vo_wintitle == NULL)
3396 vo_wintitle = strdup(mp_basename2(mpctx->filename));
3399 if (edl_filename) {
3400 if (edl_records) free_edl(edl_records);
3401 next_edl_record = edl_records = edl_parse_file();
3403 if (edl_output_filename) {
3404 if (edl_fd) fclose(edl_fd);
3405 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3407 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Can't open EDL file [%s] for writing.\n",
3408 filename_recode(edl_output_filename));
3412 //==================== Open VOB-Sub ============================
3414 current_module="vobsub";
3415 if (vobsub_name){
3416 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3417 if(vo_vobsub==NULL)
3418 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
3419 filename_recode(vobsub_name));
3420 } else if (sub_auto && mpctx->filename){
3421 /* try to autodetect vobsub from movie filename ::atmos */
3422 char *buf = strdup(mpctx->filename), *psub;
3423 char *pdot = strrchr(buf, '.');
3424 char *pslash = strrchr(buf, '/');
3425 #if defined(__MINGW32__) || defined(__CYGWIN__)
3426 if (!pslash) pslash = strrchr(buf, '\\');
3427 #endif
3428 if (pdot && (!pslash || pdot > pslash))
3429 *pdot = '\0';
3430 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3431 /* try from ~/.mplayer/sub */
3432 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3433 char *bname;
3434 int l;
3435 bname = strrchr(buf,'/');
3436 #if defined(__MINGW32__) || defined(__CYGWIN__)
3437 if(!bname) bname = strrchr(buf,'\\');
3438 #endif
3439 if(bname) bname++;
3440 else bname = buf;
3441 l = strlen(psub) + strlen(bname) + 1;
3442 psub = realloc(psub,l);
3443 strcat(psub,bname);
3444 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3445 free(psub);
3447 free(buf);
3449 if(vo_vobsub){
3450 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3451 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3452 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3454 // setup global sub numbering
3455 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3456 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3459 //============ Open & Sync STREAM --- fork cache2 ====================
3461 mpctx->stream=NULL;
3462 mpctx->demuxer=NULL;
3463 if (mpctx->d_audio) {
3464 //free_demuxer_stream(mpctx->d_audio);
3465 mpctx->d_audio=NULL;
3467 if (mpctx->d_video) {
3468 //free_demuxer_stream(d_video);
3469 mpctx->d_video=NULL;
3471 mpctx->sh_audio=NULL;
3472 mpctx->sh_video=NULL;
3474 current_module="open_stream";
3475 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3476 if(!mpctx->stream) { // error...
3477 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3478 goto goto_next_file;
3480 mpctx->initialized_flags|=INITIALIZED_STREAM;
3482 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3483 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3484 "playlist support will not be used automatically.\n"
3485 "MPlayer's playlist code is unsafe and should only be used with "
3486 "trusted sources.\nPlayback will probably fail.\n\n");
3487 #if 0
3488 play_tree_t* entry;
3489 // Handle playlist
3490 current_module="handle_playlist";
3491 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3492 filename_recode(mpctx->filename));
3493 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3494 mpctx->eof=playtree_add_playlist(mpctx, entry);
3495 goto goto_next_file;
3496 #endif
3498 mpctx->stream->start_pos+=seek_to_byte;
3500 if(stream_dump_type==5){
3501 unsigned char buf[4096];
3502 int len;
3503 FILE *f;
3504 current_module="dumpstream";
3505 stream_reset(mpctx->stream);
3506 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3507 f=fopen(stream_dump_name,"wb");
3508 if(!f){
3509 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3510 exit_player(mpctx, EXIT_ERROR);
3512 if (dvd_chapter > 1) {
3513 int chapter = dvd_chapter - 1;
3514 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3516 while(!mpctx->stream->eof && !async_quit_request){
3517 len=stream_read(mpctx->stream,buf,4096);
3518 if(len>0) {
3519 if(fwrite(buf,len,1,f) != 1) {
3520 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3521 exit_player(mpctx, EXIT_ERROR);
3524 if(dvd_last_chapter > 0) {
3525 int chapter = -1;
3526 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3527 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3528 break;
3531 if(fclose(f)) {
3532 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3533 exit_player(mpctx, EXIT_ERROR);
3535 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3536 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3539 #ifdef CONFIG_DVDREAD
3540 if(mpctx->stream->type==STREAMTYPE_DVD){
3541 current_module="dvd lang->id";
3542 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3543 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3544 // setup global sub numbering
3545 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3546 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3547 current_module=NULL;
3549 #endif
3551 #ifdef CONFIG_DVDNAV
3552 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3553 current_module="dvdnav lang->id";
3554 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3555 dvdsub_lang_id = -3;
3556 if(dvdsub_lang && opts->sub_id==-1)
3557 dvdsub_lang_id = opts->sub_id = mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3558 // setup global sub numbering
3559 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3560 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3561 current_module=NULL;
3563 #endif
3565 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3566 goto_enable_cache:
3567 if(stream_cache_size>0){
3568 int res;
3569 current_module="enable_cache";
3570 res = stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3571 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3572 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0));
3573 if(res == 0)
3574 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3577 //============ Open DEMUXERS --- DETECT file type =======================
3578 current_module="demux_open";
3580 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3582 // HACK to get MOV Reference Files working
3584 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3586 unsigned char* playlist_entry;
3587 play_tree_t *list = NULL, *entry = NULL;
3589 current_module="handle_demux_playlist";
3590 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3592 char *temp, *bname;
3594 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3595 filename_recode(playlist_entry));
3597 bname=mp_basename(playlist_entry);
3598 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3599 continue;
3601 if (!strcmp(playlist_entry, mpctx->filename)) // ignoring self-reference
3602 continue;
3604 entry = play_tree_new();
3606 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3608 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3609 if (temp)
3611 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3612 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3613 strcat(temp, playlist_entry);
3614 if (!strcmp(temp, mpctx->filename)) {
3615 free(temp);
3616 continue;
3618 play_tree_add_file(entry,temp);
3619 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3620 free(temp);
3623 else
3624 play_tree_add_file(entry,playlist_entry);
3626 if(!list)
3627 list = entry;
3628 else
3629 play_tree_append_entry(list,entry);
3631 free_demuxer(mpctx->demuxer);
3632 mpctx->demuxer = NULL;
3634 if (list)
3636 entry = play_tree_new();
3637 play_tree_set_child(entry,list);
3638 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3639 goto goto_next_file;
3643 if(!mpctx->demuxer)
3644 goto goto_next_file;
3646 if (mpctx->demuxer->matroska_data.ordered_chapters)
3647 build_ordered_chapter_timeline(mpctx);
3649 if (!mpctx->sources) {
3650 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
3651 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
3652 .demuxer = mpctx->demuxer};
3653 mpctx->num_sources = 1;
3656 if(dvd_chapter>1) {
3657 double pts;
3658 if (seek_chapter(mpctx, dvd_chapter-1, &pts, NULL) >= 0 && pts > -1.0)
3659 seek(mpctx, pts, SEEK_ABSOLUTE);
3662 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3664 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3665 int i;
3666 int maxid = -1;
3667 // setup global sub numbering
3668 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3669 for (i = 0; i < MAX_S_STREAMS; i++)
3670 if (mpctx->demuxer->s_streams[i])
3671 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3672 mpctx->global_sub_size += maxid + 1;
3674 // Make opts->sub_id always selectable if set.
3675 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3676 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3678 #ifdef CONFIG_ASS
3679 if (opts->ass_enabled && ass_library && use_embedded_fonts) {
3680 for (int j = 0; j < mpctx->num_sources; j++) {
3681 struct demuxer *d = mpctx->sources[j].demuxer;
3682 for (int i = 0; i < d->num_attachments; i++) {
3683 struct demux_attachment *att = d->attachments + i;
3684 if (att->name && att->type && att->data && att->data_size
3685 && (strcmp(att->type, "application/x-truetype-font") == 0
3686 || strcmp(att->type, "application/x-font") == 0))
3687 ass_add_font(ass_library, att->name, att->data, att->data_size);
3691 #endif
3693 current_module="demux_open2";
3695 //file_format=demuxer->file_format;
3697 mpctx->d_audio=mpctx->demuxer->audio;
3698 mpctx->d_video=mpctx->demuxer->video;
3699 mpctx->d_sub=mpctx->demuxer->sub;
3701 if (ts_prog) {
3702 int tmp = ts_prog;
3703 mp_property_do("switch_program", M_PROPERTY_SET, &tmp, mpctx);
3705 // select audio stream
3706 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3708 // DUMP STREAMS:
3709 if((stream_dump_type)&&(stream_dump_type<4)){
3710 FILE *f;
3711 demux_stream_t *ds=NULL;
3712 current_module="dump";
3713 // select stream to dump
3714 switch(stream_dump_type){
3715 case 1: ds=mpctx->d_audio;break;
3716 case 2: ds=mpctx->d_video;break;
3717 case 3: ds=mpctx->d_sub;break;
3719 if(!ds){
3720 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"dump: FATAL: Selected stream missing!\n");
3721 exit_player(mpctx, EXIT_ERROR);
3723 // disable other streams:
3724 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3725 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3726 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3727 // let's dump it!
3728 f=fopen(stream_dump_name,"wb");
3729 if(!f){
3730 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3731 exit_player(mpctx, EXIT_ERROR);
3733 while(!ds->eof){
3734 unsigned char* start;
3735 int in_size=ds_get_packet(ds,&start);
3736 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3737 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3738 if(in_size>0) fwrite(start,in_size,1,f);
3739 if(dvd_last_chapter>0) {
3740 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3741 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3742 break;
3745 fclose(f);
3746 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3747 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3750 mpctx->sh_audio=mpctx->d_audio->sh;
3751 mpctx->sh_video=mpctx->d_video->sh;
3753 if(mpctx->sh_video){
3755 current_module="video_read_properties";
3756 if(!video_read_properties(mpctx->sh_video)) {
3757 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Video: Cannot read properties.\n");
3758 mpctx->sh_video=mpctx->d_video->sh=NULL;
3759 } else {
3760 mp_tmsg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
3761 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3762 mpctx->sh_video->fps,mpctx->sh_video->frametime
3765 /* need to set fps here for output encoders to pick it up in their init */
3766 if(force_fps){
3767 mpctx->sh_video->fps=force_fps;
3768 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3770 vo_fps = mpctx->sh_video->fps;
3772 if(!mpctx->sh_video->fps && !force_fps){
3773 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"FPS not specified in the header or invalid, use the -fps option.\n");
3774 mpctx->sh_video=mpctx->d_video->sh=NULL;
3780 if(!mpctx->sh_video && !mpctx->sh_audio){
3781 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "No stream found.\n");
3782 #ifdef CONFIG_DVBIN
3783 if(mpctx->stream->type == STREAMTYPE_DVB)
3785 int dir;
3786 int v = mpctx->last_dvb_step;
3787 if(v > 0)
3788 dir = DVB_CHANNEL_HIGHER;
3789 else
3790 dir = DVB_CHANNEL_LOWER;
3792 if(dvb_step_channel(mpctx->stream, dir)) {
3793 mpctx->stop_play = PT_NEXT_ENTRY;
3794 mpctx->dvbin_reopen = 1;
3797 #endif
3798 goto goto_next_file; // exit_player(_("Fatal error"));
3801 /* display clip info */
3802 demux_info_print(mpctx->demuxer);
3804 //================== Read SUBTITLES (DVD & TEXT) ==========================
3805 if(vo_spudec==NULL && mpctx->sh_video &&
3806 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3807 init_vo_spudec(mpctx);
3810 // after reading video params we should load subtitles because
3811 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3812 // check .sub
3813 current_module="read_subtitles_file";
3814 double sub_fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
3815 if(sub_name){
3816 for (i = 0; sub_name[i] != NULL; ++i)
3817 add_subtitles(mpctx, sub_name[i], sub_fps, 0);
3819 if(sub_auto) { // auto load sub file ...
3820 char *psub = get_path( "sub/" );
3821 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3822 int i = 0;
3823 free(psub); // release the buffer created by get_path() above
3824 while (tmp[i]) {
3825 add_subtitles(mpctx, tmp[i], sub_fps, 1);
3826 free(tmp[i++]);
3828 free(tmp);
3830 if (mpctx->set_of_sub_size > 0) {
3831 // setup global sub numbering
3832 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3833 mpctx->global_sub_size += mpctx->set_of_sub_size;
3837 if (mpctx->global_sub_size) {
3838 select_subtitle(mpctx);
3839 if(subdata)
3840 switch (stream_dump_type) {
3841 case 3: list_sub_file(subdata); break;
3842 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3843 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3844 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3845 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3846 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3850 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3851 filename_recode(mpctx->filename));
3852 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3853 if (mpctx->sh_video) {
3854 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3855 if (mpctx->sh_video->format >= 0x20202020)
3856 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3857 else
3858 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3859 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3860 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3861 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3862 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3863 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3865 if (mpctx->sh_audio) {
3866 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3867 if (mpctx->sh_audio->format >= 0x20202020)
3868 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3869 else
3870 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3871 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3872 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3873 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3875 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2f\n", mpctx->timeline ?
3876 mpctx->timeline[mpctx->num_timeline_parts].start :
3877 demuxer_get_time_length(mpctx->demuxer));
3878 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n",
3879 mpctx->stream->seek && (!mpctx->demuxer || mpctx->demuxer->seekable));
3880 if (mpctx->demuxer) {
3881 if (mpctx->demuxer->num_chapters == 0)
3882 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3883 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3886 if(!mpctx->sh_video) goto main; // audio-only
3888 if(!reinit_video_chain(mpctx)) {
3889 if(!mpctx->sh_video){
3890 if(!mpctx->sh_audio) goto goto_next_file;
3891 goto main; // exit_player(_("Fatal error"));
3895 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3896 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3898 #ifdef CONFIG_FREETYPE
3899 force_load_font = 1;
3900 #endif
3902 //================== MAIN: ==========================
3903 main:
3904 current_module="main";
3906 if(playing_msg) {
3907 char* msg = property_expand_string(mpctx, playing_msg);
3908 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3909 free(msg);
3913 // Disable the term OSD in verbose mode
3914 if(verbose) term_osd = 0;
3917 //int frame_corr_num=0; //
3918 //float v_frame=0; // Video
3919 //float num_frames=0; // number of frames played
3921 int frame_time_remaining=0; // flag
3922 int blit_frame=0;
3924 // Make sure old OSD does not stay around,
3925 // e.g. with -fixed-vo and same-resolution files
3926 clear_osd_msgs();
3927 update_osd_msg(mpctx);
3929 //================ SETUP AUDIO ==========================
3931 if(mpctx->sh_audio){
3932 reinit_audio_chain(mpctx);
3933 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3934 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3937 current_module="av_init";
3939 if(mpctx->sh_video){
3940 mpctx->sh_video->timer=0;
3941 if (! ignore_start)
3942 audio_delay += mpctx->sh_video->stream_delay;
3944 if(mpctx->sh_audio){
3945 if (start_volume >= 0)
3946 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3947 if (! ignore_start)
3948 audio_delay -= mpctx->sh_audio->stream_delay;
3949 mpctx->delay=-audio_delay;
3952 if(!mpctx->sh_audio){
3953 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Audio: no sound\n");
3954 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3955 ds_free_packs(mpctx->d_audio); // free buffered chunks
3956 //mpctx->d_audio->id=-2; // do not read audio chunks
3957 //uninit_player(mpctx, INITIALIZED_AO); // close device
3959 if(!mpctx->sh_video){
3960 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Video: no video\n");
3961 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3962 ds_free_packs(mpctx->d_video);
3963 mpctx->d_video->id=-2;
3964 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3967 if (!mpctx->sh_video && !mpctx->sh_audio)
3968 goto goto_next_file;
3970 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3971 if(force_fps && mpctx->sh_video){
3972 vo_fps = mpctx->sh_video->fps=force_fps;
3973 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3974 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx->sh_video->fps,mpctx->sh_video->frametime);
3977 mp_input_set_section(mpctx->input, NULL);
3978 //TODO: add desired (stream-based) sections here
3979 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3980 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3982 //==================== START PLAYING =======================
3984 if(opts->loop_times>1) opts->loop_times--; else
3985 if(opts->loop_times==1) opts->loop_times = -1;
3987 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Starting playback...\n");
3989 total_time_usage_start=GetTimer();
3990 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3991 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3992 play_n_frames=play_n_frames_mf;
3994 if(play_n_frames==0){
3995 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
3998 // If there's a timeline force an absolute seek to initialize state
3999 if (seek_to_sec || mpctx->timeline) {
4000 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
4001 end_at.pos += seek_to_sec;
4004 if (end_at.type == END_AT_SIZE) {
4005 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Option -endpos in MPlayer does not yet support size units.\n");
4006 end_at.type = END_AT_NONE;
4009 #ifdef CONFIG_DVDNAV
4010 mp_dvdnav_context_free(mpctx);
4011 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4012 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
4013 mp_dvdnav_cell_has_changed(mpctx->stream,1);
4015 #endif
4017 get_relative_time(mpctx); // reset current delta
4018 mpctx->time_frame = 0;
4019 mpctx->drop_message_shown = 0;
4020 mpctx->update_video_immediately = true;
4021 mpctx->total_avsync_change = 0;
4022 // Make sure VO knows current pause state
4023 if (mpctx->sh_video)
4024 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
4025 NULL);
4027 while(!mpctx->stop_play){
4028 float aq_sleep_time=0;
4030 if(dvd_last_chapter>0) {
4031 int cur_chapter = get_current_chapter(mpctx);
4032 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
4033 goto goto_next_file;
4036 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
4037 mpctx->sh_audio = mpctx->d_audio->sh;
4038 mpctx->sh_audio->ds = mpctx->d_audio;
4039 reinit_audio_chain(mpctx);
4042 /*========================== PLAY AUDIO ============================*/
4044 if (mpctx->sh_audio && !mpctx->paused)
4045 if (!fill_audio_out_buffers(mpctx))
4046 // at eof, all audio at least written to ao
4047 if (!mpctx->sh_video)
4048 mpctx->stop_play = AT_END_OF_FILE;
4051 if(!mpctx->sh_video) {
4052 // handle audio-only case:
4053 double a_pos=0;
4054 // sh_audio can be NULL due to video stream switching
4055 // TODO: handle this better
4056 if (mpctx->sh_audio)
4057 a_pos = playing_audio_pts(mpctx);
4059 print_status(mpctx, a_pos, false);
4061 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
4062 mpctx->stop_play = PT_NEXT_ENTRY;
4063 update_subtitles(mpctx, &mpctx->opts, NULL, a_pos, mpctx->video_offset,
4064 mpctx->d_sub, 0);
4065 update_osd_msg(mpctx);
4067 } else {
4069 /*========================== PLAY VIDEO ============================*/
4071 vo_pts=mpctx->sh_video->timer*90000.0;
4072 vo_fps=mpctx->sh_video->fps;
4074 blit_frame = mpctx->video_out->frame_loaded;
4075 if (!blit_frame) {
4076 double frame_time = update_video(mpctx);
4077 blit_frame = mpctx->video_out->frame_loaded;
4078 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
4079 if (mpctx->sh_video->vf_initialized < 0) {
4080 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "\nFATAL: Could not initialize video filters (-vf) or video output (-vo).\n");
4081 mpctx->stop_play = PT_NEXT_ENTRY;
4082 goto goto_next_file;
4084 if (blit_frame) {
4085 struct sh_video *sh_video = mpctx->sh_video;
4086 update_subtitles(mpctx, &mpctx->opts, sh_video, sh_video->pts,
4087 mpctx->video_offset, mpctx->d_sub, 0);
4088 update_teletext(sh_video, mpctx->demuxer, 0);
4089 update_osd_msg(mpctx);
4090 struct vf_instance *vf = mpctx->sh_video->vfilter;
4091 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
4092 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
4093 vo_osd_changed(0);
4095 if (frame_time < 0)
4096 mpctx->stop_play = AT_END_OF_FILE;
4097 else {
4098 if (mpctx->update_video_immediately) {
4099 // Show this frame immediately, rest normally
4100 mpctx->update_video_immediately = false;
4101 } else {
4102 mpctx->time_frame += frame_time / opts->playback_speed;
4103 adjust_sync(mpctx, frame_time);
4107 if (mpctx->timeline) {
4108 struct timeline_part *next = mpctx->timeline + mpctx->timeline_part + 1;
4109 if (mpctx->sh_video->pts >= next->start
4110 || mpctx->stop_play == AT_END_OF_FILE
4111 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
4112 seek(mpctx, next->start, SEEK_ABSOLUTE);
4113 continue;
4117 // ==========================================================================
4119 // current_module="draw_osd";
4120 // if(vo_config_count) mpctx->video_out->draw_osd();
4122 current_module="vo_check_events";
4123 vo_check_events(mpctx->video_out);
4125 #ifdef CONFIG_X11
4126 if (stop_xscreensaver) {
4127 current_module = "stop_xscreensaver";
4128 xscreensaver_heartbeat(mpctx->x11_state);
4130 #endif
4131 if (heartbeat_cmd) {
4132 static unsigned last_heartbeat;
4133 unsigned now = GetTimerMS();
4134 if (now - last_heartbeat > 30000) {
4135 last_heartbeat = now;
4136 system(heartbeat_cmd);
4140 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
4142 //====================== FLIP PAGE (VIDEO BLT): =========================
4144 current_module="flip_page";
4145 if (!frame_time_remaining && blit_frame) {
4146 unsigned int t2=GetTimer();
4147 unsigned int pts_us = mpctx->last_time + mpctx->time_frame * 1e6;
4148 int duration = -1;
4149 double pts2 = mpctx->video_out->next_pts2;
4150 if (pts2 != MP_NOPTS_VALUE && opts->correct_pts) {
4151 // expected A/V sync correction is ignored
4152 double diff = (pts2 - mpctx->sh_video->pts);
4153 diff /= opts->playback_speed;
4154 if (mpctx->time_frame < 0)
4155 diff += mpctx->time_frame;
4156 if (diff < 0)
4157 diff = 0;
4158 if (diff > 10)
4159 diff = 10;
4160 duration = diff * 1e6;
4162 vo_flip_page(mpctx->video_out, pts_us|1, duration);
4164 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
4165 vout_time_usage += mpctx->last_vo_flip_duration;
4166 if (mpctx->video_out->driver->flip_page_timed) {
4167 // No need to adjust sync based on flip speed
4168 mpctx->last_vo_flip_duration = 0;
4169 // For print_status - VO call finishing early is OK for sync
4170 mpctx->time_frame -= get_relative_time(mpctx);
4172 print_status(mpctx, MP_NOPTS_VALUE, true);
4174 else
4175 print_status(mpctx, MP_NOPTS_VALUE, false);
4177 //============================ Auto QUALITY ============================
4179 /*Output quality adjustments:*/
4180 if(auto_quality>0){
4181 current_module="autoq";
4182 // float total=0.000001f * (GetTimer()-aq_total_time);
4183 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4184 if(output_quality<auto_quality && aq_sleep_time>0)
4185 ++output_quality;
4186 else
4187 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4188 if(output_quality>1 && aq_sleep_time<0)
4189 --output_quality;
4190 else
4191 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4192 output_quality=0;
4193 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4194 set_video_quality(mpctx->sh_video,output_quality);
4197 if (!frame_time_remaining && blit_frame) {
4198 if (play_n_frames >= 0) {
4199 --play_n_frames;
4200 if (play_n_frames <= 0)
4201 mpctx->stop_play = PT_NEXT_ENTRY;
4203 if (mpctx->step_frames > 0) {
4204 mpctx->step_frames--;
4205 if (mpctx->step_frames == 0)
4206 pause_player(mpctx);
4211 // FIXME: add size based support for -endpos
4212 if (end_at.type == END_AT_TIME &&
4213 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
4214 mpctx->stop_play = PT_NEXT_ENTRY;
4216 } // end if(mpctx->sh_video)
4218 #ifdef CONFIG_DVDNAV
4219 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4220 nav_highlight_t hl;
4221 mp_dvdnav_get_highlight (mpctx->stream, &hl);
4222 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4223 vo_osd_changed (OSDTYPE_DVDNAV);
4225 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
4226 double ar = -1.0;
4227 if (stream_control (mpctx->demuxer->stream,
4228 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
4229 != STREAM_UNSUPPORTED)
4230 mpctx->sh_video->stream_aspect = ar;
4233 #endif
4235 //================= Keyboard events, SEEKing ====================
4237 current_module="key_events";
4240 while (1) {
4241 mp_cmd_t* cmd;
4242 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
4243 run_command(mpctx, cmd);
4244 mp_cmd_free(cmd);
4245 if (mpctx->stop_play)
4246 break;
4248 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
4249 || mpctx->abs_seek_pos)
4250 break;
4251 if (mpctx->sh_video) {
4252 update_osd_msg(mpctx);
4253 int hack = vo_osd_changed(0);
4254 vo_osd_changed(hack);
4255 if (hack)
4256 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
4257 add_step_frame(mpctx);
4258 break;
4260 else
4261 vo_osd_changed(0);
4263 pause_loop(mpctx);
4267 // handle -sstep
4268 if (step_sec > 0 && !mpctx->paused) {
4269 mpctx->osd_function=OSD_FFW;
4270 mpctx->rel_seek_secs+=step_sec;
4273 edl_update(mpctx);
4275 /* Looping. */
4276 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
4277 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
4279 if(opts->loop_times>1) opts->loop_times--; else
4280 if(opts->loop_times==1) opts->loop_times=-1;
4281 play_n_frames=play_n_frames_mf;
4282 mpctx->stop_play=0;
4283 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
4286 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
4287 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
4289 mpctx->rel_seek_secs=0;
4290 mpctx->abs_seek_pos=0;
4293 } // while(!mpctx->stop_play)
4295 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4297 #ifdef CONFIG_DVBIN
4298 if(mpctx->dvbin_reopen)
4300 mpctx->stop_play = 0;
4301 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4302 cache_uninit(mpctx->stream);
4303 mpctx->dvbin_reopen = 0;
4304 goto goto_enable_cache;
4306 #endif
4309 goto_next_file: // don't jump here after ao/vo/getch initialization!
4311 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4313 if(benchmark){
4314 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4315 double total_time_usage;
4316 total_time_usage_start=GetTimer()-total_time_usage_start;
4317 total_time_usage = (float)total_time_usage_start*0.000001;
4318 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4319 video_time_usage,vout_time_usage,audio_time_usage,
4320 total_time_usage-tot,total_time_usage);
4321 if(total_time_usage>0.0)
4322 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4323 100.0*video_time_usage/total_time_usage,
4324 100.0*vout_time_usage/total_time_usage,
4325 100.0*audio_time_usage/total_time_usage,
4326 100.0*(total_time_usage-tot)/total_time_usage,
4327 100.0);
4328 if(total_frame_cnt && frame_dropping)
4329 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4330 total_frame_cnt-drop_frame_cnt,
4331 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4332 drop_frame_cnt,
4333 100*drop_frame_cnt/total_frame_cnt,
4334 total_frame_cnt,
4335 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4338 // time to uninit all, except global stuff:
4339 uninit_player(mpctx, INITIALIZED_ALL-(opts->fixed_vo?INITIALIZED_VO:0));
4341 if(mpctx->set_of_sub_size > 0) {
4342 current_module="sub_free";
4343 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4344 sub_free(mpctx->set_of_subtitles[i]);
4345 #ifdef CONFIG_ASS
4346 if(mpctx->set_of_ass_tracks[i])
4347 ass_free_track( mpctx->set_of_ass_tracks[i] );
4348 #endif
4350 mpctx->set_of_sub_size = 0;
4352 vo_sub_last = vo_sub=NULL;
4353 subdata=NULL;
4354 #ifdef CONFIG_ASS
4355 ass_track = NULL;
4356 if(ass_library)
4357 ass_clear_fonts(ass_library);
4358 #endif
4360 if (!mpctx->stop_play) // In case some goto jumped here...
4361 mpctx->stop_play = PT_NEXT_ENTRY;
4363 int playtree_direction = 1;
4365 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4366 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4367 play_tree_iter_free(mpctx->playtree_iter);
4368 mpctx->playtree_iter = NULL;
4370 mpctx->play_tree_step = 1;
4371 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4372 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4373 if(mpctx->playtree_iter) {
4374 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4375 play_tree_iter_free(mpctx->playtree_iter);
4376 mpctx->playtree_iter = NULL;
4379 } else if (mpctx->stop_play == PT_STOP) {
4380 play_tree_iter_free(mpctx->playtree_iter);
4381 mpctx->playtree_iter = NULL;
4382 } else { // NEXT PREV SRC
4383 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4386 while(mpctx->playtree_iter != NULL) {
4387 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4388 if(mpctx->filename == NULL) {
4389 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4390 play_tree_iter_free(mpctx->playtree_iter);
4391 mpctx->playtree_iter = NULL;
4393 } else
4394 break;
4397 if(mpctx->playtree_iter != NULL || player_idle_mode){
4398 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4399 mpctx->stop_play = 0;
4400 goto play_next_file;
4404 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4406 return 1;
4408 #endif /* DISABLE_MAIN */