Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH v2 1/4] avcodec/hevc_parse: keep nal->nuh_layer_id > 0
       [not found] <20230106155230.487282-1-quinkblack@foxmail.com>
@ 2023-01-06 15:52 ` Zhao Zhili
  2023-01-06 15:52 ` [FFmpeg-devel] [PATCH v2 2/4] avcodec/cbs_h2645: " Zhao Zhili
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 4+ messages in thread
From: Zhao Zhili @ 2023-01-06 15:52 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Zhao Zhili

From: Zhao Zhili <zhilizhao@tencent.com>

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
---
 libavcodec/hevc_parse.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/libavcodec/hevc_parse.c b/libavcodec/hevc_parse.c
index 29dfd479f3..dddb293df6 100644
--- a/libavcodec/hevc_parse.c
+++ b/libavcodec/hevc_parse.c
@@ -37,8 +37,6 @@ static int hevc_decode_nal_units(const uint8_t *buf, int buf_size, HEVCParamSets
 
     for (i = 0; i < pkt.nb_nals; i++) {
         H2645NAL *nal = &pkt.nals[i];
-        if (nal->nuh_layer_id > 0)
-            continue;
 
         /* ignore everything except parameter sets and VCL NALUs */
         switch (nal->type) {
-- 
2.25.1


_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [FFmpeg-devel] [PATCH v2 2/4] avcodec/cbs_h2645: keep nal->nuh_layer_id > 0
       [not found] <20230106155230.487282-1-quinkblack@foxmail.com>
  2023-01-06 15:52 ` [FFmpeg-devel] [PATCH v2 1/4] avcodec/hevc_parse: keep nal->nuh_layer_id > 0 Zhao Zhili
@ 2023-01-06 15:52 ` Zhao Zhili
  2023-01-06 15:52 ` [FFmpeg-devel] [PATCH v2 3/4] avcodec/hevcdec: add nuh_layer_id option Zhao Zhili
  2023-01-06 15:52 ` [FFmpeg-devel] [PATCH v2 4/4] avcodec: add hevc_extract_layer bsf Zhao Zhili
  3 siblings, 0 replies; 4+ messages in thread
From: Zhao Zhili @ 2023-01-06 15:52 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Zhao Zhili

From: Zhao Zhili <zhilizhao@tencent.com>

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
---
 libavcodec/cbs_h2645.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index 4ee06003c3..b12abede29 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -477,9 +477,6 @@ static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
         AVBufferRef *ref;
         size_t size = nal->size;
 
-        if (nal->nuh_layer_id > 0)
-            continue;
-
         // Remove trailing zeroes.
         while (size > 0 && nal->data[size - 1] == 0)
             --size;
-- 
2.25.1


_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [FFmpeg-devel] [PATCH v2 3/4] avcodec/hevcdec: add nuh_layer_id option
       [not found] <20230106155230.487282-1-quinkblack@foxmail.com>
  2023-01-06 15:52 ` [FFmpeg-devel] [PATCH v2 1/4] avcodec/hevc_parse: keep nal->nuh_layer_id > 0 Zhao Zhili
  2023-01-06 15:52 ` [FFmpeg-devel] [PATCH v2 2/4] avcodec/cbs_h2645: " Zhao Zhili
@ 2023-01-06 15:52 ` Zhao Zhili
  2023-01-06 15:52 ` [FFmpeg-devel] [PATCH v2 4/4] avcodec: add hevc_extract_layer bsf Zhao Zhili
  3 siblings, 0 replies; 4+ messages in thread
From: Zhao Zhili @ 2023-01-06 15:52 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Zhao Zhili

From: Zhao Zhili <zhilizhao@tencent.com>

It can be used to decode selected independent non-base layer. One
use case is alpha layer decoding.

Partially fix #7965.

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
---
 libavcodec/hevcdec.c | 56 +++++++++++++++++++++++++++++++++++++++++++-
 libavcodec/version.h |  2 +-
 2 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index 567e8d81d4..0b3d4e84d4 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -2956,6 +2956,35 @@ static int hevc_frame_end(HEVCContext *s)
     return 0;
 }
 
+static int check_layer(HEVCContext *s)
+{
+    const HEVCVPS *vps;
+
+    if (!s->nuh_layer_id)
+        return 0;
+
+    for (int i = 0; i < FF_ARRAY_ELEMS(s->ps.vps_list); i++) {
+        if (!s->ps.vps_list[i])
+            continue;
+
+        vps = (const HEVCVPS *)s->ps.vps_list[i]->data;
+        if (vps->vps_max_layers == 1) {
+            av_log(s->avctx, AV_LOG_ERROR,
+                    "Specified nuh_layer_id is %d but vps_max_layers is 1.\n",
+                    s->nuh_layer_id);
+            // Keep going for now.
+        } else if (s->nuh_layer_id > vps->vps_max_layer_id) {
+            av_log(s->avctx, AV_LOG_ERROR,
+                    "Specified nuh_layer_id is %d but vps_max_layer_id is %d.\n",
+                    s->nuh_layer_id, vps->vps_max_layer_id);
+            // It's a known issue that Apple videotoolbox encoder doesn't set
+            // vps_max_layer_id correctly. Keep going on purpose.
+        }
+    }
+
+    return 0;
+}
+
 static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
 {
     HEVCLocalContext *lc = s->HEVClc;
@@ -2977,6 +3006,9 @@ static int decode_nal_unit(HEVCContext *s, const H2645NAL *nal)
                 goto fail;
         }
         ret = ff_hevc_decode_nal_vps(gb, s->avctx, &s->ps);
+        if (ret < 0)
+            goto fail;
+        ret = check_layer(s);
         if (ret < 0)
             goto fail;
         break;
@@ -3216,7 +3248,8 @@ static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
 
         if (s->avctx->skip_frame >= AVDISCARD_ALL ||
             (s->avctx->skip_frame >= AVDISCARD_NONREF
-            && ff_hevc_nal_is_nonref(nal->type)) || nal->nuh_layer_id > 0)
+            && ff_hevc_nal_is_nonref(nal->type)) ||
+            nal->nuh_layer_id != s->nuh_layer_id)
             continue;
 
         ret = decode_nal_unit(s, nal);
