Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
Resolves techyian#158 - Set framerate as double.
Browse files Browse the repository at this point in the history
  • Loading branch information
techyian committed Sep 3, 2020
1 parent 49f14f8 commit 4435fd1
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ private void ConfigureQuantisationParameter(int outputPort)

private void ConfigureVideoProfile(int outputPort)
{
var rational = new MMAL_RATIONAL_T(MMALCameraConfig.Framerate);
var macroblocks = (MMALCameraConfig.Resolution.Width >> 4) * (MMALCameraConfig.Resolution.Height >> 4);
var macroblocksPSec = macroblocks * (MMALCameraConfig.Framerate.Num / MMALCameraConfig.Framerate.Den);
var macroblocksPSec = macroblocks * (rational.Num / rational.Den);

List<VideoLevel> videoLevels = GetNormalLevelLimits();

Expand Down
10 changes: 5 additions & 5 deletions src/MMALSharp/Components/MMALCameraComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void Initialise(IOutputCaptureHandler stillCaptureHandler = null, IOutput
1,
MMALCameraConfig.Resolution.Width,
MMALCameraConfig.Resolution.Height,
3 + Math.Max(0, (MMALCameraConfig.Framerate.Num - 30) / 10),
3 + Math.Max(0, (new MMAL_RATIONAL_T(MMALCameraConfig.Framerate).Num - 30) / 10),
0,
0,
MMALCameraConfig.ClockMode);
Expand Down Expand Up @@ -228,7 +228,7 @@ private void InitialisePreview()
MMALCameraConfig.EncodingSubFormat,
width: MMALCameraConfig.Resolution.Width,
height: MMALCameraConfig.Resolution.Height,
framerate: MMALCameraConfig.Framerate.Num);
framerate: MMALCameraConfig.Framerate);

MMALLog.Logger.LogDebug("Commit preview");

Expand Down Expand Up @@ -269,7 +269,7 @@ private void InitialiseVideo(IOutputCaptureHandler handler)
MMALCameraConfig.EncodingSubFormat,
width: currentWidth,
height: currentHeight,
framerate: MMALCameraConfig.Framerate.Num,
framerate: MMALCameraConfig.Framerate,
bufferNum: Math.Max(this.VideoPort.BufferNumRecommended, 3),
bufferSize: Math.Max(this.VideoPort.BufferSizeRecommended, this.VideoPort.BufferSizeMin),
crop: new Rectangle(0, 0, currentWidth, currentHeight));
Expand Down Expand Up @@ -340,7 +340,7 @@ private void InitialiseStill(IOutputCaptureHandler handler)
null,
width: currentWidth,
height: currentHeight,
framerate: MMALCameraConfig.Framerate.Num,
framerate: MMALCameraConfig.Framerate,
bufferNum: Math.Max(this.StillPort.BufferNumRecommended, 3),
bufferSize: Math.Max(this.StillPort.BufferSizeRecommended, this.StillPort.BufferSizeMin),
crop: new Rectangle(0, 0, currentWidth, currentHeight));
Expand All @@ -354,7 +354,7 @@ private void InitialiseStill(IOutputCaptureHandler handler)
MMALCameraConfig.EncodingSubFormat,
width: resolution.Width,
height: resolution.Height,
framerate: MMALCameraConfig.Framerate.Num,
framerate: MMALCameraConfig.Framerate,
bufferNum: Math.Max(this.StillPort.BufferNumRecommended, 3),
bufferSize: Math.Max(this.StillPort.BufferSizeRecommended, this.StillPort.BufferSizeMin),
crop: new Rectangle(0, 0, currentWidth, currentHeight));
Expand Down
2 changes: 1 addition & 1 deletion src/MMALSharp/MMALCameraConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public static class MMALCameraConfig
/// <summary>
/// The working framerate of the camera.
/// </summary>
public static MMAL_RATIONAL_T Framerate { get; set; } = new MMAL_RATIONAL_T(30, 1);
public static double Framerate { get; set; } = 30;

/*
* -----------------------------------------------------------------------------------------------------------
Expand Down
39 changes: 33 additions & 6 deletions src/MMALSharp/Native/MMALUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) Ian Auty and contributors. All rights reserved.
// Licensed under the MIT License. Please see LICENSE.txt for License info.
// </copyright>

using System;
using System.Runtime.InteropServices;

namespace MMALSharp.Native
Expand Down Expand Up @@ -180,15 +180,42 @@ public MMAL_FLOAT_RECT_T(double x, double y, double width, double height)
[StructLayout(LayoutKind.Sequential)]
public struct MMAL_RATIONAL_T
{
private int num, den;
private int _num, _den;

public int Num => _num;
public int Den => _den;

public int Num => num;
public int Den => den;
/// <summary>
/// Creates a new <see cref="MMAL_RATIONAL_T"/> accepting a numerator value.
/// </summary>
/// <param name="num">The numerator.</param>
public MMAL_RATIONAL_T(double num)
{
if (num < 1)
{
var multiplier = 100;
var doubleNum = num * 100;

while (doubleNum < 1)
{
doubleNum *= 10;
multiplier *= 10;
}

_num = Convert.ToInt32(doubleNum);
_den = multiplier;
}
else
{
_num = Convert.ToInt32(num * 10);
_den = 10;
}
}

public MMAL_RATIONAL_T(int num, int den)
{
this.num = num;
this.den = den;
_num = num;
_den = den;
}
}
}
2 changes: 1 addition & 1 deletion src/MMALSharp/Ports/IMMALPortConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface IMMALPortConfig
/// <summary>
/// The framerate of the outputted data.
/// </summary>
int Framerate { get; }
double Framerate { get; }

/// <summary>
/// The quality of our outputted data.
Expand Down
7 changes: 6 additions & 1 deletion src/MMALSharp/Ports/IPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ public interface IPort : IMMALObject
/// <summary>
/// The framerate we are processing data in.
/// </summary>
MMAL_RATIONAL_T FrameRate { get; }
double FrameRate { get; }

/// <summary>
/// The framerate represented as a <see cref="MMAL_RATIONAL_T"/>.
/// </summary>
MMAL_RATIONAL_T FrameRateRational { get; }

/// <summary>
/// The working video color space, specific to video ports.
Expand Down
2 changes: 1 addition & 1 deletion src/MMALSharp/Ports/Inputs/InputPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public virtual void Configure(IMMALPortConfig config, IPort copyPort, IInputCapt

if (config.Framerate > 0)
{
this.FrameRate = new MMAL_RATIONAL_T(config.Framerate, 1);
this.FrameRate = config.Framerate;
}

if (config.Bitrate > 0)
Expand Down
4 changes: 2 additions & 2 deletions src/MMALSharp/Ports/MMALPortConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class MMALPortConfig : IMMALPortConfig
/// <summary>
/// The framerate of the outputted data.
/// </summary>
public int Framerate { get; }
public double Framerate { get; }

/// <summary>
/// The quality value. Can be used with JPEG encoding (value between 1-100). Can be used with H.264 encoding which affects the quantization parameter (typical values between 10-40, see wiki for info). Set both bitrate param and quality param to 0 for variable bitrate.
Expand Down Expand Up @@ -112,7 +112,7 @@ public MMALPortConfig(
bool storeMotionVectors = false,
int width = 0,
int height = 0,
int framerate = 0,
double framerate = 0,
bool zeroCopy = false,
int bufferNum = 0,
int bufferSize = 0,
Expand Down
2 changes: 1 addition & 1 deletion src/MMALSharp/Ports/Outputs/OutputPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public virtual void Configure(IMMALPortConfig config, IInputPort copyFrom, IOutp

if (config.Framerate > 0)
{
this.FrameRate = new MMAL_RATIONAL_T(config.Framerate, 1);
this.FrameRate = config.Framerate;
}

if (config.Bitrate > 0)
Expand Down
12 changes: 10 additions & 2 deletions src/MMALSharp/Ports/PortBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,18 @@ public Rectangle Crop
/// <summary>
/// The framerate we are processing data in.
/// </summary>
public MMAL_RATIONAL_T FrameRate
public double FrameRate
{
get => this.Ptr->Format->Es->Video.FrameRate.Num;
internal set => this.Ptr->Format->Es->Video.FrameRate = new MMAL_RATIONAL_T(value);
}

/// <summary>
/// The framerate represented as a <see cref="MMAL_RATIONAL_T"/>.
/// </summary>
public MMAL_RATIONAL_T FrameRateRational
{
get => this.Ptr->Format->Es->Video.FrameRate;
internal set => this.Ptr->Format->Es->Video.FrameRate = new MMAL_RATIONAL_T(value.Num, value.Den);
}

/// <summary>
Expand Down
17 changes: 15 additions & 2 deletions tests/MMALSharp.Tests/ConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// </copyright>

using System;
using System.Runtime.InteropServices;
using MMALSharp.Common.Utility;
using MMALSharp.Components;
using MMALSharp.Config;
Expand Down Expand Up @@ -282,7 +281,7 @@ public void SetThenGetZoom(double x, double y, double width, double height)
[MMALTestsAttribute]
public void SetThenGetShutterSpeed(int shutterSpeed)
{
MMALCameraConfig.Framerate = new MMAL_RATIONAL_T(0, 0);
MMALCameraConfig.Framerate = 0;
MMALCameraConfig.SensorMode = MMALSensorMode.Mode1;
MMALCameraConfig.AwbMode = MMAL_PARAM_AWBMODE_T.MMAL_PARAM_AWBMODE_OFF;
MMALCameraConfig.ShutterSpeed = shutterSpeed;
Expand Down Expand Up @@ -393,5 +392,19 @@ public void SetThenGetDigitalGain(double digitalGain)
Assert.ThrowsAny<Exception>(() => Fixture.MMALCamera.ConfigureCameraSettings());
}
}

[Theory]
[InlineData(25)]
[InlineData(25.5)]
[InlineData(0.005)]
[MMALTests]
public void SetThenGetFramerate(double framerate)
{
MMALCameraConfig.Framerate = framerate;

Fixture.MMALCamera.ConfigureCameraSettings();

Assert.True((double)Fixture.MMALCamera.Camera.StillPort.FrameRateRational.Num / Fixture.MMALCamera.Camera.StillPort.FrameRateRational.Den == framerate);
}
}
}
2 changes: 1 addition & 1 deletion tests/MMALSharp.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void SetConfigurationDefaults()
MMALCameraConfig.ShutterSpeed = 0;
MMALCameraConfig.SensorMode = MMALSensorMode.Mode0;
MMALCameraConfig.VideoStabilisation = true;
MMALCameraConfig.Framerate = new MMAL_RATIONAL_T(10, 1);
MMALCameraConfig.Framerate = 10;
MMALCameraConfig.Encoding = MMALEncoding.OPAQUE;
MMALCameraConfig.EncodingSubFormat = MMALEncoding.I420;
MMALCameraConfig.VideoColorSpace = MMALEncoding.MMAL_COLOR_SPACE_ITUR_BT709;
Expand Down

0 comments on commit 4435fd1

Please sign in to comment.