2016-01-13 15:25:58 +02:00
|
|
|
/*
|
2016-09-28 18:30:27 +02:00
|
|
|
* This file is part of FFmpeg.
|
2016-01-13 15:25:58 +02:00
|
|
|
*
|
2016-09-28 18:30:27 +02:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2016-01-13 15:25:58 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2016-09-28 18:30:27 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2016-01-13 15:25:58 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2016-09-28 18:30:27 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2016-01-13 15:25:58 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2022-02-08 11:12:17 +02:00
|
|
|
#include <stdatomic.h>
|
2016-01-13 15:25:58 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-09-08 05:17:27 +02:00
|
|
|
#include <mfxvideo.h>
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2018-09-02 20:06:48 +02:00
|
|
|
#if HAVE_PTHREADS
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2021-08-20 23:48:03 +02:00
|
|
|
#define COBJMACROS
|
2016-01-13 15:25:58 +02:00
|
|
|
#if CONFIG_VAAPI
|
|
|
|
#include "hwcontext_vaapi.h"
|
|
|
|
#endif
|
2021-08-20 23:48:03 +02:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
#include "hwcontext_d3d11va.h"
|
|
|
|
#endif
|
2016-01-13 15:25:58 +02:00
|
|
|
#if CONFIG_DXVA2
|
2024-05-17 21:10:49 +02:00
|
|
|
#include <initguid.h>
|
2016-01-13 15:25:58 +02:00
|
|
|
#include "hwcontext_dxva2.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "hwcontext.h"
|
|
|
|
#include "hwcontext_internal.h"
|
|
|
|
#include "hwcontext_qsv.h"
|
|
|
|
#include "mem.h"
|
|
|
|
#include "pixfmt.h"
|
|
|
|
#include "pixdesc.h"
|
|
|
|
#include "time.h"
|
2021-12-23 04:26:37 +02:00
|
|
|
#include "imgutils.h"
|
2021-01-04 04:46:14 +02:00
|
|
|
#include "avassert.h"
|
2016-01-13 15:25:58 +02:00
|
|
|
|
2020-02-26 10:39:29 +02:00
|
|
|
#define QSV_VERSION_ATLEAST(MAJOR, MINOR) \
|
|
|
|
(MFX_VERSION_MAJOR > (MAJOR) || \
|
|
|
|
MFX_VERSION_MAJOR == (MAJOR) && MFX_VERSION_MINOR >= (MINOR))
|
|
|
|
|
2024-03-11 06:56:02 +02:00
|
|
|
#define QSV_RUNTIME_VERSION_ATLEAST(MFX_VERSION, MAJOR, MINOR) \
|
|
|
|
((MFX_VERSION.Major > (MAJOR)) || \
|
|
|
|
(MFX_VERSION.Major == (MAJOR) && MFX_VERSION.Minor >= (MINOR)))
|
|
|
|
|
2021-08-20 23:48:03 +02:00
|
|
|
#define MFX_IMPL_VIA_MASK(impl) (0x0f00 & (impl))
|
2020-08-19 03:43:12 +02:00
|
|
|
#define QSV_ONEVPL QSV_VERSION_ATLEAST(2, 0)
|
|
|
|
#define QSV_HAVE_OPAQUE !QSV_ONEVPL
|
2021-08-20 23:48:03 +02:00
|
|
|
|
2021-01-04 04:46:14 +02:00
|
|
|
#if QSV_ONEVPL
|
|
|
|
#include <mfxdispatcher.h>
|
|
|
|
#else
|
|
|
|
#define MFXUnload(a) do { } while(0)
|
|
|
|
#endif
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
typedef struct QSVDevicePriv {
|
|
|
|
AVBufferRef *child_device_ctx;
|
|
|
|
} QSVDevicePriv;
|
|
|
|
|
|
|
|
typedef struct QSVDeviceContext {
|
2024-02-11 17:45:08 +02:00
|
|
|
/**
|
|
|
|
* The public AVQSVDeviceContext. See hwcontext_qsv.h for it.
|
|
|
|
*/
|
|
|
|
AVQSVDeviceContext p;
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
mfxHDL handle;
|
|
|
|
mfxHandleType handle_type;
|
|
|
|
mfxVersion ver;
|
|
|
|
mfxIMPL impl;
|
|
|
|
|
|
|
|
enum AVHWDeviceType child_device_type;
|
|
|
|
enum AVPixelFormat child_pix_fmt;
|
|
|
|
} QSVDeviceContext;
|
|
|
|
|
|
|
|
typedef struct QSVFramesContext {
|
2024-02-11 20:42:08 +02:00
|
|
|
/**
|
|
|
|
* The public AVQSVFramesContext. See hwcontext_qsv.h for it.
|
|
|
|
*/
|
|
|
|
AVQSVFramesContext p;
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
mfxSession session_download;
|
2022-02-08 11:12:17 +02:00
|
|
|
atomic_int session_download_init;
|
2016-01-13 15:25:58 +02:00
|
|
|
mfxSession session_upload;
|
2022-02-08 11:12:17 +02:00
|
|
|
atomic_int session_upload_init;
|
2018-09-02 20:06:48 +02:00
|
|
|
#if HAVE_PTHREADS
|
|
|
|
pthread_mutex_t session_lock;
|
|
|
|
#endif
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
AVBufferRef *child_frames_ref;
|
|
|
|
mfxFrameSurface1 *surfaces_internal;
|
2021-08-20 23:48:03 +02:00
|
|
|
mfxHDLPair *handle_pairs_internal;
|
2016-01-13 15:25:58 +02:00
|
|
|
int nb_surfaces_used;
|
|
|
|
|
|
|
|
// used in the frame allocator for non-opaque surfaces
|
|
|
|
mfxMemId *mem_ids;
|
2020-08-19 03:43:12 +02:00
|
|
|
#if QSV_HAVE_OPAQUE
|
2016-01-13 15:25:58 +02:00
|
|
|
// used in the opaque alloc request for opaque surfaces
|
|
|
|
mfxFrameSurface1 **surface_ptrs;
|
|
|
|
|
|
|
|
mfxExtOpaqueSurfaceAlloc opaque_alloc;
|
|
|
|
mfxExtBuffer *ext_buffers[1];
|
2020-08-19 03:43:12 +02:00
|
|
|
#endif
|
2022-04-06 12:10:08 +02:00
|
|
|
AVFrame realigned_upload_frame;
|
|
|
|
AVFrame realigned_download_frame;
|
2024-05-08 08:03:09 +02:00
|
|
|
|
|
|
|
mfxFrameInfo frame_info;
|
2016-01-13 15:25:58 +02:00
|
|
|
} QSVFramesContext;
|
|
|
|
|
2024-05-08 08:03:09 +02:00
|
|
|
typedef struct QSVSurface {
|
|
|
|
mfxFrameSurface1 mfx_surface;
|
|
|
|
AVFrame *child_frame;
|
|
|
|
} QSVSurface;
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
static const struct {
|
|
|
|
enum AVPixelFormat pix_fmt;
|
|
|
|
uint32_t fourcc;
|
2022-10-06 09:35:33 +02:00
|
|
|
uint16_t mfx_shift;
|
2016-01-13 15:25:58 +02:00
|
|
|
} supported_pixel_formats[] = {
|
2022-10-06 09:35:33 +02:00
|
|
|
{ AV_PIX_FMT_NV12, MFX_FOURCC_NV12, 0 },
|
|
|
|
{ AV_PIX_FMT_BGRA, MFX_FOURCC_RGB4, 0 },
|
|
|
|
{ AV_PIX_FMT_P010, MFX_FOURCC_P010, 1 },
|
|
|
|
{ AV_PIX_FMT_PAL8, MFX_FOURCC_P8, 0 },
|
2020-02-26 10:39:29 +02:00
|
|
|
{ AV_PIX_FMT_YUYV422,
|
2022-10-06 09:35:33 +02:00
|
|
|
MFX_FOURCC_YUY2, 0 },
|
2023-02-14 04:45:59 +02:00
|
|
|
#if CONFIG_VAAPI
|
2022-03-10 08:34:22 +02:00
|
|
|
{ AV_PIX_FMT_UYVY422,
|
|
|
|
MFX_FOURCC_UYVY, 0 },
|
2023-02-14 04:45:59 +02:00
|
|
|
#endif
|
2020-02-26 10:39:29 +02:00
|
|
|
{ AV_PIX_FMT_Y210,
|
2022-10-06 09:35:33 +02:00
|
|
|
MFX_FOURCC_Y210, 1 },
|
2022-09-06 06:53:37 +02:00
|
|
|
// VUYX is used for VAAPI child device,
|
|
|
|
// the SDK only delares support for AYUV
|
|
|
|
{ AV_PIX_FMT_VUYX,
|
2022-10-06 09:35:33 +02:00
|
|
|
MFX_FOURCC_AYUV, 0 },
|
2022-10-06 09:35:35 +02:00
|
|
|
// XV30 is used for VAAPI child device,
|
|
|
|
// the SDK only delares support for Y410
|
|
|
|
{ AV_PIX_FMT_XV30,
|
|
|
|
MFX_FOURCC_Y410, 0 },
|
2022-10-06 09:35:37 +02:00
|
|
|
#if QSV_VERSION_ATLEAST(1, 31)
|
|
|
|
// P012 is used for VAAPI child device,
|
|
|
|
// the SDK only delares support for P016
|
|
|
|
{ AV_PIX_FMT_P012,
|
|
|
|
MFX_FOURCC_P016, 1 },
|
|
|
|
// Y212 is used for VAAPI child device,
|
|
|
|
// the SDK only delares support for Y216
|
|
|
|
{ AV_PIX_FMT_Y212,
|
|
|
|
MFX_FOURCC_Y216, 1 },
|
|
|
|
// XV36 is used for VAAPI child device,
|
|
|
|
// the SDK only delares support for Y416
|
|
|
|
{ AV_PIX_FMT_XV36,
|
|
|
|
MFX_FOURCC_Y416, 1 },
|
|
|
|
#endif
|
2016-01-13 15:25:58 +02:00
|
|
|
};
|
|
|
|
|
2022-01-11 08:55:38 +02:00
|
|
|
extern int ff_qsv_get_surface_base_handle(mfxFrameSurface1 *surf,
|
|
|
|
enum AVHWDeviceType base_dev_type,
|
|
|
|
void **base_handle);
|
|
|
|
|
2024-05-08 08:03:09 +02:00
|
|
|
static int qsv_init_surface(AVHWFramesContext *ctx, mfxFrameSurface1 *surf);
|
|
|
|
|
2022-01-11 08:55:38 +02:00
|
|
|
/**
|
|
|
|
* Caller needs to allocate enough space for base_handle pointer.
|
|
|
|
**/
|
|
|
|
int ff_qsv_get_surface_base_handle(mfxFrameSurface1 *surf,
|
|
|
|
enum AVHWDeviceType base_dev_type,
|
|
|
|
void **base_handle)
|
|
|
|
{
|
|
|
|
mfxHDLPair *handle_pair;
|
|
|
|
handle_pair = surf->Data.MemId;
|
|
|
|
switch (base_dev_type) {
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
case AV_HWDEVICE_TYPE_VAAPI:
|
|
|
|
base_handle[0] = handle_pair->first;
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
case AV_HWDEVICE_TYPE_D3D11VA:
|
|
|
|
base_handle[0] = handle_pair->first;
|
2022-01-29 16:37:38 +02:00
|
|
|
base_handle[1] = handle_pair->second;
|
2022-01-11 08:55:38 +02:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
case AV_HWDEVICE_TYPE_DXVA2:
|
|
|
|
base_handle[0] = handle_pair->first;
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2017-03-05 01:57:44 +02:00
|
|
|
static uint32_t qsv_fourcc_from_pix_fmt(enum AVPixelFormat pix_fmt)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++) {
|
|
|
|
if (supported_pixel_formats[i].pix_fmt == pix_fmt)
|
|
|
|
return supported_pixel_formats[i].fourcc;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-06 09:35:33 +02:00
|
|
|
static uint16_t qsv_shift_from_pix_fmt(enum AVPixelFormat pix_fmt)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++) {
|
|
|
|
if (supported_pixel_formats[i].pix_fmt == pix_fmt)
|
|
|
|
return supported_pixel_formats[i].mfx_shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-20 23:48:05 +02:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
static uint32_t qsv_get_d3d11va_bind_flags(int mem_type)
|
|
|
|
{
|
|
|
|
uint32_t bind_flags = 0;
|
|
|
|
|
|
|
|
if ((mem_type & MFX_MEMTYPE_VIDEO_MEMORY_ENCODER_TARGET) && (mem_type & MFX_MEMTYPE_INTERNAL_FRAME))
|
|
|
|
bind_flags = D3D11_BIND_DECODER | D3D11_BIND_VIDEO_ENCODER;
|
|
|
|
else
|
|
|
|
bind_flags = D3D11_BIND_DECODER;
|
|
|
|
|
|
|
|
if ((MFX_MEMTYPE_FROM_VPPOUT & mem_type) || (MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET & mem_type))
|
|
|
|
bind_flags = D3D11_BIND_RENDER_TARGET;
|
|
|
|
|
|
|
|
return bind_flags;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-23 04:26:37 +02:00
|
|
|
static int qsv_fill_border(AVFrame *dst, const AVFrame *src)
|
|
|
|
{
|
|
|
|
const AVPixFmtDescriptor *desc;
|
|
|
|
int i, planes_nb = 0;
|
|
|
|
if (dst->format != src->format)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
desc = av_pix_fmt_desc_get(dst->format);
|
|
|
|
|
|
|
|
for (i = 0; i < desc->nb_components; i++)
|
|
|
|
planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
|
|
|
|
|
|
|
|
for (i = 0; i < planes_nb; i++) {
|
|
|
|
int sheight, dheight, y;
|
|
|
|
ptrdiff_t swidth = av_image_get_linesize(src->format,
|
|
|
|
src->width,
|
|
|
|
i);
|
|
|
|
ptrdiff_t dwidth = av_image_get_linesize(dst->format,
|
|
|
|
dst->width,
|
|
|
|
i);
|
|
|
|
const AVComponentDescriptor comp = desc->comp[i];
|
|
|
|
if (swidth < 0 || dwidth < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
sheight = src->height;
|
|
|
|
dheight = dst->height;
|
|
|
|
if (i) {
|
|
|
|
sheight = AV_CEIL_RSHIFT(src->height, desc->log2_chroma_h);
|
|
|
|
dheight = AV_CEIL_RSHIFT(dst->height, desc->log2_chroma_h);
|
|
|
|
}
|
|
|
|
//fill right padding
|
|
|
|
for (y = 0; y < sheight; y++) {
|
|
|
|
void *line_ptr = dst->data[i] + y*dst->linesize[i] + swidth;
|
|
|
|
av_memcpy_backptr(line_ptr,
|
|
|
|
comp.depth > 8 ? 2 : 1,
|
|
|
|
dwidth - swidth);
|
|
|
|
}
|
|
|
|
//fill bottom padding
|
|
|
|
for (y = sheight; y < dheight; y++) {
|
|
|
|
memcpy(dst->data[i]+y*dst->linesize[i],
|
|
|
|
dst->data[i]+(sheight-1)*dst->linesize[i],
|
|
|
|
dwidth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
static int qsv_device_init(AVHWDeviceContext *ctx)
|
|
|
|
{
|
2024-02-11 17:45:08 +02:00
|
|
|
QSVDeviceContext *s = ctx->hwctx;
|
|
|
|
AVQSVDeviceContext *hwctx = &s->p;
|
2021-08-20 23:48:03 +02:00
|
|
|
int hw_handle_supported = 0;
|
|
|
|
mfxHandleType handle_type;
|
|
|
|
enum AVHWDeviceType device_type;
|
|
|
|
enum AVPixelFormat pix_fmt;
|
2016-01-13 15:25:58 +02:00
|
|
|
mfxStatus err;
|
|
|
|
|
|
|
|
err = MFXQueryIMPL(hwctx->session, &s->impl);
|
|
|
|
if (err == MFX_ERR_NONE)
|
|
|
|
err = MFXQueryVersion(hwctx->session, &s->ver);
|
|
|
|
if (err != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes\n");
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2021-08-20 23:48:03 +02:00
|
|
|
if (MFX_IMPL_VIA_VAAPI == MFX_IMPL_VIA_MASK(s->impl)) {
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
handle_type = MFX_HANDLE_VA_DISPLAY;
|
|
|
|
device_type = AV_HWDEVICE_TYPE_VAAPI;
|
|
|
|
pix_fmt = AV_PIX_FMT_VAAPI;
|
|
|
|
hw_handle_supported = 1;
|
|
|
|
#endif
|
|
|
|
} else if (MFX_IMPL_VIA_D3D11 == MFX_IMPL_VIA_MASK(s->impl)) {
|
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
handle_type = MFX_HANDLE_D3D11_DEVICE;
|
|
|
|
device_type = AV_HWDEVICE_TYPE_D3D11VA;
|
|
|
|
pix_fmt = AV_PIX_FMT_D3D11;
|
|
|
|
hw_handle_supported = 1;
|
|
|
|
#endif
|
|
|
|
} else if (MFX_IMPL_VIA_D3D9 == MFX_IMPL_VIA_MASK(s->impl)) {
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
|
|
|
|
device_type = AV_HWDEVICE_TYPE_DXVA2;
|
|
|
|
pix_fmt = AV_PIX_FMT_DXVA2_VLD;
|
|
|
|
hw_handle_supported = 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hw_handle_supported) {
|
|
|
|
err = MFXVideoCORE_GetHandle(hwctx->session, handle_type, &s->handle);
|
|
|
|
if (err == MFX_ERR_NONE) {
|
|
|
|
s->handle_type = handle_type;
|
|
|
|
s->child_device_type = device_type;
|
|
|
|
s->child_pix_fmt = pix_fmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!s->handle) {
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "No supported hw handle could be retrieved "
|
|
|
|
"from the session\n");
|
|
|
|
}
|
2016-01-13 15:25:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qsv_frames_uninit(AVHWFramesContext *ctx)
|
|
|
|
{
|
2024-02-11 20:42:08 +02:00
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
if (s->session_download) {
|
|
|
|
MFXVideoVPP_Close(s->session_download);
|
|
|
|
MFXClose(s->session_download);
|
|
|
|
}
|
|
|
|
s->session_download = NULL;
|
2018-09-02 20:06:48 +02:00
|
|
|
s->session_download_init = 0;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
if (s->session_upload) {
|
|
|
|
MFXVideoVPP_Close(s->session_upload);
|
|
|
|
MFXClose(s->session_upload);
|
|
|
|
}
|
|
|
|
s->session_upload = NULL;
|
2018-09-02 20:06:48 +02:00
|
|
|
s->session_upload_init = 0;
|
|
|
|
|
|
|
|
#if HAVE_PTHREADS
|
|
|
|
pthread_mutex_destroy(&s->session_lock);
|
|
|
|
#endif
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
av_freep(&s->mem_ids);
|
2020-08-19 03:43:12 +02:00
|
|
|
#if QSV_HAVE_OPAQUE
|
2016-01-13 15:25:58 +02:00
|
|
|
av_freep(&s->surface_ptrs);
|
2020-08-19 03:43:12 +02:00
|
|
|
#endif
|
2016-01-13 15:25:58 +02:00
|
|
|
av_freep(&s->surfaces_internal);
|
2021-08-20 23:48:03 +02:00
|
|
|
av_freep(&s->handle_pairs_internal);
|
2022-04-06 12:10:08 +02:00
|
|
|
av_frame_unref(&s->realigned_upload_frame);
|
|
|
|
av_frame_unref(&s->realigned_download_frame);
|
2016-01-13 15:25:58 +02:00
|
|
|
av_buffer_unref(&s->child_frames_ref);
|
|
|
|
}
|
|
|
|
|
2022-01-05 05:52:07 +02:00
|
|
|
static void qsv_pool_release_dummy(void *opaque, uint8_t *data)
|
2016-01-13 15:25:58 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-05-08 08:03:09 +02:00
|
|
|
static void qsv_pool_release(void *opaque, uint8_t *data)
|
|
|
|
{
|
|
|
|
AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
|
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
|
|
|
QSVSurface *qsv_surface = (QSVSurface *)data;
|
|
|
|
mfxHDLPair *hdl_pair = (mfxHDLPair *)qsv_surface->mfx_surface.Data.MemId;
|
|
|
|
AVHWFramesContext *child_frames_ctx;
|
|
|
|
|
|
|
|
if (!s->child_frames_ref)
|
|
|
|
return;
|
|
|
|
|
|
|
|
child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
|
|
|
|
if (!child_frames_ctx->device_ctx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
if (child_frames_ctx->device_ctx->type == AV_HWDEVICE_TYPE_VAAPI)
|
|
|
|
av_freep(&hdl_pair->first);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
av_freep(&hdl_pair);
|
|
|
|
av_frame_free(&qsv_surface->child_frame);
|
|
|
|
av_freep(&qsv_surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static AVBufferRef *qsv_fixed_pool_alloc(void *opaque, size_t size)
|
2016-01-13 15:25:58 +02:00
|
|
|
{
|
|
|
|
AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
|
2024-02-11 20:42:08 +02:00
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
|
|
|
AVQSVFramesContext *hwctx = &s->p;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
if (s->nb_surfaces_used < hwctx->nb_surfaces) {
|
|
|
|
s->nb_surfaces_used++;
|
|
|
|
return av_buffer_create((uint8_t*)(s->surfaces_internal + s->nb_surfaces_used - 1),
|
2022-01-05 05:52:07 +02:00
|
|
|
sizeof(*hwctx->surfaces), qsv_pool_release_dummy, NULL, 0);
|
2016-01-13 15:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-05-08 08:03:09 +02:00
|
|
|
static AVBufferRef *qsv_dynamic_pool_alloc(void *opaque, size_t size)
|
|
|
|
{
|
|
|
|
AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
|
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
|
|
|
AVHWFramesContext *child_frames_ctx;
|
|
|
|
QSVSurface *qsv_surface = NULL;
|
|
|
|
mfxHDLPair *handle_pairs_internal = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!s->child_frames_ref)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
|
|
|
|
if (!child_frames_ctx->device_ctx)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
if (child_frames_ctx->device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"QSV on dxva2 requires a fixed frame pool size\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
qsv_surface = av_calloc(1, sizeof(*qsv_surface));
|
|
|
|
if (!qsv_surface)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
qsv_surface->child_frame = av_frame_alloc();
|
|
|
|
if (!qsv_surface->child_frame)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ret = av_hwframe_get_buffer(s->child_frames_ref, qsv_surface->child_frame, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
handle_pairs_internal = av_calloc(1, sizeof(*handle_pairs_internal));
|
|
|
|
if (!handle_pairs_internal)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ret = qsv_init_surface(ctx, &qsv_surface->mfx_surface);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
if (child_frames_ctx->device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
|
|
|
|
VASurfaceID *surface_id_internal;
|
|
|
|
|
|
|
|
surface_id_internal = av_calloc(1, sizeof(*surface_id_internal));
|
|
|
|
if (!surface_id_internal)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
*surface_id_internal = (VASurfaceID)(uintptr_t)qsv_surface->child_frame->data[3];
|
|
|
|
handle_pairs_internal->first = (mfxHDL)surface_id_internal;
|
|
|
|
handle_pairs_internal->second = (mfxMemId)MFX_INFINITE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
if (child_frames_ctx->device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
|
|
|
|
AVD3D11VAFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
|
|
|
|
handle_pairs_internal->first = (mfxMemId)qsv_surface->child_frame->data[0];
|
|
|
|
|
|
|
|
if (child_frames_hwctx->BindFlags & D3D11_BIND_RENDER_TARGET)
|
|
|
|
handle_pairs_internal->second = (mfxMemId)MFX_INFINITE;
|
|
|
|
else
|
|
|
|
handle_pairs_internal->second = (mfxMemId)qsv_surface->child_frame->data[1];
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
qsv_surface->mfx_surface.Data.MemId = (mfxMemId)handle_pairs_internal;
|
|
|
|
return av_buffer_create((uint8_t *)qsv_surface, sizeof(*qsv_surface),
|
|
|
|
qsv_pool_release, ctx, 0);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (qsv_surface) {
|
|
|
|
av_frame_free(&qsv_surface->child_frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_freep(&qsv_surface);
|
|
|
|
av_freep(&handle_pairs_internal);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static AVBufferRef *qsv_pool_alloc(void *opaque, size_t size)
|
|
|
|
{
|
|
|
|
AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
|
|
|
|
AVQSVFramesContext *hwctx = ctx->hwctx;
|
|
|
|
|
|
|
|
if (hwctx->nb_surfaces == 0) {
|
|
|
|
return qsv_dynamic_pool_alloc(opaque, size);
|
|
|
|
} else {
|
|
|
|
return qsv_fixed_pool_alloc(opaque, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
static int qsv_init_child_ctx(AVHWFramesContext *ctx)
|
|
|
|
{
|
2024-02-11 17:45:08 +02:00
|
|
|
QSVDeviceContext *device_priv = ctx->device_ctx->hwctx;
|
2024-02-11 20:42:08 +02:00
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
|
|
|
AVQSVFramesContext *hwctx = &s->p;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
AVBufferRef *child_device_ref = NULL;
|
|
|
|
AVBufferRef *child_frames_ref = NULL;
|
|
|
|
|
|
|
|
AVHWDeviceContext *child_device_ctx;
|
|
|
|
AVHWFramesContext *child_frames_ctx;
|
|
|
|
|
|
|
|
int i, ret = 0;
|
|
|
|
|
|
|
|
if (!device_priv->handle) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Cannot create a non-opaque internal surface pool without "
|
|
|
|
"a hardware handle\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
child_device_ref = av_hwdevice_ctx_alloc(device_priv->child_device_type);
|
|
|
|
if (!child_device_ref)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
child_device_ctx = (AVHWDeviceContext*)child_device_ref->data;
|
|
|
|
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
|
|
|
|
AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
|
|
|
|
child_device_hwctx->display = (VADisplay)device_priv->handle;
|
|
|
|
}
|
|
|
|
#endif
|
2021-08-20 23:48:03 +02:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
if (child_device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
|
|
|
|
AVD3D11VADeviceContext *child_device_hwctx = child_device_ctx->hwctx;
|
|
|
|
ID3D11Device_AddRef((ID3D11Device*)device_priv->handle);
|
|
|
|
child_device_hwctx->device = (ID3D11Device*)device_priv->handle;
|
|
|
|
}
|
|
|
|
#endif
|
2016-01-13 15:25:58 +02:00
|
|
|
#if CONFIG_DXVA2
|
|
|
|
if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
|
|
|
|
AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
|
|
|
|
child_device_hwctx->devmgr = (IDirect3DDeviceManager9*)device_priv->handle;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = av_hwdevice_ctx_init(child_device_ref);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error initializing a child device context\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
child_frames_ref = av_hwframe_ctx_alloc(child_device_ref);
|
|
|
|
if (!child_frames_ref) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
child_frames_ctx = (AVHWFramesContext*)child_frames_ref->data;
|
|
|
|
|
|
|
|
child_frames_ctx->format = device_priv->child_pix_fmt;
|
|
|
|
child_frames_ctx->sw_format = ctx->sw_format;
|
|
|
|
child_frames_ctx->initial_pool_size = ctx->initial_pool_size;
|
2018-03-06 04:35:56 +02:00
|
|
|
child_frames_ctx->width = FFALIGN(ctx->width, 16);
|
|
|
|
child_frames_ctx->height = FFALIGN(ctx->height, 16);
|
2016-01-13 15:25:58 +02:00
|
|
|
|
2021-08-20 23:48:03 +02:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
if (child_device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
|
|
|
|
AVD3D11VAFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
|
2021-08-20 23:48:05 +02:00
|
|
|
if (hwctx->frame_type == 0)
|
|
|
|
hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
|
2021-08-20 23:48:03 +02:00
|
|
|
if (hwctx->frame_type & MFX_MEMTYPE_SHARED_RESOURCE)
|
|
|
|
child_frames_hwctx->MiscFlags = D3D11_RESOURCE_MISC_SHARED;
|
2021-08-20 23:48:05 +02:00
|
|
|
child_frames_hwctx->BindFlags = qsv_get_d3d11va_bind_flags(hwctx->frame_type);
|
2021-08-20 23:48:03 +02:00
|
|
|
}
|
|
|
|
#endif
|
2016-01-13 15:25:58 +02:00
|
|
|
#if CONFIG_DXVA2
|
|
|
|
if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
|
|
|
|
AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
|
|
|
|
if (hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)
|
|
|
|
child_frames_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
|
|
|
|
else
|
|
|
|
child_frames_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = av_hwframe_ctx_init(child_frames_ref);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error initializing a child frames context\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
if (child_device_ctx->type == AV_HWDEVICE_TYPE_VAAPI) {
|
|
|
|
AVVAAPIFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
|
2021-08-20 23:48:03 +02:00
|
|
|
for (i = 0; i < ctx->initial_pool_size; i++) {
|
|
|
|
s->handle_pairs_internal[i].first = child_frames_hwctx->surface_ids + i;
|
|
|
|
s->handle_pairs_internal[i].second = (mfxMemId)MFX_INFINITE;
|
|
|
|
s->surfaces_internal[i].Data.MemId = (mfxMemId)&s->handle_pairs_internal[i];
|
|
|
|
}
|
2016-01-13 15:25:58 +02:00
|
|
|
hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
|
|
|
|
}
|
|
|
|
#endif
|
2021-08-20 23:48:03 +02:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
if (child_device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
|
|
|
|
AVD3D11VAFramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
|
|
|
|
for (i = 0; i < ctx->initial_pool_size; i++) {
|
2021-08-20 23:48:05 +02:00
|
|
|
s->handle_pairs_internal[i].first = (mfxMemId)child_frames_hwctx->texture_infos[i].texture;
|
2021-08-20 23:48:03 +02:00
|
|
|
if(child_frames_hwctx->BindFlags & D3D11_BIND_RENDER_TARGET) {
|
|
|
|
s->handle_pairs_internal[i].second = (mfxMemId)MFX_INFINITE;
|
|
|
|
} else {
|
2021-08-20 23:48:05 +02:00
|
|
|
s->handle_pairs_internal[i].second = (mfxMemId)child_frames_hwctx->texture_infos[i].index;
|
2021-08-20 23:48:03 +02:00
|
|
|
}
|
|
|
|
s->surfaces_internal[i].Data.MemId = (mfxMemId)&s->handle_pairs_internal[i];
|
|
|
|
}
|
|
|
|
if (child_frames_hwctx->BindFlags & D3D11_BIND_RENDER_TARGET) {
|
|
|
|
hwctx->frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
|
|
|
|
} else {
|
|
|
|
hwctx->frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-01-13 15:25:58 +02:00
|
|
|
#if CONFIG_DXVA2
|
|
|
|
if (child_device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
|
|
|
|
AVDXVA2FramesContext *child_frames_hwctx = child_frames_ctx->hwctx;
|
2021-08-20 23:48:03 +02:00
|
|
|
for (i = 0; i < ctx->initial_pool_size; i++) {
|
|
|
|
s->handle_pairs_internal[i].first = (mfxMemId)child_frames_hwctx->surfaces[i];
|
|
|
|
s->handle_pairs_internal[i].second = (mfxMemId)MFX_INFINITE;
|
|
|
|
s->surfaces_internal[i].Data.MemId = (mfxMemId)&s->handle_pairs_internal[i];
|
|
|
|
}
|
2016-01-13 15:25:58 +02:00
|
|
|
if (child_frames_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
|
|
|
|
hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
|
|
|
|
else
|
|
|
|
hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
s->child_frames_ref = child_frames_ref;
|
|
|
|
child_frames_ref = NULL;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av_buffer_unref(&child_device_ref);
|
|
|
|
av_buffer_unref(&child_frames_ref);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-05 01:57:44 +02:00
|
|
|
static int qsv_init_surface(AVHWFramesContext *ctx, mfxFrameSurface1 *surf)
|
|
|
|
{
|
|
|
|
const AVPixFmtDescriptor *desc;
|
|
|
|
uint32_t fourcc;
|
|
|
|
|
|
|
|
desc = av_pix_fmt_desc_get(ctx->sw_format);
|
|
|
|
if (!desc)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
|
|
|
|
if (!fourcc)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
surf->Info.BitDepthLuma = desc->comp[0].depth;
|
|
|
|
surf->Info.BitDepthChroma = desc->comp[0].depth;
|
2022-10-06 09:35:33 +02:00
|
|
|
surf->Info.Shift = qsv_shift_from_pix_fmt(ctx->sw_format);
|
2017-03-05 01:57:44 +02:00
|
|
|
|
|
|
|
if (desc->log2_chroma_w && desc->log2_chroma_h)
|
|
|
|
surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
|
|
|
|
else if (desc->log2_chroma_w)
|
|
|
|
surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV422;
|
|
|
|
else
|
|
|
|
surf->Info.ChromaFormat = MFX_CHROMAFORMAT_YUV444;
|
|
|
|
|
|
|
|
surf->Info.FourCC = fourcc;
|
2018-03-06 04:35:56 +02:00
|
|
|
surf->Info.Width = FFALIGN(ctx->width, 16);
|
2017-03-05 01:57:44 +02:00
|
|
|
surf->Info.CropW = ctx->width;
|
2018-03-06 04:35:56 +02:00
|
|
|
surf->Info.Height = FFALIGN(ctx->height, 16);
|
2017-03-05 01:57:44 +02:00
|
|
|
surf->Info.CropH = ctx->height;
|
|
|
|
surf->Info.FrameRateExtN = 25;
|
|
|
|
surf->Info.FrameRateExtD = 1;
|
2018-02-12 10:37:16 +02:00
|
|
|
surf->Info.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
|
2017-03-05 01:57:44 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
static int qsv_init_pool(AVHWFramesContext *ctx, uint32_t fourcc)
|
|
|
|
{
|
2024-02-11 20:42:08 +02:00
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
|
|
|
AVQSVFramesContext *frames_hwctx = &s->p;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
int i, ret = 0;
|
|
|
|
|
2024-05-08 08:03:09 +02:00
|
|
|
if (ctx->initial_pool_size < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid frame pool size\n");
|
2016-01-13 15:25:58 +02:00
|
|
|
return AVERROR(EINVAL);
|
2024-05-08 08:03:09 +02:00
|
|
|
} else if (ctx->initial_pool_size == 0) {
|
|
|
|
mfxFrameSurface1 mfx_surf1;
|
|
|
|
|
|
|
|
ret = qsv_init_child_ctx(ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ffhwframesctx(ctx)->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1),
|
|
|
|
ctx, qsv_pool_alloc, NULL);
|
|
|
|
if (!ffhwframesctx(ctx)->pool_internal)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
memset(&mfx_surf1, 0, sizeof(mfx_surf1));
|
|
|
|
qsv_init_surface(ctx, &mfx_surf1);
|
|
|
|
s->frame_info = mfx_surf1.Info;
|
|
|
|
frames_hwctx->info = &s->frame_info;
|
|
|
|
frames_hwctx->nb_surfaces = 0;
|
|
|
|
|
|
|
|
return 0;
|
2016-01-13 15:25:58 +02:00
|
|
|
}
|
|
|
|
|
2021-09-14 21:31:53 +02:00
|
|
|
s->handle_pairs_internal = av_calloc(ctx->initial_pool_size,
|
|
|
|
sizeof(*s->handle_pairs_internal));
|
2021-08-20 23:48:03 +02:00
|
|
|
if (!s->handle_pairs_internal)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2021-09-14 21:31:53 +02:00
|
|
|
s->surfaces_internal = av_calloc(ctx->initial_pool_size,
|
|
|
|
sizeof(*s->surfaces_internal));
|
2016-01-13 15:25:58 +02:00
|
|
|
if (!s->surfaces_internal)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->initial_pool_size; i++) {
|
2017-03-05 01:57:44 +02:00
|
|
|
ret = qsv_init_surface(ctx, &s->surfaces_internal[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-01-13 15:25:58 +02:00
|
|
|
}
|
|
|
|
|
2020-08-19 03:43:12 +02:00
|
|
|
#if QSV_HAVE_OPAQUE
|
2016-01-13 15:25:58 +02:00
|
|
|
if (!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)) {
|
|
|
|
ret = qsv_init_child_ctx(ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2020-08-19 03:43:12 +02:00
|
|
|
#else
|
|
|
|
ret = qsv_init_child_ctx(ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
#endif
|
2016-01-13 15:25:58 +02:00
|
|
|
|
2024-02-11 23:16:32 +02:00
|
|
|
ffhwframesctx(ctx)->pool_internal = av_buffer_pool_init2(sizeof(mfxFrameSurface1),
|
|
|
|
ctx, qsv_pool_alloc, NULL);
|
|
|
|
if (!ffhwframesctx(ctx)->pool_internal)
|
2016-01-13 15:25:58 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
frames_hwctx->surfaces = s->surfaces_internal;
|
|
|
|
frames_hwctx->nb_surfaces = ctx->initial_pool_size;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static mfxStatus frame_alloc(mfxHDL pthis, mfxFrameAllocRequest *req,
|
|
|
|
mfxFrameAllocResponse *resp)
|
|
|
|
{
|
|
|
|
AVHWFramesContext *ctx = pthis;
|
2024-02-11 20:42:08 +02:00
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
|
|
|
AVQSVFramesContext *hwctx = &s->p;
|
2016-01-13 15:25:58 +02:00
|
|
|
mfxFrameInfo *i = &req->Info;
|
2024-05-08 08:03:08 +02:00
|
|
|
mfxFrameInfo *i1 = hwctx->nb_surfaces ? &hwctx->surfaces[0].Info : hwctx->info;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
if (!(req->Type & MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET) ||
|
|
|
|
!(req->Type & (MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT)) ||
|
|
|
|
!(req->Type & MFX_MEMTYPE_EXTERNAL_FRAME))
|
|
|
|
return MFX_ERR_UNSUPPORTED;
|
2019-03-08 08:41:22 +02:00
|
|
|
if (i->Width > i1->Width || i->Height > i1->Height ||
|
2016-01-13 15:25:58 +02:00
|
|
|
i->FourCC != i1->FourCC || i->ChromaFormat != i1->ChromaFormat) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Mismatching surface properties in an "
|
|
|
|
"allocation request: %dx%d %d %d vs %dx%d %d %d\n",
|
|
|
|
i->Width, i->Height, i->FourCC, i->ChromaFormat,
|
|
|
|
i1->Width, i1->Height, i1->FourCC, i1->ChromaFormat);
|
|
|
|
return MFX_ERR_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
resp->mids = s->mem_ids;
|
|
|
|
resp->NumFrameActual = hwctx->nb_surfaces;
|
|
|
|
|
|
|
|
return MFX_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static mfxStatus frame_free(mfxHDL pthis, mfxFrameAllocResponse *resp)
|
|
|
|
{
|
|
|
|
return MFX_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static mfxStatus frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
|
|
|
|
{
|
|
|
|
return MFX_ERR_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static mfxStatus frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
|
|
|
|
{
|
|
|
|
return MFX_ERR_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
|
|
|
|
{
|
2021-08-20 23:48:03 +02:00
|
|
|
mfxHDLPair *pair_dst = (mfxHDLPair*)hdl;
|
|
|
|
mfxHDLPair *pair_src = (mfxHDLPair*)mid;
|
|
|
|
|
|
|
|
pair_dst->first = pair_src->first;
|
|
|
|
|
|
|
|
if (pair_src->second != (mfxMemId)MFX_INFINITE)
|
|
|
|
pair_dst->second = pair_src->second;
|
2016-01-13 15:25:58 +02:00
|
|
|
return MFX_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2021-01-04 04:46:14 +02:00
|
|
|
#if QSV_ONEVPL
|
|
|
|
|
|
|
|
static int qsv_d3d11_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
|
|
|
|
{
|
2023-06-19 08:15:08 +02:00
|
|
|
int ret = AVERROR_UNKNOWN;
|
2021-01-04 04:46:14 +02:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
mfxStatus sts;
|
|
|
|
IDXGIAdapter *pDXGIAdapter;
|
|
|
|
DXGI_ADAPTER_DESC adapterDesc;
|
|
|
|
IDXGIDevice *pDXGIDevice = NULL;
|
|
|
|
HRESULT hr;
|
|
|
|
ID3D11Device *device = handle;
|
2024-05-26 23:34:05 +02:00
|
|
|
mfxVariant impl_value = {0};
|
2021-01-04 04:46:14 +02:00
|
|
|
|
|
|
|
hr = ID3D11Device_QueryInterface(device, &IID_IDXGIDevice, (void**)&pDXGIDevice);
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
hr = IDXGIDevice_GetAdapter(pDXGIDevice, &pDXGIAdapter);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error IDXGIDevice_GetAdapter %d\n", hr);
|
2023-06-19 08:15:08 +02:00
|
|
|
IDXGIDevice_Release(pDXGIDevice);
|
|
|
|
return ret;
|
2021-01-04 04:46:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hr = IDXGIAdapter_GetDesc(pDXGIAdapter, &adapterDesc);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error IDXGIAdapter_GetDesc %d\n", hr);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error ID3D11Device_QueryInterface %d\n", hr);
|
2023-06-19 08:15:08 +02:00
|
|
|
return ret;
|
2021-01-04 04:46:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl_value.Type = MFX_VARIANT_TYPE_U16;
|
|
|
|
impl_value.Data.U16 = adapterDesc.DeviceId;
|
|
|
|
sts = MFXSetConfigFilterProperty(cfg,
|
|
|
|
(const mfxU8 *)"mfxExtendedDeviceId.DeviceID", impl_value);
|
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration"
|
|
|
|
"DeviceID property: %d.\n", sts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_value.Type = MFX_VARIANT_TYPE_PTR;
|
|
|
|
impl_value.Data.Ptr = &adapterDesc.AdapterLuid;
|
|
|
|
sts = MFXSetConfigFilterProperty(cfg,
|
|
|
|
(const mfxU8 *)"mfxExtendedDeviceId.DeviceLUID", impl_value);
|
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration"
|
|
|
|
"DeviceLUID property: %d.\n", sts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_value.Type = MFX_VARIANT_TYPE_U32;
|
|
|
|
impl_value.Data.U32 = 0x0001;
|
|
|
|
sts = MFXSetConfigFilterProperty(cfg,
|
|
|
|
(const mfxU8 *)"mfxExtendedDeviceId.LUIDDeviceNodeMask", impl_value);
|
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration"
|
|
|
|
"LUIDDeviceNodeMask property: %d.\n", sts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2023-06-19 08:15:08 +02:00
|
|
|
ret = 0;
|
2021-01-04 04:46:14 +02:00
|
|
|
|
|
|
|
fail:
|
2023-06-19 08:15:08 +02:00
|
|
|
IDXGIAdapter_Release(pDXGIAdapter);
|
|
|
|
IDXGIDevice_Release(pDXGIDevice);
|
2021-01-04 04:46:14 +02:00
|
|
|
#endif
|
2023-06-19 08:15:08 +02:00
|
|
|
return ret;
|
2021-01-04 04:46:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_d3d9_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
|
|
|
|
{
|
|
|
|
int ret = AVERROR_UNKNOWN;
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
mfxStatus sts;
|
|
|
|
IDirect3DDeviceManager9* devmgr = handle;
|
2024-05-17 21:10:49 +02:00
|
|
|
IDirect3DDevice9 *device = NULL;
|
|
|
|
IDirect3DDevice9Ex *device_ex = NULL;
|
2021-01-04 04:46:14 +02:00
|
|
|
HANDLE device_handle = 0;
|
|
|
|
IDirect3D9Ex *d3d9ex = NULL;
|
2024-05-17 21:10:49 +02:00
|
|
|
IDirect3D9 *d3d9 = NULL;
|
2021-01-04 04:46:14 +02:00
|
|
|
LUID luid;
|
|
|
|
D3DDEVICE_CREATION_PARAMETERS params;
|
|
|
|
HRESULT hr;
|
2024-05-26 23:34:05 +02:00
|
|
|
mfxVariant impl_value = {0};
|
2021-01-04 04:46:14 +02:00
|
|
|
|
|
|
|
hr = IDirect3DDeviceManager9_OpenDeviceHandle(devmgr, &device_handle);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error OpenDeviceHandle %d\n", hr);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = IDirect3DDeviceManager9_LockDevice(devmgr, device_handle, &device, TRUE);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error LockDevice %d\n", hr);
|
2023-06-19 08:15:09 +02:00
|
|
|
IDirect3DDeviceManager9_CloseDeviceHandle(devmgr, device_handle);
|
2021-01-04 04:46:14 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2024-05-17 21:10:49 +02:00
|
|
|
hr = IDirect3DDevice9_QueryInterface(device, &IID_IDirect3DDevice9Ex, (void **)&device_ex);
|
|
|
|
IDirect3DDevice9_Release(device);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error IDirect3DDevice9_QueryInterface %d\n", hr);
|
|
|
|
goto unlock;
|
|
|
|
}
|
2021-01-04 04:46:14 +02:00
|
|
|
|
2024-05-17 21:10:49 +02:00
|
|
|
hr = IDirect3DDevice9Ex_GetCreationParameters(device_ex, ¶ms);
|
2021-01-04 04:46:14 +02:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error IDirect3DDevice9_GetCreationParameters %d\n", hr);
|
2024-05-17 21:10:49 +02:00
|
|
|
IDirect3DDevice9Ex_Release(device_ex);
|
2021-01-04 04:46:14 +02:00
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
|
2024-05-17 21:10:49 +02:00
|
|
|
hr = IDirect3DDevice9Ex_GetDirect3D(device_ex, &d3d9);
|
2021-01-04 04:46:14 +02:00
|
|
|
if (FAILED(hr)) {
|
2024-05-17 21:10:49 +02:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error IDirect3DDevice9Ex_GetDirect3D %d\n", hr);
|
|
|
|
IDirect3DDevice9Ex_Release(device_ex);
|
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
hr = IDirect3D9_QueryInterface(d3d9, &IID_IDirect3D9Ex, (void **)&d3d9ex);
|
|
|
|
IDirect3D9_Release(d3d9);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error IDirect3D9_QueryInterface3D %d\n", hr);
|
|
|
|
IDirect3DDevice9Ex_Release(device_ex);
|
2021-01-04 04:46:14 +02:00
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = IDirect3D9Ex_GetAdapterLUID(d3d9ex, params.AdapterOrdinal, &luid);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error IDirect3DDevice9Ex_GetAdapterLUID %d\n", hr);
|
2023-06-19 08:15:09 +02:00
|
|
|
goto release;
|
2021-01-04 04:46:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl_value.Type = MFX_VARIANT_TYPE_PTR;
|
|
|
|
impl_value.Data.Ptr = &luid;
|
|
|
|
sts = MFXSetConfigFilterProperty(cfg,
|
|
|
|
(const mfxU8 *)"mfxExtendedDeviceId.DeviceLUID", impl_value);
|
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration"
|
|
|
|
"DeviceLUID property: %d.\n", sts);
|
2023-06-19 08:15:09 +02:00
|
|
|
goto release;
|
2021-01-04 04:46:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2023-06-19 08:15:09 +02:00
|
|
|
release:
|
|
|
|
IDirect3D9Ex_Release(d3d9ex);
|
2024-05-17 21:10:49 +02:00
|
|
|
IDirect3DDevice9Ex_Release(device_ex);
|
2023-06-19 08:15:09 +02:00
|
|
|
|
2021-01-04 04:46:14 +02:00
|
|
|
unlock:
|
|
|
|
IDirect3DDeviceManager9_UnlockDevice(devmgr, device_handle, FALSE);
|
2023-06-19 08:15:09 +02:00
|
|
|
IDirect3DDeviceManager9_CloseDeviceHandle(devmgr, device_handle);
|
2021-01-04 04:46:14 +02:00
|
|
|
fail:
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_va_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
|
|
|
|
{
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
#if VA_CHECK_VERSION(1, 15, 0)
|
|
|
|
mfxStatus sts;
|
|
|
|
VADisplay dpy = handle;
|
|
|
|
VAStatus vas;
|
|
|
|
VADisplayAttribute attr = {
|
|
|
|
.type = VADisplayPCIID,
|
|
|
|
};
|
2024-05-26 23:34:05 +02:00
|
|
|
mfxVariant impl_value = {0};
|
2021-01-04 04:46:14 +02:00
|
|
|
|
|
|
|
vas = vaGetDisplayAttributes(dpy, &attr, 1);
|
|
|
|
if (vas == VA_STATUS_SUCCESS && attr.flags != VA_DISPLAY_ATTRIB_NOT_SUPPORTED) {
|
|
|
|
impl_value.Type = MFX_VARIANT_TYPE_U16;
|
|
|
|
impl_value.Data.U16 = (attr.value & 0xFFFF);
|
|
|
|
sts = MFXSetConfigFilterProperty(cfg,
|
2023-11-27 07:37:22 +02:00
|
|
|
(const mfxU8 *)"mfxImplDescription.mfxDeviceDescription.DeviceID", impl_value);
|
2021-01-04 04:46:14 +02:00
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration"
|
|
|
|
"DeviceID property: %d.\n", sts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "libva: Failed to get device id from the driver. Please "
|
|
|
|
"consider to upgrade the driver to support VA-API 1.15.0\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
#else
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "libva: This version of libva doesn't support retrieving "
|
|
|
|
"the device information from the driver. Please consider to upgrade libva to "
|
|
|
|
"support VA-API 1.15.0\n");
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_new_mfx_loader(void *ctx,
|
|
|
|
mfxHDL handle,
|
|
|
|
mfxHandleType handle_type,
|
|
|
|
mfxIMPL implementation,
|
|
|
|
mfxVersion *pver,
|
|
|
|
void **ploader)
|
|
|
|
{
|
|
|
|
mfxStatus sts;
|
|
|
|
mfxLoader loader = NULL;
|
|
|
|
mfxConfig cfg;
|
2024-05-26 23:34:05 +02:00
|
|
|
mfxVariant impl_value = {0};
|
2021-01-04 04:46:14 +02:00
|
|
|
|
|
|
|
*ploader = NULL;
|
|
|
|
loader = MFXLoad();
|
|
|
|
if (!loader) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error creating a MFX loader\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create configurations for implementation */
|
|
|
|
cfg = MFXCreateConfig(loader);
|
|
|
|
if (!cfg) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error creating a MFX configuration\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_value.Type = MFX_VARIANT_TYPE_U32;
|
|
|
|
impl_value.Data.U32 = (implementation == MFX_IMPL_SOFTWARE) ?
|
|
|
|
MFX_IMPL_TYPE_SOFTWARE : MFX_IMPL_TYPE_HARDWARE;
|
|
|
|
sts = MFXSetConfigFilterProperty(cfg,
|
|
|
|
(const mfxU8 *)"mfxImplDescription.Impl", impl_value);
|
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration "
|
|
|
|
"property: %d.\n", sts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_value.Type = MFX_VARIANT_TYPE_U32;
|
|
|
|
impl_value.Data.U32 = pver->Version;
|
|
|
|
sts = MFXSetConfigFilterProperty(cfg,
|
|
|
|
(const mfxU8 *)"mfxImplDescription.ApiVersion.Version",
|
|
|
|
impl_value);
|
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration "
|
|
|
|
"property: %d.\n", sts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2023-11-27 07:37:22 +02:00
|
|
|
impl_value.Type = MFX_VARIANT_TYPE_U32;
|
|
|
|
impl_value.Data.U32 = 0x8086; // Intel device only
|
2021-01-04 04:46:14 +02:00
|
|
|
sts = MFXSetConfigFilterProperty(cfg,
|
2023-11-27 07:37:22 +02:00
|
|
|
(const mfxU8 *)"mfxImplDescription.VendorID", impl_value);
|
2021-01-04 04:46:14 +02:00
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration"
|
|
|
|
"VendorID property: %d.\n", sts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MFX_HANDLE_VA_DISPLAY == handle_type) {
|
|
|
|
if (handle && qsv_va_update_config(ctx, handle, cfg))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
impl_value.Data.U32 = MFX_ACCEL_MODE_VIA_VAAPI;
|
|
|
|
} else if (MFX_HANDLE_D3D9_DEVICE_MANAGER == handle_type) {
|
|
|
|
if (handle && qsv_d3d9_update_config(ctx, handle, cfg))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
impl_value.Data.U32 = MFX_ACCEL_MODE_VIA_D3D9;
|
|
|
|
} else {
|
|
|
|
if (handle && qsv_d3d11_update_config(ctx, handle, cfg))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
impl_value.Data.U32 = MFX_ACCEL_MODE_VIA_D3D11;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_value.Type = MFX_VARIANT_TYPE_U32;
|
|
|
|
sts = MFXSetConfigFilterProperty(cfg,
|
|
|
|
(const mfxU8 *)"mfxImplDescription.AccelerationMode", impl_value);
|
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error adding a MFX configuration"
|
|
|
|
"AccelerationMode property: %d.\n", sts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ploader = loader;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (loader)
|
|
|
|
MFXUnload(loader);
|
|
|
|
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_create_mfx_session_from_loader(void *ctx, mfxLoader loader, mfxSession *psession)
|
|
|
|
{
|
|
|
|
mfxStatus sts;
|
|
|
|
mfxSession session = NULL;
|
|
|
|
uint32_t impl_idx = 0;
|
|
|
|
mfxVersion ver;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
/* Enumerate all implementations */
|
|
|
|
mfxImplDescription *impl_desc;
|
|
|
|
|
|
|
|
sts = MFXEnumImplementations(loader, impl_idx,
|
|
|
|
MFX_IMPLCAPS_IMPLDESCSTRUCTURE,
|
|
|
|
(mfxHDL *)&impl_desc);
|
|
|
|
/* Failed to find an available implementation */
|
|
|
|
if (sts == MFX_ERR_NOT_FOUND)
|
|
|
|
break;
|
|
|
|
else if (sts != MFX_ERR_NONE) {
|
|
|
|
impl_idx++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sts = MFXCreateSession(loader, impl_idx, &session);
|
|
|
|
MFXDispReleaseImplDescription(loader, impl_desc);
|
|
|
|
if (sts == MFX_ERR_NONE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
impl_idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error creating a MFX session: %d.\n", sts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
sts = MFXQueryVersion(session, &ver);
|
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error querying a MFX session: %d.\n", sts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "Initialize MFX session: implementation "
|
|
|
|
"version is %d.%d\n", ver.Major, ver.Minor);
|
|
|
|
|
|
|
|
*psession = session;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (session)
|
|
|
|
MFXClose(session);
|
|
|
|
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_create_mfx_session(void *ctx,
|
|
|
|
mfxHDL handle,
|
|
|
|
mfxHandleType handle_type,
|
|
|
|
mfxIMPL implementation,
|
|
|
|
mfxVersion *pver,
|
|
|
|
mfxSession *psession,
|
|
|
|
void **ploader)
|
|
|
|
{
|
|
|
|
mfxLoader loader = NULL;
|
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE,
|
|
|
|
"Use Intel(R) oneVPL to create MFX session, API version is "
|
|
|
|
"%d.%d, the required implementation version is %d.%d\n",
|
|
|
|
MFX_VERSION_MAJOR, MFX_VERSION_MINOR, pver->Major, pver->Minor);
|
|
|
|
|
|
|
|
if (handle_type != MFX_HANDLE_VA_DISPLAY &&
|
|
|
|
handle_type != MFX_HANDLE_D3D9_DEVICE_MANAGER &&
|
|
|
|
handle_type != MFX_HANDLE_D3D11_DEVICE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Invalid MFX device handle type\n");
|
|
|
|
return AVERROR(EXDEV);
|
|
|
|
}
|
|
|
|
|
|
|
|
*psession = NULL;
|
|
|
|
|
|
|
|
if (!*ploader) {
|
|
|
|
if (qsv_new_mfx_loader(ctx, handle, handle_type, implementation, pver, (void **)&loader))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
av_assert0(loader);
|
|
|
|
} else
|
|
|
|
loader = *ploader; // Use the input mfxLoader to create mfx session
|
|
|
|
|
|
|
|
if (qsv_create_mfx_session_from_loader(ctx, loader, psession))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!*ploader)
|
|
|
|
*ploader = loader;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (!*ploader && loader)
|
|
|
|
MFXUnload(loader);
|
|
|
|
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static int qsv_create_mfx_session(void *ctx,
|
|
|
|
mfxHDL handle,
|
|
|
|
mfxHandleType handle_type,
|
|
|
|
mfxIMPL implementation,
|
|
|
|
mfxVersion *pver,
|
|
|
|
mfxSession *psession,
|
|
|
|
void **ploader)
|
|
|
|
{
|
|
|
|
mfxVersion ver;
|
|
|
|
mfxStatus sts;
|
|
|
|
mfxSession session = NULL;
|
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE,
|
|
|
|
"Use Intel(R) Media SDK to create MFX session, API version is "
|
|
|
|
"%d.%d, the required implementation version is %d.%d\n",
|
|
|
|
MFX_VERSION_MAJOR, MFX_VERSION_MINOR, pver->Major, pver->Minor);
|
|
|
|
|
|
|
|
*ploader = NULL;
|
|
|
|
*psession = NULL;
|
|
|
|
ver = *pver;
|
|
|
|
sts = MFXInit(implementation, &ver, &session);
|
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session: "
|
|
|
|
"%d.\n", sts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
sts = MFXQueryVersion(session, &ver);
|
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error querying an MFX session: "
|
|
|
|
"%d.\n", sts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "Initialize MFX session: implementation "
|
|
|
|
"version is %d.%d\n", ver.Major, ver.Minor);
|
|
|
|
|
|
|
|
MFXClose(session);
|
|
|
|
|
|
|
|
sts = MFXInit(implementation, &ver, &session);
|
|
|
|
if (sts != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session: "
|
|
|
|
"%d.\n", sts);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
*psession = session;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (session)
|
|
|
|
MFXClose(session);
|
|
|
|
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
static int qsv_init_internal_session(AVHWFramesContext *ctx,
|
|
|
|
mfxSession *session, int upload)
|
|
|
|
{
|
2024-03-24 22:29:44 +02:00
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
|
|
|
AVQSVFramesContext *frames_hwctx = &s->p;
|
2024-02-11 17:45:08 +02:00
|
|
|
QSVDeviceContext *device_priv = ctx->device_ctx->hwctx;
|
|
|
|
AVQSVDeviceContext *hwctx = &device_priv->p;
|
2020-08-19 03:43:12 +02:00
|
|
|
int opaque = 0;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
mfxFrameAllocator frame_allocator = {
|
|
|
|
.pthis = ctx,
|
|
|
|
.Alloc = frame_alloc,
|
|
|
|
.Lock = frame_lock,
|
|
|
|
.Unlock = frame_unlock,
|
|
|
|
.GetHDL = frame_get_hdl,
|
|
|
|
.Free = frame_free,
|
|
|
|
};
|
|
|
|
|
|
|
|
mfxVideoParam par;
|
|
|
|
mfxStatus err;
|
2021-01-04 04:46:14 +02:00
|
|
|
int ret = AVERROR_UNKNOWN;
|
|
|
|
/* hwctx->loader is non-NULL for oneVPL user and NULL for non-oneVPL user */
|
|
|
|
void **loader = &hwctx->loader;
|
2024-03-11 06:56:02 +02:00
|
|
|
mfxSession parent_session = hwctx->session;
|
|
|
|
mfxIMPL impl;
|
|
|
|
mfxVersion ver;
|
|
|
|
|
|
|
|
err = MFXQueryIMPL(parent_session, &impl);
|
|
|
|
if (err == MFX_ERR_NONE)
|
|
|
|
err = MFXQueryVersion(parent_session, &ver);
|
|
|
|
if (err != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error querying the session attributes.\n");
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
2016-01-13 15:25:58 +02:00
|
|
|
|
2020-08-19 03:43:12 +02:00
|
|
|
#if QSV_HAVE_OPAQUE
|
|
|
|
opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
|
|
|
|
#endif
|
|
|
|
|
2021-01-04 04:46:14 +02:00
|
|
|
ret = qsv_create_mfx_session(ctx, device_priv->handle, device_priv->handle_type,
|
|
|
|
device_priv->impl, &device_priv->ver, session, loader);
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
if (device_priv->handle) {
|
|
|
|
err = MFXVideoCORE_SetHandle(*session, device_priv->handle_type,
|
|
|
|
device_priv->handle);
|
2021-01-04 04:46:14 +02:00
|
|
|
if (err != MFX_ERR_NONE) {
|
|
|
|
ret = AVERROR_UNKNOWN;
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-01-13 15:25:58 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 06:56:02 +02:00
|
|
|
if (QSV_RUNTIME_VERSION_ATLEAST(ver, 1, 25)) {
|
|
|
|
err = MFXJoinSession(parent_session, *session);
|
|
|
|
if (err != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error joining session.\n");
|
|
|
|
ret = AVERROR_UNKNOWN;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
if (!opaque) {
|
|
|
|
err = MFXVideoCORE_SetFrameAllocator(*session, &frame_allocator);
|
2021-01-04 04:46:14 +02:00
|
|
|
if (err != MFX_ERR_NONE) {
|
|
|
|
ret = AVERROR_UNKNOWN;
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-01-13 15:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&par, 0, sizeof(par));
|
|
|
|
|
2020-08-19 03:43:12 +02:00
|
|
|
if (!opaque) {
|
|
|
|
par.IOPattern = upload ? MFX_IOPATTERN_OUT_VIDEO_MEMORY :
|
|
|
|
MFX_IOPATTERN_IN_VIDEO_MEMORY;
|
|
|
|
}
|
|
|
|
#if QSV_HAVE_OPAQUE
|
|
|
|
else {
|
2016-01-13 15:25:58 +02:00
|
|
|
par.ExtParam = s->ext_buffers;
|
|
|
|
par.NumExtParam = FF_ARRAY_ELEMS(s->ext_buffers);
|
|
|
|
par.IOPattern = upload ? MFX_IOPATTERN_OUT_OPAQUE_MEMORY :
|
|
|
|
MFX_IOPATTERN_IN_OPAQUE_MEMORY;
|
|
|
|
}
|
2020-08-19 03:43:12 +02:00
|
|
|
#endif
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
par.IOPattern |= upload ? MFX_IOPATTERN_IN_SYSTEM_MEMORY :
|
|
|
|
MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
|
|
|
|
par.AsyncDepth = 1;
|
|
|
|
|
2024-05-08 08:03:08 +02:00
|
|
|
par.vpp.In = frames_hwctx->nb_surfaces ? frames_hwctx->surfaces[0].Info : *frames_hwctx->info;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
/* Apparently VPP requires the frame rate to be set to some value, otherwise
|
|
|
|
* init will fail (probably for the framerate conversion filter). Since we
|
|
|
|
* are only doing data upload/download here, we just invent an arbitrary
|
|
|
|
* value */
|
|
|
|
par.vpp.In.FrameRateExtN = 25;
|
|
|
|
par.vpp.In.FrameRateExtD = 1;
|
|
|
|
par.vpp.Out = par.vpp.In;
|
|
|
|
|
|
|
|
err = MFXVideoVPP_Init(*session, &par);
|
|
|
|
if (err != MFX_ERR_NONE) {
|
2016-08-10 12:54:31 +02:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "Error opening the internal VPP session."
|
|
|
|
"Surface upload/download will not be possible\n");
|
2021-01-04 04:46:14 +02:00
|
|
|
|
|
|
|
ret = AVERROR_UNKNOWN;
|
|
|
|
goto fail;
|
2016-01-13 15:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2021-01-04 04:46:14 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (*session)
|
|
|
|
MFXClose(*session);
|
|
|
|
|
|
|
|
*session = NULL;
|
|
|
|
|
|
|
|
return ret;
|
2016-01-13 15:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_frames_init(AVHWFramesContext *ctx)
|
|
|
|
{
|
2024-02-11 20:42:08 +02:00
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
|
|
|
AVQSVFramesContext *frames_hwctx = &s->p;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
2020-08-19 03:43:12 +02:00
|
|
|
int opaque = 0;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
2017-03-05 01:57:44 +02:00
|
|
|
uint32_t fourcc;
|
2016-01-13 15:25:58 +02:00
|
|
|
int i, ret;
|
|
|
|
|
2020-08-19 03:43:12 +02:00
|
|
|
#if QSV_HAVE_OPAQUE
|
|
|
|
opaque = !!(frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME);
|
|
|
|
#endif
|
|
|
|
|
2017-03-05 01:57:44 +02:00
|
|
|
fourcc = qsv_fourcc_from_pix_fmt(ctx->sw_format);
|
2016-01-13 15:25:58 +02:00
|
|
|
if (!fourcc) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format\n");
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ctx->pool) {
|
|
|
|
ret = qsv_init_pool(ctx, fourcc);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 03:43:12 +02:00
|
|
|
if (!opaque) {
|
|
|
|
s->mem_ids = av_calloc(frames_hwctx->nb_surfaces, sizeof(*s->mem_ids));
|
|
|
|
if (!s->mem_ids)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (i = 0; i < frames_hwctx->nb_surfaces; i++)
|
|
|
|
s->mem_ids[i] = frames_hwctx->surfaces[i].Data.MemId;
|
|
|
|
}
|
|
|
|
#if QSV_HAVE_OPAQUE
|
|
|
|
else {
|
2021-09-14 21:31:53 +02:00
|
|
|
s->surface_ptrs = av_calloc(frames_hwctx->nb_surfaces,
|
|
|
|
sizeof(*s->surface_ptrs));
|
2016-01-13 15:25:58 +02:00
|
|
|
if (!s->surface_ptrs)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (i = 0; i < frames_hwctx->nb_surfaces; i++)
|
|
|
|
s->surface_ptrs[i] = frames_hwctx->surfaces + i;
|
|
|
|
|
|
|
|
s->opaque_alloc.In.Surfaces = s->surface_ptrs;
|
|
|
|
s->opaque_alloc.In.NumSurface = frames_hwctx->nb_surfaces;
|
|
|
|
s->opaque_alloc.In.Type = frames_hwctx->frame_type;
|
|
|
|
|
|
|
|
s->opaque_alloc.Out = s->opaque_alloc.In;
|
|
|
|
|
|
|
|
s->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
|
|
|
|
s->opaque_alloc.Header.BufferSz = sizeof(s->opaque_alloc);
|
|
|
|
|
|
|
|
s->ext_buffers[0] = (mfxExtBuffer*)&s->opaque_alloc;
|
|
|
|
}
|
2020-08-19 03:43:12 +02:00
|
|
|
#endif
|
2016-01-13 15:25:58 +02:00
|
|
|
|
2018-09-02 20:06:48 +02:00
|
|
|
s->session_download = NULL;
|
|
|
|
s->session_upload = NULL;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
2018-09-02 20:06:48 +02:00
|
|
|
s->session_download_init = 0;
|
|
|
|
s->session_upload_init = 0;
|
|
|
|
|
|
|
|
#if HAVE_PTHREADS
|
|
|
|
pthread_mutex_init(&s->session_lock, NULL);
|
|
|
|
#endif
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
|
|
|
|
{
|
|
|
|
frame->buf[0] = av_buffer_pool_get(ctx->pool);
|
|
|
|
if (!frame->buf[0])
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
frame->data[3] = frame->buf[0]->data;
|
|
|
|
frame->format = AV_PIX_FMT_QSV;
|
|
|
|
frame->width = ctx->width;
|
|
|
|
frame->height = ctx->height;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_transfer_get_formats(AVHWFramesContext *ctx,
|
|
|
|
enum AVHWFrameTransferDirection dir,
|
|
|
|
enum AVPixelFormat **formats)
|
|
|
|
{
|
|
|
|
enum AVPixelFormat *fmts;
|
|
|
|
|
|
|
|
fmts = av_malloc_array(2, sizeof(*fmts));
|
|
|
|
if (!fmts)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
fmts[0] = ctx->sw_format;
|
|
|
|
fmts[1] = AV_PIX_FMT_NONE;
|
|
|
|
|
|
|
|
*formats = fmts;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 01:57:45 +02:00
|
|
|
static int qsv_frames_derive_from(AVHWFramesContext *dst_ctx,
|
|
|
|
AVHWFramesContext *src_ctx, int flags)
|
|
|
|
{
|
|
|
|
AVQSVFramesContext *src_hwctx = src_ctx->hwctx;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
switch (dst_ctx->device_ctx->type) {
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
case AV_HWDEVICE_TYPE_VAAPI:
|
|
|
|
{
|
|
|
|
AVVAAPIFramesContext *dst_hwctx = dst_ctx->hwctx;
|
2021-09-14 21:31:53 +02:00
|
|
|
dst_hwctx->surface_ids = av_calloc(src_hwctx->nb_surfaces,
|
|
|
|
sizeof(*dst_hwctx->surface_ids));
|
2017-03-05 01:57:45 +02:00
|
|
|
if (!dst_hwctx->surface_ids)
|
|
|
|
return AVERROR(ENOMEM);
|
2021-08-20 23:48:03 +02:00
|
|
|
for (i = 0; i < src_hwctx->nb_surfaces; i++) {
|
|
|
|
mfxHDLPair *pair = (mfxHDLPair*)src_hwctx->surfaces[i].Data.MemId;
|
|
|
|
dst_hwctx->surface_ids[i] = *(VASurfaceID*)pair->first;
|
|
|
|
}
|
2017-03-05 01:57:45 +02:00
|
|
|
dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2021-08-20 23:48:03 +02:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
case AV_HWDEVICE_TYPE_D3D11VA:
|
|
|
|
{
|
2022-05-07 08:24:39 +02:00
|
|
|
D3D11_TEXTURE2D_DESC texDesc;
|
2024-05-17 21:10:49 +02:00
|
|
|
AVD3D11VAFramesContext *dst_hwctx;
|
2022-05-07 08:24:39 +02:00
|
|
|
dst_ctx->initial_pool_size = src_ctx->initial_pool_size;
|
2024-05-17 21:10:49 +02:00
|
|
|
dst_hwctx = dst_ctx->hwctx;
|
2022-05-07 08:24:39 +02:00
|
|
|
dst_hwctx->texture_infos = av_calloc(src_hwctx->nb_surfaces,
|
|
|
|
sizeof(*dst_hwctx->texture_infos));
|
|
|
|
if (!dst_hwctx->texture_infos)
|
|
|
|
return AVERROR(ENOMEM);
|
2021-08-20 23:48:03 +02:00
|
|
|
if (src_hwctx->frame_type & MFX_MEMTYPE_SHARED_RESOURCE)
|
|
|
|
dst_hwctx->MiscFlags = D3D11_RESOURCE_MISC_SHARED;
|
2022-05-07 08:24:39 +02:00
|
|
|
for (i = 0; i < src_hwctx->nb_surfaces; i++) {
|
|
|
|
mfxHDLPair *pair = (mfxHDLPair*)src_hwctx->surfaces[i].Data.MemId;
|
|
|
|
dst_hwctx->texture_infos[i].texture = (ID3D11Texture2D*)pair->first;
|
|
|
|
dst_hwctx->texture_infos[i].index = pair->second == (mfxMemId)MFX_INFINITE ? (intptr_t)0 : (intptr_t)pair->second;
|
|
|
|
}
|
2024-07-23 03:27:02 +02:00
|
|
|
if (src_hwctx->nb_surfaces) {
|
|
|
|
ID3D11Texture2D_GetDesc(dst_hwctx->texture_infos[0].texture, &texDesc);
|
|
|
|
dst_hwctx->BindFlags = texDesc.BindFlags;
|
|
|
|
} else
|
|
|
|
dst_hwctx->BindFlags = qsv_get_d3d11va_bind_flags(src_hwctx->frame_type);
|
2021-08-20 23:48:03 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2017-03-05 01:57:45 +02:00
|
|
|
#if CONFIG_DXVA2
|
|
|
|
case AV_HWDEVICE_TYPE_DXVA2:
|
|
|
|
{
|
|
|
|
AVDXVA2FramesContext *dst_hwctx = dst_ctx->hwctx;
|
2021-09-14 21:31:53 +02:00
|
|
|
dst_hwctx->surfaces = av_calloc(src_hwctx->nb_surfaces,
|
|
|
|
sizeof(*dst_hwctx->surfaces));
|
2017-03-05 01:57:45 +02:00
|
|
|
if (!dst_hwctx->surfaces)
|
|
|
|
return AVERROR(ENOMEM);
|
2021-08-20 23:48:03 +02:00
|
|
|
for (i = 0; i < src_hwctx->nb_surfaces; i++) {
|
|
|
|
mfxHDLPair *pair = (mfxHDLPair*)src_hwctx->surfaces[i].Data.MemId;
|
|
|
|
dst_hwctx->surfaces[i] = (IDirect3DSurface9*)pair->first;
|
|
|
|
}
|
2017-03-05 01:57:45 +02:00
|
|
|
dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
|
|
|
|
if (src_hwctx->frame_type == MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
|
|
|
|
dst_hwctx->surface_type = DXVA2_VideoDecoderRenderTarget;
|
|
|
|
else
|
|
|
|
dst_hwctx->surface_type = DXVA2_VideoProcessorRenderTarget;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-06 11:33:15 +02:00
|
|
|
static int qsv_map_from(AVHWFramesContext *ctx,
|
|
|
|
AVFrame *dst, const AVFrame *src, int flags)
|
|
|
|
{
|
2024-02-11 20:42:08 +02:00
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
2016-10-06 11:33:15 +02:00
|
|
|
mfxFrameSurface1 *surf = (mfxFrameSurface1*)src->data[3];
|
|
|
|
AVHWFramesContext *child_frames_ctx;
|
2017-03-05 01:57:45 +02:00
|
|
|
const AVPixFmtDescriptor *desc;
|
|
|
|
uint8_t *child_data;
|
2016-10-06 11:33:15 +02:00
|
|
|
AVFrame *dummy;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!s->child_frames_ref)
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
|
|
|
|
|
2017-03-05 01:57:45 +02:00
|
|
|
switch (child_frames_ctx->device_ctx->type) {
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
case AV_HWDEVICE_TYPE_VAAPI:
|
2021-08-20 23:48:03 +02:00
|
|
|
{
|
|
|
|
mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
|
2021-09-13 08:22:22 +02:00
|
|
|
/* pair->first is *VASurfaceID while data[3] in vaapi frame is VASurfaceID, so
|
|
|
|
* we need this casting for vaapi.
|
|
|
|
* Add intptr_t to force cast from VASurfaceID(uint) type to pointer(long) type
|
|
|
|
* to avoid compile warning */
|
|
|
|
child_data = (uint8_t*)(intptr_t)*(VASurfaceID*)pair->first;
|
2017-03-05 01:57:45 +02:00
|
|
|
break;
|
2021-08-20 23:48:03 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
case AV_HWDEVICE_TYPE_D3D11VA:
|
|
|
|
{
|
|
|
|
mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
|
|
|
|
child_data = pair->first;
|
|
|
|
break;
|
|
|
|
}
|
2017-03-05 01:57:45 +02:00
|
|
|
#endif
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
case AV_HWDEVICE_TYPE_DXVA2:
|
2021-08-20 23:48:03 +02:00
|
|
|
{
|
|
|
|
mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
|
|
|
|
child_data = pair->first;
|
2017-03-05 01:57:45 +02:00
|
|
|
break;
|
2021-08-20 23:48:03 +02:00
|
|
|
}
|
2017-03-05 01:57:45 +02:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dst->format == child_frames_ctx->format) {
|
|
|
|
ret = ff_hwframe_map_create(s->child_frames_ref,
|
|
|
|
dst, src, NULL, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
dst->width = src->width;
|
|
|
|
dst->height = src->height;
|
2021-08-20 23:48:03 +02:00
|
|
|
|
|
|
|
if (child_frames_ctx->device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
|
|
|
|
mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
|
|
|
|
dst->data[0] = pair->first;
|
2022-05-07 08:24:41 +02:00
|
|
|
dst->data[1] = pair->second == (mfxMemId)MFX_INFINITE ? (uint8_t *)0 : pair->second;
|
2021-08-20 23:48:03 +02:00
|
|
|
} else {
|
|
|
|
dst->data[3] = child_data;
|
|
|
|
}
|
2017-03-05 01:57:45 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
desc = av_pix_fmt_desc_get(dst->format);
|
|
|
|
if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
|
|
|
|
// This only supports mapping to software.
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
|
2016-10-06 11:33:15 +02:00
|
|
|
dummy = av_frame_alloc();
|
|
|
|
if (!dummy)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
dummy->buf[0] = av_buffer_ref(src->buf[0]);
|
|
|
|
dummy->hw_frames_ctx = av_buffer_ref(s->child_frames_ref);
|
|
|
|
if (!dummy->buf[0] || !dummy->hw_frames_ctx)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
dummy->format = child_frames_ctx->format;
|
|
|
|
dummy->width = src->width;
|
|
|
|
dummy->height = src->height;
|
2021-08-20 23:48:03 +02:00
|
|
|
|
|
|
|
if (child_frames_ctx->device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
|
|
|
|
mfxHDLPair *pair = (mfxHDLPair*)surf->Data.MemId;
|
|
|
|
dummy->data[0] = pair->first;
|
2022-05-07 08:24:41 +02:00
|
|
|
dummy->data[1] = pair->second == (mfxMemId)MFX_INFINITE ? (uint8_t *)0 : pair->second;
|
2021-08-20 23:48:03 +02:00
|
|
|
} else {
|
|
|
|
dummy->data[3] = child_data;
|
|
|
|
}
|
2016-10-06 11:33:15 +02:00
|
|
|
|
|
|
|
ret = av_hwframe_map(dst, dummy, flags);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av_frame_free(&dummy);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-10 14:58:08 +02:00
|
|
|
static int qsv_transfer_data_child(AVHWFramesContext *ctx, AVFrame *dst,
|
|
|
|
const AVFrame *src)
|
|
|
|
{
|
2024-02-11 20:42:08 +02:00
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
2016-08-10 14:58:08 +02:00
|
|
|
AVHWFramesContext *child_frames_ctx = (AVHWFramesContext*)s->child_frames_ref->data;
|
|
|
|
int download = !!src->hw_frames_ctx;
|
|
|
|
mfxFrameSurface1 *surf = (mfxFrameSurface1*)(download ? src->data[3] : dst->data[3]);
|
|
|
|
|
|
|
|
AVFrame *dummy;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
dummy = av_frame_alloc();
|
|
|
|
if (!dummy)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
dummy->format = child_frames_ctx->format;
|
|
|
|
dummy->width = src->width;
|
|
|
|
dummy->height = src->height;
|
|
|
|
dummy->buf[0] = download ? src->buf[0] : dst->buf[0];
|
|
|
|
dummy->data[3] = surf->Data.MemId;
|
|
|
|
dummy->hw_frames_ctx = s->child_frames_ref;
|
|
|
|
|
|
|
|
ret = download ? av_hwframe_transfer_data(dst, dummy, 0) :
|
|
|
|
av_hwframe_transfer_data(dummy, src, 0);
|
|
|
|
|
|
|
|
dummy->buf[0] = NULL;
|
|
|
|
dummy->data[3] = NULL;
|
|
|
|
dummy->hw_frames_ctx = NULL;
|
|
|
|
|
|
|
|
av_frame_free(&dummy);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-21 11:58:52 +02:00
|
|
|
static int map_frame_to_surface(const AVFrame *frame, mfxFrameSurface1 *surface)
|
|
|
|
{
|
|
|
|
switch (frame->format) {
|
|
|
|
case AV_PIX_FMT_NV12:
|
|
|
|
case AV_PIX_FMT_P010:
|
2022-10-06 09:35:37 +02:00
|
|
|
case AV_PIX_FMT_P012:
|
2018-05-21 11:58:52 +02:00
|
|
|
surface->Data.Y = frame->data[0];
|
|
|
|
surface->Data.UV = frame->data[1];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AV_PIX_FMT_YUV420P:
|
|
|
|
surface->Data.Y = frame->data[0];
|
|
|
|
surface->Data.U = frame->data[1];
|
|
|
|
surface->Data.V = frame->data[2];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AV_PIX_FMT_BGRA:
|
|
|
|
surface->Data.B = frame->data[0];
|
|
|
|
surface->Data.G = frame->data[0] + 1;
|
|
|
|
surface->Data.R = frame->data[0] + 2;
|
|
|
|
surface->Data.A = frame->data[0] + 3;
|
|
|
|
break;
|
2020-02-26 10:39:29 +02:00
|
|
|
case AV_PIX_FMT_YUYV422:
|
|
|
|
surface->Data.Y = frame->data[0];
|
|
|
|
surface->Data.U = frame->data[0] + 1;
|
|
|
|
surface->Data.V = frame->data[0] + 3;
|
|
|
|
break;
|
2018-05-21 11:58:52 +02:00
|
|
|
|
2020-02-26 10:39:29 +02:00
|
|
|
case AV_PIX_FMT_Y210:
|
2022-10-06 09:35:37 +02:00
|
|
|
case AV_PIX_FMT_Y212:
|
2020-02-26 10:39:29 +02:00
|
|
|
surface->Data.Y16 = (mfxU16 *)frame->data[0];
|
|
|
|
surface->Data.U16 = (mfxU16 *)frame->data[0] + 1;
|
|
|
|
surface->Data.V16 = (mfxU16 *)frame->data[0] + 3;
|
|
|
|
break;
|
2022-09-06 06:53:37 +02:00
|
|
|
case AV_PIX_FMT_VUYX:
|
|
|
|
surface->Data.V = frame->data[0];
|
|
|
|
surface->Data.U = frame->data[0] + 1;
|
|
|
|
surface->Data.Y = frame->data[0] + 2;
|
|
|
|
// Only set Data.A to a valid address, the SDK doesn't
|
|
|
|
// use the value from the frame.
|
|
|
|
surface->Data.A = frame->data[0] + 3;
|
|
|
|
break;
|
2022-10-06 09:35:35 +02:00
|
|
|
case AV_PIX_FMT_XV30:
|
|
|
|
surface->Data.U = frame->data[0];
|
|
|
|
break;
|
2022-10-06 09:35:37 +02:00
|
|
|
case AV_PIX_FMT_XV36:
|
|
|
|
surface->Data.U = frame->data[0];
|
|
|
|
surface->Data.Y = frame->data[0] + 2;
|
|
|
|
surface->Data.V = frame->data[0] + 4;
|
|
|
|
// Only set Data.A to a valid address, the SDK doesn't
|
|
|
|
// use the value from the frame.
|
|
|
|
surface->Data.A = frame->data[0] + 6;
|
|
|
|
break;
|
2023-02-14 04:45:59 +02:00
|
|
|
#if CONFIG_VAAPI
|
2022-03-10 08:34:22 +02:00
|
|
|
case AV_PIX_FMT_UYVY422:
|
|
|
|
surface->Data.Y = frame->data[0] + 1;
|
|
|
|
surface->Data.U = frame->data[0];
|
|
|
|
surface->Data.V = frame->data[0] + 2;
|
|
|
|
break;
|
2020-02-26 10:39:29 +02:00
|
|
|
#endif
|
2018-05-21 11:58:52 +02:00
|
|
|
default:
|
|
|
|
return MFX_ERR_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
surface->Data.Pitch = frame->linesize[0];
|
|
|
|
surface->Data.TimeStamp = frame->pts;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-08 11:12:17 +02:00
|
|
|
static int qsv_internal_session_check_init(AVHWFramesContext *ctx, int upload)
|
|
|
|
{
|
2024-02-11 20:42:08 +02:00
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
2022-02-08 11:12:17 +02:00
|
|
|
atomic_int *inited = upload ? &s->session_upload_init : &s->session_download_init;
|
|
|
|
mfxSession *session = upload ? &s->session_upload : &s->session_download;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (atomic_load(inited))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#if HAVE_PTHREADS
|
|
|
|
pthread_mutex_lock(&s->session_lock);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!atomic_load(inited)) {
|
|
|
|
ret = qsv_init_internal_session(ctx, session, upload);
|
|
|
|
atomic_store(inited, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_PTHREADS
|
|
|
|
pthread_mutex_unlock(&s->session_lock);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
|
|
|
|
const AVFrame *src)
|
|
|
|
{
|
2024-02-11 20:42:08 +02:00
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
2016-01-13 15:25:58 +02:00
|
|
|
mfxFrameSurface1 out = {{ 0 }};
|
|
|
|
mfxFrameSurface1 *in = (mfxFrameSurface1*)src->data[3];
|
|
|
|
|
|
|
|
mfxSyncPoint sync = NULL;
|
|
|
|
mfxStatus err;
|
2018-09-02 20:06:48 +02:00
|
|
|
int ret = 0;
|
2022-04-06 12:10:08 +02:00
|
|
|
/* download to temp frame if the output is not padded as libmfx requires */
|
|
|
|
AVFrame *tmp_frame = &s->realigned_download_frame;
|
|
|
|
AVFrame *dst_frame;
|
|
|
|
int realigned = 0;
|
2018-09-02 20:06:48 +02:00
|
|
|
|
2022-02-08 11:12:17 +02:00
|
|
|
ret = qsv_internal_session_check_init(ctx, 0);
|
2018-09-02 20:06:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
2022-04-06 12:10:08 +02:00
|
|
|
/* According to MSDK spec for mfxframeinfo, "Width must be a multiple of 16.
|
|
|
|
* Height must be a multiple of 16 for progressive frame sequence and a
|
|
|
|
* multiple of 32 otherwise.", so allign all frames to 16 before downloading. */
|
|
|
|
if (dst->height & 15 || dst->linesize[0] & 15) {
|
|
|
|
realigned = 1;
|
|
|
|
if (tmp_frame->format != dst->format ||
|
|
|
|
tmp_frame->width != FFALIGN(dst->linesize[0], 16) ||
|
|
|
|
tmp_frame->height != FFALIGN(dst->height, 16)) {
|
|
|
|
av_frame_unref(tmp_frame);
|
|
|
|
|
|
|
|
tmp_frame->format = dst->format;
|
|
|
|
tmp_frame->width = FFALIGN(dst->linesize[0], 16);
|
|
|
|
tmp_frame->height = FFALIGN(dst->height, 16);
|
|
|
|
ret = av_frame_get_buffer(tmp_frame, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dst_frame = realigned ? tmp_frame : dst;
|
|
|
|
|
2016-08-10 12:54:31 +02:00
|
|
|
if (!s->session_download) {
|
2016-08-10 14:58:08 +02:00
|
|
|
if (s->child_frames_ref)
|
2022-04-06 12:10:08 +02:00
|
|
|
return qsv_transfer_data_child(ctx, dst_frame, src);
|
2016-08-10 14:58:08 +02:00
|
|
|
|
2016-08-10 12:54:31 +02:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Surface download not possible\n");
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
out.Info = in->Info;
|
2022-04-06 12:10:08 +02:00
|
|
|
map_frame_to_surface(dst_frame, &out);
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
do {
|
|
|
|
err = MFXVideoVPP_RunFrameVPPAsync(s->session_download, in, &out, NULL, &sync);
|
|
|
|
if (err == MFX_WRN_DEVICE_BUSY)
|
|
|
|
av_usleep(1);
|
|
|
|
} while (err == MFX_WRN_DEVICE_BUSY);
|
|
|
|
|
|
|
|
if (err < 0 || !sync) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error downloading the surface\n");
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
err = MFXVideoCORE_SyncOperation(s->session_download, sync, 1000);
|
|
|
|
} while (err == MFX_WRN_IN_EXECUTION);
|
|
|
|
if (err < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation: %d\n", err);
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2022-04-06 12:10:08 +02:00
|
|
|
if (realigned) {
|
|
|
|
tmp_frame->width = dst->width;
|
|
|
|
tmp_frame->height = dst->height;
|
|
|
|
ret = av_frame_copy(dst, tmp_frame);
|
|
|
|
tmp_frame->width = FFALIGN(dst->linesize[0], 16);
|
|
|
|
tmp_frame->height = FFALIGN(dst->height, 16);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst,
|
|
|
|
const AVFrame *src)
|
|
|
|
{
|
2024-02-11 20:42:08 +02:00
|
|
|
QSVFramesContext *s = ctx->hwctx;
|
2016-01-13 15:25:58 +02:00
|
|
|
mfxFrameSurface1 in = {{ 0 }};
|
|
|
|
mfxFrameSurface1 *out = (mfxFrameSurface1*)dst->data[3];
|
2021-12-23 04:26:37 +02:00
|
|
|
mfxFrameInfo tmp_info;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
mfxSyncPoint sync = NULL;
|
|
|
|
mfxStatus err;
|
2018-09-30 11:59:52 +02:00
|
|
|
int ret = 0;
|
2018-09-17 13:16:44 +02:00
|
|
|
/* make a copy if the input is not padded as libmfx requires */
|
2022-04-06 12:10:08 +02:00
|
|
|
AVFrame *tmp_frame = &s->realigned_upload_frame;
|
2019-04-16 14:28:09 +02:00
|
|
|
const AVFrame *src_frame;
|
2018-09-17 13:16:44 +02:00
|
|
|
int realigned = 0;
|
|
|
|
|
2022-02-08 11:12:17 +02:00
|
|
|
ret = qsv_internal_session_check_init(ctx, 1);
|
2018-09-02 20:06:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
2021-12-23 04:26:37 +02:00
|
|
|
/* According to MSDK spec for mfxframeinfo, "Width must be a multiple of 16.
|
|
|
|
* Height must be a multiple of 16 for progressive frame sequence and a
|
|
|
|
* multiple of 32 otherwise.", so allign all frames to 16 before uploading. */
|
2019-04-15 15:23:40 +02:00
|
|
|
if (src->height & 15 || src->linesize[0] & 15) {
|
2018-09-17 13:16:44 +02:00
|
|
|
realigned = 1;
|
2021-12-23 04:26:37 +02:00
|
|
|
if (tmp_frame->format != src->format ||
|
|
|
|
tmp_frame->width != FFALIGN(src->width, 16) ||
|
|
|
|
tmp_frame->height != FFALIGN(src->height, 16)) {
|
|
|
|
av_frame_unref(tmp_frame);
|
|
|
|
|
|
|
|
tmp_frame->format = src->format;
|
|
|
|
tmp_frame->width = FFALIGN(src->width, 16);
|
|
|
|
tmp_frame->height = FFALIGN(src->height, 16);
|
|
|
|
ret = av_frame_get_buffer(tmp_frame, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ret = av_frame_copy(tmp_frame, src);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_unref(tmp_frame);
|
2018-09-17 13:16:44 +02:00
|
|
|
return ret;
|
2021-12-23 04:26:37 +02:00
|
|
|
}
|
|
|
|
ret = qsv_fill_border(tmp_frame, src);
|
2018-09-17 13:16:44 +02:00
|
|
|
if (ret < 0) {
|
2021-12-23 04:26:37 +02:00
|
|
|
av_frame_unref(tmp_frame);
|
2018-09-17 13:16:44 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2021-12-23 04:26:37 +02:00
|
|
|
|
|
|
|
tmp_info = out->Info;
|
|
|
|
out->Info.CropW = FFMIN(out->Info.Width, tmp_frame->width);
|
|
|
|
out->Info.CropH = FFMIN(out->Info.Height, tmp_frame->height);
|
2018-09-17 13:16:44 +02:00
|
|
|
}
|
|
|
|
|
2021-12-23 04:26:37 +02:00
|
|
|
src_frame = realigned ? tmp_frame : src;
|
2018-09-17 13:16:44 +02:00
|
|
|
|
2016-08-10 12:54:31 +02:00
|
|
|
if (!s->session_upload) {
|
2016-08-10 14:58:08 +02:00
|
|
|
if (s->child_frames_ref)
|
2018-09-17 13:16:44 +02:00
|
|
|
return qsv_transfer_data_child(ctx, dst, src_frame);
|
2016-08-10 14:58:08 +02:00
|
|
|
|
2016-08-10 12:54:31 +02:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Surface upload not possible\n");
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
in.Info = out->Info;
|
2018-09-17 13:16:44 +02:00
|
|
|
map_frame_to_surface(src_frame, &in);
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
do {
|
|
|
|
err = MFXVideoVPP_RunFrameVPPAsync(s->session_upload, &in, out, NULL, &sync);
|
|
|
|
if (err == MFX_WRN_DEVICE_BUSY)
|
|
|
|
av_usleep(1);
|
|
|
|
} while (err == MFX_WRN_DEVICE_BUSY);
|
|
|
|
|
|
|
|
if (err < 0 || !sync) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error uploading the surface\n");
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
err = MFXVideoCORE_SyncOperation(s->session_upload, sync, 1000);
|
|
|
|
} while (err == MFX_WRN_IN_EXECUTION);
|
|
|
|
if (err < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error synchronizing the operation\n");
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2021-12-23 04:26:37 +02:00
|
|
|
if (realigned) {
|
|
|
|
out->Info.CropW = tmp_info.CropW;
|
|
|
|
out->Info.CropH = tmp_info.CropH;
|
|
|
|
}
|
2018-09-17 13:16:44 +02:00
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-05-08 08:03:10 +02:00
|
|
|
static int qsv_dynamic_frames_derive_to(AVHWFramesContext *dst_ctx,
|
|
|
|
AVHWFramesContext *src_ctx, int flags)
|
2017-03-05 01:57:44 +02:00
|
|
|
{
|
2024-02-11 20:42:08 +02:00
|
|
|
QSVFramesContext *s = dst_ctx->hwctx;
|
|
|
|
AVQSVFramesContext *dst_hwctx = &s->p;
|
2024-05-08 08:03:10 +02:00
|
|
|
mfxFrameSurface1 mfx_surf1;
|
2017-03-05 01:57:44 +02:00
|
|
|
|
2024-05-08 08:03:10 +02:00
|
|
|
switch (src_ctx->device_ctx->type) {
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
case AV_HWDEVICE_TYPE_VAAPI:
|
|
|
|
dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
case AV_HWDEVICE_TYPE_D3D11VA:
|
|
|
|
{
|
|
|
|
AVD3D11VAFramesContext *src_hwctx = src_ctx->hwctx;
|
|
|
|
|
|
|
|
if (src_hwctx->BindFlags & D3D11_BIND_RENDER_TARGET) {
|
|
|
|
dst_hwctx->frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
|
|
|
|
} else {
|
|
|
|
dst_hwctx->frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
|
|
|
|
}
|
2021-08-20 23:48:03 +02:00
|
|
|
}
|
2024-05-08 08:03:10 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&mfx_surf1, 0, sizeof(mfx_surf1));
|
|
|
|
qsv_init_surface(dst_ctx, &mfx_surf1);
|
|
|
|
s->frame_info = mfx_surf1.Info;
|
|
|
|
dst_hwctx->info = &s->frame_info;
|
|
|
|
dst_hwctx->nb_surfaces = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_fixed_frames_derive_to(AVHWFramesContext *dst_ctx,
|
|
|
|
AVHWFramesContext *src_ctx, int flags)
|
|
|
|
{
|
|
|
|
QSVFramesContext *s = dst_ctx->hwctx;
|
|
|
|
AVQSVFramesContext *dst_hwctx = &s->p;
|
|
|
|
int i;
|
2021-08-20 23:48:03 +02:00
|
|
|
|
2017-03-05 01:57:44 +02:00
|
|
|
switch (src_ctx->device_ctx->type) {
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
case AV_HWDEVICE_TYPE_VAAPI:
|
|
|
|
{
|
|
|
|
AVVAAPIFramesContext *src_hwctx = src_ctx->hwctx;
|
2021-09-14 21:31:53 +02:00
|
|
|
s->handle_pairs_internal = av_calloc(src_ctx->initial_pool_size,
|
|
|
|
sizeof(*s->handle_pairs_internal));
|
2021-08-20 23:48:03 +02:00
|
|
|
if (!s->handle_pairs_internal)
|
|
|
|
return AVERROR(ENOMEM);
|
2021-09-14 21:31:53 +02:00
|
|
|
s->surfaces_internal = av_calloc(src_hwctx->nb_surfaces,
|
|
|
|
sizeof(*s->surfaces_internal));
|
2017-03-05 01:57:44 +02:00
|
|
|
if (!s->surfaces_internal)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
for (i = 0; i < src_hwctx->nb_surfaces; i++) {
|
|
|
|
qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
|
2021-08-20 23:48:03 +02:00
|
|
|
s->handle_pairs_internal[i].first = src_hwctx->surface_ids + i;
|
|
|
|
s->handle_pairs_internal[i].second = (mfxMemId)MFX_INFINITE;
|
|
|
|
s->surfaces_internal[i].Data.MemId = (mfxMemId)&s->handle_pairs_internal[i];
|
2017-03-05 01:57:44 +02:00
|
|
|
}
|
|
|
|
dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
|
|
|
|
dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2021-08-20 23:48:03 +02:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
case AV_HWDEVICE_TYPE_D3D11VA:
|
|
|
|
{
|
|
|
|
AVD3D11VAFramesContext *src_hwctx = src_ctx->hwctx;
|
2021-09-14 21:31:53 +02:00
|
|
|
s->handle_pairs_internal = av_calloc(src_ctx->initial_pool_size,
|
|
|
|
sizeof(*s->handle_pairs_internal));
|
2021-08-20 23:48:03 +02:00
|
|
|
if (!s->handle_pairs_internal)
|
|
|
|
return AVERROR(ENOMEM);
|
2021-09-14 21:31:53 +02:00
|
|
|
s->surfaces_internal = av_calloc(src_ctx->initial_pool_size,
|
|
|
|
sizeof(*s->surfaces_internal));
|
2021-08-20 23:48:03 +02:00
|
|
|
if (!s->surfaces_internal)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
for (i = 0; i < src_ctx->initial_pool_size; i++) {
|
|
|
|
qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
|
2021-08-20 23:48:05 +02:00
|
|
|
s->handle_pairs_internal[i].first = (mfxMemId)src_hwctx->texture_infos[i].texture;
|
2021-08-20 23:48:03 +02:00
|
|
|
if (src_hwctx->BindFlags & D3D11_BIND_RENDER_TARGET) {
|
|
|
|
s->handle_pairs_internal[i].second = (mfxMemId)MFX_INFINITE;
|
|
|
|
} else {
|
2021-08-20 23:48:05 +02:00
|
|
|
s->handle_pairs_internal[i].second = (mfxMemId)src_hwctx->texture_infos[i].index;
|
2021-08-20 23:48:03 +02:00
|
|
|
}
|
|
|
|
s->surfaces_internal[i].Data.MemId = (mfxMemId)&s->handle_pairs_internal[i];
|
|
|
|
}
|
|
|
|
dst_hwctx->nb_surfaces = src_ctx->initial_pool_size;
|
|
|
|
if (src_hwctx->BindFlags & D3D11_BIND_RENDER_TARGET) {
|
|
|
|
dst_hwctx->frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
|
|
|
|
} else {
|
|
|
|
dst_hwctx->frame_type |= MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2017-03-05 01:57:44 +02:00
|
|
|
#if CONFIG_DXVA2
|
|
|
|
case AV_HWDEVICE_TYPE_DXVA2:
|
|
|
|
{
|
|
|
|
AVDXVA2FramesContext *src_hwctx = src_ctx->hwctx;
|
2021-09-14 21:31:53 +02:00
|
|
|
s->handle_pairs_internal = av_calloc(src_ctx->initial_pool_size,
|
|
|
|
sizeof(*s->handle_pairs_internal));
|
2021-08-20 23:48:03 +02:00
|
|
|
if (!s->handle_pairs_internal)
|
|
|
|
return AVERROR(ENOMEM);
|
2021-09-14 21:31:53 +02:00
|
|
|
s->surfaces_internal = av_calloc(src_hwctx->nb_surfaces,
|
|
|
|
sizeof(*s->surfaces_internal));
|
2017-03-05 01:57:44 +02:00
|
|
|
if (!s->surfaces_internal)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
for (i = 0; i < src_hwctx->nb_surfaces; i++) {
|
|
|
|
qsv_init_surface(dst_ctx, &s->surfaces_internal[i]);
|
2021-08-20 23:48:03 +02:00
|
|
|
s->handle_pairs_internal[i].first = (mfxMemId)src_hwctx->surfaces[i];
|
|
|
|
s->handle_pairs_internal[i].second = (mfxMemId)MFX_INFINITE;
|
|
|
|
s->surfaces_internal[i].Data.MemId = (mfxMemId)&s->handle_pairs_internal[i];
|
2017-03-05 01:57:44 +02:00
|
|
|
}
|
|
|
|
dst_hwctx->nb_surfaces = src_hwctx->nb_surfaces;
|
|
|
|
if (src_hwctx->surface_type == DXVA2_VideoProcessorRenderTarget)
|
|
|
|
dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET;
|
|
|
|
else
|
|
|
|
dst_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
|
|
|
|
dst_hwctx->surfaces = s->surfaces_internal;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-05-08 08:03:10 +02:00
|
|
|
static int qsv_frames_derive_to(AVHWFramesContext *dst_ctx,
|
|
|
|
AVHWFramesContext *src_ctx, int flags)
|
|
|
|
{
|
|
|
|
if (src_ctx->initial_pool_size < 0) {
|
|
|
|
av_log(dst_ctx, AV_LOG_ERROR, "Invalid src frame pool. \n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
} else if (src_ctx->initial_pool_size == 0) {
|
|
|
|
return qsv_dynamic_frames_derive_to(dst_ctx, src_ctx, flags);
|
|
|
|
} else {
|
|
|
|
return qsv_fixed_frames_derive_to(dst_ctx, src_ctx, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-08 08:03:11 +02:00
|
|
|
static int qsv_fixed_pool_map_to(AVHWFramesContext *dst_ctx,
|
|
|
|
AVFrame *dst, const AVFrame *src, int flags)
|
2017-03-05 01:57:44 +02:00
|
|
|
{
|
|
|
|
AVQSVFramesContext *hwctx = dst_ctx->hwctx;
|
2021-08-20 23:48:03 +02:00
|
|
|
int i, err, index = -1;
|
2017-03-05 01:57:44 +02:00
|
|
|
|
2021-08-20 23:48:03 +02:00
|
|
|
for (i = 0; i < hwctx->nb_surfaces && index < 0; i++) {
|
|
|
|
switch(src->format) {
|
2017-03-05 01:57:44 +02:00
|
|
|
#if CONFIG_VAAPI
|
2021-08-20 23:48:03 +02:00
|
|
|
case AV_PIX_FMT_VAAPI:
|
|
|
|
{
|
|
|
|
mfxHDLPair *pair = (mfxHDLPair*)hwctx->surfaces[i].Data.MemId;
|
2023-07-24 04:41:06 +02:00
|
|
|
if (*(VASurfaceID*)pair->first == (VASurfaceID)(uintptr_t)src->data[3]) {
|
2021-08-20 23:48:03 +02:00
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
case AV_PIX_FMT_D3D11:
|
|
|
|
{
|
|
|
|
mfxHDLPair *pair = (mfxHDLPair*)hwctx->surfaces[i].Data.MemId;
|
|
|
|
if (pair->first == src->data[0]
|
2022-05-07 08:24:41 +02:00
|
|
|
&& (pair->second == src->data[1]
|
|
|
|
|| (pair->second == (mfxMemId)MFX_INFINITE && src->data[1] == (uint8_t *)0))) {
|
2021-08-20 23:48:03 +02:00
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 01:57:44 +02:00
|
|
|
#endif
|
|
|
|
#if CONFIG_DXVA2
|
2021-08-20 23:48:03 +02:00
|
|
|
case AV_PIX_FMT_DXVA2_VLD:
|
|
|
|
{
|
|
|
|
mfxHDLPair *pair = (mfxHDLPair*)hwctx->surfaces[i].Data.MemId;
|
|
|
|
if (pair->first == src->data[3]) {
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 01:57:44 +02:00
|
|
|
#endif
|
2021-08-20 23:48:03 +02:00
|
|
|
}
|
2017-03-05 01:57:44 +02:00
|
|
|
}
|
2021-08-20 23:48:03 +02:00
|
|
|
if (index < 0) {
|
2017-03-05 01:57:44 +02:00
|
|
|
av_log(dst_ctx, AV_LOG_ERROR, "Trying to map from a surface which "
|
|
|
|
"is not in the mapped frames context.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ff_hwframe_map_create(dst->hw_frames_ctx,
|
|
|
|
dst, src, NULL, NULL);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
dst->width = src->width;
|
|
|
|
dst->height = src->height;
|
2021-08-20 23:48:03 +02:00
|
|
|
dst->data[3] = (uint8_t*)&hwctx->surfaces[index];
|
2017-03-05 01:57:44 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-05-08 08:03:11 +02:00
|
|
|
static void qsv_dynamic_pool_unmap(AVHWFramesContext *ctx, HWMapDescriptor *hwmap)
|
|
|
|
{
|
|
|
|
mfxFrameSurface1 *surfaces_internal = (mfxFrameSurface1 *)hwmap->priv;
|
|
|
|
mfxHDLPair *handle_pairs_internal = (mfxHDLPair *)surfaces_internal->Data.MemId;
|
|
|
|
AVHWFramesContext *src_ctx = (AVHWFramesContext *)ffhwframesctx(ctx)->source_frames->data;
|
|
|
|
|
|
|
|
switch (src_ctx->format) {
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
case AV_PIX_FMT_VAAPI:
|
|
|
|
{
|
|
|
|
av_freep(&handle_pairs_internal->first);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
case AV_PIX_FMT_D3D11:
|
|
|
|
{
|
|
|
|
/* Do nothing */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Should not reach here. \n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_freep(&handle_pairs_internal);
|
|
|
|
av_freep(&surfaces_internal);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_dynamic_pool_map_to(AVHWFramesContext *dst_ctx,
|
|
|
|
AVFrame *dst, const AVFrame *src, int flags)
|
|
|
|
{
|
|
|
|
mfxFrameSurface1 *surfaces_internal = NULL;
|
|
|
|
mfxHDLPair *handle_pairs_internal = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
surfaces_internal = av_calloc(1, sizeof(*surfaces_internal));
|
|
|
|
if (!surfaces_internal) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_pairs_internal = av_calloc(1, sizeof(*handle_pairs_internal));
|
|
|
|
if (!handle_pairs_internal) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = qsv_init_surface(dst_ctx, surfaces_internal);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
switch (src->format) {
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
case AV_PIX_FMT_VAAPI:
|
|
|
|
{
|
|
|
|
VASurfaceID *surface_id_internal;
|
|
|
|
|
|
|
|
surface_id_internal = av_calloc(1, sizeof(*surface_id_internal));
|
|
|
|
if (!surface_id_internal) {
|
|
|
|
ret =AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
*surface_id_internal = (VASurfaceID)(uintptr_t)src->data[3];
|
|
|
|
handle_pairs_internal->first = (mfxHDL)surface_id_internal;
|
|
|
|
handle_pairs_internal->second = (mfxMemId)MFX_INFINITE;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
case AV_PIX_FMT_D3D11:
|
|
|
|
{
|
|
|
|
AVHWFramesContext *src_ctx = (AVHWFramesContext*)src->hw_frames_ctx->data;
|
|
|
|
AVD3D11VAFramesContext *src_hwctx = src_ctx->hwctx;
|
|
|
|
|
|
|
|
handle_pairs_internal->first = (mfxMemId)src->data[0];
|
|
|
|
|
|
|
|
if (src_hwctx->BindFlags & D3D11_BIND_RENDER_TARGET) {
|
|
|
|
handle_pairs_internal->second = (mfxMemId)MFX_INFINITE;
|
|
|
|
} else {
|
|
|
|
handle_pairs_internal->second = (mfxMemId)src->data[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
ret = AVERROR(ENOSYS);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
surfaces_internal->Data.MemId = (mfxMemId)handle_pairs_internal;
|
|
|
|
|
|
|
|
ret = ff_hwframe_map_create(dst->hw_frames_ctx,
|
|
|
|
dst, src, qsv_dynamic_pool_unmap, surfaces_internal);
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
dst->width = src->width;
|
|
|
|
dst->height = src->height;
|
|
|
|
dst->data[3] = (uint8_t*)surfaces_internal;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av_freep(&handle_pairs_internal);
|
|
|
|
av_freep(&surfaces_internal);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_map_to(AVHWFramesContext *dst_ctx,
|
|
|
|
AVFrame *dst, const AVFrame *src, int flags)
|
|
|
|
{
|
|
|
|
AVQSVFramesContext *hwctx = dst_ctx->hwctx;
|
|
|
|
|
|
|
|
if (hwctx->nb_surfaces)
|
|
|
|
return qsv_fixed_pool_map_to(dst_ctx, dst, src, flags);
|
|
|
|
else
|
|
|
|
return qsv_dynamic_pool_map_to(dst_ctx, dst, src, flags);
|
|
|
|
}
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
static int qsv_frames_get_constraints(AVHWDeviceContext *ctx,
|
|
|
|
const void *hwconfig,
|
|
|
|
AVHWFramesConstraints *constraints)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
constraints->valid_sw_formats = av_malloc_array(FF_ARRAY_ELEMS(supported_pixel_formats) + 1,
|
|
|
|
sizeof(*constraints->valid_sw_formats));
|
|
|
|
if (!constraints->valid_sw_formats)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(supported_pixel_formats); i++)
|
|
|
|
constraints->valid_sw_formats[i] = supported_pixel_formats[i].pix_fmt;
|
|
|
|
constraints->valid_sw_formats[FF_ARRAY_ELEMS(supported_pixel_formats)] = AV_PIX_FMT_NONE;
|
|
|
|
|
|
|
|
constraints->valid_hw_formats = av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
|
|
|
|
if (!constraints->valid_hw_formats)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
constraints->valid_hw_formats[0] = AV_PIX_FMT_QSV;
|
|
|
|
constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qsv_device_free(AVHWDeviceContext *ctx)
|
|
|
|
{
|
|
|
|
AVQSVDeviceContext *hwctx = ctx->hwctx;
|
|
|
|
QSVDevicePriv *priv = ctx->user_opaque;
|
|
|
|
|
|
|
|
if (hwctx->session)
|
|
|
|
MFXClose(hwctx->session);
|
|
|
|
|
2021-01-04 04:46:14 +02:00
|
|
|
if (hwctx->loader)
|
|
|
|
MFXUnload(hwctx->loader);
|
2016-01-13 15:25:58 +02:00
|
|
|
av_buffer_unref(&priv->child_device_ctx);
|
|
|
|
av_freep(&priv);
|
|
|
|
}
|
|
|
|
|
2021-08-20 23:48:03 +02:00
|
|
|
static mfxIMPL choose_implementation(const char *device, enum AVHWDeviceType child_device_type)
|
2016-01-13 15:25:58 +02:00
|
|
|
{
|
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
mfxIMPL impl;
|
|
|
|
} impl_map[] = {
|
|
|
|
{ "auto", MFX_IMPL_AUTO },
|
|
|
|
{ "sw", MFX_IMPL_SOFTWARE },
|
|
|
|
{ "hw", MFX_IMPL_HARDWARE },
|
|
|
|
{ "auto_any", MFX_IMPL_AUTO_ANY },
|
|
|
|
{ "hw_any", MFX_IMPL_HARDWARE_ANY },
|
|
|
|
{ "hw2", MFX_IMPL_HARDWARE2 },
|
|
|
|
{ "hw3", MFX_IMPL_HARDWARE3 },
|
|
|
|
{ "hw4", MFX_IMPL_HARDWARE4 },
|
|
|
|
};
|
|
|
|
|
|
|
|
mfxIMPL impl = MFX_IMPL_AUTO_ANY;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (device) {
|
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(impl_map); i++)
|
|
|
|
if (!strcmp(device, impl_map[i].name)) {
|
|
|
|
impl = impl_map[i].impl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == FF_ARRAY_ELEMS(impl_map))
|
|
|
|
impl = strtol(device, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2021-08-20 23:48:03 +02:00
|
|
|
if (impl != MFX_IMPL_SOFTWARE) {
|
|
|
|
if (child_device_type == AV_HWDEVICE_TYPE_D3D11VA)
|
|
|
|
impl |= MFX_IMPL_VIA_D3D11;
|
|
|
|
else if (child_device_type == AV_HWDEVICE_TYPE_DXVA2)
|
|
|
|
impl |= MFX_IMPL_VIA_D3D9;
|
|
|
|
}
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
return impl;
|
|
|
|
}
|
|
|
|
|
2017-03-05 01:57:43 +02:00
|
|
|
static int qsv_device_derive_from_child(AVHWDeviceContext *ctx,
|
|
|
|
mfxIMPL implementation,
|
|
|
|
AVHWDeviceContext *child_device_ctx,
|
|
|
|
int flags)
|
2016-01-13 15:25:58 +02:00
|
|
|
{
|
|
|
|
AVQSVDeviceContext *hwctx = ctx->hwctx;
|
|
|
|
|
|
|
|
mfxVersion ver = { { 3, 1 } };
|
|
|
|
mfxHDL handle;
|
|
|
|
mfxHandleType handle_type;
|
|
|
|
mfxStatus err;
|
|
|
|
int ret;
|
|
|
|
|
2017-03-05 01:57:43 +02:00
|
|
|
switch (child_device_ctx->type) {
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
case AV_HWDEVICE_TYPE_VAAPI:
|
|
|
|
{
|
|
|
|
AVVAAPIDeviceContext *child_device_hwctx = child_device_ctx->hwctx;
|
|
|
|
handle_type = MFX_HANDLE_VA_DISPLAY;
|
|
|
|
handle = (mfxHDL)child_device_hwctx->display;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2021-08-20 23:48:03 +02:00
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
case AV_HWDEVICE_TYPE_D3D11VA:
|
|
|
|
{
|
|
|
|
AVD3D11VADeviceContext *child_device_hwctx = child_device_ctx->hwctx;
|
|
|
|
handle_type = MFX_HANDLE_D3D11_DEVICE;
|
|
|
|
handle = (mfxHDL)child_device_hwctx->device;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2017-03-05 01:57:43 +02:00
|
|
|
#if CONFIG_DXVA2
|
|
|
|
case AV_HWDEVICE_TYPE_DXVA2:
|
|
|
|
{
|
|
|
|
AVDXVA2DeviceContext *child_device_hwctx = child_device_ctx->hwctx;
|
|
|
|
handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
|
|
|
|
handle = (mfxHDL)child_device_hwctx->devmgr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
ret = AVERROR(ENOSYS);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2021-01-04 04:46:14 +02:00
|
|
|
ret = qsv_create_mfx_session(ctx, handle, handle_type, implementation, &ver,
|
|
|
|
&hwctx->session, &hwctx->loader);
|
|
|
|
if (ret)
|
2017-09-25 11:57:30 +02:00
|
|
|
goto fail;
|
|
|
|
|
2017-03-05 01:57:43 +02:00
|
|
|
err = MFXVideoCORE_SetHandle(hwctx->session, handle_type, handle);
|
|
|
|
if (err != MFX_ERR_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error setting child device handle: "
|
|
|
|
"%d\n", err);
|
|
|
|
ret = AVERROR_UNKNOWN;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (hwctx->session)
|
|
|
|
MFXClose(hwctx->session);
|
2021-01-04 04:46:14 +02:00
|
|
|
|
|
|
|
if (hwctx->loader)
|
|
|
|
MFXUnload(hwctx->loader);
|
|
|
|
|
|
|
|
hwctx->session = NULL;
|
|
|
|
hwctx->loader = NULL;
|
2017-03-05 01:57:43 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_device_derive(AVHWDeviceContext *ctx,
|
2020-05-20 21:58:03 +02:00
|
|
|
AVHWDeviceContext *child_device_ctx,
|
|
|
|
AVDictionary *opts, int flags)
|
2017-03-05 01:57:43 +02:00
|
|
|
{
|
2021-08-20 23:48:03 +02:00
|
|
|
mfxIMPL impl;
|
2023-06-19 08:15:07 +02:00
|
|
|
QSVDevicePriv *priv;
|
|
|
|
|
|
|
|
priv = av_mallocz(sizeof(*priv));
|
|
|
|
if (!priv)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
ctx->user_opaque = priv;
|
|
|
|
ctx->free = qsv_device_free;
|
|
|
|
|
2021-08-20 23:48:03 +02:00
|
|
|
impl = choose_implementation("hw_any", child_device_ctx->type);
|
|
|
|
return qsv_device_derive_from_child(ctx, impl,
|
2017-03-05 01:57:43 +02:00
|
|
|
child_device_ctx, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qsv_device_create(AVHWDeviceContext *ctx, const char *device,
|
|
|
|
AVDictionary *opts, int flags)
|
|
|
|
{
|
|
|
|
QSVDevicePriv *priv;
|
|
|
|
enum AVHWDeviceType child_device_type;
|
|
|
|
AVHWDeviceContext *child_device;
|
2019-05-06 16:45:44 +02:00
|
|
|
AVDictionary *child_device_opts;
|
2017-03-05 01:57:43 +02:00
|
|
|
AVDictionaryEntry *e;
|
|
|
|
|
|
|
|
mfxIMPL impl;
|
|
|
|
int ret;
|
|
|
|
|
2016-01-13 15:25:58 +02:00
|
|
|
priv = av_mallocz(sizeof(*priv));
|
|
|
|
if (!priv)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
ctx->user_opaque = priv;
|
|
|
|
ctx->free = qsv_device_free;
|
|
|
|
|
2021-08-20 23:48:04 +02:00
|
|
|
e = av_dict_get(opts, "child_device_type", NULL, 0);
|
|
|
|
if (e) {
|
2021-09-25 12:31:50 +02:00
|
|
|
child_device_type = av_hwdevice_find_type_by_name(e->value);
|
2021-08-20 23:48:04 +02:00
|
|
|
if (child_device_type == AV_HWDEVICE_TYPE_NONE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Unknown child device type "
|
2021-09-25 12:31:50 +02:00
|
|
|
"\"%s\".\n", e->value);
|
2021-08-20 23:48:04 +02:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2021-01-04 04:46:14 +02:00
|
|
|
#if QSV_ONEVPL
|
|
|
|
} else if (CONFIG_D3D11VA) { // Use D3D11 by default if d3d11va is enabled
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE,
|
|
|
|
"Defaulting child_device_type to AV_HWDEVICE_TYPE_D3D11VA for oneVPL."
|
|
|
|
"Please explicitly set child device type via \"-init_hw_device\" "
|
|
|
|
"option if needed.\n");
|
|
|
|
child_device_type = AV_HWDEVICE_TYPE_D3D11VA;
|
|
|
|
} else if (CONFIG_DXVA2) {
|
|
|
|
child_device_type = AV_HWDEVICE_TYPE_DXVA2;
|
|
|
|
#else
|
2021-08-20 23:48:04 +02:00
|
|
|
} else if (CONFIG_DXVA2) {
|
|
|
|
av_log(NULL, AV_LOG_WARNING,
|
|
|
|
"WARNING: defaulting child_device_type to AV_HWDEVICE_TYPE_DXVA2 for compatibility "
|
|
|
|
"with old commandlines. This behaviour will be removed "
|
|
|
|
"in the future. Please explicitly set device type via \"-init_hw_device\" option.\n");
|
2016-01-13 15:25:58 +02:00
|
|
|
child_device_type = AV_HWDEVICE_TYPE_DXVA2;
|
2021-08-20 23:48:04 +02:00
|
|
|
} else if (CONFIG_D3D11VA) {
|
|
|
|
child_device_type = AV_HWDEVICE_TYPE_D3D11VA;
|
2021-01-04 04:46:14 +02:00
|
|
|
#endif
|
2023-04-14 16:39:06 +02:00
|
|
|
} else if (CONFIG_VAAPI) {
|
|
|
|
child_device_type = AV_HWDEVICE_TYPE_VAAPI;
|
2021-08-20 23:48:04 +02:00
|
|
|
} else {
|
2016-01-13 15:25:58 +02:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "No supported child device type is enabled\n");
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:39:06 +02:00
|
|
|
#if CONFIG_VAAPI && defined(_WIN32)
|
|
|
|
/* AV_HWDEVICE_TYPE_VAAPI on Windows/Libva-win32 not supported */
|
|
|
|
/* Reject user specified child_device_type or CONFIG_VAAPI on Windows */
|
|
|
|
if (child_device_type == AV_HWDEVICE_TYPE_VAAPI) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "VAAPI child device type not supported for oneVPL on Windows"
|
|
|
|
"\"%s\".\n", e->value);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-08-20 23:48:04 +02:00
|
|
|
child_device_opts = NULL;
|
|
|
|
switch (child_device_type) {
|
|
|
|
#if CONFIG_VAAPI
|
|
|
|
case AV_HWDEVICE_TYPE_VAAPI:
|
|
|
|
{
|
|
|
|
// libmfx does not actually implement VAAPI properly, rather it
|
|
|
|
// depends on the specific behaviour of a matching iHD driver when
|
|
|
|
// used on recent Intel hardware. Set options to the VAAPI device
|
|
|
|
// creation so that we should pick a usable setup by default if
|
|
|
|
// possible, even when multiple devices and drivers are available.
|
2024-07-26 03:05:21 +02:00
|
|
|
av_dict_set(&child_device_opts, "vendor_id", "0x8086", 0);
|
|
|
|
av_dict_set(&child_device_opts, "driver", "iHD", 0);
|
2021-08-20 23:48:04 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_D3D11VA
|
|
|
|
case AV_HWDEVICE_TYPE_D3D11VA:
|
2023-11-28 06:42:27 +02:00
|
|
|
{
|
|
|
|
// Make sure the hardware vendor is Intel when multiple devices are
|
|
|
|
// available, it will be ignored if user specifies the child device
|
|
|
|
// explicitly
|
|
|
|
av_dict_set(&child_device_opts, "vendor_id", "0x8086", 0);
|
|
|
|
}
|
2021-08-20 23:48:04 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if CONFIG_DXVA2
|
|
|
|
case AV_HWDEVICE_TYPE_DXVA2:
|
2021-01-04 04:46:14 +02:00
|
|
|
#if QSV_ONEVPL
|
|
|
|
{
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE,
|
|
|
|
"d3d11va is not available or child device type is set to dxva2 "
|
|
|
|
"explicitly for oneVPL.\n");
|
|
|
|
}
|
|
|
|
#endif
|
2021-08-20 23:48:04 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "No supported child device type is enabled\n");
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
e = av_dict_get(opts, "child_device", NULL, 0);
|
2016-01-13 15:25:58 +02:00
|
|
|
ret = av_hwdevice_ctx_create(&priv->child_device_ctx, child_device_type,
|
2019-05-06 16:45:44 +02:00
|
|
|
e ? e->value : NULL, child_device_opts, 0);
|
2019-07-26 10:00:24 +02:00
|
|
|
|
|
|
|
av_dict_free(&child_device_opts);
|
2016-01-13 15:25:58 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2017-03-05 01:57:43 +02:00
|
|
|
child_device = (AVHWDeviceContext*)priv->child_device_ctx->data;
|
2016-01-13 15:25:58 +02:00
|
|
|
|
2021-08-20 23:48:03 +02:00
|
|
|
impl = choose_implementation(device, child_device_type);
|
2016-01-13 15:25:58 +02:00
|
|
|
|
2022-01-05 05:52:07 +02:00
|
|
|
return qsv_device_derive_from_child(ctx, impl, child_device, 0);
|
2016-01-13 15:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const HWContextType ff_hwcontext_type_qsv = {
|
|
|
|
.type = AV_HWDEVICE_TYPE_QSV,
|
|
|
|
.name = "QSV",
|
|
|
|
|
2024-02-11 17:45:08 +02:00
|
|
|
.device_hwctx_size = sizeof(QSVDeviceContext),
|
2024-02-11 20:42:08 +02:00
|
|
|
.frames_hwctx_size = sizeof(QSVFramesContext),
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
.device_create = qsv_device_create,
|
2017-03-05 01:57:43 +02:00
|
|
|
.device_derive = qsv_device_derive,
|
2016-01-13 15:25:58 +02:00
|
|
|
.device_init = qsv_device_init,
|
|
|
|
.frames_get_constraints = qsv_frames_get_constraints,
|
|
|
|
.frames_init = qsv_frames_init,
|
|
|
|
.frames_uninit = qsv_frames_uninit,
|
|
|
|
.frames_get_buffer = qsv_get_buffer,
|
|
|
|
.transfer_get_formats = qsv_transfer_get_formats,
|
|
|
|
.transfer_data_to = qsv_transfer_data_to,
|
|
|
|
.transfer_data_from = qsv_transfer_data_from,
|
2017-03-05 01:57:44 +02:00
|
|
|
.map_to = qsv_map_to,
|
2016-10-06 11:33:15 +02:00
|
|
|
.map_from = qsv_map_from,
|
2017-03-05 01:57:44 +02:00
|
|
|
.frames_derive_to = qsv_frames_derive_to,
|
2017-03-05 01:57:45 +02:00
|
|
|
.frames_derive_from = qsv_frames_derive_from,
|
2016-01-13 15:25:58 +02:00
|
|
|
|
|
|
|
.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_QSV, AV_PIX_FMT_NONE },
|
|
|
|
};
|