2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 /// \ingroup Properties Command2Property OSDMsgStack
30 #if defined(__MINGW32__) || defined(__CYGWIN__)
31 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
37 // #include <sys/mman.h>
38 #include <sys/types.h>
40 #include <sys/ioctl.h>
43 #define SIGHUP 1 /* hangup */
44 #define SIGQUIT 3 /* quit */
45 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
46 #define SIGBUS 10 /* bus error */
47 #define SIGPIPE 13 /* broken pipe */
67 #include "access_mpcontext.h"
68 #include "m_property.h"
70 #include "cfg-mplayer-def.h"
72 #include "libavutil/avstring.h"
74 #include "sub/subreader.h"
75 #include "sub/dec_sub.h"
78 #include "libvo/video_out.h"
80 #include "sub/font_load.h"
82 #include "ffmpeg_files/intreadwrite.h"
83 #include "sub/av_sub.h"
84 #include "libmpcodecs/dec_teletext.h"
85 #include "cpudetect.h"
89 #include "libvo/x11_common.h"
92 #include "libao2/audio_out.h"
94 #include "codec-cfg.h"
98 #include "sub/spudec.h"
99 #include "sub/vobsub.h"
101 #include "osdep/getch2.h"
102 #include "osdep/timer.h"
103 #include "osdep/findfiles.h"
105 #include "input/input.h"
108 int enable_mouse_movements
=0;
109 float start_volume
= -1;
111 #include "osdep/priority.h"
117 #include <linux/rtc.h>
120 #define RTC_IRQP_SET RTCIO_IRQP_SET
121 #define RTC_PIE_ON RTCIO_PIE_ON
122 #endif /* __linux__ */
123 #endif /* HAVE_RTC */
125 #include "stream/tv.h"
126 #include "stream/stream_radio.h"
128 #include "stream/dvbin.h"
130 #include "stream/cache2.h"
132 //**************************************************************************//
134 //**************************************************************************//
135 #include "playtree.h"
136 #include "playtreeparser.h"
138 //**************************************************************************//
140 //**************************************************************************//
141 #include "parser-cfg.h"
142 #include "parser-mpcmd.h"
144 //**************************************************************************//
146 //**************************************************************************//
148 static int cfg_inc_verbose(m_option_t
*conf
){ ++verbose
; return 0;}
152 //**************************************************************************//
153 //**************************************************************************//
154 // Input media streaming & demultiplexer:
155 //**************************************************************************//
157 static int max_framesize
=0;
159 #include "stream/stream.h"
160 #include "libmpdemux/demuxer.h"
161 #include "libmpdemux/stheader.h"
163 #ifdef CONFIG_DVDREAD
164 #include "stream/stream_dvd.h"
166 #include "stream/stream_dvdnav.h"
168 #include "libmpcodecs/dec_audio.h"
169 #include "libmpcodecs/dec_video.h"
170 #include "libmpcodecs/mp_image.h"
171 #include "libmpcodecs/vf.h"
172 #include "libmpcodecs/vd.h"
178 #include "defaultopts.h"
180 static const char help_text
[]=_(
181 "Usage: mplayer [options] [url|path/]filename\n"
183 "Basic options: (complete list in the man page)\n"
184 " -vo <drv> select video output driver ('-vo help' for a list)\n"
185 " -ao <drv> select audio output driver ('-ao help' for a list)\n"
187 " vcd://<trackno> play (S)VCD (Super Video CD) track (raw device, no mount)\n"
189 #ifdef CONFIG_DVDREAD
190 " dvd://<titleno> play DVD title from device instead of plain file\n"
192 " -alang/-slang select DVD audio/subtitle language (by 2-char country code)\n"
193 " -ss <position> seek to given (seconds or hh:mm:ss) position\n"
194 " -nosound do not play sound\n"
195 " -fs fullscreen playback (or -vm, -zoom, details in the man page)\n"
196 " -x <x> -y <y> set display resolution (for use with -vm or -zoom)\n"
197 " -sub <file> specify subtitle file to use (also see -subfps, -subdelay)\n"
198 " -playlist <file> specify playlist file\n"
199 " -vid x -aid y select video (x) and audio (y) stream to play\n"
200 " -fps x -srate y change video (x fps) and audio (y Hz) rate\n"
201 " -pp <quality> enable postprocessing filter (details in the man page)\n"
202 " -framedrop enable frame dropping (for slow machines)\n"
204 "Basic keys: (complete list in the man page, also check input.conf)\n"
205 " <- or -> seek backward/forward 10 seconds\n"
206 " down or up seek backward/forward 1 minute\n"
207 " pgdown or pgup seek backward/forward 10 minutes\n"
208 " < or > step backward/forward in playlist\n"
209 " p or SPACE pause movie (press any key to continue)\n"
210 " q or ESC stop playing and quit program\n"
211 " + or - adjust audio delay by +/- 0.1 second\n"
212 " o cycle OSD mode: none / seekbar / seekbar + timer\n"
213 " * or / increase or decrease PCM volume\n"
214 " x or z adjust subtitle delay by +/- 0.1 second\n"
215 " r or t adjust subtitle position up/down, also see -vf expand\n"
217 " * * * SEE THE MAN PAGE FOR DETAILS, FURTHER (ADVANCED) OPTIONS AND KEYS * * *\n"
221 #define Exit_SIGILL_RTCpuSel _(\
222 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
223 " It may be a bug in our new runtime CPU-detection code...\n"\
224 " Please read DOCS/HTML/en/bugreports.html.\n")
226 #define Exit_SIGILL _(\
227 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
228 " It usually happens when you run it on a CPU different than the one it was\n"\
229 " compiled/optimized for.\n"\
232 #define Exit_SIGSEGV_SIGFPE _(\
233 "- MPlayer crashed by bad usage of CPU/FPU/RAM.\n"\
234 " Recompile MPlayer with --enable-debug and make a 'gdb' backtrace and\n"\
235 " disassembly. Details in DOCS/HTML/en/bugreports_what.html#bugreports_crash.\n")
237 #define Exit_SIGCRASH _(\
238 "- MPlayer crashed. This shouldn't happen.\n"\
239 " It can be a bug in the MPlayer code _or_ in your drivers _or_ in your\n"\
240 " gcc version. If you think it's MPlayer's fault, please read\n"\
241 " DOCS/HTML/en/bugreports.html and follow the instructions there. We can't and\n"\
242 " won't help unless you provide this information when reporting a possible bug.\n")
244 #define SystemTooSlow _("\n\n"\
245 " ************************************************\n"\
246 " **** Your system is too SLOW to play this! ****\n"\
247 " ************************************************\n\n"\
248 "Possible reasons, problems, workarounds:\n"\
249 "- Most common: broken/buggy _audio_ driver\n"\
250 " - Try -ao sdl or use the OSS emulation of ALSA.\n"\
251 " - Experiment with different values for -autosync, 30 is a good start.\n"\
252 "- Slow video output\n"\
253 " - Try a different -vo driver (-vo help for a list) or try -framedrop!\n"\
255 " - Don't try to play a big DVD/DivX on a slow CPU! Try some of the lavdopts,\n"\
256 " e.g. -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all.\n"\
258 " - Try various combinations of -nobps -ni -forceidx -mc 0.\n"\
259 "- Slow media (NFS/SMB mounts, DVD, VCD etc)\n"\
260 " - Try -cache 8192.\n"\
261 "- Are you using -cache to play a non-interleaved AVI file?\n"\
262 " - Try -nocache.\n"\
263 "Read DOCS/HTML/en/video.html for tuning/speedup tips.\n"\
264 "If none of this helps you, read DOCS/HTML/en/bugreports.html.\n\n")
267 //**************************************************************************//
268 //**************************************************************************//
273 double video_time_usage
=0;
274 double vout_time_usage
=0;
275 static double audio_time_usage
=0;
276 static int total_time_usage_start
=0;
277 static int total_frame_cnt
=0;
278 static int drop_frame_cnt
=0; // total number of dropped frames
281 static int output_quality
=0;
284 static double seek_to_sec
;
285 static off_t seek_to_byte
=0;
286 static off_t step_sec
=0;
288 static m_time_size_t end_at
= { .type
= END_AT_NONE
, .pos
= 0 };
291 char **audio_codec_list
=NULL
; // override audio codec
292 char **video_codec_list
=NULL
; // override video codec
293 char **audio_fm_list
=NULL
; // override audio codec family
294 char **video_fm_list
=NULL
; // override video codec family
296 // this dvdsub_id was selected via slang
297 // use this to allow dvdnav to follow -slang across stream resets,
298 // in particular the subtitle ID for a language changes
301 static char* spudec_ifo
=NULL
;
302 int forced_subs_only
=0;
306 int stream_cache_size
=-1;
309 int stream_dump_type
=0;
312 static float default_max_pts_correction
=-1;//0.01f;
314 static int ignore_start
=0;
317 static int force_srate
=0;
318 int frame_dropping
=0; // option 0=no drop 1= drop vo 2= drop decode
319 static int play_n_frames
=-1;
320 static int play_n_frames_mf
=-1;
323 char *font_name
=NULL
;
324 char *sub_font_name
=NULL
;
325 extern int font_fontconfig
;
326 float font_factor
=0.75;
327 char **sub_name
=NULL
;
331 char *vobsub_name
=NULL
;
333 int suboverlap_enabled
= 1;
335 #include "sub/ass_mp.h"
337 char* current_module
=NULL
; // for debugging
343 #include "m_struct.h"
344 #include "libmenu/menu.h"
345 static const vf_info_t
* const libmenu_vfs
[] = {
349 static vf_instance_t
* vf_menu
= NULL
;
351 static char* menu_cfg
= NULL
;
352 static char* menu_root
= "main";
356 edl_record_ptr edl_records
= NULL
; ///< EDL entries memory area
357 edl_record_ptr next_edl_record
= NULL
; ///< only for traversing edl_records
358 FILE* edl_fd
= NULL
; ///< fd to write to when in -edlout mode.
359 int use_filedir_conf
;
360 int use_filename_title
;
362 #include "mpcommon.h"
365 #include "metadata.h"
367 const void *mpctx_get_video_out(MPContext
*mpctx
)
369 return mpctx
->video_out
;
372 const void *mpctx_get_audio_out(MPContext
*mpctx
)
374 return mpctx
->audio_out
;
377 void *mpctx_get_demuxer(MPContext
*mpctx
)
379 return mpctx
->demuxer
;
382 void *mpctx_get_playtree_iter(MPContext
*mpctx
)
384 return mpctx
->playtree_iter
;
387 void *mpctx_get_mixer(MPContext
*mpctx
)
389 return &mpctx
->mixer
;
392 int mpctx_get_global_sub_size(MPContext
*mpctx
)
394 return mpctx
->global_sub_size
;
397 int mpctx_get_osd_function(MPContext
*mpctx
)
399 return mpctx
->osd_function
;
402 static float get_relative_time(struct MPContext
*mpctx
)
404 unsigned int new_time
= GetTimer();
405 unsigned int delta
= new_time
- mpctx
->last_time
;
406 mpctx
->last_time
= new_time
;
407 return delta
* 0.000001;
410 static int is_valid_metadata_type(struct MPContext
*mpctx
, metadata_t type
) {
413 /* check for valid video stream */
414 case META_VIDEO_CODEC
:
415 case META_VIDEO_BITRATE
:
416 case META_VIDEO_RESOLUTION
:
418 if (!mpctx
->sh_video
)
423 /* check for valid audio stream */
424 case META_AUDIO_CODEC
:
425 case META_AUDIO_BITRATE
:
426 case META_AUDIO_SAMPLES
:
428 if (!mpctx
->sh_audio
)
433 /* check for valid demuxer */
434 case META_INFO_TITLE
:
435 case META_INFO_ARTIST
:
436 case META_INFO_ALBUM
:
438 case META_INFO_COMMENT
:
439 case META_INFO_TRACK
:
440 case META_INFO_GENRE
:
454 static char *get_demuxer_info(struct MPContext
*mpctx
, char *tag
) {
455 char **info
= mpctx
->demuxer
->info
;
459 return talloc_strdup(NULL
, "");
461 for (n
= 0; info
[2*n
] != NULL
; n
++)
462 if (!strcasecmp (info
[2*n
], tag
))
465 return talloc_strdup(NULL
, info
[2*n
+1] ? info
[2*n
+1] : "");
468 char *get_metadata(struct MPContext
*mpctx
, metadata_t type
)
470 sh_audio_t
* const sh_audio
= mpctx
->sh_audio
;
471 sh_video_t
* const sh_video
= mpctx
->sh_video
;
473 if (!is_valid_metadata_type(mpctx
, type
))
478 return talloc_strdup(NULL
, mp_basename(mpctx
->filename
));
479 case META_VIDEO_CODEC
:
480 if (sh_video
->format
== 0x10000001)
481 return talloc_strdup(NULL
, "mpeg1");
482 else if (sh_video
->format
== 0x10000002)
483 return talloc_strdup(NULL
, "mpeg2");
484 else if (sh_video
->format
== 0x10000004)
485 return talloc_strdup(NULL
, "mpeg4");
486 else if (sh_video
->format
== 0x10000005)
487 return talloc_strdup(NULL
, "h264");
488 else if (sh_video
->format
>= 0x20202020)
489 return talloc_asprintf(NULL
, "%.4s", (char *) &sh_video
->format
);
491 return talloc_asprintf(NULL
, "0x%08X", sh_video
->format
);
492 case META_VIDEO_BITRATE
:
493 return talloc_asprintf(NULL
, "%d kbps",
494 (int) (sh_video
->i_bps
* 8 / 1024));
495 case META_VIDEO_RESOLUTION
:
496 return talloc_asprintf(NULL
, "%d x %d", sh_video
->disp_w
,
498 case META_AUDIO_CODEC
:
499 if (sh_audio
->codec
&& sh_audio
->codec
->name
)
500 return talloc_strdup(NULL
, sh_audio
->codec
->name
);
501 return talloc_strdup(NULL
, "");
502 case META_AUDIO_BITRATE
:
503 return talloc_asprintf(NULL
, "%d kbps",
504 (int) (sh_audio
->i_bps
* 8/1000));
505 case META_AUDIO_SAMPLES
:
506 return talloc_asprintf(NULL
, "%d Hz, %d ch.", sh_audio
->samplerate
,
509 /* check for valid demuxer */
510 case META_INFO_TITLE
:
511 return get_demuxer_info(mpctx
, "Title");
513 case META_INFO_ARTIST
:
514 return get_demuxer_info(mpctx
, "Artist");
516 case META_INFO_ALBUM
:
517 return get_demuxer_info(mpctx
, "Album");
520 return get_demuxer_info(mpctx
, "Year");
522 case META_INFO_COMMENT
:
523 return get_demuxer_info(mpctx
, "Comment");
525 case META_INFO_TRACK
:
526 return get_demuxer_info(mpctx
, "Track");
528 case META_INFO_GENRE
:
529 return get_demuxer_info(mpctx
, "Genre");
535 return talloc_strdup(NULL
, "");
538 static void print_file_properties(struct MPContext
*mpctx
, const char *filename
)
540 double start_pts
= MP_NOPTS_VALUE
;
541 double video_start_pts
= MP_NOPTS_VALUE
;
542 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_FILENAME=%s\n",
543 filename_recode(filename
));
544 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_DEMUXER=%s\n", mpctx
->demuxer
->desc
->name
);
545 if (mpctx
->sh_video
) {
546 /* Assume FOURCC if all bytes >= 0x20 (' ') */
547 if (mpctx
->sh_video
->format
>= 0x20202020)
548 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx
->sh_video
->format
);
550 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FORMAT=0x%08X\n", mpctx
->sh_video
->format
);
551 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_BITRATE=%d\n", mpctx
->sh_video
->i_bps
*8);
552 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_WIDTH=%d\n", mpctx
->sh_video
->disp_w
);
553 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_HEIGHT=%d\n", mpctx
->sh_video
->disp_h
);
554 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_FPS=%5.3f\n", mpctx
->sh_video
->fps
);
555 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_VIDEO_ASPECT=%1.4f\n", mpctx
->sh_video
->aspect
);
556 video_start_pts
= ds_get_next_pts(mpctx
->d_video
);
558 if (mpctx
->sh_audio
) {
559 /* Assume FOURCC if all bytes >= 0x20 (' ') */
560 if (mpctx
->sh_audio
->format
>= 0x20202020)
561 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx
->sh_audio
->format
);
563 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_FORMAT=%d\n", mpctx
->sh_audio
->format
);
564 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_BITRATE=%d\n", mpctx
->sh_audio
->i_bps
*8);
565 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_RATE=%d\n", mpctx
->sh_audio
->samplerate
);
566 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_AUDIO_NCH=%d\n", mpctx
->sh_audio
->channels
);
567 start_pts
= ds_get_next_pts(mpctx
->d_audio
);
569 if (video_start_pts
!= MP_NOPTS_VALUE
) {
570 if (start_pts
== MP_NOPTS_VALUE
|| !mpctx
->sh_audio
||
571 (mpctx
->sh_video
&& video_start_pts
< start_pts
))
572 start_pts
= video_start_pts
;
574 if (start_pts
!= MP_NOPTS_VALUE
)
575 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_START_TIME=%.2f\n", start_pts
);
577 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_START_TIME=unknown\n");
578 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_LENGTH=%.2f\n", get_time_length(mpctx
));
579 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_SEEKABLE=%d\n",
580 mpctx
->stream
->seek
&& (!mpctx
->demuxer
|| mpctx
->demuxer
->seekable
));
581 if (mpctx
->demuxer
) {
582 if (mpctx
->demuxer
->num_chapters
== 0)
583 stream_control(mpctx
->demuxer
->stream
, STREAM_CTRL_GET_NUM_CHAPTERS
, &mpctx
->demuxer
->num_chapters
);
584 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
,"ID_CHAPTERS=%d\n", mpctx
->demuxer
->num_chapters
);
588 /// step size of mixer changes
592 static void mp_dvdnav_context_free(MPContext
*ctx
){
593 if (ctx
->nav_smpi
) free_mp_image(ctx
->nav_smpi
);
594 ctx
->nav_smpi
= NULL
;
595 free(ctx
->nav_buffer
);
596 ctx
->nav_buffer
= NULL
;
597 ctx
->nav_start
= NULL
;
598 ctx
->nav_in_size
= 0;
602 static void uninit_subs(struct demuxer
*demuxer
)
604 for (int i
= 0; i
< MAX_S_STREAMS
; i
++) {
605 struct sh_sub
*sh
= demuxer
->s_streams
[i
];
606 if (sh
&& sh
->initialized
)
611 void uninit_player(struct MPContext
*mpctx
, unsigned int mask
){
612 mask
&= mpctx
->initialized_flags
;
614 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n*** uninit(0x%X)\n",mask
);
616 if(mask
&INITIALIZED_ACODEC
){
617 mpctx
->initialized_flags
&=~INITIALIZED_ACODEC
;
618 current_module
="uninit_acodec";
619 if(mpctx
->sh_audio
) uninit_audio(mpctx
->sh_audio
);
620 mpctx
->sh_audio
=NULL
;
621 mpctx
->mixer
.afilter
= NULL
;
624 if (mask
& INITIALIZED_SUB
) {
625 mpctx
->initialized_flags
&= ~INITIALIZED_SUB
;
626 if (mpctx
->d_sub
->sh
)
627 sub_switchoff(mpctx
->d_sub
->sh
, mpctx
->osd
);
630 if(mask
&INITIALIZED_VCODEC
){
631 mpctx
->initialized_flags
&=~INITIALIZED_VCODEC
;
632 current_module
="uninit_vcodec";
633 if(mpctx
->sh_video
) uninit_video(mpctx
->sh_video
);
634 mpctx
->sh_video
=NULL
;
640 if(mask
&INITIALIZED_DEMUXER
){
641 mpctx
->initialized_flags
&=~INITIALIZED_DEMUXER
;
642 current_module
="free_demuxer";
643 if (mpctx
->num_sources
) {
644 mpctx
->demuxer
= mpctx
->sources
[0].demuxer
;
645 for (int i
= 1; i
< mpctx
->num_sources
; i
++) {
646 uninit_subs(mpctx
->sources
[i
].demuxer
);
647 free_stream(mpctx
->sources
[i
].stream
);
648 free_demuxer(mpctx
->sources
[i
].demuxer
);
651 talloc_free(mpctx
->sources
);
652 mpctx
->sources
= NULL
;
653 mpctx
->num_sources
= 0;
654 talloc_free(mpctx
->timeline
);
655 mpctx
->timeline
= NULL
;
656 mpctx
->num_timeline_parts
= 0;
657 talloc_free(mpctx
->chapters
);
658 mpctx
->chapters
= NULL
;
659 mpctx
->num_chapters
= 0;
660 mpctx
->video_offset
= 0;
662 mpctx
->stream
=mpctx
->demuxer
->stream
;
663 uninit_subs(mpctx
->demuxer
);
664 free_demuxer(mpctx
->demuxer
);
669 // kill the cache process:
670 if(mask
&INITIALIZED_STREAM
){
671 mpctx
->initialized_flags
&=~INITIALIZED_STREAM
;
672 current_module
="uninit_stream";
673 if(mpctx
->stream
) free_stream(mpctx
->stream
);
677 if(mask
&INITIALIZED_VO
){
678 mpctx
->initialized_flags
&=~INITIALIZED_VO
;
679 current_module
="uninit_vo";
680 vo_destroy(mpctx
->video_out
);
681 mpctx
->video_out
=NULL
;
683 mp_dvdnav_context_free(mpctx
);
687 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
688 if(mask
&INITIALIZED_GETCH2
){
689 mpctx
->initialized_flags
&=~INITIALIZED_GETCH2
;
690 current_module
="uninit_getch2";
691 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n[[[uninit getch2]]]\n");
696 if(mask
&INITIALIZED_VOBSUB
){
697 mpctx
->initialized_flags
&=~INITIALIZED_VOBSUB
;
698 current_module
="uninit_vobsub";
699 if(vo_vobsub
) vobsub_close(vo_vobsub
);
703 if (mask
&INITIALIZED_SPUDEC
){
704 mpctx
->initialized_flags
&=~INITIALIZED_SPUDEC
;
705 current_module
="uninit_spudec";
706 spudec_free(vo_spudec
);
710 if(mask
&INITIALIZED_AO
){
711 mpctx
->initialized_flags
&=~INITIALIZED_AO
;
712 current_module
="uninit_ao";
713 if (mpctx
->edl_muted
) mixer_mute(&mpctx
->mixer
);
714 if (mpctx
->audio_out
)
715 mpctx
->audio_out
->uninit(mpctx
->stop_play
!= AT_END_OF_FILE
);
716 mpctx
->audio_out
=NULL
;
722 void exit_player_with_rc(struct MPContext
*mpctx
, enum exit_reason how
, int rc
)
724 if (mpctx
->user_muted
&& !mpctx
->edl_muted
) mixer_mute(&mpctx
->mixer
);
725 uninit_player(mpctx
, INITIALIZED_ALL
);
726 #if defined(__MINGW32__) || defined(__CYGWIN__)
730 vo_uninit(mpctx
->x11_state
); // Close the X11 connection (if any is open).
733 current_module
="uninit_input";
734 mp_input_uninit(mpctx
->input
);
740 #ifdef CONFIG_FREETYPE
741 current_module
="uninit_font";
742 if (mpctx
->osd
&& mpctx
->osd
->sub_font
!= vo_font
)
743 free_font_desc(mpctx
->osd
->sub_font
);
744 free_font_desc(vo_font
);
748 osd_free(mpctx
->osd
);
751 ass_library_done(ass_library
);
755 current_module
="exit_player";
757 // free mplayer config
759 m_config_free(mpctx
->mconfig
);
760 mpctx
->mconfig
= NULL
;
762 if(mpctx
->playtree_iter
)
763 play_tree_iter_free(mpctx
->playtree_iter
);
764 mpctx
->playtree_iter
= NULL
;
766 play_tree_free(mpctx
->playtree
, 1);
767 mpctx
->playtree
= NULL
;
769 talloc_free(mpctx
->key_fifo
);
771 free(edl_records
); // free mem allocated for EDL
775 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"\nExiting... (%s)\n","Quit");
776 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=QUIT\n");
779 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"\nExiting... (%s)\n","End of file");
780 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=EOF\n");
783 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"\nExiting... (%s)\n","Fatal error");
784 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=ERROR\n");
787 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_EXIT=NONE\n");
789 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"max framesize was %d bytes\n",max_framesize
);
794 static void exit_player(struct MPContext
*mpctx
, enum exit_reason how
)
796 exit_player_with_rc(mpctx
, how
, 1);
800 static void child_sighandler(int x
){
802 while((pid
=waitpid(-1,NULL
,WNOHANG
)) > 0);
806 #ifdef CONFIG_CRASH_DEBUG
807 static char *prog_path
;
808 static int crash_debug
= 0;
811 static void exit_sighandler(int x
){
812 static int sig_count
=0;
813 #ifdef CONFIG_CRASH_DEBUG
814 if (!crash_debug
|| x
!= SIGTRAP
)
819 /* We're crashing bad and can't uninit cleanly :(
820 * by popular request, we make one last (dirty)
821 * effort to restore the user's
826 if(sig_count
==6) exit(1);
830 kill(getpid(),SIGKILL
);
833 mp_msg(MSGT_CPLAYER
, MSGL_FATAL
, "\n");
834 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,
835 "\nMPlayer interrupted by signal %d in module: %s\n", x
,
836 current_module
? current_module
: "unknown");
837 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SIGNAL=%d\n", x
);
845 async_quit_request
= 1;
846 return; // killed from keyboard (^C) or killed [-9]
848 #if CONFIG_RUNTIME_CPUDETECT
849 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,Exit_SIGILL_RTCpuSel
);
851 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,Exit_SIGILL
);
855 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,Exit_SIGSEGV_SIGFPE
);
857 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,Exit_SIGCRASH
);
858 #ifdef CONFIG_CRASH_DEBUG
861 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Forking...\n");
863 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Forked...\n");
864 if (gdb_pid
== 0) { // We are the child
866 snprintf(spid
, sizeof(spid
), "%i", getppid());
867 getch2_disable(); // allow terminal to work properly with gdb
868 if (execlp("gdb", "gdb", prog_path
, spid
, "-ex", "bt", NULL
) == -1)
869 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Couldn't start gdb\n");
870 } else if (gdb_pid
< 0)
871 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Couldn't fork\n");
873 waitpid(gdb_pid
, NULL
, 0);
875 if (x
== SIGTRAP
) return;
883 #include "cfg-mplayer.h"
885 static int cfg_include(m_option_t
*conf
, char *filename
)
887 return m_config_parse_config_file(conf
->priv
, filename
);
890 static void parse_cfgfiles(struct MPContext
*mpctx
, m_config_t
* conf
)
892 struct MPOpts
*opts
= &mpctx
->opts
;
895 if (!(opts
->noconfig
& 2) &&
896 m_config_parse_config_file(conf
, MPLAYER_CONFDIR
"/mplayer.conf") < 0)
897 exit_player(mpctx
, EXIT_NONE
);
898 if ((conffile
= get_path("")) == NULL
) {
899 mp_tmsg(MSGT_CPLAYER
,MSGL_WARN
,"Cannot find HOME directory.\n");
904 mkdir(conffile
, 0777);
907 if ((conffile
= get_path("config")) == NULL
) {
908 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"get_path(\"config\") problem\n");
910 if ((conffile_fd
= open(conffile
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666)) != -1) {
911 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Creating config file: %s\n", conffile
);
912 write(conffile_fd
, default_config
, strlen(default_config
));
915 if (!(opts
->noconfig
& 1) &&
916 m_config_parse_config_file(conf
, conffile
) < 0)
917 exit_player(mpctx
, EXIT_NONE
);
923 #define PROFILE_CFG_PROTOCOL "protocol."
925 static void load_per_protocol_config (m_config_t
* conf
, const char *const file
)
928 char protocol
[strlen (PROFILE_CFG_PROTOCOL
) + strlen (file
) + 1];
931 /* does filename actually uses a protocol ? */
932 str
= strstr (file
, "://");
936 sprintf (protocol
, "%s%s", PROFILE_CFG_PROTOCOL
, file
);
937 protocol
[strlen (PROFILE_CFG_PROTOCOL
)+strlen(file
)-strlen(str
)] = '\0';
938 p
= m_config_get_profile (conf
, protocol
);
941 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Loading protocol-related profile '%s'\n", protocol
);
942 m_config_set_profile(conf
,p
);
946 #define PROFILE_CFG_EXTENSION "extension."
948 static void load_per_extension_config (m_config_t
* conf
, const char *const file
)
951 char extension
[strlen (PROFILE_CFG_EXTENSION
) + 8];
954 /* does filename actually have an extension ? */
955 str
= strrchr (file
, '.');
959 sprintf (extension
, PROFILE_CFG_EXTENSION
);
960 strncat (extension
, ++str
, 7);
961 p
= m_config_get_profile (conf
, extension
);
964 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Loading extension-related profile '%s'\n", extension
);
965 m_config_set_profile(conf
,p
);
969 #define PROFILE_CFG_VO "vo."
970 #define PROFILE_CFG_AO "ao."
972 static void load_per_output_config (m_config_t
* conf
, char *cfg
, char *out
)
974 char profile
[strlen (cfg
) + strlen (out
) + 1];
977 sprintf (profile
, "%s%s", cfg
, out
);
978 p
= m_config_get_profile (conf
, profile
);
981 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Loading extension-related profile '%s'\n", profile
);
982 m_config_set_profile(conf
,p
);
987 * Tries to load a config file
988 * @return 0 if file was not found, 1 otherwise
990 static int try_load_config(m_config_t
*conf
, const char *file
)
995 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "Loading config '%s'\n", file
);
996 m_config_parse_config_file (conf
, file
);
1000 static void load_per_file_config (m_config_t
* conf
, const char *const file
)
1006 if (strlen(file
) > PATH_MAX
- 14) {
1007 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, "Filename is too long, can not load file or directory specific config files\n");
1010 sprintf (cfg
, "%s.conf", file
);
1012 name
= mp_basename(cfg
);
1013 if (use_filedir_conf
) {
1014 char dircfg
[PATH_MAX
];
1015 strcpy(dircfg
, cfg
);
1016 strcpy(dircfg
+ (name
- cfg
), "mplayer.conf");
1017 try_load_config(conf
, dircfg
);
1019 if (try_load_config(conf
, cfg
))
1023 if ((confpath
= get_path (name
)) != NULL
)
1025 try_load_config(conf
, confpath
);
1031 /* When libmpdemux performs a blocking operation (network connection or
1032 * cache filling) if the operation fails we use this function to check
1033 * if it was interrupted by the user.
1034 * The function returns a new value for eof. */
1035 static int libmpdemux_was_interrupted(struct MPContext
*mpctx
, int stop_play
)
1038 if((cmd
= mp_input_get_cmd(mpctx
->input
, 0, 0)) != NULL
) {
1041 exit_player_with_rc(mpctx
, EXIT_QUIT
, (cmd
->nargs
> 0)? cmd
->args
[0].v
.i
: 0);
1042 case MP_CMD_PLAY_TREE_STEP
: {
1043 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_NEXT_ENTRY
: PT_PREV_ENTRY
;
1044 mpctx
->play_tree_step
= (cmd
->args
[0].v
.i
== 0) ? 1 : cmd
->args
[0].v
.i
;
1046 case MP_CMD_PLAY_TREE_UP_STEP
: {
1047 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_UP_NEXT
: PT_UP_PREV
;
1049 case MP_CMD_PLAY_ALT_SRC_STEP
: {
1050 stop_play
= (cmd
->args
[0].v
.i
> 0) ? PT_NEXT_SRC
: PT_PREV_SRC
;
1058 static int playtree_add_playlist(struct MPContext
*mpctx
, play_tree_t
* entry
)
1060 play_tree_add_bpf(entry
,mpctx
->filename
);
1064 entry
= mpctx
->playtree_iter
->tree
;
1065 if(play_tree_iter_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
1066 return PT_NEXT_ENTRY
;
1068 if(mpctx
->playtree_iter
->tree
== entry
) { // Loop with a single file
1069 if(play_tree_iter_up_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
1070 return PT_NEXT_ENTRY
;
1073 play_tree_remove(entry
,1,1);
1076 play_tree_insert_entry(mpctx
->playtree_iter
->tree
,entry
);
1077 play_tree_set_params_from(entry
,mpctx
->playtree_iter
->tree
);
1078 entry
= mpctx
->playtree_iter
->tree
;
1079 if(play_tree_iter_step(mpctx
->playtree_iter
,1,0) != PLAY_TREE_ITER_ENTRY
) {
1080 return PT_NEXT_ENTRY
;
1082 play_tree_remove(entry
,1,1);
1087 void add_subtitles(struct MPContext
*mpctx
, char *filename
, float fps
, int noerr
)
1089 struct MPOpts
*opts
= &mpctx
->opts
;
1090 sub_data
*subd
= NULL
;
1091 struct ass_track
*asst
= NULL
;
1092 bool is_native_ass
= false;
1094 if (filename
== NULL
|| mpctx
->set_of_sub_size
>= MAX_SUBTITLE_FILES
)
1098 if (opts
->ass_enabled
) {
1100 asst
= mp_ass_read_stream(ass_library
, filename
, sub_cp
);
1102 asst
= mp_ass_read_stream(ass_library
, filename
, 0);
1104 is_native_ass
= asst
;
1106 subd
= sub_read_file(filename
, fps
, &mpctx
->opts
);
1108 asst
= mp_ass_read_subdata(ass_library
, subd
, fps
);
1115 subd
= sub_read_file(filename
, fps
, &mpctx
->opts
);
1118 if (!asst
&& !subd
) {
1119 mp_tmsg(MSGT_CPLAYER
, noerr
? MSGL_WARN
: MSGL_ERR
,
1120 "Cannot load subtitles: %s\n", filename_recode(filename
));
1124 mpctx
->set_of_ass_tracks
[mpctx
->set_of_sub_size
] = asst
;
1125 mpctx
->set_of_subtitles
[mpctx
->set_of_sub_size
] = subd
;
1126 mpctx
->track_was_native_ass
[mpctx
->set_of_sub_size
] = is_native_ass
;
1127 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_FILE_SUB_ID=%d\n", mpctx
->set_of_sub_size
);
1128 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_FILE_SUB_FILENAME=%s\n",
1129 filename_recode(filename
));
1130 ++mpctx
->set_of_sub_size
;
1131 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "SUB: Added subtitle file (%d): %s\n", mpctx
->set_of_sub_size
,
1132 filename_recode(filename
));
1135 void init_vo_spudec(struct MPContext
*mpctx
)
1137 spudec_free(vo_spudec
);
1138 mpctx
->initialized_flags
&= ~INITIALIZED_SPUDEC
;
1141 // we currently can't work without video stream
1142 if (!mpctx
->sh_video
)
1146 unsigned int palette
[16], width
, height
;
1147 current_module
="spudec_init_vobsub";
1148 if (vobsub_parse_ifo(NULL
,spudec_ifo
, palette
, &width
, &height
, 1, -1, NULL
) >= 0)
1149 vo_spudec
=spudec_new_scaled(palette
, width
, height
, NULL
, 0);
1152 #ifdef CONFIG_DVDREAD
1153 if (vo_spudec
==NULL
&& mpctx
->stream
->type
==STREAMTYPE_DVD
) {
1154 current_module
="spudec_init_dvdread";
1155 vo_spudec
=spudec_new_scaled(((dvd_priv_t
*)(mpctx
->stream
->priv
))->cur_pgc
->palette
,
1156 mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
,
1161 #ifdef CONFIG_DVDNAV
1162 if (vo_spudec
==NULL
&& mpctx
->stream
->type
==STREAMTYPE_DVDNAV
) {
1163 unsigned int *palette
= mp_dvdnav_get_spu_clut(mpctx
->stream
);
1164 current_module
="spudec_init_dvdnav";
1165 vo_spudec
=spudec_new_scaled(palette
, mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
, NULL
, 0);
1169 if (vo_spudec
==NULL
) {
1170 sh_sub_t
*sh
= mpctx
->d_sub
->sh
;
1171 current_module
="spudec_init_normal";
1172 vo_spudec
=spudec_new_scaled(NULL
, mpctx
->sh_video
->disp_w
, mpctx
->sh_video
->disp_h
, sh
->extradata
, sh
->extradata_len
);
1173 spudec_set_font_factor(vo_spudec
,font_factor
);
1176 if (vo_spudec
!=NULL
) {
1177 mpctx
->initialized_flags
|=INITIALIZED_SPUDEC
;
1178 mp_property_do("sub_forced_only", M_PROPERTY_SET
, &forced_subs_only
, mpctx
);
1183 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1184 * make it all work is to use the builtin SDL-bootstrap code, which
1185 * will be done automatically by replacing our main() if we include SDL.h.
1187 #if defined(__APPLE__) && defined(CONFIG_SDL)
1188 #ifdef CONFIG_SDL_SDL_H
1189 #include <SDL/SDL.h>
1196 * \brief append a formatted string
1197 * \param buf buffer to print into
1198 * \param pos position of terminating 0 in buf
1199 * \param len maximum number of characters in buf, not including terminating 0
1200 * \param format printf format string
1202 static void saddf(char *buf
, unsigned *pos
, int len
, const char *format
, ...)
1205 va_start(va
, format
);
1206 *pos
+= vsnprintf(&buf
[*pos
], len
- *pos
, format
, va
);
1215 * \brief append time in the hh:mm:ss.f format
1216 * \param buf buffer to print into
1217 * \param pos position of terminating 0 in buf
1218 * \param len maximum number of characters in buf, not including terminating 0
1219 * \param time time value to convert/append
1221 static void sadd_hhmmssf(char *buf
, unsigned *pos
, int len
, float time
) {
1222 int64_t tenths
= 10 * time
;
1223 int f1
= tenths
% 10;
1224 int ss
= (tenths
/ 10) % 60;
1225 int mm
= (tenths
/ 600) % 60;
1226 int hh
= tenths
/ 36000;
1228 saddf(buf
, pos
, len
, "unknown");
1232 saddf(buf
, pos
, len
, "%2d:", hh
);
1233 if (hh
> 0 || mm
> 0)
1234 saddf(buf
, pos
, len
, "%02d:", mm
);
1235 saddf(buf
, pos
, len
, "%02d.%1d", ss
, f1
);
1238 static void print_status(struct MPContext
*mpctx
, double a_pos
, bool at_frame
)
1240 struct MPOpts
*opts
= &mpctx
->opts
;
1241 sh_video_t
* const sh_video
= mpctx
->sh_video
;
1243 if (mpctx
->sh_audio
&& a_pos
== MP_NOPTS_VALUE
)
1244 a_pos
= playing_audio_pts(mpctx
);
1245 if (mpctx
->sh_audio
&& sh_video
&& at_frame
) {
1246 mpctx
->last_av_difference
= a_pos
- mpctx
->video_pts
- audio_delay
;
1247 if (mpctx
->time_frame
> 0)
1248 mpctx
->last_av_difference
+= mpctx
->time_frame
* opts
->playback_speed
;
1249 if (mpctx
->last_av_difference
> 0.5 && drop_frame_cnt
> 50
1250 && !mpctx
->drop_message_shown
) {
1251 mp_tmsg(MSGT_AVSYNC
,MSGL_WARN
,SystemTooSlow
);
1252 mpctx
->drop_message_shown
= true;
1263 if (screen_width
> 0)
1264 width
= screen_width
;
1267 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1268 /* Windows command line is broken (MinGW's rxvt works, but we
1269 * should not depend on that). */
1272 line
= malloc(width
+ 1); // one additional char for the terminating null
1275 if (mpctx
->sh_audio
) {
1276 saddf(line
, &pos
, width
, "A:%6.1f ", a_pos
);
1278 float len
= get_time_length(mpctx
);
1279 saddf(line
, &pos
, width
, "(");
1280 sadd_hhmmssf(line
, &pos
, width
, a_pos
);
1281 saddf(line
, &pos
, width
, ") of %.1f (", len
);
1282 sadd_hhmmssf(line
, &pos
, width
, len
);
1283 saddf(line
, &pos
, width
, ") ");
1289 saddf(line
, &pos
, width
, "V:%6.1f ", mpctx
->video_pts
);
1292 if (mpctx
->sh_audio
&& sh_video
)
1293 saddf(line
, &pos
, width
, "A-V:%7.3f ct:%7.3f ",
1294 mpctx
->last_av_difference
, mpctx
->total_avsync_change
);
1298 saddf(line
, &pos
, width
, "%3d/%3d ",
1299 (int)sh_video
->num_frames
,
1300 (int)sh_video
->num_frames_decoded
);
1304 if (sh_video
->timer
> 0.5)
1305 saddf(line
, &pos
, width
, "%2d%% %2d%% %4.1f%% ",
1306 (int)(100.0*video_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
),
1307 (int)(100.0*vout_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
),
1308 (100.0*audio_time_usage
*opts
->playback_speed
/(double)sh_video
->timer
));
1310 saddf(line
, &pos
, width
, "??%% ??%% ??,?%% ");
1311 } else if (mpctx
->sh_audio
) {
1312 if (mpctx
->delay
> 0.5)
1313 saddf(line
, &pos
, width
, "%4.1f%% ",
1314 100.0*audio_time_usage
/(double)mpctx
->delay
);
1316 saddf(line
, &pos
, width
, "??,?%% ");
1321 saddf(line
, &pos
, width
, "%d %d ", drop_frame_cnt
, output_quality
);
1323 #ifdef CONFIG_STREAM_CACHE
1325 if (stream_cache_size
> 0)
1326 saddf(line
, &pos
, width
, "%d%% ", cache_fill_status
);
1330 if (opts
->playback_speed
!= 1)
1331 saddf(line
, &pos
, width
, "%4.2fx ", opts
->playback_speed
);
1334 if (erase_to_end_of_line
) {
1336 mp_msg(MSGT_STATUSLINE
, MSGL_STATUS
, "%s%s\r", line
, erase_to_end_of_line
);
1338 memset(&line
[pos
], ' ', width
- pos
);
1340 mp_msg(MSGT_STATUSLINE
, MSGL_STATUS
, "%s\r", line
);
1346 * \brief build a chain of audio filters that converts the input format
1347 * to the ao's format, taking into account the current playback_speed.
1348 * \param sh_audio describes the requested input format of the chain.
1349 * \param ao_data describes the requested output format of the chain.
1351 static int build_afilter_chain(struct MPContext
*mpctx
, sh_audio_t
*sh_audio
,
1354 struct MPOpts
*opts
= &mpctx
->opts
;
1359 mpctx
->mixer
.afilter
= NULL
;
1362 if(af_control_any_rev(sh_audio
->afilter
,
1363 AF_CONTROL_PLAYBACK_SPEED
| AF_CONTROL_SET
,
1364 &opts
->playback_speed
)) {
1365 new_srate
= sh_audio
->samplerate
;
1367 new_srate
= sh_audio
->samplerate
* opts
->playback_speed
;
1368 if (new_srate
!= ao_data
->samplerate
) {
1369 // limits are taken from libaf/af_resample.c
1370 if (new_srate
< 8000)
1372 if (new_srate
> 192000)
1374 opts
->playback_speed
= (float)new_srate
/ (float)sh_audio
->samplerate
;
1377 result
= init_audio_filters(sh_audio
, new_srate
,
1378 &ao_data
->samplerate
, &ao_data
->channels
, &ao_data
->format
);
1379 mpctx
->mixer
.afilter
= sh_audio
->afilter
;
1384 typedef struct mp_osd_msg mp_osd_msg_t
;
1386 /// Previous message on the stack.
1390 int id
,level
,started
;
1391 /// Display duration in ms.
1395 /// OSD message stack.
1396 static mp_osd_msg_t
* osd_msg_stack
= NULL
;
1399 * \brief Add a message on the OSD message stack
1401 * If a message with the same id is already present in the stack
1402 * it is pulled on top of the stack, otherwise a new message is created.
1405 static void set_osd_msg_va(int id
, int level
, int time
, const char *fmt
,
1408 mp_osd_msg_t
*msg
,*last
=NULL
;
1411 // look if the id is already in the stack
1412 for(msg
= osd_msg_stack
; msg
&& msg
->id
!= id
;
1413 last
= msg
, msg
= msg
->prev
);
1414 // not found: alloc it
1416 msg
= calloc(1,sizeof(mp_osd_msg_t
));
1417 msg
->prev
= osd_msg_stack
;
1418 osd_msg_stack
= msg
;
1419 } else if(last
) { // found, but it's not on top of the stack
1420 last
->prev
= msg
->prev
;
1421 msg
->prev
= osd_msg_stack
;
1422 osd_msg_stack
= msg
;
1425 r
= vsnprintf(msg
->msg
, 128, fmt
, ap
);
1426 if(r
>= 128) msg
->msg
[127] = 0;
1434 void set_osd_msg(int id
, int level
, int time
, const char *fmt
, ...)
1438 set_osd_msg_va(id
, level
, time
, fmt
, ap
);
1442 void set_osd_tmsg(int id
, int level
, int time
, const char *fmt
, ...)
1446 set_osd_msg_va(id
, level
, time
, mp_gtext(fmt
), ap
);
1452 * \brief Remove a message from the OSD stack
1454 * This function can be used to get rid of a message right away.
1458 void rm_osd_msg(int id
) {
1459 mp_osd_msg_t
*msg
,*last
=NULL
;
1461 // Search for the msg
1462 for(msg
= osd_msg_stack
; msg
&& msg
->id
!= id
;
1463 last
= msg
, msg
= msg
->prev
);
1466 // Detach it from the stack and free it
1468 last
->prev
= msg
->prev
;
1470 osd_msg_stack
= msg
->prev
;
1475 * \brief Remove all messages from the OSD stack
1479 static void clear_osd_msgs(void) {
1480 mp_osd_msg_t
* msg
= osd_msg_stack
, *prev
= NULL
;
1486 osd_msg_stack
= NULL
;
1490 * \brief Get the current message from the OSD stack.
1492 * This function decrements the message timer and destroys the old ones.
1493 * The message that should be displayed is returned (if any).
1497 static mp_osd_msg_t
* get_osd_msg(struct MPContext
*mpctx
)
1499 struct MPOpts
*opts
= &mpctx
->opts
;
1500 mp_osd_msg_t
*msg
,*prev
,*last
= NULL
;
1501 static unsigned last_update
= 0;
1502 unsigned now
= GetTimerMS();
1504 char hidden_dec_done
= 0;
1506 if (mpctx
->osd_visible
) {
1507 // 36000000 means max timed visibility is 1 hour into the future, if
1508 // the difference is greater assume it's wrapped around from below 0
1509 if (mpctx
->osd_visible
- now
> 36000000) {
1510 mpctx
->osd_visible
= 0;
1511 vo_osd_progbar_type
= -1; // disable
1512 vo_osd_changed(OSDTYPE_PROGBAR
);
1513 mpctx
->osd_function
= mpctx
->paused
? OSD_PAUSE
: OSD_PLAY
;
1516 if (mpctx
->osd_show_percentage_until
- now
> 36000000)
1517 mpctx
->osd_show_percentage_until
= 0;
1519 if(!last_update
) last_update
= now
;
1520 diff
= now
>= last_update
? now
- last_update
: 0;
1524 // Look for the first message in the stack with high enough level.
1525 for(msg
= osd_msg_stack
; msg
; last
= msg
, msg
= prev
) {
1527 if (msg
->level
> opts
->osd_level
&& hidden_dec_done
)
1529 // The message has a high enough level or it is the first hidden one
1530 // in both cases we decrement the timer or kill it.
1531 if(!msg
->started
|| msg
->time
> diff
) {
1532 if(msg
->started
) msg
->time
-= diff
;
1533 else msg
->started
= 1;
1535 if (msg
->level
<= opts
->osd_level
)
1537 hidden_dec_done
= 1;
1546 osd_msg_stack
= prev
;
1555 * \brief Display the OSD bar.
1557 * Display the OSD bar or fall back on a simple message.
1561 void set_osd_bar(struct MPContext
*mpctx
, int type
,const char* name
,double min
,double max
,double val
) {
1562 struct MPOpts
*opts
= &mpctx
->opts
;
1563 if (opts
->osd_level
< 1)
1566 if(mpctx
->sh_video
) {
1567 mpctx
->osd_visible
= (GetTimerMS() + 1000) | 1;
1568 vo_osd_progbar_type
= type
;
1569 vo_osd_progbar_value
= 256*(val
-min
)/(max
-min
);
1570 vo_osd_changed(OSDTYPE_PROGBAR
);
1574 set_osd_msg(OSD_MSG_BAR
, 1, opts
->osd_duration
, "%s: %d %%",
1575 name
, ROUND(100*(val
-min
)/(max
-min
)));
1579 * \brief Display text subtitles on the OSD
1581 void set_osd_subtitle(struct MPContext
*mpctx
, subtitle
*subs
)
1585 vo_osd_changed(OSDTYPE_SUBTITLE
);
1586 if (!mpctx
->sh_video
) {
1587 // reverse order, since newest set_osd_msg is displayed first
1588 for (i
= SUB_MAX_TEXT
- 1; i
>= 0; i
--) {
1589 if (!subs
|| i
>= subs
->lines
|| !subs
->text
[i
])
1590 rm_osd_msg(OSD_MSG_SUB_BASE
+ i
);
1592 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1593 int display_time
= i
== subs
->lines
- 1 ? 180000 : 2000;
1594 set_osd_msg(OSD_MSG_SUB_BASE
+ i
, 1, display_time
, "%s", subs
->text
[i
]);
1601 * \brief Update the OSD message line.
1603 * This function displays the current message on the vo OSD or on the term.
1604 * If the stack is empty and the OSD level is high enough the timer
1605 * is displayed (only on the vo OSD).
1609 static void update_osd_msg(struct MPContext
*mpctx
)
1611 struct MPOpts
*opts
= &mpctx
->opts
;
1613 struct osd_state
*osd
= mpctx
->osd
;
1614 char osd_text_timer
[128];
1616 if (mpctx
->add_osd_seek_info
) {
1617 double percentage
= get_percent_pos(mpctx
);
1618 set_osd_bar(mpctx
, 0, "Position", 0, 100, percentage
);
1619 if (mpctx
->sh_video
)
1620 mpctx
->osd_show_percentage_until
= (GetTimerMS() + 1000) | 1;
1621 mpctx
->add_osd_seek_info
= false;
1624 // Look if we have a msg
1625 if((msg
= get_osd_msg(mpctx
))) {
1626 if (strcmp(osd
->osd_text
, msg
->msg
)) {
1627 strncpy(osd
->osd_text
, msg
->msg
, 127);
1628 if(mpctx
->sh_video
) vo_osd_changed(OSDTYPE_OSD
); else
1630 mp_msg(MSGT_CPLAYER
,MSGL_STATUS
, "%s%s\n", opts
->term_osd_esc
,
1636 if(mpctx
->sh_video
) {
1637 // fallback on the timer
1638 if (opts
->osd_level
>= 2) {
1639 int len
= get_time_length(mpctx
);
1640 int percentage
= -1;
1641 char percentage_text
[10];
1642 int pts
= get_current_time(mpctx
);
1644 if (mpctx
->osd_show_percentage_until
)
1645 percentage
= get_percent_pos(mpctx
);
1647 if (percentage
>= 0)
1648 snprintf(percentage_text
, 9, " (%d%%)", percentage
);
1650 percentage_text
[0] = 0;
1652 if (opts
->osd_level
== 3)
1653 snprintf(osd_text_timer
, 63,
1654 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1655 mpctx
->osd_function
,pts
/3600,(pts
/60)%60,pts
%60,
1656 len
/3600,(len
/60)%60,len
%60,percentage_text
);
1658 snprintf(osd_text_timer
, 63, "%c %02d:%02d:%02d%s",
1659 mpctx
->osd_function
,pts
/3600,(pts
/60)%60,
1660 pts
%60,percentage_text
);
1662 osd_text_timer
[0]=0;
1664 if (strcmp(osd
->osd_text
, osd_text_timer
)) {
1665 strncpy(osd
->osd_text
, osd_text_timer
, 63);
1666 vo_osd_changed(OSDTYPE_OSD
);
1671 // Clear the term osd line
1672 if (opts
->term_osd
&& osd
->osd_text
[0]) {
1673 osd
->osd_text
[0] = 0;
1674 printf("%s\n", opts
->term_osd_esc
);
1682 void reinit_audio_chain(struct MPContext
*mpctx
)
1684 struct MPOpts
*opts
= &mpctx
->opts
;
1685 if (!mpctx
->sh_audio
)
1687 if (!(mpctx
->initialized_flags
& INITIALIZED_ACODEC
)) {
1688 current_module
="init_audio_codec";
1689 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
1690 if(!init_best_audio_codec(mpctx
->sh_audio
,audio_codec_list
,audio_fm_list
)){
1693 mpctx
->initialized_flags
|=INITIALIZED_ACODEC
;
1694 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
1698 current_module
="af_preinit";
1699 if (!(mpctx
->initialized_flags
& INITIALIZED_AO
)) {
1700 ao_data
.samplerate
=force_srate
;
1702 ao_data
.format
= opts
->audio_output_format
;
1704 // first init to detect best values
1705 if(!init_audio_filters(mpctx
->sh_audio
, // preliminary init
1707 mpctx
->sh_audio
->samplerate
,
1709 &ao_data
.samplerate
, &ao_data
.channels
, &ao_data
.format
)){
1710 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
, "Error at audio filter chain "
1712 exit_player(mpctx
, EXIT_ERROR
);
1714 if (!(mpctx
->initialized_flags
& INITIALIZED_AO
)) {
1715 current_module
="ao2_init";
1716 ao_data
.buffersize
= opts
->ao_buffersize
;
1717 mpctx
->audio_out
= init_best_audio_out(opts
->audio_driver_list
,
1722 if(!mpctx
->audio_out
){
1723 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Could not open/initialize audio device -> no sound.\n");
1726 mpctx
->initialized_flags
|=INITIALIZED_AO
;
1727 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1728 mpctx
->audio_out
->info
->short_name
,
1729 ao_data
.samplerate
, ao_data
.channels
,
1730 af_fmt2str_short(ao_data
.format
),
1731 af_fmt2bits(ao_data
.format
)/8 );
1732 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AO: Description: %s\nAO: Author: %s\n",
1733 mpctx
->audio_out
->info
->name
, mpctx
->audio_out
->info
->author
);
1734 if(strlen(mpctx
->audio_out
->info
->comment
) > 0)
1735 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AO: Comment: %s\n", mpctx
->audio_out
->info
->comment
);
1738 // init audio filters:
1739 current_module
="af_init";
1740 if(!build_afilter_chain(mpctx
, mpctx
->sh_audio
, &ao_data
)) {
1741 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Couldn't find matching filter/ao format!\n");
1744 mpctx
->mixer
.audio_out
= mpctx
->audio_out
;
1745 mpctx
->mixer
.volstep
= volstep
;
1746 mpctx
->syncing_audio
= true;
1750 uninit_player(mpctx
, INITIALIZED_ACODEC
|INITIALIZED_AO
); // close codec and possibly AO
1751 mpctx
->sh_audio
=mpctx
->d_audio
->sh
=NULL
; // -> nosound
1752 mpctx
->d_audio
->id
= -2;
1760 // Return pts value corresponding to the end point of audio written to the
1762 static double written_audio_pts(struct MPContext
*mpctx
)
1764 sh_audio_t
*sh_audio
= mpctx
->sh_audio
;
1765 demux_stream_t
*d_audio
= mpctx
->d_audio
;
1766 double buffered_output
;
1767 // first calculate the end pts of audio that has been output by decoder
1768 double a_pts
= sh_audio
->pts
;
1769 if (a_pts
!= MP_NOPTS_VALUE
)
1770 // Good, decoder supports new way of calculating audio pts.
1771 // sh_audio->pts is the timestamp of the latest input packet with
1772 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1773 // the amount of bytes the decoder has written after that timestamp.
1774 a_pts
+= sh_audio
->pts_bytes
/ (double) sh_audio
->o_bps
;
1776 // Decoder doesn't support new way of calculating pts (or we're
1777 // being called before it has decoded anything with known timestamp).
1778 // Use the old method of audio pts calculation: take the timestamp
1779 // of last packet with known pts the decoder has read data from,
1780 // and add amount of bytes read after the beginning of that packet
1781 // divided by input bps. This will be inaccurate if the input/output
1782 // ratio is not constant for every audio packet or if it is constant
1783 // but not accurately known in sh_audio->i_bps.
1785 a_pts
= d_audio
->pts
;
1786 // ds_tell_pts returns bytes read after last timestamp from
1787 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1788 // it has read but not decoded
1789 if (sh_audio
->i_bps
)
1790 a_pts
+= (ds_tell_pts(d_audio
) - sh_audio
->a_in_buffer_len
) /
1791 (double)sh_audio
->i_bps
;
1793 // Now a_pts hopefully holds the pts for end of audio from decoder.
1794 // Substract data in buffers between decoder and audio out.
1796 // Decoded but not filtered
1797 a_pts
-= sh_audio
->a_buffer_len
/ (double)sh_audio
->o_bps
;
1799 // Data buffered in audio filters, measured in bytes of "missing" output
1800 buffered_output
= af_calc_delay(sh_audio
->afilter
);
1802 // Data that was ready for ao but was buffered because ao didn't fully
1803 // accept everything to internal buffers yet
1804 buffered_output
+= sh_audio
->a_out_buffer_len
;
1806 // Filters divide audio length by playback_speed, so multiply by it
1807 // to get the length in original units without speedup or slowdown
1808 a_pts
-= buffered_output
* mpctx
->opts
.playback_speed
/ ao_data
.bps
;
1810 return a_pts
+ mpctx
->video_offset
;
1813 // Return pts value corresponding to currently playing audio.
1814 double playing_audio_pts(struct MPContext
*mpctx
)
1816 return written_audio_pts(mpctx
) - mpctx
->opts
.playback_speed
*
1817 mpctx
->audio_out
->get_delay();
1820 static bool is_av_sub(int type
)
1822 return type
== 'b' || type
== 'p' || type
== 'x';
1825 void update_subtitles(struct MPContext
*mpctx
, double refpts
,
1826 double sub_offset
, bool reset
)
1828 struct MPOpts
*opts
= &mpctx
->opts
;
1829 struct sh_video
*sh_video
= mpctx
->sh_video
;
1830 struct demux_stream
*d_sub
= mpctx
->d_sub
;
1831 double curpts
= refpts
+ sub_delay
;
1832 unsigned char *packet
=NULL
;
1834 struct sh_sub
*sh_sub
= d_sub
->sh
;
1835 int type
= sh_sub
? sh_sub
->type
: 'v';
1836 static subtitle subs
;
1839 sub_reset(sh_sub
, mpctx
->osd
);
1840 sub_clear_text(&subs
, MP_NOPTS_VALUE
);
1842 set_osd_subtitle(mpctx
, NULL
);
1844 spudec_reset(vo_spudec
);
1845 vo_osd_changed(OSDTYPE_SPU
);
1847 #ifdef CONFIG_FFMPEG
1848 if (is_av_sub(type
))
1849 reset_avsub(sh_sub
);
1854 if (mpctx
->subdata
) {
1855 if (sub_fps
==0) sub_fps
= sh_video
? sh_video
->fps
: 25;
1856 current_module
= "find_sub";
1857 find_sub(mpctx
, mpctx
->subdata
, curpts
*
1858 (mpctx
->subdata
->sub_uses_time
? 100. : sub_fps
));
1860 mpctx
->vo_sub_last
= vo_sub
;
1864 if (vobsub_id
>= 0 || type
== 'v') {
1866 current_module
= "spudec";
1867 /* Get a sub packet from the DVD or a vobsub */
1873 len
= vobsub_get_packet(vo_vobsub
, curpts
,
1874 (void**)&packet
, ×tamp
);
1876 mp_dbg(MSGT_CPLAYER
,MSGL_V
,"\rVOB sub: len=%d v_pts=%5.3f v_timer=%5.3f sub=%5.3f ts=%d \n",len
,refpts
,sh_video
->timer
,timestamp
/ 90000.0,timestamp
);
1881 len
= ds_get_packet_sub(d_sub
, (unsigned char**)&packet
);
1883 // XXX This is wrong, sh_video->pts can be arbitrarily
1884 // much behind demuxing position. Unfortunately using
1885 // d_video->pts which would have been the simplest
1886 // improvement doesn't work because mpeg specific hacks
1887 // in video.c set d_video->pts to 0.
1888 float x
= d_sub
->pts
- refpts
;
1889 if (x
> -20 && x
< 20) // prevent missing subs on pts reset
1890 timestamp
= 90000*d_sub
->pts
;
1891 else timestamp
= 90000*curpts
;
1892 mp_dbg(MSGT_CPLAYER
, MSGL_V
, "\rDVD sub: len=%d "
1893 "v_pts=%5.3f s_pts=%5.3f ts=%d \n", len
,
1894 refpts
, d_sub
->pts
, timestamp
);
1897 if (len
<=0 || !packet
) break;
1898 // create it only here, since with some broken demuxers we might
1899 // type = v but no DVD sub and we currently do not change the
1900 // "original frame size" ever after init, leading to wrong-sized
1903 vo_spudec
= spudec_new(NULL
);
1904 if (vo_vobsub
|| timestamp
>= 0)
1905 spudec_assemble(vo_spudec
, packet
, len
, timestamp
);
1907 } else if (is_text_sub(type
) || is_av_sub(type
) || type
== 'd') {
1908 if (type
== 'd' && !d_sub
->demuxer
->teletext
) {
1909 tt_stream_props tsp
= {0};
1911 if (teletext_control(NULL
, TV_VBI_CONTROL_START
, &ptr
) == VBI_CONTROL_TRUE
)
1912 d_sub
->demuxer
->teletext
= ptr
;
1914 if (d_sub
->non_interleaved
)
1915 ds_get_next_pts(d_sub
);
1917 while (d_sub
->first
) {
1918 double subpts
= ds_get_next_pts(d_sub
) + sub_offset
;
1919 if (subpts
> curpts
) {
1920 // Libass handled subs can be fed to it in advance
1921 if (!opts
->ass_enabled
|| !is_text_sub(type
))
1923 // Try to avoid demuxing whole file at once
1924 if (d_sub
->non_interleaved
&& subpts
> curpts
+ 1)
1927 double duration
= d_sub
->first
->duration
;
1928 len
= ds_get_packet_sub(d_sub
, &packet
);
1929 if (is_av_sub(type
)) {
1930 #ifdef CONFIG_FFMPEG
1931 decode_avsub(sh_sub
, packet
, len
, subpts
, duration
);
1936 if (len
< 2) continue;
1937 len
= FFMIN(len
- 2, AV_RB16(packet
));
1941 if (d_sub
->demuxer
->teletext
) {
1942 uint8_t *p
= packet
;
1947 if (p
[0] == 2 || p
[0] == 3)
1948 teletext_control(d_sub
->demuxer
->teletext
,
1949 TV_VBI_CONTROL_DECODE_DVB
, p
+ 2);
1956 if (sh_sub
&& sh_sub
->active
) {
1957 sub_decode(sh_sub
, mpctx
->osd
, packet
, len
, subpts
, duration
);
1960 if (subpts
!= MP_NOPTS_VALUE
) {
1962 sub_clear_text(&subs
, MP_NOPTS_VALUE
);
1963 if (type
== 'a') { // ssa/ass subs without libass => convert to plaintext
1965 unsigned char* p
= packet
;
1966 for (i
=0; i
< 8 && *p
!= '\0'; p
++)
1969 if (*p
== '\0') /* Broken line? */
1974 double endpts
= MP_NOPTS_VALUE
;
1975 if (subpts
!= MP_NOPTS_VALUE
&& duration
>= 0)
1976 endpts
= subpts
+ duration
;
1977 sub_add_text(&subs
, packet
, len
, endpts
);
1978 set_osd_subtitle(mpctx
, &subs
);
1980 if (d_sub
->non_interleaved
)
1981 ds_get_next_pts(d_sub
);
1983 if (!opts
->ass_enabled
)
1984 if (sub_clear_text(&subs
, curpts
))
1985 set_osd_subtitle(mpctx
, &subs
);
1988 spudec_heartbeat(vo_spudec
, 90000*curpts
);
1989 if (spudec_changed(vo_spudec
))
1990 vo_osd_changed(OSDTYPE_SPU
);
1993 current_module
=NULL
;
1996 static void update_teletext(sh_video_t
*sh_video
, demuxer_t
*demuxer
, int reset
)
2000 if (!demuxer
->teletext
)
2003 //Also forcing page update when such ioctl is not supported or call error occured
2004 if(teletext_control(demuxer
->teletext
,TV_VBI_CONTROL_IS_CHANGED
,&page_changed
)!=VBI_CONTROL_TRUE
)
2010 if(teletext_control(demuxer
->teletext
,TV_VBI_CONTROL_GET_VBIPAGE
,&vo_osd_teletext_page
)!=VBI_CONTROL_TRUE
)
2011 vo_osd_teletext_page
=NULL
;
2012 if(teletext_control(demuxer
->teletext
,TV_VBI_CONTROL_GET_HALF_PAGE
,&vo_osd_teletext_half
)!=VBI_CONTROL_TRUE
)
2013 vo_osd_teletext_half
=0;
2014 if(teletext_control(demuxer
->teletext
,TV_VBI_CONTROL_GET_MODE
,&vo_osd_teletext_mode
)!=VBI_CONTROL_TRUE
)
2015 vo_osd_teletext_mode
=0;
2016 if(teletext_control(demuxer
->teletext
,TV_VBI_CONTROL_GET_FORMAT
,&vo_osd_teletext_format
)!=VBI_CONTROL_TRUE
)
2017 vo_osd_teletext_format
=0;
2018 vo_osd_changed(OSDTYPE_TELETEXT
);
2020 teletext_control(demuxer
->teletext
,TV_VBI_CONTROL_MARK_UNCHANGED
,NULL
);
2023 static int check_framedrop(struct MPContext
*mpctx
, double frame_time
) {
2024 struct MPOpts
*opts
= &mpctx
->opts
;
2025 // check for frame-drop:
2026 current_module
= "check_framedrop";
2027 if (mpctx
->sh_audio
&& !mpctx
->d_audio
->eof
) {
2028 static int dropped_frames
;
2029 float delay
= opts
->playback_speed
*mpctx
->audio_out
->get_delay();
2030 float d
= delay
-mpctx
->delay
;
2032 // we should avoid dropping too many frames in sequence unless we
2033 // are too late. and we allow 100ms A-V delay here:
2034 if (d
< -dropped_frames
*frame_time
-0.100 && !mpctx
->paused
2035 && !mpctx
->restart_playback
) {
2038 return frame_dropping
;
2050 static float timing_sleep(struct MPContext
*mpctx
, float time_frame
)
2054 // -------- RTC -----------
2055 current_module
="sleep_rtc";
2056 while (time_frame
> 0.000) {
2057 unsigned long rtc_ts
;
2058 if (read(rtc_fd
, &rtc_ts
, sizeof(rtc_ts
)) <= 0)
2059 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Linux RTC read error: %s\n", strerror(errno
));
2060 time_frame
-= get_relative_time(mpctx
);
2065 // assume kernel HZ=100 for softsleep, works with larger HZ but with
2066 // unnecessarily high CPU usage
2067 struct MPOpts
*opts
= &mpctx
->opts
;
2068 float margin
= opts
->softsleep
? 0.011 : 0;
2069 current_module
= "sleep_timer";
2070 while (time_frame
> margin
) {
2071 usec_sleep(1000000 * (time_frame
- margin
));
2072 time_frame
-= get_relative_time(mpctx
);
2074 if (opts
->softsleep
){
2075 current_module
= "sleep_soft";
2077 mp_tmsg(MSGT_AVSYNC
, MSGL_WARN
, "Warning! Softsleep underflow!\n");
2078 while (time_frame
> 0)
2079 time_frame
-= get_relative_time(mpctx
); // burn the CPU
2085 static int select_subtitle(MPContext
*mpctx
)
2087 struct MPOpts
*opts
= &mpctx
->opts
;
2088 // find the best sub to use
2091 mpctx
->global_sub_pos
= -1; // no subs by default
2092 if (vobsub_id
>= 0) {
2093 // if user asks for a vobsub id, use that first.
2095 found
= mp_property_do("sub_vob", M_PROPERTY_SET
, &id
, mpctx
) == M_PROPERTY_OK
;
2098 if (!found
&& opts
->sub_id
>= 0) {
2099 // if user asks for a dvd sub id, use that next.
2101 found
= mp_property_do("sub_demux", M_PROPERTY_SET
, &id
, mpctx
) == M_PROPERTY_OK
;
2105 // if there are text subs to use, use those. (autosubs come last here)
2107 found
= mp_property_do("sub_file", M_PROPERTY_SET
, &id
, mpctx
) == M_PROPERTY_OK
;
2110 if (!found
&& opts
->sub_id
== -1) {
2111 // finally select subs by language and container hints
2112 if (opts
->sub_id
== -1)
2114 demuxer_sub_track_by_lang_and_default(mpctx
->d_sub
->demuxer
,
2116 if (opts
->sub_id
>= 0) {
2118 found
= mp_property_do("sub_demux", M_PROPERTY_SET
, &id
, mpctx
) == M_PROPERTY_OK
;
2124 #ifdef CONFIG_DVDNAV
2128 /// store decoded video image
2129 static mp_image_t
* mp_dvdnav_copy_mpi(mp_image_t
*to_mpi
,
2130 mp_image_t
*from_mpi
) {
2133 /// Do not store B-frames
2134 if (from_mpi
->pict_type
== FF_B_TYPE
)
2138 to_mpi
->w
== from_mpi
->w
&&
2139 to_mpi
->h
== from_mpi
->h
&&
2140 to_mpi
->imgfmt
== from_mpi
->imgfmt
)
2144 free_mp_image(to_mpi
);
2145 if (from_mpi
->w
== 0 || from_mpi
->h
== 0)
2147 mpi
= alloc_mpi(from_mpi
->w
,from_mpi
->h
,from_mpi
->imgfmt
);
2150 copy_mpi(mpi
,from_mpi
);
2154 static void mp_dvdnav_reset_stream (MPContext
*ctx
) {
2155 struct MPOpts
*opts
= &ctx
->opts
;
2156 if (ctx
->sh_video
) {
2158 ctx
->d_video
->pts
= 0.0f
;
2159 ctx
->sh_video
->pts
= 0.0f
;
2160 ctx
->sh_video
->i_pts
= 0.0f
;
2161 ctx
->sh_video
->last_pts
= 0.0f
;
2162 ctx
->sh_video
->num_buffered_pts
= 0;
2163 ctx
->sh_video
->num_frames
= 0;
2164 ctx
->sh_video
->num_frames_decoded
= 0;
2165 ctx
->sh_video
->timer
= 0.0f
;
2166 ctx
->sh_video
->stream_delay
= 0.0f
;
2167 ctx
->sh_video
->timer
= 0;
2168 ctx
->demuxer
->stream_pts
= MP_NOPTS_VALUE
;
2171 if (ctx
->sh_audio
) {
2172 /// free audio packets and reset
2173 ds_free_packs(ctx
->d_audio
);
2174 audio_delay
-= ctx
->sh_audio
->stream_delay
;
2175 ctx
->delay
=- audio_delay
;
2176 ctx
->audio_out
->reset();
2177 resync_audio_stream(ctx
->sh_audio
);
2181 ctx
->sub_counts
[SUB_SOURCE_DEMUX
] = mp_dvdnav_number_of_subs(ctx
->stream
);
2182 if (opts
->sub_lang
&& opts
->sub_id
== dvdsub_lang_id
) {
2183 dvdsub_lang_id
= mp_dvdnav_sid_from_lang(ctx
->stream
, opts
->sub_lang
);
2184 if (dvdsub_lang_id
!= opts
->sub_id
) {
2185 opts
->sub_id
= dvdsub_lang_id
;
2186 select_subtitle(ctx
);
2190 /// clear all EOF related flags
2191 ctx
->d_video
->eof
= ctx
->d_audio
->eof
= ctx
->stream
->eof
= 0;
2194 /// Restore last decoded DVDNAV (still frame)
2195 static mp_image_t
*mp_dvdnav_restore_smpi(struct MPContext
*mpctx
,
2197 unsigned char **start
,
2198 mp_image_t
*decoded_frame
)
2200 if (mpctx
->stream
->type
!= STREAMTYPE_DVDNAV
)
2201 return decoded_frame
;
2203 /// a change occured in dvdnav stream
2204 if (mp_dvdnav_cell_has_changed(mpctx
->stream
,0)) {
2205 mp_dvdnav_read_wait(mpctx
->stream
, 1, 1);
2206 mp_dvdnav_context_free(mpctx
);
2207 mp_dvdnav_reset_stream(mpctx
);
2208 mp_dvdnav_read_wait(mpctx
->stream
, 0, 1);
2209 mp_dvdnav_cell_has_changed(mpctx
->stream
,1);
2215 /// Display still frame, if any
2216 if (mpctx
->nav_smpi
&& !mpctx
->nav_buffer
)
2217 decoded_frame
= mpctx
->nav_smpi
;
2219 /// increment video frame : continue playing after still frame
2220 len
= get_time_length(mpctx
);
2221 if (mpctx
->sh_video
->pts
>= len
&&
2222 mpctx
->sh_video
->pts
> 0.0 && len
> 0.0) {
2223 mp_dvdnav_skip_still(mpctx
->stream
);
2224 mp_dvdnav_skip_wait(mpctx
->stream
);
2226 mpctx
->sh_video
->pts
+= 1 / mpctx
->sh_video
->fps
;
2228 if (mpctx
->nav_buffer
) {
2229 *start
= mpctx
->nav_start
;
2230 *in_size
= mpctx
->nav_in_size
;
2231 if (mpctx
->nav_start
)
2232 memcpy(*start
,mpctx
->nav_buffer
,mpctx
->nav_in_size
);
2236 return decoded_frame
;
2239 /// Save last decoded DVDNAV (still frame)
2240 static void mp_dvdnav_save_smpi(struct MPContext
*mpctx
, int in_size
,
2241 unsigned char *start
,
2242 mp_image_t
*decoded_frame
)
2244 if (mpctx
->stream
->type
!= STREAMTYPE_DVDNAV
)
2247 free(mpctx
->nav_buffer
);
2249 mpctx
->nav_buffer
= malloc(in_size
);
2250 mpctx
->nav_start
= start
;
2251 mpctx
->nav_in_size
= mpctx
->nav_buffer
? in_size
: -1;
2252 if (mpctx
->nav_buffer
)
2253 memcpy(mpctx
->nav_buffer
,start
,in_size
);
2255 if (decoded_frame
&& mpctx
->nav_smpi
!= decoded_frame
)
2256 mpctx
->nav_smpi
= mp_dvdnav_copy_mpi(mpctx
->nav_smpi
,decoded_frame
);
2258 #endif /* CONFIG_DVDNAV */
2260 /* Modify video timing to match the audio timeline. There are two main
2261 * reasons this is needed. First, video and audio can start from different
2262 * positions at beginning of file or after a seek (MPlayer starts both
2263 * immediately even if they have different pts). Second, the file can have
2264 * audio timestamps that are inconsistent with the duration of the audio
2265 * packets, for example two consecutive timestamp values differing by
2266 * one second but only a packet with enough samples for half a second
2267 * of playback between them.
2269 static void adjust_sync(struct MPContext
*mpctx
, double frame_time
)
2271 current_module
= "av_sync";
2273 if (!mpctx
->sh_audio
|| mpctx
->syncing_audio
)
2276 double a_pts
= written_audio_pts(mpctx
) - mpctx
->delay
;
2277 double v_pts
= mpctx
->sh_video
->pts
;
2278 double av_delay
= a_pts
- v_pts
;
2279 // Try to sync vo_flip() so it will *finish* at given time
2280 av_delay
+= mpctx
->last_vo_flip_duration
;
2281 av_delay
-= audio_delay
; // This much pts difference is desired
2283 double change
= av_delay
* 0.1;
2284 double max_change
= default_max_pts_correction
>= 0 ?
2285 default_max_pts_correction
: frame_time
* 0.1;
2286 if (change
< -max_change
)
2287 change
= -max_change
;
2288 else if (change
> max_change
)
2289 change
= max_change
;
2290 mpctx
->delay
+= change
;
2291 mpctx
->total_avsync_change
+= change
;
2294 #define ASYNC_PLAY_DONE -3
2295 static int audio_start_sync(struct MPContext
*mpctx
, int playsize
)
2297 struct MPOpts
*opts
= &mpctx
->opts
;
2298 sh_audio_t
* const sh_audio
= mpctx
->sh_audio
;
2301 // Timing info may not be set without
2302 res
= decode_audio(sh_audio
, 1);
2307 bool did_retry
= false;
2309 double written_pts
= written_audio_pts(mpctx
);
2310 double ptsdiff
= written_pts
- mpctx
->video_pts
- mpctx
->delay
2312 bytes
= ptsdiff
* ao_data
.bps
/ mpctx
->opts
.playback_speed
;
2313 bytes
-= bytes
% (ao_data
.channels
* af_fmt2bits(ao_data
.format
) / 8);
2315 // ogg demuxers give packets without timing
2316 if (written_pts
<= 1 && sh_audio
->pts
== MP_NOPTS_VALUE
) {
2318 // Try to read more data to see packets that have pts
2319 int res
= decode_audio(sh_audio
, ao_data
.bps
);
2328 if (fabs(ptsdiff
) > 300) // pts reset or just broken?
2334 mpctx
->syncing_audio
= false;
2335 int a
= FFMIN(-bytes
, FFMAX(playsize
, 20000));
2336 int res
= decode_audio(sh_audio
, a
);
2337 bytes
+= sh_audio
->a_out_buffer_len
;
2339 memmove(sh_audio
->a_out_buffer
,
2340 sh_audio
->a_out_buffer
+
2341 sh_audio
->a_out_buffer_len
- bytes
,
2343 sh_audio
->a_out_buffer_len
= bytes
;
2346 return decode_audio(sh_audio
, playsize
);
2348 sh_audio
->a_out_buffer_len
= 0;
2353 if ((ao_data
.format
& AF_FORMAT_SIGN_MASK
) == AF_FORMAT_US
)
2355 if (bytes
>= playsize
) {
2356 /* This case could fall back to the one below with
2357 * bytes = playsize, but then silence would keep accumulating
2358 * in a_out_buffer if the AO accepts less data than it asks for
2360 char *p
= malloc(playsize
);
2361 memset(p
, fillbyte
, playsize
);
2362 playsize
= mpctx
->audio_out
->play(p
, playsize
, 0);
2364 mpctx
->delay
+= opts
->playback_speed
*playsize
/(double)ao_data
.bps
;
2365 return ASYNC_PLAY_DONE
;
2367 mpctx
->syncing_audio
= false;
2368 decode_audio_prepend_bytes(sh_audio
, bytes
, fillbyte
);
2369 return decode_audio(sh_audio
, playsize
);
2372 static int fill_audio_out_buffers(struct MPContext
*mpctx
)
2374 struct MPOpts
*opts
= &mpctx
->opts
;
2380 bool format_change
= false;
2381 sh_audio_t
* const sh_audio
= mpctx
->sh_audio
;
2383 current_module
="play_audio";
2387 // all the current uses of ao_data.pts seem to be in aos that handle
2388 // sync completely wrong; there should be no need to use ao_data.pts
2390 ao_data
.pts
= ((mpctx
->sh_video
?mpctx
->sh_video
->timer
:0)+mpctx
->delay
)*90000.0;
2391 playsize
= mpctx
->audio_out
->get_space();
2392 if (mpctx
->sh_video
|| playsize
>= ao_data
.outburst
)
2395 // handle audio-only case:
2396 // this is where mplayer sleeps during audio-only playback
2397 // to avoid 100% CPU use
2398 sleep_time
= (ao_data
.outburst
- playsize
) * 1000 / ao_data
.bps
;
2399 if (sleep_time
< 10) sleep_time
= 10; // limit to 100 wakeups per second
2400 usec_sleep(sleep_time
* 1000);
2403 // Fill buffer if needed:
2404 current_module
="decode_audio";
2407 if (!opts
->initial_audio_sync
|| (ao_data
.format
& AF_FORMAT_SPECIAL_MASK
))
2408 mpctx
->syncing_audio
= false;
2411 if (mpctx
->syncing_audio
&& mpctx
->sh_video
)
2412 res
= audio_start_sync(mpctx
, playsize
);
2414 res
= decode_audio(sh_audio
, playsize
);
2415 if (res
< 0) { // EOF, error or format change
2417 format_change
= true;
2418 else if (res
== ASYNC_PLAY_DONE
)
2420 else if (mpctx
->d_audio
->eof
) {
2422 int unitsize
= ao_data
.channels
* af_fmt2bits(ao_data
.format
) / 8;
2423 if (sh_audio
->a_out_buffer_len
< unitsize
)
2428 tt
= t
*0.000001f
; audio_time_usage
+=tt
;
2429 if (playsize
> sh_audio
->a_out_buffer_len
) {
2430 playsize
= sh_audio
->a_out_buffer_len
;
2432 playflags
|= AOPLAY_FINAL_CHUNK
;
2438 current_module
="play_audio";
2440 // Is this pts value actually useful for the aos that access it?
2441 // They're obviously badly broken in the way they handle av sync;
2442 // would not having access to this make them more broken?
2443 ao_data
.pts
= ((mpctx
->sh_video
?mpctx
->sh_video
->timer
:0)+mpctx
->delay
)*90000.0;
2444 playsize
= mpctx
->audio_out
->play(sh_audio
->a_out_buffer
, playsize
, playflags
);
2447 sh_audio
->a_out_buffer_len
-= playsize
;
2448 memmove(sh_audio
->a_out_buffer
, &sh_audio
->a_out_buffer
[playsize
],
2449 sh_audio
->a_out_buffer_len
);
2450 mpctx
->delay
+= opts
->playback_speed
*playsize
/(double)ao_data
.bps
;
2451 } else if (audio_eof
&& mpctx
->audio_out
->get_delay() < .04) {
2452 // Sanity check to avoid hanging in case current ao doesn't output
2453 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2454 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, "Audio output truncated at end.\n");
2455 sh_audio
->a_out_buffer_len
= 0;
2458 /* The format change isn't handled too gracefully. A more precise
2459 * implementation would require draining buffered old-format audio
2460 * while displaying video, then doing the output format switch.
2462 if (format_change
) {
2463 uninit_player(mpctx
, INITIALIZED_AO
);
2464 reinit_audio_chain(mpctx
);
2470 static int sleep_until_near_frame(struct MPContext
*mpctx
, float *time_frame
,
2471 float *aq_sleep_time
)
2473 struct MPOpts
*opts
= &mpctx
->opts
;
2474 double audio_limit
= 2;
2475 current_module
="calc_sleep_time";
2477 if (mpctx
->restart_playback
)
2480 *time_frame
-= get_relative_time(mpctx
); // reset timer
2482 if (mpctx
->sh_audio
&& !mpctx
->d_audio
->eof
) {
2483 float delay
= mpctx
->audio_out
->get_delay();
2484 mp_dbg(MSGT_AVSYNC
, MSGL_DBG2
, "delay=%f\n", delay
);
2486 if (opts
->autosync
) {
2488 * Adjust this raw delay value by calculating the expected
2489 * delay for this frame and generating a new value which is
2490 * weighted between the two. The higher autosync is, the
2491 * closer to the delay value gets to that which "-nosound"
2492 * would have used, and the longer it will take for A/V
2493 * sync to settle at the right value (but it eventually will.)
2494 * This settling time is very short for values below 100.
2496 float predicted
= mpctx
->delay
/ opts
->playback_speed
+ *time_frame
;
2497 float difference
= delay
- predicted
;
2498 delay
= predicted
+ difference
/ (float)opts
->autosync
;
2501 *time_frame
= delay
- mpctx
->delay
/ opts
->playback_speed
;
2503 // delay = amount of audio buffered in soundcard/driver
2504 delay
= FFMIN(delay
, 0.5);
2505 delay
= FFMAX(delay
, 0.1);
2506 audio_limit
= delay
;
2508 // If we're lagging more than 200 ms behind the right playback rate,
2509 // don't try to "catch up".
2510 // If benchmark is set always output frames as fast as possible
2511 // without sleeping.
2512 if (*time_frame
< -0.2 || opts
->benchmark
)
2516 double t
= *time_frame
- mpctx
->video_out
->flip_queue_offset
;
2522 if (t
> audio_limit
* 0.6)
2523 t
= audio_limit
* 0.5;
2524 *aq_sleep_time
+= t
;
2525 mp_input_get_cmd(mpctx
->input
, t
* 1000 + 1, 1);
2529 int reinit_video_chain(struct MPContext
*mpctx
)
2531 struct MPOpts
*opts
= &mpctx
->opts
;
2532 sh_video_t
* const sh_video
= mpctx
->sh_video
;
2534 //================== Init VIDEO (codec & libvo) ==========================
2535 if (!opts
->fixed_vo
|| !(mpctx
->initialized_flags
& INITIALIZED_VO
)) {
2536 current_module
="preinit_libvo";
2538 //shouldn't we set dvideo->id=-2 when we fail?
2539 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2540 if(!(mpctx
->video_out
=init_best_video_out(opts
, mpctx
->x11_state
, mpctx
->key_fifo
, mpctx
->input
))){
2541 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"Error opening/initializing the selected video_out (-vo) device.\n");
2544 mpctx
->initialized_flags
|=INITIALIZED_VO
;
2547 if(stream_control(mpctx
->demuxer
->stream
, STREAM_CTRL_GET_ASPECT_RATIO
, &ar
) != STREAM_UNSUPPORTED
)
2548 mpctx
->sh_video
->stream_aspect
= ar
;
2549 current_module
="init_video_filters";
2551 char* vf_arg
[] = { "_oldargs_", (char*)mpctx
->video_out
, NULL
};
2552 sh_video
->vfilter
= vf_open_filter(opts
, NULL
,"vo",vf_arg
);
2556 char* vf_arg
[] = { "_oldargs_", menu_root
, NULL
};
2557 vf_menu
= vf_open_plugin(opts
,libmenu_vfs
,sh_video
->vfilter
,"menu",vf_arg
);
2559 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Can't open libmenu video filter with root menu %s.\n",menu_root
);
2564 sh_video
->vfilter
= vf_menu
;
2568 if(opts
->ass_enabled
) {
2571 if (opts
->vf_settings
)
2572 for (i
= 0; opts
->vf_settings
[i
].name
; ++i
)
2573 if (strcmp(opts
->vf_settings
[i
].name
, "ass") == 0) {
2578 extern vf_info_t vf_info_ass
;
2579 const vf_info_t
* libass_vfs
[] = {&vf_info_ass
, NULL
};
2580 char* vf_arg
[] = {"auto", "1", NULL
};
2582 struct vf_instance
*vf_ass
= vf_open_plugin_noerr(opts
, libass_vfs
,
2587 sh_video
->vfilter
= vf_ass
;
2588 else if (retcode
== -1) // vf_ass open() returns -1 if there's VO EOSD
2589 mp_msg(MSGT_CPLAYER
, MSGL_V
, "[ass] vf_ass not needed\n");
2591 mp_msg(MSGT_CPLAYER
,MSGL_ERR
, "ASS: cannot add video filter\n");
2596 sh_video
->vfilter
= append_filters(sh_video
->vfilter
, opts
->vf_settings
);
2599 if (opts
->ass_enabled
)
2600 sh_video
->vfilter
->control(sh_video
->vfilter
, VFCTRL_INIT_EOSD
, ass_library
);
2603 current_module
="init_video_codec";
2605 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
2606 init_best_video_codec(sh_video
,video_codec_list
,video_fm_list
);
2607 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
2609 if(!sh_video
->initialized
){
2610 if(!opts
->fixed_vo
) uninit_player(mpctx
, INITIALIZED_VO
);
2614 mpctx
->initialized_flags
|=INITIALIZED_VCODEC
;
2616 if (sh_video
->codec
)
2617 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_CODEC=%s\n", sh_video
->codec
->name
);
2619 sh_video
->last_pts
= MP_NOPTS_VALUE
;
2620 sh_video
->num_buffered_pts
= 0;
2621 sh_video
->next_frame_time
= 0;
2623 if (opts
->auto_quality
> 0) {
2624 // Auto quality option enabled
2625 output_quality
=get_video_quality_max(sh_video
);
2626 if (opts
->auto_quality
> output_quality
)
2627 opts
->auto_quality
= output_quality
;
2629 output_quality
= opts
->auto_quality
;
2630 mp_msg(MSGT_CPLAYER
,MSGL_V
,"AutoQ: setting quality to %d.\n",output_quality
);
2631 set_video_quality(sh_video
,output_quality
);
2634 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2636 current_module
="init_vo";
2641 mpctx
->sh_video
= mpctx
->d_video
->sh
= NULL
;
2645 static double update_video_nocorrect_pts(struct MPContext
*mpctx
)
2647 struct sh_video
*sh_video
= mpctx
->sh_video
;
2648 double frame_time
= 0;
2649 struct vo
*video_out
= mpctx
->video_out
;
2651 current_module
= "filter_video";
2652 // In nocorrect-pts mode there is no way to properly time these frames
2653 if (vo_get_buffered_frame(video_out
, 0) >= 0)
2655 if (vf_output_queued_frame(sh_video
->vfilter
))
2657 unsigned char *packet
= NULL
;
2658 frame_time
= sh_video
->next_frame_time
;
2659 if (mpctx
->restart_playback
)
2661 int in_size
= video_read_frame(sh_video
, &sh_video
->next_frame_time
,
2662 &packet
, force_fps
);
2664 #ifdef CONFIG_DVDNAV
2665 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
2666 if (mp_dvdnav_is_eof(mpctx
->stream
))
2669 mpctx
->d_video
->eof
= 0;
2671 mpctx
->d_audio
->eof
= 0;
2672 mpctx
->stream
->eof
= 0;
2677 if (in_size
> max_framesize
)
2678 max_framesize
= in_size
;
2679 sh_video
->timer
+= frame_time
;
2680 if (mpctx
->sh_audio
)
2681 mpctx
->delay
-= frame_time
;
2682 // video_read_frame can change fps (e.g. for ASF video)
2683 vo_fps
= sh_video
->fps
;
2684 int framedrop_type
= check_framedrop(mpctx
, frame_time
);
2685 current_module
= "decode video";
2687 void *decoded_frame
;
2688 #ifdef CONFIG_DVDNAV
2689 decoded_frame
= mp_dvdnav_restore_smpi(mpctx
, &in_size
, &packet
, NULL
);
2690 if (in_size
>= 0 && !decoded_frame
)
2692 decoded_frame
= decode_video(sh_video
, packet
, in_size
, framedrop_type
,
2694 #ifdef CONFIG_DVDNAV
2695 // Save last still frame for future display
2696 mp_dvdnav_save_smpi(mpctx
, in_size
, packet
, decoded_frame
);
2698 if (decoded_frame
) {
2699 current_module
= "filter video";
2700 filter_video(sh_video
, decoded_frame
, sh_video
->pts
);
2707 static void determine_frame_pts(struct MPContext
*mpctx
)
2709 struct sh_video
*sh_video
= mpctx
->sh_video
;
2710 struct MPOpts
*opts
= &mpctx
->opts
;
2712 if (opts
->user_pts_assoc_mode
) {
2713 sh_video
->pts_assoc_mode
= opts
->user_pts_assoc_mode
;
2714 } else if (sh_video
->pts_assoc_mode
== 0) {
2715 if (mpctx
->d_video
->demuxer
->timestamp_type
== TIMESTAMP_TYPE_PTS
2716 && sh_video
->codec_reordered_pts
!= MP_NOPTS_VALUE
)
2717 sh_video
->pts_assoc_mode
= 1;
2719 sh_video
->pts_assoc_mode
= 2;
2721 int probcount1
= sh_video
->num_reordered_pts_problems
;
2722 int probcount2
= sh_video
->num_sorted_pts_problems
;
2723 if (sh_video
->pts_assoc_mode
== 2) {
2724 int tmp
= probcount1
;
2725 probcount1
= probcount2
;
2728 if (probcount1
>= probcount2
* 1.5 + 2) {
2729 sh_video
->pts_assoc_mode
= 3 - sh_video
->pts_assoc_mode
;
2730 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Switching to pts association mode "
2731 "%d.\n", sh_video
->pts_assoc_mode
);
2734 sh_video
->pts
= sh_video
->pts_assoc_mode
== 1 ?
2735 sh_video
->codec_reordered_pts
: sh_video
->sorted_pts
;
2738 static double update_video(struct MPContext
*mpctx
)
2740 struct sh_video
*sh_video
= mpctx
->sh_video
;
2741 struct vo
*video_out
= mpctx
->video_out
;
2742 sh_video
->vfilter
->control(sh_video
->vfilter
, VFCTRL_SET_OSD_OBJ
,
2743 mpctx
->osd
); // hack for vf_expand
2744 if (!mpctx
->opts
.correct_pts
)
2745 return update_video_nocorrect_pts(mpctx
);
2750 current_module
= "filter_video";
2751 if (!mpctx
->hrseek_active
2752 && vo_get_buffered_frame(video_out
, false) >= 0)
2754 // XXX Time used in this call is not counted in any performance
2756 if (vf_output_queued_frame(sh_video
->vfilter
))
2758 unsigned char *packet
= NULL
;
2759 int in_size
= ds_get_packet_pts(mpctx
->d_video
, &packet
, &pts
);
2760 if (pts
!= MP_NOPTS_VALUE
)
2761 pts
+= mpctx
->video_offset
;
2762 bool hit_eof
= false;
2764 // try to extract last frames in case of decoder lag
2766 pts
= MP_NOPTS_VALUE
;
2769 if (in_size
> max_framesize
)
2770 max_framesize
= in_size
;
2771 current_module
= "decode video";
2772 if (pts
>= mpctx
->hrseek_pts
- .005)
2773 mpctx
->hrseek_framedrop
= false;
2774 int framedrop_type
= mpctx
->hrseek_framedrop
? 1 :
2775 check_framedrop(mpctx
, sh_video
->frametime
);
2776 void *decoded_frame
= decode_video(sh_video
, packet
, in_size
,
2777 framedrop_type
, pts
);
2778 if (decoded_frame
) {
2779 determine_frame_pts(mpctx
);
2780 current_module
= "filter video";
2781 filter_video(sh_video
, decoded_frame
, sh_video
->pts
);
2782 } else if (hit_eof
) {
2783 if (vo_get_buffered_frame(video_out
, true) < 0)
2789 if (!video_out
->frame_loaded
)
2792 pts
= video_out
->next_pts
;
2793 if (pts
== MP_NOPTS_VALUE
) {
2794 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Video pts after filters MISSING\n");
2795 // Try to use decoder pts from before filters
2796 pts
= sh_video
->pts
;
2797 if (pts
== MP_NOPTS_VALUE
)
2798 pts
= sh_video
->last_pts
;
2800 if (mpctx
->hrseek_active
&& pts
< mpctx
->hrseek_pts
- .005)
2802 mpctx
->hrseek_active
= false;
2803 sh_video
->pts
= pts
;
2804 if (sh_video
->last_pts
== MP_NOPTS_VALUE
)
2805 sh_video
->last_pts
= sh_video
->pts
;
2806 else if (sh_video
->last_pts
> sh_video
->pts
) {
2807 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Decreasing video pts: %f < %f\n",
2808 sh_video
->pts
, sh_video
->last_pts
);
2809 /* If the difference in pts is small treat it as jitter around the
2810 * right value (possibly caused by incorrect timestamp ordering) and
2811 * just show this frame immediately after the last one.
2812 * Treat bigger differences as timestamp resets and start counting
2813 * timing of later frames from the position of this one. */
2814 if (sh_video
->last_pts
- sh_video
->pts
> 0.5)
2815 sh_video
->last_pts
= sh_video
->pts
;
2817 sh_video
->pts
= sh_video
->last_pts
;
2819 double frame_time
= sh_video
->pts
- sh_video
->last_pts
;
2820 sh_video
->last_pts
= sh_video
->pts
;
2821 sh_video
->timer
+= frame_time
;
2822 if (mpctx
->sh_audio
)
2823 mpctx
->delay
-= frame_time
;
2827 void pause_player(struct MPContext
*mpctx
)
2832 mpctx
->step_frames
= 0;
2833 mpctx
->time_frame
-= get_relative_time(mpctx
);
2835 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
)
2836 vo_control(mpctx
->video_out
, VOCTRL_PAUSE
, NULL
);
2838 if (mpctx
->audio_out
&& mpctx
->sh_audio
)
2839 mpctx
->audio_out
->pause(); // pause audio, keep data if possible
2842 void unpause_player(struct MPContext
*mpctx
)
2848 if (mpctx
->audio_out
&& mpctx
->sh_audio
)
2849 mpctx
->audio_out
->resume(); // resume audio
2850 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
2851 && !mpctx
->step_frames
)
2852 vo_control(mpctx
->video_out
, VOCTRL_RESUME
, NULL
); // resume video
2853 (void)get_relative_time(mpctx
); // ignore time that passed during pause
2856 void add_step_frame(struct MPContext
*mpctx
)
2858 mpctx
->step_frames
++;
2859 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
)
2860 vo_control(mpctx
->video_out
, VOCTRL_PAUSE
, NULL
);
2861 unpause_player(mpctx
);
2864 static void pause_loop(struct MPContext
*mpctx
)
2866 struct MPOpts
*opts
= &mpctx
->opts
;
2869 if (opts
->term_osd
&& !mpctx
->sh_video
) {
2870 set_osd_tmsg(OSD_MSG_PAUSE
, 1, 0, " ===== PAUSE =====");
2871 update_osd_msg(mpctx
);
2873 mp_msg(MSGT_CPLAYER
, MSGL_STATUS
, "%c%s%c",
2874 '\n', mp_gtext(" ===== PAUSE ====="), '\r');
2875 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_PAUSED\n");
2878 while ( (cmd
= mp_input_get_cmd(mpctx
->input
, 20, 1)) == NULL
2879 || cmd
->id
== MP_CMD_SET_MOUSE_POS
|| cmd
->pausing
== 4) {
2881 cmd
= mp_input_get_cmd(mpctx
->input
, 0, 0);
2882 run_command(mpctx
, cmd
);
2886 if (mpctx
->sh_video
&& mpctx
->video_out
)
2887 vo_check_events(mpctx
->video_out
);
2890 vf_menu_pause_update(vf_menu
);
2893 update_osd_msg(mpctx
);
2894 int hack
= vo_osd_changed(0);
2895 vo_osd_changed(hack
);
2902 // Find the right mute status and record position for new file position
2903 static void edl_seek_reset(MPContext
*mpctx
)
2905 mpctx
->edl_muted
= 0;
2906 next_edl_record
= edl_records
;
2908 while (next_edl_record
) {
2909 if (next_edl_record
->start_sec
>= get_current_time(mpctx
))
2912 if (next_edl_record
->action
== EDL_MUTE
)
2913 mpctx
->edl_muted
= !mpctx
->edl_muted
;
2914 next_edl_record
= next_edl_record
->next
;
2916 if ((mpctx
->user_muted
| mpctx
->edl_muted
) != mpctx
->mixer
.muted
)
2917 mixer_mute(&mpctx
->mixer
);
2921 // Execute EDL command for the current position if one exists
2922 static void edl_update(MPContext
*mpctx
)
2924 if (!next_edl_record
)
2927 if (!mpctx
->sh_video
) {
2928 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Cannot use EDL without video, disabling.\n");
2929 free_edl(edl_records
);
2930 next_edl_record
= NULL
;
2935 if (get_current_time(mpctx
) >= next_edl_record
->start_sec
) {
2936 if (next_edl_record
->action
== EDL_SKIP
) {
2937 mpctx
->osd_function
= OSD_FFW
;
2938 queue_seek(mpctx
, MPSEEK_RELATIVE
, next_edl_record
->length_sec
, 0);
2939 mp_msg(MSGT_CPLAYER
, MSGL_DBG4
, "EDL_SKIP: start [%f], stop "
2940 "[%f], length [%f]\n", next_edl_record
->start_sec
,
2941 next_edl_record
->stop_sec
, next_edl_record
->length_sec
);
2943 else if (next_edl_record
->action
== EDL_MUTE
) {
2944 mpctx
->edl_muted
= !mpctx
->edl_muted
;
2945 if ((mpctx
->user_muted
| mpctx
->edl_muted
) != mpctx
->mixer
.muted
)
2946 mixer_mute(&mpctx
->mixer
);
2947 mp_msg(MSGT_CPLAYER
, MSGL_DBG4
, "EDL_MUTE: [%f]\n",
2948 next_edl_record
->start_sec
);
2950 next_edl_record
= next_edl_record
->next
;
2954 static void reinit_decoders(struct MPContext
*mpctx
)
2956 reinit_video_chain(mpctx
);
2957 reinit_audio_chain(mpctx
);
2958 mp_property_do("sub", M_PROPERTY_SET
, &(int){mpctx
->global_sub_pos
}, mpctx
);
2961 static void seek_reset(struct MPContext
*mpctx
)
2963 if (mpctx
->sh_video
) {
2964 current_module
= "seek_video_reset";
2965 resync_video_stream(mpctx
->sh_video
);
2966 mpctx
->sh_video
->timer
= 0;
2967 vo_seek_reset(mpctx
->video_out
);
2968 mpctx
->sh_video
->timer
= 0;
2969 mpctx
->sh_video
->num_buffered_pts
= 0;
2970 mpctx
->sh_video
->last_pts
= MP_NOPTS_VALUE
;
2972 mpctx
->time_frame
= 0;
2973 mpctx
->restart_playback
= true;
2974 // Not all demuxers set d_video->pts during seek, so this value
2975 // (which is used by at least vobsub and edl code below) may
2976 // be completely wrong (probably 0).
2977 mpctx
->sh_video
->pts
= mpctx
->d_video
->pts
+ mpctx
->video_offset
;
2978 mpctx
->video_pts
= mpctx
->sh_video
->pts
;
2979 update_subtitles(mpctx
, mpctx
->sh_video
->pts
, mpctx
->video_offset
,
2981 update_teletext(mpctx
->sh_video
, mpctx
->demuxer
, 1);
2984 if (mpctx
->sh_audio
) {
2985 current_module
= "seek_audio_reset";
2986 resync_audio_stream(mpctx
->sh_audio
);
2987 mpctx
->audio_out
->reset(); // stop audio, throwing away buffered data
2988 mpctx
->sh_audio
->a_buffer_len
= 0;
2989 mpctx
->sh_audio
->a_out_buffer_len
= 0;
2990 if (!mpctx
->sh_video
)
2991 update_subtitles(mpctx
, mpctx
->sh_audio
->pts
,
2992 mpctx
->video_offset
, true);
2995 if (vo_vobsub
&& mpctx
->sh_video
) {
2996 current_module
= "seek_vobsub_reset";
2997 vobsub_seek(vo_vobsub
, mpctx
->sh_video
->pts
);
3000 edl_seek_reset(mpctx
);
3002 mpctx
->hrseek_active
= false;
3003 mpctx
->hrseek_framedrop
= false;
3004 mpctx
->total_avsync_change
= 0;
3005 audio_time_usage
= 0; video_time_usage
= 0; vout_time_usage
= 0;
3008 current_module
= NULL
;
3011 static bool timeline_set_part(struct MPContext
*mpctx
, int i
)
3013 struct timeline_part
*p
= mpctx
->timeline
+ mpctx
->timeline_part
;
3014 struct timeline_part
*n
= mpctx
->timeline
+ i
;
3015 mpctx
->timeline_part
= i
;
3016 mpctx
->video_offset
= n
->start
- n
->source_start
;
3017 if (n
->source
== p
->source
)
3019 uninit_player(mpctx
, INITIALIZED_VCODEC
| (mpctx
->opts
.fixed_vo
&& mpctx
->opts
.video_id
!= -2 ? 0 : INITIALIZED_VO
) | INITIALIZED_AO
| INITIALIZED_ACODEC
| INITIALIZED_SUB
);
3020 mpctx
->demuxer
= n
->source
->demuxer
;
3021 mpctx
->d_video
= mpctx
->demuxer
->video
;
3022 mpctx
->d_audio
= mpctx
->demuxer
->audio
;
3023 mpctx
->d_sub
= mpctx
->demuxer
->sub
;
3024 mpctx
->sh_video
= mpctx
->d_video
->sh
;
3025 mpctx
->sh_audio
= mpctx
->d_audio
->sh
;
3029 // Given pts, switch playback to the corresponding part.
3030 // Return offset within that part.
3031 static double timeline_set_from_time(struct MPContext
*mpctx
, double pts
,
3036 for (int i
= 0; i
< mpctx
->num_timeline_parts
; i
++) {
3037 struct timeline_part
*p
= mpctx
->timeline
+ i
;
3038 if (pts
< (p
+1)->start
) {
3039 *need_reset
= timeline_set_part(mpctx
, i
);
3040 return pts
- p
->start
+ p
->source_start
;
3047 // return -1 if seek failed (non-seekable stream?), 0 otherwise
3048 static int seek(MPContext
*mpctx
, struct seek_params seek
)
3050 struct MPOpts
*opts
= &mpctx
->opts
;
3052 current_module
= "seek";
3053 if (mpctx
->stop_play
== AT_END_OF_FILE
)
3054 mpctx
->stop_play
= KEEP_PLAYING
;
3055 bool hr_seek
= mpctx
->demuxer
->accurate_seek
&& opts
->correct_pts
;
3056 hr_seek
&= seek
.exact
>= 0 && seek
.type
!= MPSEEK_FACTOR
;
3057 hr_seek
&= opts
->hr_seek
== 0 && seek
.type
== MPSEEK_ABSOLUTE
3058 || opts
->hr_seek
> 0 || seek
.exact
> 0;
3059 if (seek
.type
== MPSEEK_FACTOR
3060 || seek
.type
== MPSEEK_ABSOLUTE
3061 && seek
.amount
< mpctx
->last_chapter_pts
3063 mpctx
->last_chapter_seek
= -1;
3064 if (mpctx
->timeline
&& seek
.type
== MPSEEK_FACTOR
) {
3065 seek
.amount
*= mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
3066 seek
.type
= MPSEEK_ABSOLUTE
;
3068 if ((mpctx
->demuxer
->accurate_seek
|| mpctx
->timeline
)
3069 && seek
.type
== MPSEEK_RELATIVE
) {
3070 seek
.type
= MPSEEK_ABSOLUTE
;
3071 seek
.direction
= seek
.amount
> 0 ? 1 : -1;
3072 seek
.amount
+= get_current_time(mpctx
);
3075 /* At least the liba52 decoder wants to read from the input stream
3076 * during initialization, so reinit must be done after the demux_seek()
3077 * call that clears possible stream EOF. */
3078 bool need_reset
= false;
3079 double demuxer_amount
= seek
.amount
;
3080 if (mpctx
->timeline
) {
3081 demuxer_amount
= timeline_set_from_time(mpctx
, seek
.amount
,
3083 if (demuxer_amount
== -1) {
3084 mpctx
->stop_play
= AT_END_OF_FILE
;
3085 // Clear audio from current position
3086 if (mpctx
->sh_audio
) {
3087 mpctx
->audio_out
->reset();
3088 mpctx
->sh_audio
->a_buffer_len
= 0;
3089 mpctx
->sh_audio
->a_out_buffer_len
= 0;
3094 int demuxer_style
= 0;
3095 switch (seek
.type
) {
3097 demuxer_style
|= SEEK_FACTOR
; // fallthrough
3098 case MPSEEK_ABSOLUTE
:
3099 demuxer_style
|= SEEK_ABSOLUTE
;
3101 if (hr_seek
|| seek
.direction
< 0)
3102 demuxer_style
|= SEEK_BACKWARD
;
3103 else if (seek
.direction
> 0)
3104 demuxer_style
|= SEEK_FORWARD
;
3106 int seekresult
= demux_seek(mpctx
->demuxer
, demuxer_amount
, audio_delay
,
3109 reinit_decoders(mpctx
);
3110 if (seekresult
== 0)
3115 /* Use the target time as "current position" for further relative
3116 * seeks etc until a new video frame has been decoded */
3117 if (seek
.type
== MPSEEK_ABSOLUTE
)
3118 mpctx
->video_pts
= seek
.amount
;
3121 mpctx
->hrseek_active
= true;
3122 mpctx
->hrseek_framedrop
= true;
3123 mpctx
->hrseek_pts
= seek
.amount
;
3126 mpctx
->start_timestamp
= GetTimerMS();
3131 void queue_seek(struct MPContext
*mpctx
, enum seek_type type
, double amount
,
3134 struct seek_params
*seek
= &mpctx
->seek
;
3136 case MPSEEK_RELATIVE
:
3137 if (seek
->type
== MPSEEK_FACTOR
)
3138 return; // Well... not common enough to bother doing better
3139 seek
->amount
+= amount
;
3140 seek
->exact
= FFMAX(seek
->exact
, exact
);
3141 if (seek
->type
== MPSEEK_NONE
)
3142 seek
->exact
= exact
;
3143 if (seek
->type
== MPSEEK_ABSOLUTE
)
3145 if (seek
->amount
== 0) {
3146 *seek
= (struct seek_params
){0};
3149 seek
->type
= MPSEEK_RELATIVE
;
3151 case MPSEEK_ABSOLUTE
:
3153 *seek
= (struct seek_params
) {
3160 *seek
= (struct seek_params
){0};
3167 double get_time_length(struct MPContext
*mpctx
)
3169 if (mpctx
->timeline
)
3170 return mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
3172 struct demuxer
*demuxer
= mpctx
->demuxer
;
3173 double get_time_ans
;
3174 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
3175 if (demux_control(demuxer
, DEMUXER_CTRL_GET_TIME_LENGTH
,
3176 (void *) &get_time_ans
) > 0)
3177 return get_time_ans
;
3179 struct sh_video
*sh_video
= mpctx
->d_video
->sh
;
3180 struct sh_audio
*sh_audio
= mpctx
->d_audio
->sh
;
3181 if (sh_video
&& sh_video
->i_bps
&& sh_audio
&& sh_audio
->i_bps
)
3182 return (double) (demuxer
->movi_end
- demuxer
->movi_start
) /
3183 (sh_video
->i_bps
+ sh_audio
->i_bps
);
3184 if (sh_video
&& sh_video
->i_bps
)
3185 return (double) (demuxer
->movi_end
- demuxer
->movi_start
) /
3187 if (sh_audio
&& sh_audio
->i_bps
)
3188 return (double) (demuxer
->movi_end
- demuxer
->movi_start
) /
3193 /* If there are timestamps from stream level then use those (for example
3194 * DVDs can have consistent times there while the MPEG-level timestamps
3196 double get_current_time(struct MPContext
*mpctx
)
3198 struct demuxer
*demuxer
= mpctx
->demuxer
;
3199 if (demuxer
->stream_pts
!= MP_NOPTS_VALUE
)
3200 return demuxer
->stream_pts
;
3201 struct sh_video
*sh_video
= demuxer
->video
->sh
;
3203 return mpctx
->video_pts
;
3204 return playing_audio_pts(mpctx
);
3207 int get_percent_pos(struct MPContext
*mpctx
)
3209 struct demuxer
*demuxer
= mpctx
->demuxer
;
3211 if (mpctx
->timeline
)
3212 ans
= get_current_time(mpctx
) * 100 /
3213 mpctx
->timeline
[mpctx
->num_timeline_parts
].start
;
3214 else if (demux_control(demuxer
, DEMUXER_CTRL_GET_PERCENT_POS
, &ans
) > 0)
3217 int len
= (demuxer
->movi_end
- demuxer
->movi_start
) / 100;
3218 off_t pos
= demuxer
->filepos
> 0 ?
3219 demuxer
->filepos
: stream_tell(demuxer
->stream
);
3221 ans
= (pos
- demuxer
->movi_start
) / len
;
3232 // -2 is no chapters, -1 is before first chapter
3233 int get_current_chapter(struct MPContext
*mpctx
)
3235 double current_pts
= get_current_time(mpctx
);
3236 if (!mpctx
->chapters
)
3237 return FFMAX(mpctx
->last_chapter_seek
,
3238 demuxer_get_current_chapter(mpctx
->demuxer
, current_pts
));
3241 for (i
= 1; i
< mpctx
->num_chapters
; i
++)
3242 if (current_pts
< mpctx
->chapters
[i
].start
)
3244 return FFMAX(mpctx
->last_chapter_seek
, i
- 1);
3247 // currently returns a string allocated with malloc, not talloc
3248 char *chapter_display_name(struct MPContext
*mpctx
, int chapter
)
3250 if (!mpctx
->chapters
|| !mpctx
->sh_video
)
3251 return demuxer_chapter_display_name(mpctx
->demuxer
, chapter
);
3252 return strdup(mpctx
->chapters
[chapter
].name
);
3255 int seek_chapter(struct MPContext
*mpctx
, int chapter
, double *seek_pts
,
3256 char **chapter_name
)
3258 mpctx
->last_chapter_seek
= -1;
3259 if (!mpctx
->chapters
|| !mpctx
->sh_video
) {
3260 int res
= demuxer_seek_chapter(mpctx
->demuxer
, chapter
, seek_pts
,
3263 if (*seek_pts
== -1)
3266 mpctx
->last_chapter_seek
= res
;
3267 mpctx
->last_chapter_pts
= *seek_pts
;
3273 if (chapter
>= mpctx
->num_chapters
)
3277 *seek_pts
= mpctx
->chapters
[chapter
].start
;
3278 mpctx
->last_chapter_seek
= chapter
;
3279 mpctx
->last_chapter_pts
= *seek_pts
;
3281 *chapter_name
= talloc_strdup(NULL
, mpctx
->chapters
[chapter
].name
);
3286 static void run_playloop(struct MPContext
*mpctx
)
3288 struct MPOpts
*opts
= &mpctx
->opts
;
3289 float aq_sleep_time
= 0;
3291 if (opts
->chapterrange
[1] > 0) {
3292 int cur_chapter
= get_current_chapter(mpctx
);
3293 if (cur_chapter
!=-1 && cur_chapter
+ 1 > opts
->chapterrange
[1])
3294 mpctx
->stop_play
= PT_NEXT_ENTRY
;
3297 if (!mpctx
->sh_audio
&& mpctx
->d_audio
->sh
) {
3298 mpctx
->sh_audio
= mpctx
->d_audio
->sh
;
3299 mpctx
->sh_audio
->ds
= mpctx
->d_audio
;
3300 reinit_audio_chain(mpctx
);
3303 /*========================== PLAY AUDIO ============================*/
3305 if (mpctx
->sh_audio
&& !mpctx
->paused
3306 && (!mpctx
->restart_playback
|| !mpctx
->sh_video
))
3307 if (!fill_audio_out_buffers(mpctx
))
3308 // at eof, all audio at least written to ao
3309 if (!mpctx
->sh_video
)
3310 mpctx
->stop_play
= AT_END_OF_FILE
;
3313 if (!mpctx
->sh_video
) {
3314 mpctx
->restart_playback
= false;
3315 if (mpctx
->step_frames
) {
3316 mpctx
->step_frames
= 0;
3317 pause_player(mpctx
);
3319 // handle audio-only case:
3321 // sh_audio can be NULL due to video stream switching
3322 // TODO: handle this better
3323 if (mpctx
->sh_audio
)
3324 a_pos
= playing_audio_pts(mpctx
);
3326 print_status(mpctx
, a_pos
, false);
3328 if (end_at
.type
== END_AT_TIME
&& end_at
.pos
< a_pos
)
3329 mpctx
->stop_play
= PT_NEXT_ENTRY
;
3330 update_subtitles(mpctx
, a_pos
, mpctx
->video_offset
, false);
3331 update_osd_msg(mpctx
);
3334 /*========================== PLAY VIDEO ============================*/
3336 vo_pts
= mpctx
->sh_video
->timer
* 90000.0;
3337 vo_fps
= mpctx
->sh_video
->fps
;
3339 bool blit_frame
= mpctx
->video_out
->frame_loaded
;
3340 if (!blit_frame
|| mpctx
->hrseek_active
) {
3341 double frame_time
= update_video(mpctx
);
3342 blit_frame
= mpctx
->video_out
->frame_loaded
;
3343 blit_frame
&= !mpctx
->hrseek_active
;
3344 mp_dbg(MSGT_AVSYNC
, MSGL_DBG2
, "*** ftime=%5.3f ***\n", frame_time
);
3345 if (mpctx
->sh_video
->vf_initialized
< 0) {
3346 mp_tmsg(MSGT_CPLAYER
, MSGL_FATAL
,
3347 "\nFATAL: Could not initialize video filters (-vf) "
3348 "or video output (-vo).\n");
3349 mpctx
->stop_play
= PT_NEXT_ENTRY
;
3353 mpctx
->stop_play
= AT_END_OF_FILE
;
3354 else if (!mpctx
->restart_playback
) {
3355 mpctx
->time_frame
+= frame_time
/ opts
->playback_speed
;
3356 adjust_sync(mpctx
, frame_time
);
3359 if (mpctx
->timeline
) {
3360 struct timeline_part
*next
=
3361 mpctx
->timeline
+ mpctx
->timeline_part
+ 1;
3362 if (mpctx
->sh_video
->pts
>= next
->start
3363 || mpctx
->stop_play
== AT_END_OF_FILE
3364 && mpctx
->timeline_part
+ 1 < mpctx
->num_timeline_parts
) {
3365 seek(mpctx
, (struct seek_params
){ .type
= MPSEEK_ABSOLUTE
,
3366 .amount
= next
->start
});
3371 // ==========================================================================
3373 current_module
= "vo_check_events";
3374 vo_check_events(mpctx
->video_out
);
3377 if (stop_xscreensaver
) {
3378 current_module
= "stop_xscreensaver";
3379 xscreensaver_heartbeat(mpctx
->x11_state
);
3382 if (heartbeat_cmd
) {
3383 static unsigned last_heartbeat
;
3384 unsigned now
= GetTimerMS();
3385 if (now
- last_heartbeat
> 30000) {
3386 last_heartbeat
= now
;
3387 system(heartbeat_cmd
);
3391 bool frame_time_remaining
= sleep_until_near_frame(mpctx
,
3395 //====================== FLIP PAGE (VIDEO BLT): =========================
3397 current_module
= "flip_page";
3398 if (!frame_time_remaining
&& blit_frame
) {
3399 struct sh_video
*sh_video
= mpctx
->sh_video
;
3400 mpctx
->video_pts
= sh_video
->pts
;
3401 update_subtitles(mpctx
, sh_video
->pts
, mpctx
->video_offset
, false);
3402 update_teletext(sh_video
, mpctx
->demuxer
, 0);
3403 update_osd_msg(mpctx
);
3404 struct vf_instance
*vf
= sh_video
->vfilter
;
3405 vf
->control(vf
, VFCTRL_DRAW_EOSD
, mpctx
->osd
);
3406 vf
->control(vf
, VFCTRL_DRAW_OSD
, mpctx
->osd
);
3409 mpctx
->time_frame
-= mpctx
->video_out
->flip_queue_offset
;
3410 aq_sleep_time
+= mpctx
->time_frame
;
3411 // flag 256 means: libvo driver does its timing (dvb card)
3412 if (mpctx
->time_frame
> 0.001
3413 && !(mpctx
->sh_video
->output_flags
& VFCAP_TIMER
))
3414 mpctx
->time_frame
= timing_sleep(mpctx
, mpctx
->time_frame
);
3415 mpctx
->time_frame
+= mpctx
->video_out
->flip_queue_offset
;
3417 unsigned int t2
= GetTimer();
3418 /* Playing with playback speed it's possible to get pathological
3419 * cases with mpctx->time_frame negative enough to cause an
3420 * overflow in pts_us calculation, thus the FFMAX. */
3421 double time_frame
= FFMAX(mpctx
->time_frame
, -1);
3422 unsigned int pts_us
= mpctx
->last_time
+ time_frame
* 1e6
;
3424 double pts2
= mpctx
->video_out
->next_pts2
;
3425 if (pts2
!= MP_NOPTS_VALUE
&& opts
->correct_pts
3426 && !mpctx
->restart_playback
) {
3427 // expected A/V sync correction is ignored
3428 double diff
= (pts2
- mpctx
->video_pts
);
3429 diff
/= opts
->playback_speed
;
3430 if (mpctx
->time_frame
< 0)
3431 diff
+= mpctx
->time_frame
;
3436 duration
= diff
* 1e6
;
3438 vo_flip_page(mpctx
->video_out
, pts_us
| 1, duration
);
3440 mpctx
->last_vo_flip_duration
= (GetTimer() - t2
) * 0.000001;
3441 vout_time_usage
+= mpctx
->last_vo_flip_duration
;
3442 if (mpctx
->video_out
->driver
->flip_page_timed
) {
3443 // No need to adjust sync based on flip speed
3444 mpctx
->last_vo_flip_duration
= 0;
3445 // For print_status - VO call finishing early is OK for sync
3446 mpctx
->time_frame
-= get_relative_time(mpctx
);
3448 if (mpctx
->restart_playback
) {
3449 mpctx
->syncing_audio
= true;
3450 if (mpctx
->sh_audio
&& !mpctx
->paused
)
3451 fill_audio_out_buffers(mpctx
);
3452 mpctx
->restart_playback
= false;
3453 mpctx
->time_frame
= 0;
3454 get_relative_time(mpctx
);
3456 print_status(mpctx
, MP_NOPTS_VALUE
, true);
3458 print_status(mpctx
, MP_NOPTS_VALUE
, false);
3460 //============================ Auto QUALITY ============================
3462 /*Output quality adjustments:*/
3463 if (opts
->auto_quality
> 0) {
3464 current_module
= "autoq";
3465 if (output_quality
< opts
->auto_quality
&& aq_sleep_time
> 0)
3468 if (output_quality
>1 && aq_sleep_time
<0)
3470 else if (output_quality
>0 && aq_sleep_time
<-0.050f
) // 50ms
3472 set_video_quality(mpctx
->sh_video
, output_quality
);
3475 if (!frame_time_remaining
&& blit_frame
) {
3476 if (play_n_frames
>= 0) {
3478 if (play_n_frames
<= 0)
3479 mpctx
->stop_play
= PT_NEXT_ENTRY
;
3481 if (mpctx
->step_frames
> 0) {
3482 mpctx
->step_frames
--;
3483 if (mpctx
->step_frames
== 0)
3484 pause_player(mpctx
);
3488 // FIXME: add size based support for -endpos
3489 if (end_at
.type
== END_AT_TIME
&&
3490 !frame_time_remaining
&& end_at
.pos
<= mpctx
->sh_video
->pts
)
3491 mpctx
->stop_play
= PT_NEXT_ENTRY
;
3493 } // end if(mpctx->sh_video)
3495 #ifdef CONFIG_DVDNAV
3496 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
3498 mp_dvdnav_get_highlight(mpctx
->stream
, &hl
);
3499 if (!vo_spudec
|| !spudec_apply_palette_crop(vo_spudec
, hl
.palette
, hl
.sx
, hl
.sy
, hl
.ex
, hl
.ey
)) {
3500 osd_set_nav_box(hl
.sx
, hl
.sy
, hl
.ex
, hl
.ey
);
3501 vo_osd_changed(OSDTYPE_DVDNAV
);
3503 osd_set_nav_box(0, 0, 0, 0);
3504 vo_osd_changed(OSDTYPE_DVDNAV
);
3505 vo_osd_changed(OSDTYPE_SPU
);
3508 if (mp_dvdnav_stream_has_changed(mpctx
->stream
)) {
3510 if (mpctx
->sh_video
&&
3511 stream_control(mpctx
->demuxer
->stream
,
3512 STREAM_CTRL_GET_ASPECT_RATIO
, &ar
)
3513 != STREAM_UNSUPPORTED
)
3514 mpctx
->sh_video
->stream_aspect
= ar
;
3519 //================= Keyboard events, SEEKing ====================
3521 current_module
= "key_events";
3525 while ((cmd
= mp_input_get_cmd(mpctx
->input
, 0, 1)) != NULL
) {
3526 /* Allow running consecutive seek commands to combine them,
3527 * but execute the seek before running other commands.
3528 * If the user seeks continuously (keeps arrow key down)
3529 * try to finish showing a frame from one location before doing
3530 * another seek (which could lead to unchanging display). */
3531 if (mpctx
->seek
.type
&& cmd
->id
!= MP_CMD_SEEK
3532 || mpctx
->restart_playback
&& cmd
->id
== MP_CMD_SEEK
3533 && GetTimerMS() - mpctx
->start_timestamp
< 300)
3535 cmd
= mp_input_get_cmd(mpctx
->input
, 0, 0);
3536 run_command(mpctx
, cmd
);
3538 if (mpctx
->stop_play
)
3541 if (!mpctx
->paused
|| mpctx
->stop_play
|| mpctx
->seek
.type
3542 || mpctx
->restart_playback
)
3544 if (mpctx
->sh_video
) {
3545 update_osd_msg(mpctx
);
3546 int hack
= vo_osd_changed(0);
3547 vo_osd_changed(hack
);
3549 if (redraw_osd(mpctx
->sh_video
, mpctx
->osd
) < 0) {
3550 add_step_frame(mpctx
);
3560 if (step_sec
> 0 && !mpctx
->paused
) {
3561 mpctx
->osd_function
= OSD_FFW
;
3562 queue_seek(mpctx
, MPSEEK_RELATIVE
, step_sec
, 0);
3568 if (mpctx
->stop_play
==AT_END_OF_FILE
&& opts
->loop_times
>=0) {
3569 mp_msg(MSGT_CPLAYER
, MSGL_V
, "loop_times = %d\n", opts
->loop_times
);
3571 if (opts
->loop_times
>1)
3573 else if (opts
->loop_times
==1)
3574 opts
->loop_times
= -1;
3575 play_n_frames
= play_n_frames_mf
;
3576 mpctx
->stop_play
= 0;
3577 queue_seek(mpctx
, MPSEEK_ABSOLUTE
, 0, 0);
3580 if (mpctx
->seek
.type
) {
3581 seek(mpctx
, mpctx
->seek
);
3582 mpctx
->seek
= (struct seek_params
){0};
3587 static int find_ordered_chapter_sources(struct MPContext
*mpctx
,
3588 struct content_source
*sources
,
3590 unsigned char uid_map
[][16])
3592 int num_filenames
= 0;
3593 char **filenames
= NULL
;
3594 if (num_sources
> 1) {
3595 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "This file references data from "
3596 "other sources.\n");
3597 if (mpctx
->stream
->type
!= STREAMTYPE_FILE
) {
3598 mp_msg(MSGT_CPLAYER
, MSGL_WARN
, "Playback source is not a "
3599 "normal disk file. Will not search for related files.\n");
3601 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Will scan other files in the "
3602 "same directory to find referenced sources.\n");
3603 filenames
= find_files(mpctx
->demuxer
->filename
, ".mkv",
3608 int num_left
= num_sources
- 1;
3609 for (int i
= 0; i
< num_filenames
&& num_left
> 0; i
++) {
3610 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "Checking file %s\n",
3611 filename_recode(filenames
[i
]));
3613 struct stream
*s
= open_stream(filenames
[i
], &mpctx
->opts
, &format
);
3616 struct demuxer
*d
= demux_open(&mpctx
->opts
, s
, DEMUXER_TYPE_MATROSKA
,
3617 mpctx
->opts
.audio_id
,
3618 mpctx
->opts
.video_id
,
3619 mpctx
->opts
.sub_id
, filenames
[i
]);
3624 if (d
->file_format
== DEMUXER_TYPE_MATROSKA
) {
3625 for (int i
= 1; i
< num_sources
; i
++) {
3626 if (sources
[i
].demuxer
)
3628 if (!memcmp(uid_map
[i
], d
->matroska_data
.segment_uid
, 16)) {
3629 mp_msg(MSGT_CPLAYER
, MSGL_INFO
,"Match for source %d: %s\n",
3630 i
, filename_recode(d
->filename
));
3631 sources
[i
].stream
= s
;
3632 sources
[i
].demuxer
= d
;
3644 talloc_free(filenames
);
3646 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "Failed to find ordered chapter part!\n"
3647 "There will be parts MISSING from the video!\n");
3648 for (int i
= 1, j
= 1; i
< num_sources
; i
++)
3649 if (sources
[i
].demuxer
) {
3650 sources
[j
] = sources
[i
];
3651 memcpy(uid_map
[j
], uid_map
[i
], 16);
3655 return num_sources
- num_left
;
3658 static void build_ordered_chapter_timeline(struct MPContext
*mpctx
)
3660 struct MPOpts
*opts
= &mpctx
->opts
;
3662 if (!opts
->ordered_chapters
) {
3663 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "File uses ordered chapters, but "
3664 "you have disabled support for them. Ignoring.\n");
3668 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "File uses ordered chapters, will build "
3669 "edit timeline.\n");
3671 struct demuxer
*demuxer
= mpctx
->demuxer
;
3672 struct matroska_data
*m
= &demuxer
->matroska_data
;
3674 // +1 because sources/uid_map[0] is original file even if all chapters
3675 // actually use other sources and need separate entries
3676 struct content_source
*sources
= talloc_array_ptrtype(NULL
, sources
,
3677 m
->num_ordered_chapters
+1);
3678 sources
[0].stream
= mpctx
->stream
;
3679 sources
[0].demuxer
= mpctx
->demuxer
;
3680 unsigned char uid_map
[m
->num_ordered_chapters
+1][16];
3681 int num_sources
= 1;
3682 memcpy(uid_map
[0], m
->segment_uid
, 16);
3684 for (int i
= 0; i
< m
->num_ordered_chapters
; i
++) {
3685 struct matroska_chapter
*c
= m
->ordered_chapters
+ i
;
3686 if (!c
->has_segment_uid
)
3687 memcpy(c
->segment_uid
, m
->segment_uid
, 16);
3689 for (int j
= 0; j
< num_sources
; j
++)
3690 if (!memcmp(c
->segment_uid
, uid_map
[j
], 16))
3692 memcpy(uid_map
[num_sources
], c
->segment_uid
, 16);
3693 sources
[num_sources
] = (struct content_source
){};
3699 num_sources
= find_ordered_chapter_sources(mpctx
, sources
, num_sources
,
3703 // +1 for terminating chapter with start time marking end of last real one
3704 struct timeline_part
*timeline
= talloc_array_ptrtype(NULL
, timeline
,
3705 m
->num_ordered_chapters
+ 1);
3706 struct chapter
*chapters
= talloc_array_ptrtype(NULL
, chapters
,
3707 m
->num_ordered_chapters
);
3708 uint64_t starttime
= 0;
3709 uint64_t missing_time
= 0;
3711 int num_chapters
= 0;
3712 uint64_t prev_part_offset
= 0;
3713 for (int i
= 0; i
< m
->num_ordered_chapters
; i
++) {
3714 struct matroska_chapter
*c
= m
->ordered_chapters
+ i
;
3717 for (j
= 0; j
< num_sources
; j
++) {
3718 if (!memcmp(c
->segment_uid
, uid_map
[j
], 16))
3721 missing_time
+= c
->end
- c
->start
;
3724 /* Only add a separate part if the time or file actually changes.
3725 * Matroska files have chapter divisions that are redundant from
3726 * timeline point of view because the same chapter structure is used
3727 * both to specify the timeline and for normal chapter information.
3728 * Removing a missing inserted external chapter can also cause this.
3729 * We allow for a configurable fudge factor because of files which
3730 * specify chapter end times that are one frame too early;
3731 * we don't want to try seeking over a one frame gap. */
3732 int64_t join_diff
= c
->start
- starttime
- prev_part_offset
;
3734 || FFABS(join_diff
) > opts
->chapter_merge_threshold
* 1000000
3735 || sources
+ j
!= timeline
[part_count
- 1].source
) {
3736 timeline
[part_count
].source
= sources
+ j
;
3737 timeline
[part_count
].start
= starttime
/ 1e9
;
3738 timeline
[part_count
].source_start
= c
->start
/ 1e9
;
3739 prev_part_offset
= c
->start
- starttime
;
3741 } else if (part_count
> 0 && join_diff
) {
3742 /* Chapter was merged at an inexact boundary;
3743 * adjust timestamps to match. */
3744 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Merging timeline part %d with "
3745 "offset %d ms.\n", i
, (int) join_diff
);
3746 starttime
+= join_diff
;
3748 chapters
[num_chapters
].start
= starttime
/ 1e9
;
3749 chapters
[num_chapters
].name
= talloc_strdup(chapters
, c
->name
);
3750 starttime
+= c
->end
- c
->start
;
3753 timeline
[part_count
].start
= starttime
/ 1e9
;
3756 // None of the parts come from the file itself???
3757 talloc_free(sources
);
3758 talloc_free(timeline
);
3759 talloc_free(chapters
);
3763 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Timeline contains %d parts from %d "
3764 "sources. Total length %.3f seconds.\n", part_count
, num_sources
,
3765 timeline
[part_count
].start
);
3767 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "There are %.3f seconds missing "
3768 "from the timeline!\n", missing_time
/ 1e9
);
3769 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Source files:\n");
3770 for (int i
= 0; i
< num_sources
; i
++)
3771 mp_msg(MSGT_CPLAYER
, MSGL_V
, "%d: %s\n", i
,
3772 filename_recode(sources
[i
].demuxer
->filename
));
3773 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Timeline parts: (number, start, "
3774 "source_start, source):\n");
3775 for (int i
= 0; i
< part_count
; i
++) {
3776 struct timeline_part
*p
= timeline
+ i
;
3777 mp_msg(MSGT_CPLAYER
, MSGL_V
, "%3d %9.3f %9.3f %3td\n", i
, p
->start
,
3778 p
->source_start
, p
->source
- sources
);
3780 mp_msg(MSGT_CPLAYER
, MSGL_V
, "END %9.3f\n", timeline
[part_count
].start
);
3781 mpctx
->sources
= sources
;
3782 mpctx
->num_sources
= num_sources
;
3783 mpctx
->timeline
= timeline
;
3784 mpctx
->num_timeline_parts
= part_count
;
3785 mpctx
->num_chapters
= num_chapters
;
3786 mpctx
->chapters
= chapters
;
3788 mpctx
->timeline_part
= 0;
3789 mpctx
->demuxer
= timeline
[0].source
->demuxer
;
3793 static int read_keys(void *ctx
, int fd
)
3796 return mplayer_get_key(ctx
, 0);
3799 static bool attachment_is_font(struct demux_attachment
*att
)
3801 if (!att
->name
|| !att
->type
|| !att
->data
|| !att
->data_size
)
3803 // match against MIME types
3804 if (strcmp(att
->type
, "application/x-truetype-font") == 0
3805 || strcmp(att
->type
, "application/x-font") == 0)
3807 // fallback: match against file extension
3808 if (strlen(att
->name
) > 4) {
3809 char *ext
= att
->name
+ strlen(att
->name
) - 4;
3810 if (strcasecmp(ext
, ".ttf") == 0 || strcasecmp(ext
, ".ttc") == 0
3811 || strcasecmp(ext
, ".otf") == 0)
3817 static int select_audio(demuxer_t
*demuxer
, int audio_id
, char *audio_lang
)
3820 audio_id
= demuxer_audio_track_by_lang_and_default(demuxer
, audio_lang
);
3821 if (audio_id
!= -1) // -1 (automatic) is the default behaviour of demuxers
3822 demuxer_switch_audio(demuxer
, audio_id
);
3823 if (audio_id
== -2) { // some demuxers don't yet know how to switch to no sound
3824 demuxer
->audio
->id
= -2;
3825 demuxer
->audio
->sh
= NULL
;
3827 return demuxer
->audio
->id
;
3830 static void print_version(const char* name
)
3832 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, MP_TITLE
, name
);
3834 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
3835 GetCpuCaps(&gCpuCaps
);
3837 mp_msg(MSGT_CPLAYER
, MSGL_V
,
3838 "CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNowExt: %d SSE: %d SSE2: %d SSSE3: %d\n",
3839 gCpuCaps
.hasMMX
, gCpuCaps
.hasMMX2
,
3840 gCpuCaps
.has3DNow
, gCpuCaps
.has3DNowExt
,
3841 gCpuCaps
.hasSSE
, gCpuCaps
.hasSSE2
, gCpuCaps
.hasSSSE3
);
3842 #if CONFIG_RUNTIME_CPUDETECT
3843 mp_tmsg(MSGT_CPLAYER
,MSGL_V
, "Compiled with runtime CPU detection.\n");
3845 mp_tmsg(MSGT_CPLAYER
,MSGL_V
, "Compiled for x86 CPU with extensions:");
3847 mp_msg(MSGT_CPLAYER
,MSGL_V
," MMX");
3849 mp_msg(MSGT_CPLAYER
,MSGL_V
," MMX2");
3851 mp_msg(MSGT_CPLAYER
,MSGL_V
," 3DNow");
3852 if (HAVE_AMD3DNOWEXT
)
3853 mp_msg(MSGT_CPLAYER
,MSGL_V
," 3DNowExt");
3855 mp_msg(MSGT_CPLAYER
,MSGL_V
," SSE");
3857 mp_msg(MSGT_CPLAYER
,MSGL_V
," SSE2");
3859 mp_msg(MSGT_CPLAYER
,MSGL_V
," SSSE3");
3861 mp_msg(MSGT_CPLAYER
,MSGL_V
," CMOV");
3862 mp_msg(MSGT_CPLAYER
,MSGL_V
,"\n");
3863 #endif /* CONFIG_RUNTIME_CPUDETECT */
3864 #endif /* ARCH_X86 */
3867 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
3868 * file for some tools to link against. */
3869 #ifndef DISABLE_MAIN
3870 int main(int argc
,char* argv
[]){
3871 if (argc
> 1 && !strcmp(argv
[1], "-leak-report"))
3872 talloc_enable_leak_report();
3878 /* Flag indicating whether MPlayer should exit without playing anything. */
3882 struct MPContext
*mpctx
= &(struct MPContext
){
3883 .osd_function
= OSD_PLAY
,
3884 .begin_skip
= MP_NOPTS_VALUE
,
3885 .play_tree_step
= 1,
3886 .global_sub_pos
= -1,
3887 .set_of_sub_pos
= -1,
3888 .file_format
= DEMUXER_TYPE_UNKNOWN
,
3893 srand(GetTimerMS());
3896 set_av_log_callback();
3899 mpctx
->x11_state
= vo_x11_init_state();
3901 struct MPOpts
*opts
= &mpctx
->opts
;
3902 set_default_mplayer_options(opts
);
3903 // Create the config context and register the options
3904 mpctx
->mconfig
= m_config_new(opts
, cfg_include
);
3905 m_config_register_options(mpctx
->mconfig
,mplayer_opts
);
3906 m_config_register_options(mpctx
->mconfig
, common_opts
);
3907 mp_input_register_options(mpctx
->mconfig
);
3909 // Preparse the command line
3910 m_config_preparse_command_line(mpctx
->mconfig
,argc
,argv
);
3912 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
3917 stream_tv_defaults
.immediate
= 1;
3920 parse_cfgfiles(mpctx
, mpctx
->mconfig
);
3922 mpctx
->playtree
= m_config_parse_mp_command_line(mpctx
->mconfig
, argc
, argv
);
3923 if(mpctx
->playtree
== NULL
)
3926 mpctx
->playtree
= play_tree_cleanup(mpctx
->playtree
);
3927 if(mpctx
->playtree
) {
3928 mpctx
->playtree_iter
= play_tree_iter_new(mpctx
->playtree
,mpctx
->mconfig
);
3929 if(mpctx
->playtree_iter
) {
3930 if(play_tree_iter_step(mpctx
->playtree_iter
,0,0) != PLAY_TREE_ITER_ENTRY
) {
3931 play_tree_iter_free(mpctx
->playtree_iter
);
3932 mpctx
->playtree_iter
= NULL
;
3934 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
,1);
3938 mpctx
->key_fifo
= mp_fifo_create(opts
);
3940 print_version("MPlayer");
3942 #if defined(__MINGW32__) || defined(__CYGWIN__)
3944 HMODULE kernel32
= GetModuleHandle("Kernel32.dll");
3945 BOOL
WINAPI (*setDEP
)(DWORD
) = NULL
;
3946 BOOL
WINAPI (*setDllDir
)(LPCTSTR
) = NULL
;
3948 setDEP
= GetProcAddress(kernel32
, "SetProcessDEPPolicy");
3949 setDllDir
= GetProcAddress(kernel32
, "SetDllDirectoryA");
3951 if (setDEP
) setDEP(3);
3952 if (setDllDir
) setDllDir("");
3954 // stop Windows from showing all kinds of annoying error dialogs
3955 SetErrorMode(0x8003);
3956 // request 1ms timer resolution
3960 #ifdef CONFIG_PRIORITY
3965 set_codec_path(codec_path
);
3967 if(opts
->video_driver_list
&& strcmp(opts
->video_driver_list
[0],"help")==0){
3972 if(opts
->audio_driver_list
&& strcmp(opts
->audio_driver_list
[0],"help")==0){
3977 /* Check codecs.conf. */
3978 if(!codecs_file
|| !parse_codec_cfg(codecs_file
)){
3979 if(!parse_codec_cfg(mem_ptr
=get_path("codecs.conf"))){
3980 if(!parse_codec_cfg(MPLAYER_CONFDIR
"/codecs.conf")){
3981 if(!parse_codec_cfg(NULL
)){
3982 exit_player_with_rc(mpctx
, EXIT_NONE
, 0);
3984 mp_tmsg(MSGT_CPLAYER
,MSGL_V
,"Using built-in default codecs.conf.\n");
3987 free( mem_ptr
); // release the buffer created by get_path()
3990 if(audio_codec_list
&& strcmp(audio_codec_list
[0],"help")==0){
3991 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "Available audio codecs:\n");
3992 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_CODECS\n");
3994 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
3997 if(video_codec_list
&& strcmp(video_codec_list
[0],"help")==0){
3998 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "Available video codecs:\n");
3999 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VIDEO_CODECS\n");
4001 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
4004 if(video_fm_list
&& strcmp(video_fm_list
[0],"help")==0){
4006 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
4009 if(audio_fm_list
&& strcmp(audio_fm_list
[0],"help")==0){
4011 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
4014 if(af_cfg
.list
&& strcmp(af_cfg
.list
[0],"help")==0){
4020 if(vo_fstype_list
&& strcmp(vo_fstype_list
[0],"help")==0){
4022 mp_msg(MSGT_FIXME
, MSGL_FIXME
, "\n");
4026 if((opts
->demuxer_name
&& strcmp(opts
->demuxer_name
,"help")==0) ||
4027 (opts
->audio_demuxer_name
&& strcmp(opts
->audio_demuxer_name
,"help")==0) ||
4028 (opts
->sub_demuxer_name
&& strcmp(opts
->sub_demuxer_name
,"help")==0)){
4030 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "\n");
4033 if (opts
->list_properties
) {
4034 property_print_help();
4039 exit_player(mpctx
, EXIT_NONE
);
4041 if (!mpctx
->filename
&& !opts
->player_idle_mode
) {
4042 // no file/vcd/dvd -> show HELP:
4043 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "%s", mp_gtext(help_text
));
4044 exit_player_with_rc(mpctx
, EXIT_NONE
, 0);
4047 /* Display what configure line was used */
4048 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Configuration: " CONFIGURATION
"\n");
4050 // Many users forget to include command line in bugreports...
4051 if( mp_msg_test(MSGT_CPLAYER
,MSGL_V
) ){
4052 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "CommandLine:");
4053 for(i
=1;i
<argc
;i
++)mp_msg(MSGT_CPLAYER
, MSGL_INFO
," '%s'",argv
[i
]);
4054 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, "\n");
4057 //------ load global data first ------
4059 mpctx
->osd
= osd_create();
4062 #ifdef CONFIG_FREETYPE
4065 #ifdef CONFIG_FONTCONFIG
4066 if(font_fontconfig
<= 0)
4069 #ifdef CONFIG_BITMAP_FONT
4071 vo_font
=read_font_desc(font_name
,font_factor
,verbose
>1);
4072 if(!vo_font
) mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Cannot load bitmap font: %s\n",
4073 filename_recode(font_name
));
4076 vo_font
=read_font_desc( mem_ptr
=get_path("font/font.desc"),font_factor
,verbose
>1);
4077 free(mem_ptr
); // release the buffer created by get_path()
4079 vo_font
=read_font_desc(MPLAYER_DATADIR
"/font/font.desc",font_factor
,verbose
>1);
4082 mpctx
->osd
->sub_font
= read_font_desc(sub_font_name
, font_factor
, verbose
>1);
4084 mpctx
->osd
->sub_font
= vo_font
;
4086 #ifdef CONFIG_FONTCONFIG
4091 ass_library
= mp_ass_init();
4097 char *rtc_device
= opts
->rtc_device
;
4098 // seteuid(0); /* Can't hurt to try to get root here */
4099 if ((rtc_fd
= open(rtc_device
? rtc_device
: "/dev/rtc", O_RDONLY
)) < 0)
4100 mp_tmsg(MSGT_CPLAYER
, MSGL_WARN
, "Failed to open %s: %s (it should be readable by the user.)\n",
4101 rtc_device
? rtc_device
: "/dev/rtc", strerror(errno
));
4103 unsigned long irqp
= 1024; /* 512 seemed OK. 128 is jerky. */
4105 if (ioctl(rtc_fd
, RTC_IRQP_SET
, irqp
) < 0) {
4106 mp_tmsg(MSGT_CPLAYER
, MSGL_WARN
, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp
, strerror(errno
));
4107 mp_tmsg(MSGT_CPLAYER
, MSGL_HINT
, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp
);
4110 } else if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
4111 /* variable only by the root */
4112 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno
));
4116 mp_tmsg(MSGT_CPLAYER
, MSGL_V
, "Using Linux hardware RTC timing (%ldHz).\n", irqp
);
4120 #endif /* HAVE_RTC */
4121 mp_msg(MSGT_CPLAYER
, MSGL_V
, "Using %s timing\n",
4122 opts
->softsleep
? "software" : timer_name
);
4125 load_termcap(NULL
); // load key-codes
4128 // ========== Init keyboard FIFO (connection to libvo) ============
4130 // Init input system
4131 current_module
= "init_input";
4132 mpctx
->input
= mp_input_init(&opts
->input
);
4133 mp_input_add_key_fd(mpctx
->input
, -1,0,mplayer_get_key
,NULL
, mpctx
->key_fifo
);
4135 mp_input_add_cmd_fd(mpctx
->input
, 0,USE_SELECT
,MP_INPUT_SLAVE_CMD_FUNC
,NULL
);
4136 else if (opts
->consolecontrols
)
4137 mp_input_add_key_fd(mpctx
->input
, 0, 1, read_keys
, NULL
, mpctx
->key_fifo
);
4138 // Set the libstream interrupt callback
4139 stream_set_interrupt_callback(mp_input_check_interrupt
, mpctx
->input
);
4143 if(menu_cfg
&& menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
, menu_cfg
))
4144 mp_tmsg(MSGT_CPLAYER
, MSGL_V
, "Menu initialized: %s\n", menu_cfg
);
4146 menu_cfg
= get_path("menu.conf");
4147 if(menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
, menu_cfg
))
4148 mp_tmsg(MSGT_CPLAYER
, MSGL_V
, "Menu initialized: %s\n", menu_cfg
);
4150 if(menu_init(mpctx
, mpctx
->mconfig
, mpctx
->input
,
4151 MPLAYER_CONFDIR
"/menu.conf"))
4152 mp_tmsg(MSGT_CPLAYER
, MSGL_V
, "Menu initialized: %s\n", MPLAYER_CONFDIR
"/menu.conf");
4154 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Menu init failed.\n");
4162 current_module
= NULL
;
4166 signal(SIGCHLD
,child_sighandler
);
4169 #ifdef CONFIG_CRASH_DEBUG
4170 prog_path
= argv
[0];
4172 //========= Catch terminate signals: ================
4173 // terminate requests:
4174 signal(SIGTERM
,exit_sighandler
); // kill
4175 signal(SIGHUP
,exit_sighandler
); // kill -HUP / xterm closed
4177 signal(SIGINT
,exit_sighandler
); // Interrupt from keyboard
4179 signal(SIGQUIT
,exit_sighandler
); // Quit from keyboard
4180 signal(SIGPIPE
,exit_sighandler
); // Some window managers cause this
4181 #ifdef CONFIG_SIGHANDLER
4183 signal(SIGBUS
,exit_sighandler
); // bus error
4184 signal(SIGSEGV
,exit_sighandler
); // segfault
4185 signal(SIGILL
,exit_sighandler
); // illegal instruction
4186 signal(SIGFPE
,exit_sighandler
); // floating point exc.
4187 signal(SIGABRT
,exit_sighandler
); // abort()
4188 #ifdef CONFIG_CRASH_DEBUG
4190 signal(SIGTRAP
,exit_sighandler
);
4194 // ******************* Now, let's see the per-file stuff ********************
4198 // init global sub numbers
4199 mpctx
->global_sub_size
= 0;
4200 memset(mpctx
->sub_counts
, 0, sizeof(mpctx
->sub_counts
));
4202 if (mpctx
->filename
) {
4203 load_per_protocol_config (mpctx
->mconfig
, mpctx
->filename
);
4204 load_per_extension_config (mpctx
->mconfig
, mpctx
->filename
);
4205 load_per_file_config (mpctx
->mconfig
, mpctx
->filename
);
4208 if (opts
->video_driver_list
)
4209 load_per_output_config (mpctx
->mconfig
, PROFILE_CFG_VO
, opts
->video_driver_list
[0]);
4210 if (opts
->audio_driver_list
)
4211 load_per_output_config (mpctx
->mconfig
, PROFILE_CFG_AO
, opts
->audio_driver_list
[0]);
4213 // We must enable getch2 here to be able to interrupt network connection
4215 if (opts
->consolecontrols
&& !slave_mode
) {
4216 if(mpctx
->initialized_flags
&INITIALIZED_GETCH2
)
4217 mp_tmsg(MSGT_CPLAYER
,MSGL_WARN
,"WARNING: getch2_init called twice!\n");
4219 getch2_enable(); // prepare stdin for hotkeys...
4220 mpctx
->initialized_flags
|=INITIALIZED_GETCH2
;
4221 mp_msg(MSGT_CPLAYER
,MSGL_DBG2
,"\n[[[init getch2]]]\n");
4224 // =================== GUI idle loop (STOP state) ===========================
4225 while (opts
->player_idle_mode
&& !mpctx
->filename
) {
4226 play_tree_t
* entry
= NULL
;
4228 if (mpctx
->video_out
&& mpctx
->video_out
->config_ok
)
4229 vo_control(mpctx
->video_out
, VOCTRL_PAUSE
, NULL
);
4230 while (!(cmd
= mp_input_get_cmd(mpctx
->input
, 0, 0))) { // wait for command
4231 if (mpctx
->video_out
)
4232 vo_check_events(mpctx
->video_out
);
4236 case MP_CMD_LOADFILE
:
4237 // prepare a tree entry with the new filename
4238 entry
= play_tree_new();
4239 play_tree_add_file(entry
, cmd
->args
[0].v
.s
);
4240 // The entry is added to the main playtree after the switch().
4242 case MP_CMD_LOADLIST
:
4243 entry
= parse_playlist_file(mpctx
->mconfig
, cmd
->args
[0].v
.s
);
4246 exit_player_with_rc(mpctx
, EXIT_QUIT
, (cmd
->nargs
> 0)? cmd
->args
[0].v
.i
: 0);
4248 case MP_CMD_VO_FULLSCREEN
:
4249 case MP_CMD_GET_PROPERTY
:
4250 case MP_CMD_SET_PROPERTY
:
4251 case MP_CMD_STEP_PROPERTY
:
4252 run_command(mpctx
, cmd
);
4258 if (entry
) { // user entered a command that gave a valid entry
4259 if (mpctx
->playtree
) // the playtree is always a node with one child. let's clear it
4260 play_tree_free_list(mpctx
->playtree
->child
, 1);
4261 else mpctx
->playtree
=play_tree_new(); // .. or make a brand new playtree
4263 if (!mpctx
->playtree
) continue; // couldn't make playtree! wait for next command
4265 play_tree_set_child(mpctx
->playtree
, entry
);
4267 /* Make iterator start at the top the of tree. */
4268 mpctx
->playtree_iter
= play_tree_iter_new(mpctx
->playtree
, mpctx
->mconfig
);
4269 if (!mpctx
->playtree_iter
) continue;
4271 // find the first real item in the tree
4272 if (play_tree_iter_step(mpctx
->playtree_iter
,0,0) != PLAY_TREE_ITER_ENTRY
) {
4274 play_tree_iter_free(mpctx
->playtree_iter
);
4275 mpctx
->playtree_iter
= NULL
;
4276 continue; // wait for next command
4278 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
, 1);
4281 //---------------------------------------------------------------------------
4283 if (mpctx
->video_out
&& mpctx
->sh_video
&& mpctx
->video_out
->config_ok
)
4284 vo_control(mpctx
->video_out
, VOCTRL_RESUME
, NULL
);
4286 if (mpctx
->filename
) {
4287 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"\nPlaying %s.\n",
4288 filename_recode(mpctx
->filename
));
4289 if(use_filename_title
&& opts
->vo_wintitle
== NULL
)
4290 opts
->vo_wintitle
= strdup(mp_basename(mpctx
->filename
));
4294 if (edl_records
) free_edl(edl_records
);
4295 next_edl_record
= edl_records
= edl_parse_file();
4297 if (edl_output_filename
) {
4298 if (edl_fd
) fclose(edl_fd
);
4299 if ((edl_fd
= fopen(edl_output_filename
, "w")) == NULL
)
4301 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Can't open EDL file [%s] for writing.\n",
4302 filename_recode(edl_output_filename
));
4306 //==================== Open VOB-Sub ============================
4308 current_module
="vobsub";
4310 vo_vobsub
=vobsub_open(vobsub_name
,spudec_ifo
,1,&vo_spudec
);
4312 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Cannot load subtitles: %s\n",
4313 filename_recode(vobsub_name
));
4314 } else if (sub_auto
&& mpctx
->filename
){
4315 /* try to autodetect vobsub from movie filename ::atmos */
4316 char *buf
= strdup(mpctx
->filename
), *psub
;
4317 char *pdot
= strrchr(buf
, '.');
4318 char *pslash
= strrchr(buf
, '/');
4319 #if defined(__MINGW32__) || defined(__CYGWIN__)
4320 if (!pslash
) pslash
= strrchr(buf
, '\\');
4322 if (pdot
&& (!pslash
|| pdot
> pslash
))
4324 vo_vobsub
=vobsub_open(buf
,spudec_ifo
,0,&vo_spudec
);
4325 /* try from ~/.mplayer/sub */
4326 if(!vo_vobsub
&& (psub
= get_path( "sub/" ))) {
4327 const char *bname
= mp_basename(buf
);
4329 l
= strlen(psub
) + strlen(bname
) + 1;
4330 psub
= realloc(psub
,l
);
4332 vo_vobsub
=vobsub_open(psub
,spudec_ifo
,0,&vo_spudec
);
4338 mpctx
->initialized_flags
|=INITIALIZED_VOBSUB
;
4339 vobsub_set_from_lang(vo_vobsub
, opts
->sub_lang
);
4340 mp_property_do("sub_forced_only", M_PROPERTY_SET
, &forced_subs_only
, mpctx
);
4342 // setup global sub numbering
4343 mpctx
->sub_counts
[SUB_SOURCE_VOBSUB
] = vobsub_get_indexes_count(vo_vobsub
);
4346 //============ Open & Sync STREAM --- fork cache2 ====================
4349 mpctx
->demuxer
=NULL
;
4350 if (mpctx
->d_audio
) {
4351 //free_demuxer_stream(mpctx->d_audio);
4352 mpctx
->d_audio
=NULL
;
4354 if (mpctx
->d_video
) {
4355 //free_demuxer_stream(d_video);
4356 mpctx
->d_video
=NULL
;
4358 mpctx
->sh_audio
=NULL
;
4359 mpctx
->sh_video
=NULL
;
4361 current_module
="open_stream";
4362 mpctx
->stream
= open_stream(mpctx
->filename
, opts
, &mpctx
->file_format
);
4363 if(!mpctx
->stream
) { // error...
4364 mpctx
->stop_play
= libmpdemux_was_interrupted(mpctx
, PT_NEXT_ENTRY
);
4365 goto goto_next_file
;
4367 mpctx
->initialized_flags
|=INITIALIZED_STREAM
;
4369 if(mpctx
->file_format
== DEMUXER_TYPE_PLAYLIST
) {
4370 mp_msg(MSGT_CPLAYER
, MSGL_ERR
, "\nThis looks like a playlist, but "
4371 "playlist support will not be used automatically.\n"
4372 "MPlayer's playlist code is unsafe and should only be used with "
4373 "trusted sources.\nPlayback will probably fail.\n\n");
4377 current_module
="handle_playlist";
4378 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Parsing playlist %s...\n",
4379 filename_recode(mpctx
->filename
));
4380 entry
= parse_playtree(mpctx
->stream
, mpctx
->mconfig
, 0);
4381 mpctx
->eof
=playtree_add_playlist(mpctx
, entry
);
4382 goto goto_next_file
;
4385 mpctx
->stream
->start_pos
+=seek_to_byte
;
4387 if(stream_dump_type
==5){
4388 unsigned char buf
[4096];
4391 current_module
="dumpstream";
4392 stream_reset(mpctx
->stream
);
4393 stream_seek(mpctx
->stream
,mpctx
->stream
->start_pos
);
4394 f
=fopen(opts
->stream_dump_name
,"wb");
4396 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"Cannot open dump file.\n");
4397 exit_player(mpctx
, EXIT_ERROR
);
4399 if (opts
->chapterrange
[0] > 1) {
4400 int chapter
= opts
->chapterrange
[0] - 1;
4401 stream_control(mpctx
->stream
, STREAM_CTRL_SEEK_TO_CHAPTER
, &chapter
);
4403 while(!mpctx
->stream
->eof
&& !async_quit_request
){
4404 len
=stream_read(mpctx
->stream
,buf
,4096);
4406 if(fwrite(buf
,len
,1,f
) != 1) {
4407 mp_tmsg(MSGT_GLOBAL
,MSGL_FATAL
,"%s: Error writing file.\n",opts
->stream_dump_name
);
4408 exit_player(mpctx
, EXIT_ERROR
);
4411 if (opts
->chapterrange
[1] > 0) {
4413 if (stream_control(mpctx
->stream
, STREAM_CTRL_GET_CURRENT_CHAPTER
,
4414 &chapter
) == STREAM_OK
4415 && chapter
+ 1 > opts
->chapterrange
[1])
4420 mp_tmsg(MSGT_GLOBAL
,MSGL_FATAL
,"%s: Error writing file.\n",opts
->stream_dump_name
);
4421 exit_player(mpctx
, EXIT_ERROR
);
4423 mp_tmsg(MSGT_CPLAYER
, MSGL_INFO
, "Stream dump complete.\n");
4424 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
4427 #ifdef CONFIG_DVDREAD
4428 if(mpctx
->stream
->type
==STREAMTYPE_DVD
){
4429 current_module
="dvd lang->id";
4430 if(opts
->audio_lang
&& opts
->audio_id
==-1) opts
->audio_id
=dvd_aid_from_lang(mpctx
->stream
,opts
->audio_lang
);
4431 if(opts
->sub_lang
&& opts
->sub_id
==-1) opts
->sub_id
=dvd_sid_from_lang(mpctx
->stream
,opts
->sub_lang
);
4432 // setup global sub numbering
4433 mpctx
->sub_counts
[SUB_SOURCE_DEMUX
] = dvd_number_of_subs(mpctx
->stream
);
4434 current_module
=NULL
;
4438 #ifdef CONFIG_DVDNAV
4439 if(mpctx
->stream
->type
==STREAMTYPE_DVDNAV
){
4440 current_module
="dvdnav lang->id";
4441 if(opts
->audio_lang
&& opts
->audio_id
==-1) opts
->audio_id
=mp_dvdnav_aid_from_lang(mpctx
->stream
,opts
->audio_lang
);
4442 dvdsub_lang_id
= -3;
4443 if(opts
->sub_lang
&& opts
->sub_id
==-1)
4444 dvdsub_lang_id
= opts
->sub_id
= mp_dvdnav_sid_from_lang(mpctx
->stream
,opts
->sub_lang
);
4445 // setup global sub numbering
4446 mpctx
->sub_counts
[SUB_SOURCE_DEMUX
] = mp_dvdnav_number_of_subs(mpctx
->stream
);
4447 current_module
=NULL
;
4451 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
4453 if(stream_cache_size
>0){
4455 float stream_cache_min_percent
= opts
->stream_cache_min_percent
;
4456 float stream_cache_seek_min_percent
= opts
->stream_cache_seek_min_percent
;
4457 current_module
="enable_cache";
4458 res
= stream_enable_cache(mpctx
->stream
,stream_cache_size
*1024,
4459 stream_cache_size
*1024*(stream_cache_min_percent
/ 100.0),
4460 stream_cache_size
*1024*(stream_cache_seek_min_percent
/ 100.0));
4462 if((mpctx
->stop_play
= libmpdemux_was_interrupted(mpctx
, PT_NEXT_ENTRY
))) goto goto_next_file
;
4465 //============ Open DEMUXERS --- DETECT file type =======================
4466 current_module
="demux_open";
4468 mpctx
->demuxer
=demux_open(opts
, mpctx
->stream
,mpctx
->file_format
,opts
->audio_id
,opts
->video_id
,opts
->sub_id
,mpctx
->filename
);
4470 // HACK to get MOV Reference Files working
4472 if (mpctx
->demuxer
&& mpctx
->demuxer
->type
==DEMUXER_TYPE_PLAYLIST
)
4474 unsigned char* playlist_entry
;
4475 play_tree_t
*list
= NULL
, *entry
= NULL
;
4477 current_module
="handle_demux_playlist";
4478 while (ds_get_packet(mpctx
->demuxer
->video
,&playlist_entry
)>0)
4483 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Adding file %s to element entry.\n",
4484 filename_recode(playlist_entry
));
4486 bname
=mp_basename(playlist_entry
);
4487 if ((strlen(bname
)>10) && !strncmp(bname
,"qt",2) && !strncmp(bname
+3,"gateQT",6))
4490 if (!strcmp(playlist_entry
, mpctx
->filename
)) // ignoring self-reference
4493 entry
= play_tree_new();
4495 if (mpctx
->filename
&& !strcmp(mp_basename(playlist_entry
),playlist_entry
)) // add reference path of current file
4497 temp
=malloc((strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
))+strlen(playlist_entry
)+1));
4500 strncpy(temp
, mpctx
->filename
, strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
)));
4501 temp
[strlen(mpctx
->filename
)-strlen(mp_basename(mpctx
->filename
))]='\0';
4502 strcat(temp
, playlist_entry
);
4503 if (!strcmp(temp
, mpctx
->filename
)) {
4507 play_tree_add_file(entry
,temp
);
4508 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Resolving reference to %s.\n",temp
);
4513 play_tree_add_file(entry
,playlist_entry
);
4518 play_tree_append_entry(list
,entry
);
4520 free_demuxer(mpctx
->demuxer
);
4521 mpctx
->demuxer
= NULL
;
4525 entry
= play_tree_new();
4526 play_tree_set_child(entry
,list
);
4527 mpctx
->stop_play
= playtree_add_playlist(mpctx
, entry
);
4528 goto goto_next_file
;
4532 if(!mpctx
->demuxer
) {
4533 mp_tmsg(MSGT_CPLAYER
, MSGL_ERR
, "Failed to recognize file format.\n");
4534 goto goto_next_file
;
4537 if (mpctx
->demuxer
->matroska_data
.ordered_chapters
)
4538 build_ordered_chapter_timeline(mpctx
);
4540 if (!mpctx
->sources
) {
4541 mpctx
->sources
= talloc_ptrtype(NULL
, mpctx
->sources
);
4542 *mpctx
->sources
= (struct content_source
){.stream
= mpctx
->stream
,
4543 .demuxer
= mpctx
->demuxer
};
4544 mpctx
->num_sources
= 1;
4547 mpctx
->initialized_flags
|=INITIALIZED_DEMUXER
;
4550 if (opts
->ass_enabled
&& ass_library
) {
4551 for (int j
= 0; j
< mpctx
->num_sources
; j
++) {
4552 struct demuxer
*d
= mpctx
->sources
[j
].demuxer
;
4553 for (int i
= 0; i
< d
->num_attachments
; i
++) {
4554 struct demux_attachment
*att
= d
->attachments
+ i
;
4555 if (use_embedded_fonts
&& attachment_is_font(att
))
4556 ass_add_font(ass_library
, att
->name
, att
->data
, att
->data_size
);
4562 current_module
="demux_open2";
4564 mpctx
->d_audio
=mpctx
->demuxer
->audio
;
4565 mpctx
->d_video
=mpctx
->demuxer
->video
;
4566 mpctx
->d_sub
=mpctx
->demuxer
->sub
;
4570 mp_property_do("switch_program", M_PROPERTY_SET
, &tmp
, mpctx
);
4572 // select audio stream
4573 if (mpctx
->num_sources
)
4574 for (int i
= 0; i
< mpctx
->num_sources
; i
++)
4575 select_audio(mpctx
->sources
[i
].demuxer
, opts
->audio_id
,
4578 select_audio(mpctx
->d_audio
->demuxer
, opts
->audio_id
, opts
->audio_lang
);
4581 if((stream_dump_type
)&&(stream_dump_type
<4)){
4583 demux_stream_t
*ds
=NULL
;
4584 current_module
="dump";
4585 // select stream to dump
4586 switch(stream_dump_type
){
4587 case 1: ds
=mpctx
->d_audio
;break;
4588 case 2: ds
=mpctx
->d_video
;break;
4589 case 3: ds
=mpctx
->d_sub
;break;
4592 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"dump: FATAL: Selected stream missing!\n");
4593 exit_player(mpctx
, EXIT_ERROR
);
4595 // disable other streams:
4596 if(mpctx
->d_audio
&& mpctx
->d_audio
!=ds
) {ds_free_packs(mpctx
->d_audio
); mpctx
->d_audio
->id
=-2; }
4597 if(mpctx
->d_video
&& mpctx
->d_video
!=ds
) {ds_free_packs(mpctx
->d_video
); mpctx
->d_video
->id
=-2; }
4598 if(mpctx
->d_sub
&& mpctx
->d_sub
!=ds
) {ds_free_packs(mpctx
->d_sub
); mpctx
->d_sub
->id
=-2; }
4600 f
=fopen(opts
->stream_dump_name
,"wb");
4602 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
,"Cannot open dump file.\n");
4603 exit_player(mpctx
, EXIT_ERROR
);
4606 unsigned char* start
;
4607 int in_size
=ds_get_packet(ds
,&start
);
4608 if( (mpctx
->demuxer
->file_format
==DEMUXER_TYPE_AVI
|| mpctx
->demuxer
->file_format
==DEMUXER_TYPE_ASF
|| mpctx
->demuxer
->file_format
==DEMUXER_TYPE_MOV
)
4609 && stream_dump_type
==2) fwrite(&in_size
,1,4,f
);
4610 if(in_size
>0) fwrite(start
,in_size
,1,f
);
4611 if (opts
->chapterrange
[1] > 0) {
4612 int cur_chapter
= demuxer_get_current_chapter(mpctx
->demuxer
, 0);
4613 if(cur_chapter
!=-1 && cur_chapter
+1 > opts
->chapterrange
[1])
4618 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
, "Stream dump complete.\n");
4619 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
4622 mpctx
->sh_audio
=mpctx
->d_audio
->sh
;
4623 mpctx
->sh_video
=mpctx
->d_video
->sh
;
4625 if(mpctx
->sh_video
){
4627 current_module
="video_read_properties";
4628 if(!video_read_properties(mpctx
->sh_video
)) {
4629 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"Video: Cannot read properties.\n");
4630 mpctx
->sh_video
=mpctx
->d_video
->sh
=NULL
;
4632 mp_tmsg(MSGT_CPLAYER
,MSGL_V
,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
4633 mpctx
->demuxer
->file_format
,mpctx
->sh_video
->format
, mpctx
->sh_video
->disp_w
,mpctx
->sh_video
->disp_h
,
4634 mpctx
->sh_video
->fps
,mpctx
->sh_video
->frametime
4637 /* need to set fps here for output encoders to pick it up in their init */
4639 mpctx
->sh_video
->fps
=force_fps
;
4640 mpctx
->sh_video
->frametime
=1.0f
/mpctx
->sh_video
->fps
;
4642 vo_fps
= mpctx
->sh_video
->fps
;
4644 if(!mpctx
->sh_video
->fps
&& !force_fps
){
4645 mp_tmsg(MSGT_CPLAYER
,MSGL_ERR
,"FPS not specified in the header or invalid, use the -fps option.\n");
4646 mpctx
->opts
.correct_pts
= 1;
4652 if(!mpctx
->sh_video
&& !mpctx
->sh_audio
){
4653 mp_tmsg(MSGT_CPLAYER
,MSGL_FATAL
, "No stream found.\n");
4655 if(mpctx
->stream
->type
== STREAMTYPE_DVB
)
4658 int v
= mpctx
->last_dvb_step
;
4660 dir
= DVB_CHANNEL_HIGHER
;
4662 dir
= DVB_CHANNEL_LOWER
;
4664 if(dvb_step_channel(mpctx
->stream
, dir
)) {
4665 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4666 mpctx
->dvbin_reopen
= 1;
4670 goto goto_next_file
; // exit_player(_("Fatal error"));
4673 /* display clip info */
4674 demux_info_print(mpctx
->demuxer
);
4676 //================== Read SUBTITLES (DVD & TEXT) ==========================
4677 if(vo_spudec
==NULL
&&
4678 (mpctx
->stream
->type
==STREAMTYPE_DVD
|| mpctx
->stream
->type
== STREAMTYPE_DVDNAV
)){
4679 init_vo_spudec(mpctx
);
4682 // after reading video params we should load subtitles because
4683 // we know fps so now we can adjust subtitle time to ~6 seconds AST
4685 current_module
="read_subtitles_file";
4686 double sub_fps
= mpctx
->sh_video
? mpctx
->sh_video
->fps
: 25;
4688 for (i
= 0; sub_name
[i
] != NULL
; ++i
)
4689 add_subtitles(mpctx
, sub_name
[i
], sub_fps
, 0);
4691 if(sub_auto
) { // auto load sub file ...
4692 char *psub
= get_path( "sub/" );
4693 char **tmp
= sub_filenames((psub
? psub
: ""), mpctx
->filename
);
4695 free(psub
); // release the buffer created by get_path() above
4697 add_subtitles(mpctx
, tmp
[i
], sub_fps
, 1);
4702 if (mpctx
->set_of_sub_size
> 0)
4703 mpctx
->sub_counts
[SUB_SOURCE_SUBS
] = mpctx
->set_of_sub_size
;
4705 if (select_subtitle(mpctx
)) {
4707 switch (stream_dump_type
) {
4708 case 3: list_sub_file(mpctx
->subdata
); break;
4709 case 4: dump_mpsub(mpctx
->subdata
, mpctx
->sh_video
->fps
); break;
4710 case 6: dump_srt(mpctx
->subdata
, mpctx
->sh_video
->fps
); break;
4711 case 7: dump_microdvd(mpctx
->subdata
, mpctx
->sh_video
->fps
); break;
4712 case 8: dump_jacosub(mpctx
->subdata
, mpctx
->sh_video
->fps
); break;
4713 case 9: dump_sami(mpctx
->subdata
, mpctx
->sh_video
->fps
); break;
4717 print_file_properties(mpctx
, mpctx
->filename
);
4719 if (mpctx
->sh_video
)
4720 reinit_video_chain(mpctx
);
4721 if (mpctx
->sh_video
) {
4722 if(mpctx
->sh_video
->output_flags
& VFCAP_SPU
&& vo_spudec
)
4723 spudec_set_hw_spu(vo_spudec
,mpctx
->video_out
);
4724 #ifdef CONFIG_FREETYPE
4725 force_load_font
= 1;
4727 } else if (!mpctx
->sh_audio
)
4728 goto goto_next_file
;
4730 //================== MAIN: ==========================
4731 current_module
="main";
4733 if (opts
->playing_msg
) {
4734 char* msg
= property_expand_string(mpctx
, opts
->playing_msg
);
4735 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"%s",msg
);
4740 // Disable the term OSD in verbose mode
4746 // Make sure old OSD does not stay around,
4747 // e.g. with -fixed-vo and same-resolution files
4749 update_osd_msg(mpctx
);
4751 //================ SETUP AUDIO ==========================
4753 if(mpctx
->sh_audio
){
4754 reinit_audio_chain(mpctx
);
4755 if (mpctx
->sh_audio
&& mpctx
->sh_audio
->codec
)
4756 mp_msg(MSGT_IDENTIFY
,MSGL_INFO
, "ID_AUDIO_CODEC=%s\n", mpctx
->sh_audio
->codec
->name
);
4759 current_module
="av_init";
4761 if(mpctx
->sh_video
){
4762 mpctx
->sh_video
->timer
=0;
4764 audio_delay
+= mpctx
->sh_video
->stream_delay
;
4766 if(mpctx
->sh_audio
){
4767 if (start_volume
>= 0)
4768 mixer_setvolume(&mpctx
->mixer
, start_volume
, start_volume
);
4770 audio_delay
-= mpctx
->sh_audio
->stream_delay
;
4771 mpctx
->delay
=-audio_delay
;
4774 if(!mpctx
->sh_audio
){
4775 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Audio: no sound\n");
4776 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Freeing %d unused audio chunks.\n",mpctx
->d_audio
->packs
);
4777 ds_free_packs(mpctx
->d_audio
); // free buffered chunks
4778 //mpctx->d_audio->id=-2; // do not read audio chunks
4779 //uninit_player(mpctx, INITIALIZED_AO); // close device
4781 if(!mpctx
->sh_video
){
4782 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Video: no video\n");
4783 mp_msg(MSGT_CPLAYER
,MSGL_V
,"Freeing %d unused video chunks.\n",mpctx
->d_video
->packs
);
4784 ds_free_packs(mpctx
->d_video
);
4785 mpctx
->d_video
->id
=-2;
4786 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
4789 if (!mpctx
->sh_video
&& !mpctx
->sh_audio
)
4790 goto goto_next_file
;
4792 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
4793 if(force_fps
&& mpctx
->sh_video
){
4794 vo_fps
= mpctx
->sh_video
->fps
=force_fps
;
4795 mpctx
->sh_video
->frametime
=1.0f
/mpctx
->sh_video
->fps
;
4796 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx
->sh_video
->fps
,mpctx
->sh_video
->frametime
);
4799 mp_input_set_section(mpctx
->input
, NULL
);
4800 //TODO: add desired (stream-based) sections here
4801 if (mpctx
->stream
->type
==STREAMTYPE_TV
) mp_input_set_section(mpctx
->input
, "tv");
4802 if (mpctx
->stream
->type
==STREAMTYPE_DVDNAV
) mp_input_set_section(mpctx
->input
, "dvdnav");
4804 //==================== START PLAYING =======================
4806 if(opts
->loop_times
>1) opts
->loop_times
--; else
4807 if(opts
->loop_times
==1) opts
->loop_times
= -1;
4809 mp_tmsg(MSGT_CPLAYER
,MSGL_INFO
,"Starting playback...\n");
4811 total_time_usage_start
=GetTimer();
4812 audio_time_usage
=0; video_time_usage
=0; vout_time_usage
=0;
4813 total_frame_cnt
=0; drop_frame_cnt
=0; // fix for multifile fps benchmark
4814 play_n_frames
=play_n_frames_mf
;
4816 if(play_n_frames
==0){
4817 mpctx
->stop_play
=PT_NEXT_ENTRY
; goto goto_next_file
;
4820 mpctx
->time_frame
= 0;
4821 mpctx
->drop_message_shown
= 0;
4822 mpctx
->restart_playback
= true;
4823 mpctx
->video_pts
= 0;
4824 mpctx
->hrseek_active
= false;
4825 mpctx
->hrseek_framedrop
= false;
4826 mpctx
->step_frames
= 0;
4827 mpctx
->total_avsync_change
= 0;
4828 mpctx
->last_chapter_seek
= -1;
4830 // If there's a timeline force an absolute seek to initialize state
4831 if (seek_to_sec
|| mpctx
->timeline
) {
4832 queue_seek(mpctx
, MPSEEK_ABSOLUTE
, seek_to_sec
, 0);
4833 seek(mpctx
, mpctx
->seek
);
4834 end_at
.pos
+= seek_to_sec
;
4836 if (opts
->chapterrange
[0] > 0) {
4838 if (seek_chapter(mpctx
, opts
->chapterrange
[0]-1, &pts
, NULL
) >= 0
4840 queue_seek(mpctx
, MPSEEK_ABSOLUTE
, pts
, 0);
4841 seek(mpctx
, mpctx
->seek
);
4845 if (end_at
.type
== END_AT_SIZE
) {
4846 mp_tmsg(MSGT_CPLAYER
, MSGL_WARN
, "Option -endpos in MPlayer does not yet support size units.\n");
4847 end_at
.type
= END_AT_NONE
;
4850 #ifdef CONFIG_DVDNAV
4851 mp_dvdnav_context_free(mpctx
);
4852 if (mpctx
->stream
->type
== STREAMTYPE_DVDNAV
) {
4853 mp_dvdnav_read_wait(mpctx
->stream
, 0, 1);
4854 mp_dvdnav_cell_has_changed(mpctx
->stream
,1);
4858 mpctx
->seek
= (struct seek_params
){0};
4859 get_relative_time(mpctx
); // reset current delta
4860 // Make sure VO knows current pause state
4861 if (mpctx
->sh_video
)
4862 vo_control(mpctx
->video_out
, mpctx
->paused
? VOCTRL_PAUSE
: VOCTRL_RESUME
,
4865 while (!mpctx
->stop_play
)
4866 run_playloop(mpctx
);
4868 mp_msg(MSGT_GLOBAL
,MSGL_V
,"EOF code: %d \n",mpctx
->stop_play
);
4871 if(mpctx
->dvbin_reopen
)
4873 mpctx
->stop_play
= 0;
4874 uninit_player(mpctx
, INITIALIZED_ALL
-(INITIALIZED_STREAM
|INITIALIZED_GETCH2
|(opts
->fixed_vo
?INITIALIZED_VO
:0)));
4875 cache_uninit(mpctx
->stream
);
4876 mpctx
->dvbin_reopen
= 0;
4877 goto goto_enable_cache
;
4882 goto_next_file
: // don't jump here after ao/vo/getch initialization!
4884 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"\n");
4886 if (opts
->benchmark
) {
4887 double tot
=video_time_usage
+vout_time_usage
+audio_time_usage
;
4888 double total_time_usage
;
4889 total_time_usage_start
=GetTimer()-total_time_usage_start
;
4890 total_time_usage
= (float)total_time_usage_start
*0.000001;
4891 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4892 video_time_usage
,vout_time_usage
,audio_time_usage
,
4893 total_time_usage
-tot
,total_time_usage
);
4894 if(total_time_usage
>0.0)
4895 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4896 100.0*video_time_usage
/total_time_usage
,
4897 100.0*vout_time_usage
/total_time_usage
,
4898 100.0*audio_time_usage
/total_time_usage
,
4899 100.0*(total_time_usage
-tot
)/total_time_usage
,
4901 if(total_frame_cnt
&& frame_dropping
)
4902 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4903 total_frame_cnt
-drop_frame_cnt
,
4904 (total_time_usage
>0.5)?((total_frame_cnt
-drop_frame_cnt
)/total_time_usage
):0,
4906 100*drop_frame_cnt
/total_frame_cnt
,
4908 (total_time_usage
>0.5)?(total_frame_cnt
/total_time_usage
):0);
4911 // time to uninit all, except global stuff:
4912 int uninitialize_parts
= INITIALIZED_ALL
;
4914 uninitialize_parts
-= INITIALIZED_VO
;
4915 if (opts
->gapless_audio
&& mpctx
->stop_play
== AT_END_OF_FILE
)
4916 uninitialize_parts
-= INITIALIZED_AO
;
4917 uninit_player(mpctx
, uninitialize_parts
);
4919 if(mpctx
->set_of_sub_size
> 0) {
4920 current_module
="sub_free";
4921 for(i
= 0; i
< mpctx
->set_of_sub_size
; ++i
) {
4922 sub_free(mpctx
->set_of_subtitles
[i
]);
4924 if(mpctx
->set_of_ass_tracks
[i
])
4925 ass_free_track( mpctx
->set_of_ass_tracks
[i
] );
4928 mpctx
->set_of_sub_size
= 0;
4930 mpctx
->vo_sub_last
= vo_sub
=NULL
;
4931 mpctx
->subdata
=NULL
;
4933 mpctx
->osd
->ass_track
= NULL
;
4935 ass_clear_fonts(ass_library
);
4938 if (!mpctx
->stop_play
) // In case some goto jumped here...
4939 mpctx
->stop_play
= PT_NEXT_ENTRY
;
4941 int playtree_direction
= 1;
4943 if(mpctx
->stop_play
== PT_NEXT_ENTRY
|| mpctx
->stop_play
== PT_PREV_ENTRY
) {
4944 if(play_tree_iter_step(mpctx
->playtree_iter
,mpctx
->play_tree_step
,0) != PLAY_TREE_ITER_ENTRY
) {
4945 play_tree_iter_free(mpctx
->playtree_iter
);
4946 mpctx
->playtree_iter
= NULL
;
4948 mpctx
->play_tree_step
= 1;
4949 } else if(mpctx
->stop_play
== PT_UP_NEXT
|| mpctx
->stop_play
== PT_UP_PREV
) {
4950 int direction
= mpctx
->stop_play
== PT_UP_NEXT
? 1 : -1;
4951 if(mpctx
->playtree_iter
) {
4952 if(play_tree_iter_up_step(mpctx
->playtree_iter
,direction
,0) != PLAY_TREE_ITER_ENTRY
) {
4953 play_tree_iter_free(mpctx
->playtree_iter
);
4954 mpctx
->playtree_iter
= NULL
;
4957 } else if (mpctx
->stop_play
== PT_STOP
) {
4958 play_tree_iter_free(mpctx
->playtree_iter
);
4959 mpctx
->playtree_iter
= NULL
;
4960 } else { // NEXT PREV SRC
4961 playtree_direction
= mpctx
->stop_play
== PT_PREV_SRC
? -1 : 1;
4964 while(mpctx
->playtree_iter
!= NULL
) {
4965 mpctx
->filename
= play_tree_iter_get_file(mpctx
->playtree_iter
, playtree_direction
);
4966 if(mpctx
->filename
== NULL
) {
4967 if(play_tree_iter_step(mpctx
->playtree_iter
, playtree_direction
, 0) != PLAY_TREE_ITER_ENTRY
) {
4968 play_tree_iter_free(mpctx
->playtree_iter
);
4969 mpctx
->playtree_iter
= NULL
;
4975 if (mpctx
->playtree_iter
!= NULL
|| opts
->player_idle_mode
) {
4976 if(!mpctx
->playtree_iter
) mpctx
->filename
= NULL
;
4977 mpctx
->stop_play
= 0;
4978 goto play_next_file
;
4982 exit_player_with_rc(mpctx
, EXIT_EOF
, 0);
4986 #endif /* DISABLE_MAIN */