From patchwork Wed Dec 11 15:02:03 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Wang X-Patchwork-Id: 16739 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 C7E0044A57D for ; Wed, 11 Dec 2019 17:30:22 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A326268AC7C; Wed, 11 Dec 2019 17:30: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 10BB168A9C9 for ; Wed, 11 Dec 2019 17:30:16 +0200 (EET) Received: by mail-pf1-f196.google.com with SMTP id 2so1974861pfx.6 for ; Wed, 11 Dec 2019 07:30: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; bh=jTZCYVfOR8dYK5Da0cLWy6n3D7MTHCQaXywvHPJkYE4=; b=XmGuT/hDa1H1HgBA8Do8qZg5XBYTBW4UU/7QoXdoabUE3PPGFzUeXdKvP35ksIjY4Z p9M2iZPivXELcWrhc/pfP1qxlgJe+9byKci9WouZUowKEky6/QjtP3XsC2MjxA3XYY4P BcMItAgxMIpknxXJjSycOea85wyA+Hf46bEUuhCeA/pFITEUCkrh+qu6PnteTXvaIoYT LosRn35q+36LOqv1OKE3kNldnVZBbkwron94+IgxfgeDs573LEI6v6rylMSlJ10fYr/z UxsjkgvxF9PHz0e0JkMTk7Sp8YQfpBubUeqN/XWewcdPVbVHPru8+1RF7ANyUeRSYv2l E5jQ== 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; bh=jTZCYVfOR8dYK5Da0cLWy6n3D7MTHCQaXywvHPJkYE4=; b=cRK+TWkQvPXbHA1gUfZcoi6YtSfWPOfleOkqphx49m9PrATzvz5IyJwnFcIWxVVcKN fwXXE4hBN5kTeNgLWy74jNADOzQ8YVojbc8wLeCq2GZH3D9MTsnFFLv4RGmuoe1jYHzQ MNdewGyvnw2DopiG0MnEXehvb6DUV84JS+RmuRlm6c1FTE26zUpWViYSGMb99YZv62dP VoUra5AMuwFkHlbEndQC9r5j74VavS1gnbtNH/MeoXh7HLgYYT/+dG/2SsnTseaq0aE5 tQ3zBriW6JXFTAvOfJadg7LosbHnIlITQxtnzMlDzs7p61EFv4zjccvOYJD7frCEl8UO 3Suw== X-Gm-Message-State: APjAAAVuLVYQ8J0+mkcxCEJHm9+ACFzZSmCG39lfIAljp4Srg9rzjDOs qvarmnrFInitaY1CC0MVM3Ld+bby X-Google-Smtp-Source: APXvYqyhzlSZdOkSKYBZLSCyi8hgtJcpeQeJ8IUJBsruPuiY0PBTyMOHwBvHXS7rgbv5PiN1KBI2LA== X-Received: by 2002:a63:2808:: with SMTP id o8mr4551303pgo.39.1576076531416; Wed, 11 Dec 2019 07:02:11 -0800 (PST) Received: from vpn.localdomain ([47.90.99.151]) by smtp.gmail.com with ESMTPSA id p16sm3297403pgi.50.2019.12.11.07.02.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Dec 2019 07:02:10 -0800 (PST) From: lance.lmwang@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Wed, 11 Dec 2019 23:02:03 +0800 Message-Id: <20191211150204.16965-1-lance.lmwang@gmail.com> X-Mailer: git-send-email 2.9.5 Subject: [FFmpeg-devel] [PATCH v1 1/2] avcodec/cbs_h2645: add helper functions to insert and delete SEI PREFIX messages 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 --- libavcodec/cbs_h2645.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ libavcodec/cbs_h265.h | 25 ++++++++++++++ 2 files changed, 118 insertions(+) diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c index 5f71d80..2fc957d 100644 --- a/libavcodec/cbs_h2645.c +++ b/libavcodec/cbs_h2645.c @@ -1612,3 +1612,96 @@ void ff_cbs_h264_delete_sei_message(CodedBitstreamContext *ctx, (sei->payload_count - position) * sizeof(*sei->payload)); } } + +int ff_cbs_h265_add_sei_prefix_message(CodedBitstreamContext *ctx, + CodedBitstreamFragment *au, + H265RawSEIPayload *payload) +{ + H265RawSEI *sei = NULL; + int err, i; + + // Find an existing SEI PREFIX NAL unit to add to. + for (i = 0; i < au->nb_units; i++) { + if (au->units[i].type == HEVC_NAL_SEI_PREFIX) { + sei = au->units[i].content; + if (sei->payload_count < H265_MAX_SEI_PAYLOADS) + break; + + sei = NULL; + } + } + + if (!sei) { + // Need to make a new SEI NAL unit. Insert it before the first + // slice data NAL unit; if no slice data, add at the end. + AVBufferRef *sei_ref; + + sei = av_mallocz(sizeof(*sei)); + if (!sei) { + err = AVERROR(ENOMEM); + goto fail; + } + + sei->nal_unit_header.nal_unit_type = HEVC_NAL_SEI_PREFIX; + sei->nal_unit_header.nuh_layer_id = 0; + sei->nal_unit_header.nuh_temporal_id_plus1 = 1; + + sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei), + &cbs_h265_free_sei, NULL, 0); + if (!sei_ref) { + av_freep(&sei); + err = AVERROR(ENOMEM); + goto fail; + } + + for (i = 0; i < au->nb_units; i++) { + if (au->units[i].type == HEVC_NAL_IDR_W_RADL || + au->units[i].type == HEVC_NAL_IDR_N_LP) + break; + } + + err = ff_cbs_insert_unit_content(ctx, au, i, HEVC_NAL_SEI_PREFIX, + sei, sei_ref); + av_buffer_unref(&sei_ref); + if (err < 0) + goto fail; + } + + memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload)); + ++sei->payload_count; + + return 0; +fail: + cbs_h265_free_sei_payload(payload); + return err; +} + +void ff_cbs_h265_delete_sei_prefix_message(CodedBitstreamContext *ctx, + CodedBitstreamFragment *au, + CodedBitstreamUnit *nal, + int position) +{ + H265RawSEI *sei = nal->content; + + av_assert0(nal->type == HEVC_NAL_SEI_PREFIX); + av_assert0(position >= 0 && position < sei->payload_count); + + if (position == 0 && sei->payload_count == 1) { + // Deleting NAL unit entirely. + int i; + + for (i = 0; i < au->nb_units; i++) { + if (&au->units[i] == nal) + break; + } + + ff_cbs_delete_unit(ctx, au, i); + } else { + cbs_h265_free_sei_payload(&sei->payload[position]); + + --sei->payload_count; + memmove(sei->payload + position, + sei->payload + position + 1, + (sei->payload_count - position) * sizeof(*sei->payload)); + } +} diff --git a/libavcodec/cbs_h265.h b/libavcodec/cbs_h265.h index ad746bf..b7e7dff 100644 --- a/libavcodec/cbs_h265.h +++ b/libavcodec/cbs_h265.h @@ -22,6 +22,7 @@ #include #include +#include "cbs.h" #include "cbs_h2645.h" #include "hevc.h" @@ -745,5 +746,29 @@ typedef struct CodedBitstreamH265Context { const H265RawPPS *active_pps; } CodedBitstreamH265Context; +/** + * Add an SEI message to an access unit. + * + * On success, the payload will be owned by a unit in access_unit; + * on failure, the content of the payload will be freed. + */ +int ff_cbs_h265_add_sei_prefix_message(CodedBitstreamContext *ctx, + CodedBitstreamFragment *access_unit, + H265RawSEIPayload *payload); + +/** + * Delete an SEI message from an access unit. + * + * Deletes from nal_unit, which must be an SEI PREFIX NAL unit. If this is the + * last message in nal_unit, also deletes it from access_unit. + * + * Requires nal_unit to be a unit in access_unit and position to be >= 0 + * and < the payload count of the SEI nal_unit. + */ +void ff_cbs_h265_delete_sei_prefix_message(CodedBitstreamContext *ctx, + CodedBitstreamFragment *access_unit, + CodedBitstreamUnit *nal_unit, + int position); + #endif /* AVCODEC_CBS_H265_H */