From 8a5a75b5870e606771044db2dd3de979b39e9db2 Mon Sep 17 00:00:00 2001 From: Jash Date: Sun, 8 Nov 2026 20:23:58 +0530 Subject: [PATCH 1/2] fix: drop input codec tags the output muxer rejects when copying video fixes #5491 --- src/zm_ffmpeg.cpp | 8 +++++++ src/zm_ffmpeg.h | 1 + src/zm_videostore.cpp | 2 ++ tests/CMakeLists.txt | 1 + tests/zm_ffmpeg_codec_tag.cpp | 39 +++++++++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+) create mode 100644 tests/zm_ffmpeg_codec_tag.cpp diff --git a/src/zm_ffmpeg.cpp b/src/zm_ffmpeg.cpp index 6453260dc4..30d51b113d 100644 --- a/src/zm_ffmpeg.cpp +++ b/src/zm_ffmpeg.cpp @@ -684,6 +684,14 @@ void zm_dump_codecpar(const AVCodecParameters *par) { ); } +void zm_clear_incompatible_codec_tag(const AVOutputFormat *format, AVCodecParameters *par) { + unsigned int tag; + if (format->codec_tag + and av_codec_get_id(format->codec_tag, par->codec_tag) != par->codec_id + and av_codec_get_tag2(format->codec_tag, par->codec_id, &tag)) + par->codec_tag = 0; +} + void zm_dump_codec(const AVCodecContext *codec) { Debug(1, "Dumping codec_context codec_type %d %s codec_id %d %s tag %c%c%c%c width %d height %d timebase %d/%d format %s profile %d level %d " "gop_size %d has_b_frames %d max_b_frames %d me_cmp %d me_range %d qmin %d qmax %d bit_rate %" PRId64 " qcompress %f extradata:%d:%s", diff --git a/src/zm_ffmpeg.h b/src/zm_ffmpeg.h index b13fa57b04..946283d872 100644 --- a/src/zm_ffmpeg.h +++ b/src/zm_ffmpeg.h @@ -150,6 +150,7 @@ void zm_dump_stream(AVStream *st); void zm_dump_stream_format(AVFormatContext *ic, int i, int index, int is_output); void zm_dump_codec(const AVCodecContext *codec); void zm_dump_codecpar(const AVCodecParameters *par); +void zm_clear_incompatible_codec_tag(const AVOutputFormat *format, AVCodecParameters *par); #if LIBAVUTIL_VERSION_CHECK(57, 28, 100, 28, 0) #define zm_dump_frame(frame, text) Debug(1, "%s: format %d %s sample_rate %" PRIu32 " nb_samples %d" \ diff --git a/src/zm_videostore.cpp b/src/zm_videostore.cpp index ad736b6bc3..9ea40a669d 100644 --- a/src/zm_videostore.cpp +++ b/src/zm_videostore.cpp @@ -174,6 +174,7 @@ bool VideoStore::open() { return false; } avcodec_parameters_copy(video_out_stream->codecpar, video_in_stream->codecpar); + zm_clear_incompatible_codec_tag(oc->oformat, video_out_stream->codecpar); zm_dump_codecpar(video_out_stream->codecpar); video_out_stream->avg_frame_rate = video_in_stream->avg_frame_rate; @@ -504,6 +505,7 @@ bool VideoStore::open() { return false; } avcodec_parameters_copy(video_out_stream->codecpar, video_in_stream->codecpar); + zm_clear_incompatible_codec_tag(oc->oformat, video_out_stream->codecpar); video_out_stream->avg_frame_rate = video_in_stream->avg_frame_rate; zm_dump_codecpar(video_out_stream->codecpar); } else { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index aad54f2736..825a3e5888 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -76,6 +76,7 @@ set(TEST_SOURCES zm_logger_rotate.cpp zm_logger_reload.cpp zm_ffmpeg_camera.cpp + zm_ffmpeg_codec_tag.cpp zm_ffmpeg_fallback.cpp) add_executable(tests ${TEST_SOURCES}) diff --git a/tests/zm_ffmpeg_codec_tag.cpp b/tests/zm_ffmpeg_codec_tag.cpp new file mode 100644 index 0000000000..b4a53293c4 --- /dev/null +++ b/tests/zm_ffmpeg_codec_tag.cpp @@ -0,0 +1,39 @@ +/* + * This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "zm_catch2.h" + +#include "zm_ffmpeg.h" + +static unsigned int codec_tag_for(const char *format_name, unsigned int tag) { + AVCodecParameters *par = avcodec_parameters_alloc(); + par->codec_type = AVMEDIA_TYPE_VIDEO; + par->codec_id = AV_CODEC_ID_H264; + par->codec_tag = tag; + zm_clear_incompatible_codec_tag(av_guess_format(format_name, nullptr, nullptr), par); + tag = par->codec_tag; + avcodec_parameters_free(&par); + return tag; +} + +TEST_CASE("zm_clear_incompatible_codec_tag", "[ffmpeg]") { + // 0x1b is the mpegts stream type for H.264 + REQUIRE(codec_tag_for("mp4", 0x1b) == 0); + REQUIRE(codec_tag_for("mp4", MKTAG('a', 'v', 'c', '1')) == MKTAG('a', 'v', 'c', '1')); + REQUIRE(codec_tag_for("mp4", MKTAG('h', 'e', 'v', '1')) == 0); + REQUIRE(codec_tag_for("mpegts", 0x1b) == 0x1b); +} From 1b815b13c53fef46483e15e1cd3f781c40bd3d54 Mon Sep 17 00:00:00 2001 From: Jash Date: Wed, 11 Nov 2026 09:23:25 +0530 Subject: [PATCH 2/2] test: initialize ffmpeg before looking up muxers in the codec tag test refs #5491 --- tests/zm_ffmpeg_codec_tag.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/zm_ffmpeg_codec_tag.cpp b/tests/zm_ffmpeg_codec_tag.cpp index b4a53293c4..6276a97a72 100644 --- a/tests/zm_ffmpeg_codec_tag.cpp +++ b/tests/zm_ffmpeg_codec_tag.cpp @@ -24,13 +24,16 @@ static unsigned int codec_tag_for(const char *format_name, unsigned int tag) { par->codec_type = AVMEDIA_TYPE_VIDEO; par->codec_id = AV_CODEC_ID_H264; par->codec_tag = tag; - zm_clear_incompatible_codec_tag(av_guess_format(format_name, nullptr, nullptr), par); + const AVOutputFormat *format = av_guess_format(format_name, nullptr, nullptr); + REQUIRE(format != nullptr); + zm_clear_incompatible_codec_tag(format, par); tag = par->codec_tag; avcodec_parameters_free(&par); return tag; } TEST_CASE("zm_clear_incompatible_codec_tag", "[ffmpeg]") { + FFMPEGInit(); // 0x1b is the mpegts stream type for H.264 REQUIRE(codec_tag_for("mp4", 0x1b) == 0); REQUIRE(codec_tag_for("mp4", MKTAG('a', 'v', 'c', '1')) == MKTAG('a', 'v', 'c', '1'));