예제 #1
0
 def _set_transform(self, value):
     mp = mmal.MMAL_DISPLAYREGION_T(
         mmal.MMAL_PARAMETER_HEADER_T(mmal.MMAL_PARAMETER_DISPLAYREGION, ct.sizeof(mmal.MMAL_DISPLAYREGION_T)),
         set=mmal.MMAL_DISPLAY_SET_TRANSFORM,
         transform=value,
     )
     mmal_check(mmal.mmal_port_parameter_set(self.renderer[0].input[0], mp.hdr), prefix="Failed to set transform")
예제 #2
0
 def _add_exif_tag(self, tag, value):
     # Format the tag and value into an appropriate bytes string, encoded
     # with the Exif encoding (ASCII)
     if isinstance(tag, str):
         tag = tag.encode(self.exif_encoding)
     if isinstance(value, str):
         value = value.encode(self.exif_encoding)
     elif isinstance(value, datetime.datetime):
         value = value.strftime('%Y:%m:%d %H:%M:%S').encode(self.exif_encoding)
     # MMAL_PARAMETER_EXIF_T is a variable sized structure, hence all the
     # mucking about with string buffers here...
     buf = ct.create_string_buffer(
         ct.sizeof(mmal.MMAL_PARAMETER_EXIF_T) + len(tag) + len(value) + 1)
     mp = ct.cast(buf, ct.POINTER(mmal.MMAL_PARAMETER_EXIF_T))
     mp[0].hdr.id = mmal.MMAL_PARAMETER_EXIF
     mp[0].hdr.size = len(buf)
     if (b'=' in tag or b'\x00' in value):
         data = tag + value
         mp[0].keylen = len(tag)
         mp[0].value_offset = len(tag)
         mp[0].valuelen = len(value)
     else:
         data = tag + b'=' + value
     ct.memmove(mp[0].data, data, len(data))
     mmal_check(
         mmal.mmal_port_parameter_set(self.output_port, mp[0].hdr),
         prefix="Failed to set Exif tag %s" % tag)
예제 #3
0
 def _set_fullscreen(self, value):
     mp = mmal.MMAL_DISPLAYREGION_T(
         mmal.MMAL_PARAMETER_HEADER_T(mmal.MMAL_PARAMETER_DISPLAYREGION, ct.sizeof(mmal.MMAL_DISPLAYREGION_T)),
         set=mmal.MMAL_DISPLAY_SET_FULLSCREEN,
         fullscreen=bool(value),
     )
     mmal_check(mmal.mmal_port_parameter_set(self.renderer[0].input[0], mp.hdr), prefix="Failed to set fullscreen")
예제 #4
0
 def _set_crop(self, value):
     try:
         x, y, w, h = value
     except (TypeError, ValueError) as e:
         raise PiCameraValueError("Invalid crop rectangle (x, y, w, h) tuple: %s" % value)
     mp = mmal.MMAL_DISPLAYREGION_T(
         mmal.MMAL_PARAMETER_HEADER_T(mmal.MMAL_PARAMETER_DISPLAYREGION, ct.sizeof(mmal.MMAL_DISPLAYREGION_T)),
         set=mmal.MMAL_DISPLAY_SET_SRC_RECT,
         src_rect=mmal.MMAL_RECT_T(x, y, w, h),
     )
     mmal_check(mmal.mmal_port_parameter_set(self.renderer[0].input[0], mp.hdr), prefix="Failed to set crop")
예제 #5
0
 def _set_layer(self, value):
     try:
         if not (0 <= value <= 255):
             raise PiCameraValueError("Invalid layer value: %d (valid range 0..255)" % value)
     except TypeError:
         raise PiCameraValueError("Invalid layer value: %s" % value)
     mp = mmal.MMAL_DISPLAYREGION_T(
         mmal.MMAL_PARAMETER_HEADER_T(mmal.MMAL_PARAMETER_DISPLAYREGION, ct.sizeof(mmal.MMAL_DISPLAYREGION_T)),
         set=mmal.MMAL_DISPLAY_SET_LAYER,
         layer=value,
     )
     mmal_check(mmal.mmal_port_parameter_set(self.renderer[0].input[0], mp.hdr), prefix="Failed to set layer")
