Skip to content

Commit

Permalink
[omximage] Fall back to arm jpeg encode/decode when gpu is busy
Browse files Browse the repository at this point in the history
  • Loading branch information
popcornmix committed Apr 3, 2020
1 parent 5281049 commit 9a5d864
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
50 changes: 41 additions & 9 deletions xbmc/cores/omxplayer/OMXImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ static XbmcThreads::ConditionVariable g_count_cond;
static CCriticalSection g_count_lock;
static int g_count_val;

static void limit_calls_enter()
static bool limit_calls_enter()
{
CSingleLock lock(g_count_lock);
// on Pi2 fall back to arm decode if the queue is getting big
if (g_RBP.RaspberryPiVersion() > 1 && g_count_val >= 2)
return false;

while (g_count_val >= 3)
g_count_cond.wait(lock);
g_count_val++;
return true;
}

static void limit_calls_leave()
Expand Down Expand Up @@ -99,6 +104,9 @@ bool COMXImage::CreateThumbnailFromSurface(unsigned char* buffer, unsigned int w
unsigned int format, unsigned int pitch, const std::string& destFile)
{
COMXImageEnc omxImageEnc;
if (!omxImageEnc.Gpu())
return false;

bool ret = omxImageEnc.CreateThumbnailFromSurface(buffer, width, height, format, pitch, destFile);
if (!ret)
CLog::Log(LOGNOTICE, "%s: unable to create thumbnail %s %dx%d", __func__, destFile.c_str(), width, height);
Expand Down Expand Up @@ -194,6 +202,8 @@ bool COMXImage::CreateThumb(const std::string& srcFile, unsigned int maxHeight,
bool okay = false;
COMXImageFile file;
COMXImageReEnc reenc;
if (!reenc.Gpu())
return false;
void *pDestBuffer;
unsigned int nDestSize;
int orientation = additional_info == "flipped" ? 1:0;
Expand Down Expand Up @@ -299,6 +309,9 @@ bool COMXImage::DecodeJpegToTexture(COMXImageFile *file, unsigned int width, uns
bool ret = false;
COMXTexture omx_image;

if (!omx_image.Gpu())
return false;

struct textureinfo *tex = new struct textureinfo;
if (!tex)
return NULL;
Expand Down Expand Up @@ -912,7 +925,7 @@ bool COMXImageFile::ReadFile(const std::string& inputFile, int orientation)

COMXImageDec::COMXImageDec()
{
limit_calls_enter();
m_gpu = limit_calls_enter();
m_decoded_buffer = NULL;
OMX_INIT_STRUCTURE(m_decoded_format);
m_success = false;
Expand All @@ -924,7 +937,8 @@ COMXImageDec::~COMXImageDec()

OMX_INIT_STRUCTURE(m_decoded_format);
m_decoded_buffer = NULL;
limit_calls_leave();
if (m_gpu)
limit_calls_leave();
}

void COMXImageDec::Close()
Expand Down Expand Up @@ -1074,6 +1088,9 @@ bool COMXImageDec::HandlePortSettingChange(unsigned int resize_width, unsigned i

bool COMXImageDec::Decode(const uint8_t *demuxer_content, unsigned demuxer_bytes, unsigned width, unsigned height, unsigned stride, void *pixels)
{
if (!m_gpu)
return false;

CSingleLock lock(m_OMXSection);
OMX_ERRORTYPE omx_err = OMX_ErrorNone;
OMX_BUFFERHEADERTYPE *omx_buffer = NULL;
Expand Down Expand Up @@ -1211,7 +1228,7 @@ bool COMXImageDec::Decode(const uint8_t *demuxer_content, unsigned demuxer_bytes

COMXImageEnc::COMXImageEnc()
{
limit_calls_enter();
m_gpu = limit_calls_enter();
CSingleLock lock(m_OMXSection);
OMX_INIT_STRUCTURE(m_encoded_format);
m_encoded_buffer = NULL;
Expand All @@ -1235,11 +1252,15 @@ COMXImageEnc::~COMXImageEnc()
m_omx_encoder.Deinitialize();
}
}
limit_calls_leave();
if (m_gpu)
limit_calls_leave();
}

bool COMXImageEnc::Encode(unsigned char *buffer, int size, unsigned width, unsigned height, unsigned int pitch)
{
if (!m_gpu)
return false;

CSingleLock lock(m_OMXSection);

unsigned int demuxer_bytes = 0;
Expand Down Expand Up @@ -1420,6 +1441,9 @@ bool COMXImageEnc::Encode(unsigned char *buffer, int size, unsigned width, unsig
bool COMXImageEnc::CreateThumbnailFromSurface(unsigned char* buffer, unsigned int width, unsigned int height,
unsigned int format, unsigned int pitch, const std::string& destFile)
{
if (!m_gpu)
return false;

if(format != XB_FMT_A8R8G8B8 || !buffer)
{
CLog::Log(LOGDEBUG, "%s::%s : %s failed format=0x%x\n", CLASSNAME, __func__, destFile.c_str(), format);
Expand Down Expand Up @@ -1453,7 +1477,7 @@ bool COMXImageEnc::CreateThumbnailFromSurface(unsigned char* buffer, unsigned in

COMXImageReEnc::COMXImageReEnc()
{
limit_calls_enter();
m_gpu = limit_calls_enter();
m_encoded_buffer = NULL;
m_pDestBuffer = NULL;
m_nDestAllocSize = 0;
Expand All @@ -1467,7 +1491,8 @@ COMXImageReEnc::~COMXImageReEnc()
free (m_pDestBuffer);
m_pDestBuffer = NULL;
m_nDestAllocSize = 0;
limit_calls_leave();
if (m_gpu)
limit_calls_leave();
}

void COMXImageReEnc::Close()
Expand Down Expand Up @@ -1759,6 +1784,9 @@ bool COMXImageReEnc::HandlePortSettingChange(unsigned int resize_width, unsigned

bool COMXImageReEnc::ReEncode(COMXImageFile &srcFile, unsigned int maxWidth, unsigned int maxHeight, void * &pDestBuffer, unsigned int &nDestSize)
{
if (!m_gpu)
return false;

CSingleLock lock(m_OMXSection);
OMX_ERRORTYPE omx_err = OMX_ErrorNone;

Expand Down Expand Up @@ -1931,14 +1959,15 @@ bool COMXImageReEnc::ReEncode(COMXImageFile &srcFile, unsigned int maxWidth, uns

COMXTexture::COMXTexture()
{
limit_calls_enter();
m_gpu = limit_calls_enter();
m_success = false;
}

COMXTexture::~COMXTexture()
{
Close();
limit_calls_leave();
if (m_gpu)
limit_calls_leave();
}

void COMXTexture::Close()
Expand Down Expand Up @@ -2123,6 +2152,9 @@ bool COMXTexture::HandlePortSettingChange(unsigned int resize_width, unsigned in

bool COMXTexture::Decode(const uint8_t *demuxer_content, unsigned demuxer_bytes, unsigned int width, unsigned int height, void *egl_image)
{
if (!m_gpu)
return false;

CSingleLock lock(m_OMXSection);
OMX_ERRORTYPE omx_err = OMX_ErrorNone;

Expand Down
7 changes: 7 additions & 0 deletions xbmc/cores/omxplayer/OMXImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class COMXImageDec
OMX_PARAM_PORTDEFINITIONTYPE m_decoded_format;
CCriticalSection m_OMXSection;
bool m_success;
bool m_gpu;
};

class COMXImageEnc
Expand All @@ -123,6 +124,7 @@ class COMXImageEnc
// Required overrides
bool CreateThumbnailFromSurface(unsigned char* buffer, unsigned int width, unsigned int height,
unsigned int format, unsigned int pitch, const std::string& destFile);
bool Gpu() { return m_gpu; }
protected:
bool Encode(unsigned char *buffer, int size, unsigned int width, unsigned int height, unsigned int pitch);
// Components
Expand All @@ -131,6 +133,7 @@ class COMXImageEnc
OMX_PARAM_PORTDEFINITIONTYPE m_encoded_format;
CCriticalSection m_OMXSection;
bool m_success;
bool m_gpu;
};

class COMXImageReEnc
Expand All @@ -142,6 +145,7 @@ class COMXImageReEnc
// Required overrides
void Close();
bool ReEncode(COMXImageFile &srcFile, unsigned int width, unsigned int height, void * &pDestBuffer, unsigned int &nDestSize);
bool Gpu() { return m_gpu; }
protected:
bool HandlePortSettingChange(unsigned int resize_width, unsigned int resize_height, int orientation, bool port_settings_changed);
// Components
Expand All @@ -155,6 +159,7 @@ class COMXImageReEnc
void *m_pDestBuffer;
unsigned int m_nDestAllocSize;
bool m_success;
bool m_gpu;
};

class COMXTexture
Expand All @@ -166,6 +171,7 @@ class COMXTexture
// Required overrides
void Close(void);
bool Decode(const uint8_t *data, unsigned size, unsigned int width, unsigned int height, void *egl_image);
bool Gpu() { return m_gpu; }
protected:
bool HandlePortSettingChange(unsigned int resize_width, unsigned int resize_height, void *egl_image, bool port_settings_changed);

Expand All @@ -180,6 +186,7 @@ class COMXTexture
OMX_BUFFERHEADERTYPE *m_egl_buffer;
CCriticalSection m_OMXSection;
bool m_success;
bool m_gpu;
};

extern COMXImage g_OMXImage;

0 comments on commit 9a5d864

Please sign in to comment.