input: remove unused "paused" arguments from some functions
[mplayer/glamo.git] / mplayer.c
blob4f309ff6fb0476ee977e9e80e0578083e8eb7178
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /// \file
20 /// \ingroup Properties Command2Property OSDMsgStack
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include "config.h"
26 #include "talloc.h"
28 #if defined(__MINGW32__) || defined(__CYGWIN__)
29 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
30 #include <windows.h>
31 #endif
32 #include <string.h>
33 #include <unistd.h>
35 // #include <sys/mman.h>
36 #include <sys/types.h>
37 #ifndef __MINGW32__
38 #include <sys/ioctl.h>
39 #include <sys/wait.h>
40 #else
41 #define SIGHUP 1 /* hangup */
42 #define SIGQUIT 3 /* quit */
43 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
44 #define SIGBUS 10 /* bus error */
45 #define SIGPIPE 13 /* broken pipe */
46 #endif
48 #include <sys/time.h>
49 #include <sys/stat.h>
51 #include <signal.h>
52 #include <time.h>
53 #include <fcntl.h>
54 #include <limits.h>
56 #include <errno.h>
58 #include "mp_msg.h"
59 #include "av_log.h"
62 #include "m_option.h"
63 #include "m_config.h"
64 #include "mplayer.h"
65 #include "access_mpcontext.h"
66 #include "m_property.h"
68 #include "cfg-mplayer-def.h"
70 #include "ffmpeg_files/intreadwrite.h"
71 #include "libavutil/avstring.h"
73 #include "subreader.h"
75 #include "mp_osd.h"
76 #include "libvo/video_out.h"
78 #include "libvo/font_load.h"
79 #include "libvo/sub.h"
81 #ifdef CONFIG_X11
82 #include "libvo/x11_common.h"
83 #endif
85 #include "libao2/audio_out.h"
87 #include "codec-cfg.h"
89 #include "edl.h"
91 #include "spudec.h"
92 #include "vobsub.h"
94 #include "osdep/getch2.h"
95 #include "osdep/timer.h"
96 #include "osdep/findfiles.h"
98 #include "input/input.h"
100 const int under_mencoder = 0;
101 int slave_mode=0;
102 int player_idle_mode=0;
103 int quiet=0;
104 int enable_mouse_movements=0;
105 float start_volume = -1;
107 #include "osdep/priority.h"
109 char *heartbeat_cmd;
111 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
113 #ifdef HAVE_RTC
114 #ifdef __linux__
115 #include <linux/rtc.h>
116 #else
117 #include <rtc.h>
118 #define RTC_IRQP_SET RTCIO_IRQP_SET
119 #define RTC_PIE_ON RTCIO_PIE_ON
120 #endif /* __linux__ */
121 #endif /* HAVE_RTC */
123 #include "stream/tv.h"
124 #include "stream/stream_radio.h"
125 #ifdef CONFIG_DVBIN
126 #include "stream/dvbin.h"
127 #endif
128 #include "stream/cache2.h"
130 //**************************************************************************//
131 // Playtree
132 //**************************************************************************//
133 #include "playtree.h"
134 #include "playtreeparser.h"
136 //**************************************************************************//
137 // Config
138 //**************************************************************************//
139 #include "parser-cfg.h"
140 #include "parser-mpcmd.h"
142 //**************************************************************************//
143 // Config file
144 //**************************************************************************//
146 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
148 #include "get_path.h"
150 //**************************************************************************//
151 //**************************************************************************//
152 // Input media streaming & demultiplexer:
153 //**************************************************************************//
155 static int max_framesize=0;
157 #include "stream/stream.h"
158 #include "libmpdemux/demuxer.h"
159 #include "libmpdemux/stheader.h"
161 #ifdef CONFIG_DVDREAD
162 #include "stream/stream_dvd.h"
163 #endif
164 #include "stream/stream_dvdnav.h"
166 #include "libmpcodecs/dec_audio.h"
167 #include "libmpcodecs/dec_video.h"
168 #include "libmpcodecs/mp_image.h"
169 #include "libmpcodecs/vf.h"
170 #include "libmpcodecs/vd.h"
172 #include "mixer.h"
174 #include "mp_core.h"
175 #include "options.h"
176 #include "defaultopts.h"
178 static const char help_text[]=_(
179 "Usage: mplayer [options] [url|path/]filename\n"
180 "\n"
181 "Basic options: (complete list in the man page)\n"
182 " -vo <drv> select video output driver ('-vo help' for a list)\n"
183 " -ao <drv> select audio output driver ('-ao help' for a list)\n"
184 #ifdef CONFIG_VCD
185 " vcd://<trackno> play (S)VCD (Super Video CD) track (raw device, no mount)\n"
186 #endif
187 #ifdef CONFIG_DVDREAD
188 " dvd://<titleno> play DVD title from device instead of plain file\n"
189 #endif
190 " -alang/-slang select DVD audio/subtitle language (by 2-char country code)\n"
191 " -ss <position> seek to given (seconds or hh:mm:ss) position\n"
192 " -nosound do not play sound\n"
193 " -fs fullscreen playback (or -vm, -zoom, details in the man page)\n"
194 " -x <x> -y <y> set display resolution (for use with -vm or -zoom)\n"
195 " -sub <file> specify subtitle file to use (also see -subfps, -subdelay)\n"
196 " -playlist <file> specify playlist file\n"
197 " -vid x -aid y select video (x) and audio (y) stream to play\n"
198 " -fps x -srate y change video (x fps) and audio (y Hz) rate\n"
199 " -pp <quality> enable postprocessing filter (details in the man page)\n"
200 " -framedrop enable frame dropping (for slow machines)\n"
201 "\n"
202 "Basic keys: (complete list in the man page, also check input.conf)\n"
203 " <- or -> seek backward/forward 10 seconds\n"
204 " down or up seek backward/forward 1 minute\n"
205 " pgdown or pgup seek backward/forward 10 minutes\n"
206 " < or > step backward/forward in playlist\n"
207 " p or SPACE pause movie (press any key to continue)\n"
208 " q or ESC stop playing and quit program\n"
209 " + or - adjust audio delay by +/- 0.1 second\n"
210 " o cycle OSD mode: none / seekbar / seekbar + timer\n"
211 " * or / increase or decrease PCM volume\n"
212 " x or z adjust subtitle delay by +/- 0.1 second\n"
213 " r or t adjust subtitle position up/down, also see -vf expand\n"
214 "\n"
215 " * * * SEE THE MAN PAGE FOR DETAILS, FURTHER (ADVANCED) OPTIONS AND KEYS * * *\n"
216 "\n");
219 #define Exit_SIGILL_RTCpuSel _(\
220 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
221 " It may be a bug in our new runtime CPU-detection code...\n"\
222 " Please read DOCS/HTML/en/bugreports.html.\n")
224 #define Exit_SIGILL _(\
225 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
226 " It usually happens when you run it on a CPU different than the one it was\n"\
227 " compiled/optimized for.\n"\
228 " Verify this!\n")
230 #define Exit_SIGSEGV_SIGFPE _(\
231 "- MPlayer crashed by bad usage of CPU/FPU/RAM.\n"\
232 " Recompile MPlayer with --enable-debug and make a 'gdb' backtrace and\n"\
233 " disassembly. Details in DOCS/HTML/en/bugreports_what.html#bugreports_crash.\n")
235 #define Exit_SIGCRASH _(\
236 "- MPlayer crashed. This shouldn't happen.\n"\
237 " It can be a bug in the MPlayer code _or_ in your drivers _or_ in your\n"\
238 " gcc version. If you think it's MPlayer's fault, please read\n"\
239 " DOCS/HTML/en/bugreports.html and follow the instructions there. We can't and\n"\
240 " won't help unless you provide this information when reporting a possible bug.\n")
242 #define SystemTooSlow _("\n\n"\
243 " ************************************************\n"\
244 " **** Your system is too SLOW to play this! ****\n"\
245 " ************************************************\n\n"\
246 "Possible reasons, problems, workarounds:\n"\
247 "- Most common: broken/buggy _audio_ driver\n"\
248 " - Try -ao sdl or use the OSS emulation of ALSA.\n"\
249 " - Experiment with different values for -autosync, 30 is a good start.\n"\
250 "- Slow video output\n"\
251 " - Try a different -vo driver (-vo help for a list) or try -framedrop!\n"\
252 "- Slow CPU\n"\
253 " - Don't try to play a big DVD/DivX on a slow CPU! Try some of the lavdopts,\n"\
254 " e.g. -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all.\n"\
255 "- Broken file\n"\
256 " - Try various combinations of -nobps -ni -forceidx -mc 0.\n"\
257 "- Slow media (NFS/SMB mounts, DVD, VCD etc)\n"\
258 " - Try -cache 8192.\n"\
259 "- Are you using -cache to play a non-interleaved AVI file?\n"\
260 " - Try -nocache.\n"\
261 "Read DOCS/HTML/en/video.html for tuning/speedup tips.\n"\
262 "If none of this helps you, read DOCS/HTML/en/bugreports.html.\n\n")
265 //**************************************************************************//
266 //**************************************************************************//
268 // Common FIFO functions, and keyboard/event FIFO code
269 #include "mp_fifo.h"
270 int noconsolecontrols=0;
271 //**************************************************************************//
273 // benchmark:
274 double video_time_usage=0;
275 double vout_time_usage=0;
276 static double audio_time_usage=0;
277 static int total_time_usage_start=0;
278 static int total_frame_cnt=0;
279 static int drop_frame_cnt=0; // total number of dropped frames
280 int benchmark=0;
282 // options:
283 int auto_quality=0;
284 static int output_quality=0;
286 static int list_properties = 0;
288 int term_osd = 1;
289 static char* term_osd_esc = "\x1b[A\r\x1b[K";
290 static char* playing_msg = NULL;
291 // seek:
292 static double seek_to_sec;
293 static off_t seek_to_byte=0;
294 static off_t step_sec=0;
296 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
298 // A/V sync:
299 int autosync=0; // 30 might be a good default value.
301 // codecs:
302 char **audio_codec_list=NULL; // override audio codec
303 char **video_codec_list=NULL; // override video codec
304 char **audio_fm_list=NULL; // override audio codec family
305 char **video_fm_list=NULL; // override video codec family
307 // demuxer:
308 extern char *demuxer_name; // override demuxer
309 extern char *audio_demuxer_name; // override audio demuxer
310 extern char *sub_demuxer_name; // override sub demuxer
312 // this dvdsub_id was selected via slang
313 // use this to allow dvdnav to follow -slang across stream resets,
314 // in particular the subtitle ID for a language changes
315 int dvdsub_lang_id;
316 int vobsub_id=-1;
317 char* audio_lang=NULL;
318 char* dvdsub_lang=NULL;
319 static char* spudec_ifo=NULL;
320 int forced_subs_only=0;
321 int file_filter=1;
323 // cache2:
324 int stream_cache_size=-1;
325 #ifdef CONFIG_STREAM_CACHE
326 extern int cache_fill_status;
328 float stream_cache_min_percent=20.0;
329 float stream_cache_seek_min_percent=50.0;
330 #else
331 #define cache_fill_status 0
332 #endif
334 // dump:
335 static char *stream_dump_name="stream.dump";
336 int stream_dump_type=0;
338 // A-V sync:
339 static float default_max_pts_correction=-1;//0.01f;
340 float audio_delay=0;
341 static int ignore_start=0;
343 static int softsleep=0;
345 double force_fps=0;
346 static int force_srate=0;
347 static int audio_output_format=-1; // AF_FORMAT_UNKNOWN
348 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
349 static int play_n_frames=-1;
350 static int play_n_frames_mf=-1;
352 // sub:
353 char *font_name=NULL;
354 char *sub_font_name=NULL;
355 extern int font_fontconfig;
356 float font_factor=0.75;
357 char **sub_name=NULL;
358 float sub_delay=0;
359 float sub_fps=0;
360 int sub_auto = 1;
361 char *vobsub_name=NULL;
362 /*DSP!!char *dsp=NULL;*/
363 int subcc_enabled=0;
364 int suboverlap_enabled = 1;
366 #include "ass_mp.h"
368 char* current_module=NULL; // for debugging
371 // ---
373 #ifdef CONFIG_MENU
374 #include "m_struct.h"
375 #include "libmenu/menu.h"
376 extern vf_info_t vf_info_menu;
377 static const vf_info_t* const libmenu_vfs[] = {
378 &vf_info_menu,
379 NULL
381 static vf_instance_t* vf_menu = NULL;
382 int use_menu = 0;
383 static char* menu_cfg = NULL;
384 static char* menu_root = "main";
385 #endif
388 #ifdef HAVE_RTC
389 static int nortc = 1;
390 static char* rtc_device;
391 #endif
393 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
394 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
395 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
396 int use_filedir_conf;
397 int use_filename_title;
399 #include "mpcommon.h"
400 #include "command.h"
402 #include "metadata.h"
404 #define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
406 const void *mpctx_get_video_out(MPContext *mpctx)
408 return mpctx->video_out;
411 const void *mpctx_get_audio_out(MPContext *mpctx)
413 return mpctx->audio_out;
416 void *mpctx_get_demuxer(MPContext *mpctx)
418 return mpctx->demuxer;
421 void *mpctx_get_playtree_iter(MPContext *mpctx)
423 return mpctx->playtree_iter;
426 void *mpctx_get_mixer(MPContext *mpctx)
428 return &mpctx->mixer;
431 int mpctx_get_global_sub_size(MPContext *mpctx)
433 return mpctx->global_sub_size;
436 int mpctx_get_osd_function(MPContext *mpctx)
438 return mpctx->osd_function;
441 static float get_relative_time(struct MPContext *mpctx)
443 unsigned int new_time = GetTimer();
444 unsigned int delta = new_time - mpctx->last_time;
445 mpctx->last_time = new_time;
446 return delta * 0.000001;
449 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
450 switch (type)
452 /* check for valid video stream */
453 case META_VIDEO_CODEC:
454 case META_VIDEO_BITRATE:
455 case META_VIDEO_RESOLUTION:
457 if (!mpctx->sh_video)
458 return 0;
459 break;
462 /* check for valid audio stream */
463 case META_AUDIO_CODEC:
464 case META_AUDIO_BITRATE:
465 case META_AUDIO_SAMPLES:
467 if (!mpctx->sh_audio)
468 return 0;
469 break;
472 /* check for valid demuxer */
473 case META_INFO_TITLE:
474 case META_INFO_ARTIST:
475 case META_INFO_ALBUM:
476 case META_INFO_YEAR:
477 case META_INFO_COMMENT:
478 case META_INFO_TRACK:
479 case META_INFO_GENRE:
481 if (!mpctx->demuxer)
482 return 0;
483 break;
486 default:
487 break;
490 return 1;
493 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
494 char **info = mpctx->demuxer->info;
495 int n;
497 if (!info || !tag)
498 return NULL;
500 for (n = 0; info[2*n] != NULL ; n++)
501 if (!strcasecmp (info[2*n], tag))
502 break;
504 return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
507 char *get_metadata(struct MPContext *mpctx, metadata_t type)
509 char *meta = NULL;
510 sh_audio_t * const sh_audio = mpctx->sh_audio;
511 sh_video_t * const sh_video = mpctx->sh_video;
513 if (!is_valid_metadata_type(mpctx, type))
514 return NULL;
516 switch (type)
518 case META_NAME:
520 return strdup (mp_basename2 (mpctx->filename));
523 case META_VIDEO_CODEC:
525 if (sh_video->format == 0x10000001)
526 meta = strdup ("mpeg1");
527 else if (sh_video->format == 0x10000002)
528 meta = strdup ("mpeg2");
529 else if (sh_video->format == 0x10000004)
530 meta = strdup ("mpeg4");
531 else if (sh_video->format == 0x10000005)
532 meta = strdup ("h264");
533 else if (sh_video->format >= 0x20202020)
535 meta = malloc (8);
536 sprintf (meta, "%.4s", (char *) &sh_video->format);
538 else
540 meta = malloc (8);
541 sprintf (meta, "0x%08X", sh_video->format);
543 return meta;
546 case META_VIDEO_BITRATE:
548 meta = malloc (16);
549 sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
550 return meta;
553 case META_VIDEO_RESOLUTION:
555 meta = malloc (16);
556 sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
557 return meta;
560 case META_AUDIO_CODEC:
562 if (sh_audio->codec && sh_audio->codec->name)
563 meta = strdup (sh_audio->codec->name);
564 return meta;
567 case META_AUDIO_BITRATE:
569 meta = malloc (16);
570 sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
571 return meta;
574 case META_AUDIO_SAMPLES:
576 meta = malloc (16);
577 sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
578 return meta;
581 /* check for valid demuxer */
582 case META_INFO_TITLE:
583 return get_demuxer_info(mpctx, "Title");
585 case META_INFO_ARTIST:
586 return get_demuxer_info(mpctx, "Artist");
588 case META_INFO_ALBUM:
589 return get_demuxer_info(mpctx, "Album");
591 case META_INFO_YEAR:
592 return get_demuxer_info(mpctx, "Year");
594 case META_INFO_COMMENT:
595 return get_demuxer_info(mpctx, "Comment");
597 case META_INFO_TRACK:
598 return get_demuxer_info(mpctx, "Track");
600 case META_INFO_GENRE:
601 return get_demuxer_info(mpctx, "Genre");
603 default:
604 break;
607 return meta;
610 /// step size of mixer changes
611 int volstep = 3;
613 #ifdef CONFIG_DVDNAV
614 static void mp_dvdnav_context_free(MPContext *ctx){
615 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
616 ctx->nav_smpi = NULL;
617 if (ctx->nav_buffer) free(ctx->nav_buffer);
618 ctx->nav_buffer = NULL;
619 ctx->nav_start = NULL;
620 ctx->nav_in_size = 0;
622 #endif
624 void uninit_player(struct MPContext *mpctx, unsigned int mask){
625 mask &= mpctx->initialized_flags;
627 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
629 if(mask&INITIALIZED_ACODEC){
630 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
631 current_module="uninit_acodec";
632 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
633 mpctx->sh_audio=NULL;
634 mpctx->mixer.afilter = NULL;
637 if(mask&INITIALIZED_VCODEC){
638 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
639 current_module="uninit_vcodec";
640 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
641 mpctx->sh_video=NULL;
642 #ifdef CONFIG_MENU
643 vf_menu=NULL;
644 #endif
647 if(mask&INITIALIZED_DEMUXER){
648 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
649 current_module="free_demuxer";
650 if (mpctx->num_sources) {
651 mpctx->demuxer = mpctx->sources[0].demuxer;
652 for (int i = 1; i < mpctx->num_sources; i++) {
653 free_stream(mpctx->sources[i].stream);
654 free_demuxer(mpctx->sources[i].demuxer);
657 talloc_free(mpctx->sources);
658 mpctx->sources = NULL;
659 mpctx->num_sources = 0;
660 talloc_free(mpctx->timeline);
661 mpctx->timeline = NULL;
662 mpctx->num_timeline_parts = 0;
663 talloc_free(mpctx->chapters);
664 mpctx->chapters = NULL;
665 mpctx->num_chapters = 0;
666 mpctx->video_offset = 0;
667 if(mpctx->demuxer){
668 mpctx->stream=mpctx->demuxer->stream;
669 free_demuxer(mpctx->demuxer);
671 mpctx->demuxer=NULL;
674 // kill the cache process:
675 if(mask&INITIALIZED_STREAM){
676 mpctx->initialized_flags&=~INITIALIZED_STREAM;
677 current_module="uninit_stream";
678 if(mpctx->stream) free_stream(mpctx->stream);
679 mpctx->stream=NULL;
682 if(mask&INITIALIZED_VO){
683 mpctx->initialized_flags&=~INITIALIZED_VO;
684 current_module="uninit_vo";
685 vo_destroy(mpctx->video_out);
686 mpctx->video_out=NULL;
687 #ifdef CONFIG_DVDNAV
688 mp_dvdnav_context_free(mpctx);
689 #endif
692 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
693 if(mask&INITIALIZED_GETCH2){
694 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
695 current_module="uninit_getch2";
696 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
697 // restore terminal:
698 getch2_disable();
701 if(mask&INITIALIZED_VOBSUB){
702 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
703 current_module="uninit_vobsub";
704 if(vo_vobsub) vobsub_close(vo_vobsub);
705 vo_vobsub=NULL;
708 if (mask&INITIALIZED_SPUDEC){
709 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
710 current_module="uninit_spudec";
711 spudec_free(vo_spudec);
712 vo_spudec=NULL;
715 if(mask&INITIALIZED_AO){
716 mpctx->initialized_flags&=~INITIALIZED_AO;
717 current_module="uninit_ao";
718 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
719 if (mpctx->audio_out)
720 mpctx->audio_out->uninit(mpctx->stop_play != AT_END_OF_FILE);
721 mpctx->audio_out=NULL;
724 current_module=NULL;
727 void exit_player_with_rc(struct MPContext *mpctx, enum exit_reason how, int rc)
729 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
730 uninit_player(mpctx, INITIALIZED_ALL);
731 #if defined(__MINGW32__) || defined(__CYGWIN__)
732 timeEndPeriod(1);
733 #endif
734 #ifdef CONFIG_X11
735 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
736 #endif
738 current_module="uninit_input";
739 mp_input_uninit(mpctx->input);
740 #ifdef CONFIG_MENU
741 if (use_menu)
742 menu_uninit();
743 #endif
745 #ifdef CONFIG_FREETYPE
746 current_module="uninit_font";
747 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
748 free_font_desc(mpctx->osd->sub_font);
749 free_font_desc(vo_font);
750 vo_font = NULL;
751 done_freetype();
752 #endif
753 osd_free(mpctx->osd);
755 #ifdef CONFIG_ASS
756 ass_library_done(ass_library);
757 ass_library = NULL;
758 #endif
760 current_module="exit_player";
762 // free mplayer config
763 if(mpctx->mconfig)
764 m_config_free(mpctx->mconfig);
765 mpctx->mconfig = NULL;
767 if(mpctx->playtree_iter)
768 play_tree_iter_free(mpctx->playtree_iter);
769 mpctx->playtree_iter = NULL;
770 if(mpctx->playtree)
771 play_tree_free(mpctx->playtree, 1);
772 mpctx->playtree = NULL;
774 talloc_free(mpctx->key_fifo);
776 if(edl_records != NULL) free(edl_records); // free mem allocated for EDL
777 edl_records = NULL;
778 switch(how) {
779 case EXIT_QUIT:
780 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Quit");
781 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
782 break;
783 case EXIT_EOF:
784 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","End of file");
785 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
786 break;
787 case EXIT_ERROR:
788 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Fatal error");
789 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
790 break;
791 default:
792 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
794 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
796 exit(rc);
799 static void exit_player(struct MPContext *mpctx, enum exit_reason how)
801 exit_player_with_rc(mpctx, how, 1);
804 #ifndef __MINGW32__
805 static void child_sighandler(int x){
806 pid_t pid;
807 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
809 #endif
811 #ifdef CONFIG_CRASH_DEBUG
812 static char *prog_path;
813 static int crash_debug = 0;
814 #endif
816 static void exit_sighandler(int x){
817 static int sig_count=0;
818 #ifdef CONFIG_CRASH_DEBUG
819 if (!crash_debug || x != SIGTRAP)
820 #endif
821 ++sig_count;
822 if(sig_count==5)
824 /* We're crashing bad and can't uninit cleanly :(
825 * by popular request, we make one last (dirty)
826 * effort to restore the user's
827 * terminal. */
828 getch2_disable();
829 exit(1);
831 if(sig_count==6) exit(1);
832 if(sig_count>6){
833 // can't stop :(
834 #ifndef __MINGW32__
835 kill(getpid(),SIGKILL);
836 #endif
838 mp_msg(MSGT_CPLAYER, MSGL_FATAL, "\n");
839 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,
840 "\nMPlayer interrupted by signal %d in module: %s\n", x,
841 current_module ? current_module : "unknown");
842 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
843 if(sig_count<=1)
844 switch(x){
845 case SIGINT:
846 case SIGQUIT:
847 case SIGTERM:
848 case SIGKILL:
849 async_quit_request = 1;
850 return; // killed from keyboard (^C) or killed [-9]
851 case SIGILL:
852 #if CONFIG_RUNTIME_CPUDETECT
853 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL_RTCpuSel);
854 #else
855 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL);
856 #endif
857 case SIGFPE:
858 case SIGSEGV:
859 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGSEGV_SIGFPE);
860 default:
861 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGCRASH);
862 #ifdef CONFIG_CRASH_DEBUG
863 if (crash_debug) {
864 int gdb_pid;
865 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
866 gdb_pid = fork();
867 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
868 if (gdb_pid == 0) { // We are the child
869 char spid[20];
870 snprintf(spid, sizeof(spid), "%i", getppid());
871 getch2_disable(); // allow terminal to work properly with gdb
872 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
873 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
874 } else if (gdb_pid < 0)
875 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
876 else {
877 waitpid(gdb_pid, NULL, 0);
879 if (x == SIGTRAP) return;
881 #endif
883 getch2_disable();
884 exit(1);
887 #include "cfg-mplayer.h"
889 static int cfg_include(m_option_t *conf, char *filename)
891 return m_config_parse_config_file(conf->priv, filename);
894 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
896 char *conffile;
897 int conffile_fd;
898 if (!disable_system_conf &&
899 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
900 exit_player(mpctx, EXIT_NONE);
901 if ((conffile = get_path("")) == NULL) {
902 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"Cannot find HOME directory.\n");
903 } else {
904 #ifdef __MINGW32__
905 mkdir(conffile);
906 #else
907 mkdir(conffile, 0777);
908 #endif
909 free(conffile);
910 if ((conffile = get_path("config")) == NULL) {
911 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
912 } else {
913 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
914 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Creating config file: %s\n", conffile);
915 write(conffile_fd, default_config, strlen(default_config));
916 close(conffile_fd);
918 if (!disable_user_conf &&
919 m_config_parse_config_file(conf, conffile) < 0)
920 exit_player(mpctx, EXIT_NONE);
921 free(conffile);
926 #define PROFILE_CFG_PROTOCOL "protocol."
928 static void load_per_protocol_config (m_config_t* conf, const char *const file)
930 char *str;
931 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
932 m_profile_t *p;
934 /* does filename actually uses a protocol ? */
935 str = strstr (file, "://");
936 if (!str)
937 return;
939 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
940 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
941 p = m_config_get_profile (conf, protocol);
942 if (p)
944 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading protocol-related profile '%s'\n", protocol);
945 m_config_set_profile(conf,p);
949 #define PROFILE_CFG_EXTENSION "extension."
951 static void load_per_extension_config (m_config_t* conf, const char *const file)
953 char *str;
954 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
955 m_profile_t *p;
957 /* does filename actually have an extension ? */
958 str = strrchr (file, '.');
959 if (!str)
960 return;
962 sprintf (extension, PROFILE_CFG_EXTENSION);
963 strncat (extension, ++str, 7);
964 p = m_config_get_profile (conf, extension);
965 if (p)
967 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", extension);
968 m_config_set_profile(conf,p);
972 #define PROFILE_CFG_VO "vo."
973 #define PROFILE_CFG_AO "ao."
975 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
977 char profile[strlen (cfg) + strlen (out) + 1];
978 m_profile_t *p;
980 sprintf (profile, "%s%s", cfg, out);
981 p = m_config_get_profile (conf, profile);
982 if (p)
984 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", profile);
985 m_config_set_profile(conf,p);
990 * Tries to load a config file
991 * @return 0 if file was not found, 1 otherwise
993 static int try_load_config(m_config_t *conf, const char *file)
995 struct stat st;
996 if (stat(file, &st))
997 return 0;
998 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Loading config '%s'\n", file);
999 m_config_parse_config_file (conf, file);
1000 return 1;
1003 static void load_per_file_config (m_config_t* conf, const char *const file)
1005 char *confpath;
1006 char cfg[PATH_MAX];
1007 char *name;
1009 if (strlen(file) > PATH_MAX - 14) {
1010 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Filename is too long, can not load file or directory specific config files\n");
1011 return;
1013 sprintf (cfg, "%s.conf", file);
1015 name = strrchr(cfg, '/');
1016 if (HAVE_DOS_PATHS) {
1017 char *tmp = strrchr(cfg, '\\');
1018 if (!name || tmp > name)
1019 name = tmp;
1020 tmp = strrchr(cfg, ':');
1021 if (!name || tmp > name)
1022 name = tmp;
1024 if (!name)
1025 name = cfg;
1026 else
1027 name++;
1029 if (use_filedir_conf) {
1030 char dircfg[PATH_MAX];
1031 strcpy(dircfg, cfg);
1032 strcpy(dircfg + (name - cfg), "mplayer.conf");
1033 try_load_config(conf, dircfg);
1035 if (try_load_config(conf, cfg))
1036 return;
1039 if ((confpath = get_path (name)) != NULL)
1041 try_load_config(conf, confpath);
1043 free (confpath);
1047 /* When libmpdemux performs a blocking operation (network connection or
1048 * cache filling) if the operation fails we use this function to check
1049 * if it was interrupted by the user.
1050 * The function returns a new value for eof. */
1051 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
1053 mp_cmd_t* cmd;
1054 if((cmd = mp_input_get_cmd(mpctx->input, 0, 0)) != NULL) {
1055 switch(cmd->id) {
1056 case MP_CMD_QUIT:
1057 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
1058 case MP_CMD_PLAY_TREE_STEP: {
1059 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
1060 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
1061 } break;
1062 case MP_CMD_PLAY_TREE_UP_STEP: {
1063 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
1064 } break;
1065 case MP_CMD_PLAY_ALT_SRC_STEP: {
1066 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
1067 } break;
1069 mp_cmd_free(cmd);
1071 return stop_play;
1074 #define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
1076 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
1078 play_tree_add_bpf(entry,mpctx->filename);
1081 if(!entry) {
1082 entry = mpctx->playtree_iter->tree;
1083 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1084 return PT_NEXT_ENTRY;
1086 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
1087 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1088 return PT_NEXT_ENTRY;
1091 play_tree_remove(entry,1,1);
1092 return PT_NEXT_SRC;
1094 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
1095 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1096 entry = mpctx->playtree_iter->tree;
1097 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1098 return PT_NEXT_ENTRY;
1100 play_tree_remove(entry,1,1);
1102 return PT_NEXT_SRC;
1105 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1107 struct MPOpts *opts = &mpctx->opts;
1108 sub_data *subd = NULL;
1109 struct ass_track *asst = NULL;
1111 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES) {
1112 return;
1115 #ifdef CONFIG_ASS
1116 if (opts->ass_enabled) {
1117 #ifdef CONFIG_ICONV
1118 asst = ass_read_stream(ass_library, filename, sub_cp);
1119 #else
1120 asst = ass_read_stream(ass_library, filename, 0);
1121 #endif
1122 if (!asst) {
1123 subd = sub_read_file(filename, fps);
1124 if (subd) {
1125 asst = ass_read_subdata(ass_library, subd, fps);
1126 if (asst) {
1127 sub_free(subd);
1128 subd = NULL;
1132 } else
1133 #endif
1134 subd = sub_read_file(filename, fps);
1137 if (!asst && !subd) {
1138 mp_tmsg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR,
1139 "Cannot load subtitles: %s\n", filename_recode(filename));
1140 return;
1143 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1144 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1145 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1146 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1147 filename_recode(filename));
1148 ++mpctx->set_of_sub_size;
1149 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "SUB: Added subtitle file (%d): %s\n", mpctx->set_of_sub_size,
1150 filename_recode(filename));
1153 void init_vo_spudec(struct MPContext *mpctx)
1155 if (vo_spudec)
1156 spudec_free(vo_spudec);
1157 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1158 vo_spudec = NULL;
1160 // we currently can't work without video stream
1161 if (!mpctx->sh_video)
1162 return;
1164 if (spudec_ifo) {
1165 unsigned int palette[16], width, height;
1166 current_module="spudec_init_vobsub";
1167 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1168 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1171 #ifdef CONFIG_DVDREAD
1172 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1173 current_module="spudec_init_dvdread";
1174 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1175 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1176 NULL, 0);
1178 #endif
1180 #ifdef CONFIG_DVDNAV
1181 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1182 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1183 current_module="spudec_init_dvdnav";
1184 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1186 #endif
1188 if (vo_spudec==NULL) {
1189 sh_sub_t *sh = (sh_sub_t *)mpctx->d_sub->sh;
1190 current_module="spudec_init_normal";
1191 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1192 spudec_set_font_factor(vo_spudec,font_factor);
1195 if (vo_spudec!=NULL) {
1196 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1197 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1202 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1203 * make it all work is to use the builtin SDL-bootstrap code, which
1204 * will be done automatically by replacing our main() if we include SDL.h.
1206 #if defined(__APPLE__) && defined(CONFIG_SDL)
1207 #ifdef CONFIG_SDL_SDL_H
1208 #include <SDL/SDL.h>
1209 #else
1210 #include <SDL.h>
1211 #endif
1212 #endif
1215 * \brief append a formatted string
1216 * \param buf buffer to print into
1217 * \param pos position of terminating 0 in buf
1218 * \param len maximum number of characters in buf, not including terminating 0
1219 * \param format printf format string
1221 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1223 va_list va;
1224 va_start(va, format);
1225 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1226 va_end(va);
1227 if (*pos >= len ) {
1228 buf[len] = 0;
1229 *pos = len;
1234 * \brief append time in the hh:mm:ss.f format
1235 * \param buf buffer to print into
1236 * \param pos position of terminating 0 in buf
1237 * \param len maximum number of characters in buf, not including terminating 0
1238 * \param time time value to convert/append
1240 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1241 int64_t tenths = 10 * time;
1242 int f1 = tenths % 10;
1243 int ss = (tenths / 10) % 60;
1244 int mm = (tenths / 600) % 60;
1245 int hh = tenths / 36000;
1246 if (time <= 0) {
1247 saddf(buf, pos, len, "unknown");
1248 return;
1250 if (hh > 0)
1251 saddf(buf, pos, len, "%2d:", hh);
1252 if (hh > 0 || mm > 0)
1253 saddf(buf, pos, len, "%02d:", mm);
1254 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1257 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1259 struct MPOpts *opts = &mpctx->opts;
1260 sh_video_t * const sh_video = mpctx->sh_video;
1262 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1263 a_pos = playing_audio_pts(mpctx);
1264 if (mpctx->sh_audio && sh_video && at_frame) {
1265 mpctx->last_av_difference = a_pos - sh_video->pts - audio_delay;
1266 if (mpctx->time_frame > 0)
1267 mpctx->last_av_difference += mpctx->time_frame * opts->playback_speed;
1268 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1269 && !mpctx->drop_message_shown) {
1270 mp_tmsg(MSGT_AVSYNC,MSGL_WARN,SystemTooSlow);
1271 mpctx->drop_message_shown = true;
1274 if (quiet)
1275 return;
1278 int width;
1279 char *line;
1280 unsigned pos = 0;
1281 get_screen_size();
1282 if (screen_width > 0)
1283 width = screen_width;
1284 else
1285 width = 80;
1286 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1287 /* Windows command line is broken (MinGW's rxvt works, but we
1288 * should not depend on that). */
1289 width--;
1290 #endif
1291 line = malloc(width + 1); // one additional char for the terminating null
1293 // Audio time
1294 if (mpctx->sh_audio) {
1295 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1296 if (!sh_video) {
1297 float len = demuxer_get_time_length(mpctx->demuxer);
1298 saddf(line, &pos, width, "(");
1299 sadd_hhmmssf(line, &pos, width, a_pos);
1300 saddf(line, &pos, width, ") of %.1f (", len);
1301 sadd_hhmmssf(line, &pos, width, len);
1302 saddf(line, &pos, width, ") ");
1306 // Video time
1307 if (sh_video)
1308 saddf(line, &pos, width, "V:%6.1f ", sh_video->pts);
1310 // A-V sync
1311 if (mpctx->sh_audio && sh_video)
1312 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1313 mpctx->last_av_difference, mpctx->total_avsync_change);
1315 // Video stats
1316 if (sh_video)
1317 saddf(line, &pos, width, "%3d/%3d ",
1318 (int)sh_video->num_frames,
1319 (int)sh_video->num_frames_decoded);
1321 // CPU usage
1322 if (sh_video) {
1323 if (sh_video->timer > 0.5)
1324 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1325 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1326 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1327 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1328 else
1329 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1330 } else if (mpctx->sh_audio) {
1331 if (mpctx->delay > 0.5)
1332 saddf(line, &pos, width, "%4.1f%% ",
1333 100.0*audio_time_usage/(double)mpctx->delay);
1334 else
1335 saddf(line, &pos, width, "??,?%% ");
1338 // VO stats
1339 if (sh_video)
1340 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1342 #ifdef CONFIG_STREAM_CACHE
1343 // cache stats
1344 if (stream_cache_size > 0)
1345 saddf(line, &pos, width, "%d%% ", cache_fill_status);
1346 #endif
1348 // other
1349 if (opts->playback_speed != 1)
1350 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1352 // end
1353 if (erase_to_end_of_line) {
1354 line[pos] = 0;
1355 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1356 } else {
1357 memset(&line[pos], ' ', width - pos);
1358 line[width] = 0;
1359 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1361 free(line);
1365 * \brief build a chain of audio filters that converts the input format
1366 * to the ao's format, taking into account the current playback_speed.
1367 * \param sh_audio describes the requested input format of the chain.
1368 * \param ao_data describes the requested output format of the chain.
1370 int build_afilter_chain(struct MPContext *mpctx, sh_audio_t *sh_audio, ao_data_t *ao_data)
1372 struct MPOpts *opts = &mpctx->opts;
1373 int new_srate;
1374 int result;
1375 if (!sh_audio)
1377 mpctx->mixer.afilter = NULL;
1378 return 0;
1380 if(af_control_any_rev(sh_audio->afilter,
1381 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1382 &opts->playback_speed)) {
1383 new_srate = sh_audio->samplerate;
1384 } else {
1385 new_srate = sh_audio->samplerate * opts->playback_speed;
1386 if (new_srate != ao_data->samplerate) {
1387 // limits are taken from libaf/af_resample.c
1388 if (new_srate < 8000)
1389 new_srate = 8000;
1390 if (new_srate > 192000)
1391 new_srate = 192000;
1392 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1395 result = init_audio_filters(sh_audio, new_srate,
1396 &ao_data->samplerate, &ao_data->channels, &ao_data->format);
1397 mpctx->mixer.afilter = sh_audio->afilter;
1398 return result;
1402 typedef struct mp_osd_msg mp_osd_msg_t;
1403 struct mp_osd_msg {
1404 /// Previous message on the stack.
1405 mp_osd_msg_t* prev;
1406 /// Message text.
1407 char msg[128];
1408 int id,level,started;
1409 /// Display duration in ms.
1410 unsigned time;
1413 /// OSD message stack.
1414 static mp_osd_msg_t* osd_msg_stack = NULL;
1417 * \brief Add a message on the OSD message stack
1419 * If a message with the same id is already present in the stack
1420 * it is pulled on top of the stack, otherwise a new message is created.
1423 static void set_osd_msg_va(int id, int level, int time, const char *fmt,
1424 va_list ap)
1426 mp_osd_msg_t *msg,*last=NULL;
1427 int r;
1429 // look if the id is already in the stack
1430 for(msg = osd_msg_stack ; msg && msg->id != id ;
1431 last = msg, msg = msg->prev);
1432 // not found: alloc it
1433 if(!msg) {
1434 msg = calloc(1,sizeof(mp_osd_msg_t));
1435 msg->prev = osd_msg_stack;
1436 osd_msg_stack = msg;
1437 } else if(last) { // found, but it's not on top of the stack
1438 last->prev = msg->prev;
1439 msg->prev = osd_msg_stack;
1440 osd_msg_stack = msg;
1442 // write the msg
1443 r = vsnprintf(msg->msg, 128, fmt, ap);
1444 if(r >= 128) msg->msg[127] = 0;
1445 // set id and time
1446 msg->id = id;
1447 msg->level = level;
1448 msg->time = time;
1452 void set_osd_msg(int id, int level, int time, const char *fmt, ...)
1454 va_list ap;
1455 va_start(ap, fmt);
1456 set_osd_msg_va(id, level, time, fmt, ap);
1457 va_end(ap);
1460 void set_osd_tmsg(int id, int level, int time, const char *fmt, ...)
1462 va_list ap;
1463 va_start(ap, fmt);
1464 set_osd_msg_va(id, level, time, mp_gtext(fmt), ap);
1465 va_end(ap);
1470 * \brief Remove a message from the OSD stack
1472 * This function can be used to get rid of a message right away.
1476 void rm_osd_msg(int id) {
1477 mp_osd_msg_t *msg,*last=NULL;
1479 // Search for the msg
1480 for(msg = osd_msg_stack ; msg && msg->id != id ;
1481 last = msg, msg = msg->prev);
1482 if(!msg) return;
1484 // Detach it from the stack and free it
1485 if(last)
1486 last->prev = msg->prev;
1487 else
1488 osd_msg_stack = msg->prev;
1489 free(msg);
1493 * \brief Remove all messages from the OSD stack
1497 static void clear_osd_msgs(void) {
1498 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1499 while(msg) {
1500 prev = msg->prev;
1501 free(msg);
1502 msg = prev;
1504 osd_msg_stack = NULL;
1508 * \brief Get the current message from the OSD stack.
1510 * This function decrements the message timer and destroys the old ones.
1511 * The message that should be displayed is returned (if any).
1515 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1517 struct MPOpts *opts = &mpctx->opts;
1518 mp_osd_msg_t *msg,*prev,*last = NULL;
1519 static unsigned last_update = 0;
1520 unsigned now = GetTimerMS();
1521 unsigned diff;
1522 char hidden_dec_done = 0;
1524 if (mpctx->osd_visible) {
1525 // 36000000 means max timed visibility is 1 hour into the future, if
1526 // the difference is greater assume it's wrapped around from below 0
1527 if (mpctx->osd_visible - now > 36000000) {
1528 mpctx->osd_visible = 0;
1529 vo_osd_progbar_type = -1; // disable
1530 vo_osd_changed(OSDTYPE_PROGBAR);
1531 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1534 if (mpctx->osd_show_percentage_until - now > 36000000)
1535 mpctx->osd_show_percentage_until = 0;
1537 if(!last_update) last_update = now;
1538 diff = now >= last_update ? now - last_update : 0;
1540 last_update = now;
1542 // Look for the first message in the stack with high enough level.
1543 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1544 prev = msg->prev;
1545 if (msg->level > opts->osd_level && hidden_dec_done)
1546 continue;
1547 // The message has a high enough level or it is the first hidden one
1548 // in both cases we decrement the timer or kill it.
1549 if(!msg->started || msg->time > diff) {
1550 if(msg->started) msg->time -= diff;
1551 else msg->started = 1;
1552 // display it
1553 if (msg->level <= opts->osd_level)
1554 return msg;
1555 hidden_dec_done = 1;
1556 continue;
1558 // kill the message
1559 free(msg);
1560 if(last) {
1561 last->prev = prev;
1562 msg = last;
1563 } else {
1564 osd_msg_stack = prev;
1565 msg = NULL;
1568 // Nothing found
1569 return NULL;
1573 * \brief Display the OSD bar.
1575 * Display the OSD bar or fall back on a simple message.
1579 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1580 struct MPOpts *opts = &mpctx->opts;
1581 if (opts->osd_level < 1)
1582 return;
1584 if(mpctx->sh_video) {
1585 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1586 vo_osd_progbar_type = type;
1587 vo_osd_progbar_value = 256*(val-min)/(max-min);
1588 vo_osd_changed(OSDTYPE_PROGBAR);
1589 return;
1592 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1593 name, ROUND(100*(val-min)/(max-min)));
1597 * \brief Display text subtitles on the OSD
1599 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs)
1601 int i;
1602 vo_sub = subs;
1603 vo_osd_changed(OSDTYPE_SUBTITLE);
1604 if (!mpctx->sh_video) {
1605 // reverse order, since newest set_osd_msg is displayed first
1606 for (i = SUB_MAX_TEXT - 1; i >= 0; i--) {
1607 if (!subs || i >= subs->lines || !subs->text[i])
1608 rm_osd_msg(OSD_MSG_SUB_BASE + i);
1609 else {
1610 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1611 int display_time = i == subs->lines - 1 ? 180000 : 2000;
1612 set_osd_msg(OSD_MSG_SUB_BASE + i, 1, display_time, "%s", subs->text[i]);
1619 * \brief Update the OSD message line.
1621 * This function displays the current message on the vo OSD or on the term.
1622 * If the stack is empty and the OSD level is high enough the timer
1623 * is displayed (only on the vo OSD).
1627 static void update_osd_msg(struct MPContext *mpctx)
1629 struct MPOpts *opts = &mpctx->opts;
1630 mp_osd_msg_t *msg;
1631 struct osd_state *osd = mpctx->osd;
1632 char osd_text_timer[128];
1634 if (mpctx->add_osd_seek_info) {
1635 double percentage;
1636 if (mpctx->timeline && mpctx->sh_video)
1637 percentage = mpctx->sh_video->pts * 100 /
1638 mpctx->timeline[mpctx->num_timeline_parts].start;
1639 else
1640 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1641 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1642 if (mpctx->sh_video)
1643 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1644 mpctx->add_osd_seek_info = false;
1647 // Look if we have a msg
1648 if((msg = get_osd_msg(mpctx))) {
1649 if (strcmp(osd->osd_text, msg->msg)) {
1650 strncpy(osd->osd_text, msg->msg, 127);
1651 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1652 if(term_osd) mp_msg(MSGT_CPLAYER,MSGL_STATUS,"%s%s\n",term_osd_esc,msg->msg);
1654 return;
1657 if(mpctx->sh_video) {
1658 // fallback on the timer
1659 if (opts->osd_level >= 2) {
1660 int len;
1661 if (mpctx->timeline)
1662 len = mpctx->timeline[mpctx->num_timeline_parts].start;
1663 else
1664 len = demuxer_get_time_length(mpctx->demuxer);
1665 int percentage = -1;
1666 char percentage_text[10];
1667 int pts = demuxer_get_current_time(mpctx->demuxer);
1669 if (mpctx->osd_show_percentage_until)
1670 if (mpctx->timeline)
1671 percentage = mpctx->sh_video->pts * 100 /
1672 mpctx->timeline[mpctx->num_timeline_parts].start;
1673 else
1674 percentage = demuxer_get_percent_pos(mpctx->demuxer);
1676 if (percentage >= 0)
1677 snprintf(percentage_text, 9, " (%d%%)", percentage);
1678 else
1679 percentage_text[0] = 0;
1681 if (opts->osd_level == 3)
1682 snprintf(osd_text_timer, 63,
1683 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1684 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1685 len/3600,(len/60)%60,len%60,percentage_text);
1686 else
1687 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1688 mpctx->osd_function,pts/3600,(pts/60)%60,
1689 pts%60,percentage_text);
1690 } else
1691 osd_text_timer[0]=0;
1693 if (strcmp(osd->osd_text, osd_text_timer)) {
1694 strncpy(osd->osd_text, osd_text_timer, 63);
1695 vo_osd_changed(OSDTYPE_OSD);
1697 return;
1700 // Clear the term osd line
1701 if (term_osd && osd->osd_text[0]) {
1702 osd->osd_text[0] = 0;
1703 printf("%s\n",term_osd_esc);
1707 ///@}
1708 // OSDMsgStack
1711 void reinit_audio_chain(struct MPContext *mpctx)
1713 struct MPOpts *opts = &mpctx->opts;
1714 if (!mpctx->sh_audio)
1715 return;
1716 current_module="init_audio_codec";
1717 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1718 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1719 goto init_error;
1721 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1722 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1725 current_module="af_preinit";
1726 ao_data.samplerate=force_srate;
1727 ao_data.channels=0;
1728 ao_data.format=audio_output_format;
1729 // first init to detect best values
1730 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1731 // input:
1732 mpctx->sh_audio->samplerate,
1733 // output:
1734 &ao_data.samplerate, &ao_data.channels, &ao_data.format)){
1735 mp_tmsg(MSGT_CPLAYER,MSGL_ERR, "Error at audio filter chain "
1736 "pre-init!\n");
1737 exit_player(mpctx, EXIT_ERROR);
1739 current_module="ao2_init";
1740 mpctx->audio_out = init_best_audio_out(opts->audio_driver_list,
1741 0, // plugin flag
1742 ao_data.samplerate,
1743 ao_data.channels,
1744 ao_data.format, 0);
1745 if(!mpctx->audio_out){
1746 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Could not open/initialize audio device -> no sound.\n");
1747 goto init_error;
1749 mpctx->initialized_flags|=INITIALIZED_AO;
1750 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1751 mpctx->audio_out->info->short_name,
1752 ao_data.samplerate, ao_data.channels,
1753 af_fmt2str_short(ao_data.format),
1754 af_fmt2bits(ao_data.format)/8 );
1755 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1756 mpctx->audio_out->info->name, mpctx->audio_out->info->author);
1757 if(strlen(mpctx->audio_out->info->comment) > 0)
1758 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Comment: %s\n", mpctx->audio_out->info->comment);
1759 // init audio filters:
1760 current_module="af_init";
1761 if(!build_afilter_chain(mpctx, mpctx->sh_audio, &ao_data)) {
1762 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter/ao format!\n");
1763 goto init_error;
1765 mpctx->mixer.audio_out = mpctx->audio_out;
1766 mpctx->mixer.volstep = volstep;
1767 return;
1769 init_error:
1770 uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec and possibly AO
1771 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1772 mpctx->d_audio->id = -2;
1776 ///@}
1777 // Command2Property
1780 // Return pts value corresponding to the end point of audio written to the
1781 // ao so far.
1782 static double written_audio_pts(struct MPContext *mpctx)
1784 sh_audio_t *sh_audio = mpctx->sh_audio;
1785 demux_stream_t *d_audio = mpctx->d_audio;
1786 double buffered_output;
1787 // first calculate the end pts of audio that has been output by decoder
1788 double a_pts = sh_audio->pts;
1789 if (a_pts != MP_NOPTS_VALUE)
1790 // Good, decoder supports new way of calculating audio pts.
1791 // sh_audio->pts is the timestamp of the latest input packet with
1792 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1793 // the amount of bytes the decoder has written after that timestamp.
1794 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1795 else {
1796 // Decoder doesn't support new way of calculating pts (or we're
1797 // being called before it has decoded anything with known timestamp).
1798 // Use the old method of audio pts calculation: take the timestamp
1799 // of last packet with known pts the decoder has read data from,
1800 // and add amount of bytes read after the beginning of that packet
1801 // divided by input bps. This will be inaccurate if the input/output
1802 // ratio is not constant for every audio packet or if it is constant
1803 // but not accurately known in sh_audio->i_bps.
1805 a_pts = d_audio->pts;
1806 // ds_tell_pts returns bytes read after last timestamp from
1807 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1808 // it has read but not decoded
1809 if (sh_audio->i_bps)
1810 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1811 (double)sh_audio->i_bps;
1813 // Now a_pts hopefully holds the pts for end of audio from decoder.
1814 // Substract data in buffers between decoder and audio out.
1816 // Decoded but not filtered
1817 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1819 // Data buffered in audio filters, measured in bytes of "missing" output
1820 buffered_output = af_calc_delay(sh_audio->afilter);
1822 // Data that was ready for ao but was buffered because ao didn't fully
1823 // accept everything to internal buffers yet
1824 buffered_output += sh_audio->a_out_buffer_len;
1826 // Filters divide audio length by playback_speed, so multiply by it
1827 // to get the length in original units without speedup or slowdown
1828 a_pts -= buffered_output * mpctx->opts.playback_speed / ao_data.bps;
1830 return a_pts + mpctx->video_offset;
1833 // Return pts value corresponding to currently playing audio.
1834 double playing_audio_pts(struct MPContext *mpctx)
1836 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1837 mpctx->audio_out->get_delay();
1840 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
1841 struct MPOpts *opts = &mpctx->opts;
1842 // check for frame-drop:
1843 current_module = "check_framedrop";
1844 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
1845 static int dropped_frames;
1846 float delay = opts->playback_speed*mpctx->audio_out->get_delay();
1847 float d = delay-mpctx->delay;
1848 ++total_frame_cnt;
1849 // we should avoid dropping too many frames in sequence unless we
1850 // are too late. and we allow 100ms A-V delay here:
1851 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
1852 && !mpctx->update_video_immediately) {
1853 ++drop_frame_cnt;
1854 ++dropped_frames;
1855 return frame_dropping;
1856 } else
1857 dropped_frames = 0;
1859 return 0;
1863 #ifdef HAVE_RTC
1864 int rtc_fd = -1;
1865 #endif
1867 static float timing_sleep(struct MPContext *mpctx, float time_frame)
1869 #ifdef HAVE_RTC
1870 if (rtc_fd >= 0){
1871 // -------- RTC -----------
1872 current_module="sleep_rtc";
1873 while (time_frame > 0.000) {
1874 unsigned long rtc_ts;
1875 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
1876 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC read error: %s\n", strerror(errno));
1877 time_frame -= get_relative_time(mpctx);
1879 } else
1880 #endif
1882 // assume kernel HZ=100 for softsleep, works with larger HZ but with
1883 // unnecessarily high CPU usage
1884 float margin = softsleep ? 0.011 : 0;
1885 current_module = "sleep_timer";
1886 while (time_frame > margin) {
1887 usec_sleep(1000000 * (time_frame - margin));
1888 time_frame -= get_relative_time(mpctx);
1890 if (softsleep){
1891 current_module = "sleep_soft";
1892 if (time_frame < 0)
1893 mp_tmsg(MSGT_AVSYNC, MSGL_WARN, "Warning! Softsleep underflow!\n");
1894 while (time_frame > 0)
1895 time_frame -= get_relative_time(mpctx); // burn the CPU
1898 return time_frame;
1901 static void select_subtitle(MPContext *mpctx)
1903 struct MPOpts *opts = &mpctx->opts;
1904 // find the best sub to use
1905 int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
1906 mpctx->global_sub_pos = -1; // no subs by default
1907 if (vobsub_index_id >= 0) {
1908 // if user asks for a vobsub id, use that first.
1909 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
1910 } else if (opts->sub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1911 // if user asks for a dvd sub id, use that next.
1912 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1913 } else if (mpctx->global_sub_indices[SUB_SOURCE_SUBS] >= 0) {
1914 // if there are text subs to use, use those. (autosubs come last here)
1915 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_SUBS];
1916 } else if (opts->sub_id == -1 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
1917 // finally select subs by language and container hints
1918 if (opts->sub_id == -1 && dvdsub_lang)
1919 opts->sub_id = demuxer_sub_track_by_lang(mpctx->demuxer, dvdsub_lang);
1920 if (opts->sub_id == -1)
1921 opts->sub_id = demuxer_default_sub_track(mpctx->demuxer);
1922 if (opts->sub_id >= 0)
1923 mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id;
1925 // rather than duplicate code, use the SUB_SELECT handler to init the right one.
1926 mpctx->global_sub_pos--;
1927 mp_property_do("sub",M_PROPERTY_STEP_UP,NULL, mpctx);
1930 #ifdef CONFIG_DVDNAV
1931 #ifndef FF_B_TYPE
1932 #define FF_B_TYPE 3
1933 #endif
1934 /// store decoded video image
1935 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
1936 mp_image_t *from_mpi) {
1937 mp_image_t *mpi;
1939 /// Do not store B-frames
1940 if (from_mpi->pict_type == FF_B_TYPE)
1941 return to_mpi;
1943 if (to_mpi &&
1944 to_mpi->w == from_mpi->w &&
1945 to_mpi->h == from_mpi->h &&
1946 to_mpi->imgfmt == from_mpi->imgfmt)
1947 mpi = to_mpi;
1948 else {
1949 if (to_mpi)
1950 free_mp_image(to_mpi);
1951 if (from_mpi->w == 0 || from_mpi->h == 0)
1952 return NULL;
1953 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
1956 copy_mpi(mpi,from_mpi);
1957 return mpi;
1960 static void mp_dvdnav_reset_stream (MPContext *ctx) {
1961 struct MPOpts *opts = &ctx->opts;
1962 if (ctx->sh_video) {
1963 /// clear video pts
1964 ctx->d_video->pts = 0.0f;
1965 ctx->sh_video->pts = 0.0f;
1966 ctx->sh_video->i_pts = 0.0f;
1967 ctx->sh_video->last_pts = 0.0f;
1968 ctx->sh_video->num_buffered_pts = 0;
1969 ctx->sh_video->num_frames = 0;
1970 ctx->sh_video->num_frames_decoded = 0;
1971 ctx->sh_video->timer = 0.0f;
1972 ctx->sh_video->stream_delay = 0.0f;
1973 ctx->sh_video->timer = 0;
1974 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
1977 if (ctx->sh_audio) {
1978 /// free audio packets and reset
1979 ds_free_packs(ctx->d_audio);
1980 audio_delay -= ctx->sh_audio->stream_delay;
1981 ctx->delay =- audio_delay;
1982 ctx->audio_out->reset();
1983 resync_audio_stream(ctx->sh_audio);
1986 audio_delay = 0.0f;
1987 ctx->global_sub_size = ctx->global_sub_indices[SUB_SOURCE_DEMUX] + mp_dvdnav_number_of_subs(ctx->stream);
1988 if (dvdsub_lang && opts->sub_id == dvdsub_lang_id) {
1989 dvdsub_lang_id = mp_dvdnav_sid_from_lang(ctx->stream, dvdsub_lang);
1990 if (dvdsub_lang_id != opts->sub_id) {
1991 opts->sub_id = dvdsub_lang_id;
1992 select_subtitle(ctx);
1996 /// clear all EOF related flags
1997 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
2000 /// Restore last decoded DVDNAV (still frame)
2001 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
2002 int *in_size,
2003 unsigned char **start,
2004 mp_image_t *decoded_frame)
2006 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
2007 return decoded_frame;
2009 /// a change occured in dvdnav stream
2010 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
2011 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
2012 mp_dvdnav_context_free(mpctx);
2013 mp_dvdnav_reset_stream(mpctx);
2014 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
2015 mp_dvdnav_cell_has_changed(mpctx->stream,1);
2018 if (*in_size < 0) {
2019 float len;
2021 /// Display still frame, if any
2022 if (mpctx->nav_smpi && !mpctx->nav_buffer)
2023 decoded_frame = mpctx->nav_smpi;
2025 /// increment video frame : continue playing after still frame
2026 len = demuxer_get_time_length(mpctx->demuxer);
2027 if (mpctx->sh_video->pts >= len &&
2028 mpctx->sh_video->pts > 0.0 && len > 0.0) {
2029 mp_dvdnav_skip_still(mpctx->stream);
2030 mp_dvdnav_skip_wait(mpctx->stream);
2032 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
2034 if (mpctx->nav_buffer) {
2035 *start = mpctx->nav_start;
2036 *in_size = mpctx->nav_in_size;
2037 if (mpctx->nav_start)
2038 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
2042 return decoded_frame;
2045 /// Save last decoded DVDNAV (still frame)
2046 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
2047 unsigned char *start,
2048 mp_image_t *decoded_frame)
2050 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
2051 return;
2053 if (mpctx->nav_buffer)
2054 free(mpctx->nav_buffer);
2056 mpctx->nav_buffer = malloc(in_size);
2057 mpctx->nav_start = start;
2058 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
2059 if (mpctx->nav_buffer)
2060 memcpy(mpctx->nav_buffer,start,in_size);
2062 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
2063 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
2065 #endif /* CONFIG_DVDNAV */
2067 /* Modify video timing to match the audio timeline. There are two main
2068 * reasons this is needed. First, video and audio can start from different
2069 * positions at beginning of file or after a seek (MPlayer starts both
2070 * immediately even if they have different pts). Second, the file can have
2071 * audio timestamps that are inconsistent with the duration of the audio
2072 * packets, for example two consecutive timestamp values differing by
2073 * one second but only a packet with enough samples for half a second
2074 * of playback between them.
2076 static void adjust_sync(struct MPContext *mpctx, double frame_time)
2078 current_module = "av_sync";
2080 if (!mpctx->sh_audio)
2081 return;
2083 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
2084 double v_pts = mpctx->sh_video->pts;
2085 double av_delay = a_pts - v_pts;
2086 // Try to sync vo_flip() so it will *finish* at given time
2087 av_delay += mpctx->last_vo_flip_duration;
2088 av_delay -= audio_delay; // This much pts difference is desired
2090 double change = av_delay * 0.1;
2091 double max_change = default_max_pts_correction >= 0 ?
2092 default_max_pts_correction : frame_time * 0.1;
2093 if (change < -max_change)
2094 change = -max_change;
2095 else if (change > max_change)
2096 change = max_change;
2097 mpctx->delay += change;
2098 mpctx->total_avsync_change += change;
2101 static int fill_audio_out_buffers(struct MPContext *mpctx)
2103 struct MPOpts *opts = &mpctx->opts;
2104 unsigned int t;
2105 double tt;
2106 int playsize;
2107 int playflags=0;
2108 int audio_eof=0;
2109 sh_audio_t * const sh_audio = mpctx->sh_audio;
2111 current_module="play_audio";
2113 while (1) {
2114 int sleep_time;
2115 // all the current uses of ao_data.pts seem to be in aos that handle
2116 // sync completely wrong; there should be no need to use ao_data.pts
2117 // in get_space()
2118 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2119 playsize = mpctx->audio_out->get_space();
2120 if (mpctx->sh_video || playsize >= ao_data.outburst)
2121 break;
2123 // handle audio-only case:
2124 // this is where mplayer sleeps during audio-only playback
2125 // to avoid 100% CPU use
2126 sleep_time = (ao_data.outburst - playsize) * 1000 / ao_data.bps;
2127 if (sleep_time < 10) sleep_time = 10; // limit to 100 wakeups per second
2128 usec_sleep(sleep_time * 1000);
2131 // Fill buffer if needed:
2132 current_module="decode_audio";
2133 t = GetTimer();
2134 if (decode_audio(sh_audio, playsize) < 0) // EOF or error
2135 if (mpctx->d_audio->eof) {
2136 audio_eof = 1;
2137 if (sh_audio->a_out_buffer_len == 0)
2138 return 0;
2140 t = GetTimer() - t;
2141 tt = t*0.000001f; audio_time_usage+=tt;
2142 if (playsize > sh_audio->a_out_buffer_len) {
2143 playsize = sh_audio->a_out_buffer_len;
2144 if (audio_eof)
2145 playflags |= AOPLAY_FINAL_CHUNK;
2147 if (!playsize)
2148 return 1;
2150 // play audio:
2151 current_module="play_audio";
2153 // Is this pts value actually useful for the aos that access it?
2154 // They're obviously badly broken in the way they handle av sync;
2155 // would not having access to this make them more broken?
2156 ao_data.pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2157 playsize = mpctx->audio_out->play(sh_audio->a_out_buffer, playsize, playflags);
2159 if (playsize > 0) {
2160 sh_audio->a_out_buffer_len -= playsize;
2161 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2162 sh_audio->a_out_buffer_len);
2163 mpctx->delay += opts->playback_speed*playsize/(double)ao_data.bps;
2165 else if (audio_eof && mpctx->audio_out->get_delay() < .04) {
2166 // Sanity check to avoid hanging in case current ao doesn't output
2167 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2168 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2169 sh_audio->a_out_buffer_len = 0;
2172 return 1;
2175 static int sleep_until_update(struct MPContext *mpctx, float *time_frame,
2176 float *aq_sleep_time)
2178 struct MPOpts *opts = &mpctx->opts;
2179 int frame_time_remaining = 0;
2180 current_module="calc_sleep_time";
2182 *time_frame -= get_relative_time(mpctx); // reset timer
2184 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2185 float delay = mpctx->audio_out->get_delay();
2186 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2188 if (autosync) {
2190 * Adjust this raw delay value by calculating the expected
2191 * delay for this frame and generating a new value which is
2192 * weighted between the two. The higher autosync is, the
2193 * closer to the delay value gets to that which "-nosound"
2194 * would have used, and the longer it will take for A/V
2195 * sync to settle at the right value (but it eventually will.)
2196 * This settling time is very short for values below 100.
2198 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2199 float difference = delay - predicted;
2200 delay = predicted + difference / (float)autosync;
2203 *time_frame = delay - mpctx->delay / opts->playback_speed;
2205 // delay = amount of audio buffered in soundcard/driver
2206 if (delay > 0.25) delay=0.25; else
2207 if (delay < 0.10) delay=0.10;
2208 if (*time_frame > delay*0.6) {
2209 // sleep time too big - may cause audio drops (buffer underrun)
2210 frame_time_remaining = 1;
2211 *time_frame = delay*0.5;
2213 } else {
2214 // If we're lagging more than 200 ms behind the right playback rate,
2215 // don't try to "catch up".
2216 // If benchmark is set always output frames as fast as possible
2217 // without sleeping.
2218 if (*time_frame < -0.2 || benchmark)
2219 *time_frame = 0;
2222 *aq_sleep_time += *time_frame;
2225 //============================== SLEEP: ===================================
2227 *time_frame -= mpctx->video_out->flip_queue_offset;
2228 // flag 256 means: libvo driver does its timing (dvb card)
2229 if (*time_frame > 0.001 && !(mpctx->sh_video->output_flags&256))
2230 *time_frame = timing_sleep(mpctx, *time_frame);
2231 *time_frame += mpctx->video_out->flip_queue_offset;
2232 return frame_time_remaining;
2235 int reinit_video_chain(struct MPContext *mpctx)
2237 struct MPOpts *opts = &mpctx->opts;
2238 sh_video_t * const sh_video = mpctx->sh_video;
2239 double ar=-1.0;
2240 //================== Init VIDEO (codec & libvo) ==========================
2241 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2242 current_module="preinit_libvo";
2244 //shouldn't we set dvideo->id=-2 when we fail?
2245 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2246 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2247 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Error opening/initializing the selected video_out (-vo) device.\n");
2248 goto err_out;
2250 mpctx->initialized_flags|=INITIALIZED_VO;
2253 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2254 mpctx->sh_video->stream_aspect = ar;
2255 current_module="init_video_filters";
2257 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2258 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2260 #ifdef CONFIG_MENU
2261 if(use_menu) {
2262 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2263 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2264 if(!vf_menu) {
2265 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Can't open libmenu video filter with root menu %s.\n",menu_root);
2266 use_menu = 0;
2269 if(vf_menu)
2270 sh_video->vfilter = vf_menu;
2271 #endif
2273 #ifdef CONFIG_ASS
2274 if(opts->ass_enabled) {
2275 int i;
2276 int insert = 1;
2277 if (opts->vf_settings)
2278 for (i = 0; opts->vf_settings[i].name; ++i)
2279 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2280 insert = 0;
2281 break;
2283 if (insert) {
2284 extern vf_info_t vf_info_ass;
2285 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2286 char* vf_arg[] = {"auto", "1", NULL};
2287 int retcode = 0;
2288 struct vf_instance *vf_ass = vf_open_plugin_noerr(opts, libass_vfs,
2289 sh_video->vfilter,
2290 "ass", vf_arg,
2291 &retcode);
2292 if (vf_ass)
2293 sh_video->vfilter = vf_ass;
2294 else if (retcode == -1) // vf_ass open() returns -1 if there's VO EOSD
2295 mp_msg(MSGT_CPLAYER, MSGL_V, "[ass] vf_ass not needed\n");
2296 else
2297 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2300 #endif
2302 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2304 #ifdef CONFIG_ASS
2305 if (opts->ass_enabled)
2306 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2307 #endif
2309 current_module="init_video_codec";
2311 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2312 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2313 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2315 if(!sh_video->initialized){
2316 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2317 goto err_out;
2320 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2322 if (sh_video->codec)
2323 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2325 sh_video->last_pts = MP_NOPTS_VALUE;
2326 sh_video->num_buffered_pts = 0;
2327 sh_video->next_frame_time = 0;
2329 if(auto_quality>0){
2330 // Auto quality option enabled
2331 output_quality=get_video_quality_max(sh_video);
2332 if(auto_quality>output_quality) auto_quality=output_quality;
2333 else output_quality=auto_quality;
2334 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2335 set_video_quality(sh_video,output_quality);
2338 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2340 current_module="init_vo";
2342 return 1;
2344 err_out:
2345 mpctx->sh_video = mpctx->d_video->sh = NULL;
2346 return 0;
2349 static double update_video_nocorrect_pts(struct MPContext *mpctx)
2351 struct sh_video *sh_video = mpctx->sh_video;
2352 double frame_time = 0;
2353 struct vo *video_out = mpctx->video_out;
2354 while (!video_out->frame_loaded) {
2355 current_module = "filter_video";
2356 // In nocorrect-pts mode there is no way to properly time these frames
2357 if (vo_get_buffered_frame(video_out, 0) >= 0)
2358 break;
2359 if (vf_output_queued_frame(sh_video->vfilter))
2360 continue;
2361 unsigned char *packet = NULL;
2362 frame_time = sh_video->next_frame_time;
2363 if (mpctx->update_video_immediately)
2364 frame_time = 0;
2365 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2366 &packet, force_fps);
2367 if (in_size < 0) {
2368 #ifdef CONFIG_DVDNAV
2369 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2370 if (mp_dvdnav_is_eof(mpctx->stream))
2371 return -1;
2372 if (mpctx->d_video)
2373 mpctx->d_video->eof = 0;
2374 if (mpctx->d_audio)
2375 mpctx->d_audio->eof = 0;
2376 mpctx->stream->eof = 0;
2377 } else
2378 #endif
2379 return -1;
2381 if (in_size > max_framesize)
2382 max_framesize = in_size;
2383 sh_video->timer += frame_time;
2384 if (mpctx->sh_audio)
2385 mpctx->delay -= frame_time;
2386 // video_read_frame can change fps (e.g. for ASF video)
2387 vo_fps = sh_video->fps;
2388 int framedrop_type = check_framedrop(mpctx, frame_time);
2389 current_module = "decode video";
2391 void *decoded_frame;
2392 #ifdef CONFIG_DVDNAV
2393 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2394 if (in_size >= 0 && !decoded_frame)
2395 #endif
2396 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2397 sh_video->pts);
2398 #ifdef CONFIG_DVDNAV
2399 // Save last still frame for future display
2400 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2401 #endif
2402 if (decoded_frame) {
2403 current_module = "filter video";
2404 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2405 if (!video_out->config_ok)
2406 break;
2409 return frame_time;
2412 static void determine_frame_pts(struct MPContext *mpctx)
2414 struct sh_video *sh_video = mpctx->sh_video;
2415 struct MPOpts *opts = &mpctx->opts;
2417 if (opts->user_pts_assoc_mode) {
2418 sh_video->pts_assoc_mode = opts->user_pts_assoc_mode;
2419 } else if (sh_video->pts_assoc_mode == 0) {
2420 if (sh_video->codec_reordered_pts != MP_NOPTS_VALUE)
2421 sh_video->pts_assoc_mode = 1;
2422 else
2423 sh_video->pts_assoc_mode = 2;
2424 } else {
2425 int probcount1 = sh_video->num_reordered_pts_problems;
2426 int probcount2 = sh_video->num_sorted_pts_problems;
2427 if (sh_video->pts_assoc_mode == 2) {
2428 int tmp = probcount1;
2429 probcount1 = probcount2;
2430 probcount2 = tmp;
2432 if (probcount1 >= probcount2 * 1.5 + 2) {
2433 sh_video->pts_assoc_mode = 3 - sh_video->pts_assoc_mode;
2434 mp_msg(MSGT_CPLAYER, MSGL_V, "Switching to pts association mode "
2435 "%d.\n", sh_video->pts_assoc_mode);
2438 sh_video->pts = sh_video->pts_assoc_mode == 1 ?
2439 sh_video->codec_reordered_pts : sh_video->sorted_pts;
2442 static double update_video(struct MPContext *mpctx)
2444 struct sh_video *sh_video = mpctx->sh_video;
2445 struct vo *video_out = mpctx->video_out;
2446 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2447 mpctx->osd); // hack for vf_expand
2448 if (!mpctx->opts.correct_pts)
2449 return update_video_nocorrect_pts(mpctx);
2451 double pts;
2453 bool hit_eof = false;
2454 while (!video_out->frame_loaded) {
2455 current_module = "filter_video";
2456 if (vo_get_buffered_frame(video_out, hit_eof) >= 0)
2457 break;
2458 // XXX Time used in this call is not counted in any performance
2459 // timer now, OSD time is not updated correctly for filter-added frames
2460 if (vf_output_queued_frame(sh_video->vfilter))
2461 continue;
2462 if (hit_eof)
2463 return -1;
2464 unsigned char *packet = NULL;
2465 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2466 if (pts != MP_NOPTS_VALUE)
2467 pts += mpctx->video_offset;
2468 if (in_size < 0) {
2469 // try to extract last frames in case of decoder lag
2470 in_size = 0;
2471 pts = 1e300;
2472 hit_eof = true;
2474 if (in_size > max_framesize)
2475 max_framesize = in_size;
2476 current_module = "decode video";
2477 int framedrop_type = check_framedrop(mpctx, sh_video->frametime);
2478 void *decoded_frame = decode_video(sh_video, packet, in_size,
2479 framedrop_type, pts);
2480 if (decoded_frame) {
2481 determine_frame_pts(mpctx);
2482 current_module = "filter video";
2483 if (filter_video(sh_video, decoded_frame, sh_video->pts))
2484 if (!video_out->config_ok)
2485 break; // We'd likely hang in this loop otherwise
2489 pts = video_out->next_pts;
2490 if (pts == MP_NOPTS_VALUE) {
2491 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2492 // Try to use decoder pts from before filters
2493 pts = sh_video->pts;
2494 if (pts == MP_NOPTS_VALUE)
2495 pts = sh_video->last_pts;
2497 sh_video->pts = pts;
2498 if (sh_video->last_pts == MP_NOPTS_VALUE)
2499 sh_video->last_pts = sh_video->pts;
2500 else if (sh_video->last_pts > sh_video->pts) {
2501 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2502 sh_video->pts, sh_video->last_pts);
2503 /* If the difference in pts is small treat it as jitter around the
2504 * right value (possibly caused by incorrect timestamp ordering) and
2505 * just show this frame immediately after the last one.
2506 * Treat bigger differences as timestamp resets and start counting
2507 * timing of later frames from the position of this one. */
2508 if (sh_video->last_pts - sh_video->pts > 0.5)
2509 sh_video->last_pts = sh_video->pts;
2510 else
2511 sh_video->pts = sh_video->last_pts;
2513 double frame_time = sh_video->pts - sh_video->last_pts;
2514 sh_video->last_pts = sh_video->pts;
2515 sh_video->timer += frame_time;
2516 if (mpctx->sh_audio)
2517 mpctx->delay -= frame_time;
2518 return frame_time;
2521 void pause_player(struct MPContext *mpctx)
2523 if (mpctx->paused)
2524 return;
2525 mpctx->paused = 1;
2526 mpctx->step_frames = 0;
2527 mpctx->time_frame -= get_relative_time(mpctx);
2529 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2530 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2532 if (mpctx->audio_out && mpctx->sh_audio)
2533 mpctx->audio_out->pause(); // pause audio, keep data if possible
2536 void unpause_player(struct MPContext *mpctx)
2538 if (!mpctx->paused)
2539 return;
2540 mpctx->paused = 0;
2542 if (mpctx->audio_out && mpctx->sh_audio)
2543 mpctx->audio_out->resume(); // resume audio
2544 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2545 && !mpctx->step_frames)
2546 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2547 (void)get_relative_time(mpctx); // ignore time that passed during pause
2550 void add_step_frame(struct MPContext *mpctx)
2552 mpctx->step_frames++;
2553 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2554 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2555 unpause_player(mpctx);
2558 static void pause_loop(struct MPContext *mpctx)
2560 mp_cmd_t* cmd;
2561 if (!quiet) {
2562 // Small hack to display the pause message on the OSD line.
2563 // The pause string is: "\n == PAUSE == \r" so we need to
2564 // take the first and the last char out
2565 if (term_osd && !mpctx->sh_video) {
2566 char msg[128] = _("\n ===== PAUSE =====\r");
2567 int mlen = strlen(msg);
2568 msg[mlen-1] = '\0';
2569 set_osd_msg(OSD_MSG_PAUSE, 1, 0, "%s", msg+1);
2570 update_osd_msg(mpctx);
2571 } else
2572 mp_tmsg(MSGT_CPLAYER,MSGL_STATUS,"\n ===== PAUSE =====\r");
2573 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2576 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1)) == NULL
2577 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2578 if (cmd) {
2579 cmd = mp_input_get_cmd(mpctx->input, 0, 0);
2580 run_command(mpctx, cmd);
2581 mp_cmd_free(cmd);
2582 continue;
2584 if (mpctx->sh_video && mpctx->video_out)
2585 vo_check_events(mpctx->video_out);
2586 #ifdef CONFIG_MENU
2587 if (vf_menu)
2588 vf_menu_pause_update(vf_menu);
2589 #endif
2590 usec_sleep(20000);
2591 update_osd_msg(mpctx);
2592 int hack = vo_osd_changed(0);
2593 vo_osd_changed(hack);
2594 if (hack)
2595 break;
2600 // Find the right mute status and record position for new file position
2601 static void edl_seek_reset(MPContext *mpctx)
2603 mpctx->edl_muted = 0;
2604 next_edl_record = edl_records;
2606 while (next_edl_record) {
2607 if (next_edl_record->start_sec >= mpctx->sh_video->pts)
2608 break;
2610 if (next_edl_record->action == EDL_MUTE)
2611 mpctx->edl_muted = !mpctx->edl_muted;
2612 next_edl_record = next_edl_record->next;
2614 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2615 mixer_mute(&mpctx->mixer);
2619 // Execute EDL command for the current position if one exists
2620 static void edl_update(MPContext *mpctx)
2622 if (!next_edl_record)
2623 return;
2625 if (!mpctx->sh_video) {
2626 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot use EDL without video, disabling.\n");
2627 free_edl(edl_records);
2628 next_edl_record = NULL;
2629 edl_records = NULL;
2630 return;
2633 if (mpctx->sh_video->pts >= next_edl_record->start_sec) {
2634 if (next_edl_record->action == EDL_SKIP) {
2635 mpctx->osd_function = OSD_FFW;
2636 mpctx->abs_seek_pos = 0;
2637 mpctx->rel_seek_secs = next_edl_record->length_sec;
2638 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2639 "[%f], length [%f]\n", next_edl_record->start_sec,
2640 next_edl_record->stop_sec, next_edl_record->length_sec);
2642 else if (next_edl_record->action == EDL_MUTE) {
2643 mpctx->edl_muted = !mpctx->edl_muted;
2644 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2645 mixer_mute(&mpctx->mixer);
2646 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2647 next_edl_record->start_sec );
2649 next_edl_record = next_edl_record->next;
2653 static void reinit_decoders(struct MPContext *mpctx)
2655 reinit_video_chain(mpctx);
2656 reinit_audio_chain(mpctx);
2657 mp_property_do("sub", M_PROPERTY_SET, &mpctx->global_sub_pos, mpctx);
2660 static void seek_reset(struct MPContext *mpctx)
2662 if (mpctx->sh_video) {
2663 current_module = "seek_video_reset";
2664 resync_video_stream(mpctx->sh_video);
2665 mpctx->sh_video->timer = 0;
2666 vo_seek_reset(mpctx->video_out);
2667 mpctx->sh_video->timer = 0;
2668 mpctx->sh_video->num_buffered_pts = 0;
2669 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2670 mpctx->delay = 0;
2671 mpctx->time_frame = 0;
2672 mpctx->update_video_immediately = true;
2673 // Not all demuxers set d_video->pts during seek, so this value
2674 // (which is used by at least vobsub and edl code below) may
2675 // be completely wrong (probably 0).
2676 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
2677 update_subtitles(mpctx, &mpctx->opts, mpctx->sh_video,
2678 mpctx->sh_video->pts, mpctx->video_offset,
2679 mpctx->d_sub, 1);
2680 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
2683 if (mpctx->sh_audio) {
2684 current_module = "seek_audio_reset";
2685 resync_audio_stream(mpctx->sh_audio);
2686 mpctx->audio_out->reset(); // stop audio, throwing away buffered data
2687 mpctx->sh_audio->a_buffer_len = 0;
2688 mpctx->sh_audio->a_out_buffer_len = 0;
2689 if (!mpctx->sh_video)
2690 update_subtitles(mpctx, &mpctx->opts, NULL, mpctx->sh_audio->pts,
2691 mpctx->video_offset, mpctx->d_sub, 1);
2694 if (vo_vobsub && mpctx->sh_video) {
2695 current_module = "seek_vobsub_reset";
2696 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
2699 edl_seek_reset(mpctx);
2701 mpctx->total_avsync_change = 0;
2702 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
2703 drop_frame_cnt = 0;
2705 current_module = NULL;
2708 static bool timeline_set_part(struct MPContext *mpctx, int i)
2710 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
2711 struct timeline_part *n = mpctx->timeline + i;
2712 mpctx->timeline_part = i;
2713 mpctx->video_offset = n->start - n->source_start;
2714 if (n->source == p->source)
2715 return false;
2716 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo && mpctx->opts.video_id != -2 ? 0 : INITIALIZED_VO) | INITIALIZED_AO | INITIALIZED_ACODEC);
2717 mpctx->demuxer = n->source->demuxer;
2718 mpctx->d_video = mpctx->demuxer->video;
2719 mpctx->d_audio = mpctx->demuxer->audio;
2720 mpctx->d_sub = mpctx->demuxer->sub;
2721 mpctx->sh_video = mpctx->d_video->sh;
2722 mpctx->sh_audio = mpctx->d_audio->sh;
2723 return true;
2726 // Given pts, switch playback to the corresponding part.
2727 // Return offset within that part.
2728 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
2729 bool *need_reset)
2731 if (pts < 0)
2732 pts = 0;
2733 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
2734 struct timeline_part *p = mpctx->timeline + i;
2735 if (pts < (p+1)->start) {
2736 *need_reset = timeline_set_part(mpctx, i);
2737 return pts - p->start + p->source_start;
2740 return -1;
2744 // style & SEEK_ABSOLUTE == 0 means seek relative to current position, == 1 means absolute
2745 // style & SEEK_FACTOR == 0 means amount in seconds, == 2 means fraction of file length
2746 // return -1 if seek failed (non-seekable stream?), 0 otherwise
2747 static int seek(MPContext *mpctx, double amount, int style)
2749 current_module = "seek";
2750 if (mpctx->stop_play == AT_END_OF_FILE)
2751 mpctx->stop_play = KEEP_PLAYING;
2752 if (mpctx->timeline && style & SEEK_FACTOR) {
2753 amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
2754 style &= ~SEEK_FACTOR;
2756 if ((mpctx->demuxer->accurate_seek || mpctx->timeline) && mpctx->sh_video
2757 && !(style & (SEEK_ABSOLUTE | SEEK_FACTOR))) {
2758 style |= SEEK_ABSOLUTE;
2759 if (amount > 0)
2760 style |= SEEK_FORWARD;
2761 else
2762 style |= SEEK_BACKWARD;
2763 amount += mpctx->sh_video->pts;
2766 /* At least the liba52 decoder wants to read from the input stream
2767 * during initialization, so reinit must be done after the demux_seek()
2768 * call that clears possible stream EOF. */
2769 bool need_reset = false;
2770 if (mpctx->timeline) {
2771 amount = timeline_set_from_time(mpctx, amount, &need_reset);
2772 if (amount == -1) {
2773 mpctx->stop_play = AT_END_OF_FILE;
2774 // Clear audio from current position
2775 if (mpctx->sh_audio) {
2776 mpctx->audio_out->reset();
2777 mpctx->sh_audio->a_buffer_len = 0;
2778 mpctx->sh_audio->a_out_buffer_len = 0;
2780 return -1;
2783 int seekresult = demux_seek(mpctx->demuxer, amount, audio_delay, style);
2784 if (need_reset)
2785 reinit_decoders(mpctx);
2786 if (seekresult == 0)
2787 return -1;
2789 seek_reset(mpctx);
2790 return 0;
2793 // -2 is no chapters, -1 is before first chapter
2794 int get_current_chapter(struct MPContext *mpctx)
2796 if (!mpctx->chapters || !mpctx->sh_video)
2797 return demuxer_get_current_chapter(mpctx->demuxer);
2799 int i;
2800 double current_pts = mpctx->sh_video->pts;
2801 for (i = 1; i < mpctx->num_chapters; i++)
2802 if (current_pts < mpctx->chapters[i].start)
2803 break;
2804 return i - 1;
2807 // currently returns a string allocated with malloc, not talloc
2808 char *chapter_display_name(struct MPContext *mpctx, int chapter)
2810 if (!mpctx->chapters || !mpctx->sh_video)
2811 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
2812 return strdup(mpctx->chapters[chapter].name);
2815 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
2816 char **chapter_name)
2818 if (!mpctx->chapters || !mpctx->sh_video) {
2819 int res = demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
2820 chapter_name);
2821 if (res >= 0 && *seek_pts == -1)
2822 seek_reset(mpctx);
2823 return res;
2826 if (chapter >= mpctx->num_chapters)
2827 return -1;
2828 if (chapter < 0)
2829 chapter = 0;
2830 *seek_pts = mpctx->chapters[chapter].start;
2831 if (chapter_name)
2832 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
2833 return chapter;
2836 static int find_ordered_chapter_sources(struct MPContext *mpctx,
2837 struct content_source *sources,
2838 int num_sources,
2839 unsigned char uid_map[][16])
2841 int num_filenames = 0;
2842 char **filenames = NULL;
2843 if (num_sources > 1) {
2844 mp_msg(MSGT_CPLAYER, MSGL_INFO, "This file references data from "
2845 "other sources.\n");
2846 if (mpctx->stream->type != STREAMTYPE_FILE) {
2847 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Playback source is not a "
2848 "normal disk file. Will not search for related files.\n");
2849 } else {
2850 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Will scan other files in the "
2851 "same directory to find referenced sources.\n");
2852 filenames = find_files(mpctx->demuxer->filename, ".mkv",
2853 &num_filenames);
2857 int num_left = num_sources - 1;
2858 for (int i = 0; i < num_filenames && num_left > 0; i++) {
2859 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Checking file %s\n",
2860 filename_recode(filenames[i]));
2861 int format;
2862 struct stream *s = open_stream(filenames[i], &mpctx->opts, &format);
2863 if (!s)
2864 continue;
2865 struct demuxer *d = demux_open(&mpctx->opts, s, DEMUXER_TYPE_MATROSKA,
2866 mpctx->opts.audio_id,
2867 mpctx->opts.video_id,
2868 mpctx->opts.sub_id, filenames[i]);
2869 if (!d) {
2870 free_stream(s);
2871 continue;
2873 if (d->file_format == DEMUXER_TYPE_MATROSKA) {
2874 for (int i = 1; i < num_sources; i++) {
2875 if (sources[i].demuxer)
2876 continue;
2877 if (!memcmp(uid_map[i], d->matroska_data.segment_uid, 16)) {
2878 mp_msg(MSGT_CPLAYER, MSGL_INFO,"Match for source %d: %s\n",
2879 i, filename_recode(d->filename));
2880 sources[i].stream = s;
2881 sources[i].demuxer = d;
2882 num_left--;
2883 goto match;
2887 free_demuxer(d);
2888 free_stream(s);
2889 continue;
2890 match:
2893 talloc_free(filenames);
2894 if (num_left) {
2895 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Failed to find ordered chapter part!\n"
2896 "There will be parts MISSING from the video!\n");
2897 for (int i = 1, j = 1; i < num_sources; i++)
2898 if (sources[i].demuxer) {
2899 sources[j] = sources[i];
2900 memcpy(uid_map[j], uid_map[i], 16);
2901 j++;
2904 return num_sources - num_left;
2907 static void build_ordered_chapter_timeline(struct MPContext *mpctx)
2909 if (!mpctx->opts.ordered_chapters) {
2910 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, but "
2911 "you have disabled support for them. Ignoring.\n");
2912 return;
2915 mp_msg(MSGT_CPLAYER, MSGL_INFO, "File uses ordered chapters, will build "
2916 "edit timeline.\n");
2918 struct demuxer *demuxer = mpctx->demuxer;
2919 struct matroska_data *m = &demuxer->matroska_data;
2921 // +1 because sources/uid_map[0] is original file even if all chapters
2922 // actually use other sources and need separate entries
2923 struct content_source *sources = talloc_array_ptrtype(NULL, sources,
2924 m->num_ordered_chapters+1);
2925 sources[0].stream = mpctx->stream;
2926 sources[0].demuxer = mpctx->demuxer;
2927 unsigned char uid_map[m->num_ordered_chapters+1][16];
2928 int num_sources = 1;
2929 memcpy(uid_map[0], m->segment_uid, 16);
2931 for (int i = 0; i < m->num_ordered_chapters; i++) {
2932 struct matroska_chapter *c = m->ordered_chapters + i;
2933 if (!c->has_segment_uid)
2934 memcpy(c->segment_uid, m->segment_uid, 16);
2936 for (int j = 0; j < num_sources; j++)
2937 if (!memcmp(c->segment_uid, uid_map[j], 16))
2938 goto found1;
2939 memcpy(uid_map[num_sources], c->segment_uid, 16);
2940 sources[num_sources] = (struct content_source){};
2941 num_sources++;
2942 found1:
2946 num_sources = find_ordered_chapter_sources(mpctx, sources, num_sources,
2947 uid_map);
2950 // +1 for terminating chapter with start time marking end of last real one
2951 struct timeline_part *timeline = talloc_array_ptrtype(NULL, timeline,
2952 m->num_ordered_chapters + 1);
2953 struct chapter *chapters = talloc_array_ptrtype(NULL, chapters,
2954 m->num_ordered_chapters);
2955 uint64_t starttime = 0;
2956 uint64_t missing_time = 0;
2957 int part_count = 0;
2958 int num_chapters = 0;
2959 uint64_t prev_part_offset;
2960 for (int i = 0; i < m->num_ordered_chapters; i++) {
2961 struct matroska_chapter *c = m->ordered_chapters + i;
2963 int j;
2964 for (j = 0; j < num_sources; j++) {
2965 if (!memcmp(c->segment_uid, uid_map[j], 16))
2966 goto found2;
2968 missing_time += c->end - c->start;
2969 continue;
2970 found2:;
2971 chapters[num_chapters].start = starttime / 1000.;
2972 chapters[num_chapters].name = talloc_strdup(chapters, c->name);
2973 /* Only add a separate part if the time or file actually changes.
2974 * Matroska files have chapter divisions that are redundant from
2975 * timeline point of view because the same chapter structure is used
2976 * both to specify the timeline and for normal chapter information.
2977 * Removing a missing inserted external chapter can also cause this. */
2978 if (part_count == 0 || c->start != starttime + prev_part_offset
2979 || sources + j != timeline[part_count - 1].source) {
2980 timeline[part_count].source = sources + j;
2981 timeline[part_count].start = chapters[num_chapters].start;
2982 timeline[part_count].source_start = c->start / 1000.;
2983 prev_part_offset = c->start - starttime;
2984 part_count++;
2986 starttime += c->end - c->start;
2987 num_chapters++;
2989 timeline[part_count].start = starttime / 1000.;
2991 if (!part_count) {
2992 // None of the parts come from the file itself???
2993 talloc_free(sources);
2994 talloc_free(timeline);
2995 talloc_free(chapters);
2996 return;
2999 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
3000 "sources. Total length %.3f seconds.\n", part_count, num_sources,
3001 timeline[part_count].start);
3002 if (missing_time)
3003 mp_msg(MSGT_CPLAYER, MSGL_ERR, "There are %.3f seconds missing "
3004 "from the timeline!\n", missing_time / 1000.);
3005 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
3006 for (int i = 0; i < num_sources; i++)
3007 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
3008 filename_recode(sources[i].demuxer->filename));
3009 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
3010 "source_start, source):\n");
3011 for (int i = 0; i < part_count; i++) {
3012 struct timeline_part *p = timeline + i;
3013 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
3014 p->source_start, p->source - sources);
3016 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n", timeline[part_count].start);
3017 mpctx->sources = sources;
3018 mpctx->num_sources = num_sources;
3019 mpctx->timeline = timeline;
3020 mpctx->num_timeline_parts = part_count;
3021 mpctx->num_chapters = num_chapters;
3022 mpctx->chapters = chapters;
3024 mpctx->timeline_part = 0;
3025 mpctx->demuxer = timeline[0].source->demuxer;
3029 static int read_keys(void *ctx, int fd)
3031 getch2(ctx);
3032 return mplayer_get_key(ctx, 0);
3036 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
3037 * file for some tools to link against. */
3038 #ifndef DISABLE_MAIN
3039 int main(int argc,char* argv[]){
3042 char * mem_ptr;
3044 // movie info:
3046 /* Flag indicating whether MPlayer should exit without playing anything. */
3047 int opt_exit = 0;
3049 //float a_frame=0; // Audio
3051 int i;
3053 struct MPContext *mpctx = &(struct MPContext){
3054 .osd_function = OSD_PLAY,
3055 .begin_skip = MP_NOPTS_VALUE,
3056 .play_tree_step = 1,
3057 .global_sub_pos = -1,
3058 .set_of_sub_pos = -1,
3059 .file_format = DEMUXER_TYPE_UNKNOWN,
3060 .last_dvb_step = 1,
3063 InitTimer();
3064 srand(GetTimerMS());
3066 mp_msg_init();
3067 set_av_log_callback();
3069 #ifdef CONFIG_X11
3070 mpctx->x11_state = vo_x11_init_state();
3071 #endif
3072 struct MPOpts *opts = &mpctx->opts;
3073 set_default_mplayer_options(opts);
3074 // Create the config context and register the options
3075 mpctx->mconfig = m_config_new(opts, cfg_include);
3076 m_config_register_options(mpctx->mconfig,mplayer_opts);
3077 mp_input_register_options(mpctx->mconfig);
3079 // Preparse the command line
3080 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
3082 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
3083 set_path_env();
3084 #endif
3086 #ifdef CONFIG_TV
3087 stream_tv_defaults.immediate = 1;
3088 #endif
3090 parse_cfgfiles(mpctx, mpctx->mconfig);
3092 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
3093 if(mpctx->playtree == NULL)
3094 opt_exit = 1;
3095 else {
3096 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
3097 if(mpctx->playtree) {
3098 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3099 if(mpctx->playtree_iter) {
3100 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3101 play_tree_iter_free(mpctx->playtree_iter);
3102 mpctx->playtree_iter = NULL;
3104 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3108 mpctx->key_fifo = mp_fifo_create(opts);
3110 print_version("MPlayer");
3112 #if defined(__MINGW32__) || defined(__CYGWIN__)
3114 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
3115 BOOL WINAPI (*setDEP)(DWORD) = NULL;
3116 if (kernel32)
3117 setDEP = GetProcAddress(kernel32, "SetProcessDEPPolicy");
3118 if (setDEP) setDEP(3);
3120 // stop Windows from showing all kinds of annoying error dialogs
3121 SetErrorMode(0x8003);
3122 // request 1ms timer resolution
3123 timeBeginPeriod(1);
3124 #endif
3126 #ifdef CONFIG_PRIORITY
3127 set_priority();
3128 #endif
3130 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
3131 list_video_out();
3132 opt_exit = 1;
3135 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
3136 list_audio_out();
3137 opt_exit = 1;
3140 /* Check codecs.conf. */
3141 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3142 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3143 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3144 if(!parse_codec_cfg(NULL)){
3145 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3147 mp_tmsg(MSGT_CPLAYER,MSGL_V,"Using built-in default codecs.conf.\n");
3150 free( mem_ptr ); // release the buffer created by get_path()
3153 #if 0
3154 if(video_codec_list){
3155 int i;
3156 video_codec=video_codec_list[0];
3157 for(i=0;video_codec_list[i];i++)
3158 mp_msg(MSGT_FIXME,MSGL_FIXME,"vc#%d: '%s'\n",i,video_codec_list[i]);
3160 #endif
3161 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3162 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available audio codecs:\n");
3163 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3164 list_codecs(1);
3165 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3166 opt_exit = 1;
3168 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3169 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video codecs:\n");
3170 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3171 list_codecs(0);
3172 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3173 opt_exit = 1;
3175 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3176 vfm_help();
3177 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3178 opt_exit = 1;
3180 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3181 afm_help();
3182 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3183 opt_exit = 1;
3185 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3186 af_help();
3187 printf("\n");
3188 opt_exit = 1;
3190 #ifdef CONFIG_X11
3191 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3192 fstype_help();
3193 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3194 opt_exit = 1;
3196 #endif
3197 if((demuxer_name && strcmp(demuxer_name,"help")==0) ||
3198 (audio_demuxer_name && strcmp(audio_demuxer_name,"help")==0) ||
3199 (sub_demuxer_name && strcmp(sub_demuxer_name,"help")==0)){
3200 demuxer_help();
3201 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3202 opt_exit = 1;
3204 if(list_properties) {
3205 property_print_help();
3206 opt_exit = 1;
3209 if(opt_exit)
3210 exit_player(mpctx, EXIT_NONE);
3212 if(!mpctx->filename && !player_idle_mode){
3213 // no file/vcd/dvd -> show HELP:
3214 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s", mp_gtext(help_text));
3215 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3218 /* Display what configure line was used */
3219 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3221 // Many users forget to include command line in bugreports...
3222 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3223 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "CommandLine:");
3224 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3225 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3228 //------ load global data first ------
3230 mpctx->osd = osd_create();
3232 // check font
3233 #ifdef CONFIG_FREETYPE
3234 init_freetype();
3235 #endif
3236 #ifdef CONFIG_FONTCONFIG
3237 if(font_fontconfig <= 0)
3239 #endif
3240 #ifdef CONFIG_BITMAP_FONT
3241 if(font_name){
3242 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3243 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",
3244 filename_recode(font_name));
3245 } else {
3246 // try default:
3247 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3248 free(mem_ptr); // release the buffer created by get_path()
3249 if(!vo_font)
3250 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3252 if (sub_font_name)
3253 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3254 else
3255 mpctx->osd->sub_font = vo_font;
3256 #endif
3257 #ifdef CONFIG_FONTCONFIG
3259 #endif
3261 #ifdef CONFIG_ASS
3262 ass_library = ass_init();
3263 #endif
3265 #ifdef HAVE_RTC
3266 if(!nortc)
3268 // seteuid(0); /* Can't hurt to try to get root here */
3269 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3270 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Failed to open %s: %s (it should be readable by the user.)\n",
3271 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3272 else {
3273 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3275 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3276 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp, strerror(errno));
3277 mp_tmsg(MSGT_CPLAYER, MSGL_HINT, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp);
3278 close (rtc_fd);
3279 rtc_fd = -1;
3280 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3281 /* variable only by the root */
3282 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno));
3283 close (rtc_fd);
3284 rtc_fd = -1;
3285 } else
3286 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Using Linux hardware RTC timing (%ldHz).\n", irqp);
3289 if(rtc_fd<0)
3290 #endif /* HAVE_RTC */
3291 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3292 softsleep?"software":timer_name);
3294 #ifdef HAVE_TERMCAP
3295 load_termcap(NULL); // load key-codes
3296 #endif
3298 // ========== Init keyboard FIFO (connection to libvo) ============
3300 // Init input system
3301 current_module = "init_input";
3302 mpctx->input = mp_input_init(&opts->input);
3303 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3304 if(slave_mode)
3305 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
3306 else if(!noconsolecontrols)
3307 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
3308 // Set the libstream interrupt callback
3309 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
3311 #ifdef CONFIG_MENU
3312 if(use_menu) {
3313 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3314 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3315 else {
3316 menu_cfg = get_path("menu.conf");
3317 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
3318 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
3319 else {
3320 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
3321 MPLAYER_CONFDIR "/menu.conf"))
3322 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", MPLAYER_CONFDIR"/menu.conf");
3323 else {
3324 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Menu init failed.\n");
3325 use_menu = 0;
3330 #endif
3332 current_module = NULL;
3334 /// Catch signals
3335 #ifndef __MINGW32__
3336 signal(SIGCHLD,child_sighandler);
3337 #endif
3339 #ifdef CONFIG_CRASH_DEBUG
3340 prog_path = argv[0];
3341 #endif
3342 //========= Catch terminate signals: ================
3343 // terminate requests:
3344 signal(SIGTERM,exit_sighandler); // kill
3345 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
3347 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
3349 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
3350 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
3351 #ifdef CONFIG_SIGHANDLER
3352 // fatal errors:
3353 signal(SIGBUS,exit_sighandler); // bus error
3354 signal(SIGSEGV,exit_sighandler); // segfault
3355 signal(SIGILL,exit_sighandler); // illegal instruction
3356 signal(SIGFPE,exit_sighandler); // floating point exc.
3357 signal(SIGABRT,exit_sighandler); // abort()
3358 #ifdef CONFIG_CRASH_DEBUG
3359 if (crash_debug)
3360 signal(SIGTRAP,exit_sighandler);
3361 #endif
3362 #endif
3364 // ******************* Now, let's see the per-file stuff ********************
3366 play_next_file:
3368 // init global sub numbers
3369 mpctx->global_sub_size = 0;
3370 { int i; for (i = 0; i < SUB_SOURCES; i++) mpctx->global_sub_indices[i] = -1; }
3372 if (mpctx->filename) {
3373 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
3374 load_per_extension_config (mpctx->mconfig, mpctx->filename);
3375 load_per_file_config (mpctx->mconfig, mpctx->filename);
3378 if (opts->video_driver_list)
3379 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
3380 if (opts->audio_driver_list)
3381 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
3383 // We must enable getch2 here to be able to interrupt network connection
3384 // or cache filling
3385 if(!noconsolecontrols && !slave_mode){
3386 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
3387 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"WARNING: getch2_init called twice!\n");
3388 else
3389 getch2_enable(); // prepare stdin for hotkeys...
3390 mpctx->initialized_flags|=INITIALIZED_GETCH2;
3391 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
3394 // =================== GUI idle loop (STOP state) ===========================
3395 while (player_idle_mode && !mpctx->filename) {
3396 play_tree_t * entry = NULL;
3397 mp_cmd_t * cmd;
3398 if (mpctx->video_out && mpctx->video_out->config_ok)
3399 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
3400 while (!(cmd = mp_input_get_cmd(mpctx->input, 0, 0))) { // wait for command
3401 if (mpctx->video_out)
3402 vo_check_events(mpctx->video_out);
3403 usec_sleep(20000);
3405 switch (cmd->id) {
3406 case MP_CMD_LOADFILE:
3407 // prepare a tree entry with the new filename
3408 entry = play_tree_new();
3409 play_tree_add_file(entry, cmd->args[0].v.s);
3410 // The entry is added to the main playtree after the switch().
3411 break;
3412 case MP_CMD_LOADLIST:
3413 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
3414 break;
3415 case MP_CMD_QUIT:
3416 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
3417 break;
3418 case MP_CMD_GET_PROPERTY:
3419 case MP_CMD_SET_PROPERTY:
3420 case MP_CMD_STEP_PROPERTY:
3421 run_command(mpctx, cmd);
3422 break;
3425 mp_cmd_free(cmd);
3427 if (entry) { // user entered a command that gave a valid entry
3428 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
3429 play_tree_free_list(mpctx->playtree->child, 1);
3430 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
3432 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
3434 play_tree_set_child(mpctx->playtree, entry);
3436 /* Make iterator start at the top the of tree. */
3437 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
3438 if (!mpctx->playtree_iter) continue;
3440 // find the first real item in the tree
3441 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3442 // no items!
3443 play_tree_iter_free(mpctx->playtree_iter);
3444 mpctx->playtree_iter = NULL;
3445 continue; // wait for next command
3447 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
3450 //---------------------------------------------------------------------------
3452 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
3453 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL);
3455 if (mpctx->filename) {
3456 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nPlaying %s.\n",
3457 filename_recode(mpctx->filename));
3458 if(use_filename_title && vo_wintitle == NULL)
3459 vo_wintitle = strdup(mp_basename2(mpctx->filename));
3462 if (edl_filename) {
3463 if (edl_records) free_edl(edl_records);
3464 next_edl_record = edl_records = edl_parse_file();
3466 if (edl_output_filename) {
3467 if (edl_fd) fclose(edl_fd);
3468 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
3470 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Can't open EDL file [%s] for writing.\n",
3471 filename_recode(edl_output_filename));
3475 //==================== Open VOB-Sub ============================
3477 current_module="vobsub";
3478 if (vobsub_name){
3479 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
3480 if(vo_vobsub==NULL)
3481 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
3482 filename_recode(vobsub_name));
3483 } else if (sub_auto && mpctx->filename){
3484 /* try to autodetect vobsub from movie filename ::atmos */
3485 char *buf = strdup(mpctx->filename), *psub;
3486 char *pdot = strrchr(buf, '.');
3487 char *pslash = strrchr(buf, '/');
3488 #if defined(__MINGW32__) || defined(__CYGWIN__)
3489 if (!pslash) pslash = strrchr(buf, '\\');
3490 #endif
3491 if (pdot && (!pslash || pdot > pslash))
3492 *pdot = '\0';
3493 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
3494 /* try from ~/.mplayer/sub */
3495 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
3496 char *bname;
3497 int l;
3498 bname = strrchr(buf,'/');
3499 #if defined(__MINGW32__) || defined(__CYGWIN__)
3500 if(!bname) bname = strrchr(buf,'\\');
3501 #endif
3502 if(bname) bname++;
3503 else bname = buf;
3504 l = strlen(psub) + strlen(bname) + 1;
3505 psub = realloc(psub,l);
3506 strcat(psub,bname);
3507 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
3508 free(psub);
3510 free(buf);
3512 if(vo_vobsub){
3513 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
3514 vobsub_set_from_lang(vo_vobsub, dvdsub_lang);
3515 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
3517 // setup global sub numbering
3518 mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] = mpctx->global_sub_size; // the global # of the first vobsub.
3519 mpctx->global_sub_size += vobsub_get_indexes_count(vo_vobsub);
3522 //============ Open & Sync STREAM --- fork cache2 ====================
3524 mpctx->stream=NULL;
3525 mpctx->demuxer=NULL;
3526 if (mpctx->d_audio) {
3527 //free_demuxer_stream(mpctx->d_audio);
3528 mpctx->d_audio=NULL;
3530 if (mpctx->d_video) {
3531 //free_demuxer_stream(d_video);
3532 mpctx->d_video=NULL;
3534 mpctx->sh_audio=NULL;
3535 mpctx->sh_video=NULL;
3537 current_module="open_stream";
3538 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
3539 if(!mpctx->stream) { // error...
3540 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
3541 goto goto_next_file;
3543 mpctx->initialized_flags|=INITIALIZED_STREAM;
3545 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
3546 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
3547 "playlist support will not be used automatically.\n"
3548 "MPlayer's playlist code is unsafe and should only be used with "
3549 "trusted sources.\nPlayback will probably fail.\n\n");
3550 #if 0
3551 play_tree_t* entry;
3552 // Handle playlist
3553 current_module="handle_playlist";
3554 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
3555 filename_recode(mpctx->filename));
3556 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
3557 mpctx->eof=playtree_add_playlist(mpctx, entry);
3558 goto goto_next_file;
3559 #endif
3561 mpctx->stream->start_pos+=seek_to_byte;
3563 if(stream_dump_type==5){
3564 unsigned char buf[4096];
3565 int len;
3566 FILE *f;
3567 current_module="dumpstream";
3568 stream_reset(mpctx->stream);
3569 stream_seek(mpctx->stream,mpctx->stream->start_pos);
3570 f=fopen(stream_dump_name,"wb");
3571 if(!f){
3572 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3573 exit_player(mpctx, EXIT_ERROR);
3575 if (opts->chapterrange[0] > 1) {
3576 int chapter = opts->chapterrange[0] - 1;
3577 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
3579 while(!mpctx->stream->eof && !async_quit_request){
3580 len=stream_read(mpctx->stream,buf,4096);
3581 if(len>0) {
3582 if(fwrite(buf,len,1,f) != 1) {
3583 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3584 exit_player(mpctx, EXIT_ERROR);
3587 if (opts->chapterrange[1] > 0) {
3588 int chapter = -1;
3589 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
3590 &chapter) == STREAM_OK
3591 && chapter + 1 > opts->chapterrange[1])
3592 break;
3595 if(fclose(f)) {
3596 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n",stream_dump_name);
3597 exit_player(mpctx, EXIT_ERROR);
3599 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3600 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3603 #ifdef CONFIG_DVDREAD
3604 if(mpctx->stream->type==STREAMTYPE_DVD){
3605 current_module="dvd lang->id";
3606 if(opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,audio_lang);
3607 if(dvdsub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,dvdsub_lang);
3608 // setup global sub numbering
3609 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3610 mpctx->global_sub_size += dvd_number_of_subs(mpctx->stream);
3611 current_module=NULL;
3613 #endif
3615 #ifdef CONFIG_DVDNAV
3616 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
3617 current_module="dvdnav lang->id";
3618 if(opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,audio_lang);
3619 dvdsub_lang_id = -3;
3620 if(dvdsub_lang && opts->sub_id==-1)
3621 dvdsub_lang_id = opts->sub_id = mp_dvdnav_sid_from_lang(mpctx->stream,dvdsub_lang);
3622 // setup global sub numbering
3623 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3624 mpctx->global_sub_size += mp_dvdnav_number_of_subs(mpctx->stream);
3625 current_module=NULL;
3627 #endif
3629 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
3630 goto_enable_cache:
3631 if(stream_cache_size>0){
3632 int res;
3633 current_module="enable_cache";
3634 res = stream_enable_cache(mpctx->stream,stream_cache_size*1024,
3635 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
3636 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0));
3637 if(res == 0)
3638 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
3641 //============ Open DEMUXERS --- DETECT file type =======================
3642 current_module="demux_open";
3644 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
3646 // HACK to get MOV Reference Files working
3648 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
3650 unsigned char* playlist_entry;
3651 play_tree_t *list = NULL, *entry = NULL;
3653 current_module="handle_demux_playlist";
3654 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
3656 char *temp, *bname;
3658 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
3659 filename_recode(playlist_entry));
3661 bname=mp_basename(playlist_entry);
3662 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
3663 continue;
3665 if (!strcmp(playlist_entry, mpctx->filename)) // ignoring self-reference
3666 continue;
3668 entry = play_tree_new();
3670 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
3672 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
3673 if (temp)
3675 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
3676 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
3677 strcat(temp, playlist_entry);
3678 if (!strcmp(temp, mpctx->filename)) {
3679 free(temp);
3680 continue;
3682 play_tree_add_file(entry,temp);
3683 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
3684 free(temp);
3687 else
3688 play_tree_add_file(entry,playlist_entry);
3690 if(!list)
3691 list = entry;
3692 else
3693 play_tree_append_entry(list,entry);
3695 free_demuxer(mpctx->demuxer);
3696 mpctx->demuxer = NULL;
3698 if (list)
3700 entry = play_tree_new();
3701 play_tree_set_child(entry,list);
3702 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
3703 goto goto_next_file;
3707 if(!mpctx->demuxer)
3708 goto goto_next_file;
3710 if (mpctx->demuxer->matroska_data.ordered_chapters)
3711 build_ordered_chapter_timeline(mpctx);
3713 if (!mpctx->sources) {
3714 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
3715 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
3716 .demuxer = mpctx->demuxer};
3717 mpctx->num_sources = 1;
3720 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
3722 if (mpctx->stream->type != STREAMTYPE_DVD && mpctx->stream->type != STREAMTYPE_DVDNAV) {
3723 int i;
3724 int maxid = -1;
3725 // setup global sub numbering
3726 mpctx->global_sub_indices[SUB_SOURCE_DEMUX] = mpctx->global_sub_size; // the global # of the first demux-specific sub.
3727 for (i = 0; i < MAX_S_STREAMS; i++)
3728 if (mpctx->demuxer->s_streams[i])
3729 maxid = FFMAX(maxid, mpctx->demuxer->s_streams[i]->sid);
3730 mpctx->global_sub_size += maxid + 1;
3732 // Make opts->sub_id always selectable if set.
3733 if (mpctx->global_sub_size <= mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id)
3734 mpctx->global_sub_size = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + opts->sub_id + 1;
3736 #ifdef CONFIG_ASS
3737 if (opts->ass_enabled && ass_library) {
3738 for (int j = 0; j < mpctx->num_sources; j++) {
3739 struct demuxer *d = mpctx->sources[j].demuxer;
3740 for (int i = 0; i < d->num_attachments; i++) {
3741 struct demux_attachment *att = d->attachments + i;
3742 if (use_embedded_fonts
3743 && att->name && att->type && att->data && att->data_size
3744 && (strcmp(att->type, "application/x-truetype-font") == 0
3745 || strcmp(att->type, "application/x-font") == 0))
3746 ass_add_font(ass_library, att->name, att->data, att->data_size);
3750 #endif
3752 current_module="demux_open2";
3754 //file_format=demuxer->file_format;
3756 mpctx->d_audio=mpctx->demuxer->audio;
3757 mpctx->d_video=mpctx->demuxer->video;
3758 mpctx->d_sub=mpctx->demuxer->sub;
3760 if (ts_prog) {
3761 int tmp = ts_prog;
3762 mp_property_do("switch_program", M_PROPERTY_SET, &tmp, mpctx);
3764 // select audio stream
3765 select_audio(mpctx->demuxer, opts->audio_id, audio_lang);
3767 // DUMP STREAMS:
3768 if((stream_dump_type)&&(stream_dump_type<4)){
3769 FILE *f;
3770 demux_stream_t *ds=NULL;
3771 current_module="dump";
3772 // select stream to dump
3773 switch(stream_dump_type){
3774 case 1: ds=mpctx->d_audio;break;
3775 case 2: ds=mpctx->d_video;break;
3776 case 3: ds=mpctx->d_sub;break;
3778 if(!ds){
3779 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"dump: FATAL: Selected stream missing!\n");
3780 exit_player(mpctx, EXIT_ERROR);
3782 // disable other streams:
3783 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
3784 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
3785 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
3786 // let's dump it!
3787 f=fopen(stream_dump_name,"wb");
3788 if(!f){
3789 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
3790 exit_player(mpctx, EXIT_ERROR);
3792 while(!ds->eof){
3793 unsigned char* start;
3794 int in_size=ds_get_packet(ds,&start);
3795 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
3796 && stream_dump_type==2) fwrite(&in_size,1,4,f);
3797 if(in_size>0) fwrite(start,in_size,1,f);
3798 if (opts->chapterrange[1] > 0) {
3799 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer);
3800 if(cur_chapter!=-1 && cur_chapter+1 > opts->chapterrange[1])
3801 break;
3804 fclose(f);
3805 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Core dumped ;)\n");
3806 exit_player_with_rc(mpctx, EXIT_EOF, 0);
3809 mpctx->sh_audio=mpctx->d_audio->sh;
3810 mpctx->sh_video=mpctx->d_video->sh;
3812 if(mpctx->sh_video){
3814 current_module="video_read_properties";
3815 if(!video_read_properties(mpctx->sh_video)) {
3816 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Video: Cannot read properties.\n");
3817 mpctx->sh_video=mpctx->d_video->sh=NULL;
3818 } else {
3819 mp_tmsg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
3820 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
3821 mpctx->sh_video->fps,mpctx->sh_video->frametime
3824 /* need to set fps here for output encoders to pick it up in their init */
3825 if(force_fps){
3826 mpctx->sh_video->fps=force_fps;
3827 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
3829 vo_fps = mpctx->sh_video->fps;
3831 if(!mpctx->sh_video->fps && !force_fps){
3832 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"FPS not specified in the header or invalid, use the -fps option.\n");
3833 mpctx->sh_video=mpctx->d_video->sh=NULL;
3839 if(!mpctx->sh_video && !mpctx->sh_audio){
3840 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "No stream found.\n");
3841 #ifdef CONFIG_DVBIN
3842 if(mpctx->stream->type == STREAMTYPE_DVB)
3844 int dir;
3845 int v = mpctx->last_dvb_step;
3846 if(v > 0)
3847 dir = DVB_CHANNEL_HIGHER;
3848 else
3849 dir = DVB_CHANNEL_LOWER;
3851 if(dvb_step_channel(mpctx->stream, dir)) {
3852 mpctx->stop_play = PT_NEXT_ENTRY;
3853 mpctx->dvbin_reopen = 1;
3856 #endif
3857 goto goto_next_file; // exit_player(_("Fatal error"));
3860 /* display clip info */
3861 demux_info_print(mpctx->demuxer);
3863 //================== Read SUBTITLES (DVD & TEXT) ==========================
3864 if(vo_spudec==NULL &&
3865 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
3866 init_vo_spudec(mpctx);
3869 // after reading video params we should load subtitles because
3870 // we know fps so now we can adjust subtitle time to ~6 seconds AST
3871 // check .sub
3872 current_module="read_subtitles_file";
3873 double sub_fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
3874 if(sub_name){
3875 for (i = 0; sub_name[i] != NULL; ++i)
3876 add_subtitles(mpctx, sub_name[i], sub_fps, 0);
3878 if(sub_auto) { // auto load sub file ...
3879 char *psub = get_path( "sub/" );
3880 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
3881 int i = 0;
3882 free(psub); // release the buffer created by get_path() above
3883 while (tmp[i]) {
3884 add_subtitles(mpctx, tmp[i], sub_fps, 1);
3885 free(tmp[i++]);
3887 free(tmp);
3889 if (mpctx->set_of_sub_size > 0) {
3890 // setup global sub numbering
3891 mpctx->global_sub_indices[SUB_SOURCE_SUBS] = mpctx->global_sub_size; // the global # of the first sub.
3892 mpctx->global_sub_size += mpctx->set_of_sub_size;
3896 if (mpctx->global_sub_size) {
3897 select_subtitle(mpctx);
3898 if(subdata)
3899 switch (stream_dump_type) {
3900 case 3: list_sub_file(subdata); break;
3901 case 4: dump_mpsub(subdata, mpctx->sh_video->fps); break;
3902 case 6: dump_srt(subdata, mpctx->sh_video->fps); break;
3903 case 7: dump_microdvd(subdata, mpctx->sh_video->fps); break;
3904 case 8: dump_jacosub(subdata, mpctx->sh_video->fps); break;
3905 case 9: dump_sami(subdata, mpctx->sh_video->fps); break;
3909 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
3910 filename_recode(mpctx->filename));
3911 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
3912 if (mpctx->sh_video) {
3913 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3914 if (mpctx->sh_video->format >= 0x20202020)
3915 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
3916 else
3917 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
3918 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
3919 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
3920 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
3921 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
3922 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
3924 if (mpctx->sh_audio) {
3925 /* Assume FOURCC if all bytes >= 0x20 (' ') */
3926 if (mpctx->sh_audio->format >= 0x20202020)
3927 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
3928 else
3929 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
3930 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
3931 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
3932 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
3934 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2f\n", mpctx->timeline ?
3935 mpctx->timeline[mpctx->num_timeline_parts].start :
3936 demuxer_get_time_length(mpctx->demuxer));
3937 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n",
3938 mpctx->stream->seek && (!mpctx->demuxer || mpctx->demuxer->seekable));
3939 if (mpctx->demuxer) {
3940 if (mpctx->demuxer->num_chapters == 0)
3941 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
3942 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
3945 if(!mpctx->sh_video) goto main; // audio-only
3947 if(!reinit_video_chain(mpctx)) {
3948 if(!mpctx->sh_video){
3949 if(!mpctx->sh_audio) goto goto_next_file;
3950 goto main; // exit_player(_("Fatal error"));
3954 if(mpctx->sh_video->output_flags & 0x08 && vo_spudec)
3955 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
3957 #ifdef CONFIG_FREETYPE
3958 force_load_font = 1;
3959 #endif
3961 //================== MAIN: ==========================
3962 main:
3963 current_module="main";
3965 if(playing_msg) {
3966 char* msg = property_expand_string(mpctx, playing_msg);
3967 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
3968 free(msg);
3972 // Disable the term OSD in verbose mode
3973 if(verbose) term_osd = 0;
3976 //int frame_corr_num=0; //
3977 //float v_frame=0; // Video
3978 //float num_frames=0; // number of frames played
3980 int frame_time_remaining=0; // flag
3981 int blit_frame=0;
3983 // Make sure old OSD does not stay around,
3984 // e.g. with -fixed-vo and same-resolution files
3985 clear_osd_msgs();
3986 update_osd_msg(mpctx);
3988 //================ SETUP AUDIO ==========================
3990 if(mpctx->sh_audio){
3991 reinit_audio_chain(mpctx);
3992 if (mpctx->sh_audio && mpctx->sh_audio->codec)
3993 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
3996 current_module="av_init";
3998 if(mpctx->sh_video){
3999 mpctx->sh_video->timer=0;
4000 if (! ignore_start)
4001 audio_delay += mpctx->sh_video->stream_delay;
4003 if(mpctx->sh_audio){
4004 if (start_volume >= 0)
4005 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
4006 if (! ignore_start)
4007 audio_delay -= mpctx->sh_audio->stream_delay;
4008 mpctx->delay=-audio_delay;
4011 if(!mpctx->sh_audio){
4012 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Audio: no sound\n");
4013 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
4014 ds_free_packs(mpctx->d_audio); // free buffered chunks
4015 //mpctx->d_audio->id=-2; // do not read audio chunks
4016 //uninit_player(mpctx, INITIALIZED_AO); // close device
4018 if(!mpctx->sh_video){
4019 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Video: no video\n");
4020 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
4021 ds_free_packs(mpctx->d_video);
4022 mpctx->d_video->id=-2;
4023 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
4026 if (!mpctx->sh_video && !mpctx->sh_audio)
4027 goto goto_next_file;
4029 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
4030 if(force_fps && mpctx->sh_video){
4031 vo_fps = mpctx->sh_video->fps=force_fps;
4032 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
4033 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx->sh_video->fps,mpctx->sh_video->frametime);
4036 mp_input_set_section(mpctx->input, NULL);
4037 //TODO: add desired (stream-based) sections here
4038 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
4039 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
4041 //==================== START PLAYING =======================
4043 if(opts->loop_times>1) opts->loop_times--; else
4044 if(opts->loop_times==1) opts->loop_times = -1;
4046 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Starting playback...\n");
4048 total_time_usage_start=GetTimer();
4049 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
4050 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
4051 play_n_frames=play_n_frames_mf;
4053 if(play_n_frames==0){
4054 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
4057 // If there's a timeline force an absolute seek to initialize state
4058 if (seek_to_sec || mpctx->timeline) {
4059 seek(mpctx, seek_to_sec, SEEK_ABSOLUTE);
4060 end_at.pos += seek_to_sec;
4062 if (opts->chapterrange[0] > 0) {
4063 double pts;
4064 if (seek_chapter(mpctx, opts->chapterrange[0]-1, &pts, NULL) >= 0
4065 && pts > -1.0)
4066 seek(mpctx, pts, SEEK_ABSOLUTE);
4069 if (end_at.type == END_AT_SIZE) {
4070 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Option -endpos in MPlayer does not yet support size units.\n");
4071 end_at.type = END_AT_NONE;
4074 #ifdef CONFIG_DVDNAV
4075 mp_dvdnav_context_free(mpctx);
4076 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4077 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
4078 mp_dvdnav_cell_has_changed(mpctx->stream,1);
4080 #endif
4082 get_relative_time(mpctx); // reset current delta
4083 mpctx->time_frame = 0;
4084 mpctx->drop_message_shown = 0;
4085 mpctx->update_video_immediately = true;
4086 mpctx->total_avsync_change = 0;
4087 // Make sure VO knows current pause state
4088 if (mpctx->sh_video)
4089 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
4090 NULL);
4092 while(!mpctx->stop_play){
4093 float aq_sleep_time=0;
4095 if (opts->chapterrange[1] > 0) {
4096 int cur_chapter = get_current_chapter(mpctx);
4097 if(cur_chapter!=-1 && cur_chapter+1 > opts->chapterrange[1])
4098 goto goto_next_file;
4101 if(!mpctx->sh_audio && mpctx->d_audio->sh) {
4102 mpctx->sh_audio = mpctx->d_audio->sh;
4103 mpctx->sh_audio->ds = mpctx->d_audio;
4104 reinit_audio_chain(mpctx);
4107 /*========================== PLAY AUDIO ============================*/
4109 if (mpctx->sh_audio && !mpctx->paused)
4110 if (!fill_audio_out_buffers(mpctx))
4111 // at eof, all audio at least written to ao
4112 if (!mpctx->sh_video)
4113 mpctx->stop_play = AT_END_OF_FILE;
4116 if(!mpctx->sh_video) {
4117 // handle audio-only case:
4118 double a_pos=0;
4119 // sh_audio can be NULL due to video stream switching
4120 // TODO: handle this better
4121 if (mpctx->sh_audio)
4122 a_pos = playing_audio_pts(mpctx);
4124 print_status(mpctx, a_pos, false);
4126 if(end_at.type == END_AT_TIME && end_at.pos < a_pos)
4127 mpctx->stop_play = PT_NEXT_ENTRY;
4128 update_subtitles(mpctx, &mpctx->opts, NULL, a_pos, mpctx->video_offset,
4129 mpctx->d_sub, 0);
4130 update_osd_msg(mpctx);
4132 } else {
4134 /*========================== PLAY VIDEO ============================*/
4136 vo_pts=mpctx->sh_video->timer*90000.0;
4137 vo_fps=mpctx->sh_video->fps;
4139 blit_frame = mpctx->video_out->frame_loaded;
4140 if (!blit_frame) {
4141 double frame_time = update_video(mpctx);
4142 blit_frame = mpctx->video_out->frame_loaded;
4143 mp_dbg(MSGT_AVSYNC,MSGL_DBG2,"*** ftime=%5.3f ***\n",frame_time);
4144 if (mpctx->sh_video->vf_initialized < 0) {
4145 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "\nFATAL: Could not initialize video filters (-vf) or video output (-vo).\n");
4146 mpctx->stop_play = PT_NEXT_ENTRY;
4147 goto goto_next_file;
4149 if (blit_frame) {
4150 struct sh_video *sh_video = mpctx->sh_video;
4151 update_subtitles(mpctx, &mpctx->opts, sh_video, sh_video->pts,
4152 mpctx->video_offset, mpctx->d_sub, 0);
4153 update_teletext(sh_video, mpctx->demuxer, 0);
4154 update_osd_msg(mpctx);
4155 struct vf_instance *vf = mpctx->sh_video->vfilter;
4156 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
4157 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
4158 vo_osd_changed(0);
4160 if (frame_time < 0)
4161 mpctx->stop_play = AT_END_OF_FILE;
4162 else {
4163 if (mpctx->update_video_immediately) {
4164 // Show this frame immediately, rest normally
4165 mpctx->update_video_immediately = false;
4166 } else {
4167 mpctx->time_frame += frame_time / opts->playback_speed;
4168 adjust_sync(mpctx, frame_time);
4172 if (mpctx->timeline) {
4173 struct timeline_part *next = mpctx->timeline + mpctx->timeline_part + 1;
4174 if (mpctx->sh_video->pts >= next->start
4175 || mpctx->stop_play == AT_END_OF_FILE
4176 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
4177 seek(mpctx, next->start, SEEK_ABSOLUTE);
4178 continue;
4182 // ==========================================================================
4184 // current_module="draw_osd";
4185 // if(vo_config_count) mpctx->video_out->draw_osd();
4187 current_module="vo_check_events";
4188 vo_check_events(mpctx->video_out);
4190 #ifdef CONFIG_X11
4191 if (stop_xscreensaver) {
4192 current_module = "stop_xscreensaver";
4193 xscreensaver_heartbeat(mpctx->x11_state);
4195 #endif
4196 if (heartbeat_cmd) {
4197 static unsigned last_heartbeat;
4198 unsigned now = GetTimerMS();
4199 if (now - last_heartbeat > 30000) {
4200 last_heartbeat = now;
4201 system(heartbeat_cmd);
4205 frame_time_remaining = sleep_until_update(mpctx, &mpctx->time_frame, &aq_sleep_time);
4207 //====================== FLIP PAGE (VIDEO BLT): =========================
4209 current_module="flip_page";
4210 if (!frame_time_remaining && blit_frame) {
4211 unsigned int t2=GetTimer();
4212 unsigned int pts_us = mpctx->last_time + mpctx->time_frame * 1e6;
4213 int duration = -1;
4214 double pts2 = mpctx->video_out->next_pts2;
4215 if (pts2 != MP_NOPTS_VALUE && opts->correct_pts) {
4216 // expected A/V sync correction is ignored
4217 double diff = (pts2 - mpctx->sh_video->pts);
4218 diff /= opts->playback_speed;
4219 if (mpctx->time_frame < 0)
4220 diff += mpctx->time_frame;
4221 if (diff < 0)
4222 diff = 0;
4223 if (diff > 10)
4224 diff = 10;
4225 duration = diff * 1e6;
4227 vo_flip_page(mpctx->video_out, pts_us|1, duration);
4229 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
4230 vout_time_usage += mpctx->last_vo_flip_duration;
4231 if (mpctx->video_out->driver->flip_page_timed) {
4232 // No need to adjust sync based on flip speed
4233 mpctx->last_vo_flip_duration = 0;
4234 // For print_status - VO call finishing early is OK for sync
4235 mpctx->time_frame -= get_relative_time(mpctx);
4237 print_status(mpctx, MP_NOPTS_VALUE, true);
4239 else
4240 print_status(mpctx, MP_NOPTS_VALUE, false);
4242 //============================ Auto QUALITY ============================
4244 /*Output quality adjustments:*/
4245 if(auto_quality>0){
4246 current_module="autoq";
4247 // float total=0.000001f * (GetTimer()-aq_total_time);
4248 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
4249 if(output_quality<auto_quality && aq_sleep_time>0)
4250 ++output_quality;
4251 else
4252 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
4253 if(output_quality>1 && aq_sleep_time<0)
4254 --output_quality;
4255 else
4256 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
4257 output_quality=0;
4258 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
4259 set_video_quality(mpctx->sh_video,output_quality);
4262 if (!frame_time_remaining && blit_frame) {
4263 if (play_n_frames >= 0) {
4264 --play_n_frames;
4265 if (play_n_frames <= 0)
4266 mpctx->stop_play = PT_NEXT_ENTRY;
4268 if (mpctx->step_frames > 0) {
4269 mpctx->step_frames--;
4270 if (mpctx->step_frames == 0)
4271 pause_player(mpctx);
4276 // FIXME: add size based support for -endpos
4277 if (end_at.type == END_AT_TIME &&
4278 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
4279 mpctx->stop_play = PT_NEXT_ENTRY;
4281 } // end if(mpctx->sh_video)
4283 #ifdef CONFIG_DVDNAV
4284 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4285 nav_highlight_t hl;
4286 mp_dvdnav_get_highlight (mpctx->stream, &hl);
4287 osd_set_nav_box (hl.sx, hl.sy, hl.ex, hl.ey);
4288 vo_osd_changed (OSDTYPE_DVDNAV);
4290 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
4291 double ar = -1.0;
4292 if (stream_control (mpctx->demuxer->stream,
4293 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
4294 != STREAM_UNSUPPORTED)
4295 mpctx->sh_video->stream_aspect = ar;
4298 #endif
4300 //================= Keyboard events, SEEKing ====================
4302 current_module="key_events";
4305 while (1) {
4306 mp_cmd_t* cmd;
4307 while ((cmd = mp_input_get_cmd(mpctx->input, 0, 0)) != NULL) {
4308 run_command(mpctx, cmd);
4309 mp_cmd_free(cmd);
4310 if (mpctx->stop_play)
4311 break;
4313 if (!mpctx->paused || mpctx->stop_play || mpctx->rel_seek_secs
4314 || mpctx->abs_seek_pos)
4315 break;
4316 if (mpctx->sh_video) {
4317 update_osd_msg(mpctx);
4318 int hack = vo_osd_changed(0);
4319 vo_osd_changed(hack);
4320 if (hack)
4321 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
4322 add_step_frame(mpctx);
4323 break;
4325 else
4326 vo_osd_changed(0);
4328 pause_loop(mpctx);
4332 // handle -sstep
4333 if (step_sec > 0 && !mpctx->paused) {
4334 mpctx->osd_function=OSD_FFW;
4335 mpctx->rel_seek_secs+=step_sec;
4338 edl_update(mpctx);
4340 /* Looping. */
4341 if(mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
4342 mp_msg(MSGT_CPLAYER,MSGL_V,"loop_times = %d\n", opts->loop_times);
4344 if(opts->loop_times>1) opts->loop_times--; else
4345 if(opts->loop_times==1) opts->loop_times=-1;
4346 play_n_frames=play_n_frames_mf;
4347 mpctx->stop_play=0;
4348 mpctx->abs_seek_pos=SEEK_ABSOLUTE; mpctx->rel_seek_secs=seek_to_sec;
4351 if(mpctx->rel_seek_secs || mpctx->abs_seek_pos){
4352 seek(mpctx, mpctx->rel_seek_secs, mpctx->abs_seek_pos);
4354 mpctx->rel_seek_secs=0;
4355 mpctx->abs_seek_pos=0;
4358 } // while(!mpctx->stop_play)
4360 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4362 #ifdef CONFIG_DVBIN
4363 if(mpctx->dvbin_reopen)
4365 mpctx->stop_play = 0;
4366 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4367 cache_uninit(mpctx->stream);
4368 mpctx->dvbin_reopen = 0;
4369 goto goto_enable_cache;
4371 #endif
4374 goto_next_file: // don't jump here after ao/vo/getch initialization!
4376 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4378 if(benchmark){
4379 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4380 double total_time_usage;
4381 total_time_usage_start=GetTimer()-total_time_usage_start;
4382 total_time_usage = (float)total_time_usage_start*0.000001;
4383 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4384 video_time_usage,vout_time_usage,audio_time_usage,
4385 total_time_usage-tot,total_time_usage);
4386 if(total_time_usage>0.0)
4387 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4388 100.0*video_time_usage/total_time_usage,
4389 100.0*vout_time_usage/total_time_usage,
4390 100.0*audio_time_usage/total_time_usage,
4391 100.0*(total_time_usage-tot)/total_time_usage,
4392 100.0);
4393 if(total_frame_cnt && frame_dropping)
4394 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4395 total_frame_cnt-drop_frame_cnt,
4396 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4397 drop_frame_cnt,
4398 100*drop_frame_cnt/total_frame_cnt,
4399 total_frame_cnt,
4400 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4403 // time to uninit all, except global stuff:
4404 uninit_player(mpctx, INITIALIZED_ALL-(opts->fixed_vo?INITIALIZED_VO:0));
4406 if(mpctx->set_of_sub_size > 0) {
4407 current_module="sub_free";
4408 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4409 sub_free(mpctx->set_of_subtitles[i]);
4410 #ifdef CONFIG_ASS
4411 if(mpctx->set_of_ass_tracks[i])
4412 ass_free_track( mpctx->set_of_ass_tracks[i] );
4413 #endif
4415 mpctx->set_of_sub_size = 0;
4417 vo_sub_last = vo_sub=NULL;
4418 subdata=NULL;
4419 #ifdef CONFIG_ASS
4420 ass_track = NULL;
4421 if(ass_library)
4422 ass_clear_fonts(ass_library);
4423 #endif
4425 if (!mpctx->stop_play) // In case some goto jumped here...
4426 mpctx->stop_play = PT_NEXT_ENTRY;
4428 int playtree_direction = 1;
4430 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4431 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4432 play_tree_iter_free(mpctx->playtree_iter);
4433 mpctx->playtree_iter = NULL;
4435 mpctx->play_tree_step = 1;
4436 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4437 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4438 if(mpctx->playtree_iter) {
4439 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4440 play_tree_iter_free(mpctx->playtree_iter);
4441 mpctx->playtree_iter = NULL;
4444 } else if (mpctx->stop_play == PT_STOP) {
4445 play_tree_iter_free(mpctx->playtree_iter);
4446 mpctx->playtree_iter = NULL;
4447 } else { // NEXT PREV SRC
4448 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4451 while(mpctx->playtree_iter != NULL) {
4452 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4453 if(mpctx->filename == NULL) {
4454 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4455 play_tree_iter_free(mpctx->playtree_iter);
4456 mpctx->playtree_iter = NULL;
4458 } else
4459 break;
4462 if(mpctx->playtree_iter != NULL || player_idle_mode){
4463 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4464 mpctx->stop_play = 0;
4465 goto play_next_file;
4469 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4471 return 1;
4473 #endif /* DISABLE_MAIN */