Fix opening sequences in YUV420 when odd resolutions

This commit is contained in:
2015-03-05 16:38:57 +00:00
parent 4af83f229d
commit 2b749e3da0
7 changed files with 152 additions and 92 deletions
+46 -17
View File
@@ -295,31 +295,60 @@ Bool LibAvContextHandle::initAvFormat( const char* filename, UInt& width, UInt&
Bool LibAvContextHandle::decodeAvFormat()
{
Int got_frame;
Int decResult;
/* read frames from the file */
while( av_read_frame( fmt_ctx, &pkt ) >= 0 )
{
if( pkt.stream_index == video_stream_idx )
AVPacket orig_pkt = pkt;
do
{
/* decode video frame */
Int ret = avcodec_decode_video2( video_dec_ctx, frame, &got_frame, &pkt );
if( ret < 0 )
if( pkt.stream_index == video_stream_idx )
{
fprintf( stderr, "Error decoding video frame\n" );
return false;
}
/* decode video frame */
decResult = avcodec_decode_video2( video_dec_ctx, frame, &got_frame, &pkt );
if( decResult < 0 )
{
fprintf( stderr, "Error decoding video frame\n" );
return false;
}
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 );
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 );
}
}
pkt.data += decResult;
pkt.size -= decResult;
}
av_free_packet( &pkt );
while( pkt.size > 0 );
av_free_packet( &orig_pkt );
// /* read frames from the file */
// while( av_read_frame( fmt_ctx, &pkt ) >= 0 )
// {
// if( pkt.stream_index == video_stream_idx )
// {
// /* decode video frame */
// Int ret = avcodec_decode_video2( video_dec_ctx, frame, &got_frame, &pkt );
// if( ret < 0 )
// {
// fprintf( stderr, "Error decoding video frame\n" );
// return false;
// }
//
// 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_free_packet( &pkt );
if( got_frame )
return true;
}
-27
View File
@@ -176,33 +176,6 @@ Int getMem5D( T****** array5D, Int dim0, Int dim1, Int dim2, Int dim3, Int dim4
return mem_size;
}
template<typename T>
Int getMem3ImageComponents( T**** array3D, Int dim1, Int dim2, Int ratioDim1Chroma, Int ratioDim2Chroma )
{
Int dim0 = 3;
Int i, mem_size = ( dim1 * dim2 + dim1 / ratioDim1Chroma * dim2 / ratioDim2Chroma * 2 ) * sizeof(T);
mem_size += getMem2D<T*>( array3D, dim0, dim1 );
if( ( ( *array3D )[0][0] = ( T* )xCallocMem( dim1 * dim2 + dim1 / ratioDim1Chroma * dim2 / ratioDim2Chroma * 2, sizeof(T) ) ) == NULL )
printf( "getMem3DImageComponents: array1D" );
// Luma
for( i = 1; i < dim1; i++ )
( *array3D )[0][i] = ( *array3D )[0][i - 1] + dim2;
// Chroma
( *array3D )[1][0] = ( *array3D )[0][0] + dim2 * dim1;
for( i = 1; i < dim1 / ratioDim1Chroma; i++ )
( *array3D )[1][i] = ( *array3D )[1][i - 1] + dim2 / ratioDim2Chroma;
( *array3D )[2][0] = ( *array3D )[1][0] + dim2 / ratioDim2Chroma * dim1 / ratioDim1Chroma;
for( i = 1; i < dim1 / ratioDim1Chroma; i++ )
( *array3D )[2][i] = ( *array3D )[2][i - 1] + dim2 / ratioDim2Chroma;
return mem_size;
}
template<typename T>
Void freeMem1D( T *array1D )
{
+2
View File
@@ -47,6 +47,8 @@ typedef unsigned int UInt;
typedef double Double;
typedef float Float;
//typedef uint32_t UInt32;
// ====================================================================================================================
// 64-bit integer type
// ====================================================================================================================
+56 -12
View File
@@ -75,16 +75,50 @@ PlaYUVerFrame::~PlaYUVerFrame()
if( m_pppcInputPel )
freeMem3ImageComponents<Pel>( m_pppcInputPel );
if( m_pcRGB32 )
freeMem1D( m_pcRGB32 );
if( m_pcARGB32 )
freeMem1D( m_pcARGB32 );
closePixfc();
}
static Int getMem3ImageComponents( Pel**** array3D, Int dim1, Int dim2, Int ratioDim1Chroma, Int ratioDim2Chroma )
{
Int dim0 = 3;
Int i;
UInt64 total_mem_size = 0;
UInt64 mem_size = ( dim1 * dim2 + CHROMA_RESAMPLING( dim1 ) / ratioDim1Chroma * CHROMA_RESAMPLING( dim2 ) / ratioDim2Chroma * 2 );
total_mem_size += getMem2D<Pel*>( array3D, dim0, dim1 );
if( ( ( *array3D )[0][0] = ( Pel* )xCallocMem( mem_size, sizeof(Pel) ) ) == NULL )
printf( "getMem3DImageComponents: array1D" );
total_mem_size += mem_size * sizeof(Pel);
// Luma
for( i = 1; i < dim1; i++ )
( *array3D )[0][i] = ( *array3D )[0][i - 1] + dim2;
// Chroma
( *array3D )[1][0] = ( *array3D )[0][0] + dim2 * dim1;
dim1 = CHROMARATIO( dim1, ratioDim1Chroma );
dim2 = CHROMARATIO( dim2, ratioDim2Chroma );
for( i = 1; i < dim1; i++ )
( *array3D )[1][i] = ( *array3D )[1][i - 1] + dim2;
( *array3D )[2][0] = ( *array3D )[1][0] + dim2 * dim1;
for( i = 1; i < dim1; i++ )
( *array3D )[2][i] = ( *array3D )[2][i - 1] + dim2;
return total_mem_size;
}
Void PlaYUVerFrame::init( UInt width, UInt height, Int pel_format )
{
m_pppcInputPel = NULL;
m_pcRGB32 = NULL;
m_pcARGB32 = NULL;
m_uiWidth = width;
m_uiHeight = height;
m_iPixelFormat = pel_format;
@@ -103,13 +137,13 @@ Void PlaYUVerFrame::init( UInt width, UInt height, Int pel_format )
m_bHasRGBPel = false;
if( m_pcPelFormat->colorSpace == COLOR_GRAY )
{
getMem3ImageComponents<Pel>( &m_pppcInputPel, m_uiHeight, m_uiWidth, 1, 1 );
getMem3ImageComponents( &m_pppcInputPel, m_uiHeight, m_uiWidth, 1, 1 );
}
else
{
getMem3ImageComponents<Pel>( &m_pppcInputPel, m_uiHeight, m_uiWidth, m_pcPelFormat->ratioChromaHeight, m_pcPelFormat->ratioChromaWidth );
getMem3ImageComponents( &m_pppcInputPel, m_uiHeight, m_uiWidth, m_pcPelFormat->ratioChromaHeight, m_pcPelFormat->ratioChromaWidth );
}
getMem1D( &m_pcRGB32, m_uiHeight * m_uiWidth * 4 );
getMem1D( &m_pcARGB32, m_uiHeight * m_uiWidth * 4 );
xSetupStatistics( this );
openPixfc();
@@ -136,19 +170,29 @@ UInt64 PlaYUVerFrame::getBytesPerFrame( UInt uiWidth, UInt uiHeight, Int iPixelF
UInt64 numberBytes = uiWidth * uiHeight;
if( pcPelFormat->numberChannels > 1 )
{
numberBytes += ( pcPelFormat->numberChannels - 1 ) * ( uiWidth * uiHeight ) / ( pcPelFormat->ratioChromaWidth * pcPelFormat->ratioChromaHeight );
UInt64 numberBytesChroma = ( CHROMA_RESAMPLING( uiWidth ) * CHROMA_RESAMPLING( uiHeight ) )
/ ( pcPelFormat->ratioChromaWidth * pcPelFormat->ratioChromaHeight );
numberBytes += ( pcPelFormat->numberChannels - 1 ) * numberBytesChroma;
}
return numberBytes;
}
UInt PlaYUVerFrame::getChromaWidth() const
{
return m_pcPelFormat->ratioChromaWidth > 0 ? m_uiWidth / m_pcPelFormat->ratioChromaWidth : 0;
if( m_pcPelFormat->ratioChromaWidth > 0 )
{
return m_pcPelFormat->ratioChromaWidth > 1 ? ( m_uiWidth + 1 ) / m_pcPelFormat->ratioChromaWidth : m_uiWidth;
}
return 0;
}
UInt PlaYUVerFrame::getChromaHeight() const
{
return m_pcPelFormat->ratioChromaHeight > 0 ? m_uiHeight / m_pcPelFormat->ratioChromaHeight : 0;
if( m_pcPelFormat->ratioChromaHeight > 0 )
{
return m_pcPelFormat->ratioChromaHeight > 1 ? ( m_uiHeight + 1 ) / m_pcPelFormat->ratioChromaHeight : m_uiHeight;
}
return 0;
}
UInt PlaYUVerFrame::getChromaLength() const
@@ -175,7 +219,7 @@ Void PlaYUVerFrame::fillRGBBuffer()
{
if( m_bHasRGBPel )
return;
m_pcPelFormat->fillRGBbuffer( m_pppcInputPel, m_pcRGB32, m_uiWidth, m_uiHeight );
m_pcPelFormat->fillARGB32buffer( m_pppcInputPel, m_pcARGB32, m_uiWidth, m_uiHeight );
m_bHasRGBPel = true;
}
@@ -339,7 +383,7 @@ Void PlaYUVerFrame::getCvMat( Void** ppCvFrame )
if( m_pcPelFormat->colorSpace != PlaYUVerFrame::COLOR_GRAY )
{
fillRGBBuffer();
memcpy( pcCvFrame->data, m_pcRGB32, m_uiWidth * m_uiHeight * 4 * sizeof(UChar) );
memcpy( pcCvFrame->data, m_pcARGB32, m_uiWidth * m_uiHeight * 4 * sizeof(UChar) );
}
else
{
@@ -381,7 +425,7 @@ Void PlaYUVerFrame::fromCvMat( Void* voidFrame )
Pel* pInputPelY = m_pppcInputPel[LUMA][0];
Pel* pInputPelU = m_pppcInputPel[CHROMA_U][0];
Pel* pInputPelV = m_pppcInputPel[CHROMA_V][0];
Pel* pcRGBPelInterlaced = m_pcRGB32;
Pel* pcRGBPelInterlaced = m_pcARGB32;
memcpy( pcRGBPelInterlaced, opencvFrame->data, m_uiWidth * m_uiHeight * 4 * sizeof(Pel) );
UInt* buff = ( UInt* )pcRGBPelInterlaced;
for( UInt i = 0; i < m_uiHeight * m_uiWidth; i++ )
+2 -2
View File
@@ -195,7 +195,7 @@ public:
{
fillRGBBuffer();
}
return m_pcRGB32;
return m_pcARGB32;
}
/**
@@ -243,7 +243,7 @@ private:
Bool m_bHasRGBPel;
Pel*** m_pppcInputPel;
UChar* m_pcRGB32;
UChar* m_pcARGB32;
Void init( UInt width, UInt height, Int pel_format );
Void openPixfc();
+40 -33
View File
@@ -56,37 +56,43 @@ namespace plaYUVer
* Progressive and interlaces
*/
Void frameFromBufferYUVProgressive( Pel* in, Pel ***out, UInt width, UInt height, UInt ratioChroma )
Void frameFromBufferYUVProgressive( Pel* in, Pel ***out, UInt width, UInt height, UInt ratioChromaWidth, UInt ratioChromaHeight )
{
UInt64 chromaSize = CHROMASHIFT( width, ratioChromaWidth ) * CHROMASHIFT( height, ratioChromaHeight );
memcpy( &out[LUMA][0][0], in, width * height * sizeof(Pel) );
memcpy( &out[CHROMA_U][0][0], in + width * height, width * height / ratioChroma * sizeof(Pel) );
memcpy( &out[CHROMA_V][0][0], in + width * height + width * height / ratioChroma, width * height / ratioChroma * sizeof(Pel) );
memcpy( &out[CHROMA_U][0][0], in + width * height, chromaSize * sizeof(Pel) );
memcpy( &out[CHROMA_V][0][0], in + width * height + chromaSize, chromaSize * sizeof(Pel) );
}
Void bufferFromFrameYUVProgressive( Pel*** in, Pel *out, UInt width, UInt height, UInt ratioChroma )
Void bufferFromFrameYUVProgressive( Pel*** in, Pel *out, UInt width, UInt height, UInt ratioChromaWidth, UInt ratioChromaHeight )
{
UInt64 chromaSize = CHROMASHIFT( width, ratioChromaWidth ) * CHROMASHIFT( height, ratioChromaHeight );
memcpy( out, &in[LUMA][0][0], width * height * sizeof(Pel) );
memcpy( out + width * height, &in[CHROMA_U][0][0], width * height / ratioChroma * sizeof(Pel) );
memcpy( out + width * height + width * height / ratioChroma, &in[CHROMA_V][0][0], width * height / ratioChroma * sizeof(Pel) );
memcpy( out + width * height, &in[CHROMA_U][0][0], chromaSize * sizeof(Pel) );
memcpy( out + width * height + chromaSize, &in[CHROMA_V][0][0], chromaSize * sizeof(Pel) );
}
Void fillRGBbufferYUV( Pel*** in, UChar* out, UInt width, UInt height, UInt ratioChromaHor, UInt ratioChromaVer )
Void fillARGB32bufferYUV( Pel*** in, UChar* out, UInt width, UInt height, UInt ratioChromaHor, UInt ratioChromaVer )
{
Pel* pY = in[LUMA][0];
Pel* pLineY = in[LUMA][0];
Pel* pLineU = in[CHROMA_U][0];
Pel* pLineV = in[CHROMA_V][0];
UInt uiChromaStride = width >> ratioChromaHor;
UInt uiChromaStride = CHROMASHIFT( width, ratioChromaHor );
Pel* pY;
Pel* pU;
Pel* pV;
Int iY, iU, iV, iR, iG, iB;
UInt* buff = ( UInt* )out;
UInt* pARGBLine = ( UInt* )out;
UInt* pARGB;
for( UInt y = 0; y < height >> ratioChromaVer; y++ )
{
for( Int i = 0; i < 1 << ratioChromaVer; i++ )
{
pY = pLineY;
pU = pLineU;
pV = pLineV;
pARGB = pARGBLine;
for( UInt x = 0; x < width >> ratioChromaHor; x++ )
{
iU = *pU++;
@@ -95,10 +101,11 @@ Void fillRGBbufferYUV( Pel*** in, UChar* out, UInt width, UInt height, UInt rati
{
iY = *pY++;
YUV2RGB( iY, iU, iV, iR, iG, iB );
*buff = PEL_RGB( iR, iG, iB );
buff++;
*pARGB++ = PEL_RGB( iR, iG, iB );
}
}
pLineY += width;
pARGBLine += width;
}
pLineU += uiChromaStride;
pLineV += uiChromaStride;
@@ -111,13 +118,13 @@ Void fillRGBbufferYUV( Pel*** in, UChar* out, UInt width, UInt height, UInt rati
Void frameFromBufferYUV420p( Pel *in, Pel*** out, UInt width, UInt height )
{
frameFromBufferYUVProgressive( in, out, width, height, 4 );
frameFromBufferYUVProgressive( in, out, width, height, 1, 1 );
}
Void bufferFromFrameYUV420p( Pel ***in, Pel* out, UInt width, UInt height )
{
bufferFromFrameYUVProgressive( in, out, width, height, 4 );
bufferFromFrameYUVProgressive( in, out, width, height, 1, 1 );
}
Void fillRGBbufferYUV420p( Pel*** in, UChar* out, UInt width, UInt height )
Void fillARGB32bufferYUV420p( Pel*** in, UChar* out, UInt width, UInt height )
{
#ifdef USE_SSE
if( ( width % 16 ) == 0 )
@@ -260,7 +267,7 @@ Void fillRGBbufferYUV420p( Pel*** in, UChar* out, UInt width, UInt height )
else
#endif
{
fillRGBbufferYUV( in, out, width, height, 1, 1 );
fillARGB32bufferYUV( in, out, width, height, 1, 1 );
}
}
@@ -273,7 +280,7 @@ PlaYUVerFramePelFormat yuv420p =
2,
frameFromBufferYUV420p,
bufferFromFrameYUV420p,
fillRGBbufferYUV420p,
fillARGB32bufferYUV420p,
ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_YUV420P ), };
/**
@@ -282,15 +289,15 @@ PlaYUVerFramePelFormat yuv420p =
Void frameFromBufferYUV444p( Pel *in, Pel*** out, UInt width, UInt height )
{
frameFromBufferYUVProgressive( in, out, width, height, 1 );
frameFromBufferYUVProgressive( in, out, width, height, 0, 0 );
}
Void bufferFromFrameYUV444p( Pel ***in, Pel* out, UInt width, UInt height )
{
bufferFromFrameYUVProgressive( in, out, width, height, 1 );
bufferFromFrameYUVProgressive( in, out, width, height, 0, 0 );
}
Void fillRGBbufferYUV444p( Pel*** in, UChar* out, UInt width, UInt height )
Void fillARGB32bufferYUV444p( Pel*** in, UChar* out, UInt width, UInt height )
{
fillRGBbufferYUV( in, out, width, height, 0, 0 );
fillARGB32bufferYUV( in, out, width, height, 0, 0 );
}
PlaYUVerFramePelFormat yuv444p =
{
@@ -301,7 +308,7 @@ PlaYUVerFramePelFormat yuv444p =
1,
frameFromBufferYUV444p,
bufferFromFrameYUV444p,
fillRGBbufferYUV444p,
fillARGB32bufferYUV444p,
ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_YUV444P ), };
/**
@@ -310,15 +317,15 @@ PlaYUVerFramePelFormat yuv444p =
Void frameFromBufferYUV422p( Pel *in, Pel*** out, UInt width, UInt height )
{
frameFromBufferYUVProgressive( in, out, width, height, 2 );
frameFromBufferYUVProgressive( in, out, width, height, 1, 0 );
}
Void bufferFromFrameYUV422p( Pel ***in, Pel* out, UInt width, UInt height )
{
bufferFromFrameYUVProgressive( in, out, width, height, 2 );
bufferFromFrameYUVProgressive( in, out, width, height, 1, 0 );
}
Void fillRGBbufferYUV422( Pel*** in, UChar* out, UInt width, UInt height )
Void fillARGB32bufferYUV422( Pel*** in, UChar* out, UInt width, UInt height )
{
fillRGBbufferYUV( in, out, width, height, 1, 0 );
fillARGB32bufferYUV( in, out, width, height, 1, 0 );
}
PlaYUVerFramePelFormat yuv422p =
{
@@ -329,7 +336,7 @@ PlaYUVerFramePelFormat yuv422p =
1,
frameFromBufferYUV422p,
bufferFromFrameYUV422p,
fillRGBbufferYUV422,
fillARGB32bufferYUV422,
ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_YUV422P ), };
/**
@@ -371,7 +378,7 @@ PlaYUVerFramePelFormat yuyv422 =
1,
frameFromBufferYUYV422,
bufferFromFrameYUYV422,
fillRGBbufferYUV422,
fillARGB32bufferYUV422,
ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_YUYV422 ), };
/**
@@ -386,7 +393,7 @@ Void bufferFromFrameGray( Pel ***in, Pel* out, UInt width, UInt height )
{
memcpy( out, &in[LUMA][0][0], width * height * sizeof(Pel) );
}
Void fillRGBbufferGray( Pel*** in, UChar* out, UInt width, UInt height )
Void fillARGB32bufferGray( Pel*** in, UChar* out, UInt width, UInt height )
{
Pel *inPel = in[0][0];
Pel iY;
@@ -407,7 +414,7 @@ PlaYUVerFramePelFormat gray =
0,
frameFromBufferGray,
bufferFromFrameGray,
fillRGBbufferGray,
fillARGB32bufferGray,
ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_GRAY8 ), };
/**
@@ -433,7 +440,7 @@ Void bufferFromFrameRGB( Pel ***in, Pel* out, UInt64 size, Pel* pFirst, Pel* pSe
*out++ = *pthird++;
}
}
Void fillRGBbufferRGB( Pel*** in, UChar* out, UInt width, UInt height )
Void fillARGB32bufferRGB( Pel*** in, UChar* out, UInt width, UInt height )
{
Pel* pR = in[COLOR_R][0];
Pel* pG = in[COLOR_G][0];
@@ -468,7 +475,7 @@ PlaYUVerFramePelFormat RGB24 =
1,
frameFromBufferRGB24,
bufferFromFrameRGB24,
fillRGBbufferRGB,
fillARGB32bufferRGB,
ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_RGB24 ), };
/** BGR 8 bits per pixel / 3 channels */
@@ -490,7 +497,7 @@ PlaYUVerFramePelFormat BGR24 =
1,
frameFromBufferBGR24,
bufferFromFrameBGR24,
fillRGBbufferRGB,
fillARGB32bufferRGB,
ADD_FFMPEG_PEL_FMT( AV_PIX_FMT_BGR24 ), };
PlaYUVerFramePelFormat g_PlaYUVerFramePelFormatsList[PLAYUVER_NUMBER_FORMATS] =
+6 -1
View File
@@ -31,6 +31,11 @@
namespace plaYUVer
{
#define CHROMA_RESAMPLING( X ) ( ( ( X + 1 ) >> 1 ) << 1 )
#define CHROMARATIO( SIZE, RATIO ) ( RATIO > 1 ? ( ( SIZE + 1 ) / RATIO ) : SIZE )
#define CHROMASHIFT( SIZE, SHIFT ) ( SHIFT > 0 ? ( ( SIZE + 1 ) >> SHIFT ) : SIZE )
#define CLAMP(X) X = X < 0 ? 0 : X > 255 ? 255 : X;
#define YUV2RGB( iY, iU, iV, iR, iG, iB ) \
@@ -78,7 +83,7 @@ struct structPlaYUVerFramePelFormat
Int ratioChromaHeight;
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 );
Void (*fillARGB32buffer)( Pel*** in, UChar* out, UInt width, UInt height );
Int ffmpegPelFormat;
};