Ignore svn change r30644
[mplayer/glamo.git] / mplayer.c
blobad754e9f1c88cf1f3a5c9a5223cc61badd7b9fdc
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 extern vf_info_t vf_info_menu;
378 static const vf_info_t* const libmenu_vfs[] = {
379 &vf_info_menu,
380 NULL
382 static vf_instance_t* vf_menu = NULL;
383 int use_menu = 0;
384 static char* menu_cfg = NULL;
385 static char* menu_root = "main";
386 #endif
389 #ifdef HAVE_RTC
390 static int nortc = 1;
391 static char* rtc_device;
392 #endif
394 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
395 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
396 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
397 int use_filedir_conf;
398 int use_filename_title;
400 #include "mpcommon.h"
401 #include "command.h"
403 #include "metadata.h"
405 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
407 const void *mpctx_get_video_out(MPContext *mpctx)
409 return mpctx->video_out;
412 const void *mpctx_get_audio_out(MPContext *mpctx)
414 return mpctx->audio_out;
417 void *mpctx_get_demuxer(MPContext *mpctx)
419 return mpctx->demuxer;
422 void *mpctx_get_playtree_iter(MPContext *mpctx)
424 return mpctx->playtree_iter;
427 void *mpctx_get_mixer(MPContext *mpctx)
429 return &mpctx->mixer;
432 int mpctx_get_global_sub_size(MPContext *mpctx)
434 return mpctx->global_sub_size;
437 int mpctx_get_osd_function(MPContext *mpctx)
439 return mpctx->osd_function;
442 static float get_relative_time(struct MPContext *mpctx)
444 unsigned int new_time = GetTimer();
445 unsigned int delta = new_time - mpctx->last_time;
446 mpctx->last_time = new_time;
447 return delta * 0.000001;
450 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
451 switch (type)
453 /* check for valid video stream */
454 case META_VIDEO_CODEC:
455 case META_VIDEO_BITRATE:
456 case META_VIDEO_RESOLUTION:
458 if (!mpctx->sh_video)
459 return 0;
460 break;
463 /* check for valid audio stream */
464 case META_AUDIO_CODEC:
465 case META_AUDIO_BITRATE:
466 case META_AUDIO_SAMPLES:
468 if (!mpctx->sh_audio)
469 return 0;
470 break;
473 /* check for valid demuxer */
474 case META_INFO_TITLE:
475 case META_INFO_ARTIST:
476 case META_INFO_ALBUM:
477 case META_INFO_YEAR:
478 case META_INFO_COMMENT:
479 case META_INFO_TRACK:
480 case META_INFO_GENRE:
482 if (!mpctx->demuxer)
483 return 0;
484 break;
487 default:
488 break;
491 return 1;
494 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
495 char **info = mpctx->demuxer->info;
496 int n;
498 if (!info || !tag)
499 return NULL;
501 for (n = 0; info[2*n] != NULL ; n++)
502 if (!strcasecmp (info[2*n], tag))
503 break;
505 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
508 char *get_metadata(struct MPContext *mpctx, metadata_t type)
510 char *meta = NULL;
511 sh_audio_t * const sh_audio = mpctx->sh_audio;
512 sh_video_t * const sh_video = mpctx->sh_video;
514 if (!is_valid_metadata_type(mpctx, type))
515 return NULL;
517 switch (type)
519 case META_NAME:
521 return strdup (mp_basename2 (mpctx->filename));
524 case META_VIDEO_CODEC:
526 if (sh_video->format == 0x10000001)
527 meta = strdup ("mpeg1");
528 else if (sh_video->format == 0x10000002)
529 meta = strdup ("mpeg2");
530 else if (sh_video->format == 0x10000004)
531 meta = strdup ("mpeg4");
532 else if (sh_video->format == 0x10000005)
533 meta = strdup ("h264");
534 else if (sh_video->format >= 0x20202020)
536 meta = malloc (8);
537 sprintf (meta, "%.4s", (char *) &sh_video->format);
539 else
541 meta = malloc (8);
542 sprintf (meta, "0x%08X", sh_video->format);
544 return meta;
547 case META_VIDEO_BITRATE:
549 meta = malloc (16);
550 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
551 return meta;
554 case META_VIDEO_RESOLUTION:
556 meta = malloc (16);
557 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
558 return meta;
561 case META_AUDIO_CODEC:
563 if (sh_audio->codec && sh_audio->codec->name)
564 meta = strdup (sh_audio->codec->name);
565 return meta;
568 case META_AUDIO_BITRATE:
570 meta = malloc (16);
571 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
572 return meta;
575 case META_AUDIO_SAMPLES:
577 meta = malloc (16);
578 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
579 return meta;
582 /* check for valid demuxer */
583 case META_INFO_TITLE:
584 return get_demuxer_info(mpctx, "Title");
586 case META_INFO_ARTIST:
587 return get_demuxer_info(mpctx, "Artist");
589 case META_INFO_ALBUM:
590 return get_demuxer_info(mpctx, "Album");
592 case META_INFO_YEAR:
593 return get_demuxer_info(mpctx, "Year");
595 case META_INFO_COMMENT:
596 return get_demuxer_info(mpctx, "Comment");
598 case META_INFO_TRACK:
599 return get_demuxer_info(mpctx, "Track");
601 case META_INFO_GENRE:
602 return get_demuxer_info(mpctx, "Genre");
604 default:
605 break;
608 return meta;
611 /// step size of mixer changes
612 int volstep = 3;
614 #ifdef CONFIG_DVDNAV
615 static void mp_dvdnav_context_free(MPContext *ctx){
616 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
617 ctx->nav_smpi = NULL;
618 if (ctx->nav_buffer) free(ctx->nav_buffer);
619 ctx->nav_buffer = NULL;
620 ctx->nav_start = NULL;
621 ctx->nav_in_size = 0;
623 #endif
625 void uninit_player(struct MPContext *mpctx, unsigned int mask){
626 mask &= mpctx->initialized_flags;
628 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
630 if(mask&INITIALIZED_ACODEC){
631 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
632 current_module="uninit_acodec";
633 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
634 mpctx->sh_audio=NULL;
635 mpctx->mixer.afilter = NULL;
638 if(mask&INITIALIZED_VCODEC){
639 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
640 current_module="uninit_vcodec";
641 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
642 mpctx->sh_video=NULL;
643 #ifdef CONFIG_MENU
644 vf_menu=NULL;
645 #endif
648 if(mask&INITIALIZED_DEMUXER){
649 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
650 current_module="free_demuxer";
651 if (mpctx->num_sources) {
652 mpctx->demuxer = mpctx->sources[0].demuxer;
653 for (int i = 1; i < mpctx->num_sources; i++) {
654 free_stream(mpctx->sources[i].stream);
655 free_demuxer(mpctx->sources[i].demuxer);
658 talloc_free(mpctx->sources);
659 mpctx->sources = NULL;
660 mpctx->num_sources = 0;
661 talloc_free(mpctx->timeline);
662 mpctx->timeline = NULL;
663 mpctx->num_timeline_parts = 0;
664 talloc_free(mpctx->chapters);
665 mpctx->chapters = NULL;
666 mpctx->num_chapters = 0;
667 mpctx->video_offset = 0;
668 if(mpctx->demuxer){
669 mpctx->stream=mpctx->demuxer->stream;
670 free_demuxer(mpctx->demuxer);
672 mpctx->demuxer=NULL;
675 // kill the cache process:
676 if(mask&INITIALIZED_STREAM){
677 mpctx->initialized_flags&=~INITIALIZED_STREAM;
678 current_module="uninit_stream";
679 if(mpctx->stream) free_stream(mpctx->stream);
680 mpctx->stream=NULL;
683 if(mask&INITIALIZED_VO){
684 mpctx->initialized_flags&=~INITIALIZED_VO;
685 current_module="uninit_vo";
686 vo_destroy(mpctx->video_out);
687 mpctx->video_out=NULL;
688 #ifdef CONFIG_DVDNAV
689 mp_dvdnav_context_free(mpctx);
690 #endif
693 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
694 if(mask&INITIALIZED_GETCH2){
695 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
696 current_module="uninit_getch2";
697 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
698 // restore terminal:
699 getch2_disable();
702 if(mask&INITIALIZED_VOBSUB){
703 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
704 current_module="uninit_vobsub";
705 if(vo_vobsub) vobsub_close(vo_vobsub);
706 vo_vobsub=NULL;
709 if (mask&INITIALIZED_SPUDEC){
710 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
711 current_module="uninit_spudec";
712 spudec_free(vo_spudec);
713 vo_spudec=NULL;
716 if(mask&INITIALIZED_AO){
717 mpctx->initialized_flags&=~INITIALIZED_AO;
718 current_module="uninit_ao";
719 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
720 if (mpctx->audio_out)
721 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
722 mpctx->audio_out=NULL;
725 current_module=NULL;
728 void exit_player_with_rc(struct MPContext *mpctx, enum exit_reason how, int rc)
730 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
731 uninit_player(mpctx, INITIALIZED_ALL);
732 #if defined(__MINGW32__) || defined(__CYGWIN__)
733 timeEndPeriod(1);
734 #endif
735 #ifdef CONFIG_X11
736 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
737 #endif
739 current_module="uninit_input";
740 mp_input_uninit(mpctx->input);
741 #ifdef CONFIG_MENU
742 if (use_menu)
743 menu_uninit();
744 #endif
746 #ifdef CONFIG_FREETYPE
747 current_module="uninit_font";
748 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
749 free_font_desc(mpctx->osd->sub_font);
750 free_font_desc(vo_font);
751 vo_font = NULL;
752 done_freetype();
753 #endif
754 osd_free(mpctx->osd);
756 #ifdef CONFIG_ASS
757 ass_library_done(ass_library);
758 ass_library = NULL;
759 #endif
761 current_module="exit_player";
763 // free mplayer config
764 if(mpctx->mconfig)
765 m_config_free(mpctx->mconfig);
766 mpctx->mconfig = NULL;
768 if(mpctx->playtree_iter)
769 play_tree_iter_free(mpctx->playtree_iter);
770 mpctx->playtree_iter = NULL;
771 if(mpctx->playtree)
772 play_tree_free(mpctx->playtree, 1);
773 mpctx->playtree = NULL;
775 talloc_free(mpctx->key_fifo);
777 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
778 edl_records = NULL;
779 switch(how) {
780 case EXIT_QUIT:
781 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Quit");
782 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
783 break;
784 case EXIT_EOF:
785 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","End of file");
786 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
787 break;
788 case EXIT_ERROR:
789 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Fatal error");
790 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
791 break;
792 default:
793 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
795 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
797 exit(rc);
800 static void exit_player(struct MPContext *mpctx, enum exit_reason how)
802 exit_player_with_rc(mpctx, how, 1);
805 #ifndef __MINGW32__
806 static void child_sighandler(int x){
807 pid_t pid;
808 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
810 #endif
812 #ifdef CONFIG_CRASH_DEBUG
813 static char *prog_path;
814 static int crash_debug = 0;
815 #endif
817 static void exit_sighandler(int x){
818 static int sig_count=0;
819 #ifdef CONFIG_CRASH_DEBUG
820 if (!crash_debug || x != SIGTRAP)
821 #endif
822 ++sig_count;
823 if(sig_count==5)
825 /* We're crashing bad and can't uninit cleanly :(
826 * by popular request, we make one last (dirty)
827 * effort to restore the user's
828 * terminal. */
829 getch2_disable();
830 exit(1);
832 if(sig_count==6) exit(1);
833 if(sig_count>6){
834 // can't stop :(
835 #ifndef __MINGW32__
836 kill(getpid(),SIGKILL);
837 #endif
839 mp_msg(MSGT_CPLAYER, MSGL_FATAL, "\n");
840 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,
841 "\nMPlayer interrupted by signal %d in module: %s\n", x,
842 current_module ? current_module : "unknown");
843 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
844 if(sig_count<=1)
845 switch(x){
846 case SIGINT:
847 case SIGQUIT:
848 case SIGTERM:
849 case SIGKILL:
850 async_quit_request = 1;
851 return; // killed from keyboard (^C) or killed [-9]
852 case SIGILL:
853 #if CONFIG_RUNTIME_CPUDETECT
854 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL_RTCpuSel);
855 #else
856 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL);
857 #endif
858 case SIGFPE:
859 case SIGSEGV:
860 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGSEGV_SIGFPE);
861 default:
862 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGCRASH);
863 #ifdef CONFIG_CRASH_DEBUG
864 if (crash_debug) {
865 int gdb_pid;
866 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
867 gdb_pid = fork();
868 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
869 if (gdb_pid == 0) { // We are the child
870 char spid[20];
871 snprintf(spid, sizeof(spid), "%i", getppid());
872 getch2_disable(); // allow terminal to work properly with gdb
873 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
874 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
875 } else if (gdb_pid < 0)
876 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
877 else {
878 waitpid(gdb_pid, NULL, 0);
880 if (x == SIGTRAP) return;
882 #endif
884 getch2_disable();
885 exit(1);
888 #include "cfg-mplayer.h"
890 static int cfg_include(m_option_t *conf, char *filename)
892 return m_config_parse_config_file(conf->priv, filename);
895 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
897 char *conffile;
898 int conffile_fd;
899 if (!disable_system_conf &&
900 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
901 exit_player(mpctx, EXIT_NONE);
902 if ((conffile = get_path("")) == NULL) {
903 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"Cannot find HOME directory.\n");
904 } else {
905 #ifdef __MINGW32__
906 mkdir(conffile);
907 #else
908 mkdir(conffile, 0777);
909 #endif
910 free(conffile);
911 if ((conffile = get_path("config")) == NULL) {
912 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
913 } else {
914 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
915 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Creating config file: %s\n", conffile);
916 write(conffile_fd, default_config, strlen(default_config));
917 close(conffile_fd);
919 if (!disable_user_conf &&
920 m_config_parse_config_file(conf, conffile) < 0)
921 exit_player(mpctx, EXIT_NONE);
922 free(conffile);
927 #define PROFILE_CFG_PROTOCOL "protocol."
929 static void load_per_protocol_config (m_config_t* conf, const char *const file)
931 char *str;
932 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
933 m_profile_t *p;
935 /* does filename actually uses a protocol ? */
936 str = strstr (file, "://");
937 if (!str)
938 return;
940 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
941 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
942 p = m_config_get_profile (conf, protocol);
943 if (p)
945 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading protocol-related profile '%s'\n", protocol);
946 m_config_set_profile(conf,p);
950 #define PROFILE_CFG_EXTENSION "extension."
952 static void load_per_extension_config (m_config_t* conf, const char *const file)
954 char *str;
955 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
956 m_profile_t *p;
958 /* does filename actually have an extension ? */
959 str = strrchr (file, '.');
960 if (!str)
961 return;
963 sprintf (extension, PROFILE_CFG_EXTENSION);
964 strncat (extension, ++str, 7);
965 p = m_config_get_profile (conf, extension);
966 if (p)
968 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", extension);
969 m_config_set_profile(conf,p);
973 #define PROFILE_CFG_VO "vo."
974 #define PROFILE_CFG_AO "ao."
976 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
978 char profile[strlen (cfg) + strlen (out) + 1];
979 m_profile_t *p;
981 sprintf (profile, "%s%s", cfg, out);
982 p = m_config_get_profile (conf, profile);
983 if (p)
985 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", profile);
986 m_config_set_profile(conf,p);
991 * Tries to load a config file
992 * @return 0 if file was not found, 1 otherwise
994 static int try_load_config(m_config_t *conf, const char *file)
996 struct stat st;
997 if (stat(file, &st))
998 return 0;
999 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Loading config '%s'\n", file);
1000 m_config_parse_config_file (conf, file);
1001 return 1;
1004 static void load_per_file_config (m_config_t* conf, const char *const file)
1006 char *confpath;
1007 char cfg[PATH_MAX];
1008 char *name;
1010 if (strlen(file) > PATH_MAX - 14) {
1011 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Filename is too long, can not load file or directory specific config files\n");
1012 return;
1014 sprintf (cfg, "%s.conf", file);
1016 name = strrchr(cfg, '/');
1017 if (HAVE_DOS_PATHS) {
1018 char *tmp = strrchr(cfg, '\\');
1019 if (!name || tmp > name)
1020 name = tmp;
1021 tmp = strrchr(cfg, ':');
1022 if (!name || tmp > name)
1023 name = tmp;
1025 if (!name)
1026 name = cfg;
1027 else
1028 name++;
1030 if (use_filedir_conf) {
1031 char dircfg[PATH_MAX];
1032 strcpy(dircfg, cfg);
1033 strcpy(dircfg + (name - cfg), "mplayer.conf");
1034 try_load_config(conf, dircfg);
1036 if (try_load_config(conf, cfg))
1037 return;
1040 if ((confpath = get_path (name)) != NULL)
1042 try_load_config(conf, confpath);
1044 free (confpath);
1048 /* When libmpdemux performs a blocking operation (network connection or
1049 * cache filling) if the operation fails we use this function to check
1050 * if it was interrupted by the user.
1051 * The function returns a new value for eof. */
1052 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
1054 mp_cmd_t* cmd;
1055 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
1056 switch(cmd->id) {
1057 case MP_CMD_QUIT:
1058 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
1059 case MP_CMD_PLAY_TREE_STEP: {
1060 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
1061 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
1062 } break;
1063 case MP_CMD_PLAY_TREE_UP_STEP: {
1064 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
1065 } break;
1066 case MP_CMD_PLAY_ALT_SRC_STEP: {
1067 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
1068 } break;
1070 mp_cmd_free(cmd);
1072 return stop_play;
1075 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
1077 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
1079 play_tree_add_bpf(entry,mpctx->filename);
1082 if(!entry) {
1083 entry = mpctx->playtree_iter->tree;
1084 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1085 return PT_NEXT_ENTRY;
1087 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
1088 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1089 return PT_NEXT_ENTRY;
1092 play_tree_remove(entry,1,1);
1093 return PT_NEXT_SRC;
1095 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
1096 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1097 entry = mpctx->playtree_iter->tree;
1098 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1099 return PT_NEXT_ENTRY;
1101 play_tree_remove(entry,1,1);
1103 return PT_NEXT_SRC;
1106 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1108 struct MPOpts *opts = &mpctx->opts;
1109 sub_data *subd = NULL;
1110 struct ass_track *asst = NULL;
1112 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1113 return;
1116 #ifdef CONFIG_ASS
1117 if (opts->ass_enabled) {
1118 #ifdef CONFIG_ICONV
1119 asst = ass_read_stream(ass_library, filename, sub_cp);
1120 #else
1121 asst = ass_read_stream(ass_library, filename, 0);
1122 #endif
1123 if (!asst) {
1124 subd = sub_read_file(filename, fps);
1125 if (subd) {
1126 asst = ass_read_subdata(ass_library, subd, fps);
1127 if (asst) {
1128 sub_free(subd);
1129 subd = NULL;
1133 } else
1134 #endif
1135 subd = sub_read_file(filename, fps);
1138 if (!asst && !subd) {
1139 mp_tmsg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR,
1140 "Cannot load subtitles: %s\n", filename_recode(filename));
1141 return;
1144 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1145 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1146 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1147 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1148 filename_recode(filename));
1149 ++mpctx->set_of_sub_size;
1150 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "SUB: Added subtitle file (%d): %s\n", mpctx->set_of_sub_size,
1151 filename_recode(filename));
1154 void init_vo_spudec(struct MPContext *mpctx)
1156 if (vo_spudec)
1157 spudec_free(vo_spudec);
1158 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1159 vo_spudec = NULL;
1161 // we currently can't work without video stream
1162 if (!mpctx->sh_video)
1163 return;
1165 if (spudec_ifo) {
1166 unsigned int palette[16], width, height;
1167 current_module="spudec_init_vobsub";
1168 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1169 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1172 #ifdef CONFIG_DVDREAD
1173 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1174 current_module="spudec_init_dvdread";
1175 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1176 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1177 NULL, 0);
1179 #endif
1181 #ifdef CONFIG_DVDNAV
1182 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1183 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1184 current_module="spudec_init_dvdnav";
1185 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1187 #endif
1189 if (vo_spudec==NULL) {
1190 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1191 current_module="spudec_init_normal";
1192 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1193 spudec_set_font_factor(vo_spudec,font_factor);
1196 if (vo_spudec!=NULL) {
1197 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1198 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1203 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1204 * make it all work is to use the builtin SDL-bootstrap code, which
1205 * will be done automatically by replacing our main() if we include SDL.h.
1207 #if defined(__APPLE__) && defined(CONFIG_SDL)
1208 #ifdef CONFIG_SDL_SDL_H
1209 #include <SDL/SDL.h>
1210 #else
1211 #include <SDL.h>
1212 #endif
1213 #endif
1216 * \brief append a formatted string
1217 * \param buf buffer to print into
1218 * \param pos position of terminating 0 in buf
1219 * \param len maximum number of characters in buf, not including terminating 0
1220 * \param format printf format string
1222 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1224 va_list va;
1225 va_start(va, format);
1226 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1227 va_end(va);
1228 if (*pos >= len ) {
1229 buf[len] = 0;
1230 *pos = len;
1235 * \brief append time in the hh:mm:ss.f format
1236 * \param buf buffer to print into
1237 * \param pos position of terminating 0 in buf
1238 * \param len maximum number of characters in buf, not including terminating 0
1239 * \param time time value to convert/append
1241 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1242 long tenths = 10 * time;
1243 int f1 = tenths % 10;
1244 int ss = (tenths / 10) % 60;
1245 int mm = (tenths / 600) % 60;
1246 int hh = tenths / 36000;
1247 if (time <= 0) {
1248 saddf(buf, pos, len, "unknown");
1249 return;
1251 if (hh > 0)
1252 saddf(buf, pos, len, "%2d:", hh);
1253 if (hh > 0 || mm > 0)
1254 saddf(buf, pos, len, "%02d:", mm);
1255 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1258 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1260 struct MPOpts *opts = &mpctx->opts;
1261 sh_video_t * const sh_video = mpctx->sh_video;
1263 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1264 a_pos = playing_audio_pts(mpctx);
1265 if (mpctx->sh_audio && sh_video && at_frame) {
1266 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1267 if (mpctx->time_frame > 0)
1268 mpctx->last_av_difference += mpctx->time_frame * opts->playback_speed;
1269 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1270 && !mpctx->drop_message_shown) {
1271 mp_tmsg(MSGT_AVSYNC,MSGL_WARN,SystemTooSlow);
1272 mpctx->drop_message_shown = true;
1275 if (quiet)
1276 return;
1279 int width;
1280 char *line;
1281 unsigned pos = 0;
1282 get_screen_size();
1283 if (screen_width > 0)
1284 width = screen_width;
1285 else
1286 width = 80;
1287 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1288 /* Windows command line is broken (MinGW's rxvt works, but we
1289 * should not depend on that). */
1290 width--;
1291 #endif
1292 line = malloc(width + 1); // one additional char for the terminating null
1294 // Audio time
1295 if (mpctx->sh_audio) {
1296 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1297 if (!sh_video) {
1298 float len = demuxer_get_time_length(mpctx->demuxer);
1299 saddf(line, &pos, width, "(");
1300 sadd_hhmmssf(line, &pos, width, a_pos);
1301 saddf(line, &pos, width, ") of %.1f (", len);
1302 sadd_hhmmssf(line, &pos, width, len);
1303 saddf(line, &pos, width, ") ");
1307 // Video time
1308 if (sh_video)
1309 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1311 // A-V sync
1312 if (mpctx->sh_audio && sh_video)
1313 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1314 mpctx->last_av_difference, mpctx->total_avsync_change);
1316 // Video stats
1317 if (sh_video)
1318 saddf(line, &pos, width, "%3d/%3d ",
1319 (int)sh_video->num_frames,
1320 (int)sh_video->num_frames_decoded);
1322 // CPU usage
1323 if (sh_video) {
1324 if (sh_video->timer > 0.5)
1325 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1326 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1327 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1328 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1329 else
1330 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1331 } else if (mpctx->sh_audio) {
1332 if (mpctx->delay > 0.5)
1333 saddf(line, &pos, width, "%4.1f%% ",
1334 100.0*audio_time_usage/(double)mpctx->delay);
1335 else
1336 saddf(line, &pos, width, "??,?%% ");
1339 // VO stats
1340 if (sh_video)
1341 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1343 #ifdef CONFIG_STREAM_CACHE
1344 // cache stats
1345 if (stream_cache_size > 0)
1346 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1347 #endif
1349 // other
1350 if (opts->playback_speed != 1)
1351 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1353 // end
1354 if (erase_to_end_of_line) {
1355 line[pos] = 0;
1356 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1357 } else {
1358 memset(&line[pos], ' ', width - pos);
1359 line[width] = 0;
1360 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1362 free(line);
1366 * \brief build a chain of audio filters that converts the input format
1367 * to the ao's format, taking into account the current playback_speed.
1368 * \param sh_audio describes the requested input format of the chain.
1369 * \param ao_data describes the requested output format of the chain.
1371 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1373 struct MPOpts *opts = &mpctx->opts;
1374 int new_srate;
1375 int result;
1376 if (!sh_audio)
1378 mpctx->mixer.afilter = NULL;
1379 return 0;
1381 if(af_control_any_rev(sh_audio->afilter,
1382 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1383 &opts->playback_speed)) {
1384 new_srate = sh_audio->samplerate;
1385 } else {
1386 new_srate = sh_audio->samplerate * opts->playback_speed;
1387 if (new_srate != ao_data->samplerate) {
1388 // limits are taken from libaf/af_resample.c
1389 if (new_srate < 8000)
1390 new_srate = 8000;
1391 if (new_srate > 192000)
1392 new_srate = 192000;
1393 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1396 result = init_audio_filters(sh_audio, new_srate,
1397 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1398 mpctx->mixer.afilter = sh_audio->afilter;
1399 return result;
1403 typedef struct mp_osd_msg mp_osd_msg_t;
1404 struct mp_osd_msg {
1405 /// Previous message on the stack.
1406 mp_osd_msg_t* prev;
1407 /// Message text.
1408 char msg[128];
1409 int id,level,started;
1410 /// Display duration in ms.
1411 unsigned time;
1414 /// OSD message stack.
1415 static mp_osd_msg_t* osd_msg_stack = NULL;
1418 * \brief Add a message on the OSD message stack
1420 * If a message with the same id is already present in the stack
1421 * it is pulled on top of the stack, otherwise a new message is created.
1424 static void set_osd_msg_va(int id, int level, int time, const char *fmt,
1425 va_list ap)
1427 mp_osd_msg_t *msg,*last=NULL;
1428 int r;
1430 // look if the id is already in the stack
1431 for(msg = osd_msg_stack ; msg && msg->id != id ;
1432 last = msg, msg = msg->prev);
1433 // not found: alloc it
1434 if(!msg) {
1435 msg = calloc(1,sizeof(mp_osd_msg_t));
1436 msg->prev = osd_msg_stack;
1437 osd_msg_stack = msg;
1438 } else if(last) { // found, but it's not on top of the stack
1439 last->prev = msg->prev;
1440 msg->prev = osd_msg_stack;
1441 osd_msg_stack = msg;
1443 // write the msg
1444 r = vsnprintf(msg->msg, 128, fmt, ap);
1445 if(r >= 128) msg->msg[127] = 0;
1446 // set id and time
1447 msg->id = id;
1448 msg->level = level;
1449 msg->time = time;
1453 void set_osd_msg(int id, int level, int time, const char *fmt, ...)
1455 va_list ap;
1456 va_start(ap, fmt);
1457 set_osd_msg_va(id, level, time, fmt, ap);
1458 va_end(ap);
1461 void set_osd_tmsg(int id, int level, int time, const char *fmt, ...)
1463 va_list ap;
1464 va_start(ap, fmt);
1465 set_osd_msg_va(id, level, time, mp_gtext(fmt), ap);
1466 va_end(ap);
1471 * \brief Remove a message from the OSD stack
1473 * This function can be used to get rid of a message right away.
1477 void rm_osd_msg(int id) {
1478 mp_osd_msg_t *msg,*last=NULL;
1480 // Search for the msg
1481 for(msg = osd_msg_stack ; msg && msg->id != id ;
1482 last = msg, msg = msg->prev);
1483 if(!msg) return;
1485 // Detach it from the stack and free it
1486 if(last)
1487 last->prev = msg->prev;
1488 else
1489 osd_msg_stack = msg->prev;
1490 free(msg);
1494 * \brief Remove all messages from the OSD stack
1498 static void clear_osd_msgs(void) {
1499 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1500 while(msg) {
1501 prev = msg->prev;
1502 free(msg);
1503 msg = prev;
1505 osd_msg_stack = NULL;
1509 * \brief Get the current message from the OSD stack.
1511 * This function decrements the message timer and destroys the old ones.
1512 * The message that should be displayed is returned (if any).
1516 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1518 struct MPOpts *opts = &mpctx->opts;
1519 mp_osd_msg_t *msg,*prev,*last = NULL;
1520 static unsigned last_update = 0;
1521 unsigned now = GetTimerMS();
1522 unsigned diff;
1523 char hidden_dec_done = 0;
1525 if (mpctx->osd_visible) {
1526 // 36000000 means max timed visibility is 1 hour into the future, if
1527 // the difference is greater assume it's wrapped around from below 0
1528 if (mpctx->osd_visible - now > 36000000) {
1529 mpctx->osd_visible = 0;
1530 vo_osd_progbar_type = -1; // disable
1531 vo_osd_changed(OSDTYPE_PROGBAR);
1532 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1535 if (mpctx->osd_show_percentage_until - now > 36000000)
1536 mpctx->osd_show_percentage_until = 0;
1538 if(!last_update) last_update = now;
1539 diff = now >= last_update ? now - last_update : 0;
1541 last_update = now;
1543 // Look for the first message in the stack with high enough level.
1544 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1545 prev = msg->prev;
1546 if (msg->level > opts->osd_level && hidden_dec_done)
1547 continue;
1548 // The message has a high enough level or it is the first hidden one
1549 // in both cases we decrement the timer or kill it.
1550 if(!msg->started || msg->time > diff) {
1551 if(msg->started) msg->time -= diff;
1552 else msg->started = 1;
1553 // display it
1554 if (msg->level <= opts->osd_level)
1555 return msg;
1556 hidden_dec_done = 1;
1557 continue;
1559 // kill the message
1560 free(msg);
1561 if(last) {
1562 last->prev = prev;
1563 msg = last;
1564 } else {
1565 osd_msg_stack = prev;
1566 msg = NULL;
1569 // Nothing found
1570 return NULL;
1574 * \brief Display the OSD bar.
1576 * Display the OSD bar or fall back on a simple message.
1580 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1581 struct MPOpts *opts = &mpctx->opts;
1582 if (opts->osd_level < 1)
1583 return;
1585 if(mpctx->sh_video) {
1586 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1587 vo_osd_progbar_type = type;
1588 vo_osd_progbar_value = 256*(val-min)/(max-min);
1589 vo_osd_changed(OSDTYPE_PROGBAR);
1590 return;
1593 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1594 name, ROUND(100*(val-min)/(max-min)));
1598 * \brief Display text subtitles on the OSD
1600 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs)
1602 int i;
1603 vo_sub = subs;
1604 vo_osd_changed(OSDTYPE_SUBTITLE);
1605 if (!mpctx->sh_video) {
1606 // reverse order, since newest set_osd_msg is displayed first
1607 for (i = SUB_MAX_TEXT - 1; i >= 0; i--) {
1608 if (!subs || i >= subs->lines || !subs->text[i])
1609 rm_osd_msg(OSD_MSG_SUB_BASE + i);
1610 else {
1611 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1612 int display_time = i == subs->lines - 1 ? 180000 : 2000;
1613 set_osd_msg(OSD_MSG_SUB_BASE + i, 1, display_time, "%s", subs->text[i]);
1620 * \brief Update the OSD message line.
1622 * This function displays the current message on the vo OSD or on the term.
1623 * If the stack is empty and the OSD level is high enough the timer
1624 * is displayed (only on the vo OSD).
1628 static void update_osd_msg(struct MPContext *mpctx)
1630 struct MPOpts *opts = &mpctx->opts;
1631 mp_osd_msg_t *msg;
1632 struct osd_state *osd = mpctx->osd;
1633 char osd_text_timer[128];
1635 if (mpctx->add_osd_seek_info) {
1636 double percentage;
1637 if (mpctx->timeline && mpctx->sh_video)
1638 percentage = mpctx->sh_video->pts * 100 /
1639 mpctx->timeline[mpctx->num_timeline_parts].start;
1640 else
1641 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1642 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1643 if (mpctx->sh_video)
1644 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1645 mpctx->add_osd_seek_info = false;
1648 // Look if we have a msg
1649 if((msg = get_osd_msg(mpctx))) {
1650 if (strcmp(osd->osd_text, msg->msg)) {
1651 strncpy(osd->osd_text, msg->msg, 127);
1652 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1653 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1655 return;
1658 if(mpctx->sh_video) {
1659 // fallback on the timer
1660 if (opts->osd_level >= 2) {
1661 int len;
1662 if (mpctx->timeline)
1663 len = mpctx->timeline[mpctx->num_timeline_parts].start;
1664 else
1665 len = demuxer_get_time_length(mpctx->demuxer);
1666 int percentage = -1;
1667 char percentage_text[10];
1668 int pts = demuxer_get_current_time(mpctx->demuxer);
1670 if (mpctx->osd_show_percentage_until)
1671 if (mpctx->timeline)
1672 percentage = mpctx->sh_video->pts * 100 /
1673 mpctx->timeline[mpctx->num_timeline_parts].start;
1674 else
1675 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1677 if (percentage >= 0)
1678 snprintf(percentage_text, 9, " (%d%%)", percentage);
1679 else
1680 percentage_text[0] = 0;
1682 if (opts->osd_level == 3)
1683 snprintf(osd_text_timer, 63,
1684 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1685 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1686 len/3600,(len/60)%60,len%60,percentage_text);
1687 else
1688 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1689 mpctx->osd_function,pts/3600,(pts/60)%60,
1690 pts%60,percentage_text);
1691 } else
1692 osd_text_timer[0]=0;
1694 if (strcmp(osd->osd_text, osd_text_timer)) {
1695 strncpy(osd->osd_text, osd_text_timer, 63);
1696 vo_osd_changed(OSDTYPE_OSD);
1698 return;
1701 // Clear the term osd line
1702 if (term_osd && osd->osd_text[0]) {
1703 osd->osd_text[0] = 0;
1704 printf("%s\n",term_osd_esc);
1708 ///@}
1709 // OSDMsgStack
1712 void reinit_audio_chain(struct MPContext *mpctx)
1714 struct MPOpts *opts = &mpctx->opts;
1715 if (!mpctx->sh_audio)
1716 return;
1717 current_module="init_audio_codec";
1718 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1719 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1720 goto init_error;
1722 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1723 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1726 current_module="af_preinit";
1727 ao_data.samplerate=force_srate;
1728 ao_data.channels=0;
1729 ao_data.format=audio_output_format;
1730 #if 1
1731 // first init to detect best values
1732 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1733 // input:
1734 mpctx->sh_audio->samplerate,
1735 // output:
1736 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1737 mp_tmsg(MSGT_CPLAYER,MSGL_ERR, "Error at audio filter chain "
1738 "pre-init!\n");
1739 exit_player(mpctx, EXIT_ERROR);
1741 #endif
1742 current_module="ao2_init";
1743 mpctx->audio_out = init_best_audio_out(opts->audio_driver_list,
1744 0, // plugin flag
1745 ao_data.samplerate,
1746 ao_data.channels,
1747 ao_data.format, 0);
1748 if(!mpctx->audio_out){
1749 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Could not open/initialize audio device -> no sound.\n");
1750 goto init_error;
1752 mpctx->initialized_flags|=INITIALIZED_AO;
1753 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1754 mpctx->audio_out->info->short_name,
1755 ao_data.samplerate, ao_data.channels,
1756 af_fmt2str_short(ao_data.format),
1757 af_fmt2bits(ao_data.format)/8 );
1758 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1759 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1760 if(strlen(mpctx->audio_out->info->comment) > 0)
1761 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1762 // init audio filters:
1763 #if 1
1764 current_module="af_init";
1765 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1766 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter/ao format!\n");
1767 goto init_error;
1769 #endif
1770 mpctx->mixer.audio_out = mpctx->audio_out;
1771 mpctx->mixer.volstep = volstep;
1772 return;
1774 init_error:
1775 uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec and possibly AO
1776 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1777 mpctx->d_audio->id = -2;
1781 ///@}
1782 // Command2Property
1785 // Return pts value corresponding to the end point of audio written to the
1786 // ao so far.
1787 static double written_audio_pts(struct MPContext *mpctx)
1789 sh_audio_t *sh_audio = mpctx->sh_audio;
1790 demux_stream_t *d_audio = mpctx->d_audio;
1791 double buffered_output;
1792 // first calculate the end pts of audio that has been output by decoder
1793 double a_pts = sh_audio->pts;
1794 if (a_pts != MP_NOPTS_VALUE)
1795 // Good, decoder supports new way of calculating audio pts.
1796 // sh_audio->pts is the timestamp of the latest input packet with
1797 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1798 // the amount of bytes the decoder has written after that timestamp.
1799 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1800 else {
1801 // Decoder doesn't support new way of calculating pts (or we're
1802 // being called before it has decoded anything with known timestamp).
1803 // Use the old method of audio pts calculation: take the timestamp
1804 // of last packet with known pts the decoder has read data from,
1805 // and add amount of bytes read after the beginning of that packet
1806 // divided by input bps. This will be inaccurate if the input/output
1807 // ratio is not constant for every audio packet or if it is constant
1808 // but not accurately known in sh_audio->i_bps.
1810 a_pts = d_audio->pts;
1811 // ds_tell_pts returns bytes read after last timestamp from
1812 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1813 // it has read but not decoded
1814 if (sh_audio->i_bps)
1815 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1816 (double)sh_audio->i_bps;
1818 // Now a_pts hopefully holds the pts for end of audio from decoder.
1819 // Substract data in buffers between decoder and audio out.
1821 // Decoded but not filtered
1822 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1824 // Data buffered in audio filters, measured in bytes of "missing" output
1825 buffered_output = af_calc_delay(sh_audio->afilter);
1827 // Data that was ready for ao but was buffered because ao didn't fully
1828 // accept everything to internal buffers yet
1829 buffered_output += sh_audio->a_out_buffer_len;
1831 // Filters divide audio length by playback_speed, so multiply by it
1832 // to get the length in original units without speedup or slowdown
1833 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1835 return a_pts + mpctx->video_offset;
1838 // Return pts value corresponding to currently playing audio.
1839 double playing_audio_pts(struct MPContext *mpctx)
1841 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1842 mpctx->audio_out->get_delay();
1845 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1846 struct MPOpts *opts = &mpctx->opts;
1847 // check for frame-drop:
1848 current_module = "check_framedrop";
1849 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1850 static int dropped_frames;
1851 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1852 float d = delay-mpctx->delay;
1853 ++total_frame_cnt;
1854 // we should avoid dropping too many frames in sequence unless we
1855 // are too late. and we allow 100ms A-V delay here:
1856 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1857 && !mpctx->update_video_immediately) {
1858 ++drop_frame_cnt;
1859 ++dropped_frames;
1860 return frame_dropping;
1861 } else
1862 dropped_frames = 0;
1864 return 0;
1868 #ifdef HAVE_RTC
1869 int rtc_fd = -1;
1870 #endif
1872 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1874 #ifdef HAVE_RTC
1875 if (rtc_fd >= 0){
1876 // -------- RTC -----------
1877 current_module="sleep_rtc";
1878 while (time_frame > 0.000) {
1879 unsigned long rtc_ts;
1880 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1881 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC read error: %s\n", strerror(errno));
1882 time_frame -= get_relative_time(mpctx);
1884 } else
1885 #endif
1887 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1888 // unnecessarily high CPU usage
1889 float margin = softsleep ? 0.011 : 0;
1890 current_module = "sleep_timer";
1891 while (time_frame > margin) {
1892 usec_sleep(1000000 * (time_frame - margin));
1893 time_frame -= get_relative_time(mpctx);
1895 if (softsleep){
1896 current_module = "sleep_soft";
1897 if (time_frame < 0)
1898 mp_tmsg(MSGT_AVSYNC, MSGL_WARN, "Warning! Softsleep underflow!\n");
1899 while (time_frame > 0)
1900 time_frame -= get_relative_time(mpctx); // burn the CPU
1903 return time_frame;
1906 static void select_subtitle(MPContext *mpctx)
1908 struct MPOpts *opts = &mpctx->opts;
1909 // find the best sub to use
1910 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
1911 mpctx->global_sub_pos = -1; // no subs by default
1912 if (vobsub_index_id >= 0) {
1913 // if user asks for a vobsub id, use that first.
1914 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
1915 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1916 // if user asks for a dvd sub id, use that next.
1917 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1918 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
1919 // if there are text subs to use, use those. (autosubs come last here)
1920 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
1921 } else if (opts->sub_id == -1 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1922 // finally select subs by language and container hints
1923 if (opts->sub_id == -1 && dvdsub_lang)
1924 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
1925 if (opts->sub_id == -1)
1926 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
1927 if (opts->sub_id >= 0)
1928 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1930 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
1931 mpctx->global_sub_pos--;
1932 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
1935 #ifdef CONFIG_DVDNAV
1936 #ifndef FF_B_TYPE
1937 #define FF_B_TYPE 3
1938 #endif
1939 /// store decoded video image
1940 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1941 mp_image_t *from_mpi) {
1942 mp_image_t *mpi;
1944 /// Do not store B-frames
1945 if (from_mpi->pict_type == FF_B_TYPE)
1946 return to_mpi;
1948 if (to_mpi &&
1949 to_mpi->w == from_mpi->w &&
1950 to_mpi->h == from_mpi->h &&
1951 to_mpi->imgfmt == from_mpi->imgfmt)
1952 mpi = to_mpi;
1953 else {
1954 if (to_mpi)
1955 free_mp_image(to_mpi);
1956 if (from_mpi->w == 0 || from_mpi->h == 0)
1957 return NULL;
1958 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1961 copy_mpi(mpi,from_mpi);
1962 return mpi;
1965 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1966 struct MPOpts *opts = &ctx->opts;
1967 if (ctx->sh_video) {
1968 /// clear video pts
1969 ctx->d_video->pts = 0.0f;
1970 ctx->sh_video->pts = 0.0f;
1971 ctx->sh_video->i_pts = 0.0f;
1972 ctx->sh_video->last_pts = 0.0f;
1973 ctx->sh_video->num_buffered_pts = 0;
1974 ctx->sh_video->num_frames = 0;
1975 ctx->sh_video->num_frames_decoded = 0;
1976 ctx->sh_video->timer = 0.0f;
1977 ctx->sh_video->stream_delay = 0.0f;
1978 ctx->sh_video->timer = 0;
1979 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1982 if (ctx->sh_audio) {
1983 /// free audio packets and reset
1984 ds_free_packs(ctx->d_audio);
1985 audio_delay -= ctx->sh_audio->stream_delay;
1986 ctx->delay =- audio_delay;
1987 ctx->audio_out->reset();
1988 resync_audio_stream(ctx->sh_audio);
1991 audio_delay = 0.0f;
1992 ctx->global_sub_size = ctx->global_sub_indices[SUB_SOURCE_DEMUX] + mp_dvdnav_number_of_subs(ctx->stream);
1993 if (dvdsub_lang && opts->sub_id == dvdsub_lang_id) {
1994 dvdsub_lang_id = mp_dvdnav_sid_from_lang(ctx->stream, dvdsub_lang);
1995 if (dvdsub_lang_id != opts->sub_id) {
1996 opts->sub_id = dvdsub_lang_id;
1997 select_subtitle(ctx);
2001 /// clear all EOF related flags
2002 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
2005 /// Restore last decoded DVDNAV (still frame)
2006 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
2007 int *in_size,
2008 unsigned char **start,
2009 mp_image_t *decoded_frame)
2011 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
2012 return decoded_frame;
2014 /// a change occured in dvdnav stream
2015 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
2016 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
2017 mp_dvdnav_context_free(mpctx);
2018 mp_dvdnav_reset_stream(mpctx);
2019 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
2020 mp_dvdnav_cell_has_changed(mpctx->stream,1);
2023 if (*in_size < 0) {
2024 float len;
2026 /// Display still frame, if any
2027 if (mpctx->nav_smpi && !mpctx->nav_buffer)
2028 decoded_frame = mpctx->nav_smpi;
2030 /// increment video frame : continue playing after still frame
2031 len = demuxer_get_time_length(mpctx->demuxer);
2032 if (mpctx->sh_video->pts >= len &&
2033 mpctx->sh_video->pts > 0.0 && len > 0.0) {
2034 mp_dvdnav_skip_still(mpctx->stream);
2035 mp_dvdnav_skip_wait(mpctx->stream);
2037 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
2039 if (mpctx->nav_buffer) {
2040 *start = mpctx->nav_start;
2041 *in_size = mpctx->nav_in_size;
2042 if (mpctx->nav_start)
2043 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
2047 return decoded_frame;
2050 /// Save last decoded DVDNAV (still frame)
2051 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
2052 unsigned char *start,
2053 mp_image_t *decoded_frame)
2055 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
2056 return;
2058 if (mpctx->nav_buffer)
2059 free(mpctx->nav_buffer);
2061 mpctx->nav_buffer = malloc(in_size);
2062 mpctx->nav_start = start;
2063 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
2064 if (mpctx->nav_buffer)
2065 memcpy(mpctx->nav_buffer,start,in_size);
2067 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
2068 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
2070 #endif /* CONFIG_DVDNAV */
2072 /* Modify video timing to match the audio timeline. There are two main
2073 * reasons this is needed. First, video and audio can start from different
2074 * positions at beginning of file or after a seek (MPlayer starts both
2075 * immediately even if they have different pts). Second, the file can have
2076 * audio timestamps that are inconsistent with the duration of the audio
2077 * packets, for example two consecutive timestamp values differing by
2078 * one second but only a packet with enough samples for half a second
2079 * of playback between them.
2081 static void adjust_sync(struct MPContext *mpctx, double frame_time)
2083 current_module = "av_sync";
2085 if (!mpctx->sh_audio)
2086 return;
2088 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
2089 double v_pts = mpctx->sh_video->pts;
2090 double av_delay = a_pts - v_pts;
2091 // Try to sync vo_flip() so it will *finish* at given time
2092 av_delay += mpctx->last_vo_flip_duration;
2093 av_delay -= audio_delay; // This much pts difference is desired
2095 double change = av_delay * 0.1;
2096 double max_change = default_max_pts_correction >= 0 ?
2097 default_max_pts_correction : frame_time * 0.1;
2098 if (change < -max_change)
2099 change = -max_change;
2100 else if (change > max_change)
2101 change = max_change;
2102 mpctx->delay += change;
2103 mpctx->total_avsync_change += change;
2106 static int fill_audio_out_buffers(struct MPContext *mpctx)
2108 struct MPOpts *opts = &mpctx->opts;
2109 unsigned int t;
2110 double tt;
2111 int playsize;
2112 int playflags=0;
2113 int audio_eof=0;
2114 sh_audio_t * const sh_audio = mpctx->sh_audio;
2116 current_module="play_audio";
2118 while (1) {
2119 int sleep_time;
2120 // all the current uses of ao_data.pts seem to be in aos that handle
2121 // sync completely wrong; there should be no need to use ao_data.pts
2122 // in get_space()
2123 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2124 playsize = mpctx->audio_out->get_space();
2125 if (mpctx->sh_video || playsize >= ao_data.outburst)
2126 break;
2128 // handle audio-only case:
2129 // this is where mplayer sleeps during audio-only playback
2130 // to avoid 100% CPU use
2131 sleep_time = (ao_data.outburst - playsize) * 1000 / ao_data.bps;
2132 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
2133 usec_sleep(sleep_time * 1000);
2136 // Fill buffer if needed:
2137 current_module="decode_audio";
2138 t = GetTimer();
2139 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2140 if (mpctx->d_audio->eof) {
2141 audio_eof = 1;
2142 if (sh_audio->a_out_buffer_len == 0)
2143 return 0;
2145 t = GetTimer() - t;
2146 tt = t*0.000001f; audio_time_usage+=tt;
2147 if (playsize > sh_audio->a_out_buffer_len) {
2148 playsize = sh_audio->a_out_buffer_len;
2149 if (audio_eof)
2150 playflags |= AOPLAY_FINAL_CHUNK;
2152 if (!playsize)
2153 return 1;
2155 // play audio:
2156 current_module="play_audio";
2158 // Is this pts value actually useful for the aos that access it?
2159 // They're obviously badly broken in the way they handle av sync;
2160 // would not having access to this make them more broken?
2161 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2162 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2164 if (playsize > 0) {
2165 sh_audio->a_out_buffer_len -= playsize;
2166 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2167 sh_audio->a_out_buffer_len);
2168 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2170 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2171 // Sanity check to avoid hanging in case current ao doesn't output
2172 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2173 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2174 sh_audio->a_out_buffer_len = 0;
2177 return 1;
2180 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2181 float *aq_sleep_time)
2183 struct MPOpts *opts = &mpctx->opts;
2184 int frame_time_remaining = 0;
2185 current_module="calc_sleep_time";
2187 *time_frame -= get_relative_time(mpctx); // reset timer
2189 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2190 float delay = mpctx->audio_out->get_delay();
2191 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2193 if (autosync) {
2195 * Adjust this raw delay value by calculating the expected
2196 * delay for this frame and generating a new value which is
2197 * weighted between the two. The higher autosync is, the
2198 * closer to the delay value gets to that which "-nosound"
2199 * would have used, and the longer it will take for A/V
2200 * sync to settle at the right value (but it eventually will.)
2201 * This settling time is very short for values below 100.
2203 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2204 float difference = delay - predicted;
2205 delay = predicted + difference / (float)autosync;
2208 *time_frame = delay - mpctx->delay / opts->playback_speed;
2210 // delay = amount of audio buffered in soundcard/driver
2211 if (delay > 0.25) delay=0.25; else
2212 if (delay < 0.10) delay=0.10;
2213 if (*time_frame > delay*0.6) {
2214 // sleep time too big - may cause audio drops (buffer underrun)
2215 frame_time_remaining = 1;
2216 *time_frame = delay*0.5;
2218 } else {
2219 // If we're lagging more than 200 ms behind the right playback rate,
2220 // don't try to "catch up".
2221 // If benchmark is set always output frames as fast as possible
2222 // without sleeping.
2223 if (*time_frame < -0.2 || benchmark)
2224 *time_frame = 0;
2227 *aq_sleep_time += *time_frame;
2230 //============================== SLEEP: ===================================
2232 *time_frame -= mpctx->video_out->flip_queue_offset;
2233 // flag 256 means: libvo driver does its timing (dvb card)
2234 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2235 *time_frame = timing_sleep(mpctx, *time_frame);
2236 *time_frame += mpctx->video_out->flip_queue_offset;
2237 return frame_time_remaining;
2240 int reinit_video_chain(struct MPContext *mpctx)
2242 struct MPOpts *opts = &mpctx->opts;
2243 sh_video_t * const sh_video = mpctx->sh_video;
2244 double ar=-1.0;
2245 //================== Init VIDEO (codec & libvo) ==========================
2246 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2247 current_module="preinit_libvo";
2249 //shouldn't we set dvideo->id=-2 when we fail?
2250 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2251 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2252 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Error opening/initializing the selected video_out (-vo) device.\n");
2253 goto err_out;
2255 mpctx->initialized_flags|=INITIALIZED_VO;
2258 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2259 mpctx->sh_video->stream_aspect = ar;
2260 current_module="init_video_filters";
2262 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2263 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2265 #ifdef CONFIG_MENU
2266 if(use_menu) {
2267 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2268 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2269 if(!vf_menu) {
2270 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Can't open libmenu video filter with root menu %s.\n",menu_root);
2271 use_menu = 0;
2274 if(vf_menu)
2275 sh_video->vfilter = vf_menu;
2276 #endif
2278 #ifdef CONFIG_ASS
2279 if(opts->ass_enabled) {
2280 int i;
2281 int insert = 1;
2282 if (opts->vf_settings)
2283 for (i = 0; opts->vf_settings[i].name; ++i)
2284 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2285 insert = 0;
2286 break;
2288 if (insert) {
2289 extern vf_info_t vf_info_ass;
2290 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2291 char* vf_arg[] = {"auto", "1", NULL};
2292 int retcode = 0;
2293 struct vf_instance *vf_ass = vf_open_plugin_noerr(opts, libass_vfs,
2294 sh_video->vfilter,
2295 "ass", vf_arg,
2296 &retcode);
2297 if (vf_ass)
2298 sh_video->vfilter = vf_ass;
2299 else if (retcode == -1) // vf_ass open() returns -1 if there's VO EOSD
2300 mp_msg(MSGT_CPLAYER, MSGL_V, "[ass] vf_ass not needed\n");
2301 else
2302 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2305 #endif
2307 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2309 #ifdef CONFIG_ASS
2310 if (opts->ass_enabled)
2311 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2312 #endif
2314 current_module="init_video_codec";
2316 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2317 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2318 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2320 if(!sh_video->initialized){
2321 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2322 goto err_out;
2325 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2327 if (sh_video->codec)
2328 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2330 sh_video->last_pts = MP_NOPTS_VALUE;
2331 sh_video->num_buffered_pts = 0;
2332 sh_video->next_frame_time = 0;
2334 if(auto_quality>0){
2335 // Auto quality option enabled
2336 output_quality=get_video_quality_max(sh_video);
2337 if(auto_quality>output_quality) auto_quality=output_quality;
2338 else output_quality=auto_quality;
2339 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2340 set_video_quality(sh_video,output_quality);
2343 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2345 current_module="init_vo";
2347 return 1;
2349 err_out:
2350 mpctx->sh_video = mpctx->d_video->sh = NULL;
2351 return 0;
2354 static double update_video_nocorrect_pts(struct MPContext *mpctx)
2356 struct sh_video *sh_video = mpctx->sh_video;
2357 double frame_time = 0;
2358 struct vo *video_out = mpctx->video_out;
2359 while (!video_out->frame_loaded) {
2360 current_module = "filter_video";
2361 // In nocorrect-pts mode there is no way to properly time these frames
2362 if (vo_get_buffered_frame(video_out, 0) >= 0)
2363 break;
2364 if (vf_output_queued_frame(sh_video->vfilter))
2365 continue;
2366 unsigned char *packet = NULL;
2367 frame_time = sh_video->next_frame_time;
2368 if (mpctx->update_video_immediately)
2369 frame_time = 0;
2370 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2371 &packet, force_fps);
2372 if (in_size < 0) {
2373 #ifdef CONFIG_DVDNAV
2374 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2375 if (mp_dvdnav_is_eof(mpctx->stream))
2376 return -1;
2377 if (mpctx->d_video)
2378 mpctx->d_video->eof = 0;
2379 if (mpctx->d_audio)
2380 mpctx->d_audio->eof = 0;
2381 mpctx->stream->eof = 0;
2382 } else
2383 #endif
2384 return -1;
2386 if (in_size > max_framesize)
2387 max_framesize = in_size;
2388 sh_video->timer += frame_time;
2389 if (mpctx->sh_audio)
2390 mpctx->delay -= frame_time;
2391 // video_read_frame can change fps (e.g. for ASF video)
2392 vo_fps = sh_video->fps;
2393 int framedrop_type = check_framedrop(mpctx, frame_time);
2394 current_module = "decode video";
2396 void *decoded_frame;
2397 #ifdef CONFIG_DVDNAV
2398 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2399 if (in_size >= 0 && !decoded_frame)
2400 #endif
2401 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2402 sh_video->pts);
2403 #ifdef CONFIG_DVDNAV
2404 // Save last still frame for future display
2405 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2406 #endif
2407 if (decoded_frame) {
2408 current_module = "filter video";
2409 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2410 if (!video_out->config_ok)
2411 break;
2414 return frame_time;
2417 static void determine_frame_pts(struct MPContext *mpctx)
2419 struct sh_video *sh_video = mpctx->sh_video;
2420 struct MPOpts *opts = &mpctx->opts;
2422 if (opts->user_pts_assoc_mode) {
2423 sh_video->pts_assoc_mode = opts->user_pts_assoc_mode;
2424 } else if (sh_video->pts_assoc_mode == 0) {
2425 if (sh_video->codec_reordered_pts != MP_NOPTS_VALUE)
2426 sh_video->pts_assoc_mode = 1;
2427 else
2428 sh_video->pts_assoc_mode = 2;
2429 } else {
2430 int probcount1 = sh_video->num_reordered_pts_problems;
2431 int probcount2 = sh_video->num_sorted_pts_problems;
2432 if (sh_video->pts_assoc_mode == 2) {
2433 int tmp = probcount1;
2434 probcount1 = probcount2;
2435 probcount2 = tmp;
2437 if (probcount1 >= probcount2 * 1.5 + 2) {
2438 sh_video->pts_assoc_mode = 3 - sh_video->pts_assoc_mode;
2439 mp_msg(MSGT_CPLAYER, MSGL_V, "Switching to pts association mode "
2440 "%d.\n", sh_video->pts_assoc_mode);
2443 sh_video->pts = sh_video->pts_assoc_mode == 1 ?
2444 sh_video->codec_reordered_pts : sh_video->sorted_pts;
2447 static double update_video(struct MPContext *mpctx)
2449 struct sh_video *sh_video = mpctx->sh_video;
2450 struct vo *video_out = mpctx->video_out;
2451 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2452 mpctx->osd); // hack for vf_expand
2453 if (!mpctx->opts.correct_pts)
2454 return update_video_nocorrect_pts(mpctx);
2456 double pts;
2458 bool hit_eof = false;
2459 while (!video_out->frame_loaded) {
2460 current_module = "filter_video";
2461 if (vo_get_buffered_frame(video_out, hit_eof) >= 0)
2462 break;
2463 // XXX Time used in this call is not counted in any performance
2464 // timer now, OSD time is not updated correctly for filter-added frames
2465 if (vf_output_queued_frame(sh_video->vfilter))
2466 continue;
2467 if (hit_eof)
2468 return -1;
2469 unsigned char *packet = NULL;
2470 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2471 if (pts != MP_NOPTS_VALUE)
2472 pts += mpctx->video_offset;
2473 if (in_size < 0) {
2474 // try to extract last frames in case of decoder lag
2475 in_size = 0;
2476 pts = 1e300;
2477 hit_eof = true;
2479 if (in_size > max_framesize)
2480 max_framesize = in_size;
2481 current_module = "decode video";
2482 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2483 void *decoded_frame = decode_video(sh_video, packet, in_size,
2484 framedrop_type, pts);
2485 if (decoded_frame) {
2486 determine_frame_pts(mpctx);
2487 current_module = "filter video";
2488 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2489 if (!video_out->config_ok)
2490 break; // We'd likely hang in this loop otherwise
2494 pts = video_out->next_pts;
2495 if (pts == MP_NOPTS_VALUE) {
2496 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2497 // Try to use decoder pts from before filters
2498 pts = sh_video->pts;
2499 if (pts == MP_NOPTS_VALUE)
2500 pts = sh_video->last_pts;
2502 sh_video->pts = pts;
2503 if (sh_video->last_pts == MP_NOPTS_VALUE)
2504 sh_video->last_pts = sh_video->pts;
2505 else if (sh_video->last_pts > sh_video->pts) {
2506 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2507 sh_video->pts, sh_video->last_pts);
2508 /* If the difference in pts is small treat it as jitter around the
2509 * right value (possibly caused by incorrect timestamp ordering) and
2510 * just show this frame immediately after the last one.
2511 * Treat bigger differences as timestamp resets and start counting
2512 * timing of later frames from the position of this one. */
2513 if (sh_video->last_pts - sh_video->pts > 0.5)
2514 sh_video->last_pts = sh_video->pts;
2515 else
2516 sh_video->pts = sh_video->last_pts;
2518 double frame_time = sh_video->pts - sh_video->last_pts;
2519 sh_video->last_pts = sh_video->pts;
2520 sh_video->timer += frame_time;
2521 if (mpctx->sh_audio)
2522 mpctx->delay -= frame_time;
2523 return frame_time;
2526 void pause_player(struct MPContext *mpctx)
2528 if (mpctx->paused)
2529 return;
2530 mpctx->paused = 1;
2531 mpctx->step_frames = 0;
2532 mpctx->time_frame -= get_relative_time(mpctx);
2534 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2535 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2537 if (mpctx->audio_out && mpctx->sh_audio)
2538 mpctx->audio_out->pause(); // pause audio, keep data if possible
2541 void unpause_player(struct MPContext *mpctx)
2543 if (!mpctx->paused)
2544 return;
2545 mpctx->paused = 0;
2547 if (mpctx->audio_out && mpctx->sh_audio)
2548 mpctx->audio_out->resume(); // resume audio
2549 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2550 && !mpctx->step_frames)
2551 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2552 (void)get_relative_time(mpctx); // ignore time that passed during pause
2555 void add_step_frame(struct MPContext *mpctx)
2557 mpctx->step_frames++;
2558 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2559 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2560 unpause_player(mpctx);
2563 static void pause_loop(struct MPContext *mpctx)
2565 mp_cmd_t* cmd;
2566 if (!quiet) {
2567 // Small hack to display the pause message on the OSD line.
2568 // The pause string is: "\n == PAUSE == \r" so we need to
2569 // take the first and the last char out
2570 if (term_osd && !mpctx->sh_video) {
2571 char msg[128] = _("\n ===== PAUSE =====\r");
2572 int mlen = strlen(msg);
2573 msg[mlen-1] = '\0';
2574 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2575 update_osd_msg(mpctx);
2576 } else
2577 mp_tmsg(MSGT_CPLAYER,MSGL_STATUS,"\n ===== PAUSE =====\r");
2578 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2581 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2582 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2583 if (cmd) {
2584 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2585 run_command(mpctx, cmd);
2586 mp_cmd_free(cmd);
2587 continue;
2589 if (mpctx->sh_video && mpctx->video_out)
2590 vo_check_events(mpctx->video_out);
2591 #ifdef CONFIG_MENU
2592 if (vf_menu)
2593 vf_menu_pause_update(vf_menu);
2594 #endif
2595 usec_sleep(20000);
2596 update_osd_msg(mpctx);
2597 int hack = vo_osd_changed(0);
2598 vo_osd_changed(hack);
2599 if (hack)
2600 break;
2605 // Find the right mute status and record position for new file position
2606 static void edl_seek_reset(MPContext *mpctx)
2608 mpctx->edl_muted = 0;
2609 next_edl_record = edl_records;
2611 while (next_edl_record) {
2612 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2613 break;
2615 if (next_edl_record->action == EDL_MUTE)
2616 mpctx->edl_muted = !mpctx->edl_muted;
2617 next_edl_record = next_edl_record->next;
2619 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2620 mixer_mute(&mpctx->mixer);
2624 // Execute EDL command for the current position if one exists
2625 static void edl_update(MPContext *mpctx)
2627 if (!next_edl_record)
2628 return;
2630 if (!mpctx->sh_video) {
2631 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot use EDL without video, disabling.\n");
2632 free_edl(edl_records);
2633 next_edl_record = NULL;
2634 edl_records = NULL;
2635 return;
2638 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2639 if (next_edl_record->action == EDL_SKIP) {
2640 mpctx->osd_function = OSD_FFW;
2641 mpctx->abs_seek_pos = 0;
2642 mpctx->rel_seek_secs = next_edl_record->length_sec;
2643 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2644 "[%f], length [%f]\n", next_edl_record->start_sec,
2645 next_edl_record->stop_sec, next_edl_record->length_sec);
2647 else if (next_edl_record->action == EDL_MUTE) {
2648 mpctx->edl_muted = !mpctx->edl_muted;
2649 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2650 mixer_mute(&mpctx->mixer);
2651 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2652 next_edl_record->start_sec );
2654 next_edl_record = next_edl_record->next;
2658 static void reinit_decoders(struct MPContext *mpctx)
2660 reinit_video_chain(mpctx);
2661 reinit_audio_chain(mpctx);
2662 mp_property_do("sub", M_PROPERTY_SET, &mpctx->global_sub_pos, mpctx);
2665 static bool timeline_set_part(struct MPContext *mpctx, int i)
2667 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
2668 struct timeline_part *n = mpctx->timeline + i;
2669 mpctx->timeline_part = i;
2670 mpctx->video_offset = n->start - n->source_start;
2671 if (n->source == p->source)
2672 return false;
2673 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo && mpctx->opts.video_id != -2 ? 0 : INITIALIZED_VO) | INITIALIZED_AO | INITIALIZED_ACODEC);
2674 mpctx->demuxer = n->source->demuxer;
2675 mpctx->d_video = mpctx->demuxer->video;
2676 mpctx->d_audio = mpctx->demuxer->audio;
2677 mpctx->d_sub = mpctx->demuxer->sub;
2678 mpctx->sh_video = mpctx->d_video->sh;
2679 mpctx->sh_audio = mpctx->d_audio->sh;
2680 return true;
2683 // Given pts, switch playback to the corresponding part.
2684 // Return offset within that part.
2685 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
2686 bool *need_reset)
2688 if (pts < 0)
2689 pts = 0;
2690 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
2691 struct timeline_part *p = mpctx->timeline + i;
2692 if (pts < (p+1)->start) {
2693 *need_reset = timeline_set_part(mpctx, i);
2694 return pts - p->start + p->source_start;
2697 return -1;
2701 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2702 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2703 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2704 static int seek(MPContext *mpctx, double amount, int style)
2706 current_module = "seek";
2707 if (mpctx->stop_play == AT_END_OF_FILE)
2708 mpctx->stop_play = KEEP_PLAYING;
2709 if (mpctx->timeline && style & SEEK_FACTOR) {
2710 amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
2711 style &= ~SEEK_FACTOR;
2713 if ((mpctx->demuxer->accurate_seek || mpctx->timeline) && mpctx->sh_video
2714 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2715 style |= SEEK_ABSOLUTE;
2716 if (amount > 0)
2717 style |= SEEK_FORWARD;
2718 else
2719 style |= SEEK_BACKWARD;
2720 amount += mpctx->sh_video->pts;
2723 /* At least the liba52 decoder wants to read from the input stream
2724 * during initialization, so reinit must be done after the demux_seek()
2725 * call that clears possible stream EOF. */
2726 bool need_reset = false;
2727 if (mpctx->timeline) {
2728 amount = timeline_set_from_time(mpctx, amount, &need_reset);
2729 if (amount == -1) {
2730 mpctx->stop_play = AT_END_OF_FILE;
2731 // Clear audio from current position
2732 if (mpctx->sh_audio) {
2733 mpctx->audio_out->reset();
2734 mpctx->sh_audio->a_buffer_len = 0;
2735 mpctx->sh_audio->a_out_buffer_len = 0;
2737 return -1;
2740 int seekresult = demux_seek(mpctx->demuxer, amount, audio_delay, style);
2741 if (need_reset)
2742 reinit_decoders(mpctx);
2743 if (seekresult == 0)
2744 return -1;
2746 if (mpctx->sh_video) {
2747 current_module = "seek_video_reset";
2748 resync_video_stream(mpctx->sh_video);
2749 vo_seek_reset(mpctx->video_out);
2750 mpctx->sh_video->num_buffered_pts = 0;
2751 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2752 mpctx->delay = 0;
2753 mpctx->time_frame = 0;
2754 mpctx->update_video_immediately = true;
2755 // Not all demuxers set d_video->pts during seek, so this value
2756 // (which is used by at least vobsub and edl code below) may
2757 // be completely wrong (probably 0).
2758 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
2759 update_subtitles(mpctx, &mpctx->opts, mpctx->sh_video,
2760 mpctx->sh_video->pts, mpctx->video_offset,
2761 mpctx->d_sub, 1);
2762 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2765 if (mpctx->sh_audio) {
2766 current_module = "seek_audio_reset";
2767 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2768 mpctx->sh_audio->a_buffer_len = 0;
2769 mpctx->sh_audio->a_out_buffer_len = 0;
2770 if (!mpctx->sh_video)
2771 update_subtitles(mpctx, &mpctx->opts, NULL, mpctx->sh_audio->pts,
2772 mpctx->video_offset, mpctx->d_sub, 1);
2775 if (vo_vobsub && mpctx->sh_video) {
2776 current_module = "seek_vobsub_reset";
2777 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2780 edl_seek_reset(mpctx);
2782 mpctx->total_avsync_change = 0;
2783 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2784 drop_frame_cnt = 0;
2786 current_module = NULL;
2787 return 0;
2790 int get_current_chapter(struct MPContext *mpctx)
2792 if (!mpctx->chapters || !mpctx->sh_video)
2793 return demuxer_get_current_chapter(mpctx->demuxer);
2795 int i;
2796 double current_pts = mpctx->sh_video->pts;
2797 for (i = 1; i < mpctx->num_chapters; i++)
2798 if (current_pts < mpctx->chapters[i].start)
2799 break;
2800 return i - 1;
2803 // currently returns a string allocated with malloc, not talloc
2804 char *chapter_display_name(struct MPContext *mpctx, int chapter)
2806 if (!mpctx->chapters || !mpctx->sh_video)
2807 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
2808 return strdup(mpctx->chapters[chapter].name);
2811 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
2812 char **chapter_name)
2814 if (!mpctx->chapters || !mpctx->sh_video)
2815 return demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
2816 chapter_name);
2817 if (chapter >= mpctx->num_chapters)
2818 return -1;
2819 if (chapter < 0)
2820 chapter = 0;
2821 *seek_pts = mpctx->chapters[chapter].start;
2822 if (chapter_name)
2823 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
2824 return chapter;
2827 static int find_ordered_chapter_sources(struct MPContext *mpctx,
2828 struct content_source *sources,
2829 int num_sources,
2830 unsigned char uid_map[][16])
2832 int num_filenames = 0;
2833 char **filenames = NULL;
2834 if (num_sources > 1) {
2835 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
2836 "other sources.\n");
2837 if (mpctx->stream->type != STREAMTYPE_FILE) {
2838 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
2839 "normal disk file. Will not search for related files.\n");
2840 } else {
2841 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
2842 "same directory to find referenced sources.\n");
2843 filenames = find_files(mpctx->demuxer->filename, ".mkv",
2844 &num_filenames);
2848 int num_left = num_sources - 1;
2849 for (int i = 0; i < num_filenames && num_left > 0; i++) {
2850 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
2851 filename_recode(filenames[i]));
2852 int format;
2853 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
2854 if (!s)
2855 continue;
2856 struct demuxer *d = demux_open(&mpctx->opts, s, DEMUXER_TYPE_MATROSKA,
2857 mpctx->opts.audio_id,
2858 mpctx->opts.video_id,
2859 mpctx->opts.sub_id, filenames[i]);
2860 if (!d) {
2861 free_stream(s);
2862 continue;
2864 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
2865 for (int i = 1; i < num_sources; i++) {
2866 if (sources[i].demuxer)
2867 continue;
2868 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
2869 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
2870 i, filename_recode(d->filename));
2871 sources[i].stream = s;
2872 sources[i].demuxer = d;
2873 num_left--;
2874 goto match;
2878 free_demuxer(d);
2879 free_stream(s);
2880 continue;
2881 match:
2884 talloc_free(filenames);
2885 if (num_left) {
2886 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
2887 "There will be parts MISSING from the video!\n");
2888 for (int i = 1, j = 1; i < num_sources; i++)
2889 if (sources[i].demuxer) {
2890 sources[j] = sources[i];
2891 memcpy(uid_map[j], uid_map[i], 16);
2892 j++;
2895 return num_sources - num_left;
2898 static void build_ordered_chapter_timeline(struct MPContext *mpctx)
2900 if (!mpctx->opts.ordered_chapters) {
2901 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
2902 "you have disabled support for them. Ignoring.\n");
2903 return;
2906 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
2907 "edit timeline.\n");
2909 struct demuxer *demuxer = mpctx->demuxer;
2910 struct matroska_data *m = &demuxer->matroska_data;
2912 // +1 because sources/uid_map[0] is original file even if all chapters
2913 // actually use other sources and need separate entries
2914 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
2915 m->num_ordered_chapters+1);
2916 sources[0].stream = mpctx->stream;
2917 sources[0].demuxer = mpctx->demuxer;
2918 unsigned char uid_map[m->num_ordered_chapters+1][16];
2919 int num_sources = 1;
2920 memcpy(uid_map[0], m->segment_uid, 16);
2922 for (int i = 0; i < m->num_ordered_chapters; i++) {
2923 struct matroska_chapter *c = m->ordered_chapters + i;
2924 if (!c->has_segment_uid)
2925 memcpy(c->segment_uid, m->segment_uid, 16);
2927 for (int j = 0; j < num_sources; j++)
2928 if (!memcmp(c->segment_uid, uid_map[j], 16))
2929 goto found1;
2930 memcpy(uid_map[num_sources], c->segment_uid, 16);
2931 sources[num_sources] = (struct content_source){};
2932 num_sources++;
2933 found1:
2937 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
2938 uid_map);
2941 // +1 for terminating chapter with start time marking end of last real one
2942 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
2943 m->num_ordered_chapters + 1);
2944 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
2945 m->num_ordered_chapters);
2946 uint64_t starttime = 0;
2947 uint64_t missing_time = 0;
2948 int part_count = 0;
2949 int num_chapters = 0;
2950 uint64_t prev_part_offset;
2951 for (int i = 0; i < m->num_ordered_chapters; i++) {
2952 struct matroska_chapter *c = m->ordered_chapters + i;
2954 int j;
2955 for (j = 0; j < num_sources; j++) {
2956 if (!memcmp(c->segment_uid, uid_map[j], 16))
2957 goto found2;
2959 missing_time += c->end - c->start;
2960 continue;
2961 found2:;
2962 chapters[num_chapters].start = starttime / 1000.;
2963 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
2964 /* Only add a separate part if the time or file actually changes.
2965 * Matroska files have chapter divisions that are redundant from
2966 * timeline point of view because the same chapter structure is used
2967 * both to specify the timeline and for normal chapter information.
2968 * Removing a missing inserted external chapter can also cause this. */
2969 if (part_count == 0 || c->start != starttime + prev_part_offset
2970 || sources + j != timeline[part_count - 1].source) {
2971 timeline[part_count].source = sources + j;
2972 timeline[part_count].start = chapters[num_chapters].start;
2973 timeline[part_count].source_start = c->start / 1000.;
2974 prev_part_offset = c->start - starttime;
2975 part_count++;
2977 starttime += c->end - c->start;
2978 num_chapters++;
2980 timeline[part_count].start = starttime / 1000.;
2982 if (!part_count) {
2983 // None of the parts come from the file itself???
2984 talloc_free(sources);
2985 talloc_free(timeline);
2986 talloc_free(chapters);
2987 return;
2990 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
2991 "sources. Total length %.3f seconds.\n", part_count, num_sources,
2992 timeline[part_count].start);
2993 if (missing_time)
2994 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
2995 "from the timeline!\n", missing_time / 1000.);
2996 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
2997 for (int i = 0; i < num_sources; i++)
2998 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
2999 filename_recode(sources[i].demuxer->filename));
3000 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
3001 "source_start, source):\n");
3002 for (int i = 0; i < part_count; i++) {
3003 struct timeline_part *p = timeline + i;
3004 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
3005 p->source_start, p->source - sources);
3007 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start);
3008 mpctx->sources = sources;
3009 mpctx->num_sources = num_sources;
3010 mpctx->timeline = timeline;
3011 mpctx->num_timeline_parts = part_count;
3012 mpctx->num_chapters = num_chapters;
3013 mpctx->chapters = chapters;
3015 mpctx->timeline_part = 0;
3016 mpctx->demuxer = timeline[0].source->demuxer;
3020 static int read_keys(void *ctx, int fd)
3022 getch2(ctx);
3023 return mplayer_get_key(ctx, 0);
3027 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
3028 * file for some tools to link against. */
3029 #ifndef DISABLE_MAIN
3030 int main(int argc,char* argv[]){
3033 char * mem_ptr;
3035 // movie info:
3037 /* Flag indicating whether MPlayer should exit without playing anything. */
3038 int opt_exit = 0;
3040 //float a_frame=0; // Audio
3042 int i;
3044 struct MPContext *mpctx = &(struct MPContext){
3045 .osd_function = OSD_PLAY,
3046 .begin_skip = MP_NOPTS_VALUE,
3047 .play_tree_step = 1,
3048 .global_sub_pos = -1,
3049 .set_of_sub_pos = -1,
3050 .file_format = DEMUXER_TYPE_UNKNOWN,
3051 .last_dvb_step = 1,
3054 InitTimer();
3055 srand(GetTimerMS());
3057 mp_msg_init();
3058 set_av_log_callback();
3060 #ifdef CONFIG_X11
3061 mpctx->x11_state = vo_x11_init_state();
3062 #endif
3063 struct MPOpts *opts = &mpctx->opts;
3064 set_default_mplayer_options(opts);
3065 // Create the config context and register the options
3066 mpctx->mconfig = m_config_new(opts, cfg_include);
3067 m_config_register_options(mpctx->mconfig,mplayer_opts);
3068 mp_input_register_options(mpctx->mconfig);
3070 // Preparse the command line
3071 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
3073 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
3074 set_path_env();
3075 #endif
3077 #ifdef CONFIG_TV
3078 stream_tv_defaults.immediate = 1;
3079 #endif
3081 parse_cfgfiles(mpctx, mpctx->mconfig);
3083 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
3084 if(mpctx->playtree == NULL)
3085 opt_exit = 1;
3086 else {
3087 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
3088 if(mpctx->playtree) {
3089 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3090 if(mpctx->playtree_iter) {
3091 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3092 play_tree_iter_free(mpctx->playtree_iter);
3093 mpctx->playtree_iter = NULL;
3095 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3099 mpctx->key_fifo = mp_fifo_create(opts);
3101 print_version("MPlayer");
3103 #if defined(__MINGW32__) || defined(__CYGWIN__)
3105 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
3106 BOOL WINAPI (*setDEP)(DWORD) = NULL;
3107 if (kernel32)
3108 setDEP = GetProcAddress(kernel32, "SetProcessDEPPolicy");
3109 if (setDEP) setDEP(3);
3111 // stop Windows from showing all kinds of annoying error dialogs
3112 SetErrorMode(0x8003);
3113 // request 1ms timer resolution
3114 timeBeginPeriod(1);
3115 #endif
3117 #ifdef CONFIG_PRIORITY
3118 set_priority();
3119 #endif
3121 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
3122 list_video_out();
3123 opt_exit = 1;
3126 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
3127 list_audio_out();
3128 opt_exit = 1;
3131 /* Check codecs.conf. */
3132 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3133 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3134 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3135 if(!parse_codec_cfg(NULL)){
3136 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3138 mp_tmsg(MSGT_CPLAYER,MSGL_V,"Using built-in default codecs.conf.\n");
3141 free( mem_ptr ); // release the buffer created by get_path()
3144 #if 0
3145 if(video_codec_list){
3146 int i;
3147 video_codec=video_codec_list[0];
3148 for(i=0;video_codec_list[i];i++)
3149 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
3151 #endif
3152 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3153 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available audio codecs:\n");
3154 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3155 list_codecs(1);
3156 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3157 opt_exit = 1;
3159 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3160 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video codecs:\n");
3161 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3162 list_codecs(0);
3163 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3164 opt_exit = 1;
3166 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3167 vfm_help();
3168 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3169 opt_exit = 1;
3171 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3172 afm_help();
3173 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3174 opt_exit = 1;
3176 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3177 af_help();
3178 printf("\n");
3179 opt_exit = 1;
3181 #ifdef CONFIG_X11
3182 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3183 fstype_help();
3184 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3185 opt_exit = 1;
3187 #endif
3188 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3189 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3190 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3191 demuxer_help();
3192 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3193 opt_exit = 1;
3195 if(list_properties) {
3196 property_print_help();
3197 opt_exit = 1;
3200 if(opt_exit)
3201 exit_player(mpctx, EXIT_NONE);
3203 if(!mpctx->filename && !player_idle_mode){
3204 // no file/vcd/dvd -> show HELP:
3205 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s", mp_gtext(help_text));
3206 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3209 /* Display what configure line was used */
3210 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3212 // Many users forget to include command line in bugreports...
3213 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3214 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "CommandLine:");
3215 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3216 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3219 //------ load global data first ------
3221 mpctx->osd = osd_create();
3223 // check font
3224 #ifdef CONFIG_FREETYPE
3225 init_freetype();
3226 #endif
3227 #ifdef CONFIG_FONTCONFIG
3228 if(font_fontconfig <= 0)
3230 #endif
3231 #ifdef CONFIG_BITMAP_FONT
3232 if(font_name){
3233 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3234 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",
3235 filename_recode(font_name));
3236 } else {
3237 // try default:
3238 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3239 free(mem_ptr); // release the buffer created by get_path()
3240 if(!vo_font)
3241 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3243 if (sub_font_name)
3244 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3245 else
3246 mpctx->osd->sub_font = vo_font;
3247 #endif
3248 #ifdef CONFIG_FONTCONFIG
3250 #endif
3252 #ifdef CONFIG_ASS
3253 ass_library = ass_init();
3254 #endif
3256 #ifdef HAVE_RTC
3257 if(!nortc)
3259 // seteuid(0); /* Can't hurt to try to get root here */
3260 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3261 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Failed to open %s: %s (it should be readable by the user.)\n",
3262 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3263 else {
3264 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3266 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3267 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp, strerror(errno));
3268 mp_tmsg(MSGT_CPLAYER, MSGL_HINT, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp);
3269 close (rtc_fd);
3270 rtc_fd = -1;
3271 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3272 /* variable only by the root */
3273 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno));
3274 close (rtc_fd);
3275 rtc_fd = -1;
3276 } else
3277 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Using Linux hardware RTC timing (%ldHz).\n", irqp);
3280 if(rtc_fd<0)
3281 #endif /* HAVE_RTC */
3282 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3283 softsleep?"software":timer_name);
3285 #ifdef HAVE_TERMCAP
3286 load_termcap(NULL); // load key-codes
3287 #endif
3289 // ========== Init keyboard FIFO (connection to libvo) ============
3291 // Init input system
3292 current_module = "init_input";
3293 mpctx->input = mp_input_init(&opts->input);
3294 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3295 if(slave_mode)
3296 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
3297 else if(!noconsolecontrols)
3298 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
3299 // Set the libstream interrupt callback
3300 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
3302 #ifdef CONFIG_MENU
3303 if(use_menu) {
3304 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3305 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3306 else {
3307 menu_cfg = get_path("menu.conf");
3308 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3309 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3310 else {
3311 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
3312 MPLAYER_CONFDIR "/menu.conf"))
3313 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", MPLAYER_CONFDIR"/menu.conf");
3314 else {
3315 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Menu init failed.\n");
3316 use_menu = 0;
3321 #endif
3323 current_module = NULL;
3325 /// Catch signals
3326 #ifndef __MINGW32__
3327 signal(SIGCHLD,child_sighandler);
3328 #endif
3330 #ifdef CONFIG_CRASH_DEBUG
3331 prog_path = argv[0];
3332 #endif
3333 //========= Catch terminate signals: ================
3334 // terminate requests:
3335 signal(SIGTERM,exit_sighandler); // kill
3336 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3338 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3340 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3341 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3342 #ifdef CONFIG_SIGHANDLER
3343 // fatal errors:
3344 signal(SIGBUS,exit_sighandler); // bus error
3345 signal(SIGSEGV,exit_sighandler); // segfault
3346 signal(SIGILL,exit_sighandler); // illegal instruction
3347 signal(SIGFPE,exit_sighandler); // floating point exc.
3348 signal(SIGABRT,exit_sighandler); // abort()
3349 #ifdef CONFIG_CRASH_DEBUG
3350 if (crash_debug)
3351 signal(SIGTRAP,exit_sighandler);
3352 #endif
3353 #endif
3355 // ******************* Now, let's see the per-file stuff ********************
3357 play_next_file:
3359 // init global sub numbers
3360 mpctx->global_sub_size = 0;
3361 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3363 if (mpctx->filename) {
3364 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
3365 load_per_extension_config (mpctx->mconfig, mpctx->filename);
3366 load_per_file_config (mpctx->mconfig, mpctx->filename);
3369 if (opts->video_driver_list)
3370 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
3371 if (opts->audio_driver_list)
3372 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
3374 // We must enable getch2 here to be able to interrupt network connection
3375 // or cache filling
3376 if(!noconsolecontrols && !slave_mode){
3377 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
3378 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"WARNING: getch2_init called twice!\n");
3379 else
3380 getch2_enable(); // prepare stdin for hotkeys...
3381 mpctx->initialized_flags|=INITIALIZED_GETCH2;
3382 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3385 // =================== GUI idle loop (STOP state) ===========================
3386 while (player_idle_mode && !mpctx->filename) {
3387 play_tree_t * entry = NULL;
3388 mp_cmd_t * cmd;
3389 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3390 if (mpctx->video_out)
3391 vo_check_events(mpctx->video_out);
3392 usec_sleep(20000);
3394 switch (cmd->id) {
3395 case MP_CMD_LOADFILE:
3396 // prepare a tree entry with the new filename
3397 entry = play_tree_new();
3398 play_tree_add_file(entry, cmd->args[0].v.s);
3399 // The entry is added to the main playtree after the switch().
3400 break;
3401 case MP_CMD_LOADLIST:
3402 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3403 break;
3404 case MP_CMD_QUIT:
3405 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3406 break;
3407 case MP_CMD_GET_PROPERTY:
3408 case MP_CMD_SET_PROPERTY:
3409 case MP_CMD_STEP_PROPERTY:
3410 run_command(mpctx, cmd);
3411 break;
3414 mp_cmd_free(cmd);
3416 if (entry) { // user entered a command that gave a valid entry
3417 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3418 play_tree_free_list(mpctx->playtree->child, 1);
3419 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3421 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3423 play_tree_set_child(mpctx->playtree, entry);
3425 /* Make iterator start at the top the of tree. */
3426 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3427 if (!mpctx->playtree_iter) continue;
3429 // find the first real item in the tree
3430 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3431 // no items!
3432 play_tree_iter_free(mpctx->playtree_iter);
3433 mpctx->playtree_iter = NULL;
3434 continue; // wait for next command
3436 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3439 //---------------------------------------------------------------------------
3441 if (mpctx->filename) {
3442 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nPlaying %s.\n",
3443 filename_recode(mpctx->filename));
3444 if(use_filename_title && vo_wintitle == NULL)
3445 vo_wintitle = strdup(mp_basename2(mpctx->filename));
3448 if (edl_filename) {
3449 if (edl_records) free_edl(edl_records);
3450 next_edl_record = edl_records = edl_parse_file();
3452 if (edl_output_filename) {
3453 if (edl_fd) fclose(edl_fd);
3454 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3456 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Can't open EDL file [%s] for writing.\n",
3457 filename_recode(edl_output_filename));
3461 //==================== Open VOB-Sub ============================
3463 current_module="vobsub";
3464 if (vobsub_name){
3465 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3466 if(vo_vobsub==NULL)
3467 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
3468 filename_recode(vobsub_name));
3469 } else if (sub_auto && mpctx->filename){
3470 /* try to autodetect vobsub from movie filename ::atmos */
3471 char *buf = strdup(mpctx->filename), *psub;
3472 char *pdot = strrchr(buf, '.');
3473 char *pslash = strrchr(buf, '/');
3474 #if defined(__MINGW32__) || defined(__CYGWIN__)
3475 if (!pslash) pslash = strrchr(buf, '\\');
3476 #endif
3477 if (pdot && (!pslash || pdot > pslash))
3478 *pdot = '\0';
3479 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3480 /* try from ~/.mplayer/sub */
3481 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3482 char *bname;
3483 int l;
3484 bname = strrchr(buf,'/');
3485 #if defined(__MINGW32__) || defined(__CYGWIN__)
3486 if(!bname) bname = strrchr(buf,'\\');
3487 #endif
3488 if(bname) bname++;
3489 else bname = buf;
3490 l = strlen(psub) + strlen(bname) + 1;
3491 psub = realloc(psub,l);
3492 strcat(psub,bname);
3493 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3494 free(psub);
3496 free(buf);
3498 if(vo_vobsub){
3499 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3500 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3501 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3503 // setup global sub numbering
3504 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3505 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3508 //============ Open & Sync STREAM --- fork cache2 ====================
3510 mpctx->stream=NULL;
3511 mpctx->demuxer=NULL;
3512 if (mpctx->d_audio) {
3513 //free_demuxer_stream(mpctx->d_audio);
3514 mpctx->d_audio=NULL;
3516 if (mpctx->d_video) {
3517 //free_demuxer_stream(d_video);
3518 mpctx->d_video=NULL;
3520 mpctx->sh_audio=NULL;
3521 mpctx->sh_video=NULL;
3523 current_module="open_stream";
3524 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3525 if(!mpctx->stream) { // error...
3526 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3527 goto goto_next_file;
3529 mpctx->initialized_flags|=INITIALIZED_STREAM;
3531 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3532 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3533 "playlist support will not be used automatically.\n"
3534 "MPlayer's playlist code is unsafe and should only be used with "
3535 "trusted sources.\nPlayback will probably fail.\n\n");
3536 #if 0
3537 play_tree_t* entry;
3538 // Handle playlist
3539 current_module="handle_playlist";
3540 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3541 filename_recode(mpctx->filename));
3542 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3543 mpctx->eof=playtree_add_playlist(mpctx, entry);
3544 goto goto_next_file;
3545 #endif
3547 mpctx->stream->start_pos+=seek_to_byte;
3549 if(stream_dump_type==5){
3550 unsigned char buf[4096];
3551 int len;
3552 FILE *f;
3553 current_module="dumpstream";
3554 stream_reset(mpctx->stream);
3555 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3556 f=fopen(stream_dump_name,"wb");
3557 if(!f){
3558 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3559 exit_player(mpctx, EXIT_ERROR);
3561 if (dvd_chapter > 1) {
3562 int chapter = dvd_chapter - 1;
3563 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3565 while(!mpctx->stream->eof && !async_quit_request){
3566 len=stream_read(mpctx->stream,buf,4096);
3567 if(len>0) {
3568 if(fwrite(buf,len,1,f) != 1) {
3569 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3570 exit_player(mpctx, EXIT_ERROR);
3573 if(dvd_last_chapter > 0) {
3574 int chapter = -1;
3575 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3576 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3577 break;
3580 if(fclose(f)) {
3581 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3582 exit_player(mpctx, EXIT_ERROR);
3584 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3585 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3588 #ifdef CONFIG_DVDREAD
3589 if(mpctx->stream->type==STREAMTYPE_DVD){
3590 current_module="dvd lang->id";
3591 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3592 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3593 // setup global sub numbering
3594 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3595 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3596 current_module=NULL;
3598 #endif
3600 #ifdef CONFIG_DVDNAV
3601 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3602 current_module="dvdnav lang->id";
3603 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3604 dvdsub_lang_id = -3;
3605 if(dvdsub_lang && opts->sub_id==-1)
3606 dvdsub_lang_id = opts->sub_id = mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3607 // setup global sub numbering
3608 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3609 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3610 current_module=NULL;
3612 #endif
3614 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3615 goto_enable_cache:
3616 if(stream_cache_size>0){
3617 int res;
3618 current_module="enable_cache";
3619 res = stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3620 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3621 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0));
3622 if(res == 0)
3623 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3626 //============ Open DEMUXERS --- DETECT file type =======================
3627 current_module="demux_open";
3629 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3631 // HACK to get MOV Reference Files working
3633 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3635 unsigned char* playlist_entry;
3636 play_tree_t *list = NULL, *entry = NULL;
3638 current_module="handle_demux_playlist";
3639 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3641 char *temp, *bname;
3643 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3644 filename_recode(playlist_entry));
3646 bname=mp_basename(playlist_entry);
3647 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3648 continue;
3650 if (!strcmp(playlist_entry, mpctx->filename)) // ignoring self-reference
3651 continue;
3653 entry = play_tree_new();
3655 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3657 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3658 if (temp)
3660 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3661 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3662 strcat(temp, playlist_entry);
3663 if (!strcmp(temp, mpctx->filename)) {
3664 free(temp);
3665 continue;
3667 play_tree_add_file(entry,temp);
3668 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3669 free(temp);
3672 else
3673 play_tree_add_file(entry,playlist_entry);
3675 if(!list)
3676 list = entry;
3677 else
3678 play_tree_append_entry(list,entry);
3680 free_demuxer(mpctx->demuxer);
3681 mpctx->demuxer = NULL;
3683 if (list)
3685 entry = play_tree_new();
3686 play_tree_set_child(entry,list);
3687 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3688 goto goto_next_file;
3692 if(!mpctx->demuxer)
3693 goto goto_next_file;
3695 if (mpctx->demuxer->matroska_data.ordered_chapters)
3696 build_ordered_chapter_timeline(mpctx);
3698 if (!mpctx->sources) {
3699 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
3700 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
3701 .demuxer = mpctx->demuxer};
3702 mpctx->num_sources = 1;
3705 if(dvd_chapter>1) {
3706 double pts;
3707 if (seek_chapter(mpctx, dvd_chapter-1, &pts, NULL) >= 0 && pts > -1.0)
3708 seek(mpctx, pts, SEEK_ABSOLUTE);
3711 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3713 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3714 int i;
3715 int maxid = -1;
3716 // setup global sub numbering
3717 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3718 for (i = 0; i < MAX_S_STREAMS; i++)
3719 if (mpctx->demuxer->s_streams[i])
3720 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3721 mpctx->global_sub_size += maxid + 1;
3723 // Make opts->sub_id always selectable if set.
3724 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3725 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3727 #ifdef CONFIG_ASS
3728 if (opts->ass_enabled && ass_library) {
3729 for (int j = 0; j < mpctx->num_sources; j++) {
3730 struct demuxer *d = mpctx->sources[j].demuxer;
3731 for (int i = 0; i < d->num_attachments; i++) {
3732 struct demux_attachment *att = d->attachments + i;
3733 if (use_embedded_fonts
3734 && att->name && att->type && att->data && att->data_size
3735 && (strcmp(att->type, "application/x-truetype-font") == 0
3736 || strcmp(att->type, "application/x-font") == 0))
3737 ass_add_font(ass_library, att->name, att->data, att->data_size);
3741 #endif
3743 current_module="demux_open2";
3745 //file_format=demuxer->file_format;
3747 mpctx->d_audio=mpctx->demuxer->audio;
3748 mpctx->d_video=mpctx->demuxer->video;
3749 mpctx->d_sub=mpctx->demuxer->sub;
3751 if (ts_prog) {
3752 int tmp = ts_prog;
3753 mp_property_do("switch_program", M_PROPERTY_SET, &tmp, mpctx);
3755 // select audio stream
3756 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3758 // DUMP STREAMS:
3759 if((stream_dump_type)&&(stream_dump_type<4)){
3760 FILE *f;
3761 demux_stream_t *ds=NULL;
3762 current_module="dump";
3763 // select stream to dump
3764 switch(stream_dump_type){
3765 case 1: ds=mpctx->d_audio;break;
3766 case 2: ds=mpctx->d_video;break;
3767 case 3: ds=mpctx->d_sub;break;
3769 if(!ds){
3770 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"dump: FATAL: Selected stream missing!\n");
3771 exit_player(mpctx, EXIT_ERROR);
3773 // disable other streams:
3774 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3775 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3776 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3777 // let's dump it!
3778 f=fopen(stream_dump_name,"wb");
3779 if(!f){
3780 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3781 exit_player(mpctx, EXIT_ERROR);
3783 while(!ds->eof){
3784 unsigned char* start;
3785 int in_size=ds_get_packet(ds,&start);
3786 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3787 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3788 if(in_size>0) fwrite(start,in_size,1,f);
3789 if(dvd_last_chapter>0) {
3790 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3791 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3792 break;
3795 fclose(f);
3796 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3797 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3800 mpctx->sh_audio=mpctx->d_audio->sh;
3801 mpctx->sh_video=mpctx->d_video->sh;
3803 if(mpctx->sh_video){
3805 current_module="video_read_properties";
3806 if(!video_read_properties(mpctx->sh_video)) {
3807 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Video: Cannot read properties.\n");
3808 mpctx->sh_video=mpctx->d_video->sh=NULL;
3809 } else {
3810 mp_tmsg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
3811 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3812 mpctx->sh_video->fps,mpctx->sh_video->frametime
3815 /* need to set fps here for output encoders to pick it up in their init */
3816 if(force_fps){
3817 mpctx->sh_video->fps=force_fps;
3818 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3820 vo_fps = mpctx->sh_video->fps;
3822 if(!mpctx->sh_video->fps && !force_fps){
3823 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"FPS not specified in the header or invalid, use the -fps option.\n");
3824 mpctx->sh_video=mpctx->d_video->sh=NULL;
3830 if(!mpctx->sh_video && !mpctx->sh_audio){
3831 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "No stream found.\n");
3832 #ifdef CONFIG_DVBIN
3833 if(mpctx->stream->type == STREAMTYPE_DVB)
3835 int dir;
3836 int v = mpctx->last_dvb_step;
3837 if(v > 0)
3838 dir = DVB_CHANNEL_HIGHER;
3839 else
3840 dir = DVB_CHANNEL_LOWER;
3842 if(dvb_step_channel(mpctx->stream, dir)) {
3843 mpctx->stop_play = PT_NEXT_ENTRY;
3844 mpctx->dvbin_reopen = 1;
3847 #endif
3848 goto goto_next_file; // exit_player(_("Fatal error"));
3851 /* display clip info */
3852 demux_info_print(mpctx->demuxer);
3854 //================== Read SUBTITLES (DVD & TEXT) ==========================
3855 if(vo_spudec==NULL &&
3856 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3857 init_vo_spudec(mpctx);
3860 // after reading video params we should load subtitles because
3861 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3862 // check .sub
3863 current_module="read_subtitles_file";
3864 double sub_fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
3865 if(sub_name){
3866 for (i = 0; sub_name[i] != NULL; ++i)
3867 add_subtitles(mpctx, sub_name[i], sub_fps, 0);
3869 if(sub_auto) { // auto load sub file ...
3870 char *psub = get_path( "sub/" );
3871 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3872 int i = 0;
3873 free(psub); // release the buffer created by get_path() above
3874 while (tmp[i]) {
3875 add_subtitles(mpctx, tmp[i], sub_fps, 1);
3876 free(tmp[i++]);
3878 free(tmp);
3880 if (mpctx->set_of_sub_size > 0) {
3881 // setup global sub numbering
3882 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3883 mpctx->global_sub_size += mpctx->set_of_sub_size;
3887 if (mpctx->global_sub_size) {
3888 select_subtitle(mpctx);
3889 if(subdata)
3890 switch (stream_dump_type) {
3891 case 3: list_sub_file(subdata); break;
3892 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3893 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3894 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3895 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3896 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3900 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3901 filename_recode(mpctx->filename));
3902 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3903 if (mpctx->sh_video) {
3904 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3905 if (mpctx->sh_video->format >= 0x20202020)
3906 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3907 else
3908 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3909 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3910 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3911 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3912 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3913 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3915 if (mpctx->sh_audio) {
3916 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3917 if (mpctx->sh_audio->format >= 0x20202020)
3918 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3919 else
3920 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3921 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3922 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3923 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3925 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2f\n", mpctx->timeline ?
3926 mpctx->timeline[mpctx->num_timeline_parts].start :
3927 demuxer_get_time_length(mpctx->demuxer));
3928 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n",
3929 mpctx->stream->seek && (!mpctx->demuxer || mpctx->demuxer->seekable));
3930 if (mpctx->demuxer) {
3931 if (mpctx->demuxer->num_chapters == 0)
3932 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3933 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3936 if(!mpctx->sh_video) goto main; // audio-only
3938 if(!reinit_video_chain(mpctx)) {
3939 if(!mpctx->sh_video){
3940 if(!mpctx->sh_audio) goto goto_next_file;
3941 goto main; // exit_player(_("Fatal error"));
3945 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3946 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3948 #ifdef CONFIG_FREETYPE
3949 force_load_font = 1;
3950 #endif
3952 //================== MAIN: ==========================
3953 main:
3954 current_module="main";
3956 if(playing_msg) {
3957 char* msg = property_expand_string(mpctx, playing_msg);
3958 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3959 free(msg);
3963 // Disable the term OSD in verbose mode
3964 if(verbose) term_osd = 0;
3967 //int frame_corr_num=0; //
3968 //float v_frame=0; // Video
3969 //float num_frames=0; // number of frames played
3971 int frame_time_remaining=0; // flag
3972 int blit_frame=0;
3974 // Make sure old OSD does not stay around,
3975 // e.g. with -fixed-vo and same-resolution files
3976 clear_osd_msgs();
3977 update_osd_msg(mpctx);
3979 //================ SETUP AUDIO ==========================
3981 if(mpctx->sh_audio){
3982 reinit_audio_chain(mpctx);
3983 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3984 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3987 current_module="av_init";
3989 if(mpctx->sh_video){
3990 mpctx->sh_video->timer=0;
3991 if (! ignore_start)
3992 audio_delay += mpctx->sh_video->stream_delay;
3994 if(mpctx->sh_audio){
3995 if (start_volume >= 0)
3996 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3997 if (! ignore_start)
3998 audio_delay -= mpctx->sh_audio->stream_delay;
3999 mpctx->delay=-audio_delay;
4002 if(!mpctx->sh_audio){
4003 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Audio: no sound\n");
4004 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
4005 ds_free_packs(mpctx->d_audio); // free buffered chunks
4006 //mpctx->d_audio->id=-2; // do not read audio chunks
4007 //uninit_player(mpctx, INITIALIZED_AO); // close device
4009 if(!mpctx->sh_video){
4010 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Video: no video\n");
4011 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
4012 ds_free_packs(mpctx->d_video);
4013 mpctx->d_video->id=-2;
4014 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
4017 if (!mpctx->sh_video && !mpctx->sh_audio)
4018 goto goto_next_file;
4020 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
4021 if(force_fps && mpctx->sh_video){
4022 vo_fps = mpctx->sh_video->fps=force_fps;
4023 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
4024 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx->sh_video->fps,mpctx->sh_video->frametime);
4027 mp_input_set_section(mpctx->input, NULL);
4028 //TODO: add desired (stream-based) sections here
4029 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
4030 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
4032 //==================== START PLAYING =======================
4034 if(opts->loop_times>1) opts->loop_times--; else
4035 if(opts->loop_times==1) opts->loop_times = -1;
4037 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Starting playback...\n");
4039 total_time_usage_start=GetTimer();
4040 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
4041 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
4042 play_n_frames=play_n_frames_mf;
4044 if(play_n_frames==0){
4045 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
4048 // If there's a timeline force an absolute seek to initialize state
4049 if (seek_to_sec || mpctx->timeline) {
4050 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
4051 end_at.pos += seek_to_sec;
4054 if (end_at.type == END_AT_SIZE) {
4055 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Option -endpos in MPlayer does not yet support size units.\n");
4056 end_at.type = END_AT_NONE;
4059 #ifdef CONFIG_DVDNAV
4060 mp_dvdnav_context_free(mpctx);
4061 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4062 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
4063 mp_dvdnav_cell_has_changed(mpctx->stream,1);
4065 #endif
4067 get_relative_time(mpctx); // reset current delta
4068 mpctx->time_frame = 0;
4069 mpctx->drop_message_shown = 0;
4070 mpctx->update_video_immediately = true;
4071 mpctx->total_avsync_change = 0;
4072 // Make sure VO knows current pause state
4073 if (mpctx->sh_video)
4074 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
4075 NULL);
4077 while(!mpctx->stop_play){
4078 float aq_sleep_time=0;
4080 if(dvd_last_chapter>0) {
4081 int cur_chapter = get_current_chapter(mpctx);
4082 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
4083 goto goto_next_file;
4086 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
4087 mpctx->sh_audio = mpctx->d_audio->sh;
4088 mpctx->sh_audio->ds = mpctx->d_audio;
4089 reinit_audio_chain(mpctx);
4092 /*========================== PLAY AUDIO ============================*/
4094 if (mpctx->sh_audio && !mpctx->paused)
4095 if (!fill_audio_out_buffers(mpctx))
4096 // at eof, all audio at least written to ao
4097 if (!mpctx->sh_video)
4098 mpctx->stop_play = AT_END_OF_FILE;
4101 if(!mpctx->sh_video) {
4102 // handle audio-only case:
4103 double a_pos=0;
4104 // sh_audio can be NULL due to video stream switching
4105 // TODO: handle this better
4106 if (mpctx->sh_audio)
4107 a_pos = playing_audio_pts(mpctx);
4109 print_status(mpctx, a_pos, false);
4111 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
4112 mpctx->stop_play = PT_NEXT_ENTRY;
4113 update_subtitles(mpctx, &mpctx->opts, NULL, a_pos, mpctx->video_offset,
4114 mpctx->d_sub, 0);
4115 update_osd_msg(mpctx);
4117 } else {
4119 /*========================== PLAY VIDEO ============================*/
4121 vo_pts=mpctx->sh_video->timer*90000.0;
4122 vo_fps=mpctx->sh_video->fps;
4124 blit_frame = mpctx->video_out->frame_loaded;
4125 if (!blit_frame) {
4126 double frame_time = update_video(mpctx);
4127 blit_frame = mpctx->video_out->frame_loaded;
4128 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
4129 if (mpctx->sh_video->vf_initialized < 0) {
4130 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "\nFATAL: Could not initialize video filters (-vf) or video output (-vo).\n");
4131 mpctx->stop_play = PT_NEXT_ENTRY;
4132 goto goto_next_file;
4134 if (blit_frame) {
4135 struct sh_video *sh_video = mpctx->sh_video;
4136 update_subtitles(mpctx, &mpctx->opts, sh_video, sh_video->pts,
4137 mpctx->video_offset, mpctx->d_sub, 0);
4138 update_teletext(sh_video, mpctx->demuxer, 0);
4139 update_osd_msg(mpctx);
4140 struct vf_instance *vf = mpctx->sh_video->vfilter;
4141 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
4142 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
4143 vo_osd_changed(0);
4145 if (frame_time < 0)
4146 mpctx->stop_play = AT_END_OF_FILE;
4147 else {
4148 if (mpctx->update_video_immediately) {
4149 // Show this frame immediately, rest normally
4150 mpctx->update_video_immediately = false;
4151 } else {
4152 mpctx->time_frame += frame_time / opts->playback_speed;
4153 adjust_sync(mpctx, frame_time);
4157 if (mpctx->timeline) {
4158 struct timeline_part *next = mpctx->timeline + mpctx->timeline_part + 1;
4159 if (mpctx->sh_video->pts >= next->start
4160 || mpctx->stop_play == AT_END_OF_FILE
4161 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
4162 seek(mpctx, next->start, SEEK_ABSOLUTE);
4163 continue;
4167 // ==========================================================================
4169 // current_module="draw_osd";
4170 // if(vo_config_count) mpctx->video_out->draw_osd();
4172 current_module="vo_check_events";
4173 vo_check_events(mpctx->video_out);
4175 #ifdef CONFIG_X11
4176 if (stop_xscreensaver) {
4177 current_module = "stop_xscreensaver";
4178 xscreensaver_heartbeat(mpctx->x11_state);
4180 #endif
4181 if (heartbeat_cmd) {
4182 static unsigned last_heartbeat;
4183 unsigned now = GetTimerMS();
4184 if (now - last_heartbeat > 30000) {
4185 last_heartbeat = now;
4186 system(heartbeat_cmd);
4190 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
4192 //====================== FLIP PAGE (VIDEO BLT): =========================
4194 current_module="flip_page";
4195 if (!frame_time_remaining && blit_frame) {
4196 unsigned int t2=GetTimer();
4197 unsigned int pts_us = mpctx->last_time + mpctx->time_frame * 1e6;
4198 int duration = -1;
4199 double pts2 = mpctx->video_out->next_pts2;
4200 if (pts2 != MP_NOPTS_VALUE && opts->correct_pts) {
4201 // expected A/V sync correction is ignored
4202 double diff = (pts2 - mpctx->sh_video->pts);
4203 diff /= opts->playback_speed;
4204 if (mpctx->time_frame < 0)
4205 diff += mpctx->time_frame;
4206 if (diff < 0)
4207 diff = 0;
4208 if (diff > 10)
4209 diff = 10;
4210 duration = diff * 1e6;
4212 vo_flip_page(mpctx->video_out, pts_us|1, duration);
4214 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
4215 vout_time_usage += mpctx->last_vo_flip_duration;
4216 if (mpctx->video_out->driver->flip_page_timed) {
4217 // No need to adjust sync based on flip speed
4218 mpctx->last_vo_flip_duration = 0;
4219 // For print_status - VO call finishing early is OK for sync
4220 mpctx->time_frame -= get_relative_time(mpctx);
4222 print_status(mpctx, MP_NOPTS_VALUE, true);
4224 else
4225 print_status(mpctx, MP_NOPTS_VALUE, false);
4227 //============================ Auto QUALITY ============================
4229 /*Output quality adjustments:*/
4230 if(auto_quality>0){
4231 current_module="autoq";
4232 // float total=0.000001f * (GetTimer()-aq_total_time);
4233 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4234 if(output_quality<auto_quality && aq_sleep_time>0)
4235 ++output_quality;
4236 else
4237 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4238 if(output_quality>1 && aq_sleep_time<0)
4239 --output_quality;
4240 else
4241 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4242 output_quality=0;
4243 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4244 set_video_quality(mpctx->sh_video,output_quality);
4247 if (!frame_time_remaining && blit_frame) {
4248 if (play_n_frames >= 0) {
4249 --play_n_frames;
4250 if (play_n_frames <= 0)
4251 mpctx->stop_play = PT_NEXT_ENTRY;
4253 if (mpctx->step_frames > 0) {
4254 mpctx->step_frames--;
4255 if (mpctx->step_frames == 0)
4256 pause_player(mpctx);
4261 // FIXME: add size based support for -endpos
4262 if (end_at.type == END_AT_TIME &&
4263 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
4264 mpctx->stop_play = PT_NEXT_ENTRY;
4266 } // end if(mpctx->sh_video)
4268 #ifdef CONFIG_DVDNAV
4269 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4270 nav_highlight_t hl;
4271 mp_dvdnav_get_highlight (mpctx->stream, &hl);
4272 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4273 vo_osd_changed (OSDTYPE_DVDNAV);
4275 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
4276 double ar = -1.0;
4277 if (stream_control (mpctx->demuxer->stream,
4278 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
4279 != STREAM_UNSUPPORTED)
4280 mpctx->sh_video->stream_aspect = ar;
4283 #endif
4285 //================= Keyboard events, SEEKing ====================
4287 current_module="key_events";
4290 while (1) {
4291 mp_cmd_t* cmd;
4292 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
4293 run_command(mpctx, cmd);
4294 mp_cmd_free(cmd);
4295 if (mpctx->stop_play)
4296 break;
4298 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
4299 || mpctx->abs_seek_pos)
4300 break;
4301 if (mpctx->sh_video) {
4302 update_osd_msg(mpctx);
4303 int hack = vo_osd_changed(0);
4304 vo_osd_changed(hack);
4305 if (hack)
4306 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
4307 add_step_frame(mpctx);
4308 break;
4310 else
4311 vo_osd_changed(0);
4313 pause_loop(mpctx);
4317 // handle -sstep
4318 if (step_sec > 0 && !mpctx->paused) {
4319 mpctx->osd_function=OSD_FFW;
4320 mpctx->rel_seek_secs+=step_sec;
4323 edl_update(mpctx);
4325 /* Looping. */
4326 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
4327 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
4329 if(opts->loop_times>1) opts->loop_times--; else
4330 if(opts->loop_times==1) opts->loop_times=-1;
4331 play_n_frames=play_n_frames_mf;
4332 mpctx->stop_play=0;
4333 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
4336 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
4337 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
4339 mpctx->rel_seek_secs=0;
4340 mpctx->abs_seek_pos=0;
4343 } // while(!mpctx->stop_play)
4345 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4347 #ifdef CONFIG_DVBIN
4348 if(mpctx->dvbin_reopen)
4350 mpctx->stop_play = 0;
4351 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4352 cache_uninit(mpctx->stream);
4353 mpctx->dvbin_reopen = 0;
4354 goto goto_enable_cache;
4356 #endif
4359 goto_next_file: // don't jump here after ao/vo/getch initialization!
4361 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4363 if(benchmark){
4364 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4365 double total_time_usage;
4366 total_time_usage_start=GetTimer()-total_time_usage_start;
4367 total_time_usage = (float)total_time_usage_start*0.000001;
4368 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4369 video_time_usage,vout_time_usage,audio_time_usage,
4370 total_time_usage-tot,total_time_usage);
4371 if(total_time_usage>0.0)
4372 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4373 100.0*video_time_usage/total_time_usage,
4374 100.0*vout_time_usage/total_time_usage,
4375 100.0*audio_time_usage/total_time_usage,
4376 100.0*(total_time_usage-tot)/total_time_usage,
4377 100.0);
4378 if(total_frame_cnt && frame_dropping)
4379 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4380 total_frame_cnt-drop_frame_cnt,
4381 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4382 drop_frame_cnt,
4383 100*drop_frame_cnt/total_frame_cnt,
4384 total_frame_cnt,
4385 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4388 // time to uninit all, except global stuff:
4389 uninit_player(mpctx, INITIALIZED_ALL-(opts->fixed_vo?INITIALIZED_VO:0));
4391 if(mpctx->set_of_sub_size > 0) {
4392 current_module="sub_free";
4393 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4394 sub_free(mpctx->set_of_subtitles[i]);
4395 #ifdef CONFIG_ASS
4396 if(mpctx->set_of_ass_tracks[i])
4397 ass_free_track( mpctx->set_of_ass_tracks[i] );
4398 #endif
4400 mpctx->set_of_sub_size = 0;
4402 vo_sub_last = vo_sub=NULL;
4403 subdata=NULL;
4404 #ifdef CONFIG_ASS
4405 ass_track = NULL;
4406 if(ass_library)
4407 ass_clear_fonts(ass_library);
4408 #endif
4410 if (!mpctx->stop_play) // In case some goto jumped here...
4411 mpctx->stop_play = PT_NEXT_ENTRY;
4413 int playtree_direction = 1;
4415 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4416 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4417 play_tree_iter_free(mpctx->playtree_iter);
4418 mpctx->playtree_iter = NULL;
4420 mpctx->play_tree_step = 1;
4421 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4422 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4423 if(mpctx->playtree_iter) {
4424 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4425 play_tree_iter_free(mpctx->playtree_iter);
4426 mpctx->playtree_iter = NULL;
4429 } else if (mpctx->stop_play == PT_STOP) {
4430 play_tree_iter_free(mpctx->playtree_iter);
4431 mpctx->playtree_iter = NULL;
4432 } else { // NEXT PREV SRC
4433 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4436 while(mpctx->playtree_iter != NULL) {
4437 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4438 if(mpctx->filename == NULL) {
4439 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4440 play_tree_iter_free(mpctx->playtree_iter);
4441 mpctx->playtree_iter = NULL;
4443 } else
4444 break;
4447 if(mpctx->playtree_iter != NULL || player_idle_mode){
4448 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4449 mpctx->stop_play = 0;
4450 goto play_next_file;
4454 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4456 return 1;
4458 #endif /* DISABLE_MAIN */