sumodhkrishna / ffmpeg-sharp

ffmpeg-sharp
1 stars 0 forks source link

AccessViolationException on DecodePacket #10

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
Compiling against .NET 4.0 Client Profile I receive this error as wel. The 
error dissapears however when I compile against .NET 2.0. 

Original comment by l.ordelm...@gmail.com on 23 Feb 2012 at 10:48

GoogleCodeExporter commented 9 years ago
Thank you very much, I'll try to port it to .net 4.0 as i need this on XNA 4.0 
:)

Original comment by francesc...@gmail.com on 26 Feb 2012 at 11:31

GoogleCodeExporter commented 9 years ago
Same problem here traying to use FFmpegSharp on a .NET 4 project:

"Attempted to read or write protected memory. This is often an indication that 
other memory is corrupt."

On 'int byteCount = FFmpeg.avcodec_decode_video(ref m_avCodecCtx, m_avFrame, 
out frameFinished, (byte*)packet.data, packet.size);'

Regards.

Original comment by DiegoRos...@gmail.com on 28 Feb 2012 at 12:18

GoogleCodeExporter commented 9 years ago
I have the same problem with also a .Net 4.0 project. But i cannot go back to 
2.0.
The error happens occasionally:
"Attempted to read or write protected memory. This is often an indication that 
other memory is corrupt."

has anyone a solution or workaround for this ?

Original comment by funkyfac...@gmail.com on 4 Apr 2012 at 9:11

GoogleCodeExporter commented 9 years ago
Hey

Did anyone every solve this?

I've been trying for days now! At first I thought it was due to the Calling 
Convention changes in P/Invoke from .Net3.5 to .Net4.0 but I've tried 
everything around this.

Annoyingly the project FFmpeg.AutoGen does work in .Net4.0 projects but when 
converting this project over I get other issues!

Cheers

Dave

Original comment by darkviso...@gmail.com on 15 Aug 2013 at 2:42