From patchwork Thu Dec 27 11:06:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 11559 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 5CC4344C7E6 for ; Thu, 27 Dec 2018 05:14:09 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1A7C268AD78; Thu, 27 Dec 2018 05:14:06 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5135668AD4C for ; Thu, 27 Dec 2018 05:13:58 +0200 (EET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 26 Dec 2018 19:14:02 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,403,1539673200"; d="scan'208";a="104958901" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.13.25]) by orsmga008.jf.intel.com with ESMTP; 26 Dec 2018 19:14:01 -0800 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Thu, 27 Dec 2018 19:06:09 +0800 Message-Id: <1545908769-23463-1-git-send-email-yejun.guo@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH V3 2/3] avcodec/libx264: 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" This patch just enables the path from ffmpeg to libx264, the more encoders can be added later. Signed-off-by: Guo, Yejun --- libavcodec/libx264.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index a68d0a7..a411f17 100644 --- a/libavcodec/libx264.c +++ b/libavcodec/libx264.c @@ -40,6 +40,10 @@ #include #include +// from x264.h, for quant_offsets, Macroblocks are 16x16 +// blocks of pixels (with respect to the luma plane) +#define MB_SIZE 16 + typedef struct X264Context { AVClass *class; x264_param_t params; @@ -345,6 +349,41 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, } } } + + AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ROIS); + if (sd != NULL) { + if (x4->params.rc.i_aq_mode == X264_AQ_NONE) { + av_log(ctx, AV_LOG_WARNING, "Adaptive quantization must be enabled to use ROI encoding, skipping ROI.\n"); + } else { + if (frame->interlaced_frame == 0) { + size_t mbx = (frame->width + MB_SIZE - 1) / MB_SIZE; + size_t mby = (frame->height + MB_SIZE - 1) / MB_SIZE; + float* qoffsets; + qoffsets = (float*)av_mallocz_array(mbx * mby, sizeof(*qoffsets)); + if (qoffsets == NULL) + return AVERROR(ENOMEM); + + size_t nb_rois = sd->size / sizeof(AVROI); + AVROI* rois = (AVROI*)sd->data; + for (size_t roi = 0; roi < nb_rois; roi++) { + int starty = FFMIN(mby, rois[roi].top / MB_SIZE); + int endy = FFMIN(mby, (rois[roi].bottom + MB_SIZE - 1)/ MB_SIZE); + int startx = FFMIN(mbx, rois[roi].left / MB_SIZE); + int endx = FFMIN(mbx, (rois[roi].right + MB_SIZE - 1)/ MB_SIZE); + for (int y = starty; y < endy; y++) { + for (int x = startx; x < endx; x++) { + qoffsets[x + y*mbx] = rois[roi].qoffset; + } + } + } + + x4->pic.prop.quant_offsets = qoffsets; + x4->pic.prop.quant_offsets_free = av_free; + } else { + av_log(ctx, AV_LOG_WARNING, "interlaced_frame not supported for ROI encoding yet, skipping ROI.\n"); + } + } + } } do {