From patchwork Mon Feb 14 08:56:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergio Acereda X-Patchwork-Id: 34280 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:14aa:0:0:0:0 with SMTP id bz10csp5328179nkb; Mon, 14 Feb 2022 00:56:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJzi0KeCStjCiHwA5CQrt02mM19e78Cq/1VhQKp2NCBLFvxdItOHSnehgdA+a2pyiwoPIBP7 X-Received: by 2002:a17:907:6091:: with SMTP id ht17mr10817618ejc.607.1644829017593; Mon, 14 Feb 2022 00:56:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644829017; cv=none; d=google.com; s=arc-20160816; b=AkMlh0nUO2Hl322QzBM8+zwFvTQSAbeJkd0Ukvy/zDVseQdbdc9WoefR1GsGDeAJPk ebgszh5D6jBl3Q7algPkUV4hCeSejVWtmRRJqtIByuFu4Lr4APNcn5m8t6SeqHBiIzA5 8FiKPcMyJhs53jLB9vdwa2FqRBSycB2vDLvEaU8XWSuqHtnWZWTOf63mvSZUpV17XL6F pqVxMYsI7ugpYM50vURqibe5KFuAa8pQp+Szttoeldo0jhQAdmf+rxQkEOZC/yvwIu5d SUyXi5a3dmLpPeNjdgvzjUtCGo0Y0mDL8TI5erPelOCU5cjtAUUJUwYGU/NVHbHpI3/S Gqgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=H55gezP8xZafvDt4Sbwm4F+v5JuZF7jEaPDSzUE7xz8=; b=Fv0fgqoJtvKBNJbb7K4OMLDdthVgxMGs5/7KkNqNwy6muLVvBB/zePnwEnZbVjTEB5 +eDH+Y1Fb1zjH6TXBkaiOq0CZno4nqrT1fEH2MQzk6SKfuJVnqzTsZHegoLxvHnX8lqQ kBn1FSdilSlHqW3V1S7tO9WhJD02DM+2FgqYTOh9hKF0bai02Q/x7ZuspkaZVQYmuQOn w3KrpdsSWi+1eZlK4TrvUNGSESV1yuoz8of7pHQ5yK8vu3mK7bZhTAWGFwnUFkXuEm/9 308WQUooGx2tLPL3e9K+o6Nby9SEutSBDZYLVbLSqBpUBrhiXUttvKTdfCKXsEABo73G zK+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Z0TCMa1w; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id v13si9977039edc.222.2022.02.14.00.56.56; Mon, 14 Feb 2022 00:56:57 -0800 (PST) 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=@gmail.com header.s=20210112 header.b=Z0TCMa1w; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7C2F768B15A; Mon, 14 Feb 2022 10:56:53 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 543BB68AED4 for ; Mon, 14 Feb 2022 10:56:46 +0200 (EET) Received: by mail-wr1-f42.google.com with SMTP id i14so25541546wrc.10 for ; Mon, 14 Feb 2022 00:56:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dffI+lkTlhI3iTdmx6tyCFizxeSIzwJMdEPqrVVjB9E=; b=Z0TCMa1w+Y5oeI3kQtIzVQ8VwamoZ+2LXr9gBZOP7ZqMUt4gaK2mRaGwImR5mAS9aM 1my8LJuoHctjcNtOzGyBXuPwxlylFOjk3pujXOWDU4cVsmOxx4u583XPmCAK/kdBdjy/ Xt9qHz6bcpUnFieY+MHor8Hw30OmHepOFMMZFuFefvwQBLRuUQ1qLTpcRoMcovThplyr VsrEBxTEz7Pt9Ks1rTI+OgGMkwnzeCojGem/Owu0CL3xOK29vkdSfD6UXSRcmDtKrvkk IIfr0W1u/L6BNhGd/C77LqG558kreTdA+dQNxlsPpe2PUIlRpuLIyPlEyMJAJlM2vDV5 UiNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dffI+lkTlhI3iTdmx6tyCFizxeSIzwJMdEPqrVVjB9E=; b=nxJmkCdWWXu55gmxC9urRuQ7nQj5fMClmGjN8A6c6+5bN+jyaKy9XE2H9nYN6cDh4P TDH5YRmekpGdgn9KJstScGifAs/ljTa9WotmbNWOzqnN/qSxFLGl44HQE1UJbsHYtSV/ ZwcPaWLqrZXBdDlhd/nmHYljvaVqJj5hEevlZ9sZSUg9UyaNHftG3LDnmEi7L1Z/sPPN vTnzv+Smg+OrVrl4gjP3I9kgoy7geLbu5O3gkC/Yk8I3Gc/5RbWOxuECPP+Zv3t7q/NJ nIgiuRCm83viueG7QNR/6fy1nC5ly2QeaGL2abaJfo2PUEqtJZ8XVTcK0IsZyxVXaYWH SH+g== X-Gm-Message-State: AOAM530xrMZhb0s0zEmVVCETkuUf6oL2u6zh74GYu1aGy9UlbOcZR/vT fsk36UnQsmTjEJh7bpz1TYEDceh25phn9w== X-Received: by 2002:a5d:6ca9:: with SMTP id a9mr10495689wra.28.1644829005346; Mon, 14 Feb 2022 00:56:45 -0800 (PST) Received: from localhost.localdomain (132.red-81-34-167.dynamicip.rima-tde.net. [81.34.167.132]) by smtp.gmail.com with ESMTPSA id l9sm10931125wrt.29.2022.02.14.00.56.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Feb 2022 00:56:44 -0800 (PST) From: Sergio Acereda To: ffmpeg-devel@ffmpeg.org Date: Mon, 14 Feb 2022 09:56:30 +0100 Message-Id: <20220214085630.3294-1-sergio.acereda@gmail.com> X-Mailer: git-send-email 2.33.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] avformat/img2dec: Option to play sequence backwards X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Sergio Acereda Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: I2GdZNnuTb5z This patch should allow playing an image sequence in backwards direction, without needing to apply a reverse filter. ffmpeg -i sequence%05d.png forward.mkv ffmpeg -reverse 1 -i sequence%05d.png backward.mkv Signed-off-by: Sergio Acereda --- libavformat/img2.h | 1 + libavformat/img2dec.c | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/libavformat/img2.h b/libavformat/img2.h index 5fd8ff77fc..3d01c00537 100644 --- a/libavformat/img2.h +++ b/libavformat/img2.h @@ -59,6 +59,7 @@ typedef struct VideoDemuxData { #endif int start_number; int start_number_range; + int reverse; int frame_size; int ts_from_file; int export_path_metadata; /**< enabled when set to 1. */ diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c index 8608252d83..37efb3d618 100644 --- a/libavformat/img2dec.c +++ b/libavformat/img2dec.c @@ -105,10 +105,12 @@ static int is_glob(const char *path) * @param plast_index pointer to index updated with the last number in the range * @param path path which has to be matched by the image files in the range * @param start_index minimum accepted value for the first index in the range + * @param start_index_range range for looking at the first sequence number + * @param reverse play backwards * @return -1 if no image file could be found */ static int find_image_range(AVIOContext *pb, int *pfirst_index, int *plast_index, - const char *path, int start_index, int start_index_range) + const char *path, int start_index, int start_index_range, int reverse) { char buf[1024]; int range, last_index, range1, first_index; @@ -152,8 +154,13 @@ static int find_image_range(AVIOContext *pb, int *pfirst_index, int *plast_index break; last_index += range; } - *pfirst_index = first_index; - *plast_index = last_index; + if (reverse) { + *pfirst_index = last_index; + *plast_index = first_index; + } else { + *pfirst_index = first_index; + *plast_index = last_index; + } return 0; fail: @@ -274,7 +281,7 @@ int ff_img_read_header(AVFormatContext *s1) } if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) { if (find_image_range(s1->pb, &first_index, &last_index, s->path, - s->start_number, s->start_number_range) < 0) { + s->start_number, s->start_number_range, s->reverse) < 0) { av_log(s1, AV_LOG_ERROR, "Could find no file with path '%s' and index in the range %d-%d\n", s->path, s->start_number, s->start_number + s->start_number_range - 1); @@ -307,7 +314,7 @@ int ff_img_read_header(AVFormatContext *s1) /* compute duration */ if (!s->ts_from_file) { st->start_time = 0; - st->duration = last_index - first_index + 1; + st->duration = FFABS(last_index - first_index) + 1; } } @@ -402,6 +409,13 @@ static int add_filename_as_pkt_side_data(char *filename, AVPacket *pkt) { return 0; } +static int out_bounds(const VideoDemuxData *s) +{ + if (s->reverse) + return s->img_number < s->img_last; + return s->img_number > s->img_last; +} + int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt) { VideoDemuxData *s = s1->priv_data; @@ -414,10 +428,10 @@ int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt) if (!s->is_pipe) { /* loop over input */ - if (s->loop && s->img_number > s->img_last) { + if (s->loop && out_bounds(s)) { s->img_number = s->img_first; } - if (s->img_number > s->img_last) + if (out_bounds(s)) return AVERROR_EOF; if (s->pattern_type == PT_NONE) { av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes)); @@ -554,8 +568,9 @@ int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt) } goto fail; } else { + int step = s->reverse? -1 : 1; s->img_count++; - s->img_number++; + s->img_number += step; s->pts++; return 0; } @@ -585,6 +600,7 @@ static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp { VideoDemuxData *s1 = s->priv_data; AVStream *st = s->streams[0]; + int span; if (s1->ts_from_file) { int index = av_index_search_timestamp(st, timestamp, flags); @@ -594,10 +610,13 @@ static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp return 0; } - if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first) + span = 1 + FFABS(s1->img_last - s1->img_first); + if (timestamp < 0 || !s1->loop && timestamp > span) return -1; - s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first; - s1->pts = timestamp; + else { + s1->img_number = s1->img_first + timestamp % span; + s1->pts = timestamp; + } return 0; } @@ -619,6 +638,7 @@ const AVOption ff_img_options[] = { { "none", "disable pattern matching", 0, AV_OPT_TYPE_CONST, {.i64=PT_NONE }, INT_MIN, INT_MAX, DEC, "pattern_type" }, { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, DEC }, { "start_number_range", "set range for looking at the first sequence number", OFFSET(start_number_range), AV_OPT_TYPE_INT, {.i64 = 5}, 1, INT_MAX, DEC }, + { "reverse", "reverse direction", OFFSET(reverse), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC }, { "ts_from_file", "set frame timestamp from file's one", OFFSET(ts_from_file), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, "ts_type" }, { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" }, { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },