// 非公開メソッド private ulong[][] _createItemBufferMap(PropBinaryBuilder binBuilder, PropSectionCollection sections) { var itemBufferMap = new ulong[sections.Count][]; var currentPosition = 0uL; var ns = new NullStream(); for (var i = 0; i < sections.Count; i++) { var section = sections[i]; itemBufferMap[i] = new ulong[section.Items.Count]; for (var j = 0; j < section.Items.Count; j++) { var item = section.Items[j]; ns.Seek(0, SeekOrigin.Begin); // Reference アルゴリズム未実装 var bufferMode = PropItemBufferMode.Buffered; if (item.IsNull) { bufferMode = PropItemBufferMode.Null; } binBuilder.WriteItemBuffer(ns, item, bufferMode); itemBufferMap[i][j] = currentPosition; currentPosition += (ulong)ns.Position; } } return(itemBufferMap); }
public void CollectAndSerialize() { using (var stream = new NullStream()) { ScrapeHandler.Process(_registry, stream); } }
public static void MetricForResponseSize(this Module module, string metricName, Predicate <RouteDescription> routePredicate) { var histogram = HapperGlobalMetrics.GlobalMetricsContext.Histogram(metricName, Unit.Custom("bytes")); module.After.AddItemToEndOfPipeline(ctx => { if (routePredicate(ctx.ResolvedRoute.Description)) { string lengthHeader; // if available use content length header if (ctx.Response.Headers.TryGetValue("Content-Length", out lengthHeader)) { long length; if (long.TryParse(lengthHeader, out length)) { histogram.Update(length); } } else { // if no content length - get the length of the stream // this might be suboptimal for some types of requests using (var ns = new NullStream()) { ctx.Response.Contents(ns); histogram.Update(ns.Length); } } } }); }
public static void MetricForResponseSize(this INancyModule module, string metricName, Predicate <RouteDescription> routePredicate) { var name = string.Format("{0}.{1}", module.GetType().Name, metricName); CheckNancyMetricsIsConfigured(); var histogram = NancyMetrics.CurrentConfig.Registry.Histogram(name, Unit.Custom("bytes"), SamplingType.FavourRecent); module.After.AddItemToEndOfPipeline(ctx => { if (routePredicate(ctx.ResolvedRoute.Description)) { string lengthHeader; // if available use content length header if (ctx.Response.Headers.TryGetValue("Content-Length", out lengthHeader)) { long length; if (long.TryParse(lengthHeader, out length)) { histogram.Update(length); } } else { // if no content length - get the length of the stream // this might be suboptimal for some types of requests using (var ns = new NullStream()) { ctx.Response.Contents(ns); histogram.Update(ns.Length); } } } }); }
public void SeekTest() { var target = new NullStream(); Assert.AreEqual(0, target.Position); target.Seek(10, SeekOrigin.Begin); Assert.AreEqual(0, target.Position); }
protected virtual long MeasureOverride() { var nullStream = new NullStream(); var boundedStream = new BoundedStream(nullStream); Serialize(boundedStream, null, false); return(boundedStream.RelativePosition); }
private void CreateStream(int inputChannels, int outputChannels, int lengthInSeconds, out IAudioStream sourceStream, out MonoStream monoStream) { sourceStream = new NullStream( new AudioProperties(inputChannels, 44100, 32, AudioFormat.IEEE), 44100 * inputChannels * 4 /*bytesPerSample*/ * lengthInSeconds); monoStream = new MonoStream(sourceStream, outputChannels); }
public void ConstructorTest() { var target = new NullStream(); Assert.IsTrue(target.CanWrite); Assert.IsTrue(target.CanRead); Assert.IsFalse(target.CanSeek); Assert.AreEqual(0, target.Position); Assert.AreEqual(0, target.Length); }
public void SetLengthTest() { var target = new NullStream(); Assert.AreEqual(0, target.Length); target.SetLength(10); Assert.AreEqual(0, target.Length); }
public void PositionTest() { var target = new NullStream(); Assert.AreEqual(0, target.Position); target.Position = 10; Assert.AreEqual(0, target.Position); }
public void ReadAsyncTest() { var target = new NullStream(); var buffer = new byte[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Assert.AreEqual(0, target.ReadAsync(buffer, 0, buffer.Length).Result); Assert.IsTrue(buffer.SequenceEqual(new byte[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })); }
protected virtual void Dispose(bool native) { if (_outp != null) { _outp.Dispose(); _outp = null; } if (_history != null) { _history.Dispose(); _history = null; } }
private static Stream Open(IntPtr handle, FileAccess access, int bufferSize) { Stream result; try { result = new FileStream(handle, access, false, bufferSize, false, bufferSize == 0); } catch (IOException) { result = new NullStream(); } return(result); }
public void CopyToAsyncTest() { var target = new NullStream(); target.Write(new byte[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, 0, 10); using (var stream = new MemoryStream()) { target.CopyToAsync(stream).Wait(); Assert.AreEqual(0, stream.Position); Assert.AreEqual(0, stream.Length); } }
protected override IEnumerable <long> MeasureItemsOverride() { var nullStream = new NullStream(); var boundedStream = new BoundedStream(nullStream); var serializableChildren = GetSerializableChildren(); return(serializableChildren.Select(child => { boundedStream.RelativePosition = 0; child.Serialize(boundedStream, null); return boundedStream.RelativePosition; })); }
private void CheckAndSetContentLength(Response response) { if (this.Headers.ContainsKey(ContentLength)) { return; } using (var nullStream = new NullStream()) { response.Contents.Invoke(nullStream); this.Headers[ContentLength] = nullStream.Length.ToString(CultureInfo.InvariantCulture); } }
public void WriteAsyncTest() { var target = new NullStream(); var buffer = new byte[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Assert.AreEqual(0, target.Position); Assert.AreEqual(0, target.Length); target.WriteAsync(buffer, 0, buffer.Length).Wait(); Assert.AreEqual(0, target.Position); Assert.AreEqual(0, target.Length); }
static byte[] GetHash(string fullPath) { try { using (var hasher = MD5.Create()) { using (var outStream = new NullStream()) { using (var csStream = new CryptoStream(outStream, hasher, CryptoStreamMode.Write)) new BinaryWriter(csStream).Write(File.ReadAllBytes(fullPath)); } return(hasher.Hash); } } catch { } return(null); }
private static long GetStreamCompressedSize(Stream stream) { using (var nullStream = new NullStream()) { using (var compressionStream = new GZipStream(nullStream, CompressionMode.Compress, true)) { // Copy to compression stream without allocating int read; while ((read = stream.Read(s_Buffer, 0, s_Buffer.Length)) != 0) { compressionStream.Write(s_Buffer, 0, read); } } return(nullStream.Length); } }
public void Compression() { Benchmark.Run("Compressing", 1024*1024*2, benchmark => { var stream = new NullStream(); var compressor = new SnappyDOTNETStream(stream, CompressionMode.Compress); benchmark.Stopwatch.Start(); for (int i = 0; i < benchmark.Iterations; ++i) { compressor.Write(benchmark.Input, 0, benchmark.Input.Length); compressor.Flush(); } benchmark.Stopwatch.Stop(); benchmark.Note = String.Format(" ({0:0.00 %})", stream.Written / (double)benchmark.Input.Length / benchmark.Iterations); }); }
void Update(BlockInfo blockInfo, Block block) { using (var hasher = MD5.Create()) { bool emptyHash; using (var outStream = new NullStream()) { using (var csStream = new CryptoStream(outStream, hasher, CryptoStreamMode.Write)) { var writer = new BinaryWriter(csStream); Update(writer, blockInfo, block); } emptyHash = outStream.Length == 0; } if (!emptyHash) { blockInfo.Hash = hasher.Hash; } } }
public PythonCore(IConsole term) { Dictionary <String, Object> options = new Dictionary <string, object>(); options["DivisionOptions"] = PythonDivisionOptions.New; _history = new NullStream(); _outp = new EventRaisingStreamWriter(_history); _terminal = term; _outp.StringWritten += new EventHandler <MyEvtArgs <string> >(_outp_StringWritten); _engine = Python.CreateEngine(options); _engine.Runtime.IO.SetOutput(_history, _outp); _engine.Runtime.IO.SetErrorOutput(_history, _outp); _scope = _engine.CreateScope(); _snytax = new PythonSystax(); _snytax.Engine = _engine; TrigMode = TrigMode.Deg; }
internal override void DeserializeOverride(BoundedStream stream, EventShuttle eventShuttle) { var rootStream = GetRootStream(stream); var length = GetFieldLength(); Value = length != null ? new Streamlet(rootStream, rootStream.Position, length.ByteCount) : new Streamlet(rootStream, rootStream.Position); if (length != null) { var nullStream = new NullStream(); stream.CopyTo(nullStream, (int)length.ByteCount, CopyToBufferSize); } else { stream.Seek(0, SeekOrigin.End); } }
internal override async Task DeserializeOverrideAsync(BoundedStream stream, EventShuttle eventShuttle, CancellationToken cancellationToken) { var rootStream = GetRootStream(stream); var length = GetFieldLength(); Value = length != null ? new Streamlet(rootStream, rootStream.Position, length.ByteCount) : new Streamlet(rootStream, rootStream.Position); if (length != null) { var nullStream = new NullStream(); await stream.CopyToAsync(nullStream, (int)length.ByteCount, CopyToBufferSize, cancellationToken) .ConfigureAwait(false); } else { stream.Seek(0, SeekOrigin.End); } }
public virtual void ExtractFile(FileInfo fileInfo, string targetFileName, bool overwriteExisting) { CFFOLDER containingFolder = m_folders[fileInfo.CFFILE.iFolder]; using (System.IO.Stream input = System.IO.File.Open(FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) { int toRead = (int)fileInfo.CFFILE.cbFile; CabDecompressor decompressor = new CabDecompressor((CompressionType)containingFolder.typeCompress); CabFolderStream folderStream = new CabFolderStream(input, containingFolder.coffCabStart, containingFolder.Compressed); // skip to the beginning of the file. this is done by decompressing into a null stream if (fileInfo.CFFILE.uoffFolderStart > 0) { using (NullStream ns = new NullStream()) { decompressor.DecompressBlock((int)fileInfo.CFFILE.uoffFolderStart, folderStream.SizeCompressed, folderStream.SizeUncompressed, folderStream, ns); } } using (SizeLimitedOutputStream output = new SizeLimitedOutputStream(targetFileName, System.IO.FileMode.OpenOrCreate, (int)fileInfo.CFFILE.cbFile)) { while (toRead > 0) { int nextBlock = Math.Min(toRead, folderStream.SizeUncompressed); if (folderStream.SizeUncompressed == -1) { nextBlock = toRead; } //System.Diagnostics.Debug.WriteLine(string.Format("Reading {0} bytes", nextBlock)); decompressor.DecompressBlock(nextBlock, folderStream.SizeCompressed, folderStream.SizeUncompressed, folderStream, output); toRead -= nextBlock; output.Flush(); } } } }
static byte[] GetHash(string fullPath) { try { using (var reader = new BinaryReader(new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read))) { reader.BaseStream.Position = HASH_FILE_OFFSET; var bytes = reader.ReadBytes(HASH_SIZE); if (bytes.Length != HASH_SIZE) { return(null); } using (var hasher = MD5.Create()) { using (var outStream = new NullStream()) { using (var csStream = new CryptoStream(outStream, hasher, CryptoStreamMode.Write)) new BinaryWriter(csStream).Write(bytes); } return(hasher.Hash); } } } catch { } return(null); }
private static void RunClient(ProgramOptions options) { var display = new Display(options, options.Port) {Delay = .5.Seconds()}; byte[] aesIV = null; using (var client = new TcpClient().Do(c => c.Connect(options.Host, options.Port))) using (var netStream = client.GetStream().Do(s => aesIV = ClientHandshake(s, options))) using (var compStream = options.Compression.HasValue ? new DeflateStream(netStream, CompressionMode.Decompress) : null) using (var aes = CreateAes(options, aesIV)) using (var dec = aes?.CreateDecryptor()) using (var cryptoStream = aes != null ? new CryptoStream((Stream)compStream ?? netStream, dec, CryptoStreamMode.Read) : null) using (var reader = new BinaryReader(cryptoStream ?? (Stream)compStream ?? netStream)) using (var nullStream = new NullStream()) { display.Stopwatch.Start(); var buffer = new byte[Extensions.DefaultStreamCopyBufferSize]; while (true) { display.Refresh(); string path; try { path = reader.ReadString(); } catch (EndOfStreamException) { break; } var length = reader.ReadInt64(); var file = options.Directory.GetFile(path); display.CurrentFile = new FileProgress(path, length).Do(x => x.Stopwatch.Start()); Action<long> progress = b => { display.ByteCount += (display.CurrentFile.BytesTransferred = b); display.Refresh(); }; var skipFile = (file.Exists && !options.Overwrite); if (skipFile) { reader.BaseStream.CopyTo(nullStream, length, buffer, progress); } else { if (!file.Directory.Exists) file.Directory.Create(); using (var fileStream = file.Open(FileMode.Create, FileAccess.Write, FileShare.None)) reader.BaseStream.CopyTo(fileStream, length, buffer, progress); } display.FileCount++; } } }
static public void Main(string[] args) { ConfirmQuickSyncReadiness.HaltIfNotReady(); Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; // keep ascending directories until 'media' folder is found for (int i = 0; i < 10 && !Directory.Exists("Media"); i++) { Directory.SetCurrentDirectory(".."); } Directory.SetCurrentDirectory("Media"); mfxIMPL impl = mfxIMPL.MFX_IMPL_AUTO; CodecId inputCodecId = CodecId.MFX_CODEC_JPEG; CodecId outputCodecId = CodecId.MFX_CODEC_JPEG; string outputExtension = ".transcoded.264";//this should match codecld above string inFilename; inFilename = @"C:\x\core-imaging-playground\images\IMG_2301.jpg"; // inFilename = "BigBuckBunny_320x180.264"; //inFilename = "BigBuckBunny_1920x1080.264"; //inFilename = "BigBuckBunny_3840x2160.264"; string outFilename = Path.ChangeExtension(inFilename, outputExtension); Console.WriteLine("Working directory: {0}", Environment.CurrentDirectory); Console.WriteLine("Input filename: {0}", inFilename); Console.WriteLine(); if (!File.Exists(inFilename)) { Console.WriteLine("Input file not found. Press any key to exit."); Console.ReadKey(); return; } Stream infs, outfs; BenchmarkTimer bt = null; #if !ENABLE_BENCHMARK infs = File.Open(inFilename, FileMode.Open); outfs = File.Open(outFilename, FileMode.Create); #else // delete this code for most simple example // * Benchmark Mode * // this block does a couple things: // 1. causes the file to be pre-read into memory so we are not timing disk reads. // 2. replaces the output stream with a NullStream so nothing gets written to disk. // 3. Starts the timer for benchmarking // this pre-reads file into memory for benchmarking // maximumMemoryToAllocate = (long)4L * 1024 * 1024 * 1024; Console.WriteLine("Pre-reading input"); infs = new PreReadLargeMemoryStream(File.Open(inFilename, FileMode.Open)); Console.WriteLine("Input read"); outfs = new NullStream(); bt = new BenchmarkTimer(); bt.Start(); //int minimumFrames = 4000; #endif Console.WriteLine("Output filename: {0}", Path.GetFileName((outfs as FileStream)?.Name ?? "NO OUTPUT")); Console.WriteLine(); var config = TranscoderConfiguration.BuildTranscoderConfigurationFromStream(infs, inputCodecId, outputCodecId); var transcoder = new StreamTranscoder(infs, config, impl, false); string impltext = QuickSyncStatic.ImplementationString(transcoder.lowLevelTranscoder.session); Console.WriteLine("Implementation = {0}", impltext); //string memtext = QuickSyncStatic.ImplementationString(transcoder.lowLevelTranscoder.deviceSetup.memType); //Console.WriteLine("Memory type = {0}", memtext); int modulo = 100; int count = 0; foreach (var item in transcoder.GetFrames()) { outfs.Write(item.bitstream, 0, item.bytesAvailable); if (++count % modulo == 0) { Console.Write("Frames transcoded {0}\r", count); } } Console.WriteLine("Frames transcoded {0}", count); Console.WriteLine(); if (bt != null) { bt.StopAndReport(count, infs.Position, outfs.Position); } infs.Close(); outfs.Close(); if (Debugger.IsAttached) { Console.WriteLine("done - press a key to exit"); Console.ReadKey(); } }
public NullCommunicationsChannel() : base() { _Stream = new NullStream(); }
/// <summary> /// Builds a message from buffer. /// </summary> /// <param name="buffer">buffer to read</param> /// <param name="offset">offset to start reading</param> /// <param name="length">buffer length.</param> /// <returns> /// parsed instance of EventStreamMessage. Doesn't return null, /// does throw if CRCs don't match. /// </returns> public static EventStreamMessage FromBuffer(byte[] buffer, int offset, int length) { var currentOffset = offset; //get the total length of the message var totalLength = BitConverter.ToInt32(buffer, currentOffset); //endianness conversion totalLength = EndianConversionUtility.NetworkToHostOrder(totalLength); currentOffset += SizeOfInt32; //get the length of the headers block. var headersLength = BitConverter.ToInt32(buffer, currentOffset); //endianness conversion headersLength = EndianConversionUtility.NetworkToHostOrder(headersLength); currentOffset += SizeOfInt32; //get the prelude crc var preludeCrc = BitConverter.ToInt32(buffer, currentOffset); //endianness conversion preludeCrc = EndianConversionUtility.NetworkToHostOrder(preludeCrc); var message = new EventStreamMessage(); message.Headers = new Dictionary <string, IEventStreamHeader>(StringComparer.Ordinal); using (var nullStream = new NullStream()) using (var runningChecksum = new CrcCalculatorStream(nullStream)) { //write up to the prelude crc to the checksum stream runningChecksum.Write(buffer, offset, currentOffset - offset); //compare the current crc to the prelude crc and make sure they match. if (preludeCrc != runningChecksum.Crc32) { throw new EventStreamChecksumFailureException(string.Format(CultureInfo.InvariantCulture, "Message Prelude Checksum failure. Expected {0} but was {1}", preludeCrc, runningChecksum.Crc32)); } //if the data length passed isn't enough for the total length, that's an error condition. if (totalLength != length) { throw new EventStreamChecksumFailureException( string.Format(CultureInfo.InvariantCulture, "Message Total Length didn't match the passed in length. Expected {0} but was {1}", length, totalLength)); } //now write the prelude crc to the checksum stream runningChecksum.Write(buffer, currentOffset, SizeOfInt32); currentOffset += SizeOfInt32; //prelude length is total message, minus framing and headers size. var payloadLength = totalLength - headersLength - FramingSize; //if we have headers, then loop over each one and parse them out. if (headersLength > 0) { int preOpOffset = currentOffset; while (currentOffset - PreludeLen < headersLength) { EventStreamHeader header = EventStreamHeader.FromBuffer(buffer, currentOffset, ref currentOffset); message.Headers.Add(header.Name, header); } //after parsing the header remember to write that data to the checksum stream runningChecksum.Write(buffer, preOpOffset, currentOffset - preOpOffset); } // now we're on the payload message.Payload = new byte[payloadLength]; Buffer.BlockCopy(buffer, currentOffset, message.Payload, 0, message.Payload.Length); runningChecksum.Write(buffer, currentOffset, message.Payload.Length); currentOffset += message.Payload.Length; //after reading the payload, get the message crc and make sure it matches. var trailingCrc = BitConverter.ToInt32(buffer, currentOffset); //endianness conversion. trailingCrc = EndianConversionUtility.NetworkToHostOrder(trailingCrc); if (trailingCrc != runningChecksum.Crc32) { throw new EventStreamChecksumFailureException( string.Format(CultureInfo.InvariantCulture, "Message Checksum failure. Expected {0} but was {1}", trailingCrc, runningChecksum.Crc32)); } } return(message); }
static void Main(string[] args) { ConfirmQuickSyncReadiness.HaltIfNotReady(); Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; // keep ascending directories until 'media' folder is found for (int i = 0; i < 10 && !Directory.Exists("Media"); i++) { Directory.SetCurrentDirectory(".."); } Directory.SetCurrentDirectory("Media"); int width, height; string inFilename; mfxIMPL impl = mfxIMPL.MFX_IMPL_AUTO; FourCC fourcc = FourCC.NV12; // supported: RGB3 RGB4 BGR4 BGR3 NV12 I420 IYUV YUY2 UYVY YV12 P411 P422 inFilename = "BigBuckBunny_320x180." + fourcc + ".yuv"; width = 320; height = 180; //inFilename = "BigBuckBunny_1920x1080." + fourcc + ".yuv"; width = 1920; height = 1080; string outFilename = Path.ChangeExtension(inFilename, "enc.264"); Console.WriteLine("Working directory: {0}", Environment.CurrentDirectory); Console.WriteLine("Input filename: {0}", inFilename); Console.WriteLine("Input width: {0} Input height: {1}", width, height); Console.WriteLine(); if (!File.Exists(inFilename)) { Console.WriteLine("Input file not found."); Console.WriteLine("Please let Decoder1 run to completion to create input file"); Console.WriteLine("Press any key to exit."); Console.ReadKey(); return; } Stream infs, outfs; BenchmarkTimer bt = null; #if !ENABLE_BENCHMARK infs = File.Open(inFilename, FileMode.Open); outfs = File.Open(outFilename, FileMode.Create); #else // delete this code for most simple example // * Benchmark Mode * // this block does a couple things: // 1. causes the file to be pre-read into memory so we are not timing disk reads. // 2. replaces the output stream with a NullStream so nothing gets written to disk. // 3. Starts the timer for benchmarking // this pre-reads file into memory for benchmarking long maximumMemoryToAllocate = (long)4L * 1024 * 1024 * 1024; Console.WriteLine("Pre-reading input"); infs = new PreReadLargeMemoryStream(File.Open(inFilename, FileMode.Open), maximumMemoryToAllocate); Console.WriteLine("Input read"); outfs = new NullStream(); bt = new BenchmarkTimer(); bt.Start(); int minimumFrames = 4000; #endif Console.WriteLine("Output filename: {0}", Path.GetFileName((outfs as FileStream)?.Name ?? "NO OUTPUT")); Console.WriteLine(); mfxVideoParam mfxEncParams = new mfxVideoParam(); mfxEncParams.mfx.CodecId = CodecId.MFX_CODEC_AVC; mfxEncParams.mfx.TargetUsage = TargetUsage.MFX_TARGETUSAGE_BALANCED; mfxEncParams.mfx.TargetKbps = 2000; mfxEncParams.mfx.RateControlMethod = RateControlMethod.MFX_RATECONTROL_VBR; mfxEncParams.mfx.FrameInfo.FrameRateExtN = 30; mfxEncParams.mfx.FrameInfo.FrameRateExtD = 1; mfxEncParams.mfx.FrameInfo.FourCC = FourCC.NV12; mfxEncParams.mfx.FrameInfo.ChromaFormat = ChromaFormat.MFX_CHROMAFORMAT_YUV420; mfxEncParams.mfx.FrameInfo.PicStruct = PicStruct.MFX_PICSTRUCT_PROGRESSIVE; mfxEncParams.mfx.FrameInfo.CropX = 0; mfxEncParams.mfx.FrameInfo.CropY = 0; mfxEncParams.mfx.FrameInfo.CropW = (ushort)width; mfxEncParams.mfx.FrameInfo.CropH = (ushort)height; // Width must be a multiple of 16 // Height must be a multiple of 16 in case of frame picture and a multiple of 32 in case of field picture mfxEncParams.mfx.FrameInfo.Width = QuickSyncStatic.ALIGN16(width); mfxEncParams.mfx.FrameInfo.Height = QuickSyncStatic.AlignHeightTo32or16(height, mfxEncParams.mfx.FrameInfo.PicStruct); mfxEncParams.IOPattern = IOPattern.MFX_IOPATTERN_IN_SYSTEM_MEMORY; // must be 'in system memory' mfxEncParams.AsyncDepth = 4; // Pipeline depth. Best at 4 BitStreamChunk bsc = new BitStreamChunk(); //where we receive compressed frame data //var encoder = new LowLevelEncoder2(mfxEncParams, impl); ILowLevelEncoder encoder = new LowLevelEncoder(mfxEncParams, impl); string impltext = QuickSyncStatic.ImplementationString(encoder.session); Console.WriteLine("Implementation = {0}", impltext); //string memtext = QuickSyncStatic.ImplementationString(encoder.deviceSetup.memType); //Console.WriteLine("Memory type = {0}", memtext); var formatConverter = new NV12FromXXXXConverter(fourcc, width, height); int inputFrameLength = width * height * VideoUtility.GetBitsPerPixel(fourcc) / 8; byte[] uncompressed = new byte[inputFrameLength]; int count = 0; while (infs.Read(uncompressed, 0, inputFrameLength) == inputFrameLength) { int ix = encoder.GetFreeFrameIndex(); //get index of free surface formatConverter.ConvertToNV12FrameSurface(ref encoder.Frames[ix], uncompressed, 0); encoder.EncodeFrame(ix, ref bsc); if (bsc.bytesAvailable > 0) { outfs.Write(bsc.bitstream, 0, bsc.bytesAvailable); if (++count % 100 == 0) { Console.Write("Frame {0}\r", count); } } #if ENABLE_BENCHMARK // delete this code for most simple example if (infs.Position + inputFrameLength - 1 >= infs.Length) { infs.Position = 0; } if (count >= minimumFrames) { break; } #endif } while (encoder.Flush(ref bsc)) { if (bsc.bytesAvailable > 0) { outfs.Write(bsc.bitstream, 0, bsc.bytesAvailable); if (++count % 100 == 0) { Console.Write("Frame {0}\r", count); } } } if (bt != null) { bt.StopAndReport(count, infs.Position, outfs.Position); } infs.Close(); outfs.Close(); encoder.Dispose(); Console.WriteLine("Encoded {0} frames", count); if (Debugger.IsAttached) { Console.WriteLine("done - press a key to exit"); Console.ReadKey(); } }
protected override IEnumerable<long> MeasureItemsOverride() { var nullStream = new NullStream(); var streamLimiter = new StreamLimiter(nullStream); var serializableChildren = GetSerializableChildren(); return serializableChildren.Select(child => { streamLimiter.RelativePosition = 0; child.Serialize(streamLimiter, null); return streamLimiter.RelativePosition; }); }
/// <summary> /// Converts a message into a byte buffer (usually for network transmission). /// </summary> public byte[] ToByteArray() { int headersWireLength = 0; //first we need to figure out how much space the headers will take up. if (Headers != null) { foreach (var header in Headers) { headersWireLength += header.Value.GetWireSize(); } } var payloadLength = Payload?.Length ?? 0; //total message length is the framing size + the payload size + the headers wire size. var totalLength = headersWireLength + payloadLength + FramingSize; var messageBuffer = new byte[totalLength]; //now write the total length and the headers length to the message. make sure to handle endianness conversions var offset = 0; Buffer.BlockCopy(BitConverter.GetBytes(EndianConversionUtility.HostToNetworkOrder(totalLength)), 0, messageBuffer, offset, SizeOfInt32); offset += SizeOfInt32; Buffer.BlockCopy(BitConverter.GetBytes(EndianConversionUtility.HostToNetworkOrder(headersWireLength)), 0, messageBuffer, offset, SizeOfInt32); offset += SizeOfInt32; using (var nullStream = new NullStream()) using (var runningChecksum = new CrcCalculatorStream(nullStream)) { //write the total length and headers length to the checksum stream. runningChecksum.Write(messageBuffer, 0, offset); //take the current checksum and write it to the message. Buffer.BlockCopy(BitConverter.GetBytes(EndianConversionUtility.HostToNetworkOrder(runningChecksum.Crc32)), 0, messageBuffer, offset, SizeOfInt32); //now take the current checksum and write it to the checksum stream. runningChecksum.Write(messageBuffer, offset, SizeOfInt32); offset += SizeOfInt32; //loop over the headers and write them out to the message. if (Headers != null) { foreach (var header in Headers) { offset = header.Value.WriteToBuffer(messageBuffer, offset); } //make sure to add the header bytes to the checksum stream. runningChecksum.Write(messageBuffer, PreludeLen, offset - PreludeLen); } //write the payload to the message. if (Payload != null) { Buffer.BlockCopy(Payload, 0, messageBuffer, offset, Payload.Length); //update the checksum runningChecksum.Write(messageBuffer, offset, Payload.Length); offset += Payload.Length; } //take the final checksum and add it to the end of the message. Buffer.BlockCopy(BitConverter.GetBytes(EndianConversionUtility.HostToNetworkOrder(runningChecksum.Crc32)), 0, messageBuffer, offset, SizeOfInt32); } return(messageBuffer); }