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) From patchwork Thu Jul 22 13:44:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 29016 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5d:965a:0:0:0:0:0 with SMTP id d26csp545735ios; Thu, 22 Jul 2021 07:06:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzkuG3knUU7gzECXqrPGICS2px5rce1g2cOr3sd1TdYtIJS9mr5C1G7exKw8x3VVE8aAeip X-Received: by 2002:a17:906:e257:: with SMTP id gq23mr39740ejb.545.1626962804240; Thu, 22 Jul 2021 07:06:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626962804; cv=none; d=google.com; s=arc-20160816; b=ICzEinmxI0MfNIzOVPDrlNh/oHLyp8VlfhBGCzTPpDphIyOOQE6at0w7U9Ot2aJt9S Ybj/oOZyWFNYAEJxNnlrzC9x86fQuARrgck1uFf+bqY8eMUk/TXH5uztEoV+4C6FperI z8MSNsOaUsmQ7SlFOg4y/fp5ECRIJa4pssrs6P6ifYCr5KVYU+HHUrnXglEVmB2Drimc z7OK0BVA5w32/DVs8vrOnHvm75xh/3rZKXvgym30IIgd+csY319c37VGsjsLNShogAPG XSDwgYg2VTFEJ/zxHEVIg7h4s+1cDeduj367GMRUqXWyvOPoI42dJZQkLsfjHQpNfeB/ AQNg== 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:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=d/8k64LaTB8m2ACQIOWR3cafv60nRM/2iM9Aq+4JxRE=; b=odTHnlDBgUyolDUz3PWrywjVVupD139zsq6HHlk1BYagM13DAuz7NpSY6b4ErLZpjM 3SgrjTmDLm9mQu1ACEhkQu3GVDy62L/gVFo0Ztr83cYQ6SOgEkXDZ6vvxGfEc7G3parl VeEvTKXWmDzfZxmu+mvvACJJkWBbIjUvNYmR+4I/qM1KGHusEaf7k4YMBtCqB1VHOjpU irWCK7U4bFI7FdSOrFA85huPUz01A8e1cR3OB7DEk0zfIO+lELSycDWlzlQb1fgo5C3K qVXRWJR7byy/WmtRxdgqKwT4Frw0YpIBZ6DYWemVeA9XoEjUO2HcLuxRz4SCCM45NUu+ LDjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=Tq8aahCD; 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 jy22si32689346ejc.515.2021.07.22.07.06.43; Thu, 22 Jul 2021 07:06:44 -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=Tq8aahCD; 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 54BEC68A473; Thu, 22 Jul 2021 17:06:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 77AA668A866 for ; Thu, 22 Jul 2021 17:06:33 +0300 (EEST) Received: by mail-pl1-f182.google.com with SMTP id e14so4539451plh.8 for ; Thu, 22 Jul 2021 07:06:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=LRyY2mVeCaUQe7ZUVcuJNcPWMFMT4wn9xHaA8oCxvPc=; b=Tq8aahCDfe7mJ4NSHy7zi6qpumlLI7A8Vfxo9Mlwi7oR4SDzsIYtbibAwGiHRlXSjv mAkwXQ/pphHaE0IlZ3dyuOdKDmozMaku4Km1gGN5F1pIxQFD5T/tvs6pUmIUEkF0Kydl OG8a0/JlB5QeQ80s6vPTD0IHpQpCQIHHS2iPOtZW51pw88jlegaUhbqWKtvSftm17PDd TyvZ2pVJNlWjfk+vOeouguNcLyWWASSSbikNxnIIbfxY0+zbCh6H8Qq5rNlninw1U/dy OE1Rua1o1JABqp6DVH5gsvLEcuXc3J0qsMS35c2Yn2FrVjJo6wtoOblY614ckf49Dhfb fj0w== 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=LRyY2mVeCaUQe7ZUVcuJNcPWMFMT4wn9xHaA8oCxvPc=; b=N5kuDbvCocOnFV+yNKKlQqhEKQDUZziaiZVGLWbQi6w07CTTS/V5aeqIvzXlCenJC0 BlA1BuaxvqDjipiUOAD113cVZIAi2bVXnbkDU6Ic4jAlWi2kZyFJu1xs0Zt3QRwAuXGk mSGCzkkuJDowT5x9+3+UlFpFwr25fzlkiAgVrv/WitJrHhCtB7c5O55p8opmxqTXZ78/ 4qKt+3L//GnUoYogHblj7nz2k6qkY2oDO+8gERU3tShNsGn3KIn+kOp4m0assIxwfcQE Qnpw2IcucA4oJ6g47ydNTVjCoxIa5EWoxwMyAOQW97a+dt4nRGzUjExu3pVBcrPQcCZd FSbA== X-Gm-Message-State: AOAM531qBBouSVZ+YyeOSlEh3yyzKNB316B/ttnnib0J3GHZ6drZxl40 zw5aoalxTZDtDL+2vt1Rz7KP1RD/8/k= X-Received: by 2002:a62:1a4b:0:b029:328:da0b:d83b with SMTP id a72-20020a621a4b0000b0290328da0bd83bmr41349219pfa.59.1626961539892; Thu, 22 Jul 2021 06:45:39 -0700 (PDT) Received: from localhost.localdomain ([191.83.217.217]) by smtp.gmail.com with ESMTPSA id b2sm33870136pgh.9.2021.07.22.06.45.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jul 2021 06:45:39 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Thu, 22 Jul 2021 10:44:54 -0300 Message-Id: <20210722134455.192-2-jamrial@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210722134455.192-1-jamrial@gmail.com> References: <20210722134455.192-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] avutil/film_grain_params: add support for H.274 Film Grain Characteristics 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: 1Fx+SgmPYL8V Used by codecs like H.264, HEVC, and VVC. Signed-off-by: James Almer --- Missing version bump and APIchanges entry. libavutil/film_grain_params.h | 89 +++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/libavutil/film_grain_params.h b/libavutil/film_grain_params.h index 7629e3a041..7350dfc5b8 100644 --- a/libavutil/film_grain_params.h +++ b/libavutil/film_grain_params.h @@ -28,6 +28,11 @@ enum AVFilmGrainParamsType { * The union is valid when interpreted as AVFilmGrainAOMParams (codec.aom) */ AV_FILM_GRAIN_PARAMS_AV1, + + /** + * The union is valid when interpreted as AVFilmGrainH274Params (codec.h274) + */ + AV_FILM_GRAIN_PARAMS_H274, }; /** @@ -117,6 +122,89 @@ typedef struct AVFilmGrainAOMParams { int limit_output_range; } AVFilmGrainAOMParams; +/** + * This structure describes how to handle film grain synthesis for codecs using + * the ITU-T H.274 Versatile suplemental enhancement information message. + * + * @note The struct must be allocated as part of AVFilmGrainParams using + * av_film_grain_params_alloc(). Its size is not a part of the public ABI. + */ +typedef struct AVFilmGrainH274Params { + /** + * Specifies the film grain simulation mode. + * 0 = Frequency filtering, 1 = Auto-regression + */ + int model_id; + + /** + * Specifies the bit depth used for the luma component. + */ + int bit_depth_luma; + + /** + * Specifies the bit depth used for the chroma components. + */ + int bit_depth_chroma; + + enum AVColorRange color_range; + enum AVColorPrimaries color_primaries; + enum AVColorTransferCharacteristic color_trc; + enum AVColorSpace color_space; + + /** + * Specifies the blending mode used to blend the simulated film grain + * with the decoded images. + * + * 0 = Additive, 1 = Multiplicative + */ + int blending_mode_id; + + /** + * Specifies a scale factor used in the film grain characterization equations. + */ + int log2_scale_factor; + + /** + * Indicates if the modelling of film grain for a given component is present. + */ + int component_model_present[3 /* y, cb, cr */]; + + /** + * Specifies the number of intensity intervals for which a specific set of + * model values has been estimated, with a range of [1, 256]. + */ + uint16_t num_intensity_intervals[3 /* y, cb, cr */]; + + /** + * Specifies the number of model values present for each intensity interval + * in which the film grain has been modelled, with a range of [1, 6]. + */ + uint8_t num_model_values[3 /* y, cb, cr */]; + + /** + * Specifies the lower ounds of each intensity interval for whichthe set of + * model values applies for the component. + */ + uint8_t intensity_interval_lower_bound[3 /* y, cb, cr */][256 /* intensity interval */]; + + /** + * Specifies the upper bound of each intensity interval for which the set of + * model values applies for the component. + */ + uint8_t intensity_interval_upper_bound[3 /* y, cb, cr */][256 /* intensity interval */]; + + /** + * Specifies the model values for the component for each intensity interval. + * - When model_id == 0, the following applies: + * For comp_model_value[y], the range of values is [0, 2^bit_depth_luma - 1] + * For comp_model_value[cb..cr], the range of values is [0, 2^bit_depth_chroma - 1] + * - Otherwise, the following applies: + * For comp_model_value[y], the range of values is [-2^(bit_depth_luma - 1), 2^(bit_depth_luma - 1) - 1] + * For comp_model_value[cb..cr], the range of values is [-2^(bit_depth_chroma - 1), 2^(bit_depth_chroma - 1) - 1] + */ + int16_t comp_model_value[3 /* y, cb, cr */][256 /* intensity interval */][6 /* model value */]; +} AVFilmGrainH274Params; + /** * This structure describes how to handle film grain synthesis in video * for specific codecs. Must be present on every frame where film grain is @@ -143,6 +231,7 @@ typedef struct AVFilmGrainParams { */ union { AVFilmGrainAOMParams aom; + AVFilmGrainH274Params h274; } codec; } AVFilmGrainParams; From patchwork Thu Jul 22 13:44:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 29015 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5d:965a:0:0:0:0:0 with SMTP id d26csp536126ios; Thu, 22 Jul 2021 06:54:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyXMLMAp75q6phk+FlcAHqrdK9q8qvxvIWAz6ard7ZShzvtteMHv1iUOBDqf+rsXua6nOlg X-Received: by 2002:a17:906:24c3:: with SMTP id f3mr10829ejb.145.1626962055996; Thu, 22 Jul 2021 06:54:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626962055; cv=none; d=google.com; s=arc-20160816; b=rrLBqkYI3hLSJNfhaCyo8cWgRY6NnbquuPLiNhd9zBrsoHLN2+/ojIzouTlrcoYPnj 8q0nNuTGJ+50wu84evXqfnkIbGzS2BuHU7ZH05EYcg30Z7pGDZnuUCtOXsHNUjQDZHTG ZB9VbXVWOU978ADc3DSQiGQzxakbNGkVyvxz0QlV4UYBxvikhmeQfCPhEXb2QOsVYmlI 4YWgLxbOwsZjk1OSJTblcPaUM5EzBTjXYGfXPBsnfuKxY9FVcF3/ALkjftGDpraFghgq ia258uL5MFY2TS90hOwtD7jpUFGYGIsrApD5AXzGrcY21OXMwM386+F/cx/p1wi7Qi9A zGNw== 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:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=qjeSlv8D/MwNuMXKO6ON82wObf4imEk1W7wy0LhNE9U=; b=vRCDMc3o2E7j5S25XXCI5J9qcUb7txPcOxELliaO6YxDnIlOo5LF3tuu2hzzYxy1yu 86+X/Q89k2MvZS+B2SWyLWg5HTJ1ZgC7W6uKiEiZQPzcmPOH8KwCFngzvcGlX2URSEd+ oUcaNxaAY/hvLE3C9/LAqZFiBjH7GjxS6b5olNSJVSUE+lfRwk/qmsKJJYdpin3G4RMw 7pLiw8KNQgpuU7jdXIb+4bcGRsDBKWDKvqcMPXOzDY+DfODXuYqZ0jNYYU3fteg45gr8 YoTsxVBPCdDl3vSDgNXr7MRxnUs2QyVZSN6yJk+irpftdvfuLK0BiNFRFQ1rkuGAh0sH KJWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=g0Qnt++j; 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 x41si20323912edy.592.2021.07.22.06.54.13; Thu, 22 Jul 2021 06:54: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=g0Qnt++j; 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 1859568A808; Thu, 22 Jul 2021 16:54:10 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-vs1-f51.google.com (mail-vs1-f51.google.com [209.85.217.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2A11A689DD9 for ; Thu, 22 Jul 2021 16:54:03 +0300 (EEST) Received: by mail-vs1-f51.google.com with SMTP id r25so3354161vsk.2 for ; Thu, 22 Jul 2021 06:54:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=+btBDUsgTfqYKNRcw5kd0zkcmFr8x4XrUZQGm34sIwI=; b=g0Qnt++jfdzOAJo3D0s9grmtCyVtqkdKJ/rdwLvI0DW9KGXMpLR/MvIZCzzhB7mtAH 7pJbdOVzVYuMokA78zlyjfH/hKUWKgod0Xsj9rl7c4LwOxF7IypaBIm0X8tGH3FFoWOx kKzNCDv+u0jeC4Fgm+ip0CJBUbZSUHoIu8/wE+f19mCYVdtuzwbrV9ceHx/adgTm/aCx LN/X4HbxIHPTaDKK0om7vcSdM6B8PF6ZQvNsLJ98D1WIUqUjHeo6BIyagS0wpN3fM6Vf ZE9zNAFlM3vWxt7k/gGF1t44RYU6WYmZ9LlAo2vhbXiA7rkcW7bx05d71TCIQH+H67/V mGSA== 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=+btBDUsgTfqYKNRcw5kd0zkcmFr8x4XrUZQGm34sIwI=; b=VLlZsqPdxcafAU1QGBGswGyWGEqhNbrC2910sqdcCMFhupQdHgClHIy/vLGGa9OaEs 8MxgQMd8I+xO2b/PJ2pzBNYvu68zJLwE0eW1zW7Fry9WINXf9QA9yB1SUmp9MXpVJHxM og90/pRsXm7WnnG0LpqgtHI66xVr5qiIQt+QA4fl8ldjbKl7BnTlZygkIL0So+07DVkz srpPlhStQfMwyukTayz+WCDB5aURtQT1kITV+SjI2lMQHyjwTNPPg3WyAhC/4ypuOXAv xtjrm4EXqzc+ir2dmZXJ5rTzuaKdcQI/KI9baHN1G2IzKUSG02BN5X0nnls4oAyZm9G9 V51A== X-Gm-Message-State: AOAM5331+s8YnTZRWWyAJ2h2bZhpvX1B4fBZYi5xmEngI4EE7JXyBbBZ QYtVSr1eXTZNWY0A0QLTAMQCKadst3A= X-Received: by 2002:a05:6a00:2306:b029:331:ebe8:a4b4 with SMTP id h6-20020a056a002306b0290331ebe8a4b4mr40947586pfh.15.1626961541191; Thu, 22 Jul 2021 06:45:41 -0700 (PDT) Received: from localhost.localdomain ([191.83.217.217]) by smtp.gmail.com with ESMTPSA id b2sm33870136pgh.9.2021.07.22.06.45.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jul 2021 06:45:40 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Thu, 22 Jul 2021 10:44:55 -0300 Message-Id: <20210722134455.192-3-jamrial@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210722134455.192-1-jamrial@gmail.com> References: <20210722134455.192-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/3] avcodec/h264_sei: parse and export 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: Q4P8Qt+61hdZ Signed-off-by: James Almer --- libavcodec/h264_sei.c | 47 +++++++++++++++++++++++++++++++++++ libavcodec/h264_sei.h | 22 +++++++++++++++++ libavcodec/h264_slice.c | 54 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) diff --git a/libavcodec/h264_sei.c b/libavcodec/h264_sei.c index ca2ca59e1f..ad7c27f9f6 100644 --- a/libavcodec/h264_sei.c +++ b/libavcodec/h264_sei.c @@ -49,6 +49,7 @@ void ff_h264_sei_uninit(H264SEIContext *h) h->picture_timing.present = 0; h->buffering_period.present = 0; h->frame_packing.present = 0; + h->film_grain_characteristics.present = 0; h->display_orientation.present = 0; h->afd.present = 0; @@ -410,6 +411,49 @@ static int decode_alternative_transfer(H264SEIAlternativeTransfer *h, return 0; } +static int decode_film_grain_characteristics(H264SEIFilmGrainCharacteristics *h, + GetBitContext *gb) +{ + int film_grain_characteristics_cancel_flag = get_bits1(gb); + + if (!film_grain_characteristics_cancel_flag) { + memset(h, 0, sizeof(*h)); + h->model_id = get_bits(gb, 8); + h->separate_colour_description_present_flag = get_bits1(gb); + if (h->separate_colour_description_present_flag) { + h->bit_depth_luma = get_bits(gb, 3) + 8; + h->bit_depth_chroma = get_bits(gb, 3) + 8; + h->full_range = get_bits1(gb); + h->color_primaries = get_bits(gb, 8); + h->transfer_characteristics = get_bits(gb, 8); + h->matrix_coeffs = get_bits(gb, 8); + } + h->blending_mode_id = get_bits(gb, 2); + h->log2_scale_factor = get_bits(gb, 4); + for (int c = 0; c < 3; c++) + h->comp_model_present_flag[c] = get_bits1(gb); + for (int c = 0; c < 3; c++) { + if (h->comp_model_present_flag[c]) { + h->num_intensity_intervals[c] = get_bits(gb, 8) + 1; + h->num_model_values[c] = get_bits(gb, 3) + 1; + if (h->num_model_values[c] > 6) + return AVERROR_INVALIDDATA; + for (int i = 0; i < h->num_intensity_intervals[c]; i++) { + h->intensity_interval_lower_bound[c][i] = get_bits(gb, 8); + h->intensity_interval_upper_bound[c][i] = get_bits(gb, 8); + for (int j = 0; j < h->num_model_values[c]; j++) + h->comp_model_value[c][i][j] = get_se_golomb_long(gb); + } + } + } + h->repetition_period = get_ue_golomb_long(gb); + + h->present = 1; + } + + return 0; +} + int ff_h264_sei_decode(H264SEIContext *h, GetBitContext *gb, const H264ParamSets *ps, void *logctx) { @@ -471,6 +515,9 @@ int ff_h264_sei_decode(H264SEIContext *h, GetBitContext *gb, case SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS: ret = decode_alternative_transfer(&h->alternative_transfer, &gb_payload); break; + case SEI_TYPE_FILM_GRAIN_CHARACTERISTICS: + ret = decode_film_grain_characteristics(&h->film_grain_characteristics, &gb_payload); + break; default: av_log(logctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type); } diff --git a/libavcodec/h264_sei.h b/libavcodec/h264_sei.h index 14cc559361..f9166b45df 100644 --- a/libavcodec/h264_sei.h +++ b/libavcodec/h264_sei.h @@ -165,6 +165,27 @@ typedef struct H264SEIAlternativeTransfer { int preferred_transfer_characteristics; } H264SEIAlternativeTransfer; +typedef struct H264SEIFilmGrainCharacteristics { + int present; + int model_id; + int separate_colour_description_present_flag; + int bit_depth_luma; + int bit_depth_chroma; + int full_range; + int color_primaries; + int transfer_characteristics; + int matrix_coeffs; + int blending_mode_id; + int log2_scale_factor; + int comp_model_present_flag[3]; + uint16_t num_intensity_intervals[3]; + uint8_t num_model_values[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]; + int repetition_period; +} H264SEIFilmGrainCharacteristics; + typedef struct H264SEIContext { H264SEIPictureTiming picture_timing; H264SEIAFD afd; @@ -176,6 +197,7 @@ typedef struct H264SEIContext { H264SEIDisplayOrientation display_orientation; H264SEIGreenMetaData green_metadata; H264SEIAlternativeTransfer alternative_transfer; + H264SEIFilmGrainCharacteristics film_grain_characteristics; } H264SEIContext; struct H264ParamSets; diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c index 9c2301d7e0..0b0b779409 100644 --- a/libavcodec/h264_slice.c +++ b/libavcodec/h264_slice.c @@ -28,6 +28,7 @@ #include "libavutil/avassert.h" #include "libavutil/display.h" #include "libavutil/imgutils.h" +#include "libavutil/film_grain_params.h" #include "libavutil/stereo3d.h" #include "internal.h" #include "cabac.h" @@ -1330,6 +1331,59 @@ static int h264_export_frame_props(H264Context *h) } h->sei.unregistered.nb_buf_ref = 0; + if (h->sei.film_grain_characteristics.present && + (h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN)) { + H264SEIFilmGrainCharacteristics *fgc = &h->sei.film_grain_characteristics; + AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(out); + if (!fgp) + return AVERROR(ENOMEM); + + fgp->type = AV_FILM_GRAIN_PARAMS_H274; + + fgp->codec.h274.model_id = fgc->model_id; + if (fgc->separate_colour_description_present_flag) { + fgp->codec.h274.bit_depth_luma = fgc->bit_depth_luma; + fgp->codec.h274.bit_depth_chroma = fgc->bit_depth_chroma; + fgp->codec.h274.color_range = fgc->full_range + 1; + fgp->codec.h274.color_primaries = fgc->color_primaries; + fgp->codec.h274.color_trc = fgc->transfer_characteristics; + fgp->codec.h274.color_space = fgc->matrix_coeffs; + } else { + fgp->codec.h274.bit_depth_luma = sps->bit_depth_luma; + fgp->codec.h274.bit_depth_chroma = sps->bit_depth_chroma; + if (sps->video_signal_type_present_flag) + fgp->codec.h274.color_range = sps->full_range + 1; + else + fgp->codec.h274.color_range = AVCOL_RANGE_UNSPECIFIED; + if (sps->colour_description_present_flag) { + fgp->codec.h274.color_primaries = sps->color_primaries; + fgp->codec.h274.color_trc = sps->color_trc; + fgp->codec.h274.color_space = sps->colorspace; + } else { + fgp->codec.h274.color_primaries = AVCOL_PRI_UNSPECIFIED; + fgp->codec.h274.color_trc = AVCOL_TRC_UNSPECIFIED; + fgp->codec.h274.color_space = AVCOL_SPC_UNSPECIFIED; + } + } + fgp->codec.h274.blending_mode_id = fgc->blending_mode_id; + fgp->codec.h274.log2_scale_factor = fgc->log2_scale_factor; + + memcpy(&fgp->codec.h274.component_model_present, &fgc->comp_model_present_flag, + sizeof(fgp->codec.h274.component_model_present)); + memcpy(&fgp->codec.h274.num_intensity_intervals, &fgc->num_intensity_intervals, + sizeof(fgp->codec.h274.num_intensity_intervals)); + memcpy(&fgp->codec.h274.num_model_values, &fgc->num_model_values, + sizeof(fgp->codec.h274.num_model_values)); + memcpy(&fgp->codec.h274.intensity_interval_lower_bound, &fgc->intensity_interval_lower_bound, + sizeof(fgp->codec.h274.intensity_interval_lower_bound)); + memcpy(&fgp->codec.h274.intensity_interval_upper_bound, &fgc->intensity_interval_upper_bound, + sizeof(fgp->codec.h274.intensity_interval_upper_bound)); + memcpy(&fgp->codec.h274.comp_model_value, &fgc->comp_model_value, + sizeof(fgp->codec.h274.comp_model_value)); + + fgc->present = !!fgc->repetition_period; + } + if (h->sei.picture_timing.timecode_cnt > 0) { uint32_t *tc_sd; char tcbuf[AV_TIMECODE_STR_SIZE];