private void TransformTo(IntPtr buffer, int bufferStride, TransformParameters parameters) { if (!_scalersMap.TryGetValue(parameters, out FFmpegDecodedVideoScaler videoScaler)) { videoScaler = FFmpegDecodedVideoScaler.Create(_currentFrameParameters, parameters); _scalersMap.Add(parameters, videoScaler); } int resultCode; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { resultCode = FFmpegVideoPInvokeWin.ScaleDecodedVideoFrame(_decoderHandle, videoScaler.Handle, buffer, bufferStride); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { resultCode = FFmpegVideoPInvokeLinux.ScaleDecodedVideoFrame(_decoderHandle, videoScaler.Handle, buffer, bufferStride); } else { throw new PlatformNotSupportedException(); } if (resultCode != 0) { throw new DecoderException($"An error occurred while converting decoding video frame, {_videoCodecId} codec, code: {resultCode}"); } }
private void TransformTo(IntPtr buffer, int bufferStride, TransformParameters parameters) { int resultCode = 0; try { lock (_scalersMap) { if (!_scalersMap.TryGetValue(parameters, out FFmpegDecodedVideoScaler videoScaler)) { videoScaler = FFmpegDecodedVideoScaler.Create(_currentFrameParameters, parameters); _scalersMap.Add(parameters, videoScaler); } resultCode = FFmpegVideoPInvoke.ScaleDecodedVideoFrame(_decoderHandle, videoScaler.Handle, buffer, bufferStride); } } catch (Exception ex) { throw new DecoderException($"An error occurred while converting decoding video frame, {ex.Message}"); } if (resultCode != 0) { throw new DecoderException($"An error occurred while converting decoding video frame, {_videoCodecId} codec, code: {resultCode}"); } }
private static void RtspClient_FrameReceived(object sender, RawFrame rawFrame) { if (!(rawFrame is RawVideoFrame rawVideoFrame)) { return; } FFmpegVideoDecoder decoder = GetDecoderForFrame(rawVideoFrame); IDecodedVideoFrame decodedFrame = decoder.TryDecode(rawVideoFrame); if (decodedFrame != null) { var _FrameType = rawFrame is RawH264IFrame ? "IFrame" : "PFrame"; TransformParameters _transformParameters = new TransformParameters(RectangleF.Empty, new Size(STREAM_WIDTH, STREAM_HEIGHT), ScalingPolicy.Stretch, PixelFormat.Bgra32, ScalingQuality.FastBilinear); var pictureSize = STREAM_WIDTH * STREAM_HEIGHT; IntPtr unmanagedPointer = Marshal.AllocHGlobal(pictureSize * 4); decodedFrame.TransformTo(unmanagedPointer, STREAM_WIDTH * 4, _transformParameters); byte[] managedArray = new byte[pictureSize * 4]; Marshal.Copy(unmanagedPointer, managedArray, 0, pictureSize * 4); Marshal.FreeHGlobal(unmanagedPointer); Console.WriteLine($"Frame was successfully decoded! {_FrameType } Trying to save to JPG file..."); try { string mypath = "empty";//initialize //request.AddFile("f1", "http://*****:*****@"E:\learning\testphoto\image21.jpg", ImageFormat.Jpeg); im.Save(@mypath, ImageFormat.Jpeg); return; } if (isLinux) { // Change to your path mypath = Path.Combine(WebHostEnvironment.WebRootPath, "uploads/", "1", "image21.jpg");//linux path im.Save(@mypath, ImageFormat.Jpeg); return; } throw new PlatformNotSupportedException("Not supported OS platform!!"); } catch (Exception e) { Console.WriteLine($"Error saving to file: {e.Message}"); Debug.WriteLine($"Error saving to file: {e.Message}"); Debug.WriteLine($"Stack trace: {e.StackTrace}"); } } }
private static ParametricBoxControllerParameters GetParameters(ParametricBoxController parametricBoxController) { if (!TransformParameters.TryGetValue(parametricBoxController, out ParametricBoxControllerParameters parameters)) { parameters = new ParametricBoxControllerParameters(); TransformParameters.Add(parametricBoxController, parameters); } return(parameters); }
private static byte[] TransformFrame(IDecodedVideoFrame decodedFrame, Size pictureSize) { var transformParameters = new TransformParameters( RectangleF.Empty, pictureSize, ScalingPolicy.Stretch, FrameDecoderCore.PixelFormat.Bgra32, ScalingQuality.FastBilinear); var pictureArraySize = pictureSize.Width * pictureSize.Height * 4; var unmanagedPointer = Marshal.AllocHGlobal(pictureArraySize); decodedFrame.TransformTo(unmanagedPointer, pictureSize.Width * 4, transformParameters); var managedArray = new byte[pictureArraySize]; Marshal.Copy(unmanagedPointer, managedArray, 0, pictureArraySize); Marshal.FreeHGlobal(unmanagedPointer); return(managedArray); }
private void OnFrameReceived(object sender, IDecodedVideoFrame decodedFrame) { if (!decodedFrame.FrameParameters.Equals(_frameParameters)) { _frameParameters = decodedFrame.FrameParameters; _transformParameters = new TransformParameters(System.Drawing.RectangleF.Empty, new System.Drawing.Size(_frameParameters.Width, _frameParameters.Height), ScalingPolicy.Stretch, PixelFormat.Bgr24, ScalingQuality.FastBilinear); _cvBitmap = new Image <Bgr, byte>(_frameParameters.Width, _frameParameters.Height); FrameSizeChanged?.Invoke(this, new Tuple <double, double>(_frameParameters.Width, _frameParameters.Height)); } if (_yoloWrapper.IsYoloReady()) { decodedFrame.TransformTo(_cvBitmap.Mat.DataPointer, _cvBitmap.Mat.Cols * _cvBitmap.Mat.ElementSize, _transformParameters); _yoloWrapper.FrameIn(this, _cvBitmap.Mat); } }
private void TransformTo(IntPtr buffer, int bufferStride, TransformParameters parameters) { if (!_scalersMap.TryGetValue(parameters, out FFmpegDecodedVideoScaler videoScaler)) { videoScaler = FFmpegDecodedVideoScaler.Create(_currentFrameParameters, parameters); _scalersMap.Add(parameters, videoScaler); } lock (disposalLock) { if (_disposed) { Console.WriteLine("Skipped scaling video frame, as decoder was disposed. (Therefore the frame probably wasn't wanted)"); return; } int resultCode = FFmpegVideoPInvoke.ScaleDecodedVideoFrame(_decoderHandle, videoScaler.Handle, buffer, bufferStride); if (resultCode != 0) { throw new DecoderException($"An error occurred while converting decoding video frame, {_videoCodecId} codec, code: {resultCode}"); } } }
/// <exception cref="DecoderException"></exception> public static FFmpegDecodedVideoScaler Create(DecodedVideoFrameParameters decodedVideoFrameParameters, TransformParameters transformParameters) { if (decodedVideoFrameParameters == null) { throw new ArgumentNullException(nameof(decodedVideoFrameParameters)); } if (transformParameters == null) { throw new ArgumentNullException(nameof(transformParameters)); } int sourceLeft = 0; int sourceTop = 0; int sourceWidth = decodedVideoFrameParameters.Width; int sourceHeight = decodedVideoFrameParameters.Height; int scaledWidth = decodedVideoFrameParameters.Width; int scaledHeight = decodedVideoFrameParameters.Height; if (!transformParameters.RegionOfInterest.IsEmpty) { sourceLeft = (int)(decodedVideoFrameParameters.Width * transformParameters.RegionOfInterest.Left); sourceTop = (int)(decodedVideoFrameParameters.Height * transformParameters.RegionOfInterest.Top); sourceWidth = (int)(decodedVideoFrameParameters.Width * transformParameters.RegionOfInterest.Width); sourceHeight = (int)(decodedVideoFrameParameters.Height * transformParameters.RegionOfInterest.Height); } if (!transformParameters.TargetFrameSize.IsEmpty) { scaledWidth = transformParameters.TargetFrameSize.Width; scaledHeight = transformParameters.TargetFrameSize.Height; ScalingPolicy scalingPolicy = transformParameters.ScalePolicy; float srcAspectRatio = (float)sourceWidth / sourceHeight; float destAspectRatio = (float)scaledWidth / scaledHeight; if (scalingPolicy == ScalingPolicy.Auto) { float relativeChange = Math.Abs(srcAspectRatio - destAspectRatio) / srcAspectRatio; scalingPolicy = relativeChange > MaxAspectRatioError ? ScalingPolicy.RespectAspectRatio : ScalingPolicy.Stretch; } if (scalingPolicy == ScalingPolicy.RespectAspectRatio) { if (destAspectRatio < srcAspectRatio) { scaledHeight = sourceHeight * scaledWidth / sourceWidth; } else { scaledWidth = sourceWidth * scaledHeight / sourceHeight; } } } PixelFormat scaledPixelFormat = transformParameters.TargetFormat; FFmpegPixelFormat scaledFFmpegPixelFormat = GetFFmpegPixelFormat(scaledPixelFormat); FFmpegScalingQuality scaleQuality = GetFFmpegScaleQuality(transformParameters.ScaleQuality); int resultCode = FFmpegVideoPInvoke.CreateVideoScaler(sourceLeft, sourceTop, sourceWidth, sourceHeight, decodedVideoFrameParameters.PixelFormat, scaledWidth, scaledHeight, scaledFFmpegPixelFormat, scaleQuality, out var handle); if (resultCode != 0) { throw new DecoderException(@"An error occurred while creating scaler, code: {resultCode}"); } return(new FFmpegDecodedVideoScaler(handle, scaledWidth, scaledHeight, scaledPixelFormat)); }
public void TransformTo(IntPtr buffer, int bufferStride, TransformParameters transformParameters) { _transformAction(buffer, bufferStride, transformParameters); }
public async Task SaveScreenCapAsync(String strPath = null, String strFileName = null) { WriteableBitmap writeableBitmap; lock (m_decoderLock) { if (m_lastDecodedFrame == null || State != EState.ACTIVE) { return; } TransformParameters transformParameters = new TransformParameters(RectangleF.Empty, new System.Drawing.Size(m_lastDecodedFrame.Parameters.Width, m_lastDecodedFrame.Parameters.Height), ScalingPolicy.RespectAspectRatio, PixelFormat.Bgra32, ScalingQuality.Bicubic); writeableBitmap = new WriteableBitmap( m_lastDecodedFrame.Parameters.Width, m_lastDecodedFrame.Parameters.Height, ScreenInfo.DpiX, ScreenInfo.DpiY, PixelFormats.Pbgra32, null); m_lastDecodedFrame.TransformTo(writeableBitmap.BackBuffer, writeableBitmap.BackBufferStride, transformParameters); writeableBitmap.Freeze(); } String strFullPath; if (strPath == null) { strPath = Properties.Settings.Default.PrefDownloadDir; if (String.IsNullOrWhiteSpace(strPath)) { strPath = ReceiveFilesManager.GetDefaultDownloadDirectory(); } } if (strFileName == null) { String strName = "wireboard-" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Hour + "h" + DateTime.Now.Minute + "m"; strFullPath = strPath + Path.DirectorySeparatorChar + strName + ".png"; for (int i = 2; File.Exists(strFullPath); i++) { strFullPath = strPath + Path.DirectorySeparatorChar + strName + "-" + i + ".png"; } } else { strFullPath = strPath + Path.DirectorySeparatorChar + strFileName; } try { await Task.Run(() => { BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(writeableBitmap)); using (var fileStream = new FileStream(strFullPath, FileMode.Create)) { encoder.Save(fileStream); } }); Log.i(TAG, "Saved screen capture as " + strFullPath, true); } catch (Exception e) { Log.e(TAG, "Failed to save screen capture. Error: " + e.Message, true); } }