PR #22473 opened by Gyan Doshi (GyanD)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22473
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22473.patch

New lavf API to clone programs. Useful for nested muxing scenarios.


>From 07c8671323951f465e7145b580cecc74099a80b1 Mon Sep 17 00:00:00 2001
From: Gyan Doshi <[email protected]>
Date: Fri, 17 Feb 2023 21:08:15 +0530
Subject: [PATCH 1/5] avformat: add av_program_add_stream_index2()

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.
---
 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 c59b82f107..a5d367ed70 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2025-03-28
 
 API changes, most recent first:
 
+2026-03-xx - xxxxxxxxxxx - lavf 62.14.100 - avformat.h
+  Add av_program_add_stream_index2().
+
 2026-03-07 - c23d56b173a - lavc 62.26.100 - codec_desc.h
   Add AV_CODEC_PROP_ENHANCEMENT.
 
diff --git a/libavformat/avformat.c b/libavformat/avformat.c
index 8a98f2e53e..acab9293e3 100644
--- a/libavformat/avformat.c
+++ b/libavformat/avformat.c
@@ -299,14 +299,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++) {
@@ -315,15 +315,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 ee0ddddf8d..33289e3b89 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -2230,6 +2230,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 e8fad25023..14bf700274 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -31,7 +31,7 @@
 
 #include "version_major.h"
 
-#define LIBAVFORMAT_VERSION_MINOR  13
+#define LIBAVFORMAT_VERSION_MINOR  14
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.52.0


>From 9697c7622cb919f2d8ee9873805a2bc143fb3585 Mon Sep 17 00:00:00 2001
From: Gyan Doshi <[email protected]>
Date: Tue, 7 Feb 2023 18:45:30 +0530
Subject: [PATCH 2/5] avformat: add av_program_copy()

Helper to transfer programs from one muxing context to another.
---
 doc/APIchanges         |  3 ++
 libavformat/avformat.c | 66 ++++++++++++++++++++++++++++++++++++++++++
 libavformat/avformat.h | 22 ++++++++++++++
 libavformat/version.h  |  2 +-
 4 files changed, 92 insertions(+), 1 deletion(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index a5d367ed70..70c0ab2c64 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2025-03-28
 
 API changes, most recent first:
 
+2026-03-xx - xxxxfxxxxxx - lavf 62.15.100 - avformat.h
+  Add av_program_copy().
+
 2026-03-xx - xxxxxxxxxxx - lavf 62.14.100 - avformat.h
   Add av_program_add_stream_index2().
 
diff --git a/libavformat/avformat.c b/libavformat/avformat.c
index acab9293e3..a5d890623c 100644
--- a/libavformat/avformat.c
+++ b/libavformat/avformat.c
@@ -335,6 +335,72 @@ void av_program_add_stream_index(AVFormatContext *ac, int 
progid, unsigned idx)
     return;
 }
 
+int av_program_copy(AVFormatContext *dst, const AVFormatContext *src, int 
progid, int overwrite)
+{
+    const AVProgram *src_prog = NULL;
+    AVProgram *dst_prog = NULL;
+    int ret, idx = -1;
+
+    for (unsigned i = 0; i < src->nb_programs; i++)
+        if (src->programs[i]->id == progid)
+            src_prog = src->programs[i];
+
+    if (!src_prog) {
+        av_log(dst, AV_LOG_ERROR, "source program not found: id=0x%04x\n", 
progid);
+        return AVERROR(EINVAL);
+    }
+
+    for (unsigned i = 0; i < dst->nb_programs; i++) {
+        if (dst->programs[i]->id == progid)
+            idx = i;
+    }
+
+    if (idx >= 0 && !overwrite) {
+        av_log(dst, AV_LOG_WARNING, "target muxer already has program with 
id=0x%04x; not overwriting\n", progid);
+        return 0;
+    }
+
+    av_log(dst, AV_LOG_TRACE, "%s program: id=0x%04x\n", idx >= 0 ? 
"overwriting" : "copying", progid);
+
+    if (idx >= 0) {
+        dst_prog = dst->programs[idx];
+        av_dict_free(&dst_prog->metadata);
+        av_freep(&dst_prog->stream_index);
+        dst_prog->nb_stream_indexes = 0;
+    } else {
+        dst_prog = av_mallocz(sizeof(*dst_prog));
+        if (!dst_prog)
+            return AVERROR(ENOMEM);
+        ret = av_dynarray_add_nofree(&dst->programs, &dst->nb_programs, 
dst_prog);
+        if (ret < 0) {
+            av_free(dst_prog);
+            return ret;
+        }
+    }
+
+    /* public fields */
+    dst_prog->id          = src_prog->id;
+    dst_prog->flags       = src_prog->flags;
+    dst_prog->discard     = src_prog->discard;
+    dst_prog->program_num = src_prog->program_num;
+    dst_prog->pmt_pid     = src_prog->pmt_pid;
+    dst_prog->pcr_pid     = src_prog->pcr_pid;
+    dst_prog->pmt_version = src_prog->pmt_version;
+
+    for (unsigned i = 0; i < dst->nb_streams; i++) {
+        for (unsigned j = 0; j < src_prog->nb_stream_indexes; j++)
+            if (dst->streams[i]->id == 
src->streams[src_prog->stream_index[j]]->id) {
+                if (ret = av_program_add_stream_index2(dst, dst_prog->id, i) < 
0)
+                    return ret;
+            }
+    }
+
+    if (ret = av_dict_copy(&dst_prog->metadata, src_prog->metadata, 0) < 0)
+        return ret;
+
+    return 0;
+}
+
 AVProgram *av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, 
int s)
 {
     for (unsigned i = 0; i < ic->nb_programs; i++) {
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 33289e3b89..1abbc96246 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -2069,6 +2069,28 @@ int avformat_stream_group_add_stream(AVStreamGroup *stg, 
AVStream *st);
 
 AVProgram *av_new_program(AVFormatContext *s, int id);
 
+/**
+ * Copy an AVProgram from one AVFormatContext to another.
+ *
+ * Streams in the destination context whose IDs match the IDs of the streams 
in the
+ * source AVProgram index are added to the stream index of the copied 
AVProgram.
+ *
+ * If a new program has to be added, the function expects and requires any 
existing buffer
+ * holding the array of pointers to AVPrograms in the destination context to 
have its size
+ * be a power-of-two value. This should be the case if all earlier programs 
were created
+ * using av_new_program or this function. 
+ *
+ * @param dst           pointer to the target muxer context
+ * @param src           pointer to the source muxer context
+ * @param progid        ID of the program to be copied
+ * @param overwrite     whether to overwrite if target muxer already
+ *                      contains a program with the same ID
+ *
+ * @return  >= 0 in case of success, a negative AVERROR code in case of
+ *          failure
+ */
+int av_program_copy(AVFormatContext *dst, const AVFormatContext *src, int 
progid, int overwrite);
+
 /**
  * @}
  */
diff --git a/libavformat/version.h b/libavformat/version.h
index 14bf700274..eaf74946de 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -31,7 +31,7 @@
 
 #include "version_major.h"
 
-#define LIBAVFORMAT_VERSION_MINOR  14
+#define LIBAVFORMAT_VERSION_MINOR  15
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.52.0


>From 8f638261c8aea49edbf74e95f447eb1e5e6e691f Mon Sep 17 00:00:00 2001
From: Gyan Doshi <[email protected]>
Date: Tue, 7 Feb 2023 20:07:18 +0530
Subject: [PATCH 3/5] avformat/hls: relay programs to child muxers

---
 libavformat/hlsenc.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 8eaab37a2c..614438cda2 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -830,6 +830,14 @@ static int hls_mux_init(AVFormatContext *s, VariantStream 
*vs)
         st->id = vs->streams[i]->id;
     }
 
+    for (i = 0; i < s->nb_programs; i++) {
+        ret = av_program_copy(oc, (const AVFormatContext *)s, 
s->programs[i]->id, 0);
+        if (ret < 0) {
+            av_log(s, AV_LOG_ERROR, "unable to transfer program %d to child 
muxer\n", s->programs[i]->id);
+            return ret;
+        }
+    }
+
     vs->start_pos = 0;
     vs->new_start = 1;
 
-- 
2.52.0


>From bd9831bda0c3e08550fc99ae603f5752294c93f1 Mon Sep 17 00:00:00 2001
From: Gyan Doshi <[email protected]>
Date: Tue, 7 Feb 2023 20:18:07 +0530
Subject: [PATCH 4/5] avformat/segment: relay programs to child muxers

---
 libavformat/segment.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/libavformat/segment.c b/libavformat/segment.c
index 2c7ba0e776..346c55814e 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -183,6 +183,14 @@ static int segment_mux_init(AVFormatContext *s)
         }
     }
 
+    for (i = 0; i < s->nb_programs; i++) {
+        ret = av_program_copy(oc, (const AVFormatContext *)s, 
s->programs[i]->id, 0);
+        if (ret < 0) {
+            av_log(s, AV_LOG_ERROR, "unable to transfer program %d to child 
muxer\n", s->programs[i]->id);
+            return ret;
+        }
+    }
+
     return 0;
 }
 
-- 
2.52.0


>From 49e80ac4e98fd428ee68cce23288408ddc6cca4c Mon Sep 17 00:00:00 2001
From: Gyan Doshi <[email protected]>
Date: Tue, 7 Feb 2023 20:22:24 +0530
Subject: [PATCH 5/5] avformat/tee: relay programs to child muxers

---
 libavformat/tee.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/libavformat/tee.c b/libavformat/tee.c
index 3a2ee92c3c..e26add4da8 100644
--- a/libavformat/tee.c
+++ b/libavformat/tee.c
@@ -294,6 +294,14 @@ static int open_slave(AVFormatContext *avf, char *slave, 
TeeSlave *tee_slave)
         }
     }
 
+    for (i = 0; i < avf->nb_programs; i++) {
+        ret = av_program_copy(avf2, (const AVFormatContext *)avf, 
avf->programs[i]->id, 0);
+        if (ret < 0) {
+            av_log(avf, AV_LOG_ERROR, "unable to transfer program %d to child 
muxer\n", avf->programs[i]->id);
+            return ret;
+        }
+    }
+
     ret = ff_format_output_open(avf2, filename, &options);
     if (ret < 0) {
         av_log(avf, AV_LOG_ERROR, "Slave '%s': error opening: %s\n", slave,
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to