ffmpeg: improve context allocation

The context can be better allocated if it knows which codec it will be
used with (preallocating things with the correct size). To do this we
need to delay allocation from the constructor to the Init method.
This commit is contained in:
Adrien Destugues
2020-09-05 18:38:25 +02:00
parent 628ea9eeb0
commit 3cf5015980
@@ -43,7 +43,7 @@ AVCodecEncoder::AVCodecEncoder(uint32 codecID, int bitRateScale)
fBitRateScale(bitRateScale),
fCodecID((CodecID)codecID),
fCodec(NULL),
fCodecContext(avcodec_alloc_context3(NULL)),
fCodecContext(NULL),
fCodecInitStatus(CODEC_INIT_NEEDED),
fFrame(av_frame_alloc()),
fSwsContext(NULL),
@@ -110,7 +110,10 @@ AVCodecEncoder::~AVCodecEncoder()
av_frame_free(&fFrame);
}
avcodec_free_context(&fCodecContext);
if (fCodecContext != NULL) {
avcodec_close(fCodecContext);
avcodec_free_context(&fCodecContext);
}
delete[] fChunkBuffer;
}
@@ -140,9 +143,6 @@ AVCodecEncoder::SetUp(const media_format* inputFormat)
{
TRACE("AVCodecEncoder::SetUp()\n");
if (fCodecContext == NULL)
return B_NO_INIT;
if (inputFormat == NULL)
return B_BAD_VALUE;
@@ -271,6 +271,10 @@ AVCodecEncoder::_Setup()
int rawBitRate;
fCodecContext = avcodec_alloc_context3(fCodec);
if (fCodecContext == NULL)
return B_NO_INIT;
if (fInputFormat.type == B_MEDIA_RAW_VIDEO) {
TRACE(" B_MEDIA_RAW_VIDEO\n");
@@ -278,7 +282,7 @@ AVCodecEncoder::_Setup()
AVPixelFormat pixFmt = colorspace_to_pixfmt(
fInputFormat.u.raw_video.display.format);
if (pixFmt == AV_PIX_FMT_NONE) {
TRACE("Invalid input colorspace");
TRACE("Invalid input colorspace\n");
return B_BAD_DATA;
}