Search code examples
c++opencvvideocomdirectshow

Tried to modify Videocapture::retrieve() fun


I am using C920 Logitech Web camera for the object recognition project .

I want to use my own H264 decoder for decoding the compressed stream from camera (Not caring about the performance and timing as of now which will be affected by my H264 decoder).

I want to hack the bool VideoCapture::retrieve(Mat& image, int channel=0) and replace the decoder call of the opencv retrive() function by my H264 decoder .
I took care about the return type as well as parameter list of the both decoder function (BOTH ARE SIMILAR).
But I am facing below problem in integrating/modifying my sample code . So my queries are below :

I am using OPENCV 3.0 VERSION .

Q.1) With below sample code, am getting errors-

#include "precomp.hpp"
#include "opencv2/opencv.hpp"

#include <strmif.h>
#include <iostream>

using namespace std ;
using namespace cv;

struct _AMMediaType;
typedef _AMMediaType AM_MEDIA_TYPE;

DEFINE_GUID(FORMAT_VideoInfo,0x05589f80,0xc356,0x11ce,0xbf,0x01,0x00,0xaa,0x00,0x55,0x59,0x5a);
DEFINE_GUID(MEDIATYPE_Video,0x73646976,0x0000,0x0010,0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71);
DEFINE_GUID(MEDIASUBTYPE_RGB24,0xe436eb7d,0x524f,0x11ce,0x9f,0x53,0x00,0x20,0xaf,0x0b,0xa7,0x70);


int main(int, char**)
{
    int t1= 10;
    int t2= 10;
    VideoCapture cap(0); // open the default camera
    cap.set(3,1280);
    cap.set(4,800);
    AM_MEDIA_TYPE mt;
    ZeroMemory(&mt,sizeof(AM_MEDIA_TYPE));

    mt.majortype     = MEDIATYPE_Video;
    mt.subtype       = MEDIASUBTYPE_RGB24;
    mt.formattype    = FORMAT_VideoInfo;

    if(!cap.isOpened())  // check if we succeeded
        return -1;

    for(;;)
    {

        Mat frame;
        if( !cap.grab() )
        {
            cout << "Can not grab images." << endl;
            return -1;
        }
        cap.retrieve(frame);
        imshow("cameracapture", frame);

        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

I am getting below linker errors :

1>main.obj : error LNK2001: unresolved external symbol FORMAT_VideoInfo
1>main.obj : error LNK2001: unresolved external symbol MEDIATYPE_Video
1>main.obj : error LNK2001: unresolved external symbol MEDIASUBTYPE_RGB24

I linked all lib in my project properties (by using VS2012),But no luck :(

Q.2) In OpenCV3.0\modules\videoio\src\cap_dshow.cpp file :

    mt.majortype     = MEDIATYPE_Video;
    mt.subtype       = MEDIASUBTYPE_RGB24;
    mt.formattype    = FORMAT_VideoInfo;

As my LOGITECH C920 camera is supporting H264 codec, so I tried to modify the the above variables and added below lines of code in my sample code-

    mt.majortype     = MEDIATYPE_Video;
    mt.subtype       = MEDIASUBTYPE_H264;
    mt.formattype    = FORMAT_VideoInfo;

I got below error :

main.obj :error C2065: 'MEDIASUBTYPE_H264' : undeclared identifier

What modifications required to get the H264 compressed video

Q-3) If I will modify the OpenCV code itself, then How I can build the complete OPENCV3.0 code(with my modification in windows with VS2012) and use it for my purpose ??


Solution

  • You need to define INITGUID to resolve mentioned linker errors (see Referencing GUIDs for details).

    However I don't see how it is going to help you in the part of supplying your H.264 decoder (too far from there).

    To reference H.264 subtype identifier you need

    #include <wmcodecdsp.h>
    #pragma comment(lib, "wmcodecdspuuid.lib")