Unable to RenderStream when using SampleGrabber with Microsoft DTV-DVD Audio Decoder and Direct Sound Renederer - directshow

I would like to play only audio from video file. For this I'm using directshow SampleGrabber with Major_type set to MEDIATYPE_Audio and DirectSound Audio Renderer.
This works fine for .wmv files. However when I try to play .mp4 files. The RenderStream fails.
I then tried creating filter in GrapStudioNext with following filtet
File Source->LAV Splitter (Automatically load)->SampleGrabber->MS DTV-DVD Audio Decoder->Default DirectSoude Device
The above filter works fine and I'm to listen to the audio.
However when I create the same filter graph manually in vc++. The RenderStream calls fails while connecting source filter to SampleGrabber
HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&m_pGraph);
if(FAILED(hr))
{
ReleaseResource();
return hr;
}
hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2,
(void **)&m_pBuilder);
if (SUCCEEDED(hr))
{
m_pBuilder->SetFiltergraph(m_pGraph);
}
else
{
std::cout<<"Failed to Created Capture Graph"<<std::endl;
ReleaseResource();
return hr;
}
hr = m_pGraph->QueryInterface(IID_IMediaControl, (void**)&m_pControl);
if(FAILED(hr))
{
std::cout<<"Failed to Get Media Control Interface"<<std::endl;
ReleaseResource();
return hr;
}
hr = m_pGraph->QueryInterface(IID_IMediaEvent, (void**)&m_pEvent);
if(FAILED(hr))
{
std::cout<<"Failed to Get Media Event"<<std::endl;
ReleaseResource();
return hr;
}
hr = m_pGraph->QueryInterface(IID_IMediaSeeking, (void**)&m_pMediaSeek);
if(FAILED(hr))
{
std::cout<<"Failed to Get Media Seeking"<<std::endl;
ReleaseResource();
return hr;
}
hr = m_pGraph->QueryInterface(IID_IBasicAudio, (void**)&m_pBasicAudio);
if(FAILED(hr))
{
std::cout<<"Failed to Get Basic Audio"<<std::endl;
ReleaseResource();
return hr;
}
#if 0
hr = CoCreateInstance(CLSID_AudioRender, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void**)&m_pBaseFilter);
#endif
hr = CoCreateInstance(CLSID_DSoundRender, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void**)&m_pBaseFilter);
if(FAILED(hr))
{
std::cout<<"Failed to Create Audio Render"<<std::endl;
ReleaseResource();
return hr;
}
hr = m_pGraph->AddFilter(m_pBaseFilter, L"Audio Filter");
if(FAILED(hr))
{
std::cout<<"Failed to Add Audio Render"<<std::endl;
ReleaseResource();
return hr;
}
hr = m_pGraph->AddSourceFilter((LPCWSTR)wxStr,L"SourceFlt", &m_pSourceFilter);
if(FAILED(hr))
{
std::cout<<"Failed to Add Source Filter"<<std::endl;
ReleaseResource();
return hr;
}
hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void**)&m_pBaseFilterSG);
if(FAILED(hr))
{
std::cout<<"Failed to Create Sample Grabber"<<std::endl;
ReleaseResource();
return 1;
}
hr = m_pGraph->AddFilter(m_pBaseFilterSG, L"Sample_Grabber");
if(FAILED(hr))
{
std::cout<<"Failed to Add Sample Grabber"<<std::endl;
ReleaseResource();
return 1;
}
hr = m_pBaseFilterSG->QueryInterface(IID_ISampleGrabber, (void**)&m_pSampleGrabber);
if(FAILED(hr))
{
std::cout<<"Query Interface Failed for Sample Grabber"<<std::endl;
ReleaseResource();
return 1;
}
ZeroMemory(&mt, sizeof(mt));
mt.majortype = MEDIATYPE_Audio;
mt.subtype = MEDIASUBTYPE_RAW_AAC1;
hr = m_pSampleGrabber->SetMediaType(&mt);
if(FAILED(hr))
{
std::cout<<"Failed to Set Media Type"<<std::endl;
ReleaseResource();
return 1;
}
hr = CoCreateInstance(CLSID_CMPEG2AudDecoderDS , NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void**)&m_pMSAduioDecoder);
if(FAILED(hr))
{
std::cout<<"Failed to Create Sample Grabber"<<std::endl;
ReleaseResource();
return 1;
}
hr = m_pGraph->AddFilter(m_pMSAduioDecoder, L"Audio_Decoder");
if(FAILED(hr))
{
std::cout<<"Failed to Add Adudio Decoder"<<std::endl;
ReleaseResource();
return 1;
}
hr = m_pBuilder->RenderStream( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Audio,m_pSourceFilter, m_pBaseFilterSG,m_pMSAduioDecoder);
if(FAILED(hr))
{
std::cout<<"Failed to Render First Chain"<<std::endl;
ReleaseResource();
return hr;
}
hr = m_pBuilder->RenderStream(NULL, NULL,m_pMSAduioDecoder,NULL, m_pBaseFilter);
if(FAILED(hr))
{
std::cout<<"Failed to Render Second Chain"<<std::endl;
ReleaseResource();
return hr;
}
Also when I SetMediaType to MEDIATYPE_Video the first chain connects properly. But seconds one fails even after using PIN_CATEOGARY_CAPTURE and MediaType_Audio.
Also the error I'm getting in RenderStream invalid args.
Please suggest what could be going wrong. Thanks in advance.
Pradeep