예제 #6
0
    def _create_encoder(self, quality=85, thumbnail=(64, 48, 35), bayer=False):
        super(PiImageEncoder, self)._create_encoder()

        try:
            self.output_port[0].format[0].encoding = {
                'jpeg': mmal.MMAL_ENCODING_JPEG,
                'png':  mmal.MMAL_ENCODING_PNG,
                'gif':  mmal.MMAL_ENCODING_GIF,
                'bmp':  mmal.MMAL_ENCODING_BMP,
                }[self.format]
        except KeyError:
            raise PiCameraValueError("Unrecognized format %s" % self.format)
        mmal_check(
            mmal.mmal_port_format_commit(self.output_port),
            prefix="Unable to set format on encoder output port")

        if self.format == 'jpeg':
            mmal_check(
                mmal.mmal_port_parameter_set_uint32(
                    self.output_port,
                    mmal.MMAL_PARAMETER_JPEG_Q_FACTOR,
                    quality),
                prefix="Failed to set JPEG quality")

            mmal_check(
                mmal.mmal_port_parameter_set_boolean(
                    self.camera_port,
                    mmal.MMAL_PARAMETER_ENABLE_RAW_CAPTURE,
                    int(bool(bayer))),
                prefix="Failed to set raw capture")

            if thumbnail is None:
                mp = mmal.MMAL_PARAMETER_THUMBNAIL_CONFIG_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_THUMBNAIL_CONFIGURATION,
                        ct.sizeof(mmal.MMAL_PARAMETER_THUMBNAIL_CONFIG_T)
                        ),
                    0, 0, 0, 0)
            else:
                mp = mmal.MMAL_PARAMETER_THUMBNAIL_CONFIG_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_THUMBNAIL_CONFIGURATION,
                        ct.sizeof(mmal.MMAL_PARAMETER_THUMBNAIL_CONFIG_T)
                        ),
                    1, *thumbnail)
            mmal_check(
                mmal.mmal_port_parameter_set(self.encoder[0].control, mp.hdr),
                prefix="Failed to set thumbnail configuration")

        mmal_check(
            mmal.mmal_component_enable(self.encoder),
            prefix="Unable to enable encoder component")
예제 #7
0
 def _set_alpha(self, value):
     try:
         if not (0 <= value <= 255):
             raise PiCameraValueError(
                 "Invalid alpha value: %d (valid range 0..255)" % value)
     except TypeError:
         raise PiCameraValueError("Invalid alpha value: %s" % value)
     mp = mmal.MMAL_DISPLAYREGION_T(mmal.MMAL_PARAMETER_HEADER_T(
         mmal.MMAL_PARAMETER_DISPLAYREGION,
         ct.sizeof(mmal.MMAL_DISPLAYREGION_T)),
                                    set=mmal.MMAL_DISPLAY_SET_ALPHA,
                                    alpha=value)
     mmal_check(mmal.mmal_port_parameter_set(self.renderer[0].input[0],
                                             mp.hdr),
                prefix="Failed to set alpha")
예제 #8
0
    def _create_encoder(self, format, **options):
        super(PiImageEncoder, self)._create_encoder(format, **options)

        try:
            self.output_port[0].format[0].encoding = {
                'jpeg': mmal.MMAL_ENCODING_JPEG,
                'png':  mmal.MMAL_ENCODING_PNG,
                'gif':  mmal.MMAL_ENCODING_GIF,
                'bmp':  mmal.MMAL_ENCODING_BMP,
                }[format]
        except KeyError:
            raise PiCameraValueError("Unrecognized format %s" % format)
        mmal_check(
            mmal.mmal_port_format_commit(self.output_port),
            prefix="Unable to set format on encoder output port")

        if format == 'jpeg':
            mmal_check(
                mmal.mmal_port_parameter_set_uint32(
                    self.output_port,
                    mmal.MMAL_PARAMETER_JPEG_Q_FACTOR,
                    options.get('quality', 85)),
                prefix="Failed to set JPEG quality")

            thumbnail = options.get('thumbnail', (64, 48, 35))
            if thumbnail is None:
                mp = mmal.MMAL_PARAMETER_THUMBNAIL_CONFIG_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_THUMBNAIL_CONFIGURATION,
                        ct.sizeof(mmal.MMAL_PARAMETER_THUMBNAIL_CONFIG_T)
                        ),
                    0, 0, 0, 0)
            else:
                mp = mmal.MMAL_PARAMETER_THUMBNAIL_CONFIG_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_THUMBNAIL_CONFIGURATION,
                        ct.sizeof(mmal.MMAL_PARAMETER_THUMBNAIL_CONFIG_T)
                        ),
                    1, *thumbnail)
            mmal_check(
                mmal.mmal_port_parameter_set(self.encoder[0].control, mp.hdr),
                prefix="Failed to set thumbnail configuration")

        mmal_check(
            mmal.mmal_component_enable(self.encoder),
            prefix="Unable to enable encoder component")
