Merge svn changes up to r28461
[mplayer.git] / mplayer.c
blob54331685f70919d7f69b8c115a9be56b9b39d9cd
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 #if defined(__MINGW32__) || defined(__CYGWIN__)
94 char * proc_priority=NULL;
95 #endif
97 char *heartbeat_cmd;
99 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
101 #ifdef HAVE_RTC
102 #ifdef __linux__
103 #include <linux/rtc.h>
104 #else
105 #include <rtc.h>
106 #define RTC_IRQP_SET RTCIO_IRQP_SET
107 #define RTC_PIE_ON RTCIO_PIE_ON
108 #endif /* __linux__ */
109 #endif /* HAVE_RTC */
111 #ifdef CONFIG_TV
112 #include "stream/tv.h"
113 #endif
114 #ifdef CONFIG_RADIO
115 #include "stream/stream_radio.h"
116 #endif
118 #ifdef CONFIG_DVBIN
119 #include "stream/dvbin.h"
120 #include "stream/cache2.h"
121 #endif
123 //**************************************************************************//
124 // Playtree
125 //**************************************************************************//
126 #include "playtree.h"
127 #include "playtreeparser.h"
129 int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
130 int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
132 //**************************************************************************//
133 // Config
134 //**************************************************************************//
135 #include "parser-cfg.h"
136 #include "parser-mpcmd.h"
138 //**************************************************************************//
139 // Config file
140 //**************************************************************************//
142 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
144 #include "get_path.h"
146 //**************************************************************************//
147 //**************************************************************************//
148 // Input media streaming & demultiplexer:
149 //**************************************************************************//
151 static int max_framesize=0;
153 #include "stream/stream.h"
154 #include "libmpdemux/demuxer.h"
155 #include "libmpdemux/stheader.h"
157 #ifdef CONFIG_DVDREAD
158 #include "stream/stream_dvd.h"
159 #endif
161 #ifdef CONFIG_DVDNAV
162 #include "stream/stream_dvdnav.h"
163 #endif
165 #include "libmpcodecs/dec_audio.h"
166 #include "libmpcodecs/dec_video.h"
167 #include "libmpcodecs/mp_image.h"
168 #include "libmpcodecs/vf.h"
169 #include "libmpcodecs/vd.h"
171 #include "mixer.h"
173 #include "mp_core.h"
174 #include "options.h"
175 #include "defaultopts.h"
177 //**************************************************************************//
178 //**************************************************************************//
180 // Common FIFO functions, and keyboard/event FIFO code
181 #include "mp_fifo.h"
182 int noconsolecontrols=0;
183 //**************************************************************************//
185 // benchmark:
186 double video_time_usage=0;
187 double vout_time_usage=0;
188 static double audio_time_usage=0;
189 static int total_time_usage_start=0;
190 static int total_frame_cnt=0;
191 static int drop_frame_cnt=0; // total number of dropped frames
192 int benchmark=0;
194 // options:
195 int auto_quality=0;
196 static int output_quality=0;
198 int use_gui=0;
200 #ifdef CONFIG_GUI
201 int enqueue=0;
202 #endif
204 static int list_properties = 0;
206 int osd_level=1;
207 // if nonzero, hide current OSD contents when GetTimerMS() reaches this
208 unsigned int osd_visible;
209 int osd_duration = 1000;
211 int term_osd = 1;
212 static char* term_osd_esc = "\x1b[A\r\x1b[K";
213 static char* playing_msg = NULL;
214 // seek:
215 static double seek_to_sec;
216 static off_t seek_to_byte=0;
217 static off_t step_sec=0;
218 static int loop_seek=0;
220 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
222 // A/V sync:
223 int autosync=0; // 30 might be a good default value.
225 // codecs:
226 char **audio_codec_list=NULL; // override audio codec
227 char **video_codec_list=NULL; // override video codec
228 char **audio_fm_list=NULL; // override audio codec family
229 char **video_fm_list=NULL; // override video codec family
231 // demuxer:
232 extern char *demuxer_name; // override demuxer
233 extern char *audio_demuxer_name; // override audio demuxer
234 extern char *sub_demuxer_name; // override sub demuxer
236 int vobsub_id=-1;
237 char* audio_lang=NULL;
238 char* dvdsub_lang=NULL;
239 static char* spudec_ifo=NULL;
240 int forced_subs_only=0;
241 int file_filter=1;
243 // cache2:
244 int stream_cache_size=-1;
245 #ifdef CONFIG_STREAM_CACHE
246 extern int cache_fill_status;
248 float stream_cache_min_percent=20.0;
249 float stream_cache_seek_min_percent=50.0;
250 #else
251 #define cache_fill_status 0
252 #endif
254 // dump:
255 static char *stream_dump_name="stream.dump";
256 int stream_dump_type=0;
258 // A-V sync:
259 static float default_max_pts_correction=-1;//0.01f;
260 float audio_delay=0;
261 static int ignore_start=0;
263 static int softsleep=0;
265 double force_fps=0;
266 static int force_srate=0;
267 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
268 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
269 static int play_n_frames=-1;
270 static int play_n_frames_mf=-1;
272 // sub:
273 char *font_name=NULL;
274 char *sub_font_name=NULL;
275 extern int font_fontconfig;
276 float font_factor=0.75;
277 char **sub_name=NULL;
278 float sub_delay=0;
279 float sub_fps=0;
280 int sub_auto = 1;
281 char *vobsub_name=NULL;
282 /*DSP!!char *dsp=NULL;*/
283 int subcc_enabled=0;
284 int suboverlap_enabled = 1;
286 #ifdef CONFIG_ASS
287 #include "libass/ass.h"
288 #include "libass/ass_mp.h"
289 #endif
291 char* current_module=NULL; // for debugging
294 // ---
296 #ifdef CONFIG_MENU
297 #include "m_struct.h"
298 #include "libmenu/menu.h"
299 void vf_menu_pause_update(struct vf_instance* vf);
300 extern vf_info_t vf_info_menu;
301 static const vf_info_t* const libmenu_vfs[] = {
302 &vf_info_menu,
303 NULL
305 static vf_instance_t* vf_menu = NULL;
306 int use_menu = 0;
307 static char* menu_cfg = NULL;
308 static char* menu_root = "main";
309 #endif
312 #ifdef HAVE_RTC
313 static int nortc = 1;
314 static char* rtc_device;
315 #endif
317 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
318 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
319 short edl_decision = 0; ///< 1 when an EDL operation has been made.
320 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
321 int use_filedir_conf;
323 #include "mpcommon.h"
324 #include "command.h"
326 #include "metadata.h"
328 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
330 const void *mpctx_get_video_out(MPContext *mpctx)
332 return mpctx->video_out;
335 const void *mpctx_get_audio_out(MPContext *mpctx)
337 return mpctx->audio_out;
340 void *mpctx_get_demuxer(MPContext *mpctx)
342 return mpctx->demuxer;
345 void *mpctx_get_playtree_iter(MPContext *mpctx)
347 return mpctx->playtree_iter;
350 void *mpctx_get_mixer(MPContext *mpctx)
352 return &mpctx->mixer;
355 int mpctx_get_global_sub_size(MPContext *mpctx)
357 return mpctx->global_sub_size;
360 int mpctx_get_osd_function(MPContext *mpctx)
362 return mpctx->osd_function;
365 static float get_relative_time(struct MPContext *mpctx)
367 unsigned int new_time = GetTimer();
368 unsigned int delta = new_time - mpctx->last_time;
369 mpctx->last_time = new_time;
370 return delta * 0.000001;
373 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
374 switch (type)
376 /* check for valid video stream */
377 case META_VIDEO_CODEC:
378 case META_VIDEO_BITRATE:
379 case META_VIDEO_RESOLUTION:
381 if (!mpctx->sh_video)
382 return 0;
383 break;
386 /* check for valid audio stream */
387 case META_AUDIO_CODEC:
388 case META_AUDIO_BITRATE:
389 case META_AUDIO_SAMPLES:
391 if (!mpctx->sh_audio)
392 return 0;
393 break;
396 /* check for valid demuxer */
397 case META_INFO_TITLE:
398 case META_INFO_ARTIST:
399 case META_INFO_ALBUM:
400 case META_INFO_YEAR:
401 case META_INFO_COMMENT:
402 case META_INFO_TRACK:
403 case META_INFO_GENRE:
405 if (!mpctx->demuxer)
406 return 0;
407 break;
410 default:
411 break;
414 return 1;
417 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
418 char **info = mpctx->demuxer->info;
419 int n;
421 if (!info || !tag)
422 return NULL;
424 for (n = 0; info[2*n] != NULL ; n++)
425 if (!strcmp (info[2*n], tag))
426 break;
428 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
431 char *get_metadata(struct MPContext *mpctx, metadata_t type)
433 char *meta = NULL;
434 sh_audio_t * const sh_audio = mpctx->sh_audio;
435 sh_video_t * const sh_video = mpctx->sh_video;
437 if (!is_valid_metadata_type(mpctx, type))
438 return NULL;
440 switch (type)
442 case META_NAME:
444 return strdup (mp_basename2 (mpctx->filename));
447 case META_VIDEO_CODEC:
449 if (sh_video->format == 0x10000001)
450 meta = strdup ("mpeg1");
451 else if (sh_video->format == 0x10000002)
452 meta = strdup ("mpeg2");
453 else if (sh_video->format == 0x10000004)
454 meta = strdup ("mpeg4");
455 else if (sh_video->format == 0x10000005)
456 meta = strdup ("h264");
457 else if (sh_video->format >= 0x20202020)
459 meta = malloc (8);
460 sprintf (meta, "%.4s", (char *) &sh_video->format);
462 else
464 meta = malloc (8);
465 sprintf (meta, "0x%08X", sh_video->format);
467 return meta;
470 case META_VIDEO_BITRATE:
472 meta = malloc (16);
473 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
474 return meta;
477 case META_VIDEO_RESOLUTION:
479 meta = malloc (16);
480 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
481 return meta;
484 case META_AUDIO_CODEC:
486 if (sh_audio->codec && sh_audio->codec->name)
487 meta = strdup (sh_audio->codec->name);
488 return meta;
491 case META_AUDIO_BITRATE:
493 meta = malloc (16);
494 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
495 return meta;
498 case META_AUDIO_SAMPLES:
500 meta = malloc (16);
501 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
502 return meta;
505 /* check for valid demuxer */
506 case META_INFO_TITLE:
507 return get_demuxer_info(mpctx, "Title");
509 case META_INFO_ARTIST:
510 return get_demuxer_info(mpctx, "Artist");
512 case META_INFO_ALBUM:
513 return get_demuxer_info(mpctx, "Album");
515 case META_INFO_YEAR:
516 return get_demuxer_info(mpctx, "Year");
518 case META_INFO_COMMENT:
519 return get_demuxer_info(mpctx, "Comment");
521 case META_INFO_TRACK:
522 return get_demuxer_info(mpctx, "Track");
524 case META_INFO_GENRE:
525 return get_demuxer_info(mpctx, "Genre");
527 default:
528 break;
531 return meta;
534 /// step size of mixer changes
535 int volstep = 3;
537 #ifdef CONFIG_DVDNAV
538 static void mp_dvdnav_context_free(MPContext *ctx){
539 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
540 ctx->nav_smpi = NULL;
541 if (ctx->nav_buffer) free(ctx->nav_buffer);
542 ctx->nav_buffer = NULL;
543 ctx->nav_start = NULL;
544 ctx->nav_in_size = 0;
546 #endif
548 void uninit_player(struct MPContext *mpctx, unsigned int mask){
549 mask=mpctx->initialized_flags&mask;
551 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
553 if(mask&INITIALIZED_ACODEC){
554 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
555 current_module="uninit_acodec";
556 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
557 #ifdef CONFIG_GUI
558 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
559 #endif
560 mpctx->sh_audio=NULL;
561 mpctx->mixer.afilter = NULL;
564 if(mask&INITIALIZED_VCODEC){
565 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
566 current_module="uninit_vcodec";
567 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
568 mpctx->sh_video=NULL;
569 #ifdef CONFIG_MENU
570 vf_menu=NULL;
571 #endif
574 if(mask&INITIALIZED_DEMUXER){
575 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
576 current_module="free_demuxer";
577 if(mpctx->demuxer){
578 mpctx->stream=mpctx->demuxer->stream;
579 free_demuxer(mpctx->demuxer);
581 mpctx->demuxer=NULL;
584 // kill the cache process:
585 if(mask&INITIALIZED_STREAM){
586 mpctx->initialized_flags&=~INITIALIZED_STREAM;
587 current_module="uninit_stream";
588 if(mpctx->stream) free_stream(mpctx->stream);
589 mpctx->stream=NULL;
592 if(mask&INITIALIZED_VO){
593 mpctx->initialized_flags&=~INITIALIZED_VO;
594 current_module="uninit_vo";
595 vo_destroy(mpctx->video_out);
596 mpctx->video_out=NULL;
597 #ifdef CONFIG_DVDNAV
598 mp_dvdnav_context_free(mpctx);
599 #endif
602 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
603 if(mask&INITIALIZED_GETCH2){
604 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
605 current_module="uninit_getch2";
606 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
607 // restore terminal:
608 getch2_disable();
611 if(mask&INITIALIZED_VOBSUB){
612 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
613 current_module="uninit_vobsub";
614 if(vo_vobsub) vobsub_close(vo_vobsub);
615 vo_vobsub=NULL;
618 if (mask&INITIALIZED_SPUDEC){
619 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
620 current_module="uninit_spudec";
621 spudec_free(vo_spudec);
622 vo_spudec=NULL;
625 if(mask&INITIALIZED_AO){
626 mpctx->initialized_flags&=~INITIALIZED_AO;
627 current_module="uninit_ao";
628 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
629 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
630 mpctx->audio_out=NULL;
633 #ifdef CONFIG_GUI
634 if(mask&INITIALIZED_GUI){
635 mpctx->initialized_flags&=~INITIALIZED_GUI;
636 current_module="uninit_gui";
637 guiDone();
639 #endif
641 current_module=NULL;
644 void exit_player_with_rc(struct MPContext *mpctx, exit_reason_t how, int rc)
646 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
647 uninit_player(mpctx, INITIALIZED_ALL);
648 #if defined(__MINGW32__) || defined(__CYGWIN__)
649 timeEndPeriod(1);
650 #endif
651 #ifdef CONFIG_X11
652 #ifdef CONFIG_GUI
653 if ( !use_gui )
654 #endif
655 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
656 #endif
658 current_module="uninit_input";
659 mp_input_uninit(mpctx->input);
660 #ifdef CONFIG_MENU
661 if (use_menu)
662 menu_uninit();
663 #endif
665 #ifdef CONFIG_FREETYPE
666 current_module="uninit_font";
667 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
668 free_font_desc(mpctx->osd->sub_font);
669 free_font_desc(vo_font);
670 vo_font = NULL;
671 done_freetype();
672 #endif
673 osd_free(mpctx->osd);
675 #ifdef CONFIG_ASS
676 ass_library_done(ass_library);
677 #endif
679 current_module="exit_player";
681 // free mplayer config
682 if(mpctx->mconfig)
683 m_config_free(mpctx->mconfig);
685 if(mpctx->playtree)
686 play_tree_free(mpctx->playtree, 1);
688 talloc_free(mpctx->key_fifo);
690 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
691 switch(how) {
692 case EXIT_QUIT:
693 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_quit);
694 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
695 break;
696 case EXIT_EOF:
697 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_eof);
698 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
699 break;
700 case EXIT_ERROR:
701 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_error);
702 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
703 break;
704 default:
705 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
707 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
709 exit(rc);
712 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
714 exit_player_with_rc(mpctx, how, 1);
717 #ifndef __MINGW32__
718 static void child_sighandler(int x){
719 pid_t pid;
720 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
722 #endif
724 #ifdef CONFIG_CRASH_DEBUG
725 static char *prog_path;
726 static int crash_debug = 0;
727 #endif
729 static void exit_sighandler(int x){
730 static int sig_count=0;
731 #ifdef CONFIG_CRASH_DEBUG
732 if (!crash_debug || x != SIGTRAP)
733 #endif
734 ++sig_count;
735 if(sig_count==5)
737 /* We're crashing bad and can't uninit cleanly :(
738 * by popular request, we make one last (dirty)
739 * effort to restore the user's
740 * terminal. */
741 getch2_disable();
742 exit(1);
744 if(sig_count==6) exit(1);
745 if(sig_count>6){
746 // can't stop :(
747 #ifndef __MINGW32__
748 kill(getpid(),SIGKILL);
749 #endif
751 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
752 current_module?current_module:"unknown"
754 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
755 if(sig_count<=1)
756 switch(x){
757 case SIGINT:
758 case SIGQUIT:
759 case SIGTERM:
760 case SIGKILL:
761 async_quit_request = 1;
762 return; // killed from keyboard (^C) or killed [-9]
763 case SIGILL:
764 #ifdef RUNTIME_CPUDETECT
765 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
766 #else
767 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
768 #endif
769 case SIGFPE:
770 case SIGSEGV:
771 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
772 default:
773 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
774 #ifdef CONFIG_CRASH_DEBUG
775 if (crash_debug) {
776 int gdb_pid;
777 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
778 gdb_pid = fork();
779 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
780 if (gdb_pid == 0) { // We are the child
781 char spid[20];
782 snprintf(spid, sizeof(spid), "%i", getppid());
783 getch2_disable(); // allow terminal to work properly with gdb
784 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
785 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
786 } else if (gdb_pid < 0)
787 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
788 else {
789 waitpid(gdb_pid, NULL, 0);
791 if (x == SIGTRAP) return;
793 #endif
795 getch2_disable();
796 exit(1);
799 #include "cfg-mplayer.h"
801 static int cfg_include(m_option_t *conf, char *filename)
803 return m_config_parse_config_file(conf->priv, filename);
806 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
808 char *conffile;
809 int conffile_fd;
810 if (!disable_system_conf &&
811 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
812 exit_player(mpctx, EXIT_NONE);
813 if ((conffile = get_path("")) == NULL) {
814 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
815 } else {
816 #ifdef __MINGW32__
817 mkdir(conffile);
818 #else
819 mkdir(conffile, 0777);
820 #endif
821 free(conffile);
822 if ((conffile = get_path("config")) == NULL) {
823 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
824 } else {
825 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
826 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
827 write(conffile_fd, default_config, strlen(default_config));
828 close(conffile_fd);
830 if (!disable_user_conf &&
831 m_config_parse_config_file(conf, conffile) < 0)
832 exit_player(mpctx, EXIT_NONE);
833 free(conffile);
838 #define PROFILE_CFG_PROTOCOL "protocol."
840 static void load_per_protocol_config (m_config_t* conf, const char *const file)
842 char *str;
843 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
844 m_profile_t *p;
846 /* does filename actually uses a protocol ? */
847 str = strstr (file, "://");
848 if (!str)
849 return;
851 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
852 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
853 p = m_config_get_profile (conf, protocol);
854 if (p)
856 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingProtocolProfile, protocol);
857 m_config_set_profile(conf,p);
861 #define PROFILE_CFG_EXTENSION "extension."
863 static void load_per_extension_config (m_config_t* conf, const char *const file)
865 char *str;
866 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
867 m_profile_t *p;
869 /* does filename actually have an extension ? */
870 str = strrchr (file, '.');
871 if (!str)
872 return;
874 sprintf (extension, PROFILE_CFG_EXTENSION);
875 strncat (extension, ++str, 7);
876 p = m_config_get_profile (conf, extension);
877 if (p)
879 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, extension);
880 m_config_set_profile(conf,p);
884 #define PROFILE_CFG_VO "vo."
885 #define PROFILE_CFG_AO "ao."
887 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
889 char profile[strlen (cfg) + strlen (out) + 1];
890 m_profile_t *p;
892 sprintf (profile, "%s%s", cfg, out);
893 p = m_config_get_profile (conf, profile);
894 if (p)
896 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, profile);
897 m_config_set_profile(conf,p);
901 static void load_per_file_config (m_config_t* conf, const char *const file)
903 char *confpath;
904 char cfg[strlen(file)+10];
905 struct stat st;
906 char *name;
908 sprintf (cfg, "%s.conf", file);
910 if (use_filedir_conf && !stat (cfg, &st))
912 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
913 m_config_parse_config_file (conf, cfg);
914 return;
917 if ((name = strrchr (cfg, '/')) == NULL)
918 name = cfg;
919 else
920 name++;
922 if ((confpath = get_path (name)) != NULL)
924 if (!stat (confpath, &st))
926 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
927 m_config_parse_config_file (conf, confpath);
930 free (confpath);
934 /* When libmpdemux performs a blocking operation (network connection or
935 * cache filling) if the operation fails we use this function to check
936 * if it was interrupted by the user.
937 * The function returns a new value for eof. */
938 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
940 mp_cmd_t* cmd;
941 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
942 switch(cmd->id) {
943 case MP_CMD_QUIT:
944 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
945 case MP_CMD_PLAY_TREE_STEP: {
946 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
947 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
948 } break;
949 case MP_CMD_PLAY_TREE_UP_STEP: {
950 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
951 } break;
952 case MP_CMD_PLAY_ALT_SRC_STEP: {
953 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
954 } break;
956 mp_cmd_free(cmd);
958 return stop_play;
961 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
963 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
965 play_tree_add_bpf(entry,mpctx->filename);
967 #ifdef CONFIG_GUI
968 if (use_gui) {
969 if (entry) {
970 import_playtree_playlist_into_gui(entry, mpctx->mconfig);
971 play_tree_free_list(entry,1);
973 } else
974 #endif
976 if(!entry) {
977 entry = mpctx->playtree_iter->tree;
978 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
979 return PT_NEXT_ENTRY;
981 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
982 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
983 return PT_NEXT_ENTRY;
986 play_tree_remove(entry,1,1);
987 return PT_NEXT_SRC;
989 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
990 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
991 entry = mpctx->playtree_iter->tree;
992 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
993 return PT_NEXT_ENTRY;
995 play_tree_remove(entry,1,1);
997 return PT_NEXT_SRC;
1000 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1002 sub_data *subd;
1003 #ifdef CONFIG_ASS
1004 ass_track_t *asst = 0;
1005 #endif
1007 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1008 return;
1011 subd = sub_read_file(filename, fps);
1012 #ifdef CONFIG_ASS
1013 if (ass_enabled)
1014 #ifdef CONFIG_ICONV
1015 asst = ass_read_file(ass_library, filename, sub_cp);
1016 #else
1017 asst = ass_read_file(ass_library, filename, 0);
1018 #endif
1019 if (ass_enabled && subd && !asst)
1020 asst = ass_read_subdata(ass_library, subd, fps);
1022 if (!asst && !subd)
1023 #else
1024 if(!subd)
1025 #endif
1026 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
1027 filename_recode(filename));
1029 #ifdef CONFIG_ASS
1030 if (!asst && !subd) return;
1031 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1032 #else
1033 if (!subd) return;
1034 #endif
1035 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1036 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1037 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1038 filename_recode(filename));
1039 ++mpctx->set_of_sub_size;
1040 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
1041 filename_recode(filename));
1044 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1045 void update_set_of_subtitles(struct MPContext *mpctx)
1046 // subdata was changed, set_of_sub... have to be updated.
1048 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
1049 int i;
1050 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1051 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
1052 set_of_subtitles[i-1] = set_of_subtitles[i];
1053 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
1054 --mpctx->set_of_sub_size;
1055 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
1057 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1058 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
1060 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1061 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1062 ++mpctx->set_of_sub_size;
1066 void init_vo_spudec(struct MPContext *mpctx)
1068 if (vo_spudec)
1069 spudec_free(vo_spudec);
1070 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1071 vo_spudec = NULL;
1072 if (spudec_ifo) {
1073 unsigned int palette[16], width, height;
1074 current_module="spudec_init_vobsub";
1075 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1076 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1079 #ifdef CONFIG_DVDREAD
1080 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1081 current_module="spudec_init_dvdread";
1082 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1083 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1084 NULL, 0);
1086 #endif
1088 #ifdef CONFIG_DVDNAV
1089 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1090 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1091 current_module="spudec_init_dvdnav";
1092 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1094 #endif
1096 if (vo_spudec==NULL) {
1097 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1098 current_module="spudec_init_normal";
1099 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1100 spudec_set_font_factor(vo_spudec,font_factor);
1103 if (vo_spudec!=NULL)
1104 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1108 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1109 * make it all work is to use the builtin SDL-bootstrap code, which
1110 * will be done automatically by replacing our main() if we include SDL.h.
1112 #if defined(__APPLE__) && defined(CONFIG_SDL)
1113 #include <SDL.h>
1114 #endif
1117 * \brief append a formatted string
1118 * \param buf buffer to print into
1119 * \param pos position of terminating 0 in buf
1120 * \param len maximum number of characters in buf, not including terminating 0
1121 * \param format printf format string
1123 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1125 va_list va;
1126 va_start(va, format);
1127 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1128 va_end(va);
1129 if (*pos >= len ) {
1130 buf[len] = 0;
1131 *pos = len;
1136 * \brief append time in the hh:mm:ss.f format
1137 * \param buf buffer to print into
1138 * \param pos position of terminating 0 in buf
1139 * \param len maximum number of characters in buf, not including terminating 0
1140 * \param time time value to convert/append
1142 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1143 long tenths = 10 * time;
1144 int f1 = tenths % 10;
1145 int ss = (tenths / 10) % 60;
1146 int mm = (tenths / 600) % 60;
1147 int hh = tenths / 36000;
1148 if (time <= 0) {
1149 saddf(buf, pos, len, "unknown");
1150 return;
1152 if (hh > 0)
1153 saddf(buf, pos, len, "%2d:", hh);
1154 if (hh > 0 || mm > 0)
1155 saddf(buf, pos, len, "%02d:", mm);
1156 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1159 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1161 struct MPOpts *opts = &mpctx->opts;
1162 sh_video_t * const sh_video = mpctx->sh_video;
1164 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1165 a_pos = playing_audio_pts(mpctx);
1166 if (mpctx->sh_audio && sh_video && at_frame) {
1167 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1168 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1169 && !mpctx->drop_message_shown) {
1170 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1171 mpctx->drop_message_shown = true;
1174 if (quiet)
1175 return;
1178 int width;
1179 char *line;
1180 unsigned pos = 0;
1181 get_screen_size();
1182 if (screen_width > 0)
1183 width = screen_width;
1184 else
1185 width = 80;
1186 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1187 /* Windows command line is broken (MinGW's rxvt works, but we
1188 * should not depend on that). */
1189 width--;
1190 #endif
1191 line = malloc(width + 1); // one additional char for the terminating null
1193 // Audio time
1194 if (mpctx->sh_audio) {
1195 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1196 if (!sh_video) {
1197 float len = demuxer_get_time_length(mpctx->demuxer);
1198 saddf(line, &pos, width, "(");
1199 sadd_hhmmssf(line, &pos, width, a_pos);
1200 saddf(line, &pos, width, ") of %.1f (", len);
1201 sadd_hhmmssf(line, &pos, width, len);
1202 saddf(line, &pos, width, ") ");
1206 // Video time
1207 if (sh_video)
1208 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1210 // A-V sync
1211 if (mpctx->sh_audio && sh_video)
1212 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1213 mpctx->last_av_difference, mpctx->total_avsync_change);
1215 // Video stats
1216 if (sh_video)
1217 saddf(line, &pos, width, "%3d/%3d ",
1218 (int)sh_video->num_frames,
1219 (int)sh_video->num_frames_decoded);
1221 // CPU usage
1222 if (sh_video) {
1223 if (sh_video->timer > 0.5)
1224 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1225 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1226 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1227 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1228 else
1229 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1230 } else if (mpctx->sh_audio) {
1231 if (mpctx->delay > 0.5)
1232 saddf(line, &pos, width, "%4.1f%% ",
1233 100.0*audio_time_usage/(double)mpctx->delay);
1234 else
1235 saddf(line, &pos, width, "??,?%% ");
1238 // VO stats
1239 if (sh_video)
1240 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1242 #ifdef CONFIG_STREAM_CACHE
1243 // cache stats
1244 if (stream_cache_size > 0)
1245 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1246 #endif
1248 // other
1249 if (opts->playback_speed != 1)
1250 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1252 // end
1253 if (erase_to_end_of_line) {
1254 line[pos] = 0;
1255 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1256 } else {
1257 memset(&line[pos], ' ', width - pos);
1258 line[width] = 0;
1259 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1261 free(line);
1265 * \brief build a chain of audio filters that converts the input format
1266 * to the ao's format, taking into account the current playback_speed.
1267 * \param sh_audio describes the requested input format of the chain.
1268 * \param ao_data describes the requested output format of the chain.
1270 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1272 struct MPOpts *opts = &mpctx->opts;
1273 int new_srate;
1274 int result;
1275 if (!sh_audio)
1277 #ifdef CONFIG_GUI
1278 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1279 #endif
1280 mpctx->mixer.afilter = NULL;
1281 return 0;
1283 if(af_control_any_rev(sh_audio->afilter,
1284 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1285 &opts->playback_speed)) {
1286 new_srate = sh_audio->samplerate;
1287 } else {
1288 new_srate = sh_audio->samplerate * opts->playback_speed;
1289 if (new_srate != ao_data->samplerate) {
1290 // limits are taken from libaf/af_resample.c
1291 if (new_srate < 8000)
1292 new_srate = 8000;
1293 if (new_srate > 192000)
1294 new_srate = 192000;
1295 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1298 result = init_audio_filters(sh_audio, new_srate,
1299 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1300 mpctx->mixer.afilter = sh_audio->afilter;
1301 #ifdef CONFIG_GUI
1302 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1303 #endif
1304 return result;
1308 typedef struct mp_osd_msg mp_osd_msg_t;
1309 struct mp_osd_msg {
1310 /// Previous message on the stack.
1311 mp_osd_msg_t* prev;
1312 /// Message text.
1313 char msg[128];
1314 int id,level,started;
1315 /// Display duration in ms.
1316 unsigned time;
1319 /// OSD message stack.
1320 static mp_osd_msg_t* osd_msg_stack = NULL;
1323 * \brief Add a message on the OSD message stack
1325 * If a message with the same id is already present in the stack
1326 * it is pulled on top of the stack, otherwise a new message is created.
1330 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1331 mp_osd_msg_t *msg,*last=NULL;
1332 va_list va;
1333 int r;
1335 // look if the id is already in the stack
1336 for(msg = osd_msg_stack ; msg && msg->id != id ;
1337 last = msg, msg = msg->prev);
1338 // not found: alloc it
1339 if(!msg) {
1340 msg = calloc(1,sizeof(mp_osd_msg_t));
1341 msg->prev = osd_msg_stack;
1342 osd_msg_stack = msg;
1343 } else if(last) { // found, but it's not on top of the stack
1344 last->prev = msg->prev;
1345 msg->prev = osd_msg_stack;
1346 osd_msg_stack = msg;
1348 // write the msg
1349 va_start(va,fmt);
1350 r = vsnprintf(msg->msg, 128, fmt, va);
1351 va_end(va);
1352 if(r >= 128) msg->msg[127] = 0;
1353 // set id and time
1354 msg->id = id;
1355 msg->level = level;
1356 msg->time = time;
1361 * \brief Remove a message from the OSD stack
1363 * This function can be used to get rid of a message right away.
1367 void rm_osd_msg(int id) {
1368 mp_osd_msg_t *msg,*last=NULL;
1370 // Search for the msg
1371 for(msg = osd_msg_stack ; msg && msg->id != id ;
1372 last = msg, msg = msg->prev);
1373 if(!msg) return;
1375 // Detach it from the stack and free it
1376 if(last)
1377 last->prev = msg->prev;
1378 else
1379 osd_msg_stack = msg->prev;
1380 free(msg);
1384 * \brief Remove all messages from the OSD stack
1388 static void clear_osd_msgs(void) {
1389 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1390 while(msg) {
1391 prev = msg->prev;
1392 free(msg);
1393 msg = prev;
1395 osd_msg_stack = NULL;
1399 * \brief Get the current message from the OSD stack.
1401 * This function decrements the message timer and destroys the old ones.
1402 * The message that should be displayed is returned (if any).
1406 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1408 mp_osd_msg_t *msg,*prev,*last = NULL;
1409 static unsigned last_update = 0;
1410 unsigned now = GetTimerMS();
1411 unsigned diff;
1412 char hidden_dec_done = 0;
1414 if (osd_visible) {
1415 // 36000000 means max timed visibility is 1 hour into the future, if
1416 // the difference is greater assume it's wrapped around from below 0
1417 if (osd_visible - now > 36000000) {
1418 osd_visible = 0;
1419 vo_osd_progbar_type = -1; // disable
1420 vo_osd_changed(OSDTYPE_PROGBAR);
1421 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1425 if(!last_update) last_update = now;
1426 diff = now >= last_update ? now - last_update : 0;
1428 last_update = now;
1430 // Look for the first message in the stack with high enough level.
1431 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1432 prev = msg->prev;
1433 if(msg->level > osd_level && hidden_dec_done) continue;
1434 // The message has a high enough level or it is the first hidden one
1435 // in both cases we decrement the timer or kill it.
1436 if(!msg->started || msg->time > diff) {
1437 if(msg->started) msg->time -= diff;
1438 else msg->started = 1;
1439 // display it
1440 if(msg->level <= osd_level) return msg;
1441 hidden_dec_done = 1;
1442 continue;
1444 // kill the message
1445 free(msg);
1446 if(last) {
1447 last->prev = prev;
1448 msg = last;
1449 } else {
1450 osd_msg_stack = prev;
1451 msg = NULL;
1454 // Nothing found
1455 return NULL;
1459 * \brief Display the OSD bar.
1461 * Display the OSD bar or fall back on a simple message.
1465 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1467 if(osd_level < 1) return;
1469 if(mpctx->sh_video) {
1470 osd_visible = (GetTimerMS() + 1000) | 1;
1471 vo_osd_progbar_type = type;
1472 vo_osd_progbar_value = 256*(val-min)/(max-min);
1473 vo_osd_changed(OSDTYPE_PROGBAR);
1474 return;
1477 set_osd_msg(OSD_MSG_BAR,1,osd_duration,"%s: %d %%",
1478 name,ROUND(100*(val-min)/(max-min)));
1483 * \brief Update the OSD message line.
1485 * This function displays the current message on the vo OSD or on the term.
1486 * If the stack is empty and the OSD level is high enough the timer
1487 * is displayed (only on the vo OSD).
1491 static void update_osd_msg(struct MPContext *mpctx)
1493 mp_osd_msg_t *msg;
1494 struct osd_state *osd = mpctx->osd;
1495 char osd_text_timer[128];
1497 // Look if we have a msg
1498 if((msg = get_osd_msg(mpctx))) {
1499 if (strcmp(osd->osd_text, msg->msg)) {
1500 strncpy(osd->osd_text, msg->msg, 127);
1501 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1502 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1504 return;
1507 if(mpctx->sh_video) {
1508 // fallback on the timer
1509 if(osd_level>=2) {
1510 int len = demuxer_get_time_length(mpctx->demuxer);
1511 int percentage = -1;
1512 char percentage_text[10];
1513 int pts = demuxer_get_current_time(mpctx->demuxer);
1515 if (mpctx->osd_show_percentage)
1516 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1518 if (percentage >= 0)
1519 snprintf(percentage_text, 9, " (%d%%)", percentage);
1520 else
1521 percentage_text[0] = 0;
1523 if (osd_level == 3)
1524 snprintf(osd_text_timer, 63,
1525 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1526 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1527 len/3600,(len/60)%60,len%60,percentage_text);
1528 else
1529 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1530 mpctx->osd_function,pts/3600,(pts/60)%60,
1531 pts%60,percentage_text);
1532 } else
1533 osd_text_timer[0]=0;
1535 // always decrement the percentage timer
1536 if(mpctx->osd_show_percentage)
1537 mpctx->osd_show_percentage--;
1539 if (strcmp(osd->osd_text, osd_text_timer)) {
1540 strncpy(osd->osd_text, osd_text_timer, 63);
1541 vo_osd_changed(OSDTYPE_OSD);
1543 return;
1546 // Clear the term osd line
1547 if (term_osd && osd->osd_text[0]) {
1548 osd->osd_text[0] = 0;
1549 printf("%s\n",term_osd_esc);
1553 ///@}
1554 // OSDMsgStack
1557 void reinit_audio_chain(struct MPContext *mpctx)
1559 struct MPOpts *opts = &mpctx->opts;
1560 if(mpctx->sh_audio){
1561 current_module="init_audio_codec";
1562 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1563 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1564 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1565 mpctx->d_audio->id = -2;
1566 return;
1567 } else
1568 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1569 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1572 //const ao_info_t *info=audio_out->info;
1573 current_module="af_preinit";
1574 ao_data.samplerate=force_srate;
1575 ao_data.channels=0;
1576 ao_data.format=audio_output_format;
1577 #if 1
1578 // first init to detect best values
1579 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1580 // input:
1581 mpctx->sh_audio->samplerate,
1582 // output:
1583 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1584 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1585 exit_player(mpctx, EXIT_ERROR);
1587 #endif
1588 current_module="ao2_init";
1589 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1590 0, // plugin flag
1591 ao_data.samplerate,
1592 ao_data.channels,
1593 ao_data.format,0))){
1594 // FAILED:
1595 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1596 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1597 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1598 mpctx->d_audio->id = -2;
1599 return;
1600 } else {
1601 // SUCCESS:
1602 mpctx->initialized_flags|=INITIALIZED_AO;
1603 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1604 mpctx->audio_out->info->short_name,
1605 ao_data.samplerate, ao_data.channels,
1606 af_fmt2str_short(ao_data.format),
1607 af_fmt2bits(ao_data.format)/8 );
1608 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1609 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1610 if(strlen(mpctx->audio_out->info->comment) > 0)
1611 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1612 // init audio filters:
1613 #if 1
1614 current_module="af_init";
1615 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1616 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1617 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1618 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1619 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1621 #endif
1623 mpctx->mixer.audio_out = mpctx->audio_out;
1624 mpctx->mixer.volstep = volstep;
1629 ///@}
1630 // Command2Property
1633 // Return pts value corresponding to the end point of audio written to the
1634 // ao so far.
1635 static double written_audio_pts(struct MPContext *mpctx)
1637 sh_audio_t *sh_audio = mpctx->sh_audio;
1638 demux_stream_t *d_audio = mpctx->d_audio;
1639 double buffered_output;
1640 // first calculate the end pts of audio that has been output by decoder
1641 double a_pts = sh_audio->pts;
1642 if (a_pts != MP_NOPTS_VALUE)
1643 // Good, decoder supports new way of calculating audio pts.
1644 // sh_audio->pts is the timestamp of the latest input packet with
1645 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1646 // the amount of bytes the decoder has written after that timestamp.
1647 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1648 else {
1649 // Decoder doesn't support new way of calculating pts (or we're
1650 // being called before it has decoded anything with known timestamp).
1651 // Use the old method of audio pts calculation: take the timestamp
1652 // of last packet with known pts the decoder has read data from,
1653 // and add amount of bytes read after the beginning of that packet
1654 // divided by input bps. This will be inaccurate if the input/output
1655 // ratio is not constant for every audio packet or if it is constant
1656 // but not accurately known in sh_audio->i_bps.
1658 a_pts = d_audio->pts;
1659 // ds_tell_pts returns bytes read after last timestamp from
1660 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1661 // it has read but not decoded
1662 if (sh_audio->i_bps)
1663 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1664 (double)sh_audio->i_bps;
1666 // Now a_pts hopefully holds the pts for end of audio from decoder.
1667 // Substract data in buffers between decoder and audio out.
1669 // Decoded but not filtered
1670 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1672 // Data buffered in audio filters, measured in bytes of "missing" output
1673 buffered_output = af_calc_delay(sh_audio->afilter);
1675 // Data that was ready for ao but was buffered because ao didn't fully
1676 // accept everything to internal buffers yet
1677 buffered_output += sh_audio->a_out_buffer_len;
1679 // Filters divide audio length by playback_speed, so multiply by it
1680 // to get the length in original units without speedup or slowdown
1681 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1683 return a_pts;
1686 // Return pts value corresponding to currently playing audio.
1687 double playing_audio_pts(struct MPContext *mpctx)
1689 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1690 mpctx->audio_out->get_delay();
1693 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1694 struct MPOpts *opts = &mpctx->opts;
1695 // check for frame-drop:
1696 current_module = "check_framedrop";
1697 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1698 static int dropped_frames;
1699 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1700 float d = delay-mpctx->delay;
1701 ++total_frame_cnt;
1702 // we should avoid dropping too many frames in sequence unless we
1703 // are too late. and we allow 100ms A-V delay here:
1704 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1705 && !mpctx->update_video_immediately) {
1706 ++drop_frame_cnt;
1707 ++dropped_frames;
1708 return frame_dropping;
1709 } else
1710 dropped_frames = 0;
1712 return 0;
1716 #ifdef HAVE_RTC
1717 int rtc_fd = -1;
1718 #endif
1720 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1722 #ifdef HAVE_RTC
1723 if (rtc_fd >= 0){
1724 // -------- RTC -----------
1725 current_module="sleep_rtc";
1726 while (time_frame > 0.000) {
1727 unsigned long rtc_ts;
1728 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1729 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1730 time_frame -= get_relative_time(mpctx);
1732 } else
1733 #endif
1735 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1736 // unnecessarily high CPU usage
1737 float margin = softsleep ? 0.011 : 0;
1738 current_module = "sleep_timer";
1739 while (time_frame > margin) {
1740 usec_sleep(1000000 * (time_frame - margin));
1741 time_frame -= get_relative_time(mpctx);
1743 if (softsleep){
1744 current_module = "sleep_soft";
1745 if (time_frame < 0)
1746 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1747 while (time_frame > 0)
1748 time_frame -= get_relative_time(mpctx); // burn the CPU
1751 return time_frame;
1754 #ifdef CONFIG_DVDNAV
1755 #ifndef FF_B_TYPE
1756 #define FF_B_TYPE 3
1757 #endif
1758 /// store decoded video image
1759 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1760 mp_image_t *from_mpi) {
1761 mp_image_t *mpi;
1763 /// Do not store B-frames
1764 if (from_mpi->pict_type == FF_B_TYPE)
1765 return to_mpi;
1767 if (to_mpi &&
1768 to_mpi->w == from_mpi->w &&
1769 to_mpi->h == from_mpi->h &&
1770 to_mpi->imgfmt == from_mpi->imgfmt)
1771 mpi = to_mpi;
1772 else {
1773 if (to_mpi)
1774 free_mp_image(to_mpi);
1775 if (from_mpi->w == 0 || from_mpi->h == 0)
1776 return NULL;
1777 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1780 copy_mpi(mpi,from_mpi);
1781 return mpi;
1784 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1785 struct MPOpts *opts = &ctx->opts;
1786 if (ctx->sh_video) {
1787 /// clear video pts
1788 ctx->d_video->pts = 0.0f;
1789 ctx->sh_video->pts = 0.0f;
1790 ctx->sh_video->i_pts = 0.0f;
1791 ctx->sh_video->last_pts = 0.0f;
1792 ctx->sh_video->num_buffered_pts = 0;
1793 ctx->sh_video->num_frames = 0;
1794 ctx->sh_video->num_frames_decoded = 0;
1795 ctx->sh_video->timer = 0.0f;
1796 ctx->sh_video->stream_delay = 0.0f;
1797 ctx->sh_video->timer = 0;
1798 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1801 if (ctx->sh_audio) {
1802 /// free audio packets and reset
1803 ds_free_packs(ctx->d_audio);
1804 audio_delay -= ctx->sh_audio->stream_delay;
1805 ctx->delay =- audio_delay;
1806 ctx->audio_out->reset();
1807 resync_audio_stream(ctx->sh_audio);
1810 if (ctx->d_sub) opts->sub_id = -2;
1812 audio_delay = 0.0f;
1814 /// clear all EOF related flags
1815 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1818 /// Restore last decoded DVDNAV (still frame)
1819 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1820 int *in_size,
1821 unsigned char **start,
1822 mp_image_t *decoded_frame)
1824 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1825 return decoded_frame;
1827 /// a change occured in dvdnav stream
1828 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1829 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1830 mp_dvdnav_context_free(mpctx);
1831 mp_dvdnav_reset_stream(mpctx);
1832 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1833 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1836 if (*in_size < 0) {
1837 float len;
1839 /// Display still frame, if any
1840 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1841 decoded_frame = mpctx->nav_smpi;
1843 /// increment video frame : continue playing after still frame
1844 len = demuxer_get_time_length(mpctx->demuxer);
1845 if (mpctx->sh_video->pts >= len &&
1846 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1847 mp_dvdnav_skip_still(mpctx->stream);
1848 mp_dvdnav_skip_wait(mpctx->stream);
1850 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1852 if (mpctx->nav_buffer) {
1853 *start = mpctx->nav_start;
1854 *in_size = mpctx->nav_in_size;
1855 if (mpctx->nav_start)
1856 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1860 return decoded_frame;
1863 /// Save last decoded DVDNAV (still frame)
1864 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1865 unsigned char *start,
1866 mp_image_t *decoded_frame)
1868 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1869 return;
1871 if (mpctx->nav_buffer)
1872 free(mpctx->nav_buffer);
1874 mpctx->nav_buffer = malloc(in_size);
1875 mpctx->nav_start = start;
1876 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1877 if (mpctx->nav_buffer)
1878 memcpy(mpctx->nav_buffer,start,in_size);
1880 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1881 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1883 #endif /* CONFIG_DVDNAV */
1885 /* Modify video timing to match the audio timeline. There are two main
1886 * reasons this is needed. First, video and audio can start from different
1887 * positions at beginning of file or after a seek (MPlayer starts both
1888 * immediately even if they have different pts). Second, the file can have
1889 * audio timestamps that are inconsistent with the duration of the audio
1890 * packets, for example two consecutive timestamp values differing by
1891 * one second but only a packet with enough samples for half a second
1892 * of playback between them.
1894 static void adjust_sync(struct MPContext *mpctx, double frame_time)
1896 struct MPOpts *opts = &mpctx->opts;
1897 current_module = "av_sync";
1899 if (!mpctx->sh_audio)
1900 return;
1902 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
1903 double v_pts = mpctx->sh_video->pts;
1904 double av_delay = a_pts - v_pts;
1905 // Try to sync vo_flip() so it will *finish* at given time
1906 av_delay += mpctx->last_vo_flip_duration;
1907 av_delay -= audio_delay; // This much pts difference is desired
1909 double change = av_delay * 0.1;
1910 double max_change = default_max_pts_correction >= 0 ?
1911 default_max_pts_correction : frame_time * 0.1;
1912 if (change < -max_change)
1913 change = -max_change;
1914 else if (change > max_change)
1915 change = max_change;
1916 mpctx->delay += change;
1917 mpctx->total_avsync_change += change;
1920 static int fill_audio_out_buffers(struct MPContext *mpctx)
1922 struct MPOpts *opts = &mpctx->opts;
1923 unsigned int t;
1924 double tt;
1925 int playsize;
1926 int playflags=0;
1927 int audio_eof=0;
1928 int bytes_to_write;
1929 sh_audio_t * const sh_audio = mpctx->sh_audio;
1931 current_module="play_audio";
1933 while (1) {
1934 int sleep_time;
1935 // all the current uses of ao_data.pts seem to be in aos that handle
1936 // sync completely wrong; there should be no need to use ao_data.pts
1937 // in get_space()
1938 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1939 bytes_to_write = mpctx->audio_out->get_space();
1940 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
1941 break;
1943 // handle audio-only case:
1944 // this is where mplayer sleeps during audio-only playback
1945 // to avoid 100% CPU use
1946 sleep_time = (ao_data.outburst - bytes_to_write) * 1000 / ao_data.bps;
1947 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
1948 usec_sleep(sleep_time * 1000);
1951 while (bytes_to_write) {
1952 playsize = bytes_to_write;
1953 if (playsize > MAX_OUTBURST)
1954 playsize = MAX_OUTBURST;
1955 bytes_to_write -= playsize;
1957 // Fill buffer if needed:
1958 current_module="decode_audio";
1959 t = GetTimer();
1960 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
1961 if (mpctx->d_audio->eof) {
1962 audio_eof = 1;
1963 if (sh_audio->a_out_buffer_len == 0)
1964 return 0;
1966 t = GetTimer() - t;
1967 tt = t*0.000001f; audio_time_usage+=tt;
1968 if (playsize > sh_audio->a_out_buffer_len) {
1969 playsize = sh_audio->a_out_buffer_len;
1970 if (audio_eof)
1971 playflags |= AOPLAY_FINAL_CHUNK;
1973 if (!playsize)
1974 break;
1976 // play audio:
1977 current_module="play_audio";
1979 // Is this pts value actually useful for the aos that access it?
1980 // They're obviously badly broken in the way they handle av sync;
1981 // would not having access to this make them more broken?
1982 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1983 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
1985 if (playsize > 0) {
1986 sh_audio->a_out_buffer_len -= playsize;
1987 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
1988 sh_audio->a_out_buffer_len);
1989 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
1991 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
1992 // Sanity check to avoid hanging in case current ao doesn't output
1993 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
1994 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
1995 sh_audio->a_out_buffer_len = 0;
1998 return 1;
2001 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2002 float *aq_sleep_time)
2004 struct MPOpts *opts = &mpctx->opts;
2005 int frame_time_remaining = 0;
2006 current_module="calc_sleep_time";
2008 *time_frame -= get_relative_time(mpctx); // reset timer
2010 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2011 float delay = mpctx->audio_out->get_delay();
2012 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2014 if (autosync) {
2016 * Adjust this raw delay value by calculating the expected
2017 * delay for this frame and generating a new value which is
2018 * weighted between the two. The higher autosync is, the
2019 * closer to the delay value gets to that which "-nosound"
2020 * would have used, and the longer it will take for A/V
2021 * sync to settle at the right value (but it eventually will.)
2022 * This settling time is very short for values below 100.
2024 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2025 float difference = delay - predicted;
2026 delay = predicted + difference / (float)autosync;
2029 *time_frame = delay - mpctx->delay / opts->playback_speed;
2031 // delay = amount of audio buffered in soundcard/driver
2032 if (delay > 0.25) delay=0.25; else
2033 if (delay < 0.10) delay=0.10;
2034 if (*time_frame > delay*0.6) {
2035 // sleep time too big - may cause audio drops (buffer underrun)
2036 frame_time_remaining = 1;
2037 *time_frame = delay*0.5;
2039 } else {
2040 // If we're lagging more than 200 ms behind the right playback rate,
2041 // don't try to "catch up".
2042 // If benchmark is set always output frames as fast as possible
2043 // without sleeping.
2044 if (*time_frame < -0.2 || benchmark)
2045 *time_frame = 0;
2048 *aq_sleep_time += *time_frame;
2051 //============================== SLEEP: ===================================
2053 // flag 256 means: libvo driver does its timing (dvb card)
2054 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2055 *time_frame = timing_sleep(mpctx, *time_frame);
2056 return frame_time_remaining;
2059 int reinit_video_chain(struct MPContext *mpctx)
2061 struct MPOpts *opts = &mpctx->opts;
2062 sh_video_t * const sh_video = mpctx->sh_video;
2063 double ar=-1.0;
2064 //================== Init VIDEO (codec & libvo) ==========================
2065 if(opts->fixed_vo || !(mpctx->initialized_flags&INITIALIZED_VO)){
2066 current_module="preinit_libvo";
2068 //shouldn't we set dvideo->id=-2 when we fail?
2069 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2070 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2071 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
2072 goto err_out;
2074 mpctx->initialized_flags|=INITIALIZED_VO;
2077 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2078 mpctx->sh_video->stream_aspect = ar;
2079 current_module="init_video_filters";
2081 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2082 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2084 #ifdef CONFIG_MENU
2085 if(use_menu) {
2086 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2087 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2088 if(!vf_menu) {
2089 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2090 use_menu = 0;
2093 if(vf_menu)
2094 sh_video->vfilter = vf_menu;
2095 #endif
2097 #ifdef CONFIG_ASS
2098 if(ass_enabled) {
2099 int i;
2100 int insert = 1;
2101 if (opts->vf_settings)
2102 for (i = 0; opts->vf_settings[i].name; ++i)
2103 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2104 insert = 0;
2105 break;
2107 if (insert) {
2108 extern vf_info_t vf_info_ass;
2109 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2110 char* vf_arg[] = {"auto", "1", NULL};
2111 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2112 if (vf_ass)
2113 sh_video->vfilter = vf_ass;
2114 else
2115 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2118 #endif
2120 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2122 #ifdef CONFIG_ASS
2123 if (ass_enabled)
2124 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2125 #endif
2127 current_module="init_video_codec";
2129 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2130 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2131 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2133 if(!sh_video->initialized){
2134 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2135 goto err_out;
2138 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2140 if (sh_video->codec)
2141 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2143 sh_video->last_pts = MP_NOPTS_VALUE;
2144 sh_video->num_buffered_pts = 0;
2145 sh_video->next_frame_time = 0;
2147 if(auto_quality>0){
2148 // Auto quality option enabled
2149 output_quality=get_video_quality_max(sh_video);
2150 if(auto_quality>output_quality) auto_quality=output_quality;
2151 else output_quality=auto_quality;
2152 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2153 set_video_quality(sh_video,output_quality);
2156 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2158 current_module="init_vo";
2160 return 1;
2162 err_out:
2163 mpctx->sh_video = mpctx->d_video->sh = NULL;
2164 return 0;
2167 static double update_video_nocorrect_pts(struct MPContext *mpctx,
2168 int *blit_frame)
2170 struct sh_video *sh_video = mpctx->sh_video;
2171 *blit_frame = 0;
2172 double frame_time = 0;
2173 while (1) {
2174 current_module = "filter_video";
2175 // In nocorrect-pts mode there is no way to properly time these frames
2176 if (vf_output_queued_frame(sh_video->vfilter))
2177 break;
2178 unsigned char *packet = NULL;
2179 frame_time = sh_video->next_frame_time;
2180 if (mpctx->update_video_immediately)
2181 frame_time = 0;
2182 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2183 &packet, force_fps);
2184 if (in_size < 0) {
2185 #ifdef CONFIG_DVDNAV
2186 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2187 if (mp_dvdnav_is_eof(mpctx->stream))
2188 return -1;
2189 if (mpctx->d_video)
2190 mpctx->d_video->eof = 0;
2191 if (mpctx->d_audio)
2192 mpctx->d_audio->eof = 0;
2193 mpctx->stream->eof = 0;
2194 } else
2195 #endif
2196 return -1;
2198 if (in_size > max_framesize)
2199 max_framesize = in_size;
2200 sh_video->timer += frame_time;
2201 if (mpctx->sh_audio)
2202 mpctx->delay -= frame_time;
2203 // video_read_frame can change fps (e.g. for ASF video)
2204 vo_fps = sh_video->fps;
2205 int framedrop_type = check_framedrop(mpctx, frame_time);
2206 current_module = "decode video";
2208 void *decoded_frame;
2209 #ifdef CONFIG_DVDNAV
2210 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2211 if (in_size >= 0 && !decoded_frame)
2212 #endif
2213 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2214 sh_video->pts);
2215 #ifdef CONFIG_DVDNAV
2216 // Save last still frame for future display
2217 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2218 #endif
2219 if (decoded_frame) {
2220 // These updates are done here for vf_expand OSD/subtitles
2221 update_subtitles(sh_video, mpctx->d_sub, 0);
2222 update_teletext(sh_video, mpctx->demuxer, 0);
2223 update_osd_msg(mpctx);
2224 current_module = "filter video";
2225 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2226 break;
2229 *blit_frame = 1;
2230 return frame_time;
2233 static double update_video(struct MPContext *mpctx, int *blit_frame)
2235 struct sh_video *sh_video = mpctx->sh_video;
2236 *blit_frame = 0;
2237 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2238 mpctx->osd); // hack for vf_expand
2239 if (!mpctx->opts.correct_pts)
2240 return update_video_nocorrect_pts(mpctx, blit_frame);
2242 double pts;
2244 while (1) {
2245 current_module = "filter_video";
2246 // XXX Time used in this call is not counted in any performance
2247 // timer now, OSD time is not updated correctly for filter-added frames
2248 if (vf_output_queued_frame(sh_video->vfilter))
2249 break;
2250 unsigned char *packet = NULL;
2251 bool hit_eof = false;
2252 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2253 if (in_size < 0) {
2254 // try to extract last frames in case of decoder lag
2255 in_size = 0;
2256 pts = 1e300;
2257 hit_eof = true;
2259 if (in_size > max_framesize)
2260 max_framesize = in_size;
2261 current_module = "decode video";
2262 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2263 void *decoded_frame = decode_video(sh_video, packet, in_size,
2264 framedrop_type, pts);
2265 if (decoded_frame) {
2266 // These updates are done here for vf_expand OSD/subtitles
2267 update_subtitles(sh_video, mpctx->d_sub, 0);
2268 update_teletext(sh_video, mpctx->demuxer, 0);
2269 update_osd_msg(mpctx);
2270 current_module = "filter video";
2271 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2272 break;
2273 } else if (hit_eof)
2274 return -1;
2277 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_GET_PTS, &pts);
2278 if (pts == MP_NOPTS_VALUE) {
2279 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2280 // Try to use decoder pts from before filters
2281 pts = sh_video->pts;
2283 sh_video->pts = pts;
2284 if (sh_video->last_pts == MP_NOPTS_VALUE)
2285 sh_video->last_pts = sh_video->pts;
2286 else if (sh_video->last_pts > sh_video->pts) {
2287 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2288 sh_video->pts, sh_video->last_pts);
2289 /* If the difference in pts is small treat it as jitter around the
2290 * right value (possibly caused by incorrect timestamp ordering) and
2291 * just show this frame immediately after the last one.
2292 * Treat bigger differences as timestamp resets and start counting
2293 * timing of later frames from the position of this one. */
2294 if (sh_video->last_pts - sh_video->pts > 0.5)
2295 sh_video->last_pts = sh_video->pts;
2296 else
2297 sh_video->pts = sh_video->last_pts;
2299 double frame_time = sh_video->pts - sh_video->last_pts;
2300 sh_video->last_pts = sh_video->pts;
2301 sh_video->timer += frame_time;
2302 if (mpctx->sh_audio)
2303 mpctx->delay -= frame_time;
2304 *blit_frame = 1;
2305 return frame_time;
2308 void pause_player(struct MPContext *mpctx)
2310 if (mpctx->paused)
2311 return;
2312 mpctx->paused = 1;
2313 mpctx->step_frames = 0;
2314 mpctx->time_frame -= get_relative_time(mpctx);
2316 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2317 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2319 if (mpctx->audio_out && mpctx->sh_audio)
2320 mpctx->audio_out->pause(); // pause audio, keep data if possible
2323 void unpause_player(struct MPContext *mpctx)
2325 if (!mpctx->paused)
2326 return;
2327 mpctx->paused = 0;
2329 if (mpctx->audio_out && mpctx->sh_audio)
2330 mpctx->audio_out->resume(); // resume audio
2331 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2332 && !mpctx->step_frames)
2333 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2334 (void)get_relative_time(mpctx); // ignore time that passed during pause
2337 void add_step_frame(struct MPContext *mpctx)
2339 mpctx->step_frames++;
2340 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2341 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2342 unpause_player(mpctx);
2345 static void pause_loop(struct MPContext *mpctx)
2347 mp_cmd_t* cmd;
2348 if (!quiet) {
2349 // Small hack to display the pause message on the OSD line.
2350 // The pause string is: "\n == PAUSE == \r" so we need to
2351 // take the first and the last char out
2352 if (term_osd && !mpctx->sh_video) {
2353 char msg[128] = MSGTR_Paused;
2354 int mlen = strlen(msg);
2355 msg[mlen-1] = '\0';
2356 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2357 update_osd_msg(mpctx);
2358 } else
2359 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2360 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2362 #ifdef CONFIG_GUI
2363 if (use_gui)
2364 guiGetEvent(guiCEvent, (char *)guiSetPause);
2365 #endif
2367 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2368 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2369 if (cmd) {
2370 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2371 run_command(mpctx, cmd);
2372 mp_cmd_free(cmd);
2373 continue;
2375 if (mpctx->sh_video && mpctx->video_out)
2376 vo_check_events(mpctx->video_out);
2377 #ifdef CONFIG_GUI
2378 if (use_gui) {
2379 guiEventHandling();
2380 guiGetEvent(guiReDraw, NULL);
2381 if (guiIntfStruct.Playing!=2 || (mpctx->rel_seek_secs || mpctx->abs_seek_pos))
2382 break;
2384 #endif
2385 #ifdef CONFIG_MENU
2386 if (vf_menu)
2387 vf_menu_pause_update(vf_menu);
2388 #endif
2389 usec_sleep(20000);
2390 update_osd_msg(mpctx);
2391 int hack = vo_osd_changed(0);
2392 vo_osd_changed(hack);
2393 if (hack)
2394 break;
2396 #ifdef CONFIG_GUI
2397 if (use_gui) {
2398 if (guiIntfStruct.Playing == guiSetStop)
2399 mpctx->eof = 1;
2400 else
2401 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2403 #endif
2407 // Find the right mute status and record position for new file position
2408 static void edl_seek_reset(MPContext *mpctx)
2410 mpctx->edl_muted = 0;
2411 next_edl_record = edl_records;
2413 while (next_edl_record) {
2414 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2415 break;
2417 if (next_edl_record->action == EDL_MUTE)
2418 mpctx->edl_muted = !mpctx->edl_muted;
2419 next_edl_record = next_edl_record->next;
2421 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2422 mixer_mute(&mpctx->mixer);
2426 // Execute EDL command for the current position if one exists
2427 static void edl_update(MPContext *mpctx)
2429 if (!next_edl_record)
2430 return;
2432 if (!mpctx->sh_video) {
2433 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2434 free_edl(edl_records);
2435 next_edl_record = NULL;
2436 edl_records = NULL;
2437 return;
2440 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2441 if (next_edl_record->action == EDL_SKIP) {
2442 mpctx->osd_function = OSD_FFW;
2443 mpctx->abs_seek_pos = 0;
2444 mpctx->rel_seek_secs = next_edl_record->length_sec;
2445 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2446 "[%f], length [%f]\n", next_edl_record->start_sec,
2447 next_edl_record->stop_sec, next_edl_record->length_sec);
2448 edl_decision = 1;
2450 else if (next_edl_record->action == EDL_MUTE) {
2451 mpctx->edl_muted = !mpctx->edl_muted;
2452 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2453 mixer_mute(&mpctx->mixer);
2454 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2455 next_edl_record->start_sec );
2457 next_edl_record = next_edl_record->next;
2462 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2463 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2464 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2465 static int seek(MPContext *mpctx, double amount, int style)
2467 current_module = "seek";
2468 if (demux_seek(mpctx->demuxer, amount, audio_delay, style) == 0)
2469 return -1;
2471 if (mpctx->sh_video) {
2472 current_module = "seek_video_reset";
2473 resync_video_stream(mpctx->sh_video);
2474 if (mpctx->video_out->config_ok)
2475 vo_control(mpctx->video_out, VOCTRL_RESET, NULL);
2476 mpctx->sh_video->num_buffered_pts = 0;
2477 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2478 mpctx->num_buffered_frames = 0;
2479 mpctx->delay = 0;
2480 mpctx->time_frame = 0;
2481 mpctx->update_video_immediately = true;
2482 // Not all demuxers set d_video->pts during seek, so this value
2483 // (which is used by at least vobsub and edl code below) may
2484 // be completely wrong (probably 0).
2485 mpctx->sh_video->pts = mpctx->d_video->pts;
2486 update_subtitles(mpctx->sh_video, mpctx->d_sub, 1);
2487 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2490 if (mpctx->sh_audio) {
2491 current_module = "seek_audio_reset";
2492 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2493 mpctx->sh_audio->a_buffer_len = 0;
2494 mpctx->sh_audio->a_out_buffer_len = 0;
2497 if (vo_vobsub && mpctx->sh_video) {
2498 current_module = "seek_vobsub_reset";
2499 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2502 edl_seek_reset(mpctx);
2504 mpctx->total_avsync_change = 0;
2505 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2506 drop_frame_cnt = 0;
2508 current_module = NULL;
2509 return 0;
2513 static int read_keys(void *ctx, int fd)
2515 getch2(ctx);
2516 return mplayer_get_key(ctx, 0);
2520 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2521 * file for some tools to link against. */
2522 #ifndef DISABLE_MAIN
2523 int main(int argc,char* argv[]){
2526 char * mem_ptr;
2528 // movie info:
2530 /* Flag indicating whether MPlayer should exit without playing anything. */
2531 int opt_exit = 0;
2533 //float a_frame=0; // Audio
2535 int i;
2537 int gui_no_filename=0;
2539 struct MPContext *mpctx = &(struct MPContext){
2540 .osd_function = OSD_PLAY,
2541 .begin_skip = MP_NOPTS_VALUE,
2542 .play_tree_step = 1,
2543 .global_sub_pos = -1,
2544 .set_of_sub_pos = -1,
2545 .file_format = DEMUXER_TYPE_UNKNOWN,
2546 .last_dvb_step = 1,
2549 InitTimer();
2550 srand(GetTimerMS());
2552 mp_msg_init();
2554 #ifdef CONFIG_X11
2555 mpctx->x11_state = vo_x11_init_state();
2556 #endif
2557 struct MPOpts *opts = &mpctx->opts;
2558 set_default_mplayer_options(opts);
2559 // Create the config context and register the options
2560 mpctx->mconfig = m_config_new(opts, cfg_include);
2561 m_config_register_options(mpctx->mconfig,mplayer_opts);
2562 mp_input_register_options(mpctx->mconfig);
2564 // Preparse the command line
2565 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2567 print_version("MPlayer");
2568 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2569 set_path_env();
2570 #endif
2572 #ifdef CONFIG_TV
2573 stream_tv_defaults.immediate = 1;
2574 #endif
2576 if (argc > 1 && argv[1] &&
2577 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2578 use_gui = !strcmp(argv[1], "-gui");
2579 } else
2580 if ( argv[0] )
2582 char *base = strrchr(argv[0], '/');
2583 if (!base)
2584 base = strrchr(argv[0], '\\');
2585 if (!base)
2586 base = argv[0];
2587 if(strstr(base, "gmplayer"))
2588 use_gui=1;
2591 parse_cfgfiles(mpctx, mpctx->mconfig);
2593 #ifdef CONFIG_GUI
2594 if ( use_gui ) cfg_read();
2595 #endif
2597 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2598 if(mpctx->playtree == NULL)
2599 opt_exit = 1;
2600 else {
2601 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2602 if(mpctx->playtree) {
2603 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2604 if(mpctx->playtree_iter) {
2605 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2606 play_tree_iter_free(mpctx->playtree_iter);
2607 mpctx->playtree_iter = NULL;
2609 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2613 mpctx->key_fifo = mp_fifo_create(opts);
2615 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_GUI)
2616 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2617 if(runningmplayer && mpctx->filename && use_gui){
2618 COPYDATASTRUCT csData;
2619 char file[MAX_PATH];
2620 char *filepart = mpctx->filename;
2621 if(GetFullPathName(mpctx->filename, MAX_PATH, file, &filepart)){
2622 csData.dwData = 0;
2623 csData.cbData = strlen(file)*2;
2624 csData.lpData = file;
2625 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2628 #endif
2630 #if defined(__MINGW32__) || defined(__CYGWIN__)
2631 // request 1ms timer resolution
2632 timeBeginPeriod(1);
2633 if(proc_priority){
2634 int i;
2635 for(i=0; priority_presets_defs[i].name; i++){
2636 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
2637 break;
2639 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_SettingProcessPriority,
2640 priority_presets_defs[i].name);
2641 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
2643 #endif
2644 #ifndef CONFIG_GUI
2645 if(use_gui){
2646 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2647 use_gui=0;
2649 #else
2650 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
2651 if(use_gui && !vo_init()){
2652 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2653 use_gui=0;
2655 #endif
2656 if (use_gui && mpctx->playtree_iter){
2657 char cwd[PATH_MAX+2];
2658 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2659 play_tree_iter_free(mpctx->playtree_iter);
2660 mpctx->playtree_iter=NULL;
2662 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2664 strcat(cwd, "/");
2665 // Prefix relative paths with current working directory
2666 play_tree_add_bpf(mpctx->playtree, cwd);
2668 // Import initital playtree into GUI.
2669 import_initial_playtree_into_gui(mpctx->playtree, mpctx->mconfig, enqueue);
2671 #endif /* CONFIG_GUI */
2673 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
2674 list_video_out();
2675 opt_exit = 1;
2678 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
2679 list_audio_out();
2680 opt_exit = 1;
2683 /* Check codecs.conf. */
2684 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2685 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2686 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2687 if(!parse_codec_cfg(NULL)){
2688 exit_player_with_rc(mpctx, EXIT_NONE, 0);
2690 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
2693 free( mem_ptr ); // release the buffer created by get_path()
2696 #if 0
2697 if(video_codec_list){
2698 int i;
2699 video_codec=video_codec_list[0];
2700 for(i=0;video_codec_list[i];i++)
2701 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2703 #endif
2704 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2705 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
2706 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2707 list_codecs(1);
2708 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2709 opt_exit = 1;
2711 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
2712 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
2713 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
2714 list_codecs(0);
2715 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2716 opt_exit = 1;
2718 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
2719 vfm_help();
2720 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2721 opt_exit = 1;
2723 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
2724 afm_help();
2725 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2726 opt_exit = 1;
2728 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
2729 af_help();
2730 printf("\n");
2731 opt_exit = 1;
2733 #ifdef CONFIG_X11
2734 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
2735 fstype_help();
2736 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
2737 opt_exit = 1;
2739 #endif
2740 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
2741 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
2742 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
2743 demuxer_help();
2744 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2745 opt_exit = 1;
2747 if(list_properties) {
2748 property_print_help();
2749 opt_exit = 1;
2752 if(opt_exit)
2753 exit_player(mpctx, EXIT_NONE);
2755 if (player_idle_mode && use_gui) {
2756 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
2757 exit_player_with_rc(mpctx, EXIT_NONE, 1);
2760 if(!mpctx->filename && !player_idle_mode){
2761 if(!use_gui){
2762 // no file/vcd/dvd -> show HELP:
2763 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
2764 exit_player_with_rc(mpctx, EXIT_NONE, 0);
2765 } else gui_no_filename=1;
2768 /* Display what configure line was used */
2769 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
2771 // Many users forget to include command line in bugreports...
2772 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
2773 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
2774 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
2775 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
2778 //------ load global data first ------
2780 mpctx->osd = osd_create();
2782 // check font
2783 #ifdef CONFIG_FREETYPE
2784 init_freetype();
2785 #endif
2786 #ifdef CONFIG_FONTCONFIG
2787 if(font_fontconfig <= 0)
2789 #endif
2790 #ifdef CONFIG_BITMAP_FONT
2791 if(font_name){
2792 vo_font=read_font_desc(font_name,font_factor,verbose>1);
2793 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
2794 filename_recode(font_name));
2795 } else {
2796 // try default:
2797 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
2798 free(mem_ptr); // release the buffer created by get_path()
2799 if(!vo_font)
2800 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
2802 if (sub_font_name)
2803 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
2804 else
2805 mpctx->osd->sub_font = vo_font;
2806 #endif
2807 #ifdef CONFIG_FONTCONFIG
2809 #endif
2811 #ifdef CONFIG_ASS
2812 ass_library = ass_init();
2813 #endif
2815 #ifdef HAVE_RTC
2816 if(!nortc)
2818 // seteuid(0); /* Can't hurt to try to get root here */
2819 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
2820 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
2821 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
2822 else {
2823 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
2825 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
2826 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
2827 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
2828 close (rtc_fd);
2829 rtc_fd = -1;
2830 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
2831 /* variable only by the root */
2832 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
2833 close (rtc_fd);
2834 rtc_fd = -1;
2835 } else
2836 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
2839 #ifdef CONFIG_GUI
2840 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
2841 // and now ? -- Pontscho
2842 if(use_gui) setuid( getuid() ); // strongly test, please check this.
2843 #endif
2844 if(rtc_fd<0)
2845 #endif /* HAVE_RTC */
2846 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
2847 softsleep?"software":timer_name);
2849 #ifdef HAVE_TERMCAP
2850 if ( !use_gui ) load_termcap(NULL); // load key-codes
2851 #endif
2853 // ========== Init keyboard FIFO (connection to libvo) ============
2855 // Init input system
2856 current_module = "init_input";
2857 mpctx->input = mp_input_init(&opts->input, use_gui);
2858 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
2859 if(slave_mode)
2860 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
2861 else if(!noconsolecontrols)
2862 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
2863 // Set the libstream interrupt callback
2864 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
2866 #ifdef CONFIG_MENU
2867 if(use_menu) {
2868 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2869 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2870 else {
2871 menu_cfg = get_path("menu.conf");
2872 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
2873 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
2874 else {
2875 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
2876 MPLAYER_CONFDIR "/menu.conf"))
2877 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
2878 else {
2879 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
2880 use_menu = 0;
2885 #endif
2887 current_module = NULL;
2889 /// Catch signals
2890 #ifndef __MINGW32__
2891 signal(SIGCHLD,child_sighandler);
2892 #endif
2894 #ifdef CONFIG_CRASH_DEBUG
2895 prog_path = argv[0];
2896 #endif
2897 //========= Catch terminate signals: ================
2898 // terminate requests:
2899 signal(SIGTERM,exit_sighandler); // kill
2900 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
2902 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
2904 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
2905 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
2906 #ifdef CONFIG_SIGHANDLER
2907 // fatal errors:
2908 signal(SIGBUS,exit_sighandler); // bus error
2909 signal(SIGSEGV,exit_sighandler); // segfault
2910 signal(SIGILL,exit_sighandler); // illegal instruction
2911 signal(SIGFPE,exit_sighandler); // floating point exc.
2912 signal(SIGABRT,exit_sighandler); // abort()
2913 #ifdef CONFIG_CRASH_DEBUG
2914 if (crash_debug)
2915 signal(SIGTRAP,exit_sighandler);
2916 #endif
2917 #endif
2919 #ifdef CONFIG_GUI
2920 if(use_gui){
2921 guiInit();
2922 guiGetEvent(guiSetContext, mpctx);
2923 mpctx->initialized_flags|=INITIALIZED_GUI;
2924 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
2926 #endif
2928 // ******************* Now, let's see the per-file stuff ********************
2930 play_next_file:
2932 // init global sub numbers
2933 mpctx->global_sub_size = 0;
2934 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
2936 if (mpctx->filename) {
2937 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
2938 load_per_extension_config (mpctx->mconfig, mpctx->filename);
2939 load_per_file_config (mpctx->mconfig, mpctx->filename);
2942 if (opts->video_driver_list)
2943 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
2944 if (opts->audio_driver_list)
2945 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
2947 // We must enable getch2 here to be able to interrupt network connection
2948 // or cache filling
2949 if(!noconsolecontrols && !slave_mode){
2950 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
2951 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
2952 else
2953 getch2_enable(); // prepare stdin for hotkeys...
2954 mpctx->initialized_flags|=INITIALIZED_GETCH2;
2955 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
2958 // =================== GUI idle loop (STOP state) ===========================
2959 #ifdef CONFIG_GUI
2960 if ( use_gui ) {
2961 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
2962 guiGetEvent( guiSetDefaults,0 );
2963 while ( guiIntfStruct.Playing != 1 )
2965 mp_cmd_t* cmd;
2966 usec_sleep(20000);
2967 guiEventHandling();
2968 guiGetEvent( guiReDraw,NULL );
2969 if ( (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
2970 guiGetEvent(guiIEvent, (char *)cmd->id);
2971 mp_cmd_free(cmd);
2974 guiGetEvent( guiSetParameters,NULL );
2975 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
2977 play_tree_t * entry = play_tree_new();
2978 play_tree_add_file( entry,guiIntfStruct.Filename );
2979 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
2980 else mpctx->playtree=play_tree_new();
2981 play_tree_set_child( mpctx->playtree,entry );
2982 if(mpctx->playtree)
2984 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2985 if(mpctx->playtree_iter)
2987 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
2989 play_tree_iter_free(mpctx->playtree_iter);
2990 mpctx->playtree_iter = NULL;
2992 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2997 #endif /* CONFIG_GUI */
2999 while (player_idle_mode && !mpctx->filename) {
3000 play_tree_t * entry = NULL;
3001 mp_cmd_t * cmd;
3002 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3003 if (mpctx->video_out)
3004 vo_check_events(mpctx->video_out);
3005 usec_sleep(20000);
3007 switch (cmd->id) {
3008 case MP_CMD_LOADFILE:
3009 // prepare a tree entry with the new filename
3010 entry = play_tree_new();
3011 play_tree_add_file(entry, cmd->args[0].v.s);
3012 // The entry is added to the main playtree after the switch().
3013 break;
3014 case MP_CMD_LOADLIST:
3015 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3016 break;
3017 case MP_CMD_QUIT:
3018 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3019 break;
3020 case MP_CMD_GET_PROPERTY:
3021 case MP_CMD_SET_PROPERTY:
3022 case MP_CMD_STEP_PROPERTY:
3023 run_command(mpctx, cmd);
3024 break;
3027 mp_cmd_free(cmd);
3029 if (entry) { // user entered a command that gave a valid entry
3030 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3031 play_tree_free_list(mpctx->playtree->child, 1);
3032 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3034 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3036 play_tree_set_child(mpctx->playtree, entry);
3038 /* Make iterator start at the top the of tree. */
3039 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3040 if (!mpctx->playtree_iter) continue;
3042 // find the first real item in the tree
3043 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3044 // no items!
3045 play_tree_iter_free(mpctx->playtree_iter);
3046 mpctx->playtree_iter = NULL;
3047 continue; // wait for next command
3049 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3052 //---------------------------------------------------------------------------
3054 if(mpctx->filename)
3055 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
3056 filename_recode(mpctx->filename));
3058 if (edl_filename) {
3059 if (edl_records) free_edl(edl_records);
3060 next_edl_record = edl_records = edl_parse_file();
3062 if (edl_output_filename) {
3063 if (edl_fd) fclose(edl_fd);
3064 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3066 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
3067 filename_recode(edl_output_filename));
3071 //==================== Open VOB-Sub ============================
3073 current_module="vobsub";
3074 if (vobsub_name){
3075 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3076 if(vo_vobsub==NULL)
3077 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
3078 filename_recode(vobsub_name));
3079 } else if (sub_auto && mpctx->filename){
3080 /* try to autodetect vobsub from movie filename ::atmos */
3081 char *buf = strdup(mpctx->filename), *psub;
3082 char *pdot = strrchr(buf, '.');
3083 char *pslash = strrchr(buf, '/');
3084 #if defined(__MINGW32__) || defined(__CYGWIN__)
3085 if (!pslash) pslash = strrchr(buf, '\\');
3086 #endif
3087 if (pdot && (!pslash || pdot > pslash))
3088 *pdot = '\0';
3089 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3090 /* try from ~/.mplayer/sub */
3091 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3092 char *bname;
3093 int l;
3094 bname = strrchr(buf,'/');
3095 #if defined(__MINGW32__) || defined(__CYGWIN__)
3096 if(!bname) bname = strrchr(buf,'\\');
3097 #endif
3098 if(bname) bname++;
3099 else bname = buf;
3100 l = strlen(psub) + strlen(bname) + 1;
3101 psub = realloc(psub,l);
3102 strcat(psub,bname);
3103 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3104 free(psub);
3106 free(buf);
3108 if(vo_vobsub){
3109 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3110 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3112 // setup global sub numbering
3113 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3114 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3117 //============ Open & Sync STREAM --- fork cache2 ====================
3119 mpctx->stream=NULL;
3120 mpctx->demuxer=NULL;
3121 if (mpctx->d_audio) {
3122 //free_demuxer_stream(mpctx->d_audio);
3123 mpctx->d_audio=NULL;
3125 if (mpctx->d_video) {
3126 //free_demuxer_stream(d_video);
3127 mpctx->d_video=NULL;
3129 mpctx->sh_audio=NULL;
3130 mpctx->sh_video=NULL;
3132 current_module="open_stream";
3133 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3134 if(!mpctx->stream) { // error...
3135 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3136 goto goto_next_file;
3138 mpctx->initialized_flags|=INITIALIZED_STREAM;
3140 #ifdef CONFIG_GUI
3141 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3142 #endif
3144 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3145 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3146 "playlist support will not be used automatically.\n"
3147 "MPlayer's playlist code is unsafe and should only be used with "
3148 "trusted sources.\nPlayback will probably fail.\n\n");
3149 #if 0
3150 play_tree_t* entry;
3151 // Handle playlist
3152 current_module="handle_playlist";
3153 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3154 filename_recode(mpctx->filename));
3155 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3156 mpctx->eof=playtree_add_playlist(mpctx, entry);
3157 goto goto_next_file;
3158 #endif
3160 mpctx->stream->start_pos+=seek_to_byte;
3162 if(stream_dump_type==5){
3163 unsigned char buf[4096];
3164 int len;
3165 FILE *f;
3166 current_module="dumpstream";
3167 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3168 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
3169 exit_player(mpctx, EXIT_ERROR);
3171 stream_reset(mpctx->stream);
3172 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3173 f=fopen(stream_dump_name,"wb");
3174 if(!f){
3175 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3176 exit_player(mpctx, EXIT_ERROR);
3178 if (dvd_chapter > 1) {
3179 int chapter = dvd_chapter - 1;
3180 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3182 while(!mpctx->stream->eof && !async_quit_request){
3183 len=stream_read(mpctx->stream,buf,4096);
3184 if(len>0) {
3185 if(fwrite(buf,len,1,f) != 1) {
3186 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3187 exit_player(mpctx, EXIT_ERROR);
3190 if(dvd_last_chapter > 0) {
3191 int chapter = -1;
3192 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3193 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3194 break;
3197 if(fclose(f)) {
3198 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3199 exit_player(mpctx, EXIT_ERROR);
3201 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3202 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3205 #ifdef CONFIG_DVDREAD
3206 if(mpctx->stream->type==STREAMTYPE_DVD){
3207 current_module="dvd lang->id";
3208 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3209 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3210 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3211 // setup global sub numbering
3212 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3213 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3214 current_module=NULL;
3216 #endif
3218 #ifdef CONFIG_DVDNAV
3219 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3220 current_module="dvdnav lang->id";
3221 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3222 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3223 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3224 // setup global sub numbering
3225 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3226 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3227 current_module=NULL;
3229 #endif
3231 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3232 goto_enable_cache:
3233 if(stream_cache_size>0){
3234 current_module="enable_cache";
3235 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3236 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3237 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3238 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3241 //============ Open DEMUXERS --- DETECT file type =======================
3242 current_module="demux_open";
3244 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3246 // HACK to get MOV Reference Files working
3248 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3250 unsigned char* playlist_entry;
3251 play_tree_t *list = NULL, *entry = NULL;
3253 current_module="handle_demux_playlist";
3254 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3256 char *temp, *bname;
3258 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3259 filename_recode(playlist_entry));
3261 bname=mp_basename(playlist_entry);
3262 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3263 continue;
3265 if (!strncmp(bname,mp_basename(mpctx->filename),strlen(bname))) // ignoring self-reference
3266 continue;
3268 entry = play_tree_new();
3270 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3272 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3273 if (temp)
3275 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3276 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3277 strcat(temp, playlist_entry);
3278 play_tree_add_file(entry,temp);
3279 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3280 free(temp);
3283 else
3284 play_tree_add_file(entry,playlist_entry);
3286 if(!list)
3287 list = entry;
3288 else
3289 play_tree_append_entry(list,entry);
3291 free_demuxer(mpctx->demuxer);
3292 mpctx->demuxer = NULL;
3294 if (list)
3296 entry = play_tree_new();
3297 play_tree_set_child(entry,list);
3298 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3299 goto goto_next_file;
3303 if(!mpctx->demuxer)
3304 goto goto_next_file;
3305 if(dvd_chapter>1) {
3306 float pts;
3307 if (demuxer_seek_chapter(mpctx->demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
3308 seek(mpctx, pts, SEEK_ABSOLUTE);
3311 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3313 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3314 int i;
3315 int maxid = -1;
3316 // setup global sub numbering
3317 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3318 for (i = 0; i < MAX_S_STREAMS; i++)
3319 if (mpctx->demuxer->s_streams[i])
3320 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3321 mpctx->global_sub_size += maxid + 1;
3323 // Make opts->sub_id always selectable if set.
3324 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3325 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3327 #ifdef CONFIG_ASS
3328 if (ass_enabled && ass_library) {
3329 for (i = 0; i < mpctx->demuxer->num_attachments; ++i) {
3330 demux_attachment_t* att = mpctx->demuxer->attachments + i;
3331 if (extract_embedded_fonts &&
3332 att->name && att->type && att->data && att->data_size &&
3333 (strcmp(att->type, "application/x-truetype-font") == 0 ||
3334 strcmp(att->type, "application/x-font") == 0))
3335 ass_add_font(ass_library, att->name, att->data, att->data_size);
3338 #endif
3340 current_module="demux_open2";
3342 //file_format=demuxer->file_format;
3344 mpctx->d_audio=mpctx->demuxer->audio;
3345 mpctx->d_video=mpctx->demuxer->video;
3346 mpctx->d_sub=mpctx->demuxer->sub;
3348 // select audio stream
3349 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3351 // DUMP STREAMS:
3352 if((stream_dump_type)&&(stream_dump_type<4)){
3353 FILE *f;
3354 demux_stream_t *ds=NULL;
3355 current_module="dump";
3356 // select stream to dump
3357 switch(stream_dump_type){
3358 case 1: ds=mpctx->d_audio;break;
3359 case 2: ds=mpctx->d_video;break;
3360 case 3: ds=mpctx->d_sub;break;
3362 if(!ds){
3363 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3364 exit_player(mpctx, EXIT_ERROR);
3366 // disable other streams:
3367 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3368 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3369 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3370 // let's dump it!
3371 f=fopen(stream_dump_name,"wb");
3372 if(!f){
3373 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3374 exit_player(mpctx, EXIT_ERROR);
3376 while(!ds->eof){
3377 unsigned char* start;
3378 int in_size=ds_get_packet(ds,&start);
3379 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3380 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3381 if(in_size>0) fwrite(start,in_size,1,f);
3382 if(dvd_last_chapter>0) {
3383 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3384 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3385 break;
3388 fclose(f);
3389 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3390 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3393 mpctx->sh_audio=mpctx->d_audio->sh;
3394 mpctx->sh_video=mpctx->d_video->sh;
3396 if(mpctx->sh_video){
3398 current_module="video_read_properties";
3399 if(!video_read_properties(mpctx->sh_video)) {
3400 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3401 mpctx->sh_video=mpctx->d_video->sh=NULL;
3402 } else {
3403 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_FilefmtFourccSizeFpsFtime,
3404 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3405 mpctx->sh_video->fps,mpctx->sh_video->frametime
3408 /* need to set fps here for output encoders to pick it up in their init */
3409 if(force_fps){
3410 mpctx->sh_video->fps=force_fps;
3411 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3413 vo_fps = mpctx->sh_video->fps;
3415 if(!mpctx->sh_video->fps && !force_fps){
3416 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3417 mpctx->sh_video=mpctx->d_video->sh=NULL;
3423 if(!mpctx->sh_video && !mpctx->sh_audio){
3424 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3425 #ifdef CONFIG_DVBIN
3426 if(mpctx->stream->type == STREAMTYPE_DVB)
3428 int dir;
3429 int v = mpctx->last_dvb_step;
3430 if(v > 0)
3431 dir = DVB_CHANNEL_HIGHER;
3432 else
3433 dir = DVB_CHANNEL_LOWER;
3435 if(dvb_step_channel(mpctx->stream, dir)) {
3436 mpctx->stop_play = PT_NEXT_ENTRY;
3437 mpctx->dvbin_reopen = 1;
3440 #endif
3441 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3444 /* display clip info */
3445 demux_info_print(mpctx->demuxer);
3447 //================== Read SUBTITLES (DVD & TEXT) ==========================
3448 if(vo_spudec==NULL && mpctx->sh_video &&
3449 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3450 init_vo_spudec(mpctx);
3453 if(mpctx->sh_video) {
3454 // after reading video params we should load subtitles because
3455 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3456 // check .sub
3457 current_module="read_subtitles_file";
3458 if(sub_name){
3459 for (i = 0; sub_name[i] != NULL; ++i)
3460 add_subtitles(mpctx, sub_name[i], mpctx->sh_video->fps, 0);
3462 if(sub_auto) { // auto load sub file ...
3463 char *psub = get_path( "sub/" );
3464 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3465 int i = 0;
3466 free(psub); // release the buffer created by get_path() above
3467 while (tmp[i]) {
3468 add_subtitles(mpctx, tmp[i], mpctx->sh_video->fps, 1);
3469 free(tmp[i++]);
3471 free(tmp);
3473 if (mpctx->set_of_sub_size > 0) {
3474 // setup global sub numbering
3475 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3476 mpctx->global_sub_size += mpctx->set_of_sub_size;
3480 if (mpctx->global_sub_size) {
3481 // find the best sub to use
3482 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3483 mpctx->global_sub_pos = -1; // no subs by default
3484 if (vobsub_index_id >= 0) {
3485 // if user asks for a vobsub id, use that first.
3486 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3487 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3488 // if user asks for a dvd sub id, use that next.
3489 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3490 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3491 // if there are text subs to use, use those. (autosubs come last here)
3492 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3493 } else if (opts->sub_id < 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3494 // finally select subs by language and container hints
3495 if (opts->sub_id < 0 && dvdsub_lang)
3496 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3497 if (opts->sub_id < 0)
3498 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3499 if (opts->sub_id >= 0)
3500 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3502 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3503 mpctx->global_sub_pos--;
3504 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3505 if(subdata)
3506 switch (stream_dump_type) {
3507 case 3: list_sub_file(subdata); break;
3508 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3509 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3510 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3511 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3512 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3516 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3517 filename_recode(mpctx->filename));
3518 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3519 if (mpctx->sh_video) {
3520 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3521 if (mpctx->sh_video->format >= 0x20202020)
3522 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3523 else
3524 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3525 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3526 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3527 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3528 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3529 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3531 if (mpctx->sh_audio) {
3532 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3533 if (mpctx->sh_audio->format >= 0x20202020)
3534 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3535 else
3536 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3537 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3538 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3539 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3541 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3542 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n", mpctx->stream->seek ? 1 : 0);
3543 if (mpctx->demuxer) {
3544 if (mpctx->demuxer->num_chapters == 0)
3545 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3546 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3549 if(!mpctx->sh_video) goto main; // audio-only
3551 if(!reinit_video_chain(mpctx)) {
3552 if(!mpctx->sh_video){
3553 if(!mpctx->sh_audio) goto goto_next_file;
3554 goto main; // exit_player(MSGTR_Exit_error);
3558 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3559 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3561 #ifdef CONFIG_FREETYPE
3562 force_load_font = 1;
3563 #endif
3565 //================== MAIN: ==========================
3566 main:
3567 current_module="main";
3569 if(playing_msg) {
3570 char* msg = property_expand_string(mpctx, playing_msg);
3571 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3572 free(msg);
3576 // Disable the term OSD in verbose mode
3577 if(verbose) term_osd = 0;
3580 //int frame_corr_num=0; //
3581 //float v_frame=0; // Video
3582 //float num_frames=0; // number of frames played
3584 int frame_time_remaining=0; // flag
3585 int blit_frame=0;
3586 mpctx->num_buffered_frames=0;
3588 // Make sure old OSD does not stay around,
3589 // e.g. with -fixed-vo and same-resolution files
3590 clear_osd_msgs();
3591 update_osd_msg(mpctx);
3593 //================ SETUP AUDIO ==========================
3595 if(mpctx->sh_audio){
3596 reinit_audio_chain(mpctx);
3597 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3598 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3601 current_module="av_init";
3603 if(mpctx->sh_video){
3604 mpctx->sh_video->timer=0;
3605 if (! ignore_start)
3606 audio_delay += mpctx->sh_video->stream_delay;
3608 if(mpctx->sh_audio){
3609 if (start_volume >= 0)
3610 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3611 if (! ignore_start)
3612 audio_delay -= mpctx->sh_audio->stream_delay;
3613 mpctx->delay=-audio_delay;
3616 if(!mpctx->sh_audio){
3617 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3618 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3619 ds_free_packs(mpctx->d_audio); // free buffered chunks
3620 //mpctx->d_audio->id=-2; // do not read audio chunks
3621 //uninit_player(mpctx, INITIALIZED_AO); // close device
3623 if(!mpctx->sh_video){
3624 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3625 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3626 ds_free_packs(mpctx->d_video);
3627 mpctx->d_video->id=-2;
3628 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3631 if (!mpctx->sh_video && !mpctx->sh_audio)
3632 goto goto_next_file;
3634 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3635 if(force_fps && mpctx->sh_video){
3636 vo_fps = mpctx->sh_video->fps=force_fps;
3637 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3638 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3641 #ifdef CONFIG_GUI
3642 if ( use_gui ) {
3643 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3644 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3645 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3646 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3647 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3649 #endif
3651 mp_input_set_section(mpctx->input, NULL);
3652 //TODO: add desired (stream-based) sections here
3653 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3654 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3656 //==================== START PLAYING =======================
3658 if(opts->loop_times>1) opts->loop_times--; else
3659 if(opts->loop_times==1) opts->loop_times = -1;
3661 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
3663 total_time_usage_start=GetTimer();
3664 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3665 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3666 play_n_frames=play_n_frames_mf;
3668 if(play_n_frames==0){
3669 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
3672 if (seek_to_sec) {
3673 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3674 end_at.pos += seek_to_sec;
3677 if (end_at.type == END_AT_SIZE) {
3678 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
3679 end_at.type = END_AT_NONE;
3682 #ifdef CONFIG_DVDNAV
3683 mp_dvdnav_context_free(mpctx);
3684 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3685 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3686 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3688 #endif
3690 get_relative_time(mpctx); // reset current delta
3691 mpctx->time_frame = 0;
3692 mpctx->drop_message_shown = 0;
3693 mpctx->update_video_immediately = true;
3694 mpctx->total_avsync_change = 0;
3696 while(!mpctx->stop_play){
3697 float aq_sleep_time=0;
3699 if(dvd_last_chapter>0) {
3700 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3701 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3702 goto goto_next_file;
3705 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3706 mpctx->sh_audio = mpctx->d_audio->sh;
3707 mpctx->sh_audio->ds = mpctx->d_audio;
3708 reinit_audio_chain(mpctx);
3711 /*========================== PLAY AUDIO ============================*/
3713 if (mpctx->sh_audio && !mpctx->paused)
3714 if (!fill_audio_out_buffers(mpctx))
3715 // at eof, all audio at least written to ao
3716 if (!mpctx->sh_video)
3717 mpctx->stop_play = AT_END_OF_FILE;
3720 if(!mpctx->sh_video) {
3721 // handle audio-only case:
3722 double a_pos=0;
3723 // sh_audio can be NULL due to video stream switching
3724 // TODO: handle this better
3725 if (mpctx->sh_audio)
3726 a_pos = playing_audio_pts(mpctx);
3728 print_status(mpctx, a_pos, false);
3730 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3731 mpctx->stop_play = PT_NEXT_ENTRY;
3732 update_osd_msg(mpctx);
3734 } else {
3736 /*========================== PLAY VIDEO ============================*/
3738 vo_pts=mpctx->sh_video->timer*90000.0;
3739 vo_fps=mpctx->sh_video->fps;
3741 if (!mpctx->num_buffered_frames) {
3742 double frame_time = update_video(mpctx, &blit_frame);
3743 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3744 if (mpctx->sh_video->vf_initialized < 0) {
3745 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
3746 mpctx->stop_play = PT_NEXT_ENTRY;
3747 goto goto_next_file;
3749 if (blit_frame) {
3750 struct vf_instance *vf = mpctx->sh_video->vfilter;
3751 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
3752 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
3753 vo_osd_changed(0);
3755 if (frame_time < 0)
3756 mpctx->stop_play = AT_END_OF_FILE;
3757 else {
3758 // might return with !eof && !blit_frame if !correct_pts
3759 mpctx->num_buffered_frames += blit_frame;
3760 if (mpctx->update_video_immediately) {
3761 // Show this frame immediately, rest normally
3762 mpctx->update_video_immediately = false;
3763 } else {
3764 mpctx->time_frame += frame_time / opts->playback_speed;
3765 adjust_sync(mpctx, frame_time);
3770 // ==========================================================================
3772 // current_module="draw_osd";
3773 // if(vo_config_count) mpctx->video_out->draw_osd();
3775 #ifdef CONFIG_GUI
3776 if(use_gui) guiEventHandling();
3777 #endif
3779 current_module="vo_check_events";
3780 vo_check_events(mpctx->video_out);
3782 #ifdef CONFIG_X11
3783 if (stop_xscreensaver) {
3784 current_module = "stop_xscreensaver";
3785 xscreensaver_heartbeat(mpctx->x11_state);
3787 #endif
3788 if (heartbeat_cmd) {
3789 static unsigned last_heartbeat;
3790 unsigned now = GetTimerMS();
3791 if (now - last_heartbeat > 30000) {
3792 last_heartbeat = now;
3793 system(heartbeat_cmd);
3797 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
3799 //====================== FLIP PAGE (VIDEO BLT): =========================
3801 current_module="flip_page";
3802 if (!frame_time_remaining && blit_frame) {
3803 unsigned int t2=GetTimer();
3805 vo_flip_page(mpctx->video_out);
3806 mpctx->num_buffered_frames--;
3808 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
3809 vout_time_usage += mpctx->last_vo_flip_duration;
3810 print_status(mpctx, MP_NOPTS_VALUE, true);
3812 else
3813 print_status(mpctx, MP_NOPTS_VALUE, false);
3815 //============================ Auto QUALITY ============================
3817 /*Output quality adjustments:*/
3818 if(auto_quality>0){
3819 current_module="autoq";
3820 // float total=0.000001f * (GetTimer()-aq_total_time);
3821 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
3822 if(output_quality<auto_quality && aq_sleep_time>0)
3823 ++output_quality;
3824 else
3825 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
3826 if(output_quality>1 && aq_sleep_time<0)
3827 --output_quality;
3828 else
3829 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3830 output_quality=0;
3831 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
3832 set_video_quality(mpctx->sh_video,output_quality);
3835 if (!frame_time_remaining && blit_frame) {
3836 if (play_n_frames >= 0) {
3837 --play_n_frames;
3838 if (play_n_frames <= 0)
3839 mpctx->stop_play = PT_NEXT_ENTRY;
3841 if (mpctx->step_frames > 0) {
3842 mpctx->step_frames--;
3843 if (mpctx->step_frames == 0)
3844 pause_player(mpctx);
3849 // FIXME: add size based support for -endpos
3850 if (end_at.type == END_AT_TIME &&
3851 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3852 mpctx->stop_play = PT_NEXT_ENTRY;
3854 } // end if(mpctx->sh_video)
3856 #ifdef CONFIG_DVDNAV
3857 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3858 nav_highlight_t hl;
3859 mp_dvdnav_get_highlight (mpctx->stream, &hl);
3860 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
3861 vo_osd_changed (OSDTYPE_DVDNAV);
3863 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
3864 double ar = -1.0;
3865 if (stream_control (mpctx->demuxer->stream,
3866 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
3867 != STREAM_UNSUPPORTED)
3868 mpctx->sh_video->stream_aspect = ar;
3871 #endif
3873 //================= Keyboard events, SEEKing ====================
3875 current_module="key_events";
3878 while (1) {
3879 mp_cmd_t* cmd;
3880 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
3881 run_command(mpctx, cmd);
3882 mp_cmd_free(cmd);
3883 if (mpctx->stop_play)
3884 break;
3886 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
3887 || mpctx->abs_seek_pos)
3888 break;
3889 if (mpctx->sh_video) {
3890 update_osd_msg(mpctx);
3891 int hack = vo_osd_changed(0);
3892 vo_osd_changed(hack);
3893 if (hack)
3894 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
3895 add_step_frame(mpctx);
3896 break;
3898 else
3899 vo_osd_changed(0);
3901 pause_loop(mpctx);
3905 // handle -sstep
3906 if (step_sec > 0 && !mpctx->paused) {
3907 mpctx->osd_function=OSD_FFW;
3908 mpctx->rel_seek_secs+=step_sec;
3911 edl_update(mpctx);
3913 /* Looping. */
3914 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
3915 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
3917 if(opts->loop_times>1) opts->loop_times--; else
3918 if(opts->loop_times==1) opts->loop_times=-1;
3919 play_n_frames=play_n_frames_mf;
3920 mpctx->stop_play=0;
3921 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
3922 loop_seek = 1;
3925 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
3926 if (seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos) >= 0) {
3927 // Set OSD:
3928 if(!loop_seek){
3929 if( !edl_decision )
3930 set_osd_bar(mpctx, 0,"Position",0,100,demuxer_get_percent_pos(mpctx->demuxer));
3934 mpctx->rel_seek_secs=0;
3935 mpctx->abs_seek_pos=0;
3936 loop_seek=0;
3937 edl_decision = 0;
3940 #ifdef CONFIG_GUI
3941 if(use_gui){
3942 guiEventHandling();
3943 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
3944 // get pos from frame number / total frames
3945 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
3946 } else {
3947 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
3949 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
3950 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx);
3951 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
3952 guiGetEvent( guiReDraw,NULL );
3953 guiGetEvent( guiSetVolume,NULL );
3954 if(guiIntfStruct.Playing==0) break; // STOP
3955 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
3956 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
3957 #ifdef CONFIG_DVDREAD
3958 if ( mpctx->stream->type == STREAMTYPE_DVD )
3960 dvd_priv_t * dvdp = mpctx->stream->priv;
3961 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
3963 #endif
3965 #endif /* CONFIG_GUI */
3967 } // while(!mpctx->stop_play)
3969 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
3971 #ifdef CONFIG_DVBIN
3972 if(mpctx->dvbin_reopen)
3974 mpctx->stop_play = 0;
3975 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
3976 cache_uninit(mpctx->stream);
3977 mpctx->dvbin_reopen = 0;
3978 goto goto_enable_cache;
3980 #endif
3983 goto_next_file: // don't jump here after ao/vo/getch initialization!
3985 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
3987 if(benchmark){
3988 double tot=video_time_usage+vout_time_usage+audio_time_usage;
3989 double total_time_usage;
3990 total_time_usage_start=GetTimer()-total_time_usage_start;
3991 total_time_usage = (float)total_time_usage_start*0.000001;
3992 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
3993 video_time_usage,vout_time_usage,audio_time_usage,
3994 total_time_usage-tot,total_time_usage);
3995 if(total_time_usage>0.0)
3996 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
3997 100.0*video_time_usage/total_time_usage,
3998 100.0*vout_time_usage/total_time_usage,
3999 100.0*audio_time_usage/total_time_usage,
4000 100.0*(total_time_usage-tot)/total_time_usage,
4001 100.0);
4002 if(total_frame_cnt && frame_dropping)
4003 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4004 total_frame_cnt-drop_frame_cnt,
4005 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4006 drop_frame_cnt,
4007 100*drop_frame_cnt/total_frame_cnt,
4008 total_frame_cnt,
4009 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4012 // time to uninit all, except global stuff:
4013 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI+(opts->fixed_vo?INITIALIZED_VO:0)));
4015 if(mpctx->set_of_sub_size > 0) {
4016 current_module="sub_free";
4017 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4018 sub_free(mpctx->set_of_subtitles[i]);
4019 #ifdef CONFIG_ASS
4020 if(mpctx->set_of_ass_tracks[i])
4021 ass_free_track( mpctx->set_of_ass_tracks[i] );
4022 #endif
4024 mpctx->set_of_sub_size = 0;
4026 vo_sub_last = vo_sub=NULL;
4027 subdata=NULL;
4028 #ifdef CONFIG_ASS
4029 ass_track = NULL;
4030 if(ass_library)
4031 ass_clear_fonts(ass_library);
4032 #endif
4034 if (!mpctx->stop_play) // In case some goto jumped here...
4035 mpctx->stop_play = PT_NEXT_ENTRY;
4037 int playtree_direction = 1;
4039 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4040 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4041 play_tree_iter_free(mpctx->playtree_iter);
4042 mpctx->playtree_iter = NULL;
4044 mpctx->play_tree_step = 1;
4045 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4046 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4047 if(mpctx->playtree_iter) {
4048 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4049 play_tree_iter_free(mpctx->playtree_iter);
4050 mpctx->playtree_iter = NULL;
4053 } else if (mpctx->stop_play == PT_STOP) {
4054 play_tree_iter_free(mpctx->playtree_iter);
4055 mpctx->playtree_iter = NULL;
4056 } else { // NEXT PREV SRC
4057 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4060 while(mpctx->playtree_iter != NULL) {
4061 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4062 if(mpctx->filename == NULL) {
4063 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4064 play_tree_iter_free(mpctx->playtree_iter);
4065 mpctx->playtree_iter = NULL;
4067 } else
4068 break;
4071 #ifdef CONFIG_GUI
4072 if(use_gui && !mpctx->playtree_iter) {
4073 #ifdef CONFIG_DVDREAD
4074 if(!guiIntfStruct.DiskChanged)
4075 #endif
4076 mplEnd();
4078 #endif
4080 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4081 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4082 mpctx->stop_play = 0;
4083 goto play_next_file;
4087 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4089 return 1;
4091 #endif /* DISABLE_MAIN */