public DecodedVideoFrameParameters(int width, int height, FFmpegPixelFormat pixelFormat)
 {
     Width       = width;
     Height      = height;
     PixelFormat = pixelFormat;
 }
Пример #2
0
        /// <exception cref="DecoderException"></exception>
        public static FFmpegDecodedVideoScaler Create(DecodedVideoFrameParameters decodedVideoFrameParameters,
                                                      PostVideoDecodingParameters postVideoDecodingParameters)
        {
            if (decodedVideoFrameParameters == null)
            {
                throw new ArgumentNullException(nameof(decodedVideoFrameParameters));
            }
            if (postVideoDecodingParameters == null)
            {
                throw new ArgumentNullException(nameof(postVideoDecodingParameters));
            }

            int sourceLeft   = 0;
            int sourceTop    = 0;
            int sourceWidth  = decodedVideoFrameParameters.Width;
            int sourceHeight = decodedVideoFrameParameters.Height;
            int scaledWidth  = decodedVideoFrameParameters.Width;
            int scaledHeight = decodedVideoFrameParameters.Height;

            if (!postVideoDecodingParameters.RegionOfInterest.IsEmpty)
            {
                sourceLeft =
                    (int)(decodedVideoFrameParameters.Width * postVideoDecodingParameters.RegionOfInterest.Left);
                sourceTop =
                    (int)(decodedVideoFrameParameters.Height * postVideoDecodingParameters.RegionOfInterest.Top);
                sourceWidth =
                    (int)(decodedVideoFrameParameters.Width * postVideoDecodingParameters.RegionOfInterest.Width);
                sourceHeight =
                    (int)(decodedVideoFrameParameters.Height * postVideoDecodingParameters.RegionOfInterest.Height);
            }

            if (!postVideoDecodingParameters.TargetFrameSize.IsEmpty)
            {
                scaledWidth  = postVideoDecodingParameters.TargetFrameSize.Width;
                scaledHeight = postVideoDecodingParameters.TargetFrameSize.Height;

                ScalingPolicy scalingPolicy = postVideoDecodingParameters.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       = postVideoDecodingParameters.TargetFormat;
            FFmpegPixelFormat    scaledFFmpegPixelFormat = GetFFmpegPixelFormat(scaledPixelFormat);
            FFmpegScalingQuality scaleQuality            = GetFFmpegScaleQuality(postVideoDecodingParameters.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 is occurred while creating scaler, code: {resultCode}");
            }

            return(new FFmpegDecodedVideoScaler(handle, scaledWidth, scaledHeight, scaledPixelFormat));
        }
Пример #3
0
 public static extern int CreateVideoScaler(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
                                            FFmpegPixelFormat sourcePixelFormat,
                                            int scaledWidth, int scaledHeight, FFmpegPixelFormat scaledPixelFormat, FFmpegScalingQuality qualityFlags,
                                            out IntPtr handle);
Пример #4
0
 public static extern int DecodeFrame(IntPtr handle, IntPtr rawBuffer, int rawBufferLength, out int frameWidth,
                                      out int frameHeight, out FFmpegPixelFormat framePixelFormat);
 public bool Equals(int width, int height, FFmpegPixelFormat pixelFormat)
 {
     return(Width == width && Height == height && PixelFormat == pixelFormat);
 }
        /// <exception cref="DecoderException"></exception>
        public static DecodedVideoScaler Create(DecodedVideoFrameParameters decodedVideoFrameParms, TransformParameters transformParms)
        {
            if (decodedVideoFrameParms == null)
            {
                throw new ArgumentNullException(nameof(decodedVideoFrameParms));
            }
            if (transformParms == null)
            {
                throw new ArgumentNullException(nameof(transformParms));
            }

            int sourceLeft   = 0;
            int sourceTop    = 0;
            int sourceWidth  = decodedVideoFrameParms.Width;
            int sourceHeight = decodedVideoFrameParms.Height;
            int scaledWidth  = decodedVideoFrameParms.Width;
            int scaledHeight = decodedVideoFrameParms.Height;

            if (!transformParms.Region.IsEmpty)
            {
                sourceLeft   = transformParms.Region.Left;
                sourceTop    = transformParms.Region.Top;
                sourceWidth  = transformParms.Region.Width;
                sourceHeight = transformParms.Region.Height;
            }

            if (!transformParms.TargetSize.IsEmpty)
            {
                scaledWidth  = transformParms.TargetSize.Width;
                scaledHeight = transformParms.TargetSize.Height;

                ScalingPolicy scalingPolicy = transformParms.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;
                    }
                }
            }

            FFmpegPixelFormat    pixelFormat  = ToFFmpegPixelFormat(transformParms.PixelFormat);
            FFmpegScalingQuality scaleQuality = ToFFmpegScaleQuality(transformParms.ScaleQuality);

            int resultCode = FFmpegVideoPInvoke.CreateVideoScaler
                             (
                sourceLeft, sourceTop, sourceWidth, sourceHeight,
                decodedVideoFrameParms.PixelFormat,
                scaledWidth, scaledHeight,
                pixelFormat, scaleQuality,
                out var handle
                             );

            if (resultCode != 0)
            {
                throw new DecoderException(@"An error occurred while creating scaler, code: {resultCode}");
            }

            return(new DecodedVideoScaler(handle, scaledWidth, scaledHeight, transformParms.PixelFormat));
        }
        /// <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;
            IntPtr handle;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                resultCode = FFmpegVideoPInvokeWin.CreateVideoScaler(sourceLeft, sourceTop, sourceWidth, sourceHeight,
                                                                     decodedVideoFrameParameters.PixelFormat,
                                                                     scaledWidth, scaledHeight, scaledFFmpegPixelFormat, scaleQuality, out handle);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                resultCode = FFmpegVideoPInvokeLinux.CreateVideoScaler(sourceLeft, sourceTop, sourceWidth, sourceHeight,
                                                                       decodedVideoFrameParameters.PixelFormat,
                                                                       scaledWidth, scaledHeight, scaledFFmpegPixelFormat, scaleQuality, out handle);
            }
            else
            {
                throw new PlatformNotSupportedException();
            }


            if (resultCode != 0)
            {
                throw new DecoderException(@"An error occurred while creating scaler, code: {resultCode}");
            }

            return(new FFmpegDecodedVideoScaler(handle, scaledWidth, scaledHeight, scaledPixelFormat));
        }