예제 #9
0
    def _create_encoder(self, quality=85, thumbnail=(64, 48, 35), bayer=False):
        super(PiImageEncoder, self)._create_encoder()

        try:
            self.output_port[0].format[0].encoding = {
                'jpeg': mmal.MMAL_ENCODING_JPEG,
                'png': mmal.MMAL_ENCODING_PNG,
                'gif': mmal.MMAL_ENCODING_GIF,
                'bmp': mmal.MMAL_ENCODING_BMP,
            }[self.format]
        except KeyError:
            raise PiCameraValueError("Unrecognized format %s" % self.format)
        mmal_check(mmal.mmal_port_format_commit(self.output_port),
                   prefix="Unable to set format on encoder output port")

        if self.format == 'jpeg':
            mmal_check(mmal.mmal_port_parameter_set_uint32(
                self.output_port, mmal.MMAL_PARAMETER_JPEG_Q_FACTOR, quality),
                       prefix="Failed to set JPEG quality")

            mmal_check(mmal.mmal_port_parameter_set_boolean(
                self.camera_port, mmal.MMAL_PARAMETER_ENABLE_RAW_CAPTURE,
                int(bool(bayer))),
                       prefix="Failed to set raw capture")

            if thumbnail is None:
                mp = mmal.MMAL_PARAMETER_THUMBNAIL_CONFIG_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_THUMBNAIL_CONFIGURATION,
                        ct.sizeof(mmal.MMAL_PARAMETER_THUMBNAIL_CONFIG_T)), 0,
                    0, 0, 0)
            else:
                mp = mmal.MMAL_PARAMETER_THUMBNAIL_CONFIG_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_THUMBNAIL_CONFIGURATION,
                        ct.sizeof(mmal.MMAL_PARAMETER_THUMBNAIL_CONFIG_T)), 1,
                    *thumbnail)
            mmal_check(mmal.mmal_port_parameter_set(self.encoder[0].control,
                                                    mp.hdr),
                       prefix="Failed to set thumbnail configuration")

        mmal_check(mmal.mmal_component_enable(self.encoder),
                   prefix="Unable to enable encoder component")
