2 * Copyright (C) 2021 Seungha Yang <seungha@centricular.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include "gstqsvh264enc.h"
25 #include <gst/base/gstbytewriter.h>
26 #include <gst/codecparsers/gsth264parser.h>
33 #include <gst/d3d11/gstd3d11.h>
35 #include <gst/va/gstva.h>
38 GST_DEBUG_CATEGORY_EXTERN (gst_qsv_h264_enc_debug);
39 #define GST_CAT_DEFAULT gst_qsv_h264_enc_debug
43 GST_QSV_H264_ENC_SEI_INSERT,
44 GST_QSV_H264_ENC_SEI_INSERT_AND_DROP,
45 GST_QSV_H264_ENC_SEI_DISABLED,
46 } GstQsvH264EncSeiInsertMode;
48 #define GST_TYPE_QSV_H264_ENC_SEI_INSERT_MODE (gst_qsv_h264_enc_sei_insert_mode_get_type ())
50 gst_qsv_h264_enc_sei_insert_mode_get_type (void)
52 static GType sei_insert_mode_type = 0;
53 static const GEnumValue insert_modes[] = {
54 {GST_QSV_H264_ENC_SEI_INSERT, "Insert SEI", "insert"},
55 {GST_QSV_H264_ENC_SEI_INSERT_AND_DROP,
56 "Insert SEI and remove corresponding meta from output buffer",
58 {GST_QSV_H264_ENC_SEI_DISABLED, "Disable SEI insertion", "disabled"},
62 if (g_once_init_enter (&sei_insert_mode_type)) {
64 g_enum_register_static ("GstQsvH264EncSeiInsertMode", insert_modes);
65 g_once_init_leave (&sei_insert_mode_type, type);
68 return sei_insert_mode_type;
71 #define GST_TYPE_QSV_H264_ENC_RATE_CONTROL (gst_qsv_h264_enc_rate_control_get_type ())
73 gst_qsv_h264_enc_rate_control_get_type (void)
75 static GType rate_control_type = 0;
76 static const GEnumValue rate_controls[] = {
77 {MFX_RATECONTROL_CBR, "Constant Bitrate", "cbr"},
78 {MFX_RATECONTROL_VBR, "Variable Bitrate", "vbr"},
79 {MFX_RATECONTROL_CQP, "Constant Quantizer", "cqp"},
80 {MFX_RATECONTROL_AVBR, "Average Variable Bitrate", "avbr"},
81 {MFX_RATECONTROL_LA, "VBR with look ahead (Non HRD compliant)", "la_vbr"},
82 {MFX_RATECONTROL_ICQ, "Intelligent CQP", "icq"},
83 {MFX_RATECONTROL_VCM, "Video Conferencing Mode (Non HRD compliant)", "vcm"},
84 {MFX_RATECONTROL_LA_ICQ, "Intelligent CQP with LA (Non HRD compliant)",
86 {MFX_RATECONTROL_LA_HRD, "HRD compliant LA", "la_hrd"},
87 {MFX_RATECONTROL_QVBR, "VBR with CQP", "qvbr"},
91 if (g_once_init_enter (&rate_control_type)) {
93 g_enum_register_static ("GstQsvH264EncRateControl", rate_controls);
94 g_once_init_leave (&rate_control_type, type);
97 return rate_control_type;
100 #define GST_TYPE_QSV_H264_ENC_RC_LOOKAHEAD_DS (gst_qsv_h264_enc_rc_lookahead_ds_get_type ())
102 gst_qsv_h264_enc_rc_lookahead_ds_get_type (void)
104 static GType rc_lookahead_ds_type = 0;
105 static const GEnumValue rc_lookahead_ds[] = {
106 {MFX_LOOKAHEAD_DS_UNKNOWN, "Unknown", "unknown"},
107 {MFX_LOOKAHEAD_DS_OFF, "Do not use down sampling", "off"},
108 {MFX_LOOKAHEAD_DS_2x,
109 "Down sample frames two times before estimation", "2x"},
110 {MFX_LOOKAHEAD_DS_4x,
111 "Down sample frames four times before estimation", "4x"},
112 {0, nullptr, nullptr}
115 if (g_once_init_enter (&rc_lookahead_ds_type)) {
117 g_enum_register_static ("GstQsvH264EncRCLookAheadDS", rc_lookahead_ds);
118 g_once_init_leave (&rc_lookahead_ds_type, type);
121 return rc_lookahead_ds_type;
147 PROP_RC_LOOKAHEAD_DS,
149 PROP_AVBR_CONVERGENCE,
152 PROP_DISABLE_HRD_CONFORMANCE,
156 #define DEFAULT_CABAC MFX_CODINGOPTION_UNKNOWN
158 #define DEFAULT_GOP_SIZE 30
159 #define DEFAULT_IDR_INTERVAL 0
160 #define DEFAULT_B_FRAMES 0
161 #define DEFAULT_REF_FRAMES 2
162 #define DEFAULT_BITRATE 2000
163 #define DEFAULT_MAX_BITRATE 0
164 #define DEFAULT_RATE_CONTROL MFX_RATECONTROL_VBR
165 #define DEFAULT_RC_LOOKAHEAD 10
166 #define DEFAULT_RC_LOOKAHEAD_DS MFX_LOOKAHEAD_DS_UNKNOWN
167 #define DEFAULT_AVBR_ACCURACY 0
168 #define DEFAULT_AVBR_CONVERGENCE 0
169 #define DEFAULT_IQC_QUALITY 0
170 #define DEFAULT_QVBR_QUALITY 0
171 #define DEFAULT_DISABLE_HRD_CONFORMANCE FALSE
172 #define DEFAULT_CC_INSERT GST_QSV_H264_ENC_SEI_INSERT
174 typedef struct _GstQsvH264EncClassData
181 } GstQsvH264EncClassData;
183 typedef struct _GstQsvH264Enc
185 GstQsvEncoder parent;
187 mfxExtVideoSignalInfo signal_info;
188 mfxExtCodingOption option;
189 mfxExtCodingOption2 option2;
190 mfxExtCodingOption3 option3;
193 GstH264NalParser *parser;
198 /* protected by prop_lock */
199 gboolean bitrate_updated;
200 gboolean property_updated;
221 mfxU16 rc_lookahead_ds;
223 guint avbr_convergence;
226 gboolean disable_hrd_conformance;
227 GstQsvH264EncSeiInsertMode cc_insert;
230 typedef struct _GstQsvH264EncClass
232 GstQsvEncoderClass parent_class;
233 } GstQsvH264EncClass;
235 static GstElementClass *parent_class = nullptr;
237 #define GST_QSV_H264_ENC(object) ((GstQsvH264Enc *) (object))
238 #define GST_QSV_H264_ENC_GET_CLASS(object) \
239 (G_TYPE_INSTANCE_GET_CLASS ((object),G_TYPE_FROM_INSTANCE (object),GstQsvH264EncClass))
241 static void gst_qsv_h264_enc_finalize (GObject * object);
242 static void gst_qsv_h264_enc_set_property (GObject * object, guint prop_id,
243 const GValue * value, GParamSpec * pspec);
244 static void gst_qsv_h264_enc_get_property (GObject * object, guint prop_id,
245 GValue * value, GParamSpec * pspec);
247 static gboolean gst_qsv_h264_enc_start (GstVideoEncoder * encoder);
248 static gboolean gst_qsv_h264_enc_transform_meta (GstVideoEncoder * encoder,
249 GstVideoCodecFrame * frame, GstMeta * meta);
250 static GstCaps *gst_qsv_h264_enc_getcaps (GstVideoEncoder * encoder,
253 static gboolean gst_qsv_h264_enc_set_format (GstQsvEncoder * encoder,
254 GstVideoCodecState * state, mfxVideoParam * param,
255 GPtrArray * extra_params);
256 static gboolean gst_qsv_h264_enc_set_output_state (GstQsvEncoder * encoder,
257 GstVideoCodecState * state, mfxSession session);
258 static gboolean gst_qsv_h264_enc_attach_payload (GstQsvEncoder * encoder,
259 GstVideoCodecFrame * frame, GPtrArray * payload);
260 static GstBuffer *gst_qsv_h264_enc_create_output_buffer (GstQsvEncoder *
261 encoder, mfxBitstream * bitstream);
262 static GstQsvEncoderReconfigure
263 gst_qsv_h264_enc_check_reconfigure (GstQsvEncoder * encoder, mfxSession session,
264 mfxVideoParam * param, GPtrArray * extra_params);
267 gst_qsv_h264_enc_class_init (GstQsvH264EncClass * klass, gpointer data)
269 GObjectClass *object_class = G_OBJECT_CLASS (klass);
270 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
271 GstVideoEncoderClass *encoder_class = GST_VIDEO_ENCODER_CLASS (klass);
272 GstQsvEncoderClass *qsvenc_class = GST_QSV_ENCODER_CLASS (klass);
273 GstQsvH264EncClassData *cdata = (GstQsvH264EncClassData *) data;
275 qsvenc_class->codec_id = MFX_CODEC_AVC;
276 qsvenc_class->impl_index = cdata->impl_index;
277 qsvenc_class->adapter_luid = cdata->adapter_luid;
278 qsvenc_class->display_path = cdata->display_path;
280 object_class->finalize = gst_qsv_h264_enc_finalize;
281 object_class->set_property = gst_qsv_h264_enc_set_property;
282 object_class->get_property = gst_qsv_h264_enc_get_property;
285 g_object_class_install_property (object_class, PROP_ADAPTER_LUID,
286 g_param_spec_int64 ("adapter-luid", "Adapter LUID",
287 "DXGI Adapter LUID (Locally Unique Identifier) of created device",
288 G_MININT64, G_MAXINT64, qsvenc_class->adapter_luid,
289 (GParamFlags) (GST_PARAM_CONDITIONALLY_AVAILABLE | G_PARAM_READABLE |
290 G_PARAM_STATIC_STRINGS)));
292 g_object_class_install_property (object_class, PROP_DEVICE_PATH,
293 g_param_spec_string ("device-path", "Device Path",
294 "DRM device path", cdata->display_path,
295 (GParamFlags) (GST_PARAM_CONDITIONALLY_AVAILABLE |
296 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
299 g_object_class_install_property (object_class, PROP_CABAC,
300 g_param_spec_enum ("cabac", "Cabac", "Enables CABAC entropy coding",
301 GST_TYPE_QSV_CODING_OPTION, DEFAULT_CABAC,
302 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
303 g_object_class_install_property (object_class, PROP_MIN_QP_I,
304 g_param_spec_uint ("min-qp-i", "Min QP I",
305 "Minimum allowed QP value for I-frame types (0: default)",
306 0, 51, DEFAULT_QP, (GParamFlags)
307 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
308 g_object_class_install_property (object_class, PROP_MIN_QP_P,
309 g_param_spec_uint ("min-qp-p", "Min QP P",
310 "Minimum allowed QP value for P-frame types (0: default)",
311 0, 51, DEFAULT_QP, (GParamFlags)
312 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
313 g_object_class_install_property (object_class, PROP_MIN_QP_B,
314 g_param_spec_uint ("min-qp-b", "Min QP B",
315 "Minimum allowed QP value for B-frame types (0: default)",
316 0, 51, DEFAULT_QP, (GParamFlags)
317 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
318 g_object_class_install_property (object_class, PROP_MAX_QP_I,
319 g_param_spec_uint ("max-qp-i", "Max QP I",
320 "Maximum allowed QP value for I-frame types (0: default)",
321 0, 51, DEFAULT_QP, (GParamFlags)
322 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
323 g_object_class_install_property (object_class, PROP_MAX_QP_P,
324 g_param_spec_uint ("max-qp-p", "Max QP P",
325 "Maximum allowed QP value for P-frame types (0: default)",
326 0, 51, DEFAULT_QP, (GParamFlags)
327 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
328 g_object_class_install_property (object_class, PROP_MAX_QP_B,
329 g_param_spec_uint ("max-qp-b", "Max QP B",
330 "Maximum allowed QP value for B-frame types (0: default)",
331 0, 51, DEFAULT_QP, (GParamFlags)
332 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
333 g_object_class_install_property (object_class, PROP_QP_I,
334 g_param_spec_uint ("qp-i", "QP I",
335 "Constant quantizer for I frames (0: default)",
336 0, 51, DEFAULT_QP, (GParamFlags)
337 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
338 g_object_class_install_property (object_class, PROP_QP_P,
339 g_param_spec_uint ("qp-p", "QP P",
340 "Constant quantizer for P frames (0: default)",
341 0, 51, DEFAULT_QP, (GParamFlags)
342 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
343 g_object_class_install_property (object_class, PROP_QP_B,
344 g_param_spec_uint ("qp-b", "QP B",
345 "Constant quantizer for B frames (0: default)",
346 0, 51, DEFAULT_QP, (GParamFlags)
347 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
348 g_object_class_install_property (object_class, PROP_GOP_SIZE,
349 g_param_spec_uint ("gop-size", "GOP Size",
350 "Number of pictures within a GOP (0: unspecified)",
351 0, G_MAXUSHORT, DEFAULT_GOP_SIZE, (GParamFlags)
352 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
353 g_object_class_install_property (object_class, PROP_IDR_INTERVAL,
354 g_param_spec_uint ("idr-interval", "IDR interval",
355 "IDR-frame interval in terms of I-frames. "
356 "0: every I-frame is an IDR frame, "
357 "N: \"N\" I-frames are inserted between IDR-frames",
358 0, G_MAXUSHORT, DEFAULT_IDR_INTERVAL, (GParamFlags)
359 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
360 g_object_class_install_property (object_class, PROP_B_FRAMES,
361 g_param_spec_uint ("b-frames", "B Frames",
362 "Number of B frames between I and P frames",
363 0, G_MAXUSHORT, DEFAULT_B_FRAMES, (GParamFlags)
364 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
365 g_object_class_install_property (object_class, PROP_REF_FRAMES,
366 g_param_spec_uint ("ref-frames", "Reference Frames",
367 "Number of reference frames (0: unspecified)",
368 0, 16, DEFAULT_REF_FRAMES, (GParamFlags)
369 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
370 g_object_class_install_property (object_class, PROP_BITRATE,
371 g_param_spec_uint ("bitrate", "Bitrate",
372 "Target bitrate in kbit/sec, Ignored when selected rate-control mode "
373 "is constant QP variants (i.e., \"cqp\", \"icq\", and \"la_icq\")",
374 0, G_MAXINT, DEFAULT_BITRATE, (GParamFlags)
375 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
376 g_object_class_install_property (object_class, PROP_MAX_BITRATE,
377 g_param_spec_uint ("max-bitrate", "Max Bitrate",
378 "Maximum bitrate in kbit/sec, Ignored when selected rate-control mode "
379 "is constant QP variants (i.e., \"cqp\", \"icq\", and \"la_icq\")",
380 0, G_MAXINT, DEFAULT_MAX_BITRATE, (GParamFlags)
381 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
382 g_object_class_install_property (object_class, PROP_RATE_CONTROL,
383 g_param_spec_enum ("rate-control", "Rate Control",
384 "Rate Control Method", GST_TYPE_QSV_H264_ENC_RATE_CONTROL,
385 DEFAULT_RATE_CONTROL,
386 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
387 g_object_class_install_property (object_class, PROP_RC_LOOKAHEAD,
388 g_param_spec_uint ("rc-lookahead", "Rate Control Look-ahead",
389 "Number of frames to look ahead for Rate Control, used for "
390 "\"la_vbr\", \"la_icq\", and \"la_hrd\" rate-control modes",
391 10, 100, DEFAULT_RC_LOOKAHEAD, (GParamFlags)
392 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
393 g_object_class_install_property (object_class, PROP_RC_LOOKAHEAD_DS,
394 g_param_spec_enum ("rc-lookahead-ds",
395 "Rate Control Look-ahead Downsampling",
396 "Downsampling method in look-ahead rate control",
397 GST_TYPE_QSV_H264_ENC_RC_LOOKAHEAD_DS, DEFAULT_RC_LOOKAHEAD_DS,
398 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
399 g_object_class_install_property (object_class, PROP_AVBR_ACCURACY,
400 g_param_spec_uint ("avbr-accuracy", "AVBR Accuracy",
401 "AVBR Accuracy in the unit of tenth of percent",
402 0, G_MAXUINT16, DEFAULT_AVBR_ACCURACY, (GParamFlags)
403 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
404 g_object_class_install_property (object_class, PROP_AVBR_CONVERGENCE,
405 g_param_spec_uint ("avbr-convergence", "AVBR Convergence",
406 "AVBR Convergence in the unit of 100 frames",
407 0, G_MAXUINT16, DEFAULT_AVBR_ACCURACY, (GParamFlags)
408 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
409 g_object_class_install_property (object_class, PROP_ICQ_QUALITY,
410 g_param_spec_uint ("icq-quality", "ICQ Quality",
411 "Intelligent Constant Quality for \"icq\" rate-control (0: default)",
412 0, 51, DEFAULT_IQC_QUALITY, (GParamFlags)
413 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
414 g_object_class_install_property (object_class, PROP_QVBR_QUALITY,
415 g_param_spec_uint ("qvbr-quality", "QVBR Quality",
416 "Quality level used for \"qvbr\" rate-control mode (0: default)",
417 0, 51, DEFAULT_QVBR_QUALITY, (GParamFlags)
418 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
419 g_object_class_install_property (object_class, PROP_DISABLE_HRD_CONFORMANCE,
420 g_param_spec_boolean ("disable-hrd-conformance",
421 "Disable HRD Conformance", "Allow NAL HRD non-conformant stream",
422 DEFAULT_DISABLE_HRD_CONFORMANCE,
423 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
424 g_object_class_install_property (object_class, PROP_CC_INSERT,
425 g_param_spec_enum ("cc-insert", "Closed Caption Insert",
426 "Closed Caption Insert mode. "
427 "Only CEA-708 RAW format is supported for now",
428 GST_TYPE_QSV_H264_ENC_SEI_INSERT_MODE, DEFAULT_CC_INSERT,
429 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
431 parent_class = (GstElementClass *) g_type_class_peek_parent (klass);
432 gst_element_class_set_static_metadata (element_class,
433 "Intel Quick Sync Video H.264 Encoder",
434 "Codec/Encoder/Video/Hardware",
435 "Intel Quick Sync Video H.264 Encoder",
436 "Seungha Yang <seungha@centricular.com>");
438 gst_element_class_add_pad_template (element_class,
439 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
441 gst_element_class_add_pad_template (element_class,
442 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
445 encoder_class->start = GST_DEBUG_FUNCPTR (gst_qsv_h264_enc_start);
446 encoder_class->transform_meta =
447 GST_DEBUG_FUNCPTR (gst_qsv_h264_enc_transform_meta);
448 encoder_class->getcaps = GST_DEBUG_FUNCPTR (gst_qsv_h264_enc_getcaps);
450 qsvenc_class->set_format = GST_DEBUG_FUNCPTR (gst_qsv_h264_enc_set_format);
451 qsvenc_class->set_output_state =
452 GST_DEBUG_FUNCPTR (gst_qsv_h264_enc_set_output_state);
453 qsvenc_class->attach_payload =
454 GST_DEBUG_FUNCPTR (gst_qsv_h264_enc_attach_payload);
455 qsvenc_class->create_output_buffer =
456 GST_DEBUG_FUNCPTR (gst_qsv_h264_enc_create_output_buffer);
457 qsvenc_class->check_reconfigure =
458 GST_DEBUG_FUNCPTR (gst_qsv_h264_enc_check_reconfigure);
460 gst_caps_unref (cdata->sink_caps);
461 gst_caps_unref (cdata->src_caps);
466 gst_qsv_h264_enc_init (GstQsvH264Enc * self)
468 self->cabac = DEFAULT_CABAC;
469 self->min_qp_i = DEFAULT_QP;
470 self->min_qp_p = DEFAULT_QP;
471 self->min_qp_b = DEFAULT_QP;
472 self->max_qp_i = DEFAULT_QP;
473 self->max_qp_p = DEFAULT_QP;
474 self->max_qp_p = DEFAULT_QP;
475 self->qp_i = DEFAULT_QP;
476 self->qp_p = DEFAULT_QP;
477 self->qp_b = DEFAULT_QP;
478 self->gop_size = DEFAULT_GOP_SIZE;
479 self->idr_interval = DEFAULT_IDR_INTERVAL;
480 self->bframes = DEFAULT_B_FRAMES;
481 self->ref_frames = DEFAULT_REF_FRAMES;
482 self->bitrate = DEFAULT_BITRATE;
483 self->max_bitrate = DEFAULT_MAX_BITRATE;
484 self->rate_control = DEFAULT_RATE_CONTROL;
485 self->rc_lookahead = DEFAULT_RC_LOOKAHEAD;
486 self->rc_lookahead_ds = DEFAULT_RC_LOOKAHEAD_DS;
487 self->avbr_accuracy = DEFAULT_AVBR_ACCURACY;
488 self->avbr_convergence = DEFAULT_AVBR_CONVERGENCE;
489 self->icq_quality = DEFAULT_IQC_QUALITY;
490 self->qvbr_quality = DEFAULT_QVBR_QUALITY;
491 self->disable_hrd_conformance = DEFAULT_DISABLE_HRD_CONFORMANCE;
492 self->cc_insert = DEFAULT_CC_INSERT;
494 g_mutex_init (&self->prop_lock);
496 self->parser = gst_h264_nal_parser_new ();
500 gst_qsv_h264_enc_finalize (GObject * object)
502 GstQsvH264Enc *self = GST_QSV_H264_ENC (object);
504 g_mutex_clear (&self->prop_lock);
505 gst_h264_nal_parser_free (self->parser);
507 G_OBJECT_CLASS (parent_class)->finalize (object);
511 gst_qsv_h264_enc_check_update_uint (GstQsvH264Enc * self, guint * old_val,
512 guint new_val, gboolean is_bitrate_param)
514 if (*old_val == new_val)
517 g_mutex_lock (&self->prop_lock);
519 if (is_bitrate_param)
520 self->bitrate_updated = TRUE;
522 self->property_updated = TRUE;
523 g_mutex_unlock (&self->prop_lock);
527 gst_qsv_h264_enc_check_update_enum (GstQsvH264Enc * self, mfxU16 * old_val,
530 if (*old_val == (mfxU16) new_val)
533 g_mutex_lock (&self->prop_lock);
534 *old_val = (mfxU16) new_val;
535 self->property_updated = TRUE;
536 g_mutex_unlock (&self->prop_lock);
540 gst_qsv_h264_enc_check_update_boolean (GstQsvH264Enc * self, gboolean * old_val,
543 if (*old_val == new_val)
546 g_mutex_lock (&self->prop_lock);
548 self->property_updated = TRUE;
549 g_mutex_unlock (&self->prop_lock);
553 gst_qsv_h264_enc_set_property (GObject * object, guint prop_id,
554 const GValue * value, GParamSpec * pspec)
556 GstQsvH264Enc *self = GST_QSV_H264_ENC (object);
560 gst_qsv_h264_enc_check_update_enum (self, &self->cabac,
561 g_value_get_enum (value));
563 /* MIN/MAX QP change requires new sequence */
565 gst_qsv_h264_enc_check_update_uint (self, &self->min_qp_i,
566 g_value_get_uint (value), FALSE);
569 gst_qsv_h264_enc_check_update_uint (self, &self->min_qp_p,
570 g_value_get_uint (value), FALSE);
573 gst_qsv_h264_enc_check_update_uint (self, &self->min_qp_b,
574 g_value_get_uint (value), FALSE);
577 gst_qsv_h264_enc_check_update_uint (self, &self->max_qp_i,
578 g_value_get_uint (value), FALSE);
581 gst_qsv_h264_enc_check_update_uint (self, &self->max_qp_p,
582 g_value_get_uint (value), FALSE);
585 gst_qsv_h264_enc_check_update_uint (self, &self->max_qp_b,
586 g_value_get_uint (value), FALSE);
589 gst_qsv_h264_enc_check_update_uint (self, &self->qp_i,
590 g_value_get_uint (value), TRUE);
593 gst_qsv_h264_enc_check_update_uint (self, &self->qp_p,
594 g_value_get_uint (value), TRUE);
597 gst_qsv_h264_enc_check_update_uint (self, &self->qp_b,
598 g_value_get_uint (value), TRUE);
601 gst_qsv_h264_enc_check_update_uint (self, &self->gop_size,
602 g_value_get_uint (value), FALSE);
604 case PROP_IDR_INTERVAL:
605 gst_qsv_h264_enc_check_update_uint (self, &self->idr_interval,
606 g_value_get_uint (value), FALSE);
609 gst_qsv_h264_enc_check_update_uint (self, &self->bframes,
610 g_value_get_uint (value), FALSE);
612 case PROP_REF_FRAMES:
613 gst_qsv_h264_enc_check_update_uint (self, &self->ref_frames,
614 g_value_get_uint (value), FALSE);
617 gst_qsv_h264_enc_check_update_uint (self, &self->bitrate,
618 g_value_get_uint (value), TRUE);
620 case PROP_MAX_BITRATE:
621 gst_qsv_h264_enc_check_update_uint (self, &self->max_bitrate,
622 g_value_get_uint (value), TRUE);
624 case PROP_RATE_CONTROL:
625 gst_qsv_h264_enc_check_update_enum (self, &self->rate_control,
626 g_value_get_enum (value));
628 case PROP_RC_LOOKAHEAD:
629 gst_qsv_h264_enc_check_update_uint (self, &self->rc_lookahead,
630 g_value_get_uint (value), FALSE);
632 case PROP_RC_LOOKAHEAD_DS:
633 gst_qsv_h264_enc_check_update_enum (self, &self->rc_lookahead_ds,
634 g_value_get_enum (value));
636 case PROP_AVBR_ACCURACY:
637 gst_qsv_h264_enc_check_update_uint (self, &self->avbr_accuracy,
638 g_value_get_uint (value), FALSE);
640 case PROP_AVBR_CONVERGENCE:
641 gst_qsv_h264_enc_check_update_uint (self, &self->avbr_convergence,
642 g_value_get_uint (value), FALSE);
644 case PROP_ICQ_QUALITY:
645 gst_qsv_h264_enc_check_update_uint (self, &self->icq_quality,
646 g_value_get_uint (value), FALSE);
648 case PROP_QVBR_QUALITY:
649 gst_qsv_h264_enc_check_update_uint (self, &self->qvbr_quality,
650 g_value_get_uint (value), FALSE);
652 case PROP_DISABLE_HRD_CONFORMANCE:
653 gst_qsv_h264_enc_check_update_boolean (self,
654 &self->disable_hrd_conformance, g_value_get_boolean (value));
657 /* This property is unrelated to encoder-reset */
658 self->cc_insert = (GstQsvH264EncSeiInsertMode) g_value_get_enum (value);
661 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
667 gst_qsv_h264_enc_get_property (GObject * object, guint prop_id, GValue * value,
670 GstQsvH264Enc *self = GST_QSV_H264_ENC (object);
671 GstQsvEncoderClass *klass = GST_QSV_ENCODER_GET_CLASS (self);
674 case PROP_ADAPTER_LUID:
675 g_value_set_int64 (value, klass->adapter_luid);
677 case PROP_DEVICE_PATH:
678 g_value_set_string (value, klass->display_path);
681 g_value_set_enum (value, self->cabac);
684 g_value_set_uint (value, self->min_qp_i);
687 g_value_set_uint (value, self->min_qp_p);
690 g_value_set_uint (value, self->min_qp_b);
693 g_value_set_uint (value, self->max_qp_i);
696 g_value_set_uint (value, self->max_qp_p);
699 g_value_set_uint (value, self->max_qp_b);
702 g_value_set_uint (value, self->qp_i);
705 g_value_set_uint (value, self->qp_p);
708 g_value_set_uint (value, self->qp_b);
711 g_value_set_uint (value, self->gop_size);
713 case PROP_IDR_INTERVAL:
714 g_value_set_uint (value, self->idr_interval);
717 g_value_set_uint (value, self->bframes);
719 case PROP_REF_FRAMES:
720 g_value_set_uint (value, self->ref_frames);
723 g_value_set_uint (value, self->bitrate);
725 case PROP_MAX_BITRATE:
726 g_value_set_uint (value, self->max_bitrate);
728 case PROP_RATE_CONTROL:
729 g_value_set_enum (value, self->rate_control);
731 case PROP_RC_LOOKAHEAD:
732 g_value_set_uint (value, self->rc_lookahead);
734 case PROP_RC_LOOKAHEAD_DS:
735 g_value_set_enum (value, self->rc_lookahead_ds);
737 case PROP_AVBR_ACCURACY:
738 g_value_set_uint (value, self->avbr_accuracy);
740 case PROP_AVBR_CONVERGENCE:
741 g_value_set_uint (value, self->avbr_convergence);
743 case PROP_ICQ_QUALITY:
744 g_value_set_uint (value, self->icq_quality);
746 case PROP_QVBR_QUALITY:
747 g_value_set_uint (value, self->qvbr_quality);
750 g_value_set_enum (value, self->cc_insert);
752 case PROP_DISABLE_HRD_CONFORMANCE:
753 g_value_set_boolean (value, self->disable_hrd_conformance);
756 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
762 gst_qsv_h264_enc_start (GstVideoEncoder * encoder)
764 /* To avoid negative DTS when B frame is enabled */
765 gst_video_encoder_set_min_pts (encoder, GST_SECOND * 60 * 60 * 1000);
771 gst_qsv_h264_enc_transform_meta (GstVideoEncoder * encoder,
772 GstVideoCodecFrame * frame, GstMeta * meta)
774 GstQsvH264Enc *self = GST_QSV_H264_ENC (encoder);
775 GstVideoCaptionMeta *cc_meta;
777 /* We need to handle only case CC meta should be dropped */
778 if (self->cc_insert != GST_QSV_H264_ENC_SEI_INSERT_AND_DROP)
781 if (meta->info->api != GST_VIDEO_CAPTION_META_API_TYPE)
784 cc_meta = (GstVideoCaptionMeta *) meta;
785 if (cc_meta->caption_type != GST_VIDEO_CAPTION_TYPE_CEA708_RAW)
788 /* Don't copy this meta into output buffer */
792 return GST_VIDEO_ENCODER_CLASS (parent_class)->transform_meta (encoder,
797 gst_qsv_h264_enc_getcaps (GstVideoEncoder * encoder, GstCaps * filter)
799 GstQsvH264Enc *self = GST_QSV_H264_ENC (encoder);
800 GstCaps *allowed_caps;
801 GstCaps *template_caps;
802 GstCaps *supported_caps;
803 std::set < std::string > downstream_profiles;
805 allowed_caps = gst_pad_get_allowed_caps (encoder->srcpad);
807 /* Shouldn't be any or empty though, just return template caps in this case */
808 if (!allowed_caps || gst_caps_is_empty (allowed_caps) ||
809 gst_caps_is_any (allowed_caps)) {
810 gst_clear_caps (&allowed_caps);
812 return gst_video_encoder_proxy_getcaps (encoder, nullptr, filter);
815 /* Check if downstream specified profile explicitly, then filter out
816 * incompatible interlaced field */
817 for (guint i = 0; i < gst_caps_get_size (allowed_caps); i++) {
818 const GValue *profile_value;
819 const gchar *profile;
822 s = gst_caps_get_structure (allowed_caps, i);
823 profile_value = gst_structure_get_value (s, "profile");
827 if (GST_VALUE_HOLDS_LIST (profile_value)) {
828 for (guint j = 0; j < gst_value_list_get_size (profile_value); j++) {
829 const GValue *p = gst_value_list_get_value (profile_value, j);
831 if (!G_VALUE_HOLDS_STRING (p))
834 profile = g_value_get_string (p);
836 downstream_profiles.insert (profile);
839 } else if (G_VALUE_HOLDS_STRING (profile_value)) {
840 profile = g_value_get_string (profile_value);
842 downstream_profiles.insert (profile);
846 GST_DEBUG_OBJECT (self, "Downstream specified %" G_GSIZE_FORMAT " profiles",
847 downstream_profiles.size ());
849 /* Caps returned by gst_pad_get_allowed_caps() should hold profile field
851 if (downstream_profiles.size () == 0) {
852 GST_WARNING_OBJECT (self,
853 "Allowed caps holds no profile field %" GST_PTR_FORMAT, allowed_caps);
855 gst_clear_caps (&allowed_caps);
857 return gst_video_encoder_proxy_getcaps (encoder, nullptr, filter);
860 gst_clear_caps (&allowed_caps);
862 /* Profile allows interlaced? */
864 gboolean can_support_interlaced = FALSE;
865 for (const auto &iter: downstream_profiles) {
866 if (iter == "high" || iter == "main") {
867 can_support_interlaced = TRUE;
873 GST_DEBUG_OBJECT (self, "Downstream %s support interlaced format",
874 can_support_interlaced ? "can" : "cannot");
876 if (can_support_interlaced) {
877 /* No special handling is needed */
878 return gst_video_encoder_proxy_getcaps (encoder, nullptr, filter);
881 template_caps = gst_pad_get_pad_template_caps (encoder->sinkpad);
882 template_caps = gst_caps_make_writable (template_caps);
884 gst_caps_set_simple (template_caps, "interlace-mode", G_TYPE_STRING,
885 "progressive", nullptr);
887 supported_caps = gst_video_encoder_proxy_getcaps (encoder,
888 template_caps, filter);
889 gst_caps_unref (template_caps);
891 GST_DEBUG_OBJECT (self, "Returning %" GST_PTR_FORMAT, supported_caps);
893 return supported_caps;
899 const gchar *profile_str;
902 static const H264Profile profile_map[] = {
903 {MFX_PROFILE_AVC_HIGH, "high"},
904 {MFX_PROFILE_AVC_MAIN, "main"},
905 {MFX_PROFILE_AVC_CONSTRAINED_BASELINE, "constrained-baseline"},
906 {MFX_PROFILE_AVC_PROGRESSIVE_HIGH, "progressive-high"},
907 {MFX_PROFILE_AVC_CONSTRAINED_HIGH, "constrained-high"},
908 {MFX_PROFILE_AVC_BASELINE, "baseline"}
912 gst_qsv_h264_profile_to_string (mfxU16 profile)
914 for (guint i = 0; i < G_N_ELEMENTS (profile_map); i++) {
915 if (profile_map[i].profile == profile)
916 return profile_map[i].profile_str;
923 gst_qsv_h264_profile_string_to_value (const gchar * profile_str)
925 for (guint i = 0; i < G_N_ELEMENTS (profile_map); i++) {
926 if (g_strcmp0 (profile_map[i].profile_str, profile_str) == 0)
927 return profile_map[i].profile;
930 return MFX_PROFILE_UNKNOWN;
934 gst_qsv_h264_enc_init_extra_params (GstQsvH264Enc * self)
936 memset (&self->signal_info, 0, sizeof (mfxExtVideoSignalInfo));
937 memset (&self->option, 0, sizeof (mfxExtCodingOption));
938 memset (&self->option2, 0, sizeof (mfxExtCodingOption2));
939 memset (&self->option3, 0, sizeof (mfxExtCodingOption3));
941 self->signal_info.Header.BufferId = MFX_EXTBUFF_VIDEO_SIGNAL_INFO;
942 self->signal_info.Header.BufferSz = sizeof (mfxExtVideoSignalInfo);
944 self->option.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
945 self->option.Header.BufferSz = sizeof (mfxExtCodingOption);
947 self->option2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
948 self->option2.Header.BufferSz = sizeof (mfxExtCodingOption2);
950 self->option3.Header.BufferId = MFX_EXTBUFF_CODING_OPTION3;
951 self->option3.Header.BufferSz = sizeof (mfxExtCodingOption3);
955 gst_qsv_h264_enc_set_bitrate (GstQsvH264Enc * self, mfxVideoParam * param)
960 switch (param->mfx.RateControlMethod) {
961 case MFX_RATECONTROL_CBR:
962 multiplier = (self->bitrate + 0x10000) / 0x10000;
963 param->mfx.TargetKbps = param->mfx.MaxKbps = self->bitrate / multiplier;
964 param->mfx.BRCParamMultiplier = (mfxU16) multiplier;
966 case MFX_RATECONTROL_VBR:
967 case MFX_RATECONTROL_VCM:
968 case MFX_RATECONTROL_QVBR:
969 max_val = MAX (self->bitrate, self->max_bitrate);
970 multiplier = (max_val + 0x10000) / 0x10000;
971 param->mfx.TargetKbps = self->bitrate / multiplier;
972 param->mfx.MaxKbps = self->max_bitrate / multiplier;
973 param->mfx.BRCParamMultiplier = (mfxU16) multiplier;
975 case MFX_RATECONTROL_CQP:
976 param->mfx.QPI = self->qp_i;
977 param->mfx.QPP = self->qp_p;
978 param->mfx.QPB = self->qp_b;
980 case MFX_RATECONTROL_AVBR:
981 multiplier = (self->bitrate + 0x10000) / 0x10000;
982 param->mfx.TargetKbps = self->bitrate / multiplier;
983 param->mfx.Accuracy = self->avbr_accuracy;
984 param->mfx.Convergence = self->avbr_convergence;
985 param->mfx.BRCParamMultiplier = (mfxU16) multiplier;
987 case MFX_RATECONTROL_LA:
988 multiplier = (self->bitrate + 0x10000) / 0x10000;
989 param->mfx.TargetKbps = self->bitrate / multiplier;
990 param->mfx.BRCParamMultiplier = (mfxU16) multiplier;
992 case MFX_RATECONTROL_LA_HRD:
993 max_val = MAX (self->bitrate, self->max_bitrate);
994 multiplier = (max_val + 0x10000) / 0x10000;
995 param->mfx.TargetKbps = self->bitrate / multiplier;
996 param->mfx.MaxKbps = self->max_bitrate / multiplier;
997 param->mfx.BRCParamMultiplier = (mfxU16) multiplier;
999 case MFX_RATECONTROL_ICQ:
1000 case MFX_RATECONTROL_LA_ICQ:
1001 param->mfx.ICQQuality = self->icq_quality;
1004 GST_WARNING_OBJECT (self,
1005 "Unhandled rate-control method %d", self->rate_control);
1011 gst_qsv_h264_enc_set_format (GstQsvEncoder * encoder,
1012 GstVideoCodecState * state, mfxVideoParam * param, GPtrArray * extra_params)
1014 GstQsvH264Enc *self = GST_QSV_H264_ENC (encoder);
1015 GstCaps *allowed_caps, *fixated_caps;
1016 std::set < std::string > downstream_profiles;
1017 std::set < std::string > tmp;
1018 guint bframes = self->bframes;
1019 mfxU16 cabac = self->cabac;
1020 std::string profile_str;
1021 mfxU16 mfx_profile = MFX_PROFILE_UNKNOWN;
1022 GstVideoInfo *info = &state->info;
1023 mfxExtVideoSignalInfo *signal_info = nullptr;
1024 mfxExtCodingOption *option;
1025 mfxExtCodingOption2 *option2;
1026 mfxExtCodingOption3 *option3;
1028 const gchar *stream_format;
1029 mfxFrameInfo *frame_info;
1031 frame_info = ¶m->mfx.FrameInfo;
1033 /* QSV specific alignment requirement:
1034 * width/height should be multiple of 16, and for interlaced encoding,
1035 * height should be multiple of 32 */
1036 frame_info->Width = GST_ROUND_UP_16 (info->width);
1037 if (GST_VIDEO_INFO_IS_INTERLACED (info)) {
1038 frame_info->Height = GST_ROUND_UP_32 (info->height);
1039 switch (GST_VIDEO_INFO_FIELD_ORDER (info)) {
1040 case GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST:
1041 frame_info->PicStruct = MFX_PICSTRUCT_FIELD_TFF;
1043 case GST_VIDEO_FIELD_ORDER_BOTTOM_FIELD_FIRST:
1044 frame_info->PicStruct = MFX_PICSTRUCT_FIELD_BFF;
1047 frame_info->PicStruct = MFX_PICSTRUCT_UNKNOWN;
1051 frame_info->Height = GST_ROUND_UP_16 (info->height);
1052 frame_info->PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
1055 /* QSV wouldn't be happy with this size, increase */
1056 if (frame_info->Width == 16)
1057 frame_info->Width = 32;
1059 if (frame_info->Height == 16)
1060 frame_info->Height = 32;
1062 frame_info->CropW = info->width;
1063 frame_info->CropH = info->height;
1064 if (GST_VIDEO_INFO_FPS_N (info) > 0 && GST_VIDEO_INFO_FPS_D (info) > 0) {
1065 frame_info->FrameRateExtN = GST_VIDEO_INFO_FPS_N (info);
1066 frame_info->FrameRateExtD = GST_VIDEO_INFO_FPS_D (info);
1068 /* HACK: Same as x264enc */
1069 frame_info->FrameRateExtN = 25;
1070 frame_info->FrameRateExtD = 1;
1073 frame_info->AspectRatioW = GST_VIDEO_INFO_PAR_N (info);
1074 frame_info->AspectRatioH = GST_VIDEO_INFO_PAR_D (info);
1076 /* TODO: update for non 4:2:0 formats. Currently NV12 only */
1077 frame_info->ChromaFormat = MFX_CHROMAFORMAT_YUV420;
1078 switch (GST_VIDEO_INFO_FORMAT (info)) {
1079 case GST_VIDEO_FORMAT_NV12:
1080 frame_info->FourCC = MFX_FOURCC_NV12;
1081 frame_info->BitDepthLuma = 8;
1082 frame_info->BitDepthChroma = 8;
1085 GST_ERROR_OBJECT (self, "Unexpected format %s",
1086 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (info)));
1090 allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
1091 if (!allowed_caps) {
1092 GST_WARNING_OBJECT (self, "Failed to get allowed caps");
1096 gst_qsv_h264_enc_init_extra_params (self);
1097 option = &self->option;
1098 option2 = &self->option2;
1099 option3 = &self->option3;
1101 self->packetized = FALSE;
1103 fixated_caps = gst_caps_fixate (gst_caps_copy (allowed_caps));
1104 s = gst_caps_get_structure (fixated_caps, 0);
1105 stream_format = gst_structure_get_string (s, "stream-format");
1106 if (g_strcmp0 (stream_format, "avc") == 0)
1107 self->packetized = TRUE;
1108 gst_caps_unref (fixated_caps);
1110 for (guint i = 0; i < gst_caps_get_size (allowed_caps); i++) {
1111 const GValue *profile_value;
1112 const gchar *profile;
1114 s = gst_caps_get_structure (allowed_caps, i);
1115 profile_value = gst_structure_get_value (s, "profile");
1119 if (GST_VALUE_HOLDS_LIST (profile_value)) {
1120 for (guint j = 0; j < gst_value_list_get_size (profile_value); j++) {
1121 const GValue *p = gst_value_list_get_value (profile_value, j);
1123 if (!G_VALUE_HOLDS_STRING (p))
1126 profile = g_value_get_string (p);
1128 downstream_profiles.insert (profile);
1131 } else if (G_VALUE_HOLDS_STRING (profile_value)) {
1132 profile = g_value_get_string (profile_value);
1134 downstream_profiles.insert (profile);
1138 GST_DEBUG_OBJECT (self, "Downstream supports %" G_GSIZE_FORMAT " profiles",
1139 downstream_profiles.size ());
1141 /* Prune incompatible profiles */
1142 if ((param->mfx.FrameInfo.PicStruct & MFX_PICSTRUCT_PROGRESSIVE) == 0) {
1143 /* Interlaced, only main and high profiles are allowed */
1144 downstream_profiles.erase (gst_qsv_h264_profile_to_string
1145 (MFX_PROFILE_AVC_CONSTRAINED_BASELINE));
1146 downstream_profiles.erase (gst_qsv_h264_profile_to_string
1147 (MFX_PROFILE_AVC_PROGRESSIVE_HIGH));
1148 downstream_profiles.erase (gst_qsv_h264_profile_to_string
1149 (MFX_PROFILE_AVC_CONSTRAINED_HIGH));
1150 downstream_profiles.erase (gst_qsv_h264_profile_to_string
1151 (MFX_PROFILE_AVC_BASELINE));
1154 if (downstream_profiles.empty ()) {
1155 GST_WARNING_OBJECT (self, "No compatible profile was detected");
1156 gst_clear_caps (&allowed_caps);
1161 /* baseline and constrained-high doesn't support bframe */
1162 tmp = downstream_profiles;
1163 tmp.erase (gst_qsv_h264_profile_to_string
1164 (MFX_PROFILE_AVC_CONSTRAINED_BASELINE));
1165 tmp.erase (gst_qsv_h264_profile_to_string
1166 (MFX_PROFILE_AVC_CONSTRAINED_HIGH));
1167 tmp.erase (gst_qsv_h264_profile_to_string (MFX_PROFILE_AVC_BASELINE));
1170 GST_WARNING_OBJECT (self, "None of downstream profile supports bframes");
1172 tmp = downstream_profiles;
1175 downstream_profiles = tmp;
1178 if (cabac == MFX_CODINGOPTION_ON) {
1179 /* baseline doesn't support cabac */
1180 tmp = downstream_profiles;
1181 tmp.erase (gst_qsv_h264_profile_to_string
1182 (MFX_PROFILE_AVC_CONSTRAINED_BASELINE));
1183 tmp.erase (gst_qsv_h264_profile_to_string (MFX_PROFILE_AVC_BASELINE));
1186 GST_WARNING_OBJECT (self, "None of downstream profile supports cabac");
1187 cabac = MFX_CODINGOPTION_OFF;
1188 tmp = downstream_profiles;
1190 downstream_profiles = tmp;
1193 /* we have our preference order */
1195 for (guint i = 0; i < G_N_ELEMENTS (profile_map); i++) {
1196 auto it = downstream_profiles.find (profile_map[i].profile_str);
1197 if (it != downstream_profiles.end ()) {
1204 if (profile_str.empty ()) {
1205 GST_WARNING_OBJECT (self, "Failed to determine profile");
1209 GST_DEBUG_OBJECT (self, "Selected profile %s", profile_str.c_str ());
1210 mfx_profile = gst_qsv_h264_profile_string_to_value (profile_str.c_str ());
1212 gst_clear_caps (&allowed_caps);
1214 if (cabac == MFX_CODINGOPTION_UNKNOWN) {
1215 switch (mfx_profile) {
1216 case MFX_PROFILE_AVC_CONSTRAINED_BASELINE:
1217 case MFX_PROFILE_AVC_BASELINE:
1218 cabac = MFX_CODINGOPTION_OFF;
1221 cabac = MFX_CODINGOPTION_ON;
1226 g_mutex_lock (&self->prop_lock);
1227 param->mfx.CodecId = MFX_CODEC_AVC;
1228 param->mfx.CodecProfile = mfx_profile;
1229 param->mfx.GopRefDist = bframes + 1;
1230 param->mfx.GopPicSize = self->gop_size;
1231 param->mfx.IdrInterval = self->idr_interval;
1232 param->mfx.RateControlMethod = self->rate_control;
1233 param->mfx.NumRefFrame = self->ref_frames;
1235 gst_qsv_h264_enc_set_bitrate (self, param);
1237 /* Write signal info only when upstream caps contains valid colorimetry,
1238 * because derived default colorimetry in gst_video_info_from_caps() tends to
1239 * very wrong in various cases, and it's even worse than "unknown" */
1241 GstStructure *s = gst_caps_get_structure (state->caps, 0);
1242 GstVideoColorimetry cinfo;
1245 str = gst_structure_get_string (s, "colorimetry");
1246 if (str && gst_video_colorimetry_from_string (&cinfo, str)) {
1247 signal_info = &self->signal_info;
1249 /* 0: Component, 1: PAL, 2: NTSC, 3: SECAM, 4: MAC, 5: Unspecified */
1250 signal_info->VideoFormat = 5;
1251 if (cinfo.range == GST_VIDEO_COLOR_RANGE_0_255)
1252 signal_info->VideoFullRange = 1;
1254 signal_info->VideoFullRange = 0;
1255 signal_info->ColourDescriptionPresent = 1;
1256 signal_info->ColourPrimaries =
1257 gst_video_color_primaries_to_iso (cinfo.primaries);
1258 signal_info->TransferCharacteristics =
1259 gst_video_transfer_function_to_iso (cinfo.transfer);
1260 signal_info->MatrixCoefficients =
1261 gst_video_color_matrix_to_iso (cinfo.matrix);
1265 if (cabac == MFX_CODINGOPTION_OFF)
1266 option->CAVLC = MFX_CODINGOPTION_ON;
1268 option->CAVLC = MFX_CODINGOPTION_OFF;
1270 /* TODO: property ? */
1271 option->AUDelimiter = MFX_CODINGOPTION_ON;
1273 if (self->disable_hrd_conformance) {
1274 option->NalHrdConformance = MFX_CODINGOPTION_OFF;
1275 option->VuiVclHrdParameters = MFX_CODINGOPTION_OFF;
1278 /* Enables PicTiming SEI by default */
1279 option->PicTimingSEI = MFX_CODINGOPTION_ON;
1281 /* VUI is useful in various cases, so we don't want to disable it */
1282 option2->DisableVUI = MFX_CODINGOPTION_OFF;
1284 /* Do not repeat PPS */
1285 option2->RepeatPPS = MFX_CODINGOPTION_OFF;
1287 if (param->mfx.RateControlMethod == MFX_RATECONTROL_LA ||
1288 param->mfx.RateControlMethod == MFX_RATECONTROL_LA_HRD ||
1289 param->mfx.RateControlMethod == MFX_RATECONTROL_LA_ICQ) {
1290 option2->LookAheadDS = self->rc_lookahead_ds;
1291 option2->LookAheadDepth = self->rc_lookahead;
1294 option2->MinQPI = self->min_qp_i;
1295 option2->MinQPP = self->min_qp_p;
1296 option2->MinQPB = self->min_qp_b;
1297 option2->MaxQPI = self->max_qp_i;
1298 option2->MaxQPP = self->max_qp_p;
1299 option2->MaxQPB = self->max_qp_b;
1301 /* QSV wants MFX_B_REF_PYRAMID when more than 1 b-frame is enabled */
1302 if (param->mfx.GopRefDist > 2)
1303 option2->BRefType = MFX_B_REF_PYRAMID;
1305 /* Upstream specified framerate, we will believe it's fixed framerate */
1306 if (GST_VIDEO_INFO_FPS_N (info) > 0 && GST_VIDEO_INFO_FPS_D (info) > 0) {
1307 option2->FixedFrameRate = MFX_CODINGOPTION_ON;
1308 option3->TimingInfoPresent = MFX_CODINGOPTION_ON;
1311 if (param->mfx.RateControlMethod == MFX_RATECONTROL_QVBR)
1312 option3->QVBRQuality = self->qvbr_quality;
1315 g_ptr_array_add (extra_params, signal_info);
1316 g_ptr_array_add (extra_params, option);
1317 g_ptr_array_add (extra_params, option2);
1318 g_ptr_array_add (extra_params, option3);
1320 param->ExtParam = (mfxExtBuffer **) extra_params->pdata;
1321 param->NumExtParam = extra_params->len;
1323 self->bitrate_updated = FALSE;
1324 self->property_updated = FALSE;
1326 g_mutex_unlock (&self->prop_lock);
1332 gst_qsv_h264_enc_set_output_state (GstQsvEncoder * encoder,
1333 GstVideoCodecState * state, mfxSession session)
1335 GstQsvH264Enc *self = GST_QSV_H264_ENC (encoder);
1338 GstVideoCodecState *out_state;
1339 guint bitrate, max_bitrate;
1340 guint multiplier = 1;
1341 mfxVideoParam param;
1342 const gchar *profile_str;
1344 mfxExtCodingOptionSPSPPS sps_pps;
1345 mfxExtBuffer *ext_buffers[1];
1348 GstBuffer *codec_data = nullptr;
1350 memset (¶m, 0, sizeof (mfxVideoParam));
1351 memset (&sps_pps, 0, sizeof (mfxExtCodingOptionSPSPPS));
1352 if (self->packetized) {
1353 sps_pps.Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS;
1354 sps_pps.Header.BufferSz = sizeof (mfxExtCodingOptionSPSPPS);
1356 sps_pps.SPSBuffer = sps;
1357 sps_pps.SPSBufSize = sizeof (sps);
1359 sps_pps.PPSBuffer = pps;
1360 sps_pps.PPSBufSize = sizeof (pps);
1362 ext_buffers[0] = (mfxExtBuffer *) & sps_pps;
1364 param.NumExtParam = 1;
1365 param.ExtParam = ext_buffers;
1368 status = MFXVideoENCODE_GetVideoParam (session, ¶m);
1369 if (status < MFX_ERR_NONE) {
1370 GST_ERROR_OBJECT (self, "Failed to get video param %d (%s)",
1371 QSV_STATUS_ARGS (status));
1373 } else if (status != MFX_ERR_NONE) {
1374 GST_WARNING_OBJECT (self, "GetVideoParam returned warning %d (%s)",
1375 QSV_STATUS_ARGS (status));
1378 if (self->packetized) {
1379 GstH264ParserResult rst;
1380 GstH264NalUnit sps_nalu, pps_nalu;
1383 guint8 profile_idc, profile_comp, level_idc;
1384 const guint nal_length_size = 4;
1385 const guint num_sps = 1;
1386 const guint num_pps = 1;
1388 rst = gst_h264_parser_identify_nalu_unchecked (self->parser,
1389 sps, 0, sps_pps.SPSBufSize, &sps_nalu);
1390 if (rst != GST_H264_PARSER_OK) {
1391 GST_ERROR_OBJECT (self, "Failed to identify SPS nal");
1395 if (sps_nalu.size < 4) {
1396 GST_ERROR_OBJECT (self, "Too small sps nal size %d", sps_nalu.size);
1400 data = sps_nalu.data + sps_nalu.offset + sps_nalu.header_bytes;
1401 profile_idc = data[0];
1402 profile_comp = data[1];
1403 level_idc = data[2];
1405 rst = gst_h264_parser_identify_nalu_unchecked (self->parser,
1406 pps, 0, sps_pps.PPSBufSize, &pps_nalu);
1407 if (rst != GST_H264_PARSER_OK) {
1408 GST_ERROR_OBJECT (self, "Failed to identify PPS nal");
1412 /* 5: configuration version, profile, compatibility, level, nal length
1420 * -> 11 + sps_size + pps_size
1422 codec_data = gst_buffer_new_and_alloc (11 + sps_nalu.size + pps_nalu.size);
1424 gst_buffer_map (codec_data, &info, GST_MAP_WRITE);
1426 data = (guint8 *) info.data;
1428 data[1] = profile_idc;
1429 data[2] = profile_comp;
1430 data[3] = level_idc;
1431 data[4] = 0xfc | (nal_length_size - 1);
1432 data[5] = 0xe0 | num_sps;
1434 GST_WRITE_UINT16_BE (data, sps_nalu.size);
1436 memcpy (data, sps_nalu.data + sps_nalu.offset, sps_nalu.size);
1437 data += sps_nalu.size;
1442 GST_WRITE_UINT16_BE (data, pps_nalu.size);
1444 memcpy (data, pps_nalu.data + pps_nalu.offset, pps_nalu.size);
1446 gst_buffer_unmap (codec_data, &info);
1449 caps = gst_caps_from_string ("video/x-h264, alignment = (string) au");
1450 profile_str = gst_qsv_h264_profile_to_string (param.mfx.CodecProfile);
1452 gst_caps_set_simple (caps, "profile", G_TYPE_STRING, profile_str, nullptr);
1454 if (self->packetized) {
1455 gst_caps_set_simple (caps, "stream-format", G_TYPE_STRING, "avc",
1456 "codec_data", GST_TYPE_BUFFER, codec_data, nullptr);
1457 gst_buffer_unref (codec_data);
1459 gst_caps_set_simple (caps, "stream-format", G_TYPE_STRING, "byte-stream",
1463 out_state = gst_video_encoder_set_output_state (GST_VIDEO_ENCODER (encoder),
1465 gst_video_codec_state_unref (out_state);
1467 tags = gst_tag_list_new_empty ();
1468 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_ENCODER, "qsvh264enc",
1471 if (param.mfx.BRCParamMultiplier > 0)
1472 multiplier = param.mfx.BRCParamMultiplier;
1474 switch (param.mfx.RateControlMethod) {
1475 case MFX_RATECONTROL_CQP:
1476 case MFX_RATECONTROL_ICQ:
1477 case MFX_RATECONTROL_LA_ICQ:
1478 /* We don't know target/max bitrate in this case */
1481 max_bitrate = (guint) param.mfx.MaxKbps * multiplier;
1482 bitrate = (guint) param.mfx.TargetKbps * multiplier;
1484 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
1485 GST_TAG_NOMINAL_BITRATE, bitrate * 1000, nullptr);
1488 if (max_bitrate > 0) {
1489 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
1490 GST_TAG_MAXIMUM_BITRATE, max_bitrate * 1000, nullptr);
1495 gst_video_encoder_merge_tags (GST_VIDEO_ENCODER (encoder),
1496 tags, GST_TAG_MERGE_REPLACE);
1497 gst_tag_list_unref (tags);
1503 gst_qsv_h264_enc_foreach_caption_meta (GstBuffer * buffer, GstMeta ** meta,
1504 GPtrArray * payload)
1506 GstVideoCaptionMeta *cc_meta;
1512 if ((*meta)->info->api != GST_VIDEO_CAPTION_META_API_TYPE)
1515 cc_meta = (GstVideoCaptionMeta *) (*meta);
1517 if (cc_meta->caption_type != GST_VIDEO_CAPTION_TYPE_CEA708_RAW)
1520 /* QSV requires full sei_message() structure */
1521 /* 1 byte contry_code + 10 bytes CEA-708 specific data + caption data */
1522 payload_size = 11 + cc_meta->size;
1523 extra_size = payload_size / 255;
1525 /* 1 byte SEI type + 1 byte SEI payload size (+ extra) + payload data */
1526 gst_byte_writer_init_with_size (&br, 2 + extra_size + payload_size, FALSE);
1529 gst_byte_writer_put_uint8 (&br, 4);
1531 /* SEI payload size */
1532 while (payload_size >= 0xff) {
1533 gst_byte_writer_put_uint8 (&br, 0xff);
1534 payload_size -= 0xff;
1536 gst_byte_writer_put_uint8 (&br, payload_size);
1538 /* 8-bits itu_t_t35_country_code */
1539 gst_byte_writer_put_uint8 (&br, 181);
1541 /* 16-bits itu_t_t35_provider_code */
1542 gst_byte_writer_put_uint8 (&br, 0);
1543 gst_byte_writer_put_uint8 (&br, 49);
1545 /* 32-bits ATSC_user_identifier */
1546 gst_byte_writer_put_uint8 (&br, 'G');
1547 gst_byte_writer_put_uint8 (&br, 'A');
1548 gst_byte_writer_put_uint8 (&br, '9');
1549 gst_byte_writer_put_uint8 (&br, '4');
1551 /* 8-bits ATSC1_data_user_data_type_code */
1552 gst_byte_writer_put_uint8 (&br, 3);
1555 * 1 bit process_em_data_flag (0)
1556 * 1 bit process_cc_data_flag (1)
1557 * 1 bit additional_data_flag (0)
1560 gst_byte_writer_put_uint8 (&br, ((cc_meta->size / 3) & 0x1f) | 0x40);
1562 /* 8 bits em_data, unused */
1563 gst_byte_writer_put_uint8 (&br, 255);
1565 gst_byte_writer_put_data (&br, cc_meta->data, cc_meta->size);
1568 gst_byte_writer_put_uint8 (&br, 255);
1570 p = g_new0 (mfxPayload, 1);
1571 p->BufSize = gst_byte_writer_get_pos (&br);
1572 p->NumBit = p->BufSize * 8;
1574 p->Data = gst_byte_writer_reset_and_get_data (&br);
1576 g_ptr_array_add (payload, p);
1582 gst_qsv_h264_enc_attach_payload (GstQsvEncoder * encoder,
1583 GstVideoCodecFrame * frame, GPtrArray * payload)
1585 GstQsvH264Enc *self = GST_QSV_H264_ENC (encoder);
1587 if (self->cc_insert == GST_QSV_H264_ENC_SEI_DISABLED)
1590 gst_buffer_foreach_meta (frame->input_buffer,
1591 (GstBufferForeachMetaFunc) gst_qsv_h264_enc_foreach_caption_meta,
1598 gst_qsv_h264_enc_create_output_buffer (GstQsvEncoder * encoder,
1599 mfxBitstream * bitstream)
1601 GstQsvH264Enc *self = GST_QSV_H264_ENC (encoder);
1603 GstH264ParserResult rst;
1604 GstH264NalUnit nalu;
1606 if (!self->packetized) {
1607 buf = gst_buffer_new_memdup (bitstream->Data + bitstream->DataOffset,
1608 bitstream->DataLength);
1610 buf = gst_buffer_new ();
1611 rst = gst_h264_parser_identify_nalu (self->parser,
1612 bitstream->Data + bitstream->DataOffset, 0, bitstream->DataLength,
1614 if (rst == GST_H264_PARSER_NO_NAL_END)
1615 rst = GST_H264_PARSER_OK;
1617 while (rst == GST_H264_PARSER_OK) {
1621 data = (guint8 *) g_malloc0 (nalu.size + 4);
1622 GST_WRITE_UINT32_BE (data, nalu.size);
1623 memcpy (data + 4, nalu.data + nalu.offset, nalu.size);
1625 mem = gst_memory_new_wrapped ((GstMemoryFlags) 0, data, nalu.size + 4,
1626 0, nalu.size + 4, data, (GDestroyNotify) g_free);
1627 gst_buffer_append_memory (buf, mem);
1629 rst = gst_h264_parser_identify_nalu (self->parser,
1630 bitstream->Data + bitstream->DataOffset, nalu.offset + nalu.size,
1631 bitstream->DataLength, &nalu);
1633 if (rst == GST_H264_PARSER_NO_NAL_END)
1634 rst = GST_H264_PARSER_OK;
1638 /* This buffer must be the end of a frame boundary */
1639 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_MARKER);
1644 static GstQsvEncoderReconfigure
1645 gst_qsv_h264_enc_check_reconfigure (GstQsvEncoder * encoder, mfxSession session,
1646 mfxVideoParam * param, GPtrArray * extra_params)
1648 GstQsvH264Enc *self = GST_QSV_H264_ENC (encoder);
1649 GstQsvEncoderReconfigure ret = GST_QSV_ENCODER_RECONFIGURE_NONE;
1651 g_mutex_lock (&self->prop_lock);
1652 if (self->property_updated) {
1653 ret = GST_QSV_ENCODER_RECONFIGURE_FULL;
1657 if (self->bitrate_updated) {
1659 mfxExtEncoderResetOption reset_opt;
1660 reset_opt.Header.BufferId = MFX_EXTBUFF_ENCODER_RESET_OPTION;
1661 reset_opt.Header.BufferSz = sizeof (mfxExtEncoderResetOption);
1662 reset_opt.StartNewSequence = MFX_CODINGOPTION_UNKNOWN;
1664 gst_qsv_h264_enc_set_bitrate (self, param);
1666 g_ptr_array_add (extra_params, &reset_opt);
1667 param->ExtParam = (mfxExtBuffer **) extra_params->pdata;
1668 param->NumExtParam = extra_params->len;
1670 status = MFXVideoENCODE_Query (session, param, param);
1671 g_ptr_array_remove_index (extra_params, extra_params->len - 1);
1672 param->NumExtParam = extra_params->len;
1674 if (status != MFX_ERR_NONE) {
1675 GST_WARNING_OBJECT (self, "MFXVideoENCODE_Query returned %d (%s)",
1676 QSV_STATUS_ARGS (status));
1677 ret = GST_QSV_ENCODER_RECONFIGURE_FULL;
1679 if (reset_opt.StartNewSequence == MFX_CODINGOPTION_OFF) {
1680 GST_DEBUG_OBJECT (self, "Can update without new sequence");
1681 ret = GST_QSV_ENCODER_RECONFIGURE_BITRATE;
1683 GST_DEBUG_OBJECT (self, "Need new sequence");
1684 ret = GST_QSV_ENCODER_RECONFIGURE_FULL;
1690 self->property_updated = FALSE;
1691 self->bitrate_updated = FALSE;
1692 g_mutex_unlock (&self->prop_lock);
1704 gst_qsv_h264_enc_register (GstPlugin * plugin, guint rank, guint impl_index,
1705 GstObject * device, mfxSession session)
1708 mfxVideoParam param;
1710 static const Resolution resolutions_to_check[] = {
1711 {1280, 720}, {1920, 1088}, {2560, 1440}, {3840, 2160}, {4096, 2160},
1712 {7680, 4320}, {8192, 4320}
1714 std::vector < mfxU16 > supported_profiles;
1715 Resolution max_resolution;
1716 bool supports_interlaced = false;
1718 memset (¶m, 0, sizeof (mfxVideoParam));
1719 memset (&max_resolution, 0, sizeof (Resolution));
1721 param.AsyncDepth = 4;
1722 param.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY;
1725 mfx->CodecId = MFX_CODEC_AVC;
1727 mfx->FrameInfo.Width = GST_ROUND_UP_16 (320);
1728 mfx->FrameInfo.Height = GST_ROUND_UP_16 (240);
1729 mfx->FrameInfo.CropW = 320;
1730 mfx->FrameInfo.CropH = 240;
1731 mfx->FrameInfo.FrameRateExtN = 30;
1732 mfx->FrameInfo.FrameRateExtD = 1;
1733 mfx->FrameInfo.AspectRatioW = 1;
1734 mfx->FrameInfo.AspectRatioH = 1;
1735 mfx->FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
1736 mfx->FrameInfo.FourCC = MFX_FOURCC_NV12;
1737 mfx->FrameInfo.BitDepthLuma = 8;
1738 mfx->FrameInfo.BitDepthChroma = 8;
1739 mfx->FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
1741 /* Check supported profiles */
1742 for (guint i = 0; i < G_N_ELEMENTS (profile_map); i++) {
1743 mfx->CodecProfile = profile_map[i].profile;
1745 if (MFXVideoENCODE_Query (session, ¶m, ¶m) != MFX_ERR_NONE)
1748 supported_profiles.push_back (profile_map[i].profile);
1751 if (supported_profiles.empty ()) {
1752 GST_INFO ("Device doesn't support H.264 encoding");
1756 mfx->CodecProfile = supported_profiles[0];
1758 /* Check max-resolution */
1759 for (guint i = 0; i < G_N_ELEMENTS (resolutions_to_check); i++) {
1760 mfx->FrameInfo.Width = GST_ROUND_UP_16 (resolutions_to_check[i].width);
1761 mfx->FrameInfo.Height = GST_ROUND_UP_16 (resolutions_to_check[i].height);
1762 mfx->FrameInfo.CropW = resolutions_to_check[i].width;
1763 mfx->FrameInfo.CropH = resolutions_to_check[i].height;
1765 if (MFXVideoENCODE_Query (session, ¶m, ¶m) != MFX_ERR_NONE)
1768 max_resolution.width = resolutions_to_check[i].width;
1769 max_resolution.height = resolutions_to_check[i].height;
1772 GST_INFO ("Maximum supported resolution: %dx%d",
1773 max_resolution.width, max_resolution.height);
1775 /* TODO: check supported rate-control methods and expose only supported
1776 * methods, since the device might not be able to support some of them */
1778 /* Check interlaced encoding */
1780 for (const auto &iter: supported_profiles) {
1781 if (iter == MFX_PROFILE_AVC_MAIN ||
1782 iter == MFX_PROFILE_AVC_HIGH) {
1783 /* Make sure non-lowpower mode, otherwise QSV will not accept
1784 * interlaced format during Query() */
1785 mfx->LowPower = MFX_CODINGOPTION_UNKNOWN;
1787 /* Interlaced encoding is not compatible with MFX_RATECONTROL_VCM,
1788 * make sure profile */
1789 mfx->RateControlMethod = MFX_RATECONTROL_CBR;
1791 /* Too high (MFX_LEVEL_AVC_41) or low (MFX_LEVEL_AVC_21) level will not be
1792 * accepted for interlaced encoding */
1793 mfx->CodecLevel = MFX_LEVEL_UNKNOWN;
1794 mfx->CodecProfile = iter;
1796 mfx->FrameInfo.Width = GST_ROUND_UP_16 (320);
1797 mfx->FrameInfo.Height = GST_ROUND_UP_32 (240);
1798 mfx->FrameInfo.CropW = 320;
1799 mfx->FrameInfo.CropH = 240;
1800 mfx->FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
1802 status = MFXVideoENCODE_Query (session, ¶m, ¶m);
1804 if (status == MFX_ERR_NONE) {
1805 GST_INFO ("Interlaced encoding is supported");
1806 supports_interlaced = true;
1813 /* To cover both landscape and portrait,
1814 * select max value (width in this case) */
1815 guint resolution = MAX (max_resolution.width, max_resolution.height);
1816 std::string sink_caps_str = "video/x-raw, format=(string) NV12";
1818 sink_caps_str += ", width=(int) [ 16, " + std::to_string (resolution) + " ]";
1819 sink_caps_str += ", height=(int) [ 16, " + std::to_string (resolution) + " ]";
1820 if (!supports_interlaced)
1821 sink_caps_str += ", interlace-mode = (string) progressive";
1823 GstCaps *sink_caps = gst_caps_from_string (sink_caps_str.c_str ());
1825 /* TODO: Add support for VA */
1827 GstCaps *d3d11_caps = gst_caps_copy (sink_caps);
1828 GstCapsFeatures *caps_features =
1829 gst_caps_features_new (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY, nullptr);
1830 gst_caps_set_features_simple (d3d11_caps, caps_features);
1831 gst_caps_append (d3d11_caps, sink_caps);
1832 sink_caps = d3d11_caps;
1834 GstCaps *va_caps = gst_caps_copy (sink_caps);
1835 GstCapsFeatures *caps_features =
1836 gst_caps_features_new (GST_CAPS_FEATURE_MEMORY_VA, nullptr);
1837 gst_caps_set_features_simple (va_caps, caps_features);
1838 gst_caps_append (va_caps, sink_caps);
1839 sink_caps = va_caps;
1842 std::string src_caps_str = "video/x-h264";
1843 src_caps_str += ", width=(int) [ 16, " + std::to_string (resolution) + " ]";
1844 src_caps_str += ", height=(int) [ 16, " + std::to_string (resolution) + " ]";
1846 src_caps_str += ", stream-format= (string) { avc, byte-stream }";
1847 src_caps_str += ", alignment=(string) au";
1849 if (supported_profiles.size () > 1) {
1850 src_caps_str += ", profile=(string) { ";
1852 for (const auto &iter: supported_profiles) {
1854 src_caps_str += ", ";
1857 src_caps_str += gst_qsv_h264_profile_to_string (iter);
1860 src_caps_str += " }";
1862 src_caps_str += ", profile=(string) ";
1863 src_caps_str += gst_qsv_h264_profile_to_string (supported_profiles[0]);
1867 GstCaps *src_caps = gst_caps_from_string (src_caps_str.c_str ());
1869 GST_MINI_OBJECT_FLAG_SET (sink_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1870 GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1872 GstQsvH264EncClassData *cdata = g_new0 (GstQsvH264EncClassData, 1);
1873 cdata->sink_caps = sink_caps;
1874 cdata->src_caps = src_caps;
1875 cdata->impl_index = impl_index;
1879 g_object_get (device, "adapter-luid", &device_luid, nullptr);
1880 cdata->adapter_luid = device_luid;
1882 gchar *display_path;
1883 g_object_get (device, "path", &display_path, nullptr);
1884 cdata->display_path = display_path;
1889 gchar *feature_name;
1890 GTypeInfo type_info = {
1891 sizeof (GstQsvH264EncClass),
1894 (GClassInitFunc) gst_qsv_h264_enc_class_init,
1897 sizeof (GstQsvH264Enc),
1899 (GInstanceInitFunc) gst_qsv_h264_enc_init,
1902 type_name = g_strdup ("GstQsvH264Enc");
1903 feature_name = g_strdup ("qsvh264enc");
1906 while (g_type_from_name (type_name)) {
1909 g_free (feature_name);
1910 type_name = g_strdup_printf ("GstQsvH264Device%dEnc", index);
1911 feature_name = g_strdup_printf ("qsvh264device%denc", index);
1914 type = g_type_register_static (GST_TYPE_QSV_ENCODER, type_name, &type_info,
1917 if (rank > 0 && index != 0)
1920 if (!gst_element_register (plugin, feature_name, rank, type))
1921 GST_WARNING ("Failed to register plugin '%s'", type_name);
1924 g_free (feature_name);