Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Preview and Record with MPEG2 Capture device

0.00/5 (No votes)
11 Jun 2004 1  
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.

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:

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:

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:

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

License

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