options: change -alang and -slang to use string list type
[mplayer.git] / mplayer.c
blobf0a343140c937a1d1ae7694fc8c42ad6ce5f9903
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 <math.h>
27 #include "config.h"
28 #include "talloc.h"
30 #if defined(__MINGW32__) || defined(__CYGWIN__)
31 #define _UWIN 1 /*disable Non-underscored versions of non-ANSI functions as otherwise int eof would conflict with eof()*/
32 #include <windows.h>
33 #endif
34 #include <string.h>
35 #include <unistd.h>
37 // #include <sys/mman.h>
38 #include <sys/types.h>
39 #ifndef __MINGW32__
40 #include <sys/ioctl.h>
41 #include <sys/wait.h>
42 #else
43 #define SIGHUP 1 /* hangup */
44 #define SIGQUIT 3 /* quit */
45 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
46 #define SIGBUS 10 /* bus error */
47 #define SIGPIPE 13 /* broken pipe */
48 #endif
50 #include <sys/time.h>
51 #include <sys/stat.h>
53 #include <signal.h>
54 #include <time.h>
55 #include <fcntl.h>
56 #include <limits.h>
58 #include <errno.h>
60 #include "mp_msg.h"
61 #include "av_log.h"
64 #include "m_option.h"
65 #include "m_config.h"
66 #include "mplayer.h"
67 #include "access_mpcontext.h"
68 #include "m_property.h"
70 #include "cfg-mplayer-def.h"
72 #include "libavutil/avstring.h"
74 #include "sub/subreader.h"
75 #include "sub/dec_sub.h"
77 #include "mp_osd.h"
78 #include "libvo/video_out.h"
80 #include "sub/font_load.h"
81 #include "sub/sub.h"
82 #include "ffmpeg_files/intreadwrite.h"
83 #include "sub/av_sub.h"
84 #include "libmpcodecs/dec_teletext.h"
85 #include "cpudetect.h"
86 #include "version.h"
88 #ifdef CONFIG_X11
89 #include "libvo/x11_common.h"
90 #endif
92 #include "libao2/audio_out.h"
94 #include "codec-cfg.h"
96 #include "edl.h"
98 #include "sub/spudec.h"
99 #include "sub/vobsub.h"
101 #include "osdep/getch2.h"
102 #include "osdep/timer.h"
104 #include "input/input.h"
106 int slave_mode=0;
107 int enable_mouse_movements=0;
108 float start_volume = -1;
110 #include "osdep/priority.h"
112 char *heartbeat_cmd;
114 #ifdef HAVE_RTC
115 #ifdef __linux__
116 #include <linux/rtc.h>
117 #else
118 #include <rtc.h>
119 #define RTC_IRQP_SET RTCIO_IRQP_SET
120 #define RTC_PIE_ON RTCIO_PIE_ON
121 #endif /* __linux__ */
122 #endif /* HAVE_RTC */
124 #include "stream/tv.h"
125 #include "stream/stream_radio.h"
126 #ifdef CONFIG_DVBIN
127 #include "stream/dvbin.h"
128 #endif
129 #include "stream/cache2.h"
131 //**************************************************************************//
132 // Playtree
133 //**************************************************************************//
134 #include "playtree.h"
135 #include "playtreeparser.h"
137 //**************************************************************************//
138 // Config
139 //**************************************************************************//
140 #include "parser-cfg.h"
141 #include "parser-mpcmd.h"
143 //**************************************************************************//
144 // Config file
145 //**************************************************************************//
147 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
149 #include "path.h"
151 //**************************************************************************//
152 //**************************************************************************//
153 // Input media streaming & demultiplexer:
154 //**************************************************************************//
156 static int max_framesize=0;
158 #include "stream/stream.h"
159 #include "libmpdemux/demuxer.h"
160 #include "libmpdemux/stheader.h"
162 #ifdef CONFIG_DVDREAD
163 #include "stream/stream_dvd.h"
164 #endif
165 #include "stream/stream_dvdnav.h"
167 #include "libmpcodecs/dec_audio.h"
168 #include "libmpcodecs/dec_video.h"
169 #include "libmpcodecs/mp_image.h"
170 #include "libmpcodecs/vf.h"
171 #include "libmpcodecs/vd.h"
173 #include "mixer.h"
175 #include "mp_core.h"
176 #include "options.h"
177 #include "defaultopts.h"
179 static const char help_text[]=_(
180 "Usage: mplayer [options] [url|path/]filename\n"
181 "\n"
182 "Basic options: (complete list in the man page)\n"
183 " -vo <drv> select video output driver ('-vo help' for a list)\n"
184 " -ao <drv> select audio output driver ('-ao help' for a list)\n"
185 #ifdef CONFIG_VCD
186 " vcd://<trackno> play (S)VCD (Super Video CD) track (raw device, no mount)\n"
187 #endif
188 #ifdef CONFIG_DVDREAD
189 " dvd://<titleno> play DVD title from device instead of plain file\n"
190 #endif
191 " -alang/-slang select DVD audio/subtitle language (by 2-char country code)\n"
192 " -ss <position> seek to given (seconds or hh:mm:ss) position\n"
193 " -nosound do not play sound\n"
194 " -fs fullscreen playback (or -vm, -zoom, details in the man page)\n"
195 " -x <x> -y <y> set display resolution (for use with -vm or -zoom)\n"
196 " -sub <file> specify subtitle file to use (also see -subfps, -subdelay)\n"
197 " -playlist <file> specify playlist file\n"
198 " -vid x -aid y select video (x) and audio (y) stream to play\n"
199 " -fps x -srate y change video (x fps) and audio (y Hz) rate\n"
200 " -pp <quality> enable postprocessing filter (details in the man page)\n"
201 " -framedrop enable frame dropping (for slow machines)\n"
202 "\n"
203 "Basic keys: (complete list in the man page, also check input.conf)\n"
204 " <- or -> seek backward/forward 10 seconds\n"
205 " down or up seek backward/forward 1 minute\n"
206 " pgdown or pgup seek backward/forward 10 minutes\n"
207 " < or > step backward/forward in playlist\n"
208 " p or SPACE pause movie (press any key to continue)\n"
209 " q or ESC stop playing and quit program\n"
210 " + or - adjust audio delay by +/- 0.1 second\n"
211 " o cycle OSD mode: none / seekbar / seekbar + timer\n"
212 " * or / increase or decrease PCM volume\n"
213 " x or z adjust subtitle delay by +/- 0.1 second\n"
214 " r or t adjust subtitle position up/down, also see -vf expand\n"
215 "\n"
216 " * * * SEE THE MAN PAGE FOR DETAILS, FURTHER (ADVANCED) OPTIONS AND KEYS * * *\n"
217 "\n");
220 #define Exit_SIGILL_RTCpuSel _(\
221 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
222 " It may be a bug in our new runtime CPU-detection code...\n"\
223 " Please read DOCS/HTML/en/bugreports.html.\n")
225 #define Exit_SIGILL _(\
226 "- MPlayer crashed by an 'Illegal Instruction'.\n"\
227 " It usually happens when you run it on a CPU different than the one it was\n"\
228 " compiled/optimized for.\n"\
229 " Verify this!\n")
231 #define Exit_SIGSEGV_SIGFPE _(\
232 "- MPlayer crashed by bad usage of CPU/FPU/RAM.\n"\
233 " Recompile MPlayer with --enable-debug and make a 'gdb' backtrace and\n"\
234 " disassembly. Details in DOCS/HTML/en/bugreports_what.html#bugreports_crash.\n")
236 #define Exit_SIGCRASH _(\
237 "- MPlayer crashed. This shouldn't happen.\n"\
238 " It can be a bug in the MPlayer code _or_ in your drivers _or_ in your\n"\
239 " gcc version. If you think it's MPlayer's fault, please read\n"\
240 " DOCS/HTML/en/bugreports.html and follow the instructions there. We can't and\n"\
241 " won't help unless you provide this information when reporting a possible bug.\n")
243 #define SystemTooSlow _("\n\n"\
244 " ************************************************\n"\
245 " **** Your system is too SLOW to play this! ****\n"\
246 " ************************************************\n\n"\
247 "Possible reasons, problems, workarounds:\n"\
248 "- Most common: broken/buggy _audio_ driver\n"\
249 " - Try -ao sdl or use the OSS emulation of ALSA.\n"\
250 " - Experiment with different values for -autosync, 30 is a good start.\n"\
251 "- Slow video output\n"\
252 " - Try a different -vo driver (-vo help for a list) or try -framedrop!\n"\
253 "- Slow CPU\n"\
254 " - Don't try to play a big DVD/DivX on a slow CPU! Try some of the lavdopts,\n"\
255 " e.g. -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all.\n"\
256 "- Broken file\n"\
257 " - Try various combinations of -nobps -ni -forceidx -mc 0.\n"\
258 "- Slow media (NFS/SMB mounts, DVD, VCD etc)\n"\
259 " - Try -cache 8192.\n"\
260 "- Are you using -cache to play a non-interleaved AVI file?\n"\
261 " - Try -nocache.\n"\
262 "Read DOCS/HTML/en/video.html for tuning/speedup tips.\n"\
263 "If none of this helps you, read DOCS/HTML/en/bugreports.html.\n\n")
266 //**************************************************************************//
267 //**************************************************************************//
269 #include "mp_fifo.h"
271 // benchmark:
272 double video_time_usage=0;
273 double vout_time_usage=0;
274 static double audio_time_usage=0;
275 static int total_time_usage_start=0;
276 static int total_frame_cnt=0;
277 static int drop_frame_cnt=0; // total number of dropped frames
279 // options:
280 static int output_quality=0;
282 // seek:
283 static double seek_to_sec;
284 static off_t seek_to_byte=0;
285 static off_t step_sec=0;
287 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
289 // codecs:
290 char **audio_codec_list=NULL; // override audio codec
291 char **video_codec_list=NULL; // override video codec
292 char **audio_fm_list=NULL; // override audio codec family
293 char **video_fm_list=NULL; // override video codec family
295 // this dvdsub_id was selected via slang
296 // use this to allow dvdnav to follow -slang across stream resets,
297 // in particular the subtitle ID for a language changes
298 int dvdsub_lang_id;
299 int vobsub_id=-1;
300 static char* spudec_ifo=NULL;
301 int forced_subs_only=0;
302 int file_filter=1;
304 // cache2:
305 int stream_cache_size=-1;
307 // dump:
308 int stream_dump_type=0;
310 // A-V sync:
311 static float default_max_pts_correction=-1;//0.01f;
312 float audio_delay=0;
313 static int ignore_start=0;
315 double force_fps=0;
316 static int force_srate=0;
317 int frame_dropping=0; // option 0=no drop 1= drop vo 2= drop decode
318 static int play_n_frames=-1;
319 static int play_n_frames_mf=-1;
321 // sub:
322 char *font_name=NULL;
323 char *sub_font_name=NULL;
324 extern int font_fontconfig;
325 float font_factor=0.75;
326 char **sub_name=NULL;
327 float sub_delay=0;
328 float sub_fps=0;
329 int sub_auto = 1;
330 char *vobsub_name=NULL;
331 int subcc_enabled=0;
332 int suboverlap_enabled = 1;
334 #include "sub/ass_mp.h"
336 char* current_module=NULL; // for debugging
339 // ---
341 #ifdef CONFIG_MENU
342 #include "m_struct.h"
343 #include "libmenu/menu.h"
344 static const vf_info_t* const libmenu_vfs[] = {
345 &vf_info_menu,
346 NULL
348 static vf_instance_t* vf_menu = NULL;
349 int use_menu = 0;
350 static char* menu_cfg = NULL;
351 static char* menu_root = "main";
352 #endif
355 edl_record_ptr edl_records = NULL; ///< EDL entries memory area
356 edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
357 FILE* edl_fd = NULL; ///< fd to write to when in -edlout mode.
358 int use_filedir_conf;
359 int use_filename_title;
361 #include "mpcommon.h"
362 #include "command.h"
364 #include "metadata.h"
366 const void *mpctx_get_video_out(MPContext *mpctx)
368 return mpctx->video_out;
371 void *mpctx_get_demuxer(MPContext *mpctx)
373 return mpctx->demuxer;
376 void *mpctx_get_playtree_iter(MPContext *mpctx)
378 return mpctx->playtree_iter;
381 void *mpctx_get_mixer(MPContext *mpctx)
383 return &mpctx->mixer;
386 int mpctx_get_global_sub_size(MPContext *mpctx)
388 return mpctx->global_sub_size;
391 int mpctx_get_osd_function(MPContext *mpctx)
393 return mpctx->osd_function;
396 static float get_relative_time(struct MPContext *mpctx)
398 unsigned int new_time = GetTimer();
399 unsigned int delta = new_time - mpctx->last_time;
400 mpctx->last_time = new_time;
401 return delta * 0.000001;
404 static int is_valid_metadata_type(struct MPContext *mpctx, metadata_t type) {
405 switch (type)
407 /* check for valid video stream */
408 case META_VIDEO_CODEC:
409 case META_VIDEO_BITRATE:
410 case META_VIDEO_RESOLUTION:
412 if (!mpctx->sh_video)
413 return 0;
414 break;
417 /* check for valid audio stream */
418 case META_AUDIO_CODEC:
419 case META_AUDIO_BITRATE:
420 case META_AUDIO_SAMPLES:
422 if (!mpctx->sh_audio)
423 return 0;
424 break;
427 /* check for valid demuxer */
428 case META_INFO_TITLE:
429 case META_INFO_ARTIST:
430 case META_INFO_ALBUM:
431 case META_INFO_YEAR:
432 case META_INFO_COMMENT:
433 case META_INFO_TRACK:
434 case META_INFO_GENRE:
436 if (!mpctx->demuxer)
437 return 0;
438 break;
441 default:
442 break;
445 return 1;
448 static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
449 char **info = mpctx->demuxer->info;
450 int n;
452 if (!info || !tag)
453 return talloc_strdup(NULL, "");
455 for (n = 0; info[2*n] != NULL ; n++)
456 if (!strcasecmp (info[2*n], tag))
457 break;
459 return talloc_strdup(NULL, info[2*n+1] ? info[2*n+1] : "");
462 char *get_metadata(struct MPContext *mpctx, metadata_t type)
464 sh_audio_t * const sh_audio = mpctx->sh_audio;
465 sh_video_t * const sh_video = mpctx->sh_video;
467 if (!is_valid_metadata_type(mpctx, type))
468 return NULL;
470 switch (type) {
471 case META_NAME:
472 return talloc_strdup(NULL, mp_basename(mpctx->filename));
473 case META_VIDEO_CODEC:
474 if (sh_video->format == 0x10000001)
475 return talloc_strdup(NULL, "mpeg1");
476 else if (sh_video->format == 0x10000002)
477 return talloc_strdup(NULL, "mpeg2");
478 else if (sh_video->format == 0x10000004)
479 return talloc_strdup(NULL, "mpeg4");
480 else if (sh_video->format == 0x10000005)
481 return talloc_strdup(NULL, "h264");
482 else if (sh_video->format >= 0x20202020)
483 return talloc_asprintf(NULL, "%.4s", (char *) &sh_video->format);
484 else
485 return talloc_asprintf(NULL, "0x%08X", sh_video->format);
486 case META_VIDEO_BITRATE:
487 return talloc_asprintf(NULL, "%d kbps",
488 (int) (sh_video->i_bps * 8 / 1024));
489 case META_VIDEO_RESOLUTION:
490 return talloc_asprintf(NULL, "%d x %d", sh_video->disp_w,
491 sh_video->disp_h);
492 case META_AUDIO_CODEC:
493 if (sh_audio->codec && sh_audio->codec->name)
494 return talloc_strdup(NULL, sh_audio->codec->name);
495 return talloc_strdup(NULL, "");
496 case META_AUDIO_BITRATE:
497 return talloc_asprintf(NULL, "%d kbps",
498 (int) (sh_audio->i_bps * 8/1000));
499 case META_AUDIO_SAMPLES:
500 return talloc_asprintf(NULL, "%d Hz, %d ch.", sh_audio->samplerate,
501 sh_audio->channels);
503 /* check for valid demuxer */
504 case META_INFO_TITLE:
505 return get_demuxer_info(mpctx, "Title");
507 case META_INFO_ARTIST:
508 return get_demuxer_info(mpctx, "Artist");
510 case META_INFO_ALBUM:
511 return get_demuxer_info(mpctx, "Album");
513 case META_INFO_YEAR:
514 return get_demuxer_info(mpctx, "Year");
516 case META_INFO_COMMENT:
517 return get_demuxer_info(mpctx, "Comment");
519 case META_INFO_TRACK:
520 return get_demuxer_info(mpctx, "Track");
522 case META_INFO_GENRE:
523 return get_demuxer_info(mpctx, "Genre");
525 default:
526 break;
529 return talloc_strdup(NULL, "");
532 static void print_file_properties(struct MPContext *mpctx, const char *filename)
534 double start_pts = MP_NOPTS_VALUE;
535 double video_start_pts = MP_NOPTS_VALUE;
536 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_FILENAME=%s\n",
537 filename_recode(filename));
538 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_DEMUXER=%s\n", mpctx->demuxer->desc->name);
539 if (mpctx->sh_video) {
540 /* Assume FOURCC if all bytes >= 0x20 (' ') */
541 if (mpctx->sh_video->format >= 0x20202020)
542 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=%.4s\n", (char *)&mpctx->sh_video->format);
543 else
544 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FORMAT=0x%08X\n", mpctx->sh_video->format);
545 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_BITRATE=%d\n", mpctx->sh_video->i_bps*8);
546 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_WIDTH=%d\n", mpctx->sh_video->disp_w);
547 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_HEIGHT=%d\n", mpctx->sh_video->disp_h);
548 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_FPS=%5.3f\n", mpctx->sh_video->fps);
549 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_VIDEO_ASPECT=%1.4f\n", mpctx->sh_video->aspect);
550 video_start_pts = ds_get_next_pts(mpctx->d_video);
552 if (mpctx->sh_audio) {
553 /* Assume FOURCC if all bytes >= 0x20 (' ') */
554 if (mpctx->sh_audio->format >= 0x20202020)
555 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_FORMAT=%.4s\n", (char *)&mpctx->sh_audio->format);
556 else
557 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_FORMAT=%d\n", mpctx->sh_audio->format);
558 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_BITRATE=%d\n", mpctx->sh_audio->i_bps*8);
559 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_RATE=%d\n", mpctx->sh_audio->samplerate);
560 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_AUDIO_NCH=%d\n", mpctx->sh_audio->channels);
561 start_pts = ds_get_next_pts(mpctx->d_audio);
563 if (video_start_pts != MP_NOPTS_VALUE) {
564 if (start_pts == MP_NOPTS_VALUE || !mpctx->sh_audio ||
565 (mpctx->sh_video && video_start_pts < start_pts))
566 start_pts = video_start_pts;
568 if (start_pts != MP_NOPTS_VALUE)
569 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_START_TIME=%.2f\n", start_pts);
570 else
571 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_START_TIME=unknown\n");
572 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_LENGTH=%.2f\n", get_time_length(mpctx));
573 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_SEEKABLE=%d\n",
574 mpctx->stream->seek && (!mpctx->demuxer || mpctx->demuxer->seekable));
575 if (mpctx->demuxer) {
576 if (mpctx->demuxer->num_chapters == 0)
577 stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &mpctx->demuxer->num_chapters);
578 mp_msg(MSGT_IDENTIFY,MSGL_INFO,"ID_CHAPTERS=%d\n", mpctx->demuxer->num_chapters);
582 /// step size of mixer changes
583 int volstep = 3;
585 #ifdef CONFIG_DVDNAV
586 static void mp_dvdnav_context_free(MPContext *ctx){
587 if (ctx->nav_smpi) free_mp_image(ctx->nav_smpi);
588 ctx->nav_smpi = NULL;
589 free(ctx->nav_buffer);
590 ctx->nav_buffer = NULL;
591 ctx->nav_start = NULL;
592 ctx->nav_in_size = 0;
594 #endif
596 static void uninit_subs(struct demuxer *demuxer)
598 for (int i = 0; i < MAX_S_STREAMS; i++) {
599 struct sh_sub *sh = demuxer->s_streams[i];
600 if (sh && sh->initialized)
601 sub_uninit(sh);
605 void uninit_player(struct MPContext *mpctx, unsigned int mask){
606 mask &= mpctx->initialized_flags;
608 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n*** uninit(0x%X)\n",mask);
610 if(mask&INITIALIZED_ACODEC){
611 mpctx->initialized_flags&=~INITIALIZED_ACODEC;
612 current_module="uninit_acodec";
613 if(mpctx->sh_audio) uninit_audio(mpctx->sh_audio);
614 mpctx->sh_audio=NULL;
615 mpctx->mixer.afilter = NULL;
618 if (mask & INITIALIZED_SUB) {
619 mpctx->initialized_flags &= ~INITIALIZED_SUB;
620 if (mpctx->d_sub->sh)
621 sub_switchoff(mpctx->d_sub->sh, mpctx->osd);
624 if(mask&INITIALIZED_VCODEC){
625 mpctx->initialized_flags&=~INITIALIZED_VCODEC;
626 current_module="uninit_vcodec";
627 if(mpctx->sh_video) uninit_video(mpctx->sh_video);
628 mpctx->sh_video=NULL;
629 #ifdef CONFIG_MENU
630 vf_menu=NULL;
631 #endif
634 if(mask&INITIALIZED_DEMUXER){
635 mpctx->initialized_flags&=~INITIALIZED_DEMUXER;
636 current_module="free_demuxer";
637 if (mpctx->num_sources) {
638 mpctx->demuxer = mpctx->sources[0].demuxer;
639 for (int i = 1; i < mpctx->num_sources; i++) {
640 uninit_subs(mpctx->sources[i].demuxer);
641 free_stream(mpctx->sources[i].stream);
642 free_demuxer(mpctx->sources[i].demuxer);
645 talloc_free(mpctx->sources);
646 mpctx->sources = NULL;
647 mpctx->num_sources = 0;
648 talloc_free(mpctx->timeline);
649 mpctx->timeline = NULL;
650 mpctx->num_timeline_parts = 0;
651 talloc_free(mpctx->chapters);
652 mpctx->chapters = NULL;
653 mpctx->num_chapters = 0;
654 mpctx->video_offset = 0;
655 if(mpctx->demuxer){
656 mpctx->stream=mpctx->demuxer->stream;
657 uninit_subs(mpctx->demuxer);
658 free_demuxer(mpctx->demuxer);
660 mpctx->demuxer=NULL;
663 // kill the cache process:
664 if(mask&INITIALIZED_STREAM){
665 mpctx->initialized_flags&=~INITIALIZED_STREAM;
666 current_module="uninit_stream";
667 if(mpctx->stream) free_stream(mpctx->stream);
668 mpctx->stream=NULL;
671 if(mask&INITIALIZED_VO){
672 mpctx->initialized_flags&=~INITIALIZED_VO;
673 current_module="uninit_vo";
674 vo_destroy(mpctx->video_out);
675 mpctx->video_out=NULL;
676 #ifdef CONFIG_DVDNAV
677 mp_dvdnav_context_free(mpctx);
678 #endif
681 // Must be after libvo uninit, as few vo drivers (svgalib) have tty code.
682 if(mask&INITIALIZED_GETCH2){
683 mpctx->initialized_flags&=~INITIALIZED_GETCH2;
684 current_module="uninit_getch2";
685 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[uninit getch2]]]\n");
686 // restore terminal:
687 getch2_disable();
690 if(mask&INITIALIZED_VOBSUB){
691 mpctx->initialized_flags&=~INITIALIZED_VOBSUB;
692 current_module="uninit_vobsub";
693 if(vo_vobsub) vobsub_close(vo_vobsub);
694 vo_vobsub=NULL;
697 if (mask&INITIALIZED_SPUDEC){
698 mpctx->initialized_flags&=~INITIALIZED_SPUDEC;
699 current_module="uninit_spudec";
700 spudec_free(vo_spudec);
701 vo_spudec=NULL;
704 if(mask&INITIALIZED_AO){
705 mpctx->initialized_flags&=~INITIALIZED_AO;
706 current_module="uninit_ao";
707 if (mpctx->edl_muted) mixer_mute(&mpctx->mixer);
708 if (mpctx->ao)
709 ao_uninit(mpctx->ao, mpctx->stop_play != AT_END_OF_FILE);
710 mpctx->ao = NULL;
713 current_module=NULL;
716 void exit_player_with_rc(struct MPContext *mpctx, enum exit_reason how, int rc)
718 if (mpctx->user_muted && !mpctx->edl_muted) mixer_mute(&mpctx->mixer);
719 uninit_player(mpctx, INITIALIZED_ALL);
720 #if defined(__MINGW32__) || defined(__CYGWIN__)
721 timeEndPeriod(1);
722 #endif
723 #ifdef CONFIG_X11
724 vo_uninit(mpctx->x11_state); // Close the X11 connection (if any is open).
725 #endif
727 current_module="uninit_input";
728 mp_input_uninit(mpctx->input);
729 #ifdef CONFIG_MENU
730 if (use_menu)
731 menu_uninit();
732 #endif
734 #ifdef CONFIG_FREETYPE
735 current_module="uninit_font";
736 if (mpctx->osd && mpctx->osd->sub_font != vo_font)
737 free_font_desc(mpctx->osd->sub_font);
738 free_font_desc(vo_font);
739 vo_font = NULL;
740 done_freetype();
741 #endif
742 osd_free(mpctx->osd);
744 #ifdef CONFIG_ASS
745 ass_library_done(ass_library);
746 ass_library = NULL;
747 #endif
749 current_module="exit_player";
751 // free mplayer config
752 if(mpctx->mconfig)
753 m_config_free(mpctx->mconfig);
754 mpctx->mconfig = NULL;
756 if(mpctx->playtree_iter)
757 play_tree_iter_free(mpctx->playtree_iter);
758 mpctx->playtree_iter = NULL;
759 if(mpctx->playtree)
760 play_tree_free(mpctx->playtree, 1);
761 mpctx->playtree = NULL;
763 talloc_free(mpctx->key_fifo);
765 free(edl_records); // free mem allocated for EDL
766 edl_records = NULL;
767 switch(how) {
768 case EXIT_QUIT:
769 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Quit");
770 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=QUIT\n");
771 break;
772 case EXIT_EOF:
773 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","End of file");
774 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=EOF\n");
775 break;
776 case EXIT_ERROR:
777 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nExiting... (%s)\n","Fatal error");
778 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=ERROR\n");
779 break;
780 default:
781 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_EXIT=NONE\n");
783 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"max framesize was %d bytes\n",max_framesize);
785 exit(rc);
788 static void exit_player(struct MPContext *mpctx, enum exit_reason how)
790 exit_player_with_rc(mpctx, how, 1);
793 #ifndef __MINGW32__
794 static void child_sighandler(int x){
795 pid_t pid;
796 while((pid=waitpid(-1,NULL,WNOHANG)) > 0);
798 #endif
800 #ifdef CONFIG_CRASH_DEBUG
801 static char *prog_path;
802 static int crash_debug = 0;
803 #endif
805 static void exit_sighandler(int x){
806 static int sig_count=0;
807 #ifdef CONFIG_CRASH_DEBUG
808 if (!crash_debug || x != SIGTRAP)
809 #endif
810 ++sig_count;
811 if(sig_count==5)
813 /* We're crashing bad and can't uninit cleanly :(
814 * by popular request, we make one last (dirty)
815 * effort to restore the user's
816 * terminal. */
817 getch2_disable();
818 exit(1);
820 if(sig_count==6) exit(1);
821 if(sig_count>6){
822 // can't stop :(
823 #ifndef __MINGW32__
824 kill(getpid(),SIGKILL);
825 #endif
827 mp_msg(MSGT_CPLAYER, MSGL_FATAL, "\n");
828 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,
829 "\nMPlayer interrupted by signal %d in module: %s\n", x,
830 current_module ? current_module : "unknown");
831 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SIGNAL=%d\n", x);
832 if(sig_count<=1)
833 switch(x){
834 case SIGINT:
835 case SIGPIPE:
836 case SIGQUIT:
837 case SIGTERM:
838 case SIGKILL:
839 async_quit_request = 1;
840 return; // killed from keyboard (^C) or killed [-9]
841 case SIGILL:
842 #if CONFIG_RUNTIME_CPUDETECT
843 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL_RTCpuSel);
844 #else
845 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGILL);
846 #endif
847 case SIGFPE:
848 case SIGSEGV:
849 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGSEGV_SIGFPE);
850 default:
851 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,Exit_SIGCRASH);
852 #ifdef CONFIG_CRASH_DEBUG
853 if (crash_debug) {
854 int gdb_pid;
855 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
856 gdb_pid = fork();
857 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
858 if (gdb_pid == 0) { // We are the child
859 char spid[20];
860 snprintf(spid, sizeof(spid), "%i", getppid());
861 getch2_disable(); // allow terminal to work properly with gdb
862 if (execlp("gdb", "gdb", prog_path, spid, "-ex", "bt", NULL) == -1)
863 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
864 } else if (gdb_pid < 0)
865 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
866 else {
867 waitpid(gdb_pid, NULL, 0);
869 if (x == SIGTRAP) return;
871 #endif
873 getch2_disable();
874 exit(1);
877 #include "cfg-mplayer.h"
879 static int cfg_include(m_option_t *conf, char *filename)
881 return m_config_parse_config_file(conf->priv, filename);
884 static void parse_cfgfiles(struct MPContext *mpctx, m_config_t* conf)
886 struct MPOpts *opts = &mpctx->opts;
887 char *conffile;
888 int conffile_fd;
889 if (!(opts->noconfig & 2) &&
890 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mplayer.conf") < 0)
891 exit_player(mpctx, EXIT_NONE);
892 if ((conffile = get_path("")) == NULL) {
893 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"Cannot find HOME directory.\n");
894 } else {
895 #ifdef __MINGW32__
896 mkdir(conffile);
897 #else
898 mkdir(conffile, 0777);
899 #endif
900 free(conffile);
901 if ((conffile = get_path("config")) == NULL) {
902 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
903 } else {
904 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) {
905 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Creating config file: %s\n", conffile);
906 write(conffile_fd, default_config, strlen(default_config));
907 close(conffile_fd);
909 if (!(opts->noconfig & 1) &&
910 m_config_parse_config_file(conf, conffile) < 0)
911 exit_player(mpctx, EXIT_NONE);
912 free(conffile);
917 #define PROFILE_CFG_PROTOCOL "protocol."
919 static void load_per_protocol_config (m_config_t* conf, const char *const file)
921 char *str;
922 char protocol[strlen (PROFILE_CFG_PROTOCOL) + strlen (file) + 1];
923 m_profile_t *p;
925 /* does filename actually uses a protocol ? */
926 str = strstr (file, "://");
927 if (!str)
928 return;
930 sprintf (protocol, "%s%s", PROFILE_CFG_PROTOCOL, file);
931 protocol[strlen (PROFILE_CFG_PROTOCOL)+strlen(file)-strlen(str)] = '\0';
932 p = m_config_get_profile (conf, protocol);
933 if (p)
935 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading protocol-related profile '%s'\n", protocol);
936 m_config_set_profile(conf,p);
940 #define PROFILE_CFG_EXTENSION "extension."
942 static void load_per_extension_config (m_config_t* conf, const char *const file)
944 char *str;
945 char extension[strlen (PROFILE_CFG_EXTENSION) + 8];
946 m_profile_t *p;
948 /* does filename actually have an extension ? */
949 str = strrchr (file, '.');
950 if (!str)
951 return;
953 sprintf (extension, PROFILE_CFG_EXTENSION);
954 strncat (extension, ++str, 7);
955 p = m_config_get_profile (conf, extension);
956 if (p)
958 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", extension);
959 m_config_set_profile(conf,p);
963 #define PROFILE_CFG_VO "vo."
964 #define PROFILE_CFG_AO "ao."
966 static void load_per_output_config (m_config_t* conf, char *cfg, char *out)
968 char profile[strlen (cfg) + strlen (out) + 1];
969 m_profile_t *p;
971 sprintf (profile, "%s%s", cfg, out);
972 p = m_config_get_profile (conf, profile);
973 if (p)
975 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Loading extension-related profile '%s'\n", profile);
976 m_config_set_profile(conf,p);
981 * Tries to load a config file
982 * @return 0 if file was not found, 1 otherwise
984 static int try_load_config(m_config_t *conf, const char *file)
986 struct stat st;
987 if (stat(file, &st))
988 return 0;
989 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Loading config '%s'\n", file);
990 m_config_parse_config_file (conf, file);
991 return 1;
994 static void load_per_file_config (m_config_t* conf, const char *const file)
996 char *confpath;
997 char cfg[PATH_MAX];
998 const char *name;
1000 if (strlen(file) > PATH_MAX - 14) {
1001 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Filename is too long, can not load file or directory specific config files\n");
1002 return;
1004 sprintf (cfg, "%s.conf", file);
1006 name = mp_basename(cfg);
1007 if (use_filedir_conf) {
1008 char dircfg[PATH_MAX];
1009 strcpy(dircfg, cfg);
1010 strcpy(dircfg + (name - cfg), "mplayer.conf");
1011 try_load_config(conf, dircfg);
1013 if (try_load_config(conf, cfg))
1014 return;
1017 if ((confpath = get_path (name)) != NULL)
1019 try_load_config(conf, confpath);
1021 free (confpath);
1025 /* When libmpdemux performs a blocking operation (network connection or
1026 * cache filling) if the operation fails we use this function to check
1027 * if it was interrupted by the user.
1028 * The function returns a new value for eof. */
1029 static int libmpdemux_was_interrupted(struct MPContext *mpctx, int stop_play)
1031 mp_cmd_t* cmd;
1032 if((cmd = mp_input_get_cmd(mpctx->input, 0, 0)) != NULL) {
1033 switch(cmd->id) {
1034 case MP_CMD_QUIT:
1035 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
1036 case MP_CMD_PLAY_TREE_STEP: {
1037 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_ENTRY : PT_PREV_ENTRY;
1038 mpctx->play_tree_step = (cmd->args[0].v.i == 0) ? 1 : cmd->args[0].v.i;
1039 } break;
1040 case MP_CMD_PLAY_TREE_UP_STEP: {
1041 stop_play = (cmd->args[0].v.i > 0) ? PT_UP_NEXT : PT_UP_PREV;
1042 } break;
1043 case MP_CMD_PLAY_ALT_SRC_STEP: {
1044 stop_play = (cmd->args[0].v.i > 0) ? PT_NEXT_SRC : PT_PREV_SRC;
1045 } break;
1047 mp_cmd_free(cmd);
1049 return stop_play;
1052 static int playtree_add_playlist(struct MPContext *mpctx, play_tree_t* entry)
1054 play_tree_add_bpf(entry,mpctx->filename);
1057 if(!entry) {
1058 entry = mpctx->playtree_iter->tree;
1059 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1060 return PT_NEXT_ENTRY;
1062 if(mpctx->playtree_iter->tree == entry ) { // Loop with a single file
1063 if(play_tree_iter_up_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1064 return PT_NEXT_ENTRY;
1067 play_tree_remove(entry,1,1);
1068 return PT_NEXT_SRC;
1070 play_tree_insert_entry(mpctx->playtree_iter->tree,entry);
1071 play_tree_set_params_from(entry,mpctx->playtree_iter->tree);
1072 entry = mpctx->playtree_iter->tree;
1073 if(play_tree_iter_step(mpctx->playtree_iter,1,0) != PLAY_TREE_ITER_ENTRY) {
1074 return PT_NEXT_ENTRY;
1076 play_tree_remove(entry,1,1);
1078 return PT_NEXT_SRC;
1081 void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr)
1083 struct MPOpts *opts = &mpctx->opts;
1084 sub_data *subd = NULL;
1085 struct ass_track *asst = NULL;
1086 bool is_native_ass = false;
1088 if (filename == NULL || mpctx->set_of_sub_size >= MAX_SUBTITLE_FILES)
1089 return;
1091 #ifdef CONFIG_ASS
1092 if (opts->ass_enabled) {
1093 #ifdef CONFIG_ICONV
1094 asst = mp_ass_read_stream(ass_library, filename, sub_cp);
1095 #else
1096 asst = mp_ass_read_stream(ass_library, filename, 0);
1097 #endif
1098 is_native_ass = asst;
1099 if (!asst) {
1100 subd = sub_read_file(filename, fps, &mpctx->opts);
1101 if (subd) {
1102 asst = mp_ass_read_subdata(ass_library, subd, fps);
1103 sub_free(subd);
1104 subd = NULL;
1107 } else
1108 #endif
1109 subd = sub_read_file(filename, fps, &mpctx->opts);
1112 if (!asst && !subd) {
1113 mp_tmsg(MSGT_CPLAYER, noerr ? MSGL_WARN : MSGL_ERR,
1114 "Cannot load subtitles: %s\n", filename_recode(filename));
1115 return;
1118 mpctx->set_of_ass_tracks[mpctx->set_of_sub_size] = asst;
1119 mpctx->set_of_subtitles[mpctx->set_of_sub_size] = subd;
1120 mpctx->track_was_native_ass[mpctx->set_of_sub_size] = is_native_ass;
1121 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_ID=%d\n", mpctx->set_of_sub_size);
1122 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
1123 filename_recode(filename));
1124 ++mpctx->set_of_sub_size;
1125 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "SUB: Added subtitle file (%d): %s\n", mpctx->set_of_sub_size,
1126 filename_recode(filename));
1129 void init_vo_spudec(struct MPContext *mpctx)
1131 spudec_free(vo_spudec);
1132 mpctx->initialized_flags &= ~INITIALIZED_SPUDEC;
1133 vo_spudec = NULL;
1135 // we currently can't work without video stream
1136 if (!mpctx->sh_video)
1137 return;
1139 if (spudec_ifo) {
1140 unsigned int palette[16], width, height;
1141 current_module="spudec_init_vobsub";
1142 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
1143 vo_spudec=spudec_new_scaled(palette, width, height, NULL, 0);
1146 #ifdef CONFIG_DVDREAD
1147 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVD) {
1148 current_module="spudec_init_dvdread";
1149 vo_spudec=spudec_new_scaled(((dvd_priv_t *)(mpctx->stream->priv))->cur_pgc->palette,
1150 mpctx->sh_video->disp_w, mpctx->sh_video->disp_h,
1151 NULL, 0);
1153 #endif
1155 #ifdef CONFIG_DVDNAV
1156 if (vo_spudec==NULL && mpctx->stream->type==STREAMTYPE_DVDNAV) {
1157 unsigned int *palette = mp_dvdnav_get_spu_clut(mpctx->stream);
1158 current_module="spudec_init_dvdnav";
1159 vo_spudec=spudec_new_scaled(palette, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, NULL, 0);
1161 #endif
1163 if (vo_spudec==NULL) {
1164 sh_sub_t *sh = mpctx->d_sub->sh;
1165 current_module="spudec_init_normal";
1166 vo_spudec=spudec_new_scaled(NULL, mpctx->sh_video->disp_w, mpctx->sh_video->disp_h, sh->extradata, sh->extradata_len);
1167 spudec_set_font_factor(vo_spudec,font_factor);
1170 if (vo_spudec!=NULL) {
1171 mpctx->initialized_flags|=INITIALIZED_SPUDEC;
1172 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
1177 * In Mac OS X the SDL-lib is built upon Cocoa. The easiest way to
1178 * make it all work is to use the builtin SDL-bootstrap code, which
1179 * will be done automatically by replacing our main() if we include SDL.h.
1181 #if defined(__APPLE__) && defined(CONFIG_SDL)
1182 #ifdef CONFIG_SDL_SDL_H
1183 #include <SDL/SDL.h>
1184 #else
1185 #include <SDL.h>
1186 #endif
1187 #endif
1190 * \brief append a formatted string
1191 * \param buf buffer to print into
1192 * \param pos position of terminating 0 in buf
1193 * \param len maximum number of characters in buf, not including terminating 0
1194 * \param format printf format string
1196 static void saddf(char *buf, unsigned *pos, int len, const char *format, ...)
1198 va_list va;
1199 va_start(va, format);
1200 *pos += vsnprintf(&buf[*pos], len - *pos, format, va);
1201 va_end(va);
1202 if (*pos >= len ) {
1203 buf[len] = 0;
1204 *pos = len;
1209 * \brief append time in the hh:mm:ss.f format
1210 * \param buf buffer to print into
1211 * \param pos position of terminating 0 in buf
1212 * \param len maximum number of characters in buf, not including terminating 0
1213 * \param time time value to convert/append
1215 static void sadd_hhmmssf(char *buf, unsigned *pos, int len, float time) {
1216 int64_t tenths = 10 * time;
1217 int f1 = tenths % 10;
1218 int ss = (tenths / 10) % 60;
1219 int mm = (tenths / 600) % 60;
1220 int hh = tenths / 36000;
1221 if (time <= 0) {
1222 saddf(buf, pos, len, "unknown");
1223 return;
1225 if (hh > 0)
1226 saddf(buf, pos, len, "%2d:", hh);
1227 if (hh > 0 || mm > 0)
1228 saddf(buf, pos, len, "%02d:", mm);
1229 saddf(buf, pos, len, "%02d.%1d", ss, f1);
1232 static void print_status(struct MPContext *mpctx, double a_pos, bool at_frame)
1234 struct MPOpts *opts = &mpctx->opts;
1235 sh_video_t * const sh_video = mpctx->sh_video;
1237 if (mpctx->sh_audio && a_pos == MP_NOPTS_VALUE)
1238 a_pos = playing_audio_pts(mpctx);
1239 if (mpctx->sh_audio && sh_video && at_frame) {
1240 mpctx->last_av_difference = a_pos - mpctx->video_pts - audio_delay;
1241 if (mpctx->time_frame > 0)
1242 mpctx->last_av_difference += mpctx->time_frame * opts->playback_speed;
1243 if (mpctx->last_av_difference > 0.5 && drop_frame_cnt > 50
1244 && !mpctx->drop_message_shown) {
1245 mp_tmsg(MSGT_AVSYNC,MSGL_WARN,SystemTooSlow);
1246 mpctx->drop_message_shown = true;
1249 if (opts->quiet)
1250 return;
1253 int width;
1254 char *line;
1255 unsigned pos = 0;
1256 get_screen_size();
1257 if (screen_width > 0)
1258 width = screen_width;
1259 else
1260 width = 80;
1261 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1262 /* Windows command line is broken (MinGW's rxvt works, but we
1263 * should not depend on that). */
1264 width--;
1265 #endif
1266 line = malloc(width + 1); // one additional char for the terminating null
1268 // Audio time
1269 if (mpctx->sh_audio) {
1270 saddf(line, &pos, width, "A:%6.1f ", a_pos);
1271 if (!sh_video) {
1272 float len = get_time_length(mpctx);
1273 saddf(line, &pos, width, "(");
1274 sadd_hhmmssf(line, &pos, width, a_pos);
1275 saddf(line, &pos, width, ") of %.1f (", len);
1276 sadd_hhmmssf(line, &pos, width, len);
1277 saddf(line, &pos, width, ") ");
1281 // Video time
1282 if (sh_video)
1283 saddf(line, &pos, width, "V:%6.1f ", mpctx->video_pts);
1285 // A-V sync
1286 if (mpctx->sh_audio && sh_video)
1287 saddf(line, &pos, width, "A-V:%7.3f ct:%7.3f ",
1288 mpctx->last_av_difference, mpctx->total_avsync_change);
1290 // Video stats
1291 if (sh_video)
1292 saddf(line, &pos, width, "%3d/%3d ",
1293 (int)sh_video->num_frames,
1294 (int)sh_video->num_frames_decoded);
1296 // CPU usage
1297 if (sh_video) {
1298 if (sh_video->timer > 0.5)
1299 saddf(line, &pos, width, "%2d%% %2d%% %4.1f%% ",
1300 (int)(100.0*video_time_usage*opts->playback_speed/(double)sh_video->timer),
1301 (int)(100.0*vout_time_usage*opts->playback_speed/(double)sh_video->timer),
1302 (100.0*audio_time_usage*opts->playback_speed/(double)sh_video->timer));
1303 else
1304 saddf(line, &pos, width, "??%% ??%% ??,?%% ");
1305 } else if (mpctx->sh_audio) {
1306 if (mpctx->delay > 0.5)
1307 saddf(line, &pos, width, "%4.1f%% ",
1308 100.0*audio_time_usage/(double)mpctx->delay);
1309 else
1310 saddf(line, &pos, width, "??,?%% ");
1313 // VO stats
1314 if (sh_video)
1315 saddf(line, &pos, width, "%d %d ", drop_frame_cnt, output_quality);
1317 #ifdef CONFIG_STREAM_CACHE
1318 // cache stats
1319 if (stream_cache_size > 0)
1320 saddf(line, &pos, width, "%d%% ", cache_fill_status(mpctx->stream));
1321 #endif
1323 // other
1324 if (opts->playback_speed != 1)
1325 saddf(line, &pos, width, "%4.2fx ", opts->playback_speed);
1327 // end
1328 if (erase_to_end_of_line) {
1329 line[pos] = 0;
1330 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s%s\r", line, erase_to_end_of_line);
1331 } else {
1332 memset(&line[pos], ' ', width - pos);
1333 line[width] = 0;
1334 mp_msg(MSGT_STATUSLINE, MSGL_STATUS, "%s\r", line);
1336 free(line);
1340 * \brief build a chain of audio filters that converts the input format
1341 * to the ao's format, taking into account the current playback_speed.
1342 * sh_audio describes the requested input format of the chain.
1343 * ao describes the requested output format of the chain.
1345 static int build_afilter_chain(struct MPContext *mpctx)
1347 struct sh_audio *sh_audio = mpctx->sh_audio;
1348 struct ao *ao = mpctx->ao;
1349 struct MPOpts *opts = &mpctx->opts;
1350 int new_srate;
1351 int result;
1352 if (!sh_audio)
1354 mpctx->mixer.afilter = NULL;
1355 return 0;
1357 if(af_control_any_rev(sh_audio->afilter,
1358 AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET,
1359 &opts->playback_speed)) {
1360 new_srate = sh_audio->samplerate;
1361 } else {
1362 new_srate = sh_audio->samplerate * opts->playback_speed;
1363 if (new_srate != ao->samplerate) {
1364 // limits are taken from libaf/af_resample.c
1365 if (new_srate < 8000)
1366 new_srate = 8000;
1367 if (new_srate > 192000)
1368 new_srate = 192000;
1369 opts->playback_speed = (float)new_srate / (float)sh_audio->samplerate;
1372 result = init_audio_filters(sh_audio, new_srate,
1373 &ao->samplerate, &ao->channels, &ao->format);
1374 mpctx->mixer.afilter = sh_audio->afilter;
1375 return result;
1379 typedef struct mp_osd_msg mp_osd_msg_t;
1380 struct mp_osd_msg {
1381 /// Previous message on the stack.
1382 mp_osd_msg_t* prev;
1383 /// Message text.
1384 char msg[128];
1385 int id,level,started;
1386 /// Display duration in ms.
1387 unsigned time;
1390 /// OSD message stack.
1391 static mp_osd_msg_t* osd_msg_stack = NULL;
1394 * \brief Add a message on the OSD message stack
1396 * If a message with the same id is already present in the stack
1397 * it is pulled on top of the stack, otherwise a new message is created.
1400 static void set_osd_msg_va(int id, int level, int time, const char *fmt,
1401 va_list ap)
1403 mp_osd_msg_t *msg,*last=NULL;
1404 int r;
1406 // look if the id is already in the stack
1407 for(msg = osd_msg_stack ; msg && msg->id != id ;
1408 last = msg, msg = msg->prev);
1409 // not found: alloc it
1410 if(!msg) {
1411 msg = calloc(1,sizeof(mp_osd_msg_t));
1412 msg->prev = osd_msg_stack;
1413 osd_msg_stack = msg;
1414 } else if(last) { // found, but it's not on top of the stack
1415 last->prev = msg->prev;
1416 msg->prev = osd_msg_stack;
1417 osd_msg_stack = msg;
1419 // write the msg
1420 r = vsnprintf(msg->msg, 128, fmt, ap);
1421 if(r >= 128) msg->msg[127] = 0;
1422 // set id and time
1423 msg->id = id;
1424 msg->level = level;
1425 msg->time = time;
1429 void set_osd_msg(int id, int level, int time, const char *fmt, ...)
1431 va_list ap;
1432 va_start(ap, fmt);
1433 set_osd_msg_va(id, level, time, fmt, ap);
1434 va_end(ap);
1437 void set_osd_tmsg(int id, int level, int time, const char *fmt, ...)
1439 va_list ap;
1440 va_start(ap, fmt);
1441 set_osd_msg_va(id, level, time, mp_gtext(fmt), ap);
1442 va_end(ap);
1447 * \brief Remove a message from the OSD stack
1449 * This function can be used to get rid of a message right away.
1453 void rm_osd_msg(int id) {
1454 mp_osd_msg_t *msg,*last=NULL;
1456 // Search for the msg
1457 for(msg = osd_msg_stack ; msg && msg->id != id ;
1458 last = msg, msg = msg->prev);
1459 if(!msg) return;
1461 // Detach it from the stack and free it
1462 if(last)
1463 last->prev = msg->prev;
1464 else
1465 osd_msg_stack = msg->prev;
1466 free(msg);
1470 * \brief Remove all messages from the OSD stack
1474 static void clear_osd_msgs(void) {
1475 mp_osd_msg_t* msg = osd_msg_stack, *prev = NULL;
1476 while(msg) {
1477 prev = msg->prev;
1478 free(msg);
1479 msg = prev;
1481 osd_msg_stack = NULL;
1485 * \brief Get the current message from the OSD stack.
1487 * This function decrements the message timer and destroys the old ones.
1488 * The message that should be displayed is returned (if any).
1492 static mp_osd_msg_t* get_osd_msg(struct MPContext *mpctx)
1494 struct MPOpts *opts = &mpctx->opts;
1495 mp_osd_msg_t *msg,*prev,*last = NULL;
1496 static unsigned last_update = 0;
1497 unsigned now = GetTimerMS();
1498 unsigned diff;
1499 char hidden_dec_done = 0;
1501 if (mpctx->osd_visible) {
1502 // 36000000 means max timed visibility is 1 hour into the future, if
1503 // the difference is greater assume it's wrapped around from below 0
1504 if (mpctx->osd_visible - now > 36000000) {
1505 mpctx->osd_visible = 0;
1506 vo_osd_progbar_type = -1; // disable
1507 vo_osd_changed(OSDTYPE_PROGBAR);
1508 mpctx->osd_function = mpctx->paused ? OSD_PAUSE : OSD_PLAY;
1511 if (mpctx->osd_show_percentage_until - now > 36000000)
1512 mpctx->osd_show_percentage_until = 0;
1514 if(!last_update) last_update = now;
1515 diff = now >= last_update ? now - last_update : 0;
1517 last_update = now;
1519 // Look for the first message in the stack with high enough level.
1520 for(msg = osd_msg_stack ; msg ; last = msg, msg = prev) {
1521 prev = msg->prev;
1522 if (msg->level > opts->osd_level && hidden_dec_done)
1523 continue;
1524 // The message has a high enough level or it is the first hidden one
1525 // in both cases we decrement the timer or kill it.
1526 if(!msg->started || msg->time > diff) {
1527 if(msg->started) msg->time -= diff;
1528 else msg->started = 1;
1529 // display it
1530 if (msg->level <= opts->osd_level)
1531 return msg;
1532 hidden_dec_done = 1;
1533 continue;
1535 // kill the message
1536 free(msg);
1537 if(last) {
1538 last->prev = prev;
1539 msg = last;
1540 } else {
1541 osd_msg_stack = prev;
1542 msg = NULL;
1545 // Nothing found
1546 return NULL;
1550 * \brief Display the OSD bar.
1552 * Display the OSD bar or fall back on a simple message.
1556 void set_osd_bar(struct MPContext *mpctx, int type,const char* name,double min,double max,double val) {
1557 struct MPOpts *opts = &mpctx->opts;
1558 if (opts->osd_level < 1)
1559 return;
1561 if(mpctx->sh_video) {
1562 mpctx->osd_visible = (GetTimerMS() + 1000) | 1;
1563 vo_osd_progbar_type = type;
1564 vo_osd_progbar_value = 256*(val-min)/(max-min);
1565 vo_osd_changed(OSDTYPE_PROGBAR);
1566 return;
1569 set_osd_msg(OSD_MSG_BAR, 1, opts->osd_duration, "%s: %d %%",
1570 name, ROUND(100*(val-min)/(max-min)));
1574 * \brief Display text subtitles on the OSD
1576 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs)
1578 int i;
1579 vo_sub = subs;
1580 vo_osd_changed(OSDTYPE_SUBTITLE);
1581 if (!mpctx->sh_video) {
1582 // reverse order, since newest set_osd_msg is displayed first
1583 for (i = SUB_MAX_TEXT - 1; i >= 0; i--) {
1584 if (!subs || i >= subs->lines || !subs->text[i])
1585 rm_osd_msg(OSD_MSG_SUB_BASE + i);
1586 else {
1587 // HACK: currently display time for each sub line except the last is set to 2 seconds.
1588 int display_time = i == subs->lines - 1 ? 180000 : 2000;
1589 set_osd_msg(OSD_MSG_SUB_BASE + i, 1, display_time, "%s", subs->text[i]);
1596 * \brief Update the OSD message line.
1598 * This function displays the current message on the vo OSD or on the term.
1599 * If the stack is empty and the OSD level is high enough the timer
1600 * is displayed (only on the vo OSD).
1604 static void update_osd_msg(struct MPContext *mpctx)
1606 struct MPOpts *opts = &mpctx->opts;
1607 mp_osd_msg_t *msg;
1608 struct osd_state *osd = mpctx->osd;
1609 char osd_text_timer[128];
1611 if (mpctx->add_osd_seek_info) {
1612 double percentage = get_percent_pos(mpctx);
1613 set_osd_bar(mpctx, 0, "Position", 0, 100, percentage);
1614 if (mpctx->sh_video)
1615 mpctx->osd_show_percentage_until = (GetTimerMS() + 1000) | 1;
1616 mpctx->add_osd_seek_info = false;
1619 // Look if we have a msg
1620 if((msg = get_osd_msg(mpctx))) {
1621 if (strcmp(osd->osd_text, msg->msg)) {
1622 strncpy(osd->osd_text, msg->msg, 127);
1623 if(mpctx->sh_video) vo_osd_changed(OSDTYPE_OSD); else
1624 if(opts->term_osd)
1625 mp_msg(MSGT_CPLAYER,MSGL_STATUS, "%s%s\n", opts->term_osd_esc,
1626 msg->msg);
1628 return;
1631 if(mpctx->sh_video) {
1632 // fallback on the timer
1633 if (opts->osd_level >= 2) {
1634 int len = get_time_length(mpctx);
1635 int percentage = -1;
1636 char percentage_text[10];
1637 int pts = get_current_time(mpctx);
1639 if (mpctx->osd_show_percentage_until)
1640 percentage = get_percent_pos(mpctx);
1642 if (percentage >= 0)
1643 snprintf(percentage_text, 9, " (%d%%)", percentage);
1644 else
1645 percentage_text[0] = 0;
1647 if (opts->osd_level == 3)
1648 snprintf(osd_text_timer, 63,
1649 "%c %02d:%02d:%02d / %02d:%02d:%02d%s",
1650 mpctx->osd_function,pts/3600,(pts/60)%60,pts%60,
1651 len/3600,(len/60)%60,len%60,percentage_text);
1652 else
1653 snprintf(osd_text_timer, 63, "%c %02d:%02d:%02d%s",
1654 mpctx->osd_function,pts/3600,(pts/60)%60,
1655 pts%60,percentage_text);
1656 } else
1657 osd_text_timer[0]=0;
1659 if (strcmp(osd->osd_text, osd_text_timer)) {
1660 strncpy(osd->osd_text, osd_text_timer, 63);
1661 vo_osd_changed(OSDTYPE_OSD);
1663 return;
1666 // Clear the term osd line
1667 if (opts->term_osd && osd->osd_text[0]) {
1668 osd->osd_text[0] = 0;
1669 printf("%s\n", opts->term_osd_esc);
1673 ///@}
1674 // OSDMsgStack
1677 void reinit_audio_chain(struct MPContext *mpctx)
1679 struct MPOpts *opts = &mpctx->opts;
1680 struct ao *ao;
1681 if (!mpctx->sh_audio)
1682 return;
1683 if (!(mpctx->initialized_flags & INITIALIZED_ACODEC)) {
1684 current_module="init_audio_codec";
1685 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1686 if(!init_best_audio_codec(mpctx->sh_audio,audio_codec_list,audio_fm_list)){
1687 goto init_error;
1689 mpctx->initialized_flags|=INITIALIZED_ACODEC;
1690 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1694 current_module="af_preinit";
1695 if (!(mpctx->initialized_flags & INITIALIZED_AO)) {
1696 mpctx->initialized_flags |= INITIALIZED_AO;
1697 mpctx->ao = ao_create();
1698 mpctx->ao->samplerate = force_srate;
1699 mpctx->ao->format = opts->audio_output_format;
1701 ao = mpctx->ao;
1703 // first init to detect best values
1704 if(!init_audio_filters(mpctx->sh_audio, // preliminary init
1705 // input:
1706 mpctx->sh_audio->samplerate,
1707 // output:
1708 &ao->samplerate, &ao->channels, &ao->format)) {
1709 mp_tmsg(MSGT_CPLAYER,MSGL_ERR, "Error at audio filter chain "
1710 "pre-init!\n");
1711 exit_player(mpctx, EXIT_ERROR);
1713 if (!ao->initialized) {
1714 current_module="ao2_init";
1715 ao->buffersize = opts->ao_buffersize;
1716 ao_init(ao, opts->audio_driver_list);
1717 if (!ao->initialized) {
1718 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Could not open/initialize audio device -> no sound.\n");
1719 goto init_error;
1721 mp_msg(MSGT_CPLAYER,MSGL_INFO,"AO: [%s] %dHz %dch %s (%d bytes per sample)\n",
1722 ao->driver->info->short_name,
1723 ao->samplerate, ao->channels,
1724 af_fmt2str_short(ao->format),
1725 af_fmt2bits(ao->format)/8 );
1726 mp_msg(MSGT_CPLAYER,MSGL_V,"AO: Description: %s\nAO: Author: %s\n",
1727 ao->driver->info->name, ao->driver->info->author);
1728 if (strlen(ao->driver->info->comment) > 0)
1729 mp_msg(MSGT_CPLAYER, MSGL_V, "AO: Comment: %s\n",
1730 ao->driver->info->comment);
1733 // init audio filters:
1734 current_module="af_init";
1735 if (!build_afilter_chain(mpctx)) {
1736 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter/ao format!\n");
1737 goto init_error;
1739 mpctx->mixer.ao = ao;
1740 mpctx->mixer.volstep = volstep;
1741 mpctx->syncing_audio = true;
1742 return;
1744 init_error:
1745 uninit_player(mpctx, INITIALIZED_ACODEC|INITIALIZED_AO); // close codec and possibly AO
1746 mpctx->sh_audio=mpctx->d_audio->sh=NULL; // -> nosound
1747 mpctx->d_audio->id = -2;
1751 ///@}
1752 // Command2Property
1755 // Return pts value corresponding to the end point of audio written to the
1756 // ao so far.
1757 static double written_audio_pts(struct MPContext *mpctx)
1759 sh_audio_t *sh_audio = mpctx->sh_audio;
1760 demux_stream_t *d_audio = mpctx->d_audio;
1761 double buffered_output;
1762 // first calculate the end pts of audio that has been output by decoder
1763 double a_pts = sh_audio->pts;
1764 if (a_pts != MP_NOPTS_VALUE)
1765 // Good, decoder supports new way of calculating audio pts.
1766 // sh_audio->pts is the timestamp of the latest input packet with
1767 // known pts that the decoder has decoded. sh_audio->pts_bytes is
1768 // the amount of bytes the decoder has written after that timestamp.
1769 a_pts += sh_audio->pts_bytes / (double) sh_audio->o_bps;
1770 else {
1771 // Decoder doesn't support new way of calculating pts (or we're
1772 // being called before it has decoded anything with known timestamp).
1773 // Use the old method of audio pts calculation: take the timestamp
1774 // of last packet with known pts the decoder has read data from,
1775 // and add amount of bytes read after the beginning of that packet
1776 // divided by input bps. This will be inaccurate if the input/output
1777 // ratio is not constant for every audio packet or if it is constant
1778 // but not accurately known in sh_audio->i_bps.
1780 a_pts = d_audio->pts;
1781 // ds_tell_pts returns bytes read after last timestamp from
1782 // demuxing layer, decoder might use sh_audio->a_in_buffer for bytes
1783 // it has read but not decoded
1784 if (sh_audio->i_bps)
1785 a_pts += (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len) /
1786 (double)sh_audio->i_bps;
1788 // Now a_pts hopefully holds the pts for end of audio from decoder.
1789 // Substract data in buffers between decoder and audio out.
1791 // Decoded but not filtered
1792 a_pts -= sh_audio->a_buffer_len / (double)sh_audio->o_bps;
1794 // Data buffered in audio filters, measured in bytes of "missing" output
1795 buffered_output = af_calc_delay(sh_audio->afilter);
1797 // Data that was ready for ao but was buffered because ao didn't fully
1798 // accept everything to internal buffers yet
1799 buffered_output += sh_audio->a_out_buffer_len;
1801 // Filters divide audio length by playback_speed, so multiply by it
1802 // to get the length in original units without speedup or slowdown
1803 a_pts -= buffered_output * mpctx->opts.playback_speed / mpctx->ao->bps;
1805 return a_pts + mpctx->video_offset;
1808 // Return pts value corresponding to currently playing audio.
1809 double playing_audio_pts(struct MPContext *mpctx)
1811 return written_audio_pts(mpctx) - mpctx->opts.playback_speed *
1812 ao_get_delay(mpctx->ao);
1815 static bool is_av_sub(int type)
1817 return type == 'b' || type == 'p' || type == 'x';
1820 void update_subtitles(struct MPContext *mpctx, double refpts,
1821 double sub_offset, bool reset)
1823 struct MPOpts *opts = &mpctx->opts;
1824 struct sh_video *sh_video = mpctx->sh_video;
1825 struct demux_stream *d_sub = mpctx->d_sub;
1826 double curpts = refpts + sub_delay;
1827 unsigned char *packet=NULL;
1828 int len;
1829 struct sh_sub *sh_sub = d_sub->sh;
1830 int type = sh_sub ? sh_sub->type : 'v';
1831 static subtitle subs;
1832 if (reset) {
1833 if (sh_sub)
1834 sub_reset(sh_sub, mpctx->osd);
1835 sub_clear_text(&subs, MP_NOPTS_VALUE);
1836 if (vo_sub)
1837 set_osd_subtitle(mpctx, NULL);
1838 if (vo_spudec) {
1839 spudec_reset(vo_spudec);
1840 vo_osd_changed(OSDTYPE_SPU);
1842 #ifdef CONFIG_FFMPEG
1843 if (is_av_sub(type))
1844 reset_avsub(sh_sub);
1845 #endif
1846 return;
1848 // find sub
1849 if (mpctx->subdata) {
1850 if (sub_fps==0) sub_fps = sh_video ? sh_video->fps : 25;
1851 current_module = "find_sub";
1852 find_sub(mpctx, mpctx->subdata, curpts *
1853 (mpctx->subdata->sub_uses_time ? 100. : sub_fps));
1854 if (vo_sub)
1855 mpctx->vo_sub_last = vo_sub;
1858 // DVD sub:
1859 if (vobsub_id >= 0 || type == 'v') {
1860 int timestamp;
1861 current_module = "spudec";
1862 /* Get a sub packet from the DVD or a vobsub */
1863 while(1) {
1864 // Vobsub
1865 len = 0;
1866 if (vo_vobsub) {
1867 if (curpts >= 0) {
1868 len = vobsub_get_packet(vo_vobsub, curpts,
1869 (void**)&packet, &timestamp);
1870 if (len > 0) {
1871 mp_dbg(MSGT_CPLAYER,MSGL_V,"\rVOB sub: len=%d v_pts=%5.3f v_timer=%5.3f sub=%5.3f ts=%d \n",len,refpts,sh_video->timer,timestamp / 90000.0,timestamp);
1874 } else {
1875 // DVD sub
1876 len = ds_get_packet_sub(d_sub, (unsigned char**)&packet);
1877 if (len > 0) {
1878 // XXX This is wrong, sh_video->pts can be arbitrarily
1879 // much behind demuxing position. Unfortunately using
1880 // d_video->pts which would have been the simplest
1881 // improvement doesn't work because mpeg specific hacks
1882 // in video.c set d_video->pts to 0.
1883 float x = d_sub->pts - refpts;
1884 if (x > -20 && x < 20) // prevent missing subs on pts reset
1885 timestamp = 90000*d_sub->pts;
1886 else timestamp = 90000*curpts;
1887 mp_dbg(MSGT_CPLAYER, MSGL_V, "\rDVD sub: len=%d "
1888 "v_pts=%5.3f s_pts=%5.3f ts=%d \n", len,
1889 refpts, d_sub->pts, timestamp);
1892 if (len<=0 || !packet) break;
1893 // create it only here, since with some broken demuxers we might
1894 // type = v but no DVD sub and we currently do not change the
1895 // "original frame size" ever after init, leading to wrong-sized
1896 // PGS subtitles.
1897 if (!vo_spudec)
1898 vo_spudec = spudec_new(NULL);
1899 if (vo_vobsub || timestamp >= 0)
1900 spudec_assemble(vo_spudec, packet, len, timestamp);
1902 } else if (is_text_sub(type) || is_av_sub(type) || type == 'd') {
1903 if (type == 'd' && !d_sub->demuxer->teletext) {
1904 tt_stream_props tsp = {0};
1905 void *ptr = &tsp;
1906 if (teletext_control(NULL, TV_VBI_CONTROL_START, &ptr) == VBI_CONTROL_TRUE)
1907 d_sub->demuxer->teletext = ptr;
1909 if (d_sub->non_interleaved)
1910 ds_get_next_pts(d_sub);
1912 while (d_sub->first) {
1913 double subpts = ds_get_next_pts(d_sub) + sub_offset;
1914 if (subpts > curpts) {
1915 // Libass handled subs can be fed to it in advance
1916 if (!opts->ass_enabled || !is_text_sub(type))
1917 break;
1918 // Try to avoid demuxing whole file at once
1919 if (d_sub->non_interleaved && subpts > curpts + 1)
1920 break;
1922 double duration = d_sub->first->duration;
1923 len = ds_get_packet_sub(d_sub, &packet);
1924 if (is_av_sub(type)) {
1925 #ifdef CONFIG_FFMPEG
1926 int ret = decode_avsub(sh_sub, packet, len, subpts, duration);
1927 if (ret < 0)
1928 mp_msg(MSGT_SPUDEC, MSGL_WARN, "lavc failed decoding "
1929 "subtitle\n");
1930 #endif
1931 continue;
1933 if (type == 'm') {
1934 if (len < 2) continue;
1935 len = FFMIN(len - 2, AV_RB16(packet));
1936 packet += 2;
1938 if (type == 'd') {
1939 if (d_sub->demuxer->teletext) {
1940 uint8_t *p = packet;
1941 p++;
1942 len--;
1943 while (len >= 46) {
1944 int sublen = p[1];
1945 if (p[0] == 2 || p[0] == 3)
1946 teletext_control(d_sub->demuxer->teletext,
1947 TV_VBI_CONTROL_DECODE_DVB, p + 2);
1948 p += sublen + 2;
1949 len -= sublen + 2;
1952 continue;
1954 if (sh_sub && sh_sub->active) {
1955 sub_decode(sh_sub, mpctx->osd, packet, len, subpts, duration);
1956 continue;
1958 if (subpts != MP_NOPTS_VALUE) {
1959 if (duration < 0)
1960 sub_clear_text(&subs, MP_NOPTS_VALUE);
1961 if (type == 'a') { // ssa/ass subs without libass => convert to plaintext
1962 int i;
1963 unsigned char* p = packet;
1964 for (i=0; i < 8 && *p != '\0'; p++)
1965 if (*p == ',')
1966 i++;
1967 if (*p == '\0') /* Broken line? */
1968 continue;
1969 len -= p - packet;
1970 packet = p;
1972 double endpts = MP_NOPTS_VALUE;
1973 if (subpts != MP_NOPTS_VALUE && duration >= 0)
1974 endpts = subpts + duration;
1975 sub_add_text(&subs, packet, len, endpts);
1976 set_osd_subtitle(mpctx, &subs);
1978 if (d_sub->non_interleaved)
1979 ds_get_next_pts(d_sub);
1981 if (!opts->ass_enabled)
1982 if (sub_clear_text(&subs, curpts))
1983 set_osd_subtitle(mpctx, &subs);
1985 if (vo_spudec) {
1986 spudec_heartbeat(vo_spudec, 90000*curpts);
1987 if (spudec_changed(vo_spudec))
1988 vo_osd_changed(OSDTYPE_SPU);
1991 current_module=NULL;
1994 static void update_teletext(sh_video_t *sh_video, demuxer_t *demuxer, int reset)
1996 int page_changed;
1998 if (!demuxer->teletext)
1999 return;
2001 //Also forcing page update when such ioctl is not supported or call error occured
2002 if(teletext_control(demuxer->teletext,TV_VBI_CONTROL_IS_CHANGED,&page_changed)!=VBI_CONTROL_TRUE)
2003 page_changed=1;
2005 if(!page_changed)
2006 return;
2008 if(teletext_control(demuxer->teletext,TV_VBI_CONTROL_GET_VBIPAGE,&vo_osd_teletext_page)!=VBI_CONTROL_TRUE)
2009 vo_osd_teletext_page=NULL;
2010 if(teletext_control(demuxer->teletext,TV_VBI_CONTROL_GET_HALF_PAGE,&vo_osd_teletext_half)!=VBI_CONTROL_TRUE)
2011 vo_osd_teletext_half=0;
2012 if(teletext_control(demuxer->teletext,TV_VBI_CONTROL_GET_MODE,&vo_osd_teletext_mode)!=VBI_CONTROL_TRUE)
2013 vo_osd_teletext_mode=0;
2014 if(teletext_control(demuxer->teletext,TV_VBI_CONTROL_GET_FORMAT,&vo_osd_teletext_format)!=VBI_CONTROL_TRUE)
2015 vo_osd_teletext_format=0;
2016 vo_osd_changed(OSDTYPE_TELETEXT);
2018 teletext_control(demuxer->teletext,TV_VBI_CONTROL_MARK_UNCHANGED,NULL);
2021 static int check_framedrop(struct MPContext *mpctx, double frame_time) {
2022 struct MPOpts *opts = &mpctx->opts;
2023 // check for frame-drop:
2024 current_module = "check_framedrop";
2025 if (mpctx->sh_audio && !mpctx->d_audio->eof) {
2026 static int dropped_frames;
2027 float delay = opts->playback_speed * ao_get_delay(mpctx->ao);
2028 float d = delay-mpctx->delay;
2029 ++total_frame_cnt;
2030 // we should avoid dropping too many frames in sequence unless we
2031 // are too late. and we allow 100ms A-V delay here:
2032 if (d < -dropped_frames*frame_time-0.100 && !mpctx->paused
2033 && !mpctx->restart_playback) {
2034 ++drop_frame_cnt;
2035 ++dropped_frames;
2036 return frame_dropping;
2037 } else
2038 dropped_frames = 0;
2040 return 0;
2044 #ifdef HAVE_RTC
2045 int rtc_fd = -1;
2046 #endif
2048 static float timing_sleep(struct MPContext *mpctx, float time_frame)
2050 #ifdef HAVE_RTC
2051 if (rtc_fd >= 0){
2052 // -------- RTC -----------
2053 current_module="sleep_rtc";
2054 while (time_frame > 0.000) {
2055 unsigned long rtc_ts;
2056 if (read(rtc_fd, &rtc_ts, sizeof(rtc_ts)) <= 0)
2057 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC read error: %s\n", strerror(errno));
2058 time_frame -= get_relative_time(mpctx);
2060 } else
2061 #endif
2063 // assume kernel HZ=100 for softsleep, works with larger HZ but with
2064 // unnecessarily high CPU usage
2065 struct MPOpts *opts = &mpctx->opts;
2066 float margin = opts->softsleep ? 0.011 : 0;
2067 current_module = "sleep_timer";
2068 while (time_frame > margin) {
2069 usec_sleep(1000000 * (time_frame - margin));
2070 time_frame -= get_relative_time(mpctx);
2072 if (opts->softsleep){
2073 current_module = "sleep_soft";
2074 if (time_frame < 0)
2075 mp_tmsg(MSGT_AVSYNC, MSGL_WARN, "Warning! Softsleep underflow!\n");
2076 while (time_frame > 0)
2077 time_frame -= get_relative_time(mpctx); // burn the CPU
2080 return time_frame;
2083 static int select_subtitle(MPContext *mpctx)
2085 struct MPOpts *opts = &mpctx->opts;
2086 // find the best sub to use
2087 int id;
2088 int found = 0;
2089 mpctx->global_sub_pos = -1; // no subs by default
2090 if (vobsub_id >= 0) {
2091 // if user asks for a vobsub id, use that first.
2092 id = vobsub_id;
2093 found = mp_property_do("sub_vob", M_PROPERTY_SET, &id, mpctx) == M_PROPERTY_OK;
2096 if (!found && opts->sub_id >= 0) {
2097 // if user asks for a dvd sub id, use that next.
2098 id = opts->sub_id;
2099 found = mp_property_do("sub_demux", M_PROPERTY_SET, &id, mpctx) == M_PROPERTY_OK;
2102 if (!found) {
2103 // if there are text subs to use, use those. (autosubs come last here)
2104 id = 0;
2105 found = mp_property_do("sub_file", M_PROPERTY_SET, &id, mpctx) == M_PROPERTY_OK;
2108 if (!found && opts->sub_id == -1) {
2109 // finally select subs by language and container hints
2110 if (opts->sub_id == -1)
2111 opts->sub_id =
2112 demuxer_sub_track_by_lang_and_default(mpctx->d_sub->demuxer,
2113 opts->sub_lang);
2114 if (opts->sub_id >= 0) {
2115 id = opts->sub_id;
2116 found = mp_property_do("sub_demux", M_PROPERTY_SET, &id, mpctx) == M_PROPERTY_OK;
2119 return found;
2122 #ifdef CONFIG_DVDNAV
2123 #ifndef FF_B_TYPE
2124 #define FF_B_TYPE 3
2125 #endif
2126 /// store decoded video image
2127 static mp_image_t * mp_dvdnav_copy_mpi(mp_image_t *to_mpi,
2128 mp_image_t *from_mpi) {
2129 mp_image_t *mpi;
2131 /// Do not store B-frames
2132 if (from_mpi->pict_type == FF_B_TYPE)
2133 return to_mpi;
2135 if (to_mpi &&
2136 to_mpi->w == from_mpi->w &&
2137 to_mpi->h == from_mpi->h &&
2138 to_mpi->imgfmt == from_mpi->imgfmt)
2139 mpi = to_mpi;
2140 else {
2141 if (to_mpi)
2142 free_mp_image(to_mpi);
2143 if (from_mpi->w == 0 || from_mpi->h == 0)
2144 return NULL;
2145 mpi = alloc_mpi(from_mpi->w,from_mpi->h,from_mpi->imgfmt);
2148 copy_mpi(mpi,from_mpi);
2149 return mpi;
2152 static void mp_dvdnav_reset_stream (MPContext *ctx) {
2153 struct MPOpts *opts = &ctx->opts;
2154 if (ctx->sh_video) {
2155 /// clear video pts
2156 ctx->d_video->pts = 0.0f;
2157 ctx->sh_video->pts = 0.0f;
2158 ctx->sh_video->i_pts = 0.0f;
2159 ctx->sh_video->last_pts = 0.0f;
2160 ctx->sh_video->num_buffered_pts = 0;
2161 ctx->sh_video->num_frames = 0;
2162 ctx->sh_video->num_frames_decoded = 0;
2163 ctx->sh_video->timer = 0.0f;
2164 ctx->sh_video->stream_delay = 0.0f;
2165 ctx->sh_video->timer = 0;
2166 ctx->demuxer->stream_pts = MP_NOPTS_VALUE;
2169 if (ctx->sh_audio) {
2170 /// free audio packets and reset
2171 ds_free_packs(ctx->d_audio);
2172 audio_delay -= ctx->sh_audio->stream_delay;
2173 ctx->delay =- audio_delay;
2174 ao_reset(ctx->ao);
2175 resync_audio_stream(ctx->sh_audio);
2178 audio_delay = 0.0f;
2179 ctx->sub_counts[SUB_SOURCE_DEMUX] = mp_dvdnav_number_of_subs(ctx->stream);
2180 if (opts->sub_lang && opts->sub_id == dvdsub_lang_id) {
2181 dvdsub_lang_id = mp_dvdnav_sid_from_lang(ctx->stream, opts->sub_lang);
2182 if (dvdsub_lang_id != opts->sub_id) {
2183 opts->sub_id = dvdsub_lang_id;
2184 select_subtitle(ctx);
2188 /// clear all EOF related flags
2189 ctx->d_video->eof = ctx->d_audio->eof = ctx->stream->eof = 0;
2192 /// Restore last decoded DVDNAV (still frame)
2193 static mp_image_t *mp_dvdnav_restore_smpi(struct MPContext *mpctx,
2194 int *in_size,
2195 unsigned char **start,
2196 mp_image_t *decoded_frame)
2198 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
2199 return decoded_frame;
2201 /// a change occured in dvdnav stream
2202 if (mp_dvdnav_cell_has_changed(mpctx->stream,0)) {
2203 mp_dvdnav_read_wait(mpctx->stream, 1, 1);
2204 mp_dvdnav_context_free(mpctx);
2205 mp_dvdnav_reset_stream(mpctx);
2206 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
2207 mp_dvdnav_cell_has_changed(mpctx->stream,1);
2210 if (*in_size < 0) {
2211 float len;
2213 /// Display still frame, if any
2214 if (mpctx->nav_smpi && !mpctx->nav_buffer)
2215 decoded_frame = mpctx->nav_smpi;
2217 /// increment video frame : continue playing after still frame
2218 len = get_time_length(mpctx);
2219 if (mpctx->sh_video->pts >= len &&
2220 mpctx->sh_video->pts > 0.0 && len > 0.0) {
2221 mp_dvdnav_skip_still(mpctx->stream);
2222 mp_dvdnav_skip_wait(mpctx->stream);
2224 mpctx->sh_video->pts += 1 / mpctx->sh_video->fps;
2226 if (mpctx->nav_buffer) {
2227 *start = mpctx->nav_start;
2228 *in_size = mpctx->nav_in_size;
2229 if (mpctx->nav_start)
2230 memcpy(*start,mpctx->nav_buffer,mpctx->nav_in_size);
2234 return decoded_frame;
2237 /// Save last decoded DVDNAV (still frame)
2238 static void mp_dvdnav_save_smpi(struct MPContext *mpctx, int in_size,
2239 unsigned char *start,
2240 mp_image_t *decoded_frame)
2242 if (mpctx->stream->type != STREAMTYPE_DVDNAV)
2243 return;
2245 free(mpctx->nav_buffer);
2247 mpctx->nav_buffer = malloc(in_size);
2248 mpctx->nav_start = start;
2249 mpctx->nav_in_size = mpctx->nav_buffer ? in_size : -1;
2250 if (mpctx->nav_buffer)
2251 memcpy(mpctx->nav_buffer,start,in_size);
2253 if (decoded_frame && mpctx->nav_smpi != decoded_frame)
2254 mpctx->nav_smpi = mp_dvdnav_copy_mpi(mpctx->nav_smpi,decoded_frame);
2256 #endif /* CONFIG_DVDNAV */
2258 /* Modify video timing to match the audio timeline. There are two main
2259 * reasons this is needed. First, video and audio can start from different
2260 * positions at beginning of file or after a seek (MPlayer starts both
2261 * immediately even if they have different pts). Second, the file can have
2262 * audio timestamps that are inconsistent with the duration of the audio
2263 * packets, for example two consecutive timestamp values differing by
2264 * one second but only a packet with enough samples for half a second
2265 * of playback between them.
2267 static void adjust_sync(struct MPContext *mpctx, double frame_time)
2269 current_module = "av_sync";
2271 if (!mpctx->sh_audio || mpctx->syncing_audio)
2272 return;
2274 double a_pts = written_audio_pts(mpctx) - mpctx->delay;
2275 double v_pts = mpctx->sh_video->pts;
2276 double av_delay = a_pts - v_pts;
2277 // Try to sync vo_flip() so it will *finish* at given time
2278 av_delay += mpctx->last_vo_flip_duration;
2279 av_delay -= audio_delay; // This much pts difference is desired
2281 double change = av_delay * 0.1;
2282 double max_change = default_max_pts_correction >= 0 ?
2283 default_max_pts_correction : frame_time * 0.1;
2284 if (change < -max_change)
2285 change = -max_change;
2286 else if (change > max_change)
2287 change = max_change;
2288 mpctx->delay += change;
2289 mpctx->total_avsync_change += change;
2292 #define ASYNC_PLAY_DONE -3
2293 static int audio_start_sync(struct MPContext *mpctx, int playsize)
2295 struct ao *ao = mpctx->ao;
2296 struct MPOpts *opts = &mpctx->opts;
2297 sh_audio_t * const sh_audio = mpctx->sh_audio;
2298 int res;
2300 // Timing info may not be set without
2301 res = decode_audio(sh_audio, 1);
2302 if (res < 0)
2303 return res;
2305 int bytes;
2306 bool did_retry = false;
2307 while (1) {
2308 double written_pts = written_audio_pts(mpctx);
2309 double ptsdiff = written_pts - mpctx->video_pts - mpctx->delay
2310 - audio_delay;
2311 bytes = ptsdiff * ao->bps / mpctx->opts.playback_speed;
2312 bytes -= bytes % (ao->channels * af_fmt2bits(ao->format) / 8);
2314 // ogg demuxers give packets without timing
2315 if (written_pts <= 1 && sh_audio->pts == MP_NOPTS_VALUE) {
2316 if (!did_retry) {
2317 // Try to read more data to see packets that have pts
2318 int res = decode_audio(sh_audio, ao->bps);
2319 if (res < 0)
2320 return res;
2321 did_retry = true;
2322 continue;
2324 bytes = 0;
2327 if (fabs(ptsdiff) > 300) // pts reset or just broken?
2328 bytes = 0;
2330 if (bytes > 0)
2331 break;
2333 mpctx->syncing_audio = false;
2334 int a = FFMIN(-bytes, FFMAX(playsize, 20000));
2335 int res = decode_audio(sh_audio, a);
2336 bytes += sh_audio->a_out_buffer_len;
2337 if (bytes >= 0) {
2338 memmove(sh_audio->a_out_buffer,
2339 sh_audio->a_out_buffer +
2340 sh_audio->a_out_buffer_len - bytes,
2341 bytes);
2342 sh_audio->a_out_buffer_len = bytes;
2343 if (res < 0)
2344 return res;
2345 return decode_audio(sh_audio, playsize);
2347 sh_audio->a_out_buffer_len = 0;
2348 if (res < 0)
2349 return res;
2351 int fillbyte = 0;
2352 if ((ao->format & AF_FORMAT_SIGN_MASK) == AF_FORMAT_US)
2353 fillbyte = 0x80;
2354 if (bytes >= playsize) {
2355 /* This case could fall back to the one below with
2356 * bytes = playsize, but then silence would keep accumulating
2357 * in a_out_buffer if the AO accepts less data than it asks for
2358 * in playsize. */
2359 char *p = malloc(playsize);
2360 memset(p, fillbyte, playsize);
2361 playsize = ao_play(ao, p, playsize, 0);
2362 free(p);
2363 mpctx->delay += opts->playback_speed*playsize/(double)ao->bps;
2364 return ASYNC_PLAY_DONE;
2366 mpctx->syncing_audio = false;
2367 decode_audio_prepend_bytes(sh_audio, bytes, fillbyte);
2368 return decode_audio(sh_audio, playsize);
2371 static int fill_audio_out_buffers(struct MPContext *mpctx)
2373 struct MPOpts *opts = &mpctx->opts;
2374 struct ao *ao = mpctx->ao;
2375 unsigned int t;
2376 double tt;
2377 int playsize;
2378 int playflags=0;
2379 bool audio_eof = false;
2380 bool partial_fill = false;
2381 bool format_change = false;
2382 sh_audio_t * const sh_audio = mpctx->sh_audio;
2383 bool modifiable_audio_format = !(ao->format & AF_FORMAT_SPECIAL_MASK);
2384 int unitsize = ao->channels * af_fmt2bits(ao->format) / 8;
2386 current_module="play_audio";
2388 // all the current uses of ao->pts seem to be in aos that handle
2389 // sync completely wrong; there should be no need to use ao->pts
2390 // in get_space()
2391 ao->pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2392 playsize = ao_get_space(ao);
2394 // Fill buffer if needed:
2395 current_module="decode_audio";
2396 t = GetTimer();
2398 if (!opts->initial_audio_sync || !modifiable_audio_format)
2399 mpctx->syncing_audio = false;
2401 int res;
2402 if (mpctx->syncing_audio && mpctx->sh_video)
2403 res = audio_start_sync(mpctx, playsize);
2404 else
2405 res = decode_audio(sh_audio, playsize);
2406 if (res < 0) { // EOF, error or format change
2407 if (res == -2)
2408 format_change = true;
2409 else if (res == ASYNC_PLAY_DONE)
2410 return 0;
2411 else if (mpctx->d_audio->eof)
2412 audio_eof = true;
2414 t = GetTimer() - t;
2415 tt = t*0.000001f; audio_time_usage+=tt;
2416 if (mpctx->timeline && modifiable_audio_format) {
2417 double endpts = mpctx->timeline[mpctx->timeline_part + 1].start;
2418 double bytes = (endpts - written_audio_pts(mpctx) + audio_delay)
2419 * ao->bps / opts->playback_speed;
2420 if (playsize > bytes) {
2421 playsize = FFMAX(bytes, 0);
2422 playflags |= AOPLAY_FINAL_CHUNK;
2423 audio_eof = true;
2424 partial_fill = true;
2428 if (playsize > sh_audio->a_out_buffer_len) {
2429 partial_fill = true;
2430 playsize = sh_audio->a_out_buffer_len;
2431 if (audio_eof)
2432 playflags |= AOPLAY_FINAL_CHUNK;
2434 playsize -= playsize % unitsize;
2435 if (!playsize)
2436 return partial_fill && audio_eof ? -2 : -partial_fill;
2438 // play audio:
2439 current_module="play_audio";
2441 // Is this pts value actually useful for the aos that access it?
2442 // They're obviously badly broken in the way they handle av sync;
2443 // would not having access to this make them more broken?
2444 ao->pts = ((mpctx->sh_video?mpctx->sh_video->timer:0)+mpctx->delay)*90000.0;
2445 playsize = ao_play(ao, sh_audio->a_out_buffer, playsize, playflags);
2447 if (playsize > 0) {
2448 sh_audio->a_out_buffer_len -= playsize;
2449 memmove(sh_audio->a_out_buffer, &sh_audio->a_out_buffer[playsize],
2450 sh_audio->a_out_buffer_len);
2451 mpctx->delay += opts->playback_speed*playsize/(double)ao->bps;
2452 } else if (audio_eof && ao_get_delay(ao) < .04) {
2453 // Sanity check to avoid hanging in case current ao doesn't output
2454 // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK
2455 mp_msg(MSGT_CPLAYER, MSGL_WARN, "Audio output truncated at end.\n");
2456 sh_audio->a_out_buffer_len = 0;
2459 /* The format change isn't handled too gracefully. A more precise
2460 * implementation would require draining buffered old-format audio
2461 * while displaying video, then doing the output format switch.
2463 if (format_change) {
2464 uninit_player(mpctx, INITIALIZED_AO);
2465 reinit_audio_chain(mpctx);
2466 return -1;
2469 return -partial_fill;
2472 static int sleep_until_near_frame(struct MPContext *mpctx, float *time_frame,
2473 bool sync_to_audio, float *aq_sleep_time)
2475 struct MPOpts *opts = &mpctx->opts;
2476 double audio_limit = 2;
2477 current_module="calc_sleep_time";
2479 if (mpctx->restart_playback)
2480 return 0;
2482 *time_frame -= get_relative_time(mpctx); // reset timer
2484 if (sync_to_audio) {
2485 float delay = ao_get_delay(mpctx->ao);
2486 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "delay=%f\n", delay);
2488 if (opts->autosync) {
2490 * Adjust this raw delay value by calculating the expected
2491 * delay for this frame and generating a new value which is
2492 * weighted between the two. The higher autosync is, the
2493 * closer to the delay value gets to that which "-nosound"
2494 * would have used, and the longer it will take for A/V
2495 * sync to settle at the right value (but it eventually will.)
2496 * This settling time is very short for values below 100.
2498 float predicted = mpctx->delay / opts->playback_speed + *time_frame;
2499 float difference = delay - predicted;
2500 delay = predicted + difference / (float)opts->autosync;
2503 *time_frame = delay - mpctx->delay / opts->playback_speed;
2505 // delay = amount of audio buffered in soundcard/driver
2506 delay = FFMIN(delay, 0.5);
2507 delay = FFMAX(delay, 0.1);
2508 audio_limit = delay;
2509 } else {
2510 // If we're lagging more than 200 ms behind the right playback rate,
2511 // don't try to "catch up".
2512 // If benchmark is set always output frames as fast as possible
2513 // without sleeping.
2514 if (*time_frame < -0.2 || opts->benchmark)
2515 *time_frame = 0;
2518 double t = *time_frame - mpctx->video_out->flip_queue_offset;
2520 if (t <= 0.05)
2521 return 0;
2523 t -= 0.05;
2524 if (t > audio_limit * 0.6)
2525 t = audio_limit * 0.5;
2526 *aq_sleep_time += t;
2527 mp_input_get_cmd(mpctx->input, t * 1000 + 1, 1);
2528 return 1;
2531 int reinit_video_chain(struct MPContext *mpctx)
2533 struct MPOpts *opts = &mpctx->opts;
2534 sh_video_t * const sh_video = mpctx->sh_video;
2535 if (!sh_video)
2536 return 0;
2537 double ar=-1.0;
2538 //================== Init VIDEO (codec & libvo) ==========================
2539 if (!opts->fixed_vo || !(mpctx->initialized_flags & INITIALIZED_VO)) {
2540 current_module="preinit_libvo";
2542 //shouldn't we set dvideo->id=-2 when we fail?
2543 //if((mpctx->video_out->preinit(vo_subdevice))!=0){
2544 if(!(mpctx->video_out=init_best_video_out(opts, mpctx->x11_state, mpctx->key_fifo, mpctx->input))){
2545 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Error opening/initializing the selected video_out (-vo) device.\n");
2546 goto err_out;
2548 mpctx->initialized_flags|=INITIALIZED_VO;
2551 if(stream_control(mpctx->demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) != STREAM_UNSUPPORTED)
2552 mpctx->sh_video->stream_aspect = ar;
2553 current_module="init_video_filters";
2555 char* vf_arg[] = { "_oldargs_", (char*)mpctx->video_out , NULL };
2556 sh_video->vfilter = vf_open_filter(opts, NULL,"vo",vf_arg);
2558 #ifdef CONFIG_MENU
2559 if(use_menu) {
2560 char* vf_arg[] = { "_oldargs_", menu_root, NULL };
2561 vf_menu = vf_open_plugin(opts,libmenu_vfs,sh_video->vfilter,"menu",vf_arg);
2562 if(!vf_menu) {
2563 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Can't open libmenu video filter with root menu %s.\n",menu_root);
2564 use_menu = 0;
2567 if(vf_menu)
2568 sh_video->vfilter = vf_menu;
2569 #endif
2571 #ifdef CONFIG_ASS
2572 if(opts->ass_enabled) {
2573 int i;
2574 int insert = 1;
2575 if (opts->vf_settings)
2576 for (i = 0; opts->vf_settings[i].name; ++i)
2577 if (strcmp(opts->vf_settings[i].name, "ass") == 0) {
2578 insert = 0;
2579 break;
2581 if (insert) {
2582 extern vf_info_t vf_info_ass;
2583 const vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
2584 char* vf_arg[] = {"auto", "1", NULL};
2585 int retcode = 0;
2586 struct vf_instance *vf_ass = vf_open_plugin_noerr(opts, libass_vfs,
2587 sh_video->vfilter,
2588 "ass", vf_arg,
2589 &retcode);
2590 if (vf_ass)
2591 sh_video->vfilter = vf_ass;
2592 else if (retcode == -1) // vf_ass open() returns -1 if there's VO EOSD
2593 mp_msg(MSGT_CPLAYER, MSGL_V, "[ass] vf_ass not needed\n");
2594 else
2595 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
2598 #endif
2600 sh_video->vfilter = append_filters(sh_video->vfilter, opts->vf_settings);
2602 #ifdef CONFIG_ASS
2603 if (opts->ass_enabled)
2604 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
2605 #endif
2607 current_module="init_video_codec";
2609 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2610 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
2611 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
2613 if(!sh_video->initialized){
2614 if(!opts->fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
2615 goto err_out;
2618 mpctx->initialized_flags|=INITIALIZED_VCODEC;
2620 if (sh_video->codec)
2621 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODEC=%s\n", sh_video->codec->name);
2623 sh_video->last_pts = MP_NOPTS_VALUE;
2624 sh_video->num_buffered_pts = 0;
2625 sh_video->next_frame_time = 0;
2627 if (opts->auto_quality > 0) {
2628 // Auto quality option enabled
2629 output_quality=get_video_quality_max(sh_video);
2630 if (opts->auto_quality > output_quality)
2631 opts->auto_quality = output_quality;
2632 else
2633 output_quality = opts->auto_quality;
2634 mp_msg(MSGT_CPLAYER,MSGL_V,"AutoQ: setting quality to %d.\n",output_quality);
2635 set_video_quality(sh_video,output_quality);
2638 // ========== Init display (sh_video->disp_w*sh_video->disp_h/out_fmt) ============
2640 current_module="init_vo";
2642 return 1;
2644 err_out:
2645 mpctx->sh_video = mpctx->d_video->sh = NULL;
2646 return 0;
2649 static double update_video_nocorrect_pts(struct MPContext *mpctx)
2651 struct sh_video *sh_video = mpctx->sh_video;
2652 double frame_time = 0;
2653 struct vo *video_out = mpctx->video_out;
2654 while (1) {
2655 current_module = "filter_video";
2656 // In nocorrect-pts mode there is no way to properly time these frames
2657 if (vo_get_buffered_frame(video_out, 0) >= 0)
2658 break;
2659 if (vf_output_queued_frame(sh_video->vfilter))
2660 break;
2661 unsigned char *packet = NULL;
2662 frame_time = sh_video->next_frame_time;
2663 if (mpctx->restart_playback)
2664 frame_time = 0;
2665 int in_size = video_read_frame(sh_video, &sh_video->next_frame_time,
2666 &packet, force_fps);
2667 if (in_size < 0) {
2668 #ifdef CONFIG_DVDNAV
2669 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
2670 if (mp_dvdnav_is_eof(mpctx->stream))
2671 return -1;
2672 if (mpctx->d_video)
2673 mpctx->d_video->eof = 0;
2674 if (mpctx->d_audio)
2675 mpctx->d_audio->eof = 0;
2676 mpctx->stream->eof = 0;
2677 } else
2678 #endif
2679 return -1;
2681 if (in_size > max_framesize)
2682 max_framesize = in_size;
2683 sh_video->timer += frame_time;
2684 if (mpctx->sh_audio)
2685 mpctx->delay -= frame_time;
2686 // video_read_frame can change fps (e.g. for ASF video)
2687 vo_fps = sh_video->fps;
2688 int framedrop_type = check_framedrop(mpctx, frame_time);
2689 current_module = "decode video";
2691 void *decoded_frame;
2692 #ifdef CONFIG_DVDNAV
2693 decoded_frame = mp_dvdnav_restore_smpi(mpctx, &in_size, &packet, NULL);
2694 if (in_size >= 0 && !decoded_frame)
2695 #endif
2696 decoded_frame = decode_video(sh_video, packet, in_size, framedrop_type,
2697 sh_video->pts);
2698 #ifdef CONFIG_DVDNAV
2699 // Save last still frame for future display
2700 mp_dvdnav_save_smpi(mpctx, in_size, packet, decoded_frame);
2701 #endif
2702 if (decoded_frame) {
2703 current_module = "filter video";
2704 filter_video(sh_video, decoded_frame, sh_video->pts);
2706 break;
2708 return frame_time;
2711 static void determine_frame_pts(struct MPContext *mpctx)
2713 struct sh_video *sh_video = mpctx->sh_video;
2714 struct MPOpts *opts = &mpctx->opts;
2716 if (opts->user_pts_assoc_mode) {
2717 sh_video->pts_assoc_mode = opts->user_pts_assoc_mode;
2718 } else if (sh_video->pts_assoc_mode == 0) {
2719 if (mpctx->d_video->demuxer->timestamp_type == TIMESTAMP_TYPE_PTS
2720 && sh_video->codec_reordered_pts != MP_NOPTS_VALUE)
2721 sh_video->pts_assoc_mode = 1;
2722 else
2723 sh_video->pts_assoc_mode = 2;
2724 } else {
2725 int probcount1 = sh_video->num_reordered_pts_problems;
2726 int probcount2 = sh_video->num_sorted_pts_problems;
2727 if (sh_video->pts_assoc_mode == 2) {
2728 int tmp = probcount1;
2729 probcount1 = probcount2;
2730 probcount2 = tmp;
2732 if (probcount1 >= probcount2 * 1.5 + 2) {
2733 sh_video->pts_assoc_mode = 3 - sh_video->pts_assoc_mode;
2734 mp_msg(MSGT_CPLAYER, MSGL_V, "Switching to pts association mode "
2735 "%d.\n", sh_video->pts_assoc_mode);
2738 sh_video->pts = sh_video->pts_assoc_mode == 1 ?
2739 sh_video->codec_reordered_pts : sh_video->sorted_pts;
2742 static double update_video(struct MPContext *mpctx)
2744 struct sh_video *sh_video = mpctx->sh_video;
2745 struct vo *video_out = mpctx->video_out;
2746 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
2747 mpctx->osd); // hack for vf_expand
2748 if (!mpctx->opts.correct_pts)
2749 return update_video_nocorrect_pts(mpctx);
2751 double pts;
2753 while (1) {
2754 current_module = "filter_video";
2755 if (!mpctx->hrseek_active
2756 && vo_get_buffered_frame(video_out, false) >= 0)
2757 break;
2758 // XXX Time used in this call is not counted in any performance
2759 // timer now
2760 if (vf_output_queued_frame(sh_video->vfilter))
2761 break;
2762 unsigned char *packet = NULL;
2763 int in_size = ds_get_packet_pts(mpctx->d_video, &packet, &pts);
2764 if (pts != MP_NOPTS_VALUE)
2765 pts += mpctx->video_offset;
2766 bool hit_eof = false;
2767 if (in_size < 0) {
2768 // try to extract last frames in case of decoder lag
2769 in_size = 0;
2770 pts = MP_NOPTS_VALUE;
2771 hit_eof = true;
2773 if (in_size > max_framesize)
2774 max_framesize = in_size;
2775 current_module = "decode video";
2776 if (pts >= mpctx->hrseek_pts - .005)
2777 mpctx->hrseek_framedrop = false;
2778 int framedrop_type = mpctx->hrseek_framedrop ? 1 :
2779 check_framedrop(mpctx, sh_video->frametime);
2780 void *decoded_frame = decode_video(sh_video, packet, in_size,
2781 framedrop_type, pts);
2782 if (decoded_frame) {
2783 determine_frame_pts(mpctx);
2784 current_module = "filter video";
2785 filter_video(sh_video, decoded_frame, sh_video->pts);
2786 } else if (hit_eof) {
2787 if (vo_get_buffered_frame(video_out, true) < 0)
2788 return -1;
2790 break;
2793 if (!video_out->frame_loaded)
2794 return 0;
2796 pts = video_out->next_pts;
2797 if (pts == MP_NOPTS_VALUE) {
2798 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Video pts after filters MISSING\n");
2799 // Try to use decoder pts from before filters
2800 pts = sh_video->pts;
2801 if (pts == MP_NOPTS_VALUE)
2802 pts = sh_video->last_pts;
2804 if (mpctx->hrseek_active && pts < mpctx->hrseek_pts - .005) {
2805 vo_skip_frame(video_out);
2806 return 0;
2808 mpctx->hrseek_active = false;
2809 sh_video->pts = pts;
2810 if (sh_video->last_pts == MP_NOPTS_VALUE)
2811 sh_video->last_pts = sh_video->pts;
2812 else if (sh_video->last_pts > sh_video->pts) {
2813 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Decreasing video pts: %f < %f\n",
2814 sh_video->pts, sh_video->last_pts);
2815 /* If the difference in pts is small treat it as jitter around the
2816 * right value (possibly caused by incorrect timestamp ordering) and
2817 * just show this frame immediately after the last one.
2818 * Treat bigger differences as timestamp resets and start counting
2819 * timing of later frames from the position of this one. */
2820 if (sh_video->last_pts - sh_video->pts > 0.5)
2821 sh_video->last_pts = sh_video->pts;
2822 else
2823 sh_video->pts = sh_video->last_pts;
2825 double frame_time = sh_video->pts - sh_video->last_pts;
2826 sh_video->last_pts = sh_video->pts;
2827 sh_video->timer += frame_time;
2828 if (mpctx->sh_audio)
2829 mpctx->delay -= frame_time;
2830 return frame_time;
2833 void pause_player(struct MPContext *mpctx)
2835 if (mpctx->paused)
2836 return;
2837 mpctx->paused = 1;
2838 mpctx->step_frames = 0;
2839 mpctx->time_frame -= get_relative_time(mpctx);
2841 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2842 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2844 if (mpctx->ao && mpctx->sh_audio)
2845 ao_pause(mpctx->ao); // pause audio, keep data if possible
2848 void unpause_player(struct MPContext *mpctx)
2850 if (!mpctx->paused)
2851 return;
2852 mpctx->paused = 0;
2854 if (mpctx->ao && mpctx->sh_audio)
2855 ao_resume(mpctx->ao);
2856 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok
2857 && !mpctx->step_frames)
2858 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL); // resume video
2859 (void)get_relative_time(mpctx); // ignore time that passed during pause
2862 void add_step_frame(struct MPContext *mpctx)
2864 mpctx->step_frames++;
2865 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
2866 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
2867 unpause_player(mpctx);
2870 static void pause_loop(struct MPContext *mpctx)
2872 struct MPOpts *opts = &mpctx->opts;
2873 mp_cmd_t* cmd;
2874 #ifdef CONFIG_STREAM_CACHE
2875 int old_cache_fill = stream_cache_size > 0 ?
2876 cache_fill_status(mpctx->stream) : 0;
2877 #endif
2878 if (!opts->quiet) {
2879 if (opts->term_osd && !mpctx->sh_video) {
2880 set_osd_tmsg(OSD_MSG_PAUSE, 1, 0, " ===== PAUSE =====");
2881 update_osd_msg(mpctx);
2882 } else
2883 mp_msg(MSGT_CPLAYER, MSGL_STATUS, "\n%s\r",
2884 mp_gtext(" ===== PAUSE ====="));
2885 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_PAUSED\n");
2888 while ( (cmd = mp_input_get_cmd(mpctx->input, 20, 1)) == NULL
2889 || cmd->id == MP_CMD_SET_MOUSE_POS || cmd->pausing == 4) {
2890 if (cmd) {
2891 cmd = mp_input_get_cmd(mpctx->input, 0, 0);
2892 run_command(mpctx, cmd);
2893 mp_cmd_free(cmd);
2894 continue;
2896 if (mpctx->sh_video && mpctx->video_out)
2897 vo_check_events(mpctx->video_out);
2898 #ifdef CONFIG_MENU
2899 if (vf_menu)
2900 vf_menu_pause_update(vf_menu);
2901 #endif
2902 usec_sleep(20000);
2903 update_osd_msg(mpctx);
2904 int hack = vo_osd_changed(0);
2905 vo_osd_changed(hack);
2906 if (hack)
2907 break;
2908 #ifdef CONFIG_STREAM_CACHE
2909 if (!opts->quiet && stream_cache_size > 0) {
2910 int new_cache_fill = cache_fill_status(mpctx->stream);
2911 if (new_cache_fill != old_cache_fill) {
2912 if (opts->term_osd && !mpctx->sh_video) {
2913 set_osd_tmsg(OSD_MSG_PAUSE, 1, 0, "%s %d%%",
2914 mp_gtext(" ===== PAUSE ====="),
2915 new_cache_fill);
2916 update_osd_msg(mpctx);
2917 } else
2918 mp_msg(MSGT_CPLAYER, MSGL_STATUS, "%s %d%%\r",
2919 mp_gtext(" ===== PAUSE ====="),
2920 new_cache_fill);
2921 old_cache_fill = new_cache_fill;
2924 #endif
2929 // Find the right mute status and record position for new file position
2930 static void edl_seek_reset(MPContext *mpctx)
2932 mpctx->edl_muted = 0;
2933 next_edl_record = edl_records;
2935 while (next_edl_record) {
2936 if (next_edl_record->start_sec >= get_current_time(mpctx))
2937 break;
2939 if (next_edl_record->action == EDL_MUTE)
2940 mpctx->edl_muted = !mpctx->edl_muted;
2941 next_edl_record = next_edl_record->next;
2943 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2944 mixer_mute(&mpctx->mixer);
2948 // Execute EDL command for the current position if one exists
2949 static void edl_update(MPContext *mpctx)
2951 if (!next_edl_record)
2952 return;
2954 if (!mpctx->sh_video) {
2955 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot use EDL without video, disabling.\n");
2956 free_edl(edl_records);
2957 next_edl_record = NULL;
2958 edl_records = NULL;
2959 return;
2962 if (get_current_time(mpctx) >= next_edl_record->start_sec) {
2963 if (next_edl_record->action == EDL_SKIP) {
2964 mpctx->osd_function = OSD_FFW;
2965 queue_seek(mpctx, MPSEEK_RELATIVE, next_edl_record->length_sec, 0);
2966 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop "
2967 "[%f], length [%f]\n", next_edl_record->start_sec,
2968 next_edl_record->stop_sec, next_edl_record->length_sec);
2970 else if (next_edl_record->action == EDL_MUTE) {
2971 mpctx->edl_muted = !mpctx->edl_muted;
2972 if ((mpctx->user_muted | mpctx->edl_muted) != mpctx->mixer.muted)
2973 mixer_mute(&mpctx->mixer);
2974 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n",
2975 next_edl_record->start_sec );
2977 next_edl_record = next_edl_record->next;
2981 static void reinit_decoders(struct MPContext *mpctx)
2983 reinit_video_chain(mpctx);
2984 reinit_audio_chain(mpctx);
2985 mp_property_do("sub", M_PROPERTY_SET, &(int){mpctx->global_sub_pos}, mpctx);
2988 static void seek_reset(struct MPContext *mpctx, bool reset_ao)
2990 if (mpctx->sh_video) {
2991 current_module = "seek_video_reset";
2992 resync_video_stream(mpctx->sh_video);
2993 mpctx->sh_video->timer = 0;
2994 vo_seek_reset(mpctx->video_out);
2995 mpctx->sh_video->timer = 0;
2996 mpctx->sh_video->num_buffered_pts = 0;
2997 mpctx->sh_video->last_pts = MP_NOPTS_VALUE;
2998 mpctx->delay = 0;
2999 mpctx->time_frame = 0;
3000 mpctx->restart_playback = true;
3001 // Not all demuxers set d_video->pts during seek, so this value
3002 // (which is used by at least vobsub and edl code below) may
3003 // be completely wrong (probably 0).
3004 mpctx->sh_video->pts = mpctx->d_video->pts + mpctx->video_offset;
3005 mpctx->video_pts = mpctx->sh_video->pts;
3006 update_subtitles(mpctx, mpctx->sh_video->pts, mpctx->video_offset,
3007 true);
3008 update_teletext(mpctx->sh_video, mpctx->demuxer, 1);
3011 if (mpctx->sh_audio) {
3012 current_module = "seek_audio_reset";
3013 resync_audio_stream(mpctx->sh_audio);
3014 if (reset_ao)
3015 // stop audio, throwing away buffered data
3016 ao_reset(mpctx->ao);
3017 mpctx->sh_audio->a_buffer_len = 0;
3018 mpctx->sh_audio->a_out_buffer_len = 0;
3019 if (!mpctx->sh_video)
3020 update_subtitles(mpctx, mpctx->sh_audio->pts,
3021 mpctx->video_offset, true);
3024 if (vo_vobsub && mpctx->sh_video) {
3025 current_module = "seek_vobsub_reset";
3026 vobsub_seek(vo_vobsub, mpctx->sh_video->pts);
3029 edl_seek_reset(mpctx);
3031 mpctx->hrseek_active = false;
3032 mpctx->hrseek_framedrop = false;
3033 mpctx->total_avsync_change = 0;
3034 audio_time_usage = 0; video_time_usage = 0; vout_time_usage = 0;
3035 drop_frame_cnt = 0;
3037 current_module = NULL;
3040 static bool timeline_set_part(struct MPContext *mpctx, int i)
3042 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
3043 struct timeline_part *n = mpctx->timeline + i;
3044 mpctx->timeline_part = i;
3045 mpctx->video_offset = n->start - n->source_start;
3046 if (n->source == p->source)
3047 return false;
3048 enum stop_play_reason orig_stop_play = mpctx->stop_play;
3049 if (!mpctx->sh_video && mpctx->stop_play == KEEP_PLAYING)
3050 mpctx->stop_play = AT_END_OF_FILE; // let audio uninit drain data
3051 uninit_player(mpctx, INITIALIZED_VCODEC | (mpctx->opts.fixed_vo ? 0 : INITIALIZED_VO) | (mpctx->opts.gapless_audio ? 0 : INITIALIZED_AO) | INITIALIZED_ACODEC | INITIALIZED_SUB);
3052 mpctx->stop_play = orig_stop_play;
3053 mpctx->demuxer = n->source->demuxer;
3054 mpctx->d_video = mpctx->demuxer->video;
3055 mpctx->d_audio = mpctx->demuxer->audio;
3056 mpctx->d_sub = mpctx->demuxer->sub;
3057 mpctx->sh_video = mpctx->d_video->sh;
3058 mpctx->sh_audio = mpctx->d_audio->sh;
3059 return true;
3062 // Given pts, switch playback to the corresponding part.
3063 // Return offset within that part.
3064 static double timeline_set_from_time(struct MPContext *mpctx, double pts,
3065 bool *need_reset)
3067 if (pts < 0)
3068 pts = 0;
3069 for (int i = 0; i < mpctx->num_timeline_parts; i++) {
3070 struct timeline_part *p = mpctx->timeline + i;
3071 if (pts < (p+1)->start) {
3072 *need_reset = timeline_set_part(mpctx, i);
3073 return pts - p->start + p->source_start;
3076 return -1;
3080 // return -1 if seek failed (non-seekable stream?), 0 otherwise
3081 static int seek(MPContext *mpctx, struct seek_params seek,
3082 bool timeline_fallthrough)
3084 struct MPOpts *opts = &mpctx->opts;
3086 current_module = "seek";
3087 if (mpctx->stop_play == AT_END_OF_FILE)
3088 mpctx->stop_play = KEEP_PLAYING;
3089 bool hr_seek = mpctx->demuxer->accurate_seek && opts->correct_pts;
3090 hr_seek &= seek.exact >= 0 && seek.type != MPSEEK_FACTOR;
3091 hr_seek &= opts->hr_seek == 0 && seek.type == MPSEEK_ABSOLUTE
3092 || opts->hr_seek > 0 || seek.exact > 0;
3093 if (seek.type == MPSEEK_FACTOR
3094 || seek.type == MPSEEK_ABSOLUTE
3095 && seek.amount < mpctx->last_chapter_pts
3096 || seek.amount < 0)
3097 mpctx->last_chapter_seek = -1;
3098 if (mpctx->timeline && seek.type == MPSEEK_FACTOR) {
3099 seek.amount *= mpctx->timeline[mpctx->num_timeline_parts].start;
3100 seek.type = MPSEEK_ABSOLUTE;
3102 if ((mpctx->demuxer->accurate_seek || mpctx->timeline)
3103 && seek.type == MPSEEK_RELATIVE) {
3104 seek.type = MPSEEK_ABSOLUTE;
3105 seek.direction = seek.amount > 0 ? 1 : -1;
3106 seek.amount += get_current_time(mpctx);
3109 /* At least the liba52 decoder wants to read from the input stream
3110 * during initialization, so reinit must be done after the demux_seek()
3111 * call that clears possible stream EOF. */
3112 bool need_reset = false;
3113 double demuxer_amount = seek.amount;
3114 if (mpctx->timeline) {
3115 demuxer_amount = timeline_set_from_time(mpctx, seek.amount,
3116 &need_reset);
3117 if (demuxer_amount == -1) {
3118 mpctx->stop_play = AT_END_OF_FILE;
3119 // Clear audio from current position
3120 if (mpctx->sh_audio) {
3121 ao_reset(mpctx->ao);
3122 mpctx->sh_audio->a_buffer_len = 0;
3123 mpctx->sh_audio->a_out_buffer_len = 0;
3125 return -1;
3128 int demuxer_style = 0;
3129 switch (seek.type) {
3130 case MPSEEK_FACTOR:
3131 demuxer_style |= SEEK_FACTOR; // fallthrough
3132 case MPSEEK_ABSOLUTE:
3133 demuxer_style |= SEEK_ABSOLUTE;
3135 if (hr_seek || seek.direction < 0)
3136 demuxer_style |= SEEK_BACKWARD;
3137 else if (seek.direction > 0)
3138 demuxer_style |= SEEK_FORWARD;
3140 int seekresult = demux_seek(mpctx->demuxer, demuxer_amount, audio_delay,
3141 demuxer_style);
3142 if (need_reset)
3143 reinit_decoders(mpctx);
3144 if (seekresult == 0)
3145 return -1;
3147 seek_reset(mpctx, !timeline_fallthrough);
3149 /* Use the target time as "current position" for further relative
3150 * seeks etc until a new video frame has been decoded */
3151 if (seek.type == MPSEEK_ABSOLUTE)
3152 mpctx->video_pts = seek.amount;
3154 if (hr_seek) {
3155 mpctx->hrseek_active = true;
3156 mpctx->hrseek_framedrop = true;
3157 mpctx->hrseek_pts = seek.amount;
3160 mpctx->start_timestamp = GetTimerMS();
3162 return 0;
3165 void queue_seek(struct MPContext *mpctx, enum seek_type type, double amount,
3166 int exact)
3168 struct seek_params *seek = &mpctx->seek;
3169 switch (type) {
3170 case MPSEEK_RELATIVE:
3171 if (seek->type == MPSEEK_FACTOR)
3172 return; // Well... not common enough to bother doing better
3173 seek->amount += amount;
3174 seek->exact = FFMAX(seek->exact, exact);
3175 if (seek->type == MPSEEK_NONE)
3176 seek->exact = exact;
3177 if (seek->type == MPSEEK_ABSOLUTE)
3178 return;
3179 if (seek->amount == 0) {
3180 *seek = (struct seek_params){0};
3181 return;
3183 seek->type = MPSEEK_RELATIVE;
3184 return;
3185 case MPSEEK_ABSOLUTE:
3186 case MPSEEK_FACTOR:
3187 *seek = (struct seek_params) {
3188 .type = type,
3189 .amount = amount,
3190 .exact = exact,
3192 return;
3193 case MPSEEK_NONE:
3194 *seek = (struct seek_params){0};
3195 return;
3197 abort();
3201 double get_time_length(struct MPContext *mpctx)
3203 if (mpctx->timeline)
3204 return mpctx->timeline[mpctx->num_timeline_parts].start;
3206 struct demuxer *demuxer = mpctx->demuxer;
3207 double get_time_ans;
3208 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
3209 if (demux_control(demuxer, DEMUXER_CTRL_GET_TIME_LENGTH,
3210 (void *) &get_time_ans) > 0)
3211 return get_time_ans;
3213 struct sh_video *sh_video = mpctx->d_video->sh;
3214 struct sh_audio *sh_audio = mpctx->d_audio->sh;
3215 if (sh_video && sh_video->i_bps && sh_audio && sh_audio->i_bps)
3216 return (double) (demuxer->movi_end - demuxer->movi_start) /
3217 (sh_video->i_bps + sh_audio->i_bps);
3218 if (sh_video && sh_video->i_bps)
3219 return (double) (demuxer->movi_end - demuxer->movi_start) /
3220 sh_video->i_bps;
3221 if (sh_audio && sh_audio->i_bps)
3222 return (double) (demuxer->movi_end - demuxer->movi_start) /
3223 sh_audio->i_bps;
3224 return 0;
3227 /* If there are timestamps from stream level then use those (for example
3228 * DVDs can have consistent times there while the MPEG-level timestamps
3229 * reset). */
3230 double get_current_time(struct MPContext *mpctx)
3232 struct demuxer *demuxer = mpctx->demuxer;
3233 if (demuxer->stream_pts != MP_NOPTS_VALUE)
3234 return demuxer->stream_pts;
3235 struct sh_video *sh_video = demuxer->video->sh;
3236 if (sh_video)
3237 return mpctx->video_pts;
3238 return playing_audio_pts(mpctx);
3241 int get_percent_pos(struct MPContext *mpctx)
3243 struct demuxer *demuxer = mpctx->demuxer;
3244 int ans = 0;
3245 if (mpctx->timeline)
3246 ans = get_current_time(mpctx) * 100 /
3247 mpctx->timeline[mpctx->num_timeline_parts].start;
3248 else if (demux_control(demuxer, DEMUXER_CTRL_GET_PERCENT_POS, &ans) > 0)
3250 else {
3251 int len = (demuxer->movi_end - demuxer->movi_start) / 100;
3252 off_t pos = demuxer->filepos > 0 ?
3253 demuxer->filepos : stream_tell(demuxer->stream);
3254 if (len > 0)
3255 ans = (pos - demuxer->movi_start) / len;
3256 else
3257 ans = 0;
3259 if (ans < 0)
3260 ans = 0;
3261 if (ans > 100)
3262 ans = 100;
3263 return ans;
3266 // -2 is no chapters, -1 is before first chapter
3267 int get_current_chapter(struct MPContext *mpctx)
3269 double current_pts = get_current_time(mpctx);
3270 if (!mpctx->chapters)
3271 return FFMAX(mpctx->last_chapter_seek,
3272 demuxer_get_current_chapter(mpctx->demuxer, current_pts));
3274 int i;
3275 for (i = 1; i < mpctx->num_chapters; i++)
3276 if (current_pts < mpctx->chapters[i].start)
3277 break;
3278 return FFMAX(mpctx->last_chapter_seek, i - 1);
3281 // currently returns a string allocated with malloc, not talloc
3282 char *chapter_display_name(struct MPContext *mpctx, int chapter)
3284 if (!mpctx->chapters || !mpctx->sh_video)
3285 return demuxer_chapter_display_name(mpctx->demuxer, chapter);
3286 return strdup(mpctx->chapters[chapter].name);
3289 int seek_chapter(struct MPContext *mpctx, int chapter, double *seek_pts,
3290 char **chapter_name)
3292 mpctx->last_chapter_seek = -1;
3293 if (!mpctx->chapters || !mpctx->sh_video) {
3294 int res = demuxer_seek_chapter(mpctx->demuxer, chapter, seek_pts,
3295 chapter_name);
3296 if (res >= 0) {
3297 if (*seek_pts == -1)
3298 seek_reset(mpctx, true);
3299 else {
3300 mpctx->last_chapter_seek = res;
3301 mpctx->last_chapter_pts = *seek_pts;
3304 return res;
3307 if (chapter >= mpctx->num_chapters)
3308 return -1;
3309 if (chapter < 0)
3310 chapter = 0;
3311 *seek_pts = mpctx->chapters[chapter].start;
3312 mpctx->last_chapter_seek = chapter;
3313 mpctx->last_chapter_pts = *seek_pts;
3314 if (chapter_name)
3315 *chapter_name = talloc_strdup(NULL, mpctx->chapters[chapter].name);
3316 return chapter;
3320 static void run_playloop(struct MPContext *mpctx)
3322 struct MPOpts *opts = &mpctx->opts;
3323 float aq_sleep_time = 0;
3324 bool full_audio_buffers = false;
3326 if (opts->chapterrange[1] > 0) {
3327 int cur_chapter = get_current_chapter(mpctx);
3328 if (cur_chapter!=-1 && cur_chapter + 1 > opts->chapterrange[1])
3329 mpctx->stop_play = PT_NEXT_ENTRY;
3332 if (!mpctx->sh_audio && mpctx->d_audio->sh) {
3333 mpctx->sh_audio = mpctx->d_audio->sh;
3334 mpctx->sh_audio->ds = mpctx->d_audio;
3335 reinit_audio_chain(mpctx);
3338 /*========================== PLAY AUDIO ============================*/
3340 if (mpctx->sh_audio && !mpctx->paused
3341 && (!mpctx->restart_playback || !mpctx->sh_video)) {
3342 int status = fill_audio_out_buffers(mpctx);
3343 full_audio_buffers = status >= 0;
3344 if (status == -2)
3345 // at eof, all audio at least written to ao
3346 if (!mpctx->sh_video)
3347 mpctx->stop_play = AT_END_OF_FILE;
3351 if (!mpctx->sh_video) {
3352 mpctx->restart_playback = false;
3353 if (mpctx->step_frames) {
3354 mpctx->step_frames = 0;
3355 pause_player(mpctx);
3357 // handle audio-only case:
3358 double a_pos = 0;
3359 // sh_audio can be NULL due to video stream switching
3360 // TODO: handle this better
3361 if (mpctx->sh_audio)
3362 a_pos = playing_audio_pts(mpctx);
3364 print_status(mpctx, a_pos, false);
3366 update_subtitles(mpctx, a_pos, mpctx->video_offset, false);
3367 update_osd_msg(mpctx);
3368 if (end_at.type == END_AT_TIME && end_at.pos < a_pos) {
3369 mpctx->stop_play = AT_END_OF_FILE;
3370 } else if (mpctx->timeline && mpctx->stop_play == AT_END_OF_FILE
3371 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts
3372 && mpctx->sh_audio) {
3373 struct timeline_part *p = mpctx->timeline + mpctx->timeline_part;
3374 double delay = ao_get_delay(mpctx->ao);
3375 if (!opts->gapless_audio && p->source != (p+1)->source
3376 && delay > 0.05) {
3377 mpctx->stop_play = KEEP_PLAYING;
3378 mp_input_get_cmd(mpctx->input, (delay-.05) * 1000, true);
3379 } else {
3380 seek(mpctx, (struct seek_params){ .type = MPSEEK_ABSOLUTE,
3381 .amount = (p+1)->start },
3382 true);
3384 } else if (!mpctx->stop_play) {
3385 int sleep_time = full_audio_buffers || !mpctx->sh_audio ? 100 : 20;
3386 mp_input_get_cmd(mpctx->input, sleep_time, true);
3388 } else {
3390 /*========================== PLAY VIDEO ============================*/
3392 vo_pts = mpctx->sh_video->timer * 90000.0;
3393 vo_fps = mpctx->sh_video->fps;
3395 bool blit_frame = mpctx->video_out->frame_loaded;
3396 if (!blit_frame || mpctx->hrseek_active) {
3397 double frame_time = update_video(mpctx);
3398 blit_frame = mpctx->video_out->frame_loaded;
3399 mp_dbg(MSGT_AVSYNC, MSGL_DBG2, "*** ftime=%5.3f ***\n", frame_time);
3400 if (mpctx->sh_video->vf_initialized < 0) {
3401 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL,
3402 "\nFATAL: Could not initialize video filters (-vf) "
3403 "or video output (-vo).\n");
3404 mpctx->stop_play = PT_NEXT_ENTRY;
3405 return;
3407 if (frame_time < 0)
3408 mpctx->stop_play = AT_END_OF_FILE;
3409 else if (!mpctx->restart_playback) {
3410 mpctx->time_frame += frame_time / opts->playback_speed;
3411 adjust_sync(mpctx, frame_time);
3414 if (mpctx->timeline) {
3415 struct timeline_part *next =
3416 mpctx->timeline + mpctx->timeline_part + 1;
3417 if (mpctx->sh_video->pts >= next->start
3418 || mpctx->stop_play == AT_END_OF_FILE
3419 && mpctx->timeline_part + 1 < mpctx->num_timeline_parts) {
3420 seek(mpctx, (struct seek_params){ .type = MPSEEK_ABSOLUTE,
3421 .amount = next->start },
3422 true);
3423 return;
3427 // ==========================================================================
3429 current_module = "vo_check_events";
3430 vo_check_events(mpctx->video_out);
3432 #ifdef CONFIG_X11
3433 if (stop_xscreensaver) {
3434 current_module = "stop_xscreensaver";
3435 xscreensaver_heartbeat(mpctx->x11_state);
3437 #endif
3438 if (heartbeat_cmd) {
3439 static unsigned last_heartbeat;
3440 unsigned now = GetTimerMS();
3441 if (now - last_heartbeat > 30000) {
3442 last_heartbeat = now;
3443 system(heartbeat_cmd);
3447 bool frame_time_remaining = sleep_until_near_frame(mpctx,
3448 &mpctx->time_frame,
3449 full_audio_buffers,
3450 &aq_sleep_time);
3452 //====================== FLIP PAGE (VIDEO BLT): =========================
3454 current_module = "flip_page";
3455 if (!frame_time_remaining && blit_frame) {
3456 struct sh_video *sh_video = mpctx->sh_video;
3457 mpctx->video_pts = sh_video->pts;
3458 update_subtitles(mpctx, sh_video->pts, mpctx->video_offset, false);
3459 update_teletext(sh_video, mpctx->demuxer, 0);
3460 update_osd_msg(mpctx);
3461 struct vf_instance *vf = sh_video->vfilter;
3462 vf->control(vf, VFCTRL_DRAW_EOSD, mpctx->osd);
3463 vf->control(vf, VFCTRL_DRAW_OSD, mpctx->osd);
3464 vo_osd_changed(0);
3466 mpctx->time_frame -= mpctx->video_out->flip_queue_offset;
3467 aq_sleep_time += mpctx->time_frame;
3468 // flag 256 means: libvo driver does its timing (dvb card)
3469 if (mpctx->time_frame > 0.001
3470 && !(mpctx->sh_video->output_flags & VFCAP_TIMER))
3471 mpctx->time_frame = timing_sleep(mpctx, mpctx->time_frame);
3472 mpctx->time_frame += mpctx->video_out->flip_queue_offset;
3474 unsigned int t2 = GetTimer();
3475 /* Playing with playback speed it's possible to get pathological
3476 * cases with mpctx->time_frame negative enough to cause an
3477 * overflow in pts_us calculation, thus the FFMAX. */
3478 double time_frame = FFMAX(mpctx->time_frame, -1);
3479 unsigned int pts_us = mpctx->last_time + time_frame * 1e6;
3480 int duration = -1;
3481 double pts2 = mpctx->video_out->next_pts2;
3482 if (pts2 != MP_NOPTS_VALUE && opts->correct_pts
3483 && !mpctx->restart_playback) {
3484 // expected A/V sync correction is ignored
3485 double diff = (pts2 - mpctx->video_pts);
3486 diff /= opts->playback_speed;
3487 if (mpctx->time_frame < 0)
3488 diff += mpctx->time_frame;
3489 if (diff < 0)
3490 diff = 0;
3491 if (diff > 10)
3492 diff = 10;
3493 duration = diff * 1e6;
3495 vo_flip_page(mpctx->video_out, pts_us | 1, duration);
3497 mpctx->last_vo_flip_duration = (GetTimer() - t2) * 0.000001;
3498 vout_time_usage += mpctx->last_vo_flip_duration;
3499 if (mpctx->video_out->driver->flip_page_timed) {
3500 // No need to adjust sync based on flip speed
3501 mpctx->last_vo_flip_duration = 0;
3502 // For print_status - VO call finishing early is OK for sync
3503 mpctx->time_frame -= get_relative_time(mpctx);
3505 if (mpctx->restart_playback) {
3506 mpctx->syncing_audio = true;
3507 if (mpctx->sh_audio && !mpctx->paused)
3508 fill_audio_out_buffers(mpctx);
3509 mpctx->restart_playback = false;
3510 mpctx->time_frame = 0;
3511 get_relative_time(mpctx);
3513 print_status(mpctx, MP_NOPTS_VALUE, true);
3514 } else
3515 print_status(mpctx, MP_NOPTS_VALUE, false);
3517 //============================ Auto QUALITY ============================
3519 /*Output quality adjustments:*/
3520 if (opts->auto_quality > 0) {
3521 current_module = "autoq";
3522 if (output_quality < opts->auto_quality && aq_sleep_time > 0)
3523 ++output_quality;
3524 else
3525 if (output_quality>1 && aq_sleep_time<0)
3526 --output_quality;
3527 else if (output_quality>0 && aq_sleep_time<-0.050f) // 50ms
3528 output_quality = 0;
3529 set_video_quality(mpctx->sh_video, output_quality);
3532 if (!frame_time_remaining && blit_frame) {
3533 if (play_n_frames >= 0) {
3534 --play_n_frames;
3535 if (play_n_frames <= 0)
3536 mpctx->stop_play = PT_NEXT_ENTRY;
3538 if (mpctx->step_frames > 0) {
3539 mpctx->step_frames--;
3540 if (mpctx->step_frames == 0)
3541 pause_player(mpctx);
3545 // FIXME: add size based support for -endpos
3546 if (end_at.type == END_AT_TIME &&
3547 !frame_time_remaining && end_at.pos <= mpctx->sh_video->pts)
3548 mpctx->stop_play = PT_NEXT_ENTRY;
3550 } // end if(mpctx->sh_video)
3552 #ifdef CONFIG_DVDNAV
3553 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
3554 nav_highlight_t hl;
3555 mp_dvdnav_get_highlight(mpctx->stream, &hl);
3556 if (!vo_spudec || !spudec_apply_palette_crop(vo_spudec, hl.palette, hl.sx, hl.sy, hl.ex, hl.ey)) {
3557 osd_set_nav_box(hl.sx, hl.sy, hl.ex, hl.ey);
3558 vo_osd_changed(OSDTYPE_DVDNAV);
3559 } else {
3560 osd_set_nav_box(0, 0, 0, 0);
3561 vo_osd_changed(OSDTYPE_DVDNAV);
3562 vo_osd_changed(OSDTYPE_SPU);
3565 if (mp_dvdnav_stream_has_changed(mpctx->stream)) {
3566 double ar = -1.0;
3567 if (mpctx->sh_video &&
3568 stream_control(mpctx->demuxer->stream,
3569 STREAM_CTRL_GET_ASPECT_RATIO, &ar)
3570 != STREAM_UNSUPPORTED)
3571 mpctx->sh_video->stream_aspect = ar;
3574 #endif
3576 //================= Keyboard events, SEEKing ====================
3578 current_module = "key_events";
3580 while (1) {
3581 mp_cmd_t *cmd;
3582 while ((cmd = mp_input_get_cmd(mpctx->input, 0, 1)) != NULL) {
3583 /* Allow running consecutive seek commands to combine them,
3584 * but execute the seek before running other commands.
3585 * If the user seeks continuously (keeps arrow key down)
3586 * try to finish showing a frame from one location before doing
3587 * another seek (which could lead to unchanging display). */
3588 if (mpctx->seek.type && cmd->id != MP_CMD_SEEK
3589 || mpctx->restart_playback && cmd->id == MP_CMD_SEEK
3590 && GetTimerMS() - mpctx->start_timestamp < 300)
3591 break;
3592 cmd = mp_input_get_cmd(mpctx->input, 0, 0);
3593 run_command(mpctx, cmd);
3594 mp_cmd_free(cmd);
3595 if (mpctx->stop_play)
3596 break;
3598 if (!mpctx->paused || mpctx->stop_play || mpctx->seek.type
3599 || mpctx->restart_playback)
3600 break;
3601 if (mpctx->sh_video) {
3602 update_osd_msg(mpctx);
3603 int hack = vo_osd_changed(0);
3604 vo_osd_changed(hack);
3605 if (hack) {
3606 if (redraw_osd(mpctx->sh_video, mpctx->osd) < 0) {
3607 add_step_frame(mpctx);
3608 break;
3609 } else
3610 vo_osd_changed(0);
3613 pause_loop(mpctx);
3616 // handle -sstep
3617 if (step_sec > 0 && !mpctx->paused) {
3618 mpctx->osd_function = OSD_FFW;
3619 queue_seek(mpctx, MPSEEK_RELATIVE, step_sec, 0);
3622 edl_update(mpctx);
3624 /* Looping. */
3625 if (mpctx->stop_play==AT_END_OF_FILE && opts->loop_times>=0) {
3626 mp_msg(MSGT_CPLAYER, MSGL_V, "loop_times = %d\n", opts->loop_times);
3628 if (opts->loop_times>1)
3629 opts->loop_times--;
3630 else if (opts->loop_times==1)
3631 opts->loop_times = -1;
3632 play_n_frames = play_n_frames_mf;
3633 mpctx->stop_play = 0;
3634 queue_seek(mpctx, MPSEEK_ABSOLUTE, 0, 0);
3637 if (mpctx->seek.type) {
3638 seek(mpctx, mpctx->seek, false);
3639 mpctx->seek = (struct seek_params){0};
3644 static int read_keys(void *ctx, int fd)
3646 getch2(ctx);
3647 return mplayer_get_key(ctx, 0);
3650 static bool attachment_is_font(struct demux_attachment *att)
3652 if (!att->name || !att->type || !att->data || !att->data_size)
3653 return false;
3654 // match against MIME types
3655 if (strcmp(att->type, "application/x-truetype-font") == 0
3656 || strcmp(att->type, "application/x-font") == 0)
3657 return true;
3658 // fallback: match against file extension
3659 if (strlen(att->name) > 4) {
3660 char *ext = att->name + strlen(att->name) - 4;
3661 if (strcasecmp(ext, ".ttf") == 0 || strcasecmp(ext, ".ttc") == 0
3662 || strcasecmp(ext, ".otf") == 0)
3663 return true;
3665 return false;
3668 static int select_audio(demuxer_t *demuxer, int audio_id, char **audio_lang)
3670 if (audio_id == -1)
3671 audio_id = demuxer_audio_track_by_lang_and_default(demuxer, audio_lang);
3672 if (audio_id != -1) // -1 (automatic) is the default behaviour of demuxers
3673 demuxer_switch_audio(demuxer, audio_id);
3674 if (audio_id == -2) { // some demuxers don't yet know how to switch to no sound
3675 demuxer->audio->id = -2;
3676 demuxer->audio->sh = NULL;
3678 return demuxer->audio->id;
3681 static void print_version(const char* name)
3683 mp_msg(MSGT_CPLAYER, MSGL_INFO, MP_TITLE, name);
3685 /* Test for CPU capabilities (and corresponding OS support) for optimizing */
3686 GetCpuCaps(&gCpuCaps);
3687 #if ARCH_X86
3688 mp_msg(MSGT_CPLAYER, MSGL_V,
3689 "CPUflags: MMX: %d MMX2: %d 3DNow: %d 3DNowExt: %d SSE: %d SSE2: %d SSSE3: %d\n",
3690 gCpuCaps.hasMMX, gCpuCaps.hasMMX2,
3691 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
3692 gCpuCaps.hasSSE, gCpuCaps.hasSSE2, gCpuCaps.hasSSSE3);
3693 #if CONFIG_RUNTIME_CPUDETECT
3694 mp_tmsg(MSGT_CPLAYER,MSGL_V, "Compiled with runtime CPU detection.\n");
3695 #else
3696 mp_tmsg(MSGT_CPLAYER,MSGL_V, "Compiled for x86 CPU with extensions:");
3697 if (HAVE_MMX)
3698 mp_msg(MSGT_CPLAYER,MSGL_V," MMX");
3699 if (HAVE_MMX2)
3700 mp_msg(MSGT_CPLAYER,MSGL_V," MMX2");
3701 if (HAVE_AMD3DNOW)
3702 mp_msg(MSGT_CPLAYER,MSGL_V," 3DNow");
3703 if (HAVE_AMD3DNOWEXT)
3704 mp_msg(MSGT_CPLAYER,MSGL_V," 3DNowExt");
3705 if (HAVE_SSE)
3706 mp_msg(MSGT_CPLAYER,MSGL_V," SSE");
3707 if (HAVE_SSE2)
3708 mp_msg(MSGT_CPLAYER,MSGL_V," SSE2");
3709 if (HAVE_SSSE3)
3710 mp_msg(MSGT_CPLAYER,MSGL_V," SSSE3");
3711 if (HAVE_CMOV)
3712 mp_msg(MSGT_CPLAYER,MSGL_V," CMOV");
3713 mp_msg(MSGT_CPLAYER,MSGL_V,"\n");
3714 #endif /* CONFIG_RUNTIME_CPUDETECT */
3715 #endif /* ARCH_X86 */
3718 #ifdef PTW32_STATIC_LIB
3719 static void detach_ptw32(void)
3721 pthread_win32_thread_detach_np();
3722 pthread_win32_process_detach_np();
3724 #endif
3726 /* This preprocessor directive is a hack to generate a mplayer-nomain.o object
3727 * file for some tools to link against. */
3728 #ifndef DISABLE_MAIN
3729 int main(int argc,char* argv[]){
3730 #ifdef PTW32_STATIC_LIB
3731 pthread_win32_process_attach_np();
3732 pthread_win32_thread_attach_np();
3733 atexit(detach_ptw32);
3734 #endif
3735 if (argc > 1 && !strcmp(argv[1], "-leak-report"))
3736 talloc_enable_leak_report();
3738 char * mem_ptr;
3740 // movie info:
3742 /* Flag indicating whether MPlayer should exit without playing anything. */
3743 int opt_exit = 0;
3744 int i;
3746 struct MPContext *mpctx = &(struct MPContext){
3747 .osd_function = OSD_PLAY,
3748 .begin_skip = MP_NOPTS_VALUE,
3749 .play_tree_step = 1,
3750 .global_sub_pos = -1,
3751 .set_of_sub_pos = -1,
3752 .file_format = DEMUXER_TYPE_UNKNOWN,
3753 .last_dvb_step = 1,
3756 InitTimer();
3757 srand(GetTimerMS());
3759 mp_msg_init();
3760 set_av_log_callback();
3762 #ifdef CONFIG_X11
3763 mpctx->x11_state = vo_x11_init_state();
3764 #endif
3765 struct MPOpts *opts = &mpctx->opts;
3766 set_default_mplayer_options(opts);
3767 // Create the config context and register the options
3768 mpctx->mconfig = m_config_new(opts, cfg_include);
3769 m_config_register_options(mpctx->mconfig,mplayer_opts);
3770 m_config_register_options(mpctx->mconfig, common_opts);
3771 mp_input_register_options(mpctx->mconfig);
3773 // Preparse the command line
3774 m_config_preparse_command_line(mpctx->mconfig,argc,argv);
3776 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
3777 set_path_env();
3778 #endif
3780 #ifdef CONFIG_TV
3781 stream_tv_defaults.immediate = 1;
3782 #endif
3784 parse_cfgfiles(mpctx, mpctx->mconfig);
3786 mpctx->playtree = m_config_parse_mp_command_line(mpctx->mconfig, argc, argv);
3787 if(mpctx->playtree == NULL)
3788 opt_exit = 1;
3789 else {
3790 mpctx->playtree = play_tree_cleanup(mpctx->playtree);
3791 if(mpctx->playtree) {
3792 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree,mpctx->mconfig);
3793 if(mpctx->playtree_iter) {
3794 if(play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
3795 play_tree_iter_free(mpctx->playtree_iter);
3796 mpctx->playtree_iter = NULL;
3798 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter,1);
3802 mpctx->key_fifo = mp_fifo_create(opts);
3804 print_version("MPlayer2");
3806 #if defined(__MINGW32__) || defined(__CYGWIN__)
3808 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
3809 BOOL WINAPI (*setDEP)(DWORD) = NULL;
3810 BOOL WINAPI (*setDllDir)(LPCTSTR) = NULL;
3811 if (kernel32) {
3812 setDEP = GetProcAddress(kernel32, "SetProcessDEPPolicy");
3813 setDllDir = GetProcAddress(kernel32, "SetDllDirectoryA");
3815 if (setDEP) setDEP(3);
3816 if (setDllDir) setDllDir("");
3818 // stop Windows from showing all kinds of annoying error dialogs
3819 SetErrorMode(0x8003);
3820 // request 1ms timer resolution
3821 timeBeginPeriod(1);
3822 #endif
3824 #ifdef CONFIG_PRIORITY
3825 set_priority();
3826 #endif
3828 if (codec_path)
3829 set_codec_path(codec_path);
3831 if(opts->video_driver_list && strcmp(opts->video_driver_list[0],"help")==0){
3832 list_video_out();
3833 opt_exit = 1;
3836 if(opts->audio_driver_list && strcmp(opts->audio_driver_list[0],"help")==0){
3837 list_audio_out();
3838 opt_exit = 1;
3841 /* Check codecs.conf. */
3842 if(!codecs_file || !parse_codec_cfg(codecs_file)){
3843 if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
3844 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
3845 if(!parse_codec_cfg(NULL)){
3846 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3848 mp_tmsg(MSGT_CPLAYER,MSGL_V,"Using built-in default codecs.conf.\n");
3851 free( mem_ptr ); // release the buffer created by get_path()
3854 if(audio_codec_list && strcmp(audio_codec_list[0],"help")==0){
3855 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available audio codecs:\n");
3856 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_CODECS\n");
3857 list_codecs(1);
3858 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3859 opt_exit = 1;
3861 if(video_codec_list && strcmp(video_codec_list[0],"help")==0){
3862 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Available video codecs:\n");
3863 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_CODECS\n");
3864 list_codecs(0);
3865 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3866 opt_exit = 1;
3868 if(video_fm_list && strcmp(video_fm_list[0],"help")==0){
3869 vfm_help();
3870 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3871 opt_exit = 1;
3873 if(audio_fm_list && strcmp(audio_fm_list[0],"help")==0){
3874 afm_help();
3875 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3876 opt_exit = 1;
3878 if(af_cfg.list && strcmp(af_cfg.list[0],"help")==0){
3879 af_help();
3880 printf("\n");
3881 opt_exit = 1;
3883 #ifdef CONFIG_X11
3884 if(vo_fstype_list && strcmp(vo_fstype_list[0],"help")==0){
3885 fstype_help();
3886 mp_msg(MSGT_FIXME, MSGL_FIXME, "\n");
3887 opt_exit = 1;
3889 #endif
3890 if((opts->demuxer_name && strcmp(opts->demuxer_name,"help")==0) ||
3891 (opts->audio_demuxer_name && strcmp(opts->audio_demuxer_name,"help")==0) ||
3892 (opts->sub_demuxer_name && strcmp(opts->sub_demuxer_name,"help")==0)){
3893 demuxer_help();
3894 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3895 opt_exit = 1;
3897 if (opts->list_properties) {
3898 property_print_help();
3899 opt_exit = 1;
3902 if(opt_exit)
3903 exit_player(mpctx, EXIT_NONE);
3905 if (!mpctx->filename && !opts->player_idle_mode) {
3906 // no file/vcd/dvd -> show HELP:
3907 mp_msg(MSGT_CPLAYER, MSGL_INFO, "%s", mp_gtext(help_text));
3908 exit_player_with_rc(mpctx, EXIT_NONE, 0);
3911 /* Display what configure line was used */
3912 mp_msg(MSGT_CPLAYER, MSGL_V, "Configuration: " CONFIGURATION "\n");
3914 // Many users forget to include command line in bugreports...
3915 if( mp_msg_test(MSGT_CPLAYER,MSGL_V) ){
3916 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "CommandLine:");
3917 for(i=1;i<argc;i++)mp_msg(MSGT_CPLAYER, MSGL_INFO," '%s'",argv[i]);
3918 mp_msg(MSGT_CPLAYER, MSGL_INFO, "\n");
3921 //------ load global data first ------
3923 mpctx->osd = osd_create();
3925 // check font
3926 #ifdef CONFIG_FREETYPE
3927 init_freetype();
3928 #endif
3929 #ifdef CONFIG_FONTCONFIG
3930 if(font_fontconfig <= 0)
3932 #endif
3933 #ifdef CONFIG_BITMAP_FONT
3934 if(font_name){
3935 vo_font=read_font_desc(font_name,font_factor,verbose>1);
3936 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",
3937 filename_recode(font_name));
3938 } else {
3939 // try default:
3940 vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
3941 free(mem_ptr); // release the buffer created by get_path()
3942 if(!vo_font)
3943 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
3945 if (sub_font_name)
3946 mpctx->osd->sub_font = read_font_desc(sub_font_name, font_factor, verbose>1);
3947 else
3948 mpctx->osd->sub_font = vo_font;
3949 #endif
3950 #ifdef CONFIG_FONTCONFIG
3952 #endif
3954 #ifdef CONFIG_ASS
3955 ass_library = mp_ass_init();
3956 #endif
3958 #ifdef HAVE_RTC
3959 if(opts->rtc)
3961 char *rtc_device = opts->rtc_device;
3962 // seteuid(0); /* Can't hurt to try to get root here */
3963 if ((rtc_fd = open(rtc_device ? rtc_device : "/dev/rtc", O_RDONLY)) < 0)
3964 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Failed to open %s: %s (it should be readable by the user.)\n",
3965 rtc_device ? rtc_device : "/dev/rtc", strerror(errno));
3966 else {
3967 unsigned long irqp = 1024; /* 512 seemed OK. 128 is jerky. */
3969 if (ioctl(rtc_fd, RTC_IRQP_SET, irqp) < 0) {
3970 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Linux RTC init error in ioctl (rtc_irqp_set %lu): %s\n", irqp, strerror(errno));
3971 mp_tmsg(MSGT_CPLAYER, MSGL_HINT, "Try adding \"echo %lu > /proc/sys/dev/rtc/max-user-freq\" to your system startup scripts.\n", irqp);
3972 close (rtc_fd);
3973 rtc_fd = -1;
3974 } else if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
3975 /* variable only by the root */
3976 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Linux RTC init error in ioctl (rtc_pie_on): %s\n", strerror(errno));
3977 close (rtc_fd);
3978 rtc_fd = -1;
3979 } else
3980 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Using Linux hardware RTC timing (%ldHz).\n", irqp);
3983 if(rtc_fd<0)
3984 #endif /* HAVE_RTC */
3985 mp_msg(MSGT_CPLAYER, MSGL_V, "Using %s timing\n",
3986 opts->softsleep ? "software" : timer_name);
3988 #ifdef HAVE_TERMCAP
3989 load_termcap(NULL); // load key-codes
3990 #endif
3992 // ========== Init keyboard FIFO (connection to libvo) ============
3994 // Init input system
3995 current_module = "init_input";
3996 mpctx->input = mp_input_init(&opts->input);
3997 mp_input_add_key_fd(mpctx->input, -1,0,mplayer_get_key,NULL, mpctx->key_fifo);
3998 if(slave_mode)
3999 mp_input_add_cmd_fd(mpctx->input, 0,USE_SELECT,MP_INPUT_SLAVE_CMD_FUNC,NULL);
4000 else if (opts->consolecontrols)
4001 mp_input_add_key_fd(mpctx->input, 0, 1, read_keys, NULL, mpctx->key_fifo);
4002 // Set the libstream interrupt callback
4003 stream_set_interrupt_callback(mp_input_check_interrupt, mpctx->input);
4005 #ifdef CONFIG_MENU
4006 if(use_menu) {
4007 if(menu_cfg && menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
4008 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
4009 else {
4010 menu_cfg = get_path("menu.conf");
4011 if(menu_init(mpctx, mpctx->mconfig, mpctx->input, menu_cfg))
4012 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", menu_cfg);
4013 else {
4014 if(menu_init(mpctx, mpctx->mconfig, mpctx->input,
4015 MPLAYER_CONFDIR "/menu.conf"))
4016 mp_tmsg(MSGT_CPLAYER, MSGL_V, "Menu initialized: %s\n", MPLAYER_CONFDIR"/menu.conf");
4017 else {
4018 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Menu init failed.\n");
4019 use_menu = 0;
4024 #endif
4026 current_module = NULL;
4028 /// Catch signals
4029 #ifndef __MINGW32__
4030 signal(SIGCHLD,child_sighandler);
4031 #endif
4033 #ifdef CONFIG_CRASH_DEBUG
4034 prog_path = argv[0];
4035 #endif
4036 //========= Catch terminate signals: ================
4037 // terminate requests:
4038 signal(SIGTERM,exit_sighandler); // kill
4039 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed
4041 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
4043 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
4044 signal(SIGPIPE,exit_sighandler); // Some window managers cause this
4045 #ifdef CONFIG_SIGHANDLER
4046 // fatal errors:
4047 signal(SIGBUS,exit_sighandler); // bus error
4048 signal(SIGSEGV,exit_sighandler); // segfault
4049 signal(SIGILL,exit_sighandler); // illegal instruction
4050 signal(SIGFPE,exit_sighandler); // floating point exc.
4051 signal(SIGABRT,exit_sighandler); // abort()
4052 #ifdef CONFIG_CRASH_DEBUG
4053 if (crash_debug)
4054 signal(SIGTRAP,exit_sighandler);
4055 #endif
4056 #endif
4058 // ******************* Now, let's see the per-file stuff ********************
4060 play_next_file:
4062 // init global sub numbers
4063 mpctx->global_sub_size = 0;
4064 memset(mpctx->sub_counts, 0, sizeof(mpctx->sub_counts));
4066 if (mpctx->filename) {
4067 load_per_protocol_config (mpctx->mconfig, mpctx->filename);
4068 load_per_extension_config (mpctx->mconfig, mpctx->filename);
4069 load_per_file_config (mpctx->mconfig, mpctx->filename);
4072 if (opts->video_driver_list)
4073 load_per_output_config (mpctx->mconfig, PROFILE_CFG_VO, opts->video_driver_list[0]);
4074 if (opts->audio_driver_list)
4075 load_per_output_config (mpctx->mconfig, PROFILE_CFG_AO, opts->audio_driver_list[0]);
4077 // We must enable getch2 here to be able to interrupt network connection
4078 // or cache filling
4079 if (opts->consolecontrols && !slave_mode) {
4080 if(mpctx->initialized_flags&INITIALIZED_GETCH2)
4081 mp_tmsg(MSGT_CPLAYER,MSGL_WARN,"WARNING: getch2_init called twice!\n");
4082 else
4083 getch2_enable(); // prepare stdin for hotkeys...
4084 mpctx->initialized_flags|=INITIALIZED_GETCH2;
4085 mp_msg(MSGT_CPLAYER,MSGL_DBG2,"\n[[[init getch2]]]\n");
4088 // =================== GUI idle loop (STOP state) ===========================
4089 while (opts->player_idle_mode && !mpctx->filename) {
4090 play_tree_t * entry = NULL;
4091 mp_cmd_t * cmd;
4092 if (mpctx->video_out && mpctx->video_out->config_ok)
4093 vo_control(mpctx->video_out, VOCTRL_PAUSE, NULL);
4094 while (!(cmd = mp_input_get_cmd(mpctx->input, 0, 0))) { // wait for command
4095 if (mpctx->video_out)
4096 vo_check_events(mpctx->video_out);
4097 usec_sleep(20000);
4099 switch (cmd->id) {
4100 case MP_CMD_LOADFILE:
4101 // prepare a tree entry with the new filename
4102 entry = play_tree_new();
4103 play_tree_add_file(entry, cmd->args[0].v.s);
4104 // The entry is added to the main playtree after the switch().
4105 break;
4106 case MP_CMD_LOADLIST:
4107 entry = parse_playlist_file(mpctx->mconfig, cmd->args[0].v.s);
4108 break;
4109 case MP_CMD_QUIT:
4110 exit_player_with_rc(mpctx, EXIT_QUIT, (cmd->nargs > 0)? cmd->args[0].v.i : 0);
4111 break;
4112 case MP_CMD_VO_FULLSCREEN:
4113 case MP_CMD_GET_PROPERTY:
4114 case MP_CMD_SET_PROPERTY:
4115 case MP_CMD_STEP_PROPERTY:
4116 run_command(mpctx, cmd);
4117 break;
4120 mp_cmd_free(cmd);
4122 if (entry) { // user entered a command that gave a valid entry
4123 if (mpctx->playtree) // the playtree is always a node with one child. let's clear it
4124 play_tree_free_list(mpctx->playtree->child, 1);
4125 else mpctx->playtree=play_tree_new(); // .. or make a brand new playtree
4127 if (!mpctx->playtree) continue; // couldn't make playtree! wait for next command
4129 play_tree_set_child(mpctx->playtree, entry);
4131 /* Make iterator start at the top the of tree. */
4132 mpctx->playtree_iter = play_tree_iter_new(mpctx->playtree, mpctx->mconfig);
4133 if (!mpctx->playtree_iter) continue;
4135 // find the first real item in the tree
4136 if (play_tree_iter_step(mpctx->playtree_iter,0,0) != PLAY_TREE_ITER_ENTRY) {
4137 // no items!
4138 play_tree_iter_free(mpctx->playtree_iter);
4139 mpctx->playtree_iter = NULL;
4140 continue; // wait for next command
4142 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, 1);
4145 //---------------------------------------------------------------------------
4147 if (mpctx->video_out && mpctx->sh_video && mpctx->video_out->config_ok)
4148 vo_control(mpctx->video_out, VOCTRL_RESUME, NULL);
4150 if (mpctx->filename) {
4151 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"\nPlaying %s.\n",
4152 filename_recode(mpctx->filename));
4153 if(use_filename_title && opts->vo_wintitle == NULL)
4154 opts->vo_wintitle = strdup(mp_basename(mpctx->filename));
4157 if (edl_filename) {
4158 if (edl_records) free_edl(edl_records);
4159 next_edl_record = edl_records = edl_parse_file();
4161 if (edl_output_filename) {
4162 if (edl_fd) fclose(edl_fd);
4163 if ((edl_fd = fopen(edl_output_filename, "w")) == NULL)
4165 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Can't open EDL file [%s] for writing.\n",
4166 filename_recode(edl_output_filename));
4170 //==================== Open VOB-Sub ============================
4172 current_module="vobsub";
4173 if (vobsub_name){
4174 vo_vobsub=vobsub_open(vobsub_name,spudec_ifo,1,&vo_spudec);
4175 if(vo_vobsub==NULL)
4176 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
4177 filename_recode(vobsub_name));
4178 } else if (sub_auto && mpctx->filename){
4179 /* try to autodetect vobsub from movie filename ::atmos */
4180 char *buf = strdup(mpctx->filename), *psub;
4181 char *pdot = strrchr(buf, '.');
4182 char *pslash = strrchr(buf, '/');
4183 #if defined(__MINGW32__) || defined(__CYGWIN__)
4184 if (!pslash) pslash = strrchr(buf, '\\');
4185 #endif
4186 if (pdot && (!pslash || pdot > pslash))
4187 *pdot = '\0';
4188 vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
4189 /* try from ~/.mplayer/sub */
4190 if(!vo_vobsub && (psub = get_path( "sub/" ))) {
4191 const char *bname = mp_basename(buf);
4192 int l;
4193 l = strlen(psub) + strlen(bname) + 1;
4194 psub = realloc(psub,l);
4195 strcat(psub,bname);
4196 vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
4197 free(psub);
4199 free(buf);
4201 if(vo_vobsub){
4202 mpctx->initialized_flags|=INITIALIZED_VOBSUB;
4203 vobsub_set_from_lang(vo_vobsub, opts->sub_lang);
4204 mp_property_do("sub_forced_only", M_PROPERTY_SET, &forced_subs_only, mpctx);
4206 // setup global sub numbering
4207 mpctx->sub_counts[SUB_SOURCE_VOBSUB] = vobsub_get_indexes_count(vo_vobsub);
4210 //============ Open & Sync STREAM --- fork cache2 ====================
4212 mpctx->stream=NULL;
4213 mpctx->demuxer=NULL;
4214 mpctx->d_audio=NULL;
4215 mpctx->d_video=NULL;
4216 mpctx->d_sub = NULL;
4217 mpctx->sh_audio=NULL;
4218 mpctx->sh_video=NULL;
4220 current_module="open_stream";
4221 mpctx->stream = open_stream(mpctx->filename, opts, &mpctx->file_format);
4222 if(!mpctx->stream) { // error...
4223 mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY);
4224 goto goto_next_file;
4226 mpctx->initialized_flags|=INITIALIZED_STREAM;
4228 if(mpctx->file_format == DEMUXER_TYPE_PLAYLIST) {
4229 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nThis looks like a playlist, but "
4230 "playlist support will not be used automatically.\n"
4231 "MPlayer's playlist code is unsafe and should only be used with "
4232 "trusted sources.\nPlayback will probably fail.\n\n");
4233 #if 0
4234 play_tree_t* entry;
4235 // Handle playlist
4236 current_module="handle_playlist";
4237 mp_msg(MSGT_CPLAYER,MSGL_V,"Parsing playlist %s...\n",
4238 filename_recode(mpctx->filename));
4239 entry = parse_playtree(mpctx->stream, mpctx->mconfig, 0);
4240 mpctx->eof=playtree_add_playlist(mpctx, entry);
4241 goto goto_next_file;
4242 #endif
4244 mpctx->stream->start_pos+=seek_to_byte;
4246 if(stream_dump_type==5){
4247 unsigned char buf[4096];
4248 int len;
4249 FILE *f;
4250 current_module="dumpstream";
4251 stream_reset(mpctx->stream);
4252 stream_seek(mpctx->stream,mpctx->stream->start_pos);
4253 f=fopen(opts->stream_dump_name,"wb");
4254 if(!f){
4255 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
4256 exit_player(mpctx, EXIT_ERROR);
4258 if (opts->chapterrange[0] > 1) {
4259 int chapter = opts->chapterrange[0] - 1;
4260 stream_control(mpctx->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
4262 while(!mpctx->stream->eof && !async_quit_request){
4263 len=stream_read(mpctx->stream,buf,4096);
4264 if(len>0) {
4265 if(fwrite(buf,len,1,f) != 1) {
4266 mp_tmsg(MSGT_GLOBAL,MSGL_FATAL,"%s: Error writing file.\n",opts->stream_dump_name);
4267 exit_player(mpctx, EXIT_ERROR);
4270 if (opts->chapterrange[1] > 0) {
4271 int chapter = -1;
4272 if (stream_control(mpctx->stream, STREAM_CTRL_GET_CURRENT_CHAPTER,
4273 &chapter) == STREAM_OK
4274 && chapter + 1 > opts->chapterrange[1])
4275 break;
4278 if(fclose(f)) {
4279 mp_tmsg(MSGT_GLOBAL,MSGL_FATAL,"%s: Error writing file.\n",opts->stream_dump_name);
4280 exit_player(mpctx, EXIT_ERROR);
4282 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Stream dump complete.\n");
4283 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4286 #ifdef CONFIG_DVDREAD
4287 if(mpctx->stream->type==STREAMTYPE_DVD){
4288 current_module="dvd lang->id";
4289 if(opts->audio_lang && opts->audio_id==-1) opts->audio_id=dvd_aid_from_lang(mpctx->stream,opts->audio_lang);
4290 if(opts->sub_lang && opts->sub_id==-1) opts->sub_id=dvd_sid_from_lang(mpctx->stream,opts->sub_lang);
4291 // setup global sub numbering
4292 mpctx->sub_counts[SUB_SOURCE_DEMUX] = dvd_number_of_subs(mpctx->stream);
4293 current_module=NULL;
4295 #endif
4297 #ifdef CONFIG_DVDNAV
4298 if(mpctx->stream->type==STREAMTYPE_DVDNAV){
4299 current_module="dvdnav lang->id";
4300 if(opts->audio_lang && opts->audio_id==-1) opts->audio_id=mp_dvdnav_aid_from_lang(mpctx->stream,opts->audio_lang);
4301 dvdsub_lang_id = -3;
4302 if(opts->sub_lang && opts->sub_id==-1)
4303 dvdsub_lang_id = opts->sub_id = mp_dvdnav_sid_from_lang(mpctx->stream,opts->sub_lang);
4304 // setup global sub numbering
4305 mpctx->sub_counts[SUB_SOURCE_DEMUX] = mp_dvdnav_number_of_subs(mpctx->stream);
4306 current_module=NULL;
4308 #endif
4310 // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts)
4311 goto_enable_cache:
4312 if(stream_cache_size>0){
4313 int res;
4314 float stream_cache_min_percent = opts->stream_cache_min_percent;
4315 float stream_cache_seek_min_percent = opts->stream_cache_seek_min_percent;
4316 current_module="enable_cache";
4317 res = stream_enable_cache(mpctx->stream,stream_cache_size*1024,
4318 stream_cache_size*1024*(stream_cache_min_percent / 100.0),
4319 stream_cache_size*1024*(stream_cache_seek_min_percent / 100.0));
4320 if(res == 0)
4321 if((mpctx->stop_play = libmpdemux_was_interrupted(mpctx, PT_NEXT_ENTRY))) goto goto_next_file;
4324 //============ Open DEMUXERS --- DETECT file type =======================
4325 current_module="demux_open";
4327 mpctx->demuxer=demux_open(opts, mpctx->stream,mpctx->file_format,opts->audio_id,opts->video_id,opts->sub_id,mpctx->filename);
4329 // HACK to get MOV Reference Files working
4331 if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
4333 unsigned char* playlist_entry;
4334 play_tree_t *list = NULL, *entry = NULL;
4336 current_module="handle_demux_playlist";
4337 while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
4339 char *temp;
4340 const char *bname;
4342 mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
4343 filename_recode(playlist_entry));
4345 bname=mp_basename(playlist_entry);
4346 if ((strlen(bname)>10) && !strncmp(bname,"qt",2) && !strncmp(bname+3,"gateQT",6))
4347 continue;
4349 if (!strcmp(playlist_entry, mpctx->filename)) // ignoring self-reference
4350 continue;
4352 entry = play_tree_new();
4354 if (mpctx->filename && !strcmp(mp_basename(playlist_entry),playlist_entry)) // add reference path of current file
4356 temp=malloc((strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))+strlen(playlist_entry)+1));
4357 if (temp)
4359 strncpy(temp, mpctx->filename, strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename)));
4360 temp[strlen(mpctx->filename)-strlen(mp_basename(mpctx->filename))]='\0';
4361 strcat(temp, playlist_entry);
4362 if (!strcmp(temp, mpctx->filename)) {
4363 free(temp);
4364 continue;
4366 play_tree_add_file(entry,temp);
4367 mp_msg(MSGT_CPLAYER,MSGL_V,"Resolving reference to %s.\n",temp);
4368 free(temp);
4371 else
4372 play_tree_add_file(entry,playlist_entry);
4374 if(!list)
4375 list = entry;
4376 else
4377 play_tree_append_entry(list,entry);
4379 free_demuxer(mpctx->demuxer);
4380 mpctx->demuxer = NULL;
4382 if (list)
4384 entry = play_tree_new();
4385 play_tree_set_child(entry,list);
4386 mpctx->stop_play = playtree_add_playlist(mpctx, entry);
4387 goto goto_next_file;
4391 if(!mpctx->demuxer) {
4392 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Failed to recognize file format.\n");
4393 goto goto_next_file;
4396 if (mpctx->demuxer->matroska_data.ordered_chapters)
4397 build_ordered_chapter_timeline(mpctx);
4399 if (mpctx->demuxer->type == DEMUXER_TYPE_EDL)
4400 build_edl_timeline(mpctx);
4402 if (mpctx->timeline) {
4403 mpctx->timeline_part = 0;
4404 mpctx->demuxer = mpctx->timeline[0].source->demuxer;
4406 int part_count = mpctx->num_timeline_parts;
4407 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline contains %d parts from %d "
4408 "sources. Total length %.3f seconds.\n", part_count,
4409 mpctx->num_sources, mpctx->timeline[part_count].start);
4410 mp_msg(MSGT_CPLAYER, MSGL_V, "Source files:\n");
4411 for (int i = 0; i < mpctx->num_sources; i++)
4412 mp_msg(MSGT_CPLAYER, MSGL_V, "%d: %s\n", i,
4413 filename_recode(mpctx->sources[i].demuxer->filename));
4414 mp_msg(MSGT_CPLAYER, MSGL_V, "Timeline parts: (number, start, "
4415 "source_start, source):\n");
4416 for (int i = 0; i < part_count; i++) {
4417 struct timeline_part *p = mpctx->timeline + i;
4418 mp_msg(MSGT_CPLAYER, MSGL_V, "%3d %9.3f %9.3f %3td\n", i, p->start,
4419 p->source_start, p->source - mpctx->sources);
4421 mp_msg(MSGT_CPLAYER, MSGL_V, "END %9.3f\n",
4422 mpctx->timeline[part_count].start);
4425 if (!mpctx->sources) {
4426 mpctx->sources = talloc_ptrtype(NULL, mpctx->sources);
4427 *mpctx->sources = (struct content_source){.stream = mpctx->stream,
4428 .demuxer = mpctx->demuxer};
4429 mpctx->num_sources = 1;
4432 mpctx->initialized_flags|=INITIALIZED_DEMUXER;
4434 #ifdef CONFIG_ASS
4435 if (opts->ass_enabled && ass_library) {
4436 for (int j = 0; j < mpctx->num_sources; j++) {
4437 struct demuxer *d = mpctx->sources[j].demuxer;
4438 for (int i = 0; i < d->num_attachments; i++) {
4439 struct demux_attachment *att = d->attachments + i;
4440 if (use_embedded_fonts && attachment_is_font(att))
4441 ass_add_font(ass_library, att->name, att->data, att->data_size);
4445 #endif
4447 current_module="demux_open2";
4449 mpctx->d_audio=mpctx->demuxer->audio;
4450 mpctx->d_video=mpctx->demuxer->video;
4451 mpctx->d_sub=mpctx->demuxer->sub;
4453 if (ts_prog) {
4454 int tmp = ts_prog;
4455 mp_property_do("switch_program", M_PROPERTY_SET, &tmp, mpctx);
4457 // select audio stream
4458 for (int i = 0; i < mpctx->num_sources; i++)
4459 select_audio(mpctx->sources[i].demuxer->audio->demuxer, opts->audio_id,
4460 opts->audio_lang);
4462 // DUMP STREAMS:
4463 if((stream_dump_type)&&(stream_dump_type<4)){
4464 FILE *f;
4465 demux_stream_t *ds=NULL;
4466 current_module="dump";
4467 // select stream to dump
4468 switch(stream_dump_type){
4469 case 1: ds=mpctx->d_audio;break;
4470 case 2: ds=mpctx->d_video;break;
4471 case 3: ds=mpctx->d_sub;break;
4473 if(!ds){
4474 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"dump: FATAL: Selected stream missing!\n");
4475 exit_player(mpctx, EXIT_ERROR);
4477 // disable other streams:
4478 if(mpctx->d_audio && mpctx->d_audio!=ds) {ds_free_packs(mpctx->d_audio); mpctx->d_audio->id=-2; }
4479 if(mpctx->d_video && mpctx->d_video!=ds) {ds_free_packs(mpctx->d_video); mpctx->d_video->id=-2; }
4480 if(mpctx->d_sub && mpctx->d_sub!=ds) {ds_free_packs(mpctx->d_sub); mpctx->d_sub->id=-2; }
4481 // let's dump it!
4482 f=fopen(opts->stream_dump_name,"wb");
4483 if(!f){
4484 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Cannot open dump file.\n");
4485 exit_player(mpctx, EXIT_ERROR);
4487 while(!ds->eof){
4488 unsigned char* start;
4489 int in_size=ds_get_packet(ds,&start);
4490 if( (mpctx->demuxer->file_format==DEMUXER_TYPE_AVI || mpctx->demuxer->file_format==DEMUXER_TYPE_ASF || mpctx->demuxer->file_format==DEMUXER_TYPE_MOV)
4491 && stream_dump_type==2) fwrite(&in_size,1,4,f);
4492 if(in_size>0) fwrite(start,in_size,1,f);
4493 if (opts->chapterrange[1] > 0) {
4494 int cur_chapter = demuxer_get_current_chapter(mpctx->demuxer, 0);
4495 if(cur_chapter!=-1 && cur_chapter+1 > opts->chapterrange[1])
4496 break;
4499 fclose(f);
4500 mp_tmsg(MSGT_CPLAYER ,MSGL_INFO, "Stream dump complete.\n");
4501 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4504 mpctx->sh_audio=mpctx->d_audio->sh;
4505 mpctx->sh_video=mpctx->d_video->sh;
4507 if(mpctx->sh_video){
4509 current_module="video_read_properties";
4510 if(!video_read_properties(mpctx->sh_video)) {
4511 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Video: Cannot read properties.\n");
4512 mpctx->sh_video=mpctx->d_video->sh=NULL;
4513 } else {
4514 mp_tmsg(MSGT_CPLAYER,MSGL_V,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
4515 mpctx->demuxer->file_format,mpctx->sh_video->format, mpctx->sh_video->disp_w,mpctx->sh_video->disp_h,
4516 mpctx->sh_video->fps,mpctx->sh_video->frametime
4519 /* need to set fps here for output encoders to pick it up in their init */
4520 if(force_fps){
4521 mpctx->sh_video->fps=force_fps;
4522 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
4524 vo_fps = mpctx->sh_video->fps;
4526 if(!mpctx->sh_video->fps && !force_fps){
4527 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"FPS not specified in the header or invalid, use the -fps option.\n");
4528 mpctx->opts.correct_pts = 1;
4534 if(!mpctx->sh_video && !mpctx->sh_audio){
4535 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL, "No stream found.\n");
4536 #ifdef CONFIG_DVBIN
4537 if(mpctx->stream->type == STREAMTYPE_DVB)
4539 int dir;
4540 int v = mpctx->last_dvb_step;
4541 if(v > 0)
4542 dir = DVB_CHANNEL_HIGHER;
4543 else
4544 dir = DVB_CHANNEL_LOWER;
4546 if(dvb_step_channel(mpctx->stream, dir)) {
4547 mpctx->stop_play = PT_NEXT_ENTRY;
4548 mpctx->dvbin_reopen = 1;
4551 #endif
4552 goto goto_next_file; // exit_player(_("Fatal error"));
4555 /* display clip info */
4556 demux_info_print(mpctx->demuxer);
4558 //================== Read SUBTITLES (DVD & TEXT) ==========================
4559 if(vo_spudec==NULL &&
4560 (mpctx->stream->type==STREAMTYPE_DVD || mpctx->stream->type == STREAMTYPE_DVDNAV)){
4561 init_vo_spudec(mpctx);
4564 // after reading video params we should load subtitles because
4565 // we know fps so now we can adjust subtitle time to ~6 seconds AST
4566 // check .sub
4567 current_module="read_subtitles_file";
4568 double sub_fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
4569 if(sub_name){
4570 for (i = 0; sub_name[i] != NULL; ++i)
4571 add_subtitles(mpctx, sub_name[i], sub_fps, 0);
4573 if(sub_auto) { // auto load sub file ...
4574 char *psub = get_path( "sub/" );
4575 char **tmp = sub_filenames((psub ? psub : ""), mpctx->filename);
4576 int i = 0;
4577 free(psub); // release the buffer created by get_path() above
4578 while (tmp[i]) {
4579 add_subtitles(mpctx, tmp[i], sub_fps, 1);
4580 free(tmp[i++]);
4582 free(tmp);
4584 if (mpctx->set_of_sub_size > 0)
4585 mpctx->sub_counts[SUB_SOURCE_SUBS] = mpctx->set_of_sub_size;
4587 if (select_subtitle(mpctx)) {
4588 if(mpctx->subdata)
4589 switch (stream_dump_type) {
4590 case 3: list_sub_file(mpctx->subdata); break;
4591 case 4: dump_mpsub(mpctx->subdata, mpctx->sh_video->fps); break;
4592 case 6: dump_srt(mpctx->subdata, mpctx->sh_video->fps); break;
4593 case 7: dump_microdvd(mpctx->subdata, mpctx->sh_video->fps); break;
4594 case 8: dump_jacosub(mpctx->subdata, mpctx->sh_video->fps); break;
4595 case 9: dump_sami(mpctx->subdata, mpctx->sh_video->fps); break;
4599 print_file_properties(mpctx, mpctx->filename);
4601 reinit_video_chain(mpctx);
4602 if (mpctx->sh_video) {
4603 if(mpctx->sh_video->output_flags & VFCAP_SPU && vo_spudec)
4604 spudec_set_hw_spu(vo_spudec,mpctx->video_out);
4605 #ifdef CONFIG_FREETYPE
4606 force_load_font = 1;
4607 #endif
4608 } else if (!mpctx->sh_audio)
4609 goto goto_next_file;
4611 //================== MAIN: ==========================
4612 current_module="main";
4614 if (opts->playing_msg) {
4615 char* msg = property_expand_string(mpctx, opts->playing_msg);
4616 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",msg);
4617 free(msg);
4621 // Disable the term OSD in verbose mode
4622 if (verbose)
4623 opts->term_osd = 0;
4627 // Make sure old OSD does not stay around,
4628 // e.g. with -fixed-vo and same-resolution files
4629 clear_osd_msgs();
4630 update_osd_msg(mpctx);
4632 //================ SETUP AUDIO ==========================
4634 if(mpctx->sh_audio){
4635 reinit_audio_chain(mpctx);
4636 if (mpctx->sh_audio && mpctx->sh_audio->codec)
4637 mp_msg(MSGT_IDENTIFY,MSGL_INFO, "ID_AUDIO_CODEC=%s\n", mpctx->sh_audio->codec->name);
4640 current_module="av_init";
4642 if(mpctx->sh_video){
4643 mpctx->sh_video->timer=0;
4644 if (! ignore_start)
4645 audio_delay += mpctx->sh_video->stream_delay;
4647 if(mpctx->sh_audio){
4648 if (start_volume >= 0)
4649 mixer_setvolume(&mpctx->mixer, start_volume, start_volume);
4650 if (! ignore_start)
4651 audio_delay -= mpctx->sh_audio->stream_delay;
4652 mpctx->delay=-audio_delay;
4655 if(!mpctx->sh_audio){
4656 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Audio: no sound\n");
4657 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused audio chunks.\n",mpctx->d_audio->packs);
4658 ds_free_packs(mpctx->d_audio); // free buffered chunks
4659 //mpctx->d_audio->id=-2; // do not read audio chunks
4660 //uninit_player(mpctx, INITIALIZED_AO); // close device
4662 if(!mpctx->sh_video){
4663 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Video: no video\n");
4664 mp_msg(MSGT_CPLAYER,MSGL_V,"Freeing %d unused video chunks.\n",mpctx->d_video->packs);
4665 ds_free_packs(mpctx->d_video);
4666 mpctx->d_video->id=-2;
4667 //if(!fixed_vo) uninit_player(mpctx, INITIALIZED_VO);
4670 if (!mpctx->sh_video && !mpctx->sh_audio)
4671 goto goto_next_file;
4673 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
4674 if(force_fps && mpctx->sh_video){
4675 vo_fps = mpctx->sh_video->fps=force_fps;
4676 mpctx->sh_video->frametime=1.0f/mpctx->sh_video->fps;
4677 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"FPS forced to be %5.3f (ftime: %5.3f).\n",mpctx->sh_video->fps,mpctx->sh_video->frametime);
4680 mp_input_set_section(mpctx->input, NULL);
4681 //TODO: add desired (stream-based) sections here
4682 if (mpctx->stream->type==STREAMTYPE_TV) mp_input_set_section(mpctx->input, "tv");
4683 if (mpctx->stream->type==STREAMTYPE_DVDNAV) mp_input_set_section(mpctx->input, "dvdnav");
4685 //==================== START PLAYING =======================
4687 if(opts->loop_times>1) opts->loop_times--; else
4688 if(opts->loop_times==1) opts->loop_times = -1;
4690 mp_tmsg(MSGT_CPLAYER,MSGL_INFO,"Starting playback...\n");
4692 total_time_usage_start=GetTimer();
4693 audio_time_usage=0; video_time_usage=0; vout_time_usage=0;
4694 total_frame_cnt=0; drop_frame_cnt=0; // fix for multifile fps benchmark
4695 play_n_frames=play_n_frames_mf;
4697 if(play_n_frames==0){
4698 mpctx->stop_play=PT_NEXT_ENTRY; goto goto_next_file;
4701 mpctx->time_frame = 0;
4702 mpctx->drop_message_shown = 0;
4703 mpctx->restart_playback = true;
4704 mpctx->video_pts = 0;
4705 mpctx->hrseek_active = false;
4706 mpctx->hrseek_framedrop = false;
4707 mpctx->step_frames = 0;
4708 mpctx->total_avsync_change = 0;
4709 mpctx->last_chapter_seek = -1;
4711 // If there's a timeline force an absolute seek to initialize state
4712 if (seek_to_sec || mpctx->timeline) {
4713 queue_seek(mpctx, MPSEEK_ABSOLUTE, seek_to_sec, 0);
4714 seek(mpctx, mpctx->seek, false);
4715 end_at.pos += seek_to_sec;
4717 if (opts->chapterrange[0] > 0) {
4718 double pts;
4719 if (seek_chapter(mpctx, opts->chapterrange[0]-1, &pts, NULL) >= 0
4720 && pts > -1.0) {
4721 queue_seek(mpctx, MPSEEK_ABSOLUTE, pts, 0);
4722 seek(mpctx, mpctx->seek, false);
4726 if (end_at.type == END_AT_SIZE) {
4727 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Option -endpos in MPlayer does not yet support size units.\n");
4728 end_at.type = END_AT_NONE;
4731 #ifdef CONFIG_DVDNAV
4732 mp_dvdnav_context_free(mpctx);
4733 if (mpctx->stream->type == STREAMTYPE_DVDNAV) {
4734 mp_dvdnav_read_wait(mpctx->stream, 0, 1);
4735 mp_dvdnav_cell_has_changed(mpctx->stream,1);
4737 #endif
4739 mpctx->seek = (struct seek_params){0};
4740 get_relative_time(mpctx); // reset current delta
4741 // Make sure VO knows current pause state
4742 if (mpctx->sh_video)
4743 vo_control(mpctx->video_out, mpctx->paused ? VOCTRL_PAUSE : VOCTRL_RESUME,
4744 NULL);
4746 while (!mpctx->stop_play)
4747 run_playloop(mpctx);
4749 mp_msg(MSGT_GLOBAL,MSGL_V,"EOF code: %d \n",mpctx->stop_play);
4751 #ifdef CONFIG_DVBIN
4752 if(mpctx->dvbin_reopen)
4754 mpctx->stop_play = 0;
4755 uninit_player(mpctx, INITIALIZED_ALL-(INITIALIZED_STREAM|INITIALIZED_GETCH2|(opts->fixed_vo?INITIALIZED_VO:0)));
4756 cache_uninit(mpctx->stream);
4757 mpctx->dvbin_reopen = 0;
4758 goto goto_enable_cache;
4760 #endif
4763 goto_next_file: // don't jump here after ao/vo/getch initialization!
4765 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n");
4767 if (opts->benchmark) {
4768 double tot=video_time_usage+vout_time_usage+audio_time_usage;
4769 double total_time_usage;
4770 total_time_usage_start=GetTimer()-total_time_usage_start;
4771 total_time_usage = (float)total_time_usage_start*0.000001;
4772 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\nBENCHMARKs: VC:%8.3fs VO:%8.3fs A:%8.3fs Sys:%8.3fs = %8.3fs\n",
4773 video_time_usage,vout_time_usage,audio_time_usage,
4774 total_time_usage-tot,total_time_usage);
4775 if(total_time_usage>0.0)
4776 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARK%%: VC:%8.4f%% VO:%8.4f%% A:%8.4f%% Sys:%8.4f%% = %8.4f%%\n",
4777 100.0*video_time_usage/total_time_usage,
4778 100.0*vout_time_usage/total_time_usage,
4779 100.0*audio_time_usage/total_time_usage,
4780 100.0*(total_time_usage-tot)/total_time_usage,
4781 100.0);
4782 if(total_frame_cnt && frame_dropping)
4783 mp_msg(MSGT_CPLAYER,MSGL_INFO,"BENCHMARKn: disp: %d (%3.2f fps) drop: %d (%d%%) total: %d (%3.2f fps)\n",
4784 total_frame_cnt-drop_frame_cnt,
4785 (total_time_usage>0.5)?((total_frame_cnt-drop_frame_cnt)/total_time_usage):0,
4786 drop_frame_cnt,
4787 100*drop_frame_cnt/total_frame_cnt,
4788 total_frame_cnt,
4789 (total_time_usage>0.5)?(total_frame_cnt/total_time_usage):0);
4792 // time to uninit all, except global stuff:
4793 int uninitialize_parts = INITIALIZED_ALL;
4794 if (opts->fixed_vo)
4795 uninitialize_parts -= INITIALIZED_VO;
4796 if (opts->gapless_audio && mpctx->stop_play == AT_END_OF_FILE)
4797 uninitialize_parts -= INITIALIZED_AO;
4798 uninit_player(mpctx, uninitialize_parts);
4800 if(mpctx->set_of_sub_size > 0) {
4801 current_module="sub_free";
4802 for(i = 0; i < mpctx->set_of_sub_size; ++i) {
4803 sub_free(mpctx->set_of_subtitles[i]);
4804 #ifdef CONFIG_ASS
4805 if(mpctx->set_of_ass_tracks[i])
4806 ass_free_track( mpctx->set_of_ass_tracks[i] );
4807 #endif
4809 mpctx->set_of_sub_size = 0;
4811 mpctx->vo_sub_last = vo_sub=NULL;
4812 mpctx->subdata=NULL;
4813 #ifdef CONFIG_ASS
4814 mpctx->osd->ass_track = NULL;
4815 if(ass_library)
4816 ass_clear_fonts(ass_library);
4817 #endif
4819 if (!mpctx->stop_play) // In case some goto jumped here...
4820 mpctx->stop_play = PT_NEXT_ENTRY;
4822 int playtree_direction = 1;
4824 if(mpctx->stop_play == PT_NEXT_ENTRY || mpctx->stop_play == PT_PREV_ENTRY) {
4825 if(play_tree_iter_step(mpctx->playtree_iter,mpctx->play_tree_step,0) != PLAY_TREE_ITER_ENTRY) {
4826 play_tree_iter_free(mpctx->playtree_iter);
4827 mpctx->playtree_iter = NULL;
4829 mpctx->play_tree_step = 1;
4830 } else if(mpctx->stop_play == PT_UP_NEXT || mpctx->stop_play == PT_UP_PREV) {
4831 int direction = mpctx->stop_play == PT_UP_NEXT ? 1 : -1;
4832 if(mpctx->playtree_iter) {
4833 if(play_tree_iter_up_step(mpctx->playtree_iter,direction,0) != PLAY_TREE_ITER_ENTRY) {
4834 play_tree_iter_free(mpctx->playtree_iter);
4835 mpctx->playtree_iter = NULL;
4838 } else if (mpctx->stop_play == PT_STOP) {
4839 play_tree_iter_free(mpctx->playtree_iter);
4840 mpctx->playtree_iter = NULL;
4841 } else { // NEXT PREV SRC
4842 playtree_direction = mpctx->stop_play == PT_PREV_SRC ? -1 : 1;
4845 while(mpctx->playtree_iter != NULL) {
4846 mpctx->filename = play_tree_iter_get_file(mpctx->playtree_iter, playtree_direction);
4847 if(mpctx->filename == NULL) {
4848 if(play_tree_iter_step(mpctx->playtree_iter, playtree_direction, 0) != PLAY_TREE_ITER_ENTRY) {
4849 play_tree_iter_free(mpctx->playtree_iter);
4850 mpctx->playtree_iter = NULL;
4852 } else
4853 break;
4856 if (mpctx->playtree_iter != NULL || opts->player_idle_mode) {
4857 if(!mpctx->playtree_iter) mpctx->filename = NULL;
4858 mpctx->stop_play = 0;
4859 goto play_next_file;
4863 exit_player_with_rc(mpctx, EXIT_EOF, 0);
4865 return 1;
4867 #endif /* DISABLE_MAIN */