Click here to Skip to main content
15,884,177 members
Articles / Desktop Programming / MFC

Preview and Record with MPEG2 Capture device

Rate me:
Please Sign up or sign in to vote.
4.39/5 (16 votes)
11 Jun 20048 min read 219.8K   9.7K   94   15
This article shows how to work with MPEG2 capture devices such as Preview, Record. Works with all video renderers. Grab picture and configure MPEG2 Demux.


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:

Raw capture device

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:

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:

graph edit capture device list

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:

Sample screenshot

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:

Sample screenshot

A video pin has been created.

Do the same for audio pin. Select the right audio stream of your capture device.

Sample screenshot

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.

Sample screenshot

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.

Sample screenshot

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

Sample screenshot

Do the same for the audio pin:

Sample screenshot

The result of our steps take us here:

Sample screenshot

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:

Sample screenshot

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

Sample screenshot

Preview graph

The code:


The short way – the window will be the direct show window:

MPEG2Handler = new CMPEG2Handler(this);

And when pressing on Record button, we will build this graph.


The short way – the window will be the direct show window:

MPEG2Handler = new CMPEG2Handler(this);

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 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.


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:

Sample screenshot

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

Sample screenshot

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 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).


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Web Developer
Israel Israel
Enjoy from

Comments and Discussions

GeneralMPEG2- Demultiplexer property dialog Pin
Member 464502516-Nov-09 15:01
Member 464502516-Nov-09 15:01 
Generalvideo capture card Pin
Swapnil Shah25-Apr-08 18:47
Swapnil Shah25-Apr-08 18:47 
QuestionIMpeg2Demultiplexer Problem! Pin
vikionline5-Aug-06 19:19
vikionline5-Aug-06 19:19 
QuestionProblem in Exposing a custom interface in Source Filter Pin
blacktauras11112-Feb-06 8:02
blacktauras11112-Feb-06 8:02 
Questionpreview and record with mpeg4? Pin
SewH5-Jan-06 17:01
SewH5-Jan-06 17:01 
GeneralVideo Overlyind and water mark Pin
kifayatullah9-Feb-05 19:59
kifayatullah9-Feb-05 19:59 
GeneralRe: Video Overlyind and water mark Pin
kifayatullah9-Feb-05 20:00
kifayatullah9-Feb-05 20:00 
GeneralRe: Video Overlyind and water mark Pin
tanvon malik25-Sep-07 2:40
tanvon malik25-Sep-07 2:40 
GeneralWinamp DVR PlugIn - Download Pin
BeCapture6-Jul-04 23:04
BeCapture6-Jul-04 23:04 
GeneralRe: Winamp DVR PlugIn - Download Pin
Member 97283945-Aug-04 11:15
Member 97283945-Aug-04 11:15 
GeneralRe: Winamp DVR PlugIn - Download Pin
BeCapture5-Aug-04 20:01
BeCapture5-Aug-04 20:01 
GeneralRe: Winamp DVR PlugIn - Download Pin
Anonymous22-May-05 7:07
Anonymous22-May-05 7:07 
GeneralUpdated source code Pin
BeCapture6-Jul-04 22:47
BeCapture6-Jul-04 22:47 
GeneralSupplemental from Pin
BeCapture16-Jun-04 8:29
BeCapture16-Jun-04 8:29 
GeneralRe: Supplemental from Pin
Rob van der Veer9-Nov-05 11:12
Rob van der Veer9-Nov-05 11:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.