Translation system changes part 1: wrap translated strings
[mplayer/glamo.git] / mplayer.c
blobd59e619a4d80379cb2b7405bac35f0ce9d06553d
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"
79 #include "osdep/findfiles.h"
81 #ifdef CONFIG_GUI
82 #include "gui/interface.h"
83 #endif
85 #include "input/input.h"
87 const int under_mencoder = 0;
88 int slave_mode=0;
89 int player_idle_mode=0;
90 int quiet=0;
91 int enable_mouse_movements=0;
92 float start_volume = -1;
94 #include "osdep/priority.h"
96 char *heartbeat_cmd;
98 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
100 #ifdef HAVE_RTC
101 #ifdef __linux__
102 #include <linux/rtc.h>
103 #else
104 #include <rtc.h>
105 #define RTC_IRQP_SET RTCIO_IRQP_SET
106 #define RTC_PIE_ON RTCIO_PIE_ON
107 #endif /* __linux__ */
108 #endif /* HAVE_RTC */
110 #ifdef CONFIG_TV
111 #include "stream/tv.h"
112 #endif
113 #ifdef CONFIG_RADIO
114 #include "stream/stream_radio.h"
115 #endif
117 #ifdef CONFIG_DVBIN
118 #include "stream/dvbin.h"
119 #include "stream/cache2.h"
120 #endif
122 //**************************************************************************//
123 // Playtree
124 //**************************************************************************//
125 #include "playtree.h"
126 #include "playtreeparser.h"
128 int import_playtree_playlist_into_gui(play_tree_t* my_playtree, m_config_t* config);
129 int import_initial_playtree_into_gui(play_tree_t* my_playtree, m_config_t* config, int enqueue);
131 //**************************************************************************//
132 // Config
133 //**************************************************************************//
134 #include "parser-cfg.h"
135 #include "parser-mpcmd.h"
137 //**************************************************************************//
138 // Config file
139 //**************************************************************************//
141 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
143 #include "get_path.h"
145 //**************************************************************************//
146 //**************************************************************************//
147 // Input media streaming & demultiplexer:
148 //**************************************************************************//
150 static int max_framesize=0;
152 #include "stream/stream.h"
153 #include "libmpdemux/demuxer.h"
154 #include "libmpdemux/stheader.h"
156 #ifdef CONFIG_DVDREAD
157 #include "stream/stream_dvd.h"
158 #endif
160 #ifdef CONFIG_DVDNAV
161 #include "stream/stream_dvdnav.h"
162 #endif
164 #include "libmpcodecs/dec_audio.h"
165 #include "libmpcodecs/dec_video.h"
166 #include "libmpcodecs/mp_image.h"
167 #include "libmpcodecs/vf.h"
168 #include "libmpcodecs/vd.h"
170 #include "mixer.h"
172 #include "mp_core.h"
173 #include "options.h"
174 #include "defaultopts.h"
176 //**************************************************************************//
177 //**************************************************************************//
179 // Common FIFO functions, and keyboard/event FIFO code
180 #include "mp_fifo.h"
181 int noconsolecontrols=0;
182 //**************************************************************************//
184 // benchmark:
185 double video_time_usage=0;
186 double vout_time_usage=0;
187 static double audio_time_usage=0;
188 static int total_time_usage_start=0;
189 static int total_frame_cnt=0;
190 static int drop_frame_cnt=0; // total number of dropped frames
191 int benchmark=0;
193 // options:
194 int auto_quality=0;
195 static int output_quality=0;
197 int use_gui=0;
199 #ifdef CONFIG_GUI
200 int enqueue=0;
201 #endif
203 static int list_properties = 0;
205 int term_osd = 1;
206 static char* term_osd_esc = "\x1b[A\r\x1b[K";
207 static char* playing_msg = NULL;
208 // seek:
209 static double seek_to_sec;
210 static off_t seek_to_byte=0;
211 static off_t step_sec=0;
213 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
215 // A/V sync:
216 int autosync=0; // 30 might be a good default value.
218 // codecs:
219 char **audio_codec_list=NULL; // override audio codec
220 char **video_codec_list=NULL; // override video codec
221 char **audio_fm_list=NULL; // override audio codec family
222 char **video_fm_list=NULL; // override video codec family
224 // demuxer:
225 extern char *demuxer_name; // override demuxer
226 extern char *audio_demuxer_name; // override audio demuxer
227 extern char *sub_demuxer_name; // override sub demuxer
229 int vobsub_id=-1;
230 char* audio_lang=NULL;
231 char* dvdsub_lang=NULL;
232 static char* spudec_ifo=NULL;
233 int forced_subs_only=0;
234 int file_filter=1;
236 // cache2:
237 int stream_cache_size=-1;
238 #ifdef CONFIG_STREAM_CACHE
239 extern int cache_fill_status;
241 float stream_cache_min_percent=20.0;
242 float stream_cache_seek_min_percent=50.0;
243 #else
244 #define cache_fill_status 0
245 #endif
247 // dump:
248 static char *stream_dump_name="stream.dump";
249 int stream_dump_type=0;
251 // A-V sync:
252 static float default_max_pts_correction=-1;//0.01f;
253 float audio_delay=0;
254 static int ignore_start=0;
256 static int softsleep=0;
258 double force_fps=0;
259 static int force_srate=0;
260 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
261 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
262 static int play_n_frames=-1;
263 static int play_n_frames_mf=-1;
265 // sub:
266 char *font_name=NULL;
267 char *sub_font_name=NULL;
268 extern int font_fontconfig;
269 float font_factor=0.75;
270 char **sub_name=NULL;
271 float sub_delay=0;
272 float sub_fps=0;
273 int sub_auto = 1;
274 char *vobsub_name=NULL;
275 /*DSP!!char *dsp=NULL;*/
276 int subcc_enabled=0;
277 int suboverlap_enabled = 1;
279 #ifdef CONFIG_ASS
280 #include "libass/ass.h"
281 #include "libass/ass_mp.h"
282 #endif
284 char* current_module=NULL; // for debugging
287 // ---
289 #ifdef CONFIG_MENU
290 #include "m_struct.h"
291 #include "libmenu/menu.h"
292 void vf_menu_pause_update(struct vf_instance* vf);
293 extern vf_info_t vf_info_menu;
294 static const vf_info_t* const libmenu_vfs[] = {
295 &vf_info_menu,
296 NULL
298 static vf_instance_t* vf_menu = NULL;
299 int use_menu = 0;
300 static char* menu_cfg = NULL;
301 static char* menu_root = "main";
302 #endif
305 #ifdef HAVE_RTC
306 static int nortc = 1;
307 static char* rtc_device;
308 #endif
310 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
311 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
312 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
313 int use_filedir_conf;
315 #include "mpcommon.h"
316 #include "command.h"
318 #include "metadata.h"
320 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
322 const void *mpctx_get_video_out(MPContext *mpctx)
324 return mpctx->video_out;
327 const void *mpctx_get_audio_out(MPContext *mpctx)
329 return mpctx->audio_out;
332 void *mpctx_get_demuxer(MPContext *mpctx)
334 return mpctx->demuxer;
337 void *mpctx_get_playtree_iter(MPContext *mpctx)
339 return mpctx->playtree_iter;
342 void *mpctx_get_mixer(MPContext *mpctx)
344 return &mpctx->mixer;
347 int mpctx_get_global_sub_size(MPContext *mpctx)
349 return mpctx->global_sub_size;
352 int mpctx_get_osd_function(MPContext *mpctx)
354 return mpctx->osd_function;
357 static float get_relative_time(struct MPContext *mpctx)
359 unsigned int new_time = GetTimer();
360 unsigned int delta = new_time - mpctx->last_time;
361 mpctx->last_time = new_time;
362 return delta * 0.000001;
365 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
366 switch (type)
368 /* check for valid video stream */
369 case META_VIDEO_CODEC:
370 case META_VIDEO_BITRATE:
371 case META_VIDEO_RESOLUTION:
373 if (!mpctx->sh_video)
374 return 0;
375 break;
378 /* check for valid audio stream */
379 case META_AUDIO_CODEC:
380 case META_AUDIO_BITRATE:
381 case META_AUDIO_SAMPLES:
383 if (!mpctx->sh_audio)
384 return 0;
385 break;
388 /* check for valid demuxer */
389 case META_INFO_TITLE:
390 case META_INFO_ARTIST:
391 case META_INFO_ALBUM:
392 case META_INFO_YEAR:
393 case META_INFO_COMMENT:
394 case META_INFO_TRACK:
395 case META_INFO_GENRE:
397 if (!mpctx->demuxer)
398 return 0;
399 break;
402 default:
403 break;
406 return 1;
409 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
410 char **info = mpctx->demuxer->info;
411 int n;
413 if (!info || !tag)
414 return NULL;
416 for (n = 0; info[2*n] != NULL ; n++)
417 if (!strcmp (info[2*n], tag))
418 break;
420 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
423 char *get_metadata(struct MPContext *mpctx, metadata_t type)
425 char *meta = NULL;
426 sh_audio_t * const sh_audio = mpctx->sh_audio;
427 sh_video_t * const sh_video = mpctx->sh_video;
429 if (!is_valid_metadata_type(mpctx, type))
430 return NULL;
432 switch (type)
434 case META_NAME:
436 return strdup (mp_basename2 (mpctx->filename));
439 case META_VIDEO_CODEC:
441 if (sh_video->format == 0x10000001)
442 meta = strdup ("mpeg1");
443 else if (sh_video->format == 0x10000002)
444 meta = strdup ("mpeg2");
445 else if (sh_video->format == 0x10000004)
446 meta = strdup ("mpeg4");
447 else if (sh_video->format == 0x10000005)
448 meta = strdup ("h264");
449 else if (sh_video->format >= 0x20202020)
451 meta = malloc (8);
452 sprintf (meta, "%.4s", (char *) &sh_video->format);
454 else
456 meta = malloc (8);
457 sprintf (meta, "0x%08X", sh_video->format);
459 return meta;
462 case META_VIDEO_BITRATE:
464 meta = malloc (16);
465 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
466 return meta;
469 case META_VIDEO_RESOLUTION:
471 meta = malloc (16);
472 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
473 return meta;
476 case META_AUDIO_CODEC:
478 if (sh_audio->codec && sh_audio->codec->name)
479 meta = strdup (sh_audio->codec->name);
480 return meta;
483 case META_AUDIO_BITRATE:
485 meta = malloc (16);
486 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
487 return meta;
490 case META_AUDIO_SAMPLES:
492 meta = malloc (16);
493 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
494 return meta;
497 /* check for valid demuxer */
498 case META_INFO_TITLE:
499 return get_demuxer_info(mpctx, "Title");
501 case META_INFO_ARTIST:
502 return get_demuxer_info(mpctx, "Artist");
504 case META_INFO_ALBUM:
505 return get_demuxer_info(mpctx, "Album");
507 case META_INFO_YEAR:
508 return get_demuxer_info(mpctx, "Year");
510 case META_INFO_COMMENT:
511 return get_demuxer_info(mpctx, "Comment");
513 case META_INFO_TRACK:
514 return get_demuxer_info(mpctx, "Track");
516 case META_INFO_GENRE:
517 return get_demuxer_info(mpctx, "Genre");
519 default:
520 break;
523 return meta;
526 /// step size of mixer changes
527 int volstep = 3;
529 #ifdef CONFIG_DVDNAV
530 static void mp_dvdnav_context_free(MPContext *ctx){
531 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
532 ctx->nav_smpi = NULL;
533 if (ctx->nav_buffer) free(ctx->nav_buffer);
534 ctx->nav_buffer = NULL;
535 ctx->nav_start = NULL;
536 ctx->nav_in_size = 0;
538 #endif
540 void uninit_player(struct MPContext *mpctx, unsigned int mask){
541 mask=mpctx->initialized_flags&mask;
543 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
545 if(mask&INITIALIZED_ACODEC){
546 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
547 current_module="uninit_acodec";
548 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
549 #ifdef CONFIG_GUI
550 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
551 #endif
552 mpctx->sh_audio=NULL;
553 mpctx->mixer.afilter = NULL;
556 if(mask&INITIALIZED_VCODEC){
557 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
558 current_module="uninit_vcodec";
559 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
560 mpctx->sh_video=NULL;
561 #ifdef CONFIG_MENU
562 vf_menu=NULL;
563 #endif
566 if(mask&INITIALIZED_DEMUXER){
567 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
568 current_module="free_demuxer";
569 if (mpctx->num_sources) {
570 mpctx->demuxer = mpctx->sources[0].demuxer;
571 for (int i = 1; i < mpctx->num_sources; i++) {
572 free_stream(mpctx->sources[i].stream);
573 free_demuxer(mpctx->sources[i].demuxer);
576 talloc_free(mpctx->sources);
577 mpctx->sources = NULL;
578 mpctx->num_sources = 0;
579 talloc_free(mpctx->timeline);
580 mpctx->timeline = NULL;
581 mpctx->num_timeline_parts = 0;
582 talloc_free(mpctx->chapters);
583 mpctx->chapters = NULL;
584 mpctx->num_chapters = 0;
585 mpctx->video_offset = 0;
586 if(mpctx->demuxer){
587 mpctx->stream=mpctx->demuxer->stream;
588 free_demuxer(mpctx->demuxer);
590 mpctx->demuxer=NULL;
593 // kill the cache process:
594 if(mask&INITIALIZED_STREAM){
595 mpctx->initialized_flags&=~INITIALIZED_STREAM;
596 current_module="uninit_stream";
597 if(mpctx->stream) free_stream(mpctx->stream);
598 mpctx->stream=NULL;
601 if(mask&INITIALIZED_VO){
602 mpctx->initialized_flags&=~INITIALIZED_VO;
603 current_module="uninit_vo";
604 vo_destroy(mpctx->video_out);
605 mpctx->video_out=NULL;
606 #ifdef CONFIG_DVDNAV
607 mp_dvdnav_context_free(mpctx);
608 #endif
611 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
612 if(mask&INITIALIZED_GETCH2){
613 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
614 current_module="uninit_getch2";
615 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
616 // restore terminal:
617 getch2_disable();
620 if(mask&INITIALIZED_VOBSUB){
621 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
622 current_module="uninit_vobsub";
623 if(vo_vobsub) vobsub_close(vo_vobsub);
624 vo_vobsub=NULL;
627 if (mask&INITIALIZED_SPUDEC){
628 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
629 current_module="uninit_spudec";
630 spudec_free(vo_spudec);
631 vo_spudec=NULL;
634 if(mask&INITIALIZED_AO){
635 mpctx->initialized_flags&=~INITIALIZED_AO;
636 current_module="uninit_ao";
637 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
638 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
639 mpctx->audio_out=NULL;
642 #ifdef CONFIG_GUI
643 if(mask&INITIALIZED_GUI){
644 mpctx->initialized_flags&=~INITIALIZED_GUI;
645 current_module="uninit_gui";
646 guiDone();
648 #endif
650 current_module=NULL;
653 void exit_player_with_rc(struct MPContext *mpctx, exit_reason_t how, int rc)
655 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
656 uninit_player(mpctx, INITIALIZED_ALL);
657 #if defined(__MINGW32__) || defined(__CYGWIN__)
658 timeEndPeriod(1);
659 #endif
660 #ifdef CONFIG_X11
661 #ifdef CONFIG_GUI
662 if ( !use_gui )
663 #endif
664 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
665 #endif
667 current_module="uninit_input";
668 mp_input_uninit(mpctx->input);
669 #ifdef CONFIG_MENU
670 if (use_menu)
671 menu_uninit();
672 #endif
674 #ifdef CONFIG_FREETYPE
675 current_module="uninit_font";
676 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
677 free_font_desc(mpctx->osd->sub_font);
678 free_font_desc(vo_font);
679 vo_font = NULL;
680 done_freetype();
681 #endif
682 osd_free(mpctx->osd);
684 #ifdef CONFIG_ASS
685 ass_library_done(ass_library);
686 #endif
688 current_module="exit_player";
690 // free mplayer config
691 if(mpctx->mconfig)
692 m_config_free(mpctx->mconfig);
694 if(mpctx->playtree)
695 play_tree_free(mpctx->playtree, 1);
697 talloc_free(mpctx->key_fifo);
699 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
700 switch(how) {
701 case EXIT_QUIT:
702 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_quit);
703 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
704 break;
705 case EXIT_EOF:
706 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_eof);
707 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
708 break;
709 case EXIT_ERROR:
710 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_error);
711 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
712 break;
713 default:
714 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
716 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
718 exit(rc);
721 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
723 exit_player_with_rc(mpctx, how, 1);
726 #ifndef __MINGW32__
727 static void child_sighandler(int x){
728 pid_t pid;
729 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
731 #endif
733 #ifdef CONFIG_CRASH_DEBUG
734 static char *prog_path;
735 static int crash_debug = 0;
736 #endif
738 static void exit_sighandler(int x){
739 static int sig_count=0;
740 #ifdef CONFIG_CRASH_DEBUG
741 if (!crash_debug || x != SIGTRAP)
742 #endif
743 ++sig_count;
744 if(sig_count==5)
746 /* We're crashing bad and can't uninit cleanly :(
747 * by popular request, we make one last (dirty)
748 * effort to restore the user's
749 * terminal. */
750 getch2_disable();
751 exit(1);
753 if(sig_count==6) exit(1);
754 if(sig_count>6){
755 // can't stop :(
756 #ifndef __MINGW32__
757 kill(getpid(),SIGKILL);
758 #endif
760 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
761 current_module?current_module:"unknown"
763 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
764 if(sig_count<=1)
765 switch(x){
766 case SIGINT:
767 case SIGQUIT:
768 case SIGTERM:
769 case SIGKILL:
770 async_quit_request = 1;
771 return; // killed from keyboard (^C) or killed [-9]
772 case SIGILL:
773 #if CONFIG_RUNTIME_CPUDETECT
774 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
775 #else
776 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
777 #endif
778 case SIGFPE:
779 case SIGSEGV:
780 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
781 default:
782 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
783 #ifdef CONFIG_CRASH_DEBUG
784 if (crash_debug) {
785 int gdb_pid;
786 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
787 gdb_pid = fork();
788 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
789 if (gdb_pid == 0) { // We are the child
790 char spid[20];
791 snprintf(spid, sizeof(spid), "%i", getppid());
792 getch2_disable(); // allow terminal to work properly with gdb
793 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
794 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
795 } else if (gdb_pid < 0)
796 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
797 else {
798 waitpid(gdb_pid, NULL, 0);
800 if (x == SIGTRAP) return;
802 #endif
804 getch2_disable();
805 exit(1);
808 #include "cfg-mplayer.h"
810 static int cfg_include(m_option_t *conf, char *filename)
812 return m_config_parse_config_file(conf->priv, filename);
815 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
817 char *conffile;
818 int conffile_fd;
819 if (!disable_system_conf &&
820 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
821 exit_player(mpctx, EXIT_NONE);
822 if ((conffile = get_path("")) == NULL) {
823 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
824 } else {
825 #ifdef __MINGW32__
826 mkdir(conffile);
827 #else
828 mkdir(conffile, 0777);
829 #endif
830 free(conffile);
831 if ((conffile = get_path("config")) == NULL) {
832 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
833 } else {
834 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
835 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
836 write(conffile_fd, default_config, strlen(default_config));
837 close(conffile_fd);
839 if (!disable_user_conf &&
840 m_config_parse_config_file(conf, conffile) < 0)
841 exit_player(mpctx, EXIT_NONE);
842 free(conffile);
847 #define PROFILE_CFG_PROTOCOL "protocol."
849 static void load_per_protocol_config (m_config_t* conf, const char *const file)
851 char *str;
852 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
853 m_profile_t *p;
855 /* does filename actually uses a protocol ? */
856 str = strstr (file, "://");
857 if (!str)
858 return;
860 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
861 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
862 p = m_config_get_profile (conf, protocol);
863 if (p)
865 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingProtocolProfile, protocol);
866 m_config_set_profile(conf,p);
870 #define PROFILE_CFG_EXTENSION "extension."
872 static void load_per_extension_config (m_config_t* conf, const char *const file)
874 char *str;
875 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
876 m_profile_t *p;
878 /* does filename actually have an extension ? */
879 str = strrchr (file, '.');
880 if (!str)
881 return;
883 sprintf (extension, PROFILE_CFG_EXTENSION);
884 strncat (extension, ++str, 7);
885 p = m_config_get_profile (conf, extension);
886 if (p)
888 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, extension);
889 m_config_set_profile(conf,p);
893 #define PROFILE_CFG_VO "vo."
894 #define PROFILE_CFG_AO "ao."
896 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
898 char profile[strlen (cfg) + strlen (out) + 1];
899 m_profile_t *p;
901 sprintf (profile, "%s%s", cfg, out);
902 p = m_config_get_profile (conf, profile);
903 if (p)
905 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, profile);
906 m_config_set_profile(conf,p);
910 static void load_per_file_config (m_config_t* conf, const char *const file)
912 char *confpath;
913 char cfg[strlen(file)+10];
914 struct stat st;
915 char *name;
917 sprintf (cfg, "%s.conf", file);
919 if (use_filedir_conf && !stat (cfg, &st))
921 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
922 m_config_parse_config_file (conf, cfg);
923 return;
926 if ((name = strrchr (cfg, '/')) == NULL)
927 name = cfg;
928 else
929 name++;
931 if ((confpath = get_path (name)) != NULL)
933 if (!stat (confpath, &st))
935 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
936 m_config_parse_config_file (conf, confpath);
939 free (confpath);
943 /* When libmpdemux performs a blocking operation (network connection or
944 * cache filling) if the operation fails we use this function to check
945 * if it was interrupted by the user.
946 * The function returns a new value for eof. */
947 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
949 mp_cmd_t* cmd;
950 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
951 switch(cmd->id) {
952 case MP_CMD_QUIT:
953 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
954 case MP_CMD_PLAY_TREE_STEP: {
955 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
956 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
957 } break;
958 case MP_CMD_PLAY_TREE_UP_STEP: {
959 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
960 } break;
961 case MP_CMD_PLAY_ALT_SRC_STEP: {
962 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
963 } break;
965 mp_cmd_free(cmd);
967 return stop_play;
970 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
972 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
974 play_tree_add_bpf(entry,mpctx->filename);
976 #ifdef CONFIG_GUI
977 if (use_gui) {
978 if (entry) {
979 import_playtree_playlist_into_gui(entry, mpctx->mconfig);
980 play_tree_free_list(entry,1);
982 } else
983 #endif
985 if(!entry) {
986 entry = mpctx->playtree_iter->tree;
987 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
988 return PT_NEXT_ENTRY;
990 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
991 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
992 return PT_NEXT_ENTRY;
995 play_tree_remove(entry,1,1);
996 return PT_NEXT_SRC;
998 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
999 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1000 entry = mpctx->playtree_iter->tree;
1001 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1002 return PT_NEXT_ENTRY;
1004 play_tree_remove(entry,1,1);
1006 return PT_NEXT_SRC;
1009 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1011 sub_data *subd;
1012 #ifdef CONFIG_ASS
1013 ass_track_t *asst = 0;
1014 #endif
1016 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1017 return;
1020 subd = sub_read_file(filename, fps);
1021 #ifdef CONFIG_ASS
1022 if (ass_enabled)
1023 #ifdef CONFIG_ICONV
1024 asst = ass_read_file(ass_library, filename, sub_cp);
1025 #else
1026 asst = ass_read_file(ass_library, filename, 0);
1027 #endif
1028 if (ass_enabled && subd && !asst)
1029 asst = ass_read_subdata(ass_library, subd, fps);
1031 if (!asst && !subd)
1032 #else
1033 if(!subd)
1034 #endif
1035 mp_tmsg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
1036 filename_recode(filename));
1038 #ifdef CONFIG_ASS
1039 if (!asst && !subd) return;
1040 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1041 #else
1042 if (!subd) return;
1043 #endif
1044 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1045 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1046 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1047 filename_recode(filename));
1048 ++mpctx->set_of_sub_size;
1049 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
1050 filename_recode(filename));
1053 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1054 void update_set_of_subtitles(struct MPContext *mpctx)
1055 // subdata was changed, set_of_sub... have to be updated.
1057 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
1058 int i;
1059 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1060 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
1061 set_of_subtitles[i-1] = set_of_subtitles[i];
1062 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
1063 --mpctx->set_of_sub_size;
1064 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
1066 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1067 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
1069 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1070 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1071 ++mpctx->set_of_sub_size;
1075 void init_vo_spudec(struct MPContext *mpctx)
1077 if (vo_spudec)
1078 spudec_free(vo_spudec);
1079 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1080 vo_spudec = NULL;
1081 if (spudec_ifo) {
1082 unsigned int palette[16], width, height;
1083 current_module="spudec_init_vobsub";
1084 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1085 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1088 #ifdef CONFIG_DVDREAD
1089 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1090 current_module="spudec_init_dvdread";
1091 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1092 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1093 NULL, 0);
1095 #endif
1097 #ifdef CONFIG_DVDNAV
1098 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1099 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1100 current_module="spudec_init_dvdnav";
1101 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1103 #endif
1105 if (vo_spudec==NULL) {
1106 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1107 current_module="spudec_init_normal";
1108 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1109 spudec_set_font_factor(vo_spudec,font_factor);
1112 if (vo_spudec!=NULL) {
1113 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1114 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1119 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1120 * make it all work is to use the builtin SDL-bootstrap code, which
1121 * will be done automatically by replacing our main() if we include SDL.h.
1123 #if defined(__APPLE__) && defined(CONFIG_SDL)
1124 #include <SDL.h>
1125 #endif
1128 * \brief append a formatted string
1129 * \param buf buffer to print into
1130 * \param pos position of terminating 0 in buf
1131 * \param len maximum number of characters in buf, not including terminating 0
1132 * \param format printf format string
1134 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1136 va_list va;
1137 va_start(va, format);
1138 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1139 va_end(va);
1140 if (*pos >= len ) {
1141 buf[len] = 0;
1142 *pos = len;
1147 * \brief append time in the hh:mm:ss.f format
1148 * \param buf buffer to print into
1149 * \param pos position of terminating 0 in buf
1150 * \param len maximum number of characters in buf, not including terminating 0
1151 * \param time time value to convert/append
1153 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1154 long tenths = 10 * time;
1155 int f1 = tenths % 10;
1156 int ss = (tenths / 10) % 60;
1157 int mm = (tenths / 600) % 60;
1158 int hh = tenths / 36000;
1159 if (time <= 0) {
1160 saddf(buf, pos, len, "unknown");
1161 return;
1163 if (hh > 0)
1164 saddf(buf, pos, len, "%2d:", hh);
1165 if (hh > 0 || mm > 0)
1166 saddf(buf, pos, len, "%02d:", mm);
1167 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1170 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1172 struct MPOpts *opts = &mpctx->opts;
1173 sh_video_t * const sh_video = mpctx->sh_video;
1175 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1176 a_pos = playing_audio_pts(mpctx);
1177 if (mpctx->sh_audio && sh_video && at_frame) {
1178 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1179 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1180 && !mpctx->drop_message_shown) {
1181 mp_tmsg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1182 mpctx->drop_message_shown = true;
1185 if (quiet)
1186 return;
1189 int width;
1190 char *line;
1191 unsigned pos = 0;
1192 get_screen_size();
1193 if (screen_width > 0)
1194 width = screen_width;
1195 else
1196 width = 80;
1197 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1198 /* Windows command line is broken (MinGW's rxvt works, but we
1199 * should not depend on that). */
1200 width--;
1201 #endif
1202 line = malloc(width + 1); // one additional char for the terminating null
1204 // Audio time
1205 if (mpctx->sh_audio) {
1206 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1207 if (!sh_video) {
1208 float len = demuxer_get_time_length(mpctx->demuxer);
1209 saddf(line, &pos, width, "(");
1210 sadd_hhmmssf(line, &pos, width, a_pos);
1211 saddf(line, &pos, width, ") of %.1f (", len);
1212 sadd_hhmmssf(line, &pos, width, len);
1213 saddf(line, &pos, width, ") ");
1217 // Video time
1218 if (sh_video)
1219 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1221 // A-V sync
1222 if (mpctx->sh_audio && sh_video)
1223 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1224 mpctx->last_av_difference, mpctx->total_avsync_change);
1226 // Video stats
1227 if (sh_video)
1228 saddf(line, &pos, width, "%3d/%3d ",
1229 (int)sh_video->num_frames,
1230 (int)sh_video->num_frames_decoded);
1232 // CPU usage
1233 if (sh_video) {
1234 if (sh_video->timer > 0.5)
1235 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1236 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1237 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1238 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1239 else
1240 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1241 } else if (mpctx->sh_audio) {
1242 if (mpctx->delay > 0.5)
1243 saddf(line, &pos, width, "%4.1f%% ",
1244 100.0*audio_time_usage/(double)mpctx->delay);
1245 else
1246 saddf(line, &pos, width, "??,?%% ");
1249 // VO stats
1250 if (sh_video)
1251 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1253 #ifdef CONFIG_STREAM_CACHE
1254 // cache stats
1255 if (stream_cache_size > 0)
1256 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1257 #endif
1259 // other
1260 if (opts->playback_speed != 1)
1261 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1263 // end
1264 if (erase_to_end_of_line) {
1265 line[pos] = 0;
1266 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1267 } else {
1268 memset(&line[pos], ' ', width - pos);
1269 line[width] = 0;
1270 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1272 free(line);
1276 * \brief build a chain of audio filters that converts the input format
1277 * to the ao's format, taking into account the current playback_speed.
1278 * \param sh_audio describes the requested input format of the chain.
1279 * \param ao_data describes the requested output format of the chain.
1281 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1283 struct MPOpts *opts = &mpctx->opts;
1284 int new_srate;
1285 int result;
1286 if (!sh_audio)
1288 #ifdef CONFIG_GUI
1289 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1290 #endif
1291 mpctx->mixer.afilter = NULL;
1292 return 0;
1294 if(af_control_any_rev(sh_audio->afilter,
1295 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1296 &opts->playback_speed)) {
1297 new_srate = sh_audio->samplerate;
1298 } else {
1299 new_srate = sh_audio->samplerate * opts->playback_speed;
1300 if (new_srate != ao_data->samplerate) {
1301 // limits are taken from libaf/af_resample.c
1302 if (new_srate < 8000)
1303 new_srate = 8000;
1304 if (new_srate > 192000)
1305 new_srate = 192000;
1306 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1309 result = init_audio_filters(sh_audio, new_srate,
1310 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1311 mpctx->mixer.afilter = sh_audio->afilter;
1312 #ifdef CONFIG_GUI
1313 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1314 #endif
1315 return result;
1319 typedef struct mp_osd_msg mp_osd_msg_t;
1320 struct mp_osd_msg {
1321 /// Previous message on the stack.
1322 mp_osd_msg_t* prev;
1323 /// Message text.
1324 char msg[128];
1325 int id,level,started;
1326 /// Display duration in ms.
1327 unsigned time;
1330 /// OSD message stack.
1331 static mp_osd_msg_t* osd_msg_stack = NULL;
1334 * \brief Add a message on the OSD message stack
1336 * If a message with the same id is already present in the stack
1337 * it is pulled on top of the stack, otherwise a new message is created.
1341 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1342 mp_osd_msg_t *msg,*last=NULL;
1343 va_list va;
1344 int r;
1346 // look if the id is already in the stack
1347 for(msg = osd_msg_stack ; msg && msg->id != id ;
1348 last = msg, msg = msg->prev);
1349 // not found: alloc it
1350 if(!msg) {
1351 msg = calloc(1,sizeof(mp_osd_msg_t));
1352 msg->prev = osd_msg_stack;
1353 osd_msg_stack = msg;
1354 } else if(last) { // found, but it's not on top of the stack
1355 last->prev = msg->prev;
1356 msg->prev = osd_msg_stack;
1357 osd_msg_stack = msg;
1359 // write the msg
1360 va_start(va,fmt);
1361 r = vsnprintf(msg->msg, 128, fmt, va);
1362 va_end(va);
1363 if(r >= 128) msg->msg[127] = 0;
1364 // set id and time
1365 msg->id = id;
1366 msg->level = level;
1367 msg->time = time;
1372 * \brief Remove a message from the OSD stack
1374 * This function can be used to get rid of a message right away.
1378 void rm_osd_msg(int id) {
1379 mp_osd_msg_t *msg,*last=NULL;
1381 // Search for the msg
1382 for(msg = osd_msg_stack ; msg && msg->id != id ;
1383 last = msg, msg = msg->prev);
1384 if(!msg) return;
1386 // Detach it from the stack and free it
1387 if(last)
1388 last->prev = msg->prev;
1389 else
1390 osd_msg_stack = msg->prev;
1391 free(msg);
1395 * \brief Remove all messages from the OSD stack
1399 static void clear_osd_msgs(void) {
1400 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1401 while(msg) {
1402 prev = msg->prev;
1403 free(msg);
1404 msg = prev;
1406 osd_msg_stack = NULL;
1410 * \brief Get the current message from the OSD stack.
1412 * This function decrements the message timer and destroys the old ones.
1413 * The message that should be displayed is returned (if any).
1417 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1419 struct MPOpts *opts = &mpctx->opts;
1420 mp_osd_msg_t *msg,*prev,*last = NULL;
1421 static unsigned last_update = 0;
1422 unsigned now = GetTimerMS();
1423 unsigned diff;
1424 char hidden_dec_done = 0;
1426 if (mpctx->osd_visible) {
1427 // 36000000 means max timed visibility is 1 hour into the future, if
1428 // the difference is greater assume it's wrapped around from below 0
1429 if (mpctx->osd_visible - now > 36000000) {
1430 mpctx->osd_visible = 0;
1431 vo_osd_progbar_type = -1; // disable
1432 vo_osd_changed(OSDTYPE_PROGBAR);
1433 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1436 if (mpctx->osd_show_percentage_until - now > 36000000)
1437 mpctx->osd_show_percentage_until = 0;
1439 if(!last_update) last_update = now;
1440 diff = now >= last_update ? now - last_update : 0;
1442 last_update = now;
1444 // Look for the first message in the stack with high enough level.
1445 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1446 prev = msg->prev;
1447 if (msg->level > opts->osd_level && hidden_dec_done)
1448 continue;
1449 // The message has a high enough level or it is the first hidden one
1450 // in both cases we decrement the timer or kill it.
1451 if(!msg->started || msg->time > diff) {
1452 if(msg->started) msg->time -= diff;
1453 else msg->started = 1;
1454 // display it
1455 if (msg->level <= opts->osd_level)
1456 return msg;
1457 hidden_dec_done = 1;
1458 continue;
1460 // kill the message
1461 free(msg);
1462 if(last) {
1463 last->prev = prev;
1464 msg = last;
1465 } else {
1466 osd_msg_stack = prev;
1467 msg = NULL;
1470 // Nothing found
1471 return NULL;
1475 * \brief Display the OSD bar.
1477 * Display the OSD bar or fall back on a simple message.
1481 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1482 struct MPOpts *opts = &mpctx->opts;
1483 if (opts->osd_level < 1)
1484 return;
1486 if(mpctx->sh_video) {
1487 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1488 vo_osd_progbar_type = type;
1489 vo_osd_progbar_value = 256*(val-min)/(max-min);
1490 vo_osd_changed(OSDTYPE_PROGBAR);
1491 return;
1494 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1495 name, ROUND(100*(val-min)/(max-min)));
1500 * \brief Update the OSD message line.
1502 * This function displays the current message on the vo OSD or on the term.
1503 * If the stack is empty and the OSD level is high enough the timer
1504 * is displayed (only on the vo OSD).
1508 static void update_osd_msg(struct MPContext *mpctx)
1510 struct MPOpts *opts = &mpctx->opts;
1511 mp_osd_msg_t *msg;
1512 struct osd_state *osd = mpctx->osd;
1513 char osd_text_timer[128];
1515 if (mpctx->add_osd_seek_info) {
1516 double percentage;
1517 if (mpctx->timeline && mpctx->sh_video)
1518 percentage = mpctx->sh_video->pts * 100 /
1519 mpctx->timeline[mpctx->num_timeline_parts].start;
1520 else
1521 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1522 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1523 if (mpctx->sh_video)
1524 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1525 mpctx->add_osd_seek_info = false;
1528 // Look if we have a msg
1529 if((msg = get_osd_msg(mpctx))) {
1530 if (strcmp(osd->osd_text, msg->msg)) {
1531 strncpy(osd->osd_text, msg->msg, 127);
1532 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1533 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1535 return;
1538 if(mpctx->sh_video) {
1539 // fallback on the timer
1540 if (opts->osd_level >= 2) {
1541 int len;
1542 if (mpctx->timeline)
1543 len = mpctx->timeline[mpctx->num_timeline_parts].start;
1544 else
1545 len = demuxer_get_time_length(mpctx->demuxer);
1546 int percentage = -1;
1547 char percentage_text[10];
1548 int pts = demuxer_get_current_time(mpctx->demuxer);
1550 if (mpctx->osd_show_percentage_until)
1551 if (mpctx->timeline)
1552 percentage = mpctx->sh_video->pts * 100 /
1553 mpctx->timeline[mpctx->num_timeline_parts].start;
1554 else
1555 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1557 if (percentage >= 0)
1558 snprintf(percentage_text, 9, " (%d%%)", percentage);
1559 else
1560 percentage_text[0] = 0;
1562 if (opts->osd_level == 3)
1563 snprintf(osd_text_timer, 63,
1564 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1565 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1566 len/3600,(len/60)%60,len%60,percentage_text);
1567 else
1568 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1569 mpctx->osd_function,pts/3600,(pts/60)%60,
1570 pts%60,percentage_text);
1571 } else
1572 osd_text_timer[0]=0;
1574 if (strcmp(osd->osd_text, osd_text_timer)) {
1575 strncpy(osd->osd_text, osd_text_timer, 63);
1576 vo_osd_changed(OSDTYPE_OSD);
1578 return;
1581 // Clear the term osd line
1582 if (term_osd && osd->osd_text[0]) {
1583 osd->osd_text[0] = 0;
1584 printf("%s\n",term_osd_esc);
1588 ///@}
1589 // OSDMsgStack
1592 void reinit_audio_chain(struct MPContext *mpctx)
1594 struct MPOpts *opts = &mpctx->opts;
1595 if(mpctx->sh_audio){
1596 current_module="init_audio_codec";
1597 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1598 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1599 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1600 mpctx->d_audio->id = -2;
1601 return;
1602 } else
1603 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1604 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1607 //const ao_info_t *info=audio_out->info;
1608 current_module="af_preinit";
1609 ao_data.samplerate=force_srate;
1610 ao_data.channels=0;
1611 ao_data.format=audio_output_format;
1612 #if 1
1613 // first init to detect best values
1614 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1615 // input:
1616 mpctx->sh_audio->samplerate,
1617 // output:
1618 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1619 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1620 exit_player(mpctx, EXIT_ERROR);
1622 #endif
1623 current_module="ao2_init";
1624 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1625 0, // plugin flag
1626 ao_data.samplerate,
1627 ao_data.channels,
1628 ao_data.format,0))){
1629 // FAILED:
1630 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1631 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1632 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1633 mpctx->d_audio->id = -2;
1634 return;
1635 } else {
1636 // SUCCESS:
1637 mpctx->initialized_flags|=INITIALIZED_AO;
1638 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1639 mpctx->audio_out->info->short_name,
1640 ao_data.samplerate, ao_data.channels,
1641 af_fmt2str_short(ao_data.format),
1642 af_fmt2bits(ao_data.format)/8 );
1643 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1644 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1645 if(strlen(mpctx->audio_out->info->comment) > 0)
1646 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1647 // init audio filters:
1648 #if 1
1649 current_module="af_init";
1650 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1651 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1652 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1653 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1654 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1656 #endif
1658 mpctx->mixer.audio_out = mpctx->audio_out;
1659 mpctx->mixer.volstep = volstep;
1664 ///@}
1665 // Command2Property
1668 // Return pts value corresponding to the end point of audio written to the
1669 // ao so far.
1670 static double written_audio_pts(struct MPContext *mpctx)
1672 sh_audio_t *sh_audio = mpctx->sh_audio;
1673 demux_stream_t *d_audio = mpctx->d_audio;
1674 double buffered_output;
1675 // first calculate the end pts of audio that has been output by decoder
1676 double a_pts = sh_audio->pts;
1677 if (a_pts != MP_NOPTS_VALUE)
1678 // Good, decoder supports new way of calculating audio pts.
1679 // sh_audio->pts is the timestamp of the latest input packet with
1680 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1681 // the amount of bytes the decoder has written after that timestamp.
1682 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1683 else {
1684 // Decoder doesn't support new way of calculating pts (or we're
1685 // being called before it has decoded anything with known timestamp).
1686 // Use the old method of audio pts calculation: take the timestamp
1687 // of last packet with known pts the decoder has read data from,
1688 // and add amount of bytes read after the beginning of that packet
1689 // divided by input bps. This will be inaccurate if the input/output
1690 // ratio is not constant for every audio packet or if it is constant
1691 // but not accurately known in sh_audio->i_bps.
1693 a_pts = d_audio->pts;
1694 // ds_tell_pts returns bytes read after last timestamp from
1695 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1696 // it has read but not decoded
1697 if (sh_audio->i_bps)
1698 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1699 (double)sh_audio->i_bps;
1701 // Now a_pts hopefully holds the pts for end of audio from decoder.
1702 // Substract data in buffers between decoder and audio out.
1704 // Decoded but not filtered
1705 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1707 // Data buffered in audio filters, measured in bytes of "missing" output
1708 buffered_output = af_calc_delay(sh_audio->afilter);
1710 // Data that was ready for ao but was buffered because ao didn't fully
1711 // accept everything to internal buffers yet
1712 buffered_output += sh_audio->a_out_buffer_len;
1714 // Filters divide audio length by playback_speed, so multiply by it
1715 // to get the length in original units without speedup or slowdown
1716 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1718 return a_pts + mpctx->video_offset;
1721 // Return pts value corresponding to currently playing audio.
1722 double playing_audio_pts(struct MPContext *mpctx)
1724 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1725 mpctx->audio_out->get_delay();
1728 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1729 struct MPOpts *opts = &mpctx->opts;
1730 // check for frame-drop:
1731 current_module = "check_framedrop";
1732 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1733 static int dropped_frames;
1734 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1735 float d = delay-mpctx->delay;
1736 ++total_frame_cnt;
1737 // we should avoid dropping too many frames in sequence unless we
1738 // are too late. and we allow 100ms A-V delay here:
1739 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1740 && !mpctx->update_video_immediately) {
1741 ++drop_frame_cnt;
1742 ++dropped_frames;
1743 return frame_dropping;
1744 } else
1745 dropped_frames = 0;
1747 return 0;
1751 #ifdef HAVE_RTC
1752 int rtc_fd = -1;
1753 #endif
1755 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1757 #ifdef HAVE_RTC
1758 if (rtc_fd >= 0){
1759 // -------- RTC -----------
1760 current_module="sleep_rtc";
1761 while (time_frame > 0.000) {
1762 unsigned long rtc_ts;
1763 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1764 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1765 time_frame -= get_relative_time(mpctx);
1767 } else
1768 #endif
1770 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1771 // unnecessarily high CPU usage
1772 float margin = softsleep ? 0.011 : 0;
1773 current_module = "sleep_timer";
1774 while (time_frame > margin) {
1775 usec_sleep(1000000 * (time_frame - margin));
1776 time_frame -= get_relative_time(mpctx);
1778 if (softsleep){
1779 current_module = "sleep_soft";
1780 if (time_frame < 0)
1781 mp_tmsg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1782 while (time_frame > 0)
1783 time_frame -= get_relative_time(mpctx); // burn the CPU
1786 return time_frame;
1789 #ifdef CONFIG_DVDNAV
1790 #ifndef FF_B_TYPE
1791 #define FF_B_TYPE 3
1792 #endif
1793 /// store decoded video image
1794 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1795 mp_image_t *from_mpi) {
1796 mp_image_t *mpi;
1798 /// Do not store B-frames
1799 if (from_mpi->pict_type == FF_B_TYPE)
1800 return to_mpi;
1802 if (to_mpi &&
1803 to_mpi->w == from_mpi->w &&
1804 to_mpi->h == from_mpi->h &&
1805 to_mpi->imgfmt == from_mpi->imgfmt)
1806 mpi = to_mpi;
1807 else {
1808 if (to_mpi)
1809 free_mp_image(to_mpi);
1810 if (from_mpi->w == 0 || from_mpi->h == 0)
1811 return NULL;
1812 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1815 copy_mpi(mpi,from_mpi);
1816 return mpi;
1819 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1820 struct MPOpts *opts = &ctx->opts;
1821 if (ctx->sh_video) {
1822 /// clear video pts
1823 ctx->d_video->pts = 0.0f;
1824 ctx->sh_video->pts = 0.0f;
1825 ctx->sh_video->i_pts = 0.0f;
1826 ctx->sh_video->last_pts = 0.0f;
1827 ctx->sh_video->num_buffered_pts = 0;
1828 ctx->sh_video->num_frames = 0;
1829 ctx->sh_video->num_frames_decoded = 0;
1830 ctx->sh_video->timer = 0.0f;
1831 ctx->sh_video->stream_delay = 0.0f;
1832 ctx->sh_video->timer = 0;
1833 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1836 if (ctx->sh_audio) {
1837 /// free audio packets and reset
1838 ds_free_packs(ctx->d_audio);
1839 audio_delay -= ctx->sh_audio->stream_delay;
1840 ctx->delay =- audio_delay;
1841 ctx->audio_out->reset();
1842 resync_audio_stream(ctx->sh_audio);
1845 if (ctx->d_sub) opts->sub_id = -2;
1847 audio_delay = 0.0f;
1849 /// clear all EOF related flags
1850 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1853 /// Restore last decoded DVDNAV (still frame)
1854 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1855 int *in_size,
1856 unsigned char **start,
1857 mp_image_t *decoded_frame)
1859 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1860 return decoded_frame;
1862 /// a change occured in dvdnav stream
1863 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1864 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1865 mp_dvdnav_context_free(mpctx);
1866 mp_dvdnav_reset_stream(mpctx);
1867 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1868 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1871 if (*in_size < 0) {
1872 float len;
1874 /// Display still frame, if any
1875 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1876 decoded_frame = mpctx->nav_smpi;
1878 /// increment video frame : continue playing after still frame
1879 len = demuxer_get_time_length(mpctx->demuxer);
1880 if (mpctx->sh_video->pts >= len &&
1881 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1882 mp_dvdnav_skip_still(mpctx->stream);
1883 mp_dvdnav_skip_wait(mpctx->stream);
1885 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1887 if (mpctx->nav_buffer) {
1888 *start = mpctx->nav_start;
1889 *in_size = mpctx->nav_in_size;
1890 if (mpctx->nav_start)
1891 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1895 return decoded_frame;
1898 /// Save last decoded DVDNAV (still frame)
1899 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1900 unsigned char *start,
1901 mp_image_t *decoded_frame)
1903 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1904 return;
1906 if (mpctx->nav_buffer)
1907 free(mpctx->nav_buffer);
1909 mpctx->nav_buffer = malloc(in_size);
1910 mpctx->nav_start = start;
1911 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1912 if (mpctx->nav_buffer)
1913 memcpy(mpctx->nav_buffer,start,in_size);
1915 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1916 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1918 #endif /* CONFIG_DVDNAV */
1920 /* Modify video timing to match the audio timeline. There are two main
1921 * reasons this is needed. First, video and audio can start from different
1922 * positions at beginning of file or after a seek (MPlayer starts both
1923 * immediately even if they have different pts). Second, the file can have
1924 * audio timestamps that are inconsistent with the duration of the audio
1925 * packets, for example two consecutive timestamp values differing by
1926 * one second but only a packet with enough samples for half a second
1927 * of playback between them.
1929 static void adjust_sync(struct MPContext *mpctx, double frame_time)
1931 struct MPOpts *opts = &mpctx->opts;
1932 current_module = "av_sync";
1934 if (!mpctx->sh_audio)
1935 return;
1937 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
1938 double v_pts = mpctx->sh_video->pts;
1939 double av_delay = a_pts - v_pts;
1940 // Try to sync vo_flip() so it will *finish* at given time
1941 av_delay += mpctx->last_vo_flip_duration;
1942 av_delay -= audio_delay; // This much pts difference is desired
1944 double change = av_delay * 0.1;
1945 double max_change = default_max_pts_correction >= 0 ?
1946 default_max_pts_correction : frame_time * 0.1;
1947 if (change < -max_change)
1948 change = -max_change;
1949 else if (change > max_change)
1950 change = max_change;
1951 mpctx->delay += change;
1952 mpctx->total_avsync_change += change;
1955 static int fill_audio_out_buffers(struct MPContext *mpctx)
1957 struct MPOpts *opts = &mpctx->opts;
1958 unsigned int t;
1959 double tt;
1960 int playsize;
1961 int playflags=0;
1962 int audio_eof=0;
1963 int bytes_to_write;
1964 sh_audio_t * const sh_audio = mpctx->sh_audio;
1966 current_module="play_audio";
1968 while (1) {
1969 int sleep_time;
1970 // all the current uses of ao_data.pts seem to be in aos that handle
1971 // sync completely wrong; there should be no need to use ao_data.pts
1972 // in get_space()
1973 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1974 bytes_to_write = mpctx->audio_out->get_space();
1975 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
1976 break;
1978 // handle audio-only case:
1979 // this is where mplayer sleeps during audio-only playback
1980 // to avoid 100% CPU use
1981 sleep_time = (ao_data.outburst - bytes_to_write) * 1000 / ao_data.bps;
1982 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
1983 usec_sleep(sleep_time * 1000);
1986 while (bytes_to_write) {
1987 playsize = bytes_to_write;
1988 if (playsize > MAX_OUTBURST)
1989 playsize = MAX_OUTBURST;
1990 bytes_to_write -= playsize;
1992 // Fill buffer if needed:
1993 current_module="decode_audio";
1994 t = GetTimer();
1995 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
1996 if (mpctx->d_audio->eof) {
1997 audio_eof = 1;
1998 if (sh_audio->a_out_buffer_len == 0)
1999 return 0;
2001 t = GetTimer() - t;
2002 tt = t*0.000001f; audio_time_usage+=tt;
2003 if (playsize > sh_audio->a_out_buffer_len) {
2004 playsize = sh_audio->a_out_buffer_len;
2005 if (audio_eof)
2006 playflags |= AOPLAY_FINAL_CHUNK;
2008 if (!playsize)
2009 break;
2011 // play audio:
2012 current_module="play_audio";
2014 // Is this pts value actually useful for the aos that access it?
2015 // They're obviously badly broken in the way they handle av sync;
2016 // would not having access to this make them more broken?
2017 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2018 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2020 if (playsize > 0) {
2021 sh_audio->a_out_buffer_len -= playsize;
2022 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2023 sh_audio->a_out_buffer_len);
2024 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2026 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2027 // Sanity check to avoid hanging in case current ao doesn't output
2028 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2029 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2030 sh_audio->a_out_buffer_len = 0;
2033 return 1;
2036 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2037 float *aq_sleep_time)
2039 struct MPOpts *opts = &mpctx->opts;
2040 int frame_time_remaining = 0;
2041 current_module="calc_sleep_time";
2043 *time_frame -= get_relative_time(mpctx); // reset timer
2045 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2046 float delay = mpctx->audio_out->get_delay();
2047 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2049 if (autosync) {
2051 * Adjust this raw delay value by calculating the expected
2052 * delay for this frame and generating a new value which is
2053 * weighted between the two. The higher autosync is, the
2054 * closer to the delay value gets to that which "-nosound"
2055 * would have used, and the longer it will take for A/V
2056 * sync to settle at the right value (but it eventually will.)
2057 * This settling time is very short for values below 100.
2059 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2060 float difference = delay - predicted;
2061 delay = predicted + difference / (float)autosync;
2064 *time_frame = delay - mpctx->delay / opts->playback_speed;
2066 // delay = amount of audio buffered in soundcard/driver
2067 if (delay > 0.25) delay=0.25; else
2068 if (delay < 0.10) delay=0.10;
2069 if (*time_frame > delay*0.6) {
2070 // sleep time too big - may cause audio drops (buffer underrun)
2071 frame_time_remaining = 1;
2072 *time_frame = delay*0.5;
2074 } else {
2075 // If we're lagging more than 200 ms behind the right playback rate,
2076 // don't try to "catch up".
2077 // If benchmark is set always output frames as fast as possible
2078 // without sleeping.
2079 if (*time_frame < -0.2 || benchmark)
2080 *time_frame = 0;
2083 *aq_sleep_time += *time_frame;
2086 //============================== SLEEP: ===================================
2088 // flag 256 means: libvo driver does its timing (dvb card)
2089 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2090 *time_frame = timing_sleep(mpctx, *time_frame);
2091 return frame_time_remaining;
2094 int reinit_video_chain(struct MPContext *mpctx)
2096 struct MPOpts *opts = &mpctx->opts;
2097 sh_video_t * const sh_video = mpctx->sh_video;
2098 double ar=-1.0;
2099 //================== Init VIDEO (codec & libvo) ==========================
2100 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2101 current_module="preinit_libvo";
2103 //shouldn't we set dvideo->id=-2 when we fail?
2104 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2105 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2106 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
2107 goto err_out;
2109 mpctx->initialized_flags|=INITIALIZED_VO;
2112 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2113 mpctx->sh_video->stream_aspect = ar;
2114 current_module="init_video_filters";
2116 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2117 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2119 #ifdef CONFIG_MENU
2120 if(use_menu) {
2121 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2122 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2123 if(!vf_menu) {
2124 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2125 use_menu = 0;
2128 if(vf_menu)
2129 sh_video->vfilter = vf_menu;
2130 #endif
2132 #ifdef CONFIG_ASS
2133 if(ass_enabled) {
2134 int i;
2135 int insert = 1;
2136 if (opts->vf_settings)
2137 for (i = 0; opts->vf_settings[i].name; ++i)
2138 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2139 insert = 0;
2140 break;
2142 if (insert) {
2143 extern vf_info_t vf_info_ass;
2144 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2145 char* vf_arg[] = {"auto", "1", NULL};
2146 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2147 if (vf_ass)
2148 sh_video->vfilter = vf_ass;
2149 else
2150 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2153 #endif
2155 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2157 #ifdef CONFIG_ASS
2158 if (ass_enabled)
2159 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2160 #endif
2162 current_module="init_video_codec";
2164 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2165 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2166 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2168 if(!sh_video->initialized){
2169 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2170 goto err_out;
2173 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2175 if (sh_video->codec)
2176 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2178 sh_video->last_pts = MP_NOPTS_VALUE;
2179 sh_video->num_buffered_pts = 0;
2180 sh_video->next_frame_time = 0;
2182 if(auto_quality>0){
2183 // Auto quality option enabled
2184 output_quality=get_video_quality_max(sh_video);
2185 if(auto_quality>output_quality) auto_quality=output_quality;
2186 else output_quality=auto_quality;
2187 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2188 set_video_quality(sh_video,output_quality);
2191 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2193 current_module="init_vo";
2195 return 1;
2197 err_out:
2198 mpctx->sh_video = mpctx->d_video->sh = NULL;
2199 return 0;
2202 static double update_video_nocorrect_pts(struct MPContext *mpctx,
2203 int *blit_frame)
2205 struct sh_video *sh_video = mpctx->sh_video;
2206 *blit_frame = 0;
2207 double frame_time = 0;
2208 while (1) {
2209 current_module = "filter_video";
2210 // In nocorrect-pts mode there is no way to properly time these frames
2211 if (vf_output_queued_frame(sh_video->vfilter))
2212 break;
2213 unsigned char *packet = NULL;
2214 frame_time = sh_video->next_frame_time;
2215 if (mpctx->update_video_immediately)
2216 frame_time = 0;
2217 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2218 &packet, force_fps);
2219 if (in_size < 0) {
2220 #ifdef CONFIG_DVDNAV
2221 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2222 if (mp_dvdnav_is_eof(mpctx->stream))
2223 return -1;
2224 if (mpctx->d_video)
2225 mpctx->d_video->eof = 0;
2226 if (mpctx->d_audio)
2227 mpctx->d_audio->eof = 0;
2228 mpctx->stream->eof = 0;
2229 } else
2230 #endif
2231 return -1;
2233 if (in_size > max_framesize)
2234 max_framesize = in_size;
2235 sh_video->timer += frame_time;
2236 if (mpctx->sh_audio)
2237 mpctx->delay -= frame_time;
2238 // video_read_frame can change fps (e.g. for ASF video)
2239 vo_fps = sh_video->fps;
2240 int framedrop_type = check_framedrop(mpctx, frame_time);
2241 current_module = "decode video";
2243 void *decoded_frame;
2244 #ifdef CONFIG_DVDNAV
2245 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2246 if (in_size >= 0 && !decoded_frame)
2247 #endif
2248 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2249 sh_video->pts);
2250 #ifdef CONFIG_DVDNAV
2251 // Save last still frame for future display
2252 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2253 #endif
2254 if (decoded_frame) {
2255 // These updates are done here for vf_expand OSD/subtitles
2256 update_subtitles(sh_video, mpctx->d_sub, mpctx->video_offset, 0);
2257 update_teletext(sh_video, mpctx->demuxer, 0);
2258 update_osd_msg(mpctx);
2259 current_module = "filter video";
2260 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2261 break;
2264 *blit_frame = 1;
2265 return frame_time;
2268 static double update_video(struct MPContext *mpctx, int *blit_frame)
2270 struct sh_video *sh_video = mpctx->sh_video;
2271 *blit_frame = 0;
2272 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2273 mpctx->osd); // hack for vf_expand
2274 if (!mpctx->opts.correct_pts)
2275 return update_video_nocorrect_pts(mpctx, blit_frame);
2277 double pts;
2279 while (1) {
2280 current_module = "filter_video";
2281 // XXX Time used in this call is not counted in any performance
2282 // timer now, OSD time is not updated correctly for filter-added frames
2283 if (vf_output_queued_frame(sh_video->vfilter))
2284 break;
2285 unsigned char *packet = NULL;
2286 bool hit_eof = false;
2287 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2288 if (pts != MP_NOPTS_VALUE)
2289 pts += mpctx->video_offset;
2290 if (in_size < 0) {
2291 // try to extract last frames in case of decoder lag
2292 in_size = 0;
2293 pts = 1e300;
2294 hit_eof = true;
2296 if (in_size > max_framesize)
2297 max_framesize = in_size;
2298 current_module = "decode video";
2299 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2300 void *decoded_frame = decode_video(sh_video, packet, in_size,
2301 framedrop_type, pts);
2302 if (decoded_frame) {
2303 // These updates are done here for vf_expand OSD/subtitles
2304 update_subtitles(sh_video, mpctx->d_sub, mpctx->video_offset, 0);
2305 update_teletext(sh_video, mpctx->demuxer, 0);
2306 update_osd_msg(mpctx);
2307 current_module = "filter video";
2308 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2309 break;
2310 } else if (hit_eof)
2311 return -1;
2314 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_GET_PTS, &pts);
2315 if (pts == MP_NOPTS_VALUE) {
2316 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2317 // Try to use decoder pts from before filters
2318 pts = sh_video->pts;
2319 if (pts == MP_NOPTS_VALUE)
2320 pts = sh_video->last_pts;
2322 sh_video->pts = pts;
2323 if (sh_video->last_pts == MP_NOPTS_VALUE)
2324 sh_video->last_pts = sh_video->pts;
2325 else if (sh_video->last_pts > sh_video->pts) {
2326 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2327 sh_video->pts, sh_video->last_pts);
2328 /* If the difference in pts is small treat it as jitter around the
2329 * right value (possibly caused by incorrect timestamp ordering) and
2330 * just show this frame immediately after the last one.
2331 * Treat bigger differences as timestamp resets and start counting
2332 * timing of later frames from the position of this one. */
2333 if (sh_video->last_pts - sh_video->pts > 0.5)
2334 sh_video->last_pts = sh_video->pts;
2335 else
2336 sh_video->pts = sh_video->last_pts;
2338 double frame_time = sh_video->pts - sh_video->last_pts;
2339 sh_video->last_pts = sh_video->pts;
2340 sh_video->timer += frame_time;
2341 if (mpctx->sh_audio)
2342 mpctx->delay -= frame_time;
2343 *blit_frame = 1;
2344 return frame_time;
2347 void pause_player(struct MPContext *mpctx)
2349 if (mpctx->paused)
2350 return;
2351 mpctx->paused = 1;
2352 mpctx->step_frames = 0;
2353 mpctx->time_frame -= get_relative_time(mpctx);
2355 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2356 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2358 if (mpctx->audio_out && mpctx->sh_audio)
2359 mpctx->audio_out->pause(); // pause audio, keep data if possible
2362 void unpause_player(struct MPContext *mpctx)
2364 if (!mpctx->paused)
2365 return;
2366 mpctx->paused = 0;
2368 if (mpctx->audio_out && mpctx->sh_audio)
2369 mpctx->audio_out->resume(); // resume audio
2370 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2371 && !mpctx->step_frames)
2372 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2373 (void)get_relative_time(mpctx); // ignore time that passed during pause
2376 void add_step_frame(struct MPContext *mpctx)
2378 mpctx->step_frames++;
2379 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2380 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2381 unpause_player(mpctx);
2384 static void pause_loop(struct MPContext *mpctx)
2386 mp_cmd_t* cmd;
2387 if (!quiet) {
2388 // Small hack to display the pause message on the OSD line.
2389 // The pause string is: "\n == PAUSE == \r" so we need to
2390 // take the first and the last char out
2391 if (term_osd && !mpctx->sh_video) {
2392 char msg[128] = _(MSGTR_Paused);
2393 int mlen = strlen(msg);
2394 msg[mlen-1] = '\0';
2395 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2396 update_osd_msg(mpctx);
2397 } else
2398 mp_tmsg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2399 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2401 #ifdef CONFIG_GUI
2402 if (use_gui)
2403 guiGetEvent(guiCEvent, (char *)guiSetPause);
2404 #endif
2406 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2407 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2408 if (cmd) {
2409 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2410 run_command(mpctx, cmd);
2411 mp_cmd_free(cmd);
2412 continue;
2414 if (mpctx->sh_video && mpctx->video_out)
2415 vo_check_events(mpctx->video_out);
2416 #ifdef CONFIG_GUI
2417 if (use_gui) {
2418 guiEventHandling();
2419 guiGetEvent(guiReDraw, NULL);
2420 if (guiIntfStruct.Playing!=2 || (mpctx->rel_seek_secs || mpctx->abs_seek_pos))
2421 break;
2423 #endif
2424 #ifdef CONFIG_MENU
2425 if (vf_menu)
2426 vf_menu_pause_update(vf_menu);
2427 #endif
2428 usec_sleep(20000);
2429 update_osd_msg(mpctx);
2430 int hack = vo_osd_changed(0);
2431 vo_osd_changed(hack);
2432 if (hack)
2433 break;
2435 #ifdef CONFIG_GUI
2436 if (use_gui) {
2437 if (guiIntfStruct.Playing == guiSetStop)
2438 mpctx->eof = 1;
2439 else
2440 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2442 #endif
2446 // Find the right mute status and record position for new file position
2447 static void edl_seek_reset(MPContext *mpctx)
2449 mpctx->edl_muted = 0;
2450 next_edl_record = edl_records;
2452 while (next_edl_record) {
2453 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2454 break;
2456 if (next_edl_record->action == EDL_MUTE)
2457 mpctx->edl_muted = !mpctx->edl_muted;
2458 next_edl_record = next_edl_record->next;
2460 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2461 mixer_mute(&mpctx->mixer);
2465 // Execute EDL command for the current position if one exists
2466 static void edl_update(MPContext *mpctx)
2468 if (!next_edl_record)
2469 return;
2471 if (!mpctx->sh_video) {
2472 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2473 free_edl(edl_records);
2474 next_edl_record = NULL;
2475 edl_records = NULL;
2476 return;
2479 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2480 if (next_edl_record->action == EDL_SKIP) {
2481 mpctx->osd_function = OSD_FFW;
2482 mpctx->abs_seek_pos = 0;
2483 mpctx->rel_seek_secs = next_edl_record->length_sec;
2484 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2485 "[%f], length [%f]\n", next_edl_record->start_sec,
2486 next_edl_record->stop_sec, next_edl_record->length_sec);
2488 else if (next_edl_record->action == EDL_MUTE) {
2489 mpctx->edl_muted = !mpctx->edl_muted;
2490 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2491 mixer_mute(&mpctx->mixer);
2492 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2493 next_edl_record->start_sec );
2495 next_edl_record = next_edl_record->next;
2499 static void reinit_decoders(struct MPContext *mpctx)
2501 reinit_video_chain(mpctx);
2502 reinit_audio_chain(mpctx);
2503 mp_property_do("sub", M_PROPERTY_SET, &mpctx->global_sub_pos, mpctx);
2506 static bool timeline_set_part(struct MPContext *mpctx, int i)
2508 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
2509 struct timeline_part *n = mpctx->timeline + i;
2510 mpctx->timeline_part = i;
2511 mpctx->video_offset = n->start - n->source_start;
2512 if (n->source == p->source)
2513 return false;
2514 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo && mpctx->opts.video_id != -2 ? 0 : INITIALIZED_VO) | INITIALIZED_AO | INITIALIZED_ACODEC);
2515 mpctx->demuxer = n->source->demuxer;
2516 mpctx->d_video = mpctx->demuxer->video;
2517 mpctx->d_audio = mpctx->demuxer->audio;
2518 mpctx->d_sub = mpctx->demuxer->sub;
2519 mpctx->sh_video = mpctx->d_video->sh;
2520 mpctx->sh_audio = mpctx->d_audio->sh;
2521 return true;
2524 // Given pts, switch playback to the corresponding part.
2525 // Return offset within that part.
2526 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
2527 bool *need_reset)
2529 if (pts < 0)
2530 pts = 0;
2531 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
2532 struct timeline_part *p = mpctx->timeline + i;
2533 if (pts < (p+1)->start) {
2534 *need_reset = timeline_set_part(mpctx, i);
2535 return pts - p->start + p->source_start;
2538 return -1;
2542 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2543 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2544 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2545 static int seek(MPContext *mpctx, double amount, int style)
2547 current_module = "seek";
2548 if (mpctx->stop_play == AT_END_OF_FILE)
2549 mpctx->stop_play = KEEP_PLAYING;
2550 if (mpctx->timeline && style & SEEK_FACTOR) {
2551 amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
2552 style &= ~SEEK_FACTOR;
2554 if ((mpctx->demuxer->accurate_seek || mpctx->timeline) && mpctx->sh_video
2555 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2556 style |= SEEK_ABSOLUTE;
2557 if (amount > 0)
2558 style |= SEEK_FORWARD;
2559 else
2560 style |= SEEK_BACKWARD;
2561 amount += mpctx->sh_video->pts;
2564 /* At least the liba52 decoder wants to read from the input stream
2565 * during initialization, so reinit must be done after the demux_seek()
2566 * call that clears possible stream EOF. */
2567 bool need_reset = false;
2568 if (mpctx->timeline) {
2569 amount = timeline_set_from_time(mpctx, amount, &need_reset);
2570 if (amount == -1) {
2571 mpctx->stop_play = AT_END_OF_FILE;
2572 // Clear audio from current position
2573 if (mpctx->sh_audio) {
2574 mpctx->audio_out->reset();
2575 mpctx->sh_audio->a_buffer_len = 0;
2576 mpctx->sh_audio->a_out_buffer_len = 0;
2578 return -1;
2581 int seekresult = demux_seek(mpctx->demuxer, amount, audio_delay, style);
2582 if (need_reset)
2583 reinit_decoders(mpctx);
2584 if (seekresult == 0)
2585 return -1;
2587 if (mpctx->sh_video) {
2588 current_module = "seek_video_reset";
2589 resync_video_stream(mpctx->sh_video);
2590 if (mpctx->video_out->config_ok)
2591 vo_control(mpctx->video_out, VOCTRL_RESET, NULL);
2592 mpctx->sh_video->num_buffered_pts = 0;
2593 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2594 mpctx->num_buffered_frames = 0;
2595 mpctx->delay = 0;
2596 mpctx->time_frame = 0;
2597 mpctx->update_video_immediately = true;
2598 // Not all demuxers set d_video->pts during seek, so this value
2599 // (which is used by at least vobsub and edl code below) may
2600 // be completely wrong (probably 0).
2601 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
2602 update_subtitles(mpctx->sh_video, mpctx->d_sub, mpctx->video_offset, 1);
2603 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2606 if (mpctx->sh_audio) {
2607 current_module = "seek_audio_reset";
2608 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2609 mpctx->sh_audio->a_buffer_len = 0;
2610 mpctx->sh_audio->a_out_buffer_len = 0;
2613 if (vo_vobsub && mpctx->sh_video) {
2614 current_module = "seek_vobsub_reset";
2615 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2618 edl_seek_reset(mpctx);
2620 mpctx->total_avsync_change = 0;
2621 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2622 drop_frame_cnt = 0;
2624 current_module = NULL;
2625 return 0;
2628 int get_current_chapter(struct MPContext *mpctx)
2630 if (!mpctx->chapters || !mpctx->sh_video)
2631 return demuxer_get_current_chapter(mpctx->demuxer);
2633 int i;
2634 double current_pts = mpctx->sh_video->pts;
2635 for (i = 1; i < mpctx->num_chapters; i++)
2636 if (current_pts < mpctx->chapters[i].start)
2637 break;
2638 return i - 1;
2641 // currently returns a string allocated with malloc, not talloc
2642 char *chapter_display_name(struct MPContext *mpctx, int chapter)
2644 if (!mpctx->chapters || !mpctx->sh_video)
2645 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
2646 return strdup(mpctx->chapters[chapter].name);
2649 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
2650 char **chapter_name)
2652 if (!mpctx->chapters || !mpctx->sh_video)
2653 return demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
2654 chapter_name);
2655 if (chapter >= mpctx->num_chapters)
2656 return -1;
2657 if (chapter < 0)
2658 chapter = 0;
2659 *seek_pts = mpctx->chapters[chapter].start;
2660 if (chapter_name)
2661 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
2662 return chapter;
2665 static int find_ordered_chapter_sources(struct MPContext *mpctx,
2666 struct content_source *sources,
2667 int num_sources,
2668 unsigned char uid_map[][16])
2670 int num_filenames = 0;
2671 char **filenames = NULL;
2672 if (num_sources > 1) {
2673 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
2674 "other sources.\n");
2675 if (mpctx->stream->type != STREAMTYPE_FILE) {
2676 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
2677 "normal disk file. Will not search for related files.\n");
2678 } else {
2679 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
2680 "same directory to find referenced sources.\n");
2681 filenames = find_files(mpctx->demuxer->filename, ".mkv",
2682 &num_filenames);
2686 int num_left = num_sources - 1;
2687 for (int i = 0; i < num_filenames && num_left > 0; i++) {
2688 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
2689 filename_recode(filenames[i]));
2690 int format;
2691 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
2692 if (!s)
2693 continue;
2694 struct demuxer *d = demux_open(&mpctx->opts, s, DEMUXER_TYPE_MATROSKA,
2695 mpctx->opts.audio_id,
2696 mpctx->opts.video_id,
2697 mpctx->opts.sub_id, filenames[i]);
2698 if (!d) {
2699 free_stream(s);
2700 continue;
2702 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
2703 for (int i = 1; i < num_sources; i++) {
2704 if (sources[i].demuxer)
2705 continue;
2706 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
2707 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
2708 i, filename_recode(d->filename));
2709 sources[i].stream = s;
2710 sources[i].demuxer = d;
2711 num_left--;
2712 goto match;
2716 free_demuxer(d);
2717 free_stream(s);
2718 continue;
2719 match:
2722 talloc_free(filenames);
2723 if (num_left) {
2724 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
2725 "There will be parts MISSING from the video!\n");
2726 for (int i = 1, j = 1; i < num_sources; i++)
2727 if (sources[i].demuxer) {
2728 sources[j] = sources[i];
2729 memcpy(uid_map[j], uid_map[i], 16);
2730 j++;
2733 return num_sources - num_left;
2736 static void build_ordered_chapter_timeline(struct MPContext *mpctx)
2738 if (!mpctx->opts.ordered_chapters) {
2739 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
2740 "you have disabled support for them. Ignoring.\n");
2741 return;
2744 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
2745 "edit timeline.\n");
2747 struct demuxer *demuxer = mpctx->demuxer;
2748 struct matroska_data *m = &demuxer->matroska_data;
2750 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
2751 m->num_ordered_chapters);
2752 sources[0].stream = mpctx->stream;
2753 sources[0].demuxer = mpctx->demuxer;
2754 unsigned char uid_map[m->num_ordered_chapters][16];
2755 int num_sources = 1;
2756 memcpy(uid_map[0], m->segment_uid, 16);
2758 for (int i = 0; i < m->num_ordered_chapters; i++) {
2759 struct matroska_chapter *c = m->ordered_chapters + i;
2760 if (!c->has_segment_uid)
2761 memcpy(c->segment_uid, m->segment_uid, 16);
2763 for (int j = 0; j < num_sources; j++)
2764 if (!memcmp(c->segment_uid, uid_map[j], 16))
2765 goto found1;
2766 memcpy(uid_map[num_sources], c->segment_uid, 16);
2767 sources[num_sources] = (struct content_source){};
2768 num_sources++;
2769 found1:
2773 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
2774 uid_map);
2777 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
2778 m->num_ordered_chapters + 1);
2779 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
2780 m->num_ordered_chapters);
2781 uint64_t starttime = 0;
2782 uint64_t missing_time = 0;
2783 int part_count = 0;
2784 int num_chapters = 0;
2785 uint64_t prev_part_offset;
2786 for (int i = 0; i < m->num_ordered_chapters; i++) {
2787 struct matroska_chapter *c = m->ordered_chapters + i;
2789 int j;
2790 for (j = 0; j < num_sources; j++) {
2791 if (!memcmp(c->segment_uid, uid_map[j], 16))
2792 goto found2;
2794 missing_time += c->end - c->start;
2795 continue;
2796 found2:;
2797 chapters[num_chapters].start = starttime / 1000.;
2798 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
2799 /* Only add a separate part if the time or file actually changes.
2800 * Matroska files have chapter divisions that are redundant from
2801 * timeline point of view because the same chapter structure is used
2802 * both to specify the timeline and for normal chapter information.
2803 * Removing a missing inserted external chapter can also cause this. */
2804 if (part_count == 0 || c->start != starttime + prev_part_offset
2805 || sources + j != timeline[part_count - 1].source) {
2806 timeline[part_count].source = sources + j;
2807 timeline[part_count].start = chapters[num_chapters].start;
2808 timeline[part_count].source_start = c->start / 1000.;
2809 prev_part_offset = c->start - starttime;
2810 part_count++;
2812 starttime += c->end - c->start;
2813 num_chapters++;
2815 timeline[part_count].start = starttime / 1000.;
2817 if (!part_count) {
2818 // None of the parts come from the file itself???
2819 talloc_free(sources);
2820 talloc_free(timeline);
2821 talloc_free(chapters);
2822 return;
2825 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
2826 "sources. Total length %.3f seconds.\n", part_count, num_sources,
2827 timeline[part_count].start);
2828 if (missing_time)
2829 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
2830 "from the timeline!\n", missing_time / 1000.);
2831 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
2832 for (int i = 0; i < num_sources; i++)
2833 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
2834 filename_recode(sources[i].demuxer->filename));
2835 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
2836 "source_start, source):\n");
2837 for (int i = 0; i < part_count; i++) {
2838 struct timeline_part *p = timeline + i;
2839 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
2840 p->source_start, p->source - sources);
2842 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start);
2843 mpctx->sources = sources;
2844 mpctx->num_sources = num_sources;
2845 mpctx->timeline = timeline;
2846 mpctx->num_timeline_parts = part_count;
2847 mpctx->num_chapters = num_chapters;
2848 mpctx->chapters = chapters;
2850 mpctx->timeline_part = 0;
2851 mpctx->video_offset = timeline[0].source_start;
2852 mpctx->demuxer = timeline[0].source->demuxer;
2856 static int read_keys(void *ctx, int fd)
2858 getch2(ctx);
2859 return mplayer_get_key(ctx, 0);
2863 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2864 * file for some tools to link against. */
2865 #ifndef DISABLE_MAIN
2866 int main(int argc,char* argv[]){
2869 char * mem_ptr;
2871 // movie info:
2873 /* Flag indicating whether MPlayer should exit without playing anything. */
2874 int opt_exit = 0;
2876 //float a_frame=0; // Audio
2878 int i;
2880 int gui_no_filename=0;
2882 struct MPContext *mpctx = &(struct MPContext){
2883 .osd_function = OSD_PLAY,
2884 .begin_skip = MP_NOPTS_VALUE,
2885 .play_tree_step = 1,
2886 .global_sub_pos = -1,
2887 .set_of_sub_pos = -1,
2888 .file_format = DEMUXER_TYPE_UNKNOWN,
2889 .last_dvb_step = 1,
2892 InitTimer();
2893 srand(GetTimerMS());
2895 mp_msg_init();
2897 #ifdef CONFIG_X11
2898 mpctx->x11_state = vo_x11_init_state();
2899 #endif
2900 struct MPOpts *opts = &mpctx->opts;
2901 set_default_mplayer_options(opts);
2902 // Create the config context and register the options
2903 mpctx->mconfig = m_config_new(opts, cfg_include);
2904 m_config_register_options(mpctx->mconfig,mplayer_opts);
2905 mp_input_register_options(mpctx->mconfig);
2907 // Preparse the command line
2908 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2910 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2911 set_path_env();
2912 #endif
2914 #ifdef CONFIG_TV
2915 stream_tv_defaults.immediate = 1;
2916 #endif
2918 if (argc > 1 && argv[1] &&
2919 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2920 use_gui = !strcmp(argv[1], "-gui");
2921 } else
2922 if ( argv[0] )
2924 char *base = strrchr(argv[0], '/');
2925 if (!base)
2926 base = strrchr(argv[0], '\\');
2927 if (!base)
2928 base = argv[0];
2929 if(strstr(base, "gmplayer"))
2930 use_gui=1;
2933 parse_cfgfiles(mpctx, mpctx->mconfig);
2935 #ifdef CONFIG_GUI
2936 if ( use_gui ) cfg_read();
2937 #endif
2939 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2940 if(mpctx->playtree == NULL)
2941 opt_exit = 1;
2942 else {
2943 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2944 if(mpctx->playtree) {
2945 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2946 if(mpctx->playtree_iter) {
2947 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2948 play_tree_iter_free(mpctx->playtree_iter);
2949 mpctx->playtree_iter = NULL;
2951 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2955 mpctx->key_fifo = mp_fifo_create(opts);
2957 print_version("MPlayer");
2959 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_GUI)
2960 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2961 if(runningmplayer && mpctx->filename && use_gui){
2962 COPYDATASTRUCT csData;
2963 char file[MAX_PATH];
2964 char *filepart = mpctx->filename;
2965 if(GetFullPathName(mpctx->filename, MAX_PATH, file, &filepart)){
2966 csData.dwData = 0;
2967 csData.cbData = strlen(file)*2;
2968 csData.lpData = file;
2969 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2972 #endif
2974 #if defined(__MINGW32__) || defined(__CYGWIN__)
2975 // stop Windows from showing all kinds of annoying error dialogs
2976 SetErrorMode(0x8003);
2977 // request 1ms timer resolution
2978 timeBeginPeriod(1);
2979 #endif
2981 #ifdef CONFIG_PRIORITY
2982 set_priority();
2983 #endif
2985 #ifndef CONFIG_GUI
2986 if(use_gui){
2987 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2988 use_gui=0;
2990 #else
2991 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
2992 if(use_gui && !vo_init()){
2993 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2994 use_gui=0;
2996 #endif
2997 if (use_gui && mpctx->playtree_iter){
2998 char cwd[PATH_MAX+2];
2999 // Free Playtree and Playtree-Iter as it's not used by the GUI.
3000 play_tree_iter_free(mpctx->playtree_iter);
3001 mpctx->playtree_iter=NULL;
3003 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
3005 strcat(cwd, "/");
3006 // Prefix relative paths with current working directory
3007 play_tree_add_bpf(mpctx->playtree, cwd);
3009 // Import initital playtree into GUI.
3010 import_initial_playtree_into_gui(mpctx->playtree, mpctx->mconfig, enqueue);
3012 #endif /* CONFIG_GUI */
3014 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
3015 list_video_out();
3016 opt_exit = 1;
3019 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
3020 list_audio_out();
3021 opt_exit = 1;
3024 /* Check codecs.conf. */
3025 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3026 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3027 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3028 if(!parse_codec_cfg(NULL)){
3029 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3031 mp_tmsg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
3034 free( mem_ptr ); // release the buffer created by get_path()
3037 #if 0
3038 if(video_codec_list){
3039 int i;
3040 video_codec=video_codec_list[0];
3041 for(i=0;video_codec_list[i];i++)
3042 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
3044 #endif
3045 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3046 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
3047 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3048 list_codecs(1);
3049 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3050 opt_exit = 1;
3052 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3053 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
3054 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3055 list_codecs(0);
3056 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3057 opt_exit = 1;
3059 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3060 vfm_help();
3061 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3062 opt_exit = 1;
3064 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3065 afm_help();
3066 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3067 opt_exit = 1;
3069 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3070 af_help();
3071 printf("\n");
3072 opt_exit = 1;
3074 #ifdef CONFIG_X11
3075 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3076 fstype_help();
3077 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3078 opt_exit = 1;
3080 #endif
3081 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3082 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3083 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3084 demuxer_help();
3085 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3086 opt_exit = 1;
3088 if(list_properties) {
3089 property_print_help();
3090 opt_exit = 1;
3093 if(opt_exit)
3094 exit_player(mpctx, EXIT_NONE);
3096 if (player_idle_mode && use_gui) {
3097 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
3098 exit_player_with_rc(mpctx, EXIT_NONE, 1);
3101 if(!mpctx->filename && !player_idle_mode){
3102 if(!use_gui){
3103 // no file/vcd/dvd -> show HELP:
3104 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
3105 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3106 } else gui_no_filename=1;
3109 /* Display what configure line was used */
3110 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3112 // Many users forget to include command line in bugreports...
3113 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3114 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
3115 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3116 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3119 //------ load global data first ------
3121 mpctx->osd = osd_create();
3123 // check font
3124 #ifdef CONFIG_FREETYPE
3125 init_freetype();
3126 #endif
3127 #ifdef CONFIG_FONTCONFIG
3128 if(font_fontconfig <= 0)
3130 #endif
3131 #ifdef CONFIG_BITMAP_FONT
3132 if(font_name){
3133 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3134 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
3135 filename_recode(font_name));
3136 } else {
3137 // try default:
3138 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3139 free(mem_ptr); // release the buffer created by get_path()
3140 if(!vo_font)
3141 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3143 if (sub_font_name)
3144 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3145 else
3146 mpctx->osd->sub_font = vo_font;
3147 #endif
3148 #ifdef CONFIG_FONTCONFIG
3150 #endif
3152 #ifdef CONFIG_ASS
3153 ass_library = ass_init();
3154 #endif
3156 #ifdef HAVE_RTC
3157 if(!nortc)
3159 // seteuid(0); /* Can't hurt to try to get root here */
3160 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3161 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
3162 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3163 else {
3164 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3166 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3167 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
3168 mp_tmsg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
3169 close (rtc_fd);
3170 rtc_fd = -1;
3171 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3172 /* variable only by the root */
3173 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
3174 close (rtc_fd);
3175 rtc_fd = -1;
3176 } else
3177 mp_tmsg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
3180 #ifdef CONFIG_GUI
3181 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
3182 // and now ? -- Pontscho
3183 if(use_gui) setuid( getuid() ); // strongly test, please check this.
3184 #endif
3185 if(rtc_fd<0)
3186 #endif /* HAVE_RTC */
3187 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3188 softsleep?"software":timer_name);
3190 #ifdef HAVE_TERMCAP
3191 if ( !use_gui ) load_termcap(NULL); // load key-codes
3192 #endif
3194 // ========== Init keyboard FIFO (connection to libvo) ============
3196 // Init input system
3197 current_module = "init_input";
3198 mpctx->input = mp_input_init(&opts->input, use_gui);
3199 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3200 if(slave_mode)
3201 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
3202 else if(!noconsolecontrols)
3203 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
3204 // Set the libstream interrupt callback
3205 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
3207 #ifdef CONFIG_MENU
3208 if(use_menu) {
3209 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3210 mp_tmsg(MSGT_CPLAYER, MSGL_V, MSGTR_MenuInitialized, menu_cfg);
3211 else {
3212 menu_cfg = get_path("menu.conf");
3213 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3214 mp_tmsg(MSGT_CPLAYER, MSGL_V, MSGTR_MenuInitialized, menu_cfg);
3215 else {
3216 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
3217 MPLAYER_CONFDIR "/menu.conf"))
3218 mp_tmsg(MSGT_CPLAYER, MSGL_V, MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
3219 else {
3220 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, MSGTR_MenuInitFailed);
3221 use_menu = 0;
3226 #endif
3228 current_module = NULL;
3230 /// Catch signals
3231 #ifndef __MINGW32__
3232 signal(SIGCHLD,child_sighandler);
3233 #endif
3235 #ifdef CONFIG_CRASH_DEBUG
3236 prog_path = argv[0];
3237 #endif
3238 //========= Catch terminate signals: ================
3239 // terminate requests:
3240 signal(SIGTERM,exit_sighandler); // kill
3241 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3243 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3245 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3246 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3247 #ifdef CONFIG_SIGHANDLER
3248 // fatal errors:
3249 signal(SIGBUS,exit_sighandler); // bus error
3250 signal(SIGSEGV,exit_sighandler); // segfault
3251 signal(SIGILL,exit_sighandler); // illegal instruction
3252 signal(SIGFPE,exit_sighandler); // floating point exc.
3253 signal(SIGABRT,exit_sighandler); // abort()
3254 #ifdef CONFIG_CRASH_DEBUG
3255 if (crash_debug)
3256 signal(SIGTRAP,exit_sighandler);
3257 #endif
3258 #endif
3260 #ifdef CONFIG_GUI
3261 if(use_gui){
3262 guiInit();
3263 guiGetEvent(guiSetContext, mpctx);
3264 mpctx->initialized_flags|=INITIALIZED_GUI;
3265 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
3267 #endif
3269 // ******************* Now, let's see the per-file stuff ********************
3271 play_next_file:
3273 // init global sub numbers
3274 mpctx->global_sub_size = 0;
3275 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3277 if (mpctx->filename) {
3278 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
3279 load_per_extension_config (mpctx->mconfig, mpctx->filename);
3280 load_per_file_config (mpctx->mconfig, mpctx->filename);
3283 if (opts->video_driver_list)
3284 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
3285 if (opts->audio_driver_list)
3286 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
3288 // We must enable getch2 here to be able to interrupt network connection
3289 // or cache filling
3290 if(!noconsolecontrols && !slave_mode){
3291 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
3292 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
3293 else
3294 getch2_enable(); // prepare stdin for hotkeys...
3295 mpctx->initialized_flags|=INITIALIZED_GETCH2;
3296 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3299 // =================== GUI idle loop (STOP state) ===========================
3300 #ifdef CONFIG_GUI
3301 if ( use_gui ) {
3302 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
3303 guiGetEvent( guiSetDefaults,0 );
3304 while ( guiIntfStruct.Playing != 1 )
3306 mp_cmd_t* cmd;
3307 usec_sleep(20000);
3308 guiEventHandling();
3309 guiGetEvent( guiReDraw,NULL );
3310 if ( (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
3311 guiGetEvent(guiIEvent, (char *)cmd->id);
3312 mp_cmd_free(cmd);
3315 guiGetEvent( guiSetParameters,NULL );
3316 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
3318 play_tree_t * entry = play_tree_new();
3319 play_tree_add_file( entry,guiIntfStruct.Filename );
3320 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
3321 else mpctx->playtree=play_tree_new();
3322 play_tree_set_child( mpctx->playtree,entry );
3323 if(mpctx->playtree)
3325 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3326 if(mpctx->playtree_iter)
3328 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
3330 play_tree_iter_free(mpctx->playtree_iter);
3331 mpctx->playtree_iter = NULL;
3333 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3338 #endif /* CONFIG_GUI */
3340 while (player_idle_mode && !mpctx->filename) {
3341 play_tree_t * entry = NULL;
3342 mp_cmd_t * cmd;
3343 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3344 if (mpctx->video_out)
3345 vo_check_events(mpctx->video_out);
3346 usec_sleep(20000);
3348 switch (cmd->id) {
3349 case MP_CMD_LOADFILE:
3350 // prepare a tree entry with the new filename
3351 entry = play_tree_new();
3352 play_tree_add_file(entry, cmd->args[0].v.s);
3353 // The entry is added to the main playtree after the switch().
3354 break;
3355 case MP_CMD_LOADLIST:
3356 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3357 break;
3358 case MP_CMD_QUIT:
3359 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3360 break;
3361 case MP_CMD_GET_PROPERTY:
3362 case MP_CMD_SET_PROPERTY:
3363 case MP_CMD_STEP_PROPERTY:
3364 run_command(mpctx, cmd);
3365 break;
3368 mp_cmd_free(cmd);
3370 if (entry) { // user entered a command that gave a valid entry
3371 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3372 play_tree_free_list(mpctx->playtree->child, 1);
3373 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3375 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3377 play_tree_set_child(mpctx->playtree, entry);
3379 /* Make iterator start at the top the of tree. */
3380 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3381 if (!mpctx->playtree_iter) continue;
3383 // find the first real item in the tree
3384 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3385 // no items!
3386 play_tree_iter_free(mpctx->playtree_iter);
3387 mpctx->playtree_iter = NULL;
3388 continue; // wait for next command
3390 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3393 //---------------------------------------------------------------------------
3395 if(mpctx->filename)
3396 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
3397 filename_recode(mpctx->filename));
3399 if (edl_filename) {
3400 if (edl_records) free_edl(edl_records);
3401 next_edl_record = edl_records = edl_parse_file();
3403 if (edl_output_filename) {
3404 if (edl_fd) fclose(edl_fd);
3405 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3407 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
3408 filename_recode(edl_output_filename));
3412 //==================== Open VOB-Sub ============================
3414 current_module="vobsub";
3415 if (vobsub_name){
3416 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3417 if(vo_vobsub==NULL)
3418 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
3419 filename_recode(vobsub_name));
3420 } else if (sub_auto && mpctx->filename){
3421 /* try to autodetect vobsub from movie filename ::atmos */
3422 char *buf = strdup(mpctx->filename), *psub;
3423 char *pdot = strrchr(buf, '.');
3424 char *pslash = strrchr(buf, '/');
3425 #if defined(__MINGW32__) || defined(__CYGWIN__)
3426 if (!pslash) pslash = strrchr(buf, '\\');
3427 #endif
3428 if (pdot && (!pslash || pdot > pslash))
3429 *pdot = '\0';
3430 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3431 /* try from ~/.mplayer/sub */
3432 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3433 char *bname;
3434 int l;
3435 bname = strrchr(buf,'/');
3436 #if defined(__MINGW32__) || defined(__CYGWIN__)
3437 if(!bname) bname = strrchr(buf,'\\');
3438 #endif
3439 if(bname) bname++;
3440 else bname = buf;
3441 l = strlen(psub) + strlen(bname) + 1;
3442 psub = realloc(psub,l);
3443 strcat(psub,bname);
3444 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3445 free(psub);
3447 free(buf);
3449 if(vo_vobsub){
3450 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3451 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3452 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3454 // setup global sub numbering
3455 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3456 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3459 //============ Open & Sync STREAM --- fork cache2 ====================
3461 mpctx->stream=NULL;
3462 mpctx->demuxer=NULL;
3463 if (mpctx->d_audio) {
3464 //free_demuxer_stream(mpctx->d_audio);
3465 mpctx->d_audio=NULL;
3467 if (mpctx->d_video) {
3468 //free_demuxer_stream(d_video);
3469 mpctx->d_video=NULL;
3471 mpctx->sh_audio=NULL;
3472 mpctx->sh_video=NULL;
3474 current_module="open_stream";
3475 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3476 if(!mpctx->stream) { // error...
3477 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3478 goto goto_next_file;
3480 mpctx->initialized_flags|=INITIALIZED_STREAM;
3482 #ifdef CONFIG_GUI
3483 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3484 #endif
3486 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3487 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3488 "playlist support will not be used automatically.\n"
3489 "MPlayer's playlist code is unsafe and should only be used with "
3490 "trusted sources.\nPlayback will probably fail.\n\n");
3491 #if 0
3492 play_tree_t* entry;
3493 // Handle playlist
3494 current_module="handle_playlist";
3495 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3496 filename_recode(mpctx->filename));
3497 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3498 mpctx->eof=playtree_add_playlist(mpctx, entry);
3499 goto goto_next_file;
3500 #endif
3502 mpctx->stream->start_pos+=seek_to_byte;
3504 if(stream_dump_type==5){
3505 unsigned char buf[4096];
3506 int len;
3507 FILE *f;
3508 current_module="dumpstream";
3509 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3510 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
3511 exit_player(mpctx, EXIT_ERROR);
3513 stream_reset(mpctx->stream);
3514 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3515 f=fopen(stream_dump_name,"wb");
3516 if(!f){
3517 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3518 exit_player(mpctx, EXIT_ERROR);
3520 if (dvd_chapter > 1) {
3521 int chapter = dvd_chapter - 1;
3522 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3524 while(!mpctx->stream->eof && !async_quit_request){
3525 len=stream_read(mpctx->stream,buf,4096);
3526 if(len>0) {
3527 if(fwrite(buf,len,1,f) != 1) {
3528 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3529 exit_player(mpctx, EXIT_ERROR);
3532 if(dvd_last_chapter > 0) {
3533 int chapter = -1;
3534 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3535 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3536 break;
3539 if(fclose(f)) {
3540 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3541 exit_player(mpctx, EXIT_ERROR);
3543 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3544 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3547 #ifdef CONFIG_DVDREAD
3548 if(mpctx->stream->type==STREAMTYPE_DVD){
3549 current_module="dvd lang->id";
3550 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3551 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3552 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3553 // setup global sub numbering
3554 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3555 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3556 current_module=NULL;
3558 #endif
3560 #ifdef CONFIG_DVDNAV
3561 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3562 current_module="dvdnav lang->id";
3563 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3564 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3565 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3566 // setup global sub numbering
3567 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3568 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3569 current_module=NULL;
3571 #endif
3573 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3574 goto_enable_cache:
3575 if(stream_cache_size>0){
3576 current_module="enable_cache";
3577 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3578 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3579 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3580 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3583 //============ Open DEMUXERS --- DETECT file type =======================
3584 current_module="demux_open";
3586 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3588 // HACK to get MOV Reference Files working
3590 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3592 unsigned char* playlist_entry;
3593 play_tree_t *list = NULL, *entry = NULL;
3595 current_module="handle_demux_playlist";
3596 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3598 char *temp, *bname;
3600 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3601 filename_recode(playlist_entry));
3603 bname=mp_basename(playlist_entry);
3604 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3605 continue;
3607 if (!strncmp(bname,mp_basename(mpctx->filename),strlen(bname))) // ignoring self-reference
3608 continue;
3610 entry = play_tree_new();
3612 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3614 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3615 if (temp)
3617 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3618 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3619 strcat(temp, playlist_entry);
3620 play_tree_add_file(entry,temp);
3621 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3622 free(temp);
3625 else
3626 play_tree_add_file(entry,playlist_entry);
3628 if(!list)
3629 list = entry;
3630 else
3631 play_tree_append_entry(list,entry);
3633 free_demuxer(mpctx->demuxer);
3634 mpctx->demuxer = NULL;
3636 if (list)
3638 entry = play_tree_new();
3639 play_tree_set_child(entry,list);
3640 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3641 goto goto_next_file;
3645 if(!mpctx->demuxer)
3646 goto goto_next_file;
3648 if (mpctx->demuxer->matroska_data.ordered_chapters)
3649 build_ordered_chapter_timeline(mpctx);
3651 if (!mpctx->sources) {
3652 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
3653 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
3654 .demuxer = mpctx->demuxer};
3655 mpctx->num_sources = 1;
3658 if(dvd_chapter>1) {
3659 double pts;
3660 if (seek_chapter(mpctx, dvd_chapter-1, &pts, NULL) >= 0 && pts > -1.0)
3661 seek(mpctx, pts, SEEK_ABSOLUTE);
3664 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3666 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3667 int i;
3668 int maxid = -1;
3669 // setup global sub numbering
3670 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3671 for (i = 0; i < MAX_S_STREAMS; i++)
3672 if (mpctx->demuxer->s_streams[i])
3673 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3674 mpctx->global_sub_size += maxid + 1;
3676 // Make opts->sub_id always selectable if set.
3677 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3678 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3680 #ifdef CONFIG_ASS
3681 if (ass_enabled && ass_library) {
3682 for (int j = 0; j < mpctx->num_sources; j++) {
3683 struct demuxer *d = mpctx->sources[j].demuxer;
3684 for (int i = 0; i < d->num_attachments; i++) {
3685 struct demux_attachment *att = d->attachments + i;
3686 if (extract_embedded_fonts
3687 && att->name && att->type && att->data && att->data_size
3688 && (strcmp(att->type, "application/x-truetype-font") == 0
3689 || strcmp(att->type, "application/x-font") == 0))
3690 ass_add_font(ass_library, att->name, att->data, att->data_size);
3694 #endif
3696 current_module="demux_open2";
3698 //file_format=demuxer->file_format;
3700 mpctx->d_audio=mpctx->demuxer->audio;
3701 mpctx->d_video=mpctx->demuxer->video;
3702 mpctx->d_sub=mpctx->demuxer->sub;
3704 // select audio stream
3705 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3707 // DUMP STREAMS:
3708 if((stream_dump_type)&&(stream_dump_type<4)){
3709 FILE *f;
3710 demux_stream_t *ds=NULL;
3711 current_module="dump";
3712 // select stream to dump
3713 switch(stream_dump_type){
3714 case 1: ds=mpctx->d_audio;break;
3715 case 2: ds=mpctx->d_video;break;
3716 case 3: ds=mpctx->d_sub;break;
3718 if(!ds){
3719 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3720 exit_player(mpctx, EXIT_ERROR);
3722 // disable other streams:
3723 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3724 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3725 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3726 // let's dump it!
3727 f=fopen(stream_dump_name,"wb");
3728 if(!f){
3729 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3730 exit_player(mpctx, EXIT_ERROR);
3732 while(!ds->eof){
3733 unsigned char* start;
3734 int in_size=ds_get_packet(ds,&start);
3735 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3736 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3737 if(in_size>0) fwrite(start,in_size,1,f);
3738 if(dvd_last_chapter>0) {
3739 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3740 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3741 break;
3744 fclose(f);
3745 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3746 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3749 mpctx->sh_audio=mpctx->d_audio->sh;
3750 mpctx->sh_video=mpctx->d_video->sh;
3752 if(mpctx->sh_video){
3754 current_module="video_read_properties";
3755 if(!video_read_properties(mpctx->sh_video)) {
3756 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3757 mpctx->sh_video=mpctx->d_video->sh=NULL;
3758 } else {
3759 mp_tmsg(MSGT_CPLAYER,MSGL_V,MSGTR_FilefmtFourccSizeFpsFtime,
3760 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3761 mpctx->sh_video->fps,mpctx->sh_video->frametime
3764 /* need to set fps here for output encoders to pick it up in their init */
3765 if(force_fps){
3766 mpctx->sh_video->fps=force_fps;
3767 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3769 vo_fps = mpctx->sh_video->fps;
3771 if(!mpctx->sh_video->fps && !force_fps){
3772 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3773 mpctx->sh_video=mpctx->d_video->sh=NULL;
3779 if(!mpctx->sh_video && !mpctx->sh_audio){
3780 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3781 #ifdef CONFIG_DVBIN
3782 if(mpctx->stream->type == STREAMTYPE_DVB)
3784 int dir;
3785 int v = mpctx->last_dvb_step;
3786 if(v > 0)
3787 dir = DVB_CHANNEL_HIGHER;
3788 else
3789 dir = DVB_CHANNEL_LOWER;
3791 if(dvb_step_channel(mpctx->stream, dir)) {
3792 mpctx->stop_play = PT_NEXT_ENTRY;
3793 mpctx->dvbin_reopen = 1;
3796 #endif
3797 goto goto_next_file; // exit_player(_(MSGTR_Exit_error));
3800 /* display clip info */
3801 demux_info_print(mpctx->demuxer);
3803 //================== Read SUBTITLES (DVD & TEXT) ==========================
3804 if(vo_spudec==NULL && mpctx->sh_video &&
3805 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3806 init_vo_spudec(mpctx);
3809 if(mpctx->sh_video) {
3810 // after reading video params we should load subtitles because
3811 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3812 // check .sub
3813 current_module="read_subtitles_file";
3814 if(sub_name){
3815 for (i = 0; sub_name[i] != NULL; ++i)
3816 add_subtitles(mpctx, sub_name[i], mpctx->sh_video->fps, 0);
3818 if(sub_auto) { // auto load sub file ...
3819 char *psub = get_path( "sub/" );
3820 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3821 int i = 0;
3822 free(psub); // release the buffer created by get_path() above
3823 while (tmp[i]) {
3824 add_subtitles(mpctx, tmp[i], mpctx->sh_video->fps, 1);
3825 free(tmp[i++]);
3827 free(tmp);
3829 if (mpctx->set_of_sub_size > 0) {
3830 // setup global sub numbering
3831 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3832 mpctx->global_sub_size += mpctx->set_of_sub_size;
3836 if (mpctx->global_sub_size) {
3837 // find the best sub to use
3838 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3839 mpctx->global_sub_pos = -1; // no subs by default
3840 if (vobsub_index_id >= 0) {
3841 // if user asks for a vobsub id, use that first.
3842 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3843 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3844 // if user asks for a dvd sub id, use that next.
3845 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3846 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3847 // if there are text subs to use, use those. (autosubs come last here)
3848 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3849 } else if (opts->sub_id < 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3850 // finally select subs by language and container hints
3851 if (opts->sub_id < 0 && dvdsub_lang)
3852 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3853 if (opts->sub_id < 0)
3854 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3855 if (opts->sub_id >= 0)
3856 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3858 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3859 mpctx->global_sub_pos--;
3860 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3861 if(subdata)
3862 switch (stream_dump_type) {
3863 case 3: list_sub_file(subdata); break;
3864 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3865 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3866 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3867 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3868 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3872 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3873 filename_recode(mpctx->filename));
3874 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3875 if (mpctx->sh_video) {
3876 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3877 if (mpctx->sh_video->format >= 0x20202020)
3878 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3879 else
3880 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3881 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3882 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3883 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3884 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3885 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3887 if (mpctx->sh_audio) {
3888 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3889 if (mpctx->sh_audio->format >= 0x20202020)
3890 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3891 else
3892 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3893 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3894 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3895 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3897 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3898 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n", mpctx->stream->seek ? 1 : 0);
3899 if (mpctx->demuxer) {
3900 if (mpctx->demuxer->num_chapters == 0)
3901 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3902 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3905 if(!mpctx->sh_video) goto main; // audio-only
3907 if(!reinit_video_chain(mpctx)) {
3908 if(!mpctx->sh_video){
3909 if(!mpctx->sh_audio) goto goto_next_file;
3910 goto main; // exit_player(_(MSGTR_Exit_error));
3914 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3915 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3917 #ifdef CONFIG_FREETYPE
3918 force_load_font = 1;
3919 #endif
3921 //================== MAIN: ==========================
3922 main:
3923 current_module="main";
3925 if(playing_msg) {
3926 char* msg = property_expand_string(mpctx, playing_msg);
3927 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3928 free(msg);
3932 // Disable the term OSD in verbose mode
3933 if(verbose) term_osd = 0;
3936 //int frame_corr_num=0; //
3937 //float v_frame=0; // Video
3938 //float num_frames=0; // number of frames played
3940 int frame_time_remaining=0; // flag
3941 int blit_frame=0;
3942 mpctx->num_buffered_frames=0;
3944 // Make sure old OSD does not stay around,
3945 // e.g. with -fixed-vo and same-resolution files
3946 clear_osd_msgs();
3947 update_osd_msg(mpctx);
3949 //================ SETUP AUDIO ==========================
3951 if(mpctx->sh_audio){
3952 reinit_audio_chain(mpctx);
3953 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3954 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3957 current_module="av_init";
3959 if(mpctx->sh_video){
3960 mpctx->sh_video->timer=0;
3961 if (! ignore_start)
3962 audio_delay += mpctx->sh_video->stream_delay;
3964 if(mpctx->sh_audio){
3965 if (start_volume >= 0)
3966 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3967 if (! ignore_start)
3968 audio_delay -= mpctx->sh_audio->stream_delay;
3969 mpctx->delay=-audio_delay;
3972 if(!mpctx->sh_audio){
3973 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3974 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3975 ds_free_packs(mpctx->d_audio); // free buffered chunks
3976 //mpctx->d_audio->id=-2; // do not read audio chunks
3977 //uninit_player(mpctx, INITIALIZED_AO); // close device
3979 if(!mpctx->sh_video){
3980 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3981 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3982 ds_free_packs(mpctx->d_video);
3983 mpctx->d_video->id=-2;
3984 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3987 if (!mpctx->sh_video && !mpctx->sh_audio)
3988 goto goto_next_file;
3990 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3991 if(force_fps && mpctx->sh_video){
3992 vo_fps = mpctx->sh_video->fps=force_fps;
3993 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3994 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3997 #ifdef CONFIG_GUI
3998 if ( use_gui ) {
3999 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
4000 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
4001 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
4002 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
4003 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
4005 #endif
4007 mp_input_set_section(mpctx->input, NULL);
4008 //TODO: add desired (stream-based) sections here
4009 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
4010 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
4012 //==================== START PLAYING =======================
4014 if(opts->loop_times>1) opts->loop_times--; else
4015 if(opts->loop_times==1) opts->loop_times = -1;
4017 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
4019 total_time_usage_start=GetTimer();
4020 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
4021 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
4022 play_n_frames=play_n_frames_mf;
4024 if(play_n_frames==0){
4025 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
4028 if (seek_to_sec) {
4029 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
4030 end_at.pos += seek_to_sec;
4033 if (end_at.type == END_AT_SIZE) {
4034 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
4035 end_at.type = END_AT_NONE;
4038 #ifdef CONFIG_DVDNAV
4039 mp_dvdnav_context_free(mpctx);
4040 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4041 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
4042 mp_dvdnav_cell_has_changed(mpctx->stream,1);
4044 #endif
4046 get_relative_time(mpctx); // reset current delta
4047 mpctx->time_frame = 0;
4048 mpctx->drop_message_shown = 0;
4049 mpctx->update_video_immediately = true;
4050 mpctx->total_avsync_change = 0;
4051 // Make sure VO knows current pause state
4052 if (mpctx->sh_video)
4053 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
4054 NULL);
4056 while(!mpctx->stop_play){
4057 float aq_sleep_time=0;
4059 if(dvd_last_chapter>0) {
4060 int cur_chapter = get_current_chapter(mpctx);
4061 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
4062 goto goto_next_file;
4065 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
4066 mpctx->sh_audio = mpctx->d_audio->sh;
4067 mpctx->sh_audio->ds = mpctx->d_audio;
4068 reinit_audio_chain(mpctx);
4071 /*========================== PLAY AUDIO ============================*/
4073 if (mpctx->sh_audio && !mpctx->paused)
4074 if (!fill_audio_out_buffers(mpctx))
4075 // at eof, all audio at least written to ao
4076 if (!mpctx->sh_video)
4077 mpctx->stop_play = AT_END_OF_FILE;
4080 if(!mpctx->sh_video) {
4081 // handle audio-only case:
4082 double a_pos=0;
4083 // sh_audio can be NULL due to video stream switching
4084 // TODO: handle this better
4085 if (mpctx->sh_audio)
4086 a_pos = playing_audio_pts(mpctx);
4088 print_status(mpctx, a_pos, false);
4090 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
4091 mpctx->stop_play = PT_NEXT_ENTRY;
4092 update_osd_msg(mpctx);
4094 } else {
4096 /*========================== PLAY VIDEO ============================*/
4098 vo_pts=mpctx->sh_video->timer*90000.0;
4099 vo_fps=mpctx->sh_video->fps;
4101 if (!mpctx->num_buffered_frames) {
4102 double frame_time = update_video(mpctx, &blit_frame);
4103 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
4104 if (mpctx->sh_video->vf_initialized < 0) {
4105 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
4106 mpctx->stop_play = PT_NEXT_ENTRY;
4107 goto goto_next_file;
4109 if (blit_frame) {
4110 struct vf_instance *vf = mpctx->sh_video->vfilter;
4111 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
4112 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
4113 vo_osd_changed(0);
4115 if (frame_time < 0)
4116 mpctx->stop_play = AT_END_OF_FILE;
4117 else {
4118 // might return with !eof && !blit_frame if !correct_pts
4119 mpctx->num_buffered_frames += blit_frame;
4120 if (mpctx->update_video_immediately) {
4121 // Show this frame immediately, rest normally
4122 mpctx->update_video_immediately = false;
4123 } else {
4124 mpctx->time_frame += frame_time / opts->playback_speed;
4125 adjust_sync(mpctx, frame_time);
4129 if (mpctx->timeline) {
4130 struct timeline_part *next = mpctx->timeline + mpctx->timeline_part + 1;
4131 if (mpctx->sh_video->pts >= next->start
4132 || mpctx->stop_play == AT_END_OF_FILE
4133 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
4134 seek(mpctx, next->start, SEEK_ABSOLUTE);
4135 continue;
4139 // ==========================================================================
4141 // current_module="draw_osd";
4142 // if(vo_config_count) mpctx->video_out->draw_osd();
4144 #ifdef CONFIG_GUI
4145 if(use_gui) guiEventHandling();
4146 #endif
4148 current_module="vo_check_events";
4149 vo_check_events(mpctx->video_out);
4151 #ifdef CONFIG_X11
4152 if (stop_xscreensaver) {
4153 current_module = "stop_xscreensaver";
4154 xscreensaver_heartbeat(mpctx->x11_state);
4156 #endif
4157 if (heartbeat_cmd) {
4158 static unsigned last_heartbeat;
4159 unsigned now = GetTimerMS();
4160 if (now - last_heartbeat > 30000) {
4161 last_heartbeat = now;
4162 system(heartbeat_cmd);
4166 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
4168 //====================== FLIP PAGE (VIDEO BLT): =========================
4170 current_module="flip_page";
4171 if (!frame_time_remaining && blit_frame) {
4172 unsigned int t2=GetTimer();
4174 vo_flip_page(mpctx->video_out);
4175 mpctx->num_buffered_frames--;
4177 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
4178 vout_time_usage += mpctx->last_vo_flip_duration;
4179 print_status(mpctx, MP_NOPTS_VALUE, true);
4181 else
4182 print_status(mpctx, MP_NOPTS_VALUE, false);
4184 //============================ Auto QUALITY ============================
4186 /*Output quality adjustments:*/
4187 if(auto_quality>0){
4188 current_module="autoq";
4189 // float total=0.000001f * (GetTimer()-aq_total_time);
4190 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4191 if(output_quality<auto_quality && aq_sleep_time>0)
4192 ++output_quality;
4193 else
4194 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4195 if(output_quality>1 && aq_sleep_time<0)
4196 --output_quality;
4197 else
4198 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4199 output_quality=0;
4200 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4201 set_video_quality(mpctx->sh_video,output_quality);
4204 if (!frame_time_remaining && blit_frame) {
4205 if (play_n_frames >= 0) {
4206 --play_n_frames;
4207 if (play_n_frames <= 0)
4208 mpctx->stop_play = PT_NEXT_ENTRY;
4210 if (mpctx->step_frames > 0) {
4211 mpctx->step_frames--;
4212 if (mpctx->step_frames == 0)
4213 pause_player(mpctx);
4218 // FIXME: add size based support for -endpos
4219 if (end_at.type == END_AT_TIME &&
4220 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
4221 mpctx->stop_play = PT_NEXT_ENTRY;
4223 } // end if(mpctx->sh_video)
4225 #ifdef CONFIG_DVDNAV
4226 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4227 nav_highlight_t hl;
4228 mp_dvdnav_get_highlight (mpctx->stream, &hl);
4229 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4230 vo_osd_changed (OSDTYPE_DVDNAV);
4232 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
4233 double ar = -1.0;
4234 if (stream_control (mpctx->demuxer->stream,
4235 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
4236 != STREAM_UNSUPPORTED)
4237 mpctx->sh_video->stream_aspect = ar;
4240 #endif
4242 //================= Keyboard events, SEEKing ====================
4244 current_module="key_events";
4247 while (1) {
4248 mp_cmd_t* cmd;
4249 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
4250 run_command(mpctx, cmd);
4251 mp_cmd_free(cmd);
4252 if (mpctx->stop_play)
4253 break;
4255 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
4256 || mpctx->abs_seek_pos)
4257 break;
4258 if (mpctx->sh_video) {
4259 update_osd_msg(mpctx);
4260 int hack = vo_osd_changed(0);
4261 vo_osd_changed(hack);
4262 if (hack)
4263 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
4264 add_step_frame(mpctx);
4265 break;
4267 else
4268 vo_osd_changed(0);
4270 pause_loop(mpctx);
4274 // handle -sstep
4275 if (step_sec > 0 && !mpctx->paused) {
4276 mpctx->osd_function=OSD_FFW;
4277 mpctx->rel_seek_secs+=step_sec;
4280 edl_update(mpctx);
4282 /* Looping. */
4283 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
4284 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
4286 if(opts->loop_times>1) opts->loop_times--; else
4287 if(opts->loop_times==1) opts->loop_times=-1;
4288 play_n_frames=play_n_frames_mf;
4289 mpctx->stop_play=0;
4290 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
4293 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
4294 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
4296 mpctx->rel_seek_secs=0;
4297 mpctx->abs_seek_pos=0;
4300 #ifdef CONFIG_GUI
4301 if(use_gui){
4302 guiEventHandling();
4303 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
4304 // get pos from frame number / total frames
4305 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
4306 } else {
4307 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
4309 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
4310 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx);
4311 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
4312 guiGetEvent( guiReDraw,NULL );
4313 guiGetEvent( guiSetVolume,NULL );
4314 if(guiIntfStruct.Playing==0) break; // STOP
4315 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
4316 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
4317 #ifdef CONFIG_DVDREAD
4318 if ( mpctx->stream->type == STREAMTYPE_DVD )
4320 dvd_priv_t * dvdp = mpctx->stream->priv;
4321 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
4323 #endif
4325 #endif /* CONFIG_GUI */
4327 } // while(!mpctx->stop_play)
4329 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4331 #ifdef CONFIG_DVBIN
4332 if(mpctx->dvbin_reopen)
4334 mpctx->stop_play = 0;
4335 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4336 cache_uninit(mpctx->stream);
4337 mpctx->dvbin_reopen = 0;
4338 goto goto_enable_cache;
4340 #endif
4343 goto_next_file: // don't jump here after ao/vo/getch initialization!
4345 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4347 if(benchmark){
4348 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4349 double total_time_usage;
4350 total_time_usage_start=GetTimer()-total_time_usage_start;
4351 total_time_usage = (float)total_time_usage_start*0.000001;
4352 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4353 video_time_usage,vout_time_usage,audio_time_usage,
4354 total_time_usage-tot,total_time_usage);
4355 if(total_time_usage>0.0)
4356 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4357 100.0*video_time_usage/total_time_usage,
4358 100.0*vout_time_usage/total_time_usage,
4359 100.0*audio_time_usage/total_time_usage,
4360 100.0*(total_time_usage-tot)/total_time_usage,
4361 100.0);
4362 if(total_frame_cnt && frame_dropping)
4363 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4364 total_frame_cnt-drop_frame_cnt,
4365 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4366 drop_frame_cnt,
4367 100*drop_frame_cnt/total_frame_cnt,
4368 total_frame_cnt,
4369 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4372 // time to uninit all, except global stuff:
4373 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI+(opts->fixed_vo?INITIALIZED_VO:0)));
4375 if(mpctx->set_of_sub_size > 0) {
4376 current_module="sub_free";
4377 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4378 sub_free(mpctx->set_of_subtitles[i]);
4379 #ifdef CONFIG_ASS
4380 if(mpctx->set_of_ass_tracks[i])
4381 ass_free_track( mpctx->set_of_ass_tracks[i] );
4382 #endif
4384 mpctx->set_of_sub_size = 0;
4386 vo_sub_last = vo_sub=NULL;
4387 subdata=NULL;
4388 #ifdef CONFIG_ASS
4389 ass_track = NULL;
4390 if(ass_library)
4391 ass_clear_fonts(ass_library);
4392 #endif
4394 if (!mpctx->stop_play) // In case some goto jumped here...
4395 mpctx->stop_play = PT_NEXT_ENTRY;
4397 int playtree_direction = 1;
4399 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4400 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4401 play_tree_iter_free(mpctx->playtree_iter);
4402 mpctx->playtree_iter = NULL;
4404 mpctx->play_tree_step = 1;
4405 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4406 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4407 if(mpctx->playtree_iter) {
4408 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4409 play_tree_iter_free(mpctx->playtree_iter);
4410 mpctx->playtree_iter = NULL;
4413 } else if (mpctx->stop_play == PT_STOP) {
4414 play_tree_iter_free(mpctx->playtree_iter);
4415 mpctx->playtree_iter = NULL;
4416 } else { // NEXT PREV SRC
4417 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4420 while(mpctx->playtree_iter != NULL) {
4421 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4422 if(mpctx->filename == NULL) {
4423 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4424 play_tree_iter_free(mpctx->playtree_iter);
4425 mpctx->playtree_iter = NULL;
4427 } else
4428 break;
4431 #ifdef CONFIG_GUI
4432 if(use_gui && !mpctx->playtree_iter) {
4433 #ifdef CONFIG_DVDREAD
4434 if(!guiIntfStruct.DiskChanged)
4435 #endif
4436 mplEnd();
4438 #endif
4440 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4441 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4442 mpctx->stop_play = 0;
4443 goto play_next_file;
4447 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4449 return 1;
4451 #endif /* DISABLE_MAIN */