From patchwork Fri Feb 14 03:32:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 17783 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 6D72044A76F for ; Fri, 14 Feb 2020 05:41:35 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5448468AE8A; Fri, 14 Feb 2020 05:41:35 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CA8F668A101 for ; Fri, 14 Feb 2020 05:41:28 +0200 (EET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 13 Feb 2020 19:41:27 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,439,1574150400"; d="scan'208";a="432893841" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.13.25]) by fmsmga005.fm.intel.com with ESMTP; 13 Feb 2020 19:41:26 -0800 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Fri, 14 Feb 2020 11:32:20 +0800 Message-Id: <1581651140-25325-1-git-send-email-yejun.guo@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH 3/3] libavcodec/libx264.c: add support for use_last_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 | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index bad9351..b275cc2 100644 --- a/libavcodec/libx264.c +++ b/libavcodec/libx264.c @@ -107,6 +107,12 @@ typedef struct X264Context { X264Opaque *reordered_opaque; /** + * quant_offsets that will be passed to encoder + */ + float *qoffsets; + int qsize; //in byte + + /** * If the encoder does not support ROI then warn the first time we * encounter a frame with ROI side data. */ @@ -299,9 +305,18 @@ static int x264_encode_set_roi(const AVFrame *frame, AVCodecContext *ctx, int bi int nb_rois; const AVRegionOfInterest *roi; uint32_t roi_size; - float *qoffsets; + int qsize = mbx * mby * sizeof(*x4->qoffsets); if (!sd) { + if (frame->use_last_roi) { + if (!x4->qoffsets) { + if (!x4->roi_warned) { + x4->roi_warned = 1; + av_log(ctx, AV_LOG_WARNING, "No previous data for ROI encoding, skipping ROI.\n"); + } + } + x4->pic.prop.quant_offsets = x4->qoffsets; + } return 0; } @@ -329,10 +344,18 @@ static int x264_encode_set_roi(const AVFrame *frame, AVCodecContext *ctx, int bi } nb_rois = sd->size / roi_size; - qoffsets = av_mallocz_array(mbx * mby, sizeof(*qoffsets)); - if (!qoffsets) + // re-allocate x4->qoffsets if size changed + if (x4->qsize != qsize) + av_freep(&x4->qoffsets); + + if (!x4->qoffsets) + x4->qoffsets = av_malloc(qsize); + if (!x4->qoffsets) return AVERROR(ENOMEM); + x4->qsize = qsize; + memset(x4->qoffsets, 0, x4->qsize); + // 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--) { @@ -347,7 +370,7 @@ static int x264_encode_set_roi(const AVFrame *frame, AVCodecContext *ctx, int bi endx = FFMIN(mbx, (roi->right + MB_SIZE - 1)/ MB_SIZE); if (roi->qoffset.den == 0) { - av_free(qoffsets); + av_freep(&x4->qoffsets); av_log(ctx, AV_LOG_ERROR, "AVRegionOfInterest.qoffset.den must not be zero.\n"); return AVERROR(EINVAL); } @@ -356,13 +379,12 @@ static int x264_encode_set_roi(const AVFrame *frame, AVCodecContext *ctx, int bi for (int y = starty; y < endy; y++) { for (int x = startx; x < endx; x++) { - qoffsets[x + y*mbx] = qoffset; + x4->qoffsets[x + y*mbx] = qoffset; } } } - x4->pic.prop.quant_offsets = qoffsets; - x4->pic.prop.quant_offsets_free = av_free; + x4->pic.prop.quant_offsets = x4->qoffsets; return 0; } @@ -515,6 +537,7 @@ static av_cold int X264_close(AVCodecContext *avctx) av_freep(&avctx->extradata); av_freep(&x4->sei); av_freep(&x4->reordered_opaque); + av_freep(&x4->qoffsets); if (x4->enc) { x264_encoder_close(x4->enc);