예제 #10
0
    def _create_encoder(self,
                        bitrate=17000000,
                        intra_period=0,
                        profile='high',
                        quantization=0,
                        inline_headers=True,
                        sei=False):
        super(PiVideoEncoder, self)._create_encoder()

        try:
            self.output_port[0].format[0].encoding = {
                'h264': mmal.MMAL_ENCODING_H264,
                'mjpeg': mmal.MMAL_ENCODING_MJPEG,
            }[self.format]
        except KeyError:
            raise PiCameraValueError('Unrecognized format %s' % self.format)

        if not (0 <= bitrate <= 25000000):
            raise PiCameraValueError(
                'bitrate must be between 0 (VBR) and 25Mbps')
        if quantization and bitrate:
            warnings.warn('Setting bitrate to 0 as quantization is non-zero',
                          PiCameraWarning)
            bitrate = 0
        self.output_port[0].format[0].bitrate = bitrate
        mmal_check(mmal.mmal_port_format_commit(self.output_port),
                   prefix="Unable to set format on encoder output port")

        if self.format == 'h264':
            mp = mmal.MMAL_PARAMETER_VIDEO_PROFILE_T(
                mmal.MMAL_PARAMETER_HEADER_T(
                    mmal.MMAL_PARAMETER_PROFILE,
                    ct.sizeof(mmal.MMAL_PARAMETER_VIDEO_PROFILE_T),
                ), )
            try:
                mp.profile[0].profile = {
                    'baseline':
                    mmal.MMAL_VIDEO_PROFILE_H264_BASELINE,
                    'main':
                    mmal.MMAL_VIDEO_PROFILE_H264_MAIN,
                    'high':
                    mmal.MMAL_VIDEO_PROFILE_H264_HIGH,
                    'constrained':
                    mmal.MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE,
                }[profile]
            except KeyError:
                raise PiCameraValueError("Invalid H.264 profile %s" % profile)
            mp.profile[0].level = mmal.MMAL_VIDEO_LEVEL_H264_4
            mmal_check(mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                       prefix="Unable to set encoder H.264 profile")

            mmal_check(mmal.mmal_port_parameter_set_boolean(
                self.output_port,
                mmal.MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER,
                int(inline_headers)),
                       prefix="Unable to set inline_headers")

            mmal_check(mmal.mmal_port_parameter_set_boolean(
                self.output_port, mmal.MMAL_PARAMETER_VIDEO_ENCODE_SEI_ENABLE,
                int(sei)),
                       prefix="Enable to set SEI")

            if not (bitrate and inline_headers):
                # If inline_headers is disabled, or VBR encoding is configured,
                # disable the split function
                self._next_output = None

            if intra_period:
                mp = mmal.MMAL_PARAMETER_UINT32_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_INTRAPERIOD,
                        ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T),
                    ), intra_period)
                mmal_check(mmal.mmal_port_parameter_set(
                    self.output_port, mp.hdr),
                           prefix="Unable to set encoder intra_period")

        if quantization:
            mp = mmal.MMAL_PARAMETER_UINT32_T(
                mmal.MMAL_PARAMETER_HEADER_T(
                    mmal.MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT,
                    ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T),
                ), quantization)
            mmal_check(mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                       prefix="Unable to set initial quantization")
            mp = mmal.MMAL_PARAMETER_UINT32_T(
                mmal.MMAL_PARAMETER_HEADER_T(
                    mmal.MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT,
                    ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T),
                ),
                quantization,
            )
            mmal_check(mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                       prefix="Unable to set minimum quantization")
            mp = mmal.MMAL_PARAMETER_UINT32_T(
                mmal.MMAL_PARAMETER_HEADER_T(
                    mmal.MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT,
                    ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T),
                ),
                quantization,
            )
            mmal_check(mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                       prefix="Unable to set maximum quantization")

        mmal_check(mmal.mmal_port_parameter_set_boolean(
            self.encoder[0].input[0],
            mmal.MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT, 1),
                   prefix="Unable to set immutable flag on encoder input port")

        mmal_check(mmal.mmal_component_enable(self.encoder),
                   prefix="Unable to enable video encoder component")
