[FFmpeg-devel] [RFC PATCH 4/4] avcodec/h265_metadata_bsf: add nuh_layer_id option

Zhao Zhili quinkblack at foxmail.com
Thu Jan 5 18:14:16 EET 2023


From: Zhao Zhili <zhilizhao at tencent.com>

Extract NALUs with the specified nuh_layer_id and rewrite as base
layer. For example, to extract alpha layer with nuh_layer_id equal
to 1:

./ffmpeg -i alpha.mp4 \
	-an -c:v copy \
	-bsf:v hevc_mp4toannexb,hevc_metadata=nuh_layer_id=1 \
	alpha.hevc
---
 doc/bitstream_filters.texi     |  5 +++++
 libavcodec/h265_metadata_bsf.c | 25 +++++++++++++++++++++++++
 libavcodec/version.h           |  2 +-
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index c63c20370f..8cb5bee644 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -443,6 +443,11 @@ The argument must be the name of a level (for example, @samp{5.1}), a
 or the special name @samp{auto} indicating that the filter should
 attempt to guess the level from the input stream properties.
 
+ at item nuh_layer_id
+
+Extract NALUs with the specified nuh_layer_id and rewrite as base layer.
+All other NALUs are dropped except VPS.
+
 @end table
 
 @section hevc_mp4toannexb
diff --git a/libavcodec/h265_metadata_bsf.c b/libavcodec/h265_metadata_bsf.c
index 6787bd14a1..fdaab27186 100644
--- a/libavcodec/h265_metadata_bsf.c
+++ b/libavcodec/h265_metadata_bsf.c
@@ -62,6 +62,8 @@ typedef struct H265MetadataContext {
     int level;
     int level_guess;
     int level_warned;
+
+    int nuh_layer_id;
 } H265MetadataContext;
 
 
@@ -328,6 +330,25 @@ static int h265_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
     H265MetadataContext *ctx = bsf->priv_data;
     int err, i;
 
+    if (ctx->nuh_layer_id >= 0) {
+        H265RawNALUnitHeader *header;
+        for (i = 0; i < au->nb_units; i++) {
+            if (au->units[i].type == HEVC_NAL_VPS) {
+                H265RawVPS *vps = au->units[i].content;
+                vps->vps_extension_flag = 0;
+                continue;
+            }
+
+            header = au->units[i].content;
+            if (header->nuh_layer_id != ctx->nuh_layer_id) {
+                ff_cbs_delete_unit(au, i);
+                i--;
+                continue;
+            }
+            header->nuh_layer_id = 0;
+        }
+    }
+
     // If an AUD is present, it must be the first NAL unit.
     if (au->nb_units && au->units[0].type == HEVC_NAL_AUD) {
         if (ctx->aud == BSF_ELEMENT_REMOVE)
@@ -478,6 +499,10 @@ static const AVOption h265_metadata_options[] = {
     { LEVEL("8.5", 255) },
 #undef LEVEL
 
+    { "nuh_layer_id", "Extract NALUs with the specified nuh_layer_id and rewrite as base layer",
+        OFFSET(nuh_layer_id), AV_OPT_TYPE_INT,
+        { .i64 = -1 }, -1, 62, FLAGS },
+
     { NULL }
 };
 
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 15f7c3fe3d..734a4fe097 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -30,7 +30,7 @@
 #include "version_major.h"
 
 #define LIBAVCODEC_VERSION_MINOR  56
-#define LIBAVCODEC_VERSION_MICRO 101
+#define LIBAVCODEC_VERSION_MICRO 102
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \
-- 
2.25.1



More information about the ffmpeg-devel mailing list