From patchwork Sat Mar 5 16:58:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 34620 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:d078:0:0:0:0 with SMTP id x24csp49708nkx; Sat, 5 Mar 2022 08:59:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJyz7XUQ/r3lzLYTvrN4x18a1N+B3cEy/XzYqKH/dxHN5SBy6/cqRhcg774XcCdXC5ROavLU X-Received: by 2002:a17:907:608f:b0:6da:8d84:e272 with SMTP id ht15-20020a170907608f00b006da8d84e272mr3391756ejc.559.1646499547328; Sat, 05 Mar 2022 08:59:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646499547; cv=none; d=google.com; s=arc-20160816; b=KMhj7sA4C02u8hwXpG0mwTNliYw9cDQxXWZfzjnPv7R4C6/v7sBMHKoiHl751KyWkP rmg3XStqix/V4VYoXDNW9SmksLD5Sgxh1OAnztjTrpKBdERa/rKlkPqtKuo53j7XVWOV jRN3IrzYQim3DFSmCHvxKVDTrLy9zHUMz4KYZOVtSf/WzjqedmOUWz8EoMMtcRVI9tlb /tb7yDUMBO319BYEIFQjjCRwj9/Wx3WvTPi0EtewnExJTBObRWjxZrDDivTx13uqP3TR BKay2GZy9K8DBVHpzghR4xxBH+IIfCszzyTCsrR5EDHuS8OGG1fXBDET9ujXdCkqNVMn QiuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc: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=1nSUQwbK9RJVpRFpKddqHzGQ9IiI1UwTNBEB5Ly4Xyo=; b=Wn3CKet1AltFc9rjKFTkpsDJsA/bbJTHqLEUfsNN3wLnu53UjJ6vXj3f7UEUer7SBE Jbdda343rxVpMCnfzcaWNjDivfKRysooHejmv5ydq53Cw/K/w5EiKMqHuHwrjZkAc11A rG+10yvmfn6VfBZQ5HJJcPNDuOYmW1+YxDyKDFgFiX8nkWJ5sIvbL7+Ncytj0tw3nKA5 TEYr2GP4r6aORZPtrIqP4uxtj7FNBmRrLbcehizoNSiWL3Nh5BvFYExf3w99YI7cDo3T dEFgJGopf/KI9dbfOGzwUnIGPeOy8aa2xmWXganuTBRPSH9z78SOe3Pd7QP+BvaJ8M/t STlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=ZBv5n7Hy; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z10-20020a05640240ca00b00415bbe0c49bsi5660289edb.149.2022.03.05.08.59.07; Sat, 05 Mar 2022 08:59:07 -0800 (PST) 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=@haasn.xyz header.s=mail header.b=ZBv5n7Hy; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D2FF968B10A; Sat, 5 Mar 2022 18:58:51 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AF04968B0FB for ; Sat, 5 Mar 2022 18:58:43 +0200 (EET) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 6E35B47027; Sat, 5 Mar 2022 17:58:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1646499522; bh=svxFl3uwrYVHtcEMY91onsPmvVZBHgrydG3hLUuFCmA=; h=From:To:Cc:Subject:Date:From; b=ZBv5n7HyhgEihTMi2mjXm86C3vjsPzsx2QUf0IVfgzt2iq/7SnkTDDhZ2cGvU4m8t 9ANp+tK95m7Iun7/WEOkDc0lqUwIKPw25ruvp1yS6EMhUhmEwoxYLrc1tiU3aophcO OMNi4EScbakaD713bi5RRuGfcdxSDK3QNudxf044= From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Sat, 5 Mar 2022 17:58:31 +0100 Message-Id: <20220305165833.18668-1-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3] h274: correctly infer missing comp model values 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 Cc: Niklas Haas Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: RCbuuv9Pqs35 From: Niklas Haas The ITU-R H.274 spec mentions defined behavior for what to do when num_comp_model_values is less than the amount expected by the fg model algorithm. The current behavior is basically undefined behavior in this case. In addition to fixing this, also defer the index offset calculation very slightly to better lead into the following commit. Signed-off-by: Niklas Haas --- libavcodec/h274.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libavcodec/h274.c b/libavcodec/h274.c index a69f941142..170086543f 100644 --- a/libavcodec/h274.c +++ b/libavcodec/h274.c @@ -162,6 +162,7 @@ static av_always_inline void generate(int8_t *out, int out_stride, { const uint8_t shift = h274->log2_scale_factor + 6; const uint16_t avg = avg_8x8_c(in, in_stride); + const uint8_t num_values = h274->num_model_values[c]; int16_t scale; uint8_t h, v; int8_t s = -1; @@ -191,8 +192,8 @@ static av_always_inline void generate(int8_t *out, int out_stride, return; } - h = av_clip(h274->comp_model_value[c][s][1], 2, 14) - 2; - v = av_clip(h274->comp_model_value[c][s][2], 2, 14) - 2; + h = num_values > 1 ? av_clip(h274->comp_model_value[c][s][1], 2, 14) - 2 : 6; + v = num_values > 2 ? av_clip(h274->comp_model_value[c][s][2], 2, 14) - 2 : h; init_slice(database, h, v); scale = h274->comp_model_value[c][s][0]; From patchwork Sat Mar 5 16:58:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 34619 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:d078:0:0:0:0 with SMTP id x24csp49626nkx; Sat, 5 Mar 2022 08:58:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJzI9pj/ZZJEukqwOldu0kzAsj2vpr4FZbnWWGLm4yoBZlXeT+yQSYKrgKANPuEpBfg75RPt X-Received: by 2002:a17:906:5641:b0:6da:8691:3fcc with SMTP id v1-20020a170906564100b006da86913fccmr3235851ejr.50.1646499535841; Sat, 05 Mar 2022 08:58:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646499535; cv=none; d=google.com; s=arc-20160816; b=dKNAYsx9SuFHRIOHqBAFuBNFGFRF0rYa5rxrUKG7qdRPBNgytW+0DDSNR1SuBTrwok Bu/CXDoZ1GiLtNcNW3f1CNKY4FFQ0/q+dDutSp07VHokpS+4RTZEAYMMemas2gRQt1Kz j5221jwf6GdhD/CMVeVOLyJrUT6wkND93UY2NrTlr6SMSEVSXfAVcZjD3rJyw3p5ZGja lAX84jhUtnZRTjAJ2wo5HqH9qAYhLyMTOR9TlZL03n7OsaVbfObiXQYfuGC6Y7JeLUKw phMFvHEVEiFX0ZmYK6vFV5RC5nAnVrff+3WUJQlYmN8952mhtoFlF+lNvbQHY0Pfc9D0 amPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc: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=SIitmT9rGzLitCc26CmpZ2XtItNvmH90OGf58ZqD4xk=; b=Suag/uT+94zyr1R56HMAobqX5SDGa8NT3igY8zK4WxdonZ9FxVl7cr4umZPAOg3Iia T2OIyLRztFsCuiEebcDp3YSQLrF5jMdvjlmsbPtx7rNCAfy7vNm/9dfAIv60sBcK2xql oGzUpiW2zTl7QDFNgQvfiihp6tqiwgLdvAguJjqGfjSOgpnRn5/yjy0jR+nILWlAEf1f hN6vmpnh2MT3YCI/ABjaW5oh0pFHr6AwBcNg1gza7LF0ZmHtzNM6MV5262xodOo+amBW Odc0l9oiluKXFakiCf20reesQyGHt22yMcitwy3uJvni5lt+cW0jEULvaAsuPnUpcQRe DDVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=HNwR6zPl; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id r23-20020a17090638d700b006d0c22e67a3si4828743ejd.798.2022.03.05.08.58.54; Sat, 05 Mar 2022 08:58:55 -0800 (PST) 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=@haasn.xyz header.s=mail header.b=HNwR6zPl; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9C29F68B0F2; Sat, 5 Mar 2022 18:58:50 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id ACEB368A7DD for ; Sat, 5 Mar 2022 18:58:43 +0200 (EET) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 272374702E; Sat, 5 Mar 2022 17:58:43 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1646499523; bh=IIMMUyNq8Id6JQaDR6zchCKQ71RECpFtSZp1VdKc7wg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HNwR6zPl/sq4mwtJcvgZjbJkD2ksVN6p+1P5xXodfbxFgVEL8SruDIUcItq9zrFas x/5ZcL+SC6loWSdgY7BJ9dIWG0mww9lupXTSrp+INVMUT/aw06S4DQzmGZ0WcIT8Zf xKRmbOEpDUhFkT3O7xZ8EIg7sWHTmdqtwvU9RJrQ= From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Sat, 5 Mar 2022 17:58:32 +0100 Message-Id: <20220305165833.18668-2-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220305165833.18668-1-ffmpeg@haasn.xyz> References: <20220305165833.18668-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] h274: avoid copying AVFilmGrainH274Params into the stack frame 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 Cc: Niklas Haas Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: /AG9z79alFtk From: Niklas Haas There's very little reason to make a local copy of this entire ~10 kB struct, only to precompute three minor arithmetic operations. Just move the logic to the per-block function call instead. Signed-off-by: Niklas Haas --- libavcodec/h274.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/libavcodec/h274.c b/libavcodec/h274.c index 170086543f..265bd49ea1 100644 --- a/libavcodec/h274.c +++ b/libavcodec/h274.c @@ -192,14 +192,18 @@ static av_always_inline void generate(int8_t *out, int out_stride, return; } - h = num_values > 1 ? av_clip(h274->comp_model_value[c][s][1], 2, 14) - 2 : 6; - v = num_values > 2 ? av_clip(h274->comp_model_value[c][s][2], 2, 14) - 2 : h; - init_slice(database, h, v); - scale = h274->comp_model_value[c][s][0]; if (invert) scale = -scale; + if (c > 0) + scale >>= 1; // reduce intensity for chroma (as per SMPTE RDD 5-2006) + h = num_values > 1 ? h274->comp_model_value[c][s][1] : 8; + v = num_values > 2 ? h274->comp_model_value[c][s][2] : h; + h = av_clip(h << (c > 0 ? 1 : 0), 2, 14) - 2; + v = av_clip(v << (c > 0 ? 1 : 0), 2, 14) - 2; + + init_slice(database, h, v); synth_grain_8x8_c(out, out_stride, scale, shift, &database->db[h][v][y_offset][x_offset]); @@ -219,9 +223,9 @@ int ff_h274_apply_film_grain(AVFrame *out_frame, const AVFrame *in_frame, H274FilmGrainDatabase *database, const AVFilmGrainParams *params) { - AVFilmGrainH274Params h274 = params->codec.h274; + const AVFilmGrainH274Params *h274 = ¶ms->codec.h274; av_assert1(params->type == AV_FILM_GRAIN_PARAMS_H274); - if (h274.model_id != 0) + if (h274->model_id != 0) return AVERROR_PATCHWELCOME; av_assert1(out_frame->format == in_frame->format); @@ -241,21 +245,12 @@ int ff_h274_apply_film_grain(AVFrame *out_frame, const AVFrame *in_frame, const uint8_t * const in = in_frame->data[c]; const int in_stride = in_frame->linesize[c]; - if (!h274.component_model_present[c]) { + if (!h274->component_model_present[c]) { av_image_copy_plane(out, out_stride, in, in_stride, width * sizeof(uint8_t), height); continue; } - if (c > 0) { - // Adaptation for 4:2:0 chroma subsampling - for (int i = 0; i < h274.num_intensity_intervals[c]; i++) { - h274.comp_model_value[c][i][0] >>= 1; - h274.comp_model_value[c][i][1] *= 2; - h274.comp_model_value[c][i][2] *= 2; - } - } - // Film grain synthesis is done in 8x8 blocks, but the PRNG state is // only advanced in 16x16 blocks, so use a nested loop for (int y = 0; y < height; y += 16) { @@ -271,7 +266,7 @@ int ff_h274_apply_film_grain(AVFrame *out_frame, const AVFrame *in_frame, for (int xx = 0; xx < 16 && x+xx < width; xx += 8) { generate(grain + (y+yy) * grain_stride + (x+xx), grain_stride, in + (y+yy) * in_stride + (x+xx), in_stride, - database, &h274, c, invert, (x+xx) > 0, + database, h274, c, invert, (x+xx) > 0, y_offset + yy, x_offset + xx); } } From patchwork Sat Mar 5 16:58:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 34621 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:d078:0:0:0:0 with SMTP id x24csp49793nkx; Sat, 5 Mar 2022 08:59:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJwTILcEy6oDEnT4hYelvg0Rx/f/ALnA17ap4C9hisdZk71kPTHMl75v7r9frUO1AuvF2zZb X-Received: by 2002:a17:907:9619:b0:6d7:12d6:1e66 with SMTP id gb25-20020a170907961900b006d712d61e66mr3330847ejc.592.1646499557420; Sat, 05 Mar 2022 08:59:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646499557; cv=none; d=google.com; s=arc-20160816; b=p6lT6BbrAmSVPGUa/EQqg0p5ercB62C04WCkCWASyH9gWdtMAq23MiYG+aSi0bx3SH qu+yHFLQT638ouruOGafOxjjpsVn8POqKsqQwF1NUalnYIBYf2x/AajKtXvKsNBUm+nx RIzTtqHwZI+Hbw5jjwOhWdi7TuAzYWCDRnVswzXOiDfhyFUJ4s0Gtmri1WMcm9I+Pv5G cJpCAtfJgC54Xo2IYlk4qS8hVrNvfmlIyCDsPVLB/MaiPSUPgorDg4ybp/lZDGpmHoUJ n+h/PnOB9qO0fphqDkvlXLJKjock1T7GI/bD602+QuWfHqjPMzq4nvxOG8NcDRI7czHq Q6DQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc: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=V4PgFn+u+BGJuXaPfrFSTy+IetLG4O4rYAqOCrVC9J0=; b=G6WI4f84MGx+PicOSRXvQ9QgcKyg4SUhnLVlYc5e0kqQYnexMtbl1XhbW2pc2EU2du aEur4XYNgX088zfBaKF5Dq9ILAlTT0u1TQZi2ukudRGqn8tdGKyrWbezTzphveBuN5Kt 4KHzsHeJx49kblzQMXebNc728pHj7IafpnB85vXaXHpRXSa90LR9Ib9EvmZfcQny+G9U 060/MddsPITiaBJ8jENhD3w/o26oN40kHQ/X5sTXw8MqDpO5AO4nEdkDXFJ2xev3sCoz SINu7lPKSUTfWVWERohiBrTaVBQlNImdBsbp5clEWYW7BxCS7euFhoXuqaRohW5FeqbS OHkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=kuvMBumr; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id v29-20020a17090610dd00b006d08c9350ebsi4719725ejv.640.2022.03.05.08.59.17; Sat, 05 Mar 2022 08:59:17 -0800 (PST) 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=@haasn.xyz header.s=mail header.b=kuvMBumr; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D8B9368B12A; Sat, 5 Mar 2022 18:58:52 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7779D68AF4C for ; Sat, 5 Mar 2022 18:58:44 +0200 (EET) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 08E4D47033; Sat, 5 Mar 2022 17:58:44 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1646499524; bh=eX7ajHkm6WlKM9YUbl1naIvOv76XMEc4mFJT1C4otkA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kuvMBumr4qKMR7kidPMYIqiTogCIVjMH3W4FuAZuXBZFc9HixX0pOdvueLu1TtZVz /Z0JjEbpElHqxr+gD/wkFOjkjtDTCd/gslye+zKwc/3pjHWGEiVzDW0FdGV+fJ3s/R 4xGEoWWDYTyxIqU9DVnvzX78PpbugGX3/0WV2ZDs= From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Sat, 5 Mar 2022 17:58:33 +0100 Message-Id: <20220305165833.18668-3-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220305165833.18668-1-ffmpeg@haasn.xyz> References: <20220305165833.18668-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/3] h274: support a few more pixfmts 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 Cc: Niklas Haas Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: cclTWSSugHyI From: Niklas Haas Based on extrapolating the intent of the SMPTE RDD 5-2006 spec, which only provides the parameter adaptation code for yuv420p. I've deduced that the change to `scale` has nothing to do with the reduction in grain frequency and merely reflects the fact that chroma planes have half the effective value range in typical integer encodings. As such, with this change can support e.g. 4:4:4 yuv420p as well, in which case we simply don't double the chroma grain frequencies. Still no support for 10-bit, but at least this is something. Signed-off-by: Niklas Haas --- libavcodec/h274.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/libavcodec/h274.c b/libavcodec/h274.c index 265bd49ea1..e800351a3e 100644 --- a/libavcodec/h274.c +++ b/libavcodec/h274.c @@ -157,7 +157,8 @@ static av_always_inline void generate(int8_t *out, int out_stride, const uint8_t *in, int in_stride, H274FilmGrainDatabase *database, const AVFilmGrainH274Params *h274, - int c, int invert, int deblock, + int c, uint8_t xs, uint8_t ys, + int invert, int deblock, int y_offset, int x_offset) { const uint8_t shift = h274->log2_scale_factor + 6; @@ -200,8 +201,8 @@ static av_always_inline void generate(int8_t *out, int out_stride, h = num_values > 1 ? h274->comp_model_value[c][s][1] : 8; v = num_values > 2 ? h274->comp_model_value[c][s][2] : h; - h = av_clip(h << (c > 0 ? 1 : 0), 2, 14) - 2; - v = av_clip(v << (c > 0 ? 1 : 0), 2, 14) - 2; + h = av_clip(h << xs, 2, 14) - 2; + v = av_clip(v << ys, 2, 14) - 2; init_slice(database, h, v); synth_grain_8x8_c(out, out_stride, scale, shift, @@ -224,19 +225,34 @@ int ff_h274_apply_film_grain(AVFrame *out_frame, const AVFrame *in_frame, const AVFilmGrainParams *params) { const AVFilmGrainH274Params *h274 = ¶ms->codec.h274; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(out_frame->format); av_assert1(params->type == AV_FILM_GRAIN_PARAMS_H274); if (h274->model_id != 0) return AVERROR_PATCHWELCOME; av_assert1(out_frame->format == in_frame->format); - if (in_frame->format != AV_PIX_FMT_YUV420P) + switch (out_frame->format) { + case AV_PIX_FMT_YUV444P: + case AV_PIX_FMT_YUV440P: + case AV_PIX_FMT_YUV422P: + case AV_PIX_FMT_YUV420P: + case AV_PIX_FMT_YUV411P: + case AV_PIX_FMT_YUV410P: + // 8-bit YCbCr formats with varying subsampling + break; + + default: + // TODO: support higher bit depth formats! return AVERROR_PATCHWELCOME; + } for (int c = 0; c < 3; c++) { static const uint8_t color_offset[3] = { 0, 85, 170 }; uint32_t seed = Seed_LUT[(params->seed + color_offset[c]) % 256]; - const int width = c > 0 ? AV_CEIL_RSHIFT(out_frame->width, 1) : out_frame->width; - const int height = c > 0 ? AV_CEIL_RSHIFT(out_frame->height, 1) : out_frame->height; + const uint8_t xs = c > 0 ? desc->log2_chroma_w : 0; + const uint8_t ys = c > 0 ? desc->log2_chroma_h : 0; + const int width = AV_CEIL_RSHIFT(out_frame->width, xs); + const int height = AV_CEIL_RSHIFT(out_frame->height, ys); uint8_t * const out = out_frame->data[c]; const int out_stride = out_frame->linesize[c]; @@ -266,7 +282,7 @@ int ff_h274_apply_film_grain(AVFrame *out_frame, const AVFrame *in_frame, for (int xx = 0; xx < 16 && x+xx < width; xx += 8) { generate(grain + (y+yy) * grain_stride + (x+xx), grain_stride, in + (y+yy) * in_stride + (x+xx), in_stride, - database, h274, c, invert, (x+xx) > 0, + database, h274, c, xs, ys, invert, (x+xx) > 0, y_offset + yy, x_offset + xx); } }