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 mpctx
->video_offset
= 0;
584 mpctx
->stream
=mpctx
->demuxer
->stream
;
585 free_demuxer(mpctx
->demuxer
);
590 // kill the cache process:
591 if(mask
&INITIALIZED_STREAM
){
592 mpctx
->initialized_flags
&=~INITIALIZED_STREAM
;
593 current_module
="uninit_stream";
594 if(mpctx
->stream
) free_stream(mpctx
->stream
);
598 if(mask
&INITIALIZED_VO
){
599 mpctx
->initialized_flags
&=~INITIALIZED_VO
;
600 current_module
="uninit_vo";
601 vo_destroy(mpctx
->video_out
);
602 mpctx
->video_out
=NULL
;
604 mp_dvdnav_context_free(mpctx
);
608 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
609 if(mask
&INITIALIZED_GETCH2
){
610 mpctx
->initialized_flags
&=~INITIALIZED_GETCH2
;
611 current_module
="uninit_getch2";
612 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n[[[uninit getch2]]]\n");
617 if(mask
&INITIALIZED_VOBSUB
){
618 mpctx
->initialized_flags
&=~INITIALIZED_VOBSUB
;
619 current_module
="uninit_vobsub";
620 if(vo_vobsub
) vobsub_close(vo_vobsub
);
624 if (mask
&INITIALIZED_SPUDEC
){
625 mpctx
->initialized_flags
&=~INITIALIZED_SPUDEC
;
626 current_module
="uninit_spudec";
627 spudec_free(vo_spudec
);
631 if(mask
&INITIALIZED_AO
){
632 mpctx
->initialized_flags
&=~INITIALIZED_AO
;
633 current_module
="uninit_ao";
634 if (mpctx
->edl_muted
) mixer_mute(&mpctx
->mixer
);
635 mpctx
->audio_out
->uninit(mpctx
->stop_play
!= AT_END_OF_FILE
);
636 mpctx
->audio_out
=NULL
;
640 if(mask
&INITIALIZED_GUI
){
641 mpctx
->initialized_flags
&=~INITIALIZED_GUI
;
642 current_module
="uninit_gui";
650 void exit_player_with_rc(struct MPContext
*mpctx
, exit_reason_t how
, int rc
)
652 if (mpctx
->user_muted
&& !mpctx
->edl_muted
) mixer_mute(&mpctx
->mixer
);
653 uninit_player(mpctx
, INITIALIZED_ALL
);
654 #if defined(__MINGW32__) || defined(__CYGWIN__)
661 vo_uninit(mpctx
->x11_state
); // Close the X11 connection (if any is open).
664 current_module
="uninit_input";
665 mp_input_uninit(mpctx
->input
);
671 #ifdef CONFIG_FREETYPE
672 current_module
="uninit_font";
673 if (mpctx
->osd
&& mpctx
->osd
->sub_font
!= vo_font
)
674 free_font_desc(mpctx
->osd
->sub_font
);
675 free_font_desc(vo_font
);
679 osd_free(mpctx
->osd
);
682 ass_library_done(ass_library
);
685 current_module
="exit_player";
687 // free mplayer config
689 m_config_free(mpctx
->mconfig
);
692 play_tree_free(mpctx
->playtree
, 1);
694 talloc_free(mpctx
->key_fifo
);
696 if(edl_records
!= NULL
) free(edl_records
); // free mem allocated for EDL
699 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_ExitingHow
,MSGTR_Exit_quit
);
700 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=QUIT\n");
703 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_ExitingHow
,MSGTR_Exit_eof
);
704 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=EOF\n");
707 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_ExitingHow
,MSGTR_Exit_error
);
708 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=ERROR\n");
711 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=NONE\n");
713 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"max framesize was %d bytes\n",max_framesize
);
718 static void exit_player(struct MPContext
*mpctx
, exit_reason_t how
)
720 exit_player_with_rc(mpctx
, how
, 1);
724 static void child_sighandler(int x
){
726 while((pid
=waitpid(-1,NULL
,WNOHANG
)) > 0);
730 #ifdef CONFIG_CRASH_DEBUG
731 static char *prog_path
;
732 static int crash_debug
= 0;
735 static void exit_sighandler(int x
){
736 static int sig_count
=0;
737 #ifdef CONFIG_CRASH_DEBUG
738 if (!crash_debug
|| x
!= SIGTRAP
)
743 /* We're crashing bad and can't uninit cleanly :(
744 * by popular request, we make one last (dirty)
745 * effort to restore the user's
750 if(sig_count
==6) exit(1);
754 kill(getpid(),SIGKILL
);
757 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,"\n" MSGTR_IntBySignal
,x
,
758 current_module
?current_module
:"unknown"
760 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SIGNAL=%d\n", x
);
767 async_quit_request
= 1;
768 return; // killed from keyboard (^C) or killed [-9]
770 #ifdef RUNTIME_CPUDETECT
771 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_Exit_SIGILL_RTCpuSel
);
773 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_Exit_SIGILL
);
777 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_Exit_SIGSEGV_SIGFPE
);
779 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_Exit_SIGCRASH
);
780 #ifdef CONFIG_CRASH_DEBUG
783 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Forking...\n");
785 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Forked...\n");
786 if (gdb_pid
== 0) { // We are the child
788 snprintf(spid
, sizeof(spid
), "%i", getppid());
789 getch2_disable(); // allow terminal to work properly with gdb
790 if (execlp("gdb", "gdb", prog_path
, spid
, "-ex", "bt", NULL
) == -1)
791 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Couldn't start gdb\n");
792 } else if (gdb_pid
< 0)
793 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Couldn't fork\n");
795 waitpid(gdb_pid
, NULL
, 0);
797 if (x
== SIGTRAP
) return;
805 #include "cfg-mplayer.h"
807 static int cfg_include(m_option_t
*conf
, char *filename
)
809 return m_config_parse_config_file(conf
->priv
, filename
);
812 static void parse_cfgfiles(struct MPContext
*mpctx
, m_config_t
* conf
)
816 if (!disable_system_conf
&&
817 m_config_parse_config_file(conf
, MPLAYER_CONFDIR
"/mplayer.conf") < 0)
818 exit_player(mpctx
, EXIT_NONE
);
819 if ((conffile
= get_path("")) == NULL
) {
820 mp_msg(MSGT_CPLAYER
,MSGL_WARN
,MSGTR_NoHomeDir
);
825 mkdir(conffile
, 0777);
828 if ((conffile
= get_path("config")) == NULL
) {
829 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_GetpathProblem
);
831 if ((conffile_fd
= open(conffile
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666)) != -1) {
832 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_CreatingCfgFile
, conffile
);
833 write(conffile_fd
, default_config
, strlen(default_config
));
836 if (!disable_user_conf
&&
837 m_config_parse_config_file(conf
, conffile
) < 0)
838 exit_player(mpctx
, EXIT_NONE
);
844 #define PROFILE_CFG_PROTOCOL "protocol."
846 static void load_per_protocol_config (m_config_t
* conf
, const char *const file
)
849 char protocol
[strlen (PROFILE_CFG_PROTOCOL
) + strlen (file
) + 1];
852 /* does filename actually uses a protocol ? */
853 str
= strstr (file
, "://");
857 sprintf (protocol
, "%s%s", PROFILE_CFG_PROTOCOL
, file
);
858 protocol
[strlen (PROFILE_CFG_PROTOCOL
)+strlen(file
)-strlen(str
)] = '\0';
859 p
= m_config_get_profile (conf
, protocol
);
862 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_LoadingProtocolProfile
, protocol
);
863 m_config_set_profile(conf
,p
);
867 #define PROFILE_CFG_EXTENSION "extension."
869 static void load_per_extension_config (m_config_t
* conf
, const char *const file
)
872 char extension
[strlen (PROFILE_CFG_EXTENSION
) + 8];
875 /* does filename actually have an extension ? */
876 str
= strrchr (file
, '.');
880 sprintf (extension
, PROFILE_CFG_EXTENSION
);
881 strncat (extension
, ++str
, 7);
882 p
= m_config_get_profile (conf
, extension
);
885 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_LoadingExtensionProfile
, extension
);
886 m_config_set_profile(conf
,p
);
890 #define PROFILE_CFG_VO "vo."
891 #define PROFILE_CFG_AO "ao."
893 static void load_per_output_config (m_config_t
* conf
, char *cfg
, char *out
)
895 char profile
[strlen (cfg
) + strlen (out
) + 1];
898 sprintf (profile
, "%s%s", cfg
, out
);
899 p
= m_config_get_profile (conf
, profile
);
902 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_LoadingExtensionProfile
, profile
);
903 m_config_set_profile(conf
,p
);
907 static void load_per_file_config (m_config_t
* conf
, const char *const file
)
910 char cfg
[strlen(file
)+10];
914 sprintf (cfg
, "%s.conf", file
);
916 if (use_filedir_conf
&& !stat (cfg
, &st
))
918 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_LoadingConfig
, cfg
);
919 m_config_parse_config_file (conf
, cfg
);
923 if ((name
= strrchr (cfg
, '/')) == NULL
)
928 if ((confpath
= get_path (name
)) != NULL
)
930 if (!stat (confpath
, &st
))
932 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_LoadingConfig
, confpath
);
933 m_config_parse_config_file (conf
, confpath
);
940 /* When libmpdemux performs a blocking operation (network connection or
941 * cache filling) if the operation fails we use this function to check
942 * if it was interrupted by the user.
943 * The function returns a new value for eof. */
944 static int libmpdemux_was_interrupted(struct MPContext
*mpctx
, int stop_play
)
947 if((cmd
= mp_input_get_cmd(mpctx
->input
, 0,0,0)) != NULL
) {
950 exit_player_with_rc(mpctx
, EXIT_QUIT
, (cmd
->nargs
> 0)? cmd
->args
[0].v
.i
: 0);
951 case MP_CMD_PLAY_TREE_STEP
: {
952 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_NEXT_ENTRY
: PT_PREV_ENTRY
;
953 mpctx
->play_tree_step
= (cmd
->args
[0].v
.i
== 0) ? 1 : cmd
->args
[0].v
.i
;
955 case MP_CMD_PLAY_TREE_UP_STEP
: {
956 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_UP_NEXT
: PT_UP_PREV
;
958 case MP_CMD_PLAY_ALT_SRC_STEP
: {
959 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_NEXT_SRC
: PT_PREV_SRC
;
967 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
969 static int playtree_add_playlist(struct MPContext
*mpctx
, play_tree_t
* entry
)
971 play_tree_add_bpf(entry
,mpctx
->filename
);
976 import_playtree_playlist_into_gui(entry
, mpctx
->mconfig
);
977 play_tree_free_list(entry
,1);
983 entry
= mpctx
->playtree_iter
->tree
;
984 if(play_tree_iter_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
985 return PT_NEXT_ENTRY
;
987 if(mpctx
->playtree_iter
->tree
== entry
) { // Loop with a single file
988 if(play_tree_iter_up_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
989 return PT_NEXT_ENTRY
;
992 play_tree_remove(entry
,1,1);
995 play_tree_insert_entry(mpctx
->playtree_iter
->tree
,entry
);
996 play_tree_set_params_from(entry
,mpctx
->playtree_iter
->tree
);
997 entry
= mpctx
->playtree_iter
->tree
;
998 if(play_tree_iter_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
999 return PT_NEXT_ENTRY
;
1001 play_tree_remove(entry
,1,1);
1006 void add_subtitles(struct MPContext
*mpctx
, char *filename
, float fps
, int noerr
)
1010 ass_track_t
*asst
= 0;
1013 if (filename
== NULL
|| mpctx
->set_of_sub_size
>= MAX_SUBTITLE_FILES
) {
1017 subd
= sub_read_file(filename
, fps
);
1021 asst
= ass_read_file(ass_library
, filename
, sub_cp
);
1023 asst
= ass_read_file(ass_library
, filename
, 0);
1025 if (ass_enabled
&& subd
&& !asst
)
1026 asst
= ass_read_subdata(ass_library
, subd
, fps
);
1032 mp_msg(MSGT_CPLAYER
, noerr
? MSGL_WARN
: MSGL_ERR
, MSGTR_CantLoadSub
,
1033 filename_recode(filename
));
1036 if (!asst
&& !subd
) return;
1037 mpctx
->set_of_ass_tracks
[mpctx
->set_of_sub_size
] = asst
;
1041 mpctx
->set_of_subtitles
[mpctx
->set_of_sub_size
] = subd
;
1042 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_FILE_SUB_ID=%d\n", mpctx
->set_of_sub_size
);
1043 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_FILE_SUB_FILENAME=%s\n",
1044 filename_recode(filename
));
1045 ++mpctx
->set_of_sub_size
;
1046 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, MSGTR_AddedSubtitleFile
, mpctx
->set_of_sub_size
,
1047 filename_recode(filename
));
1050 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1051 void update_set_of_subtitles(struct MPContext
*mpctx
)
1052 // subdata was changed, set_of_sub... have to be updated.
1054 sub_data
** const set_of_subtitles
= mpctx
->set_of_subtitles
;
1056 if (mpctx
->set_of_sub_size
> 0 && subdata
== NULL
) { // *subdata was deleted
1057 for (i
= mpctx
->set_of_sub_pos
+ 1; i
< mpctx
->set_of_sub_size
; ++i
)
1058 set_of_subtitles
[i
-1] = set_of_subtitles
[i
];
1059 set_of_subtitles
[mpctx
->set_of_sub_size
-1] = NULL
;
1060 --mpctx
->set_of_sub_size
;
1061 if (mpctx
->set_of_sub_size
> 0) subdata
= set_of_subtitles
[mpctx
->set_of_sub_pos
=0];
1063 else if (mpctx
->set_of_sub_size
> 0 && subdata
!= NULL
) { // *subdata was changed
1064 set_of_subtitles
[mpctx
->set_of_sub_pos
] = subdata
;
1066 else if (mpctx
->set_of_sub_size
<= 0 && subdata
!= NULL
) { // *subdata was added
1067 set_of_subtitles
[mpctx
->set_of_sub_pos
=mpctx
->set_of_sub_size
] = subdata
;
1068 ++mpctx
->set_of_sub_size
;
1072 void init_vo_spudec(struct MPContext
*mpctx
)
1075 spudec_free(vo_spudec
);
1076 mpctx
->initialized_flags
&= ~INITIALIZED_SPUDEC
;
1079 unsigned int palette
[16], width
, height
;
1080 current_module
="spudec_init_vobsub";
1081 if (vobsub_parse_ifo(NULL
,spudec_ifo
, palette
, &width
, &height
, 1, -1, NULL
) >= 0)
1082 vo_spudec
=spudec_new_scaled(palette
, width
, height
, NULL
, 0);
1085 #ifdef CONFIG_DVDREAD
1086 if (vo_spudec
==NULL
&& mpctx
->stream
->type
==STREAMTYPE_DVD
) {
1087 current_module
="spudec_init_dvdread";
1088 vo_spudec
=spudec_new_scaled(((dvd_priv_t
*)(mpctx
->stream
->priv
))->cur_pgc
->palette
,
1089 mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
,
1094 #ifdef CONFIG_DVDNAV
1095 if (vo_spudec
==NULL
&& mpctx
->stream
->type
==STREAMTYPE_DVDNAV
) {
1096 unsigned int *palette
= mp_dvdnav_get_spu_clut(mpctx
->stream
);
1097 current_module
="spudec_init_dvdnav";
1098 vo_spudec
=spudec_new_scaled(palette
, mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
, NULL
, 0);
1102 if (vo_spudec
==NULL
) {
1103 sh_sub_t
*sh
= (sh_sub_t
*)mpctx
->d_sub
->sh
;
1104 current_module
="spudec_init_normal";
1105 vo_spudec
=spudec_new_scaled(NULL
, mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
, sh
->extradata
, sh
->extradata_len
);
1106 spudec_set_font_factor(vo_spudec
,font_factor
);
1109 if (vo_spudec
!=NULL
)
1110 mpctx
->initialized_flags
|=INITIALIZED_SPUDEC
;
1114 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1115 * make it all work is to use the builtin SDL-bootstrap code, which
1116 * will be done automatically by replacing our main() if we include SDL.h.
1118 #if defined(__APPLE__) && defined(CONFIG_SDL)
1123 * \brief append a formatted string
1124 * \param buf buffer to print into
1125 * \param pos position of terminating 0 in buf
1126 * \param len maximum number of characters in buf, not including terminating 0
1127 * \param format printf format string
1129 static void saddf(char *buf
, unsigned *pos
, int len
, const char *format
, ...)
1132 va_start(va
, format
);
1133 *pos
+= vsnprintf(&buf
[*pos
], len
- *pos
, format
, va
);
1142 * \brief append time in the hh:mm:ss.f format
1143 * \param buf buffer to print into
1144 * \param pos position of terminating 0 in buf
1145 * \param len maximum number of characters in buf, not including terminating 0
1146 * \param time time value to convert/append
1148 static void sadd_hhmmssf(char *buf
, unsigned *pos
, int len
, float time
) {
1149 long tenths
= 10 * time
;
1150 int f1
= tenths
% 10;
1151 int ss
= (tenths
/ 10) % 60;
1152 int mm
= (tenths
/ 600) % 60;
1153 int hh
= tenths
/ 36000;
1155 saddf(buf
, pos
, len
, "unknown");
1159 saddf(buf
, pos
, len
, "%2d:", hh
);
1160 if (hh
> 0 || mm
> 0)
1161 saddf(buf
, pos
, len
, "%02d:", mm
);
1162 saddf(buf
, pos
, len
, "%02d.%1d", ss
, f1
);
1165 static void print_status(struct MPContext
*mpctx
, double a_pos
, bool at_frame
)
1167 struct MPOpts
*opts
= &mpctx
->opts
;
1168 sh_video_t
* const sh_video
= mpctx
->sh_video
;
1170 if (mpctx
->sh_audio
&& a_pos
== MP_NOPTS_VALUE
)
1171 a_pos
= playing_audio_pts(mpctx
);
1172 if (mpctx
->sh_audio
&& sh_video
&& at_frame
) {
1173 mpctx
->last_av_difference
= a_pos
- sh_video
->pts
- audio_delay
;
1174 if (mpctx
->last_av_difference
> 0.5 && drop_frame_cnt
> 50
1175 && !mpctx
->drop_message_shown
) {
1176 mp_msg(MSGT_AVSYNC
,MSGL_WARN
,MSGTR_SystemTooSlow
);
1177 mpctx
->drop_message_shown
= true;
1188 if (screen_width
> 0)
1189 width
= screen_width
;
1192 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1193 /* Windows command line is broken (MinGW's rxvt works, but we
1194 * should not depend on that). */
1197 line
= malloc(width
+ 1); // one additional char for the terminating null
1200 if (mpctx
->sh_audio
) {
1201 saddf(line
, &pos
, width
, "A:%6.1f ", a_pos
);
1203 float len
= demuxer_get_time_length(mpctx
->demuxer
);
1204 saddf(line
, &pos
, width
, "(");
1205 sadd_hhmmssf(line
, &pos
, width
, a_pos
);
1206 saddf(line
, &pos
, width
, ") of %.1f (", len
);
1207 sadd_hhmmssf(line
, &pos
, width
, len
);
1208 saddf(line
, &pos
, width
, ") ");
1214 saddf(line
, &pos
, width
, "V:%6.1f ", sh_video
->pts
);
1217 if (mpctx
->sh_audio
&& sh_video
)
1218 saddf(line
, &pos
, width
, "A-V:%7.3f ct:%7.3f ",
1219 mpctx
->last_av_difference
, mpctx
->total_avsync_change
);
1223 saddf(line
, &pos
, width
, "%3d/%3d ",
1224 (int)sh_video
->num_frames
,
1225 (int)sh_video
->num_frames_decoded
);
1229 if (sh_video
->timer
> 0.5)
1230 saddf(line
, &pos
, width
, "%2d%% %2d%% %4.1f%% ",
1231 (int)(100.0*video_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
),
1232 (int)(100.0*vout_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
),
1233 (100.0*audio_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
));
1235 saddf(line
, &pos
, width
, "??%% ??%% ??,?%% ");
1236 } else if (mpctx
->sh_audio
) {
1237 if (mpctx
->delay
> 0.5)
1238 saddf(line
, &pos
, width
, "%4.1f%% ",
1239 100.0*audio_time_usage
/(double)mpctx
->delay
);
1241 saddf(line
, &pos
, width
, "??,?%% ");
1246 saddf(line
, &pos
, width
, "%d %d ", drop_frame_cnt
, output_quality
);
1248 #ifdef CONFIG_STREAM_CACHE
1250 if (stream_cache_size
> 0)
1251 saddf(line
, &pos
, width
, "%d%% ", cache_fill_status
);
1255 if (opts
->playback_speed
!= 1)
1256 saddf(line
, &pos
, width
, "%4.2fx ", opts
->playback_speed
);
1259 if (erase_to_end_of_line
) {
1261 mp_msg(MSGT_STATUSLINE
, MSGL_STATUS
, "%s%s\r", line
, erase_to_end_of_line
);
1263 memset(&line
[pos
], ' ', width
- pos
);
1265 mp_msg(MSGT_STATUSLINE
, MSGL_STATUS
, "%s\r", line
);
1271 * \brief build a chain of audio filters that converts the input format
1272 * to the ao's format, taking into account the current playback_speed.
1273 * \param sh_audio describes the requested input format of the chain.
1274 * \param ao_data describes the requested output format of the chain.
1276 int build_afilter_chain(struct MPContext
*mpctx
, sh_audio_t
*sh_audio
, ao_data_t
*ao_data
)
1278 struct MPOpts
*opts
= &mpctx
->opts
;
1284 if (use_gui
) guiGetEvent(guiSetAfilter
, (char *)NULL
);
1286 mpctx
->mixer
.afilter
= NULL
;
1289 if(af_control_any_rev(sh_audio
->afilter
,
1290 AF_CONTROL_PLAYBACK_SPEED
| AF_CONTROL_SET
,
1291 &opts
->playback_speed
)) {
1292 new_srate
= sh_audio
->samplerate
;
1294 new_srate
= sh_audio
->samplerate
* opts
->playback_speed
;
1295 if (new_srate
!= ao_data
->samplerate
) {
1296 // limits are taken from libaf/af_resample.c
1297 if (new_srate
< 8000)
1299 if (new_srate
> 192000)
1301 opts
->playback_speed
= (float)new_srate
/ (float)sh_audio
->samplerate
;
1304 result
= init_audio_filters(sh_audio
, new_srate
,
1305 &ao_data
->samplerate
, &ao_data
->channels
, &ao_data
->format
);
1306 mpctx
->mixer
.afilter
= sh_audio
->afilter
;
1308 if (use_gui
) guiGetEvent(guiSetAfilter
, (char *)sh_audio
->afilter
);
1314 typedef struct mp_osd_msg mp_osd_msg_t
;
1316 /// Previous message on the stack.
1320 int id
,level
,started
;
1321 /// Display duration in ms.
1325 /// OSD message stack.
1326 static mp_osd_msg_t
* osd_msg_stack
= NULL
;
1329 * \brief Add a message on the OSD message stack
1331 * If a message with the same id is already present in the stack
1332 * it is pulled on top of the stack, otherwise a new message is created.
1336 void set_osd_msg(int id
, int level
, int time
, const char* fmt
, ...) {
1337 mp_osd_msg_t
*msg
,*last
=NULL
;
1341 // look if the id is already in the stack
1342 for(msg
= osd_msg_stack
; msg
&& msg
->id
!= id
;
1343 last
= msg
, msg
= msg
->prev
);
1344 // not found: alloc it
1346 msg
= calloc(1,sizeof(mp_osd_msg_t
));
1347 msg
->prev
= osd_msg_stack
;
1348 osd_msg_stack
= msg
;
1349 } else if(last
) { // found, but it's not on top of the stack
1350 last
->prev
= msg
->prev
;
1351 msg
->prev
= osd_msg_stack
;
1352 osd_msg_stack
= msg
;
1356 r
= vsnprintf(msg
->msg
, 128, fmt
, va
);
1358 if(r
>= 128) msg
->msg
[127] = 0;
1367 * \brief Remove a message from the OSD stack
1369 * This function can be used to get rid of a message right away.
1373 void rm_osd_msg(int id
) {
1374 mp_osd_msg_t
*msg
,*last
=NULL
;
1376 // Search for the msg
1377 for(msg
= osd_msg_stack
; msg
&& msg
->id
!= id
;
1378 last
= msg
, msg
= msg
->prev
);
1381 // Detach it from the stack and free it
1383 last
->prev
= msg
->prev
;
1385 osd_msg_stack
= msg
->prev
;
1390 * \brief Remove all messages from the OSD stack
1394 static void clear_osd_msgs(void) {
1395 mp_osd_msg_t
* msg
= osd_msg_stack
, *prev
= NULL
;
1401 osd_msg_stack
= NULL
;
1405 * \brief Get the current message from the OSD stack.
1407 * This function decrements the message timer and destroys the old ones.
1408 * The message that should be displayed is returned (if any).
1412 static mp_osd_msg_t
* get_osd_msg(struct MPContext
*mpctx
)
1414 struct MPOpts
*opts
= &mpctx
->opts
;
1415 mp_osd_msg_t
*msg
,*prev
,*last
= NULL
;
1416 static unsigned last_update
= 0;
1417 unsigned now
= GetTimerMS();
1419 char hidden_dec_done
= 0;
1421 if (mpctx
->osd_visible
) {
1422 // 36000000 means max timed visibility is 1 hour into the future, if
1423 // the difference is greater assume it's wrapped around from below 0
1424 if (mpctx
->osd_visible
- now
> 36000000) {
1425 mpctx
->osd_visible
= 0;
1426 vo_osd_progbar_type
= -1; // disable
1427 vo_osd_changed(OSDTYPE_PROGBAR
);
1428 mpctx
->osd_function
= mpctx
->paused
? OSD_PAUSE
: OSD_PLAY
;
1431 if (mpctx
->osd_show_percentage_until
- now
> 36000000)
1432 mpctx
->osd_show_percentage_until
= 0;
1434 if(!last_update
) last_update
= now
;
1435 diff
= now
>= last_update
? now
- last_update
: 0;
1439 // Look for the first message in the stack with high enough level.
1440 for(msg
= osd_msg_stack
; msg
; last
= msg
, msg
= prev
) {
1442 if (msg
->level
> opts
->osd_level
&& hidden_dec_done
)
1444 // The message has a high enough level or it is the first hidden one
1445 // in both cases we decrement the timer or kill it.
1446 if(!msg
->started
|| msg
->time
> diff
) {
1447 if(msg
->started
) msg
->time
-= diff
;
1448 else msg
->started
= 1;
1450 if (msg
->level
<= opts
->osd_level
)
1452 hidden_dec_done
= 1;
1461 osd_msg_stack
= prev
;
1470 * \brief Display the OSD bar.
1472 * Display the OSD bar or fall back on a simple message.
1476 void set_osd_bar(struct MPContext
*mpctx
, int type
,const char* name
,double min
,double max
,double val
) {
1477 struct MPOpts
*opts
= &mpctx
->opts
;
1478 if (opts
->osd_level
< 1)
1481 if(mpctx
->sh_video
) {
1482 mpctx
->osd_visible
= (GetTimerMS() + 1000) | 1;
1483 vo_osd_progbar_type
= type
;
1484 vo_osd_progbar_value
= 256*(val
-min
)/(max
-min
);
1485 vo_osd_changed(OSDTYPE_PROGBAR
);
1489 set_osd_msg(OSD_MSG_BAR
, 1, opts
->osd_duration
, "%s: %d %%",
1490 name
, ROUND(100*(val
-min
)/(max
-min
)));
1495 * \brief Update the OSD message line.
1497 * This function displays the current message on the vo OSD or on the term.
1498 * If the stack is empty and the OSD level is high enough the timer
1499 * is displayed (only on the vo OSD).
1503 static void update_osd_msg(struct MPContext
*mpctx
)
1505 struct MPOpts
*opts
= &mpctx
->opts
;
1507 struct osd_state
*osd
= mpctx
->osd
;
1508 char osd_text_timer
[128];
1510 if (mpctx
->add_osd_seek_info
) {
1512 if (mpctx
->timeline
&& mpctx
->sh_video
)
1513 percentage
= mpctx
->sh_video
->pts
* 100 /
1514 mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
1516 percentage
= demuxer_get_percent_pos(mpctx
->demuxer
);
1517 set_osd_bar(mpctx
, 0, "Position", 0, 100, percentage
);
1518 if (mpctx
->sh_video
)
1519 mpctx
->osd_show_percentage_until
= (GetTimerMS() + 1000) | 1;
1520 mpctx
->add_osd_seek_info
= false;
1523 // Look if we have a msg
1524 if((msg
= get_osd_msg(mpctx
))) {
1525 if (strcmp(osd
->osd_text
, msg
->msg
)) {
1526 strncpy(osd
->osd_text
, msg
->msg
, 127);
1527 if(mpctx
->sh_video
) vo_osd_changed(OSDTYPE_OSD
); else
1528 if(term_osd
) mp_msg(MSGT_CPLAYER
,MSGL_STATUS
,"%s%s\n",term_osd_esc
,msg
->msg
);
1533 if(mpctx
->sh_video
) {
1534 // fallback on the timer
1535 if (opts
->osd_level
>= 2) {
1537 if (mpctx
->timeline
)
1538 len
= mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
1540 len
= demuxer_get_time_length(mpctx
->demuxer
);
1541 int percentage
= -1;
1542 char percentage_text
[10];
1543 int pts
= demuxer_get_current_time(mpctx
->demuxer
);
1545 if (mpctx
->osd_show_percentage_until
)
1546 if (mpctx
->timeline
)
1547 percentage
= mpctx
->sh_video
->pts
* 100 /
1548 mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
1550 percentage
= demuxer_get_percent_pos(mpctx
->demuxer
);
1552 if (percentage
>= 0)
1553 snprintf(percentage_text
, 9, " (%d%%)", percentage
);
1555 percentage_text
[0] = 0;
1557 if (opts
->osd_level
== 3)
1558 snprintf(osd_text_timer
, 63,
1559 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1560 mpctx
->osd_function
,pts
/3600,(pts
/60)%60,pts
%60,
1561 len
/3600,(len
/60)%60,len
%60,percentage_text
);
1563 snprintf(osd_text_timer
, 63, "%c %02d:%02d:%02d%s",
1564 mpctx
->osd_function
,pts
/3600,(pts
/60)%60,
1565 pts
%60,percentage_text
);
1567 osd_text_timer
[0]=0;
1569 if (strcmp(osd
->osd_text
, osd_text_timer
)) {
1570 strncpy(osd
->osd_text
, osd_text_timer
, 63);
1571 vo_osd_changed(OSDTYPE_OSD
);
1576 // Clear the term osd line
1577 if (term_osd
&& osd
->osd_text
[0]) {
1578 osd
->osd_text
[0] = 0;
1579 printf("%s\n",term_osd_esc
);
1587 void reinit_audio_chain(struct MPContext
*mpctx
)
1589 struct MPOpts
*opts
= &mpctx
->opts
;
1590 if(mpctx
->sh_audio
){
1591 current_module
="init_audio_codec";
1592 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
1593 if(!init_best_audio_codec(mpctx
->sh_audio
,audio_codec_list
,audio_fm_list
)){
1594 mpctx
->sh_audio
=mpctx
->d_audio
->sh
=NULL
; // failed to init :(
1595 mpctx
->d_audio
->id
= -2;
1598 mpctx
->initialized_flags
|=INITIALIZED_ACODEC
;
1599 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
1602 //const ao_info_t *info=audio_out->info;
1603 current_module
="af_preinit";
1604 ao_data
.samplerate
=force_srate
;
1606 ao_data
.format
=audio_output_format
;
1608 // first init to detect best values
1609 if(!init_audio_filters(mpctx
->sh_audio
, // preliminary init
1611 mpctx
->sh_audio
->samplerate
,
1613 &ao_data
.samplerate
, &ao_data
.channels
, &ao_data
.format
)){
1614 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_AudioFilterChainPreinitError
);
1615 exit_player(mpctx
, EXIT_ERROR
);
1618 current_module
="ao2_init";
1619 if(!(mpctx
->audio_out
=init_best_audio_out(opts
->audio_driver_list
,
1623 ao_data
.format
,0))){
1625 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_CannotInitAO
);
1626 uninit_player(mpctx
, INITIALIZED_ACODEC
); // close codec
1627 mpctx
->sh_audio
=mpctx
->d_audio
->sh
=NULL
; // -> nosound
1628 mpctx
->d_audio
->id
= -2;
1632 mpctx
->initialized_flags
|=INITIALIZED_AO
;
1633 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1634 mpctx
->audio_out
->info
->short_name
,
1635 ao_data
.samplerate
, ao_data
.channels
,
1636 af_fmt2str_short(ao_data
.format
),
1637 af_fmt2bits(ao_data
.format
)/8 );
1638 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AO: Description: %s\nAO: Author: %s\n",
1639 mpctx
->audio_out
->info
->name
, mpctx
->audio_out
->info
->author
);
1640 if(strlen(mpctx
->audio_out
->info
->comment
) > 0)
1641 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AO: Comment: %s\n", mpctx
->audio_out
->info
->comment
);
1642 // init audio filters:
1644 current_module
="af_init";
1645 if(!build_afilter_chain(mpctx
, mpctx
->sh_audio
, &ao_data
)) {
1646 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_NoMatchingFilter
);
1647 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1648 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1649 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1653 mpctx
->mixer
.audio_out
= mpctx
->audio_out
;
1654 mpctx
->mixer
.volstep
= volstep
;
1663 // Return pts value corresponding to the end point of audio written to the
1665 static double written_audio_pts(struct MPContext
*mpctx
)
1667 sh_audio_t
*sh_audio
= mpctx
->sh_audio
;
1668 demux_stream_t
*d_audio
= mpctx
->d_audio
;
1669 double buffered_output
;
1670 // first calculate the end pts of audio that has been output by decoder
1671 double a_pts
= sh_audio
->pts
;
1672 if (a_pts
!= MP_NOPTS_VALUE
)
1673 // Good, decoder supports new way of calculating audio pts.
1674 // sh_audio->pts is the timestamp of the latest input packet with
1675 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1676 // the amount of bytes the decoder has written after that timestamp.
1677 a_pts
+= sh_audio
->pts_bytes
/ (double) sh_audio
->o_bps
;
1679 // Decoder doesn't support new way of calculating pts (or we're
1680 // being called before it has decoded anything with known timestamp).
1681 // Use the old method of audio pts calculation: take the timestamp
1682 // of last packet with known pts the decoder has read data from,
1683 // and add amount of bytes read after the beginning of that packet
1684 // divided by input bps. This will be inaccurate if the input/output
1685 // ratio is not constant for every audio packet or if it is constant
1686 // but not accurately known in sh_audio->i_bps.
1688 a_pts
= d_audio
->pts
;
1689 // ds_tell_pts returns bytes read after last timestamp from
1690 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1691 // it has read but not decoded
1692 if (sh_audio
->i_bps
)
1693 a_pts
+= (ds_tell_pts(d_audio
) - sh_audio
->a_in_buffer_len
) /
1694 (double)sh_audio
->i_bps
;
1696 // Now a_pts hopefully holds the pts for end of audio from decoder.
1697 // Substract data in buffers between decoder and audio out.
1699 // Decoded but not filtered
1700 a_pts
-= sh_audio
->a_buffer_len
/ (double)sh_audio
->o_bps
;
1702 // Data buffered in audio filters, measured in bytes of "missing" output
1703 buffered_output
= af_calc_delay(sh_audio
->afilter
);
1705 // Data that was ready for ao but was buffered because ao didn't fully
1706 // accept everything to internal buffers yet
1707 buffered_output
+= sh_audio
->a_out_buffer_len
;
1709 // Filters divide audio length by playback_speed, so multiply by it
1710 // to get the length in original units without speedup or slowdown
1711 a_pts
-= buffered_output
* mpctx
->opts
.playback_speed
/ ao_data
.bps
;
1713 return a_pts
+ mpctx
->video_offset
;
1716 // Return pts value corresponding to currently playing audio.
1717 double playing_audio_pts(struct MPContext
*mpctx
)
1719 return written_audio_pts(mpctx
) - mpctx
->opts
.playback_speed
*
1720 mpctx
->audio_out
->get_delay();
1723 static int check_framedrop(struct MPContext
*mpctx
, double frame_time
) {
1724 struct MPOpts
*opts
= &mpctx
->opts
;
1725 // check for frame-drop:
1726 current_module
= "check_framedrop";
1727 if (mpctx
->sh_audio
&& !mpctx
->d_audio
->eof
) {
1728 static int dropped_frames
;
1729 float delay
= opts
->playback_speed
*mpctx
->audio_out
->get_delay();
1730 float d
= delay
-mpctx
->delay
;
1732 // we should avoid dropping too many frames in sequence unless we
1733 // are too late. and we allow 100ms A-V delay here:
1734 if (d
< -dropped_frames
*frame_time
-0.100 && !mpctx
->paused
1735 && !mpctx
->update_video_immediately
) {
1738 return frame_dropping
;
1750 static float timing_sleep(struct MPContext
*mpctx
, float time_frame
)
1754 // -------- RTC -----------
1755 current_module
="sleep_rtc";
1756 while (time_frame
> 0.000) {
1757 unsigned long rtc_ts
;
1758 if (read(rtc_fd
, &rtc_ts
, sizeof(rtc_ts
)) <= 0)
1759 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, MSGTR_LinuxRTCReadError
, strerror(errno
));
1760 time_frame
-= get_relative_time(mpctx
);
1765 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1766 // unnecessarily high CPU usage
1767 float margin
= softsleep
? 0.011 : 0;
1768 current_module
= "sleep_timer";
1769 while (time_frame
> margin
) {
1770 usec_sleep(1000000 * (time_frame
- margin
));
1771 time_frame
-= get_relative_time(mpctx
);
1774 current_module
= "sleep_soft";
1776 mp_msg(MSGT_AVSYNC
, MSGL_WARN
, MSGTR_SoftsleepUnderflow
);
1777 while (time_frame
> 0)
1778 time_frame
-= get_relative_time(mpctx
); // burn the CPU
1784 #ifdef CONFIG_DVDNAV
1788 /// store decoded video image
1789 static mp_image_t
* mp_dvdnav_copy_mpi(mp_image_t
*to_mpi
,
1790 mp_image_t
*from_mpi
) {
1793 /// Do not store B-frames
1794 if (from_mpi
->pict_type
== FF_B_TYPE
)
1798 to_mpi
->w
== from_mpi
->w
&&
1799 to_mpi
->h
== from_mpi
->h
&&
1800 to_mpi
->imgfmt
== from_mpi
->imgfmt
)
1804 free_mp_image(to_mpi
);
1805 if (from_mpi
->w
== 0 || from_mpi
->h
== 0)
1807 mpi
= alloc_mpi(from_mpi
->w
,from_mpi
->h
,from_mpi
->imgfmt
);
1810 copy_mpi(mpi
,from_mpi
);
1814 static void mp_dvdnav_reset_stream (MPContext
*ctx
) {
1815 struct MPOpts
*opts
= &ctx
->opts
;
1816 if (ctx
->sh_video
) {
1818 ctx
->d_video
->pts
= 0.0f
;
1819 ctx
->sh_video
->pts
= 0.0f
;
1820 ctx
->sh_video
->i_pts
= 0.0f
;
1821 ctx
->sh_video
->last_pts
= 0.0f
;
1822 ctx
->sh_video
->num_buffered_pts
= 0;
1823 ctx
->sh_video
->num_frames
= 0;
1824 ctx
->sh_video
->num_frames_decoded
= 0;
1825 ctx
->sh_video
->timer
= 0.0f
;
1826 ctx
->sh_video
->stream_delay
= 0.0f
;
1827 ctx
->sh_video
->timer
= 0;
1828 ctx
->demuxer
->stream_pts
= MP_NOPTS_VALUE
;
1831 if (ctx
->sh_audio
) {
1832 /// free audio packets and reset
1833 ds_free_packs(ctx
->d_audio
);
1834 audio_delay
-= ctx
->sh_audio
->stream_delay
;
1835 ctx
->delay
=- audio_delay
;
1836 ctx
->audio_out
->reset();
1837 resync_audio_stream(ctx
->sh_audio
);
1840 if (ctx
->d_sub
) opts
->sub_id
= -2;
1844 /// clear all EOF related flags
1845 ctx
->d_video
->eof
= ctx
->d_audio
->eof
= ctx
->stream
->eof
= 0;
1848 /// Restore last decoded DVDNAV (still frame)
1849 static mp_image_t
*mp_dvdnav_restore_smpi(struct MPContext
*mpctx
,
1851 unsigned char **start
,
1852 mp_image_t
*decoded_frame
)
1854 if (mpctx
->stream
->type
!= STREAMTYPE_DVDNAV
)
1855 return decoded_frame
;
1857 /// a change occured in dvdnav stream
1858 if (mp_dvdnav_cell_has_changed(mpctx
->stream
,0)) {
1859 mp_dvdnav_read_wait(mpctx
->stream
, 1, 1);
1860 mp_dvdnav_context_free(mpctx
);
1861 mp_dvdnav_reset_stream(mpctx
);
1862 mp_dvdnav_read_wait(mpctx
->stream
, 0, 1);
1863 mp_dvdnav_cell_has_changed(mpctx
->stream
,1);
1869 /// Display still frame, if any
1870 if (mpctx
->nav_smpi
&& !mpctx
->nav_buffer
)
1871 decoded_frame
= mpctx
->nav_smpi
;
1873 /// increment video frame : continue playing after still frame
1874 len
= demuxer_get_time_length(mpctx
->demuxer
);
1875 if (mpctx
->sh_video
->pts
>= len
&&
1876 mpctx
->sh_video
->pts
> 0.0 && len
> 0.0) {
1877 mp_dvdnav_skip_still(mpctx
->stream
);
1878 mp_dvdnav_skip_wait(mpctx
->stream
);
1880 mpctx
->sh_video
->pts
+= 1 / mpctx
->sh_video
->fps
;
1882 if (mpctx
->nav_buffer
) {
1883 *start
= mpctx
->nav_start
;
1884 *in_size
= mpctx
->nav_in_size
;
1885 if (mpctx
->nav_start
)
1886 memcpy(*start
,mpctx
->nav_buffer
,mpctx
->nav_in_size
);
1890 return decoded_frame
;
1893 /// Save last decoded DVDNAV (still frame)
1894 static void mp_dvdnav_save_smpi(struct MPContext
*mpctx
, int in_size
,
1895 unsigned char *start
,
1896 mp_image_t
*decoded_frame
)
1898 if (mpctx
->stream
->type
!= STREAMTYPE_DVDNAV
)
1901 if (mpctx
->nav_buffer
)
1902 free(mpctx
->nav_buffer
);
1904 mpctx
->nav_buffer
= malloc(in_size
);
1905 mpctx
->nav_start
= start
;
1906 mpctx
->nav_in_size
= mpctx
->nav_buffer
? in_size
: -1;
1907 if (mpctx
->nav_buffer
)
1908 memcpy(mpctx
->nav_buffer
,start
,in_size
);
1910 if (decoded_frame
&& mpctx
->nav_smpi
!= decoded_frame
)
1911 mpctx
->nav_smpi
= mp_dvdnav_copy_mpi(mpctx
->nav_smpi
,decoded_frame
);
1913 #endif /* CONFIG_DVDNAV */
1915 /* Modify video timing to match the audio timeline. There are two main
1916 * reasons this is needed. First, video and audio can start from different
1917 * positions at beginning of file or after a seek (MPlayer starts both
1918 * immediately even if they have different pts). Second, the file can have
1919 * audio timestamps that are inconsistent with the duration of the audio
1920 * packets, for example two consecutive timestamp values differing by
1921 * one second but only a packet with enough samples for half a second
1922 * of playback between them.
1924 static void adjust_sync(struct MPContext
*mpctx
, double frame_time
)
1926 struct MPOpts
*opts
= &mpctx
->opts
;
1927 current_module
= "av_sync";
1929 if (!mpctx
->sh_audio
)
1932 double a_pts
= written_audio_pts(mpctx
) - mpctx
->delay
;
1933 double v_pts
= mpctx
->sh_video
->pts
;
1934 double av_delay
= a_pts
- v_pts
;
1935 // Try to sync vo_flip() so it will *finish* at given time
1936 av_delay
+= mpctx
->last_vo_flip_duration
;
1937 av_delay
-= audio_delay
; // This much pts difference is desired
1939 double change
= av_delay
* 0.1;
1940 double max_change
= default_max_pts_correction
>= 0 ?
1941 default_max_pts_correction
: frame_time
* 0.1;
1942 if (change
< -max_change
)
1943 change
= -max_change
;
1944 else if (change
> max_change
)
1945 change
= max_change
;
1946 mpctx
->delay
+= change
;
1947 mpctx
->total_avsync_change
+= change
;
1950 static int fill_audio_out_buffers(struct MPContext
*mpctx
)
1952 struct MPOpts
*opts
= &mpctx
->opts
;
1959 sh_audio_t
* const sh_audio
= mpctx
->sh_audio
;
1961 current_module
="play_audio";
1965 // all the current uses of ao_data.pts seem to be in aos that handle
1966 // sync completely wrong; there should be no need to use ao_data.pts
1968 ao_data
.pts
= ((mpctx
->sh_video
?mpctx
->sh_video
->timer
:0)+mpctx
->delay
)*90000.0;
1969 bytes_to_write
= mpctx
->audio_out
->get_space();
1970 if (mpctx
->sh_video
|| bytes_to_write
>= ao_data
.outburst
)
1973 // handle audio-only case:
1974 // this is where mplayer sleeps during audio-only playback
1975 // to avoid 100% CPU use
1976 sleep_time
= (ao_data
.outburst
- bytes_to_write
) * 1000 / ao_data
.bps
;
1977 if (sleep_time
< 10) sleep_time
= 10; // limit to 100 wakeups per second
1978 usec_sleep(sleep_time
* 1000);
1981 while (bytes_to_write
) {
1982 playsize
= bytes_to_write
;
1983 if (playsize
> MAX_OUTBURST
)
1984 playsize
= MAX_OUTBURST
;
1985 bytes_to_write
-= playsize
;
1987 // Fill buffer if needed:
1988 current_module
="decode_audio";
1990 if (decode_audio(sh_audio
, playsize
) < 0) // EOF or error
1991 if (mpctx
->d_audio
->eof
) {
1993 if (sh_audio
->a_out_buffer_len
== 0)
1997 tt
= t
*0.000001f
; audio_time_usage
+=tt
;
1998 if (playsize
> sh_audio
->a_out_buffer_len
) {
1999 playsize
= sh_audio
->a_out_buffer_len
;
2001 playflags
|= AOPLAY_FINAL_CHUNK
;
2007 current_module
="play_audio";
2009 // Is this pts value actually useful for the aos that access it?
2010 // They're obviously badly broken in the way they handle av sync;
2011 // would not having access to this make them more broken?
2012 ao_data
.pts
= ((mpctx
->sh_video
?mpctx
->sh_video
->timer
:0)+mpctx
->delay
)*90000.0;
2013 playsize
= mpctx
->audio_out
->play(sh_audio
->a_out_buffer
, playsize
, playflags
);
2016 sh_audio
->a_out_buffer_len
-= playsize
;
2017 memmove(sh_audio
->a_out_buffer
, &sh_audio
->a_out_buffer
[playsize
],
2018 sh_audio
->a_out_buffer_len
);
2019 mpctx
->delay
+= opts
->playback_speed
*playsize
/(double)ao_data
.bps
;
2021 else if (audio_eof
&& mpctx
->audio_out
->get_delay() < .04) {
2022 // Sanity check to avoid hanging in case current ao doesn't output
2023 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2024 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, "Audio output truncated at end.\n");
2025 sh_audio
->a_out_buffer_len
= 0;
2031 static int sleep_until_update(struct MPContext
*mpctx
, float *time_frame
,
2032 float *aq_sleep_time
)
2034 struct MPOpts
*opts
= &mpctx
->opts
;
2035 int frame_time_remaining
= 0;
2036 current_module
="calc_sleep_time";
2038 *time_frame
-= get_relative_time(mpctx
); // reset timer
2040 if (mpctx
->sh_audio
&& !mpctx
->d_audio
->eof
) {
2041 float delay
= mpctx
->audio_out
->get_delay();
2042 mp_dbg(MSGT_AVSYNC
, MSGL_DBG2
, "delay=%f\n", delay
);
2046 * Adjust this raw delay value by calculating the expected
2047 * delay for this frame and generating a new value which is
2048 * weighted between the two. The higher autosync is, the
2049 * closer to the delay value gets to that which "-nosound"
2050 * would have used, and the longer it will take for A/V
2051 * sync to settle at the right value (but it eventually will.)
2052 * This settling time is very short for values below 100.
2054 float predicted
= mpctx
->delay
/ opts
->playback_speed
+ *time_frame
;
2055 float difference
= delay
- predicted
;
2056 delay
= predicted
+ difference
/ (float)autosync
;
2059 *time_frame
= delay
- mpctx
->delay
/ opts
->playback_speed
;
2061 // delay = amount of audio buffered in soundcard/driver
2062 if (delay
> 0.25) delay
=0.25; else
2063 if (delay
< 0.10) delay
=0.10;
2064 if (*time_frame
> delay
*0.6) {
2065 // sleep time too big - may cause audio drops (buffer underrun)
2066 frame_time_remaining
= 1;
2067 *time_frame
= delay
*0.5;
2070 // If we're lagging more than 200 ms behind the right playback rate,
2071 // don't try to "catch up".
2072 // If benchmark is set always output frames as fast as possible
2073 // without sleeping.
2074 if (*time_frame
< -0.2 || benchmark
)
2078 *aq_sleep_time
+= *time_frame
;
2081 //============================== SLEEP: ===================================
2083 // flag 256 means: libvo driver does its timing (dvb card)
2084 if (*time_frame
> 0.001 && !(mpctx
->sh_video
->output_flags
&256))
2085 *time_frame
= timing_sleep(mpctx
, *time_frame
);
2086 return frame_time_remaining
;
2089 int reinit_video_chain(struct MPContext
*mpctx
)
2091 struct MPOpts
*opts
= &mpctx
->opts
;
2092 sh_video_t
* const sh_video
= mpctx
->sh_video
;
2094 //================== Init VIDEO (codec & libvo) ==========================
2095 if (!opts
->fixed_vo
|| !(mpctx
->initialized_flags
& INITIALIZED_VO
)) {
2096 current_module
="preinit_libvo";
2098 //shouldn't we set dvideo->id=-2 when we fail?
2099 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2100 if(!(mpctx
->video_out
=init_best_video_out(opts
, mpctx
->x11_state
, mpctx
->key_fifo
, mpctx
->input
))){
2101 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_ErrorInitializingVODevice
);
2104 mpctx
->initialized_flags
|=INITIALIZED_VO
;
2107 if(stream_control(mpctx
->demuxer
->stream
, STREAM_CTRL_GET_ASPECT_RATIO
, &ar
) != STREAM_UNSUPPORTED
)
2108 mpctx
->sh_video
->stream_aspect
= ar
;
2109 current_module
="init_video_filters";
2111 char* vf_arg
[] = { "_oldargs_", (char*)mpctx
->video_out
, NULL
};
2112 sh_video
->vfilter
= vf_open_filter(opts
, NULL
,"vo",vf_arg
);
2116 char* vf_arg
[] = { "_oldargs_", menu_root
, NULL
};
2117 vf_menu
= vf_open_plugin(opts
,libmenu_vfs
,sh_video
->vfilter
,"menu",vf_arg
);
2119 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_CantOpenLibmenuFilterWithThisRootMenu
,menu_root
);
2124 sh_video
->vfilter
= vf_menu
;
2131 if (opts
->vf_settings
)
2132 for (i
= 0; opts
->vf_settings
[i
].name
; ++i
)
2133 if (strcmp(opts
->vf_settings
[i
].name
, "ass") == 0) {
2138 extern vf_info_t vf_info_ass
;
2139 const vf_info_t
* libass_vfs
[] = {&vf_info_ass
, NULL
};
2140 char* vf_arg
[] = {"auto", "1", NULL
};
2141 vf_instance_t
* vf_ass
= vf_open_plugin(opts
, libass_vfs
,sh_video
->vfilter
,"ass",vf_arg
);
2143 sh_video
->vfilter
= vf_ass
;
2145 mp_msg(MSGT_CPLAYER
,MSGL_ERR
, "ASS: cannot add video filter\n");
2150 sh_video
->vfilter
= append_filters(sh_video
->vfilter
, opts
->vf_settings
);
2154 sh_video
->vfilter
->control(sh_video
->vfilter
, VFCTRL_INIT_EOSD
, ass_library
);
2157 current_module
="init_video_codec";
2159 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
2160 init_best_video_codec(sh_video
,video_codec_list
,video_fm_list
);
2161 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
2163 if(!sh_video
->initialized
){
2164 if(!opts
->fixed_vo
) uninit_player(mpctx
, INITIALIZED_VO
);
2168 mpctx
->initialized_flags
|=INITIALIZED_VCODEC
;
2170 if (sh_video
->codec
)
2171 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_CODEC=%s\n", sh_video
->codec
->name
);
2173 sh_video
->last_pts
= MP_NOPTS_VALUE
;
2174 sh_video
->num_buffered_pts
= 0;
2175 sh_video
->next_frame_time
= 0;
2178 // Auto quality option enabled
2179 output_quality
=get_video_quality_max(sh_video
);
2180 if(auto_quality
>output_quality
) auto_quality
=output_quality
;
2181 else output_quality
=auto_quality
;
2182 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AutoQ: setting quality to %d.\n",output_quality
);
2183 set_video_quality(sh_video
,output_quality
);
2186 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2188 current_module
="init_vo";
2193 mpctx
->sh_video
= mpctx
->d_video
->sh
= NULL
;
2197 static double update_video_nocorrect_pts(struct MPContext
*mpctx
,
2200 struct sh_video
*sh_video
= mpctx
->sh_video
;
2202 double frame_time
= 0;
2204 current_module
= "filter_video";
2205 // In nocorrect-pts mode there is no way to properly time these frames
2206 if (vf_output_queued_frame(sh_video
->vfilter
))
2208 unsigned char *packet
= NULL
;
2209 frame_time
= sh_video
->next_frame_time
;
2210 if (mpctx
->update_video_immediately
)
2212 int in_size
= video_read_frame(sh_video
, &sh_video
->next_frame_time
,
2213 &packet
, force_fps
);
2215 #ifdef CONFIG_DVDNAV
2216 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
2217 if (mp_dvdnav_is_eof(mpctx
->stream
))
2220 mpctx
->d_video
->eof
= 0;
2222 mpctx
->d_audio
->eof
= 0;
2223 mpctx
->stream
->eof
= 0;
2228 if (in_size
> max_framesize
)
2229 max_framesize
= in_size
;
2230 sh_video
->timer
+= frame_time
;
2231 if (mpctx
->sh_audio
)
2232 mpctx
->delay
-= frame_time
;
2233 // video_read_frame can change fps (e.g. for ASF video)
2234 vo_fps
= sh_video
->fps
;
2235 int framedrop_type
= check_framedrop(mpctx
, frame_time
);
2236 current_module
= "decode video";
2238 void *decoded_frame
;
2239 #ifdef CONFIG_DVDNAV
2240 decoded_frame
= mp_dvdnav_restore_smpi(mpctx
, &in_size
, &packet
, NULL
);
2241 if (in_size
>= 0 && !decoded_frame
)
2243 decoded_frame
= decode_video(sh_video
, packet
, in_size
, framedrop_type
,
2245 #ifdef CONFIG_DVDNAV
2246 // Save last still frame for future display
2247 mp_dvdnav_save_smpi(mpctx
, in_size
, packet
, decoded_frame
);
2249 if (decoded_frame
) {
2250 // These updates are done here for vf_expand OSD/subtitles
2251 update_subtitles(sh_video
, mpctx
->d_sub
, mpctx
->video_offset
, 0);
2252 update_teletext(sh_video
, mpctx
->demuxer
, 0);
2253 update_osd_msg(mpctx
);
2254 current_module
= "filter video";
2255 if (filter_video(sh_video
, decoded_frame
, sh_video
->pts
))
2263 static double update_video(struct MPContext
*mpctx
, int *blit_frame
)
2265 struct sh_video
*sh_video
= mpctx
->sh_video
;
2267 sh_video
->vfilter
->control(sh_video
->vfilter
, VFCTRL_SET_OSD_OBJ
,
2268 mpctx
->osd
); // hack for vf_expand
2269 if (!mpctx
->opts
.correct_pts
)
2270 return update_video_nocorrect_pts(mpctx
, blit_frame
);
2275 current_module
= "filter_video";
2276 // XXX Time used in this call is not counted in any performance
2277 // timer now, OSD time is not updated correctly for filter-added frames
2278 if (vf_output_queued_frame(sh_video
->vfilter
))
2280 unsigned char *packet
= NULL
;
2281 bool hit_eof
= false;
2282 int in_size
= ds_get_packet_pts(mpctx
->d_video
, &packet
, &pts
);
2283 if (pts
!= MP_NOPTS_VALUE
)
2284 pts
+= mpctx
->video_offset
;
2286 // try to extract last frames in case of decoder lag
2291 if (in_size
> max_framesize
)
2292 max_framesize
= in_size
;
2293 current_module
= "decode video";
2294 int framedrop_type
= check_framedrop(mpctx
, sh_video
->frametime
);
2295 void *decoded_frame
= decode_video(sh_video
, packet
, in_size
,
2296 framedrop_type
, pts
);
2297 if (decoded_frame
) {
2298 // These updates are done here for vf_expand OSD/subtitles
2299 update_subtitles(sh_video
, mpctx
->d_sub
, mpctx
->video_offset
, 0);
2300 update_teletext(sh_video
, mpctx
->demuxer
, 0);
2301 update_osd_msg(mpctx
);
2302 current_module
= "filter video";
2303 if (filter_video(sh_video
, decoded_frame
, sh_video
->pts
))
2309 sh_video
->vfilter
->control(sh_video
->vfilter
, VFCTRL_GET_PTS
, &pts
);
2310 if (pts
== MP_NOPTS_VALUE
) {
2311 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Video pts after filters MISSING\n");
2312 // Try to use decoder pts from before filters
2313 pts
= sh_video
->pts
;
2315 sh_video
->pts
= pts
;
2316 if (sh_video
->last_pts
== MP_NOPTS_VALUE
)
2317 sh_video
->last_pts
= sh_video
->pts
;
2318 else if (sh_video
->last_pts
> sh_video
->pts
) {
2319 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Decreasing video pts: %f < %f\n",
2320 sh_video
->pts
, sh_video
->last_pts
);
2321 /* If the difference in pts is small treat it as jitter around the
2322 * right value (possibly caused by incorrect timestamp ordering) and
2323 * just show this frame immediately after the last one.
2324 * Treat bigger differences as timestamp resets and start counting
2325 * timing of later frames from the position of this one. */
2326 if (sh_video
->last_pts
- sh_video
->pts
> 0.5)
2327 sh_video
->last_pts
= sh_video
->pts
;
2329 sh_video
->pts
= sh_video
->last_pts
;
2331 double frame_time
= sh_video
->pts
- sh_video
->last_pts
;
2332 sh_video
->last_pts
= sh_video
->pts
;
2333 sh_video
->timer
+= frame_time
;
2334 if (mpctx
->sh_audio
)
2335 mpctx
->delay
-= frame_time
;
2340 void pause_player(struct MPContext
*mpctx
)
2345 mpctx
->step_frames
= 0;
2346 mpctx
->time_frame
-= get_relative_time(mpctx
);
2348 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
)
2349 vo_control(mpctx
->video_out
, VOCTRL_PAUSE
, NULL
);
2351 if (mpctx
->audio_out
&& mpctx
->sh_audio
)
2352 mpctx
->audio_out
->pause(); // pause audio, keep data if possible
2355 void unpause_player(struct MPContext
*mpctx
)
2361 if (mpctx
->audio_out
&& mpctx
->sh_audio
)
2362 mpctx
->audio_out
->resume(); // resume audio
2363 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
2364 && !mpctx
->step_frames
)
2365 vo_control(mpctx
->video_out
, VOCTRL_RESUME
, NULL
); // resume video
2366 (void)get_relative_time(mpctx
); // ignore time that passed during pause
2369 void add_step_frame(struct MPContext
*mpctx
)
2371 mpctx
->step_frames
++;
2372 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
)
2373 vo_control(mpctx
->video_out
, VOCTRL_PAUSE
, NULL
);
2374 unpause_player(mpctx
);
2377 static void pause_loop(struct MPContext
*mpctx
)
2381 // Small hack to display the pause message on the OSD line.
2382 // The pause string is: "\n == PAUSE == \r" so we need to
2383 // take the first and the last char out
2384 if (term_osd
&& !mpctx
->sh_video
) {
2385 char msg
[128] = MSGTR_Paused
;
2386 int mlen
= strlen(msg
);
2388 set_osd_msg(OSD_MSG_PAUSE
, 1, 0, "%s", msg
+1);
2389 update_osd_msg(mpctx
);
2391 mp_msg(MSGT_CPLAYER
,MSGL_STATUS
,MSGTR_Paused
);
2392 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_PAUSED\n");
2396 guiGetEvent(guiCEvent
, (char *)guiSetPause
);
2399 while ( (cmd
= mp_input_get_cmd(mpctx
->input
, 20, 1, 1)) == NULL
2400 || cmd
->id
== MP_CMD_SET_MOUSE_POS
|| cmd
->pausing
== 4) {
2402 cmd
= mp_input_get_cmd(mpctx
->input
, 0,1,0);
2403 run_command(mpctx
, cmd
);
2407 if (mpctx
->sh_video
&& mpctx
->video_out
)
2408 vo_check_events(mpctx
->video_out
);
2412 guiGetEvent(guiReDraw
, NULL
);
2413 if (guiIntfStruct
.Playing
!=2 || (mpctx
->rel_seek_secs
|| mpctx
->abs_seek_pos
))
2419 vf_menu_pause_update(vf_menu
);
2422 update_osd_msg(mpctx
);
2423 int hack
= vo_osd_changed(0);
2424 vo_osd_changed(hack
);
2430 if (guiIntfStruct
.Playing
== guiSetStop
)
2433 guiGetEvent(guiCEvent
, (char *)guiSetPlay
);
2439 // Find the right mute status and record position for new file position
2440 static void edl_seek_reset(MPContext
*mpctx
)
2442 mpctx
->edl_muted
= 0;
2443 next_edl_record
= edl_records
;
2445 while (next_edl_record
) {
2446 if (next_edl_record
->start_sec
>= mpctx
->sh_video
->pts
)
2449 if (next_edl_record
->action
== EDL_MUTE
)
2450 mpctx
->edl_muted
= !mpctx
->edl_muted
;
2451 next_edl_record
= next_edl_record
->next
;
2453 if ((mpctx
->user_muted
| mpctx
->edl_muted
) != mpctx
->mixer
.muted
)
2454 mixer_mute(&mpctx
->mixer
);
2458 // Execute EDL command for the current position if one exists
2459 static void edl_update(MPContext
*mpctx
)
2461 if (!next_edl_record
)
2464 if (!mpctx
->sh_video
) {
2465 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, MSGTR_EdlNOsh_video
);
2466 free_edl(edl_records
);
2467 next_edl_record
= NULL
;
2472 if (mpctx
->sh_video
->pts
>= next_edl_record
->start_sec
) {
2473 if (next_edl_record
->action
== EDL_SKIP
) {
2474 mpctx
->osd_function
= OSD_FFW
;
2475 mpctx
->abs_seek_pos
= 0;
2476 mpctx
->rel_seek_secs
= next_edl_record
->length_sec
;
2477 mp_msg(MSGT_CPLAYER
, MSGL_DBG4
, "EDL_SKIP: start [%f], stop "
2478 "[%f], length [%f]\n", next_edl_record
->start_sec
,
2479 next_edl_record
->stop_sec
, next_edl_record
->length_sec
);
2481 else if (next_edl_record
->action
== EDL_MUTE
) {
2482 mpctx
->edl_muted
= !mpctx
->edl_muted
;
2483 if ((mpctx
->user_muted
| mpctx
->edl_muted
) != mpctx
->mixer
.muted
)
2484 mixer_mute(&mpctx
->mixer
);
2485 mp_msg(MSGT_CPLAYER
, MSGL_DBG4
, "EDL_MUTE: [%f]\n",
2486 next_edl_record
->start_sec
);
2488 next_edl_record
= next_edl_record
->next
;
2492 static void reinit_decoders(struct MPContext
*mpctx
)
2494 reinit_video_chain(mpctx
);
2495 reinit_audio_chain(mpctx
);
2496 mp_property_do("sub", M_PROPERTY_SET
, &mpctx
->global_sub_pos
, mpctx
);
2499 static bool timeline_set_part(struct MPContext
*mpctx
, int i
)
2501 struct timeline_part
*p
= mpctx
->timeline
+ mpctx
->timeline_part
;
2502 struct timeline_part
*n
= mpctx
->timeline
+ i
;
2503 mpctx
->timeline_part
= i
;
2504 mpctx
->video_offset
= n
->start
- n
->source_start
;
2505 if (n
->source
== p
->source
)
2507 uninit_player(mpctx
, INITIALIZED_VCODEC
| (mpctx
->opts
.fixed_vo
&& mpctx
->opts
.video_id
!= -2 ? 0 : INITIALIZED_VO
) | INITIALIZED_AO
| INITIALIZED_ACODEC
);
2508 mpctx
->demuxer
= n
->source
->demuxer
;
2509 mpctx
->d_video
= mpctx
->demuxer
->video
;
2510 mpctx
->d_audio
= mpctx
->demuxer
->audio
;
2511 mpctx
->d_sub
= mpctx
->demuxer
->sub
;
2512 mpctx
->sh_video
= mpctx
->d_video
->sh
;
2513 mpctx
->sh_audio
= mpctx
->d_audio
->sh
;
2517 // Given pts, switch playback to the corresponding part.
2518 // Return offset within that part.
2519 static double timeline_set_from_time(struct MPContext
*mpctx
, double pts
,
2524 for (int i
= 0; i
< mpctx
->num_timeline_parts
; i
++) {
2525 struct timeline_part
*p
= mpctx
->timeline
+ i
;
2526 if (pts
< (p
+1)->start
) {
2527 *need_reset
= timeline_set_part(mpctx
, i
);
2528 return pts
- p
->start
+ p
->source_start
;
2535 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2536 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2537 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2538 static int seek(MPContext
*mpctx
, double amount
, int style
)
2540 current_module
= "seek";
2541 if (mpctx
->stop_play
== AT_END_OF_FILE
)
2542 mpctx
->stop_play
= KEEP_PLAYING
;
2543 if (mpctx
->timeline
&& style
& SEEK_FACTOR
) {
2544 amount
*= mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
2545 style
&= ~SEEK_FACTOR
;
2547 if ((mpctx
->demuxer
->accurate_seek
|| mpctx
->timeline
) && mpctx
->sh_video
2548 && !(style
& (SEEK_ABSOLUTE
| SEEK_FACTOR
))) {
2549 style
|= SEEK_ABSOLUTE
;
2551 style
|= SEEK_FORWARD
;
2553 style
|= SEEK_BACKWARD
;
2554 amount
+= mpctx
->sh_video
->pts
;
2557 /* At least the liba52 decoder wants to read from the input stream
2558 * during initialization, so reinit must be done after the demux_seek()
2559 * call that clears possible stream EOF. */
2560 bool need_reset
= false;
2561 if (mpctx
->timeline
) {
2562 amount
= timeline_set_from_time(mpctx
, amount
, &need_reset
);
2564 mpctx
->stop_play
= AT_END_OF_FILE
;
2565 // Clear audio from current position
2566 if (mpctx
->sh_audio
) {
2567 mpctx
->audio_out
->reset();
2568 mpctx
->sh_audio
->a_buffer_len
= 0;
2569 mpctx
->sh_audio
->a_out_buffer_len
= 0;
2574 int seekresult
= demux_seek(mpctx
->demuxer
, amount
, audio_delay
, style
);
2576 reinit_decoders(mpctx
);
2577 if (seekresult
== 0)
2580 if (mpctx
->sh_video
) {
2581 current_module
= "seek_video_reset";
2582 resync_video_stream(mpctx
->sh_video
);
2583 if (mpctx
->video_out
->config_ok
)
2584 vo_control(mpctx
->video_out
, VOCTRL_RESET
, NULL
);
2585 mpctx
->sh_video
->num_buffered_pts
= 0;
2586 mpctx
->sh_video
->last_pts
= MP_NOPTS_VALUE
;
2587 mpctx
->num_buffered_frames
= 0;
2589 mpctx
->time_frame
= 0;
2590 mpctx
->update_video_immediately
= true;
2591 // Not all demuxers set d_video->pts during seek, so this value
2592 // (which is used by at least vobsub and edl code below) may
2593 // be completely wrong (probably 0).
2594 mpctx
->sh_video
->pts
= mpctx
->d_video
->pts
+ mpctx
->video_offset
;
2595 update_subtitles(mpctx
->sh_video
, mpctx
->d_sub
, mpctx
->video_offset
, 1);
2596 update_teletext(mpctx
->sh_video
, mpctx
->demuxer
, 1);
2599 if (mpctx
->sh_audio
) {
2600 current_module
= "seek_audio_reset";
2601 mpctx
->audio_out
->reset(); // stop audio, throwing away buffered data
2602 mpctx
->sh_audio
->a_buffer_len
= 0;
2603 mpctx
->sh_audio
->a_out_buffer_len
= 0;
2606 if (vo_vobsub
&& mpctx
->sh_video
) {
2607 current_module
= "seek_vobsub_reset";
2608 vobsub_seek(vo_vobsub
, mpctx
->sh_video
->pts
);
2611 edl_seek_reset(mpctx
);
2613 mpctx
->total_avsync_change
= 0;
2614 audio_time_usage
= 0; video_time_usage
= 0; vout_time_usage
= 0;
2617 current_module
= NULL
;
2621 static int find_ordered_chapter_sources(struct MPContext
*mpctx
,
2622 struct content_source
*sources
,
2624 unsigned char uid_map
[][16])
2626 int num_filenames
= 0;
2627 char **filenames
= NULL
;
2628 if (num_sources
> 1) {
2629 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "This file references data from "
2630 "other sources.\n");
2631 if (mpctx
->stream
->type
!= STREAMTYPE_FILE
) {
2632 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, "Playback source is not a "
2633 "normal disk file. Will not search for related files.\n");
2635 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Will scan other files in the "
2636 "same directory to find referenced sources.\n");
2637 filenames
= find_files(mpctx
->demuxer
->filename
, ".mkv",
2642 int num_left
= num_sources
- 1;
2643 for (int i
= 0; i
< num_filenames
&& num_left
> 0; i
++) {
2644 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Checking file %s\n",
2645 filename_recode(filenames
[i
]));
2647 struct stream
*s
= open_stream(filenames
[i
], &mpctx
->opts
, &format
);
2650 struct demuxer
*d
= demux_open(&mpctx
->opts
, s
, DEMUXER_TYPE_MATROSKA
,
2651 mpctx
->opts
.audio_id
,
2652 mpctx
->opts
.video_id
,
2653 mpctx
->opts
.sub_id
, filenames
[i
]);
2658 if (d
->file_format
== DEMUXER_TYPE_MATROSKA
) {
2659 for (int i
= 1; i
< num_sources
; i
++) {
2660 if (sources
[i
].demuxer
)
2662 if (!memcmp(uid_map
[i
], d
->matroska_data
.segment_uid
, 16)) {
2663 mp_msg(MSGT_CPLAYER
, MSGL_INFO
,"Match for source %d: %s\n",
2664 i
, filename_recode(d
->filename
));
2665 sources
[i
].stream
= s
;
2666 sources
[i
].demuxer
= d
;
2678 talloc_free(filenames
);
2680 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Failed to find ordered chapter part!\n"
2681 "There will be parts MISSING from the video!\n");
2682 for (int i
= 1, j
= 1; i
< num_sources
; i
++)
2683 if (sources
[i
].demuxer
) {
2684 sources
[j
] = sources
[i
];
2685 memcpy(uid_map
[j
], uid_map
[i
], 16);
2689 return num_sources
- num_left
;
2692 static void build_ordered_chapter_timeline(struct MPContext
*mpctx
)
2694 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "File uses ordered chapters, will build "
2695 "edit timeline.\n");
2697 struct demuxer
*demuxer
= mpctx
->demuxer
;
2698 struct matroska_data
*m
= &demuxer
->matroska_data
;
2700 struct content_source
*sources
= talloc_array_ptrtype(NULL
, sources
,
2701 m
->num_ordered_chapters
);
2702 sources
[0].stream
= mpctx
->stream
;
2703 sources
[0].demuxer
= mpctx
->demuxer
;
2704 unsigned char uid_map
[m
->num_ordered_chapters
][16];
2705 int num_sources
= 1;
2706 memcpy(uid_map
[0], m
->segment_uid
, 16);
2708 for (int i
= 0; i
< m
->num_ordered_chapters
; i
++) {
2709 struct matroska_chapter
*c
= m
->ordered_chapters
+ i
;
2710 if (!c
->has_segment_uid
)
2711 memcpy(c
->segment_uid
, m
->segment_uid
, 16);
2713 for (int j
= 0; j
< num_sources
; j
++)
2714 if (!memcmp(c
->segment_uid
, uid_map
[j
], 16))
2716 memcpy(uid_map
[num_sources
], c
->segment_uid
, 16);
2717 sources
[num_sources
] = (struct content_source
){};
2723 num_sources
= find_ordered_chapter_sources(mpctx
, sources
, num_sources
,
2727 struct timeline_part
*timeline
= talloc_array_ptrtype(NULL
, timeline
,
2728 m
->num_ordered_chapters
+ 1);
2729 uint64_t starttime
= 0;
2730 uint64_t missing_time
= 0;
2732 for (int i
= 0; i
< m
->num_ordered_chapters
; i
++) {
2733 struct matroska_chapter
*c
= m
->ordered_chapters
+ i
;
2736 for (j
= 0; j
< num_sources
; j
++) {
2737 if (!memcmp(c
->segment_uid
, uid_map
[j
], 16))
2740 missing_time
+= c
->end
- c
->start
;
2743 // Only add a separate part if the time or file actually changes
2744 uint64_t prev_end
= !part_count
? 0 : starttime
2745 - timeline
[part_count
- 1].start
2746 + timeline
[part_count
- 1].source_start
;
2747 if (part_count
== 0 || c
->start
!= prev_end
2748 || sources
+ j
!= timeline
[part_count
- 1].source
) {
2749 timeline
[part_count
].source
= sources
+ j
;
2750 timeline
[part_count
].start
= starttime
/ 1000.;
2751 timeline
[part_count
].source_start
= c
->start
/ 1000.;
2754 starttime
+= c
->end
- c
->start
;
2756 timeline
[part_count
].start
= starttime
/ 1000.;
2759 // None of the parts come from the file itself???
2760 talloc_free(sources
);
2761 talloc_free(timeline
);
2765 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Timeline contains %d parts from %d "
2766 "sources. Total length %.3f seconds.\n", part_count
, num_sources
,
2767 timeline
[part_count
].start
);
2769 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "There are %.3f seconds missing "
2770 "from the timeline!\n", missing_time
/ 1000.);
2771 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Source files:\n");
2772 for (int i
= 0; i
< num_sources
; i
++)
2773 mp_msg(MSGT_CPLAYER
, MSGL_V
, "%d: %s\n", i
,
2774 filename_recode(sources
[i
].demuxer
->filename
));
2775 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Timeline parts: (number, start, "
2776 "source_start, source):\n");
2777 for (int i
= 0; i
< part_count
; i
++) {
2778 struct timeline_part
*p
= timeline
+ i
;
2779 mp_msg(MSGT_CPLAYER
, MSGL_V
, "%3d %9.3f %9.3f %3td\n", i
, p
->start
,
2780 p
->source_start
, p
->source
- sources
);
2782 mp_msg(MSGT_CPLAYER
, MSGL_V
, "END %9.3f\n", timeline
[part_count
].start
);
2783 mpctx
->sources
= sources
;
2784 mpctx
->num_sources
= num_sources
;
2785 mpctx
->timeline
= timeline
;
2786 mpctx
->num_timeline_parts
= part_count
;
2787 mpctx
->timeline_part
= 0;
2788 mpctx
->video_offset
= timeline
[0].source_start
;
2789 mpctx
->demuxer
= timeline
[0].source
->demuxer
;
2793 static int read_keys(void *ctx
, int fd
)
2796 return mplayer_get_key(ctx
, 0);
2800 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2801 * file for some tools to link against. */
2802 #ifndef DISABLE_MAIN
2803 int main(int argc
,char* argv
[]){
2810 /* Flag indicating whether MPlayer should exit without playing anything. */
2813 //float a_frame=0; // Audio
2817 int gui_no_filename
=0;
2819 struct MPContext
*mpctx
= &(struct MPContext
){
2820 .osd_function
= OSD_PLAY
,
2821 .begin_skip
= MP_NOPTS_VALUE
,
2822 .play_tree_step
= 1,
2823 .global_sub_pos
= -1,
2824 .set_of_sub_pos
= -1,
2825 .file_format
= DEMUXER_TYPE_UNKNOWN
,
2830 srand(GetTimerMS());
2835 mpctx
->x11_state
= vo_x11_init_state();
2837 struct MPOpts
*opts
= &mpctx
->opts
;
2838 set_default_mplayer_options(opts
);
2839 // Create the config context and register the options
2840 mpctx
->mconfig
= m_config_new(opts
, cfg_include
);
2841 m_config_register_options(mpctx
->mconfig
,mplayer_opts
);
2842 mp_input_register_options(mpctx
->mconfig
);
2844 // Preparse the command line
2845 m_config_preparse_command_line(mpctx
->mconfig
,argc
,argv
);
2847 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2852 stream_tv_defaults
.immediate
= 1;
2855 if (argc
> 1 && argv
[1] &&
2856 (!strcmp(argv
[1], "-gui") || !strcmp(argv
[1], "-nogui"))) {
2857 use_gui
= !strcmp(argv
[1], "-gui");
2861 char *base
= strrchr(argv
[0], '/');
2863 base
= strrchr(argv
[0], '\\');
2866 if(strstr(base
, "gmplayer"))
2870 parse_cfgfiles(mpctx
, mpctx
->mconfig
);
2873 if ( use_gui
) cfg_read();
2876 mpctx
->playtree
= m_config_parse_mp_command_line(mpctx
->mconfig
, argc
, argv
);
2877 if(mpctx
->playtree
== NULL
)
2880 mpctx
->playtree
= play_tree_cleanup(mpctx
->playtree
);
2881 if(mpctx
->playtree
) {
2882 mpctx
->playtree_iter
= play_tree_iter_new(mpctx
->playtree
,mpctx
->mconfig
);
2883 if(mpctx
->playtree_iter
) {
2884 if(play_tree_iter_step(mpctx
->playtree_iter
,0,0) != PLAY_TREE_ITER_ENTRY
) {
2885 play_tree_iter_free(mpctx
->playtree_iter
);
2886 mpctx
->playtree_iter
= NULL
;
2888 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
,1);
2892 mpctx
->key_fifo
= mp_fifo_create(opts
);
2894 print_version("MPlayer");
2896 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_GUI)
2897 void *runningmplayer
= FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2898 if(runningmplayer
&& mpctx
->filename
&& use_gui
){
2899 COPYDATASTRUCT csData
;
2900 char file
[MAX_PATH
];
2901 char *filepart
= mpctx
->filename
;
2902 if(GetFullPathName(mpctx
->filename
, MAX_PATH
, file
, &filepart
)){
2904 csData
.cbData
= strlen(file
)*2;
2905 csData
.lpData
= file
;
2906 SendMessage(runningmplayer
, WM_COPYDATA
, (WPARAM
)runningmplayer
, (LPARAM
)&csData
);
2911 #if defined(__MINGW32__) || defined(__CYGWIN__)
2912 // stop Windows from showing all kinds of annoying error dialogs
2913 SetErrorMode(0x8003);
2914 // request 1ms timer resolution
2918 #ifdef CONFIG_PRIORITY
2924 mp_msg(MSGT_CPLAYER
,MSGL_WARN
,MSGTR_NoGui
);
2928 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
2929 if(use_gui
&& !vo_init()){
2930 mp_msg(MSGT_CPLAYER
,MSGL_WARN
,MSGTR_GuiNeedsX
);
2934 if (use_gui
&& mpctx
->playtree_iter
){
2935 char cwd
[PATH_MAX
+2];
2936 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2937 play_tree_iter_free(mpctx
->playtree_iter
);
2938 mpctx
->playtree_iter
=NULL
;
2940 if (getcwd(cwd
, PATH_MAX
) != (char *)NULL
)
2943 // Prefix relative paths with current working directory
2944 play_tree_add_bpf(mpctx
->playtree
, cwd
);
2946 // Import initital playtree into GUI.
2947 import_initial_playtree_into_gui(mpctx
->playtree
, mpctx
->mconfig
, enqueue
);
2949 #endif /* CONFIG_GUI */
2951 if(opts
->video_driver_list
&& strcmp(opts
->video_driver_list
[0],"help")==0){
2956 if(opts
->audio_driver_list
&& strcmp(opts
->audio_driver_list
[0],"help")==0){
2961 /* Check codecs.conf. */
2962 if(!codecs_file
|| !parse_codec_cfg(codecs_file
)){
2963 if(!parse_codec_cfg(mem_ptr
=get_path("codecs.conf"))){
2964 if(!parse_codec_cfg(MPLAYER_CONFDIR
"/codecs.conf")){
2965 if(!parse_codec_cfg(NULL
)){
2966 exit_player_with_rc(mpctx
, EXIT_NONE
, 0);
2968 mp_msg(MSGT_CPLAYER
,MSGL_V
,MSGTR_BuiltinCodecsConf
);
2971 free( mem_ptr
); // release the buffer created by get_path()
2975 if(video_codec_list
){
2977 video_codec
=video_codec_list
[0];
2978 for(i
=0;video_codec_list
[i
];i
++)
2979 mp_msg(MSGT_FIXME
,MSGL_FIXME
,"vc#%d: '%s'\n",i
,video_codec_list
[i
]);
2982 if(audio_codec_list
&& strcmp(audio_codec_list
[0],"help")==0){
2983 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, MSGTR_AvailableAudioCodecs
);
2984 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_CODECS\n");
2986 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
2989 if(video_codec_list
&& strcmp(video_codec_list
[0],"help")==0){
2990 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, MSGTR_AvailableVideoCodecs
);
2991 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_CODECS\n");
2993 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
2996 if(video_fm_list
&& strcmp(video_fm_list
[0],"help")==0){
2998 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3001 if(audio_fm_list
&& strcmp(audio_fm_list
[0],"help")==0){
3003 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3006 if(af_cfg
.list
&& strcmp(af_cfg
.list
[0],"help")==0){
3012 if(vo_fstype_list
&& strcmp(vo_fstype_list
[0],"help")==0){
3014 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3018 if((demuxer_name
&& strcmp(demuxer_name
,"help")==0) ||
3019 (audio_demuxer_name
&& strcmp(audio_demuxer_name
,"help")==0) ||
3020 (sub_demuxer_name
&& strcmp(sub_demuxer_name
,"help")==0)){
3022 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "\n");
3025 if(list_properties
) {
3026 property_print_help();
3031 exit_player(mpctx
, EXIT_NONE
);
3033 if (player_idle_mode
&& use_gui
) {
3034 mp_msg(MSGT_CPLAYER
, MSGL_FATAL
, MSGTR_NoIdleAndGui
);
3035 exit_player_with_rc(mpctx
, EXIT_NONE
, 1);
3038 if(!mpctx
->filename
&& !player_idle_mode
){
3040 // no file/vcd/dvd -> show HELP:
3041 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, help_text
);
3042 exit_player_with_rc(mpctx
, EXIT_NONE
, 0);
3043 } else gui_no_filename
=1;
3046 /* Display what configure line was used */
3047 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Configuration: " CONFIGURATION
"\n");
3049 // Many users forget to include command line in bugreports...
3050 if( mp_msg_test(MSGT_CPLAYER
,MSGL_V
) ){
3051 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, MSGTR_CommandLine
);
3052 for(i
=1;i
<argc
;i
++)mp_msg(MSGT_CPLAYER
, MSGL_INFO
," '%s'",argv
[i
]);
3053 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "\n");
3056 //------ load global data first ------
3058 mpctx
->osd
= osd_create();
3061 #ifdef CONFIG_FREETYPE
3064 #ifdef CONFIG_FONTCONFIG
3065 if(font_fontconfig
<= 0)
3068 #ifdef CONFIG_BITMAP_FONT
3070 vo_font
=read_font_desc(font_name
,font_factor
,verbose
>1);
3071 if(!vo_font
) mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_CantLoadFont
,
3072 filename_recode(font_name
));
3075 vo_font
=read_font_desc( mem_ptr
=get_path("font/font.desc"),font_factor
,verbose
>1);
3076 free(mem_ptr
); // release the buffer created by get_path()
3078 vo_font
=read_font_desc(MPLAYER_DATADIR
"/font/font.desc",font_factor
,verbose
>1);
3081 mpctx
->osd
->sub_font
= read_font_desc(sub_font_name
, font_factor
, verbose
>1);
3083 mpctx
->osd
->sub_font
= vo_font
;
3085 #ifdef CONFIG_FONTCONFIG
3090 ass_library
= ass_init();
3096 // seteuid(0); /* Can't hurt to try to get root here */
3097 if ((rtc_fd
= open(rtc_device
? rtc_device
: "/dev/rtc", O_RDONLY
)) < 0)
3098 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, MSGTR_RTCDeviceNotOpenable
,
3099 rtc_device
? rtc_device
: "/dev/rtc", strerror(errno
));
3101 unsigned long irqp
= 1024; /* 512 seemed OK. 128 is jerky. */
3103 if (ioctl(rtc_fd
, RTC_IRQP_SET
, irqp
) < 0) {
3104 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, MSGTR_LinuxRTCInitErrorIrqpSet
, irqp
, strerror(errno
));
3105 mp_msg(MSGT_CPLAYER
, MSGL_HINT
, MSGTR_IncreaseRTCMaxUserFreq
, irqp
);
3108 } else if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
3109 /* variable only by the root */
3110 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, MSGTR_LinuxRTCInitErrorPieOn
, strerror(errno
));
3114 mp_msg(MSGT_CPLAYER
, MSGL_V
, MSGTR_UsingRTCTiming
, irqp
);
3118 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
3119 // and now ? -- Pontscho
3120 if(use_gui
) setuid( getuid() ); // strongly test, please check this.
3123 #endif /* HAVE_RTC */
3124 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Using %s timing\n",
3125 softsleep
?"software":timer_name
);
3128 if ( !use_gui
) load_termcap(NULL
); // load key-codes
3131 // ========== Init keyboard FIFO (connection to libvo) ============
3133 // Init input system
3134 current_module
= "init_input";
3135 mpctx
->input
= mp_input_init(&opts
->input
, use_gui
);
3136 mp_input_add_key_fd(mpctx
->input
, -1,0,mplayer_get_key
,NULL
, mpctx
->key_fifo
);
3138 mp_input_add_cmd_fd(mpctx
->input
, 0,USE_SELECT
,MP_INPUT_SLAVE_CMD_FUNC
,NULL
);
3139 else if(!noconsolecontrols
)
3140 mp_input_add_key_fd(mpctx
->input
, 0, 1, read_keys
, NULL
, mpctx
->key_fifo
);
3141 // Set the libstream interrupt callback
3142 stream_set_interrupt_callback(mp_input_check_interrupt
, mpctx
->input
);
3146 if(menu_cfg
&& menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
, menu_cfg
))
3147 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_MenuInitialized
, menu_cfg
);
3149 menu_cfg
= get_path("menu.conf");
3150 if(menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
, menu_cfg
))
3151 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_MenuInitialized
, menu_cfg
);
3153 if(menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
,
3154 MPLAYER_CONFDIR
"/menu.conf"))
3155 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_MenuInitialized
, MPLAYER_CONFDIR
"/menu.conf");
3157 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_MenuInitFailed
);
3165 current_module
= NULL
;
3169 signal(SIGCHLD
,child_sighandler
);
3172 #ifdef CONFIG_CRASH_DEBUG
3173 prog_path
= argv
[0];
3175 //========= Catch terminate signals: ================
3176 // terminate requests:
3177 signal(SIGTERM
,exit_sighandler
); // kill
3178 signal(SIGHUP
,exit_sighandler
); // kill -HUP / xterm closed
3180 signal(SIGINT
,exit_sighandler
); // Interrupt from keyboard
3182 signal(SIGQUIT
,exit_sighandler
); // Quit from keyboard
3183 signal(SIGPIPE
,exit_sighandler
); // Some window managers cause this
3184 #ifdef CONFIG_SIGHANDLER
3186 signal(SIGBUS
,exit_sighandler
); // bus error
3187 signal(SIGSEGV
,exit_sighandler
); // segfault
3188 signal(SIGILL
,exit_sighandler
); // illegal instruction
3189 signal(SIGFPE
,exit_sighandler
); // floating point exc.
3190 signal(SIGABRT
,exit_sighandler
); // abort()
3191 #ifdef CONFIG_CRASH_DEBUG
3193 signal(SIGTRAP
,exit_sighandler
);
3200 guiGetEvent(guiSetContext
, mpctx
);
3201 mpctx
->initialized_flags
|=INITIALIZED_GUI
;
3202 guiGetEvent( guiCEvent
,(char *)((gui_no_filename
) ? 0 : 1) );
3206 // ******************* Now, let's see the per-file stuff ********************
3210 // init global sub numbers
3211 mpctx
->global_sub_size
= 0;
3212 { int i
; for (i
= 0; i
< SUB_SOURCES
; i
++) mpctx
->global_sub_indices
[i
] = -1; }
3214 if (mpctx
->filename
) {
3215 load_per_protocol_config (mpctx
->mconfig
, mpctx
->filename
);
3216 load_per_extension_config (mpctx
->mconfig
, mpctx
->filename
);
3217 load_per_file_config (mpctx
->mconfig
, mpctx
->filename
);
3220 if (opts
->video_driver_list
)
3221 load_per_output_config (mpctx
->mconfig
, PROFILE_CFG_VO
, opts
->video_driver_list
[0]);
3222 if (opts
->audio_driver_list
)
3223 load_per_output_config (mpctx
->mconfig
, PROFILE_CFG_AO
, opts
->audio_driver_list
[0]);
3225 // We must enable getch2 here to be able to interrupt network connection
3227 if(!noconsolecontrols
&& !slave_mode
){
3228 if(mpctx
->initialized_flags
&INITIALIZED_GETCH2
)
3229 mp_msg(MSGT_CPLAYER
,MSGL_WARN
,MSGTR_Getch2InitializedTwice
);
3231 getch2_enable(); // prepare stdin for hotkeys...
3232 mpctx
->initialized_flags
|=INITIALIZED_GETCH2
;
3233 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n[[[init getch2]]]\n");
3236 // =================== GUI idle loop (STOP state) ===========================
3239 mpctx
->file_format
=DEMUXER_TYPE_UNKNOWN
;
3240 guiGetEvent( guiSetDefaults
,0 );
3241 while ( guiIntfStruct
.Playing
!= 1 )
3246 guiGetEvent( guiReDraw
,NULL
);
3247 if ( (cmd
= mp_input_get_cmd(mpctx
->input
, 0,0,0)) != NULL
) {
3248 guiGetEvent(guiIEvent
, (char *)cmd
->id
);
3252 guiGetEvent( guiSetParameters
,NULL
);
3253 if ( guiIntfStruct
.StreamType
== STREAMTYPE_STREAM
)
3255 play_tree_t
* entry
= play_tree_new();
3256 play_tree_add_file( entry
,guiIntfStruct
.Filename
);
3257 if ( mpctx
->playtree
) play_tree_free_list( mpctx
->playtree
->child
,1 );
3258 else mpctx
->playtree
=play_tree_new();
3259 play_tree_set_child( mpctx
->playtree
,entry
);
3262 mpctx
->playtree_iter
= play_tree_iter_new(mpctx
->playtree
,mpctx
->mconfig
);
3263 if(mpctx
->playtree_iter
)
3265 if(play_tree_iter_step(mpctx
->playtree_iter
,0,0) != PLAY_TREE_ITER_ENTRY
)
3267 play_tree_iter_free(mpctx
->playtree_iter
);
3268 mpctx
->playtree_iter
= NULL
;
3270 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
,1);
3275 #endif /* CONFIG_GUI */
3277 while (player_idle_mode
&& !mpctx
->filename
) {
3278 play_tree_t
* entry
= NULL
;
3280 while (!(cmd
= mp_input_get_cmd(mpctx
->input
, 0,1,0))) { // wait for command
3281 if (mpctx
->video_out
)
3282 vo_check_events(mpctx
->video_out
);
3286 case MP_CMD_LOADFILE
:
3287 // prepare a tree entry with the new filename
3288 entry
= play_tree_new();
3289 play_tree_add_file(entry
, cmd
->args
[0].v
.s
);
3290 // The entry is added to the main playtree after the switch().
3292 case MP_CMD_LOADLIST
:
3293 entry
= parse_playlist_file(mpctx
->mconfig
, cmd
->args
[0].v
.s
);
3296 exit_player_with_rc(mpctx
, EXIT_QUIT
, (cmd
->nargs
> 0)? cmd
->args
[0].v
.i
: 0);
3298 case MP_CMD_GET_PROPERTY
:
3299 case MP_CMD_SET_PROPERTY
:
3300 case MP_CMD_STEP_PROPERTY
:
3301 run_command(mpctx
, cmd
);
3307 if (entry
) { // user entered a command that gave a valid entry
3308 if (mpctx
->playtree
) // the playtree is always a node with one child. let's clear it
3309 play_tree_free_list(mpctx
->playtree
->child
, 1);
3310 else mpctx
->playtree
=play_tree_new(); // .. or make a brand new playtree
3312 if (!mpctx
->playtree
) continue; // couldn't make playtree! wait for next command
3314 play_tree_set_child(mpctx
->playtree
, entry
);
3316 /* Make iterator start at the top the of tree. */
3317 mpctx
->playtree_iter
= play_tree_iter_new(mpctx
->playtree
, mpctx
->mconfig
);
3318 if (!mpctx
->playtree_iter
) continue;
3320 // find the first real item in the tree
3321 if (play_tree_iter_step(mpctx
->playtree_iter
,0,0) != PLAY_TREE_ITER_ENTRY
) {
3323 play_tree_iter_free(mpctx
->playtree_iter
);
3324 mpctx
->playtree_iter
= NULL
;
3325 continue; // wait for next command
3327 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
, 1);
3330 //---------------------------------------------------------------------------
3333 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_Playing
,
3334 filename_recode(mpctx
->filename
));
3337 if (edl_records
) free_edl(edl_records
);
3338 next_edl_record
= edl_records
= edl_parse_file();
3340 if (edl_output_filename
) {
3341 if (edl_fd
) fclose(edl_fd
);
3342 if ((edl_fd
= fopen(edl_output_filename
, "w")) == NULL
)
3344 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, MSGTR_EdlCantOpenForWrite
,
3345 filename_recode(edl_output_filename
));
3349 //==================== Open VOB-Sub ============================
3351 current_module
="vobsub";
3353 vo_vobsub
=vobsub_open(vobsub_name
,spudec_ifo
,1,&vo_spudec
);
3355 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_CantLoadSub
,
3356 filename_recode(vobsub_name
));
3357 } else if (sub_auto
&& mpctx
->filename
){
3358 /* try to autodetect vobsub from movie filename ::atmos */
3359 char *buf
= strdup(mpctx
->filename
), *psub
;
3360 char *pdot
= strrchr(buf
, '.');
3361 char *pslash
= strrchr(buf
, '/');
3362 #if defined(__MINGW32__) || defined(__CYGWIN__)
3363 if (!pslash
) pslash
= strrchr(buf
, '\\');
3365 if (pdot
&& (!pslash
|| pdot
> pslash
))
3367 vo_vobsub
=vobsub_open(buf
,spudec_ifo
,0,&vo_spudec
);
3368 /* try from ~/.mplayer/sub */
3369 if(!vo_vobsub
&& (psub
= get_path( "sub/" ))) {
3372 bname
= strrchr(buf
,'/');
3373 #if defined(__MINGW32__) || defined(__CYGWIN__)
3374 if(!bname
) bname
= strrchr(buf
,'\\');
3378 l
= strlen(psub
) + strlen(bname
) + 1;
3379 psub
= realloc(psub
,l
);
3381 vo_vobsub
=vobsub_open(psub
,spudec_ifo
,0,&vo_spudec
);
3387 mpctx
->initialized_flags
|=INITIALIZED_VOBSUB
;
3388 vobsub_set_from_lang(vo_vobsub
, dvdsub_lang
);
3390 // setup global sub numbering
3391 mpctx
->global_sub_indices
[SUB_SOURCE_VOBSUB
] = mpctx
->global_sub_size
; // the global # of the first vobsub.
3392 mpctx
->global_sub_size
+= vobsub_get_indexes_count(vo_vobsub
);
3395 //============ Open & Sync STREAM --- fork cache2 ====================
3398 mpctx
->demuxer
=NULL
;
3399 if (mpctx
->d_audio
) {
3400 //free_demuxer_stream(mpctx->d_audio);
3401 mpctx
->d_audio
=NULL
;
3403 if (mpctx
->d_video
) {
3404 //free_demuxer_stream(d_video);
3405 mpctx
->d_video
=NULL
;
3407 mpctx
->sh_audio
=NULL
;
3408 mpctx
->sh_video
=NULL
;
3410 current_module
="open_stream";
3411 mpctx
->stream
= open_stream(mpctx
->filename
, opts
, &mpctx
->file_format
);
3412 if(!mpctx
->stream
) { // error...
3413 mpctx
->stop_play
= libmpdemux_was_interrupted(mpctx
, PT_NEXT_ENTRY
);
3414 goto goto_next_file
;
3416 mpctx
->initialized_flags
|=INITIALIZED_STREAM
;
3419 if ( use_gui
) guiGetEvent( guiSetStream
,(char *)mpctx
->stream
);
3422 if(mpctx
->file_format
== DEMUXER_TYPE_PLAYLIST
) {
3423 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "\nThis looks like a playlist, but "
3424 "playlist support will not be used automatically.\n"
3425 "MPlayer's playlist code is unsafe and should only be used with "
3426 "trusted sources.\nPlayback will probably fail.\n\n");
3430 current_module
="handle_playlist";
3431 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Parsing playlist %s...\n",
3432 filename_recode(mpctx
->filename
));
3433 entry
= parse_playtree(mpctx
->stream
, mpctx
->mconfig
, 0);
3434 mpctx
->eof
=playtree_add_playlist(mpctx
, entry
);
3435 goto goto_next_file
;
3438 mpctx
->stream
->start_pos
+=seek_to_byte
;
3440 if(stream_dump_type
==5){
3441 unsigned char buf
[4096];
3444 current_module
="dumpstream";
3445 if(mpctx
->stream
->type
==STREAMTYPE_STREAM
&& mpctx
->stream
->fd
<0){
3446 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_DumpstreamFdUnavailable
);
3447 exit_player(mpctx
, EXIT_ERROR
);
3449 stream_reset(mpctx
->stream
);
3450 stream_seek(mpctx
->stream
,mpctx
->stream
->start_pos
);
3451 f
=fopen(stream_dump_name
,"wb");
3453 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_CantOpenDumpfile
);
3454 exit_player(mpctx
, EXIT_ERROR
);
3456 if (dvd_chapter
> 1) {
3457 int chapter
= dvd_chapter
- 1;
3458 stream_control(mpctx
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
, &chapter
);
3460 while(!mpctx
->stream
->eof
&& !async_quit_request
){
3461 len
=stream_read(mpctx
->stream
,buf
,4096);
3463 if(fwrite(buf
,len
,1,f
) != 1) {
3464 mp_msg(MSGT_MENCODER
,MSGL_FATAL
,MSGTR_ErrorWritingFile
,stream_dump_name
);
3465 exit_player(mpctx
, EXIT_ERROR
);
3468 if(dvd_last_chapter
> 0) {
3470 if (stream_control(mpctx
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
3471 &chapter
) == STREAM_OK
&& chapter
+ 1 > dvd_last_chapter
)
3476 mp_msg(MSGT_MENCODER
,MSGL_FATAL
,MSGTR_ErrorWritingFile
,stream_dump_name
);
3477 exit_player(mpctx
, EXIT_ERROR
);
3479 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_CoreDumped
);
3480 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
3483 #ifdef CONFIG_DVDREAD
3484 if(mpctx
->stream
->type
==STREAMTYPE_DVD
){
3485 current_module
="dvd lang->id";
3486 if(opts
->audio_id
==-1) opts
->audio_id
=dvd_aid_from_lang(mpctx
->stream
,audio_lang
);
3487 if(dvdsub_lang
&& opts
->sub_id
==-2) opts
->sub_id
=-1;
3488 if(dvdsub_lang
&& opts
->sub_id
==-1) opts
->sub_id
=dvd_sid_from_lang(mpctx
->stream
,dvdsub_lang
);
3489 // setup global sub numbering
3490 mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] = mpctx
->global_sub_size
; // the global # of the first demux-specific sub.
3491 mpctx
->global_sub_size
+= dvd_number_of_subs(mpctx
->stream
);
3492 current_module
=NULL
;
3496 #ifdef CONFIG_DVDNAV
3497 if(mpctx
->stream
->type
==STREAMTYPE_DVDNAV
){
3498 current_module
="dvdnav lang->id";
3499 if(opts
->audio_id
==-1) opts
->audio_id
=mp_dvdnav_aid_from_lang(mpctx
->stream
,audio_lang
);
3500 if(dvdsub_lang
&& opts
->sub_id
==-2) opts
->sub_id
=-1;
3501 if(dvdsub_lang
&& opts
->sub_id
==-1) opts
->sub_id
=mp_dvdnav_sid_from_lang(mpctx
->stream
,dvdsub_lang
);
3502 // setup global sub numbering
3503 mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] = mpctx
->global_sub_size
; // the global # of the first demux-specific sub.
3504 mpctx
->global_sub_size
+= mp_dvdnav_number_of_subs(mpctx
->stream
);
3505 current_module
=NULL
;
3509 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3511 if(stream_cache_size
>0){
3512 current_module
="enable_cache";
3513 if(!stream_enable_cache(mpctx
->stream
,stream_cache_size
*1024,
3514 stream_cache_size
*1024*(stream_cache_min_percent
/ 100.0),
3515 stream_cache_size
*1024*(stream_cache_seek_min_percent
/ 100.0)))
3516 if((mpctx
->stop_play
= libmpdemux_was_interrupted(mpctx
, PT_NEXT_ENTRY
))) goto goto_next_file
;
3519 //============ Open DEMUXERS --- DETECT file type =======================
3520 current_module
="demux_open";
3522 mpctx
->demuxer
=demux_open(opts
, mpctx
->stream
,mpctx
->file_format
,opts
->audio_id
,opts
->video_id
,opts
->sub_id
,mpctx
->filename
);
3524 // HACK to get MOV Reference Files working
3526 if (mpctx
->demuxer
&& mpctx
->demuxer
->type
==DEMUXER_TYPE_PLAYLIST
)
3528 unsigned char* playlist_entry
;
3529 play_tree_t
*list
= NULL
, *entry
= NULL
;
3531 current_module
="handle_demux_playlist";
3532 while (ds_get_packet(mpctx
->demuxer
->video
,&playlist_entry
)>0)
3536 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Adding file %s to element entry.\n",
3537 filename_recode(playlist_entry
));
3539 bname
=mp_basename(playlist_entry
);
3540 if ((strlen(bname
)>10) && !strncmp(bname
,"qt",2) && !strncmp(bname
+3,"gateQT",6))
3543 if (!strncmp(bname
,mp_basename(mpctx
->filename
),strlen(bname
))) // ignoring self-reference
3546 entry
= play_tree_new();
3548 if (mpctx
->filename
&& !strcmp(mp_basename(playlist_entry
),playlist_entry
)) // add reference path of current file
3550 temp
=malloc((strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
))+strlen(playlist_entry
)+1));
3553 strncpy(temp
, mpctx
->filename
, strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
)));
3554 temp
[strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
))]='\0';
3555 strcat(temp
, playlist_entry
);
3556 play_tree_add_file(entry
,temp
);
3557 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Resolving reference to %s.\n",temp
);
3562 play_tree_add_file(entry
,playlist_entry
);
3567 play_tree_append_entry(list
,entry
);
3569 free_demuxer(mpctx
->demuxer
);
3570 mpctx
->demuxer
= NULL
;
3574 entry
= play_tree_new();
3575 play_tree_set_child(entry
,list
);
3576 mpctx
->stop_play
= playtree_add_playlist(mpctx
, entry
);
3577 goto goto_next_file
;
3582 goto goto_next_file
;
3584 if (mpctx
->demuxer
->matroska_data
.ordered_chapters
)
3585 build_ordered_chapter_timeline(mpctx
);
3587 if (!mpctx
->sources
) {
3588 mpctx
->sources
= talloc_ptrtype(NULL
, mpctx
->sources
);
3589 *mpctx
->sources
= (struct content_source
){.stream
= mpctx
->stream
,
3590 .demuxer
= mpctx
->demuxer
};
3591 mpctx
->num_sources
= 1;
3596 if (demuxer_seek_chapter(mpctx
->demuxer
, dvd_chapter
-1, 1, &pts
, NULL
, NULL
) >= 0 && pts
> -1.0)
3597 seek(mpctx
, pts
, SEEK_ABSOLUTE
);
3600 mpctx
->initialized_flags
|=INITIALIZED_DEMUXER
;
3602 if (mpctx
->stream
->type
!= STREAMTYPE_DVD
&& mpctx
->stream
->type
!= STREAMTYPE_DVDNAV
) {
3605 // setup global sub numbering
3606 mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] = mpctx
->global_sub_size
; // the global # of the first demux-specific sub.
3607 for (i
= 0; i
< MAX_S_STREAMS
; i
++)
3608 if (mpctx
->demuxer
->s_streams
[i
])
3609 maxid
= FFMAX(maxid
, mpctx
->demuxer
->s_streams
[i
]->sid
);
3610 mpctx
->global_sub_size
+= maxid
+ 1;
3612 // Make opts->sub_id always selectable if set.
3613 if (mpctx
->global_sub_size
<= mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] + opts
->sub_id
)
3614 mpctx
->global_sub_size
= mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] + opts
->sub_id
+ 1;
3617 if (ass_enabled
&& ass_library
) {
3618 for (int j
= 0; j
< mpctx
->num_sources
; j
++) {
3619 struct demuxer
*d
= mpctx
->sources
[j
].demuxer
;
3620 for (int i
= 0; i
< d
->num_attachments
; i
++) {
3621 struct demux_attachment
*att
= d
->attachments
+ i
;
3622 if (extract_embedded_fonts
3623 && att
->name
&& att
->type
&& att
->data
&& att
->data_size
3624 && (strcmp(att
->type
, "application/x-truetype-font") == 0
3625 || strcmp(att
->type
, "application/x-font") == 0))
3626 ass_add_font(ass_library
, att
->name
, att
->data
, att
->data_size
);
3632 current_module
="demux_open2";
3634 //file_format=demuxer->file_format;
3636 mpctx
->d_audio
=mpctx
->demuxer
->audio
;
3637 mpctx
->d_video
=mpctx
->demuxer
->video
;
3638 mpctx
->d_sub
=mpctx
->demuxer
->sub
;
3640 // select audio stream
3641 select_audio(mpctx
->demuxer
, opts
->audio_id
, audio_lang
);
3644 if((stream_dump_type
)&&(stream_dump_type
<4)){
3646 demux_stream_t
*ds
=NULL
;
3647 current_module
="dump";
3648 // select stream to dump
3649 switch(stream_dump_type
){
3650 case 1: ds
=mpctx
->d_audio
;break;
3651 case 2: ds
=mpctx
->d_video
;break;
3652 case 3: ds
=mpctx
->d_sub
;break;
3655 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_DumpSelectedStreamMissing
);
3656 exit_player(mpctx
, EXIT_ERROR
);
3658 // disable other streams:
3659 if(mpctx
->d_audio
&& mpctx
->d_audio
!=ds
) {ds_free_packs(mpctx
->d_audio
); mpctx
->d_audio
->id
=-2; }
3660 if(mpctx
->d_video
&& mpctx
->d_video
!=ds
) {ds_free_packs(mpctx
->d_video
); mpctx
->d_video
->id
=-2; }
3661 if(mpctx
->d_sub
&& mpctx
->d_sub
!=ds
) {ds_free_packs(mpctx
->d_sub
); mpctx
->d_sub
->id
=-2; }
3663 f
=fopen(stream_dump_name
,"wb");
3665 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_CantOpenDumpfile
);
3666 exit_player(mpctx
, EXIT_ERROR
);
3669 unsigned char* start
;
3670 int in_size
=ds_get_packet(ds
,&start
);
3671 if( (mpctx
->demuxer
->file_format
==DEMUXER_TYPE_AVI
|| mpctx
->demuxer
->file_format
==DEMUXER_TYPE_ASF
|| mpctx
->demuxer
->file_format
==DEMUXER_TYPE_MOV
)
3672 && stream_dump_type
==2) fwrite(&in_size
,1,4,f
);
3673 if(in_size
>0) fwrite(start
,in_size
,1,f
);
3674 if(dvd_last_chapter
>0) {
3675 int cur_chapter
= demuxer_get_current_chapter(mpctx
->demuxer
);
3676 if(cur_chapter
!=-1 && cur_chapter
+1>dvd_last_chapter
)
3681 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_CoreDumped
);
3682 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
3685 mpctx
->sh_audio
=mpctx
->d_audio
->sh
;
3686 mpctx
->sh_video
=mpctx
->d_video
->sh
;
3688 if(mpctx
->sh_video
){
3690 current_module
="video_read_properties";
3691 if(!video_read_properties(mpctx
->sh_video
)) {
3692 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_CannotReadVideoProperties
);
3693 mpctx
->sh_video
=mpctx
->d_video
->sh
=NULL
;
3695 mp_msg(MSGT_CPLAYER
,MSGL_V
,MSGTR_FilefmtFourccSizeFpsFtime
,
3696 mpctx
->demuxer
->file_format
,mpctx
->sh_video
->format
, mpctx
->sh_video
->disp_w
,mpctx
->sh_video
->disp_h
,
3697 mpctx
->sh_video
->fps
,mpctx
->sh_video
->frametime
3700 /* need to set fps here for output encoders to pick it up in their init */
3702 mpctx
->sh_video
->fps
=force_fps
;
3703 mpctx
->sh_video
->frametime
=1.0f
/mpctx
->sh_video
->fps
;
3705 vo_fps
= mpctx
->sh_video
->fps
;
3707 if(!mpctx
->sh_video
->fps
&& !force_fps
){
3708 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_FPSnotspecified
);
3709 mpctx
->sh_video
=mpctx
->d_video
->sh
=NULL
;
3715 if(!mpctx
->sh_video
&& !mpctx
->sh_audio
){
3716 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
, MSGTR_NoStreamFound
);
3718 if(mpctx
->stream
->type
== STREAMTYPE_DVB
)
3721 int v
= mpctx
->last_dvb_step
;
3723 dir
= DVB_CHANNEL_HIGHER
;
3725 dir
= DVB_CHANNEL_LOWER
;
3727 if(dvb_step_channel(mpctx
->stream
, dir
)) {
3728 mpctx
->stop_play
= PT_NEXT_ENTRY
;
3729 mpctx
->dvbin_reopen
= 1;
3733 goto goto_next_file
; // exit_player(MSGTR_Exit_error);
3736 /* display clip info */
3737 demux_info_print(mpctx
->demuxer
);
3739 //================== Read SUBTITLES (DVD & TEXT) ==========================
3740 if(vo_spudec
==NULL
&& mpctx
->sh_video
&&
3741 (mpctx
->stream
->type
==STREAMTYPE_DVD
|| mpctx
->stream
->type
== STREAMTYPE_DVDNAV
)){
3742 init_vo_spudec(mpctx
);
3745 if(mpctx
->sh_video
) {
3746 // after reading video params we should load subtitles because
3747 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3749 current_module
="read_subtitles_file";
3751 for (i
= 0; sub_name
[i
] != NULL
; ++i
)
3752 add_subtitles(mpctx
, sub_name
[i
], mpctx
->sh_video
->fps
, 0);
3754 if(sub_auto
) { // auto load sub file ...
3755 char *psub
= get_path( "sub/" );
3756 char **tmp
= sub_filenames((psub
? psub
: ""), mpctx
->filename
);
3758 free(psub
); // release the buffer created by get_path() above
3760 add_subtitles(mpctx
, tmp
[i
], mpctx
->sh_video
->fps
, 1);
3765 if (mpctx
->set_of_sub_size
> 0) {
3766 // setup global sub numbering
3767 mpctx
->global_sub_indices
[SUB_SOURCE_SUBS
] = mpctx
->global_sub_size
; // the global # of the first sub.
3768 mpctx
->global_sub_size
+= mpctx
->set_of_sub_size
;
3772 if (mpctx
->global_sub_size
) {
3773 // find the best sub to use
3774 int vobsub_index_id
= vobsub_get_index_by_id(vo_vobsub
, vobsub_id
);
3775 mpctx
->global_sub_pos
= -1; // no subs by default
3776 if (vobsub_index_id
>= 0) {
3777 // if user asks for a vobsub id, use that first.
3778 mpctx
->global_sub_pos
= mpctx
->global_sub_indices
[SUB_SOURCE_VOBSUB
] + vobsub_index_id
;
3779 } else if (opts
->sub_id
>= 0 && mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] >= 0) {
3780 // if user asks for a dvd sub id, use that next.
3781 mpctx
->global_sub_pos
= mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] + opts
->sub_id
;
3782 } else if (mpctx
->global_sub_indices
[SUB_SOURCE_SUBS
] >= 0) {
3783 // if there are text subs to use, use those. (autosubs come last here)
3784 mpctx
->global_sub_pos
= mpctx
->global_sub_indices
[SUB_SOURCE_SUBS
];
3785 } else if (opts
->sub_id
< 0 && mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] >= 0) {
3786 // finally select subs by language and container hints
3787 if (opts
->sub_id
< 0 && dvdsub_lang
)
3788 opts
->sub_id
= demuxer_sub_track_by_lang(mpctx
->demuxer
, dvdsub_lang
);
3789 if (opts
->sub_id
< 0)
3790 opts
->sub_id
= demuxer_default_sub_track(mpctx
->demuxer
);
3791 if (opts
->sub_id
>= 0)
3792 mpctx
->global_sub_pos
= mpctx
->global_sub_indices
[SUB_SOURCE_DEMUX
] + opts
->sub_id
;
3794 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3795 mpctx
->global_sub_pos
--;
3796 mp_property_do("sub",M_PROPERTY_STEP_UP
,NULL
, mpctx
);
3798 switch (stream_dump_type
) {
3799 case 3: list_sub_file(subdata
); break;
3800 case 4: dump_mpsub(subdata
, mpctx
->sh_video
->fps
); break;
3801 case 6: dump_srt(subdata
, mpctx
->sh_video
->fps
); break;
3802 case 7: dump_microdvd(subdata
, mpctx
->sh_video
->fps
); break;
3803 case 8: dump_jacosub(subdata
, mpctx
->sh_video
->fps
); break;
3804 case 9: dump_sami(subdata
, mpctx
->sh_video
->fps
); break;
3808 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_FILENAME=%s\n",
3809 filename_recode(mpctx
->filename
));
3810 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_DEMUXER=%s\n", mpctx
->demuxer
->desc
->name
);
3811 if (mpctx
->sh_video
) {
3812 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3813 if (mpctx
->sh_video
->format
>= 0x20202020)
3814 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx
->sh_video
->format
);
3816 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FORMAT=0x%08X\n", mpctx
->sh_video
->format
);
3817 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_BITRATE=%d\n", mpctx
->sh_video
->i_bps
*8);
3818 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_WIDTH=%d\n", mpctx
->sh_video
->disp_w
);
3819 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_HEIGHT=%d\n", mpctx
->sh_video
->disp_h
);
3820 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FPS=%5.3f\n", mpctx
->sh_video
->fps
);
3821 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_ASPECT=%1.4f\n", mpctx
->sh_video
->aspect
);
3823 if (mpctx
->sh_audio
) {
3824 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3825 if (mpctx
->sh_audio
->format
>= 0x20202020)
3826 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx
->sh_audio
->format
);
3828 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_FORMAT=%d\n", mpctx
->sh_audio
->format
);
3829 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_BITRATE=%d\n", mpctx
->sh_audio
->i_bps
*8);
3830 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_RATE=%d\n", mpctx
->sh_audio
->samplerate
);
3831 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_NCH=%d\n", mpctx
->sh_audio
->channels
);
3833 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx
->demuxer
));
3834 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_SEEKABLE=%d\n", mpctx
->stream
->seek
? 1 : 0);
3835 if (mpctx
->demuxer
) {
3836 if (mpctx
->demuxer
->num_chapters
== 0)
3837 stream_control(mpctx
->demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
, &mpctx
->demuxer
->num_chapters
);
3838 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_CHAPTERS=%d\n", mpctx
->demuxer
->num_chapters
);
3841 if(!mpctx
->sh_video
) goto main
; // audio-only
3843 if(!reinit_video_chain(mpctx
)) {
3844 if(!mpctx
->sh_video
){
3845 if(!mpctx
->sh_audio
) goto goto_next_file
;
3846 goto main
; // exit_player(MSGTR_Exit_error);
3850 if(mpctx
->sh_video
->output_flags
& 0x08 && vo_spudec
)
3851 spudec_set_hw_spu(vo_spudec
,mpctx
->video_out
);
3853 #ifdef CONFIG_FREETYPE
3854 force_load_font
= 1;
3857 //================== MAIN: ==========================
3859 current_module
="main";
3862 char* msg
= property_expand_string(mpctx
, playing_msg
);
3863 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"%s",msg
);
3868 // Disable the term OSD in verbose mode
3869 if(verbose
) term_osd
= 0;
3872 //int frame_corr_num=0; //
3873 //float v_frame=0; // Video
3874 //float num_frames=0; // number of frames played
3876 int frame_time_remaining
=0; // flag
3878 mpctx
->num_buffered_frames
=0;
3880 // Make sure old OSD does not stay around,
3881 // e.g. with -fixed-vo and same-resolution files
3883 update_osd_msg(mpctx
);
3885 //================ SETUP AUDIO ==========================
3887 if(mpctx
->sh_audio
){
3888 reinit_audio_chain(mpctx
);
3889 if (mpctx
->sh_audio
&& mpctx
->sh_audio
->codec
)
3890 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
, "ID_AUDIO_CODEC=%s\n", mpctx
->sh_audio
->codec
->name
);
3893 current_module
="av_init";
3895 if(mpctx
->sh_video
){
3896 mpctx
->sh_video
->timer
=0;
3898 audio_delay
+= mpctx
->sh_video
->stream_delay
;
3900 if(mpctx
->sh_audio
){
3901 if (start_volume
>= 0)
3902 mixer_setvolume(&mpctx
->mixer
, start_volume
, start_volume
);
3904 audio_delay
-= mpctx
->sh_audio
->stream_delay
;
3905 mpctx
->delay
=-audio_delay
;
3908 if(!mpctx
->sh_audio
){
3909 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_NoSound
);
3910 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Freeing %d unused audio chunks.\n",mpctx
->d_audio
->packs
);
3911 ds_free_packs(mpctx
->d_audio
); // free buffered chunks
3912 //mpctx->d_audio->id=-2; // do not read audio chunks
3913 //uninit_player(mpctx, INITIALIZED_AO); // close device
3915 if(!mpctx
->sh_video
){
3916 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_Video_NoVideo
);
3917 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Freeing %d unused video chunks.\n",mpctx
->d_video
->packs
);
3918 ds_free_packs(mpctx
->d_video
);
3919 mpctx
->d_video
->id
=-2;
3920 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3923 if (!mpctx
->sh_video
&& !mpctx
->sh_audio
)
3924 goto goto_next_file
;
3926 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3927 if(force_fps
&& mpctx
->sh_video
){
3928 vo_fps
= mpctx
->sh_video
->fps
=force_fps
;
3929 mpctx
->sh_video
->frametime
=1.0f
/mpctx
->sh_video
->fps
;
3930 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_FPSforced
,mpctx
->sh_video
->fps
,mpctx
->sh_video
->frametime
);
3935 if ( mpctx
->sh_audio
) guiIntfStruct
.AudioType
=mpctx
->sh_audio
->channels
; else guiIntfStruct
.AudioType
=0;
3936 if ( !mpctx
->sh_video
&& mpctx
->sh_audio
) guiGetEvent( guiSetAudioOnly
,(char *)1 ); else guiGetEvent( guiSetAudioOnly
,(char *)0 );
3937 guiGetEvent( guiSetFileFormat
,(char *)mpctx
->demuxer
->file_format
);
3938 if ( guiGetEvent( guiSetValues
,(char *)mpctx
->sh_video
) ) goto goto_next_file
;
3939 guiGetEvent( guiSetDemuxer
,(char *)mpctx
->demuxer
);
3943 mp_input_set_section(mpctx
->input
, NULL
);
3944 //TODO: add desired (stream-based) sections here
3945 if (mpctx
->stream
->type
==STREAMTYPE_TV
) mp_input_set_section(mpctx
->input
, "tv");
3946 if (mpctx
->stream
->type
==STREAMTYPE_DVDNAV
) mp_input_set_section(mpctx
->input
, "dvdnav");
3948 //==================== START PLAYING =======================
3950 if(opts
->loop_times
>1) opts
->loop_times
--; else
3951 if(opts
->loop_times
==1) opts
->loop_times
= -1;
3953 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,MSGTR_StartPlaying
);
3955 total_time_usage_start
=GetTimer();
3956 audio_time_usage
=0; video_time_usage
=0; vout_time_usage
=0;
3957 total_frame_cnt
=0; drop_frame_cnt
=0; // fix for multifile fps benchmark
3958 play_n_frames
=play_n_frames_mf
;
3960 if(play_n_frames
==0){
3961 mpctx
->stop_play
=PT_NEXT_ENTRY
; goto goto_next_file
;
3965 seek(mpctx
, seek_to_sec
, SEEK_ABSOLUTE
);
3966 end_at
.pos
+= seek_to_sec
;
3969 if (end_at
.type
== END_AT_SIZE
) {
3970 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, MSGTR_MPEndposNoSizeBased
);
3971 end_at
.type
= END_AT_NONE
;
3974 #ifdef CONFIG_DVDNAV
3975 mp_dvdnav_context_free(mpctx
);
3976 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
3977 mp_dvdnav_read_wait(mpctx
->stream
, 0, 1);
3978 mp_dvdnav_cell_has_changed(mpctx
->stream
,1);
3982 get_relative_time(mpctx
); // reset current delta
3983 mpctx
->time_frame
= 0;
3984 mpctx
->drop_message_shown
= 0;
3985 mpctx
->update_video_immediately
= true;
3986 mpctx
->total_avsync_change
= 0;
3987 // Make sure VO knows current pause state
3988 if (mpctx
->sh_video
)
3989 vo_control(mpctx
->video_out
, mpctx
->paused
? VOCTRL_PAUSE
: VOCTRL_RESUME
,
3992 while(!mpctx
->stop_play
){
3993 float aq_sleep_time
=0;
3995 if(dvd_last_chapter
>0) {
3996 int cur_chapter
= demuxer_get_current_chapter(mpctx
->demuxer
);
3997 if(cur_chapter
!=-1 && cur_chapter
+1>dvd_last_chapter
)
3998 goto goto_next_file
;
4001 if(!mpctx
->sh_audio
&& mpctx
->d_audio
->sh
) {
4002 mpctx
->sh_audio
= mpctx
->d_audio
->sh
;
4003 mpctx
->sh_audio
->ds
= mpctx
->d_audio
;
4004 reinit_audio_chain(mpctx
);
4007 /*========================== PLAY AUDIO ============================*/
4009 if (mpctx
->sh_audio
&& !mpctx
->paused
)
4010 if (!fill_audio_out_buffers(mpctx
))
4011 // at eof, all audio at least written to ao
4012 if (!mpctx
->sh_video
)
4013 mpctx
->stop_play
= AT_END_OF_FILE
;
4016 if(!mpctx
->sh_video
) {
4017 // handle audio-only case:
4019 // sh_audio can be NULL due to video stream switching
4020 // TODO: handle this better
4021 if (mpctx
->sh_audio
)
4022 a_pos
= playing_audio_pts(mpctx
);
4024 print_status(mpctx
, a_pos
, false);
4026 if(end_at
.type
== END_AT_TIME
&& end_at
.pos
< a_pos
)
4027 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4028 update_osd_msg(mpctx
);
4032 /*========================== PLAY VIDEO ============================*/
4034 vo_pts
=mpctx
->sh_video
->timer
*90000.0;
4035 vo_fps
=mpctx
->sh_video
->fps
;
4037 if (!mpctx
->num_buffered_frames
) {
4038 double frame_time
= update_video(mpctx
, &blit_frame
);
4039 mp_dbg(MSGT_AVSYNC
,MSGL_DBG2
,"*** ftime=%5.3f ***\n",frame_time
);
4040 if (mpctx
->sh_video
->vf_initialized
< 0) {
4041 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
, MSGTR_NotInitializeVOPorVO
);
4042 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4043 goto goto_next_file
;
4046 struct vf_instance
*vf
= mpctx
->sh_video
->vfilter
;
4047 vf
->control(vf
, VFCTRL_DRAW_EOSD
, NULL
);
4048 vf
->control(vf
, VFCTRL_DRAW_OSD
, mpctx
->osd
);
4052 mpctx
->stop_play
= AT_END_OF_FILE
;
4054 // might return with !eof && !blit_frame if !correct_pts
4055 mpctx
->num_buffered_frames
+= blit_frame
;
4056 if (mpctx
->update_video_immediately
) {
4057 // Show this frame immediately, rest normally
4058 mpctx
->update_video_immediately
= false;
4060 mpctx
->time_frame
+= frame_time
/ opts
->playback_speed
;
4061 adjust_sync(mpctx
, frame_time
);
4065 if (mpctx
->timeline
) {
4066 struct timeline_part
*next
= mpctx
->timeline
+ mpctx
->timeline_part
+ 1;
4067 if (mpctx
->sh_video
->pts
>= next
->start
4068 || mpctx
->stop_play
== AT_END_OF_FILE
4069 && mpctx
->timeline_part
+ 1 < mpctx
->num_timeline_parts
) {
4070 seek(mpctx
, next
->start
, SEEK_ABSOLUTE
);
4075 // ==========================================================================
4077 // current_module="draw_osd";
4078 // if(vo_config_count) mpctx->video_out->draw_osd();
4081 if(use_gui
) guiEventHandling();
4084 current_module
="vo_check_events";
4085 vo_check_events(mpctx
->video_out
);
4088 if (stop_xscreensaver
) {
4089 current_module
= "stop_xscreensaver";
4090 xscreensaver_heartbeat(mpctx
->x11_state
);
4093 if (heartbeat_cmd
) {
4094 static unsigned last_heartbeat
;
4095 unsigned now
= GetTimerMS();
4096 if (now
- last_heartbeat
> 30000) {
4097 last_heartbeat
= now
;
4098 system(heartbeat_cmd
);
4102 frame_time_remaining
= sleep_until_update(mpctx
, &mpctx
->time_frame
, &aq_sleep_time
);
4104 //====================== FLIP PAGE (VIDEO BLT): =========================
4106 current_module
="flip_page";
4107 if (!frame_time_remaining
&& blit_frame
) {
4108 unsigned int t2
=GetTimer();
4110 vo_flip_page(mpctx
->video_out
);
4111 mpctx
->num_buffered_frames
--;
4113 mpctx
->last_vo_flip_duration
= (GetTimer() - t2
) * 0.000001;
4114 vout_time_usage
+= mpctx
->last_vo_flip_duration
;
4115 print_status(mpctx
, MP_NOPTS_VALUE
, true);
4118 print_status(mpctx
, MP_NOPTS_VALUE
, false);
4120 //============================ Auto QUALITY ============================
4122 /*Output quality adjustments:*/
4124 current_module
="autoq";
4125 // float total=0.000001f * (GetTimer()-aq_total_time);
4126 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4127 if(output_quality
<auto_quality
&& aq_sleep_time
>0)
4130 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4131 if(output_quality
>1 && aq_sleep_time
<0)
4134 if(output_quality
>0 && aq_sleep_time
<-0.050f
) // 50ms
4136 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4137 set_video_quality(mpctx
->sh_video
,output_quality
);
4140 if (!frame_time_remaining
&& blit_frame
) {
4141 if (play_n_frames
>= 0) {
4143 if (play_n_frames
<= 0)
4144 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4146 if (mpctx
->step_frames
> 0) {
4147 mpctx
->step_frames
--;
4148 if (mpctx
->step_frames
== 0)
4149 pause_player(mpctx
);
4154 // FIXME: add size based support for -endpos
4155 if (end_at
.type
== END_AT_TIME
&&
4156 !frame_time_remaining
&& end_at
.pos
<= mpctx
->sh_video
->pts
)
4157 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4159 } // end if(mpctx->sh_video)
4161 #ifdef CONFIG_DVDNAV
4162 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
4164 mp_dvdnav_get_highlight (mpctx
->stream
, &hl
);
4165 osd_set_nav_box (hl
.sx
, hl
.sy
, hl
.ex
, hl
.ey
);
4166 vo_osd_changed (OSDTYPE_DVDNAV
);
4168 if (mp_dvdnav_stream_has_changed(mpctx
->stream
)) {
4170 if (stream_control (mpctx
->demuxer
->stream
,
4171 STREAM_CTRL_GET_ASPECT_RATIO
, &ar
)
4172 != STREAM_UNSUPPORTED
)
4173 mpctx
->sh_video
->stream_aspect
= ar
;
4178 //================= Keyboard events, SEEKing ====================
4180 current_module
="key_events";
4185 while ((cmd
= mp_input_get_cmd(mpctx
->input
, 0,0,0)) != NULL
) {
4186 run_command(mpctx
, cmd
);
4188 if (mpctx
->stop_play
)
4191 if (!mpctx
->paused
|| mpctx
->stop_play
|| mpctx
->rel_seek_secs
4192 || mpctx
->abs_seek_pos
)
4194 if (mpctx
->sh_video
) {
4195 update_osd_msg(mpctx
);
4196 int hack
= vo_osd_changed(0);
4197 vo_osd_changed(hack
);
4199 if (redraw_osd(mpctx
->sh_video
, mpctx
->osd
) < 0) {
4200 add_step_frame(mpctx
);
4211 if (step_sec
> 0 && !mpctx
->paused
) {
4212 mpctx
->osd_function
=OSD_FFW
;
4213 mpctx
->rel_seek_secs
+=step_sec
;
4219 if(mpctx
->stop_play
==AT_END_OF_FILE
&& opts
->loop_times
>=0) {
4220 mp_msg(MSGT_CPLAYER
,MSGL_V
,"loop_times = %d\n", opts
->loop_times
);
4222 if(opts
->loop_times
>1) opts
->loop_times
--; else
4223 if(opts
->loop_times
==1) opts
->loop_times
=-1;
4224 play_n_frames
=play_n_frames_mf
;
4226 mpctx
->abs_seek_pos
=SEEK_ABSOLUTE
; mpctx
->rel_seek_secs
=seek_to_sec
;
4229 if(mpctx
->rel_seek_secs
|| mpctx
->abs_seek_pos
){
4230 seek(mpctx
, mpctx
->rel_seek_secs
, mpctx
->abs_seek_pos
);
4232 mpctx
->rel_seek_secs
=0;
4233 mpctx
->abs_seek_pos
=0;
4239 if(mpctx
->demuxer
->file_format
==DEMUXER_TYPE_AVI
&& mpctx
->sh_video
&& mpctx
->sh_video
->video
.dwLength
>2){
4240 // get pos from frame number / total frames
4241 guiIntfStruct
.Position
=(float)mpctx
->d_video
->pack_no
*100.0f
/mpctx
->sh_video
->video
.dwLength
;
4243 guiIntfStruct
.Position
=demuxer_get_percent_pos(mpctx
->demuxer
);
4245 if ( mpctx
->sh_video
) guiIntfStruct
.TimeSec
=mpctx
->sh_video
->pts
;
4246 else if ( mpctx
->sh_audio
) guiIntfStruct
.TimeSec
=playing_audio_pts(mpctx
);
4247 guiIntfStruct
.LengthInSec
=demuxer_get_time_length(mpctx
->demuxer
);
4248 guiGetEvent( guiReDraw
,NULL
);
4249 guiGetEvent( guiSetVolume
,NULL
);
4250 if(guiIntfStruct
.Playing
==0) break; // STOP
4251 if(guiIntfStruct
.Playing
==2) mpctx
->osd_function
=OSD_PAUSE
;
4252 if ( guiIntfStruct
.DiskChanged
|| guiIntfStruct
.NewPlay
) goto goto_next_file
;
4253 #ifdef CONFIG_DVDREAD
4254 if ( mpctx
->stream
->type
== STREAMTYPE_DVD
)
4256 dvd_priv_t
* dvdp
= mpctx
->stream
->priv
;
4257 guiIntfStruct
.DVD
.current_chapter
=dvd_chapter_from_cell(dvdp
,guiIntfStruct
.DVD
.current_title
-1, dvdp
->cur_cell
)+1;
4261 #endif /* CONFIG_GUI */
4263 } // while(!mpctx->stop_play)
4265 mp_msg(MSGT_GLOBAL
,MSGL_V
,"EOF code: %d \n",mpctx
->stop_play
);
4268 if(mpctx
->dvbin_reopen
)
4270 mpctx
->stop_play
= 0;
4271 uninit_player(mpctx
, INITIALIZED_ALL
-(INITIALIZED_GUI
|INITIALIZED_STREAM
|INITIALIZED_GETCH2
|(opts
->fixed_vo
?INITIALIZED_VO
:0)));
4272 cache_uninit(mpctx
->stream
);
4273 mpctx
->dvbin_reopen
= 0;
4274 goto goto_enable_cache
;
4279 goto_next_file
: // don't jump here after ao/vo/getch initialization!
4281 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"\n");
4284 double tot
=video_time_usage
+vout_time_usage
+audio_time_usage
;
4285 double total_time_usage
;
4286 total_time_usage_start
=GetTimer()-total_time_usage_start
;
4287 total_time_usage
= (float)total_time_usage_start
*0.000001;
4288 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4289 video_time_usage
,vout_time_usage
,audio_time_usage
,
4290 total_time_usage
-tot
,total_time_usage
);
4291 if(total_time_usage
>0.0)
4292 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4293 100.0*video_time_usage
/total_time_usage
,
4294 100.0*vout_time_usage
/total_time_usage
,
4295 100.0*audio_time_usage
/total_time_usage
,
4296 100.0*(total_time_usage
-tot
)/total_time_usage
,
4298 if(total_frame_cnt
&& frame_dropping
)
4299 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4300 total_frame_cnt
-drop_frame_cnt
,
4301 (total_time_usage
>0.5)?((total_frame_cnt
-drop_frame_cnt
)/total_time_usage
):0,
4303 100*drop_frame_cnt
/total_frame_cnt
,
4305 (total_time_usage
>0.5)?(total_frame_cnt
/total_time_usage
):0);
4308 // time to uninit all, except global stuff:
4309 uninit_player(mpctx
, INITIALIZED_ALL
-(INITIALIZED_GUI
+(opts
->fixed_vo
?INITIALIZED_VO
:0)));
4311 if(mpctx
->set_of_sub_size
> 0) {
4312 current_module
="sub_free";
4313 for(i
= 0; i
< mpctx
->set_of_sub_size
; ++i
) {
4314 sub_free(mpctx
->set_of_subtitles
[i
]);
4316 if(mpctx
->set_of_ass_tracks
[i
])
4317 ass_free_track( mpctx
->set_of_ass_tracks
[i
] );
4320 mpctx
->set_of_sub_size
= 0;
4322 vo_sub_last
= vo_sub
=NULL
;
4327 ass_clear_fonts(ass_library
);
4330 if (!mpctx
->stop_play
) // In case some goto jumped here...
4331 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4333 int playtree_direction
= 1;
4335 if(mpctx
->stop_play
== PT_NEXT_ENTRY
|| mpctx
->stop_play
== PT_PREV_ENTRY
) {
4336 if(play_tree_iter_step(mpctx
->playtree_iter
,mpctx
->play_tree_step
,0) != PLAY_TREE_ITER_ENTRY
) {
4337 play_tree_iter_free(mpctx
->playtree_iter
);
4338 mpctx
->playtree_iter
= NULL
;
4340 mpctx
->play_tree_step
= 1;
4341 } else if(mpctx
->stop_play
== PT_UP_NEXT
|| mpctx
->stop_play
== PT_UP_PREV
) {
4342 int direction
= mpctx
->stop_play
== PT_UP_NEXT
? 1 : -1;
4343 if(mpctx
->playtree_iter
) {
4344 if(play_tree_iter_up_step(mpctx
->playtree_iter
,direction
,0) != PLAY_TREE_ITER_ENTRY
) {
4345 play_tree_iter_free(mpctx
->playtree_iter
);
4346 mpctx
->playtree_iter
= NULL
;
4349 } else if (mpctx
->stop_play
== PT_STOP
) {
4350 play_tree_iter_free(mpctx
->playtree_iter
);
4351 mpctx
->playtree_iter
= NULL
;
4352 } else { // NEXT PREV SRC
4353 playtree_direction
= mpctx
->stop_play
== PT_PREV_SRC
? -1 : 1;
4356 while(mpctx
->playtree_iter
!= NULL
) {
4357 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
, playtree_direction
);
4358 if(mpctx
->filename
== NULL
) {
4359 if(play_tree_iter_step(mpctx
->playtree_iter
, playtree_direction
, 0) != PLAY_TREE_ITER_ENTRY
) {
4360 play_tree_iter_free(mpctx
->playtree_iter
);
4361 mpctx
->playtree_iter
= NULL
;
4368 if(use_gui
&& !mpctx
->playtree_iter
) {
4369 #ifdef CONFIG_DVDREAD
4370 if(!guiIntfStruct
.DiskChanged
)
4376 if(use_gui
|| mpctx
->playtree_iter
!= NULL
|| player_idle_mode
){
4377 if(!mpctx
->playtree_iter
) mpctx
->filename
= NULL
;
4378 mpctx
->stop_play
= 0;
4379 goto play_next_file
;
4383 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
4387 #endif /* DISABLE_MAIN */