From patchwork Mon Jan 13 11:37:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 17319 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 F2AB844937B for ; Mon, 13 Jan 2020 13:46:20 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C986268AD2C; Mon, 13 Jan 2020 13:46:20 +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 4657368AC9B for ; Mon, 13 Jan 2020 13:46:12 +0200 (EET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 13 Jan 2020 03:46:10 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,428,1571727600"; d="scan'208";a="219274837" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.13.25]) by fmsmga008.fm.intel.com with ESMTP; 13 Jan 2020 03:46:10 -0800 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Jan 2020 19:37:56 +0800 Message-Id: <1578915476-21834-1-git-send-email-yejun.guo@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH] lavfi/dnn_processing: refine code to use function av_image_copy_plane for data copy 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 --- libavfilter/vf_dnn_processing.c | 85 ++++++++++++----------------------------- 1 file changed, 24 insertions(+), 61 deletions(-) diff --git a/libavfilter/vf_dnn_processing.c b/libavfilter/vf_dnn_processing.c index 13273f2..492df93 100644 --- a/libavfilter/vf_dnn_processing.c +++ b/libavfilter/vf_dnn_processing.c @@ -27,6 +27,7 @@ #include "libavutil/opt.h" #include "libavutil/pixdesc.h" #include "libavutil/avassert.h" +#include "libavutil/imgutils.h" #include "avfilter.h" #include "dnn_interface.h" #include "formats.h" @@ -231,6 +232,8 @@ static int config_output(AVFilterLink *outlink) static int copy_from_frame_to_dnn(DNNData *dnn_input, const AVFrame *frame) { + int bytewidth = av_image_get_linesize(frame->format, frame->width, 0); + switch (frame->format) { case AV_PIX_FMT_RGB24: case AV_PIX_FMT_BGR24: @@ -244,42 +247,17 @@ static int copy_from_frame_to_dnn(DNNData *dnn_input, const AVFrame *frame) } } } else { - uint8_t *dnn_input_data = dnn_input->data; av_assert0(dnn_input->dt == DNN_UINT8); - for (int i = 0; i < frame->height; i++) { - for(int j = 0; j < frame->width * 3; j++) { - int k = i * frame->linesize[0] + j; - int t = i * frame->width * 3 + j; - dnn_input_data[t] = frame->data[0][k]; - } - } + av_image_copy_plane(dnn_input->data, bytewidth, + frame->data[0], frame->linesize[0], + bytewidth, frame->height); } return 0; case AV_PIX_FMT_GRAY8: - { - uint8_t *dnn_input_data = dnn_input->data; - av_assert0(dnn_input->dt == DNN_UINT8); - for (int i = 0; i < frame->height; i++) { - for(int j = 0; j < frame->width; j++) { - int k = i * frame->linesize[0] + j; - int t = i * frame->width + j; - dnn_input_data[t] = frame->data[0][k]; - } - } - } - return 0; case AV_PIX_FMT_GRAYF32: - { - float *dnn_input_data = dnn_input->data; - av_assert0(dnn_input->dt == DNN_FLOAT); - for (int i = 0; i < frame->height; i++) { - for(int j = 0; j < frame->width; j++) { - int k = i * frame->linesize[0] + j * sizeof(float); - int t = i * frame->width + j; - dnn_input_data[t] = *(float*)(frame->data[0] + k); - } - } - } + av_image_copy_plane(dnn_input->data, bytewidth, + frame->data[0], frame->linesize[0], + bytewidth, frame->height); return 0; default: return AVERROR(EIO); @@ -290,6 +268,8 @@ static int copy_from_frame_to_dnn(DNNData *dnn_input, const AVFrame *frame) static int copy_from_dnn_to_frame(AVFrame *frame, const DNNData *dnn_output) { + int bytewidth = av_image_get_linesize(frame->format, frame->width, 0); + switch (frame->format) { case AV_PIX_FMT_RGB24: case AV_PIX_FMT_BGR24: @@ -303,42 +283,25 @@ static int copy_from_dnn_to_frame(AVFrame *frame, const DNNData *dnn_output) } } } else { - uint8_t *dnn_output_data = dnn_output->data; av_assert0(dnn_output->dt == DNN_UINT8); - for (int i = 0; i < frame->height; i++) { - for(int j = 0; j < frame->width * 3; j++) { - int k = i * frame->linesize[0] + j; - int t = i * frame->width * 3 + j; - frame->data[0][k] = dnn_output_data[t]; - } - } + av_image_copy_plane(frame->data[0], frame->linesize[0], + dnn_output->data, bytewidth, + bytewidth, frame->height); } return 0; case AV_PIX_FMT_GRAY8: - { - uint8_t *dnn_output_data = dnn_output->data; - av_assert0(dnn_output->dt == DNN_UINT8); - for (int i = 0; i < frame->height; i++) { - for(int j = 0; j < frame->width; j++) { - int k = i * frame->linesize[0] + j; - int t = i * frame->width + j; - frame->data[0][k] = dnn_output_data[t]; - } - } - } + // it is possible that data type of dnn output is float32, + // need to add support for such case when needed. + av_assert0(dnn_output->dt == DNN_UINT8); + av_image_copy_plane(frame->data[0], frame->linesize[0], + dnn_output->data, bytewidth, + bytewidth, frame->height); return 0; case AV_PIX_FMT_GRAYF32: - { - float *dnn_output_data = dnn_output->data; - av_assert0(dnn_output->dt == DNN_FLOAT); - for (int i = 0; i < frame->height; i++) { - for(int j = 0; j < frame->width; j++) { - int k = i * frame->linesize[0] + j * sizeof(float); - int t = i * frame->width + j; - *(float*)(frame->data[0] + k) = dnn_output_data[t]; - } - } - } + av_assert0(dnn_output->dt == DNN_FLOAT); + av_image_copy_plane(frame->data[0], frame->linesize[0], + dnn_output->data, bytewidth, + bytewidth, frame->height); return 0; default: return AVERROR(EIO);