[1/3] avutil/spherical: add functions to retrieve and request projection names

Message ID 20170404202419.5536-1-jamrial@gmail.com
State New
Headers show

Commit Message

James Almer April 4, 2017, 8:24 p.m.
Signed-off-by: James Almer <jamrial@gmail.com>
---
 doc/APIchanges        |  4 ++++
 libavutil/spherical.c | 28 ++++++++++++++++++++++++++++
 libavutil/spherical.h | 18 ++++++++++++++++++
 libavutil/version.h   |  2 +-
 4 files changed, 51 insertions(+), 1 deletion(-)

Comments

Vittorio Giovara April 6, 2017, 6:34 p.m. | #1
On Tue, Apr 4, 2017 at 10:24 PM, James Almer <jamrial@gmail.com> wrote:
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  doc/APIchanges        |  4 ++++
>  libavutil/spherical.c | 28 ++++++++++++++++++++++++++++
>  libavutil/spherical.h | 18 ++++++++++++++++++
>  libavutil/version.h   |  2 +-
>  4 files changed, 51 insertions(+), 1 deletion(-)

The set looks good to me, thank you
Luca Barbato April 7, 2017, 8:54 a.m. | #2
On 04/04/2017 22:24, James Almer wrote:
> +const char *av_spherical_projection_name(enum AVSphericalProjection projection)
> +{
> +    if ((unsigned)projection >= FF_ARRAY_ELEMS(spherical_projection_names))
> +        return "unknown";
> +
> +    return spherical_projection_names[projection];
> +}
> +
> +int av_spherical_from_name(const char *name)
> +{
> +    int i;
> +
> +    for (i = 0; i < FF_ARRAY_ELEMS(spherical_projection_names); i++) {
> +        size_t len = strlen(spherical_projection_names[i]);
> +        if (!strncmp(spherical_projection_names[i], name, len))
> +            return i;
> +    }
> +
> +    return -1;
> +}

Having an explicit NONE or UNKNOWN with the negative value sounds
better. Then av_spherical_from_name can output the enum directly...

lu

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index a0ca3b7ac..09b4b3155 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -13,6 +13,10 @@  libavutil:     2017-03-23
 
 API changes, most recent first:
 
+2017-04-xx - xxxxxxx - lavu 56.1.0 - spherical.h
+  Add av_spherical_projection_name().
+  Add av_spherical_from_name().
+
 2017-03-xx - xxxxxxx - lavc 57.37.0 - avcodec.h
   Add AVCodecContext.hwaccel_flags field. This will control some hwaccels at
   a later point.
diff --git a/libavutil/spherical.c b/libavutil/spherical.c
index f5accc487..7b03b09da 100644
--- a/libavutil/spherical.c
+++ b/libavutil/spherical.c
@@ -18,6 +18,7 @@ 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "common.h"
 #include "mem.h"
 #include "spherical.h"
 
@@ -50,3 +51,30 @@  void av_spherical_tile_bounds(AVSphericalMapping *map,
     *right  = orig_width  - width  - *left;
     *bottom = orig_height - height - *top;
 }
+
+static const char *spherical_projection_names[] = {
+    [AV_SPHERICAL_EQUIRECTANGULAR]      = "equirectangular",
+    [AV_SPHERICAL_CUBEMAP]              = "cubemap",
+    [AV_SPHERICAL_EQUIRECTANGULAR_TILE] = "tiled equirectangular",
+};
+
+const char *av_spherical_projection_name(enum AVSphericalProjection projection)
+{
+    if ((unsigned)projection >= FF_ARRAY_ELEMS(spherical_projection_names))
+        return "unknown";
+
+    return spherical_projection_names[projection];
+}
+
+int av_spherical_from_name(const char *name)
+{
+    int i;
+
+    for (i = 0; i < FF_ARRAY_ELEMS(spherical_projection_names); i++) {
+        size_t len = strlen(spherical_projection_names[i]);
+        if (!strncmp(spherical_projection_names[i], name, len))
+            return i;
+    }
+
+    return -1;
+}
diff --git a/libavutil/spherical.h b/libavutil/spherical.h
index fd662cf67..caa023fd4 100644
--- a/libavutil/spherical.h
+++ b/libavutil/spherical.h
@@ -206,6 +206,24 @@  void av_spherical_tile_bounds(AVSphericalMapping *map,
                               size_t width, size_t height,
                               size_t *left, size_t *top,
                               size_t *right, size_t *bottom);
+
+/**
+ * Provide a human-readable name of a given AVSphericalProjection.
+ *
+ * @param projection The input AVSphericalProjection.
+ *
+ * @return The name of the AVSphericalProjection, or "unknown".
+ */
+const char *av_spherical_projection_name(enum AVSphericalProjection projection);
+
+/**
+ * Get the AVSphericalProjection form a human-readable name.
+ *
+ * @param name The input string.
+ *
+ * @return The AVSphericalProjection value, or -1 if not found.
+ */
+int av_spherical_from_name(const char *name);
 /**
  * @}
  * @}
diff --git a/libavutil/version.h b/libavutil/version.h
index b8425ea2c..fd72ff431 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -54,7 +54,7 @@ 
  */
 
 #define LIBAVUTIL_VERSION_MAJOR 56
-#define LIBAVUTIL_VERSION_MINOR  0
+#define LIBAVUTIL_VERSION_MINOR  1
 #define LIBAVUTIL_VERSION_MICRO  0
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \