From patchwork Fri Feb 11 22:08:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergio Acereda X-Patchwork-Id: 34234 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:14aa:0:0:0:0 with SMTP id bz10csp3599704nkb; Fri, 11 Feb 2022 14:09:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJxZqeL/sZHhK9SKvWpDMrg0zm3EWOmIGps/wBDG5m5wgtu4s6W1SMVxyIYN/nJulZWeCns1 X-Received: by 2002:a05:6402:530a:: with SMTP id eo10mr4116954edb.133.1644617346107; Fri, 11 Feb 2022 14:09:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644617346; cv=none; d=google.com; s=arc-20160816; b=wtH78/qTsbnHe5ZEuqFRD/zgnh4X/PdBVz5LtRVFZflm15L8MtQVUsUzl1vGWGOKbf 7HDjBUbaD1JHiqSWO4obFbks88g017NFutE0a2JrWLMUhunGnWJupRoyzxF1UDj1zBv+ t2gEvjKXgUGJ5t0Lb/62BnwJ3uUCY4ZSG3rOCCc1toMwi5ItL53SuS1VDKxk3tHJuqMi +ugvsf0Fa0YyjxH6JJYBvccw02EmqQZL5lpYklAhhPJcWhFLRc0C4ijzzPCHLFVATzFP ySS9xUR9ir6W9u109CiuWjnYwHMona/UHjd/GkXQboI8purDiQFHzfh7epJBSMMSIyCV 4V5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:content-language:to:from:user-agent:mime-version :date:message-id:dkim-signature:delivered-to; bh=impa6wgZrdoOSDkKQhmLmNa7QQmLWqu3A+b/tV46i1g=; b=0v+DdeL6ABte/OYuMVQCHP3xwYpbwIZZIKQNJ5MKiZdPgZx8UwTGo9RBUKu7Uk/btE Tdq1mDDO22P3YbC5sp2HdLizMPEPe96TLcl0ssY0yWP6BFfdBixOki2kqp94zjFNUwyf VT0pZkKePVDhZjTBAmpFCvrkytFtoyR4qE39EBG6/1IKXeITwjTHOMKgfJz24BEvXgPK 54TgJ2D1rGmhWffRDXuL1o2/K89kOo4EPDaRQUAKa24unqVhwDUqSwgE/gyt+j3QlhEX TKtvr5yuOW0grZoCjS2CYaq7K40c69Zh/d8OAzjO3g4aqHRyJB4BgA/HHhqKPiLs/lFX F95Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=KSnEUYeP; 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 ek25si13033303edb.485.2022.02.11.14.08.43; Fri, 11 Feb 2022 14:09:06 -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=KSnEUYeP; 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 2262168B21E; Sat, 12 Feb 2022 00:08:39 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A020268AEE0 for ; Sat, 12 Feb 2022 00:08:31 +0200 (EET) Received: by mail-wr1-f47.google.com with SMTP id q7so17307303wrc.13 for ; Fri, 11 Feb 2022 14:08:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:from:subject:to :content-language:content-transfer-encoding; bh=LBTcc4tI0Cbai9Aj2scDq0GIuzMfLadqOf05Z3rsVFA=; b=KSnEUYeP5azWA6kwNHxchqalZuS8i5IYL1sZFzEZQaLyvVdCqNhgWyXPEh176kq+7h Nd1t3FNq2GdSGB21/rdEtmn9CWjIC5ivY1S+Bi90hWaOiXuIgs3G28t15zdZQhJPqDPy f7/R79Otx+LFCRxE/Fr5zddVjlRq5mn7X7e3MmozyvGpDZO/48UJxnjWCDQqfLRVLs1T GgeLGuNS/wBJayIx0iou4T3fAu9GXzrxWemmYogp5u51ofH90zPj2eALgCX+DSBLCfhD BA7FlKUGgnYDu6Ont7JfIqyD0VLHIOuhyGDyJmnT64PB+oNFaWtLs3g5GjuwbXE9uqvR WCJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:from :subject:to:content-language:content-transfer-encoding; bh=LBTcc4tI0Cbai9Aj2scDq0GIuzMfLadqOf05Z3rsVFA=; b=bVKHwhdkKEVQbK9VjL+3/G4c46GCJHeoIUTVsgC2uQdyQOFDjA48lbuPoRusHAifAX GyozpDVpY2BJrYZakONDWqwUQ4oRXATlh2YfgKIJV5sjAUMaUMXtHdrRwi+T+YJQ8qZa qUVqD8SMfIiQsb33CdNIsyAuKr/Wdak4Q2HEh3WN1bJYUh8yVs+J7rI3rKBY0hSPswYZ ywifS8/v0+7Ij0qG8XBRPnQZd5iyBbIBgcGtW4oO+76JavAnlty6uUmd/+qXUBHe4RGY yeRzIpjLjW1BPY7i44vIftMNGm5+7sAFpsdqI4jAhbcyRIU3NwS+miRi8uQa8IWpdEgt JiHQ== X-Gm-Message-State: AOAM533P7XlhaYx2rVtK481Za9iVY3lKk+58ODCh8SuACNrekHAKccuV qJIrfQNeZn9SQxSVLshb0LHQIh25MsiTKg== X-Received: by 2002:a05:6000:188:: with SMTP id p8mr2829148wrx.84.1644617310520; Fri, 11 Feb 2022 14:08:30 -0800 (PST) Received: from [192.168.1.39] (132.red-81-34-167.dynamicip.rima-tde.net. [81.34.167.132]) by smtp.gmail.com with ESMTPSA id u14sm6485508wmq.41.2022.02.11.14.08.29 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 11 Feb 2022 14:08:29 -0800 (PST) Message-ID: <84c88034-b1db-6ccd-7f92-507c251c7368@gmail.com> Date: Fri, 11 Feb 2022 23:08:28 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:91.0) Gecko/20100101 Thunderbird/91.6.0 From: Sergio Acereda To: ffmpeg-devel@ffmpeg.org Content-Language: en-US Subject: [FFmpeg-devel] [PATCH] 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: opp6sl+PWDQw This patch should allow to play 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 | 34 ++++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 10 deletions(-) - 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 = abs(last_index - first_index) + 1; } } @@ -413,11 +420,12 @@ int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt) AVCodecParameters *par = s1->streams[0]->codecpar; if (!s->is_pipe) { + int bad = s->reverse? (s->img_number < s->img_last) : (s->img_number > s->img_last); /* loop over input */ - if (s->loop && s->img_number > s->img_last) { + if (s->loop && bad) { s->img_number = s->img_first; } - if (s->img_number > s->img_last) + if (bad) return AVERROR_EOF; if (s->pattern_type == PT_NONE) { av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes)); @@ -554,8 +562,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; } @@ -594,9 +603,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) + int bad = s1->reverse? (timestamp < s1->img_last) : (timestamp > s1->img_last); + if (timestamp < 0 || !s1->loop && bad) return -1; - s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first; + int dir = s1->reverse? -1 : 1; + int span = 1 + dir * (s1->img_last - s1->img_first); + int rel = timestamp % span; + s1->img_number = s1->img_first + dir * rel; s1->pts = timestamp; return 0; } @@ -619,6 +632,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" }, 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..7b85e01aca 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,