Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Commit 9583e56

Browse files
committed
Latest changes for standalone encode/decode components. Tidying up of configuration overloads in MMALDownstreamComponent.
1 parent cfb57f1 commit 9583e56

23 files changed

Lines changed: 400 additions & 187 deletions

src/MMALSharp.Processing/Handlers/TransformStreamCaptureHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ public TransformStreamCaptureHandler(Stream inputStream, string outputDirectory,
3939
/// <returns>A <see cref="ProcessResult"/> object containing read image data.</returns>
4040
public override ProcessResult Process(uint allocSize)
4141
{
42-
var buffer = new byte[allocSize - 128];
42+
var buffer = new byte[allocSize];
4343

44-
var read = this.InputStream.Read(buffer, 0, (int)allocSize - 128);
44+
var read = this.InputStream.Read(buffer, 0, (int)allocSize);
4545

4646
this.TotalRead += read;
4747

48-
if (read < allocSize - 128)
48+
if (read < allocSize)
4949
{
5050
return new ProcessResult { Success = true, BufferFeed = buffer, EOF = true, DataLength = read };
5151
}

src/MMALSharp/Components/EncoderComponents/MMALImageEncoder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ public MMALImageEncoder(ICaptureHandler handler, bool rawBayer = false, bool use
9494
}
9595

9696
/// <inheritdoc />>
97-
public override MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMALEncoding encodingType, MMALEncoding pixelFormat, int quality, int bitrate = 0, bool zeroCopy = false)
97+
public override MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMALEncoding encodingType, MMALEncoding pixelFormat, int quality, bool zeroCopy = false)
9898
{
99-
base.ConfigureOutputPort(outputPort, encodingType, pixelFormat, quality, bitrate, zeroCopy);
99+
base.ConfigureOutputPort(outputPort, encodingType, pixelFormat, quality, zeroCopy);
100100

101101
if (this.RawBayer)
102102
{

src/MMALSharp/Components/EncoderComponents/MMALImageFileEncoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public override unsafe MMALDownstreamComponent ConfigureInputPort(MMALEncoding e
5252
this.Inputs[0].Ptr->Format->Type = MMALFormat.MMAL_ES_TYPE_T.MMAL_ES_TYPE_VIDEO;
5353
this.Inputs[0].Ptr->Format->Es->Video.Height = height;
5454
this.Inputs[0].Ptr->Format->Es->Video.Width = width;
55-
this.Inputs[0].Ptr->Format->Es->Video.FrameRate = new MMAL_RATIONAL_T(25, 1);
55+
this.Inputs[0].Ptr->Format->Es->Video.FrameRate = new MMAL_RATIONAL_T(0, 1);
5656
this.Inputs[0].Ptr->Format->Es->Video.Par = new MMAL_RATIONAL_T(1, 1);
5757
this.Inputs[0].Ptr->Format->Es->Video.Crop = new MMAL_RECT_T(0, 0, width, height);
5858

src/MMALSharp/Components/EncoderComponents/MMALVideoDecoder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public MMALVideoDecoder(ICaptureHandler handler, DateTime? timeout = null)
6565
this.Timeout = timeout;
6666
}
6767

68-
/// <inheritdoc />>
69-
public override MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMALEncoding encodingType, MMALEncoding pixelFormat, int quality, int bitrate = 0, bool zeroCopy = false)
68+
/// <inheritdoc />
69+
public override MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMALEncoding encodingType, MMALEncoding pixelFormat, int framerate, int bitrate, bool zeroCopy = false)
7070
{
71-
base.ConfigureOutputPort(outputPort, encodingType, pixelFormat, quality, bitrate, zeroCopy);
71+
base.ConfigureOutputPort(outputPort, encodingType, pixelFormat, framerate, bitrate, zeroCopy);
7272
((VideoPort)this.Outputs[outputPort]).Timeout = this.Timeout;
7373

7474
return this;

src/MMALSharp/Components/EncoderComponents/MMALVideoEncoder.cs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ public unsafe class MMALVideoEncoder : MMALEncoderBase
3636
/// Signifies the max bitrate supported for H.264 Level 4.2 (62.5Mbits/s)
3737
/// </summary>
3838
public const int MaxBitrateLevel42 = 62500000; // 62.5Mbits/s
39-
40-
/// <summary>
41-
/// The working bitrate of this <see cref="MMALVideoEncoder"/>.
42-
/// </summary>
43-
public int Bitrate { get; set; }
4439

4540
/// <summary>
4641
/// The working H.264 level.
@@ -122,9 +117,9 @@ public MMALVideoEncoder(ICaptureHandler handler, DateTime? timeout = null, Split
122117
}
123118

124119
/// <inheritdoc />>
125-
public override MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMALEncoding encodingType, MMALEncoding pixelFormat, int quality, int bitrate = 0, bool zeroCopy = false)
120+
public override MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMALEncoding encodingType, MMALEncoding pixelFormat, int framerate, int quality, int bitrate, bool zeroCopy = false)
126121
{
127-
base.ConfigureOutputPort(outputPort, encodingType, pixelFormat, quality, bitrate, zeroCopy);
122+
base.ConfigureOutputPort(outputPort, encodingType, pixelFormat, framerate, quality, bitrate, zeroCopy);
128123

129124
if (MMALCameraConfig.VideoColorSpace != null &&
130125
MMALCameraConfig.VideoColorSpace.EncType == MMALEncoding.EncodingType.ColorSpace)
@@ -139,8 +134,7 @@ public override MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMAL
139134

140135
this.Outputs[outputPort].Ptr->BufferSize = 512 * 1024;
141136
this.Quality = quality;
142-
this.Bitrate = bitrate;
143-
137+
144138
if (this.Outputs[outputPort].EncodingType == MMALEncoding.H264)
145139
{
146140
this.ConfigureIntraPeriod(outputPort);
@@ -194,8 +188,8 @@ internal DateTime CalculateSplit()
194188
return tempDt.AddMinutes(this.Split.Value);
195189
}
196190
}
197-
198-
private void ConfigureBitrate(int outputPort)
191+
192+
internal void ConfigureBitrate(int outputPort)
199193
{
200194
if (this.Outputs[outputPort].EncodingType == MMALEncoding.H264)
201195
{
@@ -216,47 +210,48 @@ private void ConfigureBitrate(int outputPort)
216210

217211
var level = levelList.Where(c => c.Level == MMALCameraConfig.VideoLevel).First();
218212

219-
if (this.Bitrate > level.Maxbitrate)
213+
if (this.Outputs[outputPort].Bitrate > level.Maxbitrate)
220214
{
221215
throw new PiCameraError("Bitrate requested exceeds maximum for selected Video Level and Profile");
222216
}
223217
}
224218
else if (this.Outputs[outputPort].EncodingType == MMALEncoding.MJPEG)
225219
{
226-
if (this.Bitrate > MaxBitrateMJPEG)
220+
if (this.Outputs[outputPort].Bitrate > MaxBitrateMJPEG)
227221
{
228222
MMALLog.Logger.Warn("Bitrate too high: Reducing to 25MBit/s");
229-
this.Bitrate = MaxBitrateMJPEG;
223+
this.Outputs[outputPort].Bitrate = MaxBitrateMJPEG;
230224
}
231225
}
232-
233-
this.Outputs[outputPort].Ptr->Format->Bitrate = this.Bitrate;
226+
this.Outputs[outputPort].Ptr->Format->Bitrate = this.Outputs[outputPort].Bitrate;
234227
this.Outputs[outputPort].Ptr->Format->Es->Video.FrameRate = new MMAL_RATIONAL_T(0, 1);
235228
this.Outputs[outputPort].Commit();
229+
230+
this.Outputs[outputPort].Ptr->Format->Bitrate = this.Outputs[outputPort].Bitrate;
236231
}
237-
238-
private void ConfigureRateControl(int outputPort)
232+
233+
internal void ConfigureRateControl(int outputPort)
239234
{
240235
MMAL_PARAMETER_VIDEO_RATECONTROL_T param = new MMAL_PARAMETER_VIDEO_RATECONTROL_T(new MMAL_PARAMETER_HEADER_T(MMALParametersVideo.MMAL_PARAMETER_RATECONTROL, Marshal.SizeOf<MMAL_PARAMETER_VIDEO_RATECONTROL_T>()), MMALCameraConfig.RateControl);
241236
MMALCheck(MMALPort.mmal_port_parameter_set(this.Outputs[outputPort].Ptr, param.HdrPtr), "Unable to set ratecontrol.");
242237
}
243238

