From 3dfc1de0424fbb804fd4066b0873eea6d45f1433 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Mon, 10 Nov 2014 22:55:09 +0000 Subject: [PATCH] Improve the interface for the different pixel formats Unify the get pixel function Complete the interface Change some defines from PlaYUVerDefs.h Add a class to represent a pixel Improve LibAvContextHandle --- source/app/PlaYUVerFrameStatistics.cpp | 9 +- source/app/ViewArea.cpp | 18 ++-- source/app/plaYUVerApp.cpp | 4 +- source/lib/PlaYUVerDefs.h | 38 +++---- source/lib/PlaYUVerFrame.cpp | 83 +++++++++------ source/lib/PlaYUVerFrame.h | 48 +++++++-- source/lib/PlaYUVerFramePixelFormats.cpp | 123 ++++++++++------------- source/lib/PlaYUVerFramePixelFormats.h | 3 +- source/lib/PlaYUVerStream.cpp | 9 +- source/lib/ffmpeg/LibAvContextHandle.cpp | 44 ++++---- source/lib/ffmpeg/LibAvContextHandle.h | 9 +- 11 files changed, 211 insertions(+), 177 deletions(-) diff --git a/source/app/PlaYUVerFrameStatistics.cpp b/source/app/PlaYUVerFrameStatistics.cpp index 20f3027..f8c17d3 100644 --- a/source/app/PlaYUVerFrameStatistics.cpp +++ b/source/app/PlaYUVerFrameStatistics.cpp @@ -343,13 +343,8 @@ void PlaYUVerFrameStatistics::calcHistogramValues() } if( d->calcLuma && ( d->imageColorSpace == PlaYUVerFrame::COLOR_RGB || d->imageColorSpace == PlaYUVerFrame::COLOR_ARGB ) ) { - Pixel pixel_value = - { - d->imageColorSpace, - *( data[COLOR_R] ), - *( data[COLOR_G] ), - *( data[COLOR_B] ) }; - luma = PlaYUVerFrame::ConvertPixel( pixel_value, PlaYUVerFrame::COLOR_YUV ).Luma; + PlaYUVerFrame::Pixel currPixel( d->imageColorSpace, *( data[COLOR_R] ), *( data[COLOR_G] ), *( data[COLOR_B] ) ); + luma = PlaYUVerFrame::ConvertPixel( currPixel, PlaYUVerFrame::COLOR_YUV ).Y(); d->histogram[luma + j * d->histoSegments]++; } for( j = 0; j < d->imageChannels; j++ ) diff --git a/source/app/ViewArea.cpp b/source/app/ViewArea.cpp index 40848bb..0579095 100644 --- a/source/app/ViewArea.cpp +++ b/source/app/ViewArea.cpp @@ -355,7 +355,7 @@ void ViewArea::paintEvent( QPaintEvent *event ) { Int imageWidth = m_pixmap.width(); Int imageHeight = m_pixmap.height(); - Pixel sPixelValue; + PlaYUVerFrame::Pixel sPixelValue; QFont font( "Helvetica" ); font.setPixelSize( 12 ); @@ -378,39 +378,39 @@ void ViewArea::paintEvent( QPaintEvent *event ) { sPixelValue = m_pcCurrFrame->getPixelValue( pixelTopLeft.x(), pixelTopLeft.y(), PlaYUVerFrame::COLOR_YUV ); - if( sPixelValue.Luma < 128 ) + if( sPixelValue.Y() < 128 ) painter.setPen( QColor( Qt::white ) ); else painter.setPen( QColor( Qt::black ) ); painter.drawText( pixelRect, Qt::AlignCenter, - "Y: " + QString::number( sPixelValue.Luma ) + "\n" + "U: " + QString::number( sPixelValue.ChromaU ) + "\n" + "V: " - + QString::number( sPixelValue.ChromaV ) ); + "Y: " + QString::number( sPixelValue.Y() ) + "\n" + "U: " + QString::number( sPixelValue.Cb() ) + "\n" + "V: " + + QString::number( sPixelValue.Cr() ) ); } if( frFormat == PlaYUVerFrame::COLOR_GRAY ) { sPixelValue = m_pcCurrFrame->getPixelValue( pixelTopLeft.x(), pixelTopLeft.y(), PlaYUVerFrame::COLOR_YUV ); - if( sPixelValue.Luma < 128 ) + if( sPixelValue.Y() < 128 ) painter.setPen( QColor( Qt::white ) ); else painter.setPen( QColor( Qt::black ) ); - painter.drawText( pixelRect, Qt::AlignCenter, "Y: " + QString::number( sPixelValue.Luma ) ); + painter.drawText( pixelRect, Qt::AlignCenter, "Y: " + QString::number( sPixelValue.Y() ) ); } if( ( frFormat == PlaYUVerFrame::COLOR_RGB ) ) { sPixelValue = m_pcCurrFrame->getPixelValue( pixelTopLeft.x(), pixelTopLeft.y(), PlaYUVerFrame::COLOR_RGB ); - if( ( sPixelValue.ColorR + sPixelValue.ColorG + sPixelValue.ColorB ) < 128 * 3 ) + if( ( sPixelValue.R() + sPixelValue.G() + sPixelValue.B() ) < 128 * 3 ) painter.setPen( QColor( Qt::white ) ); else painter.setPen( QColor( Qt::black ) ); painter.drawText( pixelRect, Qt::AlignCenter, - "R: " + QString::number( sPixelValue.ColorR ) + "\n" + "G: " + QString::number( sPixelValue.ColorG ) + "\n" + "B: " - + QString::number( sPixelValue.ColorB ) ); + "R: " + QString::number( sPixelValue.R() ) + "\n" + "G: " + QString::number( sPixelValue.G() ) + "\n" + "B: " + + QString::number( sPixelValue.B() ) ); } } } diff --git a/source/app/plaYUVerApp.cpp b/source/app/plaYUVerApp.cpp index bf2b97b..22338a9 100644 --- a/source/app/plaYUVerApp.cpp +++ b/source/app/plaYUVerApp.cpp @@ -914,7 +914,7 @@ Void plaYUVerApp::chageSubWindowSelection() Void plaYUVerApp::updatePixelValueStatusBar( const QPoint & pos, PlaYUVerFrame* frame ) { - Pixel sPixelValue; + PlaYUVerFrame::Pixel sPixelValue; Int iWidth, iHeight; Int posX = pos.x(), posY = pos.y(); QString strPixel; @@ -927,7 +927,7 @@ Void plaYUVerApp::updatePixelValueStatusBar( const QPoint & pos, PlaYUVerFrame* if( ( posX < iWidth ) && ( posX >= 0 ) && ( posY < iHeight ) && ( posY >= 0 ) ) { sPixelValue = curFrame->getPixelValue( pos.x(), pos.y(), PlaYUVerFrame::COLOR_YUV ); - strPixel = QString( "Y: %1 U: %2 V: %3" ).arg( sPixelValue.Luma ).arg( sPixelValue.ChromaU ).arg( sPixelValue.ChromaV ); + strPixel = QString( "Y: %1 U: %2 V: %3" ).arg( sPixelValue.Y() ).arg( sPixelValue.Cb() ).arg( sPixelValue.Cr() ); strStatus.append( strPixel ); statusBar()->showMessage( strStatus, 3000 ); } diff --git a/source/lib/PlaYUVerDefs.h b/source/lib/PlaYUVerDefs.h index fb27988..96d64c7 100644 --- a/source/lib/PlaYUVerDefs.h +++ b/source/lib/PlaYUVerDefs.h @@ -126,25 +126,25 @@ enum RGBcomponent COLOR_B, }; -typedef struct __pixel_definition -{ - Int color_space; - union - { - Pel Luma; - Pel ColorR; - }; - union - { - Pel ChromaU; - Pel ColorG; - }; - union - { - Pel ChromaV; - Pel ColorB; - }; -}Pixel; +//typedef struct __pixel_definition +//{ +// Int color_space; +// union +// { +// Pel Luma; +// Pel ColorR; +// }; +// union +// { +// Pel ChromaU; +// Pel ColorG; +// }; +// union +// { +// Pel ChromaV; +// Pel ColorB; +// }; +//}Pixel; } // NAMESPACE diff --git a/source/lib/PlaYUVerFrame.cpp b/source/lib/PlaYUVerFrame.cpp index d9d2ca8..70df3c1 100644 --- a/source/lib/PlaYUVerFrame.cpp +++ b/source/lib/PlaYUVerFrame.cpp @@ -40,9 +40,6 @@ namespace plaYUVer { - - - Int PlaYUVerFrame::isRGBorYUVorGray( Int pixel_format ) { switch( pixel_format ) @@ -225,16 +222,16 @@ UInt PlaYUVerFrame::getChromaLength() const return getChromaWidth() * getChromaHeight(); } -Void PlaYUVerFrame::FrameFromBuffer( Pel *input_buffer, Int pel_format ) +Void PlaYUVerFrame::frameFromBuffer( Pel *Buff, UInt64 uiBuffSize ) { - if( m_iPixelFormat != pel_format ) + if( uiBuffSize != getBytesPerFrame() ) return; - m_pcPelFormat->frameFromBuffer( input_buffer, m_pppcInputPel, m_uiWidth, m_uiHeight ); + m_pcPelFormat->frameFromBuffer( Buff, m_pppcInputPel, m_uiWidth, m_uiHeight ); m_bHasRGBPel = false; fillRGBBuffer(); } -Void PlaYUVerFrame::FrameToBuffer( Pel *output_buffer ) +Void PlaYUVerFrame::frameToBuffer( Pel *output_buffer ) { m_pcPelFormat->bufferFromFrame( m_pppcInputPel, output_buffer, m_uiWidth, m_uiHeight ); } @@ -261,7 +258,7 @@ Void PlaYUVerFrame::copyFrom( PlaYUVerFrame* input_frame, UInt xPos, UInt yPos ) return; m_bHasRGBPel = false; Pel ***pInput = input_frame->getPelBufferYUV(); - for( Int ch = 0; ch < m_pcPelFormat->numberChannels; ch++ ) + for( UInt ch = 0; ch < m_pcPelFormat->numberChannels; ch++ ) { Int ratioH = ch > 0 ? m_pcPelFormat->ratioChromaHeight : 1; Int ratioW = ch > 0 ? m_pcPelFormat->ratioChromaWidth : 1; @@ -272,41 +269,41 @@ Void PlaYUVerFrame::copyFrom( PlaYUVerFrame* input_frame, UInt xPos, UInt yPos ) } } -Pixel PlaYUVerFrame::getPixelValue( Int xPos, Int yPos, Int eColorSpace ) +PlaYUVerFrame::Pixel PlaYUVerFrame::getPixelValue( Int xPos, Int yPos, Int eColorSpace ) { - Pixel PelValue = m_pcPelFormat->getPixelValue( m_pppcInputPel, xPos, yPos ); - ConvertPixel( PelValue, eColorSpace ); - return PelValue; + PlaYUVerFrame::Pixel PixelValue( m_pcPelFormat->colorSpace, 0, 0, 0 ); + for( UInt ch = 0; ch < m_pcPelFormat->numberChannels; ch++ ) + { + Int ratioH = ch > 0 ? m_pcPelFormat->ratioChromaHeight : 1; + Int ratioW = ch > 0 ? m_pcPelFormat->ratioChromaWidth : 1; + PixelValue.Components()[ch] = m_pppcInputPel[ch][yPos / ratioH][xPos / ratioW]; + } + return ConvertPixel( PixelValue, eColorSpace ); } -Pixel PlaYUVerFrame::ConvertPixel( Pixel sInputPixel, Int eOutputSpace ) +PlaYUVerFrame::Pixel PlaYUVerFrame::ConvertPixel( PlaYUVerFrame::Pixel inputPixel, Int eOutputSpace ) { Int outA, outB, outC; - Pixel sOutputPixel = - { - COLOR_INVALID, - 0, - 0, - 0 }; + PlaYUVerFrame::Pixel outPixel(COLOR_INVALID, 0, 0, 0); - if( sInputPixel.color_space == eOutputSpace ) - return sInputPixel; + if( inputPixel.ColorSpace() == eOutputSpace || eOutputSpace == COLOR_ARGB || eOutputSpace == COLOR_GRAY ) + return inputPixel; if( eOutputSpace == COLOR_RGB ) { - yuvToRgb( sInputPixel.Luma, sInputPixel.ChromaU, sInputPixel.ChromaV, outA, outB, outC ); - sOutputPixel.ColorR = outA; - sOutputPixel.ColorG = outB; - sOutputPixel.ColorB = outC; + yuvToRgb( inputPixel.Y(), inputPixel.Cb(), inputPixel.Cr(), outA, outB, outC ); + outPixel.R() = outA; + outPixel.G() = outB; + outPixel.B() = outC; } if( eOutputSpace == COLOR_YUV ) { - rgbToYuv( sInputPixel.ColorR, sInputPixel.ColorG, sInputPixel.ColorB, outA, outB, outC ); - sOutputPixel.Luma = outA; - sOutputPixel.ChromaU = outB; - sOutputPixel.ChromaV = outC; + rgbToYuv( inputPixel.R(), inputPixel.G(), inputPixel.B(), outA, outB, outC ); + outPixel.Y() = outA; + outPixel.Cb() = outB; + outPixel.Cr() = outC; } - return sOutputPixel; + return outPixel; } /* @@ -365,6 +362,32 @@ Void PlaYUVerFrame::FrametoRGB8Pixfc() #endif } +Void PlaYUVerFrame::copyFrom( struct AVFrame *ffAvFrame, Int iPixelFormat ) +{ +#ifdef USE_FFMPEG + if( m_iPixelFormat != iPixelFormat ) + return; + m_bHasRGBPel = false; + for( UInt ch = 0; ch < m_pcPelFormat->numberChannels; ch++ ) + { + Pel *pPel = m_pppcInputPel[ch][0]; + uint8_t *ffPel = ffAvFrame->data[ch]; + if( !ffPel ) continue; + Int ratioH = ch > 0 ? m_pcPelFormat->ratioChromaHeight : 1; + Int ratioW = ch > 0 ? m_pcPelFormat->ratioChromaWidth : 1; + for( UInt y = 0; y < m_uiHeight / ratioH; y++ ) + { + for( UInt x = 0; x < m_uiWidth / ratioW; x++ ) + { + *pPel++ = ffPel[x]; + } + ffPel += ffAvFrame->linesize[ch]; + } + } +#endif +} + + #ifdef USE_OPENCV cv::Mat PlaYUVerFrame::getCvMat() { diff --git a/source/lib/PlaYUVerFrame.h b/source/lib/PlaYUVerFrame.h index 13ed8e6..3d22b7f 100644 --- a/source/lib/PlaYUVerFrame.h +++ b/source/lib/PlaYUVerFrame.h @@ -41,14 +41,42 @@ class PixFcSSE; struct AVFrame; - namespace plaYUVer { - class PlaYUVerFrame { public: + 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::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]; } + }; + enum ColorSpace { COLOR_INVALID = -1, @@ -74,12 +102,12 @@ public: static QStringList supportedPixelFormatListNames(); PlaYUVerFrame( UInt width = 0, UInt height = 0, Int pel_format = 0 ); - PlaYUVerFrame( PlaYUVerFrame *other ); - PlaYUVerFrame( PlaYUVerFrame *other, QRect area ); + PlaYUVerFrame( PlaYUVerFrame * ); + PlaYUVerFrame( PlaYUVerFrame *, QRect area ); ~PlaYUVerFrame(); UInt64 getBytesPerFrame(); - static UInt64 getBytesPerFrame( UInt uiWidth, UInt uiHeight, Int iPixelFormat ); + static UInt64 getBytesPerFrame( UInt, UInt, Int ); Int getColorSpace() const; Int getNumberChannels() const; @@ -87,16 +115,16 @@ public: UInt getChromaHeight() const; UInt getChromaLength() const; - Void FrameFromBuffer( Pel*, Int ); - Void FrameToBuffer( Pel* ); + Void frameFromBuffer( Pel*, UInt64 ); + Void frameToBuffer( Pel* ); Void fillRGBBuffer(); Void copyFrom( PlaYUVerFrame* ); Void copyFrom( PlaYUVerFrame*, UInt, UInt ); - Pixel getPixelValue( Int xPos, Int yPos, Int eColorSpace ); - static Pixel ConvertPixel( Pixel, Int ); + PlaYUVerFrame::Pixel getPixelValue( Int, Int, Int ); + static PlaYUVerFrame::Pixel ConvertPixel( Pixel, Int ); UInt getWidth() const { @@ -140,7 +168,7 @@ public: /** * Interface to other libs */ - Void copyFrom( struct AVFrame * ); + Void copyFrom( struct AVFrame *, Int iPixelFormat ); #ifdef USE_OPENCV cv::Mat getCvMat(); diff --git a/source/lib/PlaYUVerFramePixelFormats.cpp b/source/lib/PlaYUVerFramePixelFormats.cpp index 2cf154b..62149ba 100644 --- a/source/lib/PlaYUVerFramePixelFormats.cpp +++ b/source/lib/PlaYUVerFramePixelFormats.cpp @@ -91,15 +91,7 @@ Void fillRGBbufferYUV( Pel*** in, UChar* out, UInt width, UInt height, UInt rati /** * YUV420p 12 bits per pixel */ -Pixel getPixelValueYUV420p( Pel ***Img, Int xPos, Int yPos ) -{ - Pixel pixel_value; - pixel_value.color_space = PlaYUVerFrame::COLOR_YUV; - pixel_value.Luma = Img[LUMA][yPos][xPos]; - pixel_value.ChromaU = Img[CHROMA_U][yPos >> 1][xPos >> 1]; - pixel_value.ChromaV = Img[CHROMA_V][yPos >> 1][xPos >> 1]; - return pixel_value; -} + Void frameFromBufferYUV420p( Pel *in, Pel*** out, UInt width, UInt height ) { frameFromBufferYUVProgressive( in, out, width, height, 4 ); @@ -119,7 +111,6 @@ PlaYUVerFramePelFormat yuv420p = 3, 2, 2, - getPixelValueYUV420p, frameFromBufferYUV420p, bufferFromFrameYUV420p, fillRGBbufferYUV420p, @@ -128,15 +119,7 @@ PlaYUVerFramePelFormat yuv420p = /** * YUV444p 24 bits per pixel */ -Pixel getPixelValueYUV444p( Pel ***Img, Int xPos, Int yPos ) -{ - Pixel pixel_value; - pixel_value.color_space = PlaYUVerFrame::COLOR_YUV; - pixel_value.Luma = Img[LUMA][yPos][xPos]; - pixel_value.ChromaU = Img[CHROMA_U][yPos][xPos]; - pixel_value.ChromaV = Img[CHROMA_V][yPos][xPos]; - return pixel_value; -} + Void frameFromBufferYUV444p( Pel *in, Pel*** out, UInt width, UInt height ) { frameFromBufferYUVProgressive( in, out, width, height, 1 ); @@ -156,7 +139,6 @@ PlaYUVerFramePelFormat yuv444p = 3, 1, 1, - getPixelValueYUV444p, frameFromBufferYUV444p, bufferFromFrameYUV444p, fillRGBbufferYUV444p, @@ -165,15 +147,7 @@ PlaYUVerFramePelFormat yuv444p = /** * YUV422p 16 bits per pixel */ -Pixel getPixelValueYUV422( Pel ***Img, Int xPos, Int yPos ) -{ - Pixel pixel_value; - pixel_value.color_space = PlaYUVerFrame::COLOR_YUV; - pixel_value.Luma = Img[LUMA][yPos][xPos]; - pixel_value.ChromaU = Img[CHROMA_U][yPos][xPos >> 1]; - pixel_value.ChromaV = Img[CHROMA_V][yPos][xPos >> 1]; - return pixel_value; -} + Void frameFromBufferYUV422p( Pel *in, Pel*** out, UInt width, UInt height ) { frameFromBufferYUVProgressive( in, out, width, height, 2 ); @@ -193,7 +167,6 @@ PlaYUVerFramePelFormat yuv422p = 3, 2, 1, - getPixelValueYUV422, frameFromBufferYUV422p, bufferFromFrameYUV422p, fillRGBbufferYUV422, @@ -236,7 +209,6 @@ PlaYUVerFramePelFormat yuyv422 = 3, 2, 1, - getPixelValueYUV422, frameFromBufferYUYV422, bufferFromFrameYUYV422, fillRGBbufferYUV422, @@ -246,15 +218,6 @@ PlaYUVerFramePelFormat yuyv422 = * Gray Format */ -Pixel getPixelValueGray( Pel ***Img, Int xPos, Int yPos ) -{ - Pixel pixel_value; - pixel_value.color_space = PlaYUVerFrame::COLOR_YUV; - pixel_value.Luma = Img[LUMA][yPos][xPos]; - pixel_value.ChromaU = 0; - pixel_value.ChromaV = 0; - return pixel_value; -} Void frameFromBufferGray( Pel *in, Pel*** out, UInt width, UInt height ) { memcpy( &out[LUMA][0][0], in, width * height * sizeof(Pel) ); @@ -282,7 +245,6 @@ PlaYUVerFramePelFormat gray = 1, 0, 0, - getPixelValueGray, frameFromBufferGray, bufferFromFrameGray, fillRGBbufferGray, @@ -293,41 +255,25 @@ PlaYUVerFramePelFormat gray = * Interlaced */ -/** RGB 8 bits per pixel / 3 channels */ -Pixel getPixelValueRGB24( Pel ***Img, Int xPos, Int yPos ) +Void frameFromBufferRGB( Pel *in, Pel*** out, UInt64 size, Pel* pFirst, Pel* pSecond, Pel* pthird ) { - Pixel pixel_value; - pixel_value.color_space = PlaYUVerFrame::COLOR_RGB; - pixel_value.ColorR = Img[COLOR_R][yPos][xPos]; - pixel_value.ColorG = Img[COLOR_G][yPos][xPos]; - pixel_value.ColorB = Img[COLOR_B][yPos][xPos]; - return pixel_value; -} -Void frameFromBufferRGB24( Pel *in, Pel*** out, UInt width, UInt height ) -{ - Pel* pR = out[COLOR_R][0]; - Pel* pG = out[COLOR_G][0]; - Pel* pB = out[COLOR_B][0]; - for( UInt i = 0; i < width * height; i++ ) + for( UInt i = 0; i < size; i++ ) { - *pR++ = *in++; - *pG++ = *in++; - *pB++ = *in++; + *pFirst++ = *in++; + *pSecond++ = *in++; + *pthird++ = *in++; } } -Void bufferFromFrameRGB24( Pel ***in, Pel* out, UInt width, UInt height ) +Void bufferFromFrameRGB( Pel ***in, Pel* out, UInt64 size, Pel* pFirst, Pel* pSecond, Pel* pthird ) { - Pel* pR = in[COLOR_R][0]; - Pel* pG = in[COLOR_G][0]; - Pel* pB = in[COLOR_B][0]; - for( UInt i = 0; i < width * height; i++ ) + for( UInt i = 0; i < size; i++ ) { - *out++ = *pR++; - *out++ = *pG++; - *out++ = *pB++; + *out++ = *pFirst++; + *out++ = *pSecond++; + *out++ = *pthird++; } } -Void fillRGBbufferRGB24( Pel*** in, UChar* out, UInt width, UInt height ) +Void fillRGBbufferRGB( Pel*** in, UChar* out, UInt width, UInt height ) { Pel* pR = in[COLOR_R][0]; Pel* pG = in[COLOR_G][0]; @@ -342,19 +288,51 @@ Void fillRGBbufferRGB24( Pel*** in, UChar* out, UInt width, UInt height ) *buff++ = pelRgb( iR, iG, iB ); } } + +/** RGB 8 bits per pixel / 3 channels */ +Void frameFromBufferRGB24( Pel *in, Pel*** out, UInt width, UInt height ) +{ + frameFromBufferRGB( in, out, width * height, out[COLOR_R][0], out[COLOR_G][0], out[COLOR_B][0] ); +} +Void bufferFromFrameRGB24( Pel ***in, Pel* out, UInt width, UInt height ) +{ + bufferFromFrameRGB( in, out, width * height, in[COLOR_R][0], in[COLOR_G][0], in[COLOR_B][0] ); +} + PlaYUVerFramePelFormat rgb24 = { - "RGB8", + "RGB24", PlaYUVerFrame::COLOR_RGB, 3, 1, 1, - getPixelValueRGB24, frameFromBufferRGB24, bufferFromFrameRGB24, - fillRGBbufferRGB24, + fillRGBbufferRGB, ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_RGB24 ), }; +/** BGR 8 bits per pixel / 3 channels */ +Void frameFromBufferBGR24( Pel *in, Pel*** out, UInt width, UInt height ) +{ + frameFromBufferRGB( in, out, width * height, out[COLOR_B][0], out[COLOR_G][0], out[COLOR_R][0] ); +} +Void bufferFromFrameBGR24( Pel ***in, Pel* out, UInt width, UInt height ) +{ + bufferFromFrameRGB( in, out, width * height, in[COLOR_B][0], in[COLOR_G][0], in[COLOR_R][0] ); +} + +PlaYUVerFramePelFormat BGR24 = +{ + "BRG24", + PlaYUVerFrame::COLOR_RGB, + 3, + 1, + 1, + frameFromBufferBGR24, + bufferFromFrameBGR24, + fillRGBbufferRGB, + ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_BGR24 ), }; + PlaYUVerFramePelFormat g_PlaYUVerFramePelFormatsList[PLAYUVER_NUMBER_FORMATS] = { yuv420p, @@ -362,7 +340,8 @@ PlaYUVerFramePelFormat g_PlaYUVerFramePelFormatsList[PLAYUVER_NUMBER_FORMATS] = yuv422p, yuyv422, gray, - rgb24, }; + rgb24, + BGR24, }; } // NAMESPACE diff --git a/source/lib/PlaYUVerFramePixelFormats.h b/source/lib/PlaYUVerFramePixelFormats.h index a3c98a3..27951fa 100644 --- a/source/lib/PlaYUVerFramePixelFormats.h +++ b/source/lib/PlaYUVerFramePixelFormats.h @@ -94,7 +94,6 @@ struct structPlaYUVerFramePelFormat UInt numberChannels; Int ratioChromaWidth; Int ratioChromaHeight; - Pixel (*getPixelValue)( Pel ***Img, Int xPos, Int yPos ); Void (*frameFromBuffer)( Pel *in, Pel*** out, UInt width, UInt height ); Void (*bufferFromFrame)( Pel ***in, Pel* out, UInt width, UInt height ); Void (*fillRGBbuffer)( Pel*** in, UChar* out, UInt width, UInt height ); @@ -103,7 +102,7 @@ struct structPlaYUVerFramePelFormat typedef struct structPlaYUVerFramePelFormat PlaYUVerFramePelFormat; -#define PLAYUVER_NUMBER_FORMATS 6 +#define PLAYUVER_NUMBER_FORMATS 7 extern PlaYUVerFramePelFormat g_PlaYUVerFramePelFormatsList[PLAYUVER_NUMBER_FORMATS]; } // NAMESPACE diff --git a/source/lib/PlaYUVerStream.cpp b/source/lib/PlaYUVerStream.cpp index f4f5769..93c5fed 100644 --- a/source/lib/PlaYUVerStream.cpp +++ b/source/lib/PlaYUVerStream.cpp @@ -532,8 +532,9 @@ Void PlaYUVerStream::readFrame() if( !m_cLibAvContext->decodeAvFormat() ) { m_iErrorStatus = READING; + return; } - m_pcNextFrame->FrameFromBuffer( m_cLibAvContext->video_dst_data[0], m_iPixelFormat ); + m_pcNextFrame->frameFromBuffer( m_cLibAvContext->m_pchFrameBuffer, m_cLibAvContext->m_uiFrameBufferSize ); } else #endif @@ -547,7 +548,7 @@ Void PlaYUVerStream::readFrame() << endl; return; } - m_pcNextFrame->FrameFromBuffer( m_pStreamBuffer, m_iPixelFormat ); + m_pcNextFrame->frameFromBuffer( m_pStreamBuffer, bytes_read ); } //Int time = m_cTimer.elapsed(); //m_uiAveragePlayInterval = ( m_uiAveragePlayInterval + time) / 2; @@ -558,7 +559,7 @@ Void PlaYUVerStream::readFrame() Void PlaYUVerStream::writeFrame() { UInt64 frame_bytes_input = m_pcCurrFrame->getBytesPerFrame(); - m_pcCurrFrame->FrameToBuffer( m_pStreamBuffer ); + m_pcCurrFrame->frameToBuffer( m_pStreamBuffer ); UInt64 bytes_read = fwrite( m_pStreamBuffer, sizeof(Pel), frame_bytes_input, m_pFile ); if( bytes_read != frame_bytes_input ) { @@ -570,7 +571,7 @@ Void PlaYUVerStream::writeFrame() Void PlaYUVerStream::writeFrame( PlaYUVerFrame *pcFrame ) { UInt64 frame_bytes_input = pcFrame->getBytesPerFrame(); - pcFrame->FrameToBuffer( m_pStreamBuffer ); + pcFrame->frameToBuffer( m_pStreamBuffer ); UInt64 bytes_read = fwrite( m_pStreamBuffer, sizeof(Pel), frame_bytes_input, m_pFile ); if( bytes_read != frame_bytes_input ) { diff --git a/source/lib/ffmpeg/LibAvContextHandle.cpp b/source/lib/ffmpeg/LibAvContextHandle.cpp index 0b0ce21..b29ebe4 100644 --- a/source/lib/ffmpeg/LibAvContextHandle.cpp +++ b/source/lib/ffmpeg/LibAvContextHandle.cpp @@ -26,6 +26,7 @@ #include "LibAvContextHandle.h" #include "PlaYUVerFrame.h" #include "PlaYUVerFramePixelFormats.h" +#include "LibMemory.h" namespace plaYUVer { @@ -77,22 +78,20 @@ Void LibAvContextHandle::closeAvFormat() avformat_close_input( &fmt_ctx ); av_free( frame ); - av_free( video_dst_data[0] ); + //av_free( video_dst_data[0] ); } m_bHasStream = false; } Bool LibAvContextHandle::initAvFormat( char* filename, UInt& width, UInt& height, Int& pixel_format, UInt& frame_rate, UInt64& num_frames ) { - int ret = 0; - fmt_ctx = NULL; video_dec_ctx = NULL; video_stream = NULL; - video_dst_data[0] = NULL; - video_dst_data[1] = NULL; - video_dst_data[2] = NULL; - video_dst_data[3] = NULL; +// video_dst_data[0] = NULL; +// video_dst_data[1] = NULL; +// video_dst_data[2] = NULL; +// video_dst_data[3] = NULL; video_stream_idx = -1; frame = NULL; m_bHasStream = false; @@ -136,15 +135,18 @@ Bool LibAvContextHandle::initAvFormat( char* filename, UInt& width, UInt& height video_stream = fmt_ctx->streams[video_stream_idx]; video_dec_ctx = video_stream->codec; - /* allocate image where the decoded image will be put */ - ret = av_image_alloc( video_dst_data, video_dst_linesize, video_dec_ctx->width, video_dec_ctx->height, video_dec_ctx->pix_fmt, 1 ); - if( ret < 0 ) - { - //qDebug( ) << " Could not allocate raw video buffer !!!" << endl; - closeAvFormat(); - return false; - } - video_dst_bufsize = ret; +// /* allocate image where the decoded image will be put */ +// ret = av_image_alloc( video_dst_data, video_dst_linesize, video_dec_ctx->width, video_dec_ctx->height, video_dec_ctx->pix_fmt, 1 ); +// if( ret < 0 ) +// { +// //qDebug( ) << " Could not allocate raw video buffer !!!" << endl; +// closeAvFormat(); +// return false; +// } +// video_dst_bufsize = ret; + + m_uiFrameBufferSize = av_image_get_buffer_size( video_dec_ctx->pix_fmt, video_dec_ctx->width, video_dec_ctx->height, 1); + getMem1D( &m_pchFrameBuffer, m_uiFrameBufferSize ); } const char *codec_name = avcodec_get_name( video_dec_ctx->codec_id ); @@ -207,7 +209,7 @@ Bool LibAvContextHandle::initAvFormat( char* filename, UInt& width, UInt& height if( !frame ) { //qDebug( ) << " Could not allocate frame !!!" << endl; - ret = AVERROR( ENOMEM ); + //ret = AVERROR( ENOMEM ); closeAvFormat(); return false; } @@ -240,10 +242,14 @@ Bool LibAvContextHandle::decodeAvFormat() if( got_frame ) { + av_image_copy_to_buffer( m_pchFrameBuffer, m_uiFrameBufferSize, + frame->data,frame->linesize, video_dec_ctx->pix_fmt, video_dec_ctx->width, + video_dec_ctx->height, 1); + /* copy decoded frame to destination buffer: * this is required since rawvideo expects non aligned data */ - av_image_copy( video_dst_data, video_dst_linesize, ( const uint8_t ** )( frame->data ), frame->linesize, video_dec_ctx->pix_fmt, video_dec_ctx->width, - video_dec_ctx->height ); + // av_image_copy( video_dst_data, video_dst_linesize, ( const uint8_t ** )( frame->data ), frame->linesize, video_dec_ctx->pix_fmt, video_dec_ctx->width, + // video_dec_ctx->height ); } } av_free_packet( &pkt ); diff --git a/source/lib/ffmpeg/LibAvContextHandle.h b/source/lib/ffmpeg/LibAvContextHandle.h index 5a08ec1..3fd2eeb 100644 --- a/source/lib/ffmpeg/LibAvContextHandle.h +++ b/source/lib/ffmpeg/LibAvContextHandle.h @@ -75,9 +75,12 @@ public: return m_uiSecs; } - uint8_t *video_dst_data[4]; - int video_dst_linesize[4]; - int video_dst_bufsize; +// uint8_t *video_dst_data[4]; +// int video_dst_linesize[4]; +// int video_dst_bufsize; + + UChar* m_pchFrameBuffer; + UInt64 m_uiFrameBufferSize; private: AVFormatContext *fmt_ctx;