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"
82 #include "gui/interface.h"
85 #include "input/input.h"
87 const int under_mencoder
= 0;
89 int player_idle_mode
=0;
91 int enable_mouse_movements
=0;
92 float start_volume
= -1;
94 #include "osdep/priority.h"
98 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
102 #include <linux/rtc.h>
105 #define RTC_IRQP_SET RTCIO_IRQP_SET
106 #define RTC_PIE_ON RTCIO_PIE_ON
107 #endif /* __linux__ */
108 #endif /* HAVE_RTC */
111 #include "stream/tv.h"
114 #include "stream/stream_radio.h"
118 #include "stream/dvbin.h"
119 #include "stream/cache2.h"
122 //**************************************************************************//
124 //**************************************************************************//
125 #include "playtree.h"
126 #include "playtreeparser.h"
128 int import_playtree_playlist_into_gui(play_tree_t
* my_playtree
, m_config_t
* config
);
129 int import_initial_playtree_into_gui(play_tree_t
* my_playtree
, m_config_t
* config
, int enqueue
);
131 //**************************************************************************//
133 //**************************************************************************//
134 #include "parser-cfg.h"
135 #include "parser-mpcmd.h"
137 //**************************************************************************//
139 //**************************************************************************//
141 static int cfg_inc_verbose(m_option_t
*conf
){ ++verbose
; return 0;}
143 #include "get_path.h"
145 //**************************************************************************//
146 //**************************************************************************//
147 // Input media streaming & demultiplexer:
148 //**************************************************************************//
150 static int max_framesize
=0;
152 #include "stream/stream.h"
153 #include "libmpdemux/demuxer.h"
154 #include "libmpdemux/stheader.h"
156 #ifdef CONFIG_DVDREAD
157 #include "stream/stream_dvd.h"
161 #include "stream/stream_dvdnav.h"
164 #include "libmpcodecs/dec_audio.h"
165 #include "libmpcodecs/dec_video.h"
166 #include "libmpcodecs/mp_image.h"
167 #include "libmpcodecs/vf.h"
168 #include "libmpcodecs/vd.h"
174 #include "defaultopts.h"
176 //**************************************************************************//
177 //**************************************************************************//
179 // Common FIFO functions, and keyboard/event FIFO code
181 int noconsolecontrols
=0;
182 //**************************************************************************//
185 double video_time_usage
=0;
186 double vout_time_usage
=0;
187 static double audio_time_usage
=0;
188 static int total_time_usage_start
=0;
189 static int total_frame_cnt
=0;
190 static int drop_frame_cnt
=0; // total number of dropped frames
195 static int output_quality
=0;
203 static int list_properties
= 0;
206 static char* term_osd_esc
= "\x1b[A\r\x1b[K";
207 static char* playing_msg
= NULL
;
209 static double seek_to_sec
;
210 static off_t seek_to_byte
=0;
211 static off_t step_sec
=0;
213 static m_time_size_t end_at
= { .type
= END_AT_NONE
, .pos
= 0 };
216 int autosync
=0; // 30 might be a good default value.
219 char **audio_codec_list
=NULL
; // override audio codec
220 char **video_codec_list
=NULL
; // override video codec
221 char **audio_fm_list
=NULL
; // override audio codec family
222 char **video_fm_list
=NULL
; // override video codec family
225 extern char *demuxer_name
; // override demuxer
226 extern char *audio_demuxer_name
; // override audio demuxer
227 extern char *sub_demuxer_name
; // override sub demuxer
230 char* audio_lang
=NULL
;
231 char* dvdsub_lang
=NULL
;
232 static char* spudec_ifo
=NULL
;
233 int forced_subs_only
=0;
237 int stream_cache_size
=-1;
238 #ifdef CONFIG_STREAM_CACHE
239 extern int cache_fill_status
;
241 float stream_cache_min_percent
=20.0;
242 float stream_cache_seek_min_percent
=50.0;
244 #define cache_fill_status 0
248 static char *stream_dump_name
="stream.dump";
249 int stream_dump_type
=0;
252 static float default_max_pts_correction
=-1;//0.01f;
254 static int ignore_start
=0;
256 static int softsleep
=0;
259 static int force_srate
=0;
260 static int audio_output_format
=-1; // AF_FORMAT_UNKNOWN
261 int frame_dropping
=0; // option 0=no drop 1= drop vo 2= drop decode
262 static int play_n_frames
=-1;
263 static int play_n_frames_mf
=-1;
266 char *font_name
=NULL
;
267 char *sub_font_name
=NULL
;
268 extern int font_fontconfig
;
269 float font_factor
=0.75;
270 char **sub_name
=NULL
;
274 char *vobsub_name
=NULL
;
275 /*DSP!!char *dsp=NULL;*/
277 int suboverlap_enabled
= 1;
280 #include "libass/ass.h"
281 #include "libass/ass_mp.h"
284 char* current_module
=NULL
; // for debugging
290 #include "m_struct.h"
291 #include "libmenu/menu.h"
292 void vf_menu_pause_update(struct vf_instance
* vf
);
293 extern vf_info_t vf_info_menu
;
294 static const vf_info_t
* const libmenu_vfs
[] = {
298 static vf_instance_t
* vf_menu
= NULL
;
300 static char* menu_cfg
= NULL
;
301 static char* menu_root
= "main";
306 static int nortc
= 1;
307 static char* rtc_device
;
310 edl_record_ptr edl_records
= NULL
; ///< EDL entries memory area
311 edl_record_ptr next_edl_record
= NULL
; ///< only for traversing edl_records
312 FILE* edl_fd
= NULL
; ///< fd to write to when in -edlout mode.
313 int use_filedir_conf
;
315 #include "mpcommon.h"
318 #include "metadata.h"
320 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
322 const void *mpctx_get_video_out(MPContext
*mpctx
)
324 return mpctx
->video_out
;
327 const void *mpctx_get_audio_out(MPContext
*mpctx
)
329 return mpctx
->audio_out
;
332 void *mpctx_get_demuxer(MPContext
*mpctx
)
334 return mpctx
->demuxer
;
337 void *mpctx_get_playtree_iter(MPContext
*mpctx
)
339 return mpctx
->playtree_iter
;
342 void *mpctx_get_mixer(MPContext
*mpctx
)
344 return &mpctx
->mixer
;
347 int mpctx_get_global_sub_size(MPContext
*mpctx
)
349 return mpctx
->global_sub_size
;
352 int mpctx_get_osd_function(MPContext
*mpctx
)
354 return mpctx
->osd_function
;
357 static float get_relative_time(struct MPContext
*mpctx
)
359 unsigned int new_time
= GetTimer();
360 unsigned int delta
= new_time
- mpctx
->last_time
;
361 mpctx
->last_time
= new_time
;
362 return delta
* 0.000001;
365 static int is_valid_metadata_type(struct MPContext
*mpctx
, metadata_t type
) {
368 /* check for valid video stream */
369 case META_VIDEO_CODEC
:
370 case META_VIDEO_BITRATE
:
371 case META_VIDEO_RESOLUTION
:
373 if (!mpctx
->sh_video
)
378 /* check for valid audio stream */
379 case META_AUDIO_CODEC
:
380 case META_AUDIO_BITRATE
:
381 case META_AUDIO_SAMPLES
:
383 if (!mpctx
->sh_audio
)
388 /* check for valid demuxer */
389 case META_INFO_TITLE
:
390 case META_INFO_ARTIST
:
391 case META_INFO_ALBUM
:
393 case META_INFO_COMMENT
:
394 case META_INFO_TRACK
:
395 case META_INFO_GENRE
:
409 static char *get_demuxer_info(struct MPContext
*mpctx
, char *tag
) {
410 char **info
= mpctx
->demuxer
->info
;
416 for (n
= 0; info
[2*n
] != NULL
; n
++)
417 if (!strcmp (info
[2*n
], tag
))
420 return info
[2*n
+1] ? strdup (info
[2*n
+1]) : NULL
;
423 char *get_metadata(struct MPContext
*mpctx
, metadata_t type
)
426 sh_audio_t
* const sh_audio
= mpctx
->sh_audio
;
427 sh_video_t
* const sh_video
= mpctx
->sh_video
;
429 if (!is_valid_metadata_type(mpctx
, type
))
436 return strdup (mp_basename2 (mpctx
->filename
));
439 case META_VIDEO_CODEC
:
441 if (sh_video
->format
== 0x10000001)
442 meta
= strdup ("mpeg1");
443 else if (sh_video
->format
== 0x10000002)
444 meta
= strdup ("mpeg2");
445 else if (sh_video
->format
== 0x10000004)
446 meta
= strdup ("mpeg4");
447 else if (sh_video
->format
== 0x10000005)
448 meta
= strdup ("h264");
449 else if (sh_video
->format
>= 0x20202020)
452 sprintf (meta
, "%.4s", (char *) &sh_video
->format
);
457 sprintf (meta
, "0x%08X", sh_video
->format
);
462 case META_VIDEO_BITRATE
:
465 sprintf (meta
, "%d kbps", (int) (sh_video
->i_bps
* 8 / 1024));
469 case META_VIDEO_RESOLUTION
:
472 sprintf (meta
, "%d x %d", sh_video
->disp_w
, sh_video
->disp_h
);
476 case META_AUDIO_CODEC
:
478 if (sh_audio
->codec
&& sh_audio
->codec
->name
)
479 meta
= strdup (sh_audio
->codec
->name
);
483 case META_AUDIO_BITRATE
:
486 sprintf (meta
, "%d kbps", (int) (sh_audio
->i_bps
* 8/1000));
490 case META_AUDIO_SAMPLES
:
493 sprintf (meta
, "%d Hz, %d ch.", sh_audio
->samplerate
, sh_audio
->channels
);
497 /* check for valid demuxer */
498 case META_INFO_TITLE
:
499 return get_demuxer_info(mpctx
, "Title");
501 case META_INFO_ARTIST
:
502 return get_demuxer_info(mpctx
, "Artist");
504 case META_INFO_ALBUM
:
505 return get_demuxer_info(mpctx
, "Album");
508 return get_demuxer_info(mpctx
, "Year");
510 case META_INFO_COMMENT
:
511 return get_demuxer_info(mpctx
, "Comment");
513 case META_INFO_TRACK
:
514 return get_demuxer_info(mpctx
, "Track");
516 case META_INFO_GENRE
:
517 return get_demuxer_info(mpctx
, "Genre");
526 /// step size of mixer changes
530 static void mp_dvdnav_context_free(MPContext
*ctx
){
531 if (ctx
->nav_smpi
) free_mp_image(ctx
->nav_smpi
);
532 ctx
->nav_smpi
= NULL
;
533 if (ctx
->nav_buffer
) free(ctx
->nav_buffer
);
534 ctx
->nav_buffer
= NULL
;
535 ctx
->nav_start
= NULL
;
536 ctx
->nav_in_size
= 0;
540 void uninit_player(struct MPContext
*mpctx
, unsigned int mask
){
541 mask
=mpctx
->initialized_flags
&mask
;
543 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n*** uninit(0x%X)\n",mask
);
545 if(mask
&INITIALIZED_ACODEC
){
546 mpctx
->initialized_flags
&=~INITIALIZED_ACODEC
;
547 current_module
="uninit_acodec";
548 if(mpctx
->sh_audio
) uninit_audio(mpctx
->sh_audio
);
550 if (use_gui
) guiGetEvent(guiSetAfilter
, (char *)NULL
);
552 mpctx
->sh_audio
=NULL
;
553 mpctx
->mixer
.afilter
= NULL
;
556 if(mask
&INITIALIZED_VCODEC
){
557 mpctx
->initialized_flags
&=~INITIALIZED_VCODEC
;
558 current_module
="uninit_vcodec";
559 if(mpctx
->sh_video
) uninit_video(mpctx
->sh_video
);
560 mpctx
->sh_video
=NULL
;
566 if(mask
&INITIALIZED_DEMUXER
){
567 mpctx
->initialized_flags
&=~INITIALIZED_DEMUXER
;
568 current_module
="free_demuxer";
569 if (mpctx
->num_sources
) {
570 mpctx
->demuxer
= mpctx
->sources
[0].demuxer
;
571 for (int i
= 1; i
< mpctx
->num_sources
; i
++) {
572 free_stream(mpctx
->sources
[i
].stream
);
573 free_demuxer(mpctx
->sources
[i
].demuxer
);
576 talloc_free(mpctx
->sources
);
577 mpctx
->sources
= NULL
;
578 mpctx
->num_sources
= 0;
579 talloc_free(mpctx
->timeline
);
580 mpctx
->timeline
= NULL
;
581 mpctx
->num_timeline_parts
= 0;
582 talloc_free(mpctx
->chapters
);
583 mpctx
->num_chapters
= 0;
584 mpctx
->video_offset
= 0;
586 mpctx
->stream
=mpctx
->demuxer
->stream
;
587 free_demuxer(mpctx
->demuxer
);
592 // kill the cache process:
593 if(mask
&INITIALIZED_STREAM
){
594 mpctx
->initialized_flags
&=~INITIALIZED_STREAM
;
595 current_module
="uninit_stream";
596 if(mpctx
->stream
) free_stream(mpctx
->stream
);
600 if(mask
&INITIALIZED_VO
){
601 mpctx
->initialized_flags
&=~INITIALIZED_VO
;
602 current_module
="uninit_vo";
603 vo_destroy(mpctx
->video_out
);
604 mpctx
->video_out
=NULL
;
606 mp_dvdnav_context_free(mpctx
);
610 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
611 if(mask
&INITIALIZED_GETCH2
){
612 mpctx
->initialized_flags
&=~INITIALIZED_GETCH2
;
613 current_module
="uninit_getch2";
614 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n[[[uninit getch2]]]\n");
619 if(mask
&INITIALIZED_VOBSUB
){
620 mpctx
->initialized_flags
&=~INITIALIZED_VOBSUB
;
621 current_module
="uninit_vobsub";
622 if(vo_vobsub
) vobsub_close(vo_vobsub
);
626 if (mask
&INITIALIZED_SPUDEC
){
627 mpctx
->initialized_flags
&=~INITIALIZED_SPUDEC
;
628 current_module
="uninit_spudec";
629 spudec_free(vo_spudec
);
633 if(mask
&INITIALIZED_AO
){
634 mpctx
->initialized_flags
&=~INITIALIZED_AO
;
635 current_module
="uninit_ao";
636 if (mpctx
->edl_muted
) mixer_mute(&mpctx
->mixer
);
637 mpctx
->audio_out
->uninit(mpctx
->stop_play
!= AT_END_OF_FILE
);
638 mpctx
->audio_out
=NULL
;
642 if(mask
&INITIALIZED_GUI
){
643 mpctx
->initialized_flags
&=~INITIALIZED_GUI
;
644 current_module
="uninit_gui";
652 void exit_player_with_rc(struct MPContext
*mpctx
, exit_reason_t how
, int rc
)
654 if (mpctx
->user_muted
&& !mpctx
->edl_muted
) mixer_mute(&mpctx
->mixer
);
655 uninit_player(mpctx
, INITIALIZED_ALL
);
656 #if defined(__MINGW32__) || defined(__CYGWIN__)
663 vo_uninit(mpctx
->x11_state
); // Close the X11 connection (if any is open).
666 current_module
="uninit_input";
667 mp_input_uninit(mpctx
->input
);
673 #ifdef CONFIG_FREETYPE
674 current_module
="uninit_font";
675 if (mpctx
->osd
&& mpctx
->osd
->sub_font
!= vo_font
)
676 free_font_desc(mpctx
->osd
->sub_font
);
677 free_font_desc(vo_font
);
681 osd_free(mpctx
->osd
);
684 ass_library_done(ass_library
);
687 current_module
="exit_player";
689 // free mplayer config
691 m_config_free(mpctx
->mconfig
);
694 play_tree_free(mpctx
->playtree
, 1);
696 talloc_free(mpctx
->key_fifo
);
698 if(edl_records
!= NULL
) free(edl_records
); // free mem allocated for EDL
701 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_ExitingHow
,MSGTR_Exit_quit
);
702 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=QUIT\n");
705 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_ExitingHow
,MSGTR_Exit_eof
);
706 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=EOF\n");
709 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_ExitingHow
,MSGTR_Exit_error
);
710 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=ERROR\n");
713 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=NONE\n");
715 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"max framesize was %d bytes\n",max_framesize
);
720 static void exit_player(struct MPContext
*mpctx
, exit_reason_t how
)
722 exit_player_with_rc(mpctx
, how
, 1);
726 static void child_sighandler(int x
){
728 while((pid
=waitpid(-1,NULL
,WNOHANG
)) > 0);
732 #ifdef CONFIG_CRASH_DEBUG
733 static char *prog_path
;
734 static int crash_debug
= 0;
737 static void exit_sighandler(int x
){
738 static int sig_count
=0;
739 #ifdef CONFIG_CRASH_DEBUG
740 if (!crash_debug
|| x
!= SIGTRAP
)
745 /* We're crashing bad and can't uninit cleanly :(
746 * by popular request, we make one last (dirty)
747 * effort to restore the user's
752 if(sig_count
==6) exit(1);
756 kill(getpid(),SIGKILL
);
759 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,"\n" MSGTR_IntBySignal
,x
,
760 current_module
?current_module
:"unknown"
762 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SIGNAL=%d\n", x
);
769 async_quit_request
= 1;
770 return; // killed from keyboard (^C) or killed [-9]
772 #ifdef RUNTIME_CPUDETECT
773 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_Exit_SIGILL_RTCpuSel
);
775 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_Exit_SIGILL
);
779 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_Exit_SIGSEGV_SIGFPE
);
781 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_Exit_SIGCRASH
);
782 #ifdef CONFIG_CRASH_DEBUG
785 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Forking...\n");
787 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Forked...\n");
788 if (gdb_pid
== 0) { // We are the child
790 snprintf(spid
, sizeof(spid
), "%i", getppid());
791 getch2_disable(); // allow terminal to work properly with gdb
792 if (execlp("gdb", "gdb", prog_path
, spid
, "-ex", "bt", NULL
) == -1)
793 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Couldn't start gdb\n");
794 } else if (gdb_pid
< 0)
795 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Couldn't fork\n");
797 waitpid(gdb_pid
, NULL
, 0);
799 if (x
== SIGTRAP
) return;
807 #include "cfg-mplayer.h"
809 static int cfg_include(m_option_t
*conf
, char *filename
)
811 return m_config_parse_config_file(conf
->priv
, filename
);
814 static void parse_cfgfiles(struct MPContext
*mpctx
, m_config_t
* conf
)
818 if (!disable_system_conf
&&
819 m_config_parse_config_file(conf
, MPLAYER_CONFDIR
"/mplayer.conf") < 0)
820 exit_player(mpctx
, EXIT_NONE
);
821 if ((conffile
= get_path("")) == NULL
) {
822 mp_msg(MSGT_CPLAYER
,MSGL_WARN
,MSGTR_NoHomeDir
);
827 mkdir(conffile
, 0777);
830 if ((conffile
= get_path("config")) == NULL
) {
831 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_GetpathProblem
);
833 if ((conffile_fd
= open(conffile
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666)) != -1) {
834 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_CreatingCfgFile
, conffile
);
835 write(conffile_fd
, default_config
, strlen(default_config
));
838 if (!disable_user_conf
&&
839 m_config_parse_config_file(conf
, conffile
) < 0)
840 exit_player(mpctx
, EXIT_NONE
);
846 #define PROFILE_CFG_PROTOCOL "protocol."
848 static void load_per_protocol_config (m_config_t
* conf
, const char *const file
)
851 char protocol
[strlen (PROFILE_CFG_PROTOCOL
) + strlen (file
) + 1];
854 /* does filename actually uses a protocol ? */
855 str
= strstr (file
, "://");
859 sprintf (protocol
, "%s%s", PROFILE_CFG_PROTOCOL
, file
);
860 protocol
[strlen (PROFILE_CFG_PROTOCOL
)+strlen(file
)-strlen(str
)] = '\0';
861 p
= m_config_get_profile (conf
, protocol
);
864 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_LoadingProtocolProfile
, protocol
);
865 m_config_set_profile(conf
,p
);
869 #define PROFILE_CFG_EXTENSION "extension."
871 static void load_per_extension_config (m_config_t
* conf
, const char *const file
)
874 char extension
[strlen (PROFILE_CFG_EXTENSION
) + 8];
877 /* does filename actually have an extension ? */
878 str
= strrchr (file
, '.');
882 sprintf (extension
, PROFILE_CFG_EXTENSION
);
883 strncat (extension
, ++str
, 7);
884 p
= m_config_get_profile (conf
, extension
);
887 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_LoadingExtensionProfile
, extension
);
888 m_config_set_profile(conf
,p
);
892 #define PROFILE_CFG_VO "vo."
893 #define PROFILE_CFG_AO "ao."
895 static void load_per_output_config (m_config_t
* conf
, char *cfg
, char *out
)
897 char profile
[strlen (cfg
) + strlen (out
) + 1];
900 sprintf (profile
, "%s%s", cfg
, out
);
901 p
= m_config_get_profile (conf
, profile
);
904 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_LoadingExtensionProfile
, profile
);
905 m_config_set_profile(conf
,p
);
909 static void load_per_file_config (m_config_t
* conf
, const char *const file
)
912 char cfg
[strlen(file
)+10];
916 sprintf (cfg
, "%s.conf", file
);
918 if (use_filedir_conf
&& !stat (cfg
, &st
))
920 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_LoadingConfig
, cfg
);
921 m_config_parse_config_file (conf
, cfg
);
925 if ((name
= strrchr (cfg
, '/')) == NULL
)
930 if ((confpath
= get_path (name
)) != NULL
)
932 if (!stat (confpath
, &st
))
934 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_LoadingConfig
, confpath
);
935 m_config_parse_config_file (conf
, confpath
);
942 /* When libmpdemux performs a blocking operation (network connection or
943 * cache filling) if the operation fails we use this function to check
944 * if it was interrupted by the user.
945 * The function returns a new value for eof. */
946 static int libmpdemux_was_interrupted(struct MPContext
*mpctx
, int stop_play
)
949 if((cmd
= mp_input_get_cmd(mpctx
->input
, 0,0,0)) != NULL
) {
952 exit_player_with_rc(mpctx
, EXIT_QUIT
, (cmd
->nargs
> 0)? cmd
->args
[0].v
.i
: 0);
953 case MP_CMD_PLAY_TREE_STEP
: {
954 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_NEXT_ENTRY
: PT_PREV_ENTRY
;
955 mpctx
->play_tree_step
= (cmd
->args
[0].v
.i
== 0) ? 1 : cmd
->args
[0].v
.i
;
957 case MP_CMD_PLAY_TREE_UP_STEP
: {
958 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_UP_NEXT
: PT_UP_PREV
;
960 case MP_CMD_PLAY_ALT_SRC_STEP
: {
961 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_NEXT_SRC
: PT_PREV_SRC
;
969 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
971 static int playtree_add_playlist(struct MPContext
*mpctx
, play_tree_t
* entry
)
973 play_tree_add_bpf(entry
,mpctx
->filename
);
978 import_playtree_playlist_into_gui(entry
, mpctx
->mconfig
);
979 play_tree_free_list(entry
,1);
985 entry
= mpctx
->playtree_iter
->tree
;
986 if(play_tree_iter_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
987 return PT_NEXT_ENTRY
;
989 if(mpctx
->playtree_iter
->tree
== entry
) { // Loop with a single file
990 if(play_tree_iter_up_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
991 return PT_NEXT_ENTRY
;
994 play_tree_remove(entry
,1,1);
997 play_tree_insert_entry(mpctx
->playtree_iter
->tree
,entry
);
998 play_tree_set_params_from(entry
,mpctx
->playtree_iter
->tree
);
999 entry
= mpctx
->playtree_iter
->tree
;
1000 if(play_tree_iter_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
1001 return PT_NEXT_ENTRY
;
1003 play_tree_remove(entry
,1,1);
1008 void add_subtitles(struct MPContext
*mpctx
, char *filename
, float fps
, int noerr
)
1012 ass_track_t
*asst
= 0;
1015 if (filename
== NULL
|| mpctx
->set_of_sub_size
>= MAX_SUBTITLE_FILES
) {
1019 subd
= sub_read_file(filename
, fps
);
1023 asst
= ass_read_file(ass_library
, filename
, sub_cp
);
1025 asst
= ass_read_file(ass_library
, filename
, 0);
1027 if (ass_enabled
&& subd
&& !asst
)
1028 asst
= ass_read_subdata(ass_library
, subd
, fps
);
1034 mp_msg(MSGT_CPLAYER
, noerr
? MSGL_WARN
: MSGL_ERR
, MSGTR_CantLoadSub
,
1035 filename_recode(filename
));
1038 if (!asst
&& !subd
) return;
1039 mpctx
->set_of_ass_tracks
[mpctx
->set_of_sub_size
] = asst
;
1043 mpctx
->set_of_subtitles
[mpctx
->set_of_sub_size
] = subd
;
1044 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_FILE_SUB_ID=%d\n", mpctx
->set_of_sub_size
);
1045 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_FILE_SUB_FILENAME=%s\n",
1046 filename_recode(filename
));
1047 ++mpctx
->set_of_sub_size
;
1048 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, MSGTR_AddedSubtitleFile
, mpctx
->set_of_sub_size
,
1049 filename_recode(filename
));
1052 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1053 void update_set_of_subtitles(struct MPContext
*mpctx
)
1054 // subdata was changed, set_of_sub... have to be updated.
1056 sub_data
** const set_of_subtitles
= mpctx
->set_of_subtitles
;
1058 if (mpctx
->set_of_sub_size
> 0 && subdata
== NULL
) { // *subdata was deleted
1059 for (i
= mpctx
->set_of_sub_pos
+ 1; i
< mpctx
->set_of_sub_size
; ++i
)
1060 set_of_subtitles
[i
-1] = set_of_subtitles
[i
];
1061 set_of_subtitles
[mpctx
->set_of_sub_size
-1] = NULL
;
1062 --mpctx
->set_of_sub_size
;
1063 if (mpctx
->set_of_sub_size
> 0) subdata
= set_of_subtitles
[mpctx
->set_of_sub_pos
=0];
1065 else if (mpctx
->set_of_sub_size
> 0 && subdata
!= NULL
) { // *subdata was changed
1066 set_of_subtitles
[mpctx
->set_of_sub_pos
] = subdata
;
1068 else if (mpctx
->set_of_sub_size
<= 0 && subdata
!= NULL
) { // *subdata was added
1069 set_of_subtitles
[mpctx
->set_of_sub_pos
=mpctx
->set_of_sub_size
] = subdata
;
1070 ++mpctx
->set_of_sub_size
;
1074 void init_vo_spudec(struct MPContext
*mpctx
)
1077 spudec_free(vo_spudec
);
1078 mpctx
->initialized_flags
&= ~INITIALIZED_SPUDEC
;
1081 unsigned int palette
[16], width
, height
;
1082 current_module
="spudec_init_vobsub";
1083 if (vobsub_parse_ifo(NULL
,spudec_ifo
, palette
, &width
, &height
, 1, -1, NULL
) >= 0)
1084 vo_spudec
=spudec_new_scaled(palette
, width
, height
, NULL
, 0);
1087 #ifdef CONFIG_DVDREAD
1088 if (vo_spudec
==NULL
&& mpctx
->stream
->type
==STREAMTYPE_DVD
) {
1089 current_module
="spudec_init_dvdread";
1090 vo_spudec
=spudec_new_scaled(((dvd_priv_t
*)(mpctx
->stream
->priv
))->cur_pgc
->palette
,
1091 mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
,
1096 #ifdef CONFIG_DVDNAV
1097 if (vo_spudec
==NULL
&& mpctx
->stream
->type
==STREAMTYPE_DVDNAV
) {
1098 unsigned int *palette
= mp_dvdnav_get_spu_clut(mpctx
->stream
);
1099 current_module
="spudec_init_dvdnav";
1100 vo_spudec
=spudec_new_scaled(palette
, mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
, NULL
, 0);
1104 if (vo_spudec
==NULL
) {
1105 sh_sub_t
*sh
= (sh_sub_t
*)mpctx
->d_sub
->sh
;
1106 current_module
="spudec_init_normal";
1107 vo_spudec
=spudec_new_scaled(NULL
, mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
, sh
->extradata
, sh
->extradata_len
);
1108 spudec_set_font_factor(vo_spudec
,font_factor
);
1111 if (vo_spudec
!=NULL
) {
1112 mpctx
->initialized_flags
|=INITIALIZED_SPUDEC
;
1113 mp_property_do("sub_forced_only", M_PROPERTY_SET
, &forced_subs_only
, mpctx
);
1118 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1119 * make it all work is to use the builtin SDL-bootstrap code, which
1120 * will be done automatically by replacing our main() if we include SDL.h.
1122 #if defined(__APPLE__) && defined(CONFIG_SDL)
1127 * \brief append a formatted string
1128 * \param buf buffer to print into
1129 * \param pos position of terminating 0 in buf
1130 * \param len maximum number of characters in buf, not including terminating 0
1131 * \param format printf format string
1133 static void saddf(char *buf
, unsigned *pos
, int len
, const char *format
, ...)
1136 va_start(va
, format
);
1137 *pos
+= vsnprintf(&buf
[*pos
], len
- *pos
, format
, va
);
1146 * \brief append time in the hh:mm:ss.f format
1147 * \param buf buffer to print into
1148 * \param pos position of terminating 0 in buf
1149 * \param len maximum number of characters in buf, not including terminating 0
1150 * \param time time value to convert/append
1152 static void sadd_hhmmssf(char *buf
, unsigned *pos
, int len
, float time
) {
1153 long tenths
= 10 * time
;
1154 int f1
= tenths
% 10;
1155 int ss
= (tenths
/ 10) % 60;
1156 int mm
= (tenths
/ 600) % 60;
1157 int hh
= tenths
/ 36000;
1159 saddf(buf
, pos
, len
, "unknown");
1163 saddf(buf
, pos
, len
, "%2d:", hh
);
1164 if (hh
> 0 || mm
> 0)
1165 saddf(buf
, pos
, len
, "%02d:", mm
);
1166 saddf(buf
, pos
, len
, "%02d.%1d", ss
, f1
);
1169 static void print_status(struct MPContext
*mpctx
, double a_pos
, bool at_frame
)
1171 struct MPOpts
*opts
= &mpctx
->opts
;
1172 sh_video_t
* const sh_video
= mpctx
->sh_video
;
1174 if (mpctx
->sh_audio
&& a_pos
== MP_NOPTS_VALUE
)
1175 a_pos
= playing_audio_pts(mpctx
);
1176 if (mpctx
->sh_audio
&& sh_video
&& at_frame
) {
1177 mpctx
->last_av_difference
= a_pos
- sh_video
->pts
- audio_delay
;
1178 if (mpctx
->last_av_difference
> 0.5 && drop_frame_cnt
> 50
1179 && !mpctx
->drop_message_shown
) {
1180 mp_msg(MSGT_AVSYNC
,MSGL_WARN
,MSGTR_SystemTooSlow
);
1181 mpctx
->drop_message_shown
= true;
1192 if (screen_width
> 0)
1193 width
= screen_width
;
1196 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1197 /* Windows command line is broken (MinGW's rxvt works, but we
1198 * should not depend on that). */
1201 line
= malloc(width
+ 1); // one additional char for the terminating null
1204 if (mpctx
->sh_audio
) {
1205 saddf(line
, &pos
, width
, "A:%6.1f ", a_pos
);
1207 float len
= demuxer_get_time_length(mpctx
->demuxer
);
1208 saddf(line
, &pos
, width
, "(");
1209 sadd_hhmmssf(line
, &pos
, width
, a_pos
);
1210 saddf(line
, &pos
, width
, ") of %.1f (", len
);
1211 sadd_hhmmssf(line
, &pos
, width
, len
);
1212 saddf(line
, &pos
, width
, ") ");
1218 saddf(line
, &pos
, width
, "V:%6.1f ", sh_video
->pts
);
1221 if (mpctx
->sh_audio
&& sh_video
)
1222 saddf(line
, &pos
, width
, "A-V:%7.3f ct:%7.3f ",
1223 mpctx
->last_av_difference
, mpctx
->total_avsync_change
);
1227 saddf(line
, &pos
, width
, "%3d/%3d ",
1228 (int)sh_video
->num_frames
,
1229 (int)sh_video
->num_frames_decoded
);
1233 if (sh_video
->timer
> 0.5)
1234 saddf(line
, &pos
, width
, "%2d%% %2d%% %4.1f%% ",
1235 (int)(100.0*video_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
),
1236 (int)(100.0*vout_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
),
1237 (100.0*audio_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
));
1239 saddf(line
, &pos
, width
, "??%% ??%% ??,?%% ");
1240 } else if (mpctx
->sh_audio
) {
1241 if (mpctx
->delay
> 0.5)
1242 saddf(line
, &pos
, width
, "%4.1f%% ",
1243 100.0*audio_time_usage
/(double)mpctx
->delay
);
1245 saddf(line
, &pos
, width
, "??,?%% ");
1250 saddf(line
, &pos
, width
, "%d %d ", drop_frame_cnt
, output_quality
);
1252 #ifdef CONFIG_STREAM_CACHE
1254 if (stream_cache_size
> 0)
1255 saddf(line
, &pos
, width
, "%d%% ", cache_fill_status
);
1259 if (opts
->playback_speed
!= 1)
1260 saddf(line
, &pos
, width
, "%4.2fx ", opts
->playback_speed
);
1263 if (erase_to_end_of_line
) {
1265 mp_msg(MSGT_STATUSLINE
, MSGL_STATUS
, "%s%s\r", line
, erase_to_end_of_line
);
1267 memset(&line
[pos
], ' ', width
- pos
);
1269 mp_msg(MSGT_STATUSLINE
, MSGL_STATUS
, "%s\r", line
);
1275 * \brief build a chain of audio filters that converts the input format
1276 * to the ao's format, taking into account the current playback_speed.
1277 * \param sh_audio describes the requested input format of the chain.
1278 * \param ao_data describes the requested output format of the chain.
1280 int build_afilter_chain(struct MPContext
*mpctx
, sh_audio_t
*sh_audio
, ao_data_t
*ao_data
)
1282 struct MPOpts
*opts
= &mpctx
->opts
;
1288 if (use_gui
) guiGetEvent(guiSetAfilter
, (char *)NULL
);
1290 mpctx
->mixer
.afilter
= NULL
;
1293 if(af_control_any_rev(sh_audio
->afilter
,
1294 AF_CONTROL_PLAYBACK_SPEED
| AF_CONTROL_SET
,
1295 &opts
->playback_speed
)) {
1296 new_srate
= sh_audio
->samplerate
;
1298 new_srate
= sh_audio
->samplerate
* opts
->playback_speed
;
1299 if (new_srate
!= ao_data
->samplerate
) {
1300 // limits are taken from libaf/af_resample.c
1301 if (new_srate
< 8000)
1303 if (new_srate
> 192000)
1305 opts
->playback_speed
= (float)new_srate
/ (float)sh_audio
->samplerate
;
1308 result
= init_audio_filters(sh_audio
, new_srate
,
1309 &ao_data
->samplerate
, &ao_data
->channels
, &ao_data
->format
);
1310 mpctx
->mixer
.afilter
= sh_audio
->afilter
;
1312 if (use_gui
) guiGetEvent(guiSetAfilter
, (char *)sh_audio
->afilter
);
1318 typedef struct mp_osd_msg mp_osd_msg_t
;
1320 /// Previous message on the stack.
1324 int id
,level
,started
;
1325 /// Display duration in ms.
1329 /// OSD message stack.
1330 static mp_osd_msg_t
* osd_msg_stack
= NULL
;
1333 * \brief Add a message on the OSD message stack
1335 * If a message with the same id is already present in the stack
1336 * it is pulled on top of the stack, otherwise a new message is created.
1340 void set_osd_msg(int id
, int level
, int time
, const char* fmt
, ...) {
1341 mp_osd_msg_t
*msg
,*last
=NULL
;
1345 // look if the id is already in the stack
1346 for(msg
= osd_msg_stack
; msg
&& msg
->id
!= id
;
1347 last
= msg
, msg
= msg
->prev
);
1348 // not found: alloc it
1350 msg
= calloc(1,sizeof(mp_osd_msg_t
));
1351 msg
->prev
= osd_msg_stack
;
1352 osd_msg_stack
= msg
;
1353 } else if(last
) { // found, but it's not on top of the stack
1354 last
->prev
= msg
->prev
;
1355 msg
->prev
= osd_msg_stack
;
1356 osd_msg_stack
= msg
;
1360 r
= vsnprintf(msg
->msg
, 128, fmt
, va
);
1362 if(r
>= 128) msg
->msg
[127] = 0;
1371 * \brief Remove a message from the OSD stack
1373 * This function can be used to get rid of a message right away.
1377 void rm_osd_msg(int id
) {
1378 mp_osd_msg_t
*msg
,*last
=NULL
;
1380 // Search for the msg
1381 for(msg
= osd_msg_stack
; msg
&& msg
->id
!= id
;
1382 last
= msg
, msg
= msg
->prev
);
1385 // Detach it from the stack and free it
1387 last
->prev
= msg
->prev
;
1389 osd_msg_stack
= msg
->prev
;
1394 * \brief Remove all messages from the OSD stack
1398 static void clear_osd_msgs(void) {
1399 mp_osd_msg_t
* msg
= osd_msg_stack
, *prev
= NULL
;
1405 osd_msg_stack
= NULL
;
1409 * \brief Get the current message from the OSD stack.
1411 * This function decrements the message timer and destroys the old ones.
1412 * The message that should be displayed is returned (if any).
1416 static mp_osd_msg_t
* get_osd_msg(struct MPContext
*mpctx
)
1418 struct MPOpts
*opts
= &mpctx
->opts
;
1419 mp_osd_msg_t
*msg
,*prev
,*last
= NULL
;
1420 static unsigned last_update
= 0;
1421 unsigned now
= GetTimerMS();
1423 char hidden_dec_done
= 0;
1425 if (mpctx
->osd_visible
) {
1426 // 36000000 means max timed visibility is 1 hour into the future, if
1427 // the difference is greater assume it's wrapped around from below 0
1428 if (mpctx
->osd_visible
- now
> 36000000) {
1429 mpctx
->osd_visible
= 0;
1430 vo_osd_progbar_type
= -1; // disable
1431 vo_osd_changed(OSDTYPE_PROGBAR
);
1432 mpctx
->osd_function
= mpctx
->paused
? OSD_PAUSE
: OSD_PLAY
;
1435 if (mpctx
->osd_show_percentage_until
- now
> 36000000)
1436 mpctx
->osd_show_percentage_until
= 0;
1438 if(!last_update
) last_update
= now
;
1439 diff
= now
>= last_update
? now
- last_update
: 0;
1443 // Look for the first message in the stack with high enough level.
1444 for(msg
= osd_msg_stack
; msg
; last
= msg
, msg
= prev
) {
1446 if (msg
->level
> opts
->osd_level
&& hidden_dec_done
)
1448 // The message has a high enough level or it is the first hidden one
1449 // in both cases we decrement the timer or kill it.
1450 if(!msg
->started
|| msg
->time
> diff
) {
1451 if(msg
->started
) msg
->time
-= diff
;
1452 else msg
->started
= 1;
1454 if (msg
->level
<= opts
->osd_level
)
1456 hidden_dec_done
= 1;
1465 osd_msg_stack
= prev
;
1474 * \brief Display the OSD bar.
1476 * Display the OSD bar or fall back on a simple message.
1480 void set_osd_bar(struct MPContext
*mpctx
, int type
,const char* name
,double min
,double max
,double val
) {
1481 struct MPOpts
*opts
= &mpctx
->opts
;
1482 if (opts
->osd_level
< 1)
1485 if(mpctx
->sh_video
) {
1486 mpctx
->osd_visible
= (GetTimerMS() + 1000) | 1;
1487 vo_osd_progbar_type
= type
;
1488 vo_osd_progbar_value
= 256*(val
-min
)/(max
-min
);
1489 vo_osd_changed(OSDTYPE_PROGBAR
);
1493 set_osd_msg(OSD_MSG_BAR
, 1, opts
->osd_duration
, "%s: %d %%",
1494 name
, ROUND(100*(val
-min
)/(max
-min
)));
1499 * \brief Update the OSD message line.
1501 * This function displays the current message on the vo OSD or on the term.
1502 * If the stack is empty and the OSD level is high enough the timer
1503 * is displayed (only on the vo OSD).
1507 static void update_osd_msg(struct MPContext
*mpctx
)
1509 struct MPOpts
*opts
= &mpctx
->opts
;
1511 struct osd_state
*osd
= mpctx
->osd
;
1512 char osd_text_timer
[128];
1514 if (mpctx
->add_osd_seek_info
) {
1516 if (mpctx
->timeline
&& mpctx
->sh_video
)
1517 percentage
= mpctx
->sh_video
->pts
* 100 /
1518 mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
1520 percentage
= demuxer_get_percent_pos(mpctx
->demuxer
);
1521 set_osd_bar(mpctx
, 0, "Position", 0, 100, percentage
);
1522 if (mpctx
->sh_video
)
1523 mpctx
->osd_show_percentage_until
= (GetTimerMS() + 1000) | 1;
1524 mpctx
->add_osd_seek_info
= false;
1527 // Look if we have a msg
1528 if((msg
= get_osd_msg(mpctx
))) {
1529 if (strcmp(osd
->osd_text
, msg
->msg
)) {
1530 strncpy(osd
->osd_text
, msg
->msg
, 127);
1531 if(mpctx
->sh_video
) vo_osd_changed(OSDTYPE_OSD
); else
1532 if(term_osd
) mp_msg(MSGT_CPLAYER
,MSGL_STATUS
,"%s%s\n",term_osd_esc
,msg
->msg
);
1537 if(mpctx
->sh_video
) {
1538 // fallback on the timer
1539 if (opts
->osd_level
>= 2) {
1541 if (mpctx
->timeline
)
1542 len
= mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
1544 len
= demuxer_get_time_length(mpctx
->demuxer
);
1545 int percentage
= -1;
1546 char percentage_text
[10];
1547 int pts
= demuxer_get_current_time(mpctx
->demuxer
);
1549 if (mpctx
->osd_show_percentage_until
)
1550 if (mpctx
->timeline
)
1551 percentage
= mpctx
->sh_video
->pts
* 100 /
1552 mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
1554 percentage
= demuxer_get_percent_pos(mpctx
->demuxer
);
1556 if (percentage
>= 0)
1557 snprintf(percentage_text
, 9, " (%d%%)", percentage
);
1559 percentage_text
[0] = 0;
1561 if (opts
->osd_level
== 3)
1562 snprintf(osd_text_timer
, 63,
1563 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1564 mpctx
->osd_function
,pts
/3600,(pts
/60)%60,pts
%60,
1565 len
/3600,(len
/60)%60,len
%60,percentage_text
);
1567 snprintf(osd_text_timer
, 63, "%c %02d:%02d:%02d%s",
1568 mpctx
->osd_function
,pts
/3600,(pts
/60)%60,
1569 pts
%60,percentage_text
);
1571 osd_text_timer
[0]=0;
1573 if (strcmp(osd
->osd_text
, osd_text_timer
)) {
1574 strncpy(osd
->osd_text
, osd_text_timer
, 63);
1575 vo_osd_changed(OSDTYPE_OSD
);
1580 // Clear the term osd line
1581 if (term_osd
&& osd
->osd_text
[0]) {
1582 osd
->osd_text
[0] = 0;
1583 printf("%s\n",term_osd_esc
);
1591 void reinit_audio_chain(struct MPContext
*mpctx
)
1593 struct MPOpts
*opts
= &mpctx
->opts
;
1594 if(mpctx
->sh_audio
){
1595 current_module
="init_audio_codec";
1596 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
1597 if(!init_best_audio_codec(mpctx
->sh_audio
,audio_codec_list
,audio_fm_list
)){
1598 mpctx
->sh_audio
=mpctx
->d_audio
->sh
=NULL
; // failed to init :(
1599 mpctx
->d_audio
->id
= -2;
1602 mpctx
->initialized_flags
|=INITIALIZED_ACODEC
;
1603 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
1606 //const ao_info_t *info=audio_out->info;
1607 current_module
="af_preinit";
1608 ao_data
.samplerate
=force_srate
;
1610 ao_data
.format
=audio_output_format
;
1612 // first init to detect best values
1613 if(!init_audio_filters(mpctx
->sh_audio
, // preliminary init
1615 mpctx
->sh_audio
->samplerate
,
1617 &ao_data
.samplerate
, &ao_data
.channels
, &ao_data
.format
)){
1618 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_AudioFilterChainPreinitError
);
1619 exit_player(mpctx
, EXIT_ERROR
);
1622 current_module
="ao2_init";
1623 if(!(mpctx
->audio_out
=init_best_audio_out(opts
->audio_driver_list
,
1627 ao_data
.format
,0))){
1629 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_CannotInitAO
);
1630 uninit_player(mpctx
, INITIALIZED_ACODEC
); // close codec
1631 mpctx
->sh_audio
=mpctx
->d_audio
->sh
=NULL
; // -> nosound
1632 mpctx
->d_audio
->id
= -2;
1636 mpctx
->initialized_flags
|=INITIALIZED_AO
;
1637 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1638 mpctx
->audio_out
->info
->short_name
,
1639 ao_data
.samplerate
, ao_data
.channels
,
1640 af_fmt2str_short(ao_data
.format
),
1641 af_fmt2bits(ao_data
.format
)/8 );
1642 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AO: Description: %s\nAO: Author: %s\n",
1643 mpctx
->audio_out
->info
->name
, mpctx
->audio_out
->info
->author
);
1644 if(strlen(mpctx
->audio_out
->info
->comment
) > 0)
1645 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AO: Comment: %s\n", mpctx
->audio_out
->info
->comment
);
1646 // init audio filters:
1648 current_module
="af_init";
1649 if(!build_afilter_chain(mpctx
, mpctx
->sh_audio
, &ao_data
)) {
1650 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_NoMatchingFilter
);
1651 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1652 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1653 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1657 mpctx
->mixer
.audio_out
= mpctx
->audio_out
;
1658 mpctx
->mixer
.volstep
= volstep
;
1667 // Return pts value corresponding to the end point of audio written to the
1669 static double written_audio_pts(struct MPContext
*mpctx
)
1671 sh_audio_t
*sh_audio
= mpctx
->sh_audio
;
1672 demux_stream_t
*d_audio
= mpctx
->d_audio
;
1673 double buffered_output
;
1674 // first calculate the end pts of audio that has been output by decoder
1675 double a_pts
= sh_audio
->pts
;
1676 if (a_pts
!= MP_NOPTS_VALUE
)
1677 // Good, decoder supports new way of calculating audio pts.
1678 // sh_audio->pts is the timestamp of the latest input packet with
1679 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1680 // the amount of bytes the decoder has written after that timestamp.
1681 a_pts
+= sh_audio
->pts_bytes
/ (double) sh_audio
->o_bps
;
1683 // Decoder doesn't support new way of calculating pts (or we're
1684 // being called before it has decoded anything with known timestamp).
1685 // Use the old method of audio pts calculation: take the timestamp
1686 // of last packet with known pts the decoder has read data from,
1687 // and add amount of bytes read after the beginning of that packet
1688 // divided by input bps. This will be inaccurate if the input/output
1689 // ratio is not constant for every audio packet or if it is constant
1690 // but not accurately known in sh_audio->i_bps.
1692 a_pts
= d_audio
->pts
;
1693 // ds_tell_pts returns bytes read after last timestamp from
1694 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1695 // it has read but not decoded
1696 if (sh_audio
->i_bps
)
1697 a_pts
+= (ds_tell_pts(d_audio
) - sh_audio
->a_in_buffer_len
) /
1698 (double)sh_audio
->i_bps
;
1700 // Now a_pts hopefully holds the pts for end of audio from decoder.
1701 // Substract data in buffers between decoder and audio out.
1703 // Decoded but not filtered
1704 a_pts
-= sh_audio
->a_buffer_len
/ (double)sh_audio
->o_bps
;
1706 // Data buffered in audio filters, measured in bytes of "missing" output
1707 buffered_output
= af_calc_delay(sh_audio
->afilter
);
1709 // Data that was ready for ao but was buffered because ao didn't fully
1710 // accept everything to internal buffers yet
1711 buffered_output
+= sh_audio
->a_out_buffer_len
;
1713 // Filters divide audio length by playback_speed, so multiply by it
1714 // to get the length in original units without speedup or slowdown
1715 a_pts
-= buffered_output
* mpctx
->opts
.playback_speed
/ ao_data
.bps
;
1717 return a_pts
+ mpctx
->video_offset
;
1720 // Return pts value corresponding to currently playing audio.
1721 double playing_audio_pts(struct MPContext
*mpctx
)
1723 return written_audio_pts(mpctx
) - mpctx
->opts
.playback_speed
*
1724 mpctx
->audio_out
->get_delay();
1727 static int check_framedrop(struct MPContext
*mpctx
, double frame_time
) {
1728 struct MPOpts
*opts
= &mpctx
->opts
;
1729 // check for frame-drop:
1730 current_module
= "check_framedrop";
1731 if (mpctx
->sh_audio
&& !mpctx
->d_audio
->eof
) {
1732 static int dropped_frames
;
1733 float delay
= opts
->playback_speed
*mpctx
->audio_out
->get_delay();
1734 float d
= delay
-mpctx
->delay
;
1736 // we should avoid dropping too many frames in sequence unless we
1737 // are too late. and we allow 100ms A-V delay here:
1738 if (d
< -dropped_frames
*frame_time
-0.100 && !mpctx
->paused
1739 && !mpctx
->update_video_immediately
) {
1742 return frame_dropping
;
1754 static float timing_sleep(struct MPContext
*mpctx
, float time_frame
)
1758 // -------- RTC -----------
1759 current_module
="sleep_rtc";
1760 while (time_frame
> 0.000) {
1761 unsigned long rtc_ts
;
1762 if (read(rtc_fd
, &rtc_ts
, sizeof(rtc_ts
)) <= 0)
1763 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, MSGTR_LinuxRTCReadError
, strerror(errno
));
1764 time_frame
-= get_relative_time(mpctx
);
1769 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1770 // unnecessarily high CPU usage
1771 float margin
= softsleep
? 0.011 : 0;
1772 current_module
= "sleep_timer";
1773 while (time_frame
> margin
) {
1774 usec_sleep(1000000 * (time_frame
- margin
));
1775 time_frame
-= get_relative_time(mpctx
);
1778 current_module
= "sleep_soft";
1780 mp_msg(MSGT_AVSYNC
, MSGL_WARN
, MSGTR_SoftsleepUnderflow
);
1781 while (time_frame
> 0)
1782 time_frame
-= get_relative_time(mpctx
); // burn the CPU
1788 #ifdef CONFIG_DVDNAV
1792 /// store decoded video image
1793 static mp_image_t
* mp_dvdnav_copy_mpi(mp_image_t
*to_mpi
,
1794 mp_image_t
*from_mpi
) {
1797 /// Do not store B-frames
1798 if (from_mpi
->pict_type
== FF_B_TYPE
)
1802 to_mpi
->w
== from_mpi
->w
&&
1803 to_mpi
->h
== from_mpi
->h
&&
1804 to_mpi
->imgfmt
== from_mpi
->imgfmt
)
1808 free_mp_image(to_mpi
);
1809 if (from_mpi
->w
== 0 || from_mpi
->h
== 0)
1811 mpi
= alloc_mpi(from_mpi
->w
,from_mpi
->h
,from_mpi
->imgfmt
);
1814 copy_mpi(mpi
,from_mpi
);
1818 static void mp_dvdnav_reset_stream (MPContext
*ctx
) {
1819 struct MPOpts
*opts
= &ctx
->opts
;
1820 if (ctx
->sh_video
) {
1822 ctx
->d_video
->pts
= 0.0f
;
1823 ctx
->sh_video
->pts
= 0.0f
;
1824 ctx
->sh_video
->i_pts
= 0.0f
;
1825 ctx
->sh_video
->last_pts
= 0.0f
;
1826 ctx
->sh_video
->num_buffered_pts
= 0;
1827 ctx
->sh_video
->num_frames
= 0;
1828 ctx
->sh_video
->num_frames_decoded
= 0;
1829 ctx
->sh_video
->timer
= 0.0f
;
1830 ctx
->sh_video
->stream_delay
= 0.0f
;
1831 ctx
->sh_video
->timer
= 0;
1832 ctx
->demuxer
->stream_pts
= MP_NOPTS_VALUE
;
1835 if (ctx
->sh_audio
) {
1836 /// free audio packets and reset
1837 ds_free_packs(ctx
->d_audio
);
1838 audio_delay
-= ctx
->sh_audio
->stream_delay
;
1839 ctx
->delay
=- audio_delay
;
1840 ctx
->audio_out
->reset();
1841 resync_audio_stream(ctx
->sh_audio
);
1844 if (ctx
->d_sub
) opts
->sub_id
= -2;
1848 /// clear all EOF related flags
1849 ctx
->d_video
->eof
= ctx
->d_audio
->eof
= ctx
->stream
->eof
= 0;
1852 /// Restore last decoded DVDNAV (still frame)
1853 static mp_image_t
*mp_dvdnav_restore_smpi(struct MPContext
*mpctx
,
1855 unsigned char **start
,
1856 mp_image_t
*decoded_frame
)
1858 if (mpctx
->stream
->type
!= STREAMTYPE_DVDNAV
)
1859 return decoded_frame
;
1861 /// a change occured in dvdnav stream
1862 if (mp_dvdnav_cell_has_changed(mpctx
->stream
,0)) {
1863 mp_dvdnav_read_wait(mpctx
->stream
, 1, 1);
1864 mp_dvdnav_context_free(mpctx
);
1865 mp_dvdnav_reset_stream(mpctx
);
1866 mp_dvdnav_read_wait(mpctx
->stream
, 0, 1);
1867 mp_dvdnav_cell_has_changed(mpctx
->stream
,1);
1873 /// Display still frame, if any
1874 if (mpctx
->nav_smpi
&& !mpctx
->nav_buffer
)
1875 decoded_frame
= mpctx
->nav_smpi
;
1877 /// increment video frame : continue playing after still frame
1878 len
= demuxer_get_time_length(mpctx
->demuxer
);
1879 if (mpctx
->sh_video
->pts
>= len
&&
1880 mpctx
->sh_video
->pts
> 0.0 && len
> 0.0) {
1881 mp_dvdnav_skip_still(mpctx
->stream
);
1882 mp_dvdnav_skip_wait(mpctx
->stream
);
1884 mpctx
->sh_video
->pts
+= 1 / mpctx
->sh_video
->fps
;
1886 if (mpctx
->nav_buffer
) {
1887 *start
= mpctx
->nav_start
;
1888 *in_size
= mpctx
->nav_in_size
;
1889 if (mpctx
->nav_start
)
1890 memcpy(*start
,mpctx
->nav_buffer
,mpctx
->nav_in_size
);
1894 return decoded_frame
;
1897 /// Save last decoded DVDNAV (still frame)
1898 static void mp_dvdnav_save_smpi(struct MPContext
*mpctx
, int in_size
,
1899 unsigned char *start
,
1900 mp_image_t
*decoded_frame
)
1902 if (mpctx
->stream
->type
!= STREAMTYPE_DVDNAV
)
1905 if (mpctx
->nav_buffer
)
1906 free(mpctx
->nav_buffer
);
1908 mpctx
->nav_buffer
= malloc(in_size
);
1909 mpctx
->nav_start
= start
;
1910 mpctx
->nav_in_size
= mpctx
->nav_buffer
? in_size
: -1;
1911 if (mpctx
->nav_buffer
)
1912 memcpy(mpctx
->nav_buffer
,start
,in_size
);
1914 if (decoded_frame
&& mpctx
->nav_smpi
!= decoded_frame
)
1915 mpctx
->nav_smpi
= mp_dvdnav_copy_mpi(mpctx
->nav_smpi
,decoded_frame
);
1917 #endif /* CONFIG_DVDNAV */
1919 /* Modify video timing to match the audio timeline. There are two main
1920 * reasons this is needed. First, video and audio can start from different
1921 * positions at beginning of file or after a seek (MPlayer starts both
1922 * immediately even if they have different pts). Second, the file can have
1923 * audio timestamps that are inconsistent with the duration of the audio
1924 * packets, for example two consecutive timestamp values differing by
1925 * one second but only a packet with enough samples for half a second
1926 * of playback between them.
1928 static void adjust_sync(struct MPContext
*mpctx
, double frame_time
)
1930 struct MPOpts
*opts
= &mpctx
->opts
;
1931 current_module
= "av_sync";
1933 if (!mpctx
->sh_audio
)
1936 double a_pts
= written_audio_pts(mpctx
) - mpctx
->delay
;
1937 double v_pts
= mpctx
->sh_video
->pts
;
1938 double av_delay
= a_pts
- v_pts
;
1939 // Try to sync vo_flip() so it will *finish* at given time
1940 av_delay
+= mpctx
->last_vo_flip_duration
;
1941 av_delay
-= audio_delay
; // This much pts difference is desired
1943 double change
= av_delay
* 0.1;
1944 double max_change
= default_max_pts_correction
>= 0 ?
1945 default_max_pts_correction
: frame_time
* 0.1;
1946 if (change
< -max_change
)
1947 change
= -max_change
;
1948 else if (change
> max_change
)
1949 change
= max_change
;
1950 mpctx
->delay
+= change
;
1951 mpctx
->total_avsync_change
+= change
;
1954 static int fill_audio_out_buffers(struct MPContext
*mpctx
)
1956 struct MPOpts
*opts
= &mpctx
->opts
;
1963 sh_audio_t
* const sh_audio
= mpctx
->sh_audio
;
1965 current_module
="play_audio";
1969 // all the current uses of ao_data.pts seem to be in aos that handle
1970 // sync completely wrong; there should be no need to use ao_data.pts
1972 ao_data
.pts
= ((mpctx
->sh_video
?mpctx
->sh_video
->timer
:0)+mpctx
->delay
)*90000.0;
1973 bytes_to_write
= mpctx
->audio_out
->get_space();
1974 if (mpctx
->sh_video
|| bytes_to_write
>= ao_data
.outburst
)
1977 // handle audio-only case:
1978 // this is where mplayer sleeps during audio-only playback
1979 // to avoid 100% CPU use
1980 sleep_time
= (ao_data
.outburst
- bytes_to_write
) * 1000 / ao_data
.bps
;
1981 if (sleep_time
< 10) sleep_time
= 10; // limit to 100 wakeups per second
1982 usec_sleep(sleep_time
* 1000);
1985 while (bytes_to_write
) {
1986 playsize
= bytes_to_write
;
1987 if (playsize
> MAX_OUTBURST
)
1988 playsize
= MAX_OUTBURST
;
1989 bytes_to_write
-= playsize
;
1991 // Fill buffer if needed:
1992 current_module
="decode_audio";
1994 if (decode_audio(sh_audio
, playsize
) < 0) // EOF or error
1995 if (mpctx
->d_audio
->eof
) {
1997 if (sh_audio
->a_out_buffer_len
== 0)
2001 tt
= t
*0.000001f
; audio_time_usage
+=tt
;
2002 if (playsize
> sh_audio
->a_out_buffer_len
) {
2003 playsize
= sh_audio
->a_out_buffer_len
;
2005 playflags
|= AOPLAY_FINAL_CHUNK
;
2011 current_module
="play_audio";
2013 // Is this pts value actually useful for the aos that access it?
2014 // They're obviously badly broken in the way they handle av sync;
2015 // would not having access to this make them more broken?
2016 ao_data
.pts
= ((mpctx
->sh_video
?mpctx
->sh_video
->timer
:0)+mpctx
->delay
)*90000.0;
2017 playsize
= mpctx
->audio_out
->play(sh_audio
->a_out_buffer
, playsize
, playflags
);
2020 sh_audio
->a_out_buffer_len
-= playsize
;
2021 memmove(sh_audio
->a_out_buffer
, &sh_audio
->a_out_buffer
[playsize
],
2022 sh_audio
->a_out_buffer_len
);
2023 mpctx
->delay
+= opts
->playback_speed
*playsize
/(double)ao_data
.bps
;
2025 else if (audio_eof
&& mpctx
->audio_out
->get_delay() < .04) {
2026 // Sanity check to avoid hanging in case current ao doesn't output
2027 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2028 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, "Audio output truncated at end.\n");
2029 sh_audio
->a_out_buffer_len
= 0;
2035 static int sleep_until_update(struct MPContext
*mpctx
, float *time_frame
,
2036 float *aq_sleep_time
)
2038 struct MPOpts
*opts
= &mpctx
->opts
;
2039 int frame_time_remaining
= 0;
2040 current_module
="calc_sleep_time";
2042 *time_frame
-= get_relative_time(mpctx
); // reset timer
2044 if (mpctx
->sh_audio
&& !mpctx
->d_audio
->eof
) {
2045 float delay
= mpctx
->audio_out
->get_delay();
2046 mp_dbg(MSGT_AVSYNC
, MSGL_DBG2
, "delay=%f\n", delay
);
2050 * Adjust this raw delay value by calculating the expected
2051 * delay for this frame and generating a new value which is
2052 * weighted between the two. The higher autosync is, the
2053 * closer to the delay value gets to that which "-nosound"
2054 * would have used, and the longer it will take for A/V
2055 * sync to settle at the right value (but it eventually will.)
2056 * This settling time is very short for values below 100.
2058 float predicted
= mpctx
->delay
/ opts
->playback_speed
+ *time_frame
;
2059 float difference
= delay
- predicted
;
2060 delay
= predicted
+ difference
/ (float)autosync
;
2063 *time_frame
= delay
- mpctx
->delay
/ opts
->playback_speed
;
2065 // delay = amount of audio buffered in soundcard/driver
2066 if (delay
> 0.25) delay
=0.25; else
2067 if (delay
< 0.10) delay
=0.10;
2068 if (*time_frame
> delay
*0.6) {
2069 // sleep time too big - may cause audio drops (buffer underrun)
2070 frame_time_remaining
= 1;
2071 *time_frame
= delay
*0.5;
2074 // If we're lagging more than 200 ms behind the right playback rate,
2075 // don't try to "catch up".
2076 // If benchmark is set always output frames as fast as possible
2077 // without sleeping.
2078 if (*time_frame
< -0.2 || benchmark
)
2082 *aq_sleep_time
+= *time_frame
;
2085 //============================== SLEEP: ===================================
2087 // flag 256 means: libvo driver does its timing (dvb card)
2088 if (*time_frame
> 0.001 && !(mpctx
->sh_video
->output_flags
&256))
2089 *time_frame
= timing_sleep(mpctx
, *time_frame
);
2090 return frame_time_remaining
;
2093 int reinit_video_chain(struct MPContext
*mpctx
)
2095 struct MPOpts
*opts
= &mpctx
->opts
;
2096 sh_video_t
* const sh_video
= mpctx
->sh_video
;
2098 //================== Init VIDEO (codec & libvo) ==========================
2099 if (!opts
->fixed_vo
|| !(mpctx
->initialized_flags
& INITIALIZED_VO
)) {
2100 current_module
="preinit_libvo";
2102 //shouldn't we set dvideo->id=-2 when we fail?
2103 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2104 if(!(mpctx
->video_out
=init_best_video_out(opts
, mpctx
->x11_state
, mpctx
->key_fifo
, mpctx
->input
))){
2105 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_ErrorInitializingVODevice
);
2108 mpctx
->initialized_flags
|=INITIALIZED_VO
;
2111 if(stream_control(mpctx
->demuxer
->stream
, STREAM_CTRL_GET_ASPECT_RATIO
, &ar
) != STREAM_UNSUPPORTED
)
2112 mpctx
->sh_video
->stream_aspect
= ar
;
2113 current_module
="init_video_filters";
2115 char* vf_arg
[] = { "_oldargs_", (char*)mpctx
->video_out
, NULL
};
2116 sh_video
->vfilter
= vf_open_filter(opts
, NULL
,"vo",vf_arg
);
2120 char* vf_arg
[] = { "_oldargs_", menu_root
, NULL
};
2121 vf_menu
= vf_open_plugin(opts
,libmenu_vfs
,sh_video
->vfilter
,"menu",vf_arg
);
2123 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_CantOpenLibmenuFilterWithThisRootMenu
,menu_root
);
2128 sh_video
->vfilter
= vf_menu
;
2135 if (opts
->vf_settings
)
2136 for (i
= 0; opts
->vf_settings
[i
].name
; ++i
)
2137 if (strcmp(opts
->vf_settings
[i
].name
, "ass") == 0) {
2142 extern vf_info_t vf_info_ass
;
2143 const vf_info_t
* libass_vfs
[] = {&vf_info_ass
, NULL
};
2144 char* vf_arg
[] = {"auto", "1", NULL
};
2145 vf_instance_t
* vf_ass
= vf_open_plugin(opts
, libass_vfs
,sh_video
->vfilter
,"ass",vf_arg
);
2147 sh_video
->vfilter
= vf_ass
;
2149 mp_msg(MSGT_CPLAYER
,MSGL_ERR
, "ASS: cannot add video filter\n");
2154 sh_video
->vfilter
= append_filters(sh_video
->vfilter
, opts
->vf_settings
);
2158 sh_video
->vfilter
->control(sh_video
->vfilter
, VFCTRL_INIT_EOSD
, ass_library
);
2161 current_module
="init_video_codec";
2163 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
2164 init_best_video_codec(sh_video
,video_codec_list
,video_fm_list
);
2165 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
2167 if(!sh_video
->initialized
){
2168 if(!opts
->fixed_vo
) uninit_player(mpctx
, INITIALIZED_VO
);
2172 mpctx
->initialized_flags
|=INITIALIZED_VCODEC
;
2174 if (sh_video
->codec
)
2175 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_CODEC=%s\n", sh_video
->codec
->name
);
2177 sh_video
->last_pts
= MP_NOPTS_VALUE
;
2178 sh_video
->num_buffered_pts
= 0;
2179 sh_video
->next_frame_time
= 0;
2182 // Auto quality option enabled
2183 output_quality
=get_video_quality_max(sh_video
);
2184 if(auto_quality
>output_quality
) auto_quality
=output_quality
;
2185 else output_quality
=auto_quality
;
2186 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AutoQ: setting quality to %d.\n",output_quality
);
2187 set_video_quality(sh_video
,output_quality
);
2190 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2192 current_module
="init_vo";
2197 mpctx
->sh_video
= mpctx
->d_video
->sh
= NULL
;
2201 static double update_video_nocorrect_pts(struct MPContext
*mpctx
,
2204 struct sh_video
*sh_video
= mpctx
->sh_video
;
2206 double frame_time
= 0;
2208 current_module
= "filter_video";
2209 // In nocorrect-pts mode there is no way to properly time these frames
2210 if (vf_output_queued_frame(sh_video
->vfilter
))
2212 unsigned char *packet
= NULL
;
2213 frame_time
= sh_video
->next_frame_time
;
2214 if (mpctx
->update_video_immediately
)
2216 int in_size
= video_read_frame(sh_video
, &sh_video
->next_frame_time
,
2217 &packet
, force_fps
);
2219 #ifdef CONFIG_DVDNAV
2220 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
2221 if (mp_dvdnav_is_eof(mpctx
->stream
))
2224 mpctx
->d_video
->eof
= 0;
2226 mpctx
->d_audio
->eof
= 0;
2227 mpctx
->stream
->eof
= 0;
2232 if (in_size
> max_framesize
)
2233 max_framesize
= in_size
;
2234 sh_video
->timer
+= frame_time
;
2235 if (mpctx
->sh_audio
)
2236 mpctx
->delay
-= frame_time
;
2237 // video_read_frame can change fps (e.g. for ASF video)
2238 vo_fps
= sh_video
->fps
;
2239 int framedrop_type
= check_framedrop(mpctx
, frame_time
);
2240 current_module
= "decode video";
2242 void *decoded_frame
;
2243 #ifdef CONFIG_DVDNAV
2244 decoded_frame
= mp_dvdnav_restore_smpi(mpctx
, &in_size
, &packet
, NULL
);
2245 if (in_size
>= 0 && !decoded_frame
)
2247 decoded_frame
= decode_video(sh_video
, packet
, in_size
, framedrop_type
,
2249 #ifdef CONFIG_DVDNAV
2250 // Save last still frame for future display
2251 mp_dvdnav_save_smpi(mpctx
, in_size
, packet
, decoded_frame
);
2253 if (decoded_frame
) {
2254 // These updates are done here for vf_expand OSD/subtitles
2255 update_subtitles(sh_video
, mpctx
->d_sub
, mpctx
->video_offset
, 0);
2256 update_teletext(sh_video
, mpctx
->demuxer
, 0);
2257 update_osd_msg(mpctx
);
2258 current_module
= "filter video";
2259 if (filter_video(sh_video
, decoded_frame
, sh_video
->pts
))
2267 static double update_video(struct MPContext
*mpctx
, int *blit_frame
)
2269 struct sh_video
*sh_video
= mpctx
->sh_video
;
2271 sh_video
->vfilter
->control(sh_video
->vfilter
, VFCTRL_SET_OSD_OBJ
,
2272 mpctx
->osd
); // hack for vf_expand
2273 if (!mpctx
->opts
.correct_pts
)
2274 return update_video_nocorrect_pts(mpctx
, blit_frame
);
2279 current_module
= "filter_video";
2280 // XXX Time used in this call is not counted in any performance
2281 // timer now, OSD time is not updated correctly for filter-added frames
2282 if (vf_output_queued_frame(sh_video
->vfilter
))
2284 unsigned char *packet
= NULL
;
2285 bool hit_eof
= false;
2286 int in_size
= ds_get_packet_pts(mpctx
->d_video
, &packet
, &pts
);
2287 if (pts
!= MP_NOPTS_VALUE
)
2288 pts
+= mpctx
->video_offset
;
2290 // try to extract last frames in case of decoder lag
2295 if (in_size
> max_framesize
)
2296 max_framesize
= in_size
;
2297 current_module
= "decode video";
2298 int framedrop_type
= check_framedrop(mpctx
, sh_video
->frametime
);
2299 void *decoded_frame
= decode_video(sh_video
, packet
, in_size
,
2300 framedrop_type
, pts
);
2301 if (decoded_frame
) {
2302 // These updates are done here for vf_expand OSD/subtitles
2303 update_subtitles(sh_video
, mpctx
->d_sub
, mpctx
->video_offset
, 0);
2304 update_teletext(sh_video
, mpctx
->demuxer
, 0);
2305 update_osd_msg(mpctx
);
2306 current_module
= "filter video";
2307 if (filter_video(sh_video
, decoded_frame
, sh_video
->pts
))
2313 sh_video
->vfilter
->control(sh_video
->vfilter
, VFCTRL_GET_PTS
, &pts
);
2314 if (pts
== MP_NOPTS_VALUE
) {
2315 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Video pts after filters MISSING\n");
2316 // Try to use decoder pts from before filters
2317 pts
= sh_video
->pts
;
2319 sh_video
->pts
= pts
;
2320 if (sh_video
->last_pts
== MP_NOPTS_VALUE
)
2321 sh_video
->last_pts
= sh_video
->pts
;
2322 else if (sh_video
->last_pts
> sh_video
->pts
) {
2323 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Decreasing video pts: %f < %f\n",
2324 sh_video
->pts
, sh_video
->last_pts
);
2325 /* If the difference in pts is small treat it as jitter around the
2326 * right value (possibly caused by incorrect timestamp ordering) and
2327 * just show this frame immediately after the last one.
2328 * Treat bigger differences as timestamp resets and start counting
2329 * timing of later frames from the position of this one. */
2330 if (sh_video
->last_pts
- sh_video
->pts
> 0.5)
2331 sh_video
->last_pts
= sh_video
->pts
;
2333 sh_video
->pts
= sh_video
->last_pts
;
2335 double frame_time
= sh_video
->pts
- sh_video
->last_pts
;
2336 sh_video
->last_pts
= sh_video
->pts
;
2337 sh_video
->timer
+= frame_time
;
2338 if (mpctx
->sh_audio
)
2339 mpctx
->delay
-= frame_time
;
2344 void pause_player(struct MPContext
*mpctx
)
2349 mpctx
->step_frames
= 0;
2350 mpctx
->time_frame
-= get_relative_time(mpctx
);
2352 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
)
2353 vo_control(mpctx
->video_out
, VOCTRL_PAUSE
, NULL
);
2355 if (mpctx
->audio_out
&& mpctx
->sh_audio
)
2356 mpctx
->audio_out
->pause(); // pause audio, keep data if possible
2359 void unpause_player(struct MPContext
*mpctx
)
2365 if (mpctx
->audio_out
&& mpctx
->sh_audio
)
2366 mpctx
->audio_out
->resume(); // resume audio
2367 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
2368 && !mpctx
->step_frames
)
2369 vo_control(mpctx
->video_out
, VOCTRL_RESUME
, NULL
); // resume video
2370 (void)get_relative_time(mpctx
); // ignore time that passed during pause
2373 void add_step_frame(struct MPContext
*mpctx
)
2375 mpctx
->step_frames
++;
2376 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
)
2377 vo_control(mpctx
->video_out
, VOCTRL_PAUSE
, NULL
);
2378 unpause_player(mpctx
);
2381 static void pause_loop(struct MPContext
*mpctx
)
2385 // Small hack to display the pause message on the OSD line.
2386 // The pause string is: "\n == PAUSE == \r" so we need to
2387 // take the first and the last char out
2388 if (term_osd
&& !mpctx
->sh_video
) {
2389 char msg
[128] = MSGTR_Paused
;
2390 int mlen
= strlen(msg
);
2392 set_osd_msg(OSD_MSG_PAUSE
, 1, 0, "%s", msg
+1);
2393 update_osd_msg(mpctx
);
2395 mp_msg(MSGT_CPLAYER
,MSGL_STATUS
,MSGTR_Paused
);
2396 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_PAUSED\n");
2400 guiGetEvent(guiCEvent
, (char *)guiSetPause
);
2403 while ( (cmd
= mp_input_get_cmd(mpctx
->input
, 20, 1, 1)) == NULL
2404 || cmd
->id
== MP_CMD_SET_MOUSE_POS
|| cmd
->pausing
== 4) {
2406 cmd
= mp_input_get_cmd(mpctx
->input
, 0,1,0);
2407 run_command(mpctx
, cmd
);
2411 if (mpctx
->sh_video
&& mpctx
->video_out
)
2412 vo_check_events(mpctx
->video_out
);
2416 guiGetEvent(guiReDraw
, NULL
);
2417 if (guiIntfStruct
.Playing
!=2 || (mpctx
->rel_seek_secs
|| mpctx
->abs_seek_pos
))
2423 vf_menu_pause_update(vf_menu
);
2426 update_osd_msg(mpctx
);
2427 int hack
= vo_osd_changed(0);
2428 vo_osd_changed(hack
);
2434 if (guiIntfStruct
.Playing
== guiSetStop
)
2437 guiGetEvent(guiCEvent
, (char *)guiSetPlay
);
2443 // Find the right mute status and record position for new file position
2444 static void edl_seek_reset(MPContext
*mpctx
)
2446 mpctx
->edl_muted
= 0;
2447 next_edl_record
= edl_records
;
2449 while (next_edl_record
) {
2450 if (next_edl_record
->start_sec
>= mpctx
->sh_video
->pts
)
2453 if (next_edl_record
->action
== EDL_MUTE
)
2454 mpctx
->edl_muted
= !mpctx
->edl_muted
;
2455 next_edl_record
= next_edl_record
->next
;
2457 if ((mpctx
->user_muted
| mpctx
->edl_muted
) != mpctx
->mixer
.muted
)
2458 mixer_mute(&mpctx
->mixer
);
2462 // Execute EDL command for the current position if one exists
2463 static void edl_update(MPContext
*mpctx
)
2465 if (!next_edl_record
)
2468 if (!mpctx
->sh_video
) {
2469 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, MSGTR_EdlNOsh_video
);
2470 free_edl(edl_records
);
2471 next_edl_record
= NULL
;
2476 if (mpctx
->sh_video
->pts
>= next_edl_record
->start_sec
) {
2477 if (next_edl_record
->action
== EDL_SKIP
) {
2478 mpctx
->osd_function
= OSD_FFW
;
2479 mpctx
->abs_seek_pos
= 0;
2480 mpctx
->rel_seek_secs
= next_edl_record
->length_sec
;
2481 mp_msg(MSGT_CPLAYER
, MSGL_DBG4
, "EDL_SKIP: start [%f], stop "
2482 "[%f], length [%f]\n", next_edl_record
->start_sec
,
2483 next_edl_record
->stop_sec
, next_edl_record
->length_sec
);
2485 else if (next_edl_record
->action
== EDL_MUTE
) {
2486 mpctx
->edl_muted
= !mpctx
->edl_muted
;
2487 if ((mpctx
->user_muted
| mpctx
->edl_muted
) != mpctx
->mixer
.muted
)
2488 mixer_mute(&mpctx
->mixer
);
2489 mp_msg(MSGT_CPLAYER
, MSGL_DBG4
, "EDL_MUTE: [%f]\n",
2490 next_edl_record
->start_sec
);
2492 next_edl_record
= next_edl_record
->next
;
2496 static void reinit_decoders(struct MPContext
*mpctx
)
2498 reinit_video_chain(mpctx
);
2499 reinit_audio_chain(mpctx
);
2500 mp_property_do("sub", M_PROPERTY_SET
, &mpctx
->global_sub_pos
, mpctx
);
2503 static bool timeline_set_part(struct MPContext
*mpctx
, int i
)
2505 struct timeline_part
*p
= mpctx
->timeline
+ mpctx
->timeline_part
;
2506 struct timeline_part
*n
= mpctx
->timeline
+ i
;
2507 mpctx
->timeline_part
= i
;
2508 mpctx
->video_offset
= n
->start
- n
->source_start
;
2509 if (n
->source
== p
->source
)
2511 uninit_player(mpctx
, INITIALIZED_VCODEC
| (mpctx
->opts
.fixed_vo
&& mpctx
->opts
.video_id
!= -2 ? 0 : INITIALIZED_VO
) | INITIALIZED_AO
| INITIALIZED_ACODEC
);
2512 mpctx
->demuxer
= n
->source
->demuxer
;
2513 mpctx
->d_video
= mpctx
->demuxer
->video
;
2514 mpctx
->d_audio
= mpctx
->demuxer
->audio
;
2515 mpctx
->d_sub
= mpctx
->demuxer
->sub
;
2516 mpctx
->sh_video
= mpctx
->d_video
->sh
;
2517 mpctx
->sh_audio
= mpctx
->d_audio
->sh
;
2521 // Given pts, switch playback to the corresponding part.
2522 // Return offset within that part.
2523 static double timeline_set_from_time(struct MPContext
*mpctx
, double pts
,
2528 for (int i
= 0; i
< mpctx
->num_timeline_parts
; i
++) {
2529 struct timeline_part
*p
= mpctx
->timeline
+ i
;
2530 if (pts
< (p
+1)->start
) {
2531 *need_reset
= timeline_set_part(mpctx
, i
);
2532 return pts
- p
->start
+ p
->source_start
;
2539 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2540 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2541 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2542 static int seek(MPContext
*mpctx
, double amount
, int style
)
2544 current_module
= "seek";
2545 if (mpctx
->stop_play
== AT_END_OF_FILE
)
2546 mpctx
->stop_play
= KEEP_PLAYING
;
2547 if (mpctx
->timeline
&& style
& SEEK_FACTOR
) {
2548 amount
*= mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
2549 style
&= ~SEEK_FACTOR
;
2551 if ((mpctx
->demuxer
->accurate_seek
|| mpctx
->timeline
) && mpctx
->sh_video
2552 && !(style
& (SEEK_ABSOLUTE
| SEEK_FACTOR
))) {
2553 style
|= SEEK_ABSOLUTE
;
2555 style
|= SEEK_FORWARD
;
2557 style
|= SEEK_BACKWARD
;
2558 amount
+= mpctx
->sh_video
->pts
;
2561 /* At least the liba52 decoder wants to read from the input stream
2562 * during initialization, so reinit must be done after the demux_seek()
2563 * call that clears possible stream EOF. */
2564 bool need_reset
= false;
2565 if (mpctx
->timeline
) {
2566 amount
= timeline_set_from_time(mpctx
, amount
, &need_reset
);
2568 mpctx
->stop_play
= AT_END_OF_FILE
;
2569 // Clear audio from current position
2570 if (mpctx
->sh_audio
) {
2571 mpctx
->audio_out
->reset();
2572 mpctx
->sh_audio
->a_buffer_len
= 0;
2573 mpctx
->sh_audio
->a_out_buffer_len
= 0;
2578 int seekresult
= demux_seek(mpctx
->demuxer
, amount
, audio_delay
, style
);
2580 reinit_decoders(mpctx
);
2581 if (seekresult
== 0)
2584 if (mpctx
->sh_video
) {
2585 current_module
= "seek_video_reset";
2586 resync_video_stream(mpctx
->sh_video
);
2587 if (mpctx
->video_out
->config_ok
)
2588 vo_control(mpctx
->video_out
, VOCTRL_RESET
, NULL
);
2589 mpctx
->sh_video
->num_buffered_pts
= 0;
2590 mpctx
->sh_video
->last_pts
= MP_NOPTS_VALUE
;
2591 mpctx
->num_buffered_frames
= 0;
2593 mpctx
->time_frame
= 0;
2594 mpctx
->update_video_immediately
= true;
2595 // Not all demuxers set d_video->pts during seek, so this value
2596 // (which is used by at least vobsub and edl code below) may
2597 // be completely wrong (probably 0).
2598 mpctx
->sh_video
->pts
= mpctx
->d_video
->pts
+ mpctx
->video_offset
;
2599 update_subtitles(mpctx
->sh_video
, mpctx
->d_sub
, mpctx
->video_offset
, 1);
2600 update_teletext(mpctx
->sh_video
, mpctx
->demuxer
, 1);
2603 if (mpctx
->sh_audio
) {
2604 current_module
= "seek_audio_reset";
2605 mpctx
->audio_out
->reset(); // stop audio, throwing away buffered data
2606 mpctx
->sh_audio
->a_buffer_len
= 0;
2607 mpctx
->sh_audio
->a_out_buffer_len
= 0;
2610 if (vo_vobsub
&& mpctx
->sh_video
) {
2611 current_module
= "seek_vobsub_reset";
2612 vobsub_seek(vo_vobsub
, mpctx
->sh_video
->pts
);
2615 edl_seek_reset(mpctx
);
2617 mpctx
->total_avsync_change
= 0;
2618 audio_time_usage
= 0; video_time_usage
= 0; vout_time_usage
= 0;
2621 current_module
= NULL
;
2625 int get_current_chapter(struct MPContext
*mpctx
)
2627 if (!mpctx
->chapters
|| !mpctx
->sh_video
)
2628 return demuxer_get_current_chapter(mpctx
->demuxer
);
2631 double current_pts
= mpctx
->sh_video
->pts
;
2632 for (i
= 1; i
< mpctx
->num_chapters
; i
++)
2633 if (current_pts
< mpctx
->chapters
[i
].start
)
2638 // currently returns a string allocated with malloc, not talloc
2639 char *chapter_display_name(struct MPContext
*mpctx
, int chapter
)
2641 if (!mpctx
->chapters
|| !mpctx
->sh_video
)
2642 return demuxer_chapter_display_name(mpctx
->demuxer
, chapter
);
2643 return strdup(mpctx
->chapters
[chapter
].name
);
2646 int seek_chapter(struct MPContext
*mpctx
, int chapter
, double *seek_pts
,
2647 char **chapter_name
)
2649 if (!mpctx
->chapters
|| !mpctx
->sh_video
)
2650 return demuxer_seek_chapter(mpctx
->demuxer
, chapter
, seek_pts
,
2652 if (chapter
>= mpctx
->num_chapters
)
2656 *seek_pts
= mpctx
->chapters
[chapter
].start
;
2658 *chapter_name
= talloc_strdup(NULL
, mpctx
->chapters
[chapter
].name
);
2662 static int find_ordered_chapter_sources(struct MPContext
*mpctx
,
2663 struct content_source
*sources
,
2665 unsigned char uid_map
[][16])
2667 int num_filenames
= 0;
2668 char **filenames
= NULL
;
2669 if (num_sources
> 1) {
2670 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "This file references data from "
2671 "other sources.\n");
2672 if (mpctx
->stream
->type
!= STREAMTYPE_FILE
) {
2673 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, "Playback source is not a "
2674 "normal disk file. Will not search for related files.\n");
2676 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Will scan other files in the "
2677 "same directory to find referenced sources.\n");
2678 filenames
= find_files(mpctx
->demuxer
->filename
, ".mkv",
2683 int num_left
= num_sources
- 1;
2684 for (int i
= 0; i
< num_filenames
&& num_left
> 0; i
++) {
2685 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Checking file %s\n",
2686 filename_recode(filenames
[i
]));
2688 struct stream
*s
= open_stream(filenames
[i
], &mpctx
->opts
, &format
);
2691 struct demuxer
*d
= demux_open(&mpctx
->opts
, s
, DEMUXER_TYPE_MATROSKA
,
2692 mpctx
->opts
.audio_id
,
2693 mpctx
->opts
.video_id
,
2694 mpctx
->opts
.sub_id
, filenames
[i
]);
2699 if (d
->file_format
== DEMUXER_TYPE_MATROSKA
) {
2700 for (int i
= 1; i
< num_sources
; i
++) {
2701 if (sources
[i
].demuxer
)
2703 if (!memcmp(uid_map
[i
], d
->matroska_data
.segment_uid
, 16)) {
2704 mp_msg(MSGT_CPLAYER
, MSGL_INFO
,"Match for source %d: %s\n",
2705 i
, filename_recode(d
->filename
));
2706 sources
[i
].stream
= s
;
2707 sources
[i
].demuxer
= d
;
2719 talloc_free(filenames
);
2721 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Failed to find ordered chapter part!\n"
2722 "There will be parts MISSING from the video!\n");
2723 for (int i
= 1, j
= 1; i
< num_sources
; i
++)
2724 if (sources
[i
].demuxer
) {
2725 sources
[j
] = sources
[i
];
2726 memcpy(uid_map
[j
], uid_map
[i
], 16);
2730 return num_sources
- num_left
;
2733 static void build_ordered_chapter_timeline(struct MPContext
*mpctx
)
2735 if (!mpctx
->opts
.ordered_chapters
) {
2736 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "File uses ordered chapters, but "
2737 "you have disabled support for them. Ignoring.\n");
2741 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "File uses ordered chapters, will build "
2742 "edit timeline.\n");
2744 struct demuxer
*demuxer
= mpctx
->demuxer
;
2745 struct matroska_data
*m
= &demuxer
->matroska_data
;
2747 struct content_source
*sources
= talloc_array_ptrtype(NULL
, sources
,
2748 m
->num_ordered_chapters
);
2749 sources
[0].stream
= mpctx
->stream
;
2750 sources
[0].demuxer
= mpctx
->demuxer
;
2751 unsigned char uid_map
[m
->num_ordered_chapters
][16];
2752 int num_sources
= 1;
2753 memcpy(uid_map
[0], m
->segment_uid
, 16);
2755 for (int i
= 0; i
< m
->num_ordered_chapters
; i
++) {
2756 struct matroska_chapter
*c
= m
->ordered_chapters
+ i
;
2757 if (!c
->has_segment_uid
)
2758 memcpy(c
->segment_uid
, m
->segment_uid
, 16);
2760 for (int j
= 0; j
< num_sources
; j
++)
2761 if (!memcmp(c
->segment_uid
, uid_map
[j
], 16))
2763 memcpy(uid_map
[num_sources
], c
->segment_uid
, 16);
2764 sources
[num_sources
] = (struct content_source
){};
2770 num_sources
= find_ordered_chapter_sources(mpctx
, sources
, num_sources
,
2774 struct timeline_part
*timeline
= talloc_array_ptrtype(NULL
, timeline
,
2775 m
->num_ordered_chapters
+ 1);
2776 struct chapter
*chapters
= talloc_array_ptrtype(NULL
, chapters
,
2777 m
->num_ordered_chapters
);
2778 uint64_t starttime
= 0;
2779 uint64_t missing_time
= 0;
2781 int num_chapters
= 0;
2782 for (int i
= 0; i
< m
->num_ordered_chapters
; i
++) {
2783 struct matroska_chapter
*c
= m
->ordered_chapters
+ i
;
2786 for (j
= 0; j
< num_sources
; j
++) {
2787 if (!memcmp(c
->segment_uid
, uid_map
[j
], 16))
2790 missing_time
+= c
->end
- c
->start
;
2793 chapters
[num_chapters
].start
= starttime
/ 1000.;
2794 chapters
[num_chapters
].name
= talloc_strdup(chapters
, c
->name
);
2795 // Only add a separate part if the time or file actually changes
2796 uint64_t prev_end
= !part_count
? 0 : starttime
2797 - timeline
[part_count
- 1].start
2798 + timeline
[part_count
- 1].source_start
;
2799 if (part_count
== 0 || c
->start
!= prev_end
2800 || sources
+ j
!= timeline
[part_count
- 1].source
) {
2801 timeline
[part_count
].source
= sources
+ j
;
2802 timeline
[part_count
].start
= chapters
[num_chapters
].start
;
2803 timeline
[part_count
].source_start
= c
->start
/ 1000.;
2806 starttime
+= c
->end
- c
->start
;
2809 timeline
[part_count
].start
= starttime
/ 1000.;
2812 // None of the parts come from the file itself???
2813 talloc_free(sources
);
2814 talloc_free(timeline
);
2815 talloc_free(chapters
);
2819 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Timeline contains %d parts from %d "
2820 "sources. Total length %.3f seconds.\n", part_count
, num_sources
,
2821 timeline
[part_count
].start
);
2823 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "There are %.3f seconds missing "
2824 "from the timeline!\n", missing_time
/ 1000.);
2825 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Source files:\n");
2826 for (int i
= 0; i
< num_sources
; i
++)
2827 mp_msg(MSGT_CPLAYER
, MSGL_V
, "%d: %s\n", i
,
2828 filename_recode(sources
[i
].demuxer
->filename
));
2829 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Timeline parts: (number, start, "
2830 "source_start, source):\n");
2831 for (int i
= 0; i
< part_count
; i
++) {
2832 struct timeline_part
*p
= timeline
+ i
;
2833 mp_msg(MSGT_CPLAYER
, MSGL_V
, "%3d %9.3f %9.3f %3td\n", i
, p
->start
,
2834 p
->source_start
, p
->source
- sources
);
2836 mp_msg(MSGT_CPLAYER
, MSGL_V
, "END %9.3f\n", timeline
[part_count
].start
);
2837 mpctx
->sources
= sources
;
2838 mpctx
->num_sources
= num_sources
;
2839 mpctx
->timeline
= timeline
;
2840 mpctx
->num_timeline_parts
= part_count
;
2841 mpctx
->num_chapters
= num_chapters
;
2842 mpctx
->chapters
= chapters
;
2844 mpctx
->timeline_part
= 0;
2845 mpctx
->video_offset
= timeline
[0].source_start
;
2846 mpctx
->demuxer
= timeline
[0].source
->demuxer
;
2850 static int read_keys(void *ctx
, int fd
)
2853 return mplayer_get_key(ctx
, 0);
2857 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2858 * file for some tools to link against. */
2859 #ifndef DISABLE_MAIN
2860 int main(int argc
,char* argv
[]){
2867 /* Flag indicating whether MPlayer should exit without playing anything. */
2870 //float a_frame=0; // Audio
2874 int gui_no_filename
=0;
2876 struct MPContext
*mpctx
= &(struct MPContext
){
2877 .osd_function
= OSD_PLAY
,
2878 .begin_skip
= MP_NOPTS_VALUE
,
2879 .play_tree_step
= 1,
2880 .global_sub_pos
= -1,
2881 .set_of_sub_pos
= -1,
2882 .file_format
= DEMUXER_TYPE_UNKNOWN
,
2887 srand(GetTimerMS());
2892 mpctx
->x11_state
= vo_x11_init_state();
2894 struct MPOpts
*opts
= &mpctx
->opts
;
2895 set_default_mplayer_options(opts
);
2896 // Create the config context and register the options
2897 mpctx
->mconfig
= m_config_new(opts
, cfg_include
);
2898 m_config_register_options(mpctx
->mconfig
,mplayer_opts
);
2899 mp_input_register_options(mpctx
->mconfig
);
2901 // Preparse the command line
2902 m_config_preparse_command_line(mpctx
->mconfig
,argc
,argv
);
2904 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2909 stream_tv_defaults
.immediate
= 1;
2912 if (argc
> 1 && argv
[1] &&
2913 (!strcmp(argv
[1], "-gui") || !strcmp(argv
[1], "-nogui"))) {
2914 use_gui
= !strcmp(argv
[1], "-gui");
2918 char *base
= strrchr(argv
[0], '/');
2920 base
= strrchr(argv
[0], '\\');
2923 if(strstr(base
, "gmplayer"))
2927 parse_cfgfiles(mpctx
, mpctx
->mconfig
);
2930 if ( use_gui
) cfg_read();
2933 mpctx
->playtree
= m_config_parse_mp_command_line(mpctx
->mconfig
, argc
, argv
);
2934 if(mpctx
->playtree
== NULL
)
2937 mpctx
->playtree
= play_tree_cleanup(mpctx
->playtree
);
2938 if(mpctx
->playtree
) {
2939 mpctx
->playtree_iter
= play_tree_iter_new(mpctx
->playtree
,mpctx
->mconfig
);
2940 if(mpctx
->playtree_iter
) {
2941 if(play_tree_iter_step(mpctx
->playtree_iter
,0,0) != PLAY_TREE_ITER_ENTRY
) {
2942 play_tree_iter_free(mpctx
->playtree_iter
);
2943 mpctx
->playtree_iter
= NULL
;
2945 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
,1);
2949 mpctx
->key_fifo
= mp_fifo_create(opts
);
2951 print_version("MPlayer");
2953 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_GUI)
2954 void *runningmplayer
= FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2955 if(runningmplayer
&& mpctx
->filename
&& use_gui
){
2956 COPYDATASTRUCT csData
;
2957 char file
[MAX_PATH
];
2958 char *filepart
= mpctx
->filename
;
2959 if(GetFullPathName(mpctx
->filename
, MAX_PATH
, file
, &filepart
)){
2961 csData
.cbData
= strlen(file
)*2;
2962 csData
.lpData
= file
;
2963 SendMessage(runningmplayer
, WM_COPYDATA
, (WPARAM
)runningmplayer
, (LPARAM
)&csData
);
2968 #if defined(__MINGW32__) || defined(__CYGWIN__)
2969 // stop Windows from showing all kinds of annoying error dialogs
2970 SetErrorMode(0x8003);
2971 // request 1ms timer resolution
2975 #ifdef CONFIG_PRIORITY
2981 mp_msg(MSGT_CPLAYER
,MSGL_WARN
,MSGTR_NoGui
);
2985 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
2986 if(use_gui
&& !vo_init()){
2987 mp_msg(MSGT_CPLAYER
,MSGL_WARN
,MSGTR_GuiNeedsX
);
2991 if (use_gui
&& mpctx
->playtree_iter
){
2992 char cwd
[PATH_MAX
+2];
2993 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2994 play_tree_iter_free(mpctx
->playtree_iter
);
2995 mpctx
->playtree_iter
=NULL
;
2997 if (getcwd(cwd
, PATH_MAX
) != (char *)NULL
)
3000 // Prefix relative paths with current working directory
3001 play_tree_add_bpf(mpctx
->playtree
, cwd
);
3003 // Import initital playtree into GUI.
3004 import_initial_playtree_into_gui(mpctx
->playtree
, mpctx
->mconfig
, enqueue
);
3006 #endif /* CONFIG_GUI */
3008 if(opts
->video_driver_list
&& strcmp(opts
->video_driver_list
[0],"help")==0){
3013 if(opts
->audio_driver_list
&& strcmp(opts
->audio_driver_list
[0],"help")==0){
3018 /* Check codecs.conf. */
3019 if(!codecs_file
|| !parse_codec_cfg(codecs_file
)){
3020 if(!parse_codec_cfg(mem_ptr
=get_path("codecs.conf"))){
3021 if(!parse_codec_cfg(MPLAYER_CONFDIR
"/codecs.conf")){
3022 if(!parse_codec_cfg(NULL
)){
3023 exit_player_with_rc(mpctx
, EXIT_NONE
, 0);
3025 mp_msg(MSGT_CPLAYER
,MSGL_V
,MSGTR_BuiltinCodecsConf
);
3028 free( mem_ptr
); // release the buffer created by get_path()
3032 if(video_codec_list
){
3034 video_codec
=video_codec_list
[0];
3035 for(i
=0;video_codec_list
[i
];i
++)
3036 mp_msg(MSGT_FIXME
,MSGL_FIXME
,"vc#%d: '%s'\n",i
,video_codec_list
[i
]);
3039 if(audio_codec_list
&& strcmp(audio_codec_list
[0],"help")==0){
3040 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, MSGTR_AvailableAudioCodecs
);
3041 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_CODECS\n");
3043 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3046 if(video_codec_list
&& strcmp(video_codec_list
[0],"help")==0){
3047 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, MSGTR_AvailableVideoCodecs
);
3048 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_CODECS\n");
3050 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3053 if(video_fm_list
&& strcmp(video_fm_list
[0],"help")==0){
3055 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3058 if(audio_fm_list
&& strcmp(audio_fm_list
[0],"help")==0){
3060 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3063 if(af_cfg
.list
&& strcmp(af_cfg
.list
[0],"help")==0){
3069 if(vo_fstype_list
&& strcmp(vo_fstype_list
[0],"help")==0){
3071 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3075 if((demuxer_name
&& strcmp(demuxer_name
,"help")==0) ||
3076 (audio_demuxer_name
&& strcmp(audio_demuxer_name
,"help")==0) ||
3077 (sub_demuxer_name
&& strcmp(sub_demuxer_name
,"help")==0)){
3079 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "\n");
3082 if(list_properties
) {
3083 property_print_help();
3088 exit_player(mpctx
, EXIT_NONE
);
3090 if (player_idle_mode
&& use_gui
) {
3091 mp_msg(MSGT_CPLAYER
, MSGL_FATAL
, MSGTR_NoIdleAndGui
);
3092 exit_player_with_rc(mpctx
, EXIT_NONE
, 1);
3095 if(!mpctx
->filename
&& !player_idle_mode
){
3097 // no file/vcd/dvd -> show HELP:
3098 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, help_text
);
3099 exit_player_with_rc(mpctx
, EXIT_NONE
, 0);
3100 } else gui_no_filename
=1;
3103 /* Display what configure line was used */
3104 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Configuration: " CONFIGURATION
"\n");
3106 // Many users forget to include command line in bugreports...
3107 if( mp_msg_test(MSGT_CPLAYER
,MSGL_V
) ){
3108 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, MSGTR_CommandLine
);
3109 for(i
=1;i
<argc
;i
++)mp_msg(MSGT_CPLAYER
, MSGL_INFO
," '%s'",argv
[i
]);
3110 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "\n");
3113 //------ load global data first ------
3115 mpctx
->osd
= osd_create();
3118 #ifdef CONFIG_FREETYPE
3121 #ifdef CONFIG_FONTCONFIG
3122 if(font_fontconfig
<= 0)
3125 #ifdef CONFIG_BITMAP_FONT
3127 vo_font
=read_font_desc(font_name
,font_factor
,verbose
>1);
3128 if(!vo_font
) mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_CantLoadFont
,
3129 filename_recode(font_name
));
3132 vo_font
=read_font_desc( mem_ptr
=get_path("font/font.desc"),font_factor
,verbose
>1);
3133 free(mem_ptr
); // release the buffer created by get_path()
3135 vo_font
=read_font_desc(MPLAYER_DATADIR
"/font/font.desc",font_factor
,verbose
>1);
3138 mpctx
->osd
->sub_font
= read_font_desc(sub_font_name
, font_factor
, verbose
>1);
3140 mpctx
->osd
->sub_font
= vo_font
;
3142 #ifdef CONFIG_FONTCONFIG
3147 ass_library
= ass_init();
3153 // seteuid(0); /* Can't hurt to try to get root here */
3154 if ((rtc_fd
= open(rtc_device
? rtc_device
: "/dev/rtc", O_RDONLY
)) < 0)
3155 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, MSGTR_RTCDeviceNotOpenable
,
3156 rtc_device
? rtc_device
: "/dev/rtc", strerror(errno
));
3158 unsigned long irqp
= 1024; /* 512 seemed OK. 128 is jerky. */
3160 if (ioctl(rtc_fd
, RTC_IRQP_SET
, irqp
) < 0) {
3161 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, MSGTR_LinuxRTCInitErrorIrqpSet
, irqp
, strerror(errno
));
3162 mp_msg(MSGT_CPLAYER
, MSGL_HINT
, MSGTR_IncreaseRTCMaxUserFreq
, irqp
);
3165 } else if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
3166 /* variable only by the root */
3167 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, MSGTR_LinuxRTCInitErrorPieOn
, strerror(errno
));
3171 mp_msg(MSGT_CPLAYER
, MSGL_V
, MSGTR_UsingRTCTiming
, irqp
);
3175 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
3176 // and now ? -- Pontscho
3177 if(use_gui
) setuid( getuid() ); // strongly test, please check this.
3180 #endif /* HAVE_RTC */
3181 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Using %s timing\n",
3182 softsleep
?"software":timer_name
);
3185 if ( !use_gui
) load_termcap(NULL
); // load key-codes
3188 // ========== Init keyboard FIFO (connection to libvo) ============
3190 // Init input system
3191 current_module
= "init_input";
3192 mpctx
->input
= mp_input_init(&opts
->input
, use_gui
);
3193 mp_input_add_key_fd(mpctx
->input
, -1,0,mplayer_get_key
,NULL
, mpctx
->key_fifo
);
3195 mp_input_add_cmd_fd(mpctx
->input
, 0,USE_SELECT
,MP_INPUT_SLAVE_CMD_FUNC
,NULL
);
3196 else if(!noconsolecontrols
)
3197 mp_input_add_key_fd(mpctx
->input
, 0, 1, read_keys
, NULL
, mpctx
->key_fifo
);
3198 // Set the libstream interrupt callback
3199 stream_set_interrupt_callback(mp_input_check_interrupt
, mpctx
->input
);
3203 if(menu_cfg
&& menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
, menu_cfg
))
3204 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_MenuInitialized
, menu_cfg
);
3206 menu_cfg
= get_path("menu.conf");
3207 if(menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
, menu_cfg
))
3208 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_MenuInitialized
, menu_cfg
);
3210 if(menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
,
3211 MPLAYER_CONFDIR
"/menu.conf"))
3212 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_MenuInitialized
, MPLAYER_CONFDIR
"/menu.conf");
3214 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_MenuInitFailed
);
3222 current_module
= NULL
;
3226 signal(SIGCHLD
,child_sighandler
);
3229 #ifdef CONFIG_CRASH_DEBUG
3230 prog_path
= argv
[0];
3232 //========= Catch terminate signals: ================
3233 // terminate requests:
3234 signal(SIGTERM
,exit_sighandler
); // kill
3235 signal(SIGHUP
,exit_sighandler
); // kill -HUP / xterm closed
3237 signal(SIGINT
,exit_sighandler
); // Interrupt from keyboard
3239 signal(SIGQUIT
,exit_sighandler
); // Quit from keyboard
3240 signal(SIGPIPE
,exit_sighandler
); // Some window managers cause this
3241 #ifdef CONFIG_SIGHANDLER
3243 signal(SIGBUS
,exit_sighandler
); // bus error
3244 signal(SIGSEGV
,exit_sighandler
); // segfault
3245 signal(SIGILL
,exit_sighandler
); // illegal instruction
3246 signal(SIGFPE
,exit_sighandler
); // floating point exc.
3247 signal(SIGABRT
,exit_sighandler
); // abort()
3248 #ifdef CONFIG_CRASH_DEBUG
3250 signal(SIGTRAP
,exit_sighandler
);
3257 guiGetEvent(guiSetContext
, mpctx
);
3258 mpctx
->initialized_flags
|=INITIALIZED_GUI
;
3259 guiGetEvent( guiCEvent
,(char *)((gui_no_filename
) ? 0 : 1) );
3263 // ******************* Now, let's see the per-file stuff ********************
3267 // init global sub numbers
3268 mpctx
->global_sub_size
= 0;
3269 { int i
; for (i
= 0; i
< SUB_SOURCES
; i
++) mpctx
->global_sub_indices
[i
] = -1; }
3271 if (mpctx
->filename
) {
3272 load_per_protocol_config (mpctx
->mconfig
, mpctx
->filename
);
3273 load_per_extension_config (mpctx
->mconfig
, mpctx
->filename
);
3274 load_per_file_config (mpctx
->mconfig
, mpctx
->filename
);
3277 if (opts
->video_driver_list
)
3278 load_per_output_config (mpctx
->mconfig
, PROFILE_CFG_VO
, opts
->video_driver_list
[0]);
3279 if (opts
->audio_driver_list
)
3280 load_per_output_config (mpctx
->mconfig
, PROFILE_CFG_AO
, opts
->audio_driver_list
[0]);
3282 // We must enable getch2 here to be able to interrupt network connection
3284 if(!noconsolecontrols
&& !slave_mode
){
3285 if(mpctx
->initialized_flags
&INITIALIZED_GETCH2
)
3286 mp_msg(MSGT_CPLAYER
,MSGL_WARN
,MSGTR_Getch2InitializedTwice
);
3288 getch2_enable(); // prepare stdin for hotkeys...
3289 mpctx
->initialized_flags
|=INITIALIZED_GETCH2
;
3290 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n[[[init getch2]]]\n");
3293 // =================== GUI idle loop (STOP state) ===========================
3296 mpctx
->file_format
=DEMUXER_TYPE_UNKNOWN
;
3297 guiGetEvent( guiSetDefaults
,0 );
3298 while ( guiIntfStruct
.Playing
!= 1 )
3303 guiGetEvent( guiReDraw
,NULL
);
3304 if ( (cmd
= mp_input_get_cmd(mpctx
->input
, 0,0,0)) != NULL
) {
3305 guiGetEvent(guiIEvent
, (char *)cmd
->id
);
3309 guiGetEvent( guiSetParameters
,NULL
);
3310 if ( guiIntfStruct
.StreamType
== STREAMTYPE_STREAM
)
3312 play_tree_t
* entry
= play_tree_new();
3313 play_tree_add_file( entry
,guiIntfStruct
.Filename
);
3314 if ( mpctx
->playtree
) play_tree_free_list( mpctx
->playtree
->child
,1 );
3315 else mpctx
->playtree
=play_tree_new();
3316 play_tree_set_child( mpctx
->playtree
,entry
);
3319 mpctx
->playtree_iter
= play_tree_iter_new(mpctx
->playtree
,mpctx
->mconfig
);
3320 if(mpctx
->playtree_iter
)
3322 if(play_tree_iter_step(mpctx
->playtree_iter
,0,0) != PLAY_TREE_ITER_ENTRY
)
3324 play_tree_iter_free(mpctx
->playtree_iter
);
3325 mpctx
->playtree_iter
= NULL
;
3327 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
,1);
3332 #endif /* CONFIG_GUI */
3334 while (player_idle_mode
&& !mpctx
->filename
) {
3335 play_tree_t
* entry
= NULL
;
3337 while (!(cmd
= mp_input_get_cmd(mpctx
->input
, 0,1,0))) { // wait for command
3338 if (mpctx
->video_out
)
3339 vo_check_events(mpctx
->video_out
);
3343 case MP_CMD_LOADFILE
:
3344 // prepare a tree entry with the new filename
3345 entry
= play_tree_new();
3346 play_tree_add_file(entry
, cmd
->args
[0].v
.s
);
3347 // The entry is added to the main playtree after the switch().
3349 case MP_CMD_LOADLIST
:
3350 entry
= parse_playlist_file(mpctx
->mconfig
, cmd
->args
[0].v
.s
);
3353 exit_player_with_rc(mpctx
, EXIT_QUIT
, (cmd
->nargs
> 0)? cmd
->args
[0].v
.i
: 0);
3355 case MP_CMD_GET_PROPERTY
:
3356 case MP_CMD_SET_PROPERTY
:
3357 case MP_CMD_STEP_PROPERTY
:
3358 run_command(mpctx
, cmd
);
3364 if (entry
) { // user entered a command that gave a valid entry
3365 if (mpctx
->playtree
) // the playtree is always a node with one child. let's clear it
3366 play_tree_free_list(mpctx
->playtree
->child
, 1);
3367 else mpctx
->playtree
=play_tree_new(); // .. or make a brand new playtree
3369 if (!mpctx
->playtree
) continue; // couldn't make playtree! wait for next command
3371 play_tree_set_child(mpctx
->playtree
, entry
);
3373 /* Make iterator start at the top the of tree. */
3374 mpctx
->playtree_iter
= play_tree_iter_new(mpctx
->playtree
, mpctx
->mconfig
);
3375 if (!mpctx
->playtree_iter
) continue;
3377 // find the first real item in the tree
3378 if (play_tree_iter_step(mpctx
->playtree_iter
,0,0) != PLAY_TREE_ITER_ENTRY
) {
3380 play_tree_iter_free(mpctx
->playtree_iter
);
3381 mpctx
->playtree_iter
= NULL
;
3382 continue; // wait for next command
3384 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
, 1);
3387 //---------------------------------------------------------------------------
3390 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_Playing
,
3391 filename_recode(mpctx
->filename
));
3394 if (edl_records
) free_edl(edl_records
);
3395 next_edl_record
= edl_records
= edl_parse_file();
3397 if (edl_output_filename
) {
3398 if (edl_fd
) fclose(edl_fd
);
3399 if ((edl_fd
= fopen(edl_output_filename
, "w")) == NULL
)
3401 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, MSGTR_EdlCantOpenForWrite
,
3402 filename_recode(edl_output_filename
));
3406 //==================== Open VOB-Sub ============================
3408 current_module
="vobsub";
3410 vo_vobsub
=vobsub_open(vobsub_name
,spudec_ifo
,1,&vo_spudec
);
3412 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_CantLoadSub
,
3413 filename_recode(vobsub_name
));
3414 } else if (sub_auto
&& mpctx
->filename
){
3415 /* try to autodetect vobsub from movie filename ::atmos */
3416 char *buf
= strdup(mpctx
->filename
), *psub
;
3417 char *pdot
= strrchr(buf
, '.');
3418 char *pslash
= strrchr(buf
, '/');
3419 #if defined(__MINGW32__) || defined(__CYGWIN__)
3420 if (!pslash
) pslash
= strrchr(buf
, '\\');
3422 if (pdot
&& (!pslash
|| pdot
> pslash
))
3424 vo_vobsub
=vobsub_open(buf
,spudec_ifo
,0,&vo_spudec
);
3425 /* try from ~/.mplayer/sub */
3426 if(!vo_vobsub
&& (psub
= get_path( "sub/" ))) {
3429 bname
= strrchr(buf
,'/');
3430 #if defined(__MINGW32__) || defined(__CYGWIN__)
3431 if(!bname
) bname
= strrchr(buf
,'\\');
3435 l
= strlen(psub
) + strlen(bname
) + 1;
3436 psub
= realloc(psub
,l
);
3438 vo_vobsub
=vobsub_open(psub
,spudec_ifo
,0,&vo_spudec
);
3444 mpctx
->initialized_flags
|=INITIALIZED_VOBSUB
;
3445 vobsub_set_from_lang(vo_vobsub
, dvdsub_lang
);
3446 mp_property_do("sub_forced_only", M_PROPERTY_SET
, &forced_subs_only
, mpctx
);
3448 // setup global sub numbering
3449 mpctx
->global_sub_indices
[SUB_SOURCE_VOBSUB
] = mpctx
->global_sub_size
; // the global # of the first vobsub.
3450 mpctx
->global_sub_size
+= vobsub_get_indexes_count(vo_vobsub
);
3453 //============ Open & Sync STREAM --- fork cache2 ====================
3456 mpctx
->demuxer
=NULL
;
3457 if (mpctx
->d_audio
) {
3458 //free_demuxer_stream(mpctx->d_audio);
3459 mpctx
->d_audio
=NULL
;
3461 if (mpctx
->d_video
) {
3462 //free_demuxer_stream(d_video);
3463 mpctx
->d_video
=NULL
;
3465 mpctx
->sh_audio
=NULL
;
3466 mpctx
->sh_video
=NULL
;
3468 current_module
="open_stream";
3469 mpctx
->stream
= open_stream(mpctx
->filename
, opts
, &mpctx
->file_format
);
3470 if(!mpctx
->stream
) { // error...
3471 mpctx
->stop_play
= libmpdemux_was_interrupted(mpctx
, PT_NEXT_ENTRY
);
3472 goto goto_next_file
;
3474 mpctx
->initialized_flags
|=INITIALIZED_STREAM
;
3477 if ( use_gui
) guiGetEvent( guiSetStream
,(char *)mpctx
->stream
);
3480 if(mpctx
->file_format
== DEMUXER_TYPE_PLAYLIST
) {
3481 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "\nThis looks like a playlist, but "
3482 "playlist support will not be used automatically.\n"
3483 "MPlayer's playlist code is unsafe and should only be used with "
3484 "trusted sources.\nPlayback will probably fail.\n\n");
3488 current_module
="handle_playlist";
3489 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Parsing playlist %s...\n",
3490 filename_recode(mpctx
->filename
));
3491 entry
= parse_playtree(mpctx
->stream
, mpctx
->mconfig
, 0);
3492 mpctx
->eof
=playtree_add_playlist(mpctx
, entry
);
3493 goto goto_next_file
;
3496 mpctx
->stream
->start_pos
+=seek_to_byte
;
3498 if(stream_dump_type
==5){
3499 unsigned char buf
[4096];
3502 current_module
="dumpstream";
3503 if(mpctx
->stream
->type
==STREAMTYPE_STREAM
&& mpctx
->stream
->fd
<0){
3504 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_DumpstreamFdUnavailable
);
3505 exit_player(mpctx
, EXIT_ERROR
);
3507 stream_reset(mpctx
->stream
);
3508 stream_seek(mpctx
->stream
,mpctx
->stream
->start_pos
);
3509 f
=fopen(stream_dump_name
,"wb");
3511 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_CantOpenDumpfile
);
3512 exit_player(mpctx
, EXIT_ERROR
);
3514 if (dvd_chapter
> 1) {
3515 int chapter
= dvd_chapter
- 1;
3516 stream_control(mpctx
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
, &chapter
);
3518 while(!mpctx
->stream
->eof
&& !async_quit_request
){
3519 len
=stream_read(mpctx
->stream
,buf
,4096);
3521 if(fwrite(buf
,len
,1,f
) != 1) {
3522 mp_msg(MSGT_MENCODER
,MSGL_FATAL
,MSGTR_ErrorWritingFile
,stream_dump_name
);
3523 exit_player(mpctx
, EXIT_ERROR
);
3526 if(dvd_last_chapter
> 0) {
3528 if (stream_control(mpctx
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
3529 &chapter
) == STREAM_OK
&& chapter
+ 1 > dvd_last_chapter
)
3534 mp_msg(MSGT_MENCODER
,MSGL_FATAL
,MSGTR_ErrorWritingFile
,stream_dump_name
);
3535 exit_player(mpctx
, EXIT_ERROR
);
3537 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_CoreDumped
);
3538 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
3541 #ifdef CONFIG_DVDREAD
3542 if(mpctx
->stream
->type
==STREAMTYPE_DVD
){
3543 current_module
="dvd lang->id";
3544 if(opts
->audio_id
==-1) opts
->audio_id
=dvd_aid_from_lang(mpctx
->stream
,audio_lang
);
3545 if(dvdsub_lang
&& opts
->sub_id
==-2) opts
->sub_id
=-1;
3546 if(dvdsub_lang
&& opts
->sub_id
==-1) opts
->sub_id
=dvd_sid_from_lang(mpctx
->stream
,dvdsub_lang
);
3547 // setup global sub numbering
3548 mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] = mpctx
->global_sub_size
; // the global # of the first demux-specific sub.
3549 mpctx
->global_sub_size
+= dvd_number_of_subs(mpctx
->stream
);
3550 current_module
=NULL
;
3554 #ifdef CONFIG_DVDNAV
3555 if(mpctx
->stream
->type
==STREAMTYPE_DVDNAV
){
3556 current_module
="dvdnav lang->id";
3557 if(opts
->audio_id
==-1) opts
->audio_id
=mp_dvdnav_aid_from_lang(mpctx
->stream
,audio_lang
);
3558 if(dvdsub_lang
&& opts
->sub_id
==-2) opts
->sub_id
=-1;
3559 if(dvdsub_lang
&& opts
->sub_id
==-1) opts
->sub_id
=mp_dvdnav_sid_from_lang(mpctx
->stream
,dvdsub_lang
);
3560 // setup global sub numbering
3561 mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] = mpctx
->global_sub_size
; // the global # of the first demux-specific sub.
3562 mpctx
->global_sub_size
+= mp_dvdnav_number_of_subs(mpctx
->stream
);
3563 current_module
=NULL
;
3567 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3569 if(stream_cache_size
>0){
3570 current_module
="enable_cache";
3571 if(!stream_enable_cache(mpctx
->stream
,stream_cache_size
*1024,
3572 stream_cache_size
*1024*(stream_cache_min_percent
/ 100.0),
3573 stream_cache_size
*1024*(stream_cache_seek_min_percent
/ 100.0)))
3574 if((mpctx
->stop_play
= libmpdemux_was_interrupted(mpctx
, PT_NEXT_ENTRY
))) goto goto_next_file
;
3577 //============ Open DEMUXERS --- DETECT file type =======================
3578 current_module
="demux_open";
3580 mpctx
->demuxer
=demux_open(opts
, mpctx
->stream
,mpctx
->file_format
,opts
->audio_id
,opts
->video_id
,opts
->sub_id
,mpctx
->filename
);
3582 // HACK to get MOV Reference Files working
3584 if (mpctx
->demuxer
&& mpctx
->demuxer
->type
==DEMUXER_TYPE_PLAYLIST
)
3586 unsigned char* playlist_entry
;
3587 play_tree_t
*list
= NULL
, *entry
= NULL
;
3589 current_module
="handle_demux_playlist";
3590 while (ds_get_packet(mpctx
->demuxer
->video
,&playlist_entry
)>0)
3594 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Adding file %s to element entry.\n",
3595 filename_recode(playlist_entry
));
3597 bname
=mp_basename(playlist_entry
);
3598 if ((strlen(bname
)>10) && !strncmp(bname
,"qt",2) && !strncmp(bname
+3,"gateQT",6))
3601 if (!strncmp(bname
,mp_basename(mpctx
->filename
),strlen(bname
))) // ignoring self-reference
3604 entry
= play_tree_new();
3606 if (mpctx
->filename
&& !strcmp(mp_basename(playlist_entry
),playlist_entry
)) // add reference path of current file
3608 temp
=malloc((strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
))+strlen(playlist_entry
)+1));
3611 strncpy(temp
, mpctx
->filename
, strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
)));
3612 temp
[strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
))]='\0';
3613 strcat(temp
, playlist_entry
);
3614 play_tree_add_file(entry
,temp
);
3615 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Resolving reference to %s.\n",temp
);
3620 play_tree_add_file(entry
,playlist_entry
);
3625 play_tree_append_entry(list
,entry
);
3627 free_demuxer(mpctx
->demuxer
);
3628 mpctx
->demuxer
= NULL
;
3632 entry
= play_tree_new();
3633 play_tree_set_child(entry
,list
);
3634 mpctx
->stop_play
= playtree_add_playlist(mpctx
, entry
);
3635 goto goto_next_file
;
3640 goto goto_next_file
;
3642 if (mpctx
->demuxer
->matroska_data
.ordered_chapters
)
3643 build_ordered_chapter_timeline(mpctx
);
3645 if (!mpctx
->sources
) {
3646 mpctx
->sources
= talloc_ptrtype(NULL
, mpctx
->sources
);
3647 *mpctx
->sources
= (struct content_source
){.stream
= mpctx
->stream
,
3648 .demuxer
= mpctx
->demuxer
};
3649 mpctx
->num_sources
= 1;
3654 if (seek_chapter(mpctx
, dvd_chapter
-1, &pts
, NULL
) >= 0 && pts
> -1.0)
3655 seek(mpctx
, pts
, SEEK_ABSOLUTE
);
3658 mpctx
->initialized_flags
|=INITIALIZED_DEMUXER
;
3660 if (mpctx
->stream
->type
!= STREAMTYPE_DVD
&& mpctx
->stream
->type
!= STREAMTYPE_DVDNAV
) {
3663 // setup global sub numbering
3664 mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] = mpctx
->global_sub_size
; // the global # of the first demux-specific sub.
3665 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
3666 if (mpctx
->demuxer
->s_streams
[i
])
3667 maxid
= FFMAX(maxid
, mpctx
->demuxer
->s_streams
[i
]->sid
);
3668 mpctx
->global_sub_size
+= maxid
+ 1;
3670 // Make opts->sub_id always selectable if set.
3671 if (mpctx
->global_sub_size
<= mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] + opts
->sub_id
)
3672 mpctx
->global_sub_size
= mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] + opts
->sub_id
+ 1;
3675 if (ass_enabled
&& ass_library
) {
3676 for (int j
= 0; j
< mpctx
->num_sources
; j
++) {
3677 struct demuxer
*d
= mpctx
->sources
[j
].demuxer
;
3678 for (int i
= 0; i
< d
->num_attachments
; i
++) {
3679 struct demux_attachment
*att
= d
->attachments
+ i
;
3680 if (extract_embedded_fonts
3681 && att
->name
&& att
->type
&& att
->data
&& att
->data_size
3682 && (strcmp(att
->type
, "application/x-truetype-font") == 0
3683 || strcmp(att
->type
, "application/x-font") == 0))
3684 ass_add_font(ass_library
, att
->name
, att
->data
, att
->data_size
);
3690 current_module
="demux_open2";
3692 //file_format=demuxer->file_format;
3694 mpctx
->d_audio
=mpctx
->demuxer
->audio
;
3695 mpctx
->d_video
=mpctx
->demuxer
->video
;
3696 mpctx
->d_sub
=mpctx
->demuxer
->sub
;
3698 // select audio stream
3699 select_audio(mpctx
->demuxer
, opts
->audio_id
, audio_lang
);
3702 if((stream_dump_type
)&&(stream_dump_type
<4)){
3704 demux_stream_t
*ds
=NULL
;
3705 current_module
="dump";
3706 // select stream to dump
3707 switch(stream_dump_type
){
3708 case 1: ds
=mpctx
->d_audio
;break;
3709 case 2: ds
=mpctx
->d_video
;break;
3710 case 3: ds
=mpctx
->d_sub
;break;
3713 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_DumpSelectedStreamMissing
);
3714 exit_player(mpctx
, EXIT_ERROR
);
3716 // disable other streams:
3717 if(mpctx
->d_audio
&& mpctx
->d_audio
!=ds
) {ds_free_packs(mpctx
->d_audio
); mpctx
->d_audio
->id
=-2; }
3718 if(mpctx
->d_video
&& mpctx
->d_video
!=ds
) {ds_free_packs(mpctx
->d_video
); mpctx
->d_video
->id
=-2; }
3719 if(mpctx
->d_sub
&& mpctx
->d_sub
!=ds
) {ds_free_packs(mpctx
->d_sub
); mpctx
->d_sub
->id
=-2; }
3721 f
=fopen(stream_dump_name
,"wb");
3723 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_CantOpenDumpfile
);
3724 exit_player(mpctx
, EXIT_ERROR
);
3727 unsigned char* start
;
3728 int in_size
=ds_get_packet(ds
,&start
);
3729 if( (mpctx
->demuxer
->file_format
==DEMUXER_TYPE_AVI
|| mpctx
->demuxer
->file_format
==DEMUXER_TYPE_ASF
|| mpctx
->demuxer
->file_format
==DEMUXER_TYPE_MOV
)
3730 && stream_dump_type
==2) fwrite(&in_size
,1,4,f
);
3731 if(in_size
>0) fwrite(start
,in_size
,1,f
);
3732 if(dvd_last_chapter
>0) {
3733 int cur_chapter
= demuxer_get_current_chapter(mpctx
->demuxer
);
3734 if(cur_chapter
!=-1 && cur_chapter
+1>dvd_last_chapter
)
3739 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_CoreDumped
);
3740 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
3743 mpctx
->sh_audio
=mpctx
->d_audio
->sh
;
3744 mpctx
->sh_video
=mpctx
->d_video
->sh
;
3746 if(mpctx
->sh_video
){
3748 current_module
="video_read_properties";
3749 if(!video_read_properties(mpctx
->sh_video
)) {
3750 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_CannotReadVideoProperties
);
3751 mpctx
->sh_video
=mpctx
->d_video
->sh
=NULL
;
3753 mp_msg(MSGT_CPLAYER
,MSGL_V
,MSGTR_FilefmtFourccSizeFpsFtime
,
3754 mpctx
->demuxer
->file_format
,mpctx
->sh_video
->format
, mpctx
->sh_video
->disp_w
,mpctx
->sh_video
->disp_h
,
3755 mpctx
->sh_video
->fps
,mpctx
->sh_video
->frametime
3758 /* need to set fps here for output encoders to pick it up in their init */
3760 mpctx
->sh_video
->fps
=force_fps
;
3761 mpctx
->sh_video
->frametime
=1.0f
/mpctx
->sh_video
->fps
;
3763 vo_fps
= mpctx
->sh_video
->fps
;
3765 if(!mpctx
->sh_video
->fps
&& !force_fps
){
3766 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_FPSnotspecified
);
3767 mpctx
->sh_video
=mpctx
->d_video
->sh
=NULL
;
3773 if(!mpctx
->sh_video
&& !mpctx
->sh_audio
){
3774 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
, MSGTR_NoStreamFound
);
3776 if(mpctx
->stream
->type
== STREAMTYPE_DVB
)
3779 int v
= mpctx
->last_dvb_step
;
3781 dir
= DVB_CHANNEL_HIGHER
;
3783 dir
= DVB_CHANNEL_LOWER
;
3785 if(dvb_step_channel(mpctx
->stream
, dir
)) {
3786 mpctx
->stop_play
= PT_NEXT_ENTRY
;
3787 mpctx
->dvbin_reopen
= 1;
3791 goto goto_next_file
; // exit_player(MSGTR_Exit_error);
3794 /* display clip info */
3795 demux_info_print(mpctx
->demuxer
);
3797 //================== Read SUBTITLES (DVD & TEXT) ==========================
3798 if(vo_spudec
==NULL
&& mpctx
->sh_video
&&
3799 (mpctx
->stream
->type
==STREAMTYPE_DVD
|| mpctx
->stream
->type
== STREAMTYPE_DVDNAV
)){
3800 init_vo_spudec(mpctx
);
3803 if(mpctx
->sh_video
) {
3804 // after reading video params we should load subtitles because
3805 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3807 current_module
="read_subtitles_file";
3809 for (i
= 0; sub_name
[i
] != NULL
; ++i
)
3810 add_subtitles(mpctx
, sub_name
[i
], mpctx
->sh_video
->fps
, 0);
3812 if(sub_auto
) { // auto load sub file ...
3813 char *psub
= get_path( "sub/" );
3814 char **tmp
= sub_filenames((psub
? psub
: ""), mpctx
->filename
);
3816 free(psub
); // release the buffer created by get_path() above
3818 add_subtitles(mpctx
, tmp
[i
], mpctx
->sh_video
->fps
, 1);
3823 if (mpctx
->set_of_sub_size
> 0) {
3824 // setup global sub numbering
3825 mpctx
->global_sub_indices
[SUB_SOURCE_SUBS
] = mpctx
->global_sub_size
; // the global # of the first sub.
3826 mpctx
->global_sub_size
+= mpctx
->set_of_sub_size
;
3830 if (mpctx
->global_sub_size
) {
3831 // find the best sub to use
3832 int vobsub_index_id
= vobsub_get_index_by_id(vo_vobsub
, vobsub_id
);
3833 mpctx
->global_sub_pos
= -1; // no subs by default
3834 if (vobsub_index_id
>= 0) {
3835 // if user asks for a vobsub id, use that first.
3836 mpctx
->global_sub_pos
= mpctx
->global_sub_indices
[SUB_SOURCE_VOBSUB
] + vobsub_index_id
;
3837 } else if (opts
->sub_id
>= 0 && mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] >= 0) {
3838 // if user asks for a dvd sub id, use that next.
3839 mpctx
->global_sub_pos
= mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] + opts
->sub_id
;
3840 } else if (mpctx
->global_sub_indices
[SUB_SOURCE_SUBS
] >= 0) {
3841 // if there are text subs to use, use those. (autosubs come last here)
3842 mpctx
->global_sub_pos
= mpctx
->global_sub_indices
[SUB_SOURCE_SUBS
];
3843 } else if (opts
->sub_id
< 0 && mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] >= 0) {
3844 // finally select subs by language and container hints
3845 if (opts
->sub_id
< 0 && dvdsub_lang
)
3846 opts
->sub_id
= demuxer_sub_track_by_lang(mpctx
->demuxer
, dvdsub_lang
);
3847 if (opts
->sub_id
< 0)
3848 opts
->sub_id
= demuxer_default_sub_track(mpctx
->demuxer
);
3849 if (opts
->sub_id
>= 0)
3850 mpctx
->global_sub_pos
= mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] + opts
->sub_id
;
3852 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3853 mpctx
->global_sub_pos
--;
3854 mp_property_do("sub",M_PROPERTY_STEP_UP
,NULL
, mpctx
);
3856 switch (stream_dump_type
) {
3857 case 3: list_sub_file(subdata
); break;
3858 case 4: dump_mpsub(subdata
, mpctx
->sh_video
->fps
); break;
3859 case 6: dump_srt(subdata
, mpctx
->sh_video
->fps
); break;
3860 case 7: dump_microdvd(subdata
, mpctx
->sh_video
->fps
); break;
3861 case 8: dump_jacosub(subdata
, mpctx
->sh_video
->fps
); break;
3862 case 9: dump_sami(subdata
, mpctx
->sh_video
->fps
); break;
3866 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_FILENAME=%s\n",
3867 filename_recode(mpctx
->filename
));
3868 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_DEMUXER=%s\n", mpctx
->demuxer
->desc
->name
);
3869 if (mpctx
->sh_video
) {
3870 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3871 if (mpctx
->sh_video
->format
>= 0x20202020)
3872 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx
->sh_video
->format
);
3874 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FORMAT=0x%08X\n", mpctx
->sh_video
->format
);
3875 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_BITRATE=%d\n", mpctx
->sh_video
->i_bps
*8);
3876 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_WIDTH=%d\n", mpctx
->sh_video
->disp_w
);
3877 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_HEIGHT=%d\n", mpctx
->sh_video
->disp_h
);
3878 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FPS=%5.3f\n", mpctx
->sh_video
->fps
);
3879 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_ASPECT=%1.4f\n", mpctx
->sh_video
->aspect
);
3881 if (mpctx
->sh_audio
) {
3882 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3883 if (mpctx
->sh_audio
->format
>= 0x20202020)
3884 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx
->sh_audio
->format
);
3886 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_FORMAT=%d\n", mpctx
->sh_audio
->format
);
3887 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_BITRATE=%d\n", mpctx
->sh_audio
->i_bps
*8);
3888 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_RATE=%d\n", mpctx
->sh_audio
->samplerate
);
3889 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_NCH=%d\n", mpctx
->sh_audio
->channels
);
3891 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx
->demuxer
));
3892 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_SEEKABLE=%d\n", mpctx
->stream
->seek
? 1 : 0);
3893 if (mpctx
->demuxer
) {
3894 if (mpctx
->demuxer
->num_chapters
== 0)
3895 stream_control(mpctx
->demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
, &mpctx
->demuxer
->num_chapters
);
3896 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_CHAPTERS=%d\n", mpctx
->demuxer
->num_chapters
);
3899 if(!mpctx
->sh_video
) goto main
; // audio-only
3901 if(!reinit_video_chain(mpctx
)) {
3902 if(!mpctx
->sh_video
){
3903 if(!mpctx
->sh_audio
) goto goto_next_file
;
3904 goto main
; // exit_player(MSGTR_Exit_error);
3908 if(mpctx
->sh_video
->output_flags
& 0x08 && vo_spudec
)
3909 spudec_set_hw_spu(vo_spudec
,mpctx
->video_out
);
3911 #ifdef CONFIG_FREETYPE
3912 force_load_font
= 1;
3915 //================== MAIN: ==========================
3917 current_module
="main";
3920 char* msg
= property_expand_string(mpctx
, playing_msg
);
3921 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"%s",msg
);
3926 // Disable the term OSD in verbose mode
3927 if(verbose
) term_osd
= 0;
3930 //int frame_corr_num=0; //
3931 //float v_frame=0; // Video
3932 //float num_frames=0; // number of frames played
3934 int frame_time_remaining
=0; // flag
3936 mpctx
->num_buffered_frames
=0;
3938 // Make sure old OSD does not stay around,
3939 // e.g. with -fixed-vo and same-resolution files
3941 update_osd_msg(mpctx
);
3943 //================ SETUP AUDIO ==========================
3945 if(mpctx
->sh_audio
){
3946 reinit_audio_chain(mpctx
);
3947 if (mpctx
->sh_audio
&& mpctx
->sh_audio
->codec
)
3948 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
, "ID_AUDIO_CODEC=%s\n", mpctx
->sh_audio
->codec
->name
);
3951 current_module
="av_init";
3953 if(mpctx
->sh_video
){
3954 mpctx
->sh_video
->timer
=0;
3956 audio_delay
+= mpctx
->sh_video
->stream_delay
;
3958 if(mpctx
->sh_audio
){
3959 if (start_volume
>= 0)
3960 mixer_setvolume(&mpctx
->mixer
, start_volume
, start_volume
);
3962 audio_delay
-= mpctx
->sh_audio
->stream_delay
;
3963 mpctx
->delay
=-audio_delay
;
3966 if(!mpctx
->sh_audio
){
3967 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_NoSound
);
3968 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Freeing %d unused audio chunks.\n",mpctx
->d_audio
->packs
);
3969 ds_free_packs(mpctx
->d_audio
); // free buffered chunks
3970 //mpctx->d_audio->id=-2; // do not read audio chunks
3971 //uninit_player(mpctx, INITIALIZED_AO); // close device
3973 if(!mpctx
->sh_video
){
3974 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_Video_NoVideo
);
3975 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Freeing %d unused video chunks.\n",mpctx
->d_video
->packs
);
3976 ds_free_packs(mpctx
->d_video
);
3977 mpctx
->d_video
->id
=-2;
3978 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3981 if (!mpctx
->sh_video
&& !mpctx
->sh_audio
)
3982 goto goto_next_file
;
3984 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3985 if(force_fps
&& mpctx
->sh_video
){
3986 vo_fps
= mpctx
->sh_video
->fps
=force_fps
;
3987 mpctx
->sh_video
->frametime
=1.0f
/mpctx
->sh_video
->fps
;
3988 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_FPSforced
,mpctx
->sh_video
->fps
,mpctx
->sh_video
->frametime
);
3993 if ( mpctx
->sh_audio
) guiIntfStruct
.AudioType
=mpctx
->sh_audio
->channels
; else guiIntfStruct
.AudioType
=0;
3994 if ( !mpctx
->sh_video
&& mpctx
->sh_audio
) guiGetEvent( guiSetAudioOnly
,(char *)1 ); else guiGetEvent( guiSetAudioOnly
,(char *)0 );
3995 guiGetEvent( guiSetFileFormat
,(char *)mpctx
->demuxer
->file_format
);
3996 if ( guiGetEvent( guiSetValues
,(char *)mpctx
->sh_video
) ) goto goto_next_file
;
3997 guiGetEvent( guiSetDemuxer
,(char *)mpctx
->demuxer
);
4001 mp_input_set_section(mpctx
->input
, NULL
);
4002 //TODO: add desired (stream-based) sections here
4003 if (mpctx
->stream
->type
==STREAMTYPE_TV
) mp_input_set_section(mpctx
->input
, "tv");
4004 if (mpctx
->stream
->type
==STREAMTYPE_DVDNAV
) mp_input_set_section(mpctx
->input
, "dvdnav");
4006 //==================== START PLAYING =======================
4008 if(opts
->loop_times
>1) opts
->loop_times
--; else
4009 if(opts
->loop_times
==1) opts
->loop_times
= -1;
4011 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_StartPlaying
);
4013 total_time_usage_start
=GetTimer();
4014 audio_time_usage
=0; video_time_usage
=0; vout_time_usage
=0;
4015 total_frame_cnt
=0; drop_frame_cnt
=0; // fix for multifile fps benchmark
4016 play_n_frames
=play_n_frames_mf
;
4018 if(play_n_frames
==0){
4019 mpctx
->stop_play
=PT_NEXT_ENTRY
; goto goto_next_file
;
4023 seek(mpctx
, seek_to_sec
, SEEK_ABSOLUTE
);
4024 end_at
.pos
+= seek_to_sec
;
4027 if (end_at
.type
== END_AT_SIZE
) {
4028 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, MSGTR_MPEndposNoSizeBased
);
4029 end_at
.type
= END_AT_NONE
;
4032 #ifdef CONFIG_DVDNAV
4033 mp_dvdnav_context_free(mpctx
);
4034 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
4035 mp_dvdnav_read_wait(mpctx
->stream
, 0, 1);
4036 mp_dvdnav_cell_has_changed(mpctx
->stream
,1);
4040 get_relative_time(mpctx
); // reset current delta
4041 mpctx
->time_frame
= 0;
4042 mpctx
->drop_message_shown
= 0;
4043 mpctx
->update_video_immediately
= true;
4044 mpctx
->total_avsync_change
= 0;
4045 // Make sure VO knows current pause state
4046 if (mpctx
->sh_video
)
4047 vo_control(mpctx
->video_out
, mpctx
->paused
? VOCTRL_PAUSE
: VOCTRL_RESUME
,
4050 while(!mpctx
->stop_play
){
4051 float aq_sleep_time
=0;
4053 if(dvd_last_chapter
>0) {
4054 int cur_chapter
= get_current_chapter(mpctx
);
4055 if(cur_chapter
!=-1 && cur_chapter
+1>dvd_last_chapter
)
4056 goto goto_next_file
;
4059 if(!mpctx
->sh_audio
&& mpctx
->d_audio
->sh
) {
4060 mpctx
->sh_audio
= mpctx
->d_audio
->sh
;
4061 mpctx
->sh_audio
->ds
= mpctx
->d_audio
;
4062 reinit_audio_chain(mpctx
);
4065 /*========================== PLAY AUDIO ============================*/
4067 if (mpctx
->sh_audio
&& !mpctx
->paused
)
4068 if (!fill_audio_out_buffers(mpctx
))
4069 // at eof, all audio at least written to ao
4070 if (!mpctx
->sh_video
)
4071 mpctx
->stop_play
= AT_END_OF_FILE
;
4074 if(!mpctx
->sh_video
) {
4075 // handle audio-only case:
4077 // sh_audio can be NULL due to video stream switching
4078 // TODO: handle this better
4079 if (mpctx
->sh_audio
)
4080 a_pos
= playing_audio_pts(mpctx
);
4082 print_status(mpctx
, a_pos
, false);
4084 if(end_at
.type
== END_AT_TIME
&& end_at
.pos
< a_pos
)
4085 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4086 update_osd_msg(mpctx
);
4090 /*========================== PLAY VIDEO ============================*/
4092 vo_pts
=mpctx
->sh_video
->timer
*90000.0;
4093 vo_fps
=mpctx
->sh_video
->fps
;
4095 if (!mpctx
->num_buffered_frames
) {
4096 double frame_time
= update_video(mpctx
, &blit_frame
);
4097 mp_dbg(MSGT_AVSYNC
,MSGL_DBG2
,"*** ftime=%5.3f ***\n",frame_time
);
4098 if (mpctx
->sh_video
->vf_initialized
< 0) {
4099 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
, MSGTR_NotInitializeVOPorVO
);
4100 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4101 goto goto_next_file
;
4104 struct vf_instance
*vf
= mpctx
->sh_video
->vfilter
;
4105 vf
->control(vf
, VFCTRL_DRAW_EOSD
, NULL
);
4106 vf
->control(vf
, VFCTRL_DRAW_OSD
, mpctx
->osd
);
4110 mpctx
->stop_play
= AT_END_OF_FILE
;
4112 // might return with !eof && !blit_frame if !correct_pts
4113 mpctx
->num_buffered_frames
+= blit_frame
;
4114 if (mpctx
->update_video_immediately
) {
4115 // Show this frame immediately, rest normally
4116 mpctx
->update_video_immediately
= false;
4118 mpctx
->time_frame
+= frame_time
/ opts
->playback_speed
;
4119 adjust_sync(mpctx
, frame_time
);
4123 if (mpctx
->timeline
) {
4124 struct timeline_part
*next
= mpctx
->timeline
+ mpctx
->timeline_part
+ 1;
4125 if (mpctx
->sh_video
->pts
>= next
->start
4126 || mpctx
->stop_play
== AT_END_OF_FILE
4127 && mpctx
->timeline_part
+ 1 < mpctx
->num_timeline_parts
) {
4128 seek(mpctx
, next
->start
, SEEK_ABSOLUTE
);
4133 // ==========================================================================
4135 // current_module="draw_osd";
4136 // if(vo_config_count) mpctx->video_out->draw_osd();
4139 if(use_gui
) guiEventHandling();
4142 current_module
="vo_check_events";
4143 vo_check_events(mpctx
->video_out
);
4146 if (stop_xscreensaver
) {
4147 current_module
= "stop_xscreensaver";
4148 xscreensaver_heartbeat(mpctx
->x11_state
);
4151 if (heartbeat_cmd
) {
4152 static unsigned last_heartbeat
;
4153 unsigned now
= GetTimerMS();
4154 if (now
- last_heartbeat
> 30000) {
4155 last_heartbeat
= now
;
4156 system(heartbeat_cmd
);
4160 frame_time_remaining
= sleep_until_update(mpctx
, &mpctx
->time_frame
, &aq_sleep_time
);
4162 //====================== FLIP PAGE (VIDEO BLT): =========================
4164 current_module
="flip_page";
4165 if (!frame_time_remaining
&& blit_frame
) {
4166 unsigned int t2
=GetTimer();
4168 vo_flip_page(mpctx
->video_out
);
4169 mpctx
->num_buffered_frames
--;
4171 mpctx
->last_vo_flip_duration
= (GetTimer() - t2
) * 0.000001;
4172 vout_time_usage
+= mpctx
->last_vo_flip_duration
;
4173 print_status(mpctx
, MP_NOPTS_VALUE
, true);
4176 print_status(mpctx
, MP_NOPTS_VALUE
, false);
4178 //============================ Auto QUALITY ============================
4180 /*Output quality adjustments:*/
4182 current_module
="autoq";
4183 // float total=0.000001f * (GetTimer()-aq_total_time);
4184 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4185 if(output_quality
<auto_quality
&& aq_sleep_time
>0)
4188 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4189 if(output_quality
>1 && aq_sleep_time
<0)
4192 if(output_quality
>0 && aq_sleep_time
<-0.050f
) // 50ms
4194 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4195 set_video_quality(mpctx
->sh_video
,output_quality
);
4198 if (!frame_time_remaining
&& blit_frame
) {
4199 if (play_n_frames
>= 0) {
4201 if (play_n_frames
<= 0)
4202 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4204 if (mpctx
->step_frames
> 0) {
4205 mpctx
->step_frames
--;
4206 if (mpctx
->step_frames
== 0)
4207 pause_player(mpctx
);
4212 // FIXME: add size based support for -endpos
4213 if (end_at
.type
== END_AT_TIME
&&
4214 !frame_time_remaining
&& end_at
.pos
<= mpctx
->sh_video
->pts
)
4215 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4217 } // end if(mpctx->sh_video)
4219 #ifdef CONFIG_DVDNAV
4220 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
4222 mp_dvdnav_get_highlight (mpctx
->stream
, &hl
);
4223 osd_set_nav_box (hl
.sx
, hl
.sy
, hl
.ex
, hl
.ey
);
4224 vo_osd_changed (OSDTYPE_DVDNAV
);
4226 if (mp_dvdnav_stream_has_changed(mpctx
->stream
)) {
4228 if (stream_control (mpctx
->demuxer
->stream
,
4229 STREAM_CTRL_GET_ASPECT_RATIO
, &ar
)
4230 != STREAM_UNSUPPORTED
)
4231 mpctx
->sh_video
->stream_aspect
= ar
;
4236 //================= Keyboard events, SEEKing ====================
4238 current_module
="key_events";
4243 while ((cmd
= mp_input_get_cmd(mpctx
->input
, 0,0,0)) != NULL
) {
4244 run_command(mpctx
, cmd
);
4246 if (mpctx
->stop_play
)
4249 if (!mpctx
->paused
|| mpctx
->stop_play
|| mpctx
->rel_seek_secs
4250 || mpctx
->abs_seek_pos
)
4252 if (mpctx
->sh_video
) {
4253 update_osd_msg(mpctx
);
4254 int hack
= vo_osd_changed(0);
4255 vo_osd_changed(hack
);
4257 if (redraw_osd(mpctx
->sh_video
, mpctx
->osd
) < 0) {
4258 add_step_frame(mpctx
);
4269 if (step_sec
> 0 && !mpctx
->paused
) {
4270 mpctx
->osd_function
=OSD_FFW
;
4271 mpctx
->rel_seek_secs
+=step_sec
;
4277 if(mpctx
->stop_play
==AT_END_OF_FILE
&& opts
->loop_times
>=0) {
4278 mp_msg(MSGT_CPLAYER
,MSGL_V
,"loop_times = %d\n", opts
->loop_times
);
4280 if(opts
->loop_times
>1) opts
->loop_times
--; else
4281 if(opts
->loop_times
==1) opts
->loop_times
=-1;
4282 play_n_frames
=play_n_frames_mf
;
4284 mpctx
->abs_seek_pos
=SEEK_ABSOLUTE
; mpctx
->rel_seek_secs
=seek_to_sec
;
4287 if(mpctx
->rel_seek_secs
|| mpctx
->abs_seek_pos
){
4288 seek(mpctx
, mpctx
->rel_seek_secs
, mpctx
->abs_seek_pos
);
4290 mpctx
->rel_seek_secs
=0;
4291 mpctx
->abs_seek_pos
=0;
4297 if(mpctx
->demuxer
->file_format
==DEMUXER_TYPE_AVI
&& mpctx
->sh_video
&& mpctx
->sh_video
->video
.dwLength
>2){
4298 // get pos from frame number / total frames
4299 guiIntfStruct
.Position
=(float)mpctx
->d_video
->pack_no
*100.0f
/mpctx
->sh_video
->video
.dwLength
;
4301 guiIntfStruct
.Position
=demuxer_get_percent_pos(mpctx
->demuxer
);
4303 if ( mpctx
->sh_video
) guiIntfStruct
.TimeSec
=mpctx
->sh_video
->pts
;
4304 else if ( mpctx
->sh_audio
) guiIntfStruct
.TimeSec
=playing_audio_pts(mpctx
);
4305 guiIntfStruct
.LengthInSec
=demuxer_get_time_length(mpctx
->demuxer
);
4306 guiGetEvent( guiReDraw
,NULL
);
4307 guiGetEvent( guiSetVolume
,NULL
);
4308 if(guiIntfStruct
.Playing
==0) break; // STOP
4309 if(guiIntfStruct
.Playing
==2) mpctx
->osd_function
=OSD_PAUSE
;
4310 if ( guiIntfStruct
.DiskChanged
|| guiIntfStruct
.NewPlay
) goto goto_next_file
;
4311 #ifdef CONFIG_DVDREAD
4312 if ( mpctx
->stream
->type
== STREAMTYPE_DVD
)
4314 dvd_priv_t
* dvdp
= mpctx
->stream
->priv
;
4315 guiIntfStruct
.DVD
.current_chapter
=dvd_chapter_from_cell(dvdp
,guiIntfStruct
.DVD
.current_title
-1, dvdp
->cur_cell
)+1;
4319 #endif /* CONFIG_GUI */
4321 } // while(!mpctx->stop_play)
4323 mp_msg(MSGT_GLOBAL
,MSGL_V
,"EOF code: %d \n",mpctx
->stop_play
);
4326 if(mpctx
->dvbin_reopen
)
4328 mpctx
->stop_play
= 0;
4329 uninit_player(mpctx
, INITIALIZED_ALL
-(INITIALIZED_GUI
|INITIALIZED_STREAM
|INITIALIZED_GETCH2
|(opts
->fixed_vo
?INITIALIZED_VO
:0)));
4330 cache_uninit(mpctx
->stream
);
4331 mpctx
->dvbin_reopen
= 0;
4332 goto goto_enable_cache
;
4337 goto_next_file
: // don't jump here after ao/vo/getch initialization!
4339 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"\n");
4342 double tot
=video_time_usage
+vout_time_usage
+audio_time_usage
;
4343 double total_time_usage
;
4344 total_time_usage_start
=GetTimer()-total_time_usage_start
;
4345 total_time_usage
= (float)total_time_usage_start
*0.000001;
4346 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4347 video_time_usage
,vout_time_usage
,audio_time_usage
,
4348 total_time_usage
-tot
,total_time_usage
);
4349 if(total_time_usage
>0.0)
4350 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4351 100.0*video_time_usage
/total_time_usage
,
4352 100.0*vout_time_usage
/total_time_usage
,
4353 100.0*audio_time_usage
/total_time_usage
,
4354 100.0*(total_time_usage
-tot
)/total_time_usage
,
4356 if(total_frame_cnt
&& frame_dropping
)
4357 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4358 total_frame_cnt
-drop_frame_cnt
,
4359 (total_time_usage
>0.5)?((total_frame_cnt
-drop_frame_cnt
)/total_time_usage
):0,
4361 100*drop_frame_cnt
/total_frame_cnt
,
4363 (total_time_usage
>0.5)?(total_frame_cnt
/total_time_usage
):0);
4366 // time to uninit all, except global stuff:
4367 uninit_player(mpctx
, INITIALIZED_ALL
-(INITIALIZED_GUI
+(opts
->fixed_vo
?INITIALIZED_VO
:0)));
4369 if(mpctx
->set_of_sub_size
> 0) {
4370 current_module
="sub_free";
4371 for(i
= 0; i
< mpctx
->set_of_sub_size
; ++i
) {
4372 sub_free(mpctx
->set_of_subtitles
[i
]);
4374 if(mpctx
->set_of_ass_tracks
[i
])
4375 ass_free_track( mpctx
->set_of_ass_tracks
[i
] );
4378 mpctx
->set_of_sub_size
= 0;
4380 vo_sub_last
= vo_sub
=NULL
;
4385 ass_clear_fonts(ass_library
);
4388 if (!mpctx
->stop_play
) // In case some goto jumped here...
4389 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4391 int playtree_direction
= 1;
4393 if(mpctx
->stop_play
== PT_NEXT_ENTRY
|| mpctx
->stop_play
== PT_PREV_ENTRY
) {
4394 if(play_tree_iter_step(mpctx
->playtree_iter
,mpctx
->play_tree_step
,0) != PLAY_TREE_ITER_ENTRY
) {
4395 play_tree_iter_free(mpctx
->playtree_iter
);
4396 mpctx
->playtree_iter
= NULL
;
4398 mpctx
->play_tree_step
= 1;
4399 } else if(mpctx
->stop_play
== PT_UP_NEXT
|| mpctx
->stop_play
== PT_UP_PREV
) {
4400 int direction
= mpctx
->stop_play
== PT_UP_NEXT
? 1 : -1;
4401 if(mpctx
->playtree_iter
) {
4402 if(play_tree_iter_up_step(mpctx
->playtree_iter
,direction
,0) != PLAY_TREE_ITER_ENTRY
) {
4403 play_tree_iter_free(mpctx
->playtree_iter
);
4404 mpctx
->playtree_iter
= NULL
;
4407 } else if (mpctx
->stop_play
== PT_STOP
) {
4408 play_tree_iter_free(mpctx
->playtree_iter
);
4409 mpctx
->playtree_iter
= NULL
;
4410 } else { // NEXT PREV SRC
4411 playtree_direction
= mpctx
->stop_play
== PT_PREV_SRC
? -1 : 1;
4414 while(mpctx
->playtree_iter
!= NULL
) {
4415 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
, playtree_direction
);
4416 if(mpctx
->filename
== NULL
) {
4417 if(play_tree_iter_step(mpctx
->playtree_iter
, playtree_direction
, 0) != PLAY_TREE_ITER_ENTRY
) {
4418 play_tree_iter_free(mpctx
->playtree_iter
);
4419 mpctx
->playtree_iter
= NULL
;
4426 if(use_gui
&& !mpctx
->playtree_iter
) {
4427 #ifdef CONFIG_DVDREAD
4428 if(!guiIntfStruct
.DiskChanged
)
4434 if(use_gui
|| mpctx
->playtree_iter
!= NULL
|| player_idle_mode
){
4435 if(!mpctx
->playtree_iter
) mpctx
->filename
= NULL
;
4436 mpctx
->stop_play
= 0;
4437 goto play_next_file
;
4441 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
4445 #endif /* DISABLE_MAIN */