This is totally wrong. You don't have capture sources at all, so trying to leverage Capture Graph Builder is weak in first place. You don't have a single pin in your graph which is PIN_CATEGORY_CAPTURE and so it fails.
You know all the filters you need to use, CoCreateInstance, IGraphBuilder::AddFilter and IPin::Connect are sufficient for graph building, you don't need capture graph builder here at all.

Related

Is there any control property to fix video playback speed problem when using ffmpeg to decode in Qt platform?

I want to play local video file in Qt platform using ffmpeg to decode.Everything is OK except that play speed is as twice as normal.
The first thing I think about is that there must be a sampling frequency involved.But to be a new to ffmpeg,I don't know how to fix this problem.
Above is my code to read frame,is anyone can tell me what's wrong with the code ?
void VideoThread::run()
{
m_pInFmtCtx = avformat_alloc_context(); //ini struct
char path[] = "d:/test.mp4";
// open specific file
if(avformat_open_input(&m_pInFmtCtx, *path, NULL, NULL)){
{
qDebug()<<"get rtsp failed";
return;
}
else
{
qDebug()<<"get rtsp success";
}
if(avformat_find_stream_info(m_pInFmtCtx, NULL) < 0)
{
qDebug()<<"could not find stream information";
return;
}
int nVideoIndex = -1;
for(int i = 0; i < m_pInFmtCtx->nb_streams; i++)
{
if(m_pInFmtCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
nVideoIndex = i;
break;
}
}
if(nVideoIndex == -1)
{
qDebug()<<"could not find video stream";
return;
}
qDebug("---------------- File Information ---------------");
m_pCodecCtx = m_pInFmtCtx->streams[nVideoIndex]->codec;
m_pCodec = avcodec_find_decoder(m_pCodecCtx->codec_id);
if(!m_pCodec)
{
qDebug()<<"could not find codec";
return;
}
//start Decoder
if (avcodec_open2(m_pCodecCtx, m_pCodec, NULL) < 0) {
qDebug("Could not open codec.\n");
return;
}
//malloc space for stroring frame
m_pFrame = av_frame_alloc();
m_pFrameRGB = av_frame_alloc();
m_pOutBuf = (uint8_t*)av_malloc(avpicture_get_size(AV_PIX_FMT_RGB32, m_pCodecCtx->width, m_pCodecCtx->height));
avpicture_fill((AVPicture*)m_pFrameRGB, m_pOutBuf, AV_PIX_FMT_RGB32, m_pCodecCtx->width, m_pCodecCtx->height);
//for color switch,from YUV to RGB
struct SwsContext *pImgCtx = sws_getContext(m_pCodecCtx->width, m_pCodecCtx->height, m_pCodecCtx->pix_fmt,
m_pCodecCtx->width, m_pCodecCtx->height, AV_PIX_FMT_RGB32, SWS_BICUBIC, NULL, NULL, NULL);
int nSize = m_pCodecCtx->width * m_pCodecCtx->height;
m_pPacket = (AVPacket *)av_malloc(sizeof(AVPacket));
if(av_new_packet(m_pPacket, nSize) != 0)
{
qDebug()<<"new packet failed";
}
//isInterruptionRequested is a flag,determine whether the thread is over
// read each frame from specific video file
while (!isInterruptionRequested())
{
int nGotPic = 0;
if(av_read_frame(m_pInFmtCtx, m_pPacket) >= 0)
{
if(m_pPacket->stream_index == nVideoIndex)
{
//avcodec_decode_video2()transform from packet to frame
if(avcodec_decode_video2(m_pCodecCtx, m_pFrame, &nGotPic, m_pPacket) < 0)
{
qDebug()<<"decode failed";
return;
}
if(nGotPic)
{ // transform to RGB color
sws_scale(pImgCtx, (const uint8_t* const*)m_pFrame->data,
m_pFrame->linesize, 0, m_pCodecCtx->height, m_pFrameRGB->data,
m_pFrameRGB->linesize);
// save to QImage,for later use
QImage *pImage = new QImage((uchar*)m_pOutBuf, m_pCodecCtx->width, m_pCodecCtx->height, QImage::Format_RGB32);
}
}
}
av_free_packet(m_pPacket);
msleep(5);
}
exec();
}

Changing resolution using media foundation under win 7

I am writing an app on top media foundation under win 7, I use IMFMediaSource to query the cameras interfaces to get frames and other properties. its weird but I cant find a way to change resolution. it seems that if I used IMFCaptureSource i could use SetCurrentDeviceMediaType to change resolution but its only supported in Windows 8. so we cant change resolution under win 7 using media foundation?? is there a way to use direct show with IMFMediaSource to change resolution??
if so, can anyone help with some code sample?
thanks!
ok, so I found out eventually. Iam using IMFSourceReader to get samples from the MFMediaSource, so after configurating the SourceReader you can iterate the native media types that the camera supports like this :
HRESULT nativeTypeErrorCode = S_OK;
DWORD count = 0;
UINT32 streamIndex = 0;
UINT32 requiredWidth = 1600;
UINT32 requiredheight = 900;
while (nativeTypeErrorCode == S_OK)
{
IMFMediaType * nativeType = NULL;
nativeTypeErrorCode = m_pReader->GetNativeMediaType(streamIndex, count, &nativeType);
if(nativeTypeErrorCode != S_OK) continue;
// get the media type
GUID nativeGuid = {0};
hr = nativeType->GetGUID(MF_MT_SUBTYPE, &nativeGuid);
if (FAILED(hr)) return hr;
UINT32 width, height;
hr = ::MFGetAttributeSize(nativeType, MF_MT_FRAME_SIZE, &width, &height);
if (FAILED(hr)) return hr;
if(nativeGuid == <my type guid> && width == requiredWidth && height == requiredheight)
{
// found native config, set it
hr = m_pReader->SetCurrentMediaType(streamIndex, NULL, nativeType);
if (FAILED(hr)) return hr;
break;
}
SafeRelease(&nativeType);
count++;
}
this means that Iam not creating a new media type with the resolution I require, I get the native media type with the configuration I need, and I set it on the SourceReader.
hope it will help the future media foundation traveler... :)
You can query directshow interface from IMediaSource which can change resolution.
for ex:for Camera control properties I do like this.
IAMCameraControl* m_pCameraControl = NULL;
HRESULT hr = S_OK;
hr = pMediaSource->QueryInterface(IID_PPV_ARGS(&m_pCameraControl));
if (m_pCameraControl == NULL)
{
return E_FAIL;
}
In the same way in your case I am not sure about the interface but I guess it will be in following way.
IAMStreamConfig * m_pStreamConfig = NULL;
HRESULT hr = S_OK;
hr = pMediaSource->QueryInterface(IID_PPV_ARGS(&m_pStreamConfig ));
if (m_pCameraControl == NULL)
{
return E_FAIL;
}

