Skip to content

Commit

Permalink
Supress avcodec_close() deprecation warning
Browse files Browse the repository at this point in the history
Note, CodecContext2::close() function marked as deprecated for a long
time.
  • Loading branch information
h4tr3d committed Nov 18, 2024
1 parent 69b6bd7 commit 47c4c43
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED yes)
# Warnings
set (AVCPP_WARNING_OPTIONS
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall -Wextra>
-Wall -Wextra -Werror=return-type -Wwrite-strings>
$<$<CXX_COMPILER_ID:MSVC>:
/W4>)

Expand Down
2 changes: 2 additions & 0 deletions src/avutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ extern "C" {
#define API_AVFORMAT_URL ((LIBAVFORMAT_VERSION_MAJOR > 58) || (LIBAVFORMAT_VERSION_MAJOR == 58 && LIBAVFORMAT_VERSION_MINOR >= 7))
// net key frame flags: AV_FRAME_FLAG_KEY (FFmpeg 6.1)
#define API_FRAME_KEY ((LIBAVUTIL_VERSION_MAJOR > 58) || (LIBAVUTIL_VERSION_MAJOR == 58 && LIBAVUTIL_VERSION_MINOR >= 29))
// avcodec_close() support
#define API_AVCODEC_CLOSE (LIBAVCODEC_VERSION_MAJOR < 61)

#if defined(__ICL) || defined (__INTEL_COMPILER)
# define FF_DISABLE_DEPRECATION_WARNINGS __pragma(warning(push)) __pragma(warning(disable:1478))
Expand Down
61 changes: 60 additions & 1 deletion src/codeccontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,71 @@ void CodecContext2::open(Dictionary &options, const Codec &codec, OptionalErrorC
options.assign(prt);
}

namespace {
// Close code in new way: recreate context with parameters coping
// Use legacy avcodec_close() for old ffmpeg versions.
int codec_close(AVCodecContext*& ctx)
{
#if API_AVCODEC_CLOSE
return avcodec_close(ctx);
#else
AVCodecContext *ctxNew = nullptr;
AVCodecParameters *parTmp = nullptr;

int ret;

ctxNew = avcodec_alloc_context3(ctx->codec);
if (!ctxNew) {
ret = AVERROR(ENOMEM);
goto fail;
}

parTmp = avcodec_parameters_alloc();
if (!parTmp) {
ret = AVERROR(ENOMEM);
goto fail;
}

ret = avcodec_parameters_from_context(parTmp, ctx);
if (ret < 0)
goto fail;

ret = avcodec_parameters_to_context(ctxNew, parTmp);
if (ret < 0)
goto fail;

ctxNew->pkt_timebase = ctx->pkt_timebase;


#if (LIBAVCODEC_VERSION_MAJOR < 61) || (defined(FF_API_TICKS_PER_FRAME) && FF_API_TICKS_PER_FRAME)
FF_DISABLE_DEPRECATION_WARNINGS
ctxNew->ticks_per_frame = ctx->ticks_per_frame;
FF_ENABLE_DEPRECATION_WARNINGS
#endif

avcodec_free_context(&ctx);
ctx = ctxNew;

ctxNew = nullptr;
ret = 0;

fail:
avcodec_free_context(&ctxNew);
avcodec_parameters_free(&parTmp);

return ret;
#endif
}
}

void CodecContext2::close(OptionalErrorCode ec)
{
clear_if(ec);
if (isOpened())
{
avcodec_close(m_raw);
if (auto sts = codec_close(m_raw); sts) {
throws_if(ec, sts, ffmpeg_category());
}
return;
}
throws_if(ec, Errors::CodecNotOpened);
Expand Down

0 comments on commit 47c4c43

Please sign in to comment.