From 2906612df2bec8034046038dd025e5432fbe5a7f Mon Sep 17 00:00:00 2001 From: gnattu Date: Tue, 15 Oct 2024 07:43:16 +0800 Subject: [PATCH 1/6] avcodec: add remove_dovi and remove_hdr10plus option to hevc_metadata bsf --- .../0077-add-remove-dovi-hdr10plus-bsf.patch | 73 +++++++++++++++++++ debian/patches/series | 1 + 2 files changed, 74 insertions(+) create mode 100644 debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch diff --git a/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch b/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch new file mode 100644 index 0000000000..686d6b62e8 --- /dev/null +++ b/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch @@ -0,0 +1,73 @@ +Index: FFmpeg/libavcodec/bsf/h265_metadata.c +=================================================================== +--- FFmpeg.orig/libavcodec/bsf/h265_metadata.c ++++ FFmpeg/libavcodec/bsf/h265_metadata.c +@@ -27,6 +27,7 @@ + #include "h2645data.h" + #include "hevc.h" + #include "h265_profile_level.h" ++#include "itut35.h" + + enum { + LEVEL_UNSET = -2, +@@ -62,6 +63,8 @@ typedef struct H265MetadataContext { + int level; + int level_guess; + int level_warned; ++ int remove_dovi; ++ int remove_hdr10plus; + } H265MetadataContext; + + +@@ -385,6 +388,37 @@ static int h265_metadata_update_fragment + if (err < 0) + return err; + } ++ if (ctx->remove_dovi) { ++ if (au->units[i].type == HEVC_NAL_UNSPEC62) { // Dolby Vision RPU ++ ff_cbs_delete_unit(au, i); ++ av_log(bsf, AV_LOG_DEBUG, "Removing Dolby Vision RPU\n"); ++ } ++ if (au->units[i].type == HEVC_NAL_UNSPEC63) { // Dolby Vision EL ++ ff_cbs_delete_unit(au, i); ++ av_log(bsf, AV_LOG_DEBUG, "Removing Dolby Vision EL\n"); ++ } ++ } ++ if (ctx->remove_hdr10plus) { ++ // This implementation is not strictly correct as it does not decode the entire NAL. ++ // There could be multiple SEIs packed within a single NAL, and some of them may not be HDR10+ metadata. ++ // The current implementation simply removes the entire NAL without further inspection. ++ if (au->units[i].type == HEVC_NAL_SEI_PREFIX && au->units[i].data_size > 8 * sizeof(uint8_t)) { ++ uint8_t *nal_sei = au->units[i].data; ++ // This Matches ITU-T T.35 SMPTE ST 2094-40 ++ if (nal_sei[0] == 0x4E && nal_sei[1] == 0x01 && nal_sei[2] == 0x04) { ++ if (nal_sei[4] == ITU_T_T35_COUNTRY_CODE_US && nal_sei[6] == ITU_T_T35_PROVIDER_CODE_SMTPE) { ++ // identifier for HDR10+ ++ const uint8_t smpte2094_40_provider_oriented_code = 0x01; ++ const uint8_t smpte2094_40_application_identifier = 0x04; ++ if (nal_sei[8] == smpte2094_40_provider_oriented_code && nal_sei[9] == smpte2094_40_application_identifier) { ++ av_log(bsf, AV_LOG_DEBUG, "Found HDR10+ metadata, removing NAL\n"); ++ ff_cbs_delete_unit(au, i); ++ } ++ } ++ ++ } ++ } ++ } + } + + return 0; +@@ -478,6 +512,13 @@ static const AVOption h265_metadata_opti + { LEVEL("8.5", 255) }, + #undef LEVEL + ++ { "remove_dovi", "Remove Dolby Vision BL and RPU", ++ OFFSET(remove_dovi), AV_OPT_TYPE_BOOL, ++ { .i64 = 0 }, 0, 1, FLAGS }, ++ { "remove_hdr10plus", "Remove NALs including HDR10+ metadata", ++ OFFSET(remove_hdr10plus), AV_OPT_TYPE_BOOL, ++ { .i64 = 0 }, 0, 1, FLAGS }, ++ + { NULL } + }; + diff --git a/debian/patches/series b/debian/patches/series index 89de7fae22..8a9bb4501a 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -74,3 +74,4 @@ 0074-fix-the-sub2video-perf-regressions.patch 0075-allow-vpl-qsv-to-init-with-the-legacy-msdk-path.patch 0076-alway-set-videotoolboxenc-pixel-buffer-info.patch +0077-add-remove-dovi-hdr10plus-bsf.patch From ba9dd312ef83fd6074ca6ad2ab0966c6ebb93b8a Mon Sep 17 00:00:00 2001 From: gnattu Date: Tue, 15 Oct 2024 08:48:59 +0800 Subject: [PATCH 2/6] also remove dovi config --- debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch b/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch index 686d6b62e8..130eec9d2a 100644 --- a/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch +++ b/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch @@ -19,7 +19,7 @@ Index: FFmpeg/libavcodec/bsf/h265_metadata.c } H265MetadataContext; -@@ -385,6 +388,37 @@ static int h265_metadata_update_fragment +@@ -385,6 +388,40 @@ static int h265_metadata_update_fragment if (err < 0) return err; } @@ -32,6 +32,9 @@ Index: FFmpeg/libavcodec/bsf/h265_metadata.c + ff_cbs_delete_unit(au, i); + av_log(bsf, AV_LOG_DEBUG, "Removing Dolby Vision EL\n"); + } ++ av_packet_side_data_remove(bsf->par_out->coded_side_data, ++ &bsf->par_out->nb_coded_side_data, ++ AV_PKT_DATA_DOVI_CONF); + } + if (ctx->remove_hdr10plus) { + // This implementation is not strictly correct as it does not decode the entire NAL. @@ -57,7 +60,7 @@ Index: FFmpeg/libavcodec/bsf/h265_metadata.c } return 0; -@@ -478,6 +512,13 @@ static const AVOption h265_metadata_opti +@@ -478,6 +515,13 @@ static const AVOption h265_metadata_opti { LEVEL("8.5", 255) }, #undef LEVEL From 75ab7952a32f28f141af12e486027d16f4812212 Mon Sep 17 00:00:00 2001 From: gnattu Date: Tue, 15 Oct 2024 12:23:18 +0800 Subject: [PATCH 3/6] avcodec/hevc_metadata: only remove side config once --- .../0077-add-remove-dovi-hdr10plus-bsf.patch | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch b/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch index 130eec9d2a..e8c17a082d 100644 --- a/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch +++ b/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch @@ -19,7 +19,7 @@ Index: FFmpeg/libavcodec/bsf/h265_metadata.c } H265MetadataContext; -@@ -385,6 +388,40 @@ static int h265_metadata_update_fragment +@@ -385,6 +388,37 @@ static int h265_metadata_update_fragment if (err < 0) return err; } @@ -32,9 +32,6 @@ Index: FFmpeg/libavcodec/bsf/h265_metadata.c + ff_cbs_delete_unit(au, i); + av_log(bsf, AV_LOG_DEBUG, "Removing Dolby Vision EL\n"); + } -+ av_packet_side_data_remove(bsf->par_out->coded_side_data, -+ &bsf->par_out->nb_coded_side_data, -+ AV_PKT_DATA_DOVI_CONF); + } + if (ctx->remove_hdr10plus) { + // This implementation is not strictly correct as it does not decode the entire NAL. @@ -60,7 +57,20 @@ Index: FFmpeg/libavcodec/bsf/h265_metadata.c } return 0; -@@ -478,6 +515,13 @@ static const AVOption h265_metadata_opti +@@ -399,6 +433,12 @@ static const CBSBSFType h265_metadata_ty + + static int h265_metadata_init(AVBSFContext *bsf) + { ++ if (((H265MetadataContext *)bsf->priv_data)->remove_dovi) { ++ av_log(bsf, AV_LOG_WARNING, "Removing Dolby Vision config\n"); ++ av_packet_side_data_remove(bsf->par_out->coded_side_data, ++ &bsf->par_out->nb_coded_side_data, ++ AV_PKT_DATA_DOVI_CONF); ++ } + return ff_cbs_bsf_generic_init(bsf, &h265_metadata_type); + } + +@@ -478,6 +518,13 @@ static const AVOption h265_metadata_opti { LEVEL("8.5", 255) }, #undef LEVEL From 6c1cdd3043610707150f095a4e3a246363023fa6 Mon Sep 17 00:00:00 2001 From: gnattu Date: Tue, 15 Oct 2024 13:53:37 +0800 Subject: [PATCH 4/6] remove redundant log --- debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch b/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch index e8c17a082d..8fcc97bb92 100644 --- a/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch +++ b/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch @@ -57,12 +57,11 @@ Index: FFmpeg/libavcodec/bsf/h265_metadata.c } return 0; -@@ -399,6 +433,12 @@ static const CBSBSFType h265_metadata_ty +@@ -399,6 +433,11 @@ static const CBSBSFType h265_metadata_ty static int h265_metadata_init(AVBSFContext *bsf) { + if (((H265MetadataContext *)bsf->priv_data)->remove_dovi) { -+ av_log(bsf, AV_LOG_WARNING, "Removing Dolby Vision config\n"); + av_packet_side_data_remove(bsf->par_out->coded_side_data, + &bsf->par_out->nb_coded_side_data, + AV_PKT_DATA_DOVI_CONF); @@ -70,7 +69,7 @@ Index: FFmpeg/libavcodec/bsf/h265_metadata.c return ff_cbs_bsf_generic_init(bsf, &h265_metadata_type); } -@@ -478,6 +518,13 @@ static const AVOption h265_metadata_opti +@@ -478,6 +517,13 @@ static const AVOption h265_metadata_opti { LEVEL("8.5", 255) }, #undef LEVEL From 15dd01d2d4a316a9cc001de03bac3bb231975108 Mon Sep 17 00:00:00 2001 From: gnattu Date: Wed, 16 Oct 2024 19:05:36 +0800 Subject: [PATCH 5/6] bsf/hevc_metadata: fix documentation or remove_dovi --- debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch b/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch index 8fcc97bb92..32579463ab 100644 --- a/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch +++ b/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch @@ -73,7 +73,7 @@ Index: FFmpeg/libavcodec/bsf/h265_metadata.c { LEVEL("8.5", 255) }, #undef LEVEL -+ { "remove_dovi", "Remove Dolby Vision BL and RPU", ++ { "remove_dovi", "Remove Dolby Vision EL and RPU", + OFFSET(remove_dovi), AV_OPT_TYPE_BOOL, + { .i64 = 0 }, 0, 1, FLAGS }, + { "remove_hdr10plus", "Remove NALs including HDR10+ metadata", From 547d233809dfa91439012158f9645fe7b5ad5a69 Mon Sep 17 00:00:00 2001 From: gnattu Date: Wed, 16 Oct 2024 19:08:08 +0800 Subject: [PATCH 6/6] bsf/av1_metadata: add remove_dovi and remove_hdr10plus --- .../0077-add-remove-dovi-hdr10plus-bsf.patch | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch b/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch index 32579463ab..9187848144 100644 --- a/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch +++ b/debian/patches/0077-add-remove-dovi-hdr10plus-bsf.patch @@ -83,3 +83,103 @@ Index: FFmpeg/libavcodec/bsf/h265_metadata.c { NULL } }; +Index: FFmpeg/libavcodec/bsf/av1_metadata.c +=================================================================== +--- FFmpeg.orig/libavcodec/bsf/av1_metadata.c ++++ FFmpeg/libavcodec/bsf/av1_metadata.c +@@ -17,6 +17,7 @@ + */ + + #include "libavutil/common.h" ++#include "libavutil/intreadwrite.h" + #include "libavutil/opt.h" + + #include "bsf.h" +@@ -24,6 +25,7 @@ + #include "cbs.h" + #include "cbs_bsf.h" + #include "cbs_av1.h" ++#include "itut35.h" + + typedef struct AV1MetadataContext { + CBSBSFContext common; +@@ -42,6 +44,9 @@ typedef struct AV1MetadataContext { + int num_ticks_per_picture; + + int delete_padding; ++ ++ int remove_dovi; ++ int remove_hdr10plus; + } AV1MetadataContext; + + +@@ -140,6 +145,42 @@ static int av1_metadata_update_fragment( + } + } + ++ if (ctx->remove_dovi ||ctx->remove_hdr10plus) { ++ int provider_code, provider_oriented_code, application_identifier; ++ for (i = frag->nb_units - 1; i >= 0; i--) { ++ if (frag->units[i].type == AV1_OBU_METADATA) { ++ AV1RawOBU *obu = frag->units[i].content; ++ AV1RawMetadataITUTT35 *t35 = &obu->obu.metadata.metadata.itut_t35; ++ if (obu->obu.metadata.metadata_type != AV1_METADATA_TYPE_ITUT_T35 || ++ t35->itu_t_t35_country_code != ITU_T_T35_COUNTRY_CODE_US || ++ t35->payload_size < 6) { ++ continue; ++ } ++ ++ provider_code = AV_RB16(t35->payload); ++ ++ if (ctx->remove_dovi && provider_code == ITU_T_T35_PROVIDER_CODE_DOLBY) { ++ provider_oriented_code = AV_RB32(t35->payload + 2); ++ // Dolby Vision RPU ++ if (provider_oriented_code == 0x800) { ++ av_log(bsf, AV_LOG_DEBUG, "Removing Dolby Vision RPU\n"); ++ ff_cbs_delete_unit(frag, i); ++ } ++ } ++ ++ if (ctx->remove_hdr10plus && provider_code == ITU_T_T35_PROVIDER_CODE_SMTPE) { ++ provider_oriented_code = AV_RB16(t35->payload + 2); ++ application_identifier = AV_RB8(t35->payload + 4); ++ // HDR10+ Metadata ++ if (provider_oriented_code == 0x01 && application_identifier == 0x04) { ++ av_log(bsf, AV_LOG_DEBUG, "Removing HDR10+ Metadata\n"); ++ ff_cbs_delete_unit(frag, i); ++ } ++ } ++ } ++ } ++ } ++ + return 0; + } + +@@ -158,6 +199,12 @@ static int av1_metadata_init(AVBSFContex + .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER, + }; + ++ if (ctx->remove_dovi) { ++ av_packet_side_data_remove(bsf->par_out->coded_side_data, ++ &bsf->par_out->nb_coded_side_data, ++ AV_PKT_DATA_DOVI_CONF); ++ } ++ + return ff_cbs_bsf_generic_init(bsf, &av1_metadata_type); + } + +@@ -206,6 +253,13 @@ static const AVOption av1_metadata_optio + OFFSET(delete_padding), AV_OPT_TYPE_BOOL, + { .i64 = 0 }, 0, 1, FLAGS}, + ++ { "remove_dovi", "Remove Dolby Vision RPU", ++ OFFSET(remove_dovi), AV_OPT_TYPE_BOOL, ++ { .i64 = 0 }, 0, 1, FLAGS }, ++ { "remove_hdr10plus", "Remove HDR10+ metadata", ++ OFFSET(remove_hdr10plus), AV_OPT_TYPE_BOOL, ++ { .i64 = 0 }, 0, 1, FLAGS }, ++ + { NULL } + }; +