[FFmpeg-devel] [PATCH v5] avcodec/libvpxenc: optimize parsing vpx_svc_ref_frame_config

Wonkap Jang wonkap at google.com
Thu Feb 18 21:01:31 EET 2021


Getting rid of unnecessary use of AVDictionary object in parsing
vpx_svc_ref_frame_config.
---
 libavcodec/libvpxenc.c | 73 +++++++++++++++++++++++++++++++-----------
 1 file changed, 55 insertions(+), 18 deletions(-)

diff --git a/libavcodec/libvpxenc.c b/libavcodec/libvpxenc.c
index 284cb9a108..dfa0763fff 100644
--- a/libavcodec/libvpxenc.c
+++ b/libavcodec/libvpxenc.c
@@ -42,6 +42,7 @@
 #include "libavutil/mathematics.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/avstring.h"
 
 /**
  * Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
@@ -127,7 +128,6 @@ typedef struct VPxEncoderContext {
     int roi_warned;
 #if CONFIG_LIBVPX_VP9_ENCODER && defined(VPX_CTRL_VP9E_SET_MAX_INTER_BITRATE_PCT)
     vpx_svc_ref_frame_config_t ref_frame_config;
-    AVDictionary *vpx_ref_frame_config;
 #endif
 } VPxContext;
 
@@ -561,18 +561,13 @@ static int vpx_ts_param_parse(VPxContext *ctx, struct vpx_codec_enc_cfg *enccfg,
 }
 
 #if CONFIG_LIBVPX_VP9_ENCODER && defined(VPX_CTRL_VP9E_SET_MAX_INTER_BITRATE_PCT)
-static int vpx_ref_frame_config_parse(VPxContext *ctx, const struct vpx_codec_enc_cfg *enccfg,
-                                      char *key, char *value, enum AVCodecID codec_id)
+static int vpx_ref_frame_config_set_value(vpx_svc_ref_frame_config_t *ref_frame_config,
+                                          int ss_number_layers, char *key, char *value)
 {
     size_t value_len = strlen(value);
-    int ss_number_layers = enccfg->ss_number_layers;
-    vpx_svc_ref_frame_config_t *ref_frame_config = &ctx->ref_frame_config;
 
     if (!value_len)
-        return -1;
-
-    if (codec_id != AV_CODEC_ID_VP9)
-        return -1;
+        return AVERROR(EINVAL);
 
     if (!strcmp(key, "rfc_update_buffer_slot")) {
         vp8_ts_parse_int_array(ref_frame_config->update_buffer_slot, value, value_len, ss_number_layers);
@@ -600,6 +595,49 @@ static int vpx_ref_frame_config_parse(VPxContext *ctx, const struct vpx_codec_en
 
     return 0;
 }
+
+static int vpx_parse_ref_frame_config_element(vpx_svc_ref_frame_config_t *ref_frame_config,
+                                              int ss_number_layers, const char **buf)
+{
+    const char key_val_sep[] = "=";
+    const char pairs_sep[] = ":";
+    char *key = av_get_token(buf, key_val_sep);
+    char *val = NULL;
+    int ret;
+
+    if (key && *key && strspn(*buf, key_val_sep)) {
+        (*buf)++;
+        val = av_get_token(buf, pairs_sep);
+    }
+
+    if (key && *key && val && *val)
+        ret = vpx_ref_frame_config_set_value(ref_frame_config, ss_number_layers, key, val);
+    else
+        ret = AVERROR(EINVAL);
+
+    av_freep(&key);
+    av_freep(&val);
+
+    return ret;
+}
+
+static int vpx_parse_ref_frame_config(vpx_svc_ref_frame_config_t *ref_frame_config,
+                                      int ss_number_layers, const char *str)
+{
+    int ret = 0;
+
+    while (*str) {
+        ret =
+            vpx_parse_ref_frame_config_element(ref_frame_config, ss_number_layers, &str);
+        if (ret < 0)
+            return ret;
+
+        if (*str)
+            str++;
+    }
+
+    return ret;
+}
 #endif
 
 #if CONFIG_LIBVPX_VP9_ENCODER
@@ -1594,20 +1632,19 @@ static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt,
 
             if (en) {
                 if (avctx->codec_id == AV_CODEC_ID_VP9) {
-                    AVDictionaryEntry* en2 = NULL;
-                    av_dict_parse_string(&ctx->vpx_ref_frame_config, en->value, "=", ":", 0);
-
-                    while ((en2 = av_dict_get(ctx->vpx_ref_frame_config, "", en2, AV_DICT_IGNORE_SUFFIX))) {
-                        if (vpx_ref_frame_config_parse(ctx, enccfg, en2->key, en2->value, avctx->codec_id) < 0)
-                            av_log(avctx, AV_LOG_WARNING,
-                                   "Error parsing option '%s = %s'.\n",
-                                   en2->key, en2->value);
+                    int ret;
+                    ret = vpx_parse_ref_frame_config(&ctx->ref_frame_config,
+                                                   enccfg->ss_number_layers, en->value);
+                    if (ret < 0) {
+                        av_log(avctx, AV_LOG_WARNING,
+                               "Error parsing ref_frame_config option %s.\n", en->value);
+                        return ret;
                     }
 
                     codecctl_intp(avctx, VP9E_SET_SVC_REF_FRAME_CONFIG, (int *)&ctx->ref_frame_config);
                 } else {
                     av_log(avctx, AV_LOG_WARNING,
-                           "Error using option ref-frame-config for a non-VP9 codec\n");
+                           "Using option ref-frame-config for a non-VP9 codec\n");
                 }
             }
 #endif
-- 
2.30.0.617.g56c4b15f3c-goog



More information about the ffmpeg-devel mailing list