Skip to content

AccessViolationException on DecodePacket #10

@GoogleCodeExporter

Description

@GoogleCodeExporter
I'm trying to use this to display videos on XNA 4.0 and I'm getting an 
AccessViolationException when calling DecodePacket. I'm using a 
VideoScalingStream just like the winforms example (I also tried with 
VideoDecoderStream with the same result).

Here are the relevant snippets of my code.
It throws the exception in the very first call of Update.
[...]
        Texture2D VideoTexture;
        VideoScalingStream VideoStream;
        Color[] NextFrameRawData;
        bool VideoPlaying;
[...]
        void InitVideo()
        {
            LogManager.Log("SongManager.InitVideo - Attempting to" +
            "initialize video stream.");

            try
            {
                MediaFile file = new MediaFile(Data.VideoFile);

                LogManager.Log("SongManager.InitVideo - Iterating through " +
                    file.Streams.Count + " streams.");
                foreach (DecoderStream stream in file.Streams)
                {
                    if (stream != null && 
                        stream.GetType() == typeof(VideoDecoderStream))
                    {
                        LogManager.Log("SongManager.InitVideo - " +
                        "Found a suitable stream. Creating video stream.");

                        VideoDecoderStream DecoderStream = stream as VideoDecoderStream;

                        // Adjust the video rectangle to fit screen without
                        // affecting aspect ratio
                        VideoRect = new Rectangle(0, 0, DecoderStream.Width,
                            DecoderStream.Height);
                        MetricsHelper.ResizeAndCenter(ScreenRect, ref VideoRect);

                        VideoStream = new VideoScalingStream(
                            DecoderStream, VideoRect.Width, 
                            VideoRect.Height, PixelFormat.PIX_FMT_RGB32);

                        NextFrameRawData = new Color[VideoStream.FrameSize / 3];
                        VideoTexture = new Texture2D(Globals.Screen.GraphicsDevice, 
                            VideoStream.Width, VideoStream.Height, false, 
                            SurfaceFormat.Color);

                        LogManager.Log("SongManager.InitVideo - " +
                        "Stream successfully created.");
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                LogManager.Log("SongManager.InitVideo - Caught an exception:\r\n" 
                    + e.ToString());
                VideoStream = null;
            }
        }
[...]
        public string VideoFile
        {
            get { return Data.VideoFile; }
            set
            {
                Data.VideoFile = value;
                InitVideo();
            }
        }
[...]
        public void Update(GameTime gameTime)
        {
            if (Playing)
            {
                if (!VideoPlaying && VideoStream != null &&
                    MediaPlayer.PlayPosition >= Data.VideoOffset)
                    VideoPlaying = true;

                if (VideoPlaying)
                {
                    byte[] nextFrame;
                    VideoPlaying = VideoStream.ReadFrame(out nextFrame);

                    if (VideoPlaying)
                    {
                        int j = 0;

                        for (int i = 0, length = nextFrame.Length; i < length; i += 3)
                            NextFrameRawData[j++] = new Color(nextFrame[i], nextFrame[i + 1], nextFrame[i + 2]);

                        VideoTexture.SetData<Color>(NextFrameRawData, 0, NextFrameRawData.Length);
                    }
                }
[...]
        public void Draw(SpriteBatch dst)
        {
            // Hide background when video starts playing
            if (BackgroundData != null && !VideoPlaying)
                dst.Draw(BackgroundData, BackgroundRect, Color.White);

            // Draw current frame of the video
            if (VideoStream != null && VideoPlaying)
                dst.Draw(VideoTexture, VideoRect, Color.White);


Here are the details of the exception:
System.AccessViolationException was unhandled
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=FFmpegSharp
  StackTrace:
       at FFmpegSharp.Interop.FFmpeg.avcodec_decode_video(AVCodecContext& pAVCodecContext, AVFrame* pAVFrame, Boolean& got_picture_ptr, Byte* buf, Int32 buf_size)
       at FFmpegSharp.VideoDecoderStream.DecodePacket(AVPacket& packet) in C:\Users\Francesco\Documents\Visual Studio 2010\Projects\ffmpeg-sharp\src\VideoDecoderStream.cs:line 130
       at FFmpegSharp.DecoderStream.ReadNextPacket() in C:\Users\Francesco\Documents\Visual Studio 2010\Projects\ffmpeg-sharp\src\DecoderStream.cs:line 233
       at FFmpegSharp.DecoderStream.Read(Byte[] buffer, Int32 offset, Int32 count) in C:\Users\Francesco\Documents\Visual Studio 2010\Projects\ffmpeg-sharp\src\DecoderStream.cs:line 179
       at FFmpegSharp.VideoDecoderStream.ReadFrame(Byte[]& frame) in C:\Users\Francesco\Documents\Visual Studio 2010\Projects\ffmpeg-sharp\src\VideoDecoderStream.cs:line 152
       at FFmpegSharp.VideoScalingStream.ReadFrame(Byte[]& frame) in C:\Users\Francesco\Documents\Visual Studio 2010\Projects\ffmpeg-sharp\src\VideoScalingStream.cs:line 86
       at DDR_Clone.SongManager.Update(GameTime gameTime) in C:\Users\Francesco\Documents\Visual Studio 2010\Projects\DDR_Clone\DDR_Clone\DDR_Clone\SongManager.cs:line 426
       at DDR_Clone.Game1.Update(GameTime gameTime) in C:\Users\Francesco\Documents\Visual Studio 2010\Projects\DDR_Clone\DDR_Clone\DDR_Clone\Game1.cs:line 113
       at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
       at Microsoft.Xna.Framework.Game.Run()
       at DDR_Clone.Program.Main(String[] args) in C:\Users\Francesco\Documents\Visual Studio 2010\Projects\DDR_Clone\DDR_Clone\DDR_Clone\Program.cs:line 15
  InnerException: 

I'm using the exact version of ffmpeg you provided in Issue #9. I'm compiling 
this on Visual Studio Ultimate 2010 with XNA 4.0. I compiled ffmpeg-sharp 
myself after converting the project to Visual Studio 2010.
This is all built for x86.

Original issue reported on code.google.com by francesc...@gmail.com on 20 Feb 2012 at 4:07

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions