sync with en/mplayer.1 rev. 30936
[mplayer/glamo.git] / mplayer.c
blob36f9155fd0d319b26c2cb466569f64a9d625c204
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 "config.h"
26 #if defined(__MINGW32__) || defined(__CYGWIN__)
27 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
28 #include <windows.h>
29 #endif
30 #include <string.h>
31 #include <unistd.h>
33 // #include <sys/mman.h>
34 #include <sys/types.h>
35 #ifndef __MINGW32__
36 #include <sys/ioctl.h>
37 #include <sys/wait.h>
38 #else
39 #define SIGHUP 1 /* hangup */
40 #define SIGQUIT 3 /* quit */
41 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
42 #define SIGBUS 10 /* bus error */
43 #define SIGPIPE 13 /* broken pipe */
44 #endif
46 #include <sys/time.h>
47 #include <sys/stat.h>
49 #include <signal.h>
50 #include <time.h>
51 #include <fcntl.h>
52 #include <limits.h>
54 #include <errno.h>
56 #include "mp_msg.h"
58 #define HELP_MP_DEFINE_STATIC
59 #include "help_mp.h"
61 #include "m_option.h"
62 #include "m_config.h"
63 #include "m_property.h"
65 #include "cfg-mplayer-def.h"
67 #include "libavutil/intreadwrite.h"
68 #include "libavutil/avstring.h"
70 #include "subreader.h"
72 #include "libvo/video_out.h"
74 #include "libvo/font_load.h"
75 #include "libvo/sub.h"
77 #ifdef CONFIG_X11
78 #include "libvo/x11_common.h"
79 #endif
81 #include "libao2/audio_out.h"
83 #include "codec-cfg.h"
85 #include "edl.h"
86 #include "mplayer.h"
87 #include "spudec.h"
88 #include "vobsub.h"
89 #include "access_mpcontext.h"
91 #include "osdep/getch2.h"
92 #include "osdep/timer.h"
94 #include "gui/interface.h"
96 #include "input/input.h"
97 #include "loader/codecpath.h"
99 int slave_mode=0;
100 int player_idle_mode=0;
101 int quiet=0;
102 int enable_mouse_movements=0;
103 float start_volume = -1;
105 #include "osdep/priority.h"
107 char *heartbeat_cmd;
109 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
111 #ifdef HAVE_RTC
112 #ifdef __linux__
113 #include <linux/rtc.h>
114 #else
115 #include <rtc.h>
116 #define RTC_IRQP_SET RTCIO_IRQP_SET
117 #define RTC_PIE_ON RTCIO_PIE_ON
118 #endif /* __linux__ */
119 #endif /* HAVE_RTC */
121 #include "stream/tv.h"
122 #include "stream/stream_radio.h"
123 #ifdef CONFIG_DVBIN
124 #include "stream/dvbin.h"
125 #endif
126 #include "stream/cache2.h"
128 //**************************************************************************//
129 // Playtree
130 //**************************************************************************//
131 #include "playtree.h"
132 #include "playtreeparser.h"
134 //**************************************************************************//
135 // Config
136 //**************************************************************************//
137 #include "parser-cfg.h"
138 #include "parser-mpcmd.h"
140 m_config_t* mconfig;
142 //**************************************************************************//
143 // Config file
144 //**************************************************************************//
146 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
148 static int cfg_include(m_option_t *conf, char *filename){
149 return m_config_parse_config_file(mconfig, filename);
152 #include "get_path.h"
154 //**************************************************************************//
155 //**************************************************************************//
156 // Input media streaming & demultiplexer:
157 //**************************************************************************//
159 static int max_framesize=0;
161 #include "stream/stream.h"
162 #include "libmpdemux/demuxer.h"
163 #include "libmpdemux/stheader.h"
165 #ifdef CONFIG_DVDREAD
166 #include "stream/stream_dvd.h"
167 #endif
168 #include "stream/stream_dvdnav.h"
170 #include "libmpcodecs/dec_audio.h"
171 #include "libmpcodecs/dec_video.h"
172 #include "libmpcodecs/mp_image.h"
173 #include "libmpcodecs/vf.h"
174 #include "libmpcodecs/vd.h"
176 #include "mixer.h"
178 #include "mp_core.h"
180 //**************************************************************************//
181 //**************************************************************************//
183 // Common FIFO functions, and keyboard/event FIFO code
184 #include "mp_fifo.h"
185 int noconsolecontrols=0;
186 //**************************************************************************//
188 // Not all functions in mplayer.c take the context as an argument yet
189 static MPContext mpctx_s = {
190 .osd_function = OSD_PLAY,
191 .begin_skip = MP_NOPTS_VALUE,
192 .play_tree_step = 1,
193 .global_sub_pos = -1,
194 .set_of_sub_pos = -1,
195 .file_format = DEMUXER_TYPE_UNKNOWN,
196 .loop_times = -1,
197 #ifdef CONFIG_DVBIN
198 .last_dvb_step = 1,
199 #endif
202 static MPContext *mpctx = &mpctx_s;
204 int fixed_vo=0;
206 // benchmark:
207 double video_time_usage=0;
208 double vout_time_usage=0;
209 static double audio_time_usage=0;
210 static int total_time_usage_start=0;
211 static int total_frame_cnt=0;
212 static int drop_frame_cnt=0; // total number of dropped frames
213 int benchmark=0;
215 // options:
216 #define DEFAULT_STARTUP_DECODE_RETRY 8
217 int auto_quality=0;
218 static int output_quality=0;
220 float playback_speed=1.0;
222 int use_gui=0;
224 #ifdef CONFIG_GUI
225 int enqueue=0;
226 #endif
228 static int list_properties = 0;
230 int osd_level=1;
231 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
232 unsigned int osd_visible;
233 int osd_duration = 1000;
235 int term_osd = 1;
236 static char* term_osd_esc = "\x1b[A\r\x1b[K";
237 static char* playing_msg = NULL;
238 // seek:
239 static double seek_to_sec;
240 static off_t seek_to_byte=0;
241 static off_t step_sec=0;
242 static int loop_seek=0;
244 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
246 // A/V sync:
247 int autosync=0; // 30 might be a good default value.
249 // may be changed by GUI: (FIXME!)
250 float rel_seek_secs=0;
251 int abs_seek_pos=0;
253 // codecs:
254 char **audio_codec_list=NULL; // override audio codec
255 char **video_codec_list=NULL; // override video codec
256 char **audio_fm_list=NULL; // override audio codec family
257 char **video_fm_list=NULL; // override video codec family
259 // demuxer:
260 extern char *demuxer_name; // override demuxer
261 extern char *audio_demuxer_name; // override audio demuxer
262 extern char *sub_demuxer_name; // override sub demuxer
264 // streaming:
265 int audio_id=-1;
266 int video_id=-1;
267 int dvdsub_id=-1;
268 // this dvdsub_id was selected via slang
269 // use this to allow dvdnav to follow -slang across stream resets,
270 // in particular the subtitle ID for a language changes
271 int dvdsub_lang_id;
272 int vobsub_id=-1;
273 char* audio_lang=NULL;
274 char* dvdsub_lang=NULL;
275 static char* spudec_ifo=NULL;
276 char* filename=NULL; //"MI2-Trailer.avi";
277 int forced_subs_only=0;
278 int file_filter=1;
280 // cache2:
281 int stream_cache_size=-1;
282 #ifdef CONFIG_STREAM_CACHE
283 extern int cache_fill_status;
285 float stream_cache_min_percent=20.0;
286 float stream_cache_seek_min_percent=50.0;
287 #else
288 #define cache_fill_status 0
289 #endif
291 // dump:
292 static char *stream_dump_name="stream.dump";
293 int stream_dump_type=0;
295 // A-V sync:
296 static float default_max_pts_correction=-1;//0.01f;
297 static float max_pts_correction=0;//default_max_pts_correction;
298 static float c_total=0;
299 float audio_delay=0;
300 static int ignore_start=0;
302 static int softsleep=0;
304 double force_fps=0;
305 static int force_srate=0;
306 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
307 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
308 static int play_n_frames=-1;
309 static int play_n_frames_mf=-1;
311 // screen info:
312 char** video_driver_list=NULL;
313 char** audio_driver_list=NULL;
315 // sub:
316 char *font_name=NULL;
317 char *sub_font_name=NULL;
318 extern int font_fontconfig;
319 float font_factor=0.75;
320 char **sub_name=NULL;
321 float sub_delay=0;
322 float sub_fps=0;
323 int sub_auto = 1;
324 char *vobsub_name=NULL;
325 /*DSP!!char *dsp=NULL;*/
326 int subcc_enabled=0;
327 int suboverlap_enabled = 1;
329 #include "libass/ass_mp.h"
331 char* current_module=NULL; // for debugging
334 // ---
336 #ifdef CONFIG_MENU
337 #include "m_struct.h"
338 #include "libmenu/menu.h"
339 extern vf_info_t vf_info_menu;
340 static vf_info_t* libmenu_vfs[] = {
341 &vf_info_menu,
342 NULL
344 static vf_instance_t* vf_menu = NULL;
345 int use_menu = 0;
346 static char* menu_cfg = NULL;
347 static char* menu_root = "main";
348 #endif
351 #ifdef HAVE_RTC
352 static int nortc = 1;
353 static char* rtc_device;
354 #endif
356 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
357 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
358 short edl_decision = 0; ///< 1 when an EDL operation has been made.
359 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
360 int use_filedir_conf;
361 int use_filename_title;
363 static unsigned int initialized_flags=0;
364 #include "mpcommon.h"
365 #include "command.h"
367 #include "metadata.h"
369 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
371 const void *mpctx_get_video_out(MPContext *mpctx)
373 return mpctx->video_out;
376 const void *mpctx_get_audio_out(MPContext *mpctx)
378 return mpctx->audio_out;
381 void *mpctx_get_demuxer(MPContext *mpctx)
383 return mpctx->demuxer;
386 void *mpctx_get_playtree_iter(MPContext *mpctx)
388 return mpctx->playtree_iter;
391 void *mpctx_get_mixer(MPContext *mpctx)
393 return &mpctx->mixer;
396 int mpctx_get_global_sub_size(MPContext *mpctx)
398 return mpctx->global_sub_size;
401 int mpctx_get_osd_function(MPContext *mpctx)
403 return mpctx->osd_function;
406 static int is_valid_metadata_type (metadata_t type) {
407 switch (type)
409 /* check for valid video stream */
410 case META_VIDEO_CODEC:
411 case META_VIDEO_BITRATE:
412 case META_VIDEO_RESOLUTION:
414 if (!mpctx->sh_video)
415 return 0;
416 break;
419 /* check for valid audio stream */
420 case META_AUDIO_CODEC:
421 case META_AUDIO_BITRATE:
422 case META_AUDIO_SAMPLES:
424 if (!mpctx->sh_audio)
425 return 0;
426 break;
429 /* check for valid demuxer */
430 case META_INFO_TITLE:
431 case META_INFO_ARTIST:
432 case META_INFO_ALBUM:
433 case META_INFO_YEAR:
434 case META_INFO_COMMENT:
435 case META_INFO_TRACK:
436 case META_INFO_GENRE:
438 if (!mpctx->demuxer)
439 return 0;
440 break;
443 default:
444 break;
447 return 1;
450 static char *get_demuxer_info (char *tag) {
451 char **info = mpctx->demuxer->info;
452 int n;
454 if (!info || !tag)
455 return NULL;
457 for (n = 0; info[2*n] != NULL ; n++)
458 if (!strcasecmp (info[2*n], tag))
459 break;
461 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
464 char *get_metadata (metadata_t type) {
465 char *meta = NULL;
466 sh_audio_t * const sh_audio = mpctx->sh_audio;
467 sh_video_t * const sh_video = mpctx->sh_video;
469 if (!is_valid_metadata_type (type))
470 return NULL;
472 switch (type)
474 case META_NAME:
476 return strdup (mp_basename2 (filename));
479 case META_VIDEO_CODEC:
481 if (sh_video->format == 0x10000001)
482 meta = strdup ("mpeg1");
483 else if (sh_video->format == 0x10000002)
484 meta = strdup ("mpeg2");
485 else if (sh_video->format == 0x10000004)
486 meta = strdup ("mpeg4");
487 else if (sh_video->format == 0x10000005)
488 meta = strdup ("h264");
489 else if (sh_video->format >= 0x20202020)
491 meta = malloc (8);
492 sprintf (meta, "%.4s", (char *) &sh_video->format);
494 else
496 meta = malloc (8);
497 sprintf (meta, "0x%08X", sh_video->format);
499 return meta;
502 case META_VIDEO_BITRATE:
504 meta = malloc (16);
505 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
506 return meta;
509 case META_VIDEO_RESOLUTION:
511 meta = malloc (16);
512 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
513 return meta;
516 case META_AUDIO_CODEC:
518 if (sh_audio->codec && sh_audio->codec->name)
519 meta = strdup (sh_audio->codec->name);
520 return meta;
523 case META_AUDIO_BITRATE:
525 meta = malloc (16);
526 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
527 return meta;
530 case META_AUDIO_SAMPLES:
532 meta = malloc (16);
533 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
534 return meta;
537 /* check for valid demuxer */
538 case META_INFO_TITLE:
539 return get_demuxer_info ("Title");
541 case META_INFO_ARTIST:
542 return get_demuxer_info ("Artist");
544 case META_INFO_ALBUM:
545 return get_demuxer_info ("Album");
547 case META_INFO_YEAR:
548 return get_demuxer_info ("Year");
550 case META_INFO_COMMENT:
551 return get_demuxer_info ("Comment");
553 case META_INFO_TRACK:
554 return get_demuxer_info ("Track");
556 case META_INFO_GENRE:
557 return get_demuxer_info ("Genre");
559 default:
560 break;
563 return meta;
566 /// step size of mixer changes
567 int volstep = 3;
569 #ifdef CONFIG_DVDNAV
570 static void mp_dvdnav_context_free(MPContext *ctx){
571 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
572 ctx->nav_smpi = NULL;
573 if (ctx->nav_buffer) free(ctx->nav_buffer);
574 ctx->nav_buffer = NULL;
575 ctx->nav_start = NULL;
576 ctx->nav_in_size = 0;
578 #endif
580 void uninit_player(unsigned int mask){
581 mask &= initialized_flags;
583 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
585 if(mask&INITIALIZED_ACODEC){
586 initialized_flags&=~INITIALIZED_ACODEC;
587 current_module="uninit_acodec";
588 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
589 #ifdef CONFIG_GUI
590 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
591 #endif
592 mpctx->sh_audio=NULL;
593 mpctx->mixer.afilter = NULL;
596 if(mask&INITIALIZED_VCODEC){
597 initialized_flags&=~INITIALIZED_VCODEC;
598 current_module="uninit_vcodec";
599 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
600 mpctx->sh_video=NULL;
601 #ifdef CONFIG_MENU
602 vf_menu=NULL;
603 #endif
606 if(mask&INITIALIZED_DEMUXER){
607 initialized_flags&=~INITIALIZED_DEMUXER;
608 current_module="free_demuxer";
609 if(mpctx->demuxer){
610 mpctx->stream=mpctx->demuxer->stream;
611 free_demuxer(mpctx->demuxer);
613 mpctx->demuxer=NULL;
616 // kill the cache process:
617 if(mask&INITIALIZED_STREAM){
618 initialized_flags&=~INITIALIZED_STREAM;
619 current_module="uninit_stream";
620 if(mpctx->stream) free_stream(mpctx->stream);
621 mpctx->stream=NULL;
624 if(mask&INITIALIZED_VO){
625 initialized_flags&=~INITIALIZED_VO;
626 current_module="uninit_vo";
627 mpctx->video_out->uninit();
628 mpctx->video_out=NULL;
629 #ifdef CONFIG_DVDNAV
630 mp_dvdnav_context_free(mpctx);
631 #endif
634 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
635 if(mask&INITIALIZED_GETCH2){
636 initialized_flags&=~INITIALIZED_GETCH2;
637 current_module="uninit_getch2";
638 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
639 // restore terminal:
640 getch2_disable();
643 if(mask&INITIALIZED_VOBSUB){
644 initialized_flags&=~INITIALIZED_VOBSUB;
645 current_module="uninit_vobsub";
646 if(vo_vobsub) vobsub_close(vo_vobsub);
647 vo_vobsub=NULL;
650 if (mask&INITIALIZED_SPUDEC){
651 initialized_flags&=~INITIALIZED_SPUDEC;
652 current_module="uninit_spudec";
653 spudec_free(vo_spudec);
654 vo_spudec=NULL;
657 if(mask&INITIALIZED_AO){
658 initialized_flags&=~INITIALIZED_AO;
659 current_module="uninit_ao";
660 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
661 if (mpctx->audio_out) mpctx->audio_out->uninit(mpctx->eof?0:1);
662 mpctx->audio_out=NULL;
665 #ifdef CONFIG_GUI
666 if(mask&INITIALIZED_GUI){
667 initialized_flags&=~INITIALIZED_GUI;
668 current_module="uninit_gui";
669 guiDone();
671 #endif
673 if(mask&INITIALIZED_INPUT){
674 initialized_flags&=~INITIALIZED_INPUT;
675 current_module="uninit_input";
676 mp_input_uninit();
677 #ifdef CONFIG_MENU
678 if (use_menu)
679 menu_uninit();
680 #endif
683 current_module=NULL;
686 void exit_player_with_rc(enum exit_reason how, int rc)
689 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
690 uninit_player(INITIALIZED_ALL);
691 #if defined(__MINGW32__) || defined(__CYGWIN__)
692 timeEndPeriod(1);
693 #endif
694 #ifdef CONFIG_X11
695 #ifdef CONFIG_GUI
696 if ( !use_gui )
697 #endif
698 vo_uninit(); // Close the X11 connection (if any is open).
699 #endif
701 #ifdef CONFIG_FREETYPE
702 current_module="uninit_font";
703 if (sub_font && sub_font != vo_font) free_font_desc(sub_font);
704 sub_font = NULL;
705 if (vo_font) free_font_desc(vo_font);
706 vo_font = NULL;
707 done_freetype();
708 #endif
709 free_osd_list();
711 #ifdef CONFIG_ASS
712 ass_library_done(ass_library);
713 ass_library = NULL;
714 #endif
716 current_module="exit_player";
718 // free mplayer config
719 if(mconfig)
720 m_config_free(mconfig);
721 mconfig = NULL;
723 if(mpctx->playtree_iter)
724 play_tree_iter_free(mpctx->playtree_iter);
725 mpctx->playtree_iter = NULL;
726 if(mpctx->playtree)
727 play_tree_free(mpctx->playtree, 1);
728 mpctx->playtree = NULL;
731 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
732 edl_records = NULL;
733 switch(how) {
734 case EXIT_QUIT:
735 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_quit);
736 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
737 break;
738 case EXIT_EOF:
739 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_eof);
740 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
741 break;
742 case EXIT_ERROR:
743 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_error);
744 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
745 break;
746 default:
747 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
749 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
751 exit(rc);
754 void exit_player(enum exit_reason how)
756 exit_player_with_rc(how, 1);
759 #ifndef __MINGW32__
760 static void child_sighandler(int x){
761 pid_t pid;
762 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
764 #endif
766 #ifdef CONFIG_CRASH_DEBUG
767 static char *prog_path;
768 static int crash_debug = 0;
769 #endif
771 static void exit_sighandler(int x){
772 static int sig_count=0;
773 #ifdef CONFIG_CRASH_DEBUG
774 if (!crash_debug || x != SIGTRAP)
775 #endif
776 ++sig_count;
777 if(initialized_flags==0 && sig_count>1) exit(1);
778 if(sig_count==5)
780 /* We're crashing bad and can't uninit cleanly :(
781 * by popular request, we make one last (dirty)
782 * effort to restore the user's
783 * terminal. */
784 getch2_disable();
785 exit(1);
787 if(sig_count==6) exit(1);
788 if(sig_count>6){
789 // can't stop :(
790 #ifndef __MINGW32__
791 kill(getpid(),SIGKILL);
792 #endif
794 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
795 current_module?current_module:"unknown"
797 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
798 if(sig_count<=1)
799 switch(x){
800 case SIGINT:
801 case SIGQUIT:
802 case SIGTERM:
803 case SIGKILL:
804 async_quit_request = 1;
805 return; // killed from keyboard (^C) or killed [-9]
806 case SIGILL:
807 #if CONFIG_RUNTIME_CPUDETECT
808 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
809 #else
810 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
811 #endif
812 case SIGFPE:
813 case SIGSEGV:
814 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
815 default:
816 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
817 #ifdef CONFIG_CRASH_DEBUG
818 if (crash_debug) {
819 int gdb_pid;
820 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
821 gdb_pid = fork();
822 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
823 if (gdb_pid == 0) { // We are the child
824 char spid[20];
825 snprintf(spid, sizeof(spid), "%i", getppid());
826 getch2_disable(); // allow terminal to work properly with gdb
827 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
828 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
829 } else if (gdb_pid < 0)
830 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
831 else {
832 waitpid(gdb_pid, NULL, 0);
834 if (x == SIGTRAP) return;
836 #endif
838 getch2_disable();
839 exit(1);
842 #include "cfg-mplayer.h"
844 static void parse_cfgfiles( m_config_t* conf )
846 char *conffile;
847 int conffile_fd;
848 if (!disable_system_conf &&
849 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
850 exit_player(EXIT_NONE);
851 if ((conffile = get_path("")) == NULL) {
852 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
853 } else {
854 #ifdef __MINGW32__
855 mkdir(conffile);
856 #else
857 mkdir(conffile, 0777);
858 #endif
859 free(conffile);
860 if ((conffile = get_path("config")) == NULL) {
861 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
862 } else {
863 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
864 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
865 write(conffile_fd, default_config, strlen(default_config));
866 close(conffile_fd);
868 if (!disable_user_conf &&
869 m_config_parse_config_file(conf, conffile) < 0)
870 exit_player(EXIT_NONE);
871 free(conffile);
876 #define PROFILE_CFG_PROTOCOL "protocol."
878 static void load_per_protocol_config (m_config_t* conf, const char *const file)
880 char *str;
881 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
882 m_profile_t *p;
884 /* does filename actually uses a protocol ? */
885 str = strstr (file, "://");
886 if (!str)
887 return;
889 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
890 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
891 p = m_config_get_profile (conf, protocol);
892 if (p)
894 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingProtocolProfile, protocol);
895 m_config_set_profile(conf,p);
899 #define PROFILE_CFG_EXTENSION "extension."
901 static void load_per_extension_config (m_config_t* conf, const char *const file)
903 char *str;
904 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
905 m_profile_t *p;
907 /* does filename actually have an extension ? */
908 str = strrchr (filename, '.');
909 if (!str)
910 return;
912 sprintf (extension, PROFILE_CFG_EXTENSION);
913 strncat (extension, ++str, 7);
914 p = m_config_get_profile (conf, extension);
915 if (p)
917 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, extension);
918 m_config_set_profile(conf,p);
922 #define PROFILE_CFG_VO "vo."
923 #define PROFILE_CFG_AO "ao."
925 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
927 char profile[strlen (cfg) + strlen (out) + 1];
928 m_profile_t *p;
930 sprintf (profile, "%s%s", cfg, out);
931 p = m_config_get_profile (conf, profile);
932 if (p)
934 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, profile);
935 m_config_set_profile(conf,p);
940 * Tries to load a config file
941 * @return 0 if file was not found, 1 otherwise
943 static int try_load_config(m_config_t *conf, const char *file)
945 struct stat st;
946 if (stat(file, &st))
947 return 0;
948 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, file);
949 m_config_parse_config_file (conf, file);
950 return 1;
953 static void load_per_file_config (m_config_t* conf, const char *const file)
955 char *confpath;
956 char cfg[PATH_MAX];
957 char *name;
959 if (strlen(file) > PATH_MAX - 14) {
960 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Filename is too long, can not load file or directory specific config files\n");
961 return;
963 sprintf (cfg, "%s.conf", file);
965 name = strrchr(cfg, '/');
966 if (HAVE_DOS_PATHS) {
967 char *tmp = strrchr(cfg, '\\');
968 if (!name || tmp > name)
969 name = tmp;
970 tmp = strrchr(cfg, ':');
971 if (!name || tmp > name)
972 name = tmp;
974 if (!name)
975 name = cfg;
976 else
977 name++;
979 if (use_filedir_conf) {
980 char dircfg[PATH_MAX];
981 strcpy(dircfg, cfg);
982 strcpy(dircfg + (name - cfg), "mplayer.conf");
983 try_load_config(conf, dircfg);
985 if (try_load_config(conf, cfg))
986 return;
989 if ((confpath = get_path (name)) != NULL)
991 try_load_config(conf, confpath);
993 free (confpath);
997 /* When libmpdemux performs a blocking operation (network connection or
998 * cache filling) if the operation fails we use this function to check
999 * if it was interrupted by the user.
1000 * The function returns a new value for eof. */
1001 static int libmpdemux_was_interrupted(int eof) {
1002 mp_cmd_t* cmd;
1003 if((cmd = mp_input_get_cmd(0,0,0)) != NULL) {
1004 switch(cmd->id) {
1005 case MP_CMD_QUIT:
1006 exit_player_with_rc(EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
1007 case MP_CMD_PLAY_TREE_STEP: {
1008 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
1009 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
1010 } break;
1011 case MP_CMD_PLAY_TREE_UP_STEP: {
1012 eof = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
1013 } break;
1014 case MP_CMD_PLAY_ALT_SRC_STEP: {
1015 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
1016 } break;
1018 mp_cmd_free(cmd);
1020 return eof;
1023 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
1025 static int playtree_add_playlist(play_tree_t* entry)
1027 play_tree_add_bpf(entry,filename);
1029 #ifdef CONFIG_GUI
1030 if (use_gui) {
1031 if (entry) {
1032 import_playtree_playlist_into_gui(entry, mconfig);
1033 play_tree_free_list(entry,1);
1035 } else
1036 #endif
1038 if(!entry) {
1039 entry = mpctx->playtree_iter->tree;
1040 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1041 return PT_NEXT_ENTRY;
1043 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
1044 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1045 return PT_NEXT_ENTRY;
1048 play_tree_remove(entry,1,1);
1049 return PT_NEXT_SRC;
1051 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
1052 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1053 entry = mpctx->playtree_iter->tree;
1054 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1055 return PT_NEXT_ENTRY;
1057 play_tree_remove(entry,1,1);
1059 return PT_NEXT_SRC;
1062 void add_subtitles(char *filename, float fps, int noerr)
1064 sub_data *subd;
1065 #ifdef CONFIG_ASS
1066 ass_track_t *asst = 0;
1067 #endif
1069 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1070 return;
1073 subd = sub_read_file(filename, fps);
1074 #ifdef CONFIG_ASS
1075 if (ass_enabled)
1076 #ifdef CONFIG_ICONV
1077 asst = ass_read_stream(ass_library, filename, sub_cp);
1078 #else
1079 asst = ass_read_stream(ass_library, filename, 0);
1080 #endif
1081 if (ass_enabled && subd && !asst)
1082 asst = ass_read_subdata(ass_library, subd, fps);
1084 if (!asst && !subd)
1085 #else
1086 if(!subd)
1087 #endif
1088 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
1089 filename_recode(filename));
1091 #ifdef CONFIG_ASS
1092 if (!asst && !subd) return;
1093 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1094 #else
1095 if (!subd) return;
1096 #endif
1097 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1098 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1099 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1100 filename_recode(filename));
1101 ++mpctx->set_of_sub_size;
1102 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
1103 filename_recode(filename));
1106 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1107 void update_set_of_subtitles(void)
1108 // subdata was changed, set_of_sub... have to be updated.
1110 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
1111 int i;
1112 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1113 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
1114 set_of_subtitles[i-1] = set_of_subtitles[i];
1115 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
1116 --mpctx->set_of_sub_size;
1117 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
1119 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1120 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
1122 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1123 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1124 ++mpctx->set_of_sub_size;
1128 void init_vo_spudec(void) {
1129 if (vo_spudec)
1130 spudec_free(vo_spudec);
1131 initialized_flags &= ~INITIALIZED_SPUDEC;
1132 vo_spudec = NULL;
1134 // we currently can't work without video stream
1135 if (!mpctx->sh_video)
1136 return;
1138 if (spudec_ifo) {
1139 unsigned int palette[16], width, height;
1140 current_module="spudec_init_vobsub";
1141 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1142 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1145 #ifdef CONFIG_DVDREAD
1146 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1147 current_module="spudec_init_dvdread";
1148 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1149 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1150 NULL, 0);
1152 #endif
1154 #ifdef CONFIG_DVDNAV
1155 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1156 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1157 current_module="spudec_init_dvdnav";
1158 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1160 #endif
1162 if (vo_spudec==NULL) {
1163 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1164 current_module="spudec_init_normal";
1165 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1166 spudec_set_font_factor(vo_spudec,font_factor);
1169 if (vo_spudec!=NULL) {
1170 initialized_flags|=INITIALIZED_SPUDEC;
1171 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1176 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1177 * make it all work is to use the builtin SDL-bootstrap code, which
1178 * will be done automatically by replacing our main() if we include SDL.h.
1180 #if defined(__APPLE__) && defined(CONFIG_SDL)
1181 #ifdef CONFIG_SDL_SDL_H
1182 #include <SDL/SDL.h>
1183 #else
1184 #include <SDL.h>
1185 #endif
1186 #endif
1189 * \brief append a formatted string
1190 * \param buf buffer to print into
1191 * \param pos position of terminating 0 in buf
1192 * \param len maximum number of characters in buf, not including terminating 0
1193 * \param format printf format string
1195 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1197 va_list va;
1198 va_start(va, format);
1199 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1200 va_end(va);
1201 if (*pos >= len ) {
1202 buf[len] = 0;
1203 *pos = len;
1208 * \brief append time in the hh:mm:ss.f format
1209 * \param buf buffer to print into
1210 * \param pos position of terminating 0 in buf
1211 * \param len maximum number of characters in buf, not including terminating 0
1212 * \param time time value to convert/append
1214 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1215 int64_t tenths = 10 * time;
1216 int f1 = tenths % 10;
1217 int ss = (tenths / 10) % 60;
1218 int mm = (tenths / 600) % 60;
1219 int hh = tenths / 36000;
1220 if (time <= 0) {
1221 saddf(buf, pos, len, "unknown");
1222 return;
1224 if (hh > 0)
1225 saddf(buf, pos, len, "%2d:", hh);
1226 if (hh > 0 || mm > 0)
1227 saddf(buf, pos, len, "%02d:", mm);
1228 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1232 * \brief print the status line
1233 * \param a_pos audio position
1234 * \param a_v A-V desynchronization
1235 * \param corr amount out A-V synchronization
1237 static void print_status(float a_pos, float a_v, float corr)
1239 sh_video_t * const sh_video = mpctx->sh_video;
1240 int width;
1241 char *line;
1242 unsigned pos = 0;
1243 get_screen_size();
1244 if (screen_width > 0)
1245 width = screen_width;
1246 else
1247 width = 80;
1248 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1249 /* Windows command line is broken (MinGW's rxvt works, but we
1250 * should not depend on that). */
1251 width--;
1252 #endif
1253 line = malloc(width + 1); // one additional char for the terminating null
1255 // Audio time
1256 if (mpctx->sh_audio) {
1257 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1258 if (!sh_video) {
1259 float len = demuxer_get_time_length(mpctx->demuxer);
1260 saddf(line, &pos, width, "(");
1261 sadd_hhmmssf(line, &pos, width, a_pos);
1262 saddf(line, &pos, width, ") of %.1f (", len);
1263 sadd_hhmmssf(line, &pos, width, len);
1264 saddf(line, &pos, width, ") ");
1268 // Video time
1269 if (sh_video)
1270 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1272 // A-V sync
1273 if (mpctx->sh_audio && sh_video)
1274 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
1276 // Video stats
1277 if (sh_video)
1278 saddf(line, &pos, width, "%3d/%3d ",
1279 (int)sh_video->num_frames,
1280 (int)sh_video->num_frames_decoded);
1282 // CPU usage
1283 if (sh_video) {
1284 if (sh_video->timer > 0.5)
1285 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1286 (int)(100.0*video_time_usage*playback_speed/(double)sh_video->timer),
1287 (int)(100.0*vout_time_usage*playback_speed/(double)sh_video->timer),
1288 (100.0*audio_time_usage*playback_speed/(double)sh_video->timer));
1289 else
1290 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1291 } else if (mpctx->sh_audio) {
1292 if (mpctx->delay > 0.5)
1293 saddf(line, &pos, width, "%4.1f%% ",
1294 100.0*audio_time_usage/(double)mpctx->delay);
1295 else
1296 saddf(line, &pos, width, "??,?%% ");
1299 // VO stats
1300 if (sh_video)
1301 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1303 #ifdef CONFIG_STREAM_CACHE
1304 // cache stats
1305 if (stream_cache_size > 0)
1306 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1307 #endif
1309 // other
1310 if (playback_speed != 1)
1311 saddf(line, &pos, width, "%4.2fx ", playback_speed);
1313 // end
1314 if (erase_to_end_of_line) {
1315 line[pos] = 0;
1316 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1317 } else {
1318 memset(&line[pos], ' ', width - pos);
1319 line[width] = 0;
1320 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1322 free(line);
1326 * \brief build a chain of audio filters that converts the input format
1327 * to the ao's format, taking into account the current playback_speed.
1328 * \param sh_audio describes the requested input format of the chain.
1329 * \param ao_data describes the requested output format of the chain.
1331 int build_afilter_chain(sh_audio_t *sh_audio, ao_data_t *ao_data)
1333 int new_srate;
1334 int result;
1335 if (!sh_audio)
1337 #ifdef CONFIG_GUI
1338 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1339 #endif
1340 mpctx->mixer.afilter = NULL;
1341 return 0;
1343 if(af_control_any_rev(sh_audio->afilter,
1344 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1345 &playback_speed)) {
1346 new_srate = sh_audio->samplerate;
1347 } else {
1348 new_srate = sh_audio->samplerate * playback_speed;
1349 if (new_srate != ao_data->samplerate) {
1350 // limits are taken from libaf/af_resample.c
1351 if (new_srate < 8000)
1352 new_srate = 8000;
1353 if (new_srate > 192000)
1354 new_srate = 192000;
1355 playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1358 result = init_audio_filters(sh_audio, new_srate,
1359 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1360 mpctx->mixer.afilter = sh_audio->afilter;
1361 #ifdef CONFIG_GUI
1362 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1363 #endif
1364 return result;
1368 typedef struct mp_osd_msg mp_osd_msg_t;
1369 struct mp_osd_msg {
1370 /// Previous message on the stack.
1371 mp_osd_msg_t* prev;
1372 /// Message text.
1373 char msg[128];
1374 int id,level,started;
1375 /// Display duration in ms.
1376 unsigned time;
1379 /// OSD message stack.
1380 static mp_osd_msg_t* osd_msg_stack = NULL;
1383 * \brief Add a message on the OSD message stack
1385 * If a message with the same id is already present in the stack
1386 * it is pulled on top of the stack, otherwise a new message is created.
1390 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1391 mp_osd_msg_t *msg,*last=NULL;
1392 va_list va;
1393 int r;
1395 // look if the id is already in the stack
1396 for(msg = osd_msg_stack ; msg && msg->id != id ;
1397 last = msg, msg = msg->prev);
1398 // not found: alloc it
1399 if(!msg) {
1400 msg = calloc(1,sizeof(mp_osd_msg_t));
1401 msg->prev = osd_msg_stack;
1402 osd_msg_stack = msg;
1403 } else if(last) { // found, but it's not on top of the stack
1404 last->prev = msg->prev;
1405 msg->prev = osd_msg_stack;
1406 osd_msg_stack = msg;
1408 // write the msg
1409 va_start(va,fmt);
1410 r = vsnprintf(msg->msg, 128, fmt, va);
1411 va_end(va);
1412 if(r >= 128) msg->msg[127] = 0;
1413 // set id and time
1414 msg->id = id;
1415 msg->level = level;
1416 msg->time = time;
1421 * \brief Remove a message from the OSD stack
1423 * This function can be used to get rid of a message right away.
1427 void rm_osd_msg(int id) {
1428 mp_osd_msg_t *msg,*last=NULL;
1430 // Search for the msg
1431 for(msg = osd_msg_stack ; msg && msg->id != id ;
1432 last = msg, msg = msg->prev);
1433 if(!msg) return;
1435 // Detach it from the stack and free it
1436 if(last)
1437 last->prev = msg->prev;
1438 else
1439 osd_msg_stack = msg->prev;
1440 free(msg);
1444 * \brief Remove all messages from the OSD stack
1448 static void clear_osd_msgs(void) {
1449 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1450 while(msg) {
1451 prev = msg->prev;
1452 free(msg);
1453 msg = prev;
1455 osd_msg_stack = NULL;
1459 * \brief Get the current message from the OSD stack.
1461 * This function decrements the message timer and destroys the old ones.
1462 * The message that should be displayed is returned (if any).
1466 static mp_osd_msg_t* get_osd_msg(void) {
1467 mp_osd_msg_t *msg,*prev,*last = NULL;
1468 static unsigned last_update = 0;
1469 unsigned now = GetTimerMS();
1470 unsigned diff;
1471 char hidden_dec_done = 0;
1473 if (osd_visible) {
1474 // 36000000 means max timed visibility is 1 hour into the future, if
1475 // the difference is greater assume it's wrapped around from below 0
1476 if (osd_visible - now > 36000000) {
1477 osd_visible = 0;
1478 vo_osd_progbar_type = -1; // disable
1479 vo_osd_changed(OSDTYPE_PROGBAR);
1480 if (mpctx->osd_function != OSD_PAUSE)
1481 mpctx->osd_function = OSD_PLAY;
1485 if(!last_update) last_update = now;
1486 diff = now >= last_update ? now - last_update : 0;
1488 last_update = now;
1490 // Look for the first message in the stack with high enough level.
1491 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1492 prev = msg->prev;
1493 if(msg->level > osd_level && hidden_dec_done) continue;
1494 // The message has a high enough level or it is the first hidden one
1495 // in both cases we decrement the timer or kill it.
1496 if(!msg->started || msg->time > diff) {
1497 if(msg->started) msg->time -= diff;
1498 else msg->started = 1;
1499 // display it
1500 if(msg->level <= osd_level) return msg;
1501 hidden_dec_done = 1;
1502 continue;
1504 // kill the message
1505 free(msg);
1506 if(last) {
1507 last->prev = prev;
1508 msg = last;
1509 } else {
1510 osd_msg_stack = prev;
1511 msg = NULL;
1514 // Nothing found
1515 return NULL;
1519 * \brief Display the OSD bar.
1521 * Display the OSD bar or fall back on a simple message.
1525 void set_osd_bar(int type,const char* name,double min,double max,double val) {
1527 if(osd_level < 1) return;
1529 if(mpctx->sh_video) {
1530 osd_visible = (GetTimerMS() + 1000) | 1;
1531 vo_osd_progbar_type = type;
1532 vo_osd_progbar_value = 256*(val-min)/(max-min);
1533 vo_osd_changed(OSDTYPE_PROGBAR);
1534 return;
1537 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1538 name,ROUND(100*(val-min)/(max-min)));
1542 * \brief Display text subtitles on the OSD
1544 void set_osd_subtitle(subtitle *subs) {
1545 int i;
1546 vo_sub = subs;
1547 vo_osd_changed(OSDTYPE_SUBTITLE);
1548 if (!mpctx->sh_video) {
1549 // reverse order, since newest set_osd_msg is displayed first
1550 for (i = SUB_MAX_TEXT - 1; i >= 0; i--) {
1551 if (!subs || i >= subs->lines || !subs->text[i])
1552 rm_osd_msg(OSD_MSG_SUB_BASE + i);
1553 else {
1554 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1555 int display_time = i == subs->lines - 1 ? 180000 : 2000;
1556 set_osd_msg(OSD_MSG_SUB_BASE + i, 1, display_time, "%s", subs->text[i]);
1563 * \brief Update the OSD message line.
1565 * This function displays the current message on the vo OSD or on the term.
1566 * If the stack is empty and the OSD level is high enough the timer
1567 * is displayed (only on the vo OSD).
1571 static void update_osd_msg(void) {
1572 mp_osd_msg_t *msg;
1573 static char osd_text[128] = "";
1574 static char osd_text_timer[128];
1576 // we need some mem for vo_osd_text
1577 vo_osd_text = (unsigned char*)osd_text;
1579 // Look if we have a msg
1580 if((msg = get_osd_msg())) {
1581 if(strcmp(osd_text,msg->msg)) {
1582 strncpy((char*)osd_text, msg->msg, 127);
1583 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1584 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1586 return;
1589 if(mpctx->sh_video) {
1590 // fallback on the timer
1591 if(osd_level>=2) {
1592 int len = demuxer_get_time_length(mpctx->demuxer);
1593 int percentage = -1;
1594 char percentage_text[10];
1595 int pts = demuxer_get_current_time(mpctx->demuxer);
1597 if (mpctx->osd_show_percentage)
1598 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1600 if (percentage >= 0)
1601 snprintf(percentage_text, 9, " (%d%%)", percentage);
1602 else
1603 percentage_text[0] = 0;
1605 if (osd_level == 3)
1606 snprintf(osd_text_timer, 63,
1607 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1608 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1609 len/3600,(len/60)%60,len%60,percentage_text);
1610 else
1611 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1612 mpctx->osd_function,pts/3600,(pts/60)%60,
1613 pts%60,percentage_text);
1614 } else
1615 osd_text_timer[0]=0;
1617 // always decrement the percentage timer
1618 if(mpctx->osd_show_percentage)
1619 mpctx->osd_show_percentage--;
1621 if(strcmp(osd_text,osd_text_timer)) {
1622 strncpy(osd_text, osd_text_timer, 63);
1623 vo_osd_changed(OSDTYPE_OSD);
1625 return;
1628 // Clear the term osd line
1629 if(term_osd && osd_text[0]) {
1630 osd_text[0] = 0;
1631 printf("%s\n",term_osd_esc);
1635 ///@}
1636 // OSDMsgStack
1639 void reinit_audio_chain(void) {
1640 if (!mpctx->sh_audio)
1641 return;
1642 current_module="init_audio_codec";
1643 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1644 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1645 goto init_error;
1647 initialized_flags|=INITIALIZED_ACODEC;
1648 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1651 current_module="af_preinit";
1652 ao_data.samplerate=force_srate;
1653 ao_data.channels=0;
1654 ao_data.format=audio_output_format;
1655 // first init to detect best values
1656 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1657 // input:
1658 mpctx->sh_audio->samplerate,
1659 // output:
1660 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1661 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1662 exit_player(EXIT_ERROR);
1664 current_module="ao2_init";
1665 mpctx->audio_out = init_best_audio_out(audio_driver_list,
1666 0, // plugin flag
1667 ao_data.samplerate,
1668 ao_data.channels,
1669 ao_data.format, 0);
1670 if(!mpctx->audio_out){
1671 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1672 goto init_error;
1674 initialized_flags|=INITIALIZED_AO;
1675 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1676 mpctx->audio_out->info->short_name,
1677 ao_data.samplerate, ao_data.channels,
1678 af_fmt2str_short(ao_data.format),
1679 af_fmt2bits(ao_data.format)/8 );
1680 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1681 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1682 if(strlen(mpctx->audio_out->info->comment) > 0)
1683 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1684 // init audio filters:
1685 current_module="af_init";
1686 if(!build_afilter_chain(mpctx->sh_audio, &ao_data)) {
1687 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1688 goto init_error;
1690 mpctx->mixer.audio_out = mpctx->audio_out;
1691 mpctx->mixer.volstep = volstep;
1692 return;
1694 init_error:
1695 uninit_player(INITIALIZED_ACODEC|INITIALIZED_AO); // close codec and possibly AO
1696 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1697 mpctx->d_audio->id = -2;
1701 ///@}
1702 // Command2Property
1705 // Return pts value corresponding to the end point of audio written to the
1706 // ao so far.
1707 static double written_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio)
1709 double buffered_output;
1710 // first calculate the end pts of audio that has been output by decoder
1711 double a_pts = sh_audio->pts;
1712 if (a_pts != MP_NOPTS_VALUE)
1713 // Good, decoder supports new way of calculating audio pts.
1714 // sh_audio->pts is the timestamp of the latest input packet with
1715 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1716 // the amount of bytes the decoder has written after that timestamp.
1717 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1718 else {
1719 // Decoder doesn't support new way of calculating pts (or we're
1720 // being called before it has decoded anything with known timestamp).
1721 // Use the old method of audio pts calculation: take the timestamp
1722 // of last packet with known pts the decoder has read data from,
1723 // and add amount of bytes read after the beginning of that packet
1724 // divided by input bps. This will be inaccurate if the input/output
1725 // ratio is not constant for every audio packet or if it is constant
1726 // but not accurately known in sh_audio->i_bps.
1728 a_pts = d_audio->pts;
1729 // ds_tell_pts returns bytes read after last timestamp from
1730 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1731 // it has read but not decoded
1732 if (sh_audio->i_bps)
1733 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1734 (double)sh_audio->i_bps;
1736 // Now a_pts hopefully holds the pts for end of audio from decoder.
1737 // Substract data in buffers between decoder and audio out.
1739 // Decoded but not filtered
1740 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1742 // Data buffered in audio filters, measured in bytes of "missing" output
1743 buffered_output = af_calc_delay(sh_audio->afilter);
1745 // Data that was ready for ao but was buffered because ao didn't fully
1746 // accept everything to internal buffers yet
1747 buffered_output += sh_audio->a_out_buffer_len;
1749 // Filters divide audio length by playback_speed, so multiply by it
1750 // to get the length in original units without speedup or slowdown
1751 a_pts -= buffered_output * playback_speed / ao_data.bps;
1753 return a_pts;
1756 // Return pts value corresponding to currently playing audio.
1757 double playing_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio,
1758 const ao_functions_t *audio_out)
1760 return written_audio_pts(sh_audio, d_audio) - playback_speed *
1761 audio_out->get_delay();
1764 static int check_framedrop(double frame_time) {
1765 // check for frame-drop:
1766 current_module = "check_framedrop";
1767 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1768 static int dropped_frames;
1769 float delay = playback_speed*mpctx->audio_out->get_delay();
1770 float d = delay-mpctx->delay;
1771 ++total_frame_cnt;
1772 // we should avoid dropping too many frames in sequence unless we
1773 // are too late. and we allow 100ms A-V delay here:
1774 if (d < -dropped_frames*frame_time-0.100 &&
1775 mpctx->osd_function != OSD_PAUSE) {
1776 ++drop_frame_cnt;
1777 ++dropped_frames;
1778 return frame_dropping;
1779 } else
1780 dropped_frames = 0;
1782 return 0;
1785 static int generate_video_frame(sh_video_t *sh_video, demux_stream_t *d_video)
1787 unsigned char *start;
1788 int in_size;
1789 int hit_eof=0;
1790 double pts;
1792 while (1) {
1793 int drop_frame = check_framedrop(sh_video->frametime);
1794 void *decoded_frame;
1795 current_module = "decode video";
1796 // XXX Time used in this call is not counted in any performance
1797 // timer now, OSD is not updated correctly for filter-added frames
1798 if (vf_output_queued_frame(sh_video->vfilter))
1799 break;
1800 current_module = "video_read_frame";
1801 in_size = ds_get_packet_pts(d_video, &start, &pts);
1802 if (in_size < 0) {
1803 // try to extract last frames in case of decoder lag
1804 in_size = 0;
1805 pts = 1e300;
1806 hit_eof = 1;
1808 if (in_size > max_framesize)
1809 max_framesize = in_size;
1810 current_module = "decode video";
1811 decoded_frame = decode_video(sh_video, start, in_size, drop_frame, pts);
1812 if (decoded_frame) {
1813 update_subtitles(sh_video, sh_video->pts, mpctx->d_sub, 0);
1814 update_teletext(sh_video, mpctx->demuxer, 0);
1815 update_osd_msg();
1816 current_module = "filter video";
1817 if (filter_video(sh_video, decoded_frame, sh_video->pts))
1818 break;
1819 } else if (drop_frame)
1820 return -1;
1821 if (hit_eof)
1822 return 0;
1824 return 1;
1827 #ifdef HAVE_RTC
1828 int rtc_fd = -1;
1829 #endif
1831 static float timing_sleep(float time_frame)
1833 #ifdef HAVE_RTC
1834 if (rtc_fd >= 0){
1835 // -------- RTC -----------
1836 current_module="sleep_rtc";
1837 while (time_frame > 0.000) {
1838 unsigned long rtc_ts;
1839 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1840 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1841 time_frame -= GetRelativeTime();
1843 } else
1844 #endif
1846 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1847 // unnecessarily high CPU usage
1848 float margin = softsleep ? 0.011 : 0;
1849 current_module = "sleep_timer";
1850 while (time_frame > margin) {
1851 usec_sleep(1000000 * (time_frame - margin));
1852 time_frame -= GetRelativeTime();
1854 if (softsleep){
1855 current_module = "sleep_soft";
1856 if (time_frame < 0)
1857 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1858 while (time_frame > 0)
1859 time_frame-=GetRelativeTime(); // burn the CPU
1862 return time_frame;
1865 static void select_subtitle(MPContext *mpctx) {
1866 // find the best sub to use
1867 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
1868 mpctx->global_sub_pos = -1; // no subs by default
1869 if (vobsub_index_id >= 0) {
1870 // if user asks for a vobsub id, use that first.
1871 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
1872 } else if (dvdsub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1873 // if user asks for a dvd sub id, use that next.
1874 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id;
1875 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
1876 // if there are text subs to use, use those. (autosubs come last here)
1877 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
1878 } else if (dvdsub_id == -1 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1879 // finally select subs by language and container hints
1880 if (dvdsub_id == -1 && dvdsub_lang)
1881 dvdsub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
1882 if (dvdsub_id == -1)
1883 dvdsub_id = demuxer_default_sub_track(mpctx->demuxer);
1884 if (dvdsub_id >= 0)
1885 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id;
1887 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
1888 mpctx->global_sub_pos--;
1889 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
1892 #ifdef CONFIG_DVDNAV
1893 #ifndef FF_B_TYPE
1894 #define FF_B_TYPE 3
1895 #endif
1896 /// store decoded video image
1897 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1898 mp_image_t *from_mpi) {
1899 mp_image_t *mpi;
1901 /// Do not store B-frames
1902 if (from_mpi->pict_type == FF_B_TYPE)
1903 return to_mpi;
1905 if (to_mpi &&
1906 to_mpi->w == from_mpi->w &&
1907 to_mpi->h == from_mpi->h &&
1908 to_mpi->imgfmt == from_mpi->imgfmt)
1909 mpi = to_mpi;
1910 else {
1911 if (to_mpi)
1912 free_mp_image(to_mpi);
1913 if (from_mpi->w == 0 || from_mpi->h == 0)
1914 return NULL;
1915 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1918 copy_mpi(mpi,from_mpi);
1919 return mpi;
1922 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1923 if (ctx->sh_video) {
1924 /// clear video pts
1925 ctx->d_video->pts = 0.0f;
1926 ctx->sh_video->pts = 0.0f;
1927 ctx->sh_video->i_pts = 0.0f;
1928 ctx->sh_video->last_pts = 0.0f;
1929 ctx->sh_video->num_buffered_pts = 0;
1930 ctx->sh_video->num_frames = 0;
1931 ctx->sh_video->num_frames_decoded = 0;
1932 ctx->sh_video->timer = 0.0f;
1933 ctx->sh_video->stream_delay = 0.0f;
1934 ctx->sh_video->timer = 0;
1935 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1938 if (ctx->sh_audio) {
1939 /// free audio packets and reset
1940 ds_free_packs(ctx->d_audio);
1941 audio_delay -= ctx->sh_audio->stream_delay;
1942 ctx->delay =- audio_delay;
1943 ctx->audio_out->reset();
1944 resync_audio_stream(ctx->sh_audio);
1947 audio_delay = 0.0f;
1948 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + mp_dvdnav_number_of_subs(mpctx->stream);
1949 if (dvdsub_lang && dvdsub_id == dvdsub_lang_id) {
1950 dvdsub_lang_id = mp_dvdnav_sid_from_lang(ctx->stream, dvdsub_lang);
1951 if (dvdsub_lang_id != dvdsub_id) {
1952 dvdsub_id = dvdsub_lang_id;
1953 select_subtitle(ctx);
1957 /// clear all EOF related flags
1958 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1961 /// Restore last decoded DVDNAV (still frame)
1962 static mp_image_t *mp_dvdnav_restore_smpi(int *in_size,
1963 unsigned char **start,
1964 mp_image_t *decoded_frame)
1966 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1967 return decoded_frame;
1969 /// a change occured in dvdnav stream
1970 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1971 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1972 mp_dvdnav_context_free(mpctx);
1973 mp_dvdnav_reset_stream(mpctx);
1974 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1975 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1978 if (*in_size < 0) {
1979 float len;
1981 /// Display still frame, if any
1982 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1983 decoded_frame = mpctx->nav_smpi;
1985 /// increment video frame : continue playing after still frame
1986 len = demuxer_get_time_length(mpctx->demuxer);
1987 if (mpctx->sh_video->pts >= len &&
1988 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1989 mp_dvdnav_skip_still(mpctx->stream);
1990 mp_dvdnav_skip_wait(mpctx->stream);
1992 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1994 if (mpctx->nav_buffer) {
1995 *start = mpctx->nav_start;
1996 *in_size = mpctx->nav_in_size;
1997 if (mpctx->nav_start)
1998 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
2002 return decoded_frame;
2005 /// Save last decoded DVDNAV (still frame)
2006 static void mp_dvdnav_save_smpi(int in_size,
2007 unsigned char *start,
2008 mp_image_t *decoded_frame)
2010 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
2011 return;
2013 if (mpctx->nav_buffer)
2014 free(mpctx->nav_buffer);
2016 mpctx->nav_buffer = malloc(in_size);
2017 mpctx->nav_start = start;
2018 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
2019 if (mpctx->nav_buffer)
2020 memcpy(mpctx->nav_buffer,start,in_size);
2022 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
2023 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
2025 #endif /* CONFIG_DVDNAV */
2027 static void adjust_sync_and_print_status(int between_frames, float timing_error)
2029 current_module="av_sync";
2031 if(mpctx->sh_audio){
2032 double a_pts, v_pts;
2034 if (autosync)
2036 * If autosync is enabled, the value for delay must be calculated
2037 * a bit differently. It is set only to the difference between
2038 * the audio and video timers. Any attempt to include the real
2039 * or corrected delay causes the pts_correction code below to
2040 * try to correct for the changes in delay which autosync is
2041 * trying to measure. This keeps the two from competing, but still
2042 * allows the code to correct for PTS drift *only*. (Using a delay
2043 * value here, even a "corrected" one, would be incompatible with
2044 * autosync mode.)
2046 a_pts = written_audio_pts(mpctx->sh_audio, mpctx->d_audio) - mpctx->delay;
2047 else
2048 a_pts = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
2050 v_pts = mpctx->sh_video->pts;
2053 static int drop_message=0;
2054 double AV_delay = a_pts - audio_delay - v_pts;
2055 double x;
2056 if (AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
2057 ++drop_message;
2058 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
2060 if (autosync)
2061 x = AV_delay*0.1f;
2062 else
2063 /* Do not correct target time for the next frame if this frame
2064 * was late not because of wrong target time but because the
2065 * target time could not be met */
2066 x = (AV_delay + timing_error * playback_speed) * 0.1f;
2067 if (x < -max_pts_correction)
2068 x = -max_pts_correction;
2069 else if (x> max_pts_correction)
2070 x = max_pts_correction;
2071 if (default_max_pts_correction >= 0)
2072 max_pts_correction = default_max_pts_correction;
2073 else
2074 max_pts_correction = mpctx->sh_video->frametime*0.10; // +-10% of time
2075 if (!between_frames) {
2076 mpctx->delay+=x;
2077 c_total+=x;
2079 if(!quiet)
2080 print_status(a_pts - audio_delay, AV_delay, c_total);
2083 } else {
2084 // No audio:
2086 if (!quiet)
2087 print_status(0, 0, 0);
2091 static int fill_audio_out_buffers(void)
2093 unsigned int t;
2094 double tt;
2095 int playsize;
2096 int playflags=0;
2097 int audio_eof=0;
2098 int bytes_to_write;
2099 sh_audio_t * const sh_audio = mpctx->sh_audio;
2101 current_module="play_audio";
2103 while (1) {
2104 int sleep_time;
2105 // all the current uses of ao_data.pts seem to be in aos that handle
2106 // sync completely wrong; there should be no need to use ao_data.pts
2107 // in get_space()
2108 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2109 bytes_to_write = mpctx->audio_out->get_space();
2110 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
2111 break;
2113 // handle audio-only case:
2114 // this is where mplayer sleeps during audio-only playback
2115 // to avoid 100% CPU use
2116 sleep_time = (ao_data.outburst - bytes_to_write) * 1000 / ao_data.bps;
2117 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
2118 usec_sleep(sleep_time * 1000);
2121 while (bytes_to_write) {
2122 playsize = bytes_to_write;
2123 if (playsize > MAX_OUTBURST)
2124 playsize = MAX_OUTBURST;
2125 bytes_to_write -= playsize;
2127 // Fill buffer if needed:
2128 current_module="decode_audio";
2129 t = GetTimer();
2130 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2131 if (mpctx->d_audio->eof) {
2132 audio_eof = 1;
2133 if (sh_audio->a_out_buffer_len == 0)
2134 return 0;
2136 t = GetTimer() - t;
2137 tt = t*0.000001f; audio_time_usage+=tt;
2138 if (playsize > sh_audio->a_out_buffer_len) {
2139 playsize = sh_audio->a_out_buffer_len;
2140 if (audio_eof)
2141 playflags |= AOPLAY_FINAL_CHUNK;
2143 if (!playsize)
2144 break;
2146 // play audio:
2147 current_module="play_audio";
2149 // Is this pts value actually useful for the aos that access it?
2150 // They're obviously badly broken in the way they handle av sync;
2151 // would not having access to this make them more broken?
2152 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2153 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2155 if (playsize > 0) {
2156 sh_audio->a_out_buffer_len -= playsize;
2157 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2158 sh_audio->a_out_buffer_len);
2159 mpctx->delay += playback_speed*playsize/(double)ao_data.bps;
2161 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2162 // Sanity check to avoid hanging in case current ao doesn't output
2163 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2164 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2165 sh_audio->a_out_buffer_len = 0;
2168 return 1;
2171 static int sleep_until_update(float *time_frame, float *aq_sleep_time)
2173 int frame_time_remaining = 0;
2174 current_module="calc_sleep_time";
2176 *time_frame -= GetRelativeTime(); // reset timer
2178 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2179 float delay = mpctx->audio_out->get_delay();
2180 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2182 if (autosync) {
2184 * Adjust this raw delay value by calculating the expected
2185 * delay for this frame and generating a new value which is
2186 * weighted between the two. The higher autosync is, the
2187 * closer to the delay value gets to that which "-nosound"
2188 * would have used, and the longer it will take for A/V
2189 * sync to settle at the right value (but it eventually will.)
2190 * This settling time is very short for values below 100.
2192 float predicted = mpctx->delay / playback_speed + *time_frame;
2193 float difference = delay - predicted;
2194 delay = predicted + difference / (float)autosync;
2197 *time_frame = delay - mpctx->delay / playback_speed;
2199 // delay = amount of audio buffered in soundcard/driver
2200 if (delay > 0.25) delay=0.25; else
2201 if (delay < 0.10) delay=0.10;
2202 if (*time_frame > delay*0.6) {
2203 // sleep time too big - may cause audio drops (buffer underrun)
2204 frame_time_remaining = 1;
2205 *time_frame = delay*0.5;
2207 } else {
2208 // If we're lagging more than 200 ms behind the right playback rate,
2209 // don't try to "catch up".
2210 // If benchmark is set always output frames as fast as possible
2211 // without sleeping.
2212 if (*time_frame < -0.2 || benchmark)
2213 *time_frame = 0;
2216 *aq_sleep_time += *time_frame;
2219 //============================== SLEEP: ===================================
2221 // flag 256 means: libvo driver does its timing (dvb card)
2222 if (*time_frame > 0.001 && !(vo_flags&256))
2223 *time_frame = timing_sleep(*time_frame);
2224 return frame_time_remaining;
2227 int reinit_video_chain(void) {
2228 sh_video_t * const sh_video = mpctx->sh_video;
2229 double ar=-1.0;
2230 //================== Init VIDEO (codec & libvo) ==========================
2231 if(!fixed_vo || !(initialized_flags&INITIALIZED_VO)){
2232 current_module="preinit_libvo";
2234 //shouldn't we set dvideo->id=-2 when we fail?
2235 vo_config_count=0;
2236 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2237 if(!(mpctx->video_out=init_best_video_out(video_driver_list))){
2238 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
2239 goto err_out;
2241 initialized_flags|=INITIALIZED_VO;
2244 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2245 mpctx->sh_video->stream_aspect = ar;
2246 current_module="init_video_filters";
2248 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2249 sh_video->vfilter=(void*)vf_open_filter(NULL,"vo",vf_arg);
2251 #ifdef CONFIG_MENU
2252 if(use_menu) {
2253 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2254 vf_menu = vf_open_plugin(libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2255 if(!vf_menu) {
2256 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2257 use_menu = 0;
2260 if(vf_menu)
2261 sh_video->vfilter=(void*)vf_menu;
2262 #endif
2264 #ifdef CONFIG_ASS
2265 if(ass_enabled) {
2266 int i;
2267 int insert = 1;
2268 if (vf_settings)
2269 for (i = 0; vf_settings[i].name; ++i)
2270 if (strcmp(vf_settings[i].name, "ass") == 0) {
2271 insert = 0;
2272 break;
2274 if (insert) {
2275 extern vf_info_t vf_info_ass;
2276 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2277 char* vf_arg[] = {"auto", "1", NULL};
2278 vf_instance_t* vf_ass = vf_open_plugin(libass_vfs,sh_video->vfilter,"ass",vf_arg);
2279 if (vf_ass)
2280 sh_video->vfilter=(void*)vf_ass;
2281 else
2282 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2285 #endif
2287 sh_video->vfilter=(void*)append_filters(sh_video->vfilter);
2289 #ifdef CONFIG_ASS
2290 if (ass_enabled)
2291 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2292 #endif
2294 current_module="init_video_codec";
2296 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2297 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2298 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2300 if(!sh_video->initialized){
2301 if(!fixed_vo) uninit_player(INITIALIZED_VO);
2302 goto err_out;
2305 initialized_flags|=INITIALIZED_VCODEC;
2307 if (sh_video->codec)
2308 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2310 sh_video->last_pts = MP_NOPTS_VALUE;
2311 sh_video->num_buffered_pts = 0;
2312 sh_video->next_frame_time = 0;
2314 if(auto_quality>0){
2315 // Auto quality option enabled
2316 output_quality=get_video_quality_max(sh_video);
2317 if(auto_quality>output_quality) auto_quality=output_quality;
2318 else output_quality=auto_quality;
2319 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2320 set_video_quality(sh_video,output_quality);
2323 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2325 current_module="init_vo";
2327 return 1;
2329 err_out:
2330 mpctx->sh_video = mpctx->d_video->sh = NULL;
2331 return 0;
2334 static double update_video(int *blit_frame)
2336 sh_video_t * const sh_video = mpctx->sh_video;
2337 //-------------------- Decode a frame: -----------------------
2338 double frame_time;
2339 *blit_frame = 0; // Don't blit if we hit EOF
2340 if (!correct_pts) {
2341 unsigned char* start=NULL;
2342 void *decoded_frame = NULL;
2343 int drop_frame=0;
2344 int in_size;
2346 current_module = "video_read_frame";
2347 frame_time = sh_video->next_frame_time;
2348 in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2349 &start, force_fps);
2350 #ifdef CONFIG_DVDNAV
2351 /// wait, still frame or EOF
2352 if (mpctx->stream->type == STREAMTYPE_DVDNAV && in_size < 0) {
2353 if (mp_dvdnav_is_eof(mpctx->stream)) return -1;
2354 if (mpctx->d_video) mpctx->d_video->eof = 0;
2355 if (mpctx->d_audio) mpctx->d_audio->eof = 0;
2356 mpctx->stream->eof = 0;
2357 } else
2358 #endif
2359 if (in_size < 0)
2360 return -1;
2361 if (in_size > max_framesize)
2362 max_framesize = in_size; // stats
2363 sh_video->timer += frame_time;
2364 if (mpctx->sh_audio)
2365 mpctx->delay -= frame_time;
2366 // video_read_frame can change fps (e.g. for ASF video)
2367 vo_fps = sh_video->fps;
2368 drop_frame = check_framedrop(frame_time);
2369 update_subtitles(sh_video, sh_video->pts, mpctx->d_sub, 0);
2370 update_teletext(sh_video, mpctx->demuxer, 0);
2371 update_osd_msg();
2372 current_module = "decode_video";
2373 #ifdef CONFIG_DVDNAV
2374 decoded_frame = mp_dvdnav_restore_smpi(&in_size,&start,decoded_frame);
2375 /// still frame has been reached, no need to decode
2376 if (in_size > 0 && !decoded_frame)
2377 #endif
2378 decoded_frame = decode_video(sh_video, start, in_size, drop_frame,
2379 sh_video->pts);
2380 #ifdef CONFIG_DVDNAV
2381 /// save back last still frame for future display
2382 mp_dvdnav_save_smpi(in_size,start,decoded_frame);
2383 #endif
2384 current_module = "filter_video";
2385 *blit_frame = (decoded_frame && filter_video(sh_video, decoded_frame,
2386 sh_video->pts));
2388 else {
2389 int res = generate_video_frame(sh_video, mpctx->d_video);
2390 if (!res)
2391 return -1;
2392 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter,
2393 VFCTRL_GET_PTS, &sh_video->pts);
2394 if (sh_video->pts == MP_NOPTS_VALUE) {
2395 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts after filters MISSING\n");
2396 sh_video->pts = sh_video->last_pts;
2398 if (sh_video->last_pts == MP_NOPTS_VALUE)
2399 sh_video->last_pts= sh_video->pts;
2400 else if (sh_video->last_pts > sh_video->pts) {
2401 sh_video->last_pts = sh_video->pts;
2402 mp_msg(MSGT_CPLAYER, MSGL_INFO, "pts value < previous\n");
2404 frame_time = sh_video->pts - sh_video->last_pts;
2405 sh_video->last_pts = sh_video->pts;
2406 sh_video->timer += frame_time;
2407 if(mpctx->sh_audio)
2408 mpctx->delay -= frame_time;
2409 *blit_frame = res > 0;
2411 return frame_time;
2414 static void pause_loop(void)
2416 mp_cmd_t* cmd;
2417 if (!quiet) {
2418 // Small hack to display the pause message on the OSD line.
2419 // The pause string is: "\n == PAUSE == \r" so we need to
2420 // take the first and the last char out
2421 if (term_osd && !mpctx->sh_video) {
2422 char msg[128] = MSGTR_Paused;
2423 int mlen = strlen(msg);
2424 msg[mlen-1] = '\0';
2425 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2426 update_osd_msg();
2427 } else
2428 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2429 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2431 #ifdef CONFIG_GUI
2432 if (use_gui)
2433 guiGetEvent(guiCEvent, (char *)guiSetPause);
2434 #endif
2435 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2436 mpctx->video_out->control(VOCTRL_PAUSE, NULL);
2438 if (mpctx->audio_out && mpctx->sh_audio)
2439 mpctx->audio_out->pause(); // pause audio, keep data if possible
2441 while ( (cmd = mp_input_get_cmd(20, 1, 1)) == NULL || cmd->pausing == 4) {
2442 if (cmd) {
2443 cmd = mp_input_get_cmd(0,1,0);
2444 run_command(mpctx, cmd);
2445 mp_cmd_free(cmd);
2446 continue;
2448 if (mpctx->sh_video && mpctx->video_out && vo_config_count)
2449 mpctx->video_out->check_events();
2450 #ifdef CONFIG_GUI
2451 if (use_gui) {
2452 guiEventHandling();
2453 guiGetEvent(guiReDraw, NULL);
2454 if (guiIntfStruct.Playing!=2 || (rel_seek_secs || abs_seek_pos))
2455 break;
2457 #endif
2458 #ifdef CONFIG_MENU
2459 if (vf_menu)
2460 vf_menu_pause_update(vf_menu);
2461 #endif
2462 usec_sleep(20000);
2464 if (cmd && cmd->id == MP_CMD_PAUSE) {
2465 cmd = mp_input_get_cmd(0,1,0);
2466 mp_cmd_free(cmd);
2468 mpctx->osd_function=OSD_PLAY;
2469 if (mpctx->audio_out && mpctx->sh_audio) {
2470 if (mpctx->eof) // do not play remaining audio if we e.g. switch to the next file
2471 mpctx->audio_out->reset();
2472 else
2473 mpctx->audio_out->resume(); // resume audio
2475 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2476 mpctx->video_out->control(VOCTRL_RESUME, NULL); // resume video
2477 (void)GetRelativeTime(); // ignore time that passed during pause
2478 #ifdef CONFIG_GUI
2479 if (use_gui) {
2480 if (guiIntfStruct.Playing == guiSetStop)
2481 mpctx->eof = 1;
2482 else
2483 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2485 #endif
2489 // Find the right mute status and record position for new file position
2490 static void edl_seek_reset(MPContext *mpctx)
2492 mpctx->edl_muted = 0;
2493 next_edl_record = edl_records;
2495 while (next_edl_record) {
2496 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2497 break;
2499 if (next_edl_record->action == EDL_MUTE)
2500 mpctx->edl_muted = !mpctx->edl_muted;
2501 next_edl_record = next_edl_record->next;
2503 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2504 mixer_mute(&mpctx->mixer);
2508 // Execute EDL command for the current position if one exists
2509 static void edl_update(MPContext *mpctx)
2511 if (!next_edl_record)
2512 return;
2514 if (!mpctx->sh_video) {
2515 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2516 free_edl(edl_records);
2517 next_edl_record = NULL;
2518 edl_records = NULL;
2519 return;
2522 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2523 if (next_edl_record->action == EDL_SKIP) {
2524 mpctx->osd_function = OSD_FFW;
2525 abs_seek_pos = 0;
2526 rel_seek_secs = next_edl_record->length_sec;
2527 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2528 "[%f], length [%f]\n", next_edl_record->start_sec,
2529 next_edl_record->stop_sec, next_edl_record->length_sec);
2530 edl_decision = 1;
2532 else if (next_edl_record->action == EDL_MUTE) {
2533 mpctx->edl_muted = !mpctx->edl_muted;
2534 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2535 mixer_mute(&mpctx->mixer);
2536 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2537 next_edl_record->start_sec );
2539 next_edl_record = next_edl_record->next;
2544 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2545 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2546 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2547 static int seek(MPContext *mpctx, double amount, int style)
2549 current_module = "seek";
2550 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2551 return -1;
2553 mpctx->startup_decode_retry = DEFAULT_STARTUP_DECODE_RETRY;
2554 if (mpctx->sh_video) {
2555 current_module = "seek_video_reset";
2556 if (vo_config_count)
2557 mpctx->video_out->control(VOCTRL_RESET, NULL);
2558 mpctx->num_buffered_frames = 0;
2559 mpctx->delay = 0;
2560 mpctx->time_frame = 0;
2561 // Not all demuxers set d_video->pts during seek, so this value
2562 // (which is used by at least vobsub and edl code below) may
2563 // be completely wrong (probably 0).
2564 mpctx->sh_video->pts = mpctx->d_video->pts;
2565 update_subtitles(mpctx->sh_video, mpctx->sh_video->pts, mpctx->d_sub, 1);
2566 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2569 if (mpctx->sh_audio) {
2570 current_module = "seek_audio_reset";
2571 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2572 if (!mpctx->sh_video)
2573 update_subtitles(NULL, mpctx->sh_audio->pts, mpctx->d_sub, 1);
2576 if (vo_vobsub && mpctx->sh_video) {
2577 current_module = "seek_vobsub_reset";
2578 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2581 edl_seek_reset(mpctx);
2583 c_total = 0;
2584 max_pts_correction = 0.1;
2585 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2586 drop_frame_cnt = 0;
2588 current_module = NULL;
2589 return 0;
2592 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2593 * file for some tools to link against. */
2594 #ifndef DISABLE_MAIN
2595 int main(int argc,char* argv[]){
2598 char * mem_ptr;
2600 // movie info:
2602 /* Flag indicating whether MPlayer should exit without playing anything. */
2603 int opt_exit = 0;
2605 //float a_frame=0; // Audio
2607 int i;
2609 int gui_no_filename=0;
2611 InitTimer();
2612 srand(GetTimerMS());
2614 mp_msg_init();
2616 // Create the config context and register the options
2617 mconfig = m_config_new();
2618 m_config_register_options(mconfig,mplayer_opts);
2619 mp_input_register_options(mconfig);
2621 // Preparse the command line
2622 m_config_preparse_command_line(mconfig,argc,argv);
2624 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2625 set_path_env();
2626 #endif
2628 #ifdef CONFIG_TV
2629 stream_tv_defaults.immediate = 1;
2630 #endif
2632 if (argc > 1 && argv[1] &&
2633 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2634 use_gui = !strcmp(argv[1], "-gui");
2635 } else
2636 if ( argv[0] )
2638 char *base = strrchr(argv[0], '/');
2639 if (!base)
2640 base = strrchr(argv[0], '\\');
2641 if (!base)
2642 base = argv[0];
2643 if(strstr(base, "gmplayer"))
2644 use_gui=1;
2647 parse_cfgfiles(mconfig);
2649 #ifdef CONFIG_GUI
2650 if ( use_gui ) cfg_read();
2651 #endif
2653 mpctx->playtree = m_config_parse_mp_command_line(mconfig, argc, argv);
2654 if(mpctx->playtree == NULL)
2655 opt_exit = 1;
2656 else {
2657 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2658 if(mpctx->playtree) {
2659 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
2660 if(mpctx->playtree_iter) {
2661 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2662 play_tree_iter_free(mpctx->playtree_iter);
2663 mpctx->playtree_iter = NULL;
2665 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2670 print_version("MPlayer");
2672 #if defined(__MINGW32__) || defined(__CYGWIN__)
2673 #ifdef CONFIG_GUI
2674 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2675 if(runningmplayer && filename && use_gui){
2676 COPYDATASTRUCT csData;
2677 char file[MAX_PATH];
2678 char *filepart = filename;
2679 if(GetFullPathName(filename, MAX_PATH, file, &filepart)){
2680 csData.dwData = 0;
2681 csData.cbData = strlen(file)*2;
2682 csData.lpData = file;
2683 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2686 #endif
2689 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
2690 BOOL WINAPI (*setDEP)(DWORD) = NULL;
2691 if (kernel32)
2692 setDEP = GetProcAddress(kernel32, "SetProcessDEPPolicy");
2693 if (setDEP) setDEP(3);
2695 // stop Windows from showing all kinds of annoying error dialogs
2696 SetErrorMode(0x8003);
2697 // request 1ms timer resolution
2698 timeBeginPeriod(1);
2699 #endif
2701 #ifdef CONFIG_PRIORITY
2702 set_priority();
2703 #endif
2705 #ifdef CONFIG_WIN32DLL
2706 if (codec_path)
2707 SetCodecPath(codec_path);
2708 #endif
2710 #ifndef CONFIG_GUI
2711 if(use_gui){
2712 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2713 use_gui=0;
2715 #else
2716 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
2717 if(use_gui && !vo_init()){
2718 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2719 use_gui=0;
2721 #endif
2722 if (use_gui && mpctx->playtree_iter){
2723 char cwd[PATH_MAX+2];
2724 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2725 play_tree_iter_free(mpctx->playtree_iter);
2726 mpctx->playtree_iter=NULL;
2728 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2730 strcat(cwd, "/");
2731 // Prefix relative paths with current working directory
2732 play_tree_add_bpf(mpctx->playtree, cwd);
2734 // Import initital playtree into GUI.
2735 import_initial_playtree_into_gui(mpctx->playtree, mconfig, enqueue);
2737 #endif /* CONFIG_GUI */
2739 if(video_driver_list && strcmp(video_driver_list[0],"help")==0){
2740 list_video_out();
2741 opt_exit = 1;
2744 if(audio_driver_list && strcmp(audio_driver_list[0],"help")==0){
2745 list_audio_out();
2746 opt_exit = 1;
2749 /* Check codecs.conf. */
2750 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2751 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2752 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2753 if(!parse_codec_cfg(NULL)){
2754 exit_player_with_rc(EXIT_NONE, 0);
2756 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2759 free( mem_ptr ); // release the buffer created by get_path()
2762 #if 0
2763 if(video_codec_list){
2764 int i;
2765 video_codec=video_codec_list[0];
2766 for(i=0;video_codec_list[i];i++)
2767 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2769 #endif
2770 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2771 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2772 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2773 list_codecs(1);
2774 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2775 opt_exit = 1;
2777 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2778 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2779 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2780 list_codecs(0);
2781 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2782 opt_exit = 1;
2784 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2785 vfm_help();
2786 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2787 opt_exit = 1;
2789 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2790 afm_help();
2791 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2792 opt_exit = 1;
2794 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2795 af_help();
2796 printf("\n");
2797 opt_exit = 1;
2799 #ifdef CONFIG_X11
2800 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2801 fstype_help();
2802 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2803 opt_exit = 1;
2805 #endif
2806 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2807 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2808 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2809 demuxer_help();
2810 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2811 opt_exit = 1;
2813 if(list_properties) {
2814 property_print_help();
2815 opt_exit = 1;
2818 if(opt_exit)
2819 exit_player(EXIT_NONE);
2821 if (player_idle_mode && use_gui) {
2822 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2823 exit_player_with_rc(EXIT_NONE, 1);
2826 if(!filename && !player_idle_mode){
2827 if(!use_gui){
2828 // no file/vcd/dvd -> show HELP:
2829 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2830 exit_player_with_rc(EXIT_NONE, 0);
2831 } else gui_no_filename=1;
2834 /* Display what configure line was used */
2835 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2837 // Many users forget to include command line in bugreports...
2838 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2839 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2840 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2841 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2844 //------ load global data first ------
2846 // check font
2847 #ifdef CONFIG_FREETYPE
2848 init_freetype();
2849 #endif
2850 #ifdef CONFIG_FONTCONFIG
2851 if(font_fontconfig <= 0)
2853 #endif
2854 #ifdef CONFIG_BITMAP_FONT
2855 if(font_name){
2856 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2857 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2858 filename_recode(font_name));
2859 } else {
2860 // try default:
2861 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2862 free(mem_ptr); // release the buffer created by get_path()
2863 if(!vo_font)
2864 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2866 if (sub_font_name)
2867 sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2868 else
2869 sub_font = vo_font;
2870 #endif
2871 #ifdef CONFIG_FONTCONFIG
2873 #endif
2875 vo_init_osd();
2877 #ifdef CONFIG_ASS
2878 ass_library = ass_init();
2879 #endif
2881 #ifdef HAVE_RTC
2882 if(!nortc)
2884 // seteuid(0); /* Can't hurt to try to get root here */
2885 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2886 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2887 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2888 else {
2889 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2891 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2892 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2893 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2894 close (rtc_fd);
2895 rtc_fd = -1;
2896 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2897 /* variable only by the root */
2898 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2899 close (rtc_fd);
2900 rtc_fd = -1;
2901 } else
2902 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2905 #ifdef CONFIG_GUI
2906 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2907 // and now ? -- Pontscho
2908 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2909 #endif
2910 if(rtc_fd<0)
2911 #endif /* HAVE_RTC */
2912 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2913 softsleep?"software":timer_name);
2915 #ifdef HAVE_TERMCAP
2916 if ( !use_gui ) load_termcap(NULL); // load key-codes
2917 #endif
2919 // ========== Init keyboard FIFO (connection to libvo) ============
2921 // Init input system
2922 current_module = "init_input";
2923 mp_input_init(use_gui);
2924 mp_input_add_key_fd(-1,0,mplayer_get_key,NULL);
2925 if(slave_mode)
2926 mp_input_add_cmd_fd(0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
2927 else if(!noconsolecontrols)
2928 mp_input_add_event_fd(0, getch2);
2929 // Set the libstream interrupt callback
2930 stream_set_interrupt_callback(mp_input_check_interrupt);
2932 #ifdef CONFIG_MENU
2933 if(use_menu) {
2934 if(menu_cfg && menu_init(mpctx, menu_cfg))
2935 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_MenuInitialized, menu_cfg);
2936 else {
2937 menu_cfg = get_path("menu.conf");
2938 if(menu_init(mpctx, menu_cfg))
2939 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_MenuInitialized, menu_cfg);
2940 else {
2941 if(menu_init(mpctx, MPLAYER_CONFDIR "/menu.conf"))
2942 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2943 else {
2944 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_MenuInitFailed);
2945 use_menu = 0;
2950 #endif
2952 initialized_flags|=INITIALIZED_INPUT;
2953 current_module = NULL;
2955 /// Catch signals
2956 #ifndef __MINGW32__
2957 signal(SIGCHLD,child_sighandler);
2958 #endif
2960 #ifdef CONFIG_CRASH_DEBUG
2961 prog_path = argv[0];
2962 #endif
2963 //========= Catch terminate signals: ================
2964 // terminate requests:
2965 signal(SIGTERM,exit_sighandler); // kill
2966 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2968 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2970 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2971 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2972 #ifdef CONFIG_SIGHANDLER
2973 // fatal errors:
2974 signal(SIGBUS,exit_sighandler); // bus error
2975 signal(SIGSEGV,exit_sighandler); // segfault
2976 signal(SIGILL,exit_sighandler); // illegal instruction
2977 signal(SIGFPE,exit_sighandler); // floating point exc.
2978 signal(SIGABRT,exit_sighandler); // abort()
2979 #ifdef CONFIG_CRASH_DEBUG
2980 if (crash_debug)
2981 signal(SIGTRAP,exit_sighandler);
2982 #endif
2983 #endif
2985 #ifdef CONFIG_GUI
2986 if(use_gui){
2987 guiInit();
2988 guiGetEvent(guiSetContext, mpctx);
2989 initialized_flags|=INITIALIZED_GUI;
2990 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2992 #endif
2994 // ******************* Now, let's see the per-file stuff ********************
2996 play_next_file:
2998 // init global sub numbers
2999 mpctx->global_sub_size = 0;
3000 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3002 if (filename) {
3003 load_per_protocol_config (mconfig, filename);
3004 load_per_extension_config (mconfig, filename);
3005 load_per_file_config (mconfig, filename);
3008 if (video_driver_list)
3009 load_per_output_config (mconfig, PROFILE_CFG_VO, video_driver_list[0]);
3010 if (audio_driver_list)
3011 load_per_output_config (mconfig, PROFILE_CFG_AO, audio_driver_list[0]);
3013 // We must enable getch2 here to be able to interrupt network connection
3014 // or cache filling
3015 if(!noconsolecontrols && !slave_mode){
3016 if(initialized_flags&INITIALIZED_GETCH2)
3017 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
3018 else
3019 getch2_enable(); // prepare stdin for hotkeys...
3020 initialized_flags|=INITIALIZED_GETCH2;
3021 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3024 // =================== GUI idle loop (STOP state) ===========================
3025 #ifdef CONFIG_GUI
3026 if ( use_gui ) {
3027 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
3028 guiGetEvent( guiSetDefaults,0 );
3029 while ( guiIntfStruct.Playing != 1 )
3031 mp_cmd_t* cmd;
3032 usec_sleep(20000);
3033 guiEventHandling();
3034 guiGetEvent( guiReDraw,NULL );
3035 if ( (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
3036 guiGetEvent(guiIEvent, (char *)cmd->id);
3037 mp_cmd_free(cmd);
3040 guiGetEvent( guiSetParameters,NULL );
3041 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
3043 play_tree_t * entry = play_tree_new();
3044 play_tree_add_file( entry,guiIntfStruct.Filename );
3045 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
3046 else mpctx->playtree=play_tree_new();
3047 play_tree_set_child( mpctx->playtree,entry );
3048 if(mpctx->playtree)
3050 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
3051 if(mpctx->playtree_iter)
3053 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
3055 play_tree_iter_free(mpctx->playtree_iter);
3056 mpctx->playtree_iter = NULL;
3058 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3063 #endif /* CONFIG_GUI */
3065 while (player_idle_mode && !filename) {
3066 play_tree_t * entry = NULL;
3067 mp_cmd_t * cmd;
3068 if (mpctx->video_out && vo_config_count)
3069 mpctx->video_out->control(VOCTRL_PAUSE, NULL);
3070 while (!(cmd = mp_input_get_cmd(0,1,0))) { // wait for command
3071 if (mpctx->video_out && vo_config_count) mpctx->video_out->check_events();
3072 usec_sleep(20000);
3074 switch (cmd->id) {
3075 case MP_CMD_LOADFILE:
3076 // prepare a tree entry with the new filename
3077 entry = play_tree_new();
3078 play_tree_add_file(entry, cmd->args[0].v.s);
3079 // The entry is added to the main playtree after the switch().
3080 break;
3081 case MP_CMD_LOADLIST:
3082 entry = parse_playlist_file(cmd->args[0].v.s);
3083 break;
3084 case MP_CMD_QUIT:
3085 exit_player_with_rc(EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3086 break;
3087 case MP_CMD_GET_PROPERTY:
3088 case MP_CMD_SET_PROPERTY:
3089 case MP_CMD_STEP_PROPERTY:
3090 run_command(mpctx, cmd);
3091 break;
3094 mp_cmd_free(cmd);
3096 if (entry) { // user entered a command that gave a valid entry
3097 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3098 play_tree_free_list(mpctx->playtree->child, 1);
3099 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3101 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3103 play_tree_set_child(mpctx->playtree, entry);
3105 /* Make iterator start at the top the of tree. */
3106 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mconfig);
3107 if (!mpctx->playtree_iter) continue;
3109 // find the first real item in the tree
3110 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3111 // no items!
3112 play_tree_iter_free(mpctx->playtree_iter);
3113 mpctx->playtree_iter = NULL;
3114 continue; // wait for next command
3116 filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3119 //---------------------------------------------------------------------------
3121 if (mpctx->video_out && vo_config_count)
3122 mpctx->video_out->control(VOCTRL_RESUME, NULL);
3124 if(filename) {
3125 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
3126 filename_recode(filename));
3127 if(use_filename_title && vo_wintitle == NULL)
3128 vo_wintitle = strdup ( mp_basename2 (filename));
3131 if (edl_filename) {
3132 if (edl_records) free_edl(edl_records);
3133 next_edl_record = edl_records = edl_parse_file();
3135 if (edl_output_filename) {
3136 if (edl_fd) fclose(edl_fd);
3137 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3139 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
3140 filename_recode(edl_output_filename));
3144 //==================== Open VOB-Sub ============================
3146 current_module="vobsub";
3147 if (vobsub_name){
3148 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3149 if(vo_vobsub==NULL)
3150 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
3151 filename_recode(vobsub_name));
3152 } else if (sub_auto && filename){
3153 /* try to autodetect vobsub from movie filename ::atmos */
3154 char *buf = strdup(filename), *psub;
3155 char *pdot = strrchr(buf, '.');
3156 char *pslash = strrchr(buf, '/');
3157 #if defined(__MINGW32__) || defined(__CYGWIN__)
3158 if (!pslash) pslash = strrchr(buf, '\\');
3159 #endif
3160 if (pdot && (!pslash || pdot > pslash))
3161 *pdot = '\0';
3162 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3163 /* try from ~/.mplayer/sub */
3164 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3165 char *bname;
3166 int l;
3167 bname = strrchr(buf,'/');
3168 #if defined(__MINGW32__) || defined(__CYGWIN__)
3169 if(!bname) bname = strrchr(buf,'\\');
3170 #endif
3171 if(bname) bname++;
3172 else bname = buf;
3173 l = strlen(psub) + strlen(bname) + 1;
3174 psub = realloc(psub,l);
3175 strcat(psub,bname);
3176 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3177 free(psub);
3179 free(buf);
3181 if(vo_vobsub){
3182 initialized_flags|=INITIALIZED_VOBSUB;
3183 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3184 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3186 // setup global sub numbering
3187 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3188 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3191 //============ Open & Sync STREAM --- fork cache2 ====================
3193 mpctx->stream=NULL;
3194 mpctx->demuxer=NULL;
3195 if (mpctx->d_audio) {
3196 //free_demuxer_stream(mpctx->d_audio);
3197 mpctx->d_audio=NULL;
3199 if (mpctx->d_video) {
3200 //free_demuxer_stream(d_video);
3201 mpctx->d_video=NULL;
3203 mpctx->sh_audio=NULL;
3204 mpctx->sh_video=NULL;
3206 current_module="open_stream";
3207 mpctx->stream=open_stream(filename,0,&mpctx->file_format);
3208 if(!mpctx->stream) { // error...
3209 mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY);
3210 goto goto_next_file;
3212 initialized_flags|=INITIALIZED_STREAM;
3214 #ifdef CONFIG_GUI
3215 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3216 #endif
3218 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3219 play_tree_t* entry;
3220 // Handle playlist
3221 current_module="handle_playlist";
3222 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3223 filename_recode(filename));
3224 entry = parse_playtree(mpctx->stream,0);
3225 mpctx->eof=playtree_add_playlist(entry);
3226 goto goto_next_file;
3228 mpctx->stream->start_pos+=seek_to_byte;
3230 if(stream_dump_type==5){
3231 unsigned char buf[4096];
3232 int len;
3233 FILE *f;
3234 current_module="dumpstream";
3235 stream_reset(mpctx->stream);
3236 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3237 f=fopen(stream_dump_name,"wb");
3238 if(!f){
3239 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3240 exit_player(EXIT_ERROR);
3242 if (dvd_chapter > 1) {
3243 int chapter = dvd_chapter - 1;
3244 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3246 while(!mpctx->stream->eof && !async_quit_request){
3247 len=stream_read(mpctx->stream,buf,4096);
3248 if(len>0) {
3249 if(fwrite(buf,len,1,f) != 1) {
3250 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3251 exit_player(EXIT_ERROR);
3254 if(dvd_last_chapter > 0) {
3255 int chapter = -1;
3256 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3257 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3258 break;
3261 if(fclose(f)) {
3262 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3263 exit_player(EXIT_ERROR);
3265 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3266 exit_player_with_rc(EXIT_EOF, 0);
3269 #ifdef CONFIG_DVDREAD
3270 if(mpctx->stream->type==STREAMTYPE_DVD){
3271 current_module="dvd lang->id";
3272 if(audio_id==-1) audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3273 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3274 // setup global sub numbering
3275 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3276 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3277 current_module=NULL;
3279 #endif
3281 #ifdef CONFIG_DVDNAV
3282 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3283 current_module="dvdnav lang->id";
3284 if(audio_id==-1) audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3285 dvdsub_lang_id = -3;
3286 if(dvdsub_lang && dvdsub_id==-1)
3287 dvdsub_lang_id=dvdsub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3288 // setup global sub numbering
3289 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3290 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3291 current_module=NULL;
3293 #endif
3295 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3296 goto_enable_cache:
3297 if(stream_cache_size>0){
3298 int res;
3299 current_module="enable_cache";
3300 res = stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3301 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3302 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0));
3303 if(res == 0)
3304 if((mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY))) goto goto_next_file;
3307 //============ Open DEMUXERS --- DETECT file type =======================
3308 current_module="demux_open";
3310 mpctx->demuxer=demux_open(mpctx->stream,mpctx->file_format,audio_id,video_id,dvdsub_id,filename);
3312 // HACK to get MOV Reference Files working
3314 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3316 unsigned char* playlist_entry;
3317 play_tree_t *list = NULL, *entry = NULL;
3319 current_module="handle_demux_playlist";
3320 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3322 char *temp, *bname;
3324 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3325 filename_recode(playlist_entry));
3327 bname=mp_basename(playlist_entry);
3328 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3329 continue;
3331 if (!strcmp(playlist_entry,filename)) // ignoring self-reference
3332 continue;
3334 entry = play_tree_new();
3336 if (filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3338 temp=malloc((strlen(filename)-strlen(mp_basename(filename))+strlen(playlist_entry)+1));
3339 if (temp)
3341 strncpy(temp, filename, strlen(filename)-strlen(mp_basename(filename)));
3342 temp[strlen(filename)-strlen(mp_basename(filename))]='\0';
3343 strcat(temp, playlist_entry);
3344 if (!strcmp(temp, filename)) {
3345 free(temp);
3346 continue;
3348 play_tree_add_file(entry,temp);
3349 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3350 free(temp);
3353 else
3354 play_tree_add_file(entry,playlist_entry);
3356 if(!list)
3357 list = entry;
3358 else
3359 play_tree_append_entry(list,entry);
3361 free_demuxer(mpctx->demuxer);
3362 mpctx->demuxer = NULL;
3364 if (list)
3366 entry = play_tree_new();
3367 play_tree_set_child(entry,list);
3368 mpctx->eof=playtree_add_playlist(entry);
3369 goto goto_next_file;
3373 if(!mpctx->demuxer)
3374 goto goto_next_file;
3375 if(dvd_chapter>1) {
3376 float pts;
3377 if (demuxer_seek_chapter(mpctx->demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
3378 seek(mpctx, pts, SEEK_ABSOLUTE);
3381 initialized_flags|=INITIALIZED_DEMUXER;
3383 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3384 int i;
3385 int maxid = -1;
3386 // setup global sub numbering
3387 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3388 for (i = 0; i < MAX_S_STREAMS; i++)
3389 if (mpctx->demuxer->s_streams[i])
3390 maxid = FFMAX(maxid, ((sh_sub_t *)mpctx->demuxer->s_streams[i])->sid);
3391 mpctx->global_sub_size += maxid + 1;
3393 // Make dvdsub_id always selectable if set.
3394 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id)
3395 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id + 1;
3397 #ifdef CONFIG_ASS
3398 if (ass_enabled && ass_library) {
3399 for (i = 0; i < mpctx->demuxer->num_attachments; ++i) {
3400 demux_attachment_t* att = mpctx->demuxer->attachments + i;
3401 if (extract_embedded_fonts &&
3402 att->name && att->type && att->data && att->data_size &&
3403 (strcmp(att->type, "application/x-truetype-font") == 0 ||
3404 strcmp(att->type, "application/x-font") == 0))
3405 ass_add_font(ass_library, att->name, att->data, att->data_size);
3408 #endif
3410 current_module="demux_open2";
3412 //file_format=demuxer->file_format;
3414 mpctx->d_audio=mpctx->demuxer->audio;
3415 mpctx->d_video=mpctx->demuxer->video;
3416 mpctx->d_sub=mpctx->demuxer->sub;
3418 if (ts_prog) {
3419 int tmp = ts_prog;
3420 mp_property_do("switch_program", M_PROPERTY_SET, &tmp, mpctx);
3422 // select audio stream
3423 select_audio(mpctx->demuxer, audio_id, audio_lang);
3425 // DUMP STREAMS:
3426 if((stream_dump_type)&&(stream_dump_type<4)){
3427 FILE *f;
3428 demux_stream_t *ds=NULL;
3429 current_module="dump";
3430 // select stream to dump
3431 switch(stream_dump_type){
3432 case 1: ds=mpctx->d_audio;break;
3433 case 2: ds=mpctx->d_video;break;
3434 case 3: ds=mpctx->d_sub;break;
3436 if(!ds){
3437 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3438 exit_player(EXIT_ERROR);
3440 // disable other streams:
3441 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3442 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3443 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3444 // let's dump it!
3445 f=fopen(stream_dump_name,"wb");
3446 if(!f){
3447 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3448 exit_player(EXIT_ERROR);
3450 while(!ds->eof){
3451 unsigned char* start;
3452 int in_size=ds_get_packet(ds,&start);
3453 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3454 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3455 if(in_size>0) fwrite(start,in_size,1,f);
3456 if(dvd_last_chapter>0) {
3457 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3458 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3459 break;
3462 fclose(f);
3463 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3464 exit_player_with_rc(EXIT_EOF, 0);
3467 mpctx->sh_audio=mpctx->d_audio->sh;
3468 mpctx->sh_video=mpctx->d_video->sh;
3470 if(mpctx->sh_video){
3472 current_module="video_read_properties";
3473 if(!video_read_properties(mpctx->sh_video)) {
3474 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3475 mpctx->sh_video=mpctx->d_video->sh=NULL;
3476 } else {
3477 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_FilefmtFourccSizeFpsFtime,
3478 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3479 mpctx->sh_video->fps,mpctx->sh_video->frametime
3482 /* need to set fps here for output encoders to pick it up in their init */
3483 if(force_fps){
3484 mpctx->sh_video->fps=force_fps;
3485 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3487 vo_fps = mpctx->sh_video->fps;
3489 if(!mpctx->sh_video->fps && !force_fps){
3490 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3491 mpctx->sh_video=mpctx->d_video->sh=NULL;
3497 if(!mpctx->sh_video && !mpctx->sh_audio){
3498 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3499 #ifdef CONFIG_DVBIN
3500 if(mpctx->stream->type == STREAMTYPE_DVB)
3502 int dir;
3503 int v = mpctx->last_dvb_step;
3504 if(v > 0)
3505 dir = DVB_CHANNEL_HIGHER;
3506 else
3507 dir = DVB_CHANNEL_LOWER;
3509 if(dvb_step_channel(mpctx->stream, dir))
3510 mpctx->eof = mpctx->dvbin_reopen = 1;
3512 #endif
3513 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3516 /* display clip info */
3517 demux_info_print(mpctx->demuxer);
3519 //================== Read SUBTITLES (DVD & TEXT) ==========================
3520 if(vo_spudec==NULL &&
3521 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3522 init_vo_spudec();
3525 if(1 || mpctx->sh_video) {
3526 // after reading video params we should load subtitles because
3527 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3528 // check .sub
3529 double fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
3530 current_module="read_subtitles_file";
3531 if(sub_name){
3532 for (i = 0; sub_name[i] != NULL; ++i)
3533 add_subtitles (sub_name[i], fps, 0);
3535 if(sub_auto) { // auto load sub file ...
3536 char *psub = get_path( "sub/" );
3537 char **tmp = sub_filenames((psub ? psub : ""), filename);
3538 int i = 0;
3539 free(psub); // release the buffer created by get_path() above
3540 while (tmp[i]) {
3541 add_subtitles (tmp[i], fps, 1);
3542 free(tmp[i++]);
3544 free(tmp);
3546 if (mpctx->set_of_sub_size > 0) {
3547 // setup global sub numbering
3548 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3549 mpctx->global_sub_size += mpctx->set_of_sub_size;
3553 if (mpctx->global_sub_size) {
3554 select_subtitle(mpctx);
3555 if(subdata)
3556 switch (stream_dump_type) {
3557 case 3: list_sub_file(subdata); break;
3558 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3559 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3560 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3561 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3562 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3566 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3567 filename_recode(filename));
3568 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3569 if (mpctx->sh_video) {
3570 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3571 if (mpctx->sh_video->format >= 0x20202020)
3572 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3573 else
3574 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3575 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3576 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3577 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3578 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3579 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3581 if (mpctx->sh_audio) {
3582 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3583 if (mpctx->sh_audio->format >= 0x20202020)
3584 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3585 else
3586 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3587 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3588 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3589 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3591 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3592 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n",
3593 mpctx->stream->seek && (!mpctx->demuxer || mpctx->demuxer->seekable));
3594 if (mpctx->demuxer) {
3595 if (mpctx->demuxer->num_chapters == 0)
3596 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3597 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3600 if(!mpctx->sh_video) goto main; // audio-only
3602 if(!reinit_video_chain()) {
3603 if(!mpctx->sh_video){
3604 if(!mpctx->sh_audio) goto goto_next_file;
3605 goto main; // exit_player(MSGTR_Exit_error);
3609 if(vo_flags & 0x08 && vo_spudec)
3610 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3612 #ifdef CONFIG_FREETYPE
3613 force_load_font = 1;
3614 #endif
3616 //================== MAIN: ==========================
3617 main:
3618 current_module="main";
3620 if(playing_msg) {
3621 char* msg = property_expand_string(mpctx, playing_msg);
3622 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3623 free(msg);
3627 // Disable the term OSD in verbose mode
3628 if(verbose) term_osd = 0;
3631 //int frame_corr_num=0; //
3632 //float v_frame=0; // Video
3633 //float num_frames=0; // number of frames played
3635 int frame_time_remaining=0; // flag
3636 int blit_frame=0;
3637 mpctx->num_buffered_frames=0;
3639 // Make sure old OSD does not stay around,
3640 // e.g. with -fixed-vo and same-resolution files
3641 clear_osd_msgs();
3642 update_osd_msg();
3644 //================ SETUP AUDIO ==========================
3646 if(mpctx->sh_audio){
3647 reinit_audio_chain();
3648 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3649 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3652 current_module="av_init";
3654 if(mpctx->sh_video){
3655 mpctx->sh_video->timer=0;
3656 if (! ignore_start)
3657 audio_delay += mpctx->sh_video->stream_delay;
3659 if(mpctx->sh_audio){
3660 if (start_volume >= 0)
3661 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3662 if (! ignore_start)
3663 audio_delay -= mpctx->sh_audio->stream_delay;
3664 mpctx->delay=-audio_delay;
3667 if(!mpctx->sh_audio){
3668 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3669 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3670 ds_free_packs(mpctx->d_audio); // free buffered chunks
3671 //mpctx->d_audio->id=-2; // do not read audio chunks
3672 //uninit_player(INITIALIZED_AO); // close device
3674 if(!mpctx->sh_video){
3675 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3676 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3677 ds_free_packs(mpctx->d_video);
3678 mpctx->d_video->id=-2;
3679 //if(!fixed_vo) uninit_player(INITIALIZED_VO);
3682 if (!mpctx->sh_video && !mpctx->sh_audio)
3683 goto goto_next_file;
3685 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3686 if(force_fps && mpctx->sh_video){
3687 vo_fps = mpctx->sh_video->fps=force_fps;
3688 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3689 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3692 #ifdef CONFIG_GUI
3693 if ( use_gui ) {
3694 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3695 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3696 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3697 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3698 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3700 #endif
3702 mp_input_set_section(NULL);
3703 //TODO: add desired (stream-based) sections here
3704 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section("tv");
3705 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section("dvdnav");
3707 //==================== START PLAYING =======================
3709 if(mpctx->loop_times>1) mpctx->loop_times--; else
3710 if(mpctx->loop_times==1) mpctx->loop_times = -1;
3712 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3714 total_time_usage_start=GetTimer();
3715 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3716 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3717 play_n_frames=play_n_frames_mf;
3718 mpctx->startup_decode_retry = DEFAULT_STARTUP_DECODE_RETRY;
3720 if(play_n_frames==0){
3721 mpctx->eof=PT_NEXT_ENTRY; goto goto_next_file;
3724 if (seek_to_sec) {
3725 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3726 end_at.pos += seek_to_sec;
3729 if (end_at.type == END_AT_SIZE) {
3730 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3731 end_at.type = END_AT_NONE;
3734 #ifdef CONFIG_DVDNAV
3735 mp_dvdnav_context_free(mpctx);
3736 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3737 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3738 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3740 #endif
3742 while(!mpctx->eof){
3743 float aq_sleep_time=0;
3745 if(dvd_last_chapter>0) {
3746 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3747 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3748 goto goto_next_file;
3751 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3752 mpctx->sh_audio = mpctx->d_audio->sh;
3753 mpctx->sh_audio->ds = mpctx->d_audio;
3754 reinit_audio_chain();
3757 /*========================== PLAY AUDIO ============================*/
3759 if (mpctx->sh_audio)
3760 if (!fill_audio_out_buffers())
3761 // at eof, all audio at least written to ao
3762 if (!mpctx->sh_video)
3763 mpctx->eof = PT_NEXT_ENTRY;
3766 if(!mpctx->sh_video) {
3767 // handle audio-only case:
3768 double a_pos=0;
3769 // sh_audio can be NULL due to video stream switching
3770 // TODO: handle this better
3771 if((!quiet || end_at.type == END_AT_TIME) && mpctx->sh_audio)
3772 a_pos = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3774 if(!quiet)
3775 print_status(a_pos, 0, 0);
3777 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3778 mpctx->eof = PT_NEXT_ENTRY;
3779 update_subtitles(NULL, a_pos, mpctx->d_sub, 0);
3780 update_osd_msg();
3782 } else {
3784 /*========================== PLAY VIDEO ============================*/
3786 vo_pts=mpctx->sh_video->timer*90000.0;
3787 vo_fps=mpctx->sh_video->fps;
3789 if (!mpctx->num_buffered_frames) {
3790 double frame_time = update_video(&blit_frame);
3791 while (!blit_frame && mpctx->startup_decode_retry > 0) {
3792 double delay = mpctx->delay;
3793 // these initial decode failures are probably due to codec delay,
3794 // ignore them and also their probably nonsense durations
3795 update_video(&blit_frame);
3796 mpctx->delay = delay;
3797 mpctx->startup_decode_retry--;
3799 mpctx->startup_decode_retry = 0;
3800 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3801 if (mpctx->sh_video->vf_initialized < 0) {
3802 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3803 mpctx->eof = 1; goto goto_next_file;
3805 if (frame_time < 0)
3806 mpctx->eof = 1;
3807 else {
3808 // might return with !eof && !blit_frame if !correct_pts
3809 mpctx->num_buffered_frames += blit_frame;
3810 mpctx->time_frame += frame_time / playback_speed; // for nosound
3814 // ==========================================================================
3816 // current_module="draw_osd";
3817 // if(vo_config_count) mpctx->video_out->draw_osd();
3819 #ifdef CONFIG_GUI
3820 if(use_gui) guiEventHandling();
3821 #endif
3823 current_module="vo_check_events";
3824 if (vo_config_count) mpctx->video_out->check_events();
3826 #ifdef CONFIG_X11
3827 if (stop_xscreensaver) {
3828 current_module = "stop_xscreensaver";
3829 xscreensaver_heartbeat();
3831 #endif
3832 if (heartbeat_cmd) {
3833 static unsigned last_heartbeat;
3834 unsigned now = GetTimerMS();
3835 if (now - last_heartbeat > 30000) {
3836 last_heartbeat = now;
3837 system(heartbeat_cmd);
3841 frame_time_remaining = sleep_until_update(&mpctx->time_frame, &aq_sleep_time);
3843 //====================== FLIP PAGE (VIDEO BLT): =========================
3845 current_module="flip_page";
3846 if (!frame_time_remaining && blit_frame) {
3847 unsigned int t2=GetTimer();
3849 if(vo_config_count) mpctx->video_out->flip_page();
3850 mpctx->num_buffered_frames--;
3852 vout_time_usage += (GetTimer() - t2) * 0.000001;
3854 //====================== A-V TIMESTAMP CORRECTION: =========================
3856 adjust_sync_and_print_status(frame_time_remaining, mpctx->time_frame);
3858 //============================ Auto QUALITY ============================
3860 /*Output quality adjustments:*/
3861 if(auto_quality>0){
3862 current_module="autoq";
3863 // float total=0.000001f * (GetTimer()-aq_total_time);
3864 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3865 if(output_quality<auto_quality && aq_sleep_time>0)
3866 ++output_quality;
3867 else
3868 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3869 if(output_quality>1 && aq_sleep_time<0)
3870 --output_quality;
3871 else
3872 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3873 output_quality=0;
3874 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3875 set_video_quality(mpctx->sh_video,output_quality);
3878 if (play_n_frames >= 0 && !frame_time_remaining && blit_frame) {
3879 --play_n_frames;
3880 if (play_n_frames <= 0) mpctx->eof = PT_NEXT_ENTRY;
3884 // FIXME: add size based support for -endpos
3885 if (end_at.type == END_AT_TIME &&
3886 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3887 mpctx->eof = PT_NEXT_ENTRY;
3889 } // end if(mpctx->sh_video)
3891 #ifdef CONFIG_DVDNAV
3892 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3893 nav_highlight_t hl;
3894 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3895 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3896 vo_osd_changed (OSDTYPE_DVDNAV);
3898 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
3899 double ar = -1.0;
3900 if (stream_control (mpctx->demuxer->stream,
3901 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
3902 != STREAM_UNSUPPORTED)
3903 mpctx->sh_video->stream_aspect = ar;
3906 #endif
3908 //============================ Handle PAUSE ===============================
3910 current_module="pause";
3912 if (mpctx->osd_function == OSD_PAUSE) {
3913 mpctx->was_paused = 1;
3914 pause_loop();
3917 // handle -sstep
3918 if(step_sec>0) {
3919 mpctx->osd_function=OSD_FFW;
3920 rel_seek_secs+=step_sec;
3923 edl_update(mpctx);
3925 //================= Keyboard events, SEEKing ====================
3927 current_module="key_events";
3930 mp_cmd_t* cmd;
3931 int brk_cmd = 0;
3932 while( !brk_cmd && (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
3933 brk_cmd = run_command(mpctx, cmd);
3934 mp_cmd_free(cmd);
3935 if (brk_cmd == 2)
3936 goto goto_enable_cache;
3939 mpctx->was_paused = 0;
3941 /* Looping. */
3942 if(mpctx->eof==1 && mpctx->loop_times>=0) {
3943 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d, eof = %d\n", mpctx->loop_times,mpctx->eof);
3945 if(mpctx->loop_times>1) mpctx->loop_times--; else
3946 if(mpctx->loop_times==1) mpctx->loop_times=-1;
3947 play_n_frames=play_n_frames_mf;
3948 mpctx->eof=0;
3949 abs_seek_pos=SEEK_ABSOLUTE; rel_seek_secs=seek_to_sec;
3950 loop_seek = 1;
3953 if(rel_seek_secs || abs_seek_pos){
3954 if (seek(mpctx, rel_seek_secs, abs_seek_pos) >= 0) {
3955 // Set OSD:
3956 if(!loop_seek){
3957 if( !edl_decision )
3958 set_osd_bar(0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3962 rel_seek_secs=0;
3963 abs_seek_pos=0;
3964 loop_seek=0;
3965 edl_decision = 0;
3968 #ifdef CONFIG_GUI
3969 if(use_gui){
3970 guiEventHandling();
3971 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3972 // get pos from frame number / total frames
3973 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3974 } else {
3975 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3977 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3978 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3979 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3980 guiGetEvent( guiReDraw,NULL );
3981 guiGetEvent( guiSetVolume,NULL );
3982 if(guiIntfStruct.Playing==0) break; // STOP
3983 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3984 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3985 #ifdef CONFIG_DVDREAD
3986 if ( mpctx->stream->type == STREAMTYPE_DVD )
3988 dvd_priv_t * dvdp = mpctx->stream->priv;
3989 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3991 #endif
3993 #endif /* CONFIG_GUI */
3995 } // while(!mpctx->eof)
3997 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->eof);
3999 #ifdef CONFIG_DVBIN
4000 if(mpctx->dvbin_reopen)
4002 mpctx->eof = 0;
4003 uninit_player(INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_INPUT|INITIALIZED_GETCH2|(fixed_vo?INITIALIZED_VO:0)));
4004 cache_uninit(mpctx->stream);
4005 mpctx->dvbin_reopen = 0;
4006 goto goto_enable_cache;
4008 #endif
4011 goto_next_file: // don't jump here after ao/vo/getch initialization!
4013 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4015 if(benchmark){
4016 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4017 double total_time_usage;
4018 total_time_usage_start=GetTimer()-total_time_usage_start;
4019 total_time_usage = (float)total_time_usage_start*0.000001;
4020 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4021 video_time_usage,vout_time_usage,audio_time_usage,
4022 total_time_usage-tot,total_time_usage);
4023 if(total_time_usage>0.0)
4024 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4025 100.0*video_time_usage/total_time_usage,
4026 100.0*vout_time_usage/total_time_usage,
4027 100.0*audio_time_usage/total_time_usage,
4028 100.0*(total_time_usage-tot)/total_time_usage,
4029 100.0);
4030 if(total_frame_cnt && frame_dropping)
4031 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4032 total_frame_cnt-drop_frame_cnt,
4033 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4034 drop_frame_cnt,
4035 100*drop_frame_cnt/total_frame_cnt,
4036 total_frame_cnt,
4037 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4040 // time to uninit all, except global stuff:
4041 uninit_player(INITIALIZED_ALL-(INITIALIZED_GUI+INITIALIZED_INPUT+(fixed_vo?INITIALIZED_VO:0)));
4043 if(mpctx->set_of_sub_size > 0) {
4044 current_module="sub_free";
4045 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4046 sub_free(mpctx->set_of_subtitles[i]);
4047 #ifdef CONFIG_ASS
4048 if(mpctx->set_of_ass_tracks[i])
4049 ass_free_track( mpctx->set_of_ass_tracks[i] );
4050 #endif
4052 mpctx->set_of_sub_size = 0;
4054 vo_sub_last = vo_sub=NULL;
4055 subdata=NULL;
4056 #ifdef CONFIG_ASS
4057 ass_track = NULL;
4058 if(ass_library)
4059 ass_clear_fonts(ass_library);
4060 #endif
4062 if(mpctx->eof == PT_NEXT_ENTRY || mpctx->eof == PT_PREV_ENTRY) {
4063 mpctx->eof = mpctx->eof == PT_NEXT_ENTRY ? 1 : -1;
4064 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) == PLAY_TREE_ITER_ENTRY) {
4065 mpctx->eof = 1;
4066 } else {
4067 play_tree_iter_free(mpctx->playtree_iter);
4068 mpctx->playtree_iter = NULL;
4070 mpctx->play_tree_step = 1;
4071 } else if(mpctx->eof == PT_UP_NEXT || mpctx->eof == PT_UP_PREV) {
4072 mpctx->eof = mpctx->eof == PT_UP_NEXT ? 1 : -1;
4073 if(mpctx->playtree_iter) {
4074 if(play_tree_iter_up_step(mpctx->playtree_iter,mpctx->eof,0) == PLAY_TREE_ITER_ENTRY) {
4075 mpctx->eof = 1;
4076 } else {
4077 play_tree_iter_free(mpctx->playtree_iter);
4078 mpctx->playtree_iter = NULL;
4081 } else if (mpctx->eof == PT_STOP) {
4082 play_tree_iter_free(mpctx->playtree_iter);
4083 mpctx->playtree_iter = NULL;
4084 } else { // NEXT PREV SRC
4085 mpctx->eof = mpctx->eof == PT_PREV_SRC ? -1 : 1;
4088 if(mpctx->eof == 0) mpctx->eof = 1;
4090 while(mpctx->playtree_iter != NULL) {
4091 filename = play_tree_iter_get_file(mpctx->playtree_iter,mpctx->eof);
4092 if(filename == NULL) {
4093 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->eof,0) != PLAY_TREE_ITER_ENTRY) {
4094 play_tree_iter_free(mpctx->playtree_iter);
4095 mpctx->playtree_iter = NULL;
4097 } else
4098 break;
4101 #ifdef CONFIG_GUI
4102 if(use_gui && !mpctx->playtree_iter) {
4103 #ifdef CONFIG_DVDREAD
4104 if(!guiIntfStruct.DiskChanged)
4105 #endif
4106 mplEnd();
4108 #endif
4110 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4111 if(!mpctx->playtree_iter) filename = NULL;
4112 mpctx->eof = 0;
4113 goto play_next_file;
4117 exit_player_with_rc(EXIT_EOF, 0);
4119 return 1;
4121 #endif /* DISABLE_MAIN */