📄 ddgrab.cpp
字号:
return ret;
}
HRESULT DDGrabber::getVideoInfo(unsigned int id, int* width, int* height, double* rate, int* nrFramesCaptured, int* nrFramesTotal, double* totalDuration)
{
if (!width || !height || !nrFramesCaptured || !nrFramesTotal) return E_POINTER;
if (id >= VideoCBs.size()) return E_NOINTERFACE;
CSampleGrabberCB* CB = VideoCBs.at(id);
if (!CB) return E_POINTER;
if (!CB->pbFormat)
{
*width = 0;
*height = 0;
*rate = 0;
} else {
VIDEOINFOHEADER * h = (VIDEOINFOHEADER*) CB->pbFormat;
if (!h) return E_POINTER;
*width = h->bmiHeader.biWidth;
*height = h->bmiHeader.biHeight;
if (h->AvgTimePerFrame == 0) *rate = 0; // can't calc rate correctly...
else *rate = 10000000.0/h->AvgTimePerFrame; // make it samples per second.
}
*nrFramesCaptured = CB->frames.size();
*nrFramesTotal = CB->frameNr;
*totalDuration = 0;
// see if we can get more reliable nrFramesTotal and rate information
IMediaSeeking* pMediaSeeking;
pGraphBuilder->QueryInterface(IID_IMediaSeeking, (void **)&pMediaSeeking);
if (pMediaSeeking)
{
LONGLONG duration = 0;
LONGLONG durationus = 0;
if (SUCCEEDED(pMediaSeeking->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME))) pMediaSeeking->GetDuration(&durationus);
*totalDuration = durationus/10000000.0;
if (SUCCEEDED(pMediaSeeking->SetTimeFormat(&TIME_FORMAT_FRAME)) && SUCCEEDED(pMediaSeeking->GetDuration(&duration)))
{
if (stopForced) *nrFramesTotal = duration; // if we stopped early, calculate nrFramesTotal
if (*rate == 0 && durationus) *rate = duration/(durationus/10000000.0);
} else {
// if we stopped early, calculate nrFramesTotal
if (stopForced && durationus && *rate) *nrFramesTotal = -(*rate)*(durationus/10000000.0);
}
pMediaSeeking->Release();
}
// some things don't work right if rate is 0
if (*rate == 0) *rate = 1;
return S_OK;
}
HRESULT DDGrabber::getAudioInfo(unsigned int id, int* nrChannels, double* rate, int* bits, int* nrFramesCaptured, int* nrFramesTotal, GUID* subtype, double* totalDuration)
{
if (!nrChannels || !rate || !bits || !nrFramesCaptured || !nrFramesTotal) return E_POINTER;
if (id >= AudioCBs.size()) return E_NOINTERFACE;
CSampleGrabberCB* CB = AudioCBs.at(id);
if (!CB) return E_POINTER;
if (!CB->pbFormat)
{
*nrChannels = 0;
*rate = 0;
*bits = 0;
} else {
WAVEFORMATEX * h = (WAVEFORMATEX*) CB->pbFormat;
if (!h) return E_POINTER;
*nrChannels = h->nChannels;
*rate = h->nSamplesPerSec;
*bits = h->wBitsPerSample;
*subtype = CB->subtype;
//#ifdef _DEBUG
// int nrBytes = sizeof(WAVEFORMATEX) + h->cbSize;
//
// for (int i=0;i<nrBytes;i++) _RPT1(_CRT_WARN,"%02x\n",(int)((unsigned char*)h)[i]);
//#endif
}
*nrFramesCaptured = CB->frames.size();
*nrFramesTotal = CB->frameNr;
*totalDuration = 0;
IMediaSeeking* pMediaSeeking;
pGraphBuilder->QueryInterface(IID_IMediaSeeking, (void **)&pMediaSeeking);
if (pMediaSeeking)
{
LONGLONG durationus = 0;
if (SUCCEEDED(pMediaSeeking->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME))) pMediaSeeking->GetDuration(&durationus);
*totalDuration = durationus/10000000.0;
pMediaSeeking->Release();
}
return S_OK;
}
void DDGrabber::getCaptureInfo(int* nrVideo, int* nrAudio)
{
if (!nrVideo || !nrAudio) return;
*nrVideo = (VideoCBs.size()>0&&VideoCBs.at(0) && VideoCBs.at(0)->disabled)?0:VideoCBs.size();
*nrAudio = (AudioCBs.size()>0&&AudioCBs.at(0) && AudioCBs.at(0)->disabled)?0:AudioCBs.size();
}
// data must be freed by caller
HRESULT DDGrabber::getVideoFrame(unsigned int id, int frameNr, BYTE** data, int* nrBytes, double* time)
{
if (!data || !nrBytes) return E_POINTER;
if (id >= VideoCBs.size()) return E_NOINTERFACE;
CSampleGrabberCB* CB = VideoCBs.at(id);
if (!CB) return E_POINTER;
if (CB->frameNr == 0) return E_NOINTERFACE;
if (frameNr < 0 || frameNr >= CB->frames.size()) return E_NOINTERFACE;
BYTE* tmp = (BYTE*)CB->frames.at(frameNr);
if (!tmp) return E_NOINTERFACE;
*nrBytes = CB->frameBytes.at(frameNr);
*time = CB->frameTimes.at(frameNr);
*data = (BYTE*) malloc(*nrBytes);
if (!*data) return E_OUTOFMEMORY;
memcpy(*data,tmp,*nrBytes);
return S_OK;
}
// data must be freed by caller
HRESULT DDGrabber::getAudioFrame(unsigned int id, int frameNr, BYTE** data, int* nrBytes, double* time)
{
if (!data || !nrBytes) return E_POINTER;
if (id >= AudioCBs.size()) return E_NOINTERFACE;
CSampleGrabberCB* CB = AudioCBs.at(id);
if (!CB) return E_POINTER;
if (CB->frameNr == 0) return E_NOINTERFACE;
if (frameNr < 0 || frameNr >= CB->frames.size()) return E_NOINTERFACE;
BYTE* tmp = (BYTE*)CB->frames.at(frameNr);
if (!tmp) return E_NOINTERFACE;
*nrBytes = CB->frameBytes.at(frameNr);
*time = CB->frameTimes.at(frameNr);
*data = (BYTE*) malloc(*nrBytes);
if (!*data) return E_OUTOFMEMORY;
memcpy(*data,tmp,*nrBytes);
return S_OK;
}
void DDGrabber::setFrames(int* frameNrs, int nrFrames)
{
if (!frameNrs) return;
this->frameNrs.clear();
for (int i=0; i<nrFrames; i++) this->frameNrs.add(frameNrs[i]);
for (int i=0; i < VideoCBs.size(); i++)
{
CSampleGrabberCB* CB = VideoCBs.at(i);
if (CB)
{
CB->frames.clear();
CB->frameNrs.clear();
for (int i=0; i<nrFrames; i++) CB->frameNrs.add(frameNrs[i]);
CB->frameNr = 0;
}
}
// the meaning of frames doesn't make much sense for audio...
}
void DDGrabber::setTime(double startTime, double stopTime)
{
this->startTime = startTime;
this->stopTime = stopTime;
frameNrs.clear();
for (int i=0; i < VideoCBs.size(); i++)
{
CSampleGrabberCB* CB = VideoCBs.at(i);
if (CB)
{
CB->frames.clear();
CB->frameNrs.clear();
CB->frameNr = 0;
CB->startTime = startTime;
CB->stopTime = stopTime;
}
}
for (int i=0; i < AudioCBs.size(); i++)
{
CSampleGrabberCB* CB = AudioCBs.at(i);
if (CB)
{
CB->frames.clear();
CB->frameNrs.clear();
CB->startTime = startTime;
CB->stopTime = stopTime;
}
}
}
#ifdef MATLAB_MEX_FILE
void DDGrabber::setMatlabCommand(char * matlabCommand)
{
this->matlabCommand = matlabCommand;
}
#endif
void DDGrabber::disableVideo()
{
for (int i=0; i < VideoCBs.size(); i++)
{
if (VideoCBs.at(i)) VideoCBs.at(i)->disabled = true;
}
}
void DDGrabber::disableAudio()
{
for (int i=0; i < AudioCBs.size(); i++)
{
if (AudioCBs.at(i)) AudioCBs.at(i)->disabled = true;
}
}
void DDGrabber::setTrySeeking(int tryseek)
{
tryseeking = tryseek;
}
HRESULT DDGrabber::changeToNull(IGraphBuilder* pGraphBuilder, IBaseFilter* pRenderer)
{
HRESULT hr;
if (!pGraphBuilder || !pRenderer) return E_POINTER;
IPin* outPin = connectedToInput(pRenderer);
if (!outPin) return E_NOINTERFACE;
// Add the Null Renderer filter to the graph.
IBaseFilter *pNull;
if (FAILED(hr = ::CoCreateInstance(CLSID_NullRenderer,NULL,CLSCTX_INPROC_SERVER,IID_IBaseFilter, (void**)&pNull))) return hr;
if (FAILED(hr = pGraphBuilder->AddFilter(pNull, L"NullRender"))) return hr;
if (FAILED(hr = outPin->Disconnect())) return hr;
IPin* inPin = getInputPin(pNull);
if (!inPin) return E_NOINTERFACE;
#ifdef _DEBUG
FILTER_INFO info;
pRenderer->QueryFilterInfo(&info);
char str[100];
WideCharToMultiByte( CP_ACP, 0, info.achName, -1, str, 100, NULL, NULL );
_RPT0(_CRT_WARN,str);
_RPT0(_CRT_WARN," Removed\n");
#endif
if (FAILED(hr = pGraphBuilder->RemoveFilter(pRenderer))) return hr;
pRenderer->Release();
hr = pGraphBuilder->Connect(outPin,inPin);
return hr;
}
HRESULT DDGrabber::insertCapture(IGraphBuilder* pGraphBuilder, IBaseFilter* pRenderer, AM_MEDIA_TYPE* mt, CSampleGrabberCB** grabberCB)
{
HRESULT hr;
if (!pGraphBuilder || !pRenderer || !mt || !grabberCB) return E_POINTER;
IPin* rendererPin = getInputPin(pRenderer);
IPin* upStreamPin = connectedToInput(pRenderer);
IPin* grabberInPin = NULL;
IPin* grabberOutPin = NULL;
IBaseFilter* pGrabberBaseFilter = NULL;
ISampleGrabber* pSampleGrabber = NULL;
if (!upStreamPin || !rendererPin) return E_NOINTERFACE;
// use the for loop so that we can break
for (int i=0;i<1;i++)
{
_RPT0(_CRT_WARN,"Making Grabber\n");
if (FAILED(hr = ::CoCreateInstance(CLSID_SampleGrabber,NULL,CLSCTX_INPROC_SERVER,IID_IBaseFilter, (LPVOID *)&pGrabberBaseFilter))) return hr;
pGrabberBaseFilter->QueryInterface(IID_ISampleGrabber, (void**)&pSampleGrabber);
if (pSampleGrabber == NULL) return E_NOINTERFACE;
if (FAILED(hr = pGraphBuilder->AddFilter(pGrabberBaseFilter,L"Grabber"))) break;
if (FAILED(hr = pSampleGrabber->SetMediaType(mt))) break;
if (!(grabberInPin = getInputPin(pGrabberBaseFilter))) { hr = E_NOINTERFACE; break;}
if (!(grabberOutPin = getOutputPin(pGrabberBaseFilter))) { hr = E_NOINTERFACE; break;}
_RPT0(_CRT_WARN,"Disconnecting Pins\n");
if (FAILED(hr = upStreamPin->Disconnect())) break;
if (FAILED(hr = rendererPin->Disconnect())) break;
_RPT0(_CRT_WARN,"Connecting Pins\n");
if (FAILED(hr = pGraphBuilder->Connect(upStreamPin,grabberInPin))) break;
_RPT0(_CRT_WARN,"Connecting Renderer Pins\n");
if (FAILED(hr = pGraphBuilder->Connect(grabberOutPin,rendererPin))) break;
if (FAILED(hr = pSampleGrabber->SetBufferSamples(FALSE))) break;
*grabberCB = new CSampleGrabberCB();
if (!*grabberCB) { hr = E_OUTOFMEMORY; break;}
_RPT0(_CRT_WARN,"Setting Callback\n");
if (FAILED(hr = pSampleGrabber->SetCallback( *grabberCB, 1 ))) break;
if (FAILED(hr = pSampleGrabber->GetConnectedMediaType(mt))) break;
// I don't know how long this pointer will stay valid... so copy it
if ((*grabberCB)->pbFormat) free((*grabberCB)->pbFormat);
(*grabberCB)->pbFormat = (BYTE*)malloc(mt->cbFormat);
if (!(*grabberCB)->pbFormat) { hr = E_OUTOFMEMORY; break;}
memcpy((*grabberCB)->pbFormat,mt->pbFormat,mt->cbFormat);
}
rendererPin->Release();
upStreamPin->Release();
if (grabberOutPin) grabberOutPin->Release();
if (grabberInPin) grabberInPin->Release();
if (pGrabberBaseFilter) pGrabberBaseFilter->Release();
if (pSampleGrabber) pSampleGrabber->Release();
if (FAILED(hr) && pGrabberBaseFilter)
{
_RPT0(_CRT_WARN,"Reconnecting original Pins\n");
upStreamPin->Disconnect();
if (grabberInPin) grabberInPin->Disconnect();
if (grabberOutPin) grabberOutPin->Disconnect();
rendererPin->Disconnect();
pGraphBuilder->Connect(upStreamPin,rendererPin);
_RPT0(_CRT_WARN,"Removing Grabber\n");
pGraphBuilder->RemoveFilter(pGrabberBaseFilter);
}
return hr;
}
HRESULT DDGrabber::insertVideoCapture(IGraphBuilder* pGraphBuilder, IBaseFilter* pRenderer)
{
AM_MEDIA_TYPE mt = {0};
CSampleGrabberCB* grabberCB;
if (!pGraphBuilder || !pRenderer) return E_POINTER;
mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;
mt.formattype = FORMAT_VideoInfo;
_RPT0(_CRT_WARN,"Trying to add a VideoCapture.\n");
HRESULT hr = insertCapture(pGraphBuilder, pRenderer, &mt, &grabberCB);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -