Ignore svn changes up to r30559
[mplayer/kovensky.git] / mplayer.c
blob9eff2c16d7e8a304e896f8e110e0724f418b93ec
1 /*
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.
19 /// \file
20 /// \ingroup Properties Command2Property OSDMsgStack
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include "config.h"
26 #include "talloc.h"
28 #if defined(__MINGW32__) || defined(__CYGWIN__)
29 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
30 #include <windows.h>
31 #endif
32 #include <string.h>
33 #include <unistd.h>
35 // #include <sys/mman.h>
36 #include <sys/types.h>
37 #ifndef __MINGW32__
38 #include <sys/ioctl.h>
39 #include <sys/wait.h>
40 #else
41 #define SIGHUP 1 /* hangup */
42 #define SIGQUIT 3 /* quit */
43 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
44 #define SIGBUS 10 /* bus error */
45 #define SIGPIPE 13 /* broken pipe */
46 #endif
48 #include <sys/time.h>
49 #include <sys/stat.h>
51 #include <signal.h>
52 #include <time.h>
53 #include <fcntl.h>
54 #include <limits.h>
56 #include <errno.h>
58 #include "mp_msg.h"
59 #include "av_log.h"
61 #include "help_mp.h"
63 #include "m_option.h"
64 #include "m_config.h"
65 #include "mplayer.h"
66 #include "access_mpcontext.h"
67 #include "m_property.h"
69 #include "cfg-mplayer-def.h"
71 #include "ffmpeg_files/intreadwrite.h"
72 #include "libavutil/avstring.h"
74 #include "subreader.h"
76 #include "mp_osd.h"
77 #include "libvo/video_out.h"
79 #include "libvo/font_load.h"
80 #include "libvo/sub.h"
82 #ifdef CONFIG_X11
83 #include "libvo/x11_common.h"
84 #endif
86 #include "libao2/audio_out.h"
88 #include "codec-cfg.h"
90 #include "edl.h"
92 #include "spudec.h"
93 #include "vobsub.h"
95 #include "osdep/getch2.h"
96 #include "osdep/timer.h"
97 #include "osdep/findfiles.h"
99 #include "input/input.h"
101 const int under_mencoder = 0;
102 int slave_mode=0;
103 int player_idle_mode=0;
104 int quiet=0;
105 int enable_mouse_movements=0;
106 float start_volume = -1;
108 #include "osdep/priority.h"
110 char *heartbeat_cmd;
112 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
114 #ifdef HAVE_RTC
115 #ifdef __linux__
116 #include <linux/rtc.h>
117 #else
118 #include <rtc.h>
119 #define RTC_IRQP_SET RTCIO_IRQP_SET
120 #define RTC_PIE_ON RTCIO_PIE_ON
121 #endif /* __linux__ */
122 #endif /* HAVE_RTC */
124 #include "stream/tv.h"
125 #include "stream/stream_radio.h"
126 #ifdef CONFIG_DVBIN
127 #include "stream/dvbin.h"
128 #endif
129 #include "stream/cache2.h"
131 //**************************************************************************//
132 // Playtree
133 //**************************************************************************//
134 #include "playtree.h"
135 #include "playtreeparser.h"
137 //**************************************************************************//
138 // Config
139 //**************************************************************************//
140 #include "parser-cfg.h"
141 #include "parser-mpcmd.h"
143 //**************************************************************************//
144 // Config file
145 //**************************************************************************//
147 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
149 #include "get_path.h"
151 //**************************************************************************//
152 //**************************************************************************//
153 // Input media streaming & demultiplexer:
154 //**************************************************************************//
156 static int max_framesize=0;
158 #include "stream/stream.h"
159 #include "libmpdemux/demuxer.h"
160 #include "libmpdemux/stheader.h"
162 #ifdef CONFIG_DVDREAD
163 #include "stream/stream_dvd.h"
164 #endif
165 #include "stream/stream_dvdnav.h"
167 #include "libmpcodecs/dec_audio.h"
168 #include "libmpcodecs/dec_video.h"
169 #include "libmpcodecs/mp_image.h"
170 #include "libmpcodecs/vf.h"
171 #include "libmpcodecs/vd.h"
173 #include "mixer.h"
175 #include "mp_core.h"
176 #include "options.h"
177 #include "defaultopts.h"
179 static const char help_text[]=_(
180 "Usage: mplayer [options] [url|path/]filename\n"
181 "\n"
182 "Basic options: (complete list in the man page)\n"
183 " -vo <drv> select video output driver ('-vo help' for a list)\n"
184 " -ao <drv> select audio output driver ('-ao help' for a list)\n"
185 #ifdef CONFIG_VCD
186 " vcd://<trackno> play (S)VCD (Super Video CD) track (raw device, no mount)\n"
187 #endif
188 #ifdef CONFIG_DVDREAD
189 " dvd://<titleno> play DVD title from device instead of plain file\n"
190 #endif
191 " -alang/-slang select DVD audio/subtitle language (by 2-char country code)\n"
192 " -ss <position> seek to given (seconds or hh:mm:ss) position\n"
193 " -nosound do not play sound\n"
194 " -fs fullscreen playback (or -vm, -zoom, details in the man page)\n"
195 " -x <x> -y <y> set display resolution (for use with -vm or -zoom)\n"
196 " -sub <file> specify subtitle file to use (also see -subfps, -subdelay)\n"
197 " -playlist <file> specify playlist file\n"
198 " -vid x -aid y select video (x) and audio (y) stream to play\n"
199 " -fps x -srate y change video (x fps) and audio (y Hz) rate\n"
200 " -pp <quality> enable postprocessing filter (details in the man page)\n"
201 " -framedrop enable frame dropping (for slow machines)\n"
202 "\n"
203 "Basic keys: (complete list in the man page, also check input.conf)\n"
204 " <- or -> seek backward/forward 10 seconds\n"
205 " down or up seek backward/forward 1 minute\n"
206 " pgdown or pgup seek backward/forward 10 minutes\n"
207 " < or > step backward/forward in playlist\n"
208 " p or SPACE pause movie (press any key to continue)\n"
209 " q or ESC stop playing and quit program\n"
210 " + or - adjust audio delay by +/- 0.1 second\n"
211 " o cycle OSD mode: none / seekbar / seekbar + timer\n"
212 " * or / increase or decrease PCM volume\n"
213 " x or z adjust subtitle delay by +/- 0.1 second\n"
214 " r or t adjust subtitle position up/down, also see -vf expand\n"
215 "\n"
216 " * * * SEE THE MAN PAGE FOR DETAILS, FURTHER (ADVANCED) OPTIONS AND KEYS * * *\n"
217 "\n");
220 #define Exit_SIGILL_RTCpuSel _(\
221 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
222 " It may be a bug in our new runtime CPU-detection code...\n"\
223 " Please read DOCS/HTML/en/bugreports.html.\n")
225 #define Exit_SIGILL _(\
226 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
227 " It usually happens when you run it on a CPU different than the one it was\n"\
228 " compiled/optimized for.\n"\
229 " Verify this!\n")
231 #define Exit_SIGSEGV_SIGFPE _(\
232 "- MPlayer crashed by bad usage of CPU/FPU/RAM.\n"\
233 " Recompile MPlayer with --enable-debug and make a 'gdb' backtrace and\n"\
234 " disassembly. Details in DOCS/HTML/en/bugreports_what.html#bugreports_crash.\n")
236 #define Exit_SIGCRASH _(\
237 "- MPlayer crashed. This shouldn't happen.\n"\
238 " It can be a bug in the MPlayer code _or_ in your drivers _or_ in your\n"\
239 " gcc version. If you think it's MPlayer's fault, please read\n"\
240 " DOCS/HTML/en/bugreports.html and follow the instructions there. We can't and\n"\
241 " won't help unless you provide this information when reporting a possible bug.\n")
243 #define SystemTooSlow _("\n\n"\
244 " ************************************************\n"\
245 " **** Your system is too SLOW to play this! ****\n"\
246 " ************************************************\n\n"\
247 "Possible reasons, problems, workarounds:\n"\
248 "- Most common: broken/buggy _audio_ driver\n"\
249 " - Try -ao sdl or use the OSS emulation of ALSA.\n"\
250 " - Experiment with different values for -autosync, 30 is a good start.\n"\
251 "- Slow video output\n"\
252 " - Try a different -vo driver (-vo help for a list) or try -framedrop!\n"\
253 "- Slow CPU\n"\
254 " - Don't try to play a big DVD/DivX on a slow CPU! Try some of the lavdopts,\n"\
255 " e.g. -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all.\n"\
256 "- Broken file\n"\
257 " - Try various combinations of -nobps -ni -forceidx -mc 0.\n"\
258 "- Slow media (NFS/SMB mounts, DVD, VCD etc)\n"\
259 " - Try -cache 8192.\n"\
260 "- Are you using -cache to play a non-interleaved AVI file?\n"\
261 " - Try -nocache.\n"\
262 "Read DOCS/HTML/en/video.html for tuning/speedup tips.\n"\
263 "If none of this helps you, read DOCS/HTML/en/bugreports.html.\n\n")
266 //**************************************************************************//
267 //**************************************************************************//
269 // Common FIFO functions, and keyboard/event FIFO code
270 #include "mp_fifo.h"
271 int noconsolecontrols=0;
272 //**************************************************************************//
274 // benchmark:
275 double video_time_usage=0;
276 double vout_time_usage=0;
277 static double audio_time_usage=0;
278 static int total_time_usage_start=0;
279 static int total_frame_cnt=0;
280 static int drop_frame_cnt=0; // total number of dropped frames
281 int benchmark=0;
283 // options:
284 int auto_quality=0;
285 static int output_quality=0;
287 static int list_properties = 0;
289 int term_osd = 1;
290 static char* term_osd_esc = "\x1b[A\r\x1b[K";
291 static char* playing_msg = NULL;
292 // seek:
293 static double seek_to_sec;
294 static off_t seek_to_byte=0;
295 static off_t step_sec=0;
297 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
299 // A/V sync:
300 int autosync=0; // 30 might be a good default value.
302 // codecs:
303 char **audio_codec_list=NULL; // override audio codec
304 char **video_codec_list=NULL; // override video codec
305 char **audio_fm_list=NULL; // override audio codec family
306 char **video_fm_list=NULL; // override video codec family
308 // demuxer:
309 extern char *demuxer_name; // override demuxer
310 extern char *audio_demuxer_name; // override audio demuxer
311 extern char *sub_demuxer_name; // override sub demuxer
313 // this dvdsub_id was selected via slang
314 // use this to allow dvdnav to follow -slang across stream resets,
315 // in particular the subtitle ID for a language changes
316 int dvdsub_lang_id;
317 int vobsub_id=-1;
318 char* audio_lang=NULL;
319 char* dvdsub_lang=NULL;
320 static char* spudec_ifo=NULL;
321 int forced_subs_only=0;
322 int file_filter=1;
324 // cache2:
325 int stream_cache_size=-1;
326 #ifdef CONFIG_STREAM_CACHE
327 extern int cache_fill_status;
329 float stream_cache_min_percent=20.0;
330 float stream_cache_seek_min_percent=50.0;
331 #else
332 #define cache_fill_status 0
333 #endif
335 // dump:
336 static char *stream_dump_name="stream.dump";
337 int stream_dump_type=0;
339 // A-V sync:
340 static float default_max_pts_correction=-1;//0.01f;
341 float audio_delay=0;
342 static int ignore_start=0;
344 static int softsleep=0;
346 double force_fps=0;
347 static int force_srate=0;
348 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
349 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
350 static int play_n_frames=-1;
351 static int play_n_frames_mf=-1;
353 // sub:
354 char *font_name=NULL;
355 char *sub_font_name=NULL;
356 extern int font_fontconfig;
357 float font_factor=0.75;
358 char **sub_name=NULL;
359 float sub_delay=0;
360 float sub_fps=0;
361 int sub_auto = 1;
362 char *vobsub_name=NULL;
363 /*DSP!!char *dsp=NULL;*/
364 int subcc_enabled=0;
365 int suboverlap_enabled = 1;
367 #include "ass_mp.h"
369 char* current_module=NULL; // for debugging
372 // ---
374 #ifdef CONFIG_MENU
375 #include "m_struct.h"
376 #include "libmenu/menu.h"
377 void vf_menu_pause_update(struct vf_instance* vf);
378 extern vf_info_t vf_info_menu;
379 static const vf_info_t* const libmenu_vfs[] = {
380 &vf_info_menu,
381 NULL
383 static vf_instance_t* vf_menu = NULL;
384 int use_menu = 0;
385 static char* menu_cfg = NULL;
386 static char* menu_root = "main";
387 #endif
390 #ifdef HAVE_RTC
391 static int nortc = 1;
392 static char* rtc_device;
393 #endif
395 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
396 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
397 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
398 int use_filedir_conf;
399 int use_filename_title;
401 #include "mpcommon.h"
402 #include "command.h"
404 #include "metadata.h"
406 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
408 const void *mpctx_get_video_out(MPContext *mpctx)
410 return mpctx->video_out;
413 const void *mpctx_get_audio_out(MPContext *mpctx)
415 return mpctx->audio_out;
418 void *mpctx_get_demuxer(MPContext *mpctx)
420 return mpctx->demuxer;
423 void *mpctx_get_playtree_iter(MPContext *mpctx)
425 return mpctx->playtree_iter;
428 void *mpctx_get_mixer(MPContext *mpctx)
430 return &mpctx->mixer;
433 int mpctx_get_global_sub_size(MPContext *mpctx)
435 return mpctx->global_sub_size;
438 int mpctx_get_osd_function(MPContext *mpctx)
440 return mpctx->osd_function;
443 static float get_relative_time(struct MPContext *mpctx)
445 unsigned int new_time = GetTimer();
446 unsigned int delta = new_time - mpctx->last_time;
447 mpctx->last_time = new_time;
448 return delta * 0.000001;
451 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
452 switch (type)
454 /* check for valid video stream */
455 case META_VIDEO_CODEC:
456 case META_VIDEO_BITRATE:
457 case META_VIDEO_RESOLUTION:
459 if (!mpctx->sh_video)
460 return 0;
461 break;
464 /* check for valid audio stream */
465 case META_AUDIO_CODEC:
466 case META_AUDIO_BITRATE:
467 case META_AUDIO_SAMPLES:
469 if (!mpctx->sh_audio)
470 return 0;
471 break;
474 /* check for valid demuxer */
475 case META_INFO_TITLE:
476 case META_INFO_ARTIST:
477 case META_INFO_ALBUM:
478 case META_INFO_YEAR:
479 case META_INFO_COMMENT:
480 case META_INFO_TRACK:
481 case META_INFO_GENRE:
483 if (!mpctx->demuxer)
484 return 0;
485 break;
488 default:
489 break;
492 return 1;
495 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
496 char **info = mpctx->demuxer->info;
497 int n;
499 if (!info || !tag)
500 return NULL;
502 for (n = 0; info[2*n] != NULL ; n++)
503 if (!strcasecmp (info[2*n], tag))
504 break;
506 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
509 char *get_metadata(struct MPContext *mpctx, metadata_t type)
511 char *meta = NULL;
512 sh_audio_t * const sh_audio = mpctx->sh_audio;
513 sh_video_t * const sh_video = mpctx->sh_video;
515 if (!is_valid_metadata_type(mpctx, type))
516 return NULL;
518 switch (type)
520 case META_NAME:
522 return strdup (mp_basename2 (mpctx->filename));
525 case META_VIDEO_CODEC:
527 if (sh_video->format == 0x10000001)
528 meta = strdup ("mpeg1");
529 else if (sh_video->format == 0x10000002)
530 meta = strdup ("mpeg2");
531 else if (sh_video->format == 0x10000004)
532 meta = strdup ("mpeg4");
533 else if (sh_video->format == 0x10000005)
534 meta = strdup ("h264");
535 else if (sh_video->format >= 0x20202020)
537 meta = malloc (8);
538 sprintf (meta, "%.4s", (char *) &sh_video->format);
540 else
542 meta = malloc (8);
543 sprintf (meta, "0x%08X", sh_video->format);
545 return meta;
548 case META_VIDEO_BITRATE:
550 meta = malloc (16);
551 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
552 return meta;
555 case META_VIDEO_RESOLUTION:
557 meta = malloc (16);
558 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
559 return meta;
562 case META_AUDIO_CODEC:
564 if (sh_audio->codec && sh_audio->codec->name)
565 meta = strdup (sh_audio->codec->name);
566 return meta;
569 case META_AUDIO_BITRATE:
571 meta = malloc (16);
572 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
573 return meta;
576 case META_AUDIO_SAMPLES:
578 meta = malloc (16);
579 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
580 return meta;
583 /* check for valid demuxer */
584 case META_INFO_TITLE:
585 return get_demuxer_info(mpctx, "Title");
587 case META_INFO_ARTIST:
588 return get_demuxer_info(mpctx, "Artist");
590 case META_INFO_ALBUM:
591 return get_demuxer_info(mpctx, "Album");
593 case META_INFO_YEAR:
594 return get_demuxer_info(mpctx, "Year");
596 case META_INFO_COMMENT:
597 return get_demuxer_info(mpctx, "Comment");
599 case META_INFO_TRACK:
600 return get_demuxer_info(mpctx, "Track");
602 case META_INFO_GENRE:
603 return get_demuxer_info(mpctx, "Genre");
605 default:
606 break;
609 return meta;
612 /// step size of mixer changes
613 int volstep = 3;
615 #ifdef CONFIG_DVDNAV
616 static void mp_dvdnav_context_free(MPContext *ctx){
617 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
618 ctx->nav_smpi = NULL;
619 if (ctx->nav_buffer) free(ctx->nav_buffer);
620 ctx->nav_buffer = NULL;
621 ctx->nav_start = NULL;
622 ctx->nav_in_size = 0;
624 #endif
626 void uninit_player(struct MPContext *mpctx, unsigned int mask){
627 mask &= mpctx->initialized_flags;
629 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
631 if(mask&INITIALIZED_ACODEC){
632 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
633 current_module="uninit_acodec";
634 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
635 mpctx->sh_audio=NULL;
636 mpctx->mixer.afilter = NULL;
639 if(mask&INITIALIZED_VCODEC){
640 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
641 current_module="uninit_vcodec";
642 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
643 mpctx->sh_video=NULL;
644 #ifdef CONFIG_MENU
645 vf_menu=NULL;
646 #endif
649 if(mask&INITIALIZED_DEMUXER){
650 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
651 current_module="free_demuxer";
652 if (mpctx->num_sources) {
653 mpctx->demuxer = mpctx->sources[0].demuxer;
654 for (int i = 1; i < mpctx->num_sources; i++) {
655 free_stream(mpctx->sources[i].stream);
656 free_demuxer(mpctx->sources[i].demuxer);
659 talloc_free(mpctx->sources);
660 mpctx->sources = NULL;
661 mpctx->num_sources = 0;
662 talloc_free(mpctx->timeline);
663 mpctx->timeline = NULL;
664 mpctx->num_timeline_parts = 0;
665 talloc_free(mpctx->chapters);
666 mpctx->chapters = NULL;
667 mpctx->num_chapters = 0;
668 mpctx->video_offset = 0;
669 if(mpctx->demuxer){
670 mpctx->stream=mpctx->demuxer->stream;
671 free_demuxer(mpctx->demuxer);
673 mpctx->demuxer=NULL;
676 // kill the cache process:
677 if(mask&INITIALIZED_STREAM){
678 mpctx->initialized_flags&=~INITIALIZED_STREAM;
679 current_module="uninit_stream";
680 if(mpctx->stream) free_stream(mpctx->stream);
681 mpctx->stream=NULL;
684 if(mask&INITIALIZED_VO){
685 mpctx->initialized_flags&=~INITIALIZED_VO;
686 current_module="uninit_vo";
687 vo_destroy(mpctx->video_out);
688 mpctx->video_out=NULL;
689 #ifdef CONFIG_DVDNAV
690 mp_dvdnav_context_free(mpctx);
691 #endif
694 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
695 if(mask&INITIALIZED_GETCH2){
696 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
697 current_module="uninit_getch2";
698 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
699 // restore terminal:
700 getch2_disable();
703 if(mask&INITIALIZED_VOBSUB){
704 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
705 current_module="uninit_vobsub";
706 if(vo_vobsub) vobsub_close(vo_vobsub);
707 vo_vobsub=NULL;
710 if (mask&INITIALIZED_SPUDEC){
711 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
712 current_module="uninit_spudec";
713 spudec_free(vo_spudec);
714 vo_spudec=NULL;
717 if(mask&INITIALIZED_AO){
718 mpctx->initialized_flags&=~INITIALIZED_AO;
719 current_module="uninit_ao";
720 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
721 if (mpctx->audio_out)
722 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
723 mpctx->audio_out=NULL;
726 current_module=NULL;
729 void exit_player_with_rc(struct MPContext *mpctx, exit_reason_t how, int rc)
731 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
732 uninit_player(mpctx, INITIALIZED_ALL);
733 #if defined(__MINGW32__) || defined(__CYGWIN__)
734 timeEndPeriod(1);
735 #endif
736 #ifdef CONFIG_X11
737 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
738 #endif
740 current_module="uninit_input";
741 mp_input_uninit(mpctx->input);
742 #ifdef CONFIG_MENU
743 if (use_menu)
744 menu_uninit();
745 #endif
747 #ifdef CONFIG_FREETYPE
748 current_module="uninit_font";
749 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
750 free_font_desc(mpctx->osd->sub_font);
751 free_font_desc(vo_font);
752 vo_font = NULL;
753 done_freetype();
754 #endif
755 osd_free(mpctx->osd);
757 #ifdef CONFIG_ASS
758 ass_library_done(ass_library);
759 ass_library = NULL;
760 #endif
762 current_module="exit_player";
764 // free mplayer config
765 if(mpctx->mconfig)
766 m_config_free(mpctx->mconfig);
767 mpctx->mconfig = NULL;
769 if(mpctx->playtree_iter)
770 play_tree_iter_free(mpctx->playtree_iter);
771 mpctx->playtree_iter = NULL;
772 if(mpctx->playtree)
773 play_tree_free(mpctx->playtree, 1);
774 mpctx->playtree = NULL;
776 talloc_free(mpctx->key_fifo);
778 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
779 edl_records = NULL;
780 switch(how) {
781 case EXIT_QUIT:
782 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Quit");
783 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
784 break;
785 case EXIT_EOF:
786 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","End of file");
787 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
788 break;
789 case EXIT_ERROR:
790 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Fatal error");
791 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
792 break;
793 default:
794 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
796 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
798 exit(rc);
801 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
803 exit_player_with_rc(mpctx, how, 1);
806 #ifndef __MINGW32__
807 static void child_sighandler(int x){
808 pid_t pid;
809 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
811 #endif
813 #ifdef CONFIG_CRASH_DEBUG
814 static char *prog_path;
815 static int crash_debug = 0;
816 #endif
818 static void exit_sighandler(int x){
819 static int sig_count=0;
820 #ifdef CONFIG_CRASH_DEBUG
821 if (!crash_debug || x != SIGTRAP)
822 #endif
823 ++sig_count;
824 if(sig_count==5)
826 /* We're crashing bad and can't uninit cleanly :(
827 * by popular request, we make one last (dirty)
828 * effort to restore the user's
829 * terminal. */
830 getch2_disable();
831 exit(1);
833 if(sig_count==6) exit(1);
834 if(sig_count>6){
835 // can't stop :(
836 #ifndef __MINGW32__
837 kill(getpid(),SIGKILL);
838 #endif
840 mp_msg(MSGT_CPLAYER, MSGL_FATAL, "\n");
841 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,
842 "\nMPlayer interrupted by signal %d in module: %s\n", x,
843 current_module ? current_module : "unknown");
844 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
845 if(sig_count<=1)
846 switch(x){
847 case SIGINT:
848 case SIGQUIT:
849 case SIGTERM:
850 case SIGKILL:
851 async_quit_request = 1;
852 return; // killed from keyboard (^C) or killed [-9]
853 case SIGILL:
854 #if CONFIG_RUNTIME_CPUDETECT
855 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL_RTCpuSel);
856 #else
857 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL);
858 #endif
859 case SIGFPE:
860 case SIGSEGV:
861 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGSEGV_SIGFPE);
862 default:
863 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGCRASH);
864 #ifdef CONFIG_CRASH_DEBUG
865 if (crash_debug) {
866 int gdb_pid;
867 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
868 gdb_pid = fork();
869 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
870 if (gdb_pid == 0) { // We are the child
871 char spid[20];
872 snprintf(spid, sizeof(spid), "%i", getppid());
873 getch2_disable(); // allow terminal to work properly with gdb
874 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
875 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
876 } else if (gdb_pid < 0)
877 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
878 else {
879 waitpid(gdb_pid, NULL, 0);
881 if (x == SIGTRAP) return;
883 #endif
885 getch2_disable();
886 exit(1);
889 #include "cfg-mplayer.h"
891 static int cfg_include(m_option_t *conf, char *filename)
893 return m_config_parse_config_file(conf->priv, filename);
896 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
898 char *conffile;
899 int conffile_fd;
900 if (!disable_system_conf &&
901 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
902 exit_player(mpctx, EXIT_NONE);
903 if ((conffile = get_path("")) == NULL) {
904 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"Cannot find HOME directory.\n");
905 } else {
906 #ifdef __MINGW32__
907 mkdir(conffile);
908 #else
909 mkdir(conffile, 0777);
910 #endif
911 free(conffile);
912 if ((conffile = get_path("config")) == NULL) {
913 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
914 } else {
915 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
916 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Creating config file: %s\n", conffile);
917 write(conffile_fd, default_config, strlen(default_config));
918 close(conffile_fd);
920 if (!disable_user_conf &&
921 m_config_parse_config_file(conf, conffile) < 0)
922 exit_player(mpctx, EXIT_NONE);
923 free(conffile);
928 #define PROFILE_CFG_PROTOCOL "protocol."
930 static void load_per_protocol_config (m_config_t* conf, const char *const file)
932 char *str;
933 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
934 m_profile_t *p;
936 /* does filename actually uses a protocol ? */
937 str = strstr (file, "://");
938 if (!str)
939 return;
941 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
942 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
943 p = m_config_get_profile (conf, protocol);
944 if (p)
946 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading protocol-related profile '%s'\n", protocol);
947 m_config_set_profile(conf,p);
951 #define PROFILE_CFG_EXTENSION "extension."
953 static void load_per_extension_config (m_config_t* conf, const char *const file)
955 char *str;
956 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
957 m_profile_t *p;
959 /* does filename actually have an extension ? */
960 str = strrchr (file, '.');
961 if (!str)
962 return;
964 sprintf (extension, PROFILE_CFG_EXTENSION);
965 strncat (extension, ++str, 7);
966 p = m_config_get_profile (conf, extension);
967 if (p)
969 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", extension);
970 m_config_set_profile(conf,p);
974 #define PROFILE_CFG_VO "vo."
975 #define PROFILE_CFG_AO "ao."
977 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
979 char profile[strlen (cfg) + strlen (out) + 1];
980 m_profile_t *p;
982 sprintf (profile, "%s%s", cfg, out);
983 p = m_config_get_profile (conf, profile);
984 if (p)
986 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", profile);
987 m_config_set_profile(conf,p);
992 * Tries to load a config file
993 * @return 0 if file was not found, 1 otherwise
995 static int try_load_config(m_config_t *conf, const char *file)
997 struct stat st;
998 if (stat(file, &st))
999 return 0;
1000 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Loading config '%s'\n", file);
1001 m_config_parse_config_file (conf, file);
1002 return 1;
1005 static void load_per_file_config (m_config_t* conf, const char *const file)
1007 char *confpath;
1008 char cfg[PATH_MAX];
1009 char *name;
1011 if (strlen(file) > PATH_MAX - 14) {
1012 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Filename is too long, can not load file or directory specific config files\n");
1013 return;
1015 sprintf (cfg, "%s.conf", file);
1017 name = strrchr(cfg, '/');
1018 if (HAVE_DOS_PATHS) {
1019 char *tmp = strrchr(cfg, '\\');
1020 if (!name || tmp > name)
1021 name = tmp;
1022 tmp = strrchr(cfg, ':');
1023 if (!name || tmp > name)
1024 name = tmp;
1026 if (!name)
1027 name = cfg;
1028 else
1029 name++;
1031 if (use_filedir_conf) {
1032 char dircfg[PATH_MAX];
1033 strcpy(dircfg, cfg);
1034 strcpy(dircfg + (name - cfg), "mplayer.conf");
1035 try_load_config(conf, dircfg);
1037 if (try_load_config(conf, cfg))
1038 return;
1041 if ((confpath = get_path (name)) != NULL)
1043 try_load_config(conf, confpath);
1045 free (confpath);
1049 /* When libmpdemux performs a blocking operation (network connection or
1050 * cache filling) if the operation fails we use this function to check
1051 * if it was interrupted by the user.
1052 * The function returns a new value for eof. */
1053 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
1055 mp_cmd_t* cmd;
1056 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
1057 switch(cmd->id) {
1058 case MP_CMD_QUIT:
1059 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
1060 case MP_CMD_PLAY_TREE_STEP: {
1061 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
1062 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
1063 } break;
1064 case MP_CMD_PLAY_TREE_UP_STEP: {
1065 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
1066 } break;
1067 case MP_CMD_PLAY_ALT_SRC_STEP: {
1068 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
1069 } break;
1071 mp_cmd_free(cmd);
1073 return stop_play;
1076 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
1078 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
1080 play_tree_add_bpf(entry,mpctx->filename);
1083 if(!entry) {
1084 entry = mpctx->playtree_iter->tree;
1085 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1086 return PT_NEXT_ENTRY;
1088 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
1089 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1090 return PT_NEXT_ENTRY;
1093 play_tree_remove(entry,1,1);
1094 return PT_NEXT_SRC;
1096 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
1097 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1098 entry = mpctx->playtree_iter->tree;
1099 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1100 return PT_NEXT_ENTRY;
1102 play_tree_remove(entry,1,1);
1104 return PT_NEXT_SRC;
1107 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1109 struct MPOpts *opts = &mpctx->opts;
1110 sub_data *subd = NULL;
1111 struct ass_track *asst = NULL;
1113 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1114 return;
1117 #ifdef CONFIG_ASS
1118 if (opts->ass_enabled) {
1119 #ifdef CONFIG_ICONV
1120 asst = ass_read_stream(ass_library, filename, sub_cp);
1121 #else
1122 asst = ass_read_stream(ass_library, filename, 0);
1123 #endif
1124 if (!asst) {
1125 subd = sub_read_file(filename, fps);
1126 if (subd) {
1127 asst = ass_read_subdata(ass_library, subd, fps);
1128 if (asst) {
1129 sub_free(subd);
1130 subd = NULL;
1134 } else
1135 #endif
1136 subd = sub_read_file(filename, fps);
1139 if (!asst && !subd) {
1140 mp_tmsg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR,
1141 "Cannot load subtitles: %s\n", filename_recode(filename));
1142 return;
1145 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1146 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1147 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1148 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1149 filename_recode(filename));
1150 ++mpctx->set_of_sub_size;
1151 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "SUB: Added subtitle file (%d): %s\n", mpctx->set_of_sub_size,
1152 filename_recode(filename));
1155 void init_vo_spudec(struct MPContext *mpctx)
1157 if (vo_spudec)
1158 spudec_free(vo_spudec);
1159 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1160 vo_spudec = NULL;
1162 // we currently can't work without video stream
1163 if (!mpctx->sh_video)
1164 return;
1166 if (spudec_ifo) {
1167 unsigned int palette[16], width, height;
1168 current_module="spudec_init_vobsub";
1169 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1170 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1173 #ifdef CONFIG_DVDREAD
1174 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1175 current_module="spudec_init_dvdread";
1176 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1177 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1178 NULL, 0);
1180 #endif
1182 #ifdef CONFIG_DVDNAV
1183 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1184 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1185 current_module="spudec_init_dvdnav";
1186 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1188 #endif
1190 if (vo_spudec==NULL) {
1191 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1192 current_module="spudec_init_normal";
1193 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1194 spudec_set_font_factor(vo_spudec,font_factor);
1197 if (vo_spudec!=NULL) {
1198 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1199 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1204 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1205 * make it all work is to use the builtin SDL-bootstrap code, which
1206 * will be done automatically by replacing our main() if we include SDL.h.
1208 #if defined(__APPLE__) && defined(CONFIG_SDL)
1209 #ifdef CONFIG_SDL_SDL_H
1210 #include <SDL/SDL.h>
1211 #else
1212 #include <SDL.h>
1213 #endif
1214 #endif
1217 * \brief append a formatted string
1218 * \param buf buffer to print into
1219 * \param pos position of terminating 0 in buf
1220 * \param len maximum number of characters in buf, not including terminating 0
1221 * \param format printf format string
1223 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1225 va_list va;
1226 va_start(va, format);
1227 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1228 va_end(va);
1229 if (*pos >= len ) {
1230 buf[len] = 0;
1231 *pos = len;
1236 * \brief append time in the hh:mm:ss.f format
1237 * \param buf buffer to print into
1238 * \param pos position of terminating 0 in buf
1239 * \param len maximum number of characters in buf, not including terminating 0
1240 * \param time time value to convert/append
1242 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1243 long tenths = 10 * time;
1244 int f1 = tenths % 10;
1245 int ss = (tenths / 10) % 60;
1246 int mm = (tenths / 600) % 60;
1247 int hh = tenths / 36000;
1248 if (time <= 0) {
1249 saddf(buf, pos, len, "unknown");
1250 return;
1252 if (hh > 0)
1253 saddf(buf, pos, len, "%2d:", hh);
1254 if (hh > 0 || mm > 0)
1255 saddf(buf, pos, len, "%02d:", mm);
1256 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1259 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1261 struct MPOpts *opts = &mpctx->opts;
1262 sh_video_t * const sh_video = mpctx->sh_video;
1264 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1265 a_pos = playing_audio_pts(mpctx);
1266 if (mpctx->sh_audio && sh_video && at_frame) {
1267 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1268 if (mpctx->time_frame > 0)
1269 mpctx->last_av_difference += mpctx->time_frame * opts->playback_speed;
1270 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1271 && !mpctx->drop_message_shown) {
1272 mp_tmsg(MSGT_AVSYNC,MSGL_WARN,SystemTooSlow);
1273 mpctx->drop_message_shown = true;
1276 if (quiet)
1277 return;
1280 int width;
1281 char *line;
1282 unsigned pos = 0;
1283 get_screen_size();
1284 if (screen_width > 0)
1285 width = screen_width;
1286 else
1287 width = 80;
1288 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1289 /* Windows command line is broken (MinGW's rxvt works, but we
1290 * should not depend on that). */
1291 width--;
1292 #endif
1293 line = malloc(width + 1); // one additional char for the terminating null
1295 // Audio time
1296 if (mpctx->sh_audio) {
1297 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1298 if (!sh_video) {
1299 float len = demuxer_get_time_length(mpctx->demuxer);
1300 saddf(line, &pos, width, "(");
1301 sadd_hhmmssf(line, &pos, width, a_pos);
1302 saddf(line, &pos, width, ") of %.1f (", len);
1303 sadd_hhmmssf(line, &pos, width, len);
1304 saddf(line, &pos, width, ") ");
1308 // Video time
1309 if (sh_video)
1310 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1312 // A-V sync
1313 if (mpctx->sh_audio && sh_video)
1314 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1315 mpctx->last_av_difference, mpctx->total_avsync_change);
1317 // Video stats
1318 if (sh_video)
1319 saddf(line, &pos, width, "%3d/%3d ",
1320 (int)sh_video->num_frames,
1321 (int)sh_video->num_frames_decoded);
1323 // CPU usage
1324 if (sh_video) {
1325 if (sh_video->timer > 0.5)
1326 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1327 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1328 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1329 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1330 else
1331 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1332 } else if (mpctx->sh_audio) {
1333 if (mpctx->delay > 0.5)
1334 saddf(line, &pos, width, "%4.1f%% ",
1335 100.0*audio_time_usage/(double)mpctx->delay);
1336 else
1337 saddf(line, &pos, width, "??,?%% ");
1340 // VO stats
1341 if (sh_video)
1342 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1344 #ifdef CONFIG_STREAM_CACHE
1345 // cache stats
1346 if (stream_cache_size > 0)
1347 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1348 #endif
1350 // other
1351 if (opts->playback_speed != 1)
1352 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1354 // end
1355 if (erase_to_end_of_line) {
1356 line[pos] = 0;
1357 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1358 } else {
1359 memset(&line[pos], ' ', width - pos);
1360 line[width] = 0;
1361 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1363 free(line);
1367 * \brief build a chain of audio filters that converts the input format
1368 * to the ao's format, taking into account the current playback_speed.
1369 * \param sh_audio describes the requested input format of the chain.
1370 * \param ao_data describes the requested output format of the chain.
1372 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1374 struct MPOpts *opts = &mpctx->opts;
1375 int new_srate;
1376 int result;
1377 if (!sh_audio)
1379 mpctx->mixer.afilter = NULL;
1380 return 0;
1382 if(af_control_any_rev(sh_audio->afilter,
1383 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1384 &opts->playback_speed)) {
1385 new_srate = sh_audio->samplerate;
1386 } else {
1387 new_srate = sh_audio->samplerate * opts->playback_speed;
1388 if (new_srate != ao_data->samplerate) {
1389 // limits are taken from libaf/af_resample.c
1390 if (new_srate < 8000)
1391 new_srate = 8000;
1392 if (new_srate > 192000)
1393 new_srate = 192000;
1394 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1397 result = init_audio_filters(sh_audio, new_srate,
1398 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1399 mpctx->mixer.afilter = sh_audio->afilter;
1400 return result;
1404 typedef struct mp_osd_msg mp_osd_msg_t;
1405 struct mp_osd_msg {
1406 /// Previous message on the stack.
1407 mp_osd_msg_t* prev;
1408 /// Message text.
1409 char msg[128];
1410 int id,level,started;
1411 /// Display duration in ms.
1412 unsigned time;
1415 /// OSD message stack.
1416 static mp_osd_msg_t* osd_msg_stack = NULL;
1419 * \brief Add a message on the OSD message stack
1421 * If a message with the same id is already present in the stack
1422 * it is pulled on top of the stack, otherwise a new message is created.
1425 static void set_osd_msg_va(int id, int level, int time, const char *fmt,
1426 va_list ap)
1428 mp_osd_msg_t *msg,*last=NULL;
1429 int r;
1431 // look if the id is already in the stack
1432 for(msg = osd_msg_stack ; msg && msg->id != id ;
1433 last = msg, msg = msg->prev);
1434 // not found: alloc it
1435 if(!msg) {
1436 msg = calloc(1,sizeof(mp_osd_msg_t));
1437 msg->prev = osd_msg_stack;
1438 osd_msg_stack = msg;
1439 } else if(last) { // found, but it's not on top of the stack
1440 last->prev = msg->prev;
1441 msg->prev = osd_msg_stack;
1442 osd_msg_stack = msg;
1444 // write the msg
1445 r = vsnprintf(msg->msg, 128, fmt, ap);
1446 if(r >= 128) msg->msg[127] = 0;
1447 // set id and time
1448 msg->id = id;
1449 msg->level = level;
1450 msg->time = time;
1454 void set_osd_msg(int id, int level, int time, const char *fmt, ...)
1456 va_list ap;
1457 va_start(ap, fmt);
1458 set_osd_msg_va(id, level, time, fmt, ap);
1459 va_end(ap);
1462 void set_osd_tmsg(int id, int level, int time, const char *fmt, ...)
1464 va_list ap;
1465 va_start(ap, fmt);
1466 set_osd_msg_va(id, level, time, mp_gtext(fmt), ap);
1467 va_end(ap);
1472 * \brief Remove a message from the OSD stack
1474 * This function can be used to get rid of a message right away.
1478 void rm_osd_msg(int id) {
1479 mp_osd_msg_t *msg,*last=NULL;
1481 // Search for the msg
1482 for(msg = osd_msg_stack ; msg && msg->id != id ;
1483 last = msg, msg = msg->prev);
1484 if(!msg) return;
1486 // Detach it from the stack and free it
1487 if(last)
1488 last->prev = msg->prev;
1489 else
1490 osd_msg_stack = msg->prev;
1491 free(msg);
1495 * \brief Remove all messages from the OSD stack
1499 static void clear_osd_msgs(void) {
1500 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1501 while(msg) {
1502 prev = msg->prev;
1503 free(msg);
1504 msg = prev;
1506 osd_msg_stack = NULL;
1510 * \brief Get the current message from the OSD stack.
1512 * This function decrements the message timer and destroys the old ones.
1513 * The message that should be displayed is returned (if any).
1517 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1519 struct MPOpts *opts = &mpctx->opts;
1520 mp_osd_msg_t *msg,*prev,*last = NULL;
1521 static unsigned last_update = 0;
1522 unsigned now = GetTimerMS();
1523 unsigned diff;
1524 char hidden_dec_done = 0;
1526 if (mpctx->osd_visible) {
1527 // 36000000 means max timed visibility is 1 hour into the future, if
1528 // the difference is greater assume it's wrapped around from below 0
1529 if (mpctx->osd_visible - now > 36000000) {
1530 mpctx->osd_visible = 0;
1531 vo_osd_progbar_type = -1; // disable
1532 vo_osd_changed(OSDTYPE_PROGBAR);
1533 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1536 if (mpctx->osd_show_percentage_until - now > 36000000)
1537 mpctx->osd_show_percentage_until = 0;
1539 if(!last_update) last_update = now;
1540 diff = now >= last_update ? now - last_update : 0;
1542 last_update = now;
1544 // Look for the first message in the stack with high enough level.
1545 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1546 prev = msg->prev;
1547 if (msg->level > opts->osd_level && hidden_dec_done)
1548 continue;
1549 // The message has a high enough level or it is the first hidden one
1550 // in both cases we decrement the timer or kill it.
1551 if(!msg->started || msg->time > diff) {
1552 if(msg->started) msg->time -= diff;
1553 else msg->started = 1;
1554 // display it
1555 if (msg->level <= opts->osd_level)
1556 return msg;
1557 hidden_dec_done = 1;
1558 continue;
1560 // kill the message
1561 free(msg);
1562 if(last) {
1563 last->prev = prev;
1564 msg = last;
1565 } else {
1566 osd_msg_stack = prev;
1567 msg = NULL;
1570 // Nothing found
1571 return NULL;
1575 * \brief Display the OSD bar.
1577 * Display the OSD bar or fall back on a simple message.
1581 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1582 struct MPOpts *opts = &mpctx->opts;
1583 if (opts->osd_level < 1)
1584 return;
1586 if(mpctx->sh_video) {
1587 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1588 vo_osd_progbar_type = type;
1589 vo_osd_progbar_value = 256*(val-min)/(max-min);
1590 vo_osd_changed(OSDTYPE_PROGBAR);
1591 return;
1594 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1595 name, ROUND(100*(val-min)/(max-min)));
1599 * \brief Display text subtitles on the OSD
1601 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs)
1603 int i;
1604 vo_sub = subs;
1605 vo_osd_changed(OSDTYPE_SUBTITLE);
1606 if (!mpctx->sh_video) {
1607 // reverse order, since newest set_osd_msg is displayed first
1608 for (i = SUB_MAX_TEXT - 1; i >= 0; i--) {
1609 if (!subs || i >= subs->lines || !subs->text[i])
1610 rm_osd_msg(OSD_MSG_SUB_BASE + i);
1611 else {
1612 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1613 int display_time = i == subs->lines - 1 ? 180000 : 2000;
1614 set_osd_msg(OSD_MSG_SUB_BASE + i, 1, display_time, "%s", subs->text[i]);
1621 * \brief Update the OSD message line.
1623 * This function displays the current message on the vo OSD or on the term.
1624 * If the stack is empty and the OSD level is high enough the timer
1625 * is displayed (only on the vo OSD).
1629 static void update_osd_msg(struct MPContext *mpctx)
1631 struct MPOpts *opts = &mpctx->opts;
1632 mp_osd_msg_t *msg;
1633 struct osd_state *osd = mpctx->osd;
1634 char osd_text_timer[128];
1636 if (mpctx->add_osd_seek_info) {
1637 double percentage;
1638 if (mpctx->timeline && mpctx->sh_video)
1639 percentage = mpctx->sh_video->pts * 100 /
1640 mpctx->timeline[mpctx->num_timeline_parts].start;
1641 else
1642 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1643 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1644 if (mpctx->sh_video)
1645 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1646 mpctx->add_osd_seek_info = false;
1649 // Look if we have a msg
1650 if((msg = get_osd_msg(mpctx))) {
1651 if (strcmp(osd->osd_text, msg->msg)) {
1652 strncpy(osd->osd_text, msg->msg, 127);
1653 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1654 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1656 return;
1659 if(mpctx->sh_video) {
1660 // fallback on the timer
1661 if (opts->osd_level >= 2) {
1662 int len;
1663 if (mpctx->timeline)
1664 len = mpctx->timeline[mpctx->num_timeline_parts].start;
1665 else
1666 len = demuxer_get_time_length(mpctx->demuxer);
1667 int percentage = -1;
1668 char percentage_text[10];
1669 int pts = demuxer_get_current_time(mpctx->demuxer);
1671 if (mpctx->osd_show_percentage_until)
1672 if (mpctx->timeline)
1673 percentage = mpctx->sh_video->pts * 100 /
1674 mpctx->timeline[mpctx->num_timeline_parts].start;
1675 else
1676 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1678 if (percentage >= 0)
1679 snprintf(percentage_text, 9, " (%d%%)", percentage);
1680 else
1681 percentage_text[0] = 0;
1683 if (opts->osd_level == 3)
1684 snprintf(osd_text_timer, 63,
1685 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1686 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1687 len/3600,(len/60)%60,len%60,percentage_text);
1688 else
1689 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1690 mpctx->osd_function,pts/3600,(pts/60)%60,
1691 pts%60,percentage_text);
1692 } else
1693 osd_text_timer[0]=0;
1695 if (strcmp(osd->osd_text, osd_text_timer)) {
1696 strncpy(osd->osd_text, osd_text_timer, 63);
1697 vo_osd_changed(OSDTYPE_OSD);
1699 return;
1702 // Clear the term osd line
1703 if (term_osd && osd->osd_text[0]) {
1704 osd->osd_text[0] = 0;
1705 printf("%s\n",term_osd_esc);
1709 ///@}
1710 // OSDMsgStack
1713 void reinit_audio_chain(struct MPContext *mpctx)
1715 struct MPOpts *opts = &mpctx->opts;
1716 if (!mpctx->sh_audio)
1717 return;
1718 current_module="init_audio_codec";
1719 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1720 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1721 goto init_error;
1723 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1724 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1727 current_module="af_preinit";
1728 ao_data.samplerate=force_srate;
1729 ao_data.channels=0;
1730 ao_data.format=audio_output_format;
1731 #if 1
1732 // first init to detect best values
1733 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1734 // input:
1735 mpctx->sh_audio->samplerate,
1736 // output:
1737 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1738 mp_tmsg(MSGT_CPLAYER,MSGL_ERR, "Error at audio filter chain "
1739 "pre-init!\n");
1740 exit_player(mpctx, EXIT_ERROR);
1742 #endif
1743 current_module="ao2_init";
1744 mpctx->audio_out = init_best_audio_out(opts->audio_driver_list,
1745 0, // plugin flag
1746 ao_data.samplerate,
1747 ao_data.channels,
1748 ao_data.format, 0);
1749 if(!mpctx->audio_out){
1750 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Could not open/initialize audio device -> no sound.\n");
1751 goto init_error;
1753 mpctx->initialized_flags|=INITIALIZED_AO;
1754 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1755 mpctx->audio_out->info->short_name,
1756 ao_data.samplerate, ao_data.channels,
1757 af_fmt2str_short(ao_data.format),
1758 af_fmt2bits(ao_data.format)/8 );
1759 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1760 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1761 if(strlen(mpctx->audio_out->info->comment) > 0)
1762 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1763 // init audio filters:
1764 #if 1
1765 current_module="af_init";
1766 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1767 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter/ao format!\n");
1768 goto init_error;
1770 #endif
1771 mpctx->mixer.audio_out = mpctx->audio_out;
1772 mpctx->mixer.volstep = volstep;
1773 return;
1775 init_error:
1776 uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec and possibly AO
1777 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1778 mpctx->d_audio->id = -2;
1782 ///@}
1783 // Command2Property
1786 // Return pts value corresponding to the end point of audio written to the
1787 // ao so far.
1788 static double written_audio_pts(struct MPContext *mpctx)
1790 sh_audio_t *sh_audio = mpctx->sh_audio;
1791 demux_stream_t *d_audio = mpctx->d_audio;
1792 double buffered_output;
1793 // first calculate the end pts of audio that has been output by decoder
1794 double a_pts = sh_audio->pts;
1795 if (a_pts != MP_NOPTS_VALUE)
1796 // Good, decoder supports new way of calculating audio pts.
1797 // sh_audio->pts is the timestamp of the latest input packet with
1798 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1799 // the amount of bytes the decoder has written after that timestamp.
1800 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1801 else {
1802 // Decoder doesn't support new way of calculating pts (or we're
1803 // being called before it has decoded anything with known timestamp).
1804 // Use the old method of audio pts calculation: take the timestamp
1805 // of last packet with known pts the decoder has read data from,
1806 // and add amount of bytes read after the beginning of that packet
1807 // divided by input bps. This will be inaccurate if the input/output
1808 // ratio is not constant for every audio packet or if it is constant
1809 // but not accurately known in sh_audio->i_bps.
1811 a_pts = d_audio->pts;
1812 // ds_tell_pts returns bytes read after last timestamp from
1813 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1814 // it has read but not decoded
1815 if (sh_audio->i_bps)
1816 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1817 (double)sh_audio->i_bps;
1819 // Now a_pts hopefully holds the pts for end of audio from decoder.
1820 // Substract data in buffers between decoder and audio out.
1822 // Decoded but not filtered
1823 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1825 // Data buffered in audio filters, measured in bytes of "missing" output
1826 buffered_output = af_calc_delay(sh_audio->afilter);
1828 // Data that was ready for ao but was buffered because ao didn't fully
1829 // accept everything to internal buffers yet
1830 buffered_output += sh_audio->a_out_buffer_len;
1832 // Filters divide audio length by playback_speed, so multiply by it
1833 // to get the length in original units without speedup or slowdown
1834 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1836 return a_pts + mpctx->video_offset;
1839 // Return pts value corresponding to currently playing audio.
1840 double playing_audio_pts(struct MPContext *mpctx)
1842 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1843 mpctx->audio_out->get_delay();
1846 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1847 struct MPOpts *opts = &mpctx->opts;
1848 // check for frame-drop:
1849 current_module = "check_framedrop";
1850 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1851 static int dropped_frames;
1852 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1853 float d = delay-mpctx->delay;
1854 ++total_frame_cnt;
1855 // we should avoid dropping too many frames in sequence unless we
1856 // are too late. and we allow 100ms A-V delay here:
1857 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1858 && !mpctx->update_video_immediately) {
1859 ++drop_frame_cnt;
1860 ++dropped_frames;
1861 return frame_dropping;
1862 } else
1863 dropped_frames = 0;
1865 return 0;
1869 #ifdef HAVE_RTC
1870 int rtc_fd = -1;
1871 #endif
1873 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1875 #ifdef HAVE_RTC
1876 if (rtc_fd >= 0){
1877 // -------- RTC -----------
1878 current_module="sleep_rtc";
1879 while (time_frame > 0.000) {
1880 unsigned long rtc_ts;
1881 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1882 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC read error: %s\n", strerror(errno));
1883 time_frame -= get_relative_time(mpctx);
1885 } else
1886 #endif
1888 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1889 // unnecessarily high CPU usage
1890 float margin = softsleep ? 0.011 : 0;
1891 current_module = "sleep_timer";
1892 while (time_frame > margin) {
1893 usec_sleep(1000000 * (time_frame - margin));
1894 time_frame -= get_relative_time(mpctx);
1896 if (softsleep){
1897 current_module = "sleep_soft";
1898 if (time_frame < 0)
1899 mp_tmsg(MSGT_AVSYNC, MSGL_WARN, "Warning! Softsleep underflow!\n");
1900 while (time_frame > 0)
1901 time_frame -= get_relative_time(mpctx); // burn the CPU
1904 return time_frame;
1907 static void select_subtitle(MPContext *mpctx)
1909 struct MPOpts *opts = &mpctx->opts;
1910 // find the best sub to use
1911 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
1912 mpctx->global_sub_pos = -1; // no subs by default
1913 if (vobsub_index_id >= 0) {
1914 // if user asks for a vobsub id, use that first.
1915 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
1916 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1917 // if user asks for a dvd sub id, use that next.
1918 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1919 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
1920 // if there are text subs to use, use those. (autosubs come last here)
1921 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
1922 } else if (opts->sub_id == -1 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1923 // finally select subs by language and container hints
1924 if (opts->sub_id == -1 && dvdsub_lang)
1925 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
1926 if (opts->sub_id == -1)
1927 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
1928 if (opts->sub_id >= 0)
1929 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1931 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
1932 mpctx->global_sub_pos--;
1933 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
1936 #ifdef CONFIG_DVDNAV
1937 #ifndef FF_B_TYPE
1938 #define FF_B_TYPE 3
1939 #endif
1940 /// store decoded video image
1941 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1942 mp_image_t *from_mpi) {
1943 mp_image_t *mpi;
1945 /// Do not store B-frames
1946 if (from_mpi->pict_type == FF_B_TYPE)
1947 return to_mpi;
1949 if (to_mpi &&
1950 to_mpi->w == from_mpi->w &&
1951 to_mpi->h == from_mpi->h &&
1952 to_mpi->imgfmt == from_mpi->imgfmt)
1953 mpi = to_mpi;
1954 else {
1955 if (to_mpi)
1956 free_mp_image(to_mpi);
1957 if (from_mpi->w == 0 || from_mpi->h == 0)
1958 return NULL;
1959 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1962 copy_mpi(mpi,from_mpi);
1963 return mpi;
1966 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1967 struct MPOpts *opts = &ctx->opts;
1968 if (ctx->sh_video) {
1969 /// clear video pts
1970 ctx->d_video->pts = 0.0f;
1971 ctx->sh_video->pts = 0.0f;
1972 ctx->sh_video->i_pts = 0.0f;
1973 ctx->sh_video->last_pts = 0.0f;
1974 ctx->sh_video->num_buffered_pts = 0;
1975 ctx->sh_video->num_frames = 0;
1976 ctx->sh_video->num_frames_decoded = 0;
1977 ctx->sh_video->timer = 0.0f;
1978 ctx->sh_video->stream_delay = 0.0f;
1979 ctx->sh_video->timer = 0;
1980 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1983 if (ctx->sh_audio) {
1984 /// free audio packets and reset
1985 ds_free_packs(ctx->d_audio);
1986 audio_delay -= ctx->sh_audio->stream_delay;
1987 ctx->delay =- audio_delay;
1988 ctx->audio_out->reset();
1989 resync_audio_stream(ctx->sh_audio);
1992 audio_delay = 0.0f;
1993 ctx->global_sub_size = ctx->global_sub_indices[SUB_SOURCE_DEMUX] + mp_dvdnav_number_of_subs(ctx->stream);
1994 if (dvdsub_lang && opts->sub_id == dvdsub_lang_id) {
1995 dvdsub_lang_id = mp_dvdnav_sid_from_lang(ctx->stream, dvdsub_lang);
1996 if (dvdsub_lang_id != opts->sub_id) {
1997 opts->sub_id = dvdsub_lang_id;
1998 select_subtitle(ctx);
2002 /// clear all EOF related flags
2003 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
2006 /// Restore last decoded DVDNAV (still frame)
2007 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
2008 int *in_size,
2009 unsigned char **start,
2010 mp_image_t *decoded_frame)
2012 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
2013 return decoded_frame;
2015 /// a change occured in dvdnav stream
2016 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
2017 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
2018 mp_dvdnav_context_free(mpctx);
2019 mp_dvdnav_reset_stream(mpctx);
2020 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
2021 mp_dvdnav_cell_has_changed(mpctx->stream,1);
2024 if (*in_size < 0) {
2025 float len;
2027 /// Display still frame, if any
2028 if (mpctx->nav_smpi && !mpctx->nav_buffer)
2029 decoded_frame = mpctx->nav_smpi;
2031 /// increment video frame : continue playing after still frame
2032 len = demuxer_get_time_length(mpctx->demuxer);
2033 if (mpctx->sh_video->pts >= len &&
2034 mpctx->sh_video->pts > 0.0 && len > 0.0) {
2035 mp_dvdnav_skip_still(mpctx->stream);
2036 mp_dvdnav_skip_wait(mpctx->stream);
2038 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
2040 if (mpctx->nav_buffer) {
2041 *start = mpctx->nav_start;
2042 *in_size = mpctx->nav_in_size;
2043 if (mpctx->nav_start)
2044 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
2048 return decoded_frame;
2051 /// Save last decoded DVDNAV (still frame)
2052 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
2053 unsigned char *start,
2054 mp_image_t *decoded_frame)
2056 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
2057 return;
2059 if (mpctx->nav_buffer)
2060 free(mpctx->nav_buffer);
2062 mpctx->nav_buffer = malloc(in_size);
2063 mpctx->nav_start = start;
2064 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
2065 if (mpctx->nav_buffer)
2066 memcpy(mpctx->nav_buffer,start,in_size);
2068 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
2069 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
2071 #endif /* CONFIG_DVDNAV */
2073 /* Modify video timing to match the audio timeline. There are two main
2074 * reasons this is needed. First, video and audio can start from different
2075 * positions at beginning of file or after a seek (MPlayer starts both
2076 * immediately even if they have different pts). Second, the file can have
2077 * audio timestamps that are inconsistent with the duration of the audio
2078 * packets, for example two consecutive timestamp values differing by
2079 * one second but only a packet with enough samples for half a second
2080 * of playback between them.
2082 static void adjust_sync(struct MPContext *mpctx, double frame_time)
2084 current_module = "av_sync";
2086 if (!mpctx->sh_audio)
2087 return;
2089 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
2090 double v_pts = mpctx->sh_video->pts;
2091 double av_delay = a_pts - v_pts;
2092 // Try to sync vo_flip() so it will *finish* at given time
2093 av_delay += mpctx->last_vo_flip_duration;
2094 av_delay -= audio_delay; // This much pts difference is desired
2096 double change = av_delay * 0.1;
2097 double max_change = default_max_pts_correction >= 0 ?
2098 default_max_pts_correction : frame_time * 0.1;
2099 if (change < -max_change)
2100 change = -max_change;
2101 else if (change > max_change)
2102 change = max_change;
2103 mpctx->delay += change;
2104 mpctx->total_avsync_change += change;
2107 static int fill_audio_out_buffers(struct MPContext *mpctx)
2109 struct MPOpts *opts = &mpctx->opts;
2110 unsigned int t;
2111 double tt;
2112 int playsize;
2113 int playflags=0;
2114 int audio_eof=0;
2115 sh_audio_t * const sh_audio = mpctx->sh_audio;
2117 current_module="play_audio";
2119 while (1) {
2120 int sleep_time;
2121 // all the current uses of ao_data.pts seem to be in aos that handle
2122 // sync completely wrong; there should be no need to use ao_data.pts
2123 // in get_space()
2124 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2125 playsize = mpctx->audio_out->get_space();
2126 if (mpctx->sh_video || playsize >= ao_data.outburst)
2127 break;
2129 // handle audio-only case:
2130 // this is where mplayer sleeps during audio-only playback
2131 // to avoid 100% CPU use
2132 sleep_time = (ao_data.outburst - playsize) * 1000 / ao_data.bps;
2133 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
2134 usec_sleep(sleep_time * 1000);
2137 // Fill buffer if needed:
2138 current_module="decode_audio";
2139 t = GetTimer();
2140 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2141 if (mpctx->d_audio->eof) {
2142 audio_eof = 1;
2143 if (sh_audio->a_out_buffer_len == 0)
2144 return 0;
2146 t = GetTimer() - t;
2147 tt = t*0.000001f; audio_time_usage+=tt;
2148 if (playsize > sh_audio->a_out_buffer_len) {
2149 playsize = sh_audio->a_out_buffer_len;
2150 if (audio_eof)
2151 playflags |= AOPLAY_FINAL_CHUNK;
2153 if (!playsize)
2154 return 1;
2156 // play audio:
2157 current_module="play_audio";
2159 // Is this pts value actually useful for the aos that access it?
2160 // They're obviously badly broken in the way they handle av sync;
2161 // would not having access to this make them more broken?
2162 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2163 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2165 if (playsize > 0) {
2166 sh_audio->a_out_buffer_len -= playsize;
2167 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2168 sh_audio->a_out_buffer_len);
2169 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2171 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2172 // Sanity check to avoid hanging in case current ao doesn't output
2173 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2174 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2175 sh_audio->a_out_buffer_len = 0;
2178 return 1;
2181 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2182 float *aq_sleep_time)
2184 struct MPOpts *opts = &mpctx->opts;
2185 int frame_time_remaining = 0;
2186 current_module="calc_sleep_time";
2188 *time_frame -= get_relative_time(mpctx); // reset timer
2190 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2191 float delay = mpctx->audio_out->get_delay();
2192 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2194 if (autosync) {
2196 * Adjust this raw delay value by calculating the expected
2197 * delay for this frame and generating a new value which is
2198 * weighted between the two. The higher autosync is, the
2199 * closer to the delay value gets to that which "-nosound"
2200 * would have used, and the longer it will take for A/V
2201 * sync to settle at the right value (but it eventually will.)
2202 * This settling time is very short for values below 100.
2204 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2205 float difference = delay - predicted;
2206 delay = predicted + difference / (float)autosync;
2209 *time_frame = delay - mpctx->delay / opts->playback_speed;
2211 // delay = amount of audio buffered in soundcard/driver
2212 if (delay > 0.25) delay=0.25; else
2213 if (delay < 0.10) delay=0.10;
2214 if (*time_frame > delay*0.6) {
2215 // sleep time too big - may cause audio drops (buffer underrun)
2216 frame_time_remaining = 1;
2217 *time_frame = delay*0.5;
2219 } else {
2220 // If we're lagging more than 200 ms behind the right playback rate,
2221 // don't try to "catch up".
2222 // If benchmark is set always output frames as fast as possible
2223 // without sleeping.
2224 if (*time_frame < -0.2 || benchmark)
2225 *time_frame = 0;
2228 *aq_sleep_time += *time_frame;
2231 //============================== SLEEP: ===================================
2233 *time_frame -= mpctx->video_out->flip_queue_offset;
2234 // flag 256 means: libvo driver does its timing (dvb card)
2235 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2236 *time_frame = timing_sleep(mpctx, *time_frame);
2237 *time_frame += mpctx->video_out->flip_queue_offset;
2238 return frame_time_remaining;
2241 int reinit_video_chain(struct MPContext *mpctx)
2243 struct MPOpts *opts = &mpctx->opts;
2244 sh_video_t * const sh_video = mpctx->sh_video;
2245 double ar=-1.0;
2246 //================== Init VIDEO (codec & libvo) ==========================
2247 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2248 current_module="preinit_libvo";
2250 //shouldn't we set dvideo->id=-2 when we fail?
2251 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2252 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2253 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Error opening/initializing the selected video_out (-vo) device.\n");
2254 goto err_out;
2256 mpctx->initialized_flags|=INITIALIZED_VO;
2259 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2260 mpctx->sh_video->stream_aspect = ar;
2261 current_module="init_video_filters";
2263 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2264 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2266 #ifdef CONFIG_MENU
2267 if(use_menu) {
2268 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2269 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2270 if(!vf_menu) {
2271 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Can't open libmenu video filter with root menu %s.\n",menu_root);
2272 use_menu = 0;
2275 if(vf_menu)
2276 sh_video->vfilter = vf_menu;
2277 #endif
2279 #ifdef CONFIG_ASS
2280 if(opts->ass_enabled) {
2281 int i;
2282 int insert = 1;
2283 if (opts->vf_settings)
2284 for (i = 0; opts->vf_settings[i].name; ++i)
2285 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2286 insert = 0;
2287 break;
2289 if (insert) {
2290 extern vf_info_t vf_info_ass;
2291 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2292 char* vf_arg[] = {"auto", "1", NULL};
2293 int retcode = 0;
2294 struct vf_instance *vf_ass = vf_open_plugin_noerr(opts, libass_vfs,
2295 sh_video->vfilter,
2296 "ass", vf_arg,
2297 &retcode);
2298 if (vf_ass)
2299 sh_video->vfilter = vf_ass;
2300 else if (retcode == -1) // vf_ass open() returns -1 if there's VO EOSD
2301 mp_msg(MSGT_CPLAYER, MSGL_V, "[ass] vf_ass not needed\n");
2302 else
2303 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2306 #endif
2308 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2310 #ifdef CONFIG_ASS
2311 if (opts->ass_enabled)
2312 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2313 #endif
2315 current_module="init_video_codec";
2317 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2318 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2319 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2321 if(!sh_video->initialized){
2322 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2323 goto err_out;
2326 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2328 if (sh_video->codec)
2329 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2331 sh_video->last_pts = MP_NOPTS_VALUE;
2332 sh_video->num_buffered_pts = 0;
2333 sh_video->next_frame_time = 0;
2335 if(auto_quality>0){
2336 // Auto quality option enabled
2337 output_quality=get_video_quality_max(sh_video);
2338 if(auto_quality>output_quality) auto_quality=output_quality;
2339 else output_quality=auto_quality;
2340 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2341 set_video_quality(sh_video,output_quality);
2344 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2346 current_module="init_vo";
2348 return 1;
2350 err_out:
2351 mpctx->sh_video = mpctx->d_video->sh = NULL;
2352 return 0;
2355 static double update_video_nocorrect_pts(struct MPContext *mpctx)
2357 struct sh_video *sh_video = mpctx->sh_video;
2358 double frame_time = 0;
2359 struct vo *video_out = mpctx->video_out;
2360 while (!video_out->frame_loaded) {
2361 current_module = "filter_video";
2362 // In nocorrect-pts mode there is no way to properly time these frames
2363 if (vo_get_buffered_frame(video_out, 0) >= 0)
2364 break;
2365 if (vf_output_queued_frame(sh_video->vfilter))
2366 continue;
2367 unsigned char *packet = NULL;
2368 frame_time = sh_video->next_frame_time;
2369 if (mpctx->update_video_immediately)
2370 frame_time = 0;
2371 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2372 &packet, force_fps);
2373 if (in_size < 0) {
2374 #ifdef CONFIG_DVDNAV
2375 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2376 if (mp_dvdnav_is_eof(mpctx->stream))
2377 return -1;
2378 if (mpctx->d_video)
2379 mpctx->d_video->eof = 0;
2380 if (mpctx->d_audio)
2381 mpctx->d_audio->eof = 0;
2382 mpctx->stream->eof = 0;
2383 } else
2384 #endif
2385 return -1;
2387 if (in_size > max_framesize)
2388 max_framesize = in_size;
2389 sh_video->timer += frame_time;
2390 if (mpctx->sh_audio)
2391 mpctx->delay -= frame_time;
2392 // video_read_frame can change fps (e.g. for ASF video)
2393 vo_fps = sh_video->fps;
2394 int framedrop_type = check_framedrop(mpctx, frame_time);
2395 current_module = "decode video";
2397 void *decoded_frame;
2398 #ifdef CONFIG_DVDNAV
2399 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2400 if (in_size >= 0 && !decoded_frame)
2401 #endif
2402 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2403 sh_video->pts);
2404 #ifdef CONFIG_DVDNAV
2405 // Save last still frame for future display
2406 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2407 #endif
2408 if (decoded_frame) {
2409 current_module = "filter video";
2410 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2411 if (!video_out->config_ok)
2412 break;
2415 return frame_time;
2418 static void determine_frame_pts(struct MPContext *mpctx)
2420 struct sh_video *sh_video = mpctx->sh_video;
2421 struct MPOpts *opts = &mpctx->opts;
2423 if (opts->user_pts_assoc_mode) {
2424 sh_video->pts_assoc_mode = opts->user_pts_assoc_mode;
2425 } else if (sh_video->pts_assoc_mode == 0) {
2426 if (sh_video->codec_reordered_pts != MP_NOPTS_VALUE)
2427 sh_video->pts_assoc_mode = 1;
2428 else
2429 sh_video->pts_assoc_mode = 2;
2430 } else {
2431 int probcount1 = sh_video->num_reordered_pts_problems;
2432 int probcount2 = sh_video->num_sorted_pts_problems;
2433 if (sh_video->pts_assoc_mode == 2) {
2434 int tmp = probcount1;
2435 probcount1 = probcount2;
2436 probcount2 = tmp;
2438 if (probcount1 >= probcount2 * 1.5 + 2) {
2439 sh_video->pts_assoc_mode = 3 - sh_video->pts_assoc_mode;
2440 mp_msg(MSGT_CPLAYER, MSGL_V, "Switching to pts association mode "
2441 "%d.\n", sh_video->pts_assoc_mode);
2444 sh_video->pts = sh_video->pts_assoc_mode == 1 ?
2445 sh_video->codec_reordered_pts : sh_video->sorted_pts;
2448 static double update_video(struct MPContext *mpctx)
2450 struct sh_video *sh_video = mpctx->sh_video;
2451 struct vo *video_out = mpctx->video_out;
2452 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2453 mpctx->osd); // hack for vf_expand
2454 if (!mpctx->opts.correct_pts)
2455 return update_video_nocorrect_pts(mpctx);
2457 double pts;
2459 bool hit_eof = false;
2460 while (!video_out->frame_loaded) {
2461 current_module = "filter_video";
2462 if (vo_get_buffered_frame(video_out, hit_eof) >= 0)
2463 break;
2464 // XXX Time used in this call is not counted in any performance
2465 // timer now, OSD time is not updated correctly for filter-added frames
2466 if (vf_output_queued_frame(sh_video->vfilter))
2467 continue;
2468 if (hit_eof)
2469 return -1;
2470 unsigned char *packet = NULL;
2471 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2472 if (pts != MP_NOPTS_VALUE)
2473 pts += mpctx->video_offset;
2474 if (in_size < 0) {
2475 // try to extract last frames in case of decoder lag
2476 in_size = 0;
2477 pts = 1e300;
2478 hit_eof = true;
2480 if (in_size > max_framesize)
2481 max_framesize = in_size;
2482 current_module = "decode video";
2483 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2484 void *decoded_frame = decode_video(sh_video, packet, in_size,
2485 framedrop_type, pts);
2486 if (decoded_frame) {
2487 determine_frame_pts(mpctx);
2488 current_module = "filter video";
2489 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2490 if (!video_out->config_ok)
2491 break; // We'd likely hang in this loop otherwise
2495 pts = video_out->next_pts;
2496 if (pts == MP_NOPTS_VALUE) {
2497 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2498 // Try to use decoder pts from before filters
2499 pts = sh_video->pts;
2500 if (pts == MP_NOPTS_VALUE)
2501 pts = sh_video->last_pts;
2503 sh_video->pts = pts;
2504 if (sh_video->last_pts == MP_NOPTS_VALUE)
2505 sh_video->last_pts = sh_video->pts;
2506 else if (sh_video->last_pts > sh_video->pts) {
2507 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2508 sh_video->pts, sh_video->last_pts);
2509 /* If the difference in pts is small treat it as jitter around the
2510 * right value (possibly caused by incorrect timestamp ordering) and
2511 * just show this frame immediately after the last one.
2512 * Treat bigger differences as timestamp resets and start counting
2513 * timing of later frames from the position of this one. */
2514 if (sh_video->last_pts - sh_video->pts > 0.5)
2515 sh_video->last_pts = sh_video->pts;
2516 else
2517 sh_video->pts = sh_video->last_pts;
2519 double frame_time = sh_video->pts - sh_video->last_pts;
2520 sh_video->last_pts = sh_video->pts;
2521 sh_video->timer += frame_time;
2522 if (mpctx->sh_audio)
2523 mpctx->delay -= frame_time;
2524 return frame_time;
2527 void pause_player(struct MPContext *mpctx)
2529 if (mpctx->paused)
2530 return;
2531 mpctx->paused = 1;
2532 mpctx->step_frames = 0;
2533 mpctx->time_frame -= get_relative_time(mpctx);
2535 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2536 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2538 if (mpctx->audio_out && mpctx->sh_audio)
2539 mpctx->audio_out->pause(); // pause audio, keep data if possible
2542 void unpause_player(struct MPContext *mpctx)
2544 if (!mpctx->paused)
2545 return;
2546 mpctx->paused = 0;
2548 if (mpctx->audio_out && mpctx->sh_audio)
2549 mpctx->audio_out->resume(); // resume audio
2550 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2551 && !mpctx->step_frames)
2552 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2553 (void)get_relative_time(mpctx); // ignore time that passed during pause
2556 void add_step_frame(struct MPContext *mpctx)
2558 mpctx->step_frames++;
2559 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2560 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2561 unpause_player(mpctx);
2564 static void pause_loop(struct MPContext *mpctx)
2566 mp_cmd_t* cmd;
2567 if (!quiet) {
2568 // Small hack to display the pause message on the OSD line.
2569 // The pause string is: "\n == PAUSE == \r" so we need to
2570 // take the first and the last char out
2571 if (term_osd && !mpctx->sh_video) {
2572 char msg[128] = _("\n ===== PAUSE =====\r");
2573 int mlen = strlen(msg);
2574 msg[mlen-1] = '\0';
2575 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2576 update_osd_msg(mpctx);
2577 } else
2578 mp_tmsg(MSGT_CPLAYER,MSGL_STATUS,"\n ===== PAUSE =====\r");
2579 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2582 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2583 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2584 if (cmd) {
2585 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2586 run_command(mpctx, cmd);
2587 mp_cmd_free(cmd);
2588 continue;
2590 if (mpctx->sh_video && mpctx->video_out)
2591 vo_check_events(mpctx->video_out);
2592 #ifdef CONFIG_MENU
2593 if (vf_menu)
2594 vf_menu_pause_update(vf_menu);
2595 #endif
2596 usec_sleep(20000);
2597 update_osd_msg(mpctx);
2598 int hack = vo_osd_changed(0);
2599 vo_osd_changed(hack);
2600 if (hack)
2601 break;
2606 // Find the right mute status and record position for new file position
2607 static void edl_seek_reset(MPContext *mpctx)
2609 mpctx->edl_muted = 0;
2610 next_edl_record = edl_records;
2612 while (next_edl_record) {
2613 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2614 break;
2616 if (next_edl_record->action == EDL_MUTE)
2617 mpctx->edl_muted = !mpctx->edl_muted;
2618 next_edl_record = next_edl_record->next;
2620 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2621 mixer_mute(&mpctx->mixer);
2625 // Execute EDL command for the current position if one exists
2626 static void edl_update(MPContext *mpctx)
2628 if (!next_edl_record)
2629 return;
2631 if (!mpctx->sh_video) {
2632 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot use EDL without video, disabling.\n");
2633 free_edl(edl_records);
2634 next_edl_record = NULL;
2635 edl_records = NULL;
2636 return;
2639 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2640 if (next_edl_record->action == EDL_SKIP) {
2641 mpctx->osd_function = OSD_FFW;
2642 mpctx->abs_seek_pos = 0;
2643 mpctx->rel_seek_secs = next_edl_record->length_sec;
2644 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2645 "[%f], length [%f]\n", next_edl_record->start_sec,
2646 next_edl_record->stop_sec, next_edl_record->length_sec);
2648 else if (next_edl_record->action == EDL_MUTE) {
2649 mpctx->edl_muted = !mpctx->edl_muted;
2650 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2651 mixer_mute(&mpctx->mixer);
2652 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2653 next_edl_record->start_sec );
2655 next_edl_record = next_edl_record->next;
2659 static void reinit_decoders(struct MPContext *mpctx)
2661 reinit_video_chain(mpctx);
2662 reinit_audio_chain(mpctx);
2663 mp_property_do("sub", M_PROPERTY_SET, &mpctx->global_sub_pos, mpctx);
2666 static bool timeline_set_part(struct MPContext *mpctx, int i)
2668 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
2669 struct timeline_part *n = mpctx->timeline + i;
2670 mpctx->timeline_part = i;
2671 mpctx->video_offset = n->start - n->source_start;
2672 if (n->source == p->source)
2673 return false;
2674 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo && mpctx->opts.video_id != -2 ? 0 : INITIALIZED_VO) | INITIALIZED_AO | INITIALIZED_ACODEC);
2675 mpctx->demuxer = n->source->demuxer;
2676 mpctx->d_video = mpctx->demuxer->video;
2677 mpctx->d_audio = mpctx->demuxer->audio;
2678 mpctx->d_sub = mpctx->demuxer->sub;
2679 mpctx->sh_video = mpctx->d_video->sh;
2680 mpctx->sh_audio = mpctx->d_audio->sh;
2681 return true;
2684 // Given pts, switch playback to the corresponding part.
2685 // Return offset within that part.
2686 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
2687 bool *need_reset)
2689 if (pts < 0)
2690 pts = 0;
2691 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
2692 struct timeline_part *p = mpctx->timeline + i;
2693 if (pts < (p+1)->start) {
2694 *need_reset = timeline_set_part(mpctx, i);
2695 return pts - p->start + p->source_start;
2698 return -1;
2702 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2703 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2704 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2705 static int seek(MPContext *mpctx, double amount, int style)
2707 current_module = "seek";
2708 if (mpctx->stop_play == AT_END_OF_FILE)
2709 mpctx->stop_play = KEEP_PLAYING;
2710 if (mpctx->timeline && style & SEEK_FACTOR) {
2711 amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
2712 style &= ~SEEK_FACTOR;
2714 if ((mpctx->demuxer->accurate_seek || mpctx->timeline) && mpctx->sh_video
2715 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2716 style |= SEEK_ABSOLUTE;
2717 if (amount > 0)
2718 style |= SEEK_FORWARD;
2719 else
2720 style |= SEEK_BACKWARD;
2721 amount += mpctx->sh_video->pts;
2724 /* At least the liba52 decoder wants to read from the input stream
2725 * during initialization, so reinit must be done after the demux_seek()
2726 * call that clears possible stream EOF. */
2727 bool need_reset = false;
2728 if (mpctx->timeline) {
2729 amount = timeline_set_from_time(mpctx, amount, &need_reset);
2730 if (amount == -1) {
2731 mpctx->stop_play = AT_END_OF_FILE;
2732 // Clear audio from current position
2733 if (mpctx->sh_audio) {
2734 mpctx->audio_out->reset();
2735 mpctx->sh_audio->a_buffer_len = 0;
2736 mpctx->sh_audio->a_out_buffer_len = 0;
2738 return -1;
2741 int seekresult = demux_seek(mpctx->demuxer, amount, audio_delay, style);
2742 if (need_reset)
2743 reinit_decoders(mpctx);
2744 if (seekresult == 0)
2745 return -1;
2747 if (mpctx->sh_video) {
2748 current_module = "seek_video_reset";
2749 resync_video_stream(mpctx->sh_video);
2750 vo_seek_reset(mpctx->video_out);
2751 mpctx->sh_video->num_buffered_pts = 0;
2752 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2753 mpctx->delay = 0;
2754 mpctx->time_frame = 0;
2755 mpctx->update_video_immediately = true;
2756 // Not all demuxers set d_video->pts during seek, so this value
2757 // (which is used by at least vobsub and edl code below) may
2758 // be completely wrong (probably 0).
2759 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
2760 update_subtitles(mpctx, &mpctx->opts, mpctx->sh_video,
2761 mpctx->sh_video->pts, mpctx->video_offset,
2762 mpctx->d_sub, 1);
2763 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2766 if (mpctx->sh_audio) {
2767 current_module = "seek_audio_reset";
2768 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2769 mpctx->sh_audio->a_buffer_len = 0;
2770 mpctx->sh_audio->a_out_buffer_len = 0;
2771 if (!mpctx->sh_video)
2772 update_subtitles(mpctx, &mpctx->opts, NULL, mpctx->sh_audio->pts,
2773 mpctx->video_offset, mpctx->d_sub, 1);
2776 if (vo_vobsub && mpctx->sh_video) {
2777 current_module = "seek_vobsub_reset";
2778 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2781 edl_seek_reset(mpctx);
2783 mpctx->total_avsync_change = 0;
2784 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2785 drop_frame_cnt = 0;
2787 current_module = NULL;
2788 return 0;
2791 int get_current_chapter(struct MPContext *mpctx)
2793 if (!mpctx->chapters || !mpctx->sh_video)
2794 return demuxer_get_current_chapter(mpctx->demuxer);
2796 int i;
2797 double current_pts = mpctx->sh_video->pts;
2798 for (i = 1; i < mpctx->num_chapters; i++)
2799 if (current_pts < mpctx->chapters[i].start)
2800 break;
2801 return i - 1;
2804 // currently returns a string allocated with malloc, not talloc
2805 char *chapter_display_name(struct MPContext *mpctx, int chapter)
2807 if (!mpctx->chapters || !mpctx->sh_video)
2808 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
2809 return strdup(mpctx->chapters[chapter].name);
2812 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
2813 char **chapter_name)
2815 if (!mpctx->chapters || !mpctx->sh_video)
2816 return demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
2817 chapter_name);
2818 if (chapter >= mpctx->num_chapters)
2819 return -1;
2820 if (chapter < 0)
2821 chapter = 0;
2822 *seek_pts = mpctx->chapters[chapter].start;
2823 if (chapter_name)
2824 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
2825 return chapter;
2828 static int find_ordered_chapter_sources(struct MPContext *mpctx,
2829 struct content_source *sources,
2830 int num_sources,
2831 unsigned char uid_map[][16])
2833 int num_filenames = 0;
2834 char **filenames = NULL;
2835 if (num_sources > 1) {
2836 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
2837 "other sources.\n");
2838 if (mpctx->stream->type != STREAMTYPE_FILE) {
2839 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
2840 "normal disk file. Will not search for related files.\n");
2841 } else {
2842 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
2843 "same directory to find referenced sources.\n");
2844 filenames = find_files(mpctx->demuxer->filename, ".mkv",
2845 &num_filenames);
2849 int num_left = num_sources - 1;
2850 for (int i = 0; i < num_filenames && num_left > 0; i++) {
2851 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
2852 filename_recode(filenames[i]));
2853 int format;
2854 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
2855 if (!s)
2856 continue;
2857 struct demuxer *d = demux_open(&mpctx->opts, s, DEMUXER_TYPE_MATROSKA,
2858 mpctx->opts.audio_id,
2859 mpctx->opts.video_id,
2860 mpctx->opts.sub_id, filenames[i]);
2861 if (!d) {
2862 free_stream(s);
2863 continue;
2865 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
2866 for (int i = 1; i < num_sources; i++) {
2867 if (sources[i].demuxer)
2868 continue;
2869 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
2870 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
2871 i, filename_recode(d->filename));
2872 sources[i].stream = s;
2873 sources[i].demuxer = d;
2874 num_left--;
2875 goto match;
2879 free_demuxer(d);
2880 free_stream(s);
2881 continue;
2882 match:
2885 talloc_free(filenames);
2886 if (num_left) {
2887 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
2888 "There will be parts MISSING from the video!\n");
2889 for (int i = 1, j = 1; i < num_sources; i++)
2890 if (sources[i].demuxer) {
2891 sources[j] = sources[i];
2892 memcpy(uid_map[j], uid_map[i], 16);
2893 j++;
2896 return num_sources - num_left;
2899 static void build_ordered_chapter_timeline(struct MPContext *mpctx)
2901 if (!mpctx->opts.ordered_chapters) {
2902 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
2903 "you have disabled support for them. Ignoring.\n");
2904 return;
2907 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
2908 "edit timeline.\n");
2910 struct demuxer *demuxer = mpctx->demuxer;
2911 struct matroska_data *m = &demuxer->matroska_data;
2913 // +1 because sources/uid_map[0] is original file even if all chapters
2914 // actually use other sources and need separate entries
2915 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
2916 m->num_ordered_chapters+1);
2917 sources[0].stream = mpctx->stream;
2918 sources[0].demuxer = mpctx->demuxer;
2919 unsigned char uid_map[m->num_ordered_chapters+1][16];
2920 int num_sources = 1;
2921 memcpy(uid_map[0], m->segment_uid, 16);
2923 for (int i = 0; i < m->num_ordered_chapters; i++) {
2924 struct matroska_chapter *c = m->ordered_chapters + i;
2925 if (!c->has_segment_uid)
2926 memcpy(c->segment_uid, m->segment_uid, 16);
2928 for (int j = 0; j < num_sources; j++)
2929 if (!memcmp(c->segment_uid, uid_map[j], 16))
2930 goto found1;
2931 memcpy(uid_map[num_sources], c->segment_uid, 16);
2932 sources[num_sources] = (struct content_source){};
2933 num_sources++;
2934 found1:
2938 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
2939 uid_map);
2942 // +1 for terminating chapter with start time marking end of last real one
2943 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
2944 m->num_ordered_chapters + 1);
2945 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
2946 m->num_ordered_chapters);
2947 uint64_t starttime = 0;
2948 uint64_t missing_time = 0;
2949 int part_count = 0;
2950 int num_chapters = 0;
2951 uint64_t prev_part_offset;
2952 for (int i = 0; i < m->num_ordered_chapters; i++) {
2953 struct matroska_chapter *c = m->ordered_chapters + i;
2955 int j;
2956 for (j = 0; j < num_sources; j++) {
2957 if (!memcmp(c->segment_uid, uid_map[j], 16))
2958 goto found2;
2960 missing_time += c->end - c->start;
2961 continue;
2962 found2:;
2963 chapters[num_chapters].start = starttime / 1000.;
2964 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
2965 /* Only add a separate part if the time or file actually changes.
2966 * Matroska files have chapter divisions that are redundant from
2967 * timeline point of view because the same chapter structure is used
2968 * both to specify the timeline and for normal chapter information.
2969 * Removing a missing inserted external chapter can also cause this. */
2970 if (part_count == 0 || c->start != starttime + prev_part_offset
2971 || sources + j != timeline[part_count - 1].source) {
2972 timeline[part_count].source = sources + j;
2973 timeline[part_count].start = chapters[num_chapters].start;
2974 timeline[part_count].source_start = c->start / 1000.;
2975 prev_part_offset = c->start - starttime;
2976 part_count++;
2978 starttime += c->end - c->start;
2979 num_chapters++;
2981 timeline[part_count].start = starttime / 1000.;
2983 if (!part_count) {
2984 // None of the parts come from the file itself???
2985 talloc_free(sources);
2986 talloc_free(timeline);
2987 talloc_free(chapters);
2988 return;
2991 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
2992 "sources. Total length %.3f seconds.\n", part_count, num_sources,
2993 timeline[part_count].start);
2994 if (missing_time)
2995 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
2996 "from the timeline!\n", missing_time / 1000.);
2997 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
2998 for (int i = 0; i < num_sources; i++)
2999 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
3000 filename_recode(sources[i].demuxer->filename));
3001 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
3002 "source_start, source):\n");
3003 for (int i = 0; i < part_count; i++) {
3004 struct timeline_part *p = timeline + i;
3005 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
3006 p->source_start, p->source - sources);
3008 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start);
3009 mpctx->sources = sources;
3010 mpctx->num_sources = num_sources;
3011 mpctx->timeline = timeline;
3012 mpctx->num_timeline_parts = part_count;
3013 mpctx->num_chapters = num_chapters;
3014 mpctx->chapters = chapters;
3016 mpctx->timeline_part = 0;
3017 mpctx->demuxer = timeline[0].source->demuxer;
3021 static int read_keys(void *ctx, int fd)
3023 getch2(ctx);
3024 return mplayer_get_key(ctx, 0);
3028 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
3029 * file for some tools to link against. */
3030 #ifndef DISABLE_MAIN
3031 int main(int argc,char* argv[]){
3034 char * mem_ptr;
3036 // movie info:
3038 /* Flag indicating whether MPlayer should exit without playing anything. */
3039 int opt_exit = 0;
3041 //float a_frame=0; // Audio
3043 int i;
3045 struct MPContext *mpctx = &(struct MPContext){
3046 .osd_function = OSD_PLAY,
3047 .begin_skip = MP_NOPTS_VALUE,
3048 .play_tree_step = 1,
3049 .global_sub_pos = -1,
3050 .set_of_sub_pos = -1,
3051 .file_format = DEMUXER_TYPE_UNKNOWN,
3052 .last_dvb_step = 1,
3055 InitTimer();
3056 srand(GetTimerMS());
3058 mp_msg_init();
3059 set_av_log_callback();
3061 #ifdef CONFIG_X11
3062 mpctx->x11_state = vo_x11_init_state();
3063 #endif
3064 struct MPOpts *opts = &mpctx->opts;
3065 set_default_mplayer_options(opts);
3066 // Create the config context and register the options
3067 mpctx->mconfig = m_config_new(opts, cfg_include);
3068 m_config_register_options(mpctx->mconfig,mplayer_opts);
3069 mp_input_register_options(mpctx->mconfig);
3071 // Preparse the command line
3072 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
3074 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
3075 set_path_env();
3076 #endif
3078 #ifdef CONFIG_TV
3079 stream_tv_defaults.immediate = 1;
3080 #endif
3082 parse_cfgfiles(mpctx, mpctx->mconfig);
3084 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
3085 if(mpctx->playtree == NULL)
3086 opt_exit = 1;
3087 else {
3088 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
3089 if(mpctx->playtree) {
3090 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3091 if(mpctx->playtree_iter) {
3092 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3093 play_tree_iter_free(mpctx->playtree_iter);
3094 mpctx->playtree_iter = NULL;
3096 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3100 mpctx->key_fifo = mp_fifo_create(opts);
3102 print_version("MPlayer");
3104 #if defined(__MINGW32__) || defined(__CYGWIN__)
3106 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
3107 BOOL WINAPI (*setDEP)(DWORD) = NULL;
3108 if (kernel32)
3109 setDEP = GetProcAddress(kernel32, "SetProcessDEPPolicy");
3110 if (setDEP) setDEP(3);
3112 // stop Windows from showing all kinds of annoying error dialogs
3113 SetErrorMode(0x8003);
3114 // request 1ms timer resolution
3115 timeBeginPeriod(1);
3116 #endif
3118 #ifdef CONFIG_PRIORITY
3119 set_priority();
3120 #endif
3122 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
3123 list_video_out();
3124 opt_exit = 1;
3127 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
3128 list_audio_out();
3129 opt_exit = 1;
3132 /* Check codecs.conf. */
3133 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3134 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3135 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3136 if(!parse_codec_cfg(NULL)){
3137 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3139 mp_tmsg(MSGT_CPLAYER,MSGL_V,"Using built-in default codecs.conf.\n");
3142 free( mem_ptr ); // release the buffer created by get_path()
3145 #if 0
3146 if(video_codec_list){
3147 int i;
3148 video_codec=video_codec_list[0];
3149 for(i=0;video_codec_list[i];i++)
3150 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
3152 #endif
3153 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3154 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available audio codecs:\n");
3155 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3156 list_codecs(1);
3157 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3158 opt_exit = 1;
3160 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3161 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video codecs:\n");
3162 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3163 list_codecs(0);
3164 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3165 opt_exit = 1;
3167 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3168 vfm_help();
3169 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3170 opt_exit = 1;
3172 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3173 afm_help();
3174 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3175 opt_exit = 1;
3177 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3178 af_help();
3179 printf("\n");
3180 opt_exit = 1;
3182 #ifdef CONFIG_X11
3183 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3184 fstype_help();
3185 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3186 opt_exit = 1;
3188 #endif
3189 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3190 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3191 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3192 demuxer_help();
3193 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3194 opt_exit = 1;
3196 if(list_properties) {
3197 property_print_help();
3198 opt_exit = 1;
3201 if(opt_exit)
3202 exit_player(mpctx, EXIT_NONE);
3204 if(!mpctx->filename && !player_idle_mode){
3205 // no file/vcd/dvd -> show HELP:
3206 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s", mp_gtext(help_text));
3207 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3210 /* Display what configure line was used */
3211 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3213 // Many users forget to include command line in bugreports...
3214 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3215 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "CommandLine:");
3216 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3217 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3220 //------ load global data first ------
3222 mpctx->osd = osd_create();
3224 // check font
3225 #ifdef CONFIG_FREETYPE
3226 init_freetype();
3227 #endif
3228 #ifdef CONFIG_FONTCONFIG
3229 if(font_fontconfig <= 0)
3231 #endif
3232 #ifdef CONFIG_BITMAP_FONT
3233 if(font_name){
3234 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3235 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",
3236 filename_recode(font_name));
3237 } else {
3238 // try default:
3239 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3240 free(mem_ptr); // release the buffer created by get_path()
3241 if(!vo_font)
3242 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3244 if (sub_font_name)
3245 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3246 else
3247 mpctx->osd->sub_font = vo_font;
3248 #endif
3249 #ifdef CONFIG_FONTCONFIG
3251 #endif
3253 #ifdef CONFIG_ASS
3254 ass_library = ass_init();
3255 #endif
3257 #ifdef HAVE_RTC
3258 if(!nortc)
3260 // seteuid(0); /* Can't hurt to try to get root here */
3261 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3262 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Failed to open %s: %s (it should be readable by the user.)\n",
3263 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3264 else {
3265 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3267 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3268 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp, strerror(errno));
3269 mp_tmsg(MSGT_CPLAYER, MSGL_HINT, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp);
3270 close (rtc_fd);
3271 rtc_fd = -1;
3272 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3273 /* variable only by the root */
3274 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno));
3275 close (rtc_fd);
3276 rtc_fd = -1;
3277 } else
3278 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Using Linux hardware RTC timing (%ldHz).\n", irqp);
3281 if(rtc_fd<0)
3282 #endif /* HAVE_RTC */
3283 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3284 softsleep?"software":timer_name);
3286 #ifdef HAVE_TERMCAP
3287 load_termcap(NULL); // load key-codes
3288 #endif
3290 // ========== Init keyboard FIFO (connection to libvo) ============
3292 // Init input system
3293 current_module = "init_input";
3294 mpctx->input = mp_input_init(&opts->input);
3295 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3296 if(slave_mode)
3297 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
3298 else if(!noconsolecontrols)
3299 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
3300 // Set the libstream interrupt callback
3301 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
3303 #ifdef CONFIG_MENU
3304 if(use_menu) {
3305 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3306 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3307 else {
3308 menu_cfg = get_path("menu.conf");
3309 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3310 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3311 else {
3312 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
3313 MPLAYER_CONFDIR "/menu.conf"))
3314 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", MPLAYER_CONFDIR"/menu.conf");
3315 else {
3316 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Menu init failed.\n");
3317 use_menu = 0;
3322 #endif
3324 current_module = NULL;
3326 /// Catch signals
3327 #ifndef __MINGW32__
3328 signal(SIGCHLD,child_sighandler);
3329 #endif
3331 #ifdef CONFIG_CRASH_DEBUG
3332 prog_path = argv[0];
3333 #endif
3334 //========= Catch terminate signals: ================
3335 // terminate requests:
3336 signal(SIGTERM,exit_sighandler); // kill
3337 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3339 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3341 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3342 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3343 #ifdef CONFIG_SIGHANDLER
3344 // fatal errors:
3345 signal(SIGBUS,exit_sighandler); // bus error
3346 signal(SIGSEGV,exit_sighandler); // segfault
3347 signal(SIGILL,exit_sighandler); // illegal instruction
3348 signal(SIGFPE,exit_sighandler); // floating point exc.
3349 signal(SIGABRT,exit_sighandler); // abort()
3350 #ifdef CONFIG_CRASH_DEBUG
3351 if (crash_debug)
3352 signal(SIGTRAP,exit_sighandler);
3353 #endif
3354 #endif
3356 // ******************* Now, let's see the per-file stuff ********************
3358 play_next_file:
3360 // init global sub numbers
3361 mpctx->global_sub_size = 0;
3362 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3364 if (mpctx->filename) {
3365 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
3366 load_per_extension_config (mpctx->mconfig, mpctx->filename);
3367 load_per_file_config (mpctx->mconfig, mpctx->filename);
3370 if (opts->video_driver_list)
3371 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
3372 if (opts->audio_driver_list)
3373 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
3375 // We must enable getch2 here to be able to interrupt network connection
3376 // or cache filling
3377 if(!noconsolecontrols && !slave_mode){
3378 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
3379 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"WARNING: getch2_init called twice!\n");
3380 else
3381 getch2_enable(); // prepare stdin for hotkeys...
3382 mpctx->initialized_flags|=INITIALIZED_GETCH2;
3383 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3386 // =================== GUI idle loop (STOP state) ===========================
3387 while (player_idle_mode && !mpctx->filename) {
3388 play_tree_t * entry = NULL;
3389 mp_cmd_t * cmd;
3390 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3391 if (mpctx->video_out)
3392 vo_check_events(mpctx->video_out);
3393 usec_sleep(20000);
3395 switch (cmd->id) {
3396 case MP_CMD_LOADFILE:
3397 // prepare a tree entry with the new filename
3398 entry = play_tree_new();
3399 play_tree_add_file(entry, cmd->args[0].v.s);
3400 // The entry is added to the main playtree after the switch().
3401 break;
3402 case MP_CMD_LOADLIST:
3403 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3404 break;
3405 case MP_CMD_QUIT:
3406 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3407 break;
3408 case MP_CMD_GET_PROPERTY:
3409 case MP_CMD_SET_PROPERTY:
3410 case MP_CMD_STEP_PROPERTY:
3411 run_command(mpctx, cmd);
3412 break;
3415 mp_cmd_free(cmd);
3417 if (entry) { // user entered a command that gave a valid entry
3418 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3419 play_tree_free_list(mpctx->playtree->child, 1);
3420 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3422 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3424 play_tree_set_child(mpctx->playtree, entry);
3426 /* Make iterator start at the top the of tree. */
3427 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3428 if (!mpctx->playtree_iter) continue;
3430 // find the first real item in the tree
3431 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3432 // no items!
3433 play_tree_iter_free(mpctx->playtree_iter);
3434 mpctx->playtree_iter = NULL;
3435 continue; // wait for next command
3437 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3440 //---------------------------------------------------------------------------
3442 if (mpctx->filename) {
3443 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nPlaying %s.\n",
3444 filename_recode(mpctx->filename));
3445 if(use_filename_title && vo_wintitle == NULL)
3446 vo_wintitle = strdup(mp_basename2(mpctx->filename));
3449 if (edl_filename) {
3450 if (edl_records) free_edl(edl_records);
3451 next_edl_record = edl_records = edl_parse_file();
3453 if (edl_output_filename) {
3454 if (edl_fd) fclose(edl_fd);
3455 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3457 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Can't open EDL file [%s] for writing.\n",
3458 filename_recode(edl_output_filename));
3462 //==================== Open VOB-Sub ============================
3464 current_module="vobsub";
3465 if (vobsub_name){
3466 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3467 if(vo_vobsub==NULL)
3468 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
3469 filename_recode(vobsub_name));
3470 } else if (sub_auto && mpctx->filename){
3471 /* try to autodetect vobsub from movie filename ::atmos */
3472 char *buf = strdup(mpctx->filename), *psub;
3473 char *pdot = strrchr(buf, '.');
3474 char *pslash = strrchr(buf, '/');
3475 #if defined(__MINGW32__) || defined(__CYGWIN__)
3476 if (!pslash) pslash = strrchr(buf, '\\');
3477 #endif
3478 if (pdot && (!pslash || pdot > pslash))
3479 *pdot = '\0';
3480 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3481 /* try from ~/.mplayer/sub */
3482 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3483 char *bname;
3484 int l;
3485 bname = strrchr(buf,'/');
3486 #if defined(__MINGW32__) || defined(__CYGWIN__)
3487 if(!bname) bname = strrchr(buf,'\\');
3488 #endif
3489 if(bname) bname++;
3490 else bname = buf;
3491 l = strlen(psub) + strlen(bname) + 1;
3492 psub = realloc(psub,l);
3493 strcat(psub,bname);
3494 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3495 free(psub);
3497 free(buf);
3499 if(vo_vobsub){
3500 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3501 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3502 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3504 // setup global sub numbering
3505 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3506 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3509 //============ Open & Sync STREAM --- fork cache2 ====================
3511 mpctx->stream=NULL;
3512 mpctx->demuxer=NULL;
3513 if (mpctx->d_audio) {
3514 //free_demuxer_stream(mpctx->d_audio);
3515 mpctx->d_audio=NULL;
3517 if (mpctx->d_video) {
3518 //free_demuxer_stream(d_video);
3519 mpctx->d_video=NULL;
3521 mpctx->sh_audio=NULL;
3522 mpctx->sh_video=NULL;
3524 current_module="open_stream";
3525 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3526 if(!mpctx->stream) { // error...
3527 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3528 goto goto_next_file;
3530 mpctx->initialized_flags|=INITIALIZED_STREAM;
3532 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3533 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3534 "playlist support will not be used automatically.\n"
3535 "MPlayer's playlist code is unsafe and should only be used with "
3536 "trusted sources.\nPlayback will probably fail.\n\n");
3537 #if 0
3538 play_tree_t* entry;
3539 // Handle playlist
3540 current_module="handle_playlist";
3541 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3542 filename_recode(mpctx->filename));
3543 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3544 mpctx->eof=playtree_add_playlist(mpctx, entry);
3545 goto goto_next_file;
3546 #endif
3548 mpctx->stream->start_pos+=seek_to_byte;
3550 if(stream_dump_type==5){
3551 unsigned char buf[4096];
3552 int len;
3553 FILE *f;
3554 current_module="dumpstream";
3555 stream_reset(mpctx->stream);
3556 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3557 f=fopen(stream_dump_name,"wb");
3558 if(!f){
3559 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3560 exit_player(mpctx, EXIT_ERROR);
3562 if (dvd_chapter > 1) {
3563 int chapter = dvd_chapter - 1;
3564 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3566 while(!mpctx->stream->eof && !async_quit_request){
3567 len=stream_read(mpctx->stream,buf,4096);
3568 if(len>0) {
3569 if(fwrite(buf,len,1,f) != 1) {
3570 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3571 exit_player(mpctx, EXIT_ERROR);
3574 if(dvd_last_chapter > 0) {
3575 int chapter = -1;
3576 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3577 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3578 break;
3581 if(fclose(f)) {
3582 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3583 exit_player(mpctx, EXIT_ERROR);
3585 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3586 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3589 #ifdef CONFIG_DVDREAD
3590 if(mpctx->stream->type==STREAMTYPE_DVD){
3591 current_module="dvd lang->id";
3592 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3593 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3594 // setup global sub numbering
3595 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3596 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3597 current_module=NULL;
3599 #endif
3601 #ifdef CONFIG_DVDNAV
3602 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3603 current_module="dvdnav lang->id";
3604 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3605 dvdsub_lang_id = -3;
3606 if(dvdsub_lang && opts->sub_id==-1)
3607 dvdsub_lang_id = opts->sub_id = mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3608 // setup global sub numbering
3609 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3610 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3611 current_module=NULL;
3613 #endif
3615 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3616 goto_enable_cache:
3617 if(stream_cache_size>0){
3618 int res;
3619 current_module="enable_cache";
3620 res = stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3621 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3622 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0));
3623 if(res == 0)
3624 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3627 //============ Open DEMUXERS --- DETECT file type =======================
3628 current_module="demux_open";
3630 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3632 // HACK to get MOV Reference Files working
3634 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3636 unsigned char* playlist_entry;
3637 play_tree_t *list = NULL, *entry = NULL;
3639 current_module="handle_demux_playlist";
3640 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3642 char *temp, *bname;
3644 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3645 filename_recode(playlist_entry));
3647 bname=mp_basename(playlist_entry);
3648 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3649 continue;
3651 if (!strcmp(playlist_entry, mpctx->filename)) // ignoring self-reference
3652 continue;
3654 entry = play_tree_new();
3656 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3658 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3659 if (temp)
3661 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3662 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3663 strcat(temp, playlist_entry);
3664 if (!strcmp(temp, mpctx->filename)) {
3665 free(temp);
3666 continue;
3668 play_tree_add_file(entry,temp);
3669 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3670 free(temp);
3673 else
3674 play_tree_add_file(entry,playlist_entry);
3676 if(!list)
3677 list = entry;
3678 else
3679 play_tree_append_entry(list,entry);
3681 free_demuxer(mpctx->demuxer);
3682 mpctx->demuxer = NULL;
3684 if (list)
3686 entry = play_tree_new();
3687 play_tree_set_child(entry,list);
3688 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3689 goto goto_next_file;
3693 if(!mpctx->demuxer)
3694 goto goto_next_file;
3696 if (mpctx->demuxer->matroska_data.ordered_chapters)
3697 build_ordered_chapter_timeline(mpctx);
3699 if (!mpctx->sources) {
3700 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
3701 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
3702 .demuxer = mpctx->demuxer};
3703 mpctx->num_sources = 1;
3706 if(dvd_chapter>1) {
3707 double pts;
3708 if (seek_chapter(mpctx, dvd_chapter-1, &pts, NULL) >= 0 && pts > -1.0)
3709 seek(mpctx, pts, SEEK_ABSOLUTE);
3712 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3714 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3715 int i;
3716 int maxid = -1;
3717 // setup global sub numbering
3718 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3719 for (i = 0; i < MAX_S_STREAMS; i++)
3720 if (mpctx->demuxer->s_streams[i])
3721 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3722 mpctx->global_sub_size += maxid + 1;
3724 // Make opts->sub_id always selectable if set.
3725 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3726 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3728 #ifdef CONFIG_ASS
3729 if (opts->ass_enabled && ass_library) {
3730 for (int j = 0; j < mpctx->num_sources; j++) {
3731 struct demuxer *d = mpctx->sources[j].demuxer;
3732 for (int i = 0; i < d->num_attachments; i++) {
3733 struct demux_attachment *att = d->attachments + i;
3734 if (use_embedded_fonts
3735 && att->name && att->type && att->data && att->data_size
3736 && (strcmp(att->type, "application/x-truetype-font") == 0
3737 || strcmp(att->type, "application/x-font") == 0))
3738 ass_add_font(ass_library, att->name, att->data, att->data_size);
3742 #endif
3744 current_module="demux_open2";
3746 //file_format=demuxer->file_format;
3748 mpctx->d_audio=mpctx->demuxer->audio;
3749 mpctx->d_video=mpctx->demuxer->video;
3750 mpctx->d_sub=mpctx->demuxer->sub;
3752 if (ts_prog) {
3753 int tmp = ts_prog;
3754 mp_property_do("switch_program", M_PROPERTY_SET, &tmp, mpctx);
3756 // select audio stream
3757 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3759 // DUMP STREAMS:
3760 if((stream_dump_type)&&(stream_dump_type<4)){
3761 FILE *f;
3762 demux_stream_t *ds=NULL;
3763 current_module="dump";
3764 // select stream to dump
3765 switch(stream_dump_type){
3766 case 1: ds=mpctx->d_audio;break;
3767 case 2: ds=mpctx->d_video;break;
3768 case 3: ds=mpctx->d_sub;break;
3770 if(!ds){
3771 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"dump: FATAL: Selected stream missing!\n");
3772 exit_player(mpctx, EXIT_ERROR);
3774 // disable other streams:
3775 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3776 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3777 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3778 // let's dump it!
3779 f=fopen(stream_dump_name,"wb");
3780 if(!f){
3781 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3782 exit_player(mpctx, EXIT_ERROR);
3784 while(!ds->eof){
3785 unsigned char* start;
3786 int in_size=ds_get_packet(ds,&start);
3787 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3788 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3789 if(in_size>0) fwrite(start,in_size,1,f);
3790 if(dvd_last_chapter>0) {
3791 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3792 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3793 break;
3796 fclose(f);
3797 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3798 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3801 mpctx->sh_audio=mpctx->d_audio->sh;
3802 mpctx->sh_video=mpctx->d_video->sh;
3804 if(mpctx->sh_video){
3806 current_module="video_read_properties";
3807 if(!video_read_properties(mpctx->sh_video)) {
3808 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Video: Cannot read properties.\n");
3809 mpctx->sh_video=mpctx->d_video->sh=NULL;
3810 } else {
3811 mp_tmsg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
3812 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3813 mpctx->sh_video->fps,mpctx->sh_video->frametime
3816 /* need to set fps here for output encoders to pick it up in their init */
3817 if(force_fps){
3818 mpctx->sh_video->fps=force_fps;
3819 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3821 vo_fps = mpctx->sh_video->fps;
3823 if(!mpctx->sh_video->fps && !force_fps){
3824 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"FPS not specified in the header or invalid, use the -fps option.\n");
3825 mpctx->sh_video=mpctx->d_video->sh=NULL;
3831 if(!mpctx->sh_video && !mpctx->sh_audio){
3832 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "No stream found.\n");
3833 #ifdef CONFIG_DVBIN
3834 if(mpctx->stream->type == STREAMTYPE_DVB)
3836 int dir;
3837 int v = mpctx->last_dvb_step;
3838 if(v > 0)
3839 dir = DVB_CHANNEL_HIGHER;
3840 else
3841 dir = DVB_CHANNEL_LOWER;
3843 if(dvb_step_channel(mpctx->stream, dir)) {
3844 mpctx->stop_play = PT_NEXT_ENTRY;
3845 mpctx->dvbin_reopen = 1;
3848 #endif
3849 goto goto_next_file; // exit_player(_("Fatal error"));
3852 /* display clip info */
3853 demux_info_print(mpctx->demuxer);
3855 //================== Read SUBTITLES (DVD & TEXT) ==========================
3856 if(vo_spudec==NULL &&
3857 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3858 init_vo_spudec(mpctx);
3861 // after reading video params we should load subtitles because
3862 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3863 // check .sub
3864 current_module="read_subtitles_file";
3865 double sub_fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
3866 if(sub_name){
3867 for (i = 0; sub_name[i] != NULL; ++i)
3868 add_subtitles(mpctx, sub_name[i], sub_fps, 0);
3870 if(sub_auto) { // auto load sub file ...
3871 char *psub = get_path( "sub/" );
3872 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3873 int i = 0;
3874 free(psub); // release the buffer created by get_path() above
3875 while (tmp[i]) {
3876 add_subtitles(mpctx, tmp[i], sub_fps, 1);
3877 free(tmp[i++]);
3879 free(tmp);
3881 if (mpctx->set_of_sub_size > 0) {
3882 // setup global sub numbering
3883 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3884 mpctx->global_sub_size += mpctx->set_of_sub_size;
3888 if (mpctx->global_sub_size) {
3889 select_subtitle(mpctx);
3890 if(subdata)
3891 switch (stream_dump_type) {
3892 case 3: list_sub_file(subdata); break;
3893 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3894 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3895 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3896 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3897 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3901 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3902 filename_recode(mpctx->filename));
3903 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3904 if (mpctx->sh_video) {
3905 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3906 if (mpctx->sh_video->format >= 0x20202020)
3907 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3908 else
3909 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3910 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3911 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3912 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3913 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3914 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3916 if (mpctx->sh_audio) {
3917 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3918 if (mpctx->sh_audio->format >= 0x20202020)
3919 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3920 else
3921 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3922 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3923 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3924 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3926 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2f\n", mpctx->timeline ?
3927 mpctx->timeline[mpctx->num_timeline_parts].start :
3928 demuxer_get_time_length(mpctx->demuxer));
3929 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n",
3930 mpctx->stream->seek && (!mpctx->demuxer || mpctx->demuxer->seekable));
3931 if (mpctx->demuxer) {
3932 if (mpctx->demuxer->num_chapters == 0)
3933 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3934 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3937 if(!mpctx->sh_video) goto main; // audio-only
3939 if(!reinit_video_chain(mpctx)) {
3940 if(!mpctx->sh_video){
3941 if(!mpctx->sh_audio) goto goto_next_file;
3942 goto main; // exit_player(_("Fatal error"));
3946 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3947 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3949 #ifdef CONFIG_FREETYPE
3950 force_load_font = 1;
3951 #endif
3953 //================== MAIN: ==========================
3954 main:
3955 current_module="main";
3957 if(playing_msg) {
3958 char* msg = property_expand_string(mpctx, playing_msg);
3959 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3960 free(msg);
3964 // Disable the term OSD in verbose mode
3965 if(verbose) term_osd = 0;
3968 //int frame_corr_num=0; //
3969 //float v_frame=0; // Video
3970 //float num_frames=0; // number of frames played
3972 int frame_time_remaining=0; // flag
3973 int blit_frame=0;
3975 // Make sure old OSD does not stay around,
3976 // e.g. with -fixed-vo and same-resolution files
3977 clear_osd_msgs();
3978 update_osd_msg(mpctx);
3980 //================ SETUP AUDIO ==========================
3982 if(mpctx->sh_audio){
3983 reinit_audio_chain(mpctx);
3984 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3985 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3988 current_module="av_init";
3990 if(mpctx->sh_video){
3991 mpctx->sh_video->timer=0;
3992 if (! ignore_start)
3993 audio_delay += mpctx->sh_video->stream_delay;
3995 if(mpctx->sh_audio){
3996 if (start_volume >= 0)
3997 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3998 if (! ignore_start)
3999 audio_delay -= mpctx->sh_audio->stream_delay;
4000 mpctx->delay=-audio_delay;
4003 if(!mpctx->sh_audio){
4004 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Audio: no sound\n");
4005 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
4006 ds_free_packs(mpctx->d_audio); // free buffered chunks
4007 //mpctx->d_audio->id=-2; // do not read audio chunks
4008 //uninit_player(mpctx, INITIALIZED_AO); // close device
4010 if(!mpctx->sh_video){
4011 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Video: no video\n");
4012 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
4013 ds_free_packs(mpctx->d_video);
4014 mpctx->d_video->id=-2;
4015 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
4018 if (!mpctx->sh_video && !mpctx->sh_audio)
4019 goto goto_next_file;
4021 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
4022 if(force_fps && mpctx->sh_video){
4023 vo_fps = mpctx->sh_video->fps=force_fps;
4024 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
4025 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx->sh_video->fps,mpctx->sh_video->frametime);
4028 mp_input_set_section(mpctx->input, NULL);
4029 //TODO: add desired (stream-based) sections here
4030 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
4031 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
4033 //==================== START PLAYING =======================
4035 if(opts->loop_times>1) opts->loop_times--; else
4036 if(opts->loop_times==1) opts->loop_times = -1;
4038 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Starting playback...\n");
4040 total_time_usage_start=GetTimer();
4041 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
4042 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
4043 play_n_frames=play_n_frames_mf;
4045 if(play_n_frames==0){
4046 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
4049 // If there's a timeline force an absolute seek to initialize state
4050 if (seek_to_sec || mpctx->timeline) {
4051 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
4052 end_at.pos += seek_to_sec;
4055 if (end_at.type == END_AT_SIZE) {
4056 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Option -endpos in MPlayer does not yet support size units.\n");
4057 end_at.type = END_AT_NONE;
4060 #ifdef CONFIG_DVDNAV
4061 mp_dvdnav_context_free(mpctx);
4062 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4063 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
4064 mp_dvdnav_cell_has_changed(mpctx->stream,1);
4066 #endif
4068 get_relative_time(mpctx); // reset current delta
4069 mpctx->time_frame = 0;
4070 mpctx->drop_message_shown = 0;
4071 mpctx->update_video_immediately = true;
4072 mpctx->total_avsync_change = 0;
4073 // Make sure VO knows current pause state
4074 if (mpctx->sh_video)
4075 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
4076 NULL);
4078 while(!mpctx->stop_play){
4079 float aq_sleep_time=0;
4081 if(dvd_last_chapter>0) {
4082 int cur_chapter = get_current_chapter(mpctx);
4083 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
4084 goto goto_next_file;
4087 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
4088 mpctx->sh_audio = mpctx->d_audio->sh;
4089 mpctx->sh_audio->ds = mpctx->d_audio;
4090 reinit_audio_chain(mpctx);
4093 /*========================== PLAY AUDIO ============================*/
4095 if (mpctx->sh_audio && !mpctx->paused)
4096 if (!fill_audio_out_buffers(mpctx))
4097 // at eof, all audio at least written to ao
4098 if (!mpctx->sh_video)
4099 mpctx->stop_play = AT_END_OF_FILE;
4102 if(!mpctx->sh_video) {
4103 // handle audio-only case:
4104 double a_pos=0;
4105 // sh_audio can be NULL due to video stream switching
4106 // TODO: handle this better
4107 if (mpctx->sh_audio)
4108 a_pos = playing_audio_pts(mpctx);
4110 print_status(mpctx, a_pos, false);
4112 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
4113 mpctx->stop_play = PT_NEXT_ENTRY;
4114 update_subtitles(mpctx, &mpctx->opts, NULL, a_pos, mpctx->video_offset,
4115 mpctx->d_sub, 0);
4116 update_osd_msg(mpctx);
4118 } else {
4120 /*========================== PLAY VIDEO ============================*/
4122 vo_pts=mpctx->sh_video->timer*90000.0;
4123 vo_fps=mpctx->sh_video->fps;
4125 blit_frame = mpctx->video_out->frame_loaded;
4126 if (!blit_frame) {
4127 double frame_time = update_video(mpctx);
4128 blit_frame = mpctx->video_out->frame_loaded;
4129 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
4130 if (mpctx->sh_video->vf_initialized < 0) {
4131 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "\nFATAL: Could not initialize video filters (-vf) or video output (-vo).\n");
4132 mpctx->stop_play = PT_NEXT_ENTRY;
4133 goto goto_next_file;
4135 if (blit_frame) {
4136 struct sh_video *sh_video = mpctx->sh_video;
4137 update_subtitles(mpctx, &mpctx->opts, sh_video, sh_video->pts,
4138 mpctx->video_offset, mpctx->d_sub, 0);
4139 update_teletext(sh_video, mpctx->demuxer, 0);
4140 update_osd_msg(mpctx);
4141 struct vf_instance *vf = mpctx->sh_video->vfilter;
4142 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
4143 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
4144 vo_osd_changed(0);
4146 if (frame_time < 0)
4147 mpctx->stop_play = AT_END_OF_FILE;
4148 else {
4149 if (mpctx->update_video_immediately) {
4150 // Show this frame immediately, rest normally
4151 mpctx->update_video_immediately = false;
4152 } else {
4153 mpctx->time_frame += frame_time / opts->playback_speed;
4154 adjust_sync(mpctx, frame_time);
4158 if (mpctx->timeline) {
4159 struct timeline_part *next = mpctx->timeline + mpctx->timeline_part + 1;
4160 if (mpctx->sh_video->pts >= next->start
4161 || mpctx->stop_play == AT_END_OF_FILE
4162 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
4163 seek(mpctx, next->start, SEEK_ABSOLUTE);
4164 continue;
4168 // ==========================================================================
4170 // current_module="draw_osd";
4171 // if(vo_config_count) mpctx->video_out->draw_osd();
4173 current_module="vo_check_events";
4174 vo_check_events(mpctx->video_out);
4176 #ifdef CONFIG_X11
4177 if (stop_xscreensaver) {
4178 current_module = "stop_xscreensaver";
4179 xscreensaver_heartbeat(mpctx->x11_state);
4181 #endif
4182 if (heartbeat_cmd) {
4183 static unsigned last_heartbeat;
4184 unsigned now = GetTimerMS();
4185 if (now - last_heartbeat > 30000) {
4186 last_heartbeat = now;
4187 system(heartbeat_cmd);
4191 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
4193 //====================== FLIP PAGE (VIDEO BLT): =========================
4195 current_module="flip_page";
4196 if (!frame_time_remaining && blit_frame) {
4197 unsigned int t2=GetTimer();
4198 unsigned int pts_us = mpctx->last_time + mpctx->time_frame * 1e6;
4199 int duration = -1;
4200 double pts2 = mpctx->video_out->next_pts2;
4201 if (pts2 != MP_NOPTS_VALUE && opts->correct_pts) {
4202 // expected A/V sync correction is ignored
4203 double diff = (pts2 - mpctx->sh_video->pts);
4204 diff /= opts->playback_speed;
4205 if (mpctx->time_frame < 0)
4206 diff += mpctx->time_frame;
4207 if (diff < 0)
4208 diff = 0;
4209 if (diff > 10)
4210 diff = 10;
4211 duration = diff * 1e6;
4213 vo_flip_page(mpctx->video_out, pts_us|1, duration);
4215 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
4216 vout_time_usage += mpctx->last_vo_flip_duration;
4217 if (mpctx->video_out->driver->flip_page_timed) {
4218 // No need to adjust sync based on flip speed
4219 mpctx->last_vo_flip_duration = 0;
4220 // For print_status - VO call finishing early is OK for sync
4221 mpctx->time_frame -= get_relative_time(mpctx);
4223 print_status(mpctx, MP_NOPTS_VALUE, true);
4225 else
4226 print_status(mpctx, MP_NOPTS_VALUE, false);
4228 //============================ Auto QUALITY ============================
4230 /*Output quality adjustments:*/
4231 if(auto_quality>0){
4232 current_module="autoq";
4233 // float total=0.000001f * (GetTimer()-aq_total_time);
4234 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4235 if(output_quality<auto_quality && aq_sleep_time>0)
4236 ++output_quality;
4237 else
4238 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4239 if(output_quality>1 && aq_sleep_time<0)
4240 --output_quality;
4241 else
4242 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4243 output_quality=0;
4244 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4245 set_video_quality(mpctx->sh_video,output_quality);
4248 if (!frame_time_remaining && blit_frame) {
4249 if (play_n_frames >= 0) {
4250 --play_n_frames;
4251 if (play_n_frames <= 0)
4252 mpctx->stop_play = PT_NEXT_ENTRY;
4254 if (mpctx->step_frames > 0) {
4255 mpctx->step_frames--;
4256 if (mpctx->step_frames == 0)
4257 pause_player(mpctx);
4262 // FIXME: add size based support for -endpos
4263 if (end_at.type == END_AT_TIME &&
4264 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
4265 mpctx->stop_play = PT_NEXT_ENTRY;
4267 } // end if(mpctx->sh_video)
4269 #ifdef CONFIG_DVDNAV
4270 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4271 nav_highlight_t hl;
4272 mp_dvdnav_get_highlight (mpctx->stream, &hl);
4273 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4274 vo_osd_changed (OSDTYPE_DVDNAV);
4276 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
4277 double ar = -1.0;
4278 if (stream_control (mpctx->demuxer->stream,
4279 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
4280 != STREAM_UNSUPPORTED)
4281 mpctx->sh_video->stream_aspect = ar;
4284 #endif
4286 //================= Keyboard events, SEEKing ====================
4288 current_module="key_events";
4291 while (1) {
4292 mp_cmd_t* cmd;
4293 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
4294 run_command(mpctx, cmd);
4295 mp_cmd_free(cmd);
4296 if (mpctx->stop_play)
4297 break;
4299 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
4300 || mpctx->abs_seek_pos)
4301 break;
4302 if (mpctx->sh_video) {
4303 update_osd_msg(mpctx);
4304 int hack = vo_osd_changed(0);
4305 vo_osd_changed(hack);
4306 if (hack)
4307 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
4308 add_step_frame(mpctx);
4309 break;
4311 else
4312 vo_osd_changed(0);
4314 pause_loop(mpctx);
4318 // handle -sstep
4319 if (step_sec > 0 && !mpctx->paused) {
4320 mpctx->osd_function=OSD_FFW;
4321 mpctx->rel_seek_secs+=step_sec;
4324 edl_update(mpctx);
4326 /* Looping. */
4327 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
4328 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
4330 if(opts->loop_times>1) opts->loop_times--; else
4331 if(opts->loop_times==1) opts->loop_times=-1;
4332 play_n_frames=play_n_frames_mf;
4333 mpctx->stop_play=0;
4334 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
4337 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
4338 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
4340 mpctx->rel_seek_secs=0;
4341 mpctx->abs_seek_pos=0;
4344 } // while(!mpctx->stop_play)
4346 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4348 #ifdef CONFIG_DVBIN
4349 if(mpctx->dvbin_reopen)
4351 mpctx->stop_play = 0;
4352 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4353 cache_uninit(mpctx->stream);
4354 mpctx->dvbin_reopen = 0;
4355 goto goto_enable_cache;
4357 #endif
4360 goto_next_file: // don't jump here after ao/vo/getch initialization!
4362 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4364 if(benchmark){
4365 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4366 double total_time_usage;
4367 total_time_usage_start=GetTimer()-total_time_usage_start;
4368 total_time_usage = (float)total_time_usage_start*0.000001;
4369 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4370 video_time_usage,vout_time_usage,audio_time_usage,
4371 total_time_usage-tot,total_time_usage);
4372 if(total_time_usage>0.0)
4373 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4374 100.0*video_time_usage/total_time_usage,
4375 100.0*vout_time_usage/total_time_usage,
4376 100.0*audio_time_usage/total_time_usage,
4377 100.0*(total_time_usage-tot)/total_time_usage,
4378 100.0);
4379 if(total_frame_cnt && frame_dropping)
4380 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4381 total_frame_cnt-drop_frame_cnt,
4382 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4383 drop_frame_cnt,
4384 100*drop_frame_cnt/total_frame_cnt,
4385 total_frame_cnt,
4386 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4389 // time to uninit all, except global stuff:
4390 uninit_player(mpctx, INITIALIZED_ALL-(opts->fixed_vo?INITIALIZED_VO:0));
4392 if(mpctx->set_of_sub_size > 0) {
4393 current_module="sub_free";
4394 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4395 sub_free(mpctx->set_of_subtitles[i]);
4396 #ifdef CONFIG_ASS
4397 if(mpctx->set_of_ass_tracks[i])
4398 ass_free_track( mpctx->set_of_ass_tracks[i] );
4399 #endif
4401 mpctx->set_of_sub_size = 0;
4403 vo_sub_last = vo_sub=NULL;
4404 subdata=NULL;
4405 #ifdef CONFIG_ASS
4406 ass_track = NULL;
4407 if(ass_library)
4408 ass_clear_fonts(ass_library);
4409 #endif
4411 if (!mpctx->stop_play) // In case some goto jumped here...
4412 mpctx->stop_play = PT_NEXT_ENTRY;
4414 int playtree_direction = 1;
4416 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4417 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4418 play_tree_iter_free(mpctx->playtree_iter);
4419 mpctx->playtree_iter = NULL;
4421 mpctx->play_tree_step = 1;
4422 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4423 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4424 if(mpctx->playtree_iter) {
4425 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4426 play_tree_iter_free(mpctx->playtree_iter);
4427 mpctx->playtree_iter = NULL;
4430 } else if (mpctx->stop_play == PT_STOP) {
4431 play_tree_iter_free(mpctx->playtree_iter);
4432 mpctx->playtree_iter = NULL;
4433 } else { // NEXT PREV SRC
4434 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4437 while(mpctx->playtree_iter != NULL) {
4438 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4439 if(mpctx->filename == NULL) {
4440 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4441 play_tree_iter_free(mpctx->playtree_iter);
4442 mpctx->playtree_iter = NULL;
4444 } else
4445 break;
4448 if(mpctx->playtree_iter != NULL || player_idle_mode){
4449 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4450 mpctx->stop_play = 0;
4451 goto play_next_file;
4455 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4457 return 1;
4459 #endif /* DISABLE_MAIN */