Esempio n. 1
0
        protected override VideoFrame GetFrame(int n)
        {
            var frame = base.GetFrame(n);

            StaticEnv.MakeWritable(frame);
            unsafe
            {
                using (var stream = new UnmanagedMemoryStream((byte *)frame.GetWritePtr().ToPointer(),
                                                              frame.GetRowSize(), frame.GetRowSize(), FileAccess.Write))
                    using (var writer = new BinaryWriter(stream))
                    {
                        writer.Write(nameof(OverlayConfig));
                        writer.Write(MinOverlayArea);
                        writer.Write(MinSourceArea);
                        writer.Write(AspectRatio1);
                        writer.Write(AspectRatio2);
                        writer.Write(Angle1);
                        writer.Write(Angle2);
                        writer.Write(MinSampleArea);
                        writer.Write(RequiredSampleArea);
                        writer.Write(MaxSampleDiff);
                        writer.Write(Subpixel);
                        writer.Write(ScaleBase);
                        writer.Write(Branches);
                        writer.Write(AcceptableDiff);
                        writer.Write(Correction);
                        writer.Write(MinX);
                        writer.Write(MaxX);
                        writer.Write(MinY);
                        writer.Write(MaxY);
                        writer.Write(MinArea);
                        writer.Write(MaxArea);
                        writer.Write(FixedAspectRatio);
                        writer.Write(BranchMaxDiff);
                        writer.Write(WarpSteps);
                        writer.Write(WarpOffset);
                        writer.Write(WarpPoints.Count);
                        foreach (var point in WarpPoints)
                        {
                            writer.Write(point.X);
                            writer.Write(point.Y);
                            writer.Write(point.Right);
                            writer.Write(point.Bottom);
                        }
                    }
            }
            return(frame);
        }
Esempio n. 2
0
        protected override VideoFrame GetFrame(int n)
        {
            var frame = base.GetFrame(n);

            StaticEnv.MakeWritable(frame);
            unsafe
            {
                using var stream = new UnmanagedMemoryStream(
                          (byte *)frame.GetWritePtr().ToPointer(),
                          frame.GetRowSize(), frame.GetRowSize(), FileAccess.Write);
                using var writer = new BinaryWriter(stream);
                writer.Write(nameof(Rect));
                writer.Write(Left);
                writer.Write(Top);
                writer.Write(Right);
                writer.Write(Bottom);
            }
            return(frame);
        }
