1 /* GStreamer Intel MSDK plugin
2 * Copyright (c) 2016, Oblong Industries, Inc.
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.
36 #include <mfxplugin.h>
38 #include <gst/allocators/gstdmabuf.h>
40 #include "gstmsdkh265enc.h"
42 GST_DEBUG_CATEGORY_EXTERN (gst_msdkh265enc_debug);
43 #define GST_CAT_DEFAULT gst_msdkh265enc_debug
47 PROP_LOW_POWER = GST_MSDKENC_PROP_MAX,
50 #define PROP_LOWPOWER_DEFAULT FALSE
52 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_STATIC_CAPS ("video/x-raw, "
56 "format = (string) { NV12, I420, YV12, YUY2, UYVY, BGRA, P010_10LE }, "
57 "framerate = (fraction) [0, MAX], "
58 "width = (int) [ 16, MAX ], height = (int) [ 16, MAX ],"
59 "interlace-mode = (string) progressive" ";"
60 GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_DMABUF,
63 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
66 GST_STATIC_CAPS ("video/x-h265, "
67 "framerate = (fraction) [0/1, MAX], "
68 "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
69 "stream-format = (string) byte-stream , alignment = (string) au , "
70 "profile = (string) { main, main-10 } ")
73 #define gst_msdkh265enc_parent_class parent_class
74 G_DEFINE_TYPE (GstMsdkH265Enc, gst_msdkh265enc, GST_TYPE_MSDKENC);
77 gst_msdkh265enc_set_format (GstMsdkEnc * encoder)
83 gst_msdkh265enc_configure (GstMsdkEnc * encoder)
85 GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
88 const mfxPluginUID *uid;
90 session = gst_msdk_context_get_session (encoder->context);
92 if (encoder->hardware)
93 uid = &MFX_PLUGINID_HEVCE_HW;
95 uid = &MFX_PLUGINID_HEVCE_SW;
97 status = MFXVideoUSER_Load (session, uid, 1);
98 if (status < MFX_ERR_NONE) {
99 GST_ERROR_OBJECT (h265enc, "Media SDK Plugin load failed (%s)",
100 msdk_status_to_string (status));
102 } else if (status > MFX_ERR_NONE) {
103 GST_WARNING_OBJECT (h265enc, "Media SDK Plugin load warning: %s",
104 msdk_status_to_string (status));
107 encoder->param.mfx.CodecId = MFX_CODEC_HEVC;
109 if (encoder->param.mfx.FrameInfo.FourCC == MFX_FOURCC_P010)
110 encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_MAIN10;
112 encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_MAIN;
114 /* IdrInterval field of MediaSDK HEVC encoder behaves differently
115 * than other encoders. IdrInteval == 1 indicate every
116 * I-frame should be an IDR, IdrInteval == 2 means every other
117 * I-frame is an IDR etc. So we generalize the behaviour of property
118 * "i-frames" by incrementing the value by one in each case*/
119 encoder->param.mfx.IdrInterval += 1;
121 /* Enable Extended coding options */
122 gst_msdkenc_ensure_extended_coding_options (encoder);
124 encoder->param.mfx.LowPower =
125 (h265enc->lowpower ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF);
130 static inline const gchar *
131 level_to_string (gint level)
134 case MFX_LEVEL_HEVC_1:
136 case MFX_LEVEL_HEVC_2:
138 case MFX_LEVEL_HEVC_21:
140 case MFX_LEVEL_HEVC_3:
142 case MFX_LEVEL_HEVC_31:
144 case MFX_LEVEL_HEVC_4:
146 case MFX_LEVEL_HEVC_41:
148 case MFX_LEVEL_HEVC_5:
150 case MFX_LEVEL_HEVC_51:
152 case MFX_LEVEL_HEVC_52:
154 case MFX_LEVEL_HEVC_6:
156 case MFX_LEVEL_HEVC_61:
158 case MFX_LEVEL_HEVC_62:
168 gst_msdkh265enc_set_src_caps (GstMsdkEnc * encoder)
171 GstStructure *structure;
174 caps = gst_caps_new_empty_simple ("video/x-h265");
175 structure = gst_caps_get_structure (caps, 0);
177 gst_structure_set (structure, "stream-format", G_TYPE_STRING, "byte-stream",
180 gst_structure_set (structure, "alignment", G_TYPE_STRING, "au", NULL);
182 if (encoder->param.mfx.FrameInfo.FourCC == MFX_FOURCC_P010)
183 gst_structure_set (structure, "profile", G_TYPE_STRING, "main-10", NULL);
185 gst_structure_set (structure, "profile", G_TYPE_STRING, "main", NULL);
187 level = level_to_string (encoder->param.mfx.CodecLevel);
189 gst_structure_set (structure, "level", G_TYPE_STRING, level, NULL);
195 gst_msdkh265enc_set_property (GObject * object, guint prop_id,
196 const GValue * value, GParamSpec * pspec)
198 GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
200 if (gst_msdkenc_set_common_property (object, prop_id, value, pspec))
203 GST_OBJECT_LOCK (thiz);
207 thiz->lowpower = g_value_get_boolean (value);
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213 GST_OBJECT_UNLOCK (thiz);
217 gst_msdkh265enc_get_property (GObject * object, guint prop_id, GValue * value,
220 GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
222 if (gst_msdkenc_get_common_property (object, prop_id, value, pspec))
225 GST_OBJECT_LOCK (thiz);
228 g_value_set_boolean (value, thiz->lowpower);
231 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234 GST_OBJECT_UNLOCK (thiz);
238 gst_msdkh265enc_class_init (GstMsdkH265EncClass * klass)
240 GObjectClass *gobject_class;
241 GstElementClass *element_class;
242 GstMsdkEncClass *encoder_class;
244 gobject_class = G_OBJECT_CLASS (klass);
245 element_class = GST_ELEMENT_CLASS (klass);
246 encoder_class = GST_MSDKENC_CLASS (klass);
248 gobject_class->set_property = gst_msdkh265enc_set_property;
249 gobject_class->get_property = gst_msdkh265enc_get_property;
251 encoder_class->set_format = gst_msdkh265enc_set_format;
252 encoder_class->configure = gst_msdkh265enc_configure;
253 encoder_class->set_src_caps = gst_msdkh265enc_set_src_caps;
255 gst_msdkenc_install_common_properties (encoder_class);
257 g_object_class_install_property (gobject_class, PROP_LOW_POWER,
258 g_param_spec_boolean ("low-power", "Low power", "Enable low power mode",
259 PROP_LOWPOWER_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
261 gst_element_class_set_static_metadata (element_class,
262 "Intel MSDK H265 encoder",
263 "Codec/Encoder/Video",
264 "H265 video encoder based on Intel Media SDK",
265 "Josep Torra <jtorra@oblong.com>");
267 gst_element_class_add_static_pad_template (element_class, &sink_factory);
268 gst_element_class_add_static_pad_template (element_class, &src_factory);
272 gst_msdkh265enc_init (GstMsdkH265Enc * thiz)
274 thiz->lowpower = PROP_LOWPOWER_DEFAULT;