From patchwork Tue Mar 23 20:07:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 26575 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id DEFBE44A546 for ; Tue, 23 Mar 2021 22:07:54 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BA82768AA87; Tue, 23 Mar 2021 22:07:54 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qv1-f43.google.com (mail-qv1-f43.google.com [209.85.219.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 49BE168A7FC for ; Tue, 23 Mar 2021 22:07:47 +0200 (EET) Received: by mail-qv1-f43.google.com with SMTP id g8so11159313qvx.1 for ; Tue, 23 Mar 2021 13:07:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=lT26GHjPgkcrRJOmnmR0f410kibD7XHCGQzDS28y5jY=; b=qdCIpVxoFzqSEhN2OSdNXFwPCT+NSU9nN02J3nwtL5C7QDai9nBh+rjljCRUGpwvVe zuqo8HLw2eTIjx/iuAWV8Q/oNJzdet1WkngpHhlCWiQHMR/F1aM9CL23pyhT3LgQ5uZd BFIMbWlaM3XQKIyXJt6eKHi0X8GwitXp08hMqFjxcYNocUDsk0JcahHcDWlpQbVrzZdx d5/IDMfbTqmXK3Y18udL4LhIyoRSe6ccMLPVF9KmGnxgNcM1YgkXlyQree2zXM5mu6wO UOQ+WXQCGztkN9udKcu5xLeAfekKcJOms2JC4N8Hs53BcrzlCgVVZ2HzPhKAetkMxkI5 S3Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lT26GHjPgkcrRJOmnmR0f410kibD7XHCGQzDS28y5jY=; b=XTF7ING0zkS2RCmrTX9cM54REYJ5jVyHFKq/OFmeCHMTkPd5e/5bO/bfKqdCyiXaTm +HuIGtXqHkX9ZGWXjd5r8FZexsz/QFnPMFJDPWjMS3QZCrnyBy8kTDmNyZZospsO8qAC VIiQ5s9Qf55DDju1AGhQJdh0Z59hs77Ur7d063LmHC04hD/Oe7e5x7Zu/vvbtSjHj53k O83oFirYRMkEzeiX8erR+Ha/35U9Vme6TtND3XsrKesdHxoHegBfYXUArGc822aCY78C rRyOs9+Lb8knOtcrYz8e0rAO5yVauJrDKnc4LTQ9q2TQExWtk8+ItyoeBEINDB6pk9Iq 9K7Q== X-Gm-Message-State: AOAM530KazfQA8mUC2eEkoF04v7ouQBhQnQLchnutPrpKUzgt4sZP/Ac 0yhW2BexaOU7M1NjzEiMnt2fLt+p55k= X-Google-Smtp-Source: ABdhPJzGpk17kuhIrlHjBkhRq1wF4jeZecWowFQONS67SMVA//R+Uw/zPnbORaVGrFJ+Ay7rRIZ50w== X-Received: by 2002:ad4:5629:: with SMTP id cb9mr6937587qvb.62.1616530065550; Tue, 23 Mar 2021 13:07:45 -0700 (PDT) Received: from localhost.localdomain ([181.23.86.39]) by smtp.gmail.com with ESMTPSA id i78sm14217582qke.46.2021.03.23.13.07.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Mar 2021 13:07:45 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Tue, 23 Mar 2021 17:07:13 -0300 Message-Id: <20210323200713.2429-1-jamrial@gmail.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210323183622.2219-1-jamrial@gmail.com> References: <20210323183622.2219-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2] avformat/utils: add helper functions to retrieve index entries from an AVStream 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: James Almer --- Changes since v1: idx is now unsigned. The functions now allocate an AVIndexEntry the user must free, so sizeof(AVIndexEntry) is not tied to the ABI. libavformat/avformat.h | 41 ++++++++++++++++++++++++++++++++++++++++ libavformat/utils.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 765bc3b6f5..6596d6daab 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -2754,6 +2754,47 @@ int av_find_default_stream_index(AVFormatContext *s); */ int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags); +/** + * Get the index entry count for the given AVStream. + * + * @param st stream + * @return the number of index entries in the stream + */ +int av_index_get_entries_count(AVStream *st); + +/** + * Get the AVIndexEntry corresponding to the given index. + * + * @param st stream containing the requested AVIndexEntry + * @param index_entry pointer to pointer to AVIndexEntry where to store the entry. + * on success, the caller owns the returned entry and must free + * it with av_free(). On failure, or if the timestamp could not be + found, this field will remain untouched + * @param idx the desired index + * @return >= 0 on success. AVERROR(ENOENT) if no entry for the requested index could + be found. other negative AVERROR codes on failure. + */ +int av_index_get_entry(AVStream *st, AVIndexEntry **index_entry, unsigned int idx); + +/** + * Get the AVIndexEntry corresponding to the given timestamp. + * + * @param st stream containing the requested AVIndexEntry + * @param index_entry pointer to pointer to AVIndexEntry where to store the entry. + * on success, the caller owns the returned entry and must free + * it with av_free(). On failure, or if the timestamp could not be + found, this field will remain untouched + * @param timestamp timestamp to retrieve the index entry for + * @param flags if AVSEEK_FLAG_BACKWARD then the returned entry will correspond + * to the timestamp which is <= the requested one, if backward + * is 0, then it will be >= + * if AVSEEK_FLAG_ANY seek to any frame, only keyframes otherwise + * @return >= 0 on success. AVERROR(ENOENT) if no entry for the requested timestamp could + * be found. other negative AVERROR codes on failure. + */ +int av_index_get_entry_from_timestamp(AVStream *st, AVIndexEntry **index_entry, + int64_t wanted_timestamp, int flags); + /** * Add an index entry into a sorted list. Update the entry if the list * already contains it. diff --git a/libavformat/utils.c b/libavformat/utils.c index f31826f2ea..88e01f604a 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -2129,6 +2129,49 @@ int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags) wanted_timestamp, flags); } +int av_index_get_entries_count(AVStream *st) +{ + return st->internal->nb_index_entries; +} + +int av_index_get_entry(AVStream *st, AVIndexEntry **index_entry, unsigned int idx) +{ + AVIndexEntry *ie; + + if (idx >= st->internal->nb_index_entries) + return AVERROR(ENOENT); + + ie = av_memdup(&st->internal->index_entries[idx], sizeof(*ie)); + + if (!ie) + return AVERROR(ENOMEM); + + *index_entry = ie; + + return 0; +} + +int av_index_get_entry_from_timestamp(AVStream *st, AVIndexEntry **index_entry, + int64_t wanted_timestamp, int flags) +{ + AVIndexEntry *ie; + int idx = ff_index_search_timestamp(st->internal->index_entries, + st->internal->nb_index_entries, + wanted_timestamp, flags); + + if (idx < 0) + return AVERROR(ENOENT); + + ie = av_memdup(&st->internal->index_entries[idx], sizeof(*ie)); + + if (!ie) + return AVERROR(ENOMEM); + + *index_entry = ie; + + return 0; +} + static int64_t ff_read_timestamp(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit, int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t )) {