Introduction
MPEG2 capture devices are different from RAW- TV Tuner capture devices, as we have known for many years.
The RAW Capture devices such as USB Camera, TV Tuner, output YUV uncompressed data that can be viewed easily under Windows Direct Show as follows:
Add Raw capture device to graph and then render its output capture pin:

The YUV Capture device and the MPEG Capture device are from the same family of WDM capture devices, and when enumerating the Direct Show filter list, we get them in the same list.
For example, when known applications like MSN Messenger, Net meeting, Yahoo Messenger, Windows Movie Maker, or third party applications like Sonic MyDVD, Inter Video WINDVDCreator2, search for capture devices available, they will find the MPEG Capture devices and show it in the choice list.
But the question is do they support it. The answer is no, and why?
Because, in order to use MPEG Capture, it is not like the raw capture device, you cannot add it into the graph and select to render the output pin.
The AMCap tries to build graph using render stream but I can mention that it does not work all the time.
In MPEG Capture device, you need to build a different graph if it is manually, or using RenderStream of the graph builder.
It is true that an MPEG capture device need to have its own settings but it also can work with default settings. This document refers to the fact that the MPEG capture device can work with default settings with out any configuration.
The installation of capture devices can setup it to default mode. When we initiate Run on the graph, the capture device will work in default mode.
There are ways to configure the encoder parameters, for example, using IEncoderAPI (see DirectX9).
In this article, I will show how to build a MPEG2 capture graph in two modes:
- DirectX8.1 � referring to the previous Video Renderer.
- DirectX9 � and VMR � using Video Mixing Renderer9 (Windows 2000) and Video Mixing Renderer (VMR- Windows XP).
This is because there are difference in the usage in those video renderers, for example, full screen, Windows repaint, setting the video window.
For example: a MPEG2 capture graph:

Source code brief
In this demo project, we will also do the following:
- How to configure colors (tuning) brightness, hue , chroma-saturation and contrast.
- How to configure the input source - Composite and SVIDEO.
- In the next article, I will describe how to work with Tuner in MPEG WDM Capture device.
- Configuring Microsoft MPEG2- (push mode) demulitiplexer.
- Working with the three video renderer.
- Switching to full mode for the three video renderer.
Using GraphEdit:
Before we dig into the code, I would like to explain to those who are not familiar with the GraphEdit tool (Directx SDK\bin\utils), how to build the MPEG2 graph using GraphEdit.
Open GraphEdit and select from the Video Capture Sources:

As you can see here, the USB Camera is the raw capture device and the BeCapture capture is the MPEG capture device, and they are located in the same place.
Add your MPEG capture device.
Add Microsoft MPEG2- Demultiplexer. (It is located under DirectShow Filters entry in the devices tree.) Right click to bring the property page up front of the MPEG2- Demux.
Please note that if you get an error, then the property page cannot be shown. It means that you don�t have the DirectX SDK installed, and probably you have only DirectX run time (redist.) installed.
When the property page of the MPEG2 Demux is opened, you will get this:

Write Video in the Name and select MPEG2 Program Video. (We will use program stream and not transport stream in this article.)
Then select the Create button:

A video pin has been created.
Do the same for audio pin. Select the right audio stream of your capture device.

Then click to create the audio pin.
The result is that now we have MPEG2 Demux configured to one Audio pin and one Video pin.
- After you create your output pin, you need to configure the stream_id Mappings.
- Creating the stream ID map should be done after you connect the capture device to the MPEG2 demux. So at this stage, close the property page (OK) and connect the two filters.
- Then open again and move to the stream_id Mappings tab.

Stream_id Mappings before we configure it.
How to configure the stream ID mapping
I assume you know the stream ID of your program stream. Those values are part of the encoder settings. The value of stream ID can be set from 0xE0 to 0xEF for Video Stream and 0xC0 to 0xCF to Audio Stream.
Most of the encoders that have one single channel, set the video and the audio stream ID to be 0xE0 and 0xC0, and this is how we will configure it now. Select 0xE0 in the stream ID combo, select Video in the Pin Combo, and Elementary Stream (A/V only) as shown here.

