This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#47. Add rawcam port configuration types.
- Loading branch information
Showing
5 changed files
with
414 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// <copyright file="MMALRawcamComponent.cs" company="Techyian"> | ||
// Copyright (c) Ian Auty. All rights reserved. | ||
// Licensed under the MIT License. Please see LICENSE.txt for License info. | ||
// </copyright> | ||
|
||
using MMALSharp.Ports.Inputs; | ||
using MMALSharp.Native; | ||
using System; | ||
using MMALSharp.Ports.Outputs; | ||
using MMALSharp.Ports; | ||
using MMALSharp.Handlers; | ||
using System.Runtime.InteropServices; | ||
using MMALSharp.Common.Utility; | ||
using Microsoft.Extensions.Logging; | ||
using static MMALSharp.MMALNativeExceptionHelper; | ||
using static MMALSharp.Native.MMALParameters; | ||
|
||
namespace MMALSharp.Components | ||
{ | ||
/// <summary> | ||
/// ---------------------------------------------------------------------------------------- | ||
/// PLEASE NOTE: THIS IS AN EXPERIMENTAL COMPONENT AND IS NOT BASED ON PRODUCTION READY CODE - https://www.raspberrypi.org/forums/viewtopic.php?f=43&t=109137. | ||
/// ---------------------------------------------------------------------------------------- | ||
/// This component interfaces directly to the camera receiver peripheral to | ||
/// pass the image data and metadata that is received over CSI, CCP2, or CPI. | ||
/// It does not use the ISP or the VPU to perform any form of processing on the image. | ||
/// There are a few options within the peripheral for converting between Bayer | ||
/// bit depths, and packing/unpacking DPCM data - that functionality is exposed. | ||
/// https://github.com/raspberrypi/firmware/blob/master/documentation/ilcomponents/rawcam.html | ||
/// </summary> | ||
public class MMALRawcamComponent : MMALDownstreamHandlerComponent | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of the <see cref="MMALRawcamComponent"/> class. | ||
/// </summary> | ||
public unsafe MMALRawcamComponent() | ||
: base(MMAL_COMPONENT_RAWCAM) | ||
{ | ||
// Default to use still image port behaviour. | ||
this.Inputs.Add(new InputPort((IntPtr)(&(*this.Ptr->Input[0])), this, Guid.NewGuid())); | ||
this.Outputs.Add(new StillPort((IntPtr)(&(*this.Ptr->Output[0])), this, Guid.NewGuid())); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new instance of the <see cref="MMALRawcamComponent"/> class. | ||
/// </summary> | ||
/// <param name="outputPortType">The user defined output port type.</param> | ||
public unsafe MMALRawcamComponent(Type outputPortType) | ||
: base(MMAL_COMPONENT_RAWCAM) | ||
{ | ||
this.Inputs.Add(new InputPort((IntPtr)(&(*this.Ptr->Input[0])), this, Guid.NewGuid())); | ||
this.Outputs.Add((IOutputPort)Activator.CreateInstance(outputPortType, (IntPtr)(&(*this.Ptr->Output[0])), this, Guid.NewGuid())); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override IDownstreamComponent ConfigureOutputPort(int outputPort, IMMALPortConfig config, IOutputCaptureHandler handler) | ||
{ | ||
if (config is MMALRawcamPortConfig) | ||
{ | ||
var rawcamConfig = config as MMALRawcamPortConfig; | ||
|
||
this.ConfigureCameraInterface(rawcamConfig.CameraInterface); | ||
this.ConfigureCameraClockingMode(rawcamConfig.ClockingMode); | ||
this.ConfigureCameraRxConfig(rawcamConfig.RxConfig); | ||
this.ConfigureTimingRegisters(rawcamConfig.TimingConfig); | ||
} | ||
else | ||
{ | ||
MMALLog.Logger.LogWarning($"Rawcam component should be given port configuration of type {nameof(MMALRawcamPortConfig)}. Defaults will be used."); | ||
} | ||
|
||
return base.ConfigureOutputPort(outputPort, config, handler); | ||
} | ||
|
||
private unsafe void ConfigureCameraInterface(MMAL_CAMERA_INTERFACE_T cameraInterface) | ||
{ | ||
MMAL_PARAMETER_CAMERA_INTERFACE_T param = new MMAL_PARAMETER_CAMERA_INTERFACE_T(new MMAL_PARAMETER_HEADER_T(MMALParametersCamera.MMAL_PARAMETER_CAMERA_INTERFACE, Marshal.SizeOf<MMAL_PARAMETER_CAMERA_INTERFACE_T>()), cameraInterface); | ||
|
||
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(param)); | ||
|
||
Marshal.StructureToPtr(param, ptr, false); | ||
|
||
try | ||
{ | ||
MMALCheck( | ||
MMALPort.mmal_port_parameter_set(this.Inputs[0].Ptr, (MMAL_PARAMETER_HEADER_T*)ptr), | ||
"Unable to set camera interface type."); | ||
} | ||
finally | ||
{ | ||
Marshal.FreeHGlobal(ptr); | ||
} | ||
} | ||
|
||
private unsafe void ConfigureCameraClockingMode(MMAL_CAMERA_CLOCKING_MODE_T clockingMode) | ||
{ | ||
MMAL_PARAMETER_CAMERA_CLOCKING_MODE_T param = new MMAL_PARAMETER_CAMERA_CLOCKING_MODE_T(new MMAL_PARAMETER_HEADER_T(MMALParametersCamera.MMAL_PARAMETER_CAMERA_CLOCKING_MODE, Marshal.SizeOf<MMAL_PARAMETER_CAMERA_CLOCKING_MODE_T>()), clockingMode); | ||
|
||
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(param)); | ||
|
||
Marshal.StructureToPtr(param, ptr, false); | ||
|
||
try | ||
{ | ||
MMALCheck( | ||
MMALPort.mmal_port_parameter_set(this.Inputs[0].Ptr, (MMAL_PARAMETER_HEADER_T*)ptr), | ||
"Unable to set camera clocking mode."); | ||
} | ||
finally | ||
{ | ||
Marshal.FreeHGlobal(ptr); | ||
} | ||
} | ||
|
||
private unsafe void ConfigureCameraRxConfig(MMALRawcamRxConfig rxConfig) | ||
{ | ||
MMAL_PARAMETER_CAMERA_RX_CONFIG_T param = new MMAL_PARAMETER_CAMERA_RX_CONFIG_T(new MMAL_PARAMETER_HEADER_T(MMALParametersCamera.MMAL_PARAMETER_CAMERA_RX_CONFIG, Marshal.SizeOf<MMAL_PARAMETER_CAMERA_RX_CONFIG_T>()), rxConfig.DecodeConfig, | ||
rxConfig.EncodeConfig, rxConfig.UnpackConfig, rxConfig.PackConfig, rxConfig.DataLanes, rxConfig.EncodeBlockLength, rxConfig.EmbeddedDataLines, rxConfig.ImageId); | ||
|
||
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(param)); | ||
|
||
Marshal.StructureToPtr(param, ptr, false); | ||
|
||
try | ||
{ | ||
MMALCheck( | ||
MMALPort.mmal_port_parameter_set(this.Inputs[0].Ptr, (MMAL_PARAMETER_HEADER_T*)ptr), | ||
"Unable to set camera peripheral config."); | ||
} | ||
finally | ||
{ | ||
Marshal.FreeHGlobal(ptr); | ||
} | ||
} | ||
|
||
private unsafe void ConfigureTimingRegisters(MMALRawcamTimingConfig timingConfig) | ||
{ | ||
MMAL_PARAMETER_CAMERA_RX_TIMING_T param = new MMAL_PARAMETER_CAMERA_RX_TIMING_T(new MMAL_PARAMETER_HEADER_T(MMALParametersCamera.MMAL_PARAMETER_CAMERA_RX_TIMING, Marshal.SizeOf<MMAL_PARAMETER_CAMERA_RX_TIMING_T>()), timingConfig.Timing1, | ||
timingConfig.Timing2, timingConfig.Timing3, timingConfig.Timing4, timingConfig.Timing5, timingConfig.Term1, timingConfig.Term2, | ||
timingConfig.CpiTiming1, timingConfig.CpiTiming2); | ||
|
||
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(param)); | ||
|
||
Marshal.StructureToPtr(param, ptr, false); | ||
|
||
try | ||
{ | ||
MMALCheck( | ||
MMALPort.mmal_port_parameter_set(this.Inputs[0].Ptr, (MMAL_PARAMETER_HEADER_T*)ptr), | ||
"Unable to set camera timing registers."); | ||
} | ||
finally | ||
{ | ||
Marshal.FreeHGlobal(ptr); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// <copyright file="MMALRawcamPortConfig.cs" company="Techyian"> | ||
// Copyright (c) Ian Auty. All rights reserved. | ||
// Licensed under the MIT License. Please see LICENSE.txt for License info. | ||
// </copyright> | ||
|
||
using MMALSharp.Native; | ||
using System; | ||
|
||
namespace MMALSharp.Ports | ||
{ | ||
/// <summary> | ||
/// Represents a port configuration object for use with the Rawcam component. | ||
/// </summary> | ||
public class MMALRawcamPortConfig : MMALPortConfig | ||
{ | ||
/// <summary> | ||
/// The physical camera interface type. | ||
/// </summary> | ||
public MMAL_CAMERA_INTERFACE_T CameraInterface { get; set; } | ||
|
||
/// <summary> | ||
/// Camera peripheral clocking mode. | ||
/// </summary> | ||
public MMAL_CAMERA_CLOCKING_MODE_T ClockingMode { get; set; } | ||
|
||
/// <summary> | ||
/// The receiver peripheral configuration for unpacking/packing DPCM, and decoding or encoding Bayer images. | ||
/// </summary> | ||
public MMALRawcamRxConfig RxConfig { get; set; } | ||
|
||
/// <summary> | ||
/// Camera peripheral timing registers. | ||
/// </summary> | ||
public MMALRawcamTimingConfig TimingConfig { get; set; } | ||
|
||
/// <summary> | ||
/// Create a new instance of <see cref="MMALRawcamPortConfig"/>. | ||
/// </summary> | ||
/// <param name="encodingType">The encoding type. Set this to specify the output format. Colour format should be | ||
/// RGB565, RGB888, ABGR8888, YUV420 packed planar, YUV422 packed | ||
/// planar, or one of the flavours of YUYV.</param> | ||
/// <param name="pixelFormat">The pixel format.</param> | ||
/// <param name="bitrate">The output bitrate.</param> | ||
/// <param name="timeout">Video record timeout.</param> | ||
/// <param name="cameraInterface">The physical camera interface type.</param> | ||
/// <param name="clockingMode">Camera peripheral clocking mode.</param> | ||
/// <param name="rxConfig">The receiver peripheral configuration for unpacking/packing DPCM, and decoding or encoding Bayer images.</param> | ||
/// <param name="timingConfig">Camera peripheral timing registers.</param> | ||
public MMALRawcamPortConfig(MMALEncoding encodingType, MMALEncoding pixelFormat, int bitrate, DateTime? timeout, | ||
MMAL_CAMERA_INTERFACE_T cameraInterface, MMAL_CAMERA_CLOCKING_MODE_T clockingMode, MMALRawcamRxConfig rxConfig, | ||
MMALRawcamTimingConfig timingConfig) | ||
: base(encodingType, pixelFormat, 0, bitrate, timeout, null, false) | ||
{ | ||
this.CameraInterface = cameraInterface; | ||
this.ClockingMode = clockingMode; | ||
this.RxConfig = rxConfig; | ||
this.TimingConfig = timingConfig; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// <copyright file="MMALRawcamRxConfig.cs" company="Techyian"> | ||
// Copyright (c) Ian Auty. All rights reserved. | ||
// Licensed under the MIT License. Please see LICENSE.txt for License info. | ||
// </copyright> | ||
|
||
using MMALSharp.Native; | ||
|
||
namespace MMALSharp.Ports | ||
{ | ||
/// <summary> | ||
/// The receiver peripheral configuration for unpacking/packing DPCM, and decoding or encoding Bayer images. | ||
/// </summary> | ||
public class MMALRawcamRxConfig | ||
{ | ||
/// <summary> | ||
/// Bayer decoding value. | ||
/// </summary> | ||
public MMAL_CAMERA_RX_CONFIG_DECODE DecodeConfig { get; set; } | ||
|
||
/// <summary> | ||
/// Bayer encoding value. | ||
/// </summary> | ||
public MMAL_CAMERA_RX_CONFIG_ENCODE EncodeConfig { get; set; } | ||
|
||
/// <summary> | ||
/// DPCM unpacking value. | ||
/// </summary> | ||
public MMAL_CAMERA_RX_CONFIG_UNPACK UnpackConfig { get; set; } | ||
|
||
/// <summary> | ||
/// DPCM packing value. | ||
/// </summary> | ||
public MMAL_CAMERA_RX_CONFIG_PACK PackConfig { get; set; } | ||
|
||
/// <summary> | ||
/// Unsure. | ||
/// </summary> | ||
public int DataLanes { get; set; } | ||
|
||
/// <summary> | ||
/// Unsure. | ||
/// </summary> | ||
public int EncodeBlockLength { get; set; } | ||
|
||
/// <summary> | ||
/// Unsure. | ||
/// </summary> | ||
public int EmbeddedDataLines { get; set; } | ||
|
||
/// <summary> | ||
/// Unsure. | ||
/// </summary> | ||
public int ImageId { get; set; } | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="MMALRawcamRxConfig"/> for use with the Rawcam component port configuration. | ||
/// </summary> | ||
/// <param name="decodeConfig">Bayer decoding value.</param> | ||
/// <param name="encodeConfig">Bayer encoding value.</param> | ||
/// <param name="unpackConfig">DPCM unpacking value.</param> | ||
/// <param name="packConfig">DPCM packing value.</param> | ||
/// <param name="dataLanes">Data lanes value - unsure.</param> | ||
/// <param name="encodeBlockLength">Encode block length value - Unsure.</param> | ||
/// <param name="embeddedDataLanes">Embedded data lanes value - Unsure.</param> | ||
/// <param name="imageId">Image ID value - Unsure.</param> | ||
public MMALRawcamRxConfig(MMAL_CAMERA_RX_CONFIG_DECODE decodeConfig, | ||
MMAL_CAMERA_RX_CONFIG_ENCODE encodeConfig, | ||
MMAL_CAMERA_RX_CONFIG_UNPACK unpackConfig, | ||
MMAL_CAMERA_RX_CONFIG_PACK packConfig, | ||
int dataLanes, | ||
int encodeBlockLength, | ||
int embeddedDataLanes, | ||
int imageId) | ||
{ | ||
this.DecodeConfig = decodeConfig; | ||
this.EncodeConfig = encodeConfig; | ||
this.UnpackConfig = unpackConfig; | ||
this.PackConfig = packConfig; | ||
this.DataLanes = dataLanes; | ||
this.EncodeBlockLength = encodeBlockLength; | ||
this.EmbeddedDataLines = embeddedDataLanes; | ||
this.ImageId = imageId; | ||
} | ||
} | ||
} |
Oops, something went wrong.
610ba75
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tracked against #124.