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 " }"
70 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
73 GST_STATIC_CAPS (GST_MSDK_CAPS_STR (COMMON_FORMAT,
74 "{ NV12, P010_10LE }") "; "
75 GST_MSDK_CAPS_MAKE_WITH_VA_FEATURE ("NV12")));
77 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
80 GST_STATIC_CAPS ("video/x-vp9, "
81 "framerate = (fraction) [0/1, MAX], "
82 "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
83 "profile = (string) " SRC_PROFILES)
86 #define gst_msdkvp9enc_parent_class parent_class
87 G_DEFINE_TYPE (GstMsdkVP9Enc, gst_msdkvp9enc, GST_TYPE_MSDKENC);
90 gst_msdkvp9enc_set_format (GstMsdkEnc * encoder)
92 GstMsdkVP9Enc *thiz = GST_MSDKVP9ENC (encoder);
93 GstCaps *template_caps;
94 GstCaps *allowed_caps = NULL;
96 thiz->profile = MFX_PROFILE_VP9_0;
97 template_caps = gst_static_pad_template_get_caps (&src_factory);
98 allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
100 /* If downstream has ANY caps let encoder decide profile and level */
101 if (allowed_caps == template_caps) {
102 GST_INFO_OBJECT (thiz,
103 "downstream has ANY caps, profile/level set to auto");
104 } else if (allowed_caps) {
106 const gchar *profile;
108 if (gst_caps_is_empty (allowed_caps)) {
109 gst_caps_unref (allowed_caps);
110 gst_caps_unref (template_caps);
114 allowed_caps = gst_caps_make_writable (allowed_caps);
115 allowed_caps = gst_caps_fixate (allowed_caps);
116 s = gst_caps_get_structure (allowed_caps, 0);
117 profile = gst_structure_get_string (s, "profile");
120 if (!strcmp (profile, "3")) {
121 thiz->profile = MFX_PROFILE_VP9_3;
122 } else if (!strcmp (profile, "2")) {
123 thiz->profile = MFX_PROFILE_VP9_2;
124 } else if (!strcmp (profile, "1")) {
125 thiz->profile = MFX_PROFILE_VP9_1;
126 } else if (!strcmp (profile, "0")) {
127 thiz->profile = MFX_PROFILE_VP9_0;
129 g_assert_not_reached ();
133 gst_caps_unref (allowed_caps);
136 gst_caps_unref (template_caps);
142 gst_msdkvp9enc_configure (GstMsdkEnc * encoder)
144 GstMsdkVP9Enc *vp9enc = GST_MSDKVP9ENC (encoder);
147 if (encoder->hardware) {
148 session = gst_msdk_context_get_session (encoder->context);
150 if (!gst_msdk_load_plugin (session, &MFX_PLUGINID_VP9E_HW, 1, "msdkvp9enc"))
154 encoder->num_extra_frames = encoder->async_depth - 1;
155 encoder->param.mfx.CodecId = MFX_CODEC_VP9;
156 encoder->param.mfx.CodecLevel = 0;
158 switch (encoder->param.mfx.FrameInfo.FourCC) {
159 #if (MFX_VERSION >= 1027)
160 case MFX_FOURCC_Y410:
161 encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_3;
165 case MFX_FOURCC_P010:
166 encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_2;
169 case MFX_FOURCC_AYUV:
170 encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_1;
174 encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_0;
178 /* As the frame width and height is rounded up to 128 and 32 since commit 8daac1c,
179 * so the width, height for initialization should be rounded up to 128 and 32
180 * too because VP9 encoder in MSDK will do some check on width and height.
182 encoder->param.mfx.FrameInfo.Width =
183 GST_ROUND_UP_128 (encoder->param.mfx.FrameInfo.CropW);
184 encoder->param.mfx.FrameInfo.Height =
185 GST_ROUND_UP_32 (encoder->param.mfx.FrameInfo.CropH);
187 /* Always turn on this flag for VP9 */
188 encoder->param.mfx.LowPower = MFX_CODINGOPTION_ON;
190 /* Enable Extended coding options */
191 gst_msdkenc_ensure_extended_coding_options (encoder);
193 memset (&vp9enc->ext_vp9, 0, sizeof (vp9enc->ext_vp9));
194 vp9enc->ext_vp9.Header.BufferId = MFX_EXTBUFF_VP9_PARAM;
195 vp9enc->ext_vp9.Header.BufferSz = sizeof (vp9enc->ext_vp9);
196 vp9enc->ext_vp9.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
198 gst_msdkenc_add_extra_param (encoder, (mfxExtBuffer *) & vp9enc->ext_vp9);
203 static inline const gchar *
204 profile_to_string (gint profile)
207 case MFX_PROFILE_VP9_3:
209 case MFX_PROFILE_VP9_2:
211 case MFX_PROFILE_VP9_1:
213 case MFX_PROFILE_VP9_0:
223 gst_msdkvp9enc_set_src_caps (GstMsdkEnc * encoder)
226 GstStructure *structure;
227 const gchar *profile;
229 caps = gst_caps_new_empty_simple ("video/x-vp9");
230 structure = gst_caps_get_structure (caps, 0);
232 profile = profile_to_string (encoder->param.mfx.CodecProfile);
234 gst_structure_set (structure, "profile", G_TYPE_STRING, profile, NULL);
240 gst_msdkvp9enc_set_property (GObject * object, guint prop_id,
241 const GValue * value, GParamSpec * pspec)
243 GstMsdkVP9Enc *thiz = GST_MSDKVP9ENC (object);
245 if (!gst_msdkenc_set_common_property (object, prop_id, value, pspec))
246 GST_WARNING_OBJECT (thiz, "Failed to set common encode property");
250 gst_msdkvp9enc_get_property (GObject * object, guint prop_id, GValue * value,
253 GstMsdkVP9Enc *thiz = GST_MSDKVP9ENC (object);
255 if (!gst_msdkenc_get_common_property (object, prop_id, value, pspec))
256 GST_WARNING_OBJECT (thiz, "Failed to get common encode property");
260 gst_msdkvp9enc_class_init (GstMsdkVP9EncClass * klass)
262 GObjectClass *gobject_class;
263 GstElementClass *element_class;
264 GstMsdkEncClass *encoder_class;
266 gobject_class = G_OBJECT_CLASS (klass);
267 element_class = GST_ELEMENT_CLASS (klass);
268 encoder_class = GST_MSDKENC_CLASS (klass);
270 gobject_class->set_property = gst_msdkvp9enc_set_property;
271 gobject_class->get_property = gst_msdkvp9enc_get_property;
273 encoder_class->set_format = gst_msdkvp9enc_set_format;
274 encoder_class->configure = gst_msdkvp9enc_configure;
275 encoder_class->set_src_caps = gst_msdkvp9enc_set_src_caps;
276 encoder_class->qp_max = 255;
277 encoder_class->qp_min = 0;
279 gst_msdkenc_install_common_properties (encoder_class);
281 gst_element_class_set_static_metadata (element_class,
282 "Intel MSDK VP9 encoder",
283 "Codec/Encoder/Video/Hardware",
284 "VP9 video encoder based on " MFX_API_SDK,
285 "Haihao Xiang <haihao.xiang@intel.com>");
287 gst_element_class_add_static_pad_template (element_class, &sink_factory);
288 gst_element_class_add_static_pad_template (element_class, &src_factory);
292 gst_msdkvp9enc_init (GstMsdkVP9Enc * thiz)