From patchwork Tue Mar 23 18:36:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 26573 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 8967A449BDA for ; Tue, 23 Mar 2021 20:37:10 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 62A5068AA2F; Tue, 23 Mar 2021 20:37:10 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f169.google.com (mail-qk1-f169.google.com [209.85.222.169]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9EB2B68A3AF for ; Tue, 23 Mar 2021 20:37:04 +0200 (EET) Received: by mail-qk1-f169.google.com with SMTP id 7so15412629qka.7 for ; Tue, 23 Mar 2021 11:37:04 -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=VcQYHu38knA4gVQyghdMTdrrAs+KzlUaGBC8hKj8VtE=; b=nuMclu3eyKLQwkzobMjCZsHuePmEsUVSglcdUd3j2DCBxEO+BH3zrDBn6S+KqDnHLt kFGAubpXTjKOf925YSQC8qjWWkWFTSpF139XWfRLvLGEmbU+DGjj1F2cgPhYNzGKQj5H qYwZN5TfXkLBJXfo+rMAWb97ij1RgpFUgVS42t0KqAA2rofHVaFDCup+Gv9pXWMmvbvH VAGT6U63l8BPV1TySEeofDozwcrYravifJcT18SuVBJDHEEXwsWf6AwUhBYDiR01RdKy XnsQEmoaPWVWqEAnxzOBmJTtCwRM1r71LG88Xdho1fAexVWbDFi2rohKUYzHOkyTEbDb dg7Q== 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=VcQYHu38knA4gVQyghdMTdrrAs+KzlUaGBC8hKj8VtE=; b=YkqUNPBfbzPIKqx2vpoTzJ+IyHetbFjs7r/AZYWPwSqCCEVVh02c+oR5S5ppowiXGC Xd3UfsCd1r5eY+ZyBjR6syVlIV9o01Z78+0ESSRnUJCfjkWXVTec9b/ojexbHZcCZPvb hI+fepX/YJshcs7oVomdzDA+gYbEhL2H5YIzuto98fb0Dal7MEcmT0LgaFU5UiPd8GzT BHrovbt8STImnUINgA3wUYC6U5lsq4yeqGRx1pfowqZbjEegMxVbtcXdzb+aiX6KYFac CBOed+od6rAXdmnhuBSbGmGEpP2wQQyeLGuCDYrntoyJMk86pgAUjY3avnixlbPrutIh tCYA== X-Gm-Message-State: AOAM5332XtX6lboVBXZQgNpjSj8Fr4GXHVhGVFE/yAN4IoyPvO9FENsn ZHK2Z5fMZ62Zyaw8ntzRYZBoVnudU+g= X-Google-Smtp-Source: ABdhPJyvDtTKQKIWASETWZe6hKYM1M5+CnaCHEf5vXF7mkobwJNDQjHs5mEI8F7FPkriLudOq7BLSw== X-Received: by 2002:a37:30f:: with SMTP id 15mr6928760qkd.494.1616524622886; Tue, 23 Mar 2021 11:37:02 -0700 (PDT) Received: from localhost.localdomain ([181.23.86.39]) by smtp.gmail.com with ESMTPSA id m17sm14259722qkh.82.2021.03.23.11.37.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Mar 2021 11:37:02 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Tue, 23 Mar 2021 15:36:22 -0300 Message-Id: <20210323183622.2219-1-jamrial@gmail.com> X-Mailer: git-send-email 2.31.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] 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 --- Following "lavf: move AVStream.*index_entries* to AVStreamInternal", it was revealed that some library users apparently found these fields useful and were accessing them despite not being public. This patch introduces a few functions to recover this functionality in a proper and supported way. We can't simply return a const pointer to the corresponding AVIndexEntry in the array because it may change at any time by for example calling av_add_index_entry(), so it's either a copy of the AVIndexEntry, or adding half a dozen output parameters to the function signature for each field in AVIndexEntry. libavformat/avformat.h | 39 +++++++++++++++++++++++++++++++++++++++ libavformat/utils.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 765bc3b6f5..ac8b57149e 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -2754,6 +2754,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 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 an AVIndexEntry where to store the entry. On failure, + * or if the index entry could not be found, this struct 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, int idx); + +/** + * Get the AVIndexEntry corresponding to the given timestamp. + * + * @param st stream containing the requested AVIndexEntry + * @param index_entry pointer to an AVIndexEntry where to store the entry. On failure, + * or if the index entry could not be found, this struct 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..4e7a8bf23e 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -2129,6 +2129,38 @@ 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, int idx) +{ + if (idx < 0) + return AVERROR(EINVAL); + if (idx >= st->internal->nb_index_entries) + return AVERROR(ENOENT); + + memcpy(index_entry, &st->internal->index_entries[idx], sizeof(*index_entry)); + + return 0; +} + +int av_index_get_entry_from_timestamp(AVStream *st, AVIndexEntry *index_entry, + 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 AVERROR(ENOENT); + + memcpy(index_entry, &st->internal->index_entries[idx], sizeof(*index_entry)); + + 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 )) {