def do_fixate_caps(self, direction: Gst.PadDirection, caps: Gst.Caps, othercaps: Gst.Caps) -> Gst.Caps:
        """
            caps: initial caps
            othercaps: target caps
        """
        if direction == Gst.PadDirection.SRC:
            return othercaps.fixate()
        else:
            # Fixate only output caps
            in_width, in_height = [caps.get_structure(0).get_value(v) for v in ['width', 'height']]

            if (self._left + self._right) > in_width:
                raise ValueError("Left and Right Bounds exceed Input Width")

            if (self._bottom + self._top) > in_height:
                raise ValueError("Top and Bottom Bounds exceed Input Height")

            width = in_width - self._left - self._right
            height = in_height - self._top - self._bottom

            new_format = othercaps.get_structure(0).copy()

            # https://lazka.github.io/pgi-docs/index.html#Gst-1.0/classes/Structure.html#Gst.Structure.fixate_field_nearest_int
            new_format.fixate_field_nearest_int("width", width)
            new_format.fixate_field_nearest_int("height", height)
            new_caps = Gst.Caps.new_empty()
            new_caps.append_structure(new_format)

            # https://lazka.github.io/pgi-docs/index.html#Gst-1.0/classes/Caps.html#Gst.Caps.fixate
            return new_caps.fixate()
    def do_set_caps(self, incaps: Gst.Caps, outcaps: Gst.Caps) -> bool:

        in_w, in_h = [incaps.get_structure(0).get_value(v) for v in ['width', 'height']]
        out_w, out_h = [outcaps.get_structure(0).get_value(v) for v in ['width', 'height']]

        # if input_size == output_size set plugin to passthrough mode
        # https://gstreamer.freedesktop.org/documentation/additional/design/element-transform.html?gi-language=c#processing
        if in_h == out_h and in_w == out_w:
            self.set_passthrough(True)

        return True
Example #3
0
def gst_buffer_with_caps_for_tensor(buffer: Gst.Buffer,
                                    caps: Gst.Caps) -> np.ndarray:
    """ Converts Gst.Buffer with Gst.Caps (stores buffer info) to np.ndarray """

    structure = caps.get_structure(0)  # Gst.Structure

    width, height = structure.get_value("width"), structure.get_value("height")

    # GstVideo.VideoFormat
    video_format = utils.gst_video_format_from_string(
        structure.get_value('format'))

    channels = utils.get_num_channels(video_format)

    dtype = get_np_dtype(video_format)  # np.dtype

    format_info = GstVideo.VideoFormat.get_info(
        video_format)  # GstVideo.VideoFormatInfo

    return gst_buffer_for_tensor(buffer,
                                 width=width,
                                 height=height,
                                 channels=channels,
                                 dtype=dtype,
                                 bpp=format_info.bits)
Example #4
0
    def do_fixate(self, caps: Gst.Caps) -> Gst.Caps:
        Gst.info("Fixating caps")

        structure = caps.get_structure(0).copy()

        Gst.info(f"Incoming caps: {structure}")

        genicam_format = self.get_cam_node_val("PixelFormat")
        structure.fixate_field_string(
            "format",
            self.get_format_from_genicam(genicam_format).gst)
        self.set_cam_node_val(
            "PixelFormat",
            self.get_format_from_gst(structure.get_value("format")).genicam,
        )

        height = self.get_cam_node_val("Height")
        structure.fixate_field_nearest_int("height", height)
        self.set_cam_node_val("Height", structure.get_value("height"))

        if self.center_y:
            self.set_cam_node_val(
                "OffsetY",
                (self.get_cam_node_range("Height")[1] -
                 self.get_cam_node_val("Height")) // 2,
            )
        else:
            self.set_cam_node_val("OffsetY", self.offset_y)

        width = self.get_cam_node_val("Width")
        structure.fixate_field_nearest_int("width", width)
        self.set_cam_node_val("Width", structure.get_value("width"))

        if self.center_x:
            self.set_cam_node_val(
                "OffsetX",
                (self.get_cam_node_range("Width")[1] -
                 self.get_cam_node_val("Width")) // 2,
            )
        else:
            self.set_cam_node_val("OffsetX", self.offset_x)

        if self.cam_node_available("AcquisitionFrameRateEnable"):
            self.set_cam_node_val("AcquisitionFrameRateEnable", True)
        else:
            self.set_cam_node_val("AcquisitionFrameRateAuto", "Off")
            self.set_cam_node_val("AcquisitionFrameRateEnabled", True)

        frame_rate = self.get_cam_node_val("AcquisitionFrameRate")
        structure.fixate_field_nearest_fraction("framerate", frame_rate, 1)
        self.set_cam_node_val("AcquisitionFrameRate",
                              float(structure.get_value("framerate")), True)

        Gst.info(f"Fixated caps: {structure}")

        new_caps = Gst.Caps.new_empty()
        new_caps.append_structure(structure)

        return new_caps.fixate()
