[FFmpeg-devel] [PATCH v2 1/3] avcodec/hevc: Rewrite scalability_mask_flag parse in decode_vps_ext

Zhao Zhili quinkblack at foxmail.com
Sun Dec 15 08:38:51 EET 2024


From: Zhao Zhili <zhilizhao at tencent.com>

Remove a for loop and make it easy to extend to support other types
of scalability. Move ScalabilityMask to hevc header file so it can
be used in hevc decoder.
---
 libavcodec/hevc/hevc.h |  7 +++++++
 libavcodec/hevc/ps.c   | 33 +++++++++++++++------------------
 libavcodec/hevc/ps.h   |  2 ++
 3 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/libavcodec/hevc/hevc.h b/libavcodec/hevc/hevc.h
index 8bd59142db..b2229fda40 100644
--- a/libavcodec/hevc/hevc.h
+++ b/libavcodec/hevc/hevc.h
@@ -162,5 +162,12 @@ enum {
     HEVC_MAX_PALETTE_PREDICTOR_SIZE = 128,
 };
 
+enum HEVCScalabilityMask {
+    HEVC_SCALABILITY_DEPTH      = 1 << (15 - 0),
+    HEVC_SCALABILITY_MULTIVIEW  = 1 << (15 - 1),
+    HEVC_SCALABILITY_SPATIAL    = 1 << (15 - 2),
+    HEVC_SCALABILITY_AUXILIARY  = 1 << (15 - 3),
+    HEVC_SCALABILITY_MASK_MAX   = 0xFFFF,
+};
 
 #endif /* AVCODEC_HEVC_HEVC_H */
diff --git a/libavcodec/hevc/ps.c b/libavcodec/hevc/ps.c
index a1d352eec5..5b1bcd9b7c 100644
--- a/libavcodec/hevc/ps.c
+++ b/libavcodec/hevc/ps.c
@@ -450,14 +450,6 @@ static void hevc_vps_free(FFRefStructOpaque opaque, void *obj)
     av_freep(&vps->data);
 }
 
-enum ScalabilityMask {
-    HEVC_SCALABILITY_DEPTH      = 0,
-    HEVC_SCALABILITY_MULTIVIEW  = 1,
-    HEVC_SCALABILITY_SPATIAL    = 2,
-    HEVC_SCALABILITY_AUXILIARY  = 3,
-    HEVC_SCALABILITY_MASK_MAX   = 15,
-};
-
 enum DependencyType {
     HEVC_DEP_TYPE_SAMPLE = 0,
     HEVC_DEP_TYPE_MV     = 1,
@@ -532,17 +524,22 @@ static int decode_vps_ext(GetBitContext *gb, AVCodecContext *avctx, HEVCVPS *vps
         return AVERROR_INVALIDDATA;
 
     splitting_flag = get_bits1(gb);
-    num_scalability_types = 0;
-    for (int i = 0; i <= HEVC_SCALABILITY_MASK_MAX; i++) {
-        int scalability_mask_flag = get_bits1(gb);
-        if (scalability_mask_flag && (i != HEVC_SCALABILITY_MULTIVIEW)) {
-            av_log(avctx, AV_LOG_ERROR, "Scalability type %d not supported\n", i);
-            return AVERROR_PATCHWELCOME;
-        }
-        num_scalability_types += scalability_mask_flag;
-    }
-    if (num_scalability_types != 1)
+    vps->scalability_mask_flag = get_bits(gb, 16);
+    num_scalability_types = av_popcount(vps->scalability_mask_flag);
+    if (!num_scalability_types) {
+        av_log(avctx, AV_LOG_ERROR, "Missing scalability mask\n");
         return AVERROR_INVALIDDATA;
+    } else if (num_scalability_types > 1) {
+        av_log(avctx, AV_LOG_ERROR, "Scalability number %d not supported\n",
+               num_scalability_types);
+        return AVERROR_PATCHWELCOME;
+    }
+
+    if (!(vps->scalability_mask_flag & HEVC_SCALABILITY_MULTIVIEW)) {
+        av_log(avctx, AV_LOG_ERROR, "Scalability type %d not supported\n",
+               15 - ff_ctz(vps->scalability_mask_flag));
+        return AVERROR_PATCHWELCOME;
+    }
 
     if (!splitting_flag)
         dimension_id_len = get_bits(gb, 3) + 1;
diff --git a/libavcodec/hevc/ps.h b/libavcodec/hevc/ps.h
index 6f5b1f8755..d3465e3d27 100644
--- a/libavcodec/hevc/ps.h
+++ b/libavcodec/hevc/ps.h
@@ -205,6 +205,8 @@ typedef struct HEVCVPS {
      */
     int nb_layers;
 
+    uint16_t scalability_mask_flag;
+
     // LayerIdxInVps[nuh_layer_id], i.e. a mapping of nuh_layer_id to VPS layer
     // indices. Valid values are between 0 and HEVC_VPS_MAX_LAYERS. Entries for
     // unmapped values of nuh_layer_id are set to -1.
-- 
2.46.0



More information about the ffmpeg-devel mailing list