From 4ee4a9bff96f2bc93796bfeadafe618fb6e5e94e Mon Sep 17 00:00:00 2001 From: Marek Vasut Date: Fri, 11 Feb 2022 23:44:20 +0100 Subject: [PATCH] jpegdec: Factor out gst_jpeg_turbo_parse_ext_fmt_convert() Pull out peer caps checking code into gst_jpeg_turbo_parse_ext_fmt_convert(). This code is used by libjpeg-turbo extras to determine whether peer is capable of handling buffers into which libjpeg-turbo can directly decode data. This kind of check must be performed before jpeg_start_decompress() is called in gst_jpeg_dec_prepare_decode() as well as in gst_jpeg_dec_negotiate(), hence the common code. This commit does modify the code a little to make it easier to call from both call sites without much duplication, hence the extra `if (*clrspc)` test. Part-of: --- .../gst-plugins-good/ext/jpeg/gstjpegdec.c | 96 ++++++++++--------- 1 file changed, 53 insertions(+), 43 deletions(-) diff --git a/subprojects/gst-plugins-good/ext/jpeg/gstjpegdec.c b/subprojects/gst-plugins-good/ext/jpeg/gstjpegdec.c index db81b416a3..6e4199b06b 100644 --- a/subprojects/gst-plugins-good/ext/jpeg/gstjpegdec.c +++ b/subprojects/gst-plugins-good/ext/jpeg/gstjpegdec.c @@ -1008,6 +1008,55 @@ gst_fmt_to_jpeg_turbo_ext_fmt (GstVideoFormat gstfmt) return 0; } } + +static void +gst_jpeg_turbo_parse_ext_fmt_convert (GstJpegDec * dec, gint * clrspc) +{ + GstCaps *peer_caps, *dec_caps; + + dec_caps = gst_static_caps_get (&gst_jpeg_dec_src_pad_template.static_caps); + peer_caps = + gst_pad_peer_query_caps (GST_VIDEO_DECODER_SRC_PAD (dec), dec_caps); + gst_caps_unref (dec_caps); + + GST_DEBUG ("Received caps from peer: %" GST_PTR_FORMAT, peer_caps); + dec->format_convert = FALSE; + if (!gst_caps_is_empty (peer_caps)) { + GstStructure *peerstruct; + const gchar *peerformat; + GstVideoFormat peerfmt; + + if (!gst_caps_is_fixed (peer_caps)) + peer_caps = gst_caps_fixate (peer_caps); + + peerstruct = gst_caps_get_structure (peer_caps, 0); + peerformat = gst_structure_get_string (peerstruct, "format"); + peerfmt = gst_video_format_from_string (peerformat); + + switch (peerfmt) { + case GST_VIDEO_FORMAT_RGB: + case GST_VIDEO_FORMAT_RGBx: + case GST_VIDEO_FORMAT_xRGB: + case GST_VIDEO_FORMAT_RGBA: + case GST_VIDEO_FORMAT_ARGB: + case GST_VIDEO_FORMAT_BGR: + case GST_VIDEO_FORMAT_BGRx: + case GST_VIDEO_FORMAT_xBGR: + case GST_VIDEO_FORMAT_BGRA: + case GST_VIDEO_FORMAT_ABGR: + if (clrspc) + *clrspc = JCS_RGB; + dec->format = peerfmt; + dec->format_convert = TRUE; + dec->libjpeg_ext_format = gst_fmt_to_jpeg_turbo_ext_fmt (peerfmt); + break; + default: + break; + } + } + gst_caps_unref (peer_caps); + GST_DEBUG_OBJECT (dec, "format_convert=%d", dec->format_convert); +} #endif static void @@ -1017,7 +1066,6 @@ gst_jpeg_dec_negotiate (GstJpegDec * dec, gint width, gint height, gint clrspc, GstVideoCodecState *outstate; GstVideoInfo *info; GstVideoFormat format; - GstCaps *peer_caps, *dec_caps; #ifdef JCS_EXTENSIONS if (dec->format_convert) { @@ -1052,48 +1100,10 @@ gst_jpeg_dec_negotiate (GstJpegDec * dec, gint width, gint height, gint clrspc, gst_video_codec_state_unref (outstate); } #ifdef JCS_EXTENSIONS - dec_caps = gst_static_caps_get (&gst_jpeg_dec_src_pad_template.static_caps); - peer_caps = - gst_pad_peer_query_caps (GST_VIDEO_DECODER_SRC_PAD (dec), dec_caps); - gst_caps_unref (dec_caps); - - GST_DEBUG ("Received caps from peer: %" GST_PTR_FORMAT, peer_caps); - dec->format_convert = FALSE; - if (!gst_caps_is_empty (peer_caps)) { - GstStructure *peerstruct; - const gchar *peerformat; - GstVideoFormat peerfmt; - - if (!gst_caps_is_fixed (peer_caps)) - peer_caps = gst_caps_fixate (peer_caps); - - peerstruct = gst_caps_get_structure (peer_caps, 0); - peerformat = gst_structure_get_string (peerstruct, "format"); - peerfmt = gst_video_format_from_string (peerformat); - - switch (peerfmt) { - case GST_VIDEO_FORMAT_RGB: - case GST_VIDEO_FORMAT_RGBx: - case GST_VIDEO_FORMAT_xRGB: - case GST_VIDEO_FORMAT_RGBA: - case GST_VIDEO_FORMAT_ARGB: - case GST_VIDEO_FORMAT_BGR: - case GST_VIDEO_FORMAT_BGRx: - case GST_VIDEO_FORMAT_xBGR: - case GST_VIDEO_FORMAT_BGRA: - case GST_VIDEO_FORMAT_ABGR: - clrspc = JCS_RGB; - format = peerfmt; - dec->format_convert = TRUE; - dec->libjpeg_ext_format = gst_fmt_to_jpeg_turbo_ext_fmt (peerfmt); - break; - default: - break; - } - } - dec->format = format; - gst_caps_unref (peer_caps); - GST_DEBUG_OBJECT (dec, "format_convert=%d", dec->format_convert); + /* Determine if libjpeg-turbo direct format conversion can be used + * with current caps and if so, adjust $dec to enable it and $clrspc + * accordingly. */ + gst_jpeg_turbo_parse_ext_fmt_convert (dec, &clrspc); #endif outstate =