예제 #1
0
 public void WriteBuffer(SByteBuffer outBuffer)
 {
     outBuffer.WriteInt(this.uid);
     outBuffer.WriteString(this.npc_id);
     outBuffer.WriteByte((byte)this.entity_type);
     outBuffer.WriteInt(this.level);
     outBuffer.WriteInt(this.star);
     outBuffer.WriteInt(this.quality);
     outBuffer.WriteFloat(this.hp);
     outBuffer.WriteFloat(this.mp);
     outBuffer.WriteInt(this.ai_type);
     outBuffer.WriteInt(this.pos);
 }
예제 #2
0
 public void ReadFromBuffer(SByteBuffer outBuffer)
 {
     this.uid         = outBuffer.ReadInt();
     this.npc_id      = outBuffer.ReadString();
     this.entity_type = (EntityType)outBuffer.ReadByte();
     this.level       = outBuffer.ReadInt();
     this.star        = outBuffer.ReadInt();
     this.quality     = outBuffer.ReadInt();
     this.hp          = outBuffer.ReadFloat();
     this.mp          = outBuffer.ReadFloat();
     this.ai_type     = outBuffer.ReadInt();
     this.pos         = outBuffer.ReadInt();
 }
예제 #3
0
        public static void Main(string[] args)
        {
            WrapperUtils.RegisterLibrariesPathSimple("ffmpeg-x64", "ffmpeg-x86");
            AvFormat.RegisterAll();
            AvCodec.RegisterAll();
            AvFormat.NetworkInit();

            AvFormatContext format = AvFormatContext.Allocate();

            if (!format.OpenInput(@"http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"))
            {
                throw new Exception("Failed to open file :(");
            }
            if (!format.FindStreamInfo())
            {
                throw new Exception("Failed to find stream info :(");
            }

            AvStream pStream = null;

            foreach (AvStream avStream in format.Streams)
            {
                if (avStream.Codec.Type == AvMediaType.Video)
                {
                    pStream = avStream;
                    break;
                }
            }

            if (pStream == null)
            {
                throw new Exception("Could not find video stream :(");
            }

            AvCodecContext codecContext = pStream.Codec;

            int           width        = codecContext.Width;
            int           height       = codecContext.Height;
            AvPixelFormat sourceFormat = codecContext.PixelFormat;
            AvPixelFormat targetFormat = AvPixelFormat.Bgr24;

            SwsContext convertContext = SwsContext.Get(width, height, sourceFormat, width, height, targetFormat,
                                                       SwsFlags.FastBilinear);

            if (convertContext == null)
            {
                throw new Exception("Could not initialize the conversion context");
            }

            AvFrame     convertedFrame           = AvFrame.Allocate();
            int         convertedFrameBufferSize = AvPicture.GetSize(targetFormat, width, height);
            SByteBuffer convertedFrameBuffer     = AvUtil.Malloc((ulong)convertedFrameBufferSize);

            ((AvPicture)convertedFrame).Fill(convertedFrameBuffer, targetFormat, width, height);

            AvCodec codec = AvCodec.FindDecoder(codecContext.Id);

            if (codec == null)
            {
                throw new Exception("Unsupported codec");
            }

            if (codec.HasCapability(CodecCapabilities.Truncated))
            {
                codecContext.Flags |= CodecFlags.Truncated;
            }

            if (!codecContext.Open2(codec))
            {
                throw new Exception("Could not open codec");
            }

            AvFrame frame = AvFrame.Allocate();

            AvPacket packet = AvPacket.Create();

            packet.Init();

            int frameNumber = 0;

            while (frameNumber < 500)
            {
                if (!format.ReadFrame(packet))
                {
                    throw new Exception("Could not read frame!");
                }

                if (packet.StreamIndex != pStream.Index)
                {
                    continue;
                }

                int gotPicture;
                int size = codecContext.DecodeVideo2(frame, out gotPicture, packet);
                if (size < 0)
                {
                    throw new Exception("Error while decoding frame " + frameNumber);
                }

                if (gotPicture == 1)
                {
                    Console.WriteLine($"Frame: {frameNumber}");

                    SByteBufferArray src       = frame.Data;
                    SByteBufferArray dst       = convertedFrame.Data;
                    IntArray         srcStride = frame.LineSize;
                    IntArray         dstStride = convertedFrame.LineSize;
                    convertContext.Scale(src, srcStride, 0, height, dst, dstStride);

                    int linesize = dstStride[0];
                    using (
                        Bitmap bitmap = new Bitmap(width, height, linesize,
                                                   PixelFormat.Format24bppRgb, convertedFrame.Data0))
                    {
                        bitmap.Save(@"frame.buffer." + frameNumber + ".jpg", ImageFormat.Jpeg);
                    }
                    frameNumber++;
                }
            }

            convertedFrame.Free();
            convertedFrameBuffer.Free();
            convertContext.Free();
            frame.Free();
            codecContext.Close();
            format.CloseInput();
        }