msdkh265enc: slice size control
[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 <gst/allocators/gstdmabuf.h>
37
38 #include "gstmsdkh265enc.h"
39
40 GST_DEBUG_CATEGORY_EXTERN (gst_msdkh265enc_debug);
41 #define GST_CAT_DEFAULT gst_msdkh265enc_debug
42
43 enum
44 {
45   PROP_LOW_POWER = GST_MSDKENC_PROP_MAX,
46   PROP_TILE_ROW,
47   PROP_TILE_COL,
48   PROP_MAX_SLICE_SIZE,
49 };
50
51 #define PROP_LOWPOWER_DEFAULT           FALSE
52 #define PROP_TILE_ROW_DEFAULT           1
53 #define PROP_TILE_COL_DEFAULT           1
54 #define PROP_MAX_SLICE_SIZE_DEFAULT     0
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   const mfxPluginUID *uid;
95
96   session = gst_msdk_context_get_session (encoder->context);
97
98   if (encoder->hardware)
99     uid = &MFX_PLUGINID_HEVCE_HW;
100   else
101     uid = &MFX_PLUGINID_HEVCE_SW;
102
103   if (!gst_msdk_load_plugin (session, uid, 1, "msdkh265enc"))
104     return FALSE;
105
106   encoder->param.mfx.CodecId = MFX_CODEC_HEVC;
107
108   switch (encoder->param.mfx.FrameInfo.FourCC) {
109     case MFX_FOURCC_P010:
110       encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_MAIN10;
111       break;
112     case MFX_FOURCC_AYUV:
113 #if (MFX_VERSION >= 1027)
114     case MFX_FOURCC_Y410:
115 #endif
116       encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_REXT;
117       break;
118     default:
119       encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_MAIN;
120   }
121
122   /* IdrInterval field of MediaSDK HEVC encoder behaves differently
123    * than other encoders. IdrInteval == 1 indicate every
124    * I-frame should be an IDR, IdrInteval == 2 means every other
125    * I-frame is an IDR etc. So we generalize the behaviour of property
126    * "i-frames" by incrementing the value by one in each case*/
127   encoder->param.mfx.IdrInterval += 1;
128
129   /* Enable Extended coding options */
130   encoder->option2.MaxSliceSize = h265enc->max_slice_size;
131   gst_msdkenc_ensure_extended_coding_options (encoder);
132
133   if (h265enc->num_tile_rows > 1 || h265enc->num_tile_cols > 1) {
134     h265enc->ext_tiles.Header.BufferId = MFX_EXTBUFF_HEVC_TILES;
135     h265enc->ext_tiles.Header.BufferSz = sizeof (h265enc->ext_tiles);
136     h265enc->ext_tiles.NumTileRows = h265enc->num_tile_rows;
137     h265enc->ext_tiles.NumTileColumns = h265enc->num_tile_cols;
138
139     gst_msdkenc_add_extra_param (encoder,
140         (mfxExtBuffer *) & h265enc->ext_tiles);
141
142     /* Set a valid value to NumSlice */
143     if (encoder->param.mfx.NumSlice == 0)
144       encoder->param.mfx.NumSlice =
145           h265enc->num_tile_rows * h265enc->num_tile_cols;
146   }
147
148   encoder->param.mfx.LowPower =
149       (h265enc->lowpower ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF);
150
151   return TRUE;
152 }
153
154 static inline const gchar *
155 level_to_string (gint level)
156 {
157   switch (level) {
158     case MFX_LEVEL_HEVC_1:
159       return "1";
160     case MFX_LEVEL_HEVC_2:
161       return "2";
162     case MFX_LEVEL_HEVC_21:
163       return "2.1";
164     case MFX_LEVEL_HEVC_3:
165       return "3";
166     case MFX_LEVEL_HEVC_31:
167       return "3.1";
168     case MFX_LEVEL_HEVC_4:
169       return "4";
170     case MFX_LEVEL_HEVC_41:
171       return "4.1";
172     case MFX_LEVEL_HEVC_5:
173       return "5";
174     case MFX_LEVEL_HEVC_51:
175       return "5.1";
176     case MFX_LEVEL_HEVC_52:
177       return "5.2";
178     case MFX_LEVEL_HEVC_6:
179       return "6";
180     case MFX_LEVEL_HEVC_61:
181       return "6.1";
182     case MFX_LEVEL_HEVC_62:
183       return "6.2";
184     default:
185       break;
186   }
187
188   return NULL;
189 }
190
191 static GstCaps *
192 gst_msdkh265enc_set_src_caps (GstMsdkEnc * encoder)
193 {
194   GstCaps *caps;
195   GstStructure *structure;
196   const gchar *level;
197
198   caps = gst_caps_new_empty_simple ("video/x-h265");
199   structure = gst_caps_get_structure (caps, 0);
200
201   gst_structure_set (structure, "stream-format", G_TYPE_STRING, "byte-stream",
202       NULL);
203
204   gst_structure_set (structure, "alignment", G_TYPE_STRING, "au", NULL);
205
206   switch (encoder->param.mfx.FrameInfo.FourCC) {
207     case MFX_FOURCC_P010:
208       gst_structure_set (structure, "profile", G_TYPE_STRING, "main-10", NULL);
209       break;
210     case MFX_FOURCC_AYUV:
211       gst_structure_set (structure, "profile", G_TYPE_STRING, "main-444", NULL);
212       break;
213 #if (MFX_VERSION >= 1027)
214     case MFX_FOURCC_Y410:
215       gst_structure_set (structure, "profile", G_TYPE_STRING, "main-444-10",
216           NULL);
217       break;
218 #endif
219     default:
220       gst_structure_set (structure, "profile", G_TYPE_STRING, "main", NULL);
221       break;
222   }
223
224   level = level_to_string (encoder->param.mfx.CodecLevel);
225   if (level)
226     gst_structure_set (structure, "level", G_TYPE_STRING, level, NULL);
227
228   return caps;
229 }
230
231 static void
232 gst_msdkh265enc_set_property (GObject * object, guint prop_id,
233     const GValue * value, GParamSpec * pspec)
234 {
235   GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
236
237   if (gst_msdkenc_set_common_property (object, prop_id, value, pspec))
238     return;
239
240   GST_OBJECT_LOCK (thiz);
241
242   switch (prop_id) {
243     case PROP_LOW_POWER:
244       thiz->lowpower = g_value_get_boolean (value);
245       break;
246
247     case PROP_TILE_ROW:
248       thiz->num_tile_rows = g_value_get_uint (value);
249       break;
250
251     case PROP_TILE_COL:
252       thiz->num_tile_cols = g_value_get_uint (value);
253       break;
254
255     case PROP_MAX_SLICE_SIZE:
256       thiz->max_slice_size = g_value_get_uint (value);
257       break;
258
259     default:
260       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261       break;
262   }
263   GST_OBJECT_UNLOCK (thiz);
264 }
265
266 static void
267 gst_msdkh265enc_get_property (GObject * object, guint prop_id, GValue * value,
268     GParamSpec * pspec)
269 {
270   GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
271
272   if (gst_msdkenc_get_common_property (object, prop_id, value, pspec))
273     return;
274
275   GST_OBJECT_LOCK (thiz);
276   switch (prop_id) {
277     case PROP_LOW_POWER:
278       g_value_set_boolean (value, thiz->lowpower);
279       break;
280
281     case PROP_TILE_ROW:
282       g_value_set_uint (value, thiz->num_tile_rows);
283       break;
284
285     case PROP_TILE_COL:
286       g_value_set_uint (value, thiz->num_tile_cols);
287       break;
288
289     case PROP_MAX_SLICE_SIZE:
290       g_value_set_uint (value, thiz->max_slice_size);
291       break;
292
293     default:
294       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295       break;
296   }
297   GST_OBJECT_UNLOCK (thiz);
298 }
299
300 static gboolean
301 gst_msdkh265enc_need_reconfig (GstMsdkEnc * encoder, GstVideoCodecFrame * frame)
302 {
303   GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
304
305   return gst_msdkenc_get_roi_params (encoder, frame, h265enc->roi);
306 }
307
308 static void
309 gst_msdkh265enc_set_extra_params (GstMsdkEnc * encoder,
310     GstVideoCodecFrame * frame)
311 {
312   GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
313
314   if (h265enc->roi[0].NumROI)
315     gst_msdkenc_add_extra_param (encoder, (mfxExtBuffer *) & h265enc->roi[0]);
316 }
317
318 static void
319 gst_msdkh265enc_class_init (GstMsdkH265EncClass * klass)
320 {
321   GObjectClass *gobject_class;
322   GstElementClass *element_class;
323   GstMsdkEncClass *encoder_class;
324
325   gobject_class = G_OBJECT_CLASS (klass);
326   element_class = GST_ELEMENT_CLASS (klass);
327   encoder_class = GST_MSDKENC_CLASS (klass);
328
329   gobject_class->set_property = gst_msdkh265enc_set_property;
330   gobject_class->get_property = gst_msdkh265enc_get_property;
331
332   encoder_class->set_format = gst_msdkh265enc_set_format;
333   encoder_class->configure = gst_msdkh265enc_configure;
334   encoder_class->set_src_caps = gst_msdkh265enc_set_src_caps;
335   encoder_class->need_reconfig = gst_msdkh265enc_need_reconfig;
336   encoder_class->set_extra_params = gst_msdkh265enc_set_extra_params;
337
338   gst_msdkenc_install_common_properties (encoder_class);
339
340   g_object_class_install_property (gobject_class, PROP_LOW_POWER,
341       g_param_spec_boolean ("low-power", "Low power", "Enable low power mode",
342           PROP_LOWPOWER_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
343
344   g_object_class_install_property (gobject_class, PROP_TILE_ROW,
345       g_param_spec_uint ("num-tile-rows", "number of rows for tiled encoding",
346           "number of rows for tiled encoding",
347           1, 8192, PROP_TILE_ROW_DEFAULT,
348           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
349
350   g_object_class_install_property (gobject_class, PROP_TILE_COL,
351       g_param_spec_uint ("num-tile-cols",
352           "number of columns for tiled encoding",
353           "number of columns for tiled encoding", 1, 8192,
354           PROP_TILE_COL_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
355
356   g_object_class_install_property (gobject_class, PROP_MAX_SLICE_SIZE,
357       g_param_spec_uint ("max-slice-size", "Max Slice Size",
358           "Maximum slice size in bytes (if enabled MSDK will ignore the control over num-slices)",
359           0, G_MAXUINT32, PROP_MAX_SLICE_SIZE_DEFAULT,
360           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
361
362   gst_element_class_set_static_metadata (element_class,
363       "Intel MSDK H265 encoder",
364       "Codec/Encoder/Video/Hardware",
365       "H265 video encoder based on Intel Media SDK",
366       "Josep Torra <jtorra@oblong.com>");
367
368   gst_element_class_add_static_pad_template (element_class, &sink_factory);
369   gst_element_class_add_static_pad_template (element_class, &src_factory);
370 }
371
372 static void
373 gst_msdkh265enc_init (GstMsdkH265Enc * thiz)
374 {
375   GstMsdkEnc *msdk_enc = (GstMsdkEnc *) thiz;
376   thiz->lowpower = PROP_LOWPOWER_DEFAULT;
377   thiz->num_tile_rows = PROP_TILE_ROW_DEFAULT;
378   thiz->num_tile_cols = PROP_TILE_COL_DEFAULT;
379   thiz->max_slice_size = PROP_MAX_SLICE_SIZE_DEFAULT;
380   msdk_enc->num_extra_frames = 1;
381 }