예제 #11
0
파일: encoders.py 프로젝트: isls/lernen
    def _create_encoder(
        self, bitrate=17000000, intra_period=0, profile="high", quantization=0, inline_headers=True, **options
    ):
        super(PiVideoEncoder, self)._create_encoder(**options)

        try:
            self.output_port[0].format[0].encoding = {
                "h264": mmal.MMAL_ENCODING_H264,
                "mjpeg": mmal.MMAL_ENCODING_MJPEG,
            }[self.format]
        except KeyError:
            raise PiCameraValueError("Unrecognized format %s" % self.format)

        if not (0 <= bitrate <= 25000000):
            raise PiCameraValueError("bitrate must be between 0 (VBR) and 25Mbps")
        if quantization and bitrate:
            warnings.warn("Setting bitrate to 0 as quantization is non-zero", PiCameraWarning)
            bitrate = 0
        self.output_port[0].format[0].bitrate = bitrate
        mmal_check(mmal.mmal_port_format_commit(self.output_port), prefix="Unable to set format on encoder output port")

        if self.format == "h264":
            mp = mmal.MMAL_PARAMETER_VIDEO_PROFILE_T(
                mmal.MMAL_PARAMETER_HEADER_T(
                    mmal.MMAL_PARAMETER_PROFILE, ct.sizeof(mmal.MMAL_PARAMETER_VIDEO_PROFILE_T)
                )
            )
            try:
                mp.profile[0].profile = {
                    "baseline": mmal.MMAL_VIDEO_PROFILE_H264_BASELINE,
                    "main": mmal.MMAL_VIDEO_PROFILE_H264_MAIN,
                    "high": mmal.MMAL_VIDEO_PROFILE_H264_HIGH,
                    "constrained": mmal.MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE,
                }[profile]
            except KeyError:
                raise PiCameraValueError("Invalid H.264 profile %s" % profile)
            mp.profile[0].level = mmal.MMAL_VIDEO_LEVEL_H264_4
            mmal_check(
                mmal.mmal_port_parameter_set(self.output_port, mp.hdr), prefix="Unable to set encoder H.264 profile"
            )

            mmal_check(
                mmal.mmal_port_parameter_set_boolean(
                    self.output_port, mmal.MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER, int(inline_headers)
                ),
                prefix="Unable to set inline_headers",
            )

            if not (bitrate and inline_headers):
                # If inline_headers is disabled, or VBR encoding is configured,
                # disable the split function
                self._next_output = None

            if intra_period:
                mp = mmal.MMAL_PARAMETER_UINT32_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_INTRAPERIOD, ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T)
                    ),
                    intra_period,
                )
                mmal_check(
                    mmal.mmal_port_parameter_set(self.output_port, mp.hdr), prefix="Unable to set encoder intra_period"
                )

        if quantization:
            mp = mmal.MMAL_PARAMETER_UINT32_T(
                mmal.MMAL_PARAMETER_HEADER_T(
                    mmal.MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT, ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T)
                ),
                quantization,
            )
            mmal_check(
                mmal.mmal_port_parameter_set(self.output_port, mp.hdr), prefix="Unable to set initial quantization"
            )
            mp = mmal.MMAL_PARAMETER_UINT32_T(
                mmal.MMAL_PARAMETER_HEADER_T(
                    mmal.MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT, ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T)
                ),
                quantization,
            )
            mmal_check(
                mmal.mmal_port_parameter_set(self.output_port, mp.hdr), prefix="Unable to set minimum quantization"
            )
            mp = mmal.MMAL_PARAMETER_UINT32_T(
                mmal.MMAL_PARAMETER_HEADER_T(
                    mmal.MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT, ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T)
                ),
                quantization,
            )
            mmal_check(
                mmal.mmal_port_parameter_set(self.output_port, mp.hdr), prefix="Unable to set maximum quantization"
            )

        mmal_check(
            mmal.mmal_port_parameter_set_boolean(
                self.encoder[0].input[0], mmal.MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT, 1
            ),
            prefix="Unable to set immutable flag on encoder input port",
        )

        mmal_check(mmal.mmal_component_enable(self.encoder), prefix="Unable to enable video encoder component")
