[FFmpeg-cvslog] Merge commit 'c46db38cde8e8fd8ecb1c6602f10ec0e002f29a8'
Hendrik Leppkes
git at videolan.org
Wed Jun 22 22:50:06 CEST 2016
ffmpeg | branch: master | Hendrik Leppkes <h.leppkes at gmail.com> | Wed Jun 22 22:38:49 2016 +0200| [3689efea3d59579291ecafccc3d9cbe720e7bea3] | committer: Hendrik Leppkes
Merge commit 'c46db38cde8e8fd8ecb1c6602f10ec0e002f29a8'
* commit 'c46db38cde8e8fd8ecb1c6602f10ec0e002f29a8':
hwcontext: add a dxva2 implementation
Merged-by: Hendrik Leppkes <h.leppkes at gmail.com>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=3689efea3d59579291ecafccc3d9cbe720e7bea3
---
doc/APIchanges | 3 +
libavutil/Makefile | 3 +
libavutil/hwcontext.c | 3 +
libavutil/hwcontext.h | 1 +
libavutil/hwcontext_dxva2.c | 292 ++++++++++++++++++++++++++++++++++++++++
libavutil/hwcontext_dxva2.h | 72 ++++++++++
libavutil/hwcontext_internal.h | 1 +
libavutil/version.h | 2 +-
8 files changed, 376 insertions(+), 1 deletion(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index 15d52fe..4b93596 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,9 @@ libavutil: 2015-08-28
API changes, most recent first:
+2016-06-22 - xxxxxxx - lavu 55.25.100 - hwcontext_dxva2.h
+ Add new installed header with DXVA2-specific hwcontext definitions.
+
2016-04-27 - fb91871 - lavu 55.23.100 - log.h
Add a new function av_log_format_line2() which returns number of bytes
written to the target buffer.
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 1f05215..1e06176 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -34,6 +34,7 @@ HEADERS = adler32.h \
hmac.h \
hwcontext.h \
hwcontext_cuda.h \
+ hwcontext_dxva2.h \
hwcontext_vaapi.h \
hwcontext_vdpau.h \
imgutils.h \
@@ -152,6 +153,7 @@ OBJS = adler32.o \
OBJS-$(!HAVE_ATOMICS_NATIVE) += atomic.o \
OBJS-$(CONFIG_CUDA) += hwcontext_cuda.o
+OBJS-$(CONFIG_DXVA2) += hwcontext_dxva2.o
OBJS-$(CONFIG_LZO) += lzo.o
OBJS-$(CONFIG_OPENCL) += opencl.o opencl_internal.o
OBJS-$(CONFIG_VAAPI) += hwcontext_vaapi.o
@@ -163,6 +165,7 @@ OBJS += $(COMPAT_OBJS:%=../compat/%)
SLIBOBJS-$(HAVE_GNU_WINDRES) += avutilres.o
SKIPHEADERS-$(CONFIG_CUDA) += hwcontext_cuda.h
+SKIPHEADERS-$(CONFIG_DXVA2) += hwcontext_dxva2.h
SKIPHEADERS-$(CONFIG_VAAPI) += hwcontext_vaapi.h
SKIPHEADERS-$(CONFIG_VDPAU) += hwcontext_vdpau.h
SKIPHEADERS-$(HAVE_ATOMICS_GCC) += atomic_gcc.h
diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c
index 619f21e..eeeb288 100644
--- a/libavutil/hwcontext.c
+++ b/libavutil/hwcontext.c
@@ -32,6 +32,9 @@ static const HWContextType *hw_table[] = {
#if CONFIG_CUDA
&ff_hwcontext_type_cuda,
#endif
+#if CONFIG_DXVA2
+ &ff_hwcontext_type_dxva2,
+#endif
#if CONFIG_VAAPI
&ff_hwcontext_type_vaapi,
#endif
diff --git a/libavutil/hwcontext.h b/libavutil/hwcontext.h
index 7b93b20..dc81699 100644
--- a/libavutil/hwcontext.h
+++ b/libavutil/hwcontext.h
@@ -28,6 +28,7 @@ enum AVHWDeviceType {
AV_HWDEVICE_TYPE_VDPAU,
AV_HWDEVICE_TYPE_CUDA,
AV_HWDEVICE_TYPE_VAAPI,
+ AV_HWDEVICE_TYPE_DXVA2,
};
typedef struct AVHWDeviceInternal AVHWDeviceInternal;
diff --git a/libavutil/hwcontext_dxva2.c b/libavutil/hwcontext_dxva2.c
new file mode 100644
index 0000000..56ae317
--- /dev/null
+++ b/libavutil/hwcontext_dxva2.c
@@ -0,0 +1,292 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * 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.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * 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
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <windows.h>
+
+#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600
+#undef _WIN32_WINNT
+#define _WIN32_WINNT 0x0600
+#endif
+#define DXVA2API_USE_BITFIELDS
+#define COBJMACROS
+
+#include <d3d9.h>
+#include <dxva2api.h>
+#include <initguid.h>
+
+#include "common.h"
+#include "hwcontext.h"
+#include "hwcontext_dxva2.h"
+#include "hwcontext_internal.h"
+#include "imgutils.h"
+#include "pixdesc.h"
+#include "pixfmt.h"
+
+typedef struct DXVA2FramesContext {
+ IDirect3DSurface9 **surfaces_internal;
+ int nb_surfaces_used;
+
+ HANDLE device_handle;
+ IDirectXVideoAccelerationService *service;
+
+ D3DFORMAT format;
+} DXVA2FramesContext;
+
+static const struct {
+ D3DFORMAT d3d_format;
+ enum AVPixelFormat pix_fmt;
+} supported_formats[] = {
+ { MKTAG('N', 'V', '1', '2'), AV_PIX_FMT_NV12 },
+};
+
+DEFINE_GUID(video_decoder_service, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
+DEFINE_GUID(video_processor_service, 0xfc51a552, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
+
+static void dxva2_frames_uninit(AVHWFramesContext *ctx)
+{
+ AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
+ AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
+ DXVA2FramesContext *s = ctx->internal->priv;
+ int i;
+
+ if (frames_hwctx->decoder_to_release)
+ IDirectXVideoDecoder_Release(frames_hwctx->decoder_to_release);
+
+ if (s->surfaces_internal) {
+ for (i = 0; i < frames_hwctx->nb_surfaces; i++) {
+ if (s->surfaces_internal[i])
+ IDirect3DSurface9_Release(s->surfaces_internal[i]);
+ }
+ }
+ av_freep(&s->surfaces_internal);
+
+ if (s->service) {
+ IDirectXVideoAccelerationService_Release(s->service);
+ s->service = NULL;
+ }
+
+ if (s->device_handle != INVALID_HANDLE_VALUE) {
+ IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, s->device_handle);
+ s->device_handle = INVALID_HANDLE_VALUE;
+ }
+}
+
+static AVBufferRef *dxva2_pool_alloc(void *opaque, int size)
+{
+ AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
+ DXVA2FramesContext *s = ctx->internal->priv;
+ AVDXVA2FramesContext *hwctx = ctx->hwctx;
+
+ 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],
+ sizeof(*hwctx->surfaces), NULL, 0, 0);
+ }
+
+ return NULL;
+}
+
+static int dxva2_init_pool(AVHWFramesContext *ctx)
+{
+ AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
+ AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
+ DXVA2FramesContext *s = ctx->internal->priv;
+ int decode = (frames_hwctx->surface_type == DXVA2_VideoDecoderRenderTarget);
+
+ int i;
+ HRESULT hr;
+
+ if (ctx->initial_pool_size <= 0)
+ return 0;
+
+ hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &s->device_handle);
+ if (FAILED(hr)) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
+ return AVERROR_UNKNOWN;
+ }
+
+ hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr,
+ s->device_handle,
+ decode ? &video_decoder_service : &video_processor_service,
+ (void **)&s->service);
+ if (FAILED(hr)) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to create the video service\n");
+ return AVERROR_UNKNOWN;
+ }
+
+ for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
+ if (ctx->sw_format == supported_formats[i].pix_fmt) {
+ s->format = supported_formats[i].d3d_format;
+ break;
+ }
+ }
+ if (i == FF_ARRAY_ELEMS(supported_formats)) {
+ av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
+ av_get_pix_fmt_name(ctx->sw_format));
+ return AVERROR(EINVAL);
+ }
+
+ s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
+ sizeof(*s->surfaces_internal));
+ if (!s->surfaces_internal)
+ return AVERROR(ENOMEM);
+
+ hr = IDirectXVideoAccelerationService_CreateSurface(s->service,
+ ctx->width, ctx->height,
+ ctx->initial_pool_size - 1,
+ s->format, D3DPOOL_DEFAULT, 0,
+ frames_hwctx->surface_type,
+ s->surfaces_internal, NULL);
+ if (FAILED(hr)) {
+ av_log(ctx, AV_LOG_ERROR, "Could not create the surfaces\n");
+ return AVERROR_UNKNOWN;
+ }
+
+ ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(*s->surfaces_internal),
+ ctx, dxva2_pool_alloc, NULL);
+ if (!ctx->internal->pool_internal)
+ return AVERROR(ENOMEM);
+
+ frames_hwctx->surfaces = s->surfaces_internal;
+ frames_hwctx->nb_surfaces = ctx->initial_pool_size;
+
+ return 0;
+}
+
+static int dxva2_frames_init(AVHWFramesContext *ctx)
+{
+ AVDXVA2FramesContext *hwctx = ctx->hwctx;
+ DXVA2FramesContext *s = ctx->internal->priv;
+ int ret;
+
+ if (hwctx->surface_type != DXVA2_VideoDecoderRenderTarget &&
+ hwctx->surface_type != DXVA2_VideoProcessorRenderTarget) {
+ av_log(ctx, AV_LOG_ERROR, "Unknown surface type: %lu\n",
+ hwctx->surface_type);
+ return AVERROR(EINVAL);
+ }
+
+ s->device_handle = INVALID_HANDLE_VALUE;
+
+ /* init the frame pool if the caller didn't provide one */
+ if (!ctx->pool) {
+ ret = dxva2_init_pool(ctx);
+ if (ret < 0) {
+ av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+static int dxva2_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_DXVA2_VLD;
+ frame->width = ctx->width;
+ frame->height = ctx->height;
+
+ return 0;
+}
+
+static int dxva2_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;
+}
+
+static int dxva2_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
+ const AVFrame *src)
+{
+ IDirect3DSurface9 *surface;
+ D3DSURFACE_DESC surfaceDesc;
+ D3DLOCKED_RECT LockedRect;
+ HRESULT hr;
+
+ int download = !!src->hw_frames_ctx;
+
+ surface = (IDirect3DSurface9*)(download ? src->data[3] : dst->data[3]);
+
+ hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
+ if (FAILED(hr)) {
+ av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
+ return AVERROR_UNKNOWN;
+ }
+
+ hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL,
+ download ? D3DLOCK_READONLY : D3DLOCK_DISCARD);
+ if (FAILED(hr)) {
+ av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
+ return AVERROR_UNKNOWN;
+ }
+
+ if (download) {
+ av_image_copy_plane(dst->data[0], dst->linesize[0],
+ (uint8_t*)LockedRect.pBits, LockedRect.Pitch,
+ src->width, src->height);
+ av_image_copy_plane(dst->data[1], dst->linesize[1],
+ (uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
+ LockedRect.Pitch, src->width, src->height / 2);
+ } else {
+ av_image_copy_plane((uint8_t*)LockedRect.pBits, LockedRect.Pitch,
+ dst->data[0], dst->linesize[0],
+ src->width, src->height);
+ av_image_copy_plane((uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
+ LockedRect.Pitch, dst->data[1], dst->linesize[1],
+ src->width, src->height / 2);
+ }
+
+ IDirect3DSurface9_UnlockRect(surface);
+
+ return 0;
+}
+
+const HWContextType ff_hwcontext_type_dxva2 = {
+ .type = AV_HWDEVICE_TYPE_DXVA2,
+ .name = "DXVA2",
+
+ .device_hwctx_size = sizeof(AVDXVA2DeviceContext),
+ .frames_hwctx_size = sizeof(AVDXVA2FramesContext),
+ .frames_priv_size = sizeof(DXVA2FramesContext),
+
+ .frames_init = dxva2_frames_init,
+ .frames_uninit = dxva2_frames_uninit,
+ .frames_get_buffer = dxva2_get_buffer,
+ .transfer_get_formats = dxva2_transfer_get_formats,
+ .transfer_data_to = dxva2_transfer_data,
+ .transfer_data_from = dxva2_transfer_data,
+
+ .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_NONE },
+};
diff --git a/libavutil/hwcontext_dxva2.h b/libavutil/hwcontext_dxva2.h
new file mode 100644
index 0000000..6c36cb4
--- /dev/null
+++ b/libavutil/hwcontext_dxva2.h
@@ -0,0 +1,72 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * 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.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * 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
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+
+#ifndef AVUTIL_HWCONTEXT_DXVA2_H
+#define AVUTIL_HWCONTEXT_DXVA2_H
+
+/**
+ * @file
+ * An API-specific header for AV_HWDEVICE_TYPE_DXVA2.
+ *
+ * Only fixed-size pools are supported.
+ *
+ * For user-allocated pools, AVHWFramesContext.pool must return AVBufferRefs
+ * with the data pointer set to a pointer to IDirect3DSurface9.
+ */
+
+#include <d3d9.h>
+#include <dxva2api.h>
+
+/**
+ * This struct is allocated as AVHWDeviceContext.hwctx
+ */
+typedef struct AVDXVA2DeviceContext {
+ IDirect3DDeviceManager9 *devmgr;
+} AVDXVA2DeviceContext;
+
+/**
+ * This struct is allocated as AVHWFramesContext.hwctx
+ */
+typedef struct AVDXVA2FramesContext {
+ /**
+ * The surface type (e.g. DXVA2_VideoProcessorRenderTarget or
+ * DXVA2_VideoDecoderRenderTarget). Must be set by the caller.
+ */
+ DWORD surface_type;
+
+ /**
+ * The surface pool. When an external pool is not provided by the caller,
+ * this will be managed (allocated and filled on init, freed on uninit) by
+ * libavutil.
+ */
+ IDirect3DSurface9 **surfaces;
+ int nb_surfaces;
+
+ /**
+ * Certain drivers require the decoder to be destroyed before the surfaces.
+ * To allow internally managed pools to work properly in such cases, this
+ * field is provided.
+ *
+ * If it is non-NULL, libavutil will call IDirectXVideoDecoder_Release() on
+ * it just before the internal surface pool is freed.
+ */
+ IDirectXVideoDecoder *decoder_to_release;
+} AVDXVA2FramesContext;
+
+#endif /* AVUTIL_HWCONTEXT_DXVA2_H */
diff --git a/libavutil/hwcontext_internal.h b/libavutil/hwcontext_internal.h
index f18b616..e62324c 100644
--- a/libavutil/hwcontext_internal.h
+++ b/libavutil/hwcontext_internal.h
@@ -97,6 +97,7 @@ struct AVHWFramesInternal {
};
extern const HWContextType ff_hwcontext_type_cuda;
+extern const HWContextType ff_hwcontext_type_dxva2;
extern const HWContextType ff_hwcontext_type_vaapi;
extern const HWContextType ff_hwcontext_type_vdpau;
diff --git a/libavutil/version.h b/libavutil/version.h
index 35b6374..588ab5a 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -64,7 +64,7 @@
*/
#define LIBAVUTIL_VERSION_MAJOR 55
-#define LIBAVUTIL_VERSION_MINOR 24
+#define LIBAVUTIL_VERSION_MINOR 25
#define LIBAVUTIL_VERSION_MICRO 100
#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
======================================================================
diff --cc doc/APIchanges
index 15d52fe,b89303a..4b93596
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@@ -15,11 -13,13 +15,14 @@@ libavutil: 2015-08-2
API changes, most recent first:
-2016-xx-xx - xxxxxxx - lavu 55.11.0 - hwcontext_dxva2.h
++2016-06-22 - xxxxxxx - lavu 55.25.100 - hwcontext_dxva2.h
+ Add new installed header with DXVA2-specific hwcontext definitions.
+
-2016-xx-xx - xxxxxxx - lavu 55.10.0 - opt.h
- Add av_opt_copy().
+2016-04-27 - fb91871 - lavu 55.23.100 - log.h
+ Add a new function av_log_format_line2() which returns number of bytes
+ written to the target buffer.
-2016-xx-xx - xxxxxxx - lavc 57.16.0 - avcodec.h
+2016-04-21 - 7fc329e - lavc 57.37.100 - avcodec.h
Add a new audio/video encoding and decoding API with decoupled input
and output -- avcodec_send_packet(), avcodec_receive_frame(),
avcodec_send_frame() and avcodec_receive_packet().
diff --cc libavutil/Makefile
index 1f05215,637ad3b..1e06176
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@@ -129,40 -97,27 +130,42 @@@ OBJS = adler32.
opt.o \
parseutils.o \
pixdesc.o \
+ pixelutils.o \
random_seed.o \
rational.o \
+ reverse.o \
rc4.o \
+ ripemd.o \
samplefmt.o \
sha.o \
+ sha512.o \
stereo3d.o \
+ threadmessage.o \
time.o \
+ timecode.o \
tree.o \
+ twofish.o \
utils.o \
+ xga_font_data.o \
xtea.o \
+ tea.o \
+
+OBJS-$(!HAVE_ATOMICS_NATIVE) += atomic.o \
OBJS-$(CONFIG_CUDA) += hwcontext_cuda.o
+ OBJS-$(CONFIG_DXVA2) += hwcontext_dxva2.o
OBJS-$(CONFIG_LZO) += lzo.o
+OBJS-$(CONFIG_OPENCL) += opencl.o opencl_internal.o
OBJS-$(CONFIG_VAAPI) += hwcontext_vaapi.o
OBJS-$(CONFIG_VDPAU) += hwcontext_vdpau.o
OBJS += $(COMPAT_OBJS:%=../compat/%)
+# Windows resource file
+SLIBOBJS-$(HAVE_GNU_WINDRES) += avutilres.o
+
SKIPHEADERS-$(CONFIG_CUDA) += hwcontext_cuda.h
+ SKIPHEADERS-$(CONFIG_DXVA2) += hwcontext_dxva2.h
SKIPHEADERS-$(CONFIG_VAAPI) += hwcontext_vaapi.h
SKIPHEADERS-$(CONFIG_VDPAU) += hwcontext_vdpau.h
SKIPHEADERS-$(HAVE_ATOMICS_GCC) += atomic_gcc.h
diff --cc libavutil/hwcontext_dxva2.c
index 0000000,62cca6f..56ae317
mode 000000,100644..100644
--- a/libavutil/hwcontext_dxva2.c
+++ b/libavutil/hwcontext_dxva2.c
@@@ -1,0 -1,292 +1,292 @@@
+ /*
- * This file is part of Libav.
++ * This file is part of FFmpeg.
+ *
- * Libav is free software; you can redistribute it and/or
++ * FFmpeg is free software; you can redistribute it and/or
+ * 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.
+ *
- * Libav is distributed in the hope that it will be useful,
++ * FFmpeg is distributed in the hope that it will be useful,
+ * 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
- * License along with Libav; if not, write to the Free Software
++ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+ #include <windows.h>
+
+ #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600
+ #undef _WIN32_WINNT
+ #define _WIN32_WINNT 0x0600
+ #endif
+ #define DXVA2API_USE_BITFIELDS
+ #define COBJMACROS
+
+ #include <d3d9.h>
+ #include <dxva2api.h>
+ #include <initguid.h>
+
+ #include "common.h"
+ #include "hwcontext.h"
+ #include "hwcontext_dxva2.h"
+ #include "hwcontext_internal.h"
+ #include "imgutils.h"
+ #include "pixdesc.h"
+ #include "pixfmt.h"
+
+ typedef struct DXVA2FramesContext {
+ IDirect3DSurface9 **surfaces_internal;
+ int nb_surfaces_used;
+
+ HANDLE device_handle;
+ IDirectXVideoAccelerationService *service;
+
+ D3DFORMAT format;
+ } DXVA2FramesContext;
+
+ static const struct {
+ D3DFORMAT d3d_format;
+ enum AVPixelFormat pix_fmt;
+ } supported_formats[] = {
+ { MKTAG('N', 'V', '1', '2'), AV_PIX_FMT_NV12 },
+ };
+
+ DEFINE_GUID(video_decoder_service, 0xfc51a551, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
+ DEFINE_GUID(video_processor_service, 0xfc51a552, 0xd5e7, 0x11d9, 0xaf, 0x55, 0x00, 0x05, 0x4e, 0x43, 0xff, 0x02);
+
+ static void dxva2_frames_uninit(AVHWFramesContext *ctx)
+ {
+ AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
+ AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
+ DXVA2FramesContext *s = ctx->internal->priv;
+ int i;
+
+ if (frames_hwctx->decoder_to_release)
+ IDirectXVideoDecoder_Release(frames_hwctx->decoder_to_release);
+
+ if (s->surfaces_internal) {
+ for (i = 0; i < frames_hwctx->nb_surfaces; i++) {
+ if (s->surfaces_internal[i])
+ IDirect3DSurface9_Release(s->surfaces_internal[i]);
+ }
+ }
+ av_freep(&s->surfaces_internal);
+
+ if (s->service) {
+ IDirectXVideoAccelerationService_Release(s->service);
+ s->service = NULL;
+ }
+
+ if (s->device_handle != INVALID_HANDLE_VALUE) {
+ IDirect3DDeviceManager9_CloseDeviceHandle(device_hwctx->devmgr, s->device_handle);
+ s->device_handle = INVALID_HANDLE_VALUE;
+ }
+ }
+
+ static AVBufferRef *dxva2_pool_alloc(void *opaque, int size)
+ {
+ AVHWFramesContext *ctx = (AVHWFramesContext*)opaque;
+ DXVA2FramesContext *s = ctx->internal->priv;
+ AVDXVA2FramesContext *hwctx = ctx->hwctx;
+
+ 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],
+ sizeof(*hwctx->surfaces), NULL, 0, 0);
+ }
+
+ return NULL;
+ }
+
+ static int dxva2_init_pool(AVHWFramesContext *ctx)
+ {
+ AVDXVA2FramesContext *frames_hwctx = ctx->hwctx;
+ AVDXVA2DeviceContext *device_hwctx = ctx->device_ctx->hwctx;
+ DXVA2FramesContext *s = ctx->internal->priv;
+ int decode = (frames_hwctx->surface_type == DXVA2_VideoDecoderRenderTarget);
+
+ int i;
+ HRESULT hr;
+
+ if (ctx->initial_pool_size <= 0)
+ return 0;
+
+ hr = IDirect3DDeviceManager9_OpenDeviceHandle(device_hwctx->devmgr, &s->device_handle);
+ if (FAILED(hr)) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to open device handle\n");
+ return AVERROR_UNKNOWN;
+ }
+
+ hr = IDirect3DDeviceManager9_GetVideoService(device_hwctx->devmgr,
+ s->device_handle,
+ decode ? &video_decoder_service : &video_processor_service,
+ (void **)&s->service);
+ if (FAILED(hr)) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to create the video service\n");
+ return AVERROR_UNKNOWN;
+ }
+
+ for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
+ if (ctx->sw_format == supported_formats[i].pix_fmt) {
+ s->format = supported_formats[i].d3d_format;
+ break;
+ }
+ }
+ if (i == FF_ARRAY_ELEMS(supported_formats)) {
+ av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n",
+ av_get_pix_fmt_name(ctx->sw_format));
+ return AVERROR(EINVAL);
+ }
+
+ s->surfaces_internal = av_mallocz_array(ctx->initial_pool_size,
+ sizeof(*s->surfaces_internal));
+ if (!s->surfaces_internal)
+ return AVERROR(ENOMEM);
+
+ hr = IDirectXVideoAccelerationService_CreateSurface(s->service,
+ ctx->width, ctx->height,
+ ctx->initial_pool_size - 1,
+ s->format, D3DPOOL_DEFAULT, 0,
+ frames_hwctx->surface_type,
+ s->surfaces_internal, NULL);
+ if (FAILED(hr)) {
+ av_log(ctx, AV_LOG_ERROR, "Could not create the surfaces\n");
+ return AVERROR_UNKNOWN;
+ }
+
+ ctx->internal->pool_internal = av_buffer_pool_init2(sizeof(*s->surfaces_internal),
+ ctx, dxva2_pool_alloc, NULL);
+ if (!ctx->internal->pool_internal)
+ return AVERROR(ENOMEM);
+
+ frames_hwctx->surfaces = s->surfaces_internal;
+ frames_hwctx->nb_surfaces = ctx->initial_pool_size;
+
+ return 0;
+ }
+
+ static int dxva2_frames_init(AVHWFramesContext *ctx)
+ {
+ AVDXVA2FramesContext *hwctx = ctx->hwctx;
+ DXVA2FramesContext *s = ctx->internal->priv;
+ int ret;
+
+ if (hwctx->surface_type != DXVA2_VideoDecoderRenderTarget &&
+ hwctx->surface_type != DXVA2_VideoProcessorRenderTarget) {
+ av_log(ctx, AV_LOG_ERROR, "Unknown surface type: %lu\n",
+ hwctx->surface_type);
+ return AVERROR(EINVAL);
+ }
+
+ s->device_handle = INVALID_HANDLE_VALUE;
+
+ /* init the frame pool if the caller didn't provide one */
+ if (!ctx->pool) {
+ ret = dxva2_init_pool(ctx);
+ if (ret < 0) {
+ av_log(ctx, AV_LOG_ERROR, "Error creating an internal frame pool\n");
+ return ret;
+ }
+ }
+
+ return 0;
+ }
+
+ static int dxva2_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_DXVA2_VLD;
+ frame->width = ctx->width;
+ frame->height = ctx->height;
+
+ return 0;
+ }
+
+ static int dxva2_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;
+ }
+
+ static int dxva2_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
+ const AVFrame *src)
+ {
+ IDirect3DSurface9 *surface;
+ D3DSURFACE_DESC surfaceDesc;
+ D3DLOCKED_RECT LockedRect;
+ HRESULT hr;
+
+ int download = !!src->hw_frames_ctx;
+
+ surface = (IDirect3DSurface9*)(download ? src->data[3] : dst->data[3]);
+
+ hr = IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
+ if (FAILED(hr)) {
+ av_log(ctx, AV_LOG_ERROR, "Error getting a surface description\n");
+ return AVERROR_UNKNOWN;
+ }
+
+ hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL,
+ download ? D3DLOCK_READONLY : D3DLOCK_DISCARD);
+ if (FAILED(hr)) {
+ av_log(ctx, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
+ return AVERROR_UNKNOWN;
+ }
+
+ if (download) {
+ av_image_copy_plane(dst->data[0], dst->linesize[0],
+ (uint8_t*)LockedRect.pBits, LockedRect.Pitch,
+ src->width, src->height);
+ av_image_copy_plane(dst->data[1], dst->linesize[1],
+ (uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
+ LockedRect.Pitch, src->width, src->height / 2);
+ } else {
+ av_image_copy_plane((uint8_t*)LockedRect.pBits, LockedRect.Pitch,
+ dst->data[0], dst->linesize[0],
+ src->width, src->height);
+ av_image_copy_plane((uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height,
+ LockedRect.Pitch, dst->data[1], dst->linesize[1],
+ src->width, src->height / 2);
+ }
+
+ IDirect3DSurface9_UnlockRect(surface);
+
+ return 0;
+ }
+
+ const HWContextType ff_hwcontext_type_dxva2 = {
+ .type = AV_HWDEVICE_TYPE_DXVA2,
+ .name = "DXVA2",
+
+ .device_hwctx_size = sizeof(AVDXVA2DeviceContext),
+ .frames_hwctx_size = sizeof(AVDXVA2FramesContext),
+ .frames_priv_size = sizeof(DXVA2FramesContext),
+
+ .frames_init = dxva2_frames_init,
+ .frames_uninit = dxva2_frames_uninit,
+ .frames_get_buffer = dxva2_get_buffer,
+ .transfer_get_formats = dxva2_transfer_get_formats,
+ .transfer_data_to = dxva2_transfer_data,
+ .transfer_data_from = dxva2_transfer_data,
+
+ .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_NONE },
+ };
diff --cc libavutil/hwcontext_dxva2.h
index 0000000,2290c26..6c36cb4
mode 000000,100644..100644
--- a/libavutil/hwcontext_dxva2.h
+++ b/libavutil/hwcontext_dxva2.h
@@@ -1,0 -1,72 +1,72 @@@
+ /*
- * This file is part of Libav.
++ * This file is part of FFmpeg.
+ *
- * Libav is free software; you can redistribute it and/or
++ * FFmpeg is free software; you can redistribute it and/or
+ * 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.
+ *
- * Libav is distributed in the hope that it will be useful,
++ * FFmpeg is distributed in the hope that it will be useful,
+ * 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
- * License along with Libav; if not, write to the Free Software
++ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+
+ #ifndef AVUTIL_HWCONTEXT_DXVA2_H
+ #define AVUTIL_HWCONTEXT_DXVA2_H
+
+ /**
+ * @file
+ * An API-specific header for AV_HWDEVICE_TYPE_DXVA2.
+ *
+ * Only fixed-size pools are supported.
+ *
+ * For user-allocated pools, AVHWFramesContext.pool must return AVBufferRefs
+ * with the data pointer set to a pointer to IDirect3DSurface9.
+ */
+
+ #include <d3d9.h>
+ #include <dxva2api.h>
+
+ /**
+ * This struct is allocated as AVHWDeviceContext.hwctx
+ */
+ typedef struct AVDXVA2DeviceContext {
+ IDirect3DDeviceManager9 *devmgr;
+ } AVDXVA2DeviceContext;
+
+ /**
+ * This struct is allocated as AVHWFramesContext.hwctx
+ */
+ typedef struct AVDXVA2FramesContext {
+ /**
+ * The surface type (e.g. DXVA2_VideoProcessorRenderTarget or
+ * DXVA2_VideoDecoderRenderTarget). Must be set by the caller.
+ */
+ DWORD surface_type;
+
+ /**
+ * The surface pool. When an external pool is not provided by the caller,
+ * this will be managed (allocated and filled on init, freed on uninit) by
+ * libavutil.
+ */
+ IDirect3DSurface9 **surfaces;
+ int nb_surfaces;
+
+ /**
+ * Certain drivers require the decoder to be destroyed before the surfaces.
+ * To allow internally managed pools to work properly in such cases, this
+ * field is provided.
+ *
+ * If it is non-NULL, libavutil will call IDirectXVideoDecoder_Release() on
+ * it just before the internal surface pool is freed.
+ */
+ IDirectXVideoDecoder *decoder_to_release;
+ } AVDXVA2FramesContext;
+
+ #endif /* AVUTIL_HWCONTEXT_DXVA2_H */
diff --cc libavutil/version.h
index 35b6374,7bbac41..588ab5a
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@@ -63,9 -53,9 +63,9 @@@
* @{
*/
-#define LIBAVUTIL_VERSION_MAJOR 55
-#define LIBAVUTIL_VERSION_MINOR 11
-#define LIBAVUTIL_VERSION_MICRO 0
+#define LIBAVUTIL_VERSION_MAJOR 55
- #define LIBAVUTIL_VERSION_MINOR 24
++#define LIBAVUTIL_VERSION_MINOR 25
+#define LIBAVUTIL_VERSION_MICRO 100
#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
LIBAVUTIL_VERSION_MINOR, \
More information about the ffmpeg-cvslog
mailing list