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