Exemplo n.º 1
0
        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();
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
 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();
 }
Exemplo n.º 4
0
        private async Task ReadFromDisk()
        {
            int i = 0;

            foreach (var filename in filenames)
            {
                try
                {
                    logger.NotifyFillstate(dataToParse.Count, "ReadBuffer");
                    logger.NotifyFillstate(i, "FilesRead");
                    await dataToParse.Enqueue(new MemoryStream(File.ReadAllBytes(filename), false));

                    i++;
                }
                catch (Exception e) { Console.WriteLine(e); }
            }
            dataToParse.CompleteAdding();
        }
Exemplo n.º 5
0
        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();
        }
Exemplo n.º 6
0
        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");
            }
        }