[FFmpeg-devel] [PATCH] Add advance vps/sps/pps id to hevc_metadata bsf

Eran Gonen eran.gonen at cloudinary.com
Wed Jan 8 15:10:19 EET 2020


---
 libavcodec/h265_metadata_bsf.c | 117 +++++++++++++++++++++++++++++++--
 1 file changed, 111 insertions(+), 6 deletions(-)

diff --git a/libavcodec/h265_metadata_bsf.c b/libavcodec/h265_metadata_bsf.c
index b3a1fda144..796046ac7f 100644
--- a/libavcodec/h265_metadata_bsf.c
+++ b/libavcodec/h265_metadata_bsf.c
@@ -68,6 +68,10 @@ typedef struct H265MetadataContext {
     int level;
     int level_guess;
     int level_warned;
+
+    int advance_vps_id;
+    int advance_sps_id;
+    int advance_pps_id;
 } H265MetadataContext;
 
 
@@ -168,6 +172,10 @@ static int h265_metadata_update_vps(AVBSFContext *bsf,
 {
     H265MetadataContext *ctx = bsf->priv_data;
 
+    if(ctx->advance_vps_id > -1) {
+        vps->vps_video_parameter_set_id = (vps->vps_video_parameter_set_id + ctx->advance_vps_id) % HEVC_MAX_VPS_COUNT;
+    }
+
     if (ctx->tick_rate.num && ctx->tick_rate.den) {
         int num, den;
 
@@ -200,6 +208,13 @@ static int h265_metadata_update_sps(AVBSFContext *bsf,
     int need_vui = 0;
     int crop_unit_x, crop_unit_y;
 
+    if(ctx->advance_vps_id > -1) {
+        sps->sps_video_parameter_set_id = (sps->sps_video_parameter_set_id + ctx->advance_vps_id) % HEVC_MAX_VPS_COUNT;
+    }
+    if(ctx->advance_sps_id > -1) {
+        sps->sps_seq_parameter_set_id = (sps->sps_seq_parameter_set_id + ctx->advance_sps_id) % HEVC_MAX_SPS_COUNT;
+    }
+
     if (ctx->sample_aspect_ratio.num && ctx->sample_aspect_ratio.den) {
         // Table E-1.
         static const AVRational sar_idc[] = {
@@ -336,6 +351,32 @@ static int h265_metadata_update_sps(AVBSFContext *bsf,
     return 0;
 }
 
+static int h265_metadata_update_pps(AVBSFContext *bsf,
+                                    H265RawPPS *pps)
+{
+    H265MetadataContext *ctx = bsf->priv_data;
+
+    if(ctx->advance_sps_id > -1) {
+        pps->pps_seq_parameter_set_id = (pps->pps_seq_parameter_set_id + ctx->advance_sps_id) % HEVC_MAX_SPS_COUNT;
+    }
+    if(ctx->advance_pps_id > -1) {
+        pps->pps_pic_parameter_set_id = (pps->pps_pic_parameter_set_id + ctx->advance_pps_id) % HEVC_MAX_PPS_COUNT;
+    }
+
+    return 0;
+}
+
+static int h265_metadata_update_slice_header(AVBSFContext *bsf,
+        H265RawSliceHeader *slice)
+{
+    H265MetadataContext *ctx = bsf->priv_data;
+
+    if(ctx->advance_pps_id > -1) {
+        slice->slice_pic_parameter_set_id = (slice->slice_pic_parameter_set_id + ctx->advance_pps_id) % HEVC_MAX_PPS_COUNT;
+    }
+    return 0;
+}
+
 static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
 {
     H265MetadataContext *ctx = bsf->priv_data;
@@ -406,15 +447,42 @@ static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
         h265_metadata_guess_level(bsf, au);
 
     for (i = 0; i < au->nb_units; i++) {
-        if (au->units[i].type == HEVC_NAL_VPS) {
+        switch (au->units[i].type) {
+        case HEVC_NAL_VPS:
             err = h265_metadata_update_vps(bsf, au->units[i].content);
             if (err < 0)
                 goto fail;
-        }
-        if (au->units[i].type == HEVC_NAL_SPS) {
+            break;
+        case HEVC_NAL_SPS:
             err = h265_metadata_update_sps(bsf, au->units[i].content);
             if (err < 0)
                 goto fail;
+            break;
+        case HEVC_NAL_PPS:
+            err = h265_metadata_update_pps(bsf, au->units[i].content);
+            if (err < 0)
+                goto fail;
+            break;
+        case HEVC_NAL_TRAIL_N:
+        case HEVC_NAL_TRAIL_R:
+        case HEVC_NAL_TSA_N:
+        case HEVC_NAL_TSA_R:
+        case HEVC_NAL_STSA_N:
+        case HEVC_NAL_STSA_R:
+        case HEVC_NAL_BLA_W_LP:
+        case HEVC_NAL_BLA_W_RADL:
+        case HEVC_NAL_BLA_N_LP:
+        case HEVC_NAL_IDR_W_RADL:
+        case HEVC_NAL_IDR_N_LP:
+        case HEVC_NAL_CRA_NUT:
+        case HEVC_NAL_RADL_N:
+        case HEVC_NAL_RADL_R:
+        case HEVC_NAL_RASL_N:
+        case HEVC_NAL_RASL_R:
+            err = h265_metadata_update_slice_header(bsf, au->units[i].content);
+            if (err < 0)
+                goto fail;
+            break;
         }
     }
 
@@ -455,15 +523,42 @@ static int h265_metadata_init(AVBSFContext *bsf)
             h265_metadata_guess_level(bsf, au);
 
         for (i = 0; i < au->nb_units; i++) {
-            if (au->units[i].type == HEVC_NAL_VPS) {
+            switch (au->units[i].type) {
+            case HEVC_NAL_VPS:
                 err = h265_metadata_update_vps(bsf, au->units[i].content);
                 if (err < 0)
                     goto fail;
-            }
-            if (au->units[i].type == HEVC_NAL_SPS) {
+                break;
+            case HEVC_NAL_SPS:
                 err = h265_metadata_update_sps(bsf, au->units[i].content);
                 if (err < 0)
                     goto fail;
+                break;
+            case HEVC_NAL_PPS:
+                err = h265_metadata_update_pps(bsf, au->units[i].content);
+                if (err < 0)
+                    goto fail;
+                break;
+            case HEVC_NAL_TRAIL_N:
+            case HEVC_NAL_TRAIL_R:
+            case HEVC_NAL_TSA_N:
+            case HEVC_NAL_TSA_R:
+            case HEVC_NAL_STSA_N:
+            case HEVC_NAL_STSA_R:
+            case HEVC_NAL_BLA_W_LP:
+            case HEVC_NAL_BLA_W_RADL:
+            case HEVC_NAL_BLA_N_LP:
+            case HEVC_NAL_IDR_W_RADL:
+            case HEVC_NAL_IDR_N_LP:
+            case HEVC_NAL_CRA_NUT:
+            case HEVC_NAL_RADL_N:
+            case HEVC_NAL_RADL_R:
+            case HEVC_NAL_RASL_N:
+            case HEVC_NAL_RASL_R:
+                err = h265_metadata_update_slice_header(bsf, au->units[i].content);
+                if (err < 0)
+                    goto fail;
+                break;
             }
         }
 
@@ -547,6 +642,16 @@ static const AVOption h265_metadata_options[] = {
         OFFSET(crop_bottom), AV_OPT_TYPE_INT,
         { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
 
+    { "advance_vps_id", "Advance the vps id",
+        OFFSET(advance_vps_id), AV_OPT_TYPE_INT,
+        { .i64 = -1 }, -1, HEVC_MAX_VPS_COUNT, FLAGS },
+    {  "advance_sps_id", "Advance the sps id",
+        OFFSET(advance_sps_id), AV_OPT_TYPE_INT,
+        { .i64 = -1 }, -1, HEVC_MAX_SPS_COUNT, FLAGS },
+    {  "advance_pps_id", "Advance the pps id",
+        OFFSET(advance_pps_id), AV_OPT_TYPE_INT,
+        { .i64 = -1 }, -1, HEVC_MAX_PPS_COUNT, FLAGS },
+
     { "level", "Set level (tables A.6 and A.7)",
         OFFSET(level), AV_OPT_TYPE_INT,
         { .i64 = LEVEL_UNSET }, LEVEL_UNSET, 0xff, FLAGS, "level" },
-- 
2.24.0



More information about the ffmpeg-devel mailing list