From 76276c92104c31ee936ba5c76a76072f09978c5f Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 16 Jan 2018 11:53:44 +0100 Subject: video: rewrite filtering glue code Get rid of the old vf.c code. Replace it with a generic filtering framework, which can potentially handle more than just --vf. At least reimplementing --af with this code is planned. This changes some --vf semantics (including runtime behavior and the "vf" command). The most important ones are listed in interface-changes. vf_convert.c is renamed to f_swscale.c. It is now an internal filter that can not be inserted by the user manually. f_lavfi.c is a refactor of player/lavfi.c. The latter will be removed once --lavfi-complex is reimplemented on top of f_lavfi.c. (which is conceptually easy, but a big mess due to the data flow changes). The existing filters are all changed heavily. The data flow of the new filter framework is different. Especially EOF handling changes - EOF is now a "frame" rather than a state, and must be passed through exactly once. Another major thing is that all filters must support dynamic format changes. The filter reconfig() function goes away. (This sounds complex, but since all filters need to handle EOF draining anyway, they can use the same code, and it removes the mess with reconfig() having to predict the output format, which completely breaks with libavfilter anyway.) In addition, there is no automatic format negotiation or conversion. libavfilter's primitive and insufficient API simply doesn't allow us to do this in a reasonable way. Instead, filters can use f_autoconvert as sub-filter, and tell it which formats they support. This filter will in turn add actual conversion filters, such as f_swscale, to perform necessary format changes. vf_vapoursynth.c uses the same basic principle of operation as before, but with worryingly different details in data flow. Still appears to work. The hardware deint filters (vf_vavpp.c, vf_d3d11vpp.c, vf_vdpaupp.c) are heavily changed. Fortunately, they all used refqueue.c, which is for sharing the data flow logic (especially for managing future/past surfaces and such). It turns out it can be used to factor out most of the data flow. Some of these filters accepted software input. Instead of having ad-hoc upload code in each filter, surface upload is now delegated to f_autoconvert, which can use f_hwupload to perform this. Exporting VO capabilities is still a big mess (mp_stream_info stuff). The D3D11 code drops the redundant image formats, and all code uses the hw_subfmt (sw_format in FFmpeg) instead. Although that too seems to be a big mess for now. f_async_queue is unused. --- video/filter/refqueue.c | 192 ++++++++-- video/filter/refqueue.h | 18 +- video/filter/vf.c | 797 ------------------------------------------ video/filter/vf.h | 179 ---------- video/filter/vf_convert.c | 133 ------- video/filter/vf_d3d11vpp.c | 318 ++++++++--------- video/filter/vf_format.c | 135 +++---- video/filter/vf_lavfi.c | 517 --------------------------- video/filter/vf_sub.c | 177 +++++----- video/filter/vf_vapoursynth.c | 682 ++++++++++++++++++------------------ video/filter/vf_vavpp.c | 291 ++++++--------- video/filter/vf_vdpaupp.c | 175 ++++------ 12 files changed, 1031 insertions(+), 2583 deletions(-) delete mode 100644 video/filter/vf.c delete mode 100644 video/filter/vf.h delete mode 100644 video/filter/vf_convert.c delete mode 100644 video/filter/vf_lavfi.c (limited to 'video/filter') diff --git a/video/filter/refqueue.c b/video/filter/refqueue.c index 6b2e5a2110..964fa29c08 100644 --- a/video/filter/refqueue.c +++ b/video/filter/refqueue.c @@ -17,12 +17,25 @@ #include +#include + #include "common/common.h" +#include "filters/f_autoconvert.h" +#include "filters/filter_internal.h" #include "video/mp_image.h" #include "refqueue.h" struct mp_refqueue { + struct mp_filter *filter; + struct mp_autoconvert *conv; + struct mp_pin *in, *out; + + struct mp_image *in_format; + + // Buffered frame in case of format changes. + struct mp_image *next; + int needed_past_frames; int needed_future_frames; int flags; @@ -38,17 +51,37 @@ struct mp_refqueue { int pos; }; -struct mp_refqueue *mp_refqueue_alloc(void) +static bool mp_refqueue_has_output(struct mp_refqueue *q); + +static void refqueue_dtor(void *p) { - struct mp_refqueue *q = talloc_zero(NULL, struct mp_refqueue); + struct mp_refqueue *q = p; mp_refqueue_flush(q); - return q; + mp_image_unrefp(&q->in_format); + talloc_free(q->conv->f); } -void mp_refqueue_free(struct mp_refqueue *q) +struct mp_refqueue *mp_refqueue_alloc(struct mp_filter *f) { + struct mp_refqueue *q = talloc_zero(f, struct mp_refqueue); + talloc_set_destructor(q, refqueue_dtor); + q->filter = f; + + q->conv = mp_autoconvert_create(f); + if (!q->conv) + abort(); + + q->in = q->conv->f->pins[1]; + mp_pin_connect(q->conv->f->pins[0], f->ppins[0]); + q->out = f->ppins[1]; + mp_refqueue_flush(q); - talloc_free(q); + return q; +} + +void mp_refqueue_add_in_format(struct mp_refqueue *q, int fmt, int subfmt) +{ + mp_autoconvert_add_imgfmt(q->conv, fmt, subfmt); } // The minimum number of frames required before and after the current frame. @@ -103,18 +136,12 @@ void mp_refqueue_flush(struct mp_refqueue *q) q->pos = -1; q->second_field = false; q->eof = false; + mp_image_unrefp(&q->next); } -// Add a new frame to the queue. (Call mp_refqueue_next() to advance the -// current frame and to discard unneeded past frames.) -// Ownership goes to the mp_refqueue. -// Passing NULL means EOF, in which case mp_refqueue_need_input() will return -// false even if not enough future frames are available. -void mp_refqueue_add_input(struct mp_refqueue *q, struct mp_image *img) +static void mp_refqueue_add_input(struct mp_refqueue *q, struct mp_image *img) { - q->eof = !img; - if (!img) - return; + assert(img); MP_TARRAY_INSERT_AT(q, q->queue, q->num_queue, 0, img); q->pos++; @@ -122,12 +149,12 @@ void mp_refqueue_add_input(struct mp_refqueue *q, struct mp_image *img) assert(q->pos >= 0 && q->pos < q->num_queue); } -bool mp_refqueue_need_input(struct mp_refqueue *q) +static bool mp_refqueue_need_input(struct mp_refqueue *q) { return q->pos < q->needed_future_frames && !q->eof; } -bool mp_refqueue_has_output(struct mp_refqueue *q) +static bool mp_refqueue_has_output(struct mp_refqueue *q) { return q->pos >= 0 && !mp_refqueue_need_input(q); } @@ -161,18 +188,8 @@ static bool output_next_field(struct mp_refqueue *q) return true; } -// Advance current field, depending on interlace flags. -void mp_refqueue_next_field(struct mp_refqueue *q) -{ - if (!mp_refqueue_has_output(q)) - return; - - if (!output_next_field(q)) - mp_refqueue_next(q); -} - // Advance to next input frame (skips fields even in field output mode). -void mp_refqueue_next(struct mp_refqueue *q) +static void mp_refqueue_next(struct mp_refqueue *q) { if (!mp_refqueue_has_output(q)) return; @@ -192,6 +209,16 @@ void mp_refqueue_next(struct mp_refqueue *q) assert(q->pos >= -1 && q->pos < q->num_queue); } +// Advance current field, depending on interlace flags. +static void mp_refqueue_next_field(struct mp_refqueue *q) +{ + if (!mp_refqueue_has_output(q)) + return; + + if (!output_next_field(q)) + mp_refqueue_next(q); +} + // Return a frame by relative position: // -1: first past frame // 0: current frame @@ -219,3 +246,114 @@ bool mp_refqueue_is_second_field(struct mp_refqueue *q) { return mp_refqueue_has_output(q) && q->second_field; } + +// Return non-NULL if a format change happened. A format change is defined by +// a change in image parameters, using broad enough checks that happen to be +// sufficient for all users of refqueue. +// On format change, the refqueue transparently drains remaining frames, and +// once that is done, this function returns a mp_image reference of the new +// frame. Reinit the low level video processor based on it, and then leave the +// reference alone and continue normally. +// All frames returned in the future will have a compatible format. +struct mp_image *mp_refqueue_execute_reinit(struct mp_refqueue *q) +{ + if (mp_refqueue_has_output(q) || !q->next) + return NULL; + + struct mp_image *cur = q->next; + q->next = NULL; + + mp_image_unrefp(&q->in_format); + mp_refqueue_flush(q); + + q->in_format = mp_image_new_ref(cur); + if (!q->in_format) + abort(); + mp_image_unref_data(q->in_format); + + mp_refqueue_add_input(q, cur); + return cur; +} + +// Main processing function. Call this in the filter process function. +// Returns if enough input frames are available for filtering, and output pin +// needs data; in other words, if this returns true, you render a frame and +// output it. +// If this returns true, you must call mp_refqueue_write_out_pin() to make +// progress. +bool mp_refqueue_can_output(struct mp_refqueue *q) +{ + if (!mp_pin_in_needs_data(q->out)) + return false; + + // Strictly return any output first to reduce latency. + if (mp_refqueue_has_output(q)) + return true; + + if (q->next) { + // Make it call again for mp_refqueue_execute_reinit(). + mp_filter_internal_mark_progress(q->filter); + return false; + } + + struct mp_frame frame = mp_pin_out_read(q->in); + if (frame.type == MP_FRAME_NONE) + return false; + + if (frame.type == MP_FRAME_EOF) { + q->eof = true; + if (mp_refqueue_has_output(q)) { + mp_pin_out_unread(q->in, frame); + return true; + } + mp_pin_in_write(q->out, frame); + mp_refqueue_flush(q); + return false; + } + + if (frame.type != MP_FRAME_VIDEO) { + MP_ERR(q->filter, "unsupported frame type\n"); + mp_frame_unref(&frame); + mp_filter_internal_mark_failed(q->filter); + return false; + } + + struct mp_image *img = frame.data; + + if (!q->in_format || !!q->in_format->hwctx != !!img->hwctx || + (img->hwctx && img->hwctx->data != q->in_format->hwctx->data) || + !mp_image_params_equal(&q->in_format->params, &img->params)) + { + q->next = img; + q->eof = true; + mp_filter_internal_mark_progress(q->filter); + return false; + } + + mp_refqueue_add_input(q, img); + + if (mp_refqueue_has_output(q)) + return true; + + mp_pin_out_request_data(q->in); + return false; +} + +// (Accepts NULL for generic errors.) +void mp_refqueue_write_out_pin(struct mp_refqueue *q, struct mp_image *mpi) +{ + if (mpi) { + mp_pin_in_write(q->out, MAKE_FRAME(MP_FRAME_VIDEO, mpi)); + } else { + MP_WARN(q->filter, "failed to output frame\n"); + mp_filter_internal_mark_failed(q->filter); + } + mp_refqueue_next_field(q); +} + +// Return frame for current format (without data). Reference is owned by q, +// might go away on further queue accesses. NULL if none yet. +struct mp_image *mp_refqueue_get_format(struct mp_refqueue *q) +{ + return q->in_format; +} diff --git a/video/filter/refqueue.h b/video/filter/refqueue.h index bb23506ac2..0a8ace0031 100644 --- a/video/filter/refqueue.h +++ b/video/filter/refqueue.h @@ -3,22 +3,26 @@ #include +#include "filters/filter.h" + // A helper for deinterlacers which require past/future reference frames. struct mp_refqueue; -struct mp_refqueue *mp_refqueue_alloc(void); -void mp_refqueue_free(struct mp_refqueue *q); +struct mp_refqueue *mp_refqueue_alloc(struct mp_filter *f); + +void mp_refqueue_add_in_format(struct mp_refqueue *q, int fmt, int subfmt); void mp_refqueue_set_refs(struct mp_refqueue *q, int past, int future); void mp_refqueue_flush(struct mp_refqueue *q); -void mp_refqueue_add_input(struct mp_refqueue *q, struct mp_image *img); -bool mp_refqueue_need_input(struct mp_refqueue *q); -bool mp_refqueue_has_output(struct mp_refqueue *q); -void mp_refqueue_next(struct mp_refqueue *q); -void mp_refqueue_next_field(struct mp_refqueue *q); struct mp_image *mp_refqueue_get(struct mp_refqueue *q, int pos); +struct mp_image *mp_refqueue_execute_reinit(struct mp_refqueue *q); +bool mp_refqueue_can_output(struct mp_refqueue *q); +void mp_refqueue_write_out_pin(struct mp_refqueue *q, struct mp_image *mpi); + +struct mp_image *mp_refqueue_get_format(struct mp_refqueue *q); + enum { MP_MODE_DEINT = (1 << 0), // deinterlacing enabled MP_MODE_OUTPUT_FIELDS = (1 << 1), // output fields separately diff --git a/video/filter/vf.c b/video/filter/vf.c deleted file mode 100644 index d5df466ba8..0000000000 --- a/video/filter/vf.c +++ /dev/null @@ -1,797 +0,0 @@ -/* - * 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 . - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "config.h" - -#include "common/common.h" -#include "common/global.h" -#include "common/msg.h" -#include "options/m_option.h" -#include "options/m_config.h" - -#include "options/options.h" - -#include "video/img_format.h" -#include "video/mp_image.h" -#include "video/mp_image_pool.h" -#include "vf.h" - -extern const vf_info_t vf_info_format; -extern const vf_info_t vf_info_sub; -extern const vf_info_t vf_info_convert; -extern const vf_info_t vf_info_lavfi; -extern const vf_info_t vf_info_lavfi_bridge; -extern const vf_info_t vf_info_vaapi; -extern const vf_info_t vf_info_vapoursynth; -extern const vf_info_t vf_info_vapoursynth_lazy; -extern const vf_info_t vf_info_vdpaupp; -extern const vf_info_t vf_info_d3d11vpp; - -// list of available filters: -static const vf_info_t *const filter_list[] = { - &vf_info_format, - &vf_info_sub, - &vf_info_convert, - &vf_info_lavfi, - &vf_info_lavfi_bridge, -#if HAVE_VAPOURSYNTH_CORE && HAVE_VAPOURSYNTH - &vf_info_vapoursynth, -#endif -#if HAVE_VAPOURSYNTH_CORE && HAVE_VAPOURSYNTH_LAZY - &vf_info_vapoursynth_lazy, -#endif -#if HAVE_VAAPI - &vf_info_vaapi, -#endif -#if HAVE_VDPAU - &vf_info_vdpaupp, -#endif -#if HAVE_D3D_HWACCEL - &vf_info_d3d11vpp, -#endif - NULL -}; - -static void vf_uninit_filter(vf_instance_t *vf); - -static bool get_desc(struct m_obj_desc *dst, int index) -{ - if (index >= MP_ARRAY_SIZE(filter_list) - 1) - return false; - const vf_info_t *vf = filter_list[index]; - *dst = (struct m_obj_desc) { - .name = vf->name, - .description = vf->description, - .priv_size = vf->priv_size, - .priv_defaults = vf->priv_defaults, - .options = vf->options, - .p = vf, - .print_help = vf->print_help, - }; - return true; -} - -// For the vf option -const struct m_obj_list vf_obj_list = { - .get_desc = get_desc, - .description = "video filters", - .allow_disable_entries = true, - .allow_unknown_entries = true, -}; - -// Try the cmd on each filter (starting with the first), and stop at the first -// filter which does not return CONTROL_UNKNOWN for it. -int vf_control_any(struct vf_chain *c, int cmd, void *arg) -{ - for (struct vf_instance *cur = c->first; cur; cur = cur->next) { - if (cur->control) { - int r = cur->control(cur, cmd, arg); - if (r != CONTROL_UNKNOWN) - return r; - } - } - return CONTROL_UNKNOWN; -} - -int vf_control_by_label(struct vf_chain *c,int cmd, void *arg, bstr label) -{ - char *label_str = bstrdup0(NULL, label); - struct vf_instance *cur = vf_find_by_label(c, label_str); - talloc_free(label_str); - if (cur) { - return cur->control ? cur->control(cur, cmd, arg) : CONTROL_NA; - } else { - return CONTROL_UNKNOWN; - } -} - -static void vf_control_all(struct vf_chain *c, int cmd, void *arg) -{ - for (struct vf_instance *cur = c->first; cur; cur = cur->next) { - if (cur->control) - cur->control(cur, cmd, arg); - } -} - -int vf_send_command(struct vf_chain *c, char *label, char *cmd, char *arg) -{ - char *args[2] = {cmd, arg}; - if (strcmp(label, "all") == 0) { - vf_control_all(c, VFCTRL_COMMAND, args); - return 0; - } else { - return vf_control_by_label(c, VFCTRL_COMMAND, args, bstr0(label)); - } -} - -static void vf_fix_img_params(struct mp_image *img, struct mp_image_params *p) -{ - // Filters must absolutely set these correctly. - assert(img->w == p->w && img->h == p->h); - assert(img->imgfmt == p->imgfmt); - // Too many things don't set this correctly. - // If --colormatrix is used, decoder and filter chain disagree too. - // In general, it's probably more convenient to force these here, - // instead of requiring filters to set these correctly. - img->params = *p; -} - -// Get a new image for filter output, with size and pixel format according to -// the last vf_config call. -struct mp_image *vf_alloc_out_image(struct vf_instance *vf) -{ - struct mp_image_params *p = &vf->fmt_out; - assert(p->imgfmt); - struct mp_image *img = mp_image_pool_get(vf->out_pool, p->imgfmt, p->w, p->h); - if (img) - vf_fix_img_params(img, p); - return img; -} - -// Returns false on failure; then the image can't be written to. -bool vf_make_out_image_writeable(struct vf_instance *vf, struct mp_image *img) -{ - struct mp_image_params *p = &vf->fmt_out; - assert(p->imgfmt); - assert(p->imgfmt == img->imgfmt); - assert(p->w == img->w && p->h == img->h); - return mp_image_pool_make_writeable(vf->out_pool, img); -} - -//============================================================================ - -// The default callback assumes all formats are passed through. -static int vf_default_query_format(struct vf_instance *vf, unsigned int fmt) -{ - return vf_next_query_format(vf, fmt); -} - -void vf_print_filter_chain(struct vf_chain *c, int msglevel, - struct vf_instance *vf) -{ - if (!mp_msg_test(c->log, msglevel)) - return; - - for (vf_instance_t *f = c->first; f; f = f->next) { - char b[256] = {0}; - mp_snprintf_cat(b, sizeof(b), " [%s] ", f->full_name); - if (f->label) - mp_snprintf_cat(b, sizeof(b), "\"%s\" ", f->label); - mp_snprintf_cat(b, sizeof(b), "%s", mp_image_params_to_str(&f->fmt_out)); - if (f->autoinserted) - mp_snprintf_cat(b, sizeof(b), " [a]"); - if (f == vf) - mp_snprintf_cat(b, sizeof(b), " <---"); - mp_msg(c->log, msglevel, "%s\n", b); - } -} - -static struct vf_instance *vf_open(struct vf_chain *c, const char *name, - char **args) -{ - const char *lavfi_name = NULL; - char **lavfi_args = NULL; - struct m_obj_desc desc; - if (!m_obj_list_find(&desc, &vf_obj_list, bstr0(name))) { - if (!m_obj_list_find(&desc, &vf_obj_list, bstr0("lavfi-bridge"))) { - MP_ERR(c, "Couldn't find video filter '%s'.\n", name); - return NULL; - } - lavfi_name = name; - lavfi_args = args; - args = NULL; - if (strncmp(lavfi_name, "lavfi-", 6) == 0) - lavfi_name += 6; - } - vf_instance_t *vf = talloc_zero(NULL, struct vf_instance); - *vf = (vf_instance_t) { - .full_name = talloc_strdup(vf, name), - .info = desc.p, - .log = mp_log_new(vf, c->log, name), - .hwdec_devs = c->hwdec_devs, - .query_format = vf_default_query_format, - .out_pool = mp_image_pool_new(vf), - .chain = c, - }; - struct m_config *config = - m_config_from_obj_desc_and_args(vf, vf->log, c->global, &desc, - name, c->opts->vf_defs, args); - if (!config) - goto error; - if (lavfi_name) { - // Pass the filter arguments as proper sub-options to the bridge filter. - struct m_config_option *name_opt = m_config_get_co(config, bstr0("name")); - assert(name_opt); - assert(name_opt->opt->type == &m_option_type_string); - if (m_config_set_option_raw(config, name_opt, &lavfi_name, 0) < 0) - goto error; - struct m_config_option *opts = m_config_get_co(config, bstr0("opts")); - assert(opts); - assert(opts->opt->type == &m_option_type_keyvalue_list); - if (m_config_set_option_raw(config, opts, &lavfi_args, 0) < 0) - goto error; - vf->full_name = talloc_asprintf(vf, "%s (lavfi)", vf->full_name); - } - vf->priv = config->optstruct; - int retcode = vf->info->open(vf); - if (retcode < 1) - goto error; - return vf; - -error: - MP_ERR(c, "Creating filter '%s' failed.\n", name); - talloc_free(vf); - return NULL; -} - -static vf_instance_t *vf_open_filter(struct vf_chain *c, const char *name, - char **args) -{ - int i, l = 0; - for (i = 0; args && args[2 * i]; i++) - l += 1 + strlen(args[2 * i]) + 1 + strlen(args[2 * i + 1]); - l += strlen(name); - char *str = malloc(l + 1); - if (!str) - return NULL; - char *p = str; - p += sprintf(str, "%s", name); - for (i = 0; args && args[2 * i]; i++) - p += sprintf(p, " %s=%s", args[2 * i], args[2 * i + 1]); - MP_INFO(c, "Opening video filter: [%s]\n", str); - free(str); - return vf_open(c, name, args); -} - -void vf_remove_filter(struct vf_chain *c, struct vf_instance *vf) -{ - assert(vf != c->first && vf != c->last); // these are sentinels - struct vf_instance *prev = c->first; - while (prev && prev->next != vf) - prev = prev->next; - assert(prev); // not inserted - prev->next = vf->next; - vf_uninit_filter(vf); - c->initialized = 0; -} - -struct vf_instance *vf_append_filter(struct vf_chain *c, const char *name, - char **args) -{ - struct vf_instance *vf = vf_open_filter(c, name, args); - if (vf) { - // Insert it before the last filter, which is the "out" pseudo-filter - // (But after the "in" pseudo-filter) - struct vf_instance **pprev = &c->first->next; - while (*pprev && (*pprev)->next) - pprev = &(*pprev)->next; - vf->next = *pprev ? *pprev : NULL; - *pprev = vf; - c->initialized = 0; - } - return vf; -} - -int vf_append_filter_list(struct vf_chain *c, struct m_obj_settings *list) -{ - for (int n = 0; list && list[n].name; n++) { - if (!list[n].enabled) - continue; - struct vf_instance *vf = - vf_append_filter(c, list[n].name, list[n].attribs); - if (vf) { - if (list[n].label) { - vf->label = talloc_strdup(vf, list[n].label); - } else { - for (int i = 0; i < 100; i++) { - char* label = talloc_asprintf(vf, "%s.%02d", list[n].name, i); - if (vf_find_by_label(c, label)) { - talloc_free(label); - } else { - vf->label = label; - break; - } - } - } - } - } - return 0; -} - -// Used by filters to add a filtered frame to the output queue. -// Ownership of img is transferred from caller to the filter chain. -void vf_add_output_frame(struct vf_instance *vf, struct mp_image *img) -{ - if (img) { - vf_fix_img_params(img, &vf->fmt_out); - MP_TARRAY_APPEND(vf, vf->out_queued, vf->num_out_queued, img); - } -} - -static bool vf_has_output_frame(struct vf_instance *vf) -{ - if (!vf->num_out_queued && vf->filter_out) { - if (vf->filter_out(vf) < 0) - MP_ERR(vf, "Error filtering frame.\n"); - } - return vf->num_out_queued > 0; -} - -static struct mp_image *vf_dequeue_output_frame(struct vf_instance *vf) -{ - struct mp_image *res = NULL; - if (vf_has_output_frame(vf)) { - res = vf->out_queued[0]; - MP_TARRAY_REMOVE_AT(vf->out_queued, vf->num_out_queued, 0); - } - return res; -} - -static int vf_do_filter(struct vf_instance *vf, struct mp_image *img) -{ - assert(vf->fmt_in.imgfmt); - if (img) - assert(mp_image_params_equal(&img->params, &vf->fmt_in)); - - if (vf->filter_ext) { - int r = vf->filter_ext(vf, img); - if (r < 0) - MP_ERR(vf, "Error filtering frame.\n"); - return r; - } else { - if (img) { - if (vf->filter) - img = vf->filter(vf, img); - vf_add_output_frame(vf, img); - } - return 0; - } -} - -// Input a frame into the filter chain. Ownership of img is transferred. -// Return >= 0 on success, < 0 on failure (even if output frames were produced) -int vf_filter_frame(struct vf_chain *c, struct mp_image *img) -{ - assert(img); - if (c->initialized < 1) { - talloc_free(img); - return -1; - } - assert(mp_image_params_equal(&img->params, &c->input_params)); - return vf_do_filter(c->first, img); -} - -// Similar to vf_output_frame(), but only ensure that the filter "until" has -// output, instead of the end of the filter chain. -static int vf_output_frame_until(struct vf_chain *c, struct vf_instance *until, - bool eof) -{ - if (until->num_out_queued) - return 1; - if (c->initialized < 1) - return -1; - while (1) { - struct vf_instance *last = NULL; - for (struct vf_instance * cur = c->first; cur; cur = cur->next) { - // Flush remaining frames on EOF, but do that only if the previous - // filters have been flushed (i.e. they have no more output). - if (eof && !last) { - int r = vf_do_filter(cur, NULL); - if (r < 0) - return r; - } - if (vf_has_output_frame(cur)) - last = cur; - if (cur == until) - break; - } - if (!last) - return 0; - if (last == until) - return 1; - int r = vf_do_filter(last->next, vf_dequeue_output_frame(last)); - if (r < 0) - return r; - } -} - -// Output the next queued image (if any) from the full filter chain. -// The frame can be retrieved with vf_read_output_frame(). -// eof: if set, assume there's no more input i.e. vf_filter_frame() will -// not be called (until reset) - flush all internally delayed frames -// returns: -1: error, 0: no output, 1: output available -int vf_output_frame(struct vf_chain *c, bool eof) -{ - return vf_output_frame_until(c, c->last, eof); -} - -struct mp_image *vf_read_output_frame(struct vf_chain *c) -{ - if (!c->last->num_out_queued) - vf_output_frame(c, false); - return vf_dequeue_output_frame(c->last); -} - -// Undo the previous vf_read_output_frame(). -void vf_unread_output_frame(struct vf_chain *c, struct mp_image *img) -{ - struct vf_instance *vf = c->last; - MP_TARRAY_INSERT_AT(vf, vf->out_queued, vf->num_out_queued, 0, img); -} - -// Some filters (vf_vapoursynth) filter on separate threads, and may need new -// input from the decoder, even though the core does not need a new output image -// yet (this is required to get proper pipelining in the filter). If the filter -// needs new data, it will call c->wakeup_callback, which in turn causes the -// core to recheck the filter chain, calling this function. Each filter is asked -// whether it needs a frame (with vf->needs_input), and if so, it will try to -// feed it a new frame. If this fails, it will request a new frame from the -// core by returning 1. -// returns -1: error, 0: nothing needed, 1: add new frame with vf_filter_frame() -int vf_needs_input(struct vf_chain *c) -{ - struct vf_instance *prev = c->first; - for (struct vf_instance *cur = c->first; cur; cur = cur->next) { - while (cur->needs_input && cur->needs_input(cur)) { - // Get frames from preceding filters, or if there are none, - // request new frames from decoder. - int r = vf_output_frame_until(c, prev, false); - if (r < 1) - return r < 0 ? -1 : 1; - r = vf_do_filter(cur, vf_dequeue_output_frame(prev)); - if (r < 0) - return r; - } - prev = cur; - } - return 0; -} - -static void vf_forget_frames(struct vf_instance *vf) -{ - for (int n = 0; n < vf->num_out_queued; n++) - talloc_free(vf->out_queued[n]); - vf->num_out_queued = 0; -} - -static void vf_chain_forget_frames(struct vf_chain *c) -{ - for (struct vf_instance *cur = c->first; cur; cur = cur->next) - vf_forget_frames(cur); -} - -void vf_seek_reset(struct vf_chain *c) -{ - vf_control_all(c, VFCTRL_SEEK_RESET, NULL); - vf_chain_forget_frames(c); -} - -int vf_next_query_format(struct vf_instance *vf, unsigned int fmt) -{ - return fmt >= IMGFMT_START && fmt < IMGFMT_END - ? vf->last_outfmts[fmt - IMGFMT_START] : 0; -} - -// Mark accepted input formats in fmts[]. Note that ->query_format will -// typically (but not always) call vf_next_query_format() to check whether -// an output format is supported. -static void query_formats(uint8_t *fmts, struct vf_instance *vf) -{ - for (int n = IMGFMT_START; n < IMGFMT_END; n++) - fmts[n - IMGFMT_START] = vf->query_format(vf, n); -} - -static bool is_conv_filter(struct vf_instance *vf) -{ - return vf && (strcmp(vf->info->name, "convert") == 0 || vf->autoinserted); -} - -static const char *find_conv_filter(uint8_t *fmts_in, uint8_t *fmts_out) -{ - for (int n = 0; filter_list[n]; n++) { - if (filter_list[n]->test_conversion) { - for (int a = IMGFMT_START; a < IMGFMT_END; a++) { - for (int b = IMGFMT_START; b < IMGFMT_END; b++) { - if (fmts_in[a - IMGFMT_START] && fmts_out[b - IMGFMT_START] && - filter_list[n]->test_conversion(a, b)) - return filter_list[n]->name; - } - } - } - } - return "convert"; -} - -static void update_formats(struct vf_chain *c, struct vf_instance *vf, - uint8_t *fmts) -{ - if (vf->next) - update_formats(c, vf->next, vf->last_outfmts); - query_formats(fmts, vf); - bool has_in = false, has_out = false; - for (int n = IMGFMT_START; n < IMGFMT_END; n++) { - has_in |= !!fmts[n - IMGFMT_START]; - has_out |= !!vf->last_outfmts[n - IMGFMT_START]; - } - if (has_out && !has_in && !is_conv_filter(vf) && - !is_conv_filter(vf->next)) - { - // If there are output formats, but no input formats (meaning the - // filters after vf work, but vf can't output any format the filters - // after it accept), try to insert a conversion filter. - MP_INFO(c, "Using conversion filter.\n"); - // Determine which output formats the filter _could_ accept. For this - // to work after the conversion filter is inserted, it is assumed that - // conversion filters have a single set of in/output formats that can - // be converted to each other. - uint8_t out_formats[IMGFMT_END - IMGFMT_START]; - for (int n = IMGFMT_START; n < IMGFMT_END; n++) { - out_formats[n - IMGFMT_START] = vf->last_outfmts[n - IMGFMT_START]; - vf->last_outfmts[n - IMGFMT_START] = 1; - } - query_formats(fmts, vf); - const char *filter = find_conv_filter(fmts, out_formats); - char **args = NULL; - char *args_no_warn[] = {"warn", "no", NULL}; - if (strcmp(filter, "scale") == 0) - args = args_no_warn; - struct vf_instance *conv = vf_open(c, filter, args); - if (conv) { - conv->autoinserted = true; - conv->next = vf->next; - vf->next = conv; - update_formats(c, conv, vf->last_outfmts); - query_formats(fmts, vf); - } - } - for (int n = IMGFMT_START; n < IMGFMT_END; n++) - has_in |= !!fmts[n - IMGFMT_START]; - if (!has_in) { - // Pretend all out formats work. All this does it getting better - // error messages in some cases, so we can configure all filter - // until it fails, which will be visible in vf_print_filter_chain(). - for (int n = IMGFMT_START; n < IMGFMT_END; n++) - vf->last_outfmts[n - IMGFMT_START] = 1; - query_formats(fmts, vf); - } -} - -// Insert a conversion filter _after_ vf. -// vf needs to have been successfully configured, vf->next unconfigured but -// with formats negotiated. -static void auto_insert_conversion_filter_if_needed(struct vf_chain *c, - struct vf_instance *vf) -{ - if (!vf->next || vf->next->query_format(vf->next, vf->fmt_out.imgfmt) || - is_conv_filter(vf) || is_conv_filter(vf->next)) - return; - - MP_INFO(c, "Using conversion filter.\n"); - - uint8_t fmts[IMGFMT_END - IMGFMT_START]; - query_formats(fmts, vf->next); - - uint8_t out_formats[IMGFMT_END - IMGFMT_START]; - for (int n = IMGFMT_START; n < IMGFMT_END; n++) - out_formats[n - IMGFMT_START] = n == vf->fmt_out.imgfmt; - - const char *filter = find_conv_filter(out_formats, fmts); - char **args = NULL; - char *args_no_warn[] = {"warn", "no", NULL}; - if (strcmp(filter, "scale") == 0) - args = args_no_warn; - struct vf_instance *conv = vf_open(c, filter, args); - if (conv) { - conv->autoinserted = true; - conv->next = vf->next; - vf->next = conv; - update_formats(c, conv, vf->last_outfmts); - } -} - -static int vf_reconfig_wrapper(struct vf_instance *vf, - const struct mp_image_params *p) -{ - vf_forget_frames(vf); - if (vf->out_pool) - mp_image_pool_clear(vf->out_pool); - - if (!vf->query_format(vf, p->imgfmt)) - return -2; - - vf->fmt_out = vf->fmt_in = *p; - - if (!mp_image_params_valid(&vf->fmt_in)) - return -2; - - int r = 0; - if (vf->reconfig) - r = vf->reconfig(vf, &vf->fmt_in, &vf->fmt_out); - - if (!mp_image_params_equal(&vf->fmt_in, p)) - r = -2; - - if (!mp_image_params_valid(&vf->fmt_out)) - r = -2; - - // Fix csp in case of pixel format change - if (r >= 0) - mp_image_params_guess_csp(&vf->fmt_out); - - return r; -} - -int vf_reconfig(struct vf_chain *c, const struct mp_image_params *params) -{ - int r = 0; - vf_seek_reset(c); - for (struct vf_instance *vf = c->first; vf; ) { - struct vf_instance *next = vf->next; - if (vf->autoinserted) - vf_remove_filter(c, vf); - vf = next; - } - c->input_params = *params; - c->first->fmt_in = *params; - struct mp_image_params cur = *params; - - uint8_t unused[IMGFMT_END - IMGFMT_START]; - update_formats(c, c->first, unused); - AVBufferRef *hwfctx = c->in_hwframes_ref; - struct vf_instance *failing = NULL; - for (struct vf_instance *vf = c->first; vf; vf = vf->next) { - av_buffer_unref(&vf->in_hwframes_ref); - av_buffer_unref(&vf->out_hwframes_ref); - vf->in_hwframes_ref = hwfctx ? av_buffer_ref(hwfctx) : NULL; - vf->out_hwframes_ref = hwfctx ? av_buffer_ref(hwfctx) : NULL; - r = vf_reconfig_wrapper(vf, &cur); - if (r < 0) { - failing = vf; - break; - } - cur = vf->fmt_out; - hwfctx = vf->out_hwframes_ref; - // Recheck if the new output format works with the following filters. - auto_insert_conversion_filter_if_needed(c, vf); - } - c->output_params = cur; - c->initialized = r < 0 ? -1 : 1; - int loglevel = r < 0 ? MSGL_WARN : MSGL_V; - if (r == -2) - MP_ERR(c, "Image formats incompatible or invalid.\n"); - mp_msg(c->log, loglevel, "Video filter chain:\n"); - vf_print_filter_chain(c, loglevel, failing); - if (r < 0) - c->output_params = (struct mp_image_params){0}; - return r; -} - -// Hack to get mp_image.hwctx before vf_reconfig() -void vf_set_proto_frame(struct vf_chain *c, struct mp_image *img) -{ - av_buffer_unref(&c->in_hwframes_ref); - c->in_hwframes_ref = img && img->hwctx ? av_buffer_ref(img->hwctx) : NULL; -} - -struct vf_instance *vf_find_by_label(struct vf_chain *c, const char *label) -{ - struct vf_instance *vf = c->first; - while (vf) { - if (vf->label && label && strcmp(vf->label, label) == 0) - return vf; - vf = vf->next; - } - return NULL; -} - -static void vf_uninit_filter(vf_instance_t *vf) -{ - av_buffer_unref(&vf->in_hwframes_ref); - av_buffer_unref(&vf->out_hwframes_ref); - if (vf->uninit) - vf->uninit(vf); - vf_forget_frames(vf); - talloc_free(vf); -} - -static int input_query_format(struct vf_instance *vf, unsigned int fmt) -{ - // Setting fmt_in is guaranteed by vf_reconfig(). - if (fmt == vf->fmt_in.imgfmt) - return vf_next_query_format(vf, fmt); - return 0; -} - -static int output_query_format(struct vf_instance *vf, unsigned int fmt) -{ - struct vf_chain *c = (void *)vf->priv; - if (fmt >= IMGFMT_START && fmt < IMGFMT_END) - return c->allowed_output_formats[fmt - IMGFMT_START]; - return 0; -} - -struct vf_chain *vf_new(struct mpv_global *global) -{ - struct vf_chain *c = talloc_ptrtype(NULL, c); - *c = (struct vf_chain){ - .opts = global->opts, - .log = mp_log_new(c, global->log, "!vf"), - .global = global, - }; - static const struct vf_info in = { .name = "in" }; - c->first = talloc(c, struct vf_instance); - *c->first = (struct vf_instance) { - .full_name = "in", - .log = c->log, - .info = &in, - .query_format = input_query_format, - }; - static const struct vf_info out = { .name = "out" }; - c->last = talloc(c, struct vf_instance); - *c->last = (struct vf_instance) { - .full_name = "out", - .log = c->log, - .info = &out, - .query_format = output_query_format, - .priv = (void *)c, - }; - c->first->next = c->last; - return c; -} - -void vf_destroy(struct vf_chain *c) -{ - if (!c) - return; - av_buffer_unref(&c->in_hwframes_ref); - while (c->first) { - vf_instance_t *vf = c->first; - c->first = vf->next; - vf_uninit_filter(vf); - } - vf_chain_forget_frames(c); - talloc_free(c); -} diff --git a/video/filter/vf.h b/video/filter/vf.h deleted file mode 100644 index 5146a4d15b..0000000000 --- a/video/filter/vf.h +++ /dev/null @@ -1,179 +0,0 @@ -/* - * 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 . - */ - -#ifndef MPLAYER_VF_H -#define MPLAYER_VF_H - -#include - -#include "video/mp_image.h" -#include "common/common.h" - -struct MPOpts; -struct mpv_global; -struct vf_instance; -struct vf_priv_s; -struct m_obj_settings; - -typedef struct vf_info { - const char *description; - const char *name; - int (*open)(struct vf_instance *vf); - int priv_size; - const void *priv_defaults; - const struct m_option *options; - void (*print_help)(struct mp_log *log); - bool (*test_conversion)(int in, int out); -} vf_info_t; - -typedef struct vf_instance { - const vf_info_t *info; - char *full_name; - - // Initialize the filter. The filter must set *out to the same image - // params as the images the filter functions will return for the given - // *in format. - // Note that by default, only formats reported as supported by query_format - // will be allowed for *in. - // Returns >= 0 on success, < 0 on error. - int (*reconfig)(struct vf_instance *vf, struct mp_image_params *in, - struct mp_image_params *out); - - int (*control)(struct vf_instance *vf, int request, void *data); - int (*query_format)(struct vf_instance *vf, unsigned int fmt); - - // Filter mpi and return the result. The input mpi reference is owned by - // the filter, the returned reference is owned by the caller. - // Return NULL if the output frame is skipped. - struct mp_image *(*filter)(struct vf_instance *vf, struct mp_image *mpi); - - // Like filter(), but can return an error code ( >= 0 means success). This - // callback is also more practical when the filter can return multiple - // output images. Use vf_add_output_frame() to queue output frames. - // Warning: this is called with mpi==NULL if there is no more input at - // all (i.e. the video has reached end of file condition). This - // can be used to output delayed or otherwise remaining images. - int (*filter_ext)(struct vf_instance *vf, struct mp_image *mpi); - - // Produce an output frame. This is called after filter or filter_ext. - // You can add 0 or more frames with vf_add_output_frame(). (This allows - // distributing the filter load over time -> typically add at most 1 frame.) - // If this adds no frame (or is NULL), then the caller assumes that the - // filter needs new input. - // Return a negative value on error. (No more frames is not an error.) - // May be called multiple times, even if the filter gives no output. - int (*filter_out)(struct vf_instance *vf); - - // Optional function that checks whether the filter needs additional - // input. This is for filters with asynchronous behavior: they filter - // frames in the background, and to get good pipelining behavior, new - // data should be fed, even if the playback core doesn't need any yet. - bool (*needs_input)(struct vf_instance *vf); - - void (*uninit)(struct vf_instance *vf); - - char *label; - bool autoinserted; - - struct mp_image_params fmt_in, fmt_out; - - // This is a dirty hack. - struct AVBufferRef *in_hwframes_ref, *out_hwframes_ref; - - struct mp_image_pool *out_pool; - struct vf_priv_s *priv; - struct mp_log *log; - struct mp_hwdec_devices *hwdec_devs; - - struct mp_image **out_queued; - int num_out_queued; - - // Caches valid output formats. - uint8_t last_outfmts[IMGFMT_END - IMGFMT_START]; - - struct vf_chain *chain; - struct vf_instance *next; -} vf_instance_t; - -// A chain of video filters -struct vf_chain { - int initialized; // 0: no, 1: yes, -1: attempted to, but failed - - struct vf_instance *first, *last; - - struct mp_image_params input_params; - struct mp_image_params output_params; - uint8_t allowed_output_formats[IMGFMT_END - IMGFMT_START]; - - double container_fps; - double display_fps; - - struct mp_log *log; - struct MPOpts *opts; - struct mpv_global *global; - struct mp_hwdec_devices *hwdec_devs; - - // This is a dirty hack. - struct AVBufferRef *in_hwframes_ref; - - // Call when the filter chain wants new processing (for filters with - // asynchronous behavior) - must be immutable once filters are created, - // since they are supposed to call it from foreign threads. - void (*wakeup_callback)(void *ctx); - void *wakeup_callback_ctx; -}; - -enum vf_ctrl { - VFCTRL_SEEK_RESET = 1, // reset on picture and PTS discontinuities - VFCTRL_GET_METADATA, // Get frame metadata from lavfi filters (e.g., cropdetect) - /* Hack to make the OSD state object available to vf_sub which - * access OSD/subtitle state outside of normal OSD draw time. */ - VFCTRL_INIT_OSD, - VFCTRL_COMMAND, -}; - -struct vf_chain *vf_new(struct mpv_global *global); -void vf_destroy(struct vf_chain *c); -void vf_set_proto_frame(struct vf_chain *c, struct mp_image *img); -int vf_reconfig(struct vf_chain *c, const struct mp_image_params *params); -int vf_control_any(struct vf_chain *c, int cmd, void *arg); -int vf_control_by_label(struct vf_chain *c, int cmd, void *arg, bstr label); -int vf_filter_frame(struct vf_chain *c, struct mp_image *img); -int vf_output_frame(struct vf_chain *c, bool eof); -int vf_needs_input(struct vf_chain *c); -struct mp_image *vf_read_output_frame(struct vf_chain *c); -void vf_unread_output_frame(struct vf_chain *c, struct mp_image *img); -void vf_seek_reset(struct vf_chain *c); -struct vf_instance *vf_append_filter(struct vf_chain *c, const char *name, - char **args); -void vf_remove_filter(struct vf_chain *c, struct vf_instance *vf); -int vf_append_filter_list(struct vf_chain *c, struct m_obj_settings *list); -struct vf_instance *vf_find_by_label(struct vf_chain *c, const char *label); -void vf_print_filter_chain(struct vf_chain *c, int msglevel, - struct vf_instance *vf); - -int vf_send_command(struct vf_chain *c, char *label, char *cmd, char *arg); - -// Filter internal API -struct mp_image *vf_alloc_out_image(struct vf_instance *vf); -bool vf_make_out_image_writeable(struct vf_instance *vf, struct mp_image *img); -void vf_add_output_frame(struct vf_instance *vf, struct mp_image *img); - -// default wrappers: -int vf_next_query_format(struct vf_instance *vf, unsigned int fmt); - -#endif /* MPLAYER_VF_H */ diff --git a/video/filter/vf_convert.c b/video/filter/vf_convert.c deleted file mode 100644 index 7a7fdce228..0000000000 --- a/video/filter/vf_convert.c +++ /dev/null @@ -1,133 +0,0 @@ -/* - * 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 . - */ - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "common/av_common.h" -#include "common/msg.h" - -#include "options/options.h" - -#include "video/img_format.h" -#include "video/mp_image.h" -#include "video/sws_utils.h" -#include "video/fmt-conversion.h" -#include "vf.h" - -struct vf_priv_s { - struct mp_sws_context *sws; -}; - -static int find_best_out(vf_instance_t *vf, int in_format) -{ - int best = 0; - for (int out_format = IMGFMT_START; out_format < IMGFMT_END; out_format++) { - if (!vf_next_query_format(vf, out_format)) - continue; - if (sws_isSupportedOutput(imgfmt2pixfmt(out_format)) < 1) - continue; - if (best) { - int candidate = mp_imgfmt_select_best(best, out_format, in_format); - if (candidate) - best = candidate; - } else { - best = out_format; - } - } - return best; -} - -static int reconfig(struct vf_instance *vf, struct mp_image_params *in, - struct mp_image_params *out) -{ - unsigned int best = find_best_out(vf, in->imgfmt); - if (!best) { - MP_WARN(vf, "no supported output format found\n"); - return -1; - } - - *out = *in; - out->imgfmt = best; - - // If we convert from RGB to YUV, default to limited range. - if (mp_imgfmt_get_forced_csp(in->imgfmt) == MP_CSP_RGB && - mp_imgfmt_get_forced_csp(out->imgfmt) == MP_CSP_AUTO) - out->color.levels = MP_CSP_LEVELS_TV; - - mp_image_params_guess_csp(out); - - mp_sws_set_from_cmdline(vf->priv->sws, vf->chain->global); - vf->priv->sws->src = *in; - vf->priv->sws->dst = *out; - - if (mp_sws_reinit(vf->priv->sws) < 0) { - // error... - MP_WARN(vf, "Couldn't init libswscale for this setup\n"); - return -1; - } - return 0; -} - -static struct mp_image *filter(struct vf_instance *vf, struct mp_image *mpi) -{ - struct mp_image *dmpi = vf_alloc_out_image(vf); - if (!dmpi) - return NULL; - mp_image_copy_attributes(dmpi, mpi); - - mp_sws_scale(vf->priv->sws, dmpi, mpi); - - talloc_free(mpi); - return dmpi; -} - -static int query_format(struct vf_instance *vf, unsigned int fmt) -{ - if (IMGFMT_IS_HWACCEL(fmt) || sws_isSupportedInput(imgfmt2pixfmt(fmt)) < 1) - return 0; - return !!find_best_out(vf, fmt); -} - -static void uninit(struct vf_instance *vf) -{ -} - -static int vf_open(vf_instance_t *vf) -{ - vf->reconfig = reconfig; - vf->filter = filter; - vf->query_format = query_format; - vf->uninit = uninit; - vf->priv->sws = mp_sws_alloc(vf); - vf->priv->sws->log = vf->log; - return 1; -} - -const vf_info_t vf_info_convert = { - .description = "image format conversion with libswscale", - .name = "convert", - .open = vf_open, - .priv_size = sizeof(struct vf_priv_s), -}; diff --git a/video/filter/vf_d3d11vpp.c b/video/filter/vf_d3d11vpp.c index 3be49ede80..fb96a44e65 100644 --- a/video/filter/vf_d3d11vpp.c +++ b/video/filter/vf_d3d11vpp.c @@ -25,9 +25,13 @@ #include "common/common.h" #include "osdep/timer.h" #include "osdep/windows_utils.h" -#include "vf.h" +#include "filters/f_autoconvert.h" +#include "filters/filter.h" +#include "filters/filter_internal.h" +#include "filters/user_filters.h" #include "refqueue.h" #include "video/hwdec.h" +#include "video/mp_image.h" #include "video/mp_image_pool.h" // missing in MinGW @@ -38,8 +42,17 @@ #define D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_INVERSE_TELECINE 0x10 #define D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_FRAME_RATE_CONVERSION 0x20 -struct vf_priv_s { +struct opts { + int deint_enabled; + int interlaced_only; + int mode; +}; + +struct priv { + struct opts *opts; + ID3D11Device *vo_dev; + const int *vo_formats; ID3D11DeviceContext *device_ctx; ID3D11VideoDevice *video_dev; @@ -61,10 +74,6 @@ struct vf_priv_s { struct mp_image_pool *pool; struct mp_refqueue *queue; - - int deint_enabled; - int interlaced_only; - int mode; }; static void release_tex(void *arg) @@ -76,8 +85,8 @@ static void release_tex(void *arg) static struct mp_image *alloc_pool(void *pctx, int fmt, int w, int h) { - struct vf_instance *vf = pctx; - struct vf_priv_s *p = vf->priv; + struct mp_filter *vf = pctx; + struct priv *p = vf->priv; HRESULT hr; ID3D11Texture2D *texture = NULL; @@ -100,7 +109,7 @@ static struct mp_image *alloc_pool(void *pctx, int fmt, int w, int h) if (!mpi) abort(); - mp_image_setfmt(mpi, p->out_params.imgfmt); + mp_image_setfmt(mpi, IMGFMT_D3D11); mp_image_set_size(mpi, w, h); mpi->params.hw_subfmt = p->out_params.hw_subfmt; @@ -110,29 +119,15 @@ static struct mp_image *alloc_pool(void *pctx, int fmt, int w, int h) return mpi; } -static void flush_frames(struct vf_instance *vf) +static void flush_frames(struct mp_filter *vf) { - struct vf_priv_s *p = vf->priv; + struct priv *p = vf->priv; mp_refqueue_flush(p->queue); } -static int filter_ext(struct vf_instance *vf, struct mp_image *in) -{ - struct vf_priv_s *p = vf->priv; - - mp_refqueue_set_refs(p->queue, 0, 0); - mp_refqueue_set_mode(p->queue, - (p->deint_enabled ? MP_MODE_DEINT : 0) | - MP_MODE_OUTPUT_FIELDS | - (p->interlaced_only ? MP_MODE_INTERLACED_ONLY : 0)); - - mp_refqueue_add_input(p->queue, in); - return 0; -} - -static void destroy_video_proc(struct vf_instance *vf) +static void destroy_video_proc(struct mp_filter *vf) { - struct vf_priv_s *p = vf->priv; + struct priv *p = vf->priv; if (p->video_proc) ID3D11VideoProcessor_Release(p->video_proc); @@ -143,9 +138,9 @@ static void destroy_video_proc(struct vf_instance *vf) p->vp_enum = NULL; } -static int recreate_video_proc(struct vf_instance *vf) +static int recreate_video_proc(struct mp_filter *vf) { - struct vf_priv_s *p = vf->priv; + struct priv *p = vf->priv; HRESULT hr; destroy_video_proc(vf); @@ -168,7 +163,7 @@ static int recreate_video_proc(struct vf_instance *vf) goto fail; MP_VERBOSE(vf, "Found %d rate conversion caps. Looking for caps=0x%x.\n", - (int)caps.RateConversionCapsCount, p->mode); + (int)caps.RateConversionCapsCount, p->opts->mode); int rindex = -1; for (int n = 0; n < caps.RateConversionCapsCount; n++) { @@ -178,7 +173,7 @@ static int recreate_video_proc(struct vf_instance *vf) if (FAILED(hr)) goto fail; MP_VERBOSE(vf, " - %d: 0x%08x\n", n, (unsigned)rcaps.ProcessorCaps); - if (rcaps.ProcessorCaps & p->mode) { + if (rcaps.ProcessorCaps & p->opts->mode) { MP_VERBOSE(vf, " (matching)\n"); if (rindex < 0) rindex = n; @@ -248,17 +243,19 @@ fail: return -1; } -static int render(struct vf_instance *vf) +static struct mp_image *render(struct mp_filter *vf) { - struct vf_priv_s *p = vf->priv; + struct priv *p = vf->priv; int res = -1; HRESULT hr; ID3D11VideoProcessorInputView *in_view = NULL; ID3D11VideoProcessorOutputView *out_view = NULL; struct mp_image *in = NULL, *out = NULL; - out = mp_image_pool_get(p->pool, p->out_params.imgfmt, p->params.w, p->params.h); - if (!out) + out = mp_image_pool_get(p->pool, IMGFMT_D3D11, p->params.w, p->params.h); + if (!out) { + MP_WARN(vf, "failed to allocate frame\n"); goto cleanup; + } ID3D11Texture2D *d3d_out_tex = (void *)out->planes[0]; @@ -325,8 +322,10 @@ static int render(struct vf_instance *vf) (ID3D11Resource *)d3d_out_tex, p->vp_enum, &outdesc, &out_view); - if (FAILED(hr)) + if (FAILED(hr)) { + MP_ERR(vf, "Could not create ID3D11VideoProcessorOutputView\n"); goto cleanup; + } D3D11_VIDEO_PROCESSOR_STREAM stream = { .Enable = TRUE, @@ -346,87 +345,73 @@ cleanup: ID3D11VideoProcessorInputView_Release(in_view); if (out_view) ID3D11VideoProcessorOutputView_Release(out_view); - if (res >= 0) { - vf_add_output_frame(vf, out); - } else { - talloc_free(out); - } - mp_refqueue_next_field(p->queue); - return res; + if (res < 0) + TA_FREEP(&out); + return out; } -static int filter_out(struct vf_instance *vf) +static bool vo_supports(struct priv *p, int subfmt) { - struct vf_priv_s *p = vf->priv; - - if (!mp_refqueue_has_output(p->queue)) - return 0; - - // no filtering - if (!mp_refqueue_should_deint(p->queue) && !p->require_filtering) { - struct mp_image *in = mp_image_new_ref(mp_refqueue_get(p->queue, 0)); - if (!in) - return -1; - mp_image_set_params(in, &p->out_params); - vf_add_output_frame(vf, in); - mp_refqueue_next(p->queue); - return 0; + for (int n = 0; p->vo_formats && p->vo_formats[n]; n++) { + if (p->vo_formats[n] == subfmt) + return true; } - - return render(vf); + return false; } -static int reconfig(struct vf_instance *vf, struct mp_image_params *in, - struct mp_image_params *out) +static void vf_d3d11vpp_process(struct mp_filter *vf) { - struct vf_priv_s *p = vf->priv; - - flush_frames(vf); - talloc_free(p->pool); - p->pool = NULL; + struct priv *p = vf->priv; + + struct mp_image *in_fmt = mp_refqueue_execute_reinit(p->queue); + if (in_fmt) { + mp_image_pool_clear(p->pool); + + destroy_video_proc(vf); + + p->params = in_fmt->params; + p->out_params = p->params; + + if (vo_supports(p, IMGFMT_NV12)) { + p->out_params.hw_subfmt = IMGFMT_NV12; + p->out_format = DXGI_FORMAT_NV12; + p->out_shared = false; + p->out_rgb = false; + } else { + p->out_params.hw_subfmt = IMGFMT_RGB0; + p->out_format = DXGI_FORMAT_B8G8R8A8_UNORM; + p->out_shared = true; + p->out_rgb = true; + } + p->out_params.hw_flags = 0; - destroy_video_proc(vf); + p->require_filtering = p->params.hw_subfmt != p->out_params.hw_subfmt; + } - *out = *in; + if (!mp_refqueue_can_output(p->queue)) + return; - if (vf_next_query_format(vf, IMGFMT_D3D11VA) || - vf_next_query_format(vf, IMGFMT_D3D11NV12)) - { - out->imgfmt = vf_next_query_format(vf, IMGFMT_D3D11VA) - ? IMGFMT_D3D11VA : IMGFMT_D3D11NV12; - out->hw_subfmt = IMGFMT_NV12; - p->out_format = DXGI_FORMAT_NV12; - p->out_shared = false; - p->out_rgb = false; + if (!mp_refqueue_should_deint(p->queue) && !p->require_filtering) { + // no filtering + struct mp_image *in = mp_image_new_ref(mp_refqueue_get(p->queue, 0)); + if (!in) { + mp_filter_internal_mark_failed(vf); + return; + } + mp_refqueue_write_out_pin(p->queue, in); } else { - out->imgfmt = IMGFMT_D3D11RGB; - out->hw_subfmt = IMGFMT_RGB0; - p->out_format = DXGI_FORMAT_B8G8R8A8_UNORM; - p->out_shared = true; - p->out_rgb = true; + mp_refqueue_write_out_pin(p->queue, render(vf)); } - out->hw_flags = 0; - - p->require_filtering = in->hw_subfmt != out->hw_subfmt; - - p->params = *in; - p->out_params = *out; - - p->pool = mp_image_pool_new(vf); - mp_image_pool_set_allocator(p->pool, alloc_pool, vf); - mp_image_pool_set_lru(p->pool); - - return 0; } -static void uninit(struct vf_instance *vf) +static void uninit(struct mp_filter *vf) { - struct vf_priv_s *p = vf->priv; + struct priv *p = vf->priv; destroy_video_proc(vf); flush_frames(vf); - mp_refqueue_free(p->queue); + talloc_free(p->queue); talloc_free(p->pool); if (p->video_ctx) @@ -442,69 +427,55 @@ static void uninit(struct vf_instance *vf) ID3D11Device_Release(p->vo_dev); } -static int query_format(struct vf_instance *vf, unsigned int imgfmt) +static const struct mp_filter_info vf_d3d11vpp_filter = { + .name = "d3d11vpp", + .process = vf_d3d11vpp_process, + .reset = flush_frames, + .destroy = uninit, + .priv_size = sizeof(struct priv), +}; + +static struct mp_filter *vf_d3d11vpp_create(struct mp_filter *parent, + void *options) { - if (imgfmt == IMGFMT_D3D11VA || - imgfmt == IMGFMT_D3D11NV12 || - imgfmt == IMGFMT_D3D11RGB) - { - return vf_next_query_format(vf, IMGFMT_D3D11VA) || - vf_next_query_format(vf, IMGFMT_D3D11NV12) || - vf_next_query_format(vf, IMGFMT_D3D11RGB); + struct mp_filter *f = mp_filter_create(parent, &vf_d3d11vpp_filter); + if (!f) { + talloc_free(options); + return NULL; } - return 0; -} -static bool test_conversion(int in, int out) -{ - return (in == IMGFMT_D3D11VA || - in == IMGFMT_D3D11NV12 || - in == IMGFMT_D3D11RGB) && - (out == IMGFMT_D3D11VA || - out == IMGFMT_D3D11NV12 || - out == IMGFMT_D3D11RGB); -} + mp_filter_add_pin(f, MP_PIN_IN, "in"); + mp_filter_add_pin(f, MP_PIN_OUT, "out"); -static int control(struct vf_instance *vf, int request, void* data) -{ - switch (request){ - case VFCTRL_SEEK_RESET: - flush_frames(vf); - return true; - default: - return CONTROL_UNKNOWN; - } -} + struct priv *p = f->priv; + p->opts = talloc_steal(p, options); -static int vf_open(vf_instance_t *vf) -{ - struct vf_priv_s *p = vf->priv; + // Special path for vf_d3d11_create_outconv(): disable all processing except + // possibly surface format conversions. + if (!p->opts) { + static const struct opts opts = {0}; + p->opts = (struct opts *)&opts; + } - if (!vf->hwdec_devs) - return 0; + p->queue = mp_refqueue_alloc(f); - vf->reconfig = reconfig; - vf->filter_ext = filter_ext; - vf->filter_out = filter_out; - vf->query_format = query_format; - vf->uninit = uninit; - vf->control = control; + struct mp_stream_info *info = mp_filter_find_stream_info(f); + if (!info || !info->hwdec_devs) + goto fail; - hwdec_devices_request_all(vf->hwdec_devs); - AVBufferRef *ref = - hwdec_devices_get_lavc(vf->hwdec_devs, AV_HWDEVICE_TYPE_D3D11VA); - if (!ref) - return 0; + hwdec_devices_request_all(info->hwdec_devs); - AVHWDeviceContext *hwctx = (void *)ref->data; - AVD3D11VADeviceContext *d3dctx = hwctx->hwctx; + struct mp_hwdec_ctx *hwctx = + hwdec_devices_get_by_lavc(info->hwdec_devs, AV_HWDEVICE_TYPE_D3D11VA); + if (!hwctx || !hwctx->av_device_ref) + goto fail; + AVHWDeviceContext *avhwctx = (void *)hwctx->av_device_ref->data; + AVD3D11VADeviceContext *d3dctx = avhwctx->hwctx; p->vo_dev = d3dctx->device; ID3D11Device_AddRef(p->vo_dev); - av_buffer_unref(&ref); - - p->queue = mp_refqueue_alloc(); + p->vo_formats = hwctx->supported_formats; HRESULT hr; @@ -521,14 +492,26 @@ static int vf_open(vf_instance_t *vf) if (FAILED(hr)) goto fail; - return 1; + p->pool = mp_image_pool_new(f); + mp_image_pool_set_allocator(p->pool, alloc_pool, f); + mp_image_pool_set_lru(p->pool); + + mp_refqueue_add_in_format(p->queue, IMGFMT_D3D11, 0); + + mp_refqueue_set_refs(p->queue, 0, 0); + mp_refqueue_set_mode(p->queue, + (p->opts->deint_enabled ? MP_MODE_DEINT : 0) | + MP_MODE_OUTPUT_FIELDS | + (p->opts->interlaced_only ? MP_MODE_INTERLACED_ONLY : 0)); + + return f; fail: - uninit(vf); - return 0; + talloc_free(f); + return NULL; } -#define OPT_BASE_STRUCT struct vf_priv_s +#define OPT_BASE_STRUCT struct opts static const m_option_t vf_opts_fields[] = { OPT_FLAG("deint", deint_enabled, 0), OPT_FLAG("interlaced-only", interlaced_only, 0), @@ -542,16 +525,25 @@ static const m_option_t vf_opts_fields[] = { {0} }; -const vf_info_t vf_info_d3d11vpp = { - .description = "D3D11 Video Post-Process Filter", - .name = "d3d11vpp", - .test_conversion = test_conversion, - .open = vf_open, - .priv_size = sizeof(struct vf_priv_s), - .priv_defaults = &(const struct vf_priv_s) { - .deint_enabled = 1, - .interlaced_only = 1, - .mode = D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_BOB, +const struct mp_user_filter_entry vf_d3d11vpp = { + .desc = { + .description = "D3D11 Video Post-Process Filter", + .name = "d3d11vpp", + .priv_size = sizeof(OPT_BASE_STRUCT), + .priv_defaults = &(const OPT_BASE_STRUCT) { + .deint_enabled = 1, + .interlaced_only = 1, + .mode = D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_BOB, + }, + .options = vf_opts_fields, }, - .options = vf_opts_fields, + .create = vf_d3d11vpp_create, }; + +// Create a filter for the purpose of converting the sub-format for hwdec +// interops which are incapable of handling some formats (ANGLE). +struct mp_filter *vf_d3d11_crea