Esempio n. 3
0
        protected override VideoFrame GetFrame(int n)
        {
            var input = Child.GetFrame(n, StaticEnv);

            if (Intensity <= double.Epsilon)
            {
                return(input);
            }
            var writable   = GetVideoInfo().pixel_type == Child.GetVideoInfo().pixel_type&& StaticEnv.MakeWritable(input);
            var output     = writable ? input : NewVideoFrame(StaticEnv);
            var pixelSize  = Sample.GetVideoInfo().IsRGB() ? 3 : 1;
            var firstFrame = Math.Max(0, n - AdjacentFramesCount);
            var lastFrame  = Math.Min(Child.GetVideoInfo().num_frames - 1, n + AdjacentFramesCount);
            var dimensions = new[]
            {
                Enumerable.Range(n, lastFrame - n + 1),
                Enumerable.Range(firstFrame, n - firstFrame)
            }.SelectMany(range => range.Select(frame =>
            {
                using (new VideoFrameCollector())
                    return(string.IsNullOrEmpty(CacheId) || frame == n
                        ? histogramCache.GetFrame(frame,
                                                  () => Extrapolation ? (frame == n ? input : Child.GetFrame(frame, StaticEnv)) : null,
                                                  () => Sample.GetFrame(frame, StaticEnv),
                                                  () => Reference.GetFrame(frame, StaticEnv),
                                                  () => SampleMask?.GetFrame(frame, StaticEnv),
                                                  () => ReferenceMask?.GetFrame(frame, StaticEnv))
                        : histogramCache[frame]);
            }).TakeWhile(dims =>
            {
                var current = histogramCache[n];
                return(dims != null && current.All(pair =>
                                                   current == dims || !dims[pair.Key].Empty &&
                                                   CompareHist(dims[pair.Key].DiffHist, pair.Value.DiffHist) < AdjacentFramesDiff));
            }).SelectMany(p => p)).ToList();

            Parallel.ForEach(planes, parallelOptions, plane =>
            {
                Parallel.ForEach(realChannels, parallelOptions, channel =>
                {
                    var currentDimensions = dimensions
                                            .Where(p => p.Key.Equal(plane, channel))
                                            .Select(p => p.Value).ToArray();
                    var sampleHist    = AverageHist(sampleBits, currentDimensions.Select(p => p.SampleHist).ToArray());
                    var referenceHist = AverageHist(referenceBits, currentDimensions.Select(p => p.ReferenceHist).ToArray());

                    if (sampleHist == null || referenceHist == null)
                    {
                        return;
                    }

                    var map = GetTransitionMap(sampleHist, referenceHist, n, plane);

                    if (Extrapolation)
                    {
                        var srcHist = AverageHist(referenceBits, currentDimensions.Select(p => p.InputHist).ToArray());
                        Extrapolate(map, srcHist, GetLowColor(referenceBits), GetHighColor(referenceBits, plane));
                    }

                    Interpolate(map, GetLowColor(referenceBits), GetHighColor(referenceBits, plane), sampleBits, referenceBits);

                    if (Intensity < 1)
                    {
                        var decreased = new ColorMap(sampleBits, n, Dither);
                        for (var color = 0; color < 1 << sampleBits; color++)
                        {
                            decreased.AddReal(color, map.Average(color) * Intensity + color * (1 - Intensity));
                        }
                        map = decreased;
                    }

                    var tuple = map.GetColorsAndWeights();

                    NativeUtils.ApplyColorMap(DynamicNoise ? Seed ^ n : 0,
                                              input.GetReadPtr(plane), input.GetPitch(plane), sampleBits > 8,
                                              output.GetWritePtr(plane), output.GetPitch(plane), referenceBits > 8,
                                              input.GetRowSize(plane), input.GetHeight(plane), pixelSize, channel,
                                              map.FixedMap, tuple.Item1, tuple.Item2);
                });
            });

            if (!writable)
            {
                input.Dispose();
            }
            return(output);
        }
Esempio n. 4
0
        protected override VideoFrame GetFrame(int n)
        {
            var input           = Child.GetFrame(n, StaticEnv);
            var sampleFrames    = Enumerable.Range(n - tr, tr * 2 + 1).Select(p => Sample.GetFrame(p, StaticEnv)).ToList();
            var referenceFrames = Enumerable.Range(n - tr, tr * 2 + 1).Select(p => Reference.GetFrame(p, StaticEnv)).ToList();
            var writable        = GetVideoInfo().pixel_type == Child.GetVideoInfo().pixel_type&& StaticEnv.MakeWritable(input);
            var output          = writable ? input : NewVideoFrame(StaticEnv);

            using (var sampleMaskFrame = SampleMask?.GetFrame(n, StaticEnv))
                using (var refMaskFrame = ReferenceMask?.GetFrame(n, StaticEnv))
                {
                    var pixelSize = Sample.GetVideoInfo().IsRGB() ? 3 : 1;
                    Parallel.ForEach(planes, plane =>
                    {
                        Parallel.ForEach(realChannels, channel =>
                        {
                            int[] sampleHist = null, referenceHist = null;
                            Parallel.Invoke(
                                () => sampleHist    = GetHistogram(sampleFrames, sampleMaskFrame, pixelSize, channel, plane, Sample.GetVideoInfo().pixel_type, sampleBits, false),
                                () => referenceHist = GetHistogram(referenceFrames, refMaskFrame, pixelSize, channel, plane, Reference.GetVideoInfo().pixel_type, referenceBits, LimitedRange));

                            var map = GetTransitionMap(sampleHist, referenceHist, n, plane);

                            var tuple = map.GetColorsAndWeights();

                            NativeUtils.ApplyColorMap(
                                input.GetReadPtr(plane), input.GetPitch(plane), sampleBits > 8,
                                output.GetWritePtr(plane), output.GetPitch(plane), referenceBits > 8,
                                input.GetRowSize(plane), input.GetHeight(plane), pixelSize, channel,
                                map.fixedMap, tuple.Item1, tuple.Item2);
                        });
                    });
                }
            if (!writable)
            {
                input.Dispose();
            }
            sampleFrames.ForEach(p => p.Dispose());
            referenceFrames.ForEach(p => p.Dispose());
            return(output);
        }
