From patchwork Wed Jan 23 16:11:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 11837 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 4CAB944DD4A for ; Wed, 23 Jan 2019 10:19:42 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7378F68AAF3; Wed, 23 Jan 2019 10:19:30 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 42E3168A960 for ; Wed, 23 Jan 2019 10:19:23 +0200 (EET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 23 Jan 2019 00:19:39 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,510,1539673200"; d="scan'208";a="109058324" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.13.25]) by orsmga007.jf.intel.com with ESMTP; 23 Jan 2019 00:19:38 -0800 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Thu, 24 Jan 2019 00:11:05 +0800 Message-Id: <1548259865-30496-1-git-send-email-yejun.guo@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH V3] avcodec/libx265: add support for ROI-based encoding 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Guo, Yejun --- libavcodec/libx265.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/libavcodec/libx265.c b/libavcodec/libx265.c index 27c90b3..9841536 100644 --- a/libavcodec/libx265.c +++ b/libavcodec/libx265.c @@ -285,6 +285,65 @@ static av_cold int libx265_encode_init(AVCodecContext *avctx) return 0; } +static av_cold int libx265_encode_set_roi(libx265Context *ctx, const AVFrame *frame, x265_picture* pic) +{ + AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST); + if (sd) { + if (ctx->params->rc.aqMode == X265_AQ_NONE) { + av_log(ctx, AV_LOG_WARNING, "Adaptive quantization must be enabled to use ROI encoding, skipping ROI.\n"); + } else { + /* 8x8 block when qg-size is 8, 16*16 block otherwise. */ + 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 nb_rois; + AVRegionOfInterest *roi; + float *qoffsets; /* will be freed after encode is called. */ + 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); + 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); + } + + if (roi->qoffset.den == 0) { + av_free(qoffsets); + av_log(ctx, AV_LOG_ERROR, "AVRegionOfInterest.qoffset.den must not be zero.\n"); + 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; + + 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; + } + } + return 0; +} + static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic, int *got_packet) { @@ -314,10 +373,17 @@ static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt, pic->pict_type == AV_PICTURE_TYPE_P ? X265_TYPE_P : pic->pict_type == AV_PICTURE_TYPE_B ? X265_TYPE_B : X265_TYPE_AUTO; + + ret = libx265_encode_set_roi(ctx, pic, &x265pic); + if (ret < 0) + return ret; } ret = ctx->api->encoder_encode(ctx->encoder, &nal, &nnal, pic ? &x265pic : NULL, &x265pic_out); + + av_freep(&x265pic.quantOffsets); + if (ret < 0) return AVERROR_EXTERNAL;