[FFmpeg-devel] [PATCH v2] avformat/utils: add helper functions to retrieve index entries from an AVStream

James Almer jamrial at gmail.com
Tue Mar 23 22:07:13 EET 2021


Signed-off-by: James Almer <jamrial at gmail.com>
---
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 ))
 {
-- 
2.31.0



More information about the ffmpeg-devel mailing list