synced with r21989
[mplayer/greg.git] / mplayer.c
blob915816d9f88911f83620e738b670e55f1c051f3d
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "config.h"
9 #ifdef WIN32
10 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
11 #include <windows.h>
12 #endif
13 #include <string.h>
14 #include <unistd.h>
16 // #include <sys/mman.h>
17 #include <sys/types.h>
18 #ifndef __MINGW32__
19 #include <sys/ioctl.h>
20 #include <sys/wait.h>
21 #else
22 #define SIGHUP 1 /* hangup */
23 #define SIGQUIT 3 /* quit */
24 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
25 #define SIGBUS 10 /* bus error */
26 #define SIGPIPE 13 /* broken pipe */
27 extern int mp_input_win32_slave_cmd_func(int fd,char* dest,int size);
28 #endif
30 #include <sys/time.h>
31 #include <sys/stat.h>
33 #include <signal.h>
34 #include <time.h>
35 #include <fcntl.h>
36 #include <limits.h>
38 #include <errno.h>
40 #include "version.h"
42 #include "mp_msg.h"
44 #define HELP_MP_DEFINE_STATIC
45 #include "help_mp.h"
47 #include "m_option.h"
48 #include "m_config.h"
49 #include "m_property.h"
51 #include "cfg-mplayer-def.h"
53 #include "subreader.h"
55 #include "libvo/video_out.h"
57 #include "libvo/font_load.h"
58 #include "libvo/sub.h"
60 #ifdef HAVE_X11
61 #include "libvo/x11_common.h"
62 #endif
64 #include "libao2/audio_out.h"
66 #include "codec-cfg.h"
68 #include "edl.h"
70 #include "spudec.h"
71 #include "vobsub.h"
73 #include "osdep/getch2.h"
74 #include "osdep/timer.h"
76 #include "cpudetect.h"
78 #ifdef HAVE_NEW_GUI
79 #include "Gui/interface.h"
80 #endif
82 #include "input/input.h"
84 int slave_mode=0;
85 int player_idle_mode=0;
86 int quiet=0;
87 int enable_mouse_movements=0;
89 #ifdef WIN32
90 char * proc_priority=NULL;
91 #endif
93 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
95 #ifdef HAVE_RTC
96 #ifdef __linux__
97 #include <linux/rtc.h>
98 #else
99 #include <rtc.h>
100 #define RTC_IRQP_SET RTCIO_IRQP_SET
101 #define RTC_PIE_ON RTCIO_PIE_ON
102 #endif /* __linux__ */
103 #endif /* HAVE_RTC */
105 #ifdef USE_TV
106 #include "stream/tv.h"
107 #endif
108 #ifdef USE_RADIO
109 #include "stream/stream_radio.h"
110 #endif
112 #ifdef HAS_DVBIN_SUPPORT
113 #include "stream/dvbin.h"
114 static int last_dvb_step = 1;
115 static int dvbin_reopen = 0;
116 #include "stream/cache2.h"
117 #endif
119 //**************************************************************************//
120 // Playtree
121 //**************************************************************************//
122 #include "playtree.h"
123 #include "playtreeparser.h"
125 #ifdef HAVE_NEW_GUI
126 extern int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
127 extern int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
128 #endif
130 play_tree_t* playtree;
131 play_tree_iter_t* playtree_iter = NULL;
132 static int play_tree_step = 1;
134 #define PT_NEXT_ENTRY 1
135 #define PT_PREV_ENTRY -1
136 #define PT_NEXT_SRC 2
137 #define PT_PREV_SRC -2
138 #define PT_UP_NEXT 3
139 #define PT_UP_PREV -3
141 //**************************************************************************//
142 // Config
143 //**************************************************************************//
144 #include "parser-cfg.h"
145 #include "parser-mpcmd.h"
147 m_config_t* mconfig;
149 //**************************************************************************//
150 // Config file
151 //**************************************************************************//
153 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
155 static int cfg_include(m_option_t *conf, char *filename){
156 return m_config_parse_config_file(mconfig, filename);
159 #include "get_path.c"
161 //**************************************************************************//
162 // XScreensaver
163 //**************************************************************************//
165 #ifdef HAVE_X11
166 void xscreensaver_heartbeat(void);
167 #endif
169 //**************************************************************************//
170 //**************************************************************************//
171 // Input media streaming & demultiplexer:
172 //**************************************************************************//
174 static int max_framesize=0;
176 #include "stream/stream.h"
177 #include "libmpdemux/demuxer.h"
178 #include "libmpdemux/stheader.h"
179 //#include "parse_es.h"
180 #include "libmpdemux/matroska.h"
182 #ifdef USE_DVDREAD
183 #include "stream/stream_dvd.h"
184 #endif
186 #ifdef USE_DVDNAV
187 #include "stream/stream_dvdnav.h"
188 #endif
190 #include "libmpcodecs/dec_audio.h"
191 #include "libmpcodecs/dec_video.h"
192 #include "libmpcodecs/mp_image.h"
193 #include "libmpcodecs/vf.h"
194 #include "libmpcodecs/vd.h"
196 //**************************************************************************//
198 static void reinit_audio_chain(void);
200 //**************************************************************************//
201 //**************************************************************************//
203 // Common FIFO functions, and keyboard/event FIFO code
204 #include "fifo.c"
205 int noconsolecontrols=0;
206 //**************************************************************************//
208 vo_functions_t *video_out=NULL;
209 ao_functions_t *audio_out=NULL;
211 int fixed_vo=0;
212 int eof=0;
214 // benchmark:
215 double video_time_usage=0;
216 double vout_time_usage=0;
217 static double audio_time_usage=0;
218 static int total_time_usage_start=0;
219 static int total_frame_cnt=0;
220 static int drop_frame_cnt=0; // total number of dropped frames
221 int benchmark=0;
223 // options:
224 int auto_quality=0;
225 static int output_quality=0;
227 float playback_speed=1.0;
229 int use_gui=0;
231 #ifdef HAVE_NEW_GUI
232 int enqueue=0;
233 #endif
235 static int list_properties = 0;
237 #define MAX_OSD_LEVEL 3
238 #define MAX_TERM_OSD_LEVEL 1
240 int osd_level=1;
241 int osd_level_saved=-1;
242 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
243 unsigned int osd_visible;
244 static int osd_function=OSD_PLAY;
245 static int osd_show_percentage = 0;
246 static int osd_duration = 1000;
248 static int term_osd = 1;
249 static char* term_osd_esc = "\x1b[A\r\x1b[K";
250 static char* playing_msg = NULL;
251 // seek:
252 static char *seek_to_sec=NULL;
253 static off_t seek_to_byte=0;
254 static off_t step_sec=0;
255 static int loop_times=-1;
256 static int loop_seek=0;
258 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
260 // A/V sync:
261 int autosync=0; // 30 might be a good default value.
263 // may be changed by GUI: (FIXME!)
264 float rel_seek_secs=0;
265 int abs_seek_pos=0;
267 // codecs:
268 char **audio_codec_list=NULL; // override audio codec
269 char **video_codec_list=NULL; // override video codec
270 char **audio_fm_list=NULL; // override audio codec family
271 char **video_fm_list=NULL; // override video codec family
273 // demuxer:
274 extern char *demuxer_name; // override demuxer
275 extern char *audio_demuxer_name; // override audio demuxer
276 extern char *sub_demuxer_name; // override sub demuxer
278 // streaming:
279 int audio_id=-1;
280 int video_id=-1;
281 int dvdsub_id=-2;
282 int vobsub_id=-1;
283 char* audio_lang=NULL;
284 char* dvdsub_lang=NULL;
285 static char* spudec_ifo=NULL;
286 char* filename=NULL; //"MI2-Trailer.avi";
287 int forced_subs_only=0;
288 int file_filter=1;
290 // cache2:
291 int stream_cache_size=-1;
292 #ifdef USE_STREAM_CACHE
293 extern int cache_fill_status;
295 float stream_cache_min_percent=20.0;
296 float stream_cache_seek_min_percent=50.0;
297 #else
298 #define cache_fill_status 0
299 #endif
301 // dump:
302 static char *stream_dump_name="stream.dump";
303 int stream_dump_type=0;
305 // A-V sync:
306 static float default_max_pts_correction=-1;//0.01f;
307 static float max_pts_correction=0;//default_max_pts_correction;
308 static float c_total=0;
309 float audio_delay=0;
310 static int ignore_start=0;
312 static int softsleep=0;
314 float force_fps=0;
315 static int force_srate=0;
316 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
317 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
318 static int play_n_frames=-1;
319 static int play_n_frames_mf=-1;
321 // screen info:
322 char** video_driver_list=NULL;
323 char** audio_driver_list=NULL;
325 extern char *vo_subdevice;
326 extern char *ao_subdevice;
328 // codec outfmt flags (defined in libmpcodecs/vd.c)
329 extern int vo_flags;
331 // sub:
332 char *font_name=NULL;
333 #ifdef HAVE_FONTCONFIG
334 extern int font_fontconfig;
335 #endif
336 float font_factor=0.75;
337 char **sub_name=NULL;
338 float sub_delay=0;
339 float sub_fps=0;
340 int sub_auto = 1;
341 char *vobsub_name=NULL;
342 /*DSP!!char *dsp=NULL;*/
343 int subcc_enabled=0;
344 int suboverlap_enabled = 1;
346 sub_data* set_of_subtitles[MAX_SUBTITLE_FILES];
347 int set_of_sub_size = 0;
348 int set_of_sub_pos = -1;
349 double sub_last_pts = -303;
351 int global_sub_size = 0; // this encompasses all subtitle sources
352 int global_sub_pos = -1; // this encompasses all subtitle sources
353 #define SUB_SOURCE_SUBS 0
354 #define SUB_SOURCE_VOBSUB 1
355 #define SUB_SOURCE_DEMUX 2
356 #define SUB_SOURCES 3
357 int global_sub_indices[SUB_SOURCES];
359 #ifdef USE_ASS
360 #include "libass/ass.h"
361 #include "libass/ass_mp.h"
363 // set_of_ass_tracks[i] contains subtitles from set_of_subtitles[i] parsed by libass
364 // or NULL if format unsupported
365 ass_track_t* set_of_ass_tracks[MAX_SUBTITLE_FILES];
366 ass_track_t* ass_track = 0; // current track to render
367 #endif
369 extern int mp_msg_levels[MSGT_MAX];
370 extern int mp_msg_level_all;
372 static stream_t* stream=NULL;
373 static demuxer_t *demuxer=NULL;
374 static sh_audio_t *sh_audio=NULL;
375 static sh_video_t *sh_video=NULL;
376 static demux_stream_t *d_audio=NULL;
377 static demux_stream_t *d_video=NULL;
378 static demux_stream_t *d_dvdsub=NULL;
380 char* current_module=NULL; // for debugging
382 extern int vo_gamma_gamma;
383 extern int vo_gamma_brightness;
384 extern int vo_gamma_contrast;
385 extern int vo_gamma_saturation;
386 extern int vo_gamma_hue;
388 // ---
390 #ifdef HAVE_MENU
391 #include "m_struct.h"
392 #include "libmenu/menu.h"
393 extern void vf_menu_pause_update(struct vf_instance_s* vf);
394 extern vf_info_t vf_info_menu;
395 static vf_info_t* libmenu_vfs[] = {
396 &vf_info_menu,
397 NULL
399 static vf_instance_t* vf_menu = NULL;
400 static int use_menu = 0;
401 static char* menu_cfg = NULL;
402 static char* menu_root = "main";
403 #endif
406 #ifdef HAVE_RTC
407 static int nortc = 1;
408 static char* rtc_device;
409 #endif
411 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
412 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
413 short user_muted = 0; ///< Stores whether user wanted muted mode.
414 short edl_muted = 0; ///< Stores whether EDL is currently in muted mode.
415 short edl_decision = 0; ///< 1 when an EDL operation has been made.
416 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
417 float begin_skip = MP_NOPTS_VALUE; ///< start time of the current skip while on edlout mode
418 int use_filedir_conf;
420 static unsigned int inited_flags=0;
421 #define INITED_VO 1
422 #define INITED_AO 2
423 #define INITED_GUI 4
424 #define INITED_GETCH2 8
425 #define INITED_SPUDEC 32
426 #define INITED_STREAM 64
427 #define INITED_INPUT 128
428 #define INITED_VOBSUB 256
429 #define INITED_DEMUXER 512
430 #define INITED_ACODEC 1024
431 #define INITED_VCODEC 2048
432 #define INITED_ALL 0xFFFF
434 #include "metadata.h"
436 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
438 static int is_valid_metadata_type (metadata_t type) {
439 switch (type)
441 /* check for valid video stream */
442 case META_VIDEO_CODEC:
443 case META_VIDEO_BITRATE:
444 case META_VIDEO_RESOLUTION:
446 if (!sh_video)
447 return 0;
448 break;
451 /* check for valid audio stream */
452 case META_AUDIO_CODEC:
453 case META_AUDIO_BITRATE:
454 case META_AUDIO_SAMPLES:
456 if (!sh_audio)
457 return 0;
458 break;
461 /* check for valid demuxer */
462 case META_INFO_TITLE:
463 case META_INFO_ARTIST:
464 case META_INFO_ALBUM:
465 case META_INFO_YEAR:
466 case META_INFO_COMMENT:
467 case META_INFO_TRACK:
468 case META_INFO_GENRE:
470 if (!demuxer)
471 return 0;
472 break;
475 default:
476 break;
479 return 1;
482 static char *get_demuxer_info (char *tag) {
483 char **info = demuxer->info;
484 int n;
486 if (!info || !tag)
487 return NULL;
489 for (n = 0; info[2*n] != NULL ; n++)
490 if (!strcmp (info[2*n], tag))
491 break;
493 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
496 char *get_metadata (metadata_t type) {
497 char *meta = NULL;
499 if (!is_valid_metadata_type (type))
500 return NULL;
502 switch (type)
504 case META_NAME:
506 return strdup (mp_basename2 (filename));
509 case META_VIDEO_CODEC:
511 if (sh_video->format == 0x10000001)
512 meta = strdup ("mpeg1");
513 else if (sh_video->format == 0x10000002)
514 meta = strdup ("mpeg2");
515 else if (sh_video->format == 0x10000004)
516 meta = strdup ("mpeg4");
517 else if (sh_video->format == 0x10000005)
518 meta = strdup ("h264");
519 else if (sh_video->format >= 0x20202020)
521 meta = (char *) malloc (8);
522 sprintf (meta, "%.4s", (char *) &sh_video->format);
524 else
526 meta = (char *) malloc (8);
527 sprintf (meta, "0x%08X", sh_video->format);
529 return meta;
532 case META_VIDEO_BITRATE:
534 meta = (char *) malloc (16);
535 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
536 return meta;
539 case META_VIDEO_RESOLUTION:
541 meta = (char *) malloc (16);
542 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
543 return meta;
546 case META_AUDIO_CODEC:
548 if (sh_audio->codec && sh_audio->codec->name)
549 meta = strdup (sh_audio->codec->name);
550 return meta;
553 case META_AUDIO_BITRATE:
555 meta = (char *) malloc (16);
556 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
557 return meta;
560 case META_AUDIO_SAMPLES:
562 meta = (char *) malloc (16);
563 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
564 return meta;
567 /* check for valid demuxer */
568 case META_INFO_TITLE:
569 return get_demuxer_info ("Title");
571 case META_INFO_ARTIST:
572 return get_demuxer_info ("Artist");
574 case META_INFO_ALBUM:
575 return get_demuxer_info ("Album");
577 case META_INFO_YEAR:
578 return get_demuxer_info ("Year");
580 case META_INFO_COMMENT:
581 return get_demuxer_info ("Comment");
583 case META_INFO_TRACK:
584 return get_demuxer_info ("Track");
586 case META_INFO_GENRE:
587 return get_demuxer_info ("Genre");
589 default:
590 break;
593 return meta;
596 #include "mixer.h"
597 mixer_t mixer;
598 /// step size of mixer changes
599 int volstep = 3;
601 static void uninit_player(unsigned int mask){
602 mask=inited_flags&mask;
604 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
606 if(mask&INITED_ACODEC){
607 inited_flags&=~INITED_ACODEC;
608 current_module="uninit_acodec";
609 if(sh_audio) uninit_audio(sh_audio);
610 #ifdef HAVE_NEW_GUI
611 guiGetEvent(guiSetAfilter, (char *)NULL);
612 #endif
613 sh_audio=NULL;
616 if(mask&INITED_VCODEC){
617 inited_flags&=~INITED_VCODEC;
618 current_module="uninit_vcodec";
619 if(sh_video) uninit_video(sh_video);
620 sh_video=NULL;
621 #ifdef HAVE_MENU
622 vf_menu=NULL;
623 #endif
626 if(mask&INITED_DEMUXER){
627 inited_flags&=~INITED_DEMUXER;
628 current_module="free_demuxer";
629 if(demuxer){
630 stream=demuxer->stream;
631 free_demuxer(demuxer);
633 demuxer=NULL;
636 // kill the cache process:
637 if(mask&INITED_STREAM){
638 inited_flags&=~INITED_STREAM;
639 current_module="uninit_stream";
640 if(stream) free_stream(stream);
641 stream=NULL;
644 if(mask&INITED_VO){
645 inited_flags&=~INITED_VO;
646 current_module="uninit_vo";
647 video_out->uninit();
648 video_out=NULL;
651 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
652 if(mask&INITED_GETCH2){
653 inited_flags&=~INITED_GETCH2;
654 current_module="uninit_getch2";
655 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
656 // restore terminal:
657 getch2_disable();
660 if(mask&INITED_VOBSUB){
661 inited_flags&=~INITED_VOBSUB;
662 current_module="uninit_vobsub";
663 if(vo_vobsub) vobsub_close(vo_vobsub);
664 vo_vobsub=NULL;
667 if (mask&INITED_SPUDEC){
668 inited_flags&=~INITED_SPUDEC;
669 current_module="uninit_spudec";
670 spudec_free(vo_spudec);
671 vo_spudec=NULL;
674 if(mask&INITED_AO){
675 inited_flags&=~INITED_AO;
676 current_module="uninit_ao";
677 if (user_muted | edl_muted) mixer_mute(&mixer);
678 audio_out->uninit(eof?0:1); audio_out=NULL;
681 #ifdef HAVE_NEW_GUI
682 if(mask&INITED_GUI){
683 inited_flags&=~INITED_GUI;
684 current_module="uninit_gui";
685 guiDone();
687 #endif
689 if(mask&INITED_INPUT){
690 inited_flags&=~INITED_INPUT;
691 current_module="uninit_input";
692 mp_input_uninit();
695 current_module=NULL;
698 static void exit_player_with_rc(const char* how, int rc){
700 uninit_player(INITED_ALL);
701 #ifdef HAVE_X11
702 #ifdef HAVE_NEW_GUI
703 if ( !use_gui )
704 #endif
705 vo_uninit(); // Close the X11 connection (if any is open).
706 #endif
708 #ifdef HAVE_FREETYPE
709 current_module="uninit_font";
710 if (vo_font) free_font_desc(vo_font);
711 vo_font = NULL;
712 done_freetype();
713 #endif
714 free_osd_list();
716 #ifdef USE_ASS
717 ass_library_done(ass_library);
718 #endif
720 current_module="exit_player";
722 // free mplayer config
723 if(mconfig)
724 m_config_free(mconfig);
726 if(playtree)
727 play_tree_free(playtree, 1);
730 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
731 if(how) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,how);
732 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
734 exit(rc);
737 void exit_player(const char* how){
738 exit_player_with_rc(how, 1);
741 #ifndef __MINGW32__
742 static void child_sighandler(int x){
743 pid_t pid;
744 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
746 #endif
748 #ifdef CRASH_DEBUG
749 static char *prog_path;
750 static int crash_debug = 0;
751 #endif
753 static void exit_sighandler(int x){
754 static int sig_count=0;
755 #ifdef CRASH_DEBUG
756 if (!crash_debug || x != SIGTRAP)
757 #endif
758 ++sig_count;
759 if(inited_flags==0 && sig_count>1) exit(1);
760 if(sig_count==5)
762 /* We're crashing bad and can't uninit cleanly :(
763 * by popular request, we make one last (dirty)
764 * effort to restore the user's
765 * terminal. */
766 getch2_disable();
767 exit(1);
769 if(sig_count==6) exit(1);
770 if(sig_count>6){
771 // can't stop :(
772 #ifndef __MINGW32__
773 kill(getpid(),SIGKILL);
774 #endif
776 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
777 current_module?current_module:"unknown"
779 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
780 if(sig_count<=1)
781 switch(x){
782 case SIGINT:
783 case SIGQUIT:
784 case SIGTERM:
785 case SIGKILL:
786 break; // killed from keyboard (^C) or killed [-9]
787 case SIGILL:
788 #ifdef RUNTIME_CPUDETECT
789 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
790 #else
791 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
792 #endif
793 case SIGFPE:
794 case SIGSEGV:
795 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
796 default:
797 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
798 #ifdef CRASH_DEBUG
799 if (crash_debug) {
800 int gdb_pid;
801 char spid[20];
802 snprintf(spid, 19, "%i", getpid());
803 spid[19] = 0;
804 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
805 gdb_pid = fork();
806 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
807 if (gdb_pid == 0) { // We are the child
808 if (execlp("gdb", "gdb", prog_path, spid, NULL) == -1)
809 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
810 } else if (gdb_pid < 0)
811 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
812 else {
813 waitpid(gdb_pid, NULL, 0);
815 if (x == SIGTRAP) return;
817 #endif
819 exit_player(NULL);
822 extern void mp_input_register_options(m_config_t* cfg);
824 #include "cfg-mplayer.h"
826 static void parse_cfgfiles( m_config_t* conf )
828 char *conffile;
829 int conffile_fd;
830 if (m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
831 exit_player(NULL);
832 if ((conffile = get_path("")) == NULL) {
833 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
834 } else {
835 #ifdef __MINGW32__
836 mkdir(conffile);
837 #else
838 mkdir(conffile, 0777);
839 #endif
840 free(conffile);
841 if ((conffile = get_path("config")) == NULL) {
842 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
843 } else {
844 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
845 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
846 write(conffile_fd, default_config, strlen(default_config));
847 close(conffile_fd);
849 if (m_config_parse_config_file(conf, conffile) < 0)
850 exit_player(NULL);
851 free(conffile);
856 void load_per_file_config (m_config_t* conf, const char *const file)
858 char *confpath;
859 char cfg[strlen(file)+10];
860 struct stat st;
861 char *name;
863 sprintf (cfg, "%s.conf", file);
865 if (use_filedir_conf && !stat (cfg, &st))
867 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
868 m_config_parse_config_file (conf, cfg);
869 return;
872 if ((name = strrchr (cfg, '/')) == NULL)
873 name = cfg;
874 else
875 name++;
877 if ((confpath = get_path (name)) != NULL)
879 if (!stat (confpath, &st))
881 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
882 m_config_parse_config_file (conf, confpath);
885 free (confpath);
889 /* When libmpdemux performs a blocking operation (network connection or
890 * cache filling) if the operation fails we use this function to check
891 * if it was interrupted by the user.
892 * The function returns a new value for eof. */
893 static int libmpdemux_was_interrupted(int eof) {
894 mp_cmd_t* cmd;
895 if((cmd = mp_input_get_cmd(0,0,0)) != NULL) {
896 switch(cmd->id) {
897 case MP_CMD_QUIT:
898 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
899 case MP_CMD_PLAY_TREE_STEP: {
900 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
901 play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
902 } break;
903 case MP_CMD_PLAY_TREE_UP_STEP: {
904 eof = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
905 } break;
906 case MP_CMD_PLAY_ALT_SRC_STEP: {
907 eof = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
908 } break;
910 mp_cmd_free(cmd);
912 return eof;
915 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
917 int playtree_add_playlist(play_tree_t* entry)
919 play_tree_add_bpf(entry,filename);
921 #ifdef HAVE_NEW_GUI
922 if (use_gui) {
923 if (entry) {
924 import_playtree_playlist_into_gui(entry, mconfig);
925 play_tree_free_list(entry,1);
927 } else
928 #endif
930 if(!entry) {
931 entry = playtree_iter->tree;
932 if(play_tree_iter_step(playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
933 return PT_NEXT_ENTRY;
935 if(playtree_iter->tree == entry ) { // Loop with a single file
936 if(play_tree_iter_up_step(playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
937 return PT_NEXT_ENTRY;
940 play_tree_remove(entry,1,1);
941 return PT_NEXT_SRC;
943 play_tree_insert_entry(playtree_iter->tree,entry);
944 play_tree_set_params_from(entry,playtree_iter->tree);
945 entry = playtree_iter->tree;
946 if(play_tree_iter_step(playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
947 return PT_NEXT_ENTRY;
949 play_tree_remove(entry,1,1);
951 return PT_NEXT_SRC;
954 int sub_source(void)
956 int source = -1;
957 int top = -1;
958 int i;
959 for (i = 0; i < SUB_SOURCES; i++) {
960 int j = global_sub_indices[i];
961 if ((j >= 0) && (j > top) && (global_sub_pos >= j)) {
962 source = i;
963 top = j;
966 return source;
969 sub_data* subdata = NULL;
970 static subtitle* vo_sub_last = NULL;
972 void add_subtitles(char *filename, float fps, int silent)
974 sub_data *subd;
975 #ifdef USE_ASS
976 ass_track_t *asst = 0;
977 #endif
979 if (filename == NULL || set_of_sub_size >= MAX_SUBTITLE_FILES) {
980 return;
983 subd = sub_read_file(filename, fps);
984 #ifdef USE_ASS
985 if (ass_enabled)
986 #ifdef USE_ICONV
987 asst = ass_read_file(ass_library, filename, sub_cp);
988 #else
989 asst = ass_read_file(ass_library, filename, 0);
990 #endif
991 if (ass_enabled && subd && !asst)
992 asst = ass_read_subdata(ass_library, subd, fps);
994 if (!asst && !subd && !silent)
995 #else
996 if(!subd && !silent)
997 #endif
998 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_CantLoadSub, filename);
1000 #ifdef USE_ASS
1001 if (!asst && !subd) return;
1002 set_of_ass_tracks[set_of_sub_size] = asst;
1003 #else
1004 if (!subd) return;
1005 #endif
1006 set_of_subtitles[set_of_sub_size] = subd;
1007 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", set_of_sub_size);
1008 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n", filename);
1009 ++set_of_sub_size;
1010 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, set_of_sub_size, filename);
1013 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1014 void update_set_of_subtitles(void)
1015 // subdata was changed, set_of_sub... have to be updated.
1017 int i;
1018 if (set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1019 for (i = set_of_sub_pos + 1; i < set_of_sub_size; ++i)
1020 set_of_subtitles[i-1] = set_of_subtitles[i];
1021 set_of_subtitles[set_of_sub_size-1] = NULL;
1022 --set_of_sub_size;
1023 if (set_of_sub_size > 0) subdata = set_of_subtitles[set_of_sub_pos=0];
1025 else if (set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1026 set_of_subtitles[set_of_sub_pos] = subdata;
1028 else if (set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1029 set_of_subtitles[set_of_sub_pos=set_of_sub_size] = subdata;
1030 ++set_of_sub_size;
1034 void init_vo_spudec(void) {
1035 if (vo_spudec)
1036 spudec_free(vo_spudec);
1037 inited_flags &= ~INITED_SPUDEC;
1038 vo_spudec = NULL;
1039 if (spudec_ifo) {
1040 unsigned int palette[16], width, height;
1041 current_module="spudec_init_vobsub";
1042 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1043 vo_spudec=spudec_new_scaled(palette, width, height);
1046 #ifdef USE_DVDREAD
1047 if (vo_spudec==NULL && stream->type==STREAMTYPE_DVD) {
1048 current_module="spudec_init_dvdread";
1049 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(stream->priv))->cur_pgc->palette,
1050 sh_video->disp_w, sh_video->disp_h);
1052 #endif
1054 #ifdef USE_DVDNAV
1055 if (vo_spudec==NULL && stream->type==STREAMTYPE_DVDNAV) {
1056 unsigned int *palette = mp_dvdnav_get_spu_clut(stream);
1057 current_module="spudec_init_dvdnav";
1058 vo_spudec=spudec_new_scaled(palette, sh_video->disp_w, sh_video->disp_h);
1060 #endif
1062 if ((vo_spudec == NULL) && (demuxer->type == DEMUXER_TYPE_MATROSKA) &&
1063 (d_dvdsub->sh != NULL) && (((sh_sub_t *)d_dvdsub->sh)->type == 'v')) {
1064 sh_sub_t *mkv_sh_sub = (sh_sub_t *)d_dvdsub->sh;
1065 current_module = "spudec_init_matroska";
1066 vo_spudec =
1067 spudec_new_scaled_vobsub(mkv_sh_sub->palette, mkv_sh_sub->colors,
1068 mkv_sh_sub->custom_colors, mkv_sh_sub->width,
1069 mkv_sh_sub->height);
1070 forced_subs_only = mkv_sh_sub->forced_subs_only;
1073 if (vo_spudec==NULL) {
1074 sh_sub_t *sh = (sh_sub_t *)d_dvdsub->sh;
1075 unsigned int *palette = NULL;
1076 if (sh && sh->has_palette)
1077 palette = sh->palette;
1078 current_module="spudec_init_normal";
1079 vo_spudec=spudec_new_scaled(palette, sh_video->disp_w, sh_video->disp_h);
1080 spudec_set_font_factor(vo_spudec,font_factor);
1083 if (vo_spudec!=NULL)
1084 inited_flags|=INITED_SPUDEC;
1088 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1089 * make it all work is to use the builtin SDL-bootstrap code, which
1090 * will be done automatically by replacing our main() if we include SDL.h.
1092 #if defined(SYS_DARWIN) && defined(HAVE_SDL)
1093 #include <SDL.h>
1094 #endif
1097 * \brief append a formatted string
1098 * \param buf buffer to print into
1099 * \param pos position of terminating 0 in buf
1100 * \param len maximum number of characters in buf, not including terminating 0
1101 * \param format printf format string
1103 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1105 va_list va;
1106 va_start(va, format);
1107 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1108 va_end(va);
1109 if (*pos >= len ) {
1110 buf[len] = 0;
1111 *pos = len;
1116 * \brief append time in the hh:mm:ss.f format
1117 * \param buf buffer to print into
1118 * \param pos position of terminating 0 in buf
1119 * \param len maximum number of characters in buf, not including terminating 0
1120 * \param time time value to convert/append
1122 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1123 long tenths = 10 * time;
1124 int f1 = tenths % 10;
1125 int ss = (tenths / 10) % 60;
1126 int mm = (tenths / 600) % 60;
1127 int hh = tenths / 36000;
1128 if (time <= 0) {
1129 saddf(buf, pos, len, "unknown");
1130 return;
1132 if (hh > 0)
1133 saddf(buf, pos, len, "%2d:", hh);
1134 if (hh > 0 || mm > 0)
1135 saddf(buf, pos, len, "%02d:", mm);
1136 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1140 * \brief print the status line
1141 * \param a_pos audio position
1142 * \param a_v A-V desynchronization
1143 * \param corr amount out A-V synchronization
1145 static void print_status(float a_pos, float a_v, float corr)
1147 int width;
1148 char *line;
1149 unsigned pos = 0;
1150 get_screen_size();
1151 if (screen_width > 0)
1152 width = screen_width;
1153 else
1154 width = 80;
1155 #ifdef WIN32
1156 /* Windows command line is broken (MinGW's rxvt works, but we
1157 * should not depend on that). */
1158 width--;
1159 #endif
1160 line = malloc(width + 1); // one additional char for the terminating null
1162 // Audio time
1163 if (sh_audio) {
1164 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1165 if (!sh_video) {
1166 float len = demuxer_get_time_length(demuxer);
1167 saddf(line, &pos, width, "(");
1168 sadd_hhmmssf(line, &pos, width, a_pos);
1169 saddf(line, &pos, width, ") of %.1f (", len);
1170 sadd_hhmmssf(line, &pos, width, len);
1171 saddf(line, &pos, width, ") ");
1175 // Video time
1176 if (sh_video)
1177 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1179 // A-V sync
1180 if (sh_audio && sh_video)
1181 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ", a_v, corr);
1183 // Video stats
1184 if (sh_video)
1185 saddf(line, &pos, width, "%3d/%3d ",
1186 (int)sh_video->num_frames,
1187 (int)sh_video->num_frames_decoded);
1189 // CPU usage
1190 if (sh_video) {
1191 if (sh_video->timer > 0.5)
1192 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1193 (int)(100.0*video_time_usage*playback_speed/(double)sh_video->timer),
1194 (int)(100.0*vout_time_usage*playback_speed/(double)sh_video->timer),
1195 (100.0*audio_time_usage*playback_speed/(double)sh_video->timer));
1196 else
1197 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1198 } else if (sh_audio) {
1199 if (sh_audio->delay > 0.5)
1200 saddf(line, &pos, width, "%4.1f%% ",
1201 100.0*audio_time_usage/(double)sh_audio->delay);
1202 else
1203 saddf(line, &pos, width, "??,?%% ");
1206 // VO stats
1207 if (sh_video)
1208 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1210 #ifdef USE_STREAM_CACHE
1211 // cache stats
1212 if (stream_cache_size > 0)
1213 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1214 #endif
1216 // other
1217 if (playback_speed != 1)
1218 saddf(line, &pos, width, "%4.2fx ", playback_speed);
1220 // end
1221 if (erase_to_end_of_line) {
1222 line[pos] = 0;
1223 mp_msg(MSGT_AVSYNC, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1224 } else {
1225 memset(&line[pos], ' ', width - pos);
1226 line[width] = 0;
1227 mp_msg(MSGT_AVSYNC, MSGL_STATUS, "%s\r", line);
1229 free(line);
1233 * \brief build a chain of audio filters that converts the input format
1234 * to the ao's format, taking into account the current playback_speed.
1235 * \param sh_audio describes the requested input format of the chain.
1236 * \param ao_data describes the requested output format of the chain.
1238 static int build_afilter_chain(sh_audio_t *sh_audio, ao_data_t *ao_data)
1240 int new_srate;
1241 int result;
1242 if (!sh_audio)
1244 #ifdef HAVE_NEW_GUI
1245 guiGetEvent(guiSetAfilter, (char *)NULL);
1246 #endif
1247 mixer.afilter = NULL;
1248 return 0;
1250 new_srate = sh_audio->samplerate * playback_speed;
1251 if (new_srate != ao_data->samplerate) {
1252 // limits are taken from libaf/af_resample.c
1253 if (new_srate < 8000)
1254 new_srate = 8000;
1255 if (new_srate > 192000)
1256 new_srate = 192000;
1257 playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1259 result = init_audio_filters(sh_audio, new_srate,
1260 sh_audio->channels, sh_audio->sample_format,
1261 &ao_data->samplerate, &ao_data->channels, &ao_data->format,
1262 ao_data->outburst * 4, ao_data->buffersize);
1263 mixer.afilter = sh_audio->afilter;
1264 #ifdef HAVE_NEW_GUI
1265 guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1266 #endif
1267 return result;
1271 * \brief Log the currently displayed subtitle to a file
1273 * Logs the current or last displayed subtitle together with filename
1274 * and time information to ~/.mplayer/subtitle_log
1276 * Intended purpose is to allow convenient marking of bogus subtitles
1277 * which need to be fixed while watching the movie.
1280 static void log_sub(void){
1281 char *fname;
1282 FILE *f;
1283 int i;
1285 if (subdata == NULL || vo_sub_last == NULL) return;
1286 fname = get_path("subtitle_log");
1287 f = fopen(fname, "a");
1288 if (!f) return;
1289 fprintf(f, "----------------------------------------------------------\n");
1290 if (subdata->sub_uses_time) {
1291 fprintf(f, "N: %s S: %02ld:%02ld:%02ld.%02ld E: %02ld:%02ld:%02ld.%02ld\n", filename,
1292 vo_sub_last->start/360000, (vo_sub_last->start/6000)%60,
1293 (vo_sub_last->start/100)%60, vo_sub_last->start%100,
1294 vo_sub_last->end/360000, (vo_sub_last->end/6000)%60,
1295 (vo_sub_last->end/100)%60, vo_sub_last->end%100);
1296 } else {
1297 fprintf(f, "N: %s S: %ld E: %ld\n", filename, vo_sub_last->start, vo_sub_last->end);
1299 for (i = 0; i < vo_sub_last->lines; i++) {
1300 fprintf(f, "%s\n", vo_sub_last->text[i]);
1302 fclose(f);
1305 /// \defgroup OSDMsgStack OSD message stack
1307 ///@{
1309 #define OSD_MSG_TV_CHANNEL 0
1310 #define OSD_MSG_TEXT 1
1311 #define OSD_MSG_SUB_DELAY 2
1312 #define OSD_MSG_SPEED 3
1313 #define OSD_MSG_OSD_STATUS 4
1314 #define OSD_MSG_BAR 5
1315 #define OSD_MSG_PAUSE 6
1316 #define OSD_MSG_RADIO_CHANNEL 7
1317 /// Base id for messages generated from the commmand to property bridge.
1318 #define OSD_MSG_PROPERTY 0x100
1321 typedef struct mp_osd_msg mp_osd_msg_t;
1322 struct mp_osd_msg {
1323 /// Previous message on the stack.
1324 mp_osd_msg_t* prev;
1325 /// Message text.
1326 char msg[64];
1327 int id,level,started;
1328 /// Display duration in ms.
1329 unsigned time;
1332 /// OSD message stack.
1333 static mp_osd_msg_t* osd_msg_stack = NULL;
1336 * \brief Add a message on the OSD message stack
1338 * If a message with the same id is already present in the stack
1339 * it is pulled on top of the stack, otherwise a new message is created.
1343 static void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1344 mp_osd_msg_t *msg,*last=NULL;
1345 va_list va;
1346 int r;
1348 // look if the id is already in the stack
1349 for(msg = osd_msg_stack ; msg && msg->id != id ;
1350 last = msg, msg = msg->prev);
1351 // not found: alloc it
1352 if(!msg) {
1353 msg = calloc(1,sizeof(mp_osd_msg_t));
1354 msg->prev = osd_msg_stack;
1355 osd_msg_stack = msg;
1356 } else if(last) { // found, but it's not on top of the stack
1357 last->prev = msg->prev;
1358 msg->prev = osd_msg_stack;
1359 osd_msg_stack = msg;
1361 // write the msg
1362 va_start(va,fmt);
1363 r = vsnprintf(msg->msg, 64, fmt, va);
1364 va_end(va);
1365 if(r >= 64) msg->msg[63] = 0;
1366 // set id and time
1367 msg->id = id;
1368 msg->level = level;
1369 msg->time = time;
1374 * \brief Remove a message from the OSD stack
1376 * This function can be used to get rid of a message right away.
1380 static void rm_osd_msg(int id) {
1381 mp_osd_msg_t *msg,*last=NULL;
1383 // Search for the msg
1384 for(msg = osd_msg_stack ; msg && msg->id != id ;
1385 last = msg, msg = msg->prev);
1386 if(!msg) return;
1388 // Detach it from the stack and free it
1389 if(last)
1390 last->prev = msg->prev;
1391 else
1392 osd_msg_stack = msg->prev;
1393 free(msg);
1397 * \brief Remove all messages from the OSD stack
1401 static void clear_osd_msgs(void) {
1402 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1403 while(msg) {
1404 prev = msg->prev;
1405 free(msg);
1406 msg = prev;
1408 osd_msg_stack = NULL;
1412 * \brief Get the current message from the OSD stack.
1414 * This function decrements the message timer and destroys the old ones.
1415 * The message that should be displayed is returned (if any).
1419 static mp_osd_msg_t* get_osd_msg(void) {
1420 mp_osd_msg_t *msg,*prev,*last = NULL;
1421 static unsigned last_update = 0;
1422 unsigned now = GetTimerMS();
1423 unsigned diff;
1424 char hidden_dec_done = 0;
1426 if(!last_update) last_update = now;
1427 diff = now >= last_update ? now - last_update : 0;
1429 last_update = now;
1431 // Look for the first message in the stack with high enough level.
1432 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1433 prev = msg->prev;
1434 if(msg->level > osd_level && hidden_dec_done) continue;
1435 // The message has a high enough level or it is the first hidden one
1436 // in both cases we decrement the timer or kill it.
1437 if(!msg->started || msg->time > diff) {
1438 if(msg->started) msg->time -= diff;
1439 else msg->started = 1;
1440 // display it
1441 if(msg->level <= osd_level) return msg;
1442 hidden_dec_done = 1;
1443 continue;
1445 // kill the message
1446 free(msg);
1447 if(last) {
1448 last->prev = prev;
1449 msg = last;
1450 } else {
1451 osd_msg_stack = prev;
1452 msg = NULL;
1455 // Nothing found
1456 return NULL;
1460 * \brief Display the OSD bar.
1462 * Display the OSD bar or fall back on a simple message.
1466 static void set_osd_bar(int type,const char* name,double min,double max,double val) {
1468 if(osd_level < 1) return;
1470 if(sh_video) {
1471 osd_visible = (GetTimerMS() + 1000) | 1;
1472 vo_osd_progbar_type = type;
1473 vo_osd_progbar_value = 256*(val-min)/(max-min);
1474 vo_osd_changed(OSDTYPE_PROGBAR);
1475 return;
1478 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1479 name,ROUND(100*(val-min)/(max-min)));
1484 * \brief Update the OSD message line.
1486 * This function displays the current message on the vo OSD or on the term.
1487 * If the stack is empty and the OSD level is high enough the timer
1488 * is displayed (only on the vo OSD).
1492 static void update_osd_msg(void) {
1493 mp_osd_msg_t *msg;
1494 static char osd_text[64] = "";
1495 static char osd_text_timer[64];
1497 // we need some mem for vo_osd_text
1498 vo_osd_text = (unsigned char*)osd_text;
1500 // Look if we have a msg
1501 if((msg = get_osd_msg())) {
1502 if(strcmp(osd_text,msg->msg)) {
1503 strncpy((char*)osd_text, msg->msg, 63);
1504 if(sh_video) vo_osd_changed(OSDTYPE_OSD); else
1505 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1507 return;
1510 if(sh_video) {
1511 // fallback on the timer
1512 if(osd_level>=2) {
1513 int len = demuxer_get_time_length(demuxer);
1514 int percentage = -1;
1515 char percentage_text[10];
1516 int pts = demuxer_get_current_time(demuxer);
1518 if (osd_show_percentage)
1519 percentage = demuxer_get_percent_pos(demuxer);
1521 if (percentage >= 0)
1522 snprintf(percentage_text, 9, " (%d%%)", percentage);
1523 else
1524 percentage_text[0] = 0;
1526 if (osd_level == 3)
1527 snprintf(osd_text_timer, 63,
1528 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1529 osd_function,pts/3600,(pts/60)%60,pts%60,
1530 len/3600,(len/60)%60,len%60,percentage_text);
1531 else
1532 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1533 osd_function,pts/3600,(pts/60)%60,
1534 pts%60,percentage_text);
1535 } else
1536 osd_text_timer[0]=0;
1538 // always decrement the percentage timer
1539 if(osd_show_percentage)
1540 osd_show_percentage--;
1542 if(strcmp(osd_text,osd_text_timer)) {
1543 strncpy(osd_text, osd_text_timer, 63);
1544 vo_osd_changed(OSDTYPE_OSD);
1546 return;
1549 // Clear the term osd line
1550 if(term_osd && osd_text[0]) {
1551 osd_text[0] = 0;
1552 printf("%s\n",term_osd_esc);
1556 ///@}
1557 // OSDMsgStack
1559 /// \defgroup Properties
1560 ///@{
1562 /// \defgroup GeneralProperties General properties
1563 /// \ingroup Properties
1564 ///@{
1566 /// OSD level (RW)
1567 static int mp_property_osdlevel(m_option_t* prop,int action,void* arg) {
1568 return m_property_choice(prop,action,arg,&osd_level);
1571 /// Playback speed (RW)
1572 static int mp_property_playback_speed(m_option_t* prop,int action,void* arg) {
1573 switch(action) {
1574 case M_PROPERTY_SET:
1575 if(!arg) return M_PROPERTY_ERROR;
1576 M_PROPERTY_CLAMP(prop,*(float*)arg);
1577 playback_speed = *(float*)arg;
1578 build_afilter_chain(sh_audio, &ao_data);
1579 return M_PROPERTY_OK;
1580 case M_PROPERTY_STEP_UP:
1581 case M_PROPERTY_STEP_DOWN:
1582 playback_speed += (arg ? *(float*)arg : 0.1) *
1583 (action == M_PROPERTY_STEP_DOWN ? -1 : 1);
1584 M_PROPERTY_CLAMP(prop,playback_speed);
1585 build_afilter_chain(sh_audio, &ao_data);
1586 return M_PROPERTY_OK;
1588 return m_property_float_range(prop,action,arg,&playback_speed);
1591 /// filename with path (RO)
1592 static int mp_property_path(m_option_t* prop,int action,void* arg) {
1593 return m_property_string_ro(prop,action,arg,filename);
1596 /// filename without path (RO)
1597 static int mp_property_filename(m_option_t* prop,int action,void* arg) {
1598 char* f;
1599 if(!filename) return M_PROPERTY_UNAVAILABLE;
1600 if(((f = strrchr(filename,'/')) || (f = strrchr(filename,'\\'))) && f[1])
1601 f++;
1602 else
1603 f = filename;
1604 return m_property_string_ro(prop,action,arg,f);
1607 /// Demuxer name (RO)
1608 static int mp_property_demuxer(m_option_t* prop,int action,void* arg) {
1609 if(!demuxer) return M_PROPERTY_UNAVAILABLE;
1610 return m_property_string_ro(prop,action,arg,(char*)demuxer->desc->name);
1613 /// Position in the stream (RW)
1614 static int mp_property_stream_pos(m_option_t* prop,int action,void* arg) {
1615 if (!demuxer || !demuxer->stream) return M_PROPERTY_UNAVAILABLE;
1616 if (!arg) return M_PROPERTY_ERROR;
1617 switch (action) {
1618 case M_PROPERTY_GET:
1619 *(off_t*)arg = stream_tell(demuxer->stream);
1620 return M_PROPERTY_OK;
1621 case M_PROPERTY_SET:
1622 M_PROPERTY_CLAMP(prop,*(off_t*)arg);
1623 stream_seek(demuxer->stream, *(off_t*)arg);
1624 return M_PROPERTY_OK;
1626 return M_PROPERTY_NOT_IMPLEMENTED;
1629 /// Stream start offset (RO)
1630 static int mp_property_stream_start(m_option_t* prop,int action,void* arg) {
1631 if (!demuxer || !demuxer->stream) return M_PROPERTY_UNAVAILABLE;
1632 switch (action) {
1633 case M_PROPERTY_GET:
1634 *(off_t*)arg = demuxer->stream->start_pos;
1635 return M_PROPERTY_OK;
1637 return M_PROPERTY_NOT_IMPLEMENTED;
1640 /// Stream end offset (RO)
1641 static int mp_property_stream_end(m_option_t* prop,int action,void* arg) {
1642 if (!demuxer || !demuxer->stream) return M_PROPERTY_UNAVAILABLE;
1643 switch (action) {
1644 case M_PROPERTY_GET:
1645 *(off_t*)arg = demuxer->stream->end_pos;
1646 return M_PROPERTY_OK;
1648 return M_PROPERTY_NOT_IMPLEMENTED;
1651 /// Stream length (RO)
1652 static int mp_property_stream_length(m_option_t* prop,int action,void* arg) {
1653 if (!demuxer || !demuxer->stream) return M_PROPERTY_UNAVAILABLE;
1654 switch (action) {
1655 case M_PROPERTY_GET:
1656 *(off_t*)arg = demuxer->stream->end_pos - demuxer->stream->start_pos;
1657 return M_PROPERTY_OK;
1659 return M_PROPERTY_NOT_IMPLEMENTED;
1662 /// Media length in seconds (RO)
1663 static int mp_property_length(m_option_t* prop,int action,void* arg) {
1664 double len;
1666 if(!demuxer ||
1667 !(int)(len = demuxer_get_time_length(demuxer)))
1668 return M_PROPERTY_UNAVAILABLE;
1670 switch(action) {
1671 case M_PROPERTY_PRINT:
1672 if(!arg) return M_PROPERTY_ERROR;
1673 else {
1674 int h, m, s = len;
1675 h = s/3600;
1676 s -= h*3600;
1677 m = s/60;
1678 s -= m*60;
1679 *(char**)arg = malloc(20);
1680 if(h > 0) sprintf(*(char**)arg,"%d:%02d:%02d",h,m,s);
1681 else if(m > 0) sprintf(*(char**)arg,"%d:%02d",m,s);
1682 else sprintf(*(char**)arg,"%d",s);
1683 return M_PROPERTY_OK;
1685 break;
1687 return m_property_double_ro(prop,action,arg,len);
1690 ///@}
1692 /// \defgroup AudioProperties Audio properties
1693 /// \ingroup Properties
1694 ///@{
1696 /// Volume (RW)
1697 static int mp_property_volume(m_option_t* prop,int action,void* arg) {
1699 if(!sh_audio) return M_PROPERTY_UNAVAILABLE;
1701 switch(action) {
1702 case M_PROPERTY_GET:
1703 if(!arg) return M_PROPERTY_ERROR;
1704 mixer_getbothvolume(&mixer,arg);
1705 return M_PROPERTY_OK;
1706 case M_PROPERTY_PRINT:{
1707 float vol;
1708 if(!arg) return M_PROPERTY_ERROR;
1709 mixer_getbothvolume(&mixer,&vol);
1710 return m_property_float_range(prop,action,arg,&vol);
1712 case M_PROPERTY_STEP_UP:
1713 case M_PROPERTY_STEP_DOWN:
1714 case M_PROPERTY_SET:
1715 break;
1716 default:
1717 return M_PROPERTY_NOT_IMPLEMENTED;
1720 if (edl_muted) return M_PROPERTY_DISABLED;
1721 user_muted = 0;
1723 switch(action) {
1724 case M_PROPERTY_SET:
1725 if(!arg) return M_PROPERTY_ERROR;
1726 M_PROPERTY_CLAMP(prop,*(float*)arg);
1727 mixer_setvolume(&mixer,*(float*)arg,*(float*)arg);
1728 return M_PROPERTY_OK;
1729 case M_PROPERTY_STEP_UP:
1730 if(arg && *(float*)arg <= 0)
1731 mixer_decvolume(&mixer);
1732 else
1733 mixer_incvolume(&mixer);
1734 return M_PROPERTY_OK;
1735 case M_PROPERTY_STEP_DOWN:
1736 if(arg && *(float*)arg <= 0)
1737 mixer_incvolume(&mixer);
1738 else
1739 mixer_decvolume(&mixer);
1740 return M_PROPERTY_OK;
1742 return M_PROPERTY_NOT_IMPLEMENTED;
1745 /// Mute (RW)
1746 static int mp_property_mute(m_option_t* prop,int action,void* arg) {
1748 if(!sh_audio) return M_PROPERTY_UNAVAILABLE;
1750 switch(action) {
1751 case M_PROPERTY_SET:
1752 if(edl_muted) return M_PROPERTY_DISABLED;
1753 if(!arg) return M_PROPERTY_ERROR;
1754 if((!!*(int*)arg) != mixer.muted)
1755 mixer_mute(&mixer);
1756 user_muted = mixer.muted;
1757 return M_PROPERTY_OK;
1758 case M_PROPERTY_STEP_UP:
1759 case M_PROPERTY_STEP_DOWN:
1760 if(edl_muted) return M_PROPERTY_DISABLED;
1761 mixer_mute(&mixer);
1762 user_muted = mixer.muted;
1763 return M_PROPERTY_OK;
1764 case M_PROPERTY_PRINT:
1765 if(!arg) return M_PROPERTY_ERROR;
1766 if(edl_muted) {
1767 *(char**)arg = strdup(MSGTR_EnabledEdl);
1768 return M_PROPERTY_OK;
1770 default:
1771 return m_property_flag(prop,action,arg,&mixer.muted);
1776 /// Audio delay (RW)
1777 static int mp_property_audio_delay(m_option_t* prop,int action,void* arg) {
1778 if(!(sh_audio && sh_video)) return M_PROPERTY_UNAVAILABLE;
1779 switch(action) {
1780 case M_PROPERTY_SET:
1781 case M_PROPERTY_STEP_UP:
1782 case M_PROPERTY_STEP_DOWN:
1783 if(!arg) return M_PROPERTY_ERROR;
1784 else {
1785 float delay = audio_delay;
1786 m_property_delay(prop,action,arg,&audio_delay);
1787 if(sh_audio) sh_audio->delay -= audio_delay-delay;
1789 return M_PROPERTY_OK;
1790 default:
1791 return m_property_delay(prop,action,arg,&audio_delay);
1795 /// Audio codec tag (RO)
1796 static int mp_property_audio_format(m_option_t* prop,int action,void* arg) {
1797 if(!sh_audio) return M_PROPERTY_UNAVAILABLE;
1798 return m_property_int_ro(prop,action,arg,sh_audio->format);
1801 /// Audio bitrate (RO)
1802 static int mp_property_audio_bitrate(m_option_t* prop,int action,void* arg) {
1803 if(!sh_audio) return M_PROPERTY_UNAVAILABLE;
1804 return m_property_int_ro(prop,action,arg,sh_audio->i_bps);
1807 /// Samplerate (RO)
1808 static int mp_property_samplerate(m_option_t* prop,int action,void* arg) {
1809 if(!sh_audio) return M_PROPERTY_UNAVAILABLE;
1810 return m_property_int_ro(prop,action,arg,sh_audio->samplerate);
1813 /// Number of channels (RO)
1814 static int mp_property_channels(m_option_t* prop,int action,void* arg) {
1815 if(!sh_audio) return M_PROPERTY_UNAVAILABLE;
1816 switch(action) {
1817 case M_PROPERTY_PRINT:
1818 if(!arg) return M_PROPERTY_ERROR;
1819 switch(sh_audio->channels) {
1820 case 1: *(char**)arg = strdup("mono"); break;
1821 case 2: *(char**)arg = strdup("stereo"); break;
1822 default:
1823 *(char**)arg = malloc(32);
1824 sprintf(*(char**)arg,"%d channels",sh_audio->channels);
1826 return M_PROPERTY_OK;
1828 return m_property_int_ro(prop,action,arg,sh_audio->channels);
1831 /// Selected audio id (RW)
1832 static int mp_property_audio(m_option_t* prop,int action,void* arg) {
1833 int current_id = -1, tmp;
1835 switch(action) {
1836 case M_PROPERTY_GET:
1837 if(!sh_audio) return M_PROPERTY_UNAVAILABLE;
1838 if(!arg) return M_PROPERTY_ERROR;
1839 *(int*)arg = audio_id;
1840 return M_PROPERTY_OK;
1841 case M_PROPERTY_PRINT:
1842 if(!sh_audio) return M_PROPERTY_UNAVAILABLE;
1843 if(!arg) return M_PROPERTY_ERROR;
1845 if (audio_id < 0)
1846 *(char**)arg = strdup(MSGTR_Disabled);
1847 else {
1848 char lang[40] = MSGTR_Unknown;
1849 if (demuxer->type == DEMUXER_TYPE_MATROSKA)
1850 demux_mkv_get_audio_lang(demuxer, audio_id, lang, 9);
1851 #ifdef USE_DVDREAD
1852 else if(stream->type == STREAMTYPE_DVD) {
1853 int code = dvd_lang_from_aid(stream, audio_id);
1854 if (code) {
1855 lang[0] = code >> 8;
1856 lang[1] = code;
1857 lang[2] = 0;
1860 #endif
1862 #ifdef USE_DVDNAV
1863 else if(stream->type == STREAMTYPE_DVDNAV)
1864 dvdnav_lang_from_aid(stream, audio_id, lang);
1865 #endif
1866 *(char**)arg = malloc(64);
1867 snprintf(*(char**)arg, 64, "(%d) %s", audio_id, lang);
1869 return M_PROPERTY_OK;
1871 case M_PROPERTY_STEP_UP:
1872 case M_PROPERTY_SET:
1873 if(action==M_PROPERTY_SET && arg)
1874 tmp = *((int*)arg);
1875 else
1876 tmp = -1;
1877 current_id = demuxer->audio->id;
1878 audio_id = demuxer_switch_audio(demuxer, tmp);
1879 if(audio_id == -2 || (audio_id > -1 && demuxer->audio->id != current_id && current_id != -2))
1880 uninit_player(INITED_AO | INITED_ACODEC);
1881 if(audio_id > -1 && demuxer->audio->id != current_id) {
1882 sh_audio_t *sh2;
1883 sh2 = demuxer->a_streams[demuxer->audio->id];
1884 if(sh2) {
1885 sh2->ds = demuxer->audio;
1886 sh_audio = sh2;
1887 reinit_audio_chain();
1890 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_TRACK=%d\n", audio_id);
1891 return M_PROPERTY_OK;
1892 default:
1893 return M_PROPERTY_NOT_IMPLEMENTED;
1898 static int reinit_video_chain(void);
1899 /// Selected video id (RW)
1900 static int mp_property_video(m_option_t* prop,int action,void* arg) {
1901 int current_id = -1, tmp;
1903 switch(action) {
1904 case M_PROPERTY_GET:
1905 if(!sh_video) return M_PROPERTY_UNAVAILABLE;
1906 if(!arg) return M_PROPERTY_ERROR;
1907 *(int*)arg = video_id;
1908 return M_PROPERTY_OK;
1909 case M_PROPERTY_PRINT:
1910 if(!sh_video) return M_PROPERTY_UNAVAILABLE;
1911 if(!arg) return M_PROPERTY_ERROR;
1913 if (video_id < 0)
1914 *(char**)arg = strdup(MSGTR_Disabled);
1915 else {
1916 char lang[40] = MSGTR_Unknown;
1917 *(char**)arg = malloc(64);
1918 snprintf(*(char**)arg, 64, "(%d) %s", video_id, lang);
1920 return M_PROPERTY_OK;
1922 case M_PROPERTY_STEP_UP:
1923 case M_PROPERTY_SET:
1924 current_id = demuxer->video->id;
1925 if(action==M_PROPERTY_SET && arg)
1926 tmp = *((int*)arg);
1927 else
1928 tmp = -1;
1929 video_id = demuxer_switch_video(demuxer, tmp);
1930 if(video_id == -2 || (video_id > -1 && demuxer->video->id != current_id && current_id != -2))
1931 uninit_player(INITED_VCODEC | (fixed_vo && video_id != -2 ? 0 : INITED_VO));
1932 if(video_id > -1 && demuxer->video->id != current_id) {
1933 sh_video_t *sh2;
1934 sh2 = demuxer->v_streams[demuxer->video->id];
1935 if(sh2) {
1936 sh2->ds = demuxer->video;
1937 sh_video = sh2;
1938 reinit_video_chain();
1941 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_TRACK=%d\n", video_id);
1942 return M_PROPERTY_OK;
1944 default:
1945 return M_PROPERTY_NOT_IMPLEMENTED;
1949 static int mp_property_program(m_option_t* prop,int action,void* arg) {
1950 demux_program_t prog;
1952 switch(action) {
1953 case M_PROPERTY_STEP_UP:
1954 case M_PROPERTY_SET:
1955 if(action==M_PROPERTY_SET && arg)
1956 prog.progid = *((int*)arg);
1957 else
1958 prog.progid = -1;
1959 if(demux_control(demuxer, DEMUXER_CTRL_IDENTIFY_PROGRAM, &prog) == DEMUXER_CTRL_NOTIMPL)
1960 return M_PROPERTY_ERROR;
1962 mp_property_do("switch_audio", M_PROPERTY_SET, &prog.aid);
1963 mp_property_do("switch_video", M_PROPERTY_SET, &prog.vid);
1964 return M_PROPERTY_OK;
1966 default:
1967 return M_PROPERTY_NOT_IMPLEMENTED;
1971 ///@}
1973 /// \defgroup VideoProperties Video properties
1974 /// \ingroup Properties
1975 ///@{
1977 /// Fullscreen state (RW)
1978 static int mp_property_fullscreen(m_option_t* prop,int action,void* arg) {
1980 if(!video_out) return M_PROPERTY_UNAVAILABLE;
1982 switch(action) {
1983 case M_PROPERTY_SET:
1984 if(!arg) return M_PROPERTY_ERROR;
1985 M_PROPERTY_CLAMP(prop,*(int*)arg);
1986 if(vo_fs == !!*(int*)arg) return M_PROPERTY_OK;
1987 case M_PROPERTY_STEP_UP:
1988 case M_PROPERTY_STEP_DOWN:
1989 #ifdef HAVE_NEW_GUI
1990 if(use_gui) guiGetEvent(guiIEvent,(char*)MP_CMD_GUI_FULLSCREEN);
1991 else
1992 #endif
1993 if(vo_config_count) video_out->control(VOCTRL_FULLSCREEN, 0);
1994 return M_PROPERTY_OK;
1995 default:
1996 return m_property_flag(prop,action,arg,&vo_fs);
2000 static int mp_property_deinterlace(m_option_t* prop,int action,void* arg) {
2001 int deinterlace;
2002 vf_instance_t *vf;
2003 if (!sh_video || !sh_video->vfilter) return M_PROPERTY_UNAVAILABLE;
2004 vf = sh_video->vfilter;
2005 switch(action) {
2006 case M_PROPERTY_GET:
2007 if(!arg) return M_PROPERTY_ERROR;
2008 vf->control(vf, VFCTRL_GET_DEINTERLACE, arg);
2009 return M_PROPERTY_OK;
2010 case M_PROPERTY_SET:
2011 if(!arg) return M_PROPERTY_ERROR;
2012 M_PROPERTY_CLAMP(prop,*(int*)arg);
2013 vf->control(vf, VFCTRL_SET_DEINTERLACE, arg);
2014 return M_PROPERTY_OK;
2015 case M_PROPERTY_STEP_UP:
2016 case M_PROPERTY_STEP_DOWN:
2017 vf->control(vf, VFCTRL_GET_DEINTERLACE, &deinterlace);
2018 deinterlace = !deinterlace;
2019 vf->control(vf, VFCTRL_SET_DEINTERLACE, &deinterlace);
2020 return M_PROPERTY_OK;
2022 return M_PROPERTY_NOT_IMPLEMENTED;
2025 /// Panscan (RW)
2026 static int mp_property_panscan(m_option_t* prop,int action,void* arg) {
2028 if(!video_out || video_out->control(VOCTRL_GET_PANSCAN,NULL ) != VO_TRUE)
2029 return M_PROPERTY_UNAVAILABLE;
2031 switch(action) {
2032 case M_PROPERTY_SET:
2033 if(!arg) return M_PROPERTY_ERROR;
2034 M_PROPERTY_CLAMP(prop,*(float*)arg);
2035 vo_panscan = *(float*)arg;
2036 video_out->control(VOCTRL_SET_PANSCAN,NULL);
2037 return M_PROPERTY_OK;
2038 case M_PROPERTY_STEP_UP:
2039 case M_PROPERTY_STEP_DOWN:
2040 vo_panscan += (arg ? *(float*)arg : 0.1) *
2041 (action == M_PROPERTY_STEP_DOWN ? -1 : 1);
2042 if(vo_panscan > 1) vo_panscan = 1;
2043 else if(vo_panscan < 0) vo_panscan = 0;
2044 video_out->control(VOCTRL_SET_PANSCAN,NULL);
2045 return M_PROPERTY_OK;
2046 default:
2047 return m_property_float_range(prop,action,arg,&vo_panscan);
2051 /// Helper to set vo flags.
2052 /** \ingroup PropertyImplHelper
2054 static int mp_property_vo_flag(m_option_t* prop,int action,void* arg,
2055 int vo_ctrl,int* vo_var) {
2057 if(!video_out) return M_PROPERTY_UNAVAILABLE;
2059 switch(action) {
2060 case M_PROPERTY_SET:
2061 if(!arg) return M_PROPERTY_ERROR;
2062 M_PROPERTY_CLAMP(prop,*(int*)arg);
2063 if(*vo_var == !!*(int*)arg) return M_PROPERTY_OK;
2064 case M_PROPERTY_STEP_UP:
2065 case M_PROPERTY_STEP_DOWN:
2066 if(vo_config_count) video_out->control(vo_ctrl, 0);
2067 return M_PROPERTY_OK;
2068 default:
2069 return m_property_flag(prop,action,arg,vo_var);
2073 /// Window always on top (RW)
2074 static int mp_property_ontop(m_option_t* prop,int action,void* arg) {
2075 return mp_property_vo_flag(prop,action,arg,VOCTRL_ONTOP,&vo_ontop);
2078 /// Display in the root window (RW)
2079 static int mp_property_rootwin(m_option_t* prop,int action,void* arg) {
2080 return mp_property_vo_flag(prop,action,arg,VOCTRL_ROOTWIN,&vo_rootwin);
2083 /// Show window borders (RW)
2084 static int mp_property_border(m_option_t* prop,int action,void* arg) {
2085 return mp_property_vo_flag(prop,action,arg,VOCTRL_BORDER,&vo_border);
2088 /// Framedropping state (RW)
2089 static int mp_property_framedropping(m_option_t* prop,int action,void* arg) {
2091 if(!sh_video) return M_PROPERTY_UNAVAILABLE;
2093 switch(action) {
2094 case M_PROPERTY_PRINT:
2095 if(!arg) return M_PROPERTY_ERROR;
2096 *(char**)arg = strdup(frame_dropping == 1 ? MSGTR_Enabled :
2097 (frame_dropping == 2 ? MSGTR_HardFrameDrop : MSGTR_Disabled));
2098 return M_PROPERTY_OK;
2099 default:
2100 return m_property_choice(prop,action,arg,&frame_dropping);
2104 /// Color settings, try to use vf/vo then fall back on TV. (RW)
2105 static int mp_property_gamma(m_option_t* prop,int action,void* arg) {
2106 int* gamma = prop->priv, r;
2108 if(!sh_video) return M_PROPERTY_UNAVAILABLE;
2110 if(gamma[0] == 1000) {
2111 gamma[0] = 0;
2112 get_video_colors (sh_video, prop->name, gamma);
2115 switch(action) {
2116 case M_PROPERTY_SET:
2117 if(!arg) return M_PROPERTY_ERROR;
2118 M_PROPERTY_CLAMP(prop,*(int*)arg);
2119 *gamma = *(int*)arg;
2120 r = set_video_colors(sh_video, prop->name, *gamma);
2121 if(r <= 0) break;
2122 return r;
2123 case M_PROPERTY_GET:
2124 if(!arg) return M_PROPERTY_ERROR;
2125 r = get_video_colors (sh_video, prop->name, arg);
2126 if(r <= 0) break;
2127 return r;
2128 case M_PROPERTY_STEP_UP:
2129 case M_PROPERTY_STEP_DOWN:
2130 *gamma += (arg ? *(int*)arg : 1) *
2131 (action == M_PROPERTY_STEP_DOWN ? -1 : 1);
2132 M_PROPERTY_CLAMP(prop,*gamma);
2133 r = set_video_colors(sh_video, prop->name, *gamma);
2134 if(r <= 0) break;
2135 return r;
2136 default:
2137 return M_PROPERTY_NOT_IMPLEMENTED;
2140 #ifdef USE_TV
2141 if(demuxer->type == DEMUXER_TYPE_TV) {
2142 int l = strlen(prop->name);
2143 char tv_prop[3+l+1];
2144 sprintf(tv_prop,"tv_%s",prop->name);
2145 return mp_property_do(tv_prop,action,arg);
2147 #endif
2149 return M_PROPERTY_UNAVAILABLE;
2152 /// VSync (RW)
2153 static int mp_property_vsync(m_option_t* prop,int action,void* arg) {
2154 return m_property_flag(prop,action,arg,&vo_vsync);
2157 /// Video codec tag (RO)
2158 static int mp_property_video_format(m_option_t* prop,int action,void* arg) {
2159 if(!sh_video) return M_PROPERTY_UNAVAILABLE;
2160 return m_property_int_ro(prop,action,arg,sh_video->format);
2163 /// Video bitrate (RO)
2164 static int mp_property_video_bitrate(m_option_t* prop,int action,void* arg) {
2165 if(!sh_video) return M_PROPERTY_UNAVAILABLE;
2166 return m_property_int_ro(prop,action,arg,sh_video->i_bps);
2169 /// Video display width (RO)
2170 static int mp_property_width(m_option_t* prop,int action,void* arg) {
2171 if(!sh_video) return M_PROPERTY_UNAVAILABLE;
2172 return m_property_int_ro(prop,action,arg,sh_video->disp_w);
2175 /// Video display height (RO)
2176 static int mp_property_height(m_option_t* prop,int action,void* arg) {
2177 if(!sh_video) return M_PROPERTY_UNAVAILABLE;
2178 return m_property_int_ro(prop,action,arg,sh_video->disp_h);
2181 /// Video fps (RO)
2182 static int mp_property_fps(m_option_t* prop,int action,void* arg) {
2183 if(!sh_video) return M_PROPERTY_UNAVAILABLE;
2184 return m_property_float_ro(prop,action,arg,sh_video->fps);
2187 /// Video aspect (RO)
2188 static int mp_property_aspect(m_option_t* prop,int action,void* arg) {
2189 if(!sh_video) return M_PROPERTY_UNAVAILABLE;
2190 return m_property_float_ro(prop,action,arg,sh_video->aspect);
2193 ///@}
2195 /// \defgroup SubProprties Subtitles properties
2196 /// \ingroup Properties
2197 ///@{
2199 /// Text subtitle position (RW)
2200 static int mp_property_sub_pos(m_option_t* prop,int action,void* arg) {
2201 if(!sh_video) return M_PROPERTY_UNAVAILABLE;
2203 switch(action) {
2204 case M_PROPERTY_SET:
2205 if(!arg) return M_PROPERTY_ERROR;
2206 case M_PROPERTY_STEP_UP:
2207 case M_PROPERTY_STEP_DOWN:
2208 vo_osd_changed(OSDTYPE_SUBTITLE);
2209 default:
2210 return m_property_int_range(prop,action,arg,&sub_pos);
2214 /// Selected subtitles (RW)
2215 static int mp_property_sub(m_option_t* prop,int action,void* arg) {
2216 int source = -1, reset_spu = 0;
2217 char* sub_name;
2219 if(global_sub_size <= 0) return M_PROPERTY_UNAVAILABLE;
2221 switch(action) {
2222 case M_PROPERTY_GET:
2223 if(!arg) return M_PROPERTY_ERROR;
2224 *(int*)arg = global_sub_pos;
2225 return M_PROPERTY_OK;
2226 case M_PROPERTY_PRINT:
2227 if(!arg) return M_PROPERTY_ERROR;
2228 *(char**)arg = malloc(64);
2229 (*(char**)arg)[63] = 0;
2230 sub_name = 0;
2231 if(subdata)
2232 sub_name = subdata->filename;
2233 #ifdef USE_ASS
2234 if (ass_track && ass_track->name)
2235 sub_name = ass_track->name;
2236 #endif
2237 if(sub_name) {
2238 char *tmp,*tmp2;
2239 tmp = sub_name;
2240 if ((tmp2 = strrchr(tmp, '/')))
2241 tmp = tmp2+1;
2243 snprintf(*(char**)arg, 63, "(%d) %s%s",
2244 set_of_sub_pos + 1,
2245 strlen(tmp) < 20 ? "" : "...",
2246 strlen(tmp) < 20 ? tmp : tmp+strlen(tmp)-19);
2247 return M_PROPERTY_OK;
2250 #ifdef USE_DVDNAV
2251 if(stream->type==STREAMTYPE_DVDNAV) {
2252 if(vo_spudec && dvdsub_id >= 0) {
2253 unsigned char lang[3];
2254 if(dvdnav_lang_from_sid(stream, dvdsub_id, lang)) {
2255 snprintf(*(char**)arg, 63, "(%d) %s", dvdsub_id, lang);
2256 return M_PROPERTY_OK;
2260 #endif
2262 if (demuxer->type == DEMUXER_TYPE_MATROSKA && dvdsub_id >= 0) {
2263 char lang[40] = MSGTR_Unknown;
2264 demux_mkv_get_sub_lang(demuxer, dvdsub_id, lang, 9);
2265 snprintf(*(char**)arg, 63, "(%d) %s", dvdsub_id, lang);
2266 return M_PROPERTY_OK;
2268 #ifdef HAVE_OGGVORBIS
2269 if (demuxer->type == DEMUXER_TYPE_OGG && d_dvdsub && dvdsub_id >= 0) {
2270 char *lang = demux_ogg_sub_lang(demuxer, dvdsub_id);
2271 if (!lang) lang = MSGTR_Unknown;
2272 snprintf(*(char**)arg, 63, "(%d) %s",
2273 dvdsub_id, lang);
2274 return M_PROPERTY_OK;
2276 #endif
2277 if (vo_vobsub && vobsub_id >= 0) {
2278 const char *language = MSGTR_Unknown;
2279 language = vobsub_get_id(vo_vobsub, (unsigned int) vobsub_id);
2280 snprintf(*(char**)arg, 63, "(%d) %s",
2281 vobsub_id, language ? language : MSGTR_Unknown);
2282 return M_PROPERTY_OK;
2284 #ifdef USE_DVDREAD
2285 if (vo_spudec && stream->type == STREAMTYPE_DVD && dvdsub_id >= 0) {
2286 char lang[3];
2287 int code = dvd_lang_from_sid(stream, dvdsub_id);
2288 lang[0] = code >> 8;
2289 lang[1] = code;
2290 lang[2] = 0;
2291 snprintf(*(char**)arg, 63, "(%d) %s",
2292 dvdsub_id, lang);
2293 return M_PROPERTY_OK;
2295 #endif
2296 if (dvdsub_id >= 0) {
2297 snprintf(*(char**)arg, 63, "(%d) %s", dvdsub_id, MSGTR_Unknown);
2298 return M_PROPERTY_OK;
2300 snprintf(*(char**)arg, 63, MSGTR_Disabled);
2301 return M_PROPERTY_OK;
2303 case M_PROPERTY_SET:
2304 if(!arg) return M_PROPERTY_ERROR;
2305 if(*(int*)arg < -1) *(int*)arg = -1;
2306 else if(*(int*)arg >= global_sub_size) *(int*)arg = global_sub_size-1;
2307 global_sub_pos = *(int*)arg;
2308 break;
2309 case M_PROPERTY_STEP_UP:
2310 global_sub_pos += 2;
2311 global_sub_pos = (global_sub_pos % (global_sub_size+1)) - 1;
2312 break;
2313 case M_PROPERTY_STEP_DOWN:
2314 global_sub_pos += global_sub_size+1;
2315 global_sub_pos = (global_sub_pos % (global_sub_size+1)) - 1;
2316 break;
2317 default:
2318 return M_PROPERTY_NOT_IMPLEMENTED;
2321 if (global_sub_pos >= 0)
2322 source = sub_source();
2324 mp_msg(MSGT_CPLAYER, MSGL_DBG3,
2325 "subtitles: %d subs, (v@%d s@%d d@%d), @%d, source @%d\n",
2326 global_sub_size, global_sub_indices[SUB_SOURCE_VOBSUB],
2327 global_sub_indices[SUB_SOURCE_SUBS],
2328 global_sub_indices[SUB_SOURCE_DEMUX],
2329 global_sub_pos, source);
2331 set_of_sub_pos = -1;
2332 subdata = NULL;
2333 vo_sub_last = vo_sub = NULL;
2335 vobsub_id = -1;
2336 dvdsub_id = -1;
2337 if (d_dvdsub) {
2338 if(d_dvdsub->id > -2) reset_spu = 1;
2339 d_dvdsub->id = -2;
2341 #ifdef USE_ASS
2342 ass_track = 0;
2343 #endif
2345 if (source == SUB_SOURCE_VOBSUB) {
2346 vobsub_id = global_sub_pos - global_sub_indices[SUB_SOURCE_VOBSUB];
2347 } else if (source == SUB_SOURCE_SUBS) {
2348 set_of_sub_pos = global_sub_pos - global_sub_indices[SUB_SOURCE_SUBS];
2349 #ifdef USE_ASS
2350 if (ass_enabled && set_of_ass_tracks[set_of_sub_pos])
2351 ass_track = set_of_ass_tracks[set_of_sub_pos];
2352 else
2353 #endif
2355 subdata = set_of_subtitles[set_of_sub_pos];
2356 vo_osd_changed(OSDTYPE_SUBTITLE);
2358 } else if (source == SUB_SOURCE_DEMUX) {
2359 dvdsub_id = global_sub_pos - global_sub_indices[SUB_SOURCE_DEMUX];
2360 if (d_dvdsub) {
2361 #ifdef USE_DVDREAD
2362 if (vo_spudec && stream->type == STREAMTYPE_DVD) {
2363 d_dvdsub->id = dvdsub_id;
2364 spudec_reset(vo_spudec);
2366 #endif
2368 #ifdef USE_DVDNAV
2369 if (vo_spudec && stream->type == STREAMTYPE_DVDNAV) {
2370 d_dvdsub->id = dvdsub_id;
2371 spudec_reset(vo_spudec);
2373 #endif
2374 if (stream->type != STREAMTYPE_DVD && stream->type != STREAMTYPE_DVDNAV) {
2375 int i = 0;
2376 for (d_dvdsub->id = 0; d_dvdsub->id < MAX_S_STREAMS; d_dvdsub->id++) {
2377 if (demuxer->s_streams[d_dvdsub->id]) {
2378 if (i == dvdsub_id) break;
2379 i++;
2382 d_dvdsub->sh = demuxer->s_streams[d_dvdsub->id];
2384 if (demuxer->type == DEMUXER_TYPE_MATROSKA) {
2385 d_dvdsub->id = demux_mkv_change_subs(demuxer, dvdsub_id);
2387 if (d_dvdsub->sh && d_dvdsub->id >= 0) {
2388 sh_sub_t *sh = d_dvdsub->sh;
2389 if (sh->type == 'v')
2390 init_vo_spudec();
2391 #ifdef USE_ASS
2392 else if (ass_enabled && sh->type == 'a')
2393 ass_track = sh->ass_track;
2394 #endif
2397 } else { // off
2398 vo_osd_changed(OSDTYPE_SUBTITLE);
2399 if(vo_spudec) vo_osd_changed(OSDTYPE_SPU);
2401 #ifdef USE_DVDREAD
2402 if (vo_spudec && (stream->type == STREAMTYPE_DVD || stream->type == STREAMTYPE_DVDNAV) && dvdsub_id < 0 && reset_spu) {
2403 dvdsub_id = -2;
2404 d_dvdsub->id = dvdsub_id;
2405 spudec_reset(vo_spudec);
2407 #endif
2409 return M_PROPERTY_OK;
2412 /// Subtitle delay (RW)
2413 static int mp_property_sub_delay(m_option_t* prop,int action,void* arg) {
2414 if(!sh_video) return M_PROPERTY_UNAVAILABLE;
2415 return m_property_delay(prop,action,arg,&sub_delay);
2418 /// Alignment of text subtitles (RW)
2419 static int mp_property_sub_alignment(m_option_t* prop,int action,void* arg) {
2420 char* name[] = { MSGTR_Top, MSGTR_Center, MSGTR_Bottom };
2422 if(!sh_video || global_sub_pos < 0 || sub_source() != SUB_SOURCE_SUBS)
2423 return M_PROPERTY_UNAVAILABLE;
2425 switch(action) {
2426 case M_PROPERTY_PRINT:
2427 if(!arg) return M_PROPERTY_ERROR;
2428 M_PROPERTY_CLAMP(prop,sub_alignment);
2429 *(char**)arg = strdup(name[sub_alignment]);
2430 return M_PROPERTY_OK;
2431 case M_PROPERTY_SET:
2432 if(!arg) return M_PROPERTY_ERROR;
2433 case M_PROPERTY_STEP_UP:
2434 case M_PROPERTY_STEP_DOWN:
2435 vo_osd_changed(OSDTYPE_SUBTITLE);
2436 default:
2437 return m_property_choice(prop,action,arg,&sub_alignment);
2441 /// Subtitle visibility (RW)
2442 static int mp_property_sub_visibility(m_option_t* prop,int action,void* arg) {
2443 if(!sh_video) return M_PROPERTY_UNAVAILABLE;
2445 switch(action) {
2446 case M_PROPERTY_SET:
2447 if(!arg) return M_PROPERTY_ERROR;
2448 case M_PROPERTY_STEP_UP:
2449 case M_PROPERTY_STEP_DOWN:
2450 vo_osd_changed(OSDTYPE_SUBTITLE);
2451 if(vo_spudec) vo_osd_changed(OSDTYPE_SPU);
2452 default:
2453 return m_property_flag(prop,action,arg,&sub_visibility);
2457 /// Show only forced subtitles (RW)
2458 static int mp_property_sub_forced_only(m_option_t* prop,int action,void* arg) {
2459 if(!vo_spudec) return M_PROPERTY_UNAVAILABLE;
2461 switch(action) {
2462 case M_PROPERTY_SET:
2463 if(!arg) return M_PROPERTY_ERROR;
2464 case M_PROPERTY_STEP_UP:
2465 case M_PROPERTY_STEP_DOWN:
2466 m_property_flag(prop,action,arg,&forced_subs_only);
2467 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
2468 return M_PROPERTY_OK;
2469 default:
2470 return m_property_flag(prop,action,arg,&forced_subs_only);
2475 ///@}
2477 /// \defgroup TVProperties TV properties
2478 /// \ingroup Properties
2479 ///@{
2481 #ifdef USE_TV
2483 /// TV color settings (RW)
2484 static int mp_property_tv_color(m_option_t* prop,int action,void* arg) {
2485 int r,val;
2486 tvi_handle_t* tvh = demuxer->priv;
2487 if(demuxer->type != DEMUXER_TYPE_TV || !tvh) return M_PROPERTY_UNAVAILABLE;
2489 switch(action) {
2490 case M_PROPERTY_SET:
2491 if(!arg) return M_PROPERTY_ERROR;
2492 M_PROPERTY_CLAMP(prop,*(int*)arg);
2493 return tv_set_color_options(tvh,(int)prop->priv,*(int*)arg);
2494 case M_PROPERTY_GET:
2495 return tv_get_color_options(tvh,(int)prop->priv,arg);
2496 case M_PROPERTY_STEP_UP:
2497 case M_PROPERTY_STEP_DOWN:
2498 if((r = tv_get_color_options(tvh,(int)prop->priv,&val)) >= 0) {
2499 if(!r) return M_PROPERTY_ERROR;
2500 val += (arg ? *(int*)arg : 1) *
2501 (action == M_PROPERTY_STEP_DOWN ? -1 : 1);
2502 M_PROPERTY_CLAMP(prop,val);
2503 return tv_set_color_options(tvh,(int)prop->priv,val);
2505 return M_PROPERTY_ERROR;
2507 return M_PROPERTY_NOT_IMPLEMENTED;
2510 #endif
2512 ///@}
2514 /// All properties available in MPlayer.
2515 /** \ingroup Properties
2517 static m_option_t mp_properties[] = {
2518 // General
2519 { "osdlevel", mp_property_osdlevel, CONF_TYPE_INT,
2520 M_OPT_RANGE, 0, 3, NULL },
2521 { "speed", mp_property_playback_speed, CONF_TYPE_FLOAT,
2522 M_OPT_RANGE, 0.01, 100.0, NULL },
2523 { "filename", mp_property_filename, CONF_TYPE_STRING,
2524 0, 0, 0, NULL },
2525 { "path", mp_property_path, CONF_TYPE_STRING,
2526 0, 0, 0, NULL },
2527 { "demuxer", mp_property_demuxer, CONF_TYPE_STRING,
2528 0, 0, 0, NULL },
2529 { "stream_pos", mp_property_stream_pos, CONF_TYPE_POSITION,
2530 M_OPT_MIN, 0, 0, NULL },
2531 { "stream_start", mp_property_stream_start, CONF_TYPE_POSITION,
2532 M_OPT_MIN, 0, 0, NULL },
2533 { "stream_end", mp_property_stream_end, CONF_TYPE_POSITION,
2534 M_OPT_MIN, 0, 0, NULL },
2535 { "stream_length", mp_property_stream_length, CONF_TYPE_POSITION,
2536 M_OPT_MIN, 0, 0, NULL },
2537 { "length", mp_property_length, CONF_TYPE_DOUBLE,
2538 0, 0, 0, NULL },
2540 // Audio
2541 { "volume", mp_property_volume, CONF_TYPE_FLOAT,
2542 M_OPT_RANGE, 0, 100, NULL },
2543 { "mute", mp_property_mute, CONF_TYPE_FLAG,
2544 M_OPT_RANGE, 0, 1, NULL },
2545 { "audio_delay", mp_property_audio_delay, CONF_TYPE_FLOAT,
2546 M_OPT_RANGE, -100, 100, NULL },
2547 { "audio_format", mp_property_audio_format, CONF_TYPE_INT,
2548 0, 0, 0, NULL },
2549 { "audio_bitrate", mp_property_audio_bitrate, CONF_TYPE_INT,
2550 0, 0, 0, NULL },
2551 { "samplerate", mp_property_samplerate, CONF_TYPE_INT,
2552 0, 0, 0, NULL },
2553 { "channels", mp_property_channels, CONF_TYPE_INT,
2554 0, 0, 0, NULL },
2555 { "switch_audio", mp_property_audio, CONF_TYPE_INT,
2556 CONF_RANGE, -2, MAX_A_STREAMS-1, NULL },
2558 // Video
2559 { "fullscreen", mp_property_fullscreen, CONF_TYPE_FLAG,
2560 M_OPT_RANGE, 0, 1, NULL },
2561 { "deinterlace", mp_property_deinterlace, CONF_TYPE_FLAG,
2562 M_OPT_RANGE, 0, 1, NULL },
2563 { "ontop", mp_property_ontop, CONF_TYPE_FLAG,
2564 M_OPT_RANGE, 0, 1, NULL },
2565 { "rootwin", mp_property_rootwin, CONF_TYPE_FLAG,
2566 M_OPT_RANGE, 0, 1, NULL },
2567 { "border", mp_property_border, CONF_TYPE_FLAG,
2568 M_OPT_RANGE, 0, 1, NULL },
2569 { "framedropping", mp_property_framedropping, CONF_TYPE_INT,
2570 M_OPT_RANGE, 0, 2, NULL },
2571 { "gamma", mp_property_gamma, CONF_TYPE_INT,
2572 M_OPT_RANGE, -100, 100, &vo_gamma_gamma },
2573 { "brightness", mp_property_gamma, CONF_TYPE_INT,
2574 M_OPT_RANGE, -100, 100, &vo_gamma_brightness },
2575 { "contrast", mp_property_gamma, CONF_TYPE_INT,
2576 M_OPT_RANGE, -100, 100, &vo_gamma_contrast },
2577 { "saturation", mp_property_gamma, CONF_TYPE_INT,
2578 M_OPT_RANGE, -100, 100, &vo_gamma_saturation },
2579 { "hue", mp_property_gamma, CONF_TYPE_INT,
2580 M_OPT_RANGE, -100, 100, &vo_gamma_hue },
2581 { "panscan", mp_property_panscan, CONF_TYPE_FLOAT,
2582 M_OPT_RANGE, 0, 1, NULL },
2583 { "vsync", mp_property_vsync, CONF_TYPE_FLAG,
2584 M_OPT_RANGE, 0, 1, NULL },
2585 { "video_format", mp_property_video_format, CONF_TYPE_INT,
2586 0, 0, 0, NULL },
2587 { "video_bitrate", mp_property_video_bitrate, CONF_TYPE_INT,
2588 0, 0, 0, NULL },
2589 { "width", mp_property_width, CONF_TYPE_INT,
2590 0, 0, 0, NULL },
2591 { "height", mp_property_height, CONF_TYPE_INT,
2592 0, 0, 0, NULL },
2593 { "fps", mp_property_fps, CONF_TYPE_FLOAT,
2594 0, 0, 0, NULL },
2595 { "aspect", mp_property_aspect, CONF_TYPE_FLOAT,
2596 0, 0, 0, NULL },
2597 { "switch_video", mp_property_video, CONF_TYPE_INT,
2598 CONF_RANGE, -2, MAX_V_STREAMS-1, NULL },
2599 { "switch_program", mp_property_program, CONF_TYPE_INT,
2600 CONF_RANGE, -1, 65535, NULL },
2602 // Subs
2603 { "sub", mp_property_sub, CONF_TYPE_INT,
2604 M_OPT_MIN, -1, 0, NULL },
2605 { "sub_delay", mp_property_sub_delay, CONF_TYPE_FLOAT,
2606 0, 0, 0, NULL },
2607 { "sub_pos", mp_property_sub_pos, CONF_TYPE_INT,
2608 M_OPT_RANGE, 0, 100, NULL },
2609 { "sub_alignment", mp_property_sub_alignment, CONF_TYPE_INT,
2610 M_OPT_RANGE, 0, 2, NULL },
2611 { "sub_visibility", mp_property_sub_visibility, CONF_TYPE_FLAG,
2612 M_OPT_RANGE, 0, 1, NULL },
2613 { "sub_forced_only", mp_property_sub_forced_only, CONF_TYPE_FLAG,
2614 M_OPT_RANGE, 0, 1, NULL },
2616 #ifdef USE_TV
2617 { "tv_brightness", mp_property_tv_color, CONF_TYPE_INT,
2618 M_OPT_RANGE, -100, 100, (void*)TV_COLOR_BRIGHTNESS },
2619 { "tv_contrast", mp_property_tv_color, CONF_TYPE_INT,
2620 M_OPT_RANGE, -100, 100, (void*)TV_COLOR_CONTRAST },
2621 { "tv_saturation", mp_property_tv_color, CONF_TYPE_INT,
2622 M_OPT_RANGE, -100, 100, (void*)TV_COLOR_SATURATION },
2623 { "tv_hue", mp_property_tv_color, CONF_TYPE_INT,
2624 M_OPT_RANGE, -100, 100, (void*)TV_COLOR_HUE },
2625 #endif
2627 { NULL, NULL, NULL, 0, 0, 0, NULL }
2630 m_option_t* mp_property_find(const char* name) {
2631 return m_option_list_find(mp_properties,name);
2634 int mp_property_do(const char* name,int action, void* val) {
2635 m_option_t* p = mp_property_find(name);
2636 if(!p) return M_PROPERTY_UNAVAILABLE;
2637 return m_property_do(p,action,val);
2640 ///@}
2641 // Properties group
2645 * \defgroup Command2Property Command to property bridge
2647 * It is used to handle most commands that just set a property
2648 * and optionally display something on the OSD.
2649 * Two kinds of commands are handled: adjust or toggle.
2651 * Adjust commands take 1 or 2 parameters: <value> <abs>
2652 * If <abs> is non-zero the property is set to the given value
2653 * otherwise it is adjusted.
2655 * Toggle commands take 0 or 1 parameters. With no parameter
2656 * or a value less than the property minimum it just steps the
2657 * property to its next value. Otherwise it sets it to the given
2658 * value.
2663 /// List of the commands that can be handled by setting a property.
2664 static struct {
2665 /// property name
2666 const char* name;
2667 /// cmd id
2668 int cmd;
2669 /// set/adjust or toggle command
2670 int toggle;
2671 /// progressbar type
2672 int osd_progbar;
2673 /// osd msg id if it must be shared
2674 int osd_id;
2675 /// osd msg template
2676 const char* osd_msg;
2677 } set_prop_cmd[] = {
2678 // audio
2679 { "volume", MP_CMD_VOLUME, 0, OSD_VOLUME, -1, MSGTR_Volume },
2680 { "mute", MP_CMD_MUTE, 1, 0, -1, MSGTR_MuteStatus },
2681 { "audio_delay", MP_CMD_AUDIO_DELAY, 0, 0, -1, MSGTR_AVDelayStatus },
2682 { "switch_audio", MP_CMD_SWITCH_AUDIO, 1, 0, -1, MSGTR_OSDAudio },
2683 // video
2684 { "fullscreen", MP_CMD_VO_FULLSCREEN, 1, 0, -1, NULL },
2685 { "panscan", MP_CMD_PANSCAN, 0, OSD_PANSCAN, -1, MSGTR_Panscan },
2686 { "ontop", MP_CMD_VO_ONTOP, 1, 0, -1, MSGTR_OnTopStatus },
2687 { "rootwin", MP_CMD_VO_ROOTWIN, 1, 0, -1, MSGTR_RootwinStatus },
2688 { "border", MP_CMD_VO_BORDER, 1, 0, -1, MSGTR_BorderStatus },
2689 { "framedropping", MP_CMD_FRAMEDROPPING, 1, 0, -1, MSGTR_FramedroppingStatus },
2690 { "gamma", MP_CMD_GAMMA, 0, OSD_BRIGHTNESS, -1, MSGTR_Gamma },
2691 { "brightness", MP_CMD_BRIGHTNESS, 0, OSD_BRIGHTNESS, -1, MSGTR_Brightness },
2692 { "contrast", MP_CMD_CONTRAST, 0, OSD_CONTRAST, -1, MSGTR_Contrast },
2693 { "saturation", MP_CMD_SATURATION, 0, OSD_SATURATION, -1, MSGTR_Saturation },
2694 { "hue", MP_CMD_HUE, 0, OSD_HUE, -1, MSGTR_Hue },
2695 { "vsync", MP_CMD_SWITCH_VSYNC, 1, 0, -1, MSGTR_VSyncStatus },
2696 // subs
2697 { "sub", MP_CMD_SUB_SELECT, 1, 0, -1, MSGTR_SubSelectStatus },
2698 { "sub_pos", MP_CMD_SUB_POS, 0, 0, -1, MSGTR_SubPosStatus },
2699 { "sub_alignment", MP_CMD_SUB_ALIGNMENT, 1, 0, -1, MSGTR_SubAlignStatus },
2700 { "sub_delay", MP_CMD_SUB_DELAY, 0, 0, OSD_MSG_SUB_DELAY, MSGTR_SubDelayStatus },
2701 { "sub_visibility", MP_CMD_SUB_VISIBILITY, 1, 0, -1, MSGTR_SubVisibleStatus },
2702 { "sub_forced_only", MP_CMD_SUB_FORCED_ONLY, 1, 0, -1, MSGTR_SubForcedOnlyStatus },
2703 #ifdef USE_TV
2704 { "tv_brightness", MP_CMD_TV_SET_BRIGHTNESS, 0, OSD_BRIGHTNESS, -1, MSGTR_Brightness },
2705 { "tv_hue", MP_CMD_TV_SET_HUE, 0, OSD_HUE, -1, MSGTR_Hue },
2706 { "tv_saturation", MP_CMD_TV_SET_SATURATION, 0, OSD_SATURATION, -1, MSGTR_Saturation },
2707 { "tv_contrast", MP_CMD_TV_SET_CONTRAST, 0, OSD_CONTRAST, -1, MSGTR_Contrast },
2708 #endif
2709 { NULL, 0, 0, 0, -1, NULL }
2712 /// Handle commands that set a property.
2713 static int set_property_command(mp_cmd_t* cmd) {
2714 int i,r;
2715 m_option_t* prop;
2717 // look for the command
2718 for(i = 0 ; set_prop_cmd[i].name ; i++)
2719 if(set_prop_cmd[i].cmd == cmd->id) break;
2720 if(!set_prop_cmd[i].name) return 0;
2722 // get the property
2723 prop = mp_property_find(set_prop_cmd[i].name);
2724 if(!prop) return 0;
2726 // toggle command
2727 if(set_prop_cmd[i].toggle) {
2728 // set to value
2729 if(cmd->nargs > 0 && cmd->args[0].v.i >= prop->min)
2730 r = m_property_do(prop,M_PROPERTY_SET,&cmd->args[0].v.i);
2731 else
2732 r = m_property_do(prop,M_PROPERTY_STEP_UP,NULL);
2733 } else if(cmd->args[1].v.i) //set
2734 r = m_property_do(prop,M_PROPERTY_SET,&cmd->args[0].v);
2735 else // adjust
2736 r = m_property_do(prop,M_PROPERTY_STEP_UP,&cmd->args[0].v);
2738 if(r <= 0) return 1;
2740 if(set_prop_cmd[i].osd_progbar) {
2741 if(prop->type == CONF_TYPE_INT) {
2742 if(m_property_do(prop,M_PROPERTY_GET,&r) > 0)
2743 set_osd_bar(set_prop_cmd[i].osd_progbar,
2744 set_prop_cmd[i].osd_msg,
2745 prop->min,prop->max,r);
2746 } else if(prop->type == CONF_TYPE_FLOAT) {
2747 float f;
2748 if(m_property_do(prop,M_PROPERTY_GET,&f) > 0)
2749 set_osd_bar(set_prop_cmd[i].osd_progbar,set_prop_cmd[i].osd_msg,
2750 prop->min,prop->max,f);
2751 } else
2752 mp_msg(MSGT_CPLAYER,MSGL_ERR, "Property use an unsupported type.\n");
2753 return 1;
2756 if(set_prop_cmd[i].osd_msg) {
2757 char* val = m_property_print(prop);
2758 if(val) {
2759 set_osd_msg(set_prop_cmd[i].osd_id >= 0 ? set_prop_cmd[i].osd_id :
2760 OSD_MSG_PROPERTY+i,1,osd_duration,
2761 set_prop_cmd[i].osd_msg,val);
2762 free(val);
2765 return 1;
2768 static void reinit_audio_chain(void) {
2769 if(sh_audio){
2770 current_module="init_audio_codec";
2771 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2772 if(!init_best_audio_codec(sh_audio,audio_codec_list,audio_fm_list)){
2773 sh_audio=d_audio->sh=NULL; // failed to init :(
2774 d_audio->id = -2;
2775 return;
2776 } else
2777 inited_flags|=INITED_ACODEC;
2778 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2781 //const ao_info_t *info=audio_out->info;
2782 current_module="af_preinit";
2783 ao_data.samplerate=force_srate;
2784 ao_data.channels=0;
2785 ao_data.format=audio_output_format;
2786 #if 1
2787 // first init to detect best values
2788 if(!preinit_audio_filters(sh_audio,
2789 // input:
2790 (int)(sh_audio->samplerate*playback_speed),
2791 sh_audio->channels, sh_audio->sample_format,
2792 // output:
2793 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
2794 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
2795 exit_player(MSGTR_Exit_error);
2797 #endif
2798 current_module="ao2_init";
2799 if(!(audio_out=init_best_audio_out(audio_driver_list,
2800 0, // plugin flag
2801 ao_data.samplerate,
2802 ao_data.channels,
2803 ao_data.format,0))){
2804 // FAILED:
2805 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
2806 uninit_player(INITED_ACODEC); // close codec
2807 sh_audio=d_audio->sh=NULL; // -> nosound
2808 d_audio->id = -2;
2809 return;
2810 } else {
2811 // SUCCESS:
2812 inited_flags|=INITED_AO;
2813 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
2814 audio_out->info->short_name,
2815 ao_data.samplerate, ao_data.channels,
2816 af_fmt2str_short(ao_data.format),
2817 af_fmt2bits(ao_data.format)/8 );
2818 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
2819 audio_out->info->name, audio_out->info->author);
2820 if(strlen(audio_out->info->comment) > 0)
2821 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", audio_out->info->comment);
2822 // init audio filters:
2823 #if 1
2824 current_module="af_init";
2825 if(!build_afilter_chain(sh_audio, &ao_data)) {
2826 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
2827 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
2828 // uninit_player(INITED_ACODEC|INITED_AO); // close codec & ao
2829 // sh_audio=d_audio->sh=NULL; // -> nosound
2831 #endif
2833 mixer.audio_out = audio_out;
2834 mixer.volstep = volstep;
2839 ///@}
2840 // Command2Property
2843 // Return pts value corresponding to the end point of audio written to the
2844 // ao so far.
2845 static double written_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio)
2847 // first calculate the end pts of audio that has been output by decoder
2848 double a_pts = sh_audio->pts;
2849 if (a_pts != MP_NOPTS_VALUE)
2850 // Good, decoder supports new way of calculating audio pts.
2851 // sh_audio->pts is the timestamp of the latest input packet with
2852 // known pts that the decoder has decoded. sh_audio->pts_bytes is
2853 // the amount of bytes the decoder has written after that timestamp.
2854 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
2855 else {
2856 // Decoder doesn't support new way of calculating pts (or we're
2857 // being called before it has decoded anything with known timestamp).
2858 // Use the old method of audio pts calculation: take the timestamp
2859 // of last packet with known pts the decoder has read data from,
2860 // and add amount of bytes read after the beginning of that packet
2861 // divided by input bps. This will be inaccurate if the input/output
2862 // ratio is not constant for every audio packet or if it is constant
2863 // but not accurately known in sh_audio->i_bps.
2865 a_pts = d_audio->pts;
2866 // ds_tell_pts returns bytes read after last timestamp from
2867 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
2868 // it has read but not decoded
2869 if (sh_audio->i_bps)
2870 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
2871 (double)sh_audio->i_bps;
2873 // Now a_pts hopefully holds the pts for end of audio from decoder.
2874 // Substract data in buffers between decoder and audio out.
2876 // Decoded but not filtered
2877 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
2879 // Data that was ready for ao but was buffered because ao didn't fully
2880 // accept everything to internal buffers yet
2881 a_pts -= sh_audio->a_out_buffer_len * playback_speed / (double)ao_data.bps;
2883 return a_pts;
2886 // Return pts value corresponding to currently playing audio.
2887 static double playing_audio_pts(sh_audio_t *sh_audio, demux_stream_t *d_audio,
2888 ao_functions_t *audio_out)
2890 return written_audio_pts(sh_audio, d_audio) - playback_speed *
2891 audio_out->get_delay();
2894 static void update_subtitles(void)
2896 unsigned char *packet=NULL;
2897 int len;
2898 char type = d_dvdsub->sh ? ((sh_sub_t *)d_dvdsub->sh)->type : 'v';
2899 // find sub
2900 if (subdata) {
2901 double pts = sh_video->pts;
2902 if (sub_fps==0) sub_fps = sh_video->fps;
2903 current_module = "find_sub";
2904 if (pts > sub_last_pts || pts < sub_last_pts-1.0) {
2905 find_sub(subdata, (pts+sub_delay) *
2906 (subdata->sub_uses_time ? 100. : sub_fps));
2907 if (vo_sub) vo_sub_last = vo_sub;
2908 // FIXME! frame counter...
2909 sub_last_pts = pts;
2913 // DVD sub:
2914 if (vo_config_count && vo_spudec && type == 'v') {
2915 int timestamp;
2916 current_module = "spudec";
2917 spudec_heartbeat(vo_spudec, 90000*sh_video->timer);
2918 /* Get a sub packet from the DVD or a vobsub and make a timestamp
2919 * relative to sh_video->timer */
2920 while(1) {
2921 // Vobsub
2922 len = 0;
2923 if (vo_vobsub) {
2924 if (sh_video->pts+sub_delay >= 0) {
2925 len = vobsub_get_packet(vo_vobsub, sh_video->pts+sub_delay,
2926 (void**)&packet, &timestamp);
2927 if (len > 0) {
2928 timestamp -= (sh_video->pts + sub_delay - sh_video->timer)*90000;
2929 mp_dbg(MSGT_CPLAYER,MSGL_V,"\rVOB sub: len=%d v_pts=%5.3f v_timer=%5.3f sub=%5.3f ts=%d \n",len,sh_video->pts,sh_video->timer,timestamp / 90000.0,timestamp);
2932 } else {
2933 // DVD sub
2934 len = ds_get_packet_sub(d_dvdsub, (unsigned char**)&packet);
2935 if (len > 0) {
2936 // XXX This is wrong, sh_video->pts can be arbitrarily
2937 // much behind demuxing position. Unfortunately using
2938 // d_video->pts which would have been the simplest
2939 // improvement doesn't work because mpeg specific hacks
2940 // in video.c set d_video->pts to 0.
2941 float x = d_dvdsub->pts - sh_video->pts;
2942 if (x > -20 && x < 20) // prevent missing subs on pts reset
2943 timestamp = 90000*(sh_video->timer + d_dvdsub->pts
2944 + sub_delay - sh_video->pts);
2945 else timestamp = 90000*(sh_video->timer + sub_delay);
2946 mp_dbg(MSGT_CPLAYER, MSGL_V, "\rDVD sub: len=%d "
2947 "v_pts=%5.3f s_pts=%5.3f ts=%d \n", len,
2948 sh_video->pts, d_dvdsub->pts, timestamp);
2951 if (len<=0 || !packet) break;
2952 if (timestamp >= 0)
2953 spudec_assemble(vo_spudec, packet, len, timestamp);
2956 if (spudec_changed(vo_spudec))
2957 vo_osd_changed(OSDTYPE_SPU);
2958 } else if (dvdsub_id >= 0 && type == 't') {
2959 double pts = MP_NOPTS_VALUE;
2960 while (d_dvdsub->first) {
2961 double nextpts = ds_get_next_pts(d_dvdsub);
2962 if (nextpts == MP_NOPTS_VALUE || nextpts - sub_delay > sh_video->pts)
2963 break;
2964 len = ds_get_packet_sub(d_dvdsub, &packet);
2965 pts = nextpts - sub_delay;
2967 if (pts != MP_NOPTS_VALUE) {
2968 static subtitle subs;
2969 sub_clear_text(&subs, MP_NOPTS_VALUE);
2970 sub_add_text(&subs, packet, len, MP_NOPTS_VALUE);
2971 vo_sub = &subs;
2972 vo_osd_changed(OSDTYPE_SUBTITLE);
2975 current_module=NULL;
2978 static int generate_video_frame(sh_video_t *sh_video, demux_stream_t *d_video)
2980 unsigned char *start;
2981 int in_size;
2982 int hit_eof=0;
2983 double pts;
2985 while (1) {
2986 void *decoded_frame;
2987 current_module = "decode video";
2988 // XXX Time used in this call is not counted in any performance
2989 // timer now, OSD is not updated correctly for filter-added frames
2990 if (vf_output_queued_frame(sh_video->vfilter))
2991 break;
2992 current_module = "video_read_frame";
2993 in_size = ds_get_packet_pts(d_video, &start, &pts);
2994 if (in_size < 0) {
2995 // try to extract last frames in case of decoder lag
2996 in_size = 0;
2997 pts = 1e300;
2998 hit_eof = 1;
3000 if (in_size > max_framesize)
3001 max_framesize = in_size;
3002 if (pts == MP_NOPTS_VALUE)
3003 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts value from demuxer MISSING\n");
3004 current_module = "decode video";
3005 decoded_frame = decode_video(sh_video, start, in_size, 0, pts);
3006 if (decoded_frame) {
3007 update_subtitles();
3008 update_osd_msg();
3009 current_module = "filter video";
3010 if (filter_video(sh_video, decoded_frame, sh_video->pts))
3011 break;
3013 if (hit_eof)
3014 return 0;
3016 return 1;
3019 static void rescale_input_coordinates(int ix, int iy, double *dx, double *dy) {
3020 //remove the borders, if any, and rescale to the range [0,1],[0,1]
3021 if(vo_fs) { //we are in full-screen mode
3022 if(vo_screenwidth > vo_dwidth) //there are borders along the x axis
3023 ix -= (vo_screenwidth - vo_dwidth) / 2;
3024 if(vo_screenheight > vo_dheight) //there are borders along the y axis (usual way)
3025 iy -= (vo_screenheight - vo_dheight) / 2;
3027 if(ix < 0 || ix > vo_dwidth) {*dx = *dy = -1.0; return; } //we are on one of the borders
3028 if(iy < 0 || iy > vo_dheight) {*dx = *dy = -1.0; return; } //we are on one of the borders
3031 *dx = (double) ix / (double) vo_dwidth;
3032 *dy = (double) iy / (double) vo_dheight;
3034 mp_msg(MSGT_CPLAYER,MSGL_V, "\r\nrescaled coordinates: %.3lf, %.3lf, screen (%d x %d), vodisplay: (%d, %d), fullscreen: %d\r\n",
3035 *dx, *dy, vo_screenwidth, vo_screenheight, vo_dwidth, vo_dheight, vo_fs);
3038 #ifdef HAVE_RTC
3039 int rtc_fd = -1;
3040 #endif
3042 static float timing_sleep(float time_frame)
3044 #ifdef HAVE_RTC
3045 if (rtc_fd >= 0){
3046 // -------- RTC -----------
3047 current_module="sleep_rtc";
3048 while (time_frame > 0.000) {
3049 unsigned long rtc_ts;
3050 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
3051 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
3052 time_frame -= GetRelativeTime();
3054 } else
3055 #endif
3056 #ifdef SYS_DARWIN
3058 current_module = "sleep_darwin";
3059 while (time_frame > 0.005) {
3060 usec_sleep(1000000*time_frame);
3061 time_frame -= GetRelativeTime();
3064 #else
3066 // assume kernel HZ=100 for softsleep, works with larger HZ but with
3067 // unnecessarily high CPU usage
3068 float margin = softsleep ? 0.011 : 0;
3069 current_module = "sleep_timer";
3070 while (time_frame > margin) {
3071 usec_sleep(1000000 * (time_frame - margin));
3072 time_frame -= GetRelativeTime();
3074 if (softsleep){
3075 current_module = "sleep_soft";
3076 if (time_frame < 0)
3077 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
3078 while (time_frame > 0)
3079 time_frame-=GetRelativeTime(); // burn the CPU
3082 #endif /* SYS_DARWIN */
3083 return time_frame;
3086 static void adjust_sync_and_print_status(int between_frames, float timing_error)
3088 current_module="av_sync";
3090 if(sh_audio){
3091 double a_pts, v_pts;
3093 if (autosync)
3095 * If autosync is enabled, the value for delay must be calculated
3096 * a bit differently. It is set only to the difference between
3097 * the audio and video timers. Any attempt to include the real
3098 * or corrected delay causes the pts_correction code below to
3099 * try to correct for the changes in delay which autosync is
3100 * trying to measure. This keeps the two from competing, but still
3101 * allows the code to correct for PTS drift *only*. (Using a delay
3102 * value here, even a "corrected" one, would be incompatible with
3103 * autosync mode.)
3105 a_pts = written_audio_pts(sh_audio, d_audio) - sh_audio->delay;
3106 else
3107 a_pts = playing_audio_pts(sh_audio, d_audio, audio_out);
3109 v_pts = sh_video->pts;
3112 static int drop_message=0;
3113 double AV_delay = a_pts - audio_delay - v_pts;
3114 double x;
3115 if (AV_delay>0.5 && drop_frame_cnt>50 && drop_message==0){
3116 ++drop_message;
3117 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
3119 if (autosync)
3120 x = AV_delay*0.1f;
3121 else
3122 /* Do not correct target time for the next frame if this frame
3123 * was late not because of wrong target time but because the
3124 * target time could not be met */
3125 x = (AV_delay + timing_error * playback_speed) * 0.1f;
3126 if (x < -max_pts_correction)
3127 x = -max_pts_correction;
3128 else if (x> max_pts_correction)
3129 x = max_pts_correction;
3130 if (default_max_pts_correction >= 0)
3131 max_pts_correction = default_max_pts_correction;
3132 else
3133 max_pts_correction = sh_video->frametime*0.10; // +-10% of time
3134 if (!between_frames) {
3135 sh_audio->delay+=x;
3136 c_total+=x;
3138 if(!quiet)
3139 print_status(a_pts - audio_delay, AV_delay, c_total);
3142 } else {
3143 // No audio:
3145 if (!quiet)
3146 print_status(0, 0, 0);
3150 static int fill_audio_out_buffers(void)
3152 unsigned int t;
3153 double tt;
3154 int playsize;
3155 int playflags=0;
3156 int audio_eof=0;
3157 int bytes_to_write;
3159 current_module="play_audio";
3161 while (1) {
3162 // all the current uses of ao_data.pts seem to be in aos that handle
3163 // sync completely wrong; there should be no need to use ao_data.pts
3164 // in get_space()
3165 ao_data.pts = ((sh_video?sh_video->timer:0)+sh_audio->delay)*90000.0;
3166 bytes_to_write = audio_out->get_space();
3167 if (sh_video || bytes_to_write >= ao_data.outburst)
3168 break;
3170 // handle audio-only case:
3171 // this is where mplayer sleeps during audio-only playback
3172 // to avoid 100% CPU use
3173 usec_sleep(10000); // Wait a tick before retry
3176 while (bytes_to_write) {
3177 playsize = bytes_to_write;
3178 if (playsize > MAX_OUTBURST)
3179 playsize = MAX_OUTBURST;
3180 bytes_to_write -= playsize;
3182 // Fill buffer if needed:
3183 current_module="decode_audio";
3184 t = GetTimer();
3185 while (sh_audio->a_out_buffer_len < playsize) {
3186 int buflen = sh_audio->a_out_buffer_len;
3187 int ret = decode_audio(sh_audio, &sh_audio->a_out_buffer[buflen],
3188 playsize - buflen, // min bytes
3189 sh_audio->a_out_buffer_size - buflen // max
3191 if (ret <= 0) { // EOF?
3192 if (d_audio->eof) {
3193 audio_eof = 1;
3194 if (sh_audio->a_out_buffer_len == 0)
3195 return 0;
3197 break;
3199 sh_audio->a_out_buffer_len += ret;
3201 t = GetTimer() - t;
3202 tt = t*0.000001f; audio_time_usage+=tt;
3203 if (playsize > sh_audio->a_out_buffer_len) {
3204 playsize = sh_audio->a_out_buffer_len;
3205 if (audio_eof)
3206 playflags |= AOPLAY_FINAL_CHUNK;
3208 if (!playsize)
3209 break;
3211 // play audio:
3212 current_module="play_audio";
3214 // Is this pts value actually useful for the aos that access it?
3215 // They're obviously badly broken in the way they handle av sync;
3216 // would not having access to this make them more broken?
3217 ao_data.pts = ((sh_video?sh_video->timer:0)+sh_audio->delay)*90000.0;
3218 playsize = audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
3220 if (playsize > 0) {
3221 sh_audio->a_out_buffer_len -= playsize;
3222 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
3223 sh_audio->a_out_buffer_len);
3224 sh_audio->delay += playback_speed*playsize/(double)ao_data.bps;
3226 else if (audio_eof && audio_out->get_delay() < .04) {
3227 // Sanity check to avoid hanging in case current ao doesn't output
3228 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
3229 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
3230 sh_audio->a_out_buffer_len = 0;
3233 return 1;
3236 static int sleep_until_update(float *time_frame, float *aq_sleep_time)
3238 int frame_time_remaining = 0;
3239 current_module="calc_sleep_time";
3241 *time_frame -= GetRelativeTime(); // reset timer
3243 if (sh_audio && !d_audio->eof) {
3244 float delay = audio_out->get_delay();
3245 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
3247 if (autosync) {
3249 * Adjust this raw delay value by calculating the expected
3250 * delay for this frame and generating a new value which is
3251 * weighted between the two. The higher autosync is, the
3252 * closer to the delay value gets to that which "-nosound"
3253 * would have used, and the longer it will take for A/V
3254 * sync to settle at the right value (but it eventually will.)
3255 * This settling time is very short for values below 100.
3257 float predicted = sh_audio->delay / playback_speed + *time_frame;
3258 float difference = delay - predicted;
3259 delay = predicted + difference / (float)autosync;
3262 *time_frame = delay - sh_audio->delay / playback_speed;
3264 // delay = amount of audio buffered in soundcard/driver
3265 if (delay > 0.25) delay=0.25; else
3266 if (delay < 0.10) delay=0.10;
3267 if (*time_frame > delay*0.6) {
3268 // sleep time too big - may cause audio drops (buffer underrun)
3269 frame_time_remaining = 1;
3270 *time_frame = delay*0.5;
3272 } else {
3273 // If we're lagging more than 200 ms behind the right playback rate,
3274 // don't try to "catch up".
3275 // If benchmark is set always output frames as fast as possible
3276 // without sleeping.
3277 if (*time_frame < -0.2 || benchmark)
3278 *time_frame = 0;
3281 *aq_sleep_time += *time_frame;
3284 //============================== SLEEP: ===================================
3286 // flag 256 means: libvo driver does its timing (dvb card)
3287 if (*time_frame > 0.001 && !(vo_flags&256))
3288 *time_frame = timing_sleep(*time_frame);
3289 return frame_time_remaining;
3292 static int reinit_video_chain(void) {
3293 //================== Init VIDEO (codec & libvo) ==========================
3294 if(!fixed_vo || !(inited_flags&INITED_VO)){
3295 current_module="preinit_libvo";
3297 //shouldn't we set dvideo->id=-2 when we fail?
3298 vo_config_count=0;
3299 //if((video_out->preinit(vo_subdevice))!=0){
3300 if(!(video_out=init_best_video_out(video_driver_list))){
3301 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
3302 goto err_out;
3304 sh_video->video_out=video_out;
3305 inited_flags|=INITED_VO;
3308 current_module="init_video_filters";
3310 char* vf_arg[] = { "_oldargs_", (char*)video_out , NULL };
3311 sh_video->vfilter=(void*)vf_open_filter(NULL,"vo",vf_arg);
3313 #ifdef HAVE_MENU
3314 if(use_menu) {
3315 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
3316 vf_menu = vf_open_plugin(libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
3317 if(!vf_menu) {
3318 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
3319 use_menu = 0;
3322 if(vf_menu)
3323 sh_video->vfilter=(void*)vf_menu;
3324 #endif
3326 #ifdef USE_ASS
3327 if(ass_enabled) {
3328 int i;
3329 int insert = 1;
3330 if (vf_settings)
3331 for (i = 0; vf_settings[i].name; ++i)
3332 if (strcmp(vf_settings[i].name, "ass") == 0) {
3333 insert = 0;
3334 break;
3336 if (insert) {
3337 extern vf_info_t vf_info_ass;
3338 vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
3339 char* vf_arg[] = {"auto", "1", NULL};
3340 vf_instance_t* vf_ass = vf_open_plugin(libass_vfs,sh_video->vfilter,"ass",vf_arg);
3341 if (vf_ass)
3342 sh_video->vfilter=(void*)vf_ass;
3343 else
3344 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
3347 #endif
3349 sh_video->vfilter=(void*)append_filters(sh_video->vfilter);
3351 #ifdef USE_ASS
3352 if (ass_enabled)
3353 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
3354 #endif
3356 current_module="init_video_codec";
3358 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
3359 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
3360 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
3362 if(!sh_video->inited){
3363 if(!fixed_vo) uninit_player(INITED_VO);
3364 goto err_out;
3367 inited_flags|=INITED_VCODEC;
3369 if (sh_video->codec)
3370 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
3372 sh_video->last_pts = MP_NOPTS_VALUE;
3373 sh_video->num_buffered_pts = 0;
3374 sh_video->next_frame_time = 0;
3376 if(auto_quality>0){
3377 // Auto quality option enabled
3378 output_quality=get_video_quality_max(sh_video);
3379 if(auto_quality>output_quality) auto_quality=output_quality;
3380 else output_quality=auto_quality;
3381 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
3382 set_video_quality(sh_video,output_quality);
3385 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
3387 current_module="init_vo";
3389 return 1;
3391 err_out:
3392 sh_video = d_video->sh = NULL;
3393 return 0;
3396 static double update_video(int *blit_frame)
3398 //-------------------- Decode a frame: -----------------------
3399 double frame_time;
3400 *blit_frame = 0; // Don't blit if we hit EOF
3401 if (!correct_pts) {
3402 unsigned char* start=NULL;
3403 void *decoded_frame;
3404 int drop_frame=0;
3405 int in_size;
3407 current_module = "video_read_frame";
3408 frame_time = sh_video->next_frame_time;
3409 in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
3410 &start, force_fps);
3411 if (in_size < 0)
3412 return -1;
3413 if (in_size > max_framesize)
3414 max_framesize = in_size; // stats
3415 sh_video->timer += frame_time;
3416 if (sh_audio)
3417 sh_audio->delay -= frame_time;
3418 // video_read_frame can change fps (e.g. for ASF video)
3419 vo_fps = sh_video->fps;
3420 // check for frame-drop:
3421 current_module = "check_framedrop";
3422 if (sh_audio && !d_audio->eof) {
3423 static int dropped_frames;
3424 float delay = playback_speed*audio_out->get_delay();
3425 float d = delay-sh_audio->delay;
3426 // we should avoid dropping too many frames in sequence unless we
3427 // are too late. and we allow 100ms A-V delay here:
3428 if (d < -dropped_frames*frame_time-0.100 &&
3429 osd_function != OSD_PAUSE) {
3430 drop_frame = frame_dropping;
3431 ++drop_frame_cnt;
3432 ++dropped_frames;
3433 } else
3434 drop_frame = dropped_frames = 0;
3435 ++total_frame_cnt;
3437 update_subtitles();
3438 update_osd_msg();
3439 current_module = "decode_video";
3440 decoded_frame = decode_video(sh_video, start, in_size, drop_frame,
3441 sh_video->pts);
3442 current_module = "filter_video";
3443 *blit_frame = (decoded_frame && filter_video(sh_video, decoded_frame,
3444 sh_video->pts));
3446 else {
3447 if (!generate_video_frame(sh_video, d_video))
3448 return -1;
3449 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter,
3450 VFCTRL_GET_PTS, &sh_video->pts);
3451 if (sh_video->pts == MP_NOPTS_VALUE) {
3452 mp_msg(MSGT_CPLAYER, MSGL_ERR, "pts after filters MISSING\n");
3453 sh_video->pts = sh_video->last_pts;
3455 if (sh_video->last_pts == MP_NOPTS_VALUE)
3456 sh_video->last_pts= sh_video->pts;
3457 else if (sh_video->last_pts >= sh_video->pts) {
3458 sh_video->last_pts = sh_video->pts;
3459 mp_msg(MSGT_CPLAYER, MSGL_WARN, "pts value <= previous");
3461 frame_time = sh_video->pts - sh_video->last_pts;
3462 sh_video->last_pts = sh_video->pts;
3463 sh_video->timer += frame_time;
3464 if(sh_audio)
3465 sh_audio->delay -= frame_time;
3466 *blit_frame = 1;
3468 return frame_time;
3471 void pause_loop(void)
3473 mp_cmd_t* cmd;
3474 if (!quiet) {
3475 // Small hack to display the pause message on the OSD line.
3476 // The pause string is: "\n == PAUSE == \r" so we need to
3477 // take the first and the last char out
3478 if (term_osd && !sh_video) {
3479 char msg[128] = MSGTR_Paused;
3480 int mlen = strlen(msg);
3481 msg[mlen-1] = '\0';
3482 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
3483 update_osd_msg();
3484 } else
3485 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
3486 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
3487 fflush(stdout);
3489 #ifdef HAVE_NEW_GUI
3490 if (use_gui)
3491 guiGetEvent(guiCEvent, (char *)guiSetPause);
3492 #endif
3493 if (video_out && sh_video && vo_config_count)
3494 video_out->control(VOCTRL_PAUSE, NULL);
3496 if (audio_out && sh_audio)
3497 audio_out->pause(); // pause audio, keep data if possible
3499 while ( (cmd = mp_input_get_cmd(20, 1, 1)) == NULL) {
3500 if (sh_video && video_out && vo_config_count)
3501 video_out->check_events();
3502 #ifdef HAVE_NEW_GUI
3503 if (use_gui) {
3504 guiEventHandling();
3505 guiGetEvent(guiReDraw, NULL);
3506 if (guiIntfStruct.Playing!=2 || (rel_seek_secs || abs_seek_pos))
3507 break;
3509 #endif
3510 #ifdef HAVE_MENU
3511 if (vf_menu)
3512 vf_menu_pause_update(vf_menu);
3513 #endif
3514 usec_sleep(20000);
3516 if (cmd && cmd->id == MP_CMD_PAUSE) {
3517 cmd = mp_input_get_cmd(0,1,0);
3518 mp_cmd_free(cmd);
3520 osd_function=OSD_PLAY;
3521 if (audio_out && sh_audio)
3522 audio_out->resume(); // resume audio
3523 if (video_out && sh_video && vo_config_count)
3524 video_out->control(VOCTRL_RESUME, NULL); // resume video
3525 (void)GetRelativeTime(); // ignore time that passed during pause
3526 #ifdef HAVE_NEW_GUI
3527 if (use_gui) {
3528 if (guiIntfStruct.Playing == guiSetStop)
3529 eof = 1;
3530 else
3531 guiGetEvent(guiCEvent, (char *)guiSetPlay);
3533 #endif
3536 void print_version(void){
3537 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s\n", MP_TITLE);
3539 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
3540 GetCpuCaps(&gCpuCaps);
3541 #ifdef ARCH_X86
3542 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
3543 gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
3544 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
3545 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
3546 #ifdef RUNTIME_CPUDETECT
3547 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
3548 #else
3549 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
3550 #ifdef HAVE_MMX
3551 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
3552 #endif
3553 #ifdef HAVE_MMX2
3554 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
3555 #endif
3556 #ifdef HAVE_3DNOW
3557 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
3558 #endif
3559 #ifdef HAVE_3DNOWEX
3560 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
3561 #endif
3562 #ifdef HAVE_SSE
3563 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
3564 #endif
3565 #ifdef HAVE_SSE2
3566 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
3567 #endif
3568 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3569 #endif /* RUNTIME_CPUDETECT */
3570 #endif /* ARCH_X86 */
3573 int main(int argc,char* argv[]){
3576 char * mem_ptr;
3578 int file_format=DEMUXER_TYPE_UNKNOWN;
3580 // movie info:
3582 /* Flag indicating whether MPlayer should exit without playing anything. */
3583 int opt_exit = 0;
3585 //float a_frame=0; // Audio
3587 int i;
3588 char *tmp;
3590 int gui_no_filename=0;
3592 srand((int) time(NULL));
3594 InitTimer();
3596 mp_msg_init();
3598 for(i=1; i<argc; i++)
3599 if(!strcmp(argv[i], "-really-quiet"))
3600 verbose= -10;
3602 print_version();
3603 #if defined(WIN32) && defined(USE_WIN32DLL)
3604 set_path_env();
3605 #endif /*WIN32 && USE_WIN32DLL*/
3607 #ifdef USE_TV
3608 tv_param_immediate = 1;
3609 #endif
3611 if (argc > 1 && argv[1] &&
3612 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
3613 use_gui = !strcmp(argv[1], "-gui");
3614 } else
3615 if ( argv[0] )
3617 char *base = strrchr(argv[0], '/');
3618 if (!base)
3619 base = strrchr(argv[0], '\\');
3620 if (!base)
3621 base = argv[0];
3622 if(strstr(base, "gmplayer"))
3623 use_gui=1;
3626 mconfig = m_config_new();
3627 m_config_register_options(mconfig,mplayer_opts);
3628 // TODO : add something to let modules register their options
3629 mp_input_register_options(mconfig);
3630 parse_cfgfiles(mconfig);
3632 #ifdef HAVE_NEW_GUI
3633 if ( use_gui ) cfg_read();
3634 #endif
3636 playtree = m_config_parse_mp_command_line(mconfig, argc, argv);
3637 if(playtree == NULL)
3638 opt_exit = 1;
3639 else {
3640 playtree = play_tree_cleanup(playtree);
3641 if(playtree) {
3642 playtree_iter = play_tree_iter_new(playtree,mconfig);
3643 if(playtree_iter) {
3644 if(play_tree_iter_step(playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3645 play_tree_iter_free(playtree_iter);
3646 playtree_iter = NULL;
3648 filename = play_tree_iter_get_file(playtree_iter,1);
3653 #if defined(WIN32) && defined(HAVE_NEW_GUI)
3654 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
3655 if(runningmplayer && filename && use_gui){
3656 COPYDATASTRUCT csData;
3657 char file[MAX_PATH];
3658 char *filepart = filename;
3659 if(GetFullPathName(filename, MAX_PATH, file, &filepart)){
3660 csData.dwData = 0;
3661 csData.cbData = strlen(file)*2;
3662 csData.lpData = file;
3663 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
3666 #endif
3668 #ifdef WIN32
3669 if(proc_priority){
3670 int i;
3671 for(i=0; priority_presets_defs[i].name; i++){
3672 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
3673 break;
3675 mp_msg(MSGT_CPLAYER,MSGL_STATUS,"Setting process priority: %s\n",
3676 priority_presets_defs[i].name);
3677 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
3679 #endif
3680 #ifndef HAVE_NEW_GUI
3681 if(use_gui){
3682 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
3683 use_gui=0;
3685 #else
3686 #ifndef WIN32
3687 if(use_gui && !vo_init()){
3688 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
3689 use_gui=0;
3691 #endif
3692 if (use_gui && playtree_iter){
3693 char cwd[PATH_MAX+2];
3694 // Free Playtree and Playtree-Iter as it's not used by the GUI.
3695 play_tree_iter_free(playtree_iter);
3696 playtree_iter=NULL;
3698 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
3700 strcat(cwd, "/");
3701 // Prefix relative paths with current working directory
3702 play_tree_add_bpf(playtree, cwd);
3704 // Import initital playtree into GUI.
3705 import_initial_playtree_into_gui(playtree, mconfig, enqueue);
3707 #endif /* HAVE_NEW_GUI */
3709 if(video_driver_list && strcmp(video_driver_list[0],"help")==0){
3710 list_video_out();
3711 opt_exit = 1;
3714 if(audio_driver_list && strcmp(audio_driver_list[0],"help")==0){
3715 list_audio_out();
3716 opt_exit = 1;
3719 /* Check codecs.conf. */
3720 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3721 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3722 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3723 if(!parse_codec_cfg(NULL)){
3724 exit_player_with_rc(NULL, 0);
3726 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
3729 free( mem_ptr ); // release the buffer created by get_path()
3732 #if 0
3733 if(video_codec_list){
3734 int i;
3735 video_codec=video_codec_list[0];
3736 for(i=0;video_codec_list[i];i++)
3737 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
3739 #endif
3740 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3741 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
3742 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3743 list_codecs(1);
3744 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3745 opt_exit = 1;
3747 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3748 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
3749 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3750 list_codecs(0);
3751 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3752 opt_exit = 1;
3754 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3755 vfm_help();
3756 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3757 opt_exit = 1;
3759 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3760 afm_help();
3761 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3762 opt_exit = 1;
3764 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3765 af_help();
3766 printf("\n");
3767 opt_exit = 1;
3769 #ifdef HAVE_X11
3770 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3771 fstype_help();
3772 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3773 opt_exit = 1;
3775 #endif
3776 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3777 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3778 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3779 demuxer_help();
3780 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3781 opt_exit = 1;
3783 if(list_properties) {
3784 m_properties_print_help_list(mp_properties);
3785 opt_exit = 1;
3788 if(opt_exit)
3789 exit_player(NULL);
3791 if (player_idle_mode && use_gui) {
3792 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
3793 exit_player_with_rc(NULL, 1);
3796 if(!filename && !player_idle_mode){
3797 if(!use_gui){
3798 // no file/vcd/dvd -> show HELP:
3799 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
3800 exit_player_with_rc(NULL, 0);
3801 } else gui_no_filename=1;
3804 /* Display what configure line was used */
3805 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3807 // Many users forget to include command line in bugreports...
3808 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3809 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
3810 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3811 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3814 //------ load global data first ------
3816 // check font
3817 #ifdef HAVE_FREETYPE
3818 init_freetype();
3819 #endif
3820 #ifdef HAVE_FONTCONFIG
3821 if(!font_fontconfig)
3823 #endif
3824 #ifdef HAVE_BITMAP_FONT
3825 if(font_name){
3826 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3827 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,font_name);
3828 } else {
3829 // try default:
3830 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3831 free(mem_ptr); // release the buffer created by get_path()
3832 if(!vo_font)
3833 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3835 #endif
3836 #ifdef HAVE_FONTCONFIG
3838 #endif
3840 vo_init_osd();
3842 #ifdef USE_ASS
3843 ass_library = ass_init();
3844 #endif
3846 #ifdef HAVE_RTC
3847 if(!nortc)
3849 // seteuid(0); /* Can't hurt to try to get root here */
3850 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3851 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
3852 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3853 else {
3854 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3856 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3857 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
3858 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
3859 close (rtc_fd);
3860 rtc_fd = -1;
3861 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3862 /* variable only by the root */
3863 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
3864 close (rtc_fd);
3865 rtc_fd = -1;
3866 } else
3867 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
3870 #ifdef HAVE_NEW_GUI
3871 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
3872 // and now ? -- Pontscho
3873 if(use_gui) setuid( getuid() ); // strongly test, please check this.
3874 #endif
3875 if(rtc_fd<0)
3876 #endif /* HAVE_RTC */
3877 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3878 softsleep?"software":timer_name);
3880 #ifdef USE_TERMCAP
3881 if ( !use_gui ) load_termcap(NULL); // load key-codes
3882 #endif
3884 // ========== Init keyboard FIFO (connection to libvo) ============
3886 // Init input system
3887 current_module = "init_input";
3888 mp_input_init(use_gui);
3889 #if 0
3890 make_pipe(&keyb_fifo_get,&keyb_fifo_put);
3892 if(keyb_fifo_get > 0)
3893 mp_input_add_key_fd(keyb_fifo_get,1,NULL,NULL);
3894 #else
3895 mp_input_add_key_fd(-1,0,mplayer_get_key,NULL);
3896 #endif
3897 if(slave_mode)
3898 #ifndef __MINGW32__
3899 mp_input_add_cmd_fd(0,1,NULL,NULL);
3900 #else
3901 mp_input_add_cmd_fd(0,0,mp_input_win32_slave_cmd_func,NULL);
3902 #endif
3903 else if(!noconsolecontrols)
3904 #ifndef HAVE_NO_POSIX_SELECT
3905 mp_input_add_key_fd(0,1,NULL,NULL);
3906 #else
3907 mp_input_add_key_fd(0,0,NULL,NULL);
3908 #endif
3910 inited_flags|=INITED_INPUT;
3911 current_module = NULL;
3913 #ifdef HAVE_MENU
3914 if(use_menu) {
3915 if(menu_cfg && menu_init(menu_cfg))
3916 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
3917 else {
3918 menu_cfg = get_path("menu.conf");
3919 if(menu_init(menu_cfg))
3920 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
3921 else {
3922 if(menu_init(MPLAYER_CONFDIR "/menu.conf"))
3923 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
3924 else {
3925 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
3926 use_menu = 0;
3931 #endif
3933 /// Catch signals
3934 #ifndef __MINGW32__
3935 signal(SIGCHLD,child_sighandler);
3936 #endif
3938 #ifdef CRASH_DEBUG
3939 prog_path = argv[0];
3940 #endif
3941 //========= Catch terminate signals: ================
3942 // terminate requests:
3943 signal(SIGTERM,exit_sighandler); // kill
3944 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3946 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3948 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3949 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3950 #ifdef ENABLE_SIGHANDLER
3951 // fatal errors:
3952 signal(SIGBUS,exit_sighandler); // bus error
3953 signal(SIGSEGV,exit_sighandler); // segfault
3954 signal(SIGILL,exit_sighandler); // illegal instruction
3955 signal(SIGFPE,exit_sighandler); // floating point exc.
3956 signal(SIGABRT,exit_sighandler); // abort()
3957 #ifdef CRASH_DEBUG
3958 if (crash_debug)
3959 signal(SIGTRAP,exit_sighandler);
3960 #endif
3961 #endif
3963 #ifdef HAVE_NEW_GUI
3964 if(use_gui){
3965 guiInit();
3966 inited_flags|=INITED_GUI;
3967 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
3969 #endif
3971 // ******************* Now, let's see the per-file stuff ********************
3973 play_next_file:
3975 // init global sub numbers
3976 global_sub_size = 0;
3977 { int i; for (i = 0; i < SUB_SOURCES; i++) global_sub_indices[i] = -1; }
3979 if (filename) load_per_file_config (mconfig, filename);
3981 // We must enable getch2 here to be able to interrupt network connection
3982 // or cache filling
3983 if(!noconsolecontrols && !slave_mode){
3984 if(inited_flags&INITED_GETCH2)
3985 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
3986 else
3987 getch2_enable(); // prepare stdin for hotkeys...
3988 inited_flags|=INITED_GETCH2;
3989 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3992 // =================== GUI idle loop (STOP state) ===========================
3993 #ifdef HAVE_NEW_GUI
3994 if ( use_gui ) {
3995 file_format=DEMUXER_TYPE_UNKNOWN;
3996 guiGetEvent( guiSetDefaults,0 );
3997 while ( guiIntfStruct.Playing != 1 )
3999 mp_cmd_t* cmd;
4000 usec_sleep(20000);
4001 guiEventHandling();
4002 guiGetEvent( guiReDraw,NULL );
4003 if ( (cmd = mp_input_get_cmd(0,0,0)) != NULL) guiGetEvent( guiIEvent,(char *)cmd->id );
4005 guiGetEvent( guiSetParameters,NULL );
4006 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
4008 play_tree_t * entry = play_tree_new();
4009 play_tree_add_file( entry,guiIntfStruct.Filename );
4010 if ( playtree ) play_tree_free_list( playtree->child,1 );
4011 else playtree=play_tree_new();
4012 play_tree_set_child( playtree,entry );
4013 if(playtree)
4015 playtree_iter = play_tree_iter_new(playtree,mconfig);
4016 if(playtree_iter)
4018 if(play_tree_iter_step(playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
4020 play_tree_iter_free(playtree_iter);
4021 playtree_iter = NULL;
4023 filename = play_tree_iter_get_file(playtree_iter,1);
4028 #endif /* HAVE_NEW_GUI */
4030 while (player_idle_mode && !filename) {
4031 play_tree_t * entry = NULL;
4032 mp_cmd_t * cmd;
4033 while (!(cmd = mp_input_get_cmd(0,1,0))) { // wait for command
4034 if (video_out && vo_config_count) video_out->check_events();
4035 usec_sleep(20000);
4037 switch (cmd->id) {
4038 case MP_CMD_LOADFILE:
4039 // prepare a tree entry with the new filename
4040 entry = play_tree_new();
4041 play_tree_add_file(entry, cmd->args[0].v.s);
4042 // The entry is added to the main playtree after the switch().
4043 break;
4044 case MP_CMD_LOADLIST:
4045 entry = parse_playlist_file(cmd->args[0].v.s);
4046 break;
4047 case MP_CMD_QUIT:
4048 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
4049 break;
4052 mp_cmd_free(cmd);
4054 if (entry) { // user entered a command that gave a valid entry
4055 if (playtree) // the playtree is always a node with one child. let's clear it
4056 play_tree_free_list(playtree->child, 1);
4057 else playtree=play_tree_new(); // .. or make a brand new playtree
4059 if (!playtree) continue; // couldn't make playtree! wait for next command
4061 play_tree_set_child(playtree, entry);
4063 /* Make iterator start at the top the of tree. */
4064 playtree_iter = play_tree_iter_new(playtree, mconfig);
4065 if (!playtree_iter) continue;
4067 // find the first real item in the tree
4068 if (play_tree_iter_step(playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
4069 // no items!
4070 play_tree_iter_free(playtree_iter);
4071 playtree_iter = NULL;
4072 continue; // wait for next command
4074 filename = play_tree_iter_get_file(playtree_iter, 1);
4077 //---------------------------------------------------------------------------
4079 if(filename) mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing, filename);
4081 if (edl_filename) {
4082 if (edl_records) free_edl(edl_records);
4083 next_edl_record = edl_records = edl_parse_file();
4085 if (edl_output_filename) {
4086 if (edl_fd) fclose(edl_fd);
4087 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
4089 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
4090 edl_output_filename);
4094 //==================== Open VOB-Sub ============================
4096 current_module="vobsub";
4097 if (vobsub_name){
4098 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
4099 if(vo_vobsub==NULL)
4100 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,vobsub_name);
4101 }else if(sub_auto && filename && (strlen(filename)>=5)){
4102 /* try to autodetect vobsub from movie filename ::atmos */
4103 char *buf = malloc((strlen(filename)-3)),*psub;
4104 memset(buf,0,strlen(filename)-3); // make sure string is terminated
4105 strncpy(buf, filename, strlen(filename)-4);
4106 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
4107 /* try from ~/.mplayer/sub */
4108 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
4109 char *bname;
4110 int l;
4111 bname = strrchr(buf,'/');
4112 #ifdef WIN32
4113 if(!bname) bname = strrchr(buf,'\\');
4114 #endif
4115 if(bname) bname++;
4116 else bname = buf;
4117 l = strlen(psub) + strlen(bname) + 1;
4118 psub = realloc(psub,l);
4119 strcat(psub,bname);
4120 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
4121 free(psub);
4123 free(buf);
4125 if(vo_vobsub){
4126 inited_flags|=INITED_VOBSUB;
4127 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
4128 // check if vobsub requested only to display forced subtitles
4129 forced_subs_only=vobsub_get_forced_subs_flag(vo_vobsub);
4131 // setup global sub numbering
4132 global_sub_indices[SUB_SOURCE_VOBSUB] = global_sub_size; // the global # of the first vobsub.
4133 global_sub_size += vobsub_get_indexes_count(vo_vobsub);
4136 //============ Open & Sync STREAM --- fork cache2 ====================
4138 stream=NULL;
4139 demuxer=NULL;
4140 if (d_audio) {
4141 //free_demuxer_stream(d_audio);
4142 d_audio=NULL;
4144 if (d_video) {
4145 //free_demuxer_stream(d_video);
4146 d_video=NULL;
4148 sh_audio=NULL;
4149 sh_video=NULL;
4151 current_module="open_stream";
4152 stream=open_stream(filename,0,&file_format);
4153 if(!stream) { // error...
4154 eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY);
4155 goto goto_next_file;
4157 inited_flags|=INITED_STREAM;
4159 #ifdef HAVE_NEW_GUI
4160 if ( use_gui ) guiGetEvent( guiSetStream,(char *)stream );
4161 #endif
4163 if(file_format == DEMUXER_TYPE_PLAYLIST) {
4164 play_tree_t* entry;
4165 // Handle playlist
4166 current_module="handle_playlist";
4167 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",filename);
4168 entry = parse_playtree(stream,0);
4169 eof=playtree_add_playlist(entry);
4170 goto goto_next_file;
4172 stream->start_pos+=seek_to_byte;
4174 if(stream_dump_type==5){
4175 unsigned char buf[4096];
4176 int len;
4177 FILE *f;
4178 current_module="dumpstream";
4179 if(stream->type==STREAMTYPE_STREAM && stream->fd<0){
4180 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
4181 exit_player(MSGTR_Exit_error);
4183 stream_reset(stream);
4184 stream_seek(stream,stream->start_pos);
4185 f=fopen(stream_dump_name,"wb");
4186 if(!f){
4187 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
4188 exit_player(MSGTR_Exit_error);
4190 while(!stream->eof){
4191 len=stream_read(stream,buf,4096);
4192 if(len>0) {
4193 if(fwrite(buf,len,1,f) != 1) {
4194 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
4195 exit_player(MSGTR_Exit_error);
4199 if(fclose(f)) {
4200 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
4201 exit_player(MSGTR_Exit_error);
4203 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
4204 exit_player_with_rc(MSGTR_Exit_eof, 0);
4207 #ifdef USE_DVDREAD
4208 if(stream->type==STREAMTYPE_DVD){
4209 current_module="dvd lang->id";
4210 if(audio_id==-1) audio_id=dvd_aid_from_lang(stream,audio_lang);
4211 if(dvdsub_lang && dvdsub_id==-2) dvdsub_id=-1;
4212 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvd_sid_from_lang(stream,dvdsub_lang);
4213 // setup global sub numbering
4214 global_sub_indices[SUB_SOURCE_DEMUX] = global_sub_size; // the global # of the first demux-specific sub.
4215 global_sub_size += dvd_number_of_subs(stream);
4216 current_module=NULL;
4218 #endif
4220 #ifdef USE_DVDNAV
4221 if(stream->type==STREAMTYPE_DVDNAV){
4222 current_module="dvdnav lang->id";
4223 if(audio_id==-1) audio_id=dvdnav_aid_from_lang(stream,audio_lang);
4224 if(dvdsub_lang && dvdsub_id==-2) dvdsub_id=-1;
4225 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvdnav_sid_from_lang(stream,dvdsub_lang);
4226 // setup global sub numbering
4227 global_sub_indices[SUB_SOURCE_DEMUX] = global_sub_size; // the global # of the first demux-specific sub.
4228 global_sub_size += dvdnav_number_of_subs(stream);
4229 current_module=NULL;
4231 #endif
4233 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
4234 goto_enable_cache:
4235 if(stream_cache_size>0){
4236 current_module="enable_cache";
4237 if(!stream_enable_cache(stream,stream_cache_size*1024,
4238 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
4239 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
4240 if((eof = libmpdemux_was_interrupted(PT_NEXT_ENTRY))) goto goto_next_file;
4243 //============ Open DEMUXERS --- DETECT file type =======================
4244 current_module="demux_open";
4246 demuxer=demux_open(stream,file_format,audio_id,video_id,dvdsub_id,filename);
4248 // HACK to get MOV Reference Files working
4250 if (demuxer && demuxer->type==DEMUXER_TYPE_PLAYLIST)
4252 unsigned char* playlist_entry;
4253 play_tree_t *list = NULL, *entry = NULL;
4255 current_module="handle_demux_playlist";
4256 while (ds_get_packet(demuxer->video,&playlist_entry)>0)
4258 char *temp, *bname;
4260 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",playlist_entry);
4262 bname=mp_basename(playlist_entry);
4263 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
4264 continue;
4266 if (!strncmp(bname,mp_basename(filename),strlen(bname))) // ignoring self-reference
4267 continue;
4269 entry = play_tree_new();
4271 if (filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
4273 temp=malloc((strlen(filename)-strlen(mp_basename(filename))+strlen(playlist_entry)+1));
4274 if (temp)
4276 strncpy(temp, filename, strlen(filename)-strlen(mp_basename(filename)));
4277 temp[strlen(filename)-strlen(mp_basename(filename))]='\0';
4278 strcat(temp, playlist_entry);
4279 play_tree_add_file(entry,temp);
4280 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
4281 free(temp);
4284 else
4285 play_tree_add_file(entry,playlist_entry);
4287 if(!list)
4288 list = entry;
4289 else
4290 play_tree_append_entry(list,entry);
4292 free_demuxer(demuxer);
4293 demuxer = NULL;
4295 if (list)
4297 entry = play_tree_new();
4298 play_tree_set_child(entry,list);
4299 eof=playtree_add_playlist(entry);
4300 goto goto_next_file;
4304 if(!demuxer)
4306 #if 0
4307 play_tree_t* entry;
4308 // Handle playlist
4309 current_module="handle_playlist";
4310 switch(stream->type){
4311 case STREAMTYPE_VCD:
4312 case STREAMTYPE_DVD:
4313 case STREAMTYPE_CDDA:
4314 case STREAMTYPE_VCDBINCUE:
4315 // don't try to parse raw media as playlist, it's unlikely
4316 goto goto_next_file;
4318 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FallingBackOnPlaylist,filename);
4319 stream_reset(stream);
4320 stream_seek(stream,stream->start_pos);
4321 entry = parse_playtree(stream,0);
4322 if(!entry)
4323 mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_FormatNotRecognized);
4324 else
4325 eof=playtree_add_playlist(entry);
4326 #endif
4327 goto goto_next_file;
4329 inited_flags|=INITED_DEMUXER;
4331 if (stream->type != STREAMTYPE_DVD && stream->type != STREAMTYPE_DVDNAV) {
4332 int i;
4333 // setup global sub numbering
4334 global_sub_indices[SUB_SOURCE_DEMUX] = global_sub_size; // the global # of the first demux-specific sub.
4335 for (i = 0; i < MAX_S_STREAMS; i++)
4336 if (demuxer->s_streams[i])
4337 global_sub_size++;
4340 current_module="demux_open2";
4342 //file_format=demuxer->file_format;
4344 d_audio=demuxer->audio;
4345 d_video=demuxer->video;
4346 d_dvdsub=demuxer->sub;
4348 // DUMP STREAMS:
4349 if((stream_dump_type)&&(stream_dump_type<4)){
4350 FILE *f;
4351 demux_stream_t *ds=NULL;
4352 current_module="dump";
4353 // select stream to dump
4354 switch(stream_dump_type){
4355 case 1: ds=d_audio;break;
4356 case 2: ds=d_video;break;
4357 case 3: ds=d_dvdsub;break;
4359 if(!ds){
4360 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
4361 exit_player(MSGTR_Exit_error);
4363 // disable other streams:
4364 if(d_audio && d_audio!=ds) {ds_free_packs(d_audio); d_audio->id=-2; }
4365 if(d_video && d_video!=ds) {ds_free_packs(d_video); d_video->id=-2; }
4366 if(d_dvdsub && d_dvdsub!=ds) {ds_free_packs(d_dvdsub); d_dvdsub->id=-2; }
4367 // let's dump it!
4368 f=fopen(stream_dump_name,"wb");
4369 if(!f){
4370 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
4371 exit_player(MSGTR_Exit_error);
4373 while(!ds->eof){
4374 unsigned char* start;
4375 int in_size=ds_get_packet(ds,&start);
4376 if( (demuxer->file_format==DEMUXER_TYPE_AVI || demuxer->file_format==DEMUXER_TYPE_ASF || demuxer->file_format==DEMUXER_TYPE_MOV)
4377 && stream_dump_type==2) fwrite(&in_size,1,4,f);
4378 if(in_size>0) fwrite(start,in_size,1,f);
4380 fclose(f);
4381 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
4382 exit_player_with_rc(MSGTR_Exit_eof, 0);
4385 sh_audio=d_audio->sh;
4386 sh_video=d_video->sh;
4388 if(sh_video){
4390 current_module="video_read_properties";
4391 if(!video_read_properties(sh_video)) {
4392 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
4393 sh_video=d_video->sh=NULL;
4394 } else {
4395 mp_msg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
4396 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
4397 sh_video->fps,sh_video->frametime
4400 /* need to set fps here for output encoders to pick it up in their init */
4401 if(force_fps){
4402 sh_video->fps=force_fps;
4403 sh_video->frametime=1.0f/sh_video->fps;
4405 vo_fps = sh_video->fps;
4407 if(!sh_video->fps && !force_fps){
4408 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
4409 sh_video=d_video->sh=NULL;
4415 fflush(stdout);
4417 if(!sh_video && !sh_audio){
4418 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
4419 #ifdef HAS_DVBIN_SUPPORT
4420 if((stream->type == STREAMTYPE_DVB) && stream->priv)
4422 dvb_priv_t *priv = (dvb_priv_t*) stream->priv;
4423 if(priv->is_on)
4425 int dir;
4426 int v = last_dvb_step;
4427 if(v > 0)
4428 dir = DVB_CHANNEL_HIGHER;
4429 else
4430 dir = DVB_CHANNEL_LOWER;
4432 if(dvb_step_channel(priv, dir))
4433 eof = dvbin_reopen = 1;
4436 #endif
4437 goto goto_next_file; // exit_player(MSGTR_Exit_error);
4440 /* display clip info */
4441 demux_info_print(demuxer);
4443 //================== Read SUBTITLES (DVD & TEXT) ==========================
4444 if(vo_spudec==NULL && sh_video &&
4445 (stream->type==STREAMTYPE_DVD || stream->type == STREAMTYPE_DVDNAV || d_dvdsub->id >= 0)){
4446 init_vo_spudec();
4449 // Apply current settings for forced subs
4450 if (vo_spudec!=NULL)
4451 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
4453 if(sh_video) {
4454 // after reading video params we should load subtitles because
4455 // we know fps so now we can adjust subtitle time to ~6 seconds AST
4456 // check .sub
4457 current_module="read_subtitles_file";
4458 if(sub_name){
4459 for (i = 0; sub_name[i] != NULL; ++i)
4460 add_subtitles (sub_name[i], sh_video->fps, 0);
4462 if(sub_auto) { // auto load sub file ...
4463 char *psub = get_path( "sub/" );
4464 char **tmp = sub_filenames((psub ? psub : ""), filename);
4465 int i = 0;
4466 free(psub); // release the buffer created by get_path() above
4467 while (tmp[i]) {
4468 add_subtitles (tmp[i], sh_video->fps, 0);
4469 free(tmp[i++]);
4471 free(tmp);
4473 if (set_of_sub_size > 0) {
4474 // setup global sub numbering
4475 global_sub_indices[SUB_SOURCE_SUBS] = global_sub_size; // the global # of the first sub.
4476 global_sub_size += set_of_sub_size;
4480 if (global_sub_size) {
4481 // find the best sub to use
4482 if (vobsub_id >= 0) {
4483 // if user asks for a vobsub id, use that first.
4484 global_sub_pos = global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_id;
4485 } else if (dvdsub_id >= 0 && global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
4486 // if user asks for a dvd sub id, use that next.
4487 global_sub_pos = global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id;
4488 } else if (global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
4489 // if there are text subs to use, use those. (autosubs come last here)
4490 global_sub_pos = global_sub_indices[SUB_SOURCE_SUBS];
4492 } else if (global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
4493 // if nothing else works, get subs from the demuxer.
4494 global_sub_pos = global_sub_indices[SUB_SOURCE_DEMUX];
4496 } else {
4497 // nothing worth doing automatically.
4498 global_sub_pos = -1;
4500 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
4501 global_sub_pos--;
4502 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL);
4503 if(subdata)
4504 switch (stream_dump_type) {
4505 case 3: list_sub_file(subdata); break;
4506 case 4: dump_mpsub(subdata, sh_video->fps); break;
4507 case 6: dump_srt(subdata, sh_video->fps); break;
4508 case 7: dump_microdvd(subdata, sh_video->fps); break;
4509 case 8: dump_jacosub(subdata, sh_video->fps); break;
4510 case 9: dump_sami(subdata, sh_video->fps); break;
4514 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n", filename);
4515 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", demuxer->desc->name);
4516 if (sh_video) {
4517 /* Assume FOURCC if all bytes >= 0x20 (' ') */
4518 if (sh_video->format >= 0x20202020)
4519 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&sh_video->format);
4520 else
4521 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", sh_video->format);
4522 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", sh_video->i_bps*8);
4523 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", sh_video->disp_w);
4524 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", sh_video->disp_h);
4525 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", sh_video->fps);
4526 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", sh_video->aspect);
4528 if (sh_audio) {
4529 /* Assume FOURCC if all bytes >= 0x20 (' ') */
4530 if (sh_audio->format >= 0x20202020)
4531 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&sh_audio->format);
4532 else
4533 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", sh_audio->format);
4534 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", sh_audio->i_bps*8);
4535 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", sh_audio->samplerate);
4536 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", sh_audio->channels);
4538 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(demuxer));
4540 if(!sh_video) goto main; // audio-only
4542 if(!reinit_video_chain()) {
4543 if(!sh_video){
4544 if(!sh_audio) goto goto_next_file;
4545 goto main; // exit_player(MSGTR_Exit_error);
4549 if(vo_flags & 0x08 && vo_spudec)
4550 spudec_set_hw_spu(vo_spudec,video_out);
4552 #ifdef HAVE_FREETYPE
4553 force_load_font = 1;
4554 #endif
4556 //================== MAIN: ==========================
4557 main:
4558 current_module="main";
4560 if(playing_msg) {
4561 char* msg = m_properties_expand_string(mp_properties,playing_msg);
4562 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
4563 free(msg);
4567 // Disable the term OSD in verbose mode
4568 if(verbose) term_osd = 0;
4569 fflush(stdout);
4572 //int frame_corr_num=0; //
4573 //float v_frame=0; // Video
4574 float time_frame=0; // Timer
4575 //float num_frames=0; // number of frames played
4576 int grab_frames=0;
4578 int frame_time_remaining=0; // flag
4579 int blit_frame=0;
4580 int was_paused=0;
4582 // Make sure old OSD does not stay around,
4583 // e.g. with -fixed-vo and same-resolution files
4584 clear_osd_msgs();
4585 update_osd_msg();
4587 //================ SETUP AUDIO ==========================
4589 if(sh_audio){
4590 reinit_audio_chain();
4591 if (sh_audio && sh_audio->codec)
4592 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", sh_audio->codec->name);
4595 current_module="av_init";
4597 if(sh_video){
4598 sh_video->timer=0;
4599 if (! ignore_start)
4600 audio_delay += sh_video->stream_delay;
4602 if(sh_audio){
4603 if (! ignore_start)
4604 audio_delay -= sh_audio->stream_delay;
4605 sh_audio->delay=-audio_delay;
4608 if(!sh_audio){
4609 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
4610 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",d_audio->packs);
4611 ds_free_packs(d_audio); // free buffered chunks
4612 //d_audio->id=-2; // do not read audio chunks
4613 //uninit_player(INITED_AO); // close device
4615 if(!sh_video){
4616 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
4617 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",d_video->packs);
4618 ds_free_packs(d_video);
4619 d_video->id=-2;
4620 //if(!fixed_vo) uninit_player(INITED_VO);
4623 if (!sh_video && !sh_audio)
4624 goto goto_next_file;
4626 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
4627 if(force_fps && sh_video){
4628 vo_fps = sh_video->fps=force_fps;
4629 sh_video->frametime=1.0f/sh_video->fps;
4630 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,sh_video->fps,sh_video->frametime);
4633 #ifdef HAVE_NEW_GUI
4634 if ( use_gui ) {
4635 if ( sh_audio ) guiIntfStruct.AudioType=sh_audio->channels; else guiIntfStruct.AudioType=0;
4636 if ( !sh_video && sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
4637 guiGetEvent( guiSetFileFormat,(char *)demuxer->file_format );
4638 if ( guiGetEvent( guiSetValues,(char *)sh_video ) ) goto goto_next_file;
4639 guiGetEvent( guiSetDemuxer,(char *)demuxer );
4641 #endif
4643 //==================== START PLAYING =======================
4645 if(loop_times>1) loop_times--; else
4646 if(loop_times==1) loop_times = -1;
4648 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);fflush(stdout);
4650 total_time_usage_start=GetTimer();
4651 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
4652 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
4653 play_n_frames=play_n_frames_mf;
4655 if(play_n_frames==0){
4656 eof=PT_NEXT_ENTRY; goto goto_next_file;
4659 while(!eof){
4660 float aq_sleep_time=0;
4661 if(!sh_audio && d_audio->sh) {
4662 sh_audio = d_audio->sh;
4663 sh_audio->ds = d_audio;
4664 reinit_audio_chain();
4667 /*========================== PLAY AUDIO ============================*/
4669 if (sh_audio)
4670 if (!fill_audio_out_buffers())
4671 // at eof, all audio at least written to ao
4672 if (!sh_video)
4673 eof = PT_NEXT_ENTRY;
4676 if(!sh_video) {
4677 // handle audio-only case:
4678 double a_pos=0;
4679 if(!quiet || end_at.type == END_AT_TIME )
4680 a_pos = playing_audio_pts(sh_audio, d_audio, audio_out);
4682 if(!quiet)
4683 print_status(a_pos, 0, 0);
4685 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
4686 eof = PT_NEXT_ENTRY;
4687 update_osd_msg();
4689 } else {
4691 /*========================== PLAY VIDEO ============================*/
4693 vo_pts=sh_video->timer*90000.0;
4694 vo_fps=sh_video->fps;
4696 if (!frame_time_remaining) {
4697 double frame_time = update_video(&blit_frame);
4698 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
4699 if (sh_video->vf_inited < 0) {
4700 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
4701 eof = 1; goto goto_next_file;
4703 if (frame_time < 0)
4704 eof = 1;
4705 else
4706 time_frame += frame_time / playback_speed; // for nosound
4709 // ==========================================================================
4711 // current_module="draw_osd";
4712 // if(vo_config_count) video_out->draw_osd();
4714 #ifdef HAVE_NEW_GUI
4715 if(use_gui) guiEventHandling();
4716 #endif
4718 current_module="vo_check_events";
4719 if (vo_config_count) video_out->check_events();
4721 #ifdef HAVE_X11
4722 if (stop_xscreensaver) {
4723 current_module = "stop_xscreensaver";
4724 xscreensaver_heartbeat();
4726 #endif
4728 frame_time_remaining = sleep_until_update(&time_frame, &aq_sleep_time);
4730 //====================== FLIP PAGE (VIDEO BLT): =========================
4732 current_module="flip_page";
4733 if (!frame_time_remaining && blit_frame) {
4734 unsigned int t2=GetTimer();
4736 if(vo_config_count) video_out->flip_page();
4738 vout_time_usage += (GetTimer() - t2) * 0.000001;
4740 //====================== A-V TIMESTAMP CORRECTION: =========================
4742 adjust_sync_and_print_status(frame_time_remaining, time_frame);
4744 //============================ Auto QUALITY ============================
4746 /*Output quality adjustments:*/
4747 if(auto_quality>0){
4748 current_module="autoq";
4749 // float total=0.000001f * (GetTimer()-aq_total_time);
4750 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4751 if(output_quality<auto_quality && aq_sleep_time>0)
4752 ++output_quality;
4753 else
4754 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4755 if(output_quality>1 && aq_sleep_time<0)
4756 --output_quality;
4757 else
4758 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4759 output_quality=0;
4760 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4761 set_video_quality(sh_video,output_quality);
4764 if (play_n_frames >= 0 && !frame_time_remaining && blit_frame) {
4765 --play_n_frames;
4766 if (play_n_frames <= 0) eof = PT_NEXT_ENTRY;
4770 // FIXME: add size based support for -endpos
4771 if (end_at.type == END_AT_TIME &&
4772 !frame_time_remaining && end_at.pos <= sh_video->pts)
4773 eof = PT_NEXT_ENTRY;
4775 } // end if(sh_video)
4777 #ifdef USE_DVDNAV
4778 if (stream->type == STREAMTYPE_DVDNAV) {
4779 nav_highlight_t hl;
4780 mp_dvdnav_get_highlight (stream, &hl);
4781 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4782 vo_osd_changed (OSDTYPE_DVDNAV);
4784 #endif
4786 //============================ Handle PAUSE ===============================
4788 current_module="pause";
4790 if(osd_visible){
4791 // 36000000 means max timed visibility is 1 hour into the future, if
4792 // the difference is greater assume it's wrapped around from below 0
4793 if (osd_visible - GetTimerMS() > 36000000) {
4794 osd_visible = 0;
4795 vo_osd_progbar_type=-1; // disable
4796 vo_osd_changed(OSDTYPE_PROGBAR);
4797 if (osd_function != OSD_PAUSE)
4798 osd_function = OSD_PLAY;
4802 if (osd_function == OSD_PAUSE) {
4803 pause_loop();
4804 was_paused = 1;
4807 // handle -sstep
4808 if(step_sec>0) {
4809 osd_function=OSD_FFW;
4810 rel_seek_secs+=step_sec;
4813 //================= EDL =========================================
4815 if( next_edl_record ) { // Are we (still?) doing EDL?
4816 if ( !sh_video ) {
4817 mp_msg( MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video );
4818 free_edl(edl_records);
4819 next_edl_record = NULL;
4820 edl_records = NULL;
4821 } else {
4822 if( sh_video->pts >= next_edl_record->start_sec ) {
4823 if( next_edl_record->action == EDL_SKIP ) {
4824 osd_function = OSD_FFW;
4825 abs_seek_pos = 0;
4826 rel_seek_secs = next_edl_record->length_sec;
4827 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop [%f], length [%f]\n", next_edl_record->start_sec, next_edl_record->stop_sec, next_edl_record->length_sec );
4828 edl_decision = 1;
4829 } else if( next_edl_record->action == EDL_MUTE ) {
4830 edl_muted = !edl_muted;
4831 if ((user_muted | edl_muted) != mixer.muted) mixer_mute(&mixer);
4832 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n", next_edl_record->start_sec );
4834 next_edl_record=next_edl_record->next;
4839 //================= Keyboard events, SEEKing ====================
4841 current_module="key_events";
4844 mp_cmd_t* cmd;
4845 int brk_cmd = 0;
4846 while( !brk_cmd && (cmd = mp_input_get_cmd(0,0,0)) != NULL) {
4847 if(!set_property_command(cmd))
4848 switch(cmd->id) {
4849 case MP_CMD_SEEK : {
4850 float v;
4851 int abs;
4852 if(sh_video)
4853 osd_show_percentage = sh_video->fps;
4854 v = cmd->args[0].v.f;
4855 abs = (cmd->nargs > 1) ? cmd->args[1].v.i : 0;
4856 if(abs==2) { /* Absolute seek to a specific timestamp in seconds */
4857 abs_seek_pos = 1;
4858 if(sh_video)
4859 osd_function= (v > sh_video->pts) ? OSD_FFW : OSD_REW;
4860 rel_seek_secs = v;
4862 else if(abs) { /* Absolute seek by percentage */
4863 abs_seek_pos = 3;
4864 if(sh_video)
4865 osd_function= OSD_FFW; // Direction isn't set correctly
4866 rel_seek_secs = v/100.0;
4868 else {
4869 rel_seek_secs+= v;
4870 osd_function= (v > 0) ? OSD_FFW : OSD_REW;
4872 brk_cmd = 1;
4873 } break;
4874 case MP_CMD_SET_PROPERTY: {
4875 m_option_t* prop = mp_property_find(cmd->args[0].v.s);
4876 if(!prop) mp_msg(MSGT_CPLAYER,MSGL_WARN,"Unknown property: '%s'\n",cmd->args[0].v.s);
4877 else if(m_property_parse(prop,cmd->args[1].v.s) <= 0)
4878 mp_msg(MSGT_CPLAYER,MSGL_WARN,"Failed to set property '%s' to '%s'.\n",
4879 cmd->args[0].v.s,cmd->args[1].v.s);
4881 } break;
4882 case MP_CMD_STEP_PROPERTY: {
4883 m_option_t* prop = mp_property_find(cmd->args[0].v.s);
4884 float arg = cmd->args[1].v.f;
4885 if(!prop) mp_msg(MSGT_CPLAYER,MSGL_WARN, "Unknown property: '%s'\n",cmd->args[0].v.s);
4886 else if(m_property_do(prop,M_PROPERTY_STEP_UP, arg ? &arg : NULL) <= 0)
4887 mp_msg(MSGT_CPLAYER,MSGL_WARN, "Failed to increment property '%s' by %f.\n",cmd->args[0].v.s, arg);
4888 } break;
4889 case MP_CMD_GET_PROPERTY: {
4890 m_option_t* prop;
4891 void* val;
4892 prop = mp_property_find(cmd->args[0].v.s);
4893 if(!prop) {
4894 mp_msg(MSGT_CPLAYER,MSGL_WARN,"Unknown property: '%s'\n",cmd->args[0].v.s);
4895 break;
4897 /* Use m_option_print directly to get easily parseable values. */
4898 val = calloc(1,prop->type->size);
4899 if(m_property_do(prop,M_PROPERTY_GET,val) <= 0) {
4900 mp_msg(MSGT_CPLAYER,MSGL_WARN,"Failed to get value of property '%s'.\n",
4901 cmd->args[0].v.s);
4902 break;
4904 tmp = m_option_print(prop,val);
4905 if(!tmp || tmp == (char*)-1) {
4906 mp_msg(MSGT_CPLAYER,MSGL_WARN,"Failed to print value of property '%s'.\n",
4907 cmd->args[0].v.s);
4908 break;
4910 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_%s=%s\n",cmd->args[0].v.s,tmp);
4911 free(tmp);
4912 } break;
4913 case MP_CMD_EDL_MARK:
4914 if( edl_fd ) {
4915 float v = sh_video ? sh_video->pts :
4916 playing_audio_pts(sh_audio, d_audio, audio_out);
4918 if(begin_skip == MP_NOPTS_VALUE)
4920 begin_skip = v;
4921 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_EdloutStartSkip);
4922 }else{
4923 if(begin_skip > v)
4925 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_EdloutBadStop);
4926 }else{
4927 fprintf(edl_fd, "%f %f %d\n", begin_skip, v, 0);
4928 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_EdloutEndSkip);
4930 begin_skip = MP_NOPTS_VALUE;
4933 break;
4934 case MP_CMD_SWITCH_RATIO : {
4935 if (cmd->nargs == 0 || cmd->args[0].v.f == -1)
4936 movie_aspect = (float) sh_video->disp_w / sh_video->disp_h;
4937 else
4938 movie_aspect = cmd->args[0].v.f;
4939 mpcodecs_config_vo (sh_video, sh_video->disp_w, sh_video->disp_h, 0);
4940 } break;
4941 case MP_CMD_SPEED_INCR : {
4942 float v = cmd->args[0].v.f;
4943 playback_speed += v;
4944 build_afilter_chain(sh_audio, &ao_data);
4945 set_osd_msg(OSD_MSG_SPEED,1,osd_duration,MSGTR_OSDSpeed, playback_speed);
4946 } break;
4947 case MP_CMD_SPEED_MULT : {
4948 float v = cmd->args[0].v.f;
4949 playback_speed *= v;
4950 build_afilter_chain(sh_audio, &ao_data);
4951 set_osd_msg(OSD_MSG_SPEED,1,osd_duration,MSGTR_OSDSpeed, playback_speed);
4952 } break;
4953 case MP_CMD_SPEED_SET : {
4954 float v = cmd->args[0].v.f;
4955 playback_speed = v;
4956 build_afilter_chain(sh_audio, &ao_data);
4957 set_osd_msg(OSD_MSG_SPEED,1,osd_duration,MSGTR_OSDSpeed, playback_speed);
4958 } break;
4959 case MP_CMD_FRAME_STEP :
4960 case MP_CMD_PAUSE : {
4961 cmd->pausing = 1;
4962 brk_cmd = 1;
4963 } break;
4964 case MP_CMD_FILE_FILTER : {
4965 file_filter = cmd->args[0].v.i;
4966 break;
4968 case MP_CMD_QUIT : {
4969 exit_player_with_rc(MSGTR_Exit_quit, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
4971 case MP_CMD_GRAB_FRAMES : {
4972 grab_frames=2;
4973 } break;
4974 case MP_CMD_PLAY_TREE_STEP : {
4975 int n = cmd->args[0].v.i == 0 ? 1 : cmd->args[0].v.i;
4976 int force = cmd->args[1].v.i;
4978 #ifdef HAVE_NEW_GUI
4979 if (use_gui) {
4980 int i=0;
4981 if (n>0)
4982 for (i=0;i<n;i++)
4983 mplNext();
4984 else
4985 for (i=0;i<-1*n;i++)
4986 mplPrev();
4987 } else
4988 #endif
4990 if(!force && playtree_iter) {
4991 play_tree_iter_t* i = play_tree_iter_new_copy(playtree_iter);
4993 if(play_tree_iter_step(i,n,0) == PLAY_TREE_ITER_ENTRY)
4994 eof = (n > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
4995 play_tree_iter_free(i);
4996 } else
4997 eof = (n > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
4998 if(eof)
4999 play_tree_step = n;
5000 brk_cmd = 1;
5002 } break;
5003 case MP_CMD_PLAY_TREE_UP_STEP : {
5004 int n = cmd->args[0].v.i > 0 ? 1 : -1;
5005 int force = cmd->args[1].v.i;
5007 if(!force && playtree_iter) {
5008 play_tree_iter_t* i = play_tree_iter_new_copy(playtree_iter);
5009 if(play_tree_iter_up_step(i,n,0) == PLAY_TREE_ITER_ENTRY)
5010 eof = (n > 0) ? PT_UP_NEXT : PT_UP_PREV;
5011 play_tree_iter_free(i);
5012 } else
5013 eof = (n > 0) ? PT_UP_NEXT : PT_UP_PREV;
5014 brk_cmd = 1;
5015 } break;
5016 case MP_CMD_PLAY_ALT_SRC_STEP : {
5017 if(playtree_iter && playtree_iter->num_files > 1) {
5018 int v = cmd->args[0].v.i;
5019 if(v > 0 && playtree_iter->file < playtree_iter->num_files)
5020 eof = PT_NEXT_SRC;
5021 else if(v < 0 && playtree_iter->file > 1)
5022 eof = PT_PREV_SRC;
5024 brk_cmd = 1;
5025 } break;
5026 case MP_CMD_SUB_STEP : {
5027 if (sh_video) {
5028 int movement = cmd->args[0].v.i;
5029 step_sub(subdata, sh_video->pts, movement);
5030 #ifdef USE_ASS
5031 if (ass_track)
5032 sub_delay += ass_step_sub(ass_track, (sh_video->pts + sub_delay) * 1000 + .5, movement) / 1000.;
5033 #endif
5034 set_osd_msg(OSD_MSG_SUB_DELAY,1,osd_duration,
5035 MSGTR_OSDSubDelay, ROUND(sub_delay*1000));
5037 } break;
5038 case MP_CMD_SUB_LOG : {
5039 log_sub();
5040 } break;
5041 case MP_CMD_OSD : {
5042 int v = cmd->args[0].v.i;
5043 int max = (term_osd && !sh_video) ? MAX_TERM_OSD_LEVEL : MAX_OSD_LEVEL;
5044 if(osd_level > max) osd_level = max;
5045 if(v < 0)
5046 osd_level=(osd_level+1)%(max+1);
5047 else
5048 osd_level= v > max ? max : v;
5049 /* Show OSD state when disabled, but not when an explicit
5050 argument is given to the OSD command, i.e. in slave mode. */
5051 if (v == -1 && osd_level <= 1)
5052 set_osd_msg(OSD_MSG_OSD_STATUS,0,osd_duration,
5053 MSGTR_OSDosd, osd_level ? MSGTR_OSDenabled : MSGTR_OSDdisabled);
5054 else
5055 rm_osd_msg(OSD_MSG_OSD_STATUS);
5056 } break;
5057 case MP_CMD_OSD_SHOW_TEXT : {
5058 set_osd_msg(OSD_MSG_TEXT,cmd->args[2].v.i,
5059 (cmd->args[1].v.i < 0 ? osd_duration : cmd->args[1].v.i),
5060 "%-.63s",cmd->args[0].v.s);
5061 } break;
5062 case MP_CMD_OSD_SHOW_PROPERTY_TEXT : {
5063 char* txt = m_properties_expand_string(mp_properties,cmd->args[0].v.s);
5064 /* if no argument supplied take default osd_duration, else <arg> ms. */
5065 if(txt) {
5066 set_osd_msg(OSD_MSG_TEXT,cmd->args[2].v.i,
5067 (cmd->args[1].v.i < 0 ? osd_duration : cmd->args[1].v.i),
5068 "%-.63s",txt);
5069 free(txt);
5071 } break;
5072 case MP_CMD_LOADFILE : {
5073 play_tree_t* e = play_tree_new();
5074 play_tree_add_file(e,cmd->args[0].v.s);
5076 if (cmd->args[1].v.i) // append
5077 play_tree_append_entry(playtree, e);
5078 else {
5079 // Go back to the starting point.
5080 while(play_tree_iter_up_step(playtree_iter,0,1) != PLAY_TREE_ITER_END)
5081 /* NOP */;
5082 play_tree_free_list(playtree->child,1);
5083 play_tree_set_child(playtree,e);
5084 play_tree_iter_step(playtree_iter,0,0);
5085 eof = PT_NEXT_SRC;
5087 brk_cmd = 1;
5088 } break;
5089 case MP_CMD_LOADLIST : {
5090 play_tree_t* e = parse_playlist_file(cmd->args[0].v.s);
5091 if(!e)
5092 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_PlaylistLoadUnable,cmd->args[0].v.s);
5093 else {
5094 if (cmd->args[1].v.i) // append
5095 play_tree_append_entry(playtree, e);
5096 else {
5097 // Go back to the starting point.
5098 while(play_tree_iter_up_step(playtree_iter,0,1) != PLAY_TREE_ITER_END)
5099 /* NOP */;
5100 play_tree_free_list(playtree->child,1);
5101 play_tree_set_child(playtree,e);
5102 play_tree_iter_step(playtree_iter,0,0);
5103 eof = PT_NEXT_SRC;
5106 brk_cmd = 1;
5107 } break;
5108 #ifdef USE_RADIO
5109 case MP_CMD_RADIO_STEP_CHANNEL : {
5110 if (demuxer->stream->type==STREAMTYPE_RADIO) {
5111 int v = cmd->args[0].v.i;
5112 if(v > 0)
5113 radio_step_channel(demuxer->stream, RADIO_CHANNEL_HIGHER);
5114 else
5115 radio_step_channel(demuxer->stream, RADIO_CHANNEL_LOWER);
5116 if (radio_get_channel_name(demuxer->stream)) {
5117 set_osd_msg(OSD_MSG_RADIO_CHANNEL,1,osd_duration,
5118 MSGTR_OSDChannel, radio_get_channel_name(demuxer->stream));
5121 } break;
5122 case MP_CMD_RADIO_SET_CHANNEL : {
5123 if (demuxer->stream->type== STREAMTYPE_RADIO) {
5124 radio_set_channel(demuxer->stream, cmd->args[0].v.s);
5125 if (radio_get_channel_name(demuxer->stream)) {
5126 set_osd_msg(OSD_MSG_RADIO_CHANNEL,1,osd_duration,
5127 MSGTR_OSDChannel, radio_get_channel_name(demuxer->stream));
5130 } break;
5131 case MP_CMD_RADIO_SET_FREQ : {
5132 if (demuxer->stream->type== STREAMTYPE_RADIO)
5133 radio_set_freq(demuxer->stream, cmd->args[0].v.f);
5134 } break;
5135 case MP_CMD_RADIO_STEP_FREQ :
5136 if (demuxer->stream->type == STREAMTYPE_RADIO){
5137 radio_step_freq(demuxer->stream, cmd->args[0].v.f);
5139 break;
5140 #endif
5141 #ifdef USE_TV
5142 case MP_CMD_TV_SET_FREQ : {
5143 if (file_format == DEMUXER_TYPE_TV)
5144 tv_set_freq((tvi_handle_t*)(demuxer->priv), cmd->args[0].v.f * 16.0);
5145 } break;
5146 case MP_CMD_TV_SET_NORM : {
5147 if (file_format == DEMUXER_TYPE_TV)
5148 tv_set_norm((tvi_handle_t*)(demuxer->priv), cmd->args[0].v.s);
5149 } break;
5150 case MP_CMD_TV_STEP_CHANNEL : {
5151 if (file_format == DEMUXER_TYPE_TV) {
5152 int v = cmd->args[0].v.i;
5153 if(v > 0){
5154 tv_step_channel((tvi_handle_t*)(demuxer->priv), TV_CHANNEL_HIGHER);
5155 } else {
5156 tv_step_channel((tvi_handle_t*)(demuxer->priv), TV_CHANNEL_LOWER);
5158 if (tv_channel_list) {
5159 set_osd_msg(OSD_MSG_TV_CHANNEL,1,osd_duration,
5160 MSGTR_OSDChannel, tv_channel_current->name);
5161 //vo_osd_changed(OSDTYPE_SUBTITLE);
5165 #ifdef HAS_DVBIN_SUPPORT
5166 if((stream->type == STREAMTYPE_DVB) && stream->priv)
5168 dvb_priv_t *priv = (dvb_priv_t*) stream->priv;
5169 if(priv->is_on)
5171 int dir;
5172 int v = cmd->args[0].v.i;
5174 last_dvb_step = v;
5175 if(v > 0)
5176 dir = DVB_CHANNEL_HIGHER;
5177 else
5178 dir = DVB_CHANNEL_LOWER;
5181 if(dvb_step_channel(priv, dir))
5182 eof = dvbin_reopen = 1;
5185 #endif /* HAS_DVBIN_SUPPORT */
5186 break;
5187 case MP_CMD_TV_SET_CHANNEL : {
5188 if (file_format == DEMUXER_TYPE_TV) {
5189 tv_set_channel((tvi_handle_t*)(demuxer->priv), cmd->args[0].v.s);
5190 if (tv_channel_list) {
5191 set_osd_msg(OSD_MSG_TV_CHANNEL,1,osd_duration,
5192 MSGTR_OSDChannel, tv_channel_current->name);
5193 //vo_osd_changed(OSDTYPE_SUBTITLE);
5196 } break;
5197 #ifdef HAS_DVBIN_SUPPORT
5198 case MP_CMD_DVB_SET_CHANNEL:
5200 if((stream->type == STREAMTYPE_DVB) && stream->priv)
5202 dvb_priv_t *priv = (dvb_priv_t*) stream->priv;
5203 if(priv->is_on)
5205 if(priv->list->current <= cmd->args[0].v.i)
5206 last_dvb_step = 1;
5207 else
5208 last_dvb_step = -1;
5210 if(dvb_set_channel(priv, cmd->args[1].v.i, cmd->args[0].v.i))
5211 eof = dvbin_reopen = 1;
5215 break;
5216 #endif /* HAS_DVBIN_SUPPORT */
5217 case MP_CMD_TV_LAST_CHANNEL : {
5218 if (file_format == DEMUXER_TYPE_TV) {
5219 tv_last_channel((tvi_handle_t*)(demuxer->priv));
5220 if (tv_channel_list) {
5221 set_osd_msg(OSD_MSG_TV_CHANNEL,1,osd_duration,
5222 MSGTR_OSDChannel, tv_channel_current->name);
5223 //vo_osd_changed(OSDTYPE_SUBTITLE);
5226 } break;
5227 case MP_CMD_TV_STEP_NORM : {
5228 if (file_format == DEMUXER_TYPE_TV)
5229 tv_step_norm((tvi_handle_t*)(demuxer->priv));
5230 } break;
5231 case MP_CMD_TV_STEP_CHANNEL_LIST : {
5232 if (file_format == DEMUXER_TYPE_TV)
5233 tv_step_chanlist((tvi_handle_t*)(demuxer->priv));
5234 } break;
5235 #endif /* USE_TV */
5236 case MP_CMD_SUB_LOAD:
5238 if (sh_video) {
5239 int n = set_of_sub_size;
5240 add_subtitles(cmd->args[0].v.s, sh_video->fps, 0);
5241 if (n != set_of_sub_size) {
5242 if (global_sub_indices[SUB_SOURCE_SUBS] < 0)
5243 global_sub_indices[SUB_SOURCE_SUBS] = global_sub_size;
5244 ++global_sub_size;
5247 } break;
5248 case MP_CMD_SUB_REMOVE:
5250 if (sh_video) {
5251 int v = cmd->args[0].v.i;
5252 sub_data *subd;
5253 if (v < 0) {
5254 for (v = 0; v < set_of_sub_size; ++v) {
5255 subd = set_of_subtitles[v];
5256 mp_msg(MSGT_CPLAYER, MSGL_STATUS, MSGTR_RemovedSubtitleFile, v + 1, subd->filename);
5257 sub_free(subd);
5258 set_of_subtitles[v] = NULL;
5260 global_sub_indices[SUB_SOURCE_SUBS] = -1;
5261 global_sub_size -= set_of_sub_size;
5262 set_of_sub_size = 0;
5263 if (set_of_sub_pos >= 0) {
5264 global_sub_pos = -2;
5265 vo_sub_last = vo_sub = NULL;
5266 vo_osd_changed(OSDTYPE_SUBTITLE);
5267 vo_update_osd(sh_video->disp_w, sh_video->disp_h);
5268 mp_input_queue_cmd(mp_input_parse_cmd("sub_select"));
5271 else if (v < set_of_sub_size) {
5272 subd = set_of_subtitles[v];
5273 mp_msg(MSGT_CPLAYER, MSGL_STATUS, MSGTR_RemovedSubtitleFile, v + 1, subd->filename);
5274 sub_free(subd);
5275 if (set_of_sub_pos == v) {
5276 global_sub_pos = -2;
5277 vo_sub_last = vo_sub = NULL;
5278 vo_osd_changed(OSDTYPE_SUBTITLE);
5279 vo_update_osd(sh_video->disp_w, sh_video->disp_h);
5280 mp_input_queue_cmd(mp_input_parse_cmd("sub_select"));
5282 else if (set_of_sub_pos > v) {
5283 --set_of_sub_pos;
5284 --global_sub_pos;
5286 while (++v < set_of_sub_size)
5287 set_of_subtitles[v - 1] = set_of_subtitles[v];
5288 --set_of_sub_size;
5289 --global_sub_size;
5290 if (set_of_sub_size <= 0)
5291 global_sub_indices[SUB_SOURCE_SUBS] = -1;
5292 set_of_subtitles[set_of_sub_size] = NULL;
5295 } break;
5296 case MP_CMD_GET_SUB_VISIBILITY:
5298 if (sh_video) {
5299 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_SUB_VISIBILITY=%d\n", sub_visibility);
5301 } break;
5302 case MP_CMD_SCREENSHOT :
5303 if(vo_config_count){
5304 mp_msg(MSGT_CPLAYER,MSGL_INFO,"sending VFCTRL_SCREENSHOT!\n");
5305 if(CONTROL_OK!=((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_SCREENSHOT, &cmd->args[0].v.i))
5306 video_out->control(VOCTRL_SCREENSHOT, NULL);
5308 break;
5309 case MP_CMD_VF_CHANGE_RECTANGLE:
5310 set_rectangle(sh_video, cmd->args[0].v.i, cmd->args[1].v.i);
5311 break;
5313 case MP_CMD_GET_TIME_LENGTH : {
5314 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_LENGTH=%.2lf\n", demuxer_get_time_length(demuxer));
5315 } break;
5317 case MP_CMD_GET_FILENAME : {
5318 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_FILENAME='%s'\n", get_metadata (META_NAME));
5319 } break;
5321 case MP_CMD_GET_VIDEO_CODEC : {
5322 char *inf = get_metadata (META_VIDEO_CODEC);
5323 if (!inf) inf = strdup ("");
5324 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_VIDEO_CODEC='%s'\n", inf);
5325 free (inf);
5326 } break;
5328 case MP_CMD_GET_VIDEO_BITRATE : {
5329 char *inf = get_metadata (META_VIDEO_BITRATE);
5330 if (!inf) inf = strdup ("");
5331 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_VIDEO_BITRATE='%s'\n", inf);
5332 free (inf);
5333 } break;
5335 case MP_CMD_GET_VIDEO_RESOLUTION : {
5336 char *inf = get_metadata (META_VIDEO_RESOLUTION);
5337 if (!inf) inf = strdup ("");
5338 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_VIDEO_RESOLUTION='%s'\n", inf);
5339 free (inf);
5340 } break;
5342 case MP_CMD_GET_AUDIO_CODEC : {
5343 char *inf = get_metadata (META_AUDIO_CODEC);
5344 if (!inf) inf = strdup ("");
5345 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_AUDIO_CODEC='%s'\n", inf);
5346 free (inf);
5347 } break;
5349 case MP_CMD_GET_AUDIO_BITRATE : {
5350 char *inf = get_metadata (META_AUDIO_BITRATE);
5351 if (!inf) inf = strdup ("");
5352 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_AUDIO_BITRATE='%s'\n", inf);
5353 free (inf);
5354 } break;
5356 case MP_CMD_GET_AUDIO_SAMPLES : {
5357 char *inf = get_metadata (META_AUDIO_SAMPLES);
5358 if (!inf) inf = strdup ("");
5359 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_AUDIO_SAMPLES='%s'\n", inf);
5360 free (inf);
5361 } break;
5363 case MP_CMD_GET_META_TITLE : {
5364 char *inf = get_metadata (META_INFO_TITLE);
5365 if (!inf) inf = strdup ("");
5366 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_META_TITLE='%s'\n", inf);
5367 free (inf);
5368 } break;
5370 case MP_CMD_GET_META_ARTIST : {
5371 char *inf = get_metadata (META_INFO_ARTIST);
5372 if (!inf) inf = strdup ("");
5373 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_META_ARTIST='%s'\n", inf);
5374 free (inf);
5375 } break;
5377 case MP_CMD_GET_META_ALBUM : {
5378 char *inf = get_metadata (META_INFO_ALBUM);
5379 if (!inf) inf = strdup ("");
5380 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_META_ALBUM='%s'\n", inf);
5381 free (inf);
5382 } break;
5384 case MP_CMD_GET_META_YEAR : {
5385 char *inf = get_metadata (META_INFO_YEAR);
5386 if (!inf) inf = strdup ("");
5387 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_META_YEAR='%s'\n", inf);
5388 free (inf);
5389 } break;
5391 case MP_CMD_GET_META_COMMENT : {
5392 char *inf = get_metadata (META_INFO_COMMENT);
5393 if (!inf) inf = strdup ("");
5394 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_META_COMMENT='%s'\n", inf);
5395 free (inf);
5396 } break;
5398 case MP_CMD_GET_META_TRACK : {
5399 char *inf = get_metadata (META_INFO_TRACK);
5400 if (!inf) inf = strdup ("");
5401 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_META_TRACK='%s'\n", inf);
5402 free (inf);
5403 } break;
5405 case MP_CMD_GET_META_GENRE : {
5406 char *inf = get_metadata (META_INFO_GENRE);
5407 if (!inf) inf = strdup ("");
5408 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_META_GENRE='%s'\n", inf);
5409 free (inf);
5410 } break;
5412 case MP_CMD_GET_VO_FULLSCREEN : {
5413 if(video_out && vo_config_count)
5414 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_VO_FULLSCREEN=%d\n", vo_fs);
5415 } break;
5417 case MP_CMD_GET_PERCENT_POS : {
5418 mp_msg(MSGT_GLOBAL,MSGL_INFO, "ANS_PERCENT_POSITION=%d\n", demuxer_get_percent_pos(demuxer));
5419 } break;
5420 case MP_CMD_GET_TIME_POS : {
5421 float pos = 0;
5422 if (sh_video)
5423 pos = sh_video->pts;
5424 else
5425 if (sh_audio && audio_out)
5426 pos = playing_audio_pts(sh_audio, d_audio, audio_out);
5427 mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_TIME_POSITION=%.1f\n", pos);
5428 } break;
5429 case MP_CMD_RUN : {
5430 #ifndef __MINGW32__
5431 if(!fork()) {
5432 execl("/bin/sh","sh","-c",cmd->args[0].v.s,NULL);
5433 exit(0);
5435 #endif
5436 } break;
5437 case MP_CMD_KEYDOWN_EVENTS : {
5438 mplayer_put_key(cmd->args[0].v.i);
5439 } break;
5440 case MP_CMD_SEEK_CHAPTER : {
5441 int seek = cmd->args[0].v.i;
5442 int abs = (cmd->nargs > 1) ? cmd->args[1].v.i : 0;
5443 int chap;
5444 float next_pts = 0;
5445 int num_chapters;
5446 char *chapter_name;
5448 rel_seek_secs = 0;
5449 abs_seek_pos = 0;
5450 chap = demuxer_seek_chapter(demuxer, seek, abs, &next_pts, &num_chapters, &chapter_name);
5451 if(chap != -1) {
5452 if(next_pts > -1.0) {
5453 abs_seek_pos = 1;
5454 rel_seek_secs = next_pts;
5456 if(chapter_name) {
5457 set_osd_msg(OSD_MSG_TEXT, 1, osd_duration, MSGTR_OSDChapter,
5458 chap+1, chapter_name);
5459 free(chapter_name);
5461 } else {
5462 if (seek > 0)
5463 rel_seek_secs = 1000000000.;
5464 else
5465 set_osd_msg(OSD_MSG_TEXT, 1, osd_duration, MSGTR_OSDChapter, 0, MSGTR_Unknown);
5467 break;
5468 } break;
5469 case MP_CMD_SET_MOUSE_POS: {
5470 int button = -1, pointer_x, pointer_y;
5471 double dx, dy;
5472 pointer_x = cmd->args[0].v.i;
5473 pointer_y = cmd->args[1].v.i;
5474 rescale_input_coordinates(pointer_x, pointer_y, &dx, &dy);
5475 #ifdef USE_DVDNAV
5476 if(stream->type == STREAMTYPE_DVDNAV && dx > 0.0 && dy > 0.0) {
5477 pointer_x = (int) (dx * (double) sh_video->disp_w);
5478 pointer_y = (int) (dy * (double) sh_video->disp_h);
5479 mp_dvdnav_update_mouse_pos(stream, pointer_x, pointer_y, &button);
5480 if(button>0) set_osd_msg(OSD_MSG_TEXT, 1, osd_duration, "Selected button number %d", button);
5482 #endif
5483 break;
5485 #ifdef USE_DVDNAV
5486 case MP_CMD_DVDNAV: {
5487 int button = -1;
5488 if(stream->type != STREAMTYPE_DVDNAV) break;
5490 if(mp_dvdnav_handle_input(stream, cmd->args[0].v.i, &button)) {
5491 uninit_player(INITED_ALL-(INITED_STREAM|INITED_INPUT|(fixed_vo ? INITED_VO : 0)));
5492 goto goto_enable_cache;
5493 } else if(button>0) set_osd_msg(OSD_MSG_TEXT, 1, osd_duration, "Selected button number %d", button);
5494 break;
5496 #endif
5497 default : {
5498 #ifdef HAVE_NEW_GUI
5499 if ( ( use_gui )&&( cmd->id > MP_CMD_GUI_EVENTS ) ) guiGetEvent( guiIEvent,(char *)cmd->id );
5500 else
5501 #endif
5502 mp_msg(MSGT_CPLAYER, MSGL_V, "Received unknown cmd %s\n",cmd->name);
5505 switch (cmd->pausing) {
5506 case 1: // "pausing"
5507 osd_function = OSD_PAUSE;
5508 break;
5509 case 3: // "pausing_toggle"
5510 was_paused = !was_paused;
5511 // fall through
5512 case 2: // "pausing_keep"
5513 if (was_paused) osd_function = OSD_PAUSE;
5515 mp_cmd_free(cmd);
5518 was_paused = 0;
5520 if (seek_to_sec) {
5521 int a,b; float d;
5523 if (sscanf(seek_to_sec, "%d:%d:%f", &a,&b,&d)==3)
5524 rel_seek_secs += 3600*a +60*b +d ;
5525 else if (sscanf(seek_to_sec, "%d:%f", &a, &d)==2)
5526 rel_seek_secs += 60*a +d;
5527 else if (sscanf(seek_to_sec, "%f", &d)==1)
5528 rel_seek_secs += d;
5530 seek_to_sec = NULL;
5533 if (end_at.type != END_AT_NONE) {
5534 if(end_at.type == END_AT_SIZE) {
5535 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
5536 end_at.type = END_AT_NONE;
5537 } else {
5538 end_at.pos += rel_seek_secs;
5542 /* Looping. */
5543 if(eof==1 && loop_times>=0) {
5544 int l = loop_times;
5545 play_tree_iter_step(playtree_iter,0,0);
5546 loop_times = l;
5547 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d, eof = %d\n", loop_times,eof);
5549 if(loop_times>1) loop_times--; else
5550 if(loop_times==1) loop_times=-1;
5551 play_n_frames=play_n_frames_mf;
5552 eof=0;
5553 abs_seek_pos=3; rel_seek_secs=0; // seek to start of movie (0%)
5554 loop_seek = 1;
5557 if(rel_seek_secs || abs_seek_pos){
5558 current_module="seek";
5559 if(demux_seek(demuxer,rel_seek_secs,audio_delay,abs_seek_pos)){
5560 // success:
5561 /* FIXME there should be real seeking for vobsub */
5562 if(sh_video) sh_video->pts=d_video->pts;
5563 if (vo_vobsub)
5564 //vobsub_reset(vo_vobsub);
5565 vobsub_seek(vo_vobsub,sh_video->pts);
5566 fflush(stdout);
5568 if(sh_video){
5569 current_module="seek_video_reset";
5570 resync_video_stream(sh_video);
5571 if(vo_config_count) video_out->control(VOCTRL_RESET,NULL);
5572 sh_video->num_buffered_pts = 0;
5573 sh_video->last_pts = MP_NOPTS_VALUE;
5576 if(sh_audio){
5577 current_module="seek_audio_reset";
5578 audio_out->reset(); // stop audio, throwing away buffered data
5579 sh_audio->a_buffer_len = 0;
5580 sh_audio->a_out_buffer_len = 0;
5582 // Set OSD:
5583 if(!loop_seek){
5584 if( !edl_decision )
5585 set_osd_bar(0,"Position",0,100,demuxer_get_percent_pos(demuxer));
5588 if(sh_video) {
5589 c_total=0;
5590 max_pts_correction=0.1;
5591 osd_visible=(GetTimerMS() + 1000) | 1; // to revert to PLAY pointer after 1 sec
5592 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
5593 drop_frame_cnt=0;
5595 if(vo_spudec) spudec_reset(vo_spudec);
5599 * We saw a seek, have to rewind the EDL operations stack
5600 * and find the next EDL action to take care of.
5603 edl_muted = 0;
5604 next_edl_record = edl_records;
5606 while (next_edl_record)
5608 /* Trying to remember if we need to mute/unmute first;
5609 * prior EDL implementation lacks this.
5612 if (next_edl_record->start_sec >= sh_video->pts)
5613 break;
5615 if (next_edl_record->action == EDL_MUTE) edl_muted = !edl_muted;
5616 next_edl_record = next_edl_record->next;
5619 if ((user_muted | edl_muted) != mixer.muted) mixer_mute(&mixer);
5621 rel_seek_secs=0;
5622 abs_seek_pos=0;
5623 frame_time_remaining=0;
5624 current_module=NULL;
5625 loop_seek=0;
5628 #ifdef HAVE_NEW_GUI
5629 if(use_gui){
5630 guiEventHandling();
5631 if(demuxer->file_format==DEMUXER_TYPE_AVI && sh_video && sh_video->video.dwLength>2){
5632 // get pos from frame number / total frames
5633 guiIntfStruct.Position=(float)d_video->pack_no*100.0f/sh_video->video.dwLength;
5634 } else {
5635 off_t len = ( demuxer->movi_end - demuxer->movi_start );
5636 off_t pos = ( demuxer->file_format == DEMUXER_TYPE_AUDIO?stream->pos:demuxer->filepos );
5637 guiIntfStruct.Position=(len <= 0? 0.0f : ( pos - demuxer->movi_start ) * 100.0f / len );
5639 if ( sh_video ) guiIntfStruct.TimeSec=sh_video->pts;
5640 else if ( sh_audio ) guiIntfStruct.TimeSec=sh_audio->delay;
5641 guiIntfStruct.LengthInSec=demuxer_get_time_length(demuxer);
5642 guiGetEvent( guiReDraw,NULL );
5643 guiGetEvent( guiSetVolume,NULL );
5644 if(guiIntfStruct.Playing==0) break; // STOP
5645 if(guiIntfStruct.Playing==2) osd_function=OSD_PAUSE;
5646 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
5647 #ifdef USE_DVDREAD
5648 if ( stream->type == STREAMTYPE_DVD )
5650 dvd_priv_t * dvdp = stream->priv;
5651 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
5653 #endif
5655 #endif /* HAVE_NEW_GUI */
5657 } // while(!eof)
5659 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",eof);
5661 #ifdef HAS_DVBIN_SUPPORT
5662 if(dvbin_reopen)
5664 eof = 0;
5665 uninit_player(INITED_ALL-(INITED_STREAM|INITED_INPUT));
5666 cache_uninit(stream);
5667 dvbin_reopen = 0;
5668 goto goto_enable_cache;
5670 #endif
5673 goto_next_file: // don't jump here after ao/vo/getch initialization!
5675 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
5677 if(benchmark){
5678 double tot=video_time_usage+vout_time_usage+audio_time_usage;
5679 double total_time_usage;
5680 total_time_usage_start=GetTimer()-total_time_usage_start;
5681 total_time_usage = (float)total_time_usage_start*0.000001;
5682 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
5683 video_time_usage,vout_time_usage,audio_time_usage,
5684 total_time_usage-tot,total_time_usage);
5685 if(total_time_usage>0.0)
5686 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
5687 100.0*video_time_usage/total_time_usage,
5688 100.0*vout_time_usage/total_time_usage,
5689 100.0*audio_time_usage/total_time_usage,
5690 100.0*(total_time_usage-tot)/total_time_usage,
5691 100.0);
5692 if(total_frame_cnt && frame_dropping)
5693 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
5694 total_frame_cnt-drop_frame_cnt,
5695 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
5696 drop_frame_cnt,
5697 100*drop_frame_cnt/total_frame_cnt,
5698 total_frame_cnt,
5699 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
5703 // time to uninit all, except global stuff:
5704 uninit_player(INITED_ALL-(INITED_GUI+INITED_INPUT+(fixed_vo?INITED_VO:0)));
5706 if ( set_of_sub_size > 0 )
5708 current_module="sub_free";
5709 for (i = 0; i < set_of_sub_size; ++i) {
5710 sub_free( set_of_subtitles[i] );
5711 #ifdef USE_ASS
5712 if ( set_of_ass_tracks[i] )
5713 ass_free_track( set_of_ass_tracks[i] );
5714 #endif
5716 set_of_sub_size = 0;
5718 vo_sub_last = vo_sub=NULL;
5719 subdata=NULL;
5720 #ifdef USE_ASS
5721 ass_track = NULL;
5722 #endif
5724 if(eof == PT_NEXT_ENTRY || eof == PT_PREV_ENTRY) {
5725 eof = eof == PT_NEXT_ENTRY ? 1 : -1;
5726 if(play_tree_iter_step(playtree_iter,play_tree_step,0) == PLAY_TREE_ITER_ENTRY) {
5727 eof = 1;
5728 } else {
5729 play_tree_iter_free(playtree_iter);
5730 playtree_iter = NULL;
5732 play_tree_step = 1;
5733 } else if (eof == PT_UP_NEXT || eof == PT_UP_PREV) {
5734 eof = eof == PT_UP_NEXT ? 1 : -1;
5735 if ( playtree_iter ) {
5736 if(play_tree_iter_up_step(playtree_iter,eof,0) == PLAY_TREE_ITER_ENTRY) {
5737 eof = 1;
5738 } else {
5739 play_tree_iter_free(playtree_iter);
5740 playtree_iter = NULL;
5743 } else { // NEXT PREV SRC
5744 eof = eof == PT_PREV_SRC ? -1 : 1;
5747 if(eof == 0) eof = 1;
5749 while(playtree_iter != NULL) {
5750 filename = play_tree_iter_get_file(playtree_iter,eof);
5751 if(filename == NULL) {
5752 if( play_tree_iter_step(playtree_iter,eof,0) != PLAY_TREE_ITER_ENTRY) {
5753 play_tree_iter_free(playtree_iter);
5754 playtree_iter = NULL;
5756 } else
5757 break;
5760 #ifdef HAVE_NEW_GUI
5761 if( use_gui && !playtree_iter )
5763 #ifdef USE_DVDREAD
5764 if ( !guiIntfStruct.DiskChanged )
5765 #endif
5766 mplEnd();
5768 #endif
5770 if(use_gui || playtree_iter != NULL || player_idle_mode){
5771 if (!playtree_iter) filename = NULL;
5772 eof = 0;
5773 goto play_next_file;
5777 exit_player_with_rc(MSGTR_Exit_eof, 0);
5779 return 1;