From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 173674100E for ; Sat, 12 Feb 2022 11:59:19 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 67D5968B2A6; Sat, 12 Feb 2022 13:59:18 +0200 (EET) Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 62F8F68B213 for ; Sat, 12 Feb 2022 13:59:11 +0200 (EET) Received: by mail-wr1-f49.google.com with SMTP id s10so5785446wrb.1 for ; Sat, 12 Feb 2022 03:59:11 -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=FIs0WEV7QlNjR1RJoYSVjmuD1UfmQgNo3o0xk8Hogbw=; b=DWiXCyJYoPun2hr8YFyhq+LNRX3hDzQlP3u2UWKJkB0STKWwHLWCFYps+jdlRpGefR AQgjg26rcLGPv0DU2ffmSXbmRx81OY13Hn3TPqQv7j6Nrr3txWBQ7M1nojwgr3WKThbG nyHzj8TxNMdre91eEG/APRjVcF2sVeZOfOYy1ypJr0cyUQS4AObVL3B5D1kScQ2jLW5n F6njFcUKfjPW2KIKYYg9drTn6RKMhM54Fe2zgEwqMrqkOFvStnMZBPuNK2Us2W7wCcDf 1mf1M09eRa2rRXsDQWnCV2Jkv2svQ/P9KFb4I+mtxO5DShUV41xdCHj+wAIL7ZnF44uX ImcA== 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=FIs0WEV7QlNjR1RJoYSVjmuD1UfmQgNo3o0xk8Hogbw=; b=1Jk8Wgb+TqRTkIZ/dkLnB8hMuBnfsJTgqCgAq2OguVjy1eecBb4b4xOB2jIF3OrSZL GjHfTEk/XlxblQQ/SDKUtfE5X0jtJPxkHRaHwooaL/W9Bryy6t6TT9o4yFLG0cAA4pSJ 52W41R2hYynEy6UGI51VufVKSRT9deJPl8X9icvHknwGByq1USGA3XZ4ch0/M3c+kQQA bBfPi78uQUqRvVgkUGruZrkMZwXwl9KkWYLtoIouht4ENg5LJ9D+VWlVqYKQI5LbO60b wQ4p4/5JjqzM+kOlxRIy+h4RlRJ/iEf6KMNgFWxfupmKfCYOlwY0uOIFA0YoiZTLNdat esMg== X-Gm-Message-State: AOAM531tr9U3fgsoMUYLyiC5w9drz2uk2DgV9fFANu14RdWX46P0CafC Web9fV9Kxfyyv/qMVGFqYe7b30Kf1g+TUw== X-Google-Smtp-Source: ABdhPJwvqqZsyC7Me5AT8ORygJmfAbx5H/VtRxXCdnNeJkKnMZ0vF55O1ef2VxBRp64oC+w0EM60ng== X-Received: by 2002:a5d:6506:: with SMTP id x6mr4735135wru.164.1644667150520; Sat, 12 Feb 2022 03:59:10 -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 l31sm1052153wms.0.2022.02.12.03.59.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Feb 2022 03:59:09 -0800 (PST) From: Sergio Acereda To: ffmpeg-devel@ffmpeg.org Date: Sat, 12 Feb 2022 12:57:11 +0100 Message-Id: <20220212115709.45936-1-sergio.acereda@gmail.com> X-Mailer: git-send-email 2.33.1 MIME-Version: 1.0 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 Cc: Sergio Acereda Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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 | 40 +++++++++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 13 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..f7ad85be08 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 = 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; } @@ -585,6 +594,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 bad = s1->reverse? (timestamp < s1->img_last) : (timestamp > s1->img_last); if (s1->ts_from_file) { int index = av_index_search_timestamp(st, timestamp, flags); @@ -592,12 +602,15 @@ static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp return -1; s1->img_number = ffstream(st)->index_entries[index].pos; return 0; - } - - if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first) + } else if (timestamp < 0 || !s1->loop && bad) return -1; - s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first; - s1->pts = timestamp; + else { + 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" }, -- 2.33.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".