Beispiel #1
0
 public void TestOpen()
 {
     Assert.IsNull(_codecContext.Codec);
     _codecContext.Open2(AVCodec.FindDecoder(_codecContext.CodecId));
     Assert.IsNotNull(_codecContext.Codec);
     Assert.AreEqual("h264", _codecContext.Codec.Name);
 }
Beispiel #2
0
        public void TestEncode()
        {
            AVLog.Callback += (level, msg) => Console.WriteLine(level + ": " + msg);
            AVLog.Level     = (int)AVLogLevel.Trace;

            _codecContext.Open2(AVCodec.FindDecoder(_codecContext.CodecId));
            AVPacket packet;
            var      frame = new AVFrame();

            var codec         = AVCodec.FindEncoderByName("png");
            var encodeContext = new AVCodecContext(codec)
            {
                PixelFormat = AVPixelFormat.RGB24,
                Width       = _codecContext.Width,
                Height      = _codecContext.Height
            };

            encodeContext.Open2(codec);

            var convContext = SWSContext.GetContext(_codecContext.Width, _codecContext.Height, AVPixelFormat.YUV420P,
                                                    AVPixelFormat.RGB24);
            var convBuffer = new AVPicture(AVPixelFormat.RGB24, _codecContext.Width, _codecContext.Height).AsFrame();

            int frameCounter = 0;
            int readCounter  = 0;

            while ((packet = _formatContext.ReadFrame()) != null)
            {
                if (packet.StreamIndex != 0)
                {
                    continue;
                }
                bool pic = _codecContext.DecodeVideo2(packet, frame);
                if (!pic)
                {
                    continue;
                }

                readCounter++;
                convContext.Scale(frame, convBuffer, 0, _codecContext.Height);

                var outPacket = encodeContext.EncodeVideo2(convBuffer);
                if (outPacket != null)
                {
                    frameCounter++;

                    // for checking the frames, not part of normal test
                    //var data = new byte[outPacket.Size];
                    //Marshal.Copy(outPacket.Data, data, 0, data.Length);
                    //File.WriteAllBytes((frameCounter++) + ".png", data);
                }
            }

            Assert.AreEqual(readCounter, frameCounter);
            Assert.AreEqual(245, readCounter);

            encodeContext.Close();
            _codecContext.Close();
        }