From patchwork Sat Oct 12 01:28:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hwren X-Patchwork-Id: 15709 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 D6FB544641A for ; Sat, 12 Oct 2019 04:28:45 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BF406689B09; Sat, 12 Oct 2019 04:28:45 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from m15-112.126.com (m15-112.126.com [220.181.15.112]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B056C689A52 for ; Sat, 12 Oct 2019 04:28:38 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=05OK1taAVdx9yuxEdl Eg3i4qbmVoa/OGy3OT9PrqGSc=; b=g1QmFAcxvUFgDsx5OfCCT52ZvrihFG9g3b kAktboPT7IHh4xPfXDDyV1HpBCJGpLhPev5I50JbNFwuJOOj2YDxUZm8Fuc0iCOb m8O/JJ5oFpWbwUOJ8G1K7/5L9Px+kXp8AujP+MWQkgyQxRhQH+6/hQsgkdk52m0F myB07Ck2w= Received: from localhost.localdomain (unknown [162.105.23.171]) by smtp2 (Coremail) with SMTP id DMmowABHHQ5ELKFdxXGFCQ--.55754S4; Sat, 12 Oct 2019 09:28:36 +0800 (CST) From: hwren To: ffmpeg-devel@ffmpeg.org Date: Sat, 12 Oct 2019 09:28:33 +0800 Message-Id: <1570843714-16477-2-git-send-email-hwrenx@126.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1570843714-16477-1-git-send-email-hwrenx@126.com> References: <1570843714-16477-1-git-send-email-hwrenx@126.com> X-CM-TRANSID: DMmowABHHQ5ELKFdxXGFCQ--.55754S4 X-Coremail-Antispam: 1Uf129KBjvJXoW3XFWrWF4UCF13tryDGr4kJFb_yoWfXry3pr 1FyF9rZwn3Xa1Fyay8JF1Fvr45ZF40g3WUJw4DAw1vvayIvryYgry7KFW7KayIqaykta4j kr4ktFy5W343ta7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07j8qXdUUUUU= X-Originating-IP: [162.105.23.171] X-CM-SenderInfo: pkzuv0b06rjloofrz/1tbiZwRM6V16byOEXAAAs1 Subject: [FFmpeg-devel] [PATCH 2/3] lavc/libxvas2: optimize code style and data acces 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: hwrenx MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: hwrenx Optimize using of spaces and indents, change data access from multiplication to iteration. Signed-off-by: hwrenx --- libavcodec/libxavs2.c | 139 ++++++++++++++++++++++++++------------------------ 1 file changed, 71 insertions(+), 68 deletions(-) diff --git a/libavcodec/libxavs2.c b/libavcodec/libxavs2.c index 0179a1e..47ba74d 100644 --- a/libavcodec/libxavs2.c +++ b/libavcodec/libxavs2.c @@ -26,15 +26,16 @@ #include "mpeg12.h" #include "libavutil/avstring.h" -#define xavs2_opt_set2(name, format, ...) do{ \ - char opt_str[16] = {0}; \ - int err; \ - av_strlcatf(opt_str, sizeof(opt_str), format, __VA_ARGS__); \ - err = cae->api->opt_set2(cae->param, name, opt_str); \ - if (err < 0) {\ - av_log(avctx, AV_LOG_WARNING, "Invalid value for %s: %s\n", name, opt_str);\ - }\ -} while(0); +#define xavs2_opt_set2(name, format, ...) \ + do { \ + char opt_str[16] = {0}; \ + int err; \ + av_strlcatf(opt_str, sizeof(opt_str), format, __VA_ARGS__); \ + err = cae->api->opt_set2(cae->param, name, opt_str); \ + if (err < 0) { \ + av_log(avctx, AV_LOG_WARNING, "Invalid value for %s: %s\n", name, opt_str); \ + } \ + } while (0); typedef struct XAVS2EContext { AVClass *class; @@ -51,7 +52,7 @@ typedef struct XAVS2EContext { char *xavs2_opts; xavs2_outpacket_t packet; - xavs2_param_t *param; + xavs2_param_t * param; const xavs2_api_t *api; @@ -59,12 +60,12 @@ typedef struct XAVS2EContext { static av_cold int xavs2_init(AVCodecContext *avctx) { - XAVS2EContext *cae= avctx->priv_data; - int bit_depth, code; + XAVS2EContext *cae = avctx->priv_data; + int bit_depth, code; bit_depth = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? 8 : 10; - /* get API handler */ + // get API handler cae->api = xavs2_api_get(bit_depth); if (!cae->api) { av_log(avctx, AV_LOG_ERROR, "api get failed\n"); @@ -77,24 +78,21 @@ static av_cold int xavs2_init(AVCodecContext *avctx) return AVERROR(ENOMEM); } - xavs2_opt_set2("Width", "%d", avctx->width); - xavs2_opt_set2("Height", "%d", avctx->height); - xavs2_opt_set2("BFrames", "%d", avctx->max_b_frames); - xavs2_opt_set2("BitDepth", "%d", bit_depth); - xavs2_opt_set2("Log", "%d", cae->log_level); - xavs2_opt_set2("Preset", "%d", cae->preset_level); - - xavs2_opt_set2("IntraPeriodMax", "%d", avctx->gop_size); - xavs2_opt_set2("IntraPeriodMin", "%d", avctx->gop_size); - - xavs2_opt_set2("ThreadFrames", "%d", avctx->thread_count); - xavs2_opt_set2("ThreadRows", "%d", cae->lcu_row_threads); - - xavs2_opt_set2("OpenGOP", "%d", !(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)); + xavs2_opt_set2("Width", "%d", avctx->width); + xavs2_opt_set2("Height", "%d", avctx->height); + xavs2_opt_set2("BFrames", "%d", avctx->max_b_frames); + xavs2_opt_set2("BitDepth", "%d", bit_depth); + xavs2_opt_set2("Log", "%d", cae->log_level); + xavs2_opt_set2("Preset", "%d", cae->preset_level); + xavs2_opt_set2("IntraPeriodMax", "%d", avctx->gop_size); + xavs2_opt_set2("IntraPeriodMin", "%d", avctx->gop_size); + xavs2_opt_set2("ThreadFrames", "%d", avctx->thread_count); + xavs2_opt_set2("ThreadRows", "%d", cae->lcu_row_threads); + xavs2_opt_set2("OpenGOP", "%d", !(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)); if (cae->xavs2_opts) { - AVDictionary *dict = NULL; - AVDictionaryEntry *en = NULL; + AVDictionary * dict = NULL; + AVDictionaryEntry *en = NULL; if (!av_dict_parse_string(&dict, cae->xavs2_opts, "=", ":", 0)) { while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) { @@ -104,26 +102,23 @@ static av_cold int xavs2_init(AVCodecContext *avctx) } } - /* Rate control */ + // Rate control if (avctx->bit_rate > 0) { - xavs2_opt_set2("RateControl", "%d", 1); - xavs2_opt_set2("TargetBitRate", "%"PRId64"", avctx->bit_rate); - xavs2_opt_set2("InitialQP", "%d", cae->initial_qp); - xavs2_opt_set2("MaxQP", "%d", avctx->qmax >= 0 ? avctx->qmax : cae->max_qp); - xavs2_opt_set2("MinQP", "%d", avctx->qmin >= 0 ? avctx->qmin : cae->min_qp); + xavs2_opt_set2("RateControl", "%d", 1); + xavs2_opt_set2("TargetBitRate", "%" PRId64 "", avctx->bit_rate); + xavs2_opt_set2("InitialQP", "%d", cae->initial_qp); + xavs2_opt_set2("MaxQP", "%d", avctx->qmax >= 0 ? avctx->qmax : cae->max_qp); + xavs2_opt_set2("MinQP", "%d", avctx->qmin >= 0 ? avctx->qmin : cae->min_qp); } else { - xavs2_opt_set2("InitialQP", "%d", cae->qp); + xavs2_opt_set2("InitialQP", "%d", cae->qp); } - ff_mpeg12_find_best_frame_rate(avctx->framerate, &code, NULL, NULL, 0); - - xavs2_opt_set2("FrameRate", "%d", code); + xavs2_opt_set2("FrameRate", "%d", code); cae->encoder = cae->api->encoder_create(cae->param); - if (!cae->encoder) { - av_log(avctx,AV_LOG_ERROR, "Can not create encoder. Null pointer returned\n"); + av_log(avctx, AV_LOG_ERROR, "Can not create encoder. Null pointer returned\n"); return AVERROR(EINVAL); } @@ -132,44 +127,53 @@ static av_cold int xavs2_init(AVCodecContext *avctx) static void xavs2_copy_frame_with_shift(xavs2_picture_t *pic, const AVFrame *frame, const int shift_in) { - int j, k; - for (k = 0; k < 3; k++) { - int i_stride = pic->img.i_stride[k]; - for (j = 0; j < pic->img.i_lines[k]; j++) { - uint16_t *p_plane = (uint16_t *)&pic->img.img_planes[k][j * i_stride]; - int i; - uint8_t *p_buffer = frame->data[k] + frame->linesize[k] * j; - memset(p_plane, 0, i_stride); - for (i = 0; i < pic->img.i_width[k]; i++) { - p_plane[i] = p_buffer[i] << shift_in; + uint16_t *p_plane; + uint8_t * p_buffer; + int wIdx, hIdx, plane; + + for (plane = 0; plane < 3; plane++) { + p_plane = (uint16_t *)pic->img.img_planes[plane]; + p_buffer = frame->data[plane]; + for (hIdx = 0; hIdx < pic->img.i_lines[plane]; hIdx++) { + memset(p_plane, 0, pic->img.i_stride[plane]); + for (wIdx = 0; wIdx < pic->img.i_width[plane]; wIdx++) { + p_plane[wIdx] = p_buffer[wIdx] << shift_in; } + p_plane += pic->img.i_stride[plane]; + p_buffer += frame->linesize[plane]; } } } static void xavs2_copy_frame(xavs2_picture_t *pic, const AVFrame *frame) { - int j, k; - for (k = 0; k < 3; k++) { - for (j = 0; j < pic->img.i_lines[k]; j++) { - memcpy( pic->img.img_planes[k] + pic->img.i_stride[k] * j, - frame->data[k]+frame->linesize[k] * j, - pic->img.i_width[k] * pic->img.in_sample_size); + uint16_t *p_plane; + uint8_t * p_buffer; + int hIdx, plane; + + for (plane = 0; plane < 3; plane++) { + p_plane = (uint16_t *)pic->img.img_planes[plane]; + p_buffer = frame->data[plane]; + for (hIdx = 0; hIdx < pic->img.i_lines[plane]; hIdx++) { + memcpy(pic->img.img_planes[plane] + pic->img.i_stride[plane] * hIdx, + frame->data[plane] + frame->linesize[plane] * hIdx, + pic->img.i_width[plane] * pic->img.in_sample_size); + p_plane += pic->img.i_stride[plane]; + p_buffer += frame->linesize[plane]; } } } -static int xavs2_encode_frame(AVCodecContext *avctx, AVPacket *pkt, - const AVFrame *frame, int *got_packet) +static int xavs2_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet) { - XAVS2EContext *cae = avctx->priv_data; + XAVS2EContext * cae = avctx->priv_data; xavs2_picture_t pic; - int ret; + int ret; - /* create the XAVS2 video encoder */ - /* read frame data and send to the XAVS2 video encoder */ + // create the XAVS2 video encoder + // read frame data and send to the XAVS2 video encoder if (cae->api->encoder_get_buffer(cae->encoder, &pic) < 0) { - av_log(avctx,AV_LOG_ERROR, "failed to get frame buffer\n"); + av_log(avctx, AV_LOG_ERROR, "failed to get frame buffer\n"); return AVERROR_EXTERNAL; } if (frame) { @@ -208,9 +212,8 @@ static int xavs2_encode_frame(AVCodecContext *avctx, AVPacket *pkt, cae->api->encoder_encode(cae->encoder, NULL, &cae->packet); } - if ((cae->packet.len) && (cae->packet.state != XAVS2_STATE_FLUSH_END)){ - - if (av_new_packet(pkt, cae->packet.len) < 0){ + if ((cae->packet.len) && (cae->packet.state != XAVS2_STATE_FLUSH_END)) { + if (av_new_packet(pkt, cae->packet.len) < 0) { av_log(avctx, AV_LOG_ERROR, "packet alloc failed\n"); cae->api->encoder_packet_unref(cae->encoder, &cae->packet); return AVERROR(ENOMEM); @@ -235,7 +238,7 @@ static int xavs2_encode_frame(AVCodecContext *avctx, AVPacket *pkt, static av_cold int xavs2_close(AVCodecContext *avctx) { XAVS2EContext *cae = avctx->priv_data; - /* destroy the encoder */ + // destroy the encoder if (cae->api) { cae->api->encoder_destroy(cae->encoder);