From patchwork Fri Jul 9 03:32:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fei Wang X-Patchwork-Id: 28876 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5d:965a:0:0:0:0:0 with SMTP id d26csp270416ios; Thu, 8 Jul 2021 20:35:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxh12uYjlLnzCL9h/l3bwHeaZHLPsA0ozabQP1Xf7kjUHyPj9JQDJZShP7P9yKm/uN8Q/Ev X-Received: by 2002:a17:906:8584:: with SMTP id v4mr34592035ejx.301.1625801734994; Thu, 08 Jul 2021 20:35:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625801734; cv=none; d=google.com; s=arc-20160816; b=wnRXTZp2pOahIQUrI6AP1cBSNVSD/8SYmNvYAEcAgvos6ekoaIKPNxo0fn1U47+TUI AHDAyI115w3L0uGPEH966zEktdGCHjw64DkV2T9X+36NbrRN24yOGfx+KP9c+X75UEW3 W6CKR+istkuLNl3TYLi/v6np/FjfDhbwaT0cov5xPTx5AML/XZi1Ide4ZiMutu3USiPO Z/r+BmyGxYpKMPx20qmr+TTIMZOaX0zYuwLn32zfZd2oX/IYexNCtmevGjGT7po//XWZ HzH2KgHFQvcEC0S1Qwco6pJpVSGP5UaOa4qGrouFF/lK6fQ6sD6qPRShGcy28mJkMV5S 6HTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:delivered-to; bh=v4OvrmsgpolDFvMVU5v8hqTohNS8nXvwc8u77KDWV5k=; b=MHKbCfSXesL6FU5f01Tm0TgTaI1rJKzsaaPHyMPNMisVawLqvwz7g6oYYOCuqJFRtc ymS5lGyM0/ILAEIWp7kFp2/NXwaaEFmpm3nSh8aKYRvgE2I4mFJ39d6Ufi8YbD1hjdZ2 kiMAK6mRd2BEIxFlLHfd+Nw54fttrl4oCgs3jn2EmO7YPORp19VeT3iYQiUPmALGMMFJ qla2hOrSSVu8A16CGPRd94tlCmEiRZY4ltRuWXkhXytXtXsce3dbyURM7ZwRzRLTJ0VL aVFRIzMmN0eqD8ZEhUv2v7PLO9rUAZMm9orbwbQUb8M5UzDn823fSg6Yic/aKqMdqaWx +1ig== ARC-Authentication-Results: i=1; mx.google.com; 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=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hd33si5679985ejc.706.2021.07.08.20.35.30; Thu, 08 Jul 2021 20:35:34 -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; 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=NONE dis=NONE) header.from=intel.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 25FED68A50C; Fri, 9 Jul 2021 06:35:07 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2D4BF68A369 for ; Fri, 9 Jul 2021 06:34:58 +0300 (EEST) X-IronPort-AV: E=McAfee;i="6200,9189,10039"; a="196814900" X-IronPort-AV: E=Sophos;i="5.84,225,1620716400"; d="scan'208";a="196814900" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Jul 2021 20:34:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.84,225,1620716400"; d="scan'208";a="461973662" Received: from f.sh.intel.com ([10.239.159.147]) by fmsmga008.fm.intel.com with ESMTP; 08 Jul 2021 20:34:52 -0700 From: Fei Wang To: ffmpeg-devel@ffmpeg.org Date: Fri, 9 Jul 2021 11:32:23 +0800 Message-Id: <20210709033229.19125-3-fei.w.wang@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210709033229.19125-1-fei.w.wang@intel.com> References: <20210709033229.19125-1-fei.w.wang@intel.com> Subject: [FFmpeg-devel] [PATCH v4 3/9] avcodec/av1dec: support setup shear process 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: Fei Wang MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Ms1a9jR4GMu+ Defined in spec 7.11.3.6/7.11.3.7. Signed-off-by: Fei Wang --- update: 1. fix possible overflow issue. libavcodec/av1dec.c | 98 +++++++++++++++++++++++++++++++++++++++++++++ libavcodec/av1dec.h | 1 + 2 files changed, 99 insertions(+) diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c index 1dda0f9160..d808a9d4c6 100644 --- a/libavcodec/av1dec.c +++ b/libavcodec/av1dec.c @@ -28,6 +28,34 @@ #include "internal.h" #include "profiles.h" +/**< same with Div_Lut defined in spec 7.11.3.7 */ +static const uint16_t div_lut[AV1_DIV_LUT_NUM] = { + 16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768, + 15709, 15650, 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142, + 15087, 15033, 14980, 14926, 14873, 14821, 14769, 14717, 14665, 14614, 14564, + 14513, 14463, 14413, 14364, 14315, 14266, 14218, 14170, 14122, 14075, 14028, + 13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662, 13618, 13574, 13530, + 13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107, 13066, + 13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633, + 12596, 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228, + 12193, 12157, 12122, 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848, + 11815, 11782, 11749, 11716, 11683, 11651, 11619, 11586, 11555, 11523, 11491, + 11460, 11429, 11398, 11367, 11336, 11305, 11275, 11245, 11215, 11185, 11155, + 11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894, 10866, 10838, + 10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538, + 10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255, + 10230, 10205, 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986, + 9963, 9939, 9916, 9892, 9869, 9846, 9823, 9800, 9777, 9754, 9732, + 9709, 9687, 9664, 9642, 9620, 9598, 9576, 9554, 9533, 9511, 9489, + 9468, 9447, 9425, 9404, 9383, 9362, 9341, 9321, 9300, 9279, 9259, + 9239, 9218, 9198, 9178, 9158, 9138, 9118, 9098, 9079, 9059, 9039, + 9020, 9001, 8981, 8962, 8943, 8924, 8905, 8886, 8867, 8849, 8830, + 8812, 8793, 8775, 8756, 8738, 8720, 8702, 8684, 8666, 8648, 8630, + 8613, 8595, 8577, 8560, 8542, 8525, 8508, 8490, 8473, 8456, 8439, + 8422, 8405, 8389, 8372, 8355, 8339, 8322, 8306, 8289, 8273, 8257, + 8240, 8224, 8208, 8192 +}; + static uint32_t inverse_recenter(int r, uint32_t v) { if (v > 2 * r) @@ -97,6 +125,70 @@ static void read_global_param(AV1DecContext *s, int type, int ref, int idx) -mx, mx + 1, r) << prec_diff) + round; } +static uint64_t round_two(uint64_t x, uint16_t n) +{ + if (n == 0) + return x; + return ((x + ((uint64_t)1 << (n - 1))) >> n); +} + +static int64_t round_two_signed(int64_t x, uint16_t n) +{ + return ((x<0) ? -((int64_t)round_two(-x, n)) : (int64_t)round_two(x, n)); +} + +/** + * Resolve divisor process. + * see spec 7.11.3.7 + */ +static int16_t resolve_divisor(uint32_t d, uint16_t *shift) +{ + int32_t e, f; + + *shift = av_log2(d); + e = d - (1 << (*shift)); + if (*shift > AV1_DIV_LUT_BITS) + f = round_two(e, *shift - AV1_DIV_LUT_BITS); + else + f = e << (AV1_DIV_LUT_BITS - (*shift)); + + *shift += AV1_DIV_LUT_PREC_BITS; + + return div_lut[f]; +} + +/** + * check if global motion params is valid. + * see spec 7.11.3.6 + */ +static uint8_t get_shear_params_valid(AV1DecContext *s, int idx) +{ + int16_t alpha, beta, gamma, delta, divf, divs; + int64_t v, w; + int32_t *param = &s->cur_frame.gm_params[idx][0]; + if (param[2] < 0) + return 0; + + alpha = av_clip_int16(param[2] - (1 << AV1_WARPEDMODEL_PREC_BITS)); + beta = av_clip_int16(param[3]); + divf = resolve_divisor(abs(param[2]), &divs); + v = (int64_t)param[4] * (1 << AV1_WARPEDMODEL_PREC_BITS); + w = (int64_t)param[3] * param[4]; + gamma = av_clip_int16((int)round_two_signed((v * divf), divs)); + delta = av_clip_int16(param[5] - (int)round_two_signed((w * divf), divs) - (1 << AV1_WARPEDMODEL_PREC_BITS)); + + alpha = round_two_signed(alpha, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; + beta = round_two_signed(beta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; + gamma = round_two_signed(gamma, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; + delta = round_two_signed(delta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; + + if ((4 * abs(alpha) + 7 * abs(beta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS) || + (4 * abs(gamma) + 4 * abs(delta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS)) + return 0; + + return 1; +} + /** * update gm type/params, since cbs already implemented part of this funcation, * so we don't need to full implement spec. @@ -144,6 +236,9 @@ static void global_motion_params(AV1DecContext *s) read_global_param(s, type, ref, 0); read_global_param(s, type, ref, 1); } + if (type <= AV1_WARP_MODEL_AFFINE) { + s->cur_frame.gm_invalid[ref] = !get_shear_params_valid(s, ref); + } } } @@ -509,6 +604,9 @@ static int av1_frame_ref(AVCodecContext *avctx, AV1Frame *dst, const AV1Frame *s dst->spatial_id = src->spatial_id; dst->temporal_id = src->temporal_id; + memcpy(dst->gm_invalid, + src->gm_invalid, + AV1_NUM_REF_FRAMES * sizeof(uint8_t)); memcpy(dst->gm_type, src->gm_type, AV1_NUM_REF_FRAMES * sizeof(uint8_t)); diff --git a/libavcodec/av1dec.h b/libavcodec/av1dec.h index 248a68750f..4e140588b9 100644 --- a/libavcodec/av1dec.h +++ b/libavcodec/av1dec.h @@ -42,6 +42,7 @@ typedef struct AV1Frame { int temporal_id; int spatial_id; + uint8_t gm_invalid[AV1_NUM_REF_FRAMES]; uint8_t gm_type[AV1_NUM_REF_FRAMES]; int32_t gm_params[AV1_NUM_REF_FRAMES][6];