From 927bd289e52b4fbd5eefe4721b0b1c5416b68efa Mon Sep 17 00:00:00 2001 From: Thibault Saunier Date: Thu, 29 Oct 2020 10:54:45 -0300 Subject: [PATCH] openh264enc: Add support for main and high profiles Those are supported (to a certain extent) so we should not limit ourself to baseline Part-of: --- docs/plugins/gst_plugins_cache.json | 2 +- ext/openh264/gstopenh264enc.cpp | 43 ++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/docs/plugins/gst_plugins_cache.json b/docs/plugins/gst_plugins_cache.json index 9bd916e727..0553a5f70e 100644 --- a/docs/plugins/gst_plugins_cache.json +++ b/docs/plugins/gst_plugins_cache.json @@ -213650,7 +213650,7 @@ "presence": "always" }, "src": { - "caps": "video/x-h264:\n stream-format: byte-stream\n alignment: au\n profile: baseline\n", + "caps": "video/x-h264:\n stream-format: byte-stream\n alignment: au\n profile: { (string)constrained-baseline, (string)baseline, (string)main, (string)constrained-high, (string)high }\n", "direction": "src", "presence": "always" } diff --git a/ext/openh264/gstopenh264enc.cpp b/ext/openh264/gstopenh264enc.cpp index c725138ef1..0f054592d1 100644 --- a/ext/openh264/gstopenh264enc.cpp +++ b/ext/openh264/gstopenh264enc.cpp @@ -222,8 +222,11 @@ GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS - ("video/x-h264, stream-format=(string)\"byte-stream\", alignment=(string)\"au\", profile=(string)\"baseline\"") - ); + ("video/x-h264, " + "stream-format=(string)\"byte-stream\", alignment=(string)\"au\"," + "profile = (string) { constrained-baseline, baseline, main, constrained-high, high }" + ) +); /* class initialization */ @@ -678,6 +681,29 @@ gst_openh264enc_stop (GstVideoEncoder * encoder) return TRUE; } +static EProfileIdc +gst_openh264enc_get_profile_from_caps (GstCaps *outcaps, GstCaps *allowed_caps) +{ + EProfileIdc oh264_profile = PRO_BASELINE; + GstStructure *allowed_s = gst_caps_get_structure (allowed_caps, 0); + GstStructure *s = gst_caps_get_structure (outcaps, 0); + const gchar *profile = gst_structure_get_string (allowed_s, "profile"); + + if (!profile) + return oh264_profile; + + gst_structure_set (s, "profile", G_TYPE_STRING, profile, NULL); + if (!g_strcmp0 (profile, "constrained-baseline") || + !g_strcmp0 (profile, "baseline")) + return PRO_BASELINE; + else if (!g_strcmp0 (profile, "main")) + return PRO_MAIN; + else if (!g_strcmp0 (profile, "high")) + return PRO_HIGH; + + g_assert_not_reached (); + return PRO_BASELINE; +} static gboolean gst_openh264enc_set_format (GstVideoEncoder * encoder, @@ -694,6 +720,7 @@ gst_openh264enc_set_format (GstVideoEncoder * encoder, GstVideoCodecState *output_state; openh264enc->frame_count = 0; int video_format = videoFormatI420; + GstCaps *allowed_caps = NULL; debug_caps = gst_caps_to_string (state->caps); GST_DEBUG_OBJECT (openh264enc, "gst_e26d4_enc_set_format called, caps: %s", @@ -721,6 +748,12 @@ gst_openh264enc_set_format (GstVideoEncoder * encoder, unsigned int uiTraceLevel = WELS_LOG_ERROR; openh264enc->encoder->SetOption (ENCODER_OPTION_TRACE_LEVEL, &uiTraceLevel); + outcaps = gst_static_pad_template_get_caps (&gst_openh264enc_src_template); + outcaps = gst_caps_make_writable (outcaps); + allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder)); + allowed_caps = gst_caps_make_writable (allowed_caps); + allowed_caps = gst_caps_fixate (allowed_caps); + GST_OBJECT_LOCK (openh264enc); openh264enc->encoder->GetDefaultParams (&enc_params); @@ -753,7 +786,7 @@ gst_openh264enc_set_format (GstVideoEncoder * encoder, enc_params.bPrefixNalAddingCtrl = 0; enc_params.fMaxFrameRate = fps_n * 1.0 / fps_d; enc_params.iLoopFilterDisableIdc = openh264enc->deblocking_mode; - enc_params.sSpatialLayers[0].uiProfileIdc = PRO_BASELINE; + enc_params.sSpatialLayers[0].uiProfileIdc = gst_openh264enc_get_profile_from_caps (outcaps, allowed_caps); enc_params.sSpatialLayers[0].iVideoWidth = enc_params.iPicWidth; enc_params.sSpatialLayers[0].iVideoHeight = enc_params.iPicHeight; enc_params.sSpatialLayers[0].fFrameRate = fps_n * 1.0 / fps_d; @@ -803,10 +836,6 @@ gst_openh264enc_set_format (GstVideoEncoder * encoder, openh264enc->encoder->SetOption (ENCODER_OPTION_DATAFORMAT, &video_format); - outcaps = - gst_caps_copy (gst_static_pad_template_get_caps - (&gst_openh264enc_src_template)); - output_state = gst_video_encoder_set_output_state (encoder, outcaps, state); gst_video_codec_state_unref (output_state);