updated changelog with recent vidix api changes
[mplayer/glamo.git] / mplayer.c
blob0f14fe86b170e6915ba96d4923500617db9549e2
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 extern int mp_input_win32_slave_cmd_func(int fd,char* dest,int size);
28 #endif
30 #include <sys/time.h>
31 #include <sys/stat.h>
33 #include <signal.h>
34 #include <time.h>
35 #include <fcntl.h>
36 #include <limits.h>
38 #include <errno.h>
40 #include "version.h"
42 #include "mp_msg.h"
44 #define HELP_MP_DEFINE_STATIC
45 #include "help_mp.h"
47 #include "m_option.h"
48 #include "m_config.h"
49 #include "m_property.h"
51 #include "cfg-mplayer-def.h"
53 #include "subreader.h"
55 #include "libvo/video_out.h"
57 #include "libvo/font_load.h"
58 #include "libvo/sub.h"
60 #ifdef HAVE_X11
61 #include "libvo/x11_common.h"
62 #endif
64 #include "libao2/audio_out.h"
66 #include "codec-cfg.h"
68 #include "edl.h"
70 #include "spudec.h"
71 #include "vobsub.h"
73 #include "osdep/getch2.h"
74 #include "osdep/timer.h"
76 #include "cpudetect.h"
78 #ifdef HAVE_NEW_GUI
79 #include "Gui/interface.h"
80 #endif
82 #include "input/input.h"
84 int slave_mode=0;
85 int player_idle_mode=0;
86 int quiet=0;
87 int enable_mouse_movements=0;
89 #ifdef WIN32
90 char * proc_priority=NULL;
91 #endif
93 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
95 #ifdef HAVE_RTC
96 #ifdef __linux__
97 #include <linux/rtc.h>
98 #else
99 #include <rtc.h>
100 #define RTC_IRQP_SET RTCIO_IRQP_SET
101 #define RTC_PIE_ON RTCIO_PIE_ON
102 #endif /* __linux__ */
103 #endif /* HAVE_RTC */
105 #ifdef USE_TV
106 #include "stream/tv.h"
107 #endif
108 #ifdef USE_RADIO
109 #include "stream/stream_radio.h"
110 #endif
112 #ifdef HAS_DVBIN_SUPPORT
113 #include "stream/dvbin.h"
114 #include "stream/cache2.h"
115 #endif
117 //**************************************************************************//
118 // Playtree
119 //**************************************************************************//
120 #include "playtree.h"
121 #include "playtreeparser.h"
123 #ifdef HAVE_NEW_GUI
124 extern int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
125 extern int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
126 #endif
128 //**************************************************************************//
129 // Config
130 //**************************************************************************//
131 #include "parser-cfg.h"
132 #include "parser-mpcmd.h"
134 m_config_t* mconfig;
136 //**************************************************************************//
137 // Config file
138 //**************************************************************************//
140 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
142 static int cfg_include(m_option_t *conf, char *filename){
143 return m_config_parse_config_file(mconfig, filename);
146 #include "get_path.c"
148 //**************************************************************************//
149 // XScreensaver
150 //**************************************************************************//
152 #ifdef HAVE_X11
153 void xscreensaver_heartbeat(void);
154 #endif
156 //**************************************************************************//
157 //**************************************************************************//
158 // Input media streaming & demultiplexer:
159 //**************************************************************************//
161 static int max_framesize=0;
163 #include "stream/stream.h"
164 #include "libmpdemux/demuxer.h"
165 #include "libmpdemux/stheader.h"
166 //#include "parse_es.h"
167 #include "libmpdemux/matroska.h"
169 #ifdef USE_DVDREAD
170 #include "stream/stream_dvd.h"
171 #endif
173 #ifdef USE_DVDNAV
174 #include "stream/stream_dvdnav.h"
175 #endif
177 #include "libmpcodecs/dec_audio.h"
178 #include "libmpcodecs/dec_video.h"
179 #include "libmpcodecs/mp_image.h"
180 #include "libmpcodecs/vf.h"
181 #include "libmpcodecs/vd.h"
183 #include "mixer.h"
185 #include "mp_core.h"
187 //**************************************************************************//
188 //**************************************************************************//
190 // Common FIFO functions, and keyboard/event FIFO code
191 #include "mp_fifo.h"
192 int noconsolecontrols=0;
193 //**************************************************************************//
195 // Not all functions in mplayer.c take the context as an argument yet
196 static MPContext *mpctx = &(MPContext){
197 .osd_function = OSD_PLAY,
198 .begin_skip = MP_NOPTS_VALUE,
199 .play_tree_step = 1,
200 .global_sub_pos = -1,
201 .set_of_sub_pos = -1,
202 .file_format = DEMUXER_TYPE_UNKNOWN,
203 #ifdef HAS_DVBIN_SUPPORT
204 .last_dvb_step = 1,
205 #endif
208 int fixed_vo=0;
210 // benchmark:
211 double video_time_usage=0;
212 double vout_time_usage=0;
213 static double audio_time_usage=0;
214 static int total_time_usage_start=0;
215 static int total_frame_cnt=0;
216 static int drop_frame_cnt=0; // total number of dropped frames
217 int benchmark=0;
219 // options:
220 int auto_quality=0;
221 static int output_quality=0;
223 float playback_speed=1.0;
225 int use_gui=0;
227 #ifdef HAVE_NEW_GUI
228 int enqueue=0;
229 #endif
231 static int list_properties = 0;
233 int osd_level=1;
234 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
235 unsigned int osd_visible;
236 int osd_duration = 1000;
238 int term_osd = 1;
239 static char* term_osd_esc = "\x1b[A\r\x1b[K";
240 static char* playing_msg = NULL;
241 // seek:
242 static double seek_to_sec;
243 static off_t seek_to_byte=0;
244 static off_t step_sec=0;
245 static int loop_times=-1;
246 static int loop_seek=0;
248 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
250 // A/V sync:
251 int autosync=0; // 30 might be a good default value.
253 // may be changed by GUI: (FIXME!)
254 float rel_seek_secs=0;
255 int abs_seek_pos=0;
257 // codecs:
258 char **audio_codec_list=NULL; // override audio codec
259 char **video_codec_list=NULL; // override video codec
260 char **audio_fm_list=NULL; // override audio codec family
261 char **video_fm_list=NULL; // override video codec family
263 // demuxer:
264 extern char *demuxer_name; // override demuxer
265 extern char *audio_demuxer_name; // override audio demuxer
266 extern char *sub_demuxer_name; // override sub demuxer
268 // streaming:
269 int audio_id=-1;
270 int video_id=-1;
271 int dvdsub_id=-2;
272 int vobsub_id=-1;
273 char* audio_lang=NULL;
274 char* dvdsub_lang=NULL;
275 static char* spudec_ifo=NULL;
276 char* filename=NULL; //"MI2-Trailer.avi";
277 int forced_subs_only=0;
278 int file_filter=1;
280 // cache2:
281 int stream_cache_size=-1;
282 #ifdef USE_STREAM_CACHE
283 extern int cache_fill_status;
285 float stream_cache_min_percent=20.0;
286 float stream_cache_seek_min_percent=50.0;
287 #else
288 #define cache_fill_status 0
289 #endif
291 // dump:
292 static char *stream_dump_name="stream.dump";
293 int stream_dump_type=0;
295 // A-V sync:
296 static float default_max_pts_correction=-1;//0.01f;
297 static float max_pts_correction=0;//default_max_pts_correction;
298 static float c_total=0;
299 float audio_delay=0;
300 static int ignore_start=0;
302 static int softsleep=0;
304 float force_fps=0;
305 static int force_srate=0;
306 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
307 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
308 static int play_n_frames=-1;
309 static int play_n_frames_mf=-1;
311 // screen info:
312 char** video_driver_list=NULL;
313 char** audio_driver_list=NULL;
315 extern char *vo_subdevice;
316 extern char *ao_subdevice;
318 // codec outfmt flags (defined in libmpcodecs/vd.c)
319 extern int vo_flags;
321 // sub:
322 char *font_name=NULL;
323 #ifdef HAVE_FONTCONFIG
324 extern int font_fontconfig;
325 #endif
326 float font_factor=0.75;
327 char **sub_name=NULL;
328 float sub_delay=0;
329 float sub_fps=0;
330 int sub_auto = 1;
331 char *vobsub_name=NULL;
332 /*DSP!!char *dsp=NULL;*/
333 int subcc_enabled=0;
334 int suboverlap_enabled = 1;
336 #ifdef USE_ASS
337 #include "libass/ass.h"
338 #include "libass/ass_mp.h"
339 #endif
341 extern int mp_msg_levels[MSGT_MAX];
342 extern int mp_msg_level_all;
344 char* current_module=NULL; // for debugging
347 // ---
349 #ifdef HAVE_MENU
350 #include "m_struct.h"
351 #include "libmenu/menu.h"
352 extern void vf_menu_pause_update(struct vf_instance_s* vf);
353 extern vf_info_t vf_info_menu;
354 static vf_info_t* libmenu_vfs[] = {
355 &vf_info_menu,
356 NULL
358 static vf_instance_t* vf_menu = NULL;
359 static int use_menu = 0;
360 static char* menu_cfg = NULL;
361 static char* menu_root = "main";
362 #endif
365 #ifdef HAVE_RTC
366 static int nortc = 1;
367 static char* rtc_device;
368 #endif
370 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
371 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
372 short edl_decision = 0; ///< 1 when an EDL operation has been made.
373 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
374 int use_filedir_conf;
376 static unsigned int inited_flags=0;
377 #include "mpcommon.h"
378 #include "command.h"
380 #include "metadata.h"
382 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
384 void *mpctx_get_video_out(MPContext *mpctx)
386 return mpctx->video_out;
389 void *mpctx_get_audio_out(MPContext *mpctx)
391 return mpctx->audio_out;
394 void *mpctx_get_playtree_iter(MPContext *mpctx)
396 return mpctx->playtree_iter;
399 void *mpctx_get_mixer(MPContext *mpctx)
401 return &mpctx->mixer;
404 int mpctx_get_global_sub_size(MPContext *mpctx)
406 return mpctx->global_sub_size;
409 static int is_valid_metadata_type (metadata_t type) {
410 switch (type)
412 /* check for valid video stream */
413 case META_VIDEO_CODEC:
414 case META_VIDEO_BITRATE:
415 case META_VIDEO_RESOLUTION:
417 if (!mpctx->sh_video)
418 return 0;
419 break;
422 /* check for valid audio stream */
423 case META_AUDIO_CODEC:
424 case META_AUDIO_BITRATE:
425 case META_AUDIO_SAMPLES:
427 if (!mpctx->sh_audio)
428 return 0;
429 break;
432 /* check for valid demuxer */
433 case META_INFO_TITLE:
434 case META_INFO_ARTIST:
435 case META_INFO_ALBUM:
436 case META_INFO_YEAR:
437 case META_INFO_COMMENT:
438 case META_INFO_TRACK:
439 case META_INFO_GENRE:
441 if (!mpctx->demuxer)
442 return 0;
443 break;
446 default:
447 break;
450 return 1;
453 static char *get_demuxer_info (char *tag) {
454 char **info = mpctx->demuxer->info;
455 int n;
457 if (!info || !tag)
458 return NULL;
460 for (n = 0; info[2*n] != NULL ; n++)
461 if (!strcmp (info[2*n], tag))
462 break;
464 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
467 char *get_metadata (metadata_t type) {
468 char *meta = NULL;
469 sh_audio_t * const sh_audio = mpctx->sh_audio;
470 sh_video_t * const sh_video = mpctx->sh_video;
472 if (!is_valid_metadata_type (type))
473 return NULL;
475 switch (type)
477 case META_NAME:
479 return strdup (mp_basename2 (filename));
482 case META_VIDEO_CODEC:
484 if (sh_video->format == 0x10000001)
485 meta = strdup ("mpeg1");
486 else if (sh_video->format == 0x10000002)
487 meta = strdup ("mpeg2");
488 else if (sh_video->format == 0x10000004)
489 meta = strdup ("mpeg4");
490 else if (sh_video->format == 0x10000005)
491 meta = strdup ("h264");
492 else if (sh_video->format >= 0x20202020)
494 meta = (char *) malloc (8);
495 sprintf (meta, "%.4s", (char *) &sh_video->format);
497 else
499 meta = (char *) malloc (8);
500 sprintf (meta, "0x%08X", sh_video->format);
502 return meta;
505 case META_VIDEO_BITRATE:
507 meta = (char *) malloc (16);
508 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
509 return meta;
512 case META_VIDEO_RESOLUTION:
514 meta = (char *) malloc (16);
515 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
516 return meta;
519 case META_AUDIO_CODEC:
521 if (sh_audio->codec && sh_audio->codec->name)
522 meta = strdup (sh_audio->codec->name);
523 return meta;
526 case META_AUDIO_BITRATE:
528 meta = (char *) malloc (16);
529 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
530 return meta;
533 case META_AUDIO_SAMPLES:
535 meta = (char *) malloc (16);
536 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
537 return meta;
540 /* check for valid demuxer */
541 case META_INFO_TITLE:
542 return get_demuxer_info ("Title");
544 case META_INFO_ARTIST:
545 return get_demuxer_info ("Artist");
547 case META_INFO_ALBUM:
548 return get_demuxer_info ("Album");
550 case META_INFO_YEAR:
551 return get_demuxer_info ("Year");
553 case META_INFO_COMMENT:
554 return get_demuxer_info ("Comment");
556 case META_INFO_TRACK:
557 return get_demuxer_info ("Track");
559 case META_INFO_GENRE:
560 return get_demuxer_info ("Genre");
562 default:
563 break;
566 return meta;
569 /// step size of mixer changes
570 int volstep = 3;
572 void uninit_player(unsigned int mask){
573 mask=inited_flags&mask;
575 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
577 if(mask&INITED_ACODEC){
578 inited_flags&=~INITED_ACODEC;
579 current_module="uninit_acodec";
580 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
581 #ifdef HAVE_NEW_GUI
582 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
583 #endif
584 mpctx->sh_audio=NULL;
587 if(mask&INITED_VCODEC){
588 inited_flags&=~INITED_VCODEC;
589 current_module="uninit_vcodec";
590 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
591 mpctx->sh_video=NULL;
592 #ifdef HAVE_MENU
593 vf_menu=NULL;
594 #endif
597 if(mask&INITED_DEMUXER){
598 inited_flags&=~INITED_DEMUXER;
599 current_module="free_demuxer";
600 if(mpctx->demuxer){
601 mpctx->stream=mpctx->demuxer->stream;
602 free_demuxer(mpctx->demuxer);
604 mpctx->demuxer=NULL;
607 // kill the cache process:
608 if(mask&INITED_STREAM){
609 inited_flags&=~INITED_STREAM;
610 current_module="uninit_stream";
611 if(mpctx->stream) free_stream(mpctx->stream);
612 mpctx->stream=NULL;
615 if(mask&INITED_VO){
616 inited_flags&=~INITED_VO;
617 current_module="uninit_vo";
618 mpctx->video_out->uninit();
619 mpctx->video_out=NULL;
622 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
623 if(mask&INITED_GETCH2){
624 inited_flags&=~INITED_GETCH2;
625 current_module="uninit_getch2";
626 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
627 // restore terminal:
628 getch2_disable();
631 if(mask&INITED_VOBSUB){
632 inited_flags&=~INITED_VOBSUB;
633 current_module="uninit_vobsub";
634 if(vo_vobsub) vobsub_close(vo_vobsub);
635 vo_vobsub=NULL;
638 if (mask&INITED_SPUDEC){
639 inited_flags&=~INITED_SPUDEC;
640 current_module="uninit_spudec";
641 spudec_free(vo_spudec);
642 vo_spudec=NULL;
645 if(mask&INITED_AO){
646 inited_flags&=~INITED_AO;
647 current_module="uninit_ao";
648 if (mpctx->user_muted | mpctx->edl_muted) mixer_mute(&mpctx->mixer);
649 mpctx->audio_out->uninit(mpctx->eof?0:1); mpctx->audio_out=NULL;
652 #ifdef HAVE_NEW_GUI
653 if(mask&INITED_GUI){
654 inited_flags&=~INITED_GUI;
655 current_module="uninit_gui";
656 guiDone();
658 #endif
660 if(mask&INITED_INPUT){
661 inited_flags&=~INITED_INPUT;
662 current_module="uninit_input";
663 mp_input_uninit();
666 current_module=NULL;
669 void exit_player_with_rc(const char* how, int rc){
671 uninit_player(INITED_ALL);
672 #ifdef HAVE_X11
673 #ifdef HAVE_NEW_GUI
674 if ( !use_gui )
675 #endif
676 vo_uninit(); // Close the X11 connection (if any is open).
677 #endif
679 #ifdef HAVE_FREETYPE
680 current_module="uninit_font";
681 if (vo_font) free_font_desc(vo_font);
682 vo_font = NULL;
683 done_freetype();
684 #endif
685 free_osd_list();
687 #ifdef USE_ASS
688 ass_library_done(ass_library);
689 #endif
691 current_module="exit_player";
693 // free mplayer config
694 if(mconfig)
695 m_config_free(mconfig);
697 if(mpctx->playtree)
698 play_tree_free(mpctx->playtree, 1);
701 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
702 if(how) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,how);
703 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
705 exit(rc);
708 void exit_player(const char* how){
709 exit_player_with_rc(how, 1);
712 #ifndef __MINGW32__
713 static void child_sighandler(int x){
714 pid_t pid;
715 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
717 #endif
719 #ifdef CRASH_DEBUG
720 static char *prog_path;
721 static int crash_debug = 0;
722 #endif
724 static void exit_sighandler(int x){
725 static int sig_count=0;
726 #ifdef CRASH_DEBUG
727 if (!crash_debug || x != SIGTRAP)
728 #endif
729 ++sig_count;
730 if(inited_flags==0 && sig_count>1) exit(1);
731 if(sig_count==5)
733 /* We're crashing bad and can't uninit cleanly :(
734 * by popular request, we make one last (dirty)
735 * effort to restore the user's
736 * terminal. */
737 getch2_disable();
738 exit(1);
740 if(sig_count==6) exit(1);
741 if(sig_count>6){
742 // can't stop :(
743 #ifndef __MINGW32__
744 kill(getpid(),SIGKILL);
745 #endif
747 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
748 current_module?current_module:"unknown"
750 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
751 if(sig_count<=1)
752 switch(x){
753 case SIGINT:
754 case SIGQUIT:
755 case SIGTERM:
756 case SIGKILL:
757 break; // killed from keyboard (^C) or killed [-9]
758 case SIGILL:
759 #ifdef RUNTIME_CPUDETECT
760 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
761 #else
762 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
763 #endif
764 case SIGFPE:
765 case SIGSEGV:
766 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
767 default:
768 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
769 #ifdef CRASH_DEBUG
770 if (crash_debug) {
771 int gdb_pid;
772 char spid[20];
773 snprintf(spid, 19, "%i", getpid());
774 spid[19] = 0;
775 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
776 gdb_pid = fork();
777 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
778 if (gdb_pid == 0) { // We are the child
779 if (execlp("gdb", "gdb", prog_path, spid, NULL) == -1)
780 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
781 } else if (gdb_pid < 0)
782 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
783 else {
784 waitpid(gdb_pid, NULL, 0);
786 if (x == SIGTRAP) return;
788 #endif
790 exit_player(NULL);
793 extern void mp_input_register_options(m_config_t* cfg);
795 #include "cfg-mplayer.h"
797 static void parse_cfgfiles( m_config_t* conf )
799 char *conffile;
800 int conffile_fd;
801 if (m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
802 exit_player(NULL);
803 if ((conffile = get_path("")) == NULL) {
804 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
805 } else {
806 #ifdef __MINGW32__
807 mkdir(conffile);
808 #else
809 mkdir(conffile, 0777);
810 #endif
811 free(conffile);
812 if ((conffile = get_path("config")) == NULL) {
813 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
814 } else {
815 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
816 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
817 write(conffile_fd, default_config, strlen(default_config));
818 close(conffile_fd);
820 if (m_config_parse_config_file(conf, conffile) < 0)
821 exit_player(NULL);
822 free(conffile);
827 void load_per_file_config (m_config_t* conf, const char *const file)
829 char *confpath;
830 char cfg[strlen(file)+10];
831 struct stat st;
832 char *name;
834 sprintf (cfg, "%s.conf", file);
836 if (use_filedir_conf && !stat (cfg, &st))
838 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
839 m_config_parse_config_file (conf, cfg);
840 return;
843 if ((name = strrchr (cfg, '/')) == NULL)
844 name = cfg;
845 else
846 name++;
848 if ((confpath = get_path (name)) != NULL)
850 if (!stat (confpath, &st))
852 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
853 m_config_parse_config_file (conf, confpath);
856 free (confpath);
860 /* When libmpdemux performs a blocking operation (network connection or
861 * cache filling) if the operation fails we use this function to check
862 * if it was interrupted by the user.
863 * The function returns a new value for eof. */
864 static int libmpdemux_was_interrupted(int eof) {
865 mp_cmd_t* cmd;
866 if((cmd = mp_input_get_cmd(0,0,0)) != NULL) {
867 switch(cmd->id) {
868 case MP_CMD_QUIT:
869 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
870 case MP_CMD_PLAY_TREE_STEP: {
871 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
872 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
873 } break;
874 case MP_CMD_PLAY_TREE_UP_STEP: {
875 eof = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
876 } break;
877 case MP_CMD_PLAY_ALT_SRC_STEP: {
878 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
879 } break;
881 mp_cmd_free(cmd);
883 return eof;
886 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
888 int playtree_add_playlist(play_tree_t* entry)
890 play_tree_add_bpf(entry,filename);
892 #ifdef HAVE_NEW_GUI
893 if (use_gui) {
894 if (entry) {
895 import_playtree_playlist_into_gui(entry, mconfig);
896 play_tree_free_list(entry,1);
898 } else
899 #endif
901 if(!entry) {
902 entry = mpctx->playtree_iter->tree;
903 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
904 return PT_NEXT_ENTRY;
906 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
907 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
908 return PT_NEXT_ENTRY;
911 play_tree_remove(entry,1,1);
912 return PT_NEXT_SRC;
914 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
915 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
916 entry = mpctx->playtree_iter->tree;
917 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
918 return PT_NEXT_ENTRY;
920 play_tree_remove(entry,1,1);
922 return PT_NEXT_SRC;
925 void add_subtitles(char *filename, float fps, int silent)
927 sub_data *subd;
928 #ifdef USE_ASS
929 ass_track_t *asst = 0;
930 #endif
932 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
933 return;
936 subd = sub_read_file(filename, fps);
937 #ifdef USE_ASS
938 if (ass_enabled)
939 #ifdef USE_ICONV
940 asst = ass_read_file(ass_library, filename, sub_cp);
941 #else
942 asst = ass_read_file(ass_library, filename, 0);
943 #endif
944 if (ass_enabled && subd && !asst)
945 asst = ass_read_subdata(ass_library, subd, fps);
947 if (!asst && !subd && !silent)
948 #else
949 if(!subd && !silent)
950 #endif
951 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_CantLoadSub,
952 filename_recode(filename));
954 #ifdef USE_ASS
955 if (!asst && !subd) return;
956 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
957 #else
958 if (!subd) return;
959 #endif
960 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
961 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
962 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
963 filename_recode(filename));
964 ++mpctx->set_of_sub_size;
965 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
966 filename_recode(filename));
969 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
970 void update_set_of_subtitles(void)
971 // subdata was changed, set_of_sub... have to be updated.
973 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
974 int i;
975 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
976 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
977 set_of_subtitles[i-1] = set_of_subtitles[i];
978 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
979 --mpctx->set_of_sub_size;
980 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
982 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
983 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
985 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
986 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
987 ++mpctx->set_of_sub_size;
991 void init_vo_spudec(void) {
992 if (vo_spudec)
993 spudec_free(vo_spudec);
994 inited_flags &= ~INITED_SPUDEC;
995 vo_spudec = NULL;
996 if (spudec_ifo) {
997 unsigned int palette[16], width, height;
998 current_module="spudec_init_vobsub";
999 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1000 vo_spudec=spudec_new_scaled(palette, width, height);
1003 #ifdef USE_DVDREAD
1004 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1005 current_module="spudec_init_dvdread";
1006 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1007 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1009 #endif
1011 #ifdef USE_DVDNAV
1012 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1013 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1014 current_module="spudec_init_dvdnav";
1015 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1017 #endif
1019 if ((vo_spudec == NULL) && (mpctx->demuxer->type == DEMUXER_TYPE_MATROSKA) &&
1020 (mpctx->d_sub->sh != NULL) && (((sh_sub_t *)mpctx->d_sub->sh)->type == 'v')) {
1021 sh_sub_t *mkv_sh_sub = (sh_sub_t *)mpctx->d_sub->sh;
1022 current_module = "spudec_init_matroska";
1023 vo_spudec =
1024 spudec_new_scaled_vobsub(mkv_sh_sub->palette, mkv_sh_sub->colors,
1025 mkv_sh_sub->custom_colors, mkv_sh_sub->width,
1026 mkv_sh_sub->height);
1027 forced_subs_only = mkv_sh_sub->forced_subs_only;
1030 if (vo_spudec==NULL) {
1031 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1032 unsigned int *palette = NULL;
1033 if (sh && sh->has_palette)
1034 palette = sh->palette;
1035 current_module="spudec_init_normal";
1036 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1037 spudec_set_font_factor(vo_spudec,font_factor);
1040 if (vo_spudec!=NULL)
1041 inited_flags|=INITED_SPUDEC;
1045 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1046 * make it all work is to use the builtin SDL-bootstrap code, which
1047 * will be done automatically by replacing our main() if we include SDL.h.
1049 #if defined(SYS_DARWIN) && defined(HAVE_SDL)
1050 #include <SDL.h>
1051 #endif
1054 * \brief append a formatted string
1055 * \param buf buffer to print into
1056 * \param pos position of terminating 0 in buf
1057 * \param len maximum number of characters in buf, not including terminating 0
1058 * \param format printf format string
1060 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1062 va_list va;
1063 va_start(va, format);
1064 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1065 va_end(va);
1066 if (*pos >= len ) {
1067 buf[len] = 0;
1068 *pos = len;
1073 * \brief append time in the hh:mm:ss.f format
1074 * \param buf buffer to print into
1075 * \param pos position of terminating 0 in buf
1076 * \param len maximum number of characters in buf, not including terminating 0
1077 * \param time time value to convert/append
1079 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1080 long tenths = 10 * time;
1081 int f1 = tenths % 10;
1082 int ss = (tenths / 10) % 60;
1083 int mm = (tenths / 600) % 60;
1084 int hh = tenths / 36000;
1085 if (time <= 0) {
1086 saddf(buf, pos, len, "unknown");
1087 return;
1089 if (hh > 0)
1090 saddf(buf, pos, len, "%2d:", hh);
1091 if (hh > 0 || mm > 0)
1092 saddf(buf, pos, len, "%02d:", mm);
1093 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1097 * \brief print the status line
1098 * \param a_pos audio position
1099 * \param a_v A-V desynchronization
1100 * \param corr amount out A-V synchronization
1102 static void print_status(float a_pos, float a_v, float corr)
1104 sh_video_t * const sh_video = mpctx->sh_video;
1105 int width;
1106 char *line;
1107 unsigned pos = 0;
1108 get_screen_size();
1109 if (screen_width > 0)
1110 width = screen_width;
1111 else
1112 width = 80;
1113 #ifdef WIN32
1114 /* Windows command line is broken (MinGW's rxvt works, but we
1115 * should not depend on that). */
1116 width--;
1117 #endif
1118 line = malloc(width + 1); // one additional char for the terminating null
1120 // Audio time
1121 if (mpctx->sh_audio) {
1122 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1123 if (!sh_video) {
1124 float len = demuxer_get_time_length(mpctx->demuxer);
1125 saddf(line, &pos, width, "(");
1126 sadd_hhmmssf(line, &pos, width, a_pos);
1127 saddf(line, &pos, width, ") of %.1f (", len);
1128 sadd_hhmmssf(line, &pos, width, len);
1129 saddf(line, &pos, width, ") ");
1133 // Video time
1134 if (sh_video)
1135 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1137 // A-V sync
1138 if (mpctx->sh_audio && sh_video)
1139 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
1141 // Video stats
1142 if (sh_video)
1143 saddf(line, &pos, width, "%3d/%3d ",
1144 (int)sh_video->num_frames,
1145 (int)sh_video->num_frames_decoded);
1147 // CPU usage
1148 if (sh_video) {
1149 if (sh_video->timer > 0.5)
1150 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1151 (int)(100.0*video_time_usage*playback_speed/(double)sh_video->timer),
1152 (int)(100.0*vout_time_usage*playback_speed/(double)sh_video->timer),
1153 (100.0*audio_time_usage*playback_speed/(double)sh_video->timer));
1154 else
1155 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1156 } else if (mpctx->sh_audio) {
1157 if (mpctx->delay > 0.5)
1158 saddf(line, &pos, width, "%4.1f%% ",
1159 100.0*audio_time_usage/(double)mpctx->delay);
1160 else
1161 saddf(line, &pos, width, "??,?%% ");
1164 // VO stats
1165 if (sh_video)
1166 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1168 #ifdef USE_STREAM_CACHE
1169 // cache stats
1170 if (stream_cache_size > 0)
1171 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1172 #endif
1174 // other
1175 if (playback_speed != 1)
1176 saddf(line, &pos, width, "%4.2fx ", playback_speed);
1178 // end
1179 if (erase_to_end_of_line) {
1180 line[pos] = 0;
1181 mp_msg(MSGT_AVSYNC, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1182 } else {
1183 memset(&line[pos], ' ', width - pos);
1184 line[width] = 0;
1185 mp_msg(MSGT_AVSYNC, MSGL_STATUS, "%s\r", line);
1187 free(line);
1191 * \brief build a chain of audio filters that converts the input format
1192 * to the ao's format, taking into account the current playback_speed.
1193 * \param sh_audio describes the requested input format of the chain.
1194 * \param ao_data describes the requested output format of the chain.
1196 int build_afilter_chain(sh_audio_t *sh_audio, ao_data_t *ao_data)
1198 int new_srate;
1199 int result;
1200 if (!sh_audio)
1202 #ifdef HAVE_NEW_GUI
1203 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1204 #endif
1205 mpctx->mixer.afilter = NULL;
1206 return 0;
1208 new_srate = sh_audio->samplerate * playback_speed;
1209 if (new_srate != ao_data->samplerate) {
1210 // limits are taken from libaf/af_resample.c
1211 if (new_srate < 8000)
1212 new_srate = 8000;
1213 if (new_srate > 192000)
1214 new_srate = 192000;
1215 playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1217 result = init_audio_filters(sh_audio, new_srate,
1218 sh_audio->channels, sh_audio->sample_format,
1219 &ao_data->samplerate, &ao_data->channels, &ao_data->format,
1220 ao_data->outburst * 4, ao_data->buffersize);
1221 mpctx->mixer.afilter = sh_audio->afilter;
1222 #ifdef HAVE_NEW_GUI
1223 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1224 #endif
1225 return result;
1229 typedef struct mp_osd_msg mp_osd_msg_t;
1230 struct mp_osd_msg {
1231 /// Previous message on the stack.
1232 mp_osd_msg_t* prev;
1233 /// Message text.
1234 char msg[64];
1235 int id,level,started;
1236 /// Display duration in ms.
1237 unsigned time;
1240 /// OSD message stack.
1241 static mp_osd_msg_t* osd_msg_stack = NULL;
1244 * \brief Add a message on the OSD message stack
1246 * If a message with the same id is already present in the stack
1247 * it is pulled on top of the stack, otherwise a new message is created.
1251 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1252 mp_osd_msg_t *msg,*last=NULL;
1253 va_list va;
1254 int r;
1256 // look if the id is already in the stack
1257 for(msg = osd_msg_stack ; msg && msg->id != id ;
1258 last = msg, msg = msg->prev);
1259 // not found: alloc it
1260 if(!msg) {
1261 msg = calloc(1,sizeof(mp_osd_msg_t));
1262 msg->prev = osd_msg_stack;
1263 osd_msg_stack = msg;
1264 } else if(last) { // found, but it's not on top of the stack
1265 last->prev = msg->prev;
1266 msg->prev = osd_msg_stack;
1267 osd_msg_stack = msg;
1269 // write the msg
1270 va_start(va,fmt);
1271 r = vsnprintf(msg->msg, 64, fmt, va);
1272 va_end(va);
1273 if(r >= 64) msg->msg[63] = 0;
1274 // set id and time
1275 msg->id = id;
1276 msg->level = level;
1277 msg->time = time;
1282 * \brief Remove a message from the OSD stack
1284 * This function can be used to get rid of a message right away.
1288 void rm_osd_msg(int id) {
1289 mp_osd_msg_t *msg,*last=NULL;
1291 // Search for the msg
1292 for(msg = osd_msg_stack ; msg && msg->id != id ;
1293 last = msg, msg = msg->prev);
1294 if(!msg) return;
1296 // Detach it from the stack and free it
1297 if(last)
1298 last->prev = msg->prev;
1299 else
1300 osd_msg_stack = msg->prev;
1301 free(msg);
1305 * \brief Remove all messages from the OSD stack
1309 static void clear_osd_msgs(void) {
1310 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1311 while(msg) {
1312 prev = msg->prev;
1313 free(msg);
1314 msg = prev;
1316 osd_msg_stack = NULL;
1320 * \brief Get the current message from the OSD stack.
1322 * This function decrements the message timer and destroys the old ones.
1323 * The message that should be displayed is returned (if any).
1327 static mp_osd_msg_t* get_osd_msg(void) {
1328 mp_osd_msg_t *msg,*prev,*last = NULL;
1329 static unsigned last_update = 0;
1330 unsigned now = GetTimerMS();
1331 unsigned diff;
1332 char hidden_dec_done = 0;
1334 if(!last_update) last_update = now;
1335 diff = now >= last_update ? now - last_update : 0;
1337 last_update = now;
1339 // Look for the first message in the stack with high enough level.
1340 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1341 prev = msg->prev;
1342 if(msg->level > osd_level && hidden_dec_done) continue;
1343 // The message has a high enough level or it is the first hidden one
1344 // in both cases we decrement the timer or kill it.
1345 if(!msg->started || msg->time > diff) {
1346 if(msg->started) msg->time -= diff;
1347 else msg->started = 1;
1348 // display it
1349 if(msg->level <= osd_level) return msg;
1350 hidden_dec_done = 1;
1351 continue;
1353 // kill the message
1354 free(msg);
1355 if(last) {
1356 last->prev = prev;
1357 msg = last;
1358 } else {
1359 osd_msg_stack = prev;
1360 msg = NULL;
1363 // Nothing found
1364 return NULL;
1368 * \brief Display the OSD bar.
1370 * Display the OSD bar or fall back on a simple message.
1374 void set_osd_bar(int type,const char* name,double min,double max,double val) {
1376 if(osd_level < 1) return;
1378 if(mpctx->sh_video) {
1379 osd_visible = (GetTimerMS() + 1000) | 1;
1380 vo_osd_progbar_type = type;
1381 vo_osd_progbar_value = 256*(val-min)/(max-min);
1382 vo_osd_changed(OSDTYPE_PROGBAR);
1383 return;
1386 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1387 name,ROUND(100*(val-min)/(max-min)));
1392 * \brief Update the OSD message line.
1394 * This function displays the current message on the vo OSD or on the term.
1395 * If the stack is empty and the OSD level is high enough the timer
1396 * is displayed (only on the vo OSD).
1400 static void update_osd_msg(void) {
1401 mp_osd_msg_t *msg;
1402 static char osd_text[64] = "";
1403 static char osd_text_timer[64];
1405 // we need some mem for vo_osd_text
1406 vo_osd_text = (unsigned char*)osd_text;
1408 // Look if we have a msg
1409 if((msg = get_osd_msg())) {
1410 if(strcmp(osd_text,msg->msg)) {
1411 strncpy((char*)osd_text, msg->msg, 63);
1412 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1413 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1415 return;
1418 if(mpctx->sh_video) {
1419 // fallback on the timer
1420 if(osd_level>=2) {
1421 int len = demuxer_get_time_length(mpctx->demuxer);
1422 int percentage = -1;
1423 char percentage_text[10];
1424 int pts = demuxer_get_current_time(mpctx->demuxer);
1426 if (mpctx->osd_show_percentage)
1427 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1429 if (percentage >= 0)
1430 snprintf(percentage_text, 9, " (%d%%)", percentage);
1431 else
1432 percentage_text[0] = 0;
1434 if (osd_level == 3)
1435 snprintf(osd_text_timer, 63,
1436 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1437 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1438 len/3600,(len/60)%60,len%60,percentage_text);
1439 else
1440 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1441 mpctx->osd_function,pts/3600,(pts/60)%60,
1442 pts%60,percentage_text);
1443 } else
1444 osd_text_timer[0]=0;
1446 // always decrement the percentage timer
1447 if(mpctx->osd_show_percentage)
1448 mpctx->osd_show_percentage--;
1450 if(strcmp(osd_text,osd_text_timer)) {
1451 strncpy(osd_text, osd_text_timer, 63);
1452 vo_osd_changed(OSDTYPE_OSD);
1454 return;
1457 // Clear the term osd line
1458 if(term_osd && osd_text[0]) {
1459 osd_text[0] = 0;
1460 printf("%s\n",term_osd_esc);
1464 ///@}
1465 // OSDMsgStack
1468 void reinit_audio_chain(void) {
1469 if(mpctx->sh_audio){
1470 current_module="init_audio_codec";
1471 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1472 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1473 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1474 mpctx->d_audio->id = -2;
1475 return;
1476 } else
1477 inited_flags|=INITED_ACODEC;
1478 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1481 //const ao_info_t *info=audio_out->info;
1482 current_module="af_preinit";
1483 ao_data.samplerate=force_srate;
1484 ao_data.channels=0;
1485 ao_data.format=audio_output_format;
1486 #if 1
1487 // first init to detect best values
1488 if(!preinit_audio_filters(mpctx->sh_audio,
1489 // input:
1490 (int)(mpctx->sh_audio->samplerate*playback_speed),
1491 mpctx->sh_audio->channels, mpctx->sh_audio->sample_format,
1492 // output:
1493 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1494 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1495 exit_player(MSGTR_Exit_error);
1497 #endif
1498 current_module="ao2_init";
1499 if(!(mpctx->audio_out=init_best_audio_out(audio_driver_list,
1500 0, // plugin flag
1501 ao_data.samplerate,
1502 ao_data.channels,
1503 ao_data.format,0))){
1504 // FAILED:
1505 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1506 uninit_player(INITED_ACODEC); // close codec
1507 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1508 mpctx->d_audio->id = -2;
1509 return;
1510 } else {
1511 // SUCCESS:
1512 inited_flags|=INITED_AO;
1513 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1514 mpctx->audio_out->info->short_name,
1515 ao_data.samplerate, ao_data.channels,
1516 af_fmt2str_short(ao_data.format),
1517 af_fmt2bits(ao_data.format)/8 );
1518 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1519 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1520 if(strlen(mpctx->audio_out->info->comment) > 0)
1521 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1522 // init audio filters:
1523 #if 1
1524 current_module="af_init";
1525 if(!build_afilter_chain(mpctx->sh_audio, &ao_data)) {
1526 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1527 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1528 // uninit_player(INITED_ACODEC|INITED_AO); // close codec & ao
1529 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1531 #endif
1533 mpctx->mixer.audio_out = mpctx->audio_out;
1534 mpctx->mixer.volstep = volstep;
1539 ///@}
1540 // Command2Property
1543 // Return pts value corresponding to the end point of audio written to the
1544 // ao so far.
1545 static double written_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio)
1547 // first calculate the end pts of audio that has been output by decoder
1548 double a_pts = sh_audio->pts;
1549 if (a_pts != MP_NOPTS_VALUE)
1550 // Good, decoder supports new way of calculating audio pts.
1551 // sh_audio->pts is the timestamp of the latest input packet with
1552 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1553 // the amount of bytes the decoder has written after that timestamp.
1554 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1555 else {
1556 // Decoder doesn't support new way of calculating pts (or we're
1557 // being called before it has decoded anything with known timestamp).
1558 // Use the old method of audio pts calculation: take the timestamp
1559 // of last packet with known pts the decoder has read data from,
1560 // and add amount of bytes read after the beginning of that packet
1561 // divided by input bps. This will be inaccurate if the input/output
1562 // ratio is not constant for every audio packet or if it is constant
1563 // but not accurately known in sh_audio->i_bps.
1565 a_pts = d_audio->pts;
1566 // ds_tell_pts returns bytes read after last timestamp from
1567 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1568 // it has read but not decoded
1569 if (sh_audio->i_bps)
1570 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1571 (double)sh_audio->i_bps;
1573 // Now a_pts hopefully holds the pts for end of audio from decoder.
1574 // Substract data in buffers between decoder and audio out.
1576 // Decoded but not filtered
1577 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1579 // Data that was ready for ao but was buffered because ao didn't fully
1580 // accept everything to internal buffers yet
1581 a_pts -= sh_audio->a_out_buffer_len * playback_speed / (double)ao_data.bps;
1583 return a_pts;
1586 // Return pts value corresponding to currently playing audio.
1587 double playing_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio,
1588 ao_functions_t *audio_out)
1590 return written_audio_pts(sh_audio, d_audio) - playback_speed *
1591 audio_out->get_delay();
1594 static int generate_video_frame(sh_video_t *sh_video, demux_stream_t *d_video)
1596 unsigned char *start;
1597 int in_size;
1598 int hit_eof=0;
1599 double pts;
1601 while (1) {
1602 void *decoded_frame;
1603 current_module = "decode video";
1604 // XXX Time used in this call is not counted in any performance
1605 // timer now, OSD is not updated correctly for filter-added frames
1606 if (vf_output_queued_frame(sh_video->vfilter))
1607 break;
1608 current_module = "video_read_frame";
1609 in_size = ds_get_packet_pts(d_video, &start, &pts);
1610 if (in_size < 0) {
1611 // try to extract last frames in case of decoder lag
1612 in_size = 0;
1613 pts = 1e300;
1614 hit_eof = 1;
1616 if (in_size > max_framesize)
1617 max_framesize = in_size;
1618 current_module = "decode video";
1619 decoded_frame = decode_video(sh_video, start, in_size, 0, pts);
1620 if (decoded_frame) {
1621 update_subtitles(sh_video, mpctx->d_sub, 0);
1622 update_osd_msg();
1623 current_module = "filter video";
1624 if (filter_video(sh_video, decoded_frame, sh_video->pts))
1625 break;
1627 if (hit_eof)
1628 return 0;
1630 return 1;
1633 #ifdef HAVE_RTC
1634 int rtc_fd = -1;
1635 #endif
1637 static float timing_sleep(float time_frame)
1639 #ifdef HAVE_RTC
1640 if (rtc_fd >= 0){
1641 // -------- RTC -----------
1642 current_module="sleep_rtc";
1643 while (time_frame > 0.000) {
1644 unsigned long rtc_ts;
1645 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1646 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1647 time_frame -= GetRelativeTime();
1649 } else
1650 #endif
1651 #ifdef SYS_DARWIN
1653 current_module = "sleep_darwin";
1654 while (time_frame > 0.005) {
1655 usec_sleep(1000000*time_frame);
1656 time_frame -= GetRelativeTime();
1659 #else
1661 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1662 // unnecessarily high CPU usage
1663 float margin = softsleep ? 0.011 : 0;
1664 current_module = "sleep_timer";
1665 while (time_frame > margin) {
1666 usec_sleep(1000000 * (time_frame - margin));
1667 time_frame -= GetRelativeTime();
1669 if (softsleep){
1670 current_module = "sleep_soft";
1671 if (time_frame < 0)
1672 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1673 while (time_frame > 0)
1674 time_frame-=GetRelativeTime(); // burn the CPU
1677 #endif /* SYS_DARWIN */
1678 return time_frame;
1681 static void adjust_sync_and_print_status(int between_frames, float timing_error)
1683 current_module="av_sync";
1685 if(mpctx->sh_audio){
1686 double a_pts, v_pts;
1688 if (autosync)
1690 * If autosync is enabled, the value for delay must be calculated
1691 * a bit differently. It is set only to the difference between
1692 * the audio and video timers. Any attempt to include the real
1693 * or corrected delay causes the pts_correction code below to
1694 * try to correct for the changes in delay which autosync is
1695 * trying to measure. This keeps the two from competing, but still
1696 * allows the code to correct for PTS drift *only*. (Using a delay
1697 * value here, even a "corrected" one, would be incompatible with
1698 * autosync mode.)
1700 a_pts = written_audio_pts(mpctx->sh_audio, mpctx->d_audio) - mpctx->delay;
1701 else
1702 a_pts = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
1704 v_pts = mpctx->sh_video->pts;
1707 static int drop_message=0;
1708 double AV_delay = a_pts - audio_delay - v_pts;
1709 double x;
1710 if (AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
1711 ++drop_message;
1712 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1714 if (autosync)
1715 x = AV_delay*0.1f;
1716 else
1717 /* Do not correct target time for the next frame if this frame
1718 * was late not because of wrong target time but because the
1719 * target time could not be met */
1720 x = (AV_delay + timing_error * playback_speed) * 0.1f;
1721 if (x < -max_pts_correction)
1722 x = -max_pts_correction;
1723 else if (x> max_pts_correction)
1724 x = max_pts_correction;
1725 if (default_max_pts_correction >= 0)
1726 max_pts_correction = default_max_pts_correction;
1727 else
1728 max_pts_correction = mpctx->sh_video->frametime*0.10; // +-10% of time
1729 if (!between_frames) {
1730 mpctx->delay+=x;
1731 c_total+=x;
1733 if(!quiet)
1734 print_status(a_pts - audio_delay, AV_delay, c_total);
1737 } else {
1738 // No audio:
1740 if (!quiet)
1741 print_status(0, 0, 0);
1745 static int fill_audio_out_buffers(void)
1747 unsigned int t;
1748 double tt;
1749 int playsize;
1750 int playflags=0;
1751 int audio_eof=0;
1752 int bytes_to_write;
1753 sh_audio_t * const sh_audio = mpctx->sh_audio;
1755 current_module="play_audio";
1757 while (1) {
1758 // all the current uses of ao_data.pts seem to be in aos that handle
1759 // sync completely wrong; there should be no need to use ao_data.pts
1760 // in get_space()
1761 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1762 bytes_to_write = mpctx->audio_out->get_space();
1763 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
1764 break;
1766 // handle audio-only case:
1767 // this is where mplayer sleeps during audio-only playback
1768 // to avoid 100% CPU use
1769 usec_sleep(10000); // Wait a tick before retry
1772 while (bytes_to_write) {
1773 playsize = bytes_to_write;
1774 if (playsize > MAX_OUTBURST)
1775 playsize = MAX_OUTBURST;
1776 bytes_to_write -= playsize;
1778 // Fill buffer if needed:
1779 current_module="decode_audio";
1780 t = GetTimer();
1781 while (sh_audio->a_out_buffer_len < playsize) {
1782 int buflen = sh_audio->a_out_buffer_len;
1783 int ret = decode_audio(sh_audio, &sh_audio->a_out_buffer[buflen],
1784 playsize - buflen, // min bytes
1785 sh_audio->a_out_buffer_size - buflen // max
1787 if (ret <= 0) { // EOF?
1788 if (mpctx->d_audio->eof) {
1789 audio_eof = 1;
1790 if (sh_audio->a_out_buffer_len == 0)
1791 return 0;
1793 break;
1795 sh_audio->a_out_buffer_len += ret;
1797 t = GetTimer() - t;
1798 tt = t*0.000001f; audio_time_usage+=tt;
1799 if (playsize > sh_audio->a_out_buffer_len) {
1800 playsize = sh_audio->a_out_buffer_len;
1801 if (audio_eof)
1802 playflags |= AOPLAY_FINAL_CHUNK;
1804 if (!playsize)
1805 break;
1807 // play audio:
1808 current_module="play_audio";
1810 // Is this pts value actually useful for the aos that access it?
1811 // They're obviously badly broken in the way they handle av sync;
1812 // would not having access to this make them more broken?
1813 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1814 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
1816 if (playsize > 0) {
1817 sh_audio->a_out_buffer_len -= playsize;
1818 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
1819 sh_audio->a_out_buffer_len);
1820 mpctx->delay += playback_speed*playsize/(double)ao_data.bps;
1822 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
1823 // Sanity check to avoid hanging in case current ao doesn't output
1824 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
1825 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
1826 sh_audio->a_out_buffer_len = 0;
1829 return 1;
1832 static int sleep_until_update(float *time_frame, float *aq_sleep_time)
1834 int frame_time_remaining = 0;
1835 current_module="calc_sleep_time";
1837 *time_frame -= GetRelativeTime(); // reset timer
1839 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1840 float delay = mpctx->audio_out->get_delay();
1841 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
1843 if (autosync) {
1845 * Adjust this raw delay value by calculating the expected
1846 * delay for this frame and generating a new value which is
1847 * weighted between the two. The higher autosync is, the
1848 * closer to the delay value gets to that which "-nosound"
1849 * would have used, and the longer it will take for A/V
1850 * sync to settle at the right value (but it eventually will.)
1851 * This settling time is very short for values below 100.
1853 float predicted = mpctx->delay / playback_speed + *time_frame;
1854 float difference = delay - predicted;
1855 delay = predicted + difference / (float)autosync;
1858 *time_frame = delay - mpctx->delay / playback_speed;
1860 // delay = amount of audio buffered in soundcard/driver
1861 if (delay > 0.25) delay=0.25; else
1862 if (delay < 0.10) delay=0.10;
1863 if (*time_frame > delay*0.6) {
1864 // sleep time too big - may cause audio drops (buffer underrun)
1865 frame_time_remaining = 1;
1866 *time_frame = delay*0.5;
1868 } else {
1869 // If we're lagging more than 200 ms behind the right playback rate,
1870 // don't try to "catch up".
1871 // If benchmark is set always output frames as fast as possible
1872 // without sleeping.
1873 if (*time_frame < -0.2 || benchmark)
1874 *time_frame = 0;
1877 *aq_sleep_time += *time_frame;
1880 //============================== SLEEP: ===================================
1882 // flag 256 means: libvo driver does its timing (dvb card)
1883 if (*time_frame > 0.001 && !(vo_flags&256))
1884 *time_frame = timing_sleep(*time_frame);
1885 return frame_time_remaining;
1888 int reinit_video_chain(void) {
1889 sh_video_t * const sh_video = mpctx->sh_video;
1890 //================== Init VIDEO (codec & libvo) ==========================
1891 if(!fixed_vo || !(inited_flags&INITED_VO)){
1892 current_module="preinit_libvo";
1894 //shouldn't we set dvideo->id=-2 when we fail?
1895 vo_config_count=0;
1896 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
1897 if(!(mpctx->video_out=init_best_video_out(video_driver_list))){
1898 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
1899 goto err_out;
1901 sh_video->video_out=mpctx->video_out;
1902 inited_flags|=INITED_VO;
1905 current_module="init_video_filters";
1907 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
1908 sh_video->vfilter=(void*)vf_open_filter(NULL,"vo",vf_arg);
1910 #ifdef HAVE_MENU
1911 if(use_menu) {
1912 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
1913 vf_menu = vf_open_plugin(libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
1914 if(!vf_menu) {
1915 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
1916 use_menu = 0;
1919 if(vf_menu)
1920 sh_video->vfilter=(void*)vf_menu;
1921 #endif
1923 #ifdef USE_ASS
1924 if(ass_enabled) {
1925 int i;
1926 int insert = 1;
1927 if (vf_settings)
1928 for (i = 0; vf_settings[i].name; ++i)
1929 if (strcmp(vf_settings[i].name, "ass") == 0) {
1930 insert = 0;
1931 break;
1933 if (insert) {
1934 extern vf_info_t vf_info_ass;
1935 vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
1936 char* vf_arg[] = {"auto", "1", NULL};
1937 vf_instance_t* vf_ass = vf_open_plugin(libass_vfs,sh_video->vfilter,"ass",vf_arg);
1938 if (vf_ass)
1939 sh_video->vfilter=(void*)vf_ass;
1940 else
1941 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
1944 #endif
1946 sh_video->vfilter=(void*)append_filters(sh_video->vfilter);
1948 #ifdef USE_ASS
1949 if (ass_enabled)
1950 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
1951 #endif
1953 current_module="init_video_codec";
1955 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1956 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
1957 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1959 if(!sh_video->inited){
1960 if(!fixed_vo) uninit_player(INITED_VO);
1961 goto err_out;
1964 inited_flags|=INITED_VCODEC;
1966 if (sh_video->codec)
1967 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
1969 sh_video->last_pts = MP_NOPTS_VALUE;
1970 sh_video->num_buffered_pts = 0;
1971 sh_video->next_frame_time = 0;
1973 if(auto_quality>0){
1974 // Auto quality option enabled
1975 output_quality=get_video_quality_max(sh_video);
1976 if(auto_quality>output_quality) auto_quality=output_quality;
1977 else output_quality=auto_quality;
1978 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
1979 set_video_quality(sh_video,output_quality);
1982 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
1984 current_module="init_vo";
1986 return 1;
1988 err_out:
1989 mpctx->sh_video = mpctx->d_video->sh = NULL;
1990 return 0;
1993 static double update_video(int *blit_frame)
1995 sh_video_t * const sh_video = mpctx->sh_video;
1996 //-------------------- Decode a frame: -----------------------
1997 double frame_time;
1998 *blit_frame = 0; // Don't blit if we hit EOF
1999 if (!correct_pts) {
2000 unsigned char* start=NULL;
2001 void *decoded_frame;
2002 int drop_frame=0;
2003 int in_size;
2005 current_module = "video_read_frame";
2006 frame_time = sh_video->next_frame_time;
2007 in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2008 &start, force_fps);
2009 if (in_size < 0)
2010 return -1;
2011 if (in_size > max_framesize)
2012 max_framesize = in_size; // stats
2013 sh_video->timer += frame_time;
2014 if (mpctx->sh_audio)
2015 mpctx->delay -= frame_time;
2016 // video_read_frame can change fps (e.g. for ASF video)
2017 vo_fps = sh_video->fps;
2018 // check for frame-drop:
2019 current_module = "check_framedrop";
2020 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2021 static int dropped_frames;
2022 float delay = playback_speed*mpctx->audio_out->get_delay();
2023 float d = delay-mpctx->delay;
2024 // we should avoid dropping too many frames in sequence unless we
2025 // are too late. and we allow 100ms A-V delay here:
2026 if (d < -dropped_frames*frame_time-0.100 &&
2027 mpctx->osd_function != OSD_PAUSE) {
2028 drop_frame = frame_dropping;
2029 ++drop_frame_cnt;
2030 ++dropped_frames;
2031 } else
2032 drop_frame = dropped_frames = 0;
2033 ++total_frame_cnt;
2035 update_subtitles(sh_video, mpctx->d_sub, 0);
2036 update_osd_msg();
2037 current_module = "decode_video";
2038 decoded_frame = decode_video(sh_video, start, in_size, drop_frame,
2039 sh_video->pts);
2040 current_module = "filter_video";
2041 *blit_frame = (decoded_frame && filter_video(sh_video, decoded_frame,
2042 sh_video->pts));
2044 else {
2045 if (!generate_video_frame(sh_video, mpctx->d_video))
2046 return -1;
2047 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter,
2048 VFCTRL_GET_PTS, &sh_video->pts);
2049 if (sh_video->pts == MP_NOPTS_VALUE) {
2050 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts after filters MISSING\n");
2051 sh_video->pts = sh_video->last_pts;
2053 if (sh_video->last_pts == MP_NOPTS_VALUE)
2054 sh_video->last_pts= sh_video->pts;
2055 else if (sh_video->last_pts >= sh_video->pts) {
2056 sh_video->last_pts = sh_video->pts;
2057 mp_msg(MSGT_CPLAYER, MSGL_WARN, "pts value <= previous");
2059 frame_time = sh_video->pts - sh_video->last_pts;
2060 sh_video->last_pts = sh_video->pts;
2061 sh_video->timer += frame_time;
2062 if(mpctx->sh_audio)
2063 mpctx->delay -= frame_time;
2064 *blit_frame = 1;
2066 return frame_time;
2069 void pause_loop(void)
2071 mp_cmd_t* cmd;
2072 if (!quiet) {
2073 // Small hack to display the pause message on the OSD line.
2074 // The pause string is: "\n == PAUSE == \r" so we need to
2075 // take the first and the last char out
2076 if (term_osd && !mpctx->sh_video) {
2077 char msg[128] = MSGTR_Paused;
2078 int mlen = strlen(msg);
2079 msg[mlen-1] = '\0';
2080 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2081 update_osd_msg();
2082 } else
2083 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2084 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2086 #ifdef HAVE_NEW_GUI
2087 if (use_gui)
2088 guiGetEvent(guiCEvent, (char *)guiSetPause);
2089 #endif
2090 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2091 mpctx->video_out->control(VOCTRL_PAUSE, NULL);
2093 if (mpctx->audio_out && mpctx->sh_audio)
2094 mpctx->audio_out->pause(); // pause audio, keep data if possible
2096 while ( (cmd = mp_input_get_cmd(20, 1, 1)) == NULL) {
2097 if (mpctx->sh_video && mpctx->video_out && vo_config_count)
2098 mpctx->video_out->check_events();
2099 #ifdef HAVE_NEW_GUI
2100 if (use_gui) {
2101 guiEventHandling();
2102 guiGetEvent(guiReDraw, NULL);
2103 if (guiIntfStruct.Playing!=2 || (rel_seek_secs || abs_seek_pos))
2104 break;
2106 #endif
2107 #ifdef HAVE_MENU
2108 if (vf_menu)
2109 vf_menu_pause_update(vf_menu);
2110 #endif
2111 usec_sleep(20000);
2113 if (cmd && cmd->id == MP_CMD_PAUSE) {
2114 cmd = mp_input_get_cmd(0,1,0);
2115 mp_cmd_free(cmd);
2117 mpctx->osd_function=OSD_PLAY;
2118 if (mpctx->audio_out && mpctx->sh_audio)
2119 mpctx->audio_out->resume(); // resume audio
2120 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2121 mpctx->video_out->control(VOCTRL_RESUME, NULL); // resume video
2122 (void)GetRelativeTime(); // ignore time that passed during pause
2123 #ifdef HAVE_NEW_GUI
2124 if (use_gui) {
2125 if (guiIntfStruct.Playing == guiSetStop)
2126 mpctx->eof = 1;
2127 else
2128 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2130 #endif
2133 void print_version(void){
2134 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s\n", MP_TITLE);
2136 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
2137 GetCpuCaps(&gCpuCaps);
2138 #ifdef ARCH_X86
2139 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
2140 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
2141 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
2142 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
2143 #ifdef RUNTIME_CPUDETECT
2144 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
2145 #else
2146 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
2147 #ifdef HAVE_MMX
2148 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
2149 #endif
2150 #ifdef HAVE_MMX2
2151 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
2152 #endif
2153 #ifdef HAVE_3DNOW
2154 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
2155 #endif
2156 #ifdef HAVE_3DNOWEX
2157 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
2158 #endif
2159 #ifdef HAVE_SSE
2160 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
2161 #endif
2162 #ifdef HAVE_SSE2
2163 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
2164 #endif
2165 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
2166 #endif /* RUNTIME_CPUDETECT */
2167 #endif /* ARCH_X86 */
2171 // Find the right mute status and record position for new file position
2172 static void edl_seek_reset(MPContext *mpctx)
2174 mpctx->edl_muted = 0;
2175 next_edl_record = edl_records;
2177 while (next_edl_record) {
2178 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2179 break;
2181 if (next_edl_record->action == EDL_MUTE)
2182 mpctx->edl_muted = !mpctx->edl_muted;
2183 next_edl_record = next_edl_record->next;
2185 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2186 mixer_mute(&mpctx->mixer);
2190 // Execute EDL command for the current position if one exists
2191 static void edl_update(MPContext *mpctx)
2193 if (!next_edl_record)
2194 return;
2196 if (!mpctx->sh_video) {
2197 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2198 free_edl(edl_records);
2199 next_edl_record = NULL;
2200 edl_records = NULL;
2201 return;
2204 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2205 if (next_edl_record->action == EDL_SKIP) {
2206 mpctx->osd_function = OSD_FFW;
2207 abs_seek_pos = 0;
2208 rel_seek_secs = next_edl_record->length_sec;
2209 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2210 "[%f], length [%f]\n", next_edl_record->start_sec,
2211 next_edl_record->stop_sec, next_edl_record->length_sec);
2212 edl_decision = 1;
2214 else if (next_edl_record->action == EDL_MUTE) {
2215 mpctx->edl_muted = !mpctx->edl_muted;
2216 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2217 mixer_mute(&mpctx->mixer);
2218 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2219 next_edl_record->start_sec );
2221 next_edl_record = next_edl_record->next;
2226 // style & 1 == 0 means seek relative to current position, == 1 means absolute
2227 // style & 2 == 0 means amount in seconds, == 2 means fraction of file length
2228 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2229 static int seek(MPContext *mpctx, double amount, int style)
2231 current_module = "seek";
2232 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2233 return -1;
2235 if (mpctx->sh_video) {
2236 current_module = "seek_video_reset";
2237 resync_video_stream(mpctx->sh_video);
2238 if (vo_config_count)
2239 mpctx->video_out->control(VOCTRL_RESET, NULL);
2240 mpctx->sh_video->num_buffered_pts = 0;
2241 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2242 mpctx->num_buffered_frames = 0;
2243 mpctx->delay = 0;
2244 // Not all demuxers set d_video->pts during seek, so this value
2245 // (which is used by at least vobsub and edl code below) may
2246 // be completely wrong (probably 0).
2247 mpctx->sh_video->pts = mpctx->d_video->pts;
2248 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2251 if (mpctx->sh_audio) {
2252 current_module = "seek_audio_reset";
2253 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2254 mpctx->sh_audio->a_buffer_len = 0;
2255 mpctx->sh_audio->a_out_buffer_len = 0;
2258 if (vo_vobsub) {
2259 current_module = "seek_vobsub_reset";
2260 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2263 edl_seek_reset(mpctx);
2265 c_total = 0;
2266 max_pts_correction = 0.1;
2267 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2268 drop_frame_cnt = 0;
2270 current_module = NULL;
2271 return 0;
2274 int main(int argc,char* argv[]){
2277 char * mem_ptr;
2279 // movie info:
2281 /* Flag indicating whether MPlayer should exit without playing anything. */
2282 int opt_exit = 0;
2284 //float a_frame=0; // Audio
2286 int i;
2288 int gui_no_filename=0;
2290 srand((int) time(NULL));
2292 InitTimer();
2294 mp_msg_init();
2296 for(i=1; i<argc; i++)
2297 if(!strcmp(argv[i], "-really-quiet"))
2298 verbose= -10;
2300 print_version();
2301 #if defined(WIN32) && defined(USE_WIN32DLL)
2302 set_path_env();
2303 #endif /*WIN32 && USE_WIN32DLL*/
2305 #ifdef USE_TV
2306 tv_param_immediate = 1;
2307 #endif
2309 if (argc > 1 && argv[1] &&
2310 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2311 use_gui = !strcmp(argv[1], "-gui");
2312 } else
2313 if ( argv[0] )
2315 char *base = strrchr(argv[0], '/');
2316 if (!base)
2317 base = strrchr(argv[0], '\\');
2318 if (!base)
2319 base = argv[0];
2320 if(strstr(base, "gmplayer"))
2321 use_gui=1;
2324 mconfig = m_config_new();
2325 m_config_register_options(mconfig,mplayer_opts);
2326 // TODO : add something to let modules register their options
2327 mp_input_register_options(mconfig);
2328 parse_cfgfiles(mconfig);
2330 #ifdef HAVE_NEW_GUI
2331 if ( use_gui ) cfg_read();
2332 #endif
2334 mpctx->playtree = m_config_parse_mp_command_line(mconfig, argc, argv);
2335 if(mpctx->playtree == NULL)
2336 opt_exit = 1;
2337 else {
2338 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2339 if(mpctx->playtree) {
2340 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
2341 if(mpctx->playtree_iter) {
2342 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2343 play_tree_iter_free(mpctx->playtree_iter);
2344 mpctx->playtree_iter = NULL;
2346 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2351 #if defined(WIN32) && defined(HAVE_NEW_GUI)
2352 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2353 if(runningmplayer && filename && use_gui){
2354 COPYDATASTRUCT csData;
2355 char file[MAX_PATH];
2356 char *filepart = filename;
2357 if(GetFullPathName(filename, MAX_PATH, file, &filepart)){
2358 csData.dwData = 0;
2359 csData.cbData = strlen(file)*2;
2360 csData.lpData = file;
2361 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2364 #endif
2366 #ifdef WIN32
2367 if(proc_priority){
2368 int i;
2369 for(i=0; priority_presets_defs[i].name; i++){
2370 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
2371 break;
2373 mp_msg(MSGT_CPLAYER,MSGL_STATUS,"Setting process priority: %s\n",
2374 priority_presets_defs[i].name);
2375 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
2377 #endif
2378 #ifndef HAVE_NEW_GUI
2379 if(use_gui){
2380 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2381 use_gui=0;
2383 #else
2384 #ifndef WIN32
2385 if(use_gui && !vo_init()){
2386 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2387 use_gui=0;
2389 #endif
2390 if (use_gui && mpctx->playtree_iter){
2391 char cwd[PATH_MAX+2];
2392 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2393 play_tree_iter_free(mpctx->playtree_iter);
2394 mpctx->playtree_iter=NULL;
2396 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2398 strcat(cwd, "/");
2399 // Prefix relative paths with current working directory
2400 play_tree_add_bpf(mpctx->playtree, cwd);
2402 // Import initital playtree into GUI.
2403 import_initial_playtree_into_gui(mpctx->playtree, mconfig, enqueue);
2405 #endif /* HAVE_NEW_GUI */
2407 if(video_driver_list && strcmp(video_driver_list[0],"help")==0){
2408 list_video_out();
2409 opt_exit = 1;
2412 if(audio_driver_list && strcmp(audio_driver_list[0],"help")==0){
2413 list_audio_out();
2414 opt_exit = 1;
2417 /* Check codecs.conf. */
2418 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2419 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2420 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2421 if(!parse_codec_cfg(NULL)){
2422 exit_player_with_rc(NULL, 0);
2424 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2427 free( mem_ptr ); // release the buffer created by get_path()
2430 #if 0
2431 if(video_codec_list){
2432 int i;
2433 video_codec=video_codec_list[0];
2434 for(i=0;video_codec_list[i];i++)
2435 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2437 #endif
2438 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2439 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2440 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2441 list_codecs(1);
2442 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2443 opt_exit = 1;
2445 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2446 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2447 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2448 list_codecs(0);
2449 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2450 opt_exit = 1;
2452 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2453 vfm_help();
2454 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2455 opt_exit = 1;
2457 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2458 afm_help();
2459 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2460 opt_exit = 1;
2462 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2463 af_help();
2464 printf("\n");
2465 opt_exit = 1;
2467 #ifdef HAVE_X11
2468 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2469 fstype_help();
2470 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2471 opt_exit = 1;
2473 #endif
2474 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2475 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2476 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2477 demuxer_help();
2478 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2479 opt_exit = 1;
2481 if(list_properties) {
2482 property_print_help();
2483 opt_exit = 1;
2486 if(opt_exit)
2487 exit_player(NULL);
2489 if (player_idle_mode && use_gui) {
2490 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2491 exit_player_with_rc(NULL, 1);
2494 if(!filename && !player_idle_mode){
2495 if(!use_gui){
2496 // no file/vcd/dvd -> show HELP:
2497 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2498 exit_player_with_rc(NULL, 0);
2499 } else gui_no_filename=1;
2502 /* Display what configure line was used */
2503 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2505 // Many users forget to include command line in bugreports...
2506 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2507 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2508 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2509 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2512 //------ load global data first ------
2514 // check font
2515 #ifdef HAVE_FREETYPE
2516 init_freetype();
2517 #endif
2518 #ifdef HAVE_FONTCONFIG
2519 if(!font_fontconfig)
2521 #endif
2522 #ifdef HAVE_BITMAP_FONT
2523 if(font_name){
2524 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2525 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2526 filename_recode(font_name));
2527 } else {
2528 // try default:
2529 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2530 free(mem_ptr); // release the buffer created by get_path()
2531 if(!vo_font)
2532 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2534 #endif
2535 #ifdef HAVE_FONTCONFIG
2537 #endif
2539 vo_init_osd();
2541 #ifdef USE_ASS
2542 ass_library = ass_init();
2543 #endif
2545 #ifdef HAVE_RTC
2546 if(!nortc)
2548 // seteuid(0); /* Can't hurt to try to get root here */
2549 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2550 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2551 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2552 else {
2553 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2555 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2556 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2557 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2558 close (rtc_fd);
2559 rtc_fd = -1;
2560 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2561 /* variable only by the root */
2562 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2563 close (rtc_fd);
2564 rtc_fd = -1;
2565 } else
2566 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2569 #ifdef HAVE_NEW_GUI
2570 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2571 // and now ? -- Pontscho
2572 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2573 #endif
2574 if(rtc_fd<0)
2575 #endif /* HAVE_RTC */
2576 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2577 softsleep?"software":timer_name);
2579 #ifdef USE_TERMCAP
2580 if ( !use_gui ) load_termcap(NULL); // load key-codes
2581 #endif
2583 // ========== Init keyboard FIFO (connection to libvo) ============
2585 // Init input system
2586 current_module = "init_input";
2587 mp_input_init(use_gui);
2588 mp_input_add_key_fd(-1,0,mplayer_get_key,NULL);
2589 if(slave_mode)
2590 #ifndef __MINGW32__
2591 mp_input_add_cmd_fd(0,1,NULL,NULL);
2592 #else
2593 mp_input_add_cmd_fd(0,0,mp_input_win32_slave_cmd_func,NULL);
2594 #endif
2595 else if(!noconsolecontrols)
2596 #ifdef HAVE_POSIX_SELECT
2597 mp_input_add_key_fd(0,1,NULL,NULL);
2598 #else
2599 mp_input_add_key_fd(0,0,NULL,NULL);
2600 #endif
2602 inited_flags|=INITED_INPUT;
2603 current_module = NULL;
2605 #ifdef HAVE_MENU
2606 if(use_menu) {
2607 if(menu_cfg && menu_init(mpctx, menu_cfg))
2608 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2609 else {
2610 menu_cfg = get_path("menu.conf");
2611 if(menu_init(mpctx, menu_cfg))
2612 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2613 else {
2614 if(menu_init(mpctx, MPLAYER_CONFDIR "/menu.conf"))
2615 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2616 else {
2617 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2618 use_menu = 0;
2623 #endif
2625 /// Catch signals
2626 #ifndef __MINGW32__
2627 signal(SIGCHLD,child_sighandler);
2628 #endif
2630 #ifdef CRASH_DEBUG
2631 prog_path = argv[0];
2632 #endif
2633 //========= Catch terminate signals: ================
2634 // terminate requests:
2635 signal(SIGTERM,exit_sighandler); // kill
2636 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2638 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2640 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2641 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2642 #ifdef ENABLE_SIGHANDLER
2643 // fatal errors:
2644 signal(SIGBUS,exit_sighandler); // bus error
2645 signal(SIGSEGV,exit_sighandler); // segfault
2646 signal(SIGILL,exit_sighandler); // illegal instruction
2647 signal(SIGFPE,exit_sighandler); // floating point exc.
2648 signal(SIGABRT,exit_sighandler); // abort()
2649 #ifdef CRASH_DEBUG
2650 if (crash_debug)
2651 signal(SIGTRAP,exit_sighandler);
2652 #endif
2653 #endif
2655 #ifdef HAVE_NEW_GUI
2656 if(use_gui){
2657 guiInit();
2658 guiGetEvent(guiSetContext, mpctx);
2659 inited_flags|=INITED_GUI;
2660 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2662 #endif
2664 // ******************* Now, let's see the per-file stuff ********************
2666 play_next_file:
2668 // init global sub numbers
2669 mpctx->global_sub_size = 0;
2670 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2672 if (filename) load_per_file_config (mconfig, filename);
2674 // We must enable getch2 here to be able to interrupt network connection
2675 // or cache filling
2676 if(!noconsolecontrols && !slave_mode){
2677 if(inited_flags&INITED_GETCH2)
2678 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2679 else
2680 getch2_enable(); // prepare stdin for hotkeys...
2681 inited_flags|=INITED_GETCH2;
2682 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2685 // =================== GUI idle loop (STOP state) ===========================
2686 #ifdef HAVE_NEW_GUI
2687 if ( use_gui ) {
2688 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
2689 guiGetEvent( guiSetDefaults,0 );
2690 while ( guiIntfStruct.Playing != 1 )
2692 mp_cmd_t* cmd;
2693 usec_sleep(20000);
2694 guiEventHandling();
2695 guiGetEvent( guiReDraw,NULL );
2696 if ( (cmd = mp_input_get_cmd(0,0,0)) != NULL) guiGetEvent( guiIEvent,(char *)cmd->id );
2698 guiGetEvent( guiSetParameters,NULL );
2699 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
2701 play_tree_t * entry = play_tree_new();
2702 play_tree_add_file( entry,guiIntfStruct.Filename );
2703 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
2704 else mpctx->playtree=play_tree_new();
2705 play_tree_set_child( mpctx->playtree,entry );
2706 if(mpctx->playtree)
2708 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
2709 if(mpctx->playtree_iter)
2711 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
2713 play_tree_iter_free(mpctx->playtree_iter);
2714 mpctx->playtree_iter = NULL;
2716 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2721 #endif /* HAVE_NEW_GUI */
2723 while (player_idle_mode && !filename) {
2724 play_tree_t * entry = NULL;
2725 mp_cmd_t * cmd;
2726 while (!(cmd = mp_input_get_cmd(0,1,0))) { // wait for command
2727 if (mpctx->video_out && vo_config_count) mpctx->video_out->check_events();
2728 usec_sleep(20000);
2730 switch (cmd->id) {
2731 case MP_CMD_LOADFILE:
2732 // prepare a tree entry with the new filename
2733 entry = play_tree_new();
2734 play_tree_add_file(entry, cmd->args[0].v.s);
2735 // The entry is added to the main playtree after the switch().
2736 break;
2737 case MP_CMD_LOADLIST:
2738 entry = parse_playlist_file(cmd->args[0].v.s);
2739 break;
2740 case MP_CMD_QUIT:
2741 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
2742 break;
2745 mp_cmd_free(cmd);
2747 if (entry) { // user entered a command that gave a valid entry
2748 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
2749 play_tree_free_list(mpctx->playtree->child, 1);
2750 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
2752 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
2754 play_tree_set_child(mpctx->playtree, entry);
2756 /* Make iterator start at the top the of tree. */
2757 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mconfig);
2758 if (!mpctx->playtree_iter) continue;
2760 // find the first real item in the tree
2761 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2762 // no items!
2763 play_tree_iter_free(mpctx->playtree_iter);
2764 mpctx->playtree_iter = NULL;
2765 continue; // wait for next command
2767 filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
2770 //---------------------------------------------------------------------------
2772 if(filename)
2773 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
2774 filename_recode(filename));
2776 if (edl_filename) {
2777 if (edl_records) free_edl(edl_records);
2778 next_edl_record = edl_records = edl_parse_file();
2780 if (edl_output_filename) {
2781 if (edl_fd) fclose(edl_fd);
2782 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
2784 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
2785 filename_recode(edl_output_filename));
2789 //==================== Open VOB-Sub ============================
2791 current_module="vobsub";
2792 if (vobsub_name){
2793 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
2794 if(vo_vobsub==NULL)
2795 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
2796 filename_recode(vobsub_name));
2797 }else if(sub_auto && filename && (strlen(filename)>=5)){
2798 /* try to autodetect vobsub from movie filename ::atmos */
2799 char *buf = malloc((strlen(filename)-3)),*psub;
2800 memset(buf,0,strlen(filename)-3); // make sure string is terminated
2801 strncpy(buf, filename, strlen(filename)-4);
2802 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
2803 /* try from ~/.mplayer/sub */
2804 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
2805 char *bname;
2806 int l;
2807 bname = strrchr(buf,'/');
2808 #ifdef WIN32
2809 if(!bname) bname = strrchr(buf,'\\');
2810 #endif
2811 if(bname) bname++;
2812 else bname = buf;
2813 l = strlen(psub) + strlen(bname) + 1;
2814 psub = realloc(psub,l);
2815 strcat(psub,bname);
2816 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
2817 free(psub);
2819 free(buf);
2821 if(vo_vobsub){
2822 inited_flags|=INITED_VOBSUB;
2823 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
2824 // check if vobsub requested only to display forced subtitles
2825 forced_subs_only=vobsub_get_forced_subs_flag(vo_vobsub);
2827 // setup global sub numbering
2828 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
2829 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
2832 //============ Open & Sync STREAM --- fork cache2 ====================
2834 mpctx->stream=NULL;
2835 mpctx->demuxer=NULL;
2836 if (mpctx->d_audio) {
2837 //free_demuxer_stream(mpctx->d_audio);
2838 mpctx->d_audio=NULL;
2840 if (mpctx->d_video) {
2841 //free_demuxer_stream(d_video);
2842 mpctx->d_video=NULL;
2844 mpctx->sh_audio=NULL;
2845 mpctx->sh_video=NULL;
2847 current_module="open_stream";
2848 mpctx->stream=open_stream(filename,0,&mpctx->file_format);
2849 if(!mpctx->stream) { // error...
2850 mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY);
2851 goto goto_next_file;
2853 inited_flags|=INITED_STREAM;
2855 #ifdef HAVE_NEW_GUI
2856 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
2857 #endif
2859 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
2860 play_tree_t* entry;
2861 // Handle playlist
2862 current_module="handle_playlist";
2863 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
2864 filename_recode(filename));
2865 entry = parse_playtree(mpctx->stream,0);
2866 mpctx->eof=playtree_add_playlist(entry);
2867 goto goto_next_file;
2869 mpctx->stream->start_pos+=seek_to_byte;
2871 if(stream_dump_type==5){
2872 unsigned char buf[4096];
2873 int len;
2874 FILE *f;
2875 current_module="dumpstream";
2876 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
2877 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
2878 exit_player(MSGTR_Exit_error);
2880 stream_reset(mpctx->stream);
2881 stream_seek(mpctx->stream,mpctx->stream->start_pos);
2882 f=fopen(stream_dump_name,"wb");
2883 if(!f){
2884 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
2885 exit_player(MSGTR_Exit_error);
2887 while(!mpctx->stream->eof){
2888 len=stream_read(mpctx->stream,buf,4096);
2889 if(len>0) {
2890 if(fwrite(buf,len,1,f) != 1) {
2891 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
2892 exit_player(MSGTR_Exit_error);
2896 if(fclose(f)) {
2897 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
2898 exit_player(MSGTR_Exit_error);
2900 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
2901 exit_player_with_rc(MSGTR_Exit_eof, 0);
2904 #ifdef USE_DVDREAD
2905 if(mpctx->stream->type==STREAMTYPE_DVD){
2906 current_module="dvd lang->id";
2907 if(audio_id==-1) audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
2908 if(dvdsub_lang && dvdsub_id==-2) dvdsub_id=-1;
2909 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
2910 // setup global sub numbering
2911 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
2912 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
2913 current_module=NULL;
2915 #endif
2917 #ifdef USE_DVDNAV
2918 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
2919 current_module="dvdnav lang->id";
2920 if(audio_id==-1) audio_id=dvdnav_aid_from_lang(mpctx->stream,audio_lang);
2921 if(dvdsub_lang && dvdsub_id==-2) dvdsub_id=-1;
2922 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
2923 // setup global sub numbering
2924 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
2925 mpctx->global_sub_size += dvdnav_number_of_subs(mpctx->stream);
2926 current_module=NULL;
2928 #endif
2930 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
2931 goto_enable_cache:
2932 if(stream_cache_size>0){
2933 current_module="enable_cache";
2934 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
2935 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
2936 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
2937 if((mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY))) goto goto_next_file;
2940 //============ Open DEMUXERS --- DETECT file type =======================
2941 current_module="demux_open";
2943 mpctx->demuxer=demux_open(mpctx->stream,mpctx->file_format,audio_id,video_id,dvdsub_id,filename);
2945 // HACK to get MOV Reference Files working
2947 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
2949 unsigned char* playlist_entry;
2950 play_tree_t *list = NULL, *entry = NULL;
2952 current_module="handle_demux_playlist";
2953 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
2955 char *temp, *bname;
2957 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
2958 filename_recode(playlist_entry));
2960 bname=mp_basename(playlist_entry);
2961 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
2962 continue;
2964 if (!strncmp(bname,mp_basename(filename),strlen(bname))) // ignoring self-reference
2965 continue;
2967 entry = play_tree_new();
2969 if (filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
2971 temp=malloc((strlen(filename)-strlen(mp_basename(filename))+strlen(playlist_entry)+1));
2972 if (temp)
2974 strncpy(temp, filename, strlen(filename)-strlen(mp_basename(filename)));
2975 temp[strlen(filename)-strlen(mp_basename(filename))]='\0';
2976 strcat(temp, playlist_entry);
2977 play_tree_add_file(entry,temp);
2978 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
2979 free(temp);
2982 else
2983 play_tree_add_file(entry,playlist_entry);
2985 if(!list)
2986 list = entry;
2987 else
2988 play_tree_append_entry(list,entry);
2990 free_demuxer(mpctx->demuxer);
2991 mpctx->demuxer = NULL;
2993 if (list)
2995 entry = play_tree_new();
2996 play_tree_set_child(entry,list);
2997 mpctx->eof=playtree_add_playlist(entry);
2998 goto goto_next_file;
3002 if(!mpctx->demuxer)
3003 goto goto_next_file;
3004 inited_flags|=INITED_DEMUXER;
3006 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3007 int i;
3008 // setup global sub numbering
3009 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3010 for (i = 0; i < MAX_S_STREAMS; i++)
3011 if (mpctx->demuxer->s_streams[i])
3012 mpctx->global_sub_size++;
3014 if (mpctx->global_sub_size <= dvdsub_id) mpctx->global_sub_size = dvdsub_id + 1;
3016 current_module="demux_open2";
3018 //file_format=demuxer->file_format;
3020 mpctx->d_audio=mpctx->demuxer->audio;
3021 mpctx->d_video=mpctx->demuxer->video;
3022 mpctx->d_sub=mpctx->demuxer->sub;
3024 // DUMP STREAMS:
3025 if((stream_dump_type)&&(stream_dump_type<4)){
3026 FILE *f;
3027 demux_stream_t *ds=NULL;
3028 current_module="dump";
3029 // select stream to dump
3030 switch(stream_dump_type){
3031 case 1: ds=mpctx->d_audio;break;
3032 case 2: ds=mpctx->d_video;break;
3033 case 3: ds=mpctx->d_sub;break;
3035 if(!ds){
3036 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3037 exit_player(MSGTR_Exit_error);
3039 // disable other streams:
3040 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3041 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3042 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3043 // let's dump it!
3044 f=fopen(stream_dump_name,"wb");
3045 if(!f){
3046 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3047 exit_player(MSGTR_Exit_error);
3049 while(!ds->eof){
3050 unsigned char* start;
3051 int in_size=ds_get_packet(ds,&start);
3052 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3053 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3054 if(in_size>0) fwrite(start,in_size,1,f);
3056 fclose(f);
3057 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3058 exit_player_with_rc(MSGTR_Exit_eof, 0);
3061 mpctx->sh_audio=mpctx->d_audio->sh;
3062 mpctx->sh_video=mpctx->d_video->sh;
3064 if(mpctx->sh_video){
3066 current_module="video_read_properties";
3067 if(!video_read_properties(mpctx->sh_video)) {
3068 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3069 mpctx->sh_video=mpctx->d_video->sh=NULL;
3070 } else {
3071 mp_msg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
3072 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3073 mpctx->sh_video->fps,mpctx->sh_video->frametime
3076 /* need to set fps here for output encoders to pick it up in their init */
3077 if(force_fps){
3078 mpctx->sh_video->fps=force_fps;
3079 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3081 vo_fps = mpctx->sh_video->fps;
3083 if(!mpctx->sh_video->fps && !force_fps){
3084 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3085 mpctx->sh_video=mpctx->d_video->sh=NULL;
3091 if(!mpctx->sh_video && !mpctx->sh_audio){
3092 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3093 #ifdef HAS_DVBIN_SUPPORT
3094 if((mpctx->stream->type == STREAMTYPE_DVB) && mpctx->stream->priv)
3096 dvb_priv_t *priv = (dvb_priv_t*) mpctx->stream->priv;
3097 if(priv->is_on)
3099 int dir;
3100 int v = mpctx->last_dvb_step;
3101 if(v > 0)
3102 dir = DVB_CHANNEL_HIGHER;
3103 else
3104 dir = DVB_CHANNEL_LOWER;
3106 if(dvb_step_channel(priv, dir))
3107 mpctx->eof = mpctx->dvbin_reopen = 1;
3110 #endif
3111 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3114 /* display clip info */
3115 demux_info_print(mpctx->demuxer);
3117 //================== Read SUBTITLES (DVD & TEXT) ==========================
3118 if(vo_spudec==NULL && mpctx->sh_video &&
3119 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV || mpctx->d_sub->id >= 0)){
3120 init_vo_spudec();
3123 // Apply current settings for forced subs
3124 if (vo_spudec!=NULL)
3125 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
3127 if(mpctx->sh_video) {
3128 // after reading video params we should load subtitles because
3129 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3130 // check .sub
3131 current_module="read_subtitles_file";
3132 if(sub_name){
3133 for (i = 0; sub_name[i] != NULL; ++i)
3134 add_subtitles (sub_name[i], mpctx->sh_video->fps, 0);
3136 if(sub_auto) { // auto load sub file ...
3137 char *psub = get_path( "sub/" );
3138 char **tmp = sub_filenames((psub ? psub : ""), filename);
3139 int i = 0;
3140 free(psub); // release the buffer created by get_path() above
3141 while (tmp[i]) {
3142 add_subtitles (tmp[i], mpctx->sh_video->fps, 0);
3143 free(tmp[i++]);
3145 free(tmp);
3147 if (mpctx->set_of_sub_size > 0) {
3148 // setup global sub numbering
3149 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3150 mpctx->global_sub_size += mpctx->set_of_sub_size;
3154 if (mpctx->global_sub_size) {
3155 // find the best sub to use
3156 if (vobsub_id >= 0) {
3157 // if user asks for a vobsub id, use that first.
3158 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_id;
3159 } else if (dvdsub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3160 // if user asks for a dvd sub id, use that next.
3161 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id;
3162 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3163 // if there are text subs to use, use those. (autosubs come last here)
3164 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3166 } else if (mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3167 // if nothing else works, get subs from the demuxer.
3168 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX];
3170 } else {
3171 // nothing worth doing automatically.
3172 mpctx->global_sub_pos = -1;
3174 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3175 mpctx->global_sub_pos--;
3176 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3177 if(subdata)
3178 switch (stream_dump_type) {
3179 case 3: list_sub_file(subdata); break;
3180 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3181 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3182 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3183 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3184 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3188 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3189 filename_recode(filename));
3190 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3191 if (mpctx->sh_video) {
3192 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3193 if (mpctx->sh_video->format >= 0x20202020)
3194 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3195 else
3196 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3197 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3198 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3199 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3200 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3201 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3203 if (mpctx->sh_audio) {
3204 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3205 if (mpctx->sh_audio->format >= 0x20202020)
3206 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3207 else
3208 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3209 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3210 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3211 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3213 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3215 if(!mpctx->sh_video) goto main; // audio-only
3217 if(!reinit_video_chain()) {
3218 if(!mpctx->sh_video){
3219 if(!mpctx->sh_audio) goto goto_next_file;
3220 goto main; // exit_player(MSGTR_Exit_error);
3224 if(vo_flags & 0x08 && vo_spudec)
3225 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3227 #ifdef HAVE_FREETYPE
3228 force_load_font = 1;
3229 #endif
3231 //================== MAIN: ==========================
3232 main:
3233 current_module="main";
3235 if(playing_msg) {
3236 char* msg = property_expand_string(mpctx, playing_msg);
3237 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3238 free(msg);
3242 // Disable the term OSD in verbose mode
3243 if(verbose) term_osd = 0;
3246 //int frame_corr_num=0; //
3247 //float v_frame=0; // Video
3248 float time_frame=0; // Timer
3249 //float num_frames=0; // number of frames played
3251 int frame_time_remaining=0; // flag
3252 int blit_frame=0;
3253 mpctx->num_buffered_frames=0;
3255 // Make sure old OSD does not stay around,
3256 // e.g. with -fixed-vo and same-resolution files
3257 clear_osd_msgs();
3258 update_osd_msg();
3260 //================ SETUP AUDIO ==========================
3262 if(mpctx->sh_audio){
3263 reinit_audio_chain();
3264 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3265 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3268 current_module="av_init";
3270 if(mpctx->sh_video){
3271 mpctx->sh_video->timer=0;
3272 if (! ignore_start)
3273 audio_delay += mpctx->sh_video->stream_delay;
3275 if(mpctx->sh_audio){
3276 if (! ignore_start)
3277 audio_delay -= mpctx->sh_audio->stream_delay;
3278 mpctx->delay=-audio_delay;
3281 if(!mpctx->sh_audio){
3282 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3283 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3284 ds_free_packs(mpctx->d_audio); // free buffered chunks
3285 //mpctx->d_audio->id=-2; // do not read audio chunks
3286 //uninit_player(INITED_AO); // close device
3288 if(!mpctx->sh_video){
3289 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3290 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3291 ds_free_packs(mpctx->d_video);
3292 mpctx->d_video->id=-2;
3293 //if(!fixed_vo) uninit_player(INITED_VO);
3296 if (!mpctx->sh_video && !mpctx->sh_audio)
3297 goto goto_next_file;
3299 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3300 if(force_fps && mpctx->sh_video){
3301 vo_fps = mpctx->sh_video->fps=force_fps;
3302 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3303 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3306 #ifdef HAVE_NEW_GUI
3307 if ( use_gui ) {
3308 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3309 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3310 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3311 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3312 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3314 #endif
3316 //==================== START PLAYING =======================
3318 if(loop_times>1) loop_times--; else
3319 if(loop_times==1) loop_times = -1;
3321 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3323 total_time_usage_start=GetTimer();
3324 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3325 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3326 play_n_frames=play_n_frames_mf;
3328 if(play_n_frames==0){
3329 mpctx->eof=PT_NEXT_ENTRY; goto goto_next_file;
3332 if (seek_to_sec) {
3333 seek(mpctx, seek_to_sec, 1);
3334 end_at.pos += seek_to_sec;
3337 if (end_at.type == END_AT_SIZE) {
3338 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3339 end_at.type = END_AT_NONE;
3343 while(!mpctx->eof){
3344 float aq_sleep_time=0;
3345 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3346 mpctx->sh_audio = mpctx->d_audio->sh;
3347 mpctx->sh_audio->ds = mpctx->d_audio;
3348 reinit_audio_chain();
3351 /*========================== PLAY AUDIO ============================*/
3353 if (mpctx->sh_audio)
3354 if (!fill_audio_out_buffers())
3355 // at eof, all audio at least written to ao
3356 if (!mpctx->sh_video)
3357 mpctx->eof = PT_NEXT_ENTRY;
3360 if(!mpctx->sh_video) {
3361 // handle audio-only case:
3362 double a_pos=0;
3363 if(!quiet || end_at.type == END_AT_TIME )
3364 a_pos = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3366 if(!quiet)
3367 print_status(a_pos, 0, 0);
3369 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3370 mpctx->eof = PT_NEXT_ENTRY;
3371 update_osd_msg();
3373 } else {
3375 /*========================== PLAY VIDEO ============================*/
3377 vo_pts=mpctx->sh_video->timer*90000.0;
3378 vo_fps=mpctx->sh_video->fps;
3380 if (!mpctx->num_buffered_frames) {
3381 double frame_time = update_video(&blit_frame);
3382 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3383 if (mpctx->sh_video->vf_inited < 0) {
3384 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3385 mpctx->eof = 1; goto goto_next_file;
3387 if (frame_time < 0)
3388 mpctx->eof = 1;
3389 else {
3390 // might return with !eof && !blit_frame if !correct_pts
3391 mpctx->num_buffered_frames += blit_frame;
3392 time_frame += frame_time / playback_speed; // for nosound
3396 // ==========================================================================
3398 // current_module="draw_osd";
3399 // if(vo_config_count) mpctx->video_out->draw_osd();
3401 #ifdef HAVE_NEW_GUI
3402 if(use_gui) guiEventHandling();
3403 #endif
3405 current_module="vo_check_events";
3406 if (vo_config_count) mpctx->video_out->check_events();
3408 #ifdef HAVE_X11
3409 if (stop_xscreensaver) {
3410 current_module = "stop_xscreensaver";
3411 xscreensaver_heartbeat();
3413 #endif
3415 frame_time_remaining = sleep_until_update(&time_frame, &aq_sleep_time);
3417 //====================== FLIP PAGE (VIDEO BLT): =========================
3419 current_module="flip_page";
3420 if (!frame_time_remaining && blit_frame) {
3421 unsigned int t2=GetTimer();
3423 if(vo_config_count) mpctx->video_out->flip_page();
3424 mpctx->num_buffered_frames--;
3426 vout_time_usage += (GetTimer() - t2) * 0.000001;
3428 //====================== A-V TIMESTAMP CORRECTION: =========================
3430 adjust_sync_and_print_status(frame_time_remaining, time_frame);
3432 //============================ Auto QUALITY ============================
3434 /*Output quality adjustments:*/
3435 if(auto_quality>0){
3436 current_module="autoq";
3437 // float total=0.000001f * (GetTimer()-aq_total_time);
3438 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3439 if(output_quality<auto_quality && aq_sleep_time>0)
3440 ++output_quality;
3441 else
3442 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3443 if(output_quality>1 && aq_sleep_time<0)
3444 --output_quality;
3445 else
3446 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3447 output_quality=0;
3448 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3449 set_video_quality(mpctx->sh_video,output_quality);
3452 if (play_n_frames >= 0 && !frame_time_remaining && blit_frame) {
3453 --play_n_frames;
3454 if (play_n_frames <= 0) mpctx->eof = PT_NEXT_ENTRY;
3458 // FIXME: add size based support for -endpos
3459 if (end_at.type == END_AT_TIME &&
3460 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3461 mpctx->eof = PT_NEXT_ENTRY;
3463 } // end if(mpctx->sh_video)
3465 #ifdef USE_DVDNAV
3466 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3467 nav_highlight_t hl;
3468 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3469 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3470 vo_osd_changed (OSDTYPE_DVDNAV);
3472 #endif
3474 //============================ Handle PAUSE ===============================
3476 current_module="pause";
3478 if(osd_visible){
3479 // 36000000 means max timed visibility is 1 hour into the future, if
3480 // the difference is greater assume it's wrapped around from below 0
3481 if (osd_visible - GetTimerMS() > 36000000) {
3482 osd_visible = 0;
3483 vo_osd_progbar_type=-1; // disable
3484 vo_osd_changed(OSDTYPE_PROGBAR);
3485 if (mpctx->osd_function != OSD_PAUSE)
3486 mpctx->osd_function = OSD_PLAY;
3490 if (mpctx->osd_function == OSD_PAUSE) {
3491 pause_loop();
3492 mpctx->was_paused = 1;
3495 // handle -sstep
3496 if(step_sec>0) {
3497 mpctx->osd_function=OSD_FFW;
3498 rel_seek_secs+=step_sec;
3501 edl_update(mpctx);
3503 //================= Keyboard events, SEEKing ====================
3505 current_module="key_events";
3508 mp_cmd_t* cmd;
3509 int brk_cmd = 0;
3510 while( !brk_cmd && (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
3511 brk_cmd = run_command(mpctx, cmd);
3512 mp_cmd_free(cmd);
3513 if (brk_cmd == 2)
3514 goto goto_enable_cache;
3517 mpctx->was_paused = 0;
3519 /* Looping. */
3520 if(mpctx->eof==1 && loop_times>=0) {
3521 int l = loop_times;
3522 play_tree_iter_step(mpctx->playtree_iter,0,0);
3523 loop_times = l;
3524 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d, eof = %d\n", loop_times,mpctx->eof);
3526 if(loop_times>1) loop_times--; else
3527 if(loop_times==1) loop_times=-1;
3528 play_n_frames=play_n_frames_mf;
3529 mpctx->eof=0;
3530 abs_seek_pos=1; rel_seek_secs=seek_to_sec;
3531 loop_seek = 1;
3534 if(rel_seek_secs || abs_seek_pos){
3535 if (seek(mpctx, rel_seek_secs, abs_seek_pos) >= 0) {
3536 // Set OSD:
3537 if(!loop_seek){
3538 if( !edl_decision )
3539 set_osd_bar(0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3541 // osd_function has been set (or not) by the code triggering the seek
3542 osd_visible=(GetTimerMS() + 1000) | 1; // to revert to PLAY pointer after 1 sec
3545 rel_seek_secs=0;
3546 abs_seek_pos=0;
3547 loop_seek=0;
3548 edl_decision = 0;
3551 #ifdef HAVE_NEW_GUI
3552 if(use_gui){
3553 guiEventHandling();
3554 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3555 // get pos from frame number / total frames
3556 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3557 } else {
3558 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3560 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3561 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3562 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3563 guiGetEvent( guiReDraw,NULL );
3564 guiGetEvent( guiSetVolume,NULL );
3565 if(guiIntfStruct.Playing==0) break; // STOP
3566 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3567 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3568 #ifdef USE_DVDREAD
3569 if ( mpctx->stream->type == STREAMTYPE_DVD )
3571 dvd_priv_t * dvdp = mpctx->stream->priv;
3572 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3574 #endif
3576 #endif /* HAVE_NEW_GUI */
3578 } // while(!mpctx->eof)
3580 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->eof);
3582 #ifdef HAS_DVBIN_SUPPORT
3583 if(mpctx->dvbin_reopen)
3585 mpctx->eof = 0;
3586 uninit_player(INITED_ALL-(INITED_GUI|INITED_STREAM|INITED_INPUT|INITED_GETCH2|(fixed_vo?INITED_VO:0)));
3587 cache_uninit(mpctx->stream);
3588 mpctx->dvbin_reopen = 0;
3589 goto goto_enable_cache;
3591 #endif
3594 goto_next_file: // don't jump here after ao/vo/getch initialization!
3596 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3598 if(benchmark){
3599 double tot=video_time_usage+vout_time_usage+audio_time_usage;
3600 double total_time_usage;
3601 total_time_usage_start=GetTimer()-total_time_usage_start;
3602 total_time_usage = (float)total_time_usage_start*0.000001;
3603 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
3604 video_time_usage,vout_time_usage,audio_time_usage,
3605 total_time_usage-tot,total_time_usage);
3606 if(total_time_usage>0.0)
3607 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
3608 100.0*video_time_usage/total_time_usage,
3609 100.0*vout_time_usage/total_time_usage,
3610 100.0*audio_time_usage/total_time_usage,
3611 100.0*(total_time_usage-tot)/total_time_usage,
3612 100.0);
3613 if(total_frame_cnt && frame_dropping)
3614 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
3615 total_frame_cnt-drop_frame_cnt,
3616 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
3617 drop_frame_cnt,
3618 100*drop_frame_cnt/total_frame_cnt,
3619 total_frame_cnt,
3620 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
3624 // time to uninit all, except global stuff:
3625 uninit_player(INITED_ALL-(INITED_GUI+INITED_INPUT+(fixed_vo?INITED_VO:0)));
3627 if ( mpctx->set_of_sub_size > 0 )
3629 current_module="sub_free";
3630 for (i = 0; i < mpctx->set_of_sub_size; ++i) {
3631 sub_free( mpctx->set_of_subtitles[i] );
3632 #ifdef USE_ASS
3633 if ( mpctx->set_of_ass_tracks[i] )
3634 ass_free_track( mpctx->set_of_ass_tracks[i] );
3635 #endif
3637 mpctx->set_of_sub_size = 0;
3639 vo_sub_last = vo_sub=NULL;
3640 subdata=NULL;
3641 #ifdef USE_ASS
3642 ass_track = NULL;
3643 #endif
3645 if(mpctx->eof == PT_NEXT_ENTRY || mpctx->eof == PT_PREV_ENTRY) {
3646 mpctx->eof = mpctx->eof == PT_NEXT_ENTRY ? 1 : -1;
3647 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) == PLAY_TREE_ITER_ENTRY) {
3648 mpctx->eof = 1;
3649 } else {
3650 play_tree_iter_free(mpctx->playtree_iter);
3651 mpctx->playtree_iter = NULL;
3653 mpctx->play_tree_step = 1;
3654 } else if (mpctx->eof == PT_UP_NEXT || mpctx->eof == PT_UP_PREV) {
3655 mpctx->eof = mpctx->eof == PT_UP_NEXT ? 1 : -1;
3656 if ( mpctx->playtree_iter ) {
3657 if(play_tree_iter_up_step(mpctx->playtree_iter,mpctx->eof,0) == PLAY_TREE_ITER_ENTRY) {
3658 mpctx->eof = 1;
3659 } else {
3660 play_tree_iter_free(mpctx->playtree_iter);
3661 mpctx->playtree_iter = NULL;
3664 } else { // NEXT PREV SRC
3665 mpctx->eof = mpctx->eof == PT_PREV_SRC ? -1 : 1;
3668 if(mpctx->eof == 0) mpctx->eof = 1;
3670 while(mpctx->playtree_iter != NULL) {
3671 filename = play_tree_iter_get_file(mpctx->playtree_iter,mpctx->eof);
3672 if(filename == NULL) {
3673 if( play_tree_iter_step(mpctx->playtree_iter,mpctx->eof,0) != PLAY_TREE_ITER_ENTRY) {
3674 play_tree_iter_free(mpctx->playtree_iter);
3675 mpctx->playtree_iter = NULL;
3677 } else
3678 break;
3681 #ifdef HAVE_NEW_GUI
3682 if( use_gui && !mpctx->playtree_iter )
3684 #ifdef USE_DVDREAD
3685 if ( !guiIntfStruct.DiskChanged )
3686 #endif
3687 mplEnd();
3689 #endif
3691 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
3692 if (!mpctx->playtree_iter) filename = NULL;
3693 mpctx->eof = 0;
3694 goto play_next_file;
3698 exit_player_with_rc(MSGTR_Exit_eof, 0);
3700 return 1;