From patchwork Tue Jan 7 05:03:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Wang X-Patchwork-Id: 17229 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2299D44B956 for ; Tue, 7 Jan 2020 07:04:22 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0DCBE68AAAE; Tue, 7 Jan 2020 07:04:22 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AB8EA68AADF for ; Tue, 7 Jan 2020 07:04:15 +0200 (EET) Received: by mail-pf1-f196.google.com with SMTP id w62so27979892pfw.8 for ; Mon, 06 Jan 2020 21:04:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=H63erN3G4GAMyzlzlpebPIKQWCTbD6Nrdszr0RIbgoc=; b=pH2RFSMWosoKZPIxwPRRAxYjy6weUleyo1aNw+knfXL8SKlRIsFK3ztgA8yyI1lPDH QbXxXgWY2PpMLvm0nYSVp1MCwZDXkWV/JrlYgWBNcS0m1H3CSEN/XReizeoTBtbZQXtA 9cvgOxmKfzdJ2m3QancJdHsS7NW35zC8H5ZWUyyCZQ98Vlvp662cncbjm38b8rgSJlXd AUEd5pFbZbl7Zv+aGIpu2ljjfFiVuRNiiLuCH4qUsZW0bhsNPNH5116oRlqZW0wLuY+C lvKRNvyhg+lRyXoB+Wm++pkYa3vMCt0/+x6M+LeAF8MBLsY4mi/tnvUhW1IiO66C0hrM WSbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=H63erN3G4GAMyzlzlpebPIKQWCTbD6Nrdszr0RIbgoc=; b=g+/a83JjoMpJfY+30gY0L2DvtwawFpnIbph9W5Ra/PyzxWo3OkUfPTIOvCa/TfxJ8y o+1DNipzbezXMXwgkqf/Puqx9/zuuV54Du3yoEBPOyqwtQqKdk4fb1tj1Bjyi6bj135k ZFzMiI9LnjcD65VFShYpBHatZ+F2m8cNjJPgbkQ+oW78CfiWbLlMD9+dLD8wH1EBM6Ut 41Yt+RTofOVQsBYgSwW9o0Hlxw2CRs5EodlsdnZ7JZf6Ma+Hnc1KPMWZLap9WllGL4UJ 9tcAclF0yDd+eoMItUpHIMabuytYAV21VNDjjPaMCTUHByox3R4ng7/Y5sJCN55xdQUD lT6Q== X-Gm-Message-State: APjAAAVQB5ldM97iRaOVziUcHmSwPNVHGPH9XSeHUENKTttFRHbQ9KOC zoD5owV3ojyvljL3520xsfdeS0HN X-Google-Smtp-Source: APXvYqywgJwqWBOutv2WylgAf08fv3NZjB0OWK6P1UZOdvYRiQ+rKXJV6vom5WZX60gdQG5c743jVQ== X-Received: by 2002:a63:4e22:: with SMTP id c34mr112664375pgb.214.1578373453810; Mon, 06 Jan 2020 21:04:13 -0800 (PST) Received: from vpn.localdomain ([47.90.99.151]) by smtp.gmail.com with ESMTPSA id j9sm74096591pfn.152.2020.01.06.21.04.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Jan 2020 21:04:13 -0800 (PST) From: lance.lmwang@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Tue, 7 Jan 2020 13:03:55 +0800 Message-Id: <20200107050355.17503-8-lance.lmwang@gmail.com> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20200107050355.17503-1-lance.lmwang@gmail.com> References: <20191217102217.4811-1-lance.lmwang@gmail.com> <20200107050355.17503-1-lance.lmwang@gmail.com> Subject: [FFmpeg-devel] [PATCH v7 8/8] avcodec/h265_metadata_bsf: add option to insert a string as SEI unregistered user data X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Limin Wang MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Limin Wang Signed-off-by: Limin Wang --- doc/bitstream_filters.texi | 8 ++++ libavcodec/h265_metadata_bsf.c | 68 +++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi index 8fe5b3ad75..81b41d70b3 100644 --- a/doc/bitstream_filters.texi +++ b/doc/bitstream_filters.texi @@ -376,6 +376,14 @@ The argument must be the name of a level (for example, @samp{5.1}), a or the special name @samp{auto} indicating that the filter should attempt to guess the level from the input stream properties. +@item sei_user_data +Insert a string as SEI unregistered user data. The argument must +be of the form @emph{UUID+string}, where the UUID is as a 32-character +(16 bytes) hexadecimal string possibly separated by hyphens, and the +string can be anything. + +For example, @samp{086f3693-b7b3-4f2c-9653-21492feee5b8+hello} will +insert the string ``hello'' associated with the given 32-bit UUID. @end table @section hevc_mp4toannexb diff --git a/libavcodec/h265_metadata_bsf.c b/libavcodec/h265_metadata_bsf.c index b3a1fda144..312545cbe6 100644 --- a/libavcodec/h265_metadata_bsf.c +++ b/libavcodec/h265_metadata_bsf.c @@ -16,6 +16,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/avstring.h" #include "libavutil/common.h" #include "libavutil/opt.h" @@ -23,6 +24,7 @@ #include "cbs.h" #include "cbs_h265.h" #include "hevc.h" +#include "hevc_sei.h" #include "h265_profile_level.h" enum { @@ -65,6 +67,8 @@ typedef struct H265MetadataContext { int crop_top; int crop_bottom; + const char *sei_user_data; + int level; int level_guess; int level_warned; @@ -340,7 +344,7 @@ static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *pkt) { H265MetadataContext *ctx = bsf->priv_data; CodedBitstreamFragment *au = &ctx->access_unit; - int err, i; + int err, i, j, has_sps = 0, has_vps = 0, has_keyframe = 0; err = ff_bsf_get_packet_ref(bsf, pkt); if (err < 0) @@ -410,11 +414,70 @@ static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *pkt) err = h265_metadata_update_vps(bsf, au->units[i].content); if (err < 0) goto fail; + has_vps = 1; } if (au->units[i].type == HEVC_NAL_SPS) { err = h265_metadata_update_sps(bsf, au->units[i].content); if (err < 0) goto fail; + has_sps = 1; + } + if (au->units[i].type >= HEVC_NAL_BLA_W_LP && + au->units[i].type <= HEVC_NAL_IRAP_VCL23) + has_keyframe = 1; + } + + // Only insert the SEI in access units containing SPSs or VPSs or Keyframe + if (ctx->sei_user_data && (has_sps || has_vps || has_keyframe)) { + H265RawSEIPayload payload = { + .payload_type = HEVC_SEI_TYPE_USER_DATA_UNREGISTERED, + }; + H265RawSEIUserDataUnregistered *udu = + &payload.payload.user_data_unregistered; + + for (i = j = 0; j < 32 && ctx->sei_user_data[i]; i++) { + int c, v; + c = ctx->sei_user_data[i]; + if (c == '-') { + continue; + } else if (av_isxdigit(c)) { + c = av_tolower(c); + v = (c <= '9' ? c - '0' : c - 'a' + 10); + } else { + goto invalid_user_data; + } + if (j & 1) + udu->uuid_iso_iec_11578[j / 2] |= v; + else + udu->uuid_iso_iec_11578[j / 2] = v << 4; + ++j; + } + if (j == 32 && ctx->sei_user_data[i] == '+') { + size_t len = strlen(ctx->sei_user_data + i + 1); + + udu->data_ref = av_buffer_alloc(len + 1); + if (!udu->data_ref) { + err = AVERROR(ENOMEM); + goto fail; + } + + udu->data = udu->data_ref->data; + udu->data_length = len + 1; + memcpy(udu->data, ctx->sei_user_data + i + 1, len + 1); + + err = ff_cbs_h265_add_sei_prefix_message(ctx->cbc, au, &payload); + if (err < 0) { + av_log(bsf, AV_LOG_ERROR, "Failed to add user data SEI " + "message to access unit.\n"); + goto fail; + } + + } else { +invalid_user_data: + av_log(bsf, AV_LOG_ERROR, "Invalid user data: " + "must be \"UUID+string\".\n"); + err = AVERROR(EINVAL); + goto fail; } } @@ -547,6 +610,9 @@ static const AVOption h265_metadata_options[] = { OFFSET(crop_bottom), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS }, + { "sei_user_data", "Insert SEI user data (UUID+string)", + OFFSET(sei_user_data), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS }, + { "level", "Set level (tables A.6 and A.7)", OFFSET(level), AV_OPT_TYPE_INT, { .i64 = LEVEL_UNSET }, LEVEL_UNSET, 0xff, FLAGS, "level" },