Adjustments to using the key & value API in aomenc.

This patch contains several adjustments that was brought up in the
previous commit
(https://aomedia-review.googlesource.com/c/aom/+/129381).

BUG=aomedia:2875

Change-Id: I497337e259088181b44a253b0c7135e67083fb49
diff --git a/apps/aomenc.c b/apps/aomenc.c
index 6abd856..80d36c4 100644
--- a/apps/aomenc.c
+++ b/apps/aomenc.c
@@ -314,14 +314,9 @@
                                &g_av1_codec_arg_defs.sframe_mode,
                                NULL };
 
-// The first part of this array should match the control IDs in the array
-// av1_arg_ctrl_map. To add a new option with its control ID, insert the option
-// before the comment "Option that supports the control ID API ends here."
-// Alternatively, to add a new option supported by the key & value API, just
-// append the option to this array.
 // TODO(bohanli): Currently all options are supported by the key & value API.
 // Consider removing the control ID usages?
-const arg_def_t *av1_args[] = {
+const arg_def_t *av1_ctrl_args[] = {
   &g_av1_codec_arg_defs.cpu_used_av1,
   &g_av1_codec_arg_defs.auto_altref,
   &g_av1_codec_arg_defs.sharpness,
@@ -424,8 +419,12 @@
   &g_av1_codec_arg_defs.input_chroma_subsampling_y,
 #if CONFIG_TUNE_VMAF
   &g_av1_codec_arg_defs.vmaf_model_path,
-#endif  // Option that supports the control ID API ends here.
-  NULL
+#endif
+  NULL,
+};
+
+const arg_def_t *av1_key_val_args[] = {
+  NULL,
 };
 
 static const arg_def_t *no_args[] = { NULL };
@@ -449,7 +448,8 @@
   arg_show_usage(fout, kf_args);
 #if CONFIG_AV1_ENCODER
   fprintf(fout, "\nAV1 Specific Options:\n");
-  arg_show_usage(fout, av1_args);
+  arg_show_usage(fout, av1_ctrl_args);
+  arg_show_usage(fout, av1_key_val_args);
 #endif
   fprintf(fout,
           "\nStream timebase (--timebase):\n"
@@ -475,6 +475,7 @@
 
 #if CONFIG_AV1_ENCODER
 #define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
+#define ARG_KEY_VAL_CNT_MAX NELEMENTS(av1_key_val_args)
 #endif
 
 #if !CONFIG_WEBM_IO
@@ -492,7 +493,7 @@
   stereo_format_t stereo_fmt;
   int arg_ctrls[ARG_CTRL_CNT_MAX][2];
   int arg_ctrl_cnt;
-  const char *arg_key_vals[ARG_CTRL_CNT_MAX][2];
+  const char *arg_key_vals[ARG_KEY_VAL_CNT_MAX][2];
   int arg_key_val_cnt;
   int write_webm;
   const char *film_grain_filename;
@@ -808,7 +809,7 @@
   // so we simply append it.
   if (key == AV1E_SET_TARGET_SEQ_LEVEL_IDX) {
     j = config->arg_ctrl_cnt;
-    assert(j < (int)ARG_CTRL_CNT_MAX);
+    assert(j < ARG_CTRL_CNT_MAX);
     config->arg_ctrls[j][0] = key;
     config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
     ++config->arg_ctrl_cnt;
@@ -822,7 +823,7 @@
     if (config->arg_ctrls[j][0] == key) break;
 
   /* Update/insert */
-  assert(j < (int)ARG_CTRL_CNT_MAX);
+  assert(j < ARG_CTRL_CNT_MAX);
   config->arg_ctrls[j][0] = key;
   config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
 
@@ -830,18 +831,19 @@
     warn("auto-alt-ref > 1 is deprecated... setting auto-alt-ref=1\n");
     config->arg_ctrls[j][1] = 1;
   }
+
   if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
 }
 
 static void set_config_arg_key_vals(struct stream_config *config,
-                                    const char *name, const char *val) {
+                                    const char *name, const struct arg *arg) {
   int j;
-
+  const char *val = arg->val;
   // For target level, the settings should accumulate rather than overwrite,
   // so we simply append it.
   if (strcmp(name, "target-seq-level-idx") == 0) {
     j = config->arg_key_val_cnt;
-    assert(j < (int)ARG_CTRL_CNT_MAX);
+    assert(j < ARG_KEY_VAL_CNT_MAX);
     config->arg_key_vals[j][0] = name;
     config->arg_key_vals[j][1] = val;
     ++config->arg_key_val_cnt;
@@ -855,10 +857,18 @@
     if (strcmp(name, config->arg_key_vals[j][0]) == 0) break;
 
   /* Update/insert */
-  assert(j < (int)ARG_CTRL_CNT_MAX);
+  assert(j < ARG_KEY_VAL_CNT_MAX);
   config->arg_key_vals[j][0] = name;
   config->arg_key_vals[j][1] = val;
 
+  if (strcmp(name, g_av1_codec_arg_defs.auto_altref.long_name) == 0) {
+    int auto_altref = arg_parse_int(arg);
+    if (auto_altref > 1) {
+      warn("auto-alt-ref > 1 is deprecated... setting auto-alt-ref=1\n");
+      config->arg_key_vals[j][1] = "1";
+    }
+  }
+
   if (j == config->arg_key_val_cnt) config->arg_key_val_cnt++;
 }
 
@@ -866,7 +876,8 @@
                                struct stream_state *stream, char **argv) {
   char **argi, **argj;
   struct arg arg;
-  static const arg_def_t **args_list = no_args;
+  static const arg_def_t **ctrl_args = no_args;
+  static const arg_def_t **key_val_args = no_args;
   static const int *ctrl_args_map = NULL;
   struct stream_config *config = &stream->config;
   int eos_mark_found = 0;
@@ -878,8 +889,9 @@
   } else if (strcmp(get_short_name_by_aom_encoder(global->codec), "av1") == 0) {
     // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
     // Consider to expand this set for AV1 encoder control.
-    args_list = av1_args;
+    ctrl_args = av1_ctrl_args;
     ctrl_args_map = av1_arg_ctrl_map;
+    key_val_args = av1_key_val_args;
 #endif
   }
 
@@ -1077,22 +1089,24 @@
       }
     } else {
       int i, match = 0;
-      // check if the ctrl APIs supports this arg
-      for (i = 0; ctrl_args_map && ctrl_args_map[i] && args_list[i]; i++) {
-        if (arg_match(&arg, args_list[i], argi)) {
-          match = 1;
-          if (ctrl_args_map) {
+      // check if the control ID API supports this arg
+      if (ctrl_args_map) {
+        for (i = 0; ctrl_args[i]; i++) {
+          if (arg_match(&arg, ctrl_args[i], argi)) {
+            match = 1;
             set_config_arg_ctrls(config, ctrl_args_map[i], &arg);
+            break;
           }
-          break;
         }
       }
-      // check if the key & value API supports this arg
-      for (i = 0; !match && args_list[i]; i++) {
-        if (arg_match(&arg, args_list[i], argi)) {
-          match = 1;
-          set_config_arg_key_vals(config, args_list[i]->long_name, arg.val);
-          break;
+      if (!match) {
+        // check if the key & value API supports this arg
+        for (i = 0; key_val_args[i]; i++) {
+          if (arg_match(&arg, key_val_args[i], argi)) {
+            match = 1;
+            set_config_arg_key_vals(config, key_val_args[i]->long_name, &arg);
+            break;
+          }
         }
       }
       if (!match) argj++;
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index f6b6c72..f0aa563 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -30,7 +30,6 @@
 #include "av1/arg_defs.h"
 
 #include "common/args_helper.h"
-#include "common/tools_common.h"
 
 struct av1_extracfg {
   int cpu_used;
@@ -2844,10 +2843,6 @@
   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.auto_altref, argv,
                               err_string)) {
     extra_cfg.enable_auto_alt_ref = arg_parse_uint_helper(&arg, err_string);
-    if (strlen(err_string) == 0 && extra_cfg.enable_auto_alt_ref > 1) {
-      warn("auto-alt-ref > 1 is deprecated... setting auto-alt-ref=1\n");
-      extra_cfg.enable_auto_alt_ref = 1;
-    }
   } else if (arg_match_helper(&arg, &g_av1_codec_arg_defs.noise_sens, argv,
                               err_string)) {
     extra_cfg.noise_sensitivity = arg_parse_uint_helper(&arg, err_string);