Reintroduced keeping the state of failed initialization.

As Stephan said, some apps ignore the return value of Encode(),
and since _OpenCodecIfNeeded() is called from there, this could
cause wasting of cpu cycles.

Change-Id: I80e1464d8532ebf80c514685ef3a25d98d8142fb
This commit is contained in:
JackBurton79
2018-08-16 10:09:22 +02:00
parent 53086e436e
commit 1da12a78de
2 changed files with 15 additions and 7 deletions
@@ -44,7 +44,7 @@ AVCodecEncoder::AVCodecEncoder(uint32 codecID, int bitRateScale)
fCodecID((CodecID)codecID), fCodecID((CodecID)codecID),
fCodec(NULL), fCodec(NULL),
fCodecContext(avcodec_alloc_context3(NULL)), fCodecContext(avcodec_alloc_context3(NULL)),
fCodecInitialized(false), fCodecInitStatus(CODEC_INIT_NEEDED),
fFrame(av_frame_alloc()), fFrame(av_frame_alloc()),
fSwsContext(NULL), fSwsContext(NULL),
fFramesWritten(0) fFramesWritten(0)
@@ -465,9 +465,12 @@ AVCodecEncoder::_Setup()
bool bool
AVCodecEncoder::_OpenCodecIfNeeded() AVCodecEncoder::_OpenCodecIfNeeded()
{ {
if (fCodecInitialized) if (fCodecInitStatus == CODEC_INIT_DONE)
return true; return true;
if (fCodecInitStatus == CODEC_INIT_FAILED)
return false;
fCodecContext->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL; fCodecContext->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
// Some codecs need this to be set before open // Some codecs need this to be set before open
@@ -478,13 +481,13 @@ AVCodecEncoder::_OpenCodecIfNeeded()
// Open the codec // Open the codec
int result = avcodec_open2(fCodecContext, fCodec, NULL); int result = avcodec_open2(fCodecContext, fCodec, NULL);
if (result >= 0) if (result >= 0)
fCodecInitialized = true; fCodecInitStatus = CODEC_INIT_DONE;
else else
fCodecInitialized = false; fCodecInitStatus = CODEC_INIT_FAILED;
TRACE(" avcodec_open(%p, %p): %d\n", fCodecContext, fCodec, result); TRACE(" avcodec_open(%p, %p): %d\n", fCodecContext, fCodec, result);
return fCodecInitialized; return fCodecInitStatus == CODEC_INIT_DONE;
} }
@@ -75,8 +75,13 @@ private:
CodecID fCodecID; CodecID fCodecID;
AVCodec* fCodec; AVCodec* fCodec;
AVCodecContext* fCodecContext; AVCodecContext* fCodecContext;
bool fCodecInitialized; enum {
CODEC_INIT_NEEDED = 0,
CODEC_INIT_DONE,
CODEC_INIT_FAILED
};
uint32 fCodecInitStatus;
// For video (color space conversion): // For video (color space conversion):
AVPicture fSrcFrame; AVPicture fSrcFrame;