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 20da0ee50d..0773039073 100644 --- a/src/zm_videostore.cpp +++ b/src/zm_videostore.cpp @@ -175,6 +175,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; @@ -505,6 +506,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 0a94f5652f..1a6e4694a9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -78,6 +78,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..6276a97a72 --- /dev/null +++ b/tests/zm_ffmpeg_codec_tag.cpp @@ -0,0 +1,42 @@ +/* + * 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; + 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')); + REQUIRE(codec_tag_for("mp4", MKTAG('h', 'e', 'v', '1')) == 0); + REQUIRE(codec_tag_for("mpegts", 0x1b) == 0x1b); +}