From patchwork Sun May 3 22:05:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 19465 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 1C2F144B916 for ; Mon, 4 May 2020 01:06:12 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0AFA168C1AB; Mon, 4 May 2020 01:06:12 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7DE6E68C0DF for ; Mon, 4 May 2020 01:06:05 +0300 (EEST) Received: by mail-wr1-f45.google.com with SMTP id d15so18682705wrx.3 for ; Sun, 03 May 2020 15:06:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=yZpWH3kcBD0+h+4OkUCTQXPYuUoMOnR5ayj5b+8YBhY=; b=NCmYF76hXH0KpoN6qhXk6jkxOWgH0Uy4acByJN4k+oNIB0IHUNYVVdFpAjeZ+GbxKl 2kMdyPSAfOEMcTp4zm/bWX5lRL9+yLIIgq4ZuWqG7kiZ3ORB95mzOakcQ2qjKSTx18mo ZXTUG5fFBDUMDtYM4qJoGEfUunDZHsy1MyY3Scx1WtXmvlTlz89NQ+dY5OzgIpnwWMEE d2P1tdaGVWhkg2UmAe984AIO1pdRlsuIaMx9spQZzb3oUUc4dYIr1jzSNC8d+o/8yvWP Z0QhLPr3Y3vQuNz5FVnTgEyzmy9gij+06z6VWiXxD1P4H8zsQKYX+vOCzERKhaUIs6ss YUOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yZpWH3kcBD0+h+4OkUCTQXPYuUoMOnR5ayj5b+8YBhY=; b=Q8PMRNf28sGiXRkDV9ae51RcQofBQkIYikkIOioZPDwEMAOr6F4JX82MS0Dizs1Bw0 aLW+wKhYuGXNHjVS3Tx5cqixMo+FUAFi+b7f3QebpirQdJcgOYpB64yQaR8ctE9JUntl 4p80LaNF98viNZjehPn7yfr5im48dHPuM6jjwKg/IJg0wDRU4L78Rr98rY07eUSSCA/3 7n4VeFbKnuEpK787Xe0tDMFzHmcLVVXfBJ9RJC0O+NZMEYmBi8ytoSTZBSXiw08pPbP3 ygjHgz49NT3ziD7WtXV5oQk4wB+drr4BQf0c6nyzPSo06PnKSOvqptanCzmrReWYAjAj ol3g== X-Gm-Message-State: AGi0PuZ0uiU6BHOGblifUwmfQP7mrFC/nKrIDy5V4CFDXupc4+pQvyjl 4jYMKw8hPw8arrIdLlxkxMS/R0fQE3I= X-Google-Smtp-Source: APiQypIzG0dwZ8+QCtjYcc/43VXqPEFwRowsZEbLNH12H0UKo8rEbDkgqVOBPnO47dL/1MaSHPxc1Q== X-Received: by 2002:adf:f5ce:: with SMTP id k14mr17024393wrp.39.1588543564255; Sun, 03 May 2020 15:06:04 -0700 (PDT) Received: from rywe.jkqxz.net (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id a24sm10307285wmb.24.2020.05.03.15.06.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 May 2020 15:06:03 -0700 (PDT) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Sun, 3 May 2020 23:05:40 +0100 Message-Id: <20200503220548.23543-14-sw@jkqxz.net> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200503220548.23543-1-sw@jkqxz.net> References: <20200503220548.23543-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5 14/22] cbs_h264: Simplify SEI addition 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" At the same time, move the H.264 SEI functions to a new file - the combined H.26[45] CBS file is already very large, and these functions do not require any of the common read/write elements. --- libavcodec/Makefile | 2 +- libavcodec/cbs_h264.c | 106 +++++++++++++++++++++++++++++++++++++++++ libavcodec/cbs_h264.h | 11 +++++ libavcodec/cbs_h2645.c | 96 +------------------------------------ 4 files changed, 120 insertions(+), 95 deletions(-) create mode 100644 libavcodec/cbs_h264.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 28076c2c83..076f2fd953 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -67,7 +67,7 @@ OBJS-$(CONFIG_BSWAPDSP) += bswapdsp.o OBJS-$(CONFIG_CABAC) += cabac.o OBJS-$(CONFIG_CBS) += cbs.o OBJS-$(CONFIG_CBS_AV1) += cbs_av1.o -OBJS-$(CONFIG_CBS_H264) += cbs_h2645.o h2645_parse.o +OBJS-$(CONFIG_CBS_H264) += cbs_h2645.o cbs_h264.o h2645_parse.o OBJS-$(CONFIG_CBS_H265) += cbs_h2645.o h2645_parse.o OBJS-$(CONFIG_CBS_JPEG) += cbs_jpeg.o OBJS-$(CONFIG_CBS_MPEG2) += cbs_mpeg2.o diff --git a/libavcodec/cbs_h264.c b/libavcodec/cbs_h264.c new file mode 100644 index 0000000000..75759c7f25 --- /dev/null +++ b/libavcodec/cbs_h264.c @@ -0,0 +1,106 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "cbs_h264.h" + +int ff_cbs_h264_add_sei_message(CodedBitstreamContext *ctx, + CodedBitstreamFragment *au, + H264RawSEIPayload *payload) +{ + H264RawSEI *sei = NULL; + int err, i; + + // Find an existing SEI NAL unit to add to. + for (i = 0; i < au->nb_units; i++) { + if (au->units[i].type == H264_NAL_SEI) { + sei = au->units[i].content; + if (sei->payload_count < H264_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. + CodedBitstreamUnit *unit; + + for (i = 0; i < au->nb_units; i++) { + if (au->units[i].type == H264_NAL_SLICE || + au->units[i].type == H264_NAL_IDR_SLICE) + break; + } + + err = ff_cbs_insert_unit(ctx, au, i); + if (err < 0) + goto fail; + unit = &au->units[i]; + + err = ff_cbs_alloc_unit_content2(ctx, unit); + if (err < 0) { + ff_cbs_delete_unit(ctx, au, i); + goto fail; + } + sei = unit->content; + + *sei = (H264RawSEI) { + .nal_unit_header = { + .nal_unit_type = H264_NAL_SEI, + }, + }; + } + + memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload)); + ++sei->payload_count; + + return 0; +fail: + ff_cbs_h264_free_sei_payload(payload); + return err; +} + +void ff_cbs_h264_delete_sei_message(CodedBitstreamContext *ctx, + CodedBitstreamFragment *au, + CodedBitstreamUnit *nal, + int position) +{ + H264RawSEI *sei = nal->content; + + av_assert0(nal->type == H264_NAL_SEI); + 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 { + ff_cbs_h264_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_h264.h b/libavcodec/cbs_h264.h index 65659ae52c..abc0c1b732 100644 --- a/libavcodec/cbs_h264.h +++ b/libavcodec/cbs_h264.h @@ -466,11 +466,22 @@ typedef struct CodedBitstreamH264Context { } CodedBitstreamH264Context; +/** + * Free an SEI payload structure. + * + * This is useful to have standalone when manipulating SEI messages inside + * an access unit. + */ +void ff_cbs_h264_free_sei_payload(H264RawSEIPayload *payload); + /** * 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. + * + * Adds at the end of an existing SEI NAL unit if one is present, otherwise + * creates a new SEI NAL unit to contain the message. */ int ff_cbs_h264_add_sei_message(CodedBitstreamContext *ctx, CodedBitstreamFragment *access_unit, diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c index b70aa3c39d..3911cfa100 100644 --- a/libavcodec/cbs_h2645.c +++ b/libavcodec/cbs_h2645.c @@ -1313,7 +1313,7 @@ static void cbs_h265_close(CodedBitstreamContext *ctx) av_buffer_unref(&h265->pps_ref[i]); } -static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload) +void ff_cbs_h264_free_sei_payload(H264RawSEIPayload *payload) { switch (payload->payload_type) { case H264_SEI_TYPE_BUFFERING_PERIOD: @@ -1341,7 +1341,7 @@ static void cbs_h264_free_sei(void *opaque, uint8_t *content) H264RawSEI *sei = (H264RawSEI*)content; int i; for (i = 0; i < sei->payload_count; i++) - cbs_h264_free_sei_payload(&sei->payload[i]); + ff_cbs_h264_free_sei_payload(&sei->payload[i]); av_freep(&content); } @@ -1485,95 +1485,3 @@ const CodedBitstreamType ff_cbs_type_h265 = { .close = &cbs_h265_close, }; - -int ff_cbs_h264_add_sei_message(CodedBitstreamContext *ctx, - CodedBitstreamFragment *au, - H264RawSEIPayload *payload) -{ - H264RawSEI *sei = NULL; - int err, i; - - // Find an existing SEI NAL unit to add to. - for (i = 0; i < au->nb_units; i++) { - if (au->units[i].type == H264_NAL_SEI) { - sei = au->units[i].content; - if (sei->payload_count < H264_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 = H264_NAL_SEI; - sei->nal_unit_header.nal_ref_idc = 0; - - sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei), - &cbs_h264_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 == H264_NAL_SLICE || - au->units[i].type == H264_NAL_IDR_SLICE) - break; - } - - err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI, - 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_h264_free_sei_payload(payload); - return err; -} - -void ff_cbs_h264_delete_sei_message(CodedBitstreamContext *ctx, - CodedBitstreamFragment *au, - CodedBitstreamUnit *nal, - int position) -{ - H264RawSEI *sei = nal->content; - - av_assert0(nal->type == H264_NAL_SEI); - 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_h264_free_sei_payload(&sei->payload[position]); - - --sei->payload_count; - memmove(sei->payload + position, - sei->payload + position + 1, - (sei->payload_count - position) * sizeof(*sei->payload)); - } -}