From a33b61d08261687e4a71e7d59a87354be557c8b2 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Mon, 13 Oct 2014 01:28:01 +0100 Subject: [PATCH] Change the conversion between PlaYUVerFrame and ffmpeg pel formats --- source/lib/PlaYUVerFrame.cpp | 23 +++++- source/lib/PlaYUVerFrame.h | 27 +----- source/lib/PlaYUVerFramePixelFormats.cpp | 101 +++++++++++++---------- source/lib/ffmpeg/LibAvContextHandle.cpp | 65 +++------------ 4 files changed, 96 insertions(+), 120 deletions(-) diff --git a/source/lib/PlaYUVerFrame.cpp b/source/lib/PlaYUVerFrame.cpp index ba5722d..05f8b58 100644 --- a/source/lib/PlaYUVerFrame.cpp +++ b/source/lib/PlaYUVerFrame.cpp @@ -36,11 +36,30 @@ namespace plaYUVer { +Int PlaYUVerFrame::isRGBorYUVorGray( Int pixel_format ) +{ + switch( pixel_format ) + { + case GRAY: + return COLOR_GRAY; + break; + case YUV420p: + case YUV444p: + case YUV422p: + case YUYV422: + return COLOR_YUV; + break; + case RGB8: + return COLOR_RGB; + break; + } + return COLOR_INVALID; +} + QStringList PlaYUVerFrame::supportedPixelFormatList() { QStringList formats; - Int numberFormats = sizeof( g_PlaYUVerFramePelFormatsList ) / sizeof(PlaYUVerFramePelFormat); - numberFormats = 6; + Int numberFormats = PLAYUVER_NUMBER_FORMATS; for( Int i = 0; i < numberFormats; i++ ) { formats << g_PlaYUVerFramePelFormatsList[i].name; diff --git a/source/lib/PlaYUVerFrame.h b/source/lib/PlaYUVerFrame.h index 51de341..8a508b8 100644 --- a/source/lib/PlaYUVerFrame.h +++ b/source/lib/PlaYUVerFrame.h @@ -50,10 +50,11 @@ typedef struct UInt numberChannels; Int ratioChromaWidth; Int ratioChromaHeight; - Pixel (*getPixelValue) ( Pel ***Img, Int xPos, Int yPos ); + Pixel (*getPixelValue)( Pel ***Img, Int xPos, Int yPos ); Void (*frameFromBuffer)( Pel *in, Pel*** out ); Void (*bufferFromFrame)( Pel ***in, Pel* out ); Void (*fillRGBbuffer)( Pel*** in, UChar* out ); + Int ffmpegPelFormat; } PlaYUVerFramePelFormat; #define PLAYUVER_NUMBER_FORMATS 6 @@ -84,26 +85,7 @@ public: NUMBER_FORMATS }; - static Int isRGBorYUVorGray( Int pixel_format ) - { - switch( pixel_format ) - { - case GRAY: - return COLOR_GRAY; - break; - case YUV420p: - case YUV444p: - case YUV422p: - case YUYV422: - return COLOR_YUV; - break; - case RGB8: - return COLOR_RGB; - break; - } - return COLOR_INVALID; - } - + static Int isRGBorYUVorGray( Int pixel_format ); static QStringList supportedPixelFormatList(); PlaYUVerFrame( UInt width = 0, UInt height = 0, Int pel_format = 0 ); @@ -224,8 +206,7 @@ private: Void closePixfc(); PixFcSSE* m_pcPixfc; Void FrametoRGB8Pixfc(); -} -; +}; } // NAMESPACE diff --git a/source/lib/PlaYUVerFramePixelFormats.cpp b/source/lib/PlaYUVerFramePixelFormats.cpp index a7790df..d256ea5 100644 --- a/source/lib/PlaYUVerFramePixelFormats.cpp +++ b/source/lib/PlaYUVerFramePixelFormats.cpp @@ -26,10 +26,20 @@ #include #include "LibMemory.h" #include "PlaYUVerFrame.h" +#ifdef USE_FFMPEG +#include "LibAvContextHandle.h" +#endif namespace plaYUVer { +#ifdef USE_FFMPEG +#define ADD_FFMPEG_PEL_FMT( fmt ) fmt +#else +#define ADD_FFMPEG_PEL_FMT( fmt ) 0 +#endif + + Void frameFromBuffer_yuv420p( Pel *in, Pel*** out ) { @@ -42,7 +52,7 @@ Void fillRGBbuffer_yuv420p( Pel*** in, UChar* out ) { } -Pixel getPixelValueYUV420p ( Pel ***Img, Int xPos, Int yPos ) +Pixel getPixelValueYUV420p( Pel ***Img, Int xPos, Int yPos ) { Pixel pixel_value; pixel_value.color_space = PlaYUVerFrame::COLOR_YUV; @@ -52,46 +62,6 @@ Pixel getPixelValueYUV420p ( Pel ***Img, Int xPos, Int yPos ) return pixel_value; } -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; -} - -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; -} - -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; -} - -Pixel getPixelValueRGB8 ( Pel ***Img, Int xPos, Int yPos ) -{ - 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; -} - PlaYUVerFramePelFormat yuv420p = { "YUV420p", @@ -103,8 +73,49 @@ PlaYUVerFramePelFormat yuv420p = frameFromBuffer_yuv420p, bufferFromFrame_yuv420p, fillRGBbuffer_yuv420p, + ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_YUV420P ), }; +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; +} + +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; +} + +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; +} + +Pixel getPixelValueRGB8( Pel ***Img, Int xPos, Int yPos ) +{ + 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; +} + PlaYUVerFramePelFormat yuv444p = { "YUV444p", @@ -116,6 +127,7 @@ PlaYUVerFramePelFormat yuv444p = frameFromBuffer_yuv420p, bufferFromFrame_yuv420p, fillRGBbuffer_yuv420p, + ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_YUV444P ), }; PlaYUVerFramePelFormat yuv422p = @@ -129,6 +141,7 @@ PlaYUVerFramePelFormat yuv422p = frameFromBuffer_yuv420p, bufferFromFrame_yuv420p, fillRGBbuffer_yuv420p, + ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_YUV422P ), }; PlaYUVerFramePelFormat yuv422 = @@ -142,6 +155,7 @@ PlaYUVerFramePelFormat yuv422 = frameFromBuffer_yuv420p, bufferFromFrame_yuv420p, fillRGBbuffer_yuv420p, + ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_YUYV422 ), }; PlaYUVerFramePelFormat gray = @@ -155,6 +169,7 @@ PlaYUVerFramePelFormat gray = frameFromBuffer_yuv420p, bufferFromFrame_yuv420p, fillRGBbuffer_yuv420p, + ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_GRAY8 ), }; PlaYUVerFramePelFormat rgb8 = @@ -168,6 +183,7 @@ PlaYUVerFramePelFormat rgb8 = frameFromBuffer_yuv420p, bufferFromFrame_yuv420p, fillRGBbuffer_yuv420p, + ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_RGB24 ), }; extern PlaYUVerFramePelFormat g_PlaYUVerFramePelFormatsList[PLAYUVER_NUMBER_FORMATS] = @@ -177,7 +193,6 @@ extern PlaYUVerFramePelFormat g_PlaYUVerFramePelFormatsList[PLAYUVER_NUMBER_FORM yuv422p, yuv422, gray, - rgb8, -}; + rgb8, }; } // NAMESPACE diff --git a/source/lib/ffmpeg/LibAvContextHandle.cpp b/source/lib/ffmpeg/LibAvContextHandle.cpp index 9f3eeea..a6a2eea 100644 --- a/source/lib/ffmpeg/LibAvContextHandle.cpp +++ b/source/lib/ffmpeg/LibAvContextHandle.cpp @@ -108,37 +108,12 @@ Bool LibAvContextHandle::initAvFormat( char* filename, UInt& width, UInt& height sprintf( aux_string, "%dx%d", width, height ); av_dict_set( &format_opts, "video_size", aux_string, 0 ); } -// YUV420p = 0, -// YUV444p, -// YUV422p, -// YUYV422, -// GRAY, -// RGB8, - switch( pixel_format ) - { - case PlaYUVerFrame::NO_FMT: - break; - case PlaYUVerFrame::YUV420p: - av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_YUV420P ), 0 ); - break; - case PlaYUVerFrame::YUV444p: - av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_YUV444P ), 0 ); - break; - case PlaYUVerFrame::YUV422p: - av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_YUV422P ), 0 ); - break; - case PlaYUVerFrame::YUYV422: - av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_YUYV422 ), 0 ); - break; - case PlaYUVerFrame::GRAY: - av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_GRAY8 ), 0 ); - break; - case PlaYUVerFrame::RGB8: - av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_RGB24 ), 0 ); - break; - default: - break; + + Int ffmpeg_pel_format = g_PlaYUVerFramePelFormatsList[pixel_format].ffmpegPelFormat; + if( ffmpeg_pel_format >= 0 ) + { + av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AVPixelFormat( ffmpeg_pel_format ) ), 0 ); } /* open input file, and allocate format context */ @@ -203,29 +178,15 @@ Bool LibAvContextHandle::initAvFormat( char* filename, UInt& width, UInt& height width = video_dec_ctx->width; height = video_dec_ctx->height; - switch( video_dec_ctx->pix_fmt ) + + pixel_format = PlaYUVerFrame::NO_FMT; + for( Int i = 0; i < PLAYUVER_NUMBER_FORMATS; i++ ) { - case AV_PIX_FMT_YUV420P: - pixel_format = PlaYUVerFrame::YUV420p; - break; - case AV_PIX_FMT_YUV444P: - pixel_format = PlaYUVerFrame::YUV444p; - break; - case AV_PIX_FMT_YUV422P: - pixel_format = PlaYUVerFrame::YUV422p; - break; - case AV_PIX_FMT_YUYV422: - pixel_format = PlaYUVerFrame::YUYV422; - break; - case AV_PIX_FMT_GRAY8: - pixel_format = PlaYUVerFrame::GRAY; - break; - case AV_PIX_FMT_RGB24: - pixel_format = PlaYUVerFrame::RGB8; - break; - default: - pixel_format = PlaYUVerFrame::NO_FMT; - break; + if( g_PlaYUVerFramePelFormatsList[i].ffmpegPelFormat == video_dec_ctx->pix_fmt ) + { + pixel_format = i; + break; + } } /* dump input information to stderr */