/************************************************************************/

        #region Methods
        protected bool Equals(TransformParameters other)
        {
            return
                (Region.Equals(other.Region) &&
                 TargetSize.Equals(other.TargetSize) &&
                 PixelFormat == other.PixelFormat &&
                 ScaleQuality == other.ScaleQuality);
        }
Example #2
0
        private void TransformTo(IntPtr buffer, int bufferStride, TransformParameters parameters)
        {
            if (!scalersMap.TryGetValue(parameters, out DecodedVideoScaler videoScaler))
            {
                videoScaler = DecodedVideoScaler.Create(currentFrameParms, parameters);
                scalersMap.Add(parameters, videoScaler);
            }

            lock (disposalLock)
            {
                if (disposed)
                {
                    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 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));
        }
 public void TransformTo(IntPtr buffer, int bufferStride, TransformParameters transformParms)
 {
     transformAction(buffer, bufferStride, transformParms);
 }