From patchwork Mon Jun 3 23:19:03 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 13392 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 D02894492CD for ; Tue, 4 Jun 2019 02:19:19 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BBED068A145; Tue, 4 Jun 2019 02:19:19 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 65D08689F87 for ; Tue, 4 Jun 2019 02:19:13 +0300 (EEST) Received: by mail-wm1-f67.google.com with SMTP id f10so12910293wmb.1 for ; Mon, 03 Jun 2019 16:19:13 -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=UGRV1tQTKX652dVpGuB9wo1vkEJUnEP24a/amkwo1Xg=; b=EAxuTQ1rp2yh91VVK3ZWkHQtJRz9Z4rm2r2Qq7t6lWPlDviNX/wuotWk8qhzrjLZ4c mhnQgAWCm3ruZPEoDyt561hO26JQagbAJ/J+aWgIoaDZIHW0BHQ6/KWH7ahjy73Dn1bF ikm/nkiQq5wrEsUvQl9XzdbLMQsIOGUmqz8thC2TXVMkqU7L7ENRJ51eCuDEmgtr4oGA Muruwz55v06EBRafhQaguBoUoelKnA5URSeAXchyLJFJqtscBdYIR7w7QWFASNVsgtAH 0Uhn//3zb5BsLOVJxjIKyR3eyHK0/iWi6yva7CORWudNieZ82I17nfYOlMg0AB7OgTH5 IfJA== 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=UGRV1tQTKX652dVpGuB9wo1vkEJUnEP24a/amkwo1Xg=; b=mq00CPpuP0f8FXGMzh0rrJrHtJ1bGEz0V9toMHPprqlqBexSKkeOp46NC4vWKH5i/1 rHXxNDp0bf5hWArHFvG8PahGwZrOdhp5SwV3B2wXVP+2XA0MasK/HKU8Mxv6g0m2Uxj1 RazmyXExFjyuah5xY/Cq8kq1tHSBtgev0zHzZ8FpFvUx8uuVapH4LWhJ/ldHOEoD2fCe rJtp4fNIUpd62axbrd5UiTHX8Jo5r+dKbrl7V7nozaASV4dKL+kczso72VKWWPP2dzab oHgdwuhOrIGVM5Hx3ERQrWuFzVSD5ZRSLWv5UrARct42dOGzhB/8VWUram9/ArlOA6t8 huvQ== X-Gm-Message-State: APjAAAUdQKTf/35K/u7l5FPjYj9XADuWLMRPr6d6DUFdEwHzAjs77Tai jNglLcwK4i5dznHFgT3NLXF6xnb5L1w= X-Google-Smtp-Source: APXvYqyqTPrZwOWyO33Lq6MCVTEfHbM76TsOaPWTqZZOML5VcI/ZG4lmXXLe+C2XmrOZ1ugGwFUFOw== X-Received: by 2002:a1c:ca01:: with SMTP id a1mr3798045wmg.30.1559603952385; Mon, 03 Jun 2019 16:19:12 -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 z65sm22104463wme.37.2019.06.03.16.19.11 for (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 03 Jun 2019 16:19:11 -0700 (PDT) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Tue, 4 Jun 2019 00:19:03 +0100 Message-Id: <20190603231905.9536-3-sw@jkqxz.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190603231905.9536-1-sw@jkqxz.net> References: <20190603231905.9536-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 3/5] libx265: Update ROI behaviour to match documentation 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" Equivalent to the previous patch for libx264. --- libavcodec/libx265.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/libavcodec/libx265.c b/libavcodec/libx265.c index f56def53d5..665b780643 100644 --- a/libavcodec/libx265.c +++ b/libavcodec/libx265.c @@ -316,27 +316,36 @@ static av_cold int libx265_encode_set_roi(libx265Context *ctx, const AVFrame *fr int mb_size = (ctx->params->rc.qgSize == 8) ? 8 : 16; int mbx = (frame->width + mb_size - 1) / mb_size; int mby = (frame->height + mb_size - 1) / mb_size; + int qp_range = 51 + 6 * (pic->bitDepth - 8); int nb_rois; - AVRegionOfInterest *roi; + const AVRegionOfInterest *roi; + uint32_t roi_size; float *qoffsets; /* will be freed after encode is called. */ + + roi = (const AVRegionOfInterest*)sd->data; + roi_size = roi->self_size; + if (!roi_size || sd->size % roi_size != 0) { + av_log(ctx, AV_LOG_ERROR, "Invalid AVRegionOfInterest.self_size.\n"); + return AVERROR(EINVAL); + } + nb_rois = sd->size / roi_size; + qoffsets = av_mallocz_array(mbx * mby, sizeof(*qoffsets)); if (!qoffsets) return AVERROR(ENOMEM); - nb_rois = sd->size / sizeof(AVRegionOfInterest); - roi = (AVRegionOfInterest*)sd->data; - for (int count = 0; count < nb_rois; count++) { - int starty = FFMIN(mby, roi->top / mb_size); - int endy = FFMIN(mby, (roi->bottom + mb_size - 1)/ mb_size); - int startx = FFMIN(mbx, roi->left / mb_size); - int endx = FFMIN(mbx, (roi->right + mb_size - 1)/ mb_size); + // This list must be iterated in reverse because the first + // region in the list applies when regions overlap. + for (int i = nb_rois - 1; i >= 0; i--) { + int startx, endx, starty, endy; float qoffset; - if (roi->self_size == 0) { - av_free(qoffsets); - av_log(ctx, AV_LOG_ERROR, "AVRegionOfInterest.self_size must be set to sizeof(AVRegionOfInterest).\n"); - return AVERROR(EINVAL); - } + roi = (const AVRegionOfInterest*)(sd->data + roi_size * i); + + starty = FFMIN(mby, roi->top / mb_size); + endy = FFMIN(mby, (roi->bottom + mb_size - 1)/ mb_size); + startx = FFMIN(mbx, roi->left / mb_size); + endx = FFMIN(mbx, (roi->right + mb_size - 1)/ mb_size); if (roi->qoffset.den == 0) { av_free(qoffsets); @@ -344,18 +353,11 @@ static av_cold int libx265_encode_set_roi(libx265Context *ctx, const AVFrame *fr return AVERROR(EINVAL); } qoffset = roi->qoffset.num * 1.0f / roi->qoffset.den; - qoffset = av_clipf(qoffset, -1.0f, 1.0f); - - /* qp range of x265 is from 0 to 51, just choose 25 as the scale value, - * so the range of final qoffset is [-25.0, 25.0]. - */ - qoffset = qoffset * 25; + qoffset = av_clipf(qoffset * qp_range, -qp_range, +qp_range); for (int y = starty; y < endy; y++) for (int x = startx; x < endx; x++) qoffsets[x + y*mbx] = qoffset; - - roi = (AVRegionOfInterest*)((char*)roi + roi->self_size); } pic->quantOffsets = qoffsets;