244-
private void ConfigureIntraPeriod(int outputPort)
239+
internal void ConfigureIntraPeriod(int outputPort)
245240
{
246241
if (this.Outputs[outputPort].EncodingType == MMALEncoding.H264 && MMALCameraConfig.IntraPeriod != -1)
247242
{
248243
this.Outputs[outputPort].SetParameter(MMALParametersVideo.MMAL_PARAMETER_INTRAPERIOD, MMALCameraConfig.IntraPeriod);
249244
}
250245
}
251-
252-
private void ConfigureQuantisationParameter(int outputPort)
246+
247+
internal void ConfigureQuantisationParameter(int outputPort)
253248
{
254249
this.Outputs[outputPort].SetParameter(MMALParametersVideo.MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT, this.Quality);
255250
this.Outputs[outputPort].SetParameter(MMALParametersVideo.MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT, this.Quality);
256251
this.Outputs[outputPort].SetParameter(MMALParametersVideo.MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT, this.Quality);
257252
}
258253

259-
private void ConfigureVideoProfile(int outputPort)
254+
internal void ConfigureVideoProfile(int outputPort)
260255
{
261256
var macroblocks = (MMALCameraConfig.VideoResolution.Width >> 4) * (MMALCameraConfig.VideoResolution.Height >> 4);
262257
var macroblocksPSec = macroblocks * (MMALCameraConfig.VideoFramerate.Num / MMALCameraConfig.VideoFramerate.Den);
@@ -297,25 +292,25 @@ private void ConfigureVideoProfile(int outputPort)
297292
}
298293
}
299294

300-
private void ConfigureImmutableInput(int outputPort)
295+
internal void ConfigureImmutableInput(int outputPort)
301296
{
302297
this.Inputs[0].SetParameter(MMALParametersVideo.MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT, MMALCameraConfig.ImmutableInput);
303298
}
304299

305-
private void ConfigureInlineHeaderFlag(int outputPort)
300+
internal void ConfigureInlineHeaderFlag(int outputPort)
306301
{
307302
this.Outputs[outputPort].SetParameter(MMALParametersVideo.MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER, MMALCameraConfig.InlineHeaders);
308303
}
309304

310-
private void ConfigureInlineVectorsFlag(int outputPort)
305+
internal void ConfigureInlineVectorsFlag(int outputPort)
311306
{
312307
if (this.Outputs[outputPort].EncodingType == MMALEncoding.H264)
313308
{
314309
this.Outputs[outputPort].SetParameter(MMALParametersVideo.MMAL_PARAMETER_VIDEO_ENCODE_INLINE_VECTORS, MMALCameraConfig.InlineMotionVectors);
315310
}
316311
}
317312

318-
private void ConfigureIntraRefresh(int outputPort)
313+
internal void ConfigureIntraRefresh(int outputPort)
319314
{
320315
MMAL_PARAMETER_VIDEO_INTRA_REFRESH_T param = new MMAL_PARAMETER_VIDEO_INTRA_REFRESH_T(new MMAL_PARAMETER_HEADER_T(MMALParametersVideo.MMAL_PARAMETER_VIDEO_INTRA_REFRESH, Marshal.SizeOf<MMAL_PARAMETER_VIDEO_INTRA_REFRESH_T>()), MMALParametersVideo.MMAL_VIDEO_INTRA_REFRESH_T.MMAL_VIDEO_INTRA_REFRESH_BOTH, 0, 0, 0, 0);
321316

src/MMALSharp/Components/EncoderComponents/MMALVideoFileDecoder.cs

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// </copyright>
55

66
using System;
7+
using System.Drawing;
78
using System.Text;
89
using System.Threading.Tasks;
910
using MMALSharp.Callbacks.Providers;
@@ -36,8 +37,8 @@ public MMALVideoFileDecoder(ICaptureHandler handler)
3637
/// </summary>
3738
public static MMALQueueImpl WorkingQueue { get; set; }
3839

39-
/// <inheritdoc />>
40-
public override unsafe MMALDownstreamComponent ConfigureInputPort(MMALEncoding encodingType, MMALEncoding pixelFormat, int width, int height, bool zeroCopy = false)
40+
/// <inheritdoc />
41+
public override unsafe MMALDownstreamComponent ConfigureInputPort(MMALEncoding encodingType, MMALEncoding pixelFormat, int width, int height, int framerate, int bitrate, bool zeroCopy = false)
4142
{
4243
this.InitialiseInputPort(0);
4344

@@ -46,27 +47,40 @@ public override unsafe MMALDownstreamComponent ConfigureInputPort(MMALEncoding e
4647
this.Inputs[0].Ptr->Format->Encoding = encodingType.EncodingVal;
4748
}
4849

50+
if (pixelFormat != null)
51+
{
52+
this.Inputs[0].Ptr->Format->EncodingVariant = pixelFormat.EncodingVal;
53+
}
54+
4955
this.Inputs[0].Ptr->Format->Type = MMALFormat.MMAL_ES_TYPE_T.MMAL_ES_TYPE_VIDEO;
50-
this.Inputs[0].Ptr->Format->Es->Video.Height = width;
51-
this.Inputs[0].Ptr->Format->Es->Video.Width = height;
52-
this.Inputs[0].Ptr->Format->Es->Video.FrameRate = new MMAL_RATIONAL_T(25, 1);
53-
this.Inputs[0].Ptr->Format->Es->Video.Par = new MMAL_RATIONAL_T(1, 1);
5456

57+
this.Inputs[0].Bitrate = bitrate;
58+
this.Inputs[0].Resolution = new Resolution(width, height).Pad();
59+
this.Inputs[0].Crop = new Rectangle(0, 0, width, height);
60+
this.Inputs[0].FrameRate = new MMAL_RATIONAL_T(framerate, 1);
61+
this.Inputs[0].Ptr->Format->Es->Video.Par = new MMAL_RATIONAL_T(1, 1);
62+
5563
this.Inputs[0].EncodingType = encodingType;
5664

5765
this.Inputs[0].Commit();
5866

59-
this.Inputs[0].Ptr->BufferNum = Math.Max(this.Inputs[0].Ptr->BufferNumRecommended, this.Inputs[0].Ptr->BufferNumMin);
60-
this.Inputs[0].Ptr->BufferSize = Math.Max(this.Inputs[0].Ptr->BufferSizeRecommended, this.Inputs[0].Ptr->BufferSizeMin);
67+
this.Inputs[0].Ptr->BufferNum = Math.Max(this.Inputs[0].Ptr->BufferNumMin, this.Inputs[0].Ptr->BufferNumRecommended);
68+
this.Inputs[0].Ptr->BufferSize = Math.Max(this.Inputs[0].Ptr->BufferSizeMin, this.Inputs[0].Ptr->BufferSizeRecommended);
6169

6270
return this;
6371
}
6472

65-
/// <inheritdoc />>
66-
public override unsafe MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMALEncoding encodingType, MMALEncoding pixelFormat, int quality, int bitrate = 0, bool zeroCopy = false)
73+
/// <inheritdoc />
74+
public override unsafe MMALDownstreamComponent ConfigureOutputPort(int outputPort, MMALEncoding encodingType, MMALEncoding pixelFormat, int width, int height, int framerate, int quality, int bitrate, bool zeroCopy = false)
6775
{
6876
this.InitialiseOutputPort(outputPort);
6977

78+
if (MMALCameraConfig.VideoColorSpace != null &&
79+
MMALCameraConfig.VideoColorSpace.EncType == MMALEncoding.EncodingType.ColorSpace)
80+
{
81+
this.Outputs[outputPort].VideoColorSpace = MMALCameraConfig.VideoColorSpace;
82+
}
83+
7084
if (this.ProcessingPorts.ContainsKey(outputPort))
7185
{
7286
this.ProcessingPorts.Remove(outputPort);
@@ -85,14 +99,23 @@ public override unsafe MMALDownstreamComponent ConfigureOutputPort(int outputPor
8599
this.Outputs[outputPort].SetParameter(MMALParametersCommon.MMAL_PARAMETER_ZERO_COPY, true);
86100
}
87101

102+
this.Outputs[outputPort].Ptr->Format->Bitrate = bitrate;
103+
this.Outputs[outputPort].Ptr->Format->Es->Video.FrameRate = new MMAL_RATIONAL_T(framerate, 1);
104+
this.Outputs[outputPort].Ptr->Format->Es->Video.Par = new MMAL_RATIONAL_T(1, 1);
105+
//this.Outputs[outputPort].Ptr->Format->Flags |= MMALFormat.MMAL_ES_FORMAT_FLAG_FRAMED;
106+
107+
this.Outputs[outputPort].Resolution = new Resolution(this.Width, this.Height).Pad();
108+
this.Outputs[outputPort].Crop = new Rectangle(0, 0, this.Width, this.Height);
109+
88110
this.Outputs[outputPort].Commit();
89111

90-
this.Outputs[outputPort].EncodingType = encodingType;
112+
this.Outputs[outputPort].Ptr->BufferSize = Math.Max(this.Outputs[0].Ptr->BufferSizeMin, this.Outputs[0].Ptr->BufferSizeRecommended);
113+
this.Outputs[outputPort].Ptr->BufferNum = Math.Max(this.Outputs[outputPort].Ptr->BufferNumMin, this.Outputs[outputPort].Ptr->BufferNumRecommended);
91114

92-
this.Outputs[outputPort].Ptr->BufferNum = Math.Max(this.Outputs[outputPort].Ptr->BufferNumRecommended, this.Outputs[outputPort].Ptr->BufferNumMin);
93-
this.Outputs[outputPort].Ptr->BufferSize = Math.Max(this.Outputs[outputPort].Ptr->BufferSizeRecommended, this.Outputs[outputPort].Ptr->BufferSizeMin);
115+
this.Outputs[outputPort].EncodingType = encodingType;
116+
94117
this.Outputs[outputPort].ManagedOutputCallback = OutputCallbackProvider.FindCallback(this.Outputs[outputPort]);
95-
118+
96119
return this;
97120
}
98121

@@ -109,7 +132,7 @@ public virtual async Task Convert(int outputPort = 0)
109132
this.Control.Start();
110133
this.Inputs[0].Start();
111134
this.Outputs[outputPort].Start();
112-
135+
113136
this.EnableComponent();
114137

115138
WorkingQueue = MMALQueueImpl.Create();
@@ -204,14 +227,15 @@ private unsafe void ConfigureOutputPortWithoutInit(int outputPort, MMALEncoding
204227
}
205228

206229
this.Outputs[outputPort].EncodingType = encodingType;
207-
208-
this.Outputs[outputPort].Ptr->BufferNum = Math.Max(this.Outputs[outputPort].Ptr->BufferNumRecommended, this.Outputs[outputPort].Ptr->BufferNumMin);
209-
this.Outputs[outputPort].Ptr->BufferSize = Math.Max(this.Outputs[outputPort].Ptr->BufferSizeRecommended, this.Outputs[outputPort].Ptr->BufferSizeMin);
210-
211-
MMALLog.Logger.Info($"New buffer number {this.Outputs[outputPort].Ptr->BufferNum}");
212-
MMALLog.Logger.Info($"New buffer size {this.Outputs[outputPort].Ptr->BufferSize}");
213-
230+
231+
this.Outputs[outputPort].Ptr->Format->Es->Video.Par = new MMAL_RATIONAL_T(1, 1);
232+
this.Outputs[outputPort].Resolution = new Resolution(this.Width, this.Height).Pad();
233+
this.Outputs[outputPort].Crop = new Rectangle(0, 0, this.Width, this.Height);
234+
214235
this.Outputs[outputPort].Commit();
236+
237+
this.Outputs[outputPort].Ptr->BufferNum = Math.Max(this.Outputs[outputPort].Ptr->BufferNumRecommended, this.Outputs[outputPort].Ptr->BufferNumMin);
238+
this.Outputs[outputPort].Ptr->BufferSize = this.Outputs[0].Ptr->BufferSizeRecommended;
215239
}
216240

