示例#1
0
        private void Initialize(Clip src, Clip over)
        {
            var srcInfo  = src.GetVideoInfo();
            var overInfo = over.GetVideoInfo();

            var vi = src.GetVideoInfo();

            if (Width > 0)
            {
                vi.width = Width;
            }
            if (Height > 0)
            {
                vi.height = Height;
            }
            var srcBitDepth  = srcInfo.pixel_type.GetBitDepth();
            var overBitDepth = overInfo.pixel_type.GetBitDepth();

            if (srcBitDepth != overBitDepth && ColorAdjust > 0 && ColorAdjust < 1)
            {
                throw new AvisynthException("ColorAdjust -1, 0, 1 only allowed when video bit depth is different");
            }
            var targetBitDepth = ColorAdjust >= 1 - double.Epsilon ? overBitDepth : srcBitDepth;

            vi.pixel_type   = vi.pixel_type.ChangeBitDepth(targetBitDepth);
            vi.num_frames   = Child.GetVideoInfo().num_frames;
            planes          = OverlayUtils.GetPlanes(vi.pixel_type);
            targetSubsample = new Size(srcInfo.pixel_type.GetWidthSubsample(), srcInfo.pixel_type.GetHeightSubsample());
            SetVideoInfo(ref vi);
        }
示例#2
0
        protected VideoFrame Copy(VideoFrame frame)
        {
            var res = NewVideoFrame(StaticEnv);

            Copy(frame, res, OverlayUtils.GetPlanes(GetVideoInfo().pixel_type));
            return(res);
        }
示例#3
0
        public override VideoFrame GetFrame(int n, ScriptEnvironment env)
        {
            var resFrame = NewVideoFrame(env);

            if (realPlanar && greyMask)
            {
                OverlayUtils.ResetChroma(resFrame);
            }
            using var srcFrame = base.GetFrame(n, env);
            unsafe
            {
                var planes = greyMask ? new[] { default(YUVPlanes) } : OverlayUtils.GetPlanes(GetVideoInfo().pixel_type);
                Parallel.ForEach(planes, plane =>
                {
                    var srcStride  = srcFrame.GetPitch(plane);
                    var destStride = resFrame.GetPitch(plane);
                    var width      = resFrame.GetRowSize(plane) / (hdr ? 2 : 1);
                    var height     = resFrame.GetHeight(plane);
                    if (hdr)
                    {
                        var src  = (ushort *)srcFrame.GetReadPtr(plane);
                        var dest = (ushort *)resFrame.GetWritePtr(plane);

                        Parallel.For(0, height, y =>
                        {
                            var srcData  = src + y * srcStride;
                            var destData = dest + y * destStride;
                            for (var x = 0; x < width; x++)
                            {
                                var val     = srcData[x];
                                destData[x] = val >= low && val <= high ? ushort.MaxValue : ushort.MinValue;
                            }
                        });
                    }
                    else
                    {
                        var src  = (byte *)srcFrame.GetReadPtr(plane);
                        var dest = (byte *)resFrame.GetWritePtr(plane);

                        Parallel.For(0, height, y =>
                        {
                            var srcData  = src + y * srcStride;
                            var destData = dest + y * destStride;
                            for (var x = 0; x < width; x++)
                            {
                                var val     = srcData[x];
                                destData[x] = val >= low && val <= high ? byte.MaxValue : byte.MinValue;
                            }
                        });
                    }
                });
            }

            return(resFrame);
        }
        protected override VideoFrame GetFrame(int n)
        {
            var allPlanes = OverlayUtils.GetPlanes(GetVideoInfo().pixel_type);

            if (Smooth > 0)
            {
                return(Copy(GetFrameWithSmooth(n)));
            }
            var output = NewVideoFrame(StaticEnv);

            using (var src = Source.GetFrame(n, StaticEnv))
                using (var over = Overlay.GetFrame(n, StaticEnv))
                {
                    if (GetVideoInfo().IsRGB() && realChannels.Length < 3 || Source.IsRealPlanar() && planes.Length < 3)
                    {
                        Parallel.ForEach(allPlanes, parallelOptions, plane => OverlayUtils.CopyPlane(src, output, plane));
                    }
                    unsafe
                    {
                        Parallel.ForEach(planes, parallelOptions, plane =>
                        {
                            var pixelSize = GetVideoInfo().IsRGB() ? 3 : 1;

                            var size       = new Size(src.GetRowSize(plane), src.GetHeight(plane));
                            var srcStride  = src.GetPitch(plane);
                            var overStride = over.GetPitch(plane);
                            Parallel.ForEach(realChannels, parallelOptions, channel =>
                            {
                                Parallel.For(0, size.Height, parallelOptions, y =>
                                {
                                    var srcData  = (byte *)src.GetReadPtr(plane) + y * srcStride + channel;
                                    var overData = (byte *)over.GetReadPtr(plane) + y * overStride + channel;
                                    var writer   = (byte *)output.GetWritePtr(plane) + y * output.GetPitch(plane) + channel;
                                    for (var x = 0; x < size.Width; x += pixelSize)
                                    {
                                        var srcComplexity  = GetComplexity(srcData, x, y, pixelSize, srcStride, size, Steps);
                                        var overComplexity = GetComplexity(overData, x, y, pixelSize, overStride, size, Steps);
                                        var diff           = srcComplexity - overComplexity;
                                        var srcPreferred   = diff > Preference;
                                        if (Mask)
                                        {
                                            writer[x] = srcPreferred ? byte.MinValue : byte.MaxValue;
                                        }
                                        else
                                        {
                                            writer[x] = srcPreferred ? srcData[x] : overData[x];
                                        }
                                    }
                                });
                            });
                        });
                    }
                }
            return(output);
        }