注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

widebright的个人空间

// 编程和生活

 
 
 

日志

 
 

directshow学习系列之:ACM Wrapper Filter  

2008-10-08 14:44:20|  分类: 程序设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |


Audio Compression Manager 应该是系统定义的一种编码规范,在directshow里面可以方便的通过ACM Wrapper Filter来使用里面的编码器来进行音频处理的。不过系统也提供了acmStreamOpen ,acmStreamConvert 等api函数给你直接调用,那样你就不用directshow也可以的。因为我是用来聊天时实时处理音频数据的,在用GraphEdit的是否发现这下面有个mp3的filter可以用来编码音频,所以也就使用到了这个东东。这个mp3 编码器应该是Windows系统自带的,不过功能有限制,输入44100Hz的PCM音频的时候,他最高只能输出22050Hz的mp3数据。这个是个限制,听说是为了推广他自己的wma格式,可能也是应为mp3格式是受专利保护,版权等原因吧,只有微软才知道怎么回事了。反正这个filter就是有这个限制,开源的lame mp3 编码器的filter好像可以输出44100Hz的,我没有试过,有需要的可以看看。在directshow中可以通过IAMStreamConfig 接口来设置 filter的输出音频属性的,同样你也可以这个接口来设置acm mp3 编码器的输出,不过不是很多可以选择就是22050hz 和11025hz几个。 借助acmFormatChoose 这个api函数,我们可以方便的获取到mp3格式的WAVEFORMATEX结构定义。

下面是写举例代码:大多从MSDN代码修改而来

     MMRESULT            mmr;
     ACMFORMATCHOOSE     afc;
     MPEGLAYER3WAVEFORMAT wfxEnum; //WAVEFORMATEX        wfxEnum;

     // Initialize the ACMFORMATCHOOSE members.
     memset(&afc, 0, sizeof(afc));

     afc.cbStruct    = sizeof(afc);
     afc.fdwStyle    = 0L;               // no special style flags
     afc.hwndOwner   = hDlg;             // hwnd of parent window
     afc.pwfx        =(LPWAVEFORMATEX) &wfxSelection;    // wfx to receive selection
     afc.cbwfx       = sizeof(wfxSelection);
     afc.pszTitle    = TEXT("输出MP3音频属性选择");

     // Request that all 16-bit PCM formats be displayed for the user
     // to select from.
     memset(&wfxEnum, 0, sizeof(wfxEnum));
     wfxEnum.wfx.wFormatTag = WAVE_FORMAT_MPEGLAYER3;// wfxEnum.wFormatTag =WAVE_FORMAT_PCM;
                wfxEnum.wfx.nSamplesPerSec=22050 ;
                //好像只是22050 Hz 的他才能正常工作,太奇怪了,所以这里只允许设置22050 Hz的
     afc.fdwEnum = ACM_FORMATENUMF_WFORMATTAG | ACM_FORMATENUMF_NSAMPLESPERSEC;//| ACM_FORMATENUMF_WBITSPERSAMPLE;
     afc.pwfxEnum =(LPWAVEFORMATEX) &wfxEnum;
     mmr = acmFormatChoose(&afc);
     if ((MMSYSERR_NOERROR != mmr) && (ACMERR_CANCELED != mmr))
     {
      // There was a fatal error in bringing up the list
      // dialog box (probably invalid input parameters).
      //初始化mp3属性结构为 "56bBit/s ,22050 Hz , Stereo 6KB/秒-----------------
      wfxSelection.wfx.wFormatTag =85 ;
      wfxSelection.wfx.nChannels =2 ;
      wfxSelection.wfx.nSamplesPerSec= 22050 ; //24000
      wfxSelection.wfx.nAvgBytesPerSec =7000 ;
      wfxSelection.wfx.nBlockAlign= 1 ;
      wfxSelection.wfx.wBitsPerSample= 0 ;
      wfxSelection.wfx.cbSize =12 ;
      wfxSelection.wID =1 ;
      wfxSelection.fdwFlags = 2 ;
      wfxSelection.nBlockSize = 182 ;// 168
      wfxSelection.nFramesPerBlock=1 ;
      wfxSelection.nCodecDelay = 1393 ;

      //---------------------------------------------------
     }


    }

弹出的对话框,让你选择音频格式的 样子大概是这样的。

directshow学习系列之:ACM Wrapper Filter - widebright - widebright的个人空间


    // Now set the actual format of the audio data
    hr = pPin->QueryInterface(IID_IAMStreamConfig, (void **)&pCfg);
    if (FAILED(hr))
    {
     pPin->Release();
     break;
    }           

    // Read current media type/format
    AM_MEDIA_TYPE *pmt={0};
    hr = pCfg->GetFormat(&pmt);
           //   FORMAT_WaveFormatEx
    if (SUCCEEDED(hr))
    {
     // Fill in values for the new format
     WAVEFORMATEX *pWF = (WAVEFORMATEX *) pmt->pbFormat;
     memcpy((void *)pmt->pbFormat, (void *)&wfxSelection,sizeof (wfxSelection));

     // Set the new formattype for the output pin
     hr = pCfg->SetFormat(pmt);
    
     UtilDeleteMediaType(pmt);
    }
    // Release interfaces
    pCfg->Release();

    pPin->Release();

    result =S_OK;

acmFormatChoose 说明http://msdn.microsoft.com/en-us/library/ms705084(VS.85).aspx

ACM Wrapper Filter
Microsoft DirectShow 9.0

ACM Wrapper Filter

The ACM Wrapper filter enables Audio Compression Manager (ACM) codecs to join a filter graph. It can act either as a decompression filter or as a compression filter.

As a decompression filter, the ACM Wrapper appears in the "DirectShow Filters" category (CLSID_LegacyAmFilterCategory) and has a merit of MERIT_NORMAL. The connection media type on the input pin determines which codec the filter uses. Typically, the application does not need to add the filter to the filter graph; it is pulled in automatically by the Filter Graph Manager when needed. Decompression is only to PCM audio.

As a compression filter, the ACM Wrapper appears in the "Audio Compressors" category (CLSID_AudioCompressorCategory) and has a merit of MERIT_DO_NOT_USE. Each codec appears as a separate instance. For compression, you cannot directly create the filter with CoCreateInstance. Instead, you must use the system device enumerator. For more information, see Using the System Device Enumerator.

Filter interfaces IBaseFilter, IPersist, IPersistPropertyBag
Input pin media types MEDIATYPE_Audio, MEDIASUBTYPE_NULL, FORMAT_WaveFormatEx
Input pin interfaces IMemInputPin, IPin, IQualityControl
Output pin media types MEDIATYPE_Audio, MEDIASUBTYPE_PCM, FORMAT_WaveFormatEx.

Any combination of the following are possible:

  • Samples per second (kHz): 44.1, 22.05, 11.025, or 8.0.
  • Channels: Stereo or mono.
  • Bits per sample: 8 or 16.
Output pin interfaces IAMStreamConfig, IMediaPosition, IMediaSeeking, IPin, IQualityControl
Filter CLSID CLSID_ACMWrapper
Property Page CLSID No property page.
Executable Quartz.dll
Merit MERIT_NORMAL or MERIT_DO_NOT_USE
Filter Category CLSID_LegacyAmFilterCategory or CLSID_AudioCompressorCategory

See Also

  评论这张
 
阅读(809)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017