amfcodec: Add support for h264/h265 encoding

New encoder implementations for AMD GPU using AMF API

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/2109>
This commit is contained in:
Seungha Yang 2022-03-31 04:16:24 +09:00 committed by GStreamer Marge Bot
parent c41a72a1ff
commit f413449a28
12 changed files with 4135 additions and 0 deletions

View file

@ -79,6 +79,7 @@ option('x11', type : 'feature', value : 'auto', description : 'X11 support in Vu
option('aes', type : 'feature', value : 'auto', description : 'AES encryption/decryption plugin') option('aes', type : 'feature', value : 'auto', description : 'AES encryption/decryption plugin')
option('aom', type : 'feature', value : 'auto', description : 'AOM AV1 video codec plugin') option('aom', type : 'feature', value : 'auto', description : 'AOM AV1 video codec plugin')
option('avtp', type : 'feature', value : 'auto', description : 'Audio/Video Transport Protocol (AVTP) plugin') option('avtp', type : 'feature', value : 'auto', description : 'Audio/Video Transport Protocol (AVTP) plugin')
option('amfcodec', type : 'feature', value : 'auto', description : 'AMD AMF codec plugin')
option('androidmedia', type : 'feature', value : 'auto', description : 'Video capture and codec plugins for Android') option('androidmedia', type : 'feature', value : 'auto', description : 'Video capture and codec plugins for Android')
option('applemedia', type : 'feature', value : 'auto', description : 'Video capture and codec access plugins for macOS and iOS') option('applemedia', type : 'feature', value : 'auto', description : 'Video capture and codec access plugins for macOS and iOS')
option('asio', type : 'feature', value : 'auto', description : 'Steinberg Audio Streaming Input Output (ASIO) plugin') option('asio', type : 'feature', value : 'auto', description : 'Steinberg Audio Streaming Input Output (ASIO) plugin')

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,78 @@
/* GStreamer
* Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#pragma once
#include <gst/gst.h>
#include <gst/video/video.h>
#include "gstamfutils.h"
G_BEGIN_DECLS
#define GST_TYPE_AMF_ENCODER (gst_amf_encoder_get_type())
#define GST_AMF_ENCODER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_AMF_ENCODER, GstAmfEncoder))
#define GST_AMF_ENCODER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_AMF_ENCODER, GstAmfEncoderClass))
#define GST_IS_AMF_ENCODER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_AMF_ENCODER))
#define GST_IS_AMF_ENCODER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_AMF_ENCODER))
#define GST_AMF_ENCODER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_AMF_ENCODER, GstAmfEncoderClass))
#define GST_AMF_ENCODER_CAST(obj) ((GstAmfEncoder *)obj)
typedef struct _GstAmfEncoder GstAmfEncoder;
typedef struct _GstAmfEncoderClass GstAmfEncoderClass;
typedef struct _GstAmfEncoderPrivate GstAmfEncoderPrivate;
struct _GstAmfEncoder
{
GstVideoEncoder parent;
GstAmfEncoderPrivate *priv;
};
struct _GstAmfEncoderClass
{
GstVideoEncoderClass parent_class;
gboolean (*set_format) (GstAmfEncoder * encoder,
GstVideoCodecState * state,
gpointer component);
gboolean (*set_output_state) (GstAmfEncoder * encoder,
GstVideoCodecState * state,
gpointer component);
gboolean (*set_surface_prop) (GstAmfEncoder * encoder,
GstVideoCodecFrame * frame,
gpointer surface);
GstBuffer * (*create_output_buffer) (GstAmfEncoder * encoder,
gpointer data,
gboolean * sync_point);
gboolean (*check_reconfigure) (GstAmfEncoder * encoder);
};
GType gst_amf_encoder_get_type (void);
void gst_amf_encoder_set_subclass_data (GstAmfEncoder * encoder,
gint64 adapter_luid,
const wchar_t * codec_id);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstAmfEncoder, gst_object_unref)
G_END_DECLS

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,32 @@
/* GStreamer
* Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#pragma once
#include "gstamfencoder.h"
#include <gst/d3d11/gstd3d11.h>
G_BEGIN_DECLS
void gst_amf_h264_enc_register_d3d11 (GstPlugin * plugin,
GstD3D11Device * device,
gpointer context,
guint rank);
G_END_DECLS

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,32 @@
/* GStreamer
* Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#pragma once
#include "gstamfencoder.h"
#include <gst/d3d11/gstd3d11.h>
G_BEGIN_DECLS
void gst_amf_h265_enc_register_d3d11 (GstPlugin * plugin,
GstD3D11Device * device,
gpointer context,
guint rank);
G_END_DECLS

View file

@ -0,0 +1,140 @@
/* GStreamer
* Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <core/Factory.h>
#include "gstamfutils.h"
#include <gmodule.h>
using namespace amf;
AMFFactory *_factory = nullptr;
static gboolean loaded = FALSE;
static gboolean
gst_amf_load_library (void)
{
AMF_RESULT result;
GModule *amf_module = nullptr;
AMFInit_Fn init_func = nullptr;
amf_module = g_module_open (AMF_DLL_NAMEA, G_MODULE_BIND_LAZY);
if (!amf_module)
return FALSE;
if (!g_module_symbol (amf_module, AMF_INIT_FUNCTION_NAME, (gpointer *)
& init_func)) {
g_module_close (amf_module);
amf_module = nullptr;
return FALSE;
}
result = init_func (AMF_FULL_VERSION, &_factory);
if (result != AMF_OK) {
g_module_close (amf_module);
amf_module = nullptr;
_factory = nullptr;
return FALSE;
}
return TRUE;
}
gboolean
gst_amf_init_once (void)
{
static gsize init_once = 0;
if (g_once_init_enter (&init_once)) {
loaded = gst_amf_load_library ();
g_once_init_leave (&init_once, 1);
}
return loaded;
}
gpointer
gst_amf_get_factory (void)
{
return (gpointer) _factory;
}
const gchar *
gst_amf_result_to_string (AMF_RESULT result)
{
#define CASE(err) \
case err: \
return G_STRINGIFY (err);
switch (result) {
CASE (AMF_OK);
CASE (AMF_FAIL);
CASE (AMF_UNEXPECTED);
CASE (AMF_ACCESS_DENIED);
CASE (AMF_INVALID_ARG);
CASE (AMF_OUT_OF_RANGE);
CASE (AMF_OUT_OF_MEMORY);
CASE (AMF_INVALID_POINTER);
CASE (AMF_NO_INTERFACE);
CASE (AMF_NOT_IMPLEMENTED);
CASE (AMF_NOT_SUPPORTED);
CASE (AMF_NOT_FOUND);
CASE (AMF_ALREADY_INITIALIZED);
CASE (AMF_NOT_INITIALIZED);
CASE (AMF_INVALID_FORMAT);
CASE (AMF_WRONG_STATE);
CASE (AMF_FILE_NOT_OPEN);
CASE (AMF_NO_DEVICE);
CASE (AMF_DIRECTX_FAILED);
CASE (AMF_OPENCL_FAILED);
CASE (AMF_GLX_FAILED);
CASE (AMF_XV_FAILED);
CASE (AMF_ALSA_FAILED);
CASE (AMF_EOF);
CASE (AMF_REPEAT);
CASE (AMF_INPUT_FULL);
CASE (AMF_RESOLUTION_CHANGED);
CASE (AMF_RESOLUTION_UPDATED);
CASE (AMF_INVALID_DATA_TYPE);
CASE (AMF_INVALID_RESOLUTION);
CASE (AMF_CODEC_NOT_SUPPORTED);
CASE (AMF_SURFACE_FORMAT_NOT_SUPPORTED);
CASE (AMF_SURFACE_MUST_BE_SHARED);
CASE (AMF_DECODER_NOT_PRESENT);
CASE (AMF_DECODER_SURFACE_ALLOCATION_FAILED);
CASE (AMF_DECODER_NO_FREE_SURFACES);
CASE (AMF_ENCODER_NOT_PRESENT);
CASE (AMF_DEM_ERROR);
CASE (AMF_DEM_PROPERTY_READONLY);
CASE (AMF_DEM_REMOTE_DISPLAY_CREATE_FAILED);
CASE (AMF_DEM_START_ENCODING_FAILED);
CASE (AMF_DEM_QUERY_OUTPUT_FAILED);
CASE (AMF_TAN_CLIPPING_WAS_REQUIRED);
CASE (AMF_TAN_UNSUPPORTED_VERSION);
CASE (AMF_NEED_MORE_INPUT);
default:
break;
}
#undef CASE
return "Unknown";
}

View file

@ -0,0 +1,35 @@
/* GStreamer
* Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#pragma once
#include <gst/gst.h>
#include <core/Result.h>
G_BEGIN_DECLS
gboolean gst_amf_init_once (void);
gpointer gst_amf_get_factory (void);
const gchar * gst_amf_result_to_string (AMF_RESULT result);
#define GST_AMF_RESULT_FORMAT "s (%d)"
#define GST_AMF_RESULT_ARGS(r) gst_amf_result_to_string (r), r
G_END_DECLS

View file

@ -0,0 +1,71 @@
amf_sources = [
'gstamfencoder.cpp',
'gstamfh264enc.cpp',
'gstamfh265enc.cpp',
'gstamfutils.cpp',
'plugin.cpp',
]
amf_option = get_option('amfcodec')
if amf_option.disabled()
subdir_done()
endif
platform_deps = []
extra_args = ['-DGST_USE_UNSTABLE_API']
if host_system == 'windows'
if not gstd3d11_dep.found()
if amf_option.enabled()
error('The amf was enabled explicitly, but required d3d11 was not found')
else
subdir_done()
endif
endif
code = '''
#include <windows.h>
#if !(WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP))
#error "Not building for UWP"
#endif'''
if cc.compiles(code, name : 'building for UWP')
if amf_option.enabled()
error('amf plugin does not support UWP')
else
subdir_done()
endif
endif
# Encoder needs to do sleep() by API design
winmm_lib = cc.find_library('winmm', required: amf_option)
if not winmm_lib.found() or not cc.has_header('timeapi.h')
subdir_done()
endif
platform_deps += [gstd3d11_dep, winmm_lib]
else
if amf_option.enabled()
error('amf plugin supports only Windows')
else
subdir_done()
endif
endif
# and MinGW 32bits compiler seems to be complaining about redundant-decls
if cc.get_id() != 'msvc'
extra_args += cc.get_supported_arguments([
'-Wno-redundant-decls',
])
endif
gstamfcodec = library('gstamfcodec',
amf_sources,
c_args : gst_plugins_bad_args + extra_args,
cpp_args : gst_plugins_bad_args + extra_args,
include_directories : [configinc, include_directories('include')],
dependencies : [gstbase_dep, gstvideo_dep, gstpbutils_dep, gstcodecparsers_dep, gmodule_dep] + platform_deps,
install : true,
install_dir : plugins_install_dir,
)
pkgconfig.generate(gstamfcodec, install_dir : plugins_pkgconfig_install_dir)
plugins += [gstamfcodec]

View file

@ -0,0 +1,117 @@
/* GStreamer
* Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
#include <gst/d3d11/gstd3d11.h>
#include <wrl.h>
#include <core/Factory.h>
#include <versionhelpers.h>
#include "gstamfutils.h"
#include "gstamfh264enc.h"
#include "gstamfh265enc.h"
/* *INDENT-OFF* */
using namespace Microsoft::WRL;
using namespace amf;
/* *INDENT-ON* */
static gboolean
plugin_init (GstPlugin * plugin)
{
AMFFactory *amf_factory;
ComPtr < IDXGIFactory1 > factory;
HRESULT hr;
if (!IsWindows8OrGreater ())
return TRUE;
if (!gst_amf_init_once ())
return TRUE;
amf_factory = (AMFFactory *) gst_amf_get_factory ();
if (!amf_factory)
return TRUE;
hr = CreateDXGIFactory1 (IID_PPV_ARGS (&factory));
if (FAILED (hr))
return TRUE;
/* Enumerate AMD GPUs */
for (guint idx = 0;; idx++) {
ComPtr < IDXGIAdapter1 > adapter;
AMFContextPtr context;
DXGI_ADAPTER_DESC desc;
gint64 luid;
GstD3D11Device *device;
ID3D11Device *device_handle;
AMF_RESULT result;
D3D_FEATURE_LEVEL feature_level;
AMF_DX_VERSION dx_ver = AMF_DX11_1;
hr = factory->EnumAdapters1 (idx, &adapter);
if (FAILED (hr))
break;
hr = adapter->GetDesc (&desc);
if (FAILED (hr))
continue;
if (desc.VendorId != 0x1002 && desc.VendorId != 0x1022)
continue;
luid = gst_d3d11_luid_to_int64 (&desc.AdapterLuid);
device = gst_d3d11_device_new_for_adapter_luid (luid,
D3D11_CREATE_DEVICE_BGRA_SUPPORT);
if (!device)
continue;
device_handle = gst_d3d11_device_get_device_handle (device);
feature_level = device_handle->GetFeatureLevel ();
if (feature_level >= D3D_FEATURE_LEVEL_11_1)
dx_ver = AMF_DX11_1;
else
dx_ver = AMF_DX11_0;
result = amf_factory->CreateContext (&context);
if (result == AMF_OK)
result = context->InitDX11 (device_handle, dx_ver);
if (result == AMF_OK) {
gst_amf_h264_enc_register_d3d11 (plugin, device,
(gpointer) context.GetPtr (), GST_RANK_NONE);
gst_amf_h265_enc_register_d3d11 (plugin, device,
(gpointer) context.GetPtr (), GST_RANK_NONE);
}
gst_clear_object (&device);
}
return TRUE;
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
amfcodec,
"AMD AMF Codec plugin",
plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)

View file

@ -1,3 +1,4 @@
subdir('amfcodec')
subdir('androidmedia') subdir('androidmedia')
subdir('applemedia') subdir('applemedia')
subdir('asio') subdir('asio')