Search code examples
c#directshow

Playback with Color Converter DMO distorted skipping


I'm working with another directshow graph and can't seem to wrap my head around the issue I'm seeing. When I snap the graph together in Graphedit it goes:

filesource -> demux -> decoder -> color converter dmo -> external render

This works perfect and playback is amazing!

But in code (and the graph builds perfectly without errors)... it doesn't do what I want... instead it plays the file however the playback is very pixelated with blackness and it looks like it skips ontop of the previous frame never removing the prior frame.

Heres my code:

private static void BuildGraph(IGraphBuilder pGraph, string source)
{
    int hr = 0;

    // Graph builder.
    pBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
    hr = pBuilder.SetFiltergraph(pGraph);
    CheckHR(hr, "Can't SetFiltergraph.");

    // Add File Source (Async.).
    pFileSourceAsync = (IBaseFilter)new AsyncReader();
    hr = pGraph.AddFilter(pFileSourceAsync, "File Source (Async.)");
    CheckHR(hr, "Can't add File Source (Async.) to graph.");

    // Set source filename.
    pFileSourceAsync_src = pFileSourceAsync as IFileSourceFilter;

    if (pFileSourceAsync_src == null)
    {
        CheckHR(unchecked((int)0x80004002), "Can't get IFileSourceFilter.");
    }

    hr = pFileSourceAsync_src.Load(source, null);
    CheckHR(hr, "Can't load file.");

    // Add MPEG4 Demultiplexor.
    pMPEG4Demultiplexor = (IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_MPEG4Demultiplexor));
    hr = pGraph.AddFilter(pMPEG4Demultiplexor, "MPEG4 Demultiplexor");
    CheckHR(hr, "Can't add MPEG4 Demultiplexor to graph.");

    // Add Microsoft DTV-DVD Video Decoder.
    pMicrosoftDTVDVDVideoDecoder = (IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_MicrosoftDTVDVDVideoDecoder));
    hr = pGraph.AddFilter(pMicrosoftDTVDVDVideoDecoder, "Microsoft DTV-DVD Video Decoder");
    CheckHR(hr, "Can't add Microsoft DTV-DVD Video Decoder to graph.");

    // Add Color Converter DMO.
    dmoFilter = (IBaseFilter)new DMOWrapperFilter();
    dmoWrapper = (IDMOWrapperFilter)dmoFilter;
    hr = dmoWrapper.Init(CLSID_ColorConverterDMO, DMOCategory.VideoEffect);
    CheckHR(hr, "Can't initialize Color Converter DMO.");
    hr = pGraph.AddFilter(dmoFilter, "DMO Filter");
    CheckHR(hr, "Can't add Color Converter DMO to graph.");


    //IMediaParams dmoParams = dmoFilter as IMediaParams;
    //MPData srcLeft = new MPData();
    //MPData srcTop = new MPData();
    //MPData destLeft = new MPData();
    //MPData destTop = new MPData();
    //MPData width = new MPData();
    //MPData height = new MPData();
    //srcLeft.vFloat = 0;
    //srcTop.vFloat = 0;
    //destLeft.vFloat = 0;
    //destTop.vFloat = 0;
    //width.vFloat = 1280;
    //height.vFloat = 720;
    //dmoParams.SetParam(0, srcLeft);
    //dmoParams.SetParam(1, srcTop);
    //dmoParams.SetParam(2, destLeft);
    //dmoParams.SetParam(3, destTop);
    //dmoParams.SetParam(4, width);
    //dmoParams.SetParam(5, height);


    // Add Smart Tee.
    pSmartTee = (IBaseFilter)new SmartTee();
    hr = pGraph.AddFilter(pSmartTee, "Smart Tee");
    CheckHR(hr, "Can't add Smart Tee to graph.");

    // Add Decklink Video Render.
    pDecklinkVideoRender = CreateFilterByName(@"Decklink Video Render", CLSID_ExternalRenderers);
    hr = pGraph.AddFilter(pDecklinkVideoRender, "Decklink Video Render");
    CheckHR(hr, "Can't add Decklink Video Render to graph.");

    // Add AVI Decompressor.
    pAVIDecompressor = (IBaseFilter)new AVIDec();
    hr = pGraph.AddFilter(pAVIDecompressor, "AVI Decompressor");
    CheckHR(hr, "Can't add AVI Decompressor to graph.");

    // Add Color Space Converter.
    pColorSpaceConverter = (IBaseFilter)new Colour();
    hr = pGraph.AddFilter(pColorSpaceConverter, "Color Space Converter");
    CheckHR(hr, "Can't add Color Space Converter to graph.");

    // Add Null Renderer.
    pNullRenderer = (IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_NullRenderer));
    hr = pGraph.AddFilter(pNullRenderer, "Null Renderer");
    CheckHR(hr, "Can't add Null Renderer to graph.");

    // Connect File Source (Async.) and MPEG4 Demultiplexor.
    hr = pGraph.ConnectDirect(GetPin(pFileSourceAsync, "Output"), GetPin(pMPEG4Demultiplexor, "Input"), null);
    CheckHR(hr, "Can't connect File Source (Async.) and MPEG4 Demultiplexor.");

    // Connect MPEG4 Demultiplexor and Microsoft DTV-DVD Video Decoder.
    hr = pGraph.ConnectDirect(GetPin(pMPEG4Demultiplexor, "Video 1"), GetPin(pMicrosoftDTVDVDVideoDecoder, "Video Input"), null);
    CheckHR(hr, "Can't connect MPEG4 Demultiplexor and Microsoft DTV-DVD Video Decoder.");

    // Connect Microsoft DTV-DVD Video Decoder and Color Converter DMO.
    hr = pGraph.ConnectDirect(GetPin(pMicrosoftDTVDVDVideoDecoder, "Video Output 1"), GetPin(dmoFilter, "in0"), null);
    CheckHR(hr, "Can't connect Microsoft DTV-DVD Video Decoder and Color Converter DMO.");

    // Connect Color Converter DMO and Smart Tee Filter.
    hr = pGraph.ConnectDirect(GetPin(dmoFilter, "out0"), GetPin(pSmartTee, "Input"), null);
    CheckHR(hr, "Can't connect Color Converter DMO and Smart Tee Filter.");

    // Connect Smart Tee Filter and Decklink Video Render.
    hr = pGraph.ConnectDirect(GetPin(pSmartTee, "Capture"), GetPin(pDecklinkVideoRender, "In"), null);
    CheckHR(hr, "Can't connect Smart Tee Filter and Decklink Video Render.");

    // Add SampleGrabber.
    pSampleGrabber = (IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_SampleGrabber));
    hr = pGraph.AddFilter(pSampleGrabber, "SampleGrabber");
    CheckHR(hr, "Can't add SampleGrabber to graph.");
    (pSampleGrabber as ISampleGrabber).SetBufferSamples(true);
    (pSampleGrabber as ISampleGrabber).SetOneShot(false);

    // Connect Smart Tee and SampleGrabber.
    hr = pGraph.ConnectDirect(GetPin(pSmartTee, "Preview"), GetPin(pSampleGrabber, "Input"), null);
    CheckHR(hr, "Can't connect Smart Tee and SampleGrabber.");

    // Connect SampleGrabber and AVIDecompressor.
    hr = pGraph.ConnectDirect(GetPin(pSampleGrabber, "Output"), GetPin(pAVIDecompressor, "XForm In"), null);
    CheckHR(hr, "Can't connect SampleGrabber and AVIDecompressor.");

    // Connect AVIDecompressor and Color Space Converter.
    hr = pGraph.ConnectDirect(GetPin(pAVIDecompressor, "XForm Out"), GetPin(pColorSpaceConverter, "Input"), null);
    CheckHR(hr, "Can't connect Color Space Converter and SampleGrabber.");

    // Connect Color Space Converter and Null Renderer.
    hr = pGraph.ConnectDirect(GetPin(pColorSpaceConverter, "XForm Out"), GetPin(pNullRenderer, "In"), null);
    CheckHR(hr, "Can't connect Color Space Converter and Null Renderer.");

    // Set SampleGrabber Media Type.
    AMMediaType pSampleGrabber_pmt = new AMMediaType();
    pSampleGrabber_pmt.majorType = MediaType.Video;
    pSampleGrabber_pmt.subType = MediaSubType.RGB32;
    pSampleGrabber_pmt.formatType = FormatType.VideoInfo;
    VideoInfoHeader pSampleGrabber_format = new VideoInfoHeader();
    pSampleGrabber_pmt.formatPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(pSampleGrabber_format));
    Marshal.StructureToPtr(pSampleGrabber_format, pSampleGrabber_pmt.formatPtr, false);
    hr = ((ISampleGrabber)pSampleGrabber).SetMediaType(pSampleGrabber_pmt);
    DsUtils.FreeAMMediaType(pSampleGrabber_pmt);
    CheckHR(hr, "Can't set media type to sample grabber.");
}

You'll see that I have more to the code such as hooking up a smart tee and a sample grabber (this isn't the cause as I've commented it out before to check the result and no change, eventually i want to grab still images from the video stream... so I have it in place when ready).

I think the issue is with the color converter dmo but I'm not entirely sure... the exact graph in graphedit runs without any issues however code mode gives me horrible playback. Maybe theres something I'm forgetting to do with the color converter dmo? Or the wrapper? Any ideas would be greatly appreciated.

Thanks, Cheers.


Solution

  • I don't have details about Color Converter DMO, but from experience with other Vista+ DMOs - they are not intended for use within DirectShow. Yes, they are still DMOs, with IMediaObject interface implemented, DMO Wrapper Filter is also there but small issues are ruining the integration.

    If it does not work out of the box, most likely you will have to put the DMO inside your own wrapper filter. This will definitely work out, and – good news – it is not that complicated.

    Older DMOs were definitely tested for use within DirectShow with stock wrapper filter, this is why they have been working and this is why DMO Wrapper Filter is available in first place.