3 /// \ingroup Properties Command2Property OSDMsgStack
11 #if defined(__MINGW32__) || defined(__CYGWIN__)
12 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
18 // #include <sys/mman.h>
19 #include <sys/types.h>
21 #include <sys/ioctl.h>
24 #define SIGHUP 1 /* hangup */
25 #define SIGQUIT 3 /* quit */
26 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
27 #define SIGBUS 10 /* bus error */
28 #define SIGPIPE 13 /* broken pipe */
43 #define HELP_MP_DEFINE_STATIC
49 #include "access_mpcontext.h"
50 #include "m_property.h"
52 #include "cfg-mplayer-def.h"
54 #include "libavutil/intreadwrite.h"
56 #include "subreader.h"
59 #include "libvo/video_out.h"
61 #include "libvo/font_load.h"
62 #include "libvo/sub.h"
65 #include "libvo/x11_common.h"
68 #include "libao2/audio_out.h"
70 #include "codec-cfg.h"
77 #include "osdep/getch2.h"
78 #include "osdep/timer.h"
79 #include "osdep/findfiles.h"
81 #include "input/input.h"
83 const int under_mencoder
= 0;
85 int player_idle_mode
=0;
87 int enable_mouse_movements
=0;
88 float start_volume
= -1;
90 #include "osdep/priority.h"
94 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
98 #include <linux/rtc.h>
101 #define RTC_IRQP_SET RTCIO_IRQP_SET
102 #define RTC_PIE_ON RTCIO_PIE_ON
103 #endif /* __linux__ */
104 #endif /* HAVE_RTC */
107 #include "stream/tv.h"
110 #include "stream/stream_radio.h"
114 #include "stream/dvbin.h"
115 #include "stream/cache2.h"
118 //**************************************************************************//
120 //**************************************************************************//
121 #include "playtree.h"
122 #include "playtreeparser.h"
124 //**************************************************************************//
126 //**************************************************************************//
127 #include "parser-cfg.h"
128 #include "parser-mpcmd.h"
130 //**************************************************************************//
132 //**************************************************************************//
134 static int cfg_inc_verbose(m_option_t
*conf
){ ++verbose
; return 0;}
136 #include "get_path.h"
138 //**************************************************************************//
139 //**************************************************************************//
140 // Input media streaming & demultiplexer:
141 //**************************************************************************//
143 static int max_framesize
=0;
145 #include "stream/stream.h"
146 #include "libmpdemux/demuxer.h"
147 #include "libmpdemux/stheader.h"
149 #ifdef CONFIG_DVDREAD
150 #include "stream/stream_dvd.h"
154 #include "stream/stream_dvdnav.h"
157 #include "libmpcodecs/dec_audio.h"
158 #include "libmpcodecs/dec_video.h"
159 #include "libmpcodecs/mp_image.h"
160 #include "libmpcodecs/vf.h"
161 #include "libmpcodecs/vd.h"
167 #include "defaultopts.h"
169 #define Exit_SIGILL_RTCpuSel _(\
170 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
171 " It may be a bug in our new runtime CPU-detection code...\n"\
172 " Please read DOCS/HTML/en/bugreports.html.\n")
174 #define Exit_SIGILL _(\
175 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
176 " It usually happens when you run it on a CPU different than the one it was\n"\
177 " compiled/optimized for.\n"\
180 #define Exit_SIGSEGV_SIGFPE _(\
181 "- MPlayer crashed by bad usage of CPU/FPU/RAM.\n"\
182 " Recompile MPlayer with --enable-debug and make a 'gdb' backtrace and\n"\
183 " disassembly. Details in DOCS/HTML/en/bugreports_what.html#bugreports_crash.\n")
185 #define Exit_SIGCRASH _(\
186 "- MPlayer crashed. This shouldn't happen.\n"\
187 " It can be a bug in the MPlayer code _or_ in your drivers _or_ in your\n"\
188 " gcc version. If you think it's MPlayer's fault, please read\n"\
189 " DOCS/HTML/en/bugreports.html and follow the instructions there. We can't and\n"\
190 " won't help unless you provide this information when reporting a possible bug.\n")
192 #define SystemTooSlow _("\n\n"\
193 " ************************************************\n"\
194 " **** Your system is too SLOW to play this! ****\n"\
195 " ************************************************\n\n"\
196 "Possible reasons, problems, workarounds:\n"\
197 "- Most common: broken/buggy _audio_ driver\n"\
198 " - Try -ao sdl or use the OSS emulation of ALSA.\n"\
199 " - Experiment with different values for -autosync, 30 is a good start.\n"\
200 "- Slow video output\n"\
201 " - Try a different -vo driver (-vo help for a list) or try -framedrop!\n"\
203 " - Don't try to play a big DVD/DivX on a slow CPU! Try some of the lavdopts,\n"\
204 " e.g. -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all.\n"\
206 " - Try various combinations of -nobps -ni -forceidx -mc 0.\n"\
207 "- Slow media (NFS/SMB mounts, DVD, VCD etc)\n"\
208 " - Try -cache 8192.\n"\
209 "- Are you using -cache to play a non-interleaved AVI file?\n"\
210 " - Try -nocache.\n"\
211 "Read DOCS/HTML/en/video.html for tuning/speedup tips.\n"\
212 "If none of this helps you, read DOCS/HTML/en/bugreports.html.\n\n")
215 //**************************************************************************//
216 //**************************************************************************//
218 // Common FIFO functions, and keyboard/event FIFO code
220 int noconsolecontrols
=0;
221 //**************************************************************************//
224 double video_time_usage
=0;
225 double vout_time_usage
=0;
226 static double audio_time_usage
=0;
227 static int total_time_usage_start
=0;
228 static int total_frame_cnt
=0;
229 static int drop_frame_cnt
=0; // total number of dropped frames
234 static int output_quality
=0;
236 static int list_properties
= 0;
239 static char* term_osd_esc
= "\x1b[A\r\x1b[K";
240 static char* playing_msg
= NULL
;
242 static double seek_to_sec
;
243 static off_t seek_to_byte
=0;
244 static off_t step_sec
=0;
246 static m_time_size_t end_at
= { .type
= END_AT_NONE
, .pos
= 0 };
249 int autosync
=0; // 30 might be a good default value.
252 char **audio_codec_list
=NULL
; // override audio codec
253 char **video_codec_list
=NULL
; // override video codec
254 char **audio_fm_list
=NULL
; // override audio codec family
255 char **video_fm_list
=NULL
; // override video codec family
258 extern char *demuxer_name
; // override demuxer
259 extern char *audio_demuxer_name
; // override audio demuxer
260 extern char *sub_demuxer_name
; // override sub demuxer
263 char* audio_lang
=NULL
;
264 char* dvdsub_lang
=NULL
;
265 static char* spudec_ifo
=NULL
;
266 int forced_subs_only
=0;
270 int stream_cache_size
=-1;
271 #ifdef CONFIG_STREAM_CACHE
272 extern int cache_fill_status
;
274 float stream_cache_min_percent
=20.0;
275 float stream_cache_seek_min_percent
=50.0;
277 #define cache_fill_status 0
281 static char *stream_dump_name
="stream.dump";
282 int stream_dump_type
=0;
285 static float default_max_pts_correction
=-1;//0.01f;
287 static int ignore_start
=0;
289 static int softsleep
=0;
292 static int force_srate
=0;
293 static int audio_output_format
=-1; // AF_FORMAT_UNKNOWN
294 int frame_dropping
=0; // option 0=no drop 1= drop vo 2= drop decode
295 static int play_n_frames
=-1;
296 static int play_n_frames_mf
=-1;
299 char *font_name
=NULL
;
300 char *sub_font_name
=NULL
;
301 extern int font_fontconfig
;
302 float font_factor
=0.75;
303 char **sub_name
=NULL
;
307 char *vobsub_name
=NULL
;
308 /*DSP!!char *dsp=NULL;*/
310 int suboverlap_enabled
= 1;
313 #include "libass/ass.h"
314 #include "libass/ass_mp.h"
317 char* current_module
=NULL
; // for debugging
323 #include "m_struct.h"
324 #include "libmenu/menu.h"
325 void vf_menu_pause_update(struct vf_instance
* vf
);
326 extern vf_info_t vf_info_menu
;
327 static const vf_info_t
* const libmenu_vfs
[] = {
331 static vf_instance_t
* vf_menu
= NULL
;
333 static char* menu_cfg
= NULL
;
334 static char* menu_root
= "main";
339 static int nortc
= 1;
340 static char* rtc_device
;
343 edl_record_ptr edl_records
= NULL
; ///< EDL entries memory area
344 edl_record_ptr next_edl_record
= NULL
; ///< only for traversing edl_records
345 FILE* edl_fd
= NULL
; ///< fd to write to when in -edlout mode.
346 int use_filedir_conf
;
348 #include "mpcommon.h"
351 #include "metadata.h"
353 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
355 const void *mpctx_get_video_out(MPContext
*mpctx
)
357 return mpctx
->video_out
;
360 const void *mpctx_get_audio_out(MPContext
*mpctx
)
362 return mpctx
->audio_out
;
365 void *mpctx_get_demuxer(MPContext
*mpctx
)
367 return mpctx
->demuxer
;
370 void *mpctx_get_playtree_iter(MPContext
*mpctx
)
372 return mpctx
->playtree_iter
;
375 void *mpctx_get_mixer(MPContext
*mpctx
)
377 return &mpctx
->mixer
;
380 int mpctx_get_global_sub_size(MPContext
*mpctx
)
382 return mpctx
->global_sub_size
;
385 int mpctx_get_osd_function(MPContext
*mpctx
)
387 return mpctx
->osd_function
;
390 static float get_relative_time(struct MPContext
*mpctx
)
392 unsigned int new_time
= GetTimer();
393 unsigned int delta
= new_time
- mpctx
->last_time
;
394 mpctx
->last_time
= new_time
;
395 return delta
* 0.000001;
398 static int is_valid_metadata_type(struct MPContext
*mpctx
, metadata_t type
) {
401 /* check for valid video stream */
402 case META_VIDEO_CODEC
:
403 case META_VIDEO_BITRATE
:
404 case META_VIDEO_RESOLUTION
:
406 if (!mpctx
->sh_video
)
411 /* check for valid audio stream */
412 case META_AUDIO_CODEC
:
413 case META_AUDIO_BITRATE
:
414 case META_AUDIO_SAMPLES
:
416 if (!mpctx
->sh_audio
)
421 /* check for valid demuxer */
422 case META_INFO_TITLE
:
423 case META_INFO_ARTIST
:
424 case META_INFO_ALBUM
:
426 case META_INFO_COMMENT
:
427 case META_INFO_TRACK
:
428 case META_INFO_GENRE
:
442 static char *get_demuxer_info(struct MPContext
*mpctx
, char *tag
) {
443 char **info
= mpctx
->demuxer
->info
;
449 for (n
= 0; info
[2*n
] != NULL
; n
++)
450 if (!strcasecmp (info
[2*n
], tag
))
453 return info
[2*n
+1] ? strdup (info
[2*n
+1]) : NULL
;
456 char *get_metadata(struct MPContext
*mpctx
, metadata_t type
)
459 sh_audio_t
* const sh_audio
= mpctx
->sh_audio
;
460 sh_video_t
* const sh_video
= mpctx
->sh_video
;
462 if (!is_valid_metadata_type(mpctx
, type
))
469 return strdup (mp_basename2 (mpctx
->filename
));
472 case META_VIDEO_CODEC
:
474 if (sh_video
->format
== 0x10000001)
475 meta
= strdup ("mpeg1");
476 else if (sh_video
->format
== 0x10000002)
477 meta
= strdup ("mpeg2");
478 else if (sh_video
->format
== 0x10000004)
479 meta
= strdup ("mpeg4");
480 else if (sh_video
->format
== 0x10000005)
481 meta
= strdup ("h264");
482 else if (sh_video
->format
>= 0x20202020)
485 sprintf (meta
, "%.4s", (char *) &sh_video
->format
);
490 sprintf (meta
, "0x%08X", sh_video
->format
);
495 case META_VIDEO_BITRATE
:
498 sprintf (meta
, "%d kbps", (int) (sh_video
->i_bps
* 8 / 1024));
502 case META_VIDEO_RESOLUTION
:
505 sprintf (meta
, "%d x %d", sh_video
->disp_w
, sh_video
->disp_h
);
509 case META_AUDIO_CODEC
:
511 if (sh_audio
->codec
&& sh_audio
->codec
->name
)
512 meta
= strdup (sh_audio
->codec
->name
);
516 case META_AUDIO_BITRATE
:
519 sprintf (meta
, "%d kbps", (int) (sh_audio
->i_bps
* 8/1000));
523 case META_AUDIO_SAMPLES
:
526 sprintf (meta
, "%d Hz, %d ch.", sh_audio
->samplerate
, sh_audio
->channels
);
530 /* check for valid demuxer */
531 case META_INFO_TITLE
:
532 return get_demuxer_info(mpctx
, "Title");
534 case META_INFO_ARTIST
:
535 return get_demuxer_info(mpctx
, "Artist");
537 case META_INFO_ALBUM
:
538 return get_demuxer_info(mpctx
, "Album");
541 return get_demuxer_info(mpctx
, "Year");
543 case META_INFO_COMMENT
:
544 return get_demuxer_info(mpctx
, "Comment");
546 case META_INFO_TRACK
:
547 return get_demuxer_info(mpctx
, "Track");
549 case META_INFO_GENRE
:
550 return get_demuxer_info(mpctx
, "Genre");
559 /// step size of mixer changes
563 static void mp_dvdnav_context_free(MPContext
*ctx
){
564 if (ctx
->nav_smpi
) free_mp_image(ctx
->nav_smpi
);
565 ctx
->nav_smpi
= NULL
;
566 if (ctx
->nav_buffer
) free(ctx
->nav_buffer
);
567 ctx
->nav_buffer
= NULL
;
568 ctx
->nav_start
= NULL
;
569 ctx
->nav_in_size
= 0;
573 void uninit_player(struct MPContext
*mpctx
, unsigned int mask
){
574 mask
=mpctx
->initialized_flags
&mask
;
576 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n*** uninit(0x%X)\n",mask
);
578 if(mask
&INITIALIZED_ACODEC
){
579 mpctx
->initialized_flags
&=~INITIALIZED_ACODEC
;
580 current_module
="uninit_acodec";
581 if(mpctx
->sh_audio
) uninit_audio(mpctx
->sh_audio
);
582 mpctx
->sh_audio
=NULL
;
583 mpctx
->mixer
.afilter
= NULL
;
586 if(mask
&INITIALIZED_VCODEC
){
587 mpctx
->initialized_flags
&=~INITIALIZED_VCODEC
;
588 current_module
="uninit_vcodec";
589 if(mpctx
->sh_video
) uninit_video(mpctx
->sh_video
);
590 mpctx
->sh_video
=NULL
;
596 if(mask
&INITIALIZED_DEMUXER
){
597 mpctx
->initialized_flags
&=~INITIALIZED_DEMUXER
;
598 current_module
="free_demuxer";
599 if (mpctx
->num_sources
) {
600 mpctx
->demuxer
= mpctx
->sources
[0].demuxer
;
601 for (int i
= 1; i
< mpctx
->num_sources
; i
++) {
602 free_stream(mpctx
->sources
[i
].stream
);
603 free_demuxer(mpctx
->sources
[i
].demuxer
);
606 talloc_free(mpctx
->sources
);
607 mpctx
->sources
= NULL
;
608 mpctx
->num_sources
= 0;
609 talloc_free(mpctx
->timeline
);
610 mpctx
->timeline
= NULL
;
611 mpctx
->num_timeline_parts
= 0;
612 talloc_free(mpctx
->chapters
);
613 mpctx
->chapters
= NULL
;
614 mpctx
->num_chapters
= 0;
615 mpctx
->video_offset
= 0;
617 mpctx
->stream
=mpctx
->demuxer
->stream
;
618 free_demuxer(mpctx
->demuxer
);
623 // kill the cache process:
624 if(mask
&INITIALIZED_STREAM
){
625 mpctx
->initialized_flags
&=~INITIALIZED_STREAM
;
626 current_module
="uninit_stream";
627 if(mpctx
->stream
) free_stream(mpctx
->stream
);
631 if(mask
&INITIALIZED_VO
){
632 mpctx
->initialized_flags
&=~INITIALIZED_VO
;
633 current_module
="uninit_vo";
634 vo_destroy(mpctx
->video_out
);
635 mpctx
->video_out
=NULL
;
637 mp_dvdnav_context_free(mpctx
);
641 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
642 if(mask
&INITIALIZED_GETCH2
){
643 mpctx
->initialized_flags
&=~INITIALIZED_GETCH2
;
644 current_module
="uninit_getch2";
645 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n[[[uninit getch2]]]\n");
650 if(mask
&INITIALIZED_VOBSUB
){
651 mpctx
->initialized_flags
&=~INITIALIZED_VOBSUB
;
652 current_module
="uninit_vobsub";
653 if(vo_vobsub
) vobsub_close(vo_vobsub
);
657 if (mask
&INITIALIZED_SPUDEC
){
658 mpctx
->initialized_flags
&=~INITIALIZED_SPUDEC
;
659 current_module
="uninit_spudec";
660 spudec_free(vo_spudec
);
664 if(mask
&INITIALIZED_AO
){
665 mpctx
->initialized_flags
&=~INITIALIZED_AO
;
666 current_module
="uninit_ao";
667 if (mpctx
->edl_muted
) mixer_mute(&mpctx
->mixer
);
668 mpctx
->audio_out
->uninit(mpctx
->stop_play
!= AT_END_OF_FILE
);
669 mpctx
->audio_out
=NULL
;
675 void exit_player_with_rc(struct MPContext
*mpctx
, exit_reason_t how
, int rc
)
677 uninit_player(mpctx
, INITIALIZED_ALL
);
678 #if defined(__MINGW32__) || defined(__CYGWIN__)
682 vo_uninit(mpctx
->x11_state
); // Close the X11 connection (if any is open).
685 current_module
="uninit_input";
686 mp_input_uninit(mpctx
->input
);
692 #ifdef CONFIG_FREETYPE
693 current_module
="uninit_font";
694 if (mpctx
->osd
&& mpctx
->osd
->sub_font
!= vo_font
)
695 free_font_desc(mpctx
->osd
->sub_font
);
696 free_font_desc(vo_font
);
700 osd_free(mpctx
->osd
);
703 ass_library_done(ass_library
);
706 current_module
="exit_player";
708 // free mplayer config
710 m_config_free(mpctx
->mconfig
);
713 play_tree_free(mpctx
->playtree
, 1);
715 talloc_free(mpctx
->key_fifo
);
717 if(edl_records
!= NULL
) free(edl_records
); // free mem allocated for EDL
720 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"\nExiting... (%s)\n","Quit");
721 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=QUIT\n");
724 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"\nExiting... (%s)\n","End of file");
725 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=EOF\n");
728 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"\nExiting... (%s)\n","Fatal error");
729 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=ERROR\n");
732 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=NONE\n");
734 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"max framesize was %d bytes\n",max_framesize
);
739 static void exit_player(struct MPContext
*mpctx
, exit_reason_t how
)
741 exit_player_with_rc(mpctx
, how
, 1);
745 static void child_sighandler(int x
){
747 while((pid
=waitpid(-1,NULL
,WNOHANG
)) > 0);
751 #ifdef CONFIG_CRASH_DEBUG
752 static char *prog_path
;
753 static int crash_debug
= 0;
756 static void exit_sighandler(int x
){
757 static int sig_count
=0;
758 #ifdef CONFIG_CRASH_DEBUG
759 if (!crash_debug
|| x
!= SIGTRAP
)
764 /* We're crashing bad and can't uninit cleanly :(
765 * by popular request, we make one last (dirty)
766 * effort to restore the user's
771 if(sig_count
==6) exit(1);
775 kill(getpid(),SIGKILL
);
778 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"\n" "\nMPlayer interrupted by signal %d in module: %s\n",x
,
779 current_module
?current_module
:"unknown"
781 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SIGNAL=%d\n", x
);
788 async_quit_request
= 1;
789 return; // killed from keyboard (^C) or killed [-9]
791 #if CONFIG_RUNTIME_CPUDETECT
792 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,Exit_SIGILL_RTCpuSel
);
794 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,Exit_SIGILL
);
798 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,Exit_SIGSEGV_SIGFPE
);
800 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,Exit_SIGCRASH
);
801 #ifdef CONFIG_CRASH_DEBUG
804 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Forking...\n");
806 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Forked...\n");
807 if (gdb_pid
== 0) { // We are the child
809 snprintf(spid
, sizeof(spid
), "%i", getppid());
810 getch2_disable(); // allow terminal to work properly with gdb
811 if (execlp("gdb", "gdb", prog_path
, spid
, "-ex", "bt", NULL
) == -1)
812 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Couldn't start gdb\n");
813 } else if (gdb_pid
< 0)
814 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Couldn't fork\n");
816 waitpid(gdb_pid
, NULL
, 0);
818 if (x
== SIGTRAP
) return;
826 #include "cfg-mplayer.h"
828 static int cfg_include(m_option_t
*conf
, char *filename
)
830 return m_config_parse_config_file(conf
->priv
, filename
);
833 static void parse_cfgfiles(struct MPContext
*mpctx
, m_config_t
* conf
)
837 if (!disable_system_conf
&&
838 m_config_parse_config_file(conf
, MPLAYER_CONFDIR
"/mplayer.conf") < 0)
839 exit_player(mpctx
, EXIT_NONE
);
840 if ((conffile
= get_path("")) == NULL
) {
841 mp_tmsg(MSGT_CPLAYER
,MSGL_WARN
,"Cannot find HOME directory.\n");
846 mkdir(conffile
, 0777);
849 if ((conffile
= get_path("config")) == NULL
) {
850 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"get_path(\"config\") problem\n");
852 if ((conffile_fd
= open(conffile
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666)) != -1) {
853 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Creating config file: %s\n", conffile
);
854 write(conffile_fd
, default_config
, strlen(default_config
));
857 if (!disable_user_conf
&&
858 m_config_parse_config_file(conf
, conffile
) < 0)
859 exit_player(mpctx
, EXIT_NONE
);
865 #define PROFILE_CFG_PROTOCOL "protocol."
867 static void load_per_protocol_config (m_config_t
* conf
, const char *const file
)
870 char protocol
[strlen (PROFILE_CFG_PROTOCOL
) + strlen (file
) + 1];
873 /* does filename actually uses a protocol ? */
874 str
= strstr (file
, "://");
878 sprintf (protocol
, "%s%s", PROFILE_CFG_PROTOCOL
, file
);
879 protocol
[strlen (PROFILE_CFG_PROTOCOL
)+strlen(file
)-strlen(str
)] = '\0';
880 p
= m_config_get_profile (conf
, protocol
);
883 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Loading protocol-related profile '%s'\n", protocol
);
884 m_config_set_profile(conf
,p
);
888 #define PROFILE_CFG_EXTENSION "extension."
890 static void load_per_extension_config (m_config_t
* conf
, const char *const file
)
893 char extension
[strlen (PROFILE_CFG_EXTENSION
) + 8];
896 /* does filename actually have an extension ? */
897 str
= strrchr (file
, '.');
901 sprintf (extension
, PROFILE_CFG_EXTENSION
);
902 strncat (extension
, ++str
, 7);
903 p
= m_config_get_profile (conf
, extension
);
906 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Loading extension-related profile '%s'\n", extension
);
907 m_config_set_profile(conf
,p
);
911 #define PROFILE_CFG_VO "vo."
912 #define PROFILE_CFG_AO "ao."
914 static void load_per_output_config (m_config_t
* conf
, char *cfg
, char *out
)
916 char profile
[strlen (cfg
) + strlen (out
) + 1];
919 sprintf (profile
, "%s%s", cfg
, out
);
920 p
= m_config_get_profile (conf
, profile
);
923 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Loading extension-related profile '%s'\n", profile
);
924 m_config_set_profile(conf
,p
);
928 static void load_per_file_config (m_config_t
* conf
, const char *const file
)
931 char cfg
[strlen(file
)+10];
935 sprintf (cfg
, "%s.conf", file
);
937 if (use_filedir_conf
&& !stat (cfg
, &st
))
939 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Loading config '%s'\n", cfg
);
940 m_config_parse_config_file (conf
, cfg
);
944 if ((name
= strrchr (cfg
, '/')) == NULL
)
949 if ((confpath
= get_path (name
)) != NULL
)
951 if (!stat (confpath
, &st
))
953 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Loading config '%s'\n", confpath
);
954 m_config_parse_config_file (conf
, confpath
);
961 /* When libmpdemux performs a blocking operation (network connection or
962 * cache filling) if the operation fails we use this function to check
963 * if it was interrupted by the user.
964 * The function returns a new value for eof. */
965 static int libmpdemux_was_interrupted(struct MPContext
*mpctx
, int stop_play
)
968 if((cmd
= mp_input_get_cmd(mpctx
->input
, 0,0,0)) != NULL
) {
971 exit_player_with_rc(mpctx
, EXIT_QUIT
, (cmd
->nargs
> 0)? cmd
->args
[0].v
.i
: 0);
972 case MP_CMD_PLAY_TREE_STEP
: {
973 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_NEXT_ENTRY
: PT_PREV_ENTRY
;
974 mpctx
->play_tree_step
= (cmd
->args
[0].v
.i
== 0) ? 1 : cmd
->args
[0].v
.i
;
976 case MP_CMD_PLAY_TREE_UP_STEP
: {
977 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_UP_NEXT
: PT_UP_PREV
;
979 case MP_CMD_PLAY_ALT_SRC_STEP
: {
980 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_NEXT_SRC
: PT_PREV_SRC
;
988 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
990 static int playtree_add_playlist(struct MPContext
*mpctx
, play_tree_t
* entry
)
992 play_tree_add_bpf(entry
,mpctx
->filename
);
996 entry
= mpctx
->playtree_iter
->tree
;
997 if(play_tree_iter_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
998 return PT_NEXT_ENTRY
;
1000 if(mpctx
->playtree_iter
->tree
== entry
) { // Loop with a single file
1001 if(play_tree_iter_up_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
1002 return PT_NEXT_ENTRY
;
1005 play_tree_remove(entry
,1,1);
1008 play_tree_insert_entry(mpctx
->playtree_iter
->tree
,entry
);
1009 play_tree_set_params_from(entry
,mpctx
->playtree_iter
->tree
);
1010 entry
= mpctx
->playtree_iter
->tree
;
1011 if(play_tree_iter_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
1012 return PT_NEXT_ENTRY
;
1014 play_tree_remove(entry
,1,1);
1019 void add_subtitles(struct MPContext
*mpctx
, char *filename
, float fps
, int noerr
)
1023 ass_track_t
*asst
= 0;
1026 if (filename
== NULL
|| mpctx
->set_of_sub_size
>= MAX_SUBTITLE_FILES
) {
1030 subd
= sub_read_file(filename
, fps
);
1034 asst
= ass_read_file(ass_library
, filename
, sub_cp
);
1036 asst
= ass_read_file(ass_library
, filename
, 0);
1038 if (ass_enabled
&& subd
&& !asst
)
1039 asst
= ass_read_subdata(ass_library
, subd
, fps
);
1045 mp_tmsg(MSGT_CPLAYER
, noerr
? MSGL_WARN
: MSGL_ERR
, "Cannot load subtitles: %s\n",
1046 filename_recode(filename
));
1047 #if defined(_WIN32) && !defined(HAVE_NEW_GUI)
1049 static char message
[MAX_PATH
+ 1];
1050 char *s
= strrchr(filename
, '\\');
1051 if (!s
) s
= strrchr(filename
, '/');
1052 if (s
) s
++; else s
= filename
;
1054 snprintf(message
, MAX_PATH
, "MPlayer: %s", filename_recode(s
));
1055 message
[MAX_PATH
] = 0;
1056 SetConsoleTitle(message
);
1061 if (!asst
&& !subd
) return;
1062 mpctx
->set_of_ass_tracks
[mpctx
->set_of_sub_size
] = asst
;
1066 mpctx
->set_of_subtitles
[mpctx
->set_of_sub_size
] = subd
;
1067 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_FILE_SUB_ID=%d\n", mpctx
->set_of_sub_size
);
1068 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_FILE_SUB_FILENAME=%s\n",
1069 filename_recode(filename
));
1070 ++mpctx
->set_of_sub_size
;
1071 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "SUB: Added subtitle file (%d): %s\n", mpctx
->set_of_sub_size
,
1072 filename_recode(filename
));
1075 void init_vo_spudec(struct MPContext
*mpctx
)
1078 spudec_free(vo_spudec
);
1079 mpctx
->initialized_flags
&= ~INITIALIZED_SPUDEC
;
1082 unsigned int palette
[16], width
, height
;
1083 current_module
="spudec_init_vobsub";
1084 if (vobsub_parse_ifo(NULL
,spudec_ifo
, palette
, &width
, &height
, 1, -1, NULL
) >= 0)
1085 vo_spudec
=spudec_new_scaled(palette
, width
, height
, NULL
, 0);
1088 #ifdef CONFIG_DVDREAD
1089 if (vo_spudec
==NULL
&& mpctx
->stream
->type
==STREAMTYPE_DVD
) {
1090 current_module
="spudec_init_dvdread";
1091 vo_spudec
=spudec_new_scaled(((dvd_priv_t
*)(mpctx
->stream
->priv
))->cur_pgc
->palette
,
1092 mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
,
1097 #ifdef CONFIG_DVDNAV
1098 if (vo_spudec
==NULL
&& mpctx
->stream
->type
==STREAMTYPE_DVDNAV
) {
1099 unsigned int *palette
= mp_dvdnav_get_spu_clut(mpctx
->stream
);
1100 current_module
="spudec_init_dvdnav";
1101 vo_spudec
=spudec_new_scaled(palette
, mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
, NULL
, 0);
1105 if (vo_spudec
==NULL
) {
1106 sh_sub_t
*sh
= (sh_sub_t
*)mpctx
->d_sub
->sh
;
1107 current_module
="spudec_init_normal";
1108 vo_spudec
=spudec_new_scaled(NULL
, mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
, sh
->extradata
, sh
->extradata_len
);
1109 spudec_set_font_factor(vo_spudec
,font_factor
);
1112 if (vo_spudec
!=NULL
) {
1113 mpctx
->initialized_flags
|=INITIALIZED_SPUDEC
;
1114 mp_property_do("sub_forced_only", M_PROPERTY_SET
, &forced_subs_only
, mpctx
);
1119 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1120 * make it all work is to use the builtin SDL-bootstrap code, which
1121 * will be done automatically by replacing our main() if we include SDL.h.
1123 #if defined(__APPLE__) && defined(CONFIG_SDL)
1128 * \brief append a formatted string
1129 * \param buf buffer to print into
1130 * \param pos position of terminating 0 in buf
1131 * \param len maximum number of characters in buf, not including terminating 0
1132 * \param format printf format string
1134 static void saddf(char *buf
, unsigned *pos
, int len
, const char *format
, ...)
1137 va_start(va
, format
);
1138 *pos
+= vsnprintf(&buf
[*pos
], len
- *pos
, format
, va
);
1147 * \brief append time in the hh:mm:ss.f format
1148 * \param buf buffer to print into
1149 * \param pos position of terminating 0 in buf
1150 * \param len maximum number of characters in buf, not including terminating 0
1151 * \param time time value to convert/append
1153 static void sadd_hhmmssf(char *buf
, unsigned *pos
, int len
, float time
) {
1154 long tenths
= 10 * time
;
1155 int f1
= tenths
% 10;
1156 int ss
= (tenths
/ 10) % 60;
1157 int mm
= (tenths
/ 600) % 60;
1158 int hh
= tenths
/ 36000;
1160 saddf(buf
, pos
, len
, "unknown");
1164 saddf(buf
, pos
, len
, "%2d:", hh
);
1165 if (hh
> 0 || mm
> 0)
1166 saddf(buf
, pos
, len
, "%02d:", mm
);
1167 saddf(buf
, pos
, len
, "%02d.%1d", ss
, f1
);
1170 static void print_status(struct MPContext
*mpctx
, double a_pos
, bool at_frame
)
1172 struct MPOpts
*opts
= &mpctx
->opts
;
1173 sh_video_t
* const sh_video
= mpctx
->sh_video
;
1175 if (mpctx
->sh_audio
&& a_pos
== MP_NOPTS_VALUE
)
1176 a_pos
= playing_audio_pts(mpctx
);
1177 if (mpctx
->sh_audio
&& sh_video
&& at_frame
) {
1178 mpctx
->last_av_difference
= a_pos
- sh_video
->pts
- audio_delay
;
1179 if (mpctx
->last_av_difference
> 0.5 && drop_frame_cnt
> 50
1180 && !mpctx
->drop_message_shown
) {
1181 mp_tmsg(MSGT_AVSYNC
,MSGL_WARN
,SystemTooSlow
);
1182 mpctx
->drop_message_shown
= true;
1193 if (screen_width
> 0)
1194 width
= screen_width
;
1197 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1198 /* Windows command line is broken (MinGW's rxvt works, but we
1199 * should not depend on that). */
1202 line
= malloc(width
+ 1); // one additional char for the terminating null
1205 if (mpctx
->sh_audio
) {
1206 saddf(line
, &pos
, width
, "A:%6.1f ", a_pos
);
1208 float len
= demuxer_get_time_length(mpctx
->demuxer
);
1209 saddf(line
, &pos
, width
, "(");
1210 sadd_hhmmssf(line
, &pos
, width
, a_pos
);
1211 saddf(line
, &pos
, width
, ") of %.1f (", len
);
1212 sadd_hhmmssf(line
, &pos
, width
, len
);
1213 saddf(line
, &pos
, width
, ") ");
1219 saddf(line
, &pos
, width
, "V:%6.1f ", sh_video
->pts
);
1222 if (mpctx
->sh_audio
&& sh_video
)
1223 saddf(line
, &pos
, width
, "A-V:%7.3f ct:%7.3f ",
1224 mpctx
->last_av_difference
, mpctx
->total_avsync_change
);
1228 saddf(line
, &pos
, width
, "%3d/%3d ",
1229 (int)sh_video
->num_frames
,
1230 (int)sh_video
->num_frames_decoded
);
1234 if (sh_video
->timer
> 0.5)
1235 saddf(line
, &pos
, width
, "%2d%% %2d%% %4.1f%% ",
1236 (int)(100.0*video_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
),
1237 (int)(100.0*vout_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
),
1238 (100.0*audio_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
));
1240 saddf(line
, &pos
, width
, "??%% ??%% ??,?%% ");
1241 } else if (mpctx
->sh_audio
) {
1242 if (mpctx
->delay
> 0.5)
1243 saddf(line
, &pos
, width
, "%4.1f%% ",
1244 100.0*audio_time_usage
/(double)mpctx
->delay
);
1246 saddf(line
, &pos
, width
, "??,?%% ");
1251 saddf(line
, &pos
, width
, "%d %d ", drop_frame_cnt
, output_quality
);
1253 #ifdef CONFIG_STREAM_CACHE
1255 if (stream_cache_size
> 0)
1256 saddf(line
, &pos
, width
, "%d%% ", cache_fill_status
);
1260 if (opts
->playback_speed
!= 1)
1261 saddf(line
, &pos
, width
, "%4.2fx ", opts
->playback_speed
);
1264 if (erase_to_end_of_line
) {
1266 mp_msg(MSGT_STATUSLINE
, MSGL_STATUS
, "%s%s\r", line
, erase_to_end_of_line
);
1268 memset(&line
[pos
], ' ', width
- pos
);
1270 mp_msg(MSGT_STATUSLINE
, MSGL_STATUS
, "%s\r", line
);
1276 * \brief build a chain of audio filters that converts the input format
1277 * to the ao's format, taking into account the current playback_speed.
1278 * \param sh_audio describes the requested input format of the chain.
1279 * \param ao_data describes the requested output format of the chain.
1281 int build_afilter_chain(struct MPContext
*mpctx
, sh_audio_t
*sh_audio
, ao_data_t
*ao_data
)
1283 struct MPOpts
*opts
= &mpctx
->opts
;
1288 mpctx
->mixer
.afilter
= NULL
;
1291 if(af_control_any_rev(sh_audio
->afilter
,
1292 AF_CONTROL_PLAYBACK_SPEED
| AF_CONTROL_SET
,
1293 &opts
->playback_speed
)) {
1294 new_srate
= sh_audio
->samplerate
;
1296 new_srate
= sh_audio
->samplerate
* opts
->playback_speed
;
1297 if (new_srate
!= ao_data
->samplerate
) {
1298 // limits are taken from libaf/af_resample.c
1299 if (new_srate
< 8000)
1301 if (new_srate
> 192000)
1303 opts
->playback_speed
= (float)new_srate
/ (float)sh_audio
->samplerate
;
1306 result
= init_audio_filters(sh_audio
, new_srate
,
1307 &ao_data
->samplerate
, &ao_data
->channels
, &ao_data
->format
);
1308 mpctx
->mixer
.afilter
= sh_audio
->afilter
;
1313 typedef struct mp_osd_msg mp_osd_msg_t
;
1315 /// Previous message on the stack.
1319 int id
,level
,started
;
1320 /// Display duration in ms.
1324 /// OSD message stack.
1325 static mp_osd_msg_t
* osd_msg_stack
= NULL
;
1328 * \brief Add a message on the OSD message stack
1330 * If a message with the same id is already present in the stack
1331 * it is pulled on top of the stack, otherwise a new message is created.
1335 void set_osd_msg(int id
, int level
, int time
, const char* fmt
, ...) {
1336 mp_osd_msg_t
*msg
,*last
=NULL
;
1340 // look if the id is already in the stack
1341 for(msg
= osd_msg_stack
; msg
&& msg
->id
!= id
;
1342 last
= msg
, msg
= msg
->prev
);
1343 // not found: alloc it
1345 msg
= calloc(1,sizeof(mp_osd_msg_t
));
1346 msg
->prev
= osd_msg_stack
;
1347 osd_msg_stack
= msg
;
1348 } else if(last
) { // found, but it's not on top of the stack
1349 last
->prev
= msg
->prev
;
1350 msg
->prev
= osd_msg_stack
;
1351 osd_msg_stack
= msg
;
1355 r
= vsnprintf(msg
->msg
, 128, fmt
, va
);
1357 if(r
>= 128) msg
->msg
[127] = 0;
1366 * \brief Remove a message from the OSD stack
1368 * This function can be used to get rid of a message right away.
1372 void rm_osd_msg(int id
) {
1373 mp_osd_msg_t
*msg
,*last
=NULL
;
1375 // Search for the msg
1376 for(msg
= osd_msg_stack
; msg
&& msg
->id
!= id
;
1377 last
= msg
, msg
= msg
->prev
);
1380 // Detach it from the stack and free it
1382 last
->prev
= msg
->prev
;
1384 osd_msg_stack
= msg
->prev
;
1389 * \brief Remove all messages from the OSD stack
1393 static void clear_osd_msgs(void) {
1394 mp_osd_msg_t
* msg
= osd_msg_stack
, *prev
= NULL
;
1400 osd_msg_stack
= NULL
;
1404 * \brief Get the current message from the OSD stack.
1406 * This function decrements the message timer and destroys the old ones.
1407 * The message that should be displayed is returned (if any).
1411 static mp_osd_msg_t
* get_osd_msg(struct MPContext
*mpctx
)
1413 struct MPOpts
*opts
= &mpctx
->opts
;
1414 mp_osd_msg_t
*msg
,*prev
,*last
= NULL
;
1415 static unsigned last_update
= 0;
1416 unsigned now
= GetTimerMS();
1418 char hidden_dec_done
= 0;
1420 if (mpctx
->osd_visible
) {
1421 // 36000000 means max timed visibility is 1 hour into the future, if
1422 // the difference is greater assume it's wrapped around from below 0
1423 if (mpctx
->osd_visible
- now
> 36000000) {
1424 mpctx
->osd_visible
= 0;
1425 vo_osd_progbar_type
= -1; // disable
1426 vo_osd_changed(OSDTYPE_PROGBAR
);
1427 mpctx
->osd_function
= mpctx
->paused
? OSD_PAUSE
: OSD_PLAY
;
1430 if (mpctx
->osd_show_percentage_until
- now
> 36000000)
1431 mpctx
->osd_show_percentage_until
= 0;
1433 if(!last_update
) last_update
= now
;
1434 diff
= now
>= last_update
? now
- last_update
: 0;
1438 // Look for the first message in the stack with high enough level.
1439 for(msg
= osd_msg_stack
; msg
; last
= msg
, msg
= prev
) {
1441 if (msg
->level
> opts
->osd_level
&& hidden_dec_done
)
1443 // The message has a high enough level or it is the first hidden one
1444 // in both cases we decrement the timer or kill it.
1445 if(!msg
->started
|| msg
->time
> diff
) {
1446 if(msg
->started
) msg
->time
-= diff
;
1447 else msg
->started
= 1;
1449 if (msg
->level
<= opts
->osd_level
)
1451 hidden_dec_done
= 1;
1460 osd_msg_stack
= prev
;
1469 * \brief Display the OSD bar.
1471 * Display the OSD bar or fall back on a simple message.
1475 void set_osd_bar(struct MPContext
*mpctx
, int type
,const char* name
,double min
,double max
,double val
) {
1476 struct MPOpts
*opts
= &mpctx
->opts
;
1477 if (opts
->osd_level
< 1)
1480 if(mpctx
->sh_video
) {
1481 mpctx
->osd_visible
= (GetTimerMS() + 1000) | 1;
1482 vo_osd_progbar_type
= type
;
1483 vo_osd_progbar_value
= 256*(val
-min
)/(max
-min
);
1484 vo_osd_changed(OSDTYPE_PROGBAR
);
1488 set_osd_msg(OSD_MSG_BAR
, 1, opts
->osd_duration
, "%s: %d %%",
1489 name
, ROUND(100*(val
-min
)/(max
-min
)));
1494 * \brief Update the OSD message line.
1496 * This function displays the current message on the vo OSD or on the term.
1497 * If the stack is empty and the OSD level is high enough the timer
1498 * is displayed (only on the vo OSD).
1502 static void update_osd_msg(struct MPContext
*mpctx
)
1504 struct MPOpts
*opts
= &mpctx
->opts
;
1506 struct osd_state
*osd
= mpctx
->osd
;
1507 char osd_text_timer
[128];
1509 if (mpctx
->add_osd_seek_info
) {
1511 if (mpctx
->timeline
&& mpctx
->sh_video
)
1512 percentage
= mpctx
->sh_video
->pts
* 100 /
1513 mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
1515 percentage
= demuxer_get_percent_pos(mpctx
->demuxer
);
1516 set_osd_bar(mpctx
, 0, "Position", 0, 100, percentage
);
1517 if (mpctx
->sh_video
)
1518 mpctx
->osd_show_percentage_until
= (GetTimerMS() + 1000) | 1;
1519 mpctx
->add_osd_seek_info
= false;
1522 // Look if we have a msg
1523 if((msg
= get_osd_msg(mpctx
))) {
1524 if (strcmp(osd
->osd_text
, msg
->msg
)) {
1525 strncpy(osd
->osd_text
, msg
->msg
, 127);
1526 if(mpctx
->sh_video
) vo_osd_changed(OSDTYPE_OSD
); else
1527 if(term_osd
) mp_msg(MSGT_CPLAYER
,MSGL_STATUS
,"%s%s\n",term_osd_esc
,msg
->msg
);
1532 if(mpctx
->sh_video
) {
1533 // fallback on the timer
1534 if (opts
->osd_level
>= 2) {
1536 if (mpctx
->timeline
)
1537 len
= mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
1539 len
= demuxer_get_time_length(mpctx
->demuxer
);
1540 int percentage
= -1;
1541 char percentage_text
[10];
1542 int pts
= demuxer_get_current_time(mpctx
->demuxer
);
1544 if (mpctx
->osd_show_percentage_until
)
1545 if (mpctx
->timeline
)
1546 percentage
= mpctx
->sh_video
->pts
* 100 /
1547 mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
1549 percentage
= demuxer_get_percent_pos(mpctx
->demuxer
);
1551 if (percentage
>= 0)
1552 snprintf(percentage_text
, 9, " (%d%%)", percentage
);
1554 percentage_text
[0] = 0;
1556 if (opts
->osd_level
== 3)
1557 snprintf(osd_text_timer
, 63,
1558 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1559 mpctx
->osd_function
,pts
/3600,(pts
/60)%60,pts
%60,
1560 len
/3600,(len
/60)%60,len
%60,percentage_text
);
1562 snprintf(osd_text_timer
, 63, "%c %02d:%02d:%02d%s",
1563 mpctx
->osd_function
,pts
/3600,(pts
/60)%60,
1564 pts
%60,percentage_text
);
1566 osd_text_timer
[0]=0;
1568 if (strcmp(osd
->osd_text
, osd_text_timer
)) {
1569 strncpy(osd
->osd_text
, osd_text_timer
, 63);
1570 vo_osd_changed(OSDTYPE_OSD
);
1575 // Clear the term osd line
1576 if (term_osd
&& osd
->osd_text
[0]) {
1577 osd
->osd_text
[0] = 0;
1578 printf("%s\n",term_osd_esc
);
1586 void reinit_audio_chain(struct MPContext
*mpctx
)
1588 struct MPOpts
*opts
= &mpctx
->opts
;
1589 if(mpctx
->sh_audio
){
1590 current_module
="init_audio_codec";
1591 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
1592 if(!init_best_audio_codec(mpctx
->sh_audio
,audio_codec_list
,audio_fm_list
)){
1593 mpctx
->sh_audio
=mpctx
->d_audio
->sh
=NULL
; // failed to init :(
1594 mpctx
->d_audio
->id
= -2;
1597 mpctx
->initialized_flags
|=INITIALIZED_ACODEC
;
1598 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
1601 //const ao_info_t *info=audio_out->info;
1602 current_module
="af_preinit";
1603 ao_data
.samplerate
=force_srate
;
1605 ao_data
.format
=audio_output_format
;
1607 // first init to detect best values
1608 if(!init_audio_filters(mpctx
->sh_audio
, // preliminary init
1610 mpctx
->sh_audio
->samplerate
,
1612 &ao_data
.samplerate
, &ao_data
.channels
, &ao_data
.format
)){
1613 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Error at audio filter chain pre-init!\n");
1614 exit_player(mpctx
, EXIT_ERROR
);
1617 current_module
="ao2_init";
1618 if(!(mpctx
->audio_out
=init_best_audio_out(opts
->audio_driver_list
,
1622 ao_data
.format
,0))){
1624 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Could not open/initialize audio device -> no sound.\n");
1625 uninit_player(mpctx
, INITIALIZED_ACODEC
); // close codec
1626 mpctx
->sh_audio
=mpctx
->d_audio
->sh
=NULL
; // -> nosound
1627 mpctx
->d_audio
->id
= -2;
1631 mpctx
->initialized_flags
|=INITIALIZED_AO
;
1632 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1633 mpctx
->audio_out
->info
->short_name
,
1634 ao_data
.samplerate
, ao_data
.channels
,
1635 af_fmt2str_short(ao_data
.format
),
1636 af_fmt2bits(ao_data
.format
)/8 );
1637 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AO: Description: %s\nAO: Author: %s\n",
1638 mpctx
->audio_out
->info
->name
, mpctx
->audio_out
->info
->author
);
1639 if(strlen(mpctx
->audio_out
->info
->comment
) > 0)
1640 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AO: Comment: %s\n", mpctx
->audio_out
->info
->comment
);
1641 // init audio filters:
1643 current_module
="af_init";
1644 if(!build_afilter_chain(mpctx
, mpctx
->sh_audio
, &ao_data
)) {
1645 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Couldn't find matching filter/ao format!\n");
1646 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1647 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1648 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1652 mpctx
->mixer
.audio_out
= mpctx
->audio_out
;
1653 mpctx
->mixer
.volstep
= volstep
;
1662 // Return pts value corresponding to the end point of audio written to the
1664 static double written_audio_pts(struct MPContext
*mpctx
)
1666 sh_audio_t
*sh_audio
= mpctx
->sh_audio
;
1667 demux_stream_t
*d_audio
= mpctx
->d_audio
;
1668 double buffered_output
;
1669 // first calculate the end pts of audio that has been output by decoder
1670 double a_pts
= sh_audio
->pts
;
1671 if (a_pts
!= MP_NOPTS_VALUE
)
1672 // Good, decoder supports new way of calculating audio pts.
1673 // sh_audio->pts is the timestamp of the latest input packet with
1674 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1675 // the amount of bytes the decoder has written after that timestamp.
1676 a_pts
+= sh_audio
->pts_bytes
/ (double) sh_audio
->o_bps
;
1678 // Decoder doesn't support new way of calculating pts (or we're
1679 // being called before it has decoded anything with known timestamp).
1680 // Use the old method of audio pts calculation: take the timestamp
1681 // of last packet with known pts the decoder has read data from,
1682 // and add amount of bytes read after the beginning of that packet
1683 // divided by input bps. This will be inaccurate if the input/output
1684 // ratio is not constant for every audio packet or if it is constant
1685 // but not accurately known in sh_audio->i_bps.
1687 a_pts
= d_audio
->pts
;
1688 // ds_tell_pts returns bytes read after last timestamp from
1689 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1690 // it has read but not decoded
1691 if (sh_audio
->i_bps
)
1692 a_pts
+= (ds_tell_pts(d_audio
) - sh_audio
->a_in_buffer_len
) /
1693 (double)sh_audio
->i_bps
;
1695 // Now a_pts hopefully holds the pts for end of audio from decoder.
1696 // Substract data in buffers between decoder and audio out.
1698 // Decoded but not filtered
1699 a_pts
-= sh_audio
->a_buffer_len
/ (double)sh_audio
->o_bps
;
1701 // Data buffered in audio filters, measured in bytes of "missing" output
1702 buffered_output
= af_calc_delay(sh_audio
->afilter
);
1704 // Data that was ready for ao but was buffered because ao didn't fully
1705 // accept everything to internal buffers yet
1706 buffered_output
+= sh_audio
->a_out_buffer_len
;
1708 // Filters divide audio length by playback_speed, so multiply by it
1709 // to get the length in original units without speedup or slowdown
1710 a_pts
-= buffered_output
* mpctx
->opts
.playback_speed
/ ao_data
.bps
;
1712 return a_pts
+ mpctx
->video_offset
;
1715 // Return pts value corresponding to currently playing audio.
1716 double playing_audio_pts(struct MPContext
*mpctx
)
1718 return written_audio_pts(mpctx
) - mpctx
->opts
.playback_speed
*
1719 mpctx
->audio_out
->get_delay();
1722 static int check_framedrop(struct MPContext
*mpctx
, double frame_time
) {
1723 struct MPOpts
*opts
= &mpctx
->opts
;
1724 // check for frame-drop:
1725 current_module
= "check_framedrop";
1726 if (mpctx
->sh_audio
&& !mpctx
->d_audio
->eof
) {
1727 static int dropped_frames
;
1728 float delay
= opts
->playback_speed
*mpctx
->audio_out
->get_delay();
1729 float d
= delay
-mpctx
->delay
;
1731 // we should avoid dropping too many frames in sequence unless we
1732 // are too late. and we allow 100ms A-V delay here:
1733 if (d
< -dropped_frames
*frame_time
-0.100 && !mpctx
->paused
1734 && !mpctx
->update_video_immediately
) {
1737 return frame_dropping
;
1749 static float timing_sleep(struct MPContext
*mpctx
, float time_frame
)
1753 // -------- RTC -----------
1754 current_module
="sleep_rtc";
1755 while (time_frame
> 0.000) {
1756 unsigned long rtc_ts
;
1757 if (read(rtc_fd
, &rtc_ts
, sizeof(rtc_ts
)) <= 0)
1758 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Linux RTC read error: %s\n", strerror(errno
));
1759 time_frame
-= get_relative_time(mpctx
);
1764 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1765 // unnecessarily high CPU usage
1766 float margin
= softsleep
? 0.011 : 0;
1767 current_module
= "sleep_timer";
1768 while (time_frame
> margin
) {
1769 usec_sleep(1000000 * (time_frame
- margin
));
1770 time_frame
-= get_relative_time(mpctx
);
1773 current_module
= "sleep_soft";
1775 mp_tmsg(MSGT_AVSYNC
, MSGL_WARN
, "Warning! Softsleep underflow!\n");
1776 while (time_frame
> 0)
1777 time_frame
-= get_relative_time(mpctx
); // burn the CPU
1783 #ifdef CONFIG_DVDNAV
1787 /// store decoded video image
1788 static mp_image_t
* mp_dvdnav_copy_mpi(mp_image_t
*to_mpi
,
1789 mp_image_t
*from_mpi
) {
1792 /// Do not store B-frames
1793 if (from_mpi
->pict_type
== FF_B_TYPE
)
1797 to_mpi
->w
== from_mpi
->w
&&
1798 to_mpi
->h
== from_mpi
->h
&&
1799 to_mpi
->imgfmt
== from_mpi
->imgfmt
)
1803 free_mp_image(to_mpi
);
1804 if (from_mpi
->w
== 0 || from_mpi
->h
== 0)
1806 mpi
= alloc_mpi(from_mpi
->w
,from_mpi
->h
,from_mpi
->imgfmt
);
1809 copy_mpi(mpi
,from_mpi
);
1813 static void mp_dvdnav_reset_stream (MPContext
*ctx
) {
1814 struct MPOpts
*opts
= &ctx
->opts
;
1815 if (ctx
->sh_video
) {
1817 ctx
->d_video
->pts
= 0.0f
;
1818 ctx
->sh_video
->pts
= 0.0f
;
1819 ctx
->sh_video
->i_pts
= 0.0f
;
1820 ctx
->sh_video
->last_pts
= 0.0f
;
1821 ctx
->sh_video
->num_buffered_pts
= 0;
1822 ctx
->sh_video
->num_frames
= 0;
1823 ctx
->sh_video
->num_frames_decoded
= 0;
1824 ctx
->sh_video
->timer
= 0.0f
;
1825 ctx
->sh_video
->stream_delay
= 0.0f
;
1826 ctx
->sh_video
->timer
= 0;
1827 ctx
->demuxer
->stream_pts
= MP_NOPTS_VALUE
;
1830 if (ctx
->sh_audio
) {
1831 /// free audio packets and reset
1832 ds_free_packs(ctx
->d_audio
);
1833 audio_delay
-= ctx
->sh_audio
->stream_delay
;
1834 ctx
->delay
=- audio_delay
;
1835 ctx
->audio_out
->reset();
1836 resync_audio_stream(ctx
->sh_audio
);
1839 if (ctx
->d_sub
) opts
->sub_id
= -2;
1843 /// clear all EOF related flags
1844 ctx
->d_video
->eof
= ctx
->d_audio
->eof
= ctx
->stream
->eof
= 0;
1847 /// Restore last decoded DVDNAV (still frame)
1848 static mp_image_t
*mp_dvdnav_restore_smpi(struct MPContext
*mpctx
,
1850 unsigned char **start
,
1851 mp_image_t
*decoded_frame
)
1853 if (mpctx
->stream
->type
!= STREAMTYPE_DVDNAV
)
1854 return decoded_frame
;
1856 /// a change occured in dvdnav stream
1857 if (mp_dvdnav_cell_has_changed(mpctx
->stream
,0)) {
1858 mp_dvdnav_read_wait(mpctx
->stream
, 1, 1);
1859 mp_dvdnav_context_free(mpctx
);
1860 mp_dvdnav_reset_stream(mpctx
);
1861 mp_dvdnav_read_wait(mpctx
->stream
, 0, 1);
1862 mp_dvdnav_cell_has_changed(mpctx
->stream
,1);
1868 /// Display still frame, if any
1869 if (mpctx
->nav_smpi
&& !mpctx
->nav_buffer
)
1870 decoded_frame
= mpctx
->nav_smpi
;
1872 /// increment video frame : continue playing after still frame
1873 len
= demuxer_get_time_length(mpctx
->demuxer
);
1874 if (mpctx
->sh_video
->pts
>= len
&&
1875 mpctx
->sh_video
->pts
> 0.0 && len
> 0.0) {
1876 mp_dvdnav_skip_still(mpctx
->stream
);
1877 mp_dvdnav_skip_wait(mpctx
->stream
);
1879 mpctx
->sh_video
->pts
+= 1 / mpctx
->sh_video
->fps
;
1881 if (mpctx
->nav_buffer
) {
1882 *start
= mpctx
->nav_start
;
1883 *in_size
= mpctx
->nav_in_size
;
1884 if (mpctx
->nav_start
)
1885 memcpy(*start
,mpctx
->nav_buffer
,mpctx
->nav_in_size
);
1889 return decoded_frame
;
1892 /// Save last decoded DVDNAV (still frame)
1893 static void mp_dvdnav_save_smpi(struct MPContext
*mpctx
, int in_size
,
1894 unsigned char *start
,
1895 mp_image_t
*decoded_frame
)
1897 if (mpctx
->stream
->type
!= STREAMTYPE_DVDNAV
)
1900 if (mpctx
->nav_buffer
)
1901 free(mpctx
->nav_buffer
);
1903 mpctx
->nav_buffer
= malloc(in_size
);
1904 mpctx
->nav_start
= start
;
1905 mpctx
->nav_in_size
= mpctx
->nav_buffer
? in_size
: -1;
1906 if (mpctx
->nav_buffer
)
1907 memcpy(mpctx
->nav_buffer
,start
,in_size
);
1909 if (decoded_frame
&& mpctx
->nav_smpi
!= decoded_frame
)
1910 mpctx
->nav_smpi
= mp_dvdnav_copy_mpi(mpctx
->nav_smpi
,decoded_frame
);
1912 #endif /* CONFIG_DVDNAV */
1914 /* Modify video timing to match the audio timeline. There are two main
1915 * reasons this is needed. First, video and audio can start from different
1916 * positions at beginning of file or after a seek (MPlayer starts both
1917 * immediately even if they have different pts). Second, the file can have
1918 * audio timestamps that are inconsistent with the duration of the audio
1919 * packets, for example two consecutive timestamp values differing by
1920 * one second but only a packet with enough samples for half a second
1921 * of playback between them.
1923 static void adjust_sync(struct MPContext
*mpctx
, double frame_time
)
1925 struct MPOpts
*opts
= &mpctx
->opts
;
1926 current_module
= "av_sync";
1928 if (!mpctx
->sh_audio
)
1931 double a_pts
= written_audio_pts(mpctx
) - mpctx
->delay
;
1932 double v_pts
= mpctx
->sh_video
->pts
;
1933 double av_delay
= a_pts
- v_pts
;
1934 // Try to sync vo_flip() so it will *finish* at given time
1935 av_delay
+= mpctx
->last_vo_flip_duration
;
1936 av_delay
-= audio_delay
; // This much pts difference is desired
1938 double change
= av_delay
* 0.1;
1939 double max_change
= default_max_pts_correction
>= 0 ?
1940 default_max_pts_correction
: frame_time
* 0.1;
1941 if (change
< -max_change
)
1942 change
= -max_change
;
1943 else if (change
> max_change
)
1944 change
= max_change
;
1945 mpctx
->delay
+= change
;
1946 mpctx
->total_avsync_change
+= change
;
1949 static int fill_audio_out_buffers(struct MPContext
*mpctx
)
1951 struct MPOpts
*opts
= &mpctx
->opts
;
1958 sh_audio_t
* const sh_audio
= mpctx
->sh_audio
;
1960 current_module
="play_audio";
1964 // all the current uses of ao_data.pts seem to be in aos that handle
1965 // sync completely wrong; there should be no need to use ao_data.pts
1967 ao_data
.pts
= ((mpctx
->sh_video
?mpctx
->sh_video
->timer
:0)+mpctx
->delay
)*90000.0;
1968 bytes_to_write
= mpctx
->audio_out
->get_space();
1969 if (mpctx
->sh_video
|| bytes_to_write
>= ao_data
.outburst
)
1972 // handle audio-only case:
1973 // this is where mplayer sleeps during audio-only playback
1974 // to avoid 100% CPU use
1975 sleep_time
= (ao_data
.outburst
- bytes_to_write
) * 1000 / ao_data
.bps
;
1976 if (sleep_time
< 10) sleep_time
= 10; // limit to 100 wakeups per second
1977 usec_sleep(sleep_time
* 1000);
1980 while (bytes_to_write
) {
1981 playsize
= bytes_to_write
;
1982 if (playsize
> MAX_OUTBURST
)
1983 playsize
= MAX_OUTBURST
;
1984 bytes_to_write
-= playsize
;
1986 // Fill buffer if needed:
1987 current_module
="decode_audio";
1989 if (decode_audio(sh_audio
, playsize
) < 0) // EOF or error
1990 if (mpctx
->d_audio
->eof
) {
1992 if (sh_audio
->a_out_buffer_len
== 0)
1996 tt
= t
*0.000001f
; audio_time_usage
+=tt
;
1997 if (playsize
> sh_audio
->a_out_buffer_len
) {
1998 playsize
= sh_audio
->a_out_buffer_len
;
2000 playflags
|= AOPLAY_FINAL_CHUNK
;
2006 current_module
="play_audio";
2008 // Is this pts value actually useful for the aos that access it?
2009 // They're obviously badly broken in the way they handle av sync;
2010 // would not having access to this make them more broken?
2011 ao_data
.pts
= ((mpctx
->sh_video
?mpctx
->sh_video
->timer
:0)+mpctx
->delay
)*90000.0;
2012 playsize
= mpctx
->audio_out
->play(sh_audio
->a_out_buffer
, playsize
, playflags
);
2015 sh_audio
->a_out_buffer_len
-= playsize
;
2016 memmove(sh_audio
->a_out_buffer
, &sh_audio
->a_out_buffer
[playsize
],
2017 sh_audio
->a_out_buffer_len
);
2018 mpctx
->delay
+= opts
->playback_speed
*playsize
/(double)ao_data
.bps
;
2020 else if (audio_eof
&& mpctx
->audio_out
->get_delay() < .04) {
2021 // Sanity check to avoid hanging in case current ao doesn't output
2022 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2023 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, "Audio output truncated at end.\n");
2024 sh_audio
->a_out_buffer_len
= 0;
2030 static int sleep_until_update(struct MPContext
*mpctx
, float *time_frame
,
2031 float *aq_sleep_time
)
2033 struct MPOpts
*opts
= &mpctx
->opts
;
2034 int frame_time_remaining
= 0;
2035 current_module
="calc_sleep_time";
2037 *time_frame
-= get_relative_time(mpctx
); // reset timer
2039 if (mpctx
->sh_audio
&& !mpctx
->d_audio
->eof
) {
2040 float delay
= mpctx
->audio_out
->get_delay();
2041 mp_dbg(MSGT_AVSYNC
, MSGL_DBG2
, "delay=%f\n", delay
);
2045 * Adjust this raw delay value by calculating the expected
2046 * delay for this frame and generating a new value which is
2047 * weighted between the two. The higher autosync is, the
2048 * closer to the delay value gets to that which "-nosound"
2049 * would have used, and the longer it will take for A/V
2050 * sync to settle at the right value (but it eventually will.)
2051 * This settling time is very short for values below 100.
2053 float predicted
= mpctx
->delay
/ opts
->playback_speed
+ *time_frame
;
2054 float difference
= delay
- predicted
;
2055 delay
= predicted
+ difference
/ (float)autosync
;
2058 *time_frame
= delay
- mpctx
->delay
/ opts
->playback_speed
;
2060 // delay = amount of audio buffered in soundcard/driver
2061 if (delay
> 0.25) delay
=0.25; else
2062 if (delay
< 0.10) delay
=0.10;
2063 if (*time_frame
> delay
*0.6) {
2064 // sleep time too big - may cause audio drops (buffer underrun)
2065 frame_time_remaining
= 1;
2066 *time_frame
= delay
*0.5;
2069 // If we're lagging more than 200 ms behind the right playback rate,
2070 // don't try to "catch up".
2071 // If benchmark is set always output frames as fast as possible
2072 // without sleeping.
2073 if (*time_frame
< -0.2 || benchmark
)
2077 *aq_sleep_time
+= *time_frame
;
2080 //============================== SLEEP: ===================================
2082 // flag 256 means: libvo driver does its timing (dvb card)
2083 if (*time_frame
> 0.001 && !(mpctx
->sh_video
->output_flags
&256))
2084 *time_frame
= timing_sleep(mpctx
, *time_frame
);
2085 return frame_time_remaining
;
2088 int reinit_video_chain(struct MPContext
*mpctx
)
2090 struct MPOpts
*opts
= &mpctx
->opts
;
2091 sh_video_t
* const sh_video
= mpctx
->sh_video
;
2093 //================== Init VIDEO (codec & libvo) ==========================
2094 if (!opts
->fixed_vo
|| !(mpctx
->initialized_flags
& INITIALIZED_VO
)) {
2095 current_module
="preinit_libvo";
2097 //shouldn't we set dvideo->id=-2 when we fail?
2098 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2099 if(!(mpctx
->video_out
=init_best_video_out(opts
, mpctx
->x11_state
, mpctx
->key_fifo
, mpctx
->input
))){
2100 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"Error opening/initializing the selected video_out (-vo) device.\n");
2103 mpctx
->initialized_flags
|=INITIALIZED_VO
;
2106 if(stream_control(mpctx
->demuxer
->stream
, STREAM_CTRL_GET_ASPECT_RATIO
, &ar
) != STREAM_UNSUPPORTED
)
2107 mpctx
->sh_video
->stream_aspect
= ar
;
2108 current_module
="init_video_filters";
2110 char* vf_arg
[] = { "_oldargs_", (char*)mpctx
->video_out
, NULL
};
2111 sh_video
->vfilter
= vf_open_filter(opts
, NULL
,"vo",vf_arg
);
2115 char* vf_arg
[] = { "_oldargs_", menu_root
, NULL
};
2116 vf_menu
= vf_open_plugin(opts
,libmenu_vfs
,sh_video
->vfilter
,"menu",vf_arg
);
2118 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Can't open libmenu video filter with root menu %s.\n",menu_root
);
2123 sh_video
->vfilter
= vf_menu
;
2130 if (opts
->vf_settings
)
2131 for (i
= 0; opts
->vf_settings
[i
].name
; ++i
)
2132 if (strcmp(opts
->vf_settings
[i
].name
, "ass") == 0) {
2137 extern vf_info_t vf_info_ass
;
2138 const vf_info_t
* libass_vfs
[] = {&vf_info_ass
, NULL
};
2139 char* vf_arg
[] = {"auto", "1", NULL
};
2140 vf_instance_t
* vf_ass
= vf_open_plugin(opts
, libass_vfs
,sh_video
->vfilter
,"ass",vf_arg
);
2142 sh_video
->vfilter
= vf_ass
;
2144 mp_msg(MSGT_CPLAYER
,MSGL_ERR
, "ASS: cannot add video filter\n");
2149 sh_video
->vfilter
= append_filters(sh_video
->vfilter
, opts
->vf_settings
);
2153 sh_video
->vfilter
->control(sh_video
->vfilter
, VFCTRL_INIT_EOSD
, ass_library
);
2156 current_module
="init_video_codec";
2158 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
2159 init_best_video_codec(sh_video
,video_codec_list
,video_fm_list
);
2160 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
2162 if(!sh_video
->initialized
){
2163 if(!opts
->fixed_vo
) uninit_player(mpctx
, INITIALIZED_VO
);
2167 mpctx
->initialized_flags
|=INITIALIZED_VCODEC
;
2169 if (sh_video
->codec
)
2170 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_CODEC=%s\n", sh_video
->codec
->name
);
2172 sh_video
->last_pts
= MP_NOPTS_VALUE
;
2173 sh_video
->num_buffered_pts
= 0;
2174 sh_video
->next_frame_time
= 0;
2177 // Auto quality option enabled
2178 output_quality
=get_video_quality_max(sh_video
);
2179 if(auto_quality
>output_quality
) auto_quality
=output_quality
;
2180 else output_quality
=auto_quality
;
2181 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AutoQ: setting quality to %d.\n",output_quality
);
2182 set_video_quality(sh_video
,output_quality
);
2185 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2187 current_module
="init_vo";
2192 mpctx
->sh_video
= mpctx
->d_video
->sh
= NULL
;
2196 static double update_video_nocorrect_pts(struct MPContext
*mpctx
,
2199 struct sh_video
*sh_video
= mpctx
->sh_video
;
2201 double frame_time
= 0;
2203 current_module
= "filter_video";
2204 // In nocorrect-pts mode there is no way to properly time these frames
2205 if (vf_output_queued_frame(sh_video
->vfilter
))
2207 unsigned char *packet
= NULL
;
2208 frame_time
= sh_video
->next_frame_time
;
2209 if (mpctx
->update_video_immediately
)
2211 int in_size
= video_read_frame(sh_video
, &sh_video
->next_frame_time
,
2212 &packet
, force_fps
);
2214 #ifdef CONFIG_DVDNAV
2215 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
2216 if (mp_dvdnav_is_eof(mpctx
->stream
))
2219 mpctx
->d_video
->eof
= 0;
2221 mpctx
->d_audio
->eof
= 0;
2222 mpctx
->stream
->eof
= 0;
2227 if (in_size
> max_framesize
)
2228 max_framesize
= in_size
;
2229 sh_video
->timer
+= frame_time
;
2230 if (mpctx
->sh_audio
)
2231 mpctx
->delay
-= frame_time
;
2232 // video_read_frame can change fps (e.g. for ASF video)
2233 vo_fps
= sh_video
->fps
;
2234 int framedrop_type
= check_framedrop(mpctx
, frame_time
);
2235 current_module
= "decode video";
2237 void *decoded_frame
;
2238 #ifdef CONFIG_DVDNAV
2239 decoded_frame
= mp_dvdnav_restore_smpi(mpctx
, &in_size
, &packet
, NULL
);
2240 if (in_size
>= 0 && !decoded_frame
)
2242 decoded_frame
= decode_video(sh_video
, packet
, in_size
, framedrop_type
,
2244 #ifdef CONFIG_DVDNAV
2245 // Save last still frame for future display
2246 mp_dvdnav_save_smpi(mpctx
, in_size
, packet
, decoded_frame
);
2248 if (decoded_frame
) {
2249 // These updates are done here for vf_expand OSD/subtitles
2250 update_subtitles(sh_video
, mpctx
->d_sub
, mpctx
->video_offset
, 0);
2251 update_teletext(sh_video
, mpctx
->demuxer
, 0);
2252 update_osd_msg(mpctx
);
2253 current_module
= "filter video";
2254 if (filter_video(sh_video
, decoded_frame
, sh_video
->pts
))
2262 static double update_video(struct MPContext
*mpctx
, int *blit_frame
)
2264 struct sh_video
*sh_video
= mpctx
->sh_video
;
2266 sh_video
->vfilter
->control(sh_video
->vfilter
, VFCTRL_SET_OSD_OBJ
,
2267 mpctx
->osd
); // hack for vf_expand
2268 if (!mpctx
->opts
.correct_pts
)
2269 return update_video_nocorrect_pts(mpctx
, blit_frame
);
2274 current_module
= "filter_video";
2275 // XXX Time used in this call is not counted in any performance
2276 // timer now, OSD time is not updated correctly for filter-added frames
2277 if (vf_output_queued_frame(sh_video
->vfilter
))
2279 unsigned char *packet
= NULL
;
2280 bool hit_eof
= false;
2281 int in_size
= ds_get_packet_pts(mpctx
->d_video
, &packet
, &pts
);
2282 if (pts
!= MP_NOPTS_VALUE
)
2283 pts
+= mpctx
->video_offset
;
2285 // try to extract last frames in case of decoder lag
2290 if (in_size
> max_framesize
)
2291 max_framesize
= in_size
;
2292 current_module
= "decode video";
2293 int framedrop_type
= check_framedrop(mpctx
, sh_video
->frametime
);
2294 void *decoded_frame
= decode_video(sh_video
, packet
, in_size
,
2295 framedrop_type
, pts
);
2296 if (decoded_frame
) {
2297 // These updates are done here for vf_expand OSD/subtitles
2298 update_subtitles(sh_video
, mpctx
->d_sub
, mpctx
->video_offset
, 0);
2299 update_teletext(sh_video
, mpctx
->demuxer
, 0);
2300 update_osd_msg(mpctx
);
2301 current_module
= "filter video";
2302 if (filter_video(sh_video
, decoded_frame
, sh_video
->pts
))
2308 sh_video
->vfilter
->control(sh_video
->vfilter
, VFCTRL_GET_PTS
, &pts
);
2309 if (pts
== MP_NOPTS_VALUE
) {
2310 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Video pts after filters MISSING\n");
2311 // Try to use decoder pts from before filters
2312 pts
= sh_video
->pts
;
2313 if (pts
== MP_NOPTS_VALUE
)
2314 pts
= sh_video
->last_pts
;
2316 sh_video
->pts
= pts
;
2317 if (sh_video
->last_pts
== MP_NOPTS_VALUE
)
2318 sh_video
->last_pts
= sh_video
->pts
;
2319 else if (sh_video
->last_pts
> sh_video
->pts
) {
2320 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Decreasing video pts: %f < %f\n",
2321 sh_video
->pts
, sh_video
->last_pts
);
2322 /* If the difference in pts is small treat it as jitter around the
2323 * right value (possibly caused by incorrect timestamp ordering) and
2324 * just show this frame immediately after the last one.
2325 * Treat bigger differences as timestamp resets and start counting
2326 * timing of later frames from the position of this one. */
2327 if (sh_video
->last_pts
- sh_video
->pts
> 0.5)
2328 sh_video
->last_pts
= sh_video
->pts
;
2330 sh_video
->pts
= sh_video
->last_pts
;
2332 double frame_time
= sh_video
->pts
- sh_video
->last_pts
;
2333 sh_video
->last_pts
= sh_video
->pts
;
2334 sh_video
->timer
+= frame_time
;
2335 if (mpctx
->sh_audio
)
2336 mpctx
->delay
-= frame_time
;
2341 void pause_player(struct MPContext
*mpctx
)
2346 mpctx
->step_frames
= 0;
2347 mpctx
->time_frame
-= get_relative_time(mpctx
);
2349 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
)
2350 vo_control(mpctx
->video_out
, VOCTRL_PAUSE
, NULL
);
2352 if (mpctx
->audio_out
&& mpctx
->sh_audio
)
2353 mpctx
->audio_out
->pause(); // pause audio, keep data if possible
2356 void unpause_player(struct MPContext
*mpctx
)
2362 if (mpctx
->audio_out
&& mpctx
->sh_audio
)
2363 mpctx
->audio_out
->resume(); // resume audio
2364 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
2365 && !mpctx
->step_frames
)
2366 vo_control(mpctx
->video_out
, VOCTRL_RESUME
, NULL
); // resume video
2367 (void)get_relative_time(mpctx
); // ignore time that passed during pause
2370 void add_step_frame(struct MPContext
*mpctx
)
2372 mpctx
->step_frames
++;
2373 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
)
2374 vo_control(mpctx
->video_out
, VOCTRL_PAUSE
, NULL
);
2375 unpause_player(mpctx
);
2378 static void pause_loop(struct MPContext
*mpctx
)
2382 // Small hack to display the pause message on the OSD line.
2383 // The pause string is: "\n == PAUSE == \r" so we need to
2384 // take the first and the last char out
2385 if (term_osd
&& !mpctx
->sh_video
) {
2386 char msg
[128] = _("\n ===== PAUSE =====\r");
2387 int mlen
= strlen(msg
);
2389 set_osd_msg(OSD_MSG_PAUSE
, 1, 0, "%s", msg
+1);
2390 update_osd_msg(mpctx
);
2392 mp_tmsg(MSGT_CPLAYER
,MSGL_STATUS
,"\n ===== PAUSE =====\r");
2393 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_PAUSED\n");
2396 while ( (cmd
= mp_input_get_cmd(mpctx
->input
, 20, 1, 1)) == NULL
2397 || cmd
->id
== MP_CMD_SET_MOUSE_POS
|| cmd
->pausing
== 4) {
2399 cmd
= mp_input_get_cmd(mpctx
->input
, 0,1,0);
2400 run_command(mpctx
, cmd
);
2404 if (mpctx
->sh_video
&& mpctx
->video_out
)
2405 vo_check_events(mpctx
->video_out
);
2408 vf_menu_pause_update(vf_menu
);
2411 update_osd_msg(mpctx
);
2412 int hack
= vo_osd_changed(0);
2413 vo_osd_changed(hack
);
2420 // Find the right mute status and record position for new file position
2421 static void edl_seek_reset(MPContext
*mpctx
)
2423 mpctx
->edl_muted
= 0;
2424 next_edl_record
= edl_records
;
2426 while (next_edl_record
) {
2427 if (next_edl_record
->start_sec
>= mpctx
->sh_video
->pts
)
2430 if (next_edl_record
->action
== EDL_MUTE
)
2431 mpctx
->edl_muted
= !mpctx
->edl_muted
;
2432 next_edl_record
= next_edl_record
->next
;
2434 if ((mpctx
->user_muted
| mpctx
->edl_muted
) != mpctx
->mixer
.muted
)
2435 mixer_mute(&mpctx
->mixer
);
2439 // Execute EDL command for the current position if one exists
2440 static void edl_update(MPContext
*mpctx
)
2442 if (!next_edl_record
)
2445 if (!mpctx
->sh_video
) {
2446 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Cannot use EDL without video, disabling.\n");
2447 free_edl(edl_records
);
2448 next_edl_record
= NULL
;
2453 if (mpctx
->sh_video
->pts
>= next_edl_record
->start_sec
) {
2454 if (next_edl_record
->action
== EDL_SKIP
) {
2455 mpctx
->osd_function
= OSD_FFW
;
2456 mpctx
->abs_seek_pos
= 0;
2457 mpctx
->rel_seek_secs
= next_edl_record
->length_sec
;
2458 mp_msg(MSGT_CPLAYER
, MSGL_DBG4
, "EDL_SKIP: start [%f], stop "
2459 "[%f], length [%f]\n", next_edl_record
->start_sec
,
2460 next_edl_record
->stop_sec
, next_edl_record
->length_sec
);
2462 else if (next_edl_record
->action
== EDL_MUTE
) {
2463 mpctx
->edl_muted
= !mpctx
->edl_muted
;
2464 if ((mpctx
->user_muted
| mpctx
->edl_muted
) != mpctx
->mixer
.muted
)
2465 mixer_mute(&mpctx
->mixer
);
2466 mp_msg(MSGT_CPLAYER
, MSGL_DBG4
, "EDL_MUTE: [%f]\n",
2467 next_edl_record
->start_sec
);
2469 next_edl_record
= next_edl_record
->next
;
2473 static void reinit_decoders(struct MPContext
*mpctx
)
2475 reinit_video_chain(mpctx
);
2476 reinit_audio_chain(mpctx
);
2477 mp_property_do("sub", M_PROPERTY_SET
, &mpctx
->global_sub_pos
, mpctx
);
2480 static bool timeline_set_part(struct MPContext
*mpctx
, int i
)
2482 struct timeline_part
*p
= mpctx
->timeline
+ mpctx
->timeline_part
;
2483 struct timeline_part
*n
= mpctx
->timeline
+ i
;
2484 mpctx
->timeline_part
= i
;
2485 mpctx
->video_offset
= n
->start
- n
->source_start
;
2486 if (n
->source
== p
->source
)
2488 uninit_player(mpctx
, INITIALIZED_VCODEC
| (mpctx
->opts
.fixed_vo
&& mpctx
->opts
.video_id
!= -2 ? 0 : INITIALIZED_VO
) | INITIALIZED_AO
| INITIALIZED_ACODEC
);
2489 mpctx
->demuxer
= n
->source
->demuxer
;
2490 mpctx
->d_video
= mpctx
->demuxer
->video
;
2491 mpctx
->d_audio
= mpctx
->demuxer
->audio
;
2492 mpctx
->d_sub
= mpctx
->demuxer
->sub
;
2493 mpctx
->sh_video
= mpctx
->d_video
->sh
;
2494 mpctx
->sh_audio
= mpctx
->d_audio
->sh
;
2498 // Given pts, switch playback to the corresponding part.
2499 // Return offset within that part.
2500 static double timeline_set_from_time(struct MPContext
*mpctx
, double pts
,
2505 for (int i
= 0; i
< mpctx
->num_timeline_parts
; i
++) {
2506 struct timeline_part
*p
= mpctx
->timeline
+ i
;
2507 if (pts
< (p
+1)->start
) {
2508 *need_reset
= timeline_set_part(mpctx
, i
);
2509 return pts
- p
->start
+ p
->source_start
;
2516 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2517 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2518 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2519 static int seek(MPContext
*mpctx
, double amount
, int style
)
2521 current_module
= "seek";
2522 if (mpctx
->stop_play
== AT_END_OF_FILE
)
2523 mpctx
->stop_play
= KEEP_PLAYING
;
2524 if (mpctx
->timeline
&& style
& SEEK_FACTOR
) {
2525 amount
*= mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
2526 style
&= ~SEEK_FACTOR
;
2528 if ((mpctx
->demuxer
->accurate_seek
|| mpctx
->timeline
) && mpctx
->sh_video
2529 && !(style
& (SEEK_ABSOLUTE
| SEEK_FACTOR
))) {
2530 style
|= SEEK_ABSOLUTE
;
2532 style
|= SEEK_FORWARD
;
2534 style
|= SEEK_BACKWARD
;
2535 amount
+= mpctx
->sh_video
->pts
;
2538 /* At least the liba52 decoder wants to read from the input stream
2539 * during initialization, so reinit must be done after the demux_seek()
2540 * call that clears possible stream EOF. */
2541 bool need_reset
= false;
2542 if (mpctx
->timeline
) {
2543 amount
= timeline_set_from_time(mpctx
, amount
, &need_reset
);
2545 mpctx
->stop_play
= AT_END_OF_FILE
;
2546 // Clear audio from current position
2547 if (mpctx
->sh_audio
) {
2548 mpctx
->audio_out
->reset();
2549 mpctx
->sh_audio
->a_buffer_len
= 0;
2550 mpctx
->sh_audio
->a_out_buffer_len
= 0;
2555 int seekresult
= demux_seek(mpctx
->demuxer
, amount
, audio_delay
, style
);
2557 reinit_decoders(mpctx
);
2558 if (seekresult
== 0)
2561 if (mpctx
->sh_video
) {
2562 current_module
= "seek_video_reset";
2563 resync_video_stream(mpctx
->sh_video
);
2564 if (mpctx
->video_out
->config_ok
)
2565 vo_control(mpctx
->video_out
, VOCTRL_RESET
, NULL
);
2566 mpctx
->sh_video
->num_buffered_pts
= 0;
2567 mpctx
->sh_video
->last_pts
= MP_NOPTS_VALUE
;
2568 mpctx
->num_buffered_frames
= 0;
2570 mpctx
->time_frame
= 0;
2571 mpctx
->update_video_immediately
= true;
2572 // Not all demuxers set d_video->pts during seek, so this value
2573 // (which is used by at least vobsub and edl code below) may
2574 // be completely wrong (probably 0).
2575 mpctx
->sh_video
->pts
= mpctx
->d_video
->pts
+ mpctx
->video_offset
;
2576 update_subtitles(mpctx
->sh_video
, mpctx
->d_sub
, mpctx
->video_offset
, 1);
2577 update_teletext(mpctx
->sh_video
, mpctx
->demuxer
, 1);
2580 if (mpctx
->sh_audio
) {
2581 current_module
= "seek_audio_reset";
2582 mpctx
->audio_out
->reset(); // stop audio, throwing away buffered data
2583 mpctx
->sh_audio
->a_buffer_len
= 0;
2584 mpctx
->sh_audio
->a_out_buffer_len
= 0;
2587 if (vo_vobsub
&& mpctx
->sh_video
) {
2588 current_module
= "seek_vobsub_reset";
2589 vobsub_seek(vo_vobsub
, mpctx
->sh_video
->pts
);
2592 edl_seek_reset(mpctx
);
2594 mpctx
->total_avsync_change
= 0;
2595 audio_time_usage
= 0; video_time_usage
= 0; vout_time_usage
= 0;
2598 current_module
= NULL
;
2602 int get_current_chapter(struct MPContext
*mpctx
)
2604 if (!mpctx
->chapters
|| !mpctx
->sh_video
)
2605 return demuxer_get_current_chapter(mpctx
->demuxer
);
2608 double current_pts
= mpctx
->sh_video
->pts
;
2609 for (i
= 1; i
< mpctx
->num_chapters
; i
++)
2610 if (current_pts
< mpctx
->chapters
[i
].start
)
2615 // currently returns a string allocated with malloc, not talloc
2616 char *chapter_display_name(struct MPContext
*mpctx
, int chapter
)
2618 if (!mpctx
->chapters
|| !mpctx
->sh_video
)
2619 return demuxer_chapter_display_name(mpctx
->demuxer
, chapter
);
2620 return strdup(mpctx
->chapters
[chapter
].name
);
2623 int seek_chapter(struct MPContext
*mpctx
, int chapter
, double *seek_pts
,
2624 char **chapter_name
)
2626 if (!mpctx
->chapters
|| !mpctx
->sh_video
)
2627 return demuxer_seek_chapter(mpctx
->demuxer
, chapter
, seek_pts
,
2629 if (chapter
>= mpctx
->num_chapters
)
2633 *seek_pts
= mpctx
->chapters
[chapter
].start
;
2635 *chapter_name
= talloc_strdup(NULL
, mpctx
->chapters
[chapter
].name
);
2639 static int find_ordered_chapter_sources(struct MPContext
*mpctx
,
2640 struct content_source
*sources
,
2642 unsigned char uid_map
[][16])
2644 int num_filenames
= 0;
2645 char **filenames
= NULL
;
2646 if (num_sources
> 1) {
2647 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "This file references data from "
2648 "other sources.\n");
2649 if (mpctx
->stream
->type
!= STREAMTYPE_FILE
) {
2650 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, "Playback source is not a "
2651 "normal disk file. Will not search for related files.\n");
2653 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Will scan other files in the "
2654 "same directory to find referenced sources.\n");
2655 filenames
= find_files(mpctx
->demuxer
->filename
, ".mkv",
2660 int num_left
= num_sources
- 1;
2661 for (int i
= 0; i
< num_filenames
&& num_left
> 0; i
++) {
2662 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Checking file %s\n",
2663 filename_recode(filenames
[i
]));
2665 struct stream
*s
= open_stream(filenames
[i
], &mpctx
->opts
, &format
);
2668 struct demuxer
*d
= demux_open(&mpctx
->opts
, s
, DEMUXER_TYPE_MATROSKA
,
2669 mpctx
->opts
.audio_id
,
2670 mpctx
->opts
.video_id
,
2671 mpctx
->opts
.sub_id
, filenames
[i
]);
2676 if (d
->file_format
== DEMUXER_TYPE_MATROSKA
) {
2677 for (int i
= 1; i
< num_sources
; i
++) {
2678 if (sources
[i
].demuxer
)
2680 if (!memcmp(uid_map
[i
], d
->matroska_data
.segment_uid
, 16)) {
2681 mp_msg(MSGT_CPLAYER
, MSGL_INFO
,"Match for source %d: %s\n",
2682 i
, filename_recode(d
->filename
));
2683 sources
[i
].stream
= s
;
2684 sources
[i
].demuxer
= d
;
2696 talloc_free(filenames
);
2698 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Failed to find ordered chapter part!\n"
2699 "There will be parts MISSING from the video!\n");
2700 for (int i
= 1, j
= 1; i
< num_sources
; i
++)
2701 if (sources
[i
].demuxer
) {
2702 sources
[j
] = sources
[i
];
2703 memcpy(uid_map
[j
], uid_map
[i
], 16);
2707 return num_sources
- num_left
;
2710 static void build_ordered_chapter_timeline(struct MPContext
*mpctx
)
2712 if (!mpctx
->opts
.ordered_chapters
) {
2713 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "File uses ordered chapters, but "
2714 "you have disabled support for them. Ignoring.\n");
2718 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "File uses ordered chapters, will build "
2719 "edit timeline.\n");
2721 struct demuxer
*demuxer
= mpctx
->demuxer
;
2722 struct matroska_data
*m
= &demuxer
->matroska_data
;
2724 struct content_source
*sources
= talloc_array_ptrtype(NULL
, sources
,
2725 m
->num_ordered_chapters
);
2726 sources
[0].stream
= mpctx
->stream
;
2727 sources
[0].demuxer
= mpctx
->demuxer
;
2728 unsigned char uid_map
[m
->num_ordered_chapters
][16];
2729 int num_sources
= 1;
2730 memcpy(uid_map
[0], m
->segment_uid
, 16);
2732 for (int i
= 0; i
< m
->num_ordered_chapters
; i
++) {
2733 struct matroska_chapter
*c
= m
->ordered_chapters
+ i
;
2734 if (!c
->has_segment_uid
)
2735 memcpy(c
->segment_uid
, m
->segment_uid
, 16);
2737 for (int j
= 0; j
< num_sources
; j
++)
2738 if (!memcmp(c
->segment_uid
, uid_map
[j
], 16))
2740 memcpy(uid_map
[num_sources
], c
->segment_uid
, 16);
2741 sources
[num_sources
] = (struct content_source
){};
2747 num_sources
= find_ordered_chapter_sources(mpctx
, sources
, num_sources
,
2751 struct timeline_part
*timeline
= talloc_array_ptrtype(NULL
, timeline
,
2752 m
->num_ordered_chapters
+ 1);
2753 struct chapter
*chapters
= talloc_array_ptrtype(NULL
, chapters
,
2754 m
->num_ordered_chapters
);
2755 uint64_t starttime
= 0;
2756 uint64_t missing_time
= 0;
2758 int num_chapters
= 0;
2759 uint64_t prev_part_offset
;
2760 for (int i
= 0; i
< m
->num_ordered_chapters
; i
++) {
2761 struct matroska_chapter
*c
= m
->ordered_chapters
+ i
;
2764 for (j
= 0; j
< num_sources
; j
++) {
2765 if (!memcmp(c
->segment_uid
, uid_map
[j
], 16))
2768 missing_time
+= c
->end
- c
->start
;
2771 chapters
[num_chapters
].start
= starttime
/ 1000.;
2772 chapters
[num_chapters
].name
= talloc_strdup(chapters
, c
->name
);
2773 /* Only add a separate part if the time or file actually changes.
2774 * Matroska files have chapter divisions that are redundant from
2775 * timeline point of view because the same chapter structure is used
2776 * both to specify the timeline and for normal chapter information.
2777 * Removing a missing inserted external chapter can also cause this. */
2778 if (part_count
== 0 || c
->start
!= starttime
+ prev_part_offset
2779 || sources
+ j
!= timeline
[part_count
- 1].source
) {
2780 timeline
[part_count
].source
= sources
+ j
;
2781 timeline
[part_count
].start
= chapters
[num_chapters
].start
;
2782 timeline
[part_count
].source_start
= c
->start
/ 1000.;
2783 prev_part_offset
= c
->start
- starttime
;
2786 starttime
+= c
->end
- c
->start
;
2789 timeline
[part_count
].start
= starttime
/ 1000.;
2792 // None of the parts come from the file itself???
2793 talloc_free(sources
);
2794 talloc_free(timeline
);
2795 talloc_free(chapters
);
2799 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Timeline contains %d parts from %d "
2800 "sources. Total length %.3f seconds.\n", part_count
, num_sources
,
2801 timeline
[part_count
].start
);
2803 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "There are %.3f seconds missing "
2804 "from the timeline!\n", missing_time
/ 1000.);
2805 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Source files:\n");
2806 for (int i
= 0; i
< num_sources
; i
++)
2807 mp_msg(MSGT_CPLAYER
, MSGL_V
, "%d: %s\n", i
,
2808 filename_recode(sources
[i
].demuxer
->filename
));
2809 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Timeline parts: (number, start, "
2810 "source_start, source):\n");
2811 for (int i
= 0; i
< part_count
; i
++) {
2812 struct timeline_part
*p
= timeline
+ i
;
2813 mp_msg(MSGT_CPLAYER
, MSGL_V
, "%3d %9.3f %9.3f %3td\n", i
, p
->start
,
2814 p
->source_start
, p
->source
- sources
);
2816 mp_msg(MSGT_CPLAYER
, MSGL_V
, "END %9.3f\n", timeline
[part_count
].start
);
2817 mpctx
->sources
= sources
;
2818 mpctx
->num_sources
= num_sources
;
2819 mpctx
->timeline
= timeline
;
2820 mpctx
->num_timeline_parts
= part_count
;
2821 mpctx
->num_chapters
= num_chapters
;
2822 mpctx
->chapters
= chapters
;
2824 mpctx
->timeline_part
= 0;
2825 mpctx
->video_offset
= timeline
[0].source_start
;
2826 mpctx
->demuxer
= timeline
[0].source
->demuxer
;
2830 static int read_keys(void *ctx
, int fd
)
2833 return mplayer_get_key(ctx
, 0);
2837 #ifdef PTW32_STATIC_LIB
2838 static void detach_ptw32(void)
2840 pthread_win32_thread_detach_np();
2841 pthread_win32_process_detach_np();
2845 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2846 * file for some tools to link against. */
2847 #ifndef DISABLE_MAIN
2848 int main(int argc
,char* argv
[]){
2855 /* Flag indicating whether MPlayer should exit without playing anything. */
2858 //float a_frame=0; // Audio
2862 struct MPContext
*mpctx
= &(struct MPContext
){
2863 .osd_function
= OSD_PLAY
,
2864 .begin_skip
= MP_NOPTS_VALUE
,
2865 .play_tree_step
= 1,
2866 .global_sub_pos
= -1,
2867 .set_of_sub_pos
= -1,
2868 .file_format
= DEMUXER_TYPE_UNKNOWN
,
2872 #ifdef PTW32_STATIC_LIB
2873 pthread_win32_process_attach_np();
2874 pthread_win32_thread_attach_np();
2875 atexit(detach_ptw32
);
2879 srand(GetTimerMS());
2884 mpctx
->x11_state
= vo_x11_init_state();
2886 struct MPOpts
*opts
= &mpctx
->opts
;
2887 set_default_mplayer_options(opts
);
2888 // Create the config context and register the options
2889 mpctx
->mconfig
= m_config_new(opts
, cfg_include
);
2890 m_config_register_options(mpctx
->mconfig
,mplayer_opts
);
2891 mp_input_register_options(mpctx
->mconfig
);
2893 // Preparse the command line
2894 m_config_preparse_command_line(mpctx
->mconfig
,argc
,argv
);
2896 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2901 stream_tv_defaults
.immediate
= 1;
2904 parse_cfgfiles(mpctx
, mpctx
->mconfig
);
2906 mpctx
->playtree
= m_config_parse_mp_command_line(mpctx
->mconfig
, argc
, argv
);
2907 if(mpctx
->playtree
== NULL
)
2910 mpctx
->playtree
= play_tree_cleanup(mpctx
->playtree
);
2911 if(mpctx
->playtree
) {
2912 mpctx
->playtree_iter
= play_tree_iter_new(mpctx
->playtree
,mpctx
->mconfig
);
2913 if(mpctx
->playtree_iter
) {
2914 if(play_tree_iter_step(mpctx
->playtree_iter
,0,0) != PLAY_TREE_ITER_ENTRY
) {
2915 play_tree_iter_free(mpctx
->playtree_iter
);
2916 mpctx
->playtree_iter
= NULL
;
2918 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
,1);
2922 mpctx
->key_fifo
= mp_fifo_create(opts
);
2924 print_version("MPlayer");
2926 #if defined(__MINGW32__) || defined(__CYGWIN__)
2927 // stop Windows from showing all kinds of annoying error dialogs
2928 SetErrorMode(0x8003);
2929 // request 1ms timer resolution
2933 #ifdef CONFIG_PRIORITY
2937 if(opts
->video_driver_list
&& strcmp(opts
->video_driver_list
[0],"help")==0){
2942 if(opts
->audio_driver_list
&& strcmp(opts
->audio_driver_list
[0],"help")==0){
2947 /* Check codecs.conf. */
2948 if(!codecs_file
|| !parse_codec_cfg(codecs_file
)){
2949 if(!parse_codec_cfg(mem_ptr
=get_path("codecs.conf"))){
2950 if(!parse_codec_cfg(MPLAYER_CONFDIR
"/codecs.conf")){
2951 if(!parse_codec_cfg(NULL
)){
2952 exit_player_with_rc(mpctx
, EXIT_NONE
, 0);
2954 mp_tmsg(MSGT_CPLAYER
,MSGL_V
,"Using built-in default codecs.conf.\n");
2957 free( mem_ptr
); // release the buffer created by get_path()
2961 if(video_codec_list
){
2963 video_codec
=video_codec_list
[0];
2964 for(i
=0;video_codec_list
[i
];i
++)
2965 mp_msg(MSGT_FIXME
,MSGL_FIXME
,"vc#%d: '%s'\n",i
,video_codec_list
[i
]);
2968 if(audio_codec_list
&& strcmp(audio_codec_list
[0],"help")==0){
2969 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "Available audio codecs:\n");
2970 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_CODECS\n");
2972 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
2975 if(video_codec_list
&& strcmp(video_codec_list
[0],"help")==0){
2976 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "Available video codecs:\n");
2977 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_CODECS\n");
2979 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
2982 if(video_fm_list
&& strcmp(video_fm_list
[0],"help")==0){
2984 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
2987 if(audio_fm_list
&& strcmp(audio_fm_list
[0],"help")==0){
2989 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
2992 if(af_cfg
.list
&& strcmp(af_cfg
.list
[0],"help")==0){
2998 if(vo_fstype_list
&& strcmp(vo_fstype_list
[0],"help")==0){
3000 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3004 if((demuxer_name
&& strcmp(demuxer_name
,"help")==0) ||
3005 (audio_demuxer_name
&& strcmp(audio_demuxer_name
,"help")==0) ||
3006 (sub_demuxer_name
&& strcmp(sub_demuxer_name
,"help")==0)){
3008 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "\n");
3011 if(list_properties
) {
3012 property_print_help();
3017 exit_player(mpctx
, EXIT_NONE
);
3019 if(!mpctx
->filename
&& !player_idle_mode
){
3020 // no file/vcd/dvd -> show HELP:
3021 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, help_text
);
3022 exit_player_with_rc(mpctx
, EXIT_NONE
, 0);
3025 /* Display what configure line was used */
3026 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Configuration: " CONFIGURATION
"\n");
3028 // Many users forget to include command line in bugreports...
3029 if( mp_msg_test(MSGT_CPLAYER
,MSGL_V
) ){
3030 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "CommandLine:");
3031 for(i
=1;i
<argc
;i
++)mp_msg(MSGT_CPLAYER
, MSGL_INFO
," '%s'",argv
[i
]);
3032 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "\n");
3035 //------ load global data first ------
3037 mpctx
->osd
= osd_create();
3040 #ifdef CONFIG_FREETYPE
3043 #ifdef CONFIG_FONTCONFIG
3044 if(font_fontconfig
<= 0)
3047 #ifdef CONFIG_BITMAP_FONT
3049 vo_font
=read_font_desc(font_name
,font_factor
,verbose
>1);
3050 if(!vo_font
) mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Cannot load bitmap font: %s\n",
3051 filename_recode(font_name
));
3054 vo_font
=read_font_desc( mem_ptr
=get_path("font/font.desc"),font_factor
,verbose
>1);
3055 free(mem_ptr
); // release the buffer created by get_path()
3057 vo_font
=read_font_desc(MPLAYER_DATADIR
"/font/font.desc",font_factor
,verbose
>1);
3060 mpctx
->osd
->sub_font
= read_font_desc(sub_font_name
, font_factor
, verbose
>1);
3062 mpctx
->osd
->sub_font
= vo_font
;
3064 #ifdef CONFIG_FONTCONFIG
3069 ass_library
= ass_init();
3075 // seteuid(0); /* Can't hurt to try to get root here */
3076 if ((rtc_fd
= open(rtc_device
? rtc_device
: "/dev/rtc", O_RDONLY
)) < 0)
3077 mp_tmsg(MSGT_CPLAYER
, MSGL_WARN
, "Failed to open %s: %s (it should be readable by the user.)\n",
3078 rtc_device
? rtc_device
: "/dev/rtc", strerror(errno
));
3080 unsigned long irqp
= 1024; /* 512 seemed OK. 128 is jerky. */
3082 if (ioctl(rtc_fd
, RTC_IRQP_SET
, irqp
) < 0) {
3083 mp_tmsg(MSGT_CPLAYER
, MSGL_WARN
, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp
, strerror(errno
));
3084 mp_tmsg(MSGT_CPLAYER
, MSGL_HINT
, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp
);
3087 } else if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
3088 /* variable only by the root */
3089 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno
));
3093 mp_tmsg(MSGT_CPLAYER
, MSGL_V
, "Using Linux hardware RTC timing (%ldHz).\n", irqp
);
3097 #endif /* HAVE_RTC */
3098 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Using %s timing\n",
3099 softsleep
?"software":timer_name
);
3102 load_termcap(NULL
); // load key-codes
3105 // ========== Init keyboard FIFO (connection to libvo) ============
3107 // Init input system
3108 current_module
= "init_input";
3109 mpctx
->input
= mp_input_init(&opts
->input
);
3110 mp_input_add_key_fd(mpctx
->input
, -1,0,mplayer_get_key
,NULL
, mpctx
->key_fifo
);
3112 mp_input_add_cmd_fd(mpctx
->input
, 0,USE_SELECT
,MP_INPUT_SLAVE_CMD_FUNC
,NULL
);
3113 else if(!noconsolecontrols
)
3114 mp_input_add_key_fd(mpctx
->input
, 0, 1, read_keys
, NULL
, mpctx
->key_fifo
);
3115 // Set the libstream interrupt callback
3116 stream_set_interrupt_callback(mp_input_check_interrupt
, mpctx
->input
);
3120 if(menu_cfg
&& menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
, menu_cfg
))
3121 mp_tmsg(MSGT_CPLAYER
, MSGL_V
, "Menu initialized: %s\n", menu_cfg
);
3123 menu_cfg
= get_path("menu.conf");
3124 if(menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
, menu_cfg
))
3125 mp_tmsg(MSGT_CPLAYER
, MSGL_V
, "Menu initialized: %s\n", menu_cfg
);
3127 if(menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
,
3128 MPLAYER_CONFDIR
"/menu.conf"))
3129 mp_tmsg(MSGT_CPLAYER
, MSGL_V
, "Menu initialized: %s\n", MPLAYER_CONFDIR
"/menu.conf");
3131 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Menu init failed.\n");
3139 current_module
= NULL
;
3143 signal(SIGCHLD
,child_sighandler
);
3146 #ifdef CONFIG_CRASH_DEBUG
3147 prog_path
= argv
[0];
3149 //========= Catch terminate signals: ================
3150 // terminate requests:
3151 signal(SIGTERM
,exit_sighandler
); // kill
3152 signal(SIGHUP
,exit_sighandler
); // kill -HUP / xterm closed
3154 signal(SIGINT
,exit_sighandler
); // Interrupt from keyboard
3156 signal(SIGQUIT
,exit_sighandler
); // Quit from keyboard
3157 signal(SIGPIPE
,exit_sighandler
); // Some window managers cause this
3158 #ifdef CONFIG_SIGHANDLER
3160 signal(SIGBUS
,exit_sighandler
); // bus error
3161 signal(SIGSEGV
,exit_sighandler
); // segfault
3162 signal(SIGILL
,exit_sighandler
); // illegal instruction
3163 signal(SIGFPE
,exit_sighandler
); // floating point exc.
3164 signal(SIGABRT
,exit_sighandler
); // abort()
3165 #ifdef CONFIG_CRASH_DEBUG
3167 signal(SIGTRAP
,exit_sighandler
);
3171 // ******************* Now, let's see the per-file stuff ********************
3175 // init global sub numbers
3176 mpctx
->global_sub_size
= 0;
3177 { int i
; for (i
= 0; i
< SUB_SOURCES
; i
++) mpctx
->global_sub_indices
[i
] = -1; }
3179 if (mpctx
->filename
) {
3180 load_per_protocol_config (mpctx
->mconfig
, mpctx
->filename
);
3181 load_per_extension_config (mpctx
->mconfig
, mpctx
->filename
);
3182 load_per_file_config (mpctx
->mconfig
, mpctx
->filename
);
3185 if (opts
->video_driver_list
)
3186 load_per_output_config (mpctx
->mconfig
, PROFILE_CFG_VO
, opts
->video_driver_list
[0]);
3187 if (opts
->audio_driver_list
)
3188 load_per_output_config (mpctx
->mconfig
, PROFILE_CFG_AO
, opts
->audio_driver_list
[0]);
3190 // We must enable getch2 here to be able to interrupt network connection
3192 if(!noconsolecontrols
&& !slave_mode
){
3193 if(mpctx
->initialized_flags
&INITIALIZED_GETCH2
)
3194 mp_tmsg(MSGT_CPLAYER
,MSGL_WARN
,"WARNING: getch2_init called twice!\n");
3196 getch2_enable(); // prepare stdin for hotkeys...
3197 mpctx
->initialized_flags
|=INITIALIZED_GETCH2
;
3198 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n[[[init getch2]]]\n");
3201 // =================== GUI idle loop (STOP state) ===========================
3202 while (player_idle_mode
&& !mpctx
->filename
) {
3203 play_tree_t
* entry
= NULL
;
3205 while (!(cmd
= mp_input_get_cmd(mpctx
->input
, 0,1,0))) { // wait for command
3206 if (mpctx
->video_out
)
3207 vo_check_events(mpctx
->video_out
);
3211 case MP_CMD_LOADFILE
:
3212 // prepare a tree entry with the new filename
3213 entry
= play_tree_new();
3214 play_tree_add_file(entry
, cmd
->args
[0].v
.s
);
3215 // The entry is added to the main playtree after the switch().
3217 case MP_CMD_LOADLIST
:
3218 entry
= parse_playlist_file(mpctx
->mconfig
, cmd
->args
[0].v
.s
);
3221 exit_player_with_rc(mpctx
, EXIT_QUIT
, (cmd
->nargs
> 0)? cmd
->args
[0].v
.i
: 0);
3223 case MP_CMD_GET_PROPERTY
:
3224 case MP_CMD_SET_PROPERTY
:
3225 case MP_CMD_STEP_PROPERTY
:
3226 run_command(mpctx
, cmd
);
3232 if (entry
) { // user entered a command that gave a valid entry
3233 if (mpctx
->playtree
) // the playtree is always a node with one child. let's clear it
3234 play_tree_free_list(mpctx
->playtree
->child
, 1);
3235 else mpctx
->playtree
=play_tree_new(); // .. or make a brand new playtree
3237 if (!mpctx
->playtree
) continue; // couldn't make playtree! wait for next command
3239 play_tree_set_child(mpctx
->playtree
, entry
);
3241 /* Make iterator start at the top the of tree. */
3242 mpctx
->playtree_iter
= play_tree_iter_new(mpctx
->playtree
, mpctx
->mconfig
);
3243 if (!mpctx
->playtree_iter
) continue;
3245 // find the first real item in the tree
3246 if (play_tree_iter_step(mpctx
->playtree_iter
,0,0) != PLAY_TREE_ITER_ENTRY
) {
3248 play_tree_iter_free(mpctx
->playtree_iter
);
3249 mpctx
->playtree_iter
= NULL
;
3250 continue; // wait for next command
3252 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
, 1);
3255 //---------------------------------------------------------------------------
3258 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"\nPlaying %s.\n",
3259 filename_recode(mpctx
->filename
));
3262 if (edl_records
) free_edl(edl_records
);
3263 next_edl_record
= edl_records
= edl_parse_file();
3265 if (edl_output_filename
) {
3266 if (edl_fd
) fclose(edl_fd
);
3267 if ((edl_fd
= fopen(edl_output_filename
, "w")) == NULL
)
3269 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Can't open EDL file [%s] for writing.\n",
3270 filename_recode(edl_output_filename
));
3274 //==================== Open VOB-Sub ============================
3276 current_module
="vobsub";
3278 vo_vobsub
=vobsub_open(vobsub_name
,spudec_ifo
,1,&vo_spudec
);
3280 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Cannot load subtitles: %s\n",
3281 filename_recode(vobsub_name
));
3282 } else if (sub_auto
&& mpctx
->filename
){
3283 /* try to autodetect vobsub from movie filename ::atmos */
3284 char *buf
= strdup(mpctx
->filename
), *psub
;
3285 char *pdot
= strrchr(buf
, '.');
3286 char *pslash
= strrchr(buf
, '/');
3287 #if defined(__MINGW32__) || defined(__CYGWIN__)
3288 if (!pslash
) pslash
= strrchr(buf
, '\\');
3290 if (pdot
&& (!pslash
|| pdot
> pslash
))
3292 vo_vobsub
=vobsub_open(buf
,spudec_ifo
,0,&vo_spudec
);
3293 /* try from ~/.mplayer/sub */
3294 if(!vo_vobsub
&& (psub
= get_path( "sub/" ))) {
3297 bname
= strrchr(buf
,'/');
3298 #if defined(__MINGW32__) || defined(__CYGWIN__)
3299 if(!bname
) bname
= strrchr(buf
,'\\');
3303 l
= strlen(psub
) + strlen(bname
) + 1;
3304 psub
= realloc(psub
,l
);
3306 vo_vobsub
=vobsub_open(psub
,spudec_ifo
,0,&vo_spudec
);
3312 mpctx
->initialized_flags
|=INITIALIZED_VOBSUB
;
3313 vobsub_set_from_lang(vo_vobsub
, dvdsub_lang
);
3314 mp_property_do("sub_forced_only", M_PROPERTY_SET
, &forced_subs_only
, mpctx
);
3316 // setup global sub numbering
3317 mpctx
->global_sub_indices
[SUB_SOURCE_VOBSUB
] = mpctx
->global_sub_size
; // the global # of the first vobsub.
3318 mpctx
->global_sub_size
+= vobsub_get_indexes_count(vo_vobsub
);
3321 //============ Open & Sync STREAM --- fork cache2 ====================
3324 mpctx
->demuxer
=NULL
;
3325 if (mpctx
->d_audio
) {
3326 //free_demuxer_stream(mpctx->d_audio);
3327 mpctx
->d_audio
=NULL
;
3329 if (mpctx
->d_video
) {
3330 //free_demuxer_stream(d_video);
3331 mpctx
->d_video
=NULL
;
3333 mpctx
->sh_audio
=NULL
;
3334 mpctx
->sh_video
=NULL
;
3336 current_module
="open_stream";
3337 mpctx
->stream
= open_stream(mpctx
->filename
, opts
, &mpctx
->file_format
);
3338 if(!mpctx
->stream
) { // error...
3339 mpctx
->stop_play
= libmpdemux_was_interrupted(mpctx
, PT_NEXT_ENTRY
);
3340 goto goto_next_file
;
3342 mpctx
->initialized_flags
|=INITIALIZED_STREAM
;
3344 if(mpctx
->file_format
== DEMUXER_TYPE_PLAYLIST
) {
3345 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "\nThis looks like a playlist, but "
3346 "playlist support will not be used automatically.\n"
3347 "MPlayer's playlist code is unsafe and should only be used with "
3348 "trusted sources.\nPlayback will probably fail.\n\n");
3352 current_module
="handle_playlist";
3353 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Parsing playlist %s...\n",
3354 filename_recode(mpctx
->filename
));
3355 entry
= parse_playtree(mpctx
->stream
, mpctx
->mconfig
, 0);
3356 mpctx
->eof
=playtree_add_playlist(mpctx
, entry
);
3357 goto goto_next_file
;
3360 mpctx
->stream
->start_pos
+=seek_to_byte
;
3362 if(stream_dump_type
==5){
3363 unsigned char buf
[4096];
3366 current_module
="dumpstream";
3367 if(mpctx
->stream
->type
==STREAMTYPE_STREAM
&& mpctx
->stream
->fd
<0){
3368 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"Cannot dump this stream - no file descriptor available.\n");
3369 exit_player(mpctx
, EXIT_ERROR
);
3371 stream_reset(mpctx
->stream
);
3372 stream_seek(mpctx
->stream
,mpctx
->stream
->start_pos
);
3373 f
=fopen(stream_dump_name
,"wb");
3375 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"Cannot open dump file.\n");
3376 exit_player(mpctx
, EXIT_ERROR
);
3378 if (dvd_chapter
> 1) {
3379 int chapter
= dvd_chapter
- 1;
3380 stream_control(mpctx
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
, &chapter
);
3382 while(!mpctx
->stream
->eof
&& !async_quit_request
){
3383 len
=stream_read(mpctx
->stream
,buf
,4096);
3385 if(fwrite(buf
,len
,1,f
) != 1) {
3386 mp_tmsg(MSGT_MENCODER
,MSGL_FATAL
,"%s: Error writing file.\n",stream_dump_name
);
3387 exit_player(mpctx
, EXIT_ERROR
);
3390 if(dvd_last_chapter
> 0) {
3392 if (stream_control(mpctx
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
3393 &chapter
) == STREAM_OK
&& chapter
+ 1 > dvd_last_chapter
)
3398 mp_tmsg(MSGT_MENCODER
,MSGL_FATAL
,"%s: Error writing file.\n",stream_dump_name
);
3399 exit_player(mpctx
, EXIT_ERROR
);
3401 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Core dumped ;)\n");
3402 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
3405 #ifdef CONFIG_DVDREAD
3406 if(mpctx
->stream
->type
==STREAMTYPE_DVD
){
3407 current_module
="dvd lang->id";
3408 if(opts
->audio_id
==-1) opts
->audio_id
=dvd_aid_from_lang(mpctx
->stream
,audio_lang
);
3409 if(dvdsub_lang
&& opts
->sub_id
==-2) opts
->sub_id
=-1;
3410 if(dvdsub_lang
&& opts
->sub_id
==-1) opts
->sub_id
=dvd_sid_from_lang(mpctx
->stream
,dvdsub_lang
);
3411 // setup global sub numbering
3412 mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] = mpctx
->global_sub_size
; // the global # of the first demux-specific sub.
3413 mpctx
->global_sub_size
+= dvd_number_of_subs(mpctx
->stream
);
3414 current_module
=NULL
;
3418 #ifdef CONFIG_DVDNAV
3419 if(mpctx
->stream
->type
==STREAMTYPE_DVDNAV
){
3420 current_module
="dvdnav lang->id";
3421 if(opts
->audio_id
==-1) opts
->audio_id
=mp_dvdnav_aid_from_lang(mpctx
->stream
,audio_lang
);
3422 if(dvdsub_lang
&& opts
->sub_id
==-2) opts
->sub_id
=-1;
3423 if(dvdsub_lang
&& opts
->sub_id
==-1) opts
->sub_id
=mp_dvdnav_sid_from_lang(mpctx
->stream
,dvdsub_lang
);
3424 // setup global sub numbering
3425 mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] = mpctx
->global_sub_size
; // the global # of the first demux-specific sub.
3426 mpctx
->global_sub_size
+= mp_dvdnav_number_of_subs(mpctx
->stream
);
3427 current_module
=NULL
;
3431 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3433 if(stream_cache_size
>0){
3434 current_module
="enable_cache";
3435 if(!stream_enable_cache(mpctx
->stream
,stream_cache_size
*1024,
3436 stream_cache_size
*1024*(stream_cache_min_percent
/ 100.0),
3437 stream_cache_size
*1024*(stream_cache_seek_min_percent
/ 100.0)))
3438 if((mpctx
->stop_play
= libmpdemux_was_interrupted(mpctx
, PT_NEXT_ENTRY
))) goto goto_next_file
;
3441 //============ Open DEMUXERS --- DETECT file type =======================
3442 current_module
="demux_open";
3444 mpctx
->demuxer
=demux_open(opts
, mpctx
->stream
,mpctx
->file_format
,opts
->audio_id
,opts
->video_id
,opts
->sub_id
,mpctx
->filename
);
3446 // HACK to get MOV Reference Files working
3448 if (mpctx
->demuxer
&& mpctx
->demuxer
->type
==DEMUXER_TYPE_PLAYLIST
)
3450 unsigned char* playlist_entry
;
3451 play_tree_t
*list
= NULL
, *entry
= NULL
;
3453 current_module
="handle_demux_playlist";
3454 while (ds_get_packet(mpctx
->demuxer
->video
,&playlist_entry
)>0)
3458 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Adding file %s to element entry.\n",
3459 filename_recode(playlist_entry
));
3461 bname
=mp_basename(playlist_entry
);
3462 if ((strlen(bname
)>10) && !strncmp(bname
,"qt",2) && !strncmp(bname
+3,"gateQT",6))
3465 if (!strncmp(bname
,mp_basename(mpctx
->filename
),strlen(bname
))) // ignoring self-reference
3468 entry
= play_tree_new();
3470 if (mpctx
->filename
&& !strcmp(mp_basename(playlist_entry
),playlist_entry
)) // add reference path of current file
3472 temp
=malloc((strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
))+strlen(playlist_entry
)+1));
3475 strncpy(temp
, mpctx
->filename
, strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
)));
3476 temp
[strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
))]='\0';
3477 strcat(temp
, playlist_entry
);
3478 play_tree_add_file(entry
,temp
);
3479 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Resolving reference to %s.\n",temp
);
3484 play_tree_add_file(entry
,playlist_entry
);
3489 play_tree_append_entry(list
,entry
);
3491 free_demuxer(mpctx
->demuxer
);
3492 mpctx
->demuxer
= NULL
;
3496 entry
= play_tree_new();
3497 play_tree_set_child(entry
,list
);
3498 mpctx
->stop_play
= playtree_add_playlist(mpctx
, entry
);
3499 goto goto_next_file
;
3504 goto goto_next_file
;
3506 if (mpctx
->demuxer
->matroska_data
.ordered_chapters
)
3507 build_ordered_chapter_timeline(mpctx
);
3509 if (!mpctx
->sources
) {
3510 mpctx
->sources
= talloc_ptrtype(NULL
, mpctx
->sources
);
3511 *mpctx
->sources
= (struct content_source
){.stream
= mpctx
->stream
,
3512 .demuxer
= mpctx
->demuxer
};
3513 mpctx
->num_sources
= 1;
3518 if (seek_chapter(mpctx
, dvd_chapter
-1, &pts
, NULL
) >= 0 && pts
> -1.0)
3519 seek(mpctx
, pts
, SEEK_ABSOLUTE
);
3522 mpctx
->initialized_flags
|=INITIALIZED_DEMUXER
;
3524 if (mpctx
->stream
->type
!= STREAMTYPE_DVD
&& mpctx
->stream
->type
!= STREAMTYPE_DVDNAV
) {
3527 // setup global sub numbering
3528 mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] = mpctx
->global_sub_size
; // the global # of the first demux-specific sub.
3529 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
3530 if (mpctx
->demuxer
->s_streams
[i
])
3531 maxid
= FFMAX(maxid
, mpctx
->demuxer
->s_streams
[i
]->sid
);
3532 mpctx
->global_sub_size
+= maxid
+ 1;
3534 // Make opts->sub_id always selectable if set.
3535 if (mpctx
->global_sub_size
<= mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] + opts
->sub_id
)
3536 mpctx
->global_sub_size
= mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] + opts
->sub_id
+ 1;
3539 if (ass_enabled
&& ass_library
) {
3540 for (int j
= 0; j
< mpctx
->num_sources
; j
++) {
3541 struct demuxer
*d
= mpctx
->sources
[j
].demuxer
;
3542 for (int i
= 0; i
< d
->num_attachments
; i
++) {
3543 struct demux_attachment
*att
= d
->attachments
+ i
;
3544 if (extract_embedded_fonts
3545 && att
->name
&& att
->type
&& att
->data
&& att
->data_size
3546 && (strcmp(att
->type
, "application/x-truetype-font") == 0
3547 || strcmp(att
->type
, "application/x-font") == 0))
3548 ass_add_font(ass_library
, att
->name
, att
->data
, att
->data_size
);
3554 current_module
="demux_open2";
3556 //file_format=demuxer->file_format;
3558 mpctx
->d_audio
=mpctx
->demuxer
->audio
;
3559 mpctx
->d_video
=mpctx
->demuxer
->video
;
3560 mpctx
->d_sub
=mpctx
->demuxer
->sub
;
3562 // select audio stream
3563 select_audio(mpctx
->demuxer
, opts
->audio_id
, audio_lang
);
3566 if((stream_dump_type
)&&(stream_dump_type
<4)){
3568 demux_stream_t
*ds
=NULL
;
3569 current_module
="dump";
3570 // select stream to dump
3571 switch(stream_dump_type
){
3572 case 1: ds
=mpctx
->d_audio
;break;
3573 case 2: ds
=mpctx
->d_video
;break;
3574 case 3: ds
=mpctx
->d_sub
;break;
3577 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"dump: FATAL: Selected stream missing!\n");
3578 exit_player(mpctx
, EXIT_ERROR
);
3580 // disable other streams:
3581 if(mpctx
->d_audio
&& mpctx
->d_audio
!=ds
) {ds_free_packs(mpctx
->d_audio
); mpctx
->d_audio
->id
=-2; }
3582 if(mpctx
->d_video
&& mpctx
->d_video
!=ds
) {ds_free_packs(mpctx
->d_video
); mpctx
->d_video
->id
=-2; }
3583 if(mpctx
->d_sub
&& mpctx
->d_sub
!=ds
) {ds_free_packs(mpctx
->d_sub
); mpctx
->d_sub
->id
=-2; }
3585 f
=fopen(stream_dump_name
,"wb");
3587 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"Cannot open dump file.\n");
3588 exit_player(mpctx
, EXIT_ERROR
);
3591 unsigned char* start
;
3592 int in_size
=ds_get_packet(ds
,&start
);
3593 if( (mpctx
->demuxer
->file_format
==DEMUXER_TYPE_AVI
|| mpctx
->demuxer
->file_format
==DEMUXER_TYPE_ASF
|| mpctx
->demuxer
->file_format
==DEMUXER_TYPE_MOV
)
3594 && stream_dump_type
==2) fwrite(&in_size
,1,4,f
);
3595 if(in_size
>0) fwrite(start
,in_size
,1,f
);
3596 if(dvd_last_chapter
>0) {
3597 int cur_chapter
= demuxer_get_current_chapter(mpctx
->demuxer
);
3598 if(cur_chapter
!=-1 && cur_chapter
+1>dvd_last_chapter
)
3603 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Core dumped ;)\n");
3604 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
3607 mpctx
->sh_audio
=mpctx
->d_audio
->sh
;
3608 mpctx
->sh_video
=mpctx
->d_video
->sh
;
3610 if(mpctx
->sh_video
){
3612 current_module
="video_read_properties";
3613 if(!video_read_properties(mpctx
->sh_video
)) {
3614 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Video: Cannot read properties.\n");
3615 mpctx
->sh_video
=mpctx
->d_video
->sh
=NULL
;
3617 mp_tmsg(MSGT_CPLAYER
,MSGL_V
,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
3618 mpctx
->demuxer
->file_format
,mpctx
->sh_video
->format
, mpctx
->sh_video
->disp_w
,mpctx
->sh_video
->disp_h
,
3619 mpctx
->sh_video
->fps
,mpctx
->sh_video
->frametime
3622 /* need to set fps here for output encoders to pick it up in their init */
3624 mpctx
->sh_video
->fps
=force_fps
;
3625 mpctx
->sh_video
->frametime
=1.0f
/mpctx
->sh_video
->fps
;
3627 vo_fps
= mpctx
->sh_video
->fps
;
3629 if(!mpctx
->sh_video
->fps
&& !force_fps
){
3630 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"FPS not specified in the header or invalid, use the -fps option.\n");
3631 mpctx
->sh_video
=mpctx
->d_video
->sh
=NULL
;
3637 if(!mpctx
->sh_video
&& !mpctx
->sh_audio
){
3638 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
, "No stream found.\n");
3640 if(mpctx
->stream
->type
== STREAMTYPE_DVB
)
3643 int v
= mpctx
->last_dvb_step
;
3645 dir
= DVB_CHANNEL_HIGHER
;
3647 dir
= DVB_CHANNEL_LOWER
;
3649 if(dvb_step_channel(mpctx
->stream
, dir
)) {
3650 mpctx
->stop_play
= PT_NEXT_ENTRY
;
3651 mpctx
->dvbin_reopen
= 1;
3655 goto goto_next_file
; // exit_player(_("Fatal error"));
3658 /* display clip info */
3659 demux_info_print(mpctx
->demuxer
);
3661 //================== Read SUBTITLES (DVD & TEXT) ==========================
3662 if(vo_spudec
==NULL
&& mpctx
->sh_video
&&
3663 (mpctx
->stream
->type
==STREAMTYPE_DVD
|| mpctx
->stream
->type
== STREAMTYPE_DVDNAV
)){
3664 init_vo_spudec(mpctx
);
3667 if(mpctx
->sh_video
) {
3668 // after reading video params we should load subtitles because
3669 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3671 current_module
="read_subtitles_file";
3673 for (i
= 0; sub_name
[i
] != NULL
; ++i
)
3674 add_subtitles(mpctx
, sub_name
[i
], mpctx
->sh_video
->fps
, 0);
3676 if(sub_auto
) { // auto load sub file ...
3677 char *psub
= get_path( "sub/" );
3678 char **tmp
= sub_filenames((psub
? psub
: ""), mpctx
->filename
);
3680 free(psub
); // release the buffer created by get_path() above
3682 add_subtitles(mpctx
, tmp
[i
], mpctx
->sh_video
->fps
, 1);
3687 if (mpctx
->set_of_sub_size
> 0) {
3688 // setup global sub numbering
3689 mpctx
->global_sub_indices
[SUB_SOURCE_SUBS
] = mpctx
->global_sub_size
; // the global # of the first sub.
3690 mpctx
->global_sub_size
+= mpctx
->set_of_sub_size
;
3694 if (mpctx
->global_sub_size
) {
3695 // find the best sub to use
3696 int vobsub_index_id
= vobsub_get_index_by_id(vo_vobsub
, vobsub_id
);
3697 mpctx
->global_sub_pos
= -1; // no subs by default
3698 if (vobsub_index_id
>= 0) {
3699 // if user asks for a vobsub id, use that first.
3700 mpctx
->global_sub_pos
= mpctx
->global_sub_indices
[SUB_SOURCE_VOBSUB
] + vobsub_index_id
;
3701 } else if (opts
->sub_id
>= 0 && mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] >= 0) {
3702 // if user asks for a dvd sub id, use that next.
3703 mpctx
->global_sub_pos
= mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] + opts
->sub_id
;
3704 } else if (mpctx
->global_sub_indices
[SUB_SOURCE_SUBS
] >= 0) {
3705 // if there are text subs to use, use those. (autosubs come last here)
3706 mpctx
->global_sub_pos
= mpctx
->global_sub_indices
[SUB_SOURCE_SUBS
];
3707 } else if (opts
->sub_id
< 0 && mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] >= 0) {
3708 // finally select subs by language and container hints
3709 if (opts
->sub_id
< 0 && dvdsub_lang
)
3710 opts
->sub_id
= demuxer_sub_track_by_lang(mpctx
->demuxer
, dvdsub_lang
);
3711 if (opts
->sub_id
< 0)
3712 opts
->sub_id
= demuxer_default_sub_track(mpctx
->demuxer
);
3713 if (opts
->sub_id
>= 0)
3714 mpctx
->global_sub_pos
= mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] + opts
->sub_id
;
3716 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3717 mpctx
->global_sub_pos
--;
3718 mp_property_do("sub",M_PROPERTY_STEP_UP
,NULL
, mpctx
);
3720 switch (stream_dump_type
) {
3721 case 3: list_sub_file(subdata
); break;
3722 case 4: dump_mpsub(subdata
, mpctx
->sh_video
->fps
); break;
3723 case 6: dump_srt(subdata
, mpctx
->sh_video
->fps
); break;
3724 case 7: dump_microdvd(subdata
, mpctx
->sh_video
->fps
); break;
3725 case 8: dump_jacosub(subdata
, mpctx
->sh_video
->fps
); break;
3726 case 9: dump_sami(subdata
, mpctx
->sh_video
->fps
); break;
3730 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_FILENAME=%s\n",
3731 filename_recode(mpctx
->filename
));
3732 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_DEMUXER=%s\n", mpctx
->demuxer
->desc
->name
);
3733 if (mpctx
->sh_video
) {
3734 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3735 if (mpctx
->sh_video
->format
>= 0x20202020)
3736 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx
->sh_video
->format
);
3738 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FORMAT=0x%08X\n", mpctx
->sh_video
->format
);
3739 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_BITRATE=%d\n", mpctx
->sh_video
->i_bps
*8);
3740 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_WIDTH=%d\n", mpctx
->sh_video
->disp_w
);
3741 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_HEIGHT=%d\n", mpctx
->sh_video
->disp_h
);
3742 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FPS=%5.3f\n", mpctx
->sh_video
->fps
);
3743 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_ASPECT=%1.4f\n", mpctx
->sh_video
->aspect
);
3745 if (mpctx
->sh_audio
) {
3746 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3747 if (mpctx
->sh_audio
->format
>= 0x20202020)
3748 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx
->sh_audio
->format
);
3750 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_FORMAT=%d\n", mpctx
->sh_audio
->format
);
3751 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_BITRATE=%d\n", mpctx
->sh_audio
->i_bps
*8);
3752 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_RATE=%d\n", mpctx
->sh_audio
->samplerate
);
3753 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_NCH=%d\n", mpctx
->sh_audio
->channels
);
3755 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_LENGTH=%.2f\n", demuxer_get_time_length(mpctx
->demuxer
));
3756 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_SEEKABLE=%d\n",
3757 mpctx
->stream
->seek
&& (!mpctx
->demuxer
|| mpctx
->demuxer
->seekable
));
3758 if (mpctx
->demuxer
) {
3759 if (mpctx
->demuxer
->num_chapters
== 0)
3760 stream_control(mpctx
->demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
, &mpctx
->demuxer
->num_chapters
);
3761 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_CHAPTERS=%d\n", mpctx
->demuxer
->num_chapters
);
3764 if(!mpctx
->sh_video
) goto main
; // audio-only
3766 if(!reinit_video_chain(mpctx
)) {
3767 if(!mpctx
->sh_video
){
3768 if(!mpctx
->sh_audio
) goto goto_next_file
;
3769 goto main
; // exit_player(_("Fatal error"));
3773 if(mpctx
->sh_video
->output_flags
& 0x08 && vo_spudec
)
3774 spudec_set_hw_spu(vo_spudec
,mpctx
->video_out
);
3776 #ifdef CONFIG_FREETYPE
3777 force_load_font
= 1;
3780 //================== MAIN: ==========================
3782 current_module
="main";
3785 char* msg
= property_expand_string(mpctx
, playing_msg
);
3786 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"%s",msg
);
3791 // Disable the term OSD in verbose mode
3792 if(verbose
) term_osd
= 0;
3795 //int frame_corr_num=0; //
3796 //float v_frame=0; // Video
3797 //float num_frames=0; // number of frames played
3799 int frame_time_remaining
=0; // flag
3801 mpctx
->num_buffered_frames
=0;
3803 // Make sure old OSD does not stay around,
3804 // e.g. with -fixed-vo and same-resolution files
3806 update_osd_msg(mpctx
);
3808 //================ SETUP AUDIO ==========================
3810 if(mpctx
->sh_audio
){
3811 reinit_audio_chain(mpctx
);
3812 if (mpctx
->sh_audio
&& mpctx
->sh_audio
->codec
)
3813 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
, "ID_AUDIO_CODEC=%s\n", mpctx
->sh_audio
->codec
->name
);
3816 current_module
="av_init";
3818 if(mpctx
->sh_video
){
3819 mpctx
->sh_video
->timer
=0;
3821 audio_delay
+= mpctx
->sh_video
->stream_delay
;
3823 if(mpctx
->sh_audio
){
3824 if (start_volume
>= 0)
3825 mixer_setvolume(&mpctx
->mixer
, start_volume
, start_volume
);
3827 audio_delay
-= mpctx
->sh_audio
->stream_delay
;
3828 mpctx
->delay
=-audio_delay
;
3831 if(!mpctx
->sh_audio
){
3832 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Audio: no sound\n");
3833 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Freeing %d unused audio chunks.\n",mpctx
->d_audio
->packs
);
3834 ds_free_packs(mpctx
->d_audio
); // free buffered chunks
3835 //mpctx->d_audio->id=-2; // do not read audio chunks
3836 //uninit_player(mpctx, INITIALIZED_AO); // close device
3838 if(!mpctx
->sh_video
){
3839 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Video: no video\n");
3840 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Freeing %d unused video chunks.\n",mpctx
->d_video
->packs
);
3841 ds_free_packs(mpctx
->d_video
);
3842 mpctx
->d_video
->id
=-2;
3843 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3846 if (!mpctx
->sh_video
&& !mpctx
->sh_audio
)
3847 goto goto_next_file
;
3849 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3850 if(force_fps
&& mpctx
->sh_video
){
3851 vo_fps
= mpctx
->sh_video
->fps
=force_fps
;
3852 mpctx
->sh_video
->frametime
=1.0f
/mpctx
->sh_video
->fps
;
3853 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx
->sh_video
->fps
,mpctx
->sh_video
->frametime
);
3856 mp_input_set_section(mpctx
->input
, NULL
);
3857 //TODO: add desired (stream-based) sections here
3858 if (mpctx
->stream
->type
==STREAMTYPE_TV
) mp_input_set_section(mpctx
->input
, "tv");
3859 if (mpctx
->stream
->type
==STREAMTYPE_DVDNAV
) mp_input_set_section(mpctx
->input
, "dvdnav");
3861 //==================== START PLAYING =======================
3863 if(opts
->loop_times
>1) opts
->loop_times
--; else
3864 if(opts
->loop_times
==1) opts
->loop_times
= -1;
3866 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Starting playback...\n");
3868 total_time_usage_start
=GetTimer();
3869 audio_time_usage
=0; video_time_usage
=0; vout_time_usage
=0;
3870 total_frame_cnt
=0; drop_frame_cnt
=0; // fix for multifile fps benchmark
3871 play_n_frames
=play_n_frames_mf
;
3873 if(play_n_frames
==0){
3874 mpctx
->stop_play
=PT_NEXT_ENTRY
; goto goto_next_file
;
3878 seek(mpctx
, seek_to_sec
, SEEK_ABSOLUTE
);
3879 end_at
.pos
+= seek_to_sec
;
3882 if (end_at
.type
== END_AT_SIZE
) {
3883 mp_tmsg(MSGT_CPLAYER
, MSGL_WARN
, "Option -endpos in MPlayer does not yet support size units.\n");
3884 end_at
.type
= END_AT_NONE
;
3887 #ifdef CONFIG_DVDNAV
3888 mp_dvdnav_context_free(mpctx
);
3889 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
3890 mp_dvdnav_read_wait(mpctx
->stream
, 0, 1);
3891 mp_dvdnav_cell_has_changed(mpctx
->stream
,1);
3895 get_relative_time(mpctx
); // reset current delta
3896 mpctx
->time_frame
= 0;
3897 mpctx
->drop_message_shown
= 0;
3898 mpctx
->update_video_immediately
= true;
3899 mpctx
->total_avsync_change
= 0;
3900 // Make sure VO knows current pause state
3901 if (mpctx
->sh_video
)
3902 vo_control(mpctx
->video_out
, mpctx
->paused
? VOCTRL_PAUSE
: VOCTRL_RESUME
,
3905 while(!mpctx
->stop_play
){
3906 float aq_sleep_time
=0;
3908 if(dvd_last_chapter
>0) {
3909 int cur_chapter
= get_current_chapter(mpctx
);
3910 if(cur_chapter
!=-1 && cur_chapter
+1>dvd_last_chapter
)
3911 goto goto_next_file
;
3914 if(!mpctx
->sh_audio
&& mpctx
->d_audio
->sh
) {
3915 mpctx
->sh_audio
= mpctx
->d_audio
->sh
;
3916 mpctx
->sh_audio
->ds
= mpctx
->d_audio
;
3917 reinit_audio_chain(mpctx
);
3920 /*========================== PLAY AUDIO ============================*/
3922 if (mpctx
->sh_audio
&& !mpctx
->paused
)
3923 if (!fill_audio_out_buffers(mpctx
))
3924 // at eof, all audio at least written to ao
3925 if (!mpctx
->sh_video
)
3926 mpctx
->stop_play
= AT_END_OF_FILE
;
3929 if(!mpctx
->sh_video
) {
3930 // handle audio-only case:
3932 // sh_audio can be NULL due to video stream switching
3933 // TODO: handle this better
3934 if (mpctx
->sh_audio
)
3935 a_pos
= playing_audio_pts(mpctx
);
3937 print_status(mpctx
, a_pos
, false);
3939 if(end_at
.type
== END_AT_TIME
&& end_at
.pos
< a_pos
)
3940 mpctx
->stop_play
= PT_NEXT_ENTRY
;
3941 update_osd_msg(mpctx
);
3945 /*========================== PLAY VIDEO ============================*/
3947 vo_pts
=mpctx
->sh_video
->timer
*90000.0;
3948 vo_fps
=mpctx
->sh_video
->fps
;
3950 if (!mpctx
->num_buffered_frames
) {
3951 double frame_time
= update_video(mpctx
, &blit_frame
);
3952 mp_dbg(MSGT_AVSYNC
,MSGL_DBG2
,"*** ftime=%5.3f ***\n",frame_time
);
3953 if (mpctx
->sh_video
->vf_initialized
< 0) {
3954 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
, "\nFATAL: Could not initialize video filters (-vf) or video output (-vo).\n");
3955 mpctx
->stop_play
= PT_NEXT_ENTRY
;
3956 goto goto_next_file
;
3959 struct vf_instance
*vf
= mpctx
->sh_video
->vfilter
;
3960 vf
->control(vf
, VFCTRL_DRAW_EOSD
, NULL
);
3961 vf
->control(vf
, VFCTRL_DRAW_OSD
, mpctx
->osd
);
3965 mpctx
->stop_play
= AT_END_OF_FILE
;
3967 // might return with !eof && !blit_frame if !correct_pts
3968 mpctx
->num_buffered_frames
+= blit_frame
;
3969 if (mpctx
->update_video_immediately
) {
3970 // Show this frame immediately, rest normally
3971 mpctx
->update_video_immediately
= false;
3973 mpctx
->time_frame
+= frame_time
/ opts
->playback_speed
;
3974 adjust_sync(mpctx
, frame_time
);
3978 if (mpctx
->timeline
) {
3979 struct timeline_part
*next
= mpctx
->timeline
+ mpctx
->timeline_part
+ 1;
3980 if (mpctx
->sh_video
->pts
>= next
->start
3981 || mpctx
->stop_play
== AT_END_OF_FILE
3982 && mpctx
->timeline_part
+ 1 < mpctx
->num_timeline_parts
) {
3983 seek(mpctx
, next
->start
, SEEK_ABSOLUTE
);
3988 // ==========================================================================
3990 // current_module="draw_osd";
3991 // if(vo_config_count) mpctx->video_out->draw_osd();
3993 current_module
="vo_check_events";
3994 vo_check_events(mpctx
->video_out
);
3997 if (stop_xscreensaver
) {
3998 current_module
= "stop_xscreensaver";
3999 xscreensaver_heartbeat(mpctx
->x11_state
);
4002 if (heartbeat_cmd
) {
4003 static unsigned last_heartbeat
;
4004 unsigned now
= GetTimerMS();
4005 if (now
- last_heartbeat
> 30000) {
4006 last_heartbeat
= now
;
4007 system(heartbeat_cmd
);
4011 frame_time_remaining
= sleep_until_update(mpctx
, &mpctx
->time_frame
, &aq_sleep_time
);
4013 //====================== FLIP PAGE (VIDEO BLT): =========================
4015 current_module
="flip_page";
4016 if (!frame_time_remaining
&& blit_frame
) {
4017 unsigned int t2
=GetTimer();
4019 vo_flip_page(mpctx
->video_out
);
4020 mpctx
->num_buffered_frames
--;
4022 mpctx
->last_vo_flip_duration
= (GetTimer() - t2
) * 0.000001;
4023 vout_time_usage
+= mpctx
->last_vo_flip_duration
;
4024 print_status(mpctx
, MP_NOPTS_VALUE
, true);
4027 print_status(mpctx
, MP_NOPTS_VALUE
, false);
4029 //============================ Auto QUALITY ============================
4031 /*Output quality adjustments:*/
4033 current_module
="autoq";
4034 // float total=0.000001f * (GetTimer()-aq_total_time);
4035 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4036 if(output_quality
<auto_quality
&& aq_sleep_time
>0)
4039 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4040 if(output_quality
>1 && aq_sleep_time
<0)
4043 if(output_quality
>0 && aq_sleep_time
<-0.050f
) // 50ms
4045 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4046 set_video_quality(mpctx
->sh_video
,output_quality
);
4049 if (!frame_time_remaining
&& blit_frame
) {
4050 if (play_n_frames
>= 0) {
4052 if (play_n_frames
<= 0)
4053 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4055 if (mpctx
->step_frames
> 0) {
4056 mpctx
->step_frames
--;
4057 if (mpctx
->step_frames
== 0)
4058 pause_player(mpctx
);
4063 // FIXME: add size based support for -endpos
4064 if (end_at
.type
== END_AT_TIME
&&
4065 !frame_time_remaining
&& end_at
.pos
<= mpctx
->sh_video
->pts
)
4066 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4068 } // end if(mpctx->sh_video)
4070 #ifdef CONFIG_DVDNAV
4071 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
4073 mp_dvdnav_get_highlight (mpctx
->stream
, &hl
);
4074 osd_set_nav_box (hl
.sx
, hl
.sy
, hl
.ex
, hl
.ey
);
4075 vo_osd_changed (OSDTYPE_DVDNAV
);
4077 if (mp_dvdnav_stream_has_changed(mpctx
->stream
)) {
4079 if (stream_control (mpctx
->demuxer
->stream
,
4080 STREAM_CTRL_GET_ASPECT_RATIO
, &ar
)
4081 != STREAM_UNSUPPORTED
)
4082 mpctx
->sh_video
->stream_aspect
= ar
;
4087 //================= Keyboard events, SEEKing ====================
4089 current_module
="key_events";
4094 while ((cmd
= mp_input_get_cmd(mpctx
->input
, 0,0,0)) != NULL
) {
4095 run_command(mpctx
, cmd
);
4097 if (mpctx
->stop_play
)
4100 if (!mpctx
->paused
|| mpctx
->stop_play
|| mpctx
->rel_seek_secs
4101 || mpctx
->abs_seek_pos
)
4103 if (mpctx
->sh_video
) {
4104 update_osd_msg(mpctx
);
4105 int hack
= vo_osd_changed(0);
4106 vo_osd_changed(hack
);
4108 if (redraw_osd(mpctx
->sh_video
, mpctx
->osd
) < 0) {
4109 add_step_frame(mpctx
);
4120 if (step_sec
> 0 && !mpctx
->paused
) {
4121 mpctx
->osd_function
=OSD_FFW
;
4122 mpctx
->rel_seek_secs
+=step_sec
;
4128 if(mpctx
->stop_play
==AT_END_OF_FILE
&& opts
->loop_times
>=0) {
4129 mp_msg(MSGT_CPLAYER
,MSGL_V
,"loop_times = %d\n", opts
->loop_times
);
4131 if(opts
->loop_times
>1) opts
->loop_times
--; else
4132 if(opts
->loop_times
==1) opts
->loop_times
=-1;
4133 play_n_frames
=play_n_frames_mf
;
4135 mpctx
->abs_seek_pos
=SEEK_ABSOLUTE
; mpctx
->rel_seek_secs
=seek_to_sec
;
4138 if(mpctx
->rel_seek_secs
|| mpctx
->abs_seek_pos
){
4139 seek(mpctx
, mpctx
->rel_seek_secs
, mpctx
->abs_seek_pos
);
4141 mpctx
->rel_seek_secs
=0;
4142 mpctx
->abs_seek_pos
=0;
4145 } // while(!mpctx->stop_play)
4147 mp_msg(MSGT_GLOBAL
,MSGL_V
,"EOF code: %d \n",mpctx
->stop_play
);
4150 if(mpctx
->dvbin_reopen
)
4152 mpctx
->stop_play
= 0;
4153 uninit_player(mpctx
, INITIALIZED_ALL
-(INITIALIZED_STREAM
|INITIALIZED_GETCH2
|(opts
->fixed_vo
?INITIALIZED_VO
:0)));
4154 cache_uninit(mpctx
->stream
);
4155 mpctx
->dvbin_reopen
= 0;
4156 goto goto_enable_cache
;
4161 goto_next_file
: // don't jump here after ao/vo/getch initialization!
4163 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"\n");
4166 double tot
=video_time_usage
+vout_time_usage
+audio_time_usage
;
4167 double total_time_usage
;
4168 total_time_usage_start
=GetTimer()-total_time_usage_start
;
4169 total_time_usage
= (float)total_time_usage_start
*0.000001;
4170 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4171 video_time_usage
,vout_time_usage
,audio_time_usage
,
4172 total_time_usage
-tot
,total_time_usage
);
4173 if(total_time_usage
>0.0)
4174 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4175 100.0*video_time_usage
/total_time_usage
,
4176 100.0*vout_time_usage
/total_time_usage
,
4177 100.0*audio_time_usage
/total_time_usage
,
4178 100.0*(total_time_usage
-tot
)/total_time_usage
,
4180 if(total_frame_cnt
&& frame_dropping
)
4181 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4182 total_frame_cnt
-drop_frame_cnt
,
4183 (total_time_usage
>0.5)?((total_frame_cnt
-drop_frame_cnt
)/total_time_usage
):0,
4185 100*drop_frame_cnt
/total_frame_cnt
,
4187 (total_time_usage
>0.5)?(total_frame_cnt
/total_time_usage
):0);
4190 // time to uninit all, except global stuff:
4191 uninit_player(mpctx
, INITIALIZED_ALL
-(opts
->fixed_vo
?INITIALIZED_VO
:0));
4193 if(mpctx
->set_of_sub_size
> 0) {
4194 current_module
="sub_free";
4195 for(i
= 0; i
< mpctx
->set_of_sub_size
; ++i
) {
4196 sub_free(mpctx
->set_of_subtitles
[i
]);
4198 if(mpctx
->set_of_ass_tracks
[i
])
4199 ass_free_track( mpctx
->set_of_ass_tracks
[i
] );
4202 mpctx
->set_of_sub_size
= 0;
4204 vo_sub_last
= vo_sub
=NULL
;
4209 ass_clear_fonts(ass_library
);
4212 if (!mpctx
->stop_play
) // In case some goto jumped here...
4213 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4215 int playtree_direction
= 1;
4217 if(mpctx
->stop_play
== PT_NEXT_ENTRY
|| mpctx
->stop_play
== PT_PREV_ENTRY
) {
4218 if(play_tree_iter_step(mpctx
->playtree_iter
,mpctx
->play_tree_step
,0) != PLAY_TREE_ITER_ENTRY
) {
4219 play_tree_iter_free(mpctx
->playtree_iter
);
4220 mpctx
->playtree_iter
= NULL
;
4222 mpctx
->play_tree_step
= 1;
4223 } else if(mpctx
->stop_play
== PT_UP_NEXT
|| mpctx
->stop_play
== PT_UP_PREV
) {
4224 int direction
= mpctx
->stop_play
== PT_UP_NEXT
? 1 : -1;
4225 if(mpctx
->playtree_iter
) {
4226 if(play_tree_iter_up_step(mpctx
->playtree_iter
,direction
,0) != PLAY_TREE_ITER_ENTRY
) {
4227 play_tree_iter_free(mpctx
->playtree_iter
);
4228 mpctx
->playtree_iter
= NULL
;
4231 } else if (mpctx
->stop_play
== PT_STOP
) {
4232 play_tree_iter_free(mpctx
->playtree_iter
);
4233 mpctx
->playtree_iter
= NULL
;
4234 } else { // NEXT PREV SRC
4235 playtree_direction
= mpctx
->stop_play
== PT_PREV_SRC
? -1 : 1;
4238 while(mpctx
->playtree_iter
!= NULL
) {
4239 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
, playtree_direction
);
4240 if(mpctx
->filename
== NULL
) {
4241 if(play_tree_iter_step(mpctx
->playtree_iter
, playtree_direction
, 0) != PLAY_TREE_ITER_ENTRY
) {
4242 play_tree_iter_free(mpctx
->playtree_iter
);
4243 mpctx
->playtree_iter
= NULL
;
4249 if(mpctx
->playtree_iter
!= NULL
|| player_idle_mode
){
4250 if(!mpctx
->playtree_iter
) mpctx
->filename
= NULL
;
4251 mpctx
->stop_play
= 0;
4252 goto play_next_file
;
4256 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
4260 #endif /* DISABLE_MAIN */