217241
private void LogFormat(MMALEventFormat format, PortBase port)
@@ -239,9 +263,10 @@ private void LogFormat(MMALEventFormat format, PortBase port)
239263
sb.AppendLine($"FourCC: {format.FourCC}");
240264
sb.AppendLine($"Width: {format.Width}");
241265
sb.AppendLine($"Height: {format.Height}");
266+
sb.AppendLine($"Bitrate: {format.Bitrate}");
242267
sb.AppendLine($"Crop: {format.CropX}, {format.CropY}, {format.CropWidth}, {format.CropHeight}");
243268
sb.AppendLine($"Pixel aspect ratio: {format.ParNum}, {format.ParDen}. Frame rate: {format.FramerateNum}, {format.FramerateDen}");
244-
269+
245270
if (port != null)
246271
{
247272
sb.AppendLine($"Port info: Buffers num: {port.BufferNum}(opt {port.BufferNumRecommended}, min {port.BufferNumMin}). Size: {port.BufferSize} (opt {port.BufferSizeRecommended}, min {port.BufferSizeMin}). Alignment: {port.BufferAlignmentMin}");
@@ -293,19 +318,16 @@ private void GetAndSendOutputBuffer(int outputPort = 0)
293318

294319
private void ProcessFormatChangedEvent(MMALBufferImpl buffer, int outputPort = 0)
295320
{
296-
MMALLog.Logger.Debug("Received MMAL_EVENT_FORMAT_CHANGED event");
321+
MMALLog.Logger.Info("Received MMAL_EVENT_FORMAT_CHANGED event");
297322

298323
var ev = MMALEventFormat.GetEventFormat(buffer);
299324

300-
MMALLog.Logger.Debug("-- Event format changed from -- ");
325+
MMALLog.Logger.Info("-- Event format changed from -- ");
301326
this.LogFormat(new MMALEventFormat(this.Outputs[outputPort].Format), this.Outputs[outputPort]);
302327

303-
MMALLog.Logger.Debug("-- To -- ");
328+
MMALLog.Logger.Info("-- To -- ");
304329
this.LogFormat(ev, null);
305-
306-
// Port format changed
307-
this.Outputs[outputPort].ManagedOutputCallback.Callback(buffer);
308-
330+
309331
lock (OutputPort.OutputLock)
310332
{
311333
buffer.Release();

0 commit comments

Comments
 (0)