2 * GStreamer Intel MSDK plugin
3 * Copyright (c) 2019 Intel Corporation. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * SECTION: element-msdkvp9enc
35 * @short_description: Intel MSDK VP9 encoder
37 * VP9 video encoder based on Intel MFX
39 * ## Example launch line
41 * gst-launch-1.0 videotestsrc num-buffers=90 ! msdkvp9enc ! matroskamux ! filesink location=output.webm
52 #include <gst/allocators/gstdmabuf.h>
54 #include "gstmsdkvp9enc.h"
56 GST_DEBUG_CATEGORY_EXTERN (gst_msdkvp9enc_debug);
57 #define GST_CAT_DEFAULT gst_msdkvp9enc_debug
59 #define RAW_FORMATS "NV12, I420, YV12, YUY2, UYVY, BGRA, P010_10LE, VUYA"
60 #define PROFILES "0, 1, 2"
62 #if (MFX_VERSION >= 1027)
63 #define COMMON_FORMAT "{ " RAW_FORMATS ", Y410 }"
64 #define SRC_PROFILES "{ " PROFILES ", 3 }"
66 #define COMMON_FORMAT "{ " RAW_FORMATS " }"
67 #define SRC_PROFILES "{ " PROFILES " }"
71 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
74 GST_STATIC_CAPS (GST_MSDK_CAPS_STR (COMMON_FORMAT,
75 "{ NV12, P010_10LE }") "; "
76 GST_MSDK_CAPS_MAKE_WITH_D3D11_FEATURE ("{ NV12, P010_10LE }")));
78 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
81 GST_STATIC_CAPS (GST_MSDK_CAPS_STR (COMMON_FORMAT,
82 "{ NV12, P010_10LE }") "; "
83 GST_MSDK_CAPS_MAKE_WITH_VA_FEATURE ("NV12")));
86 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
89 GST_STATIC_CAPS ("video/x-vp9, "
90 "framerate = (fraction) [0/1, MAX], "
91 "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
92 "profile = (string) " SRC_PROFILES)
95 #define gst_msdkvp9enc_parent_class parent_class
96 G_DEFINE_TYPE (GstMsdkVP9Enc, gst_msdkvp9enc, GST_TYPE_MSDKENC);
99 gst_msdkvp9enc_set_format (GstMsdkEnc * encoder)
101 GstMsdkVP9Enc *thiz = GST_MSDKVP9ENC (encoder);
102 GstCaps *template_caps;
103 GstCaps *allowed_caps = NULL;
105 thiz->profile = MFX_PROFILE_VP9_0;
106 template_caps = gst_static_pad_template_get_caps (&src_factory);
107 allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
109 /* If downstream has ANY caps let encoder decide profile and level */
110 if (allowed_caps == template_caps) {
111 GST_INFO_OBJECT (thiz,
112 "downstream has ANY caps, profile/level set to auto");
113 } else if (allowed_caps) {
115 const gchar *profile;
117 if (gst_caps_is_empty (allowed_caps)) {
118 gst_caps_unref (allowed_caps);
119 gst_caps_unref (template_caps);
123 allowed_caps = gst_caps_make_writable (allowed_caps);
124 allowed_caps = gst_caps_fixate (allowed_caps);
125 s = gst_caps_get_structure (allowed_caps, 0);
126 profile = gst_structure_get_string (s, "profile");
129 if (!strcmp (profile, "3")) {
130 thiz->profile = MFX_PROFILE_VP9_3;
131 } else if (!strcmp (profile, "2")) {
132 thiz->profile = MFX_PROFILE_VP9_2;
133 } else if (!strcmp (profile, "1")) {
134 thiz->profile = MFX_PROFILE_VP9_1;
135 } else if (!strcmp (profile, "0")) {
136 thiz->profile = MFX_PROFILE_VP9_0;
138 g_assert_not_reached ();
142 gst_caps_unref (allowed_caps);
145 gst_caps_unref (template_caps);
151 gst_msdkvp9enc_configure (GstMsdkEnc * encoder)
153 GstMsdkVP9Enc *vp9enc = GST_MSDKVP9ENC (encoder);
156 if (encoder->hardware) {
157 session = gst_msdk_context_get_session (encoder->context);
159 if (!gst_msdk_load_plugin (session, &MFX_PLUGINID_VP9E_HW, 1, "msdkvp9enc"))
163 encoder->num_extra_frames = encoder->async_depth - 1;
164 encoder->param.mfx.CodecId = MFX_CODEC_VP9;
165 encoder->param.mfx.CodecLevel = 0;
167 switch (encoder->param.mfx.FrameInfo.FourCC) {
168 #if (MFX_VERSION >= 1027)
169 case MFX_FOURCC_Y410:
170 encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_3;
174 case MFX_FOURCC_P010:
175 encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_2;
178 case MFX_FOURCC_AYUV:
179 encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_1;
183 encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_0;
187 /* As the frame width and height is rounded up to 128 and 32 since commit 8daac1c,
188 * so the width, height for initialization should be rounded up to 128 and 32
189 * too because VP9 encoder in MSDK will do some check on width and height.
191 encoder->param.mfx.FrameInfo.Width =
192 GST_ROUND_UP_128 (encoder->param.mfx.FrameInfo.CropW);
193 encoder->param.mfx.FrameInfo.Height =
194 GST_ROUND_UP_32 (encoder->param.mfx.FrameInfo.CropH);
196 /* Always turn on this flag for VP9 */
197 encoder->param.mfx.LowPower = MFX_CODINGOPTION_ON;
199 /* Enable Extended coding options */
200 gst_msdkenc_ensure_extended_coding_options (encoder);
202 memset (&vp9enc->ext_vp9, 0, sizeof (vp9enc->ext_vp9));
203 vp9enc->ext_vp9.Header.BufferId = MFX_EXTBUFF_VP9_PARAM;
204 vp9enc->ext_vp9.Header.BufferSz = sizeof (vp9enc->ext_vp9);
205 vp9enc->ext_vp9.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
207 gst_msdkenc_add_extra_param (encoder, (mfxExtBuffer *) & vp9enc->ext_vp9);
212 static inline const gchar *
213 profile_to_string (gint profile)
216 case MFX_PROFILE_VP9_3:
218 case MFX_PROFILE_VP9_2:
220 case MFX_PROFILE_VP9_1:
222 case MFX_PROFILE_VP9_0:
232 gst_msdkvp9enc_set_src_caps (GstMsdkEnc * encoder)
235 GstStructure *structure;
236 const gchar *profile;
238 caps = gst_caps_new_empty_simple ("video/x-vp9");
239 structure = gst_caps_get_structure (caps, 0);
241 profile = profile_to_string (encoder->param.mfx.CodecProfile);
243 gst_structure_set (structure, "profile", G_TYPE_STRING, profile, NULL);
249 gst_msdkvp9enc_set_property (GObject * object, guint prop_id,
250 const GValue * value, GParamSpec * pspec)
252 GstMsdkVP9Enc *thiz = GST_MSDKVP9ENC (object);
254 if (!gst_msdkenc_set_common_property (object, prop_id, value, pspec))
255 GST_WARNING_OBJECT (thiz, "Failed to set common encode property");
259 gst_msdkvp9enc_get_property (GObject * object, guint prop_id, GValue * value,
262 GstMsdkVP9Enc *thiz = GST_MSDKVP9ENC (object);
264 if (!gst_msdkenc_get_common_property (object, prop_id, value, pspec))
265 GST_WARNING_OBJECT (thiz, "Failed to get common encode property");
269 gst_msdkvp9enc_class_init (GstMsdkVP9EncClass * klass)
271 GObjectClass *gobject_class;
272 GstElementClass *element_class;
273 GstMsdkEncClass *encoder_class;
275 gobject_class = G_OBJECT_CLASS (klass);
276 element_class = GST_ELEMENT_CLASS (klass);
277 encoder_class = GST_MSDKENC_CLASS (klass);
279 gobject_class->set_property = gst_msdkvp9enc_set_property;
280 gobject_class->get_property = gst_msdkvp9enc_get_property;
282 encoder_class->set_format = gst_msdkvp9enc_set_format;
283 encoder_class->configure = gst_msdkvp9enc_configure;
284 encoder_class->set_src_caps = gst_msdkvp9enc_set_src_caps;
285 encoder_class->qp_max = 255;
286 encoder_class->qp_min = 0;
288 gst_msdkenc_install_common_properties (encoder_class);
290 gst_element_class_set_static_metadata (element_class,
291 "Intel MSDK VP9 encoder",
292 "Codec/Encoder/Video/Hardware",
293 "VP9 video encoder based on " MFX_API_SDK,
294 "Haihao Xiang <haihao.xiang@intel.com>");
296 gst_element_class_add_static_pad_template (element_class, &sink_factory);
297 gst_element_class_add_static_pad_template (element_class, &src_factory);
301 gst_msdkvp9enc_init (GstMsdkVP9Enc * thiz)