[FFmpeg-devel] [PATCH v3 1/5] avformat: add av_program_add_stream_index2()

Gyan Doshi ffmpeg at gyani.pro
Fri Feb 17 19:02:45 EET 2023


av_program_add_stream_index() added in 526efa10534
may fail to carry out its purpose but the lack of
a return value stops callers from catching any error.

Fixed in new function.
---
As suggested by Andreas in
https://ffmpeg.org/pipermail/ffmpeg-devel/2023-February/306645.html

 doc/APIchanges         |  3 +++
 libavformat/avformat.c | 21 +++++++++++++++------
 libavformat/avformat.h | 13 +++++++++++++
 libavformat/version.h  |  2 +-
 4 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 29161e30bf..889f9925b1 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2023-02-09
 
 API changes, most recent first:
 
+2023-02-xx - xxxxxxxxxx - lavf 60.3.100 - avformat.h
+  Add av_program_add_stream_index2().
+
 2023-02-16 - xxxxxxxxxx - lavf 60.2.100 - avformat.h
   Deprecate AVFormatContext io_close callback.
   The superior io_close2 callback should be used instead.
diff --git a/libavformat/avformat.c b/libavformat/avformat.c
index 708d90b38c..0f203e4f86 100644
--- a/libavformat/avformat.c
+++ b/libavformat/avformat.c
@@ -333,14 +333,14 @@ AVProgram *av_new_program(AVFormatContext *ac, int id)
     return program;
 }
 
-void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
+int av_program_add_stream_index2(AVFormatContext *ac, int progid, unsigned idx)
 {
     AVProgram *program = NULL;
     void *tmp;
 
     if (idx >= ac->nb_streams) {
-        av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
-        return;
+        av_log(ac, AV_LOG_ERROR, "stream index %d is greater than stream count %d\n", idx, ac->nb_streams);
+        return AVERROR(EINVAL);
     }
 
     for (unsigned i = 0; i < ac->nb_programs; i++) {
@@ -349,15 +349,24 @@ void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
         program = ac->programs[i];
         for (unsigned j = 0; j < program->nb_stream_indexes; j++)
             if (program->stream_index[j] == idx)
-                return;
+                return 0;
 
         tmp = av_realloc_array(program->stream_index, program->nb_stream_indexes+1, sizeof(unsigned int));
         if (!tmp)
-            return;
+            return AVERROR(ENOMEM);
         program->stream_index = tmp;
         program->stream_index[program->nb_stream_indexes++] = idx;
-        return;
+        return 0;
     }
+
+    av_log(ac, AV_LOG_ERROR, "No program with id %d found\n", progid);
+    return AVERROR(EINVAL);
+}
+
+void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
+{
+    av_program_add_stream_index2(ac, progid, idx);
+    return;
 }
 
 AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 1916aa2dc5..33ab0bc8ba 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -2046,6 +2046,19 @@ AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int
 
 void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx);
 
+/**
+ * Add the supplied index of a stream to the AVProgram with matching id.
+ *
+ * @param ac      the format context which contains the target AVProgram
+ * @param progid  the ID of the AVProgram whose stream index is to be updated
+ * @param idx     the index of the stream to be added
+ *
+ * @return >=0 upon successful addition or if index was already present,
+ *         AVERROR if no matching program is found or stream index is invalid or
+ *         the stream index array reallocation failed.
+ */
+int av_program_add_stream_index2(AVFormatContext *ac, int progid, unsigned int idx);
+
 /**
  * Find the "best" stream in the file.
  * The best stream is determined according to various heuristics as the most
diff --git a/libavformat/version.h b/libavformat/version.h
index 904e7f06aa..7ff1483912 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -31,7 +31,7 @@
 
 #include "version_major.h"
 
-#define LIBAVFORMAT_VERSION_MINOR   2
+#define LIBAVFORMAT_VERSION_MINOR   3
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.39.1



More information about the ffmpeg-devel mailing list