예제 #12
0
    def _create_encoder(
            self, bitrate=17000000, intra_period=0, profile='high',
            quantization=0, inline_headers=True, sei=False):
        super(PiVideoEncoder, self)._create_encoder()

        try:
            self.output_port[0].format[0].encoding = {
                'h264':  mmal.MMAL_ENCODING_H264,
                'mjpeg': mmal.MMAL_ENCODING_MJPEG,
                }[self.format]
        except KeyError:
            raise PiCameraValueError('Unrecognized format %s' % self.format)

        if not (0 <= bitrate <= 25000000):
            raise PiCameraValueError('bitrate must be between 0 (VBR) and 25Mbps')
        if quantization and bitrate:
            warnings.warn('Setting bitrate to 0 as quantization is non-zero', PiCameraWarning)
            bitrate = 0
        self.output_port[0].format[0].bitrate = bitrate
        mmal_check(
            mmal.mmal_port_format_commit(self.output_port),
            prefix="Unable to set format on encoder output port")

        if self.format == 'h264':
            mp = mmal.MMAL_PARAMETER_VIDEO_PROFILE_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_PROFILE,
                        ct.sizeof(mmal.MMAL_PARAMETER_VIDEO_PROFILE_T),
                        ),
                    )
            try:
                mp.profile[0].profile = {
                    'baseline':    mmal.MMAL_VIDEO_PROFILE_H264_BASELINE,
                    'main':        mmal.MMAL_VIDEO_PROFILE_H264_MAIN,
                    'high':        mmal.MMAL_VIDEO_PROFILE_H264_HIGH,
                    'constrained': mmal.MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE,
                }[profile]
            except KeyError:
                raise PiCameraValueError("Invalid H.264 profile %s" % profile)
            mp.profile[0].level = mmal.MMAL_VIDEO_LEVEL_H264_4
            mmal_check(
                mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                prefix="Unable to set encoder H.264 profile")

            mmal_check(
                mmal.mmal_port_parameter_set_boolean(
                    self.output_port,
                    mmal.MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER,
                    int(inline_headers)),
                prefix="Unable to set inline_headers")

            mmal_check(
                mmal.mmal_port_parameter_set_boolean(
                    self.output_port,
                    mmal.MMAL_PARAMETER_VIDEO_ENCODE_SEI_ENABLE,
                    int(sei)),
                prefix="Enable to set SEI")

            if not (bitrate and inline_headers):
                # If inline_headers is disabled, or VBR encoding is configured,
                # disable the split function
                self._next_output = None

            # We need the intra-period to calculate the SPS header timeout in
            # the split method below. If one is not set explicitly, query the
            # encoder's default
            if intra_period:
                mp = mmal.MMAL_PARAMETER_UINT32_T(
                        mmal.MMAL_PARAMETER_HEADER_T(
                            mmal.MMAL_PARAMETER_INTRAPERIOD,
                            ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T),
                            ),
                        intra_period
                        )
                mmal_check(
                    mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                    prefix="Unable to set encoder intra_period")
                self._intra_period = intra_period
            else:
                mp = mmal.MMAL_PARAMETER_UINT32_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_INTRAPERIOD,
                        ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T),
                        ))
                mmal_check(
                    mmal.mmal_port_parameter_get(self.output_port, mp.hdr),
                    prefix="Unable to get encoder intra_period")
                self._intra_period = mp.value

        elif self.format == 'mjpeg':
            # MJPEG doesn't have an intra_period setting as such, but as every
            # frame is a full-frame, the intra_period is effectively 1
            self._intra_period = 1

        if quantization:
            mp = mmal.MMAL_PARAMETER_UINT32_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT,
                        ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T),
                        ),
                    quantization
                    )
            mmal_check(
                mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                prefix="Unable to set initial quantization")
            mp = mmal.MMAL_PARAMETER_UINT32_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT,
                        ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T),
                        ),
                    quantization,
                    )
            mmal_check(
                mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                prefix="Unable to set minimum quantization")
            mp = mmal.MMAL_PARAMETER_UINT32_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT,
                        ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T),
                        ),
                    quantization,
                    )
            mmal_check(
                mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                prefix="Unable to set maximum quantization")

        mmal_check(
            mmal.mmal_port_parameter_set_boolean(
                self.encoder[0].input[0],
                mmal.MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
                1),
            prefix="Unable to set immutable flag on encoder input port")

        mmal_check(
            mmal.mmal_component_enable(self.encoder),
            prefix="Unable to enable video encoder component")