Then press Map to get video stream 0xE0 map as shown here:

Do the same for the audio pin:

The result of our steps take us here:

MPEG2 Capture Device connected to MPEG2 Demultiplexer and configure.
From here, it could be easy, you can render the Audio and the Video pins, or add your favorite audio decoder and video decoder from the Direct Show list.
I will note here that selecting the correct Audio and Video Decoder is important from the point of view of performance.
Not all the video and audio decoders perform the same, so try to choose the best one.
The Example code.
The code builds MPEG2 graph after selecting it from a list.
If only one MPEG2 WDM Capture exists, it selects that one and starts the main application.
The code builds manually the entire graph. I am trying to work with this approach because, eventually, your application will work with one video and audio decoder that you select to work with (render it the easy way).
The code also shows how to work with Video Renderer (of DirectX 8.1), Video Mixing Renderer9 (Directx9 � Windows2000), and VMR (DirectX9 on Windows XP).
- Application main classes
CMPEGHandler
CDirectShowGraph
In addition, we host the video window under a skinable class: CActivewindow.
For manual selection for audio and video decoder, we create an abstract class and choose two video and audio components:
CMSFTAudioDecoder
CeVideoDecoder
In order to select the video renderer to work with, use the global parameter use_vmr9 as follows:
- 0 - DirectX 8.1
- 1 - DirectX 9 Windows 2000 VMR9
- 2 - DirectX 9 Windows XP VMR
The Main Window:

In conjunction to the GraphEdit, the Preview button will build this graph:

