From patchwork Mon Jan 7 19:54:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 11671 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 6A3F944CF75 for ; Mon, 7 Jan 2019 14:02:50 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2200F68A647; Mon, 7 Jan 2019 14:02:47 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 597C868A642 for ; Mon, 7 Jan 2019 14:02:40 +0200 (EET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Jan 2019 04:02:49 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,450,1539673200"; d="scan'208";a="132246609" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.13.25]) by fmsmga002.fm.intel.com with ESMTP; 07 Jan 2019 04:02:48 -0800 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Jan 2019 03:54:38 +0800 Message-Id: <1546890878-7164-1-git-send-email-yejun.guo@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH V6 2/2] 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 | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c index a68d0a7..9cfbaed 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; @@ -282,6 +286,7 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, x264_picture_t pic_out = {0}; int pict_type; int64_t *out_opaque; + AVFrameSideData *sd; x264_picture_init( &x4->pic ); x4->pic.img.i_csp = x4->params.i_csp; @@ -345,6 +350,68 @@ 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) { + static int show_aq_warning = 1; + static int show_interlaced_frame_warning = 1; + if (x4->params.rc.i_aq_mode == X264_AQ_NONE && show_aq_warning) { + show_aq_warning = 0; + 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 nb_rois; + AVRegionOfInterest* roi; + float* qoffsets; + qoffsets = (float*)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->qoffset.den == 0) { + av_free(qoffsets); + av_log(ctx, AV_LOG_ERROR, "AVRegionOfInterest.qoffset.den should not be zero.\n"); + return AVERROR(EINVAL); + } + qoffset = roi->qoffset.num * 1.0f / roi->qoffset.den; + qoffset = FFMIN(qoffset, 1.0f); + qoffset = FFMAX(qoffset, -1.0f); + + // 25 is a number that I think it is a possible proper scale value. + qoffset = qoffset * 25; + + for (int y = starty; y < endy; y++) { + for (int x = startx; x < endx; x++) { + qoffsets[x + y*mbx] = qoffset; + } + } + + if (roi->self_size == 0) { + av_free(qoffsets); + av_log(ctx, AV_LOG_ERROR, "AVRegionOfInterest.self_size should be set to sizeof(AVRegionOfInterest).\n"); + return AVERROR(EINVAL); + } + roi = (AVRegionOfInterest*)((char*)roi + roi->self_size); + } + + x4->pic.prop.quant_offsets = qoffsets; + x4->pic.prop.quant_offsets_free = av_free; + } else if (show_interlaced_frame_warning) { + show_interlaced_frame_warning = 0; + av_log(ctx, AV_LOG_WARNING, "interlaced_frame not supported for ROI encoding yet, skipping ROI.\n"); + } + } + } } do {