Updated ffmpeg
[mplayer/kovensky.git] / mplayer.c
blobf63f8ae56462dc6e95328731d8ec92bdc2c40e96
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 uninit_player(mpctx, INITIALIZED_ALL);
668 #if defined(__MINGW32__) || defined(__CYGWIN__)
669 timeEndPeriod(1);
670 #endif
671 #ifdef CONFIG_X11
672 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
673 #endif
675 current_module="uninit_input";
676 mp_input_uninit(mpctx->input);
677 #ifdef CONFIG_MENU
678 if (use_menu)
679 menu_uninit();
680 #endif
682 #ifdef CONFIG_FREETYPE
683 current_module="uninit_font";
684 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
685 free_font_desc(mpctx->osd->sub_font);
686 free_font_desc(vo_font);
687 vo_font = NULL;
688 done_freetype();
689 #endif
690 osd_free(mpctx->osd);
692 #ifdef CONFIG_ASS
693 ass_library_done(ass_library);
694 #endif
696 current_module="exit_player";
698 // free mplayer config
699 if(mpctx->mconfig)
700 m_config_free(mpctx->mconfig);
702 if(mpctx->playtree)
703 play_tree_free(mpctx->playtree, 1);
705 talloc_free(mpctx->key_fifo);
707 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
708 switch(how) {
709 case EXIT_QUIT:
710 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Quit");
711 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
712 break;
713 case EXIT_EOF:
714 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","End of file");
715 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
716 break;
717 case EXIT_ERROR:
718 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Fatal error");
719 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
720 break;
721 default:
722 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
724 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
726 exit(rc);
729 static void exit_player(struct MPContext *mpctx, exit_reason_t how)
731 exit_player_with_rc(mpctx, how, 1);
734 #ifndef __MINGW32__
735 static void child_sighandler(int x){
736 pid_t pid;
737 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
739 #endif
741 #ifdef CONFIG_CRASH_DEBUG
742 static char *prog_path;
743 static int crash_debug = 0;
744 #endif
746 static void exit_sighandler(int x){
747 static int sig_count=0;
748 #ifdef CONFIG_CRASH_DEBUG
749 if (!crash_debug || x != SIGTRAP)
750 #endif
751 ++sig_count;
752 if(sig_count==5)
754 /* We're crashing bad and can't uninit cleanly :(
755 * by popular request, we make one last (dirty)
756 * effort to restore the user's
757 * terminal. */
758 getch2_disable();
759 exit(1);
761 if(sig_count==6) exit(1);
762 if(sig_count>6){
763 // can't stop :(
764 #ifndef __MINGW32__
765 kill(getpid(),SIGKILL);
766 #endif
768 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"\n" "\nMPlayer interrupted by signal %d in module: %s\n",x,
769 current_module?current_module:"unknown"
771 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
772 if(sig_count<=1)
773 switch(x){
774 case SIGINT:
775 case SIGQUIT:
776 case SIGTERM:
777 case SIGKILL:
778 async_quit_request = 1;
779 return; // killed from keyboard (^C) or killed [-9]
780 case SIGILL:
781 #if CONFIG_RUNTIME_CPUDETECT
782 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL_RTCpuSel);
783 #else
784 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL);
785 #endif
786 case SIGFPE:
787 case SIGSEGV:
788 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGSEGV_SIGFPE);
789 default:
790 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGCRASH);
791 #ifdef CONFIG_CRASH_DEBUG
792 if (crash_debug) {
793 int gdb_pid;
794 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
795 gdb_pid = fork();
796 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
797 if (gdb_pid == 0) { // We are the child
798 char spid[20];
799 snprintf(spid, sizeof(spid), "%i", getppid());
800 getch2_disable(); // allow terminal to work properly with gdb
801 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
802 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
803 } else if (gdb_pid < 0)
804 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
805 else {
806 waitpid(gdb_pid, NULL, 0);
808 if (x == SIGTRAP) return;
810 #endif
812 getch2_disable();
813 exit(1);
816 #include "cfg-mplayer.h"
818 static int cfg_include(m_option_t *conf, char *filename)
820 return m_config_parse_config_file(conf->priv, filename);
823 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
825 char *conffile;
826 int conffile_fd;
827 if (!disable_system_conf &&
828 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
829 exit_player(mpctx, EXIT_NONE);
830 if ((conffile = get_path("")) == NULL) {
831 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"Cannot find HOME directory.\n");
832 } else {
833 #ifdef __MINGW32__
834 mkdir(conffile);
835 #else
836 mkdir(conffile, 0777);
837 #endif
838 free(conffile);
839 if ((conffile = get_path("config")) == NULL) {
840 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
841 } else {
842 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
843 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Creating config file: %s\n", conffile);
844 write(conffile_fd, default_config, strlen(default_config));
845 close(conffile_fd);
847 if (!disable_user_conf &&
848 m_config_parse_config_file(conf, conffile) < 0)
849 exit_player(mpctx, EXIT_NONE);
850 free(conffile);
855 #define PROFILE_CFG_PROTOCOL "protocol."
857 static void load_per_protocol_config (m_config_t* conf, const char *const file)
859 char *str;
860 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
861 m_profile_t *p;
863 /* does filename actually uses a protocol ? */
864 str = strstr (file, "://");
865 if (!str)
866 return;
868 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
869 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
870 p = m_config_get_profile (conf, protocol);
871 if (p)
873 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading protocol-related profile '%s'\n", protocol);
874 m_config_set_profile(conf,p);
878 #define PROFILE_CFG_EXTENSION "extension."
880 static void load_per_extension_config (m_config_t* conf, const char *const file)
882 char *str;
883 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
884 m_profile_t *p;
886 /* does filename actually have an extension ? */
887 str = strrchr (file, '.');
888 if (!str)
889 return;
891 sprintf (extension, PROFILE_CFG_EXTENSION);
892 strncat (extension, ++str, 7);
893 p = m_config_get_profile (conf, extension);
894 if (p)
896 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", extension);
897 m_config_set_profile(conf,p);
901 #define PROFILE_CFG_VO "vo."
902 #define PROFILE_CFG_AO "ao."
904 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
906 char profile[strlen (cfg) + strlen (out) + 1];
907 m_profile_t *p;
909 sprintf (profile, "%s%s", cfg, out);
910 p = m_config_get_profile (conf, profile);
911 if (p)
913 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", profile);
914 m_config_set_profile(conf,p);
919 * Tries to load a config file
920 * @return 0 if file was not found, 1 otherwise
922 static int try_load_config(m_config_t *conf, const char *file)
924 struct stat st;
925 if (stat(file, &st))
926 return 0;
927 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Loading config '%s'\n", file);
928 m_config_parse_config_file (conf, file);
929 return 1;
932 static void load_per_file_config (m_config_t* conf, const char *const file)
934 char *confpath;
935 char cfg[PATH_MAX];
936 char *name;
938 if (strlen(file) > PATH_MAX - 14) {
939 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Filename is too long, can not load file or directory specific config files\n");
940 return;
942 sprintf (cfg, "%s.conf", file);
944 name = strrchr(cfg, '/');
945 if (HAVE_DOS_PATHS) {
946 char *tmp = strrchr(cfg, '\\');
947 if (!name || tmp > name)
948 name = tmp;
949 tmp = strrchr(cfg, ':');
950 if (!name || tmp > name)
951 name = tmp;
953 if (!name)
954 name = cfg;
955 else
956 name++;
958 if (use_filedir_conf) {
959 char dircfg[PATH_MAX];
960 strcpy(dircfg, cfg);
961 strcpy(dircfg + (name - cfg), "mplayer.conf");
962 try_load_config(conf, dircfg);
964 if (try_load_config(conf, cfg))
965 return;
968 if ((confpath = get_path (name)) != NULL)
970 try_load_config(conf, confpath);
972 free (confpath);
976 /* When libmpdemux performs a blocking operation (network connection or
977 * cache filling) if the operation fails we use this function to check
978 * if it was interrupted by the user.
979 * The function returns a new value for eof. */
980 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
982 mp_cmd_t* cmd;
983 if((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
984 switch(cmd->id) {
985 case MP_CMD_QUIT:
986 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
987 case MP_CMD_PLAY_TREE_STEP: {
988 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
989 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
990 } break;
991 case MP_CMD_PLAY_TREE_UP_STEP: {
992 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
993 } break;
994 case MP_CMD_PLAY_ALT_SRC_STEP: {
995 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
996 } break;
998 mp_cmd_free(cmd);
1000 return stop_play;
1003 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
1005 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
1007 play_tree_add_bpf(entry,mpctx->filename);
1010 if(!entry) {
1011 entry = mpctx->playtree_iter->tree;
1012 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1013 return PT_NEXT_ENTRY;
1015 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
1016 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1017 return PT_NEXT_ENTRY;
1020 play_tree_remove(entry,1,1);
1021 return PT_NEXT_SRC;
1023 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
1024 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1025 entry = mpctx->playtree_iter->tree;
1026 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1027 return PT_NEXT_ENTRY;
1029 play_tree_remove(entry,1,1);
1031 return PT_NEXT_SRC;
1034 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1036 sub_data *subd;
1037 #ifdef CONFIG_ASS
1038 ASS_Track *asst = 0;
1039 #endif
1041 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1042 return;
1045 subd = sub_read_file(filename, fps);
1046 #ifdef CONFIG_ASS
1047 if (ass_enabled)
1048 #ifdef CONFIG_ICONV
1049 asst = ass_read_file(ass_library, filename, sub_cp);
1050 #else
1051 asst = ass_read_file(ass_library, filename, 0);
1052 #endif
1053 if (ass_enabled && subd && !asst)
1054 asst = ass_read_subdata(ass_library, subd, fps);
1056 if (!asst && !subd)
1057 #else
1058 if(!subd)
1059 #endif
1060 mp_tmsg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR, "Cannot load subtitles: %s\n",
1061 filename_recode(filename));
1062 #if defined(_WIN32) && !defined(HAVE_NEW_GUI)
1063 if(filename) {
1064 static char message[MAX_PATH + 1];
1065 char *s = strrchr(filename, '\\');
1066 if (!s) s = strrchr(filename, '/');
1067 if (s) s++; else s = filename;
1068 message[0] = 0;
1069 snprintf(message, MAX_PATH, "MPlayer: %s", filename_recode(s));
1070 message[MAX_PATH] = 0;
1071 SetConsoleTitle(message);
1073 #endif
1075 #ifdef CONFIG_ASS
1076 if (!asst && !subd) return;
1077 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1078 #else
1079 if (!subd) return;
1080 #endif
1081 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1082 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1083 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1084 filename_recode(filename));
1085 ++mpctx->set_of_sub_size;
1086 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "SUB: Added subtitle file (%d): %s\n", mpctx->set_of_sub_size,
1087 filename_recode(filename));
1090 void init_vo_spudec(struct MPContext *mpctx)
1092 if (vo_spudec)
1093 spudec_free(vo_spudec);
1094 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1095 vo_spudec = NULL;
1096 if (spudec_ifo) {
1097 unsigned int palette[16], width, height;
1098 current_module="spudec_init_vobsub";
1099 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1100 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1103 #ifdef CONFIG_DVDREAD
1104 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1105 current_module="spudec_init_dvdread";
1106 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1107 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1108 NULL, 0);
1110 #endif
1112 #ifdef CONFIG_DVDNAV
1113 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1114 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1115 current_module="spudec_init_dvdnav";
1116 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1118 #endif
1120 if (vo_spudec==NULL) {
1121 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1122 current_module="spudec_init_normal";
1123 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1124 spudec_set_font_factor(vo_spudec,font_factor);
1127 if (vo_spudec!=NULL) {
1128 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1129 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1134 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1135 * make it all work is to use the builtin SDL-bootstrap code, which
1136 * will be done automatically by replacing our main() if we include SDL.h.
1138 #if defined(__APPLE__) && defined(CONFIG_SDL)
1139 #include <SDL.h>
1140 #endif
1143 * \brief append a formatted string
1144 * \param buf buffer to print into
1145 * \param pos position of terminating 0 in buf
1146 * \param len maximum number of characters in buf, not including terminating 0
1147 * \param format printf format string
1149 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1151 va_list va;
1152 va_start(va, format);
1153 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1154 va_end(va);
1155 if (*pos >= len ) {
1156 buf[len] = 0;
1157 *pos = len;
1162 * \brief append time in the hh:mm:ss.f format
1163 * \param buf buffer to print into
1164 * \param pos position of terminating 0 in buf
1165 * \param len maximum number of characters in buf, not including terminating 0
1166 * \param time time value to convert/append
1168 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1169 long tenths = 10 * time;
1170 int f1 = tenths % 10;
1171 int ss = (tenths / 10) % 60;
1172 int mm = (tenths / 600) % 60;
1173 int hh = tenths / 36000;
1174 if (time <= 0) {
1175 saddf(buf, pos, len, "unknown");
1176 return;
1178 if (hh > 0)
1179 saddf(buf, pos, len, "%2d:", hh);
1180 if (hh > 0 || mm > 0)
1181 saddf(buf, pos, len, "%02d:", mm);
1182 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1185 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1187 struct MPOpts *opts = &mpctx->opts;
1188 sh_video_t * const sh_video = mpctx->sh_video;
1190 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1191 a_pos = playing_audio_pts(mpctx);
1192 if (mpctx->sh_audio && sh_video && at_frame) {
1193 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1194 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1195 && !mpctx->drop_message_shown) {
1196 mp_tmsg(MSGT_AVSYNC,MSGL_WARN,SystemTooSlow);
1197 mpctx->drop_message_shown = true;
1200 if (quiet)
1201 return;
1204 int width;
1205 char *line;
1206 unsigned pos = 0;
1207 get_screen_size();
1208 if (screen_width > 0)
1209 width = screen_width;
1210 else
1211 width = 80;
1212 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1213 /* Windows command line is broken (MinGW's rxvt works, but we
1214 * should not depend on that). */
1215 width--;
1216 #endif
1217 line = malloc(width + 1); // one additional char for the terminating null
1219 // Audio time
1220 if (mpctx->sh_audio) {
1221 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1222 if (!sh_video) {
1223 float len = demuxer_get_time_length(mpctx->demuxer);
1224 saddf(line, &pos, width, "(");
1225 sadd_hhmmssf(line, &pos, width, a_pos);
1226 saddf(line, &pos, width, ") of %.1f (", len);
1227 sadd_hhmmssf(line, &pos, width, len);
1228 saddf(line, &pos, width, ") ");
1232 // Video time
1233 if (sh_video)
1234 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1236 // A-V sync
1237 if (mpctx->sh_audio && sh_video)
1238 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1239 mpctx->last_av_difference, mpctx->total_avsync_change);
1241 // Video stats
1242 if (sh_video)
1243 saddf(line, &pos, width, "%3d/%3d ",
1244 (int)sh_video->num_frames,
1245 (int)sh_video->num_frames_decoded);
1247 // CPU usage
1248 if (sh_video) {
1249 if (sh_video->timer > 0.5)
1250 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1251 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1252 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1253 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1254 else
1255 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1256 } else if (mpctx->sh_audio) {
1257 if (mpctx->delay > 0.5)
1258 saddf(line, &pos, width, "%4.1f%% ",
1259 100.0*audio_time_usage/(double)mpctx->delay);
1260 else
1261 saddf(line, &pos, width, "??,?%% ");
1264 // VO stats
1265 if (sh_video)
1266 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1268 #ifdef CONFIG_STREAM_CACHE
1269 // cache stats
1270 if (stream_cache_size > 0)
1271 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1272 #endif
1274 // other
1275 if (opts->playback_speed != 1)
1276 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1278 // end
1279 if (erase_to_end_of_line) {
1280 line[pos] = 0;
1281 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1282 } else {
1283 memset(&line[pos], ' ', width - pos);
1284 line[width] = 0;
1285 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1287 free(line);
1291 * \brief build a chain of audio filters that converts the input format
1292 * to the ao's format, taking into account the current playback_speed.
1293 * \param sh_audio describes the requested input format of the chain.
1294 * \param ao_data describes the requested output format of the chain.
1296 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1298 struct MPOpts *opts = &mpctx->opts;
1299 int new_srate;
1300 int result;
1301 if (!sh_audio)
1303 mpctx->mixer.afilter = NULL;
1304 return 0;
1306 if(af_control_any_rev(sh_audio->afilter,
1307 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1308 &opts->playback_speed)) {
1309 new_srate = sh_audio->samplerate;
1310 } else {
1311 new_srate = sh_audio->samplerate * opts->playback_speed;
1312 if (new_srate != ao_data->samplerate) {
1313 // limits are taken from libaf/af_resample.c
1314 if (new_srate < 8000)
1315 new_srate = 8000;
1316 if (new_srate > 192000)
1317 new_srate = 192000;
1318 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1321 result = init_audio_filters(sh_audio, new_srate,
1322 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1323 mpctx->mixer.afilter = sh_audio->afilter;
1324 return result;
1328 typedef struct mp_osd_msg mp_osd_msg_t;
1329 struct mp_osd_msg {
1330 /// Previous message on the stack.
1331 mp_osd_msg_t* prev;
1332 /// Message text.
1333 char msg[128];
1334 int id,level,started;
1335 /// Display duration in ms.
1336 unsigned time;
1339 /// OSD message stack.
1340 static mp_osd_msg_t* osd_msg_stack = NULL;
1343 * \brief Add a message on the OSD message stack
1345 * If a message with the same id is already present in the stack
1346 * it is pulled on top of the stack, otherwise a new message is created.
1350 void set_osd_msg(int id, int level, int time, const char* fmt, ...) {
1351 mp_osd_msg_t *msg,*last=NULL;
1352 va_list va;
1353 int r;
1355 // look if the id is already in the stack
1356 for(msg = osd_msg_stack ; msg && msg->id != id ;
1357 last = msg, msg = msg->prev);
1358 // not found: alloc it
1359 if(!msg) {
1360 msg = calloc(1,sizeof(mp_osd_msg_t));
1361 msg->prev = osd_msg_stack;
1362 osd_msg_stack = msg;
1363 } else if(last) { // found, but it's not on top of the stack
1364 last->prev = msg->prev;
1365 msg->prev = osd_msg_stack;
1366 osd_msg_stack = msg;
1368 // write the msg
1369 va_start(va,fmt);
1370 r = vsnprintf(msg->msg, 128, fmt, va);
1371 va_end(va);
1372 if(r >= 128) msg->msg[127] = 0;
1373 // set id and time
1374 msg->id = id;
1375 msg->level = level;
1376 msg->time = time;
1381 * \brief Remove a message from the OSD stack
1383 * This function can be used to get rid of a message right away.
1387 void rm_osd_msg(int id) {
1388 mp_osd_msg_t *msg,*last=NULL;
1390 // Search for the msg
1391 for(msg = osd_msg_stack ; msg && msg->id != id ;
1392 last = msg, msg = msg->prev);
1393 if(!msg) return;
1395 // Detach it from the stack and free it
1396 if(last)
1397 last->prev = msg->prev;
1398 else
1399 osd_msg_stack = msg->prev;
1400 free(msg);
1404 * \brief Remove all messages from the OSD stack
1408 static void clear_osd_msgs(void) {
1409 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1410 while(msg) {
1411 prev = msg->prev;
1412 free(msg);
1413 msg = prev;
1415 osd_msg_stack = NULL;
1419 * \brief Get the current message from the OSD stack.
1421 * This function decrements the message timer and destroys the old ones.
1422 * The message that should be displayed is returned (if any).
1426 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1428 struct MPOpts *opts = &mpctx->opts;
1429 mp_osd_msg_t *msg,*prev,*last = NULL;
1430 static unsigned last_update = 0;
1431 unsigned now = GetTimerMS();
1432 unsigned diff;
1433 char hidden_dec_done = 0;
1435 if (mpctx->osd_visible) {
1436 // 36000000 means max timed visibility is 1 hour into the future, if
1437 // the difference is greater assume it's wrapped around from below 0
1438 if (mpctx->osd_visible - now > 36000000) {
1439 mpctx->osd_visible = 0;
1440 vo_osd_progbar_type = -1; // disable
1441 vo_osd_changed(OSDTYPE_PROGBAR);
1442 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1445 if (mpctx->osd_show_percentage_until - now > 36000000)
1446 mpctx->osd_show_percentage_until = 0;
1448 if(!last_update) last_update = now;
1449 diff = now >= last_update ? now - last_update : 0;
1451 last_update = now;
1453 // Look for the first message in the stack with high enough level.
1454 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1455 prev = msg->prev;
1456 if (msg->level > opts->osd_level && hidden_dec_done)
1457 continue;
1458 // The message has a high enough level or it is the first hidden one
1459 // in both cases we decrement the timer or kill it.
1460 if(!msg->started || msg->time > diff) {
1461 if(msg->started) msg->time -= diff;
1462 else msg->started = 1;
1463 // display it
1464 if (msg->level <= opts->osd_level)
1465 return msg;
1466 hidden_dec_done = 1;
1467 continue;
1469 // kill the message
1470 free(msg);
1471 if(last) {
1472 last->prev = prev;
1473 msg = last;
1474 } else {
1475 osd_msg_stack = prev;
1476 msg = NULL;
1479 // Nothing found
1480 return NULL;
1484 * \brief Display the OSD bar.
1486 * Display the OSD bar or fall back on a simple message.
1490 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1491 struct MPOpts *opts = &mpctx->opts;
1492 if (opts->osd_level < 1)
1493 return;
1495 if(mpctx->sh_video) {
1496 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1497 vo_osd_progbar_type = type;
1498 vo_osd_progbar_value = 256*(val-min)/(max-min);
1499 vo_osd_changed(OSDTYPE_PROGBAR);
1500 return;
1503 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1504 name, ROUND(100*(val-min)/(max-min)));
1508 * \brief Display text subtitles on the OSD
1510 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs)
1512 int i;
1513 vo_sub = subs;
1514 vo_osd_changed(OSDTYPE_SUBTITLE);
1515 if (!mpctx->sh_video) {
1516 // reverse order, since newest set_osd_msg is displayed first
1517 for (i = SUB_MAX_TEXT - 1; i >= 0; i--) {
1518 if (!subs || i >= subs->lines || !subs->text[i])
1519 rm_osd_msg(OSD_MSG_SUB_BASE + i);
1520 else {
1521 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1522 int display_time = i == subs->lines - 1 ? 180000 : 2000;
1523 set_osd_msg(OSD_MSG_SUB_BASE + i, 1, display_time, "%s", subs->text[i]);
1530 * \brief Update the OSD message line.
1532 * This function displays the current message on the vo OSD or on the term.
1533 * If the stack is empty and the OSD level is high enough the timer
1534 * is displayed (only on the vo OSD).
1538 static void update_osd_msg(struct MPContext *mpctx)
1540 struct MPOpts *opts = &mpctx->opts;
1541 mp_osd_msg_t *msg;
1542 struct osd_state *osd = mpctx->osd;
1543 char osd_text_timer[128];
1545 if (mpctx->add_osd_seek_info) {
1546 double percentage;
1547 if (mpctx->timeline && mpctx->sh_video)
1548 percentage = mpctx->sh_video->pts * 100 /
1549 mpctx->timeline[mpctx->num_timeline_parts].start;
1550 else
1551 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1552 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1553 if (mpctx->sh_video)
1554 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1555 mpctx->add_osd_seek_info = false;
1558 // Look if we have a msg
1559 if((msg = get_osd_msg(mpctx))) {
1560 if (strcmp(osd->osd_text, msg->msg)) {
1561 strncpy(osd->osd_text, msg->msg, 127);
1562 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1563 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1565 return;
1568 if(mpctx->sh_video) {
1569 // fallback on the timer
1570 if (opts->osd_level >= 2) {
1571 int len;
1572 if (mpctx->timeline)
1573 len = mpctx->timeline[mpctx->num_timeline_parts].start;
1574 else
1575 len = demuxer_get_time_length(mpctx->demuxer);
1576 int percentage = -1;
1577 char percentage_text[10];
1578 int pts = demuxer_get_current_time(mpctx->demuxer);
1580 if (mpctx->osd_show_percentage_until)
1581 if (mpctx->timeline)
1582 percentage = mpctx->sh_video->pts * 100 /
1583 mpctx->timeline[mpctx->num_timeline_parts].start;
1584 else
1585 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1587 if (percentage >= 0)
1588 snprintf(percentage_text, 9, " (%d%%)", percentage);
1589 else
1590 percentage_text[0] = 0;
1592 if (opts->osd_level == 3)
1593 snprintf(osd_text_timer, 63,
1594 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1595 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1596 len/3600,(len/60)%60,len%60,percentage_text);
1597 else
1598 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1599 mpctx->osd_function,pts/3600,(pts/60)%60,
1600 pts%60,percentage_text);
1601 } else
1602 osd_text_timer[0]=0;
1604 if (strcmp(osd->osd_text, osd_text_timer)) {
1605 strncpy(osd->osd_text, osd_text_timer, 63);
1606 vo_osd_changed(OSDTYPE_OSD);
1608 return;
1611 // Clear the term osd line
1612 if (term_osd && osd->osd_text[0]) {
1613 osd->osd_text[0] = 0;
1614 printf("%s\n",term_osd_esc);
1618 ///@}
1619 // OSDMsgStack
1622 void reinit_audio_chain(struct MPContext *mpctx)
1624 struct MPOpts *opts = &mpctx->opts;
1625 if(mpctx->sh_audio){
1626 current_module="init_audio_codec";
1627 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1628 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1629 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // failed to init :(
1630 mpctx->d_audio->id = -2;
1631 return;
1632 } else
1633 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1634 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1637 //const ao_info_t *info=audio_out->info;
1638 current_module="af_preinit";
1639 ao_data.samplerate=force_srate;
1640 ao_data.channels=0;
1641 ao_data.format=audio_output_format;
1642 #if 1
1643 // first init to detect best values
1644 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1645 // input:
1646 mpctx->sh_audio->samplerate,
1647 // output:
1648 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1649 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Error at audio filter chain pre-init!\n");
1650 exit_player(mpctx, EXIT_ERROR);
1652 #endif
1653 current_module="ao2_init";
1654 if(!(mpctx->audio_out=init_best_audio_out(opts->audio_driver_list,
1655 0, // plugin flag
1656 ao_data.samplerate,
1657 ao_data.channels,
1658 ao_data.format,0))){
1659 // FAILED:
1660 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Could not open/initialize audio device -> no sound.\n");
1661 uninit_player(mpctx, INITIALIZED_ACODEC); // close codec
1662 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1663 mpctx->d_audio->id = -2;
1664 return;
1665 } else {
1666 // SUCCESS:
1667 mpctx->initialized_flags|=INITIALIZED_AO;
1668 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1669 mpctx->audio_out->info->short_name,
1670 ao_data.samplerate, ao_data.channels,
1671 af_fmt2str_short(ao_data.format),
1672 af_fmt2bits(ao_data.format)/8 );
1673 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1674 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1675 if(strlen(mpctx->audio_out->info->comment) > 0)
1676 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1677 // init audio filters:
1678 #if 1
1679 current_module="af_init";
1680 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1681 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter/ao format!\n");
1682 // mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format! -> NOSOUND\n");
1683 // uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec & ao
1684 // sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1686 #endif
1688 mpctx->mixer.audio_out = mpctx->audio_out;
1689 mpctx->mixer.volstep = volstep;
1694 ///@}
1695 // Command2Property
1698 // Return pts value corresponding to the end point of audio written to the
1699 // ao so far.
1700 static double written_audio_pts(struct MPContext *mpctx)
1702 sh_audio_t *sh_audio = mpctx->sh_audio;
1703 demux_stream_t *d_audio = mpctx->d_audio;
1704 double buffered_output;
1705 // first calculate the end pts of audio that has been output by decoder
1706 double a_pts = sh_audio->pts;
1707 if (a_pts != MP_NOPTS_VALUE)
1708 // Good, decoder supports new way of calculating audio pts.
1709 // sh_audio->pts is the timestamp of the latest input packet with
1710 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1711 // the amount of bytes the decoder has written after that timestamp.
1712 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1713 else {
1714 // Decoder doesn't support new way of calculating pts (or we're
1715 // being called before it has decoded anything with known timestamp).
1716 // Use the old method of audio pts calculation: take the timestamp
1717 // of last packet with known pts the decoder has read data from,
1718 // and add amount of bytes read after the beginning of that packet
1719 // divided by input bps. This will be inaccurate if the input/output
1720 // ratio is not constant for every audio packet or if it is constant
1721 // but not accurately known in sh_audio->i_bps.
1723 a_pts = d_audio->pts;
1724 // ds_tell_pts returns bytes read after last timestamp from
1725 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1726 // it has read but not decoded
1727 if (sh_audio->i_bps)
1728 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1729 (double)sh_audio->i_bps;
1731 // Now a_pts hopefully holds the pts for end of audio from decoder.
1732 // Substract data in buffers between decoder and audio out.
1734 // Decoded but not filtered
1735 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1737 // Data buffered in audio filters, measured in bytes of "missing" output
1738 buffered_output = af_calc_delay(sh_audio->afilter);
1740 // Data that was ready for ao but was buffered because ao didn't fully
1741 // accept everything to internal buffers yet
1742 buffered_output += sh_audio->a_out_buffer_len;
1744 // Filters divide audio length by playback_speed, so multiply by it
1745 // to get the length in original units without speedup or slowdown
1746 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1748 return a_pts + mpctx->video_offset;
1751 // Return pts value corresponding to currently playing audio.
1752 double playing_audio_pts(struct MPContext *mpctx)
1754 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1755 mpctx->audio_out->get_delay();
1758 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1759 struct MPOpts *opts = &mpctx->opts;
1760 // check for frame-drop:
1761 current_module = "check_framedrop";
1762 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1763 static int dropped_frames;
1764 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1765 float d = delay-mpctx->delay;
1766 ++total_frame_cnt;
1767 // we should avoid dropping too many frames in sequence unless we
1768 // are too late. and we allow 100ms A-V delay here:
1769 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1770 && !mpctx->update_video_immediately) {
1771 ++drop_frame_cnt;
1772 ++dropped_frames;
1773 return frame_dropping;
1774 } else
1775 dropped_frames = 0;
1777 return 0;
1781 #ifdef HAVE_RTC
1782 int rtc_fd = -1;
1783 #endif
1785 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1787 #ifdef HAVE_RTC
1788 if (rtc_fd >= 0){
1789 // -------- RTC -----------
1790 current_module="sleep_rtc";
1791 while (time_frame > 0.000) {
1792 unsigned long rtc_ts;
1793 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1794 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC read error: %s\n", strerror(errno));
1795 time_frame -= get_relative_time(mpctx);
1797 } else
1798 #endif
1800 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1801 // unnecessarily high CPU usage
1802 float margin = softsleep ? 0.011 : 0;
1803 current_module = "sleep_timer";
1804 while (time_frame > margin) {
1805 usec_sleep(1000000 * (time_frame - margin));
1806 time_frame -= get_relative_time(mpctx);
1808 if (softsleep){
1809 current_module = "sleep_soft";
1810 if (time_frame < 0)
1811 mp_tmsg(MSGT_AVSYNC, MSGL_WARN, "Warning! Softsleep underflow!\n");
1812 while (time_frame > 0)
1813 time_frame -= get_relative_time(mpctx); // burn the CPU
1816 return time_frame;
1819 #ifdef CONFIG_DVDNAV
1820 #ifndef FF_B_TYPE
1821 #define FF_B_TYPE 3
1822 #endif
1823 /// store decoded video image
1824 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1825 mp_image_t *from_mpi) {
1826 mp_image_t *mpi;
1828 /// Do not store B-frames
1829 if (from_mpi->pict_type == FF_B_TYPE)
1830 return to_mpi;
1832 if (to_mpi &&
1833 to_mpi->w == from_mpi->w &&
1834 to_mpi->h == from_mpi->h &&
1835 to_mpi->imgfmt == from_mpi->imgfmt)
1836 mpi = to_mpi;
1837 else {
1838 if (to_mpi)
1839 free_mp_image(to_mpi);
1840 if (from_mpi->w == 0 || from_mpi->h == 0)
1841 return NULL;
1842 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1845 copy_mpi(mpi,from_mpi);
1846 return mpi;
1849 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1850 struct MPOpts *opts = &ctx->opts;
1851 if (ctx->sh_video) {
1852 /// clear video pts
1853 ctx->d_video->pts = 0.0f;
1854 ctx->sh_video->pts = 0.0f;
1855 ctx->sh_video->i_pts = 0.0f;
1856 ctx->sh_video->last_pts = 0.0f;
1857 ctx->sh_video->num_buffered_pts = 0;
1858 ctx->sh_video->num_frames = 0;
1859 ctx->sh_video->num_frames_decoded = 0;
1860 ctx->sh_video->timer = 0.0f;
1861 ctx->sh_video->stream_delay = 0.0f;
1862 ctx->sh_video->timer = 0;
1863 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1866 if (ctx->sh_audio) {
1867 /// free audio packets and reset
1868 ds_free_packs(ctx->d_audio);
1869 audio_delay -= ctx->sh_audio->stream_delay;
1870 ctx->delay =- audio_delay;
1871 ctx->audio_out->reset();
1872 resync_audio_stream(ctx->sh_audio);
1875 if (ctx->d_sub) opts->sub_id = -2;
1877 audio_delay = 0.0f;
1879 /// clear all EOF related flags
1880 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
1883 /// Restore last decoded DVDNAV (still frame)
1884 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
1885 int *in_size,
1886 unsigned char **start,
1887 mp_image_t *decoded_frame)
1889 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1890 return decoded_frame;
1892 /// a change occured in dvdnav stream
1893 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
1894 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
1895 mp_dvdnav_context_free(mpctx);
1896 mp_dvdnav_reset_stream(mpctx);
1897 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
1898 mp_dvdnav_cell_has_changed(mpctx->stream,1);
1901 if (*in_size < 0) {
1902 float len;
1904 /// Display still frame, if any
1905 if (mpctx->nav_smpi && !mpctx->nav_buffer)
1906 decoded_frame = mpctx->nav_smpi;
1908 /// increment video frame : continue playing after still frame
1909 len = demuxer_get_time_length(mpctx->demuxer);
1910 if (mpctx->sh_video->pts >= len &&
1911 mpctx->sh_video->pts > 0.0 && len > 0.0) {
1912 mp_dvdnav_skip_still(mpctx->stream);
1913 mp_dvdnav_skip_wait(mpctx->stream);
1915 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
1917 if (mpctx->nav_buffer) {
1918 *start = mpctx->nav_start;
1919 *in_size = mpctx->nav_in_size;
1920 if (mpctx->nav_start)
1921 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
1925 return decoded_frame;
1928 /// Save last decoded DVDNAV (still frame)
1929 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
1930 unsigned char *start,
1931 mp_image_t *decoded_frame)
1933 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
1934 return;
1936 if (mpctx->nav_buffer)
1937 free(mpctx->nav_buffer);
1939 mpctx->nav_buffer = malloc(in_size);
1940 mpctx->nav_start = start;
1941 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
1942 if (mpctx->nav_buffer)
1943 memcpy(mpctx->nav_buffer,start,in_size);
1945 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
1946 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
1948 #endif /* CONFIG_DVDNAV */
1950 /* Modify video timing to match the audio timeline. There are two main
1951 * reasons this is needed. First, video and audio can start from different
1952 * positions at beginning of file or after a seek (MPlayer starts both
1953 * immediately even if they have different pts). Second, the file can have
1954 * audio timestamps that are inconsistent with the duration of the audio
1955 * packets, for example two consecutive timestamp values differing by
1956 * one second but only a packet with enough samples for half a second
1957 * of playback between them.
1959 static void adjust_sync(struct MPContext *mpctx, double frame_time)
1961 struct MPOpts *opts = &mpctx->opts;
1962 current_module = "av_sync";
1964 if (!mpctx->sh_audio)
1965 return;
1967 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
1968 double v_pts = mpctx->sh_video->pts;
1969 double av_delay = a_pts - v_pts;
1970 // Try to sync vo_flip() so it will *finish* at given time
1971 av_delay += mpctx->last_vo_flip_duration;
1972 av_delay -= audio_delay; // This much pts difference is desired
1974 double change = av_delay * 0.1;
1975 double max_change = default_max_pts_correction >= 0 ?
1976 default_max_pts_correction : frame_time * 0.1;
1977 if (change < -max_change)
1978 change = -max_change;
1979 else if (change > max_change)
1980 change = max_change;
1981 mpctx->delay += change;
1982 mpctx->total_avsync_change += change;
1985 static int fill_audio_out_buffers(struct MPContext *mpctx)
1987 struct MPOpts *opts = &mpctx->opts;
1988 unsigned int t;
1989 double tt;
1990 int playsize;
1991 int playflags=0;
1992 int audio_eof=0;
1993 int bytes_to_write;
1994 sh_audio_t * const sh_audio = mpctx->sh_audio;
1996 current_module="play_audio";
1998 while (1) {
1999 int sleep_time;
2000 // all the current uses of ao_data.pts seem to be in aos that handle
2001 // sync completely wrong; there should be no need to use ao_data.pts
2002 // in get_space()
2003 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2004 bytes_to_write = mpctx->audio_out->get_space();
2005 if (mpctx->sh_video || bytes_to_write >= ao_data.outburst)
2006 break;
2008 // handle audio-only case:
2009 // this is where mplayer sleeps during audio-only playback
2010 // to avoid 100% CPU use
2011 sleep_time = (ao_data.outburst - bytes_to_write) * 1000 / ao_data.bps;
2012 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
2013 usec_sleep(sleep_time * 1000);
2016 while (bytes_to_write) {
2017 playsize = bytes_to_write;
2018 if (playsize > MAX_OUTBURST)
2019 playsize = MAX_OUTBURST;
2020 bytes_to_write -= playsize;
2022 // Fill buffer if needed:
2023 current_module="decode_audio";
2024 t = GetTimer();
2025 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2026 if (mpctx->d_audio->eof) {
2027 audio_eof = 1;
2028 if (sh_audio->a_out_buffer_len == 0)
2029 return 0;
2031 t = GetTimer() - t;
2032 tt = t*0.000001f; audio_time_usage+=tt;
2033 if (playsize > sh_audio->a_out_buffer_len) {
2034 playsize = sh_audio->a_out_buffer_len;
2035 if (audio_eof)
2036 playflags |= AOPLAY_FINAL_CHUNK;
2038 if (!playsize)
2039 break;
2041 // play audio:
2042 current_module="play_audio";
2044 // Is this pts value actually useful for the aos that access it?
2045 // They're obviously badly broken in the way they handle av sync;
2046 // would not having access to this make them more broken?
2047 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2048 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2050 if (playsize > 0) {
2051 sh_audio->a_out_buffer_len -= playsize;
2052 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2053 sh_audio->a_out_buffer_len);
2054 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2056 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2057 // Sanity check to avoid hanging in case current ao doesn't output
2058 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2059 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2060 sh_audio->a_out_buffer_len = 0;
2063 return 1;
2066 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2067 float *aq_sleep_time)
2069 struct MPOpts *opts = &mpctx->opts;
2070 int frame_time_remaining = 0;
2071 current_module="calc_sleep_time";
2073 *time_frame -= get_relative_time(mpctx); // reset timer
2075 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2076 float delay = mpctx->audio_out->get_delay();
2077 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2079 if (autosync) {
2081 * Adjust this raw delay value by calculating the expected
2082 * delay for this frame and generating a new value which is
2083 * weighted between the two. The higher autosync is, the
2084 * closer to the delay value gets to that which "-nosound"
2085 * would have used, and the longer it will take for A/V
2086 * sync to settle at the right value (but it eventually will.)
2087 * This settling time is very short for values below 100.
2089 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2090 float difference = delay - predicted;
2091 delay = predicted + difference / (float)autosync;
2094 *time_frame = delay - mpctx->delay / opts->playback_speed;
2096 // delay = amount of audio buffered in soundcard/driver
2097 if (delay > 0.25) delay=0.25; else
2098 if (delay < 0.10) delay=0.10;
2099 if (*time_frame > delay*0.6) {
2100 // sleep time too big - may cause audio drops (buffer underrun)
2101 frame_time_remaining = 1;
2102 *time_frame = delay*0.5;
2104 } else {
2105 // If we're lagging more than 200 ms behind the right playback rate,
2106 // don't try to "catch up".
2107 // If benchmark is set always output frames as fast as possible
2108 // without sleeping.
2109 if (*time_frame < -0.2 || benchmark)
2110 *time_frame = 0;
2113 *aq_sleep_time += *time_frame;
2116 //============================== SLEEP: ===================================
2118 // flag 256 means: libvo driver does its timing (dvb card)
2119 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2120 *time_frame = timing_sleep(mpctx, *time_frame);
2121 return frame_time_remaining;
2124 int reinit_video_chain(struct MPContext *mpctx)
2126 struct MPOpts *opts = &mpctx->opts;
2127 sh_video_t * const sh_video = mpctx->sh_video;
2128 double ar=-1.0;
2129 //================== Init VIDEO (codec & libvo) ==========================
2130 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2131 current_module="preinit_libvo";
2133 //shouldn't we set dvideo->id=-2 when we fail?
2134 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2135 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2136 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Error opening/initializing the selected video_out (-vo) device.\n");
2137 goto err_out;
2139 mpctx->initialized_flags|=INITIALIZED_VO;
2142 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2143 mpctx->sh_video->stream_aspect = ar;
2144 current_module="init_video_filters";
2146 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2147 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2149 #ifdef CONFIG_MENU
2150 if(use_menu) {
2151 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2152 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2153 if(!vf_menu) {
2154 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Can't open libmenu video filter with root menu %s.\n",menu_root);
2155 use_menu = 0;
2158 if(vf_menu)
2159 sh_video->vfilter = vf_menu;
2160 #endif
2162 #ifdef CONFIG_ASS
2163 if(ass_enabled) {
2164 int i;
2165 int insert = 1;
2166 if (opts->vf_settings)
2167 for (i = 0; opts->vf_settings[i].name; ++i)
2168 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2169 insert = 0;
2170 break;
2172 if (insert) {
2173 extern vf_info_t vf_info_ass;
2174 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2175 char* vf_arg[] = {"auto", "1", NULL};
2176 vf_instance_t* vf_ass = vf_open_plugin(opts, libass_vfs,sh_video->vfilter,"ass",vf_arg);
2177 if (vf_ass)
2178 sh_video->vfilter = vf_ass;
2179 else
2180 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2183 #endif
2185 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2187 #ifdef CONFIG_ASS
2188 if (ass_enabled)
2189 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2190 #endif
2192 current_module="init_video_codec";
2194 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2195 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2196 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2198 if(!sh_video->initialized){
2199 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2200 goto err_out;
2203 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2205 if (sh_video->codec)
2206 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2208 sh_video->last_pts = MP_NOPTS_VALUE;
2209 sh_video->num_buffered_pts = 0;
2210 sh_video->next_frame_time = 0;
2212 if(auto_quality>0){
2213 // Auto quality option enabled
2214 output_quality=get_video_quality_max(sh_video);
2215 if(auto_quality>output_quality) auto_quality=output_quality;
2216 else output_quality=auto_quality;
2217 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2218 set_video_quality(sh_video,output_quality);
2221 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2223 current_module="init_vo";
2225 return 1;
2227 err_out:
2228 mpctx->sh_video = mpctx->d_video->sh = NULL;
2229 return 0;
2232 static double update_video_nocorrect_pts(struct MPContext *mpctx,
2233 int *blit_frame)
2235 struct sh_video *sh_video = mpctx->sh_video;
2236 *blit_frame = 0;
2237 double frame_time = 0;
2238 while (1) {
2239 current_module = "filter_video";
2240 // In nocorrect-pts mode there is no way to properly time these frames
2241 if (vf_output_queued_frame(sh_video->vfilter))
2242 break;
2243 unsigned char *packet = NULL;
2244 frame_time = sh_video->next_frame_time;
2245 if (mpctx->update_video_immediately)
2246 frame_time = 0;
2247 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2248 &packet, force_fps);
2249 if (in_size < 0) {
2250 #ifdef CONFIG_DVDNAV
2251 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2252 if (mp_dvdnav_is_eof(mpctx->stream))
2253 return -1;
2254 if (mpctx->d_video)
2255 mpctx->d_video->eof = 0;
2256 if (mpctx->d_audio)
2257 mpctx->d_audio->eof = 0;
2258 mpctx->stream->eof = 0;
2259 } else
2260 #endif
2261 return -1;
2263 if (in_size > max_framesize)
2264 max_framesize = in_size;
2265 sh_video->timer += frame_time;
2266 if (mpctx->sh_audio)
2267 mpctx->delay -= frame_time;
2268 // video_read_frame can change fps (e.g. for ASF video)
2269 vo_fps = sh_video->fps;
2270 int framedrop_type = check_framedrop(mpctx, frame_time);
2271 current_module = "decode video";
2273 void *decoded_frame;
2274 #ifdef CONFIG_DVDNAV
2275 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2276 if (in_size >= 0 && !decoded_frame)
2277 #endif
2278 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2279 sh_video->pts);
2280 #ifdef CONFIG_DVDNAV
2281 // Save last still frame for future display
2282 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2283 #endif
2284 if (decoded_frame) {
2285 // These updates are done here for vf_expand OSD/subtitles
2286 update_subtitles(mpctx, &mpctx->opts, sh_video, sh_video->pts,
2287 mpctx->video_offset, mpctx->d_sub, 0);
2288 update_teletext(sh_video, mpctx->demuxer, 0);
2289 update_osd_msg(mpctx);
2290 current_module = "filter video";
2291 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2292 break;
2295 *blit_frame = 1;
2296 return frame_time;
2299 static double update_video(struct MPContext *mpctx, int *blit_frame)
2301 struct sh_video *sh_video = mpctx->sh_video;
2302 struct vo *video_out = mpctx->video_out;
2303 *blit_frame = 0;
2304 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2305 mpctx->osd); // hack for vf_expand
2306 if (!mpctx->opts.correct_pts)
2307 return update_video_nocorrect_pts(mpctx, blit_frame);
2309 double pts;
2311 bool hit_eof = false;
2312 while (!video_out->frame_loaded) {
2313 current_module = "filter_video";
2314 if (vo_get_buffered_frame(video_out, hit_eof) >= 0)
2315 break;
2316 if (hit_eof)
2317 return -1;
2318 // XXX Time used in this call is not counted in any performance
2319 // timer now, OSD time is not updated correctly for filter-added frames
2320 if (vf_output_queued_frame(sh_video->vfilter))
2321 break;
2322 unsigned char *packet = NULL;
2323 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2324 if (pts != MP_NOPTS_VALUE)
2325 pts += mpctx->video_offset;
2326 if (in_size < 0) {
2327 // try to extract last frames in case of decoder lag
2328 in_size = 0;
2329 pts = 1e300;
2330 hit_eof = true;
2332 if (in_size > max_framesize)
2333 max_framesize = in_size;
2334 current_module = "decode video";
2335 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2336 void *decoded_frame = decode_video(sh_video, packet, in_size,
2337 framedrop_type, pts);
2338 if (decoded_frame) {
2339 // These updates are done here for vf_expand OSD/subtitles
2340 update_subtitles(mpctx, &mpctx->opts, sh_video, sh_video->pts,
2341 mpctx->video_offset, mpctx->d_sub, 0);
2342 update_teletext(sh_video, mpctx->demuxer, 0);
2343 update_osd_msg(mpctx);
2344 current_module = "filter video";
2345 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2346 if (!video_out->config_ok)
2347 break; // We'd likely hang in this loop otherwise
2351 pts = video_out->next_pts;
2352 if (pts == MP_NOPTS_VALUE) {
2353 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2354 // Try to use decoder pts from before filters
2355 pts = sh_video->pts;
2356 if (pts == MP_NOPTS_VALUE)
2357 pts = sh_video->last_pts;
2359 sh_video->pts = pts;
2360 if (sh_video->last_pts == MP_NOPTS_VALUE)
2361 sh_video->last_pts = sh_video->pts;
2362 else if (sh_video->last_pts > sh_video->pts) {
2363 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2364 sh_video->pts, sh_video->last_pts);
2365 /* If the difference in pts is small treat it as jitter around the
2366 * right value (possibly caused by incorrect timestamp ordering) and
2367 * just show this frame immediately after the last one.
2368 * Treat bigger differences as timestamp resets and start counting
2369 * timing of later frames from the position of this one. */
2370 if (sh_video->last_pts - sh_video->pts > 0.5)
2371 sh_video->last_pts = sh_video->pts;
2372 else
2373 sh_video->pts = sh_video->last_pts;
2375 double frame_time = sh_video->pts - sh_video->last_pts;
2376 sh_video->last_pts = sh_video->pts;
2377 sh_video->timer += frame_time;
2378 if (mpctx->sh_audio)
2379 mpctx->delay -= frame_time;
2380 *blit_frame = 1;
2381 return frame_time;
2384 void pause_player(struct MPContext *mpctx)
2386 if (mpctx->paused)
2387 return;
2388 mpctx->paused = 1;
2389 mpctx->step_frames = 0;
2390 mpctx->time_frame -= get_relative_time(mpctx);
2392 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2393 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2395 if (mpctx->audio_out && mpctx->sh_audio)
2396 mpctx->audio_out->pause(); // pause audio, keep data if possible
2399 void unpause_player(struct MPContext *mpctx)
2401 if (!mpctx->paused)
2402 return;
2403 mpctx->paused = 0;
2405 if (mpctx->audio_out && mpctx->sh_audio)
2406 mpctx->audio_out->resume(); // resume audio
2407 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2408 && !mpctx->step_frames)
2409 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2410 (void)get_relative_time(mpctx); // ignore time that passed during pause
2413 void add_step_frame(struct MPContext *mpctx)
2415 mpctx->step_frames++;
2416 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2417 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2418 unpause_player(mpctx);
2421 static void pause_loop(struct MPContext *mpctx)
2423 mp_cmd_t* cmd;
2424 if (!quiet) {
2425 // Small hack to display the pause message on the OSD line.
2426 // The pause string is: "\n == PAUSE == \r" so we need to
2427 // take the first and the last char out
2428 if (term_osd && !mpctx->sh_video) {
2429 char msg[128] = _("\n ===== PAUSE =====\r");
2430 int mlen = strlen(msg);
2431 msg[mlen-1] = '\0';
2432 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2433 update_osd_msg(mpctx);
2434 } else
2435 mp_tmsg(MSGT_CPLAYER,MSGL_STATUS,"\n ===== PAUSE =====\r");
2436 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2439 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1, 1)) == NULL
2440 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2441 if (cmd) {
2442 cmd = mp_input_get_cmd(mpctx->input, 0,1,0);
2443 run_command(mpctx, cmd);
2444 mp_cmd_free(cmd);
2445 continue;
2447 if (mpctx->sh_video && mpctx->video_out)
2448 vo_check_events(mpctx->video_out);
2449 #ifdef CONFIG_MENU
2450 if (vf_menu)
2451 vf_menu_pause_update(vf_menu);
2452 #endif
2453 usec_sleep(20000);
2454 update_osd_msg(mpctx);
2455 int hack = vo_osd_changed(0);
2456 vo_osd_changed(hack);
2457 if (hack)
2458 break;
2463 // Find the right mute status and record position for new file position
2464 static void edl_seek_reset(MPContext *mpctx)
2466 mpctx->edl_muted = 0;
2467 next_edl_record = edl_records;
2469 while (next_edl_record) {
2470 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2471 break;
2473 if (next_edl_record->action == EDL_MUTE)
2474 mpctx->edl_muted = !mpctx->edl_muted;
2475 next_edl_record = next_edl_record->next;
2477 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2478 mixer_mute(&mpctx->mixer);
2482 // Execute EDL command for the current position if one exists
2483 static void edl_update(MPContext *mpctx)
2485 if (!next_edl_record)
2486 return;
2488 if (!mpctx->sh_video) {
2489 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot use EDL without video, disabling.\n");
2490 free_edl(edl_records);
2491 next_edl_record = NULL;
2492 edl_records = NULL;
2493 return;
2496 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2497 if (next_edl_record->action == EDL_SKIP) {
2498 mpctx->osd_function = OSD_FFW;
2499 mpctx->abs_seek_pos = 0;
2500 mpctx->rel_seek_secs = next_edl_record->length_sec;
2501 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2502 "[%f], length [%f]\n", next_edl_record->start_sec,
2503 next_edl_record->stop_sec, next_edl_record->length_sec);
2505 else if (next_edl_record->action == EDL_MUTE) {
2506 mpctx->edl_muted = !mpctx->edl_muted;
2507 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2508 mixer_mute(&mpctx->mixer);
2509 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2510 next_edl_record->start_sec );
2512 next_edl_record = next_edl_record->next;
2516 static void reinit_decoders(struct MPContext *mpctx)
2518 reinit_video_chain(mpctx);
2519 reinit_audio_chain(mpctx);
2520 mp_property_do("sub", M_PROPERTY_SET, &mpctx->global_sub_pos, mpctx);
2523 static bool timeline_set_part(struct MPContext *mpctx, int i)
2525 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
2526 struct timeline_part *n = mpctx->timeline + i;
2527 mpctx->timeline_part = i;
2528 mpctx->video_offset = n->start - n->source_start;
2529 if (n->source == p->source)
2530 return false;
2531 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo && mpctx->opts.video_id != -2 ? 0 : INITIALIZED_VO) | INITIALIZED_AO | INITIALIZED_ACODEC);
2532 mpctx->demuxer = n->source->demuxer;
2533 mpctx->d_video = mpctx->demuxer->video;
2534 mpctx->d_audio = mpctx->demuxer->audio;
2535 mpctx->d_sub = mpctx->demuxer->sub;
2536 mpctx->sh_video = mpctx->d_video->sh;
2537 mpctx->sh_audio = mpctx->d_audio->sh;
2538 return true;
2541 // Given pts, switch playback to the corresponding part.
2542 // Return offset within that part.
2543 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
2544 bool *need_reset)
2546 if (pts < 0)
2547 pts = 0;
2548 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
2549 struct timeline_part *p = mpctx->timeline + i;
2550 if (pts < (p+1)->start) {
2551 *need_reset = timeline_set_part(mpctx, i);
2552 return pts - p->start + p->source_start;
2555 return -1;
2559 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2560 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2561 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2562 static int seek(MPContext *mpctx, double amount, int style)
2564 current_module = "seek";
2565 if (mpctx->stop_play == AT_END_OF_FILE)
2566 mpctx->stop_play = KEEP_PLAYING;
2567 if (mpctx->timeline && style & SEEK_FACTOR) {
2568 amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
2569 style &= ~SEEK_FACTOR;
2571 if ((mpctx->demuxer->accurate_seek || mpctx->timeline) && mpctx->sh_video
2572 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2573 style |= SEEK_ABSOLUTE;
2574 if (amount > 0)
2575 style |= SEEK_FORWARD;
2576 else
2577 style |= SEEK_BACKWARD;
2578 amount += mpctx->sh_video->pts;
2581 /* At least the liba52 decoder wants to read from the input stream
2582 * during initialization, so reinit must be done after the demux_seek()
2583 * call that clears possible stream EOF. */
2584 bool need_reset = false;
2585 if (mpctx->timeline) {
2586 amount = timeline_set_from_time(mpctx, amount, &need_reset);
2587 if (amount == -1) {
2588 mpctx->stop_play = AT_END_OF_FILE;
2589 // Clear audio from current position
2590 if (mpctx->sh_audio) {
2591 mpctx->audio_out->reset();
2592 mpctx->sh_audio->a_buffer_len = 0;
2593 mpctx->sh_audio->a_out_buffer_len = 0;
2595 return -1;
2598 int seekresult = demux_seek(mpctx->demuxer, amount, audio_delay, style);
2599 if (need_reset)
2600 reinit_decoders(mpctx);
2601 if (seekresult == 0)
2602 return -1;
2604 if (mpctx->sh_video) {
2605 current_module = "seek_video_reset";
2606 resync_video_stream(mpctx->sh_video);
2607 vo_seek_reset(mpctx->video_out);
2608 mpctx->sh_video->num_buffered_pts = 0;
2609 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2610 mpctx->delay = 0;
2611 mpctx->time_frame = 0;
2612 mpctx->update_video_immediately = true;
2613 // Not all demuxers set d_video->pts during seek, so this value
2614 // (which is used by at least vobsub and edl code below) may
2615 // be completely wrong (probably 0).
2616 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
2617 update_subtitles(mpctx, &mpctx->opts, mpctx->sh_video,
2618 mpctx->sh_video->pts, mpctx->video_offset,
2619 mpctx->d_sub, 1);
2620 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2623 if (mpctx->sh_audio) {
2624 current_module = "seek_audio_reset";
2625 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2626 mpctx->sh_audio->a_buffer_len = 0;
2627 mpctx->sh_audio->a_out_buffer_len = 0;
2628 if (!mpctx->sh_video)
2629 update_subtitles(mpctx, &mpctx->opts, NULL, mpctx->sh_audio->pts,
2630 mpctx->video_offset, mpctx->d_sub, 1);
2633 if (vo_vobsub && mpctx->sh_video) {
2634 current_module = "seek_vobsub_reset";
2635 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2638 edl_seek_reset(mpctx);
2640 mpctx->total_avsync_change = 0;
2641 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2642 drop_frame_cnt = 0;
2644 current_module = NULL;
2645 return 0;
2648 int get_current_chapter(struct MPContext *mpctx)
2650 if (!mpctx->chapters || !mpctx->sh_video)
2651 return demuxer_get_current_chapter(mpctx->demuxer);
2653 int i;
2654 double current_pts = mpctx->sh_video->pts;
2655 for (i = 1; i < mpctx->num_chapters; i++)
2656 if (current_pts < mpctx->chapters[i].start)
2657 break;
2658 return i - 1;
2661 // currently returns a string allocated with malloc, not talloc
2662 char *chapter_display_name(struct MPContext *mpctx, int chapter)
2664 if (!mpctx->chapters || !mpctx->sh_video)
2665 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
2666 return strdup(mpctx->chapters[chapter].name);
2669 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
2670 char **chapter_name)
2672 if (!mpctx->chapters || !mpctx->sh_video)
2673 return demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
2674 chapter_name);
2675 if (chapter >= mpctx->num_chapters)
2676 return -1;
2677 if (chapter < 0)
2678 chapter = 0;
2679 *seek_pts = mpctx->chapters[chapter].start;
2680 if (chapter_name)
2681 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
2682 return chapter;
2685 static int find_ordered_chapter_sources(struct MPContext *mpctx,
2686 struct content_source *sources,
2687 int num_sources,
2688 unsigned char uid_map[][16])
2690 int num_filenames = 0;
2691 char **filenames = NULL;
2692 if (num_sources > 1) {
2693 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
2694 "other sources.\n");
2695 if (mpctx->stream->type != STREAMTYPE_FILE) {
2696 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
2697 "normal disk file. Will not search for related files.\n");
2698 } else {
2699 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
2700 "same directory to find referenced sources.\n");
2701 filenames = find_files(mpctx->demuxer->filename, ".mkv",
2702 &num_filenames);
2706 int num_left = num_sources - 1;
2707 for (int i = 0; i < num_filenames && num_left > 0; i++) {
2708 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
2709 filename_recode(filenames[i]));
2710 int format;
2711 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
2712 if (!s)
2713 continue;
2714 struct demuxer *d = demux_open(&mpctx->opts, s, DEMUXER_TYPE_MATROSKA,
2715 mpctx->opts.audio_id,
2716 mpctx->opts.video_id,
2717 mpctx->opts.sub_id, filenames[i]);
2718 if (!d) {
2719 free_stream(s);
2720 continue;
2722 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
2723 for (int i = 1; i < num_sources; i++) {
2724 if (sources[i].demuxer)
2725 continue;
2726 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
2727 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
2728 i, filename_recode(d->filename));
2729 sources[i].stream = s;
2730 sources[i].demuxer = d;
2731 num_left--;
2732 goto match;
2736 free_demuxer(d);
2737 free_stream(s);
2738 continue;
2739 match:
2742 talloc_free(filenames);
2743 if (num_left) {
2744 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
2745 "There will be parts MISSING from the video!\n");
2746 for (int i = 1, j = 1; i < num_sources; i++)
2747 if (sources[i].demuxer) {
2748 sources[j] = sources[i];
2749 memcpy(uid_map[j], uid_map[i], 16);
2750 j++;
2753 return num_sources - num_left;
2756 static void build_ordered_chapter_timeline(struct MPContext *mpctx)
2758 if (!mpctx->opts.ordered_chapters) {
2759 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
2760 "you have disabled support for them. Ignoring.\n");
2761 return;
2764 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
2765 "edit timeline.\n");
2767 struct demuxer *demuxer = mpctx->demuxer;
2768 struct matroska_data *m = &demuxer->matroska_data;
2770 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
2771 m->num_ordered_chapters);
2772 sources[0].stream = mpctx->stream;
2773 sources[0].demuxer = mpctx->demuxer;
2774 unsigned char uid_map[m->num_ordered_chapters][16];
2775 int num_sources = 1;
2776 memcpy(uid_map[0], m->segment_uid, 16);
2778 for (int i = 0; i < m->num_ordered_chapters; i++) {
2779 struct matroska_chapter *c = m->ordered_chapters + i;
2780 if (!c->has_segment_uid)
2781 memcpy(c->segment_uid, m->segment_uid, 16);
2783 for (int j = 0; j < num_sources; j++)
2784 if (!memcmp(c->segment_uid, uid_map[j], 16))
2785 goto found1;
2786 memcpy(uid_map[num_sources], c->segment_uid, 16);
2787 sources[num_sources] = (struct content_source){};
2788 num_sources++;
2789 found1:
2793 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
2794 uid_map);
2797 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
2798 m->num_ordered_chapters + 1);
2799 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
2800 m->num_ordered_chapters);
2801 uint64_t starttime = 0;
2802 uint64_t missing_time = 0;
2803 int part_count = 0;
2804 int num_chapters = 0;
2805 uint64_t prev_part_offset;
2806 for (int i = 0; i < m->num_ordered_chapters; i++) {
2807 struct matroska_chapter *c = m->ordered_chapters + i;
2809 int j;
2810 for (j = 0; j < num_sources; j++) {
2811 if (!memcmp(c->segment_uid, uid_map[j], 16))
2812 goto found2;
2814 missing_time += c->end - c->start;
2815 continue;
2816 found2:;
2817 chapters[num_chapters].start = starttime / 1000.;
2818 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
2819 /* Only add a separate part if the time or file actually changes.
2820 * Matroska files have chapter divisions that are redundant from
2821 * timeline point of view because the same chapter structure is used
2822 * both to specify the timeline and for normal chapter information.
2823 * Removing a missing inserted external chapter can also cause this. */
2824 if (part_count == 0 || c->start != starttime + prev_part_offset
2825 || sources + j != timeline[part_count - 1].source) {
2826 timeline[part_count].source = sources + j;
2827 timeline[part_count].start = chapters[num_chapters].start;
2828 timeline[part_count].source_start = c->start / 1000.;
2829 prev_part_offset = c->start - starttime;
2830 part_count++;
2832 starttime += c->end - c->start;
2833 num_chapters++;
2835 timeline[part_count].start = starttime / 1000.;
2837 if (!part_count) {
2838 // None of the parts come from the file itself???
2839 talloc_free(sources);
2840 talloc_free(timeline);
2841 talloc_free(chapters);
2842 return;
2845 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
2846 "sources. Total length %.3f seconds.\n", part_count, num_sources,
2847 timeline[part_count].start);
2848 if (missing_time)
2849 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
2850 "from the timeline!\n", missing_time / 1000.);
2851 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
2852 for (int i = 0; i < num_sources; i++)
2853 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
2854 filename_recode(sources[i].demuxer->filename));
2855 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
2856 "source_start, source):\n");
2857 for (int i = 0; i < part_count; i++) {
2858 struct timeline_part *p = timeline + i;
2859 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
2860 p->source_start, p->source - sources);
2862 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start);
2863 mpctx->sources = sources;
2864 mpctx->num_sources = num_sources;
2865 mpctx->timeline = timeline;
2866 mpctx->num_timeline_parts = part_count;
2867 mpctx->num_chapters = num_chapters;
2868 mpctx->chapters = chapters;
2870 mpctx->timeline_part = 0;
2871 mpctx->video_offset = timeline[0].source_start;
2872 mpctx->demuxer = timeline[0].source->demuxer;
2876 static int read_keys(void *ctx, int fd)
2878 getch2(ctx);
2879 return mplayer_get_key(ctx, 0);
2883 #ifdef PTW32_STATIC_LIB
2884 static void detach_ptw32(void)
2886 pthread_win32_thread_detach_np();
2887 pthread_win32_process_detach_np();
2889 #endif
2891 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
2892 * file for some tools to link against. */
2893 #ifndef DISABLE_MAIN
2894 int main(int argc,char* argv[]){
2897 char * mem_ptr;
2899 // movie info:
2901 /* Flag indicating whether MPlayer should exit without playing anything. */
2902 int opt_exit = 0;
2904 //float a_frame=0; // Audio
2906 int i;
2908 struct MPContext *mpctx = &(struct MPContext){
2909 .osd_function = OSD_PLAY,
2910 .begin_skip = MP_NOPTS_VALUE,
2911 .play_tree_step = 1,
2912 .global_sub_pos = -1,
2913 .set_of_sub_pos = -1,
2914 .file_format = DEMUXER_TYPE_UNKNOWN,
2915 .last_dvb_step = 1,
2918 #ifdef PTW32_STATIC_LIB
2919 pthread_win32_process_attach_np();
2920 pthread_win32_thread_attach_np();
2921 atexit(detach_ptw32);
2922 #endif
2924 InitTimer();
2925 srand(GetTimerMS());
2927 mp_msg_init();
2929 #ifdef CONFIG_X11
2930 mpctx->x11_state = vo_x11_init_state();
2931 #endif
2932 struct MPOpts *opts = &mpctx->opts;
2933 set_default_mplayer_options(opts);
2934 // Create the config context and register the options
2935 mpctx->mconfig = m_config_new(opts, cfg_include);
2936 m_config_register_options(mpctx->mconfig,mplayer_opts);
2937 mp_input_register_options(mpctx->mconfig);
2939 // Preparse the command line
2940 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
2942 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
2943 set_path_env();
2944 #endif
2946 #ifdef CONFIG_TV
2947 stream_tv_defaults.immediate = 1;
2948 #endif
2950 parse_cfgfiles(mpctx, mpctx->mconfig);
2952 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
2953 if(mpctx->playtree == NULL)
2954 opt_exit = 1;
2955 else {
2956 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
2957 if(mpctx->playtree) {
2958 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
2959 if(mpctx->playtree_iter) {
2960 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
2961 play_tree_iter_free(mpctx->playtree_iter);
2962 mpctx->playtree_iter = NULL;
2964 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
2968 mpctx->key_fifo = mp_fifo_create(opts);
2970 print_version("MPlayer");
2972 #if defined(__MINGW32__) || defined(__CYGWIN__)
2974 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
2975 BOOL WINAPI (*setDEP)(DWORD) = NULL;
2976 if (kernel32)
2977 setDEP = GetProcAddress(kernel32, "SetProcessDEPPolicy");
2978 if (setDEP) setDEP(3);
2980 // stop Windows from showing all kinds of annoying error dialogs
2981 SetErrorMode(0x8003);
2982 // request 1ms timer resolution
2983 timeBeginPeriod(1);
2984 #endif
2986 #ifdef CONFIG_PRIORITY
2987 set_priority();
2988 #endif
2990 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
2991 list_video_out();
2992 opt_exit = 1;
2995 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
2996 list_audio_out();
2997 opt_exit = 1;
3000 /* Check codecs.conf. */
3001 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3002 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3003 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3004 if(!parse_codec_cfg(NULL)){
3005 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3007 mp_tmsg(MSGT_CPLAYER,MSGL_V,"Using built-in default codecs.conf.\n");
3010 free( mem_ptr ); // release the buffer created by get_path()
3013 #if 0
3014 if(video_codec_list){
3015 int i;
3016 video_codec=video_codec_list[0];
3017 for(i=0;video_codec_list[i];i++)
3018 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
3020 #endif
3021 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3022 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available audio codecs:\n");
3023 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3024 list_codecs(1);
3025 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3026 opt_exit = 1;
3028 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3029 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video codecs:\n");
3030 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3031 list_codecs(0);
3032 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3033 opt_exit = 1;
3035 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3036 vfm_help();
3037 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3038 opt_exit = 1;
3040 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3041 afm_help();
3042 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3043 opt_exit = 1;
3045 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3046 af_help();
3047 printf("\n");
3048 opt_exit = 1;
3050 #ifdef CONFIG_X11
3051 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3052 fstype_help();
3053 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3054 opt_exit = 1;
3056 #endif
3057 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3058 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3059 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3060 demuxer_help();
3061 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3062 opt_exit = 1;
3064 if(list_properties) {
3065 property_print_help();
3066 opt_exit = 1;
3069 if(opt_exit)
3070 exit_player(mpctx, EXIT_NONE);
3072 if(!mpctx->filename && !player_idle_mode){
3073 // no file/vcd/dvd -> show HELP:
3074 mp_msg(MSGT_CPLAYER, MSGL_INFO, help_text);
3075 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3078 /* Display what configure line was used */
3079 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3081 // Many users forget to include command line in bugreports...
3082 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3083 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "CommandLine:");
3084 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3085 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3088 //------ load global data first ------
3090 mpctx->osd = osd_create();
3092 // check font
3093 #ifdef CONFIG_FREETYPE
3094 init_freetype();
3095 #endif
3096 #ifdef CONFIG_FONTCONFIG
3097 if(font_fontconfig <= 0)
3099 #endif
3100 #ifdef CONFIG_BITMAP_FONT
3101 if(font_name){
3102 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3103 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",
3104 filename_recode(font_name));
3105 } else {
3106 // try default:
3107 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3108 free(mem_ptr); // release the buffer created by get_path()
3109 if(!vo_font)
3110 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3112 if (sub_font_name)
3113 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3114 else
3115 mpctx->osd->sub_font = vo_font;
3116 #endif
3117 #ifdef CONFIG_FONTCONFIG
3119 #endif
3121 #ifdef CONFIG_ASS
3122 ass_library = ass_init();
3123 #endif
3125 #ifdef HAVE_RTC
3126 if(!nortc)
3128 // seteuid(0); /* Can't hurt to try to get root here */
3129 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3130 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Failed to open %s: %s (it should be readable by the user.)\n",
3131 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3132 else {
3133 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3135 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3136 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp, strerror(errno));
3137 mp_tmsg(MSGT_CPLAYER, MSGL_HINT, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp);
3138 close (rtc_fd);
3139 rtc_fd = -1;
3140 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3141 /* variable only by the root */
3142 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno));
3143 close (rtc_fd);
3144 rtc_fd = -1;
3145 } else
3146 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Using Linux hardware RTC timing (%ldHz).\n", irqp);
3149 if(rtc_fd<0)
3150 #endif /* HAVE_RTC */
3151 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3152 softsleep?"software":timer_name);
3154 #ifdef HAVE_TERMCAP
3155 load_termcap(NULL); // load key-codes
3156 #endif
3158 // ========== Init keyboard FIFO (connection to libvo) ============
3160 // Init input system
3161 current_module = "init_input";
3162 mpctx->input = mp_input_init(&opts->input);
3163 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3164 if(slave_mode)
3165 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
3166 else if(!noconsolecontrols)
3167 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
3168 // Set the libstream interrupt callback
3169 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
3171 #ifdef CONFIG_MENU
3172 if(use_menu) {
3173 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3174 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3175 else {
3176 menu_cfg = get_path("menu.conf");
3177 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3178 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3179 else {
3180 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
3181 MPLAYER_CONFDIR "/menu.conf"))
3182 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", MPLAYER_CONFDIR"/menu.conf");
3183 else {
3184 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Menu init failed.\n");
3185 use_menu = 0;
3190 #endif
3192 current_module = NULL;
3194 /// Catch signals
3195 #ifndef __MINGW32__
3196 signal(SIGCHLD,child_sighandler);
3197 #endif
3199 #ifdef CONFIG_CRASH_DEBUG
3200 prog_path = argv[0];
3201 #endif
3202 //========= Catch terminate signals: ================
3203 // terminate requests:
3204 signal(SIGTERM,exit_sighandler); // kill
3205 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3207 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3209 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3210 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3211 #ifdef CONFIG_SIGHANDLER
3212 // fatal errors:
3213 signal(SIGBUS,exit_sighandler); // bus error
3214 signal(SIGSEGV,exit_sighandler); // segfault
3215 signal(SIGILL,exit_sighandler); // illegal instruction
3216 signal(SIGFPE,exit_sighandler); // floating point exc.
3217 signal(SIGABRT,exit_sighandler); // abort()
3218 #ifdef CONFIG_CRASH_DEBUG
3219 if (crash_debug)
3220 signal(SIGTRAP,exit_sighandler);
3221 #endif
3222 #endif
3224 // ******************* Now, let's see the per-file stuff ********************
3226 play_next_file:
3228 // init global sub numbers
3229 mpctx->global_sub_size = 0;
3230 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3232 if (mpctx->filename) {
3233 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
3234 load_per_extension_config (mpctx->mconfig, mpctx->filename);
3235 load_per_file_config (mpctx->mconfig, mpctx->filename);
3238 if (opts->video_driver_list)
3239 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
3240 if (opts->audio_driver_list)
3241 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
3243 // We must enable getch2 here to be able to interrupt network connection
3244 // or cache filling
3245 if(!noconsolecontrols && !slave_mode){
3246 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
3247 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"WARNING: getch2_init called twice!\n");
3248 else
3249 getch2_enable(); // prepare stdin for hotkeys...
3250 mpctx->initialized_flags|=INITIALIZED_GETCH2;
3251 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3254 // =================== GUI idle loop (STOP state) ===========================
3255 while (player_idle_mode && !mpctx->filename) {
3256 play_tree_t * entry = NULL;
3257 mp_cmd_t * cmd;
3258 while (!(cmd = mp_input_get_cmd(mpctx->input, 0,1,0))) { // wait for command
3259 if (mpctx->video_out)
3260 vo_check_events(mpctx->video_out);
3261 usec_sleep(20000);
3263 switch (cmd->id) {
3264 case MP_CMD_LOADFILE:
3265 // prepare a tree entry with the new filename
3266 entry = play_tree_new();
3267 play_tree_add_file(entry, cmd->args[0].v.s);
3268 // The entry is added to the main playtree after the switch().
3269 break;
3270 case MP_CMD_LOADLIST:
3271 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3272 break;
3273 case MP_CMD_QUIT:
3274 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3275 break;
3276 case MP_CMD_GET_PROPERTY:
3277 case MP_CMD_SET_PROPERTY:
3278 case MP_CMD_STEP_PROPERTY:
3279 run_command(mpctx, cmd);
3280 break;
3283 mp_cmd_free(cmd);
3285 if (entry) { // user entered a command that gave a valid entry
3286 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3287 play_tree_free_list(mpctx->playtree->child, 1);
3288 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3290 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3292 play_tree_set_child(mpctx->playtree, entry);
3294 /* Make iterator start at the top the of tree. */
3295 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3296 if (!mpctx->playtree_iter) continue;
3298 // find the first real item in the tree
3299 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3300 // no items!
3301 play_tree_iter_free(mpctx->playtree_iter);
3302 mpctx->playtree_iter = NULL;
3303 continue; // wait for next command
3305 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3308 //---------------------------------------------------------------------------
3310 if(mpctx->filename)
3311 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nPlaying %s.\n",
3312 filename_recode(mpctx->filename));
3314 if (edl_filename) {
3315 if (edl_records) free_edl(edl_records);
3316 next_edl_record = edl_records = edl_parse_file();
3318 if (edl_output_filename) {
3319 if (edl_fd) fclose(edl_fd);
3320 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3322 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Can't open EDL file [%s] for writing.\n",
3323 filename_recode(edl_output_filename));
3327 //==================== Open VOB-Sub ============================
3329 current_module="vobsub";
3330 if (vobsub_name){
3331 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3332 if(vo_vobsub==NULL)
3333 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
3334 filename_recode(vobsub_name));
3335 } else if (sub_auto && mpctx->filename){
3336 /* try to autodetect vobsub from movie filename ::atmos */
3337 char *buf = strdup(mpctx->filename), *psub;
3338 char *pdot = strrchr(buf, '.');
3339 char *pslash = strrchr(buf, '/');
3340 #if defined(__MINGW32__) || defined(__CYGWIN__)
3341 if (!pslash) pslash = strrchr(buf, '\\');
3342 #endif
3343 if (pdot && (!pslash || pdot > pslash))
3344 *pdot = '\0';
3345 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3346 /* try from ~/.mplayer/sub */
3347 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3348 char *bname;
3349 int l;
3350 bname = strrchr(buf,'/');
3351 #if defined(__MINGW32__) || defined(__CYGWIN__)
3352 if(!bname) bname = strrchr(buf,'\\');
3353 #endif
3354 if(bname) bname++;
3355 else bname = buf;
3356 l = strlen(psub) + strlen(bname) + 1;
3357 psub = realloc(psub,l);
3358 strcat(psub,bname);
3359 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3360 free(psub);
3362 free(buf);
3364 if(vo_vobsub){
3365 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3366 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3367 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3369 // setup global sub numbering
3370 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3371 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3374 //============ Open & Sync STREAM --- fork cache2 ====================
3376 mpctx->stream=NULL;
3377 mpctx->demuxer=NULL;
3378 if (mpctx->d_audio) {
3379 //free_demuxer_stream(mpctx->d_audio);
3380 mpctx->d_audio=NULL;
3382 if (mpctx->d_video) {
3383 //free_demuxer_stream(d_video);
3384 mpctx->d_video=NULL;
3386 mpctx->sh_audio=NULL;
3387 mpctx->sh_video=NULL;
3389 current_module="open_stream";
3390 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3391 if(!mpctx->stream) { // error...
3392 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3393 goto goto_next_file;
3395 mpctx->initialized_flags|=INITIALIZED_STREAM;
3397 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3398 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3399 "playlist support will not be used automatically.\n"
3400 "MPlayer's playlist code is unsafe and should only be used with "
3401 "trusted sources.\nPlayback will probably fail.\n\n");
3402 #if 0
3403 play_tree_t* entry;
3404 // Handle playlist
3405 current_module="handle_playlist";
3406 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3407 filename_recode(mpctx->filename));
3408 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3409 mpctx->eof=playtree_add_playlist(mpctx, entry);
3410 goto goto_next_file;
3411 #endif
3413 mpctx->stream->start_pos+=seek_to_byte;
3415 if(stream_dump_type==5){
3416 unsigned char buf[4096];
3417 int len;
3418 FILE *f;
3419 current_module="dumpstream";
3420 if(mpctx->stream->type==STREAMTYPE_STREAM && mpctx->stream->fd<0){
3421 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot dump this stream - no file descriptor available.\n");
3422 exit_player(mpctx, EXIT_ERROR);
3424 stream_reset(mpctx->stream);
3425 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3426 f=fopen(stream_dump_name,"wb");
3427 if(!f){
3428 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3429 exit_player(mpctx, EXIT_ERROR);
3431 if (dvd_chapter > 1) {
3432 int chapter = dvd_chapter - 1;
3433 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3435 while(!mpctx->stream->eof && !async_quit_request){
3436 len=stream_read(mpctx->stream,buf,4096);
3437 if(len>0) {
3438 if(fwrite(buf,len,1,f) != 1) {
3439 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3440 exit_player(mpctx, EXIT_ERROR);
3443 if(dvd_last_chapter > 0) {
3444 int chapter = -1;
3445 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3446 &chapter) == STREAM_OK && chapter + 1 > dvd_last_chapter)
3447 break;
3450 if(fclose(f)) {
3451 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3452 exit_player(mpctx, EXIT_ERROR);
3454 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3455 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3458 #ifdef CONFIG_DVDREAD
3459 if(mpctx->stream->type==STREAMTYPE_DVD){
3460 current_module="dvd lang->id";
3461 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3462 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3463 // setup global sub numbering
3464 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3465 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3466 current_module=NULL;
3468 #endif
3470 #ifdef CONFIG_DVDNAV
3471 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3472 current_module="dvdnav lang->id";
3473 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3474 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3475 // setup global sub numbering
3476 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3477 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3478 current_module=NULL;
3480 #endif
3482 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3483 goto_enable_cache:
3484 if(stream_cache_size>0){
3485 current_module="enable_cache";
3486 if(!stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3487 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3488 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0)))
3489 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3492 //============ Open DEMUXERS --- DETECT file type =======================
3493 current_module="demux_open";
3495 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3497 // HACK to get MOV Reference Files working
3499 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3501 unsigned char* playlist_entry;
3502 play_tree_t *list = NULL, *entry = NULL;
3504 current_module="handle_demux_playlist";
3505 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3507 char *temp, *bname;
3509 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3510 filename_recode(playlist_entry));
3512 bname=mp_basename(playlist_entry);
3513 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3514 continue;
3516 if (!strcmp(playlist_entry, mpctx->filename)) // ignoring self-reference
3517 continue;
3519 entry = play_tree_new();
3521 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3523 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3524 if (temp)
3526 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3527 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3528 strcat(temp, playlist_entry);
3529 if (!strcmp(temp, mpctx->filename)) {
3530 free(temp);
3531 continue;
3533 play_tree_add_file(entry,temp);
3534 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3535 free(temp);
3538 else
3539 play_tree_add_file(entry,playlist_entry);
3541 if(!list)
3542 list = entry;
3543 else
3544 play_tree_append_entry(list,entry);
3546 free_demuxer(mpctx->demuxer);
3547 mpctx->demuxer = NULL;
3549 if (list)
3551 entry = play_tree_new();
3552 play_tree_set_child(entry,list);
3553 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3554 goto goto_next_file;
3558 if(!mpctx->demuxer)
3559 goto goto_next_file;
3561 if (mpctx->demuxer->matroska_data.ordered_chapters)
3562 build_ordered_chapter_timeline(mpctx);
3564 if (!mpctx->sources) {
3565 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
3566 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
3567 .demuxer = mpctx->demuxer};
3568 mpctx->num_sources = 1;
3571 if(dvd_chapter>1) {
3572 double pts;
3573 if (seek_chapter(mpctx, dvd_chapter-1, &pts, NULL) >= 0 && pts > -1.0)
3574 seek(mpctx, pts, SEEK_ABSOLUTE);
3577 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3579 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3580 int i;
3581 int maxid = -1;
3582 // setup global sub numbering
3583 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3584 for (i = 0; i < MAX_S_STREAMS; i++)
3585 if (mpctx->demuxer->s_streams[i])
3586 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3587 mpctx->global_sub_size += maxid + 1;
3589 // Make opts->sub_id always selectable if set.
3590 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3591 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3593 #ifdef CONFIG_ASS
3594 if (ass_enabled && ass_library) {
3595 for (int j = 0; j < mpctx->num_sources; j++) {
3596 struct demuxer *d = mpctx->sources[j].demuxer;
3597 for (int i = 0; i < d->num_attachments; i++) {
3598 struct demux_attachment *att = d->attachments + i;
3599 if (extract_embedded_fonts
3600 && att->name && att->type && att->data && att->data_size
3601 && (strcmp(att->type, "application/x-truetype-font") == 0
3602 || strcmp(att->type, "application/x-font") == 0))
3603 ass_add_font(ass_library, att->name, att->data, att->data_size);
3607 #endif
3609 current_module="demux_open2";
3611 //file_format=demuxer->file_format;
3613 mpctx->d_audio=mpctx->demuxer->audio;
3614 mpctx->d_video=mpctx->demuxer->video;
3615 mpctx->d_sub=mpctx->demuxer->sub;
3617 // select audio stream
3618 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3620 // DUMP STREAMS:
3621 if((stream_dump_type)&&(stream_dump_type<4)){
3622 FILE *f;
3623 demux_stream_t *ds=NULL;
3624 current_module="dump";
3625 // select stream to dump
3626 switch(stream_dump_type){
3627 case 1: ds=mpctx->d_audio;break;
3628 case 2: ds=mpctx->d_video;break;
3629 case 3: ds=mpctx->d_sub;break;
3631 if(!ds){
3632 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"dump: FATAL: Selected stream missing!\n");
3633 exit_player(mpctx, EXIT_ERROR);
3635 // disable other streams:
3636 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3637 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3638 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3639 // let's dump it!
3640 f=fopen(stream_dump_name,"wb");
3641 if(!f){
3642 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3643 exit_player(mpctx, EXIT_ERROR);
3645 while(!ds->eof){
3646 unsigned char* start;
3647 int in_size=ds_get_packet(ds,&start);
3648 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3649 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3650 if(in_size>0) fwrite(start,in_size,1,f);
3651 if(dvd_last_chapter>0) {
3652 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3653 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3654 break;
3657 fclose(f);
3658 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3659 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3662 mpctx->sh_audio=mpctx->d_audio->sh;
3663 mpctx->sh_video=mpctx->d_video->sh;
3665 if(mpctx->sh_video){
3667 current_module="video_read_properties";
3668 if(!video_read_properties(mpctx->sh_video)) {
3669 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Video: Cannot read properties.\n");
3670 mpctx->sh_video=mpctx->d_video->sh=NULL;
3671 } else {
3672 mp_tmsg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
3673 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3674 mpctx->sh_video->fps,mpctx->sh_video->frametime
3677 /* need to set fps here for output encoders to pick it up in their init */
3678 if(force_fps){
3679 mpctx->sh_video->fps=force_fps;
3680 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3682 vo_fps = mpctx->sh_video->fps;
3684 if(!mpctx->sh_video->fps && !force_fps){
3685 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"FPS not specified in the header or invalid, use the -fps option.\n");
3686 mpctx->sh_video=mpctx->d_video->sh=NULL;
3692 if(!mpctx->sh_video && !mpctx->sh_audio){
3693 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "No stream found.\n");
3694 #ifdef CONFIG_DVBIN
3695 if(mpctx->stream->type == STREAMTYPE_DVB)
3697 int dir;
3698 int v = mpctx->last_dvb_step;
3699 if(v > 0)
3700 dir = DVB_CHANNEL_HIGHER;
3701 else
3702 dir = DVB_CHANNEL_LOWER;
3704 if(dvb_step_channel(mpctx->stream, dir)) {
3705 mpctx->stop_play = PT_NEXT_ENTRY;
3706 mpctx->dvbin_reopen = 1;
3709 #endif
3710 goto goto_next_file; // exit_player(_("Fatal error"));
3713 /* display clip info */
3714 demux_info_print(mpctx->demuxer);
3716 //================== Read SUBTITLES (DVD & TEXT) ==========================
3717 if(vo_spudec==NULL && mpctx->sh_video &&
3718 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3719 init_vo_spudec(mpctx);
3722 // after reading video params we should load subtitles because
3723 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3724 // check .sub
3725 current_module="read_subtitles_file";
3726 double sub_fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
3727 if(sub_name){
3728 for (i = 0; sub_name[i] != NULL; ++i)
3729 add_subtitles(mpctx, sub_name[i], sub_fps, 0);
3731 if(sub_auto) { // auto load sub file ...
3732 char *psub = get_path( "sub/" );
3733 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3734 int i = 0;
3735 free(psub); // release the buffer created by get_path() above
3736 while (tmp[i]) {
3737 add_subtitles(mpctx, tmp[i], sub_fps, 1);
3738 free(tmp[i++]);
3740 free(tmp);
3742 if (mpctx->set_of_sub_size > 0) {
3743 // setup global sub numbering
3744 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3745 mpctx->global_sub_size += mpctx->set_of_sub_size;
3749 if (mpctx->global_sub_size) {
3750 // find the best sub to use
3751 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
3752 mpctx->global_sub_pos = -1; // no subs by default
3753 if (vobsub_index_id >= 0) {
3754 // if user asks for a vobsub id, use that first.
3755 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
3756 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3757 // if user asks for a dvd sub id, use that next.
3758 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3759 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
3760 // if there are text subs to use, use those. (autosubs come last here)
3761 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
3762 } else if (opts->sub_id == -1 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
3763 // finally select subs by language and container hints
3764 if (opts->sub_id == -1 && dvdsub_lang)
3765 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
3766 if (opts->sub_id == -1)
3767 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
3768 if (opts->sub_id >= 0)
3769 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
3771 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
3772 mpctx->global_sub_pos--;
3773 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
3774 if(subdata)
3775 switch (stream_dump_type) {
3776 case 3: list_sub_file(subdata); break;
3777 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3778 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3779 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3780 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3781 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3785 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3786 filename_recode(mpctx->filename));
3787 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3788 if (mpctx->sh_video) {
3789 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3790 if (mpctx->sh_video->format >= 0x20202020)
3791 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3792 else
3793 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3794 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3795 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3796 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3797 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3798 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3800 if (mpctx->sh_audio) {
3801 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3802 if (mpctx->sh_audio->format >= 0x20202020)
3803 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3804 else
3805 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3806 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3807 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3808 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3810 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2f\n", demuxer_get_time_length(mpctx->demuxer));
3811 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n",
3812 mpctx->stream->seek && (!mpctx->demuxer || mpctx->demuxer->seekable));
3813 if (mpctx->demuxer) {
3814 if (mpctx->demuxer->num_chapters == 0)
3815 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3816 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3819 if(!mpctx->sh_video) goto main; // audio-only
3821 if(!reinit_video_chain(mpctx)) {
3822 if(!mpctx->sh_video){
3823 if(!mpctx->sh_audio) goto goto_next_file;
3824 goto main; // exit_player(_("Fatal error"));
3828 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3829 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3831 #ifdef CONFIG_FREETYPE
3832 force_load_font = 1;
3833 #endif
3835 //================== MAIN: ==========================
3836 main:
3837 current_module="main";
3839 if(playing_msg) {
3840 char* msg = property_expand_string(mpctx, playing_msg);
3841 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3842 free(msg);
3846 // Disable the term OSD in verbose mode
3847 if(verbose) term_osd = 0;
3850 //int frame_corr_num=0; //
3851 //float v_frame=0; // Video
3852 //float num_frames=0; // number of frames played
3854 int frame_time_remaining=0; // flag
3855 int blit_frame=0;
3857 // Make sure old OSD does not stay around,
3858 // e.g. with -fixed-vo and same-resolution files
3859 clear_osd_msgs();
3860 update_osd_msg(mpctx);
3862 //================ SETUP AUDIO ==========================
3864 if(mpctx->sh_audio){
3865 reinit_audio_chain(mpctx);
3866 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3867 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3870 current_module="av_init";
3872 if(mpctx->sh_video){
3873 mpctx->sh_video->timer=0;
3874 if (! ignore_start)
3875 audio_delay += mpctx->sh_video->stream_delay;
3877 if(mpctx->sh_audio){
3878 if (start_volume >= 0)
3879 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
3880 if (! ignore_start)
3881 audio_delay -= mpctx->sh_audio->stream_delay;
3882 mpctx->delay=-audio_delay;
3885 if(!mpctx->sh_audio){
3886 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Audio: no sound\n");
3887 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
3888 ds_free_packs(mpctx->d_audio); // free buffered chunks
3889 //mpctx->d_audio->id=-2; // do not read audio chunks
3890 //uninit_player(mpctx, INITIALIZED_AO); // close device
3892 if(!mpctx->sh_video){
3893 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Video: no video\n");
3894 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
3895 ds_free_packs(mpctx->d_video);
3896 mpctx->d_video->id=-2;
3897 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
3900 if (!mpctx->sh_video && !mpctx->sh_audio)
3901 goto goto_next_file;
3903 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
3904 if(force_fps && mpctx->sh_video){
3905 vo_fps = mpctx->sh_video->fps=force_fps;
3906 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3907 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx->sh_video->fps,mpctx->sh_video->frametime);
3910 mp_input_set_section(mpctx->input, NULL);
3911 //TODO: add desired (stream-based) sections here
3912 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
3913 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
3915 //==================== START PLAYING =======================
3917 if(opts->loop_times>1) opts->loop_times--; else
3918 if(opts->loop_times==1) opts->loop_times = -1;
3920 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Starting playback...\n");
3922 total_time_usage_start=GetTimer();
3923 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
3924 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
3925 play_n_frames=play_n_frames_mf;
3927 if(play_n_frames==0){
3928 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
3931 if (seek_to_sec) {
3932 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
3933 end_at.pos += seek_to_sec;
3936 if (end_at.type == END_AT_SIZE) {
3937 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Option -endpos in MPlayer does not yet support size units.\n");
3938 end_at.type = END_AT_NONE;
3941 #ifdef CONFIG_DVDNAV
3942 mp_dvdnav_context_free(mpctx);
3943 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3944 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
3945 mp_dvdnav_cell_has_changed(mpctx->stream,1);
3947 #endif
3949 get_relative_time(mpctx); // reset current delta
3950 mpctx->time_frame = 0;
3951 mpctx->drop_message_shown = 0;
3952 mpctx->update_video_immediately = true;
3953 mpctx->total_avsync_change = 0;
3954 // Make sure VO knows current pause state
3955 if (mpctx->sh_video)
3956 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
3957 NULL);
3959 while(!mpctx->stop_play){
3960 float aq_sleep_time=0;
3962 if(dvd_last_chapter>0) {
3963 int cur_chapter = get_current_chapter(mpctx);
3964 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
3965 goto goto_next_file;
3968 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
3969 mpctx->sh_audio = mpctx->d_audio->sh;
3970 mpctx->sh_audio->ds = mpctx->d_audio;
3971 reinit_audio_chain(mpctx);
3974 /*========================== PLAY AUDIO ============================*/
3976 if (mpctx->sh_audio && !mpctx->paused)
3977 if (!fill_audio_out_buffers(mpctx))
3978 // at eof, all audio at least written to ao
3979 if (!mpctx->sh_video)
3980 mpctx->stop_play = AT_END_OF_FILE;
3983 if(!mpctx->sh_video) {
3984 // handle audio-only case:
3985 double a_pos=0;
3986 // sh_audio can be NULL due to video stream switching
3987 // TODO: handle this better
3988 if (mpctx->sh_audio)
3989 a_pos = playing_audio_pts(mpctx);
3991 print_status(mpctx, a_pos, false);
3993 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
3994 mpctx->stop_play = PT_NEXT_ENTRY;
3995 update_subtitles(mpctx, &mpctx->opts, NULL, a_pos, mpctx->video_offset,
3996 mpctx->d_sub, 0);
3997 update_osd_msg(mpctx);
3999 } else {
4001 /*========================== PLAY VIDEO ============================*/
4003 vo_pts=mpctx->sh_video->timer*90000.0;
4004 vo_fps=mpctx->sh_video->fps;
4006 if (!mpctx->video_out->frame_loaded) {
4007 double frame_time = update_video(mpctx, &blit_frame);
4008 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
4009 if (mpctx->sh_video->vf_initialized < 0) {
4010 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "\nFATAL: Could not initialize video filters (-vf) or video output (-vo).\n");
4011 mpctx->stop_play = PT_NEXT_ENTRY;
4012 goto goto_next_file;
4014 if (blit_frame) {
4015 struct vf_instance *vf = mpctx->sh_video->vfilter;
4016 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
4017 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
4018 vo_osd_changed(0);
4020 if (frame_time < 0)
4021 mpctx->stop_play = AT_END_OF_FILE;
4022 else {
4023 if (mpctx->update_video_immediately) {
4024 // Show this frame immediately, rest normally
4025 mpctx->update_video_immediately = false;
4026 } else {
4027 mpctx->time_frame += frame_time / opts->playback_speed;
4028 adjust_sync(mpctx, frame_time);
4032 if (mpctx->timeline) {
4033 struct timeline_part *next = mpctx->timeline + mpctx->timeline_part + 1;
4034 if (mpctx->sh_video->pts >= next->start
4035 || mpctx->stop_play == AT_END_OF_FILE
4036 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
4037 seek(mpctx, next->start, SEEK_ABSOLUTE);
4038 continue;
4042 // ==========================================================================
4044 // current_module="draw_osd";
4045 // if(vo_config_count) mpctx->video_out->draw_osd();
4047 current_module="vo_check_events";
4048 vo_check_events(mpctx->video_out);
4050 #ifdef CONFIG_X11
4051 if (stop_xscreensaver) {
4052 current_module = "stop_xscreensaver";
4053 xscreensaver_heartbeat(mpctx->x11_state);
4055 #endif
4056 if (heartbeat_cmd) {
4057 static unsigned last_heartbeat;
4058 unsigned now = GetTimerMS();
4059 if (now - last_heartbeat > 30000) {
4060 last_heartbeat = now;
4061 system(heartbeat_cmd);
4065 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
4067 //====================== FLIP PAGE (VIDEO BLT): =========================
4069 current_module="flip_page";
4070 if (!frame_time_remaining && blit_frame) {
4071 unsigned int t2=GetTimer();
4073 vo_flip_page(mpctx->video_out);
4075 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
4076 vout_time_usage += mpctx->last_vo_flip_duration;
4077 print_status(mpctx, MP_NOPTS_VALUE, true);
4079 else
4080 print_status(mpctx, MP_NOPTS_VALUE, false);
4082 //============================ Auto QUALITY ============================
4084 /*Output quality adjustments:*/
4085 if(auto_quality>0){
4086 current_module="autoq";
4087 // float total=0.000001f * (GetTimer()-aq_total_time);
4088 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4089 if(output_quality<auto_quality && aq_sleep_time>0)
4090 ++output_quality;
4091 else
4092 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4093 if(output_quality>1 && aq_sleep_time<0)
4094 --output_quality;
4095 else
4096 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4097 output_quality=0;
4098 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4099 set_video_quality(mpctx->sh_video,output_quality);
4102 if (!frame_time_remaining && blit_frame) {
4103 if (play_n_frames >= 0) {
4104 --play_n_frames;
4105 if (play_n_frames <= 0)
4106 mpctx->stop_play = PT_NEXT_ENTRY;
4108 if (mpctx->step_frames > 0) {
4109 mpctx->step_frames--;
4110 if (mpctx->step_frames == 0)
4111 pause_player(mpctx);
4116 // FIXME: add size based support for -endpos
4117 if (end_at.type == END_AT_TIME &&
4118 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
4119 mpctx->stop_play = PT_NEXT_ENTRY;
4121 } // end if(mpctx->sh_video)
4123 #ifdef CONFIG_DVDNAV
4124 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4125 nav_highlight_t hl;
4126 mp_dvdnav_get_highlight (mpctx->stream, &hl);
4127 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4128 vo_osd_changed (OSDTYPE_DVDNAV);
4130 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
4131 double ar = -1.0;
4132 if (stream_control (mpctx->demuxer->stream,
4133 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
4134 != STREAM_UNSUPPORTED)
4135 mpctx->sh_video->stream_aspect = ar;
4138 #endif
4140 //================= Keyboard events, SEEKing ====================
4142 current_module="key_events";
4145 while (1) {
4146 mp_cmd_t* cmd;
4147 while ((cmd = mp_input_get_cmd(mpctx->input, 0,0,0)) != NULL) {
4148 run_command(mpctx, cmd);
4149 mp_cmd_free(cmd);
4150 if (mpctx->stop_play)
4151 break;
4153 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
4154 || mpctx->abs_seek_pos)
4155 break;
4156 if (mpctx->sh_video) {
4157 update_osd_msg(mpctx);
4158 int hack = vo_osd_changed(0);
4159 vo_osd_changed(hack);
4160 if (hack)
4161 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
4162 add_step_frame(mpctx);
4163 break;
4165 else
4166 vo_osd_changed(0);
4168 pause_loop(mpctx);
4172 // handle -sstep
4173 if (step_sec > 0 && !mpctx->paused) {
4174 mpctx->osd_function=OSD_FFW;
4175 mpctx->rel_seek_secs+=step_sec;
4178 edl_update(mpctx);
4180 /* Looping. */
4181 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
4182 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
4184 if(opts->loop_times>1) opts->loop_times--; else
4185 if(opts->loop_times==1) opts->loop_times=-1;
4186 play_n_frames=play_n_frames_mf;
4187 mpctx->stop_play=0;
4188 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
4191 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
4192 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
4194 mpctx->rel_seek_secs=0;
4195 mpctx->abs_seek_pos=0;
4198 } // while(!mpctx->stop_play)
4200 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4202 #ifdef CONFIG_DVBIN
4203 if(mpctx->dvbin_reopen)
4205 mpctx->stop_play = 0;
4206 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4207 cache_uninit(mpctx->stream);
4208 mpctx->dvbin_reopen = 0;
4209 goto goto_enable_cache;
4211 #endif
4214 goto_next_file: // don't jump here after ao/vo/getch initialization!
4216 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4218 if(benchmark){
4219 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4220 double total_time_usage;
4221 total_time_usage_start=GetTimer()-total_time_usage_start;
4222 total_time_usage = (float)total_time_usage_start*0.000001;
4223 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4224 video_time_usage,vout_time_usage,audio_time_usage,
4225 total_time_usage-tot,total_time_usage);
4226 if(total_time_usage>0.0)
4227 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4228 100.0*video_time_usage/total_time_usage,
4229 100.0*vout_time_usage/total_time_usage,
4230 100.0*audio_time_usage/total_time_usage,
4231 100.0*(total_time_usage-tot)/total_time_usage,
4232 100.0);
4233 if(total_frame_cnt && frame_dropping)
4234 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4235 total_frame_cnt-drop_frame_cnt,
4236 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4237 drop_frame_cnt,
4238 100*drop_frame_cnt/total_frame_cnt,
4239 total_frame_cnt,
4240 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4243 // time to uninit all, except global stuff:
4244 uninit_player(mpctx, INITIALIZED_ALL-(opts->fixed_vo?INITIALIZED_VO:0));
4246 if(mpctx->set_of_sub_size > 0) {
4247 current_module="sub_free";
4248 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4249 sub_free(mpctx->set_of_subtitles[i]);
4250 #ifdef CONFIG_ASS
4251 if(mpctx->set_of_ass_tracks[i])
4252 ass_free_track( mpctx->set_of_ass_tracks[i] );
4253 #endif
4255 mpctx->set_of_sub_size = 0;
4257 vo_sub_last = vo_sub=NULL;
4258 subdata=NULL;
4259 #ifdef CONFIG_ASS
4260 ass_track = NULL;
4261 if(ass_library)
4262 ass_clear_fonts(ass_library);
4263 #endif
4265 if (!mpctx->stop_play) // In case some goto jumped here...
4266 mpctx->stop_play = PT_NEXT_ENTRY;
4268 int playtree_direction = 1;
4270 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4271 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4272 play_tree_iter_free(mpctx->playtree_iter);
4273 mpctx->playtree_iter = NULL;
4275 mpctx->play_tree_step = 1;
4276 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4277 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4278 if(mpctx->playtree_iter) {
4279 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4280 play_tree_iter_free(mpctx->playtree_iter);
4281 mpctx->playtree_iter = NULL;
4284 } else if (mpctx->stop_play == PT_STOP) {
4285 play_tree_iter_free(mpctx->playtree_iter);
4286 mpctx->playtree_iter = NULL;
4287 } else { // NEXT PREV SRC
4288 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4291 while(mpctx->playtree_iter != NULL) {
4292 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4293 if(mpctx->filename == NULL) {
4294 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4295 play_tree_iter_free(mpctx->playtree_iter);
4296 mpctx->playtree_iter = NULL;
4298 } else
4299 break;
4302 if(mpctx->playtree_iter != NULL || player_idle_mode){
4303 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4304 mpctx->stop_play = 0;
4305 goto play_next_file;
4309 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4311 return 1;
4313 #endif /* DISABLE_MAIN */