예제 #13
0
    def _create_encoder(
            self, format, bitrate=17000000, intra_period=0, profile='high',
            quantization=0, inline_headers=True, **options):
        super(PiVideoEncoder, self)._create_encoder(format, **options)

        try:
            self.output_port[0].format[0].encoding = {
                'h264': mmal.MMAL_ENCODING_H264,
                }[format]
        except KeyError:
            raise PiCameraValueError('Unrecognized format %s' % format)

        if not (0 <= bitrate <= 25000000):
            raise PiCameraValueError('bitrate must be between 0 (VBR) and 25Mbps')
        if quantization and bitrate:
            warnings.warn('Setting bitrate to 0 as quantization is non-zero', PiCameraWarning)
            bitrate = 0
        self.output_port[0].format[0].bitrate = bitrate
        mmal_check(
            mmal.mmal_port_format_commit(self.output_port),
            prefix="Unable to set format on encoder output port")

        if intra_period:
            mp = mmal.MMAL_PARAMETER_UINT32_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_INTRAPERIOD,
                        ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T),
                        ),
                    intra_period
                    )
            mmal_check(
                mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                prefix="Unable to set encoder intra_period")

        if quantization:
            mp = mmal.MMAL_PARAMETER_UINT32_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT,
                        ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T),
                        ),
                    quantization
                    )
            mmal_check(
                mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                prefix="Unable to set quantization")
            mp = mmal.MMAL_PARAMETER_UINT32_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_VIDEO_ENCODE_QP_P,
                        ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T),
                        ),
                    quantization + 6,
                    )
            mmal_check(
                mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                prefix="Unable to set quantization")

        mp = mmal.MMAL_PARAMETER_VIDEO_PROFILE_T(
                mmal.MMAL_PARAMETER_HEADER_T(
                    mmal.MMAL_PARAMETER_PROFILE,
                    ct.sizeof(mmal.MMAL_PARAMETER_VIDEO_PROFILE_T),
                    ),
                )
        try:
            mp.profile[0].profile = {
                'baseline':    mmal.MMAL_VIDEO_PROFILE_H264_BASELINE,
                'main':        mmal.MMAL_VIDEO_PROFILE_H264_MAIN,
                'high':        mmal.MMAL_VIDEO_PROFILE_H264_HIGH,
                'constrained': mmal.MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE,
            }[profile]
        except KeyError:
            raise PiCameraValueError("Invalid H.264 profile %s" % profile)
        mp.profile[0].level = mmal.MMAL_VIDEO_LEVEL_H264_4
        mmal_check(
            mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
            prefix="Unable to set encoder H.264 profile")

        mmal_check(
            mmal.mmal_port_parameter_set_boolean(
                self.input_port,
                mmal.MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
                1),
            prefix="Unable to set immutable flag on encoder input port")

        mmal_check(
            mmal.mmal_port_parameter_set_boolean(
                self.output_port,
                mmal.MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER,
                int(inline_headers)),
            prefix="Unable to set inline_headers")

        if not (bitrate and inline_headers):
            # If inline_headers is disabled, or VBR encoding is configured,
            # disable the split function
            self._next_output = None

        mmal_check(
            mmal.mmal_component_enable(self.encoder),
            prefix="Unable to enable video encoder component")
예제 #14
0
    def _create_encoder(self, format, **options):
        super(PiVideoEncoder, self)._create_encoder(format, **options)

        try:
            self.output_port[0].format[0].encoding = {
                'h264': mmal.MMAL_ENCODING_H264,
                }[format]
        except KeyError:
            raise PiCameraValueError('Unrecognized format %s' % format)
        bitrate = options.get('bitrate', 17000000)
        if bitrate > 25000000:
            raise PiCameraValueError('25Mbps is the maximum bitrate')
        self.output_port[0].format[0].bitrate = bitrate
        mmal_check(
            mmal.mmal_port_format_commit(self.output_port),
            prefix="Unable to set format on encoder output port")

        if 'intraperiod' in options:
            mp = mmal.MMAL_PARAMETER_UINT32_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_INTRAPERIOD,
                        ct.sizeof(mmal.MMAL_PARAMETER_UINT32_T),
                        ),
                    int(options['intraperiod'])
                    )
            mmal_check(
                mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                prefix="Unable to set encoder intraperiod")

        if 'profile' in options:
            mp = mmal.MMAL_PARAMETER_VIDEO_PROFILE_T(
                    mmal.MMAL_PARAMETER_HEADER_T(
                        mmal.MMAL_PARAMETER_PROFILE,
                        ct.sizeof(mmal.MMAL_PARAMETER_VIDEO_PROFILE_T),
                        ),
                    )
            mp.profile[0].profile = {
                'baseline':    mmal.MMAL_VIDEO_PROFILE_H264_BASELINE,
                'main':        mmal.MMAL_VIDEO_PROFILE_H264_MAIN,
                'high':        mmal.MMAL_VIDEO_PROFILE_H264_HIGH,
                'constrained': mmal.MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE,
            }[options['profile']]
            mp.profile[0].level = mmal.MMAL_VIDEO_LEVEL_H264_4
            mmal_check(
                mmal.mmal_port_parameter_set(self.output_port, mp.hdr),
                prefix="Unable to set encoder H.264 profile")

        # XXX Why does this fail? Is it even needed?
        #try:
        #    mmal_check(
        #        mmal.mmal_port_parameter_set_boolean(
        #            enc_in,
        #            mmal.MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
        #            1),
        #        prefix="Unable to set immutable flag on encoder input port")
        #except PiCameraError as e:
        #    print(str(e))
        #    # Continue rather than abort...

        mmal_check(
            mmal.mmal_component_enable(self.encoder),
            prefix="Unable to enable video encoder component")