diff mbox series

[FFmpeg-devel] avcodec/h265_metadata_bsf: Add vps/sps/pps id offset

Message ID 20200108164736.26243-1-eran.gonen@cloudinary.com
State New
Headers show
Series [FFmpeg-devel] avcodec/h265_metadata_bsf: Add vps/sps/pps id offset
Related show

Checks

Context Check Description
andriy/ffmpeg-patchwork pending
andriy/ffmpeg-patchwork success Applied patch
andriy/ffmpeg-patchwork success Configure finished
andriy/ffmpeg-patchwork success Make finished
andriy/ffmpeg-patchwork success Make fate finished

Commit Message

Eran Gonen Jan. 8, 2020, 4:47 p.m. UTC
---
 doc/bitstream_filters.texi     |   5 ++
 libavcodec/h265_metadata_bsf.c | 106 +++++++++++++++++++++++++++++++--
 2 files changed, 105 insertions(+), 6 deletions(-)

Comments

Mark Thompson Jan. 8, 2020, 9:25 p.m. UTC | #1
On 08/01/2020 16:47, Eran Gonen wrote:
> ---
>  doc/bitstream_filters.texi     |   5 ++
>  libavcodec/h265_metadata_bsf.c | 106 +++++++++++++++++++++++++++++++--
>  2 files changed, 105 insertions(+), 6 deletions(-)

To repeat my previous comment, I don't think this belongs in h265_metadata since it's editing something which isn't really metadata.  A separate BSF for doing this sort of thing would make more sense (particularly if it then implemented some user-facing feature which you are using this transformation to enable).

Also note that you need to edit various SEI structures which refer to parameter set IDs as well (buffering_period most importantly, since that is very commonly used).

- Mark
diff mbox series

Patch

diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index 8fe5b3ad75..2e874bf6ff 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -357,6 +357,11 @@  Set poc_proportional_to_timing_flag in VPS and VUI and use this value
 to set num_ticks_poc_diff_one_minus1 (see H.265 sections 7.4.3.1 and
 E.3.1).  Ignored if @option{tick_rate} is not also set.
 
+@item vps_id_offset
+@item sps_id_offset
+@item pps_id_offset
+Replace the original VPS/SPS/PPS identifier by increasing its value by an offset.
+
 @item crop_left
 @item crop_right
 @item crop_top
diff --git a/libavcodec/h265_metadata_bsf.c b/libavcodec/h265_metadata_bsf.c
index b3a1fda144..7aeb1bffd1 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 vps_id_offset;
+    int sps_id_offset;
+    int pps_id_offset;
 } H265MetadataContext;
 
 
@@ -168,6 +172,8 @@  static int h265_metadata_update_vps(AVBSFContext *bsf,
 {
     H265MetadataContext *ctx = bsf->priv_data;
 
+    vps->vps_video_parameter_set_id = (vps->vps_video_parameter_set_id + ctx->vps_id_offset) % HEVC_MAX_VPS_COUNT;
+
     if (ctx->tick_rate.num && ctx->tick_rate.den) {
         int num, den;
 
@@ -200,6 +206,9 @@  static int h265_metadata_update_sps(AVBSFContext *bsf,
     int need_vui = 0;
     int crop_unit_x, crop_unit_y;
 
+    sps->sps_video_parameter_set_id = (sps->sps_video_parameter_set_id + ctx->vps_id_offset) % HEVC_MAX_VPS_COUNT;
+    sps->sps_seq_parameter_set_id = (sps->sps_seq_parameter_set_id + ctx->sps_id_offset) % 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 +345,27 @@  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;
+
+    pps->pps_seq_parameter_set_id = (pps->pps_seq_parameter_set_id + ctx->sps_id_offset) % HEVC_MAX_SPS_COUNT;
+    pps->pps_pic_parameter_set_id = (pps->pps_pic_parameter_set_id + ctx->pps_id_offset) % HEVC_MAX_PPS_COUNT;
+
+    return 0;
+}
+
+static int h265_metadata_update_slice_header(AVBSFContext *bsf,
+        H265RawSliceHeader *slice)
+{
+    H265MetadataContext *ctx = bsf->priv_data;
+
+    slice->slice_pic_parameter_set_id = (slice->slice_pic_parameter_set_id + ctx->pps_id_offset) % HEVC_MAX_PPS_COUNT;
+
+    return 0;
+}
+
 static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
 {
     H265MetadataContext *ctx = bsf->priv_data;
@@ -406,15 +436,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 +512,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 +631,16 @@  static const AVOption h265_metadata_options[] = {
         OFFSET(crop_bottom), AV_OPT_TYPE_INT,
         { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
 
+    { "vps_id_offset", "Increase the vps id by this offset",
+        OFFSET(vps_id_offset), AV_OPT_TYPE_INT,
+        { .i64 = 0 }, 0, HEVC_MAX_VPS_COUNT, FLAGS },
+    {  "sps_id_offset", "Increase the sps id by this offset",
+        OFFSET(sps_id_offset), AV_OPT_TYPE_INT,
+        { .i64 = 0 }, 0, HEVC_MAX_SPS_COUNT, FLAGS },
+    {  "pps_id_offset", "Increase the pps id by this offset",
+        OFFSET(pps_id_offset), AV_OPT_TYPE_INT,
+        { .i64 = 0 }, 0, 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" },