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 "gstmsdkh264enc.h"
38 #include <gst/base/base.h>
39 #include <gst/pbutils/pbutils.h>
41 GST_DEBUG_CATEGORY_EXTERN (gst_msdkh264enc_debug);
42 #define GST_CAT_DEFAULT gst_msdkh264enc_debug
46 PROP_CABAC = GST_MSDKENC_PROP_MAX,
49 PROP_RC_LA_DOWNSAMPLING,
55 #define PROP_CABAC_DEFAULT TRUE
56 #define PROP_LOWPOWER_DEFAULT FALSE
57 #define PROP_FRAME_PACKING_DEFAULT -1
58 #define PROP_RC_LA_DOWNSAMPLING_DEFAULT MFX_LOOKAHEAD_DS_UNKNOWN
59 #define PROP_TRELLIS_DEFAULT _MFX_TRELLIS_NONE
60 #define PROP_MAX_SLICE_SIZE_DEFAULT 0
61 #define PROP_B_PYRAMID_DEFAULT FALSE
63 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
66 GST_STATIC_CAPS ("video/x-h264, "
67 "framerate = (fraction) [0/1, MAX], "
68 "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
69 "stream-format = (string) byte-stream , alignment = (string) au , "
70 "profile = (string) { high, main, baseline, constrained-baseline }")
74 gst_msdkh264enc_frame_packing_get_type (void)
76 static GType format_type = 0;
77 static const GEnumValue format_types[] = {
78 {GST_VIDEO_MULTIVIEW_FRAME_PACKING_NONE, "None (default)", "none"},
79 {GST_VIDEO_MULTIVIEW_FRAME_PACKING_SIDE_BY_SIDE, "Side by Side",
81 {GST_VIDEO_MULTIVIEW_FRAME_PACKING_TOP_BOTTOM, "Top Bottom", "top-bottom"},
87 g_enum_register_static ("GstMsdkH264EncFramePacking", format_types);
93 #define gst_msdkh264enc_parent_class parent_class
94 G_DEFINE_TYPE (GstMsdkH264Enc, gst_msdkh264enc, GST_TYPE_MSDKENC);
97 insert_frame_packing_sei (GstMsdkH264Enc * thiz, GstVideoCodecFrame * frame,
98 GstVideoMultiviewMode mode)
101 GstByteReader reader;
104 if (mode != GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE
105 && mode != GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM) {
106 GST_ERROR_OBJECT (thiz, "Unsupported multiview mode %d", mode);
110 GST_DEBUG ("Inserting SEI Frame Packing for multiview mode %d", mode);
112 gst_buffer_map (frame->output_buffer, &map, GST_MAP_READ);
113 gst_byte_reader_init (&reader, map.data, map.size);
116 gst_byte_reader_masked_scan_uint32 (&reader, 0xffffff00, 0x00000100,
117 0, gst_byte_reader_get_remaining (&reader))) != -1) {
121 gst_byte_reader_skip_unchecked (&reader, offset + 3);
122 if (!gst_byte_reader_get_uint8 (&reader, &type))
127 gst_byte_reader_masked_scan_uint32 (&reader, 0xffffff00, 0x00000100, 0,
128 gst_byte_reader_get_remaining (&reader));
130 offset2 = gst_byte_reader_get_remaining (&reader);
132 /* Slice, should really be an IDR slice (5) */
133 if (type >= 1 && type <= 5) {
134 GstBuffer *new_buffer;
136 static const guint8 sei_top_bottom[] =
137 { 0x00, 0x00, 0x01, 0x06, 0x2d, 0x07, 0x82, 0x01,
138 0x00, 0x00, 0x03, 0x00, 0x01, 0x20, 0x80
140 static const guint8 sei_side_by_side[] =
141 { 0x00, 0x00, 0x01, 0x06, 0x2d, 0x07, 0x81, 0x81,
142 0x00, 0x00, 0x03, 0x00, 0x01, 0x20, 0x80
147 if (mode == GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE) {
148 sei = sei_side_by_side;
149 sei_size = sizeof (sei_side_by_side);
151 sei = sei_top_bottom;
152 sei_size = sizeof (sei_top_bottom);
155 /* Create frame packing SEI
156 * FIXME: This assumes it does not exist in the stream, which is not
157 * going to be true anymore once this is fixed:
158 * https://github.com/Intel-Media-SDK/MediaSDK/issues/13
160 new_buffer = gst_buffer_new ();
162 /* Copy all metadata */
163 gst_buffer_copy_into (new_buffer, frame->output_buffer,
164 GST_BUFFER_COPY_METADATA, 0, -1);
166 /* Copy previous NALs */
167 gst_buffer_copy_into (new_buffer, frame->output_buffer,
168 GST_BUFFER_COPY_MEMORY, 0, gst_byte_reader_get_pos (&reader) - 4);
171 gst_memory_new_wrapped (0, g_memdup (sei, sei_size), sei_size, 0,
172 sei_size, NULL, g_free);
173 gst_buffer_append_memory (new_buffer, mem);
174 gst_buffer_copy_into (new_buffer, frame->output_buffer,
175 GST_BUFFER_COPY_MEMORY, gst_byte_reader_get_pos (&reader) - 4, -1);
177 gst_buffer_unmap (frame->output_buffer, &map);
178 gst_buffer_unref (frame->output_buffer);
179 frame->output_buffer = new_buffer;
185 gst_buffer_unmap (frame->output_buffer, &map);
189 gst_msdkh264enc_pre_push (GstVideoEncoder * encoder, GstVideoCodecFrame * frame)
191 GstMsdkH264Enc *thiz = GST_MSDKH264ENC (encoder);
193 if (GST_VIDEO_CODEC_FRAME_IS_SYNC_POINT (frame) &&
194 (thiz->frame_packing != GST_VIDEO_MULTIVIEW_MODE_NONE ||
195 ((GST_VIDEO_INFO_MULTIVIEW_MODE (&thiz->base.input_state->info) !=
196 GST_VIDEO_MULTIVIEW_MODE_NONE)
197 && GST_VIDEO_INFO_MULTIVIEW_MODE (&thiz->base.
198 input_state->info) != GST_VIDEO_MULTIVIEW_MODE_MONO))) {
199 insert_frame_packing_sei (thiz, frame,
200 thiz->frame_packing !=
201 GST_VIDEO_MULTIVIEW_MODE_NONE ? thiz->frame_packing :
202 GST_VIDEO_INFO_MULTIVIEW_MODE (&thiz->base.input_state->info));
209 gst_msdkh264enc_set_format (GstMsdkEnc * encoder)
211 GstMsdkH264Enc *thiz = GST_MSDKH264ENC (encoder);
212 GstCaps *template_caps;
213 GstCaps *allowed_caps = NULL;
218 template_caps = gst_static_pad_template_get_caps (&src_factory);
219 allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
221 /* If downstream has ANY caps let encoder decide profile and level */
222 if (allowed_caps == template_caps) {
223 GST_INFO_OBJECT (thiz,
224 "downstream has ANY caps, profile/level set to auto");
225 } else if (allowed_caps) {
227 const gchar *profile;
230 if (gst_caps_is_empty (allowed_caps)) {
231 gst_caps_unref (allowed_caps);
232 gst_caps_unref (template_caps);
236 allowed_caps = gst_caps_make_writable (allowed_caps);
237 allowed_caps = gst_caps_fixate (allowed_caps);
238 s = gst_caps_get_structure (allowed_caps, 0);
240 profile = gst_structure_get_string (s, "profile");
242 if (!strcmp (profile, "high")) {
243 thiz->profile = MFX_PROFILE_AVC_HIGH;
244 } else if (!strcmp (profile, "main")) {
245 thiz->profile = MFX_PROFILE_AVC_MAIN;
246 } else if (!strcmp (profile, "baseline")) {
247 thiz->profile = MFX_PROFILE_AVC_BASELINE;
248 } else if (!strcmp (profile, "constrained-baseline")) {
249 thiz->profile = MFX_PROFILE_AVC_CONSTRAINED_BASELINE;
251 g_assert_not_reached ();
255 level = gst_structure_get_string (s, "level");
257 thiz->level = gst_codec_utils_h264_get_level_idc (level);
260 gst_caps_unref (allowed_caps);
263 gst_caps_unref (template_caps);
269 gst_msdkh264enc_configure (GstMsdkEnc * encoder)
271 GstMsdkH264Enc *thiz = GST_MSDKH264ENC (encoder);
273 encoder->param.mfx.LowPower =
274 (thiz->lowpower ? MFX_CODINGOPTION_ON : MFX_CODINGOPTION_OFF);
275 encoder->param.mfx.CodecId = MFX_CODEC_AVC;
276 encoder->param.mfx.CodecProfile = thiz->profile;
277 encoder->param.mfx.CodecLevel = thiz->level;
279 thiz->option.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
280 thiz->option.Header.BufferSz = sizeof (thiz->option);
281 if (thiz->profile == MFX_PROFILE_AVC_CONSTRAINED_BASELINE ||
282 thiz->profile == MFX_PROFILE_AVC_BASELINE ||
283 thiz->profile == MFX_PROFILE_AVC_EXTENDED) {
284 thiz->option.CAVLC = MFX_CODINGOPTION_ON;
287 (thiz->cabac ? MFX_CODINGOPTION_OFF : MFX_CODINGOPTION_ON);
290 gst_msdkenc_add_extra_param (encoder, (mfxExtBuffer *) & thiz->option);
292 encoder->option2.Trellis = thiz->trellis ? thiz->trellis : MFX_TRELLIS_OFF;
293 encoder->option2.MaxSliceSize = thiz->max_slice_size;
294 if (encoder->rate_control == MFX_RATECONTROL_LA ||
295 encoder->rate_control == MFX_RATECONTROL_LA_HRD ||
296 encoder->rate_control == MFX_RATECONTROL_LA_ICQ)
297 encoder->option2.LookAheadDS = thiz->lookahead_ds;
299 if (thiz->b_pyramid) {
300 encoder->option2.BRefType = MFX_B_REF_PYRAMID;
301 /* Don't define Gop structure for B-pyramid, otherwise EncodeInit
302 * will throw Invalid param error */
303 encoder->param.mfx.GopRefDist = 0;
306 /* Enable Extended coding options */
307 gst_msdkenc_ensure_extended_coding_options (encoder);
312 static inline const gchar *
313 profile_to_string (gint profile)
316 case MFX_PROFILE_AVC_HIGH:
318 case MFX_PROFILE_AVC_MAIN:
320 case MFX_PROFILE_AVC_BASELINE:
322 case MFX_PROFILE_AVC_CONSTRAINED_BASELINE:
323 return "constrained-baseline";
331 static inline const gchar *
332 level_to_string (gint level)
335 case MFX_LEVEL_AVC_1:
337 case MFX_LEVEL_AVC_1b:
339 case MFX_LEVEL_AVC_11:
341 case MFX_LEVEL_AVC_12:
343 case MFX_LEVEL_AVC_13:
345 case MFX_LEVEL_AVC_2:
347 case MFX_LEVEL_AVC_21:
349 case MFX_LEVEL_AVC_22:
351 case MFX_LEVEL_AVC_3:
353 case MFX_LEVEL_AVC_31:
355 case MFX_LEVEL_AVC_32:
357 case MFX_LEVEL_AVC_4:
359 case MFX_LEVEL_AVC_41:
361 case MFX_LEVEL_AVC_42:
363 case MFX_LEVEL_AVC_5:
365 case MFX_LEVEL_AVC_51:
367 case MFX_LEVEL_AVC_52:
377 gst_msdkh264enc_set_src_caps (GstMsdkEnc * encoder)
380 GstStructure *structure;
381 const gchar *profile;
384 caps = gst_caps_new_empty_simple ("video/x-h264");
385 structure = gst_caps_get_structure (caps, 0);
387 gst_structure_set (structure, "stream-format", G_TYPE_STRING, "byte-stream",
390 gst_structure_set (structure, "alignment", G_TYPE_STRING, "au", NULL);
392 profile = profile_to_string (encoder->param.mfx.CodecProfile);
394 gst_structure_set (structure, "profile", G_TYPE_STRING, profile, NULL);
396 level = level_to_string (encoder->param.mfx.CodecLevel);
398 gst_structure_set (structure, "level", G_TYPE_STRING, level, NULL);
404 gst_msdkh264enc_set_property (GObject * object, guint prop_id,
405 const GValue * value, GParamSpec * pspec)
407 GstMsdkH264Enc *thiz = GST_MSDKH264ENC (object);
409 if (gst_msdkenc_set_common_property (object, prop_id, value, pspec))
412 GST_OBJECT_LOCK (thiz);
416 thiz->cabac = g_value_get_boolean (value);
419 thiz->lowpower = g_value_get_boolean (value);
421 case PROP_FRAME_PACKING:
422 thiz->frame_packing = g_value_get_enum (value);
424 case PROP_RC_LA_DOWNSAMPLING:
425 thiz->lookahead_ds = g_value_get_enum (value);
428 thiz->trellis = g_value_get_flags (value);
430 case PROP_MAX_SLICE_SIZE:
431 thiz->max_slice_size = g_value_get_uint (value);
434 thiz->b_pyramid = g_value_get_boolean (value);
437 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
440 GST_OBJECT_UNLOCK (thiz);
445 gst_msdkh264enc_get_property (GObject * object, guint prop_id, GValue * value,
448 GstMsdkH264Enc *thiz = GST_MSDKH264ENC (object);
450 if (gst_msdkenc_get_common_property (object, prop_id, value, pspec))
453 GST_OBJECT_LOCK (thiz);
456 g_value_set_boolean (value, thiz->cabac);
459 g_value_set_boolean (value, thiz->lowpower);
461 case PROP_FRAME_PACKING:
462 g_value_set_enum (value, thiz->frame_packing);
464 case PROP_RC_LA_DOWNSAMPLING:
465 g_value_set_enum (value, thiz->lookahead_ds);
468 g_value_set_flags (value, thiz->trellis);
470 case PROP_MAX_SLICE_SIZE:
471 g_value_set_uint (value, thiz->max_slice_size);
474 g_value_set_boolean (value, thiz->b_pyramid);
477 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
480 GST_OBJECT_UNLOCK (thiz);
484 gst_msdkh264enc_class_init (GstMsdkH264EncClass * klass)
486 GObjectClass *gobject_class;
487 GstElementClass *element_class;
488 GstVideoEncoderClass *videoencoder_class;
489 GstMsdkEncClass *encoder_class;
491 gobject_class = G_OBJECT_CLASS (klass);
492 element_class = GST_ELEMENT_CLASS (klass);
493 videoencoder_class = GST_VIDEO_ENCODER_CLASS (klass);
494 encoder_class = GST_MSDKENC_CLASS (klass);
496 gobject_class->set_property = gst_msdkh264enc_set_property;
497 gobject_class->get_property = gst_msdkh264enc_get_property;
499 videoencoder_class->pre_push = gst_msdkh264enc_pre_push;
501 encoder_class->set_format = gst_msdkh264enc_set_format;
502 encoder_class->configure = gst_msdkh264enc_configure;
503 encoder_class->set_src_caps = gst_msdkh264enc_set_src_caps;
505 gst_msdkenc_install_common_properties (encoder_class);
507 g_object_class_install_property (gobject_class, PROP_CABAC,
508 g_param_spec_boolean ("cabac", "CABAC", "Enable CABAC entropy coding",
509 PROP_CABAC_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
511 g_object_class_install_property (gobject_class, PROP_LOW_POWER,
512 g_param_spec_boolean ("low-power", "Low power", "Enable low power mode",
513 PROP_LOWPOWER_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
515 g_object_class_install_property (gobject_class, PROP_FRAME_PACKING,
516 g_param_spec_enum ("frame-packing", "Frame Packing",
517 "Set frame packing mode for Stereoscopic content",
518 gst_msdkh264enc_frame_packing_get_type (), PROP_FRAME_PACKING_DEFAULT,
519 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
521 g_object_class_install_property (gobject_class, PROP_RC_LA_DOWNSAMPLING,
522 g_param_spec_enum ("rc-lookahead-ds", "Look-ahead Downsampling",
523 "Down sampling mode in look ahead bitrate control",
524 gst_msdkenc_rc_lookahead_ds_get_type (),
525 PROP_RC_LA_DOWNSAMPLING_DEFAULT,
526 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
528 g_object_class_install_property (gobject_class, PROP_TRELLIS,
529 g_param_spec_flags ("trellis", "Trellis",
530 "Enable Trellis Quantization",
531 gst_msdkenc_trellis_quantization_get_type (), _MFX_TRELLIS_NONE,
532 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
534 g_object_class_install_property (gobject_class, PROP_MAX_SLICE_SIZE,
535 g_param_spec_uint ("max-slice-size", "Max Slice Size",
536 "Maximum slice size in bytes (if enabled MSDK will ignore the control over num-slices)",
537 0, G_MAXUINT32, PROP_MAX_SLICE_SIZE_DEFAULT,
538 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
540 g_object_class_install_property (gobject_class, PROP_B_PYRAMID,
541 g_param_spec_boolean ("b-pyramid", "B-pyramid",
542 "Enable B-Pyramid Reference structure", FALSE,
543 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
545 gst_element_class_set_static_metadata (element_class,
546 "Intel MSDK H264 encoder", "Codec/Encoder/Video/Hardware",
547 "H264 video encoder based on Intel Media SDK",
548 "Josep Torra <jtorra@oblong.com>");
549 gst_element_class_add_static_pad_template (element_class, &src_factory);
553 gst_msdkh264enc_init (GstMsdkH264Enc * thiz)
555 thiz->cabac = PROP_CABAC_DEFAULT;
556 thiz->lowpower = PROP_LOWPOWER_DEFAULT;
557 thiz->frame_packing = PROP_FRAME_PACKING_DEFAULT;
558 thiz->lookahead_ds = PROP_RC_LA_DOWNSAMPLING_DEFAULT;
559 thiz->trellis = PROP_TRELLIS_DEFAULT;
560 thiz->max_slice_size = PROP_MAX_SLICE_SIZE_DEFAULT;
561 thiz->b_pyramid = PROP_B_PYRAMID_DEFAULT;