Use double instead of float for pts.
[mplayer/glamo.git] / mplayer.c
blobda0ebb5671d81ab7e08ce358c97e1f9509035a68
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "config.h"
9 #if defined(__MINGW32__) || defined(__CYGWIN__)
10 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
11 #include <windows.h>
12 #endif
13 #include <string.h>
14 #include <unistd.h>
16 // #include <sys/mman.h>
17 #include <sys/types.h>
18 #ifndef __MINGW32__
19 #include <sys/ioctl.h>
20 #include <sys/wait.h>
21 #else
22 #define SIGHUP 1 /* hangup */
23 #define SIGQUIT 3 /* quit */
24 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
25 #define SIGBUS 10 /* bus error */
26 #define SIGPIPE 13 /* broken pipe */
27 #endif
29 #include <sys/time.h>
30 #include <sys/stat.h>
32 #include <signal.h>
33 #include <time.h>
34 #include <fcntl.h>
35 #include <limits.h>
37 #include <errno.h>
39 #include "mp_msg.h"
41 #define HELP_MP_DEFINE_STATIC
42 #include "help_mp.h"
44 #include "m_option.h"
45 #include "m_config.h"
46 #include "m_property.h"
48 #include "cfg-mplayer-def.h"
50 #include "libavutil/intreadwrite.h"
51 #include "libavutil/avstring.h"
53 #include "subreader.h"
55 #include "libvo/video_out.h"
57 #include "libvo/font_load.h"
58 #include "libvo/sub.h"
60 #ifdef CONFIG_X11
61 #include "libvo/x11_common.h"
62 #endif
64 #include "libao2/audio_out.h"
66 #include "codec-cfg.h"
68 #include "edl.h"
70 #include "spudec.h"
71 #include "vobsub.h"
73 #include "osdep/getch2.h"
74 #include "osdep/timer.h"
76 #include "gui/interface.h"
78 #include "input/input.h"
80 int slave_mode=0;
81 int player_idle_mode=0;
82 int quiet=0;
83 int enable_mouse_movements=0;
84 float start_volume = -1;
86 #include "osdep/priority.h"
88 char *heartbeat_cmd;
90 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
92 #ifdef HAVE_RTC
93 #ifdef __linux__
94 #include <linux/rtc.h>
95 #else
96 #include <rtc.h>
97 #define RTC_IRQP_SET RTCIO_IRQP_SET
98 #define RTC_PIE_ON RTCIO_PIE_ON
99 #endif /* __linux__ */
100 #endif /* HAVE_RTC */
102 #include "stream/tv.h"
103 #include "stream/stream_radio.h"
104 #ifdef CONFIG_DVBIN
105 #include "stream/dvbin.h"
106 #endif
107 #include "stream/cache2.h"
109 //**************************************************************************//
110 // Playtree
111 //**************************************************************************//
112 #include "playtree.h"
113 #include "playtreeparser.h"
115 int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
116 int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
118 //**************************************************************************//
119 // Config
120 //**************************************************************************//
121 #include "parser-cfg.h"
122 #include "parser-mpcmd.h"
124 m_config_t* mconfig;
126 //**************************************************************************//
127 // Config file
128 //**************************************************************************//
130 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
132 static int cfg_include(m_option_t *conf, char *filename){
133 return m_config_parse_config_file(mconfig, filename);
136 #include "get_path.h"
138 //**************************************************************************//
139 // XScreensaver
140 //**************************************************************************//
142 void xscreensaver_heartbeat(void);
144 //**************************************************************************//
145 //**************************************************************************//
146 // Input media streaming & demultiplexer:
147 //**************************************************************************//
149 static int max_framesize=0;
151 #include "stream/stream.h"
152 #include "libmpdemux/demuxer.h"
153 #include "libmpdemux/stheader.h"
155 #ifdef CONFIG_DVDREAD
156 #include "stream/stream_dvd.h"
157 #endif
158 #include "stream/stream_dvdnav.h"
160 #include "libmpcodecs/dec_audio.h"
161 #include "libmpcodecs/dec_video.h"
162 #include "libmpcodecs/mp_image.h"
163 #include "libmpcodecs/vf.h"
164 #include "libmpcodecs/vd.h"
166 #include "mixer.h"
168 #include "mp_core.h"
170 //**************************************************************************//
171 //**************************************************************************//
173 // Common FIFO functions, and keyboard/event FIFO code
174 #include "mp_fifo.h"
175 int noconsolecontrols=0;
176 //**************************************************************************//
178 // Not all functions in mplayer.c take the context as an argument yet
179 static MPContext mpctx_s = {
180 .osd_function = OSD_PLAY,
181 .begin_skip = MP_NOPTS_VALUE,
182 .play_tree_step = 1,
183 .global_sub_pos = -1,
184 .set_of_sub_pos = -1,
185 .file_format = DEMUXER_TYPE_UNKNOWN,
186 .loop_times = -1,
187 #ifdef CONFIG_DVBIN
188 .last_dvb_step = 1,
189 #endif
192 static MPContext *mpctx = &mpctx_s;
194 int fixed_vo=0;
196 // benchmark:
197 double video_time_usage=0;
198 double vout_time_usage=0;
199 static double audio_time_usage=0;
200 static int total_time_usage_start=0;
201 static int total_frame_cnt=0;
202 static int drop_frame_cnt=0; // total number of dropped frames
203 int benchmark=0;
205 // options:
206 #define DEFAULT_STARTUP_DECODE_RETRY 8
207 int auto_quality=0;
208 static int output_quality=0;
210 float playback_speed=1.0;
212 int use_gui=0;
214 #ifdef CONFIG_GUI
215 int enqueue=0;
216 #endif
218 static int list_properties = 0;
220 int osd_level=1;
221 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
222 unsigned int osd_visible;
223 int osd_duration = 1000;
225 int term_osd = 1;
226 static char* term_osd_esc = "\x1b[A\r\x1b[K";
227 static char* playing_msg = NULL;
228 // seek:
229 static double seek_to_sec;
230 static off_t seek_to_byte=0;
231 static off_t step_sec=0;
232 static int loop_seek=0;
234 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
236 // A/V sync:
237 int autosync=0; // 30 might be a good default value.
239 // may be changed by GUI: (FIXME!)
240 float rel_seek_secs=0;
241 int abs_seek_pos=0;
243 // codecs:
244 char **audio_codec_list=NULL; // override audio codec
245 char **video_codec_list=NULL; // override video codec
246 char **audio_fm_list=NULL; // override audio codec family
247 char **video_fm_list=NULL; // override video codec family
249 // demuxer:
250 extern char *demuxer_name; // override demuxer
251 extern char *audio_demuxer_name; // override audio demuxer
252 extern char *sub_demuxer_name; // override sub demuxer
254 // streaming:
255 int audio_id=-1;
256 int video_id=-1;
257 int dvdsub_id=-1;
258 // this dvdsub_id was selected via slang
259 // use this to allow dvdnav to follow -slang across stream resets,
260 // in particular the subtitle ID for a language changes
261 int dvdsub_lang_id;
262 int vobsub_id=-1;
263 char* audio_lang=NULL;
264 char* dvdsub_lang=NULL;
265 static char* spudec_ifo=NULL;
266 char* filename=NULL; //"MI2-Trailer.avi";
267 int forced_subs_only=0;
268 int file_filter=1;
270 // cache2:
271 int stream_cache_size=-1;
272 #ifdef CONFIG_STREAM_CACHE
273 extern int cache_fill_status;
275 float stream_cache_min_percent=20.0;
276 float stream_cache_seek_min_percent=50.0;
277 #else
278 #define cache_fill_status 0
279 #endif
281 // dump:
282 static char *stream_dump_name="stream.dump";
283 int stream_dump_type=0;
285 // A-V sync:
286 static float default_max_pts_correction=-1;//0.01f;
287 static float max_pts_correction=0;//default_max_pts_correction;
288 static float c_total=0;
289 float audio_delay=0;
290 static int ignore_start=0;
292 static int softsleep=0;
294 double force_fps=0;
295 static int force_srate=0;
296 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
297 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
298 static int play_n_frames=-1;
299 static int play_n_frames_mf=-1;
301 // screen info:
302 char** video_driver_list=NULL;
303 char** audio_driver_list=NULL;
305 // sub:
306 char *font_name=NULL;
307 char *sub_font_name=NULL;
308 extern int font_fontconfig;
309 float font_factor=0.75;
310 char **sub_name=NULL;
311 float sub_delay=0;
312 float sub_fps=0;
313 int sub_auto = 1;
314 char *vobsub_name=NULL;
315 /*DSP!!char *dsp=NULL;*/
316 int subcc_enabled=0;
317 int suboverlap_enabled = 1;
319 #include "libass/ass_mp.h"
321 char* current_module=NULL; // for debugging
324 // ---
326 #ifdef CONFIG_MENU
327 #include "m_struct.h"
328 #include "libmenu/menu.h"
329 void vf_menu_pause_update(struct vf_instance_s* vf);
330 extern vf_info_t vf_info_menu;
331 static vf_info_t* libmenu_vfs[] = {
332 &vf_info_menu,
333 NULL
335 static vf_instance_t* vf_menu = NULL;
336 int use_menu = 0;
337 static char* menu_cfg = NULL;
338 static char* menu_root = "main";
339 #endif
342 #ifdef HAVE_RTC
343 static int nortc = 1;
344 static char* rtc_device;
345 #endif
347 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
348 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
349 short edl_decision = 0; ///< 1 when an EDL operation has been made.
350 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
351 int use_filedir_conf;
352 int use_filename_title;
354 static unsigned int initialized_flags=0;
355 #include "mpcommon.h"
356 #include "command.h"
358 #include "metadata.h"
360 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
362 const void *mpctx_get_video_out(MPContext *mpctx)
364 return mpctx->video_out;
367 const void *mpctx_get_audio_out(MPContext *mpctx)
369 return mpctx->audio_out;
372 void *mpctx_get_demuxer(MPContext *mpctx)
374 return mpctx->demuxer;
377 void *mpctx_get_playtree_iter(MPContext *mpctx)
379 return mpctx->playtree_iter;
382 void *mpctx_get_mixer(MPContext *mpctx)
384 return &mpctx->mixer;
387 int mpctx_get_global_sub_size(MPContext *mpctx)
389 return mpctx->global_sub_size;
392 int mpctx_get_osd_function(MPContext *mpctx)
394 return mpctx->osd_function;
397 static int is_valid_metadata_type (metadata_t type) {
398 switch (type)
400 /* check for valid video stream */
401 case META_VIDEO_CODEC:
402 case META_VIDEO_BITRATE:
403 case META_VIDEO_RESOLUTION:
405 if (!mpctx->sh_video)
406 return 0;
407 break;
410 /* check for valid audio stream */
411 case META_AUDIO_CODEC:
412 case META_AUDIO_BITRATE:
413 case META_AUDIO_SAMPLES:
415 if (!mpctx->sh_audio)
416 return 0;
417 break;
420 /* check for valid demuxer */
421 case META_INFO_TITLE:
422 case META_INFO_ARTIST:
423 case META_INFO_ALBUM:
424 case META_INFO_YEAR:
425 case META_INFO_COMMENT:
426 case META_INFO_TRACK:
427 case META_INFO_GENRE:
429 if (!mpctx->demuxer)
430 return 0;
431 break;
434 default:
435 break;
438 return 1;
441 static char *get_demuxer_info (char *tag) {
442 char **info = mpctx->demuxer->info;
443 int n;
445 if (!info || !tag)
446 return NULL;
448 for (n = 0; info[2*n] != NULL ; n++)
449 if (!strcasecmp (info[2*n], tag))
450 break;
452 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
455 char *get_metadata (metadata_t type) {
456 char *meta = NULL;
457 sh_audio_t * const sh_audio = mpctx->sh_audio;
458 sh_video_t * const sh_video = mpctx->sh_video;
460 if (!is_valid_metadata_type (type))
461 return NULL;
463 switch (type)
465 case META_NAME:
467 return strdup (mp_basename2 (filename));
470 case META_VIDEO_CODEC:
472 if (sh_video->format == 0x10000001)
473 meta = strdup ("mpeg1");
474 else if (sh_video->format == 0x10000002)
475 meta = strdup ("mpeg2");
476 else if (sh_video->format == 0x10000004)
477 meta = strdup ("mpeg4");
478 else if (sh_video->format == 0x10000005)
479 meta = strdup ("h264");
480 else if (sh_video->format >= 0x20202020)
482 meta = malloc (8);
483 sprintf (meta, "%.4s", (char *) &sh_video->format);
485 else
487 meta = malloc (8);
488 sprintf (meta, "0x%08X", sh_video->format);
490 return meta;
493 case META_VIDEO_BITRATE:
495 meta = malloc (16);
496 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
497 return meta;
500 case META_VIDEO_RESOLUTION:
502 meta = malloc (16);
503 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
504 return meta;
507 case META_AUDIO_CODEC:
509 if (sh_audio->codec && sh_audio->codec->name)
510 meta = strdup (sh_audio->codec->name);
511 return meta;
514 case META_AUDIO_BITRATE:
516 meta = malloc (16);
517 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
518 return meta;
521 case META_AUDIO_SAMPLES:
523 meta = malloc (16);
524 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
525 return meta;
528 /* check for valid demuxer */
529 case META_INFO_TITLE:
530 return get_demuxer_info ("Title");
532 case META_INFO_ARTIST:
533 return get_demuxer_info ("Artist");
535 case META_INFO_ALBUM:
536 return get_demuxer_info ("Album");
538 case META_INFO_YEAR:
539 return get_demuxer_info ("Year");
541 case META_INFO_COMMENT:
542 return get_demuxer_info ("Comment");
544 case META_INFO_TRACK:
545 return get_demuxer_info ("Track");
547 case META_INFO_GENRE:
548 return get_demuxer_info ("Genre");
550 default:
551 break;
554 return meta;
557 /// step size of mixer changes
558 int volstep = 3;
560 #ifdef CONFIG_DVDNAV
561 static void mp_dvdnav_context_free(MPContext *ctx){
562 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
563 ctx->nav_smpi = NULL;
564 if (ctx->nav_buffer) free(ctx->nav_buffer);
565 ctx->nav_buffer = NULL;
566 ctx->nav_start = NULL;
567 ctx->nav_in_size = 0;
569 #endif
571 void uninit_player(unsigned int mask){
572 mask &= initialized_flags;
574 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
576 if(mask&INITIALIZED_ACODEC){
577 initialized_flags&=~INITIALIZED_ACODEC;
578 current_module="uninit_acodec";
579 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
580 #ifdef CONFIG_GUI
581 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
582 #endif
583 mpctx->sh_audio=NULL;
584 mpctx->mixer.afilter = NULL;
587 if(mask&INITIALIZED_VCODEC){
588 initialized_flags&=~INITIALIZED_VCODEC;
589 current_module="uninit_vcodec";
590 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
591 mpctx->sh_video=NULL;
592 #ifdef CONFIG_MENU
593 vf_menu=NULL;
594 #endif
597 if(mask&INITIALIZED_DEMUXER){
598 initialized_flags&=~INITIALIZED_DEMUXER;
599 current_module="free_demuxer";
600 if(mpctx->demuxer){
601 mpctx->stream=mpctx->demuxer->stream;
602 free_demuxer(mpctx->demuxer);
604 mpctx->demuxer=NULL;
607 // kill the cache process:
608 if(mask&INITIALIZED_STREAM){
609 initialized_flags&=~INITIALIZED_STREAM;
610 current_module="uninit_stream";
611 if(mpctx->stream) free_stream(mpctx->stream);
612 mpctx->stream=NULL;
615 if(mask&INITIALIZED_VO){
616 initialized_flags&=~INITIALIZED_VO;
617 current_module="uninit_vo";
618 mpctx->video_out->uninit();
619 mpctx->video_out=NULL;
620 #ifdef CONFIG_DVDNAV
621 mp_dvdnav_context_free(mpctx);
622 #endif
625 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
626 if(mask&INITIALIZED_GETCH2){
627 initialized_flags&=~INITIALIZED_GETCH2;
628 current_module="uninit_getch2";
629 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
630 // restore terminal:
631 getch2_disable();
634 if(mask&INITIALIZED_VOBSUB){
635 initialized_flags&=~INITIALIZED_VOBSUB;
636 current_module="uninit_vobsub";
637 if(vo_vobsub) vobsub_close(vo_vobsub);
638 vo_vobsub=NULL;
641 if (mask&INITIALIZED_SPUDEC){
642 initialized_flags&=~INITIALIZED_SPUDEC;
643 current_module="uninit_spudec";
644 spudec_free(vo_spudec);
645 vo_spudec=NULL;
648 if(mask&INITIALIZED_AO){
649 initialized_flags&=~INITIALIZED_AO;
650 current_module="uninit_ao";
651 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
652 if (mpctx->audio_out) mpctx->audio_out->uninit(mpctx->eof?0:1);
653 mpctx->audio_out=NULL;
656 #ifdef CONFIG_GUI
657 if(mask&INITIALIZED_GUI){
658 initialized_flags&=~INITIALIZED_GUI;
659 current_module="uninit_gui";
660 guiDone();
662 #endif
664 if(mask&INITIALIZED_INPUT){
665 initialized_flags&=~INITIALIZED_INPUT;
666 current_module="uninit_input";
667 mp_input_uninit();
668 #ifdef CONFIG_MENU
669 if (use_menu)
670 menu_uninit();
671 #endif
674 current_module=NULL;
677 void exit_player_with_rc(exit_reason_t how, int rc){
679 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
680 uninit_player(INITIALIZED_ALL);
681 #if defined(__MINGW32__) || defined(__CYGWIN__)
682 timeEndPeriod(1);
683 #endif
684 #ifdef CONFIG_X11
685 #ifdef CONFIG_GUI
686 if ( !use_gui )
687 #endif
688 vo_uninit(); // Close the X11 connection (if any is open).
689 #endif
691 #ifdef CONFIG_FREETYPE
692 current_module="uninit_font";
693 if (sub_font && sub_font != vo_font) free_font_desc(sub_font);
694 sub_font = NULL;
695 if (vo_font) free_font_desc(vo_font);
696 vo_font = NULL;
697 done_freetype();
698 #endif
699 free_osd_list();
701 #ifdef CONFIG_ASS
702 ass_library_done(ass_library);
703 ass_library = NULL;
704 #endif
706 current_module="exit_player";
708 // free mplayer config
709 if(mconfig)
710 m_config_free(mconfig);
711 mconfig = NULL;
713 if(mpctx->playtree_iter)
714 play_tree_iter_free(mpctx->playtree_iter);
715 mpctx->playtree_iter = NULL;
716 if(mpctx->playtree)
717 play_tree_free(mpctx->playtree, 1);
718 mpctx->playtree = NULL;
721 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
722 edl_records = NULL;
723 switch(how) {
724 case EXIT_QUIT:
725 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_quit);
726 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
727 break;
728 case EXIT_EOF:
729 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_eof);
730 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
731 break;
732 case EXIT_ERROR:
733 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_error);
734 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
735 break;
736 default:
737 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
739 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
741 exit(rc);
744 void exit_player(exit_reason_t how){
745 exit_player_with_rc(how, 1);
748 #ifndef __MINGW32__
749 static void child_sighandler(int x){
750 pid_t pid;
751 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
753 #endif
755 #ifdef CONFIG_CRASH_DEBUG
756 static char *prog_path;
757 static int crash_debug = 0;
758 #endif
760 static void exit_sighandler(int x){
761 static int sig_count=0;
762 #ifdef CONFIG_CRASH_DEBUG
763 if (!crash_debug || x != SIGTRAP)
764 #endif
765 ++sig_count;
766 if(initialized_flags==0 && sig_count>1) exit(1);
767 if(sig_count==5)
769 /* We're crashing bad and can't uninit cleanly :(
770 * by popular request, we make one last (dirty)
771 * effort to restore the user's
772 * terminal. */
773 getch2_disable();
774 exit(1);
776 if(sig_count==6) exit(1);
777 if(sig_count>6){
778 // can't stop :(
779 #ifndef __MINGW32__
780 kill(getpid(),SIGKILL);
781 #endif
783 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
784 current_module?current_module:"unknown"
786 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
787 if(sig_count<=1)
788 switch(x){
789 case SIGINT:
790 case SIGQUIT:
791 case SIGTERM:
792 case SIGKILL:
793 async_quit_request = 1;
794 return; // killed from keyboard (^C) or killed [-9]
795 case SIGILL:
796 #if CONFIG_RUNTIME_CPUDETECT
797 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
798 #else
799 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
800 #endif
801 case SIGFPE:
802 case SIGSEGV:
803 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
804 default:
805 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
806 #ifdef CONFIG_CRASH_DEBUG
807 if (crash_debug) {
808 int gdb_pid;
809 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
810 gdb_pid = fork();
811 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
812 if (gdb_pid == 0) { // We are the child
813 char spid[20];
814 snprintf(spid, sizeof(spid), "%i", getppid());
815 getch2_disable(); // allow terminal to work properly with gdb
816 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
817 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
818 } else if (gdb_pid < 0)
819 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
820 else {
821 waitpid(gdb_pid, NULL, 0);
823 if (x == SIGTRAP) return;
825 #endif
827 getch2_disable();
828 exit(1);
831 void mp_input_register_options(m_config_t* cfg);
833 #include "cfg-mplayer.h"
835 static void parse_cfgfiles( m_config_t* conf )
837 char *conffile;
838 int conffile_fd;
839 if (!disable_system_conf &&
840 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
841 exit_player(EXIT_NONE);
842 if ((conffile = get_path("")) == NULL) {
843 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
844 } else {
845 #ifdef __MINGW32__
846 mkdir(conffile);
847 #else
848 mkdir(conffile, 0777);
849 #endif
850 free(conffile);
851 if ((conffile = get_path("config")) == NULL) {
852 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
853 } else {
854 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
855 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
856 write(conffile_fd, default_config, strlen(default_config));
857 close(conffile_fd);
859 if (!disable_user_conf &&
860 m_config_parse_config_file(conf, conffile) < 0)
861 exit_player(EXIT_NONE);
862 free(conffile);
867 #define PROFILE_CFG_PROTOCOL "protocol."
869 static void load_per_protocol_config (m_config_t* conf, const char *const file)
871 char *str;
872 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
873 m_profile_t *p;
875 /* does filename actually uses a protocol ? */
876 str = strstr (file, "://");
877 if (!str)
878 return;
880 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
881 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
882 p = m_config_get_profile (conf, protocol);
883 if (p)
885 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingProtocolProfile, protocol);
886 m_config_set_profile(conf,p);
890 #define PROFILE_CFG_EXTENSION "extension."
892 static void load_per_extension_config (m_config_t* conf, const char *const file)
894 char *str;
895 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
896 m_profile_t *p;
898 /* does filename actually have an extension ? */
899 str = strrchr (filename, '.');
900 if (!str)
901 return;
903 sprintf (extension, PROFILE_CFG_EXTENSION);
904 strncat (extension, ++str, 7);
905 p = m_config_get_profile (conf, extension);
906 if (p)
908 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, extension);
909 m_config_set_profile(conf,p);
913 #define PROFILE_CFG_VO "vo."
914 #define PROFILE_CFG_AO "ao."
916 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
918 char profile[strlen (cfg) + strlen (out) + 1];
919 m_profile_t *p;
921 sprintf (profile, "%s%s", cfg, out);
922 p = m_config_get_profile (conf, profile);
923 if (p)
925 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, profile);
926 m_config_set_profile(conf,p);
931 * Tries to load a config file
932 * @return 0 if file was not found, 1 otherwise
934 static int try_load_config(m_config_t *conf, const char *file)
936 struct stat st;
937 if (stat(file, &st))
938 return 0;
939 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, file);
940 m_config_parse_config_file (conf, file);
941 return 1;
944 static void load_per_file_config (m_config_t* conf, const char *const file)
946 char *confpath;
947 char cfg[PATH_MAX];
948 char *name;
950 if (strlen(file) > PATH_MAX - 14) {
951 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Filename is too long, can not load file or directory specific config files\n");
952 return;
954 sprintf (cfg, "%s.conf", file);
956 name = strrchr(cfg, '/');
957 if (HAVE_DOS_PATHS) {
958 char *tmp = strrchr(cfg, '\\');
959 if (!name || tmp > name)
960 name = tmp;
961 tmp = strrchr(cfg, ':');
962 if (!name || tmp > name)
963 name = tmp;
965 if (!name)
966 name = cfg;
967 else
968 name++;
970 if (use_filedir_conf) {
971 char dircfg[PATH_MAX];
972 strcpy(dircfg, cfg);
973 strcpy(dircfg + (name - cfg), "mplayer.conf");
974 try_load_config(conf, dircfg);
976 if (try_load_config(conf, cfg))
977 return;
980 if ((confpath = get_path (name)) != NULL)
982 try_load_config(conf, confpath);
984 free (confpath);
988 /* When libmpdemux performs a blocking operation (network connection or
989 * cache filling) if the operation fails we use this function to check
990 * if it was interrupted by the user.
991 * The function returns a new value for eof. */
992 static int libmpdemux_was_interrupted(int eof) {
993 mp_cmd_t* cmd;
994 if((cmd = mp_input_get_cmd(0,0,0)) != NULL) {
995 switch(cmd->id) {
996 case MP_CMD_QUIT:
997 exit_player_with_rc(EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
998 case MP_CMD_PLAY_TREE_STEP: {
999 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
1000 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
1001 } break;
1002 case MP_CMD_PLAY_TREE_UP_STEP: {
1003 eof = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
1004 } break;
1005 case MP_CMD_PLAY_ALT_SRC_STEP: {
1006 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
1007 } break;
1009 mp_cmd_free(cmd);
1011 return eof;
1014 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
1016 static int playtree_add_playlist(play_tree_t* entry)
1018 play_tree_add_bpf(entry,filename);
1020 #ifdef CONFIG_GUI
1021 if (use_gui) {
1022 if (entry) {
1023 import_playtree_playlist_into_gui(entry, mconfig);
1024 play_tree_free_list(entry,1);
1026 } else
1027 #endif
1029 if(!entry) {
1030 entry = mpctx->playtree_iter->tree;
1031 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1032 return PT_NEXT_ENTRY;
1034 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
1035 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1036 return PT_NEXT_ENTRY;
1039 play_tree_remove(entry,1,1);
1040 return PT_NEXT_SRC;
1042 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
1043 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1044 entry = mpctx->playtree_iter->tree;
1045 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1046 return PT_NEXT_ENTRY;
1048 play_tree_remove(entry,1,1);
1050 return PT_NEXT_SRC;
1053 void add_subtitles(char *filename, float fps, int noerr)
1055 sub_data *subd;
1056 #ifdef CONFIG_ASS
1057 ass_track_t *asst = 0;
1058 #endif
1060 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1061 return;
1064 subd = sub_read_file(filename, fps);
1065 #ifdef CONFIG_ASS
1066 if (ass_enabled)
1067 #ifdef CONFIG_ICONV
1068 asst = ass_read_file(ass_library, filename, sub_cp);
1069 #else
1070 asst = ass_read_file(ass_library, filename, 0);
1071 #endif
1072 if (ass_enabled && subd && !asst)
1073 asst = ass_read_subdata(ass_library, subd, fps);
1075 if (!asst && !subd)
1076 #else
1077 if(!subd)
1078 #endif
1079 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
1080 filename_recode(filename));
1082 #ifdef CONFIG_ASS
1083 if (!asst && !subd) return;
1084 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1085 #else
1086 if (!subd) return;
1087 #endif
1088 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1089 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1090 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1091 filename_recode(filename));
1092 ++mpctx->set_of_sub_size;
1093 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
1094 filename_recode(filename));
1097 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1098 void update_set_of_subtitles(void)
1099 // subdata was changed, set_of_sub... have to be updated.
1101 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
1102 int i;
1103 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1104 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
1105 set_of_subtitles[i-1] = set_of_subtitles[i];
1106 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
1107 --mpctx->set_of_sub_size;
1108 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
1110 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1111 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
1113 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1114 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1115 ++mpctx->set_of_sub_size;
1119 void init_vo_spudec(void) {
1120 if (vo_spudec)
1121 spudec_free(vo_spudec);
1122 initialized_flags &= ~INITIALIZED_SPUDEC;
1123 vo_spudec = NULL;
1124 if (spudec_ifo) {
1125 unsigned int palette[16], width, height;
1126 current_module="spudec_init_vobsub";
1127 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1128 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1131 #ifdef CONFIG_DVDREAD
1132 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1133 current_module="spudec_init_dvdread";
1134 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1135 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1136 NULL, 0);
1138 #endif
1140 #ifdef CONFIG_DVDNAV
1141 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1142 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1143 current_module="spudec_init_dvdnav";
1144 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1146 #endif
1148 if (vo_spudec==NULL) {
1149 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1150 current_module="spudec_init_normal";
1151 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1152 spudec_set_font_factor(vo_spudec,font_factor);
1155 if (vo_spudec!=NULL) {
1156 initialized_flags|=INITIALIZED_SPUDEC;
1157 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1162 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1163 * make it all work is to use the builtin SDL-bootstrap code, which
1164 * will be done automatically by replacing our main() if we include SDL.h.
1166 #if defined(__APPLE__) && defined(CONFIG_SDL)
1167 #ifdef CONFIG_SDL_SDL_H
1168 #include <SDL/SDL.h>
1169 #else
1170 #include <SDL.h>
1171 #endif
1172 #endif
1175 * \brief append a formatted string
1176 * \param buf buffer to print into
1177 * \param pos position of terminating 0 in buf
1178 * \param len maximum number of characters in buf, not including terminating 0
1179 * \param format printf format string
1181 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1183 va_list va;
1184 va_start(va, format);
1185 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1186 va_end(va);
1187 if (*pos >= len ) {
1188 buf[len] = 0;
1189 *pos = len;
1194 * \brief append time in the hh:mm:ss.f format
1195 * \param buf buffer to print into
1196 * \param pos position of terminating 0 in buf
1197 * \param len maximum number of characters in buf, not including terminating 0
1198 * \param time time value to convert/append
1200 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1201 long tenths = 10 * time;
1202 int f1 = tenths % 10;
1203 int ss = (tenths / 10) % 60;
1204 int mm = (tenths / 600) % 60;
1205 int hh = tenths / 36000;
1206 if (time <= 0) {
1207 saddf(buf, pos, len, "unknown");
1208 return;
1210 if (hh > 0)
1211 saddf(buf, pos, len, "%2d:", hh);
1212 if (hh > 0 || mm > 0)
1213 saddf(buf, pos, len, "%02d:", mm);
1214 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1218 * \brief print the status line
1219 * \param a_pos audio position
1220 * \param a_v A-V desynchronization
1221 * \param corr amount out A-V synchronization
1223 static void print_status(float a_pos, float a_v, float corr)
1225 sh_video_t * const sh_video = mpctx->sh_video;
1226 int width;
1227 char *line;
1228 unsigned pos = 0;
1229 get_screen_size();
1230 if (screen_width > 0)
1231 width = screen_width;
1232 else
1233 width = 80;
1234 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1235 /* Windows command line is broken (MinGW's rxvt works, but we
1236 * should not depend on that). */
1237 width--;
1238 #endif
1239 line = malloc(width + 1); // one additional char for the terminating null
1241 // Audio time
1242 if (mpctx->sh_audio) {
1243 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1244 if (!sh_video) {
1245 float len = demuxer_get_time_length(mpctx->demuxer);
1246 saddf(line, &pos, width, "(");
1247 sadd_hhmmssf(line, &pos, width, a_pos);
1248 saddf(line, &pos, width, ") of %.1f (", len);
1249 sadd_hhmmssf(line, &pos, width, len);
1250 saddf(line, &pos, width, ") ");
1254 // Video time
1255 if (sh_video)
1256 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1258 // A-V sync
1259 if (mpctx->sh_audio && sh_video)
1260 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
1262 // Video stats
1263 if (sh_video)
1264 saddf(line, &pos, width, "%3d/%3d ",
1265 (int)sh_video->num_frames,
1266 (int)sh_video->num_frames_decoded);
1268 // CPU usage
1269 if (sh_video) {
1270 if (sh_video->timer > 0.5)
1271 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1272 (int)(100.0*video_time_usage*playback_speed/(double)sh_video->timer),
1273 (int)(100.0*vout_time_usage*playback_speed/(double)sh_video->timer),
1274 (100.0*audio_time_usage*playback_speed/(double)sh_video->timer));
1275 else
1276 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1277 } else if (mpctx->sh_audio) {
1278 if (mpctx->delay > 0.5)
1279 saddf(line, &pos, width, "%4.1f%% ",
1280 100.0*audio_time_usage/(double)mpctx->delay);
1281 else
1282 saddf(line, &pos, width, "??,?%% ");
1285 // VO stats
1286 if (sh_video)
1287 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1289 #ifdef CONFIG_STREAM_CACHE
1290 // cache stats
1291 if (stream_cache_size > 0)
1292 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1293 #endif
1295 // other
1296 if (playback_speed != 1)
1297 saddf(line, &pos, width, "%4.2fx ", playback_speed);
1299 // end
1300 if (erase_to_end_of_line) {
1301 line[pos] = 0;
1302 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1303 } else {
1304 memset(&line[pos], ' ', width - pos);
1305 line[width] = 0;
1306 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1308 free(line);
1312 * \brief build a chain of audio filters that converts the input format
1313 * to the ao's format, taking into account the current playback_speed.
1314 * \param sh_audio describes the requested input format of the chain.
1315 * \param ao_data describes the requested output format of the chain.
1317 int build_afilter_chain(sh_audio_t *sh_audio, ao_data_t *ao_data)
1319 int new_srate;
1320 int result;
1321 if (!sh_audio)
1323 #ifdef CONFIG_GUI
1324 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1325 #endif
1326 mpctx->mixer.afilter = NULL;
1327 return 0;
1329 if(af_control_any_rev(sh_audio->afilter,
1330 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1331 &playback_speed)) {
1332 new_srate = sh_audio->samplerate;
1333 } else {
1334 new_srate = sh_audio->samplerate * playback_speed;
1335 if (new_srate != ao_data->samplerate) {
1336 // limits are taken from libaf/af_resample.c
1337 if (new_srate < 8000)
1338 new_srate = 8000;
1339 if (new_srate > 192000)
1340 new_srate = 192000;
1341 playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1344 result = init_audio_filters(sh_audio, new_srate,
1345 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1346 mpctx->mixer.afilter = sh_audio->afilter;
1347 #ifdef CONFIG_GUI
1348 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1349 #endif
1350 return result;
1354 typedef struct mp_osd_msg mp_osd_msg_t;
1355 struct mp_osd_msg {
1356 /// Previous message on the stack.
1357 mp_osd_msg_t* prev;
1358 /// Message text.
1359 char msg[128];
1360 int id,level,started;
1361 /// Display duration in ms.
1362 unsigned time;
1365 /// OSD message stack.
1366 static mp_osd_msg_t* osd_msg_stack = NULL;
1369 * \brief Add a message on the OSD message stack
1371 * If a message with the same id is already present in the stack
1372 * it is pulled on top of the stack, otherwise a new message is created.
1376 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1377 mp_osd_msg_t *msg,*last=NULL;
1378 va_list va;
1379 int r;
1381 // look if the id is already in the stack
1382 for(msg = osd_msg_stack ; msg && msg->id != id ;
1383 last = msg, msg = msg->prev);
1384 // not found: alloc it
1385 if(!msg) {
1386 msg = calloc(1,sizeof(mp_osd_msg_t));
1387 msg->prev = osd_msg_stack;
1388 osd_msg_stack = msg;
1389 } else if(last) { // found, but it's not on top of the stack
1390 last->prev = msg->prev;
1391 msg->prev = osd_msg_stack;
1392 osd_msg_stack = msg;
1394 // write the msg
1395 va_start(va,fmt);
1396 r = vsnprintf(msg->msg, 128, fmt, va);
1397 va_end(va);
1398 if(r >= 128) msg->msg[127] = 0;
1399 // set id and time
1400 msg->id = id;
1401 msg->level = level;
1402 msg->time = time;
1407 * \brief Remove a message from the OSD stack
1409 * This function can be used to get rid of a message right away.
1413 void rm_osd_msg(int id) {
1414 mp_osd_msg_t *msg,*last=NULL;
1416 // Search for the msg
1417 for(msg = osd_msg_stack ; msg && msg->id != id ;
1418 last = msg, msg = msg->prev);
1419 if(!msg) return;
1421 // Detach it from the stack and free it
1422 if(last)
1423 last->prev = msg->prev;
1424 else
1425 osd_msg_stack = msg->prev;
1426 free(msg);
1430 * \brief Remove all messages from the OSD stack
1434 static void clear_osd_msgs(void) {
1435 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1436 while(msg) {
1437 prev = msg->prev;
1438 free(msg);
1439 msg = prev;
1441 osd_msg_stack = NULL;
1445 * \brief Get the current message from the OSD stack.
1447 * This function decrements the message timer and destroys the old ones.
1448 * The message that should be displayed is returned (if any).
1452 static mp_osd_msg_t* get_osd_msg(void) {
1453 mp_osd_msg_t *msg,*prev,*last = NULL;
1454 static unsigned last_update = 0;
1455 unsigned now = GetTimerMS();
1456 unsigned diff;
1457 char hidden_dec_done = 0;
1459 if (osd_visible) {
1460 // 36000000 means max timed visibility is 1 hour into the future, if
1461 // the difference is greater assume it's wrapped around from below 0
1462 if (osd_visible - now > 36000000) {
1463 osd_visible = 0;
1464 vo_osd_progbar_type = -1; // disable
1465 vo_osd_changed(OSDTYPE_PROGBAR);
1466 if (mpctx->osd_function != OSD_PAUSE)
1467 mpctx->osd_function = OSD_PLAY;
1471 if(!last_update) last_update = now;
1472 diff = now >= last_update ? now - last_update : 0;
1474 last_update = now;
1476 // Look for the first message in the stack with high enough level.
1477 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1478 prev = msg->prev;
1479 if(msg->level > osd_level && hidden_dec_done) continue;
1480 // The message has a high enough level or it is the first hidden one
1481 // in both cases we decrement the timer or kill it.
1482 if(!msg->started || msg->time > diff) {
1483 if(msg->started) msg->time -= diff;
1484 else msg->started = 1;
1485 // display it
1486 if(msg->level <= osd_level) return msg;
1487 hidden_dec_done = 1;
1488 continue;
1490 // kill the message
1491 free(msg);
1492 if(last) {
1493 last->prev = prev;
1494 msg = last;
1495 } else {
1496 osd_msg_stack = prev;
1497 msg = NULL;
1500 // Nothing found
1501 return NULL;
1505 * \brief Display the OSD bar.
1507 * Display the OSD bar or fall back on a simple message.
1511 void set_osd_bar(int type,const char* name,double min,double max,double val) {
1513 if(osd_level < 1) return;
1515 if(mpctx->sh_video) {
1516 osd_visible = (GetTimerMS() + 1000) | 1;
1517 vo_osd_progbar_type = type;
1518 vo_osd_progbar_value = 256*(val-min)/(max-min);
1519 vo_osd_changed(OSDTYPE_PROGBAR);
1520 return;
1523 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1524 name,ROUND(100*(val-min)/(max-min)));
1528 * \brief Display text subtitles on the OSD
1530 void set_osd_subtitle(subtitle *subs) {
1531 int i;
1532 vo_sub = subs;
1533 vo_osd_changed(OSDTYPE_SUBTITLE);
1534 if (!mpctx->sh_video) {
1535 // reverse order, since newest set_osd_msg is displayed first
1536 for (i = SUB_MAX_TEXT - 1; i >= 0; i--) {
1537 if (!subs || i >= subs->lines || !subs->text[i])
1538 rm_osd_msg(OSD_MSG_SUB_BASE + i);
1539 else {
1540 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1541 int display_time = i == subs->lines - 1 ? 180000 : 2000;
1542 set_osd_msg(OSD_MSG_SUB_BASE + i, 1, display_time, "%s", subs->text[i]);
1549 * \brief Update the OSD message line.
1551 * This function displays the current message on the vo OSD or on the term.
1552 * If the stack is empty and the OSD level is high enough the timer
1553 * is displayed (only on the vo OSD).
1557 static void update_osd_msg(void) {
1558 mp_osd_msg_t *msg;
1559 static char osd_text[128] = "";
1560 static char osd_text_timer[128];
1562 // we need some mem for vo_osd_text
1563 vo_osd_text = (unsigned char*)osd_text;
1565 // Look if we have a msg
1566 if((msg = get_osd_msg())) {
1567 if(strcmp(osd_text,msg->msg)) {
1568 strncpy((char*)osd_text, msg->msg, 127);
1569 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1570 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1572 return;
1575 if(mpctx->sh_video) {
1576 // fallback on the timer
1577 if(osd_level>=2) {
1578 int len = demuxer_get_time_length(mpctx->demuxer);
1579 int percentage = -1;
1580 char percentage_text[10];
1581 int pts = demuxer_get_current_time(mpctx->demuxer);
1583 if (mpctx->osd_show_percentage)
1584 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1586 if (percentage >= 0)
1587 snprintf(percentage_text, 9, " (%d%%)", percentage);
1588 else
1589 percentage_text[0] = 0;
1591 if (osd_level == 3)
1592 snprintf(osd_text_timer, 63,
1593 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1594 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1595 len/3600,(len/60)%60,len%60,percentage_text);
1596 else
1597 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1598 mpctx->osd_function,pts/3600,(pts/60)%60,
1599 pts%60,percentage_text);
1600 } else
1601 osd_text_timer[0]=0;
1603 // always decrement the percentage timer
1604 if(mpctx->osd_show_percentage)
1605 mpctx->osd_show_percentage--;
1607 if(strcmp(osd_text,osd_text_timer)) {
1608 strncpy(osd_text, osd_text_timer, 63);
1609 vo_osd_changed(OSDTYPE_OSD);
1611 return;
1614 // Clear the term osd line
1615 if(term_osd && osd_text[0]) {
1616 osd_text[0] = 0;
1617 printf("%s\n",term_osd_esc);
1621 ///@}
1622 // OSDMsgStack
1625 void reinit_audio_chain(void) {
1626 if (!mpctx->sh_audio)
1627 return;
1628 current_module="init_audio_codec";
1629 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1630 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1631 goto init_error;
1633 initialized_flags|=INITIALIZED_ACODEC;
1634 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1637 current_module="af_preinit";
1638 ao_data.samplerate=force_srate;
1639 ao_data.channels=0;
1640 ao_data.format=audio_output_format;
1641 #if 1
1642 // first init to detect best values
1643 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1644 // input:
1645 mpctx->sh_audio->samplerate,
1646 // output:
1647 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1648 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1649 exit_player(EXIT_ERROR);
1651 #endif
1652 current_module="ao2_init";
1653 mpctx->audio_out = init_best_audio_out(audio_driver_list,
1654 0, // plugin flag
1655 ao_data.samplerate,
1656 ao_data.channels,
1657 ao_data.format, 0);
1658 if(!mpctx->audio_out){
1659 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1660 goto init_error;
1662 initialized_flags|=INITIALIZED_AO;
1663 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1664 mpctx->audio_out->info->short_name,
1665 ao_data.samplerate, ao_data.channels,
1666 af_fmt2str_short(ao_data.format),
1667 af_fmt2bits(ao_data.format)/8 );
1668 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1669 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1670 if(strlen(mpctx->audio_out->info->comment) > 0)
1671 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1672 // init audio filters:
1673 #if 1
1674 current_module="af_init";
1675 if(!build_afilter_chain(mpctx->sh_audio, &ao_data)) {
1676 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1677 goto init_error;
1679 #endif
1680 mpctx->mixer.audio_out = mpctx->audio_out;
1681 mpctx->mixer.volstep = volstep;
1682 return;
1684 init_error:
1685 uninit_player(INITIALIZED_ACODEC|INITIALIZED_AO); // close codec and possibly AO
1686 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1687 mpctx->d_audio->id = -2;
1691 ///@}
1692 // Command2Property
1695 // Return pts value corresponding to the end point of audio written to the
1696 // ao so far.
1697 static double written_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio)
1699 double buffered_output;
1700 // first calculate the end pts of audio that has been output by decoder
1701 double a_pts = sh_audio->pts;
1702 if (a_pts != MP_NOPTS_VALUE)
1703 // Good, decoder supports new way of calculating audio pts.
1704 // sh_audio->pts is the timestamp of the latest input packet with
1705 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1706 // the amount of bytes the decoder has written after that timestamp.
1707 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1708 else {
1709 // Decoder doesn't support new way of calculating pts (or we're
1710 // being called before it has decoded anything with known timestamp).
1711 // Use the old method of audio pts calculation: take the timestamp
1712 // of last packet with known pts the decoder has read data from,
1713 // and add amount of bytes read after the beginning of that packet
1714 // divided by input bps. This will be inaccurate if the input/output
1715 // ratio is not constant for every audio packet or if it is constant
1716 // but not accurately known in sh_audio->i_bps.
1718 a_pts = d_audio->pts;
1719 // ds_tell_pts returns bytes read after last timestamp from
1720 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1721 // it has read but not decoded
1722 if (sh_audio->i_bps)
1723 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1724 (double)sh_audio->i_bps;
1726 // Now a_pts hopefully holds the pts for end of audio from decoder.
1727 // Substract data in buffers between decoder and audio out.
1729 // Decoded but not filtered
1730 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1732 // Data buffered in audio filters, measured in bytes of "missing" output
1733 buffered_output = af_calc_delay(sh_audio->afilter);
1735 // Data that was ready for ao but was buffered because ao didn't fully
1736 // accept everything to internal buffers yet
1737 buffered_output += sh_audio->a_out_buffer_len;
1739 // Filters divide audio length by playback_speed, so multiply by it
1740 // to get the length in original units without speedup or slowdown
1741 a_pts -= buffered_output * playback_speed / ao_data.bps;
1743 return a_pts;
1746 // Return pts value corresponding to currently playing audio.
1747 double playing_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio,
1748 const ao_functions_t *audio_out)
1750 return written_audio_pts(sh_audio, d_audio) - playback_speed *
1751 audio_out->get_delay();
1754 static int check_framedrop(double frame_time) {
1755 // check for frame-drop:
1756 current_module = "check_framedrop";
1757 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1758 static int dropped_frames;
1759 float delay = playback_speed*mpctx->audio_out->get_delay();
1760 float d = delay-mpctx->delay;
1761 ++total_frame_cnt;
1762 // we should avoid dropping too many frames in sequence unless we
1763 // are too late. and we allow 100ms A-V delay here:
1764 if (d < -dropped_frames*frame_time-0.100 &&
1765 mpctx->osd_function != OSD_PAUSE) {
1766 ++drop_frame_cnt;
1767 ++dropped_frames;
1768 return frame_dropping;
1769 } else
1770 dropped_frames = 0;
1772 return 0;
1775 static int generate_video_frame(sh_video_t *sh_video, demux_stream_t *d_video)
1777 unsigned char *start;
1778 int in_size;
1779 int hit_eof=0;
1780 double pts;
1782 while (1) {
1783 int drop_frame = check_framedrop(sh_video->frametime);
1784 void *decoded_frame;
1785 current_module = "decode video";
1786 // XXX Time used in this call is not counted in any performance
1787 // timer now, OSD is not updated correctly for filter-added frames
1788 if (vf_output_queued_frame(sh_video->vfilter))
1789 break;
1790 current_module = "video_read_frame";
1791 in_size = ds_get_packet_pts(d_video, &start, &pts);
1792 if (in_size < 0) {
1793 // try to extract last frames in case of decoder lag
1794 in_size = 0;
1795 pts = 1e300;
1796 hit_eof = 1;
1798 if (in_size > max_framesize)
1799 max_framesize = in_size;
1800 current_module = "decode video";
1801 decoded_frame = decode_video(sh_video, start, in_size, drop_frame, pts);
1802 if (decoded_frame) {
1803 update_subtitles(sh_video, sh_video->pts, mpctx->d_sub, 0);
1804 update_teletext(sh_video, mpctx->demuxer, 0);
1805 update_osd_msg();
1806 current_module = "filter video";
1807 if (filter_video(sh_video, decoded_frame, sh_video->pts))
1808 break;
1809 } else if (drop_frame)
1810 return -1;
1811 if (hit_eof)
1812 return 0;
1814 return 1;
1817 #ifdef HAVE_RTC
1818 int rtc_fd = -1;
1819 #endif
1821 static float timing_sleep(float time_frame)
1823 #ifdef HAVE_RTC
1824 if (rtc_fd >= 0){
1825 // -------- RTC -----------
1826 current_module="sleep_rtc";
1827 while (time_frame > 0.000) {
1828 unsigned long rtc_ts;
1829 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1830 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1831 time_frame -= GetRelativeTime();
1833 } else
1834 #endif
1836 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1837 // unnecessarily high CPU usage
1838 float margin = softsleep ? 0.011 : 0;
1839 current_module = "sleep_timer";
1840 while (time_frame > margin) {
1841 usec_sleep(1000000 * (time_frame - margin));
1842 time_frame -= GetRelativeTime();
1844 if (softsleep){
1845 current_module = "sleep_soft";
1846 if (time_frame < 0)
1847 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1848 while (time_frame > 0)
1849 time_frame-=GetRelativeTime(); // burn the CPU
1852 return time_frame;
1855 static void select_subtitle(MPContext *mpctx) {
1856 // find the best sub to use
1857 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
1858 mpctx->global_sub_pos = -1; // no subs by default
1859 if (vobsub_index_id >= 0) {
1860 // if user asks for a vobsub id, use that first.
1861 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
1862 } else if (dvdsub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1863 // if user asks for a dvd sub id, use that next.
1864 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id;
1865 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
1866 // if there are text subs to use, use those. (autosubs come last here)
1867 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
1868 } else if (dvdsub_id == -1 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1869 // finally select subs by language and container hints
1870 if (dvdsub_id == -1 && dvdsub_lang)
1871 dvdsub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
1872 if (dvdsub_id == -1)
1873 dvdsub_id = demuxer_default_sub_track(mpctx->demuxer);
1874 if (dvdsub_id >= 0)
1875 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id;
1877 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
1878 mpctx->global_sub_pos--;
1879 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
1882 #ifdef CONFIG_DVDNAV
1883 #ifndef FF_B_TYPE
1884 #define FF_B_TYPE 3
1885 #endif
1886 /// store decoded video image
1887 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1888 mp_image_t *from_mpi) {
1889 mp_image_t *mpi;
1891 /// Do not store B-frames
1892 if (from_mpi->pict_type == FF_B_TYPE)
1893 return to_mpi;
1895 if (to_mpi &&
1896 to_mpi->w == from_mpi->w &&
1897 to_mpi->h == from_mpi->h &&
1898 to_mpi->imgfmt == from_mpi->imgfmt)
1899 mpi = to_mpi;
1900 else {
1901 if (to_mpi)
1902 free_mp_image(to_mpi);
1903 if (from_mpi->w == 0 || from_mpi->h == 0)
1904 return NULL;
1905 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1908 copy_mpi(mpi,from_mpi);
1909 return mpi;
1912 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1913 if (ctx->sh_video) {
1914 /// clear video pts
1915 ctx->d_video->pts = 0.0f;
1916 ctx->sh_video->pts = 0.0f;
1917 ctx->sh_video->i_pts = 0.0f;
1918 ctx->sh_video->last_pts = 0.0f;
1919 ctx->sh_video->num_buffered_pts = 0;
1920 ctx->sh_video->num_frames = 0;
1921 ctx->sh_video->num_frames_decoded = 0;
1922 ctx->sh_video->timer = 0.0f;
1923 ctx->sh_video->stream_delay = 0.0f;
1924 ctx->sh_video->timer = 0;
1925 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1928 if (ctx->sh_audio) {
1929 /// free audio packets and reset
1930 ds_free_packs(ctx->d_audio);
1931 audio_delay -= ctx->sh_audio->stream_delay;
1932 ctx->delay =- audio_delay;
1933 ctx->audio_out->reset();
1934 resync_audio_stream(ctx->sh_audio);
1937 audio_delay = 0.0f;
1938 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + mp_dvdnav_number_of_subs(mpctx->stream);
1939 if (dvdsub_lang && dvdsub_id == dvdsub_lang_id) {
1940 dvdsub_lang_id = mp_dvdnav_sid_from_lang(ctx->stream, dvdsub_lang);
1941 if (dvdsub_lang_id != dvdsub_id) {
1942 dvdsub_id = dvdsub_lang_id;
1943 select_subtitle(ctx);
1947 /// clear all EOF related flags
1948 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1951 /// Restore last decoded DVDNAV (still frame)
1952 static mp_image_t *mp_dvdnav_restore_smpi(int *in_size,
1953 unsigned char **start,
1954 mp_image_t *decoded_frame)
1956 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1957 return decoded_frame;
1959 /// a change occured in dvdnav stream
1960 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1961 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1962 mp_dvdnav_context_free(mpctx);
1963 mp_dvdnav_reset_stream(mpctx);
1964 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1965 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1968 if (*in_size < 0) {
1969 float len;
1971 /// Display still frame, if any
1972 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1973 decoded_frame = mpctx->nav_smpi;
1975 /// increment video frame : continue playing after still frame
1976 len = demuxer_get_time_length(mpctx->demuxer);
1977 if (mpctx->sh_video->pts >= len &&
1978 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1979 mp_dvdnav_skip_still(mpctx->stream);
1980 mp_dvdnav_skip_wait(mpctx->stream);
1982 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1984 if (mpctx->nav_buffer) {
1985 *start = mpctx->nav_start;
1986 *in_size = mpctx->nav_in_size;
1987 if (mpctx->nav_start)
1988 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1992 return decoded_frame;
1995 /// Save last decoded DVDNAV (still frame)
1996 static void mp_dvdnav_save_smpi(int in_size,
1997 unsigned char *start,
1998 mp_image_t *decoded_frame)
2000 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
2001 return;
2003 if (mpctx->nav_buffer)
2004 free(mpctx->nav_buffer);
2006 mpctx->nav_buffer = malloc(in_size);
2007 mpctx->nav_start = start;
2008 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
2009 if (mpctx->nav_buffer)
2010 memcpy(mpctx->nav_buffer,start,in_size);
2012 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
2013 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
2015 #endif /* CONFIG_DVDNAV */
2017 static void adjust_sync_and_print_status(int between_frames, float timing_error)
2019 current_module="av_sync";
2021 if(mpctx->sh_audio){
2022 double a_pts, v_pts;
2024 if (autosync)
2026 * If autosync is enabled, the value for delay must be calculated
2027 * a bit differently. It is set only to the difference between
2028 * the audio and video timers. Any attempt to include the real
2029 * or corrected delay causes the pts_correction code below to
2030 * try to correct for the changes in delay which autosync is
2031 * trying to measure. This keeps the two from competing, but still
2032 * allows the code to correct for PTS drift *only*. (Using a delay
2033 * value here, even a "corrected" one, would be incompatible with
2034 * autosync mode.)
2036 a_pts = written_audio_pts(mpctx->sh_audio, mpctx->d_audio) - mpctx->delay;
2037 else
2038 a_pts = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
2040 v_pts = mpctx->sh_video->pts;
2043 static int drop_message=0;
2044 double AV_delay = a_pts - audio_delay - v_pts;
2045 double x;
2046 if (AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
2047 ++drop_message;
2048 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
2050 if (autosync)
2051 x = AV_delay*0.1f;
2052 else
2053 /* Do not correct target time for the next frame if this frame
2054 * was late not because of wrong target time but because the
2055 * target time could not be met */
2056 x = (AV_delay + timing_error * playback_speed) * 0.1f;
2057 if (x < -max_pts_correction)
2058 x = -max_pts_correction;
2059 else if (x> max_pts_correction)
2060 x = max_pts_correction;
2061 if (default_max_pts_correction >= 0)
2062 max_pts_correction = default_max_pts_correction;
2063 else
2064 max_pts_correction = mpctx->sh_video->frametime*0.10; // +-10% of time
2065 if (!between_frames) {
2066 mpctx->delay+=x;
2067 c_total+=x;
2069 if(!quiet)
2070 print_status(a_pts - audio_delay, AV_delay, c_total);
2073 } else {
2074 // No audio:
2076 if (!quiet)
2077 print_status(0, 0, 0);
2081 static int fill_audio_out_buffers(void)
2083 unsigned int t;
2084 double tt;
2085 int playsize;
2086 int playflags=0;
2087 int audio_eof=0;
2088 int bytes_to_write;
2089 sh_audio_t * const sh_audio = mpctx->sh_audio;
2091 current_module="play_audio";
2093 while (1) {
2094 int sleep_time;
2095 // all the current uses of ao_data.pts seem to be in aos that handle
2096 // sync completely wrong; there should be no need to use ao_data.pts
2097 // in get_space()
2098 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2099 bytes_to_write = mpctx->audio_out->get_space();
2100 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
2101 break;
2103 // handle audio-only case:
2104 // this is where mplayer sleeps during audio-only playback
2105 // to avoid 100% CPU use
2106 sleep_time = (ao_data.outburst - bytes_to_write) * 1000 / ao_data.bps;
2107 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
2108 usec_sleep(sleep_time * 1000);
2111 while (bytes_to_write) {
2112 playsize = bytes_to_write;
2113 if (playsize > MAX_OUTBURST)
2114 playsize = MAX_OUTBURST;
2115 bytes_to_write -= playsize;
2117 // Fill buffer if needed:
2118 current_module="decode_audio";
2119 t = GetTimer();
2120 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2121 if (mpctx->d_audio->eof) {
2122 audio_eof = 1;
2123 if (sh_audio->a_out_buffer_len == 0)
2124 return 0;
2126 t = GetTimer() - t;
2127 tt = t*0.000001f; audio_time_usage+=tt;
2128 if (playsize > sh_audio->a_out_buffer_len) {
2129 playsize = sh_audio->a_out_buffer_len;
2130 if (audio_eof)
2131 playflags |= AOPLAY_FINAL_CHUNK;
2133 if (!playsize)
2134 break;
2136 // play audio:
2137 current_module="play_audio";
2139 // Is this pts value actually useful for the aos that access it?
2140 // They're obviously badly broken in the way they handle av sync;
2141 // would not having access to this make them more broken?
2142 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2143 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2145 if (playsize > 0) {
2146 sh_audio->a_out_buffer_len -= playsize;
2147 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2148 sh_audio->a_out_buffer_len);
2149 mpctx->delay += playback_speed*playsize/(double)ao_data.bps;
2151 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2152 // Sanity check to avoid hanging in case current ao doesn't output
2153 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2154 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2155 sh_audio->a_out_buffer_len = 0;
2158 return 1;
2161 static int sleep_until_update(float *time_frame, float *aq_sleep_time)
2163 int frame_time_remaining = 0;
2164 current_module="calc_sleep_time";
2166 *time_frame -= GetRelativeTime(); // reset timer
2168 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2169 float delay = mpctx->audio_out->get_delay();
2170 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2172 if (autosync) {
2174 * Adjust this raw delay value by calculating the expected
2175 * delay for this frame and generating a new value which is
2176 * weighted between the two. The higher autosync is, the
2177 * closer to the delay value gets to that which "-nosound"
2178 * would have used, and the longer it will take for A/V
2179 * sync to settle at the right value (but it eventually will.)
2180 * This settling time is very short for values below 100.
2182 float predicted = mpctx->delay / playback_speed + *time_frame;
2183 float difference = delay - predicted;
2184 delay = predicted + difference / (float)autosync;
2187 *time_frame = delay - mpctx->delay / playback_speed;
2189 // delay = amount of audio buffered in soundcard/driver
2190 if (delay > 0.25) delay=0.25; else
2191 if (delay < 0.10) delay=0.10;
2192 if (*time_frame > delay*0.6) {
2193 // sleep time too big - may cause audio drops (buffer underrun)
2194 frame_time_remaining = 1;
2195 *time_frame = delay*0.5;
2197 } else {
2198 // If we're lagging more than 200 ms behind the right playback rate,
2199 // don't try to "catch up".
2200 // If benchmark is set always output frames as fast as possible
2201 // without sleeping.
2202 if (*time_frame < -0.2 || benchmark)
2203 *time_frame = 0;
2206 *aq_sleep_time += *time_frame;
2209 //============================== SLEEP: ===================================
2211 // flag 256 means: libvo driver does its timing (dvb card)
2212 if (*time_frame > 0.001 && !(vo_flags&256))
2213 *time_frame = timing_sleep(*time_frame);
2214 return frame_time_remaining;
2217 int reinit_video_chain(void) {
2218 sh_video_t * const sh_video = mpctx->sh_video;
2219 double ar=-1.0;
2220 //================== Init VIDEO (codec & libvo) ==========================
2221 if(!fixed_vo || !(initialized_flags&INITIALIZED_VO)){
2222 current_module="preinit_libvo";
2224 //shouldn't we set dvideo->id=-2 when we fail?
2225 vo_config_count=0;
2226 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2227 if(!(mpctx->video_out=init_best_video_out(video_driver_list))){
2228 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
2229 goto err_out;
2231 initialized_flags|=INITIALIZED_VO;
2234 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2235 mpctx->sh_video->stream_aspect = ar;
2236 current_module="init_video_filters";
2238 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2239 sh_video->vfilter=(void*)vf_open_filter(NULL,"vo",vf_arg);
2241 #ifdef CONFIG_MENU
2242 if(use_menu) {
2243 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2244 vf_menu = vf_open_plugin(libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2245 if(!vf_menu) {
2246 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2247 use_menu = 0;
2250 if(vf_menu)
2251 sh_video->vfilter=(void*)vf_menu;
2252 #endif
2254 #ifdef CONFIG_ASS
2255 if(ass_enabled) {
2256 int i;
2257 int insert = 1;
2258 if (vf_settings)
2259 for (i = 0; vf_settings[i].name; ++i)
2260 if (strcmp(vf_settings[i].name, "ass") == 0) {
2261 insert = 0;
2262 break;
2264 if (insert) {
2265 extern vf_info_t vf_info_ass;
2266 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2267 char* vf_arg[] = {"auto", "1", NULL};
2268 vf_instance_t* vf_ass = vf_open_plugin(libass_vfs,sh_video->vfilter,"ass",vf_arg);
2269 if (vf_ass)
2270 sh_video->vfilter=(void*)vf_ass;
2271 else
2272 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2275 #endif
2277 sh_video->vfilter=(void*)append_filters(sh_video->vfilter);
2279 #ifdef CONFIG_ASS
2280 if (ass_enabled)
2281 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2282 #endif
2284 current_module="init_video_codec";
2286 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2287 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2288 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2290 if(!sh_video->initialized){
2291 if(!fixed_vo) uninit_player(INITIALIZED_VO);
2292 goto err_out;
2295 initialized_flags|=INITIALIZED_VCODEC;
2297 if (sh_video->codec)
2298 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2300 sh_video->last_pts = MP_NOPTS_VALUE;
2301 sh_video->num_buffered_pts = 0;
2302 sh_video->next_frame_time = 0;
2304 if(auto_quality>0){
2305 // Auto quality option enabled
2306 output_quality=get_video_quality_max(sh_video);
2307 if(auto_quality>output_quality) auto_quality=output_quality;
2308 else output_quality=auto_quality;
2309 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2310 set_video_quality(sh_video,output_quality);
2313 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2315 current_module="init_vo";
2317 return 1;
2319 err_out:
2320 mpctx->sh_video = mpctx->d_video->sh = NULL;
2321 return 0;
2324 static double update_video(int *blit_frame)
2326 sh_video_t * const sh_video = mpctx->sh_video;
2327 //-------------------- Decode a frame: -----------------------
2328 double frame_time;
2329 *blit_frame = 0; // Don't blit if we hit EOF
2330 if (!correct_pts) {
2331 unsigned char* start=NULL;
2332 void *decoded_frame = NULL;
2333 int drop_frame=0;
2334 int in_size;
2336 current_module = "video_read_frame";
2337 frame_time = sh_video->next_frame_time;
2338 in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2339 &start, force_fps);
2340 #ifdef CONFIG_DVDNAV
2341 /// wait, still frame or EOF
2342 if (mpctx->stream->type == STREAMTYPE_DVDNAV && in_size < 0) {
2343 if (mp_dvdnav_is_eof(mpctx->stream)) return -1;
2344 if (mpctx->d_video) mpctx->d_video->eof = 0;
2345 if (mpctx->d_audio) mpctx->d_audio->eof = 0;
2346 mpctx->stream->eof = 0;
2347 } else
2348 #endif
2349 if (in_size < 0)
2350 return -1;
2351 if (in_size > max_framesize)
2352 max_framesize = in_size; // stats
2353 sh_video->timer += frame_time;
2354 if (mpctx->sh_audio)
2355 mpctx->delay -= frame_time;
2356 // video_read_frame can change fps (e.g. for ASF video)
2357 vo_fps = sh_video->fps;
2358 drop_frame = check_framedrop(frame_time);
2359 update_subtitles(sh_video, sh_video->pts, mpctx->d_sub, 0);
2360 update_teletext(sh_video, mpctx->demuxer, 0);
2361 update_osd_msg();
2362 current_module = "decode_video";
2363 #ifdef CONFIG_DVDNAV
2364 decoded_frame = mp_dvdnav_restore_smpi(&in_size,&start,decoded_frame);
2365 /// still frame has been reached, no need to decode
2366 if (in_size > 0 && !decoded_frame)
2367 #endif
2368 decoded_frame = decode_video(sh_video, start, in_size, drop_frame,
2369 sh_video->pts);
2370 #ifdef CONFIG_DVDNAV
2371 /// save back last still frame for future display
2372 mp_dvdnav_save_smpi(in_size,start,decoded_frame);
2373 #endif
2374 current_module = "filter_video";
2375 *blit_frame = (decoded_frame && filter_video(sh_video, decoded_frame,
2376 sh_video->pts));
2378 else {
2379 int res = generate_video_frame(sh_video, mpctx->d_video);
2380 if (!res)
2381 return -1;
2382 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter,
2383 VFCTRL_GET_PTS, &sh_video->pts);
2384 if (sh_video->pts == MP_NOPTS_VALUE) {
2385 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts after filters MISSING\n");
2386 sh_video->pts = sh_video->last_pts;
2388 if (sh_video->last_pts == MP_NOPTS_VALUE)
2389 sh_video->last_pts= sh_video->pts;
2390 else if (sh_video->last_pts > sh_video->pts) {
2391 sh_video->last_pts = sh_video->pts;
2392 mp_msg(MSGT_CPLAYER, MSGL_INFO, "pts value < previous\n");
2394 frame_time = sh_video->pts - sh_video->last_pts;
2395 sh_video->last_pts = sh_video->pts;
2396 sh_video->timer += frame_time;
2397 if(mpctx->sh_audio)
2398 mpctx->delay -= frame_time;
2399 *blit_frame = res > 0;
2401 return frame_time;
2404 static void pause_loop(void)
2406 mp_cmd_t* cmd;
2407 if (!quiet) {
2408 // Small hack to display the pause message on the OSD line.
2409 // The pause string is: "\n == PAUSE == \r" so we need to
2410 // take the first and the last char out
2411 if (term_osd && !mpctx->sh_video) {
2412 char msg[128] = MSGTR_Paused;
2413 int mlen = strlen(msg);
2414 msg[mlen-1] = '\0';
2415 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2416 update_osd_msg();
2417 } else
2418 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2419 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2421 #ifdef CONFIG_GUI
2422 if (use_gui)
2423 guiGetEvent(guiCEvent, (char *)guiSetPause);
2424 #endif
2425 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2426 mpctx->video_out->control(VOCTRL_PAUSE, NULL);
2428 if (mpctx->audio_out && mpctx->sh_audio)
2429 mpctx->audio_out->pause(); // pause audio, keep data if possible
2431 while ( (cmd = mp_input_get_cmd(20, 1, 1)) == NULL || cmd->pausing == 4) {
2432 if (cmd) {
2433 cmd = mp_input_get_cmd(0,1,0);
2434 run_command(mpctx, cmd);
2435 mp_cmd_free(cmd);
2436 continue;
2438 if (mpctx->sh_video && mpctx->video_out && vo_config_count)
2439 mpctx->video_out->check_events();
2440 #ifdef CONFIG_GUI
2441 if (use_gui) {
2442 guiEventHandling();
2443 guiGetEvent(guiReDraw, NULL);
2444 if (guiIntfStruct.Playing!=2 || (rel_seek_secs || abs_seek_pos))
2445 break;
2447 #endif
2448 #ifdef CONFIG_MENU
2449 if (vf_menu)
2450 vf_menu_pause_update(vf_menu);
2451 #endif
2452 usec_sleep(20000);
2454 if (cmd && cmd->id == MP_CMD_PAUSE) {
2455 cmd = mp_input_get_cmd(0,1,0);
2456 mp_cmd_free(cmd);
2458 mpctx->osd_function=OSD_PLAY;
2459 if (mpctx->audio_out && mpctx->sh_audio) {
2460 if (mpctx->eof) // do not play remaining audio if we e.g. switch to the next file
2461 mpctx->audio_out->reset();
2462 else
2463 mpctx->audio_out->resume(); // resume audio
2465 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2466 mpctx->video_out->control(VOCTRL_RESUME, NULL); // resume video
2467 (void)GetRelativeTime(); // ignore time that passed during pause
2468 #ifdef CONFIG_GUI
2469 if (use_gui) {
2470 if (guiIntfStruct.Playing == guiSetStop)
2471 mpctx->eof = 1;
2472 else
2473 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2475 #endif
2479 // Find the right mute status and record position for new file position
2480 static void edl_seek_reset(MPContext *mpctx)
2482 mpctx->edl_muted = 0;
2483 next_edl_record = edl_records;
2485 while (next_edl_record) {
2486 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2487 break;
2489 if (next_edl_record->action == EDL_MUTE)
2490 mpctx->edl_muted = !mpctx->edl_muted;
2491 next_edl_record = next_edl_record->next;
2493 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2494 mixer_mute(&mpctx->mixer);
2498 // Execute EDL command for the current position if one exists
2499 static void edl_update(MPContext *mpctx)
2501 if (!next_edl_record)
2502 return;
2504 if (!mpctx->sh_video) {
2505 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2506 free_edl(edl_records);
2507 next_edl_record = NULL;
2508 edl_records = NULL;
2509 return;
2512 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2513 if (next_edl_record->action == EDL_SKIP) {
2514 mpctx->osd_function = OSD_FFW;
2515 abs_seek_pos = 0;
2516 rel_seek_secs = next_edl_record->length_sec;
2517 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2518 "[%f], length [%f]\n", next_edl_record->start_sec,
2519 next_edl_record->stop_sec, next_edl_record->length_sec);
2520 edl_decision = 1;
2522 else if (next_edl_record->action == EDL_MUTE) {
2523 mpctx->edl_muted = !mpctx->edl_muted;
2524 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2525 mixer_mute(&mpctx->mixer);
2526 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2527 next_edl_record->start_sec );
2529 next_edl_record = next_edl_record->next;
2534 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2535 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2536 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2537 static int seek(MPContext *mpctx, double amount, int style)
2539 current_module = "seek";
2540 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2541 return -1;
2543 mpctx->startup_decode_retry = DEFAULT_STARTUP_DECODE_RETRY;
2544 if (mpctx->sh_video) {
2545 current_module = "seek_video_reset";
2546 resync_video_stream(mpctx->sh_video);
2547 if (vo_config_count)
2548 mpctx->video_out->control(VOCTRL_RESET, NULL);
2549 mpctx->sh_video->next_frame_time = 0;
2550 mpctx->sh_video->num_buffered_pts = 0;
2551 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2552 mpctx->num_buffered_frames = 0;
2553 mpctx->delay = 0;
2554 mpctx->time_frame = 0;
2555 // Not all demuxers set d_video->pts during seek, so this value
2556 // (which is used by at least vobsub and edl code below) may
2557 // be completely wrong (probably 0).
2558 mpctx->sh_video->pts = mpctx->d_video->pts;
2559 update_subtitles(mpctx->sh_video, mpctx->sh_video->pts, mpctx->d_sub, 1);
2560 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2563 if (mpctx->sh_audio) {
2564 current_module = "seek_audio_reset";
2565 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2566 mpctx->sh_audio->a_buffer_len = 0;
2567 mpctx->sh_audio->a_out_buffer_len = 0;
2568 if (!mpctx->sh_video)
2569 update_subtitles(NULL, mpctx->sh_audio->pts, mpctx->d_sub, 1);
2572 if (vo_vobsub && mpctx->sh_video) {
2573 current_module = "seek_vobsub_reset";
2574 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2577 edl_seek_reset(mpctx);
2579 c_total = 0;
2580 max_pts_correction = 0.1;
2581 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2582 drop_frame_cnt = 0;
2584 current_module = NULL;
2585 return 0;
2588 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2589 * file for some tools to link against. */
2590 #ifndef DISABLE_MAIN
2591 int main(int argc,char* argv[]){
2594 char * mem_ptr;
2596 // movie info:
2598 /* Flag indicating whether MPlayer should exit without playing anything. */
2599 int opt_exit = 0;
2601 //float a_frame=0; // Audio
2603 int i;
2605 int gui_no_filename=0;
2607 InitTimer();
2608 srand(GetTimerMS());
2610 mp_msg_init();
2612 // Create the config context and register the options
2613 mconfig = m_config_new();
2614 m_config_register_options(mconfig,mplayer_opts);
2615 mp_input_register_options(mconfig);
2617 // Preparse the command line
2618 m_config_preparse_command_line(mconfig,argc,argv);
2620 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2621 set_path_env();
2622 #endif
2624 #ifdef CONFIG_TV
2625 stream_tv_defaults.immediate = 1;
2626 #endif
2628 if (argc > 1 && argv[1] &&
2629 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2630 use_gui = !strcmp(argv[1], "-gui");
2631 } else
2632 if ( argv[0] )
2634 char *base = strrchr(argv[0], '/');
2635 if (!base)
2636 base = strrchr(argv[0], '\\');
2637 if (!base)
2638 base = argv[0];
2639 if(strstr(base, "gmplayer"))
2640 use_gui=1;
2643 parse_cfgfiles(mconfig);
2645 #ifdef CONFIG_GUI
2646 if ( use_gui ) cfg_read();
2647 #endif
2649 mpctx->playtree = m_config_parse_mp_command_line(mconfig, argc, argv);
2650 if(mpctx->playtree == NULL)
2651 opt_exit = 1;
2652 else {
2653 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2654 if(mpctx->playtree) {
2655 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
2656 if(mpctx->playtree_iter) {
2657 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2658 play_tree_iter_free(mpctx->playtree_iter);
2659 mpctx->playtree_iter = NULL;
2661 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2666 print_version("MPlayer");
2668 #if defined(__MINGW32__) || defined(__CYGWIN__)
2669 #ifdef CONFIG_GUI
2670 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2671 if(runningmplayer && filename && use_gui){
2672 COPYDATASTRUCT csData;
2673 char file[MAX_PATH];
2674 char *filepart = filename;
2675 if(GetFullPathName(filename, MAX_PATH, file, &filepart)){
2676 csData.dwData = 0;
2677 csData.cbData = strlen(file)*2;
2678 csData.lpData = file;
2679 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2682 #endif
2685 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
2686 BOOL WINAPI (*setDEP)(DWORD) = NULL;
2687 if (kernel32)
2688 setDEP = GetProcAddress(kernel32, "SetProcessDEPPolicy");
2689 if (setDEP) setDEP(3);
2691 // stop Windows from showing all kinds of annoying error dialogs
2692 SetErrorMode(0x8003);
2693 // request 1ms timer resolution
2694 timeBeginPeriod(1);
2695 #endif
2697 #ifdef CONFIG_PRIORITY
2698 set_priority();
2699 #endif
2701 #ifndef CONFIG_GUI
2702 if(use_gui){
2703 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2704 use_gui=0;
2706 #else
2707 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
2708 if(use_gui && !vo_init()){
2709 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2710 use_gui=0;
2712 #endif
2713 if (use_gui && mpctx->playtree_iter){
2714 char cwd[PATH_MAX+2];
2715 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2716 play_tree_iter_free(mpctx->playtree_iter);
2717 mpctx->playtree_iter=NULL;
2719 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2721 strcat(cwd, "/");
2722 // Prefix relative paths with current working directory
2723 play_tree_add_bpf(mpctx->playtree, cwd);
2725 // Import initital playtree into GUI.
2726 import_initial_playtree_into_gui(mpctx->playtree, mconfig, enqueue);
2728 #endif /* CONFIG_GUI */
2730 if(video_driver_list && strcmp(video_driver_list[0],"help")==0){
2731 list_video_out();
2732 opt_exit = 1;
2735 if(audio_driver_list && strcmp(audio_driver_list[0],"help")==0){
2736 list_audio_out();
2737 opt_exit = 1;
2740 /* Check codecs.conf. */
2741 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2742 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2743 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2744 if(!parse_codec_cfg(NULL)){
2745 exit_player_with_rc(EXIT_NONE, 0);
2747 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2750 free( mem_ptr ); // release the buffer created by get_path()
2753 #if 0
2754 if(video_codec_list){
2755 int i;
2756 video_codec=video_codec_list[0];
2757 for(i=0;video_codec_list[i];i++)
2758 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2760 #endif
2761 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2762 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2763 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2764 list_codecs(1);
2765 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2766 opt_exit = 1;
2768 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2769 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2770 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2771 list_codecs(0);
2772 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2773 opt_exit = 1;
2775 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2776 vfm_help();
2777 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2778 opt_exit = 1;
2780 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2781 afm_help();
2782 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2783 opt_exit = 1;
2785 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2786 af_help();
2787 printf("\n");
2788 opt_exit = 1;
2790 #ifdef CONFIG_X11
2791 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2792 fstype_help();
2793 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2794 opt_exit = 1;
2796 #endif
2797 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2798 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2799 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2800 demuxer_help();
2801 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2802 opt_exit = 1;
2804 if(list_properties) {
2805 property_print_help();
2806 opt_exit = 1;
2809 if(opt_exit)
2810 exit_player(EXIT_NONE);
2812 if (player_idle_mode && use_gui) {
2813 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2814 exit_player_with_rc(EXIT_NONE, 1);
2817 if(!filename && !player_idle_mode){
2818 if(!use_gui){
2819 // no file/vcd/dvd -> show HELP:
2820 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2821 exit_player_with_rc(EXIT_NONE, 0);
2822 } else gui_no_filename=1;
2825 /* Display what configure line was used */
2826 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2828 // Many users forget to include command line in bugreports...
2829 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2830 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2831 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2832 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2835 //------ load global data first ------
2837 // check font
2838 #ifdef CONFIG_FREETYPE
2839 init_freetype();
2840 #endif
2841 #ifdef CONFIG_FONTCONFIG
2842 if(font_fontconfig <= 0)
2844 #endif
2845 #ifdef CONFIG_BITMAP_FONT
2846 if(font_name){
2847 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2848 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2849 filename_recode(font_name));
2850 } else {
2851 // try default:
2852 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2853 free(mem_ptr); // release the buffer created by get_path()
2854 if(!vo_font)
2855 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2857 if (sub_font_name)
2858 sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2859 else
2860 sub_font = vo_font;
2861 #endif
2862 #ifdef CONFIG_FONTCONFIG
2864 #endif
2866 vo_init_osd();
2868 #ifdef CONFIG_ASS
2869 ass_library = ass_init();
2870 #endif
2872 #ifdef HAVE_RTC
2873 if(!nortc)
2875 // seteuid(0); /* Can't hurt to try to get root here */
2876 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2877 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2878 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2879 else {
2880 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2882 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2883 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2884 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2885 close (rtc_fd);
2886 rtc_fd = -1;
2887 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2888 /* variable only by the root */
2889 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2890 close (rtc_fd);
2891 rtc_fd = -1;
2892 } else
2893 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2896 #ifdef CONFIG_GUI
2897 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2898 // and now ? -- Pontscho
2899 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2900 #endif
2901 if(rtc_fd<0)
2902 #endif /* HAVE_RTC */
2903 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2904 softsleep?"software":timer_name);
2906 #ifdef HAVE_TERMCAP
2907 if ( !use_gui ) load_termcap(NULL); // load key-codes
2908 #endif
2910 // ========== Init keyboard FIFO (connection to libvo) ============
2912 // Init input system
2913 current_module = "init_input";
2914 mp_input_init(use_gui);
2915 mp_input_add_key_fd(-1,0,mplayer_get_key,NULL);
2916 if(slave_mode)
2917 mp_input_add_cmd_fd(0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
2918 else if(!noconsolecontrols)
2919 mp_input_add_event_fd(0, getch2);
2920 // Set the libstream interrupt callback
2921 stream_set_interrupt_callback(mp_input_check_interrupt);
2923 #ifdef CONFIG_MENU
2924 if(use_menu) {
2925 if(menu_cfg && menu_init(mpctx, menu_cfg))
2926 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_MenuInitialized, menu_cfg);
2927 else {
2928 menu_cfg = get_path("menu.conf");
2929 if(menu_init(mpctx, menu_cfg))
2930 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_MenuInitialized, menu_cfg);
2931 else {
2932 if(menu_init(mpctx, MPLAYER_CONFDIR "/menu.conf"))
2933 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2934 else {
2935 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_MenuInitFailed);
2936 use_menu = 0;
2941 #endif
2943 initialized_flags|=INITIALIZED_INPUT;
2944 current_module = NULL;
2946 /// Catch signals
2947 #ifndef __MINGW32__
2948 signal(SIGCHLD,child_sighandler);
2949 #endif
2951 #ifdef CONFIG_CRASH_DEBUG
2952 prog_path = argv[0];
2953 #endif
2954 //========= Catch terminate signals: ================
2955 // terminate requests:
2956 signal(SIGTERM,exit_sighandler); // kill
2957 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2959 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2961 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2962 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2963 #ifdef CONFIG_SIGHANDLER
2964 // fatal errors:
2965 signal(SIGBUS,exit_sighandler); // bus error
2966 signal(SIGSEGV,exit_sighandler); // segfault
2967 signal(SIGILL,exit_sighandler); // illegal instruction
2968 signal(SIGFPE,exit_sighandler); // floating point exc.
2969 signal(SIGABRT,exit_sighandler); // abort()
2970 #ifdef CONFIG_CRASH_DEBUG
2971 if (crash_debug)
2972 signal(SIGTRAP,exit_sighandler);
2973 #endif
2974 #endif
2976 #ifdef CONFIG_GUI
2977 if(use_gui){
2978 guiInit();
2979 guiGetEvent(guiSetContext, mpctx);
2980 initialized_flags|=INITIALIZED_GUI;
2981 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2983 #endif
2985 // ******************* Now, let's see the per-file stuff ********************
2987 play_next_file:
2989 // init global sub numbers
2990 mpctx->global_sub_size = 0;
2991 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2993 if (filename) {
2994 load_per_protocol_config (mconfig, filename);
2995 load_per_extension_config (mconfig, filename);
2996 load_per_file_config (mconfig, filename);
2999 if (video_driver_list)
3000 load_per_output_config (mconfig, PROFILE_CFG_VO, video_driver_list[0]);
3001 if (audio_driver_list)
3002 load_per_output_config (mconfig, PROFILE_CFG_AO, audio_driver_list[0]);
3004 // We must enable getch2 here to be able to interrupt network connection
3005 // or cache filling
3006 if(!noconsolecontrols && !slave_mode){
3007 if(initialized_flags&INITIALIZED_GETCH2)
3008 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
3009 else
3010 getch2_enable(); // prepare stdin for hotkeys...
3011 initialized_flags|=INITIALIZED_GETCH2;
3012 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3015 // =================== GUI idle loop (STOP state) ===========================
3016 #ifdef CONFIG_GUI
3017 if ( use_gui ) {
3018 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
3019 guiGetEvent( guiSetDefaults,0 );
3020 while ( guiIntfStruct.Playing != 1 )
3022 mp_cmd_t* cmd;
3023 usec_sleep(20000);
3024 guiEventHandling();
3025 guiGetEvent( guiReDraw,NULL );
3026 if ( (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
3027 guiGetEvent(guiIEvent, (char *)cmd->id);
3028 mp_cmd_free(cmd);
3031 guiGetEvent( guiSetParameters,NULL );
3032 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
3034 play_tree_t * entry = play_tree_new();
3035 play_tree_add_file( entry,guiIntfStruct.Filename );
3036 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
3037 else mpctx->playtree=play_tree_new();
3038 play_tree_set_child( mpctx->playtree,entry );
3039 if(mpctx->playtree)
3041 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
3042 if(mpctx->playtree_iter)
3044 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
3046 play_tree_iter_free(mpctx->playtree_iter);
3047 mpctx->playtree_iter = NULL;
3049 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3054 #endif /* CONFIG_GUI */
3056 while (player_idle_mode && !filename) {
3057 play_tree_t * entry = NULL;
3058 mp_cmd_t * cmd;
3059 while (!(cmd = mp_input_get_cmd(0,1,0))) { // wait for command
3060 if (mpctx->video_out && vo_config_count) mpctx->video_out->check_events();
3061 usec_sleep(20000);
3063 switch (cmd->id) {
3064 case MP_CMD_LOADFILE:
3065 // prepare a tree entry with the new filename
3066 entry = play_tree_new();
3067 play_tree_add_file(entry, cmd->args[0].v.s);
3068 // The entry is added to the main playtree after the switch().
3069 break;
3070 case MP_CMD_LOADLIST:
3071 entry = parse_playlist_file(cmd->args[0].v.s);
3072 break;
3073 case MP_CMD_QUIT:
3074 exit_player_with_rc(EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3075 break;
3076 case MP_CMD_GET_PROPERTY:
3077 case MP_CMD_SET_PROPERTY:
3078 case MP_CMD_STEP_PROPERTY:
3079 run_command(mpctx, cmd);
3080 break;
3083 mp_cmd_free(cmd);
3085 if (entry) { // user entered a command that gave a valid entry
3086 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3087 play_tree_free_list(mpctx->playtree->child, 1);
3088 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3090 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3092 play_tree_set_child(mpctx->playtree, entry);
3094 /* Make iterator start at the top the of tree. */
3095 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mconfig);
3096 if (!mpctx->playtree_iter) continue;
3098 // find the first real item in the tree
3099 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3100 // no items!
3101 play_tree_iter_free(mpctx->playtree_iter);
3102 mpctx->playtree_iter = NULL;
3103 continue; // wait for next command
3105 filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3108 //---------------------------------------------------------------------------
3110 if(filename) {
3111 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
3112 filename_recode(filename));
3113 if(use_filename_title && vo_wintitle == NULL)
3114 vo_wintitle = strdup ( mp_basename2 (filename));
3117 if (edl_filename) {
3118 if (edl_records) free_edl(edl_records);
3119 next_edl_record = edl_records = edl_parse_file();
3121 if (edl_output_filename) {
3122 if (edl_fd) fclose(edl_fd);
3123 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3125 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
3126 filename_recode(edl_output_filename));
3130 //==================== Open VOB-Sub ============================
3132 current_module="vobsub";
3133 if (vobsub_name){
3134 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3135 if(vo_vobsub==NULL)
3136 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
3137 filename_recode(vobsub_name));
3138 } else if (sub_auto && filename){
3139 /* try to autodetect vobsub from movie filename ::atmos */
3140 char *buf = strdup(filename), *psub;
3141 char *pdot = strrchr(buf, '.');
3142 char *pslash = strrchr(buf, '/');
3143 #if defined(__MINGW32__) || defined(__CYGWIN__)
3144 if (!pslash) pslash = strrchr(buf, '\\');
3145 #endif
3146 if (pdot && (!pslash || pdot > pslash))
3147 *pdot = '\0';
3148 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3149 /* try from ~/.mplayer/sub */
3150 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3151 char *bname;
3152 int l;
3153 bname = strrchr(buf,'/');
3154 #if defined(__MINGW32__) || defined(__CYGWIN__)
3155 if(!bname) bname = strrchr(buf,'\\');
3156 #endif
3157 if(bname) bname++;
3158 else bname = buf;
3159 l = strlen(psub) + strlen(bname) + 1;
3160 psub = realloc(psub,l);
3161 strcat(psub,bname);
3162 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3163 free(psub);
3165 free(buf);
3167 if(vo_vobsub){
3168 initialized_flags|=INITIALIZED_VOBSUB;
3169 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3170 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3172 // setup global sub numbering
3173 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3174 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3177 //============ Open & Sync STREAM --- fork cache2 ====================
3179 mpctx->stream=NULL;
3180 mpctx->demuxer=NULL;
3181 if (mpctx->d_audio) {
3182 //free_demuxer_stream(mpctx->d_audio);
3183 mpctx->d_audio=NULL;
3185 if (mpctx->d_video) {
3186 //free_demuxer_stream(d_video);
3187 mpctx->d_video=NULL;
3189 mpctx->sh_audio=NULL;
3190 mpctx->sh_video=NULL;
3192 current_module="open_stream";
3193 mpctx->stream=open_stream(filename,0,&mpctx->file_format);
3194 if(!mpctx->stream) { // error...
3195 mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY);
3196 goto goto_next_file;
3198 initialized_flags|=INITIALIZED_STREAM;
3200 #ifdef CONFIG_GUI
3201 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3202 #endif
3204 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3205 play_tree_t* entry;
3206 // Handle playlist
3207 current_module="handle_playlist";
3208 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3209 filename_recode(filename));
3210 entry = parse_playtree(mpctx->stream,0);
3211 mpctx->eof=playtree_add_playlist(entry);
3212 goto goto_next_file;
3214 mpctx->stream->start_pos+=seek_to_byte;
3216 if(stream_dump_type==5){
3217 unsigned char buf[4096];
3218 int len;
3219 FILE *f;
3220 current_module="dumpstream";
3221 stream_reset(mpctx->stream);
3222 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3223 f=fopen(stream_dump_name,"wb");
3224 if(!f){
3225 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3226 exit_player(EXIT_ERROR);
3228 if (dvd_chapter > 1) {
3229 int chapter = dvd_chapter - 1;
3230 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3232 while(!mpctx->stream->eof && !async_quit_request){
3233 len=stream_read(mpctx->stream,buf,4096);
3234 if(len>0) {
3235 if(fwrite(buf,len,1,f) != 1) {
3236 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3237 exit_player(EXIT_ERROR);
3240 if(dvd_last_chapter > 0) {
3241 int chapter = -1;
3242 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3243 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3244 break;
3247 if(fclose(f)) {
3248 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3249 exit_player(EXIT_ERROR);
3251 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3252 exit_player_with_rc(EXIT_EOF, 0);
3255 #ifdef CONFIG_DVDREAD
3256 if(mpctx->stream->type==STREAMTYPE_DVD){
3257 current_module="dvd lang->id";
3258 if(audio_id==-1) audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3259 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3260 // setup global sub numbering
3261 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3262 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3263 current_module=NULL;
3265 #endif
3267 #ifdef CONFIG_DVDNAV
3268 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3269 current_module="dvdnav lang->id";
3270 if(audio_id==-1) audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3271 dvdsub_lang_id = -3;
3272 if(dvdsub_lang && dvdsub_id==-1)
3273 dvdsub_lang_id=dvdsub_id=mp_dvdnav_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 += mp_dvdnav_number_of_subs(mpctx->stream);
3277 current_module=NULL;
3279 #endif
3281 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3282 goto_enable_cache:
3283 if(stream_cache_size>0){
3284 current_module="enable_cache";
3285 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3286 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3287 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3288 if((mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY))) goto goto_next_file;
3291 //============ Open DEMUXERS --- DETECT file type =======================
3292 current_module="demux_open";
3294 mpctx->demuxer=demux_open(mpctx->stream,mpctx->file_format,audio_id,video_id,dvdsub_id,filename);
3296 // HACK to get MOV Reference Files working
3298 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3300 unsigned char* playlist_entry;
3301 play_tree_t *list = NULL, *entry = NULL;
3303 current_module="handle_demux_playlist";
3304 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3306 char *temp, *bname;
3308 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3309 filename_recode(playlist_entry));
3311 bname=mp_basename(playlist_entry);
3312 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3313 continue;
3315 if (!strcmp(playlist_entry,filename)) // ignoring self-reference
3316 continue;
3318 entry = play_tree_new();
3320 if (filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3322 temp=malloc((strlen(filename)-strlen(mp_basename(filename))+strlen(playlist_entry)+1));
3323 if (temp)
3325 strncpy(temp, filename, strlen(filename)-strlen(mp_basename(filename)));
3326 temp[strlen(filename)-strlen(mp_basename(filename))]='\0';
3327 strcat(temp, playlist_entry);
3328 if (!strcmp(temp, filename)) {
3329 free(temp);
3330 continue;
3332 play_tree_add_file(entry,temp);
3333 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3334 free(temp);
3337 else
3338 play_tree_add_file(entry,playlist_entry);
3340 if(!list)
3341 list = entry;
3342 else
3343 play_tree_append_entry(list,entry);
3345 free_demuxer(mpctx->demuxer);
3346 mpctx->demuxer = NULL;
3348 if (list)
3350 entry = play_tree_new();
3351 play_tree_set_child(entry,list);
3352 mpctx->eof=playtree_add_playlist(entry);
3353 goto goto_next_file;
3357 if(!mpctx->demuxer)
3358 goto goto_next_file;
3359 if(dvd_chapter>1) {
3360 float pts;
3361 if (demuxer_seek_chapter(mpctx->demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
3362 seek(mpctx, pts, SEEK_ABSOLUTE);
3365 initialized_flags|=INITIALIZED_DEMUXER;
3367 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3368 int i;
3369 int maxid = -1;
3370 // setup global sub numbering
3371 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3372 for (i = 0; i < MAX_S_STREAMS; i++)
3373 if (mpctx->demuxer->s_streams[i])
3374 maxid = FFMAX(maxid, ((sh_sub_t *)mpctx->demuxer->s_streams[i])->sid);
3375 mpctx->global_sub_size += maxid + 1;
3377 // Make dvdsub_id always selectable if set.
3378 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id)
3379 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id + 1;
3381 #ifdef CONFIG_ASS
3382 if (ass_enabled && ass_library) {
3383 for (i = 0; i < mpctx->demuxer->num_attachments; ++i) {
3384 demux_attachment_t* att = mpctx->demuxer->attachments + i;
3385 if (extract_embedded_fonts &&
3386 att->name && att->type && att->data && att->data_size &&
3387 (strcmp(att->type, "application/x-truetype-font") == 0 ||
3388 strcmp(att->type, "application/x-font") == 0))
3389 ass_add_font(ass_library, att->name, att->data, att->data_size);
3392 #endif
3394 current_module="demux_open2";
3396 //file_format=demuxer->file_format;
3398 mpctx->d_audio=mpctx->demuxer->audio;
3399 mpctx->d_video=mpctx->demuxer->video;
3400 mpctx->d_sub=mpctx->demuxer->sub;
3402 if (ts_prog) {
3403 int tmp = ts_prog;
3404 mp_property_do("switch_program", M_PROPERTY_SET, &tmp, mpctx);
3406 // select audio stream
3407 select_audio(mpctx->demuxer, audio_id, audio_lang);
3409 // DUMP STREAMS:
3410 if((stream_dump_type)&&(stream_dump_type<4)){
3411 FILE *f;
3412 demux_stream_t *ds=NULL;
3413 current_module="dump";
3414 // select stream to dump
3415 switch(stream_dump_type){
3416 case 1: ds=mpctx->d_audio;break;
3417 case 2: ds=mpctx->d_video;break;
3418 case 3: ds=mpctx->d_sub;break;
3420 if(!ds){
3421 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3422 exit_player(EXIT_ERROR);
3424 // disable other streams:
3425 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3426 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3427 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3428 // let's dump it!
3429 f=fopen(stream_dump_name,"wb");
3430 if(!f){
3431 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3432 exit_player(EXIT_ERROR);
3434 while(!ds->eof){
3435 unsigned char* start;
3436 int in_size=ds_get_packet(ds,&start);
3437 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3438 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3439 if(in_size>0) fwrite(start,in_size,1,f);
3440 if(dvd_last_chapter>0) {
3441 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3442 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3443 break;
3446 fclose(f);
3447 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3448 exit_player_with_rc(EXIT_EOF, 0);
3451 mpctx->sh_audio=mpctx->d_audio->sh;
3452 mpctx->sh_video=mpctx->d_video->sh;
3454 if(mpctx->sh_video){
3456 current_module="video_read_properties";
3457 if(!video_read_properties(mpctx->sh_video)) {
3458 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3459 mpctx->sh_video=mpctx->d_video->sh=NULL;
3460 } else {
3461 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_FilefmtFourccSizeFpsFtime,
3462 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3463 mpctx->sh_video->fps,mpctx->sh_video->frametime
3466 /* need to set fps here for output encoders to pick it up in their init */
3467 if(force_fps){
3468 mpctx->sh_video->fps=force_fps;
3469 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3471 vo_fps = mpctx->sh_video->fps;
3473 if(!mpctx->sh_video->fps && !force_fps){
3474 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3475 mpctx->sh_video=mpctx->d_video->sh=NULL;
3481 if(!mpctx->sh_video && !mpctx->sh_audio){
3482 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3483 #ifdef CONFIG_DVBIN
3484 if(mpctx->stream->type == STREAMTYPE_DVB)
3486 int dir;
3487 int v = mpctx->last_dvb_step;
3488 if(v > 0)
3489 dir = DVB_CHANNEL_HIGHER;
3490 else
3491 dir = DVB_CHANNEL_LOWER;
3493 if(dvb_step_channel(mpctx->stream, dir))
3494 mpctx->eof = mpctx->dvbin_reopen = 1;
3496 #endif
3497 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3500 /* display clip info */
3501 demux_info_print(mpctx->demuxer);
3503 //================== Read SUBTITLES (DVD & TEXT) ==========================
3504 if(vo_spudec==NULL && mpctx->sh_video &&
3505 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3506 init_vo_spudec();
3509 if(1 || mpctx->sh_video) {
3510 // after reading video params we should load subtitles because
3511 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3512 // check .sub
3513 double fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
3514 current_module="read_subtitles_file";
3515 if(sub_name){
3516 for (i = 0; sub_name[i] != NULL; ++i)
3517 add_subtitles (sub_name[i], fps, 0);
3519 if(sub_auto) { // auto load sub file ...
3520 char *psub = get_path( "sub/" );
3521 char **tmp = sub_filenames((psub ? psub : ""), filename);
3522 int i = 0;
3523 free(psub); // release the buffer created by get_path() above
3524 while (tmp[i]) {
3525 add_subtitles (tmp[i], fps, 1);
3526 free(tmp[i++]);
3528 free(tmp);
3530 if (mpctx->set_of_sub_size > 0) {
3531 // setup global sub numbering
3532 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3533 mpctx->global_sub_size += mpctx->set_of_sub_size;
3537 if (mpctx->global_sub_size) {
3538 select_subtitle(mpctx);
3539 if(subdata)
3540 switch (stream_dump_type) {
3541 case 3: list_sub_file(subdata); break;
3542 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3543 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3544 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3545 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3546 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3550 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3551 filename_recode(filename));
3552 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3553 if (mpctx->sh_video) {
3554 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3555 if (mpctx->sh_video->format >= 0x20202020)
3556 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3557 else
3558 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3559 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3560 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3561 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3562 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3563 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3565 if (mpctx->sh_audio) {
3566 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3567 if (mpctx->sh_audio->format >= 0x20202020)
3568 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3569 else
3570 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3571 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3572 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3573 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3575 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3576 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n",
3577 mpctx->stream->seek && (!mpctx->demuxer || mpctx->demuxer->seekable));
3578 if (mpctx->demuxer) {
3579 if (mpctx->demuxer->num_chapters == 0)
3580 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3581 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3584 if(!mpctx->sh_video) goto main; // audio-only
3586 if(!reinit_video_chain()) {
3587 if(!mpctx->sh_video){
3588 if(!mpctx->sh_audio) goto goto_next_file;
3589 goto main; // exit_player(MSGTR_Exit_error);
3593 if(vo_flags & 0x08 && vo_spudec)
3594 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3596 #ifdef CONFIG_FREETYPE
3597 force_load_font = 1;
3598 #endif
3600 //================== MAIN: ==========================
3601 main:
3602 current_module="main";
3604 if(playing_msg) {
3605 char* msg = property_expand_string(mpctx, playing_msg);
3606 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3607 free(msg);
3611 // Disable the term OSD in verbose mode
3612 if(verbose) term_osd = 0;
3615 //int frame_corr_num=0; //
3616 //float v_frame=0; // Video
3617 //float num_frames=0; // number of frames played
3619 int frame_time_remaining=0; // flag
3620 int blit_frame=0;
3621 mpctx->num_buffered_frames=0;
3623 // Make sure old OSD does not stay around,
3624 // e.g. with -fixed-vo and same-resolution files
3625 clear_osd_msgs();
3626 update_osd_msg();
3628 //================ SETUP AUDIO ==========================
3630 if(mpctx->sh_audio){
3631 reinit_audio_chain();
3632 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3633 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3636 current_module="av_init";
3638 if(mpctx->sh_video){
3639 mpctx->sh_video->timer=0;
3640 if (! ignore_start)
3641 audio_delay += mpctx->sh_video->stream_delay;
3643 if(mpctx->sh_audio){
3644 if (start_volume >= 0)
3645 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3646 if (! ignore_start)
3647 audio_delay -= mpctx->sh_audio->stream_delay;
3648 mpctx->delay=-audio_delay;
3651 if(!mpctx->sh_audio){
3652 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3653 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3654 ds_free_packs(mpctx->d_audio); // free buffered chunks
3655 //mpctx->d_audio->id=-2; // do not read audio chunks
3656 //uninit_player(INITIALIZED_AO); // close device
3658 if(!mpctx->sh_video){
3659 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3660 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3661 ds_free_packs(mpctx->d_video);
3662 mpctx->d_video->id=-2;
3663 //if(!fixed_vo) uninit_player(INITIALIZED_VO);
3666 if (!mpctx->sh_video && !mpctx->sh_audio)
3667 goto goto_next_file;
3669 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3670 if(force_fps && mpctx->sh_video){
3671 vo_fps = mpctx->sh_video->fps=force_fps;
3672 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3673 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3676 #ifdef CONFIG_GUI
3677 if ( use_gui ) {
3678 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3679 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3680 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3681 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3682 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3684 #endif
3686 mp_input_set_section(NULL);
3687 //TODO: add desired (stream-based) sections here
3688 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section("tv");
3689 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section("dvdnav");
3691 //==================== START PLAYING =======================
3693 if(mpctx->loop_times>1) mpctx->loop_times--; else
3694 if(mpctx->loop_times==1) mpctx->loop_times = -1;
3696 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3698 total_time_usage_start=GetTimer();
3699 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3700 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3701 play_n_frames=play_n_frames_mf;
3702 mpctx->startup_decode_retry = DEFAULT_STARTUP_DECODE_RETRY;
3704 if(play_n_frames==0){
3705 mpctx->eof=PT_NEXT_ENTRY; goto goto_next_file;
3708 if (seek_to_sec) {
3709 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3710 end_at.pos += seek_to_sec;
3713 if (end_at.type == END_AT_SIZE) {
3714 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3715 end_at.type = END_AT_NONE;
3718 #ifdef CONFIG_DVDNAV
3719 mp_dvdnav_context_free(mpctx);
3720 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3721 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3722 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3724 #endif
3726 while(!mpctx->eof){
3727 float aq_sleep_time=0;
3729 if(dvd_last_chapter>0) {
3730 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3731 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3732 goto goto_next_file;
3735 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3736 mpctx->sh_audio = mpctx->d_audio->sh;
3737 mpctx->sh_audio->ds = mpctx->d_audio;
3738 reinit_audio_chain();
3741 /*========================== PLAY AUDIO ============================*/
3743 if (mpctx->sh_audio)
3744 if (!fill_audio_out_buffers())
3745 // at eof, all audio at least written to ao
3746 if (!mpctx->sh_video)
3747 mpctx->eof = PT_NEXT_ENTRY;
3750 if(!mpctx->sh_video) {
3751 // handle audio-only case:
3752 double a_pos=0;
3753 // sh_audio can be NULL due to video stream switching
3754 // TODO: handle this better
3755 if((!quiet || end_at.type == END_AT_TIME) && mpctx->sh_audio)
3756 a_pos = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3758 if(!quiet)
3759 print_status(a_pos, 0, 0);
3761 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3762 mpctx->eof = PT_NEXT_ENTRY;
3763 update_subtitles(NULL, a_pos, mpctx->d_sub, 0);
3764 update_osd_msg();
3766 } else {
3768 /*========================== PLAY VIDEO ============================*/
3770 vo_pts=mpctx->sh_video->timer*90000.0;
3771 vo_fps=mpctx->sh_video->fps;
3773 if (!mpctx->num_buffered_frames) {
3774 double frame_time = update_video(&blit_frame);
3775 while (!blit_frame && mpctx->startup_decode_retry > 0) {
3776 double delay = mpctx->delay;
3777 // these initial decode failures are probably due to codec delay,
3778 // ignore them and also their probably nonsense durations
3779 update_video(&blit_frame);
3780 mpctx->delay = delay;
3781 mpctx->startup_decode_retry--;
3783 mpctx->startup_decode_retry = 0;
3784 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3785 if (mpctx->sh_video->vf_initialized < 0) {
3786 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3787 mpctx->eof = 1; goto goto_next_file;
3789 if (frame_time < 0)
3790 mpctx->eof = 1;
3791 else {
3792 // might return with !eof && !blit_frame if !correct_pts
3793 mpctx->num_buffered_frames += blit_frame;
3794 mpctx->time_frame += frame_time / playback_speed; // for nosound
3798 // ==========================================================================
3800 // current_module="draw_osd";
3801 // if(vo_config_count) mpctx->video_out->draw_osd();
3803 #ifdef CONFIG_GUI
3804 if(use_gui) guiEventHandling();
3805 #endif
3807 current_module="vo_check_events";
3808 if (vo_config_count) mpctx->video_out->check_events();
3810 #ifdef CONFIG_X11
3811 if (stop_xscreensaver) {
3812 current_module = "stop_xscreensaver";
3813 xscreensaver_heartbeat();
3815 #endif
3816 if (heartbeat_cmd) {
3817 static unsigned last_heartbeat;
3818 unsigned now = GetTimerMS();
3819 if (now - last_heartbeat > 30000) {
3820 last_heartbeat = now;
3821 system(heartbeat_cmd);
3825 frame_time_remaining = sleep_until_update(&mpctx->time_frame, &aq_sleep_time);
3827 //====================== FLIP PAGE (VIDEO BLT): =========================
3829 current_module="flip_page";
3830 if (!frame_time_remaining && blit_frame) {
3831 unsigned int t2=GetTimer();
3833 if(vo_config_count) mpctx->video_out->flip_page();
3834 mpctx->num_buffered_frames--;
3836 vout_time_usage += (GetTimer() - t2) * 0.000001;
3838 //====================== A-V TIMESTAMP CORRECTION: =========================
3840 adjust_sync_and_print_status(frame_time_remaining, mpctx->time_frame);
3842 //============================ Auto QUALITY ============================
3844 /*Output quality adjustments:*/
3845 if(auto_quality>0){
3846 current_module="autoq";
3847 // float total=0.000001f * (GetTimer()-aq_total_time);
3848 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3849 if(output_quality<auto_quality && aq_sleep_time>0)
3850 ++output_quality;
3851 else
3852 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3853 if(output_quality>1 && aq_sleep_time<0)
3854 --output_quality;
3855 else
3856 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3857 output_quality=0;
3858 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3859 set_video_quality(mpctx->sh_video,output_quality);
3862 if (play_n_frames >= 0 && !frame_time_remaining && blit_frame) {
3863 --play_n_frames;
3864 if (play_n_frames <= 0) mpctx->eof = PT_NEXT_ENTRY;
3868 // FIXME: add size based support for -endpos
3869 if (end_at.type == END_AT_TIME &&
3870 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3871 mpctx->eof = PT_NEXT_ENTRY;
3873 } // end if(mpctx->sh_video)
3875 #ifdef CONFIG_DVDNAV
3876 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3877 nav_highlight_t hl;
3878 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3879 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3880 vo_osd_changed (OSDTYPE_DVDNAV);
3882 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
3883 double ar = -1.0;
3884 if (stream_control (mpctx->demuxer->stream,
3885 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
3886 != STREAM_UNSUPPORTED)
3887 mpctx->sh_video->stream_aspect = ar;
3890 #endif
3892 //============================ Handle PAUSE ===============================
3894 current_module="pause";
3896 if (mpctx->osd_function == OSD_PAUSE) {
3897 mpctx->was_paused = 1;
3898 pause_loop();
3901 // handle -sstep
3902 if(step_sec>0) {
3903 mpctx->osd_function=OSD_FFW;
3904 rel_seek_secs+=step_sec;
3907 edl_update(mpctx);
3909 //================= Keyboard events, SEEKing ====================
3911 current_module="key_events";
3914 mp_cmd_t* cmd;
3915 int brk_cmd = 0;
3916 while( !brk_cmd && (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
3917 brk_cmd = run_command(mpctx, cmd);
3918 mp_cmd_free(cmd);
3919 if (brk_cmd == 2)
3920 goto goto_enable_cache;
3923 mpctx->was_paused = 0;
3925 /* Looping. */
3926 if(mpctx->eof==1 && mpctx->loop_times>=0) {
3927 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d, eof = %d\n", mpctx->loop_times,mpctx->eof);
3929 if(mpctx->loop_times>1) mpctx->loop_times--; else
3930 if(mpctx->loop_times==1) mpctx->loop_times=-1;
3931 play_n_frames=play_n_frames_mf;
3932 mpctx->eof=0;
3933 abs_seek_pos=SEEK_ABSOLUTE; rel_seek_secs=seek_to_sec;
3934 loop_seek = 1;
3937 if(rel_seek_secs || abs_seek_pos){
3938 if (seek(mpctx, rel_seek_secs, abs_seek_pos) >= 0) {
3939 // Set OSD:
3940 if(!loop_seek){
3941 if( !edl_decision )
3942 set_osd_bar(0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3946 rel_seek_secs=0;
3947 abs_seek_pos=0;
3948 loop_seek=0;
3949 edl_decision = 0;
3952 #ifdef CONFIG_GUI
3953 if(use_gui){
3954 guiEventHandling();
3955 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3956 // get pos from frame number / total frames
3957 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3958 } else {
3959 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3961 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3962 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3963 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3964 guiGetEvent( guiReDraw,NULL );
3965 guiGetEvent( guiSetVolume,NULL );
3966 if(guiIntfStruct.Playing==0) break; // STOP
3967 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3968 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3969 #ifdef CONFIG_DVDREAD
3970 if ( mpctx->stream->type == STREAMTYPE_DVD )
3972 dvd_priv_t * dvdp = mpctx->stream->priv;
3973 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3975 #endif
3977 #endif /* CONFIG_GUI */
3979 } // while(!mpctx->eof)
3981 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->eof);
3983 #ifdef CONFIG_DVBIN
3984 if(mpctx->dvbin_reopen)
3986 mpctx->eof = 0;
3987 uninit_player(INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_INPUT|INITIALIZED_GETCH2|(fixed_vo?INITIALIZED_VO:0)));
3988 cache_uninit(mpctx->stream);
3989 mpctx->dvbin_reopen = 0;
3990 goto goto_enable_cache;
3992 #endif
3995 goto_next_file: // don't jump here after ao/vo/getch initialization!
3997 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3999 if(benchmark){
4000 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4001 double total_time_usage;
4002 total_time_usage_start=GetTimer()-total_time_usage_start;
4003 total_time_usage = (float)total_time_usage_start*0.000001;
4004 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4005 video_time_usage,vout_time_usage,audio_time_usage,
4006 total_time_usage-tot,total_time_usage);
4007 if(total_time_usage>0.0)
4008 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4009 100.0*video_time_usage/total_time_usage,
4010 100.0*vout_time_usage/total_time_usage,
4011 100.0*audio_time_usage/total_time_usage,
4012 100.0*(total_time_usage-tot)/total_time_usage,
4013 100.0);
4014 if(total_frame_cnt && frame_dropping)
4015 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4016 total_frame_cnt-drop_frame_cnt,
4017 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4018 drop_frame_cnt,
4019 100*drop_frame_cnt/total_frame_cnt,
4020 total_frame_cnt,
4021 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4024 // time to uninit all, except global stuff:
4025 uninit_player(INITIALIZED_ALL-(INITIALIZED_GUI+INITIALIZED_INPUT+(fixed_vo?INITIALIZED_VO:0)));
4027 if(mpctx->set_of_sub_size > 0) {
4028 current_module="sub_free";
4029 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4030 sub_free(mpctx->set_of_subtitles[i]);
4031 #ifdef CONFIG_ASS
4032 if(mpctx->set_of_ass_tracks[i])
4033 ass_free_track( mpctx->set_of_ass_tracks[i] );
4034 #endif
4036 mpctx->set_of_sub_size = 0;
4038 vo_sub_last = vo_sub=NULL;
4039 subdata=NULL;
4040 #ifdef CONFIG_ASS
4041 ass_track = NULL;
4042 if(ass_library)
4043 ass_clear_fonts(ass_library);
4044 #endif
4046 if(mpctx->eof == PT_NEXT_ENTRY || mpctx->eof == PT_PREV_ENTRY) {
4047 mpctx->eof = mpctx->eof == PT_NEXT_ENTRY ? 1 : -1;
4048 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) == PLAY_TREE_ITER_ENTRY) {
4049 mpctx->eof = 1;
4050 } else {
4051 play_tree_iter_free(mpctx->playtree_iter);
4052 mpctx->playtree_iter = NULL;
4054 mpctx->play_tree_step = 1;
4055 } else if(mpctx->eof == PT_UP_NEXT || mpctx->eof == PT_UP_PREV) {
4056 mpctx->eof = mpctx->eof == PT_UP_NEXT ? 1 : -1;
4057 if(mpctx->playtree_iter) {
4058 if(play_tree_iter_up_step(mpctx->playtree_iter,mpctx->eof,0) == PLAY_TREE_ITER_ENTRY) {
4059 mpctx->eof = 1;
4060 } else {
4061 play_tree_iter_free(mpctx->playtree_iter);
4062 mpctx->playtree_iter = NULL;
4065 } else if (mpctx->eof == PT_STOP) {
4066 play_tree_iter_free(mpctx->playtree_iter);
4067 mpctx->playtree_iter = NULL;
4068 } else { // NEXT PREV SRC
4069 mpctx->eof = mpctx->eof == PT_PREV_SRC ? -1 : 1;
4072 if(mpctx->eof == 0) mpctx->eof = 1;
4074 while(mpctx->playtree_iter != NULL) {
4075 filename = play_tree_iter_get_file(mpctx->playtree_iter,mpctx->eof);
4076 if(filename == NULL) {
4077 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->eof,0) != PLAY_TREE_ITER_ENTRY) {
4078 play_tree_iter_free(mpctx->playtree_iter);
4079 mpctx->playtree_iter = NULL;
4081 } else
4082 break;
4085 #ifdef CONFIG_GUI
4086 if(use_gui && !mpctx->playtree_iter) {
4087 #ifdef CONFIG_DVDREAD
4088 if(!guiIntfStruct.DiskChanged)
4089 #endif
4090 mplEnd();
4092 #endif
4094 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4095 if(!mpctx->playtree_iter) filename = NULL;
4096 mpctx->eof = 0;
4097 goto play_next_file;
4101 exit_player_with_rc(EXIT_EOF, 0);
4103 return 1;
4105 #endif /* DISABLE_MAIN */