/* This file is a part of plaYUVer project * Copyright (C) 2014-2015 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 PlaYUVerFrame.h * \ingroup PlaYUVerLib * \brief Video Frame handling */ #ifndef __PLAYUVERFRAME_H__ #define __PLAYUVERFRAME_H__ #include #include #include #include #include "PlaYUVerDefs.h" #include "PlaYUVerFrameStats.h" class PixFcSSE; namespace cv { class Mat; } namespace plaYUVer { struct PlaYUVerPixFmtDescriptor; #define MAX_NUMBER_COMPONENTS 4 /** * \class PlaYUVerFrame * \ingroup PlaYUVerLib PlaYUVerLib_Frame * \brief Frame handling class */ class PlaYUVerFrame: public PlaYUVerFrameStats { public: /** * \class Pixel * \ingroup PlaYUVerLib PlaYUVerLib_Frame * \brief Pel handling class */ class Pixel { private: Int m_iColorSpace; Pel PixelComponents[3]; public: Pixel() { m_iColorSpace = PlaYUVerFrame::COLOR_INVALID; PixelComponents[0] = 0; PixelComponents[1] = 0; PixelComponents[2] = 0; } Pixel( Int ColorSpace, Pel c0, Pel c1, Pel c2 ) { m_iColorSpace = ColorSpace == PlaYUVerFrame::COLOR_GRAY ? PlaYUVerFrame::COLOR_YUV : ColorSpace; PixelComponents[0] = c0; PixelComponents[1] = c1; PixelComponents[2] = c2; } Int ColorSpace() { return m_iColorSpace; } Pel* Components() { return PixelComponents; } Pel& Y() { return PixelComponents[0]; } Pel& Cb() { return PixelComponents[1]; } Pel& Cr() { return PixelComponents[2]; } Pel& R() { return PixelComponents[0]; } Pel& G() { return PixelComponents[1]; } Pel& B() { return PixelComponents[2]; } }; /** ColorSpace Enum * List of supported color spaces */ enum ColorSpace { COLOR_INVALID = -1, //!< Invalid COLOR_YUV = 0, //!< YUV COLOR_RGB = 1, //!< RGB COLOR_ARGB = 3, //!< RGB + Alpha COLOR_GRAY = 2, //!< Grayscale }; /** ColorSpace Enum * List of supported pixel formats (deprecated) */ enum PixelFormats { NO_FMT = -1, YUV420p = 0, YUV444p, YUV422p, YUYV422, GRAY, RGB24, BRG24, NUMBER_FORMATS }; /** * Function that handles the supported color space * of PlaYUVerFrame * @return vector of strings with pixel formats names */ static std::vector supportedColorSpacesListNames(); /** * Function that handles the supported pixel formats * of PlaYUVerFrame * @return vector of strings with pixel formats names */ static std::vector supportedPixelFormatListNames(); static std::vector supportedPixelFormatListNames( Int colorSpace ); /** * Get number of bytes per frame of a specific * pixel format * @return number of bytes per frame */ static UInt64 getBytesPerFrame( UInt uiWidth, UInt uiHeight, Int iPixelFormat, Int bitsPixel ); /** * Convert a Pixel to a new color space * @param inputPixel input pixel (PlaYUVerFrame::Pixel) * @param eOutputSpace output color space * @return converted pixel */ static PlaYUVerFrame::Pixel ConvertPixel( Pixel inputPixel, ColorSpace eOutputSpace ); /** * Creates a new frame using the following configuration * * @param width width of the frame * @param height height of the frame * @param pel_format pixel format index (always use PixelFormats enum) * * @note this function might misbehave if the pixel format enum is not correct */ PlaYUVerFrame( UInt width, UInt height, Int pelFormat = 0, Int bitsPixel = 8 ); /** * Creates and new frame with the configuration of an * existing one and copy its contents * * @param other existing frame to copy from */ PlaYUVerFrame( PlaYUVerFrame * ); /** * Creates and new frame with the configuration of an * existing one and copy its contents. * This function only copies a specific region from the existing frame * * @param other existing frame to copy from * @param posX position X to crop from * @param posY position Y to crop from * @param areaWidth crop width * @param areaHeight crop height */ PlaYUVerFrame( PlaYUVerFrame *other, UInt posX, UInt posY, UInt areaWidth, UInt areaHeight ); ~PlaYUVerFrame(); /** * Get number of bytes per frame of an existing frame * @return number of bytes per frame */ UInt64 getBytesPerFrame(); Int getColorSpace() const; UInt getNumberChannels() const; UInt getChromaWidth() const; UInt getChromaHeight() const; UInt getChromaLength() const; Void frameFromBuffer( Byte*, UInt64 ); Void frameToBuffer( Byte* ); Void fillRGBBuffer(); Void copyFrom( PlaYUVerFrame* ); Void copyFrom( PlaYUVerFrame*, UInt, UInt ); PlaYUVerFrame::Pixel getPixelValue( Int xPos, Int yPos, ColorSpace eColorSpace = COLOR_INVALID ); std::string getPelFmtName() { return m_cPelFmtName; } UInt getWidth() const { return m_uiWidth; } UInt getHeight() const { return m_uiHeight; } Int getPelFormat() const { return m_iPixelFormat; } Int getBitsPel() const { return m_iBitsPel; } Bool isValid() const { return ( m_uiWidth > 0 ) && ( m_uiHeight > 0 ) && ( m_iPixelFormat >= 0 ); } Bool haveSameFmt( PlaYUVerFrame* other ) const { if( other ) { return ( m_uiWidth == other->getWidth() ) && ( m_uiHeight == other->getHeight() ) && ( m_iPixelFormat == other->getPelFormat() ); } else { return false; } } Pel*** getPelBufferYUV() const { return m_pppcInputPel; } Pel*** getPelBufferYUV() { m_bHasHistogram = false; m_bHasRGBPel = false; return m_pppcInputPel; } UChar* getRGBBuffer() const { if( m_bHasRGBPel ) { return m_pcARGB32; } return NULL; } UChar* getRGBBuffer() { if( !m_bHasRGBPel ) { fillRGBBuffer(); } return m_pcARGB32; } /** * Interface with OpenCV lib */ cv::Mat* getCvMat( Bool convertToGray = false ); Void fromCvMat( cv::Mat* ); /** * @defgroup PlaYUVerLib_QualityMetrics Quality Metrics Interface * @{ * \ingroup PlaYUVerLib * Quality metrics interface * * @} */ //! @ingroup PlaYUVerLib_QualityMetrics //! @{ enum QualityMetrics { NO_METRIC = -1, PSNR_METRIC = 0, MSE_METRIC, SSIM_METRIC, NUMBER_METRICS, }; static std::vector supportedQualityMetricsList() { std::vector metrics; metrics.push_back( "PSNR" ); metrics.push_back( "MSE" ); metrics.push_back( "SSIM" ); return metrics; } Double getQuality( Int Metric, PlaYUVerFrame* Org, Int component ); Double getMSE( PlaYUVerFrame* Org, Int component ); Double getPSNR( PlaYUVerFrame* Org, Int component ); Double getSSIM( PlaYUVerFrame* Org, Int component ); //! @} private: //! Strcut with the pixel format description. PlaYUVerPixFmtDescriptor* m_pcPelFormat; std::string m_cPelFmtName; UInt m_uiWidth; //!< Width of the frame UInt m_uiHeight; //!< Height of the frame Int m_iPixelFormat; //!< Pixel format number (it follows the list of supported pixel formats) Int m_iNumberChannels; //!< Number of channels Int m_iBitsPel; //!< Bits per pixel/channel Pel*** m_pppcInputPel; Bool m_bHasRGBPel; //!< Flag indicating that the ARGB buffer was computed UChar* m_pcARGB32; //!< Buffer with the ARGB pixels used in Qt libs /** * Common constructor function of a frame * * @param width width of the frame * @param height height of the frame * @param pel_format pixel format index (always use PixelFormats enum) * */ Void init( UInt width, UInt height, Int pel_format, Int bitsPixel ); Void openPixfc(); Void closePixfc(); PixFcSSE* m_pcPixfc; Void FrametoRGB8Pixfc(); }; } // NAMESPACE #endif // __PLAYUVERFRAME_H__