Add missing #include, fixes the warning:
[mplayer/greg.git] / mplayer.c
blobe9acc685c4bf4792cf34a2c88d6fdf7170358904
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "config.h"
9 #ifdef WIN32
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 "version.h"
41 #include "mp_msg.h"
43 #define HELP_MP_DEFINE_STATIC
44 #include "help_mp.h"
46 #include "m_option.h"
47 #include "m_config.h"
48 #include "m_property.h"
50 #include "cfg-mplayer-def.h"
52 #include "subreader.h"
54 #include "libvo/video_out.h"
56 #include "libvo/font_load.h"
57 #include "libvo/sub.h"
59 #ifdef HAVE_X11
60 #include "libvo/x11_common.h"
61 #endif
63 #include "libao2/audio_out.h"
65 #include "codec-cfg.h"
67 #include "edl.h"
69 #include "spudec.h"
70 #include "vobsub.h"
72 #include "osdep/getch2.h"
73 #include "osdep/timer.h"
75 #include "cpudetect.h"
77 #ifdef HAVE_NEW_GUI
78 #include "gui/interface.h"
79 #endif
81 #include "input/input.h"
83 int slave_mode=0;
84 int player_idle_mode=0;
85 int quiet=0;
86 int enable_mouse_movements=0;
88 #ifdef WIN32
89 char * proc_priority=NULL;
90 #endif
92 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
94 #ifdef HAVE_RTC
95 #ifdef __linux__
96 #include <linux/rtc.h>
97 #else
98 #include <rtc.h>
99 #define RTC_IRQP_SET RTCIO_IRQP_SET
100 #define RTC_PIE_ON RTCIO_PIE_ON
101 #endif /* __linux__ */
102 #endif /* HAVE_RTC */
104 #ifdef USE_TV
105 #include "stream/tv.h"
106 #endif
107 #ifdef USE_RADIO
108 #include "stream/stream_radio.h"
109 #endif
111 #ifdef HAS_DVBIN_SUPPORT
112 #include "stream/dvbin.h"
113 #include "stream/cache2.h"
114 #endif
116 //**************************************************************************//
117 // Playtree
118 //**************************************************************************//
119 #include "playtree.h"
120 #include "playtreeparser.h"
122 extern int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
123 extern int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
125 //**************************************************************************//
126 // Config
127 //**************************************************************************//
128 #include "parser-cfg.h"
129 #include "parser-mpcmd.h"
131 m_config_t* mconfig;
133 //**************************************************************************//
134 // Config file
135 //**************************************************************************//
137 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
139 static int cfg_include(m_option_t *conf, char *filename){
140 return m_config_parse_config_file(mconfig, filename);
143 #include "get_path.h"
145 //**************************************************************************//
146 // XScreensaver
147 //**************************************************************************//
149 void xscreensaver_heartbeat(void);
151 //**************************************************************************//
152 //**************************************************************************//
153 // Input media streaming & demultiplexer:
154 //**************************************************************************//
156 static int max_framesize=0;
158 #include "stream/stream.h"
159 #include "libmpdemux/demuxer.h"
160 #include "libmpdemux/stheader.h"
161 //#include "parse_es.h"
162 #include "libmpdemux/matroska.h"
164 #ifdef USE_DVDREAD
165 #include "stream/stream_dvd.h"
166 #endif
168 #ifdef USE_DVDNAV
169 #include "stream/stream_dvdnav.h"
170 #endif
172 #include "libmpcodecs/dec_audio.h"
173 #include "libmpcodecs/dec_video.h"
174 #include "libmpcodecs/mp_image.h"
175 #include "libmpcodecs/vf.h"
176 #include "libmpcodecs/vd.h"
178 #include "mixer.h"
180 #include "mp_core.h"
182 //**************************************************************************//
183 //**************************************************************************//
185 // Common FIFO functions, and keyboard/event FIFO code
186 #include "mp_fifo.h"
187 int noconsolecontrols=0;
188 //**************************************************************************//
190 // Not all functions in mplayer.c take the context as an argument yet
191 static MPContext mpctx_s = {
192 .osd_function = OSD_PLAY,
193 .begin_skip = MP_NOPTS_VALUE,
194 .play_tree_step = 1,
195 .global_sub_pos = -1,
196 .set_of_sub_pos = -1,
197 .file_format = DEMUXER_TYPE_UNKNOWN,
198 .loop_times = -1,
199 #ifdef HAS_DVBIN_SUPPORT
200 .last_dvb_step = 1,
201 #endif
204 static MPContext *mpctx = &mpctx_s;
206 int fixed_vo=0;
208 // benchmark:
209 double video_time_usage=0;
210 double vout_time_usage=0;
211 static double audio_time_usage=0;
212 static int total_time_usage_start=0;
213 static int total_frame_cnt=0;
214 static int drop_frame_cnt=0; // total number of dropped frames
215 int benchmark=0;
217 // options:
218 int auto_quality=0;
219 static int output_quality=0;
221 float playback_speed=1.0;
223 int use_gui=0;
225 #ifdef HAVE_NEW_GUI
226 int enqueue=0;
227 #endif
229 static int list_properties = 0;
231 int osd_level=1;
232 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
233 unsigned int osd_visible;
234 int osd_duration = 1000;
236 int term_osd = 1;
237 static char* term_osd_esc = "\x1b[A\r\x1b[K";
238 static char* playing_msg = NULL;
239 // seek:
240 static double seek_to_sec;
241 static off_t seek_to_byte=0;
242 static off_t step_sec=0;
243 static int loop_seek=0;
245 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
247 // A/V sync:
248 int autosync=0; // 30 might be a good default value.
250 // may be changed by GUI: (FIXME!)
251 float rel_seek_secs=0;
252 int abs_seek_pos=0;
254 // codecs:
255 char **audio_codec_list=NULL; // override audio codec
256 char **video_codec_list=NULL; // override video codec
257 char **audio_fm_list=NULL; // override audio codec family
258 char **video_fm_list=NULL; // override video codec family
260 // demuxer:
261 extern char *demuxer_name; // override demuxer
262 extern char *audio_demuxer_name; // override audio demuxer
263 extern char *sub_demuxer_name; // override sub demuxer
265 // streaming:
266 int audio_id=-1;
267 int video_id=-1;
268 int dvdsub_id=-2;
269 int vobsub_id=-1;
270 char* audio_lang=NULL;
271 char* dvdsub_lang=NULL;
272 static char* spudec_ifo=NULL;
273 char* filename=NULL; //"MI2-Trailer.avi";
274 int forced_subs_only=0;
275 int file_filter=1;
277 // cache2:
278 int stream_cache_size=-1;
279 #ifdef USE_STREAM_CACHE
280 extern int cache_fill_status;
282 float stream_cache_min_percent=20.0;
283 float stream_cache_seek_min_percent=50.0;
284 #else
285 #define cache_fill_status 0
286 #endif
288 // dump:
289 static char *stream_dump_name="stream.dump";
290 int stream_dump_type=0;
292 // A-V sync:
293 static float default_max_pts_correction=-1;//0.01f;
294 static float max_pts_correction=0;//default_max_pts_correction;
295 static float c_total=0;
296 float audio_delay=0;
297 static int ignore_start=0;
299 static int softsleep=0;
301 float force_fps=0;
302 static int force_srate=0;
303 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
304 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
305 static int play_n_frames=-1;
306 static int play_n_frames_mf=-1;
308 // screen info:
309 char** video_driver_list=NULL;
310 char** audio_driver_list=NULL;
312 // sub:
313 char *font_name=NULL;
314 char *sub_font_name=NULL;
315 #ifdef HAVE_FONTCONFIG
316 extern int font_fontconfig;
317 #endif
318 float font_factor=0.75;
319 char **sub_name=NULL;
320 float sub_delay=0;
321 float sub_fps=0;
322 int sub_auto = 1;
323 char *vobsub_name=NULL;
324 /*DSP!!char *dsp=NULL;*/
325 int subcc_enabled=0;
326 int suboverlap_enabled = 1;
328 #ifdef USE_ASS
329 #include "libass/ass.h"
330 #include "libass/ass_mp.h"
331 #endif
333 extern int mp_msg_levels[MSGT_MAX];
334 extern int mp_msg_level_all;
336 char* current_module=NULL; // for debugging
339 // ---
341 #ifdef HAVE_MENU
342 #include "m_struct.h"
343 #include "libmenu/menu.h"
344 extern void vf_menu_pause_update(struct vf_instance_s* vf);
345 extern vf_info_t vf_info_menu;
346 static vf_info_t* libmenu_vfs[] = {
347 &vf_info_menu,
348 NULL
350 static vf_instance_t* vf_menu = NULL;
351 int use_menu = 0;
352 static char* menu_cfg = NULL;
353 static char* menu_root = "main";
354 #endif
357 #ifdef HAVE_RTC
358 static int nortc = 1;
359 static char* rtc_device;
360 #endif
362 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
363 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
364 short edl_decision = 0; ///< 1 when an EDL operation has been made.
365 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
366 int use_filedir_conf;
368 static unsigned int inited_flags=0;
369 #include "mpcommon.h"
370 #include "command.h"
372 #include "metadata.h"
374 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
376 void *mpctx_get_video_out(MPContext *mpctx)
378 return mpctx->video_out;
381 void *mpctx_get_audio_out(MPContext *mpctx)
383 return mpctx->audio_out;
386 void *mpctx_get_demuxer(MPContext *mpctx)
388 return mpctx->demuxer;
391 void *mpctx_get_playtree_iter(MPContext *mpctx)
393 return mpctx->playtree_iter;
396 void *mpctx_get_mixer(MPContext *mpctx)
398 return &mpctx->mixer;
401 int mpctx_get_global_sub_size(MPContext *mpctx)
403 return mpctx->global_sub_size;
406 int mpctx_get_osd_function(MPContext *mpctx)
408 return mpctx->osd_function;
411 static int is_valid_metadata_type (metadata_t type) {
412 switch (type)
414 /* check for valid video stream */
415 case META_VIDEO_CODEC:
416 case META_VIDEO_BITRATE:
417 case META_VIDEO_RESOLUTION:
419 if (!mpctx->sh_video)
420 return 0;
421 break;
424 /* check for valid audio stream */
425 case META_AUDIO_CODEC:
426 case META_AUDIO_BITRATE:
427 case META_AUDIO_SAMPLES:
429 if (!mpctx->sh_audio)
430 return 0;
431 break;
434 /* check for valid demuxer */
435 case META_INFO_TITLE:
436 case META_INFO_ARTIST:
437 case META_INFO_ALBUM:
438 case META_INFO_YEAR:
439 case META_INFO_COMMENT:
440 case META_INFO_TRACK:
441 case META_INFO_GENRE:
443 if (!mpctx->demuxer)
444 return 0;
445 break;
448 default:
449 break;
452 return 1;
455 static char *get_demuxer_info (char *tag) {
456 char **info = mpctx->demuxer->info;
457 int n;
459 if (!info || !tag)
460 return NULL;
462 for (n = 0; info[2*n] != NULL ; n++)
463 if (!strcmp (info[2*n], tag))
464 break;
466 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
469 char *get_metadata (metadata_t type) {
470 char *meta = NULL;
471 sh_audio_t * const sh_audio = mpctx->sh_audio;
472 sh_video_t * const sh_video = mpctx->sh_video;
474 if (!is_valid_metadata_type (type))
475 return NULL;
477 switch (type)
479 case META_NAME:
481 return strdup (mp_basename2 (filename));
484 case META_VIDEO_CODEC:
486 if (sh_video->format == 0x10000001)
487 meta = strdup ("mpeg1");
488 else if (sh_video->format == 0x10000002)
489 meta = strdup ("mpeg2");
490 else if (sh_video->format == 0x10000004)
491 meta = strdup ("mpeg4");
492 else if (sh_video->format == 0x10000005)
493 meta = strdup ("h264");
494 else if (sh_video->format >= 0x20202020)
496 meta = malloc (8);
497 sprintf (meta, "%.4s", (char *) &sh_video->format);
499 else
501 meta = malloc (8);
502 sprintf (meta, "0x%08X", sh_video->format);
504 return meta;
507 case META_VIDEO_BITRATE:
509 meta = malloc (16);
510 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
511 return meta;
514 case META_VIDEO_RESOLUTION:
516 meta = malloc (16);
517 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
518 return meta;
521 case META_AUDIO_CODEC:
523 if (sh_audio->codec && sh_audio->codec->name)
524 meta = strdup (sh_audio->codec->name);
525 return meta;
528 case META_AUDIO_BITRATE:
530 meta = malloc (16);
531 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
532 return meta;
535 case META_AUDIO_SAMPLES:
537 meta = malloc (16);
538 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
539 return meta;
542 /* check for valid demuxer */
543 case META_INFO_TITLE:
544 return get_demuxer_info ("Title");
546 case META_INFO_ARTIST:
547 return get_demuxer_info ("Artist");
549 case META_INFO_ALBUM:
550 return get_demuxer_info ("Album");
552 case META_INFO_YEAR:
553 return get_demuxer_info ("Year");
555 case META_INFO_COMMENT:
556 return get_demuxer_info ("Comment");
558 case META_INFO_TRACK:
559 return get_demuxer_info ("Track");
561 case META_INFO_GENRE:
562 return get_demuxer_info ("Genre");
564 default:
565 break;
568 return meta;
571 /// step size of mixer changes
572 int volstep = 3;
574 void uninit_player(unsigned int mask){
575 mask=inited_flags&mask;
577 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
579 if(mask&INITED_ACODEC){
580 inited_flags&=~INITED_ACODEC;
581 current_module="uninit_acodec";
582 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
583 #ifdef HAVE_NEW_GUI
584 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
585 #endif
586 mpctx->sh_audio=NULL;
587 mpctx->mixer.afilter = NULL;
590 if(mask&INITED_VCODEC){
591 inited_flags&=~INITED_VCODEC;
592 current_module="uninit_vcodec";
593 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
594 mpctx->sh_video=NULL;
595 #ifdef HAVE_MENU
596 vf_menu=NULL;
597 #endif
600 if(mask&INITED_DEMUXER){
601 inited_flags&=~INITED_DEMUXER;
602 current_module="free_demuxer";
603 if(mpctx->demuxer){
604 mpctx->stream=mpctx->demuxer->stream;
605 free_demuxer(mpctx->demuxer);
607 mpctx->demuxer=NULL;
610 // kill the cache process:
611 if(mask&INITED_STREAM){
612 inited_flags&=~INITED_STREAM;
613 current_module="uninit_stream";
614 if(mpctx->stream) free_stream(mpctx->stream);
615 mpctx->stream=NULL;
618 if(mask&INITED_VO){
619 inited_flags&=~INITED_VO;
620 current_module="uninit_vo";
621 mpctx->video_out->uninit();
622 mpctx->video_out=NULL;
625 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
626 if(mask&INITED_GETCH2){
627 inited_flags&=~INITED_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&INITED_VOBSUB){
635 inited_flags&=~INITED_VOBSUB;
636 current_module="uninit_vobsub";
637 if(vo_vobsub) vobsub_close(vo_vobsub);
638 vo_vobsub=NULL;
641 if (mask&INITED_SPUDEC){
642 inited_flags&=~INITED_SPUDEC;
643 current_module="uninit_spudec";
644 spudec_free(vo_spudec);
645 vo_spudec=NULL;
648 if(mask&INITED_AO){
649 inited_flags&=~INITED_AO;
650 current_module="uninit_ao";
651 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
652 mpctx->audio_out->uninit(mpctx->eof?0:1); mpctx->audio_out=NULL;
655 #ifdef HAVE_NEW_GUI
656 if(mask&INITED_GUI){
657 inited_flags&=~INITED_GUI;
658 current_module="uninit_gui";
659 guiDone();
661 #endif
663 if(mask&INITED_INPUT){
664 inited_flags&=~INITED_INPUT;
665 current_module="uninit_input";
666 mp_input_uninit();
667 #ifdef HAVE_MENU
668 if (use_menu)
669 menu_uninit();
670 #endif
673 current_module=NULL;
676 void exit_player_with_rc(const char* how, int rc){
678 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
679 uninit_player(INITED_ALL);
680 #ifdef HAVE_X11
681 #ifdef HAVE_NEW_GUI
682 if ( !use_gui )
683 #endif
684 vo_uninit(); // Close the X11 connection (if any is open).
685 #endif
687 #ifdef HAVE_FREETYPE
688 current_module="uninit_font";
689 if (vo_font) free_font_desc(vo_font);
690 vo_font = NULL;
691 done_freetype();
692 #endif
693 free_osd_list();
695 #ifdef USE_ASS
696 ass_library_done(ass_library);
697 #endif
699 current_module="exit_player";
701 // free mplayer config
702 if(mconfig)
703 m_config_free(mconfig);
705 if(mpctx->playtree)
706 play_tree_free(mpctx->playtree, 1);
709 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
710 if(how) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,how);
711 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
713 exit(rc);
716 void exit_player(const char* how){
717 exit_player_with_rc(how, 1);
720 #ifndef __MINGW32__
721 static void child_sighandler(int x){
722 pid_t pid;
723 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
725 #endif
727 #ifdef CRASH_DEBUG
728 static char *prog_path;
729 static int crash_debug = 0;
730 #endif
732 static void exit_sighandler(int x){
733 static int sig_count=0;
734 #ifdef CRASH_DEBUG
735 if (!crash_debug || x != SIGTRAP)
736 #endif
737 ++sig_count;
738 if(inited_flags==0 && sig_count>1) exit(1);
739 if(sig_count==5)
741 /* We're crashing bad and can't uninit cleanly :(
742 * by popular request, we make one last (dirty)
743 * effort to restore the user's
744 * terminal. */
745 getch2_disable();
746 exit(1);
748 if(sig_count==6) exit(1);
749 if(sig_count>6){
750 // can't stop :(
751 #ifndef __MINGW32__
752 kill(getpid(),SIGKILL);
753 #endif
755 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
756 current_module?current_module:"unknown"
758 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
759 if(sig_count<=1)
760 switch(x){
761 case SIGINT:
762 case SIGQUIT:
763 case SIGTERM:
764 case SIGKILL:
765 break; // killed from keyboard (^C) or killed [-9]
766 case SIGILL:
767 #ifdef RUNTIME_CPUDETECT
768 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
769 #else
770 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
771 #endif
772 case SIGFPE:
773 case SIGSEGV:
774 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
775 default:
776 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
777 #ifdef CRASH_DEBUG
778 if (crash_debug) {
779 int gdb_pid;
780 char spid[20];
781 snprintf(spid, 19, "%i", getpid());
782 spid[19] = 0;
783 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
784 gdb_pid = fork();
785 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
786 if (gdb_pid == 0) { // We are the child
787 getch2_disable(); // allow terminal to work properly with gdb
788 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
789 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
790 } else if (gdb_pid < 0)
791 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
792 else {
793 waitpid(gdb_pid, NULL, 0);
795 if (x == SIGTRAP) return;
797 #endif
799 exit_player(NULL);
802 extern void mp_input_register_options(m_config_t* cfg);
804 #include "cfg-mplayer.h"
806 static void parse_cfgfiles( m_config_t* conf )
808 char *conffile;
809 int conffile_fd;
810 if (m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
811 exit_player(NULL);
812 if ((conffile = get_path("")) == NULL) {
813 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
814 } else {
815 #ifdef __MINGW32__
816 mkdir(conffile);
817 #else
818 mkdir(conffile, 0777);
819 #endif
820 free(conffile);
821 if ((conffile = get_path("config")) == NULL) {
822 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
823 } else {
824 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
825 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
826 write(conffile_fd, default_config, strlen(default_config));
827 close(conffile_fd);
829 if (m_config_parse_config_file(conf, conffile) < 0)
830 exit_player(NULL);
831 free(conffile);
836 void load_per_file_config (m_config_t* conf, const char *const file)
838 char *confpath;
839 char cfg[strlen(file)+10];
840 struct stat st;
841 char *name;
843 sprintf (cfg, "%s.conf", file);
845 if (use_filedir_conf && !stat (cfg, &st))
847 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
848 m_config_parse_config_file (conf, cfg);
849 return;
852 if ((name = strrchr (cfg, '/')) == NULL)
853 name = cfg;
854 else
855 name++;
857 if ((confpath = get_path (name)) != NULL)
859 if (!stat (confpath, &st))
861 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
862 m_config_parse_config_file (conf, confpath);
865 free (confpath);
869 /* When libmpdemux performs a blocking operation (network connection or
870 * cache filling) if the operation fails we use this function to check
871 * if it was interrupted by the user.
872 * The function returns a new value for eof. */
873 static int libmpdemux_was_interrupted(int eof) {
874 mp_cmd_t* cmd;
875 if((cmd = mp_input_get_cmd(0,0,0)) != NULL) {
876 switch(cmd->id) {
877 case MP_CMD_QUIT:
878 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
879 case MP_CMD_PLAY_TREE_STEP: {
880 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
881 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
882 } break;
883 case MP_CMD_PLAY_TREE_UP_STEP: {
884 eof = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
885 } break;
886 case MP_CMD_PLAY_ALT_SRC_STEP: {
887 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
888 } break;
890 mp_cmd_free(cmd);
892 return eof;
895 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
897 int playtree_add_playlist(play_tree_t* entry)
899 play_tree_add_bpf(entry,filename);
901 #ifdef HAVE_NEW_GUI
902 if (use_gui) {
903 if (entry) {
904 import_playtree_playlist_into_gui(entry, mconfig);
905 play_tree_free_list(entry,1);
907 } else
908 #endif
910 if(!entry) {
911 entry = mpctx->playtree_iter->tree;
912 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
913 return PT_NEXT_ENTRY;
915 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
916 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
917 return PT_NEXT_ENTRY;
920 play_tree_remove(entry,1,1);
921 return PT_NEXT_SRC;
923 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
924 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
925 entry = mpctx->playtree_iter->tree;
926 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
927 return PT_NEXT_ENTRY;
929 play_tree_remove(entry,1,1);
931 return PT_NEXT_SRC;
934 void add_subtitles(char *filename, float fps, int noerr)
936 sub_data *subd;
937 #ifdef USE_ASS
938 ass_track_t *asst = 0;
939 #endif
941 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
942 return;
945 subd = sub_read_file(filename, fps);
946 #ifdef USE_ASS
947 if (ass_enabled)
948 #ifdef USE_ICONV
949 asst = ass_read_file(ass_library, filename, sub_cp);
950 #else
951 asst = ass_read_file(ass_library, filename, 0);
952 #endif
953 if (ass_enabled && subd && !asst)
954 asst = ass_read_subdata(ass_library, subd, fps);
956 if (!asst && !subd)
957 #else
958 if(!subd)
959 #endif
960 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
961 filename_recode(filename));
963 #ifdef USE_ASS
964 if (!asst && !subd) return;
965 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
966 #else
967 if (!subd) return;
968 #endif
969 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
970 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
971 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
972 filename_recode(filename));
973 ++mpctx->set_of_sub_size;
974 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
975 filename_recode(filename));
978 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
979 void update_set_of_subtitles(void)
980 // subdata was changed, set_of_sub... have to be updated.
982 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
983 int i;
984 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
985 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
986 set_of_subtitles[i-1] = set_of_subtitles[i];
987 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
988 --mpctx->set_of_sub_size;
989 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
991 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
992 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
994 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
995 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
996 ++mpctx->set_of_sub_size;
1000 void init_vo_spudec(void) {
1001 if (vo_spudec)
1002 spudec_free(vo_spudec);
1003 inited_flags &= ~INITED_SPUDEC;
1004 vo_spudec = NULL;
1005 if (spudec_ifo) {
1006 unsigned int palette[16], width, height;
1007 current_module="spudec_init_vobsub";
1008 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1009 vo_spudec=spudec_new_scaled(palette, width, height);
1012 #ifdef USE_DVDREAD
1013 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1014 current_module="spudec_init_dvdread";
1015 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1016 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1018 #endif
1020 #ifdef USE_DVDNAV
1021 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1022 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1023 current_module="spudec_init_dvdnav";
1024 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1026 #endif
1028 if ((vo_spudec == NULL) && (mpctx->demuxer->type == DEMUXER_TYPE_MATROSKA) &&
1029 (mpctx->d_sub->sh != NULL) && (((sh_sub_t *)mpctx->d_sub->sh)->type == 'v')) {
1030 sh_sub_t *mkv_sh_sub = (sh_sub_t *)mpctx->d_sub->sh;
1031 current_module = "spudec_init_matroska";
1032 vo_spudec =
1033 spudec_new_scaled_vobsub(mkv_sh_sub->palette, mkv_sh_sub->colors,
1034 mkv_sh_sub->custom_colors, mkv_sh_sub->width,
1035 mkv_sh_sub->height);
1036 forced_subs_only = mkv_sh_sub->forced_subs_only;
1039 if (vo_spudec==NULL) {
1040 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1041 unsigned int *palette = NULL;
1042 if (sh && sh->has_palette)
1043 palette = sh->palette;
1044 current_module="spudec_init_normal";
1045 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1046 spudec_set_font_factor(vo_spudec,font_factor);
1049 if (vo_spudec!=NULL)
1050 inited_flags|=INITED_SPUDEC;
1054 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1055 * make it all work is to use the builtin SDL-bootstrap code, which
1056 * will be done automatically by replacing our main() if we include SDL.h.
1058 #if defined(__APPLE__) && defined(HAVE_SDL)
1059 #include <SDL.h>
1060 #endif
1063 * \brief append a formatted string
1064 * \param buf buffer to print into
1065 * \param pos position of terminating 0 in buf
1066 * \param len maximum number of characters in buf, not including terminating 0
1067 * \param format printf format string
1069 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1071 va_list va;
1072 va_start(va, format);
1073 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1074 va_end(va);
1075 if (*pos >= len ) {
1076 buf[len] = 0;
1077 *pos = len;
1082 * \brief append time in the hh:mm:ss.f format
1083 * \param buf buffer to print into
1084 * \param pos position of terminating 0 in buf
1085 * \param len maximum number of characters in buf, not including terminating 0
1086 * \param time time value to convert/append
1088 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1089 long tenths = 10 * time;
1090 int f1 = tenths % 10;
1091 int ss = (tenths / 10) % 60;
1092 int mm = (tenths / 600) % 60;
1093 int hh = tenths / 36000;
1094 if (time <= 0) {
1095 saddf(buf, pos, len, "unknown");
1096 return;
1098 if (hh > 0)
1099 saddf(buf, pos, len, "%2d:", hh);
1100 if (hh > 0 || mm > 0)
1101 saddf(buf, pos, len, "%02d:", mm);
1102 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1106 * \brief print the status line
1107 * \param a_pos audio position
1108 * \param a_v A-V desynchronization
1109 * \param corr amount out A-V synchronization
1111 static void print_status(float a_pos, float a_v, float corr)
1113 sh_video_t * const sh_video = mpctx->sh_video;
1114 int width;
1115 char *line;
1116 unsigned pos = 0;
1117 get_screen_size();
1118 if (screen_width > 0)
1119 width = screen_width;
1120 else
1121 width = 80;
1122 #ifdef WIN32
1123 /* Windows command line is broken (MinGW's rxvt works, but we
1124 * should not depend on that). */
1125 width--;
1126 #endif
1127 line = malloc(width + 1); // one additional char for the terminating null
1129 // Audio time
1130 if (mpctx->sh_audio) {
1131 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1132 if (!sh_video) {
1133 float len = demuxer_get_time_length(mpctx->demuxer);
1134 saddf(line, &pos, width, "(");
1135 sadd_hhmmssf(line, &pos, width, a_pos);
1136 saddf(line, &pos, width, ") of %.1f (", len);
1137 sadd_hhmmssf(line, &pos, width, len);
1138 saddf(line, &pos, width, ") ");
1142 // Video time
1143 if (sh_video)
1144 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1146 // A-V sync
1147 if (mpctx->sh_audio && sh_video)
1148 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
1150 // Video stats
1151 if (sh_video)
1152 saddf(line, &pos, width, "%3d/%3d ",
1153 (int)sh_video->num_frames,
1154 (int)sh_video->num_frames_decoded);
1156 // CPU usage
1157 if (sh_video) {
1158 if (sh_video->timer > 0.5)
1159 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1160 (int)(100.0*video_time_usage*playback_speed/(double)sh_video->timer),
1161 (int)(100.0*vout_time_usage*playback_speed/(double)sh_video->timer),
1162 (100.0*audio_time_usage*playback_speed/(double)sh_video->timer));
1163 else
1164 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1165 } else if (mpctx->sh_audio) {
1166 if (mpctx->delay > 0.5)
1167 saddf(line, &pos, width, "%4.1f%% ",
1168 100.0*audio_time_usage/(double)mpctx->delay);
1169 else
1170 saddf(line, &pos, width, "??,?%% ");
1173 // VO stats
1174 if (sh_video)
1175 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1177 #ifdef USE_STREAM_CACHE
1178 // cache stats
1179 if (stream_cache_size > 0)
1180 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1181 #endif
1183 // other
1184 if (playback_speed != 1)
1185 saddf(line, &pos, width, "%4.2fx ", playback_speed);
1187 // end
1188 if (erase_to_end_of_line) {
1189 line[pos] = 0;
1190 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1191 } else {
1192 memset(&line[pos], ' ', width - pos);
1193 line[width] = 0;
1194 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1196 free(line);
1200 * \brief build a chain of audio filters that converts the input format
1201 * to the ao's format, taking into account the current playback_speed.
1202 * \param sh_audio describes the requested input format of the chain.
1203 * \param ao_data describes the requested output format of the chain.
1205 int build_afilter_chain(sh_audio_t *sh_audio, ao_data_t *ao_data)
1207 int new_srate;
1208 int result;
1209 if (!sh_audio)
1211 #ifdef HAVE_NEW_GUI
1212 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1213 #endif
1214 mpctx->mixer.afilter = NULL;
1215 return 0;
1217 if(af_control_any_rev(sh_audio->afilter,
1218 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1219 &playback_speed)) {
1220 new_srate = sh_audio->samplerate;
1221 } else {
1222 new_srate = sh_audio->samplerate * playback_speed;
1223 if (new_srate != ao_data->samplerate) {
1224 // limits are taken from libaf/af_resample.c
1225 if (new_srate < 8000)
1226 new_srate = 8000;
1227 if (new_srate > 192000)
1228 new_srate = 192000;
1229 playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1232 result = init_audio_filters(sh_audio, new_srate,
1233 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1234 mpctx->mixer.afilter = sh_audio->afilter;
1235 #ifdef HAVE_NEW_GUI
1236 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1237 #endif
1238 return result;
1242 typedef struct mp_osd_msg mp_osd_msg_t;
1243 struct mp_osd_msg {
1244 /// Previous message on the stack.
1245 mp_osd_msg_t* prev;
1246 /// Message text.
1247 char msg[64];
1248 int id,level,started;
1249 /// Display duration in ms.
1250 unsigned time;
1253 /// OSD message stack.
1254 static mp_osd_msg_t* osd_msg_stack = NULL;
1257 * \brief Add a message on the OSD message stack
1259 * If a message with the same id is already present in the stack
1260 * it is pulled on top of the stack, otherwise a new message is created.
1264 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1265 mp_osd_msg_t *msg,*last=NULL;
1266 va_list va;
1267 int r;
1269 // look if the id is already in the stack
1270 for(msg = osd_msg_stack ; msg && msg->id != id ;
1271 last = msg, msg = msg->prev);
1272 // not found: alloc it
1273 if(!msg) {
1274 msg = calloc(1,sizeof(mp_osd_msg_t));
1275 msg->prev = osd_msg_stack;
1276 osd_msg_stack = msg;
1277 } else if(last) { // found, but it's not on top of the stack
1278 last->prev = msg->prev;
1279 msg->prev = osd_msg_stack;
1280 osd_msg_stack = msg;
1282 // write the msg
1283 va_start(va,fmt);
1284 r = vsnprintf(msg->msg, 64, fmt, va);
1285 va_end(va);
1286 if(r >= 64) msg->msg[63] = 0;
1287 // set id and time
1288 msg->id = id;
1289 msg->level = level;
1290 msg->time = time;
1295 * \brief Remove a message from the OSD stack
1297 * This function can be used to get rid of a message right away.
1301 void rm_osd_msg(int id) {
1302 mp_osd_msg_t *msg,*last=NULL;
1304 // Search for the msg
1305 for(msg = osd_msg_stack ; msg && msg->id != id ;
1306 last = msg, msg = msg->prev);
1307 if(!msg) return;
1309 // Detach it from the stack and free it
1310 if(last)
1311 last->prev = msg->prev;
1312 else
1313 osd_msg_stack = msg->prev;
1314 free(msg);
1318 * \brief Remove all messages from the OSD stack
1322 static void clear_osd_msgs(void) {
1323 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1324 while(msg) {
1325 prev = msg->prev;
1326 free(msg);
1327 msg = prev;
1329 osd_msg_stack = NULL;
1333 * \brief Get the current message from the OSD stack.
1335 * This function decrements the message timer and destroys the old ones.
1336 * The message that should be displayed is returned (if any).
1340 static mp_osd_msg_t* get_osd_msg(void) {
1341 mp_osd_msg_t *msg,*prev,*last = NULL;
1342 static unsigned last_update = 0;
1343 unsigned now = GetTimerMS();
1344 unsigned diff;
1345 char hidden_dec_done = 0;
1347 if (osd_visible) {
1348 // 36000000 means max timed visibility is 1 hour into the future, if
1349 // the difference is greater assume it's wrapped around from below 0
1350 if (osd_visible - now > 36000000) {
1351 osd_visible = 0;
1352 vo_osd_progbar_type = -1; // disable
1353 vo_osd_changed(OSDTYPE_PROGBAR);
1354 if (mpctx->osd_function != OSD_PAUSE)
1355 mpctx->osd_function = OSD_PLAY;
1359 if(!last_update) last_update = now;
1360 diff = now >= last_update ? now - last_update : 0;
1362 last_update = now;
1364 // Look for the first message in the stack with high enough level.
1365 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1366 prev = msg->prev;
1367 if(msg->level > osd_level && hidden_dec_done) continue;
1368 // The message has a high enough level or it is the first hidden one
1369 // in both cases we decrement the timer or kill it.
1370 if(!msg->started || msg->time > diff) {
1371 if(msg->started) msg->time -= diff;
1372 else msg->started = 1;
1373 // display it
1374 if(msg->level <= osd_level) return msg;
1375 hidden_dec_done = 1;
1376 continue;
1378 // kill the message
1379 free(msg);
1380 if(last) {
1381 last->prev = prev;
1382 msg = last;
1383 } else {
1384 osd_msg_stack = prev;
1385 msg = NULL;
1388 // Nothing found
1389 return NULL;
1393 * \brief Display the OSD bar.
1395 * Display the OSD bar or fall back on a simple message.
1399 void set_osd_bar(int type,const char* name,double min,double max,double val) {
1401 if(osd_level < 1) return;
1403 if(mpctx->sh_video) {
1404 osd_visible = (GetTimerMS() + 1000) | 1;
1405 vo_osd_progbar_type = type;
1406 vo_osd_progbar_value = 256*(val-min)/(max-min);
1407 vo_osd_changed(OSDTYPE_PROGBAR);
1408 return;
1411 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1412 name,ROUND(100*(val-min)/(max-min)));
1417 * \brief Update the OSD message line.
1419 * This function displays the current message on the vo OSD or on the term.
1420 * If the stack is empty and the OSD level is high enough the timer
1421 * is displayed (only on the vo OSD).
1425 static void update_osd_msg(void) {
1426 mp_osd_msg_t *msg;
1427 static char osd_text[64] = "";
1428 static char osd_text_timer[64];
1430 // we need some mem for vo_osd_text
1431 vo_osd_text = (unsigned char*)osd_text;
1433 // Look if we have a msg
1434 if((msg = get_osd_msg())) {
1435 if(strcmp(osd_text,msg->msg)) {
1436 strncpy((char*)osd_text, msg->msg, 63);
1437 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1438 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1440 return;
1443 if(mpctx->sh_video) {
1444 // fallback on the timer
1445 if(osd_level>=2) {
1446 int len = demuxer_get_time_length(mpctx->demuxer);
1447 int percentage = -1;
1448 char percentage_text[10];
1449 int pts = demuxer_get_current_time(mpctx->demuxer);
1451 if (mpctx->osd_show_percentage)
1452 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1454 if (percentage >= 0)
1455 snprintf(percentage_text, 9, " (%d%%)", percentage);
1456 else
1457 percentage_text[0] = 0;
1459 if (osd_level == 3)
1460 snprintf(osd_text_timer, 63,
1461 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1462 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1463 len/3600,(len/60)%60,len%60,percentage_text);
1464 else
1465 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1466 mpctx->osd_function,pts/3600,(pts/60)%60,
1467 pts%60,percentage_text);
1468 } else
1469 osd_text_timer[0]=0;
1471 // always decrement the percentage timer
1472 if(mpctx->osd_show_percentage)
1473 mpctx->osd_show_percentage--;
1475 if(strcmp(osd_text,osd_text_timer)) {
1476 strncpy(osd_text, osd_text_timer, 63);
1477 vo_osd_changed(OSDTYPE_OSD);
1479 return;
1482 // Clear the term osd line
1483 if(term_osd && osd_text[0]) {
1484 osd_text[0] = 0;
1485 printf("%s\n",term_osd_esc);
1489 ///@}
1490 // OSDMsgStack
1493 void reinit_audio_chain(void) {
1494 if(mpctx->sh_audio){
1495 current_module="init_audio_codec";
1496 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1497 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1498 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1499 mpctx->d_audio->id = -2;
1500 return;
1501 } else
1502 inited_flags|=INITED_ACODEC;
1503 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1506 //const ao_info_t *info=audio_out->info;
1507 current_module="af_preinit";
1508 ao_data.samplerate=force_srate;
1509 ao_data.channels=0;
1510 ao_data.format=audio_output_format;
1511 #if 1
1512 // first init to detect best values
1513 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1514 // input:
1515 mpctx->sh_audio->samplerate,
1516 // output:
1517 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1518 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1519 exit_player(MSGTR_Exit_error);
1521 #endif
1522 current_module="ao2_init";
1523 if(!(mpctx->audio_out=init_best_audio_out(audio_driver_list,
1524 0, // plugin flag
1525 ao_data.samplerate,
1526 ao_data.channels,
1527 ao_data.format,0))){
1528 // FAILED:
1529 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1530 uninit_player(INITED_ACODEC); // close codec
1531 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1532 mpctx->d_audio->id = -2;
1533 return;
1534 } else {
1535 // SUCCESS:
1536 inited_flags|=INITED_AO;
1537 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1538 mpctx->audio_out->info->short_name,
1539 ao_data.samplerate, ao_data.channels,
1540 af_fmt2str_short(ao_data.format),
1541 af_fmt2bits(ao_data.format)/8 );
1542 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1543 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1544 if(strlen(mpctx->audio_out->info->comment) > 0)
1545 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1546 // init audio filters:
1547 #if 1
1548 current_module="af_init";
1549 if(!build_afilter_chain(mpctx->sh_audio, &ao_data)) {
1550 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1551 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1552 // uninit_player(INITED_ACODEC|INITED_AO); // close codec & ao
1553 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1555 #endif
1557 mpctx->mixer.audio_out = mpctx->audio_out;
1558 mpctx->mixer.volstep = volstep;
1563 ///@}
1564 // Command2Property
1567 // Return pts value corresponding to the end point of audio written to the
1568 // ao so far.
1569 static double written_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio)
1571 double buffered_output;
1572 // first calculate the end pts of audio that has been output by decoder
1573 double a_pts = sh_audio->pts;
1574 if (a_pts != MP_NOPTS_VALUE)
1575 // Good, decoder supports new way of calculating audio pts.
1576 // sh_audio->pts is the timestamp of the latest input packet with
1577 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1578 // the amount of bytes the decoder has written after that timestamp.
1579 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1580 else {
1581 // Decoder doesn't support new way of calculating pts (or we're
1582 // being called before it has decoded anything with known timestamp).
1583 // Use the old method of audio pts calculation: take the timestamp
1584 // of last packet with known pts the decoder has read data from,
1585 // and add amount of bytes read after the beginning of that packet
1586 // divided by input bps. This will be inaccurate if the input/output
1587 // ratio is not constant for every audio packet or if it is constant
1588 // but not accurately known in sh_audio->i_bps.
1590 a_pts = d_audio->pts;
1591 // ds_tell_pts returns bytes read after last timestamp from
1592 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1593 // it has read but not decoded
1594 if (sh_audio->i_bps)
1595 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1596 (double)sh_audio->i_bps;
1598 // Now a_pts hopefully holds the pts for end of audio from decoder.
1599 // Substract data in buffers between decoder and audio out.
1601 // Decoded but not filtered
1602 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1604 // Data buffered in audio filters, measured in bytes of "missing" output
1605 buffered_output = af_calc_delay(sh_audio->afilter);
1607 // Data that was ready for ao but was buffered because ao didn't fully
1608 // accept everything to internal buffers yet
1609 buffered_output += sh_audio->a_out_buffer_len;
1611 // Filters divide audio length by playback_speed, so multiply by it
1612 // to get the length in original units without speedup or slowdown
1613 a_pts -= buffered_output * playback_speed / ao_data.bps;
1615 return a_pts;
1618 // Return pts value corresponding to currently playing audio.
1619 double playing_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio,
1620 ao_functions_t *audio_out)
1622 return written_audio_pts(sh_audio, d_audio) - playback_speed *
1623 audio_out->get_delay();
1626 static int generate_video_frame(sh_video_t *sh_video, demux_stream_t *d_video)
1628 unsigned char *start;
1629 int in_size;
1630 int hit_eof=0;
1631 double pts;
1633 while (1) {
1634 void *decoded_frame;
1635 current_module = "decode video";
1636 // XXX Time used in this call is not counted in any performance
1637 // timer now, OSD is not updated correctly for filter-added frames
1638 if (vf_output_queued_frame(sh_video->vfilter))
1639 break;
1640 current_module = "video_read_frame";
1641 in_size = ds_get_packet_pts(d_video, &start, &pts);
1642 if (in_size < 0) {
1643 // try to extract last frames in case of decoder lag
1644 in_size = 0;
1645 pts = 1e300;
1646 hit_eof = 1;
1648 if (in_size > max_framesize)
1649 max_framesize = in_size;
1650 current_module = "decode video";
1651 decoded_frame = decode_video(sh_video, start, in_size, 0, pts);
1652 if (decoded_frame) {
1653 update_subtitles(sh_video, mpctx->d_sub, 0);
1654 update_teletext(sh_video, mpctx->demuxer, 0);
1655 update_osd_msg();
1656 current_module = "filter video";
1657 if (filter_video(sh_video, decoded_frame, sh_video->pts))
1658 break;
1660 if (hit_eof)
1661 return 0;
1663 return 1;
1666 #ifdef HAVE_RTC
1667 int rtc_fd = -1;
1668 #endif
1670 static float timing_sleep(float time_frame)
1672 #ifdef HAVE_RTC
1673 if (rtc_fd >= 0){
1674 // -------- RTC -----------
1675 current_module="sleep_rtc";
1676 while (time_frame > 0.000) {
1677 unsigned long rtc_ts;
1678 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1679 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1680 time_frame -= GetRelativeTime();
1682 } else
1683 #endif
1685 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1686 // unnecessarily high CPU usage
1687 float margin = softsleep ? 0.011 : 0;
1688 current_module = "sleep_timer";
1689 while (time_frame > margin) {
1690 usec_sleep(1000000 * (time_frame - margin));
1691 time_frame -= GetRelativeTime();
1693 if (softsleep){
1694 current_module = "sleep_soft";
1695 if (time_frame < 0)
1696 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1697 while (time_frame > 0)
1698 time_frame-=GetRelativeTime(); // burn the CPU
1701 return time_frame;
1704 static void adjust_sync_and_print_status(int between_frames, float timing_error)
1706 current_module="av_sync";
1708 if(mpctx->sh_audio){
1709 double a_pts, v_pts;
1711 if (autosync)
1713 * If autosync is enabled, the value for delay must be calculated
1714 * a bit differently. It is set only to the difference between
1715 * the audio and video timers. Any attempt to include the real
1716 * or corrected delay causes the pts_correction code below to
1717 * try to correct for the changes in delay which autosync is
1718 * trying to measure. This keeps the two from competing, but still
1719 * allows the code to correct for PTS drift *only*. (Using a delay
1720 * value here, even a "corrected" one, would be incompatible with
1721 * autosync mode.)
1723 a_pts = written_audio_pts(mpctx->sh_audio, mpctx->d_audio) - mpctx->delay;
1724 else
1725 a_pts = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
1727 v_pts = mpctx->sh_video->pts;
1730 static int drop_message=0;
1731 double AV_delay = a_pts - audio_delay - v_pts;
1732 double x;
1733 if (AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
1734 ++drop_message;
1735 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1737 if (autosync)
1738 x = AV_delay*0.1f;
1739 else
1740 /* Do not correct target time for the next frame if this frame
1741 * was late not because of wrong target time but because the
1742 * target time could not be met */
1743 x = (AV_delay + timing_error * playback_speed) * 0.1f;
1744 if (x < -max_pts_correction)
1745 x = -max_pts_correction;
1746 else if (x> max_pts_correction)
1747 x = max_pts_correction;
1748 if (default_max_pts_correction >= 0)
1749 max_pts_correction = default_max_pts_correction;
1750 else
1751 max_pts_correction = mpctx->sh_video->frametime*0.10; // +-10% of time
1752 if (!between_frames) {
1753 mpctx->delay+=x;
1754 c_total+=x;
1756 if(!quiet)
1757 print_status(a_pts - audio_delay, AV_delay, c_total);
1760 } else {
1761 // No audio:
1763 if (!quiet)
1764 print_status(0, 0, 0);
1768 static int fill_audio_out_buffers(void)
1770 unsigned int t;
1771 double tt;
1772 int playsize;
1773 int playflags=0;
1774 int audio_eof=0;
1775 int bytes_to_write;
1776 sh_audio_t * const sh_audio = mpctx->sh_audio;
1778 current_module="play_audio";
1780 while (1) {
1781 // all the current uses of ao_data.pts seem to be in aos that handle
1782 // sync completely wrong; there should be no need to use ao_data.pts
1783 // in get_space()
1784 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1785 bytes_to_write = mpctx->audio_out->get_space();
1786 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
1787 break;
1789 // handle audio-only case:
1790 // this is where mplayer sleeps during audio-only playback
1791 // to avoid 100% CPU use
1792 usec_sleep(10000); // Wait a tick before retry
1795 while (bytes_to_write) {
1796 playsize = bytes_to_write;
1797 if (playsize > MAX_OUTBURST)
1798 playsize = MAX_OUTBURST;
1799 bytes_to_write -= playsize;
1801 // Fill buffer if needed:
1802 current_module="decode_audio";
1803 t = GetTimer();
1804 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
1805 if (mpctx->d_audio->eof) {
1806 audio_eof = 1;
1807 if (sh_audio->a_out_buffer_len == 0)
1808 return 0;
1810 t = GetTimer() - t;
1811 tt = t*0.000001f; audio_time_usage+=tt;
1812 if (playsize > sh_audio->a_out_buffer_len) {
1813 playsize = sh_audio->a_out_buffer_len;
1814 if (audio_eof)
1815 playflags |= AOPLAY_FINAL_CHUNK;
1817 if (!playsize)
1818 break;
1820 // play audio:
1821 current_module="play_audio";
1823 // Is this pts value actually useful for the aos that access it?
1824 // They're obviously badly broken in the way they handle av sync;
1825 // would not having access to this make them more broken?
1826 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1827 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
1829 if (playsize > 0) {
1830 sh_audio->a_out_buffer_len -= playsize;
1831 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
1832 sh_audio->a_out_buffer_len);
1833 mpctx->delay += playback_speed*playsize/(double)ao_data.bps;
1835 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
1836 // Sanity check to avoid hanging in case current ao doesn't output
1837 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
1838 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
1839 sh_audio->a_out_buffer_len = 0;
1842 return 1;
1845 static int sleep_until_update(float *time_frame, float *aq_sleep_time)
1847 int frame_time_remaining = 0;
1848 current_module="calc_sleep_time";
1850 *time_frame -= GetRelativeTime(); // reset timer
1852 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1853 float delay = mpctx->audio_out->get_delay();
1854 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
1856 if (autosync) {
1858 * Adjust this raw delay value by calculating the expected
1859 * delay for this frame and generating a new value which is
1860 * weighted between the two. The higher autosync is, the
1861 * closer to the delay value gets to that which "-nosound"
1862 * would have used, and the longer it will take for A/V
1863 * sync to settle at the right value (but it eventually will.)
1864 * This settling time is very short for values below 100.
1866 float predicted = mpctx->delay / playback_speed + *time_frame;
1867 float difference = delay - predicted;
1868 delay = predicted + difference / (float)autosync;
1871 *time_frame = delay - mpctx->delay / playback_speed;
1873 // delay = amount of audio buffered in soundcard/driver
1874 if (delay > 0.25) delay=0.25; else
1875 if (delay < 0.10) delay=0.10;
1876 if (*time_frame > delay*0.6) {
1877 // sleep time too big - may cause audio drops (buffer underrun)
1878 frame_time_remaining = 1;
1879 *time_frame = delay*0.5;
1881 } else {
1882 // If we're lagging more than 200 ms behind the right playback rate,
1883 // don't try to "catch up".
1884 // If benchmark is set always output frames as fast as possible
1885 // without sleeping.
1886 if (*time_frame < -0.2 || benchmark)
1887 *time_frame = 0;
1890 *aq_sleep_time += *time_frame;
1893 //============================== SLEEP: ===================================
1895 // flag 256 means: libvo driver does its timing (dvb card)
1896 if (*time_frame > 0.001 && !(vo_flags&256))
1897 *time_frame = timing_sleep(*time_frame);
1898 return frame_time_remaining;
1901 int reinit_video_chain(void) {
1902 sh_video_t * const sh_video = mpctx->sh_video;
1903 double ar=-1.0;
1904 //================== Init VIDEO (codec & libvo) ==========================
1905 if(!fixed_vo || !(inited_flags&INITED_VO)){
1906 current_module="preinit_libvo";
1908 //shouldn't we set dvideo->id=-2 when we fail?
1909 vo_config_count=0;
1910 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
1911 if(!(mpctx->video_out=init_best_video_out(video_driver_list))){
1912 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
1913 goto err_out;
1915 inited_flags|=INITED_VO;
1918 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
1919 mpctx->sh_video->stream_aspect = ar;
1920 current_module="init_video_filters";
1922 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
1923 sh_video->vfilter=(void*)vf_open_filter(NULL,"vo",vf_arg);
1925 #ifdef HAVE_MENU
1926 if(use_menu) {
1927 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
1928 vf_menu = vf_open_plugin(libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
1929 if(!vf_menu) {
1930 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
1931 use_menu = 0;
1934 if(vf_menu)
1935 sh_video->vfilter=(void*)vf_menu;
1936 #endif
1938 #ifdef USE_ASS
1939 if(ass_enabled) {
1940 int i;
1941 int insert = 1;
1942 if (vf_settings)
1943 for (i = 0; vf_settings[i].name; ++i)
1944 if (strcmp(vf_settings[i].name, "ass") == 0) {
1945 insert = 0;
1946 break;
1948 if (insert) {
1949 extern vf_info_t vf_info_ass;
1950 vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
1951 char* vf_arg[] = {"auto", "1", NULL};
1952 vf_instance_t* vf_ass = vf_open_plugin(libass_vfs,sh_video->vfilter,"ass",vf_arg);
1953 if (vf_ass)
1954 sh_video->vfilter=(void*)vf_ass;
1955 else
1956 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
1959 #endif
1961 sh_video->vfilter=(void*)append_filters(sh_video->vfilter);
1963 #ifdef USE_ASS
1964 if (ass_enabled)
1965 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
1966 #endif
1968 current_module="init_video_codec";
1970 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1971 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
1972 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1974 if(!sh_video->inited){
1975 if(!fixed_vo) uninit_player(INITED_VO);
1976 goto err_out;
1979 inited_flags|=INITED_VCODEC;
1981 if (sh_video->codec)
1982 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
1984 sh_video->last_pts = MP_NOPTS_VALUE;
1985 sh_video->num_buffered_pts = 0;
1986 sh_video->next_frame_time = 0;
1988 if(auto_quality>0){
1989 // Auto quality option enabled
1990 output_quality=get_video_quality_max(sh_video);
1991 if(auto_quality>output_quality) auto_quality=output_quality;
1992 else output_quality=auto_quality;
1993 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
1994 set_video_quality(sh_video,output_quality);
1997 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
1999 current_module="init_vo";
2001 return 1;
2003 err_out:
2004 mpctx->sh_video = mpctx->d_video->sh = NULL;
2005 return 0;
2008 static double update_video(int *blit_frame)
2010 sh_video_t * const sh_video = mpctx->sh_video;
2011 //-------------------- Decode a frame: -----------------------
2012 double frame_time;
2013 *blit_frame = 0; // Don't blit if we hit EOF
2014 if (!correct_pts) {
2015 unsigned char* start=NULL;
2016 void *decoded_frame;
2017 int drop_frame=0;
2018 int in_size;
2020 current_module = "video_read_frame";
2021 frame_time = sh_video->next_frame_time;
2022 in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2023 &start, force_fps);
2024 if (in_size < 0)
2025 return -1;
2026 if (in_size > max_framesize)
2027 max_framesize = in_size; // stats
2028 sh_video->timer += frame_time;
2029 if (mpctx->sh_audio)
2030 mpctx->delay -= frame_time;
2031 // video_read_frame can change fps (e.g. for ASF video)
2032 vo_fps = sh_video->fps;
2033 // check for frame-drop:
2034 current_module = "check_framedrop";
2035 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2036 static int dropped_frames;
2037 float delay = playback_speed*mpctx->audio_out->get_delay();
2038 float d = delay-mpctx->delay;
2039 // we should avoid dropping too many frames in sequence unless we
2040 // are too late. and we allow 100ms A-V delay here:
2041 if (d < -dropped_frames*frame_time-0.100 &&
2042 mpctx->osd_function != OSD_PAUSE) {
2043 drop_frame = frame_dropping;
2044 ++drop_frame_cnt;
2045 ++dropped_frames;
2046 } else
2047 drop_frame = dropped_frames = 0;
2048 ++total_frame_cnt;
2050 update_subtitles(sh_video, mpctx->d_sub, 0);
2051 update_teletext(sh_video, mpctx->demuxer, 0);
2052 update_osd_msg();
2053 current_module = "decode_video";
2054 decoded_frame = decode_video(sh_video, start, in_size, drop_frame,
2055 sh_video->pts);
2056 current_module = "filter_video";
2057 *blit_frame = (decoded_frame && filter_video(sh_video, decoded_frame,
2058 sh_video->pts));
2060 else {
2061 if (!generate_video_frame(sh_video, mpctx->d_video))
2062 return -1;
2063 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter,
2064 VFCTRL_GET_PTS, &sh_video->pts);
2065 if (sh_video->pts == MP_NOPTS_VALUE) {
2066 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts after filters MISSING\n");
2067 sh_video->pts = sh_video->last_pts;
2069 if (sh_video->last_pts == MP_NOPTS_VALUE)
2070 sh_video->last_pts= sh_video->pts;
2071 else if (sh_video->last_pts >= sh_video->pts) {
2072 sh_video->last_pts = sh_video->pts;
2073 mp_msg(MSGT_CPLAYER, MSGL_INFO, "pts value <= previous\n");
2075 frame_time = sh_video->pts - sh_video->last_pts;
2076 sh_video->last_pts = sh_video->pts;
2077 sh_video->timer += frame_time;
2078 if(mpctx->sh_audio)
2079 mpctx->delay -= frame_time;
2080 *blit_frame = 1;
2082 return frame_time;
2085 void pause_loop(void)
2087 mp_cmd_t* cmd;
2088 if (!quiet) {
2089 // Small hack to display the pause message on the OSD line.
2090 // The pause string is: "\n == PAUSE == \r" so we need to
2091 // take the first and the last char out
2092 if (term_osd && !mpctx->sh_video) {
2093 char msg[128] = MSGTR_Paused;
2094 int mlen = strlen(msg);
2095 msg[mlen-1] = '\0';
2096 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2097 update_osd_msg();
2098 } else
2099 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2100 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2102 #ifdef HAVE_NEW_GUI
2103 if (use_gui)
2104 guiGetEvent(guiCEvent, (char *)guiSetPause);
2105 #endif
2106 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2107 mpctx->video_out->control(VOCTRL_PAUSE, NULL);
2109 if (mpctx->audio_out && mpctx->sh_audio)
2110 mpctx->audio_out->pause(); // pause audio, keep data if possible
2112 while ( (cmd = mp_input_get_cmd(20, 1, 1)) == NULL
2113 || cmd->id == MP_CMD_SET_MOUSE_POS) {
2114 if (cmd) {
2115 cmd = mp_input_get_cmd(0,1,0);
2116 mp_cmd_free(cmd);
2117 continue;
2119 if (mpctx->sh_video && mpctx->video_out && vo_config_count)
2120 mpctx->video_out->check_events();
2121 #ifdef HAVE_NEW_GUI
2122 if (use_gui) {
2123 guiEventHandling();
2124 guiGetEvent(guiReDraw, NULL);
2125 if (guiIntfStruct.Playing!=2 || (rel_seek_secs || abs_seek_pos))
2126 break;
2128 #endif
2129 #ifdef HAVE_MENU
2130 if (vf_menu)
2131 vf_menu_pause_update(vf_menu);
2132 #endif
2133 usec_sleep(20000);
2135 if (cmd && cmd->id == MP_CMD_PAUSE) {
2136 cmd = mp_input_get_cmd(0,1,0);
2137 mp_cmd_free(cmd);
2139 mpctx->osd_function=OSD_PLAY;
2140 if (mpctx->audio_out && mpctx->sh_audio)
2141 mpctx->audio_out->resume(); // resume audio
2142 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2143 mpctx->video_out->control(VOCTRL_RESUME, NULL); // resume video
2144 (void)GetRelativeTime(); // ignore time that passed during pause
2145 #ifdef HAVE_NEW_GUI
2146 if (use_gui) {
2147 if (guiIntfStruct.Playing == guiSetStop)
2148 mpctx->eof = 1;
2149 else
2150 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2152 #endif
2155 void print_version(void){
2156 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s\n", MP_TITLE);
2158 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
2159 GetCpuCaps(&gCpuCaps);
2160 #ifdef ARCH_X86
2161 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
2162 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
2163 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
2164 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
2165 #ifdef RUNTIME_CPUDETECT
2166 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
2167 #else
2168 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
2169 #ifdef HAVE_MMX
2170 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
2171 #endif
2172 #ifdef HAVE_MMX2
2173 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
2174 #endif
2175 #ifdef HAVE_3DNOW
2176 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
2177 #endif
2178 #ifdef HAVE_3DNOWEX
2179 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
2180 #endif
2181 #ifdef HAVE_SSE
2182 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
2183 #endif
2184 #ifdef HAVE_SSE2
2185 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
2186 #endif
2187 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
2188 #endif /* RUNTIME_CPUDETECT */
2189 #endif /* ARCH_X86 */
2193 // Find the right mute status and record position for new file position
2194 static void edl_seek_reset(MPContext *mpctx)
2196 mpctx->edl_muted = 0;
2197 next_edl_record = edl_records;
2199 while (next_edl_record) {
2200 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2201 break;
2203 if (next_edl_record->action == EDL_MUTE)
2204 mpctx->edl_muted = !mpctx->edl_muted;
2205 next_edl_record = next_edl_record->next;
2207 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2208 mixer_mute(&mpctx->mixer);
2212 // Execute EDL command for the current position if one exists
2213 static void edl_update(MPContext *mpctx)
2215 if (!next_edl_record)
2216 return;
2218 if (!mpctx->sh_video) {
2219 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2220 free_edl(edl_records);
2221 next_edl_record = NULL;
2222 edl_records = NULL;
2223 return;
2226 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2227 if (next_edl_record->action == EDL_SKIP) {
2228 mpctx->osd_function = OSD_FFW;
2229 abs_seek_pos = 0;
2230 rel_seek_secs = next_edl_record->length_sec;
2231 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2232 "[%f], length [%f]\n", next_edl_record->start_sec,
2233 next_edl_record->stop_sec, next_edl_record->length_sec);
2234 edl_decision = 1;
2236 else if (next_edl_record->action == EDL_MUTE) {
2237 mpctx->edl_muted = !mpctx->edl_muted;
2238 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2239 mixer_mute(&mpctx->mixer);
2240 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2241 next_edl_record->start_sec );
2243 next_edl_record = next_edl_record->next;
2248 // style & 1 == 0 means seek relative to current position, == 1 means absolute
2249 // style & 2 == 0 means amount in seconds, == 2 means fraction of file length
2250 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2251 static int seek(MPContext *mpctx, double amount, int style)
2253 current_module = "seek";
2254 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2255 return -1;
2257 if (mpctx->sh_video) {
2258 current_module = "seek_video_reset";
2259 resync_video_stream(mpctx->sh_video);
2260 if (vo_config_count)
2261 mpctx->video_out->control(VOCTRL_RESET, NULL);
2262 mpctx->sh_video->num_buffered_pts = 0;
2263 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2264 mpctx->num_buffered_frames = 0;
2265 mpctx->delay = 0;
2266 // Not all demuxers set d_video->pts during seek, so this value
2267 // (which is used by at least vobsub and edl code below) may
2268 // be completely wrong (probably 0).
2269 mpctx->sh_video->pts = mpctx->d_video->pts;
2270 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2271 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2274 if (mpctx->sh_audio) {
2275 current_module = "seek_audio_reset";
2276 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2277 mpctx->sh_audio->a_buffer_len = 0;
2278 mpctx->sh_audio->a_out_buffer_len = 0;
2281 if (vo_vobsub && mpctx->sh_video) {
2282 current_module = "seek_vobsub_reset";
2283 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2286 edl_seek_reset(mpctx);
2288 c_total = 0;
2289 max_pts_correction = 0.1;
2290 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2291 drop_frame_cnt = 0;
2293 current_module = NULL;
2294 return 0;
2297 int main(int argc,char* argv[]){
2300 char * mem_ptr;
2302 // movie info:
2304 /* Flag indicating whether MPlayer should exit without playing anything. */
2305 int opt_exit = 0;
2307 //float a_frame=0; // Audio
2309 int i;
2311 int gui_no_filename=0;
2313 srand((int) time(NULL));
2315 InitTimer();
2317 mp_msg_init();
2319 for(i=1; i<argc; i++)
2320 if(!strcmp(argv[i], "-really-quiet"))
2321 verbose= -10;
2323 print_version();
2324 #if defined(WIN32) && defined(USE_WIN32DLL)
2325 set_path_env();
2326 #endif /*WIN32 && USE_WIN32DLL*/
2328 #ifdef USE_TV
2329 stream_tv_defaults.immediate = 1;
2330 #endif
2332 if (argc > 1 && argv[1] &&
2333 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2334 use_gui = !strcmp(argv[1], "-gui");
2335 } else
2336 if ( argv[0] )
2338 char *base = strrchr(argv[0], '/');
2339 if (!base)
2340 base = strrchr(argv[0], '\\');
2341 if (!base)
2342 base = argv[0];
2343 if(strstr(base, "gmplayer"))
2344 use_gui=1;
2347 mconfig = m_config_new();
2348 m_config_register_options(mconfig,mplayer_opts);
2349 // TODO : add something to let modules register their options
2350 mp_input_register_options(mconfig);
2351 parse_cfgfiles(mconfig);
2353 #ifdef HAVE_NEW_GUI
2354 if ( use_gui ) cfg_read();
2355 #endif
2357 mpctx->playtree = m_config_parse_mp_command_line(mconfig, argc, argv);
2358 if(mpctx->playtree == NULL)
2359 opt_exit = 1;
2360 else {
2361 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2362 if(mpctx->playtree) {
2363 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
2364 if(mpctx->playtree_iter) {
2365 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2366 play_tree_iter_free(mpctx->playtree_iter);
2367 mpctx->playtree_iter = NULL;
2369 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2374 #if defined(WIN32) && defined(HAVE_NEW_GUI)
2375 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2376 if(runningmplayer && filename && use_gui){
2377 COPYDATASTRUCT csData;
2378 char file[MAX_PATH];
2379 char *filepart = filename;
2380 if(GetFullPathName(filename, MAX_PATH, file, &filepart)){
2381 csData.dwData = 0;
2382 csData.cbData = strlen(file)*2;
2383 csData.lpData = file;
2384 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2387 #endif
2389 #ifdef WIN32
2390 if(proc_priority){
2391 int i;
2392 for(i=0; priority_presets_defs[i].name; i++){
2393 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
2394 break;
2396 mp_msg(MSGT_CPLAYER,MSGL_STATUS,"Setting process priority: %s\n",
2397 priority_presets_defs[i].name);
2398 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
2400 #endif
2401 #ifndef HAVE_NEW_GUI
2402 if(use_gui){
2403 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2404 use_gui=0;
2406 #else
2407 #ifndef WIN32
2408 if(use_gui && !vo_init()){
2409 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2410 use_gui=0;
2412 #endif
2413 if (use_gui && mpctx->playtree_iter){
2414 char cwd[PATH_MAX+2];
2415 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2416 play_tree_iter_free(mpctx->playtree_iter);
2417 mpctx->playtree_iter=NULL;
2419 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2421 strcat(cwd, "/");
2422 // Prefix relative paths with current working directory
2423 play_tree_add_bpf(mpctx->playtree, cwd);
2425 // Import initital playtree into GUI.
2426 import_initial_playtree_into_gui(mpctx->playtree, mconfig, enqueue);
2428 #endif /* HAVE_NEW_GUI */
2430 if(video_driver_list && strcmp(video_driver_list[0],"help")==0){
2431 list_video_out();
2432 opt_exit = 1;
2435 if(audio_driver_list && strcmp(audio_driver_list[0],"help")==0){
2436 list_audio_out();
2437 opt_exit = 1;
2440 /* Check codecs.conf. */
2441 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2442 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2443 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2444 if(!parse_codec_cfg(NULL)){
2445 exit_player_with_rc(NULL, 0);
2447 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2450 free( mem_ptr ); // release the buffer created by get_path()
2453 #if 0
2454 if(video_codec_list){
2455 int i;
2456 video_codec=video_codec_list[0];
2457 for(i=0;video_codec_list[i];i++)
2458 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2460 #endif
2461 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2462 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2463 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2464 list_codecs(1);
2465 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2466 opt_exit = 1;
2468 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2469 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2470 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2471 list_codecs(0);
2472 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2473 opt_exit = 1;
2475 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2476 vfm_help();
2477 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2478 opt_exit = 1;
2480 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2481 afm_help();
2482 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2483 opt_exit = 1;
2485 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2486 af_help();
2487 printf("\n");
2488 opt_exit = 1;
2490 #ifdef HAVE_X11
2491 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2492 fstype_help();
2493 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2494 opt_exit = 1;
2496 #endif
2497 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2498 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2499 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2500 demuxer_help();
2501 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2502 opt_exit = 1;
2504 if(list_properties) {
2505 property_print_help();
2506 opt_exit = 1;
2509 if(opt_exit)
2510 exit_player(NULL);
2512 if (player_idle_mode && use_gui) {
2513 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2514 exit_player_with_rc(NULL, 1);
2517 if(!filename && !player_idle_mode){
2518 if(!use_gui){
2519 // no file/vcd/dvd -> show HELP:
2520 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2521 exit_player_with_rc(NULL, 0);
2522 } else gui_no_filename=1;
2525 /* Display what configure line was used */
2526 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2528 // Many users forget to include command line in bugreports...
2529 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2530 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2531 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2532 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2535 //------ load global data first ------
2537 // check font
2538 #ifdef HAVE_FREETYPE
2539 init_freetype();
2540 #endif
2541 #ifdef HAVE_FONTCONFIG
2542 if(font_fontconfig <= 0)
2544 #endif
2545 #ifdef HAVE_BITMAP_FONT
2546 if(font_name){
2547 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2548 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2549 filename_recode(font_name));
2550 } else {
2551 // try default:
2552 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2553 free(mem_ptr); // release the buffer created by get_path()
2554 if(!vo_font)
2555 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2557 if (sub_font_name)
2558 sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2559 else
2560 sub_font = vo_font;
2561 #endif
2562 #ifdef HAVE_FONTCONFIG
2564 #endif
2566 vo_init_osd();
2568 #ifdef USE_ASS
2569 ass_library = ass_init();
2570 #endif
2572 #ifdef HAVE_RTC
2573 if(!nortc)
2575 // seteuid(0); /* Can't hurt to try to get root here */
2576 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2577 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2578 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2579 else {
2580 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2582 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2583 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2584 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2585 close (rtc_fd);
2586 rtc_fd = -1;
2587 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2588 /* variable only by the root */
2589 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2590 close (rtc_fd);
2591 rtc_fd = -1;
2592 } else
2593 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2596 #ifdef HAVE_NEW_GUI
2597 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2598 // and now ? -- Pontscho
2599 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2600 #endif
2601 if(rtc_fd<0)
2602 #endif /* HAVE_RTC */
2603 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2604 softsleep?"software":timer_name);
2606 #ifdef USE_TERMCAP
2607 if ( !use_gui ) load_termcap(NULL); // load key-codes
2608 #endif
2610 // ========== Init keyboard FIFO (connection to libvo) ============
2612 // Init input system
2613 current_module = "init_input";
2614 mp_input_init(use_gui);
2615 mp_input_add_key_fd(-1,0,mplayer_get_key,NULL);
2616 if(slave_mode)
2617 #ifndef __MINGW32__
2618 mp_input_add_cmd_fd(0,1,NULL,NULL);
2619 #else
2620 mp_input_add_cmd_fd(0,0,mp_input_win32_slave_cmd_func,NULL);
2621 #endif
2622 else if(!noconsolecontrols)
2623 mp_input_add_event_fd(0, getch2);
2625 #ifdef HAVE_MENU
2626 if(use_menu) {
2627 if(menu_cfg && menu_init(mpctx, menu_cfg))
2628 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2629 else {
2630 menu_cfg = get_path("menu.conf");
2631 if(menu_init(mpctx, menu_cfg))
2632 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2633 else {
2634 if(menu_init(mpctx, MPLAYER_CONFDIR "/menu.conf"))
2635 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2636 else {
2637 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2638 use_menu = 0;
2643 #endif
2645 inited_flags|=INITED_INPUT;
2646 current_module = NULL;
2648 /// Catch signals
2649 #ifndef __MINGW32__
2650 signal(SIGCHLD,child_sighandler);
2651 #endif
2653 #ifdef CRASH_DEBUG
2654 prog_path = argv[0];
2655 #endif
2656 //========= Catch terminate signals: ================
2657 // terminate requests:
2658 signal(SIGTERM,exit_sighandler); // kill
2659 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2661 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2663 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2664 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2665 #ifdef ENABLE_SIGHANDLER
2666 // fatal errors:
2667 signal(SIGBUS,exit_sighandler); // bus error
2668 signal(SIGSEGV,exit_sighandler); // segfault
2669 signal(SIGILL,exit_sighandler); // illegal instruction
2670 signal(SIGFPE,exit_sighandler); // floating point exc.
2671 signal(SIGABRT,exit_sighandler); // abort()
2672 #ifdef CRASH_DEBUG
2673 if (crash_debug)
2674 signal(SIGTRAP,exit_sighandler);
2675 #endif
2676 #endif
2678 #ifdef HAVE_NEW_GUI
2679 if(use_gui){
2680 guiInit();
2681 guiGetEvent(guiSetContext, mpctx);
2682 inited_flags|=INITED_GUI;
2683 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2685 #endif
2687 // ******************* Now, let's see the per-file stuff ********************
2689 play_next_file:
2691 // init global sub numbers
2692 mpctx->global_sub_size = 0;
2693 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2695 if (filename) load_per_file_config (mconfig, filename);
2697 // We must enable getch2 here to be able to interrupt network connection
2698 // or cache filling
2699 if(!noconsolecontrols && !slave_mode){
2700 if(inited_flags&INITED_GETCH2)
2701 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2702 else
2703 getch2_enable(); // prepare stdin for hotkeys...
2704 inited_flags|=INITED_GETCH2;
2705 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2708 // =================== GUI idle loop (STOP state) ===========================
2709 #ifdef HAVE_NEW_GUI
2710 if ( use_gui ) {
2711 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
2712 guiGetEvent( guiSetDefaults,0 );
2713 while ( guiIntfStruct.Playing != 1 )
2715 mp_cmd_t* cmd;
2716 usec_sleep(20000);
2717 guiEventHandling();
2718 guiGetEvent( guiReDraw,NULL );
2719 if ( (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
2720 guiGetEvent(guiIEvent, (char *)cmd->id);
2721 mp_cmd_free(cmd);
2724 guiGetEvent( guiSetParameters,NULL );
2725 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
2727 play_tree_t * entry = play_tree_new();
2728 play_tree_add_file( entry,guiIntfStruct.Filename );
2729 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
2730 else mpctx->playtree=play_tree_new();
2731 play_tree_set_child( mpctx->playtree,entry );
2732 if(mpctx->playtree)
2734 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
2735 if(mpctx->playtree_iter)
2737 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
2739 play_tree_iter_free(mpctx->playtree_iter);
2740 mpctx->playtree_iter = NULL;
2742 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2747 #endif /* HAVE_NEW_GUI */
2749 while (player_idle_mode && !filename) {
2750 play_tree_t * entry = NULL;
2751 mp_cmd_t * cmd;
2752 while (!(cmd = mp_input_get_cmd(0,1,0))) { // wait for command
2753 if (mpctx->video_out && vo_config_count) mpctx->video_out->check_events();
2754 usec_sleep(20000);
2756 switch (cmd->id) {
2757 case MP_CMD_LOADFILE:
2758 // prepare a tree entry with the new filename
2759 entry = play_tree_new();
2760 play_tree_add_file(entry, cmd->args[0].v.s);
2761 // The entry is added to the main playtree after the switch().
2762 break;
2763 case MP_CMD_LOADLIST:
2764 entry = parse_playlist_file(cmd->args[0].v.s);
2765 break;
2766 case MP_CMD_QUIT:
2767 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
2768 break;
2771 mp_cmd_free(cmd);
2773 if (entry) { // user entered a command that gave a valid entry
2774 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
2775 play_tree_free_list(mpctx->playtree->child, 1);
2776 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
2778 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
2780 play_tree_set_child(mpctx->playtree, entry);
2782 /* Make iterator start at the top the of tree. */
2783 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mconfig);
2784 if (!mpctx->playtree_iter) continue;
2786 // find the first real item in the tree
2787 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2788 // no items!
2789 play_tree_iter_free(mpctx->playtree_iter);
2790 mpctx->playtree_iter = NULL;
2791 continue; // wait for next command
2793 filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
2796 //---------------------------------------------------------------------------
2798 if(filename)
2799 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
2800 filename_recode(filename));
2802 if (edl_filename) {
2803 if (edl_records) free_edl(edl_records);
2804 next_edl_record = edl_records = edl_parse_file();
2806 if (edl_output_filename) {
2807 if (edl_fd) fclose(edl_fd);
2808 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
2810 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
2811 filename_recode(edl_output_filename));
2815 //==================== Open VOB-Sub ============================
2817 current_module="vobsub";
2818 if (vobsub_name){
2819 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
2820 if(vo_vobsub==NULL)
2821 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
2822 filename_recode(vobsub_name));
2823 }else if(sub_auto && filename && (strlen(filename)>=5)){
2824 /* try to autodetect vobsub from movie filename ::atmos */
2825 char *buf = malloc((strlen(filename)-3)),*psub;
2826 memset(buf,0,strlen(filename)-3); // make sure string is terminated
2827 strncpy(buf, filename, strlen(filename)-4);
2828 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
2829 /* try from ~/.mplayer/sub */
2830 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
2831 char *bname;
2832 int l;
2833 bname = strrchr(buf,'/');
2834 #ifdef WIN32
2835 if(!bname) bname = strrchr(buf,'\\');
2836 #endif
2837 if(bname) bname++;
2838 else bname = buf;
2839 l = strlen(psub) + strlen(bname) + 1;
2840 psub = realloc(psub,l);
2841 strcat(psub,bname);
2842 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
2843 free(psub);
2845 free(buf);
2847 if(vo_vobsub){
2848 inited_flags|=INITED_VOBSUB;
2849 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
2850 // check if vobsub requested only to display forced subtitles
2851 forced_subs_only=vobsub_get_forced_subs_flag(vo_vobsub);
2853 // setup global sub numbering
2854 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
2855 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
2858 //============ Open & Sync STREAM --- fork cache2 ====================
2860 mpctx->stream=NULL;
2861 mpctx->demuxer=NULL;
2862 if (mpctx->d_audio) {
2863 //free_demuxer_stream(mpctx->d_audio);
2864 mpctx->d_audio=NULL;
2866 if (mpctx->d_video) {
2867 //free_demuxer_stream(d_video);
2868 mpctx->d_video=NULL;
2870 mpctx->sh_audio=NULL;
2871 mpctx->sh_video=NULL;
2873 current_module="open_stream";
2874 mpctx->stream=open_stream(filename,0,&mpctx->file_format);
2875 if(!mpctx->stream) { // error...
2876 mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY);
2877 goto goto_next_file;
2879 inited_flags|=INITED_STREAM;
2881 #ifdef HAVE_NEW_GUI
2882 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
2883 #endif
2885 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
2886 play_tree_t* entry;
2887 // Handle playlist
2888 current_module="handle_playlist";
2889 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
2890 filename_recode(filename));
2891 entry = parse_playtree(mpctx->stream,0);
2892 mpctx->eof=playtree_add_playlist(entry);
2893 goto goto_next_file;
2895 mpctx->stream->start_pos+=seek_to_byte;
2897 if(stream_dump_type==5){
2898 unsigned char buf[4096];
2899 int len;
2900 FILE *f;
2901 current_module="dumpstream";
2902 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
2903 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
2904 exit_player(MSGTR_Exit_error);
2906 stream_reset(mpctx->stream);
2907 stream_seek(mpctx->stream,mpctx->stream->start_pos);
2908 f=fopen(stream_dump_name,"wb");
2909 if(!f){
2910 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
2911 exit_player(MSGTR_Exit_error);
2913 while(!mpctx->stream->eof){
2914 len=stream_read(mpctx->stream,buf,4096);
2915 if(len>0) {
2916 if(fwrite(buf,len,1,f) != 1) {
2917 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
2918 exit_player(MSGTR_Exit_error);
2922 if(fclose(f)) {
2923 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
2924 exit_player(MSGTR_Exit_error);
2926 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
2927 exit_player_with_rc(MSGTR_Exit_eof, 0);
2930 #ifdef USE_DVDREAD
2931 if(mpctx->stream->type==STREAMTYPE_DVD){
2932 current_module="dvd lang->id";
2933 if(audio_id==-1) audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
2934 if(dvdsub_lang && dvdsub_id==-2) dvdsub_id=-1;
2935 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
2936 // setup global sub numbering
2937 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
2938 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
2939 current_module=NULL;
2941 #endif
2943 #ifdef USE_DVDNAV
2944 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
2945 current_module="dvdnav lang->id";
2946 if(audio_id==-1) audio_id=dvdnav_aid_from_lang(mpctx->stream,audio_lang);
2947 if(dvdsub_lang && dvdsub_id==-2) dvdsub_id=-1;
2948 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
2949 // setup global sub numbering
2950 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
2951 mpctx->global_sub_size += dvdnav_number_of_subs(mpctx->stream);
2952 current_module=NULL;
2954 #endif
2956 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
2957 goto_enable_cache:
2958 if(stream_cache_size>0){
2959 current_module="enable_cache";
2960 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
2961 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
2962 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
2963 if((mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY))) goto goto_next_file;
2966 //============ Open DEMUXERS --- DETECT file type =======================
2967 current_module="demux_open";
2969 mpctx->demuxer=demux_open(mpctx->stream,mpctx->file_format,audio_id,video_id,dvdsub_id,filename);
2971 // HACK to get MOV Reference Files working
2973 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
2975 unsigned char* playlist_entry;
2976 play_tree_t *list = NULL, *entry = NULL;
2978 current_module="handle_demux_playlist";
2979 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
2981 char *temp, *bname;
2983 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
2984 filename_recode(playlist_entry));
2986 bname=mp_basename(playlist_entry);
2987 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
2988 continue;
2990 if (!strncmp(bname,mp_basename(filename),strlen(bname))) // ignoring self-reference
2991 continue;
2993 entry = play_tree_new();
2995 if (filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
2997 temp=malloc((strlen(filename)-strlen(mp_basename(filename))+strlen(playlist_entry)+1));
2998 if (temp)
3000 strncpy(temp, filename, strlen(filename)-strlen(mp_basename(filename)));
3001 temp[strlen(filename)-strlen(mp_basename(filename))]='\0';
3002 strcat(temp, playlist_entry);
3003 play_tree_add_file(entry,temp);
3004 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3005 free(temp);
3008 else
3009 play_tree_add_file(entry,playlist_entry);
3011 if(!list)
3012 list = entry;
3013 else
3014 play_tree_append_entry(list,entry);
3016 free_demuxer(mpctx->demuxer);
3017 mpctx->demuxer = NULL;
3019 if (list)
3021 entry = play_tree_new();
3022 play_tree_set_child(entry,list);
3023 mpctx->eof=playtree_add_playlist(entry);
3024 goto goto_next_file;
3028 if(!mpctx->demuxer)
3029 goto goto_next_file;
3030 inited_flags|=INITED_DEMUXER;
3032 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3033 int i;
3034 int maxid = -1;
3035 // setup global sub numbering
3036 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3037 for (i = 0; i < MAX_S_STREAMS; i++)
3038 if (mpctx->demuxer->s_streams[i])
3039 maxid = FFMAX(maxid, ((sh_sub_t *)mpctx->demuxer->s_streams[i])->sid);
3040 mpctx->global_sub_size += maxid + 1;
3042 // Make dvdsub_id always selectable if set.
3043 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id)
3044 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id + 1;
3046 current_module="demux_open2";
3048 //file_format=demuxer->file_format;
3050 mpctx->d_audio=mpctx->demuxer->audio;
3051 mpctx->d_video=mpctx->demuxer->video;
3052 mpctx->d_sub=mpctx->demuxer->sub;
3054 // DUMP STREAMS:
3055 if((stream_dump_type)&&(stream_dump_type<4)){
3056 FILE *f;
3057 demux_stream_t *ds=NULL;
3058 current_module="dump";
3059 // select stream to dump
3060 switch(stream_dump_type){
3061 case 1: ds=mpctx->d_audio;break;
3062 case 2: ds=mpctx->d_video;break;
3063 case 3: ds=mpctx->d_sub;break;
3065 if(!ds){
3066 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3067 exit_player(MSGTR_Exit_error);
3069 // disable other streams:
3070 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3071 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3072 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3073 // let's dump it!
3074 f=fopen(stream_dump_name,"wb");
3075 if(!f){
3076 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3077 exit_player(MSGTR_Exit_error);
3079 while(!ds->eof){
3080 unsigned char* start;
3081 int in_size=ds_get_packet(ds,&start);
3082 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3083 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3084 if(in_size>0) fwrite(start,in_size,1,f);
3086 fclose(f);
3087 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3088 exit_player_with_rc(MSGTR_Exit_eof, 0);
3091 mpctx->sh_audio=mpctx->d_audio->sh;
3092 mpctx->sh_video=mpctx->d_video->sh;
3094 if(mpctx->sh_video){
3096 current_module="video_read_properties";
3097 if(!video_read_properties(mpctx->sh_video)) {
3098 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3099 mpctx->sh_video=mpctx->d_video->sh=NULL;
3100 } else {
3101 mp_msg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
3102 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3103 mpctx->sh_video->fps,mpctx->sh_video->frametime
3106 /* need to set fps here for output encoders to pick it up in their init */
3107 if(force_fps){
3108 mpctx->sh_video->fps=force_fps;
3109 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3111 vo_fps = mpctx->sh_video->fps;
3113 if(!mpctx->sh_video->fps && !force_fps){
3114 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3115 mpctx->sh_video=mpctx->d_video->sh=NULL;
3121 if(!mpctx->sh_video && !mpctx->sh_audio){
3122 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3123 #ifdef HAS_DVBIN_SUPPORT
3124 if(mpctx->stream->type == STREAMTYPE_DVB)
3126 int dir;
3127 int v = mpctx->last_dvb_step;
3128 if(v > 0)
3129 dir = DVB_CHANNEL_HIGHER;
3130 else
3131 dir = DVB_CHANNEL_LOWER;
3133 if(dvb_step_channel(mpctx->stream, dir))
3134 mpctx->eof = mpctx->dvbin_reopen = 1;
3136 #endif
3137 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3140 /* display clip info */
3141 demux_info_print(mpctx->demuxer);
3143 //================== Read SUBTITLES (DVD & TEXT) ==========================
3144 if(vo_spudec==NULL && mpctx->sh_video &&
3145 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV || mpctx->d_sub->id >= 0)){
3146 init_vo_spudec();
3149 // Apply current settings for forced subs
3150 if (vo_spudec!=NULL)
3151 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
3153 if(mpctx->sh_video) {
3154 // after reading video params we should load subtitles because
3155 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3156 // check .sub
3157 current_module="read_subtitles_file";
3158 if(sub_name){
3159 for (i = 0; sub_name[i] != NULL; ++i)
3160 add_subtitles (sub_name[i], mpctx->sh_video->fps, 0);
3162 if(sub_auto) { // auto load sub file ...
3163 char *psub = get_path( "sub/" );
3164 char **tmp = sub_filenames((psub ? psub : ""), filename);
3165 int i = 0;
3166 free(psub); // release the buffer created by get_path() above
3167 while (tmp[i]) {
3168 add_subtitles (tmp[i], mpctx->sh_video->fps, 1);
3169 free(tmp[i++]);
3171 free(tmp);
3173 if (mpctx->set_of_sub_size > 0) {
3174 // setup global sub numbering
3175 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3176 mpctx->global_sub_size += mpctx->set_of_sub_size;
3180 if (mpctx->global_sub_size) {
3181 // find the best sub to use
3182 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3183 if (vobsub_index_id >= 0) {
3184 // if user asks for a vobsub id, use that first.
3185 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3186 } else if (dvdsub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3187 // if user asks for a dvd sub id, use that next.
3188 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id;
3189 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3190 // if there are text subs to use, use those. (autosubs come last here)
3191 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3193 } else if (mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3194 // if nothing else works, get subs from the demuxer.
3195 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX];
3197 } else {
3198 // nothing worth doing automatically.
3199 mpctx->global_sub_pos = -1;
3201 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3202 mpctx->global_sub_pos--;
3203 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3204 if(subdata)
3205 switch (stream_dump_type) {
3206 case 3: list_sub_file(subdata); break;
3207 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3208 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3209 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3210 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3211 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3215 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3216 filename_recode(filename));
3217 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3218 if (mpctx->sh_video) {
3219 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3220 if (mpctx->sh_video->format >= 0x20202020)
3221 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3222 else
3223 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3224 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3225 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3226 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3227 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3228 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3230 if (mpctx->sh_audio) {
3231 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3232 if (mpctx->sh_audio->format >= 0x20202020)
3233 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3234 else
3235 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3236 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3237 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3238 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3240 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3242 if(!mpctx->sh_video) goto main; // audio-only
3244 if(!reinit_video_chain()) {
3245 if(!mpctx->sh_video){
3246 if(!mpctx->sh_audio) goto goto_next_file;
3247 goto main; // exit_player(MSGTR_Exit_error);
3251 if(vo_flags & 0x08 && vo_spudec)
3252 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3254 #ifdef HAVE_FREETYPE
3255 force_load_font = 1;
3256 #endif
3258 //================== MAIN: ==========================
3259 main:
3260 current_module="main";
3262 if(playing_msg) {
3263 char* msg = property_expand_string(mpctx, playing_msg);
3264 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3265 free(msg);
3269 // Disable the term OSD in verbose mode
3270 if(verbose) term_osd = 0;
3273 //int frame_corr_num=0; //
3274 //float v_frame=0; // Video
3275 float time_frame=0; // Timer
3276 //float num_frames=0; // number of frames played
3278 int frame_time_remaining=0; // flag
3279 int blit_frame=0;
3280 mpctx->num_buffered_frames=0;
3282 // Make sure old OSD does not stay around,
3283 // e.g. with -fixed-vo and same-resolution files
3284 clear_osd_msgs();
3285 update_osd_msg();
3287 //================ SETUP AUDIO ==========================
3289 if(mpctx->sh_audio){
3290 reinit_audio_chain();
3291 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3292 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3295 current_module="av_init";
3297 if(mpctx->sh_video){
3298 mpctx->sh_video->timer=0;
3299 if (! ignore_start)
3300 audio_delay += mpctx->sh_video->stream_delay;
3302 if(mpctx->sh_audio){
3303 if (! ignore_start)
3304 audio_delay -= mpctx->sh_audio->stream_delay;
3305 mpctx->delay=-audio_delay;
3308 if(!mpctx->sh_audio){
3309 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3310 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3311 ds_free_packs(mpctx->d_audio); // free buffered chunks
3312 //mpctx->d_audio->id=-2; // do not read audio chunks
3313 //uninit_player(INITED_AO); // close device
3315 if(!mpctx->sh_video){
3316 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3317 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3318 ds_free_packs(mpctx->d_video);
3319 mpctx->d_video->id=-2;
3320 //if(!fixed_vo) uninit_player(INITED_VO);
3323 if (!mpctx->sh_video && !mpctx->sh_audio)
3324 goto goto_next_file;
3326 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3327 if(force_fps && mpctx->sh_video){
3328 vo_fps = mpctx->sh_video->fps=force_fps;
3329 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3330 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3333 #ifdef HAVE_NEW_GUI
3334 if ( use_gui ) {
3335 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3336 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3337 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3338 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3339 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3341 #endif
3343 mp_input_set_section(NULL);
3344 //TODO: add desired (stream-based) sections here
3345 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section("tv");
3347 //==================== START PLAYING =======================
3349 if(mpctx->loop_times>1) mpctx->loop_times--; else
3350 if(mpctx->loop_times==1) mpctx->loop_times = -1;
3352 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3354 total_time_usage_start=GetTimer();
3355 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3356 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3357 play_n_frames=play_n_frames_mf;
3359 if(play_n_frames==0){
3360 mpctx->eof=PT_NEXT_ENTRY; goto goto_next_file;
3363 if (seek_to_sec) {
3364 seek(mpctx, seek_to_sec, 1);
3365 end_at.pos += seek_to_sec;
3368 if (end_at.type == END_AT_SIZE) {
3369 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3370 end_at.type = END_AT_NONE;
3374 while(!mpctx->eof){
3375 float aq_sleep_time=0;
3376 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3377 mpctx->sh_audio = mpctx->d_audio->sh;
3378 mpctx->sh_audio->ds = mpctx->d_audio;
3379 reinit_audio_chain();
3382 /*========================== PLAY AUDIO ============================*/
3384 if (mpctx->sh_audio)
3385 if (!fill_audio_out_buffers())
3386 // at eof, all audio at least written to ao
3387 if (!mpctx->sh_video)
3388 mpctx->eof = PT_NEXT_ENTRY;
3391 if(!mpctx->sh_video) {
3392 // handle audio-only case:
3393 double a_pos=0;
3394 if(!quiet || end_at.type == END_AT_TIME )
3395 a_pos = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3397 if(!quiet)
3398 print_status(a_pos, 0, 0);
3400 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3401 mpctx->eof = PT_NEXT_ENTRY;
3402 update_osd_msg();
3404 } else {
3406 /*========================== PLAY VIDEO ============================*/
3408 vo_pts=mpctx->sh_video->timer*90000.0;
3409 vo_fps=mpctx->sh_video->fps;
3411 if (!mpctx->num_buffered_frames) {
3412 double frame_time = update_video(&blit_frame);
3413 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3414 if (mpctx->sh_video->vf_inited < 0) {
3415 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3416 mpctx->eof = 1; goto goto_next_file;
3418 if (frame_time < 0)
3419 mpctx->eof = 1;
3420 else {
3421 // might return with !eof && !blit_frame if !correct_pts
3422 mpctx->num_buffered_frames += blit_frame;
3423 time_frame += frame_time / playback_speed; // for nosound
3427 // ==========================================================================
3429 // current_module="draw_osd";
3430 // if(vo_config_count) mpctx->video_out->draw_osd();
3432 #ifdef HAVE_NEW_GUI
3433 if(use_gui) guiEventHandling();
3434 #endif
3436 current_module="vo_check_events";
3437 if (vo_config_count) mpctx->video_out->check_events();
3439 #ifdef HAVE_X11
3440 if (stop_xscreensaver) {
3441 current_module = "stop_xscreensaver";
3442 xscreensaver_heartbeat();
3444 #endif
3446 frame_time_remaining = sleep_until_update(&time_frame, &aq_sleep_time);
3448 //====================== FLIP PAGE (VIDEO BLT): =========================
3450 current_module="flip_page";
3451 if (!frame_time_remaining && blit_frame) {
3452 unsigned int t2=GetTimer();
3454 if(vo_config_count) mpctx->video_out->flip_page();
3455 mpctx->num_buffered_frames--;
3457 vout_time_usage += (GetTimer() - t2) * 0.000001;
3459 //====================== A-V TIMESTAMP CORRECTION: =========================
3461 adjust_sync_and_print_status(frame_time_remaining, time_frame);
3463 //============================ Auto QUALITY ============================
3465 /*Output quality adjustments:*/
3466 if(auto_quality>0){
3467 current_module="autoq";
3468 // float total=0.000001f * (GetTimer()-aq_total_time);
3469 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3470 if(output_quality<auto_quality && aq_sleep_time>0)
3471 ++output_quality;
3472 else
3473 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3474 if(output_quality>1 && aq_sleep_time<0)
3475 --output_quality;
3476 else
3477 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3478 output_quality=0;
3479 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3480 set_video_quality(mpctx->sh_video,output_quality);
3483 if (play_n_frames >= 0 && !frame_time_remaining && blit_frame) {
3484 --play_n_frames;
3485 if (play_n_frames <= 0) mpctx->eof = PT_NEXT_ENTRY;
3489 // FIXME: add size based support for -endpos
3490 if (end_at.type == END_AT_TIME &&
3491 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3492 mpctx->eof = PT_NEXT_ENTRY;
3494 } // end if(mpctx->sh_video)
3496 #ifdef USE_DVDNAV
3497 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3498 nav_highlight_t hl;
3499 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3500 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3501 vo_osd_changed (OSDTYPE_DVDNAV);
3503 #endif
3505 //============================ Handle PAUSE ===============================
3507 current_module="pause";
3509 if (mpctx->osd_function == OSD_PAUSE) {
3510 pause_loop();
3511 mpctx->was_paused = 1;
3514 // handle -sstep
3515 if(step_sec>0) {
3516 mpctx->osd_function=OSD_FFW;
3517 rel_seek_secs+=step_sec;
3520 edl_update(mpctx);
3522 //================= Keyboard events, SEEKing ====================
3524 current_module="key_events";
3527 mp_cmd_t* cmd;
3528 int brk_cmd = 0;
3529 while( !brk_cmd && (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
3530 brk_cmd = run_command(mpctx, cmd);
3531 mp_cmd_free(cmd);
3532 if (brk_cmd == 2)
3533 goto goto_enable_cache;
3536 mpctx->was_paused = 0;
3538 /* Looping. */
3539 if(mpctx->eof==1 && mpctx->loop_times>=0) {
3540 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d, eof = %d\n", mpctx->loop_times,mpctx->eof);
3542 if(mpctx->loop_times>1) mpctx->loop_times--; else
3543 if(mpctx->loop_times==1) mpctx->loop_times=-1;
3544 play_n_frames=play_n_frames_mf;
3545 mpctx->eof=0;
3546 abs_seek_pos=1; rel_seek_secs=seek_to_sec;
3547 loop_seek = 1;
3550 if(rel_seek_secs || abs_seek_pos){
3551 if (seek(mpctx, rel_seek_secs, abs_seek_pos) >= 0) {
3552 // Set OSD:
3553 if(!loop_seek){
3554 if( !edl_decision )
3555 set_osd_bar(0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3559 rel_seek_secs=0;
3560 abs_seek_pos=0;
3561 loop_seek=0;
3562 edl_decision = 0;
3565 #ifdef HAVE_NEW_GUI
3566 if(use_gui){
3567 guiEventHandling();
3568 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3569 // get pos from frame number / total frames
3570 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3571 } else {
3572 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3574 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3575 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3576 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3577 guiGetEvent( guiReDraw,NULL );
3578 guiGetEvent( guiSetVolume,NULL );
3579 if(guiIntfStruct.Playing==0) break; // STOP
3580 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3581 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3582 #ifdef USE_DVDREAD
3583 if ( mpctx->stream->type == STREAMTYPE_DVD )
3585 dvd_priv_t * dvdp = mpctx->stream->priv;
3586 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3588 #endif
3590 #endif /* HAVE_NEW_GUI */
3592 } // while(!mpctx->eof)
3594 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->eof);
3596 #ifdef HAS_DVBIN_SUPPORT
3597 if(mpctx->dvbin_reopen)
3599 mpctx->eof = 0;
3600 uninit_player(INITED_ALL-(INITED_GUI|INITED_STREAM|INITED_INPUT|INITED_GETCH2|(fixed_vo?INITED_VO:0)));
3601 cache_uninit(mpctx->stream);
3602 mpctx->dvbin_reopen = 0;
3603 goto goto_enable_cache;
3605 #endif
3608 goto_next_file: // don't jump here after ao/vo/getch initialization!
3610 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3612 if(benchmark){
3613 double tot=video_time_usage+vout_time_usage+audio_time_usage;
3614 double total_time_usage;
3615 total_time_usage_start=GetTimer()-total_time_usage_start;
3616 total_time_usage = (float)total_time_usage_start*0.000001;
3617 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
3618 video_time_usage,vout_time_usage,audio_time_usage,
3619 total_time_usage-tot,total_time_usage);
3620 if(total_time_usage>0.0)
3621 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
3622 100.0*video_time_usage/total_time_usage,
3623 100.0*vout_time_usage/total_time_usage,
3624 100.0*audio_time_usage/total_time_usage,
3625 100.0*(total_time_usage-tot)/total_time_usage,
3626 100.0);
3627 if(total_frame_cnt && frame_dropping)
3628 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
3629 total_frame_cnt-drop_frame_cnt,
3630 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
3631 drop_frame_cnt,
3632 100*drop_frame_cnt/total_frame_cnt,
3633 total_frame_cnt,
3634 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
3638 // time to uninit all, except global stuff:
3639 uninit_player(INITED_ALL-(INITED_GUI+INITED_INPUT+(fixed_vo?INITED_VO:0)));
3641 if ( mpctx->set_of_sub_size > 0 )
3643 current_module="sub_free";
3644 for (i = 0; i < mpctx->set_of_sub_size; ++i) {
3645 sub_free( mpctx->set_of_subtitles[i] );
3646 #ifdef USE_ASS
3647 if ( mpctx->set_of_ass_tracks[i] )
3648 ass_free_track( mpctx->set_of_ass_tracks[i] );
3649 #endif
3651 mpctx->set_of_sub_size = 0;
3653 vo_sub_last = vo_sub=NULL;
3654 subdata=NULL;
3655 #ifdef USE_ASS
3656 ass_track = NULL;
3657 #endif
3659 if(mpctx->eof == PT_NEXT_ENTRY || mpctx->eof == PT_PREV_ENTRY) {
3660 mpctx->eof = mpctx->eof == PT_NEXT_ENTRY ? 1 : -1;
3661 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) == PLAY_TREE_ITER_ENTRY) {
3662 mpctx->eof = 1;
3663 } else {
3664 play_tree_iter_free(mpctx->playtree_iter);
3665 mpctx->playtree_iter = NULL;
3667 mpctx->play_tree_step = 1;
3668 } else if (mpctx->eof == PT_UP_NEXT || mpctx->eof == PT_UP_PREV) {
3669 mpctx->eof = mpctx->eof == PT_UP_NEXT ? 1 : -1;
3670 if ( mpctx->playtree_iter ) {
3671 if(play_tree_iter_up_step(mpctx->playtree_iter,mpctx->eof,0) == PLAY_TREE_ITER_ENTRY) {
3672 mpctx->eof = 1;
3673 } else {
3674 play_tree_iter_free(mpctx->playtree_iter);
3675 mpctx->playtree_iter = NULL;
3678 } else { // NEXT PREV SRC
3679 mpctx->eof = mpctx->eof == PT_PREV_SRC ? -1 : 1;
3682 if(mpctx->eof == 0) mpctx->eof = 1;
3684 while(mpctx->playtree_iter != NULL) {
3685 filename = play_tree_iter_get_file(mpctx->playtree_iter,mpctx->eof);
3686 if(filename == NULL) {
3687 if( play_tree_iter_step(mpctx->playtree_iter,mpctx->eof,0) != PLAY_TREE_ITER_ENTRY) {
3688 play_tree_iter_free(mpctx->playtree_iter);
3689 mpctx->playtree_iter = NULL;
3691 } else
3692 break;
3695 #ifdef HAVE_NEW_GUI
3696 if( use_gui && !mpctx->playtree_iter )
3698 #ifdef USE_DVDREAD
3699 if ( !guiIntfStruct.DiskChanged )
3700 #endif
3701 mplEnd();
3703 #endif
3705 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
3706 if (!mpctx->playtree_iter) filename = NULL;
3707 mpctx->eof = 0;
3708 goto play_next_file;
3712 exit_player_with_rc(MSGTR_Exit_eof, 0);
3714 return 1;