44// </copyright>
55
66using System ;
7+ using System . Drawing ;
78using System . Text ;
89using System . Threading . Tasks ;
910using 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