From patchwork Thu Jul 22 13:44:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 29017 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5d:965a:0:0:0:0:0 with SMTP id d26csp551285ios; Thu, 22 Jul 2021 07:13:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFJ3cbTWmGIPB1Aq6m4OSUv/JAQUYf8xdt1/Pa/fm1aPue30PKDZrmFUtu49KWmdutcT42 X-Received: by 2002:a17:907:76b8:: with SMTP id jw24mr79292ejc.375.1626963195479; Thu, 22 Jul 2021 07:13:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626963195; cv=none; d=google.com; s=arc-20160816; b=hCIedY8XX2cOwWmdAt4q5MGXV8yNBYEjiWWGxsP/YaZgUgDZnQ1mi8CwKXqf1qqtE9 U8HaJx7QFbtt/Q3ydzSdxorsoj+iQLq2Lng9tVc4CX+egCaZwylDd8+qheSV4klIdEDt VjhtnZS7Cn5q31Pm3miLZQSXiKnPAJ7VeLvWrIl0SPH25OrMtvklnJaiE5ibV5MI3xo3 2aV3PRSi/OxCWPCEfwgAzEQYiUpHorp6w+KtZBj1/BfIz/r0Mo9YqnN+3St9MlpcmrR1 dbMrigjew1eux+QCx+Fl56jieAGCS/69aTsC+c25Fyh9cLGF87yKg4ZDbjOIAoBUQsdK 7wAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=V3g7mVkEHDEgppSPHkBYOQDgqQ9Z5nibb5dR/SvVrMg=; b=r/Qfjh+4+N95ynn5kvxy66LiF9SNfxW3hes0EyyrkrqDIlVjIKe0yAn+5VDxW4s2ze lPYNw2n0X2tpOr5nIZkFoegIyR9GieTg/sy2lk1ecXgM05WlaLCuroN5+WaV2BMF2q5a TkkIXGVO40anYxGlqAlehDdzTjL79AgmWl2Bus80h/J9sPwOOV9la0MtC5T59sfywexY SMwCuAyPpTWrZAJIKwLgbhsF/3FWGWoFAKINkMHMuN4W/cVGXKOhtO84GCsS574FNGfu coG/My6UKXiAbQpOXB9agTJsWSbumSPb/iBCVv0nGO6cpPcp/bE9QcnUc/6fa+Q9lGdN wI+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b="N/hEszhy"; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id mj21si6146985ejb.25.2021.07.22.07.13.15; Thu, 22 Jul 2021 07:13:15 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b="N/hEszhy"; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EA73868AAAB; Thu, 22 Jul 2021 17:13:11 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E793268A467 for ; Thu, 22 Jul 2021 17:13:04 +0300 (EEST) Received: by mail-pj1-f49.google.com with SMTP id h6-20020a17090a6486b029017613554465so5709297pjj.4 for ; Thu, 22 Jul 2021 07:13:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=SL8hOQjtv3Npq46BHykLxpRK30u030odpG5Fcs3cVIQ=; b=N/hEszhyKp7sK60nB0bbo79///PoJRYNW4j4IC4RR07b8h2jIVwOFWtRVgHyaTwiEF TCw4IcWQSC3P2WFSwZj5lu1ZtUMi8iqM4IyMwvVk5bgNGuUOB0pLd86TkPFzB/S8n9jq 7apGm+S4cbM/6vtKjeMYx6Yls0FIlnggwI911YLbJOyNtQIl6bmk1LPA4n5Qe5jI+6xX b3Br3oIy7L6c+GyBoeXc7wV+JK8nkdzdm4f5TWC5yWJjA4BjF0NSoBMqMhwqhPv4Boaa yYS0GiYkYBW+R1Ve1YIKMKfDEueyOASrQJtIEuztkHnZ5Qrco6On6/MXaedoFAIocG8W oiLQ== 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:mime-version :content-transfer-encoding; bh=SL8hOQjtv3Npq46BHykLxpRK30u030odpG5Fcs3cVIQ=; b=UfTV8wU6j4SISYEbqK+xlnM6DwN/Q9lMhpIgi106w0IIXFK/or1EtmUqFe5D4vbDOQ 38YvuqSRJwECwak4x8o59I1Nosr2eis6eOnbxrTeS08ezXzd2Eu4Ab4ezityLXHepuxJ 5gfnfgU6eZwmzHeoqt4SrwfYq5pxHcFcOBPt4mk83xBZfhnKPE/PqAiQvySqIkB4x+4L HJ4eJ8hiO9UTB9MMPPKXBJIJrXEtVvRfYb3W6E1pJY8Srp47URlUA0n/O8RSE5BP3a14 Ia8JiuZye8Ra5c2FaYrvmqVfuGKcL3o6CAvYcujo5rIhn0ftUs7rac5AXXinj5iRHA4I S6hA== X-Gm-Message-State: AOAM530bfypSxDXTRR4x7P9apARB3CF1BpNmNyzvds+w1gCJt5+pMBtS KLinLOH+4vXwSi688x9shjaZhsElX7Y= X-Received: by 2002:a65:62da:: with SMTP id m26mr74403pgv.370.1626961538563; Thu, 22 Jul 2021 06:45:38 -0700 (PDT) Received: from localhost.localdomain ([191.83.217.217]) by smtp.gmail.com with ESMTPSA id b2sm33870136pgh.9.2021.07.22.06.45.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jul 2021 06:45:38 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Thu, 22 Jul 2021 10:44:53 -0300 Message-Id: <20210722134455.192-1-jamrial@gmail.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3] cbs_h264: add support for Film Grain Characteristics SEI messages X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: QNjJJDlDguWx Signed-off-by: James Almer --- libavcodec/cbs_h264.h | 21 +++++++ libavcodec/cbs_h2645.c | 6 ++ libavcodec/cbs_h264_syntax_template.c | 83 +++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) diff --git a/libavcodec/cbs_h264.h b/libavcodec/cbs_h264.h index 5a8641a333..ca9b688c05 100644 --- a/libavcodec/cbs_h264.h +++ b/libavcodec/cbs_h264.h @@ -272,6 +272,27 @@ typedef struct H264RawSEIRecoveryPoint { uint8_t changing_slice_group_idc; } H264RawSEIRecoveryPoint; +typedef struct H264RawFilmGrainCharacteristics { + uint8_t film_grain_characteristics_cancel_flag; + uint8_t film_grain_model_id; + uint8_t separate_colour_description_present_flag; + uint8_t film_grain_bit_depth_luma_minus8; + uint8_t film_grain_bit_depth_chroma_minus8; + uint8_t film_grain_full_range_flag; + uint8_t film_grain_colour_primaries; + uint8_t film_grain_transfer_characteristics; + uint8_t film_grain_matrix_coefficients; + uint8_t blending_mode_id; + uint8_t log2_scale_factor; + uint8_t comp_model_present_flag[3]; + uint8_t num_intensity_intervals_minus1[3]; + uint8_t num_model_values_minus1[3]; + uint8_t intensity_interval_lower_bound[3][256]; + uint8_t intensity_interval_upper_bound[3][256]; + int16_t comp_model_value[3][256][6]; + uint8_t film_grain_characteristics_repetition_period; +} H264RawFilmGrainCharacteristics; + typedef struct H264RawSEIDisplayOrientation { uint8_t display_orientation_cancel_flag; uint8_t hor_flip; diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c index 6005d46e0d..61c1e13cf7 100644 --- a/libavcodec/cbs_h2645.c +++ b/libavcodec/cbs_h2645.c @@ -1570,6 +1570,12 @@ static const SEIMessageTypeDescriptor cbs_sei_h264_types[] = { sizeof(H264RawSEIRecoveryPoint), SEI_MESSAGE_RW(h264, sei_recovery_point), }, + { + SEI_TYPE_FILM_GRAIN_CHARACTERISTICS, + 1, 0, + sizeof(H264RawFilmGrainCharacteristics), + SEI_MESSAGE_RW(h264, film_grain_characteristics), + }, { SEI_TYPE_DISPLAY_ORIENTATION, 1, 0, diff --git a/libavcodec/cbs_h264_syntax_template.c b/libavcodec/cbs_h264_syntax_template.c index 9587f33985..0f8bba4a0d 100644 --- a/libavcodec/cbs_h264_syntax_template.c +++ b/libavcodec/cbs_h264_syntax_template.c @@ -719,6 +719,89 @@ static int FUNC(sei_recovery_point)(CodedBitstreamContext *ctx, RWContext *rw, return 0; } +static int FUNC(film_grain_characteristics)(CodedBitstreamContext *ctx, RWContext *rw, + H264RawFilmGrainCharacteristics *current, + SEIMessageState *state) +{ + CodedBitstreamH264Context *h264 = ctx->priv_data; + const H264RawSPS *sps; + int err, c, i, j; + + HEADER("Film Grain Characteristics"); + + sps = h264->active_sps; + if (!sps) { + // If there is exactly one possible SPS but it is not yet active + // then just assume that it should be the active one. + int i, k = -1; + for (i = 0; i < H264_MAX_SPS_COUNT; i++) { + if (h264->sps[i]) { + if (k >= 0) { + k = -1; + break; + } + k = i; + } + } + if (k >= 0) + sps = h264->sps[k]; + } + + flag(film_grain_characteristics_cancel_flag); + if (!current->film_grain_characteristics_cancel_flag) { + int filmGrainBitDepth[3]; + + u(2, film_grain_model_id, 0, 1); + flag(separate_colour_description_present_flag); + if (current->separate_colour_description_present_flag) { + ub(3, film_grain_bit_depth_luma_minus8); + ub(3, film_grain_bit_depth_chroma_minus8); + flag(film_grain_full_range_flag); + ub(8, film_grain_colour_primaries); + ub(8, film_grain_transfer_characteristics); + ub(8, film_grain_matrix_coefficients); + } else { + if (!sps) { + av_log(ctx->log_ctx, AV_LOG_ERROR, + "No active SPS for film_grain_characteristics.\n"); + return AVERROR_INVALIDDATA; + } + infer(film_grain_bit_depth_luma_minus8, sps->bit_depth_luma_minus8); + infer(film_grain_bit_depth_chroma_minus8, sps->bit_depth_chroma_minus8); + infer(film_grain_full_range_flag, sps->vui.video_full_range_flag); + infer(film_grain_colour_primaries, sps->vui.colour_primaries); + infer(film_grain_transfer_characteristics, sps->vui.transfer_characteristics); + infer(film_grain_matrix_coefficients, sps->vui.matrix_coefficients); + } + + filmGrainBitDepth[0] = current->film_grain_bit_depth_luma_minus8 + 8; + filmGrainBitDepth[1] = + filmGrainBitDepth[2] = current->film_grain_bit_depth_chroma_minus8 + 8; + + u(2, blending_mode_id, 0, 1); + ub(4, log2_scale_factor); + for (c = 0; c < 3; c++) + flags(comp_model_present_flag[c], 1, c); + for (c = 0; c < 3; c++) { + if (current->comp_model_present_flag[c]) { + ubs(8, num_intensity_intervals_minus1[c], 1, c); + us(3, num_model_values_minus1[c], 0, 5, 1, c); + for (i = 0; i <= current->num_intensity_intervals_minus1[c]; i++) { + ubs(8, intensity_interval_lower_bound[c][i], 2, c, i); + ubs(8, intensity_interval_upper_bound[c][i], 2, c, i); + for (j = 0; j <= current->num_model_values_minus1[c]; j++) + ses(comp_model_value[c][i][j], 0 - current->film_grain_model_id * (1 << (filmGrainBitDepth[c] - 1)), + ((1 << filmGrainBitDepth[c]) - 1) - current->film_grain_model_id * (1 << (filmGrainBitDepth[c] - 1)), + 3, c, i, j); + } + } + } + ue(film_grain_characteristics_repetition_period, 0, 16384); + } + + return 0; +} + static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEIDisplayOrientation *current, SEIMessageState *sei)