Remove trailing whitespace from most files
[mplayer/glamo.git] / mplayer.c
blob543896c834a30dd5a7eb5a9b181dc3e52128ef6e
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 #define Exit_SIGILL_RTCpuSel _(\
177 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
178 " It may be a bug in our new runtime CPU-detection code...\n"\
179 " Please read DOCS/HTML/en/bugreports.html.\n")
181 #define Exit_SIGILL _(\
182 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
183 " It usually happens when you run it on a CPU different than the one it was\n"\
184 " compiled/optimized for.\n"\
185 " Verify this!\n")
187 #define Exit_SIGSEGV_SIGFPE _(\
188 "- MPlayer crashed by bad usage of CPU/FPU/RAM.\n"\
189 " Recompile MPlayer with --enable-debug and make a 'gdb' backtrace and\n"\
190 " disassembly. Details in DOCS/HTML/en/bugreports_what.html#bugreports_crash.\n")
192 #define Exit_SIGCRASH _(\
193 "- MPlayer crashed. This shouldn't happen.\n"\
194 " It can be a bug in the MPlayer code _or_ in your drivers _or_ in your\n"\
195 " gcc version. If you think it's MPlayer's fault, please read\n"\
196 " DOCS/HTML/en/bugreports.html and follow the instructions there. We can't and\n"\
197 " won't help unless you provide this information when reporting a possible bug.\n")
199 #define SystemTooSlow _("\n\n"\
200 " ************************************************\n"\
201 " **** Your system is too SLOW to play this! ****\n"\
202 " ************************************************\n\n"\
203 "Possible reasons, problems, workarounds:\n"\
204 "- Most common: broken/buggy _audio_ driver\n"\
205 " - Try -ao sdl or use the OSS emulation of ALSA.\n"\
206 " - Experiment with different values for -autosync, 30 is a good start.\n"\
207 "- Slow video output\n"\
208 " - Try a different -vo driver (-vo help for a list) or try -framedrop!\n"\
209 "- Slow CPU\n"\
210 " - Don't try to play a big DVD/DivX on a slow CPU! Try some of the lavdopts,\n"\
211 " e.g. -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all.\n"\
212 "- Broken file\n"\
213 " - Try various combinations of -nobps -ni -forceidx -mc 0.\n"\
214 "- Slow media (NFS/SMB mounts, DVD, VCD etc)\n"\
215 " - Try -cache 8192.\n"\
216 "- Are you using -cache to play a non-interleaved AVI file?\n"\
217 " - Try -nocache.\n"\
218 "Read DOCS/HTML/en/video.html for tuning/speedup tips.\n"\
219 "If none of this helps you, read DOCS/HTML/en/bugreports.html.\n\n")
222 //**************************************************************************//
223 //**************************************************************************//
225 // Common FIFO functions, and keyboard/event FIFO code
226 #include "mp_fifo.h"
227 int noconsolecontrols=0;
228 //**************************************************************************//
230 // benchmark:
231 double video_time_usage=0;
232 double vout_time_usage=0;
233 static double audio_time_usage=0;
234 static int total_time_usage_start=0;
235 static int total_frame_cnt=0;
236 static int drop_frame_cnt=0; // total number of dropped frames
237 int benchmark=0;
239 // options:
240 int auto_quality=0;
241 static int output_quality=0;
243 int use_gui=0;
245 #ifdef CONFIG_GUI
246 int enqueue=0;
247 #endif
249 static int list_properties = 0;
251 int term_osd = 1;
252 static char* term_osd_esc = "\x1b[A\r\x1b[K";
253 static char* playing_msg = NULL;
254 // seek:
255 static double seek_to_sec;
256 static off_t seek_to_byte=0;
257 static off_t step_sec=0;
259 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
261 // A/V sync:
262 int autosync=0; // 30 might be a good default value.
264 // codecs:
265 char **audio_codec_list=NULL; // override audio codec
266 char **video_codec_list=NULL; // override video codec
267 char **audio_fm_list=NULL; // override audio codec family
268 char **video_fm_list=NULL; // override video codec family
270 // demuxer:
271 extern char *demuxer_name; // override demuxer
272 extern char *audio_demuxer_name; // override audio demuxer
273 extern char *sub_demuxer_name; // override sub demuxer
275 int vobsub_id=-1;
276 char* audio_lang=NULL;
277 char* dvdsub_lang=NULL;
278 static char* spudec_ifo=NULL;
279 int forced_subs_only=0;
280 int file_filter=1;
282 // cache2:
283 int stream_cache_size=-1;
284 #ifdef CONFIG_STREAM_CACHE
285 extern int cache_fill_status;
287 float stream_cache_min_percent=20.0;
288 float stream_cache_seek_min_percent=50.0;
289 #else
290 #define cache_fill_status 0
291 #endif
293 // dump:
294 static char *stream_dump_name="stream.dump";
295 int stream_dump_type=0;
297 // A-V sync:
298 static float default_max_pts_correction=-1;//0.01f;
299 float audio_delay=0;
300 static int ignore_start=0;
302 static int softsleep=0;
304 double force_fps=0;
305 static int force_srate=0;
306 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
307 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
308 static int play_n_frames=-1;
309 static int play_n_frames_mf=-1;
311 // sub:
312 char *font_name=NULL;
313 char *sub_font_name=NULL;
314 extern int font_fontconfig;
315 float font_factor=0.75;
316 char **sub_name=NULL;
317 float sub_delay=0;
318 float sub_fps=0;
319 int sub_auto = 1;
320 char *vobsub_name=NULL;
321 /*DSP!!char *dsp=NULL;*/
322 int subcc_enabled=0;
323 int suboverlap_enabled = 1;
325 #ifdef CONFIG_ASS
326 #include "libass/ass.h"
327 #include "libass/ass_mp.h"
328 #endif
330 char* current_module=NULL; // for debugging
333 // ---
335 #ifdef CONFIG_MENU
336 #include "m_struct.h"
337 #include "libmenu/menu.h"
338 void vf_menu_pause_update(struct vf_instance* vf);
339 extern vf_info_t vf_info_menu;
340 static const vf_info_t* const libmenu_vfs[] = {
341 &vf_info_menu,
342 NULL
344 static vf_instance_t* vf_menu = NULL;
345 int use_menu = 0;
346 static char* menu_cfg = NULL;
347 static char* menu_root = "main";
348 #endif
351 #ifdef HAVE_RTC
352 static int nortc = 1;
353 static char* rtc_device;
354 #endif
356 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
357 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
358 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
359 int use_filedir_conf;
361 #include "mpcommon.h"
362 #include "command.h"
364 #include "metadata.h"
366 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
368 const void *mpctx_get_video_out(MPContext *mpctx)
370 return mpctx->video_out;
373 const void *mpctx_get_audio_out(MPContext *mpctx)
375 return mpctx->audio_out;
378 void *mpctx_get_demuxer(MPContext *mpctx)
380 return mpctx->demuxer;
383 void *mpctx_get_playtree_iter(MPContext *mpctx)
385 return mpctx->playtree_iter;
388 void *mpctx_get_mixer(MPContext *mpctx)
390 return &mpctx->mixer;
393 int mpctx_get_global_sub_size(MPContext *mpctx)
395 return mpctx->global_sub_size;
398 int mpctx_get_osd_function(MPContext *mpctx)
400 return mpctx->osd_function;
403 static float get_relative_time(struct MPContext *mpctx)
405 unsigned int new_time = GetTimer();
406 unsigned int delta = new_time - mpctx->last_time;
407 mpctx->last_time = new_time;
408 return delta * 0.000001;
411 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
412 switch (type)
414 /* check for valid video stream */
415 case META_VIDEO_CODEC:
416 case META_VIDEO_BITRATE:
417 case META_VIDEO_RESOLUTION:
419 if (!mpctx->sh_video)
420 return 0;
421 break;
424 /* check for valid audio stream */
425 case META_AUDIO_CODEC:
426 case META_AUDIO_BITRATE:
427 case META_AUDIO_SAMPLES:
429 if (!mpctx->sh_audio)
430 return 0;
431 break;
434 /* check for valid demuxer */
435 case META_INFO_TITLE:
436 case META_INFO_ARTIST:
437 case META_INFO_ALBUM:
438 case META_INFO_YEAR:
439 case META_INFO_COMMENT:
440 case META_INFO_TRACK:
441 case META_INFO_GENRE:
443 if (!mpctx->demuxer)
444 return 0;
445 break;
448 default:
449 break;
452 return 1;
455 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
456 char **info = mpctx->demuxer->info;
457 int n;
459 if (!info || !tag)
460 return NULL;
462 for (n = 0; info[2*n] != NULL ; n++)
463 if (!strcmp (info[2*n], tag))
464 break;
466 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
469 char *get_metadata(struct MPContext *mpctx, metadata_t type)
471 char *meta = NULL;
472 sh_audio_t * const sh_audio = mpctx->sh_audio;
473 sh_video_t * const sh_video = mpctx->sh_video;
475 if (!is_valid_metadata_type(mpctx, type))
476 return NULL;
478 switch (type)
480 case META_NAME:
482 return strdup (mp_basename2 (mpctx->filename));
485 case META_VIDEO_CODEC:
487 if (sh_video->format == 0x10000001)
488 meta = strdup ("mpeg1");
489 else if (sh_video->format == 0x10000002)
490 meta = strdup ("mpeg2");
491 else if (sh_video->format == 0x10000004)
492 meta = strdup ("mpeg4");
493 else if (sh_video->format == 0x10000005)
494 meta = strdup ("h264");
495 else if (sh_video->format >= 0x20202020)
497 meta = malloc (8);
498 sprintf (meta, "%.4s", (char *) &sh_video->format);
500 else
502 meta = malloc (8);
503 sprintf (meta, "0x%08X", sh_video->format);
505 return meta;
508 case META_VIDEO_BITRATE:
510 meta = malloc (16);
511 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
512 return meta;
515 case META_VIDEO_RESOLUTION:
517 meta = malloc (16);
518 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
519 return meta;
522 case META_AUDIO_CODEC:
524 if (sh_audio->codec && sh_audio->codec->name)
525 meta = strdup (sh_audio->codec->name);
526 return meta;
529 case META_AUDIO_BITRATE:
531 meta = malloc (16);
532 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
533 return meta;
536 case META_AUDIO_SAMPLES:
538 meta = malloc (16);
539 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
540 return meta;
543 /* check for valid demuxer */
544 case META_INFO_TITLE:
545 return get_demuxer_info(mpctx, "Title");
547 case META_INFO_ARTIST:
548 return get_demuxer_info(mpctx, "Artist");
550 case META_INFO_ALBUM:
551 return get_demuxer_info(mpctx, "Album");
553 case META_INFO_YEAR:
554 return get_demuxer_info(mpctx, "Year");
556 case META_INFO_COMMENT:
557 return get_demuxer_info(mpctx, "Comment");
559 case META_INFO_TRACK:
560 return get_demuxer_info(mpctx, "Track");
562 case META_INFO_GENRE:
563 return get_demuxer_info(mpctx, "Genre");
565 default:
566 break;
569 return meta;
572 /// step size of mixer changes
573 int volstep = 3;
575 #ifdef CONFIG_DVDNAV
576 static void mp_dvdnav_context_free(MPContext *ctx){
577 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
578 ctx->nav_smpi = NULL;
579 if (ctx->nav_buffer) free(ctx->nav_buffer);
580 ctx->nav_buffer = NULL;
581 ctx->nav_start = NULL;
582 ctx->nav_in_size = 0;
584 #endif
586 void uninit_player(struct MPContext *mpctx, unsigned int mask){
587 mask=mpctx->initialized_flags&mask;
589 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
591 if(mask&INITIALIZED_ACODEC){
592 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
593 current_module="uninit_acodec";
594 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
595 #ifdef CONFIG_GUI
596 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
597 #endif
598 mpctx->sh_audio=NULL;
599 mpctx->mixer.afilter = NULL;
602 if(mask&INITIALIZED_VCODEC){
603 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
604 current_module="uninit_vcodec";
605 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
606 mpctx->sh_video=NULL;
607 #ifdef CONFIG_MENU
608 vf_menu=NULL;
609 #endif
612 if(mask&INITIALIZED_DEMUXER){
613 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
614 current_module="free_demuxer";
615 if (mpctx->num_sources) {
616 mpctx->demuxer = mpctx->sources[0].demuxer;
617 for (int i = 1; i < mpctx->num_sources; i++) {
618 free_stream(mpctx->sources[i].stream);
619 free_demuxer(mpctx->sources[i].demuxer);
622 talloc_free(mpctx->sources);
623 mpctx->sources = NULL;
624 mpctx->num_sources = 0;
625 talloc_free(mpctx->timeline);
626 mpctx->timeline = NULL;
627 mpctx->num_timeline_parts = 0;
628 talloc_free(mpctx->chapters);
629 mpctx->chapters = NULL;
630 mpctx->num_chapters = 0;
631 mpctx->video_offset = 0;
632 if(mpctx->demuxer){
633 mpctx->stream=mpctx->demuxer->stream;
634 free_demuxer(mpctx->demuxer);
636 mpctx->demuxer=NULL;
639 // kill the cache process:
640 if(mask&INITIALIZED_STREAM){
641 mpctx->initialized_flags&=~INITIALIZED_STREAM;
642 current_module="uninit_stream";
643 if(mpctx->stream) free_stream(mpctx->stream);
644 mpctx->stream=NULL;
647 if(mask&INITIALIZED_VO){
648 mpctx->initialized_flags&=~INITIALIZED_VO;
649 current_module="uninit_vo";
650 vo_destroy(mpctx->video_out);
651 mpctx->video_out=NULL;
652 #ifdef CONFIG_DVDNAV
653 mp_dvdnav_context_free(mpctx);
654 #endif
657 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
658 if(mask&INITIALIZED_GETCH2){
659 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
660 current_module="uninit_getch2";
661 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
662 // restore terminal:
663 getch2_disable();
666 if(mask&INITIALIZED_VOBSUB){
667 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
668 current_module="uninit_vobsub";
669 if(vo_vobsub) vobsub_close(vo_vobsub);
670 vo_vobsub=NULL;
673 if (mask&INITIALIZED_SPUDEC){
674 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
675 current_module="uninit_spudec";
676 spudec_free(vo_spudec);
677 vo_spudec=NULL;
680 if(mask&INITIALIZED_AO){
681 mpctx->initialized_flags&=~INITIALIZED_AO;
682 current_module="uninit_ao";
683 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
684 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
685 mpctx->audio_out=NULL;
688 #ifdef CONFIG_GUI
689 if(mask&INITIALIZED_GUI){
690 mpctx->initialized_flags&=~INITIALIZED_GUI;
691 current_module="uninit_gui";
692 guiDone();
694 #endif
696 current_module=NULL;
699 void exit_player_with_rc(struct MPContext *mpctx, exit_reason_t how, int rc)
701 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
702 uninit_player(mpctx, INITIALIZED_ALL);
703 #if defined(__MINGW32__) || defined(__CYGWIN__)
704 timeEndPeriod(1);
705 #endif
706 #ifdef CONFIG_X11
707 #ifdef CONFIG_GUI
708 if ( !use_gui )
709 #endif
710 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
711 #endif
713 current_module="uninit_input";
714 mp_input_uninit(mpctx->input);
715 #ifdef CONFIG_MENU
716 if (use_menu)
717 menu_uninit();
718 #endif
720 #ifdef CONFIG_FREETYPE
721 current_module="uninit_font";
722 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
723 free_font_desc(mpctx->osd->sub_font);
724 free_font_desc(vo_font);
725 vo_font = NULL;
726 done_freetype();
727 #endif
728 osd_free(mpctx->osd);
730 #ifdef CONFIG_ASS
731 ass_library_done(ass_library);
732 #endif
734 current_module="exit_player";
736 // free mplayer config
737 if(mpctx->mconfig)
738 m_config_free(mpctx->mconfig);
740 if(mpctx->playtree)
741 play_tree_free(mpctx->playtree, 1);
743 talloc_free(mpctx->key_fifo);
745 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
746 switch(how) {
747 case EXIT_QUIT:
748 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Quit");
749 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
750 break;
751 case EXIT_EOF:
752 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","End of file");
753 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
754 break;
755 case EXIT_ERROR:
756 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Fatal error");
757 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
758 break;
759 default:
760 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
762 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
764 exit(rc);
767 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
769 exit_player_with_rc(mpctx, how, 1);
772 #ifndef __MINGW32__
773 static void child_sighandler(int x){
774 pid_t pid;
775 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
777 #endif
779 #ifdef CONFIG_CRASH_DEBUG
780 static char *prog_path;
781 static int crash_debug = 0;
782 #endif
784 static void exit_sighandler(int x){
785 static int sig_count=0;
786 #ifdef CONFIG_CRASH_DEBUG
787 if (!crash_debug || x != SIGTRAP)
788 #endif
789 ++sig_count;
790 if(sig_count==5)
792 /* We're crashing bad and can't uninit cleanly :(
793 * by popular request, we make one last (dirty)
794 * effort to restore the user's
795 * terminal. */
796 getch2_disable();
797 exit(1);
799 if(sig_count==6) exit(1);
800 if(sig_count>6){
801 // can't stop :(
802 #ifndef __MINGW32__
803 kill(getpid(),SIGKILL);
804 #endif
806 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"\n" "\nMPlayer interrupted by signal %d in module: %s\n",x,
807 current_module?current_module:"unknown"
809 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
810 if(sig_count<=1)
811 switch(x){
812 case SIGINT:
813 case SIGQUIT:
814 case SIGTERM:
815 case SIGKILL:
816 async_quit_request = 1;
817 return; // killed from keyboard (^C) or killed [-9]
818 case SIGILL:
819 #if CONFIG_RUNTIME_CPUDETECT
820 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL_RTCpuSel);
821 #else
822 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL);
823 #endif
824 case SIGFPE:
825 case SIGSEGV:
826 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGSEGV_SIGFPE);
827 default:
828 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGCRASH);
829 #ifdef CONFIG_CRASH_DEBUG
830 if (crash_debug) {
831 int gdb_pid;
832 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
833 gdb_pid = fork();
834 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
835 if (gdb_pid == 0) { // We are the child
836 char spid[20];
837 snprintf(spid, sizeof(spid), "%i", getppid());
838 getch2_disable(); // allow terminal to work properly with gdb
839 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
840 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
841 } else if (gdb_pid < 0)
842 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
843 else {
844 waitpid(gdb_pid, NULL, 0);
846 if (x == SIGTRAP) return;
848 #endif
850 getch2_disable();
851 exit(1);
854 #include "cfg-mplayer.h"
856 static int cfg_include(m_option_t *conf, char *filename)
858 return m_config_parse_config_file(conf->priv, filename);
861 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
863 char *conffile;
864 int conffile_fd;
865 if (!disable_system_conf &&
866 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
867 exit_player(mpctx, EXIT_NONE);
868 if ((conffile = get_path("")) == NULL) {
869 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"Cannot find HOME directory.\n");
870 } else {
871 #ifdef __MINGW32__
872 mkdir(conffile);
873 #else
874 mkdir(conffile, 0777);
875 #endif
876 free(conffile);
877 if ((conffile = get_path("config")) == NULL) {
878 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
879 } else {
880 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
881 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Creating config file: %s\n", conffile);
882 write(conffile_fd, default_config, strlen(default_config));
883 close(conffile_fd);
885 if (!disable_user_conf &&
886 m_config_parse_config_file(conf, conffile) < 0)
887 exit_player(mpctx, EXIT_NONE);
888 free(conffile);
893 #define PROFILE_CFG_PROTOCOL "protocol."
895 static void load_per_protocol_config (m_config_t* conf, const char *const file)
897 char *str;
898 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
899 m_profile_t *p;
901 /* does filename actually uses a protocol ? */
902 str = strstr (file, "://");
903 if (!str)
904 return;
906 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
907 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
908 p = m_config_get_profile (conf, protocol);
909 if (p)
911 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading protocol-related profile '%s'\n", protocol);
912 m_config_set_profile(conf,p);
916 #define PROFILE_CFG_EXTENSION "extension."
918 static void load_per_extension_config (m_config_t* conf, const char *const file)
920 char *str;
921 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
922 m_profile_t *p;
924 /* does filename actually have an extension ? */
925 str = strrchr (file, '.');
926 if (!str)
927 return;
929 sprintf (extension, PROFILE_CFG_EXTENSION);
930 strncat (extension, ++str, 7);
931 p = m_config_get_profile (conf, extension);
932 if (p)
934 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", extension);
935 m_config_set_profile(conf,p);
939 #define PROFILE_CFG_VO "vo."
940 #define PROFILE_CFG_AO "ao."
942 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
944 char profile[strlen (cfg) + strlen (out) + 1];
945 m_profile_t *p;
947 sprintf (profile, "%s%s", cfg, out);
948 p = m_config_get_profile (conf, profile);
949 if (p)
951 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", profile);
952 m_config_set_profile(conf,p);
956 static void load_per_file_config (m_config_t* conf, const char *const file)
958 char *confpath;
959 char cfg[strlen(file)+10];
960 struct stat st;
961 char *name;
963 sprintf (cfg, "%s.conf", file);
965 if (use_filedir_conf && !stat (cfg, &st))
967 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading config '%s'\n", cfg);
968 m_config_parse_config_file (conf, cfg);
969 return;
972 if ((name = strrchr (cfg, '/')) == NULL)
973 name = cfg;
974 else
975 name++;
977 if ((confpath = get_path (name)) != NULL)
979 if (!stat (confpath, &st))
981 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading config '%s'\n", confpath);
982 m_config_parse_config_file (conf, confpath);
985 free (confpath);
989 /* When libmpdemux performs a blocking operation (network connection or
990 * cache filling) if the operation fails we use this function to check
991 * if it was interrupted by the user.
992 * The function returns a new value for eof. */
993 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
995 mp_cmd_t* cmd;
996 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
997 switch(cmd->id) {
998 case MP_CMD_QUIT:
999 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
1000 case MP_CMD_PLAY_TREE_STEP: {
1001 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
1002 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
1003 } break;
1004 case MP_CMD_PLAY_TREE_UP_STEP: {
1005 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
1006 } break;
1007 case MP_CMD_PLAY_ALT_SRC_STEP: {
1008 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
1009 } break;
1011 mp_cmd_free(cmd);
1013 return stop_play;
1016 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
1018 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
1020 play_tree_add_bpf(entry,mpctx->filename);
1022 #ifdef CONFIG_GUI
1023 if (use_gui) {
1024 if (entry) {
1025 import_playtree_playlist_into_gui(entry, mpctx->mconfig);
1026 play_tree_free_list(entry,1);
1028 } else
1029 #endif
1031 if(!entry) {
1032 entry = mpctx->playtree_iter->tree;
1033 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1034 return PT_NEXT_ENTRY;
1036 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
1037 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1038 return PT_NEXT_ENTRY;
1041 play_tree_remove(entry,1,1);
1042 return PT_NEXT_SRC;
1044 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
1045 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1046 entry = mpctx->playtree_iter->tree;
1047 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1048 return PT_NEXT_ENTRY;
1050 play_tree_remove(entry,1,1);
1052 return PT_NEXT_SRC;
1055 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1057 sub_data *subd;
1058 #ifdef CONFIG_ASS
1059 ass_track_t *asst = 0;
1060 #endif
1062 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1063 return;
1066 subd = sub_read_file(filename, fps);
1067 #ifdef CONFIG_ASS
1068 if (ass_enabled)
1069 #ifdef CONFIG_ICONV
1070 asst = ass_read_file(ass_library, filename, sub_cp);
1071 #else
1072 asst = ass_read_file(ass_library, filename, 0);
1073 #endif
1074 if (ass_enabled && subd && !asst)
1075 asst = ass_read_subdata(ass_library, subd, fps);
1077 if (!asst && !subd)
1078 #else
1079 if(!subd)
1080 #endif
1081 mp_tmsg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, "Cannot load subtitles: %s\n",
1082 filename_recode(filename));
1084 #ifdef CONFIG_ASS
1085 if (!asst && !subd) return;
1086 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1087 #else
1088 if (!subd) return;
1089 #endif
1090 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1091 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1092 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1093 filename_recode(filename));
1094 ++mpctx->set_of_sub_size;
1095 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "SUB: Added subtitle file (%d): %s\n", mpctx->set_of_sub_size,
1096 filename_recode(filename));
1099 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
1100 void update_set_of_subtitles(struct MPContext *mpctx)
1101 // subdata was changed, set_of_sub... have to be updated.
1103 sub_data ** const set_of_subtitles = mpctx->set_of_subtitles;
1104 int i;
1105 if (mpctx->set_of_sub_size > 0 && subdata == NULL) { // *subdata was deleted
1106 for (i = mpctx->set_of_sub_pos + 1; i < mpctx->set_of_sub_size; ++i)
1107 set_of_subtitles[i-1] = set_of_subtitles[i];
1108 set_of_subtitles[mpctx->set_of_sub_size-1] = NULL;
1109 --mpctx->set_of_sub_size;
1110 if (mpctx->set_of_sub_size > 0) subdata = set_of_subtitles[mpctx->set_of_sub_pos=0];
1112 else if (mpctx->set_of_sub_size > 0 && subdata != NULL) { // *subdata was changed
1113 set_of_subtitles[mpctx->set_of_sub_pos] = subdata;
1115 else if (mpctx->set_of_sub_size <= 0 && subdata != NULL) { // *subdata was added
1116 set_of_subtitles[mpctx->set_of_sub_pos=mpctx->set_of_sub_size] = subdata;
1117 ++mpctx->set_of_sub_size;
1121 void init_vo_spudec(struct MPContext *mpctx)
1123 if (vo_spudec)
1124 spudec_free(vo_spudec);
1125 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1126 vo_spudec = NULL;
1127 if (spudec_ifo) {
1128 unsigned int palette[16], width, height;
1129 current_module="spudec_init_vobsub";
1130 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1131 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1134 #ifdef CONFIG_DVDREAD
1135 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1136 current_module="spudec_init_dvdread";
1137 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1138 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1139 NULL, 0);
1141 #endif
1143 #ifdef CONFIG_DVDNAV
1144 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1145 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1146 current_module="spudec_init_dvdnav";
1147 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1149 #endif
1151 if (vo_spudec==NULL) {
1152 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1153 current_module="spudec_init_normal";
1154 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1155 spudec_set_font_factor(vo_spudec,font_factor);
1158 if (vo_spudec!=NULL) {
1159 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1160 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1165 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1166 * make it all work is to use the builtin SDL-bootstrap code, which
1167 * will be done automatically by replacing our main() if we include SDL.h.
1169 #if defined(__APPLE__) && defined(CONFIG_SDL)
1170 #include <SDL.h>
1171 #endif
1174 * \brief append a formatted string
1175 * \param buf buffer to print into
1176 * \param pos position of terminating 0 in buf
1177 * \param len maximum number of characters in buf, not including terminating 0
1178 * \param format printf format string
1180 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1182 va_list va;
1183 va_start(va, format);
1184 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1185 va_end(va);
1186 if (*pos >= len ) {
1187 buf[len] = 0;
1188 *pos = len;
1193 * \brief append time in the hh:mm:ss.f format
1194 * \param buf buffer to print into
1195 * \param pos position of terminating 0 in buf
1196 * \param len maximum number of characters in buf, not including terminating 0
1197 * \param time time value to convert/append
1199 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1200 long tenths = 10 * time;
1201 int f1 = tenths % 10;
1202 int ss = (tenths / 10) % 60;
1203 int mm = (tenths / 600) % 60;
1204 int hh = tenths / 36000;
1205 if (time <= 0) {
1206 saddf(buf, pos, len, "unknown");
1207 return;
1209 if (hh > 0)
1210 saddf(buf, pos, len, "%2d:", hh);
1211 if (hh > 0 || mm > 0)
1212 saddf(buf, pos, len, "%02d:", mm);
1213 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1216 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1218 struct MPOpts *opts = &mpctx->opts;
1219 sh_video_t * const sh_video = mpctx->sh_video;
1221 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1222 a_pos = playing_audio_pts(mpctx);
1223 if (mpctx->sh_audio && sh_video && at_frame) {
1224 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1225 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1226 && !mpctx->drop_message_shown) {
1227 mp_tmsg(MSGT_AVSYNC,MSGL_WARN,SystemTooSlow);
1228 mpctx->drop_message_shown = true;
1231 if (quiet)
1232 return;
1235 int width;
1236 char *line;
1237 unsigned pos = 0;
1238 get_screen_size();
1239 if (screen_width > 0)
1240 width = screen_width;
1241 else
1242 width = 80;
1243 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1244 /* Windows command line is broken (MinGW's rxvt works, but we
1245 * should not depend on that). */
1246 width--;
1247 #endif
1248 line = malloc(width + 1); // one additional char for the terminating null
1250 // Audio time
1251 if (mpctx->sh_audio) {
1252 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1253 if (!sh_video) {
1254 float len = demuxer_get_time_length(mpctx->demuxer);
1255 saddf(line, &pos, width, "(");
1256 sadd_hhmmssf(line, &pos, width, a_pos);
1257 saddf(line, &pos, width, ") of %.1f (", len);
1258 sadd_hhmmssf(line, &pos, width, len);
1259 saddf(line, &pos, width, ") ");
1263 // Video time
1264 if (sh_video)
1265 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1267 // A-V sync
1268 if (mpctx->sh_audio && sh_video)
1269 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1270 mpctx->last_av_difference, mpctx->total_avsync_change);
1272 // Video stats
1273 if (sh_video)
1274 saddf(line, &pos, width, "%3d/%3d ",
1275 (int)sh_video->num_frames,
1276 (int)sh_video->num_frames_decoded);
1278 // CPU usage
1279 if (sh_video) {
1280 if (sh_video->timer > 0.5)
1281 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1282 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1283 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1284 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1285 else
1286 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1287 } else if (mpctx->sh_audio) {
1288 if (mpctx->delay > 0.5)
1289 saddf(line, &pos, width, "%4.1f%% ",
1290 100.0*audio_time_usage/(double)mpctx->delay);
1291 else
1292 saddf(line, &pos, width, "??,?%% ");
1295 // VO stats
1296 if (sh_video)
1297 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1299 #ifdef CONFIG_STREAM_CACHE
1300 // cache stats
1301 if (stream_cache_size > 0)
1302 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1303 #endif
1305 // other
1306 if (opts->playback_speed != 1)
1307 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1309 // end
1310 if (erase_to_end_of_line) {
1311 line[pos] = 0;
1312 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1313 } else {
1314 memset(&line[pos], ' ', width - pos);
1315 line[width] = 0;
1316 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1318 free(line);
1322 * \brief build a chain of audio filters that converts the input format
1323 * to the ao's format, taking into account the current playback_speed.
1324 * \param sh_audio describes the requested input format of the chain.
1325 * \param ao_data describes the requested output format of the chain.
1327 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1329 struct MPOpts *opts = &mpctx->opts;
1330 int new_srate;
1331 int result;
1332 if (!sh_audio)
1334 #ifdef CONFIG_GUI
1335 if (use_gui) guiGetEvent(guiSetAfilter, (char *)NULL);
1336 #endif
1337 mpctx->mixer.afilter = NULL;
1338 return 0;
1340 if(af_control_any_rev(sh_audio->afilter,
1341 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1342 &opts->playback_speed)) {
1343 new_srate = sh_audio->samplerate;
1344 } else {
1345 new_srate = sh_audio->samplerate * opts->playback_speed;
1346 if (new_srate != ao_data->samplerate) {
1347 // limits are taken from libaf/af_resample.c
1348 if (new_srate < 8000)
1349 new_srate = 8000;
1350 if (new_srate > 192000)
1351 new_srate = 192000;
1352 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1355 result = init_audio_filters(sh_audio, new_srate,
1356 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1357 mpctx->mixer.afilter = sh_audio->afilter;
1358 #ifdef CONFIG_GUI
1359 if (use_gui) guiGetEvent(guiSetAfilter, (char *)sh_audio->afilter);
1360 #endif
1361 return result;
1365 typedef struct mp_osd_msg mp_osd_msg_t;
1366 struct mp_osd_msg {
1367 /// Previous message on the stack.
1368 mp_osd_msg_t* prev;
1369 /// Message text.
1370 char msg[128];
1371 int id,level,started;
1372 /// Display duration in ms.
1373 unsigned time;
1376 /// OSD message stack.
1377 static mp_osd_msg_t* osd_msg_stack = NULL;
1380 * \brief Add a message on the OSD message stack
1382 * If a message with the same id is already present in the stack
1383 * it is pulled on top of the stack, otherwise a new message is created.
1387 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1388 mp_osd_msg_t *msg,*last=NULL;
1389 va_list va;
1390 int r;
1392 // look if the id is already in the stack
1393 for(msg = osd_msg_stack ; msg && msg->id != id ;
1394 last = msg, msg = msg->prev);
1395 // not found: alloc it
1396 if(!msg) {
1397 msg = calloc(1,sizeof(mp_osd_msg_t));
1398 msg->prev = osd_msg_stack;
1399 osd_msg_stack = msg;
1400 } else if(last) { // found, but it's not on top of the stack
1401 last->prev = msg->prev;
1402 msg->prev = osd_msg_stack;
1403 osd_msg_stack = msg;
1405 // write the msg
1406 va_start(va,fmt);
1407 r = vsnprintf(msg->msg, 128, fmt, va);
1408 va_end(va);
1409 if(r >= 128) msg->msg[127] = 0;
1410 // set id and time
1411 msg->id = id;
1412 msg->level = level;
1413 msg->time = time;
1418 * \brief Remove a message from the OSD stack
1420 * This function can be used to get rid of a message right away.
1424 void rm_osd_msg(int id) {
1425 mp_osd_msg_t *msg,*last=NULL;
1427 // Search for the msg
1428 for(msg = osd_msg_stack ; msg && msg->id != id ;
1429 last = msg, msg = msg->prev);
1430 if(!msg) return;
1432 // Detach it from the stack and free it
1433 if(last)
1434 last->prev = msg->prev;
1435 else
1436 osd_msg_stack = msg->prev;
1437 free(msg);
1441 * \brief Remove all messages from the OSD stack
1445 static void clear_osd_msgs(void) {
1446 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1447 while(msg) {
1448 prev = msg->prev;
1449 free(msg);
1450 msg = prev;
1452 osd_msg_stack = NULL;
1456 * \brief Get the current message from the OSD stack.
1458 * This function decrements the message timer and destroys the old ones.
1459 * The message that should be displayed is returned (if any).
1463 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1465 struct MPOpts *opts = &mpctx->opts;
1466 mp_osd_msg_t *msg,*prev,*last = NULL;
1467 static unsigned last_update = 0;
1468 unsigned now = GetTimerMS();
1469 unsigned diff;
1470 char hidden_dec_done = 0;
1472 if (mpctx->osd_visible) {
1473 // 36000000 means max timed visibility is 1 hour into the future, if
1474 // the difference is greater assume it's wrapped around from below 0
1475 if (mpctx->osd_visible - now > 36000000) {
1476 mpctx->osd_visible = 0;
1477 vo_osd_progbar_type = -1; // disable
1478 vo_osd_changed(OSDTYPE_PROGBAR);
1479 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1482 if (mpctx->osd_show_percentage_until - now > 36000000)
1483 mpctx->osd_show_percentage_until = 0;
1485 if(!last_update) last_update = now;
1486 diff = now >= last_update ? now - last_update : 0;
1488 last_update = now;
1490 // Look for the first message in the stack with high enough level.
1491 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1492 prev = msg->prev;
1493 if (msg->level > opts->osd_level && hidden_dec_done)
1494 continue;
1495 // The message has a high enough level or it is the first hidden one
1496 // in both cases we decrement the timer or kill it.
1497 if(!msg->started || msg->time > diff) {
1498 if(msg->started) msg->time -= diff;
1499 else msg->started = 1;
1500 // display it
1501 if (msg->level <= opts->osd_level)
1502 return msg;
1503 hidden_dec_done = 1;
1504 continue;
1506 // kill the message
1507 free(msg);
1508 if(last) {
1509 last->prev = prev;
1510 msg = last;
1511 } else {
1512 osd_msg_stack = prev;
1513 msg = NULL;
1516 // Nothing found
1517 return NULL;
1521 * \brief Display the OSD bar.
1523 * Display the OSD bar or fall back on a simple message.
1527 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1528 struct MPOpts *opts = &mpctx->opts;
1529 if (opts->osd_level < 1)
1530 return;
1532 if(mpctx->sh_video) {
1533 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1534 vo_osd_progbar_type = type;
1535 vo_osd_progbar_value = 256*(val-min)/(max-min);
1536 vo_osd_changed(OSDTYPE_PROGBAR);
1537 return;
1540 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1541 name, ROUND(100*(val-min)/(max-min)));
1546 * \brief Update the OSD message line.
1548 * This function displays the current message on the vo OSD or on the term.
1549 * If the stack is empty and the OSD level is high enough the timer
1550 * is displayed (only on the vo OSD).
1554 static void update_osd_msg(struct MPContext *mpctx)
1556 struct MPOpts *opts = &mpctx->opts;
1557 mp_osd_msg_t *msg;
1558 struct osd_state *osd = mpctx->osd;
1559 char osd_text_timer[128];
1561 if (mpctx->add_osd_seek_info) {
1562 double percentage;
1563 if (mpctx->timeline && mpctx->sh_video)
1564 percentage = mpctx->sh_video->pts * 100 /
1565 mpctx->timeline[mpctx->num_timeline_parts].start;
1566 else
1567 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1568 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1569 if (mpctx->sh_video)
1570 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1571 mpctx->add_osd_seek_info = false;
1574 // Look if we have a msg
1575 if((msg = get_osd_msg(mpctx))) {
1576 if (strcmp(osd->osd_text, msg->msg)) {
1577 strncpy(osd->osd_text, msg->msg, 127);
1578 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1579 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1581 return;
1584 if(mpctx->sh_video) {
1585 // fallback on the timer
1586 if (opts->osd_level >= 2) {
1587 int len;
1588 if (mpctx->timeline)
1589 len = mpctx->timeline[mpctx->num_timeline_parts].start;
1590 else
1591 len = demuxer_get_time_length(mpctx->demuxer);
1592 int percentage = -1;
1593 char percentage_text[10];
1594 int pts = demuxer_get_current_time(mpctx->demuxer);
1596 if (mpctx->osd_show_percentage_until)
1597 if (mpctx->timeline)
1598 percentage = mpctx->sh_video->pts * 100 /
1599 mpctx->timeline[mpctx->num_timeline_parts].start;
1600 else
1601 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1603 if (percentage >= 0)
1604 snprintf(percentage_text, 9, " (%d%%)", percentage);
1605 else
1606 percentage_text[0] = 0;
1608 if (opts->osd_level == 3)
1609 snprintf(osd_text_timer, 63,
1610 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1611 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1612 len/3600,(len/60)%60,len%60,percentage_text);
1613 else
1614 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1615 mpctx->osd_function,pts/3600,(pts/60)%60,
1616 pts%60,percentage_text);
1617 } else
1618 osd_text_timer[0]=0;
1620 if (strcmp(osd->osd_text, osd_text_timer)) {
1621 strncpy(osd->osd_text, osd_text_timer, 63);
1622 vo_osd_changed(OSDTYPE_OSD);
1624 return;
1627 // Clear the term osd line
1628 if (term_osd && osd->osd_text[0]) {
1629 osd->osd_text[0] = 0;
1630 printf("%s\n",term_osd_esc);
1634 ///@}
1635 // OSDMsgStack
1638 void reinit_audio_chain(struct MPContext *mpctx)
1640 struct MPOpts *opts = &mpctx->opts;
1641 if(mpctx->sh_audio){
1642 current_module="init_audio_codec";
1643 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1644 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1645 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1646 mpctx->d_audio->id = -2;
1647 return;
1648 } else
1649 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1650 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1653 //const ao_info_t *info=audio_out->info;
1654 current_module="af_preinit";
1655 ao_data.samplerate=force_srate;
1656 ao_data.channels=0;
1657 ao_data.format=audio_output_format;
1658 #if 1
1659 // first init to detect best values
1660 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1661 // input:
1662 mpctx->sh_audio->samplerate,
1663 // output:
1664 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1665 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Error at audio filter chain pre-init!\n");
1666 exit_player(mpctx, EXIT_ERROR);
1668 #endif
1669 current_module="ao2_init";
1670 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1671 0, // plugin flag
1672 ao_data.samplerate,
1673 ao_data.channels,
1674 ao_data.format,0))){
1675 // FAILED:
1676 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Could not open/initialize audio device -> no sound.\n");
1677 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1678 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1679 mpctx->d_audio->id = -2;
1680 return;
1681 } else {
1682 // SUCCESS:
1683 mpctx->initialized_flags|=INITIALIZED_AO;
1684 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1685 mpctx->audio_out->info->short_name,
1686 ao_data.samplerate, ao_data.channels,
1687 af_fmt2str_short(ao_data.format),
1688 af_fmt2bits(ao_data.format)/8 );
1689 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1690 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1691 if(strlen(mpctx->audio_out->info->comment) > 0)
1692 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1693 // init audio filters:
1694 #if 1
1695 current_module="af_init";
1696 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1697 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter/ao format!\n");
1698 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1699 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1700 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1702 #endif
1704 mpctx->mixer.audio_out = mpctx->audio_out;
1705 mpctx->mixer.volstep = volstep;
1710 ///@}
1711 // Command2Property
1714 // Return pts value corresponding to the end point of audio written to the
1715 // ao so far.
1716 static double written_audio_pts(struct MPContext *mpctx)
1718 sh_audio_t *sh_audio = mpctx->sh_audio;
1719 demux_stream_t *d_audio = mpctx->d_audio;
1720 double buffered_output;
1721 // first calculate the end pts of audio that has been output by decoder
1722 double a_pts = sh_audio->pts;
1723 if (a_pts != MP_NOPTS_VALUE)
1724 // Good, decoder supports new way of calculating audio pts.
1725 // sh_audio->pts is the timestamp of the latest input packet with
1726 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1727 // the amount of bytes the decoder has written after that timestamp.
1728 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1729 else {
1730 // Decoder doesn't support new way of calculating pts (or we're
1731 // being called before it has decoded anything with known timestamp).
1732 // Use the old method of audio pts calculation: take the timestamp
1733 // of last packet with known pts the decoder has read data from,
1734 // and add amount of bytes read after the beginning of that packet
1735 // divided by input bps. This will be inaccurate if the input/output
1736 // ratio is not constant for every audio packet or if it is constant
1737 // but not accurately known in sh_audio->i_bps.
1739 a_pts = d_audio->pts;
1740 // ds_tell_pts returns bytes read after last timestamp from
1741 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1742 // it has read but not decoded
1743 if (sh_audio->i_bps)
1744 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1745 (double)sh_audio->i_bps;
1747 // Now a_pts hopefully holds the pts for end of audio from decoder.
1748 // Substract data in buffers between decoder and audio out.
1750 // Decoded but not filtered
1751 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1753 // Data buffered in audio filters, measured in bytes of "missing" output
1754 buffered_output = af_calc_delay(sh_audio->afilter);
1756 // Data that was ready for ao but was buffered because ao didn't fully
1757 // accept everything to internal buffers yet
1758 buffered_output += sh_audio->a_out_buffer_len;
1760 // Filters divide audio length by playback_speed, so multiply by it
1761 // to get the length in original units without speedup or slowdown
1762 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1764 return a_pts + mpctx->video_offset;
1767 // Return pts value corresponding to currently playing audio.
1768 double playing_audio_pts(struct MPContext *mpctx)
1770 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1771 mpctx->audio_out->get_delay();
1774 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1775 struct MPOpts *opts = &mpctx->opts;
1776 // check for frame-drop:
1777 current_module = "check_framedrop";
1778 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1779 static int dropped_frames;
1780 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1781 float d = delay-mpctx->delay;
1782 ++total_frame_cnt;
1783 // we should avoid dropping too many frames in sequence unless we
1784 // are too late. and we allow 100ms A-V delay here:
1785 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1786 && !mpctx->update_video_immediately) {
1787 ++drop_frame_cnt;
1788 ++dropped_frames;
1789 return frame_dropping;
1790 } else
1791 dropped_frames = 0;
1793 return 0;
1797 #ifdef HAVE_RTC
1798 int rtc_fd = -1;
1799 #endif
1801 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1803 #ifdef HAVE_RTC
1804 if (rtc_fd >= 0){
1805 // -------- RTC -----------
1806 current_module="sleep_rtc";
1807 while (time_frame > 0.000) {
1808 unsigned long rtc_ts;
1809 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1810 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC read error: %s\n", strerror(errno));
1811 time_frame -= get_relative_time(mpctx);
1813 } else
1814 #endif
1816 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1817 // unnecessarily high CPU usage
1818 float margin = softsleep ? 0.011 : 0;
1819 current_module = "sleep_timer";
1820 while (time_frame > margin) {
1821 usec_sleep(1000000 * (time_frame - margin));
1822 time_frame -= get_relative_time(mpctx);
1824 if (softsleep){
1825 current_module = "sleep_soft";
1826 if (time_frame < 0)
1827 mp_tmsg(MSGT_AVSYNC, MSGL_WARN, "Warning! Softsleep underflow!\n");
1828 while (time_frame > 0)
1829 time_frame -= get_relative_time(mpctx); // burn the CPU
1832 return time_frame;
1835 #ifdef CONFIG_DVDNAV
1836 #ifndef FF_B_TYPE
1837 #define FF_B_TYPE 3
1838 #endif
1839 /// store decoded video image
1840 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1841 mp_image_t *from_mpi) {
1842 mp_image_t *mpi;
1844 /// Do not store B-frames
1845 if (from_mpi->pict_type == FF_B_TYPE)
1846 return to_mpi;
1848 if (to_mpi &&
1849 to_mpi->w == from_mpi->w &&
1850 to_mpi->h == from_mpi->h &&
1851 to_mpi->imgfmt == from_mpi->imgfmt)
1852 mpi = to_mpi;
1853 else {
1854 if (to_mpi)
1855 free_mp_image(to_mpi);
1856 if (from_mpi->w == 0 || from_mpi->h == 0)
1857 return NULL;
1858 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1861 copy_mpi(mpi,from_mpi);
1862 return mpi;
1865 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1866 struct MPOpts *opts = &ctx->opts;
1867 if (ctx->sh_video) {
1868 /// clear video pts
1869 ctx->d_video->pts = 0.0f;
1870 ctx->sh_video->pts = 0.0f;
1871 ctx->sh_video->i_pts = 0.0f;
1872 ctx->sh_video->last_pts = 0.0f;
1873 ctx->sh_video->num_buffered_pts = 0;
1874 ctx->sh_video->num_frames = 0;
1875 ctx->sh_video->num_frames_decoded = 0;
1876 ctx->sh_video->timer = 0.0f;
1877 ctx->sh_video->stream_delay = 0.0f;
1878 ctx->sh_video->timer = 0;
1879 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1882 if (ctx->sh_audio) {
1883 /// free audio packets and reset
1884 ds_free_packs(ctx->d_audio);
1885 audio_delay -= ctx->sh_audio->stream_delay;
1886 ctx->delay =- audio_delay;
1887 ctx->audio_out->reset();
1888 resync_audio_stream(ctx->sh_audio);
1891 if (ctx->d_sub) opts->sub_id = -2;
1893 audio_delay = 0.0f;
1895 /// clear all EOF related flags
1896 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1899 /// Restore last decoded DVDNAV (still frame)
1900 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1901 int *in_size,
1902 unsigned char **start,
1903 mp_image_t *decoded_frame)
1905 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1906 return decoded_frame;
1908 /// a change occured in dvdnav stream
1909 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1910 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1911 mp_dvdnav_context_free(mpctx);
1912 mp_dvdnav_reset_stream(mpctx);
1913 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1914 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1917 if (*in_size < 0) {
1918 float len;
1920 /// Display still frame, if any
1921 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1922 decoded_frame = mpctx->nav_smpi;
1924 /// increment video frame : continue playing after still frame
1925 len = demuxer_get_time_length(mpctx->demuxer);
1926 if (mpctx->sh_video->pts >= len &&
1927 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1928 mp_dvdnav_skip_still(mpctx->stream);
1929 mp_dvdnav_skip_wait(mpctx->stream);
1931 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1933 if (mpctx->nav_buffer) {
1934 *start = mpctx->nav_start;
1935 *in_size = mpctx->nav_in_size;
1936 if (mpctx->nav_start)
1937 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1941 return decoded_frame;
1944 /// Save last decoded DVDNAV (still frame)
1945 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1946 unsigned char *start,
1947 mp_image_t *decoded_frame)
1949 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1950 return;
1952 if (mpctx->nav_buffer)
1953 free(mpctx->nav_buffer);
1955 mpctx->nav_buffer = malloc(in_size);
1956 mpctx->nav_start = start;
1957 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1958 if (mpctx->nav_buffer)
1959 memcpy(mpctx->nav_buffer,start,in_size);
1961 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1962 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1964 #endif /* CONFIG_DVDNAV */
1966 /* Modify video timing to match the audio timeline. There are two main
1967 * reasons this is needed. First, video and audio can start from different
1968 * positions at beginning of file or after a seek (MPlayer starts both
1969 * immediately even if they have different pts). Second, the file can have
1970 * audio timestamps that are inconsistent with the duration of the audio
1971 * packets, for example two consecutive timestamp values differing by
1972 * one second but only a packet with enough samples for half a second
1973 * of playback between them.
1975 static void adjust_sync(struct MPContext *mpctx, double frame_time)
1977 struct MPOpts *opts = &mpctx->opts;
1978 current_module = "av_sync";
1980 if (!mpctx->sh_audio)
1981 return;
1983 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
1984 double v_pts = mpctx->sh_video->pts;
1985 double av_delay = a_pts - v_pts;
1986 // Try to sync vo_flip() so it will *finish* at given time
1987 av_delay += mpctx->last_vo_flip_duration;
1988 av_delay -= audio_delay; // This much pts difference is desired
1990 double change = av_delay * 0.1;
1991 double max_change = default_max_pts_correction >= 0 ?
1992 default_max_pts_correction : frame_time * 0.1;
1993 if (change < -max_change)
1994 change = -max_change;
1995 else if (change > max_change)
1996 change = max_change;
1997 mpctx->delay += change;
1998 mpctx->total_avsync_change += change;
2001 static int fill_audio_out_buffers(struct MPContext *mpctx)
2003 struct MPOpts *opts = &mpctx->opts;
2004 unsigned int t;
2005 double tt;
2006 int playsize;
2007 int playflags=0;
2008 int audio_eof=0;
2009 int bytes_to_write;
2010 sh_audio_t * const sh_audio = mpctx->sh_audio;
2012 current_module="play_audio";
2014 while (1) {
2015 int sleep_time;
2016 // all the current uses of ao_data.pts seem to be in aos that handle
2017 // sync completely wrong; there should be no need to use ao_data.pts
2018 // in get_space()
2019 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2020 bytes_to_write = mpctx->audio_out->get_space();
2021 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
2022 break;
2024 // handle audio-only case:
2025 // this is where mplayer sleeps during audio-only playback
2026 // to avoid 100% CPU use
2027 sleep_time = (ao_data.outburst - bytes_to_write) * 1000 / ao_data.bps;
2028 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
2029 usec_sleep(sleep_time * 1000);
2032 while (bytes_to_write) {
2033 playsize = bytes_to_write;
2034 if (playsize > MAX_OUTBURST)
2035 playsize = MAX_OUTBURST;
2036 bytes_to_write -= playsize;
2038 // Fill buffer if needed:
2039 current_module="decode_audio";
2040 t = GetTimer();
2041 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2042 if (mpctx->d_audio->eof) {
2043 audio_eof = 1;
2044 if (sh_audio->a_out_buffer_len == 0)
2045 return 0;
2047 t = GetTimer() - t;
2048 tt = t*0.000001f; audio_time_usage+=tt;
2049 if (playsize > sh_audio->a_out_buffer_len) {
2050 playsize = sh_audio->a_out_buffer_len;
2051 if (audio_eof)
2052 playflags |= AOPLAY_FINAL_CHUNK;
2054 if (!playsize)
2055 break;
2057 // play audio:
2058 current_module="play_audio";
2060 // Is this pts value actually useful for the aos that access it?
2061 // They're obviously badly broken in the way they handle av sync;
2062 // would not having access to this make them more broken?
2063 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2064 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2066 if (playsize > 0) {
2067 sh_audio->a_out_buffer_len -= playsize;
2068 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2069 sh_audio->a_out_buffer_len);
2070 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2072 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2073 // Sanity check to avoid hanging in case current ao doesn't output
2074 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2075 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2076 sh_audio->a_out_buffer_len = 0;
2079 return 1;
2082 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2083 float *aq_sleep_time)
2085 struct MPOpts *opts = &mpctx->opts;
2086 int frame_time_remaining = 0;
2087 current_module="calc_sleep_time";
2089 *time_frame -= get_relative_time(mpctx); // reset timer
2091 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2092 float delay = mpctx->audio_out->get_delay();
2093 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2095 if (autosync) {
2097 * Adjust this raw delay value by calculating the expected
2098 * delay for this frame and generating a new value which is
2099 * weighted between the two. The higher autosync is, the
2100 * closer to the delay value gets to that which "-nosound"
2101 * would have used, and the longer it will take for A/V
2102 * sync to settle at the right value (but it eventually will.)
2103 * This settling time is very short for values below 100.
2105 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2106 float difference = delay - predicted;
2107 delay = predicted + difference / (float)autosync;
2110 *time_frame = delay - mpctx->delay / opts->playback_speed;
2112 // delay = amount of audio buffered in soundcard/driver
2113 if (delay > 0.25) delay=0.25; else
2114 if (delay < 0.10) delay=0.10;
2115 if (*time_frame > delay*0.6) {
2116 // sleep time too big - may cause audio drops (buffer underrun)
2117 frame_time_remaining = 1;
2118 *time_frame = delay*0.5;
2120 } else {
2121 // If we're lagging more than 200 ms behind the right playback rate,
2122 // don't try to "catch up".
2123 // If benchmark is set always output frames as fast as possible
2124 // without sleeping.
2125 if (*time_frame < -0.2 || benchmark)
2126 *time_frame = 0;
2129 *aq_sleep_time += *time_frame;
2132 //============================== SLEEP: ===================================
2134 // flag 256 means: libvo driver does its timing (dvb card)
2135 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2136 *time_frame = timing_sleep(mpctx, *time_frame);
2137 return frame_time_remaining;
2140 int reinit_video_chain(struct MPContext *mpctx)
2142 struct MPOpts *opts = &mpctx->opts;
2143 sh_video_t * const sh_video = mpctx->sh_video;
2144 double ar=-1.0;
2145 //================== Init VIDEO (codec & libvo) ==========================
2146 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2147 current_module="preinit_libvo";
2149 //shouldn't we set dvideo->id=-2 when we fail?
2150 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2151 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2152 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Error opening/initializing the selected video_out (-vo) device.\n");
2153 goto err_out;
2155 mpctx->initialized_flags|=INITIALIZED_VO;
2158 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2159 mpctx->sh_video->stream_aspect = ar;
2160 current_module="init_video_filters";
2162 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2163 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2165 #ifdef CONFIG_MENU
2166 if(use_menu) {
2167 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2168 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2169 if(!vf_menu) {
2170 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Can't open libmenu video filter with root menu %s.\n",menu_root);
2171 use_menu = 0;
2174 if(vf_menu)
2175 sh_video->vfilter = vf_menu;
2176 #endif
2178 #ifdef CONFIG_ASS
2179 if(ass_enabled) {
2180 int i;
2181 int insert = 1;
2182 if (opts->vf_settings)
2183 for (i = 0; opts->vf_settings[i].name; ++i)
2184 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2185 insert = 0;
2186 break;
2188 if (insert) {
2189 extern vf_info_t vf_info_ass;
2190 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2191 char* vf_arg[] = {"auto", "1", NULL};
2192 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2193 if (vf_ass)
2194 sh_video->vfilter = vf_ass;
2195 else
2196 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2199 #endif
2201 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2203 #ifdef CONFIG_ASS
2204 if (ass_enabled)
2205 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2206 #endif
2208 current_module="init_video_codec";
2210 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2211 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2212 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2214 if(!sh_video->initialized){
2215 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2216 goto err_out;
2219 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2221 if (sh_video->codec)
2222 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2224 sh_video->last_pts = MP_NOPTS_VALUE;
2225 sh_video->num_buffered_pts = 0;
2226 sh_video->next_frame_time = 0;
2228 if(auto_quality>0){
2229 // Auto quality option enabled
2230 output_quality=get_video_quality_max(sh_video);
2231 if(auto_quality>output_quality) auto_quality=output_quality;
2232 else output_quality=auto_quality;
2233 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2234 set_video_quality(sh_video,output_quality);
2237 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2239 current_module="init_vo";
2241 return 1;
2243 err_out:
2244 mpctx->sh_video = mpctx->d_video->sh = NULL;
2245 return 0;
2248 static double update_video_nocorrect_pts(struct MPContext *mpctx,
2249 int *blit_frame)
2251 struct sh_video *sh_video = mpctx->sh_video;
2252 *blit_frame = 0;
2253 double frame_time = 0;
2254 while (1) {
2255 current_module = "filter_video";
2256 // In nocorrect-pts mode there is no way to properly time these frames
2257 if (vf_output_queued_frame(sh_video->vfilter))
2258 break;
2259 unsigned char *packet = NULL;
2260 frame_time = sh_video->next_frame_time;
2261 if (mpctx->update_video_immediately)
2262 frame_time = 0;
2263 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2264 &packet, force_fps);
2265 if (in_size < 0) {
2266 #ifdef CONFIG_DVDNAV
2267 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2268 if (mp_dvdnav_is_eof(mpctx->stream))
2269 return -1;
2270 if (mpctx->d_video)
2271 mpctx->d_video->eof = 0;
2272 if (mpctx->d_audio)
2273 mpctx->d_audio->eof = 0;
2274 mpctx->stream->eof = 0;
2275 } else
2276 #endif
2277 return -1;
2279 if (in_size > max_framesize)
2280 max_framesize = in_size;
2281 sh_video->timer += frame_time;
2282 if (mpctx->sh_audio)
2283 mpctx->delay -= frame_time;
2284 // video_read_frame can change fps (e.g. for ASF video)
2285 vo_fps = sh_video->fps;
2286 int framedrop_type = check_framedrop(mpctx, frame_time);
2287 current_module = "decode video";
2289 void *decoded_frame;
2290 #ifdef CONFIG_DVDNAV
2291 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2292 if (in_size >= 0 && !decoded_frame)
2293 #endif
2294 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2295 sh_video->pts);
2296 #ifdef CONFIG_DVDNAV
2297 // Save last still frame for future display
2298 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2299 #endif
2300 if (decoded_frame) {
2301 // These updates are done here for vf_expand OSD/subtitles
2302 update_subtitles(sh_video, mpctx->d_sub, mpctx->video_offset, 0);
2303 update_teletext(sh_video, mpctx->demuxer, 0);
2304 update_osd_msg(mpctx);
2305 current_module = "filter video";
2306 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2307 break;
2310 *blit_frame = 1;
2311 return frame_time;
2314 static double update_video(struct MPContext *mpctx, int *blit_frame)
2316 struct sh_video *sh_video = mpctx->sh_video;
2317 *blit_frame = 0;
2318 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2319 mpctx->osd); // hack for vf_expand
2320 if (!mpctx->opts.correct_pts)
2321 return update_video_nocorrect_pts(mpctx, blit_frame);
2323 double pts;
2325 while (1) {
2326 current_module = "filter_video";
2327 // XXX Time used in this call is not counted in any performance
2328 // timer now, OSD time is not updated correctly for filter-added frames
2329 if (vf_output_queued_frame(sh_video->vfilter))
2330 break;
2331 unsigned char *packet = NULL;
2332 bool hit_eof = false;
2333 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2334 if (pts != MP_NOPTS_VALUE)
2335 pts += mpctx->video_offset;
2336 if (in_size < 0) {
2337 // try to extract last frames in case of decoder lag
2338 in_size = 0;
2339 pts = 1e300;
2340 hit_eof = true;
2342 if (in_size > max_framesize)
2343 max_framesize = in_size;
2344 current_module = "decode video";
2345 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2346 void *decoded_frame = decode_video(sh_video, packet, in_size,
2347 framedrop_type, pts);
2348 if (decoded_frame) {
2349 // These updates are done here for vf_expand OSD/subtitles
2350 update_subtitles(sh_video, mpctx->d_sub, mpctx->video_offset, 0);
2351 update_teletext(sh_video, mpctx->demuxer, 0);
2352 update_osd_msg(mpctx);
2353 current_module = "filter video";
2354 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2355 break;
2356 } else if (hit_eof)
2357 return -1;
2360 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_GET_PTS, &pts);
2361 if (pts == MP_NOPTS_VALUE) {
2362 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2363 // Try to use decoder pts from before filters
2364 pts = sh_video->pts;
2365 if (pts == MP_NOPTS_VALUE)
2366 pts = sh_video->last_pts;
2368 sh_video->pts = pts;
2369 if (sh_video->last_pts == MP_NOPTS_VALUE)
2370 sh_video->last_pts = sh_video->pts;
2371 else if (sh_video->last_pts > sh_video->pts) {
2372 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2373 sh_video->pts, sh_video->last_pts);
2374 /* If the difference in pts is small treat it as jitter around the
2375 * right value (possibly caused by incorrect timestamp ordering) and
2376 * just show this frame immediately after the last one.
2377 * Treat bigger differences as timestamp resets and start counting
2378 * timing of later frames from the position of this one. */
2379 if (sh_video->last_pts - sh_video->pts > 0.5)
2380 sh_video->last_pts = sh_video->pts;
2381 else
2382 sh_video->pts = sh_video->last_pts;
2384 double frame_time = sh_video->pts - sh_video->last_pts;
2385 sh_video->last_pts = sh_video->pts;
2386 sh_video->timer += frame_time;
2387 if (mpctx->sh_audio)
2388 mpctx->delay -= frame_time;
2389 *blit_frame = 1;
2390 return frame_time;
2393 void pause_player(struct MPContext *mpctx)
2395 if (mpctx->paused)
2396 return;
2397 mpctx->paused = 1;
2398 mpctx->step_frames = 0;
2399 mpctx->time_frame -= get_relative_time(mpctx);
2401 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2402 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2404 if (mpctx->audio_out && mpctx->sh_audio)
2405 mpctx->audio_out->pause(); // pause audio, keep data if possible
2408 void unpause_player(struct MPContext *mpctx)
2410 if (!mpctx->paused)
2411 return;
2412 mpctx->paused = 0;
2414 if (mpctx->audio_out && mpctx->sh_audio)
2415 mpctx->audio_out->resume(); // resume audio
2416 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2417 && !mpctx->step_frames)
2418 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2419 (void)get_relative_time(mpctx); // ignore time that passed during pause
2422 void add_step_frame(struct MPContext *mpctx)
2424 mpctx->step_frames++;
2425 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2426 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2427 unpause_player(mpctx);
2430 static void pause_loop(struct MPContext *mpctx)
2432 mp_cmd_t* cmd;
2433 if (!quiet) {
2434 // Small hack to display the pause message on the OSD line.
2435 // The pause string is: "\n == PAUSE == \r" so we need to
2436 // take the first and the last char out
2437 if (term_osd && !mpctx->sh_video) {
2438 char msg[128] = _("\n ===== PAUSE =====\r");
2439 int mlen = strlen(msg);
2440 msg[mlen-1] = '\0';
2441 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2442 update_osd_msg(mpctx);
2443 } else
2444 mp_tmsg(MSGT_CPLAYER,MSGL_STATUS,"\n ===== PAUSE =====\r");
2445 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2447 #ifdef CONFIG_GUI
2448 if (use_gui)
2449 guiGetEvent(guiCEvent, (char *)guiSetPause);
2450 #endif
2452 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2453 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2454 if (cmd) {
2455 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2456 run_command(mpctx, cmd);
2457 mp_cmd_free(cmd);
2458 continue;
2460 if (mpctx->sh_video && mpctx->video_out)
2461 vo_check_events(mpctx->video_out);
2462 #ifdef CONFIG_GUI
2463 if (use_gui) {
2464 guiEventHandling();
2465 guiGetEvent(guiReDraw, NULL);
2466 if (guiIntfStruct.Playing!=2 || (mpctx->rel_seek_secs || mpctx->abs_seek_pos))
2467 break;
2469 #endif
2470 #ifdef CONFIG_MENU
2471 if (vf_menu)
2472 vf_menu_pause_update(vf_menu);
2473 #endif
2474 usec_sleep(20000);
2475 update_osd_msg(mpctx);
2476 int hack = vo_osd_changed(0);
2477 vo_osd_changed(hack);
2478 if (hack)
2479 break;
2481 #ifdef CONFIG_GUI
2482 if (use_gui) {
2483 if (guiIntfStruct.Playing == guiSetStop)
2484 mpctx->eof = 1;
2485 else
2486 guiGetEvent(guiCEvent, (char *)guiSetPlay);
2488 #endif
2492 // Find the right mute status and record position for new file position
2493 static void edl_seek_reset(MPContext *mpctx)
2495 mpctx->edl_muted = 0;
2496 next_edl_record = edl_records;
2498 while (next_edl_record) {
2499 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2500 break;
2502 if (next_edl_record->action == EDL_MUTE)
2503 mpctx->edl_muted = !mpctx->edl_muted;
2504 next_edl_record = next_edl_record->next;
2506 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2507 mixer_mute(&mpctx->mixer);
2511 // Execute EDL command for the current position if one exists
2512 static void edl_update(MPContext *mpctx)
2514 if (!next_edl_record)
2515 return;
2517 if (!mpctx->sh_video) {
2518 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot use EDL without video, disabling.\n");
2519 free_edl(edl_records);
2520 next_edl_record = NULL;
2521 edl_records = NULL;
2522 return;
2525 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2526 if (next_edl_record->action == EDL_SKIP) {
2527 mpctx->osd_function = OSD_FFW;
2528 mpctx->abs_seek_pos = 0;
2529 mpctx->rel_seek_secs = next_edl_record->length_sec;
2530 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2531 "[%f], length [%f]\n", next_edl_record->start_sec,
2532 next_edl_record->stop_sec, next_edl_record->length_sec);
2534 else if (next_edl_record->action == EDL_MUTE) {
2535 mpctx->edl_muted = !mpctx->edl_muted;
2536 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2537 mixer_mute(&mpctx->mixer);
2538 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2539 next_edl_record->start_sec );
2541 next_edl_record = next_edl_record->next;
2545 static void reinit_decoders(struct MPContext *mpctx)
2547 reinit_video_chain(mpctx);
2548 reinit_audio_chain(mpctx);
2549 mp_property_do("sub", M_PROPERTY_SET, &mpctx->global_sub_pos, mpctx);
2552 static bool timeline_set_part(struct MPContext *mpctx, int i)
2554 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
2555 struct timeline_part *n = mpctx->timeline + i;
2556 mpctx->timeline_part = i;
2557 mpctx->video_offset = n->start - n->source_start;
2558 if (n->source == p->source)
2559 return false;
2560 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo && mpctx->opts.video_id != -2 ? 0 : INITIALIZED_VO) | INITIALIZED_AO | INITIALIZED_ACODEC);
2561 mpctx->demuxer = n->source->demuxer;
2562 mpctx->d_video = mpctx->demuxer->video;
2563 mpctx->d_audio = mpctx->demuxer->audio;
2564 mpctx->d_sub = mpctx->demuxer->sub;
2565 mpctx->sh_video = mpctx->d_video->sh;
2566 mpctx->sh_audio = mpctx->d_audio->sh;
2567 return true;
2570 // Given pts, switch playback to the corresponding part.
2571 // Return offset within that part.
2572 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
2573 bool *need_reset)
2575 if (pts < 0)
2576 pts = 0;
2577 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
2578 struct timeline_part *p = mpctx->timeline + i;
2579 if (pts < (p+1)->start) {
2580 *need_reset = timeline_set_part(mpctx, i);
2581 return pts - p->start + p->source_start;
2584 return -1;
2588 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2589 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2590 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2591 static int seek(MPContext *mpctx, double amount, int style)
2593 current_module = "seek";
2594 if (mpctx->stop_play == AT_END_OF_FILE)
2595 mpctx->stop_play = KEEP_PLAYING;
2596 if (mpctx->timeline && style & SEEK_FACTOR) {
2597 amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
2598 style &= ~SEEK_FACTOR;
2600 if ((mpctx->demuxer->accurate_seek || mpctx->timeline) && mpctx->sh_video
2601 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2602 style |= SEEK_ABSOLUTE;
2603 if (amount > 0)
2604 style |= SEEK_FORWARD;
2605 else
2606 style |= SEEK_BACKWARD;
2607 amount += mpctx->sh_video->pts;
2610 /* At least the liba52 decoder wants to read from the input stream
2611 * during initialization, so reinit must be done after the demux_seek()
2612 * call that clears possible stream EOF. */
2613 bool need_reset = false;
2614 if (mpctx->timeline) {
2615 amount = timeline_set_from_time(mpctx, amount, &need_reset);
2616 if (amount == -1) {
2617 mpctx->stop_play = AT_END_OF_FILE;
2618 // Clear audio from current position
2619 if (mpctx->sh_audio) {
2620 mpctx->audio_out->reset();
2621 mpctx->sh_audio->a_buffer_len = 0;
2622 mpctx->sh_audio->a_out_buffer_len = 0;
2624 return -1;
2627 int seekresult = demux_seek(mpctx->demuxer, amount, audio_delay, style);
2628 if (need_reset)
2629 reinit_decoders(mpctx);
2630 if (seekresult == 0)
2631 return -1;
2633 if (mpctx->sh_video) {
2634 current_module = "seek_video_reset";
2635 resync_video_stream(mpctx->sh_video);
2636 if (mpctx->video_out->config_ok)
2637 vo_control(mpctx->video_out, VOCTRL_RESET, NULL);
2638 mpctx->sh_video->num_buffered_pts = 0;
2639 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2640 mpctx->num_buffered_frames = 0;
2641 mpctx->delay = 0;
2642 mpctx->time_frame = 0;
2643 mpctx->update_video_immediately = true;
2644 // Not all demuxers set d_video->pts during seek, so this value
2645 // (which is used by at least vobsub and edl code below) may
2646 // be completely wrong (probably 0).
2647 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
2648 update_subtitles(mpctx->sh_video, mpctx->d_sub, mpctx->video_offset, 1);
2649 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2652 if (mpctx->sh_audio) {
2653 current_module = "seek_audio_reset";
2654 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2655 mpctx->sh_audio->a_buffer_len = 0;
2656 mpctx->sh_audio->a_out_buffer_len = 0;
2659 if (vo_vobsub && mpctx->sh_video) {
2660 current_module = "seek_vobsub_reset";
2661 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2664 edl_seek_reset(mpctx);
2666 mpctx->total_avsync_change = 0;
2667 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2668 drop_frame_cnt = 0;
2670 current_module = NULL;
2671 return 0;
2674 int get_current_chapter(struct MPContext *mpctx)
2676 if (!mpctx->chapters || !mpctx->sh_video)
2677 return demuxer_get_current_chapter(mpctx->demuxer);
2679 int i;
2680 double current_pts = mpctx->sh_video->pts;
2681 for (i = 1; i < mpctx->num_chapters; i++)
2682 if (current_pts < mpctx->chapters[i].start)
2683 break;
2684 return i - 1;
2687 // currently returns a string allocated with malloc, not talloc
2688 char *chapter_display_name(struct MPContext *mpctx, int chapter)
2690 if (!mpctx->chapters || !mpctx->sh_video)
2691 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
2692 return strdup(mpctx->chapters[chapter].name);
2695 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
2696 char **chapter_name)
2698 if (!mpctx->chapters || !mpctx->sh_video)
2699 return demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
2700 chapter_name);
2701 if (chapter >= mpctx->num_chapters)
2702 return -1;
2703 if (chapter < 0)
2704 chapter = 0;
2705 *seek_pts = mpctx->chapters[chapter].start;
2706 if (chapter_name)
2707 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
2708 return chapter;
2711 static int find_ordered_chapter_sources(struct MPContext *mpctx,
2712 struct content_source *sources,
2713 int num_sources,
2714 unsigned char uid_map[][16])
2716 int num_filenames = 0;
2717 char **filenames = NULL;
2718 if (num_sources > 1) {
2719 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
2720 "other sources.\n");
2721 if (mpctx->stream->type != STREAMTYPE_FILE) {
2722 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
2723 "normal disk file. Will not search for related files.\n");
2724 } else {
2725 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
2726 "same directory to find referenced sources.\n");
2727 filenames = find_files(mpctx->demuxer->filename, ".mkv",
2728 &num_filenames);
2732 int num_left = num_sources - 1;
2733 for (int i = 0; i < num_filenames && num_left > 0; i++) {
2734 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
2735 filename_recode(filenames[i]));
2736 int format;
2737 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
2738 if (!s)
2739 continue;
2740 struct demuxer *d = demux_open(&mpctx->opts, s, DEMUXER_TYPE_MATROSKA,
2741 mpctx->opts.audio_id,
2742 mpctx->opts.video_id,
2743 mpctx->opts.sub_id, filenames[i]);
2744 if (!d) {
2745 free_stream(s);
2746 continue;
2748 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
2749 for (int i = 1; i < num_sources; i++) {
2750 if (sources[i].demuxer)
2751 continue;
2752 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
2753 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
2754 i, filename_recode(d->filename));
2755 sources[i].stream = s;
2756 sources[i].demuxer = d;
2757 num_left--;
2758 goto match;
2762 free_demuxer(d);
2763 free_stream(s);
2764 continue;
2765 match:
2768 talloc_free(filenames);
2769 if (num_left) {
2770 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
2771 "There will be parts MISSING from the video!\n");
2772 for (int i = 1, j = 1; i < num_sources; i++)
2773 if (sources[i].demuxer) {
2774 sources[j] = sources[i];
2775 memcpy(uid_map[j], uid_map[i], 16);
2776 j++;
2779 return num_sources - num_left;
2782 static void build_ordered_chapter_timeline(struct MPContext *mpctx)
2784 if (!mpctx->opts.ordered_chapters) {
2785 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
2786 "you have disabled support for them. Ignoring.\n");
2787 return;
2790 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
2791 "edit timeline.\n");
2793 struct demuxer *demuxer = mpctx->demuxer;
2794 struct matroska_data *m = &demuxer->matroska_data;
2796 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
2797 m->num_ordered_chapters);
2798 sources[0].stream = mpctx->stream;
2799 sources[0].demuxer = mpctx->demuxer;
2800 unsigned char uid_map[m->num_ordered_chapters][16];
2801 int num_sources = 1;
2802 memcpy(uid_map[0], m->segment_uid, 16);
2804 for (int i = 0; i < m->num_ordered_chapters; i++) {
2805 struct matroska_chapter *c = m->ordered_chapters + i;
2806 if (!c->has_segment_uid)
2807 memcpy(c->segment_uid, m->segment_uid, 16);
2809 for (int j = 0; j < num_sources; j++)
2810 if (!memcmp(c->segment_uid, uid_map[j], 16))
2811 goto found1;
2812 memcpy(uid_map[num_sources], c->segment_uid, 16);
2813 sources[num_sources] = (struct content_source){};
2814 num_sources++;
2815 found1:
2819 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
2820 uid_map);
2823 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
2824 m->num_ordered_chapters + 1);
2825 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
2826 m->num_ordered_chapters);
2827 uint64_t starttime = 0;
2828 uint64_t missing_time = 0;
2829 int part_count = 0;
2830 int num_chapters = 0;
2831 uint64_t prev_part_offset;
2832 for (int i = 0; i < m->num_ordered_chapters; i++) {
2833 struct matroska_chapter *c = m->ordered_chapters + i;
2835 int j;
2836 for (j = 0; j < num_sources; j++) {
2837 if (!memcmp(c->segment_uid, uid_map[j], 16))
2838 goto found2;
2840 missing_time += c->end - c->start;
2841 continue;
2842 found2:;
2843 chapters[num_chapters].start = starttime / 1000.;
2844 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
2845 /* Only add a separate part if the time or file actually changes.
2846 * Matroska files have chapter divisions that are redundant from
2847 * timeline point of view because the same chapter structure is used
2848 * both to specify the timeline and for normal chapter information.
2849 * Removing a missing inserted external chapter can also cause this. */
2850 if (part_count == 0 || c->start != starttime + prev_part_offset
2851 || sources + j != timeline[part_count - 1].source) {
2852 timeline[part_count].source = sources + j;
2853 timeline[part_count].start = chapters[num_chapters].start;
2854 timeline[part_count].source_start = c->start / 1000.;
2855 prev_part_offset = c->start - starttime;
2856 part_count++;
2858 starttime += c->end - c->start;
2859 num_chapters++;
2861 timeline[part_count].start = starttime / 1000.;
2863 if (!part_count) {
2864 // None of the parts come from the file itself???
2865 talloc_free(sources);
2866 talloc_free(timeline);
2867 talloc_free(chapters);
2868 return;
2871 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
2872 "sources. Total length %.3f seconds.\n", part_count, num_sources,
2873 timeline[part_count].start);
2874 if (missing_time)
2875 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
2876 "from the timeline!\n", missing_time / 1000.);
2877 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
2878 for (int i = 0; i < num_sources; i++)
2879 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
2880 filename_recode(sources[i].demuxer->filename));
2881 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
2882 "source_start, source):\n");
2883 for (int i = 0; i < part_count; i++) {
2884 struct timeline_part *p = timeline + i;
2885 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
2886 p->source_start, p->source - sources);
2888 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start);
2889 mpctx->sources = sources;
2890 mpctx->num_sources = num_sources;
2891 mpctx->timeline = timeline;
2892 mpctx->num_timeline_parts = part_count;
2893 mpctx->num_chapters = num_chapters;
2894 mpctx->chapters = chapters;
2896 mpctx->timeline_part = 0;
2897 mpctx->video_offset = timeline[0].source_start;
2898 mpctx->demuxer = timeline[0].source->demuxer;
2902 static int read_keys(void *ctx, int fd)
2904 getch2(ctx);
2905 return mplayer_get_key(ctx, 0);
2909 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2910 * file for some tools to link against. */
2911 #ifndef DISABLE_MAIN
2912 int main(int argc,char* argv[]){
2915 char * mem_ptr;
2917 // movie info:
2919 /* Flag indicating whether MPlayer should exit without playing anything. */
2920 int opt_exit = 0;
2922 //float a_frame=0; // Audio
2924 int i;
2926 int gui_no_filename=0;
2928 struct MPContext *mpctx = &(struct MPContext){
2929 .osd_function = OSD_PLAY,
2930 .begin_skip = MP_NOPTS_VALUE,
2931 .play_tree_step = 1,
2932 .global_sub_pos = -1,
2933 .set_of_sub_pos = -1,
2934 .file_format = DEMUXER_TYPE_UNKNOWN,
2935 .last_dvb_step = 1,
2938 InitTimer();
2939 srand(GetTimerMS());
2941 mp_msg_init();
2943 #ifdef CONFIG_X11
2944 mpctx->x11_state = vo_x11_init_state();
2945 #endif
2946 struct MPOpts *opts = &mpctx->opts;
2947 set_default_mplayer_options(opts);
2948 // Create the config context and register the options
2949 mpctx->mconfig = m_config_new(opts, cfg_include);
2950 m_config_register_options(mpctx->mconfig,mplayer_opts);
2951 mp_input_register_options(mpctx->mconfig);
2953 // Preparse the command line
2954 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2956 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2957 set_path_env();
2958 #endif
2960 #ifdef CONFIG_TV
2961 stream_tv_defaults.immediate = 1;
2962 #endif
2964 if (argc > 1 && argv[1] &&
2965 (!strcmp(argv[1], "-gui") || !strcmp(argv[1], "-nogui"))) {
2966 use_gui = !strcmp(argv[1], "-gui");
2967 } else
2968 if ( argv[0] )
2970 char *base = strrchr(argv[0], '/');
2971 if (!base)
2972 base = strrchr(argv[0], '\\');
2973 if (!base)
2974 base = argv[0];
2975 if(strstr(base, "gmplayer"))
2976 use_gui=1;
2979 parse_cfgfiles(mpctx, mpctx->mconfig);
2981 #ifdef CONFIG_GUI
2982 if ( use_gui ) cfg_read();
2983 #endif
2985 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2986 if(mpctx->playtree == NULL)
2987 opt_exit = 1;
2988 else {
2989 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2990 if(mpctx->playtree) {
2991 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2992 if(mpctx->playtree_iter) {
2993 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2994 play_tree_iter_free(mpctx->playtree_iter);
2995 mpctx->playtree_iter = NULL;
2997 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3001 mpctx->key_fifo = mp_fifo_create(opts);
3003 print_version("MPlayer");
3005 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_GUI)
3006 void *runningmplayer = FindWindow("MPlayer GUI for Windows", "MPlayer for Windows");
3007 if(runningmplayer && mpctx->filename && use_gui){
3008 COPYDATASTRUCT csData;
3009 char file[MAX_PATH];
3010 char *filepart = mpctx->filename;
3011 if(GetFullPathName(mpctx->filename, MAX_PATH, file, &filepart)){
3012 csData.dwData = 0;
3013 csData.cbData = strlen(file)*2;
3014 csData.lpData = file;
3015 SendMessage(runningmplayer, WM_COPYDATA, (WPARAM)runningmplayer, (LPARAM)&csData);
3018 #endif
3020 #if defined(__MINGW32__) || defined(__CYGWIN__)
3021 // stop Windows from showing all kinds of annoying error dialogs
3022 SetErrorMode(0x8003);
3023 // request 1ms timer resolution
3024 timeBeginPeriod(1);
3025 #endif
3027 #ifdef CONFIG_PRIORITY
3028 set_priority();
3029 #endif
3031 #ifndef CONFIG_GUI
3032 if(use_gui){
3033 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"MPlayer was compiled WITHOUT GUI support.\n");
3034 use_gui=0;
3036 #else
3037 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
3038 if(use_gui && !vo_init()){
3039 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"MPlayer GUI requires X11.\n");
3040 use_gui=0;
3042 #endif
3043 if (use_gui && mpctx->playtree_iter){
3044 char cwd[PATH_MAX+2];
3045 // Free Playtree and Playtree-Iter as it's not used by the GUI.
3046 play_tree_iter_free(mpctx->playtree_iter);
3047 mpctx->playtree_iter=NULL;
3049 if (getcwd(cwd, PATH_MAX) != (char *)NULL)
3051 strcat(cwd, "/");
3052 // Prefix relative paths with current working directory
3053 play_tree_add_bpf(mpctx->playtree, cwd);
3055 // Import initital playtree into GUI.
3056 import_initial_playtree_into_gui(mpctx->playtree, mpctx->mconfig, enqueue);
3058 #endif /* CONFIG_GUI */
3060 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
3061 list_video_out();
3062 opt_exit = 1;
3065 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
3066 list_audio_out();
3067 opt_exit = 1;
3070 /* Check codecs.conf. */
3071 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3072 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3073 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3074 if(!parse_codec_cfg(NULL)){
3075 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3077 mp_tmsg(MSGT_CPLAYER,MSGL_V,"Using built-in default codecs.conf.\n");
3080 free( mem_ptr ); // release the buffer created by get_path()
3083 #if 0
3084 if(video_codec_list){
3085 int i;
3086 video_codec=video_codec_list[0];
3087 for(i=0;video_codec_list[i];i++)
3088 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
3090 #endif
3091 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3092 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available audio codecs:\n");
3093 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3094 list_codecs(1);
3095 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3096 opt_exit = 1;
3098 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3099 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video codecs:\n");
3100 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3101 list_codecs(0);
3102 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3103 opt_exit = 1;
3105 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3106 vfm_help();
3107 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3108 opt_exit = 1;
3110 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3111 afm_help();
3112 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3113 opt_exit = 1;
3115 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3116 af_help();
3117 printf("\n");
3118 opt_exit = 1;
3120 #ifdef CONFIG_X11
3121 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3122 fstype_help();
3123 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3124 opt_exit = 1;
3126 #endif
3127 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3128 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3129 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3130 demuxer_help();
3131 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3132 opt_exit = 1;
3134 if(list_properties) {
3135 property_print_help();
3136 opt_exit = 1;
3139 if(opt_exit)
3140 exit_player(mpctx, EXIT_NONE);
3142 if (player_idle_mode && use_gui) {
3143 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "The -idle option cannot be used with GMPlayer.\n");
3144 exit_player_with_rc(mpctx, EXIT_NONE, 1);
3147 if(!mpctx->filename && !player_idle_mode){
3148 if(!use_gui){
3149 // no file/vcd/dvd -> show HELP:
3150 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
3151 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3152 } else gui_no_filename=1;
3155 /* Display what configure line was used */
3156 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3158 // Many users forget to include command line in bugreports...
3159 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3160 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "CommandLine:");
3161 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3162 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3165 //------ load global data first ------
3167 mpctx->osd = osd_create();
3169 // check font
3170 #ifdef CONFIG_FREETYPE
3171 init_freetype();
3172 #endif
3173 #ifdef CONFIG_FONTCONFIG
3174 if(font_fontconfig <= 0)
3176 #endif
3177 #ifdef CONFIG_BITMAP_FONT
3178 if(font_name){
3179 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3180 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",
3181 filename_recode(font_name));
3182 } else {
3183 // try default:
3184 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3185 free(mem_ptr); // release the buffer created by get_path()
3186 if(!vo_font)
3187 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3189 if (sub_font_name)
3190 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3191 else
3192 mpctx->osd->sub_font = vo_font;
3193 #endif
3194 #ifdef CONFIG_FONTCONFIG
3196 #endif
3198 #ifdef CONFIG_ASS
3199 ass_library = ass_init();
3200 #endif
3202 #ifdef HAVE_RTC
3203 if(!nortc)
3205 // seteuid(0); /* Can't hurt to try to get root here */
3206 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3207 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Failed to open %s: %s (it should be readable by the user.)\n",
3208 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3209 else {
3210 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3212 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3213 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp, strerror(errno));
3214 mp_tmsg(MSGT_CPLAYER, MSGL_HINT, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp);
3215 close (rtc_fd);
3216 rtc_fd = -1;
3217 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3218 /* variable only by the root */
3219 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno));
3220 close (rtc_fd);
3221 rtc_fd = -1;
3222 } else
3223 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Using Linux hardware RTC timing (%ldHz).\n", irqp);
3226 #ifdef CONFIG_GUI
3227 // breaks DGA and SVGAlib and VESA drivers: --A'rpi
3228 // and now ? -- Pontscho
3229 if(use_gui) setuid( getuid() ); // strongly test, please check this.
3230 #endif
3231 if(rtc_fd<0)
3232 #endif /* HAVE_RTC */
3233 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3234 softsleep?"software":timer_name);
3236 #ifdef HAVE_TERMCAP
3237 if ( !use_gui ) load_termcap(NULL); // load key-codes
3238 #endif
3240 // ========== Init keyboard FIFO (connection to libvo) ============
3242 // Init input system
3243 current_module = "init_input";
3244 mpctx->input = mp_input_init(&opts->input, use_gui);
3245 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3246 if(slave_mode)
3247 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
3248 else if(!noconsolecontrols)
3249 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
3250 // Set the libstream interrupt callback
3251 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
3253 #ifdef CONFIG_MENU
3254 if(use_menu) {
3255 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3256 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3257 else {
3258 menu_cfg = get_path("menu.conf");
3259 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3260 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3261 else {
3262 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
3263 MPLAYER_CONFDIR "/menu.conf"))
3264 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", MPLAYER_CONFDIR"/menu.conf");
3265 else {
3266 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Menu init failed.\n");
3267 use_menu = 0;
3272 #endif
3274 current_module = NULL;
3276 /// Catch signals
3277 #ifndef __MINGW32__
3278 signal(SIGCHLD,child_sighandler);
3279 #endif
3281 #ifdef CONFIG_CRASH_DEBUG
3282 prog_path = argv[0];
3283 #endif
3284 //========= Catch terminate signals: ================
3285 // terminate requests:
3286 signal(SIGTERM,exit_sighandler); // kill
3287 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3289 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3291 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3292 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3293 #ifdef CONFIG_SIGHANDLER
3294 // fatal errors:
3295 signal(SIGBUS,exit_sighandler); // bus error
3296 signal(SIGSEGV,exit_sighandler); // segfault
3297 signal(SIGILL,exit_sighandler); // illegal instruction
3298 signal(SIGFPE,exit_sighandler); // floating point exc.
3299 signal(SIGABRT,exit_sighandler); // abort()
3300 #ifdef CONFIG_CRASH_DEBUG
3301 if (crash_debug)
3302 signal(SIGTRAP,exit_sighandler);
3303 #endif
3304 #endif
3306 #ifdef CONFIG_GUI
3307 if(use_gui){
3308 guiInit();
3309 guiGetEvent(guiSetContext, mpctx);
3310 mpctx->initialized_flags|=INITIALIZED_GUI;
3311 guiGetEvent( guiCEvent,(char *)((gui_no_filename) ? 0 : 1) );
3313 #endif
3315 // ******************* Now, let's see the per-file stuff ********************
3317 play_next_file:
3319 // init global sub numbers
3320 mpctx->global_sub_size = 0;
3321 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3323 if (mpctx->filename) {
3324 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
3325 load_per_extension_config (mpctx->mconfig, mpctx->filename);
3326 load_per_file_config (mpctx->mconfig, mpctx->filename);
3329 if (opts->video_driver_list)
3330 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
3331 if (opts->audio_driver_list)
3332 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
3334 // We must enable getch2 here to be able to interrupt network connection
3335 // or cache filling
3336 if(!noconsolecontrols && !slave_mode){
3337 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
3338 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"WARNING: getch2_init called twice!\n");
3339 else
3340 getch2_enable(); // prepare stdin for hotkeys...
3341 mpctx->initialized_flags|=INITIALIZED_GETCH2;
3342 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3345 // =================== GUI idle loop (STOP state) ===========================
3346 #ifdef CONFIG_GUI
3347 if ( use_gui ) {
3348 mpctx->file_format=DEMUXER_TYPE_UNKNOWN;
3349 guiGetEvent( guiSetDefaults,0 );
3350 while ( guiIntfStruct.Playing != 1 )
3352 mp_cmd_t* cmd;
3353 usec_sleep(20000);
3354 guiEventHandling();
3355 guiGetEvent( guiReDraw,NULL );
3356 if ( (cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
3357 guiGetEvent(guiIEvent, (char *)cmd->id);
3358 mp_cmd_free(cmd);
3361 guiGetEvent( guiSetParameters,NULL );
3362 if ( guiIntfStruct.StreamType == STREAMTYPE_STREAM )
3364 play_tree_t * entry = play_tree_new();
3365 play_tree_add_file( entry,guiIntfStruct.Filename );
3366 if ( mpctx->playtree ) play_tree_free_list( mpctx->playtree->child,1 );
3367 else mpctx->playtree=play_tree_new();
3368 play_tree_set_child( mpctx->playtree,entry );
3369 if(mpctx->playtree)
3371 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3372 if(mpctx->playtree_iter)
3374 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY)
3376 play_tree_iter_free(mpctx->playtree_iter);
3377 mpctx->playtree_iter = NULL;
3379 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3384 #endif /* CONFIG_GUI */
3386 while (player_idle_mode && !mpctx->filename) {
3387 play_tree_t * entry = NULL;
3388 mp_cmd_t * cmd;
3389 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3390 if (mpctx->video_out)
3391 vo_check_events(mpctx->video_out);
3392 usec_sleep(20000);
3394 switch (cmd->id) {
3395 case MP_CMD_LOADFILE:
3396 // prepare a tree entry with the new filename
3397 entry = play_tree_new();
3398 play_tree_add_file(entry, cmd->args[0].v.s);
3399 // The entry is added to the main playtree after the switch().
3400 break;
3401 case MP_CMD_LOADLIST:
3402 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3403 break;
3404 case MP_CMD_QUIT:
3405 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3406 break;
3407 case MP_CMD_GET_PROPERTY:
3408 case MP_CMD_SET_PROPERTY:
3409 case MP_CMD_STEP_PROPERTY:
3410 run_command(mpctx, cmd);
3411 break;
3414 mp_cmd_free(cmd);
3416 if (entry) { // user entered a command that gave a valid entry
3417 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3418 play_tree_free_list(mpctx->playtree->child, 1);
3419 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3421 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3423 play_tree_set_child(mpctx->playtree, entry);
3425 /* Make iterator start at the top the of tree. */
3426 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3427 if (!mpctx->playtree_iter) continue;
3429 // find the first real item in the tree
3430 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3431 // no items!
3432 play_tree_iter_free(mpctx->playtree_iter);
3433 mpctx->playtree_iter = NULL;
3434 continue; // wait for next command
3436 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3439 //---------------------------------------------------------------------------
3441 if(mpctx->filename)
3442 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nPlaying %s.\n",
3443 filename_recode(mpctx->filename));
3445 if (edl_filename) {
3446 if (edl_records) free_edl(edl_records);
3447 next_edl_record = edl_records = edl_parse_file();
3449 if (edl_output_filename) {
3450 if (edl_fd) fclose(edl_fd);
3451 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3453 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Can't open EDL file [%s] for writing.\n",
3454 filename_recode(edl_output_filename));
3458 //==================== Open VOB-Sub ============================
3460 current_module="vobsub";
3461 if (vobsub_name){
3462 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3463 if(vo_vobsub==NULL)
3464 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
3465 filename_recode(vobsub_name));
3466 } else if (sub_auto && mpctx->filename){
3467 /* try to autodetect vobsub from movie filename ::atmos */
3468 char *buf = strdup(mpctx->filename), *psub;
3469 char *pdot = strrchr(buf, '.');
3470 char *pslash = strrchr(buf, '/');
3471 #if defined(__MINGW32__) || defined(__CYGWIN__)
3472 if (!pslash) pslash = strrchr(buf, '\\');
3473 #endif
3474 if (pdot && (!pslash || pdot > pslash))
3475 *pdot = '\0';
3476 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3477 /* try from ~/.mplayer/sub */
3478 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3479 char *bname;
3480 int l;
3481 bname = strrchr(buf,'/');
3482 #if defined(__MINGW32__) || defined(__CYGWIN__)
3483 if(!bname) bname = strrchr(buf,'\\');
3484 #endif
3485 if(bname) bname++;
3486 else bname = buf;
3487 l = strlen(psub) + strlen(bname) + 1;
3488 psub = realloc(psub,l);
3489 strcat(psub,bname);
3490 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3491 free(psub);
3493 free(buf);
3495 if(vo_vobsub){
3496 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3497 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3498 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3500 // setup global sub numbering
3501 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3502 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3505 //============ Open & Sync STREAM --- fork cache2 ====================
3507 mpctx->stream=NULL;
3508 mpctx->demuxer=NULL;
3509 if (mpctx->d_audio) {
3510 //free_demuxer_stream(mpctx->d_audio);
3511 mpctx->d_audio=NULL;
3513 if (mpctx->d_video) {
3514 //free_demuxer_stream(d_video);
3515 mpctx->d_video=NULL;
3517 mpctx->sh_audio=NULL;
3518 mpctx->sh_video=NULL;
3520 current_module="open_stream";
3521 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3522 if(!mpctx->stream) { // error...
3523 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3524 goto goto_next_file;
3526 mpctx->initialized_flags|=INITIALIZED_STREAM;
3528 #ifdef CONFIG_GUI
3529 if ( use_gui ) guiGetEvent( guiSetStream,(char *)mpctx->stream );
3530 #endif
3532 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3533 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3534 "playlist support will not be used automatically.\n"
3535 "MPlayer's playlist code is unsafe and should only be used with "
3536 "trusted sources.\nPlayback will probably fail.\n\n");
3537 #if 0
3538 play_tree_t* entry;
3539 // Handle playlist
3540 current_module="handle_playlist";
3541 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3542 filename_recode(mpctx->filename));
3543 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3544 mpctx->eof=playtree_add_playlist(mpctx, entry);
3545 goto goto_next_file;
3546 #endif
3548 mpctx->stream->start_pos+=seek_to_byte;
3550 if(stream_dump_type==5){
3551 unsigned char buf[4096];
3552 int len;
3553 FILE *f;
3554 current_module="dumpstream";
3555 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3556 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot dump this stream - no file descriptor available.\n");
3557 exit_player(mpctx, EXIT_ERROR);
3559 stream_reset(mpctx->stream);
3560 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3561 f=fopen(stream_dump_name,"wb");
3562 if(!f){
3563 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3564 exit_player(mpctx, EXIT_ERROR);
3566 if (dvd_chapter > 1) {
3567 int chapter = dvd_chapter - 1;
3568 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3570 while(!mpctx->stream->eof && !async_quit_request){
3571 len=stream_read(mpctx->stream,buf,4096);
3572 if(len>0) {
3573 if(fwrite(buf,len,1,f) != 1) {
3574 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3575 exit_player(mpctx, EXIT_ERROR);
3578 if(dvd_last_chapter > 0) {
3579 int chapter = -1;
3580 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3581 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3582 break;
3585 if(fclose(f)) {
3586 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3587 exit_player(mpctx, EXIT_ERROR);
3589 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3590 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3593 #ifdef CONFIG_DVDREAD
3594 if(mpctx->stream->type==STREAMTYPE_DVD){
3595 current_module="dvd lang->id";
3596 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3597 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3598 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3599 // setup global sub numbering
3600 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3601 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3602 current_module=NULL;
3604 #endif
3606 #ifdef CONFIG_DVDNAV
3607 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3608 current_module="dvdnav lang->id";
3609 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3610 if(dvdsub_lang && opts->sub_id==-2) opts->sub_id=-1;
3611 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3612 // setup global sub numbering
3613 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3614 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3615 current_module=NULL;
3617 #endif
3619 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3620 goto_enable_cache:
3621 if(stream_cache_size>0){
3622 current_module="enable_cache";
3623 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3624 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3625 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3626 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3629 //============ Open DEMUXERS --- DETECT file type =======================
3630 current_module="demux_open";
3632 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3634 // HACK to get MOV Reference Files working
3636 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3638 unsigned char* playlist_entry;
3639 play_tree_t *list = NULL, *entry = NULL;
3641 current_module="handle_demux_playlist";
3642 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3644 char *temp, *bname;
3646 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3647 filename_recode(playlist_entry));
3649 bname=mp_basename(playlist_entry);
3650 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3651 continue;
3653 if (!strncmp(bname,mp_basename(mpctx->filename),strlen(bname))) // ignoring self-reference
3654 continue;
3656 entry = play_tree_new();
3658 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3660 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3661 if (temp)
3663 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3664 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3665 strcat(temp, playlist_entry);
3666 play_tree_add_file(entry,temp);
3667 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3668 free(temp);
3671 else
3672 play_tree_add_file(entry,playlist_entry);
3674 if(!list)
3675 list = entry;
3676 else
3677 play_tree_append_entry(list,entry);
3679 free_demuxer(mpctx->demuxer);
3680 mpctx->demuxer = NULL;
3682 if (list)
3684 entry = play_tree_new();
3685 play_tree_set_child(entry,list);
3686 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3687 goto goto_next_file;
3691 if(!mpctx->demuxer)
3692 goto goto_next_file;
3694 if (mpctx->demuxer->matroska_data.ordered_chapters)
3695 build_ordered_chapter_timeline(mpctx);
3697 if (!mpctx->sources) {
3698 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
3699 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
3700 .demuxer = mpctx->demuxer};
3701 mpctx->num_sources = 1;
3704 if(dvd_chapter>1) {
3705 double pts;
3706 if (seek_chapter(mpctx, dvd_chapter-1, &pts, NULL) >= 0 && pts > -1.0)
3707 seek(mpctx, pts, SEEK_ABSOLUTE);
3710 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3712 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3713 int i;
3714 int maxid = -1;
3715 // setup global sub numbering
3716 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3717 for (i = 0; i < MAX_S_STREAMS; i++)
3718 if (mpctx->demuxer->s_streams[i])
3719 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3720 mpctx->global_sub_size += maxid + 1;
3722 // Make opts->sub_id always selectable if set.
3723 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3724 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3726 #ifdef CONFIG_ASS
3727 if (ass_enabled && ass_library) {
3728 for (int j = 0; j < mpctx->num_sources; j++) {
3729 struct demuxer *d = mpctx->sources[j].demuxer;
3730 for (int i = 0; i < d->num_attachments; i++) {
3731 struct demux_attachment *att = d->attachments + i;
3732 if (extract_embedded_fonts
3733 && att->name && att->type && att->data && att->data_size
3734 && (strcmp(att->type, "application/x-truetype-font") == 0
3735 || strcmp(att->type, "application/x-font") == 0))
3736 ass_add_font(ass_library, att->name, att->data, att->data_size);
3740 #endif
3742 current_module="demux_open2";
3744 //file_format=demuxer->file_format;
3746 mpctx->d_audio=mpctx->demuxer->audio;
3747 mpctx->d_video=mpctx->demuxer->video;
3748 mpctx->d_sub=mpctx->demuxer->sub;
3750 // select audio stream
3751 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3753 // DUMP STREAMS:
3754 if((stream_dump_type)&&(stream_dump_type<4)){
3755 FILE *f;
3756 demux_stream_t *ds=NULL;
3757 current_module="dump";
3758 // select stream to dump
3759 switch(stream_dump_type){
3760 case 1: ds=mpctx->d_audio;break;
3761 case 2: ds=mpctx->d_video;break;
3762 case 3: ds=mpctx->d_sub;break;
3764 if(!ds){
3765 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"dump: FATAL: Selected stream missing!\n");
3766 exit_player(mpctx, EXIT_ERROR);
3768 // disable other streams:
3769 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3770 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3771 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3772 // let's dump it!
3773 f=fopen(stream_dump_name,"wb");
3774 if(!f){
3775 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3776 exit_player(mpctx, EXIT_ERROR);
3778 while(!ds->eof){
3779 unsigned char* start;
3780 int in_size=ds_get_packet(ds,&start);
3781 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3782 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3783 if(in_size>0) fwrite(start,in_size,1,f);
3784 if(dvd_last_chapter>0) {
3785 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3786 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3787 break;
3790 fclose(f);
3791 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3792 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3795 mpctx->sh_audio=mpctx->d_audio->sh;
3796 mpctx->sh_video=mpctx->d_video->sh;
3798 if(mpctx->sh_video){
3800 current_module="video_read_properties";
3801 if(!video_read_properties(mpctx->sh_video)) {
3802 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Video: Cannot read properties.\n");
3803 mpctx->sh_video=mpctx->d_video->sh=NULL;
3804 } else {
3805 mp_tmsg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
3806 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3807 mpctx->sh_video->fps,mpctx->sh_video->frametime
3810 /* need to set fps here for output encoders to pick it up in their init */
3811 if(force_fps){
3812 mpctx->sh_video->fps=force_fps;
3813 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3815 vo_fps = mpctx->sh_video->fps;
3817 if(!mpctx->sh_video->fps && !force_fps){
3818 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"FPS not specified in the header or invalid, use the -fps option.\n");
3819 mpctx->sh_video=mpctx->d_video->sh=NULL;
3825 if(!mpctx->sh_video && !mpctx->sh_audio){
3826 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "No stream found.\n");
3827 #ifdef CONFIG_DVBIN
3828 if(mpctx->stream->type == STREAMTYPE_DVB)
3830 int dir;
3831 int v = mpctx->last_dvb_step;
3832 if(v > 0)
3833 dir = DVB_CHANNEL_HIGHER;
3834 else
3835 dir = DVB_CHANNEL_LOWER;
3837 if(dvb_step_channel(mpctx->stream, dir)) {
3838 mpctx->stop_play = PT_NEXT_ENTRY;
3839 mpctx->dvbin_reopen = 1;
3842 #endif
3843 goto goto_next_file; // exit_player(_("Fatal error"));
3846 /* display clip info */
3847 demux_info_print(mpctx->demuxer);
3849 //================== Read SUBTITLES (DVD & TEXT) ==========================
3850 if(vo_spudec==NULL && mpctx->sh_video &&
3851 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3852 init_vo_spudec(mpctx);
3855 if(mpctx->sh_video) {
3856 // after reading video params we should load subtitles because
3857 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3858 // check .sub
3859 current_module="read_subtitles_file";
3860 if(sub_name){
3861 for (i = 0; sub_name[i] != NULL; ++i)
3862 add_subtitles(mpctx, sub_name[i], mpctx->sh_video->fps, 0);
3864 if(sub_auto) { // auto load sub file ...
3865 char *psub = get_path( "sub/" );
3866 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3867 int i = 0;
3868 free(psub); // release the buffer created by get_path() above
3869 while (tmp[i]) {
3870 add_subtitles(mpctx, tmp[i], mpctx->sh_video->fps, 1);
3871 free(tmp[i++]);
3873 free(tmp);
3875 if (mpctx->set_of_sub_size > 0) {
3876 // setup global sub numbering
3877 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3878 mpctx->global_sub_size += mpctx->set_of_sub_size;
3882 if (mpctx->global_sub_size) {
3883 // find the best sub to use
3884 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3885 mpctx->global_sub_pos = -1; // no subs by default
3886 if (vobsub_index_id >= 0) {
3887 // if user asks for a vobsub id, use that first.
3888 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3889 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3890 // if user asks for a dvd sub id, use that next.
3891 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3892 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3893 // if there are text subs to use, use those. (autosubs come last here)
3894 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3895 } else if (opts->sub_id < 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3896 // finally select subs by language and container hints
3897 if (opts->sub_id < 0 && dvdsub_lang)
3898 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3899 if (opts->sub_id < 0)
3900 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3901 if (opts->sub_id >= 0)
3902 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3904 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3905 mpctx->global_sub_pos--;
3906 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3907 if(subdata)
3908 switch (stream_dump_type) {
3909 case 3: list_sub_file(subdata); break;
3910 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3911 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3912 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3913 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3914 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3918 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3919 filename_recode(mpctx->filename));
3920 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3921 if (mpctx->sh_video) {
3922 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3923 if (mpctx->sh_video->format >= 0x20202020)
3924 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3925 else
3926 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3927 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3928 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3929 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3930 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3931 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3933 if (mpctx->sh_audio) {
3934 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3935 if (mpctx->sh_audio->format >= 0x20202020)
3936 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3937 else
3938 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3939 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3940 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3941 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3943 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3944 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n", mpctx->stream->seek ? 1 : 0);
3945 if (mpctx->demuxer) {
3946 if (mpctx->demuxer->num_chapters == 0)
3947 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3948 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3951 if(!mpctx->sh_video) goto main; // audio-only
3953 if(!reinit_video_chain(mpctx)) {
3954 if(!mpctx->sh_video){
3955 if(!mpctx->sh_audio) goto goto_next_file;
3956 goto main; // exit_player(_("Fatal error"));
3960 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3961 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3963 #ifdef CONFIG_FREETYPE
3964 force_load_font = 1;
3965 #endif
3967 //================== MAIN: ==========================
3968 main:
3969 current_module="main";
3971 if(playing_msg) {
3972 char* msg = property_expand_string(mpctx, playing_msg);
3973 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3974 free(msg);
3978 // Disable the term OSD in verbose mode
3979 if(verbose) term_osd = 0;
3982 //int frame_corr_num=0; //
3983 //float v_frame=0; // Video
3984 //float num_frames=0; // number of frames played
3986 int frame_time_remaining=0; // flag
3987 int blit_frame=0;
3988 mpctx->num_buffered_frames=0;
3990 // Make sure old OSD does not stay around,
3991 // e.g. with -fixed-vo and same-resolution files
3992 clear_osd_msgs();
3993 update_osd_msg(mpctx);
3995 //================ SETUP AUDIO ==========================
3997 if(mpctx->sh_audio){
3998 reinit_audio_chain(mpctx);
3999 if (mpctx->sh_audio && mpctx->sh_audio->codec)
4000 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
4003 current_module="av_init";
4005 if(mpctx->sh_video){
4006 mpctx->sh_video->timer=0;
4007 if (! ignore_start)
4008 audio_delay += mpctx->sh_video->stream_delay;
4010 if(mpctx->sh_audio){
4011 if (start_volume >= 0)
4012 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
4013 if (! ignore_start)
4014 audio_delay -= mpctx->sh_audio->stream_delay;
4015 mpctx->delay=-audio_delay;
4018 if(!mpctx->sh_audio){
4019 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Audio: no sound\n");
4020 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
4021 ds_free_packs(mpctx->d_audio); // free buffered chunks
4022 //mpctx->d_audio->id=-2; // do not read audio chunks
4023 //uninit_player(mpctx, INITIALIZED_AO); // close device
4025 if(!mpctx->sh_video){
4026 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Video: no video\n");
4027 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
4028 ds_free_packs(mpctx->d_video);
4029 mpctx->d_video->id=-2;
4030 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
4033 if (!mpctx->sh_video && !mpctx->sh_audio)
4034 goto goto_next_file;
4036 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
4037 if(force_fps && mpctx->sh_video){
4038 vo_fps = mpctx->sh_video->fps=force_fps;
4039 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
4040 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx->sh_video->fps,mpctx->sh_video->frametime);
4043 #ifdef CONFIG_GUI
4044 if ( use_gui ) {
4045 if ( mpctx->sh_audio ) guiIntfStruct.AudioType=mpctx->sh_audio->channels; else guiIntfStruct.AudioType=0;
4046 if ( !mpctx->sh_video && mpctx->sh_audio ) guiGetEvent( guiSetAudioOnly,(char *)1 ); else guiGetEvent( guiSetAudioOnly,(char *)0 );
4047 guiGetEvent( guiSetFileFormat,(char *)mpctx->demuxer->file_format );
4048 if ( guiGetEvent( guiSetValues,(char *)mpctx->sh_video ) ) goto goto_next_file;
4049 guiGetEvent( guiSetDemuxer,(char *)mpctx->demuxer );
4051 #endif
4053 mp_input_set_section(mpctx->input, NULL);
4054 //TODO: add desired (stream-based) sections here
4055 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
4056 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
4058 //==================== START PLAYING =======================
4060 if(opts->loop_times>1) opts->loop_times--; else
4061 if(opts->loop_times==1) opts->loop_times = -1;
4063 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Starting playback...\n");
4065 total_time_usage_start=GetTimer();
4066 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
4067 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
4068 play_n_frames=play_n_frames_mf;
4070 if(play_n_frames==0){
4071 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
4074 if (seek_to_sec) {
4075 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
4076 end_at.pos += seek_to_sec;
4079 if (end_at.type == END_AT_SIZE) {
4080 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Option -endpos in MPlayer does not yet support size units.\n");
4081 end_at.type = END_AT_NONE;
4084 #ifdef CONFIG_DVDNAV
4085 mp_dvdnav_context_free(mpctx);
4086 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4087 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
4088 mp_dvdnav_cell_has_changed(mpctx->stream,1);
4090 #endif
4092 get_relative_time(mpctx); // reset current delta
4093 mpctx->time_frame = 0;
4094 mpctx->drop_message_shown = 0;
4095 mpctx->update_video_immediately = true;
4096 mpctx->total_avsync_change = 0;
4097 // Make sure VO knows current pause state
4098 if (mpctx->sh_video)
4099 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
4100 NULL);
4102 while(!mpctx->stop_play){
4103 float aq_sleep_time=0;
4105 if(dvd_last_chapter>0) {
4106 int cur_chapter = get_current_chapter(mpctx);
4107 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
4108 goto goto_next_file;
4111 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
4112 mpctx->sh_audio = mpctx->d_audio->sh;
4113 mpctx->sh_audio->ds = mpctx->d_audio;
4114 reinit_audio_chain(mpctx);
4117 /*========================== PLAY AUDIO ============================*/
4119 if (mpctx->sh_audio && !mpctx->paused)
4120 if (!fill_audio_out_buffers(mpctx))
4121 // at eof, all audio at least written to ao
4122 if (!mpctx->sh_video)
4123 mpctx->stop_play = AT_END_OF_FILE;
4126 if(!mpctx->sh_video) {
4127 // handle audio-only case:
4128 double a_pos=0;
4129 // sh_audio can be NULL due to video stream switching
4130 // TODO: handle this better
4131 if (mpctx->sh_audio)
4132 a_pos = playing_audio_pts(mpctx);
4134 print_status(mpctx, a_pos, false);
4136 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
4137 mpctx->stop_play = PT_NEXT_ENTRY;
4138 update_osd_msg(mpctx);
4140 } else {
4142 /*========================== PLAY VIDEO ============================*/
4144 vo_pts=mpctx->sh_video->timer*90000.0;
4145 vo_fps=mpctx->sh_video->fps;
4147 if (!mpctx->num_buffered_frames) {
4148 double frame_time = update_video(mpctx, &blit_frame);
4149 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
4150 if (mpctx->sh_video->vf_initialized < 0) {
4151 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "\nFATAL: Could not initialize video filters (-vf) or video output (-vo).\n");
4152 mpctx->stop_play = PT_NEXT_ENTRY;
4153 goto goto_next_file;
4155 if (blit_frame) {
4156 struct vf_instance *vf = mpctx->sh_video->vfilter;
4157 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
4158 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
4159 vo_osd_changed(0);
4161 if (frame_time < 0)
4162 mpctx->stop_play = AT_END_OF_FILE;
4163 else {
4164 // might return with !eof && !blit_frame if !correct_pts
4165 mpctx->num_buffered_frames += blit_frame;
4166 if (mpctx->update_video_immediately) {
4167 // Show this frame immediately, rest normally
4168 mpctx->update_video_immediately = false;
4169 } else {
4170 mpctx->time_frame += frame_time / opts->playback_speed;
4171 adjust_sync(mpctx, frame_time);
4175 if (mpctx->timeline) {
4176 struct timeline_part *next = mpctx->timeline + mpctx->timeline_part + 1;
4177 if (mpctx->sh_video->pts >= next->start
4178 || mpctx->stop_play == AT_END_OF_FILE
4179 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
4180 seek(mpctx, next->start, SEEK_ABSOLUTE);
4181 continue;
4185 // ==========================================================================
4187 // current_module="draw_osd";
4188 // if(vo_config_count) mpctx->video_out->draw_osd();
4190 #ifdef CONFIG_GUI
4191 if(use_gui) guiEventHandling();
4192 #endif
4194 current_module="vo_check_events";
4195 vo_check_events(mpctx->video_out);
4197 #ifdef CONFIG_X11
4198 if (stop_xscreensaver) {
4199 current_module = "stop_xscreensaver";
4200 xscreensaver_heartbeat(mpctx->x11_state);
4202 #endif
4203 if (heartbeat_cmd) {
4204 static unsigned last_heartbeat;
4205 unsigned now = GetTimerMS();
4206 if (now - last_heartbeat > 30000) {
4207 last_heartbeat = now;
4208 system(heartbeat_cmd);
4212 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
4214 //====================== FLIP PAGE (VIDEO BLT): =========================
4216 current_module="flip_page";
4217 if (!frame_time_remaining && blit_frame) {
4218 unsigned int t2=GetTimer();
4220 vo_flip_page(mpctx->video_out);
4221 mpctx->num_buffered_frames--;
4223 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
4224 vout_time_usage += mpctx->last_vo_flip_duration;
4225 print_status(mpctx, MP_NOPTS_VALUE, true);
4227 else
4228 print_status(mpctx, MP_NOPTS_VALUE, false);
4230 //============================ Auto QUALITY ============================
4232 /*Output quality adjustments:*/
4233 if(auto_quality>0){
4234 current_module="autoq";
4235 // float total=0.000001f * (GetTimer()-aq_total_time);
4236 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4237 if(output_quality<auto_quality && aq_sleep_time>0)
4238 ++output_quality;
4239 else
4240 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4241 if(output_quality>1 && aq_sleep_time<0)
4242 --output_quality;
4243 else
4244 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4245 output_quality=0;
4246 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4247 set_video_quality(mpctx->sh_video,output_quality);
4250 if (!frame_time_remaining && blit_frame) {
4251 if (play_n_frames >= 0) {
4252 --play_n_frames;
4253 if (play_n_frames <= 0)
4254 mpctx->stop_play = PT_NEXT_ENTRY;
4256 if (mpctx->step_frames > 0) {
4257 mpctx->step_frames--;
4258 if (mpctx->step_frames == 0)
4259 pause_player(mpctx);
4264 // FIXME: add size based support for -endpos
4265 if (end_at.type == END_AT_TIME &&
4266 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
4267 mpctx->stop_play = PT_NEXT_ENTRY;
4269 } // end if(mpctx->sh_video)
4271 #ifdef CONFIG_DVDNAV
4272 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4273 nav_highlight_t hl;
4274 mp_dvdnav_get_highlight (mpctx->stream, &hl);
4275 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4276 vo_osd_changed (OSDTYPE_DVDNAV);
4278 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
4279 double ar = -1.0;
4280 if (stream_control (mpctx->demuxer->stream,
4281 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
4282 != STREAM_UNSUPPORTED)
4283 mpctx->sh_video->stream_aspect = ar;
4286 #endif
4288 //================= Keyboard events, SEEKing ====================
4290 current_module="key_events";
4293 while (1) {
4294 mp_cmd_t* cmd;
4295 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
4296 run_command(mpctx, cmd);
4297 mp_cmd_free(cmd);
4298 if (mpctx->stop_play)
4299 break;
4301 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
4302 || mpctx->abs_seek_pos)
4303 break;
4304 if (mpctx->sh_video) {
4305 update_osd_msg(mpctx);
4306 int hack = vo_osd_changed(0);
4307 vo_osd_changed(hack);
4308 if (hack)
4309 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
4310 add_step_frame(mpctx);
4311 break;
4313 else
4314 vo_osd_changed(0);
4316 pause_loop(mpctx);
4320 // handle -sstep
4321 if (step_sec > 0 && !mpctx->paused) {
4322 mpctx->osd_function=OSD_FFW;
4323 mpctx->rel_seek_secs+=step_sec;
4326 edl_update(mpctx);
4328 /* Looping. */
4329 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
4330 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
4332 if(opts->loop_times>1) opts->loop_times--; else
4333 if(opts->loop_times==1) opts->loop_times=-1;
4334 play_n_frames=play_n_frames_mf;
4335 mpctx->stop_play=0;
4336 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
4339 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
4340 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
4342 mpctx->rel_seek_secs=0;
4343 mpctx->abs_seek_pos=0;
4346 #ifdef CONFIG_GUI
4347 if(use_gui){
4348 guiEventHandling();
4349 if(mpctx->demuxer->file_format==DEMUXER_TYPE_AVI && mpctx->sh_video && mpctx->sh_video->video.dwLength>2){
4350 // get pos from frame number / total frames
4351 guiIntfStruct.Position=(float)mpctx->d_video->pack_no*100.0f/mpctx->sh_video->video.dwLength;
4352 } else {
4353 guiIntfStruct.Position=demuxer_get_percent_pos(mpctx->demuxer);
4355 if ( mpctx->sh_video ) guiIntfStruct.TimeSec=mpctx->sh_video->pts;
4356 else if ( mpctx->sh_audio ) guiIntfStruct.TimeSec=playing_audio_pts(mpctx);
4357 guiIntfStruct.LengthInSec=demuxer_get_time_length(mpctx->demuxer);
4358 guiGetEvent( guiReDraw,NULL );
4359 guiGetEvent( guiSetVolume,NULL );
4360 if(guiIntfStruct.Playing==0) break; // STOP
4361 if(guiIntfStruct.Playing==2) mpctx->osd_function=OSD_PAUSE;
4362 if ( guiIntfStruct.DiskChanged || guiIntfStruct.NewPlay ) goto goto_next_file;
4363 #ifdef CONFIG_DVDREAD
4364 if ( mpctx->stream->type == STREAMTYPE_DVD )
4366 dvd_priv_t * dvdp = mpctx->stream->priv;
4367 guiIntfStruct.DVD.current_chapter=dvd_chapter_from_cell(dvdp,guiIntfStruct.DVD.current_title-1, dvdp->cur_cell)+1;
4369 #endif
4371 #endif /* CONFIG_GUI */
4373 } // while(!mpctx->stop_play)
4375 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4377 #ifdef CONFIG_DVBIN
4378 if(mpctx->dvbin_reopen)
4380 mpctx->stop_play = 0;
4381 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI|INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4382 cache_uninit(mpctx->stream);
4383 mpctx->dvbin_reopen = 0;
4384 goto goto_enable_cache;
4386 #endif
4389 goto_next_file: // don't jump here after ao/vo/getch initialization!
4391 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4393 if(benchmark){
4394 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4395 double total_time_usage;
4396 total_time_usage_start=GetTimer()-total_time_usage_start;
4397 total_time_usage = (float)total_time_usage_start*0.000001;
4398 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4399 video_time_usage,vout_time_usage,audio_time_usage,
4400 total_time_usage-tot,total_time_usage);
4401 if(total_time_usage>0.0)
4402 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4403 100.0*video_time_usage/total_time_usage,
4404 100.0*vout_time_usage/total_time_usage,
4405 100.0*audio_time_usage/total_time_usage,
4406 100.0*(total_time_usage-tot)/total_time_usage,
4407 100.0);
4408 if(total_frame_cnt && frame_dropping)
4409 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4410 total_frame_cnt-drop_frame_cnt,
4411 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4412 drop_frame_cnt,
4413 100*drop_frame_cnt/total_frame_cnt,
4414 total_frame_cnt,
4415 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4418 // time to uninit all, except global stuff:
4419 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_GUI+(opts->fixed_vo?INITIALIZED_VO:0)));
4421 if(mpctx->set_of_sub_size > 0) {
4422 current_module="sub_free";
4423 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4424 sub_free(mpctx->set_of_subtitles[i]);
4425 #ifdef CONFIG_ASS
4426 if(mpctx->set_of_ass_tracks[i])
4427 ass_free_track( mpctx->set_of_ass_tracks[i] );
4428 #endif
4430 mpctx->set_of_sub_size = 0;
4432 vo_sub_last = vo_sub=NULL;
4433 subdata=NULL;
4434 #ifdef CONFIG_ASS
4435 ass_track = NULL;
4436 if(ass_library)
4437 ass_clear_fonts(ass_library);
4438 #endif
4440 if (!mpctx->stop_play) // In case some goto jumped here...
4441 mpctx->stop_play = PT_NEXT_ENTRY;
4443 int playtree_direction = 1;
4445 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4446 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4447 play_tree_iter_free(mpctx->playtree_iter);
4448 mpctx->playtree_iter = NULL;
4450 mpctx->play_tree_step = 1;
4451 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4452 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4453 if(mpctx->playtree_iter) {
4454 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4455 play_tree_iter_free(mpctx->playtree_iter);
4456 mpctx->playtree_iter = NULL;
4459 } else if (mpctx->stop_play == PT_STOP) {
4460 play_tree_iter_free(mpctx->playtree_iter);
4461 mpctx->playtree_iter = NULL;
4462 } else { // NEXT PREV SRC
4463 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4466 while(mpctx->playtree_iter != NULL) {
4467 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4468 if(mpctx->filename == NULL) {
4469 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4470 play_tree_iter_free(mpctx->playtree_iter);
4471 mpctx->playtree_iter = NULL;
4473 } else
4474 break;
4477 #ifdef CONFIG_GUI
4478 if(use_gui && !mpctx->playtree_iter) {
4479 #ifdef CONFIG_DVDREAD
4480 if(!guiIntfStruct.DiskChanged)
4481 #endif
4482 mplEnd();
4484 #endif
4486 if(use_gui || mpctx->playtree_iter != NULL || player_idle_mode){
4487 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4488 mpctx->stop_play = 0;
4489 goto play_next_file;
4493 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4495 return 1;
4497 #endif /* DISABLE_MAIN */