From patchwork Sat Apr 3 23:12:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 26733 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 E60B24493DB for ; Sun, 4 Apr 2021 02:12:44 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B8A3B68AA90; Sun, 4 Apr 2021 02:12:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 40A4C68A98D for ; Sun, 4 Apr 2021 02:12:37 +0300 (EEST) Received: by mail-qk1-f171.google.com with SMTP id 7so8408244qka.7 for ; Sat, 03 Apr 2021 16:12:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=u8s7zXKr7SaBrBftyJyhIpzodSIma7VMCE5aQQSgVc0=; b=Xe2/EEJxxMkya7Hc8ieMtavFmRI19+P0Vsu41LmZMhnPuL/dDH1hjQAcCnYpa6Uagn 7AS6hjpWVILxpP9evIT80joMFoFR+RtDEcS3YpWpN8PH8imvr2H0KdyQXMxr56SmlNpN hEIiuLjjBN2dvF6OjWm50WB4M8WFbD6HrYgb/+g5/ZUonmF9YJeKUhRpRcsgVc6geRJ7 WCFBc4HydD2PAPz0e6VSxSoe9ADm6j6WUo91qHimmHlHCO6tcMO/7Wy1PxtVSQlhUR1j J9yMl7rDdBlSi7dsgzRZr9BVDbdvnZvfT6Y4WxVrzFlETreiAb8PdIsfmPaYTDvrv4+l 6bGQ== 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:mime-version :content-transfer-encoding; bh=u8s7zXKr7SaBrBftyJyhIpzodSIma7VMCE5aQQSgVc0=; b=uTlIrOYynYnKrjf9WCndC2lL/K/zfOIKmPanfrJimUhHrrPp84vaRB8l5OpFMSRdqP w2yOpuHileGDThGGJpTIICYE2lCaDMESLojBGSNe+t/4SC3VGUacwMnPaOMmdPMwic6D HTbnWU0owQiBLiQd4SGlt6dXwQznqWRmQsQLllTvdMmI/NLE4B4HS5ilf92v87r6G+rA /bItnQPOYAodc3LDSvmCM6OKWgaoNNMlyOtyOa0ttSKWZZaAnog/ymU1eA5poDW6iObN 7OfE5wEbszJLbi9f5QUEX/RkCohgkmP1KXaYCpZ+0OJr8NqmaFyTc2tCRBiNvf7RiG6i lPRg== X-Gm-Message-State: AOAM53015/GUjfj3IG+1OEqwHBHP2LOsHi/C7HcEz+yWWue/DVz83j8h Pf/LFAav10m3sgH/Ru2Kjx6WorBP7VM= X-Google-Smtp-Source: ABdhPJxXPOaQ6XWPJFAavthLUaJmwJp9stJcFgalakkphF35qRulV7wO1x2HoZsh1/bw9ua3oLLb9w== X-Received: by 2002:a37:68c1:: with SMTP id d184mr18196479qkc.395.1617491555694; Sat, 03 Apr 2021 16:12:35 -0700 (PDT) Received: from localhost.localdomain ([191.83.214.60]) by smtp.gmail.com with ESMTPSA id h75sm10094119qke.80.2021.04.03.16.12.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Apr 2021 16:12:35 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sat, 3 Apr 2021 20:12:00 -0300 Message-Id: <20210403231200.1165-1-jamrial@gmail.com> X-Mailer: git-send-email 2.31.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4] 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 --- Now using the avformat_ prefix as Anton requested. I forgot about it when i made v3. libavformat/avformat.h | 39 +++++++++++++++++++++++++++++++++++++++ libavformat/utils.c | 27 +++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 6a9b09160c..a1e87ef891 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -2767,6 +2767,45 @@ 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 avformat_index_get_entries_count(AVStream *st); + +/** + * Get the AVIndexEntry corresponding to the given index. + * + * @param st Stream containing the requested AVIndexEntry. + * @param idx The desired index. + * @return A pointer to the requested AVIndexEntry if it exists, NULL otherwise. + * + * @note The pointer returned by this function is only guaranteed to be valid + * until any function that could alter the stream or the AVFormatContext + * that cointains it is called. + */ +const AVIndexEntry *avformat_index_get_entry(AVStream *st, int idx); + +/** + * Get the AVIndexEntry corresponding to the given timestamp. + * + * @param st Stream containing the requested AVIndexEntry. + * @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 A pointer to the requested AVIndexEntry if it exists, NULL otherwise. + * + * @note The pointer returned by this function is only guaranteed to be valid + * until any function that could alter the stream or the AVFormatContext + * that cointains it is called. + */ +const AVIndexEntry *avformat_index_get_entry_from_timestamp(AVStream *st, + 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 e9bf31e38b..942d7c8390 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -2176,6 +2176,33 @@ int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags) wanted_timestamp, flags); } +int avformat_index_get_entries_count(AVStream *st) +{ + return st->internal->nb_index_entries; +} + +const AVIndexEntry *avformat_index_get_entry(AVStream *st, int idx) +{ + if (idx < 0 || idx >= st->internal->nb_index_entries) + return NULL; + + return &st->internal->index_entries[idx]; +} + +const AVIndexEntry *avformat_index_get_entry_from_timestamp(AVStream *st, + int64_t wanted_timestamp, + int flags) +{ + int idx = ff_index_search_timestamp(st->internal->index_entries, + st->internal->nb_index_entries, + wanted_timestamp, flags); + + if (idx < 0) + return NULL; + + return &st->internal->index_entries[idx]; +} + 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 )) {