From patchwork Fri Feb 14 03:32:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 17782 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 8179F44A76F for ; Fri, 14 Feb 2020 05:41:27 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6D82F68AE56; Fri, 14 Feb 2020 05:41:27 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BD33E68A101 for ; Fri, 14 Feb 2020 05:41:20 +0200 (EET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 13 Feb 2020 19:41:18 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,439,1574150400"; d="scan'208";a="434655968" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.13.25]) by fmsmga006.fm.intel.com with ESMTP; 13 Feb 2020 19:41:18 -0800 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Fri, 14 Feb 2020 11:32:12 +0800 Message-Id: <1581651132-25279-1-git-send-email-yejun.guo@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH 2/3] libavcodec/libx264.c: refine code to extract function x264_encode_set_roi 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 Cc: yejun.guo@intel.com MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Guo, Yejun --- libavcodec/libx264.c | 148 ++++++++++++++++++++++++++++----------------------- 1 file changed, 80 insertions(+), 68 deletions(-) diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index ca8f6c0..bad9351 100644 --- a/libavcodec/libx264.c +++ b/libavcodec/libx264.c @@ -289,6 +289,83 @@ static void reconfig_encoder(AVCodecContext *ctx, const AVFrame *frame) } } +static int x264_encode_set_roi(const AVFrame *frame, AVCodecContext *ctx, int bit_depth) +{ + AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST); + X264Context *x4 = ctx->priv_data; + int mbx = (frame->width + MB_SIZE - 1) / MB_SIZE; + int mby = (frame->height + MB_SIZE - 1) / MB_SIZE; + int qp_range = 51 + 6 * (bit_depth - 8); + int nb_rois; + const AVRegionOfInterest *roi; + uint32_t roi_size; + float *qoffsets; + + if (!sd) { + return 0; + } + + if (x4->params.rc.i_aq_mode == X264_AQ_NONE) { + if (!x4->roi_warned) { + x4->roi_warned = 1; + av_log(ctx, AV_LOG_WARNING, "Adaptive quantization must be enabled to use ROI encoding, skipping ROI.\n"); + } + return 0; + } + + if (frame->interlaced_frame != 0) { + if (!x4->roi_warned) { + x4->roi_warned = 1; + av_log(ctx, AV_LOG_WARNING, "interlaced_frame not supported for ROI encoding yet, skipping ROI.\n"); + } + return 0; + } + + 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); + + // 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; + + 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); + 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 * 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; + } + } + } + + x4->pic.prop.quant_offsets = qoffsets; + x4->pic.prop.quant_offsets_free = av_free; + return 0; +} + static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, int *got_packet) { @@ -300,7 +377,6 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, int bit_depth; int64_t wallclock = 0; X264Opaque *out_opaque; - AVFrameSideData *sd; x264_picture_init( &x4->pic ); x4->pic.img.i_csp = x4->params.i_csp; @@ -367,73 +443,9 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, } } - sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST); - if (sd) { - if (x4->params.rc.i_aq_mode == X264_AQ_NONE) { - if (!x4->roi_warned) { - x4->roi_warned = 1; - av_log(ctx, AV_LOG_WARNING, "Adaptive quantization must be enabled to use ROI encoding, skipping ROI.\n"); - } - } else { - if (frame->interlaced_frame == 0) { - int mbx = (frame->width + MB_SIZE - 1) / MB_SIZE; - int mby = (frame->height + MB_SIZE - 1) / MB_SIZE; - int qp_range = 51 + 6 * (bit_depth - 8); - int nb_rois; - const AVRegionOfInterest *roi; - uint32_t roi_size; - float *qoffsets; - - 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); - - // 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; - - 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); - 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 * 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; - } - } - } - - x4->pic.prop.quant_offsets = qoffsets; - x4->pic.prop.quant_offsets_free = av_free; - } else { - if (!x4->roi_warned) { - x4->roi_warned = 1; - av_log(ctx, AV_LOG_WARNING, "interlaced_frame not supported for ROI encoding yet, skipping ROI.\n"); - } - } - } - } + ret = x264_encode_set_roi(frame, ctx, bit_depth); + if (ret < 0) + return ret; } do {