Skip to content

Commit 47c4c43

Browse files
committed
Supress avcodec_close() deprecation warning
Note, CodecContext2::close() function marked as deprecated for a long time.
1 parent 69b6bd7 commit 47c4c43

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED yes)
2323
# Warnings
2424
set (AVCPP_WARNING_OPTIONS
2525
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
26-
-Wall -Wextra>
26+
-Wall -Wextra -Werror=return-type -Wwrite-strings>
2727
$<$<CXX_COMPILER_ID:MSVC>:
2828
/W4>)
2929

src/avutils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ extern "C" {
2828
#define API_AVFORMAT_URL ((LIBAVFORMAT_VERSION_MAJOR > 58) || (LIBAVFORMAT_VERSION_MAJOR == 58 && LIBAVFORMAT_VERSION_MINOR >= 7))
2929
// net key frame flags: AV_FRAME_FLAG_KEY (FFmpeg 6.1)
3030
#define API_FRAME_KEY ((LIBAVUTIL_VERSION_MAJOR > 58) || (LIBAVUTIL_VERSION_MAJOR == 58 && LIBAVUTIL_VERSION_MINOR >= 29))
31+
// avcodec_close() support
32+
#define API_AVCODEC_CLOSE (LIBAVCODEC_VERSION_MAJOR < 61)
3133

3234
#if defined(__ICL) || defined (__INTEL_COMPILER)
3335
# define FF_DISABLE_DEPRECATION_WARNINGS __pragma(warning(push)) __pragma(warning(disable:1478))

src/codeccontext.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,71 @@ void CodecContext2::open(Dictionary &options, const Codec &codec, OptionalErrorC
448448
options.assign(prt);
449449
}
450450

451+
namespace {
452+
// Close code in new way: recreate context with parameters coping
453+
// Use legacy avcodec_close() for old ffmpeg versions.
454+
int codec_close(AVCodecContext*& ctx)
455+
{
456+
#if API_AVCODEC_CLOSE
457+
return avcodec_close(ctx);
458+
#else
459+
AVCodecContext *ctxNew = nullptr;
460+
AVCodecParameters *parTmp = nullptr;
461+
462+
int ret;
463+
464+
ctxNew = avcodec_alloc_context3(ctx->codec);
465+
if (!ctxNew) {
466+
ret = AVERROR(ENOMEM);
467+
goto fail;
468+
}
469+
470+
parTmp = avcodec_parameters_alloc();
471+
if (!parTmp) {
472+
ret = AVERROR(ENOMEM);
473+
goto fail;
474+
}
475+
476+
ret = avcodec_parameters_from_context(parTmp, ctx);
477+
if (ret < 0)
478+
goto fail;
479+
480+
ret = avcodec_parameters_to_context(ctxNew, parTmp);
481+
if (ret < 0)
482+
goto fail;
483+
484+
ctxNew->pkt_timebase = ctx->pkt_timebase;
485+
486+
487+
#if (LIBAVCODEC_VERSION_MAJOR < 61) || (defined(FF_API_TICKS_PER_FRAME) && FF_API_TICKS_PER_FRAME)
488+
FF_DISABLE_DEPRECATION_WARNINGS
489+
ctxNew->ticks_per_frame = ctx->ticks_per_frame;
490+
FF_ENABLE_DEPRECATION_WARNINGS
491+
#endif
492+
493+
avcodec_free_context(&ctx);
494+
ctx = ctxNew;
495+
496+
ctxNew = nullptr;
497+
ret = 0;
498+
499+
fail:
500+
avcodec_free_context(&ctxNew);
501+
avcodec_parameters_free(&parTmp);
502+
503+
return ret;
504+
#endif
505+
}
506+
}
507+
451508
void CodecContext2::close(OptionalErrorCode ec)
452509
{
453510
clear_if(ec);
454511
if (isOpened())
455512
{
456-
avcodec_close(m_raw);
513+
if (auto sts = codec_close(m_raw); sts) {
514+
throws_if(ec, sts, ffmpeg_category());
515+
}
457516
return;
458517
}
459518
throws_if(ec, Errors::CodecNotOpened);

0 commit comments

Comments
 (0)