From patchwork Sat Apr 28 17:05:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wm4 X-Patchwork-Id: 8685 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:155:0:0:0:0:0 with SMTP id c82-v6csp1882261jad; Sat, 28 Apr 2018 10:12:08 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoSBIcQs/aV4MJNwUE90s7WotOgKZOhArPectkVO5aokqEjWmUOyw6Y3pP710SjH7M3vibY X-Received: by 10.28.12.134 with SMTP id 128mr3686538wmm.18.1524935528777; Sat, 28 Apr 2018 10:12:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524935528; cv=none; d=google.com; s=arc-20160816; b=P6A/rkkuWnGWaZXniT+8Sfabt0tOsL4bo5BKN/5A7Zi5JW7NrcfxHU3OAhNx8SWp8N L97MvyP7WuPSV454lybHrtnpOLx614JL/pqiTlYLEw/58XtClj0wA+moxu2orzlNcqgW HLMxGykjBoUCmCbHvEmvzWjA0Y09Rj/hpKplCkDfMweWdim2c6J+ehx2U1iiSnEbgKmC wIUf/CwpAnm6CL3iYzKNg4GRmiiaGfLOjFaJqMVAoI1LhHngR6K1QRf0wQOyQUszJNCC xISMEWYIUiF2qIf22nfMgXuZGPfitfRB4XfwV1DCKnmxfvI/6+MPMR9DR3rb3/dEeya7 Xkgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=ofao28S6nMwQy1JrWEsn5a1KFIuUKwpVkZh36dkIy78=; b=aEkkNQvYqZQaWTE/kUhw9uz/HuGMOWEzrVdILh6xt3V1jws8y2bcchfXAk7CkEix4c NPdfJwH2Pl4HigY6jbBCNv6JiEzJsPFZXcKARSTPkcq2v5Ao8REfy/m+WqzSqL9qXMcY hoOqSpJEsPvbGh4sTFpf0aO3meggmn+FuP8JO7yZxWVYNWqO3ok9gAx6rEh+jtCy0SDi aLI0TYaakYciDsfjBEI3VPMBIBUGjmic8iuvRfxdJLK9MhruaU7SNCqO06+xMnVGs2sn moLR76z9/foYdpVZru07+LKXo+UiSqvsaRE0HV3VAlJvPjC4PcWlsoRfsRr59iKWmH7U x5tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@googlemail.com header.s=20161025 header.b=ECFMZttZ; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id c3-v6si3140912wrd.398.2018.04.28.10.12.07; Sat, 28 Apr 2018 10:12:08 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@googlemail.com header.s=20161025 header.b=ECFMZttZ; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8162168A584; Sat, 28 Apr 2018 20:11:34 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f52.google.com (mail-wm0-f52.google.com [74.125.82.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 99C4B68A540 for ; Sat, 28 Apr 2018 20:11:28 +0300 (EEST) Received: by mail-wm0-f52.google.com with SMTP id j4so7114653wme.1 for ; Sat, 28 Apr 2018 10:12:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=XWM8P7CWUfwtfM0WOZYgR7OR/zhti9Hivvz923A/Gng=; b=ECFMZttZT3nUIAd/f1ROHQU60EHNh/+xmkUfZE9ZprGUoMDhksUoy0lP1d6cJG9CRT xK+6PogExKP3JIgnw8pyNBzNq2CqUD/fzS/+vP07sTxUnTrgZl0qeRP8vYegGbfQTHuP I0600RSDssh+J0W8Pp/G1vgpidnOqbLV3tckUKpDSwloA8B9wI1SxWJWuCQktXOBVn/P GM4W3pR0rUaXm6iFOkUeboJyOza7IaHbzx4A3y5qiPePTwAL1ajHNfA6HbNyb4Ukmbah ifvqcuw4fEpCN5IuIBA+LA4MJFGjLI/HqV4/sIcVTw7+wc2ojYM2ybfN5SkZAmlM1EqP cEpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=XWM8P7CWUfwtfM0WOZYgR7OR/zhti9Hivvz923A/Gng=; b=tXzu6CqIDQ2k/e8GLPU+KycmnCF9k0HPK+ogke9VDZBeR7OYPwxlVRvtQHgDSQlgxi 9PiABTZw15gRX42AuI5OWHMKmqN3UgVov90EBjOQWng+8SCfpZOCoMnHtNpaUG71CkMW rSpbZt+VtAY/SgoYJK7XA6A3ufZwngpMG9ACKASUMhoZTDwWepMBUdeumvx3ptRomozN RcjjF3v1yUMpWQTU9sOB1/YMheBCc8UKhTq1TDf2h+KgiSranBLuOT1aCYefyNkMMlhX 8CFNKX0qlr/Sjky9QWKWTAeBNXCeHXfmcu6faI3KfOeBXOi3orpRQ29s2/YZ7bixNiq9 E0yA== X-Gm-Message-State: ALQs6tD1ttvq2TY+Uvm2H6udN4T7LxHTIGihQtFiijgWy6URAKfdaWWo J/PExI9doygkdfsnTnx5c+IGlA== X-Received: by 10.28.3.8 with SMTP id 8mr3820476wmd.77.1524935114073; Sat, 28 Apr 2018 10:05:14 -0700 (PDT) Received: from debian.speedport.ip (p2003006CCD400529356A485EC7273119.dip0.t-ipconnect.de. [2003:6c:cd40:529:356a:485e:c727:3119]) by smtp.googlemail.com with ESMTPSA id b185sm3578606wmb.25.2018.04.28.10.05.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 28 Apr 2018 10:05:13 -0700 (PDT) From: wm4 To: ffmpeg-devel@ffmpeg.org Date: Sat, 28 Apr 2018 19:05:29 +0200 Message-Id: <20180428170529.18504-1-nfxjfg@googlemail.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180427193723.8361-1-nfxjfg@googlemail.com> References: <20180427193723.8361-1-nfxjfg@googlemail.com> Subject: [FFmpeg-devel] [PATCH] avformat: add vapoursynth wrapper X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: wm4 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This can "demux" .vpy files. Some minor code copied from other LGPL parts of FFmpeg. I did not found a good way to test a few of the more obscure features, like VFR nodes, compat pixel formats, or nodes with dynamic size/format changes. These can be easily implemented on demand. --- configure | 5 + libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/vapoursynth.c | 421 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 428 insertions(+) create mode 100644 libavformat/vapoursynth.c diff --git a/configure b/configure index 9fa1665496..fcc93ecc30 100755 --- a/configure +++ b/configure @@ -303,6 +303,7 @@ External library support: --disable-sdl2 disable sdl2 [autodetect] --disable-securetransport disable Secure Transport, needed for TLS support on OSX if openssl and gnutls are not used [autodetect] + --enable-vapoursynth enable VapourSynth demuxer [no] --disable-xlib disable xlib [autodetect] --disable-zlib disable zlib [autodetect] @@ -1724,6 +1725,7 @@ EXTERNAL_LIBRARY_LIST=" mediacodec openal opengl + vapoursynth " HWACCEL_AUTODETECT_LIBRARY_LIST=" @@ -3088,6 +3090,7 @@ libx265_encoder_deps="libx265" libxavs_encoder_deps="libxavs" libxvid_encoder_deps="libxvid" libzvbi_teletext_decoder_deps="libzvbi" +vapoursynth_demuxer_deps="vapoursynth" videotoolbox_suggest="coreservices" videotoolbox_deps="corefoundation coremedia corevideo" videotoolbox_encoder_deps="videotoolbox VTCompressionSessionPrepareToEncodeFrames" @@ -6130,6 +6133,8 @@ enabled rkmpp && { require_pkg_config rkmpp rockchip_mpp rockchip/r { enabled libdrm || die "ERROR: rkmpp requires --enable-libdrm"; } } +enabled vapoursynth && require_pkg_config vapoursynth "vapoursynth-script >= 42" VSScript.h vsscript_init + if enabled gcrypt; then GCRYPT_CONFIG="${cross_prefix}libgcrypt-config" diff --git a/libavformat/Makefile b/libavformat/Makefile index 3eeca5091d..e1e74a8f43 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -570,6 +570,7 @@ OBJS-$(CONFIG_LIBRTMPTE_PROTOCOL) += librtmp.o OBJS-$(CONFIG_LIBSRT_PROTOCOL) += libsrt.o OBJS-$(CONFIG_LIBSSH_PROTOCOL) += libssh.o OBJS-$(CONFIG_LIBSMBCLIENT_PROTOCOL) += libsmbclient.o +OBJS-$(CONFIG_VAPOURSYNTH_DEMUXER) += vapoursynth.o # protocols I/O OBJS-$(CONFIG_ASYNC_PROTOCOL) += async.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index d582778b3b..a94364f41d 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -482,6 +482,7 @@ extern AVOutputFormat ff_chromaprint_muxer; extern AVInputFormat ff_libgme_demuxer; extern AVInputFormat ff_libmodplug_demuxer; extern AVInputFormat ff_libopenmpt_demuxer; +extern AVInputFormat ff_vapoursynth_demuxer; #include "libavformat/muxer_list.c" #include "libavformat/demuxer_list.c" diff --git a/libavformat/vapoursynth.c b/libavformat/vapoursynth.c new file mode 100644 index 0000000000..6758a7bf31 --- /dev/null +++ b/libavformat/vapoursynth.c @@ -0,0 +1,421 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** +* @file +* VapourSynth demuxer +* +* Synthesizes vapour (?) +*/ + +#include + +#include +#include + +#include "libavutil/avassert.h" +#include "libavutil/avstring.h" +#include "libavutil/eval.h" +#include "libavutil/imgutils.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "avformat.h" +#include "internal.h" + +typedef struct VSContext { + const AVClass *class; + + const VSAPI *vsapi; + VSCore *vscore; + VSScript *vss; + int did_init; + + VSNodeRef *outnode; + int is_cfr; + int current_frame; + + int c_order[4]; + + /* options */ + int64_t max_size; +} VSContext; + +#define OFFSET(x) offsetof(VSContext, x) +#define A AV_OPT_FLAG_AUDIO_PARAM +#define D AV_OPT_FLAG_DECODING_PARAM +static const AVOption options[] = { + {"max_size", "set max file size supported (in bytes)", OFFSET(max_size), AV_OPT_TYPE_INT64, {.i64 = 1 * 1024 * 1024}, 0, SIZE_MAX - 1, A|D}, + {NULL} +}; + +static av_cold int read_close_vs(AVFormatContext *s) +{ + VSContext *vs = s->priv_data; + + if (vs->outnode) + vs->vsapi->freeNode(vs->outnode); + + vsscript_freeScript(vs->vss); + vs->vss = NULL; + vs->vsapi = NULL; + vs->vscore = NULL; + vs->outnode = NULL; + + if (vs->did_init) + vsscript_finalize(); + vs->did_init = 0; + + return 0; +} + +static av_cold int is_native_endian(enum AVPixelFormat pixfmt) +{ + enum AVPixelFormat other = av_pix_fmt_swap_endianness(pixfmt); + const AVPixFmtDescriptor *pd; + if (other == AV_PIX_FMT_NONE || other == pixfmt) + return 1; // not affected by byte order + pd = av_pix_fmt_desc_get(pixfmt); + return pd && (!!HAVE_BIGENDIAN == !!(pd->flags & AV_PIX_FMT_FLAG_BE)); +} + +static av_cold enum AVPixelFormat match_pixfmt(const VSFormat *vsf, int c_order[4]) +{ + static const int yuv_order[4] = {0, 1, 2, 0}; + static const int rgb_order[4] = {1, 2, 0, 0}; + const AVPixFmtDescriptor *pd; + + for (pd = av_pix_fmt_desc_next(NULL); pd; pd = av_pix_fmt_desc_next(pd)) { + int is_rgb, is_yuv, i; + const int *order; + enum AVPixelFormat pixfmt; + + pixfmt = av_pix_fmt_desc_get_id(pd); + + if (pd->flags & (AV_PIX_FMT_FLAG_BAYER | AV_PIX_FMT_FLAG_ALPHA | + AV_PIX_FMT_FLAG_HWACCEL | AV_PIX_FMT_FLAG_BITSTREAM)) + continue; + + if (pd->log2_chroma_w != vsf->subSamplingW || + pd->log2_chroma_h != vsf->subSamplingH) + continue; + + is_rgb = vsf->colorFamily == cmRGB; + if (is_rgb != !!(pd->flags & AV_PIX_FMT_FLAG_RGB)) + continue; + + is_yuv = vsf->colorFamily == cmYUV || + vsf->colorFamily == cmYCoCg || + vsf->colorFamily == cmGray; + if (!is_rgb && !is_yuv) + continue; + + if (vsf->sampleType != ((pd->flags & AV_PIX_FMT_FLAG_FLOAT) ? stFloat : stInteger)) + continue; + + if (av_pix_fmt_count_planes(pixfmt) != vsf->numPlanes) + continue; + + if (strncmp(pd->name, "xyz", 3) == 0) + continue; + + if (!is_native_endian(pixfmt)) + continue; + + order = is_yuv ? yuv_order : rgb_order; + + for (i = 0; i < pd->nb_components; i++) { + const AVComponentDescriptor *c = &pd->comp[i]; + if (order[c->plane] != i || + c->offset != 0 || c->shift != 0 || + c->step != vsf->bytesPerSample || + c->depth != vsf->bitsPerSample) + goto cont; + } + + // Use it. + memcpy(c_order, order, sizeof(int[4])); + return pixfmt; + + cont: ; + } + + return AV_PIX_FMT_NONE; +} + +static av_cold int read_header_vs(AVFormatContext *s) +{ + AVStream *st; + AVIOContext *pb = s->pb; + VSContext *vs = s->priv_data; + int64_t sz = avio_size(pb); + char *buf = NULL; + char dummy; + const VSVideoInfo *info; + int err; + + if (!vsscript_init()) { + av_log(s, AV_LOG_ERROR, "Failed to initialize VSScript (possibly PYTHONPATH not set).\n"); + err = AVERROR_EXTERNAL; + goto done; + } + + vs->did_init = 1; + + if (sz < 0 || sz > vs->max_size) { + if (sz < 0) + av_log(s, AV_LOG_WARNING, "Could not determine file size\n"); + sz = vs->max_size; + } + + buf = av_malloc(sz + 1); + if (!buf) { + err = AVERROR(ENOMEM); + goto done; + } + sz = avio_read(pb, buf, sz); + + if (sz < 0) { + av_log(s, AV_LOG_ERROR, "Could not read script.\n"); + err = sz; + goto done; + } + + // Data left means our buffer (the max_size option) is too small + if (avio_read(pb, &dummy, 1) == 1) { + av_log(s, AV_LOG_ERROR, "File size is larger than max_size option " + "value %"PRIi64", consider increasing the max_size option\n", + vs->max_size); + err = AVERROR_BUFFER_TOO_SMALL; + goto done; + } + + if (vsscript_createScript(&vs->vss)) { + av_log(s, AV_LOG_ERROR, "Failed to create script instance.\n"); + err = AVERROR_EXTERNAL; + goto done; + } + + buf[sz] = '\0'; + if (vsscript_evaluateScript(&vs->vss, buf, s->url, 0)) { + const char *msg = vsscript_getError(vs->vss); + av_log(s, AV_LOG_ERROR, "Failed to parse script: %s\n", msg ? msg : "(unknown)"); + err = AVERROR_EXTERNAL; + goto done; + } + + vs->vsapi = vsscript_getVSApi(); + vs->vscore = vsscript_getCore(vs->vss); + + vs->outnode = vsscript_getOutput(vs->vss, 0); + if (!vs->outnode) { + av_log(s, AV_LOG_ERROR, "Could not get script output node.\n"); + err = AVERROR_EXTERNAL; + goto done; + } + + st = avformat_new_stream(s, NULL); + if (!st) { + err = AVERROR(ENOMEM); + goto done; + } + + info = vs->vsapi->getVideoInfo(vs->outnode); + + if (!info->format || !info->width || !info->height) { + av_log(s, AV_LOG_ERROR, "Non-constant input format not supported.\n"); + err = AVERROR_PATCHWELCOME; + goto done; + } + + if (info->fpsDen) { + vs->is_cfr = 1; + avpriv_set_pts_info(st, 64, info->fpsDen, info->fpsNum); + st->duration = info->numFrames; + } else { + // VFR. Just set "something". + avpriv_set_pts_info(st, 64, 1, AV_TIME_BASE); + s->ctx_flags |= AVFMTCTX_UNSEEKABLE; + } + + st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; + st->codecpar->codec_id = AV_CODEC_ID_WRAPPED_AVFRAME; + st->codecpar->width = info->width; + st->codecpar->height = info->height; + st->codecpar->format = match_pixfmt(info->format, vs->c_order); + + if (st->codecpar->format == AV_PIX_FMT_NONE) { + av_log(s, AV_LOG_ERROR, "Unsupported VS pixel format %s\n", info->format->name); + err = AVERROR_EXTERNAL; + goto done; + } + av_log(s, AV_LOG_VERBOSE, "VS format %s -> pixfmt %s\n", info->format->name, + av_get_pix_fmt_name(st->codecpar->format)); + + if (info->format->colorFamily == cmYCoCg) + st->codecpar->color_space = AVCOL_SPC_YCGCO; + +done: + av_free(buf); + if (err < 0) + read_close_vs(s); + return err; +} + +static void free_frame(void *opaque, uint8_t *data) +{ + AVFrame *frame = (AVFrame *)data; + + av_frame_free(&frame); +} + +static int get_vs_prop_int(AVFormatContext *s, const VSMap *map, const char *name, int def) +{ + VSContext *vs = s->priv_data; + int64_t res; + int err = 1; + + res = vs->vsapi->propGetInt(map, name, 0, &err); + return err || res < INT_MIN || res > INT_MAX ? def : res; +} + +static int read_packet_vs(AVFormatContext *s, AVPacket *pkt) +{ + VSContext *vs = s->priv_data; + AVStream *st = s->streams[0]; + AVFrame *frame = NULL; + char vserr[80]; + const VSFrameRef *vsframe = NULL; + const VSVideoInfo *info = vs->vsapi->getVideoInfo(vs->outnode); + int err = 0; + const uint8_t *src_data[4]; + int src_linesizes[4]; + const VSMap *props; + int i; + + if (vs->current_frame >= info->numFrames) + return AVERROR_EOF; + + vsframe = vs->vsapi->getFrame(vs->current_frame, vs->outnode, vserr, sizeof(vserr)); + if (!vsframe) { + av_log(s, AV_LOG_ERROR, "Error getting frame: %s\n", vserr); + err = AVERROR_EXTERNAL; + goto end; + } + + props = vs->vsapi->getFramePropsRO(vsframe); + + frame = av_frame_alloc(); + if (!frame) { + err = AVERROR(ENOMEM); + goto end; + } + + frame->format = st->codecpar->format; + frame->width = st->codecpar->width; + frame->height = st->codecpar->height; + frame->colorspace = st->codecpar->color_space; + + // Values according to ISO/IEC 14496-10. + frame->colorspace = get_vs_prop_int(s, props, "_Matrix", frame->colorspace); + frame->color_primaries = get_vs_prop_int(s, props, "_Primaries", frame->color_primaries); + frame->color_trc = get_vs_prop_int(s, props, "_Transfer", frame->color_trc); + + if (get_vs_prop_int(s, props, "_ColorRange", 1) == 0) + frame->color_range = AVCOL_RANGE_JPEG; + + frame->sample_aspect_ratio.num = get_vs_prop_int(s, props, "_SARNum", 0); + frame->sample_aspect_ratio.den = get_vs_prop_int(s, props, "_SARDen", 1); + + av_assert0(vs->vsapi->getFrameWidth(vsframe, 0) == frame->width); + av_assert0(vs->vsapi->getFrameHeight(vsframe, 0) == frame->height); + + err = av_frame_get_buffer(frame, 0); + if (err < 0) + goto end; + + for (i = 0; i < info->format->numPlanes; i++) { + int p = vs->c_order[i]; + src_data[i] = vs->vsapi->getReadPtr(vsframe, p); + src_linesizes[i] = vs->vsapi->getStride(vsframe, p); + } + + av_image_copy(frame->data, frame->linesize, src_data, src_linesizes, + frame->format, frame->width, frame->height); + + pkt->buf = av_buffer_create((uint8_t*)frame, sizeof(*frame), + free_frame, NULL, 0); + if (!pkt->buf) { + err = AVERROR(ENOMEM); + goto end; + } + + frame = NULL; // pkt owns it now + + pkt->data = pkt->buf->data; + pkt->size = pkt->buf->size; + pkt->flags |= AV_PKT_FLAG_TRUSTED; + + if (vs->is_cfr) + pkt->pts = vs->current_frame; + + vs->current_frame++; + +end: + if (err < 0) + av_packet_unref(pkt); + av_frame_free(&frame); + vs->vsapi->freeFrame(vsframe); + return err; +} + +static int read_seek_vs(AVFormatContext *s, int stream_idx, int64_t ts, int flags) +{ + VSContext *vs = s->priv_data; + + if (!vs->is_cfr) + return AVERROR(ENOSYS); + + vs->current_frame = FFMIN(FFMAX(0, ts), s->streams[0]->duration); + return 0; +} + +static av_cold int probe_vs(AVProbeData *p) +{ + // Explicitly do not support this. VS scripts are written in Python, and + // can run arbitrary code on the user's system. + return 0; +} + +static const AVClass class_vs = { + .class_name = "VapourSynth demuxer", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVInputFormat ff_vapoursynth_demuxer = { + .name = "vapoursynth", + .long_name = NULL_IF_CONFIG_SMALL("VapourSynth demuxer"), + .priv_data_size = sizeof(VSContext), + .read_probe = probe_vs, + .read_header = read_header_vs, + .read_packet = read_packet_vs, + .read_close = read_close_vs, + .read_seek = read_seek_vs, + .priv_class = &class_vs, +};