Add option -noordered-chapters.
[mplayer/glamo.git] / mplayer.c
blob8bcaa3430bdea4749ed1340fe8eba66f01101451
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->num_chapters = 0;
584 mpctx->video_offset = 0;
585 if(mpctx->demuxer){
586 mpctx->stream=mpctx->demuxer->stream;
587 free_demuxer(mpctx->demuxer);
589 mpctx->demuxer=NULL;
592 // kill the cache process:
593 if(mask&INITIALIZED_STREAM){
594 mpctx->initialized_flags&=~INITIALIZED_STREAM;
595 current_module="uninit_stream";
596 if(mpctx->stream) free_stream(mpctx->stream);
597 mpctx->stream=NULL;
600 if(mask&INITIALIZED_VO){
601 mpctx->initialized_flags&=~INITIALIZED_VO;
602 current_module="uninit_vo";
603 vo_destroy(mpctx->video_out);
604 mpctx->video_out=NULL;
605 #ifdef CONFIG_DVDNAV
606 mp_dvdnav_context_free(mpctx);
607 #endif
610 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
611 if(mask&INITIALIZED_GETCH2){
612 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
613 current_module="uninit_getch2";
614 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
615 // restore terminal:
616 getch2_disable();
619 if(mask&INITIALIZED_VOBSUB){
620 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
621 current_module="uninit_vobsub";
622 if(vo_vobsub) vobsub_close(vo_vobsub);
623 vo_vobsub=NULL;
626 if (mask&INITIALIZED_SPUDEC){
627 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
628 current_module="uninit_spudec";
629 spudec_free(vo_spudec);
630 vo_spudec=NULL;
633 if(mask&INITIALIZED_AO){
634 mpctx->initialized_flags&=~INITIALIZED_AO;
635 current_module="uninit_ao";
636 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
637 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
638 mpctx->audio_out=NULL;
641 #ifdef CONFIG_GUI
642 if(mask&INITIALIZED_GUI){
643 mpctx->initialized_flags&=~INITIALIZED_GUI;
644 current_module="uninit_gui";
645 guiDone();
647 #endif
649 current_module=NULL;
652 void exit_player_with_rc(struct MPContext *mpctx, exit_reason_t how, int rc)
654 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
655 uninit_player(mpctx, INITIALIZED_ALL);
656 #if defined(__MINGW32__) || defined(__CYGWIN__)
657 timeEndPeriod(1);
658 #endif
659 #ifdef CONFIG_X11
660 #ifdef CONFIG_GUI
661 if ( !use_gui )
662 #endif
663 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
664 #endif
666 current_module="uninit_input";
667 mp_input_uninit(mpctx->input);
668 #ifdef CONFIG_MENU
669 if (use_menu)
670 menu_uninit();
671 #endif
673 #ifdef CONFIG_FREETYPE
674 current_module="uninit_font";
675 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
676 free_font_desc(mpctx->osd->sub_font);
677 free_font_desc(vo_font);
678 vo_font = NULL;
679 done_freetype();
680 #endif
681 osd_free(mpctx->osd);
683 #ifdef CONFIG_ASS
684 ass_library_done(ass_library);
685 #endif
687 current_module="exit_player";
689 // free mplayer config
690 if(mpctx->mconfig)
691 m_config_free(mpctx->mconfig);
693 if(mpctx->playtree)
694 play_tree_free(mpctx->playtree, 1);
696 talloc_free(mpctx->key_fifo);
698 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
699 switch(how) {
700 case EXIT_QUIT:
701 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_quit);
702 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
703 break;
704 case EXIT_EOF:
705 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_eof);
706 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
707 break;
708 case EXIT_ERROR:
709 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_ExitingHow,MSGTR_Exit_error);
710 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
711 break;
712 default:
713 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
715 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
717 exit(rc);
720 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
722 exit_player_with_rc(mpctx, how, 1);
725 #ifndef __MINGW32__
726 static void child_sighandler(int x){
727 pid_t pid;
728 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
730 #endif
732 #ifdef CONFIG_CRASH_DEBUG
733 static char *prog_path;
734 static int crash_debug = 0;
735 #endif
737 static void exit_sighandler(int x){
738 static int sig_count=0;
739 #ifdef CONFIG_CRASH_DEBUG
740 if (!crash_debug || x != SIGTRAP)
741 #endif
742 ++sig_count;
743 if(sig_count==5)
745 /* We're crashing bad and can't uninit cleanly :(
746 * by popular request, we make one last (dirty)
747 * effort to restore the user's
748 * terminal. */
749 getch2_disable();
750 exit(1);
752 if(sig_count==6) exit(1);
753 if(sig_count>6){
754 // can't stop :(
755 #ifndef __MINGW32__
756 kill(getpid(),SIGKILL);
757 #endif
759 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
760 current_module?current_module:"unknown"
762 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
763 if(sig_count<=1)
764 switch(x){
765 case SIGINT:
766 case SIGQUIT:
767 case SIGTERM:
768 case SIGKILL:
769 async_quit_request = 1;
770 return; // killed from keyboard (^C) or killed [-9]
771 case SIGILL:
772 #ifdef RUNTIME_CPUDETECT
773 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL_RTCpuSel);
774 #else
775 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGILL);
776 #endif
777 case SIGFPE:
778 case SIGSEGV:
779 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
780 default:
781 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
782 #ifdef CONFIG_CRASH_DEBUG
783 if (crash_debug) {
784 int gdb_pid;
785 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
786 gdb_pid = fork();
787 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
788 if (gdb_pid == 0) { // We are the child
789 char spid[20];
790 snprintf(spid, sizeof(spid), "%i", getppid());
791 getch2_disable(); // allow terminal to work properly with gdb
792 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
793 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
794 } else if (gdb_pid < 0)
795 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
796 else {
797 waitpid(gdb_pid, NULL, 0);
799 if (x == SIGTRAP) return;
801 #endif
803 getch2_disable();
804 exit(1);
807 #include "cfg-mplayer.h"
809 static int cfg_include(m_option_t *conf, char *filename)
811 return m_config_parse_config_file(conf->priv, filename);
814 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
816 char *conffile;
817 int conffile_fd;
818 if (!disable_system_conf &&
819 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
820 exit_player(mpctx, EXIT_NONE);
821 if ((conffile = get_path("")) == NULL) {
822 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoHomeDir);
823 } else {
824 #ifdef __MINGW32__
825 mkdir(conffile);
826 #else
827 mkdir(conffile, 0777);
828 #endif
829 free(conffile);
830 if ((conffile = get_path("config")) == NULL) {
831 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
832 } else {
833 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
834 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CreatingCfgFile, conffile);
835 write(conffile_fd, default_config, strlen(default_config));
836 close(conffile_fd);
838 if (!disable_user_conf &&
839 m_config_parse_config_file(conf, conffile) < 0)
840 exit_player(mpctx, EXIT_NONE);
841 free(conffile);
846 #define PROFILE_CFG_PROTOCOL "protocol."
848 static void load_per_protocol_config (m_config_t* conf, const char *const file)
850 char *str;
851 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
852 m_profile_t *p;
854 /* does filename actually uses a protocol ? */
855 str = strstr (file, "://");
856 if (!str)
857 return;
859 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
860 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
861 p = m_config_get_profile (conf, protocol);
862 if (p)
864 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingProtocolProfile, protocol);
865 m_config_set_profile(conf,p);
869 #define PROFILE_CFG_EXTENSION "extension."
871 static void load_per_extension_config (m_config_t* conf, const char *const file)
873 char *str;
874 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
875 m_profile_t *p;
877 /* does filename actually have an extension ? */
878 str = strrchr (file, '.');
879 if (!str)
880 return;
882 sprintf (extension, PROFILE_CFG_EXTENSION);
883 strncat (extension, ++str, 7);
884 p = m_config_get_profile (conf, extension);
885 if (p)
887 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, extension);
888 m_config_set_profile(conf,p);
892 #define PROFILE_CFG_VO "vo."
893 #define PROFILE_CFG_AO "ao."
895 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
897 char profile[strlen (cfg) + strlen (out) + 1];
898 m_profile_t *p;
900 sprintf (profile, "%s%s", cfg, out);
901 p = m_config_get_profile (conf, profile);
902 if (p)
904 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingExtensionProfile, profile);
905 m_config_set_profile(conf,p);
909 static void load_per_file_config (m_config_t* conf, const char *const file)
911 char *confpath;
912 char cfg[strlen(file)+10];
913 struct stat st;
914 char *name;
916 sprintf (cfg, "%s.conf", file);
918 if (use_filedir_conf && !stat (cfg, &st))
920 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, cfg);
921 m_config_parse_config_file (conf, cfg);
922 return;
925 if ((name = strrchr (cfg, '/')) == NULL)
926 name = cfg;
927 else
928 name++;
930 if ((confpath = get_path (name)) != NULL)
932 if (!stat (confpath, &st))
934 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_LoadingConfig, confpath);
935 m_config_parse_config_file (conf, confpath);
938 free (confpath);
942 /* When libmpdemux performs a blocking operation (network connection or
943 * cache filling) if the operation fails we use this function to check
944 * if it was interrupted by the user.
945 * The function returns a new value for eof. */
946 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
948 mp_cmd_t* cmd;
949 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
950 switch(cmd->id) {
951 case MP_CMD_QUIT:
952 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
953 case MP_CMD_PLAY_TREE_STEP: {
954 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
955 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
956 } break;
957 case MP_CMD_PLAY_TREE_UP_STEP: {
958 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
959 } break;
960 case MP_CMD_PLAY_ALT_SRC_STEP: {
961 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
962 } break;
964 mp_cmd_free(cmd);
966 return stop_play;
969 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
971 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
973 play_tree_add_bpf(entry,mpctx->filename);
975 #ifdef CONFIG_GUI
976 if (use_gui) {
977 if (entry) {
978 import_playtree_playlist_into_gui(entry, mpctx->mconfig);
979 play_tree_free_list(entry,1);
981 } else
982 #endif
984 if(!entry) {
985 entry = mpctx->playtree_iter->tree;
986 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
987 return PT_NEXT_ENTRY;
989 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
990 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
991 return PT_NEXT_ENTRY;
994 play_tree_remove(entry,1,1);
995 return PT_NEXT_SRC;
997 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
998 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
999 entry = mpctx->playtree_iter->tree;
1000 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1001 return PT_NEXT_ENTRY;
1003 play_tree_remove(entry,1,1);
1005 return PT_NEXT_SRC;
1008 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1010 sub_data *subd;
1011 #ifdef CONFIG_ASS
1012 ass_track_t *asst = 0;
1013 #endif
1015 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1016 return;
1019 subd = sub_read_file(filename, fps);
1020 #ifdef CONFIG_ASS
1021 if (ass_enabled)
1022 #ifdef CONFIG_ICONV
1023 asst = ass_read_file(ass_library, filename, sub_cp);
1024 #else
1025 asst = ass_read_file(ass_library, filename, 0);
1026 #endif
1027 if (ass_enabled && subd && !asst)
1028 asst = ass_read_subdata(ass_library, subd, fps);
1030 if (!asst && !subd)
1031 #else
1032 if(!subd)
1033 #endif
1034 mp_msg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, MSGTR_CantLoadSub,
1035 filename_recode(filename));
1037 #ifdef CONFIG_ASS
1038 if (!asst && !subd) return;
1039 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1040 #else
1041 if (!subd) return;
1042 #endif
1043 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1044 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1045 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1046 filename_recode(filename));
1047 ++mpctx->set_of_sub_size;
1048 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, mpctx->set_of_sub_size,
1049 filename_recode(filename));
1052 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1053 void update_set_of_subtitles(struct MPContext *mpctx)
1054 // subdata was changed, set_of_sub... have to be updated.
1056 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
1057 int i;
1058 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1059 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
1060 set_of_subtitles[i-1] = set_of_subtitles[i];
1061 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
1062 --mpctx->set_of_sub_size;
1063 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
1065 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1066 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
1068 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1069 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1070 ++mpctx->set_of_sub_size;
1074 void init_vo_spudec(struct MPContext *mpctx)
1076 if (vo_spudec)
1077 spudec_free(vo_spudec);
1078 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1079 vo_spudec = NULL;
1080 if (spudec_ifo) {
1081 unsigned int palette[16], width, height;
1082 current_module="spudec_init_vobsub";
1083 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1084 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1087 #ifdef CONFIG_DVDREAD
1088 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1089 current_module="spudec_init_dvdread";
1090 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1091 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1092 NULL, 0);
1094 #endif
1096 #ifdef CONFIG_DVDNAV
1097 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1098 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1099 current_module="spudec_init_dvdnav";
1100 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1102 #endif
1104 if (vo_spudec==NULL) {
1105 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1106 current_module="spudec_init_normal";
1107 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1108 spudec_set_font_factor(vo_spudec,font_factor);
1111 if (vo_spudec!=NULL)
1112 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1116 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1117 * make it all work is to use the builtin SDL-bootstrap code, which
1118 * will be done automatically by replacing our main() if we include SDL.h.
1120 #if defined(__APPLE__) && defined(CONFIG_SDL)
1121 #include <SDL.h>
1122 #endif
1125 * \brief append a formatted string
1126 * \param buf buffer to print into
1127 * \param pos position of terminating 0 in buf
1128 * \param len maximum number of characters in buf, not including terminating 0
1129 * \param format printf format string
1131 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1133 va_list va;
1134 va_start(va, format);
1135 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1136 va_end(va);
1137 if (*pos >= len ) {
1138 buf[len] = 0;
1139 *pos = len;
1144 * \brief append time in the hh:mm:ss.f format
1145 * \param buf buffer to print into
1146 * \param pos position of terminating 0 in buf
1147 * \param len maximum number of characters in buf, not including terminating 0
1148 * \param time time value to convert/append
1150 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1151 long tenths = 10 * time;
1152 int f1 = tenths % 10;
1153 int ss = (tenths / 10) % 60;
1154 int mm = (tenths / 600) % 60;
1155 int hh = tenths / 36000;
1156 if (time <= 0) {
1157 saddf(buf, pos, len, "unknown");
1158 return;
1160 if (hh > 0)
1161 saddf(buf, pos, len, "%2d:", hh);
1162 if (hh > 0 || mm > 0)
1163 saddf(buf, pos, len, "%02d:", mm);
1164 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1167 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1169 struct MPOpts *opts = &mpctx->opts;
1170 sh_video_t * const sh_video = mpctx->sh_video;
1172 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1173 a_pos = playing_audio_pts(mpctx);
1174 if (mpctx->sh_audio && sh_video && at_frame) {
1175 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1176 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1177 && !mpctx->drop_message_shown) {
1178 mp_msg(MSGT_AVSYNC,MSGL_WARN,MSGTR_SystemTooSlow);
1179 mpctx->drop_message_shown = true;
1182 if (quiet)
1183 return;
1186 int width;
1187 char *line;
1188 unsigned pos = 0;
1189 get_screen_size();
1190 if (screen_width > 0)
1191 width = screen_width;
1192 else
1193 width = 80;
1194 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1195 /* Windows command line is broken (MinGW's rxvt works, but we
1196 * should not depend on that). */
1197 width--;
1198 #endif
1199 line = malloc(width + 1); // one additional char for the terminating null
1201 // Audio time
1202 if (mpctx->sh_audio) {
1203 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1204 if (!sh_video) {
1205 float len = demuxer_get_time_length(mpctx->demuxer);
1206 saddf(line, &pos, width, "(");
1207 sadd_hhmmssf(line, &pos, width, a_pos);
1208 saddf(line, &pos, width, ") of %.1f (", len);
1209 sadd_hhmmssf(line, &pos, width, len);
1210 saddf(line, &pos, width, ") ");
1214 // Video time
1215 if (sh_video)
1216 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1218 // A-V sync
1219 if (mpctx->sh_audio && sh_video)
1220 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1221 mpctx->last_av_difference, mpctx->total_avsync_change);
1223 // Video stats
1224 if (sh_video)
1225 saddf(line, &pos, width, "%3d/%3d ",
1226 (int)sh_video->num_frames,
1227 (int)sh_video->num_frames_decoded);
1229 // CPU usage
1230 if (sh_video) {
1231 if (sh_video->timer > 0.5)
1232 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1233 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1234 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1235 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1236 else
1237 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1238 } else if (mpctx->sh_audio) {
1239 if (mpctx->delay > 0.5)
1240 saddf(line, &pos, width, "%4.1f%% ",
1241 100.0*audio_time_usage/(double)mpctx->delay);
1242 else
1243 saddf(line, &pos, width, "??,?%% ");
1246 // VO stats
1247 if (sh_video)
1248 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1250 #ifdef CONFIG_STREAM_CACHE
1251 // cache stats
1252 if (stream_cache_size > 0)
1253 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1254 #endif
1256 // other
1257 if (opts->playback_speed != 1)
1258 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1260 // end
1261 if (erase_to_end_of_line) {
1262 line[pos] = 0;
1263 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1264 } else {
1265 memset(&line[pos], ' ', width - pos);
1266 line[width] = 0;
1267 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1269 free(line);
1273 * \brief build a chain of audio filters that converts the input format
1274 * to the ao's format, taking into account the current playback_speed.
1275 * \param sh_audio describes the requested input format of the chain.
1276 * \param ao_data describes the requested output format of the chain.
1278 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1280 struct MPOpts *opts = &mpctx->opts;
1281 int new_srate;
1282 int result;
1283 if (!sh_audio)
1285 #ifdef CONFIG_GUI
1286 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1287 #endif
1288 mpctx->mixer.afilter = NULL;
1289 return 0;
1291 if(af_control_any_rev(sh_audio->afilter,
1292 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1293 &opts->playback_speed)) {
1294 new_srate = sh_audio->samplerate;
1295 } else {
1296 new_srate = sh_audio->samplerate * opts->playback_speed;
1297 if (new_srate != ao_data->samplerate) {
1298 // limits are taken from libaf/af_resample.c
1299 if (new_srate < 8000)
1300 new_srate = 8000;
1301 if (new_srate > 192000)
1302 new_srate = 192000;
1303 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1306 result = init_audio_filters(sh_audio, new_srate,
1307 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1308 mpctx->mixer.afilter = sh_audio->afilter;
1309 #ifdef CONFIG_GUI
1310 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1311 #endif
1312 return result;
1316 typedef struct mp_osd_msg mp_osd_msg_t;
1317 struct mp_osd_msg {
1318 /// Previous message on the stack.
1319 mp_osd_msg_t* prev;
1320 /// Message text.
1321 char msg[128];
1322 int id,level,started;
1323 /// Display duration in ms.
1324 unsigned time;
1327 /// OSD message stack.
1328 static mp_osd_msg_t* osd_msg_stack = NULL;
1331 * \brief Add a message on the OSD message stack
1333 * If a message with the same id is already present in the stack
1334 * it is pulled on top of the stack, otherwise a new message is created.
1338 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1339 mp_osd_msg_t *msg,*last=NULL;
1340 va_list va;
1341 int r;
1343 // look if the id is already in the stack
1344 for(msg = osd_msg_stack ; msg && msg->id != id ;
1345 last = msg, msg = msg->prev);
1346 // not found: alloc it
1347 if(!msg) {
1348 msg = calloc(1,sizeof(mp_osd_msg_t));
1349 msg->prev = osd_msg_stack;
1350 osd_msg_stack = msg;
1351 } else if(last) { // found, but it's not on top of the stack
1352 last->prev = msg->prev;
1353 msg->prev = osd_msg_stack;
1354 osd_msg_stack = msg;
1356 // write the msg
1357 va_start(va,fmt);
1358 r = vsnprintf(msg->msg, 128, fmt, va);
1359 va_end(va);
1360 if(r >= 128) msg->msg[127] = 0;
1361 // set id and time
1362 msg->id = id;
1363 msg->level = level;
1364 msg->time = time;
1369 * \brief Remove a message from the OSD stack
1371 * This function can be used to get rid of a message right away.
1375 void rm_osd_msg(int id) {
1376 mp_osd_msg_t *msg,*last=NULL;
1378 // Search for the msg
1379 for(msg = osd_msg_stack ; msg && msg->id != id ;
1380 last = msg, msg = msg->prev);
1381 if(!msg) return;
1383 // Detach it from the stack and free it
1384 if(last)
1385 last->prev = msg->prev;
1386 else
1387 osd_msg_stack = msg->prev;
1388 free(msg);
1392 * \brief Remove all messages from the OSD stack
1396 static void clear_osd_msgs(void) {
1397 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1398 while(msg) {
1399 prev = msg->prev;
1400 free(msg);
1401 msg = prev;
1403 osd_msg_stack = NULL;
1407 * \brief Get the current message from the OSD stack.
1409 * This function decrements the message timer and destroys the old ones.
1410 * The message that should be displayed is returned (if any).
1414 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1416 struct MPOpts *opts = &mpctx->opts;
1417 mp_osd_msg_t *msg,*prev,*last = NULL;
1418 static unsigned last_update = 0;
1419 unsigned now = GetTimerMS();
1420 unsigned diff;
1421 char hidden_dec_done = 0;
1423 if (mpctx->osd_visible) {
1424 // 36000000 means max timed visibility is 1 hour into the future, if
1425 // the difference is greater assume it's wrapped around from below 0
1426 if (mpctx->osd_visible - now > 36000000) {
1427 mpctx->osd_visible = 0;
1428 vo_osd_progbar_type = -1; // disable
1429 vo_osd_changed(OSDTYPE_PROGBAR);
1430 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1433 if (mpctx->osd_show_percentage_until - now > 36000000)
1434 mpctx->osd_show_percentage_until = 0;
1436 if(!last_update) last_update = now;
1437 diff = now >= last_update ? now - last_update : 0;
1439 last_update = now;
1441 // Look for the first message in the stack with high enough level.
1442 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1443 prev = msg->prev;
1444 if (msg->level > opts->osd_level && hidden_dec_done)
1445 continue;
1446 // The message has a high enough level or it is the first hidden one
1447 // in both cases we decrement the timer or kill it.
1448 if(!msg->started || msg->time > diff) {
1449 if(msg->started) msg->time -= diff;
1450 else msg->started = 1;
1451 // display it
1452 if (msg->level <= opts->osd_level)
1453 return msg;
1454 hidden_dec_done = 1;
1455 continue;
1457 // kill the message
1458 free(msg);
1459 if(last) {
1460 last->prev = prev;
1461 msg = last;
1462 } else {
1463 osd_msg_stack = prev;
1464 msg = NULL;
1467 // Nothing found
1468 return NULL;
1472 * \brief Display the OSD bar.
1474 * Display the OSD bar or fall back on a simple message.
1478 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1479 struct MPOpts *opts = &mpctx->opts;
1480 if (opts->osd_level < 1)
1481 return;
1483 if(mpctx->sh_video) {
1484 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1485 vo_osd_progbar_type = type;
1486 vo_osd_progbar_value = 256*(val-min)/(max-min);
1487 vo_osd_changed(OSDTYPE_PROGBAR);
1488 return;
1491 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1492 name, ROUND(100*(val-min)/(max-min)));
1497 * \brief Update the OSD message line.
1499 * This function displays the current message on the vo OSD or on the term.
1500 * If the stack is empty and the OSD level is high enough the timer
1501 * is displayed (only on the vo OSD).
1505 static void update_osd_msg(struct MPContext *mpctx)
1507 struct MPOpts *opts = &mpctx->opts;
1508 mp_osd_msg_t *msg;
1509 struct osd_state *osd = mpctx->osd;
1510 char osd_text_timer[128];
1512 if (mpctx->add_osd_seek_info) {
1513 double percentage;
1514 if (mpctx->timeline && mpctx->sh_video)
1515 percentage = mpctx->sh_video->pts * 100 /
1516 mpctx->timeline[mpctx->num_timeline_parts].start;
1517 else
1518 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1519 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1520 if (mpctx->sh_video)
1521 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1522 mpctx->add_osd_seek_info = false;
1525 // Look if we have a msg
1526 if((msg = get_osd_msg(mpctx))) {
1527 if (strcmp(osd->osd_text, msg->msg)) {
1528 strncpy(osd->osd_text, msg->msg, 127);
1529 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1530 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1532 return;
1535 if(mpctx->sh_video) {
1536 // fallback on the timer
1537 if (opts->osd_level >= 2) {
1538 int len;
1539 if (mpctx->timeline)
1540 len = mpctx->timeline[mpctx->num_timeline_parts].start;
1541 else
1542 len = demuxer_get_time_length(mpctx->demuxer);
1543 int percentage = -1;
1544 char percentage_text[10];
1545 int pts = demuxer_get_current_time(mpctx->demuxer);
1547 if (mpctx->osd_show_percentage_until)
1548 if (mpctx->timeline)
1549 percentage = mpctx->sh_video->pts * 100 /
1550 mpctx->timeline[mpctx->num_timeline_parts].start;
1551 else
1552 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1554 if (percentage >= 0)
1555 snprintf(percentage_text, 9, " (%d%%)", percentage);
1556 else
1557 percentage_text[0] = 0;
1559 if (opts->osd_level == 3)
1560 snprintf(osd_text_timer, 63,
1561 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1562 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1563 len/3600,(len/60)%60,len%60,percentage_text);
1564 else
1565 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1566 mpctx->osd_function,pts/3600,(pts/60)%60,
1567 pts%60,percentage_text);
1568 } else
1569 osd_text_timer[0]=0;
1571 if (strcmp(osd->osd_text, osd_text_timer)) {
1572 strncpy(osd->osd_text, osd_text_timer, 63);
1573 vo_osd_changed(OSDTYPE_OSD);
1575 return;
1578 // Clear the term osd line
1579 if (term_osd && osd->osd_text[0]) {
1580 osd->osd_text[0] = 0;
1581 printf("%s\n",term_osd_esc);
1585 ///@}
1586 // OSDMsgStack
1589 void reinit_audio_chain(struct MPContext *mpctx)
1591 struct MPOpts *opts = &mpctx->opts;
1592 if(mpctx->sh_audio){
1593 current_module="init_audio_codec";
1594 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1595 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1596 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1597 mpctx->d_audio->id = -2;
1598 return;
1599 } else
1600 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1601 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1604 //const ao_info_t *info=audio_out->info;
1605 current_module="af_preinit";
1606 ao_data.samplerate=force_srate;
1607 ao_data.channels=0;
1608 ao_data.format=audio_output_format;
1609 #if 1
1610 // first init to detect best values
1611 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1612 // input:
1613 mpctx->sh_audio->samplerate,
1614 // output:
1615 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1616 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
1617 exit_player(mpctx, EXIT_ERROR);
1619 #endif
1620 current_module="ao2_init";
1621 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1622 0, // plugin flag
1623 ao_data.samplerate,
1624 ao_data.channels,
1625 ao_data.format,0))){
1626 // FAILED:
1627 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotInitAO);
1628 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1629 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1630 mpctx->d_audio->id = -2;
1631 return;
1632 } else {
1633 // SUCCESS:
1634 mpctx->initialized_flags|=INITIALIZED_AO;
1635 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1636 mpctx->audio_out->info->short_name,
1637 ao_data.samplerate, ao_data.channels,
1638 af_fmt2str_short(ao_data.format),
1639 af_fmt2bits(ao_data.format)/8 );
1640 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1641 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1642 if(strlen(mpctx->audio_out->info->comment) > 0)
1643 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1644 // init audio filters:
1645 #if 1
1646 current_module="af_init";
1647 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1648 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
1649 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1650 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1651 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1653 #endif
1655 mpctx->mixer.audio_out = mpctx->audio_out;
1656 mpctx->mixer.volstep = volstep;
1661 ///@}
1662 // Command2Property
1665 // Return pts value corresponding to the end point of audio written to the
1666 // ao so far.
1667 static double written_audio_pts(struct MPContext *mpctx)
1669 sh_audio_t *sh_audio = mpctx->sh_audio;
1670 demux_stream_t *d_audio = mpctx->d_audio;
1671 double buffered_output;
1672 // first calculate the end pts of audio that has been output by decoder
1673 double a_pts = sh_audio->pts;
1674 if (a_pts != MP_NOPTS_VALUE)
1675 // Good, decoder supports new way of calculating audio pts.
1676 // sh_audio->pts is the timestamp of the latest input packet with
1677 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1678 // the amount of bytes the decoder has written after that timestamp.
1679 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1680 else {
1681 // Decoder doesn't support new way of calculating pts (or we're
1682 // being called before it has decoded anything with known timestamp).
1683 // Use the old method of audio pts calculation: take the timestamp
1684 // of last packet with known pts the decoder has read data from,
1685 // and add amount of bytes read after the beginning of that packet
1686 // divided by input bps. This will be inaccurate if the input/output
1687 // ratio is not constant for every audio packet or if it is constant
1688 // but not accurately known in sh_audio->i_bps.
1690 a_pts = d_audio->pts;
1691 // ds_tell_pts returns bytes read after last timestamp from
1692 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1693 // it has read but not decoded
1694 if (sh_audio->i_bps)
1695 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1696 (double)sh_audio->i_bps;
1698 // Now a_pts hopefully holds the pts for end of audio from decoder.
1699 // Substract data in buffers between decoder and audio out.
1701 // Decoded but not filtered
1702 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1704 // Data buffered in audio filters, measured in bytes of "missing" output
1705 buffered_output = af_calc_delay(sh_audio->afilter);
1707 // Data that was ready for ao but was buffered because ao didn't fully
1708 // accept everything to internal buffers yet
1709 buffered_output += sh_audio->a_out_buffer_len;
1711 // Filters divide audio length by playback_speed, so multiply by it
1712 // to get the length in original units without speedup or slowdown
1713 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1715 return a_pts + mpctx->video_offset;
1718 // Return pts value corresponding to currently playing audio.
1719 double playing_audio_pts(struct MPContext *mpctx)
1721 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1722 mpctx->audio_out->get_delay();
1725 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1726 struct MPOpts *opts = &mpctx->opts;
1727 // check for frame-drop:
1728 current_module = "check_framedrop";
1729 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1730 static int dropped_frames;
1731 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1732 float d = delay-mpctx->delay;
1733 ++total_frame_cnt;
1734 // we should avoid dropping too many frames in sequence unless we
1735 // are too late. and we allow 100ms A-V delay here:
1736 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1737 && !mpctx->update_video_immediately) {
1738 ++drop_frame_cnt;
1739 ++dropped_frames;
1740 return frame_dropping;
1741 } else
1742 dropped_frames = 0;
1744 return 0;
1748 #ifdef HAVE_RTC
1749 int rtc_fd = -1;
1750 #endif
1752 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1754 #ifdef HAVE_RTC
1755 if (rtc_fd >= 0){
1756 // -------- RTC -----------
1757 current_module="sleep_rtc";
1758 while (time_frame > 0.000) {
1759 unsigned long rtc_ts;
1760 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1761 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCReadError, strerror(errno));
1762 time_frame -= get_relative_time(mpctx);
1764 } else
1765 #endif
1767 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1768 // unnecessarily high CPU usage
1769 float margin = softsleep ? 0.011 : 0;
1770 current_module = "sleep_timer";
1771 while (time_frame > margin) {
1772 usec_sleep(1000000 * (time_frame - margin));
1773 time_frame -= get_relative_time(mpctx);
1775 if (softsleep){
1776 current_module = "sleep_soft";
1777 if (time_frame < 0)
1778 mp_msg(MSGT_AVSYNC, MSGL_WARN, MSGTR_SoftsleepUnderflow);
1779 while (time_frame > 0)
1780 time_frame -= get_relative_time(mpctx); // burn the CPU
1783 return time_frame;
1786 #ifdef CONFIG_DVDNAV
1787 #ifndef FF_B_TYPE
1788 #define FF_B_TYPE 3
1789 #endif
1790 /// store decoded video image
1791 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1792 mp_image_t *from_mpi) {
1793 mp_image_t *mpi;
1795 /// Do not store B-frames
1796 if (from_mpi->pict_type == FF_B_TYPE)
1797 return to_mpi;
1799 if (to_mpi &&
1800 to_mpi->w == from_mpi->w &&
1801 to_mpi->h == from_mpi->h &&
1802 to_mpi->imgfmt == from_mpi->imgfmt)
1803 mpi = to_mpi;
1804 else {
1805 if (to_mpi)
1806 free_mp_image(to_mpi);
1807 if (from_mpi->w == 0 || from_mpi->h == 0)
1808 return NULL;
1809 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1812 copy_mpi(mpi,from_mpi);
1813 return mpi;
1816 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1817 struct MPOpts *opts = &ctx->opts;
1818 if (ctx->sh_video) {
1819 /// clear video pts
1820 ctx->d_video->pts = 0.0f;
1821 ctx->sh_video->pts = 0.0f;
1822 ctx->sh_video->i_pts = 0.0f;
1823 ctx->sh_video->last_pts = 0.0f;
1824 ctx->sh_video->num_buffered_pts = 0;
1825 ctx->sh_video->num_frames = 0;
1826 ctx->sh_video->num_frames_decoded = 0;
1827 ctx->sh_video->timer = 0.0f;
1828 ctx->sh_video->stream_delay = 0.0f;
1829 ctx->sh_video->timer = 0;
1830 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1833 if (ctx->sh_audio) {
1834 /// free audio packets and reset
1835 ds_free_packs(ctx->d_audio);
1836 audio_delay -= ctx->sh_audio->stream_delay;
1837 ctx->delay =- audio_delay;
1838 ctx->audio_out->reset();
1839 resync_audio_stream(ctx->sh_audio);
1842 if (ctx->d_sub) opts->sub_id = -2;
1844 audio_delay = 0.0f;
1846 /// clear all EOF related flags
1847 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1850 /// Restore last decoded DVDNAV (still frame)
1851 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1852 int *in_size,
1853 unsigned char **start,
1854 mp_image_t *decoded_frame)
1856 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1857 return decoded_frame;
1859 /// a change occured in dvdnav stream
1860 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1861 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1862 mp_dvdnav_context_free(mpctx);
1863 mp_dvdnav_reset_stream(mpctx);
1864 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1865 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1868 if (*in_size < 0) {
1869 float len;
1871 /// Display still frame, if any
1872 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1873 decoded_frame = mpctx->nav_smpi;
1875 /// increment video frame : continue playing after still frame
1876 len = demuxer_get_time_length(mpctx->demuxer);
1877 if (mpctx->sh_video->pts >= len &&
1878 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1879 mp_dvdnav_skip_still(mpctx->stream);
1880 mp_dvdnav_skip_wait(mpctx->stream);
1882 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1884 if (mpctx->nav_buffer) {
1885 *start = mpctx->nav_start;
1886 *in_size = mpctx->nav_in_size;
1887 if (mpctx->nav_start)
1888 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1892 return decoded_frame;
1895 /// Save last decoded DVDNAV (still frame)
1896 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1897 unsigned char *start,
1898 mp_image_t *decoded_frame)
1900 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1901 return;
1903 if (mpctx->nav_buffer)
1904 free(mpctx->nav_buffer);
1906 mpctx->nav_buffer = malloc(in_size);
1907 mpctx->nav_start = start;
1908 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1909 if (mpctx->nav_buffer)
1910 memcpy(mpctx->nav_buffer,start,in_size);
1912 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1913 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1915 #endif /* CONFIG_DVDNAV */
1917 /* Modify video timing to match the audio timeline. There are two main
1918 * reasons this is needed. First, video and audio can start from different
1919 * positions at beginning of file or after a seek (MPlayer starts both
1920 * immediately even if they have different pts). Second, the file can have
1921 * audio timestamps that are inconsistent with the duration of the audio
1922 * packets, for example two consecutive timestamp values differing by
1923 * one second but only a packet with enough samples for half a second
1924 * of playback between them.
1926 static void adjust_sync(struct MPContext *mpctx, double frame_time)
1928 struct MPOpts *opts = &mpctx->opts;
1929 current_module = "av_sync";
1931 if (!mpctx->sh_audio)
1932 return;
1934 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
1935 double v_pts = mpctx->sh_video->pts;
1936 double av_delay = a_pts - v_pts;
1937 // Try to sync vo_flip() so it will *finish* at given time
1938 av_delay += mpctx->last_vo_flip_duration;
1939 av_delay -= audio_delay; // This much pts difference is desired
1941 double change = av_delay * 0.1;
1942 double max_change = default_max_pts_correction >= 0 ?
1943 default_max_pts_correction : frame_time * 0.1;
1944 if (change < -max_change)
1945 change = -max_change;
1946 else if (change > max_change)
1947 change = max_change;
1948 mpctx->delay += change;
1949 mpctx->total_avsync_change += change;
1952 static int fill_audio_out_buffers(struct MPContext *mpctx)
1954 struct MPOpts *opts = &mpctx->opts;
1955 unsigned int t;
1956 double tt;
1957 int playsize;
1958 int playflags=0;
1959 int audio_eof=0;
1960 int bytes_to_write;
1961 sh_audio_t * const sh_audio = mpctx->sh_audio;
1963 current_module="play_audio";
1965 while (1) {
1966 int sleep_time;
1967 // all the current uses of ao_data.pts seem to be in aos that handle
1968 // sync completely wrong; there should be no need to use ao_data.pts
1969 // in get_space()
1970 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1971 bytes_to_write = mpctx->audio_out->get_space();
1972 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
1973 break;
1975 // handle audio-only case:
1976 // this is where mplayer sleeps during audio-only playback
1977 // to avoid 100% CPU use
1978 sleep_time = (ao_data.outburst - bytes_to_write) * 1000 / ao_data.bps;
1979 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
1980 usec_sleep(sleep_time * 1000);
1983 while (bytes_to_write) {
1984 playsize = bytes_to_write;
1985 if (playsize > MAX_OUTBURST)
1986 playsize = MAX_OUTBURST;
1987 bytes_to_write -= playsize;
1989 // Fill buffer if needed:
1990 current_module="decode_audio";
1991 t = GetTimer();
1992 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
1993 if (mpctx->d_audio->eof) {
1994 audio_eof = 1;
1995 if (sh_audio->a_out_buffer_len == 0)
1996 return 0;
1998 t = GetTimer() - t;
1999 tt = t*0.000001f; audio_time_usage+=tt;
2000 if (playsize > sh_audio->a_out_buffer_len) {
2001 playsize = sh_audio->a_out_buffer_len;
2002 if (audio_eof)
2003 playflags |= AOPLAY_FINAL_CHUNK;
2005 if (!playsize)
2006 break;
2008 // play audio:
2009 current_module="play_audio";
2011 // Is this pts value actually useful for the aos that access it?
2012 // They're obviously badly broken in the way they handle av sync;
2013 // would not having access to this make them more broken?
2014 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2015 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2017 if (playsize > 0) {
2018 sh_audio->a_out_buffer_len -= playsize;
2019 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2020 sh_audio->a_out_buffer_len);
2021 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2023 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2024 // Sanity check to avoid hanging in case current ao doesn't output
2025 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2026 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2027 sh_audio->a_out_buffer_len = 0;
2030 return 1;
2033 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2034 float *aq_sleep_time)
2036 struct MPOpts *opts = &mpctx->opts;
2037 int frame_time_remaining = 0;
2038 current_module="calc_sleep_time";
2040 *time_frame -= get_relative_time(mpctx); // reset timer
2042 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2043 float delay = mpctx->audio_out->get_delay();
2044 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2046 if (autosync) {
2048 * Adjust this raw delay value by calculating the expected
2049 * delay for this frame and generating a new value which is
2050 * weighted between the two. The higher autosync is, the
2051 * closer to the delay value gets to that which "-nosound"
2052 * would have used, and the longer it will take for A/V
2053 * sync to settle at the right value (but it eventually will.)
2054 * This settling time is very short for values below 100.
2056 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2057 float difference = delay - predicted;
2058 delay = predicted + difference / (float)autosync;
2061 *time_frame = delay - mpctx->delay / opts->playback_speed;
2063 // delay = amount of audio buffered in soundcard/driver
2064 if (delay > 0.25) delay=0.25; else
2065 if (delay < 0.10) delay=0.10;
2066 if (*time_frame > delay*0.6) {
2067 // sleep time too big - may cause audio drops (buffer underrun)
2068 frame_time_remaining = 1;
2069 *time_frame = delay*0.5;
2071 } else {
2072 // If we're lagging more than 200 ms behind the right playback rate,
2073 // don't try to "catch up".
2074 // If benchmark is set always output frames as fast as possible
2075 // without sleeping.
2076 if (*time_frame < -0.2 || benchmark)
2077 *time_frame = 0;
2080 *aq_sleep_time += *time_frame;
2083 //============================== SLEEP: ===================================
2085 // flag 256 means: libvo driver does its timing (dvb card)
2086 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2087 *time_frame = timing_sleep(mpctx, *time_frame);
2088 return frame_time_remaining;
2091 int reinit_video_chain(struct MPContext *mpctx)
2093 struct MPOpts *opts = &mpctx->opts;
2094 sh_video_t * const sh_video = mpctx->sh_video;
2095 double ar=-1.0;
2096 //================== Init VIDEO (codec & libvo) ==========================
2097 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2098 current_module="preinit_libvo";
2100 //shouldn't we set dvideo->id=-2 when we fail?
2101 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2102 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2103 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorInitializingVODevice);
2104 goto err_out;
2106 mpctx->initialized_flags|=INITIALIZED_VO;
2109 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2110 mpctx->sh_video->stream_aspect = ar;
2111 current_module="init_video_filters";
2113 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2114 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2116 #ifdef CONFIG_MENU
2117 if(use_menu) {
2118 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2119 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2120 if(!vf_menu) {
2121 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantOpenLibmenuFilterWithThisRootMenu,menu_root);
2122 use_menu = 0;
2125 if(vf_menu)
2126 sh_video->vfilter = vf_menu;
2127 #endif
2129 #ifdef CONFIG_ASS
2130 if(ass_enabled) {
2131 int i;
2132 int insert = 1;
2133 if (opts->vf_settings)
2134 for (i = 0; opts->vf_settings[i].name; ++i)
2135 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2136 insert = 0;
2137 break;
2139 if (insert) {
2140 extern vf_info_t vf_info_ass;
2141 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2142 char* vf_arg[] = {"auto", "1", NULL};
2143 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2144 if (vf_ass)
2145 sh_video->vfilter = vf_ass;
2146 else
2147 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2150 #endif
2152 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2154 #ifdef CONFIG_ASS
2155 if (ass_enabled)
2156 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2157 #endif
2159 current_module="init_video_codec";
2161 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2162 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2163 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2165 if(!sh_video->initialized){
2166 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2167 goto err_out;
2170 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2172 if (sh_video->codec)
2173 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2175 sh_video->last_pts = MP_NOPTS_VALUE;
2176 sh_video->num_buffered_pts = 0;
2177 sh_video->next_frame_time = 0;
2179 if(auto_quality>0){
2180 // Auto quality option enabled
2181 output_quality=get_video_quality_max(sh_video);
2182 if(auto_quality>output_quality) auto_quality=output_quality;
2183 else output_quality=auto_quality;
2184 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2185 set_video_quality(sh_video,output_quality);
2188 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2190 current_module="init_vo";
2192 return 1;
2194 err_out:
2195 mpctx->sh_video = mpctx->d_video->sh = NULL;
2196 return 0;
2199 static double update_video_nocorrect_pts(struct MPContext *mpctx,
2200 int *blit_frame)
2202 struct sh_video *sh_video = mpctx->sh_video;
2203 *blit_frame = 0;
2204 double frame_time = 0;
2205 while (1) {
2206 current_module = "filter_video";
2207 // In nocorrect-pts mode there is no way to properly time these frames
2208 if (vf_output_queued_frame(sh_video->vfilter))
2209 break;
2210 unsigned char *packet = NULL;
2211 frame_time = sh_video->next_frame_time;
2212 if (mpctx->update_video_immediately)
2213 frame_time = 0;
2214 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2215 &packet, force_fps);
2216 if (in_size < 0) {
2217 #ifdef CONFIG_DVDNAV
2218 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2219 if (mp_dvdnav_is_eof(mpctx->stream))
2220 return -1;
2221 if (mpctx->d_video)
2222 mpctx->d_video->eof = 0;
2223 if (mpctx->d_audio)
2224 mpctx->d_audio->eof = 0;
2225 mpctx->stream->eof = 0;
2226 } else
2227 #endif
2228 return -1;
2230 if (in_size > max_framesize)
2231 max_framesize = in_size;
2232 sh_video->timer += frame_time;
2233 if (mpctx->sh_audio)
2234 mpctx->delay -= frame_time;
2235 // video_read_frame can change fps (e.g. for ASF video)
2236 vo_fps = sh_video->fps;
2237 int framedrop_type = check_framedrop(mpctx, frame_time);
2238 current_module = "decode video";
2240 void *decoded_frame;
2241 #ifdef CONFIG_DVDNAV
2242 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2243 if (in_size >= 0 && !decoded_frame)
2244 #endif
2245 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2246 sh_video->pts);
2247 #ifdef CONFIG_DVDNAV
2248 // Save last still frame for future display
2249 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2250 #endif
2251 if (decoded_frame) {
2252 // These updates are done here for vf_expand OSD/subtitles
2253 update_subtitles(sh_video, mpctx->d_sub, mpctx->video_offset, 0);
2254 update_teletext(sh_video, mpctx->demuxer, 0);
2255 update_osd_msg(mpctx);
2256 current_module = "filter video";
2257 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2258 break;
2261 *blit_frame = 1;
2262 return frame_time;
2265 static double update_video(struct MPContext *mpctx, int *blit_frame)
2267 struct sh_video *sh_video = mpctx->sh_video;
2268 *blit_frame = 0;
2269 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2270 mpctx->osd); // hack for vf_expand
2271 if (!mpctx->opts.correct_pts)
2272 return update_video_nocorrect_pts(mpctx, blit_frame);
2274 double pts;
2276 while (1) {
2277 current_module = "filter_video";
2278 // XXX Time used in this call is not counted in any performance
2279 // timer now, OSD time is not updated correctly for filter-added frames
2280 if (vf_output_queued_frame(sh_video->vfilter))
2281 break;
2282 unsigned char *packet = NULL;
2283 bool hit_eof = false;
2284 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2285 if (pts != MP_NOPTS_VALUE)
2286 pts += mpctx->video_offset;
2287 if (in_size < 0) {
2288 // try to extract last frames in case of decoder lag
2289 in_size = 0;
2290 pts = 1e300;
2291 hit_eof = true;
2293 if (in_size > max_framesize)
2294 max_framesize = in_size;
2295 current_module = "decode video";
2296 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2297 void *decoded_frame = decode_video(sh_video, packet, in_size,
2298 framedrop_type, pts);
2299 if (decoded_frame) {
2300 // These updates are done here for vf_expand OSD/subtitles
2301 update_subtitles(sh_video, mpctx->d_sub, mpctx->video_offset, 0);
2302 update_teletext(sh_video, mpctx->demuxer, 0);
2303 update_osd_msg(mpctx);
2304 current_module = "filter video";
2305 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2306 break;
2307 } else if (hit_eof)
2308 return -1;
2311 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_GET_PTS, &pts);
2312 if (pts == MP_NOPTS_VALUE) {
2313 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2314 // Try to use decoder pts from before filters
2315 pts = sh_video->pts;
2317 sh_video->pts = pts;
2318 if (sh_video->last_pts == MP_NOPTS_VALUE)
2319 sh_video->last_pts = sh_video->pts;
2320 else if (sh_video->last_pts > sh_video->pts) {
2321 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2322 sh_video->pts, sh_video->last_pts);
2323 /* If the difference in pts is small treat it as jitter around the
2324 * right value (possibly caused by incorrect timestamp ordering) and
2325 * just show this frame immediately after the last one.
2326 * Treat bigger differences as timestamp resets and start counting
2327 * timing of later frames from the position of this one. */
2328 if (sh_video->last_pts - sh_video->pts > 0.5)
2329 sh_video->last_pts = sh_video->pts;
2330 else
2331 sh_video->pts = sh_video->last_pts;
2333 double frame_time = sh_video->pts - sh_video->last_pts;
2334 sh_video->last_pts = sh_video->pts;
2335 sh_video->timer += frame_time;
2336 if (mpctx->sh_audio)
2337 mpctx->delay -= frame_time;
2338 *blit_frame = 1;
2339 return frame_time;
2342 void pause_player(struct MPContext *mpctx)
2344 if (mpctx->paused)
2345 return;
2346 mpctx->paused = 1;
2347 mpctx->step_frames = 0;
2348 mpctx->time_frame -= get_relative_time(mpctx);
2350 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2351 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2353 if (mpctx->audio_out && mpctx->sh_audio)
2354 mpctx->audio_out->pause(); // pause audio, keep data if possible
2357 void unpause_player(struct MPContext *mpctx)
2359 if (!mpctx->paused)
2360 return;
2361 mpctx->paused = 0;
2363 if (mpctx->audio_out && mpctx->sh_audio)
2364 mpctx->audio_out->resume(); // resume audio
2365 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2366 && !mpctx->step_frames)
2367 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2368 (void)get_relative_time(mpctx); // ignore time that passed during pause
2371 void add_step_frame(struct MPContext *mpctx)
2373 mpctx->step_frames++;
2374 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2375 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2376 unpause_player(mpctx);
2379 static void pause_loop(struct MPContext *mpctx)
2381 mp_cmd_t* cmd;
2382 if (!quiet) {
2383 // Small hack to display the pause message on the OSD line.
2384 // The pause string is: "\n == PAUSE == \r" so we need to
2385 // take the first and the last char out
2386 if (term_osd && !mpctx->sh_video) {
2387 char msg[128] = MSGTR_Paused;
2388 int mlen = strlen(msg);
2389 msg[mlen-1] = '\0';
2390 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2391 update_osd_msg(mpctx);
2392 } else
2393 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_Paused);
2394 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2396 #ifdef CONFIG_GUI
2397 if (use_gui)
2398 guiGetEvent(guiCEvent, (char *)guiSetPause);
2399 #endif
2401 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2402 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2403 if (cmd) {
2404 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2405 run_command(mpctx, cmd);
2406 mp_cmd_free(cmd);
2407 continue;
2409 if (mpctx->sh_video && mpctx->video_out)
2410 vo_check_events(mpctx->video_out);
2411 #ifdef CONFIG_GUI
2412 if (use_gui) {
2413 guiEventHandling();
2414 guiGetEvent(guiReDraw, NULL);
2415 if (guiIntfStruct.Playing!=2 || (mpctx->rel_seek_secs || mpctx->abs_seek_pos))
2416 break;
2418 #endif
2419 #ifdef CONFIG_MENU
2420 if (vf_menu)
2421 vf_menu_pause_update(vf_menu);
2422 #endif
2423 usec_sleep(20000);
2424 update_osd_msg(mpctx);
2425 int hack = vo_osd_changed(0);
2426 vo_osd_changed(hack);
2427 if (hack)
2428 break;
2430 #ifdef CONFIG_GUI
2431 if (use_gui) {
2432 if (guiIntfStruct.Playing == guiSetStop)
2433 mpctx->eof = 1;
2434 else
2435 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2437 #endif
2441 // Find the right mute status and record position for new file position
2442 static void edl_seek_reset(MPContext *mpctx)
2444 mpctx->edl_muted = 0;
2445 next_edl_record = edl_records;
2447 while (next_edl_record) {
2448 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2449 break;
2451 if (next_edl_record->action == EDL_MUTE)
2452 mpctx->edl_muted = !mpctx->edl_muted;
2453 next_edl_record = next_edl_record->next;
2455 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2456 mixer_mute(&mpctx->mixer);
2460 // Execute EDL command for the current position if one exists
2461 static void edl_update(MPContext *mpctx)
2463 if (!next_edl_record)
2464 return;
2466 if (!mpctx->sh_video) {
2467 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlNOsh_video);
2468 free_edl(edl_records);
2469 next_edl_record = NULL;
2470 edl_records = NULL;
2471 return;
2474 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2475 if (next_edl_record->action == EDL_SKIP) {
2476 mpctx->osd_function = OSD_FFW;
2477 mpctx->abs_seek_pos = 0;
2478 mpctx->rel_seek_secs = next_edl_record->length_sec;
2479 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2480 "[%f], length [%f]\n", next_edl_record->start_sec,
2481 next_edl_record->stop_sec, next_edl_record->length_sec);
2483 else if (next_edl_record->action == EDL_MUTE) {
2484 mpctx->edl_muted = !mpctx->edl_muted;
2485 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2486 mixer_mute(&mpctx->mixer);
2487 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2488 next_edl_record->start_sec );
2490 next_edl_record = next_edl_record->next;
2494 static void reinit_decoders(struct MPContext *mpctx)
2496 reinit_video_chain(mpctx);
2497 reinit_audio_chain(mpctx);
2498 mp_property_do("sub", M_PROPERTY_SET, &mpctx->global_sub_pos, mpctx);
2501 static bool timeline_set_part(struct MPContext *mpctx, int i)
2503 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
2504 struct timeline_part *n = mpctx->timeline + i;
2505 mpctx->timeline_part = i;
2506 mpctx->video_offset = n->start - n->source_start;
2507 if (n->source == p->source)
2508 return false;
2509 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo && mpctx->opts.video_id != -2 ? 0 : INITIALIZED_VO) | INITIALIZED_AO | INITIALIZED_ACODEC);
2510 mpctx->demuxer = n->source->demuxer;
2511 mpctx->d_video = mpctx->demuxer->video;
2512 mpctx->d_audio = mpctx->demuxer->audio;
2513 mpctx->d_sub = mpctx->demuxer->sub;
2514 mpctx->sh_video = mpctx->d_video->sh;
2515 mpctx->sh_audio = mpctx->d_audio->sh;
2516 return true;
2519 // Given pts, switch playback to the corresponding part.
2520 // Return offset within that part.
2521 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
2522 bool *need_reset)
2524 if (pts < 0)
2525 pts = 0;
2526 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
2527 struct timeline_part *p = mpctx->timeline + i;
2528 if (pts < (p+1)->start) {
2529 *need_reset = timeline_set_part(mpctx, i);
2530 return pts - p->start + p->source_start;
2533 return -1;
2537 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2538 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2539 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2540 static int seek(MPContext *mpctx, double amount, int style)
2542 current_module = "seek";
2543 if (mpctx->stop_play == AT_END_OF_FILE)
2544 mpctx->stop_play = KEEP_PLAYING;
2545 if (mpctx->timeline && style & SEEK_FACTOR) {
2546 amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
2547 style &= ~SEEK_FACTOR;
2549 if ((mpctx->demuxer->accurate_seek || mpctx->timeline) && mpctx->sh_video
2550 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2551 style |= SEEK_ABSOLUTE;
2552 if (amount > 0)
2553 style |= SEEK_FORWARD;
2554 else
2555 style |= SEEK_BACKWARD;
2556 amount += mpctx->sh_video->pts;
2559 /* At least the liba52 decoder wants to read from the input stream
2560 * during initialization, so reinit must be done after the demux_seek()
2561 * call that clears possible stream EOF. */
2562 bool need_reset = false;
2563 if (mpctx->timeline) {
2564 amount = timeline_set_from_time(mpctx, amount, &need_reset);
2565 if (amount == -1) {
2566 mpctx->stop_play = AT_END_OF_FILE;
2567 // Clear audio from current position
2568 if (mpctx->sh_audio) {
2569 mpctx->audio_out->reset();
2570 mpctx->sh_audio->a_buffer_len = 0;
2571 mpctx->sh_audio->a_out_buffer_len = 0;
2573 return -1;
2576 int seekresult = demux_seek(mpctx->demuxer, amount, audio_delay, style);
2577 if (need_reset)
2578 reinit_decoders(mpctx);
2579 if (seekresult == 0)
2580 return -1;
2582 if (mpctx->sh_video) {
2583 current_module = "seek_video_reset";
2584 resync_video_stream(mpctx->sh_video);
2585 if (mpctx->video_out->config_ok)
2586 vo_control(mpctx->video_out, VOCTRL_RESET, NULL);
2587 mpctx->sh_video->num_buffered_pts = 0;
2588 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2589 mpctx->num_buffered_frames = 0;
2590 mpctx->delay = 0;
2591 mpctx->time_frame = 0;
2592 mpctx->update_video_immediately = true;
2593 // Not all demuxers set d_video->pts during seek, so this value
2594 // (which is used by at least vobsub and edl code below) may
2595 // be completely wrong (probably 0).
2596 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
2597 update_subtitles(mpctx->sh_video, mpctx->d_sub, mpctx->video_offset, 1);
2598 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2601 if (mpctx->sh_audio) {
2602 current_module = "seek_audio_reset";
2603 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2604 mpctx->sh_audio->a_buffer_len = 0;
2605 mpctx->sh_audio->a_out_buffer_len = 0;
2608 if (vo_vobsub && mpctx->sh_video) {
2609 current_module = "seek_vobsub_reset";
2610 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2613 edl_seek_reset(mpctx);
2615 mpctx->total_avsync_change = 0;
2616 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2617 drop_frame_cnt = 0;
2619 current_module = NULL;
2620 return 0;
2623 int get_current_chapter(struct MPContext *mpctx)
2625 if (!mpctx->chapters || !mpctx->sh_video)
2626 return demuxer_get_current_chapter(mpctx->demuxer);
2628 int i;
2629 double current_pts = mpctx->sh_video->pts;
2630 for (i = 1; i < mpctx->num_chapters; i++)
2631 if (current_pts < mpctx->chapters[i].start)
2632 break;
2633 return i - 1;
2636 // currently returns a string allocated with malloc, not talloc
2637 char *chapter_display_name(struct MPContext *mpctx, int chapter)
2639 if (!mpctx->chapters || !mpctx->sh_video)
2640 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
2641 return strdup(mpctx->chapters[chapter].name);
2644 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
2645 char **chapter_name)
2647 if (!mpctx->chapters || !mpctx->sh_video)
2648 return demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
2649 chapter_name);
2650 if (chapter >= mpctx->num_chapters)
2651 return -1;
2652 if (chapter < 0)
2653 chapter = 0;
2654 *seek_pts = mpctx->chapters[chapter].start;
2655 if (chapter_name)
2656 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
2657 return chapter;
2660 static int find_ordered_chapter_sources(struct MPContext *mpctx,
2661 struct content_source *sources,
2662 int num_sources,
2663 unsigned char uid_map[][16])
2665 int num_filenames = 0;
2666 char **filenames = NULL;
2667 if (num_sources > 1) {
2668 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
2669 "other sources.\n");
2670 if (mpctx->stream->type != STREAMTYPE_FILE) {
2671 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
2672 "normal disk file. Will not search for related files.\n");
2673 } else {
2674 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
2675 "same directory to find referenced sources.\n");
2676 filenames = find_files(mpctx->demuxer->filename, ".mkv",
2677 &num_filenames);
2681 int num_left = num_sources - 1;
2682 for (int i = 0; i < num_filenames && num_left > 0; i++) {
2683 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
2684 filename_recode(filenames[i]));
2685 int format;
2686 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
2687 if (!s)
2688 continue;
2689 struct demuxer *d = demux_open(&mpctx->opts, s, DEMUXER_TYPE_MATROSKA,
2690 mpctx->opts.audio_id,
2691 mpctx->opts.video_id,
2692 mpctx->opts.sub_id, filenames[i]);
2693 if (!d) {
2694 free_stream(s);
2695 continue;
2697 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
2698 for (int i = 1; i < num_sources; i++) {
2699 if (sources[i].demuxer)
2700 continue;
2701 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
2702 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
2703 i, filename_recode(d->filename));
2704 sources[i].stream = s;
2705 sources[i].demuxer = d;
2706 num_left--;
2707 goto match;
2711 free_demuxer(d);
2712 free_stream(s);
2713 continue;
2714 match:
2717 talloc_free(filenames);
2718 if (num_left) {
2719 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
2720 "There will be parts MISSING from the video!\n");
2721 for (int i = 1, j = 1; i < num_sources; i++)
2722 if (sources[i].demuxer) {
2723 sources[j] = sources[i];
2724 memcpy(uid_map[j], uid_map[i], 16);
2725 j++;
2728 return num_sources - num_left;
2731 static void build_ordered_chapter_timeline(struct MPContext *mpctx)
2733 if (!mpctx->opts.ordered_chapters) {
2734 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
2735 "you have disabled support for them. Ignoring.\n");
2736 return;
2739 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
2740 "edit timeline.\n");
2742 struct demuxer *demuxer = mpctx->demuxer;
2743 struct matroska_data *m = &demuxer->matroska_data;
2745 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
2746 m->num_ordered_chapters);
2747 sources[0].stream = mpctx->stream;
2748 sources[0].demuxer = mpctx->demuxer;
2749 unsigned char uid_map[m->num_ordered_chapters][16];
2750 int num_sources = 1;
2751 memcpy(uid_map[0], m->segment_uid, 16);
2753 for (int i = 0; i < m->num_ordered_chapters; i++) {
2754 struct matroska_chapter *c = m->ordered_chapters + i;
2755 if (!c->has_segment_uid)
2756 memcpy(c->segment_uid, m->segment_uid, 16);
2758 for (int j = 0; j < num_sources; j++)
2759 if (!memcmp(c->segment_uid, uid_map[j], 16))
2760 goto found1;
2761 memcpy(uid_map[num_sources], c->segment_uid, 16);
2762 sources[num_sources] = (struct content_source){};
2763 num_sources++;
2764 found1:
2768 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
2769 uid_map);
2772 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
2773 m->num_ordered_chapters + 1);
2774 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
2775 m->num_ordered_chapters);
2776 uint64_t starttime = 0;
2777 uint64_t missing_time = 0;
2778 int part_count = 0;
2779 int num_chapters = 0;
2780 for (int i = 0; i < m->num_ordered_chapters; i++) {
2781 struct matroska_chapter *c = m->ordered_chapters + i;
2783 int j;
2784 for (j = 0; j < num_sources; j++) {
2785 if (!memcmp(c->segment_uid, uid_map[j], 16))
2786 goto found2;
2788 missing_time += c->end - c->start;
2789 continue;
2790 found2:;
2791 chapters[num_chapters].start = starttime / 1000.;
2792 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
2793 // Only add a separate part if the time or file actually changes
2794 uint64_t prev_end = !part_count ? 0 : starttime
2795 - timeline[part_count - 1].start
2796 + timeline[part_count - 1].source_start;
2797 if (part_count == 0 || c->start != prev_end
2798 || sources + j != timeline[part_count - 1].source) {
2799 timeline[part_count].source = sources + j;
2800 timeline[part_count].start = chapters[num_chapters].start;
2801 timeline[part_count].source_start = c->start / 1000.;
2802 part_count++;
2804 starttime += c->end - c->start;
2805 num_chapters++;
2807 timeline[part_count].start = starttime / 1000.;
2809 if (!part_count) {
2810 // None of the parts come from the file itself???
2811 talloc_free(sources);
2812 talloc_free(timeline);
2813 talloc_free(chapters);
2814 return;
2817 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
2818 "sources. Total length %.3f seconds.\n", part_count, num_sources,
2819 timeline[part_count].start);
2820 if (missing_time)
2821 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
2822 "from the timeline!\n", missing_time / 1000.);
2823 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
2824 for (int i = 0; i < num_sources; i++)
2825 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
2826 filename_recode(sources[i].demuxer->filename));
2827 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
2828 "source_start, source):\n");
2829 for (int i = 0; i < part_count; i++) {
2830 struct timeline_part *p = timeline + i;
2831 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
2832 p->source_start, p->source - sources);
2834 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start);
2835 mpctx->sources = sources;
2836 mpctx->num_sources = num_sources;
2837 mpctx->timeline = timeline;
2838 mpctx->num_timeline_parts = part_count;
2839 mpctx->num_chapters = num_chapters;
2840 mpctx->chapters = chapters;
2842 mpctx->timeline_part = 0;
2843 mpctx->video_offset = timeline[0].source_start;
2844 mpctx->demuxer = timeline[0].source->demuxer;
2848 static int read_keys(void *ctx, int fd)
2850 getch2(ctx);
2851 return mplayer_get_key(ctx, 0);
2855 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2856 * file for some tools to link against. */
2857 #ifndef DISABLE_MAIN
2858 int main(int argc,char* argv[]){
2861 char * mem_ptr;
2863 // movie info:
2865 /* Flag indicating whether MPlayer should exit without playing anything. */
2866 int opt_exit = 0;
2868 //float a_frame=0; // Audio
2870 int i;
2872 int gui_no_filename=0;
2874 struct MPContext *mpctx = &(struct MPContext){
2875 .osd_function = OSD_PLAY,
2876 .begin_skip = MP_NOPTS_VALUE,
2877 .play_tree_step = 1,
2878 .global_sub_pos = -1,
2879 .set_of_sub_pos = -1,
2880 .file_format = DEMUXER_TYPE_UNKNOWN,
2881 .last_dvb_step = 1,
2884 InitTimer();
2885 srand(GetTimerMS());
2887 mp_msg_init();
2889 #ifdef CONFIG_X11
2890 mpctx->x11_state = vo_x11_init_state();
2891 #endif
2892 struct MPOpts *opts = &mpctx->opts;
2893 set_default_mplayer_options(opts);
2894 // Create the config context and register the options
2895 mpctx->mconfig = m_config_new(opts, cfg_include);
2896 m_config_register_options(mpctx->mconfig,mplayer_opts);
2897 mp_input_register_options(mpctx->mconfig);
2899 // Preparse the command line
2900 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2902 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2903 set_path_env();
2904 #endif
2906 #ifdef CONFIG_TV
2907 stream_tv_defaults.immediate = 1;
2908 #endif
2910 if (argc > 1 && argv[1] &&
2911 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2912 use_gui = !strcmp(argv[1], "-gui");
2913 } else
2914 if ( argv[0] )
2916 char *base = strrchr(argv[0], '/');
2917 if (!base)
2918 base = strrchr(argv[0], '\\');
2919 if (!base)
2920 base = argv[0];
2921 if(strstr(base, "gmplayer"))
2922 use_gui=1;
2925 parse_cfgfiles(mpctx, mpctx->mconfig);
2927 #ifdef CONFIG_GUI
2928 if ( use_gui ) cfg_read();
2929 #endif
2931 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2932 if(mpctx->playtree == NULL)
2933 opt_exit = 1;
2934 else {
2935 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2936 if(mpctx->playtree) {
2937 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2938 if(mpctx->playtree_iter) {
2939 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2940 play_tree_iter_free(mpctx->playtree_iter);
2941 mpctx->playtree_iter = NULL;
2943 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2947 mpctx->key_fifo = mp_fifo_create(opts);
2949 print_version("MPlayer");
2951 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_GUI)
2952 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
2953 if(runningmplayer && mpctx->filename && use_gui){
2954 COPYDATASTRUCT csData;
2955 char file[MAX_PATH];
2956 char *filepart = mpctx->filename;
2957 if(GetFullPathName(mpctx->filename, MAX_PATH, file, &filepart)){
2958 csData.dwData = 0;
2959 csData.cbData = strlen(file)*2;
2960 csData.lpData = file;
2961 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
2964 #endif
2966 #if defined(__MINGW32__) || defined(__CYGWIN__)
2967 // stop Windows from showing all kinds of annoying error dialogs
2968 SetErrorMode(0x8003);
2969 // request 1ms timer resolution
2970 timeBeginPeriod(1);
2971 #endif
2973 #ifdef CONFIG_PRIORITY
2974 set_priority();
2975 #endif
2977 #ifndef CONFIG_GUI
2978 if(use_gui){
2979 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_NoGui);
2980 use_gui=0;
2982 #else
2983 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
2984 if(use_gui && !vo_init()){
2985 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_GuiNeedsX);
2986 use_gui=0;
2988 #endif
2989 if (use_gui && mpctx->playtree_iter){
2990 char cwd[PATH_MAX+2];
2991 // Free Playtree and Playtree-Iter as it's not used by the GUI.
2992 play_tree_iter_free(mpctx->playtree_iter);
2993 mpctx->playtree_iter=NULL;
2995 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
2997 strcat(cwd, "/");
2998 // Prefix relative paths with current working directory
2999 play_tree_add_bpf(mpctx->playtree, cwd);
3001 // Import initital playtree into GUI.
3002 import_initial_playtree_into_gui(mpctx->playtree, mpctx->mconfig, enqueue);
3004 #endif /* CONFIG_GUI */
3006 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
3007 list_video_out();
3008 opt_exit = 1;
3011 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
3012 list_audio_out();
3013 opt_exit = 1;
3016 /* Check codecs.conf. */
3017 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3018 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3019 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3020 if(!parse_codec_cfg(NULL)){
3021 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3023 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_BuiltinCodecsConf);
3026 free( mem_ptr ); // release the buffer created by get_path()
3029 #if 0
3030 if(video_codec_list){
3031 int i;
3032 video_codec=video_codec_list[0];
3033 for(i=0;video_codec_list[i];i++)
3034 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
3036 #endif
3037 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3038 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableAudioCodecs);
3039 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3040 list_codecs(1);
3041 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3042 opt_exit = 1;
3044 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3045 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AvailableVideoCodecs);
3046 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3047 list_codecs(0);
3048 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3049 opt_exit = 1;
3051 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3052 vfm_help();
3053 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3054 opt_exit = 1;
3056 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3057 afm_help();
3058 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3059 opt_exit = 1;
3061 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3062 af_help();
3063 printf("\n");
3064 opt_exit = 1;
3066 #ifdef CONFIG_X11
3067 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3068 fstype_help();
3069 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3070 opt_exit = 1;
3072 #endif
3073 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3074 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3075 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3076 demuxer_help();
3077 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3078 opt_exit = 1;
3080 if(list_properties) {
3081 property_print_help();
3082 opt_exit = 1;
3085 if(opt_exit)
3086 exit_player(mpctx, EXIT_NONE);
3088 if (player_idle_mode && use_gui) {
3089 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoIdleAndGui);
3090 exit_player_with_rc(mpctx, EXIT_NONE, 1);
3093 if(!mpctx->filename && !player_idle_mode){
3094 if(!use_gui){
3095 // no file/vcd/dvd -> show HELP:
3096 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
3097 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3098 } else gui_no_filename=1;
3101 /* Display what configure line was used */
3102 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3104 // Many users forget to include command line in bugreports...
3105 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3106 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_CommandLine);
3107 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3108 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3111 //------ load global data first ------
3113 mpctx->osd = osd_create();
3115 // check font
3116 #ifdef CONFIG_FREETYPE
3117 init_freetype();
3118 #endif
3119 #ifdef CONFIG_FONTCONFIG
3120 if(font_fontconfig <= 0)
3122 #endif
3123 #ifdef CONFIG_BITMAP_FONT
3124 if(font_name){
3125 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3126 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,
3127 filename_recode(font_name));
3128 } else {
3129 // try default:
3130 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3131 free(mem_ptr); // release the buffer created by get_path()
3132 if(!vo_font)
3133 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3135 if (sub_font_name)
3136 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3137 else
3138 mpctx->osd->sub_font = vo_font;
3139 #endif
3140 #ifdef CONFIG_FONTCONFIG
3142 #endif
3144 #ifdef CONFIG_ASS
3145 ass_library = ass_init();
3146 #endif
3148 #ifdef HAVE_RTC
3149 if(!nortc)
3151 // seteuid(0); /* Can't hurt to try to get root here */
3152 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3153 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_RTCDeviceNotOpenable,
3154 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3155 else {
3156 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3158 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3159 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_LinuxRTCInitErrorIrqpSet, irqp, strerror(errno));
3160 mp_msg(MSGT_CPLAYER, MSGL_HINT, MSGTR_IncreaseRTCMaxUserFreq, irqp);
3161 close (rtc_fd);
3162 rtc_fd = -1;
3163 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3164 /* variable only by the root */
3165 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_LinuxRTCInitErrorPieOn, strerror(errno));
3166 close (rtc_fd);
3167 rtc_fd = -1;
3168 } else
3169 mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_UsingRTCTiming, irqp);
3172 #ifdef CONFIG_GUI
3173 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
3174 // and now ? -- Pontscho
3175 if(use_gui) setuid( getuid() ); // strongly test, please check this.
3176 #endif
3177 if(rtc_fd<0)
3178 #endif /* HAVE_RTC */
3179 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3180 softsleep?"software":timer_name);
3182 #ifdef HAVE_TERMCAP
3183 if ( !use_gui ) load_termcap(NULL); // load key-codes
3184 #endif
3186 // ========== Init keyboard FIFO (connection to libvo) ============
3188 // Init input system
3189 current_module = "init_input";
3190 mpctx->input = mp_input_init(&opts->input, use_gui);
3191 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3192 if(slave_mode)
3193 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
3194 else if(!noconsolecontrols)
3195 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
3196 // Set the libstream interrupt callback
3197 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
3199 #ifdef CONFIG_MENU
3200 if(use_menu) {
3201 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3202 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
3203 else {
3204 menu_cfg = get_path("menu.conf");
3205 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3206 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, menu_cfg);
3207 else {
3208 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
3209 MPLAYER_CONFDIR "/menu.conf"))
3210 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitialized, MPLAYER_CONFDIR"/menu.conf");
3211 else {
3212 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_MenuInitFailed);
3213 use_menu = 0;
3218 #endif
3220 current_module = NULL;
3222 /// Catch signals
3223 #ifndef __MINGW32__
3224 signal(SIGCHLD,child_sighandler);
3225 #endif
3227 #ifdef CONFIG_CRASH_DEBUG
3228 prog_path = argv[0];
3229 #endif
3230 //========= Catch terminate signals: ================
3231 // terminate requests:
3232 signal(SIGTERM,exit_sighandler); // kill
3233 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3235 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3237 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3238 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3239 #ifdef CONFIG_SIGHANDLER
3240 // fatal errors:
3241 signal(SIGBUS,exit_sighandler); // bus error
3242 signal(SIGSEGV,exit_sighandler); // segfault
3243 signal(SIGILL,exit_sighandler); // illegal instruction
3244 signal(SIGFPE,exit_sighandler); // floating point exc.
3245 signal(SIGABRT,exit_sighandler); // abort()
3246 #ifdef CONFIG_CRASH_DEBUG
3247 if (crash_debug)
3248 signal(SIGTRAP,exit_sighandler);
3249 #endif
3250 #endif
3252 #ifdef CONFIG_GUI
3253 if(use_gui){
3254 guiInit();
3255 guiGetEvent(guiSetContext, mpctx);
3256 mpctx->initialized_flags|=INITIALIZED_GUI;
3257 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
3259 #endif
3261 // ******************* Now, let's see the per-file stuff ********************
3263 play_next_file:
3265 // init global sub numbers
3266 mpctx->global_sub_size = 0;
3267 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3269 if (mpctx->filename) {
3270 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
3271 load_per_extension_config (mpctx->mconfig, mpctx->filename);
3272 load_per_file_config (mpctx->mconfig, mpctx->filename);
3275 if (opts->video_driver_list)
3276 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
3277 if (opts->audio_driver_list)
3278 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
3280 // We must enable getch2 here to be able to interrupt network connection
3281 // or cache filling
3282 if(!noconsolecontrols && !slave_mode){
3283 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
3284 mp_msg(MSGT_CPLAYER,MSGL_WARN,MSGTR_Getch2InitializedTwice);
3285 else
3286 getch2_enable(); // prepare stdin for hotkeys...
3287 mpctx->initialized_flags|=INITIALIZED_GETCH2;
3288 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3291 // =================== GUI idle loop (STOP state) ===========================
3292 #ifdef CONFIG_GUI
3293 if ( use_gui ) {
3294 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
3295 guiGetEvent( guiSetDefaults,0 );
3296 while ( guiIntfStruct.Playing != 1 )
3298 mp_cmd_t* cmd;
3299 usec_sleep(20000);
3300 guiEventHandling();
3301 guiGetEvent( guiReDraw,NULL );
3302 if ( (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
3303 guiGetEvent(guiIEvent, (char *)cmd->id);
3304 mp_cmd_free(cmd);
3307 guiGetEvent( guiSetParameters,NULL );
3308 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
3310 play_tree_t * entry = play_tree_new();
3311 play_tree_add_file( entry,guiIntfStruct.Filename );
3312 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
3313 else mpctx->playtree=play_tree_new();
3314 play_tree_set_child( mpctx->playtree,entry );
3315 if(mpctx->playtree)
3317 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3318 if(mpctx->playtree_iter)
3320 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
3322 play_tree_iter_free(mpctx->playtree_iter);
3323 mpctx->playtree_iter = NULL;
3325 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3330 #endif /* CONFIG_GUI */
3332 while (player_idle_mode && !mpctx->filename) {
3333 play_tree_t * entry = NULL;
3334 mp_cmd_t * cmd;
3335 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3336 if (mpctx->video_out)
3337 vo_check_events(mpctx->video_out);
3338 usec_sleep(20000);
3340 switch (cmd->id) {
3341 case MP_CMD_LOADFILE:
3342 // prepare a tree entry with the new filename
3343 entry = play_tree_new();
3344 play_tree_add_file(entry, cmd->args[0].v.s);
3345 // The entry is added to the main playtree after the switch().
3346 break;
3347 case MP_CMD_LOADLIST:
3348 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3349 break;
3350 case MP_CMD_QUIT:
3351 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3352 break;
3353 case MP_CMD_GET_PROPERTY:
3354 case MP_CMD_SET_PROPERTY:
3355 case MP_CMD_STEP_PROPERTY:
3356 run_command(mpctx, cmd);
3357 break;
3360 mp_cmd_free(cmd);
3362 if (entry) { // user entered a command that gave a valid entry
3363 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3364 play_tree_free_list(mpctx->playtree->child, 1);
3365 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3367 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3369 play_tree_set_child(mpctx->playtree, entry);
3371 /* Make iterator start at the top the of tree. */
3372 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3373 if (!mpctx->playtree_iter) continue;
3375 // find the first real item in the tree
3376 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3377 // no items!
3378 play_tree_iter_free(mpctx->playtree_iter);
3379 mpctx->playtree_iter = NULL;
3380 continue; // wait for next command
3382 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3385 //---------------------------------------------------------------------------
3387 if(mpctx->filename)
3388 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
3389 filename_recode(mpctx->filename));
3391 if (edl_filename) {
3392 if (edl_records) free_edl(edl_records);
3393 next_edl_record = edl_records = edl_parse_file();
3395 if (edl_output_filename) {
3396 if (edl_fd) fclose(edl_fd);
3397 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3399 mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_EdlCantOpenForWrite,
3400 filename_recode(edl_output_filename));
3404 //==================== Open VOB-Sub ============================
3406 current_module="vobsub";
3407 if (vobsub_name){
3408 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3409 if(vo_vobsub==NULL)
3410 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,
3411 filename_recode(vobsub_name));
3412 } else if (sub_auto && mpctx->filename){
3413 /* try to autodetect vobsub from movie filename ::atmos */
3414 char *buf = strdup(mpctx->filename), *psub;
3415 char *pdot = strrchr(buf, '.');
3416 char *pslash = strrchr(buf, '/');
3417 #if defined(__MINGW32__) || defined(__CYGWIN__)
3418 if (!pslash) pslash = strrchr(buf, '\\');
3419 #endif
3420 if (pdot && (!pslash || pdot > pslash))
3421 *pdot = '\0';
3422 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3423 /* try from ~/.mplayer/sub */
3424 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3425 char *bname;
3426 int l;
3427 bname = strrchr(buf,'/');
3428 #if defined(__MINGW32__) || defined(__CYGWIN__)
3429 if(!bname) bname = strrchr(buf,'\\');
3430 #endif
3431 if(bname) bname++;
3432 else bname = buf;
3433 l = strlen(psub) + strlen(bname) + 1;
3434 psub = realloc(psub,l);
3435 strcat(psub,bname);
3436 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3437 free(psub);
3439 free(buf);
3441 if(vo_vobsub){
3442 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3443 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3445 // setup global sub numbering
3446 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3447 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3450 //============ Open & Sync STREAM --- fork cache2 ====================
3452 mpctx->stream=NULL;
3453 mpctx->demuxer=NULL;
3454 if (mpctx->d_audio) {
3455 //free_demuxer_stream(mpctx->d_audio);
3456 mpctx->d_audio=NULL;
3458 if (mpctx->d_video) {
3459 //free_demuxer_stream(d_video);
3460 mpctx->d_video=NULL;
3462 mpctx->sh_audio=NULL;
3463 mpctx->sh_video=NULL;
3465 current_module="open_stream";
3466 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3467 if(!mpctx->stream) { // error...
3468 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3469 goto goto_next_file;
3471 mpctx->initialized_flags|=INITIALIZED_STREAM;
3473 #ifdef CONFIG_GUI
3474 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3475 #endif
3477 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3478 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3479 "playlist support will not be used automatically.\n"
3480 "MPlayer's playlist code is unsafe and should only be used with "
3481 "trusted sources.\nPlayback will probably fail.\n\n");
3482 #if 0
3483 play_tree_t* entry;
3484 // Handle playlist
3485 current_module="handle_playlist";
3486 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3487 filename_recode(mpctx->filename));
3488 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3489 mpctx->eof=playtree_add_playlist(mpctx, entry);
3490 goto goto_next_file;
3491 #endif
3493 mpctx->stream->start_pos+=seek_to_byte;
3495 if(stream_dump_type==5){
3496 unsigned char buf[4096];
3497 int len;
3498 FILE *f;
3499 current_module="dumpstream";
3500 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3501 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpstreamFdUnavailable);
3502 exit_player(mpctx, EXIT_ERROR);
3504 stream_reset(mpctx->stream);
3505 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3506 f=fopen(stream_dump_name,"wb");
3507 if(!f){
3508 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3509 exit_player(mpctx, EXIT_ERROR);
3511 if (dvd_chapter > 1) {
3512 int chapter = dvd_chapter - 1;
3513 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3515 while(!mpctx->stream->eof && !async_quit_request){
3516 len=stream_read(mpctx->stream,buf,4096);
3517 if(len>0) {
3518 if(fwrite(buf,len,1,f) != 1) {
3519 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3520 exit_player(mpctx, EXIT_ERROR);
3523 if(dvd_last_chapter > 0) {
3524 int chapter = -1;
3525 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3526 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3527 break;
3530 if(fclose(f)) {
3531 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile,stream_dump_name);
3532 exit_player(mpctx, EXIT_ERROR);
3534 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3535 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3538 #ifdef CONFIG_DVDREAD
3539 if(mpctx->stream->type==STREAMTYPE_DVD){
3540 current_module="dvd lang->id";
3541 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3542 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3543 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3544 // setup global sub numbering
3545 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3546 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3547 current_module=NULL;
3549 #endif
3551 #ifdef CONFIG_DVDNAV
3552 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3553 current_module="dvdnav lang->id";
3554 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3555 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3556 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3557 // setup global sub numbering
3558 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3559 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3560 current_module=NULL;
3562 #endif
3564 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3565 goto_enable_cache:
3566 if(stream_cache_size>0){
3567 current_module="enable_cache";
3568 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3569 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3570 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3571 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3574 //============ Open DEMUXERS --- DETECT file type =======================
3575 current_module="demux_open";
3577 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3579 // HACK to get MOV Reference Files working
3581 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3583 unsigned char* playlist_entry;
3584 play_tree_t *list = NULL, *entry = NULL;
3586 current_module="handle_demux_playlist";
3587 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3589 char *temp, *bname;
3591 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3592 filename_recode(playlist_entry));
3594 bname=mp_basename(playlist_entry);
3595 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3596 continue;
3598 if (!strncmp(bname,mp_basename(mpctx->filename),strlen(bname))) // ignoring self-reference
3599 continue;
3601 entry = play_tree_new();
3603 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3605 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3606 if (temp)
3608 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3609 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3610 strcat(temp, playlist_entry);
3611 play_tree_add_file(entry,temp);
3612 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3613 free(temp);
3616 else
3617 play_tree_add_file(entry,playlist_entry);
3619 if(!list)
3620 list = entry;
3621 else
3622 play_tree_append_entry(list,entry);
3624 free_demuxer(mpctx->demuxer);
3625 mpctx->demuxer = NULL;
3627 if (list)
3629 entry = play_tree_new();
3630 play_tree_set_child(entry,list);
3631 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3632 goto goto_next_file;
3636 if(!mpctx->demuxer)
3637 goto goto_next_file;
3639 if (mpctx->demuxer->matroska_data.ordered_chapters)
3640 build_ordered_chapter_timeline(mpctx);
3642 if (!mpctx->sources) {
3643 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
3644 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
3645 .demuxer = mpctx->demuxer};
3646 mpctx->num_sources = 1;
3649 if(dvd_chapter>1) {
3650 double pts;
3651 if (seek_chapter(mpctx, dvd_chapter-1, &pts, NULL) >= 0 && pts > -1.0)
3652 seek(mpctx, pts, SEEK_ABSOLUTE);
3655 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3657 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3658 int i;
3659 int maxid = -1;
3660 // setup global sub numbering
3661 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3662 for (i = 0; i < MAX_S_STREAMS; i++)
3663 if (mpctx->demuxer->s_streams[i])
3664 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3665 mpctx->global_sub_size += maxid + 1;
3667 // Make opts->sub_id always selectable if set.
3668 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3669 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3671 #ifdef CONFIG_ASS
3672 if (ass_enabled && ass_library) {
3673 for (int j = 0; j < mpctx->num_sources; j++) {
3674 struct demuxer *d = mpctx->sources[j].demuxer;
3675 for (int i = 0; i < d->num_attachments; i++) {
3676 struct demux_attachment *att = d->attachments + i;
3677 if (extract_embedded_fonts
3678 && att->name && att->type && att->data && att->data_size
3679 && (strcmp(att->type, "application/x-truetype-font") == 0
3680 || strcmp(att->type, "application/x-font") == 0))
3681 ass_add_font(ass_library, att->name, att->data, att->data_size);
3685 #endif
3687 current_module="demux_open2";
3689 //file_format=demuxer->file_format;
3691 mpctx->d_audio=mpctx->demuxer->audio;
3692 mpctx->d_video=mpctx->demuxer->video;
3693 mpctx->d_sub=mpctx->demuxer->sub;
3695 // select audio stream
3696 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3698 // DUMP STREAMS:
3699 if((stream_dump_type)&&(stream_dump_type<4)){
3700 FILE *f;
3701 demux_stream_t *ds=NULL;
3702 current_module="dump";
3703 // select stream to dump
3704 switch(stream_dump_type){
3705 case 1: ds=mpctx->d_audio;break;
3706 case 2: ds=mpctx->d_video;break;
3707 case 3: ds=mpctx->d_sub;break;
3709 if(!ds){
3710 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_DumpSelectedStreamMissing);
3711 exit_player(mpctx, EXIT_ERROR);
3713 // disable other streams:
3714 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3715 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3716 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3717 // let's dump it!
3718 f=fopen(stream_dump_name,"wb");
3719 if(!f){
3720 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_CantOpenDumpfile);
3721 exit_player(mpctx, EXIT_ERROR);
3723 while(!ds->eof){
3724 unsigned char* start;
3725 int in_size=ds_get_packet(ds,&start);
3726 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3727 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3728 if(in_size>0) fwrite(start,in_size,1,f);
3729 if(dvd_last_chapter>0) {
3730 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3731 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3732 break;
3735 fclose(f);
3736 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CoreDumped);
3737 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3740 mpctx->sh_audio=mpctx->d_audio->sh;
3741 mpctx->sh_video=mpctx->d_video->sh;
3743 if(mpctx->sh_video){
3745 current_module="video_read_properties";
3746 if(!video_read_properties(mpctx->sh_video)) {
3747 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CannotReadVideoProperties);
3748 mpctx->sh_video=mpctx->d_video->sh=NULL;
3749 } else {
3750 mp_msg(MSGT_CPLAYER,MSGL_V,MSGTR_FilefmtFourccSizeFpsFtime,
3751 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3752 mpctx->sh_video->fps,mpctx->sh_video->frametime
3755 /* need to set fps here for output encoders to pick it up in their init */
3756 if(force_fps){
3757 mpctx->sh_video->fps=force_fps;
3758 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3760 vo_fps = mpctx->sh_video->fps;
3762 if(!mpctx->sh_video->fps && !force_fps){
3763 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_FPSnotspecified);
3764 mpctx->sh_video=mpctx->d_video->sh=NULL;
3770 if(!mpctx->sh_video && !mpctx->sh_audio){
3771 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NoStreamFound);
3772 #ifdef CONFIG_DVBIN
3773 if(mpctx->stream->type == STREAMTYPE_DVB)
3775 int dir;
3776 int v = mpctx->last_dvb_step;
3777 if(v > 0)
3778 dir = DVB_CHANNEL_HIGHER;
3779 else
3780 dir = DVB_CHANNEL_LOWER;
3782 if(dvb_step_channel(mpctx->stream, dir)) {
3783 mpctx->stop_play = PT_NEXT_ENTRY;
3784 mpctx->dvbin_reopen = 1;
3787 #endif
3788 goto goto_next_file; // exit_player(MSGTR_Exit_error);
3791 /* display clip info */
3792 demux_info_print(mpctx->demuxer);
3794 //================== Read SUBTITLES (DVD & TEXT) ==========================
3795 if(vo_spudec==NULL && mpctx->sh_video &&
3796 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3797 init_vo_spudec(mpctx);
3800 if(mpctx->sh_video) {
3801 // after reading video params we should load subtitles because
3802 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3803 // check .sub
3804 current_module="read_subtitles_file";
3805 if(sub_name){
3806 for (i = 0; sub_name[i] != NULL; ++i)
3807 add_subtitles(mpctx, sub_name[i], mpctx->sh_video->fps, 0);
3809 if(sub_auto) { // auto load sub file ...
3810 char *psub = get_path( "sub/" );
3811 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3812 int i = 0;
3813 free(psub); // release the buffer created by get_path() above
3814 while (tmp[i]) {
3815 add_subtitles(mpctx, tmp[i], mpctx->sh_video->fps, 1);
3816 free(tmp[i++]);
3818 free(tmp);
3820 if (mpctx->set_of_sub_size > 0) {
3821 // setup global sub numbering
3822 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3823 mpctx->global_sub_size += mpctx->set_of_sub_size;
3827 if (mpctx->global_sub_size) {
3828 // find the best sub to use
3829 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3830 mpctx->global_sub_pos = -1; // no subs by default
3831 if (vobsub_index_id >= 0) {
3832 // if user asks for a vobsub id, use that first.
3833 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3834 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3835 // if user asks for a dvd sub id, use that next.
3836 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3837 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3838 // if there are text subs to use, use those. (autosubs come last here)
3839 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3840 } else if (opts->sub_id < 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3841 // finally select subs by language and container hints
3842 if (opts->sub_id < 0 && dvdsub_lang)
3843 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3844 if (opts->sub_id < 0)
3845 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3846 if (opts->sub_id >= 0)
3847 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3849 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3850 mpctx->global_sub_pos--;
3851 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3852 if(subdata)
3853 switch (stream_dump_type) {
3854 case 3: list_sub_file(subdata); break;
3855 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3856 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3857 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3858 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3859 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3863 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3864 filename_recode(mpctx->filename));
3865 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3866 if (mpctx->sh_video) {
3867 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3868 if (mpctx->sh_video->format >= 0x20202020)
3869 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3870 else
3871 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3872 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3873 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3874 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3875 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3876 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3878 if (mpctx->sh_audio) {
3879 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3880 if (mpctx->sh_audio->format >= 0x20202020)
3881 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3882 else
3883 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3884 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3885 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3886 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3888 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3889 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n", mpctx->stream->seek ? 1 : 0);
3890 if (mpctx->demuxer) {
3891 if (mpctx->demuxer->num_chapters == 0)
3892 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3893 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3896 if(!mpctx->sh_video) goto main; // audio-only
3898 if(!reinit_video_chain(mpctx)) {
3899 if(!mpctx->sh_video){
3900 if(!mpctx->sh_audio) goto goto_next_file;
3901 goto main; // exit_player(MSGTR_Exit_error);
3905 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3906 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3908 #ifdef CONFIG_FREETYPE
3909 force_load_font = 1;
3910 #endif
3912 //================== MAIN: ==========================
3913 main:
3914 current_module="main";
3916 if(playing_msg) {
3917 char* msg = property_expand_string(mpctx, playing_msg);
3918 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3919 free(msg);
3923 // Disable the term OSD in verbose mode
3924 if(verbose) term_osd = 0;
3927 //int frame_corr_num=0; //
3928 //float v_frame=0; // Video
3929 //float num_frames=0; // number of frames played
3931 int frame_time_remaining=0; // flag
3932 int blit_frame=0;
3933 mpctx->num_buffered_frames=0;
3935 // Make sure old OSD does not stay around,
3936 // e.g. with -fixed-vo and same-resolution files
3937 clear_osd_msgs();
3938 update_osd_msg(mpctx);
3940 //================ SETUP AUDIO ==========================
3942 if(mpctx->sh_audio){
3943 reinit_audio_chain(mpctx);
3944 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3945 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3948 current_module="av_init";
3950 if(mpctx->sh_video){
3951 mpctx->sh_video->timer=0;
3952 if (! ignore_start)
3953 audio_delay += mpctx->sh_video->stream_delay;
3955 if(mpctx->sh_audio){
3956 if (start_volume >= 0)
3957 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3958 if (! ignore_start)
3959 audio_delay -= mpctx->sh_audio->stream_delay;
3960 mpctx->delay=-audio_delay;
3963 if(!mpctx->sh_audio){
3964 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_NoSound);
3965 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3966 ds_free_packs(mpctx->d_audio); // free buffered chunks
3967 //mpctx->d_audio->id=-2; // do not read audio chunks
3968 //uninit_player(mpctx, INITIALIZED_AO); // close device
3970 if(!mpctx->sh_video){
3971 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Video_NoVideo);
3972 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3973 ds_free_packs(mpctx->d_video);
3974 mpctx->d_video->id=-2;
3975 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3978 if (!mpctx->sh_video && !mpctx->sh_audio)
3979 goto goto_next_file;
3981 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3982 if(force_fps && mpctx->sh_video){
3983 vo_fps = mpctx->sh_video->fps=force_fps;
3984 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3985 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_FPSforced,mpctx->sh_video->fps,mpctx->sh_video->frametime);
3988 #ifdef CONFIG_GUI
3989 if ( use_gui ) {
3990 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
3991 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
3992 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
3993 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
3994 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
3996 #endif
3998 mp_input_set_section(mpctx->input, NULL);
3999 //TODO: add desired (stream-based) sections here
4000 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
4001 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
4003 //==================== START PLAYING =======================
4005 if(opts->loop_times>1) opts->loop_times--; else
4006 if(opts->loop_times==1) opts->loop_times = -1;
4008 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_StartPlaying);
4010 total_time_usage_start=GetTimer();
4011 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
4012 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
4013 play_n_frames=play_n_frames_mf;
4015 if(play_n_frames==0){
4016 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
4019 if (seek_to_sec) {
4020 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
4021 end_at.pos += seek_to_sec;
4024 if (end_at.type == END_AT_SIZE) {
4025 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_MPEndposNoSizeBased);
4026 end_at.type = END_AT_NONE;
4029 #ifdef CONFIG_DVDNAV
4030 mp_dvdnav_context_free(mpctx);
4031 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4032 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
4033 mp_dvdnav_cell_has_changed(mpctx->stream,1);
4035 #endif
4037 get_relative_time(mpctx); // reset current delta
4038 mpctx->time_frame = 0;
4039 mpctx->drop_message_shown = 0;
4040 mpctx->update_video_immediately = true;
4041 mpctx->total_avsync_change = 0;
4042 // Make sure VO knows current pause state
4043 if (mpctx->sh_video)
4044 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
4045 NULL);
4047 while(!mpctx->stop_play){
4048 float aq_sleep_time=0;
4050 if(dvd_last_chapter>0) {
4051 int cur_chapter = get_current_chapter(mpctx);
4052 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
4053 goto goto_next_file;
4056 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
4057 mpctx->sh_audio = mpctx->d_audio->sh;
4058 mpctx->sh_audio->ds = mpctx->d_audio;
4059 reinit_audio_chain(mpctx);
4062 /*========================== PLAY AUDIO ============================*/
4064 if (mpctx->sh_audio && !mpctx->paused)
4065 if (!fill_audio_out_buffers(mpctx))
4066 // at eof, all audio at least written to ao
4067 if (!mpctx->sh_video)
4068 mpctx->stop_play = AT_END_OF_FILE;
4071 if(!mpctx->sh_video) {
4072 // handle audio-only case:
4073 double a_pos=0;
4074 // sh_audio can be NULL due to video stream switching
4075 // TODO: handle this better
4076 if (mpctx->sh_audio)
4077 a_pos = playing_audio_pts(mpctx);
4079 print_status(mpctx, a_pos, false);
4081 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
4082 mpctx->stop_play = PT_NEXT_ENTRY;
4083 update_osd_msg(mpctx);
4085 } else {
4087 /*========================== PLAY VIDEO ============================*/
4089 vo_pts=mpctx->sh_video->timer*90000.0;
4090 vo_fps=mpctx->sh_video->fps;
4092 if (!mpctx->num_buffered_frames) {
4093 double frame_time = update_video(mpctx, &blit_frame);
4094 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
4095 if (mpctx->sh_video->vf_initialized < 0) {
4096 mp_msg(MSGT_CPLAYER,MSGL_FATAL, MSGTR_NotInitializeVOPorVO);
4097 mpctx->stop_play = PT_NEXT_ENTRY;
4098 goto goto_next_file;
4100 if (blit_frame) {
4101 struct vf_instance *vf = mpctx->sh_video->vfilter;
4102 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
4103 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
4104 vo_osd_changed(0);
4106 if (frame_time < 0)
4107 mpctx->stop_play = AT_END_OF_FILE;
4108 else {
4109 // might return with !eof && !blit_frame if !correct_pts
4110 mpctx->num_buffered_frames += blit_frame;
4111 if (mpctx->update_video_immediately) {
4112 // Show this frame immediately, rest normally
4113 mpctx->update_video_immediately = false;
4114 } else {
4115 mpctx->time_frame += frame_time / opts->playback_speed;
4116 adjust_sync(mpctx, frame_time);
4120 if (mpctx->timeline) {
4121 struct timeline_part *next = mpctx->timeline + mpctx->timeline_part + 1;
4122 if (mpctx->sh_video->pts >= next->start
4123 || mpctx->stop_play == AT_END_OF_FILE
4124 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
4125 seek(mpctx, next->start, SEEK_ABSOLUTE);
4126 continue;
4130 // ==========================================================================
4132 // current_module="draw_osd";
4133 // if(vo_config_count) mpctx->video_out->draw_osd();
4135 #ifdef CONFIG_GUI
4136 if(use_gui) guiEventHandling();
4137 #endif
4139 current_module="vo_check_events";
4140 vo_check_events(mpctx->video_out);
4142 #ifdef CONFIG_X11
4143 if (stop_xscreensaver) {
4144 current_module = "stop_xscreensaver";
4145 xscreensaver_heartbeat(mpctx->x11_state);
4147 #endif
4148 if (heartbeat_cmd) {
4149 static unsigned last_heartbeat;
4150 unsigned now = GetTimerMS();
4151 if (now - last_heartbeat > 30000) {
4152 last_heartbeat = now;
4153 system(heartbeat_cmd);
4157 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
4159 //====================== FLIP PAGE (VIDEO BLT): =========================
4161 current_module="flip_page";
4162 if (!frame_time_remaining && blit_frame) {
4163 unsigned int t2=GetTimer();
4165 vo_flip_page(mpctx->video_out);
4166 mpctx->num_buffered_frames--;
4168 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
4169 vout_time_usage += mpctx->last_vo_flip_duration;
4170 print_status(mpctx, MP_NOPTS_VALUE, true);
4172 else
4173 print_status(mpctx, MP_NOPTS_VALUE, false);
4175 //============================ Auto QUALITY ============================
4177 /*Output quality adjustments:*/
4178 if(auto_quality>0){
4179 current_module="autoq";
4180 // float total=0.000001f * (GetTimer()-aq_total_time);
4181 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4182 if(output_quality<auto_quality && aq_sleep_time>0)
4183 ++output_quality;
4184 else
4185 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4186 if(output_quality>1 && aq_sleep_time<0)
4187 --output_quality;
4188 else
4189 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4190 output_quality=0;
4191 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4192 set_video_quality(mpctx->sh_video,output_quality);
4195 if (!frame_time_remaining && blit_frame) {
4196 if (play_n_frames >= 0) {
4197 --play_n_frames;
4198 if (play_n_frames <= 0)
4199 mpctx->stop_play = PT_NEXT_ENTRY;
4201 if (mpctx->step_frames > 0) {
4202 mpctx->step_frames--;
4203 if (mpctx->step_frames == 0)
4204 pause_player(mpctx);
4209 // FIXME: add size based support for -endpos
4210 if (end_at.type == END_AT_TIME &&
4211 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
4212 mpctx->stop_play = PT_NEXT_ENTRY;
4214 } // end if(mpctx->sh_video)
4216 #ifdef CONFIG_DVDNAV
4217 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4218 nav_highlight_t hl;
4219 mp_dvdnav_get_highlight (mpctx->stream, &hl);
4220 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4221 vo_osd_changed (OSDTYPE_DVDNAV);
4223 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
4224 double ar = -1.0;
4225 if (stream_control (mpctx->demuxer->stream,
4226 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
4227 != STREAM_UNSUPPORTED)
4228 mpctx->sh_video->stream_aspect = ar;
4231 #endif
4233 //================= Keyboard events, SEEKing ====================
4235 current_module="key_events";
4238 while (1) {
4239 mp_cmd_t* cmd;
4240 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
4241 run_command(mpctx, cmd);
4242 mp_cmd_free(cmd);
4243 if (mpctx->stop_play)
4244 break;
4246 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
4247 || mpctx->abs_seek_pos)
4248 break;
4249 if (mpctx->sh_video) {
4250 update_osd_msg(mpctx);
4251 int hack = vo_osd_changed(0);
4252 vo_osd_changed(hack);
4253 if (hack)
4254 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
4255 add_step_frame(mpctx);
4256 break;
4258 else
4259 vo_osd_changed(0);
4261 pause_loop(mpctx);
4265 // handle -sstep
4266 if (step_sec > 0 && !mpctx->paused) {
4267 mpctx->osd_function=OSD_FFW;
4268 mpctx->rel_seek_secs+=step_sec;
4271 edl_update(mpctx);
4273 /* Looping. */
4274 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
4275 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
4277 if(opts->loop_times>1) opts->loop_times--; else
4278 if(opts->loop_times==1) opts->loop_times=-1;
4279 play_n_frames=play_n_frames_mf;
4280 mpctx->stop_play=0;
4281 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
4284 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
4285 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
4287 mpctx->rel_seek_secs=0;
4288 mpctx->abs_seek_pos=0;
4291 #ifdef CONFIG_GUI
4292 if(use_gui){
4293 guiEventHandling();
4294 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
4295 // get pos from frame number / total frames
4296 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
4297 } else {
4298 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
4300 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
4301 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx);
4302 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
4303 guiGetEvent( guiReDraw,NULL );
4304 guiGetEvent( guiSetVolume,NULL );
4305 if(guiIntfStruct.Playing==0) break; // STOP
4306 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
4307 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
4308 #ifdef CONFIG_DVDREAD
4309 if ( mpctx->stream->type == STREAMTYPE_DVD )
4311 dvd_priv_t * dvdp = mpctx->stream->priv;
4312 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
4314 #endif
4316 #endif /* CONFIG_GUI */
4318 } // while(!mpctx->stop_play)
4320 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4322 #ifdef CONFIG_DVBIN
4323 if(mpctx->dvbin_reopen)
4325 mpctx->stop_play = 0;
4326 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4327 cache_uninit(mpctx->stream);
4328 mpctx->dvbin_reopen = 0;
4329 goto goto_enable_cache;
4331 #endif
4334 goto_next_file: // don't jump here after ao/vo/getch initialization!
4336 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4338 if(benchmark){
4339 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4340 double total_time_usage;
4341 total_time_usage_start=GetTimer()-total_time_usage_start;
4342 total_time_usage = (float)total_time_usage_start*0.000001;
4343 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4344 video_time_usage,vout_time_usage,audio_time_usage,
4345 total_time_usage-tot,total_time_usage);
4346 if(total_time_usage>0.0)
4347 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4348 100.0*video_time_usage/total_time_usage,
4349 100.0*vout_time_usage/total_time_usage,
4350 100.0*audio_time_usage/total_time_usage,
4351 100.0*(total_time_usage-tot)/total_time_usage,
4352 100.0);
4353 if(total_frame_cnt && frame_dropping)
4354 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4355 total_frame_cnt-drop_frame_cnt,
4356 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4357 drop_frame_cnt,
4358 100*drop_frame_cnt/total_frame_cnt,
4359 total_frame_cnt,
4360 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4363 // time to uninit all, except global stuff:
4364 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI+(opts->fixed_vo?INITIALIZED_VO:0)));
4366 if(mpctx->set_of_sub_size > 0) {
4367 current_module="sub_free";
4368 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4369 sub_free(mpctx->set_of_subtitles[i]);
4370 #ifdef CONFIG_ASS
4371 if(mpctx->set_of_ass_tracks[i])
4372 ass_free_track( mpctx->set_of_ass_tracks[i] );
4373 #endif
4375 mpctx->set_of_sub_size = 0;
4377 vo_sub_last = vo_sub=NULL;
4378 subdata=NULL;
4379 #ifdef CONFIG_ASS
4380 ass_track = NULL;
4381 if(ass_library)
4382 ass_clear_fonts(ass_library);
4383 #endif
4385 if (!mpctx->stop_play) // In case some goto jumped here...
4386 mpctx->stop_play = PT_NEXT_ENTRY;
4388 int playtree_direction = 1;
4390 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4391 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4392 play_tree_iter_free(mpctx->playtree_iter);
4393 mpctx->playtree_iter = NULL;
4395 mpctx->play_tree_step = 1;
4396 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4397 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4398 if(mpctx->playtree_iter) {
4399 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4400 play_tree_iter_free(mpctx->playtree_iter);
4401 mpctx->playtree_iter = NULL;
4404 } else if (mpctx->stop_play == PT_STOP) {
4405 play_tree_iter_free(mpctx->playtree_iter);
4406 mpctx->playtree_iter = NULL;
4407 } else { // NEXT PREV SRC
4408 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4411 while(mpctx->playtree_iter != NULL) {
4412 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4413 if(mpctx->filename == NULL) {
4414 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4415 play_tree_iter_free(mpctx->playtree_iter);
4416 mpctx->playtree_iter = NULL;
4418 } else
4419 break;
4422 #ifdef CONFIG_GUI
4423 if(use_gui && !mpctx->playtree_iter) {
4424 #ifdef CONFIG_DVDREAD
4425 if(!guiIntfStruct.DiskChanged)
4426 #endif
4427 mplEnd();
4429 #endif
4431 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4432 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4433 mpctx->stop_play = 0;
4434 goto play_next_file;
4438 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4440 return 1;
4442 #endif /* DISABLE_MAIN */