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 <gst/allocators/gstdmabuf.h>
38 #include "gstmsdkh265enc.h"
40 GST_DEBUG_CATEGORY_EXTERN (gst_msdkh265enc_debug);
41 #define GST_CAT_DEFAULT gst_msdkh265enc_debug
45 #ifndef GST_REMOVE_DEPRECATED
46 PROP_LOW_POWER = GST_MSDKENC_PROP_MAX,
49 PROP_TILE_ROW = GST_MSDKENC_PROP_MAX,
58 GST_MSDK_FLAG_LOW_POWER = 1 << 0,
59 GST_MSDK_FLAG_TUNE_MODE = 1 << 1,
62 #define PROP_LOWPOWER_DEFAULT FALSE
63 #define PROP_TILE_ROW_DEFAULT 1
64 #define PROP_TILE_COL_DEFAULT 1
65 #define PROP_MAX_SLICE_SIZE_DEFAULT 0
66 #define PROP_TUNE_MODE_DEFAULT MFX_CODINGOPTION_UNKNOWN
68 #define RAW_FORMATS "NV12, I420, YV12, YUY2, UYVY, BGRA, P010_10LE, VUYA"
69 #define PROFILES "main, main-10, main-444"
71 #if (MFX_VERSION >= 1031)
72 #define COMMON_FORMAT "{ " RAW_FORMATS ", Y410, Y210, P012_LE }"
73 #define PRFOLIE_STR "{ " PROFILES ", main-444-10, main-422-10, main-12 }"
74 #elif (MFX_VERSION >= 1027)
75 #define COMMON_FORMAT "{ " RAW_FORMATS ", Y410, Y210 }"
76 #define PRFOLIE_STR "{ " PROFILES ", main-444-10, main-422-10 }"
78 #define COMMON_FORMAT "{ " RAW_FORMATS " }"
79 #define PRFOLIE_STR "{ " PROFILES " }"
82 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
85 GST_STATIC_CAPS (GST_MSDK_CAPS_STR (COMMON_FORMAT,
86 "{ NV12, P010_10LE }")));
88 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
91 GST_STATIC_CAPS ("video/x-h265, "
92 "framerate = (fraction) [0/1, MAX], "
93 "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
94 "stream-format = (string) byte-stream , alignment = (string) au , "
95 "profile = (string) " PRFOLIE_STR)
98 #define gst_msdkh265enc_parent_class parent_class
99 G_DEFINE_TYPE (GstMsdkH265Enc, gst_msdkh265enc, GST_TYPE_MSDKENC);
102 gst_msdkh265enc_insert_sei (GstMsdkH265Enc * thiz, GstVideoCodecFrame * frame,
105 GstBuffer *new_buffer;
108 thiz->parser = gst_h265_parser_new ();
110 new_buffer = gst_h265_parser_insert_sei (thiz->parser,
111 frame->output_buffer, sei_mem);
114 GST_WARNING_OBJECT (thiz, "Cannot insert SEI nal into AU buffer");
118 gst_buffer_unref (frame->output_buffer);
119 frame->output_buffer = new_buffer;
123 gst_msdkh265enc_add_cc (GstMsdkH265Enc * thiz, GstVideoCodecFrame * frame)
125 GstVideoCaptionMeta *cc_meta;
126 gpointer iter = NULL;
127 GstBuffer *in_buf = frame->input_buffer;
128 GstMemory *mem = NULL;
130 if (thiz->cc_sei_array)
131 g_array_set_size (thiz->cc_sei_array, 0);
134 (GstVideoCaptionMeta *) gst_buffer_iterate_meta_filtered (in_buf,
135 &iter, GST_VIDEO_CAPTION_META_API_TYPE))) {
136 GstH265SEIMessage sei;
137 GstH265RegisteredUserData *rud;
140 if (cc_meta->caption_type != GST_VIDEO_CAPTION_TYPE_CEA708_RAW)
143 memset (&sei, 0, sizeof (GstH265SEIMessage));
144 sei.payloadType = GST_H265_SEI_REGISTERED_USER_DATA;
145 rud = &sei.payload.registered_user_data;
147 rud->country_code = 181;
148 rud->size = cc_meta->size + 10;
150 data = g_malloc (rud->size);
151 memcpy (data + 9, cc_meta->data, cc_meta->size);
153 data[0] = 0; /* 16-bits itu_t_t35_provider_code */
155 data[2] = 'G'; /* 32-bits ATSC_user_identifier */
159 data[6] = 3; /* 8-bits ATSC1_data_user_data_type_code */
161 * 1 bit process_em_data_flag (0)
162 * 1 bit process_cc_data_flag (1)
163 * 1 bit additional_data_flag (0)
166 data[7] = ((cc_meta->size / 3) & 0x1f) | 0x40;
167 data[8] = 255; /* 8 bits em_data, unused */
168 data[cc_meta->size + 9] = 255; /* 8 marker bits */
172 if (!thiz->cc_sei_array) {
174 g_array_new (FALSE, FALSE, sizeof (GstH265SEIMessage));
175 g_array_set_clear_func (thiz->cc_sei_array,
176 (GDestroyNotify) gst_h265_sei_free);
179 g_array_append_val (thiz->cc_sei_array, sei);
182 if (!thiz->cc_sei_array || !thiz->cc_sei_array->len)
185 /* layer_id and temporal_id will be updated by parser later */
186 mem = gst_h265_create_sei_memory (0, 1, 4, thiz->cc_sei_array);
189 GST_WARNING_OBJECT (thiz, "Cannot create SEI nal unit");
193 GST_DEBUG_OBJECT (thiz,
194 "Inserting %d closed caption SEI message(s)", thiz->cc_sei_array->len);
196 gst_msdkh265enc_insert_sei (thiz, frame, mem);
197 gst_memory_unref (mem);
201 gst_msdkh265enc_pre_push (GstVideoEncoder * encoder, GstVideoCodecFrame * frame)
203 GstMsdkH265Enc *thiz = GST_MSDKH265ENC (encoder);
205 gst_msdkh265enc_add_cc (thiz, frame);
211 gst_msdkh265enc_set_format (GstMsdkEnc * encoder)
217 gst_msdkh265enc_configure (GstMsdkEnc * encoder)
219 GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
221 const mfxPluginUID *uid;
223 session = gst_msdk_context_get_session (encoder->context);
225 if (encoder->hardware)
226 uid = &MFX_PLUGINID_HEVCE_HW;
228 uid = &MFX_PLUGINID_HEVCE_SW;
230 if (!gst_msdk_load_plugin (session, uid, 1, "msdkh265enc"))
233 encoder->param.mfx.CodecId = MFX_CODEC_HEVC;
235 switch (encoder->param.mfx.FrameInfo.FourCC) {
236 case MFX_FOURCC_P010:
237 encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_MAIN10;
239 case MFX_FOURCC_AYUV:
240 case MFX_FOURCC_YUY2:
241 #if (MFX_VERSION >= 1027)
242 case MFX_FOURCC_Y410:
243 case MFX_FOURCC_Y210:
245 #if (MFX_VERSION >= 1031)
246 case MFX_FOURCC_P016:
248 encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_REXT;
251 encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_MAIN;
254 /* IdrInterval field of MediaSDK HEVC encoder behaves differently
255 * than other encoders. IdrInteval == 1 indicate every
256 * I-frame should be an IDR, IdrInteval == 2 means every other
257 * I-frame is an IDR etc. So we generalize the behaviour of property
258 * "i-frames" by incrementing the value by one in each case*/
259 encoder->param.mfx.IdrInterval += 1;
261 /* Enable Extended coding options */
262 encoder->option2.MaxSliceSize = h265enc->max_slice_size;
263 gst_msdkenc_ensure_extended_coding_options (encoder);
265 if (h265enc->num_tile_rows > 1 || h265enc->num_tile_cols > 1) {
266 h265enc->ext_tiles.Header.BufferId = MFX_EXTBUFF_HEVC_TILES;
267 h265enc->ext_tiles.Header.BufferSz = sizeof (h265enc->ext_tiles);
268 h265enc->ext_tiles.NumTileRows = h265enc->num_tile_rows;
269 h265enc->ext_tiles.NumTileColumns = h265enc->num_tile_cols;
271 gst_msdkenc_add_extra_param (encoder,
272 (mfxExtBuffer *) & h265enc->ext_tiles);
274 /* Set a valid value to NumSlice */
275 if (encoder->param.mfx.NumSlice == 0)
276 encoder->param.mfx.NumSlice =
277 h265enc->num_tile_rows * h265enc->num_tile_cols;
280 encoder->param.mfx.LowPower = h265enc->tune_mode;
285 static inline const gchar *
286 level_to_string (gint level)
289 case MFX_LEVEL_HEVC_1:
291 case MFX_LEVEL_HEVC_2:
293 case MFX_LEVEL_HEVC_21:
295 case MFX_LEVEL_HEVC_3:
297 case MFX_LEVEL_HEVC_31:
299 case MFX_LEVEL_HEVC_4:
301 case MFX_LEVEL_HEVC_41:
303 case MFX_LEVEL_HEVC_5:
305 case MFX_LEVEL_HEVC_51:
307 case MFX_LEVEL_HEVC_52:
309 case MFX_LEVEL_HEVC_6:
311 case MFX_LEVEL_HEVC_61:
313 case MFX_LEVEL_HEVC_62:
323 gst_msdkh265enc_set_src_caps (GstMsdkEnc * encoder)
326 GstStructure *structure;
329 caps = gst_caps_new_empty_simple ("video/x-h265");
330 structure = gst_caps_get_structure (caps, 0);
332 gst_structure_set (structure, "stream-format", G_TYPE_STRING, "byte-stream",
335 gst_structure_set (structure, "alignment", G_TYPE_STRING, "au", NULL);
337 switch (encoder->param.mfx.FrameInfo.FourCC) {
338 case MFX_FOURCC_P010:
339 gst_structure_set (structure, "profile", G_TYPE_STRING, "main-10", NULL);
341 case MFX_FOURCC_AYUV:
342 gst_structure_set (structure, "profile", G_TYPE_STRING, "main-444", NULL);
344 case MFX_FOURCC_YUY2:
345 /* The profile is main-422-10 for 8-bit 422 */
346 gst_structure_set (structure, "profile", G_TYPE_STRING, "main-422-10",
349 #if (MFX_VERSION >= 1027)
350 case MFX_FOURCC_Y410:
351 gst_structure_set (structure, "profile", G_TYPE_STRING, "main-444-10",
354 case MFX_FOURCC_Y210:
355 gst_structure_set (structure, "profile", G_TYPE_STRING, "main-422-10",
359 #if (MFX_VERSION >= 1031)
360 case MFX_FOURCC_P016:
361 gst_structure_set (structure, "profile", G_TYPE_STRING, "main-12", NULL);
365 gst_structure_set (structure, "profile", G_TYPE_STRING, "main", NULL);
369 level = level_to_string (encoder->param.mfx.CodecLevel);
371 gst_structure_set (structure, "level", G_TYPE_STRING, level, NULL);
377 gst_msdkh265enc_finalize (GObject * object)
379 GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
382 gst_h265_parser_free (thiz->parser);
383 if (thiz->cc_sei_array)
384 g_array_unref (thiz->cc_sei_array);
386 G_OBJECT_CLASS (parent_class)->finalize (object);
390 gst_msdkh265enc_set_property (GObject * object, guint prop_id,
391 const GValue * value, GParamSpec * pspec)
393 GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
395 if (gst_msdkenc_set_common_property (object, prop_id, value, pspec))
398 GST_OBJECT_LOCK (thiz);
401 #ifndef GST_REMOVE_DEPRECATED
403 thiz->lowpower = g_value_get_boolean (value);
404 thiz->prop_flag |= GST_MSDK_FLAG_LOW_POWER;
406 /* Ignore it if user set tune mode explicitly */
407 if (!(thiz->prop_flag & GST_MSDK_FLAG_TUNE_MODE))
409 thiz->lowpower ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF;
415 thiz->num_tile_rows = g_value_get_uint (value);
419 thiz->num_tile_cols = g_value_get_uint (value);
422 case PROP_MAX_SLICE_SIZE:
423 thiz->max_slice_size = g_value_get_uint (value);
427 thiz->tune_mode = g_value_get_enum (value);
428 thiz->prop_flag |= GST_MSDK_FLAG_TUNE_MODE;
432 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
435 GST_OBJECT_UNLOCK (thiz);
439 gst_msdkh265enc_get_property (GObject * object, guint prop_id, GValue * value,
442 GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
444 if (gst_msdkenc_get_common_property (object, prop_id, value, pspec))
447 GST_OBJECT_LOCK (thiz);
449 #ifndef GST_REMOVE_DEPRECATED
451 g_value_set_boolean (value, thiz->lowpower);
456 g_value_set_uint (value, thiz->num_tile_rows);
460 g_value_set_uint (value, thiz->num_tile_cols);
463 case PROP_MAX_SLICE_SIZE:
464 g_value_set_uint (value, thiz->max_slice_size);
468 g_value_set_enum (value, thiz->tune_mode);
472 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
475 GST_OBJECT_UNLOCK (thiz);
479 gst_msdkh265enc_need_reconfig (GstMsdkEnc * encoder, GstVideoCodecFrame * frame)
481 GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
483 return gst_msdkenc_get_roi_params (encoder, frame, h265enc->roi);
487 gst_msdkh265enc_set_extra_params (GstMsdkEnc * encoder,
488 GstVideoCodecFrame * frame)
490 GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
492 if (h265enc->roi[0].NumROI)
493 gst_msdkenc_add_extra_param (encoder, (mfxExtBuffer *) & h265enc->roi[0]);
497 gst_msdkh265enc_need_conversion (GstMsdkEnc * encoder, GstVideoInfo * info,
498 GstVideoFormat * out_format)
500 GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
502 switch (GST_VIDEO_INFO_FORMAT (info)) {
503 case GST_VIDEO_FORMAT_NV12:
504 case GST_VIDEO_FORMAT_P010_10LE:
505 case GST_VIDEO_FORMAT_VUYA:
506 #if (MFX_VERSION >= 1027)
507 case GST_VIDEO_FORMAT_Y410:
508 case GST_VIDEO_FORMAT_Y210:
510 #if (MFX_VERSION >= 1031)
511 case GST_VIDEO_FORMAT_P012_LE:
515 case GST_VIDEO_FORMAT_YUY2:
516 #if (MFX_VERSION >= 1027)
517 if (encoder->codename >= MFX_PLATFORM_ICELAKE &&
518 h265enc->tune_mode == MFX_CODINGOPTION_OFF)
522 if (GST_VIDEO_INFO_COMP_DEPTH (info, 0) == 10)
523 *out_format = GST_VIDEO_FORMAT_P010_10LE;
525 *out_format = GST_VIDEO_FORMAT_NV12;
531 gst_msdkh265enc_class_init (GstMsdkH265EncClass * klass)
533 GObjectClass *gobject_class;
534 GstElementClass *element_class;
535 GstVideoEncoderClass *videoencoder_class;
536 GstMsdkEncClass *encoder_class;
538 gobject_class = G_OBJECT_CLASS (klass);
539 element_class = GST_ELEMENT_CLASS (klass);
540 videoencoder_class = GST_VIDEO_ENCODER_CLASS (klass);
541 encoder_class = GST_MSDKENC_CLASS (klass);
543 gobject_class->finalize = gst_msdkh265enc_finalize;
544 gobject_class->set_property = gst_msdkh265enc_set_property;
545 gobject_class->get_property = gst_msdkh265enc_get_property;
547 videoencoder_class->pre_push = gst_msdkh265enc_pre_push;
549 encoder_class->set_format = gst_msdkh265enc_set_format;
550 encoder_class->configure = gst_msdkh265enc_configure;
551 encoder_class->set_src_caps = gst_msdkh265enc_set_src_caps;
552 encoder_class->need_reconfig = gst_msdkh265enc_need_reconfig;
553 encoder_class->set_extra_params = gst_msdkh265enc_set_extra_params;
554 encoder_class->need_conversion = gst_msdkh265enc_need_conversion;
556 gst_msdkenc_install_common_properties (encoder_class);
558 #ifndef GST_REMOVE_DEPRECATED
559 g_object_class_install_property (gobject_class, PROP_LOW_POWER,
560 g_param_spec_boolean ("low-power", "Low power",
561 "Enable low power mode (DEPRECATED, use tune instead)",
562 PROP_LOWPOWER_DEFAULT,
563 G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
566 g_object_class_install_property (gobject_class, PROP_TILE_ROW,
567 g_param_spec_uint ("num-tile-rows", "number of rows for tiled encoding",
568 "number of rows for tiled encoding",
569 1, 8192, PROP_TILE_ROW_DEFAULT,
570 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
572 g_object_class_install_property (gobject_class, PROP_TILE_COL,
573 g_param_spec_uint ("num-tile-cols",
574 "number of columns for tiled encoding",
575 "number of columns for tiled encoding", 1, 8192,
576 PROP_TILE_COL_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
578 g_object_class_install_property (gobject_class, PROP_MAX_SLICE_SIZE,
579 g_param_spec_uint ("max-slice-size", "Max Slice Size",
580 "Maximum slice size in bytes (if enabled MSDK will ignore the control over num-slices)",
581 0, G_MAXUINT32, PROP_MAX_SLICE_SIZE_DEFAULT,
582 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
584 g_object_class_install_property (gobject_class, PROP_TUNE_MODE,
585 g_param_spec_enum ("tune", "Encoder tuning",
586 "Encoder tuning option",
587 gst_msdkenc_tune_mode_get_type (), PROP_TUNE_MODE_DEFAULT,
588 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
590 gst_element_class_set_static_metadata (element_class,
591 "Intel MSDK H265 encoder",
592 "Codec/Encoder/Video/Hardware",
593 "H265 video encoder based on Intel Media SDK",
594 "Josep Torra <jtorra@oblong.com>");
596 gst_element_class_add_static_pad_template (element_class, &sink_factory);
597 gst_element_class_add_static_pad_template (element_class, &src_factory);
601 gst_msdkh265enc_init (GstMsdkH265Enc * thiz)
603 GstMsdkEnc *msdk_enc = (GstMsdkEnc *) thiz;
604 thiz->lowpower = PROP_LOWPOWER_DEFAULT;
605 thiz->num_tile_rows = PROP_TILE_ROW_DEFAULT;
606 thiz->num_tile_cols = PROP_TILE_COL_DEFAULT;
607 thiz->max_slice_size = PROP_MAX_SLICE_SIZE_DEFAULT;
608 thiz->tune_mode = PROP_TUNE_MODE_DEFAULT;
609 msdk_enc->num_extra_frames = 1;