Improve frame allocation

This commit is contained in:
jfmcarreira committed 2014-04-01 02:25:14 +01:00
1 parent 2dcdfaccf5
commit b187bf6424
3 files changed
+107 -111

No files matched your search

+67 -27
View File
@@ -52,7 +52,7 @@ typedef unsigned long long UInt64;
static inline Void* xMallocMem( SizeT nitems ) static inline Void* xMallocMem( SizeT nitems )
{ {
Void *d; Void *d;
if ( (d = malloc( nitems )) == NULL ) if( ( d = malloc( nitems ) ) == NULL )
{ {
printf( "malloc failed.\n" ); printf( "malloc failed.\n" );
return NULL; return NULL;
@@ -64,13 +64,13 @@ static inline Void* xCallocMem( SizeT nitems, SizeT size )
{ {
size_t padded_size = nitems * size; size_t padded_size = nitems * size;
Void *d = xMallocMem( padded_size ); Void *d = xMallocMem( padded_size );
memset( d, 0, (int) padded_size ); memset( d, 0, ( int )padded_size );
return d; return d;
} }
static inline Void xFreePointer( Void *pointer ) static inline Void xFreePointer( Void *pointer )
{ {
if ( pointer != NULL ) if( pointer != NULL )
{ {
free( pointer ); free( pointer );
pointer = NULL; pointer = NULL;
@@ -96,15 +96,15 @@ Int getMem2D( T*** array2D, Int dim0, Int dim1 )
{ {
Int i; Int i;
if ( (*array2D = (T**) xMallocMem( dim0 * sizeof(T*) )) == NULL ) printf( if( ( *array2D = ( T** )xMallocMem( dim0 * sizeof(T*) ) ) == NULL )
"get_mem2Dint: array2D" ); printf( "get_mem2Dint: array2D" );
if ( (*(*array2D) = (T*) xCallocMem( dim0 * dim1, sizeof(T) )) == NULL ) printf( if( ( *( *array2D ) = ( T* )xCallocMem( dim0 * dim1, sizeof(T) ) ) == NULL )
"get_mem2Dint: array2D" ); printf( "get_mem2Dint: array2D" );
for ( i = 1; i < dim0; i++ ) for( i = 1; i < dim0; i++ )
(*array2D)[i] = (*array2D)[i - 1] + dim1; ( *array2D )[i] = ( *array2D )[i - 1] + dim1;
return dim0 * (sizeof(T*) + dim1 * sizeof(T)); return dim0 * ( sizeof(T*) + dim1 * sizeof(T) );
} }
template<typename T> template<typename T>
@@ -112,13 +112,13 @@ Int getMem3D( T**** array3D, Int dim0, Int dim1, Int dim2 )
{ {
Int i, mem_size = dim0 * sizeof(T**); Int i, mem_size = dim0 * sizeof(T**);
if ( ((*array3D) = (T***) xMallocMem( dim0 * sizeof(T**) )) == NULL ) printf( if( ( ( *array3D ) = ( T*** )xMallocMem( dim0 * sizeof(T**) ) ) == NULL )
"get_mem3Dint: array3D" ); printf( "get_mem3Dint: array3D" );
mem_size += getMem2D( *array3D, dim0 * dim1, dim2 ); mem_size += getMem2D( *array3D, dim0 * dim1, dim2 );
for ( i = 1; i < dim0; i++ ) for( i = 1; i < dim0; i++ )
(*array3D)[i] = (*array3D)[i - 1] + dim1; ( *array3D )[i] = ( *array3D )[i - 1] + dim1;
return mem_size; return mem_size;
} }
@@ -128,13 +128,13 @@ Int getMem4D( T***** array4D, Int dim0, Int dim1, Int dim2, Int dim3 )
{ {
int i, mem_size = dim0 * sizeof(int***); int i, mem_size = dim0 * sizeof(int***);
if ( ((*array4D) = (T****) xMallocMem( dim0 * sizeof(T***) )) == NULL ) printf( if( ( ( *array4D ) = ( T**** )xMallocMem( dim0 * sizeof(T***) ) ) == NULL )
"get_mem4Dint: array4D" ); printf( "get_mem4Dint: array4D" );
mem_size += getMem3D( *array4D, dim0 * dim1, dim2, dim3 ); mem_size += getMem3D( *array4D, dim0 * dim1, dim2, dim3 );
for ( i = 1; i < dim0; i++ ) for( i = 1; i < dim0; i++ )
(*array4D)[i] = (*array4D)[i - 1] + dim1; ( *array4D )[i] = ( *array4D )[i - 1] + dim1;
return mem_size; return mem_size;
} }
@@ -144,13 +144,40 @@ Int getMem5D( T****** array5D, Int dim0, Int dim1, Int dim2, Int dim3, Int dim4
{ {
int i, mem_size = dim0 * sizeof(T****); int i, mem_size = dim0 * sizeof(T****);
if ( ((*array5D) = (T*****) xMallocMem( dim0 * sizeof(T****) )) == NULL ) printf( if( ( ( *array5D ) = ( T***** )xMallocMem( dim0 * sizeof(T****) ) ) == NULL )
"get_mem5Dint: array5D" ); printf( "get_mem5Dint: array5D" );
mem_size += getMem4D( *array5D, dim0 * dim1, dim2, dim3, dim4 ); mem_size += getMem4D( *array5D, dim0 * dim1, dim2, dim3, dim4 );
for ( i = 1; i < dim0; i++ ) for( i = 1; i < dim0; i++ )
(*array5D)[i] = (*array5D)[i - 1] + dim1; ( *array5D )[i] = ( *array5D )[i - 1] + dim1;
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;
( *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; return mem_size;
} }
@@ -167,9 +194,9 @@ Void freeMem1D( T *array1D )
template<typename T> template<typename T>
Void freeMem2D( T** array2D ) Void freeMem2D( T** array2D )
{ {
if ( array2D ) if( array2D )
{ {
if ( *array2D ) if( *array2D )
xFreeMem( *array2D ); xFreeMem( *array2D );
else else
printf( "free_mem2Dint: trying to free unused memory" ); printf( "free_mem2Dint: trying to free unused memory" );
@@ -185,7 +212,7 @@ Void freeMem2D( T** array2D )
template<typename T> template<typename T>
Void freeMem3D( T*** array3D ) Void freeMem3D( T*** array3D )
{ {
if ( array3D ) if( array3D )
{ {
freeMem2D( *array3D ); freeMem2D( *array3D );
xFreeMem( array3D ); xFreeMem( array3D );
@@ -199,7 +226,7 @@ Void freeMem3D( T*** array3D )
template<typename T> template<typename T>
Void freeMem4D( T**** array4D ) Void freeMem4D( T**** array4D )
{ {
if ( array4D ) if( array4D )
{ {
freeMem3D( *array4D ); freeMem3D( *array4D );
xFreeMem( array4D ); xFreeMem( array4D );
@@ -213,7 +240,7 @@ Void freeMem4D( T**** array4D )
template<typename T> template<typename T>
Void freeMem5D( T***** array5D ) Void freeMem5D( T***** array5D )
{ {
if ( array5D ) if( array5D )
{ {
freeMem4D( *array5D ); freeMem4D( *array5D );
xFreeMem( array5D ); xFreeMem( array5D );
@@ -224,6 +251,19 @@ Void freeMem5D( T***** array5D )
} }
} }
template<typename T>
Void freeMem3ImageComponents( T*** array3D )
{
if( array3D )
{
if( **array3D )
xFreeMem( **array3D );
freeMem2D<T*>( array3D );
}
}
} // NAMESPACE } // NAMESPACE
#endif // __LIBMEMORY_H__ #endif // __LIBMEMORY_H__
+39 -83
View File
@@ -32,10 +32,7 @@ namespace plaYUVer
PlaYUVerFrame::PlaYUVerFrame( UInt width, UInt height, Int pel_format ) PlaYUVerFrame::PlaYUVerFrame( UInt width, UInt height, Int pel_format )
{ {
m_pppcInputPel[0] = NULL; m_pppcInputPel = NULL;
m_pppcInputPel[1] = NULL;
m_pppcInputPel[2] = NULL;
m_uiWidth = width; m_uiWidth = width;
m_uiHeight = height; m_uiHeight = height;
m_iPixelFormat = pel_format; m_iPixelFormat = pel_format;
@@ -45,48 +42,32 @@ PlaYUVerFrame::PlaYUVerFrame( UInt width, UInt height, Int pel_format )
switch( m_iPixelFormat ) switch( m_iPixelFormat )
{ {
case YUV420p: case YUV420p:
get_mem2Dpel( &( m_pppcInputPel[LUMA] ), m_uiHeight, m_uiWidth ); getMem3ImageComponents<Pel>( &m_pppcInputPel, m_uiHeight, m_uiWidth, 2, 2 );
get_mem2Dpel( &( m_pppcInputPel[CHROMA_U] ), m_uiHeight / 2, m_uiWidth / 2 );
get_mem2Dpel( &( m_pppcInputPel[CHROMA_V] ), m_uiHeight / 2, m_uiWidth / 2 );
break; break;
case YUV444p: case YUV444p:
get_mem2Dpel( &( m_pppcInputPel[LUMA] ), m_uiHeight, m_uiWidth ); getMem3ImageComponents<Pel>( &m_pppcInputPel, m_uiHeight, m_uiWidth, 1, 1 );
get_mem2Dpel( &( m_pppcInputPel[CHROMA_U] ), m_uiHeight, m_uiWidth );
get_mem2Dpel( &( m_pppcInputPel[CHROMA_V] ), m_uiHeight, m_uiWidth );
break; break;
case YUV422p: case YUV422p:
get_mem2Dpel( &( m_pppcInputPel[LUMA] ), m_uiHeight, m_uiWidth ); getMem3ImageComponents<Pel>( &m_pppcInputPel, m_uiHeight, m_uiWidth, 1, 2 );
get_mem2Dpel( &( m_pppcInputPel[CHROMA_U] ), m_uiHeight, m_uiWidth / 2);
get_mem2Dpel( &( m_pppcInputPel[CHROMA_V] ), m_uiHeight, m_uiWidth / 2 );
break; break;
case YUV410p: case YUV410p:
case YUV411p: case YUV411p:
get_mem2Dpel( &( m_pppcInputPel[LUMA] ), m_uiHeight, m_uiWidth );
get_mem2Dpel( &( m_pppcInputPel[CHROMA_U] ), m_uiHeight, m_uiWidth );
get_mem2Dpel( &( m_pppcInputPel[CHROMA_V] ), m_uiHeight, m_uiWidth );
break; break;
case GRAY: case GRAY:
get_mem2Dpel( &( m_pppcInputPel[0] ), m_uiHeight, m_uiWidth ); getMem3ImageComponents<Pel>( &m_pppcInputPel, m_uiHeight, m_uiWidth, 4, 4 );
break; break;
case RGB8: case RGB8:
get_mem2Dpel( &( m_pppcInputPel[COLOR_R] ), m_uiHeight, m_uiWidth ); getMem3ImageComponents<Pel>( &m_pppcInputPel, m_uiHeight, m_uiWidth, 1, 1 );
get_mem2Dpel( &( m_pppcInputPel[COLOR_G] ), m_uiHeight, m_uiWidth );
get_mem2Dpel( &( m_pppcInputPel[COLOR_B] ), m_uiHeight, m_uiWidth );
break; break;
} }
getMem1D<Pel>( &m_pcRGBPelInterlaced, m_uiHeight * m_uiWidth * 3 ); getMem1D<Pel>( &m_pcRGBPelInterlaced, m_uiHeight * m_uiWidth * 3 );
openPixfc(); openPixfc();
} }
PlaYUVerFrame::~PlaYUVerFrame() PlaYUVerFrame::~PlaYUVerFrame()
{ {
if( m_pppcInputPel[0] ) if( m_pppcInputPel )
free_mem2Dpel( m_pppcInputPel[LUMA] ); freeMem3ImageComponents<Pel>( m_pppcInputPel );
if( m_pppcInputPel[1] )
free_mem2Dpel( m_pppcInputPel[CHROMA_U] );
if( m_pppcInputPel[2] )
free_mem2Dpel( m_pppcInputPel[CHROMA_V] );
if( m_pcRGBPelInterlaced ) if( m_pcRGBPelInterlaced )
freeMem1D<UChar>( m_pcRGBPelInterlaced ); freeMem1D<UChar>( m_pcRGBPelInterlaced );
@@ -94,15 +75,15 @@ PlaYUVerFrame::~PlaYUVerFrame()
closePixfc(); closePixfc();
} }
Void PlaYUVerFrame::openPixfc() Void PlaYUVerFrame::openPixfc()
{ {
#ifdef USE_PIXFC #ifdef USE_PIXFC
m_pcPixfc = NULL; m_pcPixfc = NULL;
PixFcPixelFormat input_format = PixFcYUV420P; PixFcPixelFormat input_format = PixFcYUV420P;
PixFcFlag flags = (PixFcFlag)(PixFcFlag_Default + PixFcFlag_NNbResamplingOnly); PixFcFlag flags = (PixFcFlag)(PixFcFlag_Default + PixFcFlag_NNbResamplingOnly);
UInt input_row_size = m_uiWidth * 1.5; // size of each row in ARGB output buffer (in bytes) UInt input_row_size = m_uiWidth;
if (create_pixfc(&m_pcPixfc, input_format, PixFcRGB24, m_uiWidth, m_uiHeight, input_row_size, m_uiWidth * 3, flags ) != 0) { if (create_pixfc(&m_pcPixfc, input_format, PixFcRGB24, m_uiWidth, m_uiHeight, input_row_size, m_uiWidth * 3, flags ) != 0)
{
fprintf(stderr, "Error creating struct pixfc\n"); fprintf(stderr, "Error creating struct pixfc\n");
m_pcPixfc = NULL; m_pcPixfc = NULL;
return; return;
@@ -114,57 +95,31 @@ Void PlaYUVerFrame::closePixfc()
{ {
#ifdef USE_PIXFC #ifdef USE_PIXFC
if( m_pcPixfc ) if( m_pcPixfc )
destroy_pixfc(m_pcPixfc); destroy_pixfc(m_pcPixfc);
#endif #endif
} }
#ifdef USE_PIXFC #ifdef USE_PIXFC
inline Void PlaYUVerFrame::FrametoRGB8Pixfc() inline Void PlaYUVerFrame::FrametoRGB8Pixfc()
{ {
Pel* pInputPelY = &( m_pppcInputPel[LUMA][0][0] );
Pel *pRGBPelR = NULL, *pRGBPelG = NULL, *pRGBPelB = NULL;
if( m_pppcRGBPel )
{
pRGBPelR = &( m_pppcRGBPel[COLOR_R][0][0] );
pRGBPelG = &( m_pppcRGBPel[COLOR_G][0][0] );
pRGBPelB = &( m_pppcRGBPel[COLOR_B][0][0] );
}
Pel* pcRGBPelInterlaced = m_pcRGBPelInterlaced;
switch( m_iPixelFormat ) switch( m_iPixelFormat )
{ {
case YUV420p: case YUV420p:
m_pcPixfc->convert(m_pcPixfc, pInputPelY, pRGBPelR); m_pcPixfc->convert(m_pcPixfc, m_pppcInputPel, m_pcRGBPelInterlaced);
for( UInt y = 0; y < m_uiHeight; y++ )
for( UInt x = 0; x < m_uiWidth; x++ )
{
*pcRGBPelInterlaced++ = *pRGBPelR++;
*pcRGBPelInterlaced++ = *pRGBPelG++;
*pcRGBPelInterlaced++ = *pRGBPelB++;
}
break; break;
case YUV422p: case YUV422p:
break; break;
case YUV444p: case YUV444p:
m_pcPixfc->convert(m_pcPixfc, pInputPelY, pRGBPelR); m_pcPixfc->convert(m_pcPixfc, m_pppcInputPel, m_pcRGBPelInterlaced);
for( UInt y = 0; y < m_uiHeight; y++ )
for( UInt x = 0; x < m_uiWidth; x++ )
{
*pcRGBPelInterlaced++ = *pRGBPelR++;
*pcRGBPelInterlaced++ = *pRGBPelG++;
*pcRGBPelInterlaced++ = *pRGBPelB++;
}
break; break;
case YUV410p: case YUV410p:
case YUV411p: case YUV411p:
break; break;
case GRAY: case GRAY:
break; break;
case RGB8: case RGB8:
break; break;
default: default:
// No action. // No action.
break; break;
} }
@@ -216,7 +171,6 @@ static inline Void yuvToRgb( Int iY, Int iU, Int iV, Int &iR, Int &iG, Int &iB )
iB = 255; iB = 255;
} }
template<typename T> template<typename T>
Void rgbToyuv( T iR, T iG, T iB, T &iY, T &iU, T &iV ) Void rgbToyuv( T iR, T iG, T iB, T &iY, T &iU, T &iV )
{ {
@@ -461,26 +415,28 @@ UInt64 PlaYUVerFrame::getBytesPerFrame()
Pixel PlaYUVerFrame::ConvertPixel( Pixel sInputPixel, ColorSpace eOutputSpace ) Pixel PlaYUVerFrame::ConvertPixel( Pixel sInputPixel, ColorSpace eOutputSpace )
{ {
Pixel sOutputPixel = {COLOR_INVALID,0,0,0}; Pixel sOutputPixel =
{
COLOR_INVALID,
0,
0,
0 };
if( sInputPixel.color_space == eOutputSpace ) if( sInputPixel.color_space == eOutputSpace )
return sInputPixel; return sInputPixel;
if( eOutputSpace == COLOR_RGB ) if( eOutputSpace == COLOR_RGB )
{ {
yuvToRgb( sInputPixel.Luma, sInputPixel.ChromaU, sInputPixel.ChromaU, yuvToRgb( sInputPixel.Luma, sInputPixel.ChromaU, sInputPixel.ChromaU, sOutputPixel.ColorR, sOutputPixel.ColorG, sOutputPixel.ColorB );
sOutputPixel.ColorR, sOutputPixel.ColorG, sOutputPixel.ColorB );
} }
if( eOutputSpace == COLOR_YUV ) if( eOutputSpace == COLOR_YUV )
{ {
rgbToyuv( sInputPixel.ColorR, sInputPixel.ColorG, sInputPixel.ColorB, rgbToyuv( sInputPixel.ColorR, sInputPixel.ColorG, sInputPixel.ColorB, sOutputPixel.Luma, sOutputPixel.ChromaU, sOutputPixel.ChromaU );
sOutputPixel.Luma, sOutputPixel.ChromaU, sOutputPixel.ChromaU );
} }
return sOutputPixel; return sOutputPixel;
} }
Pixel PlaYUVerFrame::getPixelValue( const QPoint &pos, ColorSpace space )
Pixel PlaYUVerFrame::getPixelValue(const QPoint &pos, ColorSpace space)
{ {
Pixel pixel_value; Pixel pixel_value;
switch( m_iPixelFormat ) switch( m_iPixelFormat )
@@ -488,8 +444,8 @@ Pixel PlaYUVerFrame::getPixelValue(const QPoint &pos, ColorSpace space)
case YUV420p: case YUV420p:
pixel_value.color_space = COLOR_YUV; pixel_value.color_space = COLOR_YUV;
pixel_value.Luma = m_pppcInputPel[LUMA][pos.y()][pos.x()]; pixel_value.Luma = m_pppcInputPel[LUMA][pos.y()][pos.x()];
pixel_value.ChromaU = m_pppcInputPel[CHROMA_U][pos.y()>>1][pos.x()>>1]; pixel_value.ChromaU = m_pppcInputPel[CHROMA_U][pos.y() >> 1][pos.x() >> 1];
pixel_value.ChromaV = m_pppcInputPel[CHROMA_V][pos.y()>>1][pos.x()>>1]; pixel_value.ChromaV = m_pppcInputPel[CHROMA_V][pos.y() >> 1][pos.x() >> 1];
break; break;
case YUV444p: case YUV444p:
pixel_value.Luma = m_pppcInputPel[LUMA][pos.y()][pos.x()]; pixel_value.Luma = m_pppcInputPel[LUMA][pos.y()][pos.x()];
@@ -499,14 +455,14 @@ Pixel PlaYUVerFrame::getPixelValue(const QPoint &pos, ColorSpace space)
case YUV422p: case YUV422p:
pixel_value.color_space = COLOR_YUV; pixel_value.color_space = COLOR_YUV;
pixel_value.Luma = m_pppcInputPel[LUMA][pos.y()][pos.x()]; pixel_value.Luma = m_pppcInputPel[LUMA][pos.y()][pos.x()];
pixel_value.ChromaU = m_pppcInputPel[CHROMA_U][pos.y()][pos.x()>>1]; pixel_value.ChromaU = m_pppcInputPel[CHROMA_U][pos.y()][pos.x() >> 1];
pixel_value.ChromaV = m_pppcInputPel[CHROMA_V][pos.y()][pos.x()>>1]; pixel_value.ChromaV = m_pppcInputPel[CHROMA_V][pos.y()][pos.x() >> 1];
break; break;
case YUV410p: case YUV410p:
pixel_value.color_space = COLOR_YUV; pixel_value.color_space = COLOR_YUV;
pixel_value.Luma = m_pppcInputPel[LUMA][pos.y()][pos.x()]; pixel_value.Luma = m_pppcInputPel[LUMA][pos.y()][pos.x()];
pixel_value.ChromaU = m_pppcInputPel[CHROMA_U][pos.y()>>2][pos.x()>>2]; pixel_value.ChromaU = m_pppcInputPel[CHROMA_U][pos.y() >> 2][pos.x() >> 2];
pixel_value.ChromaV = m_pppcInputPel[CHROMA_V][pos.y()>>2][pos.x()>>2]; pixel_value.ChromaV = m_pppcInputPel[CHROMA_V][pos.y() >> 2][pos.x() >> 2];
break; break;
case GRAY: case GRAY:
pixel_value.color_space = COLOR_YUV; pixel_value.color_space = COLOR_YUV;
@@ -527,7 +483,6 @@ Pixel PlaYUVerFrame::getPixelValue(const QPoint &pos, ColorSpace space)
return pixel_value; return pixel_value;
} }
// Format conversion // Format conversion
#ifdef USE_OPENCV #ifdef USE_OPENCV
@@ -536,4 +491,5 @@ cv::Mat PlaYUVerFrame::getCvMat()
return cv::Mat(); return cv::Mat();
} }
#endif #endif
} // NAMESPACE }
// NAMESPACE
+1 -1
View File
@@ -128,7 +128,7 @@ private:
Int m_iPixelFormat; Int m_iPixelFormat;
Bool m_bHasRGBPel; Bool m_bHasRGBPel;
Pel** m_pppcInputPel[3]; Pel*** m_pppcInputPel;
UChar* m_pcRGBPelInterlaced; UChar* m_pcRGBPelInterlaced;
Void openPixfc(); Void openPixfc();