2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 /// \ingroup Properties Command2Property OSDMsgStack
30 #if defined(__MINGW32__) || defined(__CYGWIN__)
31 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
37 // #include <sys/mman.h>
38 #include <sys/types.h>
40 #include <sys/ioctl.h>
43 #define SIGHUP 1 /* hangup */
44 #define SIGQUIT 3 /* quit */
45 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
46 #define SIGBUS 10 /* bus error */
47 #define SIGPIPE 13 /* broken pipe */
67 #include "access_mpcontext.h"
68 #include "m_property.h"
70 #include "cfg-mplayer-def.h"
72 #include "libavutil/avstring.h"
74 #include "subreader.h"
77 #include "libvo/video_out.h"
79 #include "libvo/font_load.h"
80 #include "libvo/sub.h"
83 #include "libvo/x11_common.h"
86 #include "libao2/audio_out.h"
88 #include "codec-cfg.h"
95 #include "osdep/getch2.h"
96 #include "osdep/timer.h"
97 #include "osdep/findfiles.h"
99 #include "input/input.h"
101 const int under_mencoder
= 0;
103 int player_idle_mode
=0;
104 int enable_mouse_movements
=0;
105 float start_volume
= -1;
107 #include "osdep/priority.h"
111 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
115 #include <linux/rtc.h>
118 #define RTC_IRQP_SET RTCIO_IRQP_SET
119 #define RTC_PIE_ON RTCIO_PIE_ON
120 #endif /* __linux__ */
121 #endif /* HAVE_RTC */
123 #include "stream/tv.h"
124 #include "stream/stream_radio.h"
126 #include "stream/dvbin.h"
128 #include "stream/cache2.h"
130 //**************************************************************************//
132 //**************************************************************************//
133 #include "playtree.h"
134 #include "playtreeparser.h"
136 //**************************************************************************//
138 //**************************************************************************//
139 #include "parser-cfg.h"
140 #include "parser-mpcmd.h"
142 //**************************************************************************//
144 //**************************************************************************//
146 static int cfg_inc_verbose(m_option_t
*conf
){ ++verbose
; return 0;}
150 //**************************************************************************//
151 //**************************************************************************//
152 // Input media streaming & demultiplexer:
153 //**************************************************************************//
155 static int max_framesize
=0;
157 #include "stream/stream.h"
158 #include "libmpdemux/demuxer.h"
159 #include "libmpdemux/stheader.h"
161 #ifdef CONFIG_DVDREAD
162 #include "stream/stream_dvd.h"
164 #include "stream/stream_dvdnav.h"
166 #include "libmpcodecs/dec_audio.h"
167 #include "libmpcodecs/dec_video.h"
168 #include "libmpcodecs/mp_image.h"
169 #include "libmpcodecs/vf.h"
170 #include "libmpcodecs/vd.h"
176 #include "defaultopts.h"
178 static const char help_text
[]=_(
179 "Usage: mplayer [options] [url|path/]filename\n"
181 "Basic options: (complete list in the man page)\n"
182 " -vo <drv> select video output driver ('-vo help' for a list)\n"
183 " -ao <drv> select audio output driver ('-ao help' for a list)\n"
185 " vcd://<trackno> play (S)VCD (Super Video CD) track (raw device, no mount)\n"
187 #ifdef CONFIG_DVDREAD
188 " dvd://<titleno> play DVD title from device instead of plain file\n"
190 " -alang/-slang select DVD audio/subtitle language (by 2-char country code)\n"
191 " -ss <position> seek to given (seconds or hh:mm:ss) position\n"
192 " -nosound do not play sound\n"
193 " -fs fullscreen playback (or -vm, -zoom, details in the man page)\n"
194 " -x <x> -y <y> set display resolution (for use with -vm or -zoom)\n"
195 " -sub <file> specify subtitle file to use (also see -subfps, -subdelay)\n"
196 " -playlist <file> specify playlist file\n"
197 " -vid x -aid y select video (x) and audio (y) stream to play\n"
198 " -fps x -srate y change video (x fps) and audio (y Hz) rate\n"
199 " -pp <quality> enable postprocessing filter (details in the man page)\n"
200 " -framedrop enable frame dropping (for slow machines)\n"
202 "Basic keys: (complete list in the man page, also check input.conf)\n"
203 " <- or -> seek backward/forward 10 seconds\n"
204 " down or up seek backward/forward 1 minute\n"
205 " pgdown or pgup seek backward/forward 10 minutes\n"
206 " < or > step backward/forward in playlist\n"
207 " p or SPACE pause movie (press any key to continue)\n"
208 " q or ESC stop playing and quit program\n"
209 " + or - adjust audio delay by +/- 0.1 second\n"
210 " o cycle OSD mode: none / seekbar / seekbar + timer\n"
211 " * or / increase or decrease PCM volume\n"
212 " x or z adjust subtitle delay by +/- 0.1 second\n"
213 " r or t adjust subtitle position up/down, also see -vf expand\n"
215 " * * * SEE THE MAN PAGE FOR DETAILS, FURTHER (ADVANCED) OPTIONS AND KEYS * * *\n"
219 #define Exit_SIGILL_RTCpuSel _(\
220 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
221 " It may be a bug in our new runtime CPU-detection code...\n"\
222 " Please read DOCS/HTML/en/bugreports.html.\n")
224 #define Exit_SIGILL _(\
225 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
226 " It usually happens when you run it on a CPU different than the one it was\n"\
227 " compiled/optimized for.\n"\
230 #define Exit_SIGSEGV_SIGFPE _(\
231 "- MPlayer crashed by bad usage of CPU/FPU/RAM.\n"\
232 " Recompile MPlayer with --enable-debug and make a 'gdb' backtrace and\n"\
233 " disassembly. Details in DOCS/HTML/en/bugreports_what.html#bugreports_crash.\n")
235 #define Exit_SIGCRASH _(\
236 "- MPlayer crashed. This shouldn't happen.\n"\
237 " It can be a bug in the MPlayer code _or_ in your drivers _or_ in your\n"\
238 " gcc version. If you think it's MPlayer's fault, please read\n"\
239 " DOCS/HTML/en/bugreports.html and follow the instructions there. We can't and\n"\
240 " won't help unless you provide this information when reporting a possible bug.\n")
242 #define SystemTooSlow _("\n\n"\
243 " ************************************************\n"\
244 " **** Your system is too SLOW to play this! ****\n"\
245 " ************************************************\n\n"\
246 "Possible reasons, problems, workarounds:\n"\
247 "- Most common: broken/buggy _audio_ driver\n"\
248 " - Try -ao sdl or use the OSS emulation of ALSA.\n"\
249 " - Experiment with different values for -autosync, 30 is a good start.\n"\
250 "- Slow video output\n"\
251 " - Try a different -vo driver (-vo help for a list) or try -framedrop!\n"\
253 " - Don't try to play a big DVD/DivX on a slow CPU! Try some of the lavdopts,\n"\
254 " e.g. -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all.\n"\
256 " - Try various combinations of -nobps -ni -forceidx -mc 0.\n"\
257 "- Slow media (NFS/SMB mounts, DVD, VCD etc)\n"\
258 " - Try -cache 8192.\n"\
259 "- Are you using -cache to play a non-interleaved AVI file?\n"\
260 " - Try -nocache.\n"\
261 "Read DOCS/HTML/en/video.html for tuning/speedup tips.\n"\
262 "If none of this helps you, read DOCS/HTML/en/bugreports.html.\n\n")
265 //**************************************************************************//
266 //**************************************************************************//
268 // Common FIFO functions, and keyboard/event FIFO code
270 int noconsolecontrols
=0;
271 //**************************************************************************//
274 double video_time_usage
=0;
275 double vout_time_usage
=0;
276 static double audio_time_usage
=0;
277 static int total_time_usage_start
=0;
278 static int total_frame_cnt
=0;
279 static int drop_frame_cnt
=0; // total number of dropped frames
284 static int output_quality
=0;
286 static int list_properties
= 0;
289 static char* term_osd_esc
= "\x1b[A\r\x1b[K";
290 static char* playing_msg
= NULL
;
292 static double seek_to_sec
;
293 static off_t seek_to_byte
=0;
294 static off_t step_sec
=0;
296 static m_time_size_t end_at
= { .type
= END_AT_NONE
, .pos
= 0 };
299 char **audio_codec_list
=NULL
; // override audio codec
300 char **video_codec_list
=NULL
; // override video codec
301 char **audio_fm_list
=NULL
; // override audio codec family
302 char **video_fm_list
=NULL
; // override video codec family
304 // this dvdsub_id was selected via slang
305 // use this to allow dvdnav to follow -slang across stream resets,
306 // in particular the subtitle ID for a language changes
309 static char* spudec_ifo
=NULL
;
310 int forced_subs_only
=0;
314 int stream_cache_size
=-1;
317 int stream_dump_type
=0;
320 static float default_max_pts_correction
=-1;//0.01f;
322 static int ignore_start
=0;
325 static int force_srate
=0;
326 int frame_dropping
=0; // option 0=no drop 1= drop vo 2= drop decode
327 static int play_n_frames
=-1;
328 static int play_n_frames_mf
=-1;
331 char *font_name
=NULL
;
332 char *sub_font_name
=NULL
;
333 extern int font_fontconfig
;
334 float font_factor
=0.75;
335 char **sub_name
=NULL
;
339 char *vobsub_name
=NULL
;
341 int suboverlap_enabled
= 1;
345 char* current_module
=NULL
; // for debugging
351 #include "m_struct.h"
352 #include "libmenu/menu.h"
353 static const vf_info_t
* const libmenu_vfs
[] = {
357 static vf_instance_t
* vf_menu
= NULL
;
359 static char* menu_cfg
= NULL
;
360 static char* menu_root
= "main";
364 edl_record_ptr edl_records
= NULL
; ///< EDL entries memory area
365 edl_record_ptr next_edl_record
= NULL
; ///< only for traversing edl_records
366 FILE* edl_fd
= NULL
; ///< fd to write to when in -edlout mode.
367 int use_filedir_conf
;
368 int use_filename_title
;
370 #include "mpcommon.h"
373 #include "metadata.h"
375 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
377 const void *mpctx_get_video_out(MPContext
*mpctx
)
379 return mpctx
->video_out
;
382 const void *mpctx_get_audio_out(MPContext
*mpctx
)
384 return mpctx
->audio_out
;
387 void *mpctx_get_demuxer(MPContext
*mpctx
)
389 return mpctx
->demuxer
;
392 void *mpctx_get_playtree_iter(MPContext
*mpctx
)
394 return mpctx
->playtree_iter
;
397 void *mpctx_get_mixer(MPContext
*mpctx
)
399 return &mpctx
->mixer
;
402 int mpctx_get_global_sub_size(MPContext
*mpctx
)
404 return mpctx
->global_sub_size
;
407 int mpctx_get_osd_function(MPContext
*mpctx
)
409 return mpctx
->osd_function
;
412 static float get_relative_time(struct MPContext
*mpctx
)
414 unsigned int new_time
= GetTimer();
415 unsigned int delta
= new_time
- mpctx
->last_time
;
416 mpctx
->last_time
= new_time
;
417 return delta
* 0.000001;
420 static int is_valid_metadata_type(struct MPContext
*mpctx
, metadata_t type
) {
423 /* check for valid video stream */
424 case META_VIDEO_CODEC
:
425 case META_VIDEO_BITRATE
:
426 case META_VIDEO_RESOLUTION
:
428 if (!mpctx
->sh_video
)
433 /* check for valid audio stream */
434 case META_AUDIO_CODEC
:
435 case META_AUDIO_BITRATE
:
436 case META_AUDIO_SAMPLES
:
438 if (!mpctx
->sh_audio
)
443 /* check for valid demuxer */
444 case META_INFO_TITLE
:
445 case META_INFO_ARTIST
:
446 case META_INFO_ALBUM
:
448 case META_INFO_COMMENT
:
449 case META_INFO_TRACK
:
450 case META_INFO_GENRE
:
464 static char *get_demuxer_info(struct MPContext
*mpctx
, char *tag
) {
465 char **info
= mpctx
->demuxer
->info
;
471 for (n
= 0; info
[2*n
] != NULL
; n
++)
472 if (!strcasecmp (info
[2*n
], tag
))
475 return info
[2*n
+1] ? strdup (info
[2*n
+1]) : NULL
;
478 char *get_metadata(struct MPContext
*mpctx
, metadata_t type
)
481 sh_audio_t
* const sh_audio
= mpctx
->sh_audio
;
482 sh_video_t
* const sh_video
= mpctx
->sh_video
;
484 if (!is_valid_metadata_type(mpctx
, type
))
491 return strdup (mp_basename2 (mpctx
->filename
));
494 case META_VIDEO_CODEC
:
496 if (sh_video
->format
== 0x10000001)
497 meta
= strdup ("mpeg1");
498 else if (sh_video
->format
== 0x10000002)
499 meta
= strdup ("mpeg2");
500 else if (sh_video
->format
== 0x10000004)
501 meta
= strdup ("mpeg4");
502 else if (sh_video
->format
== 0x10000005)
503 meta
= strdup ("h264");
504 else if (sh_video
->format
>= 0x20202020)
507 sprintf (meta
, "%.4s", (char *) &sh_video
->format
);
512 sprintf (meta
, "0x%08X", sh_video
->format
);
517 case META_VIDEO_BITRATE
:
520 sprintf (meta
, "%d kbps", (int) (sh_video
->i_bps
* 8 / 1024));
524 case META_VIDEO_RESOLUTION
:
527 sprintf (meta
, "%d x %d", sh_video
->disp_w
, sh_video
->disp_h
);
531 case META_AUDIO_CODEC
:
533 if (sh_audio
->codec
&& sh_audio
->codec
->name
)
534 meta
= strdup (sh_audio
->codec
->name
);
538 case META_AUDIO_BITRATE
:
541 sprintf (meta
, "%d kbps", (int) (sh_audio
->i_bps
* 8/1000));
545 case META_AUDIO_SAMPLES
:
548 sprintf (meta
, "%d Hz, %d ch.", sh_audio
->samplerate
, sh_audio
->channels
);
552 /* check for valid demuxer */
553 case META_INFO_TITLE
:
554 return get_demuxer_info(mpctx
, "Title");
556 case META_INFO_ARTIST
:
557 return get_demuxer_info(mpctx
, "Artist");
559 case META_INFO_ALBUM
:
560 return get_demuxer_info(mpctx
, "Album");
563 return get_demuxer_info(mpctx
, "Year");
565 case META_INFO_COMMENT
:
566 return get_demuxer_info(mpctx
, "Comment");
568 case META_INFO_TRACK
:
569 return get_demuxer_info(mpctx
, "Track");
571 case META_INFO_GENRE
:
572 return get_demuxer_info(mpctx
, "Genre");
581 static void print_file_properties(struct MPContext
*mpctx
, const char *filename
)
583 double start_pts
= MP_NOPTS_VALUE
;
584 double video_start_pts
= MP_NOPTS_VALUE
;
585 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_FILENAME=%s\n",
586 filename_recode(filename
));
587 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_DEMUXER=%s\n", mpctx
->demuxer
->desc
->name
);
588 if (mpctx
->sh_video
) {
589 /* Assume FOURCC if all bytes >= 0x20 (' ') */
590 if (mpctx
->sh_video
->format
>= 0x20202020)
591 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx
->sh_video
->format
);
593 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FORMAT=0x%08X\n", mpctx
->sh_video
->format
);
594 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_BITRATE=%d\n", mpctx
->sh_video
->i_bps
*8);
595 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_WIDTH=%d\n", mpctx
->sh_video
->disp_w
);
596 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_HEIGHT=%d\n", mpctx
->sh_video
->disp_h
);
597 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FPS=%5.3f\n", mpctx
->sh_video
->fps
);
598 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_ASPECT=%1.4f\n", mpctx
->sh_video
->aspect
);
599 video_start_pts
= ds_get_next_pts(mpctx
->d_video
);
601 if (mpctx
->sh_audio
) {
602 /* Assume FOURCC if all bytes >= 0x20 (' ') */
603 if (mpctx
->sh_audio
->format
>= 0x20202020)
604 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx
->sh_audio
->format
);
606 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_FORMAT=%d\n", mpctx
->sh_audio
->format
);
607 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_BITRATE=%d\n", mpctx
->sh_audio
->i_bps
*8);
608 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_RATE=%d\n", mpctx
->sh_audio
->samplerate
);
609 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_NCH=%d\n", mpctx
->sh_audio
->channels
);
610 start_pts
= ds_get_next_pts(mpctx
->d_audio
);
612 if (video_start_pts
!= MP_NOPTS_VALUE
) {
613 if (start_pts
== MP_NOPTS_VALUE
|| !mpctx
->sh_audio
||
614 (mpctx
->sh_video
&& video_start_pts
< start_pts
))
615 start_pts
= video_start_pts
;
617 if (start_pts
!= MP_NOPTS_VALUE
)
618 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_START_TIME=%.2f\n", start_pts
);
620 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_START_TIME=unknown\n");
621 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_LENGTH=%.2f\n", get_time_length(mpctx
));
622 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_SEEKABLE=%d\n",
623 mpctx
->stream
->seek
&& (!mpctx
->demuxer
|| mpctx
->demuxer
->seekable
));
624 if (mpctx
->demuxer
) {
625 if (mpctx
->demuxer
->num_chapters
== 0)
626 stream_control(mpctx
->demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
, &mpctx
->demuxer
->num_chapters
);
627 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_CHAPTERS=%d\n", mpctx
->demuxer
->num_chapters
);
631 /// step size of mixer changes
635 static void mp_dvdnav_context_free(MPContext
*ctx
){
636 if (ctx
->nav_smpi
) free_mp_image(ctx
->nav_smpi
);
637 ctx
->nav_smpi
= NULL
;
638 free(ctx
->nav_buffer
);
639 ctx
->nav_buffer
= NULL
;
640 ctx
->nav_start
= NULL
;
641 ctx
->nav_in_size
= 0;
645 void uninit_player(struct MPContext
*mpctx
, unsigned int mask
){
646 mask
&= mpctx
->initialized_flags
;
648 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n*** uninit(0x%X)\n",mask
);
650 if(mask
&INITIALIZED_ACODEC
){
651 mpctx
->initialized_flags
&=~INITIALIZED_ACODEC
;
652 current_module
="uninit_acodec";
653 if(mpctx
->sh_audio
) uninit_audio(mpctx
->sh_audio
);
654 mpctx
->sh_audio
=NULL
;
655 mpctx
->mixer
.afilter
= NULL
;
658 if(mask
&INITIALIZED_VCODEC
){
659 mpctx
->initialized_flags
&=~INITIALIZED_VCODEC
;
660 current_module
="uninit_vcodec";
661 if(mpctx
->sh_video
) uninit_video(mpctx
->sh_video
);
662 mpctx
->sh_video
=NULL
;
668 if(mask
&INITIALIZED_DEMUXER
){
669 mpctx
->initialized_flags
&=~INITIALIZED_DEMUXER
;
670 current_module
="free_demuxer";
671 if (mpctx
->num_sources
) {
672 mpctx
->demuxer
= mpctx
->sources
[0].demuxer
;
673 for (int i
= 1; i
< mpctx
->num_sources
; i
++) {
674 free_stream(mpctx
->sources
[i
].stream
);
675 free_demuxer(mpctx
->sources
[i
].demuxer
);
678 talloc_free(mpctx
->sources
);
679 mpctx
->sources
= NULL
;
680 mpctx
->num_sources
= 0;
681 talloc_free(mpctx
->timeline
);
682 mpctx
->timeline
= NULL
;
683 mpctx
->num_timeline_parts
= 0;
684 talloc_free(mpctx
->chapters
);
685 mpctx
->chapters
= NULL
;
686 mpctx
->num_chapters
= 0;
687 mpctx
->video_offset
= 0;
689 mpctx
->stream
=mpctx
->demuxer
->stream
;
690 free_demuxer(mpctx
->demuxer
);
695 // kill the cache process:
696 if(mask
&INITIALIZED_STREAM
){
697 mpctx
->initialized_flags
&=~INITIALIZED_STREAM
;
698 current_module
="uninit_stream";
699 if(mpctx
->stream
) free_stream(mpctx
->stream
);
703 if(mask
&INITIALIZED_VO
){
704 mpctx
->initialized_flags
&=~INITIALIZED_VO
;
705 current_module
="uninit_vo";
706 vo_destroy(mpctx
->video_out
);
707 mpctx
->video_out
=NULL
;
709 mp_dvdnav_context_free(mpctx
);
713 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
714 if(mask
&INITIALIZED_GETCH2
){
715 mpctx
->initialized_flags
&=~INITIALIZED_GETCH2
;
716 current_module
="uninit_getch2";
717 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n[[[uninit getch2]]]\n");
722 if(mask
&INITIALIZED_VOBSUB
){
723 mpctx
->initialized_flags
&=~INITIALIZED_VOBSUB
;
724 current_module
="uninit_vobsub";
725 if(vo_vobsub
) vobsub_close(vo_vobsub
);
729 if (mask
&INITIALIZED_SPUDEC
){
730 mpctx
->initialized_flags
&=~INITIALIZED_SPUDEC
;
731 current_module
="uninit_spudec";
732 spudec_free(vo_spudec
);
736 if(mask
&INITIALIZED_AO
){
737 mpctx
->initialized_flags
&=~INITIALIZED_AO
;
738 current_module
="uninit_ao";
739 if (mpctx
->edl_muted
) mixer_mute(&mpctx
->mixer
);
740 if (mpctx
->audio_out
)
741 mpctx
->audio_out
->uninit(mpctx
->stop_play
!= AT_END_OF_FILE
);
742 mpctx
->audio_out
=NULL
;
748 void exit_player_with_rc(struct MPContext
*mpctx
, enum exit_reason how
, int rc
)
750 if (mpctx
->user_muted
&& !mpctx
->edl_muted
) mixer_mute(&mpctx
->mixer
);
751 uninit_player(mpctx
, INITIALIZED_ALL
);
752 #if defined(__MINGW32__) || defined(__CYGWIN__)
756 vo_uninit(mpctx
->x11_state
); // Close the X11 connection (if any is open).
759 current_module
="uninit_input";
760 mp_input_uninit(mpctx
->input
);
766 #ifdef CONFIG_FREETYPE
767 current_module
="uninit_font";
768 if (mpctx
->osd
&& mpctx
->osd
->sub_font
!= vo_font
)
769 free_font_desc(mpctx
->osd
->sub_font
);
770 free_font_desc(vo_font
);
774 osd_free(mpctx
->osd
);
777 ass_library_done(ass_library
);
781 current_module
="exit_player";
783 // free mplayer config
785 m_config_free(mpctx
->mconfig
);
786 mpctx
->mconfig
= NULL
;
788 if(mpctx
->playtree_iter
)
789 play_tree_iter_free(mpctx
->playtree_iter
);
790 mpctx
->playtree_iter
= NULL
;
792 play_tree_free(mpctx
->playtree
, 1);
793 mpctx
->playtree
= NULL
;
795 talloc_free(mpctx
->key_fifo
);
797 free(edl_records
); // free mem allocated for EDL
801 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"\nExiting... (%s)\n","Quit");
802 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=QUIT\n");
805 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"\nExiting... (%s)\n","End of file");
806 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=EOF\n");
809 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"\nExiting... (%s)\n","Fatal error");
810 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=ERROR\n");
813 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=NONE\n");
815 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"max framesize was %d bytes\n",max_framesize
);
820 static void exit_player(struct MPContext
*mpctx
, enum exit_reason how
)
822 exit_player_with_rc(mpctx
, how
, 1);
826 static void child_sighandler(int x
){
828 while((pid
=waitpid(-1,NULL
,WNOHANG
)) > 0);
832 #ifdef CONFIG_CRASH_DEBUG
833 static char *prog_path
;
834 static int crash_debug
= 0;
837 static void exit_sighandler(int x
){
838 static int sig_count
=0;
839 #ifdef CONFIG_CRASH_DEBUG
840 if (!crash_debug
|| x
!= SIGTRAP
)
845 /* We're crashing bad and can't uninit cleanly :(
846 * by popular request, we make one last (dirty)
847 * effort to restore the user's
852 if(sig_count
==6) exit(1);
856 kill(getpid(),SIGKILL
);
859 mp_msg(MSGT_CPLAYER
, MSGL_FATAL
, "\n");
860 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,
861 "\nMPlayer interrupted by signal %d in module: %s\n", x
,
862 current_module
? current_module
: "unknown");
863 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SIGNAL=%d\n", x
);
871 async_quit_request
= 1;
872 return; // killed from keyboard (^C) or killed [-9]
874 #if CONFIG_RUNTIME_CPUDETECT
875 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,Exit_SIGILL_RTCpuSel
);
877 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,Exit_SIGILL
);
881 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,Exit_SIGSEGV_SIGFPE
);
883 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,Exit_SIGCRASH
);
884 #ifdef CONFIG_CRASH_DEBUG
887 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Forking...\n");
889 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Forked...\n");
890 if (gdb_pid
== 0) { // We are the child
892 snprintf(spid
, sizeof(spid
), "%i", getppid());
893 getch2_disable(); // allow terminal to work properly with gdb
894 if (execlp("gdb", "gdb", prog_path
, spid
, "-ex", "bt", NULL
) == -1)
895 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Couldn't start gdb\n");
896 } else if (gdb_pid
< 0)
897 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Couldn't fork\n");
899 waitpid(gdb_pid
, NULL
, 0);
901 if (x
== SIGTRAP
) return;
909 #include "cfg-mplayer.h"
911 static int cfg_include(m_option_t
*conf
, char *filename
)
913 return m_config_parse_config_file(conf
->priv
, filename
);
916 static void parse_cfgfiles(struct MPContext
*mpctx
, m_config_t
* conf
)
920 if (!disable_system_conf
&&
921 m_config_parse_config_file(conf
, MPLAYER_CONFDIR
"/mplayer.conf") < 0)
922 exit_player(mpctx
, EXIT_NONE
);
923 if ((conffile
= get_path("")) == NULL
) {
924 mp_tmsg(MSGT_CPLAYER
,MSGL_WARN
,"Cannot find HOME directory.\n");
929 mkdir(conffile
, 0777);
932 if ((conffile
= get_path("config")) == NULL
) {
933 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"get_path(\"config\") problem\n");
935 if ((conffile_fd
= open(conffile
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666)) != -1) {
936 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Creating config file: %s\n", conffile
);
937 write(conffile_fd
, default_config
, strlen(default_config
));
940 if (!disable_user_conf
&&
941 m_config_parse_config_file(conf
, conffile
) < 0)
942 exit_player(mpctx
, EXIT_NONE
);
948 #define PROFILE_CFG_PROTOCOL "protocol."
950 static void load_per_protocol_config (m_config_t
* conf
, const char *const file
)
953 char protocol
[strlen (PROFILE_CFG_PROTOCOL
) + strlen (file
) + 1];
956 /* does filename actually uses a protocol ? */
957 str
= strstr (file
, "://");
961 sprintf (protocol
, "%s%s", PROFILE_CFG_PROTOCOL
, file
);
962 protocol
[strlen (PROFILE_CFG_PROTOCOL
)+strlen(file
)-strlen(str
)] = '\0';
963 p
= m_config_get_profile (conf
, protocol
);
966 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Loading protocol-related profile '%s'\n", protocol
);
967 m_config_set_profile(conf
,p
);
971 #define PROFILE_CFG_EXTENSION "extension."
973 static void load_per_extension_config (m_config_t
* conf
, const char *const file
)
976 char extension
[strlen (PROFILE_CFG_EXTENSION
) + 8];
979 /* does filename actually have an extension ? */
980 str
= strrchr (file
, '.');
984 sprintf (extension
, PROFILE_CFG_EXTENSION
);
985 strncat (extension
, ++str
, 7);
986 p
= m_config_get_profile (conf
, extension
);
989 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Loading extension-related profile '%s'\n", extension
);
990 m_config_set_profile(conf
,p
);
994 #define PROFILE_CFG_VO "vo."
995 #define PROFILE_CFG_AO "ao."
997 static void load_per_output_config (m_config_t
* conf
, char *cfg
, char *out
)
999 char profile
[strlen (cfg
) + strlen (out
) + 1];
1002 sprintf (profile
, "%s%s", cfg
, out
);
1003 p
= m_config_get_profile (conf
, profile
);
1006 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Loading extension-related profile '%s'\n", profile
);
1007 m_config_set_profile(conf
,p
);
1012 * Tries to load a config file
1013 * @return 0 if file was not found, 1 otherwise
1015 static int try_load_config(m_config_t
*conf
, const char *file
)
1018 if (stat(file
, &st
))
1020 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "Loading config '%s'\n", file
);
1021 m_config_parse_config_file (conf
, file
);
1025 static void load_per_file_config (m_config_t
* conf
, const char *const file
)
1031 if (strlen(file
) > PATH_MAX
- 14) {
1032 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, "Filename is too long, can not load file or directory specific config files\n");
1035 sprintf (cfg
, "%s.conf", file
);
1037 name
= strrchr(cfg
, '/');
1038 if (HAVE_DOS_PATHS
) {
1039 char *tmp
= strrchr(cfg
, '\\');
1040 if (!name
|| tmp
> name
)
1042 tmp
= strrchr(cfg
, ':');
1043 if (!name
|| tmp
> name
)
1051 if (use_filedir_conf
) {
1052 char dircfg
[PATH_MAX
];
1053 strcpy(dircfg
, cfg
);
1054 strcpy(dircfg
+ (name
- cfg
), "mplayer.conf");
1055 try_load_config(conf
, dircfg
);
1057 if (try_load_config(conf
, cfg
))
1061 if ((confpath
= get_path (name
)) != NULL
)
1063 try_load_config(conf
, confpath
);
1069 /* When libmpdemux performs a blocking operation (network connection or
1070 * cache filling) if the operation fails we use this function to check
1071 * if it was interrupted by the user.
1072 * The function returns a new value for eof. */
1073 static int libmpdemux_was_interrupted(struct MPContext
*mpctx
, int stop_play
)
1076 if((cmd
= mp_input_get_cmd(mpctx
->input
, 0, 0)) != NULL
) {
1079 exit_player_with_rc(mpctx
, EXIT_QUIT
, (cmd
->nargs
> 0)? cmd
->args
[0].v
.i
: 0);
1080 case MP_CMD_PLAY_TREE_STEP
: {
1081 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_NEXT_ENTRY
: PT_PREV_ENTRY
;
1082 mpctx
->play_tree_step
= (cmd
->args
[0].v
.i
== 0) ? 1 : cmd
->args
[0].v
.i
;
1084 case MP_CMD_PLAY_TREE_UP_STEP
: {
1085 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_UP_NEXT
: PT_UP_PREV
;
1087 case MP_CMD_PLAY_ALT_SRC_STEP
: {
1088 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_NEXT_SRC
: PT_PREV_SRC
;
1096 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
1098 static int playtree_add_playlist(struct MPContext
*mpctx
, play_tree_t
* entry
)
1100 play_tree_add_bpf(entry
,mpctx
->filename
);
1104 entry
= mpctx
->playtree_iter
->tree
;
1105 if(play_tree_iter_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
1106 return PT_NEXT_ENTRY
;
1108 if(mpctx
->playtree_iter
->tree
== entry
) { // Loop with a single file
1109 if(play_tree_iter_up_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
1110 return PT_NEXT_ENTRY
;
1113 play_tree_remove(entry
,1,1);
1116 play_tree_insert_entry(mpctx
->playtree_iter
->tree
,entry
);
1117 play_tree_set_params_from(entry
,mpctx
->playtree_iter
->tree
);
1118 entry
= mpctx
->playtree_iter
->tree
;
1119 if(play_tree_iter_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
1120 return PT_NEXT_ENTRY
;
1122 play_tree_remove(entry
,1,1);
1127 void add_subtitles(struct MPContext
*mpctx
, char *filename
, float fps
, int noerr
)
1129 struct MPOpts
*opts
= &mpctx
->opts
;
1130 sub_data
*subd
= NULL
;
1131 struct ass_track
*asst
= NULL
;
1133 if (filename
== NULL
|| mpctx
->set_of_sub_size
>= MAX_SUBTITLE_FILES
) {
1138 if (opts
->ass_enabled
) {
1140 asst
= ass_read_stream(ass_library
, filename
, sub_cp
);
1142 asst
= ass_read_stream(ass_library
, filename
, 0);
1145 subd
= sub_read_file(filename
, fps
);
1147 asst
= ass_read_subdata(ass_library
, subd
, fps
);
1156 subd
= sub_read_file(filename
, fps
);
1159 if (!asst
&& !subd
) {
1160 mp_tmsg(MSGT_CPLAYER
, noerr
? MSGL_WARN
: MSGL_ERR
,
1161 "Cannot load subtitles: %s\n", filename_recode(filename
));
1165 mpctx
->set_of_ass_tracks
[mpctx
->set_of_sub_size
] = asst
;
1166 mpctx
->set_of_subtitles
[mpctx
->set_of_sub_size
] = subd
;
1167 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_FILE_SUB_ID=%d\n", mpctx
->set_of_sub_size
);
1168 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_FILE_SUB_FILENAME=%s\n",
1169 filename_recode(filename
));
1170 ++mpctx
->set_of_sub_size
;
1171 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "SUB: Added subtitle file (%d): %s\n", mpctx
->set_of_sub_size
,
1172 filename_recode(filename
));
1175 void init_vo_spudec(struct MPContext
*mpctx
)
1178 spudec_free(vo_spudec
);
1179 mpctx
->initialized_flags
&= ~INITIALIZED_SPUDEC
;
1182 // we currently can't work without video stream
1183 if (!mpctx
->sh_video
)
1187 unsigned int palette
[16], width
, height
;
1188 current_module
="spudec_init_vobsub";
1189 if (vobsub_parse_ifo(NULL
,spudec_ifo
, palette
, &width
, &height
, 1, -1, NULL
) >= 0)
1190 vo_spudec
=spudec_new_scaled(palette
, width
, height
, NULL
, 0);
1193 #ifdef CONFIG_DVDREAD
1194 if (vo_spudec
==NULL
&& mpctx
->stream
->type
==STREAMTYPE_DVD
) {
1195 current_module
="spudec_init_dvdread";
1196 vo_spudec
=spudec_new_scaled(((dvd_priv_t
*)(mpctx
->stream
->priv
))->cur_pgc
->palette
,
1197 mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
,
1202 #ifdef CONFIG_DVDNAV
1203 if (vo_spudec
==NULL
&& mpctx
->stream
->type
==STREAMTYPE_DVDNAV
) {
1204 unsigned int *palette
= mp_dvdnav_get_spu_clut(mpctx
->stream
);
1205 current_module
="spudec_init_dvdnav";
1206 vo_spudec
=spudec_new_scaled(palette
, mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
, NULL
, 0);
1210 if (vo_spudec
==NULL
) {
1211 sh_sub_t
*sh
= mpctx
->d_sub
->sh
;
1212 current_module
="spudec_init_normal";
1213 vo_spudec
=spudec_new_scaled(NULL
, mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
, sh
->extradata
, sh
->extradata_len
);
1214 spudec_set_font_factor(vo_spudec
,font_factor
);
1217 if (vo_spudec
!=NULL
) {
1218 mpctx
->initialized_flags
|=INITIALIZED_SPUDEC
;
1219 mp_property_do("sub_forced_only", M_PROPERTY_SET
, &forced_subs_only
, mpctx
);
1224 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1225 * make it all work is to use the builtin SDL-bootstrap code, which
1226 * will be done automatically by replacing our main() if we include SDL.h.
1228 #if defined(__APPLE__) && defined(CONFIG_SDL)
1229 #ifdef CONFIG_SDL_SDL_H
1230 #include <SDL/SDL.h>
1237 * \brief append a formatted string
1238 * \param buf buffer to print into
1239 * \param pos position of terminating 0 in buf
1240 * \param len maximum number of characters in buf, not including terminating 0
1241 * \param format printf format string
1243 static void saddf(char *buf
, unsigned *pos
, int len
, const char *format
, ...)
1246 va_start(va
, format
);
1247 *pos
+= vsnprintf(&buf
[*pos
], len
- *pos
, format
, va
);
1256 * \brief append time in the hh:mm:ss.f format
1257 * \param buf buffer to print into
1258 * \param pos position of terminating 0 in buf
1259 * \param len maximum number of characters in buf, not including terminating 0
1260 * \param time time value to convert/append
1262 static void sadd_hhmmssf(char *buf
, unsigned *pos
, int len
, float time
) {
1263 int64_t tenths
= 10 * time
;
1264 int f1
= tenths
% 10;
1265 int ss
= (tenths
/ 10) % 60;
1266 int mm
= (tenths
/ 600) % 60;
1267 int hh
= tenths
/ 36000;
1269 saddf(buf
, pos
, len
, "unknown");
1273 saddf(buf
, pos
, len
, "%2d:", hh
);
1274 if (hh
> 0 || mm
> 0)
1275 saddf(buf
, pos
, len
, "%02d:", mm
);
1276 saddf(buf
, pos
, len
, "%02d.%1d", ss
, f1
);
1279 static void print_status(struct MPContext
*mpctx
, double a_pos
, bool at_frame
)
1281 struct MPOpts
*opts
= &mpctx
->opts
;
1282 sh_video_t
* const sh_video
= mpctx
->sh_video
;
1284 if (mpctx
->sh_audio
&& a_pos
== MP_NOPTS_VALUE
)
1285 a_pos
= playing_audio_pts(mpctx
);
1286 if (mpctx
->sh_audio
&& sh_video
&& at_frame
) {
1287 mpctx
->last_av_difference
= a_pos
- sh_video
->pts
- audio_delay
;
1288 if (mpctx
->time_frame
> 0)
1289 mpctx
->last_av_difference
+= mpctx
->time_frame
* opts
->playback_speed
;
1290 if (mpctx
->last_av_difference
> 0.5 && drop_frame_cnt
> 50
1291 && !mpctx
->drop_message_shown
) {
1292 mp_tmsg(MSGT_AVSYNC
,MSGL_WARN
,SystemTooSlow
);
1293 mpctx
->drop_message_shown
= true;
1304 if (screen_width
> 0)
1305 width
= screen_width
;
1308 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1309 /* Windows command line is broken (MinGW's rxvt works, but we
1310 * should not depend on that). */
1313 line
= malloc(width
+ 1); // one additional char for the terminating null
1316 if (mpctx
->sh_audio
) {
1317 saddf(line
, &pos
, width
, "A:%6.1f ", a_pos
);
1319 float len
= get_time_length(mpctx
);
1320 saddf(line
, &pos
, width
, "(");
1321 sadd_hhmmssf(line
, &pos
, width
, a_pos
);
1322 saddf(line
, &pos
, width
, ") of %.1f (", len
);
1323 sadd_hhmmssf(line
, &pos
, width
, len
);
1324 saddf(line
, &pos
, width
, ") ");
1330 saddf(line
, &pos
, width
, "V:%6.1f ", sh_video
->pts
);
1333 if (mpctx
->sh_audio
&& sh_video
)
1334 saddf(line
, &pos
, width
, "A-V:%7.3f ct:%7.3f ",
1335 mpctx
->last_av_difference
, mpctx
->total_avsync_change
);
1339 saddf(line
, &pos
, width
, "%3d/%3d ",
1340 (int)sh_video
->num_frames
,
1341 (int)sh_video
->num_frames_decoded
);
1345 if (sh_video
->timer
> 0.5)
1346 saddf(line
, &pos
, width
, "%2d%% %2d%% %4.1f%% ",
1347 (int)(100.0*video_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
),
1348 (int)(100.0*vout_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
),
1349 (100.0*audio_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
));
1351 saddf(line
, &pos
, width
, "??%% ??%% ??,?%% ");
1352 } else if (mpctx
->sh_audio
) {
1353 if (mpctx
->delay
> 0.5)
1354 saddf(line
, &pos
, width
, "%4.1f%% ",
1355 100.0*audio_time_usage
/(double)mpctx
->delay
);
1357 saddf(line
, &pos
, width
, "??,?%% ");
1362 saddf(line
, &pos
, width
, "%d %d ", drop_frame_cnt
, output_quality
);
1364 #ifdef CONFIG_STREAM_CACHE
1366 if (stream_cache_size
> 0)
1367 saddf(line
, &pos
, width
, "%d%% ", cache_fill_status
);
1371 if (opts
->playback_speed
!= 1)
1372 saddf(line
, &pos
, width
, "%4.2fx ", opts
->playback_speed
);
1375 if (erase_to_end_of_line
) {
1377 mp_msg(MSGT_STATUSLINE
, MSGL_STATUS
, "%s%s\r", line
, erase_to_end_of_line
);
1379 memset(&line
[pos
], ' ', width
- pos
);
1381 mp_msg(MSGT_STATUSLINE
, MSGL_STATUS
, "%s\r", line
);
1387 * \brief build a chain of audio filters that converts the input format
1388 * to the ao's format, taking into account the current playback_speed.
1389 * \param sh_audio describes the requested input format of the chain.
1390 * \param ao_data describes the requested output format of the chain.
1392 int build_afilter_chain(struct MPContext
*mpctx
, sh_audio_t
*sh_audio
, ao_data_t
*ao_data
)
1394 struct MPOpts
*opts
= &mpctx
->opts
;
1399 mpctx
->mixer
.afilter
= NULL
;
1402 if(af_control_any_rev(sh_audio
->afilter
,
1403 AF_CONTROL_PLAYBACK_SPEED
| AF_CONTROL_SET
,
1404 &opts
->playback_speed
)) {
1405 new_srate
= sh_audio
->samplerate
;
1407 new_srate
= sh_audio
->samplerate
* opts
->playback_speed
;
1408 if (new_srate
!= ao_data
->samplerate
) {
1409 // limits are taken from libaf/af_resample.c
1410 if (new_srate
< 8000)
1412 if (new_srate
> 192000)
1414 opts
->playback_speed
= (float)new_srate
/ (float)sh_audio
->samplerate
;
1417 result
= init_audio_filters(sh_audio
, new_srate
,
1418 &ao_data
->samplerate
, &ao_data
->channels
, &ao_data
->format
);
1419 mpctx
->mixer
.afilter
= sh_audio
->afilter
;
1424 typedef struct mp_osd_msg mp_osd_msg_t
;
1426 /// Previous message on the stack.
1430 int id
,level
,started
;
1431 /// Display duration in ms.
1435 /// OSD message stack.
1436 static mp_osd_msg_t
* osd_msg_stack
= NULL
;
1439 * \brief Add a message on the OSD message stack
1441 * If a message with the same id is already present in the stack
1442 * it is pulled on top of the stack, otherwise a new message is created.
1445 static void set_osd_msg_va(int id
, int level
, int time
, const char *fmt
,
1448 mp_osd_msg_t
*msg
,*last
=NULL
;
1451 // look if the id is already in the stack
1452 for(msg
= osd_msg_stack
; msg
&& msg
->id
!= id
;
1453 last
= msg
, msg
= msg
->prev
);
1454 // not found: alloc it
1456 msg
= calloc(1,sizeof(mp_osd_msg_t
));
1457 msg
->prev
= osd_msg_stack
;
1458 osd_msg_stack
= msg
;
1459 } else if(last
) { // found, but it's not on top of the stack
1460 last
->prev
= msg
->prev
;
1461 msg
->prev
= osd_msg_stack
;
1462 osd_msg_stack
= msg
;
1465 r
= vsnprintf(msg
->msg
, 128, fmt
, ap
);
1466 if(r
>= 128) msg
->msg
[127] = 0;
1474 void set_osd_msg(int id
, int level
, int time
, const char *fmt
, ...)
1478 set_osd_msg_va(id
, level
, time
, fmt
, ap
);
1482 void set_osd_tmsg(int id
, int level
, int time
, const char *fmt
, ...)
1486 set_osd_msg_va(id
, level
, time
, mp_gtext(fmt
), ap
);
1492 * \brief Remove a message from the OSD stack
1494 * This function can be used to get rid of a message right away.
1498 void rm_osd_msg(int id
) {
1499 mp_osd_msg_t
*msg
,*last
=NULL
;
1501 // Search for the msg
1502 for(msg
= osd_msg_stack
; msg
&& msg
->id
!= id
;
1503 last
= msg
, msg
= msg
->prev
);
1506 // Detach it from the stack and free it
1508 last
->prev
= msg
->prev
;
1510 osd_msg_stack
= msg
->prev
;
1515 * \brief Remove all messages from the OSD stack
1519 static void clear_osd_msgs(void) {
1520 mp_osd_msg_t
* msg
= osd_msg_stack
, *prev
= NULL
;
1526 osd_msg_stack
= NULL
;
1530 * \brief Get the current message from the OSD stack.
1532 * This function decrements the message timer and destroys the old ones.
1533 * The message that should be displayed is returned (if any).
1537 static mp_osd_msg_t
* get_osd_msg(struct MPContext
*mpctx
)
1539 struct MPOpts
*opts
= &mpctx
->opts
;
1540 mp_osd_msg_t
*msg
,*prev
,*last
= NULL
;
1541 static unsigned last_update
= 0;
1542 unsigned now
= GetTimerMS();
1544 char hidden_dec_done
= 0;
1546 if (mpctx
->osd_visible
) {
1547 // 36000000 means max timed visibility is 1 hour into the future, if
1548 // the difference is greater assume it's wrapped around from below 0
1549 if (mpctx
->osd_visible
- now
> 36000000) {
1550 mpctx
->osd_visible
= 0;
1551 vo_osd_progbar_type
= -1; // disable
1552 vo_osd_changed(OSDTYPE_PROGBAR
);
1553 mpctx
->osd_function
= mpctx
->paused
? OSD_PAUSE
: OSD_PLAY
;
1556 if (mpctx
->osd_show_percentage_until
- now
> 36000000)
1557 mpctx
->osd_show_percentage_until
= 0;
1559 if(!last_update
) last_update
= now
;
1560 diff
= now
>= last_update
? now
- last_update
: 0;
1564 // Look for the first message in the stack with high enough level.
1565 for(msg
= osd_msg_stack
; msg
; last
= msg
, msg
= prev
) {
1567 if (msg
->level
> opts
->osd_level
&& hidden_dec_done
)
1569 // The message has a high enough level or it is the first hidden one
1570 // in both cases we decrement the timer or kill it.
1571 if(!msg
->started
|| msg
->time
> diff
) {
1572 if(msg
->started
) msg
->time
-= diff
;
1573 else msg
->started
= 1;
1575 if (msg
->level
<= opts
->osd_level
)
1577 hidden_dec_done
= 1;
1586 osd_msg_stack
= prev
;
1595 * \brief Display the OSD bar.
1597 * Display the OSD bar or fall back on a simple message.
1601 void set_osd_bar(struct MPContext
*mpctx
, int type
,const char* name
,double min
,double max
,double val
) {
1602 struct MPOpts
*opts
= &mpctx
->opts
;
1603 if (opts
->osd_level
< 1)
1606 if(mpctx
->sh_video
) {
1607 mpctx
->osd_visible
= (GetTimerMS() + 1000) | 1;
1608 vo_osd_progbar_type
= type
;
1609 vo_osd_progbar_value
= 256*(val
-min
)/(max
-min
);
1610 vo_osd_changed(OSDTYPE_PROGBAR
);
1614 set_osd_msg(OSD_MSG_BAR
, 1, opts
->osd_duration
, "%s: %d %%",
1615 name
, ROUND(100*(val
-min
)/(max
-min
)));
1619 * \brief Display text subtitles on the OSD
1621 void set_osd_subtitle(struct MPContext
*mpctx
, subtitle
*subs
)
1625 vo_osd_changed(OSDTYPE_SUBTITLE
);
1626 if (!mpctx
->sh_video
) {
1627 // reverse order, since newest set_osd_msg is displayed first
1628 for (i
= SUB_MAX_TEXT
- 1; i
>= 0; i
--) {
1629 if (!subs
|| i
>= subs
->lines
|| !subs
->text
[i
])
1630 rm_osd_msg(OSD_MSG_SUB_BASE
+ i
);
1632 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1633 int display_time
= i
== subs
->lines
- 1 ? 180000 : 2000;
1634 set_osd_msg(OSD_MSG_SUB_BASE
+ i
, 1, display_time
, "%s", subs
->text
[i
]);
1641 * \brief Update the OSD message line.
1643 * This function displays the current message on the vo OSD or on the term.
1644 * If the stack is empty and the OSD level is high enough the timer
1645 * is displayed (only on the vo OSD).
1649 static void update_osd_msg(struct MPContext
*mpctx
)
1651 struct MPOpts
*opts
= &mpctx
->opts
;
1653 struct osd_state
*osd
= mpctx
->osd
;
1654 char osd_text_timer
[128];
1656 if (mpctx
->add_osd_seek_info
) {
1657 double percentage
= get_percent_pos(mpctx
);
1658 set_osd_bar(mpctx
, 0, "Position", 0, 100, percentage
);
1659 if (mpctx
->sh_video
)
1660 mpctx
->osd_show_percentage_until
= (GetTimerMS() + 1000) | 1;
1661 mpctx
->add_osd_seek_info
= false;
1664 // Look if we have a msg
1665 if((msg
= get_osd_msg(mpctx
))) {
1666 if (strcmp(osd
->osd_text
, msg
->msg
)) {
1667 strncpy(osd
->osd_text
, msg
->msg
, 127);
1668 if(mpctx
->sh_video
) vo_osd_changed(OSDTYPE_OSD
); else
1669 if(term_osd
) mp_msg(MSGT_CPLAYER
,MSGL_STATUS
,"%s%s\n",term_osd_esc
,msg
->msg
);
1674 if(mpctx
->sh_video
) {
1675 // fallback on the timer
1676 if (opts
->osd_level
>= 2) {
1677 int len
= get_time_length(mpctx
);
1678 int percentage
= -1;
1679 char percentage_text
[10];
1680 int pts
= get_current_time(mpctx
);
1682 if (mpctx
->osd_show_percentage_until
)
1683 percentage
= get_percent_pos(mpctx
);
1685 if (percentage
>= 0)
1686 snprintf(percentage_text
, 9, " (%d%%)", percentage
);
1688 percentage_text
[0] = 0;
1690 if (opts
->osd_level
== 3)
1691 snprintf(osd_text_timer
, 63,
1692 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1693 mpctx
->osd_function
,pts
/3600,(pts
/60)%60,pts
%60,
1694 len
/3600,(len
/60)%60,len
%60,percentage_text
);
1696 snprintf(osd_text_timer
, 63, "%c %02d:%02d:%02d%s",
1697 mpctx
->osd_function
,pts
/3600,(pts
/60)%60,
1698 pts
%60,percentage_text
);
1700 osd_text_timer
[0]=0;
1702 if (strcmp(osd
->osd_text
, osd_text_timer
)) {
1703 strncpy(osd
->osd_text
, osd_text_timer
, 63);
1704 vo_osd_changed(OSDTYPE_OSD
);
1709 // Clear the term osd line
1710 if (term_osd
&& osd
->osd_text
[0]) {
1711 osd
->osd_text
[0] = 0;
1712 printf("%s\n",term_osd_esc
);
1720 void reinit_audio_chain(struct MPContext
*mpctx
)
1722 struct MPOpts
*opts
= &mpctx
->opts
;
1723 if (!mpctx
->sh_audio
)
1725 if (!(mpctx
->initialized_flags
& INITIALIZED_ACODEC
)) {
1726 current_module
="init_audio_codec";
1727 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
1728 if(!init_best_audio_codec(mpctx
->sh_audio
,audio_codec_list
,audio_fm_list
)){
1731 mpctx
->initialized_flags
|=INITIALIZED_ACODEC
;
1732 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
1736 current_module
="af_preinit";
1737 if (!(mpctx
->initialized_flags
& INITIALIZED_AO
)) {
1738 ao_data
.samplerate
=force_srate
;
1740 ao_data
.format
= opts
->audio_output_format
;
1742 // first init to detect best values
1743 if(!init_audio_filters(mpctx
->sh_audio
, // preliminary init
1745 mpctx
->sh_audio
->samplerate
,
1747 &ao_data
.samplerate
, &ao_data
.channels
, &ao_data
.format
)){
1748 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
, "Error at audio filter chain "
1750 exit_player(mpctx
, EXIT_ERROR
);
1752 if (!(mpctx
->initialized_flags
& INITIALIZED_AO
)) {
1753 current_module
="ao2_init";
1754 ao_data
.buffersize
= opts
->ao_buffersize
;
1755 mpctx
->audio_out
= init_best_audio_out(opts
->audio_driver_list
,
1760 if(!mpctx
->audio_out
){
1761 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Could not open/initialize audio device -> no sound.\n");
1764 mpctx
->initialized_flags
|=INITIALIZED_AO
;
1765 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1766 mpctx
->audio_out
->info
->short_name
,
1767 ao_data
.samplerate
, ao_data
.channels
,
1768 af_fmt2str_short(ao_data
.format
),
1769 af_fmt2bits(ao_data
.format
)/8 );
1770 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AO: Description: %s\nAO: Author: %s\n",
1771 mpctx
->audio_out
->info
->name
, mpctx
->audio_out
->info
->author
);
1772 if(strlen(mpctx
->audio_out
->info
->comment
) > 0)
1773 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AO: Comment: %s\n", mpctx
->audio_out
->info
->comment
);
1776 // init audio filters:
1777 current_module
="af_init";
1778 if(!build_afilter_chain(mpctx
, mpctx
->sh_audio
, &ao_data
)) {
1779 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Couldn't find matching filter/ao format!\n");
1782 mpctx
->mixer
.audio_out
= mpctx
->audio_out
;
1783 mpctx
->mixer
.volstep
= volstep
;
1784 mpctx
->syncing_audio
= true;
1788 uninit_player(mpctx
, INITIALIZED_ACODEC
|INITIALIZED_AO
); // close codec and possibly AO
1789 mpctx
->sh_audio
=mpctx
->d_audio
->sh
=NULL
; // -> nosound
1790 mpctx
->d_audio
->id
= -2;
1798 // Return pts value corresponding to the end point of audio written to the
1800 static double written_audio_pts(struct MPContext
*mpctx
)
1802 sh_audio_t
*sh_audio
= mpctx
->sh_audio
;
1803 demux_stream_t
*d_audio
= mpctx
->d_audio
;
1804 double buffered_output
;
1805 // first calculate the end pts of audio that has been output by decoder
1806 double a_pts
= sh_audio
->pts
;
1807 if (a_pts
!= MP_NOPTS_VALUE
)
1808 // Good, decoder supports new way of calculating audio pts.
1809 // sh_audio->pts is the timestamp of the latest input packet with
1810 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1811 // the amount of bytes the decoder has written after that timestamp.
1812 a_pts
+= sh_audio
->pts_bytes
/ (double) sh_audio
->o_bps
;
1814 // Decoder doesn't support new way of calculating pts (or we're
1815 // being called before it has decoded anything with known timestamp).
1816 // Use the old method of audio pts calculation: take the timestamp
1817 // of last packet with known pts the decoder has read data from,
1818 // and add amount of bytes read after the beginning of that packet
1819 // divided by input bps. This will be inaccurate if the input/output
1820 // ratio is not constant for every audio packet or if it is constant
1821 // but not accurately known in sh_audio->i_bps.
1823 a_pts
= d_audio
->pts
;
1824 // ds_tell_pts returns bytes read after last timestamp from
1825 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1826 // it has read but not decoded
1827 if (sh_audio
->i_bps
)
1828 a_pts
+= (ds_tell_pts(d_audio
) - sh_audio
->a_in_buffer_len
) /
1829 (double)sh_audio
->i_bps
;
1831 // Now a_pts hopefully holds the pts for end of audio from decoder.
1832 // Substract data in buffers between decoder and audio out.
1834 // Decoded but not filtered
1835 a_pts
-= sh_audio
->a_buffer_len
/ (double)sh_audio
->o_bps
;
1837 // Data buffered in audio filters, measured in bytes of "missing" output
1838 buffered_output
= af_calc_delay(sh_audio
->afilter
);
1840 // Data that was ready for ao but was buffered because ao didn't fully
1841 // accept everything to internal buffers yet
1842 buffered_output
+= sh_audio
->a_out_buffer_len
;
1844 // Filters divide audio length by playback_speed, so multiply by it
1845 // to get the length in original units without speedup or slowdown
1846 a_pts
-= buffered_output
* mpctx
->opts
.playback_speed
/ ao_data
.bps
;
1848 return a_pts
+ mpctx
->video_offset
;
1851 // Return pts value corresponding to currently playing audio.
1852 double playing_audio_pts(struct MPContext
*mpctx
)
1854 return written_audio_pts(mpctx
) - mpctx
->opts
.playback_speed
*
1855 mpctx
->audio_out
->get_delay();
1858 static int check_framedrop(struct MPContext
*mpctx
, double frame_time
) {
1859 struct MPOpts
*opts
= &mpctx
->opts
;
1860 // check for frame-drop:
1861 current_module
= "check_framedrop";
1862 if (mpctx
->sh_audio
&& !mpctx
->d_audio
->eof
) {
1863 static int dropped_frames
;
1864 float delay
= opts
->playback_speed
*mpctx
->audio_out
->get_delay();
1865 float d
= delay
-mpctx
->delay
;
1867 // we should avoid dropping too many frames in sequence unless we
1868 // are too late. and we allow 100ms A-V delay here:
1869 if (d
< -dropped_frames
*frame_time
-0.100 && !mpctx
->paused
1870 && !mpctx
->restart_playback
) {
1873 return frame_dropping
;
1885 static float timing_sleep(struct MPContext
*mpctx
, float time_frame
)
1889 // -------- RTC -----------
1890 current_module
="sleep_rtc";
1891 while (time_frame
> 0.000) {
1892 unsigned long rtc_ts
;
1893 if (read(rtc_fd
, &rtc_ts
, sizeof(rtc_ts
)) <= 0)
1894 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Linux RTC read error: %s\n", strerror(errno
));
1895 time_frame
-= get_relative_time(mpctx
);
1900 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1901 // unnecessarily high CPU usage
1902 struct MPOpts
*opts
= &mpctx
->opts
;
1903 float margin
= opts
->softsleep
? 0.011 : 0;
1904 current_module
= "sleep_timer";
1905 while (time_frame
> margin
) {
1906 usec_sleep(1000000 * (time_frame
- margin
));
1907 time_frame
-= get_relative_time(mpctx
);
1909 if (opts
->softsleep
){
1910 current_module
= "sleep_soft";
1912 mp_tmsg(MSGT_AVSYNC
, MSGL_WARN
, "Warning! Softsleep underflow!\n");
1913 while (time_frame
> 0)
1914 time_frame
-= get_relative_time(mpctx
); // burn the CPU
1920 static int select_subtitle(MPContext
*mpctx
)
1922 struct MPOpts
*opts
= &mpctx
->opts
;
1923 // find the best sub to use
1926 mpctx
->global_sub_pos
= -1; // no subs by default
1927 if (vobsub_id
>= 0) {
1928 // if user asks for a vobsub id, use that first.
1930 found
= mp_property_do("sub_vob", M_PROPERTY_SET
, &id
, mpctx
) == M_PROPERTY_OK
;
1933 if (!found
&& opts
->sub_id
>= 0) {
1934 // if user asks for a dvd sub id, use that next.
1936 found
= mp_property_do("sub_demux", M_PROPERTY_SET
, &id
, mpctx
) == M_PROPERTY_OK
;
1940 // if there are text subs to use, use those. (autosubs come last here)
1942 found
= mp_property_do("sub_file", M_PROPERTY_SET
, &id
, mpctx
) == M_PROPERTY_OK
;
1945 if (!found
&& opts
->sub_id
== -1) {
1946 // finally select subs by language and container hints
1947 if (opts
->sub_id
== -1)
1949 demuxer_sub_track_by_lang_and_default(mpctx
->d_sub
->demuxer
,
1951 if (opts
->sub_id
>= 0) {
1953 found
= mp_property_do("sub_demux", M_PROPERTY_SET
, &id
, mpctx
) == M_PROPERTY_OK
;
1959 #ifdef CONFIG_DVDNAV
1963 /// store decoded video image
1964 static mp_image_t
* mp_dvdnav_copy_mpi(mp_image_t
*to_mpi
,
1965 mp_image_t
*from_mpi
) {
1968 /// Do not store B-frames
1969 if (from_mpi
->pict_type
== FF_B_TYPE
)
1973 to_mpi
->w
== from_mpi
->w
&&
1974 to_mpi
->h
== from_mpi
->h
&&
1975 to_mpi
->imgfmt
== from_mpi
->imgfmt
)
1979 free_mp_image(to_mpi
);
1980 if (from_mpi
->w
== 0 || from_mpi
->h
== 0)
1982 mpi
= alloc_mpi(from_mpi
->w
,from_mpi
->h
,from_mpi
->imgfmt
);
1985 copy_mpi(mpi
,from_mpi
);
1989 static void mp_dvdnav_reset_stream (MPContext
*ctx
) {
1990 struct MPOpts
*opts
= &ctx
->opts
;
1991 if (ctx
->sh_video
) {
1993 ctx
->d_video
->pts
= 0.0f
;
1994 ctx
->sh_video
->pts
= 0.0f
;
1995 ctx
->sh_video
->i_pts
= 0.0f
;
1996 ctx
->sh_video
->last_pts
= 0.0f
;
1997 ctx
->sh_video
->num_buffered_pts
= 0;
1998 ctx
->sh_video
->num_frames
= 0;
1999 ctx
->sh_video
->num_frames_decoded
= 0;
2000 ctx
->sh_video
->timer
= 0.0f
;
2001 ctx
->sh_video
->stream_delay
= 0.0f
;
2002 ctx
->sh_video
->timer
= 0;
2003 ctx
->demuxer
->stream_pts
= MP_NOPTS_VALUE
;
2006 if (ctx
->sh_audio
) {
2007 /// free audio packets and reset
2008 ds_free_packs(ctx
->d_audio
);
2009 audio_delay
-= ctx
->sh_audio
->stream_delay
;
2010 ctx
->delay
=- audio_delay
;
2011 ctx
->audio_out
->reset();
2012 resync_audio_stream(ctx
->sh_audio
);
2016 ctx
->sub_counts
[SUB_SOURCE_DEMUX
] = mp_dvdnav_number_of_subs(ctx
->stream
);
2017 if (opts
->sub_lang
&& opts
->sub_id
== dvdsub_lang_id
) {
2018 dvdsub_lang_id
= mp_dvdnav_sid_from_lang(ctx
->stream
, opts
->sub_lang
);
2019 if (dvdsub_lang_id
!= opts
->sub_id
) {
2020 opts
->sub_id
= dvdsub_lang_id
;
2021 select_subtitle(ctx
);
2025 /// clear all EOF related flags
2026 ctx
->d_video
->eof
= ctx
->d_audio
->eof
= ctx
->stream
->eof
= 0;
2029 /// Restore last decoded DVDNAV (still frame)
2030 static mp_image_t
*mp_dvdnav_restore_smpi(struct MPContext
*mpctx
,
2032 unsigned char **start
,
2033 mp_image_t
*decoded_frame
)
2035 if (mpctx
->stream
->type
!= STREAMTYPE_DVDNAV
)
2036 return decoded_frame
;
2038 /// a change occured in dvdnav stream
2039 if (mp_dvdnav_cell_has_changed(mpctx
->stream
,0)) {
2040 mp_dvdnav_read_wait(mpctx
->stream
, 1, 1);
2041 mp_dvdnav_context_free(mpctx
);
2042 mp_dvdnav_reset_stream(mpctx
);
2043 mp_dvdnav_read_wait(mpctx
->stream
, 0, 1);
2044 mp_dvdnav_cell_has_changed(mpctx
->stream
,1);
2050 /// Display still frame, if any
2051 if (mpctx
->nav_smpi
&& !mpctx
->nav_buffer
)
2052 decoded_frame
= mpctx
->nav_smpi
;
2054 /// increment video frame : continue playing after still frame
2055 len
= get_time_length(mpctx
);
2056 if (mpctx
->sh_video
->pts
>= len
&&
2057 mpctx
->sh_video
->pts
> 0.0 && len
> 0.0) {
2058 mp_dvdnav_skip_still(mpctx
->stream
);
2059 mp_dvdnav_skip_wait(mpctx
->stream
);
2061 mpctx
->sh_video
->pts
+= 1 / mpctx
->sh_video
->fps
;
2063 if (mpctx
->nav_buffer
) {
2064 *start
= mpctx
->nav_start
;
2065 *in_size
= mpctx
->nav_in_size
;
2066 if (mpctx
->nav_start
)
2067 memcpy(*start
,mpctx
->nav_buffer
,mpctx
->nav_in_size
);
2071 return decoded_frame
;
2074 /// Save last decoded DVDNAV (still frame)
2075 static void mp_dvdnav_save_smpi(struct MPContext
*mpctx
, int in_size
,
2076 unsigned char *start
,
2077 mp_image_t
*decoded_frame
)
2079 if (mpctx
->stream
->type
!= STREAMTYPE_DVDNAV
)
2082 free(mpctx
->nav_buffer
);
2084 mpctx
->nav_buffer
= malloc(in_size
);
2085 mpctx
->nav_start
= start
;
2086 mpctx
->nav_in_size
= mpctx
->nav_buffer
? in_size
: -1;
2087 if (mpctx
->nav_buffer
)
2088 memcpy(mpctx
->nav_buffer
,start
,in_size
);
2090 if (decoded_frame
&& mpctx
->nav_smpi
!= decoded_frame
)
2091 mpctx
->nav_smpi
= mp_dvdnav_copy_mpi(mpctx
->nav_smpi
,decoded_frame
);
2093 #endif /* CONFIG_DVDNAV */
2095 /* Modify video timing to match the audio timeline. There are two main
2096 * reasons this is needed. First, video and audio can start from different
2097 * positions at beginning of file or after a seek (MPlayer starts both
2098 * immediately even if they have different pts). Second, the file can have
2099 * audio timestamps that are inconsistent with the duration of the audio
2100 * packets, for example two consecutive timestamp values differing by
2101 * one second but only a packet with enough samples for half a second
2102 * of playback between them.
2104 static void adjust_sync(struct MPContext
*mpctx
, double frame_time
)
2106 current_module
= "av_sync";
2108 if (!mpctx
->sh_audio
|| mpctx
->syncing_audio
)
2111 double a_pts
= written_audio_pts(mpctx
) - mpctx
->delay
;
2112 double v_pts
= mpctx
->sh_video
->pts
;
2113 double av_delay
= a_pts
- v_pts
;
2114 // Try to sync vo_flip() so it will *finish* at given time
2115 av_delay
+= mpctx
->last_vo_flip_duration
;
2116 av_delay
-= audio_delay
; // This much pts difference is desired
2118 double change
= av_delay
* 0.1;
2119 double max_change
= default_max_pts_correction
>= 0 ?
2120 default_max_pts_correction
: frame_time
* 0.1;
2121 if (change
< -max_change
)
2122 change
= -max_change
;
2123 else if (change
> max_change
)
2124 change
= max_change
;
2125 mpctx
->delay
+= change
;
2126 mpctx
->total_avsync_change
+= change
;
2129 #define ASYNC_PLAY_DONE -3
2130 static int audio_start_sync(struct MPContext
*mpctx
, int playsize
)
2132 struct MPOpts
*opts
= &mpctx
->opts
;
2133 sh_audio_t
* const sh_audio
= mpctx
->sh_audio
;
2136 // Timing info may not be set without
2137 res
= decode_audio(sh_audio
, 1);
2140 double ptsdiff
= written_audio_pts(mpctx
) - mpctx
->sh_video
->pts
-
2141 mpctx
->delay
- audio_delay
;
2142 int bytes
= ptsdiff
* ao_data
.bps
/ mpctx
->opts
.playback_speed
;
2143 bytes
-= bytes
% (ao_data
.channels
* af_fmt2bits(ao_data
.format
) / 8);
2145 if (fabs(ptsdiff
) > 300) // pts reset or just broken?
2149 mpctx
->syncing_audio
= false;
2151 int a
= FFMIN(-bytes
, FFMAX(playsize
, 20000));
2152 int res
= decode_audio(sh_audio
, a
);
2153 bytes
+= sh_audio
->a_out_buffer_len
;
2155 memmove(sh_audio
->a_out_buffer
,
2156 sh_audio
->a_out_buffer
+
2157 sh_audio
->a_out_buffer_len
- bytes
,
2159 sh_audio
->a_out_buffer_len
= bytes
;
2162 return decode_audio(sh_audio
, playsize
);
2164 sh_audio
->a_out_buffer_len
= 0;
2170 if ((ao_data
.format
& AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
2172 if (bytes
>= playsize
) {
2173 /* This case could fall back to the one below with
2174 * bytes = playsize, but then silence would keep accumulating
2175 * in a_out_buffer if the AO accepts less data than it asks for
2177 char *p
= malloc(playsize
);
2178 memset(p
, fillbyte
, playsize
);
2179 playsize
= mpctx
->audio_out
->play(p
, playsize
, 0);
2181 mpctx
->delay
+= opts
->playback_speed
*playsize
/(double)ao_data
.bps
;
2182 return ASYNC_PLAY_DONE
;
2184 mpctx
->syncing_audio
= false;
2185 decode_audio_prepend_bytes(sh_audio
, bytes
, fillbyte
);
2186 return decode_audio(sh_audio
, playsize
);
2190 static int fill_audio_out_buffers(struct MPContext
*mpctx
)
2192 struct MPOpts
*opts
= &mpctx
->opts
;
2198 bool format_change
= false;
2199 sh_audio_t
* const sh_audio
= mpctx
->sh_audio
;
2201 current_module
="play_audio";
2205 // all the current uses of ao_data.pts seem to be in aos that handle
2206 // sync completely wrong; there should be no need to use ao_data.pts
2208 ao_data
.pts
= ((mpctx
->sh_video
?mpctx
->sh_video
->timer
:0)+mpctx
->delay
)*90000.0;
2209 playsize
= mpctx
->audio_out
->get_space();
2210 if (mpctx
->sh_video
|| playsize
>= ao_data
.outburst
)
2213 // handle audio-only case:
2214 // this is where mplayer sleeps during audio-only playback
2215 // to avoid 100% CPU use
2216 sleep_time
= (ao_data
.outburst
- playsize
) * 1000 / ao_data
.bps
;
2217 if (sleep_time
< 10) sleep_time
= 10; // limit to 100 wakeups per second
2218 usec_sleep(sleep_time
* 1000);
2221 // Fill buffer if needed:
2222 current_module
="decode_audio";
2225 if (!opts
->initial_audio_sync
|| (ao_data
.format
& AF_FORMAT_SPECIAL_MASK
))
2226 mpctx
->syncing_audio
= false;
2229 if (mpctx
->syncing_audio
&& mpctx
->sh_video
)
2230 res
= audio_start_sync(mpctx
, playsize
);
2232 res
= decode_audio(sh_audio
, playsize
);
2233 if (res
< 0) { // EOF, error or format change
2235 format_change
= true;
2236 else if (res
== ASYNC_PLAY_DONE
)
2238 else if (mpctx
->d_audio
->eof
) {
2240 int unitsize
= ao_data
.channels
* af_fmt2bits(ao_data
.format
) / 8;
2241 if (sh_audio
->a_out_buffer_len
< unitsize
)
2246 tt
= t
*0.000001f
; audio_time_usage
+=tt
;
2247 if (playsize
> sh_audio
->a_out_buffer_len
) {
2248 playsize
= sh_audio
->a_out_buffer_len
;
2250 playflags
|= AOPLAY_FINAL_CHUNK
;
2256 current_module
="play_audio";
2258 // Is this pts value actually useful for the aos that access it?
2259 // They're obviously badly broken in the way they handle av sync;
2260 // would not having access to this make them more broken?
2261 ao_data
.pts
= ((mpctx
->sh_video
?mpctx
->sh_video
->timer
:0)+mpctx
->delay
)*90000.0;
2262 playsize
= mpctx
->audio_out
->play(sh_audio
->a_out_buffer
, playsize
, playflags
);
2265 sh_audio
->a_out_buffer_len
-= playsize
;
2266 memmove(sh_audio
->a_out_buffer
, &sh_audio
->a_out_buffer
[playsize
],
2267 sh_audio
->a_out_buffer_len
);
2268 mpctx
->delay
+= opts
->playback_speed
*playsize
/(double)ao_data
.bps
;
2270 else if (audio_eof
&& mpctx
->audio_out
->get_delay() < .04) {
2271 // Sanity check to avoid hanging in case current ao doesn't output
2272 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2273 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, "Audio output truncated at end.\n");
2274 sh_audio
->a_out_buffer_len
= 0;
2277 /* The format change isn't handled too gracefully. A more precise
2278 * implementation would require draining buffered old-format audio
2279 * while displaying video, then doing the output format switch.
2281 if (format_change
) {
2282 uninit_player(mpctx
, INITIALIZED_AO
);
2283 reinit_audio_chain(mpctx
);
2289 static int sleep_until_update(struct MPContext
*mpctx
, float *time_frame
,
2290 float *aq_sleep_time
)
2292 struct MPOpts
*opts
= &mpctx
->opts
;
2293 int frame_time_remaining
= 0;
2294 current_module
="calc_sleep_time";
2296 *time_frame
-= get_relative_time(mpctx
); // reset timer
2298 if (mpctx
->sh_audio
&& !mpctx
->d_audio
->eof
) {
2299 float delay
= mpctx
->audio_out
->get_delay();
2300 mp_dbg(MSGT_AVSYNC
, MSGL_DBG2
, "delay=%f\n", delay
);
2302 if (opts
->autosync
) {
2304 * Adjust this raw delay value by calculating the expected
2305 * delay for this frame and generating a new value which is
2306 * weighted between the two. The higher autosync is, the
2307 * closer to the delay value gets to that which "-nosound"
2308 * would have used, and the longer it will take for A/V
2309 * sync to settle at the right value (but it eventually will.)
2310 * This settling time is very short for values below 100.
2312 float predicted
= mpctx
->delay
/ opts
->playback_speed
+ *time_frame
;
2313 float difference
= delay
- predicted
;
2314 delay
= predicted
+ difference
/ (float)opts
->autosync
;
2317 *time_frame
= delay
- mpctx
->delay
/ opts
->playback_speed
;
2318 *time_frame
-= mpctx
->video_out
->flip_queue_offset
;
2320 // delay = amount of audio buffered in soundcard/driver
2321 if (delay
> 0.25) delay
=0.25; else
2322 if (delay
< 0.10) delay
=0.10;
2324 if (*time_frame
> delay
*0.6) {
2325 // sleep time too big - may cause audio drops (buffer underrun)
2326 frame_time_remaining
= 1;
2327 *time_frame
= delay
*0.5;
2330 // If we're lagging more than 200 ms behind the right playback rate,
2331 // don't try to "catch up".
2332 // If benchmark is set always output frames as fast as possible
2333 // without sleeping.
2334 if (*time_frame
< -0.2 || benchmark
)
2336 *time_frame
-= mpctx
->video_out
->flip_queue_offset
;
2339 *aq_sleep_time
+= *time_frame
;
2342 //============================== SLEEP: ===================================
2344 // flag 256 means: libvo driver does its timing (dvb card)
2345 if (*time_frame
> 0.001 && !(mpctx
->sh_video
->output_flags
&256))
2346 *time_frame
= timing_sleep(mpctx
, *time_frame
);
2347 *time_frame
+= mpctx
->video_out
->flip_queue_offset
;
2348 return frame_time_remaining
;
2351 int reinit_video_chain(struct MPContext
*mpctx
)
2353 struct MPOpts
*opts
= &mpctx
->opts
;
2354 sh_video_t
* const sh_video
= mpctx
->sh_video
;
2356 //================== Init VIDEO (codec & libvo) ==========================
2357 if (!opts
->fixed_vo
|| !(mpctx
->initialized_flags
& INITIALIZED_VO
)) {
2358 current_module
="preinit_libvo";
2360 //shouldn't we set dvideo->id=-2 when we fail?
2361 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2362 if(!(mpctx
->video_out
=init_best_video_out(opts
, mpctx
->x11_state
, mpctx
->key_fifo
, mpctx
->input
))){
2363 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"Error opening/initializing the selected video_out (-vo) device.\n");
2366 mpctx
->initialized_flags
|=INITIALIZED_VO
;
2369 if(stream_control(mpctx
->demuxer
->stream
, STREAM_CTRL_GET_ASPECT_RATIO
, &ar
) != STREAM_UNSUPPORTED
)
2370 mpctx
->sh_video
->stream_aspect
= ar
;
2371 current_module
="init_video_filters";
2373 char* vf_arg
[] = { "_oldargs_", (char*)mpctx
->video_out
, NULL
};
2374 sh_video
->vfilter
= vf_open_filter(opts
, NULL
,"vo",vf_arg
);
2378 char* vf_arg
[] = { "_oldargs_", menu_root
, NULL
};
2379 vf_menu
= vf_open_plugin(opts
,libmenu_vfs
,sh_video
->vfilter
,"menu",vf_arg
);
2381 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Can't open libmenu video filter with root menu %s.\n",menu_root
);
2386 sh_video
->vfilter
= vf_menu
;
2390 if(opts
->ass_enabled
) {
2393 if (opts
->vf_settings
)
2394 for (i
= 0; opts
->vf_settings
[i
].name
; ++i
)
2395 if (strcmp(opts
->vf_settings
[i
].name
, "ass") == 0) {
2400 extern vf_info_t vf_info_ass
;
2401 const vf_info_t
* libass_vfs
[] = {&vf_info_ass
, NULL
};
2402 char* vf_arg
[] = {"auto", "1", NULL
};
2404 struct vf_instance
*vf_ass
= vf_open_plugin_noerr(opts
, libass_vfs
,
2409 sh_video
->vfilter
= vf_ass
;
2410 else if (retcode
== -1) // vf_ass open() returns -1 if there's VO EOSD
2411 mp_msg(MSGT_CPLAYER
, MSGL_V
, "[ass] vf_ass not needed\n");
2413 mp_msg(MSGT_CPLAYER
,MSGL_ERR
, "ASS: cannot add video filter\n");
2418 sh_video
->vfilter
= append_filters(sh_video
->vfilter
, opts
->vf_settings
);
2421 if (opts
->ass_enabled
)
2422 sh_video
->vfilter
->control(sh_video
->vfilter
, VFCTRL_INIT_EOSD
, ass_library
);
2425 current_module
="init_video_codec";
2427 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
2428 init_best_video_codec(sh_video
,video_codec_list
,video_fm_list
);
2429 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
2431 if(!sh_video
->initialized
){
2432 if(!opts
->fixed_vo
) uninit_player(mpctx
, INITIALIZED_VO
);
2436 mpctx
->initialized_flags
|=INITIALIZED_VCODEC
;
2438 if (sh_video
->codec
)
2439 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_CODEC=%s\n", sh_video
->codec
->name
);
2441 sh_video
->last_pts
= MP_NOPTS_VALUE
;
2442 sh_video
->num_buffered_pts
= 0;
2443 sh_video
->next_frame_time
= 0;
2446 // Auto quality option enabled
2447 output_quality
=get_video_quality_max(sh_video
);
2448 if(auto_quality
>output_quality
) auto_quality
=output_quality
;
2449 else output_quality
=auto_quality
;
2450 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AutoQ: setting quality to %d.\n",output_quality
);
2451 set_video_quality(sh_video
,output_quality
);
2454 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2456 current_module
="init_vo";
2461 mpctx
->sh_video
= mpctx
->d_video
->sh
= NULL
;
2465 static double update_video_nocorrect_pts(struct MPContext
*mpctx
)
2467 struct sh_video
*sh_video
= mpctx
->sh_video
;
2468 double frame_time
= 0;
2469 struct vo
*video_out
= mpctx
->video_out
;
2470 while (!video_out
->frame_loaded
) {
2471 current_module
= "filter_video";
2472 // In nocorrect-pts mode there is no way to properly time these frames
2473 if (vo_get_buffered_frame(video_out
, 0) >= 0)
2475 if (vf_output_queued_frame(sh_video
->vfilter
))
2477 unsigned char *packet
= NULL
;
2478 frame_time
= sh_video
->next_frame_time
;
2479 if (mpctx
->restart_playback
)
2481 int in_size
= video_read_frame(sh_video
, &sh_video
->next_frame_time
,
2482 &packet
, force_fps
);
2484 #ifdef CONFIG_DVDNAV
2485 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
2486 if (mp_dvdnav_is_eof(mpctx
->stream
))
2489 mpctx
->d_video
->eof
= 0;
2491 mpctx
->d_audio
->eof
= 0;
2492 mpctx
->stream
->eof
= 0;
2497 if (in_size
> max_framesize
)
2498 max_framesize
= in_size
;
2499 sh_video
->timer
+= frame_time
;
2500 if (mpctx
->sh_audio
)
2501 mpctx
->delay
-= frame_time
;
2502 // video_read_frame can change fps (e.g. for ASF video)
2503 vo_fps
= sh_video
->fps
;
2504 int framedrop_type
= check_framedrop(mpctx
, frame_time
);
2505 current_module
= "decode video";
2507 void *decoded_frame
;
2508 #ifdef CONFIG_DVDNAV
2509 decoded_frame
= mp_dvdnav_restore_smpi(mpctx
, &in_size
, &packet
, NULL
);
2510 if (in_size
>= 0 && !decoded_frame
)
2512 decoded_frame
= decode_video(sh_video
, packet
, in_size
, framedrop_type
,
2514 #ifdef CONFIG_DVDNAV
2515 // Save last still frame for future display
2516 mp_dvdnav_save_smpi(mpctx
, in_size
, packet
, decoded_frame
);
2518 if (decoded_frame
) {
2519 current_module
= "filter video";
2520 if (filter_video(sh_video
, decoded_frame
, sh_video
->pts
))
2521 if (!video_out
->config_ok
)
2528 static void determine_frame_pts(struct MPContext
*mpctx
)
2530 struct sh_video
*sh_video
= mpctx
->sh_video
;
2531 struct MPOpts
*opts
= &mpctx
->opts
;
2533 if (opts
->user_pts_assoc_mode
) {
2534 sh_video
->pts_assoc_mode
= opts
->user_pts_assoc_mode
;
2535 } else if (sh_video
->pts_assoc_mode
== 0) {
2536 if (sh_video
->codec_reordered_pts
!= MP_NOPTS_VALUE
)
2537 sh_video
->pts_assoc_mode
= 1;
2539 sh_video
->pts_assoc_mode
= 2;
2541 int probcount1
= sh_video
->num_reordered_pts_problems
;
2542 int probcount2
= sh_video
->num_sorted_pts_problems
;
2543 if (sh_video
->pts_assoc_mode
== 2) {
2544 int tmp
= probcount1
;
2545 probcount1
= probcount2
;
2548 if (probcount1
>= probcount2
* 1.5 + 2) {
2549 sh_video
->pts_assoc_mode
= 3 - sh_video
->pts_assoc_mode
;
2550 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Switching to pts association mode "
2551 "%d.\n", sh_video
->pts_assoc_mode
);
2554 sh_video
->pts
= sh_video
->pts_assoc_mode
== 1 ?
2555 sh_video
->codec_reordered_pts
: sh_video
->sorted_pts
;
2558 static double update_video(struct MPContext
*mpctx
)
2560 struct sh_video
*sh_video
= mpctx
->sh_video
;
2561 struct vo
*video_out
= mpctx
->video_out
;
2562 sh_video
->vfilter
->control(sh_video
->vfilter
, VFCTRL_SET_OSD_OBJ
,
2563 mpctx
->osd
); // hack for vf_expand
2564 if (!mpctx
->opts
.correct_pts
)
2565 return update_video_nocorrect_pts(mpctx
);
2569 bool hit_eof
= false;
2570 while (!video_out
->frame_loaded
) {
2571 current_module
= "filter_video";
2572 if (vo_get_buffered_frame(video_out
, hit_eof
) >= 0)
2574 // XXX Time used in this call is not counted in any performance
2575 // timer now, OSD time is not updated correctly for filter-added frames
2576 if (vf_output_queued_frame(sh_video
->vfilter
))
2580 unsigned char *packet
= NULL
;
2581 int in_size
= ds_get_packet_pts(mpctx
->d_video
, &packet
, &pts
);
2582 if (pts
!= MP_NOPTS_VALUE
)
2583 pts
+= mpctx
->video_offset
;
2585 // try to extract last frames in case of decoder lag
2587 pts
= MP_NOPTS_VALUE
;
2590 if (in_size
> max_framesize
)
2591 max_framesize
= in_size
;
2592 current_module
= "decode video";
2593 int framedrop_type
= check_framedrop(mpctx
, sh_video
->frametime
);
2594 void *decoded_frame
= decode_video(sh_video
, packet
, in_size
,
2595 framedrop_type
, pts
);
2596 if (decoded_frame
) {
2597 determine_frame_pts(mpctx
);
2598 current_module
= "filter video";
2599 if (filter_video(sh_video
, decoded_frame
, sh_video
->pts
))
2600 if (!video_out
->config_ok
)
2601 break; // We'd likely hang in this loop otherwise
2605 pts
= video_out
->next_pts
;
2606 if (pts
== MP_NOPTS_VALUE
) {
2607 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Video pts after filters MISSING\n");
2608 // Try to use decoder pts from before filters
2609 pts
= sh_video
->pts
;
2610 if (pts
== MP_NOPTS_VALUE
)
2611 pts
= sh_video
->last_pts
;
2613 sh_video
->pts
= pts
;
2614 if (sh_video
->last_pts
== MP_NOPTS_VALUE
)
2615 sh_video
->last_pts
= sh_video
->pts
;
2616 else if (sh_video
->last_pts
> sh_video
->pts
) {
2617 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Decreasing video pts: %f < %f\n",
2618 sh_video
->pts
, sh_video
->last_pts
);
2619 /* If the difference in pts is small treat it as jitter around the
2620 * right value (possibly caused by incorrect timestamp ordering) and
2621 * just show this frame immediately after the last one.
2622 * Treat bigger differences as timestamp resets and start counting
2623 * timing of later frames from the position of this one. */
2624 if (sh_video
->last_pts
- sh_video
->pts
> 0.5)
2625 sh_video
->last_pts
= sh_video
->pts
;
2627 sh_video
->pts
= sh_video
->last_pts
;
2629 double frame_time
= sh_video
->pts
- sh_video
->last_pts
;
2630 sh_video
->last_pts
= sh_video
->pts
;
2631 sh_video
->timer
+= frame_time
;
2632 if (mpctx
->sh_audio
)
2633 mpctx
->delay
-= frame_time
;
2637 void pause_player(struct MPContext
*mpctx
)
2642 mpctx
->step_frames
= 0;
2643 mpctx
->time_frame
-= get_relative_time(mpctx
);
2645 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
)
2646 vo_control(mpctx
->video_out
, VOCTRL_PAUSE
, NULL
);
2648 if (mpctx
->audio_out
&& mpctx
->sh_audio
)
2649 mpctx
->audio_out
->pause(); // pause audio, keep data if possible
2652 void unpause_player(struct MPContext
*mpctx
)
2658 if (mpctx
->audio_out
&& mpctx
->sh_audio
)
2659 mpctx
->audio_out
->resume(); // resume audio
2660 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
2661 && !mpctx
->step_frames
)
2662 vo_control(mpctx
->video_out
, VOCTRL_RESUME
, NULL
); // resume video
2663 (void)get_relative_time(mpctx
); // ignore time that passed during pause
2666 void add_step_frame(struct MPContext
*mpctx
)
2668 mpctx
->step_frames
++;
2669 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
)
2670 vo_control(mpctx
->video_out
, VOCTRL_PAUSE
, NULL
);
2671 unpause_player(mpctx
);
2674 static void pause_loop(struct MPContext
*mpctx
)
2676 struct MPOpts
*opts
= &mpctx
->opts
;
2679 // Small hack to display the pause message on the OSD line.
2680 // The pause string is: "\n == PAUSE == \r" so we need to
2681 // take the first and the last char out
2682 if (term_osd
&& !mpctx
->sh_video
) {
2683 char msg
[128] = _("\n ===== PAUSE =====\r");
2684 int mlen
= strlen(msg
);
2686 set_osd_msg(OSD_MSG_PAUSE
, 1, 0, "%s", msg
+1);
2687 update_osd_msg(mpctx
);
2689 mp_tmsg(MSGT_CPLAYER
,MSGL_STATUS
,"\n ===== PAUSE =====\r");
2690 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_PAUSED\n");
2693 while ( (cmd
= mp_input_get_cmd(mpctx
->input
, 20, 1)) == NULL
2694 || cmd
->id
== MP_CMD_SET_MOUSE_POS
|| cmd
->pausing
== 4) {
2696 cmd
= mp_input_get_cmd(mpctx
->input
, 0, 0);
2697 run_command(mpctx
, cmd
);
2701 if (mpctx
->sh_video
&& mpctx
->video_out
)
2702 vo_check_events(mpctx
->video_out
);
2705 vf_menu_pause_update(vf_menu
);
2708 update_osd_msg(mpctx
);
2709 int hack
= vo_osd_changed(0);
2710 vo_osd_changed(hack
);
2717 // Find the right mute status and record position for new file position
2718 static void edl_seek_reset(MPContext
*mpctx
)
2720 mpctx
->edl_muted
= 0;
2721 next_edl_record
= edl_records
;
2723 while (next_edl_record
) {
2724 if (next_edl_record
->start_sec
>= mpctx
->sh_video
->pts
)
2727 if (next_edl_record
->action
== EDL_MUTE
)
2728 mpctx
->edl_muted
= !mpctx
->edl_muted
;
2729 next_edl_record
= next_edl_record
->next
;
2731 if ((mpctx
->user_muted
| mpctx
->edl_muted
) != mpctx
->mixer
.muted
)
2732 mixer_mute(&mpctx
->mixer
);
2736 // Execute EDL command for the current position if one exists
2737 static void edl_update(MPContext
*mpctx
)
2739 if (!next_edl_record
)
2742 if (!mpctx
->sh_video
) {
2743 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Cannot use EDL without video, disabling.\n");
2744 free_edl(edl_records
);
2745 next_edl_record
= NULL
;
2750 if (mpctx
->sh_video
->pts
>= next_edl_record
->start_sec
) {
2751 if (next_edl_record
->action
== EDL_SKIP
) {
2752 mpctx
->osd_function
= OSD_FFW
;
2753 mpctx
->abs_seek_pos
= 0;
2754 mpctx
->rel_seek_secs
= next_edl_record
->length_sec
;
2755 mp_msg(MSGT_CPLAYER
, MSGL_DBG4
, "EDL_SKIP: start [%f], stop "
2756 "[%f], length [%f]\n", next_edl_record
->start_sec
,
2757 next_edl_record
->stop_sec
, next_edl_record
->length_sec
);
2759 else if (next_edl_record
->action
== EDL_MUTE
) {
2760 mpctx
->edl_muted
= !mpctx
->edl_muted
;
2761 if ((mpctx
->user_muted
| mpctx
->edl_muted
) != mpctx
->mixer
.muted
)
2762 mixer_mute(&mpctx
->mixer
);
2763 mp_msg(MSGT_CPLAYER
, MSGL_DBG4
, "EDL_MUTE: [%f]\n",
2764 next_edl_record
->start_sec
);
2766 next_edl_record
= next_edl_record
->next
;
2770 static void reinit_decoders(struct MPContext
*mpctx
)
2772 reinit_video_chain(mpctx
);
2773 reinit_audio_chain(mpctx
);
2774 mp_property_do("sub", M_PROPERTY_SET
, &mpctx
->global_sub_pos
, mpctx
);
2777 static void seek_reset(struct MPContext
*mpctx
)
2779 if (mpctx
->sh_video
) {
2780 current_module
= "seek_video_reset";
2781 resync_video_stream(mpctx
->sh_video
);
2782 mpctx
->sh_video
->timer
= 0;
2783 vo_seek_reset(mpctx
->video_out
);
2784 mpctx
->sh_video
->timer
= 0;
2785 mpctx
->sh_video
->num_buffered_pts
= 0;
2786 mpctx
->sh_video
->last_pts
= MP_NOPTS_VALUE
;
2788 mpctx
->time_frame
= 0;
2789 mpctx
->restart_playback
= true;
2790 // Not all demuxers set d_video->pts during seek, so this value
2791 // (which is used by at least vobsub and edl code below) may
2792 // be completely wrong (probably 0).
2793 mpctx
->sh_video
->pts
= mpctx
->d_video
->pts
+ mpctx
->video_offset
;
2794 update_subtitles(mpctx
, &mpctx
->opts
, mpctx
->sh_video
,
2795 mpctx
->sh_video
->pts
, mpctx
->video_offset
,
2797 update_teletext(mpctx
->sh_video
, mpctx
->demuxer
, 1);
2800 if (mpctx
->sh_audio
) {
2801 current_module
= "seek_audio_reset";
2802 resync_audio_stream(mpctx
->sh_audio
);
2803 mpctx
->audio_out
->reset(); // stop audio, throwing away buffered data
2804 mpctx
->sh_audio
->a_buffer_len
= 0;
2805 mpctx
->sh_audio
->a_out_buffer_len
= 0;
2806 if (!mpctx
->sh_video
)
2807 update_subtitles(mpctx
, &mpctx
->opts
, NULL
, mpctx
->sh_audio
->pts
,
2808 mpctx
->video_offset
, mpctx
->d_sub
, 1);
2811 if (vo_vobsub
&& mpctx
->sh_video
) {
2812 current_module
= "seek_vobsub_reset";
2813 vobsub_seek(vo_vobsub
, mpctx
->sh_video
->pts
);
2816 edl_seek_reset(mpctx
);
2818 mpctx
->total_avsync_change
= 0;
2819 audio_time_usage
= 0; video_time_usage
= 0; vout_time_usage
= 0;
2822 current_module
= NULL
;
2825 static bool timeline_set_part(struct MPContext
*mpctx
, int i
)
2827 struct timeline_part
*p
= mpctx
->timeline
+ mpctx
->timeline_part
;
2828 struct timeline_part
*n
= mpctx
->timeline
+ i
;
2829 mpctx
->timeline_part
= i
;
2830 mpctx
->video_offset
= n
->start
- n
->source_start
;
2831 if (n
->source
== p
->source
)
2833 uninit_player(mpctx
, INITIALIZED_VCODEC
| (mpctx
->opts
.fixed_vo
&& mpctx
->opts
.video_id
!= -2 ? 0 : INITIALIZED_VO
) | INITIALIZED_AO
| INITIALIZED_ACODEC
);
2834 mpctx
->demuxer
= n
->source
->demuxer
;
2835 mpctx
->d_video
= mpctx
->demuxer
->video
;
2836 mpctx
->d_audio
= mpctx
->demuxer
->audio
;
2837 mpctx
->d_sub
= mpctx
->demuxer
->sub
;
2838 mpctx
->sh_video
= mpctx
->d_video
->sh
;
2839 mpctx
->sh_audio
= mpctx
->d_audio
->sh
;
2843 // Given pts, switch playback to the corresponding part.
2844 // Return offset within that part.
2845 static double timeline_set_from_time(struct MPContext
*mpctx
, double pts
,
2850 for (int i
= 0; i
< mpctx
->num_timeline_parts
; i
++) {
2851 struct timeline_part
*p
= mpctx
->timeline
+ i
;
2852 if (pts
< (p
+1)->start
) {
2853 *need_reset
= timeline_set_part(mpctx
, i
);
2854 return pts
- p
->start
+ p
->source_start
;
2861 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2862 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2863 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2864 static int seek(MPContext
*mpctx
, double amount
, int style
)
2866 current_module
= "seek";
2867 if (mpctx
->stop_play
== AT_END_OF_FILE
)
2868 mpctx
->stop_play
= KEEP_PLAYING
;
2869 if (style
& SEEK_FACTOR
2870 || style
& SEEK_ABSOLUTE
&& amount
< mpctx
->last_chapter_pts
2872 mpctx
->last_chapter_seek
= -1;
2873 if (mpctx
->timeline
&& style
& SEEK_FACTOR
) {
2874 amount
*= mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
2875 style
&= ~SEEK_FACTOR
;
2877 if ((mpctx
->demuxer
->accurate_seek
|| mpctx
->timeline
) && mpctx
->sh_video
2878 && !(style
& (SEEK_ABSOLUTE
| SEEK_FACTOR
))) {
2879 style
|= SEEK_ABSOLUTE
;
2881 style
|= SEEK_FORWARD
;
2883 style
|= SEEK_BACKWARD
;
2884 amount
+= mpctx
->sh_video
->pts
;
2887 /* At least the liba52 decoder wants to read from the input stream
2888 * during initialization, so reinit must be done after the demux_seek()
2889 * call that clears possible stream EOF. */
2890 bool need_reset
= false;
2891 if (mpctx
->timeline
) {
2892 amount
= timeline_set_from_time(mpctx
, amount
, &need_reset
);
2894 mpctx
->stop_play
= AT_END_OF_FILE
;
2895 // Clear audio from current position
2896 if (mpctx
->sh_audio
) {
2897 mpctx
->audio_out
->reset();
2898 mpctx
->sh_audio
->a_buffer_len
= 0;
2899 mpctx
->sh_audio
->a_out_buffer_len
= 0;
2904 int seekresult
= demux_seek(mpctx
->demuxer
, amount
, audio_delay
, style
);
2906 reinit_decoders(mpctx
);
2907 if (seekresult
== 0)
2915 double get_time_length(struct MPContext
*mpctx
)
2917 if (mpctx
->timeline
)
2918 return mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
2920 struct demuxer
*demuxer
= mpctx
->demuxer
;
2921 double get_time_ans
;
2922 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
2923 if (demux_control(demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
,
2924 (void *) &get_time_ans
) > 0)
2925 return get_time_ans
;
2927 struct sh_video
*sh_video
= mpctx
->d_video
->sh
;
2928 struct sh_audio
*sh_audio
= mpctx
->d_audio
->sh
;
2929 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
2930 return (double) (demuxer
->movi_end
- demuxer
->movi_start
) /
2931 (sh_video
->i_bps
+ sh_audio
->i_bps
);
2932 if (sh_video
&& sh_video
->i_bps
)
2933 return (double) (demuxer
->movi_end
- demuxer
->movi_start
) /
2935 if (sh_audio
&& sh_audio
->i_bps
)
2936 return (double) (demuxer
->movi_end
- demuxer
->movi_start
) /
2941 /* If there are timestamps from stream level then use those (for example
2942 * DVDs can have consistent times there while the MPEG-level timestamps
2944 double get_current_time(struct MPContext
*mpctx
)
2946 struct demuxer
*demuxer
= mpctx
->demuxer
;
2947 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
2948 return demuxer
->stream_pts
;
2949 struct sh_video
*sh_video
= demuxer
->video
->sh
;
2951 return sh_video
->pts
;
2952 return playing_audio_pts(mpctx
);
2955 int get_percent_pos(struct MPContext
*mpctx
)
2957 struct demuxer
*demuxer
= mpctx
->demuxer
;
2959 if (mpctx
->timeline
)
2960 ans
= get_current_time(mpctx
) * 100 /
2961 mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
2962 else if (demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
) > 0)
2965 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
2966 off_t pos
= demuxer
->filepos
> 0 ?
2967 demuxer
->filepos
: stream_tell(demuxer
->stream
);
2969 ans
= (pos
- demuxer
->movi_start
) / len
;
2980 // -2 is no chapters, -1 is before first chapter
2981 int get_current_chapter(struct MPContext
*mpctx
)
2983 double current_pts
= get_current_time(mpctx
);
2984 if (!mpctx
->chapters
)
2985 return FFMAX(mpctx
->last_chapter_seek
,
2986 demuxer_get_current_chapter(mpctx
->demuxer
, current_pts
));
2989 for (i
= 1; i
< mpctx
->num_chapters
; i
++)
2990 if (current_pts
< mpctx
->chapters
[i
].start
)
2992 return FFMAX(mpctx
->last_chapter_seek
, i
- 1);
2995 // currently returns a string allocated with malloc, not talloc
2996 char *chapter_display_name(struct MPContext
*mpctx
, int chapter
)
2998 if (!mpctx
->chapters
|| !mpctx
->sh_video
)
2999 return demuxer_chapter_display_name(mpctx
->demuxer
, chapter
);
3000 return strdup(mpctx
->chapters
[chapter
].name
);
3003 int seek_chapter(struct MPContext
*mpctx
, int chapter
, double *seek_pts
,
3004 char **chapter_name
)
3006 mpctx
->last_chapter_seek
= -1;
3007 if (!mpctx
->chapters
|| !mpctx
->sh_video
) {
3008 int res
= demuxer_seek_chapter(mpctx
->demuxer
, chapter
, seek_pts
,
3011 if (*seek_pts
== -1)
3014 mpctx
->last_chapter_seek
= res
;
3015 mpctx
->last_chapter_pts
= *seek_pts
;
3021 if (chapter
>= mpctx
->num_chapters
)
3025 *seek_pts
= mpctx
->chapters
[chapter
].start
;
3026 mpctx
->last_chapter_seek
= chapter
;
3027 mpctx
->last_chapter_pts
= *seek_pts
;
3029 *chapter_name
= talloc_strdup(NULL
, mpctx
->chapters
[chapter
].name
);
3033 static int find_ordered_chapter_sources(struct MPContext
*mpctx
,
3034 struct content_source
*sources
,
3036 unsigned char uid_map
[][16])
3038 int num_filenames
= 0;
3039 char **filenames
= NULL
;
3040 if (num_sources
> 1) {
3041 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "This file references data from "
3042 "other sources.\n");
3043 if (mpctx
->stream
->type
!= STREAMTYPE_FILE
) {
3044 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, "Playback source is not a "
3045 "normal disk file. Will not search for related files.\n");
3047 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Will scan other files in the "
3048 "same directory to find referenced sources.\n");
3049 filenames
= find_files(mpctx
->demuxer
->filename
, ".mkv",
3054 int num_left
= num_sources
- 1;
3055 for (int i
= 0; i
< num_filenames
&& num_left
> 0; i
++) {
3056 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Checking file %s\n",
3057 filename_recode(filenames
[i
]));
3059 struct stream
*s
= open_stream(filenames
[i
], &mpctx
->opts
, &format
);
3062 struct demuxer
*d
= demux_open(&mpctx
->opts
, s
, DEMUXER_TYPE_MATROSKA
,
3063 mpctx
->opts
.audio_id
,
3064 mpctx
->opts
.video_id
,
3065 mpctx
->opts
.sub_id
, filenames
[i
]);
3070 if (d
->file_format
== DEMUXER_TYPE_MATROSKA
) {
3071 for (int i
= 1; i
< num_sources
; i
++) {
3072 if (sources
[i
].demuxer
)
3074 if (!memcmp(uid_map
[i
], d
->matroska_data
.segment_uid
, 16)) {
3075 mp_msg(MSGT_CPLAYER
, MSGL_INFO
,"Match for source %d: %s\n",
3076 i
, filename_recode(d
->filename
));
3077 sources
[i
].stream
= s
;
3078 sources
[i
].demuxer
= d
;
3090 talloc_free(filenames
);
3092 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Failed to find ordered chapter part!\n"
3093 "There will be parts MISSING from the video!\n");
3094 for (int i
= 1, j
= 1; i
< num_sources
; i
++)
3095 if (sources
[i
].demuxer
) {
3096 sources
[j
] = sources
[i
];
3097 memcpy(uid_map
[j
], uid_map
[i
], 16);
3101 return num_sources
- num_left
;
3104 static void build_ordered_chapter_timeline(struct MPContext
*mpctx
)
3106 if (!mpctx
->opts
.ordered_chapters
) {
3107 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "File uses ordered chapters, but "
3108 "you have disabled support for them. Ignoring.\n");
3112 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "File uses ordered chapters, will build "
3113 "edit timeline.\n");
3115 struct demuxer
*demuxer
= mpctx
->demuxer
;
3116 struct matroska_data
*m
= &demuxer
->matroska_data
;
3118 // +1 because sources/uid_map[0] is original file even if all chapters
3119 // actually use other sources and need separate entries
3120 struct content_source
*sources
= talloc_array_ptrtype(NULL
, sources
,
3121 m
->num_ordered_chapters
+1);
3122 sources
[0].stream
= mpctx
->stream
;
3123 sources
[0].demuxer
= mpctx
->demuxer
;
3124 unsigned char uid_map
[m
->num_ordered_chapters
+1][16];
3125 int num_sources
= 1;
3126 memcpy(uid_map
[0], m
->segment_uid
, 16);
3128 for (int i
= 0; i
< m
->num_ordered_chapters
; i
++) {
3129 struct matroska_chapter
*c
= m
->ordered_chapters
+ i
;
3130 if (!c
->has_segment_uid
)
3131 memcpy(c
->segment_uid
, m
->segment_uid
, 16);
3133 for (int j
= 0; j
< num_sources
; j
++)
3134 if (!memcmp(c
->segment_uid
, uid_map
[j
], 16))
3136 memcpy(uid_map
[num_sources
], c
->segment_uid
, 16);
3137 sources
[num_sources
] = (struct content_source
){};
3143 num_sources
= find_ordered_chapter_sources(mpctx
, sources
, num_sources
,
3147 // +1 for terminating chapter with start time marking end of last real one
3148 struct timeline_part
*timeline
= talloc_array_ptrtype(NULL
, timeline
,
3149 m
->num_ordered_chapters
+ 1);
3150 struct chapter
*chapters
= talloc_array_ptrtype(NULL
, chapters
,
3151 m
->num_ordered_chapters
);
3152 uint64_t starttime
= 0;
3153 uint64_t missing_time
= 0;
3155 int num_chapters
= 0;
3156 uint64_t prev_part_offset
= 0;
3157 for (int i
= 0; i
< m
->num_ordered_chapters
; i
++) {
3158 struct matroska_chapter
*c
= m
->ordered_chapters
+ i
;
3161 for (j
= 0; j
< num_sources
; j
++) {
3162 if (!memcmp(c
->segment_uid
, uid_map
[j
], 16))
3165 missing_time
+= c
->end
- c
->start
;
3168 chapters
[num_chapters
].start
= starttime
/ 1000.;
3169 chapters
[num_chapters
].name
= talloc_strdup(chapters
, c
->name
);
3170 /* Only add a separate part if the time or file actually changes.
3171 * Matroska files have chapter divisions that are redundant from
3172 * timeline point of view because the same chapter structure is used
3173 * both to specify the timeline and for normal chapter information.
3174 * Removing a missing inserted external chapter can also cause this. */
3175 if (part_count
== 0 || c
->start
!= starttime
+ prev_part_offset
3176 || sources
+ j
!= timeline
[part_count
- 1].source
) {
3177 timeline
[part_count
].source
= sources
+ j
;
3178 timeline
[part_count
].start
= chapters
[num_chapters
].start
;
3179 timeline
[part_count
].source_start
= c
->start
/ 1000.;
3180 prev_part_offset
= c
->start
- starttime
;
3183 starttime
+= c
->end
- c
->start
;
3186 timeline
[part_count
].start
= starttime
/ 1000.;
3189 // None of the parts come from the file itself???
3190 talloc_free(sources
);
3191 talloc_free(timeline
);
3192 talloc_free(chapters
);
3196 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Timeline contains %d parts from %d "
3197 "sources. Total length %.3f seconds.\n", part_count
, num_sources
,
3198 timeline
[part_count
].start
);
3200 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "There are %.3f seconds missing "
3201 "from the timeline!\n", missing_time
/ 1000.);
3202 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Source files:\n");
3203 for (int i
= 0; i
< num_sources
; i
++)
3204 mp_msg(MSGT_CPLAYER
, MSGL_V
, "%d: %s\n", i
,
3205 filename_recode(sources
[i
].demuxer
->filename
));
3206 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Timeline parts: (number, start, "
3207 "source_start, source):\n");
3208 for (int i
= 0; i
< part_count
; i
++) {
3209 struct timeline_part
*p
= timeline
+ i
;
3210 mp_msg(MSGT_CPLAYER
, MSGL_V
, "%3d %9.3f %9.3f %3td\n", i
, p
->start
,
3211 p
->source_start
, p
->source
- sources
);
3213 mp_msg(MSGT_CPLAYER
, MSGL_V
, "END %9.3f\n", timeline
[part_count
].start
);
3214 mpctx
->sources
= sources
;
3215 mpctx
->num_sources
= num_sources
;
3216 mpctx
->timeline
= timeline
;
3217 mpctx
->num_timeline_parts
= part_count
;
3218 mpctx
->num_chapters
= num_chapters
;
3219 mpctx
->chapters
= chapters
;
3221 mpctx
->timeline_part
= 0;
3222 mpctx
->demuxer
= timeline
[0].source
->demuxer
;
3226 static int read_keys(void *ctx
, int fd
)
3229 return mplayer_get_key(ctx
, 0);
3233 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
3234 * file for some tools to link against. */
3235 #ifndef DISABLE_MAIN
3236 int main(int argc
,char* argv
[]){
3243 /* Flag indicating whether MPlayer should exit without playing anything. */
3247 struct MPContext
*mpctx
= &(struct MPContext
){
3248 .osd_function
= OSD_PLAY
,
3249 .begin_skip
= MP_NOPTS_VALUE
,
3250 .play_tree_step
= 1,
3251 .global_sub_pos
= -1,
3252 .set_of_sub_pos
= -1,
3253 .file_format
= DEMUXER_TYPE_UNKNOWN
,
3258 srand(GetTimerMS());
3261 set_av_log_callback();
3264 mpctx
->x11_state
= vo_x11_init_state();
3266 struct MPOpts
*opts
= &mpctx
->opts
;
3267 set_default_mplayer_options(opts
);
3268 // Create the config context and register the options
3269 mpctx
->mconfig
= m_config_new(opts
, cfg_include
);
3270 m_config_register_options(mpctx
->mconfig
,mplayer_opts
);
3271 m_config_register_options(mpctx
->mconfig
, common_opts
);
3272 mp_input_register_options(mpctx
->mconfig
);
3274 // Preparse the command line
3275 m_config_preparse_command_line(mpctx
->mconfig
,argc
,argv
);
3277 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
3282 stream_tv_defaults
.immediate
= 1;
3285 parse_cfgfiles(mpctx
, mpctx
->mconfig
);
3287 mpctx
->playtree
= m_config_parse_mp_command_line(mpctx
->mconfig
, argc
, argv
);
3288 if(mpctx
->playtree
== NULL
)
3291 mpctx
->playtree
= play_tree_cleanup(mpctx
->playtree
);
3292 if(mpctx
->playtree
) {
3293 mpctx
->playtree_iter
= play_tree_iter_new(mpctx
->playtree
,mpctx
->mconfig
);
3294 if(mpctx
->playtree_iter
) {
3295 if(play_tree_iter_step(mpctx
->playtree_iter
,0,0) != PLAY_TREE_ITER_ENTRY
) {
3296 play_tree_iter_free(mpctx
->playtree_iter
);
3297 mpctx
->playtree_iter
= NULL
;
3299 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
,1);
3303 mpctx
->key_fifo
= mp_fifo_create(opts
);
3305 print_version("MPlayer");
3307 #if defined(__MINGW32__) || defined(__CYGWIN__)
3309 HMODULE kernel32
= GetModuleHandle("Kernel32.dll");
3310 BOOL
WINAPI (*setDEP
)(DWORD
) = NULL
;
3311 BOOL
WINAPI (*setDllDir
)(LPCTSTR
) = NULL
;
3313 setDEP
= GetProcAddress(kernel32
, "SetProcessDEPPolicy");
3314 setDllDir
= GetProcAddress(kernel32
, "SetDllDirectoryA");
3316 if (setDEP
) setDEP(3);
3317 if (setDllDir
) setDllDir("");
3319 // stop Windows from showing all kinds of annoying error dialogs
3320 SetErrorMode(0x8003);
3321 // request 1ms timer resolution
3325 #ifdef CONFIG_PRIORITY
3330 set_codec_path(codec_path
);
3332 if(opts
->video_driver_list
&& strcmp(opts
->video_driver_list
[0],"help")==0){
3337 if(opts
->audio_driver_list
&& strcmp(opts
->audio_driver_list
[0],"help")==0){
3342 /* Check codecs.conf. */
3343 if(!codecs_file
|| !parse_codec_cfg(codecs_file
)){
3344 if(!parse_codec_cfg(mem_ptr
=get_path("codecs.conf"))){
3345 if(!parse_codec_cfg(MPLAYER_CONFDIR
"/codecs.conf")){
3346 if(!parse_codec_cfg(NULL
)){
3347 exit_player_with_rc(mpctx
, EXIT_NONE
, 0);
3349 mp_tmsg(MSGT_CPLAYER
,MSGL_V
,"Using built-in default codecs.conf.\n");
3352 free( mem_ptr
); // release the buffer created by get_path()
3355 if(audio_codec_list
&& strcmp(audio_codec_list
[0],"help")==0){
3356 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "Available audio codecs:\n");
3357 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_CODECS\n");
3359 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3362 if(video_codec_list
&& strcmp(video_codec_list
[0],"help")==0){
3363 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "Available video codecs:\n");
3364 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_CODECS\n");
3366 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3369 if(video_fm_list
&& strcmp(video_fm_list
[0],"help")==0){
3371 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3374 if(audio_fm_list
&& strcmp(audio_fm_list
[0],"help")==0){
3376 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3379 if(af_cfg
.list
&& strcmp(af_cfg
.list
[0],"help")==0){
3385 if(vo_fstype_list
&& strcmp(vo_fstype_list
[0],"help")==0){
3387 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3391 if((opts
->demuxer_name
&& strcmp(opts
->demuxer_name
,"help")==0) ||
3392 (opts
->audio_demuxer_name
&& strcmp(opts
->audio_demuxer_name
,"help")==0) ||
3393 (opts
->sub_demuxer_name
&& strcmp(opts
->sub_demuxer_name
,"help")==0)){
3395 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "\n");
3398 if(list_properties
) {
3399 property_print_help();
3404 exit_player(mpctx
, EXIT_NONE
);
3406 if(!mpctx
->filename
&& !player_idle_mode
){
3407 // no file/vcd/dvd -> show HELP:
3408 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "%s", mp_gtext(help_text
));
3409 exit_player_with_rc(mpctx
, EXIT_NONE
, 0);
3412 /* Display what configure line was used */
3413 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Configuration: " CONFIGURATION
"\n");
3415 // Many users forget to include command line in bugreports...
3416 if( mp_msg_test(MSGT_CPLAYER
,MSGL_V
) ){
3417 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "CommandLine:");
3418 for(i
=1;i
<argc
;i
++)mp_msg(MSGT_CPLAYER
, MSGL_INFO
," '%s'",argv
[i
]);
3419 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "\n");
3422 //------ load global data first ------
3424 mpctx
->osd
= osd_create();
3427 #ifdef CONFIG_FREETYPE
3430 #ifdef CONFIG_FONTCONFIG
3431 if(font_fontconfig
<= 0)
3434 #ifdef CONFIG_BITMAP_FONT
3436 vo_font
=read_font_desc(font_name
,font_factor
,verbose
>1);
3437 if(!vo_font
) mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Cannot load bitmap font: %s\n",
3438 filename_recode(font_name
));
3441 vo_font
=read_font_desc( mem_ptr
=get_path("font/font.desc"),font_factor
,verbose
>1);
3442 free(mem_ptr
); // release the buffer created by get_path()
3444 vo_font
=read_font_desc(MPLAYER_DATADIR
"/font/font.desc",font_factor
,verbose
>1);
3447 mpctx
->osd
->sub_font
= read_font_desc(sub_font_name
, font_factor
, verbose
>1);
3449 mpctx
->osd
->sub_font
= vo_font
;
3451 #ifdef CONFIG_FONTCONFIG
3456 ass_library
= ass_init();
3462 char *rtc_device
= opts
->rtc_device
;
3463 // seteuid(0); /* Can't hurt to try to get root here */
3464 if ((rtc_fd
= open(rtc_device
? rtc_device
: "/dev/rtc", O_RDONLY
)) < 0)
3465 mp_tmsg(MSGT_CPLAYER
, MSGL_WARN
, "Failed to open %s: %s (it should be readable by the user.)\n",
3466 rtc_device
? rtc_device
: "/dev/rtc", strerror(errno
));
3468 unsigned long irqp
= 1024; /* 512 seemed OK. 128 is jerky. */
3470 if (ioctl(rtc_fd
, RTC_IRQP_SET
, irqp
) < 0) {
3471 mp_tmsg(MSGT_CPLAYER
, MSGL_WARN
, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp
, strerror(errno
));
3472 mp_tmsg(MSGT_CPLAYER
, MSGL_HINT
, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp
);
3475 } else if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
3476 /* variable only by the root */
3477 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno
));
3481 mp_tmsg(MSGT_CPLAYER
, MSGL_V
, "Using Linux hardware RTC timing (%ldHz).\n", irqp
);
3485 #endif /* HAVE_RTC */
3486 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Using %s timing\n",
3487 opts
->softsleep
? "software" : timer_name
);
3490 load_termcap(NULL
); // load key-codes
3493 // ========== Init keyboard FIFO (connection to libvo) ============
3495 // Init input system
3496 current_module
= "init_input";
3497 mpctx
->input
= mp_input_init(&opts
->input
);
3498 mp_input_add_key_fd(mpctx
->input
, -1,0,mplayer_get_key
,NULL
, mpctx
->key_fifo
);
3500 mp_input_add_cmd_fd(mpctx
->input
, 0,USE_SELECT
,MP_INPUT_SLAVE_CMD_FUNC
,NULL
);
3501 else if(!noconsolecontrols
)
3502 mp_input_add_key_fd(mpctx
->input
, 0, 1, read_keys
, NULL
, mpctx
->key_fifo
);
3503 // Set the libstream interrupt callback
3504 stream_set_interrupt_callback(mp_input_check_interrupt
, mpctx
->input
);
3508 if(menu_cfg
&& menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
, menu_cfg
))
3509 mp_tmsg(MSGT_CPLAYER
, MSGL_V
, "Menu initialized: %s\n", menu_cfg
);
3511 menu_cfg
= get_path("menu.conf");
3512 if(menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
, menu_cfg
))
3513 mp_tmsg(MSGT_CPLAYER
, MSGL_V
, "Menu initialized: %s\n", menu_cfg
);
3515 if(menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
,
3516 MPLAYER_CONFDIR
"/menu.conf"))
3517 mp_tmsg(MSGT_CPLAYER
, MSGL_V
, "Menu initialized: %s\n", MPLAYER_CONFDIR
"/menu.conf");
3519 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Menu init failed.\n");
3527 current_module
= NULL
;
3531 signal(SIGCHLD
,child_sighandler
);
3534 #ifdef CONFIG_CRASH_DEBUG
3535 prog_path
= argv
[0];
3537 //========= Catch terminate signals: ================
3538 // terminate requests:
3539 signal(SIGTERM
,exit_sighandler
); // kill
3540 signal(SIGHUP
,exit_sighandler
); // kill -HUP / xterm closed
3542 signal(SIGINT
,exit_sighandler
); // Interrupt from keyboard
3544 signal(SIGQUIT
,exit_sighandler
); // Quit from keyboard
3545 signal(SIGPIPE
,exit_sighandler
); // Some window managers cause this
3546 #ifdef CONFIG_SIGHANDLER
3548 signal(SIGBUS
,exit_sighandler
); // bus error
3549 signal(SIGSEGV
,exit_sighandler
); // segfault
3550 signal(SIGILL
,exit_sighandler
); // illegal instruction
3551 signal(SIGFPE
,exit_sighandler
); // floating point exc.
3552 signal(SIGABRT
,exit_sighandler
); // abort()
3553 #ifdef CONFIG_CRASH_DEBUG
3555 signal(SIGTRAP
,exit_sighandler
);
3559 // ******************* Now, let's see the per-file stuff ********************
3563 // init global sub numbers
3564 mpctx
->global_sub_size
= 0;
3565 memset(mpctx
->sub_counts
, 0, sizeof(mpctx
->sub_counts
));
3567 if (mpctx
->filename
) {
3568 load_per_protocol_config (mpctx
->mconfig
, mpctx
->filename
);
3569 load_per_extension_config (mpctx
->mconfig
, mpctx
->filename
);
3570 load_per_file_config (mpctx
->mconfig
, mpctx
->filename
);
3573 if (opts
->video_driver_list
)
3574 load_per_output_config (mpctx
->mconfig
, PROFILE_CFG_VO
, opts
->video_driver_list
[0]);
3575 if (opts
->audio_driver_list
)
3576 load_per_output_config (mpctx
->mconfig
, PROFILE_CFG_AO
, opts
->audio_driver_list
[0]);
3578 // We must enable getch2 here to be able to interrupt network connection
3580 if(!noconsolecontrols
&& !slave_mode
){
3581 if(mpctx
->initialized_flags
&INITIALIZED_GETCH2
)
3582 mp_tmsg(MSGT_CPLAYER
,MSGL_WARN
,"WARNING: getch2_init called twice!\n");
3584 getch2_enable(); // prepare stdin for hotkeys...
3585 mpctx
->initialized_flags
|=INITIALIZED_GETCH2
;
3586 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n[[[init getch2]]]\n");
3589 // =================== GUI idle loop (STOP state) ===========================
3590 while (player_idle_mode
&& !mpctx
->filename
) {
3591 play_tree_t
* entry
= NULL
;
3593 if (mpctx
->video_out
&& mpctx
->video_out
->config_ok
)
3594 vo_control(mpctx
->video_out
, VOCTRL_PAUSE
, NULL
);
3595 while (!(cmd
= mp_input_get_cmd(mpctx
->input
, 0, 0))) { // wait for command
3596 if (mpctx
->video_out
)
3597 vo_check_events(mpctx
->video_out
);
3601 case MP_CMD_LOADFILE
:
3602 // prepare a tree entry with the new filename
3603 entry
= play_tree_new();
3604 play_tree_add_file(entry
, cmd
->args
[0].v
.s
);
3605 // The entry is added to the main playtree after the switch().
3607 case MP_CMD_LOADLIST
:
3608 entry
= parse_playlist_file(mpctx
->mconfig
, cmd
->args
[0].v
.s
);
3611 exit_player_with_rc(mpctx
, EXIT_QUIT
, (cmd
->nargs
> 0)? cmd
->args
[0].v
.i
: 0);
3613 case MP_CMD_VO_FULLSCREEN
:
3614 case MP_CMD_GET_PROPERTY
:
3615 case MP_CMD_SET_PROPERTY
:
3616 case MP_CMD_STEP_PROPERTY
:
3617 run_command(mpctx
, cmd
);
3623 if (entry
) { // user entered a command that gave a valid entry
3624 if (mpctx
->playtree
) // the playtree is always a node with one child. let's clear it
3625 play_tree_free_list(mpctx
->playtree
->child
, 1);
3626 else mpctx
->playtree
=play_tree_new(); // .. or make a brand new playtree
3628 if (!mpctx
->playtree
) continue; // couldn't make playtree! wait for next command
3630 play_tree_set_child(mpctx
->playtree
, entry
);
3632 /* Make iterator start at the top the of tree. */
3633 mpctx
->playtree_iter
= play_tree_iter_new(mpctx
->playtree
, mpctx
->mconfig
);
3634 if (!mpctx
->playtree_iter
) continue;
3636 // find the first real item in the tree
3637 if (play_tree_iter_step(mpctx
->playtree_iter
,0,0) != PLAY_TREE_ITER_ENTRY
) {
3639 play_tree_iter_free(mpctx
->playtree_iter
);
3640 mpctx
->playtree_iter
= NULL
;
3641 continue; // wait for next command
3643 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
, 1);
3646 //---------------------------------------------------------------------------
3648 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
)
3649 vo_control(mpctx
->video_out
, VOCTRL_RESUME
, NULL
);
3651 if (mpctx
->filename
) {
3652 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"\nPlaying %s.\n",
3653 filename_recode(mpctx
->filename
));
3654 if(use_filename_title
&& opts
->vo_wintitle
== NULL
)
3655 opts
->vo_wintitle
= strdup(mp_basename2(mpctx
->filename
));
3659 if (edl_records
) free_edl(edl_records
);
3660 next_edl_record
= edl_records
= edl_parse_file();
3662 if (edl_output_filename
) {
3663 if (edl_fd
) fclose(edl_fd
);
3664 if ((edl_fd
= fopen(edl_output_filename
, "w")) == NULL
)
3666 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Can't open EDL file [%s] for writing.\n",
3667 filename_recode(edl_output_filename
));
3671 //==================== Open VOB-Sub ============================
3673 current_module
="vobsub";
3675 vo_vobsub
=vobsub_open(vobsub_name
,spudec_ifo
,1,&vo_spudec
);
3677 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Cannot load subtitles: %s\n",
3678 filename_recode(vobsub_name
));
3679 } else if (sub_auto
&& mpctx
->filename
){
3680 /* try to autodetect vobsub from movie filename ::atmos */
3681 char *buf
= strdup(mpctx
->filename
), *psub
;
3682 char *pdot
= strrchr(buf
, '.');
3683 char *pslash
= strrchr(buf
, '/');
3684 #if defined(__MINGW32__) || defined(__CYGWIN__)
3685 if (!pslash
) pslash
= strrchr(buf
, '\\');
3687 if (pdot
&& (!pslash
|| pdot
> pslash
))
3689 vo_vobsub
=vobsub_open(buf
,spudec_ifo
,0,&vo_spudec
);
3690 /* try from ~/.mplayer/sub */
3691 if(!vo_vobsub
&& (psub
= get_path( "sub/" ))) {
3694 bname
= strrchr(buf
,'/');
3695 #if defined(__MINGW32__) || defined(__CYGWIN__)
3696 if(!bname
) bname
= strrchr(buf
,'\\');
3700 l
= strlen(psub
) + strlen(bname
) + 1;
3701 psub
= realloc(psub
,l
);
3703 vo_vobsub
=vobsub_open(psub
,spudec_ifo
,0,&vo_spudec
);
3709 mpctx
->initialized_flags
|=INITIALIZED_VOBSUB
;
3710 vobsub_set_from_lang(vo_vobsub
, opts
->sub_lang
);
3711 mp_property_do("sub_forced_only", M_PROPERTY_SET
, &forced_subs_only
, mpctx
);
3713 // setup global sub numbering
3714 mpctx
->sub_counts
[SUB_SOURCE_VOBSUB
] = vobsub_get_indexes_count(vo_vobsub
);
3717 //============ Open & Sync STREAM --- fork cache2 ====================
3720 mpctx
->demuxer
=NULL
;
3721 if (mpctx
->d_audio
) {
3722 //free_demuxer_stream(mpctx->d_audio);
3723 mpctx
->d_audio
=NULL
;
3725 if (mpctx
->d_video
) {
3726 //free_demuxer_stream(d_video);
3727 mpctx
->d_video
=NULL
;
3729 mpctx
->sh_audio
=NULL
;
3730 mpctx
->sh_video
=NULL
;
3732 current_module
="open_stream";
3733 mpctx
->stream
= open_stream(mpctx
->filename
, opts
, &mpctx
->file_format
);
3734 if(!mpctx
->stream
) { // error...
3735 mpctx
->stop_play
= libmpdemux_was_interrupted(mpctx
, PT_NEXT_ENTRY
);
3736 goto goto_next_file
;
3738 mpctx
->initialized_flags
|=INITIALIZED_STREAM
;
3740 if(mpctx
->file_format
== DEMUXER_TYPE_PLAYLIST
) {
3741 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "\nThis looks like a playlist, but "
3742 "playlist support will not be used automatically.\n"
3743 "MPlayer's playlist code is unsafe and should only be used with "
3744 "trusted sources.\nPlayback will probably fail.\n\n");
3748 current_module
="handle_playlist";
3749 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Parsing playlist %s...\n",
3750 filename_recode(mpctx
->filename
));
3751 entry
= parse_playtree(mpctx
->stream
, mpctx
->mconfig
, 0);
3752 mpctx
->eof
=playtree_add_playlist(mpctx
, entry
);
3753 goto goto_next_file
;
3756 mpctx
->stream
->start_pos
+=seek_to_byte
;
3758 if(stream_dump_type
==5){
3759 unsigned char buf
[4096];
3762 current_module
="dumpstream";
3763 stream_reset(mpctx
->stream
);
3764 stream_seek(mpctx
->stream
,mpctx
->stream
->start_pos
);
3765 f
=fopen(opts
->stream_dump_name
,"wb");
3767 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"Cannot open dump file.\n");
3768 exit_player(mpctx
, EXIT_ERROR
);
3770 if (opts
->chapterrange
[0] > 1) {
3771 int chapter
= opts
->chapterrange
[0] - 1;
3772 stream_control(mpctx
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
, &chapter
);
3774 while(!mpctx
->stream
->eof
&& !async_quit_request
){
3775 len
=stream_read(mpctx
->stream
,buf
,4096);
3777 if(fwrite(buf
,len
,1,f
) != 1) {
3778 mp_tmsg(MSGT_MENCODER
,MSGL_FATAL
,"%s: Error writing file.\n",opts
->stream_dump_name
);
3779 exit_player(mpctx
, EXIT_ERROR
);
3782 if (opts
->chapterrange
[1] > 0) {
3784 if (stream_control(mpctx
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
3785 &chapter
) == STREAM_OK
3786 && chapter
+ 1 > opts
->chapterrange
[1])
3791 mp_tmsg(MSGT_MENCODER
,MSGL_FATAL
,"%s: Error writing file.\n",opts
->stream_dump_name
);
3792 exit_player(mpctx
, EXIT_ERROR
);
3794 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Core dumped ;)\n");
3795 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
3798 #ifdef CONFIG_DVDREAD
3799 if(mpctx
->stream
->type
==STREAMTYPE_DVD
){
3800 current_module
="dvd lang->id";
3801 if(opts
->audio_lang
&& opts
->audio_id
==-1) opts
->audio_id
=dvd_aid_from_lang(mpctx
->stream
,opts
->audio_lang
);
3802 if(opts
->sub_lang
&& opts
->sub_id
==-1) opts
->sub_id
=dvd_sid_from_lang(mpctx
->stream
,opts
->sub_lang
);
3803 // setup global sub numbering
3804 mpctx
->sub_counts
[SUB_SOURCE_DEMUX
] = dvd_number_of_subs(mpctx
->stream
);
3805 current_module
=NULL
;
3809 #ifdef CONFIG_DVDNAV
3810 if(mpctx
->stream
->type
==STREAMTYPE_DVDNAV
){
3811 current_module
="dvdnav lang->id";
3812 if(opts
->audio_lang
&& opts
->audio_id
==-1) opts
->audio_id
=mp_dvdnav_aid_from_lang(mpctx
->stream
,opts
->audio_lang
);
3813 dvdsub_lang_id
= -3;
3814 if(opts
->sub_lang
&& opts
->sub_id
==-1)
3815 dvdsub_lang_id
= opts
->sub_id
= mp_dvdnav_sid_from_lang(mpctx
->stream
,opts
->sub_lang
);
3816 // setup global sub numbering
3817 mpctx
->sub_counts
[SUB_SOURCE_DEMUX
] = mp_dvdnav_number_of_subs(mpctx
->stream
);
3818 current_module
=NULL
;
3822 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3824 if(stream_cache_size
>0){
3826 float stream_cache_min_percent
= opts
->stream_cache_min_percent
;
3827 float stream_cache_seek_min_percent
= opts
->stream_cache_seek_min_percent
;
3828 current_module
="enable_cache";
3829 res
= stream_enable_cache(mpctx
->stream
,stream_cache_size
*1024,
3830 stream_cache_size
*1024*(stream_cache_min_percent
/ 100.0),
3831 stream_cache_size
*1024*(stream_cache_seek_min_percent
/ 100.0));
3833 if((mpctx
->stop_play
= libmpdemux_was_interrupted(mpctx
, PT_NEXT_ENTRY
))) goto goto_next_file
;
3836 //============ Open DEMUXERS --- DETECT file type =======================
3837 current_module
="demux_open";
3839 mpctx
->demuxer
=demux_open(opts
, mpctx
->stream
,mpctx
->file_format
,opts
->audio_id
,opts
->video_id
,opts
->sub_id
,mpctx
->filename
);
3841 // HACK to get MOV Reference Files working
3843 if (mpctx
->demuxer
&& mpctx
->demuxer
->type
==DEMUXER_TYPE_PLAYLIST
)
3845 unsigned char* playlist_entry
;
3846 play_tree_t
*list
= NULL
, *entry
= NULL
;
3848 current_module
="handle_demux_playlist";
3849 while (ds_get_packet(mpctx
->demuxer
->video
,&playlist_entry
)>0)
3853 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Adding file %s to element entry.\n",
3854 filename_recode(playlist_entry
));
3856 bname
=mp_basename(playlist_entry
);
3857 if ((strlen(bname
)>10) && !strncmp(bname
,"qt",2) && !strncmp(bname
+3,"gateQT",6))
3860 if (!strcmp(playlist_entry
, mpctx
->filename
)) // ignoring self-reference
3863 entry
= play_tree_new();
3865 if (mpctx
->filename
&& !strcmp(mp_basename(playlist_entry
),playlist_entry
)) // add reference path of current file
3867 temp
=malloc((strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
))+strlen(playlist_entry
)+1));
3870 strncpy(temp
, mpctx
->filename
, strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
)));
3871 temp
[strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
))]='\0';
3872 strcat(temp
, playlist_entry
);
3873 if (!strcmp(temp
, mpctx
->filename
)) {
3877 play_tree_add_file(entry
,temp
);
3878 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Resolving reference to %s.\n",temp
);
3883 play_tree_add_file(entry
,playlist_entry
);
3888 play_tree_append_entry(list
,entry
);
3890 free_demuxer(mpctx
->demuxer
);
3891 mpctx
->demuxer
= NULL
;
3895 entry
= play_tree_new();
3896 play_tree_set_child(entry
,list
);
3897 mpctx
->stop_play
= playtree_add_playlist(mpctx
, entry
);
3898 goto goto_next_file
;
3902 if(!mpctx
->demuxer
) {
3903 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Failed to recognize file format.\n");
3904 goto goto_next_file
;
3907 if (mpctx
->demuxer
->matroska_data
.ordered_chapters
)
3908 build_ordered_chapter_timeline(mpctx
);
3910 if (!mpctx
->sources
) {
3911 mpctx
->sources
= talloc_ptrtype(NULL
, mpctx
->sources
);
3912 *mpctx
->sources
= (struct content_source
){.stream
= mpctx
->stream
,
3913 .demuxer
= mpctx
->demuxer
};
3914 mpctx
->num_sources
= 1;
3917 mpctx
->initialized_flags
|=INITIALIZED_DEMUXER
;
3920 if (opts
->ass_enabled
&& ass_library
) {
3921 for (int j
= 0; j
< mpctx
->num_sources
; j
++) {
3922 struct demuxer
*d
= mpctx
->sources
[j
].demuxer
;
3923 for (int i
= 0; i
< d
->num_attachments
; i
++) {
3924 struct demux_attachment
*att
= d
->attachments
+ i
;
3925 if (use_embedded_fonts
&& attachment_is_font(att
))
3926 ass_add_font(ass_library
, att
->name
, att
->data
, att
->data_size
);
3932 current_module
="demux_open2";
3934 mpctx
->d_audio
=mpctx
->demuxer
->audio
;
3935 mpctx
->d_video
=mpctx
->demuxer
->video
;
3936 mpctx
->d_sub
=mpctx
->demuxer
->sub
;
3940 mp_property_do("switch_program", M_PROPERTY_SET
, &tmp
, mpctx
);
3942 // select audio stream
3943 if (mpctx
->num_sources
)
3944 for (int i
= 0; i
< mpctx
->num_sources
; i
++)
3945 select_audio(mpctx
->sources
[i
].demuxer
, opts
->audio_id
,
3948 select_audio(mpctx
->d_audio
->demuxer
, opts
->audio_id
, opts
->audio_lang
);
3951 if((stream_dump_type
)&&(stream_dump_type
<4)){
3953 demux_stream_t
*ds
=NULL
;
3954 current_module
="dump";
3955 // select stream to dump
3956 switch(stream_dump_type
){
3957 case 1: ds
=mpctx
->d_audio
;break;
3958 case 2: ds
=mpctx
->d_video
;break;
3959 case 3: ds
=mpctx
->d_sub
;break;
3962 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"dump: FATAL: Selected stream missing!\n");
3963 exit_player(mpctx
, EXIT_ERROR
);
3965 // disable other streams:
3966 if(mpctx
->d_audio
&& mpctx
->d_audio
!=ds
) {ds_free_packs(mpctx
->d_audio
); mpctx
->d_audio
->id
=-2; }
3967 if(mpctx
->d_video
&& mpctx
->d_video
!=ds
) {ds_free_packs(mpctx
->d_video
); mpctx
->d_video
->id
=-2; }
3968 if(mpctx
->d_sub
&& mpctx
->d_sub
!=ds
) {ds_free_packs(mpctx
->d_sub
); mpctx
->d_sub
->id
=-2; }
3970 f
=fopen(opts
->stream_dump_name
,"wb");
3972 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"Cannot open dump file.\n");
3973 exit_player(mpctx
, EXIT_ERROR
);
3976 unsigned char* start
;
3977 int in_size
=ds_get_packet(ds
,&start
);
3978 if( (mpctx
->demuxer
->file_format
==DEMUXER_TYPE_AVI
|| mpctx
->demuxer
->file_format
==DEMUXER_TYPE_ASF
|| mpctx
->demuxer
->file_format
==DEMUXER_TYPE_MOV
)
3979 && stream_dump_type
==2) fwrite(&in_size
,1,4,f
);
3980 if(in_size
>0) fwrite(start
,in_size
,1,f
);
3981 if (opts
->chapterrange
[1] > 0) {
3982 int cur_chapter
= demuxer_get_current_chapter(mpctx
->demuxer
, 0);
3983 if(cur_chapter
!=-1 && cur_chapter
+1 > opts
->chapterrange
[1])
3988 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Core dumped ;)\n");
3989 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
3992 mpctx
->sh_audio
=mpctx
->d_audio
->sh
;
3993 mpctx
->sh_video
=mpctx
->d_video
->sh
;
3995 if(mpctx
->sh_video
){
3997 current_module
="video_read_properties";
3998 if(!video_read_properties(mpctx
->sh_video
)) {
3999 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Video: Cannot read properties.\n");
4000 mpctx
->sh_video
=mpctx
->d_video
->sh
=NULL
;
4002 mp_tmsg(MSGT_CPLAYER
,MSGL_V
,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
4003 mpctx
->demuxer
->file_format
,mpctx
->sh_video
->format
, mpctx
->sh_video
->disp_w
,mpctx
->sh_video
->disp_h
,
4004 mpctx
->sh_video
->fps
,mpctx
->sh_video
->frametime
4007 /* need to set fps here for output encoders to pick it up in their init */
4009 mpctx
->sh_video
->fps
=force_fps
;
4010 mpctx
->sh_video
->frametime
=1.0f
/mpctx
->sh_video
->fps
;
4012 vo_fps
= mpctx
->sh_video
->fps
;
4014 if(!mpctx
->sh_video
->fps
&& !force_fps
){
4015 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"FPS not specified in the header or invalid, use the -fps option.\n");
4016 mpctx
->opts
.correct_pts
= 1;
4022 if(!mpctx
->sh_video
&& !mpctx
->sh_audio
){
4023 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
, "No stream found.\n");
4025 if(mpctx
->stream
->type
== STREAMTYPE_DVB
)
4028 int v
= mpctx
->last_dvb_step
;
4030 dir
= DVB_CHANNEL_HIGHER
;
4032 dir
= DVB_CHANNEL_LOWER
;
4034 if(dvb_step_channel(mpctx
->stream
, dir
)) {
4035 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4036 mpctx
->dvbin_reopen
= 1;
4040 goto goto_next_file
; // exit_player(_("Fatal error"));
4043 /* display clip info */
4044 demux_info_print(mpctx
->demuxer
);
4046 //================== Read SUBTITLES (DVD & TEXT) ==========================
4047 if(vo_spudec
==NULL
&&
4048 (mpctx
->stream
->type
==STREAMTYPE_DVD
|| mpctx
->stream
->type
== STREAMTYPE_DVDNAV
)){
4049 init_vo_spudec(mpctx
);
4052 // after reading video params we should load subtitles because
4053 // we know fps so now we can adjust subtitle time to ~6 seconds AST
4055 current_module
="read_subtitles_file";
4056 double sub_fps
= mpctx
->sh_video
? mpctx
->sh_video
->fps
: 25;
4058 for (i
= 0; sub_name
[i
] != NULL
; ++i
)
4059 add_subtitles(mpctx
, sub_name
[i
], sub_fps
, 0);
4061 if(sub_auto
) { // auto load sub file ...
4062 char *psub
= get_path( "sub/" );
4063 char **tmp
= sub_filenames((psub
? psub
: ""), mpctx
->filename
);
4065 free(psub
); // release the buffer created by get_path() above
4067 add_subtitles(mpctx
, tmp
[i
], sub_fps
, 1);
4072 if (mpctx
->set_of_sub_size
> 0)
4073 mpctx
->sub_counts
[SUB_SOURCE_SUBS
] = mpctx
->set_of_sub_size
;
4075 if (select_subtitle(mpctx
)) {
4077 switch (stream_dump_type
) {
4078 case 3: list_sub_file(subdata
); break;
4079 case 4: dump_mpsub(subdata
, mpctx
->sh_video
->fps
); break;
4080 case 6: dump_srt(subdata
, mpctx
->sh_video
->fps
); break;
4081 case 7: dump_microdvd(subdata
, mpctx
->sh_video
->fps
); break;
4082 case 8: dump_jacosub(subdata
, mpctx
->sh_video
->fps
); break;
4083 case 9: dump_sami(subdata
, mpctx
->sh_video
->fps
); break;
4087 print_file_properties(mpctx
, mpctx
->filename
);
4089 if(!mpctx
->sh_video
) goto main
; // audio-only
4091 if(!reinit_video_chain(mpctx
)) {
4092 if(!mpctx
->sh_video
){
4093 if(!mpctx
->sh_audio
) goto goto_next_file
;
4094 goto main
; // exit_player(_("Fatal error"));
4098 if(mpctx
->sh_video
->output_flags
& 0x08 && vo_spudec
)
4099 spudec_set_hw_spu(vo_spudec
,mpctx
->video_out
);
4101 #ifdef CONFIG_FREETYPE
4102 force_load_font
= 1;
4105 //================== MAIN: ==========================
4107 current_module
="main";
4110 char* msg
= property_expand_string(mpctx
, playing_msg
);
4111 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"%s",msg
);
4116 // Disable the term OSD in verbose mode
4117 if(verbose
) term_osd
= 0;
4120 int frame_time_remaining
=0; // flag
4123 // Make sure old OSD does not stay around,
4124 // e.g. with -fixed-vo and same-resolution files
4126 update_osd_msg(mpctx
);
4128 //================ SETUP AUDIO ==========================
4130 if(mpctx
->sh_audio
){
4131 reinit_audio_chain(mpctx
);
4132 if (mpctx
->sh_audio
&& mpctx
->sh_audio
->codec
)
4133 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
, "ID_AUDIO_CODEC=%s\n", mpctx
->sh_audio
->codec
->name
);
4136 current_module
="av_init";
4138 if(mpctx
->sh_video
){
4139 mpctx
->sh_video
->timer
=0;
4141 audio_delay
+= mpctx
->sh_video
->stream_delay
;
4143 if(mpctx
->sh_audio
){
4144 if (start_volume
>= 0)
4145 mixer_setvolume(&mpctx
->mixer
, start_volume
, start_volume
);
4147 audio_delay
-= mpctx
->sh_audio
->stream_delay
;
4148 mpctx
->delay
=-audio_delay
;
4151 if(!mpctx
->sh_audio
){
4152 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Audio: no sound\n");
4153 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Freeing %d unused audio chunks.\n",mpctx
->d_audio
->packs
);
4154 ds_free_packs(mpctx
->d_audio
); // free buffered chunks
4155 //mpctx->d_audio->id=-2; // do not read audio chunks
4156 //uninit_player(mpctx, INITIALIZED_AO); // close device
4158 if(!mpctx
->sh_video
){
4159 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Video: no video\n");
4160 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Freeing %d unused video chunks.\n",mpctx
->d_video
->packs
);
4161 ds_free_packs(mpctx
->d_video
);
4162 mpctx
->d_video
->id
=-2;
4163 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
4166 if (!mpctx
->sh_video
&& !mpctx
->sh_audio
)
4167 goto goto_next_file
;
4169 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
4170 if(force_fps
&& mpctx
->sh_video
){
4171 vo_fps
= mpctx
->sh_video
->fps
=force_fps
;
4172 mpctx
->sh_video
->frametime
=1.0f
/mpctx
->sh_video
->fps
;
4173 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx
->sh_video
->fps
,mpctx
->sh_video
->frametime
);
4176 mp_input_set_section(mpctx
->input
, NULL
);
4177 //TODO: add desired (stream-based) sections here
4178 if (mpctx
->stream
->type
==STREAMTYPE_TV
) mp_input_set_section(mpctx
->input
, "tv");
4179 if (mpctx
->stream
->type
==STREAMTYPE_DVDNAV
) mp_input_set_section(mpctx
->input
, "dvdnav");
4181 //==================== START PLAYING =======================
4183 if(opts
->loop_times
>1) opts
->loop_times
--; else
4184 if(opts
->loop_times
==1) opts
->loop_times
= -1;
4186 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Starting playback...\n");
4188 total_time_usage_start
=GetTimer();
4189 audio_time_usage
=0; video_time_usage
=0; vout_time_usage
=0;
4190 total_frame_cnt
=0; drop_frame_cnt
=0; // fix for multifile fps benchmark
4191 play_n_frames
=play_n_frames_mf
;
4193 if(play_n_frames
==0){
4194 mpctx
->stop_play
=PT_NEXT_ENTRY
; goto goto_next_file
;
4197 // If there's a timeline force an absolute seek to initialize state
4198 if (seek_to_sec
|| mpctx
->timeline
) {
4199 seek(mpctx
, seek_to_sec
, SEEK_ABSOLUTE
);
4200 end_at
.pos
+= seek_to_sec
;
4202 if (opts
->chapterrange
[0] > 0) {
4204 if (seek_chapter(mpctx
, opts
->chapterrange
[0]-1, &pts
, NULL
) >= 0
4206 seek(mpctx
, pts
, SEEK_ABSOLUTE
);
4209 if (end_at
.type
== END_AT_SIZE
) {
4210 mp_tmsg(MSGT_CPLAYER
, MSGL_WARN
, "Option -endpos in MPlayer does not yet support size units.\n");
4211 end_at
.type
= END_AT_NONE
;
4214 #ifdef CONFIG_DVDNAV
4215 mp_dvdnav_context_free(mpctx
);
4216 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
4217 mp_dvdnav_read_wait(mpctx
->stream
, 0, 1);
4218 mp_dvdnav_cell_has_changed(mpctx
->stream
,1);
4222 get_relative_time(mpctx
); // reset current delta
4223 mpctx
->time_frame
= 0;
4224 mpctx
->drop_message_shown
= 0;
4225 mpctx
->restart_playback
= true;
4226 mpctx
->total_avsync_change
= 0;
4227 mpctx
->last_chapter_seek
= -1;
4228 // Make sure VO knows current pause state
4229 if (mpctx
->sh_video
)
4230 vo_control(mpctx
->video_out
, mpctx
->paused
? VOCTRL_PAUSE
: VOCTRL_RESUME
,
4233 while(!mpctx
->stop_play
){
4234 float aq_sleep_time
=0;
4236 if (opts
->chapterrange
[1] > 0) {
4237 int cur_chapter
= get_current_chapter(mpctx
);
4238 if(cur_chapter
!=-1 && cur_chapter
+1 > opts
->chapterrange
[1])
4239 goto goto_next_file
;
4242 if(!mpctx
->sh_audio
&& mpctx
->d_audio
->sh
) {
4243 mpctx
->sh_audio
= mpctx
->d_audio
->sh
;
4244 mpctx
->sh_audio
->ds
= mpctx
->d_audio
;
4245 reinit_audio_chain(mpctx
);
4248 /*========================== PLAY AUDIO ============================*/
4250 if (mpctx
->sh_audio
&& !mpctx
->paused
4251 && (!mpctx
->restart_playback
|| !mpctx
->sh_video
))
4252 if (!fill_audio_out_buffers(mpctx
))
4253 // at eof, all audio at least written to ao
4254 if (!mpctx
->sh_video
)
4255 mpctx
->stop_play
= AT_END_OF_FILE
;
4258 if(!mpctx
->sh_video
) {
4259 // handle audio-only case:
4261 // sh_audio can be NULL due to video stream switching
4262 // TODO: handle this better
4263 if (mpctx
->sh_audio
)
4264 a_pos
= playing_audio_pts(mpctx
);
4266 print_status(mpctx
, a_pos
, false);
4268 if(end_at
.type
== END_AT_TIME
&& end_at
.pos
< a_pos
)
4269 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4270 update_subtitles(mpctx
, &mpctx
->opts
, NULL
, a_pos
, mpctx
->video_offset
,
4272 update_osd_msg(mpctx
);
4276 /*========================== PLAY VIDEO ============================*/
4278 vo_pts
=mpctx
->sh_video
->timer
*90000.0;
4279 vo_fps
=mpctx
->sh_video
->fps
;
4281 blit_frame
= mpctx
->video_out
->frame_loaded
;
4283 double frame_time
= update_video(mpctx
);
4284 blit_frame
= mpctx
->video_out
->frame_loaded
;
4285 mp_dbg(MSGT_AVSYNC
,MSGL_DBG2
,"*** ftime=%5.3f ***\n",frame_time
);
4286 if (mpctx
->sh_video
->vf_initialized
< 0) {
4287 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
, "\nFATAL: Could not initialize video filters (-vf) or video output (-vo).\n");
4288 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4289 goto goto_next_file
;
4292 struct sh_video
*sh_video
= mpctx
->sh_video
;
4293 update_subtitles(mpctx
, &mpctx
->opts
, sh_video
, sh_video
->pts
,
4294 mpctx
->video_offset
, mpctx
->d_sub
, 0);
4295 update_teletext(sh_video
, mpctx
->demuxer
, 0);
4296 update_osd_msg(mpctx
);
4297 struct vf_instance
*vf
= mpctx
->sh_video
->vfilter
;
4298 vf
->control(vf
, VFCTRL_DRAW_EOSD
, NULL
);
4299 vf
->control(vf
, VFCTRL_DRAW_OSD
, mpctx
->osd
);
4303 mpctx
->stop_play
= AT_END_OF_FILE
;
4304 else if (!mpctx
->restart_playback
) {
4305 mpctx
->time_frame
+= frame_time
/ opts
->playback_speed
;
4306 adjust_sync(mpctx
, frame_time
);
4309 if (mpctx
->timeline
) {
4310 struct timeline_part
*next
= mpctx
->timeline
+ mpctx
->timeline_part
+ 1;
4311 if (mpctx
->sh_video
->pts
>= next
->start
4312 || mpctx
->stop_play
== AT_END_OF_FILE
4313 && mpctx
->timeline_part
+ 1 < mpctx
->num_timeline_parts
) {
4314 seek(mpctx
, next
->start
, SEEK_ABSOLUTE
);
4319 // ==========================================================================
4321 // current_module="draw_osd";
4322 // if(vo_config_count) mpctx->video_out->draw_osd();
4324 current_module
="vo_check_events";
4325 vo_check_events(mpctx
->video_out
);
4328 if (stop_xscreensaver
) {
4329 current_module
= "stop_xscreensaver";
4330 xscreensaver_heartbeat(mpctx
->x11_state
);
4333 if (heartbeat_cmd
) {
4334 static unsigned last_heartbeat
;
4335 unsigned now
= GetTimerMS();
4336 if (now
- last_heartbeat
> 30000) {
4337 last_heartbeat
= now
;
4338 system(heartbeat_cmd
);
4342 frame_time_remaining
= sleep_until_update(mpctx
, &mpctx
->time_frame
, &aq_sleep_time
);
4344 //====================== FLIP PAGE (VIDEO BLT): =========================
4346 current_module
="flip_page";
4347 if (!frame_time_remaining
&& blit_frame
) {
4348 unsigned int t2
=GetTimer();
4349 unsigned int pts_us
= mpctx
->last_time
+ mpctx
->time_frame
* 1e6
;
4351 double pts2
= mpctx
->video_out
->next_pts2
;
4352 if (pts2
!= MP_NOPTS_VALUE
&& opts
->correct_pts
4353 && !mpctx
->restart_playback
) {
4354 // expected A/V sync correction is ignored
4355 double diff
= (pts2
- mpctx
->sh_video
->pts
);
4356 diff
/= opts
->playback_speed
;
4357 if (mpctx
->time_frame
< 0)
4358 diff
+= mpctx
->time_frame
;
4363 duration
= diff
* 1e6
;
4365 vo_flip_page(mpctx
->video_out
, pts_us
|1, duration
);
4367 mpctx
->last_vo_flip_duration
= (GetTimer() - t2
) * 0.000001;
4368 vout_time_usage
+= mpctx
->last_vo_flip_duration
;
4369 if (mpctx
->video_out
->driver
->flip_page_timed
) {
4370 // No need to adjust sync based on flip speed
4371 mpctx
->last_vo_flip_duration
= 0;
4372 // For print_status - VO call finishing early is OK for sync
4373 mpctx
->time_frame
-= get_relative_time(mpctx
);
4375 if (mpctx
->restart_playback
) {
4376 mpctx
->syncing_audio
= true;
4377 if (mpctx
->sh_audio
&& !mpctx
->paused
)
4378 fill_audio_out_buffers(mpctx
);
4379 mpctx
->restart_playback
= false;
4380 mpctx
->time_frame
= 0;
4381 get_relative_time(mpctx
);
4383 print_status(mpctx
, MP_NOPTS_VALUE
, true);
4386 print_status(mpctx
, MP_NOPTS_VALUE
, false);
4388 //============================ Auto QUALITY ============================
4390 /*Output quality adjustments:*/
4392 current_module
="autoq";
4393 // float total=0.000001f * (GetTimer()-aq_total_time);
4394 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4395 if(output_quality
<auto_quality
&& aq_sleep_time
>0)
4398 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4399 if(output_quality
>1 && aq_sleep_time
<0)
4402 if(output_quality
>0 && aq_sleep_time
<-0.050f
) // 50ms
4404 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4405 set_video_quality(mpctx
->sh_video
,output_quality
);
4408 if (!frame_time_remaining
&& blit_frame
) {
4409 if (play_n_frames
>= 0) {
4411 if (play_n_frames
<= 0)
4412 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4414 if (mpctx
->step_frames
> 0) {
4415 mpctx
->step_frames
--;
4416 if (mpctx
->step_frames
== 0)
4417 pause_player(mpctx
);
4422 // FIXME: add size based support for -endpos
4423 if (end_at
.type
== END_AT_TIME
&&
4424 !frame_time_remaining
&& end_at
.pos
<= mpctx
->sh_video
->pts
)
4425 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4427 } // end if(mpctx->sh_video)
4429 #ifdef CONFIG_DVDNAV
4430 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
4432 mp_dvdnav_get_highlight (mpctx
->stream
, &hl
);
4433 if (!vo_spudec
|| !spudec_apply_palette_crop(vo_spudec
, hl
.palette
, hl
.sx
, hl
.sy
, hl
.ex
, hl
.ey
)) {
4434 osd_set_nav_box (hl
.sx
, hl
.sy
, hl
.ex
, hl
.ey
);
4435 vo_osd_changed (OSDTYPE_DVDNAV
);
4437 osd_set_nav_box(0, 0, 0, 0);
4438 vo_osd_changed(OSDTYPE_DVDNAV
);
4439 vo_osd_changed(OSDTYPE_SPU
);
4442 if (mp_dvdnav_stream_has_changed(mpctx
->stream
)) {
4444 if (mpctx
->sh_video
&&
4445 stream_control (mpctx
->demuxer
->stream
,
4446 STREAM_CTRL_GET_ASPECT_RATIO
, &ar
)
4447 != STREAM_UNSUPPORTED
)
4448 mpctx
->sh_video
->stream_aspect
= ar
;
4453 //================= Keyboard events, SEEKing ====================
4455 current_module
="key_events";
4460 while ((cmd
= mp_input_get_cmd(mpctx
->input
, 0, 0)) != NULL
) {
4461 run_command(mpctx
, cmd
);
4463 if (mpctx
->stop_play
)
4465 if (mpctx
->rel_seek_secs
|| mpctx
->abs_seek_pos
) {
4466 cmd
= mp_input_get_cmd(mpctx
->input
, 0, 1);
4467 /* Allow seek commands to be combined, but execute the real seek
4468 * before processing other commands */
4469 if (!cmd
|| cmd
->id
!= MP_CMD_SEEK
)
4473 if (!mpctx
->paused
|| mpctx
->stop_play
|| mpctx
->rel_seek_secs
4474 || mpctx
->abs_seek_pos
)
4476 if (mpctx
->sh_video
) {
4477 update_osd_msg(mpctx
);
4478 int hack
= vo_osd_changed(0);
4479 vo_osd_changed(hack
);
4481 if (redraw_osd(mpctx
->sh_video
, mpctx
->osd
) < 0) {
4482 add_step_frame(mpctx
);
4494 if (step_sec
> 0 && !mpctx
->paused
) {
4495 mpctx
->osd_function
=OSD_FFW
;
4496 mpctx
->rel_seek_secs
+=step_sec
;
4502 if(mpctx
->stop_play
==AT_END_OF_FILE
&& opts
->loop_times
>=0) {
4503 mp_msg(MSGT_CPLAYER
,MSGL_V
,"loop_times = %d\n", opts
->loop_times
);
4505 if(opts
->loop_times
>1) opts
->loop_times
--; else
4506 if(opts
->loop_times
==1) opts
->loop_times
=-1;
4507 play_n_frames
=play_n_frames_mf
;
4509 mpctx
->abs_seek_pos
=SEEK_ABSOLUTE
; mpctx
->rel_seek_secs
=seek_to_sec
;
4512 if(mpctx
->rel_seek_secs
|| mpctx
->abs_seek_pos
){
4513 seek(mpctx
, mpctx
->rel_seek_secs
, mpctx
->abs_seek_pos
);
4515 mpctx
->rel_seek_secs
=0;
4516 mpctx
->abs_seek_pos
=0;
4519 } // while(!mpctx->stop_play)
4521 mp_msg(MSGT_GLOBAL
,MSGL_V
,"EOF code: %d \n",mpctx
->stop_play
);
4524 if(mpctx
->dvbin_reopen
)
4526 mpctx
->stop_play
= 0;
4527 uninit_player(mpctx
, INITIALIZED_ALL
-(INITIALIZED_STREAM
|INITIALIZED_GETCH2
|(opts
->fixed_vo
?INITIALIZED_VO
:0)));
4528 cache_uninit(mpctx
->stream
);
4529 mpctx
->dvbin_reopen
= 0;
4530 goto goto_enable_cache
;
4535 goto_next_file
: // don't jump here after ao/vo/getch initialization!
4537 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"\n");
4540 double tot
=video_time_usage
+vout_time_usage
+audio_time_usage
;
4541 double total_time_usage
;
4542 total_time_usage_start
=GetTimer()-total_time_usage_start
;
4543 total_time_usage
= (float)total_time_usage_start
*0.000001;
4544 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4545 video_time_usage
,vout_time_usage
,audio_time_usage
,
4546 total_time_usage
-tot
,total_time_usage
);
4547 if(total_time_usage
>0.0)
4548 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4549 100.0*video_time_usage
/total_time_usage
,
4550 100.0*vout_time_usage
/total_time_usage
,
4551 100.0*audio_time_usage
/total_time_usage
,
4552 100.0*(total_time_usage
-tot
)/total_time_usage
,
4554 if(total_frame_cnt
&& frame_dropping
)
4555 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4556 total_frame_cnt
-drop_frame_cnt
,
4557 (total_time_usage
>0.5)?((total_frame_cnt
-drop_frame_cnt
)/total_time_usage
):0,
4559 100*drop_frame_cnt
/total_frame_cnt
,
4561 (total_time_usage
>0.5)?(total_frame_cnt
/total_time_usage
):0);
4564 // time to uninit all, except global stuff:
4565 int uninitialize_parts
= INITIALIZED_ALL
;
4567 uninitialize_parts
-= INITIALIZED_VO
;
4568 if (opts
->gapless_audio
&& mpctx
->stop_play
== AT_END_OF_FILE
)
4569 uninitialize_parts
-= INITIALIZED_AO
;
4570 uninit_player(mpctx
, uninitialize_parts
);
4572 if(mpctx
->set_of_sub_size
> 0) {
4573 current_module
="sub_free";
4574 for(i
= 0; i
< mpctx
->set_of_sub_size
; ++i
) {
4575 sub_free(mpctx
->set_of_subtitles
[i
]);
4577 if(mpctx
->set_of_ass_tracks
[i
])
4578 ass_free_track( mpctx
->set_of_ass_tracks
[i
] );
4581 mpctx
->set_of_sub_size
= 0;
4583 vo_sub_last
= vo_sub
=NULL
;
4588 ass_clear_fonts(ass_library
);
4591 if (!mpctx
->stop_play
) // In case some goto jumped here...
4592 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4594 int playtree_direction
= 1;
4596 if(mpctx
->stop_play
== PT_NEXT_ENTRY
|| mpctx
->stop_play
== PT_PREV_ENTRY
) {
4597 if(play_tree_iter_step(mpctx
->playtree_iter
,mpctx
->play_tree_step
,0) != PLAY_TREE_ITER_ENTRY
) {
4598 play_tree_iter_free(mpctx
->playtree_iter
);
4599 mpctx
->playtree_iter
= NULL
;
4601 mpctx
->play_tree_step
= 1;
4602 } else if(mpctx
->stop_play
== PT_UP_NEXT
|| mpctx
->stop_play
== PT_UP_PREV
) {
4603 int direction
= mpctx
->stop_play
== PT_UP_NEXT
? 1 : -1;
4604 if(mpctx
->playtree_iter
) {
4605 if(play_tree_iter_up_step(mpctx
->playtree_iter
,direction
,0) != PLAY_TREE_ITER_ENTRY
) {
4606 play_tree_iter_free(mpctx
->playtree_iter
);
4607 mpctx
->playtree_iter
= NULL
;
4610 } else if (mpctx
->stop_play
== PT_STOP
) {
4611 play_tree_iter_free(mpctx
->playtree_iter
);
4612 mpctx
->playtree_iter
= NULL
;
4613 } else { // NEXT PREV SRC
4614 playtree_direction
= mpctx
->stop_play
== PT_PREV_SRC
? -1 : 1;
4617 while(mpctx
->playtree_iter
!= NULL
) {
4618 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
, playtree_direction
);
4619 if(mpctx
->filename
== NULL
) {
4620 if(play_tree_iter_step(mpctx
->playtree_iter
, playtree_direction
, 0) != PLAY_TREE_ITER_ENTRY
) {
4621 play_tree_iter_free(mpctx
->playtree_iter
);
4622 mpctx
->playtree_iter
= NULL
;
4628 if(mpctx
->playtree_iter
!= NULL
|| player_idle_mode
){
4629 if(!mpctx
->playtree_iter
) mpctx
->filename
= NULL
;
4630 mpctx
->stop_play
= 0;
4631 goto play_next_file
;
4635 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
4639 #endif /* DISABLE_MAIN */