Example #5
0
    def caps_to_fmt_string(caps: Gst.Caps):
        """
        Convert Gst.Caps to a string that is useable in file names.
        """

        if not caps or not caps.is_fixed():
            return ""

        fmt = caps.get_structure(0)
        width = fmt.get_value("width")
        height = fmt.get_value("height")
        frmt = fmt.get_value("format")

        fps_str = re.search("\d{1,2}/\d", fmt.to_string())

        fps_str = fps_str.group(0)

        return "{}_{}x{}_{}_{}".format(str(frmt), str(width), str(height),
                                       fps_str[:fps_str.find("/")],
                                       fps_str[fps_str.find("/") + 1:])
    def caps_to_fmt_string(caps: Gst.Caps):
        """
        Convert Gst.Caps to a string that is useable in file names.
        """

        if not caps or not caps.is_fixed():
            return ""

        fmt = caps.get_structure(0)
        width = fmt.get_value("width")
        height = fmt.get_value("height")
        frmt = fmt.get_value("format")

        fps_str = re.search("\d{1,2}/\d", fmt.to_string())

        fps_str = fps_str.group(0)

        return "{}_{}x{}_{}_{}".format(str(frmt),
                                       str(width),
                                       str(height),
                                       fps_str[:fps_str.find("/")],
                                       fps_str[fps_str.find("/")+1:])
Example #7
0
def gst_buffer_with_caps_to_ndarray(buffer: Gst.Buffer, caps: Gst.Caps) -> np.ndarray:
    """ Converts Gst.Buffer with Gst.Caps (stores buffer info) to np.ndarray """

    structure = caps.get_structure(0)  # Gst.Structure

    width, height = structure.get_value("width"), structure.get_value("height")
    # print('gst_buffer_with_caps_to_ndarray', width, height)
    
    video_format = None
    video_format = gst_video_format_from_string(structure.get_value('format'))

    channels = get_num_channels(video_format)
    dtype = get_np_dtype(video_format)  # np.dtype

    return gst_buffer_to_ndarray(buffer, width=width, height=height, channels=channels, dtype=dtype)
Example #8
0
def get_buffer_size(caps: Gst.Caps) -> Tuple[bool, Tuple[int, int]]:
    """
        Get Gst.Buffer's (width, height) from Gst.Caps

        :param caps:
        :type caps: Gst.Caps

        :rtype: bool (success flag)
        :rtype: tuple (int, int) -> (width, height)
    """

    caps_struct = caps.get_structure(0)
    (success, width) = caps_struct.get_int('width')
    if not success:
        return False, (0, 0)
    (success, height) = caps_struct.get_int('height')
    if not success:
        return False, (0, 0)
    return True, (width, height)
Example #9
0
def get_buffer_size_from_gst_caps(caps: Gst.Caps) -> typ.Tuple[int, int]:
    """Returns buffers width, height from Gst.Caps """
    structure = caps.get_structure(0)  # Gst.Structure
    return structure.get_value("width"), structure.get_value("height")