Skip to content

Commit 387f109

Browse files
committed
Don't use deprecated avcodec_close()
Use avcodec_free_context() straight away available on all our targets (lavc 55.52.0 2014)
1 parent 851e618 commit 387f109

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

src/encoder/encoderffmpegcore.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ EncoderFfmpegCore::~EncoderFfmpegCore() {
7070

7171

7272
if (m_pStream != NULL) {
73-
avcodec_close(m_pStream->codec);
73+
avcodec_free_context(&m_pStream->codec);
7474
}
7575

7676
if (m_pEncodeFormatCtx != NULL) {
@@ -432,7 +432,7 @@ int EncoderFfmpegCore::writeAudioFrame(AVFormatContext *formatctx,
432432

433433

434434
void EncoderFfmpegCore::closeAudio(AVStream *stream) {
435-
avcodec_close(stream->codec);
435+
avcodec_free_context(&stream->codec);
436436
av_free(m_pSamples);
437437
}
438438

src/sources/soundsourceffmpeg.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,26 @@ SINT readNextPacket(
958958
}
959959
} // namespace
960960

961+
bool SoundSourceFFmpeg::deepFlushBuffers() {
962+
bool ret = false;
963+
AVCodecParameters* pParams = avcodec_parameters_alloc();
964+
if (!pParams) {
965+
return false;
966+
}
967+
avcodec_parameters_from_context(pParams, m_pavCodecContext);
968+
const AVCodec* pCodec = m_pavCodecContext->codec;
969+
AVCodecContextPtr pavCodecContext = AVCodecContextPtr::alloc(pCodec);
970+
if (pavCodecContext) {
971+
avcodec_parameters_to_context(pavCodecContext, pParams);
972+
if (avcodec_open2(pavCodecContext, pCodec, nullptr) == 0) {
973+
m_pavCodecContext = std::move(pavCodecContext);
974+
ret = true;
975+
}
976+
}
977+
avcodec_parameters_free(&pParams);
978+
return ret;
979+
}
980+
961981
bool SoundSourceFFmpeg::adjustCurrentPosition(SINT startIndex) {
962982
DEBUG_ASSERT(frameIndexRange().containsIndex(startIndex));
963983

@@ -989,9 +1009,11 @@ bool SoundSourceFFmpeg::adjustCurrentPosition(SINT startIndex) {
9891009
// was limited to -661 instead of -2111. The workaround here is to reopen
9901010
// the codec which initializes all buffers with zero.
9911011
// Slow: 43 us (Core Ultra 5 125U)
992-
const AVCodec* pCodec = m_pavCodecContext->codec;
993-
avcodec_close(m_pavCodecContext);
994-
avcodec_open2(m_pavCodecContext, pCodec, nullptr);
1012+
if (!deepFlushBuffers()) {
1013+
kLogger.warning() << "deepFlushBuffers failed";
1014+
m_frameBuffer.invalidate();
1015+
return false;
1016+
}
9951017
}
9961018

9971019
// Seek to new position

src/sources/soundsourceffmpeg.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class SoundSourceFFmpeg : public SoundSource {
3333
private:
3434
const CSAMPLE* resampleDecodedAVFrame(AVFrame* pavDecodedFrame);
3535

36+
// recreates the AVCodecContext for cases where the lightweight
37+
// avcodec_flush_buffers() is not sufficient
38+
bool deepFlushBuffers();
39+
3640
// Seek to the requested start index (if needed) or return false
3741
// upon seek errors.
3842
bool adjustCurrentPosition(

0 commit comments

Comments
 (0)