msdk: Remove all DMABuf caps features on Windows
[platform/upstream/gstreamer.git] / sys / msdk / gstmsdkh265enc.c
1 /* GStreamer Intel MSDK plugin
2  * Copyright (c) 2016, Oblong Industries, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  *
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.
14  *
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.
18  *
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.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #  include <config.h>
34 #endif
35
36 #include <mfxplugin.h>
37
38 #include <gst/allocators/gstdmabuf.h>
39
40 #include "gstmsdkh265enc.h"
41
42 GST_DEBUG_CATEGORY_EXTERN (gst_msdkh265enc_debug);
43 #define GST_CAT_DEFAULT gst_msdkh265enc_debug
44
45 enum
46 {
47   PROP_LOW_POWER = GST_MSDKENC_PROP_MAX,
48 };
49
50 #define PROP_LOWPOWER_DEFAULT           FALSE
51
52 #define COMMON_FORMAT "{ NV12, I420, YV12, YUY2, UYVY, BGRA, P010_10LE }"
53
54 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
55     GST_PAD_SINK,
56     GST_PAD_ALWAYS,
57     GST_STATIC_CAPS (GST_MSDK_CAPS_STR (COMMON_FORMAT, "NV12")));
58
59 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
60     GST_PAD_SRC,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS ("video/x-h265, "
63         "framerate = (fraction) [0/1, MAX], "
64         "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
65         "stream-format = (string) byte-stream , alignment = (string) au , "
66         "profile = (string) { main, main-10 } ")
67     );
68
69 #define gst_msdkh265enc_parent_class parent_class
70 G_DEFINE_TYPE (GstMsdkH265Enc, gst_msdkh265enc, GST_TYPE_MSDKENC);
71
72 static gboolean
73 gst_msdkh265enc_set_format (GstMsdkEnc * encoder)
74 {
75   return TRUE;
76 }
77
78 static gboolean
79 gst_msdkh265enc_configure (GstMsdkEnc * encoder)
80 {
81   GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
82   mfxSession session;
83   mfxStatus status;
84   const mfxPluginUID *uid;
85
86   session = gst_msdk_context_get_session (encoder->context);
87
88   if (encoder->hardware)
89     uid = &MFX_PLUGINID_HEVCE_HW;
90   else
91     uid = &MFX_PLUGINID_HEVCE_SW;
92
93   status = MFXVideoUSER_Load (session, uid, 1);
94   if (status < MFX_ERR_NONE) {
95     GST_ERROR_OBJECT (h265enc, "Media SDK Plugin load failed (%s)",
96         msdk_status_to_string (status));
97     return FALSE;
98   } else if (status > MFX_ERR_NONE) {
99     GST_WARNING_OBJECT (h265enc, "Media SDK Plugin load warning: %s",
100         msdk_status_to_string (status));
101   }
102
103   encoder->param.mfx.CodecId = MFX_CODEC_HEVC;
104
105   if (encoder->param.mfx.FrameInfo.FourCC == MFX_FOURCC_P010)
106     encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_MAIN10;
107   else
108     encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_MAIN;
109
110   /* IdrInterval field of MediaSDK HEVC encoder behaves differently
111    * than other encoders. IdrInteval == 1 indicate every
112    * I-frame should be an IDR, IdrInteval == 2 means every other
113    * I-frame is an IDR etc. So we generalize the behaviour of property
114    * "i-frames" by incrementing the value by one in each case*/
115   encoder->param.mfx.IdrInterval += 1;
116
117   /* Enable Extended coding options */
118   gst_msdkenc_ensure_extended_coding_options (encoder);
119
120   encoder->param.mfx.LowPower =
121       (h265enc->lowpower ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF);
122
123   return TRUE;
124 }
125
126 static inline const gchar *
127 level_to_string (gint level)
128 {
129   switch (level) {
130     case MFX_LEVEL_HEVC_1:
131       return "1";
132     case MFX_LEVEL_HEVC_2:
133       return "2";
134     case MFX_LEVEL_HEVC_21:
135       return "2.1";
136     case MFX_LEVEL_HEVC_3:
137       return "3";
138     case MFX_LEVEL_HEVC_31:
139       return "3.1";
140     case MFX_LEVEL_HEVC_4:
141       return "4";
142     case MFX_LEVEL_HEVC_41:
143       return "4.1";
144     case MFX_LEVEL_HEVC_5:
145       return "5";
146     case MFX_LEVEL_HEVC_51:
147       return "5.1";
148     case MFX_LEVEL_HEVC_52:
149       return "5.2";
150     case MFX_LEVEL_HEVC_6:
151       return "6";
152     case MFX_LEVEL_HEVC_61:
153       return "6.1";
154     case MFX_LEVEL_HEVC_62:
155       return "6.2";
156     default:
157       break;
158   }
159
160   return NULL;
161 }
162
163 static GstCaps *
164 gst_msdkh265enc_set_src_caps (GstMsdkEnc * encoder)
165 {
166   GstCaps *caps;
167   GstStructure *structure;
168   const gchar *level;
169
170   caps = gst_caps_new_empty_simple ("video/x-h265");
171   structure = gst_caps_get_structure (caps, 0);
172
173   gst_structure_set (structure, "stream-format", G_TYPE_STRING, "byte-stream",
174       NULL);
175
176   gst_structure_set (structure, "alignment", G_TYPE_STRING, "au", NULL);
177
178   if (encoder->param.mfx.FrameInfo.FourCC == MFX_FOURCC_P010)
179     gst_structure_set (structure, "profile", G_TYPE_STRING, "main-10", NULL);
180   else
181     gst_structure_set (structure, "profile", G_TYPE_STRING, "main", NULL);
182
183   level = level_to_string (encoder->param.mfx.CodecLevel);
184   if (level)
185     gst_structure_set (structure, "level", G_TYPE_STRING, level, NULL);
186
187   return caps;
188 }
189
190 static void
191 gst_msdkh265enc_set_property (GObject * object, guint prop_id,
192     const GValue * value, GParamSpec * pspec)
193 {
194   GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
195
196   if (gst_msdkenc_set_common_property (object, prop_id, value, pspec))
197     return;
198
199   GST_OBJECT_LOCK (thiz);
200
201   switch (prop_id) {
202     case PROP_LOW_POWER:
203       thiz->lowpower = g_value_get_boolean (value);
204       break;
205     default:
206       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207       break;
208   }
209   GST_OBJECT_UNLOCK (thiz);
210 }
211
212 static void
213 gst_msdkh265enc_get_property (GObject * object, guint prop_id, GValue * value,
214     GParamSpec * pspec)
215 {
216   GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
217
218   if (gst_msdkenc_get_common_property (object, prop_id, value, pspec))
219     return;
220
221   GST_OBJECT_LOCK (thiz);
222   switch (prop_id) {
223     case PROP_LOW_POWER:
224       g_value_set_boolean (value, thiz->lowpower);
225       break;
226     default:
227       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228       break;
229   }
230   GST_OBJECT_UNLOCK (thiz);
231 }
232
233 static void
234 gst_msdkh265enc_class_init (GstMsdkH265EncClass * klass)
235 {
236   GObjectClass *gobject_class;
237   GstElementClass *element_class;
238   GstMsdkEncClass *encoder_class;
239
240   gobject_class = G_OBJECT_CLASS (klass);
241   element_class = GST_ELEMENT_CLASS (klass);
242   encoder_class = GST_MSDKENC_CLASS (klass);
243
244   gobject_class->set_property = gst_msdkh265enc_set_property;
245   gobject_class->get_property = gst_msdkh265enc_get_property;
246
247   encoder_class->set_format = gst_msdkh265enc_set_format;
248   encoder_class->configure = gst_msdkh265enc_configure;
249   encoder_class->set_src_caps = gst_msdkh265enc_set_src_caps;
250
251   gst_msdkenc_install_common_properties (encoder_class);
252
253   g_object_class_install_property (gobject_class, PROP_LOW_POWER,
254       g_param_spec_boolean ("low-power", "Low power", "Enable low power mode",
255           PROP_LOWPOWER_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256
257   gst_element_class_set_static_metadata (element_class,
258       "Intel MSDK H265 encoder",
259       "Codec/Encoder/Video/Hardware",
260       "H265 video encoder based on Intel Media SDK",
261       "Josep Torra <jtorra@oblong.com>");
262
263   gst_element_class_add_static_pad_template (element_class, &sink_factory);
264   gst_element_class_add_static_pad_template (element_class, &src_factory);
265 }
266
267 static void
268 gst_msdkh265enc_init (GstMsdkH265Enc * thiz)
269 {
270   thiz->lowpower = PROP_LOWPOWER_DEFAULT;
271 }