Preview graph
The code:
Preview:
The short way � the window will be the direct show window:
MPEG2Handler = new CMPEG2Handler(this);
MPEG2Handler->Preview();
MPEG2Handler->Run();
And when pressing on Record button, we will build this graph.
Record:
The short way � the window will be the direct show window:
MPEG2Handler = new CMPEG2Handler(this);
MPEG2Handler->Record();
MPEG2Handler->Run();
Application menu:
Using the menu, now you can grab picture. This is using the new VMR9 and VMR of DirectX9. It has become pretty common tasks on capture devices. The second issue is the function SetVideoClippingWindow. This function should be set before connecting the video renderer to the video decoder. If not, when running the graph, it will use the default Direct Show video window. The DirectX->DX Window shows this behavior. The same thing for the put_Owner function for DX8.1 Video renderer.
Pause:
Pause should be a threat specially in the capture driver itself. It could be that pause will not do the job you requested on several capture devices because Pause on live source is different from the pause on file that already exists.
Debugging
To debug DirectShow filters graph, you need to register it using function AddGraphToRot that can be found using search in the DirectX SDK samples (also a known task).
After you add this code (don't forget the remove as well), run your application and open GraphEdit tool. Click this button:

And your filters graph that you created will appear in this window:

Select it and your building graph will open. This task can fail (Abnormal termination). Ignore it and repeat the debugging stages until it succeeds.
Feedback and Improvements
Post comments, questions, and stories to the forum below so all can benefit. I will try to keep an eye on the forums. Feel free to send me a copy of the post or reminder, if I haven't checked the forums for a while.
Feel free to submit your patches and improvements to support@becapture.com. If they fit within the scope of the project, I'll integrate them, give you credit for your work, and post the updated project on this page for everyone.
If I will see involvement and good participation and request, I will submit more versions. This is the second article (the first was working with Stream Buffer engine in MPEG2 WDM Capture device here at The Code Project). I would like to hear what you think and also regarding some more classes like converting from MPEG2 �> DIVX, MPEG2 - > MP3, MPEG2 -> WMV (Windows Media encoder).
Eventually, I will combine all my articles together to one big application that will create a complete application with many features.
Visual Basic users:
My suggestion is to combine this article and the Working with SBE.dll article into one DLL and then use it inside Visual Basic. (I have already did it, so if you wish this DLL available for Visual Basic or Java, I will be glad to forward it with documentation).
| You must Sign In to use this message board. |
|
|
 |
|
 |
Hi,
Sorry for bothering you, but I have difficulties to open MPEG2- Demultiplexer property dialog.
I have installed Window SDK and DirectX (oct 2008), but still can't show that property dialog. Any idea what goes wrong?
Many thanks indeed.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
hi I am working on Video Capture Card(H.264). But when I am using Graphedt.exe ,It is not showing that card. i mean it does not detect that card. what should i do? plz help me out.. I purchased 4 channel DVR card.
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
I am trying to add an output pin to the MPEG2 Demultiplexer in an application of my own. Some of the ideas I have got from ur code. However I am getting some error:
hr = CoCreateInstance(CLSID_MPEG2Demultiplexer, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **) &pMPEG2DemuxFilter) ;
hr = pGraph->AddFilter(pMPEG2DemuxFilter, L"MPEG2 Demux");
hr = pMPEG2DemuxFilter->QueryInterface(IID_IMpeg2Demultiplexer, (void **) &pDemuxInterface);
hr = pDemuxInterface->CreateOutputPin(&pMediaType, L"Video", &pDemuxVideoOutPin); // I receive the error here!
The problem I face is, How to define the pDemuxInterface. I have used the following line as you have in your code:
interface IMpeg2Demultiplexer *pDemuxInterface = NULL;
But where is the interface defined as I am getting the following errors:
error C2027: use of undefined type 'IMpeg2Demultiplexer' see declaration of 'IMpeg2Demultiplexer' error C2227: left of '->CreateOutputPin' must point to class/struct/union
I have also included the strmif.h header as well. Is there something else also I must call??
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
I create a source filter by implementing CSource. the problem is i unable to expose a custom interface that i use to send data in & out of the filter; the code is Build successfully but when i use it in GRAPHEDIT it gives me the message "No Such Interface Supported - 0x80004002". and same error in the code as well.
i expose an interface in a Sink filter that i made previously, it works well but i unable to do the same thing in Source filter. can you help me how this can be done in Source filters (based on PushSource). Is this possible in Source filters if yes then tell me if there is any new technique is to be used or tell me the procedure or an alternative if you can ----------- ITS VERY URGENT
any thing else you want to ask just mail me on but here is some thing that i can provide you at this time.
my whole filter contains two (2) classes and an exposing interface and here are thier declarations
////////// CinetSource.h
class CinetSource : public CSource, public IVSource // my exposing interface { public: //DECLARE_IUNKNOWN; static CUnknown * WINAPI CreateInstance(LPUNKNOWN lpunk, HRESULT *phr);
STDMETHODIMP RecieveData(PBYTE *pdata); // the method of IVSource
private: CinetSourceStrm *m_pPin; CinetSource(LPUNKNOWN lpunk, HRESULT *phr); ~CinetSource();
STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void ** ppv);
HRESULT STDMETHODCALLTYPE GetClassID(CLSID __RPC_FAR *pClassID); };
/////////// CinetSourceStrm.h
class CinetSource;
class CinetSourceStrm : public CSourceStream { public: CinetSourceStrm(HRESULT *phr, CSource *pParent); ~CinetSourceStrm();
// plots data into the supplied video frame HRESULT FillBuffer(IMediaSample *pms);
// Ask for buffers of the size appropriate to the agreed media type HRESULT DecideBufferSize(IMemAllocator *pIMemAlloc, ALLOCATOR_PROPERTIES *pProperties);
// Set the agreed media type HRESULT SetMediaType(const CMediaType *pMediaType);
// Because we calculate the ball there is no reason why we // can't calculate it in any one of a set of formats... HRESULT CheckMediaType(const CMediaType *pMediaType); HRESULT GetMediaType(int iPosition, CMediaType *pmt);
// Resets the stream time to zero HRESULT OnThreadCreate(void);
// Quality control notifications sent to us STDMETHODIMP Notify(IBaseFilter * pSender, Quality q); };
/////////// IVSource.h
MIDL_INTERFACE("93AFFF9F-D34B-4da3-96E5-97819974C45B")
IVSource : public IPersist // i also try the same thing using "IUnknown" but the error is same { public: virtual STDMETHODIMP RecieveData(PBYTE *pdata) = 0; };
///////////////////////////////////////////////////////////////////////
Help me in this regard or direct me to any concerning person.
thanks
-- Azfar Khan
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
Is this project applicable for mpeg4 capture device? Because now i'm doing a project which needs to record the video stream from the network camera. My network camera is AXIS 211 which streams mjpeg and mpeg4. ~ThAnK YoU~
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
Its me kifayatullah i m working in a software house at Islamabad Pakistan i have problem that i have develop a project to capture video from a capture card. by using standard technique of SampleCB method. now i have problem that i want to use VMR in this and want to put a water mark on my video stream. please help me in this regard. Kifayatullah kifaayet@yahoo.com Islamabad Pakistan
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Hello ,
To build Winamp Plug In you can learn here at TheCodeProject.com. What we did is the following:
Using the MPEG2 Preview and capture source code and the Working with SBE.Dll (Time Shift) Sources i build a Winamp Digital Video Recorder Plug In The result is the same the the look is Winamp. Now you can not only play Video files under Winamp but also Record and watch TV , prepare your video tapes into DVD and burn it with your favorite DVD authoring software.
To Download the DVR PlugIn go to "http://www.becapture.com/downloads/Winamp DVR Plugin.exe"
Password is BeCaptureWinamp2004.
In the future i will show more application that those classes can fit into.
Thanks Support@becapture.com
Enjoy from beCapture.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Hi
The Password is BeCaptureWinamp2004
If the winamp DVR PlugIn does not work on your capture device , please let me know at support@becapture.com and i will do my best to support it.
The Winamp PlugIn uses those classes and the new revision that can be found at http://www.becapture.com/English/BeCaptureSDk.htm
Section 3 and 4 are new version for the two articles that i published here. The new version should support EHOME based capture device.
Thanks Support@becapture.com
Enjoy from beCapture.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Yeah I'm using a black gold TV card, hoping it would be possible to use through winamp? Ive got the plugin, but I tells me there is no capture device? Can you help?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hello
I updated the source code to support EHOME Windows Media Center Device drivers as well. I hope this code will work now with any MPEG2 Capture device. If not, please let me know and if it possible, please send me graph edit screen shoot of your device.
You can download it from here http://www.becapture.com/downloads/SDk/TheCodeProjects/Articles/BeCaptureMPEG2/Part2/mpeg2CaptureApp2.zip
The source try to figure what kind of mpeg device you have , if it has only one video capture source or two: video capture source and wdm encoder streaming device. The code uses now PreviewEx to bulid those graph.
In addition you can download a WINAMP DVR PlugIn that it is based on those classes. The Winamp DVR PlugIn does the same thing that this demo does, But using this plug in you can watch Video inside Winamp. (Password is BeCaptureWinamp2004)
Please let me know for any thought, comment, you might have.
Thanks Support@BeCapture.com
Enjoy from beCapture.com
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
Supplemental from Support@becapture.com
Hello There are MPEG2 Capture card that this code will not work with. This is because the new capture device graph builds differently. Those capture device are from the family of Microsoft Windows Media Center Edition and this is a new subject (for driver developer is much different). I will post a new article regarding those capture device but mean while here is a 'short' explanation. 1) If your capture device under “Video Capture Source” does not have OUTPUT MPEG2 pin (major type Stream and Sub Type Program) so it must have Separate Video and Audio pin. In that case try to find the Actual MPEG2 Capture device under “WDM Streaming Encoder Devices” . If it there try to add it to the graph and act like it was in the Video Capture source and build the graph with this filter as described. 2) The other option is to build the entire graph. In short , add the device under Video Capture Sources to the graph and also the device under “WDM Streaming Encoder Devices” . (they need to be with similar name) Connect between them And than build the entire graph as describe (MPEG2 Demux …..) Again , I will post a new article with a new example that will try to work with most of the MPEG2 capture devices
Thanks And Enjoy from BeCapture.com
Enjoy from beCapture.com
|
| Sign In·View Thread·PermaLink | 3.80/5 |
|
|
|
 |
|
 |
Could you provide us with a link to that article (if it is already finished)?
I have a Hauppauge PVR 150 and I have a lot of problems trying to configure the mpeg2 multiplexer in code.
|
| Sign In·View Thread·PermaLink | 2.25/5 |
|
|
|
 |
|
|