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 PROP_LOW_POWER = GST_MSDKENC_PROP_MAX,
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
56 #define RAW_FORMATS "NV12, I420, YV12, YUY2, UYVY, BGRA, P010_10LE, VUYA"
58 #if (MFX_VERSION >= 1027)
59 #define COMMON_FORMAT "{ " RAW_FORMATS ", Y410, Y210 }"
61 #define COMMON_FORMAT "{ " RAW_FORMATS " }"
64 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
67 GST_STATIC_CAPS (GST_MSDK_CAPS_STR (COMMON_FORMAT,
68 "{ NV12, P010_10LE }")));
70 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
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, main-422-10 } ")
80 #define gst_msdkh265enc_parent_class parent_class
81 G_DEFINE_TYPE (GstMsdkH265Enc, gst_msdkh265enc, GST_TYPE_MSDKENC);
84 gst_msdkh265enc_insert_sei (GstMsdkH265Enc * thiz, GstVideoCodecFrame * frame,
87 GstBuffer *new_buffer;
90 thiz->parser = gst_h265_parser_new ();
92 new_buffer = gst_h265_parser_insert_sei (thiz->parser,
93 frame->output_buffer, sei_mem);
96 GST_WARNING_OBJECT (thiz, "Cannot insert SEI nal into AU buffer");
100 gst_buffer_unref (frame->output_buffer);
101 frame->output_buffer = new_buffer;
105 gst_msdkh265enc_add_cc (GstMsdkH265Enc * thiz, GstVideoCodecFrame * frame)
107 GstVideoCaptionMeta *cc_meta;
108 gpointer iter = NULL;
109 GstBuffer *in_buf = frame->input_buffer;
110 GstMemory *mem = NULL;
112 if (thiz->cc_sei_array)
113 g_array_set_size (thiz->cc_sei_array, 0);
116 (GstVideoCaptionMeta *) gst_buffer_iterate_meta_filtered (in_buf,
117 &iter, GST_VIDEO_CAPTION_META_API_TYPE))) {
118 GstH265SEIMessage sei;
119 GstH265RegisteredUserData *rud;
122 if (cc_meta->caption_type != GST_VIDEO_CAPTION_TYPE_CEA708_RAW)
125 memset (&sei, 0, sizeof (GstH265SEIMessage));
126 sei.payloadType = GST_H265_SEI_REGISTERED_USER_DATA;
127 rud = &sei.payload.registered_user_data;
129 rud->country_code = 181;
130 rud->size = cc_meta->size + 10;
132 data = g_malloc (rud->size);
133 memcpy (data + 9, cc_meta->data, cc_meta->size);
135 data[0] = 0; /* 16-bits itu_t_t35_provider_code */
137 data[2] = 'G'; /* 32-bits ATSC_user_identifier */
141 data[6] = 3; /* 8-bits ATSC1_data_user_data_type_code */
143 * 1 bit process_em_data_flag (0)
144 * 1 bit process_cc_data_flag (1)
145 * 1 bit additional_data_flag (0)
148 data[7] = ((cc_meta->size / 3) & 0x1f) | 0x40;
149 data[8] = 255; /* 8 bits em_data, unused */
150 data[cc_meta->size + 9] = 255; /* 8 marker bits */
154 if (!thiz->cc_sei_array) {
156 g_array_new (FALSE, FALSE, sizeof (GstH265SEIMessage));
157 g_array_set_clear_func (thiz->cc_sei_array,
158 (GDestroyNotify) gst_h265_sei_free);
161 g_array_append_val (thiz->cc_sei_array, sei);
164 if (!thiz->cc_sei_array || !thiz->cc_sei_array->len)
167 /* layer_id and temporal_id will be updated by parser later */
168 mem = gst_h265_create_sei_memory (0, 1, 4, thiz->cc_sei_array);
171 GST_WARNING_OBJECT (thiz, "Cannot create SEI nal unit");
175 GST_DEBUG_OBJECT (thiz,
176 "Inserting %d closed caption SEI message(s)", thiz->cc_sei_array->len);
178 gst_msdkh265enc_insert_sei (thiz, frame, mem);
179 gst_memory_unref (mem);
183 gst_msdkh265enc_pre_push (GstVideoEncoder * encoder, GstVideoCodecFrame * frame)
185 GstMsdkH265Enc *thiz = GST_MSDKH265ENC (encoder);
187 gst_msdkh265enc_add_cc (thiz, frame);
193 gst_msdkh265enc_set_format (GstMsdkEnc * encoder)
199 gst_msdkh265enc_configure (GstMsdkEnc * encoder)
201 GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
203 const mfxPluginUID *uid;
205 session = gst_msdk_context_get_session (encoder->context);
207 if (encoder->hardware)
208 uid = &MFX_PLUGINID_HEVCE_HW;
210 uid = &MFX_PLUGINID_HEVCE_SW;
212 if (!gst_msdk_load_plugin (session, uid, 1, "msdkh265enc"))
215 encoder->param.mfx.CodecId = MFX_CODEC_HEVC;
217 switch (encoder->param.mfx.FrameInfo.FourCC) {
218 case MFX_FOURCC_P010:
219 encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_MAIN10;
221 case MFX_FOURCC_AYUV:
222 case MFX_FOURCC_YUY2:
223 #if (MFX_VERSION >= 1027)
224 case MFX_FOURCC_Y410:
225 case MFX_FOURCC_Y210:
227 encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_REXT;
230 encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_MAIN;
233 /* IdrInterval field of MediaSDK HEVC encoder behaves differently
234 * than other encoders. IdrInteval == 1 indicate every
235 * I-frame should be an IDR, IdrInteval == 2 means every other
236 * I-frame is an IDR etc. So we generalize the behaviour of property
237 * "i-frames" by incrementing the value by one in each case*/
238 encoder->param.mfx.IdrInterval += 1;
240 /* Enable Extended coding options */
241 encoder->option2.MaxSliceSize = h265enc->max_slice_size;
242 gst_msdkenc_ensure_extended_coding_options (encoder);
244 if (h265enc->num_tile_rows > 1 || h265enc->num_tile_cols > 1) {
245 h265enc->ext_tiles.Header.BufferId = MFX_EXTBUFF_HEVC_TILES;
246 h265enc->ext_tiles.Header.BufferSz = sizeof (h265enc->ext_tiles);
247 h265enc->ext_tiles.NumTileRows = h265enc->num_tile_rows;
248 h265enc->ext_tiles.NumTileColumns = h265enc->num_tile_cols;
250 gst_msdkenc_add_extra_param (encoder,
251 (mfxExtBuffer *) & h265enc->ext_tiles);
253 /* Set a valid value to NumSlice */
254 if (encoder->param.mfx.NumSlice == 0)
255 encoder->param.mfx.NumSlice =
256 h265enc->num_tile_rows * h265enc->num_tile_cols;
259 encoder->param.mfx.LowPower =
260 (h265enc->lowpower ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF);
265 static inline const gchar *
266 level_to_string (gint level)
269 case MFX_LEVEL_HEVC_1:
271 case MFX_LEVEL_HEVC_2:
273 case MFX_LEVEL_HEVC_21:
275 case MFX_LEVEL_HEVC_3:
277 case MFX_LEVEL_HEVC_31:
279 case MFX_LEVEL_HEVC_4:
281 case MFX_LEVEL_HEVC_41:
283 case MFX_LEVEL_HEVC_5:
285 case MFX_LEVEL_HEVC_51:
287 case MFX_LEVEL_HEVC_52:
289 case MFX_LEVEL_HEVC_6:
291 case MFX_LEVEL_HEVC_61:
293 case MFX_LEVEL_HEVC_62:
303 gst_msdkh265enc_set_src_caps (GstMsdkEnc * encoder)
306 GstStructure *structure;
309 caps = gst_caps_new_empty_simple ("video/x-h265");
310 structure = gst_caps_get_structure (caps, 0);
312 gst_structure_set (structure, "stream-format", G_TYPE_STRING, "byte-stream",
315 gst_structure_set (structure, "alignment", G_TYPE_STRING, "au", NULL);
317 switch (encoder->param.mfx.FrameInfo.FourCC) {
318 case MFX_FOURCC_P010:
319 gst_structure_set (structure, "profile", G_TYPE_STRING, "main-10", NULL);
321 case MFX_FOURCC_AYUV:
322 gst_structure_set (structure, "profile", G_TYPE_STRING, "main-444", NULL);
324 case MFX_FOURCC_YUY2:
325 /* The profile is main-422-10 for 8-bit 422 */
326 gst_structure_set (structure, "profile", G_TYPE_STRING, "main-422-10",
329 #if (MFX_VERSION >= 1027)
330 case MFX_FOURCC_Y410:
331 gst_structure_set (structure, "profile", G_TYPE_STRING, "main-444-10",
334 case MFX_FOURCC_Y210:
335 gst_structure_set (structure, "profile", G_TYPE_STRING, "main-422-10",
340 gst_structure_set (structure, "profile", G_TYPE_STRING, "main", NULL);
344 level = level_to_string (encoder->param.mfx.CodecLevel);
346 gst_structure_set (structure, "level", G_TYPE_STRING, level, NULL);
352 gst_msdkh265enc_finalize (GObject * object)
354 GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
357 gst_h264_nal_parser_free (thiz->parser);
358 if (thiz->cc_sei_array)
359 g_array_unref (thiz->cc_sei_array);
361 G_OBJECT_CLASS (parent_class)->finalize (object);
365 gst_msdkh265enc_set_property (GObject * object, guint prop_id,
366 const GValue * value, GParamSpec * pspec)
368 GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
370 if (gst_msdkenc_set_common_property (object, prop_id, value, pspec))
373 GST_OBJECT_LOCK (thiz);
377 thiz->lowpower = g_value_get_boolean (value);
381 thiz->num_tile_rows = g_value_get_uint (value);
385 thiz->num_tile_cols = g_value_get_uint (value);
388 case PROP_MAX_SLICE_SIZE:
389 thiz->max_slice_size = g_value_get_uint (value);
393 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
396 GST_OBJECT_UNLOCK (thiz);
400 gst_msdkh265enc_get_property (GObject * object, guint prop_id, GValue * value,
403 GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
405 if (gst_msdkenc_get_common_property (object, prop_id, value, pspec))
408 GST_OBJECT_LOCK (thiz);
411 g_value_set_boolean (value, thiz->lowpower);
415 g_value_set_uint (value, thiz->num_tile_rows);
419 g_value_set_uint (value, thiz->num_tile_cols);
422 case PROP_MAX_SLICE_SIZE:
423 g_value_set_uint (value, thiz->max_slice_size);
427 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
430 GST_OBJECT_UNLOCK (thiz);
434 gst_msdkh265enc_need_reconfig (GstMsdkEnc * encoder, GstVideoCodecFrame * frame)
436 GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
438 return gst_msdkenc_get_roi_params (encoder, frame, h265enc->roi);
442 gst_msdkh265enc_set_extra_params (GstMsdkEnc * encoder,
443 GstVideoCodecFrame * frame)
445 GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
447 if (h265enc->roi[0].NumROI)
448 gst_msdkenc_add_extra_param (encoder, (mfxExtBuffer *) & h265enc->roi[0]);
452 gst_msdkh265enc_need_conversion (GstMsdkEnc * encoder, GstVideoInfo * info,
453 GstVideoFormat * out_format)
455 GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
457 switch (GST_VIDEO_INFO_FORMAT (info)) {
458 case GST_VIDEO_FORMAT_NV12:
459 case GST_VIDEO_FORMAT_P010_10LE:
460 case GST_VIDEO_FORMAT_VUYA:
461 #if (MFX_VERSION >= 1027)
462 case GST_VIDEO_FORMAT_Y410:
463 case GST_VIDEO_FORMAT_Y210:
467 case GST_VIDEO_FORMAT_YUY2:
468 #if (MFX_VERSION >= 1027)
469 if (encoder->codename >= MFX_PLATFORM_ICELAKE && !h265enc->lowpower)
473 if (GST_VIDEO_INFO_COMP_DEPTH (info, 0) == 10)
474 *out_format = GST_VIDEO_FORMAT_P010_10LE;
476 *out_format = GST_VIDEO_FORMAT_NV12;
482 gst_msdkh265enc_class_init (GstMsdkH265EncClass * klass)
484 GObjectClass *gobject_class;
485 GstElementClass *element_class;
486 GstVideoEncoderClass *videoencoder_class;
487 GstMsdkEncClass *encoder_class;
489 gobject_class = G_OBJECT_CLASS (klass);
490 element_class = GST_ELEMENT_CLASS (klass);
491 videoencoder_class = GST_VIDEO_ENCODER_CLASS (klass);
492 encoder_class = GST_MSDKENC_CLASS (klass);
494 gobject_class->finalize = gst_msdkh265enc_finalize;
495 gobject_class->set_property = gst_msdkh265enc_set_property;
496 gobject_class->get_property = gst_msdkh265enc_get_property;
498 videoencoder_class->pre_push = gst_msdkh265enc_pre_push;
500 encoder_class->set_format = gst_msdkh265enc_set_format;
501 encoder_class->configure = gst_msdkh265enc_configure;
502 encoder_class->set_src_caps = gst_msdkh265enc_set_src_caps;
503 encoder_class->need_reconfig = gst_msdkh265enc_need_reconfig;
504 encoder_class->set_extra_params = gst_msdkh265enc_set_extra_params;
505 encoder_class->need_conversion = gst_msdkh265enc_need_conversion;
507 gst_msdkenc_install_common_properties (encoder_class);
509 g_object_class_install_property (gobject_class, PROP_LOW_POWER,
510 g_param_spec_boolean ("low-power", "Low power", "Enable low power mode",
511 PROP_LOWPOWER_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
513 g_object_class_install_property (gobject_class, PROP_TILE_ROW,
514 g_param_spec_uint ("num-tile-rows", "number of rows for tiled encoding",
515 "number of rows for tiled encoding",
516 1, 8192, PROP_TILE_ROW_DEFAULT,
517 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
519 g_object_class_install_property (gobject_class, PROP_TILE_COL,
520 g_param_spec_uint ("num-tile-cols",
521 "number of columns for tiled encoding",
522 "number of columns for tiled encoding", 1, 8192,
523 PROP_TILE_COL_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
525 g_object_class_install_property (gobject_class, PROP_MAX_SLICE_SIZE,
526 g_param_spec_uint ("max-slice-size", "Max Slice Size",
527 "Maximum slice size in bytes (if enabled MSDK will ignore the control over num-slices)",
528 0, G_MAXUINT32, PROP_MAX_SLICE_SIZE_DEFAULT,
529 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
531 gst_element_class_set_static_metadata (element_class,
532 "Intel MSDK H265 encoder",
533 "Codec/Encoder/Video/Hardware",
534 "H265 video encoder based on Intel Media SDK",
535 "Josep Torra <jtorra@oblong.com>");
537 gst_element_class_add_static_pad_template (element_class, &sink_factory);
538 gst_element_class_add_static_pad_template (element_class, &src_factory);
542 gst_msdkh265enc_init (GstMsdkH265Enc * thiz)
544 GstMsdkEnc *msdk_enc = (GstMsdkEnc *) thiz;
545 thiz->lowpower = PROP_LOWPOWER_DEFAULT;
546 thiz->num_tile_rows = PROP_TILE_ROW_DEFAULT;
547 thiz->num_tile_cols = PROP_TILE_COL_DEFAULT;
548 thiz->max_slice_size = PROP_MAX_SLICE_SIZE_DEFAULT;
549 msdk_enc->num_extra_frames = 1;