⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mexddgrab.cpp

📁 mmread reads virtually any media file. If Windows Media Play can play it, so should mmread. It uses
💻 CPP
字号:
/***************************************************
This is the matlab interface code to the grabber code.
It just wraps the grabber functions and does some error
conversion.

Written by Micah Richert.
07/14/2005
**************************************************/

#include "mex.h"
#include "DDGrab.h"

TCHAR str[200];

TCHAR* message(HRESULT hr)
{
	if (hr == S_OK)
	{
		return "";
	} else {
		if (AMGetErrorText(hr,str,200) != 0) return str;
		return "Unknown error";
	}
}

DDGrabber DDG;

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	if (nrhs < 1 || !mxIsChar(prhs[0])) mexErrMsgTxt("First parameter must be the command (a string)");

	char cmd[100];
	mxGetString(prhs[0],cmd,100);

	if (!strcmp("buildGraph",cmd))
	{
		if (nrhs < 2 || !mxIsChar(prhs[1])) mexErrMsgTxt("buildGraph: second parameter must be the filename (as a string)");
		if (nlhs > 0) mexErrMsgTxt("buildGraph: there are no outputs");
		int filenamelen = mxGetN(prhs[1])+1;
		char* filename = new char[filenamelen];
		if (!filename) mexErrMsgTxt("buildGraph: out of memory");
		mxGetString(prhs[1],filename,filenamelen);

		char* errmsg =  message(DDG.buildGraph(filename));
		free(filename);

		if (strcmp("",errmsg)) mexErrMsgTxt(errmsg);
		plhs[0] = NULL;
	} else if (!strcmp("doCapture",cmd)) {
		if (nlhs > 0) mexErrMsgTxt("doCapture: there are no outputs");
		char* errmsg =  message(DDG.doCapture());
		if (strcmp("",errmsg)) mexErrMsgTxt(errmsg);
		plhs[0] = NULL;
	} else if (!strcmp("getVideoInfo",cmd)) {
		if (nrhs < 2 || !mxIsNumeric(prhs[1])) mexErrMsgTxt("getVideoInfo: second parameter must be the video stream id (as a number)");
		if (nlhs > 5) mexErrMsgTxt("getVideoInfo: there are only 5 output values: width, height, rate, nrFramesCaptured, nrFramesTotal");

		unsigned int id = mxGetScalar(prhs[1]);
		int width,height,rate,nrFramesCaptured,nrFramesTotal;
		char* errmsg =  message(DDG.getVideoInfo(id, &width, &height,&rate, &nrFramesCaptured, &nrFramesTotal));

		if (strcmp("",errmsg)) mexErrMsgTxt(errmsg);

		if (nlhs >= 1) {plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(plhs[0])[0] = width; }
		if (nlhs >= 2) {plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(plhs[1])[0] = height; }
		if (nlhs >= 3) {plhs[2] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(plhs[2])[0] = rate; }
		if (nlhs >= 4) {plhs[3] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(plhs[3])[0] = nrFramesCaptured; }
		if (nlhs >= 5) {plhs[4] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(plhs[4])[0] = nrFramesTotal; }
	} else if (!strcmp("getAudioInfo",cmd)) {
		if (nrhs < 2 || !mxIsNumeric(prhs[1])) mexErrMsgTxt("getAudioInfo: second parameter must be the audio stream id (as a number)");
		if (nlhs > 5) mexErrMsgTxt("getAudioInfo: there are only 5 output values: nrChannels, rate, bits, nrFramesCaptured, nrFramesTotal");

		unsigned int id = mxGetScalar(prhs[1]);
		int nrChannels,rate,bits,nrFramesCaptured,nrFramesTotal;
		char* errmsg =  message(DDG.getAudioInfo(id, &nrChannels, &rate, &bits, &nrFramesCaptured, &nrFramesTotal));

		if (strcmp("",errmsg)) mexErrMsgTxt(errmsg);

		if (nlhs >= 1) {plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(plhs[0])[0] = nrChannels; }
		if (nlhs >= 2) {plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(plhs[1])[0] = rate; }
		if (nlhs >= 3) {plhs[2] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(plhs[2])[0] = bits; }
		if (nlhs >= 4) {plhs[3] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(plhs[3])[0] = nrFramesCaptured; }
		if (nlhs >= 5) {plhs[4] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(plhs[4])[0] = nrFramesTotal; }
	} else if (!strcmp("getCaptureInfo",cmd)) {
		if (nlhs > 2) mexErrMsgTxt("getCaptureInfo: there are only 2 output values: nrVideo, nrAudio");

		int nrVideo, nrAudio;
		DDG.getCaptureInfo(&nrVideo, &nrAudio);

		if (nlhs >= 1) {plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(plhs[0])[0] = nrVideo; }
		if (nlhs >= 2) {plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL); mxGetPr(plhs[1])[0] = nrAudio; }
	} else if (!strcmp("getVideoFrame",cmd)) {
		if (nrhs < 3 || !mxIsNumeric(prhs[1]) || !mxIsNumeric(prhs[2])) mexErrMsgTxt("getVideoFrame: second parameter must be the audio stream id (as a number) and third parameter must be the frame number");
		if (nlhs > 1) mexErrMsgTxt("getVideoFrame: there are only 1 output value: data");

		unsigned int id = mxGetScalar(prhs[1]);
		int frameNr = mxGetScalar(prhs[2]);
		char* data;
		int nrBytes;
		int dims[] = {1,1};
		char* errmsg =  message(DDG.getVideoFrame(id, frameNr, &data, &nrBytes));

		if (strcmp("",errmsg)) mexErrMsgTxt(errmsg);

		dims[0] = nrBytes;
		plhs[0] = mxCreateNumericArray(2, dims, mxUINT8_CLASS, mxREAL); // empty 2d matrix
		memcpy(mxGetPr(plhs[0]),data,nrBytes);
		free(data);
	} else if (!strcmp("getAudioFrame",cmd)) {
		if (nrhs < 3 || !mxIsNumeric(prhs[1]) || !mxIsNumeric(prhs[2])) mexErrMsgTxt("getAudioFrame: second parameter must be the audio stream id (as a number) and third parameter must be the frame number");
		if (nlhs > 1) mexErrMsgTxt("getAudioFrame: there are only 1 output value: data");

		unsigned int id = mxGetScalar(prhs[1]);
		int frameNr = mxGetScalar(prhs[2]);
		char* data;
		int nrBytes;
		int dims[] = {1,1};
		mxClassID mxClass;
		char* errmsg =  message(DDG.getAudioFrame(id, frameNr, &data, &nrBytes));

		if (strcmp("",errmsg)) mexErrMsgTxt(errmsg);

		int nrChannels,rate,bits,nrFramesCaptured,nrFramesTotal;
		DDG.getAudioInfo(id, &nrChannels, &rate, &bits, &nrFramesCaptured, &nrFramesTotal);

		switch (bits)
		{
			case 4:
			{
				char* tmpdata = (char*)malloc(nrBytes*2);
				int i;

				for (i=0;i<nrBytes;i++)
				{
					tmpdata[i*2] = ((0x8&data[i])?-1:1)*(0x7&data[i]);
					tmpdata[i*2+1] = ((0x80&data[i])?-1:1)*((0x70&data[i])>>4);
				}

				free(data);
				data = tmpdata;
				mxClass = mxINT8_CLASS;
				dims[0] = nrBytes*2;
				break;
			}
			case 8:
			{
		 		dims[0] = nrBytes;
				mxClass = mxINT8_CLASS;
				break;
			}
			case 16:
			{
				mxClass = mxINT16_CLASS;
				dims[0] = nrBytes/2;
				break;
			}
			case 24:
			{
				int* tmpdata = (int*)malloc(nrBytes/3*4);
				int i;

				for (i=0;i<nrBytes/3;i++)
				{
					tmpdata[i] = ((0x80&data[i*3+2])?-1:1)*(((0x7F&data[i+2])<<16)+(data[i]<<8)+data[i]);
				}

				free(data);
				data = (char*)tmpdata;

				mxClass = mxINT32_CLASS;
				dims[0] = nrBytes/3;
				break;
			}
			case 32:
			{
				mxClass = mxINT32_CLASS;
				dims[0] = nrBytes/4;
				break;
			}
			default:
			{
		 		dims[0] = nrBytes;
				mxClass = mxINT8_CLASS;
				break;
			}
		}

		plhs[0] = mxCreateNumericArray(2, dims, mxClass, mxREAL); // empty 2d matrix
		memcpy(mxGetPr(plhs[0]),data,nrBytes);
		free(data);
	} else if (!strcmp("setFrames",cmd)) {
		if (nrhs < 2 || !mxIsDouble(prhs[1])) mexErrMsgTxt("setFrames: second parameter must be the frame numbers (as doubles)");
		if (nlhs > 0) mexErrMsgTxt("setFrames: there are no outputs");
		int nrFrames = mxGetN(prhs[1]) * mxGetM(prhs[1]);
		int* frameNrs = new int[nrFrames];
		if (!frameNrs) mexErrMsgTxt("setFrames: out of memory");
		double* data = mxGetPr(prhs[1]);
		for (int i=0; i<nrFrames; i++) frameNrs[i] = data[i];

		DDG.setFrames(frameNrs, nrFrames);
		plhs[0] = NULL;
	} else if (!strcmp("setTime",cmd)) {
		if (nrhs < 3 || !mxIsDouble(prhs[1]) || !mxIsDouble(prhs[2])) mexErrMsgTxt("setTime: start and stop time are required (as doubles)");
		if (nlhs > 0) mexErrMsgTxt("setTime: there are no outputs");

		DDG.setTime(mxGetScalar(prhs[1]), mxGetScalar(prhs[2]));
		plhs[0] = NULL;
	} else if (!strcmp("disableVideo",cmd)) {
		if (nlhs > 0) mexErrMsgTxt("disableVideo: there are no outputs");
		DDG.disableVideo();
		plhs[0] = NULL;
	} else if (!strcmp("disableAudio",cmd)) {
		if (nlhs > 0) mexErrMsgTxt("disableAudio: there are no outputs");
		DDG.disableAudio();
		plhs[0] = NULL;
	} else if (!strcmp("cleanUp",cmd)) {
		if (nlhs > 0) mexErrMsgTxt("cleanUp: there are no outputs");
		DDG.cleanUp();
		plhs[0] = NULL;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -