[FFmpeg-devel] [PATCH 2/3] lavc/libxvas2: optimize code style and data acces

hwren hwrenx at 126.com
Sat Oct 12 04:28:33 EEST 2019


From: hwrenx <hwrenx at 126.com>

Optimize using of spaces and indents, change data access from multiplication to iteration.

Signed-off-by: hwrenx <hwrenx at 126.com>
---
 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);
 
-- 
2.7.4



More information about the ffmpeg-devel mailing list