From patchwork Thu Feb 28 19:10:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 12171 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 3AAB84439EF for ; Thu, 28 Feb 2019 13:20:00 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1BA7768A4BD; Thu, 28 Feb 2019 13:20:00 +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 843EF689C23 for ; Thu, 28 Feb 2019 13:19:53 +0200 (EET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Feb 2019 03:19:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,423,1544515200"; d="scan'208";a="150733376" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.13.25]) by fmsmga001.fm.intel.com with ESMTP; 28 Feb 2019 03:19:49 -0800 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Fri, 1 Mar 2019 03:10:20 +0800 Message-Id: <1551381020-18742-1-git-send-email-yejun.guo@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH V2] avcodec/libvpxenc: add VP8 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/libvpxenc.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/libavcodec/libvpxenc.c b/libavcodec/libvpxenc.c index c823b8a..0f8efcc 100644 --- a/libavcodec/libvpxenc.c +++ b/libavcodec/libvpxenc.c @@ -1057,6 +1057,134 @@ static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out) return size; } +static int vp8_encode_set_roi(AVCodecContext *avctx, const AVFrame *frame) +{ + /* range of vpx_roi_map_t.delta_q[i] is [-63, 63] */ +#define MAX_DELTA_Q 63 + + const AVRegionOfInterest *roi = NULL; + vpx_roi_map_t roi_map; + int ret = 0; + int nb_rois; + AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST); + VPxContext *ctx = avctx->priv_data; + vpx_active_map_t active_map = { 0, 0, 0 }; + int segment_id = 0; + + /* record the mapping from delta_q to "segment id + 1". + * delta_q is shift with MAX_DELTA_Q, and so the range is [0, 2*MAX_DELTA_Q]. + * add 1 to segment id, so no mapping if the value of array element is zero. + */ + int segment_mapping[2 * MAX_DELTA_Q + 1] = {0}; + + /* segment id 0 in roi_map is reserved for the areas not covered by AVRegionOfInterest. + * segment id 0 in roi_map is also for the areas with AVRegionOfInterest.qoffset near 0. + */ + segment_mapping[MAX_DELTA_Q] = segment_id + 1; + segment_id++; + + active_map.rows = (frame->height + 15) / 16; + active_map.cols = (frame->width + 15) / 16; + + if (!sd) { + /* For the case that some middle frames do not have ROI while other frames have ROIs. + * Due to libvpx behaivor, we have to reset VP8E_SET_ACTIVEMAP, otherwise the previous + * ROIs continue working for frames without ROIs. + */ + if (vpx_codec_control(&ctx->encoder, VP8E_SET_ACTIVEMAP, &active_map)) { + log_encoder_error(avctx, "Failed to set VP8E_SET_ACTIVEMAP codec control.\n"); + return AVERROR_INVALIDDATA; + } + return 0; + } + + memset(&roi_map, 0, sizeof(roi_map)); + roi_map.rows = active_map.rows; + roi_map.cols = active_map.cols; + roi_map.roi_map = av_mallocz(roi_map.rows * roi_map.cols); + if (!roi_map.roi_map) { + av_log(avctx, AV_LOG_ERROR, "roi_map alloc failed.\n"); + ret = AVERROR(ENOMEM); + goto fail; + } + + active_map.active_map = av_malloc(active_map.rows * active_map.cols); + if (!active_map.active_map) { + av_log(avctx, AV_LOG_ERROR, "active_map alloc failed.\n"); + ret = AVERROR(ENOMEM); + goto fail; + } + /* set 1 to enable the corresponding element of vpx_roi_map_t.roi_map. */ + memset(active_map.active_map, 1, active_map.rows * active_map.cols); + + roi = (const AVRegionOfInterest*)sd->data; + if (!roi->self_size || sd->size % roi->self_size != 0) { + av_log(ctx, AV_LOG_ERROR, "Invalid AVRegionOfInterest.self_size.\n"); + ret = AVERROR(EINVAL); + goto fail; + } + nb_rois = sd->size / roi->self_size; + + // This list must be iterated in reverse because regions are + // defined in order of decreasing importance. + for (int i = nb_rois - 1; i >= 0; i--) { + int qoffset; + int mapping_index; + int mapping_value; + int starty = FFMIN(roi_map.rows, roi->top / 16); + int endy = FFMIN(roi_map.rows, (roi->bottom + 15) / 16); + int startx = FFMIN(roi_map.cols, roi->left / 16); + int endx = FFMIN(roi_map.cols, (roi->right + 15) / 16); + + roi = (const AVRegionOfInterest*)(sd->data + roi->self_size * i); + if (roi->qoffset.den == 0) { + av_log(ctx, AV_LOG_ERROR, "AVRegionOfInterest.qoffset.den must not be zero.\n"); + ret = AVERROR(EINVAL); + goto fail; + } + + qoffset = (int)(roi->qoffset.num * 1.0f / roi->qoffset.den * MAX_DELTA_Q); + qoffset = av_clip(qoffset, -MAX_DELTA_Q, MAX_DELTA_Q); + + mapping_index = qoffset + MAX_DELTA_Q; + if (!segment_mapping[mapping_index]) { + if (segment_id > 3) { + av_log(ctx, AV_LOG_WARNING, + "ROI only support 4 segments (and segment 0 is reserved for non-ROIs), skipping this one.\n"); + roi = (AVRegionOfInterest*)((char*)roi + roi->self_size); + continue; + } + + segment_mapping[mapping_index] = segment_id + 1; + roi_map.delta_q[segment_id] = qoffset; + segment_id++; + } + + mapping_value = segment_mapping[mapping_index]; + + for (int y = starty; y < endy; y++) + for (int x = startx; x < endx; x++) + roi_map.roi_map[x + y * roi_map.cols] = mapping_value - 1; + } + + if (vpx_codec_control(&ctx->encoder, VP8E_SET_ROI_MAP, &roi_map)) { + log_encoder_error(avctx, "Failed to set VP8E_SET_ROI_MAP codec control.\n"); + ret = AVERROR_INVALIDDATA; + goto fail; + } + + if (vpx_codec_control(&ctx->encoder, VP8E_SET_ACTIVEMAP, &active_map)) { + log_encoder_error(avctx, "Failed to set VP8E_SET_ACTIVEMAP codec control.\n"); + ret = AVERROR_INVALIDDATA; + goto fail; + } + +fail: + av_freep(&active_map.active_map); + av_freep(&roi_map.roi_map); + return ret; +} + static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet) { @@ -1113,6 +1241,9 @@ static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt, flags |= strtoul(en->value, NULL, 10); } } + + if (avctx->codec_id == AV_CODEC_ID_VP8) + vp8_encode_set_roi(avctx, frame); } res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp,