Displaying raw yuv data in a Windows form PDF Print E-mail
Written by Daniel P   
Thursday, 09 November 2006

I've mentioned that in my code for decoding i-frames from an mpeg stream, I used a buffer for the decoded yuv data that I passed to the constructor of a Bitmap object. If you look at the possible constructors for the Bitmap object, none can take raw yuv data. You have to perform a colorspace tranform but then if you save the Bitmap object as jpeg, the save method has to perform the reverse operation. Since I just wanted to look at the result, I tried to created a Direct3D surface with a fourcc code as a pixel format but my hardware wouldn't allow it (an integrate vidcard on a laptop). So I went back to the antiques store and used a clipper object for a DirectDraw surface to do the job. Here's the code for the InitializeGraphics methods which is passed a pointer to the raw yuv data as argument.


// we need this to create surfaces
SurfaceDescription description = new SurfaceDescription();

// our DirectDraw device
device = new Device();

// set the cooperative level to normal (ie not fullscreen)
device.SetCooperativeLevel(this, CooperativeLevelFlags.Normal);

// set the display mode width and height, and 32 bit color depth.
// you might want to change this to your monitor settings
device.SetDisplayMode(800, 600, 32, 0, false);

// create a primary surface
description.SurfaceCaps.PrimarySurface = true;

// create the primary surface
primary = new Surface(description, device);

// set a clipper object around our form
Clipper clipper = new Clipper(device);
clipper.Window = this;
primary.Clipper = clipper;

// clear out the SurfaceDescription structure.
description.Clear();

// our similated backBuffer will cover the client area
description.Width = this.ClientRectangle.Width;
description.Height = this.ClientRectangle.Height;

// create a fourcc code for NV12 and set the surf descr
int NV12 = MakeFourCC('N', 'V', '1', '2' );
PixelFormat pf = new PixelFormat();
pf.FourCC = NV12;
pf.FourCcIsValid = true;
description.PixelFormatStructure = pf;

// we want an offscreen surface in vid mem
SurfaceCaps caps = new SurfaceCaps();
caps.OffScreenPlain = true;
caps.VideoMemory = true;
description.SurfaceCaps = caps;

// Create the backbuffer
backBuffer = new Surface(description, device);

// scan0 points to our yuv data
byte * scanPtr = (byte *)scan0;
// we have 12 bits per pixel in NV12 with
// one plane for the y's and another plane for pack u's and v's
int lenght = width*height*12/8;
// lock the buffer and copy the data in (might want to optimize this)
GraphicsStream gs = backBuffer.Lock(LockFlags.WriteOnly).Data;
for( int i = 0; i < lenght; i++ )
gs.Write(*scanPtr++);
backBuffer.Unlock();

Then I just blit to the primary surface in the OnPaint method. I used the fourcc code NV12 because my card supports it and MS recommends it.



Bookmark it...
Digg!Reddit!Del.icio.us!Google!Facebook!Slashdot!Technorati!StumbleUpon!Newsvine!Furl!Yahoo!Ma.gnolia!
 
Fun with bits and bytes PDF Print E-mail
Written by Daniel P   
Thursday, 19 October 2006

I have been doing some low-level video processing recently. I thought that since the I-frames of an mpeg movie are very similar to a jpeg image, it would be simple to extract these. While using a SampleGrabber in DirectShow or, I suppose, some switches with ffmpeg, it's probably not too difficult, I wanted a more "basic" approach.

So I started to study the Mpeg-1 standard carefully and write some code in C#. After awhile, my code parses the headers (pack, system, and packet) of a system stream, finds its way to an I-frame, decodes the DCT coefficients, feeds it to the IDCT, and stuffs the result in a buffer that I pass to a constructor of Bitmap object that I save.

I'm not a big fan (that's an understatement) of software patents and since the Mpeg standards are replete with these, I was happy with an abstract view like a DirectShow filter, for example. But using FFMeg has forced me to pay more attention to low-level stuff.

It seems that most Mpeg codecs are based on the "reference codec", I didnt' really look at it. I just read the standard and wrote code to do the task. For example, the code for the IDCT is a direct translation of the mathematical formula, hence the result is not very efficient. But it was certainly a good learning exercise.

I'll probably write some materials about this exercise in the near future but before I'd like to finish a more user-friendly version of my wrapper for FFMpeg from .Net.



Bookmark it...
Digg!Reddit!Del.icio.us!Google!Facebook!Slashdot!Technorati!StumbleUpon!Newsvine!Furl!Yahoo!Ma.gnolia!
 
<< Start < Prev 1 2 3 4 5 6 7 8 9 10 Next > End >>

Results 25 - 28 of 81
Joomla Template by Joomlashack
Joomla Templates by JoomlaShack Joomla Templates by Compass Design