jpegdec: Factor out gst_jpeg_turbo_parse_ext_fmt_convert()
authorMarek Vasut <marex@denx.de>
Fri, 11 Feb 2022 22:44:20 +0000 (23:44 +0100)
committerMarek Vasut <marex@denx.de>
Sun, 13 Feb 2022 13:46:31 +0000 (14:46 +0100)
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: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1687>

subprojects/gst-plugins-good/ext/jpeg/gstjpegdec.c

index db81b41..6e4199b 100644 (file)
@@ -1008,50 +1008,12 @@ gst_fmt_to_jpeg_turbo_ext_fmt (GstVideoFormat gstfmt)
       return 0;
   }
 }
-#endif
 
 static void
-gst_jpeg_dec_negotiate (GstJpegDec * dec, gint width, gint height, gint clrspc,
-    gboolean interlaced)
+gst_jpeg_turbo_parse_ext_fmt_convert (GstJpegDec * dec, gint * clrspc)
 {
-  GstVideoCodecState *outstate;
-  GstVideoInfo *info;
-  GstVideoFormat format;
   GstCaps *peer_caps, *dec_caps;
 
-#ifdef JCS_EXTENSIONS
-  if (dec->format_convert) {
-    format = dec->format;
-  } else
-#endif
-  {
-    switch (clrspc) {
-      case JCS_RGB:
-        format = GST_VIDEO_FORMAT_RGB;
-        break;
-      case JCS_GRAYSCALE:
-        format = GST_VIDEO_FORMAT_GRAY8;
-        break;
-      default:
-        format = GST_VIDEO_FORMAT_I420;
-        break;
-    }
-  }
-
-  /* Compare to currently configured output state */
-  outstate = gst_video_decoder_get_output_state (GST_VIDEO_DECODER (dec));
-  if (outstate) {
-    info = &outstate->info;
-
-    if (width == GST_VIDEO_INFO_WIDTH (info) &&
-        height == GST_VIDEO_INFO_HEIGHT (info) &&
-        format == GST_VIDEO_INFO_FORMAT (info)) {
-      gst_video_codec_state_unref (outstate);
-      return;
-    }
-    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);
@@ -1082,8 +1044,9 @@ gst_jpeg_dec_negotiate (GstJpegDec * dec, gint width, gint height, gint clrspc,
       case GST_VIDEO_FORMAT_xBGR:
       case GST_VIDEO_FORMAT_BGRA:
       case GST_VIDEO_FORMAT_ABGR:
-        clrspc = JCS_RGB;
-        format = peerfmt;
+        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;
@@ -1091,9 +1054,56 @@ gst_jpeg_dec_negotiate (GstJpegDec * dec, gint width, gint height, gint clrspc,
         break;
     }
   }
-  dec->format = format;
   gst_caps_unref (peer_caps);
   GST_DEBUG_OBJECT (dec, "format_convert=%d", dec->format_convert);
+}
+#endif
+
+static void
+gst_jpeg_dec_negotiate (GstJpegDec * dec, gint width, gint height, gint clrspc,
+    gboolean interlaced)
+{
+  GstVideoCodecState *outstate;
+  GstVideoInfo *info;
+  GstVideoFormat format;
+
+#ifdef JCS_EXTENSIONS
+  if (dec->format_convert) {
+    format = dec->format;
+  } else
+#endif
+  {
+    switch (clrspc) {
+      case JCS_RGB:
+        format = GST_VIDEO_FORMAT_RGB;
+        break;
+      case JCS_GRAYSCALE:
+        format = GST_VIDEO_FORMAT_GRAY8;
+        break;
+      default:
+        format = GST_VIDEO_FORMAT_I420;
+        break;
+    }
+  }
+
+  /* Compare to currently configured output state */
+  outstate = gst_video_decoder_get_output_state (GST_VIDEO_DECODER (dec));
+  if (outstate) {
+    info = &outstate->info;
+
+    if (width == GST_VIDEO_INFO_WIDTH (info) &&
+        height == GST_VIDEO_INFO_HEIGHT (info) &&
+        format == GST_VIDEO_INFO_FORMAT (info)) {
+      gst_video_codec_state_unref (outstate);
+      return;
+    }
+    gst_video_codec_state_unref (outstate);
+  }
+#ifdef JCS_EXTENSIONS
+  /* 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 =