msdk: support P010_10LE in DMABuf mode for VP9/HEVC encoding
[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 RAW_FORMATS "NV12, I420, YV12, YUY2, UYVY, BGRA, P010_10LE, VUYA"
53
54 #if (MFX_VERSION >= 1027)
55 #define COMMON_FORMAT "{ " RAW_FORMATS ", Y410 }"
56 #else
57 #define COMMON_FORMAT "{ " RAW_FORMATS " }"
58 #endif
59
60 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
61     GST_PAD_SINK,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS (GST_MSDK_CAPS_STR (COMMON_FORMAT,
64             "{ NV12, P010_10LE }")));
65
66 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
67     GST_PAD_SRC,
68     GST_PAD_ALWAYS,
69     GST_STATIC_CAPS ("video/x-h265, "
70         "framerate = (fraction) [0/1, MAX], "
71         "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
72         "stream-format = (string) byte-stream , alignment = (string) au , "
73         "profile = (string) { main, main-10, main-444, main-444-10 } ")
74     );
75
76 #define gst_msdkh265enc_parent_class parent_class
77 G_DEFINE_TYPE (GstMsdkH265Enc, gst_msdkh265enc, GST_TYPE_MSDKENC);
78
79 static gboolean
80 gst_msdkh265enc_set_format (GstMsdkEnc * encoder)
81 {
82   return TRUE;
83 }
84
85 static gboolean
86 gst_msdkh265enc_configure (GstMsdkEnc * encoder)
87 {
88   GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
89   mfxSession session;
90   mfxStatus status;
91   const mfxPluginUID *uid;
92
93   session = gst_msdk_context_get_session (encoder->context);
94
95   if (encoder->hardware)
96     uid = &MFX_PLUGINID_HEVCE_HW;
97   else
98     uid = &MFX_PLUGINID_HEVCE_SW;
99
100   status = MFXVideoUSER_Load (session, uid, 1);
101   if (status < MFX_ERR_NONE) {
102     GST_ERROR_OBJECT (h265enc, "Media SDK Plugin load failed (%s)",
103         msdk_status_to_string (status));
104     return FALSE;
105   } else if (status > MFX_ERR_NONE) {
106     GST_WARNING_OBJECT (h265enc, "Media SDK Plugin load warning: %s",
107         msdk_status_to_string (status));
108   }
109
110   encoder->param.mfx.CodecId = MFX_CODEC_HEVC;
111
112   switch (encoder->param.mfx.FrameInfo.FourCC) {
113     case MFX_FOURCC_P010:
114       encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_MAIN10;
115       break;
116     case MFX_FOURCC_AYUV:
117 #if (MFX_VERSION >= 1027)
118     case MFX_FOURCC_Y410:
119 #endif
120       encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_REXT;
121       break;
122     default:
123       encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_MAIN;
124   }
125
126   /* IdrInterval field of MediaSDK HEVC encoder behaves differently
127    * than other encoders. IdrInteval == 1 indicate every
128    * I-frame should be an IDR, IdrInteval == 2 means every other
129    * I-frame is an IDR etc. So we generalize the behaviour of property
130    * "i-frames" by incrementing the value by one in each case*/
131   encoder->param.mfx.IdrInterval += 1;
132
133   /* Enable Extended coding options */
134   gst_msdkenc_ensure_extended_coding_options (encoder);
135
136   encoder->param.mfx.LowPower =
137       (h265enc->lowpower ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF);
138
139   return TRUE;
140 }
141
142 static inline const gchar *
143 level_to_string (gint level)
144 {
145   switch (level) {
146     case MFX_LEVEL_HEVC_1:
147       return "1";
148     case MFX_LEVEL_HEVC_2:
149       return "2";
150     case MFX_LEVEL_HEVC_21:
151       return "2.1";
152     case MFX_LEVEL_HEVC_3:
153       return "3";
154     case MFX_LEVEL_HEVC_31:
155       return "3.1";
156     case MFX_LEVEL_HEVC_4:
157       return "4";
158     case MFX_LEVEL_HEVC_41:
159       return "4.1";
160     case MFX_LEVEL_HEVC_5:
161       return "5";
162     case MFX_LEVEL_HEVC_51:
163       return "5.1";
164     case MFX_LEVEL_HEVC_52:
165       return "5.2";
166     case MFX_LEVEL_HEVC_6:
167       return "6";
168     case MFX_LEVEL_HEVC_61:
169       return "6.1";
170     case MFX_LEVEL_HEVC_62:
171       return "6.2";
172     default:
173       break;
174   }
175
176   return NULL;
177 }
178
179 static GstCaps *
180 gst_msdkh265enc_set_src_caps (GstMsdkEnc * encoder)
181 {
182   GstCaps *caps;
183   GstStructure *structure;
184   const gchar *level;
185
186   caps = gst_caps_new_empty_simple ("video/x-h265");
187   structure = gst_caps_get_structure (caps, 0);
188
189   gst_structure_set (structure, "stream-format", G_TYPE_STRING, "byte-stream",
190       NULL);
191
192   gst_structure_set (structure, "alignment", G_TYPE_STRING, "au", NULL);
193
194   switch (encoder->param.mfx.FrameInfo.FourCC) {
195     case MFX_FOURCC_P010:
196       gst_structure_set (structure, "profile", G_TYPE_STRING, "main-10", NULL);
197       break;
198     case MFX_FOURCC_AYUV:
199       gst_structure_set (structure, "profile", G_TYPE_STRING, "main-444", NULL);
200       break;
201 #if (MFX_VERSION >= 1027)
202     case MFX_FOURCC_Y410:
203       gst_structure_set (structure, "profile", G_TYPE_STRING, "main-444-10",
204           NULL);
205       break;
206 #endif
207     default:
208       gst_structure_set (structure, "profile", G_TYPE_STRING, "main", NULL);
209       break;
210   }
211
212   level = level_to_string (encoder->param.mfx.CodecLevel);
213   if (level)
214     gst_structure_set (structure, "level", G_TYPE_STRING, level, NULL);
215
216   return caps;
217 }
218
219 static void
220 gst_msdkh265enc_set_property (GObject * object, guint prop_id,
221     const GValue * value, GParamSpec * pspec)
222 {
223   GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
224
225   if (gst_msdkenc_set_common_property (object, prop_id, value, pspec))
226     return;
227
228   GST_OBJECT_LOCK (thiz);
229
230   switch (prop_id) {
231     case PROP_LOW_POWER:
232       thiz->lowpower = g_value_get_boolean (value);
233       break;
234     default:
235       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236       break;
237   }
238   GST_OBJECT_UNLOCK (thiz);
239 }
240
241 static void
242 gst_msdkh265enc_get_property (GObject * object, guint prop_id, GValue * value,
243     GParamSpec * pspec)
244 {
245   GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
246
247   if (gst_msdkenc_get_common_property (object, prop_id, value, pspec))
248     return;
249
250   GST_OBJECT_LOCK (thiz);
251   switch (prop_id) {
252     case PROP_LOW_POWER:
253       g_value_set_boolean (value, thiz->lowpower);
254       break;
255     default:
256       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257       break;
258   }
259   GST_OBJECT_UNLOCK (thiz);
260 }
261
262 static void
263 gst_msdkh265enc_class_init (GstMsdkH265EncClass * klass)
264 {
265   GObjectClass *gobject_class;
266   GstElementClass *element_class;
267   GstMsdkEncClass *encoder_class;
268
269   gobject_class = G_OBJECT_CLASS (klass);
270   element_class = GST_ELEMENT_CLASS (klass);
271   encoder_class = GST_MSDKENC_CLASS (klass);
272
273   gobject_class->set_property = gst_msdkh265enc_set_property;
274   gobject_class->get_property = gst_msdkh265enc_get_property;
275
276   encoder_class->set_format = gst_msdkh265enc_set_format;
277   encoder_class->configure = gst_msdkh265enc_configure;
278   encoder_class->set_src_caps = gst_msdkh265enc_set_src_caps;
279
280   gst_msdkenc_install_common_properties (encoder_class);
281
282   g_object_class_install_property (gobject_class, PROP_LOW_POWER,
283       g_param_spec_boolean ("low-power", "Low power", "Enable low power mode",
284           PROP_LOWPOWER_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
285
286   gst_element_class_set_static_metadata (element_class,
287       "Intel MSDK H265 encoder",
288       "Codec/Encoder/Video/Hardware",
289       "H265 video encoder based on Intel Media SDK",
290       "Josep Torra <jtorra@oblong.com>");
291
292   gst_element_class_add_static_pad_template (element_class, &sink_factory);
293   gst_element_class_add_static_pad_template (element_class, &src_factory);
294 }
295
296 static void
297 gst_msdkh265enc_init (GstMsdkH265Enc * thiz)
298 {
299   GstMsdkEnc *msdk_enc = (GstMsdkEnc *) thiz;
300   thiz->lowpower = PROP_LOWPOWER_DEFAULT;
301   msdk_enc->num_extra_frames = 1;
302 }