public void TestBucketIndexSupport() { SetUp(); InitDe(); int[] bucketIndices = de.GetBucketIndices(dt); Console.WriteLine($"bucket indices: {Arrays.ToString(bucketIndices)}"); List <EncoderResult> bucketInfo = de.GetBucketInfo(bucketIndices); List <Double> expectedList = new List <double> { 320.25, 3.5, .167, 14.8 }; List <int> encodings = new List <int>(); for (int i = 0; i < bucketInfo.Count; i++) { EncoderResult r = bucketInfo[i]; double actual = (double)r.GetValue(); double expected1 = expectedList[i]; Assert.AreEqual(expected1, actual, 4.0); encodings.AddRange(r.GetEncoding()); } Assert.IsTrue(expected.SequenceEqual(encodings.ToArray())); }
public void TestTopDownCompute() { SetUp(); InitDe(); List <EncoderResult> topDown = de.TopDownCompute(bits); List <Double> expectedList = new List <double> { 320.25, 3.5, .167, 14.8 }; for (int i = 0; i < topDown.Count; i++) { EncoderResult r = topDown[i]; double actual = (double)r.GetValue(); double expected = expectedList[i]; Assert.AreEqual(expected, actual, 4.0); } }
public StopEncoderFallback(EncoderResult result) { this.result = result; }
public StopEncoderFallbackBuffer(StopEncoderFallback fallback) { this.result = fallback.result ?? new EncoderResult(); }
public static PhpString iconv(Context ctx, string in_charset, string out_charset, PhpString str) { // check args if (str.IsDefault) { PhpException.ArgumentNull("str"); return(default(PhpString)); } if (out_charset == null) { PhpException.ArgumentNull("out_charset"); return(default(PhpString)); } // resolve out_charset bool transliterate, discard_ilseq; out_charset = ParseOutputEncoding(out_charset, out transliterate, out discard_ilseq); var out_encoding = ResolveEncoding(ctx, out_charset); if (out_encoding == null) { PhpException.Throw(PhpError.Notice, Resources.LibResources.wrong_charset, out_charset, in_charset, out_charset); return(default(PhpString)); } // encoding fallback EncoderFallback enc_fallback; var out_result = new EncoderResult(); if (transliterate) { enc_fallback = new TranslitEncoderFallback(); // transliterate unknown characters } else if (discard_ilseq) { enc_fallback = new IgnoreEncoderFallback(); // ignore character and continue } else { enc_fallback = new StopEncoderFallback(out_result); // throw notice and discard all remaining characters } //// out_encoding.Clone() ensures it is NOT readOnly //// then set EncoderFallback to catch handle unconvertable characters //out_encoding = (Encoding)out_encoding.Clone(); out_encoding = Encoding.GetEncoding(out_encoding.CodePage, enc_fallback, DecoderFallback.ExceptionFallback); try { // if (str.ContainsBinaryData) { // resolve in_charset if (in_charset == null) { PhpException.ArgumentNull("in_charset"); return(default(PhpString)); } var in_encoding = ResolveEncoding(ctx, in_charset); if (in_encoding == null) { PhpException.Throw(PhpError.Notice, Resources.LibResources.wrong_charset, in_charset, in_charset, out_charset); return(default(PhpString)); } // TODO: in_encoding.Clone() ensures it is NOT readOnly, then set DecoderFallback to catch invalid byte sequences // convert <in_charset> to <out_charset> return(new PhpString(out_encoding.GetBytes(str.ToString(in_encoding)))); } else { // convert UTF16 to <out_charset> return(new PhpString(str.ToBytes(out_encoding))); } } finally { if (out_result.firstFallbackCharIndex >= 0) { // Notice: iconv(): Detected an illegal character in input string PhpException.Throw(Core.PhpError.Notice, Resources.LibResources.illegal_character); } } }
public void TestNonPeriodicBottomUp() { SetUp(); builder.Name("day of week") .W(5) .N(14) .Radius(1.0) .MinVal(1.0) .MaxVal(10.0) .Periodic(false) .Forced(true); InitSe(); Console.WriteLine(string.Format("Testing non-periodic encoder encoding resolution of {0}", se.GetResolution())); Assert.IsTrue(Arrays.AreEqual(se.Encode(1d), new[] { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 })); Assert.IsTrue(Arrays.AreEqual(se.Encode(2d), new[] { 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 })); Assert.IsTrue(Arrays.AreEqual(se.Encode(10d), new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 })); // Test that we get the same encoder when we construct it using resolution // instead of n SetUp(); builder.Name("day of week") .W(5) .Radius(5.0) .MinVal(1.0) .MaxVal(10.0) .Periodic(false) .Forced(true); InitSe(); double v = se.GetMinVal(); while (v < se.GetMaxVal()) { int[] output = se.Encode(v); DecodeResult decodedInWhile = (DecodeResult)se.Decode(output, ""); Console.WriteLine("decoding " + Arrays.ToString(output) + string.Format("({0})=>", v) + se.DecodedToStr(decodedInWhile)); Assert.AreEqual(decodedInWhile.GetFields().Count, 1, 0); List <RangeList> rangeListInWhile = new List <RangeList>(decodedInWhile.GetFields().Values); Assert.AreEqual(rangeListInWhile[0].Count, 1, 0); MinMax minMax = rangeListInWhile[0].GetRanges()[0]; Assert.AreEqual(minMax.Min(), minMax.Max(), 0); Assert.IsTrue(Math.Abs(minMax.Min() - v) <= se.GetResolution()); List <EncoderResult> topDowns = se.TopDownCompute(output); EncoderResult topDown = topDowns[0]; Console.WriteLine("topDown => " + topDown); Assert.IsTrue(Arrays.AreEqual(topDown.GetEncoding(), output)); Assert.IsTrue(Math.Abs(((double)topDown.GetValue()) - v) <= se.GetResolution()); //Test bucket support int[] bucketIndices = se.GetBucketIndices(v); Console.WriteLine("bucket index => " + bucketIndices[0]); topDown = se.GetBucketInfo(bucketIndices)[0]; Assert.IsTrue(Math.Abs(((double)topDown.GetValue()) - v) <= se.GetResolution() / 2); Assert.AreEqual(topDown.GetScalar(), (int)Convert.ChangeType(topDown.GetValue(), typeof(int))); Assert.IsTrue(Arrays.AreEqual(topDown.GetEncoding(), output)); // Next value v += se.GetResolution() / 4; } // Make sure we can fill in holes DecodeResult decoded = (DecodeResult)se.Decode(new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1 }, ""); Assert.AreEqual(decoded.GetFields().Count, 1, 0); List <RangeList> rangeList = new List <RangeList>(decoded.GetFields().Values); Assert.AreEqual(1, rangeList[0].Count, 0); Console.WriteLine("decodedToStr of " + rangeList + " => " + se.DecodedToStr(decoded)); decoded = (DecodeResult)se.Decode(new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1 }, ""); Assert.AreEqual(decoded.GetFields().Count, 1, 0); rangeList = new List <RangeList>(decoded.GetFields().Values); Assert.AreEqual(1, rangeList[0].Count, 0); Console.WriteLine("decodedToStr of " + rangeList + " => " + se.DecodedToStr(decoded)); // Test min and max SetUp(); builder.Name("scalar") .W(3) .MinVal(1.0) .MaxVal(10.0) .Periodic(false) .Forced(true); InitSe(); List <EncoderResult> decode = se.TopDownCompute(new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 }); Assert.AreEqual(10, (double)decode[0].GetScalar(), 0); decode = se.TopDownCompute(new[] { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); Assert.AreEqual(1, (double)decode[0].GetScalar(), 0); // Make sure only the last and first encoding encodes to max and min, and there is no value greater than max or min SetUp(); builder.Name("scalar") .W(3) .N(140) .Radius(1.0) .MinVal(1.0) .MaxVal(141.0) .Periodic(false) .Forced(true); InitSe(); List <int[]> iterlist = new List <int[]>(); for (int i = 0; i < 137; i++) { iterlist.Add(new int[140]); ArrayUtils.SetRangeTo(iterlist[i], i, i + 3, 1); decode = se.TopDownCompute(iterlist[i]); int value = (int)decode[0].GetScalar(); Assert.IsTrue(value <= 141); Assert.IsTrue(value >= 1); Assert.IsTrue(value < 141 || i == 137); Assert.IsTrue(value > 1 || i == 0); } // ------------------------------------------------------------------------- // Test the input description generation and top-down compute on a small number // non-periodic encoder SetUp(); builder.Name("scalar") .W(3) .N(15) .MinVal(.001) .MaxVal(.002) .Periodic(false) .Forced(true); InitSe(); Console.WriteLine(string.Format("\nTesting non-periodic encoder decoding resolution of {0}...", se.GetResolution())); v = se.GetMinVal(); while (v < se.GetMaxVal()) { int[] output = se.Encode(v); decoded = (DecodeResult)se.Decode(output, ""); Console.WriteLine(string.Format("decoding ({0})=>", v) + " " + se.DecodedToStr(decoded)); Assert.AreEqual(decoded.GetFields().Count, 1, 0); rangeList = new List <RangeList>(decoded.GetFields().Values); Assert.AreEqual(rangeList[0].Count, 1, 0); MinMax minMax = rangeList[0].GetRanges()[0]; Assert.AreEqual(minMax.Min(), minMax.Max(), 0); Assert.IsTrue(Math.Abs(minMax.Min() - v) <= se.GetResolution()); decode = se.TopDownCompute(output); Console.WriteLine("topdown => " + Arrays.ToString(decode.ToArray())); Console.WriteLine("{0} <= {1}", Math.Abs(decode[0].GetScalar() - v), se.GetResolution() / 2); Assert.IsTrue(Math.Abs(decode[0].GetScalar() - v) <= se.GetResolution() / 2); v += (se.GetResolution() / 4); } // ------------------------------------------------------------------------- // Test the input description generation on a large number, non-periodic encoder SetUp(); builder.Name("scalar") .W(3) .N(15) .MinVal(1.0) .MaxVal(1000000000.0) .Periodic(false) .Forced(true); InitSe(); Console.WriteLine(string.Format("\nTesting non-periodic encoder decoding resolution of {0}...", se.GetResolution())); v = se.GetMinVal(); while (v < se.GetMaxVal()) { int[] output = se.Encode(v); decoded = (DecodeResult)se.Decode(output, ""); Console.WriteLine(string.Format("decoding ({0})=>", v) + " " + se.DecodedToStr(decoded)); Assert.AreEqual(decoded.GetFields().Count, 1, 0); rangeList = new List <RangeList>(decoded.GetFields().Values); Assert.AreEqual(rangeList[0].Count, 1, 0); MinMax minMax = rangeList[0].GetRanges()[0]; Assert.AreEqual(minMax.Min(), minMax.Max(), 0); Assert.IsTrue(Math.Abs(minMax.Min() - v) <= se.GetResolution()); decode = se.TopDownCompute(output); Console.WriteLine("topdown => " + decode); Assert.IsTrue(Math.Abs(decode[0].GetScalar() - v) <= se.GetResolution() / 2); v += (se.GetResolution() / 4); } }
public void TestDecodeAndResolution() { SetUp(); builder.Name("scalar"); InitSe(); double resolution = se.GetResolution(); StringBuilder @out = new StringBuilder(); for (double v = se.GetMinVal(); v < se.GetMaxVal(); v += (resolution / 4.0d)) { int[] output = se.Encode(v); DecodeResult decodedInFor = (DecodeResult)se.Decode(output, ""); Console.WriteLine(@out.Append("decoding ").Append(Arrays.ToString(output)).Append(" ("). Append(string.Format("{0}", v)).Append(")=> ").Append(se.DecodedToStr(decodedInFor))); @out.Length = 0; Map <string, RangeList> fieldsMapInFor = decodedInFor.GetFields(); Assert.AreEqual(1, fieldsMapInFor.Count); RangeList ranges = new List <RangeList>(fieldsMapInFor.Values)[0]; Assert.AreEqual(1, ranges.Count); Assert.AreEqual(ranges.GetRange(0).Min(), ranges.GetRange(0).Max(), 0); Assert.IsTrue(ranges.GetRange(0).Min() - v < se.GetResolution()); EncoderResult topDown = se.TopDownCompute(output)[0]; Console.WriteLine("topdown => " + topDown); Assert.IsTrue(Arrays.AreEqual(topDown.GetEncoding(), output)); Assert.IsTrue(Math.Abs(((double)topDown.Get(1)) - v) <= se.GetResolution() / 2); //Test bucket support int[] bucketIndices = se.GetBucketIndices(v); Console.WriteLine("bucket index => " + bucketIndices[0]); topDown = se.GetBucketInfo(bucketIndices)[0]; Assert.IsTrue(Math.Abs(((double)topDown.Get(1)) - v) <= se.GetResolution() / 2); Assert.AreEqual(topDown.Get(1), se.GetBucketValues <double>(typeof(double)).ToArray()[bucketIndices[0]]); Assert.AreEqual(topDown.Get(2), topDown.Get(1)); Assert.IsTrue(Arrays.AreEqual(topDown.GetEncoding(), output)); } // ----------------------------------------------------------------------- // Test the input description generation on a large number, periodic encoder SetUp(); builder.Name("scalar") .W(3) .Radius(1.5) .MinVal(1.0) .MaxVal(8.0) .Periodic(true) .Forced(true); InitSe(); Console.WriteLine("\nTesting periodic encoder decoding, resolution of " + se.GetResolution()); //Test with a "hole" int[] encoded = new[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }; DecodeResult decoded = (DecodeResult)se.Decode(encoded, ""); Map <string, RangeList> fieldsMap = decoded.GetFields(); Assert.AreEqual(1, fieldsMap.Count); Assert.AreEqual(1, decoded.GetRanges("scalar").Count); Assert.AreEqual("7.5, 7.5", decoded.GetRanges("scalar").GetRange(0).ToString()); //Test with something wider than w, and with a hole, and wrapped encoded = new[] { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }; decoded = (DecodeResult)se.Decode(encoded, ""); fieldsMap = decoded.GetFields(); Assert.AreEqual(1, fieldsMap.Count); Assert.AreEqual(2, decoded.GetRanges("scalar").Count); Assert.AreEqual("7.5, 8.0", decoded.GetRanges("scalar").GetRange(0).ToString()); //Test with something wider than w, no hole encoded = new[] { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; decoded = (DecodeResult)se.Decode(encoded, ""); fieldsMap = decoded.GetFields(); Assert.AreEqual(1, fieldsMap.Count); Assert.AreEqual(1, decoded.GetRanges("scalar").Count); Assert.AreEqual(decoded.GetRanges("scalar").GetRange(0).ToString(), "1.5, 2.5"); //Test with 2 ranges encoded = new[] { 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0 }; decoded = (DecodeResult)se.Decode(encoded, ""); fieldsMap = decoded.GetFields(); Assert.AreEqual(1, fieldsMap.Count); Assert.AreEqual(2, decoded.GetRanges("scalar").Count); Assert.AreEqual(decoded.GetRanges("scalar").GetRange(0).ToString(), "1.5, 1.5"); Assert.AreEqual(decoded.GetRanges("scalar").GetRange(1).ToString(), "5.5, 6.0"); //Test with 2 ranges, 1 of which is narrower than w encoded = new[] { 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0 }; decoded = (DecodeResult)se.Decode(encoded, ""); fieldsMap = decoded.GetFields(); Assert.AreEqual(1, fieldsMap.Count); Assert.AreEqual(2, decoded.GetRanges("scalar").Count); Assert.AreEqual(decoded.GetRanges("scalar").GetRange(0).ToString(), "1.5, 1.5"); Assert.AreEqual(decoded.GetRanges("scalar").GetRange(1).ToString(), "5.5, 6.0"); }
private static EncoderResult Encoder(string inputFile, string outputPath, BasicMediaFormatInfo info, int videoIndex, int audioIndex) { EncoderResult encoderResult = new EncoderResult(); encoderResult.VideoInfo = null; encoderResult.AudioInfo = null; if (videoIndex != -1 && info.MediaInfo[videoIndex].CodecType == "video" && info.MediaInfo[videoIndex].CodecName != "mjpeg" && info.MediaInfo[videoIndex].CodecName != "png") { encoderResult.VideoInfo = new BasicVideoInfo((BasicVideoInfo)info.MediaInfo[videoIndex]); } if (audioIndex != -1 && info.MediaInfo[audioIndex].CodecType == "audio") { encoderResult.AudioInfo = new BasicAudioInfo((BasicAudioInfo)info.MediaInfo[audioIndex]); } encoderResult.Extension = null; if (encoderResult.VideoInfo != null) { encoderResult.Extension = ".mp4"; } else if (encoderResult.AudioInfo != null) { if (encoderResult.AudioInfo.CodecName == "vorbis" && encoderResult.AudioInfo.Channels <= 2) { encoderResult.Extension = ".ogg"; } else { encoderResult.Extension = ".m4a"; } } else { throw new Exception("\"" + inputFile + "\" does not have multimedia content."); } StringBuilder config = new StringBuilder(); if (encoderResult.VideoInfo != null) { if (encoderResult.VideoInfo.Width * encoderResult.VideoInfo.Height > 1920 * 1080) { double scale = 1920.0 / encoderResult.VideoInfo.Width; double heightScale = 1080.0 / encoderResult.VideoInfo.Height; if (scale > heightScale) { scale = heightScale; } encoderResult.VideoInfo.Width = (int)(encoderResult.VideoInfo.Width * scale); encoderResult.VideoInfo.Height = (int)(encoderResult.VideoInfo.Height * scale); } if (encoderResult.VideoInfo.Width * encoderResult.VideoInfo.Height > 1280 * 720 && encoderResult.VideoInfo.FrameRate > 30.0) { encoderResult.VideoInfo.FrameRate = 30.0; } if (encoderResult.VideoInfo.FrameRate > 60.0) { encoderResult.VideoInfo.FrameRate = 60.0; } if (encoderResult.VideoInfo.CodecName == "h264" && encoderResult.VideoInfo.Width == ((BasicVideoInfo)info.MediaInfo[videoIndex]).Width && encoderResult.VideoInfo.Height == ((BasicVideoInfo)info.MediaInfo[videoIndex]).Height && encoderResult.VideoInfo.FrameRate == ((BasicVideoInfo)info.MediaInfo[videoIndex]).FrameRate && encoderResult.VideoInfo.Level <= 4.2) { config.Append("-map 0:"); config.Append(videoIndex); config.Append(" -c:v copy"); } else { config.Append("-map 0:"); config.Append(videoIndex); config.Append(" -c:v libx264 -preset fast -crf 25 -s "); config.Append(encoderResult.VideoInfo.Width); config.Append("x"); config.Append(encoderResult.VideoInfo.Height); config.Append(" -r "); config.Append(encoderResult.VideoInfo.FrameRate); } } if (encoderResult.AudioInfo != null) { if (encoderResult.VideoInfo != null) { config.Append(" "); } if ((encoderResult.AudioInfo.CodecName == "aac" || encoderResult.AudioInfo.CodecName == "vorbis") && encoderResult.AudioInfo.Channels <= 2) { config.Append("-map 0:"); config.Append(audioIndex); config.Append(" -c:a copy"); } else { config.Append("-map 0:"); config.Append(audioIndex); config.Append(" -c:a aac -ac 2"); } } if (info.Size > 4294967296) { int bitrate = 0; double totalDuration = 0; if (encoderResult.VideoInfo != null && encoderResult.AudioInfo == null) { bitrate = encoderResult.VideoInfo.Bitrate; totalDuration = encoderResult.VideoInfo.Duration; } else if (encoderResult.VideoInfo == null && encoderResult.AudioInfo != null) { bitrate = encoderResult.AudioInfo.Bitrate; totalDuration = encoderResult.AudioInfo.Duration; } else if (encoderResult.VideoInfo == null && encoderResult.AudioInfo == null) { bitrate = encoderResult.VideoInfo.Bitrate + encoderResult.AudioInfo.Bitrate; totalDuration = encoderResult.VideoInfo.Duration; } if (bitrate == 0) { bitrate = info.Bitrate; } if (totalDuration == 0) { totalDuration = info.Duration; } double duration = 33822867456.0 / bitrate; double start = 0.0; int parts = (int)(totalDuration / duration); if (totalDuration % duration != 0.0) { parts++; } encoderResult.Name = new string[parts]; for (int i = 0; i < parts; i++) { encoderResult.Name[i] = Path.GetFileNameWithoutExtension(inputFile) + " part " + (i + 1).ToString(); string output = Path.Combine(outputPath, MediaInjector.ValidFilename(encoderResult.Name[i]) + encoderResult.Extension); string cut = "-ss " + start.ToString() + " -noaccurate_seek -t " + duration.ToString(); string arguments = cut + " -i \"" + inputFile + "\" " + config.ToString() + " \"" + output + "\""; FFmpeg(arguments); start += duration; } } else { encoderResult.Name = new string[1]; encoderResult.Name[0] = Path.GetFileNameWithoutExtension(inputFile); string output = Path.Combine(outputPath, MediaInjector.ValidFilename(encoderResult.Name[0]) + encoderResult.Extension); string arguments = "-i \"" + inputFile + "\" " + config.ToString() + " \"" + output + "\""; FFmpeg(arguments); } return(encoderResult); }