强制Mpeg2Demultiplexer使用ffdshow渲染H264数字电视video

我花了很多时间试图让DTVViewer的DirectShow样本工作不幸,但没有成功。 DVBT网络的video格式是H264,我发现IFilterGraph的IntelliConnect行为更喜欢使用Mpeg2video格式。

对于那些想要查看代码的人来说,就是这样。 如果你对DirectShow一无所知我分享了我对这段代码的经验。 最可能的问题在本教程的第5步和第6步中描述。

  • 连接filter的辅助函数代码:

     public static void UnsafeConnectFilters(IFilterGraph2 graph, IBaseFilter source, IBaseFilter dest, Func sourceMediaPredicate=null, Func destMediaPredicate=null) { foreach(IPin spin in IteratePinsByDirection(source, PinDirection.Output)) { if(IsConnected(spin)) continue; int fetched; AMMediaType[] sourceTypes=GetMajorType(spin, out fetched); if(fetched>0) { Guid sourceType=sourceTypes[0].majorType; try { if(sourceMediaPredicate!=null&&!sourceMediaPredicate(sourceTypes[0])) continue; foreach(IPin pin in IteratePinsByDirection(dest, PinDirection.Input)) { if(IsConnected(pin)) continue; var types=GetMajorType(pin, out fetched); try { if(fetched>0) { Guid destType=types[0].majorType; if(destMediaPredicate!=null&&!destMediaPredicate(types[0])) continue; if(sourceType==destType) { spin.Connect(pin, types[0]); return; } } else { spin.Connect(pin, sourceTypes[0]); return; } } finally { } } } finally { } } } } 

有谁知道:

  1. 我应该如何将h264引脚连接到ffdshow?
  2. 我应该如何推荐使用h264video解码图?

  • 教程和细节

    1. 创建图表

       _graph = (IFilterGraph2)new FilterGraph(); 
    2. 我们正在使用DVBT网络

       IBaseFilter networkProvider = (IBaseFilter) new DVBTNetworkProvider(); 

      …必须调到602000KHz @ 8MHz ONID = 1 TSID = 1 SID = 6

       ITuner tuner = (ITuner) networkProvider; IDVBTuningSpace tuningspace = (IDVBTuningSpace) new DVBTuningSpace(); tuningspace.put_UniqueName("DVBT TuningSpace"); tuningspace.put_FriendlyName("DVBT TuningSpace"); tuningspace.put__NetworkType(typeof (DVBTNetworkProvider).GUID); tuningspace.put_SystemType(DVBSystemType.Terrestrial); ITuneRequest request; tuningspace.CreateTuneRequest(out request); ILocator locator = (ILocator) new DVBTLocator(); locator.put_CarrierFrequency(602000); ((IDVBTLocator) locator).put_Bandwidth(8); request.put_Locator(locator); IDVBTuneRequest dvbrequest = (IDVBTuneRequest) request; dvbrequest.put_TSID(1); dvbrequest.put_ONID(1); dvbrequest.put_SID(6); _graph.AddFilter(networkProvider, "Network Provider"); 
    3. 创建一个mpeg2 demux,从单个电视流中获取单独的EPG / Vidoe /音频/文本流

       _mpeg2Demultiplexer = (IBaseFilter) new MPEG2Demultiplexer(); _graph.AddFilter(_mpeg2Demultiplexer, "MPEG-2 Demultiplexer"); 

      现在我们搜索BDA源filter的本地filter,在我的例子中是IT9135 BDA Fitler

       DsDevice[] devicesOfCat = DsDevice.GetDevicesOfCat(FilterCategory.BDASourceFiltersCategory); IBaseFilter iteDeviceFilter; _graph.AddSourceFilterForMoniker( devicesOfCat[0].Mon, null, devicesOfCat[0].Name, out iteDeviceFilter); 
    4. 现在连接filter: [DVBT Net. Provider]->[BDA Src Filter]->[MPEG2Demux]-> ... [DVBT Net. Provider]->[BDA Src Filter]->[MPEG2Demux]-> ...

       UnsafeConnectFilters(_graph, networkProvider, iteDeviceFilter); UnsafeConnectFilters(_graph, iteDeviceFilter, _mpeg2Demultiplexer); 

      必须将两个filter连接到demux,以提供epg(节目指南数据)。 抱歉,我不知道他们具体是什么:P 。 它们位于BDATransportInformationRenderersCategory类别下。 我们尝试按名称找到它们并将它们连接到demux

       DsDevice[] dsDevices = DsDevice.GetDevicesOfCat(FilterCategory.BDATransportInformationRenderersCategory); foreach (DsDevice dsDevice in dsDevices) { IBaseFilter filter; _graph.AddSourceFilterForMoniker( dsDevice.Mon, null, dsDevice.Name, out filter); if(dsDevice.Name == "BDA MPEG2 Transport Information Filter") _bdaTIF = filter; else if(dsDevice.Name == "MPEG-2 Sections and Tables") { _mpeg2SectionsAndTables = filter; } UnsafeConnectFilters(_graph, _mpeg2Demultiplexer, filter); } 

      现在,demux连接到MPEG-2 Sections and Tables以及BDA MPEG2 Transport Information Filter

    5. 现在创建h264video类型,并将输出引脚添加到此类型的demux

       AMMediaType h264 = new AMMediaType(); h264.formatType = FormatType.VideoInfo2; h264.subType = MediaSubType.H264; h264.majorType = MediaType.Video; IPin h264pin; ((IMpeg2Demultiplexer) _mpeg2Demultiplexer).CreateOutputPin(h264, "h264", out h264pin); 

      下面,我尝试搜索能够处理H264video的ffdshowvideo解码器,它位于DirectShow Filters类别下(如GraphStudio )。

       DsDevice[] directshowfilters = DsDevice.GetDevicesOfCat(FilterCategory.LegacyAmFilterCategory); IBaseFilter ffdshow = null; foreach (DsDevice directshowfilter in directshowfilters) { if(directshowfilter.Name == "ffdshow Video Decoder") { _graph.AddSourceFilterForMoniker( directshowfilter.Mon, null, directshowfilter.Name, out ffdshow); break; } } 
    6. 为video输出创建video渲染器

       _videoRenderer = new VideoRendererDefault(); _graph.AddFilter((IBaseFilter)_videoRenderer, "Video Renderer"); 

      ……和音频……

       DsDevice defaultDirectSound = DsDevice.GetDevicesOfCat(FilterCategory.AudioRendererCategory)[0]; _graph.AddSourceFilterForMoniker( defaultDirectSound.Mon, null, defaultDirectSound.Name, out _audioRender); 

      在这里,我尝试将demux的h264输出引脚连接到ffdshow。 此方法调用失败,出现AccessViolationException。 我不知道如何将这两者连接在一起:(

      注释这一行将导致图形开始运行,尽管图中有一个断开的ffdshowVideoDecoderfilter,但不会显示任何内容。 IntelliConnect将Mpeg2video输出连接到本地可用的video解码器,正如我所说,它不会显示任何内容。

       // UnsafeConnectFilters(_graph, _mpeg2Demultiplexer, ffdshow, type => type.majorType == MediaType.Video && type.subType == MediaSubType.H264); 
    7. ConnectFilters是从directshowlib的DTVViewer样本借来的

       ConnectFilters(); 

      我在这里调动了实际调音

       tuner.put_TuningSpace(tuningspace); tuner.put_TuneRequest(request); 
    8. 启动图表并希望显示某些声音或video

       int hr = (_graph as IMediaControl).Run(); DsError.ThrowExceptionForHR(hr); 
    9. 检查图表是否正在运行…

       FilterState pfs; hr = (_graph as IMediaControl).GetState(1000, out pfs); DsError.ThrowExceptionForHR(hr); 

      它说图表正在运行。

您是否检查过您的ffdshow是否已启用H264 / AVC? 打开filter属性,在“编解码器”部分,应启用H264 / AVC格式(您也可以禁用Mpeg2解码器,以确保它不喜欢这种格式)。

另外,你可以尝试使用另一个Mpeg2解复用器。 默认的“MPEG-2解复用器”在不同的环境中表现不同。 还有许多其他filter可以解复用TS,如果你可以投入一些钱,我建议使用MainConcept或Elecard。