2021-12-19 12:39:53 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2021 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.
|
|
|
|
*/
|
|
|
|
|
2022-10-21 10:08:49 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-qsvvp9enc
|
|
|
|
* @title: qsvvp9enc
|
|
|
|
*
|
|
|
|
* Intel Quick Sync VP9 encoder
|
|
|
|
*
|
|
|
|
* ## Example launch line
|
|
|
|
* ```
|
|
|
|
* gst-launch-1.0 videotestsrc ! qsvvp9enc ! vp9parse ! matroskamux ! filesink location=out.mkv
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
|
|
|
|
2021-12-19 12:39:53 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstqsvvp9enc.h"
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <set>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <gst/d3d11/gstd3d11.h>
|
|
|
|
#else
|
2022-03-25 13:04:05 +00:00
|
|
|
#include <gst/va/gstva.h>
|
2021-12-19 12:39:53 +00:00
|
|
|
#endif
|
|
|
|
|
2024-04-01 16:36:28 +00:00
|
|
|
#ifdef HAVE_GST_D3D12
|
|
|
|
#include <gst/d3d12/gstd3d12.h>
|
|
|
|
#endif
|
|
|
|
|
2022-04-16 15:00:55 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_qsv_vp9_enc_debug);
|
2021-12-19 12:39:53 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_qsv_vp9_enc_debug
|
|
|
|
|
2022-10-21 10:08:49 +00:00
|
|
|
/**
|
|
|
|
* GstQsvVP9EncRateControl:
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2021-12-19 12:39:53 +00:00
|
|
|
#define GST_TYPE_QSV_VP9_ENC_RATE_CONTROL (gst_qsv_vp9_enc_rate_control_get_type ())
|
|
|
|
static GType
|
|
|
|
gst_qsv_vp9_enc_rate_control_get_type (void)
|
|
|
|
{
|
|
|
|
static GType rate_control_type = 0;
|
|
|
|
static const GEnumValue rate_controls[] = {
|
2022-10-21 10:08:49 +00:00
|
|
|
/**
|
|
|
|
* GstQsvVP9EncRateControl::cbr:
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2021-12-19 12:39:53 +00:00
|
|
|
{MFX_RATECONTROL_CBR, "Constant Bitrate", "cbr"},
|
2022-10-21 10:08:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GstQsvVP9EncRateControl::vbr:
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2021-12-19 12:39:53 +00:00
|
|
|
{MFX_RATECONTROL_VBR, "Variable Bitrate", "vbr"},
|
2022-10-21 10:08:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GstQsvVP9EncRateControl::cqp:
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2022-03-27 14:27:54 +00:00
|
|
|
{MFX_RATECONTROL_CQP, "Constant Quantizer", "cqp"},
|
2022-10-21 10:08:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GstQsvVP9EncRateControl::icq:
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
2022-03-27 14:27:54 +00:00
|
|
|
{MFX_RATECONTROL_ICQ, "Intelligent CQP", "icq"},
|
2021-12-19 12:39:53 +00:00
|
|
|
{0, nullptr, nullptr}
|
|
|
|
};
|
|
|
|
|
2023-02-19 13:43:57 +00:00
|
|
|
GST_QSV_CALL_ONCE_BEGIN {
|
|
|
|
rate_control_type =
|
2021-12-19 12:39:53 +00:00
|
|
|
g_enum_register_static ("GstQsvVP9EncRateControl", rate_controls);
|
2023-02-19 13:43:57 +00:00
|
|
|
} GST_QSV_CALL_ONCE_END;
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
return rate_control_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2022-03-27 14:27:54 +00:00
|
|
|
PROP_QP_I,
|
|
|
|
PROP_QP_P,
|
2021-12-19 12:39:53 +00:00
|
|
|
PROP_GOP_SIZE,
|
|
|
|
PROP_REF_FRAMES,
|
|
|
|
PROP_BITRATE,
|
|
|
|
PROP_MAX_BITRATE,
|
|
|
|
PROP_RATE_CONTROL,
|
2022-03-27 14:27:54 +00:00
|
|
|
PROP_ICQ_QUALITY,
|
2021-12-19 12:39:53 +00:00
|
|
|
};
|
|
|
|
|
2022-03-27 14:27:54 +00:00
|
|
|
#define DEFAULT_QP 0
|
2021-12-19 12:39:53 +00:00
|
|
|
#define DEFAULT_GOP_SIZE 0
|
2022-03-27 14:27:54 +00:00
|
|
|
#define DEFAULT_REF_FRAMES 1
|
2021-12-19 12:39:53 +00:00
|
|
|
#define DEFAULT_BITRATE 2000
|
|
|
|
#define DEFAULT_MAX_BITRATE 0
|
2022-04-08 15:00:59 +00:00
|
|
|
#define DEFAULT_RATE_CONTROL MFX_RATECONTROL_VBR
|
2022-03-27 14:27:54 +00:00
|
|
|
#define DEFAULT_IQC_QUALITY 0
|
2021-12-19 12:39:53 +00:00
|
|
|
|
2022-10-21 10:08:49 +00:00
|
|
|
#define DOC_SINK_CAPS_COMM \
|
|
|
|
"format = (string) { NV12, P010_10LE, VUYA, Y410 }, " \
|
|
|
|
"width = (int) [16, 8192 ], " \
|
|
|
|
"height = (int) [16, 8192 ]"
|
|
|
|
|
|
|
|
#define DOC_SINK_CAPS \
|
|
|
|
"video/x-raw(memory:D3D11Memory), " DOC_SINK_CAPS_COMM "; " \
|
2024-04-01 16:36:28 +00:00
|
|
|
"video/x-raw(memory:D3D12Memory), " DOC_SINK_CAPS_COMM "; " \
|
2022-10-21 10:08:49 +00:00
|
|
|
"video/x-raw(memory:VAMemory), " DOC_SINK_CAPS_COMM "; " \
|
|
|
|
"video/x-raw, " DOC_SINK_CAPS_COMM
|
|
|
|
|
|
|
|
#define DOC_SRC_CAPS \
|
|
|
|
"video/x-vp9, width = (int) [16, 8192 ], height = (int) [16, 8192 ], " \
|
|
|
|
"profile = (string) { 0, 2, 1, 3 }"
|
|
|
|
|
2021-12-19 12:39:53 +00:00
|
|
|
typedef struct _GstQsvVP9EncClassData
|
|
|
|
{
|
|
|
|
GstCaps *sink_caps;
|
|
|
|
GstCaps *src_caps;
|
|
|
|
guint impl_index;
|
|
|
|
gint64 adapter_luid;
|
|
|
|
gchar *display_path;
|
2022-04-20 14:06:56 +00:00
|
|
|
gchar *description;
|
2024-04-01 16:36:28 +00:00
|
|
|
gboolean d3d12_interop;
|
2021-12-19 12:39:53 +00:00
|
|
|
} GstQsvVP9EncClassData;
|
|
|
|
|
|
|
|
typedef struct _GstQsvVP9Enc
|
|
|
|
{
|
|
|
|
GstQsvEncoder parent;
|
|
|
|
|
|
|
|
mfxExtVP9Param vp9_param;
|
|
|
|
|
|
|
|
mfxU16 profile;
|
|
|
|
|
|
|
|
GMutex prop_lock;
|
|
|
|
/* protected by prop_lock */
|
|
|
|
gboolean bitrate_updated;
|
|
|
|
gboolean property_updated;
|
|
|
|
|
|
|
|
/* properties */
|
2022-03-27 14:27:54 +00:00
|
|
|
guint qp_i;
|
|
|
|
guint qp_p;
|
2021-12-19 12:39:53 +00:00
|
|
|
guint gop_size;
|
|
|
|
guint ref_frames;
|
|
|
|
guint bitrate;
|
|
|
|
guint max_bitrate;
|
|
|
|
mfxU16 rate_control;
|
2022-03-27 14:27:54 +00:00
|
|
|
guint icq_quality;
|
2021-12-19 12:39:53 +00:00
|
|
|
} GstQsvVP9Enc;
|
|
|
|
|
|
|
|
typedef struct _GstQsvVP9EncClass
|
|
|
|
{
|
|
|
|
GstQsvEncoderClass parent_class;
|
|
|
|
} GstQsvVP9EncClass;
|
|
|
|
|
|
|
|
static GstElementClass *parent_class = nullptr;
|
|
|
|
|
|
|
|
#define GST_QSV_VP9_ENC(object) ((GstQsvVP9Enc *) (object))
|
|
|
|
#define GST_QSV_VP9_ENC_GET_CLASS(object) \
|
|
|
|
(G_TYPE_INSTANCE_GET_CLASS ((object),G_TYPE_FROM_INSTANCE (object),GstQsvVP9EncClass))
|
|
|
|
|
|
|
|
static void gst_qsv_vp9_enc_finalize (GObject * object);
|
|
|
|
static void gst_qsv_vp9_enc_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_qsv_vp9_enc_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
|
|
|
|
static GstCaps *gst_qsv_vp9_enc_getcaps (GstVideoEncoder * encoder,
|
|
|
|
GstCaps * filter);
|
|
|
|
|
|
|
|
static gboolean gst_qsv_vp9_enc_set_format (GstQsvEncoder * encoder,
|
|
|
|
GstVideoCodecState * state, mfxVideoParam * param,
|
|
|
|
GPtrArray * extra_params);
|
|
|
|
static gboolean gst_qsv_vp9_enc_set_output_state (GstQsvEncoder * encoder,
|
|
|
|
GstVideoCodecState * state, mfxSession session);
|
|
|
|
static GstQsvEncoderReconfigure
|
2022-03-27 14:27:54 +00:00
|
|
|
gst_qsv_vp9_enc_check_reconfigure (GstQsvEncoder * encoder, mfxSession session,
|
|
|
|
mfxVideoParam * param, GPtrArray * extra_params);
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_qsv_vp9_enc_class_init (GstQsvVP9EncClass * klass, gpointer data)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
GstVideoEncoderClass *encoder_class = GST_VIDEO_ENCODER_CLASS (klass);
|
|
|
|
GstQsvEncoderClass *qsvenc_class = GST_QSV_ENCODER_CLASS (klass);
|
|
|
|
GstQsvVP9EncClassData *cdata = (GstQsvVP9EncClassData *) data;
|
2022-10-21 10:08:49 +00:00
|
|
|
GstPadTemplate *pad_templ;
|
|
|
|
GstCaps *doc_caps;
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
qsvenc_class->codec_id = MFX_CODEC_VP9;
|
|
|
|
qsvenc_class->impl_index = cdata->impl_index;
|
|
|
|
qsvenc_class->adapter_luid = cdata->adapter_luid;
|
2022-03-02 19:22:15 +00:00
|
|
|
qsvenc_class->display_path = cdata->display_path;
|
2024-04-01 16:36:28 +00:00
|
|
|
qsvenc_class->d3d12_interop = cdata->d3d12_interop;
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
object_class->finalize = gst_qsv_vp9_enc_finalize;
|
|
|
|
object_class->set_property = gst_qsv_vp9_enc_set_property;
|
|
|
|
object_class->get_property = gst_qsv_vp9_enc_get_property;
|
|
|
|
|
2022-03-27 14:27:54 +00:00
|
|
|
g_object_class_install_property (object_class, PROP_QP_I,
|
2022-04-08 15:00:59 +00:00
|
|
|
g_param_spec_uint ("qp-i", "QP I",
|
|
|
|
"Constant quantizer for I frames (0: default)",
|
2022-03-27 14:27:54 +00:00
|
|
|
0, 255, DEFAULT_QP, (GParamFlags)
|
|
|
|
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
|
|
|
|
g_object_class_install_property (object_class, PROP_QP_P,
|
2022-04-08 15:00:59 +00:00
|
|
|
g_param_spec_uint ("qp-p", "QP P",
|
|
|
|
"Constant quantizer for P frames (0: default)",
|
2022-03-27 14:27:54 +00:00
|
|
|
0, 255, DEFAULT_QP, (GParamFlags)
|
|
|
|
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
|
2021-12-19 12:39:53 +00:00
|
|
|
g_object_class_install_property (object_class, PROP_GOP_SIZE,
|
|
|
|
g_param_spec_uint ("gop-size", "GOP Size",
|
|
|
|
"Number of pictures within a GOP (0: unspecified)",
|
|
|
|
0, G_MAXINT, DEFAULT_GOP_SIZE, (GParamFlags)
|
|
|
|
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
|
|
|
|
g_object_class_install_property (object_class, PROP_REF_FRAMES,
|
|
|
|
g_param_spec_uint ("ref-frames", "Reference Frames",
|
|
|
|
"Number of reference frames (0: unspecified)",
|
2022-03-27 14:27:54 +00:00
|
|
|
0, 3, DEFAULT_REF_FRAMES, (GParamFlags)
|
2021-12-19 12:39:53 +00:00
|
|
|
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
|
|
|
|
g_object_class_install_property (object_class, PROP_BITRATE,
|
|
|
|
g_param_spec_uint ("bitrate", "Bitrate",
|
|
|
|
"Target bitrate in kbit/sec, Ignored when selected rate-control mode "
|
2022-03-27 14:27:54 +00:00
|
|
|
"is constant QP variants (i.e., \"cqp\" and \"icq\")",
|
|
|
|
0, G_MAXUINT16, DEFAULT_BITRATE, (GParamFlags)
|
2021-12-19 12:39:53 +00:00
|
|
|
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
|
|
|
|
g_object_class_install_property (object_class, PROP_MAX_BITRATE,
|
|
|
|
g_param_spec_uint ("max-bitrate", "Max Bitrate",
|
|
|
|
"Maximum bitrate in kbit/sec, Ignored when selected rate-control mode "
|
2022-03-27 14:27:54 +00:00
|
|
|
"is constant QP variants (i.e., \"cqp\" and \"icq\")",
|
|
|
|
0, G_MAXUINT16, DEFAULT_MAX_BITRATE, (GParamFlags)
|
2021-12-19 12:39:53 +00:00
|
|
|
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
|
|
|
|
g_object_class_install_property (object_class, PROP_RATE_CONTROL,
|
|
|
|
g_param_spec_enum ("rate-control", "Rate Control",
|
|
|
|
"Rate Control Method", GST_TYPE_QSV_VP9_ENC_RATE_CONTROL,
|
|
|
|
DEFAULT_RATE_CONTROL,
|
|
|
|
(GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
|
2022-03-27 14:27:54 +00:00
|
|
|
g_object_class_install_property (object_class, PROP_ICQ_QUALITY,
|
|
|
|
g_param_spec_uint ("icq-quality", "ICQ Quality",
|
|
|
|
"Intelligent Constant Quality for \"icq\" rate-control (0: default)",
|
|
|
|
0, 255, DEFAULT_IQC_QUALITY, (GParamFlags)
|
|
|
|
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
parent_class = (GstElementClass *) g_type_class_peek_parent (klass);
|
2022-04-20 14:06:56 +00:00
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
std::string long_name = "Intel Quick Sync Video " +
|
|
|
|
std::string (cdata->description) + " VP9 Encoder";
|
|
|
|
|
|
|
|
gst_element_class_set_metadata (element_class, long_name.c_str (),
|
|
|
|
"Codec/Encoder/Video/Hardware",
|
|
|
|
"Intel Quick Sync Video VP9 Encoder",
|
|
|
|
"Seungha Yang <seungha@centricular.com>");
|
|
|
|
#else
|
2021-12-19 12:39:53 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class,
|
|
|
|
"Intel Quick Sync Video VP9 Encoder",
|
|
|
|
"Codec/Encoder/Video/Hardware",
|
|
|
|
"Intel Quick Sync Video VP9 Encoder",
|
|
|
|
"Seungha Yang <seungha@centricular.com>");
|
2022-04-20 14:06:56 +00:00
|
|
|
#endif
|
2021-12-19 12:39:53 +00:00
|
|
|
|
2022-10-21 10:08:49 +00:00
|
|
|
pad_templ = gst_pad_template_new ("sink",
|
|
|
|
GST_PAD_SINK, GST_PAD_ALWAYS, cdata->sink_caps);
|
|
|
|
doc_caps = gst_caps_from_string (DOC_SINK_CAPS);
|
|
|
|
gst_pad_template_set_documentation_caps (pad_templ, doc_caps);
|
|
|
|
gst_caps_unref (doc_caps);
|
|
|
|
gst_element_class_add_pad_template (element_class, pad_templ);
|
|
|
|
|
|
|
|
pad_templ = gst_pad_template_new ("src",
|
|
|
|
GST_PAD_SRC, GST_PAD_ALWAYS, cdata->src_caps);
|
|
|
|
doc_caps = gst_caps_from_string (DOC_SRC_CAPS);
|
|
|
|
gst_pad_template_set_documentation_caps (pad_templ, doc_caps);
|
|
|
|
gst_caps_unref (doc_caps);
|
|
|
|
gst_element_class_add_pad_template (element_class, pad_templ);
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
encoder_class->getcaps = GST_DEBUG_FUNCPTR (gst_qsv_vp9_enc_getcaps);
|
|
|
|
|
|
|
|
qsvenc_class->set_format = GST_DEBUG_FUNCPTR (gst_qsv_vp9_enc_set_format);
|
|
|
|
qsvenc_class->set_output_state =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_qsv_vp9_enc_set_output_state);
|
|
|
|
qsvenc_class->check_reconfigure =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_qsv_vp9_enc_check_reconfigure);
|
|
|
|
|
2022-10-21 10:08:49 +00:00
|
|
|
gst_type_mark_as_plugin_api (GST_TYPE_QSV_VP9_ENC_RATE_CONTROL,
|
|
|
|
(GstPluginAPIFlags) 0);
|
|
|
|
|
2021-12-19 12:39:53 +00:00
|
|
|
gst_caps_unref (cdata->sink_caps);
|
|
|
|
gst_caps_unref (cdata->src_caps);
|
2022-04-20 14:06:56 +00:00
|
|
|
g_free (cdata->description);
|
2021-12-19 12:39:53 +00:00
|
|
|
g_free (cdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_qsv_vp9_enc_init (GstQsvVP9Enc * self)
|
|
|
|
{
|
2022-03-27 14:27:54 +00:00
|
|
|
self->qp_i = DEFAULT_QP;
|
|
|
|
self->qp_p = DEFAULT_QP;
|
2021-12-19 12:39:53 +00:00
|
|
|
self->gop_size = DEFAULT_GOP_SIZE;
|
|
|
|
self->ref_frames = DEFAULT_REF_FRAMES;
|
|
|
|
self->bitrate = DEFAULT_BITRATE;
|
|
|
|
self->max_bitrate = DEFAULT_MAX_BITRATE;
|
|
|
|
self->rate_control = DEFAULT_RATE_CONTROL;
|
2022-03-27 14:27:54 +00:00
|
|
|
self->icq_quality = DEFAULT_IQC_QUALITY;
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
g_mutex_init (&self->prop_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_qsv_vp9_enc_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstQsvVP9Enc *self = GST_QSV_VP9_ENC (object);
|
|
|
|
|
|
|
|
g_mutex_clear (&self->prop_lock);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_qsv_vp9_enc_check_update_uint (GstQsvVP9Enc * self, guint * old_val,
|
|
|
|
guint new_val, gboolean is_bitrate_param)
|
|
|
|
{
|
|
|
|
if (*old_val == new_val)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*old_val = new_val;
|
|
|
|
if (is_bitrate_param)
|
|
|
|
self->bitrate_updated = TRUE;
|
|
|
|
else
|
|
|
|
self->property_updated = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_qsv_vp9_enc_check_update_enum (GstQsvVP9Enc * self, mfxU16 * old_val,
|
|
|
|
gint new_val)
|
|
|
|
{
|
|
|
|
if (*old_val == (mfxU16) new_val)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*old_val = (mfxU16) new_val;
|
|
|
|
self->property_updated = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_qsv_vp9_enc_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstQsvVP9Enc *self = GST_QSV_VP9_ENC (object);
|
|
|
|
|
2023-02-20 12:42:19 +00:00
|
|
|
g_mutex_lock (&self->prop_lock);
|
2021-12-19 12:39:53 +00:00
|
|
|
switch (prop_id) {
|
2022-03-27 14:27:54 +00:00
|
|
|
case PROP_QP_I:
|
|
|
|
gst_qsv_vp9_enc_check_update_uint (self, &self->qp_i,
|
|
|
|
g_value_get_uint (value), TRUE);
|
|
|
|
break;
|
|
|
|
case PROP_QP_P:
|
|
|
|
gst_qsv_vp9_enc_check_update_uint (self, &self->qp_p,
|
|
|
|
g_value_get_uint (value), TRUE);
|
|
|
|
break;
|
2021-12-19 12:39:53 +00:00
|
|
|
case PROP_GOP_SIZE:
|
|
|
|
gst_qsv_vp9_enc_check_update_uint (self, &self->gop_size,
|
|
|
|
g_value_get_uint (value), FALSE);
|
|
|
|
break;
|
|
|
|
case PROP_REF_FRAMES:
|
|
|
|
gst_qsv_vp9_enc_check_update_uint (self, &self->ref_frames,
|
|
|
|
g_value_get_uint (value), FALSE);
|
|
|
|
break;
|
|
|
|
case PROP_BITRATE:
|
|
|
|
gst_qsv_vp9_enc_check_update_uint (self, &self->bitrate,
|
|
|
|
g_value_get_uint (value), TRUE);
|
|
|
|
break;
|
|
|
|
case PROP_MAX_BITRATE:
|
|
|
|
gst_qsv_vp9_enc_check_update_uint (self, &self->max_bitrate,
|
|
|
|
g_value_get_uint (value), TRUE);
|
|
|
|
break;
|
|
|
|
case PROP_RATE_CONTROL:
|
|
|
|
gst_qsv_vp9_enc_check_update_enum (self, &self->rate_control,
|
|
|
|
g_value_get_enum (value));
|
|
|
|
break;
|
2022-03-27 14:27:54 +00:00
|
|
|
case PROP_ICQ_QUALITY:
|
|
|
|
gst_qsv_vp9_enc_check_update_uint (self, &self->icq_quality,
|
|
|
|
g_value_get_uint (value), FALSE);
|
|
|
|
break;
|
2021-12-19 12:39:53 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2023-02-20 12:42:19 +00:00
|
|
|
g_mutex_unlock (&self->prop_lock);
|
2021-12-19 12:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_qsv_vp9_enc_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstQsvVP9Enc *self = GST_QSV_VP9_ENC (object);
|
|
|
|
|
2023-02-20 12:42:19 +00:00
|
|
|
g_mutex_lock (&self->prop_lock);
|
2021-12-19 12:39:53 +00:00
|
|
|
switch (prop_id) {
|
2022-03-27 14:27:54 +00:00
|
|
|
case PROP_QP_I:
|
|
|
|
g_value_set_uint (value, self->qp_i);
|
|
|
|
break;
|
|
|
|
case PROP_QP_P:
|
|
|
|
g_value_set_uint (value, self->qp_p);
|
|
|
|
break;
|
2021-12-19 12:39:53 +00:00
|
|
|
case PROP_GOP_SIZE:
|
|
|
|
g_value_set_uint (value, self->gop_size);
|
|
|
|
break;
|
|
|
|
case PROP_REF_FRAMES:
|
|
|
|
g_value_set_uint (value, self->ref_frames);
|
|
|
|
break;
|
|
|
|
case PROP_BITRATE:
|
|
|
|
g_value_set_uint (value, self->bitrate);
|
|
|
|
break;
|
|
|
|
case PROP_MAX_BITRATE:
|
|
|
|
g_value_set_uint (value, self->max_bitrate);
|
|
|
|
break;
|
|
|
|
case PROP_RATE_CONTROL:
|
|
|
|
g_value_set_enum (value, self->rate_control);
|
|
|
|
break;
|
2022-03-27 14:27:54 +00:00
|
|
|
case PROP_ICQ_QUALITY:
|
|
|
|
g_value_set_uint (value, self->icq_quality);
|
|
|
|
break;
|
2021-12-19 12:39:53 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2023-02-20 12:42:19 +00:00
|
|
|
g_mutex_unlock (&self->prop_lock);
|
2021-12-19 12:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstCaps *
|
|
|
|
gst_qsv_vp9_enc_getcaps (GstVideoEncoder * encoder, GstCaps * filter)
|
|
|
|
{
|
|
|
|
GstQsvVP9Enc *self = GST_QSV_VP9_ENC (encoder);
|
|
|
|
GstCaps *allowed_caps;
|
|
|
|
GstCaps *template_caps;
|
|
|
|
GstCaps *supported_caps;
|
|
|
|
std::set < std::string > downstream_profiles;
|
|
|
|
|
|
|
|
allowed_caps = gst_pad_get_allowed_caps (encoder->srcpad);
|
|
|
|
|
|
|
|
/* Shouldn't be any or empty though, just return template caps in this case */
|
|
|
|
if (!allowed_caps || gst_caps_is_empty (allowed_caps) ||
|
|
|
|
gst_caps_is_any (allowed_caps)) {
|
|
|
|
gst_clear_caps (&allowed_caps);
|
|
|
|
|
|
|
|
return gst_video_encoder_proxy_getcaps (encoder, nullptr, filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if downstream specified profile explicitly, then filter out
|
|
|
|
* incompatible raw video format */
|
|
|
|
for (guint i = 0; i < gst_caps_get_size (allowed_caps); i++) {
|
|
|
|
const GValue *profile_value;
|
|
|
|
const gchar *profile;
|
|
|
|
GstStructure *s;
|
|
|
|
|
|
|
|
s = gst_caps_get_structure (allowed_caps, i);
|
|
|
|
profile_value = gst_structure_get_value (s, "profile");
|
|
|
|
if (!profile_value)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (GST_VALUE_HOLDS_LIST (profile_value)) {
|
|
|
|
for (guint j = 0; j < gst_value_list_get_size (profile_value); j++) {
|
|
|
|
const GValue *p = gst_value_list_get_value (profile_value, j);
|
|
|
|
|
|
|
|
if (!G_VALUE_HOLDS_STRING (p))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
profile = g_value_get_string (p);
|
|
|
|
if (profile)
|
|
|
|
downstream_profiles.insert (profile);
|
|
|
|
}
|
|
|
|
} else if (G_VALUE_HOLDS_STRING (profile_value)) {
|
|
|
|
profile = g_value_get_string (profile_value);
|
|
|
|
if (g_strcmp0 (profile, "0") == 0 || g_strcmp0 (profile, "1") == 0 ||
|
|
|
|
g_strcmp0 (profile, "2") == 0 || g_strcmp0 (profile, "3") == 0) {
|
|
|
|
downstream_profiles.insert (profile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Downstream specified %" G_GSIZE_FORMAT " profiles",
|
|
|
|
downstream_profiles.size ());
|
|
|
|
|
|
|
|
/* Caps returned by gst_pad_get_allowed_caps() should hold profile field
|
|
|
|
* already */
|
|
|
|
if (downstream_profiles.size () == 0) {
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"Allowed caps holds no profile field %" GST_PTR_FORMAT, allowed_caps);
|
|
|
|
|
2022-03-23 17:52:45 +00:00
|
|
|
gst_clear_caps (&allowed_caps);
|
|
|
|
|
2021-12-19 12:39:53 +00:00
|
|
|
return gst_video_encoder_proxy_getcaps (encoder, nullptr, filter);
|
|
|
|
}
|
|
|
|
|
2022-03-23 17:52:45 +00:00
|
|
|
gst_clear_caps (&allowed_caps);
|
|
|
|
|
2021-12-19 12:39:53 +00:00
|
|
|
template_caps = gst_pad_get_pad_template_caps (encoder->sinkpad);
|
|
|
|
template_caps = gst_caps_make_writable (template_caps);
|
|
|
|
|
|
|
|
if (downstream_profiles.size () == 1) {
|
|
|
|
std::string format;
|
|
|
|
const std::string & profile = *downstream_profiles.begin ();
|
|
|
|
|
|
|
|
if (profile == "0") {
|
|
|
|
format = "NV12";
|
|
|
|
} else if (profile == "1") {
|
|
|
|
format = "VUYA";
|
|
|
|
} else if (profile == "2") {
|
|
|
|
format = "P010_10LE";
|
|
|
|
} else if (profile == "3") {
|
|
|
|
format = "Y410";
|
|
|
|
} else {
|
|
|
|
gst_clear_caps (&template_caps);
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_caps_set_simple (template_caps, "format", G_TYPE_STRING,
|
|
|
|
format.c_str (), nullptr);
|
|
|
|
} else {
|
|
|
|
GValue formats = G_VALUE_INIT;
|
|
|
|
|
|
|
|
g_value_init (&formats, GST_TYPE_LIST);
|
|
|
|
|
|
|
|
/* *INDENT-OFF* */
|
|
|
|
for (const auto & iter : downstream_profiles) {
|
|
|
|
GValue val = G_VALUE_INIT;
|
|
|
|
g_value_init (&val, G_TYPE_STRING);
|
|
|
|
if (iter == "0") {
|
|
|
|
g_value_set_static_string (&val, "NV12");
|
|
|
|
} else if (iter == "1") {
|
|
|
|
g_value_set_static_string (&val, "VUYA");
|
|
|
|
} else if (iter == "2") {
|
|
|
|
g_value_set_static_string (&val, "P010_10LE");
|
|
|
|
} else if (iter == "3") {
|
|
|
|
g_value_set_static_string (&val, "Y410");
|
|
|
|
} else {
|
|
|
|
g_value_unset (&val);
|
|
|
|
gst_clear_caps (&template_caps);
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_value_list_append_and_take_value (&formats, &val);
|
|
|
|
}
|
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
|
|
|
gst_caps_set_value (template_caps, "format", &formats);
|
|
|
|
g_value_unset (&formats);
|
|
|
|
}
|
|
|
|
|
|
|
|
supported_caps = gst_video_encoder_proxy_getcaps (encoder,
|
|
|
|
template_caps, filter);
|
|
|
|
gst_caps_unref (template_caps);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Returning %" GST_PTR_FORMAT, supported_caps);
|
|
|
|
|
|
|
|
return supported_caps;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
mfxU16 profile;
|
|
|
|
const gchar *profile_str;
|
|
|
|
const gchar *raw_format;
|
|
|
|
} VP9Profile;
|
|
|
|
|
|
|
|
static const VP9Profile profile_map[] = {
|
|
|
|
/* preference order */
|
|
|
|
{MFX_PROFILE_VP9_0, "0", "NV12"},
|
|
|
|
{MFX_PROFILE_VP9_2, "2", "P010_10LE"},
|
|
|
|
{MFX_PROFILE_VP9_1, "1", "VUYA"},
|
|
|
|
{MFX_PROFILE_VP9_3, "3", "Y410"},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const gchar *
|
|
|
|
gst_qsv_vp9_profile_to_string (mfxU16 profile)
|
|
|
|
{
|
|
|
|
for (guint i = 0; i < G_N_ELEMENTS (profile_map); i++) {
|
|
|
|
if (profile_map[i].profile == profile)
|
|
|
|
return profile_map[i].profile_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_qsv_vp9_enc_init_vp9_param (mfxExtVP9Param * param)
|
|
|
|
{
|
|
|
|
memset (param, 0, sizeof (mfxExtVP9Param));
|
|
|
|
|
|
|
|
param->Header.BufferId = MFX_EXTBUFF_VP9_PARAM;
|
|
|
|
param->Header.BufferSz = sizeof (mfxExtVP9Param);
|
|
|
|
}
|
|
|
|
|
2022-03-27 14:27:54 +00:00
|
|
|
static void
|
|
|
|
gst_qsv_vp9_enc_set_bitrate (GstQsvVP9Enc * self, mfxVideoParam * param)
|
|
|
|
{
|
|
|
|
switch (param->mfx.RateControlMethod) {
|
|
|
|
case MFX_RATECONTROL_CBR:
|
|
|
|
param->mfx.TargetKbps = param->mfx.MaxKbps = self->bitrate;
|
|
|
|
param->mfx.BRCParamMultiplier = 1;
|
|
|
|
break;
|
|
|
|
case MFX_RATECONTROL_VBR:
|
|
|
|
param->mfx.TargetKbps = self->bitrate;
|
|
|
|
param->mfx.MaxKbps = self->max_bitrate;
|
|
|
|
param->mfx.BRCParamMultiplier = 1;
|
|
|
|
break;
|
|
|
|
case MFX_RATECONTROL_CQP:
|
|
|
|
param->mfx.QPI = self->qp_i;
|
|
|
|
param->mfx.QPP = self->qp_p;
|
|
|
|
break;
|
|
|
|
case MFX_RATECONTROL_ICQ:
|
|
|
|
param->mfx.ICQQuality = self->icq_quality;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GST_WARNING_OBJECT (self,
|
|
|
|
"Unhandled rate-control method %d", self->rate_control);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-19 12:39:53 +00:00
|
|
|
static gboolean
|
|
|
|
gst_qsv_vp9_enc_set_format (GstQsvEncoder * encoder,
|
|
|
|
GstVideoCodecState * state, mfxVideoParam * param, GPtrArray * extra_params)
|
|
|
|
{
|
|
|
|
GstQsvVP9Enc *self = GST_QSV_VP9_ENC (encoder);
|
|
|
|
mfxU16 mfx_profile = MFX_PROFILE_UNKNOWN;
|
|
|
|
GstVideoInfo *info = &state->info;
|
|
|
|
mfxExtVP9Param *vp9_param;
|
|
|
|
mfxFrameInfo *frame_info;
|
2023-11-21 11:56:36 +00:00
|
|
|
GstVideoFormat format;
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
frame_info = ¶m->mfx.FrameInfo;
|
|
|
|
|
|
|
|
/* QSV expects this resolution, but actual coded frame resolution will be
|
|
|
|
* signalled via mfxExtVP9Param */
|
|
|
|
frame_info->Width = frame_info->CropW = GST_ROUND_UP_16 (info->width);
|
|
|
|
frame_info->Height = frame_info->CropH = GST_ROUND_UP_16 (info->height);
|
|
|
|
|
|
|
|
frame_info->PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
|
|
|
|
|
|
|
|
if (GST_VIDEO_INFO_FPS_N (info) > 0 && GST_VIDEO_INFO_FPS_D (info) > 0) {
|
|
|
|
frame_info->FrameRateExtN = GST_VIDEO_INFO_FPS_N (info);
|
|
|
|
frame_info->FrameRateExtD = GST_VIDEO_INFO_FPS_D (info);
|
|
|
|
} else {
|
|
|
|
/* HACK: Same as x264enc */
|
|
|
|
frame_info->FrameRateExtN = 25;
|
|
|
|
frame_info->FrameRateExtD = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame_info->AspectRatioW = GST_VIDEO_INFO_PAR_N (info);
|
|
|
|
frame_info->AspectRatioH = GST_VIDEO_INFO_PAR_D (info);
|
|
|
|
|
2023-11-21 11:56:36 +00:00
|
|
|
format = GST_VIDEO_INFO_FORMAT (info);
|
|
|
|
switch (format) {
|
2021-12-19 12:39:53 +00:00
|
|
|
case GST_VIDEO_FORMAT_NV12:
|
|
|
|
mfx_profile = MFX_PROFILE_VP9_0;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_VUYA:
|
|
|
|
mfx_profile = MFX_PROFILE_VP9_1;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_P010_10LE:
|
|
|
|
mfx_profile = MFX_PROFILE_VP9_2;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_Y410:
|
|
|
|
mfx_profile = MFX_PROFILE_VP9_3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GST_ERROR_OBJECT (self, "Unexpected format %s",
|
2023-11-21 11:56:36 +00:00
|
|
|
gst_video_format_to_string (format));
|
2021-12-19 12:39:53 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-11-21 11:56:36 +00:00
|
|
|
gst_qsv_frame_info_set_format (frame_info, format);
|
|
|
|
|
2021-12-19 12:39:53 +00:00
|
|
|
gst_qsv_vp9_enc_init_vp9_param (&self->vp9_param);
|
|
|
|
vp9_param = &self->vp9_param;
|
|
|
|
|
|
|
|
vp9_param->FrameWidth = GST_VIDEO_INFO_WIDTH (info);
|
|
|
|
vp9_param->FrameHeight = GST_VIDEO_INFO_HEIGHT (info);
|
|
|
|
|
|
|
|
/* We will always output raw VP9 frames */
|
|
|
|
vp9_param->WriteIVFHeaders = MFX_CODINGOPTION_OFF;
|
|
|
|
|
|
|
|
g_mutex_lock (&self->prop_lock);
|
|
|
|
param->mfx.CodecId = MFX_CODEC_VP9;
|
|
|
|
param->mfx.CodecProfile = mfx_profile;
|
|
|
|
param->mfx.GopRefDist = 1;
|
|
|
|
param->mfx.GopPicSize = self->gop_size;
|
|
|
|
param->mfx.RateControlMethod = self->rate_control;
|
|
|
|
param->mfx.NumRefFrame = self->ref_frames;
|
|
|
|
|
2022-03-27 14:27:54 +00:00
|
|
|
gst_qsv_vp9_enc_set_bitrate (self, param);
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
g_ptr_array_add (extra_params, vp9_param);
|
|
|
|
|
|
|
|
param->ExtParam = (mfxExtBuffer **) extra_params->pdata;
|
|
|
|
param->NumExtParam = extra_params->len;
|
|
|
|
|
|
|
|
self->bitrate_updated = FALSE;
|
|
|
|
self->property_updated = FALSE;
|
|
|
|
|
|
|
|
g_mutex_unlock (&self->prop_lock);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_qsv_vp9_enc_set_output_state (GstQsvEncoder * encoder,
|
|
|
|
GstVideoCodecState * state, mfxSession session)
|
|
|
|
{
|
|
|
|
GstQsvVP9Enc *self = GST_QSV_VP9_ENC (encoder);
|
|
|
|
GstCaps *caps;
|
|
|
|
GstTagList *tags;
|
|
|
|
GstVideoCodecState *out_state;
|
|
|
|
guint bitrate, max_bitrate;
|
|
|
|
mfxVideoParam param;
|
|
|
|
const gchar *profile_str;
|
|
|
|
mfxStatus status;
|
|
|
|
|
|
|
|
memset (¶m, 0, sizeof (mfxVideoParam));
|
|
|
|
status = MFXVideoENCODE_GetVideoParam (session, ¶m);
|
|
|
|
if (status < MFX_ERR_NONE) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to get video param %d (%s)",
|
|
|
|
QSV_STATUS_ARGS (status));
|
|
|
|
return FALSE;
|
|
|
|
} else if (status != MFX_ERR_NONE) {
|
|
|
|
GST_WARNING_OBJECT (self, "GetVideoParam returned warning %d (%s)",
|
|
|
|
QSV_STATUS_ARGS (status));
|
|
|
|
}
|
|
|
|
|
|
|
|
caps = gst_caps_from_string ("video/x-vp9");
|
|
|
|
profile_str = gst_qsv_vp9_profile_to_string (param.mfx.CodecProfile);
|
|
|
|
if (profile_str)
|
|
|
|
gst_caps_set_simple (caps, "profile", G_TYPE_STRING, profile_str, nullptr);
|
|
|
|
|
|
|
|
out_state = gst_video_encoder_set_output_state (GST_VIDEO_ENCODER (encoder),
|
|
|
|
caps, state);
|
|
|
|
gst_video_codec_state_unref (out_state);
|
|
|
|
|
|
|
|
tags = gst_tag_list_new_empty ();
|
|
|
|
gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_ENCODER, "qsvvp9enc",
|
|
|
|
nullptr);
|
|
|
|
|
2022-03-27 14:27:54 +00:00
|
|
|
switch (param.mfx.RateControlMethod) {
|
|
|
|
case MFX_RATECONTROL_CQP:
|
|
|
|
case MFX_RATECONTROL_ICQ:
|
|
|
|
/* We don't know target/max bitrate in this case */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
max_bitrate = (guint) param.mfx.MaxKbps;
|
|
|
|
bitrate = (guint) param.mfx.TargetKbps;
|
|
|
|
if (bitrate > 0) {
|
|
|
|
gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
|
|
|
|
GST_TAG_NOMINAL_BITRATE, bitrate * 1000, nullptr);
|
|
|
|
}
|
2021-12-19 12:39:53 +00:00
|
|
|
|
2022-03-27 14:27:54 +00:00
|
|
|
if (max_bitrate > 0) {
|
|
|
|
gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
|
|
|
|
GST_TAG_MAXIMUM_BITRATE, max_bitrate * 1000, nullptr);
|
|
|
|
}
|
|
|
|
break;
|
2021-12-19 12:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_video_encoder_merge_tags (GST_VIDEO_ENCODER (encoder),
|
|
|
|
tags, GST_TAG_MERGE_REPLACE);
|
|
|
|
gst_tag_list_unref (tags);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstQsvEncoderReconfigure
|
2022-03-27 14:27:54 +00:00
|
|
|
gst_qsv_vp9_enc_check_reconfigure (GstQsvEncoder * encoder, mfxSession session,
|
|
|
|
mfxVideoParam * param, GPtrArray * extra_params)
|
2021-12-19 12:39:53 +00:00
|
|
|
{
|
|
|
|
GstQsvVP9Enc *self = GST_QSV_VP9_ENC (encoder);
|
2022-03-27 14:27:54 +00:00
|
|
|
GstQsvEncoderReconfigure ret = GST_QSV_ENCODER_RECONFIGURE_NONE;
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
g_mutex_lock (&self->prop_lock);
|
|
|
|
if (self->property_updated) {
|
2022-03-27 14:27:54 +00:00
|
|
|
ret = GST_QSV_ENCODER_RECONFIGURE_FULL;
|
|
|
|
goto done;
|
2021-12-19 12:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (self->bitrate_updated) {
|
2022-03-27 14:27:54 +00:00
|
|
|
/* VP9 does not support query with MFX_EXTBUFF_ENCODER_RESET_OPTION
|
|
|
|
* Just return GST_QSV_ENCODER_RECONFIGURE_BITRATE here.
|
|
|
|
* Baseclass will care error */
|
|
|
|
gst_qsv_vp9_enc_set_bitrate (self, param);
|
2021-12-19 12:39:53 +00:00
|
|
|
|
2022-03-27 14:27:54 +00:00
|
|
|
ret = GST_QSV_ENCODER_RECONFIGURE_BITRATE;
|
2021-12-19 12:39:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 14:27:54 +00:00
|
|
|
done:
|
|
|
|
self->property_updated = FALSE;
|
|
|
|
self->bitrate_updated = FALSE;
|
2021-12-19 12:39:53 +00:00
|
|
|
g_mutex_unlock (&self->prop_lock);
|
|
|
|
|
2022-03-27 14:27:54 +00:00
|
|
|
return ret;
|
2021-12-19 12:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_qsv_vp9_enc_register (GstPlugin * plugin, guint rank, guint impl_index,
|
2024-04-01 16:36:28 +00:00
|
|
|
GstObject * device, mfxSession session, gboolean d3d12_interop)
|
2021-12-19 12:39:53 +00:00
|
|
|
{
|
|
|
|
mfxVideoParam param;
|
|
|
|
mfxInfoMFX *mfx;
|
|
|
|
std::vector < mfxU16 > supported_profiles;
|
|
|
|
std::vector < std::string > supported_formats;
|
2022-11-01 13:19:18 +00:00
|
|
|
GstQsvResolution max_resolution;
|
2021-12-19 12:39:53 +00:00
|
|
|
mfxExtVP9Param vp9_param;
|
|
|
|
mfxExtBuffer *ext_bufs[1];
|
|
|
|
|
2022-04-16 15:00:55 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_qsv_vp9_enc_debug, "qsvvp9enc", 0, "qsvvp9enc");
|
|
|
|
|
2021-12-19 12:39:53 +00:00
|
|
|
memset (¶m, 0, sizeof (mfxVideoParam));
|
2022-11-01 13:19:18 +00:00
|
|
|
memset (&max_resolution, 0, sizeof (GstQsvResolution));
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
ext_bufs[0] = (mfxExtBuffer *) & vp9_param;
|
|
|
|
|
|
|
|
param.AsyncDepth = 4;
|
|
|
|
param.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
|
|
|
|
|
|
|
|
mfx = ¶m.mfx;
|
|
|
|
mfx->LowPower = MFX_CODINGOPTION_UNKNOWN;
|
|
|
|
mfx->CodecId = MFX_CODEC_VP9;
|
|
|
|
|
|
|
|
mfx->FrameInfo.Width = mfx->FrameInfo.CropW = GST_ROUND_UP_16 (320);
|
|
|
|
mfx->FrameInfo.Height = mfx->FrameInfo.CropH = GST_ROUND_UP_16 (240);
|
|
|
|
mfx->FrameInfo.FrameRateExtN = 30;
|
|
|
|
mfx->FrameInfo.FrameRateExtD = 1;
|
|
|
|
mfx->FrameInfo.AspectRatioW = 1;
|
|
|
|
mfx->FrameInfo.AspectRatioH = 1;
|
|
|
|
mfx->FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
|
|
|
|
|
|
|
|
param.NumExtParam = 1;
|
|
|
|
param.ExtParam = ext_bufs;
|
|
|
|
|
|
|
|
for (guint i = 0; i < G_N_ELEMENTS (profile_map); i++) {
|
2023-11-21 11:56:36 +00:00
|
|
|
GstVideoFormat format;
|
|
|
|
|
2021-12-19 12:39:53 +00:00
|
|
|
mfx->CodecProfile = profile_map[i].profile;
|
|
|
|
|
|
|
|
gst_qsv_vp9_enc_init_vp9_param (&vp9_param);
|
|
|
|
vp9_param.FrameWidth = 320;
|
|
|
|
vp9_param.FrameHeight = 240;
|
|
|
|
|
|
|
|
vp9_param.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
|
|
|
|
|
|
|
|
switch (mfx->CodecProfile) {
|
|
|
|
case MFX_PROFILE_VP9_0:
|
2023-11-21 11:56:36 +00:00
|
|
|
format = GST_VIDEO_FORMAT_NV12;
|
2021-12-19 12:39:53 +00:00
|
|
|
break;
|
|
|
|
case MFX_PROFILE_VP9_1:
|
2023-11-21 11:56:36 +00:00
|
|
|
format = GST_VIDEO_FORMAT_VUYA;
|
2021-12-19 12:39:53 +00:00
|
|
|
break;
|
|
|
|
case MFX_PROFILE_VP9_2:
|
2023-11-21 11:56:36 +00:00
|
|
|
format = GST_VIDEO_FORMAT_P010_10LE;
|
2021-12-19 12:39:53 +00:00
|
|
|
break;
|
|
|
|
case MFX_PROFILE_VP9_3:
|
2023-11-21 11:56:36 +00:00
|
|
|
format = GST_VIDEO_FORMAT_Y410;
|
2021-12-19 12:39:53 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-21 11:56:36 +00:00
|
|
|
gst_qsv_frame_info_set_format (&mfx->FrameInfo, format);
|
|
|
|
|
2021-12-19 12:39:53 +00:00
|
|
|
if (MFXVideoENCODE_Query (session, ¶m, ¶m) != MFX_ERR_NONE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
supported_profiles.push_back (profile_map[i].profile);
|
|
|
|
supported_formats.push_back (profile_map[i].raw_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (supported_profiles.empty ()) {
|
|
|
|
GST_INFO ("Device doesn't support VP9 encoding");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mfx->CodecProfile = MFX_PROFILE_VP9_0;
|
2023-11-21 11:56:36 +00:00
|
|
|
gst_qsv_frame_info_set_format (&mfx->FrameInfo, GST_VIDEO_FORMAT_NV12);
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
/* Check max-resolution */
|
2022-11-01 13:19:18 +00:00
|
|
|
for (guint i = 0; i < G_N_ELEMENTS (gst_qsv_resolutions); i++) {
|
2021-12-19 12:39:53 +00:00
|
|
|
mfx->FrameInfo.Width = mfx->FrameInfo.CropW =
|
2022-11-01 13:19:18 +00:00
|
|
|
GST_ROUND_UP_16 (gst_qsv_resolutions[i].width);
|
2021-12-19 12:39:53 +00:00
|
|
|
mfx->FrameInfo.Height = mfx->FrameInfo.CropH =
|
2022-11-01 13:19:18 +00:00
|
|
|
GST_ROUND_UP_16 (gst_qsv_resolutions[i].height);
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
gst_qsv_vp9_enc_init_vp9_param (&vp9_param);
|
|
|
|
|
2022-11-01 13:19:18 +00:00
|
|
|
vp9_param.FrameWidth = gst_qsv_resolutions[i].width;
|
|
|
|
vp9_param.FrameHeight = gst_qsv_resolutions[i].height;
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
vp9_param.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
|
|
|
|
|
|
|
|
if (MFXVideoENCODE_Query (session, ¶m, ¶m) != MFX_ERR_NONE)
|
|
|
|
break;
|
|
|
|
|
2022-11-01 13:19:18 +00:00
|
|
|
max_resolution.width = gst_qsv_resolutions[i].width;
|
|
|
|
max_resolution.height = gst_qsv_resolutions[i].height;
|
2021-12-19 12:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_INFO ("Maximum supported resolution: %dx%d",
|
|
|
|
max_resolution.width, max_resolution.height);
|
|
|
|
|
|
|
|
/* TODO: check supported rate-control methods and expose only supported
|
|
|
|
* methods, since the device might not be able to support some of them */
|
|
|
|
|
|
|
|
/* To cover both landscape and portrait,
|
|
|
|
* select max value (width in this case) */
|
|
|
|
guint resolution = MAX (max_resolution.width, max_resolution.height);
|
|
|
|
std::string sink_caps_str = "video/x-raw";
|
|
|
|
|
|
|
|
sink_caps_str += ", width=(int) [ 16, " + std::to_string (resolution) + " ]";
|
|
|
|
sink_caps_str += ", height=(int) [ 16, " + std::to_string (resolution) + " ]";
|
|
|
|
|
|
|
|
/* *INDENT-OFF* */
|
|
|
|
if (supported_formats.size () > 1) {
|
|
|
|
sink_caps_str += ", format=(string) { ";
|
|
|
|
bool first = true;
|
|
|
|
for (const auto &iter: supported_formats) {
|
|
|
|
if (!first) {
|
|
|
|
sink_caps_str += ", ";
|
|
|
|
}
|
|
|
|
|
|
|
|
sink_caps_str += iter;
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
sink_caps_str += " }";
|
|
|
|
} else {
|
|
|
|
sink_caps_str += ", format=(string) " + supported_formats[0];
|
|
|
|
}
|
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
|
|
|
GstCaps *sink_caps = gst_caps_from_string (sink_caps_str.c_str ());
|
|
|
|
|
|
|
|
/* TODO: Add support for VA */
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
GstCaps *d3d11_caps = gst_caps_copy (sink_caps);
|
|
|
|
GstCapsFeatures *caps_features =
|
|
|
|
gst_caps_features_new (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY, nullptr);
|
|
|
|
gst_caps_set_features_simple (d3d11_caps, caps_features);
|
2024-04-01 16:36:28 +00:00
|
|
|
#ifdef HAVE_GST_D3D12
|
|
|
|
auto d3d12_caps = gst_caps_copy (sink_caps);
|
|
|
|
auto d3d12_feature =
|
|
|
|
gst_caps_features_new (GST_CAPS_FEATURE_MEMORY_D3D12_MEMORY, nullptr);
|
|
|
|
gst_caps_set_features_simple (d3d12_caps, d3d12_feature);
|
|
|
|
gst_caps_append (d3d11_caps, d3d12_caps);
|
|
|
|
#endif
|
2021-12-19 12:39:53 +00:00
|
|
|
gst_caps_append (d3d11_caps, sink_caps);
|
|
|
|
sink_caps = d3d11_caps;
|
2022-03-25 13:04:05 +00:00
|
|
|
#else
|
|
|
|
GstCaps *va_caps = gst_caps_copy (sink_caps);
|
|
|
|
GstCapsFeatures *caps_features =
|
|
|
|
gst_caps_features_new (GST_CAPS_FEATURE_MEMORY_VA, nullptr);
|
|
|
|
gst_caps_set_features_simple (va_caps, caps_features);
|
|
|
|
gst_caps_append (va_caps, sink_caps);
|
|
|
|
sink_caps = va_caps;
|
2021-12-19 12:39:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string src_caps_str = "video/x-vp9";
|
|
|
|
src_caps_str += ", width=(int) [ 16, " + std::to_string (resolution) + " ]";
|
|
|
|
src_caps_str += ", height=(int) [ 16, " + std::to_string (resolution) + " ]";
|
|
|
|
|
|
|
|
/* *INDENT-OFF* */
|
|
|
|
if (supported_profiles.size () > 1) {
|
|
|
|
src_caps_str += ", profile=(string) { ";
|
|
|
|
bool first = true;
|
|
|
|
for (const auto &iter: supported_profiles) {
|
|
|
|
if (!first) {
|
|
|
|
src_caps_str += ", ";
|
|
|
|
}
|
|
|
|
|
|
|
|
src_caps_str += gst_qsv_vp9_profile_to_string (iter);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
src_caps_str += " }";
|
|
|
|
} else {
|
|
|
|
src_caps_str += ", profile=(string) ";
|
|
|
|
src_caps_str += gst_qsv_vp9_profile_to_string (supported_profiles[0]);
|
|
|
|
}
|
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
|
|
|
GstCaps *src_caps = gst_caps_from_string (src_caps_str.c_str ());
|
|
|
|
|
|
|
|
GST_MINI_OBJECT_FLAG_SET (sink_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
|
|
|
|
GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
|
|
|
|
|
|
|
|
GstQsvVP9EncClassData *cdata = g_new0 (GstQsvVP9EncClassData, 1);
|
|
|
|
cdata->sink_caps = sink_caps;
|
|
|
|
cdata->src_caps = src_caps;
|
|
|
|
cdata->impl_index = impl_index;
|
2024-04-01 16:36:28 +00:00
|
|
|
cdata->d3d12_interop = d3d12_interop;
|
2021-12-19 12:39:53 +00:00
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
2022-04-20 14:06:56 +00:00
|
|
|
g_object_get (device, "adapter-luid", &cdata->adapter_luid,
|
|
|
|
"description", &cdata->description, nullptr);
|
2021-12-19 12:39:53 +00:00
|
|
|
#else
|
2022-04-20 14:06:56 +00:00
|
|
|
g_object_get (device, "path", &cdata->display_path, nullptr);
|
2021-12-19 12:39:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
GType type;
|
|
|
|
gchar *type_name;
|
|
|
|
gchar *feature_name;
|
|
|
|
GTypeInfo type_info = {
|
|
|
|
sizeof (GstQsvVP9EncClass),
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
(GClassInitFunc) gst_qsv_vp9_enc_class_init,
|
|
|
|
nullptr,
|
|
|
|
cdata,
|
|
|
|
sizeof (GstQsvVP9Enc),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gst_qsv_vp9_enc_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
type_name = g_strdup ("GstQsvVP9Enc");
|
|
|
|
feature_name = g_strdup ("qsvvp9enc");
|
|
|
|
|
|
|
|
gint index = 0;
|
|
|
|
while (g_type_from_name (type_name)) {
|
|
|
|
index++;
|
|
|
|
g_free (type_name);
|
|
|
|
g_free (feature_name);
|
|
|
|
type_name = g_strdup_printf ("GstQsvVP9Device%dEnc", index);
|
|
|
|
feature_name = g_strdup_printf ("qsvvp9device%denc", index);
|
|
|
|
}
|
|
|
|
|
|
|
|
type = g_type_register_static (GST_TYPE_QSV_ENCODER, type_name, &type_info,
|
|
|
|
(GTypeFlags) 0);
|
|
|
|
|
|
|
|
if (rank > 0 && index != 0)
|
|
|
|
rank--;
|
|
|
|
|
2022-10-21 10:08:49 +00:00
|
|
|
if (index != 0)
|
|
|
|
gst_element_type_set_skip_documentation (type);
|
|
|
|
|
2021-12-19 12:39:53 +00:00
|
|
|
if (!gst_element_register (plugin, feature_name, rank, type))
|
|
|
|
GST_WARNING ("Failed to register plugin '%s'", type_name);
|
|
|
|
|
|
|
|
g_free (type_name);
|
|
|
|
g_free (feature_name);
|
|
|
|
}
|