问题
In order to render a DirectShow graph in my WPF application, I use a sample grabber to write a bitmap to memory, and read it elsewhere in code. This may seem as a wierd solution, but seems to be the only way to get a WPF brush out of it.
The following code gives me the information I need about the bitmap:
AMMediaType mt = grabber.GetConnectedMediaType();
VideoInfoHeader header = (VideoInfoHeader)Marshal.PtrToStructure(mt.formatPtr, typeof VideoInfoHeader);
header.BmiHeader // ...
Now, header.BmiHeader
is of type BitmapInfoHeader and provides information such as the width and height of the bitmap. I need this information to write the bitmap to memory.
However, this code does not seem to work always. For example, when the sample grabber filter is connected to a MPEG-2 Program Stream pin, header
will be null
. Why does my code not work with certain media types, and what other ways are there to get it's BitmapInfoHeader structure?
Please note that even though I am using the directshow.net library, answers in C++ are also welcome.
Edit: This is how my graph looks like:
*source* -> MPEG2-Demultiplexer -> SampleGrabber -> MS DTV-DVD Video Decoder -> Video Renderer
I'm not allowed to place the grabber between the decoder and renderer. About the demux's video pin:
Major Type: Video
Sub Type: MPEG2_VIDEO
Format: MPEG2Video
After I have connected the sample grabber, it's input pin also has the above media type. When I open this graph file in my application and use grabber.GetConnectedMediaType();
, I also get the same media type. However, the formatPtr
or (pbFormat
in C++) of this media type is 0.
回答1:
If you want a bitmap, you need to place the sample grabber somewhere where you receive uncompressed video. When the samplegrabber is behind the MPEG-2 Program Stream pin, you will receive a mpeg2 program stream. Of course it it possible to get a bitmap out of that, but then you need to demultiplex and decompress manually. Both are task which you want to perform in the directshow filter.
But even if you receive uncompressed video, you might still get different formats. depending on the decoder you can receive RGB24, YUY2, YV12, ... Check the MediaSubType for what you exactly receive. If you always want to receive RGB24, you can use the RGBFilters/TransNull24 filter which is included in the sdk. See also this answer.
Edit: One additional note, if you not only want the resolution, but also the bitmap itself, you have to put the samplegrabber between the decoder and renderer. If the MS Decoder is not working, try ffdshow.
来源:https://stackoverflow.com/questions/14141499/get-bitmap-info-from-mpeg-2-program-transport-stream