From 3489b650f90c62ecf621f4f930239bb639515aca Mon Sep 17 00:00:00 2001
From: Martin Vignali <martin.vignali@gmail.com>
Date: Sun, 2 Dec 2018 13:28:33 +0100
Subject: [PATCH 1/4] avcodec/utils : add ff_int_is_in_list func

to check valid value, or return default_value
---
 libavcodec/internal.h | 12 ++++++++++++
 libavcodec/utils.c    | 19 +++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 0c2133f092..03f1d29bc2 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -404,6 +404,18 @@ int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len,
  */
 int64_t ff_guess_coded_bitrate(AVCodecContext *avctx);
 
+/**
+ * Check if a value is in the list. If not, return the default value
+ *
+ * @param ctx                Context for the log msg
+ * @param val_name           Name of the checked value, for log msg
+ * @param array_valid_values Array of valid int, ended with INT_MAX
+ * @param default_value      Value return if checked value is not in the array
+ * @return                   Value or default_value.
+ */
+int ff_int_is_in_list(void *ctx, const char * val_name, int val,
+                      int * array_valid_values, int default_value);
+
 #if defined(_WIN32) && CONFIG_SHARED && !defined(BUILDING_avcodec)
 #    define av_export_avcodec __declspec(dllimport)
 #else
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index c4c64a6ca4..2f4b5fa8a4 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -2209,3 +2209,22 @@ int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
 
     return bitrate;
 }
+
+int ff_int_is_in_list(void *ctx, const char * val_name, int val,
+                      int * array_valid_values, int default_value)
+{
+    int i = 0, ref_val;
+
+    while (1) {
+        ref_val = array_valid_values[i];
+        if (ref_val == INT_MAX)
+            break;
+        if (val == ref_val)
+            return val;
+        i++;
+    }
+    /* val is not a valid value */
+    av_log(ctx, AV_LOG_DEBUG,
+           "%s %d are not supported. Set to default value : %d\n", val_name, val, default_value);
+    return default_value;
+}
-- 
2.14.3 (Apple Git-98)