How to output PCM data in MFT

Now I have create an ogg decoder in media foundation.
I have decode the ogg data to PCM data in IMFTransform::ProcessOutput.
but I cannot play the PCM data, so now how to play the pcm data?
This is my ProcessOutput code:
HRESULT OggDecoder:: ProcessOutput(
DWORD dwFlags, DWORD cOutputBufferCount,
MFT_OUTPUT_DATA_BUFFER *pOutputSamples, // one per stream
DWORD *pdwStatus )
{
if (dwFlags != 0)
{
return E_INVALIDARG;
}
if (pOutputSamples == NULL || pdwStatus == NULL)
{
return E_POINTER;
}
// Must be exactly one output buffer.
if (cOutputBufferCount != 1)
{
return E_INVALIDARG;
}
// It must contain a sample.
if (pOutputSamples[0].pSample == NULL)
{
return E_INVALIDARG;
}
EnterCriticalSection(&m_critSec);
HRESULT hr = S_OK;
DWORD cbData = 0;
IMFMediaBuffer *pOutput = NULL;
// If we don't have an input sample, we need some input before
// we can generate any output.
if (!HasPendingOutput())
{
hr = MF_E_TRANSFORM_NEED_MORE_INPUT;
}
// Get the output buffer.
if (SUCCEEDED(hr))
{
hr = pOutputSamples[0].pSample->GetBufferByIndex(0, &pOutput);
}
if (SUCCEEDED(hr))
{
hr = pOutput->GetMaxLength(&cbData);
}
if (SUCCEEDED(hr))
{
BYTE* pPCM=NULL;
pOutputBuffer->Lock(&pPCM,NULL,NULL);
GetPCMData(&pPCM); // decode audio data here
pOutputBuffer->SetCurrentLength(nLength);
pOutputSamples[0].pSample->SetSampleTime(sampleTime);
pOutputSamples[0].pSample->SetSampleDuration(sampleDuration);
pOutputBuffer->Unlock();
}
SafeRelease(&pOutput);
LeaveCriticalSection(&m_critSec);
return hr;
}
Is there I missing something or what' wrong with this code.
thanks.
if you use topoedit.exe for debug, it can add one resampler DMO automatically which is a DMO for converting pcm to float format.
you can write the player app, and create the topology by youself, and then you add the resamplyer dmo node.