@@ -3315,6 +3348,10 @@ static int hevc_decode_extradata(HEVCContext *s, uint8_t *buf, int length, int f
     if (ret < 0)
         return ret;
 
+    ret = check_layer(s);
+    if (ret < 0)
+        return ret;
+
     /* export stream parameters from the first SPS */
     for (i = 0; i < FF_ARRAY_ELEMS(s->ps.sps_list); i++) {
         if (first && s->ps.sps_list[i]) {
@@ -3637,6 +3674,21 @@ static av_cold int hevc_decode_init(AVCodecContext *avctx)
     HEVCContext *s = avctx->priv_data;
     int ret;
 
+    if (s->nuh_layer_id > 0) {
+        if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
+            av_log(avctx, AV_LOG_ERROR,
+                    "Decoding selected nuh_layer_id is under development. "
+                    "Use -strict experimental to use it anyway.\n");
+            return AVERROR(EINVAL);
+        }
+
+        if (avctx->hwaccel) {
+            av_log(avctx, AV_LOG_ERROR,
+                    "Decoding selected nuh_layer_id doesn't work with hwaccel.\n");
+            return AVERROR(EINVAL);
+        }
+    }
+
     if (avctx->active_thread_type & FF_THREAD_SLICE) {
         s->threads_number = avctx->thread_count;
         ret = ff_slice_thread_init_progress(avctx);
@@ -3691,6 +3743,8 @@ static const AVOption options[] = {
         AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
     { "strict-displaywin", "stricly apply default display window size", OFFSET(apply_defdispwin),
         AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, PAR },
+    { "nuh_layer_id", "Select which nuh_layer_id to decode (only works with INBL)", OFFSET(nuh_layer_id),
+        AV_OPT_TYPE_INT,  {.i64 = 0}, 0, 62, PAR },
     { NULL },
 };
 
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 6b8a1dbb79..15f7c3fe3d 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 100
+#define LIBAVCODEC_VERSION_MICRO 101
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \
-- 
2.25.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [FFmpeg-devel] [PATCH v2 4/4] avcodec: add hevc_extract_layer bsf
       [not found] <20230106155230.487282-1-quinkblack@foxmail.com>
                   ` (2 preceding siblings ...)
  2023-01-06 15:52 ` [FFmpeg-devel] [PATCH v2 3/4] avcodec/hevcdec: add nuh_layer_id option Zhao Zhili
@ 2023-01-06 15:52 ` Zhao Zhili
  3 siblings, 0 replies; 4+ messages in thread
From: Zhao Zhili @ 2023-01-06 15:52 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Zhao Zhili

From: Zhao Zhili <zhilizhao@tencent.com>

For example, to extract alpha layer with nuh_layer_id equal
to 1:

./ffmpeg -i alpha.mp4 \
	-an -c:v copy \
	-bsf:v hevc_extract_layer=nuh_layer_id=1 \
	output.mp4

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
---
 configure                           |   1 +
 doc/bitstream_filters.texi          |  12 +++
 libavcodec/Makefile                 |   1 +
 libavcodec/bitstream_filters.c      |   1 +
 libavcodec/hevc_extract_layer_bsf.c | 126 ++++++++++++++++++++++++++++
 libavcodec/version.h                |   4 +-
 6 files changed, 143 insertions(+), 2 deletions(-)
 create mode 100644 libavcodec/hevc_extract_layer_bsf.c

diff --git a/configure b/configure
index 870d426b0e..a398b28790 100755
--- a/configure
+++ b/configure
@@ -3278,6 +3278,7 @@ filter_units_bsf_select="cbs"
 h264_metadata_bsf_deps="const_nan"
 h264_metadata_bsf_select="cbs_h264"
 h264_redundant_pps_bsf_select="cbs_h264"
+hevc_extract_layer_bsf_select="cbs_h265"
 hevc_metadata_bsf_select="cbs_h265"
 mjpeg2jpeg_bsf_select="jpegtables"
 mpeg2_metadata_bsf_select="cbs_mpeg2"
diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index c63c20370f..55ed8d91f9 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -382,6 +382,18 @@ This applies a specific fixup to some Blu-ray streams which contain
 redundant PPSs modifying irrelevant parameters of the stream which
 confuse other transformations which require correct extradata.
 
+@section hevc_extract_layer
+
+Extract NALUs with the specified nuh_layer_id and rewrite as base
+layer. Only works with INBL (independent non-base layer).
+
+@table @option
+
+@item nuh_layer_id
+Which layer to extract.
+
+@end table
+
 @section hevc_metadata
 
 Modify metadata embedded in an HEVC stream.
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3ab448dd49..f27d169741 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1208,6 +1208,7 @@ OBJS-$(CONFIG_H264_METADATA_BSF)          += h264_metadata_bsf.o h264_levels.o \
 OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF)       += h264_mp4toannexb_bsf.o
 OBJS-$(CONFIG_H264_REDUNDANT_PPS_BSF)     += h264_redundant_pps_bsf.o
 OBJS-$(CONFIG_HAPQA_EXTRACT_BSF)          += hapqa_extract_bsf.o hap.o
+OBJS-$(CONFIG_HEVC_EXTRACT_LAYER_BSF)     += hevc_extract_layer_bsf.o
 OBJS-$(CONFIG_HEVC_METADATA_BSF)          += h265_metadata_bsf.o h265_profile_level.o \
                                              h2645data.o
 OBJS-$(CONFIG_HEVC_MP4TOANNEXB_BSF)       += hevc_mp4toannexb_bsf.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index a3bebefe5f..ac013e8072 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -40,6 +40,7 @@ extern const FFBitStreamFilter ff_h264_metadata_bsf;
 extern const FFBitStreamFilter ff_h264_mp4toannexb_bsf;
 extern const FFBitStreamFilter ff_h264_redundant_pps_bsf;
 extern const FFBitStreamFilter ff_hapqa_extract_bsf;
+extern const FFBitStreamFilter ff_hevc_extract_layer_bsf;
 extern const FFBitStreamFilter ff_hevc_metadata_bsf;
 extern const FFBitStreamFilter ff_hevc_mp4toannexb_bsf;
 extern const FFBitStreamFilter ff_imx_dump_header_bsf;
diff --git a/libavcodec/hevc_extract_layer_bsf.c b/libavcodec/hevc_extract_layer_bsf.c
new file mode 100644
index 0000000000..84c1c87a80
--- /dev/null
+++ b/libavcodec/hevc_extract_layer_bsf.c
@@ -0,0 +1,126 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/common.h"
+#include "libavutil/opt.h"
+
+#include "bsf.h"
+#include "bsf_internal.h"
+#include "cbs.h"
+#include "cbs_bsf.h"
+#include "cbs_h265.h"
+#include "hevc.h"
+
+typedef struct HevcExtractLayerContext {
+    CBSBSFContext common;
+
+    int nuh_layer_id;
+} HevcExtractLayerContext;
+
+static int hevc_update_vps(AVBSFContext *bsf, H265RawVPS *vps)
+{
+    HevcExtractLayerContext *ctx = bsf->priv_data;
+
+    if (ctx->nuh_layer_id > 0) {
+        if (vps->vps_max_layers_minus1 == 0)
+            av_log(bsf, AV_LOG_ERROR,
+                    "vps_max_layers_minus1 is zero, only base layer is available.\n");
+        else if (ctx->nuh_layer_id > vps->vps_max_layer_id)
+            // It's a known issue that Apple videotoolbox encoder doesn't set
+            // vps_max_layer_id correctly. So this can be a false positive.
+            av_log(bsf, AV_LOG_ERROR, "Specified nuh_layer_id %d is larger than vps_max_layer_id %d, "
+                    "this might leading to empty output\n",
+                    ctx->nuh_layer_id, vps->vps_max_layer_id);
+    }
+
+    vps->vps_max_layers_minus1 = 0;
+    vps->vps_max_layer_id = 0;
+    // TODO: update vps_extension to reflect the layer changes, other than
+    // drop it entirely.
+    vps->vps_extension_flag = 0;
+
+    return 0;
+}
+
+static int hevc_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
+                                CodedBitstreamFragment *au)
+{
+    HevcExtractLayerContext *ctx = bsf->priv_data;
+    H265RawNALUnitHeader *header;
+    int ret;
+
+    for (int i = 0; i < au->nb_units; i++) {
+        if (au->units[i].type == HEVC_NAL_VPS) {
+            ret = hevc_update_vps(bsf, au->units[i].content);
+            if (ret)
+                return ret;
+            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;
+    }
+
+    return 0;
+}
+
+static const CBSBSFType hevc_extract_layer_type = {
+    .codec_id        = AV_CODEC_ID_HEVC,
+    .fragment_name   = "access unit",
+    .unit_name       = "NAL unit",
+    .update_fragment = &hevc_update_fragment,
+};
+
+static int hevc_extract_layer_init(AVBSFContext *bsf)
+{
+    return ff_cbs_bsf_generic_init(bsf, &hevc_extract_layer_type);
+}
+
+#define OFFSET(x) offsetof(HevcExtractLayerContext, x)
+#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
+static const AVOption hevc_extract_layer_options[] = {
+    { "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 = 0 }, 0, 62, FLAGS },
+    { NULL }
+};
+
+static const AVClass hevc_extract_layer_class = {
+    .class_name = "hevc_extract_layer_bsf",
+    .item_name  = av_default_item_name,
+    .option     = hevc_extract_layer_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+static const enum AVCodecID hevc_extract_layer_codec_ids[] = {
+    AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE,
+};
+
+const FFBitStreamFilter ff_hevc_extract_layer_bsf = {
+    .p.name         = "hevc_extract_layer",
+    .p.codec_ids    = hevc_extract_layer_codec_ids,
+    .p.priv_class   = &hevc_extract_layer_class,
+    .priv_data_size = sizeof(HevcExtractLayerContext),
+    .init           = &hevc_extract_layer_init,
+    .close          = &ff_cbs_bsf_generic_close,
+    .filter         = &ff_cbs_bsf_generic_filter,
+};
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 15f7c3fe3d..dfd3d5d7e5 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,8 +29,8 @@
 
 #include "version_major.h"
 
-#define LIBAVCODEC_VERSION_MINOR  56
-#define LIBAVCODEC_VERSION_MICRO 101
+#define LIBAVCODEC_VERSION_MINOR  57
+#define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \
-- 
2.25.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-01-06  7:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20230106155230.487282-1-quinkblack@foxmail.com>
2023-01-06 15:52 ` [FFmpeg-devel] [PATCH v2 1/4] avcodec/hevc_parse: keep nal->nuh_layer_id > 0 Zhao Zhili
2023-01-06 15:52 ` [FFmpeg-devel] [PATCH v2 2/4] avcodec/cbs_h2645: " Zhao Zhili
2023-01-06 15:52 ` [FFmpeg-devel] [PATCH v2 3/4] avcodec/hevcdec: add nuh_layer_id option Zhao Zhili
2023-01-06 15:52 ` [FFmpeg-devel] [PATCH v2 4/4] avcodec: add hevc_extract_layer bsf Zhao Zhili

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git