Esempio n. 1
0
        private unsafe void EncodeImagesToH264(object state)
        {
            try
            {
                while (isEncodingEvent.WaitOne())
                {
                    if (decodedFrameQueue.TryDequeue(out queueFrame))
                    {
                        var sourcePixelFormat      = AVPixelFormat.AV_PIX_FMT_BGR24;
                        var destinationPixelFormat = AVPixelFormat.AV_PIX_FMT_YUV420P; //for h.264

                        using (var vfc = new VideoFrameConverter(videoInfo.SourceFrameSize, sourcePixelFormat, videoInfo.DestinationFrameSize, destinationPixelFormat))
                        {
                            var convertedFrame = vfc.Convert(queueFrame);
                            convertedFrame.pts = frameNumber * 2;       //to do
                            h264Encoder.TryEncodeNextPacket(convertedFrame);
                        }

                        frameNumber++;
                    }
                }
            }
            catch (ObjectDisposedException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (AccessViolationException e)
            {
                Console.WriteLine(e.Message);
            }
        }
Esempio n. 2
0
        private void DecodeAllFramesToImages(object state)
        {
            try
            {
                using (var decoder = new VideoStreamDecoder(url, videoInputType))
                {
                    videoInfo = decoder.GetVideoInfo();

                    var info = decoder.GetContextInfo();
                    info.ToList().ForEach(x => Console.WriteLine($"{x.Key} = {x.Value}"));

                    var sourceSize             = decoder.FrameSize;
                    var sourcePixelFormat      = hwDeviceType == AVHWDeviceType.AV_HWDEVICE_TYPE_NONE ? decoder.PixelFormat : GetHWPixelFormat(hwDeviceType);
                    var destinationSize        = sourceSize;
                    var destinationPixelFormat = AVPixelFormat.AV_PIX_FMT_BGR24;

                    using (var vfc = new VideoFrameConverter(sourceSize, sourcePixelFormat, destinationSize, destinationPixelFormat))
                    {
                        while (decoder.TryDecodeNextFrame(out var frame) && isDecodingEvent.WaitOne())
                        {
                            var convertedFrame = vfc.Convert(frame);

                            Bitmap bitmap = new Bitmap(convertedFrame.width, convertedFrame.height, convertedFrame.linesize[0], System.Drawing.Imaging.PixelFormat.Format24bppRgb, (IntPtr)convertedFrame.data[0]);

                            if (isEncodingThreadRunning)
                            {
                                decodedFrameQueue.Enqueue(convertedFrame);
                            }

                            BitmapToImageSource(bitmap);
                        }
                    }
                }
            }
            catch (ApplicationException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (ObjectDisposedException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (AccessViolationException e)
            {
                Console.WriteLine(e.Message);
            }
        }