private async Task DecodeImage(IBoundedQueue <T> queue) { MemoryStream data; while ((data = await dataToParse.DequeueOrDefault()) != null) { var bmp = new Bitmap(data); var width = bmp.Width; var height = bmp.Height; var pixelFormat = bmp.PixelFormat; var bmp1Data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pixelFormat); var length = bmp1Data.Stride * bmp1Data.Height; byte[] bmp1Bytes = new byte[length]; Marshal.Copy(bmp1Data.Scan0, bmp1Bytes, 0, length); await queue.Enqueue(factory.FromBytes(width, height, bmp1Bytes)); bmp.UnlockBits(bmp1Data); bmp.Dispose(); data.Dispose(); logger.NotifyFillstate(dataToParse.Count, "ParseBuffer"); } }
private async Task ParseInputStream(IBoundedQueue <MutableByteImage> queue, IBoundedQueue <byte[]> chunksQueue, int width, int height, ChunkedSimpleMemoryStream memoryStream) { int count = 0; while (true) { try { var item = await chunksQueue.DequeueOrDefault(); if (item == default) { break; } _logger.NotifyFillstate(++count, "ParsedImages"); _logger.NotifyFillstate(chunksQueue.Count, "ChunkedQueue"); await queue.Enqueue(_factory.FromBytes(width, height, item)); } catch (Exception e) { _logger.LogException(e); } } if (memoryStream.HasUnwrittenData) { _logger.WriteLine("Unwritten data exists after finishing parsings." + " This indicates a severe issue with frame splitting", Verbosity.Warning); } }
public async override Task Produce(IBoundedQueue <T> queue) { foreach (var filename in filenames) { try { logger.NotifyFillstate(queue.Count, this.GetType().Name); var bmp1 = new Bitmap(filename); var height = bmp1.Height; var width = bmp1.Width; var pixelFormat = bmp1.PixelFormat; var bmp1Data = bmp1.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pixelFormat); var length = bmp1Data.Stride * bmp1Data.Height; byte[] bmp1Bytes = new byte[length]; Marshal.Copy(bmp1Data.Scan0, bmp1Bytes, 0, length); var image = factory.FromBytes(width, height, bmp1Bytes); await queue.Enqueue(image); bmp1.UnlockBits(bmp1Data); bmp1.Dispose(); } catch (Exception e) { Console.WriteLine(e); } } queue.CompleteAdding(); }
public async Task Produce(IBoundedQueue <MutableByteImage> sourceQueue) { try { if (!string.IsNullOrWhiteSpace(_arguments.PathToFfmpeg)) { FFMpegOptions.Configure(new FFMpegOptions { RootDirectory = _arguments.PathToFfmpeg }); } var result = await FFProbe.AnalyseAsync(_arguments.InputFile).ConfigureAwait(false); var width = result.PrimaryVideoStream.Width; var height = result.PrimaryVideoStream.Height; var bpp = Image.GetPixelFormatSize(System.Drawing.Imaging.PixelFormat.Format24bppRgb) / 8; var pixelsPerFrame = width * height; var frameSizeInBytes = pixelsPerFrame * bpp; _logger.WriteLine("Input from ffmpeg currently only supports rgb24-convertable input", Verbosity.Warning); var chunksQueue = BoundedQueueFactory.Get <byte[]>(4, "In-ChuQ"); using var memoryStream = new ChunkedSimpleMemoryStream(frameSizeInBytes, chunksQueue); // new MemoryStream(frameSizeInBytes); StreamPipeSink sink = new StreamPipeSink(memoryStream); var args = FFMpegArguments .FromFileInput(_arguments.InputFile).OutputToPipe(sink, options => options.DisableChannel(FFMpegCore.Enums.Channel.Audio) .UsingMultithreading(true) .ForceFormat("rawvideo") .WithCustomArgument(_arguments.CustomArgs ?? string.Empty) .ForcePixelFormat("bgr24")) .NotifyOnProgress( percent => _logger.NotifyFillstate(Convert.ToInt32(percent), "InputVideoParsing"), TimeSpan.FromSeconds(1)); var produceTask = args.ProcessAsynchronously(true).ContinueWith((_) => { chunksQueue.CompleteAdding(); sourceQueue.CompleteAdding(); }); var consumeTask = ParseInputStream(sourceQueue, chunksQueue, width, height, memoryStream) .ContinueWith((_) => _logger.WriteLine("finished reading", Verbosity.Info)); await Task.WhenAll(produceTask, consumeTask); _logger.WriteLine("finished reading", Verbosity.Info); } catch (System.ComponentModel.Win32Exception) { _logger.WriteLine("Couldn't find ffmpeg", Verbosity.Error); } catch (Exception e) { _logger.LogException(e); } }
public async override Task Produce(IBoundedQueue <T> queue) { for (int i = 0; i < count; i++) { logger.NotifyFillstate(i, "TestData"); var data = new byte[data1.Length]; ((i % 2 == 0) ? data1 : data2).CopyTo(data, 0); await queue.Enqueue(factory.FromBytes(width, height, data, format)); } queue.CompleteAdding(); }
public override async Task Produce(IBoundedQueue <T> queue) { var readingTask = Task.Run(() => ReadFromDisk()); var decodingTasks = Task.WhenAll(Enumerable.Range(0, 6).Select(x => Task.Run(() => DecodeImage(queue)))).ContinueWith(t => queue.CompleteAdding()); await Task.WhenAll(decodingTasks, readingTask); if (decodingTasks.IsFaulted || readingTask.IsFaulted) { throw decodingTasks.Exception ?? readingTask.Exception ?? new Exception("unkown stuff happened"); } }
private void BurstProducerManyConsumersTestImpl(IBoundedQueue <int> queue) { const int CONCLUDED_TIMEOUT = 10000; int capacity = queue.Capacity; var concluded = new ManualResetEvent(false); bool[] values = new bool[capacity]; ThreadStart producer = () => { Random random = new Random(); for (int v = 0; v < capacity; ++v) { queue.Put(v); } }; ThreadStart consumer = () => { int val = queue.Get(); if (CheckRange(0, capacity, val)) { values[val] = true; if (allTrue(values)) { concluded.Set(); } } }; for (int i = 0; i < capacity; ++i) { new Thread(consumer).Start(); } new Thread(producer).Start(); bool result = concluded.WaitOne(CONCLUDED_TIMEOUT); concluded.Close(); Assert.AreEqual(true, result); }
public async override Task Produce(IBoundedQueue <T> queue) { var bytesToRead = Width * Height * Image.GetPixelFormatSize(Format); while (this.InputStream.CanRead) { try { var bm = new Bitmap( Width, Height, Width, Format, Marshal.UnsafeAddrOfPinnedArrayElement(this.InputStream.ReadBytes(bytesToRead), 0)); await queue.Enqueue(factory.FromImage(bm)); } catch (Exception e) { logger.LogException(e); } } queue.CompleteAdding(); }
public ChunkedSimpleMemoryStream(int bytesPerChunk, IBoundedQueue <byte[]> chunks) { this.BytesperChunk = bytesPerChunk; this.CurrentChunk = new byte[bytesPerChunk]; _chunks = chunks; }
public abstract Task Produce(IBoundedQueue <T> queue);