Merge remote branch 'mplayer/tmp_build'
[mplayer/glamo.git] / mplayer.c
blobc306c73b4f3c8378ca6b8bb9284f293d3ba92da1
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 "ffmpeg_files/intreadwrite.h"
55 #include "libavutil/avstring.h"
57 #include "subreader.h"
59 #include "mp_osd.h"
60 #include "libvo/video_out.h"
62 #include "libvo/font_load.h"
63 #include "libvo/sub.h"
65 #ifdef CONFIG_X11
66 #include "libvo/x11_common.h"
67 #endif
69 #include "libao2/audio_out.h"
71 #include "codec-cfg.h"
73 #include "edl.h"
75 #include "spudec.h"
76 #include "vobsub.h"
78 #include "osdep/getch2.h"
79 #include "osdep/timer.h"
80 #include "osdep/findfiles.h"
82 #include "input/input.h"
84 const int under_mencoder = 0;
85 int slave_mode=0;
86 int player_idle_mode=0;
87 int quiet=0;
88 int enable_mouse_movements=0;
89 float start_volume = -1;
91 #include "osdep/priority.h"
93 char *heartbeat_cmd;
95 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
97 #ifdef HAVE_RTC
98 #ifdef __linux__
99 #include <linux/rtc.h>
100 #else
101 #include <rtc.h>
102 #define RTC_IRQP_SET RTCIO_IRQP_SET
103 #define RTC_PIE_ON RTCIO_PIE_ON
104 #endif /* __linux__ */
105 #endif /* HAVE_RTC */
107 #include "stream/tv.h"
108 #include "stream/stream_radio.h"
109 #ifdef CONFIG_DVBIN
110 #include "stream/dvbin.h"
111 #endif
112 #include "stream/cache2.h"
114 //**************************************************************************//
115 // Playtree
116 //**************************************************************************//
117 #include "playtree.h"
118 #include "playtreeparser.h"
120 //**************************************************************************//
121 // Config
122 //**************************************************************************//
123 #include "parser-cfg.h"
124 #include "parser-mpcmd.h"
126 //**************************************************************************//
127 // Config file
128 //**************************************************************************//
130 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
132 #include "get_path.h"
134 //**************************************************************************//
135 //**************************************************************************//
136 // Input media streaming & demultiplexer:
137 //**************************************************************************//
139 static int max_framesize=0;
141 #include "stream/stream.h"
142 #include "libmpdemux/demuxer.h"
143 #include "libmpdemux/stheader.h"
145 #ifdef CONFIG_DVDREAD
146 #include "stream/stream_dvd.h"
147 #endif
148 #include "stream/stream_dvdnav.h"
150 #include "libmpcodecs/dec_audio.h"
151 #include "libmpcodecs/dec_video.h"
152 #include "libmpcodecs/mp_image.h"
153 #include "libmpcodecs/vf.h"
154 #include "libmpcodecs/vd.h"
156 #include "mixer.h"
158 #include "mp_core.h"
159 #include "options.h"
160 #include "defaultopts.h"
162 #define Exit_SIGILL_RTCpuSel _(\
163 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
164 " It may be a bug in our new runtime CPU-detection code...\n"\
165 " Please read DOCS/HTML/en/bugreports.html.\n")
167 #define Exit_SIGILL _(\
168 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
169 " It usually happens when you run it on a CPU different than the one it was\n"\
170 " compiled/optimized for.\n"\
171 " Verify this!\n")
173 #define Exit_SIGSEGV_SIGFPE _(\
174 "- MPlayer crashed by bad usage of CPU/FPU/RAM.\n"\
175 " Recompile MPlayer with --enable-debug and make a 'gdb' backtrace and\n"\
176 " disassembly. Details in DOCS/HTML/en/bugreports_what.html#bugreports_crash.\n")
178 #define Exit_SIGCRASH _(\
179 "- MPlayer crashed. This shouldn't happen.\n"\
180 " It can be a bug in the MPlayer code _or_ in your drivers _or_ in your\n"\
181 " gcc version. If you think it's MPlayer's fault, please read\n"\
182 " DOCS/HTML/en/bugreports.html and follow the instructions there. We can't and\n"\
183 " won't help unless you provide this information when reporting a possible bug.\n")
185 #define SystemTooSlow _("\n\n"\
186 " ************************************************\n"\
187 " **** Your system is too SLOW to play this! ****\n"\
188 " ************************************************\n\n"\
189 "Possible reasons, problems, workarounds:\n"\
190 "- Most common: broken/buggy _audio_ driver\n"\
191 " - Try -ao sdl or use the OSS emulation of ALSA.\n"\
192 " - Experiment with different values for -autosync, 30 is a good start.\n"\
193 "- Slow video output\n"\
194 " - Try a different -vo driver (-vo help for a list) or try -framedrop!\n"\
195 "- Slow CPU\n"\
196 " - Don't try to play a big DVD/DivX on a slow CPU! Try some of the lavdopts,\n"\
197 " e.g. -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all.\n"\
198 "- Broken file\n"\
199 " - Try various combinations of -nobps -ni -forceidx -mc 0.\n"\
200 "- Slow media (NFS/SMB mounts, DVD, VCD etc)\n"\
201 " - Try -cache 8192.\n"\
202 "- Are you using -cache to play a non-interleaved AVI file?\n"\
203 " - Try -nocache.\n"\
204 "Read DOCS/HTML/en/video.html for tuning/speedup tips.\n"\
205 "If none of this helps you, read DOCS/HTML/en/bugreports.html.\n\n")
208 //**************************************************************************//
209 //**************************************************************************//
211 // Common FIFO functions, and keyboard/event FIFO code
212 #include "mp_fifo.h"
213 int noconsolecontrols=0;
214 //**************************************************************************//
216 // benchmark:
217 double video_time_usage=0;
218 double vout_time_usage=0;
219 static double audio_time_usage=0;
220 static int total_time_usage_start=0;
221 static int total_frame_cnt=0;
222 static int drop_frame_cnt=0; // total number of dropped frames
223 int benchmark=0;
225 // options:
226 int auto_quality=0;
227 static int output_quality=0;
229 static int list_properties = 0;
231 int term_osd = 1;
232 static char* term_osd_esc = "\x1b[A\r\x1b[K";
233 static char* playing_msg = NULL;
234 // seek:
235 static double seek_to_sec;
236 static off_t seek_to_byte=0;
237 static off_t step_sec=0;
239 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
241 // A/V sync:
242 int autosync=0; // 30 might be a good default value.
244 // codecs:
245 char **audio_codec_list=NULL; // override audio codec
246 char **video_codec_list=NULL; // override video codec
247 char **audio_fm_list=NULL; // override audio codec family
248 char **video_fm_list=NULL; // override video codec family
250 // demuxer:
251 extern char *demuxer_name; // override demuxer
252 extern char *audio_demuxer_name; // override audio demuxer
253 extern char *sub_demuxer_name; // override sub demuxer
255 int vobsub_id=-1;
256 char* audio_lang=NULL;
257 char* dvdsub_lang=NULL;
258 static char* spudec_ifo=NULL;
259 int forced_subs_only=0;
260 int file_filter=1;
262 // cache2:
263 int stream_cache_size=-1;
264 #ifdef CONFIG_STREAM_CACHE
265 extern int cache_fill_status;
267 float stream_cache_min_percent=20.0;
268 float stream_cache_seek_min_percent=50.0;
269 #else
270 #define cache_fill_status 0
271 #endif
273 // dump:
274 static char *stream_dump_name="stream.dump";
275 int stream_dump_type=0;
277 // A-V sync:
278 static float default_max_pts_correction=-1;//0.01f;
279 float audio_delay=0;
280 static int ignore_start=0;
282 static int softsleep=0;
284 double force_fps=0;
285 static int force_srate=0;
286 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
287 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
288 static int play_n_frames=-1;
289 static int play_n_frames_mf=-1;
291 // sub:
292 char *font_name=NULL;
293 char *sub_font_name=NULL;
294 extern int font_fontconfig;
295 float font_factor=0.75;
296 char **sub_name=NULL;
297 float sub_delay=0;
298 float sub_fps=0;
299 int sub_auto = 1;
300 char *vobsub_name=NULL;
301 /*DSP!!char *dsp=NULL;*/
302 int subcc_enabled=0;
303 int suboverlap_enabled = 1;
305 #include "ass_mp.h"
307 char* current_module=NULL; // for debugging
310 // ---
312 #ifdef CONFIG_MENU
313 #include "m_struct.h"
314 #include "libmenu/menu.h"
315 void vf_menu_pause_update(struct vf_instance* vf);
316 extern vf_info_t vf_info_menu;
317 static const vf_info_t* const libmenu_vfs[] = {
318 &vf_info_menu,
319 NULL
321 static vf_instance_t* vf_menu = NULL;
322 int use_menu = 0;
323 static char* menu_cfg = NULL;
324 static char* menu_root = "main";
325 #endif
328 #ifdef HAVE_RTC
329 static int nortc = 1;
330 static char* rtc_device;
331 #endif
333 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
334 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
335 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
336 int use_filedir_conf;
338 #include "mpcommon.h"
339 #include "command.h"
341 #include "metadata.h"
343 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
345 const void *mpctx_get_video_out(MPContext *mpctx)
347 return mpctx->video_out;
350 const void *mpctx_get_audio_out(MPContext *mpctx)
352 return mpctx->audio_out;
355 void *mpctx_get_demuxer(MPContext *mpctx)
357 return mpctx->demuxer;
360 void *mpctx_get_playtree_iter(MPContext *mpctx)
362 return mpctx->playtree_iter;
365 void *mpctx_get_mixer(MPContext *mpctx)
367 return &mpctx->mixer;
370 int mpctx_get_global_sub_size(MPContext *mpctx)
372 return mpctx->global_sub_size;
375 int mpctx_get_osd_function(MPContext *mpctx)
377 return mpctx->osd_function;
380 static float get_relative_time(struct MPContext *mpctx)
382 unsigned int new_time = GetTimer();
383 unsigned int delta = new_time - mpctx->last_time;
384 mpctx->last_time = new_time;
385 return delta * 0.000001;
388 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
389 switch (type)
391 /* check for valid video stream */
392 case META_VIDEO_CODEC:
393 case META_VIDEO_BITRATE:
394 case META_VIDEO_RESOLUTION:
396 if (!mpctx->sh_video)
397 return 0;
398 break;
401 /* check for valid audio stream */
402 case META_AUDIO_CODEC:
403 case META_AUDIO_BITRATE:
404 case META_AUDIO_SAMPLES:
406 if (!mpctx->sh_audio)
407 return 0;
408 break;
411 /* check for valid demuxer */
412 case META_INFO_TITLE:
413 case META_INFO_ARTIST:
414 case META_INFO_ALBUM:
415 case META_INFO_YEAR:
416 case META_INFO_COMMENT:
417 case META_INFO_TRACK:
418 case META_INFO_GENRE:
420 if (!mpctx->demuxer)
421 return 0;
422 break;
425 default:
426 break;
429 return 1;
432 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
433 char **info = mpctx->demuxer->info;
434 int n;
436 if (!info || !tag)
437 return NULL;
439 for (n = 0; info[2*n] != NULL ; n++)
440 if (!strcasecmp (info[2*n], tag))
441 break;
443 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
446 char *get_metadata(struct MPContext *mpctx, metadata_t type)
448 char *meta = NULL;
449 sh_audio_t * const sh_audio = mpctx->sh_audio;
450 sh_video_t * const sh_video = mpctx->sh_video;
452 if (!is_valid_metadata_type(mpctx, type))
453 return NULL;
455 switch (type)
457 case META_NAME:
459 return strdup (mp_basename2 (mpctx->filename));
462 case META_VIDEO_CODEC:
464 if (sh_video->format == 0x10000001)
465 meta = strdup ("mpeg1");
466 else if (sh_video->format == 0x10000002)
467 meta = strdup ("mpeg2");
468 else if (sh_video->format == 0x10000004)
469 meta = strdup ("mpeg4");
470 else if (sh_video->format == 0x10000005)
471 meta = strdup ("h264");
472 else if (sh_video->format >= 0x20202020)
474 meta = malloc (8);
475 sprintf (meta, "%.4s", (char *) &sh_video->format);
477 else
479 meta = malloc (8);
480 sprintf (meta, "0x%08X", sh_video->format);
482 return meta;
485 case META_VIDEO_BITRATE:
487 meta = malloc (16);
488 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
489 return meta;
492 case META_VIDEO_RESOLUTION:
494 meta = malloc (16);
495 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
496 return meta;
499 case META_AUDIO_CODEC:
501 if (sh_audio->codec && sh_audio->codec->name)
502 meta = strdup (sh_audio->codec->name);
503 return meta;
506 case META_AUDIO_BITRATE:
508 meta = malloc (16);
509 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
510 return meta;
513 case META_AUDIO_SAMPLES:
515 meta = malloc (16);
516 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
517 return meta;
520 /* check for valid demuxer */
521 case META_INFO_TITLE:
522 return get_demuxer_info(mpctx, "Title");
524 case META_INFO_ARTIST:
525 return get_demuxer_info(mpctx, "Artist");
527 case META_INFO_ALBUM:
528 return get_demuxer_info(mpctx, "Album");
530 case META_INFO_YEAR:
531 return get_demuxer_info(mpctx, "Year");
533 case META_INFO_COMMENT:
534 return get_demuxer_info(mpctx, "Comment");
536 case META_INFO_TRACK:
537 return get_demuxer_info(mpctx, "Track");
539 case META_INFO_GENRE:
540 return get_demuxer_info(mpctx, "Genre");
542 default:
543 break;
546 return meta;
549 /// step size of mixer changes
550 int volstep = 3;
552 #ifdef CONFIG_DVDNAV
553 static void mp_dvdnav_context_free(MPContext *ctx){
554 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
555 ctx->nav_smpi = NULL;
556 if (ctx->nav_buffer) free(ctx->nav_buffer);
557 ctx->nav_buffer = NULL;
558 ctx->nav_start = NULL;
559 ctx->nav_in_size = 0;
561 #endif
563 void uninit_player(struct MPContext *mpctx, unsigned int mask){
564 mask=mpctx->initialized_flags&mask;
566 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
568 if(mask&INITIALIZED_ACODEC){
569 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
570 current_module="uninit_acodec";
571 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
572 mpctx->sh_audio=NULL;
573 mpctx->mixer.afilter = NULL;
576 if(mask&INITIALIZED_VCODEC){
577 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
578 current_module="uninit_vcodec";
579 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
580 mpctx->sh_video=NULL;
581 #ifdef CONFIG_MENU
582 vf_menu=NULL;
583 #endif
586 if(mask&INITIALIZED_DEMUXER){
587 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
588 current_module="free_demuxer";
589 if (mpctx->num_sources) {
590 mpctx->demuxer = mpctx->sources[0].demuxer;
591 for (int i = 1; i < mpctx->num_sources; i++) {
592 free_stream(mpctx->sources[i].stream);
593 free_demuxer(mpctx->sources[i].demuxer);
596 talloc_free(mpctx->sources);
597 mpctx->sources = NULL;
598 mpctx->num_sources = 0;
599 talloc_free(mpctx->timeline);
600 mpctx->timeline = NULL;
601 mpctx->num_timeline_parts = 0;
602 talloc_free(mpctx->chapters);
603 mpctx->chapters = NULL;
604 mpctx->num_chapters = 0;
605 mpctx->video_offset = 0;
606 if(mpctx->demuxer){
607 mpctx->stream=mpctx->demuxer->stream;
608 free_demuxer(mpctx->demuxer);
610 mpctx->demuxer=NULL;
613 // kill the cache process:
614 if(mask&INITIALIZED_STREAM){
615 mpctx->initialized_flags&=~INITIALIZED_STREAM;
616 current_module="uninit_stream";
617 if(mpctx->stream) free_stream(mpctx->stream);
618 mpctx->stream=NULL;
621 if(mask&INITIALIZED_VO){
622 mpctx->initialized_flags&=~INITIALIZED_VO;
623 current_module="uninit_vo";
624 vo_destroy(mpctx->video_out);
625 mpctx->video_out=NULL;
626 #ifdef CONFIG_DVDNAV
627 mp_dvdnav_context_free(mpctx);
628 #endif
631 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
632 if(mask&INITIALIZED_GETCH2){
633 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
634 current_module="uninit_getch2";
635 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
636 // restore terminal:
637 getch2_disable();
640 if(mask&INITIALIZED_VOBSUB){
641 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
642 current_module="uninit_vobsub";
643 if(vo_vobsub) vobsub_close(vo_vobsub);
644 vo_vobsub=NULL;
647 if (mask&INITIALIZED_SPUDEC){
648 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
649 current_module="uninit_spudec";
650 spudec_free(vo_spudec);
651 vo_spudec=NULL;
654 if(mask&INITIALIZED_AO){
655 mpctx->initialized_flags&=~INITIALIZED_AO;
656 current_module="uninit_ao";
657 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
658 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
659 mpctx->audio_out=NULL;
662 current_module=NULL;
665 void exit_player_with_rc(struct MPContext *mpctx, exit_reason_t how, int rc)
667 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
668 uninit_player(mpctx, INITIALIZED_ALL);
669 #if defined(__MINGW32__) || defined(__CYGWIN__)
670 timeEndPeriod(1);
671 #endif
672 #ifdef CONFIG_X11
673 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
674 #endif
676 current_module="uninit_input";
677 mp_input_uninit(mpctx->input);
678 #ifdef CONFIG_MENU
679 if (use_menu)
680 menu_uninit();
681 #endif
683 #ifdef CONFIG_FREETYPE
684 current_module="uninit_font";
685 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
686 free_font_desc(mpctx->osd->sub_font);
687 free_font_desc(vo_font);
688 vo_font = NULL;
689 done_freetype();
690 #endif
691 osd_free(mpctx->osd);
693 #ifdef CONFIG_ASS
694 ass_library_done(ass_library);
695 #endif
697 current_module="exit_player";
699 // free mplayer config
700 if(mpctx->mconfig)
701 m_config_free(mpctx->mconfig);
703 if(mpctx->playtree)
704 play_tree_free(mpctx->playtree, 1);
706 talloc_free(mpctx->key_fifo);
708 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
709 switch(how) {
710 case EXIT_QUIT:
711 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Quit");
712 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
713 break;
714 case EXIT_EOF:
715 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","End of file");
716 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
717 break;
718 case EXIT_ERROR:
719 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Fatal error");
720 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
721 break;
722 default:
723 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
725 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
727 exit(rc);
730 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
732 exit_player_with_rc(mpctx, how, 1);
735 #ifndef __MINGW32__
736 static void child_sighandler(int x){
737 pid_t pid;
738 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
740 #endif
742 #ifdef CONFIG_CRASH_DEBUG
743 static char *prog_path;
744 static int crash_debug = 0;
745 #endif
747 static void exit_sighandler(int x){
748 static int sig_count=0;
749 #ifdef CONFIG_CRASH_DEBUG
750 if (!crash_debug || x != SIGTRAP)
751 #endif
752 ++sig_count;
753 if(sig_count==5)
755 /* We're crashing bad and can't uninit cleanly :(
756 * by popular request, we make one last (dirty)
757 * effort to restore the user's
758 * terminal. */
759 getch2_disable();
760 exit(1);
762 if(sig_count==6) exit(1);
763 if(sig_count>6){
764 // can't stop :(
765 #ifndef __MINGW32__
766 kill(getpid(),SIGKILL);
767 #endif
769 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"\n" "\nMPlayer interrupted by signal %d in module: %s\n",x,
770 current_module?current_module:"unknown"
772 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
773 if(sig_count<=1)
774 switch(x){
775 case SIGINT:
776 case SIGQUIT:
777 case SIGTERM:
778 case SIGKILL:
779 async_quit_request = 1;
780 return; // killed from keyboard (^C) or killed [-9]
781 case SIGILL:
782 #if CONFIG_RUNTIME_CPUDETECT
783 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL_RTCpuSel);
784 #else
785 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL);
786 #endif
787 case SIGFPE:
788 case SIGSEGV:
789 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGSEGV_SIGFPE);
790 default:
791 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGCRASH);
792 #ifdef CONFIG_CRASH_DEBUG
793 if (crash_debug) {
794 int gdb_pid;
795 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
796 gdb_pid = fork();
797 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
798 if (gdb_pid == 0) { // We are the child
799 char spid[20];
800 snprintf(spid, sizeof(spid), "%i", getppid());
801 getch2_disable(); // allow terminal to work properly with gdb
802 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
803 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
804 } else if (gdb_pid < 0)
805 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
806 else {
807 waitpid(gdb_pid, NULL, 0);
809 if (x == SIGTRAP) return;
811 #endif
813 getch2_disable();
814 exit(1);
817 #include "cfg-mplayer.h"
819 static int cfg_include(m_option_t *conf, char *filename)
821 return m_config_parse_config_file(conf->priv, filename);
824 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
826 char *conffile;
827 int conffile_fd;
828 if (!disable_system_conf &&
829 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
830 exit_player(mpctx, EXIT_NONE);
831 if ((conffile = get_path("")) == NULL) {
832 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"Cannot find HOME directory.\n");
833 } else {
834 #ifdef __MINGW32__
835 mkdir(conffile);
836 #else
837 mkdir(conffile, 0777);
838 #endif
839 free(conffile);
840 if ((conffile = get_path("config")) == NULL) {
841 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
842 } else {
843 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
844 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Creating config file: %s\n", conffile);
845 write(conffile_fd, default_config, strlen(default_config));
846 close(conffile_fd);
848 if (!disable_user_conf &&
849 m_config_parse_config_file(conf, conffile) < 0)
850 exit_player(mpctx, EXIT_NONE);
851 free(conffile);
856 #define PROFILE_CFG_PROTOCOL "protocol."
858 static void load_per_protocol_config (m_config_t* conf, const char *const file)
860 char *str;
861 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
862 m_profile_t *p;
864 /* does filename actually uses a protocol ? */
865 str = strstr (file, "://");
866 if (!str)
867 return;
869 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
870 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
871 p = m_config_get_profile (conf, protocol);
872 if (p)
874 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading protocol-related profile '%s'\n", protocol);
875 m_config_set_profile(conf,p);
879 #define PROFILE_CFG_EXTENSION "extension."
881 static void load_per_extension_config (m_config_t* conf, const char *const file)
883 char *str;
884 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
885 m_profile_t *p;
887 /* does filename actually have an extension ? */
888 str = strrchr (file, '.');
889 if (!str)
890 return;
892 sprintf (extension, PROFILE_CFG_EXTENSION);
893 strncat (extension, ++str, 7);
894 p = m_config_get_profile (conf, extension);
895 if (p)
897 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", extension);
898 m_config_set_profile(conf,p);
902 #define PROFILE_CFG_VO "vo."
903 #define PROFILE_CFG_AO "ao."
905 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
907 char profile[strlen (cfg) + strlen (out) + 1];
908 m_profile_t *p;
910 sprintf (profile, "%s%s", cfg, out);
911 p = m_config_get_profile (conf, profile);
912 if (p)
914 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", profile);
915 m_config_set_profile(conf,p);
920 * Tries to load a config file
921 * @return 0 if file was not found, 1 otherwise
923 static int try_load_config(m_config_t *conf, const char *file)
925 struct stat st;
926 if (stat(file, &st))
927 return 0;
928 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Loading config '%s'\n", file);
929 m_config_parse_config_file (conf, file);
930 return 1;
933 static void load_per_file_config (m_config_t* conf, const char *const file)
935 char *confpath;
936 char cfg[PATH_MAX];
937 char *name;
939 if (strlen(file) > PATH_MAX - 14) {
940 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Filename is too long, can not load file or directory specific config files\n");
941 return;
943 sprintf (cfg, "%s.conf", file);
945 name = strrchr(cfg, '/');
946 if (HAVE_DOS_PATHS) {
947 char *tmp = strrchr(cfg, '\\');
948 if (!name || tmp > name)
949 name = tmp;
950 tmp = strrchr(cfg, ':');
951 if (!name || tmp > name)
952 name = tmp;
954 if (!name)
955 name = cfg;
956 else
957 name++;
959 if (use_filedir_conf) {
960 char dircfg[PATH_MAX];
961 strcpy(dircfg, cfg);
962 strcpy(dircfg + (name - cfg), "mplayer.conf");
963 try_load_config(conf, dircfg);
965 if (try_load_config(conf, cfg))
966 return;
969 if ((confpath = get_path (name)) != NULL)
971 try_load_config(conf, confpath);
973 free (confpath);
977 /* When libmpdemux performs a blocking operation (network connection or
978 * cache filling) if the operation fails we use this function to check
979 * if it was interrupted by the user.
980 * The function returns a new value for eof. */
981 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
983 mp_cmd_t* cmd;
984 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
985 switch(cmd->id) {
986 case MP_CMD_QUIT:
987 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
988 case MP_CMD_PLAY_TREE_STEP: {
989 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
990 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
991 } break;
992 case MP_CMD_PLAY_TREE_UP_STEP: {
993 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
994 } break;
995 case MP_CMD_PLAY_ALT_SRC_STEP: {
996 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
997 } break;
999 mp_cmd_free(cmd);
1001 return stop_play;
1004 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
1006 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
1008 play_tree_add_bpf(entry,mpctx->filename);
1011 if(!entry) {
1012 entry = mpctx->playtree_iter->tree;
1013 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1014 return PT_NEXT_ENTRY;
1016 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
1017 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1018 return PT_NEXT_ENTRY;
1021 play_tree_remove(entry,1,1);
1022 return PT_NEXT_SRC;
1024 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
1025 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1026 entry = mpctx->playtree_iter->tree;
1027 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1028 return PT_NEXT_ENTRY;
1030 play_tree_remove(entry,1,1);
1032 return PT_NEXT_SRC;
1035 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1037 sub_data *subd;
1038 #ifdef CONFIG_ASS
1039 ASS_Track *asst = 0;
1040 #endif
1042 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1043 return;
1046 subd = sub_read_file(filename, fps);
1047 #ifdef CONFIG_ASS
1048 if (ass_enabled)
1049 #ifdef CONFIG_ICONV
1050 asst = ass_read_file(ass_library, filename, sub_cp);
1051 #else
1052 asst = ass_read_file(ass_library, filename, 0);
1053 #endif
1054 if (ass_enabled && subd && !asst)
1055 asst = ass_read_subdata(ass_library, subd, fps);
1057 if (!asst && !subd)
1058 #else
1059 if(!subd)
1060 #endif
1061 mp_tmsg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, "Cannot load subtitles: %s\n",
1062 filename_recode(filename));
1064 #ifdef CONFIG_ASS
1065 if (!asst && !subd) return;
1066 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1067 #else
1068 if (!subd) return;
1069 #endif
1070 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1071 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1072 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1073 filename_recode(filename));
1074 ++mpctx->set_of_sub_size;
1075 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "SUB: Added subtitle file (%d): %s\n", mpctx->set_of_sub_size,
1076 filename_recode(filename));
1079 void init_vo_spudec(struct MPContext *mpctx)
1081 if (vo_spudec)
1082 spudec_free(vo_spudec);
1083 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1084 vo_spudec = NULL;
1085 if (spudec_ifo) {
1086 unsigned int palette[16], width, height;
1087 current_module="spudec_init_vobsub";
1088 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1089 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1092 #ifdef CONFIG_DVDREAD
1093 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1094 current_module="spudec_init_dvdread";
1095 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1096 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1097 NULL, 0);
1099 #endif
1101 #ifdef CONFIG_DVDNAV
1102 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1103 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1104 current_module="spudec_init_dvdnav";
1105 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1107 #endif
1109 if (vo_spudec==NULL) {
1110 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1111 current_module="spudec_init_normal";
1112 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1113 spudec_set_font_factor(vo_spudec,font_factor);
1116 if (vo_spudec!=NULL) {
1117 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1118 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1123 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1124 * make it all work is to use the builtin SDL-bootstrap code, which
1125 * will be done automatically by replacing our main() if we include SDL.h.
1127 #if defined(__APPLE__) && defined(CONFIG_SDL)
1128 #include <SDL.h>
1129 #endif
1132 * \brief append a formatted string
1133 * \param buf buffer to print into
1134 * \param pos position of terminating 0 in buf
1135 * \param len maximum number of characters in buf, not including terminating 0
1136 * \param format printf format string
1138 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1140 va_list va;
1141 va_start(va, format);
1142 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1143 va_end(va);
1144 if (*pos >= len ) {
1145 buf[len] = 0;
1146 *pos = len;
1151 * \brief append time in the hh:mm:ss.f format
1152 * \param buf buffer to print into
1153 * \param pos position of terminating 0 in buf
1154 * \param len maximum number of characters in buf, not including terminating 0
1155 * \param time time value to convert/append
1157 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1158 long tenths = 10 * time;
1159 int f1 = tenths % 10;
1160 int ss = (tenths / 10) % 60;
1161 int mm = (tenths / 600) % 60;
1162 int hh = tenths / 36000;
1163 if (time <= 0) {
1164 saddf(buf, pos, len, "unknown");
1165 return;
1167 if (hh > 0)
1168 saddf(buf, pos, len, "%2d:", hh);
1169 if (hh > 0 || mm > 0)
1170 saddf(buf, pos, len, "%02d:", mm);
1171 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1174 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1176 struct MPOpts *opts = &mpctx->opts;
1177 sh_video_t * const sh_video = mpctx->sh_video;
1179 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1180 a_pos = playing_audio_pts(mpctx);
1181 if (mpctx->sh_audio && sh_video && at_frame) {
1182 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1183 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1184 && !mpctx->drop_message_shown) {
1185 mp_tmsg(MSGT_AVSYNC,MSGL_WARN,SystemTooSlow);
1186 mpctx->drop_message_shown = true;
1189 if (quiet)
1190 return;
1193 int width;
1194 char *line;
1195 unsigned pos = 0;
1196 get_screen_size();
1197 if (screen_width > 0)
1198 width = screen_width;
1199 else
1200 width = 80;
1201 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1202 /* Windows command line is broken (MinGW's rxvt works, but we
1203 * should not depend on that). */
1204 width--;
1205 #endif
1206 line = malloc(width + 1); // one additional char for the terminating null
1208 // Audio time
1209 if (mpctx->sh_audio) {
1210 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1211 if (!sh_video) {
1212 float len = demuxer_get_time_length(mpctx->demuxer);
1213 saddf(line, &pos, width, "(");
1214 sadd_hhmmssf(line, &pos, width, a_pos);
1215 saddf(line, &pos, width, ") of %.1f (", len);
1216 sadd_hhmmssf(line, &pos, width, len);
1217 saddf(line, &pos, width, ") ");
1221 // Video time
1222 if (sh_video)
1223 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1225 // A-V sync
1226 if (mpctx->sh_audio && sh_video)
1227 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1228 mpctx->last_av_difference, mpctx->total_avsync_change);
1230 // Video stats
1231 if (sh_video)
1232 saddf(line, &pos, width, "%3d/%3d ",
1233 (int)sh_video->num_frames,
1234 (int)sh_video->num_frames_decoded);
1236 // CPU usage
1237 if (sh_video) {
1238 if (sh_video->timer > 0.5)
1239 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1240 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1241 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1242 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1243 else
1244 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1245 } else if (mpctx->sh_audio) {
1246 if (mpctx->delay > 0.5)
1247 saddf(line, &pos, width, "%4.1f%% ",
1248 100.0*audio_time_usage/(double)mpctx->delay);
1249 else
1250 saddf(line, &pos, width, "??,?%% ");
1253 // VO stats
1254 if (sh_video)
1255 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1257 #ifdef CONFIG_STREAM_CACHE
1258 // cache stats
1259 if (stream_cache_size > 0)
1260 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1261 #endif
1263 // other
1264 if (opts->playback_speed != 1)
1265 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1267 // end
1268 if (erase_to_end_of_line) {
1269 line[pos] = 0;
1270 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1271 } else {
1272 memset(&line[pos], ' ', width - pos);
1273 line[width] = 0;
1274 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1276 free(line);
1280 * \brief build a chain of audio filters that converts the input format
1281 * to the ao's format, taking into account the current playback_speed.
1282 * \param sh_audio describes the requested input format of the chain.
1283 * \param ao_data describes the requested output format of the chain.
1285 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1287 struct MPOpts *opts = &mpctx->opts;
1288 int new_srate;
1289 int result;
1290 if (!sh_audio)
1292 mpctx->mixer.afilter = NULL;
1293 return 0;
1295 if(af_control_any_rev(sh_audio->afilter,
1296 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1297 &opts->playback_speed)) {
1298 new_srate = sh_audio->samplerate;
1299 } else {
1300 new_srate = sh_audio->samplerate * opts->playback_speed;
1301 if (new_srate != ao_data->samplerate) {
1302 // limits are taken from libaf/af_resample.c
1303 if (new_srate < 8000)
1304 new_srate = 8000;
1305 if (new_srate > 192000)
1306 new_srate = 192000;
1307 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1310 result = init_audio_filters(sh_audio, new_srate,
1311 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1312 mpctx->mixer.afilter = sh_audio->afilter;
1313 return result;
1317 typedef struct mp_osd_msg mp_osd_msg_t;
1318 struct mp_osd_msg {
1319 /// Previous message on the stack.
1320 mp_osd_msg_t* prev;
1321 /// Message text.
1322 char msg[128];
1323 int id,level,started;
1324 /// Display duration in ms.
1325 unsigned time;
1328 /// OSD message stack.
1329 static mp_osd_msg_t* osd_msg_stack = NULL;
1332 * \brief Add a message on the OSD message stack
1334 * If a message with the same id is already present in the stack
1335 * it is pulled on top of the stack, otherwise a new message is created.
1339 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1340 mp_osd_msg_t *msg,*last=NULL;
1341 va_list va;
1342 int r;
1344 // look if the id is already in the stack
1345 for(msg = osd_msg_stack ; msg && msg->id != id ;
1346 last = msg, msg = msg->prev);
1347 // not found: alloc it
1348 if(!msg) {
1349 msg = calloc(1,sizeof(mp_osd_msg_t));
1350 msg->prev = osd_msg_stack;
1351 osd_msg_stack = msg;
1352 } else if(last) { // found, but it's not on top of the stack
1353 last->prev = msg->prev;
1354 msg->prev = osd_msg_stack;
1355 osd_msg_stack = msg;
1357 // write the msg
1358 va_start(va,fmt);
1359 r = vsnprintf(msg->msg, 128, fmt, va);
1360 va_end(va);
1361 if(r >= 128) msg->msg[127] = 0;
1362 // set id and time
1363 msg->id = id;
1364 msg->level = level;
1365 msg->time = time;
1370 * \brief Remove a message from the OSD stack
1372 * This function can be used to get rid of a message right away.
1376 void rm_osd_msg(int id) {
1377 mp_osd_msg_t *msg,*last=NULL;
1379 // Search for the msg
1380 for(msg = osd_msg_stack ; msg && msg->id != id ;
1381 last = msg, msg = msg->prev);
1382 if(!msg) return;
1384 // Detach it from the stack and free it
1385 if(last)
1386 last->prev = msg->prev;
1387 else
1388 osd_msg_stack = msg->prev;
1389 free(msg);
1393 * \brief Remove all messages from the OSD stack
1397 static void clear_osd_msgs(void) {
1398 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1399 while(msg) {
1400 prev = msg->prev;
1401 free(msg);
1402 msg = prev;
1404 osd_msg_stack = NULL;
1408 * \brief Get the current message from the OSD stack.
1410 * This function decrements the message timer and destroys the old ones.
1411 * The message that should be displayed is returned (if any).
1415 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1417 struct MPOpts *opts = &mpctx->opts;
1418 mp_osd_msg_t *msg,*prev,*last = NULL;
1419 static unsigned last_update = 0;
1420 unsigned now = GetTimerMS();
1421 unsigned diff;
1422 char hidden_dec_done = 0;
1424 if (mpctx->osd_visible) {
1425 // 36000000 means max timed visibility is 1 hour into the future, if
1426 // the difference is greater assume it's wrapped around from below 0
1427 if (mpctx->osd_visible - now > 36000000) {
1428 mpctx->osd_visible = 0;
1429 vo_osd_progbar_type = -1; // disable
1430 vo_osd_changed(OSDTYPE_PROGBAR);
1431 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1434 if (mpctx->osd_show_percentage_until - now > 36000000)
1435 mpctx->osd_show_percentage_until = 0;
1437 if(!last_update) last_update = now;
1438 diff = now >= last_update ? now - last_update : 0;
1440 last_update = now;
1442 // Look for the first message in the stack with high enough level.
1443 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1444 prev = msg->prev;
1445 if (msg->level > opts->osd_level && hidden_dec_done)
1446 continue;
1447 // The message has a high enough level or it is the first hidden one
1448 // in both cases we decrement the timer or kill it.
1449 if(!msg->started || msg->time > diff) {
1450 if(msg->started) msg->time -= diff;
1451 else msg->started = 1;
1452 // display it
1453 if (msg->level <= opts->osd_level)
1454 return msg;
1455 hidden_dec_done = 1;
1456 continue;
1458 // kill the message
1459 free(msg);
1460 if(last) {
1461 last->prev = prev;
1462 msg = last;
1463 } else {
1464 osd_msg_stack = prev;
1465 msg = NULL;
1468 // Nothing found
1469 return NULL;
1473 * \brief Display the OSD bar.
1475 * Display the OSD bar or fall back on a simple message.
1479 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1480 struct MPOpts *opts = &mpctx->opts;
1481 if (opts->osd_level < 1)
1482 return;
1484 if(mpctx->sh_video) {
1485 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1486 vo_osd_progbar_type = type;
1487 vo_osd_progbar_value = 256*(val-min)/(max-min);
1488 vo_osd_changed(OSDTYPE_PROGBAR);
1489 return;
1492 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1493 name, ROUND(100*(val-min)/(max-min)));
1497 * \brief Display text subtitles on the OSD
1499 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs)
1501 int i;
1502 vo_sub = subs;
1503 vo_osd_changed(OSDTYPE_SUBTITLE);
1504 if (!mpctx->sh_video) {
1505 // reverse order, since newest set_osd_msg is displayed first
1506 for (i = SUB_MAX_TEXT - 1; i >= 0; i--) {
1507 if (!subs || i >= subs->lines || !subs->text[i])
1508 rm_osd_msg(OSD_MSG_SUB_BASE + i);
1509 else {
1510 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1511 int display_time = i == subs->lines - 1 ? 180000 : 2000;
1512 set_osd_msg(OSD_MSG_SUB_BASE + i, 1, display_time, "%s", subs->text[i]);
1519 * \brief Update the OSD message line.
1521 * This function displays the current message on the vo OSD or on the term.
1522 * If the stack is empty and the OSD level is high enough the timer
1523 * is displayed (only on the vo OSD).
1527 static void update_osd_msg(struct MPContext *mpctx)
1529 struct MPOpts *opts = &mpctx->opts;
1530 mp_osd_msg_t *msg;
1531 struct osd_state *osd = mpctx->osd;
1532 char osd_text_timer[128];
1534 if (mpctx->add_osd_seek_info) {
1535 double percentage;
1536 if (mpctx->timeline && mpctx->sh_video)
1537 percentage = mpctx->sh_video->pts * 100 /
1538 mpctx->timeline[mpctx->num_timeline_parts].start;
1539 else
1540 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1541 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1542 if (mpctx->sh_video)
1543 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1544 mpctx->add_osd_seek_info = false;
1547 // Look if we have a msg
1548 if((msg = get_osd_msg(mpctx))) {
1549 if (strcmp(osd->osd_text, msg->msg)) {
1550 strncpy(osd->osd_text, msg->msg, 127);
1551 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1552 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1554 return;
1557 if(mpctx->sh_video) {
1558 // fallback on the timer
1559 if (opts->osd_level >= 2) {
1560 int len;
1561 if (mpctx->timeline)
1562 len = mpctx->timeline[mpctx->num_timeline_parts].start;
1563 else
1564 len = demuxer_get_time_length(mpctx->demuxer);
1565 int percentage = -1;
1566 char percentage_text[10];
1567 int pts = demuxer_get_current_time(mpctx->demuxer);
1569 if (mpctx->osd_show_percentage_until)
1570 if (mpctx->timeline)
1571 percentage = mpctx->sh_video->pts * 100 /
1572 mpctx->timeline[mpctx->num_timeline_parts].start;
1573 else
1574 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1576 if (percentage >= 0)
1577 snprintf(percentage_text, 9, " (%d%%)", percentage);
1578 else
1579 percentage_text[0] = 0;
1581 if (opts->osd_level == 3)
1582 snprintf(osd_text_timer, 63,
1583 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1584 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1585 len/3600,(len/60)%60,len%60,percentage_text);
1586 else
1587 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1588 mpctx->osd_function,pts/3600,(pts/60)%60,
1589 pts%60,percentage_text);
1590 } else
1591 osd_text_timer[0]=0;
1593 if (strcmp(osd->osd_text, osd_text_timer)) {
1594 strncpy(osd->osd_text, osd_text_timer, 63);
1595 vo_osd_changed(OSDTYPE_OSD);
1597 return;
1600 // Clear the term osd line
1601 if (term_osd && osd->osd_text[0]) {
1602 osd->osd_text[0] = 0;
1603 printf("%s\n",term_osd_esc);
1607 ///@}
1608 // OSDMsgStack
1611 void reinit_audio_chain(struct MPContext *mpctx)
1613 struct MPOpts *opts = &mpctx->opts;
1614 if(mpctx->sh_audio){
1615 current_module="init_audio_codec";
1616 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1617 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1618 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1619 mpctx->d_audio->id = -2;
1620 return;
1621 } else
1622 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1623 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1626 //const ao_info_t *info=audio_out->info;
1627 current_module="af_preinit";
1628 ao_data.samplerate=force_srate;
1629 ao_data.channels=0;
1630 ao_data.format=audio_output_format;
1631 #if 1
1632 // first init to detect best values
1633 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1634 // input:
1635 mpctx->sh_audio->samplerate,
1636 // output:
1637 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1638 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Error at audio filter chain pre-init!\n");
1639 exit_player(mpctx, EXIT_ERROR);
1641 #endif
1642 current_module="ao2_init";
1643 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1644 0, // plugin flag
1645 ao_data.samplerate,
1646 ao_data.channels,
1647 ao_data.format,0))){
1648 // FAILED:
1649 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Could not open/initialize audio device -> no sound.\n");
1650 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1651 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1652 mpctx->d_audio->id = -2;
1653 return;
1654 } else {
1655 // SUCCESS:
1656 mpctx->initialized_flags|=INITIALIZED_AO;
1657 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1658 mpctx->audio_out->info->short_name,
1659 ao_data.samplerate, ao_data.channels,
1660 af_fmt2str_short(ao_data.format),
1661 af_fmt2bits(ao_data.format)/8 );
1662 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1663 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1664 if(strlen(mpctx->audio_out->info->comment) > 0)
1665 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1666 // init audio filters:
1667 #if 1
1668 current_module="af_init";
1669 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1670 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter/ao format!\n");
1671 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1672 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1673 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1675 #endif
1677 mpctx->mixer.audio_out = mpctx->audio_out;
1678 mpctx->mixer.volstep = volstep;
1683 ///@}
1684 // Command2Property
1687 // Return pts value corresponding to the end point of audio written to the
1688 // ao so far.
1689 static double written_audio_pts(struct MPContext *mpctx)
1691 sh_audio_t *sh_audio = mpctx->sh_audio;
1692 demux_stream_t *d_audio = mpctx->d_audio;
1693 double buffered_output;
1694 // first calculate the end pts of audio that has been output by decoder
1695 double a_pts = sh_audio->pts;
1696 if (a_pts != MP_NOPTS_VALUE)
1697 // Good, decoder supports new way of calculating audio pts.
1698 // sh_audio->pts is the timestamp of the latest input packet with
1699 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1700 // the amount of bytes the decoder has written after that timestamp.
1701 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1702 else {
1703 // Decoder doesn't support new way of calculating pts (or we're
1704 // being called before it has decoded anything with known timestamp).
1705 // Use the old method of audio pts calculation: take the timestamp
1706 // of last packet with known pts the decoder has read data from,
1707 // and add amount of bytes read after the beginning of that packet
1708 // divided by input bps. This will be inaccurate if the input/output
1709 // ratio is not constant for every audio packet or if it is constant
1710 // but not accurately known in sh_audio->i_bps.
1712 a_pts = d_audio->pts;
1713 // ds_tell_pts returns bytes read after last timestamp from
1714 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1715 // it has read but not decoded
1716 if (sh_audio->i_bps)
1717 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1718 (double)sh_audio->i_bps;
1720 // Now a_pts hopefully holds the pts for end of audio from decoder.
1721 // Substract data in buffers between decoder and audio out.
1723 // Decoded but not filtered
1724 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1726 // Data buffered in audio filters, measured in bytes of "missing" output
1727 buffered_output = af_calc_delay(sh_audio->afilter);
1729 // Data that was ready for ao but was buffered because ao didn't fully
1730 // accept everything to internal buffers yet
1731 buffered_output += sh_audio->a_out_buffer_len;
1733 // Filters divide audio length by playback_speed, so multiply by it
1734 // to get the length in original units without speedup or slowdown
1735 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1737 return a_pts + mpctx->video_offset;
1740 // Return pts value corresponding to currently playing audio.
1741 double playing_audio_pts(struct MPContext *mpctx)
1743 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1744 mpctx->audio_out->get_delay();
1747 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1748 struct MPOpts *opts = &mpctx->opts;
1749 // check for frame-drop:
1750 current_module = "check_framedrop";
1751 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1752 static int dropped_frames;
1753 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1754 float d = delay-mpctx->delay;
1755 ++total_frame_cnt;
1756 // we should avoid dropping too many frames in sequence unless we
1757 // are too late. and we allow 100ms A-V delay here:
1758 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1759 && !mpctx->update_video_immediately) {
1760 ++drop_frame_cnt;
1761 ++dropped_frames;
1762 return frame_dropping;
1763 } else
1764 dropped_frames = 0;
1766 return 0;
1770 #ifdef HAVE_RTC
1771 int rtc_fd = -1;
1772 #endif
1774 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1776 #ifdef HAVE_RTC
1777 if (rtc_fd >= 0){
1778 // -------- RTC -----------
1779 current_module="sleep_rtc";
1780 while (time_frame > 0.000) {
1781 unsigned long rtc_ts;
1782 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1783 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC read error: %s\n", strerror(errno));
1784 time_frame -= get_relative_time(mpctx);
1786 } else
1787 #endif
1789 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1790 // unnecessarily high CPU usage
1791 float margin = softsleep ? 0.011 : 0;
1792 current_module = "sleep_timer";
1793 while (time_frame > margin) {
1794 usec_sleep(1000000 * (time_frame - margin));
1795 time_frame -= get_relative_time(mpctx);
1797 if (softsleep){
1798 current_module = "sleep_soft";
1799 if (time_frame < 0)
1800 mp_tmsg(MSGT_AVSYNC, MSGL_WARN, "Warning! Softsleep underflow!\n");
1801 while (time_frame > 0)
1802 time_frame -= get_relative_time(mpctx); // burn the CPU
1805 return time_frame;
1808 #ifdef CONFIG_DVDNAV
1809 #ifndef FF_B_TYPE
1810 #define FF_B_TYPE 3
1811 #endif
1812 /// store decoded video image
1813 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1814 mp_image_t *from_mpi) {
1815 mp_image_t *mpi;
1817 /// Do not store B-frames
1818 if (from_mpi->pict_type == FF_B_TYPE)
1819 return to_mpi;
1821 if (to_mpi &&
1822 to_mpi->w == from_mpi->w &&
1823 to_mpi->h == from_mpi->h &&
1824 to_mpi->imgfmt == from_mpi->imgfmt)
1825 mpi = to_mpi;
1826 else {
1827 if (to_mpi)
1828 free_mp_image(to_mpi);
1829 if (from_mpi->w == 0 || from_mpi->h == 0)
1830 return NULL;
1831 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1834 copy_mpi(mpi,from_mpi);
1835 return mpi;
1838 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1839 struct MPOpts *opts = &ctx->opts;
1840 if (ctx->sh_video) {
1841 /// clear video pts
1842 ctx->d_video->pts = 0.0f;
1843 ctx->sh_video->pts = 0.0f;
1844 ctx->sh_video->i_pts = 0.0f;
1845 ctx->sh_video->last_pts = 0.0f;
1846 ctx->sh_video->num_buffered_pts = 0;
1847 ctx->sh_video->num_frames = 0;
1848 ctx->sh_video->num_frames_decoded = 0;
1849 ctx->sh_video->timer = 0.0f;
1850 ctx->sh_video->stream_delay = 0.0f;
1851 ctx->sh_video->timer = 0;
1852 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1855 if (ctx->sh_audio) {
1856 /// free audio packets and reset
1857 ds_free_packs(ctx->d_audio);
1858 audio_delay -= ctx->sh_audio->stream_delay;
1859 ctx->delay =- audio_delay;
1860 ctx->audio_out->reset();
1861 resync_audio_stream(ctx->sh_audio);
1864 if (ctx->d_sub) opts->sub_id = -2;
1866 audio_delay = 0.0f;
1868 /// clear all EOF related flags
1869 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1872 /// Restore last decoded DVDNAV (still frame)
1873 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1874 int *in_size,
1875 unsigned char **start,
1876 mp_image_t *decoded_frame)
1878 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1879 return decoded_frame;
1881 /// a change occured in dvdnav stream
1882 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1883 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1884 mp_dvdnav_context_free(mpctx);
1885 mp_dvdnav_reset_stream(mpctx);
1886 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1887 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1890 if (*in_size < 0) {
1891 float len;
1893 /// Display still frame, if any
1894 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1895 decoded_frame = mpctx->nav_smpi;
1897 /// increment video frame : continue playing after still frame
1898 len = demuxer_get_time_length(mpctx->demuxer);
1899 if (mpctx->sh_video->pts >= len &&
1900 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1901 mp_dvdnav_skip_still(mpctx->stream);
1902 mp_dvdnav_skip_wait(mpctx->stream);
1904 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1906 if (mpctx->nav_buffer) {
1907 *start = mpctx->nav_start;
1908 *in_size = mpctx->nav_in_size;
1909 if (mpctx->nav_start)
1910 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1914 return decoded_frame;
1917 /// Save last decoded DVDNAV (still frame)
1918 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1919 unsigned char *start,
1920 mp_image_t *decoded_frame)
1922 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1923 return;
1925 if (mpctx->nav_buffer)
1926 free(mpctx->nav_buffer);
1928 mpctx->nav_buffer = malloc(in_size);
1929 mpctx->nav_start = start;
1930 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1931 if (mpctx->nav_buffer)
1932 memcpy(mpctx->nav_buffer,start,in_size);
1934 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1935 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1937 #endif /* CONFIG_DVDNAV */
1939 /* Modify video timing to match the audio timeline. There are two main
1940 * reasons this is needed. First, video and audio can start from different
1941 * positions at beginning of file or after a seek (MPlayer starts both
1942 * immediately even if they have different pts). Second, the file can have
1943 * audio timestamps that are inconsistent with the duration of the audio
1944 * packets, for example two consecutive timestamp values differing by
1945 * one second but only a packet with enough samples for half a second
1946 * of playback between them.
1948 static void adjust_sync(struct MPContext *mpctx, double frame_time)
1950 struct MPOpts *opts = &mpctx->opts;
1951 current_module = "av_sync";
1953 if (!mpctx->sh_audio)
1954 return;
1956 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
1957 double v_pts = mpctx->sh_video->pts;
1958 double av_delay = a_pts - v_pts;
1959 // Try to sync vo_flip() so it will *finish* at given time
1960 av_delay += mpctx->last_vo_flip_duration;
1961 av_delay -= audio_delay; // This much pts difference is desired
1963 double change = av_delay * 0.1;
1964 double max_change = default_max_pts_correction >= 0 ?
1965 default_max_pts_correction : frame_time * 0.1;
1966 if (change < -max_change)
1967 change = -max_change;
1968 else if (change > max_change)
1969 change = max_change;
1970 mpctx->delay += change;
1971 mpctx->total_avsync_change += change;
1974 static int fill_audio_out_buffers(struct MPContext *mpctx)
1976 struct MPOpts *opts = &mpctx->opts;
1977 unsigned int t;
1978 double tt;
1979 int playsize;
1980 int playflags=0;
1981 int audio_eof=0;
1982 int bytes_to_write;
1983 sh_audio_t * const sh_audio = mpctx->sh_audio;
1985 current_module="play_audio";
1987 while (1) {
1988 int sleep_time;
1989 // all the current uses of ao_data.pts seem to be in aos that handle
1990 // sync completely wrong; there should be no need to use ao_data.pts
1991 // in get_space()
1992 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
1993 bytes_to_write = mpctx->audio_out->get_space();
1994 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
1995 break;
1997 // handle audio-only case:
1998 // this is where mplayer sleeps during audio-only playback
1999 // to avoid 100% CPU use
2000 sleep_time = (ao_data.outburst - bytes_to_write) * 1000 / ao_data.bps;
2001 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
2002 usec_sleep(sleep_time * 1000);
2005 while (bytes_to_write) {
2006 playsize = bytes_to_write;
2007 if (playsize > MAX_OUTBURST)
2008 playsize = MAX_OUTBURST;
2009 bytes_to_write -= playsize;
2011 // Fill buffer if needed:
2012 current_module="decode_audio";
2013 t = GetTimer();
2014 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2015 if (mpctx->d_audio->eof) {
2016 audio_eof = 1;
2017 if (sh_audio->a_out_buffer_len == 0)
2018 return 0;
2020 t = GetTimer() - t;
2021 tt = t*0.000001f; audio_time_usage+=tt;
2022 if (playsize > sh_audio->a_out_buffer_len) {
2023 playsize = sh_audio->a_out_buffer_len;
2024 if (audio_eof)
2025 playflags |= AOPLAY_FINAL_CHUNK;
2027 if (!playsize)
2028 break;
2030 // play audio:
2031 current_module="play_audio";
2033 // Is this pts value actually useful for the aos that access it?
2034 // They're obviously badly broken in the way they handle av sync;
2035 // would not having access to this make them more broken?
2036 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2037 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2039 if (playsize > 0) {
2040 sh_audio->a_out_buffer_len -= playsize;
2041 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2042 sh_audio->a_out_buffer_len);
2043 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2045 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2046 // Sanity check to avoid hanging in case current ao doesn't output
2047 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2048 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2049 sh_audio->a_out_buffer_len = 0;
2052 return 1;
2055 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2056 float *aq_sleep_time)
2058 struct MPOpts *opts = &mpctx->opts;
2059 int frame_time_remaining = 0;
2060 current_module="calc_sleep_time";
2062 *time_frame -= get_relative_time(mpctx); // reset timer
2064 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2065 float delay = mpctx->audio_out->get_delay();
2066 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2068 if (autosync) {
2070 * Adjust this raw delay value by calculating the expected
2071 * delay for this frame and generating a new value which is
2072 * weighted between the two. The higher autosync is, the
2073 * closer to the delay value gets to that which "-nosound"
2074 * would have used, and the longer it will take for A/V
2075 * sync to settle at the right value (but it eventually will.)
2076 * This settling time is very short for values below 100.
2078 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2079 float difference = delay - predicted;
2080 delay = predicted + difference / (float)autosync;
2083 *time_frame = delay - mpctx->delay / opts->playback_speed;
2085 // delay = amount of audio buffered in soundcard/driver
2086 if (delay > 0.25) delay=0.25; else
2087 if (delay < 0.10) delay=0.10;
2088 if (*time_frame > delay*0.6) {
2089 // sleep time too big - may cause audio drops (buffer underrun)
2090 frame_time_remaining = 1;
2091 *time_frame = delay*0.5;
2093 } else {
2094 // If we're lagging more than 200 ms behind the right playback rate,
2095 // don't try to "catch up".
2096 // If benchmark is set always output frames as fast as possible
2097 // without sleeping.
2098 if (*time_frame < -0.2 || benchmark)
2099 *time_frame = 0;
2102 *aq_sleep_time += *time_frame;
2105 //============================== SLEEP: ===================================
2107 // flag 256 means: libvo driver does its timing (dvb card)
2108 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2109 *time_frame = timing_sleep(mpctx, *time_frame);
2110 return frame_time_remaining;
2113 int reinit_video_chain(struct MPContext *mpctx)
2115 struct MPOpts *opts = &mpctx->opts;
2116 sh_video_t * const sh_video = mpctx->sh_video;
2117 double ar=-1.0;
2118 //================== Init VIDEO (codec & libvo) ==========================
2119 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2120 current_module="preinit_libvo";
2122 //shouldn't we set dvideo->id=-2 when we fail?
2123 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2124 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2125 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Error opening/initializing the selected video_out (-vo) device.\n");
2126 goto err_out;
2128 mpctx->initialized_flags|=INITIALIZED_VO;
2131 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2132 mpctx->sh_video->stream_aspect = ar;
2133 current_module="init_video_filters";
2135 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2136 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2138 #ifdef CONFIG_MENU
2139 if(use_menu) {
2140 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2141 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2142 if(!vf_menu) {
2143 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Can't open libmenu video filter with root menu %s.\n",menu_root);
2144 use_menu = 0;
2147 if(vf_menu)
2148 sh_video->vfilter = vf_menu;
2149 #endif
2151 #ifdef CONFIG_ASS
2152 if(ass_enabled) {
2153 int i;
2154 int insert = 1;
2155 if (opts->vf_settings)
2156 for (i = 0; opts->vf_settings[i].name; ++i)
2157 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2158 insert = 0;
2159 break;
2161 if (insert) {
2162 extern vf_info_t vf_info_ass;
2163 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2164 char* vf_arg[] = {"auto", "1", NULL};
2165 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2166 if (vf_ass)
2167 sh_video->vfilter = vf_ass;
2168 else
2169 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2172 #endif
2174 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2176 #ifdef CONFIG_ASS
2177 if (ass_enabled)
2178 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2179 #endif
2181 current_module="init_video_codec";
2183 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2184 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2185 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2187 if(!sh_video->initialized){
2188 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2189 goto err_out;
2192 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2194 if (sh_video->codec)
2195 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2197 sh_video->last_pts = MP_NOPTS_VALUE;
2198 sh_video->num_buffered_pts = 0;
2199 sh_video->next_frame_time = 0;
2201 if(auto_quality>0){
2202 // Auto quality option enabled
2203 output_quality=get_video_quality_max(sh_video);
2204 if(auto_quality>output_quality) auto_quality=output_quality;
2205 else output_quality=auto_quality;
2206 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2207 set_video_quality(sh_video,output_quality);
2210 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2212 current_module="init_vo";
2214 return 1;
2216 err_out:
2217 mpctx->sh_video = mpctx->d_video->sh = NULL;
2218 return 0;
2221 static double update_video_nocorrect_pts(struct MPContext *mpctx,
2222 int *blit_frame)
2224 struct sh_video *sh_video = mpctx->sh_video;
2225 *blit_frame = 0;
2226 double frame_time = 0;
2227 while (1) {
2228 current_module = "filter_video";
2229 // In nocorrect-pts mode there is no way to properly time these frames
2230 if (vf_output_queued_frame(sh_video->vfilter))
2231 break;
2232 unsigned char *packet = NULL;
2233 frame_time = sh_video->next_frame_time;
2234 if (mpctx->update_video_immediately)
2235 frame_time = 0;
2236 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2237 &packet, force_fps);
2238 if (in_size < 0) {
2239 #ifdef CONFIG_DVDNAV
2240 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2241 if (mp_dvdnav_is_eof(mpctx->stream))
2242 return -1;
2243 if (mpctx->d_video)
2244 mpctx->d_video->eof = 0;
2245 if (mpctx->d_audio)
2246 mpctx->d_audio->eof = 0;
2247 mpctx->stream->eof = 0;
2248 } else
2249 #endif
2250 return -1;
2252 if (in_size > max_framesize)
2253 max_framesize = in_size;
2254 sh_video->timer += frame_time;
2255 if (mpctx->sh_audio)
2256 mpctx->delay -= frame_time;
2257 // video_read_frame can change fps (e.g. for ASF video)
2258 vo_fps = sh_video->fps;
2259 int framedrop_type = check_framedrop(mpctx, frame_time);
2260 current_module = "decode video";
2262 void *decoded_frame;
2263 #ifdef CONFIG_DVDNAV
2264 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2265 if (in_size >= 0 && !decoded_frame)
2266 #endif
2267 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2268 sh_video->pts);
2269 #ifdef CONFIG_DVDNAV
2270 // Save last still frame for future display
2271 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2272 #endif
2273 if (decoded_frame) {
2274 // These updates are done here for vf_expand OSD/subtitles
2275 update_subtitles(mpctx, &mpctx->opts, sh_video, sh_video->pts,
2276 mpctx->video_offset, mpctx->d_sub, 0);
2277 update_teletext(sh_video, mpctx->demuxer, 0);
2278 update_osd_msg(mpctx);
2279 current_module = "filter video";
2280 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2281 break;
2284 *blit_frame = 1;
2285 return frame_time;
2288 static double update_video(struct MPContext *mpctx, int *blit_frame)
2290 struct sh_video *sh_video = mpctx->sh_video;
2291 struct vo *video_out = mpctx->video_out;
2292 *blit_frame = 0;
2293 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2294 mpctx->osd); // hack for vf_expand
2295 if (!mpctx->opts.correct_pts)
2296 return update_video_nocorrect_pts(mpctx, blit_frame);
2298 double pts;
2300 bool hit_eof = false;
2301 while (!video_out->frame_loaded) {
2302 current_module = "filter_video";
2303 if (vo_get_buffered_frame(video_out, hit_eof) >= 0)
2304 break;
2305 if (hit_eof)
2306 return -1;
2307 // XXX Time used in this call is not counted in any performance
2308 // timer now, OSD time is not updated correctly for filter-added frames
2309 if (vf_output_queued_frame(sh_video->vfilter))
2310 break;
2311 unsigned char *packet = NULL;
2312 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2313 if (pts != MP_NOPTS_VALUE)
2314 pts += mpctx->video_offset;
2315 if (in_size < 0) {
2316 // try to extract last frames in case of decoder lag
2317 in_size = 0;
2318 pts = 1e300;
2319 hit_eof = true;
2321 if (in_size > max_framesize)
2322 max_framesize = in_size;
2323 current_module = "decode video";
2324 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2325 void *decoded_frame = decode_video(sh_video, packet, in_size,
2326 framedrop_type, pts);
2327 if (decoded_frame) {
2328 // These updates are done here for vf_expand OSD/subtitles
2329 update_subtitles(mpctx, &mpctx->opts, sh_video, sh_video->pts,
2330 mpctx->video_offset, mpctx->d_sub, 0);
2331 update_teletext(sh_video, mpctx->demuxer, 0);
2332 update_osd_msg(mpctx);
2333 current_module = "filter video";
2334 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2335 if (!video_out->config_ok)
2336 break; // We'd likely hang in this loop otherwise
2340 pts = video_out->next_pts;
2341 if (pts == MP_NOPTS_VALUE) {
2342 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2343 // Try to use decoder pts from before filters
2344 pts = sh_video->pts;
2345 if (pts == MP_NOPTS_VALUE)
2346 pts = sh_video->last_pts;
2348 sh_video->pts = pts;
2349 if (sh_video->last_pts == MP_NOPTS_VALUE)
2350 sh_video->last_pts = sh_video->pts;
2351 else if (sh_video->last_pts > sh_video->pts) {
2352 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2353 sh_video->pts, sh_video->last_pts);
2354 /* If the difference in pts is small treat it as jitter around the
2355 * right value (possibly caused by incorrect timestamp ordering) and
2356 * just show this frame immediately after the last one.
2357 * Treat bigger differences as timestamp resets and start counting
2358 * timing of later frames from the position of this one. */
2359 if (sh_video->last_pts - sh_video->pts > 0.5)
2360 sh_video->last_pts = sh_video->pts;
2361 else
2362 sh_video->pts = sh_video->last_pts;
2364 double frame_time = sh_video->pts - sh_video->last_pts;
2365 sh_video->last_pts = sh_video->pts;
2366 sh_video->timer += frame_time;
2367 if (mpctx->sh_audio)
2368 mpctx->delay -= frame_time;
2369 *blit_frame = 1;
2370 return frame_time;
2373 void pause_player(struct MPContext *mpctx)
2375 if (mpctx->paused)
2376 return;
2377 mpctx->paused = 1;
2378 mpctx->step_frames = 0;
2379 mpctx->time_frame -= get_relative_time(mpctx);
2381 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2382 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2384 if (mpctx->audio_out && mpctx->sh_audio)
2385 mpctx->audio_out->pause(); // pause audio, keep data if possible
2388 void unpause_player(struct MPContext *mpctx)
2390 if (!mpctx->paused)
2391 return;
2392 mpctx->paused = 0;
2394 if (mpctx->audio_out && mpctx->sh_audio)
2395 mpctx->audio_out->resume(); // resume audio
2396 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2397 && !mpctx->step_frames)
2398 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2399 (void)get_relative_time(mpctx); // ignore time that passed during pause
2402 void add_step_frame(struct MPContext *mpctx)
2404 mpctx->step_frames++;
2405 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2406 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2407 unpause_player(mpctx);
2410 static void pause_loop(struct MPContext *mpctx)
2412 mp_cmd_t* cmd;
2413 if (!quiet) {
2414 // Small hack to display the pause message on the OSD line.
2415 // The pause string is: "\n == PAUSE == \r" so we need to
2416 // take the first and the last char out
2417 if (term_osd && !mpctx->sh_video) {
2418 char msg[128] = _("\n ===== PAUSE =====\r");
2419 int mlen = strlen(msg);
2420 msg[mlen-1] = '\0';
2421 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2422 update_osd_msg(mpctx);
2423 } else
2424 mp_tmsg(MSGT_CPLAYER,MSGL_STATUS,"\n ===== PAUSE =====\r");
2425 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2428 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2429 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2430 if (cmd) {
2431 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2432 run_command(mpctx, cmd);
2433 mp_cmd_free(cmd);
2434 continue;
2436 if (mpctx->sh_video && mpctx->video_out)
2437 vo_check_events(mpctx->video_out);
2438 #ifdef CONFIG_MENU
2439 if (vf_menu)
2440 vf_menu_pause_update(vf_menu);
2441 #endif
2442 usec_sleep(20000);
2443 update_osd_msg(mpctx);
2444 int hack = vo_osd_changed(0);
2445 vo_osd_changed(hack);
2446 if (hack)
2447 break;
2452 // Find the right mute status and record position for new file position
2453 static void edl_seek_reset(MPContext *mpctx)
2455 mpctx->edl_muted = 0;
2456 next_edl_record = edl_records;
2458 while (next_edl_record) {
2459 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2460 break;
2462 if (next_edl_record->action == EDL_MUTE)
2463 mpctx->edl_muted = !mpctx->edl_muted;
2464 next_edl_record = next_edl_record->next;
2466 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2467 mixer_mute(&mpctx->mixer);
2471 // Execute EDL command for the current position if one exists
2472 static void edl_update(MPContext *mpctx)
2474 if (!next_edl_record)
2475 return;
2477 if (!mpctx->sh_video) {
2478 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot use EDL without video, disabling.\n");
2479 free_edl(edl_records);
2480 next_edl_record = NULL;
2481 edl_records = NULL;
2482 return;
2485 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2486 if (next_edl_record->action == EDL_SKIP) {
2487 mpctx->osd_function = OSD_FFW;
2488 mpctx->abs_seek_pos = 0;
2489 mpctx->rel_seek_secs = next_edl_record->length_sec;
2490 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2491 "[%f], length [%f]\n", next_edl_record->start_sec,
2492 next_edl_record->stop_sec, next_edl_record->length_sec);
2494 else if (next_edl_record->action == EDL_MUTE) {
2495 mpctx->edl_muted = !mpctx->edl_muted;
2496 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2497 mixer_mute(&mpctx->mixer);
2498 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2499 next_edl_record->start_sec );
2501 next_edl_record = next_edl_record->next;
2505 static void reinit_decoders(struct MPContext *mpctx)
2507 reinit_video_chain(mpctx);
2508 reinit_audio_chain(mpctx);
2509 mp_property_do("sub", M_PROPERTY_SET, &mpctx->global_sub_pos, mpctx);
2512 static bool timeline_set_part(struct MPContext *mpctx, int i)
2514 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
2515 struct timeline_part *n = mpctx->timeline + i;
2516 mpctx->timeline_part = i;
2517 mpctx->video_offset = n->start - n->source_start;
2518 if (n->source == p->source)
2519 return false;
2520 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo && mpctx->opts.video_id != -2 ? 0 : INITIALIZED_VO) | INITIALIZED_AO | INITIALIZED_ACODEC);
2521 mpctx->demuxer = n->source->demuxer;
2522 mpctx->d_video = mpctx->demuxer->video;
2523 mpctx->d_audio = mpctx->demuxer->audio;
2524 mpctx->d_sub = mpctx->demuxer->sub;
2525 mpctx->sh_video = mpctx->d_video->sh;
2526 mpctx->sh_audio = mpctx->d_audio->sh;
2527 return true;
2530 // Given pts, switch playback to the corresponding part.
2531 // Return offset within that part.
2532 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
2533 bool *need_reset)
2535 if (pts < 0)
2536 pts = 0;
2537 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
2538 struct timeline_part *p = mpctx->timeline + i;
2539 if (pts < (p+1)->start) {
2540 *need_reset = timeline_set_part(mpctx, i);
2541 return pts - p->start + p->source_start;
2544 return -1;
2548 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2549 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2550 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2551 static int seek(MPContext *mpctx, double amount, int style)
2553 current_module = "seek";
2554 if (mpctx->stop_play == AT_END_OF_FILE)
2555 mpctx->stop_play = KEEP_PLAYING;
2556 if (mpctx->timeline && style & SEEK_FACTOR) {
2557 amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
2558 style &= ~SEEK_FACTOR;
2560 if ((mpctx->demuxer->accurate_seek || mpctx->timeline) && mpctx->sh_video
2561 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2562 style |= SEEK_ABSOLUTE;
2563 if (amount > 0)
2564 style |= SEEK_FORWARD;
2565 else
2566 style |= SEEK_BACKWARD;
2567 amount += mpctx->sh_video->pts;
2570 /* At least the liba52 decoder wants to read from the input stream
2571 * during initialization, so reinit must be done after the demux_seek()
2572 * call that clears possible stream EOF. */
2573 bool need_reset = false;
2574 if (mpctx->timeline) {
2575 amount = timeline_set_from_time(mpctx, amount, &need_reset);
2576 if (amount == -1) {
2577 mpctx->stop_play = AT_END_OF_FILE;
2578 // Clear audio from current position
2579 if (mpctx->sh_audio) {
2580 mpctx->audio_out->reset();
2581 mpctx->sh_audio->a_buffer_len = 0;
2582 mpctx->sh_audio->a_out_buffer_len = 0;
2584 return -1;
2587 int seekresult = demux_seek(mpctx->demuxer, amount, audio_delay, style);
2588 if (need_reset)
2589 reinit_decoders(mpctx);
2590 if (seekresult == 0)
2591 return -1;
2593 if (mpctx->sh_video) {
2594 current_module = "seek_video_reset";
2595 resync_video_stream(mpctx->sh_video);
2596 vo_seek_reset(mpctx->video_out);
2597 mpctx->sh_video->num_buffered_pts = 0;
2598 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2599 mpctx->delay = 0;
2600 mpctx->time_frame = 0;
2601 mpctx->update_video_immediately = true;
2602 // Not all demuxers set d_video->pts during seek, so this value
2603 // (which is used by at least vobsub and edl code below) may
2604 // be completely wrong (probably 0).
2605 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
2606 update_subtitles(mpctx, &mpctx->opts, mpctx->sh_video,
2607 mpctx->sh_video->pts, mpctx->video_offset,
2608 mpctx->d_sub, 1);
2609 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2612 if (mpctx->sh_audio) {
2613 current_module = "seek_audio_reset";
2614 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2615 mpctx->sh_audio->a_buffer_len = 0;
2616 mpctx->sh_audio->a_out_buffer_len = 0;
2617 if (!mpctx->sh_video)
2618 update_subtitles(mpctx, &mpctx->opts, NULL, mpctx->sh_audio->pts,
2619 mpctx->video_offset, mpctx->d_sub, 1);
2622 if (vo_vobsub && mpctx->sh_video) {
2623 current_module = "seek_vobsub_reset";
2624 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2627 edl_seek_reset(mpctx);
2629 mpctx->total_avsync_change = 0;
2630 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2631 drop_frame_cnt = 0;
2633 current_module = NULL;
2634 return 0;
2637 int get_current_chapter(struct MPContext *mpctx)
2639 if (!mpctx->chapters || !mpctx->sh_video)
2640 return demuxer_get_current_chapter(mpctx->demuxer);
2642 int i;
2643 double current_pts = mpctx->sh_video->pts;
2644 for (i = 1; i < mpctx->num_chapters; i++)
2645 if (current_pts < mpctx->chapters[i].start)
2646 break;
2647 return i - 1;
2650 // currently returns a string allocated with malloc, not talloc
2651 char *chapter_display_name(struct MPContext *mpctx, int chapter)
2653 if (!mpctx->chapters || !mpctx->sh_video)
2654 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
2655 return strdup(mpctx->chapters[chapter].name);
2658 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
2659 char **chapter_name)
2661 if (!mpctx->chapters || !mpctx->sh_video)
2662 return demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
2663 chapter_name);
2664 if (chapter >= mpctx->num_chapters)
2665 return -1;
2666 if (chapter < 0)
2667 chapter = 0;
2668 *seek_pts = mpctx->chapters[chapter].start;
2669 if (chapter_name)
2670 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
2671 return chapter;
2674 static int find_ordered_chapter_sources(struct MPContext *mpctx,
2675 struct content_source *sources,
2676 int num_sources,
2677 unsigned char uid_map[][16])
2679 int num_filenames = 0;
2680 char **filenames = NULL;
2681 if (num_sources > 1) {
2682 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
2683 "other sources.\n");
2684 if (mpctx->stream->type != STREAMTYPE_FILE) {
2685 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
2686 "normal disk file. Will not search for related files.\n");
2687 } else {
2688 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
2689 "same directory to find referenced sources.\n");
2690 filenames = find_files(mpctx->demuxer->filename, ".mkv",
2691 &num_filenames);
2695 int num_left = num_sources - 1;
2696 for (int i = 0; i < num_filenames && num_left > 0; i++) {
2697 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
2698 filename_recode(filenames[i]));
2699 int format;
2700 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
2701 if (!s)
2702 continue;
2703 struct demuxer *d = demux_open(&mpctx->opts, s, DEMUXER_TYPE_MATROSKA,
2704 mpctx->opts.audio_id,
2705 mpctx->opts.video_id,
2706 mpctx->opts.sub_id, filenames[i]);
2707 if (!d) {
2708 free_stream(s);
2709 continue;
2711 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
2712 for (int i = 1; i < num_sources; i++) {
2713 if (sources[i].demuxer)
2714 continue;
2715 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
2716 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
2717 i, filename_recode(d->filename));
2718 sources[i].stream = s;
2719 sources[i].demuxer = d;
2720 num_left--;
2721 goto match;
2725 free_demuxer(d);
2726 free_stream(s);
2727 continue;
2728 match:
2731 talloc_free(filenames);
2732 if (num_left) {
2733 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
2734 "There will be parts MISSING from the video!\n");
2735 for (int i = 1, j = 1; i < num_sources; i++)
2736 if (sources[i].demuxer) {
2737 sources[j] = sources[i];
2738 memcpy(uid_map[j], uid_map[i], 16);
2739 j++;
2742 return num_sources - num_left;
2745 static void build_ordered_chapter_timeline(struct MPContext *mpctx)
2747 if (!mpctx->opts.ordered_chapters) {
2748 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
2749 "you have disabled support for them. Ignoring.\n");
2750 return;
2753 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
2754 "edit timeline.\n");
2756 struct demuxer *demuxer = mpctx->demuxer;
2757 struct matroska_data *m = &demuxer->matroska_data;
2759 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
2760 m->num_ordered_chapters);
2761 sources[0].stream = mpctx->stream;
2762 sources[0].demuxer = mpctx->demuxer;
2763 unsigned char uid_map[m->num_ordered_chapters][16];
2764 int num_sources = 1;
2765 memcpy(uid_map[0], m->segment_uid, 16);
2767 for (int i = 0; i < m->num_ordered_chapters; i++) {
2768 struct matroska_chapter *c = m->ordered_chapters + i;
2769 if (!c->has_segment_uid)
2770 memcpy(c->segment_uid, m->segment_uid, 16);
2772 for (int j = 0; j < num_sources; j++)
2773 if (!memcmp(c->segment_uid, uid_map[j], 16))
2774 goto found1;
2775 memcpy(uid_map[num_sources], c->segment_uid, 16);
2776 sources[num_sources] = (struct content_source){};
2777 num_sources++;
2778 found1:
2782 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
2783 uid_map);
2786 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
2787 m->num_ordered_chapters + 1);
2788 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
2789 m->num_ordered_chapters);
2790 uint64_t starttime = 0;
2791 uint64_t missing_time = 0;
2792 int part_count = 0;
2793 int num_chapters = 0;
2794 uint64_t prev_part_offset;
2795 for (int i = 0; i < m->num_ordered_chapters; i++) {
2796 struct matroska_chapter *c = m->ordered_chapters + i;
2798 int j;
2799 for (j = 0; j < num_sources; j++) {
2800 if (!memcmp(c->segment_uid, uid_map[j], 16))
2801 goto found2;
2803 missing_time += c->end - c->start;
2804 continue;
2805 found2:;
2806 chapters[num_chapters].start = starttime / 1000.;
2807 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
2808 /* Only add a separate part if the time or file actually changes.
2809 * Matroska files have chapter divisions that are redundant from
2810 * timeline point of view because the same chapter structure is used
2811 * both to specify the timeline and for normal chapter information.
2812 * Removing a missing inserted external chapter can also cause this. */
2813 if (part_count == 0 || c->start != starttime + prev_part_offset
2814 || sources + j != timeline[part_count - 1].source) {
2815 timeline[part_count].source = sources + j;
2816 timeline[part_count].start = chapters[num_chapters].start;
2817 timeline[part_count].source_start = c->start / 1000.;
2818 prev_part_offset = c->start - starttime;
2819 part_count++;
2821 starttime += c->end - c->start;
2822 num_chapters++;
2824 timeline[part_count].start = starttime / 1000.;
2826 if (!part_count) {
2827 // None of the parts come from the file itself???
2828 talloc_free(sources);
2829 talloc_free(timeline);
2830 talloc_free(chapters);
2831 return;
2834 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
2835 "sources. Total length %.3f seconds.\n", part_count, num_sources,
2836 timeline[part_count].start);
2837 if (missing_time)
2838 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
2839 "from the timeline!\n", missing_time / 1000.);
2840 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
2841 for (int i = 0; i < num_sources; i++)
2842 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
2843 filename_recode(sources[i].demuxer->filename));
2844 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
2845 "source_start, source):\n");
2846 for (int i = 0; i < part_count; i++) {
2847 struct timeline_part *p = timeline + i;
2848 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
2849 p->source_start, p->source - sources);
2851 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start);
2852 mpctx->sources = sources;
2853 mpctx->num_sources = num_sources;
2854 mpctx->timeline = timeline;
2855 mpctx->num_timeline_parts = part_count;
2856 mpctx->num_chapters = num_chapters;
2857 mpctx->chapters = chapters;
2859 mpctx->timeline_part = 0;
2860 mpctx->video_offset = timeline[0].source_start;
2861 mpctx->demuxer = timeline[0].source->demuxer;
2865 static int read_keys(void *ctx, int fd)
2867 getch2(ctx);
2868 return mplayer_get_key(ctx, 0);
2872 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2873 * file for some tools to link against. */
2874 #ifndef DISABLE_MAIN
2875 int main(int argc,char* argv[]){
2878 char * mem_ptr;
2880 // movie info:
2882 /* Flag indicating whether MPlayer should exit without playing anything. */
2883 int opt_exit = 0;
2885 //float a_frame=0; // Audio
2887 int i;
2889 struct MPContext *mpctx = &(struct MPContext){
2890 .osd_function = OSD_PLAY,
2891 .begin_skip = MP_NOPTS_VALUE,
2892 .play_tree_step = 1,
2893 .global_sub_pos = -1,
2894 .set_of_sub_pos = -1,
2895 .file_format = DEMUXER_TYPE_UNKNOWN,
2896 .last_dvb_step = 1,
2899 InitTimer();
2900 srand(GetTimerMS());
2902 mp_msg_init();
2904 #ifdef CONFIG_X11
2905 mpctx->x11_state = vo_x11_init_state();
2906 #endif
2907 struct MPOpts *opts = &mpctx->opts;
2908 set_default_mplayer_options(opts);
2909 // Create the config context and register the options
2910 mpctx->mconfig = m_config_new(opts, cfg_include);
2911 m_config_register_options(mpctx->mconfig,mplayer_opts);
2912 mp_input_register_options(mpctx->mconfig);
2914 // Preparse the command line
2915 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2917 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2918 set_path_env();
2919 #endif
2921 #ifdef CONFIG_TV
2922 stream_tv_defaults.immediate = 1;
2923 #endif
2925 parse_cfgfiles(mpctx, mpctx->mconfig);
2927 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2928 if(mpctx->playtree == NULL)
2929 opt_exit = 1;
2930 else {
2931 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2932 if(mpctx->playtree) {
2933 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2934 if(mpctx->playtree_iter) {
2935 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2936 play_tree_iter_free(mpctx->playtree_iter);
2937 mpctx->playtree_iter = NULL;
2939 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2943 mpctx->key_fifo = mp_fifo_create(opts);
2945 print_version("MPlayer");
2947 #if defined(__MINGW32__) || defined(__CYGWIN__)
2949 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
2950 BOOL WINAPI (*setDEP)(DWORD) = NULL;
2951 if (kernel32)
2952 setDEP = GetProcAddress(kernel32, "SetProcessDEPPolicy");
2953 if (setDEP) setDEP(3);
2955 // stop Windows from showing all kinds of annoying error dialogs
2956 SetErrorMode(0x8003);
2957 // request 1ms timer resolution
2958 timeBeginPeriod(1);
2959 #endif
2961 #ifdef CONFIG_PRIORITY
2962 set_priority();
2963 #endif
2965 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
2966 list_video_out();
2967 opt_exit = 1;
2970 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
2971 list_audio_out();
2972 opt_exit = 1;
2975 /* Check codecs.conf. */
2976 if(!codecs_file || !parse_codec_cfg(codecs_file)){
2977 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
2978 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
2979 if(!parse_codec_cfg(NULL)){
2980 exit_player_with_rc(mpctx, EXIT_NONE, 0);
2982 mp_tmsg(MSGT_CPLAYER,MSGL_V,"Using built-in default codecs.conf.\n");
2985 free( mem_ptr ); // release the buffer created by get_path()
2988 #if 0
2989 if(video_codec_list){
2990 int i;
2991 video_codec=video_codec_list[0];
2992 for(i=0;video_codec_list[i];i++)
2993 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
2995 #endif
2996 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
2997 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available audio codecs:\n");
2998 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
2999 list_codecs(1);
3000 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3001 opt_exit = 1;
3003 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3004 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video codecs:\n");
3005 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3006 list_codecs(0);
3007 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3008 opt_exit = 1;
3010 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3011 vfm_help();
3012 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3013 opt_exit = 1;
3015 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3016 afm_help();
3017 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3018 opt_exit = 1;
3020 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3021 af_help();
3022 printf("\n");
3023 opt_exit = 1;
3025 #ifdef CONFIG_X11
3026 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3027 fstype_help();
3028 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3029 opt_exit = 1;
3031 #endif
3032 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3033 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3034 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3035 demuxer_help();
3036 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3037 opt_exit = 1;
3039 if(list_properties) {
3040 property_print_help();
3041 opt_exit = 1;
3044 if(opt_exit)
3045 exit_player(mpctx, EXIT_NONE);
3047 if(!mpctx->filename && !player_idle_mode){
3048 // no file/vcd/dvd -> show HELP:
3049 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
3050 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3053 /* Display what configure line was used */
3054 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3056 // Many users forget to include command line in bugreports...
3057 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3058 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "CommandLine:");
3059 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3060 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3063 //------ load global data first ------
3065 mpctx->osd = osd_create();
3067 // check font
3068 #ifdef CONFIG_FREETYPE
3069 init_freetype();
3070 #endif
3071 #ifdef CONFIG_FONTCONFIG
3072 if(font_fontconfig <= 0)
3074 #endif
3075 #ifdef CONFIG_BITMAP_FONT
3076 if(font_name){
3077 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3078 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",
3079 filename_recode(font_name));
3080 } else {
3081 // try default:
3082 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3083 free(mem_ptr); // release the buffer created by get_path()
3084 if(!vo_font)
3085 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3087 if (sub_font_name)
3088 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3089 else
3090 mpctx->osd->sub_font = vo_font;
3091 #endif
3092 #ifdef CONFIG_FONTCONFIG
3094 #endif
3096 #ifdef CONFIG_ASS
3097 ass_library = ass_init();
3098 #endif
3100 #ifdef HAVE_RTC
3101 if(!nortc)
3103 // seteuid(0); /* Can't hurt to try to get root here */
3104 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3105 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Failed to open %s: %s (it should be readable by the user.)\n",
3106 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3107 else {
3108 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3110 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3111 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp, strerror(errno));
3112 mp_tmsg(MSGT_CPLAYER, MSGL_HINT, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp);
3113 close (rtc_fd);
3114 rtc_fd = -1;
3115 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3116 /* variable only by the root */
3117 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno));
3118 close (rtc_fd);
3119 rtc_fd = -1;
3120 } else
3121 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Using Linux hardware RTC timing (%ldHz).\n", irqp);
3124 if(rtc_fd<0)
3125 #endif /* HAVE_RTC */
3126 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3127 softsleep?"software":timer_name);
3129 #ifdef HAVE_TERMCAP
3130 load_termcap(NULL); // load key-codes
3131 #endif
3133 // ========== Init keyboard FIFO (connection to libvo) ============
3135 // Init input system
3136 current_module = "init_input";
3137 mpctx->input = mp_input_init(&opts->input);
3138 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3139 if(slave_mode)
3140 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
3141 else if(!noconsolecontrols)
3142 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
3143 // Set the libstream interrupt callback
3144 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
3146 #ifdef CONFIG_MENU
3147 if(use_menu) {
3148 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3149 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3150 else {
3151 menu_cfg = get_path("menu.conf");
3152 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3153 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3154 else {
3155 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
3156 MPLAYER_CONFDIR "/menu.conf"))
3157 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", MPLAYER_CONFDIR"/menu.conf");
3158 else {
3159 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Menu init failed.\n");
3160 use_menu = 0;
3165 #endif
3167 current_module = NULL;
3169 /// Catch signals
3170 #ifndef __MINGW32__
3171 signal(SIGCHLD,child_sighandler);
3172 #endif
3174 #ifdef CONFIG_CRASH_DEBUG
3175 prog_path = argv[0];
3176 #endif
3177 //========= Catch terminate signals: ================
3178 // terminate requests:
3179 signal(SIGTERM,exit_sighandler); // kill
3180 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3182 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3184 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3185 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3186 #ifdef CONFIG_SIGHANDLER
3187 // fatal errors:
3188 signal(SIGBUS,exit_sighandler); // bus error
3189 signal(SIGSEGV,exit_sighandler); // segfault
3190 signal(SIGILL,exit_sighandler); // illegal instruction
3191 signal(SIGFPE,exit_sighandler); // floating point exc.
3192 signal(SIGABRT,exit_sighandler); // abort()
3193 #ifdef CONFIG_CRASH_DEBUG
3194 if (crash_debug)
3195 signal(SIGTRAP,exit_sighandler);
3196 #endif
3197 #endif
3199 // ******************* Now, let's see the per-file stuff ********************
3201 play_next_file:
3203 // init global sub numbers
3204 mpctx->global_sub_size = 0;
3205 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3207 if (mpctx->filename) {
3208 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
3209 load_per_extension_config (mpctx->mconfig, mpctx->filename);
3210 load_per_file_config (mpctx->mconfig, mpctx->filename);
3213 if (opts->video_driver_list)
3214 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
3215 if (opts->audio_driver_list)
3216 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
3218 // We must enable getch2 here to be able to interrupt network connection
3219 // or cache filling
3220 if(!noconsolecontrols && !slave_mode){
3221 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
3222 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"WARNING: getch2_init called twice!\n");
3223 else
3224 getch2_enable(); // prepare stdin for hotkeys...
3225 mpctx->initialized_flags|=INITIALIZED_GETCH2;
3226 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3229 // =================== GUI idle loop (STOP state) ===========================
3230 while (player_idle_mode && !mpctx->filename) {
3231 play_tree_t * entry = NULL;
3232 mp_cmd_t * cmd;
3233 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3234 if (mpctx->video_out)
3235 vo_check_events(mpctx->video_out);
3236 usec_sleep(20000);
3238 switch (cmd->id) {
3239 case MP_CMD_LOADFILE:
3240 // prepare a tree entry with the new filename
3241 entry = play_tree_new();
3242 play_tree_add_file(entry, cmd->args[0].v.s);
3243 // The entry is added to the main playtree after the switch().
3244 break;
3245 case MP_CMD_LOADLIST:
3246 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3247 break;
3248 case MP_CMD_QUIT:
3249 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3250 break;
3251 case MP_CMD_GET_PROPERTY:
3252 case MP_CMD_SET_PROPERTY:
3253 case MP_CMD_STEP_PROPERTY:
3254 run_command(mpctx, cmd);
3255 break;
3258 mp_cmd_free(cmd);
3260 if (entry) { // user entered a command that gave a valid entry
3261 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3262 play_tree_free_list(mpctx->playtree->child, 1);
3263 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3265 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3267 play_tree_set_child(mpctx->playtree, entry);
3269 /* Make iterator start at the top the of tree. */
3270 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3271 if (!mpctx->playtree_iter) continue;
3273 // find the first real item in the tree
3274 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3275 // no items!
3276 play_tree_iter_free(mpctx->playtree_iter);
3277 mpctx->playtree_iter = NULL;
3278 continue; // wait for next command
3280 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3283 //---------------------------------------------------------------------------
3285 if(mpctx->filename)
3286 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nPlaying %s.\n",
3287 filename_recode(mpctx->filename));
3289 if (edl_filename) {
3290 if (edl_records) free_edl(edl_records);
3291 next_edl_record = edl_records = edl_parse_file();
3293 if (edl_output_filename) {
3294 if (edl_fd) fclose(edl_fd);
3295 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3297 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Can't open EDL file [%s] for writing.\n",
3298 filename_recode(edl_output_filename));
3302 //==================== Open VOB-Sub ============================
3304 current_module="vobsub";
3305 if (vobsub_name){
3306 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3307 if(vo_vobsub==NULL)
3308 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
3309 filename_recode(vobsub_name));
3310 } else if (sub_auto && mpctx->filename){
3311 /* try to autodetect vobsub from movie filename ::atmos */
3312 char *buf = strdup(mpctx->filename), *psub;
3313 char *pdot = strrchr(buf, '.');
3314 char *pslash = strrchr(buf, '/');
3315 #if defined(__MINGW32__) || defined(__CYGWIN__)
3316 if (!pslash) pslash = strrchr(buf, '\\');
3317 #endif
3318 if (pdot && (!pslash || pdot > pslash))
3319 *pdot = '\0';
3320 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3321 /* try from ~/.mplayer/sub */
3322 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3323 char *bname;
3324 int l;
3325 bname = strrchr(buf,'/');
3326 #if defined(__MINGW32__) || defined(__CYGWIN__)
3327 if(!bname) bname = strrchr(buf,'\\');
3328 #endif
3329 if(bname) bname++;
3330 else bname = buf;
3331 l = strlen(psub) + strlen(bname) + 1;
3332 psub = realloc(psub,l);
3333 strcat(psub,bname);
3334 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3335 free(psub);
3337 free(buf);
3339 if(vo_vobsub){
3340 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3341 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3342 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3344 // setup global sub numbering
3345 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3346 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3349 //============ Open & Sync STREAM --- fork cache2 ====================
3351 mpctx->stream=NULL;
3352 mpctx->demuxer=NULL;
3353 if (mpctx->d_audio) {
3354 //free_demuxer_stream(mpctx->d_audio);
3355 mpctx->d_audio=NULL;
3357 if (mpctx->d_video) {
3358 //free_demuxer_stream(d_video);
3359 mpctx->d_video=NULL;
3361 mpctx->sh_audio=NULL;
3362 mpctx->sh_video=NULL;
3364 current_module="open_stream";
3365 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3366 if(!mpctx->stream) { // error...
3367 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3368 goto goto_next_file;
3370 mpctx->initialized_flags|=INITIALIZED_STREAM;
3372 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3373 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3374 "playlist support will not be used automatically.\n"
3375 "MPlayer's playlist code is unsafe and should only be used with "
3376 "trusted sources.\nPlayback will probably fail.\n\n");
3377 #if 0
3378 play_tree_t* entry;
3379 // Handle playlist
3380 current_module="handle_playlist";
3381 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3382 filename_recode(mpctx->filename));
3383 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3384 mpctx->eof=playtree_add_playlist(mpctx, entry);
3385 goto goto_next_file;
3386 #endif
3388 mpctx->stream->start_pos+=seek_to_byte;
3390 if(stream_dump_type==5){
3391 unsigned char buf[4096];
3392 int len;
3393 FILE *f;
3394 current_module="dumpstream";
3395 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3396 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot dump this stream - no file descriptor available.\n");
3397 exit_player(mpctx, EXIT_ERROR);
3399 stream_reset(mpctx->stream);
3400 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3401 f=fopen(stream_dump_name,"wb");
3402 if(!f){
3403 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3404 exit_player(mpctx, EXIT_ERROR);
3406 if (dvd_chapter > 1) {
3407 int chapter = dvd_chapter - 1;
3408 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3410 while(!mpctx->stream->eof && !async_quit_request){
3411 len=stream_read(mpctx->stream,buf,4096);
3412 if(len>0) {
3413 if(fwrite(buf,len,1,f) != 1) {
3414 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3415 exit_player(mpctx, EXIT_ERROR);
3418 if(dvd_last_chapter > 0) {
3419 int chapter = -1;
3420 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3421 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3422 break;
3425 if(fclose(f)) {
3426 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3427 exit_player(mpctx, EXIT_ERROR);
3429 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3430 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3433 #ifdef CONFIG_DVDREAD
3434 if(mpctx->stream->type==STREAMTYPE_DVD){
3435 current_module="dvd lang->id";
3436 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3437 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3438 // setup global sub numbering
3439 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3440 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3441 current_module=NULL;
3443 #endif
3445 #ifdef CONFIG_DVDNAV
3446 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3447 current_module="dvdnav lang->id";
3448 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3449 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3450 // setup global sub numbering
3451 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3452 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3453 current_module=NULL;
3455 #endif
3457 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3458 goto_enable_cache:
3459 if(stream_cache_size>0){
3460 current_module="enable_cache";
3461 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3462 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3463 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3464 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3467 //============ Open DEMUXERS --- DETECT file type =======================
3468 current_module="demux_open";
3470 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3472 // HACK to get MOV Reference Files working
3474 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3476 unsigned char* playlist_entry;
3477 play_tree_t *list = NULL, *entry = NULL;
3479 current_module="handle_demux_playlist";
3480 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3482 char *temp, *bname;
3484 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3485 filename_recode(playlist_entry));
3487 bname=mp_basename(playlist_entry);
3488 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3489 continue;
3491 if (!strcmp(playlist_entry, mpctx->filename)) // ignoring self-reference
3492 continue;
3494 entry = play_tree_new();
3496 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3498 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3499 if (temp)
3501 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3502 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3503 strcat(temp, playlist_entry);
3504 if (!strcmp(temp, mpctx->filename)) {
3505 free(temp);
3506 continue;
3508 play_tree_add_file(entry,temp);
3509 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3510 free(temp);
3513 else
3514 play_tree_add_file(entry,playlist_entry);
3516 if(!list)
3517 list = entry;
3518 else
3519 play_tree_append_entry(list,entry);
3521 free_demuxer(mpctx->demuxer);
3522 mpctx->demuxer = NULL;
3524 if (list)
3526 entry = play_tree_new();
3527 play_tree_set_child(entry,list);
3528 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3529 goto goto_next_file;
3533 if(!mpctx->demuxer)
3534 goto goto_next_file;
3536 if (mpctx->demuxer->matroska_data.ordered_chapters)
3537 build_ordered_chapter_timeline(mpctx);
3539 if (!mpctx->sources) {
3540 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
3541 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
3542 .demuxer = mpctx->demuxer};
3543 mpctx->num_sources = 1;
3546 if(dvd_chapter>1) {
3547 double pts;
3548 if (seek_chapter(mpctx, dvd_chapter-1, &pts, NULL) >= 0 && pts > -1.0)
3549 seek(mpctx, pts, SEEK_ABSOLUTE);
3552 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3554 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3555 int i;
3556 int maxid = -1;
3557 // setup global sub numbering
3558 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3559 for (i = 0; i < MAX_S_STREAMS; i++)
3560 if (mpctx->demuxer->s_streams[i])
3561 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3562 mpctx->global_sub_size += maxid + 1;
3564 // Make opts->sub_id always selectable if set.
3565 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3566 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3568 #ifdef CONFIG_ASS
3569 if (ass_enabled && ass_library) {
3570 for (int j = 0; j < mpctx->num_sources; j++) {
3571 struct demuxer *d = mpctx->sources[j].demuxer;
3572 for (int i = 0; i < d->num_attachments; i++) {
3573 struct demux_attachment *att = d->attachments + i;
3574 if (extract_embedded_fonts
3575 && att->name && att->type && att->data && att->data_size
3576 && (strcmp(att->type, "application/x-truetype-font") == 0
3577 || strcmp(att->type, "application/x-font") == 0))
3578 ass_add_font(ass_library, att->name, att->data, att->data_size);
3582 #endif
3584 current_module="demux_open2";
3586 //file_format=demuxer->file_format;
3588 mpctx->d_audio=mpctx->demuxer->audio;
3589 mpctx->d_video=mpctx->demuxer->video;
3590 mpctx->d_sub=mpctx->demuxer->sub;
3592 // select audio stream
3593 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3595 // DUMP STREAMS:
3596 if((stream_dump_type)&&(stream_dump_type<4)){
3597 FILE *f;
3598 demux_stream_t *ds=NULL;
3599 current_module="dump";
3600 // select stream to dump
3601 switch(stream_dump_type){
3602 case 1: ds=mpctx->d_audio;break;
3603 case 2: ds=mpctx->d_video;break;
3604 case 3: ds=mpctx->d_sub;break;
3606 if(!ds){
3607 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"dump: FATAL: Selected stream missing!\n");
3608 exit_player(mpctx, EXIT_ERROR);
3610 // disable other streams:
3611 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3612 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3613 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3614 // let's dump it!
3615 f=fopen(stream_dump_name,"wb");
3616 if(!f){
3617 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3618 exit_player(mpctx, EXIT_ERROR);
3620 while(!ds->eof){
3621 unsigned char* start;
3622 int in_size=ds_get_packet(ds,&start);
3623 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3624 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3625 if(in_size>0) fwrite(start,in_size,1,f);
3626 if(dvd_last_chapter>0) {
3627 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3628 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3629 break;
3632 fclose(f);
3633 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3634 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3637 mpctx->sh_audio=mpctx->d_audio->sh;
3638 mpctx->sh_video=mpctx->d_video->sh;
3640 if(mpctx->sh_video){
3642 current_module="video_read_properties";
3643 if(!video_read_properties(mpctx->sh_video)) {
3644 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Video: Cannot read properties.\n");
3645 mpctx->sh_video=mpctx->d_video->sh=NULL;
3646 } else {
3647 mp_tmsg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
3648 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3649 mpctx->sh_video->fps,mpctx->sh_video->frametime
3652 /* need to set fps here for output encoders to pick it up in their init */
3653 if(force_fps){
3654 mpctx->sh_video->fps=force_fps;
3655 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3657 vo_fps = mpctx->sh_video->fps;
3659 if(!mpctx->sh_video->fps && !force_fps){
3660 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"FPS not specified in the header or invalid, use the -fps option.\n");
3661 mpctx->sh_video=mpctx->d_video->sh=NULL;
3667 if(!mpctx->sh_video && !mpctx->sh_audio){
3668 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "No stream found.\n");
3669 #ifdef CONFIG_DVBIN
3670 if(mpctx->stream->type == STREAMTYPE_DVB)
3672 int dir;
3673 int v = mpctx->last_dvb_step;
3674 if(v > 0)
3675 dir = DVB_CHANNEL_HIGHER;
3676 else
3677 dir = DVB_CHANNEL_LOWER;
3679 if(dvb_step_channel(mpctx->stream, dir)) {
3680 mpctx->stop_play = PT_NEXT_ENTRY;
3681 mpctx->dvbin_reopen = 1;
3684 #endif
3685 goto goto_next_file; // exit_player(_("Fatal error"));
3688 /* display clip info */
3689 demux_info_print(mpctx->demuxer);
3691 //================== Read SUBTITLES (DVD & TEXT) ==========================
3692 if(vo_spudec==NULL && mpctx->sh_video &&
3693 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3694 init_vo_spudec(mpctx);
3697 // after reading video params we should load subtitles because
3698 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3699 // check .sub
3700 current_module="read_subtitles_file";
3701 double sub_fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
3702 if(sub_name){
3703 for (i = 0; sub_name[i] != NULL; ++i)
3704 add_subtitles(mpctx, sub_name[i], sub_fps, 0);
3706 if(sub_auto) { // auto load sub file ...
3707 char *psub = get_path( "sub/" );
3708 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3709 int i = 0;
3710 free(psub); // release the buffer created by get_path() above
3711 while (tmp[i]) {
3712 add_subtitles(mpctx, tmp[i], sub_fps, 1);
3713 free(tmp[i++]);
3715 free(tmp);
3717 if (mpctx->set_of_sub_size > 0) {
3718 // setup global sub numbering
3719 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3720 mpctx->global_sub_size += mpctx->set_of_sub_size;
3724 if (mpctx->global_sub_size) {
3725 // find the best sub to use
3726 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3727 mpctx->global_sub_pos = -1; // no subs by default
3728 if (vobsub_index_id >= 0) {
3729 // if user asks for a vobsub id, use that first.
3730 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3731 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3732 // if user asks for a dvd sub id, use that next.
3733 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3734 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3735 // if there are text subs to use, use those. (autosubs come last here)
3736 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3737 } else if (opts->sub_id == -1 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3738 // finally select subs by language and container hints
3739 if (opts->sub_id == -1 && dvdsub_lang)
3740 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3741 if (opts->sub_id == -1)
3742 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3743 if (opts->sub_id >= 0)
3744 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3746 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3747 mpctx->global_sub_pos--;
3748 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3749 if(subdata)
3750 switch (stream_dump_type) {
3751 case 3: list_sub_file(subdata); break;
3752 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3753 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3754 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3755 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3756 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3760 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3761 filename_recode(mpctx->filename));
3762 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3763 if (mpctx->sh_video) {
3764 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3765 if (mpctx->sh_video->format >= 0x20202020)
3766 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3767 else
3768 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3769 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3770 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3771 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3772 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3773 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3775 if (mpctx->sh_audio) {
3776 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3777 if (mpctx->sh_audio->format >= 0x20202020)
3778 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3779 else
3780 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3781 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3782 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3783 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3785 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2lf\n", demuxer_get_time_length(mpctx->demuxer));
3786 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n",
3787 mpctx->stream->seek && (!mpctx->demuxer || mpctx->demuxer->seekable));
3788 if (mpctx->demuxer) {
3789 if (mpctx->demuxer->num_chapters == 0)
3790 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3791 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3794 if(!mpctx->sh_video) goto main; // audio-only
3796 if(!reinit_video_chain(mpctx)) {
3797 if(!mpctx->sh_video){
3798 if(!mpctx->sh_audio) goto goto_next_file;
3799 goto main; // exit_player(_("Fatal error"));
3803 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3804 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3806 #ifdef CONFIG_FREETYPE
3807 force_load_font = 1;
3808 #endif
3810 //================== MAIN: ==========================
3811 main:
3812 current_module="main";
3814 if(playing_msg) {
3815 char* msg = property_expand_string(mpctx, playing_msg);
3816 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3817 free(msg);
3821 // Disable the term OSD in verbose mode
3822 if(verbose) term_osd = 0;
3825 //int frame_corr_num=0; //
3826 //float v_frame=0; // Video
3827 //float num_frames=0; // number of frames played
3829 int frame_time_remaining=0; // flag
3830 int blit_frame=0;
3832 // Make sure old OSD does not stay around,
3833 // e.g. with -fixed-vo and same-resolution files
3834 clear_osd_msgs();
3835 update_osd_msg(mpctx);
3837 //================ SETUP AUDIO ==========================
3839 if(mpctx->sh_audio){
3840 reinit_audio_chain(mpctx);
3841 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3842 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3845 current_module="av_init";
3847 if(mpctx->sh_video){
3848 mpctx->sh_video->timer=0;
3849 if (! ignore_start)
3850 audio_delay += mpctx->sh_video->stream_delay;
3852 if(mpctx->sh_audio){
3853 if (start_volume >= 0)
3854 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3855 if (! ignore_start)
3856 audio_delay -= mpctx->sh_audio->stream_delay;
3857 mpctx->delay=-audio_delay;
3860 if(!mpctx->sh_audio){
3861 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Audio: no sound\n");
3862 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3863 ds_free_packs(mpctx->d_audio); // free buffered chunks
3864 //mpctx->d_audio->id=-2; // do not read audio chunks
3865 //uninit_player(mpctx, INITIALIZED_AO); // close device
3867 if(!mpctx->sh_video){
3868 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Video: no video\n");
3869 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3870 ds_free_packs(mpctx->d_video);
3871 mpctx->d_video->id=-2;
3872 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3875 if (!mpctx->sh_video && !mpctx->sh_audio)
3876 goto goto_next_file;
3878 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3879 if(force_fps && mpctx->sh_video){
3880 vo_fps = mpctx->sh_video->fps=force_fps;
3881 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3882 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx->sh_video->fps,mpctx->sh_video->frametime);
3885 mp_input_set_section(mpctx->input, NULL);
3886 //TODO: add desired (stream-based) sections here
3887 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3888 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3890 //==================== START PLAYING =======================
3892 if(opts->loop_times>1) opts->loop_times--; else
3893 if(opts->loop_times==1) opts->loop_times = -1;
3895 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Starting playback...\n");
3897 total_time_usage_start=GetTimer();
3898 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3899 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3900 play_n_frames=play_n_frames_mf;
3902 if(play_n_frames==0){
3903 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
3906 if (seek_to_sec) {
3907 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3908 end_at.pos += seek_to_sec;
3911 if (end_at.type == END_AT_SIZE) {
3912 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Option -endpos in MPlayer does not yet support size units.\n");
3913 end_at.type = END_AT_NONE;
3916 #ifdef CONFIG_DVDNAV
3917 mp_dvdnav_context_free(mpctx);
3918 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3919 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3920 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3922 #endif
3924 get_relative_time(mpctx); // reset current delta
3925 mpctx->time_frame = 0;
3926 mpctx->drop_message_shown = 0;
3927 mpctx->update_video_immediately = true;
3928 mpctx->total_avsync_change = 0;
3929 // Make sure VO knows current pause state
3930 if (mpctx->sh_video)
3931 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
3932 NULL);
3934 while(!mpctx->stop_play){
3935 float aq_sleep_time=0;
3937 if(dvd_last_chapter>0) {
3938 int cur_chapter = get_current_chapter(mpctx);
3939 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3940 goto goto_next_file;
3943 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3944 mpctx->sh_audio = mpctx->d_audio->sh;
3945 mpctx->sh_audio->ds = mpctx->d_audio;
3946 reinit_audio_chain(mpctx);
3949 /*========================== PLAY AUDIO ============================*/
3951 if (mpctx->sh_audio && !mpctx->paused)
3952 if (!fill_audio_out_buffers(mpctx))
3953 // at eof, all audio at least written to ao
3954 if (!mpctx->sh_video)
3955 mpctx->stop_play = AT_END_OF_FILE;
3958 if(!mpctx->sh_video) {
3959 // handle audio-only case:
3960 double a_pos=0;
3961 // sh_audio can be NULL due to video stream switching
3962 // TODO: handle this better
3963 if (mpctx->sh_audio)
3964 a_pos = playing_audio_pts(mpctx);
3966 print_status(mpctx, a_pos, false);
3968 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3969 mpctx->stop_play = PT_NEXT_ENTRY;
3970 update_subtitles(mpctx, &mpctx->opts, NULL, a_pos, mpctx->video_offset,
3971 mpctx->d_sub, 0);
3972 update_osd_msg(mpctx);
3974 } else {
3976 /*========================== PLAY VIDEO ============================*/
3978 vo_pts=mpctx->sh_video->timer*90000.0;
3979 vo_fps=mpctx->sh_video->fps;
3981 if (!mpctx->video_out->frame_loaded) {
3982 double frame_time = update_video(mpctx, &blit_frame);
3983 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
3984 if (mpctx->sh_video->vf_initialized < 0) {
3985 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "\nFATAL: Could not initialize video filters (-vf) or video output (-vo).\n");
3986 mpctx->stop_play = PT_NEXT_ENTRY;
3987 goto goto_next_file;
3989 if (blit_frame) {
3990 struct vf_instance *vf = mpctx->sh_video->vfilter;
3991 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
3992 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
3993 vo_osd_changed(0);
3995 if (frame_time < 0)
3996 mpctx->stop_play = AT_END_OF_FILE;
3997 else {
3998 if (mpctx->update_video_immediately) {
3999 // Show this frame immediately, rest normally
4000 mpctx->update_video_immediately = false;
4001 } else {
4002 mpctx->time_frame += frame_time / opts->playback_speed;
4003 adjust_sync(mpctx, frame_time);
4007 if (mpctx->timeline) {
4008 struct timeline_part *next = mpctx->timeline + mpctx->timeline_part + 1;
4009 if (mpctx->sh_video->pts >= next->start
4010 || mpctx->stop_play == AT_END_OF_FILE
4011 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
4012 seek(mpctx, next->start, SEEK_ABSOLUTE);
4013 continue;
4017 // ==========================================================================
4019 // current_module="draw_osd";
4020 // if(vo_config_count) mpctx->video_out->draw_osd();
4022 current_module="vo_check_events";
4023 vo_check_events(mpctx->video_out);
4025 #ifdef CONFIG_X11
4026 if (stop_xscreensaver) {
4027 current_module = "stop_xscreensaver";
4028 xscreensaver_heartbeat(mpctx->x11_state);
4030 #endif
4031 if (heartbeat_cmd) {
4032 static unsigned last_heartbeat;
4033 unsigned now = GetTimerMS();
4034 if (now - last_heartbeat > 30000) {
4035 last_heartbeat = now;
4036 system(heartbeat_cmd);
4040 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
4042 //====================== FLIP PAGE (VIDEO BLT): =========================
4044 current_module="flip_page";
4045 if (!frame_time_remaining && blit_frame) {
4046 unsigned int t2=GetTimer();
4048 vo_flip_page(mpctx->video_out);
4050 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
4051 vout_time_usage += mpctx->last_vo_flip_duration;
4052 print_status(mpctx, MP_NOPTS_VALUE, true);
4054 else
4055 print_status(mpctx, MP_NOPTS_VALUE, false);
4057 //============================ Auto QUALITY ============================
4059 /*Output quality adjustments:*/
4060 if(auto_quality>0){
4061 current_module="autoq";
4062 // float total=0.000001f * (GetTimer()-aq_total_time);
4063 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4064 if(output_quality<auto_quality && aq_sleep_time>0)
4065 ++output_quality;
4066 else
4067 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4068 if(output_quality>1 && aq_sleep_time<0)
4069 --output_quality;
4070 else
4071 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4072 output_quality=0;
4073 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4074 set_video_quality(mpctx->sh_video,output_quality);
4077 if (!frame_time_remaining && blit_frame) {
4078 if (play_n_frames >= 0) {
4079 --play_n_frames;
4080 if (play_n_frames <= 0)
4081 mpctx->stop_play = PT_NEXT_ENTRY;
4083 if (mpctx->step_frames > 0) {
4084 mpctx->step_frames--;
4085 if (mpctx->step_frames == 0)
4086 pause_player(mpctx);
4091 // FIXME: add size based support for -endpos
4092 if (end_at.type == END_AT_TIME &&
4093 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
4094 mpctx->stop_play = PT_NEXT_ENTRY;
4096 } // end if(mpctx->sh_video)
4098 #ifdef CONFIG_DVDNAV
4099 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4100 nav_highlight_t hl;
4101 mp_dvdnav_get_highlight (mpctx->stream, &hl);
4102 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4103 vo_osd_changed (OSDTYPE_DVDNAV);
4105 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
4106 double ar = -1.0;
4107 if (stream_control (mpctx->demuxer->stream,
4108 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
4109 != STREAM_UNSUPPORTED)
4110 mpctx->sh_video->stream_aspect = ar;
4113 #endif
4115 //================= Keyboard events, SEEKing ====================
4117 current_module="key_events";
4120 while (1) {
4121 mp_cmd_t* cmd;
4122 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
4123 run_command(mpctx, cmd);
4124 mp_cmd_free(cmd);
4125 if (mpctx->stop_play)
4126 break;
4128 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
4129 || mpctx->abs_seek_pos)
4130 break;
4131 if (mpctx->sh_video) {
4132 update_osd_msg(mpctx);
4133 int hack = vo_osd_changed(0);
4134 vo_osd_changed(hack);
4135 if (hack)
4136 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
4137 add_step_frame(mpctx);
4138 break;
4140 else
4141 vo_osd_changed(0);
4143 pause_loop(mpctx);
4147 // handle -sstep
4148 if (step_sec > 0 && !mpctx->paused) {
4149 mpctx->osd_function=OSD_FFW;
4150 mpctx->rel_seek_secs+=step_sec;
4153 edl_update(mpctx);
4155 /* Looping. */
4156 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
4157 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
4159 if(opts->loop_times>1) opts->loop_times--; else
4160 if(opts->loop_times==1) opts->loop_times=-1;
4161 play_n_frames=play_n_frames_mf;
4162 mpctx->stop_play=0;
4163 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
4166 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
4167 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
4169 mpctx->rel_seek_secs=0;
4170 mpctx->abs_seek_pos=0;
4173 } // while(!mpctx->stop_play)
4175 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4177 #ifdef CONFIG_DVBIN
4178 if(mpctx->dvbin_reopen)
4180 mpctx->stop_play = 0;
4181 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4182 cache_uninit(mpctx->stream);
4183 mpctx->dvbin_reopen = 0;
4184 goto goto_enable_cache;
4186 #endif
4189 goto_next_file: // don't jump here after ao/vo/getch initialization!
4191 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4193 if(benchmark){
4194 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4195 double total_time_usage;
4196 total_time_usage_start=GetTimer()-total_time_usage_start;
4197 total_time_usage = (float)total_time_usage_start*0.000001;
4198 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4199 video_time_usage,vout_time_usage,audio_time_usage,
4200 total_time_usage-tot,total_time_usage);
4201 if(total_time_usage>0.0)
4202 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4203 100.0*video_time_usage/total_time_usage,
4204 100.0*vout_time_usage/total_time_usage,
4205 100.0*audio_time_usage/total_time_usage,
4206 100.0*(total_time_usage-tot)/total_time_usage,
4207 100.0);
4208 if(total_frame_cnt && frame_dropping)
4209 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4210 total_frame_cnt-drop_frame_cnt,
4211 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4212 drop_frame_cnt,
4213 100*drop_frame_cnt/total_frame_cnt,
4214 total_frame_cnt,
4215 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4218 // time to uninit all, except global stuff:
4219 uninit_player(mpctx, INITIALIZED_ALL-(opts->fixed_vo?INITIALIZED_VO:0));
4221 if(mpctx->set_of_sub_size > 0) {
4222 current_module="sub_free";
4223 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4224 sub_free(mpctx->set_of_subtitles[i]);
4225 #ifdef CONFIG_ASS
4226 if(mpctx->set_of_ass_tracks[i])
4227 ass_free_track( mpctx->set_of_ass_tracks[i] );
4228 #endif
4230 mpctx->set_of_sub_size = 0;
4232 vo_sub_last = vo_sub=NULL;
4233 subdata=NULL;
4234 #ifdef CONFIG_ASS
4235 ass_track = NULL;
4236 if(ass_library)
4237 ass_clear_fonts(ass_library);
4238 #endif
4240 if (!mpctx->stop_play) // In case some goto jumped here...
4241 mpctx->stop_play = PT_NEXT_ENTRY;
4243 int playtree_direction = 1;
4245 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4246 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4247 play_tree_iter_free(mpctx->playtree_iter);
4248 mpctx->playtree_iter = NULL;
4250 mpctx->play_tree_step = 1;
4251 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4252 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4253 if(mpctx->playtree_iter) {
4254 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4255 play_tree_iter_free(mpctx->playtree_iter);
4256 mpctx->playtree_iter = NULL;
4259 } else if (mpctx->stop_play == PT_STOP) {
4260 play_tree_iter_free(mpctx->playtree_iter);
4261 mpctx->playtree_iter = NULL;
4262 } else { // NEXT PREV SRC
4263 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4266 while(mpctx->playtree_iter != NULL) {
4267 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4268 if(mpctx->filename == NULL) {
4269 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4270 play_tree_iter_free(mpctx->playtree_iter);
4271 mpctx->playtree_iter = NULL;
4273 } else
4274 break;
4277 if(mpctx->playtree_iter != NULL || player_idle_mode){
4278 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4279 mpctx->stop_play = 0;
4280 goto play_next_file;
4284 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4286 return 1;
4288 #endif /* DISABLE_MAIN */