/*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <math.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mpv_talloc.h"
#include "config.h"
#include "osdep/timer.h"
#include "osdep/threads.h"
#include "misc/dispatch.h"
#include "misc/rendezvous.h"
#include "options/options.h"
#include "misc/bstr.h"
#include "vo.h"
#include "aspect.h"
#include "dr_helper.h"
#include "input/input.h"
#include "options/m_config.h"
#include "common/msg.h"
#include "common/global.h"
#include "common/stats.h"
#include "video/hwdec.h"
#include "video/mp_image.h"
#include "sub/osd.h"
#include "osdep/io.h"
#include "osdep/threads.h"
extern const struct vo_driver video_out_mediacodec_embed;
extern const struct vo_driver video_out_x11;
extern const struct vo_driver video_out_vdpau;
extern const struct vo_driver video_out_xv;
extern const struct vo_driver video_out_gpu;
extern const struct vo_driver video_out_gpu_next;
extern const struct vo_driver video_out_libmpv;
extern const struct vo_driver video_out_null;
extern const struct vo_driver video_out_image;
extern const struct vo_driver video_out_lavc;
extern const struct vo_driver video_out_caca;
extern const struct vo_driver video_out_drm;
extern const struct vo_driver video_out_direct3d;
extern const struct vo_driver video_out_sdl;
extern const struct vo_driver video_out_vaapi;
extern const struct vo_driver video_out_dmabuf_wayland;
extern const struct vo_driver video_out_wlshm;
extern const struct vo_driver video_out_tct;
extern const struct vo_driver video_out_sixel;
extern const struct vo_driver video_out_kitty;
static const struct vo_driver *const video_out_drivers[] =
{
#if HAVE_ANDROID
&video_out_mediacodec_embed,
#endif
&video_out_gpu,
&video_out_gpu_next,
#if HAVE_VDPAU
&video_out_vdpau,
#endif
#if HAVE_DIRECT3D
&video_out_direct3d,
#endif
#if HAVE_WAYLAND && HAVE_MEMFD_CREATE
&video_out_wlshm,
#endif
#if HAVE_XV
&video_out_xv,
#endif
#if HAVE_SDL2_VIDEO
&video_out_sdl,
#endif
#if HAVE_DMABUF_WAYLAND
&video_out_dmabuf_wayland,
#endif
#if HAVE_VAAPI_X11 && HAVE_GPL
&video_out_vaapi,
#endif
#if HAVE_X11
&video_out_x11,
#endif
&video_out_libmpv,
&video_out_null,
// should not be auto-selected
&video_out_image,
&video_out_tct,
#if HAVE_CACA
&video_out_caca,
#endif
#if HAVE_DRM
&video_out_drm,
#endif
#if HAVE_SIXEL
&video_out_sixel,
#endif
&video_out_kitty,
&video_out_lavc,
};
struct vo_internal {
mp_thread thread;
struct mp_dispatch_queue *dispatch;
struct dr_helper *dr_helper;
// --- The following fields are protected by lock
mp_mutex lock;
mp_cond wakeup;
bool need_wakeup;
bool terminate;
bool hasframe;
bool hasframe_rendered;
bool request_redraw; // redraw request from player to VO
bool want_redraw; // redraw request from VO to player
bool send_reset; // send VOCTRL_RESET
bool paused;
bool wakeup_on_done;
int queued_events; // event mask for the user
int internal_events; // event mask for us
double nominal_vsync_interval;
double vsync_interval;
int64_t *vsync_samples;
int num_vsync_samples;
int64_t num_total_vsync_samples;
int64_t prev_vsync;
double base_vsync;
int drop_point;
double estimated_vsync_interval;
double estimated_vsync_jitter;
bool expecting_vsync;
int64_t num_successive_vsyncs;
int64_t flip_queue_offset; // queue flip events at most this much in advance
int64_t timing_offset; // same (but from options; not VO configured)
int64_t delayed_count;
int64_t drop_count;
bool dropped_frame; // the previous frame was dropped
struct vo_frame *current_frame; // last frame queued to the VO
int64_t wakeup_pts; // time at which to pull frame from decoder
bool rendering; // true if an image is being rendered
struct vo_frame *frame_queued; // should be drawn next
int req_frames; // VO's requested value of num_frames
uint64_t current_frame_id;
double display_fps;
double reported_display_fps;
struct stats_ctx *stats;
};
extern const struct m_sub_options gl_video_conf;
static void forget_frames(struct vo *vo);
static MP_THREAD_VOID vo_thread(void *ptr);
static bool get_desc(struct m_obj_desc *dst, int index)
{
if (index >= MP_ARRAY_SIZE(video_out_drivers))
return false;
const struct vo_driver *vo = video_out_drivers[index];
*dst = (struct m_obj_desc) {
.name = vo->name,
.description = vo->description,
.priv_size = vo->priv_size,
.priv_defaults = vo->priv_defaults,
.options = vo->options,
.options_prefix = vo->options_prefix,
.global_opts = vo->global_opts,
.hidden = vo->encode,
.p = vo,
};
return true;
}
// For the vo option
const struct m_obj_list vo_obj_list = {
.get_desc = get_desc,
.description = "video outputs",
.aliases = {
{"gl", "gpu"},
{"direct3d_shaders", "direct3d"},
{"opengl", "gpu"},
{"opengl-cb", "libmpv"},
{0}
},
.allow_trailer = true,
.disallow_positional_parameters = true,
.use_global_options = true,
};
static void dispatch_wakeup_cb(void *ptr)
{
struct vo *vo = ptr;
vo_wakeup(vo);
}
// Initialize or update options from vo->opts
static void read_opts(struct vo *vo)
{
struct vo_internal *in = vo->in;
mp_mutex_lock(&in->lock);
in->timing_offset = (uint64_t)(MP_TIME_S_TO_NS(vo->opts->timing_offset));
mp_mutex_unlock(&in->lock);
}
static void update_opts(void *p)
{
struct vo *vo = p;
if (m_config_cache_update(vo->opts_cache)) {
read_opts(vo);
if (vo->driver->control) {
vo->driver->control(vo, VOCTRL_VO_OPTS_CHANGED, NULL);
// "Legacy" update of video position related options.
// Unlike VOCTRL_VO_OPTS_CHANGED, often not propagated to backends.
vo->driver->control(vo, VOCTRL_SET_PANSCAN, NULL);
}
}
}
// Does not include thread- and VO uninit.
static void dealloc_vo(struct vo *vo)
{
forget_frames(vo); // implicitly synchronized
// These must be free'd before vo->in->dispatch.
talloc_free(vo->opts_cache);
talloc_free(vo->gl_opts_cache);
talloc_free(vo->eq_opts_cache);
mp_mutex_destroy(&vo->params_mutex);
mp_mutex_destroy(&vo->in->lock);
mp_cond_destroy(&vo->in->wakeup);
talloc_free(vo);
}
static struct vo *vo_create(bool probing, struct mpv_global *global,
struct vo_extra *ex, char *name)
{
assert(ex->wakeup_cb);
struct mp_log *log = mp_log_new(NULL, global->log, "vo");
struct m_obj_desc desc;
if (!m_obj_list_find(&desc, &vo_obj_list, bstr0(name))) {
mp_msg(log, MSGL_ERR, "Video output %s not found!\n", name);
talloc_free(log);
return NULL;
};
struct vo *vo = talloc_ptrtype(NULL, vo);
*vo = (struct vo) {
.log = mp_log_new(vo, log, name),
.driver = desc.p,
.global = global,
.encode_lavc_ctx = ex->encode_lavc_ctx,
.input_ctx = ex->input_ctx,
.osd = ex->osd,
.monitor_par = 1,
.extra = *ex,
.probing = probing,
.in = talloc(vo, struct vo_internal),
};
mp_mutex_init(&vo->params_mutex);
talloc_steal(vo, log);
*vo->in = (struct vo_internal) {
.dispatch = mp_dispatch_create(vo),
.req_frames = 1,
|