TheRain
User

Fresh Boarder
Posts: 3
graphgraph
 
Click here to see the profile of this user
Can you help me with AvDn C# and mpeg encoding? - 2007/08/17 16:29 Hey Daniel,

I'm trying to encode 24-bit RBG frames, frame by frame into MPEG, but I'm getting an memory access violation and can't figure out why. Can you take a look at my code and see if you can find the reason? The RGB data being passed to the AvFrame here is valid 24-bit RGB data. I've checked it and even checked that valid 24-bit RGB data is being filled in to the AvFrame structure by accessing the memory at avframe.GetData0();

It is the last line, the EncodeVideo line that is causing the exception. "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

Almost forgot to mention. It's opening an MPEG-1 file to set the codec... which I'm sure is not the best way, but it seemed like it should work.

Here is my code

Code:

 AvDn.AvDnFrame avFrame = new AvDnFrame();         AvDnCodec avCodec;         AvDnCodecContext avCodecContext;         IntPtr output Marshal.AllocHGlobal(200000); AvDn.AvDnUtils.RegisterAll();             AvDnFormatContext avfmnt = new AvDnFormatContext("sample.mpg");             avfmnt.FindStreamInfo();             avCodecContext avfmnt.GetCodecContextForStream(0);              avCodecContext.BitRate 400000;             avCodecContext.PixFmt Av.PIX_FMT_RGB24;             avCodecContext.Width 320;             avCodecContext.Height 240;             avCodecContext.SetTimeBase(125);             avCodecContext.GopSize 15;             avCodecContext.MbDecision 2;             avCodecContext.OpenEncoder(); avFrame.FillPicStruct(RAWRGB.Scan0Av.PIX_FMT_RGB24320240); int size avCodecContext.EncodeVideo(output200000avFrame); // Here's where it breaks



Any advice you can give would be greatly appreciated.
  | | No public write access. Please register.
Daniel
Admin

Admin
Posts: 44
graph
 
Click here to see the profile of this user
Re:Can you help me with AvDn C# and mpeg encoding? - 2007/08/18 00:50 Hi,

The exception message is very misleading, it can be any data structures not initialized correctly for example.

You should create the output format with code similar to (taken from csoutputexample.cs)

outputFormat = AvDnUtils.GetOutputFormat("mpeg");

formatCtx = new AvDnFormatContext();

formatCtx.SetOutputFormat(outputFormat);
formatCtx.SetFilename(filename);

If this is not done properly, you'll get the exception you received.

-daniel
  | | No public write access. Please register.
TheRain
User

Fresh Boarder
Posts: 3
graphgraph
 
Click here to see the profile of this user
Re:Can you help me with AvDn C# and mpeg encoding? - 2007/08/18 07:24 Thanks so much for answering me. I'm trying to take single buffered RGB frames and output single buffered MPEG frames for transmission over a network. The frames would be grabbed by command from the network client and so the server would encode an RGB frame to MPEG upon receiving that command.

It seems like AvDn might not be set up with that in mind... seems more file oriented perhaps.

Thanks for your advice though, it's good to know that it might not be my output buffer or my AvDnFrame that are set up wrong.
  | | No public write access. Please register.
TheRain
User

Fresh Boarder
Posts: 3
graphgraph
 
Click here to see the profile of this user
Re:Can you help me with AvDn C# and mpeg encoding? - 2007/08/19 19:53 I figured out what the issue is. ffmpeg doesn't find a codec unless you specify YUV420P. The only way I could find to encode RGB data is to convert it to YUV420P... here's a method I wrote to take an IntPtr for an RGB frame and convert it to YUV420P:

Code:

         void RGB_to_YUV(IntPtr RGBFrameref IntPtr YUVFrameint widthint height)         {             YUVFrame Marshal.AllocHGlobal(width height 12 8);  // Allocate whole RGB Frame;             byte[] YUVBytes = new byte[width height 12 8];             int u=width*height;             int v=u+width*height/4;             byte[] RGBBytes = new byte[width height 3];             Marshal.Copy(RGBFrame,RGBBytes,0,width height 3);             int rgbPixels 0;             int chromaWidth width 2;             int chromaHeight height 2;             int yMask 1;             int xMask 1;             int yPtr 0;             int uPtr =u;             int vPtr =v;             byte Rc;             byte Gc;             byte Bc;             for (int y 0heighty++)             {                 for (int x 0widthx++)                 {                     Bc RGBBytes[rgbPixels++];                     Gc RGBBytes[rgbPixels++];                     Rc RGBBytes[rgbPixels++];                     YUVBytes[yPtr++] =(byte) ((0.257f Rc) + (0.504f Gc) + (0.098f Bc) + 16);                     if ((yMask) == && (xMask) == 0)                     {                         YUVBytes[uPtr++] = (byte)(-(0.148f Rc) - (0.291f Gc) + (0.439f Bc) + 128);                         YUVBytes[vPtr++] = (byte)((0.439f Rc) - (0.368f Gc) - (0.071f Bc) + 128);                     }                 }             }             Marshal.Copy(YUVBytes0YUVFrameYUVBytes.Length);         }

  | | No public write access. Please register.
Joomla Template by Joomlashack
Joomla Templates by JoomlaShack Joomla Templates by Compass Design