Video Renderer hangs in Directshow

I have created a filter graph manually in a Directshow experiment. Here, I have added a video source filter and a VMR-9 renderer. The Video Window of the Renderer does not move, minimize, close until the video reaches end of file. If I directly render the source filter, this does not occur. I need a solution to this.
while(1)
{
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
IBaseFilter *pInputFileFilter = NULL;
IBaseFilter *pVideoRenderer = NULL;
IPin *pFileOut = NULL, *pVidIn = NULL;
IVideoWindow *VidWindow=NULL;
string s=openfilename();
wstring ws;
ws.assign (s.begin (), s.end ());
// Initialize the COM library.
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
printf("ERROR - Could not initialize COM library");
return 1;
}
// Create the filter graph manager and query for interfaces.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
{
printf("ERROR - Could not create the Filter Graph Manager.");
return 1;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// And add the filter to the filter graph
// using the member function AddFilter.
hr = pGraph->AddSourceFilter(ws.c_str(), ws.c_str(), &pInputFileFilter);
if (SUCCEEDED(hr))
{
// Now create an instance of the video renderer
// and obtain a pointer to its IBaseFilter interface.
hr = CoCreateInstance(CLSID_VideoMixingRenderer9,NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter,
(void **)&pVideoRenderer);
if (SUCCEEDED(hr))
{
hr = pGraph->AddFilter(pVideoRenderer, L"Video Renderer");
//pVideoRenderer->QueryInterface(IID_IVideoWindow,(void**)&VidWindow);
if (SUCCEEDED(hr))
{
// Now we need to connect the output pin of the source
// to the input pin of the renderer.
// Obtain the output pin of the source filter.
// The local function GetPin does this.
pFileOut = GetPin(pInputFileFilter, PINDIR_OUTPUT);
if (pFileOut != NULL)
{ // Is the pin good?
// Obtain the input pin of the WAV renderer.
// Obtain the input pin of the WAV renderer.
pVidIn = GetPin(pVideoRenderer, PINDIR_INPUT);
if (pVidIn != NULL)
{ // Is the pin good?
// Connect the pins together:
// We use the Filter Graph Manager's
// member function Connect,
// which uses Intelligent Connect.
// If this fails, DirectShow couldn't
// render the media file.
hr = pGraph->Connect(pFileOut, pVidIn);
}
}
}
}
}
if (SUCCEEDED(hr))
{
//VidWindow->put_FullScreenMode(OATRUE);
//VidWindow->put_Owner(NULL);
// Run the graph.
hr = pControl->Run();
if (SUCCEEDED(hr))
{
// Wait for completion.
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
// Note: Do not use INFINITE in a real application, because it
// can block indefinitely.
}
hr = pControl->Stop();
}
// Now release everything we instantiated--
// that is, if it got instantiated.
if(pFileOut)
{ // If it exists, non-NULL
pFileOut->Release(); // Then release it
}
if (pVidIn)
{
pVidIn->Release();
}
if (pInputFileFilter)
{
pInputFileFilter->Release();
}
if (pVideoRenderer)
{
pVideoRenderer->Release();
}
//VidWindow->Release();
pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();
}
Look into your code at the fragment:
// Do not use INFINITE in a real application, because it
// can block indefinitely.
This is where you are expected to add a message loop and dispatch messages. This is going to bring some awaited life into your application. You can poll for completion or register your window to receive a message when such completion occurs.

