From patchwork Tue Jan 7 05:03:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Wang X-Patchwork-Id: 17228 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 EF0CC44B956 for ; Tue, 7 Jan 2020 07:04:20 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D541868A8A0; Tue, 7 Jan 2020 07:04:20 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f194.google.com (mail-pf1-f194.google.com [209.85.210.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4DC4268A956 for ; Tue, 7 Jan 2020 07:04:14 +0200 (EET) Received: by mail-pf1-f194.google.com with SMTP id x184so27996827pfb.3 for ; Mon, 06 Jan 2020 21:04:14 -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=jAzvf6pF33sXmoRFr4DcrzYlMZ87coIf8Qk3Rs7MAoE=; b=UbeqUb0VkYiC/rQ73+7y5ZXpc7esCPAHx+An0DwpQbc2r5fZMBXp1xsD4c99crOlkt KM9CBYcU89ZwBj1cTlBCjhkvitIjDPQL/D32M1f9AYW3kU+SOuwJzY442PhiHAgeuRDw OPhfUmCuNx/5RDsodrDKaRX9D7i1oUg5IFA433ck6Hduiyqx/oQsbu8SGByDiihHJpip J6Wl2dTExyZUY7OtcB+31eBDsycKcYNdghBOj8SZPwCJNhpAk1tdxrF4ryeyR4cC39rj IzCEHjYucIbfoxcrhWIMVaX0KR7+Anvyqurqi5zwdsEV6lir7exXBmCm9z4a55do98fm H30w== 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=jAzvf6pF33sXmoRFr4DcrzYlMZ87coIf8Qk3Rs7MAoE=; b=WmmTNqJgTGxB5Cp/0cReiwJ8FEKOIZ0/xXJ7E0Yb5bteDWL9M5h4xgsipl5ExlEhcm mIMu0fDhdpJ2+k8yhDc8GjiSUSWbq/UT2ZoWfQ6nL0/sOoOGtJtdMv1gt8DOPhAOaKuM TufGV2WYXr2c7BJPF8fhDAi69BVPnZDix7OTm1Y/hG517l7Go11DiV6TKyWTC92c1dqM 3MyuKL6+7fX+87ZQgliNiunuPucYC/MUDHPwxBAeOUhxYmagW/+V27ztz/cfdkRItjD1 WAUKoLwB4gZo+CbQ+lxRVhwjsPnp2sqA8JmQqIemyl6JepWcpsxv8i98bbP9s1O9VBnP okGg== X-Gm-Message-State: APjAAAUw7d1ljsGE6O3CVEwSQtdRrw1uqp9C7uvvxm/pYuoEYd1CJEUe 3iPvJYoBTP96gW/qWljCmMvrewsV X-Google-Smtp-Source: APXvYqx3rObvEQHL3f93t3L2XZkQpnEOX9BcFwj0Elc0HzpyoCwWTSdOm1KT8Y99VjWR8C1bPY2pxQ== X-Received: by 2002:a63:5512:: with SMTP id j18mr110210939pgb.189.1578373452442; Mon, 06 Jan 2020 21:04:12 -0800 (PST) Received: from vpn.localdomain ([47.90.99.151]) by smtp.gmail.com with ESMTPSA id j9sm74096591pfn.152.2020.01.06.21.04.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Jan 2020 21:04:11 -0800 (PST) From: lance.lmwang@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Tue, 7 Jan 2020 13:03:54 +0800 Message-Id: <20200107050355.17503-7-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 7/8] 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 5f71d80584..2fc957dc27 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 ad746bf35f..b7e7dff658 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 */