TheRain
User
 Fresh Boarder
| Posts: 3 |   |
|
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 RGBFrame, ref IntPtr YUVFrame, int width, int 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 = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Bc = RGBBytes[rgbPixels++];
Gc = RGBBytes[rgbPixels++];
Rc = RGBBytes[rgbPixels++];
YUVBytes[yPtr++] =(byte) ((0.257f * Rc) + (0.504f * Gc) + (0.098f * Bc) + 16);
if ((y & yMask) == 0 && (x & 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(YUVBytes, 0, YUVFrame, YUVBytes.Length);
}
|
|