diff options
Diffstat (limited to 'video/filter/vf_lavfi.c')
-rw-r--r-- | video/filter/vf_lavfi.c | 90 |
1 files changed, 55 insertions, 35 deletions
diff --git a/video/filter/vf_lavfi.c b/video/filter/vf_lavfi.c index a66bd2a971..d79bfd4011 100644 --- a/video/filter/vf_lavfi.c +++ b/video/filter/vf_lavfi.c @@ -26,6 +26,7 @@ #include <assert.h> #include <libavutil/avstring.h> +#include <libavutil/hwcontext.h> #include <libavutil/mem.h> #include <libavutil/mathematics.h> #include <libavutil/rational.h> @@ -44,6 +45,7 @@ #include "options/m_option.h" #include "common/tags.h" +#include "video/hwdec.h" #include "video/img_format.h" #include "video/mp_image.h" #include "video/sws_utils.h" @@ -108,7 +110,8 @@ static bool recreate_graph(struct vf_instance *vf, struct mp_image_params *fmt) { void *tmp = talloc_new(NULL); struct vf_priv_s *p = vf->priv; - AVFilterContext *in = NULL, *out = NULL, *f_format = NULL; + AVFilterContext *in = NULL, *out = NULL; + int ret; if (bstr0(p->cfg_graph).len == 0) { MP_FATAL(vf, "lavfi: no filter graph set\n"); @@ -130,53 +133,56 @@ static bool recreate_graph(struct vf_instance *vf, struct mp_image_params *fmt) if (!outputs || !inputs) goto error; - // Build list of acceptable output pixel formats. libavfilter will insert - // conversion filters if needed. - char *fmtstr = talloc_strdup(tmp, ""); - for (int n = IMGFMT_START; n < IMGFMT_END; n++) { - if (vf_next_query_format(vf, n)) { - const char *name = av_get_pix_fmt_name(imgfmt2pixfmt(n)); - if (name) { - const char *s = fmtstr[0] ? "|" : ""; - fmtstr = talloc_asprintf_append_buffer(fmtstr, "%s%s", s, name); - } - } - } - char *sws_flags = talloc_asprintf(tmp, "flags=%"PRId64, p->cfg_sws_flags); graph->scale_sws_opts = av_strdup(sws_flags); - AVRational timebase = AV_TIME_BASE_Q; - - char *src_args = talloc_asprintf(tmp, "%d:%d:%d:%d:%d:%d:%d", - fmt->w, fmt->h, imgfmt2pixfmt(fmt->imgfmt), - timebase.num, timebase.den, - fmt->p_w, fmt->p_h); + in = avfilter_graph_alloc_filter(graph, avfilter_get_by_name("buffer"), "src"); + if (!in) + goto error; - if (avfilter_graph_create_filter(&in, avfilter_get_by_name("buffer"), - "src", src_args, NULL, graph) < 0) + AVBufferSrcParameters *in_params = av_buffersrc_parameters_alloc(); + if (!in_params) goto error; - if (avfilter_graph_create_filter(&out, avfilter_get_by_name("buffersink"), - "out", NULL, NULL, graph) < 0) + in_params->format = imgfmt2pixfmt(fmt->imgfmt); + in_params->time_base = AV_TIME_BASE_Q; + in_params->width = fmt->w; + in_params->height = fmt->h; + in_params->sample_aspect_ratio.num = fmt->p_w; + in_params->sample_aspect_ratio.den = fmt->p_h; + // Assume it's ignored for non-hwaccel formats. + in_params->hw_frames_ctx = vf->in_hwframes_ref; + + ret = av_buffersrc_parameters_set(in, in_params); + av_free(in_params); + if (ret < 0) goto error; - if (avfilter_graph_create_filter(&f_format, avfilter_get_by_name("format"), - "format", fmtstr, NULL, graph) < 0) + if (avfilter_init_str(in, NULL) < 0) goto error; - if (avfilter_link(f_format, 0, out, 0) < 0) + if (avfilter_graph_create_filter(&out, avfilter_get_by_name("buffersink"), + "out", NULL, NULL, graph) < 0) goto error; outputs->name = av_strdup("in"); outputs->filter_ctx = in; inputs->name = av_strdup("out"); - inputs->filter_ctx = f_format; + inputs->filter_ctx = out; if (graph_parse(graph, p->cfg_graph, inputs, outputs, NULL) < 0) goto error; + if (vf->hwdec_devs) { + struct mp_hwdec_ctx *hwdec = hwdec_devices_get_first(vf->hwdec_devs); + for (int n = 0; n < graph->nb_filters; n++) { + AVFilterContext *filter = graph->filters[n]; + if (hwdec && hwdec->av_device_ref) + filter->hw_device_ctx = av_buffer_ref(hwdec->av_device_ref); + } + } + if (avfilter_graph_config(graph, NULL) < 0) goto error; @@ -233,17 +239,25 @@ static int reconfig(struct vf_instance *vf, struct mp_image_params *in, out->p_w = l_out->sample_aspect_ratio.num; out->p_h = l_out->sample_aspect_ratio.den; out->imgfmt = pixfmt2imgfmt(l_out->format); + av_buffer_unref(&vf->out_hwframes_ref); +#if LIBAVFILTER_VERSION_INT >= AV_VERSION_INT(6, 69, 100) && \ + LIBAVFILTER_VERSION_MICRO >= 100 + AVBufferRef *hw_frames_ctx = av_buffersink_get_hw_frames_ctx(p->out); +#else + AVBufferRef *hw_frames_ctx = l_out->hw_frames_ctx; +#endif + if (hw_frames_ctx) { + AVHWFramesContext *fctx = (void *)hw_frames_ctx->data; + out->hw_subfmt = pixfmt2imgfmt(fctx->sw_format); + vf->out_hwframes_ref = av_buffer_ref(hw_frames_ctx); + } return 0; } static int query_format(struct vf_instance *vf, unsigned int fmt) { - // We accept all sws-convertable formats as inputs. Output formats are - // handled in config(). The current public libavfilter API doesn't really - // allow us to do anything more sophisticated. - // This breaks with filters which accept input pixel formats not - // supported by libswscale. - return !!mp_sws_supported_format(fmt); + // Format negotiation is not possible with libavfilter. + return 1; } static AVFrame *mp_to_av(struct vf_instance *vf, struct mp_image *img) @@ -275,7 +289,7 @@ static struct mp_image *av_to_mp(struct vf_instance *vf, AVFrame *av_frame) static void get_metadata_from_av_frame(struct vf_instance *vf, AVFrame *frame) { -#if HAVE_AVFRAME_METADATA +#if LIBAVUTIL_VERSION_MICRO >= 100 struct vf_priv_s *p = vf->priv; if (!p->metadata) p->metadata = talloc_zero(p, struct mp_tags); @@ -298,6 +312,12 @@ static int filter_ext(struct vf_instance *vf, struct mp_image *mpi) if (!p->graph) return -1; + if (!mpi) { + if (p->eof) + return 0; + p->eof = true; + } + AVFrame *frame = mp_to_av(vf, mpi); int r = av_buffersrc_add_frame(p->in, frame) < 0 ? -1 : 0; av_frame_free(&frame); |