How to get data from directshow filter output pin?

I have direct show filter which takes an input and process it and give the result to outputpin.
I want to write this filter output data to a file...And i want to do it in its filter class.So i want to get the output pin buffer data.
Shortly how to reach final data of outputpin in its filter? How can i do it?
Not: The output pin is derived from CBaseOutputPin.This is an open source filter it "magically" :-) put wright data to its output pin which i can not figure out how yet...
Update:
Here is the siutuation:
Media Source ----> GFilter ----> FileWriter
I have source code of GFilter... I have no source code of FileWriter...What i want to make is make GFilter write its own data...I debug GFilter get some insight how its transform data but my attemp to write this data result with wrong data... So i deceide for now how to simply get data at its output pin...
Update[2]
In Filter outputpin somwhere the filter writer pass the file writer pin to IStreamPtr variable...Everthing seems to written to a variable m_pIStream which is type of [IStreamPtr]
GFilterOutput::CompleteConnect(IPin *pReceivePin)
{
// make sure that this is the file writer, supporting
// IStream, or we will not be able to write out the metadata
// at stop time
// m_pIStream is IStreamPtr type
m_pIStream = pReceivePin;
if (m_pIStream == NULL)
{
return E_NOINTERFACE;
}
return CBaseOutputPin::CompleteConnect(pReceivePin);
}
...
GFilterOutput::Replace(LONGLONG pos, const BYTE* pBuffer, long cBytes)
{
//OutputDebugStringA("DEBUG: Now at MuxOutput Replace");
// all media content is written when the graph is running,
// using IMemInputPin. On stop (during our stop, but after the
// file writer has stopped), we switch to IStream for the metadata.
// The in-memory index is updated after a successful call to this function, so
// any data not written on completion of Stop will not be in the index.
CAutoLock lock(&m_csWrite);
HRESULT hr = S_OK;
if (m_bUseIStream)
{
IStreamPtr pStream = GetConnected();
if (m_pIStream == NULL)
{
hr = E_NOINTERFACE;
} else {
LARGE_INTEGER liTo;
liTo.QuadPart = pos;
ULARGE_INTEGER uliUnused;
hr = m_pIStream->Seek(liTo, STREAM_SEEK_SET, &uliUnused);
if (SUCCEEDED(hr))
{
ULONG cActual;
hr = m_pIStream->Write(pBuffer, cBytes, &cActual);
if (SUCCEEDED(hr) && ((long)cActual != cBytes))
{
hr = E_FAIL;
}
}
}
} else {
// where the buffer boundaries lie is not important in this
// case, so break writes up into the buffers.
while (cBytes && (hr == S_OK))
{
IMediaSamplePtr pSample;
hr = GetDeliveryBuffer(&pSample, NULL, NULL, 0);
if (SUCCEEDED(hr))
{
long cThis = min(pSample->GetSize(), cBytes);
BYTE* pDest;
pSample->GetPointer(&pDest);
CopyMemory(pDest, pBuffer, cThis);
pSample->SetActualDataLength(cThis);
// time stamps indicate file position in bytes
LONGLONG tStart = pos;
LONGLONG tEnd = pos + cThis;
pSample->SetTime(&tStart, &tEnd);
hr = Deliver(pSample);
if (SUCCEEDED(hr))
{
pBuffer += cThis;
cBytes -= cThis;
pos += cThis;
}
}
}
}
return hr;
}
You have full source code, step it through with debugger until you reach the point where your filter calls IPin::Receive of the peer downstream filter, update/override code there and you have full control as for writing data into file etc.

Resources