]> git.sesse.net Git - ffmpeg/blobdiff - libavutil/hwcontext.c
hwcontext: Move NONE to the be the first member of AVHWDeviceType
[ffmpeg] / libavutil / hwcontext.c
index ac1e2c9d67fa84a62d9da4aa24dd3755ceec0e86..ff9fe99abae21a8c938d3ca7fc4a4ea72fd84d8b 100644 (file)
 #include "pixdesc.h"
 #include "pixfmt.h"
 
-static const HWContextType *hw_table[] = {
+static const HWContextType * const hw_table[] = {
 #if CONFIG_CUDA
     &ff_hwcontext_type_cuda,
 #endif
+#if CONFIG_DXVA2
+    &ff_hwcontext_type_dxva2,
+#endif
+#if CONFIG_LIBMFX
+    &ff_hwcontext_type_qsv,
+#endif
 #if CONFIG_VAAPI
     &ff_hwcontext_type_vaapi,
 #endif
@@ -41,6 +47,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",
+};
+
+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 > AV_HWDEVICE_TYPE_NONE &&
+        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,
@@ -59,6 +107,8 @@ static void hwdevice_ctx_free(void *opaque, uint8_t *data)
     if (ctx->free)
         ctx->free(ctx);
 
+    av_buffer_unref(&ctx->internal->source_device);
+
     av_freep(&ctx->hwctx);
     av_freep(&ctx->internal->priv);
     av_freep(&ctx->internal);
@@ -151,14 +201,19 @@ static void hwframe_ctx_free(void *opaque, uint8_t *data)
 {
     AVHWFramesContext *ctx = (AVHWFramesContext*)data;
 
-    if (ctx->internal->pool_internal)
-        av_buffer_pool_uninit(&ctx->internal->pool_internal);
+    if (ctx->internal->source_frames) {
+        av_buffer_unref(&ctx->internal->source_frames);
 
-    if (ctx->internal->hw_type->frames_uninit)
-        ctx->internal->hw_type->frames_uninit(ctx);
+    } else {
+        if (ctx->internal->pool_internal)
+            av_buffer_pool_uninit(&ctx->internal->pool_internal);
 
-    if (ctx->free)
-        ctx->free(ctx);
+        if (ctx->internal->hw_type->frames_uninit)
+            ctx->internal->hw_type->frames_uninit(ctx);
+
+        if (ctx->free)
+            ctx->free(ctx);
+    }
 
     av_buffer_unref(&ctx->device_ref);
 
@@ -209,6 +264,7 @@ AVBufferRef *av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
     ctx->device_ref = device_ref;
     ctx->device_ctx = device_ctx;
     ctx->format     = AV_PIX_FMT_NONE;
+    ctx->sw_format  = AV_PIX_FMT_NONE;
 
     ctx->internal->hw_type = hw_type;
 
@@ -259,6 +315,11 @@ int av_hwframe_ctx_init(AVBufferRef *ref)
     const enum AVPixelFormat *pix_fmt;
     int ret;
 
+    if (ctx->internal->source_frames) {
+        /* A derived frame context is already initialised. */
+        return 0;
+    }
+
     /* validate the pixel format */
     for (pix_fmt = ctx->internal->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) {
         if (*pix_fmt == ctx->format)
@@ -314,6 +375,7 @@ int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref,
 
 static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
 {
+    AVHWFramesContext *ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
     AVFrame *frame_tmp;
     int ret = 0;
 
@@ -336,8 +398,8 @@ static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
         frame_tmp->format = formats[0];
         av_freep(&formats);
     }
-    frame_tmp->width  = src->width;
-    frame_tmp->height = src->height;
+    frame_tmp->width  = ctx->width;
+    frame_tmp->height = ctx->height;
 
     ret = av_frame_get_buffer(frame_tmp, 32);
     if (ret < 0)
@@ -347,6 +409,9 @@ static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
     if (ret < 0)
         goto fail;
 
+    frame_tmp->width  = src->width;
+    frame_tmp->height = src->height;
+
     av_frame_move_ref(dst, frame_tmp);
 
 fail:
@@ -385,6 +450,35 @@ int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
     AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
     int ret;
 
+    if (ctx->internal->source_frames) {
+        // This is a derived frame context, so we allocate in the source
+        // and map the frame immediately.
+        AVFrame *src_frame;
+
+        src_frame = av_frame_alloc();
+        if (!src_frame)
+            return AVERROR(ENOMEM);
+
+        ret = av_hwframe_get_buffer(ctx->internal->source_frames,
+                                    src_frame, 0);
+        if (ret < 0)
+            return ret;
+
+        ret = av_hwframe_map(frame, src_frame, 0);
+        if (ret) {
+            av_log(ctx, AV_LOG_ERROR, "Failed to map frame into derived "
+                   "frame context: %d.\n", ret);
+            av_frame_free(&src_frame);
+            return ret;
+        }
+
+        // Free the source frame immediately - the mapped frame still
+        // contains a reference to it.
+        av_frame_free(&src_frame);
+
+        return 0;
+    }
+
     if (!ctx->internal->hw_type->frames_get_buffer)
         return AVERROR(ENOSYS);
 
@@ -448,3 +542,290 @@ void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
     }
     av_freep(constraints);
 }
+
+int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type,
+                           const char *device, AVDictionary *opts, int flags)
+{
+    AVBufferRef *device_ref = NULL;
+    AVHWDeviceContext *device_ctx;
+    int ret = 0;
+
+    device_ref = av_hwdevice_ctx_alloc(type);
+    if (!device_ref) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+    device_ctx = (AVHWDeviceContext*)device_ref->data;
+
+    if (!device_ctx->internal->hw_type->device_create) {
+        ret = AVERROR(ENOSYS);
+        goto fail;
+    }
+
+    ret = device_ctx->internal->hw_type->device_create(device_ctx, device,
+                                                       opts, flags);
+    if (ret < 0)
+        goto fail;
+
+    ret = av_hwdevice_ctx_init(device_ref);
+    if (ret < 0)
+        goto fail;
+
+    *pdevice_ref = device_ref;
+    return 0;
+fail:
+    av_buffer_unref(&device_ref);
+    *pdevice_ref = NULL;
+    return ret;
+}
+
+int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr,
+                                   enum AVHWDeviceType type,
+                                   AVBufferRef *src_ref, int flags)
+{
+    AVBufferRef *dst_ref = NULL, *tmp_ref;
+    AVHWDeviceContext *dst_ctx, *tmp_ctx;
+    int ret = 0;
+
+    tmp_ref = src_ref;
+    while (tmp_ref) {
+        tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
+        if (tmp_ctx->type == type) {
+            dst_ref = av_buffer_ref(tmp_ref);
+            if (!dst_ref) {
+                ret = AVERROR(ENOMEM);
+                goto fail;
+            }
+            goto done;
+        }
+        tmp_ref = tmp_ctx->internal->source_device;
+    }
+
+    dst_ref = av_hwdevice_ctx_alloc(type);
+    if (!dst_ref) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+    dst_ctx = (AVHWDeviceContext*)dst_ref->data;
+
+    tmp_ref = src_ref;
+    while (tmp_ref) {
+        tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
+        if (dst_ctx->internal->hw_type->device_derive) {
+            ret = dst_ctx->internal->hw_type->device_derive(dst_ctx,
+                                                            tmp_ctx,
+                                                            flags);
+            if (ret == 0) {
+                dst_ctx->internal->source_device = av_buffer_ref(src_ref);
+                if (!dst_ctx->internal->source_device) {
+                    ret = AVERROR(ENOMEM);
+                    goto fail;
+                }
+                goto done;
+            }
+            if (ret != AVERROR(ENOSYS))
+                goto fail;
+        }
+        tmp_ref = tmp_ctx->internal->source_device;
+    }
+
+    ret = AVERROR(ENOSYS);
+    goto fail;
+
+done:
+    *dst_ref_ptr = dst_ref;
+    return 0;
+
+fail:
+    av_buffer_unref(&dst_ref);
+    *dst_ref_ptr = NULL;
+    return ret;
+}
+
+static void ff_hwframe_unmap(void *opaque, uint8_t *data)
+{
+    HWMapDescriptor *hwmap = (HWMapDescriptor*)data;
+    AVHWFramesContext *ctx = opaque;
+
+    if (hwmap->unmap)
+        hwmap->unmap(ctx, hwmap);
+
+    av_frame_free(&hwmap->source);
+
+    av_buffer_unref(&hwmap->hw_frames_ctx);
+
+    av_free(hwmap);
+}
+
+int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
+                          AVFrame *dst, const AVFrame *src,
+                          void (*unmap)(AVHWFramesContext *ctx,
+                                        HWMapDescriptor *hwmap),
+                          void *priv)
+{
+    AVHWFramesContext *ctx = (AVHWFramesContext*)hwframe_ref->data;
+    HWMapDescriptor *hwmap;
+    int ret;
+
+    hwmap = av_mallocz(sizeof(*hwmap));
+    if (!hwmap) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    hwmap->source = av_frame_alloc();
+    if (!hwmap->source) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+    ret = av_frame_ref(hwmap->source, src);
+    if (ret < 0)
+        goto fail;
+
+    hwmap->hw_frames_ctx = av_buffer_ref(hwframe_ref);
+    if (!hwmap->hw_frames_ctx) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    hwmap->unmap = unmap;
+    hwmap->priv  = priv;
+
+    dst->buf[0] = av_buffer_create((uint8_t*)hwmap, sizeof(*hwmap),
+                                   &ff_hwframe_unmap, ctx, 0);
+    if (!dst->buf[0]) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    return 0;
+
+fail:
+    if (hwmap) {
+        av_buffer_unref(&hwmap->hw_frames_ctx);
+        av_frame_free(&hwmap->source);
+    }
+    av_free(hwmap);
+    return ret;
+}
+
+int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
+{
+    AVHWFramesContext *src_frames, *dst_frames;
+    HWMapDescriptor *hwmap;
+    int ret;
+
+    if (src->hw_frames_ctx && dst->hw_frames_ctx) {
+        src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
+        dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
+
+        if ((src_frames == dst_frames &&
+             src->format == dst_frames->sw_format &&
+             dst->format == dst_frames->format) ||
+            (src_frames->internal->source_frames &&
+             src_frames->internal->source_frames->data ==
+             (uint8_t*)dst_frames)) {
+            // This is an unmap operation.  We don't need to directly
+            // do anything here other than fill in the original frame,
+            // because the real unmap will be invoked when the last
+            // reference to the mapped frame disappears.
+            if (!src->buf[0]) {
+                av_log(src_frames, AV_LOG_ERROR, "Invalid mapping "
+                       "found when attempting unmap.\n");
+                return AVERROR(EINVAL);
+            }
+            hwmap = (HWMapDescriptor*)src->buf[0]->data;
+            av_frame_unref(dst);
+            return av_frame_ref(dst, hwmap->source);
+        }
+    }
+
+    if (src->hw_frames_ctx) {
+        src_frames = (AVHWFramesContext*)src->hw_frames_ctx->data;
+
+        if (src_frames->format == src->format &&
+            src_frames->internal->hw_type->map_from) {
+            ret = src_frames->internal->hw_type->map_from(src_frames,
+                                                          dst, src, flags);
+            if (ret != AVERROR(ENOSYS))
+                return ret;
+        }
+    }
+
+    if (dst->hw_frames_ctx) {
+        dst_frames = (AVHWFramesContext*)dst->hw_frames_ctx->data;
+
+        if (dst_frames->format == dst->format &&
+            dst_frames->internal->hw_type->map_to) {
+            ret = dst_frames->internal->hw_type->map_to(dst_frames,
+                                                        dst, src, flags);
+            if (ret != AVERROR(ENOSYS))
+                return ret;
+        }
+    }
+
+    return AVERROR(ENOSYS);
+}
+
+int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx,
+                                  enum AVPixelFormat format,
+                                  AVBufferRef *derived_device_ctx,
+                                  AVBufferRef *source_frame_ctx,
+                                  int flags)
+{
+    AVBufferRef   *dst_ref = NULL;
+    AVHWFramesContext *dst = NULL;
+    AVHWFramesContext *src = (AVHWFramesContext*)source_frame_ctx->data;
+    int ret;
+
+    if (src->internal->source_frames) {
+        AVHWFramesContext *src_src =
+            (AVHWFramesContext*)src->internal->source_frames->data;
+        AVHWDeviceContext *dst_dev =
+            (AVHWDeviceContext*)derived_device_ctx->data;
+
+        if (src_src->device_ctx == dst_dev) {
+            // This is actually an unmapping, so we just return a
+            // reference to the source frame context.
+            *derived_frame_ctx =
+                av_buffer_ref(src->internal->source_frames);
+            if (!*derived_frame_ctx) {
+                ret = AVERROR(ENOMEM);
+                goto fail;
+            }
+            return 0;
+        }
+    }
+
+    dst_ref = av_hwframe_ctx_alloc(derived_device_ctx);
+    if (!dst_ref) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    dst = (AVHWFramesContext*)dst_ref->data;
+
+    dst->format    = format;
+    dst->sw_format = src->sw_format;
+    dst->width     = src->width;
+    dst->height    = src->height;
+
+    dst->internal->source_frames = av_buffer_ref(source_frame_ctx);
+    if (!dst->internal->source_frames) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    ret = av_hwframe_ctx_init(dst_ref);
+    if (ret)
+        goto fail;
+
+    *derived_frame_ctx = dst_ref;
+    return 0;
+
+fail:
+    if (dst)
+        av_buffer_unref(&dst->internal->source_frames);
+    av_buffer_unref(&dst_ref);
+    return ret;
+}