diff mbox

[FFmpeg-devel] pixdesc: Add API to map color property name to enum value

Message ID 20170920122332.50329-1-vittorio.giovara@gmail.com
State New
Headers show

Commit Message

Vittorio Giovara Sept. 20, 2017, 12:23 p.m. UTC
Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
---
Updated following review.
Vittorio

 libavutil/pixdesc.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 libavutil/pixdesc.h | 25 +++++++++++++++++++++
 2 files changed, 90 insertions(+)

Comments

Moritz Barsnick Sept. 20, 2017, 1:31 p.m. UTC | #1
On Wed, Sep 20, 2017 at 14:23:32 +0200, Vittorio Giovara wrote:
> +/**
> + * @return the AVChromaLocation value for name or -1 if not found.
> + */

You missed modifying the "-1" here.

Moritz
wm4 Sept. 20, 2017, 6:30 p.m. UTC | #2
On Wed, 20 Sep 2017 14:23:32 +0200
Vittorio Giovara <vittorio.giovara@gmail.com> wrote:

> Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
> ---
> Updated following review.
> Vittorio
> 
>  libavutil/pixdesc.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++
>  libavutil/pixdesc.h | 25 +++++++++++++++++++++
>  2 files changed, 90 insertions(+)
> 
> diff --git a/libavutil/pixdesc.c b/libavutil/pixdesc.c
> index 33aa2d705f..26e208a947 100644
> --- a/libavutil/pixdesc.c
> +++ b/libavutil/pixdesc.c
> @@ -2749,26 +2749,91 @@ const char *av_color_range_name(enum AVColorRange range)
>          color_range_names[range] : NULL;
>  }
>  
> +int av_color_range_from_name(const char *name)
> +{
> +    int i;
> +
> +    for (i = 0; i < FF_ARRAY_ELEMS(color_range_names); i++) {
> +        size_t len = strlen(color_range_names[i]);
> +        if (!strncmp(color_range_names[i], name, len))
> +            return i;
> +    }
> +
> +    return AVERROR(ENOSYS);
> +}

To be honest, ENOSYS is even more confusing than -1. Think about what
happens if someone converts the error to a string and displays that to
a user.
Derek Buitenhuis Sept. 20, 2017, 8:34 p.m. UTC | #3
On 9/20/2017 7:30 PM, wm4 wrote:
> To be honest, ENOSYS is even more confusing than -1. Think about what
> happens if someone converts the error to a string and displays that to
> a user.

AVERROR(EINVAL)?

- Derek
diff mbox

Patch

diff --git a/libavutil/pixdesc.c b/libavutil/pixdesc.c
index 33aa2d705f..26e208a947 100644
--- a/libavutil/pixdesc.c
+++ b/libavutil/pixdesc.c
@@ -2749,26 +2749,91 @@  const char *av_color_range_name(enum AVColorRange range)
         color_range_names[range] : NULL;
 }
 
+int av_color_range_from_name(const char *name)
+{
+    int i;
+
+    for (i = 0; i < FF_ARRAY_ELEMS(color_range_names); i++) {
+        size_t len = strlen(color_range_names[i]);
+        if (!strncmp(color_range_names[i], name, len))
+            return i;
+    }
+
+    return AVERROR(ENOSYS);
+}
+
 const char *av_color_primaries_name(enum AVColorPrimaries primaries)
 {
     return (unsigned) primaries < AVCOL_PRI_NB ?
         color_primaries_names[primaries] : NULL;
 }
 
+int av_color_primaries_from_name(const char *name)
+{
+    int i;
+
+    for (i = 0; i < FF_ARRAY_ELEMS(color_primaries_names); i++) {
+        size_t len = strlen(color_primaries_names[i]);
+        if (!strncmp(color_primaries_names[i], name, len))
+            return i;
+    }
+
+    return AVERROR(ENOSYS);
+}
+
 const char *av_color_transfer_name(enum AVColorTransferCharacteristic transfer)
 {
     return (unsigned) transfer < AVCOL_TRC_NB ?
         color_transfer_names[transfer] : NULL;
 }
 
+int av_color_transfer_from_name(const char *name)
+{
+    int i;
+
+    for (i = 0; i < FF_ARRAY_ELEMS(color_transfer_names); i++) {
+        size_t len = strlen(color_transfer_names[i]);
+        if (!strncmp(color_transfer_names[i], name, len))
+            return i;
+    }
+
+    return AVERROR(ENOSYS);
+}
+
 const char *av_color_space_name(enum AVColorSpace space)
 {
     return (unsigned) space < AVCOL_SPC_NB ?
         color_space_names[space] : NULL;
 }
 
+int av_color_space_from_name(const char *name)
+{
+    int i;
+
+    for (i = 0; i < FF_ARRAY_ELEMS(color_space_names); i++) {
+        size_t len = strlen(color_space_names[i]);
+        if (!strncmp(color_space_names[i], name, len))
+            return i;
+    }
+
+    return AVERROR(ENOSYS);
+}
+
 const char *av_chroma_location_name(enum AVChromaLocation location)
 {
     return (unsigned) location < AVCHROMA_LOC_NB ?
         chroma_location_names[location] : NULL;
 }
+
+int av_chroma_location_from_name(const char *name)
+{
+    int i;
+
+    for (i = 0; i < FF_ARRAY_ELEMS(chroma_location_names); i++) {
+        size_t len = strlen(chroma_location_names[i]);
+        if (!strncmp(chroma_location_names[i], name, len))
+            return i;
+    }
+
+    return AVERROR(ENOSYS);
+}
diff --git a/libavutil/pixdesc.h b/libavutil/pixdesc.h
index b0ec81b81b..5542e2283c 100644
--- a/libavutil/pixdesc.h
+++ b/libavutil/pixdesc.h
@@ -250,26 +250,51 @@  int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt);
  */
 const char *av_color_range_name(enum AVColorRange range);
 
+/**
+ * @return the AVColorRange value for name or an AVError if not found.
+ */
+int av_color_range_from_name(const char *name);
+
 /**
  * @return the name for provided color primaries or NULL if unknown.
  */
 const char *av_color_primaries_name(enum AVColorPrimaries primaries);
 
+/**
+ * @return the AVColorPrimaries value for name or an AVError if not found.
+ */
+int av_color_primaries_from_name(const char *name);
+
 /**
  * @return the name for provided color transfer or NULL if unknown.
  */
 const char *av_color_transfer_name(enum AVColorTransferCharacteristic transfer);
 
+/**
+ * @return the AVColorTransferCharacteristic value for name or an AVError if not found.
+ */
+int av_color_transfer_from_name(const char *name);
+
 /**
  * @return the name for provided color space or NULL if unknown.
  */
 const char *av_color_space_name(enum AVColorSpace space);
 
+/**
+ * @return the AVColorSpace value for name or an AVError if not found.
+ */
+int av_color_space_from_name(const char *name);
+
 /**
  * @return the name for provided chroma location or NULL if unknown.
  */
 const char *av_chroma_location_name(enum AVChromaLocation location);
 
+/**
+ * @return the AVChromaLocation value for name or -1 if not found.
+ */
+int av_chroma_location_from_name(const char *name);
+
 /**
  * Return the pixel format corresponding to name.
  *