From 2e257bd823cb8b7c9398ed75ec96f9c944cd657d Mon Sep 17 00:00:00 2001 From: Jakub Karolczyk Date: Wed, 8 May 2024 10:53:58 +0100 Subject: [PATCH 1/4] [mod_av] Add support for FFmpeg 6.0 --- src/mod/applications/mod_av/avformat.c | 11 +++++++++++ src/mod/applications/mod_av/mod_av.h | 1 + 2 files changed, 12 insertions(+) diff --git a/src/mod/applications/mod_av/avformat.c b/src/mod/applications/mod_av/avformat.c index c1e00525300..b5b844ef8aa 100644 --- a/src/mod/applications/mod_av/avformat.c +++ b/src/mod/applications/mod_av/avformat.c @@ -416,6 +416,7 @@ static int interrupt_cb(void *cp) } +#if (LIBAVFORMAT_VERSION_MAJOR < LIBAVFORMAT_6_V) static int mod_avformat_alloc_output_context2(AVFormatContext **avctx, const char *format, const char *filename, av_file_context_t *context) { AVFormatContext *s = avformat_alloc_context(); @@ -489,6 +490,7 @@ static int mod_avformat_alloc_output_context2(AVFormatContext **avctx, const cha return ret; } +#endif static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt) { @@ -2235,7 +2237,16 @@ static switch_status_t av_file_open(switch_file_handle_t *handle, const char *pa return SWITCH_STATUS_SUCCESS; } +#if (LIBAVFORMAT_VERSION_MAJOR < LIBAVFORMAT_6_V) mod_avformat_alloc_output_context2(&context->fc, format, (char *)file, context); +#else + avformat_alloc_output_context2(&context->fc, NULL, format, (char *)file); + + if (context->fc) { + context->fc->interrupt_callback.callback = interrupt_cb; + context->fc->interrupt_callback.opaque = context; + } +#endif if (!context->fc) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Could not deduce output format from file extension\n"); diff --git a/src/mod/applications/mod_av/mod_av.h b/src/mod/applications/mod_av/mod_av.h index a89e6cb8f72..074a2224825 100644 --- a/src/mod/applications/mod_av/mod_av.h +++ b/src/mod/applications/mod_av/mod_av.h @@ -42,6 +42,7 @@ #define LIBAVCODEC_V 59 #define LIBAVFORMAT_V 59 +#define LIBAVFORMAT_6_V 60 #define LIBAVUTIL_V 57 struct mod_av_globals { From 3de59301368f16c93f66eb0df436cb1efd64424a Mon Sep 17 00:00:00 2001 From: Jakub Karolczyk Date: Wed, 8 May 2024 12:27:08 +0100 Subject: [PATCH 2/4] [mod_av] Add support for FFmpeg 6.1 --- src/mod/applications/mod_av/avcodec.c | 11 ++++++++++- src/mod/applications/mod_av/avformat.c | 13 ++++++++++++- src/mod/applications/mod_av/mod_av.h | 11 +++++++---- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/mod/applications/mod_av/avcodec.c b/src/mod/applications/mod_av/avcodec.c index 0293b834467..97bf55b9559 100644 --- a/src/mod/applications/mod_av/avcodec.c +++ b/src/mod/applications/mod_av/avcodec.c @@ -1557,7 +1557,11 @@ static switch_status_t switch_h264_encode(switch_codec_t *codec, switch_frame_t } avframe->pict_type = AV_PICTURE_TYPE_I; +#if (LIBAVCODEC_VERSION_MAJOR >= LIBAVCODEC_6_V && LIBAVCODEC_VERSION_MINOR >= LIBAVCODEC_61_V) + avframe->flags |= AV_FRAME_FLAG_KEY; +#else avframe->key_frame = 1; +#endif context->last_keyframe_request = switch_time_now(); } @@ -1600,9 +1604,14 @@ GCC_DIAG_ON(deprecated-declarations) } #endif +#if (LIBAVCODEC_VERSION_MAJOR >= LIBAVCODEC_6_V && LIBAVCODEC_VERSION_MINOR >= LIBAVCODEC_61_V) + if (context->need_key_frame && (avframe->flags & AV_FRAME_FLAG_KEY)) { + avframe->flags &= ~AV_FRAME_FLAG_KEY; +#else if (context->need_key_frame && avframe->key_frame == 1) { - avframe->pict_type = 0; avframe->key_frame = 0; +#endif + avframe->pict_type = 0; context->need_key_frame = 0; } diff --git a/src/mod/applications/mod_av/avformat.c b/src/mod/applications/mod_av/avformat.c index b5b844ef8aa..ac90e87fc5f 100644 --- a/src/mod/applications/mod_av/avformat.c +++ b/src/mod/applications/mod_av/avformat.c @@ -623,8 +623,13 @@ static switch_status_t add_stream(av_file_context_t *context, MediaStream *mst, c->rc_initial_buffer_occupancy = buffer_bytes * 8; if (codec_id == AV_CODEC_ID_H264) { +#if (LIBAVFORMAT_VERSION_MAJOR >= LIBAVFORMAT_6_V && LIBAVFORMAT_VERSION_MINOR >= LIBAVFORMAT_61_V) +GCC_DIAG_OFF(deprecated-declarations) +#endif c->ticks_per_frame = 2; - +#if (LIBAVFORMAT_VERSION_MAJOR >= LIBAVFORMAT_6_V && LIBAVFORMAT_VERSION_MINOR >= LIBAVFORMAT_61_V) +GCC_DIAG_ON(deprecated-declarations) +#endif c->flags|=AV_CODEC_FLAG_LOOP_FILTER; // flags=+loop c->me_cmp|= 1; // cmp=+chroma, where CHROMA = 1 @@ -3212,6 +3217,9 @@ static switch_status_t av_file_read_video(switch_file_handle_t *handle, switch_f if ((c = av_get_codec_context(mst)) && c->time_base.num) { cp = av_stream_get_parser(st); +#if (LIBAVFORMAT_VERSION_MAJOR >= LIBAVFORMAT_6_V && LIBAVFORMAT_VERSION_MINOR >= LIBAVFORMAT_61_V) +GCC_DIAG_OFF(deprecated-declarations) +#endif ticks = cp ? cp->repeat_pict + 1 : c->ticks_per_frame; // mst->next_pts += ((int64_t)AV_TIME_BASE * st->codec->time_base.num * ticks) / st->codec->time_base.den; } @@ -3221,6 +3229,9 @@ static switch_status_t av_file_read_video(switch_file_handle_t *handle, switch_f context->video_start_time, ticks, c ? c->ticks_per_frame : -1, st->time_base.num, st->time_base.den, c ? c->time_base.num : -1, c ? c->time_base.den : -1, st->start_time, st->duration == AV_NOPTS_VALUE ? context->fc->duration / AV_TIME_BASE * 1000 : st->duration, st->nb_frames, av_q2d(st->time_base)); } +#if (LIBAVFORMAT_VERSION_MAJOR >= LIBAVFORMAT_6_V && LIBAVFORMAT_VERSION_MINOR >= LIBAVFORMAT_61_V) +GCC_DIAG_ON(deprecated-declarations) +#endif again: diff --git a/src/mod/applications/mod_av/mod_av.h b/src/mod/applications/mod_av/mod_av.h index 074a2224825..f73e3eee09a 100644 --- a/src/mod/applications/mod_av/mod_av.h +++ b/src/mod/applications/mod_av/mod_av.h @@ -40,10 +40,13 @@ #ifndef MOD_AV_H #define MOD_AV_H -#define LIBAVCODEC_V 59 -#define LIBAVFORMAT_V 59 -#define LIBAVFORMAT_6_V 60 -#define LIBAVUTIL_V 57 +#define LIBAVCODEC_V 59 /* FFmpeg version >= 5.1 */ +#define LIBAVCODEC_6_V 60 /* FFmpeg version >= 6.0 */ +#define LIBAVCODEC_61_V 31 /* FFmpeg version >= 6.1 */ +#define LIBAVFORMAT_V 59 /* FFmpeg version >= 5.1 */ +#define LIBAVFORMAT_6_V 60 /* FFmpeg version >= 6.0 */ +#define LIBAVFORMAT_61_V 16 /* FFmpeg version >= 6.1 */ +#define LIBAVUTIL_V 57 /* FFmpeg version >= 5.1 */ struct mod_av_globals { int debug; From 55707a7501d935852494cf19fc58d800ed870b8a Mon Sep 17 00:00:00 2001 From: Jakub Karolczyk Date: Thu, 9 May 2024 11:45:38 +0100 Subject: [PATCH 3/4] [mod_av] Add support for FFmpeg 7.0 --- src/mod/applications/mod_av/avcodec.c | 36 +++++++++++++++++++++++--- src/mod/applications/mod_av/avformat.c | 17 +++++++++--- src/mod/applications/mod_av/mod_av.h | 2 ++ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/mod/applications/mod_av/avcodec.c b/src/mod/applications/mod_av/avcodec.c index 97bf55b9559..a9d449b562c 100644 --- a/src/mod/applications/mod_av/avcodec.c +++ b/src/mod/applications/mod_av/avcodec.c @@ -1227,8 +1227,14 @@ static switch_status_t open_encoder(h264_codec_context_t *context, uint32_t widt if (context->encoder_ctx) { if (avcodec_is_open(context->encoder_ctx)) { +#if (LIBAVCODEC_VERSION_MAJOR < LIBAVCODEC_7_V) avcodec_close(context->encoder_ctx); +#else + /* avcodec_close() will be called in avcodec_free_context() */ + avcodec_free_context(&context->encoder_ctx); +#endif } + av_free(context->encoder_ctx); context->encoder_ctx = NULL; } @@ -1320,8 +1326,14 @@ FF_ENABLE_DEPRECATION_WARNINGS if (context->encoder_ctx) { if (avcodec_is_open(context->encoder_ctx)) { +#if (LIBAVCODEC_VERSION_MAJOR < LIBAVCODEC_7_V) avcodec_close(context->encoder_ctx); +#else + /* avcodec_close() will be called in avcodec_free_context() */ + avcodec_free_context(&context->encoder_ctx); +#endif } + av_free(context->encoder_ctx); context->encoder_ctx = NULL; } @@ -1557,7 +1569,7 @@ static switch_status_t switch_h264_encode(switch_codec_t *codec, switch_frame_t } avframe->pict_type = AV_PICTURE_TYPE_I; -#if (LIBAVCODEC_VERSION_MAJOR >= LIBAVCODEC_6_V && LIBAVCODEC_VERSION_MINOR >= LIBAVCODEC_61_V) +#if ((LIBAVCODEC_VERSION_MAJOR == LIBAVCODEC_6_V && LIBAVCODEC_VERSION_MINOR >= LIBAVCODEC_61_V) || LIBAVCODEC_VERSION_MAJOR >= LIBAVCODEC_7_V) avframe->flags |= AV_FRAME_FLAG_KEY; #else avframe->key_frame = 1; @@ -1604,7 +1616,7 @@ GCC_DIAG_ON(deprecated-declarations) } #endif -#if (LIBAVCODEC_VERSION_MAJOR >= LIBAVCODEC_6_V && LIBAVCODEC_VERSION_MINOR >= LIBAVCODEC_61_V) +#if ((LIBAVCODEC_VERSION_MAJOR == LIBAVCODEC_6_V && LIBAVCODEC_VERSION_MINOR >= LIBAVCODEC_61_V) || LIBAVCODEC_VERSION_MAJOR >= LIBAVCODEC_7_V) if (context->need_key_frame && (avframe->flags & AV_FRAME_FLAG_KEY)) { avframe->flags &= ~AV_FRAME_FLAG_KEY; #else @@ -1871,14 +1883,30 @@ static switch_status_t switch_h264_destroy(switch_codec_t *codec) switch_buffer_destroy(&context->nalu_buffer); if (context->decoder_ctx) { - if (avcodec_is_open(context->decoder_ctx)) avcodec_close(context->decoder_ctx); + if (avcodec_is_open(context->decoder_ctx)) { +#if (LIBAVCODEC_VERSION_MAJOR < LIBAVCODEC_7_V) + avcodec_close(context->decoder_ctx); +#else + /* avcodec_close() will be called in avcodec_free_context() */ + avcodec_free_context(&context->decoder_ctx); +#endif + } + av_free(context->decoder_ctx); } switch_img_free(&context->img); if (context->encoder_ctx) { - if (avcodec_is_open(context->encoder_ctx)) avcodec_close(context->encoder_ctx); + if (avcodec_is_open(context->encoder_ctx)) { +#if (LIBAVCODEC_VERSION_MAJOR < LIBAVCODEC_7_V) + avcodec_close(context->encoder_ctx); +#else + /* avcodec_close() will be called in avcodec_free_context() */ + avcodec_free_context(&context->encoder_ctx); +#endif + } + av_free(context->encoder_ctx); } diff --git a/src/mod/applications/mod_av/avformat.c b/src/mod/applications/mod_av/avformat.c index ac90e87fc5f..708291ad521 100644 --- a/src/mod/applications/mod_av/avformat.c +++ b/src/mod/applications/mod_av/avformat.c @@ -623,11 +623,11 @@ static switch_status_t add_stream(av_file_context_t *context, MediaStream *mst, c->rc_initial_buffer_occupancy = buffer_bytes * 8; if (codec_id == AV_CODEC_ID_H264) { -#if (LIBAVFORMAT_VERSION_MAJOR >= LIBAVFORMAT_6_V && LIBAVFORMAT_VERSION_MINOR >= LIBAVFORMAT_61_V) +#if ((LIBAVFORMAT_VERSION_MAJOR == LIBAVFORMAT_6_V && LIBAVFORMAT_VERSION_MINOR >= LIBAVFORMAT_61_V) || LIBAVFORMAT_VERSION_MAJOR == LIBAVFORMAT_7_V) GCC_DIAG_OFF(deprecated-declarations) #endif c->ticks_per_frame = 2; -#if (LIBAVFORMAT_VERSION_MAJOR >= LIBAVFORMAT_6_V && LIBAVFORMAT_VERSION_MINOR >= LIBAVFORMAT_61_V) +#if ((LIBAVFORMAT_VERSION_MAJOR == LIBAVFORMAT_6_V && LIBAVFORMAT_VERSION_MINOR >= LIBAVFORMAT_61_V) || LIBAVFORMAT_VERSION_MAJOR == LIBAVFORMAT_7_V) GCC_DIAG_ON(deprecated-declarations) #endif @@ -1417,7 +1417,11 @@ static switch_status_t open_input_file(av_file_context_t *context, switch_file_h switch_goto_status(SWITCH_STATUS_FALSE, err); } +#if (LIBAVFORMAT_VERSION_MAJOR == LIBAVFORMAT_7_V) + handle->seekable = !(context->fc->iformat->flags & AVFMT_NOTIMESTAMPS); +#else handle->seekable = context->fc->iformat->read_seek2 ? 1 : (context->fc->iformat->read_seek ? 1 : 0); +#endif switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "file %s is %sseekable\n", filename, handle->seekable ? "" : "not "); /** Get information on the input file (number of streams etc.). */ @@ -1509,7 +1513,12 @@ static switch_status_t open_input_file(av_file_context_t *context, switch_file_h switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not open input audio codec channel 2 (error '%s')\n", get_error_text(error, ebuf, sizeof(ebuf))); if ((cc = av_get_codec_context(&context->audio_st[0]))) { +#if (LIBAVCODEC_VERSION_MAJOR < LIBAVCODEC_7_V) avcodec_close(cc); +#else + /* avcodec_close() will be called in avcodec_free_context() */ + avcodec_free_context(&cc); +#endif } context->has_audio = 0; @@ -3217,7 +3226,7 @@ static switch_status_t av_file_read_video(switch_file_handle_t *handle, switch_f if ((c = av_get_codec_context(mst)) && c->time_base.num) { cp = av_stream_get_parser(st); -#if (LIBAVFORMAT_VERSION_MAJOR >= LIBAVFORMAT_6_V && LIBAVFORMAT_VERSION_MINOR >= LIBAVFORMAT_61_V) +#if ((LIBAVFORMAT_VERSION_MAJOR == LIBAVFORMAT_6_V && LIBAVFORMAT_VERSION_MINOR >= LIBAVFORMAT_61_V) || LIBAVFORMAT_VERSION_MAJOR == LIBAVFORMAT_7_V) GCC_DIAG_OFF(deprecated-declarations) #endif ticks = cp ? cp->repeat_pict + 1 : c->ticks_per_frame; @@ -3229,7 +3238,7 @@ GCC_DIAG_OFF(deprecated-declarations) context->video_start_time, ticks, c ? c->ticks_per_frame : -1, st->time_base.num, st->time_base.den, c ? c->time_base.num : -1, c ? c->time_base.den : -1, st->start_time, st->duration == AV_NOPTS_VALUE ? context->fc->duration / AV_TIME_BASE * 1000 : st->duration, st->nb_frames, av_q2d(st->time_base)); } -#if (LIBAVFORMAT_VERSION_MAJOR >= LIBAVFORMAT_6_V && LIBAVFORMAT_VERSION_MINOR >= LIBAVFORMAT_61_V) +#if ((LIBAVFORMAT_VERSION_MAJOR == LIBAVFORMAT_6_V && LIBAVFORMAT_VERSION_MINOR >= LIBAVFORMAT_61_V) || LIBAVFORMAT_VERSION_MAJOR == LIBAVFORMAT_7_V) GCC_DIAG_ON(deprecated-declarations) #endif diff --git a/src/mod/applications/mod_av/mod_av.h b/src/mod/applications/mod_av/mod_av.h index f73e3eee09a..8152a77a815 100644 --- a/src/mod/applications/mod_av/mod_av.h +++ b/src/mod/applications/mod_av/mod_av.h @@ -42,9 +42,11 @@ #define LIBAVCODEC_V 59 /* FFmpeg version >= 5.1 */ #define LIBAVCODEC_6_V 60 /* FFmpeg version >= 6.0 */ +#define LIBAVCODEC_7_V 61 /* FFmpeg version >= 7.0 */ #define LIBAVCODEC_61_V 31 /* FFmpeg version >= 6.1 */ #define LIBAVFORMAT_V 59 /* FFmpeg version >= 5.1 */ #define LIBAVFORMAT_6_V 60 /* FFmpeg version >= 6.0 */ +#define LIBAVFORMAT_7_V 61 /* FFmpeg version >= 7.0 */ #define LIBAVFORMAT_61_V 16 /* FFmpeg version >= 6.1 */ #define LIBAVUTIL_V 57 /* FFmpeg version >= 5.1 */ From d169aae7961481272e659d5ddd8213c5ccc4a3ae Mon Sep 17 00:00:00 2001 From: Andrey Volk Date: Fri, 11 Jul 2025 13:39:48 +0300 Subject: [PATCH 4/4] [mod_av] Add support for FFmpeg 7.1 --- src/mod/applications/mod_av/avformat.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/mod/applications/mod_av/avformat.c b/src/mod/applications/mod_av/avformat.c index 708291ad521..d617a52dfe2 100644 --- a/src/mod/applications/mod_av/avformat.c +++ b/src/mod/applications/mod_av/avformat.c @@ -557,7 +557,21 @@ static switch_status_t add_stream(av_file_context_t *context, MediaStream *mst, switch ((*codec)->type) { case AVMEDIA_TYPE_AUDIO: +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100) + /* + Changelog says 61.12.100 but commit changes version actually to 61.13.100 + https://github.com/FFmpeg/FFmpeg/commit/3305767560a6303f474fffa3afb10c500059b455 + */ + { + const enum AVSampleFormat *sample_fmts = NULL; + int fmts_count = 0; + int ret = avcodec_get_supported_config(c, *codec, AV_CODEC_CONFIG_SAMPLE_FORMAT, 0, (const void**)&sample_fmts, &fmts_count); + + c->sample_fmt = (ret >= 0 && fmts_count && sample_fmts) ? sample_fmts[0] : AV_SAMPLE_FMT_FLTP; + } +#else c->sample_fmt = (*codec)->sample_fmts ? (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP; +#endif c->bit_rate = 128000; c->sample_rate = mst->sample_rate = context->handle->samplerate; #if (LIBAVCODEC_VERSION_MAJOR < LIBAVCODEC_V)