diff mbox

[FFmpeg-devel,04/24] hwcontext: Make it easier to work with device types

Message ID 20170612224041.6750-5-sw@jkqxz.net
State New
Headers show

Commit Message

Mark Thompson June 12, 2017, 10:40 p.m. UTC
Adds functions to convert to/from strings and a function to iterate
over all supported device types.  Also adds a new invalid type
AV_HWDEVICE_TYPE_NONE, which acts as a sentinel value.

(cherry picked from commit b7487f4f3c39b4b202e1ea7bb2de13902f2dee45)
---
 doc/APIchanges        |  4 ++++
 libavutil/hwcontext.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 libavutil/hwcontext.h | 28 ++++++++++++++++++++++++++++
 libavutil/version.h   |  2 +-
 4 files changed, 75 insertions(+), 1 deletion(-)

Comments

Michael Niedermayer June 13, 2017, 8:23 p.m. UTC | #1
On Mon, Jun 12, 2017 at 11:40:21PM +0100, Mark Thompson wrote:
> Adds functions to convert to/from strings and a function to iterate
> over all supported device types.  Also adds a new invalid type
> AV_HWDEVICE_TYPE_NONE, which acts as a sentinel value.
> 
> (cherry picked from commit b7487f4f3c39b4b202e1ea7bb2de13902f2dee45)
> ---
>  doc/APIchanges        |  4 ++++
>  libavutil/hwcontext.c | 42 ++++++++++++++++++++++++++++++++++++++++++
>  libavutil/hwcontext.h | 28 ++++++++++++++++++++++++++++
>  libavutil/version.h   |  2 +-
>  4 files changed, 75 insertions(+), 1 deletion(-)
> 
> diff --git a/doc/APIchanges b/doc/APIchanges
> index a6889f3930..5b2203f2b4 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -15,6 +15,10 @@ libavutil:     2015-08-28
>  
>  API changes, most recent first:
>  
> +2017-06-xx - xxxxxxx - lavu 55.65.100 - hwcontext.h
> +  Add AV_HWDEVICE_TYPE_NONE, av_hwdevice_find_type_by_name(),
> +  av_hwdevice_get_type_name() and av_hwdevice_iterate_types().
> +
>  2017-06-xx - xxxxxxx - lavu 55.64.100 - hwcontext.h
>    Add av_hwdevice_ctx_create_derived().
>  
> diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c
> index 86d290d322..7f9b1d33e3 100644
> --- a/libavutil/hwcontext.c
> +++ b/libavutil/hwcontext.c
> @@ -50,6 +50,48 @@ static const HWContextType *hw_table[] = {
>      NULL,
>  };
>  
> +const char *hw_type_names[] = {

was this intended to be static const ?

it lacks a prefix like av_ for a non static

[...]
Mark Thompson June 13, 2017, 9:22 p.m. UTC | #2
On 13/06/17 21:23, Michael Niedermayer wrote:
> On Mon, Jun 12, 2017 at 11:40:21PM +0100, Mark Thompson wrote:
>> Adds functions to convert to/from strings and a function to iterate
>> over all supported device types.  Also adds a new invalid type
>> AV_HWDEVICE_TYPE_NONE, which acts as a sentinel value.
>>
>> (cherry picked from commit b7487f4f3c39b4b202e1ea7bb2de13902f2dee45)
>> ---
>>  doc/APIchanges        |  4 ++++
>>  libavutil/hwcontext.c | 42 ++++++++++++++++++++++++++++++++++++++++++
>>  libavutil/hwcontext.h | 28 ++++++++++++++++++++++++++++
>>  libavutil/version.h   |  2 +-
>>  4 files changed, 75 insertions(+), 1 deletion(-)
>>
>> diff --git a/doc/APIchanges b/doc/APIchanges
>> index a6889f3930..5b2203f2b4 100644
>> --- a/doc/APIchanges
>> +++ b/doc/APIchanges
>> @@ -15,6 +15,10 @@ libavutil:     2015-08-28
>>  
>>  API changes, most recent first:
>>  
>> +2017-06-xx - xxxxxxx - lavu 55.65.100 - hwcontext.h
>> +  Add AV_HWDEVICE_TYPE_NONE, av_hwdevice_find_type_by_name(),
>> +  av_hwdevice_get_type_name() and av_hwdevice_iterate_types().
>> +
>>  2017-06-xx - xxxxxxx - lavu 55.64.100 - hwcontext.h
>>    Add av_hwdevice_ctx_create_derived().
>>  
>> diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c
>> index 86d290d322..7f9b1d33e3 100644
>> --- a/libavutil/hwcontext.c
>> +++ b/libavutil/hwcontext.c
>> @@ -50,6 +50,48 @@ static const HWContextType *hw_table[] = {
>>      NULL,
>>  };
>>  
>> +const char *hw_type_names[] = {
> 
> was this intended to be static const ?

Yes; fixed.

Thanks,

- Mark
diff mbox

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index a6889f3930..5b2203f2b4 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,10 @@  libavutil:     2015-08-28
 
 API changes, most recent first:
 
+2017-06-xx - xxxxxxx - lavu 55.65.100 - hwcontext.h
+  Add AV_HWDEVICE_TYPE_NONE, av_hwdevice_find_type_by_name(),
+  av_hwdevice_get_type_name() and av_hwdevice_iterate_types().
+
 2017-06-xx - xxxxxxx - lavu 55.64.100 - hwcontext.h
   Add av_hwdevice_ctx_create_derived().
 
diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c
index 86d290d322..7f9b1d33e3 100644
--- a/libavutil/hwcontext.c
+++ b/libavutil/hwcontext.c
@@ -50,6 +50,48 @@  static const HWContextType *hw_table[] = {
     NULL,
 };
 
+const char *hw_type_names[] = {
+    [AV_HWDEVICE_TYPE_CUDA]   = "cuda",
+    [AV_HWDEVICE_TYPE_DXVA2]  = "dxva2",
+    [AV_HWDEVICE_TYPE_QSV]    = "qsv",
+    [AV_HWDEVICE_TYPE_VAAPI]  = "vaapi",
+    [AV_HWDEVICE_TYPE_VDPAU]  = "vdpau",
+    [AV_HWDEVICE_TYPE_VIDEOTOOLBOX] = "videotoolbox",
+};
+
+enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
+{
+    int type;
+    for (type = 0; type < FF_ARRAY_ELEMS(hw_type_names); type++) {
+        if (hw_type_names[type] && !strcmp(hw_type_names[type], name))
+            return type;
+    }
+    return AV_HWDEVICE_TYPE_NONE;
+}
+
+const char *av_hwdevice_get_type_name(enum AVHWDeviceType type)
+{
+    if (type >= 0 && type < FF_ARRAY_ELEMS(hw_type_names))
+        return hw_type_names[type];
+    else
+        return NULL;
+}
+
+enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev)
+{
+    enum AVHWDeviceType next;
+    int i, set = 0;
+    for (i = 0; hw_table[i]; i++) {
+        if (prev != AV_HWDEVICE_TYPE_NONE && hw_table[i]->type <= prev)
+            continue;
+        if (!set || hw_table[i]->type < next) {
+            next = hw_table[i]->type;
+            set = 1;
+        }
+    }
+    return set ? next : AV_HWDEVICE_TYPE_NONE;
+}
+
 static const AVClass hwdevice_ctx_class = {
     .class_name = "AVHWDeviceContext",
     .item_name  = av_default_item_name,
diff --git a/libavutil/hwcontext.h b/libavutil/hwcontext.h
index 782dbf22e1..37e8831f6b 100644
--- a/libavutil/hwcontext.h
+++ b/libavutil/hwcontext.h
@@ -31,6 +31,7 @@  enum AVHWDeviceType {
     AV_HWDEVICE_TYPE_DXVA2,
     AV_HWDEVICE_TYPE_QSV,
     AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
+    AV_HWDEVICE_TYPE_NONE,
 };
 
 typedef struct AVHWDeviceInternal AVHWDeviceInternal;
@@ -224,6 +225,33 @@  typedef struct AVHWFramesContext {
 } AVHWFramesContext;
 
 /**
+ * Look up an AVHWDeviceType by name.
+ *
+ * @param name String name of the device type (case-insensitive).
+ * @return The type from enum AVHWDeviceType, or AV_HWDEVICE_TYPE_NONE if
+ *         not found.
+ */
+enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name);
+
+/** Get the string name of an AVHWDeviceType.
+ *
+ * @param type Type from enum AVHWDeviceType.
+ * @return Pointer to a static string containing the name, or NULL if the type
+ *         is not valid.
+ */
+const char *av_hwdevice_get_type_name(enum AVHWDeviceType type);
+
+/**
+ * Iterate over supported device types.
+ *
+ * @param type AV_HWDEVICE_TYPE_NONE initially, then the previous type
+ *             returned by this function in subsequent iterations.
+ * @return The next usable device type from enum AVHWDeviceType, or
+ *         AV_HWDEVICE_TYPE_NONE if there are no more.
+ */
+enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev);
+
+/**
  * Allocate an AVHWDeviceContext for a given hardware type.
  *
  * @param type the type of the hardware device to allocate.
diff --git a/libavutil/version.h b/libavutil/version.h
index dd8d2407da..322b683cf4 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -80,7 +80,7 @@ 
 
 
 #define LIBAVUTIL_VERSION_MAJOR  55
-#define LIBAVUTIL_VERSION_MINOR  64
+#define LIBAVUTIL_VERSION_MINOR  65
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \