Merge svn changes up to r28537
[mplayer.git] / mplayer.c
blob41dff2a72f4d14b1cc41381b4fc54339eb934c96
2 /// \file
3 /// \ingroup Properties Command2Property OSDMsgStack
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdbool.h>
8 #include "config.h"
9 #include "talloc.h"
11 #if defined(__MINGW32__) || defined(__CYGWIN__)
12 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
13 #include <windows.h>
14 #endif
15 #include <string.h>
16 #include <unistd.h>
18 // #include <sys/mman.h>
19 #include <sys/types.h>
20 #ifndef __MINGW32__
21 #include <sys/ioctl.h>
22 #include <sys/wait.h>
23 #else
24 #define SIGHUP 1 /* hangup */
25 #define SIGQUIT 3 /* quit */
26 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
27 #define SIGBUS 10 /* bus error */
28 #define SIGPIPE 13 /* broken pipe */
29 #endif
31 #include <sys/time.h>
32 #include <sys/stat.h>
34 #include <signal.h>
35 #include <time.h>
36 #include <fcntl.h>
37 #include <limits.h>
39 #include <errno.h>
41 #include "mp_msg.h"
43 #define HELP_MP_DEFINE_STATIC
44 #include "help_mp.h"
46 #include "m_option.h"
47 #include "m_config.h"
48 #include "mplayer.h"
49 #include "access_mpcontext.h"
50 #include "m_property.h"
52 #include "cfg-mplayer-def.h"
54 #include "libavutil/intreadwrite.h"
56 #include "subreader.h"
58 #include "mp_osd.h"
59 #include "libvo/video_out.h"
61 #include "libvo/font_load.h"
62 #include "libvo/sub.h"
64 #ifdef CONFIG_X11
65 #include "libvo/x11_common.h"
66 #endif
68 #include "libao2/audio_out.h"
70 #include "codec-cfg.h"
72 #include "edl.h"
74 #include "spudec.h"
75 #include "vobsub.h"
77 #include "osdep/getch2.h"
78 #include "osdep/timer.h"
80 #ifdef CONFIG_GUI
81 #include "gui/interface.h"
82 #endif
84 #include "input/input.h"
86 const int under_mencoder = 0;
87 int slave_mode=0;
88 int player_idle_mode=0;
89 int quiet=0;
90 int enable_mouse_movements=0;
91 float start_volume = -1;
93 #include "osdep/priority.h"
95 char *heartbeat_cmd;
97 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
99 #ifdef HAVE_RTC
100 #ifdef __linux__
101 #include <linux/rtc.h>
102 #else
103 #include <rtc.h>
104 #define RTC_IRQP_SET RTCIO_IRQP_SET
105 #define RTC_PIE_ON RTCIO_PIE_ON
106 #endif /* __linux__ */
107 #endif /* HAVE_RTC */
109 #ifdef CONFIG_TV
110 #include "stream/tv.h"
111 #endif
112 #ifdef CONFIG_RADIO
113 #include "stream/stream_radio.h"
114 #endif
116 #ifdef CONFIG_DVBIN
117 #include "stream/dvbin.h"
118 #include "stream/cache2.h"
119 #endif
121 //**************************************************************************//
122 // Playtree
123 //**************************************************************************//
124 #include "playtree.h"
125 #include "playtreeparser.h"
127 int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
128 int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
130 //**************************************************************************//
131 // Config
132 //**************************************************************************//
133 #include "parser-cfg.h"
134 #include "parser-mpcmd.h"
136 //**************************************************************************//
137 // Config file
138 //**************************************************************************//
140 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
142 #include "get_path.h"
144 //**************************************************************************//
145 //**************************************************************************//
146 // Input media streaming & demultiplexer:
147 //**************************************************************************//
149 static int max_framesize=0;
151 #include "stream/stream.h"
152 #include "libmpdemux/demuxer.h"
153 #include "libmpdemux/stheader.h"
155 #ifdef CONFIG_DVDREAD
156 #include "stream/stream_dvd.h"
157 #endif
159 #ifdef CONFIG_DVDNAV
160 #include "stream/stream_dvdnav.h"
161 #endif
163 #include "libmpcodecs/dec_audio.h"
164 #include "libmpcodecs/dec_video.h"
165 #include "libmpcodecs/mp_image.h"
166 #include "libmpcodecs/vf.h"
167 #include "libmpcodecs/vd.h"
169 #include "mixer.h"
171 #include "mp_core.h"
172 #include "options.h"
173 #include "defaultopts.h"
175 //**************************************************************************//
176 //**************************************************************************//
178 // Common FIFO functions, and keyboard/event FIFO code
179 #include "mp_fifo.h"
180 int noconsolecontrols=0;
181 //**************************************************************************//
183 // benchmark:
184 double video_time_usage=0;
185 double vout_time_usage=0;
186 static double audio_time_usage=0;
187 static int total_time_usage_start=0;
188 static int total_frame_cnt=0;
189 static int drop_frame_cnt=0; // total number of dropped frames
190 int benchmark=0;
192 // options:
193 int auto_quality=0;
194 static int output_quality=0;
196 int use_gui=0;
198 #ifdef CONFIG_GUI
199 int enqueue=0;
200 #endif
202 static int list_properties = 0;
204 int osd_level=1;
205 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
206 unsigned int osd_visible;
207 int osd_duration = 1000;
209 int term_osd = 1;
210 static char* term_osd_esc = "\x1b[A\r\x1b[K";
211 static char* playing_msg = NULL;
212 // seek:
213 static double seek_to_sec;
214 static off_t seek_to_byte=0;
215 static off_t step_sec=0;
216 static int loop_seek=0;
218 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
220 // A/V sync:
221 int autosync=0; // 30 might be a good default value.
223 // codecs:
224 char **audio_codec_list=NULL; // override audio codec
225 char **video_codec_list=NULL; // override video codec
226 char **audio_fm_list=NULL; // override audio codec family
227 char **video_fm_list=NULL; // override video codec family
229 // demuxer:
230 extern char *demuxer_name; // override demuxer
231 extern char *audio_demuxer_name; // override audio demuxer
232 extern char *sub_demuxer_name; // override sub demuxer
234 int vobsub_id=-1;
235 char* audio_lang=NULL;
236 char* dvdsub_lang=NULL;
237 static char* spudec_ifo=NULL;
238 int forced_subs_only=0;
239 int file_filter=1;
241 // cache2:
242 int stream_cache_size=-1;
243 #ifdef CONFIG_STREAM_CACHE
244 extern int cache_fill_status;
246 float stream_cache_min_percent=20.0;
247 float stream_cache_seek_min_percent=50.0;
248 #else
249 #define cache_fill_status 0
250 #endif
252 // dump:
253 static char *stream_dump_name="stream.dump";
254 int stream_dump_type=0;
256 // A-V sync:
257 static float default_max_pts_correction=-1;//0.01f;
258 float audio_delay=0;
259 static int ignore_start=0;
261 static int softsleep=0;
263 double force_fps=0;
264 static int force_srate=0;
265 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
266 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
267 static int play_n_frames=-1;
268 static int play_n_frames_mf=-1;
270 // sub:
271 char *font_name=NULL;
272 char *sub_font_name=NULL;
273 extern int font_fontconfig;
274 float font_factor=0.75;
275 char **sub_name=NULL;
276 float sub_delay=0;
277 float sub_fps=0;
278 int sub_auto = 1;
279 char *vobsub_name=NULL;
280 /*DSP!!char *dsp=NULL;*/
281 int subcc_enabled=0;
282 int suboverlap_enabled = 1;
284 #ifdef CONFIG_ASS
285 #include "libass/ass.h"
286 #include "libass/ass_mp.h"
287 #endif
289 char* current_module=NULL; // for debugging
292 // ---
294 #ifdef CONFIG_MENU
295 #include "m_struct.h"
296 #include "libmenu/menu.h"
297 void vf_menu_pause_update(struct vf_instance* vf);
298 extern vf_info_t vf_info_menu;
299 static const vf_info_t* const libmenu_vfs[] = {
300 &vf_info_menu,
301 NULL
303 static vf_instance_t* vf_menu = NULL;
304 int use_menu = 0;
305 static char* menu_cfg = NULL;
306 static char* menu_root = "main";
307 #endif
310 #ifdef HAVE_RTC
311 static int nortc = 1;
312 static char* rtc_device;
313 #endif
315 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
316 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
317 short edl_decision = 0; ///< 1 when an EDL operation has been made.
318 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
319 int use_filedir_conf;
321 #include "mpcommon.h"
322 #include "command.h"
324 #include "metadata.h"
326 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
328 const void *mpctx_get_video_out(MPContext *mpctx)
330 return mpctx->video_out;
333 const void *mpctx_get_audio_out(MPContext *mpctx)
335 return mpctx->audio_out;
338 void *mpctx_get_demuxer(MPContext *mpctx)
340 return mpctx->demuxer;
343 void *mpctx_get_playtree_iter(MPContext *mpctx)
345 return mpctx->playtree_iter;
348 void *mpctx_get_mixer(MPContext *mpctx)
350 return &mpctx->mixer;
353 int mpctx_get_global_sub_size(MPContext *mpctx)
355 return mpctx->global_sub_size;
358 int mpctx_get_osd_function(MPContext *mpctx)
360 return mpctx->osd_function;
363 static float get_relative_time(struct MPContext *mpctx)
365 unsigned int new_time = GetTimer();
366 unsigned int delta = new_time - mpctx->last_time;
367 mpctx->last_time = new_time;
368 return delta * 0.000001;
371 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
372 switch (type)
374 /* check for valid video stream */
375 case META_VIDEO_CODEC:
376 case META_VIDEO_BITRATE:
377 case META_VIDEO_RESOLUTION:
379 if (!mpctx->sh_video)
380 return 0;
381 break;
384 /* check for valid audio stream */
385 case META_AUDIO_CODEC:
386 case META_AUDIO_BITRATE:
387 case META_AUDIO_SAMPLES:
389 if (!mpctx->sh_audio)
390 return 0;
391 break;
394 /* check for valid demuxer */
395 case META_INFO_TITLE:
396 case META_INFO_ARTIST:
397 case META_INFO_ALBUM:
398 case META_INFO_YEAR:
399 case META_INFO_COMMENT:
400 case META_INFO_TRACK:
401 case META_INFO_GENRE:
403 if (!mpctx->demuxer)
404 return 0;
405 break;
408 default:
409 break;
412 return 1;
415 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
416 char **info = mpctx->demuxer->info;
417 int n;
419 if (!info || !tag)
420 return NULL;
422 for (n = 0; info[2*n] != NULL ; n++)
423 if (!strcmp (info[2*n], tag))
424 break;
426 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
429 char *get_metadata(struct MPContext *mpctx, metadata_t type)
431 char *meta = NULL;
432 sh_audio_t * const sh_audio = mpctx->sh_audio;
433 sh_video_t * const sh_video = mpctx->sh_video;
435 if (!is_valid_metadata_type(mpctx, type))
436 return NULL;
438 switch (type)
440 case META_NAME:
442 return strdup (mp_basename2 (mpctx->filename));
445 case META_VIDEO_CODEC:
447 if (sh_video->format == 0x10000001)
448 meta = strdup ("mpeg1");
449 else if (sh_video->format == 0x10000002)
450 meta = strdup ("mpeg2");
451 else if (sh_video->format == 0x10000004)
452 meta = strdup ("mpeg4");
453 else if (sh_video->format == 0x10000005)
454 meta = strdup ("h264");
455 else if (sh_video->format >= 0x20202020)
457 meta = malloc (8);
458 sprintf (meta, "%.4s", (char *) &sh_video->format);
460 else
462 meta = malloc (8);
463 sprintf (meta, "0x%08X", sh_video->format);
465 return meta;
468 case META_VIDEO_BITRATE:
470 meta = malloc (16);
471 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
472 return meta;
475 case META_VIDEO_RESOLUTION:
477 meta = malloc (16);
478 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
479 return meta;
482 case META_AUDIO_CODEC:
484 if (sh_audio->codec && sh_audio->codec->name)
485 meta = strdup (sh_audio->codec->name);
486 return meta;
489 case META_AUDIO_BITRATE:
491 meta = malloc (16);
492 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
493 return meta;
496 case META_AUDIO_SAMPLES:
498 meta = malloc (16);
499 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
500 return meta;
503 /* check for valid demuxer */
504 case META_INFO_TITLE:
505 return get_demuxer_info(mpctx, "Title");
507 case META_INFO_ARTIST:
508 return get_demuxer_info(mpctx, "Artist");
510 case META_INFO_ALBUM:
511 return get_demuxer_info(mpctx, "Album");
513 case META_INFO_YEAR:
514 return get_demuxer_info(mpctx, "Year");
516 case META_INFO_COMMENT:
517 return get_demuxer_info(mpctx, "Comment");
519 case META_INFO_TRACK:
520 return get_demuxer_info(mpctx, "Track");
522 case META_INFO_GENRE:
523 return get_demuxer_info(mpctx, "Genre");
525 default:
526 break;
529 return meta;
532 /// step size of mixer changes
533 int volstep = 3;
535 #ifdef CONFIG_DVDNAV
536 static void mp_dvdnav_context_free(MPContext *ctx){
537 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
538 ctx->nav_smpi = NULL;
539 if (ctx->nav_buffer) free(ctx->nav_buffer);
540 ctx->nav_buffer = NULL;
541 ctx->nav_start = NULL;
542 ctx->nav_in_size = 0;
544 #endif
546 void uninit_player(struct MPContext *mpctx, unsigned int mask){
547 mask=mpctx->initialized_flags&mask;
549 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
551 if(mask&INITIALIZED_ACODEC){
552 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
553 current_module="uninit_acodec";
554 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
555 #ifdef CONFIG_GUI
556 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
557 #endif
558 mpctx->sh_audio=NULL;
559 mpctx->mixer.afilter = NULL;
562 if(mask&INITIALIZED_VCODEC){
563 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
564 current_module="uninit_vcodec";
565 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
566 mpctx->sh_video=NULL;
567 #ifdef CONFIG_MENU
568 vf_menu=NULL;
569 #endif
572 if(mask&INITIALIZED_DEMUXER){
573 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
574 current_module="free_demuxer";
575 if(mpctx->demuxer){
576 mpctx->stream=mpctx->demuxer->stream;
577 free_demuxer(mpctx->demuxer);
579 mpctx->demuxer=NULL;
582 // kill the cache process:
583 if(mask&INITIALIZED_STREAM){
584 mpctx->initialized_flags&=~INITIALIZED_STREAM;
585 current_module="uninit_stream";
586 if(mpctx->stream) free_stream(mpctx->stream);
587 mpctx->stream=NULL;
590 if(mask&INITIALIZED_VO){
591 mpctx->initialized_flags&=~INITIALIZED_VO;
592 current_module="uninit_vo";
593 vo_destroy(mpctx->video_out);
594 mpctx->video_out=NULL;
595 #ifdef CONFIG_DVDNAV
596 mp_dvdnav_context_free(mpctx);
597 #endif
600 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
601 if(mask&INITIALIZED_GETCH2){
602 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
603 current_module="uninit_getch2";
604 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
605 // restore terminal:
606 getch2_disable();
609 if(mask&INITIALIZED_VOBSUB){
610 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
611 current_module="uninit_vobsub";
612 if(vo_vobsub) vobsub_close(vo_vobsub);
613 vo_vobsub=NULL;
616 if (mask&INITIALIZED_SPUDEC){
617 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
618 current_module="uninit_spudec";
619 spudec_free(vo_spudec);
620 vo_spudec=NULL;
623 if(mask&INITIALIZED_AO){
624 mpctx->initialized_flags&=~INITIALIZED_AO;
625 current_module="uninit_ao";
626 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
627 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
628 mpctx->audio_out=NULL;
631 #ifdef CONFIG_GUI
632 if(mask&INITIALIZED_GUI){
633 mpctx->initialized_flags&=~INITIALIZED_GUI;
634 current_module="uninit_gui";
635 guiDone();
637 #endif
639 current_module=NULL;
642 void exit_player_with_rc(struct MPContext *mpctx, exit_reason_t how, int rc)
644 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
645 uninit_player(mpctx, INITIALIZED_ALL);
646 #if defined(__MINGW32__) || defined(__CYGWIN__)
647 timeEndPeriod(1);
648 #endif
649 #ifdef CONFIG_X11
650 #ifdef CONFIG_GUI
651 if ( !use_gui )
652 #endif
653 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
654 #endif
656 current_module="uninit_input";
657 mp_input_uninit(mpctx->input);
658 #ifdef CONFIG_MENU
659 if (use_menu)
660 menu_uninit();
661 #endif
663 #ifdef CONFIG_FREETYPE
664 current_module="uninit_font";
665 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
666 free_font_desc(mpctx->osd->sub_font);
667 free_font_desc(vo_font);
668 vo_font = NULL;
669 done_freetype();
670 #endif
671 osd_free(mpctx->osd);
673 #ifdef CONFIG_ASS
674 ass_library_done(ass_library);
675 #endif
677 current_module="exit_player";
679 // free mplayer config
680 if(mpctx->mconfig)
681 m_config_free(mpctx->mconfig);
683 if(mpctx->playtree)
684 play_tree_free(mpctx->playtree, 1);
686 talloc_free(mpctx->key_fifo);
688 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
689 switch(how) {
690 case EXIT_QUIT:
691 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_quit);
692 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
693 break;
694 case EXIT_EOF:
695 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_eof);
696 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
697 break;
698 case EXIT_ERROR:
699 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_error);
700 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
701 break;
702 default:
703 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
705 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
707 exit(rc);
710 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
712 exit_player_with_rc(mpctx, how, 1);
715 #ifndef __MINGW32__
716 static void child_sighandler(int x){
717 pid_t pid;
718 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
720 #endif
722 #ifdef CONFIG_CRASH_DEBUG
723 static char *prog_path;
724 static int crash_debug = 0;
725 #endif
727 static void exit_sighandler(int x){
728 static int sig_count=0;
729 #ifdef CONFIG_CRASH_DEBUG
730 if (!crash_debug || x != SIGTRAP)
731 #endif
732 ++sig_count;
733 if(sig_count==5)
735 /* We're crashing bad and can't uninit cleanly :(
736 * by popular request, we make one last (dirty)
737 * effort to restore the user's
738 * terminal. */
739 getch2_disable();
740 exit(1);
742 if(sig_count==6) exit(1);
743 if(sig_count>6){
744 // can't stop :(
745 #ifndef __MINGW32__
746 kill(getpid(),SIGKILL);
747 #endif
749 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
750 current_module?current_module:"unknown"
752 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
753 if(sig_count<=1)
754 switch(x){
755 case SIGINT:
756 case SIGQUIT:
757 case SIGTERM:
758 case SIGKILL:
759 async_quit_request = 1;
760 return; // killed from keyboard (^C) or killed [-9]
761 case SIGILL:
762 #ifdef RUNTIME_CPUDETECT
763 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
764 #else
765 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
766 #endif
767 case SIGFPE:
768 case SIGSEGV:
769 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
770 default:
771 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
772 #ifdef CONFIG_CRASH_DEBUG
773 if (crash_debug) {
774 int gdb_pid;
775 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
776 gdb_pid = fork();
777 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
778 if (gdb_pid == 0) { // We are the child
779 char spid[20];
780 snprintf(spid, sizeof(spid), "%i", getppid());
781 getch2_disable(); // allow terminal to work properly with gdb
782 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
783 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
784 } else if (gdb_pid < 0)
785 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
786 else {
787 waitpid(gdb_pid, NULL, 0);
789 if (x == SIGTRAP) return;
791 #endif
793 getch2_disable();
794 exit(1);
797 #include "cfg-mplayer.h"
799 static int cfg_include(m_option_t *conf, char *filename)
801 return m_config_parse_config_file(conf->priv, filename);
804 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
806 char *conffile;
807 int conffile_fd;
808 if (!disable_system_conf &&
809 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
810 exit_player(mpctx, EXIT_NONE);
811 if ((conffile = get_path("")) == NULL) {
812 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
813 } else {
814 #ifdef __MINGW32__
815 mkdir(conffile);
816 #else
817 mkdir(conffile, 0777);
818 #endif
819 free(conffile);
820 if ((conffile = get_path("config")) == NULL) {
821 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
822 } else {
823 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
824 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
825 write(conffile_fd, default_config, strlen(default_config));
826 close(conffile_fd);
828 if (!disable_user_conf &&
829 m_config_parse_config_file(conf, conffile) < 0)
830 exit_player(mpctx, EXIT_NONE);
831 free(conffile);
836 #define PROFILE_CFG_PROTOCOL "protocol."
838 static void load_per_protocol_config (m_config_t* conf, const char *const file)
840 char *str;
841 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
842 m_profile_t *p;
844 /* does filename actually uses a protocol ? */
845 str = strstr (file, "://");
846 if (!str)
847 return;
849 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
850 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
851 p = m_config_get_profile (conf, protocol);
852 if (p)
854 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingProtocolProfile, protocol);
855 m_config_set_profile(conf,p);
859 #define PROFILE_CFG_EXTENSION "extension."
861 static void load_per_extension_config (m_config_t* conf, const char *const file)
863 char *str;
864 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
865 m_profile_t *p;
867 /* does filename actually have an extension ? */
868 str = strrchr (file, '.');
869 if (!str)
870 return;
872 sprintf (extension, PROFILE_CFG_EXTENSION);
873 strncat (extension, ++str, 7);
874 p = m_config_get_profile (conf, extension);
875 if (p)
877 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, extension);
878 m_config_set_profile(conf,p);
882 #define PROFILE_CFG_VO "vo."
883 #define PROFILE_CFG_AO "ao."
885 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
887 char profile[strlen (cfg) + strlen (out) + 1];
888 m_profile_t *p;
890 sprintf (profile, "%s%s", cfg, out);
891 p = m_config_get_profile (conf, profile);
892 if (p)
894 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, profile);
895 m_config_set_profile(conf,p);
899 static void load_per_file_config (m_config_t* conf, const char *const file)
901 char *confpath;
902 char cfg[strlen(file)+10];
903 struct stat st;
904 char *name;
906 sprintf (cfg, "%s.conf", file);
908 if (use_filedir_conf && !stat (cfg, &st))
910 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
911 m_config_parse_config_file (conf, cfg);
912 return;
915 if ((name = strrchr (cfg, '/')) == NULL)
916 name = cfg;
917 else
918 name++;
920 if ((confpath = get_path (name)) != NULL)
922 if (!stat (confpath, &st))
924 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
925 m_config_parse_config_file (conf, confpath);
928 free (confpath);
932 /* When libmpdemux performs a blocking operation (network connection or
933 * cache filling) if the operation fails we use this function to check
934 * if it was interrupted by the user.
935 * The function returns a new value for eof. */
936 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
938 mp_cmd_t* cmd;
939 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
940 switch(cmd->id) {
941 case MP_CMD_QUIT:
942 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
943 case MP_CMD_PLAY_TREE_STEP: {
944 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
945 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
946 } break;
947 case MP_CMD_PLAY_TREE_UP_STEP: {
948 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
949 } break;
950 case MP_CMD_PLAY_ALT_SRC_STEP: {
951 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
952 } break;
954 mp_cmd_free(cmd);
956 return stop_play;
959 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
961 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
963 play_tree_add_bpf(entry,mpctx->filename);
965 #ifdef CONFIG_GUI
966 if (use_gui) {
967 if (entry) {
968 import_playtree_playlist_into_gui(entry, mpctx->mconfig);
969 play_tree_free_list(entry,1);
971 } else
972 #endif
974 if(!entry) {
975 entry = mpctx->playtree_iter->tree;
976 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
977 return PT_NEXT_ENTRY;
979 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
980 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
981 return PT_NEXT_ENTRY;
984 play_tree_remove(entry,1,1);
985 return PT_NEXT_SRC;
987 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
988 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
989 entry = mpctx->playtree_iter->tree;
990 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
991 return PT_NEXT_ENTRY;
993 play_tree_remove(entry,1,1);
995 return PT_NEXT_SRC;
998 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1000 sub_data *subd;
1001 #ifdef CONFIG_ASS
1002 ass_track_t *asst = 0;
1003 #endif
1005 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1006 return;
1009 subd = sub_read_file(filename, fps);
1010 #ifdef CONFIG_ASS
1011 if (ass_enabled)
1012 #ifdef CONFIG_ICONV
1013 asst = ass_read_file(ass_library, filename, sub_cp);
1014 #else
1015 asst = ass_read_file(ass_library, filename, 0);
1016 #endif
1017 if (ass_enabled && subd && !asst)
1018 asst = ass_read_subdata(ass_library, subd, fps);
1020 if (!asst && !subd)
1021 #else
1022 if(!subd)
1023 #endif
1024 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
1025 filename_recode(filename));
1027 #ifdef CONFIG_ASS
1028 if (!asst && !subd) return;
1029 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1030 #else
1031 if (!subd) return;
1032 #endif
1033 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1034 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1035 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1036 filename_recode(filename));
1037 ++mpctx->set_of_sub_size;
1038 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
1039 filename_recode(filename));
1042 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1043 void update_set_of_subtitles(struct MPContext *mpctx)
1044 // subdata was changed, set_of_sub... have to be updated.
1046 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
1047 int i;
1048 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1049 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
1050 set_of_subtitles[i-1] = set_of_subtitles[i];
1051 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
1052 --mpctx->set_of_sub_size;
1053 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
1055 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1056 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
1058 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1059 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1060 ++mpctx->set_of_sub_size;
1064 void init_vo_spudec(struct MPContext *mpctx)
1066 if (vo_spudec)
1067 spudec_free(vo_spudec);
1068 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1069 vo_spudec = NULL;
1070 if (spudec_ifo) {
1071 unsigned int palette[16], width, height;
1072 current_module="spudec_init_vobsub";
1073 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1074 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1077 #ifdef CONFIG_DVDREAD
1078 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1079 current_module="spudec_init_dvdread";
1080 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1081 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1082 NULL, 0);
1084 #endif
1086 #ifdef CONFIG_DVDNAV
1087 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1088 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1089 current_module="spudec_init_dvdnav";
1090 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1092 #endif
1094 if (vo_spudec==NULL) {
1095 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1096 current_module="spudec_init_normal";
1097 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1098 spudec_set_font_factor(vo_spudec,font_factor);
1101 if (vo_spudec!=NULL)
1102 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1106 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1107 * make it all work is to use the builtin SDL-bootstrap code, which
1108 * will be done automatically by replacing our main() if we include SDL.h.
1110 #if defined(__APPLE__) && defined(CONFIG_SDL)
1111 #include <SDL.h>
1112 #endif
1115 * \brief append a formatted string
1116 * \param buf buffer to print into
1117 * \param pos position of terminating 0 in buf
1118 * \param len maximum number of characters in buf, not including terminating 0
1119 * \param format printf format string
1121 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1123 va_list va;
1124 va_start(va, format);
1125 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1126 va_end(va);
1127 if (*pos >= len ) {
1128 buf[len] = 0;
1129 *pos = len;
1134 * \brief append time in the hh:mm:ss.f format
1135 * \param buf buffer to print into
1136 * \param pos position of terminating 0 in buf
1137 * \param len maximum number of characters in buf, not including terminating 0
1138 * \param time time value to convert/append
1140 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1141 long tenths = 10 * time;
1142 int f1 = tenths % 10;
1143 int ss = (tenths / 10) % 60;
1144 int mm = (tenths / 600) % 60;
1145 int hh = tenths / 36000;
1146 if (time <= 0) {
1147 saddf(buf, pos, len, "unknown");
1148 return;
1150 if (hh > 0)
1151 saddf(buf, pos, len, "%2d:", hh);
1152 if (hh > 0 || mm > 0)
1153 saddf(buf, pos, len, "%02d:", mm);
1154 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1157 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1159 struct MPOpts *opts = &mpctx->opts;
1160 sh_video_t * const sh_video = mpctx->sh_video;
1162 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1163 a_pos = playing_audio_pts(mpctx);
1164 if (mpctx->sh_audio && sh_video && at_frame) {
1165 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1166 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1167 && !mpctx->drop_message_shown) {
1168 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1169 mpctx->drop_message_shown = true;
1172 if (quiet)
1173 return;
1176 int width;
1177 char *line;
1178 unsigned pos = 0;
1179 get_screen_size();
1180 if (screen_width > 0)
1181 width = screen_width;
1182 else
1183 width = 80;
1184 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1185 /* Windows command line is broken (MinGW's rxvt works, but we
1186 * should not depend on that). */
1187 width--;
1188 #endif
1189 line = malloc(width + 1); // one additional char for the terminating null
1191 // Audio time
1192 if (mpctx->sh_audio) {
1193 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1194 if (!sh_video) {
1195 float len = demuxer_get_time_length(mpctx->demuxer);
1196 saddf(line, &pos, width, "(");
1197 sadd_hhmmssf(line, &pos, width, a_pos);
1198 saddf(line, &pos, width, ") of %.1f (", len);
1199 sadd_hhmmssf(line, &pos, width, len);
1200 saddf(line, &pos, width, ") ");
1204 // Video time
1205 if (sh_video)
1206 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1208 // A-V sync
1209 if (mpctx->sh_audio && sh_video)
1210 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1211 mpctx->last_av_difference, mpctx->total_avsync_change);
1213 // Video stats
1214 if (sh_video)
1215 saddf(line, &pos, width, "%3d/%3d ",
1216 (int)sh_video->num_frames,
1217 (int)sh_video->num_frames_decoded);
1219 // CPU usage
1220 if (sh_video) {
1221 if (sh_video->timer > 0.5)
1222 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1223 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1224 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1225 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1226 else
1227 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1228 } else if (mpctx->sh_audio) {
1229 if (mpctx->delay > 0.5)
1230 saddf(line, &pos, width, "%4.1f%% ",
1231 100.0*audio_time_usage/(double)mpctx->delay);
1232 else
1233 saddf(line, &pos, width, "??,?%% ");
1236 // VO stats
1237 if (sh_video)
1238 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1240 #ifdef CONFIG_STREAM_CACHE
1241 // cache stats
1242 if (stream_cache_size > 0)
1243 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1244 #endif
1246 // other
1247 if (opts->playback_speed != 1)
1248 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1250 // end
1251 if (erase_to_end_of_line) {
1252 line[pos] = 0;
1253 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1254 } else {
1255 memset(&line[pos], ' ', width - pos);
1256 line[width] = 0;
1257 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1259 free(line);
1263 * \brief build a chain of audio filters that converts the input format
1264 * to the ao's format, taking into account the current playback_speed.
1265 * \param sh_audio describes the requested input format of the chain.
1266 * \param ao_data describes the requested output format of the chain.
1268 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1270 struct MPOpts *opts = &mpctx->opts;
1271 int new_srate;
1272 int result;
1273 if (!sh_audio)
1275 #ifdef CONFIG_GUI
1276 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1277 #endif
1278 mpctx->mixer.afilter = NULL;
1279 return 0;
1281 if(af_control_any_rev(sh_audio->afilter,
1282 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1283 &opts->playback_speed)) {
1284 new_srate = sh_audio->samplerate;
1285 } else {
1286 new_srate = sh_audio->samplerate * opts->playback_speed;
1287 if (new_srate != ao_data->samplerate) {
1288 // limits are taken from libaf/af_resample.c
1289 if (new_srate < 8000)
1290 new_srate = 8000;
1291 if (new_srate > 192000)
1292 new_srate = 192000;
1293 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1296 result = init_audio_filters(sh_audio, new_srate,
1297 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1298 mpctx->mixer.afilter = sh_audio->afilter;
1299 #ifdef CONFIG_GUI
1300 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1301 #endif
1302 return result;
1306 typedef struct mp_osd_msg mp_osd_msg_t;
1307 struct mp_osd_msg {
1308 /// Previous message on the stack.
1309 mp_osd_msg_t* prev;
1310 /// Message text.
1311 char msg[128];
1312 int id,level,started;
1313 /// Display duration in ms.
1314 unsigned time;
1317 /// OSD message stack.
1318 static mp_osd_msg_t* osd_msg_stack = NULL;
1321 * \brief Add a message on the OSD message stack
1323 * If a message with the same id is already present in the stack
1324 * it is pulled on top of the stack, otherwise a new message is created.
1328 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1329 mp_osd_msg_t *msg,*last=NULL;
1330 va_list va;
1331 int r;
1333 // look if the id is already in the stack
1334 for(msg = osd_msg_stack ; msg && msg->id != id ;
1335 last = msg, msg = msg->prev);
1336 // not found: alloc it
1337 if(!msg) {
1338 msg = calloc(1,sizeof(mp_osd_msg_t));
1339 msg->prev = osd_msg_stack;
1340 osd_msg_stack = msg;
1341 } else if(last) { // found, but it's not on top of the stack
1342 last->prev = msg->prev;
1343 msg->prev = osd_msg_stack;
1344 osd_msg_stack = msg;
1346 // write the msg
1347 va_start(va,fmt);
1348 r = vsnprintf(msg->msg, 128, fmt, va);
1349 va_end(va);
1350 if(r >= 128) msg->msg[127] = 0;
1351 // set id and time
1352 msg->id = id;
1353 msg->level = level;
1354 msg->time = time;
1359 * \brief Remove a message from the OSD stack
1361 * This function can be used to get rid of a message right away.
1365 void rm_osd_msg(int id) {
1366 mp_osd_msg_t *msg,*last=NULL;
1368 // Search for the msg
1369 for(msg = osd_msg_stack ; msg && msg->id != id ;
1370 last = msg, msg = msg->prev);
1371 if(!msg) return;
1373 // Detach it from the stack and free it
1374 if(last)
1375 last->prev = msg->prev;
1376 else
1377 osd_msg_stack = msg->prev;
1378 free(msg);
1382 * \brief Remove all messages from the OSD stack
1386 static void clear_osd_msgs(void) {
1387 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1388 while(msg) {
1389 prev = msg->prev;
1390 free(msg);
1391 msg = prev;
1393 osd_msg_stack = NULL;
1397 * \brief Get the current message from the OSD stack.
1399 * This function decrements the message timer and destroys the old ones.
1400 * The message that should be displayed is returned (if any).
1404 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1406 mp_osd_msg_t *msg,*prev,*last = NULL;
1407 static unsigned last_update = 0;
1408 unsigned now = GetTimerMS();
1409 unsigned diff;
1410 char hidden_dec_done = 0;
1412 if (osd_visible) {
1413 // 36000000 means max timed visibility is 1 hour into the future, if
1414 // the difference is greater assume it's wrapped around from below 0
1415 if (osd_visible - now > 36000000) {
1416 osd_visible = 0;
1417 vo_osd_progbar_type = -1; // disable
1418 vo_osd_changed(OSDTYPE_PROGBAR);
1419 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1423 if(!last_update) last_update = now;
1424 diff = now >= last_update ? now - last_update : 0;
1426 last_update = now;
1428 // Look for the first message in the stack with high enough level.
1429 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1430 prev = msg->prev;
1431 if(msg->level > osd_level && hidden_dec_done) continue;
1432 // The message has a high enough level or it is the first hidden one
1433 // in both cases we decrement the timer or kill it.
1434 if(!msg->started || msg->time > diff) {
1435 if(msg->started) msg->time -= diff;
1436 else msg->started = 1;
1437 // display it
1438 if(msg->level <= osd_level) return msg;
1439 hidden_dec_done = 1;
1440 continue;
1442 // kill the message
1443 free(msg);
1444 if(last) {
1445 last->prev = prev;
1446 msg = last;
1447 } else {
1448 osd_msg_stack = prev;
1449 msg = NULL;
1452 // Nothing found
1453 return NULL;
1457 * \brief Display the OSD bar.
1459 * Display the OSD bar or fall back on a simple message.
1463 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1465 if(osd_level < 1) return;
1467 if(mpctx->sh_video) {
1468 osd_visible = (GetTimerMS() + 1000) | 1;
1469 vo_osd_progbar_type = type;
1470 vo_osd_progbar_value = 256*(val-min)/(max-min);
1471 vo_osd_changed(OSDTYPE_PROGBAR);
1472 return;
1475 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1476 name,ROUND(100*(val-min)/(max-min)));
1481 * \brief Update the OSD message line.
1483 * This function displays the current message on the vo OSD or on the term.
1484 * If the stack is empty and the OSD level is high enough the timer
1485 * is displayed (only on the vo OSD).
1489 static void update_osd_msg(struct MPContext *mpctx)
1491 mp_osd_msg_t *msg;
1492 struct osd_state *osd = mpctx->osd;
1493 char osd_text_timer[128];
1495 // Look if we have a msg
1496 if((msg = get_osd_msg(mpctx))) {
1497 if (strcmp(osd->osd_text, msg->msg)) {
1498 strncpy(osd->osd_text, msg->msg, 127);
1499 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1500 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1502 return;
1505 if(mpctx->sh_video) {
1506 // fallback on the timer
1507 if(osd_level>=2) {
1508 int len = demuxer_get_time_length(mpctx->demuxer);
1509 int percentage = -1;
1510 char percentage_text[10];
1511 int pts = demuxer_get_current_time(mpctx->demuxer);
1513 if (mpctx->osd_show_percentage)
1514 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1516 if (percentage >= 0)
1517 snprintf(percentage_text, 9, " (%d%%)", percentage);
1518 else
1519 percentage_text[0] = 0;
1521 if (osd_level == 3)
1522 snprintf(osd_text_timer, 63,
1523 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1524 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1525 len/3600,(len/60)%60,len%60,percentage_text);
1526 else
1527 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1528 mpctx->osd_function,pts/3600,(pts/60)%60,
1529 pts%60,percentage_text);
1530 } else
1531 osd_text_timer[0]=0;
1533 // always decrement the percentage timer
1534 if(mpctx->osd_show_percentage)
1535 mpctx->osd_show_percentage--;
1537 if (strcmp(osd->osd_text, osd_text_timer)) {
1538 strncpy(osd->osd_text, osd_text_timer, 63);
1539 vo_osd_changed(OSDTYPE_OSD);
1541 return;
1544 // Clear the term osd line
1545 if (term_osd && osd->osd_text[0]) {
1546 osd->osd_text[0] = 0;
1547 printf("%s\n",term_osd_esc);
1551 ///@}
1552 // OSDMsgStack
1555 void reinit_audio_chain(struct MPContext *mpctx)
1557 struct MPOpts *opts = &mpctx->opts;
1558 if(mpctx->sh_audio){
1559 current_module="init_audio_codec";
1560 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1561 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1562 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1563 mpctx->d_audio->id = -2;
1564 return;
1565 } else
1566 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1567 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1570 //const ao_info_t *info=audio_out->info;
1571 current_module="af_preinit";
1572 ao_data.samplerate=force_srate;
1573 ao_data.channels=0;
1574 ao_data.format=audio_output_format;
1575 #if 1
1576 // first init to detect best values
1577 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1578 // input:
1579 mpctx->sh_audio->samplerate,
1580 // output:
1581 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1582 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1583 exit_player(mpctx, EXIT_ERROR);
1585 #endif
1586 current_module="ao2_init";
1587 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1588 0, // plugin flag
1589 ao_data.samplerate,
1590 ao_data.channels,
1591 ao_data.format,0))){
1592 // FAILED:
1593 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1594 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1595 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1596 mpctx->d_audio->id = -2;
1597 return;
1598 } else {
1599 // SUCCESS:
1600 mpctx->initialized_flags|=INITIALIZED_AO;
1601 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1602 mpctx->audio_out->info->short_name,
1603 ao_data.samplerate, ao_data.channels,
1604 af_fmt2str_short(ao_data.format),
1605 af_fmt2bits(ao_data.format)/8 );
1606 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1607 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1608 if(strlen(mpctx->audio_out->info->comment) > 0)
1609 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1610 // init audio filters:
1611 #if 1
1612 current_module="af_init";
1613 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1614 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1615 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1616 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1617 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1619 #endif
1621 mpctx->mixer.audio_out = mpctx->audio_out;
1622 mpctx->mixer.volstep = volstep;
1627 ///@}
1628 // Command2Property
1631 // Return pts value corresponding to the end point of audio written to the
1632 // ao so far.
1633 static double written_audio_pts(struct MPContext *mpctx)
1635 sh_audio_t *sh_audio = mpctx->sh_audio;
1636 demux_stream_t *d_audio = mpctx->d_audio;
1637 double buffered_output;
1638 // first calculate the end pts of audio that has been output by decoder
1639 double a_pts = sh_audio->pts;
1640 if (a_pts != MP_NOPTS_VALUE)
1641 // Good, decoder supports new way of calculating audio pts.
1642 // sh_audio->pts is the timestamp of the latest input packet with
1643 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1644 // the amount of bytes the decoder has written after that timestamp.
1645 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1646 else {
1647 // Decoder doesn't support new way of calculating pts (or we're
1648 // being called before it has decoded anything with known timestamp).
1649 // Use the old method of audio pts calculation: take the timestamp
1650 // of last packet with known pts the decoder has read data from,
1651 // and add amount of bytes read after the beginning of that packet
1652 // divided by input bps. This will be inaccurate if the input/output
1653 // ratio is not constant for every audio packet or if it is constant
1654 // but not accurately known in sh_audio->i_bps.
1656 a_pts = d_audio->pts;
1657 // ds_tell_pts returns bytes read after last timestamp from
1658 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1659 // it has read but not decoded
1660 if (sh_audio->i_bps)
1661 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1662 (double)sh_audio->i_bps;
1664 // Now a_pts hopefully holds the pts for end of audio from decoder.
1665 // Substract data in buffers between decoder and audio out.
1667 // Decoded but not filtered
1668 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1670 // Data buffered in audio filters, measured in bytes of "missing" output
1671 buffered_output = af_calc_delay(sh_audio->afilter);
1673 // Data that was ready for ao but was buffered because ao didn't fully
1674 // accept everything to internal buffers yet
1675 buffered_output += sh_audio->a_out_buffer_len;
1677 // Filters divide audio length by playback_speed, so multiply by it
1678 // to get the length in original units without speedup or slowdown
1679 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1681 return a_pts;
1684 // Return pts value corresponding to currently playing audio.
1685 double playing_audio_pts(struct MPContext *mpctx)
1687 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1688 mpctx->audio_out->get_delay();
1691 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1692 struct MPOpts *opts = &mpctx->opts;
1693 // check for frame-drop:
1694 current_module = "check_framedrop";
1695 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1696 static int dropped_frames;
1697 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1698 float d = delay-mpctx->delay;
1699 ++total_frame_cnt;
1700 // we should avoid dropping too many frames in sequence unless we
1701 // are too late. and we allow 100ms A-V delay here:
1702 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1703 && !mpctx->update_video_immediately) {
1704 ++drop_frame_cnt;
1705 ++dropped_frames;
1706 return frame_dropping;
1707 } else
1708 dropped_frames = 0;
1710 return 0;
1714 #ifdef HAVE_RTC
1715 int rtc_fd = -1;
1716 #endif
1718 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1720 #ifdef HAVE_RTC
1721 if (rtc_fd >= 0){
1722 // -------- RTC -----------
1723 current_module="sleep_rtc";
1724 while (time_frame > 0.000) {
1725 unsigned long rtc_ts;
1726 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1727 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1728 time_frame -= get_relative_time(mpctx);
1730 } else
1731 #endif
1733 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1734 // unnecessarily high CPU usage
1735 float margin = softsleep ? 0.011 : 0;
1736 current_module = "sleep_timer";
1737 while (time_frame > margin) {
1738 usec_sleep(1000000 * (time_frame - margin));
1739 time_frame -= get_relative_time(mpctx);
1741 if (softsleep){
1742 current_module = "sleep_soft";
1743 if (time_frame < 0)
1744 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1745 while (time_frame > 0)
1746 time_frame -= get_relative_time(mpctx); // burn the CPU
1749 return time_frame;
1752 #ifdef CONFIG_DVDNAV
1753 #ifndef FF_B_TYPE
1754 #define FF_B_TYPE 3
1755 #endif
1756 /// store decoded video image
1757 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1758 mp_image_t *from_mpi) {
1759 mp_image_t *mpi;
1761 /// Do not store B-frames
1762 if (from_mpi->pict_type == FF_B_TYPE)
1763 return to_mpi;
1765 if (to_mpi &&
1766 to_mpi->w == from_mpi->w &&
1767 to_mpi->h == from_mpi->h &&
1768 to_mpi->imgfmt == from_mpi->imgfmt)
1769 mpi = to_mpi;
1770 else {
1771 if (to_mpi)
1772 free_mp_image(to_mpi);
1773 if (from_mpi->w == 0 || from_mpi->h == 0)
1774 return NULL;
1775 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1778 copy_mpi(mpi,from_mpi);
1779 return mpi;
1782 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1783 struct MPOpts *opts = &ctx->opts;
1784 if (ctx->sh_video) {
1785 /// clear video pts
1786 ctx->d_video->pts = 0.0f;
1787 ctx->sh_video->pts = 0.0f;
1788 ctx->sh_video->i_pts = 0.0f;
1789 ctx->sh_video->last_pts = 0.0f;
1790 ctx->sh_video->num_buffered_pts = 0;
1791 ctx->sh_video->num_frames = 0;
1792 ctx->sh_video->num_frames_decoded = 0;
1793 ctx->sh_video->timer = 0.0f;
1794 ctx->sh_video->stream_delay = 0.0f;
1795 ctx->sh_video->timer = 0;
1796 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1799 if (ctx->sh_audio) {
1800 /// free audio packets and reset
1801 ds_free_packs(ctx->d_audio);
1802 audio_delay -= ctx->sh_audio->stream_delay;
1803 ctx->delay =- audio_delay;
1804 ctx->audio_out->reset();
1805 resync_audio_stream(ctx->sh_audio);
1808 if (ctx->d_sub) opts->sub_id = -2;
1810 audio_delay = 0.0f;
1812 /// clear all EOF related flags
1813 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1816 /// Restore last decoded DVDNAV (still frame)
1817 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1818 int *in_size,
1819 unsigned char **start,
1820 mp_image_t *decoded_frame)
1822 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1823 return decoded_frame;
1825 /// a change occured in dvdnav stream
1826 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1827 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1828 mp_dvdnav_context_free(mpctx);
1829 mp_dvdnav_reset_stream(mpctx);
1830 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1831 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1834 if (*in_size < 0) {
1835 float len;
1837 /// Display still frame, if any
1838 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1839 decoded_frame = mpctx->nav_smpi;
1841 /// increment video frame : continue playing after still frame
1842 len = demuxer_get_time_length(mpctx->demuxer);
1843 if (mpctx->sh_video->pts >= len &&
1844 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1845 mp_dvdnav_skip_still(mpctx->stream);
1846 mp_dvdnav_skip_wait(mpctx->stream);
1848 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1850 if (mpctx->nav_buffer) {
1851 *start = mpctx->nav_start;
1852 *in_size = mpctx->nav_in_size;
1853 if (mpctx->nav_start)
1854 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1858 return decoded_frame;
1861 /// Save last decoded DVDNAV (still frame)
1862 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1863 unsigned char *start,
1864 mp_image_t *decoded_frame)
1866 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1867 return;
1869 if (mpctx->nav_buffer)
1870 free(mpctx->nav_buffer);
1872 mpctx->nav_buffer = malloc(in_size);
1873 mpctx->nav_start = start;
1874 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1875 if (mpctx->nav_buffer)
1876 memcpy(mpctx->nav_buffer,start,in_size);
1878 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1879 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1881 #endif /* CONFIG_DVDNAV */
1883 /* Modify video timing to match the audio timeline. There are two main
1884 * reasons this is needed. First, video and audio can start from different
1885 * positions at beginning of file or after a seek (MPlayer starts both
1886 * immediately even if they have different pts). Second, the file can have
1887 * audio timestamps that are inconsistent with the duration of the audio
1888 * packets, for example two consecutive timestamp values differing by
1889 * one second but only a packet with enough samples for half a second
1890 * of playback between them.
1892 static void adjust_sync(struct MPContext *mpctx, double frame_time)
1894 struct MPOpts *opts = &mpctx->opts;
1895 current_module = "av_sync";
1897 if (!mpctx->sh_audio)
1898 return;
1900 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
1901 double v_pts = mpctx->sh_video->pts;
1902 double av_delay = a_pts - v_pts;
1903 // Try to sync vo_flip() so it will *finish* at given time
1904 av_delay += mpctx->last_vo_flip_duration;
1905 av_delay -= audio_delay; // This much pts difference is desired
1907 double change = av_delay * 0.1;
1908 double max_change = default_max_pts_correction >= 0 ?
1909 default_max_pts_correction : frame_time * 0.1;
1910 if (change < -max_change)
1911 change = -max_change;
1912 else if (change > max_change)
1913 change = max_change;
1914 mpctx->delay += change;
1915 mpctx->total_avsync_change += change;
1918 static int fill_audio_out_buffers(struct MPContext *mpctx)
1920 struct MPOpts *opts = &mpctx->opts;
1921 unsigned int t;
1922 double tt;
1923 int playsize;
1924 int playflags=0;
1925 int audio_eof=0;
1926 int bytes_to_write;
1927 sh_audio_t * const sh_audio = mpctx->sh_audio;
1929 current_module="play_audio";
1931 while (1) {
1932 int sleep_time;
1933 // all the current uses of ao_data.pts seem to be in aos that handle
1934 // sync completely wrong; there should be no need to use ao_data.pts
1935 // in get_space()
1936 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1937 bytes_to_write = mpctx->audio_out->get_space();
1938 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
1939 break;
1941 // handle audio-only case:
1942 // this is where mplayer sleeps during audio-only playback
1943 // to avoid 100% CPU use
1944 sleep_time = (ao_data.outburst - bytes_to_write) * 1000 / ao_data.bps;
1945 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
1946 usec_sleep(sleep_time * 1000);
1949 while (bytes_to_write) {
1950 playsize = bytes_to_write;
1951 if (playsize > MAX_OUTBURST)
1952 playsize = MAX_OUTBURST;
1953 bytes_to_write -= playsize;
1955 // Fill buffer if needed:
1956 current_module="decode_audio";
1957 t = GetTimer();
1958 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
1959 if (mpctx->d_audio->eof) {
1960 audio_eof = 1;
1961 if (sh_audio->a_out_buffer_len == 0)
1962 return 0;
1964 t = GetTimer() - t;
1965 tt = t*0.000001f; audio_time_usage+=tt;
1966 if (playsize > sh_audio->a_out_buffer_len) {
1967 playsize = sh_audio->a_out_buffer_len;
1968 if (audio_eof)
1969 playflags |= AOPLAY_FINAL_CHUNK;
1971 if (!playsize)
1972 break;
1974 // play audio:
1975 current_module="play_audio";
1977 // Is this pts value actually useful for the aos that access it?
1978 // They're obviously badly broken in the way they handle av sync;
1979 // would not having access to this make them more broken?
1980 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1981 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
1983 if (playsize > 0) {
1984 sh_audio->a_out_buffer_len -= playsize;
1985 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
1986 sh_audio->a_out_buffer_len);
1987 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
1989 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
1990 // Sanity check to avoid hanging in case current ao doesn't output
1991 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
1992 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
1993 sh_audio->a_out_buffer_len = 0;
1996 return 1;
1999 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2000 float *aq_sleep_time)
2002 struct MPOpts *opts = &mpctx->opts;
2003 int frame_time_remaining = 0;
2004 current_module="calc_sleep_time";
2006 *time_frame -= get_relative_time(mpctx); // reset timer
2008 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2009 float delay = mpctx->audio_out->get_delay();
2010 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2012 if (autosync) {
2014 * Adjust this raw delay value by calculating the expected
2015 * delay for this frame and generating a new value which is
2016 * weighted between the two. The higher autosync is, the
2017 * closer to the delay value gets to that which "-nosound"
2018 * would have used, and the longer it will take for A/V
2019 * sync to settle at the right value (but it eventually will.)
2020 * This settling time is very short for values below 100.
2022 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2023 float difference = delay - predicted;
2024 delay = predicted + difference / (float)autosync;
2027 *time_frame = delay - mpctx->delay / opts->playback_speed;
2029 // delay = amount of audio buffered in soundcard/driver
2030 if (delay > 0.25) delay=0.25; else
2031 if (delay < 0.10) delay=0.10;
2032 if (*time_frame > delay*0.6) {
2033 // sleep time too big - may cause audio drops (buffer underrun)
2034 frame_time_remaining = 1;
2035 *time_frame = delay*0.5;
2037 } else {
2038 // If we're lagging more than 200 ms behind the right playback rate,
2039 // don't try to "catch up".
2040 // If benchmark is set always output frames as fast as possible
2041 // without sleeping.
2042 if (*time_frame < -0.2 || benchmark)
2043 *time_frame = 0;
2046 *aq_sleep_time += *time_frame;
2049 //============================== SLEEP: ===================================
2051 // flag 256 means: libvo driver does its timing (dvb card)
2052 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2053 *time_frame = timing_sleep(mpctx, *time_frame);
2054 return frame_time_remaining;
2057 int reinit_video_chain(struct MPContext *mpctx)
2059 struct MPOpts *opts = &mpctx->opts;
2060 sh_video_t * const sh_video = mpctx->sh_video;
2061 double ar=-1.0;
2062 //================== Init VIDEO (codec & libvo) ==========================
2063 if(opts->fixed_vo || !(mpctx->initialized_flags&INITIALIZED_VO)){
2064 current_module="preinit_libvo";
2066 //shouldn't we set dvideo->id=-2 when we fail?
2067 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2068 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2069 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
2070 goto err_out;
2072 mpctx->initialized_flags|=INITIALIZED_VO;
2075 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2076 mpctx->sh_video->stream_aspect = ar;
2077 current_module="init_video_filters";
2079 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2080 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2082 #ifdef CONFIG_MENU
2083 if(use_menu) {
2084 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2085 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2086 if(!vf_menu) {
2087 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2088 use_menu = 0;
2091 if(vf_menu)
2092 sh_video->vfilter = vf_menu;
2093 #endif
2095 #ifdef CONFIG_ASS
2096 if(ass_enabled) {
2097 int i;
2098 int insert = 1;
2099 if (opts->vf_settings)
2100 for (i = 0; opts->vf_settings[i].name; ++i)
2101 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2102 insert = 0;
2103 break;
2105 if (insert) {
2106 extern vf_info_t vf_info_ass;
2107 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2108 char* vf_arg[] = {"auto", "1", NULL};
2109 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2110 if (vf_ass)
2111 sh_video->vfilter = vf_ass;
2112 else
2113 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2116 #endif
2118 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2120 #ifdef CONFIG_ASS
2121 if (ass_enabled)
2122 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2123 #endif
2125 current_module="init_video_codec";
2127 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2128 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2129 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2131 if(!sh_video->initialized){
2132 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2133 goto err_out;
2136 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2138 if (sh_video->codec)
2139 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2141 sh_video->last_pts = MP_NOPTS_VALUE;
2142 sh_video->num_buffered_pts = 0;
2143 sh_video->next_frame_time = 0;
2145 if(auto_quality>0){
2146 // Auto quality option enabled
2147 output_quality=get_video_quality_max(sh_video);
2148 if(auto_quality>output_quality) auto_quality=output_quality;
2149 else output_quality=auto_quality;
2150 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2151 set_video_quality(sh_video,output_quality);
2154 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2156 current_module="init_vo";
2158 return 1;
2160 err_out:
2161 mpctx->sh_video = mpctx->d_video->sh = NULL;
2162 return 0;
2165 static double update_video_nocorrect_pts(struct MPContext *mpctx,
2166 int *blit_frame)
2168 struct sh_video *sh_video = mpctx->sh_video;
2169 *blit_frame = 0;
2170 double frame_time = 0;
2171 while (1) {
2172 current_module = "filter_video";
2173 // In nocorrect-pts mode there is no way to properly time these frames
2174 if (vf_output_queued_frame(sh_video->vfilter))
2175 break;
2176 unsigned char *packet = NULL;
2177 frame_time = sh_video->next_frame_time;
2178 if (mpctx->update_video_immediately)
2179 frame_time = 0;
2180 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2181 &packet, force_fps);
2182 if (in_size < 0) {
2183 #ifdef CONFIG_DVDNAV
2184 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2185 if (mp_dvdnav_is_eof(mpctx->stream))
2186 return -1;
2187 if (mpctx->d_video)
2188 mpctx->d_video->eof = 0;
2189 if (mpctx->d_audio)
2190 mpctx->d_audio->eof = 0;
2191 mpctx->stream->eof = 0;
2192 } else
2193 #endif
2194 return -1;
2196 if (in_size > max_framesize)
2197 max_framesize = in_size;
2198 sh_video->timer += frame_time;
2199 if (mpctx->sh_audio)
2200 mpctx->delay -= frame_time;
2201 // video_read_frame can change fps (e.g. for ASF video)
2202 vo_fps = sh_video->fps;
2203 int framedrop_type = check_framedrop(mpctx, frame_time);
2204 current_module = "decode video";
2206 void *decoded_frame;
2207 #ifdef CONFIG_DVDNAV
2208 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2209 if (in_size >= 0 && !decoded_frame)
2210 #endif
2211 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2212 sh_video->pts);
2213 #ifdef CONFIG_DVDNAV
2214 // Save last still frame for future display
2215 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2216 #endif
2217 if (decoded_frame) {
2218 // These updates are done here for vf_expand OSD/subtitles
2219 update_subtitles(sh_video, mpctx->d_sub, 0);
2220 update_teletext(sh_video, mpctx->demuxer, 0);
2221 update_osd_msg(mpctx);
2222 current_module = "filter video";
2223 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2224 break;
2227 *blit_frame = 1;
2228 return frame_time;
2231 static double update_video(struct MPContext *mpctx, int *blit_frame)
2233 struct sh_video *sh_video = mpctx->sh_video;
2234 *blit_frame = 0;
2235 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2236 mpctx->osd); // hack for vf_expand
2237 if (!mpctx->opts.correct_pts)
2238 return update_video_nocorrect_pts(mpctx, blit_frame);
2240 double pts;
2242 while (1) {
2243 current_module = "filter_video";
2244 // XXX Time used in this call is not counted in any performance
2245 // timer now, OSD time is not updated correctly for filter-added frames
2246 if (vf_output_queued_frame(sh_video->vfilter))
2247 break;
2248 unsigned char *packet = NULL;
2249 bool hit_eof = false;
2250 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2251 if (in_size < 0) {
2252 // try to extract last frames in case of decoder lag
2253 in_size = 0;
2254 pts = 1e300;
2255 hit_eof = true;
2257 if (in_size > max_framesize)
2258 max_framesize = in_size;
2259 current_module = "decode video";
2260 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2261 void *decoded_frame = decode_video(sh_video, packet, in_size,
2262 framedrop_type, pts);
2263 if (decoded_frame) {
2264 // These updates are done here for vf_expand OSD/subtitles
2265 update_subtitles(sh_video, mpctx->d_sub, 0);
2266 update_teletext(sh_video, mpctx->demuxer, 0);
2267 update_osd_msg(mpctx);
2268 current_module = "filter video";
2269 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2270 break;
2271 } else if (hit_eof)
2272 return -1;
2275 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_GET_PTS, &pts);
2276 if (pts == MP_NOPTS_VALUE) {
2277 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2278 // Try to use decoder pts from before filters
2279 pts = sh_video->pts;
2281 sh_video->pts = pts;
2282 if (sh_video->last_pts == MP_NOPTS_VALUE)
2283 sh_video->last_pts = sh_video->pts;
2284 else if (sh_video->last_pts > sh_video->pts) {
2285 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2286 sh_video->pts, sh_video->last_pts);
2287 /* If the difference in pts is small treat it as jitter around the
2288 * right value (possibly caused by incorrect timestamp ordering) and
2289 * just show this frame immediately after the last one.
2290 * Treat bigger differences as timestamp resets and start counting
2291 * timing of later frames from the position of this one. */
2292 if (sh_video->last_pts - sh_video->pts > 0.5)
2293 sh_video->last_pts = sh_video->pts;
2294 else
2295 sh_video->pts = sh_video->last_pts;
2297 double frame_time = sh_video->pts - sh_video->last_pts;
2298 sh_video->last_pts = sh_video->pts;
2299 sh_video->timer += frame_time;
2300 if (mpctx->sh_audio)
2301 mpctx->delay -= frame_time;
2302 *blit_frame = 1;
2303 return frame_time;
2306 void pause_player(struct MPContext *mpctx)
2308 if (mpctx->paused)
2309 return;
2310 mpctx->paused = 1;
2311 mpctx->step_frames = 0;
2312 mpctx->time_frame -= get_relative_time(mpctx);
2314 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2315 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2317 if (mpctx->audio_out && mpctx->sh_audio)
2318 mpctx->audio_out->pause(); // pause audio, keep data if possible
2321 void unpause_player(struct MPContext *mpctx)
2323 if (!mpctx->paused)
2324 return;
2325 mpctx->paused = 0;
2327 if (mpctx->audio_out && mpctx->sh_audio)
2328 mpctx->audio_out->resume(); // resume audio
2329 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2330 && !mpctx->step_frames)
2331 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2332 (void)get_relative_time(mpctx); // ignore time that passed during pause
2335 void add_step_frame(struct MPContext *mpctx)
2337 mpctx->step_frames++;
2338 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2339 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2340 unpause_player(mpctx);
2343 static void pause_loop(struct MPContext *mpctx)
2345 mp_cmd_t* cmd;
2346 if (!quiet) {
2347 // Small hack to display the pause message on the OSD line.
2348 // The pause string is: "\n == PAUSE == \r" so we need to
2349 // take the first and the last char out
2350 if (term_osd && !mpctx->sh_video) {
2351 char msg[128] = MSGTR_Paused;
2352 int mlen = strlen(msg);
2353 msg[mlen-1] = '\0';
2354 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2355 update_osd_msg(mpctx);
2356 } else
2357 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2358 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2360 #ifdef CONFIG_GUI
2361 if (use_gui)
2362 guiGetEvent(guiCEvent, (char *)guiSetPause);
2363 #endif
2365 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2366 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2367 if (cmd) {
2368 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2369 run_command(mpctx, cmd);
2370 mp_cmd_free(cmd);
2371 continue;
2373 if (mpctx->sh_video && mpctx->video_out)
2374 vo_check_events(mpctx->video_out);
2375 #ifdef CONFIG_GUI
2376 if (use_gui) {
2377 guiEventHandling();
2378 guiGetEvent(guiReDraw, NULL);
2379 if (guiIntfStruct.Playing!=2 || (mpctx->rel_seek_secs || mpctx->abs_seek_pos))
2380 break;
2382 #endif
2383 #ifdef CONFIG_MENU
2384 if (vf_menu)
2385 vf_menu_pause_update(vf_menu);
2386 #endif
2387 usec_sleep(20000);
2388 update_osd_msg(mpctx);
2389 int hack = vo_osd_changed(0);
2390 vo_osd_changed(hack);
2391 if (hack)
2392 break;
2394 #ifdef CONFIG_GUI
2395 if (use_gui) {
2396 if (guiIntfStruct.Playing == guiSetStop)
2397 mpctx->eof = 1;
2398 else
2399 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2401 #endif
2405 // Find the right mute status and record position for new file position
2406 static void edl_seek_reset(MPContext *mpctx)
2408 mpctx->edl_muted = 0;
2409 next_edl_record = edl_records;
2411 while (next_edl_record) {
2412 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2413 break;
2415 if (next_edl_record->action == EDL_MUTE)
2416 mpctx->edl_muted = !mpctx->edl_muted;
2417 next_edl_record = next_edl_record->next;
2419 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2420 mixer_mute(&mpctx->mixer);
2424 // Execute EDL command for the current position if one exists
2425 static void edl_update(MPContext *mpctx)
2427 if (!next_edl_record)
2428 return;
2430 if (!mpctx->sh_video) {
2431 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2432 free_edl(edl_records);
2433 next_edl_record = NULL;
2434 edl_records = NULL;
2435 return;
2438 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2439 if (next_edl_record->action == EDL_SKIP) {
2440 mpctx->osd_function = OSD_FFW;
2441 mpctx->abs_seek_pos = 0;
2442 mpctx->rel_seek_secs = next_edl_record->length_sec;
2443 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2444 "[%f], length [%f]\n", next_edl_record->start_sec,
2445 next_edl_record->stop_sec, next_edl_record->length_sec);
2446 edl_decision = 1;
2448 else if (next_edl_record->action == EDL_MUTE) {
2449 mpctx->edl_muted = !mpctx->edl_muted;
2450 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2451 mixer_mute(&mpctx->mixer);
2452 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2453 next_edl_record->start_sec );
2455 next_edl_record = next_edl_record->next;
2460 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2461 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2462 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2463 static int seek(MPContext *mpctx, double amount, int style)
2465 current_module = "seek";
2466 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2467 return -1;
2469 if (mpctx->sh_video) {
2470 current_module = "seek_video_reset";
2471 resync_video_stream(mpctx->sh_video);
2472 if (mpctx->video_out->config_ok)
2473 vo_control(mpctx->video_out, VOCTRL_RESET, NULL);
2474 mpctx->sh_video->num_buffered_pts = 0;
2475 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2476 mpctx->num_buffered_frames = 0;
2477 mpctx->delay = 0;
2478 mpctx->time_frame = 0;
2479 mpctx->update_video_immediately = true;
2480 // Not all demuxers set d_video->pts during seek, so this value
2481 // (which is used by at least vobsub and edl code below) may
2482 // be completely wrong (probably 0).
2483 mpctx->sh_video->pts = mpctx->d_video->pts;
2484 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2485 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2488 if (mpctx->sh_audio) {
2489 current_module = "seek_audio_reset";
2490 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2491 mpctx->sh_audio->a_buffer_len = 0;
2492 mpctx->sh_audio->a_out_buffer_len = 0;
2495 if (vo_vobsub && mpctx->sh_video) {
2496 current_module = "seek_vobsub_reset";
2497 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2500 edl_seek_reset(mpctx);
2502 mpctx->total_avsync_change = 0;
2503 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2504 drop_frame_cnt = 0;
2506 current_module = NULL;
2507 return 0;
2511 static int read_keys(void *ctx, int fd)
2513 getch2(ctx);
2514 return mplayer_get_key(ctx, 0);
2518 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2519 * file for some tools to link against. */
2520 #ifndef DISABLE_MAIN
2521 int main(int argc,char* argv[]){
2524 char * mem_ptr;
2526 // movie info:
2528 /* Flag indicating whether MPlayer should exit without playing anything. */
2529 int opt_exit = 0;
2531 //float a_frame=0; // Audio
2533 int i;
2535 int gui_no_filename=0;
2537 struct MPContext *mpctx = &(struct MPContext){
2538 .osd_function = OSD_PLAY,
2539 .begin_skip = MP_NOPTS_VALUE,
2540 .play_tree_step = 1,
2541 .global_sub_pos = -1,
2542 .set_of_sub_pos = -1,
2543 .file_format = DEMUXER_TYPE_UNKNOWN,
2544 .last_dvb_step = 1,
2547 InitTimer();
2548 srand(GetTimerMS());
2550 mp_msg_init();
2552 #ifdef CONFIG_X11
2553 mpctx->x11_state = vo_x11_init_state();
2554 #endif
2555 struct MPOpts *opts = &mpctx->opts;
2556 set_default_mplayer_options(opts);
2557 // Create the config context and register the options
2558 mpctx->mconfig = m_config_new(opts, cfg_include);
2559 m_config_register_options(mpctx->mconfig,mplayer_opts);
2560 mp_input_register_options(mpctx->mconfig);
2562 // Preparse the command line
2563 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2565 print_version("MPlayer");
2566 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2567 set_path_env();
2568 #endif
2570 #ifdef CONFIG_TV
2571 stream_tv_defaults.immediate = 1;
2572 #endif
2574 if (argc > 1 && argv[1] &&
2575 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2576 use_gui = !strcmp(argv[1], "-gui");
2577 } else
2578 if ( argv[0] )
2580 char *base = strrchr(argv[0], '/');
2581 if (!base)
2582 base = strrchr(argv[0], '\\');
2583 if (!base)
2584 base = argv[0];
2585 if(strstr(base, "gmplayer"))
2586 use_gui=1;
2589 parse_cfgfiles(mpctx, mpctx->mconfig);
2591 #ifdef CONFIG_GUI
2592 if ( use_gui ) cfg_read();
2593 #endif
2595 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2596 if(mpctx->playtree == NULL)
2597 opt_exit = 1;
2598 else {
2599 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2600 if(mpctx->playtree) {
2601 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2602 if(mpctx->playtree_iter) {
2603 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2604 play_tree_iter_free(mpctx->playtree_iter);
2605 mpctx->playtree_iter = NULL;
2607 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2611 mpctx->key_fifo = mp_fifo_create(opts);
2613 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_GUI)
2614 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2615 if(runningmplayer && mpctx->filename && use_gui){
2616 COPYDATASTRUCT csData;
2617 char file[MAX_PATH];
2618 char *filepart = mpctx->filename;
2619 if(GetFullPathName(mpctx->filename, MAX_PATH, file, &filepart)){
2620 csData.dwData = 0;
2621 csData.cbData = strlen(file)*2;
2622 csData.lpData = file;
2623 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2626 #endif
2628 #if defined(__MINGW32__) || defined(__CYGWIN__)
2629 // stop Windows from showing all kinds of annoying error dialogs
2630 SetErrorMode(0x8003);
2631 // request 1ms timer resolution
2632 timeBeginPeriod(1);
2633 #endif
2635 #ifdef CONFIG_PRIORITY
2636 set_priority();
2637 #endif
2639 #ifndef CONFIG_GUI
2640 if(use_gui){
2641 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2642 use_gui=0;
2644 #else
2645 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
2646 if(use_gui && !vo_init()){
2647 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2648 use_gui=0;
2650 #endif
2651 if (use_gui && mpctx->playtree_iter){
2652 char cwd[PATH_MAX+2];
2653 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2654 play_tree_iter_free(mpctx->playtree_iter);
2655 mpctx->playtree_iter=NULL;
2657 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2659 strcat(cwd, "/");
2660 // Prefix relative paths with current working directory
2661 play_tree_add_bpf(mpctx->playtree, cwd);
2663 // Import initital playtree into GUI.
2664 import_initial_playtree_into_gui(mpctx->playtree, mpctx->mconfig, enqueue);
2666 #endif /* CONFIG_GUI */
2668 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
2669 list_video_out();
2670 opt_exit = 1;
2673 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
2674 list_audio_out();
2675 opt_exit = 1;
2678 /* Check codecs.conf. */
2679 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2680 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2681 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2682 if(!parse_codec_cfg(NULL)){
2683 exit_player_with_rc(mpctx, EXIT_NONE, 0);
2685 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2688 free( mem_ptr ); // release the buffer created by get_path()
2691 #if 0
2692 if(video_codec_list){
2693 int i;
2694 video_codec=video_codec_list[0];
2695 for(i=0;video_codec_list[i];i++)
2696 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2698 #endif
2699 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2700 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2701 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2702 list_codecs(1);
2703 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2704 opt_exit = 1;
2706 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2707 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2708 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2709 list_codecs(0);
2710 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2711 opt_exit = 1;
2713 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2714 vfm_help();
2715 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2716 opt_exit = 1;
2718 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2719 afm_help();
2720 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2721 opt_exit = 1;
2723 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2724 af_help();
2725 printf("\n");
2726 opt_exit = 1;
2728 #ifdef CONFIG_X11
2729 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2730 fstype_help();
2731 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2732 opt_exit = 1;
2734 #endif
2735 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2736 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2737 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2738 demuxer_help();
2739 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2740 opt_exit = 1;
2742 if(list_properties) {
2743 property_print_help();
2744 opt_exit = 1;
2747 if(opt_exit)
2748 exit_player(mpctx, EXIT_NONE);
2750 if (player_idle_mode && use_gui) {
2751 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2752 exit_player_with_rc(mpctx, EXIT_NONE, 1);
2755 if(!mpctx->filename && !player_idle_mode){
2756 if(!use_gui){
2757 // no file/vcd/dvd -> show HELP:
2758 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2759 exit_player_with_rc(mpctx, EXIT_NONE, 0);
2760 } else gui_no_filename=1;
2763 /* Display what configure line was used */
2764 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2766 // Many users forget to include command line in bugreports...
2767 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2768 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2769 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2770 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2773 //------ load global data first ------
2775 mpctx->osd = osd_create();
2777 // check font
2778 #ifdef CONFIG_FREETYPE
2779 init_freetype();
2780 #endif
2781 #ifdef CONFIG_FONTCONFIG
2782 if(font_fontconfig <= 0)
2784 #endif
2785 #ifdef CONFIG_BITMAP_FONT
2786 if(font_name){
2787 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2788 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2789 filename_recode(font_name));
2790 } else {
2791 // try default:
2792 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2793 free(mem_ptr); // release the buffer created by get_path()
2794 if(!vo_font)
2795 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2797 if (sub_font_name)
2798 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2799 else
2800 mpctx->osd->sub_font = vo_font;
2801 #endif
2802 #ifdef CONFIG_FONTCONFIG
2804 #endif
2806 #ifdef CONFIG_ASS
2807 ass_library = ass_init();
2808 #endif
2810 #ifdef HAVE_RTC
2811 if(!nortc)
2813 // seteuid(0); /* Can't hurt to try to get root here */
2814 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2815 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2816 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2817 else {
2818 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2820 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2821 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2822 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2823 close (rtc_fd);
2824 rtc_fd = -1;
2825 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2826 /* variable only by the root */
2827 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2828 close (rtc_fd);
2829 rtc_fd = -1;
2830 } else
2831 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2834 #ifdef CONFIG_GUI
2835 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2836 // and now ? -- Pontscho
2837 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2838 #endif
2839 if(rtc_fd<0)
2840 #endif /* HAVE_RTC */
2841 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2842 softsleep?"software":timer_name);
2844 #ifdef HAVE_TERMCAP
2845 if ( !use_gui ) load_termcap(NULL); // load key-codes
2846 #endif
2848 // ========== Init keyboard FIFO (connection to libvo) ============
2850 // Init input system
2851 current_module = "init_input";
2852 mpctx->input = mp_input_init(&opts->input, use_gui);
2853 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
2854 if(slave_mode)
2855 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
2856 else if(!noconsolecontrols)
2857 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
2858 // Set the libstream interrupt callback
2859 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
2861 #ifdef CONFIG_MENU
2862 if(use_menu) {
2863 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2864 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2865 else {
2866 menu_cfg = get_path("menu.conf");
2867 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2868 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2869 else {
2870 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
2871 MPLAYER_CONFDIR "/menu.conf"))
2872 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2873 else {
2874 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2875 use_menu = 0;
2880 #endif
2882 current_module = NULL;
2884 /// Catch signals
2885 #ifndef __MINGW32__
2886 signal(SIGCHLD,child_sighandler);
2887 #endif
2889 #ifdef CONFIG_CRASH_DEBUG
2890 prog_path = argv[0];
2891 #endif
2892 //========= Catch terminate signals: ================
2893 // terminate requests:
2894 signal(SIGTERM,exit_sighandler); // kill
2895 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2897 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2899 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2900 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2901 #ifdef CONFIG_SIGHANDLER
2902 // fatal errors:
2903 signal(SIGBUS,exit_sighandler); // bus error
2904 signal(SIGSEGV,exit_sighandler); // segfault
2905 signal(SIGILL,exit_sighandler); // illegal instruction
2906 signal(SIGFPE,exit_sighandler); // floating point exc.
2907 signal(SIGABRT,exit_sighandler); // abort()
2908 #ifdef CONFIG_CRASH_DEBUG
2909 if (crash_debug)
2910 signal(SIGTRAP,exit_sighandler);
2911 #endif
2912 #endif
2914 #ifdef CONFIG_GUI
2915 if(use_gui){
2916 guiInit();
2917 guiGetEvent(guiSetContext, mpctx);
2918 mpctx->initialized_flags|=INITIALIZED_GUI;
2919 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2921 #endif
2923 // ******************* Now, let's see the per-file stuff ********************
2925 play_next_file:
2927 // init global sub numbers
2928 mpctx->global_sub_size = 0;
2929 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2931 if (mpctx->filename) {
2932 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
2933 load_per_extension_config (mpctx->mconfig, mpctx->filename);
2934 load_per_file_config (mpctx->mconfig, mpctx->filename);
2937 if (opts->video_driver_list)
2938 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
2939 if (opts->audio_driver_list)
2940 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
2942 // We must enable getch2 here to be able to interrupt network connection
2943 // or cache filling
2944 if(!noconsolecontrols && !slave_mode){
2945 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
2946 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2947 else
2948 getch2_enable(); // prepare stdin for hotkeys...
2949 mpctx->initialized_flags|=INITIALIZED_GETCH2;
2950 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2953 // =================== GUI idle loop (STOP state) ===========================
2954 #ifdef CONFIG_GUI
2955 if ( use_gui ) {
2956 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
2957 guiGetEvent( guiSetDefaults,0 );
2958 while ( guiIntfStruct.Playing != 1 )
2960 mp_cmd_t* cmd;
2961 usec_sleep(20000);
2962 guiEventHandling();
2963 guiGetEvent( guiReDraw,NULL );
2964 if ( (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
2965 guiGetEvent(guiIEvent, (char *)cmd->id);
2966 mp_cmd_free(cmd);
2969 guiGetEvent( guiSetParameters,NULL );
2970 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
2972 play_tree_t * entry = play_tree_new();
2973 play_tree_add_file( entry,guiIntfStruct.Filename );
2974 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
2975 else mpctx->playtree=play_tree_new();
2976 play_tree_set_child( mpctx->playtree,entry );
2977 if(mpctx->playtree)
2979 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2980 if(mpctx->playtree_iter)
2982 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
2984 play_tree_iter_free(mpctx->playtree_iter);
2985 mpctx->playtree_iter = NULL;
2987 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2992 #endif /* CONFIG_GUI */
2994 while (player_idle_mode && !mpctx->filename) {
2995 play_tree_t * entry = NULL;
2996 mp_cmd_t * cmd;
2997 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
2998 if (mpctx->video_out)
2999 vo_check_events(mpctx->video_out);
3000 usec_sleep(20000);
3002 switch (cmd->id) {
3003 case MP_CMD_LOADFILE:
3004 // prepare a tree entry with the new filename
3005 entry = play_tree_new();
3006 play_tree_add_file(entry, cmd->args[0].v.s);
3007 // The entry is added to the main playtree after the switch().
3008 break;
3009 case MP_CMD_LOADLIST:
3010 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3011 break;
3012 case MP_CMD_QUIT:
3013 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3014 break;
3015 case MP_CMD_GET_PROPERTY:
3016 case MP_CMD_SET_PROPERTY:
3017 case MP_CMD_STEP_PROPERTY:
3018 run_command(mpctx, cmd);
3019 break;
3022 mp_cmd_free(cmd);
3024 if (entry) { // user entered a command that gave a valid entry
3025 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3026 play_tree_free_list(mpctx->playtree->child, 1);
3027 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3029 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3031 play_tree_set_child(mpctx->playtree, entry);
3033 /* Make iterator start at the top the of tree. */
3034 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3035 if (!mpctx->playtree_iter) continue;
3037 // find the first real item in the tree
3038 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3039 // no items!
3040 play_tree_iter_free(mpctx->playtree_iter);
3041 mpctx->playtree_iter = NULL;
3042 continue; // wait for next command
3044 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3047 //---------------------------------------------------------------------------
3049 if(mpctx->filename)
3050 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
3051 filename_recode(mpctx->filename));
3053 if (edl_filename) {
3054 if (edl_records) free_edl(edl_records);
3055 next_edl_record = edl_records = edl_parse_file();
3057 if (edl_output_filename) {
3058 if (edl_fd) fclose(edl_fd);
3059 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3061 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
3062 filename_recode(edl_output_filename));
3066 //==================== Open VOB-Sub ============================
3068 current_module="vobsub";
3069 if (vobsub_name){
3070 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3071 if(vo_vobsub==NULL)
3072 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
3073 filename_recode(vobsub_name));
3074 } else if (sub_auto && mpctx->filename){
3075 /* try to autodetect vobsub from movie filename ::atmos */
3076 char *buf = strdup(mpctx->filename), *psub;
3077 char *pdot = strrchr(buf, '.');
3078 char *pslash = strrchr(buf, '/');
3079 #if defined(__MINGW32__) || defined(__CYGWIN__)
3080 if (!pslash) pslash = strrchr(buf, '\\');
3081 #endif
3082 if (pdot && (!pslash || pdot > pslash))
3083 *pdot = '\0';
3084 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3085 /* try from ~/.mplayer/sub */
3086 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3087 char *bname;
3088 int l;
3089 bname = strrchr(buf,'/');
3090 #if defined(__MINGW32__) || defined(__CYGWIN__)
3091 if(!bname) bname = strrchr(buf,'\\');
3092 #endif
3093 if(bname) bname++;
3094 else bname = buf;
3095 l = strlen(psub) + strlen(bname) + 1;
3096 psub = realloc(psub,l);
3097 strcat(psub,bname);
3098 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3099 free(psub);
3101 free(buf);
3103 if(vo_vobsub){
3104 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3105 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3107 // setup global sub numbering
3108 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3109 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3112 //============ Open & Sync STREAM --- fork cache2 ====================
3114 mpctx->stream=NULL;
3115 mpctx->demuxer=NULL;
3116 if (mpctx->d_audio) {
3117 //free_demuxer_stream(mpctx->d_audio);
3118 mpctx->d_audio=NULL;
3120 if (mpctx->d_video) {
3121 //free_demuxer_stream(d_video);
3122 mpctx->d_video=NULL;
3124 mpctx->sh_audio=NULL;
3125 mpctx->sh_video=NULL;
3127 current_module="open_stream";
3128 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3129 if(!mpctx->stream) { // error...
3130 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3131 goto goto_next_file;
3133 mpctx->initialized_flags|=INITIALIZED_STREAM;
3135 #ifdef CONFIG_GUI
3136 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3137 #endif
3139 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3140 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3141 "playlist support will not be used automatically.\n"
3142 "MPlayer's playlist code is unsafe and should only be used with "
3143 "trusted sources.\nPlayback will probably fail.\n\n");
3144 #if 0
3145 play_tree_t* entry;
3146 // Handle playlist
3147 current_module="handle_playlist";
3148 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3149 filename_recode(mpctx->filename));
3150 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3151 mpctx->eof=playtree_add_playlist(mpctx, entry);
3152 goto goto_next_file;
3153 #endif
3155 mpctx->stream->start_pos+=seek_to_byte;
3157 if(stream_dump_type==5){
3158 unsigned char buf[4096];
3159 int len;
3160 FILE *f;
3161 current_module="dumpstream";
3162 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3163 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
3164 exit_player(mpctx, EXIT_ERROR);
3166 stream_reset(mpctx->stream);
3167 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3168 f=fopen(stream_dump_name,"wb");
3169 if(!f){
3170 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3171 exit_player(mpctx, EXIT_ERROR);
3173 if (dvd_chapter > 1) {
3174 int chapter = dvd_chapter - 1;
3175 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3177 while(!mpctx->stream->eof && !async_quit_request){
3178 len=stream_read(mpctx->stream,buf,4096);
3179 if(len>0) {
3180 if(fwrite(buf,len,1,f) != 1) {
3181 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3182 exit_player(mpctx, EXIT_ERROR);
3185 if(dvd_last_chapter > 0) {
3186 int chapter = -1;
3187 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3188 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3189 break;
3192 if(fclose(f)) {
3193 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3194 exit_player(mpctx, EXIT_ERROR);
3196 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3197 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3200 #ifdef CONFIG_DVDREAD
3201 if(mpctx->stream->type==STREAMTYPE_DVD){
3202 current_module="dvd lang->id";
3203 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3204 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3205 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3206 // setup global sub numbering
3207 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3208 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3209 current_module=NULL;
3211 #endif
3213 #ifdef CONFIG_DVDNAV
3214 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3215 current_module="dvdnav lang->id";
3216 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3217 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3218 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3219 // setup global sub numbering
3220 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3221 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3222 current_module=NULL;
3224 #endif
3226 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3227 goto_enable_cache:
3228 if(stream_cache_size>0){
3229 current_module="enable_cache";
3230 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3231 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3232 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3233 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3236 //============ Open DEMUXERS --- DETECT file type =======================
3237 current_module="demux_open";
3239 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3241 // HACK to get MOV Reference Files working
3243 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3245 unsigned char* playlist_entry;
3246 play_tree_t *list = NULL, *entry = NULL;
3248 current_module="handle_demux_playlist";
3249 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3251 char *temp, *bname;
3253 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3254 filename_recode(playlist_entry));
3256 bname=mp_basename(playlist_entry);
3257 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3258 continue;
3260 if (!strncmp(bname,mp_basename(mpctx->filename),strlen(bname))) // ignoring self-reference
3261 continue;
3263 entry = play_tree_new();
3265 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3267 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3268 if (temp)
3270 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3271 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3272 strcat(temp, playlist_entry);
3273 play_tree_add_file(entry,temp);
3274 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3275 free(temp);
3278 else
3279 play_tree_add_file(entry,playlist_entry);
3281 if(!list)
3282 list = entry;
3283 else
3284 play_tree_append_entry(list,entry);
3286 free_demuxer(mpctx->demuxer);
3287 mpctx->demuxer = NULL;
3289 if (list)
3291 entry = play_tree_new();
3292 play_tree_set_child(entry,list);
3293 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3294 goto goto_next_file;
3298 if(!mpctx->demuxer)
3299 goto goto_next_file;
3300 if(dvd_chapter>1) {
3301 float pts;
3302 if (demuxer_seek_chapter(mpctx->demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
3303 seek(mpctx, pts, SEEK_ABSOLUTE);
3306 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3308 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3309 int i;
3310 int maxid = -1;
3311 // setup global sub numbering
3312 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3313 for (i = 0; i < MAX_S_STREAMS; i++)
3314 if (mpctx->demuxer->s_streams[i])
3315 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3316 mpctx->global_sub_size += maxid + 1;
3318 // Make opts->sub_id always selectable if set.
3319 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3320 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3322 #ifdef CONFIG_ASS
3323 if (ass_enabled && ass_library) {
3324 for (i = 0; i < mpctx->demuxer->num_attachments; ++i) {
3325 demux_attachment_t* att = mpctx->demuxer->attachments + i;
3326 if (extract_embedded_fonts &&
3327 att->name && att->type && att->data && att->data_size &&
3328 (strcmp(att->type, "application/x-truetype-font") == 0 ||
3329 strcmp(att->type, "application/x-font") == 0))
3330 ass_add_font(ass_library, att->name, att->data, att->data_size);
3333 #endif
3335 current_module="demux_open2";
3337 //file_format=demuxer->file_format;
3339 mpctx->d_audio=mpctx->demuxer->audio;
3340 mpctx->d_video=mpctx->demuxer->video;
3341 mpctx->d_sub=mpctx->demuxer->sub;
3343 // select audio stream
3344 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3346 // DUMP STREAMS:
3347 if((stream_dump_type)&&(stream_dump_type<4)){
3348 FILE *f;
3349 demux_stream_t *ds=NULL;
3350 current_module="dump";
3351 // select stream to dump
3352 switch(stream_dump_type){
3353 case 1: ds=mpctx->d_audio;break;
3354 case 2: ds=mpctx->d_video;break;
3355 case 3: ds=mpctx->d_sub;break;
3357 if(!ds){
3358 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3359 exit_player(mpctx, EXIT_ERROR);
3361 // disable other streams:
3362 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3363 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3364 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3365 // let's dump it!
3366 f=fopen(stream_dump_name,"wb");
3367 if(!f){
3368 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3369 exit_player(mpctx, EXIT_ERROR);
3371 while(!ds->eof){
3372 unsigned char* start;
3373 int in_size=ds_get_packet(ds,&start);
3374 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3375 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3376 if(in_size>0) fwrite(start,in_size,1,f);
3377 if(dvd_last_chapter>0) {
3378 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3379 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3380 break;
3383 fclose(f);
3384 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3385 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3388 mpctx->sh_audio=mpctx->d_audio->sh;
3389 mpctx->sh_video=mpctx->d_video->sh;
3391 if(mpctx->sh_video){
3393 current_module="video_read_properties";
3394 if(!video_read_properties(mpctx->sh_video)) {
3395 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3396 mpctx->sh_video=mpctx->d_video->sh=NULL;
3397 } else {
3398 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_FilefmtFourccSizeFpsFtime,
3399 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3400 mpctx->sh_video->fps,mpctx->sh_video->frametime
3403 /* need to set fps here for output encoders to pick it up in their init */
3404 if(force_fps){
3405 mpctx->sh_video->fps=force_fps;
3406 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3408 vo_fps = mpctx->sh_video->fps;
3410 if(!mpctx->sh_video->fps && !force_fps){
3411 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3412 mpctx->sh_video=mpctx->d_video->sh=NULL;
3418 if(!mpctx->sh_video && !mpctx->sh_audio){
3419 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3420 #ifdef CONFIG_DVBIN
3421 if(mpctx->stream->type == STREAMTYPE_DVB)
3423 int dir;
3424 int v = mpctx->last_dvb_step;
3425 if(v > 0)
3426 dir = DVB_CHANNEL_HIGHER;
3427 else
3428 dir = DVB_CHANNEL_LOWER;
3430 if(dvb_step_channel(mpctx->stream, dir)) {
3431 mpctx->stop_play = PT_NEXT_ENTRY;
3432 mpctx->dvbin_reopen = 1;
3435 #endif
3436 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3439 /* display clip info */
3440 demux_info_print(mpctx->demuxer);
3442 //================== Read SUBTITLES (DVD & TEXT) ==========================
3443 if(vo_spudec==NULL && mpctx->sh_video &&
3444 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3445 init_vo_spudec(mpctx);
3448 if(mpctx->sh_video) {
3449 // after reading video params we should load subtitles because
3450 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3451 // check .sub
3452 current_module="read_subtitles_file";
3453 if(sub_name){
3454 for (i = 0; sub_name[i] != NULL; ++i)
3455 add_subtitles(mpctx, sub_name[i], mpctx->sh_video->fps, 0);
3457 if(sub_auto) { // auto load sub file ...
3458 char *psub = get_path( "sub/" );
3459 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3460 int i = 0;
3461 free(psub); // release the buffer created by get_path() above
3462 while (tmp[i]) {
3463 add_subtitles(mpctx, tmp[i], mpctx->sh_video->fps, 1);
3464 free(tmp[i++]);
3466 free(tmp);
3468 if (mpctx->set_of_sub_size > 0) {
3469 // setup global sub numbering
3470 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3471 mpctx->global_sub_size += mpctx->set_of_sub_size;
3475 if (mpctx->global_sub_size) {
3476 // find the best sub to use
3477 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3478 mpctx->global_sub_pos = -1; // no subs by default
3479 if (vobsub_index_id >= 0) {
3480 // if user asks for a vobsub id, use that first.
3481 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3482 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3483 // if user asks for a dvd sub id, use that next.
3484 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3485 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3486 // if there are text subs to use, use those. (autosubs come last here)
3487 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3488 } else if (opts->sub_id < 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3489 // finally select subs by language and container hints
3490 if (opts->sub_id < 0 && dvdsub_lang)
3491 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3492 if (opts->sub_id < 0)
3493 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3494 if (opts->sub_id >= 0)
3495 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3497 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3498 mpctx->global_sub_pos--;
3499 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3500 if(subdata)
3501 switch (stream_dump_type) {
3502 case 3: list_sub_file(subdata); break;
3503 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3504 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3505 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3506 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3507 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3511 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3512 filename_recode(mpctx->filename));
3513 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3514 if (mpctx->sh_video) {
3515 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3516 if (mpctx->sh_video->format >= 0x20202020)
3517 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3518 else
3519 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3520 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3521 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3522 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3523 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3524 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3526 if (mpctx->sh_audio) {
3527 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3528 if (mpctx->sh_audio->format >= 0x20202020)
3529 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3530 else
3531 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3532 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3533 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3534 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3536 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3537 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n", mpctx->stream->seek ? 1 : 0);
3538 if (mpctx->demuxer) {
3539 if (mpctx->demuxer->num_chapters == 0)
3540 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3541 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3544 if(!mpctx->sh_video) goto main; // audio-only
3546 if(!reinit_video_chain(mpctx)) {
3547 if(!mpctx->sh_video){
3548 if(!mpctx->sh_audio) goto goto_next_file;
3549 goto main; // exit_player(MSGTR_Exit_error);
3553 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3554 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3556 #ifdef CONFIG_FREETYPE
3557 force_load_font = 1;
3558 #endif
3560 //================== MAIN: ==========================
3561 main:
3562 current_module="main";
3564 if(playing_msg) {
3565 char* msg = property_expand_string(mpctx, playing_msg);
3566 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3567 free(msg);
3571 // Disable the term OSD in verbose mode
3572 if(verbose) term_osd = 0;
3575 //int frame_corr_num=0; //
3576 //float v_frame=0; // Video
3577 //float num_frames=0; // number of frames played
3579 int frame_time_remaining=0; // flag
3580 int blit_frame=0;
3581 mpctx->num_buffered_frames=0;
3583 // Make sure old OSD does not stay around,
3584 // e.g. with -fixed-vo and same-resolution files
3585 clear_osd_msgs();
3586 update_osd_msg(mpctx);
3588 //================ SETUP AUDIO ==========================
3590 if(mpctx->sh_audio){
3591 reinit_audio_chain(mpctx);
3592 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3593 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3596 current_module="av_init";
3598 if(mpctx->sh_video){
3599 mpctx->sh_video->timer=0;
3600 if (! ignore_start)
3601 audio_delay += mpctx->sh_video->stream_delay;
3603 if(mpctx->sh_audio){
3604 if (start_volume >= 0)
3605 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3606 if (! ignore_start)
3607 audio_delay -= mpctx->sh_audio->stream_delay;
3608 mpctx->delay=-audio_delay;
3611 if(!mpctx->sh_audio){
3612 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3613 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3614 ds_free_packs(mpctx->d_audio); // free buffered chunks
3615 //mpctx->d_audio->id=-2; // do not read audio chunks
3616 //uninit_player(mpctx, INITIALIZED_AO); // close device
3618 if(!mpctx->sh_video){
3619 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3620 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3621 ds_free_packs(mpctx->d_video);
3622 mpctx->d_video->id=-2;
3623 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3626 if (!mpctx->sh_video && !mpctx->sh_audio)
3627 goto goto_next_file;
3629 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3630 if(force_fps && mpctx->sh_video){
3631 vo_fps = mpctx->sh_video->fps=force_fps;
3632 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3633 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3636 #ifdef CONFIG_GUI
3637 if ( use_gui ) {
3638 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3639 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3640 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3641 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3642 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3644 #endif
3646 mp_input_set_section(mpctx->input, NULL);
3647 //TODO: add desired (stream-based) sections here
3648 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3649 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3651 //==================== START PLAYING =======================
3653 if(opts->loop_times>1) opts->loop_times--; else
3654 if(opts->loop_times==1) opts->loop_times = -1;
3656 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3658 total_time_usage_start=GetTimer();
3659 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3660 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3661 play_n_frames=play_n_frames_mf;
3663 if(play_n_frames==0){
3664 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
3667 if (seek_to_sec) {
3668 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3669 end_at.pos += seek_to_sec;
3672 if (end_at.type == END_AT_SIZE) {
3673 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3674 end_at.type = END_AT_NONE;
3677 #ifdef CONFIG_DVDNAV
3678 mp_dvdnav_context_free(mpctx);
3679 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3680 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3681 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3683 #endif
3685 get_relative_time(mpctx); // reset current delta
3686 mpctx->time_frame = 0;
3687 mpctx->drop_message_shown = 0;
3688 mpctx->update_video_immediately = true;
3689 mpctx->total_avsync_change = 0;
3691 while(!mpctx->stop_play){
3692 float aq_sleep_time=0;
3694 if(dvd_last_chapter>0) {
3695 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3696 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3697 goto goto_next_file;
3700 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3701 mpctx->sh_audio = mpctx->d_audio->sh;
3702 mpctx->sh_audio->ds = mpctx->d_audio;
3703 reinit_audio_chain(mpctx);
3706 /*========================== PLAY AUDIO ============================*/
3708 if (mpctx->sh_audio && !mpctx->paused)
3709 if (!fill_audio_out_buffers(mpctx))
3710 // at eof, all audio at least written to ao
3711 if (!mpctx->sh_video)
3712 mpctx->stop_play = AT_END_OF_FILE;
3715 if(!mpctx->sh_video) {
3716 // handle audio-only case:
3717 double a_pos=0;
3718 // sh_audio can be NULL due to video stream switching
3719 // TODO: handle this better
3720 if (mpctx->sh_audio)
3721 a_pos = playing_audio_pts(mpctx);
3723 print_status(mpctx, a_pos, false);
3725 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3726 mpctx->stop_play = PT_NEXT_ENTRY;
3727 update_osd_msg(mpctx);
3729 } else {
3731 /*========================== PLAY VIDEO ============================*/
3733 vo_pts=mpctx->sh_video->timer*90000.0;
3734 vo_fps=mpctx->sh_video->fps;
3736 if (!mpctx->num_buffered_frames) {
3737 double frame_time = update_video(mpctx, &blit_frame);
3738 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3739 if (mpctx->sh_video->vf_initialized < 0) {
3740 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3741 mpctx->stop_play = PT_NEXT_ENTRY;
3742 goto goto_next_file;
3744 if (blit_frame) {
3745 struct vf_instance *vf = mpctx->sh_video->vfilter;
3746 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
3747 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
3748 vo_osd_changed(0);
3750 if (frame_time < 0)
3751 mpctx->stop_play = AT_END_OF_FILE;
3752 else {
3753 // might return with !eof && !blit_frame if !correct_pts
3754 mpctx->num_buffered_frames += blit_frame;
3755 if (mpctx->update_video_immediately) {
3756 // Show this frame immediately, rest normally
3757 mpctx->update_video_immediately = false;
3758 } else {
3759 mpctx->time_frame += frame_time / opts->playback_speed;
3760 adjust_sync(mpctx, frame_time);
3765 // ==========================================================================
3767 // current_module="draw_osd";
3768 // if(vo_config_count) mpctx->video_out->draw_osd();
3770 #ifdef CONFIG_GUI
3771 if(use_gui) guiEventHandling();
3772 #endif
3774 current_module="vo_check_events";
3775 vo_check_events(mpctx->video_out);
3777 #ifdef CONFIG_X11
3778 if (stop_xscreensaver) {
3779 current_module = "stop_xscreensaver";
3780 xscreensaver_heartbeat(mpctx->x11_state);
3782 #endif
3783 if (heartbeat_cmd) {
3784 static unsigned last_heartbeat;
3785 unsigned now = GetTimerMS();
3786 if (now - last_heartbeat > 30000) {
3787 last_heartbeat = now;
3788 system(heartbeat_cmd);
3792 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
3794 //====================== FLIP PAGE (VIDEO BLT): =========================
3796 current_module="flip_page";
3797 if (!frame_time_remaining && blit_frame) {
3798 unsigned int t2=GetTimer();
3800 vo_flip_page(mpctx->video_out);
3801 mpctx->num_buffered_frames--;
3803 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
3804 vout_time_usage += mpctx->last_vo_flip_duration;
3805 print_status(mpctx, MP_NOPTS_VALUE, true);
3807 else
3808 print_status(mpctx, MP_NOPTS_VALUE, false);
3810 //============================ Auto QUALITY ============================
3812 /*Output quality adjustments:*/
3813 if(auto_quality>0){
3814 current_module="autoq";
3815 // float total=0.000001f * (GetTimer()-aq_total_time);
3816 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3817 if(output_quality<auto_quality && aq_sleep_time>0)
3818 ++output_quality;
3819 else
3820 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3821 if(output_quality>1 && aq_sleep_time<0)
3822 --output_quality;
3823 else
3824 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3825 output_quality=0;
3826 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3827 set_video_quality(mpctx->sh_video,output_quality);
3830 if (!frame_time_remaining && blit_frame) {
3831 if (play_n_frames >= 0) {
3832 --play_n_frames;
3833 if (play_n_frames <= 0)
3834 mpctx->stop_play = PT_NEXT_ENTRY;
3836 if (mpctx->step_frames > 0) {
3837 mpctx->step_frames--;
3838 if (mpctx->step_frames == 0)
3839 pause_player(mpctx);
3844 // FIXME: add size based support for -endpos
3845 if (end_at.type == END_AT_TIME &&
3846 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3847 mpctx->stop_play = PT_NEXT_ENTRY;
3849 } // end if(mpctx->sh_video)
3851 #ifdef CONFIG_DVDNAV
3852 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3853 nav_highlight_t hl;
3854 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3855 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3856 vo_osd_changed (OSDTYPE_DVDNAV);
3858 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
3859 double ar = -1.0;
3860 if (stream_control (mpctx->demuxer->stream,
3861 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
3862 != STREAM_UNSUPPORTED)
3863 mpctx->sh_video->stream_aspect = ar;
3866 #endif
3868 //================= Keyboard events, SEEKing ====================
3870 current_module="key_events";
3873 while (1) {
3874 mp_cmd_t* cmd;
3875 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
3876 run_command(mpctx, cmd);
3877 mp_cmd_free(cmd);
3878 if (mpctx->stop_play)
3879 break;
3881 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
3882 || mpctx->abs_seek_pos)
3883 break;
3884 if (mpctx->sh_video) {
3885 update_osd_msg(mpctx);
3886 int hack = vo_osd_changed(0);
3887 vo_osd_changed(hack);
3888 if (hack)
3889 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
3890 add_step_frame(mpctx);
3891 break;
3893 else
3894 vo_osd_changed(0);
3896 pause_loop(mpctx);
3900 // handle -sstep
3901 if (step_sec > 0 && !mpctx->paused) {
3902 mpctx->osd_function=OSD_FFW;
3903 mpctx->rel_seek_secs+=step_sec;
3906 edl_update(mpctx);
3908 /* Looping. */
3909 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
3910 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
3912 if(opts->loop_times>1) opts->loop_times--; else
3913 if(opts->loop_times==1) opts->loop_times=-1;
3914 play_n_frames=play_n_frames_mf;
3915 mpctx->stop_play=0;
3916 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
3917 loop_seek = 1;
3920 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
3921 if (seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos) >= 0) {
3922 // Set OSD:
3923 if(!loop_seek){
3924 if( !edl_decision )
3925 set_osd_bar(mpctx, 0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3929 mpctx->rel_seek_secs=0;
3930 mpctx->abs_seek_pos=0;
3931 loop_seek=0;
3932 edl_decision = 0;
3935 #ifdef CONFIG_GUI
3936 if(use_gui){
3937 guiEventHandling();
3938 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3939 // get pos from frame number / total frames
3940 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3941 } else {
3942 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3944 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3945 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx);
3946 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3947 guiGetEvent( guiReDraw,NULL );
3948 guiGetEvent( guiSetVolume,NULL );
3949 if(guiIntfStruct.Playing==0) break; // STOP
3950 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3951 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3952 #ifdef CONFIG_DVDREAD
3953 if ( mpctx->stream->type == STREAMTYPE_DVD )
3955 dvd_priv_t * dvdp = mpctx->stream->priv;
3956 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3958 #endif
3960 #endif /* CONFIG_GUI */
3962 } // while(!mpctx->stop_play)
3964 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
3966 #ifdef CONFIG_DVBIN
3967 if(mpctx->dvbin_reopen)
3969 mpctx->stop_play = 0;
3970 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
3971 cache_uninit(mpctx->stream);
3972 mpctx->dvbin_reopen = 0;
3973 goto goto_enable_cache;
3975 #endif
3978 goto_next_file: // don't jump here after ao/vo/getch initialization!
3980 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3982 if(benchmark){
3983 double tot=video_time_usage+vout_time_usage+audio_time_usage;
3984 double total_time_usage;
3985 total_time_usage_start=GetTimer()-total_time_usage_start;
3986 total_time_usage = (float)total_time_usage_start*0.000001;
3987 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
3988 video_time_usage,vout_time_usage,audio_time_usage,
3989 total_time_usage-tot,total_time_usage);
3990 if(total_time_usage>0.0)
3991 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
3992 100.0*video_time_usage/total_time_usage,
3993 100.0*vout_time_usage/total_time_usage,
3994 100.0*audio_time_usage/total_time_usage,
3995 100.0*(total_time_usage-tot)/total_time_usage,
3996 100.0);
3997 if(total_frame_cnt && frame_dropping)
3998 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
3999 total_frame_cnt-drop_frame_cnt,
4000 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4001 drop_frame_cnt,
4002 100*drop_frame_cnt/total_frame_cnt,
4003 total_frame_cnt,
4004 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4007 // time to uninit all, except global stuff:
4008 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI+(opts->fixed_vo?INITIALIZED_VO:0)));
4010 if(mpctx->set_of_sub_size > 0) {
4011 current_module="sub_free";
4012 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4013 sub_free(mpctx->set_of_subtitles[i]);
4014 #ifdef CONFIG_ASS
4015 if(mpctx->set_of_ass_tracks[i])
4016 ass_free_track( mpctx->set_of_ass_tracks[i] );
4017 #endif
4019 mpctx->set_of_sub_size = 0;
4021 vo_sub_last = vo_sub=NULL;
4022 subdata=NULL;
4023 #ifdef CONFIG_ASS
4024 ass_track = NULL;
4025 if(ass_library)
4026 ass_clear_fonts(ass_library);
4027 #endif
4029 if (!mpctx->stop_play) // In case some goto jumped here...
4030 mpctx->stop_play = PT_NEXT_ENTRY;
4032 int playtree_direction = 1;
4034 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4035 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4036 play_tree_iter_free(mpctx->playtree_iter);
4037 mpctx->playtree_iter = NULL;
4039 mpctx->play_tree_step = 1;
4040 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4041 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4042 if(mpctx->playtree_iter) {
4043 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4044 play_tree_iter_free(mpctx->playtree_iter);
4045 mpctx->playtree_iter = NULL;
4048 } else if (mpctx->stop_play == PT_STOP) {
4049 play_tree_iter_free(mpctx->playtree_iter);
4050 mpctx->playtree_iter = NULL;
4051 } else { // NEXT PREV SRC
4052 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4055 while(mpctx->playtree_iter != NULL) {
4056 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4057 if(mpctx->filename == NULL) {
4058 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4059 play_tree_iter_free(mpctx->playtree_iter);
4060 mpctx->playtree_iter = NULL;
4062 } else
4063 break;
4066 #ifdef CONFIG_GUI
4067 if(use_gui && !mpctx->playtree_iter) {
4068 #ifdef CONFIG_DVDREAD
4069 if(!guiIntfStruct.DiskChanged)
4070 #endif
4071 mplEnd();
4073 #endif
4075 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4076 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4077 mpctx->stop_play = 0;
4078 goto play_next_file;
4082 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4084 return 1;
4086 #endif /* DISABLE_MAIN */