├── .gitignore ├── include └── videocapture │ ├── linux │ ├── V4L2_Utils.h │ ├── V4L2_Devices.h │ ├── V4L2_Types.h │ └── V4L2_Capture.h │ ├── Utils.h │ ├── win │ ├── MediaFoundation_Types.h │ ├── MediaFoundation_Utils.h │ ├── MediaFoundation_Callback.h │ └── MediaFoundation_Capture.h │ ├── decklink │ ├── Decklink.h │ ├── DecklinkCallback.h │ └── DecklinkDevice.h │ ├── mac │ ├── AVFoundation_Capture.h │ ├── AVFoundation_Interface.h │ └── AVFoundation_Implementation.h │ ├── Capture.h │ ├── CapabilityFinder.h │ ├── Base.h │ └── Types.h ├── src ├── videocapture │ ├── win │ │ ├── MediaFoundation_Types.cpp │ │ ├── MediaFoundation_Callback.cpp │ │ └── MediaFoundation_Utils.cpp │ ├── linux │ │ ├── V4L2_Types.cpp │ │ ├── V4L2_Devices_Udev.cpp │ │ ├── V4L2_Devices_Default.cpp │ │ └── V4L2_Utils.cpp │ ├── mac │ │ └── AVFoundation_Capture.cpp │ ├── decklink │ │ ├── DecklinkCallback.cpp │ │ ├── DecklinkDevice.cpp │ │ └── Decklink.cpp │ ├── Utils.cpp │ ├── Base.cpp │ ├── Capture.cpp │ ├── CapabilityFinder.cpp │ └── Types.cpp ├── test_linux_device_list.cpp ├── decklink_example.cpp ├── test_conversion.cpp ├── test_capability_filter.cpp ├── api_example.cpp ├── test_v4l2_devices.cpp ├── easy_opengl_example.cpp └── opengl_example.cpp ├── docs ├── source │ ├── index.rst │ ├── gettingstarted.rst │ ├── guide.rst │ └── conf.py ├── Makefile └── make.bat ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | *.dylib 9 | 10 | # Compiled Static libraries 11 | *.lai 12 | *.la 13 | *.a 14 | -------------------------------------------------------------------------------- /include/videocapture/linux/V4L2_Utils.h: -------------------------------------------------------------------------------- 1 | #ifndef VIDEO_CAPTURE_V4L2_UTILS_H 2 | #define VIDEO_CAPTURE_V4L2_UTILS_H 3 | 4 | extern "C" { 5 | # include 6 | } 7 | 8 | namespace ca { 9 | 10 | int capture_format_to_v4l2_pixel_format(int fmt); 11 | int v4l2_pixel_format_to_capture_format(int fmt); 12 | std::string v4l2_pixel_format_to_string(int fmt); 13 | 14 | } /* namespace ca */ 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /src/videocapture/win/MediaFoundation_Types.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace ca { 4 | 5 | /* -------------------------------------- */ 6 | 7 | MediaFoundation_Device::MediaFoundation_Device() { 8 | clear(); 9 | } 10 | 11 | MediaFoundation_Device::~MediaFoundation_Device() { 12 | clear(); 13 | } 14 | 15 | void MediaFoundation_Device::clear() { 16 | } 17 | 18 | } // namespace ca 19 | -------------------------------------------------------------------------------- /src/test_linux_device_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace ca; 6 | 7 | int main() { 8 | 9 | printf("\n\nTest Devices.\n"); 10 | 11 | std::vector devices = v4l2_get_devices(); 12 | 13 | for (size_t i = 0; i < devices.size(); ++i) { 14 | printf("> %s\n", devices[i].toString().c_str()); 15 | } 16 | 17 | printf("\n\n"); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /include/videocapture/Utils.h: -------------------------------------------------------------------------------- 1 | #ifndef VIDEO_CAPTURE_UTILS_H 2 | #define VIDEO_CAPTURE_UTILS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace ca { 10 | 11 | int fps_from_rational(uint64_t num, uint64_t den); /* Converts a rational value to one of the CA_FPS_* values defined in Types.h */ 12 | std::string format_to_string(int fmt); 13 | 14 | }; // namespace ca 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /include/videocapture/win/MediaFoundation_Types.h: -------------------------------------------------------------------------------- 1 | #ifndef VIDEO_CAPTURE_MEDIA_FOUNDATION_TYPES_H 2 | #define VIDEO_CAPTURE_MEDIA_FOUNDATION_TYPES_H 3 | 4 | namespace ca { 5 | 6 | /* -------------------------------------- */ 7 | 8 | class MediaFoundation_Device { /* Wrapper around a Media Foundation device */ 9 | public: 10 | MediaFoundation_Device(); 11 | ~MediaFoundation_Device(); 12 | void clear(); 13 | }; 14 | 15 | 16 | } // namespace ca 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /docs/source/index.rst: -------------------------------------------------------------------------------- 1 | .. Video Capture documentation master file, created by 2 | sphinx-quickstart on Mon Mar 10 09:23:03 2014. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Cross Platform Video Capture Library 7 | ==================================== 8 | 9 | Video Capture is a cross platform library to capture video frames from 10 | capture devices. This library uses modern SDKs/APIs for Mac, Linux and 11 | Windows. This library is tested and developed on Win 8.1, Mac 10.9 12 | and Arch Linux (latest Linux). 13 | 14 | Contents: 15 | 16 | .. toctree:: 17 | :maxdepth: 2 18 | 19 | gettingstarted 20 | guide 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /include/videocapture/linux/V4L2_Devices.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Get device list 4 | =============== 5 | 6 | We abstract the way we retrieve devices on linux. Originally 7 | we used `udev` to retrieve a list of devices but this isn't 8 | supported on all systems. Therefore we also implemented a 9 | solution to retrieve devices which is more portatable 10 | (see V4L2_Devices_Default.cpp). 11 | 12 | */ 13 | #ifndef VIDEO_CAPTURE_V4L2_DEVICES_UDEV_H 14 | #define VIDEO_CAPTURE_V4L2_DEVICES_UDEV_H 15 | 16 | extern "C" { 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | # include 23 | # include 24 | # include 25 | # include 26 | # include 27 | # include 28 | # include 29 | } 30 | 31 | #include 32 | 33 | namespace ca { 34 | 35 | std::vector v4l2_get_devices(); 36 | 37 | } /* namespace ca */ 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /include/videocapture/decklink/Decklink.h: -------------------------------------------------------------------------------- 1 | #ifndef VIDEO_CAPTURE_DECKLINK_H 2 | #define VIDEO_CAPTURE_DECKLINK_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | namespace ca { 13 | 14 | class Decklink : public Base { 15 | public: 16 | Decklink(frame_callback fc, void* user); 17 | ~Decklink(); 18 | 19 | int open(Settings settings); 20 | int close(); 21 | int start(); 22 | int stop(); 23 | void update(); 24 | 25 | // int listDevices(); 26 | // int listCapabilities(); 27 | 28 | std::vector getDevices(); 29 | std::vector getCapabilities(int device); 30 | std::vector getOutputFormats(); 31 | 32 | IDeckLink* getDevice(int index); 33 | 34 | public: 35 | 36 | #if defined(_WIN32) 37 | static bool is_com_initialized; 38 | #endif 39 | 40 | DecklinkDevice* decklink_device; /* The opened device. */ 41 | }; 42 | 43 | } /* namespace ca */ 44 | 45 | #endif 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /include/videocapture/mac/AVFoundation_Capture.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | AVFoundation_Capture 4 | ------------- 5 | 6 | Grabbing on Mac using AVFoundation. 7 | 8 | */ 9 | #ifndef VIDEO_CAPTURE_AV_FOUNDATION_H 10 | #define VIDEO_CAPTURE_AV_FOUNDATION_H 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | namespace ca { 18 | 19 | class AVFoundation_Capture : public Base { 20 | 21 | public: 22 | AVFoundation_Capture(frame_callback fc, void* user); 23 | ~AVFoundation_Capture(); 24 | 25 | /* Interface */ 26 | int open(Settings settings); 27 | int close(); 28 | int start(); 29 | int stop(); 30 | void update(); 31 | 32 | /* Capabilities */ 33 | // int getOutputFormat(); 34 | std::vector getCapabilities(int device); 35 | std::vector getDevices(); 36 | std::vector getOutputFormats(); 37 | 38 | private: 39 | void* cap; /* The AVFoundation_Implementation interface */ 40 | }; 41 | 42 | }; // namespace ca 43 | 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /include/videocapture/linux/V4L2_Types.h: -------------------------------------------------------------------------------- 1 | #ifndef VIDEO_CAPTURE_V4L2_TYPES_H 2 | #define VIDEO_CAPTURE_V4L2_TYPES_H 3 | 4 | #include 5 | 6 | namespace ca { 7 | 8 | /* -------------------------------------- */ 9 | 10 | class V4L2_Buffer { /* A V4L2_Buffer is used to store the frames of the camera */ 11 | public: 12 | V4L2_Buffer(); 13 | ~V4L2_Buffer(); 14 | void clear(); /* Sets the buffer to NULL and size to 0. IMPORTANT: we do not free any allocated memory; the user of this buffer should do that! */ 15 | 16 | public: 17 | void* start; 18 | size_t length; 19 | }; 20 | 21 | /* -------------------------------------- */ 22 | 23 | class V4L2_Device { /* Represents a V4L2 device */ 24 | public: 25 | V4L2_Device(); 26 | ~V4L2_Device(); 27 | void clear(); 28 | std::string toString(); 29 | 30 | public: 31 | std::string path; 32 | std::string id_vendor; 33 | std::string id_product; 34 | std::string driver; 35 | std::string card; 36 | std::string bus_info; 37 | int version_major; 38 | int version_minor; 39 | int version_micro; 40 | }; 41 | 42 | } // namespace ca 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Cross Platform Video Capture 2 | ============================= 3 | 4 | Cross Platform Video Capture library for Mac, Windows and Linux. 5 | 6 | See [Read The Docs](http://video-capture.readthedocs.org/) for the documentation. 7 | 8 | Compiling 9 | ========= 10 | 11 | **Linux** 12 | 13 | ````sh 14 | cd video_capture/build 15 | ./release_x86.sh 64 16 | ```` 17 | 18 | **Windows** 19 | 20 | Open a GIT BASH shell! 21 | 22 | ````sh 23 | cd video_capture/build 24 | ./release_x86.sh 64 25 | ```` 26 | 27 | Decklink 28 | ======== 29 | Currently we're adding support for Decklink capture devices, here are just 30 | some notes about the development. 31 | 32 | ````sh 33 | 34 | Linux 35 | ----- 36 | - You need to download the DeckLink SDK 37 | - On Linux you use compile with the DeckLinkAPI.cpp and link with libDeckLinkAPI.so which 38 | is loaded automatically when you installed desktop video. 39 | - Download the Desktop Video AUR package on Arch Linux: https://aur.archlinux.org/packages/decklink/ 40 | - Extract the tarbal 41 | - Run `makepkg -s` (-s installs dependencies) 42 | - Install with: `sudo pacman -U decklink-10.2.1a1-1-x86_64.pkg.tar.xz` 43 | 44 | Windows 45 | -------- 46 | - Download the DeckLink SDK 47 | - Copy the contents of `include/*` from the DeckLink SDK to `extern/win-vs*-*/include/decklink/ 48 | 49 | 50 | ```` 51 | 52 | TODO: 53 | ===== 54 | 55 | - On Windows we need to set the desired framerate 56 | - We probably want to pass a device index to getOutputFormats() 57 | -------------------------------------------------------------------------------- /src/videocapture/linux/V4L2_Types.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace ca { 5 | 6 | /* V4L2_Buffer */ 7 | /* -------------------------------------- */ 8 | V4L2_Buffer::V4L2_Buffer() { 9 | clear(); 10 | } 11 | 12 | V4L2_Buffer::~V4L2_Buffer() { 13 | clear(); 14 | } 15 | 16 | void V4L2_Buffer::clear() { 17 | // Note that the user needs to free "start" 18 | start = NULL; 19 | length = 0; 20 | } 21 | 22 | /* V4L2_Device */ 23 | /* -------------------------------------- */ 24 | 25 | V4L2_Device::V4L2_Device() { 26 | clear(); 27 | } 28 | 29 | V4L2_Device::~V4L2_Device() { 30 | clear(); 31 | } 32 | 33 | void V4L2_Device::clear() { 34 | 35 | path.clear(); 36 | id_vendor.clear(); 37 | id_product.clear(); 38 | driver.clear(); 39 | card.clear(); 40 | bus_info.clear(); 41 | version_major = CA_NONE; 42 | version_minor = CA_NONE; 43 | version_micro = CA_NONE; 44 | } 45 | 46 | std::string V4L2_Device::toString() { 47 | 48 | std::stringstream ss; 49 | 50 | ss << "idVendor: " << id_vendor << ", " 51 | << "idProduct: " << id_product << ", " 52 | << "driver: " << driver << ", " 53 | << "card: " << card << ", " 54 | << "bufinfo: " << bus_info << ", " 55 | << "version: " << version_major << "." << version_minor << "." << version_micro << ", " 56 | << "path: " << path; 57 | 58 | 59 | std::string result = ss.str(); 60 | return result; 61 | } 62 | }; 63 | -------------------------------------------------------------------------------- /include/videocapture/win/MediaFoundation_Utils.h: -------------------------------------------------------------------------------- 1 | #ifndef VIDEO_CAPTURE_MEDIA_FOUNDATION_UTILS_H 2 | #define VIDEO_CAPTURE_MEDIA_FOUNDATION_UTILS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include /* e.g. MFEnumDeviceSources */ 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | namespace ca { 18 | 19 | std::string media_foundation_video_format_to_string(const GUID& guid); /* Convert a MF format to a string */ 20 | int media_foundation_video_format_to_capture_format(GUID guid); /* Convert a MF format to a capture format */ 21 | 22 | // Convert a WCHAR to a std::string 23 | template 24 | T string_cast( const wchar_t* src, unsigned int codePage = CP_ACP) { 25 | 26 | assert(src != 0); 27 | size_t source_length = std::wcslen(src); 28 | 29 | if(source_length > 0) { 30 | 31 | int length = ::WideCharToMultiByte(codePage, 0, src, (int)source_length, NULL, 0, NULL, NULL); 32 | if(length == 0) { 33 | return T(); 34 | } 35 | 36 | std::vector buffer( length ); 37 | ::WideCharToMultiByte(codePage, 0, src, (int)source_length, &buffer[0], length, NULL, NULL); 38 | 39 | return T(buffer.begin(), buffer.end()); 40 | } 41 | else { 42 | return T(); 43 | } 44 | } 45 | 46 | } // namespace ca 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /src/decklink_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace ca; 8 | 9 | static void on_signal(int s); 10 | static void on_frame(void* pixels, int nbytes, void* user); 11 | 12 | bool must_run = true; 13 | 14 | int main() { 15 | 16 | printf("\n\nDecklink Example\n\n"); 17 | 18 | signal(SIGINT, on_signal); 19 | 20 | Settings cfg; 21 | cfg.device = 0; 22 | cfg.capability = 15; 23 | 24 | ca::Decklink dl(on_frame, NULL); 25 | dl.listDevices(); 26 | dl.getCapabilities(0); 27 | 28 | if (dl.open(cfg) < 0) { 29 | printf("Error: cannot open the decklink device.\n"); 30 | exit(EXIT_FAILURE); 31 | }; 32 | 33 | if (dl.start() < 0) { 34 | printf("Error: failed to start capturing from the device.\n"); 35 | exit(EXIT_FAILURE); 36 | } 37 | 38 | while (must_run) { 39 | dl.update(); 40 | } 41 | 42 | if (dl.stop() < 0) { 43 | printf("Error: something went wrong when trying to stop the capture.\n"); 44 | } 45 | 46 | if (dl.close() < 0) { 47 | printf("Error: something went wrong when trying to close the capture device.\n"); 48 | } 49 | 50 | return 0; 51 | } 52 | 53 | static void on_signal(int s) { 54 | static int called = 0; 55 | printf("Verbose: received a signal.\n"); 56 | 57 | must_run = false; 58 | 59 | if (called == 1) { 60 | exit(1); 61 | } 62 | ++called; 63 | } 64 | 65 | static void on_frame(void* pixels, int nbytes, void* user) { 66 | printf("Received a frame of %d bytes.\n", nbytes); 67 | } 68 | -------------------------------------------------------------------------------- /include/videocapture/win/MediaFoundation_Callback.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Asynchronous Source Reader 4 | --------------------------- 5 | See http://msdn.microsoft.com/en-us/library/windows/desktop/gg583871(v=vs.85).aspx for more information. 6 | 7 | */ 8 | #ifndef VIDEO_CAPTURE_MEDIA_FOUNDATION_CALLBACK_H 9 | #define VIDEO_CAPTURE_MEDIA_FOUNDATION_CALLBACK_H 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | namespace ca { 17 | 18 | class MediaFoundation_Capture; 19 | 20 | class MediaFoundation_Callback : public IMFSourceReaderCallback { 21 | public: 22 | static bool createInstance(MediaFoundation_Capture* cap, MediaFoundation_Callback** cb); 23 | 24 | STDMETHODIMP QueryInterface(REFIID iid, void** v); 25 | STDMETHODIMP_(ULONG) AddRef(); 26 | STDMETHODIMP_(ULONG) Release(); 27 | 28 | STDMETHODIMP OnReadSample(HRESULT hr, DWORD streamIndex, DWORD streamFlags, LONGLONG timestamp, IMFSample* sample); 29 | STDMETHODIMP OnEvent(DWORD streamIndex, IMFMediaEvent* event); 30 | STDMETHODIMP OnFlush(DWORD streamIndex); 31 | 32 | HRESULT Wait(DWORD* streamFlags, LONGLONG* timestamp, IMFSample* sample) { return S_OK; } 33 | HRESULT Cancel() { return S_OK; } 34 | 35 | private: 36 | MediaFoundation_Callback(MediaFoundation_Capture* cap); 37 | virtual ~MediaFoundation_Callback(); 38 | 39 | private: 40 | MediaFoundation_Capture* cap; 41 | long ref_count; 42 | CRITICAL_SECTION crit_sec; 43 | }; 44 | 45 | } // namespace ca 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /include/videocapture/decklink/DecklinkCallback.h: -------------------------------------------------------------------------------- 1 | #ifndef VIDEO_CAPTURE_DECKLINK_CALLBACK_H 2 | #define VIDEO_CAPTURE_DECKLINK_CALLBACK_H 3 | 4 | #include 5 | #include 6 | 7 | namespace ca { 8 | 9 | class DecklinkCallback : public IDeckLinkInputCallback { 10 | public: 11 | DecklinkCallback(); 12 | ~DecklinkCallback(); 13 | void setCallback(frame_callback cb, void* user); 14 | 15 | /* IUnknown */ 16 | virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; } 17 | virtual ULONG STDMETHODCALLTYPE AddRef(void) { return 1; } 18 | virtual ULONG STDMETHODCALLTYPE Release(void) { return 1; } 19 | 20 | /* IDecklinkInputCallback */ 21 | virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags); 22 | virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*); 23 | 24 | frame_callback fc; /* Will be called when we received a video frame (may happen from a different thread. */ 25 | void* user; /* Is passed into the frame callback. */ 26 | }; 27 | 28 | /* ------------------------------------------------------------------------- */ 29 | 30 | inline void DecklinkCallback::setCallback(frame_callback cb, void* usr) { 31 | 32 | if(NULL == cb) { 33 | printf("Error: trying to set a callback but a NULL callback is given.\n"); 34 | return; 35 | } 36 | 37 | fc = cb; 38 | user = usr; 39 | } 40 | 41 | } /* namespace ca */ 42 | 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /include/videocapture/decklink/DecklinkDevice.h: -------------------------------------------------------------------------------- 1 | #ifndef VIDEO_CAPTURE_DECKLINK_DEVICE_H 2 | #define VIDEO_CAPTURE_DECKLINK_DEVICE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace ca { 12 | 13 | class DecklinkDevice { 14 | public: 15 | DecklinkDevice(IDeckLink* device); 16 | ~DecklinkDevice(); 17 | 18 | int open(Settings settings); 19 | int close(); 20 | int start(); 21 | int stop(); 22 | void update(); 23 | void setCallback(frame_callback cb, void* user); 24 | 25 | public: 26 | IDeckLink* device; 27 | IDeckLinkInput* input; 28 | DecklinkCallback* callback; 29 | 30 | BMDDisplayMode display_mode; /* The display mode we're going to use, is set in DecklinkDevice::open(). */ 31 | BMDPixelFormat pixel_format; /* The pixel format we're going to use, is set in DecklinkDevice::open(). */ 32 | bool is_started; /* Is set to true in start(), and false in stop(). Just a safety check. */ 33 | 34 | frame_callback fc; /* Will be called when we received a video frame (may happen from a different thread. */ 35 | void* user; /* Is passed into the frame callback. */ 36 | }; 37 | 38 | /* ------------------------------------------------------------------------- */ 39 | 40 | inline void DecklinkDevice::setCallback(frame_callback cb, void* usr) { 41 | 42 | if(NULL == cb) { 43 | printf("Error: trying to set a callback but a NULL callback is given.\n"); 44 | return; 45 | } 46 | 47 | fc = cb; 48 | user = usr; 49 | } 50 | 51 | } /* namespace ca */ 52 | 53 | #endif 54 | 55 | 56 | -------------------------------------------------------------------------------- /src/videocapture/mac/AVFoundation_Capture.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace ca { 5 | 6 | AVFoundation_Capture::AVFoundation_Capture(frame_callback fc, void* user) 7 | :Base(fc, user) 8 | ,cap(NULL) 9 | { 10 | cap = ca_av_alloc(); 11 | if(!cap) { 12 | printf("Error: cannot allocate the AVImplemenation.\n"); 13 | ::exit(EXIT_FAILURE); 14 | } 15 | 16 | ca_av_set_callback(cap, fc, user); 17 | } 18 | 19 | AVFoundation_Capture::~AVFoundation_Capture() { 20 | 21 | close(); 22 | 23 | if(cap) { 24 | ca_av_dealloc(cap); 25 | cap = NULL; 26 | } 27 | } 28 | 29 | int AVFoundation_Capture::open(Settings settings) { 30 | return ca_av_open(cap, settings); 31 | } 32 | 33 | int AVFoundation_Capture::close() { 34 | return ca_av_close(cap); 35 | } 36 | 37 | int AVFoundation_Capture::start() { 38 | return ca_av_start(cap); 39 | } 40 | 41 | int AVFoundation_Capture::stop() { 42 | return ca_av_stop(cap); 43 | } 44 | 45 | void AVFoundation_Capture::update() { 46 | } 47 | 48 | std::vector AVFoundation_Capture::getCapabilities(int device) { 49 | std::vector caps; 50 | ca_av_get_capabilities(cap, device, caps); 51 | return caps; 52 | } 53 | 54 | std::vector AVFoundation_Capture::getDevices() { 55 | std::vector result; 56 | ca_av_get_devices(cap, result); 57 | return result; 58 | } 59 | 60 | std::vector AVFoundation_Capture::getOutputFormats() { 61 | std::vector result; 62 | ca_av_get_output_formats(cap, result); 63 | return result; 64 | } 65 | /* 66 | int AVFoundation_Capture::getOutputFormat() { 67 | return ca_av_get_output_format(cap); 68 | } 69 | */ 70 | 71 | }; // namespace ca 72 | -------------------------------------------------------------------------------- /include/videocapture/mac/AVFoundation_Interface.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | C-Interface for the AVFoundation webcam grabber. 4 | 5 | */ 6 | #ifndef VIDEO_CAPTURE_AV_FOUNDATION_INTERFACE_H 7 | #define VIDEO_CAPTURE_AV_FOUNDATION_INTERFACE_H 8 | 9 | #include 10 | #include 11 | 12 | void* ca_av_alloc(); /* Allocate the AVFoundation implementation. */ 13 | void ca_av_dealloc(void* cap); /* Deallocate the AVFoundation implementation */ 14 | int ca_av_get_devices(void* cap, std::vector& result); /* Get available devices. */ 15 | int ca_av_get_capabilities(void* cap, int device, std::vector& result); /* Get capabilities. */ 16 | int ca_av_get_output_formats(void* cap, std::vector& result); /* The AVFoundation frameworks allows conversion of the raw input samples it gets from a webcam into a couple of other output formats. This is not supported by all implementations though. This function returns the formats which are supported. */ 17 | //int ca_av_get_output_format(void* cap); /* Get the output format of the pixel buffers that are passed into the callback. */ 18 | int ca_av_open(void* cap, ca::Settings settings); /* Opens the given capture device with the given settings object. */ 19 | int ca_av_close(void* cap); /* Closes and shuts down the capture device and session. */ 20 | int ca_av_start(void* cap); /* Start the capture process. */ 21 | int ca_av_stop(void* cap); /* Stop the capture process. */ 22 | void ca_av_set_callback(void* cap, ca::frame_callback fc, void* user); /* Set the callback function which will receive the frames. */ 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /src/videocapture/decklink/DecklinkCallback.cpp: -------------------------------------------------------------------------------- 1 | #if defined(__linux) 2 | # define __STDC_LIMIT_MACROS 3 | #endif 4 | #include 5 | #include 6 | #include 7 | 8 | namespace ca { 9 | 10 | DecklinkCallback::DecklinkCallback() 11 | :fc(NULL) 12 | ,user(NULL) 13 | { 14 | } 15 | 16 | DecklinkCallback::~DecklinkCallback() { 17 | fc = NULL; 18 | user = NULL; 19 | } 20 | 21 | HRESULT STDMETHODCALLTYPE DecklinkCallback::VideoInputFormatChanged(BMDVideoInputFormatChangedEvents ev, 22 | IDeckLinkDisplayMode* mode, 23 | BMDDetectedVideoInputFormatFlags falgs) 24 | { 25 | return S_OK; 26 | } 27 | 28 | HRESULT STDMETHODCALLTYPE DecklinkCallback::VideoInputFrameArrived(IDeckLinkVideoInputFrame* vframe, 29 | IDeckLinkAudioInputPacket* aframe) 30 | { 31 | HRESULT r = S_OK; 32 | char* pix = NULL; 33 | long nbytes = 0; 34 | 35 | #if !defined(NDEBUG) 36 | 37 | if (NULL == fc) { 38 | printf("Error: DecklinkCallback::VideoInputFrameArrived(): no callback set. Stopping.\n"); 39 | ::exit(EXIT_FAILURE); 40 | } 41 | 42 | if (NULL == vframe) { 43 | printf("Error: DecklinkCallback::VideoInputFrameArrived(): vframe is NULL. Stopping.\n"); 44 | ::exit(EXIT_FAILURE); 45 | } 46 | 47 | #endif 48 | 49 | nbytes = vframe->GetRowBytes() * vframe->GetHeight(); 50 | 51 | #if !defined(NDEBUG) 52 | if (INT32_MAX < nbytes) { 53 | printf("Error: then number of bytes in the current video frame is to large to hold in a int. This is not supposed to happen.\n"); 54 | ::exit(EXIT_FAILURE); 55 | } 56 | #endif 57 | 58 | r = vframe->GetBytes((void**)&pix); 59 | 60 | if (S_OK != r) { 61 | printf("Error: failed to get bytes in the decklink callback.\n"); 62 | } 63 | else { 64 | fc(pix, (int)nbytes, user); 65 | } 66 | 67 | return S_OK; 68 | } 69 | 70 | } /* namespace ca */ 71 | -------------------------------------------------------------------------------- /src/videocapture/Utils.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace ca { 4 | 5 | int fps_from_rational(uint64_t num, uint64_t den) { 6 | 7 | char buf[128] = { 0 } ; 8 | double r = 1.0 / ( double(num) / double(den) ); 9 | float fps = 0.0f; 10 | 11 | sprintf(buf, "%2.02f", r); 12 | sscanf(buf, "%f", &fps); 13 | int v = (fps * 100); 14 | 15 | switch(v) { 16 | case CA_FPS_60_00: return CA_FPS_60_00; 17 | case CA_FPS_59_94: return CA_FPS_59_94; 18 | case CA_FPS_50_00: return CA_FPS_50_00; 19 | case CA_FPS_30_00: return CA_FPS_30_00; 20 | case CA_FPS_29_97: return CA_FPS_29_97; 21 | case CA_FPS_27_50: return CA_FPS_27_50; 22 | case CA_FPS_25_00: return CA_FPS_25_00; 23 | case CA_FPS_24_00: return CA_FPS_24_00; 24 | case CA_FPS_23_98: return CA_FPS_23_98; 25 | case CA_FPS_22_50: return CA_FPS_22_50; 26 | case CA_FPS_20_00: return CA_FPS_20_00; 27 | case CA_FPS_17_50: return CA_FPS_17_50; 28 | case CA_FPS_15_00: return CA_FPS_15_00; 29 | case CA_FPS_12_50: return CA_FPS_12_50; 30 | case CA_FPS_10_00: return CA_FPS_10_00; 31 | case CA_FPS_7_50: return CA_FPS_7_50; 32 | case CA_FPS_5_00: return CA_FPS_5_00; 33 | case CA_FPS_2_00: return CA_FPS_2_00; 34 | default: return CA_NONE; 35 | } 36 | } 37 | 38 | std::string format_to_string(int fmt) { 39 | switch(fmt) { 40 | case CA_UYVY422: return "CA_UYVY422"; 41 | case CA_YUYV422: return "CA_YUYV422"; 42 | case CA_YUV422P: return "CA_YUV422P"; 43 | case CA_YUV420P: return "CA_YUV420P"; 44 | case CA_YUV420BP: return "CA_YUV420BP"; 45 | case CA_YUVJ420P: return "CA_YUVJ420P"; 46 | case CA_YUVJ420BP: return "CA_YUVJ420BP"; 47 | case CA_ARGB32: return "CA_ARGB32"; 48 | case CA_BGRA32: return "CA_BGRA32"; 49 | case CA_RGB24: return "CA_RGB24"; 50 | case CA_JPEG_OPENDML: return "CA_JPEG_OPENDML"; 51 | case CA_H264: return "CA_H264"; 52 | case CA_MJPEG: return "CA_MJPEG"; 53 | case CA_NONE: return "CA_NONE"; 54 | default: return "UNKNOWN_FORMAT"; 55 | } 56 | } 57 | 58 | } // namespace ca 59 | -------------------------------------------------------------------------------- /src/videocapture/linux/V4L2_Devices_Udev.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace ca { 5 | 6 | 7 | /* ---------------------------------------------------------------- */ 8 | 9 | /* 10 | This implementation uses udev to retrieve capture devices. 11 | We support both udev and default v4l2 ways to retrieve 12 | devices. Udev is not supported on all systems. 13 | 14 | */ 15 | std::vector v4l2_get_devices() { 16 | 17 | std::vector result; 18 | struct udev* udev; 19 | struct udev_enumerate* enumerate; 20 | struct udev_list_entry* devices; 21 | struct udev_list_entry* dev_list_entry; 22 | struct udev_device* dev; 23 | 24 | udev = udev_new(); 25 | if(!udev) { 26 | printf("Error: Cannot udev_new()\n"); 27 | return result; 28 | } 29 | 30 | enumerate = udev_enumerate_new(udev); 31 | udev_enumerate_add_match_subsystem(enumerate, "video4linux"); 32 | udev_enumerate_scan_devices(enumerate); 33 | devices = udev_enumerate_get_list_entry(enumerate); 34 | 35 | udev_list_entry_foreach(dev_list_entry, devices) { 36 | 37 | /* Get the device by syspath. */ 38 | const char* syspath = udev_list_entry_get_name(dev_list_entry); 39 | dev = udev_device_new_from_syspath(udev, syspath); 40 | 41 | if(!dev) { 42 | printf("Error: cannot get the device using the syspath: %s\n", syspath); 43 | continue; 44 | } 45 | 46 | V4L2_Device v4l2_device; 47 | v4l2_device.path = udev_device_get_devnode(dev); 48 | 49 | if(v4l2_device.path.size() == 0) { 50 | printf("Error: Cannot find devpath.\n"); 51 | continue; 52 | } 53 | 54 | dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device"); 55 | 56 | if(!dev) { 57 | printf("Error:Cannot find related usb device.\n"); 58 | continue; 59 | } 60 | 61 | v4l2_device.id_vendor = udev_device_get_sysattr_value(dev, "idVendor"); 62 | v4l2_device.id_product = udev_device_get_sysattr_value(dev, "idProduct"); 63 | 64 | result.push_back(v4l2_device); 65 | } 66 | 67 | udev_enumerate_unref(enumerate); 68 | udev_unref(udev); 69 | 70 | return result; 71 | } 72 | 73 | /* ---------------------------------------------------------------- */ 74 | 75 | } /* namespace ca */ 76 | -------------------------------------------------------------------------------- /include/videocapture/Capture.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Cross Platform Interface 4 | ------------------------ 5 | 6 | This class can be used to read back frames, cross plaftorm, 7 | from a capture device. By default we will select the media capture 8 | SDK for the current OS. Though you can specify what capture wrapper 9 | you want to use by passing one of the valid capture drivers to the 10 | constructor, see the list with supported drives in Types.h 11 | 12 | This class is a thin wrapper around the driver.. 13 | 14 | */ 15 | 16 | #ifndef VIDEO_CAPTURE_VIDEOCAPTURE_H 17 | #define VIDEO_CAPTURE_VIDEOCAPTURE_H 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #if defined(__APPLE__) 25 | # include 26 | #elif defined(__linux) 27 | # include 28 | #elif defined(_WIN32) 29 | # include 30 | #endif 31 | 32 | #if defined(USE_DECKLINK) 33 | # include 34 | #endif 35 | 36 | namespace ca { 37 | 38 | class Capture { 39 | public: 40 | Capture(frame_callback fc, void* user, int driver = CA_DEFAULT_DRIVER); 41 | ~Capture(); 42 | 43 | /* Interface */ 44 | int open(Settings settings); 45 | int close(); 46 | int start(); 47 | int stop(); 48 | void update(); 49 | 50 | /* Capabilities */ 51 | std::vector getCapabilities(int device); 52 | std::vector getDevices(); 53 | std::vector getOutputFormats(); 54 | int hasOutputFormat(int format); 55 | 56 | /* Info */ 57 | int listDevices(); 58 | int listCapabilities(int device); 59 | int listOutputFormats(); 60 | int findCapability(int device, int width, int height, int fmt); 61 | int findCapability(int device, int width, int height, int* fmt, int nfmts); /* Test several different capture formats. */ 62 | int findCapability(int device, std::vector caps); /* Test the given capabilities in order and return the best one available. It wil the first found capability or -1 when none was found. */ 63 | 64 | public: 65 | Base* cap; /* The capture implementation */ 66 | }; 67 | 68 | } /* namespace ca */ 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /src/videocapture/Base.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace ca { 4 | 5 | Base::Base(frame_callback fc, void* user) 6 | :cb_frame(fc) 7 | ,cb_user(user) 8 | { 9 | } 10 | 11 | Base::~Base() { 12 | } 13 | 14 | // List all devices 15 | int Base::listDevices() { 16 | 17 | std::vector devices = getDevices(); 18 | if(devices.size() == 0) { 19 | printf("No devices found"); 20 | return -1; 21 | } 22 | 23 | for(size_t i = 0; i < devices.size(); ++i) { 24 | printf("[%d] %s\n", devices[i].index, devices[i].name.c_str()); 25 | } 26 | 27 | return (int)devices.size(); 28 | } 29 | 30 | // List the capabilities for the given device. 31 | int Base::listCapabilities(int device) { 32 | 33 | std::vector caps = getCapabilities(device); 34 | if(caps.size() == 0) { 35 | return -1; 36 | } 37 | 38 | for(size_t i = 0; i < caps.size(); ++i) { 39 | Capability& cb = caps[i]; 40 | 41 | printf("[%02d] %d x %d @ %2.02f, %s", 42 | cb.capability_index, 43 | cb.width, 44 | cb.height, 45 | float(cb.fps/100.0f), 46 | format_to_string(cb.pixel_format).c_str() 47 | ); 48 | 49 | if (cb.description.size() > 0) { 50 | printf(", %s", cb.description.c_str()); 51 | } 52 | 53 | printf("\n"); 54 | } 55 | return (int)caps.size(); 56 | } 57 | 58 | // List output formats. 59 | int Base::listOutputFormats() { 60 | 61 | std::vector ofmts = getOutputFormats(); 62 | if(ofmts.size() == 0) { 63 | return -1; 64 | } 65 | 66 | for(size_t i = 0; i < ofmts.size(); ++i) { 67 | printf("[%d] %s\n", ofmts[i].index, format_to_string(ofmts[i].format).c_str()); 68 | } 69 | 70 | return (int)ofmts.size(); 71 | } 72 | 73 | // Find a capability 74 | int Base::findCapability(int device, int width, int height, int fmt) { 75 | 76 | int fps = -1; 77 | int result = -1; 78 | std::vector caps = getCapabilities(device); 79 | 80 | for(size_t i = 0; i < caps.size(); ++i) { 81 | Capability& cap = caps[i]; 82 | if(cap.width == width 83 | && cap.height == height 84 | && cap.pixel_format == fmt 85 | && cap.fps > fps 86 | ) 87 | { 88 | result = i; 89 | fps = cap.fps; 90 | } 91 | } 92 | 93 | return result; 94 | } 95 | 96 | }; // namespace ca 97 | -------------------------------------------------------------------------------- /src/test_conversion.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Test conversion 4 | =============== 5 | 6 | Some SDKs support automatic conversion of compressed input streams to 7 | uncompressed output streams. For example the Logitech C920 camera 8 | has support for JPEG and H264. By setting another value then `CA_NONE` 9 | we can use the SDK to convert/uncompress the incoming data. 10 | 11 | This code was written and tested on Mac using AVFoundation using a 12 | Logitech C920 webcam. 13 | 14 | */ 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | using namespace ca; 22 | 23 | static void fcallback(PixelBuffer& buffer); 24 | 25 | int main() { 26 | 27 | int r = 0; 28 | int formats[] = { CA_JPEG_OPENDML } ; 29 | int width = 1280; 30 | int height = 720; 31 | 32 | Settings cfg; 33 | cfg.device = 1; 34 | cfg.capability = 0; 35 | cfg.format = CA_UYVY422; 36 | 37 | Capture cap(fcallback, NULL); 38 | 39 | /* List the devices and output formats. */ 40 | cap.listDevices(); 41 | cap.listOutputFormats(); 42 | 43 | /* Check if there the capability is supported. */ 44 | cfg.capability = cap.findCapability(cfg.device, width, height, formats, 1); 45 | if (cfg.capability < 0) { 46 | cap.listCapabilities(cfg.device); 47 | printf("Error: failed to find the capability.\n"); 48 | return 1; 49 | } 50 | 51 | printf("We found a capability for %d x %d, capability: %d\n", width, height, cfg.capability); 52 | 53 | /* Open the capture device. */ 54 | r = cap.open(cfg); 55 | if (r < 0) { 56 | cap.listCapabilities(cfg.device); 57 | printf("Error: cannot open the device: %d\n", r); 58 | return 1; 59 | } 60 | 61 | /* Start capturing. */ 62 | if (cap.start() < 0) { 63 | cap.listCapabilities(cfg.device); 64 | printf("Error: failed to start capturing.\n"); 65 | return 1; 66 | } 67 | 68 | /* And start iterating. */ 69 | while (true) { 70 | cap.update(); 71 | SLEEP_MILLIS(5000); 72 | } 73 | 74 | return 0; 75 | } 76 | 77 | 78 | static void fcallback(PixelBuffer& buffer) { 79 | printf("Got a pixel buffer, size: %lu.\n", buffer.nbytes); 80 | 81 | #if 0 82 | static int count = 0; 83 | static char fname[1024]; 84 | 85 | sprintf(fname, "test_%04d.jpg", count); 86 | 87 | std::ofstream ofs(fname, std::ios::out | std::ios::binary); 88 | ofs.write((char*)buffer.plane[0], buffer.nbytes); 89 | ofs.close(); 90 | 91 | count++; 92 | #endif 93 | } 94 | -------------------------------------------------------------------------------- /docs/source/gettingstarted.rst: -------------------------------------------------------------------------------- 1 | *************** 2 | Getting Started 3 | *************** 4 | 5 | .. highlight:: c++ 6 | 7 | To compile Video Capture you need to do: 8 | 9 | - Make sure that you installed all dependencies 10 | - Clone the Video Capture repository from github 11 | - Compile using the build script 12 | 13 | Building the library 14 | ==================== 15 | 16 | Video Capture primary location is Github. To get the code clone the project:: 17 | 18 | git clone git@github.com:roxlu/video_capture.git 19 | 20 | 21 | Dependencies 22 | ------------ 23 | 24 | Video Capture main development systems are Mac OS 10.9, Windows 8.1 and 25 | Arch Linux. On Linux we use the Video4Linux API, on Mac we use AVFoundation 26 | which are both part of the OS. On Windows you need to download the latest 27 | Windows SDK which provides the MediaFoundation libraries. We use CMake_ to 28 | compile the library and examples. The Video Capture library contains an OpenGL 29 | example. For this OpenGL example we depend on libglfw_ 3. 30 | 31 | .. _libglfw: http://www.glfw.org 32 | 33 | 34 | Compiling Video Capture on Mac and Linux 35 | ---------------------------------------- 36 | 37 | For Mac and Linux systems we use the same compile script. To compile 38 | follow these steps. 39 | 40 | :: 41 | 42 | cd build 43 | ./release.sh 44 | 45 | Compiling Video Capture on Windows 46 | ---------------------------------- 47 | 48 | On windows we use CMake too with a build script. Development uses 49 | Microsoft Visual Studio 2012 Express. To compile on Windows follow these 50 | steps. 51 | 52 | :: 53 | 54 | cd build 55 | build.bat 64 release 56 | 57 | 58 | .. _CMake: http://www.cmake.org 59 | 60 | 61 | Compiling programs that use Video Capture 62 | ========================================= 63 | 64 | To compile a program that uses Video Capture make sure to link with the 65 | created `libvideocapture.a` on Mac and Linux and the `libvideocapture.lib` 66 | file on Windows. The library is installed in the `install` directory that 67 | we create when you use the above describe build steps. 68 | 69 | Also make sure to add a header search path to the headers that we also 70 | install into the `install` directory. 71 | 72 | Libraries to link with on Linux 73 | ------------------------------- 74 | 75 | - udev 76 | 77 | Libraries to link with on Mac 78 | ----------------------------- 79 | 80 | - CoreFoundation Framework 81 | - AVFoundation framework 82 | - Cocoa 83 | - CoreVideo 84 | - CoreMedia 85 | 86 | 87 | Libraries to link with on Windows 88 | --------------------------------- 89 | 90 | - Mfplat.lib 91 | - Mf.lib 92 | - Mfuuid.lib 93 | - Mfreadwrite.lib 94 | - Shlwapi.lib 95 | 96 | -------------------------------------------------------------------------------- /src/test_capability_filter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Capability Filtering 4 | -------------------- 5 | 6 | Often you don't know what webcam device will be used though you do want a 7 | specific pixel format, width, height or resolution. By using the `CapabilityFinder` 8 | class you can specify what attributes you find most important and filter on that. 9 | Possible filters are: 10 | 11 | - CA_WIDTH 12 | - CA_HEIGHT 13 | - CA_RATIO 14 | - CA_PIXEL_FORMAT 15 | 16 | You add these filters to the `CapabilityFinder` instance and then use e.g. 17 | `findSettingsForFormat()` which returns 0 on success and it will set the 18 | given `Settings` parameter correctly. This also means that it will set 19 | the `Settings.format` parameter which is used to convert from a capture-input 20 | format to a output format the the specific capture SDK supports. On Mac for 21 | example, when you use a Logitech C920 webcam, it can only capture at 1280x720 22 | using the CA_JPEG_OPENDML format that the camera seems to provide. Though 23 | often it's handy to use a YUV format in your application; Mac supports automatic 24 | conversion from CA_JPEG_OPENDML to CA_YUYV422. In this case we set the format 25 | member of the given `Settings` parameter in `findSettingsForFormat()`. 26 | 27 | Quick Tip! 28 | ---------- 29 | 30 | Define your filters in groups. For example if the pixel format is most important 31 | define it's values in a certain range which doesn't overlap with other filters. 32 | For examnple, choose 80-100 as the priority value for the pixel format values 33 | 60-79 for the width, 40-59 for the height, etc.. 34 | 35 | */ 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | using namespace ca; 43 | 44 | static void fcallback(PixelBuffer& buffer); 45 | 46 | int main() { 47 | 48 | printf("\nCapability Filtering Example.\n\n"); 49 | 50 | int device = 1; 51 | Capture cap(fcallback, NULL); 52 | 53 | CapabilityFinder finder(cap); 54 | finder.addFilter(CA_PIXEL_FORMAT, CA_YUYV422, 100); 55 | finder.addFilter(CA_PIXEL_FORMAT, CA_UYVY422, 100); 56 | finder.addFilter(CA_PIXEL_FORMAT, CA_JPEG_OPENDML, 100); 57 | finder.addFilter(CA_WIDTH, 1280, 95); 58 | finder.addFilter(CA_HEIGHT, 720, 95); 59 | finder.addFilter(CA_WIDTH, 800, 90); 60 | finder.addFilter(CA_HEIGHT, 600, 90); 61 | finder.addFilter(CA_WIDTH, 640, 80); 62 | finder.addFilter(CA_HEIGHT, 480, 80); 63 | 64 | Settings settings_format; 65 | if (0 == finder.findSettingsForFormat(device, CA_UYVY422, settings_format)) { 66 | printf("Best matching for format, capability: %d, format: %s\n", settings_format.capability, format_to_string(settings_format.format).c_str()); 67 | } 68 | 69 | return 0; 70 | } 71 | 72 | 73 | static void fcallback(PixelBuffer& buffer) { 74 | printf("Received a frame with format: %s\n", format_to_string(buffer.pixel_format).c_str()); 75 | } 76 | -------------------------------------------------------------------------------- /include/videocapture/CapabilityFinder.h: -------------------------------------------------------------------------------- 1 | /* -*-c++-*- */ 2 | /* 3 | 4 | Capability Filtering 5 | -------------------- 6 | 7 | Often you don't know what webcam/capture device will be used though you do want a 8 | specific pixel format, width, height or resolution. By using the `CapabilityFinder` 9 | class you can specify what attributes you find most important and filter on that. 10 | Possible filters are: 11 | 12 | - CA_WIDTH 13 | - CA_HEIGHT 14 | - CA_RATIO 15 | - CA_PIXEL_FORMAT 16 | 17 | You add these filters to the `CapabilityFinder` instance and then use e.g. 18 | `findSettingsForFormat()` which returns 0 on success and it will set the 19 | given `Settings` parameter correctly. This also means that it will set 20 | the `Settings.format` parameter which is used to convert from a capture-input 21 | format to a output format the the specific capture SDK supports if necesary. On 22 | Mac for example, when you use a Logitech C920 webcam, it can only capture at 1280x720 23 | using the CA_JPEG_OPENDML format that the camera seems to provide (atm). Though 24 | often it's handy to use a YUV format in your application; Mac supports automatic 25 | conversion from CA_JPEG_OPENDML to CA_YUYV422. In this case we set the format 26 | member of the given `Settings` parameter in `findSettingsForFormat()`. 27 | 28 | Quick Tip! 29 | ---------- 30 | 31 | Define your filters in groups. For example if the pixel format is most important 32 | define it's values in a certain range which doesn't overlap with other filters. 33 | For examnple, choose 80-100 as the priority value for the pixel format values 34 | 60-79 for the width, 40-59 for the height, etc.. 35 | 36 | Example 37 | ------- 38 | See `src/test_capability_filter.cpp`. 39 | 40 | */ 41 | #ifndef VIDEO_CAPTURE_CAPABILITY_FINDER_H 42 | #define VIDEO_CAPTURE_CAPABILITY_FINDER_H 43 | 44 | #include 45 | #include 46 | #include 47 | 48 | namespace ca { 49 | 50 | class CapabilityFinder { 51 | public: 52 | CapabilityFinder(Capture& capture); /* Initialize and pass the Capture reference that we use to retrieve capabilities from a device. */ 53 | ~CapabilityFinder(); /* Cleans up the added filters. */ 54 | int addFilter(int attribute, double value, int priority); /* Add a filter, see the info at the top of this document for more info. */ 55 | int findSettingsForFormat(int device, int format, Settings& result); /* Will return the best matching Settings (if found) based on the added filters. */ 56 | std::vector filterCapabilities(int device); /* Returns an vector of capabilities that matches any of the added filters. */ 57 | 58 | public: 59 | Capture& cap; /* Reference to the capture instance. */ 60 | std::vector filters; /* The added filters. */ 61 | }; 62 | 63 | } /* namespace ca */ 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /include/videocapture/Base.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Base Video Capture 4 | ------------------ 5 | 6 | This base video capture class is an interface for platform specific implementations. 7 | Capabilities describe the features of a capture device, like width/height/fps/pixel_format. 8 | Output Formats describe any build in pixel/codec conversion methods a SDK/OS has (listed with fastest first). 9 | 10 | */ 11 | 12 | #ifndef VIDEO_CAPTURE_BASE_H 13 | #define VIDEO_CAPTURE_BASE_H 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | namespace ca { 22 | 23 | class Base { 24 | public: 25 | Base(frame_callback fc, void* user); /* Constructor, set the frame callback function which receives the given user pointer. */ 26 | virtual ~Base(); /* Destructor. */ 27 | virtual int open(Settings cfg) = 0; /* Open the device for the given settings object (with device, capability and output format). */ 28 | virtual int close() = 0; /* Close the opened device. */ 29 | virtual int start() = 0; /* Start captureing from the device. */ 30 | virtual int stop() = 0; /* Stop captureing from the device. */ 31 | virtual void update() = 0; /* Update, some implementations need to regurlarly update. Call this at the set FPS speed. */ 32 | virtual std::vector getCapabilities(int device) = 0; /* Retrieve a list with capabilities. */ 33 | virtual std::vector getDevices() = 0; /* Retrieve a list with devices. */ 34 | virtual std::vector getOutputFormats() = 0; /* Some capture SDKs have support for automatic conversion of the raw data it receives from capture devices to more common output values like YUV. */ 35 | // virtual int getOutputFormat() = 0; /* This function should return the capture format that is used and by the capture SDK. This should be the final output format that is used. e.g. on Mac you can automotically convert from JPEG to a YUV* format, this function should return the YUV format. */ 36 | 37 | int listDevices(); /* List the available capture devices for the implementation and return the number of found devices. */ 38 | int listCapabilities(int device); /* List the available capabilities for the given device. */ 39 | int listOutputFormats(); /* List the available output formats the at SDK of the OS/.. supports. On mac these are the output formats of the AVCaptureVideoDataOutput */ 40 | int findCapability(int device, int width, int height, int fmt); /* Get the best matching capability for the given format and dimensions. We return the capability index or -1 if not found. */ 41 | 42 | public: 43 | frame_callback cb_frame; /* The frame callback. */ 44 | void* cb_user; /* The user pointer that is passed into the frame callback. */ 45 | }; 46 | 47 | }; // namespace ca 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /src/videocapture/linux/V4L2_Devices_Default.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include /* readlink */ 6 | #include /* memcmp */ 7 | #include /* open */ 8 | #include /* open */ 9 | #include /* ioctl */ 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | namespace ca { 16 | 17 | /* 18 | 19 | This file uses a "linux" standard way to retrieve capture 20 | devices. Most of this code is based on v4l2-ctl which also 21 | retrieves a list of devices. 22 | 23 | This implementation can be used when udev is not available. 24 | 25 | */ 26 | 27 | /* ---------------------------------------------------------------- */ 28 | 29 | static bool is_v4l2_dev(const char* name); 30 | 31 | /* ---------------------------------------------------------------- */ 32 | 33 | std::vector v4l2_get_devices() { 34 | 35 | std::vector result; 36 | struct dirent* ep = NULL; 37 | struct v4l2_capability vcap; 38 | std::map links; 39 | std::map cards; 40 | std::vector files; 41 | DIR* dp = NULL; 42 | 43 | /* Get /dev/video# devices. */ 44 | dp = opendir("/dev"); 45 | if (NULL == dp) { 46 | printf("Failed to open the /dev directory. Cannot list devices.\n"); 47 | return result; 48 | } 49 | 50 | while (NULL != (ep = readdir(dp))) { 51 | if (true == is_v4l2_dev(ep->d_name)) { 52 | files.push_back(std::string("/dev/") +ep->d_name); 53 | } 54 | } 55 | 56 | if (0 != closedir(dp)) { 57 | printf("Failed to close the /dev dir handle.\n"); 58 | } 59 | 60 | dp = NULL; 61 | 62 | /* Iterate over the found devices. */ 63 | std::vector::iterator it = files.begin(); 64 | while (it != files.end()) { 65 | 66 | char link[64+1]; 67 | int link_len; 68 | std::string target; 69 | std::string& filename = *it; 70 | 71 | link_len = readlink(filename.c_str(), link, 64); 72 | if (link_len < 0) { 73 | ++it; 74 | continue; 75 | } 76 | 77 | link[link_len] = '\0'; 78 | 79 | if (link[0] != '/') { 80 | target = std::string("/dev/"); 81 | } 82 | 83 | target += link; 84 | 85 | if (std::find(files.begin(), files.end(), target) == files.end()) { 86 | printf("File not found: %s\n", filename.c_str()); 87 | ++it; 88 | continue; 89 | } 90 | 91 | if (links[target].empty()) { 92 | links[target] = filename; 93 | } 94 | else { 95 | links[target] += ", " +filename; 96 | } 97 | 98 | files.erase(it); 99 | } 100 | 101 | for (size_t i = 0; i < files.size(); ++i) { 102 | V4L2_Device device; 103 | device.path = files[i]; 104 | result.push_back(device); 105 | } 106 | 107 | return result; 108 | } 109 | 110 | /* ---------------------------------------------------------------- */ 111 | 112 | static bool is_v4l2_dev(const char* name) { 113 | 114 | if (NULL == name) { 115 | return false; 116 | } 117 | 118 | return (false == memcmp(name, "video", 5)); 119 | } 120 | 121 | } /* namespace ca */ 122 | -------------------------------------------------------------------------------- /src/api_example.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | VideoCapture 4 | ------------- 5 | 6 | This example shows a minimal example on how to list 7 | the capture devices, list capabilities and output formats. 8 | 9 | */ 10 | #include 11 | 12 | #if defined(__APPLE__) || defined(__linux) 13 | # include /* usleep */ 14 | #elif defined(_WIN32) 15 | # define WIN32_LEAN_AND_MEAN 16 | # include 17 | #endif 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #define WRITE_RAW_FILE 0 25 | 26 | #if WRITE_RAW_FILE 27 | bool wrote_frame = false; 28 | std::fstream outfile; 29 | #endif 30 | 31 | using namespace ca; 32 | 33 | bool must_run = true; 34 | 35 | void fcallback(PixelBuffer& buffer); 36 | void sig_handler(int sig); 37 | 38 | int main() { 39 | printf("\nVideoCapture\n"); 40 | 41 | signal(SIGINT, sig_handler); 42 | 43 | #if WRITE_RAW_FILE 44 | outfile.open("generated.raw", std::ios::binary | std::ios::out); 45 | if (!outfile.is_open()) { 46 | printf("Error: failed to open `generated.raw`.\n"); 47 | exit(1); 48 | } 49 | #endif 50 | 51 | int width = 640; 52 | int height = 480; 53 | 54 | width = 1280; 55 | height = 720; 56 | 57 | Settings cfg; 58 | cfg.device = 0; 59 | cfg.capability = 0; 60 | cfg.format = CA_NONE; 61 | 62 | Capture cap(fcallback, NULL); // , CA_DECKLINK); 63 | cap.listDevices(); 64 | 65 | printf("\nOutput formats:\n"); 66 | cap.listOutputFormats(); 67 | cap.listCapabilities(cfg.device); 68 | 69 | std::vector caps; 70 | caps.push_back(Capability(width, height, CA_YUYV422)); 71 | caps.push_back(Capability(width, height, CA_UYVY422)); 72 | caps.push_back(Capability(width, height, CA_JPEG_OPENDML)); 73 | 74 | //caps.push_back(Capability(width, height, CA_YUV420P)); 75 | cfg.capability = cap.findCapability(cfg.device, caps); 76 | if (cfg.capability > 0) { 77 | printf("Found capability: %d\n", cfg.capability); 78 | } 79 | else { 80 | printf("Could not find any of the given capabilities.\n"); 81 | cap.listCapabilities(cfg.device); 82 | } 83 | 84 | int fmts[] = { CA_YUYV422, CA_UYVY422, CA_YUV420P, CA_JPEG_OPENDML }; 85 | cfg.capability = cap.findCapability(cfg.device, width, height, fmts, 4); 86 | if (!cfg.capability) { 87 | printf("Error: tried CA_YUYV422 and CA_UYVY formats; both didn't work."); 88 | ::exit(EXIT_FAILURE); 89 | } 90 | 91 | if(cap.open(cfg) < 0) { 92 | printf("Error: cannot open the device.\n"); 93 | ::exit(EXIT_FAILURE); 94 | } 95 | 96 | if(cap.start() < 0) { 97 | printf("Error: cannot start capture.\n"); 98 | ::exit(EXIT_FAILURE); 99 | } 100 | 101 | while(must_run == true) { 102 | cap.update(); 103 | #if defined(_WIN32) 104 | Sleep(5); 105 | #else 106 | usleep(5 * 1000); 107 | #endif 108 | } 109 | 110 | if(cap.stop() < 0) { 111 | printf("Error: cannot stop.\n"); 112 | } 113 | if(cap.close() < 0) { 114 | printf("Error: cannot close.\n"); 115 | } 116 | 117 | #if WRITE_RAW_FILE 118 | outfile.flush(); 119 | outfile.close(); 120 | printf("Info: Wrote raw YUV file.\n"); 121 | #endif 122 | 123 | return EXIT_SUCCESS; 124 | } 125 | 126 | void fcallback(PixelBuffer& buffer) { 127 | 128 | printf("Frame callback: %lu bytes, stride: %lu \n", buffer.nbytes, buffer.stride[0]); 129 | 130 | #if WRITE_RAW_FILE 131 | if (false == wrote_frame) { 132 | outfile.write((const char*)buffer.plane[0], buffer.nbytes); 133 | wrote_frame = true; 134 | } 135 | #endif 136 | } 137 | 138 | void sig_handler(int sig) { 139 | printf("Handle signal.\n"); 140 | must_run = false; 141 | } 142 | -------------------------------------------------------------------------------- /src/videocapture/win/MediaFoundation_Callback.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | namespace ca { 8 | 9 | bool MediaFoundation_Callback::createInstance(MediaFoundation_Capture* cap, MediaFoundation_Callback** cb) { 10 | 11 | if(cb == NULL) { 12 | printf("Error: the given MediaFoundation_Capture is invalid; cant create an instance.\n"); 13 | return false; 14 | } 15 | 16 | MediaFoundation_Callback* media_cb = new MediaFoundation_Callback(cap); 17 | if(!media_cb) { 18 | printf("Error: cannot allocate a MediaFoundation_Callback object - out of memory\n"); 19 | return false; 20 | } 21 | 22 | *cb = media_cb; 23 | (*cb)->AddRef(); 24 | 25 | safeReleaseMediaFoundation(&media_cb); 26 | return true; 27 | } 28 | 29 | MediaFoundation_Callback::MediaFoundation_Callback(MediaFoundation_Capture* cap) 30 | :ref_count(1) 31 | ,cap(cap) 32 | { 33 | InitializeCriticalSection(&crit_sec); 34 | } 35 | 36 | MediaFoundation_Callback::~MediaFoundation_Callback() { 37 | } 38 | 39 | HRESULT MediaFoundation_Callback::QueryInterface(REFIID iid, void** v) { 40 | static const QITAB qit[] = { 41 | QITABENT(MediaFoundation_Callback, IMFSourceReaderCallback), { 0 }, 42 | }; 43 | return QISearch(this, qit, iid, v); 44 | } 45 | 46 | ULONG MediaFoundation_Callback::AddRef() { 47 | return InterlockedIncrement(&ref_count); 48 | } 49 | 50 | ULONG MediaFoundation_Callback::Release() { 51 | ULONG ucount = InterlockedDecrement(&ref_count); 52 | if(ucount == 0) { 53 | delete this; 54 | } 55 | return ucount; 56 | } 57 | 58 | HRESULT MediaFoundation_Callback::OnReadSample(HRESULT hr, DWORD streamIndex, DWORD streamFlags, LONGLONG timestamp, IMFSample* sample) { 59 | assert(cap); 60 | assert(cap->imf_source_reader); 61 | assert(cap->cb_frame); 62 | 63 | EnterCriticalSection(&crit_sec); 64 | 65 | if(SUCCEEDED(hr) && sample) { 66 | 67 | IMFMediaBuffer* buffer; 68 | HRESULT hr = S_OK; 69 | DWORD count = 0; 70 | sample->GetBufferCount(&count); 71 | 72 | for(DWORD i = 0; i < count; ++i) { 73 | 74 | hr = sample->GetBufferByIndex(i, &buffer); 75 | 76 | 77 | if(SUCCEEDED(hr)) { 78 | 79 | DWORD length = 0; 80 | DWORD max_length = 0; 81 | BYTE* data = NULL; 82 | buffer->Lock(&data, &max_length, &length); 83 | 84 | cap->pixel_buffer.nbytes = (size_t)length; 85 | cap->pixel_buffer.plane[0] = data; 86 | cap->pixel_buffer.plane[1] = data + cap->pixel_buffer.offset[1]; 87 | cap->pixel_buffer.plane[2] = data + cap->pixel_buffer.offset[2]; 88 | cap->cb_frame(cap->pixel_buffer); 89 | 90 | buffer->Unlock(); 91 | buffer->Release(); 92 | } 93 | } 94 | } 95 | 96 | if(SUCCEEDED(hr)) { 97 | if(cap->imf_source_reader && cap->state & CA_STATE_CAPTUREING) { 98 | hr = cap->imf_source_reader->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, NULL, NULL, NULL, NULL); 99 | if(FAILED(hr)) { 100 | printf("Error: while trying to read the next sample.\n"); 101 | } 102 | } 103 | } 104 | 105 | LeaveCriticalSection(&crit_sec); 106 | return S_OK; 107 | } 108 | 109 | HRESULT MediaFoundation_Callback::OnEvent(DWORD, IMFMediaEvent* event) { 110 | return S_OK; 111 | } 112 | 113 | HRESULT MediaFoundation_Callback::OnFlush(DWORD) { 114 | return S_OK; 115 | } 116 | 117 | } // namespace ca 118 | -------------------------------------------------------------------------------- /src/videocapture/Capture.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace ca { 5 | 6 | Capture::Capture(frame_callback fc, void* user, int driver) 7 | :cap(NULL) 8 | { 9 | 10 | #if defined(__APPLE__) 11 | if(cap == NULL && driver == CA_AV_FOUNDATION) { 12 | cap = new AVFoundation_Capture(fc, user); 13 | } 14 | #endif 15 | 16 | #if defined(__linux) 17 | if(cap == NULL && driver == CA_V4L2) { 18 | cap = new V4L2_Capture(fc, user); 19 | } 20 | #endif 21 | 22 | #if defined(_WIN32) 23 | if(cap == NULL && driver == CA_MEDIA_FOUNDATION) { 24 | cap = new MediaFoundation_Capture(fc, user); 25 | } 26 | #endif 27 | 28 | #if defined(USE_DECKLINK) 29 | if (cap != NULL && driver == CA_DECKLINK) { 30 | printf("Error: cap is already initialized but the driver is CA_DECKLINK! Not supposed to happen.\n"); 31 | ::exit(EXIT_FAILURE); 32 | } 33 | 34 | if (driver == CA_DECKLINK) { 35 | cap = new Decklink(fc, user); 36 | } 37 | #endif 38 | 39 | if(cap == NULL) { 40 | printf("Error: no valid capture driver found.\n"); 41 | ::exit(EXIT_FAILURE); 42 | } 43 | } 44 | 45 | Capture::~Capture() { 46 | 47 | if(cap != NULL) { 48 | delete cap; 49 | cap = NULL; 50 | } 51 | } 52 | 53 | int Capture::open(Settings settings) { 54 | assert(cap != NULL); 55 | return cap->open(settings); 56 | } 57 | 58 | int Capture::close() { 59 | assert(cap != NULL); 60 | return cap->close(); 61 | } 62 | 63 | int Capture::start() { 64 | assert(cap != NULL); 65 | return cap->start(); 66 | } 67 | 68 | int Capture::stop() { 69 | assert(cap != NULL); 70 | return cap->stop(); 71 | } 72 | 73 | void Capture::update() { 74 | assert(cap != NULL); 75 | cap->update(); 76 | } 77 | 78 | std::vector Capture::getCapabilities(int device) { 79 | assert(cap != NULL); 80 | return cap->getCapabilities(device); 81 | } 82 | 83 | std::vector Capture::getDevices() { 84 | assert(cap != NULL); 85 | return cap->getDevices(); 86 | } 87 | 88 | std::vector Capture::getOutputFormats() { 89 | assert(cap != NULL); 90 | return cap->getOutputFormats(); 91 | } 92 | 93 | int Capture::hasOutputFormat(int format) { 94 | assert(cap != NULL); 95 | 96 | std::vector formats = getOutputFormats(); 97 | 98 | for (size_t i = 0; i < formats.size(); ++i) { 99 | if (formats[i].format == format) { 100 | return 0; 101 | } 102 | } 103 | 104 | return -1; 105 | } 106 | 107 | int Capture::listDevices() { 108 | assert(cap != NULL); 109 | return cap->listDevices(); 110 | } 111 | 112 | int Capture::listCapabilities(int device) { 113 | assert(cap != NULL); 114 | return cap->listCapabilities(device); 115 | } 116 | 117 | int Capture::listOutputFormats() { 118 | assert(cap != NULL); 119 | return cap->listOutputFormats(); 120 | } 121 | 122 | int Capture::findCapability(int device, int width, int height, int fmt) { 123 | assert(cap != NULL); 124 | return cap->findCapability(device, width, height, fmt); 125 | } 126 | 127 | int Capture::findCapability(int device, std::vector caps) { 128 | int capid = -1; 129 | for (size_t i = 0; i < caps.size(); ++i) { 130 | Capability& check = caps[i]; 131 | printf("%d, %d\n", check.width, check.height); 132 | capid = cap->findCapability(device, check.width, check.height, check.pixel_format); 133 | if (capid >= 0) { 134 | break; 135 | } 136 | } 137 | return capid; 138 | } 139 | 140 | int Capture::findCapability(int device, int width, int height, int* fmts, int nfmts) { 141 | int capid = -1; 142 | for (int i = 0; i < nfmts; ++i) { 143 | capid = cap->findCapability(device, width, height, fmts[i]); 144 | if (capid >= 0) { 145 | return capid; 146 | } 147 | } 148 | return -1; 149 | } 150 | 151 | /* ------------------------------------------------------------------------- */ 152 | 153 | } // namespace ca 154 | -------------------------------------------------------------------------------- /src/test_v4l2_devices.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Video 4 Linux Device Lists 4 | 5 | This was a basic test to get human readable names of capture 6 | devices using V4L2. We have support for udev, though udev is 7 | not available on a couple of major districtions. Therefore when 8 | udev is not found we want use the features implemented here. 9 | 10 | */ 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include /* readlink */ 19 | #include /* memcmp */ 20 | #include /* open */ 21 | #include /* open */ 22 | #include /* ioctl */ 23 | #include 24 | #include 25 | #include 26 | 27 | 28 | static int list_devices(); 29 | static bool is_v4l2_dev(const char* name); 30 | 31 | int main() { 32 | 33 | printf("\n\ntest_v4l2_devices.\n\n"); 34 | 35 | list_devices(); 36 | 37 | return 0; 38 | } 39 | 40 | 41 | static int list_devices() { 42 | 43 | printf("\nlist_devices.\n"); 44 | 45 | struct dirent* ep = NULL; 46 | struct v4l2_capability vcap; 47 | std::map links; 48 | std::map cards; 49 | std::vector files; 50 | DIR* dp = NULL; 51 | 52 | /* Get /dev/video# devices. */ 53 | dp = opendir("/dev"); 54 | if (NULL == dp) { 55 | printf("Failed to open the /dev directory. Cannot list devices.\n"); 56 | return -1; 57 | } 58 | 59 | while (NULL != (ep = readdir(dp))) { 60 | if (true == is_v4l2_dev(ep->d_name)) { 61 | files.push_back(std::string("/dev/") +ep->d_name); 62 | } 63 | } 64 | 65 | if (0 != closedir(dp)) { 66 | printf("Failed to close the /dev dir handle.\n"); 67 | } 68 | 69 | dp = NULL; 70 | 71 | /* Iterate over the found devices. */ 72 | std::vector::iterator it = files.begin(); 73 | while (it != files.end()) { 74 | 75 | char link[64+1]; 76 | int link_len; 77 | std::string target; 78 | std::string& filename = *it; 79 | 80 | link_len = readlink(filename.c_str(), link, 64); 81 | if (link_len < 0) { 82 | printf("Warning: failed to open the device: %s\n", filename.c_str()); 83 | ++it; 84 | continue; 85 | } 86 | 87 | link[link_len] = '\0'; 88 | 89 | if (link[0] != '/') { 90 | target = std::string("/dev/"); 91 | } 92 | 93 | target += link; 94 | 95 | if (std::find(files.begin(), files.end(), target) == files.end()) { 96 | printf("File not found: %s\n", filename.c_str()); 97 | ++it; 98 | continue; 99 | } 100 | 101 | if (links[target].empty()) { 102 | links[target] = filename; 103 | } 104 | else { 105 | links[target] += ", " +filename; 106 | } 107 | 108 | files.erase(it); 109 | } 110 | 111 | /* Get readable descriptions. */ 112 | for (size_t i = 0; i < files.size(); ++i) { 113 | 114 | std::string bus_info; 115 | std::string& filename = files[i]; 116 | 117 | int fd = open(filename.c_str(), O_RDWR); 118 | if (fd < 0) { 119 | printf("Failed to open: %s", filename.c_str()); 120 | continue; 121 | } 122 | 123 | ioctl(fd, VIDIOC_QUERYCAP, &vcap); 124 | close(fd); 125 | 126 | bus_info = (const char*) vcap.bus_info; 127 | 128 | if (true == cards[bus_info].empty()) { 129 | cards[bus_info] += std::string((char*)vcap.card) +" (" +bus_info +"):\n"; 130 | } 131 | 132 | cards[bus_info] += "\t" +filename; 133 | 134 | if (false == links[filename].empty()) { 135 | cards[bus_info] += " <-" +links[filename]; 136 | } 137 | 138 | cards[bus_info] += "\n"; 139 | } 140 | 141 | { 142 | std::map::iterator it = cards.begin(); 143 | while (it != cards.end()) { 144 | printf("%s\n", it->second.c_str()); 145 | ++it; 146 | } 147 | } 148 | 149 | return 0; 150 | } 151 | 152 | static bool is_v4l2_dev(const char* name) { 153 | 154 | if (NULL == name) { 155 | return false; 156 | } 157 | 158 | return (false == memcmp(name, "video", 5)); 159 | } 160 | -------------------------------------------------------------------------------- /include/videocapture/win/MediaFoundation_Capture.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | MediaFoundation_Capture 4 | ----------------------- 5 | 6 | Implements the Windows Media Foundation capture pipeline to read 7 | frames from capture devices newer Windows PCs (Win7, Win8). If you 8 | want to use this class make sure that you've installed the Windows 9 | SDK. 10 | 11 | - See: http://msdn.microsoft.com/en-us/library/windows/desktop/ms694197(v=vs.85).aspx 12 | - Dependencies: Win SDK 13 | 14 | */ 15 | #ifndef VIDEO_CAPTURE_MEDIA_FOUNDATION_H 16 | #define VIDEO_CAPTURE_MEDIA_FOUNDATION_H 17 | 18 | #include 19 | #include 20 | #include 21 | #include /* e.g. MFEnumDeviceSources */ 22 | #include 23 | #include /* MediaFoundation error codes, MF_E_* */ 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | namespace ca { 35 | 36 | class MediaFoundation_Capture : public Base { 37 | public: 38 | MediaFoundation_Capture(frame_callback fc, void* user); 39 | ~MediaFoundation_Capture(); 40 | 41 | /* Interface */ 42 | int open(Settings settings); 43 | int close(); 44 | int start(); 45 | int stop(); 46 | void update(); 47 | 48 | /* Capabilities */ 49 | std::vector getCapabilities(int device); /* Get all the capabilities for the given device number */ 50 | std::vector getDevices(); /* We query the udev USB devices. */ 51 | std::vector getOutputFormats(); /* Get the supported output formats. */ 52 | 53 | private: 54 | 55 | /* SDK Capabilities */ 56 | int createSourceReader(IMFMediaSource* mediaSource, IMFSourceReaderCallback* callback, IMFSourceReader** sourceReader); /* Create the IMFSourceReader which is basically an intermediate which allows us to process raw frames ourself. */ 57 | int createVideoDeviceSource(int device, IMFMediaSource** source); /* Creates a new IMFMediaSource object that is activated */ 58 | int getCapabilities(IMFMediaSource* source, std::vector& result); /* Get capabilities for the given source */ 59 | int setDeviceFormat(IMFMediaSource* source, DWORD formatIndex); /* Set the video capture format */ 60 | int setReaderFormat(IMFSourceReader* reader, Capability& cap); /* Set the format for the source reader. The source reader is used to process raw data, see: http://msdn.microsoft.com/en-us/library/windows/desktop/dd940436(v=vs.85).aspx for more information */ 61 | 62 | public: 63 | PixelBuffer pixel_buffer; /* This is the pixel buffer that is passed on to the frame callback. */ 64 | MediaFoundation_Callback* mf_callback; /* The MediaFoundation_Callback instance that will receive frame data */ 65 | IMFMediaSource* imf_media_source; /* The IMFMediaSource represents the capture device. */ 66 | IMFSourceReader* imf_source_reader; /* The IMFSourceReader is an intermediate that must be used to process raw video frames */ 67 | int state; /* Used to keep track of open/capture state */ 68 | bool must_shutdown_com; /* See the note about initializing COM more then once in the constructor. */ 69 | }; 70 | 71 | /* Safely release the given obj. */ 72 | template void safeReleaseMediaFoundation(T **t) { 73 | if(*t) { 74 | (*t)->Release(); 75 | *t = NULL; 76 | } 77 | } 78 | 79 | } // namespace cs 80 | #endif 81 | -------------------------------------------------------------------------------- /include/videocapture/linux/V4L2_Capture.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | V4L2_Capture 4 | ------------ 5 | 6 | Video4Linux2 Capture wrapper. 7 | 8 | */ 9 | #ifndef VIDEO_CAPTURE_V4L2_CAPTURE_H 10 | #define VIDEO_CAPTURE_V4L2_CAPTURE_H 11 | 12 | extern "C" { 13 | # include 14 | # include 15 | # include 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | # include 23 | # include 24 | # include 25 | } 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | namespace ca { 37 | 38 | int v4l2_ioctl(int fh, int request, void* arg); /* Wrapper around ioctl */ 39 | 40 | class V4L2_Capture : public Base { 41 | 42 | public: 43 | V4L2_Capture(frame_callback fc, void* user); 44 | ~V4L2_Capture(); 45 | 46 | /* Interface */ 47 | int open(Settings settings); /* Open the given device */ 48 | int close(); /* Close the previously opened device */ 49 | int start(); /* Start captureing */ 50 | int stop(); /* Stop captureing. */ 51 | void update(); /* This should be called at framerate; this will grab a new frame */ 52 | 53 | /* Capabilities */ 54 | std::vector getCapabilities(int device); /* Get all the capabilities for the given device number */ 55 | std::vector getDevices(); /* We query the udev USB devices. */ 56 | std::vector getOutputFormats(); /* Get the supported output formats. For V4L2 this is empty. */ 57 | 58 | /* IO Methods */ 59 | int initializeMMAP(int fd); /* Initialize MMAP I/O for the given file descriptor */ 60 | int shutdownMMAP(); /* Shutdown MMAP and free all buffers. */ 61 | int readFrame(); /* Reads one frame from the device */ 62 | 63 | /* Device related*/ 64 | int openDevice(std::string path); /* Open the device and return a descriptor; path is the device devpat.h */ 65 | int closeDevice(int fd); /* Close the given device descriptor; is use when opening/closing multiple devices to test e.g. capabilities; get info on the devices, etc.. */ 66 | int getDriverInfo(const char* path, V4L2_Device& result); /* Get extra driver info for the given syspath. */ 67 | int setCaptureFormat(int fd, int width, int height, int pixfmt); /* Set the pixel format for the given fd, widht, height and pixfmt. */ 68 | int getDeviceV4L2(int dx, V4L2_Device& result); /* Get the device for the given index */ 69 | int getCapabilityV4L2(int fd, struct v4l2_capability* caps); /* Get a v4l2_capability object for the given fd. */ 70 | 71 | private: 72 | int state; /* We keep track of the open/capture state so we know when to stop/close the device */ 73 | int capture_device_fd; /* File descriptor for the capture device. */ 74 | std::vector buffers; /* The buffer that are used to store the frames from the capture device . */ 75 | PixelBuffer pixel_buffer; /* The object we pass to the callback. */ 76 | }; 77 | }; // namespace ca 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /src/easy_opengl_example.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Easy OpenGL example 4 | ------------------- 5 | 6 | The capture library provides a wrapper for rendering frames 7 | using OpenGL. The wrapper tries to use one of the optimal YUV 8 | pixel formats. 9 | 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | 19 | #define ROXLU_USE_MATH 20 | #define ROXLU_USE_OPENGL 21 | #define ROXLU_IMPLEMENTATION 22 | #include 23 | 24 | #define VIDEO_CAPTURE_IMPLEMENTATION 25 | #include 26 | using namespace ca; 27 | 28 | // Capture 29 | void on_signal(int sig); /* Signal handler, to correctly shutdown the capture process */ 30 | 31 | // GLFW callbacks 32 | void button_callback(GLFWwindow* win, int bt, int action, int mods); 33 | void cursor_callback(GLFWwindow* win, double x, double y); 34 | void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods); 35 | void char_callback(GLFWwindow* win, unsigned int key); 36 | void error_callback(int err, const char* desc); 37 | void resize_callback(GLFWwindow* window, int width, int height); 38 | 39 | int main() { 40 | 41 | signal(SIGINT, on_signal); 42 | 43 | glfwSetErrorCallback(error_callback); 44 | 45 | if(!glfwInit()) { 46 | printf("Error: cannot setup glfw.\n"); 47 | return false; 48 | } 49 | 50 | glfwWindowHint(GLFW_SAMPLES, 4); 51 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 52 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); 53 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 54 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 55 | 56 | GLFWwindow* win = NULL; 57 | int w = 1280; 58 | int h = 720; 59 | 60 | win = glfwCreateWindow(w, h, "Capture", NULL, NULL); 61 | if(!win) { 62 | glfwTerminate(); 63 | exit(EXIT_FAILURE); 64 | } 65 | 66 | glfwSetFramebufferSizeCallback(win, resize_callback); 67 | glfwSetKeyCallback(win, key_callback); 68 | glfwSetCharCallback(win, char_callback); 69 | glfwSetCursorPosCallback(win, cursor_callback); 70 | glfwSetMouseButtonCallback(win, button_callback); 71 | glfwMakeContextCurrent(win); 72 | glfwSwapInterval(1); 73 | 74 | if (!gladLoadGL()) { 75 | printf("Cannot load GL.\n"); 76 | exit(EXIT_FAILURE); 77 | } 78 | 79 | // ---------------------------------------------------------------- 80 | // THIS IS WHERE YOU START CALLING OPENGL FUNCTIONS, NOT EARLIER!! 81 | // ---------------------------------------------------------------- 82 | 83 | CaptureGL capture; 84 | int device = 0; 85 | 86 | capture.cap.listDevices(); 87 | capture.cap.listCapabilities(device); 88 | 89 | if (capture.open(device, 640, 480) < 0) { 90 | printf("Cannot open the capture device.\n"); 91 | ::exit(EXIT_FAILURE); 92 | } 93 | 94 | if(capture.start() < 0) { 95 | ::exit(EXIT_FAILURE); 96 | } 97 | 98 | while(!glfwWindowShouldClose(win)) { 99 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 100 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 101 | 102 | capture.update(); 103 | capture.draw(); 104 | capture.draw(10, 10, 320, 240); 105 | 106 | glfwSwapBuffers(win); 107 | glfwPollEvents(); 108 | } 109 | 110 | glfwTerminate(); 111 | 112 | return EXIT_SUCCESS; 113 | } 114 | 115 | void char_callback(GLFWwindow* win, unsigned int key) { 116 | } 117 | 118 | void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods) { 119 | 120 | if(action != GLFW_PRESS) { 121 | return; 122 | } 123 | 124 | if(action == GLFW_PRESS) { 125 | } 126 | else if(action == GLFW_RELEASE) { 127 | } 128 | 129 | switch(key) { 130 | case GLFW_KEY_SPACE: { 131 | break; 132 | } 133 | case GLFW_KEY_ESCAPE: { 134 | glfwSetWindowShouldClose(win, GL_TRUE); 135 | break; 136 | } 137 | }; 138 | } 139 | 140 | void resize_callback(GLFWwindow* window, int width, int height) { 141 | } 142 | 143 | void cursor_callback(GLFWwindow* win, double x, double y) { 144 | } 145 | 146 | void button_callback(GLFWwindow* win, int bt, int action, int mods) { 147 | 148 | double x,y; 149 | 150 | if(action == GLFW_PRESS || action == GLFW_REPEAT) { 151 | glfwGetCursorPos(win, &x, &y); 152 | } 153 | 154 | if(action == GLFW_PRESS) { 155 | } 156 | else if(action == GLFW_RELEASE) { 157 | } 158 | } 159 | 160 | void error_callback(int err, const char* desc) { 161 | printf("GLFW error: %s (%d)\n", desc, err); 162 | } 163 | 164 | void on_signal(int sig) { 165 | ::exit(EXIT_FAILURE); 166 | } 167 | -------------------------------------------------------------------------------- /src/videocapture/CapabilityFinder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace ca { 4 | 5 | /* ------------------------------------------------------------------------- */ 6 | 7 | static bool sort_capability(const Capability& a, const Capability& b); /* Used when filtering capabilities. This will sort the capabilites on the filter_score. */ 8 | 9 | /* ------------------------------------------------------------------------- */ 10 | 11 | CapabilityFinder::CapabilityFinder(Capture& cap) 12 | :cap(cap) 13 | { 14 | } 15 | 16 | CapabilityFinder::~CapabilityFinder() { 17 | filters.clear(); 18 | } 19 | 20 | int CapabilityFinder::addFilter(int attribute, double value, int priority) { 21 | 22 | if (CA_WIDTH != attribute 23 | && CA_HEIGHT != attribute 24 | && CA_RATIO != attribute 25 | && CA_PIXEL_FORMAT != attribute) 26 | { 27 | printf("Error: invalid attribute.\n"); 28 | return -1; 29 | } 30 | 31 | filters.push_back(CapabilityFilter(attribute, value, priority)); 32 | 33 | return 0; 34 | } 35 | 36 | int CapabilityFinder::findSettingsForFormat(int device, int format, Settings& result) { 37 | 38 | result.device = device; 39 | 40 | if (0 == filters.size()) { 41 | printf("Error: cannot get the best capability because you haven't added any filters.\n"); 42 | return -1; 43 | } 44 | 45 | std::vector capabilities = filterCapabilities(device); 46 | if (0 == capabilities.size()) { 47 | printf("Error: cannot find any capability which conforms the set filters.\n"); 48 | return -2; 49 | } 50 | 51 | Capability best_capability = capabilities[0]; 52 | if (0 > best_capability.index) { 53 | printf("Error: the index value < 0; not supposed to happen.\n"); 54 | return -3; 55 | } 56 | 57 | /* Check if the found capability uses the requested format, and if not check if we can convert it*/ 58 | if (best_capability.pixel_format != format) { 59 | if (0 == cap.hasOutputFormat(format)) { 60 | result.format = format; 61 | } 62 | else { 63 | return -4; 64 | } 65 | } 66 | 67 | result.capability = best_capability.index; 68 | 69 | return 0; 70 | } 71 | 72 | std::vector CapabilityFinder::filterCapabilities(int device) { 73 | 74 | float ratio = 0.0; 75 | std::vector result; 76 | std::vector capabilities = cap.getCapabilities(device); 77 | 78 | if (0 == filters.size()) { 79 | return result; 80 | } 81 | 82 | if (0 == capabilities.size()) { 83 | return result; 84 | } 85 | 86 | for (size_t i = 0; i < filters.size(); ++i) { 87 | 88 | CapabilityFilter& filter = filters[i]; 89 | 90 | for (size_t j = 0; j < capabilities.size(); ++j) { 91 | 92 | Capability& capability = capabilities[j]; 93 | capability.index = j; 94 | 95 | switch(filter.attribute) { 96 | 97 | case CA_WIDTH: { 98 | if ((int)filter.value == capability.width) { 99 | capability.filter_score += filter.priority; 100 | } 101 | break; 102 | } 103 | 104 | case CA_HEIGHT: { 105 | if ((int)filter.value == capability.height) { 106 | capability.filter_score += filter.priority; 107 | } 108 | break; 109 | } 110 | 111 | case CA_RATIO: { 112 | 113 | if (0 == capability.width || 0 == capability.height) { 114 | printf("Error: the capability is missing a value for it's width and/or height: %d x %d.\n", capability.width, capability.height); 115 | continue; 116 | } 117 | 118 | ratio = double(capability.width) / capability.height; 119 | if (filter.value == ratio) { 120 | capability.filter_score += filter.priority; 121 | } 122 | 123 | break; 124 | } 125 | 126 | case CA_PIXEL_FORMAT: { 127 | if ((int)filter.value == capability.pixel_format) { 128 | capability.filter_score += filter.priority; 129 | } 130 | break; 131 | } 132 | default: { 133 | printf("Unhandled capability filter attribute: %d\n", filter.attribute); 134 | break; 135 | } 136 | } 137 | } 138 | } 139 | 140 | std::sort(capabilities.begin(), capabilities.end(), sort_capability); 141 | 142 | for (size_t i = 0; i < capabilities.size(); ++i) { 143 | 144 | Capability& capability = capabilities[i]; 145 | if (capability.filter_score == 0) { 146 | continue; 147 | } 148 | 149 | result.push_back(capability); 150 | } 151 | 152 | return result; 153 | } 154 | 155 | /* ------------------------------------------------------------------------- */ 156 | 157 | static bool sort_capability(const Capability& a, const Capability& b) { 158 | 159 | if (a.filter_score != b.filter_score) { 160 | return a.filter_score > b.filter_score; 161 | } 162 | 163 | return a.fps > b.fps; 164 | } 165 | 166 | 167 | } /* namespace ca */ 168 | 169 | -------------------------------------------------------------------------------- /src/videocapture/Types.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace ca { 4 | 5 | /* PIXELBUFFER */ 6 | /* -------------------------------------- */ 7 | PixelBuffer::PixelBuffer() { 8 | pixels = NULL; 9 | nbytes = 0; 10 | stride[0] = 0; 11 | stride[1] = 0; 12 | stride[2] = 0; 13 | plane[0] = NULL; 14 | plane[1] = NULL; 15 | plane[2] = NULL; 16 | width[0] = 0; 17 | width[1] = 0; 18 | width[2] = 0; 19 | height[0] = 0; 20 | height[1] = 0; 21 | height[2] = 0; 22 | offset[0] = 0; 23 | offset[1] = 0; 24 | offset[2] = 0; 25 | pixel_format = CA_NONE; 26 | user = NULL; 27 | } 28 | 29 | int PixelBuffer::setup(int w, int h, int fmt) { 30 | 31 | if (0 == w || 0 == h) { 32 | printf("error: cannot setup pixel buffer because w or h is 0.\n"); 33 | return -1; 34 | } 35 | 36 | pixel_format = fmt; 37 | width[0] = w; 38 | height[0] = h; 39 | 40 | switch (fmt) { 41 | 42 | case CA_YUV420P: { 43 | stride[0] = w; 44 | stride[1] = w / 2; 45 | stride[2] = w / 2; 46 | 47 | width[0] = w; 48 | width[1] = w / 2; 49 | width[2] = w / 2; 50 | 51 | height[0] = h; 52 | height[1] = h / 2; 53 | height[2] = h / 2; 54 | 55 | offset[0] = 0; 56 | offset[1] = (size_t)(w * h); 57 | offset[2] = (size_t)(offset[1] + (w / 2) * (h / 2)); 58 | 59 | break; 60 | } 61 | /* 62 | case CA_YUYV422: 63 | case CA_UYVY422: { 64 | stride[0] = w; 65 | stride[1] = w / 2; 66 | stride[2] = w / 2; 67 | break; 68 | } 69 | */ 70 | 71 | default: { 72 | printf("error: cannot setup the PixelBuffer for the given fmt: %d\n", fmt); 73 | return -2; 74 | } 75 | } 76 | 77 | return 0; 78 | } 79 | 80 | /* CAPABILITY */ 81 | /* -------------------------------------- */ 82 | Capability::Capability() { 83 | clear(); 84 | } 85 | 86 | Capability::Capability(int w, int h, int pixfmt) { 87 | clear(); 88 | width = w; 89 | height = h; 90 | pixel_format = pixfmt; 91 | } 92 | 93 | Capability::~Capability() { 94 | clear(); 95 | } 96 | 97 | void Capability::clear() { 98 | width = 0; 99 | height = 0; 100 | pixel_format = CA_NONE; 101 | fps = CA_NONE; 102 | capability_index = CA_NONE; 103 | fps_index = CA_NONE; 104 | pixel_format_index = CA_NONE; 105 | user = NULL; 106 | filter_score = 0; 107 | index = -1; 108 | } 109 | 110 | /* CAPABILITY FILTER */ 111 | /* -------------------------------------- */ 112 | CapabilityFilter::CapabilityFilter(int attribute, double value, int priority) 113 | :attribute(attribute) 114 | ,value(value) 115 | ,priority(priority) 116 | { 117 | 118 | } 119 | 120 | CapabilityFilter::~CapabilityFilter() { 121 | clear(); 122 | } 123 | 124 | void CapabilityFilter::clear() { 125 | attribute = CA_NONE; 126 | value = 0.0; 127 | priority = 0; 128 | } 129 | 130 | /* DEVICE */ 131 | /* -------------------------------------- */ 132 | Device::Device() { 133 | clear(); 134 | } 135 | 136 | Device::~Device() { 137 | clear(); 138 | } 139 | 140 | void Device::clear() { 141 | index = -1; 142 | name.clear(); 143 | } 144 | 145 | /* FORMAT */ 146 | /* -------------------------------------- */ 147 | Format::Format() { 148 | clear(); 149 | } 150 | 151 | Format::~Format() { 152 | clear(); 153 | } 154 | 155 | void Format::clear() { 156 | format = CA_NONE; 157 | index = CA_NONE; 158 | } 159 | 160 | /* Settings */ 161 | /* -------------------------------------- */ 162 | Settings::Settings() { 163 | clear(); 164 | } 165 | 166 | Settings::~Settings() { 167 | clear(); 168 | } 169 | 170 | void Settings::clear() { 171 | capability = CA_NONE; 172 | device = CA_NONE; 173 | format = CA_NONE; 174 | } 175 | 176 | /* Frame */ 177 | /* -------------------------------------- */ 178 | Frame::Frame() { 179 | clear(); 180 | } 181 | 182 | Frame::~Frame() { 183 | clear(); 184 | } 185 | 186 | void Frame::clear() { 187 | width.clear(); 188 | height.clear(); 189 | stride.clear(); 190 | nbytes.clear(); 191 | offset.clear(); 192 | } 193 | 194 | int Frame::set(int w, int h, int fmt) { 195 | 196 | clear(); 197 | 198 | if(fmt == CA_YUYV422) { 199 | width.push_back(w / 2); 200 | height.push_back(h); 201 | stride.push_back(w / 2); 202 | nbytes.push_back(w * h * 2); 203 | offset.push_back(0); 204 | return 1; 205 | } 206 | else if (fmt == CA_UYVY422) { 207 | width.push_back(w / 2); 208 | height.push_back(h); 209 | stride.push_back(w / 2); 210 | nbytes.push_back(w * h * 2); 211 | offset.push_back(0); 212 | return 1; 213 | } 214 | else if(fmt == CA_YUV420P) { 215 | 216 | // Y-channel 217 | width.push_back(w); 218 | height.push_back(h); 219 | stride.push_back(w); 220 | nbytes.push_back(w * h); 221 | offset.push_back(0); 222 | 223 | // Y-channel 224 | width.push_back(w / 2); 225 | height.push_back(h / 2); 226 | stride.push_back(w / 2); 227 | nbytes.push_back( (w / 2) * (h / 2) ); 228 | offset.push_back(nbytes[0]); 229 | 230 | // V-channel 231 | width.push_back(w / 2); 232 | height.push_back(h / 2); 233 | stride.push_back(w / 2); 234 | nbytes.push_back( (w / 2) * (h / 2) ); 235 | offset.push_back(nbytes[0] + nbytes[1]); 236 | return 1; 237 | } 238 | 239 | return -1; 240 | } 241 | 242 | 243 | } // namespace ca 244 | -------------------------------------------------------------------------------- /include/videocapture/mac/AVFoundation_Implementation.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | AVFoundation_Implementation 4 | ---------------- 5 | 6 | This class implements a video capturer for Mac using AVFoundation. The 7 | AVFoundation works by creating a session manager to which we add an input 8 | device. The input device contains a list of capabilities. These capabilities 9 | are represented using width/height/fps/pixel_format etc.. The VideoCapture library 10 | allows you to select a specific video capture capability. 11 | 12 | Though on Mac there is another layer we need to take of. Besides the input devices 13 | (the webcam) it has a class for output data. This output data class can convert the 14 | raw incoming frames into a different pixel format. For example, when the input data 15 | from the webcam are JPEG frames, then the output object (AVCaptureVideoDataOutput) 16 | can convert this into YUV422 for you. 17 | 18 | */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #import 25 | #import 26 | 27 | 28 | // resource: https://webrtc.googlecode.com/svn/trunk/webrtc/modules/video_capture/ios/video_capture_ios_objc.mm 29 | // resource: https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/AVFoundationPG.pdf 30 | 31 | @interface AVFoundation_Implementation : NSObject 32 | { 33 | AVCaptureSession* session; /* Manages the state of the input device. */ 34 | AVCaptureDeviceInput* input; /* Concrete instance of `AVDeviceInput`, represents the input device (webcam). */ 35 | AVCaptureVideoDataOutput* output; /* Concrete instance of `AVDeviceOutput`, used to get the video frames. */ 36 | int pixel_format; /* The pixel format in which we're capturing, is used in the callback to fill the PixelBuffer. This is a VideoCapture pixel format as defined in Types.h */ 37 | int is_pixel_buffer_set; /* Some information of the `pixel_buffer` member can only be set in the frame callback, but we don't want to set it every time we get a new frame, this flag is used for that. */ 38 | ca::PixelBuffer pixel_buffer; /* The pixel buffer that is filled/set and pass to the capture callback. */ 39 | ca::frame_callback cb_frame; /* Gets called when we receive a new frame. */ 40 | void* cb_user; /* User data that's will be passed into `cb_frame()` */ 41 | } 42 | 43 | - (id) init; /* Initialize the AVImplementation object. */ 44 | - (void) dealloc; /* Deallocate all objects that we retained. */ 45 | - (int) openDevice: (ca::Settings)settings; /* Opens the given device and sets the given capability. When settings.format is not CA_NONE, then we will ask the AVCaptureVideoDataOutput to use the given output (if supported). This will convert the data for you. */ 46 | - (int) closeDevice; /* Closes and shutsdown the currently opened device */ 47 | - (int) startCapture; /* Start the capture process on the opened device */ 48 | - (int) stopCapture; /* Stop the capture process */ 49 | - (void) setCallback: (ca::frame_callback) cb user: (void*) u; /* Set the callback function that will receive the frames */ 50 | - (int) getDevices: (std::vector&) result; /* Get a list with all the found capture devices. */ 51 | - (int) getCapabilities: (std::vector&) result forDevice: (int) dev; /* Get the capabilities for the given device. */ 52 | - (int) getOutputFormats: (std::vector&) result; /* Get the output formats into which we can automaticaly convert the raw frames we receive from the webcam */ 53 | //- (int) getOutputFormat; /* Get the output format that is used. The captured buffers are using this format. */ 54 | - (AVCaptureDevice*) getCaptureDevice: (int) device; /* Get a specific capture device (represents a webcam for example). */ 55 | - (int) getCapturePixelFormat: (CMPixelFormatType) ref; /* Converts the given CMPixelFormat to one which is used by the VideoCapture library */ 56 | - (void) printSupportedPixelFormatsByVideoDataOutput: (AVCaptureVideoDataOutput*) o; /* Used for debugging. Shows a list of supported PixelFormats by the output object */ 57 | - (std::string) cvPixelFormatToString: (NSNumber*) fmt; /* Used for debugging. Converts the given pixel format to a string. */ 58 | - (int) cvPixelFormatToCaptureFormat: (NSNumber*) fmt; /* Converts the given pixel format, from the AVCaptureVideoDataOutput.availableVideoCVPixelFormatTypes, into the values we need to he video capture library */ 59 | - (int) captureFormatToCvPixelFormat: (int) fmt; /* Converts one of the CA_RGB, CA_YUV* etc.. formats to one of the kCMPixelFormat* types */ 60 | - (NSString* const) widthHeightToCaptureSessionPreset: (int) w andHeight: (int) h; /* Get the best matching session preset we set to the session manager */ 61 | - (void) captureOutput: (AVCaptureOutput*) captureOutput didOutputSampleBuffer: (CMSampleBufferRef) sampleBuffer fromConnection: (AVCaptureConnection*) connection; /* Capture callback implementation */ 62 | 63 | @end 64 | -------------------------------------------------------------------------------- /src/videocapture/decklink/DecklinkDevice.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | namespace ca { 6 | 7 | DecklinkDevice::DecklinkDevice(IDeckLink* device) 8 | :device(device) 9 | ,input(NULL) 10 | ,callback(NULL) 11 | ,display_mode(bmdModeUnknown) 12 | ,is_started(false) 13 | ,fc(NULL) 14 | ,user(NULL) 15 | { 16 | 17 | if (NULL == device) { 18 | printf("Error: the IDeckLink device pointer is NULL. Not supposed to happen. Stopping now.\n"); 19 | exit(EXIT_FAILURE); 20 | } 21 | 22 | device->AddRef(); 23 | } 24 | 25 | DecklinkDevice::~DecklinkDevice() { 26 | 27 | if (is_started) { 28 | stop(); 29 | } 30 | 31 | if (NULL != device) { 32 | device->Release(); 33 | } 34 | 35 | if (NULL != input) { 36 | input->Release(); 37 | } 38 | 39 | if (NULL != callback) { 40 | delete callback; 41 | } 42 | 43 | callback = NULL; 44 | device = NULL; 45 | input = NULL; 46 | is_started = false; 47 | fc = NULL; 48 | user = NULL; 49 | } 50 | 51 | int DecklinkDevice::open(Settings cfg) { 52 | 53 | IDeckLinkDisplayModeIterator* mode_iter = NULL; 54 | IDeckLinkDisplayMode* mode = NULL; 55 | HRESULT r = S_OK; 56 | int i = 0; 57 | 58 | if (NULL == device) { 59 | printf("Error: cannot open DecklinkDevice, because the given/set IDeckLink* is NULL.\n"); 60 | return -1; 61 | } 62 | 63 | if (NULL != input) { 64 | printf("Error: the `input` member is not NULL, did you already open the device?\n"); 65 | return -2; 66 | } 67 | 68 | if (NULL == fc) { 69 | printf("Error: no frame callback set.\n"); 70 | return -3; 71 | } 72 | 73 | if (bmdModeUnknown != display_mode) { 74 | printf("Error: the display mode is already set, which means you didn't closed the device before reopening. Please close() first before calling open.\n"); 75 | return -3; 76 | } 77 | 78 | if (CA_NONE == cfg.capability) { 79 | printf("Error: capability is not set in DecklinkDevice::open().\n"); 80 | return -4; 81 | } 82 | 83 | r = device->QueryInterface(IID_IDeckLinkInput, (void**)&input); 84 | if (S_OK != r) { 85 | printf("Error: failed to retrieve a IID_IDeckLinkInput interface.\n"); 86 | return -5; 87 | } 88 | 89 | r = input->GetDisplayModeIterator(&mode_iter); 90 | if (S_OK != r) { 91 | printf("Error: cannot get the display mode iterator for the device: %d.\n", cfg.device); 92 | input->Release(); 93 | input = NULL; 94 | return -6; 95 | } 96 | 97 | /* @todo - See Decklink.cpp where we have the same list. */ 98 | const BMDPixelFormat formats[] = { bmdFormat8BitYUV, /* CA_UYVY422 */ 99 | bmdFormat8BitARGB, /* CA_ARGB32 */ 100 | bmdFormat8BitBGRA, /* CA_BGRA32 */ 101 | (BMDPixelFormat)0 102 | }; 103 | 104 | const int formats_map[] = { 105 | CA_UYVY422, 106 | CA_ARGB32, 107 | CA_RGBA32, 108 | 0 109 | }; 110 | 111 | /* Find the capability */ 112 | 113 | while (S_OK == mode_iter->Next(&mode)) { 114 | 115 | BMDDisplayModeSupport mode_support; 116 | int fmt_dx = 0; 117 | 118 | while (formats[fmt_dx] != 0) { 119 | 120 | r = input->DoesSupportVideoMode(mode->GetDisplayMode(), 121 | formats[fmt_dx], 122 | bmdVideoInputFlagDefault, 123 | &mode_support, 124 | NULL); 125 | if (S_OK != r) { 126 | printf("Error: failed to test if video format is supported, fmt_dx: %d\n", fmt_dx); 127 | break; 128 | } 129 | 130 | if (bmdDisplayModeNotSupported != mode_support) { 131 | if (i == cfg.capability) { 132 | display_mode = mode->GetDisplayMode(); 133 | pixel_format = formats[fmt_dx]; 134 | break; 135 | } 136 | i++; 137 | } 138 | fmt_dx++; 139 | } 140 | mode->Release(); 141 | 142 | /* Stop iterating when found. */ 143 | if (bmdModeUnknown != display_mode) { 144 | break; 145 | } 146 | } 147 | 148 | mode_iter->Release(); 149 | 150 | if (bmdModeUnknown == display_mode) { 151 | printf("Error: we couldn't find the capability that was given to DecklinkDevice.\n"); 152 | input->Release(); 153 | input = NULL; 154 | return -5; 155 | } 156 | 157 | return 0; 158 | } 159 | 160 | int DecklinkDevice::close() { 161 | 162 | display_mode = bmdModeUnknown; 163 | return 0; 164 | } 165 | 166 | int DecklinkDevice::start() { 167 | 168 | HRESULT r = S_OK; 169 | 170 | if (NULL == input) { 171 | printf("Error: cannot start capturing from the DecklinkDevice because `input` is NULL. Did you open the device?\n"); 172 | return -1; 173 | } 174 | 175 | if (NULL == fc) { 176 | printf("Error: no frame callback set, cannot start capturing in DecklinkDevice::start().\n"); 177 | return -3; 178 | } 179 | 180 | if (bmdModeUnknown == display_mode) { 181 | printf("Error: the display mode for the decklink device is still unknown. Not supposed to happen at this point. \n"); 182 | return -2; 183 | } 184 | 185 | if (NULL == callback) { 186 | callback = new DecklinkCallback(); 187 | if (NULL == callback) { 188 | printf("Error: failed to allocate the DecklinkCallback. Out of mem?\n"); 189 | return -3; 190 | } 191 | } 192 | 193 | callback->setCallback(fc, user); 194 | 195 | r = input->SetCallback(callback); 196 | if (S_OK != r) { 197 | printf("Error: failed to set the callback on the IDeckLinkInput instance.\n"); 198 | delete callback; 199 | callback = NULL; 200 | return -4; 201 | } 202 | 203 | r = input->EnableVideoInput(display_mode, pixel_format, 0); 204 | if (S_OK != r) { 205 | printf("Error: failed to set the display mode and enable the video input.\n"); 206 | delete callback; 207 | callback = NULL; 208 | return -5; 209 | } 210 | 211 | r = input->StartStreams(); 212 | if (S_OK != r) { 213 | printf("Error: failed to start the input streams.\n"); 214 | delete callback; 215 | callback = NULL; 216 | return -6; 217 | } 218 | 219 | is_started = true; 220 | 221 | return 0; 222 | } 223 | 224 | int DecklinkDevice::stop() { 225 | 226 | HRESULT r = S_OK; 227 | 228 | if (false == is_started) { 229 | printf("Error: cannot stop DecklinkDevice because it's not started yet.\n"); 230 | return -1; 231 | } 232 | 233 | is_started = false; 234 | 235 | r = input->SetCallback(NULL); 236 | if (S_OK != r) { 237 | printf("Error: setting the callback to NULL on the DecklinkDevice failed."); 238 | } 239 | 240 | r = input->StopStreams(); 241 | if (r != S_OK) { 242 | printf("Error: something went wrong while trying to stop the stream.\n"); 243 | return -2; 244 | } 245 | 246 | return 0; 247 | } 248 | 249 | void DecklinkDevice::update() { 250 | } 251 | 252 | 253 | } /* namespace ca */ 254 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = build 9 | 10 | # User-friendly check for sphinx-build 11 | ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) 12 | $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) 13 | endif 14 | 15 | # Internal variables. 16 | PAPEROPT_a4 = -D latex_paper_size=a4 17 | PAPEROPT_letter = -D latex_paper_size=letter 18 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source 19 | # the i18n builder cannot share the environment and doctrees with the others 20 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source 21 | 22 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 23 | 24 | help: 25 | @echo "Please use \`make ' where is one of" 26 | @echo " html to make standalone HTML files" 27 | @echo " dirhtml to make HTML files named index.html in directories" 28 | @echo " singlehtml to make a single large HTML file" 29 | @echo " pickle to make pickle files" 30 | @echo " json to make JSON files" 31 | @echo " htmlhelp to make HTML files and a HTML help project" 32 | @echo " qthelp to make HTML files and a qthelp project" 33 | @echo " devhelp to make HTML files and a Devhelp project" 34 | @echo " epub to make an epub" 35 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 36 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 37 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 38 | @echo " text to make text files" 39 | @echo " man to make manual pages" 40 | @echo " texinfo to make Texinfo files" 41 | @echo " info to make Texinfo files and run them through makeinfo" 42 | @echo " gettext to make PO message catalogs" 43 | @echo " changes to make an overview of all changed/added/deprecated items" 44 | @echo " xml to make Docutils-native XML files" 45 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 46 | @echo " linkcheck to check all external links for integrity" 47 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 48 | 49 | clean: 50 | rm -rf $(BUILDDIR)/* 51 | 52 | html: 53 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 54 | @echo 55 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 56 | 57 | dirhtml: 58 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 59 | @echo 60 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 61 | 62 | singlehtml: 63 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 64 | @echo 65 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 66 | 67 | pickle: 68 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 69 | @echo 70 | @echo "Build finished; now you can process the pickle files." 71 | 72 | json: 73 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 74 | @echo 75 | @echo "Build finished; now you can process the JSON files." 76 | 77 | htmlhelp: 78 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 79 | @echo 80 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 81 | ".hhp project file in $(BUILDDIR)/htmlhelp." 82 | 83 | qthelp: 84 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 85 | @echo 86 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 87 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 88 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/VideoCapture.qhcp" 89 | @echo "To view the help file:" 90 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/VideoCapture.qhc" 91 | 92 | devhelp: 93 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 94 | @echo 95 | @echo "Build finished." 96 | @echo "To view the help file:" 97 | @echo "# mkdir -p $$HOME/.local/share/devhelp/VideoCapture" 98 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/VideoCapture" 99 | @echo "# devhelp" 100 | 101 | epub: 102 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 103 | @echo 104 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 105 | 106 | latex: 107 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 108 | @echo 109 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 110 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 111 | "(use \`make latexpdf' here to do that automatically)." 112 | 113 | latexpdf: 114 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 115 | @echo "Running LaTeX files through pdflatex..." 116 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 117 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 118 | 119 | latexpdfja: 120 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 121 | @echo "Running LaTeX files through platex and dvipdfmx..." 122 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 123 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 124 | 125 | text: 126 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 127 | @echo 128 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 129 | 130 | man: 131 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 132 | @echo 133 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 134 | 135 | texinfo: 136 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 137 | @echo 138 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 139 | @echo "Run \`make' in that directory to run these through makeinfo" \ 140 | "(use \`make info' here to do that automatically)." 141 | 142 | info: 143 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 144 | @echo "Running Texinfo files through makeinfo..." 145 | make -C $(BUILDDIR)/texinfo info 146 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 147 | 148 | gettext: 149 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 150 | @echo 151 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 152 | 153 | changes: 154 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 155 | @echo 156 | @echo "The overview file is in $(BUILDDIR)/changes." 157 | 158 | linkcheck: 159 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 160 | @echo 161 | @echo "Link check complete; look for any errors in the above output " \ 162 | "or in $(BUILDDIR)/linkcheck/output.txt." 163 | 164 | doctest: 165 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 166 | @echo "Testing of doctests in the sources finished, look at the " \ 167 | "results in $(BUILDDIR)/doctest/output.txt." 168 | 169 | xml: 170 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 171 | @echo 172 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 173 | 174 | pseudoxml: 175 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 176 | @echo 177 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 178 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=build 9 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% source 10 | set I18NSPHINXOPTS=%SPHINXOPTS% source 11 | if NOT "%PAPER%" == "" ( 12 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 13 | set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% 14 | ) 15 | 16 | if "%1" == "" goto help 17 | 18 | if "%1" == "help" ( 19 | :help 20 | echo.Please use `make ^` where ^ is one of 21 | echo. html to make standalone HTML files 22 | echo. dirhtml to make HTML files named index.html in directories 23 | echo. singlehtml to make a single large HTML file 24 | echo. pickle to make pickle files 25 | echo. json to make JSON files 26 | echo. htmlhelp to make HTML files and a HTML help project 27 | echo. qthelp to make HTML files and a qthelp project 28 | echo. devhelp to make HTML files and a Devhelp project 29 | echo. epub to make an epub 30 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 31 | echo. text to make text files 32 | echo. man to make manual pages 33 | echo. texinfo to make Texinfo files 34 | echo. gettext to make PO message catalogs 35 | echo. changes to make an overview over all changed/added/deprecated items 36 | echo. xml to make Docutils-native XML files 37 | echo. pseudoxml to make pseudoxml-XML files for display purposes 38 | echo. linkcheck to check all external links for integrity 39 | echo. doctest to run all doctests embedded in the documentation if enabled 40 | goto end 41 | ) 42 | 43 | if "%1" == "clean" ( 44 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 45 | del /q /s %BUILDDIR%\* 46 | goto end 47 | ) 48 | 49 | 50 | %SPHINXBUILD% 2> nul 51 | if errorlevel 9009 ( 52 | echo. 53 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 54 | echo.installed, then set the SPHINXBUILD environment variable to point 55 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 56 | echo.may add the Sphinx directory to PATH. 57 | echo. 58 | echo.If you don't have Sphinx installed, grab it from 59 | echo.http://sphinx-doc.org/ 60 | exit /b 1 61 | ) 62 | 63 | if "%1" == "html" ( 64 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 65 | if errorlevel 1 exit /b 1 66 | echo. 67 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 68 | goto end 69 | ) 70 | 71 | if "%1" == "dirhtml" ( 72 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 73 | if errorlevel 1 exit /b 1 74 | echo. 75 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 76 | goto end 77 | ) 78 | 79 | if "%1" == "singlehtml" ( 80 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 81 | if errorlevel 1 exit /b 1 82 | echo. 83 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 84 | goto end 85 | ) 86 | 87 | if "%1" == "pickle" ( 88 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 89 | if errorlevel 1 exit /b 1 90 | echo. 91 | echo.Build finished; now you can process the pickle files. 92 | goto end 93 | ) 94 | 95 | if "%1" == "json" ( 96 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 97 | if errorlevel 1 exit /b 1 98 | echo. 99 | echo.Build finished; now you can process the JSON files. 100 | goto end 101 | ) 102 | 103 | if "%1" == "htmlhelp" ( 104 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 105 | if errorlevel 1 exit /b 1 106 | echo. 107 | echo.Build finished; now you can run HTML Help Workshop with the ^ 108 | .hhp project file in %BUILDDIR%/htmlhelp. 109 | goto end 110 | ) 111 | 112 | if "%1" == "qthelp" ( 113 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 114 | if errorlevel 1 exit /b 1 115 | echo. 116 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 117 | .qhcp project file in %BUILDDIR%/qthelp, like this: 118 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\VideoCapture.qhcp 119 | echo.To view the help file: 120 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\VideoCapture.ghc 121 | goto end 122 | ) 123 | 124 | if "%1" == "devhelp" ( 125 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 126 | if errorlevel 1 exit /b 1 127 | echo. 128 | echo.Build finished. 129 | goto end 130 | ) 131 | 132 | if "%1" == "epub" ( 133 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 134 | if errorlevel 1 exit /b 1 135 | echo. 136 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 137 | goto end 138 | ) 139 | 140 | if "%1" == "latex" ( 141 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 142 | if errorlevel 1 exit /b 1 143 | echo. 144 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 145 | goto end 146 | ) 147 | 148 | if "%1" == "latexpdf" ( 149 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 150 | cd %BUILDDIR%/latex 151 | make all-pdf 152 | cd %BUILDDIR%/.. 153 | echo. 154 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 155 | goto end 156 | ) 157 | 158 | if "%1" == "latexpdfja" ( 159 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 160 | cd %BUILDDIR%/latex 161 | make all-pdf-ja 162 | cd %BUILDDIR%/.. 163 | echo. 164 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 165 | goto end 166 | ) 167 | 168 | if "%1" == "text" ( 169 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 170 | if errorlevel 1 exit /b 1 171 | echo. 172 | echo.Build finished. The text files are in %BUILDDIR%/text. 173 | goto end 174 | ) 175 | 176 | if "%1" == "man" ( 177 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 178 | if errorlevel 1 exit /b 1 179 | echo. 180 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 181 | goto end 182 | ) 183 | 184 | if "%1" == "texinfo" ( 185 | %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo 186 | if errorlevel 1 exit /b 1 187 | echo. 188 | echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. 189 | goto end 190 | ) 191 | 192 | if "%1" == "gettext" ( 193 | %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale 194 | if errorlevel 1 exit /b 1 195 | echo. 196 | echo.Build finished. The message catalogs are in %BUILDDIR%/locale. 197 | goto end 198 | ) 199 | 200 | if "%1" == "changes" ( 201 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 202 | if errorlevel 1 exit /b 1 203 | echo. 204 | echo.The overview file is in %BUILDDIR%/changes. 205 | goto end 206 | ) 207 | 208 | if "%1" == "linkcheck" ( 209 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 210 | if errorlevel 1 exit /b 1 211 | echo. 212 | echo.Link check complete; look for any errors in the above output ^ 213 | or in %BUILDDIR%/linkcheck/output.txt. 214 | goto end 215 | ) 216 | 217 | if "%1" == "doctest" ( 218 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 219 | if errorlevel 1 exit /b 1 220 | echo. 221 | echo.Testing of doctests in the sources finished, look at the ^ 222 | results in %BUILDDIR%/doctest/output.txt. 223 | goto end 224 | ) 225 | 226 | if "%1" == "xml" ( 227 | %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml 228 | if errorlevel 1 exit /b 1 229 | echo. 230 | echo.Build finished. The XML files are in %BUILDDIR%/xml. 231 | goto end 232 | ) 233 | 234 | if "%1" == "pseudoxml" ( 235 | %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml 236 | if errorlevel 1 exit /b 1 237 | echo. 238 | echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. 239 | goto end 240 | ) 241 | 242 | :end 243 | -------------------------------------------------------------------------------- /docs/source/guide.rst: -------------------------------------------------------------------------------- 1 | .. _guide: 2 | 3 | ***************** 4 | Programmers Guide 5 | ***************** 6 | 7 | In this guide we will create a very simple program that lists the 8 | available capture devices, then lists the capabilities of a device. Once 9 | we found a capability that we want to use we open the device and start 10 | captureing. Then in a loop we will flush the buffers of the capture device 11 | and let it call our callback function. Before we start we explain a couple 12 | of concepts that we use in Video Capture. 13 | 14 | Concepts used in Video Capture 15 | ----------------------------- 16 | 17 | In Video Capture we use a couple of concepts which are shared among most 18 | SDKs/APIs we found on OSX, Linux and Windows. 19 | 20 | Device 21 | We use the term **Device** to represent something like a webcam. 22 | This **Device** can capture video in a specific **pixel format**. 23 | 24 | Pixel format 25 | A pixel format describes how the bytes of a video frame are stored. 26 | Common pixel formats for Video Capture are YUYV422, UYVV422 and YUV420P. 27 | Some OSes can convert between pixel formats (Mac). See [libyuvs](https://code.google.com/p/libyuv/wiki/Formats) 28 | documentation for some more info on format mappings.. 29 | 30 | Output formats 31 | Some SDKs have optimized solutions to decode a video stream you 32 | get from a capture device. For example on Mac you can use the OS to 33 | convert a raw YUV stream into a RGB24 stream. Some OSes even have 34 | support to decode H264, so the output formats are also related to codecs 35 | and not only pixel formats. 36 | 37 | Capability 38 | A capability describes a couple of things related to what a device 39 | can give you. These are things like the dimensions of the video frames 40 | you receive, the framerate and the pixel format. Video Capture supports 41 | querying the available capabilities of a device on Windows, Mac and Linux. 42 | 43 | Settings 44 | Video Capture uses a settings object when you want to open a device. The 45 | Settings object stores information like, what device you want to use, 46 | what capability and what pixel format you want to use. A settings object 47 | is passed into the ``open()`` method of the cature class. 48 | 49 | Frame 50 | A frame is a helper type we created which gives you information about 51 | a pixel format. If necessary you can use a ``Frame`` object to get information 52 | about strides, widths, heights, offsets etc.. for planar or non-planar 53 | pixel formats. See the opengl example where we use a Frame to get offets 54 | into the YUV420P data on windows. 55 | 56 | 57 | Getting information about a device 58 | --------------------------------- 59 | 60 | Before we open a capture device we have to inspect the capabilities 61 | of the capture device. Do detect if we found your capture device you can 62 | use the ``listDevices()`` function of the ``Capture`` class. 63 | 64 | Note that all types of the Video Capture library are using the ``ca`` namespace. 65 | The example below creates a ``Capture`` instance which is the interface to your 66 | capture device. 67 | 68 | .. highlight:: c++ 69 | 70 | :: 71 | 72 | using namespace ca; 73 | 74 | Capture capture(fc,NULL); 75 | if(capture.listDevices() < 0) { 76 | printf("Error: cannot list devices.\n"); 77 | ::exit(EXIT_FAILURE); 78 | } 79 | 80 | if(capture.listCapabilities(0) < 0) { 81 | printf("Error: cannot list capabilities for devices 0.\n"); 82 | ::exit(EXIT_FAILURE); 83 | } 84 | 85 | 86 | ``listDevices()`` will log all the found capture devices to stdout. Each 87 | device has a unique number that you will need to use when opening a device. 88 | When a function fails it will return a negative error code, so make sure 89 | to check if the result is ``< 0`` as shown above. 90 | 91 | The ``listCapabilities()`` function will list all the capabilities of the 92 | capture device. A capability describes the width, height, framerate and 93 | pixel format. From this list, pick the capability number that you want to use. 94 | The Video Capture library also provides a ``findCapability()`` function 95 | that you can use to find a specific capability for a device. This function will 96 | return the index number of the found capability or a negative value if not 97 | found. 98 | 99 | Some SDKs can convert a pixel format from the capture device into another, 100 | maybe more easy to use one. For example Mac gives you a way to convert from a 101 | YUV pixel format to RGB format. Although this is very handy, it's not recommended 102 | because converting will mostly be done on the CPU (maybe with SIMD) which means 103 | you loose some processing power for other parts of your application. Use the 104 | ``listOutputFormats()`` to inspect what output formats are supported. 105 | 106 | 107 | Opening a device 108 | ----------------- 109 | 110 | Once you've found what `device`, `capability` and `output format` you want to 111 | use you need to create a ``Settings`` object that describes how you want to 112 | use the device. Below we show how to create a ``Settings`` object and how to 113 | set what capability, device and format we want to use. 114 | 115 | :: 116 | 117 | using namespace ca; 118 | 119 | Settings settings; 120 | settings.device = 0; // Use number 0 from the device list (see listDevices()) 121 | settings.capability = 15; // Use number 15 from the capability list (see listCapabilities()) 122 | settings.format = -1 // We're not using any output format conversion (see listOutputFormats()) 123 | 124 | Once we have this ``Settings`` object we pass it into the ``open()`` function 125 | of the ``Capture`` instance. This will open the device and set the capability. 126 | Make sure to check the return values from ``open()``, when it's negative an error 127 | occured. 128 | 129 | :: 130 | 131 | using namespace ca; 132 | Capture capture(fc, NULL); 133 | 134 | Settings settings; 135 | settings.device = 0; 136 | settings.capability = 15; 137 | settings.format = -1 138 | 139 | if(capture.open(settings) < 0)) { 140 | printf("Error: cannot open the capture device.\n"); 141 | :exit(EXIT_FAILURE); 142 | } 143 | 144 | 145 | Captureing frames 146 | ----------------- 147 | 148 | After opening the capture device we can start receiving frames. Video Capture 149 | uses a callback function that is called whenever a new frame arrives. Two things 150 | are important about this callback function: 151 | 152 | - This function may be called from another thread 153 | - This function must return before a new frame arrives 154 | 155 | The callback function is passed to the constructor of the ``Capture`` class. 156 | The interface of this callback function is: 157 | 158 | :: 159 | 160 | void on_frame(void* bytes, int nbytes, void* user) 161 | 162 | 163 | - **bytes** is a pointer to the frame data from the capture device. This maybe be a pointer 164 | to planar video data when e.g. using YUV420P. See the opengl example where we use YUV420P 165 | (on Windows). 166 | - **nbytes** the number of bytes in the frame. 167 | - **user** a pointer to user data. This is the second parameter to the ``Capture`` constructor. 168 | 169 | To capture frames you use: 170 | 171 | :: 172 | 173 | if(capture.start() < 0) { 174 | printf("Error: cannot start captureing.\n"); 175 | ::exit(EXIT_FAILURE); 176 | } 177 | 178 | while(must_capture) { 179 | capture.update(); 180 | } 181 | 182 | if(capture.stop() < 0) { 183 | printf("Error: cannot stop the capture process.\n"); 184 | } 185 | 186 | 187 | Make sure to call ``update()`` at at least the same rate of the used frame rate. Some 188 | capture SDKs don't use async callbacks for which we need to process any pending frames. 189 | 190 | 191 | Closing a device 192 | ---------------- 193 | 194 | Once you're done make sure to correctly cleanup and shutdown the capture device. 195 | Closing a device will make sure that all allocated memory gets freed and the device 196 | is correctly shutdown. Note: when you don't close a device on Linux, it will continue 197 | to be in `opened` state and can't be used anymore, before you correctly close it. 198 | 199 | :: 200 | 201 | if(capture.close() < 0) { 202 | printf("Error: cannot close the capture device.\n"); 203 | } 204 | -------------------------------------------------------------------------------- /docs/source/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Video Capture documentation build configuration file, created by 4 | # sphinx-quickstart on Mon Mar 10 09:23:03 2014. 5 | # 6 | # This file is execfile()d with the current directory set to its 7 | # containing dir. 8 | # 9 | # Note that not all possible configuration values are present in this 10 | # autogenerated file. 11 | # 12 | # All configuration values have a default; values that are commented out 13 | # serve to show the default. 14 | 15 | import sys 16 | import os 17 | 18 | # If extensions (or modules to document with autodoc) are in another directory, 19 | # add these directories to sys.path here. If the directory is relative to the 20 | # documentation root, use os.path.abspath to make it absolute, like shown here. 21 | #sys.path.insert(0, os.path.abspath('.')) 22 | 23 | # -- General configuration ------------------------------------------------ 24 | 25 | # If your documentation needs a minimal Sphinx version, state it here. 26 | #needs_sphinx = '1.0' 27 | 28 | # Add any Sphinx extension module names here, as strings. They can be 29 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 30 | # ones. 31 | extensions = [] 32 | 33 | # Add any paths that contain templates here, relative to this directory. 34 | templates_path = ['_templates'] 35 | 36 | # The suffix of source filenames. 37 | source_suffix = '.rst' 38 | 39 | # The encoding of source files. 40 | #source_encoding = 'utf-8-sig' 41 | 42 | # The master toctree document. 43 | master_doc = 'index' 44 | 45 | # General information about the project. 46 | project = u'Video Capture' 47 | copyright = u'2014, roxlu' 48 | 49 | # The version info for the project you're documenting, acts as replacement for 50 | # |version| and |release|, also used in various other places throughout the 51 | # built documents. 52 | # 53 | # The short X.Y version. 54 | version = '0.0.0.1' 55 | # The full version, including alpha/beta/rc tags. 56 | release = '0.0.0.1' 57 | 58 | # The language for content autogenerated by Sphinx. Refer to documentation 59 | # for a list of supported languages. 60 | #language = None 61 | 62 | # There are two options for replacing |today|: either, you set today to some 63 | # non-false value, then it is used: 64 | #today = '' 65 | # Else, today_fmt is used as the format for a strftime call. 66 | #today_fmt = '%B %d, %Y' 67 | 68 | # List of patterns, relative to source directory, that match files and 69 | # directories to ignore when looking for source files. 70 | exclude_patterns = [] 71 | 72 | # The reST default role (used for this markup: `text`) to use for all 73 | # documents. 74 | #default_role = None 75 | 76 | # If true, '()' will be appended to :func: etc. cross-reference text. 77 | #add_function_parentheses = True 78 | 79 | # If true, the current module name will be prepended to all description 80 | # unit titles (such as .. function::). 81 | #add_module_names = True 82 | 83 | # If true, sectionauthor and moduleauthor directives will be shown in the 84 | # output. They are ignored by default. 85 | #show_authors = False 86 | 87 | # The name of the Pygments (syntax highlighting) style to use. 88 | pygments_style = 'sphinx' 89 | 90 | # A list of ignored prefixes for module index sorting. 91 | #modindex_common_prefix = [] 92 | 93 | # If true, keep warnings as "system message" paragraphs in the built documents. 94 | #keep_warnings = False 95 | 96 | 97 | # -- Options for HTML output ---------------------------------------------- 98 | 99 | # The theme to use for HTML and HTML Help pages. See the documentation for 100 | # a list of builtin themes. 101 | html_theme = 'default' 102 | 103 | # Theme options are theme-specific and customize the look and feel of a theme 104 | # further. For a list of options available for each theme, see the 105 | # documentation. 106 | #html_theme_options = {} 107 | 108 | # Add any paths that contain custom themes here, relative to this directory. 109 | #html_theme_path = [] 110 | 111 | # The name for this set of Sphinx documents. If None, it defaults to 112 | # " v documentation". 113 | #html_title = None 114 | 115 | # A shorter title for the navigation bar. Default is the same as html_title. 116 | #html_short_title = None 117 | 118 | # The name of an image file (relative to this directory) to place at the top 119 | # of the sidebar. 120 | #html_logo = None 121 | 122 | # The name of an image file (within the static path) to use as favicon of the 123 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 124 | # pixels large. 125 | #html_favicon = None 126 | 127 | # Add any paths that contain custom static files (such as style sheets) here, 128 | # relative to this directory. They are copied after the builtin static files, 129 | # so a file named "default.css" will overwrite the builtin "default.css". 130 | html_static_path = ['_static'] 131 | 132 | # Add any extra paths that contain custom files (such as robots.txt or 133 | # .htaccess) here, relative to this directory. These files are copied 134 | # directly to the root of the documentation. 135 | #html_extra_path = [] 136 | 137 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 138 | # using the given strftime format. 139 | #html_last_updated_fmt = '%b %d, %Y' 140 | 141 | # If true, SmartyPants will be used to convert quotes and dashes to 142 | # typographically correct entities. 143 | #html_use_smartypants = True 144 | 145 | # Custom sidebar templates, maps document names to template names. 146 | #html_sidebars = {} 147 | 148 | # Additional templates that should be rendered to pages, maps page names to 149 | # template names. 150 | #html_additional_pages = {} 151 | 152 | # If false, no module index is generated. 153 | #html_domain_indices = True 154 | 155 | # If false, no index is generated. 156 | #html_use_index = True 157 | 158 | # If true, the index is split into individual pages for each letter. 159 | #html_split_index = False 160 | 161 | # If true, links to the reST sources are added to the pages. 162 | #html_show_sourcelink = True 163 | 164 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 165 | #html_show_sphinx = True 166 | 167 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 168 | #html_show_copyright = True 169 | 170 | # If true, an OpenSearch description file will be output, and all pages will 171 | # contain a tag referring to it. The value of this option must be the 172 | # base URL from which the finished HTML is served. 173 | #html_use_opensearch = '' 174 | 175 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 176 | #html_file_suffix = None 177 | 178 | # Output file base name for HTML help builder. 179 | htmlhelp_basename = 'VideoCapturedoc' 180 | 181 | 182 | # -- Options for LaTeX output --------------------------------------------- 183 | 184 | latex_elements = { 185 | # The paper size ('letterpaper' or 'a4paper'). 186 | #'papersize': 'letterpaper', 187 | 188 | # The font size ('10pt', '11pt' or '12pt'). 189 | #'pointsize': '10pt', 190 | 191 | # Additional stuff for the LaTeX preamble. 192 | #'preamble': '', 193 | } 194 | 195 | # Grouping the document tree into LaTeX files. List of tuples 196 | # (source start file, target name, title, 197 | # author, documentclass [howto, manual, or own class]). 198 | latex_documents = [ 199 | ('index', 'VideoCapture.tex', u'Video Capture Documentation', 200 | u'roxlu', 'manual'), 201 | ] 202 | 203 | # The name of an image file (relative to this directory) to place at the top of 204 | # the title page. 205 | #latex_logo = None 206 | 207 | # For "manual" documents, if this is true, then toplevel headings are parts, 208 | # not chapters. 209 | #latex_use_parts = False 210 | 211 | # If true, show page references after internal links. 212 | #latex_show_pagerefs = False 213 | 214 | # If true, show URL addresses after external links. 215 | #latex_show_urls = False 216 | 217 | # Documents to append as an appendix to all manuals. 218 | #latex_appendices = [] 219 | 220 | # If false, no module index is generated. 221 | #latex_domain_indices = True 222 | 223 | 224 | # -- Options for manual page output --------------------------------------- 225 | 226 | # One entry per manual page. List of tuples 227 | # (source start file, name, description, authors, manual section). 228 | man_pages = [ 229 | ('index', 'videocapture', u'Video Capture Documentation', 230 | [u'roxlu'], 1) 231 | ] 232 | 233 | # If true, show URL addresses after external links. 234 | #man_show_urls = False 235 | 236 | 237 | # -- Options for Texinfo output ------------------------------------------- 238 | 239 | # Grouping the document tree into Texinfo files. List of tuples 240 | # (source start file, target name, title, author, 241 | # dir menu entry, description, category) 242 | texinfo_documents = [ 243 | ('index', 'VideoCapture', u'Video Capture Documentation', 244 | u'roxlu', 'VideoCapture', 'One line description of project.', 245 | 'Miscellaneous'), 246 | ] 247 | 248 | # Documents to append as an appendix to all manuals. 249 | #texinfo_appendices = [] 250 | 251 | # If false, no module index is generated. 252 | #texinfo_domain_indices = True 253 | 254 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 255 | #texinfo_show_urls = 'footnote' 256 | 257 | # If true, do not generate a @detailmenu in the "Top" node's menu. 258 | #texinfo_no_detailmenu = False 259 | -------------------------------------------------------------------------------- /src/videocapture/linux/V4L2_Utils.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace ca { 5 | 6 | int capture_format_to_v4l2_pixel_format(int fmt) { 7 | switch(fmt) { 8 | case CA_RGB24: return V4L2_PIX_FMT_RGB24; 9 | case CA_YUYV422: return V4L2_PIX_FMT_YUYV; 10 | case CA_YUV420P: return V4L2_PIX_FMT_YUV420; 11 | case CA_YUV422P: return V4L2_PIX_FMT_YUV422P; 12 | case CA_H264: return V4L2_PIX_FMT_H264; 13 | case CA_MJPEG: return V4L2_PIX_FMT_MJPEG; 14 | default: return CA_NONE; 15 | } 16 | } 17 | 18 | int v4l2_pixel_format_to_capture_format(int fmt) { 19 | switch(fmt) { 20 | case V4L2_PIX_FMT_RGB24: return CA_RGB24; 21 | case V4L2_PIX_FMT_YUYV: return CA_YUYV422; 22 | case V4L2_PIX_FMT_YUV420: return CA_YUV420P; 23 | case V4L2_PIX_FMT_YUV422P: return CA_YUV422P; 24 | case V4L2_PIX_FMT_H264: return CA_H264; 25 | case V4L2_PIX_FMT_MJPEG: return CA_MJPEG; 26 | default: return CA_NONE; 27 | } 28 | } 29 | 30 | std::string v4l2_pixel_format_to_string(int fmt) { 31 | switch(fmt) { 32 | case V4L2_PIX_FMT_RGB332: return "V4L2_PIX_FMT_RGB332"; break; 33 | case V4L2_PIX_FMT_RGB444: return "V4L2_PIX_FMT_RGB444"; break; 34 | case V4L2_PIX_FMT_RGB555: return "V4L2_PIX_FMT_RGB555"; break; 35 | case V4L2_PIX_FMT_RGB565: return "V4L2_PIX_FMT_RGB565"; break; 36 | case V4L2_PIX_FMT_RGB555X: return "V4L2_PIX_FMT_RGB555X"; break; 37 | case V4L2_PIX_FMT_RGB565X: return "V4L2_PIX_FMT_RGB565X"; break; 38 | case V4L2_PIX_FMT_BGR666: return "V4L2_PIX_FMT_BGR666"; break; 39 | case V4L2_PIX_FMT_BGR24: return "V4L2_PIX_FMT_BGR24"; break; 40 | case V4L2_PIX_FMT_RGB24: return "V4L2_PIX_FMT_RGB24"; break; 41 | case V4L2_PIX_FMT_BGR32: return "V4L2_PIX_FMT_BGR32"; break; 42 | case V4L2_PIX_FMT_RGB32: return "V4L2_PIX_FMT_RGB32"; break; 43 | case V4L2_PIX_FMT_GREY: return "V4L2_PIX_FMT_GREY"; break; 44 | case V4L2_PIX_FMT_Y4: return "V4L2_PIX_FMT_Y4"; break; 45 | case V4L2_PIX_FMT_Y6: return "V4L2_PIX_FMT_Y6"; break; 46 | case V4L2_PIX_FMT_Y10: return "V4L2_PIX_FMT_Y10"; break; 47 | case V4L2_PIX_FMT_Y12: return "V4L2_PIX_FMT_Y12"; break; 48 | case V4L2_PIX_FMT_Y16: return "V4L2_PIX_FMT_Y16"; break; 49 | case V4L2_PIX_FMT_Y10BPACK: return "V4L2_PIX_FMT_Y10BPACK"; break; 50 | case V4L2_PIX_FMT_PAL8: return "V4L2_PIX_FMT_PAL8"; break; 51 | case V4L2_PIX_FMT_YVU410: return "V4L2_PIX_FMT_YVU410"; break; 52 | case V4L2_PIX_FMT_YVU420: return "V4L2_PIX_FMT_YVU420"; break; 53 | case V4L2_PIX_FMT_YUYV: return "V4L2_PIX_FMT_YUYV"; break; 54 | case V4L2_PIX_FMT_YYUV: return "V4L2_PIX_FMT_YYUV"; break; 55 | case V4L2_PIX_FMT_YVYU: return "V4L2_PIX_FMT_YVYU"; break; 56 | case V4L2_PIX_FMT_UYVY: return "V4L2_PIX_FMT_UYVY"; break; 57 | case V4L2_PIX_FMT_VYUY: return "V4L2_PIX_FMT_VYUY"; break; 58 | case V4L2_PIX_FMT_YUV422P: return "V4L2_PIX_FMT_YUV422P"; break; 59 | case V4L2_PIX_FMT_YUV411P: return "V4L2_PIX_FMT_YUV411P"; break; 60 | case V4L2_PIX_FMT_Y41P: return "V4L2_PIX_FMT_Y41P"; break; 61 | case V4L2_PIX_FMT_YUV444: return "V4L2_PIX_FMT_YUV444"; break; 62 | case V4L2_PIX_FMT_YUV555: return "V4L2_PIX_FMT_YUV555"; break; 63 | case V4L2_PIX_FMT_YUV565: return "V4L2_PIX_FMT_YUV565"; break; 64 | case V4L2_PIX_FMT_YUV32: return "V4L2_PIX_FMT_YUV32"; break; 65 | case V4L2_PIX_FMT_YUV410: return "V4L2_PIX_FMT_YUV410"; break; 66 | case V4L2_PIX_FMT_YUV420: return "V4L2_PIX_FMT_YUV420"; break; 67 | case V4L2_PIX_FMT_HI240: return "V4L2_PIX_FMT_HI240"; break; 68 | case V4L2_PIX_FMT_HM12: return "V4L2_PIX_FMT_HM12"; break; 69 | case V4L2_PIX_FMT_M420: return "V4L2_PIX_FMT_M420"; break; 70 | case V4L2_PIX_FMT_NV12: return "V4L2_PIX_FMT_NV12"; break; 71 | case V4L2_PIX_FMT_NV21: return "V4L2_PIX_FMT_NV21"; break; 72 | case V4L2_PIX_FMT_NV16: return "V4L2_PIX_FMT_NV16"; break; 73 | case V4L2_PIX_FMT_NV61: return "V4L2_PIX_FMT_NV61"; break; 74 | #if defined(V4L2_PIX_FMT_NV24) 75 | case V4L2_PIX_FMT_NV24: return "V4L2_PIX_FMT_NV24"; break; 76 | #endif 77 | #if defined(V4L2_PIX_FMT_NV42) 78 | case V4L2_PIX_FMT_NV42: return "V4L2_PIX_FMT_NV42"; break; 79 | #endif 80 | case V4L2_PIX_FMT_NV12M: return "V4L2_PIX_FMT_NV12M"; break; 81 | #if defined(V4L2_PIX_FMT_NV21M) 82 | case V4L2_PIX_FMT_NV21M: return "V4L2_PIX_FMT_NV21M"; break; 83 | #endif 84 | #if defined(V4L2_PIX_FMT_NV12MT) 85 | case V4L2_PIX_FMT_NV12MT: return "V4L2_PIX_FMT_NV12MT"; break; 86 | #endif 87 | #if defined(V4L2_PIX_FMT_NV12MT_16X16) 88 | case V4L2_PIX_FMT_NV12MT_16X16: return "V4L2_PIX_FMT_NV12MT_16X16"; break; 89 | #endif 90 | #if defined(V4L2_PIX_FMT_YUV420M) 91 | case V4L2_PIX_FMT_YUV420M: return "V4L2_PIX_FMT_YUV420M"; break; 92 | #endif 93 | #if defined(V4L2_PIX_FMT_YVU420M) 94 | case V4L2_PIX_FMT_YVU420M: return "V4L2_PIX_FMT_YVU420M"; break; 95 | #endif 96 | case V4L2_PIX_FMT_SBGGR8: return "V4L2_PIX_FMT_SBGGR8"; break; 97 | case V4L2_PIX_FMT_SGBRG8: return "V4L2_PIX_FMT_SGBRG8"; break; 98 | case V4L2_PIX_FMT_SGRBG8: return "V4L2_PIX_FMT_SGRBG8"; break; 99 | case V4L2_PIX_FMT_SRGGB8: return "V4L2_PIX_FMT_SRGGB8"; break; 100 | case V4L2_PIX_FMT_SBGGR10: return "V4L2_PIX_FMT_SBGGR10"; break; 101 | case V4L2_PIX_FMT_SGBRG10: return "V4L2_PIX_FMT_SGBRG10"; break; 102 | case V4L2_PIX_FMT_SGRBG10: return "V4L2_PIX_FMT_SGRBG10"; break; 103 | case V4L2_PIX_FMT_SRGGB10: return "V4L2_PIX_FMT_SRGGB10"; break; 104 | case V4L2_PIX_FMT_SBGGR12: return "V4L2_PIX_FMT_SBGGR12"; break; 105 | case V4L2_PIX_FMT_SGBRG12: return "V4L2_PIX_FMT_SGBRG12"; break; 106 | case V4L2_PIX_FMT_SGRBG12: return "V4L2_PIX_FMT_SGRBG12"; break; 107 | case V4L2_PIX_FMT_SRGGB12: return "V4L2_PIX_FMT_SRGGB12"; break; 108 | #if defined(V4L2_PIX_FMT_SBGGR10DPCM8) 109 | case V4L2_PIX_FMT_SBGGR10DPCM8: return "V4L2_PIX_FMT_SBGGR10DPCM8"; break; 110 | #endif 111 | #if defined(V4L2_PIX_FMT_SGBRG10DPCM8) 112 | case V4L2_PIX_FMT_SGBRG10DPCM8: return "V4L2_PIX_FMT_SGBRG10DPCM8"; break; 113 | #endif 114 | #if defined(V4L2_PIX_FMT_SGRBG10DPCM8) 115 | case V4L2_PIX_FMT_SGRBG10DPCM8: return "V4L2_PIX_FMT_SGRBG10DPCM8"; break; 116 | #endif 117 | #if defined(V4L2_PIX_FMT_SRGGB10DPCM8) 118 | case V4L2_PIX_FMT_SRGGB10DPCM8: return "V4L2_PIX_FMT_SRGGB10DPCM8"; break; 119 | #endif 120 | case V4L2_PIX_FMT_SBGGR16: return "V4L2_PIX_FMT_SBGGR16"; break; 121 | case V4L2_PIX_FMT_MJPEG: return "V4L2_PIX_FMT_MJPEG"; break; 122 | case V4L2_PIX_FMT_JPEG: return "V4L2_PIX_FMT_JPEG"; break; 123 | case V4L2_PIX_FMT_DV: return "V4L2_PIX_FMT_DV"; break; 124 | case V4L2_PIX_FMT_MPEG: return "V4L2_PIX_FMT_MPEG"; break; 125 | case V4L2_PIX_FMT_H264: return "V4L2_PIX_FMT_H264"; break; 126 | case V4L2_PIX_FMT_H264_NO_SC: return "V4L2_PIX_FMT_H264_NO_SC"; break; 127 | #if defined(V4L2_PIX_FMT_H264_MVC) 128 | case V4L2_PIX_FMT_H264_MVC: return "V4L2_PIX_FMT_H264_MVC"; break; 129 | #endif 130 | case V4L2_PIX_FMT_H263: return "V4L2_PIX_FMT_H263"; break; 131 | case V4L2_PIX_FMT_MPEG1: return "V4L2_PIX_FMT_MPEG1"; break; 132 | case V4L2_PIX_FMT_MPEG2: return "V4L2_PIX_FMT_MPEG2"; break; 133 | case V4L2_PIX_FMT_MPEG4: return "V4L2_PIX_FMT_MPEG4"; break; 134 | case V4L2_PIX_FMT_XVID: return "V4L2_PIX_FMT_XVID"; break; 135 | case V4L2_PIX_FMT_VC1_ANNEX_G: return "V4L2_PIX_FMT_VC1_ANNEX_G"; break; 136 | case V4L2_PIX_FMT_VC1_ANNEX_L: return "V4L2_PIX_FMT_VC1_ANNEX_L"; break; 137 | #if defined(V4L2_PIX_FMT_VP8) 138 | case V4L2_PIX_FMT_VP8: return "V4L2_PIX_FMT_VP8"; break; 139 | #endif 140 | case V4L2_PIX_FMT_CPIA1: return "V4L2_PIX_FMT_CPIA1"; break; 141 | case V4L2_PIX_FMT_WNVA: return "V4L2_PIX_FMT_WNVA"; break; 142 | case V4L2_PIX_FMT_SN9C10X: return "V4L2_PIX_FMT_SN9C10X"; break; 143 | case V4L2_PIX_FMT_SN9C20X_I420: return "V4L2_PIX_FMT_SN9C20X_I420"; break; 144 | case V4L2_PIX_FMT_PWC1: return "V4L2_PIX_FMT_PWC1"; break; 145 | case V4L2_PIX_FMT_PWC2: return "V4L2_PIX_FMT_PWC2"; break; 146 | case V4L2_PIX_FMT_ET61X251: return "V4L2_PIX_FMT_ET61X251"; break; 147 | case V4L2_PIX_FMT_SPCA501: return "V4L2_PIX_FMT_SPCA501"; break; 148 | case V4L2_PIX_FMT_SPCA505: return "V4L2_PIX_FMT_SPCA505"; break; 149 | case V4L2_PIX_FMT_SPCA508: return "V4L2_PIX_FMT_SPCA508"; break; 150 | case V4L2_PIX_FMT_SPCA561: return "V4L2_PIX_FMT_SPCA561"; break; 151 | case V4L2_PIX_FMT_PAC207: return "V4L2_PIX_FMT_PAC207"; break; 152 | case V4L2_PIX_FMT_MR97310A: return "V4L2_PIX_FMT_MR97310A"; break; 153 | #if defined(V4L2_PIX_FMT_JL2005BCD) 154 | case V4L2_PIX_FMT_JL2005BCD: return "V4L2_PIX_FMT_JL2005BCD"; break; 155 | #endif 156 | case V4L2_PIX_FMT_SN9C2028: return "V4L2_PIX_FMT_SN9C2028"; break; 157 | case V4L2_PIX_FMT_SQ905C: return "V4L2_PIX_FMT_SQ905C"; break; 158 | case V4L2_PIX_FMT_PJPG: return "V4L2_PIX_FMT_PJPG"; break; 159 | case V4L2_PIX_FMT_OV511: return "V4L2_PIX_FMT_OV511"; break; 160 | case V4L2_PIX_FMT_OV518: return "V4L2_PIX_FMT_OV518"; break; 161 | case V4L2_PIX_FMT_STV0680: return "V4L2_PIX_FMT_STV0680"; break; 162 | case V4L2_PIX_FMT_TM6000: return "V4L2_PIX_FMT_TM6000"; break; 163 | case V4L2_PIX_FMT_CIT_YYVYUY: return "V4L2_PIX_FMT_CIT_YYVYUY v4l2"; break; 164 | case V4L2_PIX_FMT_KONICA420: return "V4L2_PIX_FMT_KONICA420"; break; 165 | case V4L2_PIX_FMT_JPGL: return "V4L2_PIX_FMT_JPGL"; break; 166 | case V4L2_PIX_FMT_SE401: return "V4L2_PIX_FMT_SE401"; break; 167 | #if defined(V4L2_PIX_FMT_S5C_UYVY_JPG) 168 | case V4L2_PIX_FMT_S5C_UYVY_JPG: return "V4L2_PIX_FMT_S5C_UYVY_JPG"; break; 169 | #endif 170 | default: return "UNKNOWN PIXEL FORMAT"; break; 171 | } 172 | } 173 | 174 | }; // namespace cs 175 | -------------------------------------------------------------------------------- /src/videocapture/decklink/Decklink.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #if defined(_WIN32) 5 | # include 6 | # include 7 | #endif 8 | 9 | namespace ca { 10 | 11 | #if defined(_WIN32) 12 | bool Decklink::is_com_initialized = false; 13 | #endif 14 | 15 | /* -------------------------------------------------------------------- */ 16 | 17 | Decklink::Decklink(frame_callback fc, void* user) 18 | :Base(fc, user) 19 | ,decklink_device(NULL) 20 | { 21 | 22 | #if defined(_WIN32) 23 | if (false == is_com_initialized) { 24 | /* 25 | See notes in MediaCapture_Foundation about CoInitialize ... 26 | @todo we should use the same init. 27 | */ 28 | CoInitialize(NULL); 29 | } 30 | #endif 31 | 32 | } 33 | 34 | Decklink::~Decklink() { 35 | 36 | if (decklink_device) { 37 | delete decklink_device; 38 | } 39 | 40 | decklink_device = NULL; 41 | } 42 | 43 | int Decklink::open(Settings settings) { 44 | 45 | if (NULL != decklink_device) { 46 | printf("Error: you already opened a decklink device. First close it before opening again.\n"); 47 | return -1; 48 | } 49 | 50 | /* @todo for now we only support capabilities, so settings.capability must be set. */ 51 | std::vector caps = getCapabilities(settings.device); 52 | if (settings.capability >= caps.size()) { 53 | printf("Error: Invalid capabilty: %d in Decklink::open(), caps.size(): %lu.\n", settings.capability, caps.size()); 54 | } 55 | 56 | IDeckLink* device = getDevice(settings.device); 57 | if (NULL == device) { 58 | printf("Error: cannot find the device with id: %d.\n", settings.device); 59 | return -2; 60 | } 61 | 62 | decklink_device = new DecklinkDevice(device); 63 | if (NULL == decklink_device) { 64 | printf("Error: failed to allocate the DecklinkDevice wrapper. Out of mem?\n"); 65 | device->Release(); 66 | device = NULL; 67 | return -3; 68 | } 69 | 70 | decklink_device->setCallback(cb_frame, cb_user); 71 | 72 | if (decklink_device->open(settings) < 0) { 73 | printf("Error: failed to open the decklink device implementation.\n"); 74 | delete decklink_device; 75 | decklink_device = NULL; 76 | device->Release(); 77 | device = NULL; 78 | return -4; 79 | } 80 | 81 | device->Release(); 82 | device = NULL; 83 | 84 | return 0; 85 | } 86 | 87 | int Decklink::close() { 88 | 89 | int r = 0; 90 | 91 | if (NULL == decklink_device) { 92 | printf("Error: asked to close the Decklink but it looks like you didn't open it. \n"); 93 | return -1; 94 | } 95 | 96 | if (decklink_device->close() < 0) { 97 | printf("Error: closing the DecklinkDevice returned an error.\n"); 98 | r = -2; 99 | } 100 | 101 | delete decklink_device; 102 | decklink_device = NULL; 103 | 104 | return r; 105 | } 106 | 107 | int Decklink::start() { 108 | 109 | if (NULL == decklink_device) { 110 | printf("Error: cannot start the decklink device because it's not opened yet. Call open() first.\n"); 111 | return -1; 112 | } 113 | 114 | return decklink_device->start(); 115 | } 116 | 117 | int Decklink::stop() { 118 | 119 | if (NULL == decklink_device) { 120 | printf("Error: cannot stop the decklink device because it's not opened yet. Call open() first.\n"); 121 | return -1; 122 | } 123 | 124 | return decklink_device->stop(); 125 | } 126 | 127 | void Decklink::update() { 128 | } 129 | 130 | #if 0 131 | int Decklink::listDevices() { 132 | 133 | std::vector devs = getDevices(); 134 | 135 | return 0; 136 | } 137 | #endif 138 | 139 | std::vector Decklink::getDevices() { 140 | 141 | HRESULT r = S_OK; 142 | IDeckLinkIterator* iter = NULL; 143 | std::vector devs; 144 | 145 | #if defined(_WIN32) 146 | 147 | iter = NULL; 148 | r = CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**)&iter); 149 | if (S_OK != r) { 150 | printf("Error: failed to create a DeckLink device iterator."); 151 | return devs; 152 | } 153 | 154 | #else 155 | 156 | iter = CreateDeckLinkIteratorInstance(); 157 | if (NULL == iter) { 158 | printf("Error: failed to get a decklink iterator. Do you have decklink devices installed?\n"); 159 | return devs; 160 | } 161 | 162 | #endif 163 | 164 | int i = 0; 165 | IDeckLink* dl = NULL; 166 | 167 | #if defined(_WIN32) 168 | 169 | BSTR device_name = NULL; 170 | while (S_OK == iter->Next(&dl)) { 171 | 172 | r = dl->GetModelName(&device_name); 173 | if (S_OK != r) { 174 | printf("Error: cannot retrieve the device name."); 175 | } 176 | 177 | _bstr_t devname(device_name, false); 178 | 179 | Device dev; 180 | dev.index = i; 181 | dev.name = (char*)devname; 182 | devs.push_back(dev); 183 | 184 | dl->Release(); 185 | dl = NULL; 186 | ++i; 187 | } 188 | 189 | #else 190 | # error "Not implement on other OS then Win." 191 | #endif 192 | 193 | iter->Release(); 194 | iter = NULL; 195 | 196 | return devs; 197 | } 198 | 199 | std::vector Decklink::getOutputFormats() { 200 | std::vector fmts; 201 | return fmts; 202 | } 203 | 204 | std::vector Decklink::getCapabilities(int index) { 205 | 206 | std::vector caps; 207 | HRESULT r = S_OK; 208 | IDeckLink* device = NULL; 209 | IDeckLinkInput* input = NULL; 210 | IDeckLinkDisplayModeIterator* mode_iter = NULL; 211 | IDeckLinkDisplayMode* mode = NULL; 212 | int i = 0; 213 | 214 | device = getDevice(index); 215 | if (NULL == device) { 216 | printf("Error: cannot find the given device: %d\n", index); 217 | return caps; 218 | } 219 | 220 | r = device->QueryInterface(IID_IDeckLinkInput, (void**)&input); 221 | if (S_OK != r) { 222 | printf("Error: cannot get the input interface for device: %d.\n", index); 223 | device->Release(); 224 | return caps; 225 | } 226 | 227 | r = input->GetDisplayModeIterator(&mode_iter); 228 | if (S_OK != r) { 229 | printf("Error: cannot get the display mode iterator for the device: %d.\n", index); 230 | input->Release(); 231 | device->Release(); 232 | } 233 | 234 | /* @todo see DecklinkDevice which has a copy of this list. */ 235 | const BMDPixelFormat formats[] = { bmdFormat8BitYUV, /* CA_UYVY422 */ 236 | bmdFormat8BitARGB, /* CA_ARGB32 */ 237 | bmdFormat8BitBGRA, /* CA_BGRA32 */ 238 | (BMDPixelFormat)0 239 | }; 240 | 241 | const int formats_map[] = { 242 | CA_UYVY422, 243 | CA_ARGB32, 244 | CA_RGBA32, 245 | 0 246 | }; 247 | 248 | while (S_OK == mode_iter->Next(&mode)) { 249 | 250 | BSTR mode_bstr = NULL; 251 | BMDTimeValue frame_num; 252 | BMDTimeValue frame_den; 253 | BMDDisplayModeSupport mode_support; 254 | int fmt_dx = 0; 255 | 256 | r = mode->GetName(&mode_bstr); 257 | if (S_OK != r) { 258 | printf("Error: failed to get display mode (capability) name for device: %d.\n", index); 259 | mode->Release(); 260 | continue; 261 | } 262 | 263 | r = mode->GetFrameRate(&frame_num, &frame_den); 264 | if (S_OK != r) { 265 | printf("Error: failed to get the frame rate (capability) for device: %d.\n", index); 266 | mode->Release(); 267 | continue; 268 | } 269 | 270 | while (formats[fmt_dx] != 0) { 271 | 272 | r = input->DoesSupportVideoMode(mode->GetDisplayMode(), 273 | formats[fmt_dx], 274 | bmdVideoInputFlagDefault, 275 | &mode_support, 276 | NULL); 277 | if (S_OK != r) { 278 | printf("Error: failed to test if video format is supported, fmt_dx: %d\n", fmt_dx); 279 | break; 280 | } 281 | 282 | if (bmdDisplayModeNotSupported != mode_support) { 283 | 284 | _bstr_t bs(mode_bstr, false); 285 | 286 | Capability cap; 287 | cap.width = (int) mode->GetWidth(); 288 | cap.height = (int) mode->GetHeight(); 289 | cap.fps = fps_from_rational(frame_num, frame_den); 290 | cap.pixel_format = formats_map[fmt_dx]; 291 | cap.capability_index = i; 292 | cap.fps_index = 0; 293 | cap.pixel_format_index = fmt_dx; 294 | cap.description = bs; 295 | cap.user = NULL; 296 | caps.push_back(cap); 297 | i++; 298 | } 299 | 300 | fmt_dx++; 301 | } 302 | 303 | mode->Release(); 304 | } 305 | 306 | device->Release(); 307 | input->Release(); 308 | mode_iter->Release(); 309 | 310 | return caps; 311 | } 312 | 313 | /* 314 | Get a IDeckLink pointer. The user needs to call ->Release() 315 | on the returned pointer when it's not used anymore. 316 | */ 317 | IDeckLink* Decklink::getDevice(int index) { 318 | 319 | HRESULT r = S_OK; 320 | IDeckLinkIterator* iter = NULL; 321 | std::vector devs; 322 | 323 | #if defined(_WIN32) 324 | 325 | iter = NULL; 326 | r = CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**)&iter); 327 | if (S_OK != r) { 328 | printf("Error: failed to create a DeckLink device iterator."); 329 | return NULL; 330 | } 331 | 332 | #else 333 | 334 | iter = CreateDeckLinkIteratorInstance(); 335 | if (NULL == iter) { 336 | printf("Error: failed to get a decklink iterator. Do you have decklink devices installed?\n"); 337 | return devs; 338 | } 339 | 340 | #endif 341 | 342 | int i = 0; 343 | IDeckLink* dl = NULL; 344 | IDeckLink* result = NULL; 345 | 346 | while (S_OK == iter->Next(&dl)) { 347 | 348 | if (i == index) { 349 | result = dl; 350 | result->AddRef(); 351 | } 352 | 353 | dl->Release(); 354 | dl = NULL; 355 | ++i; 356 | } 357 | 358 | 359 | iter->Release(); 360 | iter = NULL; 361 | 362 | return result; 363 | } 364 | 365 | 366 | } /* namespace ca */ 367 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /src/videocapture/win/MediaFoundation_Utils.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace ca { 4 | 5 | // Convert the MF format to one we can use. 6 | int media_foundation_video_format_to_capture_format(GUID guid) { 7 | 8 | if(IsEqualGUID(guid, MFVideoFormat_RGB24)) { return CA_RGB24; } 9 | else if(IsEqualGUID(guid, MFVideoFormat_I420)) { return CA_YUV420P; } 10 | else if(IsEqualGUID(guid, MFVideoFormat_MJPG)) { return CA_MJPEG; } 11 | else { 12 | return CA_NONE; 13 | } 14 | } 15 | 16 | // Convert a MF format to a string. 17 | #define MEDIAFOUNDATION_CHECK_VIDEOFORMAT(param, val) if (param == val) return #val 18 | 19 | std::string media_foundation_video_format_to_string(const GUID& guid) { 20 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_MAJOR_TYPE); 21 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_SUBTYPE); 22 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_ALL_SAMPLES_INDEPENDENT); 23 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_FIXED_SIZE_SAMPLES); 24 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_COMPRESSED); 25 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_SAMPLE_SIZE); 26 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_WRAPPED_TYPE); 27 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_NUM_CHANNELS); 28 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_SAMPLES_PER_SECOND); 29 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_FLOAT_SAMPLES_PER_SECOND); 30 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_AVG_BYTES_PER_SECOND); 31 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_BLOCK_ALIGNMENT); 32 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_BITS_PER_SAMPLE); 33 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_VALID_BITS_PER_SAMPLE); 34 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_SAMPLES_PER_BLOCK); 35 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_CHANNEL_MASK); 36 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_FOLDDOWN_MATRIX); 37 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_WMADRC_PEAKREF); 38 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_WMADRC_PEAKTARGET); 39 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_WMADRC_AVGREF); 40 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_WMADRC_AVGTARGET); 41 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AUDIO_PREFER_WAVEFORMATEX); 42 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AAC_PAYLOAD_TYPE); 43 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION); 44 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_FRAME_SIZE); 45 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_FRAME_RATE); 46 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_FRAME_RATE_RANGE_MAX); 47 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_FRAME_RATE_RANGE_MIN); 48 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_PIXEL_ASPECT_RATIO); 49 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_DRM_FLAGS); 50 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_PAD_CONTROL_FLAGS); 51 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_SOURCE_CONTENT_HINT); 52 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_VIDEO_CHROMA_SITING); 53 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_INTERLACE_MODE); 54 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_TRANSFER_FUNCTION); 55 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_VIDEO_PRIMARIES); 56 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_CUSTOM_VIDEO_PRIMARIES); 57 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_YUV_MATRIX); 58 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_VIDEO_LIGHTING); 59 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_VIDEO_NOMINAL_RANGE); 60 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_GEOMETRIC_APERTURE); 61 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_MINIMUM_DISPLAY_APERTURE); 62 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_PAN_SCAN_APERTURE); 63 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_PAN_SCAN_ENABLED); 64 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AVG_BITRATE); 65 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AVG_BIT_ERROR_RATE); 66 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_MAX_KEYFRAME_SPACING); 67 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_DEFAULT_STRIDE); 68 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_PALETTE); 69 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_USER_DATA); 70 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_AM_FORMAT_TYPE); 71 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_MPEG_START_TIME_CODE); 72 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_MPEG2_PROFILE); 73 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_MPEG2_LEVEL); 74 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_MPEG2_FLAGS); 75 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_MPEG_SEQUENCE_HEADER); 76 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_DV_AAUX_SRC_PACK_0); 77 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_DV_AAUX_CTRL_PACK_0); 78 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_DV_AAUX_SRC_PACK_1); 79 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_DV_AAUX_CTRL_PACK_1); 80 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_DV_VAUX_SRC_PACK); 81 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_DV_VAUX_CTRL_PACK); 82 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_ARBITRARY_HEADER); 83 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_ARBITRARY_FORMAT); 84 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_IMAGE_LOSS_TOLERANT); 85 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_MPEG4_SAMPLE_DESCRIPTION); 86 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_MPEG4_CURRENT_SAMPLE_ENTRY); 87 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_ORIGINAL_4CC); 88 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MF_MT_ORIGINAL_WAVE_FORMAT_TAG); 89 | 90 | // Media types 91 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFMediaType_Audio); 92 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFMediaType_Video); 93 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFMediaType_Protected); 94 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFMediaType_SAMI); 95 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFMediaType_Script); 96 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFMediaType_Image); 97 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFMediaType_HTML); 98 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFMediaType_Binary); 99 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFMediaType_FileTransfer); 100 | 101 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_AI44); // FCC('AI44') 102 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_ARGB32); // D3DFMT_A8R8G8B8 103 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_AYUV); // FCC('AYUV') 104 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_DV25); // FCC('dv25') 105 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_DV50); // FCC('dv50') 106 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_DVH1); // FCC('dvh1') 107 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_DVSD); // FCC('dvsd') 108 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_DVSL); // FCC('dvsl') 109 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_H264); // FCC('H264') 110 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_I420); // FCC('I420') 111 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_IYUV); // FCC('IYUV') 112 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_M4S2); // FCC('M4S2') 113 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_MJPG); 114 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_MP43); // FCC('MP43') 115 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_MP4S); // FCC('MP4S') 116 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_MP4V); // FCC('MP4V') 117 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_MPG1); // FCC('MPG1') 118 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_MSS1); // FCC('MSS1') 119 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_MSS2); // FCC('MSS2') 120 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_NV11); // FCC('NV11') 121 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_NV12); // FCC('NV12') 122 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_P010); // FCC('P010') 123 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_P016); // FCC('P016') 124 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_P210); // FCC('P210') 125 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_P216); // FCC('P216') 126 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_RGB24); // D3DFMT_R8G8B8 127 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_RGB32); // D3DFMT_X8R8G8B8 128 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_RGB555); // D3DFMT_X1R5G5B5 129 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_RGB565); // D3DFMT_R5G6B5 130 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_RGB8); 131 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_UYVY); // FCC('UYVY') 132 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_v210); // FCC('v210') 133 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_v410); // FCC('v410') 134 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_WMV1); // FCC('WMV1') 135 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_WMV2); // FCC('WMV2') 136 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_WMV3); // FCC('WMV3') 137 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_WVC1); // FCC('WVC1') 138 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_Y210); // FCC('Y210') 139 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_Y216); // FCC('Y216') 140 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_Y410); // FCC('Y410') 141 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_Y416); // FCC('Y416') 142 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_Y41P); 143 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_Y41T); 144 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_YUY2); // FCC('YUY2') 145 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_YV12); // FCC('YV12') 146 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFVideoFormat_YVYU); 147 | 148 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_PCM); // WAVE_FORMAT_PCM 149 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_Float); // WAVE_FORMAT_IEEE_FLOAT 150 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_DTS); // WAVE_FORMAT_DTS 151 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_Dolby_AC3_SPDIF); // WAVE_FORMAT_DOLBY_AC3_SPDIF 152 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_DRM); // WAVE_FORMAT_DRM 153 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_WMAudioV8); // WAVE_FORMAT_WMAUDIO2 154 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_WMAudioV9); // WAVE_FORMAT_WMAUDIO3 155 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_WMAudio_Lossless); // WAVE_FORMAT_WMAUDIO_LOSSLESS 156 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_WMASPDIF); // WAVE_FORMAT_WMASPDIF 157 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_MSP1); // WAVE_FORMAT_WMAVOICE9 158 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_MP3); // WAVE_FORMAT_MPEGLAYER3 159 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_MPEG); // WAVE_FORMAT_MPEG 160 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_AAC); // WAVE_FORMAT_MPEG_HEAAC 161 | MEDIAFOUNDATION_CHECK_VIDEOFORMAT(guid, MFAudioFormat_ADTS); // WAVE_FORMAT_MPEG_ADTS_AAC 162 | return "UNKNOWN"; 163 | } 164 | } // namespace ca 165 | -------------------------------------------------------------------------------- /include/videocapture/Types.h: -------------------------------------------------------------------------------- 1 | #ifndef VIDEO_CAPTURE_TYPES_H 2 | #define VIDEO_CAPTURE_TYPES_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | /* General */ 10 | #define CA_NONE -1 11 | 12 | /* Capture Drivers */ 13 | #define CA_MEDIA_FOUNDATION 1 /* Windows: Capture using Windows Media Foundation. */ 14 | #define CA_AV_FOUNDATION 2 /* Mac: Capture using AVFoundation. */ 15 | #define CA_V4L2 3 /* Linux: Capture using Video4Linux 2. */ 16 | #define CA_DECKLINK 4 /* All: Capture using a Decklink device. */ 17 | 18 | /* Default driver per OS */ 19 | #if defined(__APPLE__) 20 | # define CA_DEFAULT_DRIVER CA_AV_FOUNDATION 21 | #elif defined(__linux) 22 | # define CA_DEFAULT_DRIVER CA_V4L2 23 | #elif defined(_WIN32) 24 | # define CA_DEFAULT_DRIVER CA_MEDIA_FOUNDATION 25 | #else 26 | # error "We have no default capture implementation for this OS." 27 | #endif 28 | 29 | /* Pixel Formats */ 30 | #define CA_UYVY422 1 /* Cb Y0 Cr Y1 */ 31 | #define CA_YUYV422 2 /* Y0 Cb Y1 Cr */ 32 | #define CA_YUV422P 3 /* YUV422 Planar */ 33 | #define CA_YUV420P 4 /* YUV420 Planar */ 34 | #define CA_YUV420BP 5 /* YUV420 Bi Planar */ 35 | #define CA_YUVJ420P 6 /* YUV420 Planar Full Range (JPEG), J comes from the JPEG. (values 0-255 used) */ 36 | #define CA_YUVJ420BP 7 /* YUV420 Bi-Planer Full Range (JPEG), J comes fro the JPEG. (values: luma = [16,235], chroma=[16,240]) */ 37 | #define CA_ARGB32 8 /* ARGB 8:8:8:8 32bpp, ARGBARGBARGB... */ 38 | #define CA_BGRA32 9 /* BGRA 8:8:8:8 32bpp, BGRABGRABGRA... */ 39 | #define CA_RGBA32 10 /* RGBA 8:8:8:8 32bpp. */ 40 | #define CA_RGB24 11 /* RGB 8:8:8 24bit */ 41 | #define CA_JPEG_OPENDML 12 /* JPEG with Open-DML extensions */ 42 | #define CA_H264 13 /* H264 */ 43 | #define CA_MJPEG 14 /* MJPEG 2*/ 44 | 45 | /* Frame rates (IMPORANTANT: higher framerates MUST have a higher integer value for capability filtering)*/ 46 | #define CA_FPS_60_00 6000 47 | #define CA_FPS_59_94 5994 48 | #define CA_FPS_50_00 5000 49 | #define CA_FPS_30_00 3000 50 | #define CA_FPS_29_97 2997 51 | #define CA_FPS_27_50 2750 52 | #define CA_FPS_25_00 2500 53 | #define CA_FPS_23_98 2398 54 | #define CA_FPS_24_00 2400 55 | #define CA_FPS_22_50 2250 56 | #define CA_FPS_20_00 2000 57 | #define CA_FPS_17_50 1750 58 | #define CA_FPS_15_00 1500 59 | #define CA_FPS_12_50 1250 60 | #define CA_FPS_10_00 1000 61 | #define CA_FPS_7_50 750 62 | #define CA_FPS_5_00 500 63 | #define CA_FPS_2_00 200 64 | 65 | /* States (may be be used by implementations) */ 66 | #define CA_STATE_NONE 0x00 /* Default state */ 67 | #define CA_STATE_OPENED 0x01 /* The user opened a device */ 68 | #define CA_STATE_CAPTUREING 0x02 /* The user started captureing */ 69 | 70 | /* Capability Filter Attributes. */ 71 | #define CA_WIDTH 0 /* Used by the `filterCapabilities()` feature; filter on width. */ 72 | #define CA_HEIGHT 1 /* Used by the `filterCapabilities()` feature; filter on height. */ 73 | #define CA_RATIO 2 /* Used by the `filterCapabilities()` feature; filter on ratio (width/height). */ 74 | #define CA_PIXEL_FORMAT 3 /* Used by the `filterCapabilities()` feature; filter on pixel format (CA_YUYV422, etc..). */ 75 | 76 | namespace ca { 77 | 78 | 79 | class PixelBuffer; 80 | 81 | typedef void(*frame_callback)(PixelBuffer& buffer); 82 | 83 | /* -------------------------------------- */ 84 | 85 | class PixelBuffer { 86 | public: 87 | PixelBuffer(); 88 | int setup(int w, int h, int fmt); /* Set the strides, widths, heights, nbyte values for the given pixel format (CA_UYVY422, CA_YUV420P etc..) and video frame size.. Returns 0 on success otherwise < 0. */ 89 | 90 | public: 91 | uint8_t* pixels; /* When data is one continuous block of member you can use this, otherwise it points to the same location as plane[0]. */ 92 | uint8_t* plane[3]; /* Pointers to the pixel data; when we're a planar format all members are set, if packets only plane[0] */ 93 | size_t stride[3]; /* The number of bytes you should jump per row when reading the pixel data. Note that some buffer may have extra bytse at the end for memory alignment. */ 94 | size_t width[3]; /* The width; when planar each plane will have it's own value; otherwise only the first index is set. */ 95 | size_t height[3]; /* The height; when planar each plane will have it's own value; otherwise only the first index is set. */ 96 | size_t offset[3]; /* When the data is planar but packed, these contains the byte offsets from the first byte / plane. e.g. you can use this with YUV420P. */ 97 | size_t nbytes; /* The total number of bytes that make up the frame. This doesn't have to be one continuous array when the data is planar. */ 98 | int pixel_format; /* The pixel format of the buffer; e.g. CA_YUYV422, CA_UYVY422, CA_JPEG_OPENDML, etc.. */ 99 | void* user; /* Can be set to any user data that can be used in the frame callback. */ 100 | }; 101 | 102 | /* -------------------------------------- */ 103 | 104 | class Capability { /* Capability represents a possibility for a capture device. It often is related to a width/height/fps. */ 105 | public: 106 | Capability(); 107 | Capability(int width, int height, int pixfmt); /* Create a capability with the given width, height and pixel format. */ 108 | ~Capability(); 109 | void clear(); /* Resets all members to defaults. */ 110 | 111 | public: 112 | /* Set by the user */ 113 | int width; /* Width for this capability. */ 114 | int height; /* Height for this capability. */ 115 | int pixel_format; /* The pixel format for this capability, one of (CA_*) */ 116 | int fps; /* The FPS, see CA_FPS_* above. */ 117 | 118 | /* Set by the capturer implementation */ 119 | int capability_index; /* Used by the implementation. Is the ID of this specific capability */ 120 | int fps_index; /* Used by the implementation, can be an index to an FPS array that is provided by the implementation */ 121 | int pixel_format_index; /* Used by the implementation, represents an index to the pixel format for te implementation */ 122 | std::string description; /* A capture driver can add some additional information here. */ 123 | void* user; /* Can be set by the implementation to anything which is suitable */ 124 | 125 | /* Filtering */ 126 | int filter_score; /* When using `filterCapabilities()` we assign each found capability a score that is used to sort the capabilities from best match to worst. */ 127 | int index; /* The index in the capabilities vector when you call `getCapabilities()`. */ 128 | }; 129 | 130 | /* -------------------------------------- */ 131 | 132 | class CapabilityFilter { 133 | public: 134 | CapabilityFilter(int attribute, double value, int priority); 135 | ~CapabilityFilter(); 136 | void clear(); 137 | 138 | public: 139 | int attribute; 140 | double value; 141 | int priority; 142 | }; 143 | 144 | /* -------------------------------------- */ 145 | 146 | class Device { /* A device represents a capture device for the implementation. */ 147 | public: 148 | Device(); 149 | ~Device(); 150 | void clear(); /* Resets all members to defaults. */ 151 | public: 152 | int index; /* Used by the implementation. */ 153 | std::string name; /* Name of the device. */ 154 | }; 155 | 156 | /* -------------------------------------- */ 157 | 158 | class Format { /* A format is used to describe an output format into which the raw buffers from the webcam can be converted. This is often a feature from the OS, like the AVCaptureVideoDataOutput. */ 159 | public: 160 | Format(); /* Constructors; clears members. */ 161 | ~Format(); /* D'tor, clears members. */ 162 | void clear(); 163 | 164 | public: 165 | int format; /* The supported format, one of the CA_UYV*, CA_YUV*, CA_ARGB, etc.. formats. */ 166 | int index; /* Index to be used by the implementation. */ 167 | }; 168 | 169 | /* -------------------------------------- */ 170 | 171 | class Settings { /* A settings object is used to open a capture device. It describes what capability, device and output format (if any) you want to use. */ 172 | public: 173 | Settings(); /* C'tor; will reset all settings. */ 174 | ~Settings(); /* D'tor; will reset all settings. */ 175 | void clear(); /* Clear all settings. */ 176 | 177 | public: 178 | int capability; /* Number of the capability you want to use. See listCapabilities(). */ 179 | int device; /* Number of the device you want to use. See listDevices(). */ 180 | int format; /* The output format, e.g. CA_YUV422. This can be used when the capture SDK supports automatic conversion (mac/win). Some cameras capture in JPEG/H264 and the SDK can convert this to e.g. CA_YUYV422. Set the format here */ 181 | }; 182 | 183 | /* -------------------------------------- */ 184 | 185 | class Frame { /* A frame object can be used to store information about the frame data for a specific pixel format. */ 186 | public: 187 | Frame(); 188 | ~Frame(); 189 | void clear(); 190 | int set(int w, int h, int fmt); /* Set the format. This will set all members. */ 191 | 192 | public: 193 | std::vector width; /* Height per plane */ 194 | std::vector height; /* Width per plane */ 195 | std::vector stride; /* Stride per plane */ 196 | std::vector nbytes; /* Number of bytes per plane */ 197 | std::vector offset; /* Number of bytes into pixel data when the frame data is continuous */ 198 | }; 199 | 200 | /* -------------------------------------- */ 201 | 202 | }; // namespace ca 203 | 204 | #endif 205 | -------------------------------------------------------------------------------- /src/opengl_example.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | OpenGL example 4 | --------------- 5 | 6 | This file contains an example on how to use the capture library 7 | when using CA_YUV422 or CA_YUV420P, which are the default (optimal) 8 | pixel formats for most capture devices. This code was tested on 9 | Win 8.1, Mac 10.9 and Arch Linux. 10 | 11 | The code contains both the CA_YUV422 and CA_YUV420P code-flows 12 | and some things can be done to optimize the unpacking of pixel data. 13 | Though this will work fine in 99% of the cases. 14 | 15 | */ 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | #include 22 | 23 | #define ROXLU_USE_MATH 24 | #define ROXLU_USE_OPENGL 25 | #define ROXLU_IMPLEMENTATION 26 | #include 27 | 28 | #include 29 | using namespace ca; 30 | 31 | // Capture 32 | void on_signal(int sig); /* Signal handler, to correctly shutdown the capture process */ 33 | void on_frame(void* bytes, int nbytes, void* user); /* Gets called whenever new frame data is available */ 34 | 35 | Capture capture(on_frame, NULL); /* The capture object which does all the work */ 36 | Frame frame; /* Used to get information about the used pixel format, like bytes, planes, etc.. */ 37 | int pix_fmt = CA_NONE; /* The pixel format we want to use; is set below. */ 38 | int cap_width = 640; /* We try to query a capability with this height */ 39 | int cap_height = 480; /* We try to query a capability with this width */ 40 | GLuint prog = 0; /* Shader program */ 41 | GLuint vert = 0; /* The vertex shader */ 42 | GLuint frag = 0; /* The fragment shader */ 43 | GLuint vao = 0; /* Using attribute less renders still needs a vao */ 44 | GLuint tex0 = 0; /* Texture for GL_TEXTURE0 unit, used by YUYV422 and YUV420P (y-layer) */ 45 | GLuint tex1 = 0; /* Texture for GL_TEXTURE1 unit, used by YUV420P (u-layer) */ 46 | GLuint tex2 = 0; /* Texture for GL_TEXTURE2 unit, used by YUV420P (v-layer) */ 47 | unsigned char* pixels = NULL; /* Pointer to the pixels (non-planar) */ 48 | unsigned char* pixels_y = NULL; /* Pointer to pixels, y-channel, used with YUV420P */ 49 | unsigned char* pixels_u = NULL; /* Pointer to pixels, u-channel, used with YUV420P */ 50 | unsigned char* pixels_v = NULL; /* Pointer to pixels, v-channel, used with YUV420P */ 51 | bool must_update_pixels = false; /* Is set to true when we need to upload the pixels */ 52 | 53 | // Shaders 54 | static const char* CAPTURE_VS = "" 55 | "#version 330\n" 56 | "" 57 | "const vec2 pos[] = vec2[4](" 58 | " vec2(-1.0, 1.0), " 59 | " vec2(-1.0, -1.0), " 60 | " vec2( 1.0, 1.0), " 61 | " vec2( 1.0, -1.0) " 62 | ");" 63 | "" 64 | " const vec2[] tex = vec2[4]( " 65 | " vec2(0.0, 0.0), " 66 | " vec2(0.0, 1.0), " 67 | " vec2(1.0, 0.0), " 68 | " vec2(1.0, 1.0) " 69 | ");" 70 | "" 71 | "out vec2 v_texcoord; " 72 | "" 73 | "void main() { " 74 | " gl_Position = vec4(pos[gl_VertexID], 0.0, 1.0);" 75 | " v_texcoord = tex[gl_VertexID];" 76 | "}" 77 | ""; 78 | 79 | // Decode YUV422 data (Y0 Cb Y1 Cr) 80 | static const char* CAPTURE_YUYV422_FS = "" 81 | "#version 330\n" 82 | "" 83 | "uniform sampler2D u_tex;" 84 | "layout( location = 0 ) out vec4 fragcolor; " 85 | "in vec2 v_texcoord;" 86 | "" 87 | "const vec3 R_cf = vec3(1.164383, 0.000000, 1.596027);" 88 | "const vec3 G_cf = vec3(1.164383, -0.391762, -0.812968);" 89 | "const vec3 B_cf = vec3(1.164383, 2.017232, 0.000000);" 90 | "const vec3 offset = vec3(-0.0625, -0.5, -0.5);" 91 | "" 92 | "void main() {" 93 | "" 94 | " int width = textureSize(u_tex, 0).x * 2;" 95 | " float tex_x = v_texcoord.x; " 96 | " int pixel = int(floor(width * tex_x)) % 2; " 97 | " vec4 tc = texture( u_tex, v_texcoord ).rgba;" 98 | "" 99 | " float cr = tc.r; " 100 | " float y2 = tc.g; " 101 | " float cb = tc.b; " 102 | " float y1 = tc.a; " 103 | "" 104 | " float y = (pixel == 1) ? y2 : y1; " 105 | " vec3 yuv = vec3(y, cb, cr);" 106 | " yuv += offset;" 107 | "" 108 | " fragcolor.r = dot(yuv, R_cf);" 109 | " fragcolor.g = dot(yuv, G_cf);" 110 | " fragcolor.b = dot(yuv, B_cf);" 111 | " fragcolor.a = 1.0;" 112 | "}" 113 | ""; 114 | 115 | // Decode YUV420P (3 planes) 116 | static const char* CAPTURE_YUV420P_FS = "" 117 | "#version 330\n" 118 | "uniform sampler2D y_tex;" 119 | "uniform sampler2D u_tex;" 120 | "uniform sampler2D v_tex;" 121 | "in vec2 v_texcoord;" 122 | "layout( location = 0 ) out vec4 fragcolor;" 123 | "" 124 | "const vec3 R_cf = vec3(1.164383, 0.000000, 1.596027);" 125 | "const vec3 G_cf = vec3(1.164383, -0.391762, -0.812968);" 126 | "const vec3 B_cf = vec3(1.164383, 2.017232, 0.000000);" 127 | "const vec3 offset = vec3(-0.0625, -0.5, -0.5);" 128 | "" 129 | "void main() {" 130 | " float y = texture(y_tex, v_texcoord).r;" 131 | " float u = texture(u_tex, v_texcoord).r;" 132 | " float v = texture(v_tex, v_texcoord).r;" 133 | " vec3 yuv = vec3(y,u,v);" 134 | " yuv += offset;" 135 | " fragcolor = vec4(0.0, 0.0, 0.0, 1.0);" 136 | " fragcolor.r = dot(yuv, R_cf);" 137 | " fragcolor.g = dot(yuv, G_cf);" 138 | " fragcolor.b = dot(yuv, B_cf);" 139 | "}" 140 | ""; 141 | 142 | // GLFW callbacks 143 | void button_callback(GLFWwindow* win, int bt, int action, int mods); 144 | void cursor_callback(GLFWwindow* win, double x, double y); 145 | void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods); 146 | void char_callback(GLFWwindow* win, unsigned int key); 147 | void error_callback(int err, const char* desc); 148 | void resize_callback(GLFWwindow* window, int width, int height); 149 | 150 | int main() { 151 | 152 | signal(SIGINT, on_signal); 153 | 154 | glfwSetErrorCallback(error_callback); 155 | 156 | if(!glfwInit()) { 157 | printf("Error: cannot setup glfw.\n"); 158 | return false; 159 | } 160 | 161 | glfwWindowHint(GLFW_SAMPLES, 4); 162 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 163 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); 164 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 165 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 166 | 167 | GLFWwindow* win = NULL; 168 | int w = cap_width; 169 | int h = cap_height; 170 | 171 | win = glfwCreateWindow(w, h, "Capture", NULL, NULL); 172 | if(!win) { 173 | glfwTerminate(); 174 | exit(EXIT_FAILURE); 175 | } 176 | 177 | glfwSetFramebufferSizeCallback(win, resize_callback); 178 | glfwSetKeyCallback(win, key_callback); 179 | glfwSetCharCallback(win, char_callback); 180 | glfwSetCursorPosCallback(win, cursor_callback); 181 | glfwSetMouseButtonCallback(win, button_callback); 182 | glfwMakeContextCurrent(win); 183 | glfwSwapInterval(1); 184 | 185 | if (!gladLoadGL()) { 186 | printf("Cannot load GL.\n"); 187 | exit(1); 188 | } 189 | 190 | // ---------------------------------------------------------------- 191 | // THIS IS WHERE YOU START CALLING OPENGL FUNCTIONS, NOT EARLIER!! 192 | // ---------------------------------------------------------------- 193 | int capability_index = -1; 194 | int format_index = -1; 195 | 196 | capture.listCapabilities(0); 197 | 198 | #if defined(__APPLE__) || defined(__linux) 199 | pix_fmt = CA_YUYV422; 200 | #elif defined(_WIN32) 201 | pix_fmt = CA_YUV420P; 202 | #endif 203 | 204 | // Find the capability 205 | capability_index = capture.findCapability(0, cap_width, cap_height, pix_fmt); 206 | if(capability_index < 0) { 207 | capture.listCapabilities(0); 208 | printf("Error: cannot find a capability for 640x480.\n"); 209 | ::exit(EXIT_FAILURE); 210 | } 211 | 212 | // Check what output formats are supports (not all OSes support these) 213 | //capture.listOutputFormats(); 214 | 215 | // Create the settings param 216 | Settings settings; 217 | settings.device = 0; 218 | settings.capability = capability_index; 219 | settings.format = format_index; 220 | 221 | // Open the capture device 222 | if(capture.open(settings) < 0) { 223 | printf("Error: cannot open the device.\n"); 224 | ::exit(EXIT_FAILURE); 225 | } 226 | 227 | // Create the pixel buffer, we use Frame to get info about the size of the data for the current pixel format 228 | if(frame.set(cap_width, cap_height, pix_fmt) < 0) { 229 | printf("Error: cannot get frame information for the current pixel format.\n"); 230 | capture.close(); 231 | ::exit(EXIT_FAILURE); 232 | } 233 | 234 | // Create the GL objects we use to render 235 | glGenVertexArrays(1, &vao); 236 | glBindVertexArray(vao); 237 | vert = rx_create_shader(GL_VERTEX_SHADER, CAPTURE_VS); 238 | 239 | // Get the correct fragment shader 240 | if(pix_fmt == CA_YUYV422) { 241 | frag = rx_create_shader(GL_FRAGMENT_SHADER, CAPTURE_YUYV422_FS); 242 | } 243 | else if(pix_fmt == CA_YUV420P) { 244 | frag = rx_create_shader(GL_FRAGMENT_SHADER, CAPTURE_YUV420P_FS); 245 | } 246 | 247 | prog = rx_create_program(vert, frag); 248 | glLinkProgram(prog); 249 | rx_print_shader_link_info(prog); 250 | glUseProgram(prog); 251 | 252 | if(pix_fmt == CA_YUYV422) { 253 | 254 | pixels = new unsigned char[frame.nbytes[0]]; 255 | 256 | // CA_YUYV422 texture 257 | glUniform1i(glGetUniformLocation(prog, "u_tex"), 0); 258 | 259 | glGenTextures(1, &tex0); 260 | glBindTexture(GL_TEXTURE_2D, tex0); 261 | 262 | #if defined(__APPLE__) 263 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, frame.width[0], frame.height[0], 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels); 264 | #elif defined(__linux) 265 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, frame.width[0], frame.height[0], 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, pixels); 266 | #endif 267 | 268 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 269 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 270 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 271 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 272 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); 273 | } 274 | else if(pix_fmt == CA_YUV420P) { 275 | 276 | pixels_y = new unsigned char[frame.nbytes[0]]; 277 | pixels_u = new unsigned char[frame.nbytes[1]]; 278 | pixels_v = new unsigned char[frame.nbytes[2]]; 279 | 280 | // CA_YUV420P textures 281 | glUniform1i(glGetUniformLocation(prog, "y_tex"), 0); 282 | glUniform1i(glGetUniformLocation(prog, "u_tex"), 1); 283 | glUniform1i(glGetUniformLocation(prog, "v_tex"), 2); 284 | 285 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 286 | 287 | // y-channel 288 | glGenTextures(1, &tex0); 289 | glBindTexture(GL_TEXTURE_2D, tex0); 290 | glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, frame.width[0], frame.height[0], 0, GL_RED, GL_UNSIGNED_BYTE, pixels_y); 291 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 292 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 293 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 294 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 295 | 296 | // u-channel 297 | glGenTextures(1, &tex1); 298 | glBindTexture(GL_TEXTURE_2D, tex1); 299 | glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, frame.width[1], frame.height[1], 0, GL_RED, GL_UNSIGNED_BYTE, pixels_u); 300 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 301 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 302 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 303 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 304 | 305 | // v-channel 306 | glGenTextures(1, &tex2); 307 | glBindTexture(GL_TEXTURE_2D, tex2); 308 | glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, frame.width[2], frame.height[2], 0, GL_RED, GL_UNSIGNED_BYTE, pixels_v); 309 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 310 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 311 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 312 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 313 | } 314 | 315 | // Start captureing 316 | if(capture.start() < 0) { 317 | printf("Error: cannot start capture.\n"); 318 | capture.close(); 319 | ::exit(EXIT_FAILURE); 320 | } 321 | 322 | while(!glfwWindowShouldClose(win)) { 323 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 324 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 325 | 326 | capture.update(); 327 | 328 | if(must_update_pixels) { 329 | 330 | // Update YUYV422 331 | if(pix_fmt == CA_YUYV422) { 332 | 333 | glBindTexture(GL_TEXTURE_2D, tex0); 334 | 335 | #if defined(__APPLE__) 336 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frame.width[0], frame.height[0], GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pixels); 337 | #elif defined(__linux) 338 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frame.width[0], frame.height[0], GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, pixels); 339 | #endif 340 | } 341 | // Update YUV420P 342 | else if(pix_fmt == CA_YUV420P) { 343 | glBindTexture(GL_TEXTURE_2D, tex0); 344 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frame.width[0], frame.height[0], GL_RED, GL_UNSIGNED_BYTE, pixels_y); 345 | 346 | glBindTexture(GL_TEXTURE_2D, tex1); 347 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frame.width[1], frame.height[1], GL_RED, GL_UNSIGNED_BYTE, pixels_u); 348 | 349 | glBindTexture(GL_TEXTURE_2D, tex2); 350 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frame.width[2], frame.height[2], GL_RED, GL_UNSIGNED_BYTE, pixels_v); 351 | } 352 | must_update_pixels = false; 353 | } 354 | 355 | if(pix_fmt == CA_YUYV422) { 356 | glActiveTexture(GL_TEXTURE0); 357 | glBindTexture(GL_TEXTURE_2D, tex0); 358 | } 359 | else if(pix_fmt == CA_YUV420P) { 360 | glActiveTexture(GL_TEXTURE0); 361 | glBindTexture(GL_TEXTURE_2D, tex0); 362 | 363 | glActiveTexture(GL_TEXTURE1); 364 | glBindTexture(GL_TEXTURE_2D, tex1); 365 | 366 | glActiveTexture(GL_TEXTURE2); 367 | glBindTexture(GL_TEXTURE_2D, tex2); 368 | } 369 | 370 | glUseProgram(prog); 371 | glBindVertexArray(vao); 372 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 373 | 374 | glfwSwapBuffers(win); 375 | glfwPollEvents(); 376 | } 377 | 378 | if(capture.stop() < 0) { 379 | printf("Error: cannot stop capture.\n"); 380 | capture.close(); 381 | ::exit(EXIT_FAILURE); 382 | } 383 | 384 | if(capture.close() < 0) { 385 | printf("Error: cannot close the device.\n"); 386 | ::exit(EXIT_FAILURE); 387 | } 388 | glfwTerminate(); 389 | 390 | return EXIT_SUCCESS; 391 | } 392 | 393 | void char_callback(GLFWwindow* win, unsigned int key) { 394 | } 395 | 396 | void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods) { 397 | 398 | if(action != GLFW_PRESS) { 399 | return; 400 | } 401 | 402 | if(action == GLFW_PRESS) { 403 | } 404 | else if(action == GLFW_RELEASE) { 405 | } 406 | 407 | switch(key) { 408 | case GLFW_KEY_SPACE: { 409 | break; 410 | } 411 | case GLFW_KEY_ESCAPE: { 412 | glfwSetWindowShouldClose(win, GL_TRUE); 413 | break; 414 | } 415 | }; 416 | } 417 | 418 | void resize_callback(GLFWwindow* window, int width, int height) { 419 | } 420 | 421 | void cursor_callback(GLFWwindow* win, double x, double y) { 422 | } 423 | 424 | void button_callback(GLFWwindow* win, int bt, int action, int mods) { 425 | 426 | double x,y; 427 | 428 | if(action == GLFW_PRESS || action == GLFW_REPEAT) { 429 | glfwGetCursorPos(win, &x, &y); 430 | } 431 | 432 | if(action == GLFW_PRESS) { 433 | } 434 | else if(action == GLFW_RELEASE) { 435 | } 436 | } 437 | 438 | void error_callback(int err, const char* desc) { 439 | printf("GLFW error: %s (%d)\n", desc, err); 440 | } 441 | 442 | void on_frame(void* bytes, int nbytes, void* user) { 443 | // This function might be called from a different thread so we copy it over and change a flag. 444 | // In the event/draw loop we will update the GL textures from the main thread. 445 | if(pix_fmt == CA_YUYV422) { 446 | memcpy(pixels, (char*)bytes, nbytes); 447 | } 448 | else if(pix_fmt == CA_YUV420P) { 449 | memcpy(pixels_y, ((char*)bytes) + frame.offset[0], frame.nbytes[0]); 450 | memcpy(pixels_u, ((char*)bytes) + frame.offset[1], frame.nbytes[1]); 451 | memcpy(pixels_v, ((char*)bytes) + frame.offset[2], frame.nbytes[2]); 452 | } 453 | must_update_pixels = true; 454 | } 455 | 456 | void on_signal(int sig) { 457 | capture.stop(); 458 | capture.close(); 459 | ::exit(EXIT_FAILURE); 460 | } 461 | --------------------------------------------------------------------------------