Prefer DMO Windows Media codecs over the DShow ones. They are considerably
[mplayer/greg.git] / mplayer.c
blob69168855b87a4e04bf817b50d4e50c63f91f4258
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "config.h"
9 #ifdef WIN32
10 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
11 #include <windows.h>
12 #endif
13 #include <string.h>
14 #include <unistd.h>
16 // #include <sys/mman.h>
17 #include <sys/types.h>
18 #ifndef __MINGW32__
19 #include <sys/ioctl.h>
20 #include <sys/wait.h>
21 #else
22 #define SIGHUP 1 /* hangup */
23 #define SIGQUIT 3 /* quit */
24 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
25 #define SIGBUS 10 /* bus error */
26 #define SIGPIPE 13 /* broken pipe */
27 #endif
29 #include <sys/time.h>
30 #include <sys/stat.h>
32 #include <signal.h>
33 #include <time.h>
34 #include <fcntl.h>
35 #include <limits.h>
37 #include <errno.h>
39 #include "version.h"
41 #include "mp_msg.h"
43 #define HELP_MP_DEFINE_STATIC
44 #include "help_mp.h"
46 #include "m_option.h"
47 #include "m_config.h"
48 #include "m_property.h"
50 #include "cfg-mplayer-def.h"
52 #include "subreader.h"
54 #include "libvo/video_out.h"
56 #include "libvo/font_load.h"
57 #include "libvo/sub.h"
59 #ifdef HAVE_X11
60 #include "libvo/x11_common.h"
61 #endif
63 #include "libao2/audio_out.h"
65 #include "codec-cfg.h"
67 #include "edl.h"
69 #include "spudec.h"
70 #include "vobsub.h"
72 #include "osdep/getch2.h"
73 #include "osdep/timer.h"
75 #include "cpudetect.h"
77 #ifdef HAVE_NEW_GUI
78 #include "gui/interface.h"
79 #endif
81 #include "input/input.h"
83 int slave_mode=0;
84 int player_idle_mode=0;
85 int quiet=0;
86 int enable_mouse_movements=0;
88 #ifdef WIN32
89 char * proc_priority=NULL;
90 #endif
92 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
94 #ifdef HAVE_RTC
95 #ifdef __linux__
96 #include <linux/rtc.h>
97 #else
98 #include <rtc.h>
99 #define RTC_IRQP_SET RTCIO_IRQP_SET
100 #define RTC_PIE_ON RTCIO_PIE_ON
101 #endif /* __linux__ */
102 #endif /* HAVE_RTC */
104 #ifdef USE_TV
105 #include "stream/tv.h"
106 #endif
107 #ifdef USE_RADIO
108 #include "stream/stream_radio.h"
109 #endif
111 #ifdef HAS_DVBIN_SUPPORT
112 #include "stream/dvbin.h"
113 #include "stream/cache2.h"
114 #endif
116 //**************************************************************************//
117 // Playtree
118 //**************************************************************************//
119 #include "playtree.h"
120 #include "playtreeparser.h"
122 #ifdef HAVE_NEW_GUI
123 extern int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
124 extern int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
125 #endif
127 //**************************************************************************//
128 // Config
129 //**************************************************************************//
130 #include "parser-cfg.h"
131 #include "parser-mpcmd.h"
133 m_config_t* mconfig;
135 //**************************************************************************//
136 // Config file
137 //**************************************************************************//
139 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
141 static int cfg_include(m_option_t *conf, char *filename){
142 return m_config_parse_config_file(mconfig, filename);
145 #include "get_path.h"
147 //**************************************************************************//
148 // XScreensaver
149 //**************************************************************************//
151 #ifdef HAVE_X11
152 void xscreensaver_heartbeat(void);
153 #endif
155 //**************************************************************************//
156 //**************************************************************************//
157 // Input media streaming & demultiplexer:
158 //**************************************************************************//
160 static int max_framesize=0;
162 #include "stream/stream.h"
163 #include "libmpdemux/demuxer.h"
164 #include "libmpdemux/stheader.h"
165 //#include "parse_es.h"
166 #include "libmpdemux/matroska.h"
168 #ifdef USE_DVDREAD
169 #include "stream/stream_dvd.h"
170 #endif
172 #ifdef USE_DVDNAV
173 #include "stream/stream_dvdnav.h"
174 #endif
176 #include "libmpcodecs/dec_audio.h"
177 #include "libmpcodecs/dec_video.h"
178 #include "libmpcodecs/mp_image.h"
179 #include "libmpcodecs/vf.h"
180 #include "libmpcodecs/vd.h"
182 #include "mixer.h"
184 #include "mp_core.h"
186 //**************************************************************************//
187 //**************************************************************************//
189 // Common FIFO functions, and keyboard/event FIFO code
190 #include "mp_fifo.h"
191 int noconsolecontrols=0;
192 //**************************************************************************//
194 // Not all functions in mplayer.c take the context as an argument yet
195 static MPContext mpctx_s = {
196 .osd_function = OSD_PLAY,
197 .begin_skip = MP_NOPTS_VALUE,
198 .play_tree_step = 1,
199 .global_sub_pos = -1,
200 .set_of_sub_pos = -1,
201 .file_format = DEMUXER_TYPE_UNKNOWN,
202 .loop_times = -1,
203 #ifdef HAS_DVBIN_SUPPORT
204 .last_dvb_step = 1,
205 #endif
208 static MPContext *mpctx = &mpctx_s;
210 int fixed_vo=0;
212 // benchmark:
213 double video_time_usage=0;
214 double vout_time_usage=0;
215 static double audio_time_usage=0;
216 static int total_time_usage_start=0;
217 static int total_frame_cnt=0;
218 static int drop_frame_cnt=0; // total number of dropped frames
219 int benchmark=0;
221 // options:
222 int auto_quality=0;
223 static int output_quality=0;
225 float playback_speed=1.0;
227 int use_gui=0;
229 #ifdef HAVE_NEW_GUI
230 int enqueue=0;
231 #endif
233 static int list_properties = 0;
235 int osd_level=1;
236 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
237 unsigned int osd_visible;
238 int osd_duration = 1000;
240 int term_osd = 1;
241 static char* term_osd_esc = "\x1b[A\r\x1b[K";
242 static char* playing_msg = NULL;
243 // seek:
244 static double seek_to_sec;
245 static off_t seek_to_byte=0;
246 static off_t step_sec=0;
247 static int loop_seek=0;
249 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
251 // A/V sync:
252 int autosync=0; // 30 might be a good default value.
254 // may be changed by GUI: (FIXME!)
255 float rel_seek_secs=0;
256 int abs_seek_pos=0;
258 // codecs:
259 char **audio_codec_list=NULL; // override audio codec
260 char **video_codec_list=NULL; // override video codec
261 char **audio_fm_list=NULL; // override audio codec family
262 char **video_fm_list=NULL; // override video codec family
264 // demuxer:
265 extern char *demuxer_name; // override demuxer
266 extern char *audio_demuxer_name; // override audio demuxer
267 extern char *sub_demuxer_name; // override sub demuxer
269 // streaming:
270 int audio_id=-1;
271 int video_id=-1;
272 int dvdsub_id=-2;
273 int vobsub_id=-1;
274 char* audio_lang=NULL;
275 char* dvdsub_lang=NULL;
276 static char* spudec_ifo=NULL;
277 char* filename=NULL; //"MI2-Trailer.avi";
278 int forced_subs_only=0;
279 int file_filter=1;
281 // cache2:
282 int stream_cache_size=-1;
283 #ifdef USE_STREAM_CACHE
284 extern int cache_fill_status;
286 float stream_cache_min_percent=20.0;
287 float stream_cache_seek_min_percent=50.0;
288 #else
289 #define cache_fill_status 0
290 #endif
292 // dump:
293 static char *stream_dump_name="stream.dump";
294 int stream_dump_type=0;
296 // A-V sync:
297 static float default_max_pts_correction=-1;//0.01f;
298 static float max_pts_correction=0;//default_max_pts_correction;
299 static float c_total=0;
300 float audio_delay=0;
301 static int ignore_start=0;
303 static int softsleep=0;
305 float force_fps=0;
306 static int force_srate=0;
307 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
308 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
309 static int play_n_frames=-1;
310 static int play_n_frames_mf=-1;
312 // screen info:
313 char** video_driver_list=NULL;
314 char** audio_driver_list=NULL;
316 // sub:
317 char *font_name=NULL;
318 char *sub_font_name=NULL;
319 #ifdef HAVE_FONTCONFIG
320 extern int font_fontconfig;
321 #endif
322 float font_factor=0.75;
323 char **sub_name=NULL;
324 float sub_delay=0;
325 float sub_fps=0;
326 int sub_auto = 1;
327 char *vobsub_name=NULL;
328 /*DSP!!char *dsp=NULL;*/
329 int subcc_enabled=0;
330 int suboverlap_enabled = 1;
332 #ifdef USE_ASS
333 #include "libass/ass.h"
334 #include "libass/ass_mp.h"
335 #endif
337 extern int mp_msg_levels[MSGT_MAX];
338 extern int mp_msg_level_all;
340 char* current_module=NULL; // for debugging
343 // ---
345 #ifdef HAVE_MENU
346 #include "m_struct.h"
347 #include "libmenu/menu.h"
348 extern void vf_menu_pause_update(struct vf_instance_s* vf);
349 extern vf_info_t vf_info_menu;
350 static vf_info_t* libmenu_vfs[] = {
351 &vf_info_menu,
352 NULL
354 static vf_instance_t* vf_menu = NULL;
355 static int use_menu = 0;
356 static char* menu_cfg = NULL;
357 static char* menu_root = "main";
358 #endif
361 #ifdef HAVE_RTC
362 static int nortc = 1;
363 static char* rtc_device;
364 #endif
366 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
367 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
368 short edl_decision = 0; ///< 1 when an EDL operation has been made.
369 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
370 int use_filedir_conf;
372 static unsigned int inited_flags=0;
373 #include "mpcommon.h"
374 #include "command.h"
376 #include "metadata.h"
378 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
380 void *mpctx_get_video_out(MPContext *mpctx)
382 return mpctx->video_out;
385 void *mpctx_get_audio_out(MPContext *mpctx)
387 return mpctx->audio_out;
390 void *mpctx_get_playtree_iter(MPContext *mpctx)
392 return mpctx->playtree_iter;
395 void *mpctx_get_mixer(MPContext *mpctx)
397 return &mpctx->mixer;
400 int mpctx_get_global_sub_size(MPContext *mpctx)
402 return mpctx->global_sub_size;
405 static int is_valid_metadata_type (metadata_t type) {
406 switch (type)
408 /* check for valid video stream */
409 case META_VIDEO_CODEC:
410 case META_VIDEO_BITRATE:
411 case META_VIDEO_RESOLUTION:
413 if (!mpctx->sh_video)
414 return 0;
415 break;
418 /* check for valid audio stream */
419 case META_AUDIO_CODEC:
420 case META_AUDIO_BITRATE:
421 case META_AUDIO_SAMPLES:
423 if (!mpctx->sh_audio)
424 return 0;
425 break;
428 /* check for valid demuxer */
429 case META_INFO_TITLE:
430 case META_INFO_ARTIST:
431 case META_INFO_ALBUM:
432 case META_INFO_YEAR:
433 case META_INFO_COMMENT:
434 case META_INFO_TRACK:
435 case META_INFO_GENRE:
437 if (!mpctx->demuxer)
438 return 0;
439 break;
442 default:
443 break;
446 return 1;
449 static char *get_demuxer_info (char *tag) {
450 char **info = mpctx->demuxer->info;
451 int n;
453 if (!info || !tag)
454 return NULL;
456 for (n = 0; info[2*n] != NULL ; n++)
457 if (!strcmp (info[2*n], tag))
458 break;
460 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
463 char *get_metadata (metadata_t type) {
464 char *meta = NULL;
465 sh_audio_t * const sh_audio = mpctx->sh_audio;
466 sh_video_t * const sh_video = mpctx->sh_video;
468 if (!is_valid_metadata_type (type))
469 return NULL;
471 switch (type)
473 case META_NAME:
475 return strdup (mp_basename2 (filename));
478 case META_VIDEO_CODEC:
480 if (sh_video->format == 0x10000001)
481 meta = strdup ("mpeg1");
482 else if (sh_video->format == 0x10000002)
483 meta = strdup ("mpeg2");
484 else if (sh_video->format == 0x10000004)
485 meta = strdup ("mpeg4");
486 else if (sh_video->format == 0x10000005)
487 meta = strdup ("h264");
488 else if (sh_video->format >= 0x20202020)
490 meta = malloc (8);
491 sprintf (meta, "%.4s", (char *) &sh_video->format);
493 else
495 meta = malloc (8);
496 sprintf (meta, "0x%08X", sh_video->format);
498 return meta;
501 case META_VIDEO_BITRATE:
503 meta = malloc (16);
504 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
505 return meta;
508 case META_VIDEO_RESOLUTION:
510 meta = malloc (16);
511 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
512 return meta;
515 case META_AUDIO_CODEC:
517 if (sh_audio->codec && sh_audio->codec->name)
518 meta = strdup (sh_audio->codec->name);
519 return meta;
522 case META_AUDIO_BITRATE:
524 meta = malloc (16);
525 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
526 return meta;
529 case META_AUDIO_SAMPLES:
531 meta = malloc (16);
532 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
533 return meta;
536 /* check for valid demuxer */
537 case META_INFO_TITLE:
538 return get_demuxer_info ("Title");
540 case META_INFO_ARTIST:
541 return get_demuxer_info ("Artist");
543 case META_INFO_ALBUM:
544 return get_demuxer_info ("Album");
546 case META_INFO_YEAR:
547 return get_demuxer_info ("Year");
549 case META_INFO_COMMENT:
550 return get_demuxer_info ("Comment");
552 case META_INFO_TRACK:
553 return get_demuxer_info ("Track");
555 case META_INFO_GENRE:
556 return get_demuxer_info ("Genre");
558 default:
559 break;
562 return meta;
565 /// step size of mixer changes
566 int volstep = 3;
568 void uninit_player(unsigned int mask){
569 mask=inited_flags&mask;
571 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
573 if(mask&INITED_ACODEC){
574 inited_flags&=~INITED_ACODEC;
575 current_module="uninit_acodec";
576 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
577 #ifdef HAVE_NEW_GUI
578 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
579 #endif
580 mpctx->sh_audio=NULL;
581 mpctx->mixer.afilter = NULL;
584 if(mask&INITED_VCODEC){
585 inited_flags&=~INITED_VCODEC;
586 current_module="uninit_vcodec";
587 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
588 mpctx->sh_video=NULL;
589 #ifdef HAVE_MENU
590 vf_menu=NULL;
591 #endif
594 if(mask&INITED_DEMUXER){
595 inited_flags&=~INITED_DEMUXER;
596 current_module="free_demuxer";
597 if(mpctx->demuxer){
598 mpctx->stream=mpctx->demuxer->stream;
599 free_demuxer(mpctx->demuxer);
601 mpctx->demuxer=NULL;
604 // kill the cache process:
605 if(mask&INITED_STREAM){
606 inited_flags&=~INITED_STREAM;
607 current_module="uninit_stream";
608 if(mpctx->stream) free_stream(mpctx->stream);
609 mpctx->stream=NULL;
612 if(mask&INITED_VO){
613 inited_flags&=~INITED_VO;
614 current_module="uninit_vo";
615 mpctx->video_out->uninit();
616 mpctx->video_out=NULL;
619 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
620 if(mask&INITED_GETCH2){
621 inited_flags&=~INITED_GETCH2;
622 current_module="uninit_getch2";
623 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
624 // restore terminal:
625 getch2_disable();
628 if(mask&INITED_VOBSUB){
629 inited_flags&=~INITED_VOBSUB;
630 current_module="uninit_vobsub";
631 if(vo_vobsub) vobsub_close(vo_vobsub);
632 vo_vobsub=NULL;
635 if (mask&INITED_SPUDEC){
636 inited_flags&=~INITED_SPUDEC;
637 current_module="uninit_spudec";
638 spudec_free(vo_spudec);
639 vo_spudec=NULL;
642 if(mask&INITED_AO){
643 inited_flags&=~INITED_AO;
644 current_module="uninit_ao";
645 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
646 mpctx->audio_out->uninit(mpctx->eof?0:1); mpctx->audio_out=NULL;
649 #ifdef HAVE_NEW_GUI
650 if(mask&INITED_GUI){
651 inited_flags&=~INITED_GUI;
652 current_module="uninit_gui";
653 guiDone();
655 #endif
657 if(mask&INITED_INPUT){
658 inited_flags&=~INITED_INPUT;
659 current_module="uninit_input";
660 mp_input_uninit();
663 current_module=NULL;
666 void exit_player_with_rc(const char* how, int rc){
668 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
669 uninit_player(INITED_ALL);
670 #ifdef HAVE_X11
671 #ifdef HAVE_NEW_GUI
672 if ( !use_gui )
673 #endif
674 vo_uninit(); // Close the X11 connection (if any is open).
675 #endif
677 #ifdef HAVE_FREETYPE
678 current_module="uninit_font";
679 if (vo_font) free_font_desc(vo_font);
680 vo_font = NULL;
681 done_freetype();
682 #endif
683 free_osd_list();
685 #ifdef USE_ASS
686 ass_library_done(ass_library);
687 #endif
689 current_module="exit_player";
691 // free mplayer config
692 if(mconfig)
693 m_config_free(mconfig);
695 if(mpctx->playtree)
696 play_tree_free(mpctx->playtree, 1);
699 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
700 if(how) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,how);
701 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
703 exit(rc);
706 void exit_player(const char* how){
707 exit_player_with_rc(how, 1);
710 #ifndef __MINGW32__
711 static void child_sighandler(int x){
712 pid_t pid;
713 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
715 #endif
717 #ifdef CRASH_DEBUG
718 static char *prog_path;
719 static int crash_debug = 0;
720 #endif
722 static void exit_sighandler(int x){
723 static int sig_count=0;
724 #ifdef CRASH_DEBUG
725 if (!crash_debug || x != SIGTRAP)
726 #endif
727 ++sig_count;
728 if(inited_flags==0 && sig_count>1) exit(1);
729 if(sig_count==5)
731 /* We're crashing bad and can't uninit cleanly :(
732 * by popular request, we make one last (dirty)
733 * effort to restore the user's
734 * terminal. */
735 getch2_disable();
736 exit(1);
738 if(sig_count==6) exit(1);
739 if(sig_count>6){
740 // can't stop :(
741 #ifndef __MINGW32__
742 kill(getpid(),SIGKILL);
743 #endif
745 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
746 current_module?current_module:"unknown"
748 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
749 if(sig_count<=1)
750 switch(x){
751 case SIGINT:
752 case SIGQUIT:
753 case SIGTERM:
754 case SIGKILL:
755 break; // killed from keyboard (^C) or killed [-9]
756 case SIGILL:
757 #ifdef RUNTIME_CPUDETECT
758 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
759 #else
760 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
761 #endif
762 case SIGFPE:
763 case SIGSEGV:
764 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
765 default:
766 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
767 #ifdef CRASH_DEBUG
768 if (crash_debug) {
769 int gdb_pid;
770 char spid[20];
771 snprintf(spid, 19, "%i", getpid());
772 spid[19] = 0;
773 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
774 gdb_pid = fork();
775 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
776 if (gdb_pid == 0) { // We are the child
777 getch2_disable(); // allow terminal to work properly with gdb
778 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
779 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
780 } else if (gdb_pid < 0)
781 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
782 else {
783 waitpid(gdb_pid, NULL, 0);
785 if (x == SIGTRAP) return;
787 #endif
789 exit_player(NULL);
792 extern void mp_input_register_options(m_config_t* cfg);
794 #include "cfg-mplayer.h"
796 static void parse_cfgfiles( m_config_t* conf )
798 char *conffile;
799 int conffile_fd;
800 if (m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
801 exit_player(NULL);
802 if ((conffile = get_path("")) == NULL) {
803 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
804 } else {
805 #ifdef __MINGW32__
806 mkdir(conffile);
807 #else
808 mkdir(conffile, 0777);
809 #endif
810 free(conffile);
811 if ((conffile = get_path("config")) == NULL) {
812 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
813 } else {
814 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
815 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
816 write(conffile_fd, default_config, strlen(default_config));
817 close(conffile_fd);
819 if (m_config_parse_config_file(conf, conffile) < 0)
820 exit_player(NULL);
821 free(conffile);
826 void load_per_file_config (m_config_t* conf, const char *const file)
828 char *confpath;
829 char cfg[strlen(file)+10];
830 struct stat st;
831 char *name;
833 sprintf (cfg, "%s.conf", file);
835 if (use_filedir_conf && !stat (cfg, &st))
837 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
838 m_config_parse_config_file (conf, cfg);
839 return;
842 if ((name = strrchr (cfg, '/')) == NULL)
843 name = cfg;
844 else
845 name++;
847 if ((confpath = get_path (name)) != NULL)
849 if (!stat (confpath, &st))
851 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
852 m_config_parse_config_file (conf, confpath);
855 free (confpath);
859 /* When libmpdemux performs a blocking operation (network connection or
860 * cache filling) if the operation fails we use this function to check
861 * if it was interrupted by the user.
862 * The function returns a new value for eof. */
863 static int libmpdemux_was_interrupted(int eof) {
864 mp_cmd_t* cmd;
865 if((cmd = mp_input_get_cmd(0,0,0)) != NULL) {
866 switch(cmd->id) {
867 case MP_CMD_QUIT:
868 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
869 case MP_CMD_PLAY_TREE_STEP: {
870 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
871 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
872 } break;
873 case MP_CMD_PLAY_TREE_UP_STEP: {
874 eof = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
875 } break;
876 case MP_CMD_PLAY_ALT_SRC_STEP: {
877 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
878 } break;
880 mp_cmd_free(cmd);
882 return eof;
885 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
887 int playtree_add_playlist(play_tree_t* entry)
889 play_tree_add_bpf(entry,filename);
891 #ifdef HAVE_NEW_GUI
892 if (use_gui) {
893 if (entry) {
894 import_playtree_playlist_into_gui(entry, mconfig);
895 play_tree_free_list(entry,1);
897 } else
898 #endif
900 if(!entry) {
901 entry = mpctx->playtree_iter->tree;
902 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
903 return PT_NEXT_ENTRY;
905 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
906 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
907 return PT_NEXT_ENTRY;
910 play_tree_remove(entry,1,1);
911 return PT_NEXT_SRC;
913 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
914 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
915 entry = mpctx->playtree_iter->tree;
916 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
917 return PT_NEXT_ENTRY;
919 play_tree_remove(entry,1,1);
921 return PT_NEXT_SRC;
924 void add_subtitles(char *filename, float fps, int silent)
926 sub_data *subd;
927 #ifdef USE_ASS
928 ass_track_t *asst = 0;
929 #endif
931 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
932 return;
935 subd = sub_read_file(filename, fps);
936 #ifdef USE_ASS
937 if (ass_enabled)
938 #ifdef USE_ICONV
939 asst = ass_read_file(ass_library, filename, sub_cp);
940 #else
941 asst = ass_read_file(ass_library, filename, 0);
942 #endif
943 if (ass_enabled && subd && !asst)
944 asst = ass_read_subdata(ass_library, subd, fps);
946 if (!asst && !subd && !silent)
947 #else
948 if(!subd && !silent)
949 #endif
950 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_CantLoadSub,
951 filename_recode(filename));
953 #ifdef USE_ASS
954 if (!asst && !subd) return;
955 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
956 #else
957 if (!subd) return;
958 #endif
959 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
960 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
961 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
962 filename_recode(filename));
963 ++mpctx->set_of_sub_size;
964 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
965 filename_recode(filename));
968 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
969 void update_set_of_subtitles(void)
970 // subdata was changed, set_of_sub... have to be updated.
972 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
973 int i;
974 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
975 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
976 set_of_subtitles[i-1] = set_of_subtitles[i];
977 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
978 --mpctx->set_of_sub_size;
979 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
981 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
982 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
984 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
985 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
986 ++mpctx->set_of_sub_size;
990 void init_vo_spudec(void) {
991 if (vo_spudec)
992 spudec_free(vo_spudec);
993 inited_flags &= ~INITED_SPUDEC;
994 vo_spudec = NULL;
995 if (spudec_ifo) {
996 unsigned int palette[16], width, height;
997 current_module="spudec_init_vobsub";
998 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
999 vo_spudec=spudec_new_scaled(palette, width, height);
1002 #ifdef USE_DVDREAD
1003 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1004 current_module="spudec_init_dvdread";
1005 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1006 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1008 #endif
1010 #ifdef USE_DVDNAV
1011 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1012 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1013 current_module="spudec_init_dvdnav";
1014 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1016 #endif
1018 if ((vo_spudec == NULL) && (mpctx->demuxer->type == DEMUXER_TYPE_MATROSKA) &&
1019 (mpctx->d_sub->sh != NULL) && (((sh_sub_t *)mpctx->d_sub->sh)->type == 'v')) {
1020 sh_sub_t *mkv_sh_sub = (sh_sub_t *)mpctx->d_sub->sh;
1021 current_module = "spudec_init_matroska";
1022 vo_spudec =
1023 spudec_new_scaled_vobsub(mkv_sh_sub->palette, mkv_sh_sub->colors,
1024 mkv_sh_sub->custom_colors, mkv_sh_sub->width,
1025 mkv_sh_sub->height);
1026 forced_subs_only = mkv_sh_sub->forced_subs_only;
1029 if (vo_spudec==NULL) {
1030 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1031 unsigned int *palette = NULL;
1032 if (sh && sh->has_palette)
1033 palette = sh->palette;
1034 current_module="spudec_init_normal";
1035 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h);
1036 spudec_set_font_factor(vo_spudec,font_factor);
1039 if (vo_spudec!=NULL)
1040 inited_flags|=INITED_SPUDEC;
1044 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1045 * make it all work is to use the builtin SDL-bootstrap code, which
1046 * will be done automatically by replacing our main() if we include SDL.h.
1048 #if defined(SYS_DARWIN) && defined(HAVE_SDL)
1049 #include <SDL.h>
1050 #endif
1053 * \brief append a formatted string
1054 * \param buf buffer to print into
1055 * \param pos position of terminating 0 in buf
1056 * \param len maximum number of characters in buf, not including terminating 0
1057 * \param format printf format string
1059 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1061 va_list va;
1062 va_start(va, format);
1063 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1064 va_end(va);
1065 if (*pos >= len ) {
1066 buf[len] = 0;
1067 *pos = len;
1072 * \brief append time in the hh:mm:ss.f format
1073 * \param buf buffer to print into
1074 * \param pos position of terminating 0 in buf
1075 * \param len maximum number of characters in buf, not including terminating 0
1076 * \param time time value to convert/append
1078 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1079 long tenths = 10 * time;
1080 int f1 = tenths % 10;
1081 int ss = (tenths / 10) % 60;
1082 int mm = (tenths / 600) % 60;
1083 int hh = tenths / 36000;
1084 if (time <= 0) {
1085 saddf(buf, pos, len, "unknown");
1086 return;
1088 if (hh > 0)
1089 saddf(buf, pos, len, "%2d:", hh);
1090 if (hh > 0 || mm > 0)
1091 saddf(buf, pos, len, "%02d:", mm);
1092 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1096 * \brief print the status line
1097 * \param a_pos audio position
1098 * \param a_v A-V desynchronization
1099 * \param corr amount out A-V synchronization
1101 static void print_status(float a_pos, float a_v, float corr)
1103 sh_video_t * const sh_video = mpctx->sh_video;
1104 int width;
1105 char *line;
1106 unsigned pos = 0;
1107 get_screen_size();
1108 if (screen_width > 0)
1109 width = screen_width;
1110 else
1111 width = 80;
1112 #ifdef WIN32
1113 /* Windows command line is broken (MinGW's rxvt works, but we
1114 * should not depend on that). */
1115 width--;
1116 #endif
1117 line = malloc(width + 1); // one additional char for the terminating null
1119 // Audio time
1120 if (mpctx->sh_audio) {
1121 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1122 if (!sh_video) {
1123 float len = demuxer_get_time_length(mpctx->demuxer);
1124 saddf(line, &pos, width, "(");
1125 sadd_hhmmssf(line, &pos, width, a_pos);
1126 saddf(line, &pos, width, ") of %.1f (", len);
1127 sadd_hhmmssf(line, &pos, width, len);
1128 saddf(line, &pos, width, ") ");
1132 // Video time
1133 if (sh_video)
1134 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1136 // A-V sync
1137 if (mpctx->sh_audio && sh_video)
1138 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
1140 // Video stats
1141 if (sh_video)
1142 saddf(line, &pos, width, "%3d/%3d ",
1143 (int)sh_video->num_frames,
1144 (int)sh_video->num_frames_decoded);
1146 // CPU usage
1147 if (sh_video) {
1148 if (sh_video->timer > 0.5)
1149 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1150 (int)(100.0*video_time_usage*playback_speed/(double)sh_video->timer),
1151 (int)(100.0*vout_time_usage*playback_speed/(double)sh_video->timer),
1152 (100.0*audio_time_usage*playback_speed/(double)sh_video->timer));
1153 else
1154 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1155 } else if (mpctx->sh_audio) {
1156 if (mpctx->delay > 0.5)
1157 saddf(line, &pos, width, "%4.1f%% ",
1158 100.0*audio_time_usage/(double)mpctx->delay);
1159 else
1160 saddf(line, &pos, width, "??,?%% ");
1163 // VO stats
1164 if (sh_video)
1165 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1167 #ifdef USE_STREAM_CACHE
1168 // cache stats
1169 if (stream_cache_size > 0)
1170 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1171 #endif
1173 // other
1174 if (playback_speed != 1)
1175 saddf(line, &pos, width, "%4.2fx ", playback_speed);
1177 // end
1178 if (erase_to_end_of_line) {
1179 line[pos] = 0;
1180 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1181 } else {
1182 memset(&line[pos], ' ', width - pos);
1183 line[width] = 0;
1184 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1186 free(line);
1190 * \brief build a chain of audio filters that converts the input format
1191 * to the ao's format, taking into account the current playback_speed.
1192 * \param sh_audio describes the requested input format of the chain.
1193 * \param ao_data describes the requested output format of the chain.
1195 int build_afilter_chain(sh_audio_t *sh_audio, ao_data_t *ao_data)
1197 int new_srate;
1198 int result;
1199 if (!sh_audio)
1201 #ifdef HAVE_NEW_GUI
1202 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1203 #endif
1204 mpctx->mixer.afilter = NULL;
1205 return 0;
1207 if(af_control_any_rev(sh_audio->afilter,
1208 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1209 &playback_speed)) {
1210 new_srate = sh_audio->samplerate;
1211 } else {
1212 new_srate = sh_audio->samplerate * playback_speed;
1213 if (new_srate != ao_data->samplerate) {
1214 // limits are taken from libaf/af_resample.c
1215 if (new_srate < 8000)
1216 new_srate = 8000;
1217 if (new_srate > 192000)
1218 new_srate = 192000;
1219 playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1222 result = init_audio_filters(sh_audio, new_srate,
1223 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1224 mpctx->mixer.afilter = sh_audio->afilter;
1225 #ifdef HAVE_NEW_GUI
1226 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1227 #endif
1228 return result;
1232 typedef struct mp_osd_msg mp_osd_msg_t;
1233 struct mp_osd_msg {
1234 /// Previous message on the stack.
1235 mp_osd_msg_t* prev;
1236 /// Message text.
1237 char msg[64];
1238 int id,level,started;
1239 /// Display duration in ms.
1240 unsigned time;
1243 /// OSD message stack.
1244 static mp_osd_msg_t* osd_msg_stack = NULL;
1247 * \brief Add a message on the OSD message stack
1249 * If a message with the same id is already present in the stack
1250 * it is pulled on top of the stack, otherwise a new message is created.
1254 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1255 mp_osd_msg_t *msg,*last=NULL;
1256 va_list va;
1257 int r;
1259 // look if the id is already in the stack
1260 for(msg = osd_msg_stack ; msg && msg->id != id ;
1261 last = msg, msg = msg->prev);
1262 // not found: alloc it
1263 if(!msg) {
1264 msg = calloc(1,sizeof(mp_osd_msg_t));
1265 msg->prev = osd_msg_stack;
1266 osd_msg_stack = msg;
1267 } else if(last) { // found, but it's not on top of the stack
1268 last->prev = msg->prev;
1269 msg->prev = osd_msg_stack;
1270 osd_msg_stack = msg;
1272 // write the msg
1273 va_start(va,fmt);
1274 r = vsnprintf(msg->msg, 64, fmt, va);
1275 va_end(va);
1276 if(r >= 64) msg->msg[63] = 0;
1277 // set id and time
1278 msg->id = id;
1279 msg->level = level;
1280 msg->time = time;
1285 * \brief Remove a message from the OSD stack
1287 * This function can be used to get rid of a message right away.
1291 void rm_osd_msg(int id) {
1292 mp_osd_msg_t *msg,*last=NULL;
1294 // Search for the msg
1295 for(msg = osd_msg_stack ; msg && msg->id != id ;
1296 last = msg, msg = msg->prev);
1297 if(!msg) return;
1299 // Detach it from the stack and free it
1300 if(last)
1301 last->prev = msg->prev;
1302 else
1303 osd_msg_stack = msg->prev;
1304 free(msg);
1308 * \brief Remove all messages from the OSD stack
1312 static void clear_osd_msgs(void) {
1313 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1314 while(msg) {
1315 prev = msg->prev;
1316 free(msg);
1317 msg = prev;
1319 osd_msg_stack = NULL;
1323 * \brief Get the current message from the OSD stack.
1325 * This function decrements the message timer and destroys the old ones.
1326 * The message that should be displayed is returned (if any).
1330 static mp_osd_msg_t* get_osd_msg(void) {
1331 mp_osd_msg_t *msg,*prev,*last = NULL;
1332 static unsigned last_update = 0;
1333 unsigned now = GetTimerMS();
1334 unsigned diff;
1335 char hidden_dec_done = 0;
1337 if (osd_visible) {
1338 // 36000000 means max timed visibility is 1 hour into the future, if
1339 // the difference is greater assume it's wrapped around from below 0
1340 if (osd_visible - now > 36000000) {
1341 osd_visible = 0;
1342 vo_osd_progbar_type = -1; // disable
1343 vo_osd_changed(OSDTYPE_PROGBAR);
1344 if (mpctx->osd_function != OSD_PAUSE)
1345 mpctx->osd_function = OSD_PLAY;
1349 if(!last_update) last_update = now;
1350 diff = now >= last_update ? now - last_update : 0;
1352 last_update = now;
1354 // Look for the first message in the stack with high enough level.
1355 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1356 prev = msg->prev;
1357 if(msg->level > osd_level && hidden_dec_done) continue;
1358 // The message has a high enough level or it is the first hidden one
1359 // in both cases we decrement the timer or kill it.
1360 if(!msg->started || msg->time > diff) {
1361 if(msg->started) msg->time -= diff;
1362 else msg->started = 1;
1363 // display it
1364 if(msg->level <= osd_level) return msg;
1365 hidden_dec_done = 1;
1366 continue;
1368 // kill the message
1369 free(msg);
1370 if(last) {
1371 last->prev = prev;
1372 msg = last;
1373 } else {
1374 osd_msg_stack = prev;
1375 msg = NULL;
1378 // Nothing found
1379 return NULL;
1383 * \brief Display the OSD bar.
1385 * Display the OSD bar or fall back on a simple message.
1389 void set_osd_bar(int type,const char* name,double min,double max,double val) {
1391 if(osd_level < 1) return;
1393 if(mpctx->sh_video) {
1394 osd_visible = (GetTimerMS() + 1000) | 1;
1395 vo_osd_progbar_type = type;
1396 vo_osd_progbar_value = 256*(val-min)/(max-min);
1397 vo_osd_changed(OSDTYPE_PROGBAR);
1398 return;
1401 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1402 name,ROUND(100*(val-min)/(max-min)));
1407 * \brief Update the OSD message line.
1409 * This function displays the current message on the vo OSD or on the term.
1410 * If the stack is empty and the OSD level is high enough the timer
1411 * is displayed (only on the vo OSD).
1415 static void update_osd_msg(void) {
1416 mp_osd_msg_t *msg;
1417 static char osd_text[64] = "";
1418 static char osd_text_timer[64];
1420 // we need some mem for vo_osd_text
1421 vo_osd_text = (unsigned char*)osd_text;
1423 // Look if we have a msg
1424 if((msg = get_osd_msg())) {
1425 if(strcmp(osd_text,msg->msg)) {
1426 strncpy((char*)osd_text, msg->msg, 63);
1427 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1428 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1430 return;
1433 if(mpctx->sh_video) {
1434 // fallback on the timer
1435 if(osd_level>=2) {
1436 int len = demuxer_get_time_length(mpctx->demuxer);
1437 int percentage = -1;
1438 char percentage_text[10];
1439 int pts = demuxer_get_current_time(mpctx->demuxer);
1441 if (mpctx->osd_show_percentage)
1442 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1444 if (percentage >= 0)
1445 snprintf(percentage_text, 9, " (%d%%)", percentage);
1446 else
1447 percentage_text[0] = 0;
1449 if (osd_level == 3)
1450 snprintf(osd_text_timer, 63,
1451 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1452 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1453 len/3600,(len/60)%60,len%60,percentage_text);
1454 else
1455 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1456 mpctx->osd_function,pts/3600,(pts/60)%60,
1457 pts%60,percentage_text);
1458 } else
1459 osd_text_timer[0]=0;
1461 // always decrement the percentage timer
1462 if(mpctx->osd_show_percentage)
1463 mpctx->osd_show_percentage--;
1465 if(strcmp(osd_text,osd_text_timer)) {
1466 strncpy(osd_text, osd_text_timer, 63);
1467 vo_osd_changed(OSDTYPE_OSD);
1469 return;
1472 // Clear the term osd line
1473 if(term_osd && osd_text[0]) {
1474 osd_text[0] = 0;
1475 printf("%s\n",term_osd_esc);
1479 ///@}
1480 // OSDMsgStack
1483 void reinit_audio_chain(void) {
1484 if(mpctx->sh_audio){
1485 current_module="init_audio_codec";
1486 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1487 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1488 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1489 mpctx->d_audio->id = -2;
1490 return;
1491 } else
1492 inited_flags|=INITED_ACODEC;
1493 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1496 //const ao_info_t *info=audio_out->info;
1497 current_module="af_preinit";
1498 ao_data.samplerate=force_srate;
1499 ao_data.channels=0;
1500 ao_data.format=audio_output_format;
1501 #if 1
1502 // first init to detect best values
1503 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1504 // input:
1505 mpctx->sh_audio->samplerate,
1506 // output:
1507 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1508 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1509 exit_player(MSGTR_Exit_error);
1511 #endif
1512 current_module="ao2_init";
1513 if(!(mpctx->audio_out=init_best_audio_out(audio_driver_list,
1514 0, // plugin flag
1515 ao_data.samplerate,
1516 ao_data.channels,
1517 ao_data.format,0))){
1518 // FAILED:
1519 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1520 uninit_player(INITED_ACODEC); // close codec
1521 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1522 mpctx->d_audio->id = -2;
1523 return;
1524 } else {
1525 // SUCCESS:
1526 inited_flags|=INITED_AO;
1527 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1528 mpctx->audio_out->info->short_name,
1529 ao_data.samplerate, ao_data.channels,
1530 af_fmt2str_short(ao_data.format),
1531 af_fmt2bits(ao_data.format)/8 );
1532 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1533 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1534 if(strlen(mpctx->audio_out->info->comment) > 0)
1535 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1536 // init audio filters:
1537 #if 1
1538 current_module="af_init";
1539 if(!build_afilter_chain(mpctx->sh_audio, &ao_data)) {
1540 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1541 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1542 // uninit_player(INITED_ACODEC|INITED_AO); // close codec & ao
1543 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1545 #endif
1547 mpctx->mixer.audio_out = mpctx->audio_out;
1548 mpctx->mixer.volstep = volstep;
1553 ///@}
1554 // Command2Property
1557 // Return pts value corresponding to the end point of audio written to the
1558 // ao so far.
1559 static double written_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio)
1561 double buffered_output;
1562 // first calculate the end pts of audio that has been output by decoder
1563 double a_pts = sh_audio->pts;
1564 if (a_pts != MP_NOPTS_VALUE)
1565 // Good, decoder supports new way of calculating audio pts.
1566 // sh_audio->pts is the timestamp of the latest input packet with
1567 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1568 // the amount of bytes the decoder has written after that timestamp.
1569 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1570 else {
1571 // Decoder doesn't support new way of calculating pts (or we're
1572 // being called before it has decoded anything with known timestamp).
1573 // Use the old method of audio pts calculation: take the timestamp
1574 // of last packet with known pts the decoder has read data from,
1575 // and add amount of bytes read after the beginning of that packet
1576 // divided by input bps. This will be inaccurate if the input/output
1577 // ratio is not constant for every audio packet or if it is constant
1578 // but not accurately known in sh_audio->i_bps.
1580 a_pts = d_audio->pts;
1581 // ds_tell_pts returns bytes read after last timestamp from
1582 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1583 // it has read but not decoded
1584 if (sh_audio->i_bps)
1585 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1586 (double)sh_audio->i_bps;
1588 // Now a_pts hopefully holds the pts for end of audio from decoder.
1589 // Substract data in buffers between decoder and audio out.
1591 // Decoded but not filtered
1592 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1594 // Data buffered in audio filters, measured in bytes of "missing" output
1595 buffered_output = af_calc_delay(sh_audio->afilter);
1597 // Data that was ready for ao but was buffered because ao didn't fully
1598 // accept everything to internal buffers yet
1599 buffered_output += sh_audio->a_out_buffer_len;
1601 // Filters divide audio length by playback_speed, so multiply by it
1602 // to get the length in original units without speedup or slowdown
1603 a_pts -= buffered_output * playback_speed / ao_data.bps;
1605 return a_pts;
1608 // Return pts value corresponding to currently playing audio.
1609 double playing_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio,
1610 ao_functions_t *audio_out)
1612 return written_audio_pts(sh_audio, d_audio) - playback_speed *
1613 audio_out->get_delay();
1616 static int generate_video_frame(sh_video_t *sh_video, demux_stream_t *d_video)
1618 unsigned char *start;
1619 int in_size;
1620 int hit_eof=0;
1621 double pts;
1623 while (1) {
1624 void *decoded_frame;
1625 current_module = "decode video";
1626 // XXX Time used in this call is not counted in any performance
1627 // timer now, OSD is not updated correctly for filter-added frames
1628 if (vf_output_queued_frame(sh_video->vfilter))
1629 break;
1630 current_module = "video_read_frame";
1631 in_size = ds_get_packet_pts(d_video, &start, &pts);
1632 if (in_size < 0) {
1633 // try to extract last frames in case of decoder lag
1634 in_size = 0;
1635 pts = 1e300;
1636 hit_eof = 1;
1638 if (in_size > max_framesize)
1639 max_framesize = in_size;
1640 current_module = "decode video";
1641 decoded_frame = decode_video(sh_video, start, in_size, 0, pts);
1642 if (decoded_frame) {
1643 update_subtitles(sh_video, mpctx->d_sub, 0);
1644 update_teletext(sh_video, mpctx->demuxer, 0);
1645 update_osd_msg();
1646 current_module = "filter video";
1647 if (filter_video(sh_video, decoded_frame, sh_video->pts))
1648 break;
1650 if (hit_eof)
1651 return 0;
1653 return 1;
1656 #ifdef HAVE_RTC
1657 int rtc_fd = -1;
1658 #endif
1660 static float timing_sleep(float time_frame)
1662 #ifdef HAVE_RTC
1663 if (rtc_fd >= 0){
1664 // -------- RTC -----------
1665 current_module="sleep_rtc";
1666 while (time_frame > 0.000) {
1667 unsigned long rtc_ts;
1668 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1669 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1670 time_frame -= GetRelativeTime();
1672 } else
1673 #endif
1675 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1676 // unnecessarily high CPU usage
1677 float margin = softsleep ? 0.011 : 0;
1678 current_module = "sleep_timer";
1679 while (time_frame > margin) {
1680 usec_sleep(1000000 * (time_frame - margin));
1681 time_frame -= GetRelativeTime();
1683 if (softsleep){
1684 current_module = "sleep_soft";
1685 if (time_frame < 0)
1686 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1687 while (time_frame > 0)
1688 time_frame-=GetRelativeTime(); // burn the CPU
1691 return time_frame;
1694 static void adjust_sync_and_print_status(int between_frames, float timing_error)
1696 current_module="av_sync";
1698 if(mpctx->sh_audio){
1699 double a_pts, v_pts;
1701 if (autosync)
1703 * If autosync is enabled, the value for delay must be calculated
1704 * a bit differently. It is set only to the difference between
1705 * the audio and video timers. Any attempt to include the real
1706 * or corrected delay causes the pts_correction code below to
1707 * try to correct for the changes in delay which autosync is
1708 * trying to measure. This keeps the two from competing, but still
1709 * allows the code to correct for PTS drift *only*. (Using a delay
1710 * value here, even a "corrected" one, would be incompatible with
1711 * autosync mode.)
1713 a_pts = written_audio_pts(mpctx->sh_audio, mpctx->d_audio) - mpctx->delay;
1714 else
1715 a_pts = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
1717 v_pts = mpctx->sh_video->pts;
1720 static int drop_message=0;
1721 double AV_delay = a_pts - audio_delay - v_pts;
1722 double x;
1723 if (AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
1724 ++drop_message;
1725 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1727 if (autosync)
1728 x = AV_delay*0.1f;
1729 else
1730 /* Do not correct target time for the next frame if this frame
1731 * was late not because of wrong target time but because the
1732 * target time could not be met */
1733 x = (AV_delay + timing_error * playback_speed) * 0.1f;
1734 if (x < -max_pts_correction)
1735 x = -max_pts_correction;
1736 else if (x> max_pts_correction)
1737 x = max_pts_correction;
1738 if (default_max_pts_correction >= 0)
1739 max_pts_correction = default_max_pts_correction;
1740 else
1741 max_pts_correction = mpctx->sh_video->frametime*0.10; // +-10% of time
1742 if (!between_frames) {
1743 mpctx->delay+=x;
1744 c_total+=x;
1746 if(!quiet)
1747 print_status(a_pts - audio_delay, AV_delay, c_total);
1750 } else {
1751 // No audio:
1753 if (!quiet)
1754 print_status(0, 0, 0);
1758 static int fill_audio_out_buffers(void)
1760 unsigned int t;
1761 double tt;
1762 int playsize;
1763 int playflags=0;
1764 int audio_eof=0;
1765 int bytes_to_write;
1766 sh_audio_t * const sh_audio = mpctx->sh_audio;
1768 current_module="play_audio";
1770 while (1) {
1771 // all the current uses of ao_data.pts seem to be in aos that handle
1772 // sync completely wrong; there should be no need to use ao_data.pts
1773 // in get_space()
1774 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1775 bytes_to_write = mpctx->audio_out->get_space();
1776 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
1777 break;
1779 // handle audio-only case:
1780 // this is where mplayer sleeps during audio-only playback
1781 // to avoid 100% CPU use
1782 usec_sleep(10000); // Wait a tick before retry
1785 while (bytes_to_write) {
1786 playsize = bytes_to_write;
1787 if (playsize > MAX_OUTBURST)
1788 playsize = MAX_OUTBURST;
1789 bytes_to_write -= playsize;
1791 // Fill buffer if needed:
1792 current_module="decode_audio";
1793 t = GetTimer();
1794 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
1795 if (mpctx->d_audio->eof) {
1796 audio_eof = 1;
1797 if (sh_audio->a_out_buffer_len == 0)
1798 return 0;
1800 t = GetTimer() - t;
1801 tt = t*0.000001f; audio_time_usage+=tt;
1802 if (playsize > sh_audio->a_out_buffer_len) {
1803 playsize = sh_audio->a_out_buffer_len;
1804 if (audio_eof)
1805 playflags |= AOPLAY_FINAL_CHUNK;
1807 if (!playsize)
1808 break;
1810 // play audio:
1811 current_module="play_audio";
1813 // Is this pts value actually useful for the aos that access it?
1814 // They're obviously badly broken in the way they handle av sync;
1815 // would not having access to this make them more broken?
1816 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1817 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
1819 if (playsize > 0) {
1820 sh_audio->a_out_buffer_len -= playsize;
1821 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
1822 sh_audio->a_out_buffer_len);
1823 mpctx->delay += playback_speed*playsize/(double)ao_data.bps;
1825 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
1826 // Sanity check to avoid hanging in case current ao doesn't output
1827 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
1828 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
1829 sh_audio->a_out_buffer_len = 0;
1832 return 1;
1835 static int sleep_until_update(float *time_frame, float *aq_sleep_time)
1837 int frame_time_remaining = 0;
1838 current_module="calc_sleep_time";
1840 *time_frame -= GetRelativeTime(); // reset timer
1842 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1843 float delay = mpctx->audio_out->get_delay();
1844 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
1846 if (autosync) {
1848 * Adjust this raw delay value by calculating the expected
1849 * delay for this frame and generating a new value which is
1850 * weighted between the two. The higher autosync is, the
1851 * closer to the delay value gets to that which "-nosound"
1852 * would have used, and the longer it will take for A/V
1853 * sync to settle at the right value (but it eventually will.)
1854 * This settling time is very short for values below 100.
1856 float predicted = mpctx->delay / playback_speed + *time_frame;
1857 float difference = delay - predicted;
1858 delay = predicted + difference / (float)autosync;
1861 *time_frame = delay - mpctx->delay / playback_speed;
1863 // delay = amount of audio buffered in soundcard/driver
1864 if (delay > 0.25) delay=0.25; else
1865 if (delay < 0.10) delay=0.10;
1866 if (*time_frame > delay*0.6) {
1867 // sleep time too big - may cause audio drops (buffer underrun)
1868 frame_time_remaining = 1;
1869 *time_frame = delay*0.5;
1871 } else {
1872 // If we're lagging more than 200 ms behind the right playback rate,
1873 // don't try to "catch up".
1874 // If benchmark is set always output frames as fast as possible
1875 // without sleeping.
1876 if (*time_frame < -0.2 || benchmark)
1877 *time_frame = 0;
1880 *aq_sleep_time += *time_frame;
1883 //============================== SLEEP: ===================================
1885 // flag 256 means: libvo driver does its timing (dvb card)
1886 if (*time_frame > 0.001 && !(vo_flags&256))
1887 *time_frame = timing_sleep(*time_frame);
1888 return frame_time_remaining;
1891 int reinit_video_chain(void) {
1892 sh_video_t * const sh_video = mpctx->sh_video;
1893 double ar=-1.0;
1894 //================== Init VIDEO (codec & libvo) ==========================
1895 if(!fixed_vo || !(inited_flags&INITED_VO)){
1896 current_module="preinit_libvo";
1898 //shouldn't we set dvideo->id=-2 when we fail?
1899 vo_config_count=0;
1900 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
1901 if(!(mpctx->video_out=init_best_video_out(video_driver_list))){
1902 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
1903 goto err_out;
1905 inited_flags|=INITED_VO;
1908 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
1909 mpctx->sh_video->stream_aspect = ar;
1910 current_module="init_video_filters";
1912 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
1913 sh_video->vfilter=(void*)vf_open_filter(NULL,"vo",vf_arg);
1915 #ifdef HAVE_MENU
1916 if(use_menu) {
1917 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
1918 vf_menu = vf_open_plugin(libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
1919 if(!vf_menu) {
1920 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
1921 use_menu = 0;
1924 if(vf_menu)
1925 sh_video->vfilter=(void*)vf_menu;
1926 #endif
1928 #ifdef USE_ASS
1929 if(ass_enabled) {
1930 int i;
1931 int insert = 1;
1932 if (vf_settings)
1933 for (i = 0; vf_settings[i].name; ++i)
1934 if (strcmp(vf_settings[i].name, "ass") == 0) {
1935 insert = 0;
1936 break;
1938 if (insert) {
1939 extern vf_info_t vf_info_ass;
1940 vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
1941 char* vf_arg[] = {"auto", "1", NULL};
1942 vf_instance_t* vf_ass = vf_open_plugin(libass_vfs,sh_video->vfilter,"ass",vf_arg);
1943 if (vf_ass)
1944 sh_video->vfilter=(void*)vf_ass;
1945 else
1946 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
1949 #endif
1951 sh_video->vfilter=(void*)append_filters(sh_video->vfilter);
1953 #ifdef USE_ASS
1954 if (ass_enabled)
1955 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
1956 #endif
1958 current_module="init_video_codec";
1960 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1961 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
1962 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1964 if(!sh_video->inited){
1965 if(!fixed_vo) uninit_player(INITED_VO);
1966 goto err_out;
1969 inited_flags|=INITED_VCODEC;
1971 if (sh_video->codec)
1972 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
1974 sh_video->last_pts = MP_NOPTS_VALUE;
1975 sh_video->num_buffered_pts = 0;
1976 sh_video->next_frame_time = 0;
1978 if(auto_quality>0){
1979 // Auto quality option enabled
1980 output_quality=get_video_quality_max(sh_video);
1981 if(auto_quality>output_quality) auto_quality=output_quality;
1982 else output_quality=auto_quality;
1983 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
1984 set_video_quality(sh_video,output_quality);
1987 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
1989 current_module="init_vo";
1991 return 1;
1993 err_out:
1994 mpctx->sh_video = mpctx->d_video->sh = NULL;
1995 return 0;
1998 static double update_video(int *blit_frame)
2000 sh_video_t * const sh_video = mpctx->sh_video;
2001 //-------------------- Decode a frame: -----------------------
2002 double frame_time;
2003 *blit_frame = 0; // Don't blit if we hit EOF
2004 if (!correct_pts) {
2005 unsigned char* start=NULL;
2006 void *decoded_frame;
2007 int drop_frame=0;
2008 int in_size;
2010 current_module = "video_read_frame";
2011 frame_time = sh_video->next_frame_time;
2012 in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2013 &start, force_fps);
2014 if (in_size < 0)
2015 return -1;
2016 if (in_size > max_framesize)
2017 max_framesize = in_size; // stats
2018 sh_video->timer += frame_time;
2019 if (mpctx->sh_audio)
2020 mpctx->delay -= frame_time;
2021 // video_read_frame can change fps (e.g. for ASF video)
2022 vo_fps = sh_video->fps;
2023 // check for frame-drop:
2024 current_module = "check_framedrop";
2025 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2026 static int dropped_frames;
2027 float delay = playback_speed*mpctx->audio_out->get_delay();
2028 float d = delay-mpctx->delay;
2029 // we should avoid dropping too many frames in sequence unless we
2030 // are too late. and we allow 100ms A-V delay here:
2031 if (d < -dropped_frames*frame_time-0.100 &&
2032 mpctx->osd_function != OSD_PAUSE) {
2033 drop_frame = frame_dropping;
2034 ++drop_frame_cnt;
2035 ++dropped_frames;
2036 } else
2037 drop_frame = dropped_frames = 0;
2038 ++total_frame_cnt;
2040 update_subtitles(sh_video, mpctx->d_sub, 0);
2041 update_teletext(sh_video, mpctx->demuxer, 0);
2042 update_osd_msg();
2043 current_module = "decode_video";
2044 decoded_frame = decode_video(sh_video, start, in_size, drop_frame,
2045 sh_video->pts);
2046 current_module = "filter_video";
2047 *blit_frame = (decoded_frame && filter_video(sh_video, decoded_frame,
2048 sh_video->pts));
2050 else {
2051 if (!generate_video_frame(sh_video, mpctx->d_video))
2052 return -1;
2053 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter,
2054 VFCTRL_GET_PTS, &sh_video->pts);
2055 if (sh_video->pts == MP_NOPTS_VALUE) {
2056 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts after filters MISSING\n");
2057 sh_video->pts = sh_video->last_pts;
2059 if (sh_video->last_pts == MP_NOPTS_VALUE)
2060 sh_video->last_pts= sh_video->pts;
2061 else if (sh_video->last_pts >= sh_video->pts) {
2062 sh_video->last_pts = sh_video->pts;
2063 mp_msg(MSGT_CPLAYER, MSGL_INFO, "pts value <= previous\n");
2065 frame_time = sh_video->pts - sh_video->last_pts;
2066 sh_video->last_pts = sh_video->pts;
2067 sh_video->timer += frame_time;
2068 if(mpctx->sh_audio)
2069 mpctx->delay -= frame_time;
2070 *blit_frame = 1;
2072 return frame_time;
2075 void pause_loop(void)
2077 mp_cmd_t* cmd;
2078 if (!quiet) {
2079 // Small hack to display the pause message on the OSD line.
2080 // The pause string is: "\n == PAUSE == \r" so we need to
2081 // take the first and the last char out
2082 if (term_osd && !mpctx->sh_video) {
2083 char msg[128] = MSGTR_Paused;
2084 int mlen = strlen(msg);
2085 msg[mlen-1] = '\0';
2086 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2087 update_osd_msg();
2088 } else
2089 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2090 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2092 #ifdef HAVE_NEW_GUI
2093 if (use_gui)
2094 guiGetEvent(guiCEvent, (char *)guiSetPause);
2095 #endif
2096 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2097 mpctx->video_out->control(VOCTRL_PAUSE, NULL);
2099 if (mpctx->audio_out && mpctx->sh_audio)
2100 mpctx->audio_out->pause(); // pause audio, keep data if possible
2102 while ( (cmd = mp_input_get_cmd(20, 1, 1)) == NULL) {
2103 if (mpctx->sh_video && mpctx->video_out && vo_config_count)
2104 mpctx->video_out->check_events();
2105 #ifdef HAVE_NEW_GUI
2106 if (use_gui) {
2107 guiEventHandling();
2108 guiGetEvent(guiReDraw, NULL);
2109 if (guiIntfStruct.Playing!=2 || (rel_seek_secs || abs_seek_pos))
2110 break;
2112 #endif
2113 #ifdef HAVE_MENU
2114 if (vf_menu)
2115 vf_menu_pause_update(vf_menu);
2116 #endif
2117 usec_sleep(20000);
2119 if (cmd && cmd->id == MP_CMD_PAUSE) {
2120 cmd = mp_input_get_cmd(0,1,0);
2121 mp_cmd_free(cmd);
2123 mpctx->osd_function=OSD_PLAY;
2124 if (mpctx->audio_out && mpctx->sh_audio)
2125 mpctx->audio_out->resume(); // resume audio
2126 if (mpctx->video_out && mpctx->sh_video && vo_config_count)
2127 mpctx->video_out->control(VOCTRL_RESUME, NULL); // resume video
2128 (void)GetRelativeTime(); // ignore time that passed during pause
2129 #ifdef HAVE_NEW_GUI
2130 if (use_gui) {
2131 if (guiIntfStruct.Playing == guiSetStop)
2132 mpctx->eof = 1;
2133 else
2134 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2136 #endif
2139 void print_version(void){
2140 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s\n", MP_TITLE);
2142 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
2143 GetCpuCaps(&gCpuCaps);
2144 #ifdef ARCH_X86
2145 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
2146 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
2147 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
2148 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
2149 #ifdef RUNTIME_CPUDETECT
2150 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
2151 #else
2152 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
2153 #ifdef HAVE_MMX
2154 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
2155 #endif
2156 #ifdef HAVE_MMX2
2157 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
2158 #endif
2159 #ifdef HAVE_3DNOW
2160 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
2161 #endif
2162 #ifdef HAVE_3DNOWEX
2163 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
2164 #endif
2165 #ifdef HAVE_SSE
2166 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
2167 #endif
2168 #ifdef HAVE_SSE2
2169 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
2170 #endif
2171 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
2172 #endif /* RUNTIME_CPUDETECT */
2173 #endif /* ARCH_X86 */
2177 // Find the right mute status and record position for new file position
2178 static void edl_seek_reset(MPContext *mpctx)
2180 mpctx->edl_muted = 0;
2181 next_edl_record = edl_records;
2183 while (next_edl_record) {
2184 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2185 break;
2187 if (next_edl_record->action == EDL_MUTE)
2188 mpctx->edl_muted = !mpctx->edl_muted;
2189 next_edl_record = next_edl_record->next;
2191 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2192 mixer_mute(&mpctx->mixer);
2196 // Execute EDL command for the current position if one exists
2197 static void edl_update(MPContext *mpctx)
2199 if (!next_edl_record)
2200 return;
2202 if (!mpctx->sh_video) {
2203 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2204 free_edl(edl_records);
2205 next_edl_record = NULL;
2206 edl_records = NULL;
2207 return;
2210 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2211 if (next_edl_record->action == EDL_SKIP) {
2212 mpctx->osd_function = OSD_FFW;
2213 abs_seek_pos = 0;
2214 rel_seek_secs = next_edl_record->length_sec;
2215 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2216 "[%f], length [%f]\n", next_edl_record->start_sec,
2217 next_edl_record->stop_sec, next_edl_record->length_sec);
2218 edl_decision = 1;
2220 else if (next_edl_record->action == EDL_MUTE) {
2221 mpctx->edl_muted = !mpctx->edl_muted;
2222 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2223 mixer_mute(&mpctx->mixer);
2224 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2225 next_edl_record->start_sec );
2227 next_edl_record = next_edl_record->next;
2232 // style & 1 == 0 means seek relative to current position, == 1 means absolute
2233 // style & 2 == 0 means amount in seconds, == 2 means fraction of file length
2234 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2235 static int seek(MPContext *mpctx, double amount, int style)
2237 current_module = "seek";
2238 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2239 return -1;
2241 if (mpctx->sh_video) {
2242 current_module = "seek_video_reset";
2243 resync_video_stream(mpctx->sh_video);
2244 if (vo_config_count)
2245 mpctx->video_out->control(VOCTRL_RESET, NULL);
2246 mpctx->sh_video->num_buffered_pts = 0;
2247 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2248 mpctx->num_buffered_frames = 0;
2249 mpctx->delay = 0;
2250 // Not all demuxers set d_video->pts during seek, so this value
2251 // (which is used by at least vobsub and edl code below) may
2252 // be completely wrong (probably 0).
2253 mpctx->sh_video->pts = mpctx->d_video->pts;
2254 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2255 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2258 if (mpctx->sh_audio) {
2259 current_module = "seek_audio_reset";
2260 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2261 mpctx->sh_audio->a_buffer_len = 0;
2262 mpctx->sh_audio->a_out_buffer_len = 0;
2265 if (vo_vobsub) {
2266 current_module = "seek_vobsub_reset";
2267 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2270 edl_seek_reset(mpctx);
2272 c_total = 0;
2273 max_pts_correction = 0.1;
2274 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2275 drop_frame_cnt = 0;
2277 current_module = NULL;
2278 return 0;
2281 int main(int argc,char* argv[]){
2284 char * mem_ptr;
2286 // movie info:
2288 /* Flag indicating whether MPlayer should exit without playing anything. */
2289 int opt_exit = 0;
2291 //float a_frame=0; // Audio
2293 int i;
2295 int gui_no_filename=0;
2297 srand((int) time(NULL));
2299 InitTimer();
2301 mp_msg_init();
2303 for(i=1; i<argc; i++)
2304 if(!strcmp(argv[i], "-really-quiet"))
2305 verbose= -10;
2307 print_version();
2308 #if defined(WIN32) && defined(USE_WIN32DLL)
2309 set_path_env();
2310 #endif /*WIN32 && USE_WIN32DLL*/
2312 #ifdef USE_TV
2313 stream_tv_defaults.immediate = 1;
2314 #endif
2316 if (argc > 1 && argv[1] &&
2317 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2318 use_gui = !strcmp(argv[1], "-gui");
2319 } else
2320 if ( argv[0] )
2322 char *base = strrchr(argv[0], '/');
2323 if (!base)
2324 base = strrchr(argv[0], '\\');
2325 if (!base)
2326 base = argv[0];
2327 if(strstr(base, "gmplayer"))
2328 use_gui=1;
2331 mconfig = m_config_new();
2332 m_config_register_options(mconfig,mplayer_opts);
2333 // TODO : add something to let modules register their options
2334 mp_input_register_options(mconfig);
2335 parse_cfgfiles(mconfig);
2337 #ifdef HAVE_NEW_GUI
2338 if ( use_gui ) cfg_read();
2339 #endif
2341 mpctx->playtree = m_config_parse_mp_command_line(mconfig, argc, argv);
2342 if(mpctx->playtree == NULL)
2343 opt_exit = 1;
2344 else {
2345 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2346 if(mpctx->playtree) {
2347 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
2348 if(mpctx->playtree_iter) {
2349 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2350 play_tree_iter_free(mpctx->playtree_iter);
2351 mpctx->playtree_iter = NULL;
2353 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2358 #if defined(WIN32) && defined(HAVE_NEW_GUI)
2359 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2360 if(runningmplayer && filename && use_gui){
2361 COPYDATASTRUCT csData;
2362 char file[MAX_PATH];
2363 char *filepart = filename;
2364 if(GetFullPathName(filename, MAX_PATH, file, &filepart)){
2365 csData.dwData = 0;
2366 csData.cbData = strlen(file)*2;
2367 csData.lpData = file;
2368 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2371 #endif
2373 #ifdef WIN32
2374 if(proc_priority){
2375 int i;
2376 for(i=0; priority_presets_defs[i].name; i++){
2377 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
2378 break;
2380 mp_msg(MSGT_CPLAYER,MSGL_STATUS,"Setting process priority: %s\n",
2381 priority_presets_defs[i].name);
2382 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
2384 #endif
2385 #ifndef HAVE_NEW_GUI
2386 if(use_gui){
2387 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2388 use_gui=0;
2390 #else
2391 #ifndef WIN32
2392 if(use_gui && !vo_init()){
2393 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2394 use_gui=0;
2396 #endif
2397 if (use_gui && mpctx->playtree_iter){
2398 char cwd[PATH_MAX+2];
2399 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2400 play_tree_iter_free(mpctx->playtree_iter);
2401 mpctx->playtree_iter=NULL;
2403 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2405 strcat(cwd, "/");
2406 // Prefix relative paths with current working directory
2407 play_tree_add_bpf(mpctx->playtree, cwd);
2409 // Import initital playtree into GUI.
2410 import_initial_playtree_into_gui(mpctx->playtree, mconfig, enqueue);
2412 #endif /* HAVE_NEW_GUI */
2414 if(video_driver_list && strcmp(video_driver_list[0],"help")==0){
2415 list_video_out();
2416 opt_exit = 1;
2419 if(audio_driver_list && strcmp(audio_driver_list[0],"help")==0){
2420 list_audio_out();
2421 opt_exit = 1;
2424 /* Check codecs.conf. */
2425 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2426 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2427 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2428 if(!parse_codec_cfg(NULL)){
2429 exit_player_with_rc(NULL, 0);
2431 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2434 free( mem_ptr ); // release the buffer created by get_path()
2437 #if 0
2438 if(video_codec_list){
2439 int i;
2440 video_codec=video_codec_list[0];
2441 for(i=0;video_codec_list[i];i++)
2442 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2444 #endif
2445 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2446 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2447 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2448 list_codecs(1);
2449 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2450 opt_exit = 1;
2452 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2453 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2454 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2455 list_codecs(0);
2456 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2457 opt_exit = 1;
2459 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2460 vfm_help();
2461 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2462 opt_exit = 1;
2464 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2465 afm_help();
2466 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2467 opt_exit = 1;
2469 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2470 af_help();
2471 printf("\n");
2472 opt_exit = 1;
2474 #ifdef HAVE_X11
2475 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2476 fstype_help();
2477 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2478 opt_exit = 1;
2480 #endif
2481 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2482 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2483 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2484 demuxer_help();
2485 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2486 opt_exit = 1;
2488 if(list_properties) {
2489 property_print_help();
2490 opt_exit = 1;
2493 if(opt_exit)
2494 exit_player(NULL);
2496 if (player_idle_mode && use_gui) {
2497 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2498 exit_player_with_rc(NULL, 1);
2501 if(!filename && !player_idle_mode){
2502 if(!use_gui){
2503 // no file/vcd/dvd -> show HELP:
2504 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2505 exit_player_with_rc(NULL, 0);
2506 } else gui_no_filename=1;
2509 /* Display what configure line was used */
2510 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2512 // Many users forget to include command line in bugreports...
2513 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2514 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2515 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2516 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2519 //------ load global data first ------
2521 // check font
2522 #ifdef HAVE_FREETYPE
2523 init_freetype();
2524 #endif
2525 #ifdef HAVE_FONTCONFIG
2526 if(!font_fontconfig)
2528 #endif
2529 #ifdef HAVE_BITMAP_FONT
2530 if(font_name){
2531 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2532 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2533 filename_recode(font_name));
2534 } else {
2535 // try default:
2536 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2537 free(mem_ptr); // release the buffer created by get_path()
2538 if(!vo_font)
2539 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2541 if (sub_font_name)
2542 sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2543 else
2544 sub_font = vo_font;
2545 #endif
2546 #ifdef HAVE_FONTCONFIG
2548 #endif
2550 vo_init_osd();
2552 #ifdef USE_ASS
2553 ass_library = ass_init();
2554 #endif
2556 #ifdef HAVE_RTC
2557 if(!nortc)
2559 // seteuid(0); /* Can't hurt to try to get root here */
2560 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2561 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2562 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2563 else {
2564 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2566 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2567 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2568 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2569 close (rtc_fd);
2570 rtc_fd = -1;
2571 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2572 /* variable only by the root */
2573 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2574 close (rtc_fd);
2575 rtc_fd = -1;
2576 } else
2577 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2580 #ifdef HAVE_NEW_GUI
2581 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2582 // and now ? -- Pontscho
2583 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2584 #endif
2585 if(rtc_fd<0)
2586 #endif /* HAVE_RTC */
2587 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2588 softsleep?"software":timer_name);
2590 #ifdef USE_TERMCAP
2591 if ( !use_gui ) load_termcap(NULL); // load key-codes
2592 #endif
2594 // ========== Init keyboard FIFO (connection to libvo) ============
2596 // Init input system
2597 current_module = "init_input";
2598 mp_input_init(use_gui);
2599 mp_input_add_key_fd(-1,0,mplayer_get_key,NULL);
2600 if(slave_mode)
2601 #ifndef __MINGW32__
2602 mp_input_add_cmd_fd(0,1,NULL,NULL);
2603 #else
2604 mp_input_add_cmd_fd(0,0,mp_input_win32_slave_cmd_func,NULL);
2605 #endif
2606 else if(!noconsolecontrols)
2607 mp_input_add_event_fd(0, getch2);
2609 inited_flags|=INITED_INPUT;
2610 current_module = NULL;
2612 #ifdef HAVE_MENU
2613 if(use_menu) {
2614 if(menu_cfg && menu_init(mpctx, menu_cfg))
2615 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2616 else {
2617 menu_cfg = get_path("menu.conf");
2618 if(menu_init(mpctx, menu_cfg))
2619 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2620 else {
2621 if(menu_init(mpctx, MPLAYER_CONFDIR "/menu.conf"))
2622 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2623 else {
2624 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2625 use_menu = 0;
2630 #endif
2632 /// Catch signals
2633 #ifndef __MINGW32__
2634 signal(SIGCHLD,child_sighandler);
2635 #endif
2637 #ifdef CRASH_DEBUG
2638 prog_path = argv[0];
2639 #endif
2640 //========= Catch terminate signals: ================
2641 // terminate requests:
2642 signal(SIGTERM,exit_sighandler); // kill
2643 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2645 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2647 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2648 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2649 #ifdef ENABLE_SIGHANDLER
2650 // fatal errors:
2651 signal(SIGBUS,exit_sighandler); // bus error
2652 signal(SIGSEGV,exit_sighandler); // segfault
2653 signal(SIGILL,exit_sighandler); // illegal instruction
2654 signal(SIGFPE,exit_sighandler); // floating point exc.
2655 signal(SIGABRT,exit_sighandler); // abort()
2656 #ifdef CRASH_DEBUG
2657 if (crash_debug)
2658 signal(SIGTRAP,exit_sighandler);
2659 #endif
2660 #endif
2662 #ifdef HAVE_NEW_GUI
2663 if(use_gui){
2664 guiInit();
2665 guiGetEvent(guiSetContext, mpctx);
2666 inited_flags|=INITED_GUI;
2667 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2669 #endif
2671 // ******************* Now, let's see the per-file stuff ********************
2673 play_next_file:
2675 // init global sub numbers
2676 mpctx->global_sub_size = 0;
2677 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2679 if (filename) load_per_file_config (mconfig, filename);
2681 // We must enable getch2 here to be able to interrupt network connection
2682 // or cache filling
2683 if(!noconsolecontrols && !slave_mode){
2684 if(inited_flags&INITED_GETCH2)
2685 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2686 else
2687 getch2_enable(); // prepare stdin for hotkeys...
2688 inited_flags|=INITED_GETCH2;
2689 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2692 // =================== GUI idle loop (STOP state) ===========================
2693 #ifdef HAVE_NEW_GUI
2694 if ( use_gui ) {
2695 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
2696 guiGetEvent( guiSetDefaults,0 );
2697 while ( guiIntfStruct.Playing != 1 )
2699 mp_cmd_t* cmd;
2700 usec_sleep(20000);
2701 guiEventHandling();
2702 guiGetEvent( guiReDraw,NULL );
2703 if ( (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
2704 guiGetEvent(guiIEvent, (char *)cmd->id);
2705 mp_cmd_free(cmd);
2708 guiGetEvent( guiSetParameters,NULL );
2709 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
2711 play_tree_t * entry = play_tree_new();
2712 play_tree_add_file( entry,guiIntfStruct.Filename );
2713 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
2714 else mpctx->playtree=play_tree_new();
2715 play_tree_set_child( mpctx->playtree,entry );
2716 if(mpctx->playtree)
2718 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mconfig);
2719 if(mpctx->playtree_iter)
2721 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
2723 play_tree_iter_free(mpctx->playtree_iter);
2724 mpctx->playtree_iter = NULL;
2726 filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2731 #endif /* HAVE_NEW_GUI */
2733 while (player_idle_mode && !filename) {
2734 play_tree_t * entry = NULL;
2735 mp_cmd_t * cmd;
2736 while (!(cmd = mp_input_get_cmd(0,1,0))) { // wait for command
2737 if (mpctx->video_out && vo_config_count) mpctx->video_out->check_events();
2738 usec_sleep(20000);
2740 switch (cmd->id) {
2741 case MP_CMD_LOADFILE:
2742 // prepare a tree entry with the new filename
2743 entry = play_tree_new();
2744 play_tree_add_file(entry, cmd->args[0].v.s);
2745 // The entry is added to the main playtree after the switch().
2746 break;
2747 case MP_CMD_LOADLIST:
2748 entry = parse_playlist_file(cmd->args[0].v.s);
2749 break;
2750 case MP_CMD_QUIT:
2751 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
2752 break;
2755 mp_cmd_free(cmd);
2757 if (entry) { // user entered a command that gave a valid entry
2758 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
2759 play_tree_free_list(mpctx->playtree->child, 1);
2760 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
2762 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
2764 play_tree_set_child(mpctx->playtree, entry);
2766 /* Make iterator start at the top the of tree. */
2767 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mconfig);
2768 if (!mpctx->playtree_iter) continue;
2770 // find the first real item in the tree
2771 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2772 // no items!
2773 play_tree_iter_free(mpctx->playtree_iter);
2774 mpctx->playtree_iter = NULL;
2775 continue; // wait for next command
2777 filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
2780 //---------------------------------------------------------------------------
2782 if(filename)
2783 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
2784 filename_recode(filename));
2786 if (edl_filename) {
2787 if (edl_records) free_edl(edl_records);
2788 next_edl_record = edl_records = edl_parse_file();
2790 if (edl_output_filename) {
2791 if (edl_fd) fclose(edl_fd);
2792 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
2794 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
2795 filename_recode(edl_output_filename));
2799 //==================== Open VOB-Sub ============================
2801 current_module="vobsub";
2802 if (vobsub_name){
2803 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
2804 if(vo_vobsub==NULL)
2805 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
2806 filename_recode(vobsub_name));
2807 }else if(sub_auto && filename && (strlen(filename)>=5)){
2808 /* try to autodetect vobsub from movie filename ::atmos */
2809 char *buf = malloc((strlen(filename)-3)),*psub;
2810 memset(buf,0,strlen(filename)-3); // make sure string is terminated
2811 strncpy(buf, filename, strlen(filename)-4);
2812 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
2813 /* try from ~/.mplayer/sub */
2814 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
2815 char *bname;
2816 int l;
2817 bname = strrchr(buf,'/');
2818 #ifdef WIN32
2819 if(!bname) bname = strrchr(buf,'\\');
2820 #endif
2821 if(bname) bname++;
2822 else bname = buf;
2823 l = strlen(psub) + strlen(bname) + 1;
2824 psub = realloc(psub,l);
2825 strcat(psub,bname);
2826 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
2827 free(psub);
2829 free(buf);
2831 if(vo_vobsub){
2832 inited_flags|=INITED_VOBSUB;
2833 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
2834 // check if vobsub requested only to display forced subtitles
2835 forced_subs_only=vobsub_get_forced_subs_flag(vo_vobsub);
2837 // setup global sub numbering
2838 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
2839 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
2842 //============ Open & Sync STREAM --- fork cache2 ====================
2844 mpctx->stream=NULL;
2845 mpctx->demuxer=NULL;
2846 if (mpctx->d_audio) {
2847 //free_demuxer_stream(mpctx->d_audio);
2848 mpctx->d_audio=NULL;
2850 if (mpctx->d_video) {
2851 //free_demuxer_stream(d_video);
2852 mpctx->d_video=NULL;
2854 mpctx->sh_audio=NULL;
2855 mpctx->sh_video=NULL;
2857 current_module="open_stream";
2858 mpctx->stream=open_stream(filename,0,&mpctx->file_format);
2859 if(!mpctx->stream) { // error...
2860 mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY);
2861 goto goto_next_file;
2863 inited_flags|=INITED_STREAM;
2865 #ifdef HAVE_NEW_GUI
2866 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
2867 #endif
2869 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
2870 play_tree_t* entry;
2871 // Handle playlist
2872 current_module="handle_playlist";
2873 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
2874 filename_recode(filename));
2875 entry = parse_playtree(mpctx->stream,0);
2876 mpctx->eof=playtree_add_playlist(entry);
2877 goto goto_next_file;
2879 mpctx->stream->start_pos+=seek_to_byte;
2881 if(stream_dump_type==5){
2882 unsigned char buf[4096];
2883 int len;
2884 FILE *f;
2885 current_module="dumpstream";
2886 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
2887 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
2888 exit_player(MSGTR_Exit_error);
2890 stream_reset(mpctx->stream);
2891 stream_seek(mpctx->stream,mpctx->stream->start_pos);
2892 f=fopen(stream_dump_name,"wb");
2893 if(!f){
2894 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
2895 exit_player(MSGTR_Exit_error);
2897 while(!mpctx->stream->eof){
2898 len=stream_read(mpctx->stream,buf,4096);
2899 if(len>0) {
2900 if(fwrite(buf,len,1,f) != 1) {
2901 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
2902 exit_player(MSGTR_Exit_error);
2906 if(fclose(f)) {
2907 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
2908 exit_player(MSGTR_Exit_error);
2910 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
2911 exit_player_with_rc(MSGTR_Exit_eof, 0);
2914 #ifdef USE_DVDREAD
2915 if(mpctx->stream->type==STREAMTYPE_DVD){
2916 current_module="dvd lang->id";
2917 if(audio_id==-1) audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
2918 if(dvdsub_lang && dvdsub_id==-2) dvdsub_id=-1;
2919 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
2920 // setup global sub numbering
2921 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
2922 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
2923 current_module=NULL;
2925 #endif
2927 #ifdef USE_DVDNAV
2928 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
2929 current_module="dvdnav lang->id";
2930 if(audio_id==-1) audio_id=dvdnav_aid_from_lang(mpctx->stream,audio_lang);
2931 if(dvdsub_lang && dvdsub_id==-2) dvdsub_id=-1;
2932 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
2933 // setup global sub numbering
2934 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
2935 mpctx->global_sub_size += dvdnav_number_of_subs(mpctx->stream);
2936 current_module=NULL;
2938 #endif
2940 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
2941 goto_enable_cache:
2942 if(stream_cache_size>0){
2943 current_module="enable_cache";
2944 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
2945 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
2946 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
2947 if((mpctx->eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY))) goto goto_next_file;
2950 //============ Open DEMUXERS --- DETECT file type =======================
2951 current_module="demux_open";
2953 mpctx->demuxer=demux_open(mpctx->stream,mpctx->file_format,audio_id,video_id,dvdsub_id,filename);
2955 // HACK to get MOV Reference Files working
2957 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
2959 unsigned char* playlist_entry;
2960 play_tree_t *list = NULL, *entry = NULL;
2962 current_module="handle_demux_playlist";
2963 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
2965 char *temp, *bname;
2967 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
2968 filename_recode(playlist_entry));
2970 bname=mp_basename(playlist_entry);
2971 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
2972 continue;
2974 if (!strncmp(bname,mp_basename(filename),strlen(bname))) // ignoring self-reference
2975 continue;
2977 entry = play_tree_new();
2979 if (filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
2981 temp=malloc((strlen(filename)-strlen(mp_basename(filename))+strlen(playlist_entry)+1));
2982 if (temp)
2984 strncpy(temp, filename, strlen(filename)-strlen(mp_basename(filename)));
2985 temp[strlen(filename)-strlen(mp_basename(filename))]='\0';
2986 strcat(temp, playlist_entry);
2987 play_tree_add_file(entry,temp);
2988 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
2989 free(temp);
2992 else
2993 play_tree_add_file(entry,playlist_entry);
2995 if(!list)
2996 list = entry;
2997 else
2998 play_tree_append_entry(list,entry);
3000 free_demuxer(mpctx->demuxer);
3001 mpctx->demuxer = NULL;
3003 if (list)
3005 entry = play_tree_new();
3006 play_tree_set_child(entry,list);
3007 mpctx->eof=playtree_add_playlist(entry);
3008 goto goto_next_file;
3012 if(!mpctx->demuxer)
3013 goto goto_next_file;
3014 inited_flags|=INITED_DEMUXER;
3016 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3017 int i;
3018 int maxid = -1;
3019 // setup global sub numbering
3020 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3021 for (i = 0; i < MAX_S_STREAMS; i++)
3022 if (mpctx->demuxer->s_streams[i])
3023 maxid = FFMAX(maxid, ((sh_sub_t *)mpctx->demuxer->s_streams[i])->sid);
3024 mpctx->global_sub_size += maxid + 1;
3026 // Make dvdsub_id always selectable if set.
3027 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id)
3028 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id + 1;
3030 current_module="demux_open2";
3032 //file_format=demuxer->file_format;
3034 mpctx->d_audio=mpctx->demuxer->audio;
3035 mpctx->d_video=mpctx->demuxer->video;
3036 mpctx->d_sub=mpctx->demuxer->sub;
3038 // DUMP STREAMS:
3039 if((stream_dump_type)&&(stream_dump_type<4)){
3040 FILE *f;
3041 demux_stream_t *ds=NULL;
3042 current_module="dump";
3043 // select stream to dump
3044 switch(stream_dump_type){
3045 case 1: ds=mpctx->d_audio;break;
3046 case 2: ds=mpctx->d_video;break;
3047 case 3: ds=mpctx->d_sub;break;
3049 if(!ds){
3050 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3051 exit_player(MSGTR_Exit_error);
3053 // disable other streams:
3054 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3055 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3056 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3057 // let's dump it!
3058 f=fopen(stream_dump_name,"wb");
3059 if(!f){
3060 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3061 exit_player(MSGTR_Exit_error);
3063 while(!ds->eof){
3064 unsigned char* start;
3065 int in_size=ds_get_packet(ds,&start);
3066 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3067 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3068 if(in_size>0) fwrite(start,in_size,1,f);
3070 fclose(f);
3071 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3072 exit_player_with_rc(MSGTR_Exit_eof, 0);
3075 mpctx->sh_audio=mpctx->d_audio->sh;
3076 mpctx->sh_video=mpctx->d_video->sh;
3078 if(mpctx->sh_video){
3080 current_module="video_read_properties";
3081 if(!video_read_properties(mpctx->sh_video)) {
3082 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3083 mpctx->sh_video=mpctx->d_video->sh=NULL;
3084 } else {
3085 mp_msg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
3086 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3087 mpctx->sh_video->fps,mpctx->sh_video->frametime
3090 /* need to set fps here for output encoders to pick it up in their init */
3091 if(force_fps){
3092 mpctx->sh_video->fps=force_fps;
3093 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3095 vo_fps = mpctx->sh_video->fps;
3097 if(!mpctx->sh_video->fps && !force_fps){
3098 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3099 mpctx->sh_video=mpctx->d_video->sh=NULL;
3105 if(!mpctx->sh_video && !mpctx->sh_audio){
3106 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3107 #ifdef HAS_DVBIN_SUPPORT
3108 if((mpctx->stream->type == STREAMTYPE_DVB) && mpctx->stream->priv)
3110 dvb_priv_t *priv = (dvb_priv_t*) mpctx->stream->priv;
3111 if(priv->is_on)
3113 int dir;
3114 int v = mpctx->last_dvb_step;
3115 if(v > 0)
3116 dir = DVB_CHANNEL_HIGHER;
3117 else
3118 dir = DVB_CHANNEL_LOWER;
3120 if(dvb_step_channel(priv, dir))
3121 mpctx->eof = mpctx->dvbin_reopen = 1;
3124 #endif
3125 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3128 /* display clip info */
3129 demux_info_print(mpctx->demuxer);
3131 //================== Read SUBTITLES (DVD & TEXT) ==========================
3132 if(vo_spudec==NULL && mpctx->sh_video &&
3133 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV || mpctx->d_sub->id >= 0)){
3134 init_vo_spudec();
3137 // Apply current settings for forced subs
3138 if (vo_spudec!=NULL)
3139 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
3141 if(mpctx->sh_video) {
3142 // after reading video params we should load subtitles because
3143 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3144 // check .sub
3145 current_module="read_subtitles_file";
3146 if(sub_name){
3147 for (i = 0; sub_name[i] != NULL; ++i)
3148 add_subtitles (sub_name[i], mpctx->sh_video->fps, 0);
3150 if(sub_auto) { // auto load sub file ...
3151 char *psub = get_path( "sub/" );
3152 char **tmp = sub_filenames((psub ? psub : ""), filename);
3153 int i = 0;
3154 free(psub); // release the buffer created by get_path() above
3155 while (tmp[i]) {
3156 add_subtitles (tmp[i], mpctx->sh_video->fps, 0);
3157 free(tmp[i++]);
3159 free(tmp);
3161 if (mpctx->set_of_sub_size > 0) {
3162 // setup global sub numbering
3163 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3164 mpctx->global_sub_size += mpctx->set_of_sub_size;
3168 if (mpctx->global_sub_size) {
3169 // find the best sub to use
3170 if (vobsub_id >= 0) {
3171 // if user asks for a vobsub id, use that first.
3172 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_id;
3173 } else if (dvdsub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3174 // if user asks for a dvd sub id, use that next.
3175 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id;
3176 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3177 // if there are text subs to use, use those. (autosubs come last here)
3178 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3180 } else if (mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3181 // if nothing else works, get subs from the demuxer.
3182 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX];
3184 } else {
3185 // nothing worth doing automatically.
3186 mpctx->global_sub_pos = -1;
3188 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3189 mpctx->global_sub_pos--;
3190 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3191 if(subdata)
3192 switch (stream_dump_type) {
3193 case 3: list_sub_file(subdata); break;
3194 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3195 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3196 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3197 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3198 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3202 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3203 filename_recode(filename));
3204 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3205 if (mpctx->sh_video) {
3206 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3207 if (mpctx->sh_video->format >= 0x20202020)
3208 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3209 else
3210 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3211 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3212 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3213 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3214 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3215 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3217 if (mpctx->sh_audio) {
3218 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3219 if (mpctx->sh_audio->format >= 0x20202020)
3220 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3221 else
3222 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3223 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3224 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3225 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3227 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3229 if(!mpctx->sh_video) goto main; // audio-only
3231 if(!reinit_video_chain()) {
3232 if(!mpctx->sh_video){
3233 if(!mpctx->sh_audio) goto goto_next_file;
3234 goto main; // exit_player(MSGTR_Exit_error);
3238 if(vo_flags & 0x08 && vo_spudec)
3239 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3241 #ifdef HAVE_FREETYPE
3242 force_load_font = 1;
3243 #endif
3245 //================== MAIN: ==========================
3246 main:
3247 current_module="main";
3249 if(playing_msg) {
3250 char* msg = property_expand_string(mpctx, playing_msg);
3251 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3252 free(msg);
3256 // Disable the term OSD in verbose mode
3257 if(verbose) term_osd = 0;
3260 //int frame_corr_num=0; //
3261 //float v_frame=0; // Video
3262 float time_frame=0; // Timer
3263 //float num_frames=0; // number of frames played
3265 int frame_time_remaining=0; // flag
3266 int blit_frame=0;
3267 mpctx->num_buffered_frames=0;
3269 // Make sure old OSD does not stay around,
3270 // e.g. with -fixed-vo and same-resolution files
3271 clear_osd_msgs();
3272 update_osd_msg();
3274 //================ SETUP AUDIO ==========================
3276 if(mpctx->sh_audio){
3277 reinit_audio_chain();
3278 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3279 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3282 current_module="av_init";
3284 if(mpctx->sh_video){
3285 mpctx->sh_video->timer=0;
3286 if (! ignore_start)
3287 audio_delay += mpctx->sh_video->stream_delay;
3289 if(mpctx->sh_audio){
3290 if (! ignore_start)
3291 audio_delay -= mpctx->sh_audio->stream_delay;
3292 mpctx->delay=-audio_delay;
3295 if(!mpctx->sh_audio){
3296 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3297 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3298 ds_free_packs(mpctx->d_audio); // free buffered chunks
3299 //mpctx->d_audio->id=-2; // do not read audio chunks
3300 //uninit_player(INITED_AO); // close device
3302 if(!mpctx->sh_video){
3303 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3304 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3305 ds_free_packs(mpctx->d_video);
3306 mpctx->d_video->id=-2;
3307 //if(!fixed_vo) uninit_player(INITED_VO);
3310 if (!mpctx->sh_video && !mpctx->sh_audio)
3311 goto goto_next_file;
3313 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3314 if(force_fps && mpctx->sh_video){
3315 vo_fps = mpctx->sh_video->fps=force_fps;
3316 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3317 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3320 #ifdef HAVE_NEW_GUI
3321 if ( use_gui ) {
3322 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3323 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3324 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3325 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3326 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3328 #endif
3330 mp_input_set_section(NULL);
3331 //TODO: add desired (stream-based) sections here
3332 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section("tv");
3334 //==================== START PLAYING =======================
3336 if(mpctx->loop_times>1) mpctx->loop_times--; else
3337 if(mpctx->loop_times==1) mpctx->loop_times = -1;
3339 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3341 total_time_usage_start=GetTimer();
3342 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3343 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3344 play_n_frames=play_n_frames_mf;
3346 if(play_n_frames==0){
3347 mpctx->eof=PT_NEXT_ENTRY; goto goto_next_file;
3350 if (seek_to_sec) {
3351 seek(mpctx, seek_to_sec, 1);
3352 end_at.pos += seek_to_sec;
3355 if (end_at.type == END_AT_SIZE) {
3356 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3357 end_at.type = END_AT_NONE;
3361 while(!mpctx->eof){
3362 float aq_sleep_time=0;
3363 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3364 mpctx->sh_audio = mpctx->d_audio->sh;
3365 mpctx->sh_audio->ds = mpctx->d_audio;
3366 reinit_audio_chain();
3369 /*========================== PLAY AUDIO ============================*/
3371 if (mpctx->sh_audio)
3372 if (!fill_audio_out_buffers())
3373 // at eof, all audio at least written to ao
3374 if (!mpctx->sh_video)
3375 mpctx->eof = PT_NEXT_ENTRY;
3378 if(!mpctx->sh_video) {
3379 // handle audio-only case:
3380 double a_pos=0;
3381 if(!quiet || end_at.type == END_AT_TIME )
3382 a_pos = playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3384 if(!quiet)
3385 print_status(a_pos, 0, 0);
3387 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3388 mpctx->eof = PT_NEXT_ENTRY;
3389 update_osd_msg();
3391 } else {
3393 /*========================== PLAY VIDEO ============================*/
3395 vo_pts=mpctx->sh_video->timer*90000.0;
3396 vo_fps=mpctx->sh_video->fps;
3398 if (!mpctx->num_buffered_frames) {
3399 double frame_time = update_video(&blit_frame);
3400 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3401 if (mpctx->sh_video->vf_inited < 0) {
3402 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3403 mpctx->eof = 1; goto goto_next_file;
3405 if (frame_time < 0)
3406 mpctx->eof = 1;
3407 else {
3408 // might return with !eof && !blit_frame if !correct_pts
3409 mpctx->num_buffered_frames += blit_frame;
3410 time_frame += frame_time / playback_speed; // for nosound
3414 // ==========================================================================
3416 // current_module="draw_osd";
3417 // if(vo_config_count) mpctx->video_out->draw_osd();
3419 #ifdef HAVE_NEW_GUI
3420 if(use_gui) guiEventHandling();
3421 #endif
3423 current_module="vo_check_events";
3424 if (vo_config_count) mpctx->video_out->check_events();
3426 #ifdef HAVE_X11
3427 if (stop_xscreensaver) {
3428 current_module = "stop_xscreensaver";
3429 xscreensaver_heartbeat();
3431 #endif
3433 frame_time_remaining = sleep_until_update(&time_frame, &aq_sleep_time);
3435 //====================== FLIP PAGE (VIDEO BLT): =========================
3437 current_module="flip_page";
3438 if (!frame_time_remaining && blit_frame) {
3439 unsigned int t2=GetTimer();
3441 if(vo_config_count) mpctx->video_out->flip_page();
3442 mpctx->num_buffered_frames--;
3444 vout_time_usage += (GetTimer() - t2) * 0.000001;
3446 //====================== A-V TIMESTAMP CORRECTION: =========================
3448 adjust_sync_and_print_status(frame_time_remaining, time_frame);
3450 //============================ Auto QUALITY ============================
3452 /*Output quality adjustments:*/
3453 if(auto_quality>0){
3454 current_module="autoq";
3455 // float total=0.000001f * (GetTimer()-aq_total_time);
3456 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3457 if(output_quality<auto_quality && aq_sleep_time>0)
3458 ++output_quality;
3459 else
3460 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3461 if(output_quality>1 && aq_sleep_time<0)
3462 --output_quality;
3463 else
3464 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3465 output_quality=0;
3466 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3467 set_video_quality(mpctx->sh_video,output_quality);
3470 if (play_n_frames >= 0 && !frame_time_remaining && blit_frame) {
3471 --play_n_frames;
3472 if (play_n_frames <= 0) mpctx->eof = PT_NEXT_ENTRY;
3476 // FIXME: add size based support for -endpos
3477 if (end_at.type == END_AT_TIME &&
3478 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3479 mpctx->eof = PT_NEXT_ENTRY;
3481 } // end if(mpctx->sh_video)
3483 #ifdef USE_DVDNAV
3484 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3485 nav_highlight_t hl;
3486 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3487 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3488 vo_osd_changed (OSDTYPE_DVDNAV);
3490 #endif
3492 //============================ Handle PAUSE ===============================
3494 current_module="pause";
3496 if (mpctx->osd_function == OSD_PAUSE) {
3497 pause_loop();
3498 mpctx->was_paused = 1;
3501 // handle -sstep
3502 if(step_sec>0) {
3503 mpctx->osd_function=OSD_FFW;
3504 rel_seek_secs+=step_sec;
3507 edl_update(mpctx);
3509 //================= Keyboard events, SEEKing ====================
3511 current_module="key_events";
3514 mp_cmd_t* cmd;
3515 int brk_cmd = 0;
3516 while( !brk_cmd && (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
3517 brk_cmd = run_command(mpctx, cmd);
3518 mp_cmd_free(cmd);
3519 if (brk_cmd == 2)
3520 goto goto_enable_cache;
3523 mpctx->was_paused = 0;
3525 /* Looping. */
3526 if(mpctx->eof==1 && mpctx->loop_times>=0) {
3527 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d, eof = %d\n", mpctx->loop_times,mpctx->eof);
3529 if(mpctx->loop_times>1) mpctx->loop_times--; else
3530 if(mpctx->loop_times==1) mpctx->loop_times=-1;
3531 play_n_frames=play_n_frames_mf;
3532 mpctx->eof=0;
3533 abs_seek_pos=1; rel_seek_secs=seek_to_sec;
3534 loop_seek = 1;
3537 if(rel_seek_secs || abs_seek_pos){
3538 if (seek(mpctx, rel_seek_secs, abs_seek_pos) >= 0) {
3539 // Set OSD:
3540 if(!loop_seek){
3541 if( !edl_decision )
3542 set_osd_bar(0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3546 rel_seek_secs=0;
3547 abs_seek_pos=0;
3548 loop_seek=0;
3549 edl_decision = 0;
3552 #ifdef HAVE_NEW_GUI
3553 if(use_gui){
3554 guiEventHandling();
3555 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3556 // get pos from frame number / total frames
3557 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3558 } else {
3559 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3561 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3562 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx->sh_audio, mpctx->d_audio, mpctx->audio_out);
3563 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3564 guiGetEvent( guiReDraw,NULL );
3565 guiGetEvent( guiSetVolume,NULL );
3566 if(guiIntfStruct.Playing==0) break; // STOP
3567 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3568 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3569 #ifdef USE_DVDREAD
3570 if ( mpctx->stream->type == STREAMTYPE_DVD )
3572 dvd_priv_t * dvdp = mpctx->stream->priv;
3573 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3575 #endif
3577 #endif /* HAVE_NEW_GUI */
3579 } // while(!mpctx->eof)
3581 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->eof);
3583 #ifdef HAS_DVBIN_SUPPORT
3584 if(mpctx->dvbin_reopen)
3586 mpctx->eof = 0;
3587 uninit_player(INITED_ALL-(INITED_GUI|INITED_STREAM|INITED_INPUT|INITED_GETCH2|(fixed_vo?INITED_VO:0)));
3588 cache_uninit(mpctx->stream);
3589 mpctx->dvbin_reopen = 0;
3590 goto goto_enable_cache;
3592 #endif
3595 goto_next_file: // don't jump here after ao/vo/getch initialization!
3597 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3599 if(benchmark){
3600 double tot=video_time_usage+vout_time_usage+audio_time_usage;
3601 double total_time_usage;
3602 total_time_usage_start=GetTimer()-total_time_usage_start;
3603 total_time_usage = (float)total_time_usage_start*0.000001;
3604 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
3605 video_time_usage,vout_time_usage,audio_time_usage,
3606 total_time_usage-tot,total_time_usage);
3607 if(total_time_usage>0.0)
3608 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
3609 100.0*video_time_usage/total_time_usage,
3610 100.0*vout_time_usage/total_time_usage,
3611 100.0*audio_time_usage/total_time_usage,
3612 100.0*(total_time_usage-tot)/total_time_usage,
3613 100.0);
3614 if(total_frame_cnt && frame_dropping)
3615 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
3616 total_frame_cnt-drop_frame_cnt,
3617 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
3618 drop_frame_cnt,
3619 100*drop_frame_cnt/total_frame_cnt,
3620 total_frame_cnt,
3621 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
3625 // time to uninit all, except global stuff:
3626 uninit_player(INITED_ALL-(INITED_GUI+INITED_INPUT+(fixed_vo?INITED_VO:0)));
3628 if ( mpctx->set_of_sub_size > 0 )
3630 current_module="sub_free";
3631 for (i = 0; i < mpctx->set_of_sub_size; ++i) {
3632 sub_free( mpctx->set_of_subtitles[i] );
3633 #ifdef USE_ASS
3634 if ( mpctx->set_of_ass_tracks[i] )
3635 ass_free_track( mpctx->set_of_ass_tracks[i] );
3636 #endif
3638 mpctx->set_of_sub_size = 0;
3640 vo_sub_last = vo_sub=NULL;
3641 subdata=NULL;
3642 #ifdef USE_ASS
3643 ass_track = NULL;
3644 #endif
3646 if(mpctx->eof == PT_NEXT_ENTRY || mpctx->eof == PT_PREV_ENTRY) {
3647 mpctx->eof = mpctx->eof == PT_NEXT_ENTRY ? 1 : -1;
3648 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) == PLAY_TREE_ITER_ENTRY) {
3649 mpctx->eof = 1;
3650 } else {
3651 play_tree_iter_free(mpctx->playtree_iter);
3652 mpctx->playtree_iter = NULL;
3654 mpctx->play_tree_step = 1;
3655 } else if (mpctx->eof == PT_UP_NEXT || mpctx->eof == PT_UP_PREV) {
3656 mpctx->eof = mpctx->eof == PT_UP_NEXT ? 1 : -1;
3657 if ( mpctx->playtree_iter ) {
3658 if(play_tree_iter_up_step(mpctx->playtree_iter,mpctx->eof,0) == PLAY_TREE_ITER_ENTRY) {
3659 mpctx->eof = 1;
3660 } else {
3661 play_tree_iter_free(mpctx->playtree_iter);
3662 mpctx->playtree_iter = NULL;
3665 } else { // NEXT PREV SRC
3666 mpctx->eof = mpctx->eof == PT_PREV_SRC ? -1 : 1;
3669 if(mpctx->eof == 0) mpctx->eof = 1;
3671 while(mpctx->playtree_iter != NULL) {
3672 filename = play_tree_iter_get_file(mpctx->playtree_iter,mpctx->eof);
3673 if(filename == NULL) {
3674 if( play_tree_iter_step(mpctx->playtree_iter,mpctx->eof,0) != PLAY_TREE_ITER_ENTRY) {
3675 play_tree_iter_free(mpctx->playtree_iter);
3676 mpctx->playtree_iter = NULL;
3678 } else
3679 break;
3682 #ifdef HAVE_NEW_GUI
3683 if( use_gui && !mpctx->playtree_iter )
3685 #ifdef USE_DVDREAD
3686 if ( !guiIntfStruct.DiskChanged )
3687 #endif
3688 mplEnd();
3690 #endif
3692 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
3693 if (!mpctx->playtree_iter) filename = NULL;
3694 mpctx->eof = 0;
3695 goto play_next_file;
3699 exit_player_with_rc(MSGTR_Exit_eof, 0);
3701 return 1;