Esempio n. 5
0
        protected override VideoFrame GetFrame(int n)
        {
            var input = Child.GetFrame(n, StaticEnv);

            if (Intensity <= double.Epsilon)
            {
                return(input);
            }
            var sampleFrames    = Enumerable.Range(n - tr, tr * 2 + 1).Select(p => Sample.GetFrame(p, StaticEnv)).ToList();
            var referenceFrames = Enumerable.Range(n - tr, tr * 2 + 1).Select(p => Reference.GetFrame(p, StaticEnv)).ToList();
            var inputFrames     = tr == 0
                ? new List <VideoFrame> {
                input
            }
                : Enumerable.Range(n - tr, tr * 2 + 1).Select(p => Child.GetFrame(p, StaticEnv)).ToList();
            var writable = GetVideoInfo().pixel_type == Child.GetVideoInfo().pixel_type&& StaticEnv.MakeWritable(input);
            var output   = writable ? input : NewVideoFrame(StaticEnv);

            using (var sampleMaskFrame = SampleMask?.GetFrame(n, StaticEnv))
                using (var refMaskFrame = ReferenceMask?.GetFrame(n, StaticEnv))
                {
                    var pixelSize = Sample.GetVideoInfo().IsRGB() ? 3 : 1;
                    Parallel.ForEach(planes, plane =>
                    {
                        Parallel.ForEach(realChannels, channel =>
                        {
                            int[] sampleHist = null, referenceHist = null, srcHist = null;

                            Parallel.Invoke(
                                () => sampleHist    = GetHistogram(sampleFrames, sampleMaskFrame, pixelSize, channel, plane, Sample.GetVideoInfo().pixel_type, sampleBits, false),
                                () => referenceHist = GetHistogram(referenceFrames, refMaskFrame, pixelSize, channel, plane, Reference.GetVideoInfo().pixel_type, referenceBits, LimitedRange),
                                () => srcHist       = Extrapolation ? GetHistogram(inputFrames, null, pixelSize, channel, plane, Child.GetVideoInfo().pixel_type, sampleBits, LimitedRange) : null);

                            var map = GetTransitionMap(sampleHist, referenceHist, n, plane);

                            if (Extrapolation)
                            {
                                Extrapolate(map, srcHist, GetLowColor(referenceBits), GetHighColor(referenceBits, plane));
                            }

                            Interpolate(map, GetLowColor(referenceBits), GetHighColor(referenceBits, plane), sampleBits, referenceBits);

                            if (Intensity < 1)
                            {
                                var decreased = new ColorMap(sampleBits, n, Dither);
                                for (var color = 0; color < 1 << sampleBits; color++)
                                {
                                    decreased.Add(color, map.Average(color) * Intensity + color * (1 - Intensity));
                                }
                                map = decreased;
                            }

                            var tuple = map.GetColorsAndWeights();

                            NativeUtils.ApplyColorMap(DynamicNoise ? n : 0,
                                                      input.GetReadPtr(plane), input.GetPitch(plane), sampleBits > 8,
                                                      output.GetWritePtr(plane), output.GetPitch(plane), referenceBits > 8,
                                                      input.GetRowSize(plane), input.GetHeight(plane), pixelSize, channel,
                                                      map.FixedMap, tuple.Item1, tuple.Item2);
                        });
                    });
                }

            if (!writable)
            {
                input.Dispose();
            }
            sampleFrames.ForEach(p => p.Dispose());
            referenceFrames.ForEach(p => p.Dispose());
            return(output);
        }