/* This file is a part of plaYUVer project * Copyright (C) 2014-2017 by Luis Lucas (luisfrlucas@gmail.com) * Joao Carreira (jfmcarreira@gmail.com) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ /** * \file StreamHandlerOpenCV.cpp * \brief Interface with opencv lib */ // Self #include "StreamHandlerOpenCV.h" // System #include #include // Own #include "LibMemory.h" #include "PlaYUVerFrame.h" #include "PlaYUVerFramePixelFormats.h" using cv::Mat; using cv::VideoCapture; std::vector StreamHandlerOpenCV::supportedReadFormats() { INI_REGIST_PLAYUVER_SUPPORTED_FMT; #if( CV_MAJOR_VERSION >= 3 ) REGIST_PLAYUVER_SUPPORTED_ABSTRACT_FMT( &StreamHandlerOpenCV::Create, "Device", "/dev/video*" ); #endif END_REGIST_PLAYUVER_SUPPORTED_FMT; } std::vector StreamHandlerOpenCV::supportedWriteFormats() { INI_REGIST_PLAYUVER_SUPPORTED_FMT; REGIST_PLAYUVER_SUPPORTED_FMT( &StreamHandlerOpenCV::Create, "Portable PixMap ", "ppm" ); REGIST_PLAYUVER_SUPPORTED_FMT( &StreamHandlerOpenCV::Create, "Portable Network Graphics", "png" ); REGIST_PLAYUVER_SUPPORTED_FMT( &StreamHandlerOpenCV::Create, "Joint Photographic Experts Group", "jpg" ); REGIST_PLAYUVER_SUPPORTED_FMT( &StreamHandlerOpenCV::Create, "Windows Bitmap", "bmp" ); END_REGIST_PLAYUVER_SUPPORTED_FMT; } StreamHandlerOpenCV::StreamHandlerOpenCV() { pcVideoCapture = NULL; } Bool StreamHandlerOpenCV::openHandler( String strFilename, Bool bInput ) { m_cFilename = strFilename; if( bInput ) { #if( CV_VERSION_MAJOR >= 3 ) /* * Special filename to handle webcam input */ if( m_cFilename.find( "/dev/video" ) != String::npos ) { Int iDeviceId = 0; if( m_cFilename.find( "/dev/video0" ) != String::npos ) iDeviceId = 0; else if( m_cFilename.find( "/dev/video1" ) != String::npos ) iDeviceId = 1; m_strFormatName = "DEV"; m_strCodecName = "Raw Video"; pcVideoCapture = new VideoCapture( iDeviceId ); m_uiWidth = pcVideoCapture->get( CV_CAP_PROP_FRAME_WIDTH ); m_uiHeight = pcVideoCapture->get( CV_CAP_PROP_FRAME_HEIGHT ); m_dFrameRate = 25; } else #endif { m_strCodecName = m_strFormatName = uppercase( strFilename.substr( strFilename.find_last_of( "." ) + 1 ) ); Mat cvMat = cv::imread( m_cFilename ); m_uiWidth = cvMat.cols; m_uiHeight = cvMat.rows; m_dFrameRate = 1; } m_uiBitsPerPixel = 8; m_iPixelFormat = PlaYUVerFrame::findPixelFormat( "BGR" ); } return true; } Void StreamHandlerOpenCV::closeHandler() { if( pcVideoCapture ) { pcVideoCapture->release(); delete pcVideoCapture; pcVideoCapture = NULL; } } Bool StreamHandlerOpenCV::configureBuffer( PlaYUVerFrame* pcFrame ) { return true; } UInt64 StreamHandlerOpenCV::calculateFrameNumber() { if( pcVideoCapture ) { return 2; } return 1; } Bool StreamHandlerOpenCV::seek( UInt64 iFrameNum ) { // m_uiCurrFrameFileIdx = return true; } Bool StreamHandlerOpenCV::read( PlaYUVerFrame* pcFrame ) { Bool bRet = false; if( pcVideoCapture ) { Mat cvMat; bRet = pcVideoCapture->read( cvMat ); if( bRet ) bRet = pcFrame->fromMat( cvMat ); } else { Mat cvMat = cv::imread( m_cFilename ); bRet = pcFrame->fromMat( cvMat ); } if( bRet ) m_uiCurrFrameFileIdx++; return bRet; } Bool StreamHandlerOpenCV::write( PlaYUVerFrame* pcFrame ) { Bool bRet = false; Mat cvFrame; bRet = pcFrame->toMat( cvFrame ); if( bRet ) { bRet = cv::imwrite( m_cFilename, cvFrame ); } return bRet; }