From ecee80c4ef4be669d5b5d8343c858d5ebd1f673c Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 7 Apr 2022 13:12:34 -0400 Subject: [PATCH] Implement last_dts[] because next_dts[] isn't actually useful. This fixes another ffmpeg deprecation problem by keep our own copy of cur_dts --- src/zm_videostore.cpp | 26 +++++++++++++++++--------- src/zm_videostore.h | 2 ++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/zm_videostore.cpp b/src/zm_videostore.cpp index b94de982a..32c84d2cd 100644 --- a/src/zm_videostore.cpp +++ b/src/zm_videostore.cpp @@ -349,6 +349,8 @@ bool VideoStore::open() { #else avcodec_copy_context(video_out_stream->codec, video_out_ctx); #endif + last_dts[video_out_stream->index] = AV_NOPTS_VALUE; + video_out_stream->time_base = video_in_stream ? video_in_stream->time_base : AV_TIME_BASE_Q; if (monitor->GetOptVideoWriter() == Monitor::PASSTHROUGH) { // Only set orientation if doing passthrough, otherwise the frame image will be rotated @@ -470,6 +472,7 @@ bool VideoStore::open() { // We will assume that subsequent stream allocations will increase the index max_stream_index = audio_out_stream->index; + last_dts[audio_out_stream->index] = AV_NOPTS_VALUE; } // end if audio_in_stream //max_stream_index is 0-based, so add 1 @@ -1313,15 +1316,19 @@ int VideoStore::write_packet(AVPacket *pkt, AVStream *stream) { pkt->stream_index = stream->index; if (pkt->dts == AV_NOPTS_VALUE) { - Error("undefined dts"); -// Debug(1, "undef dts, fixing by setting to stream cur_dts %" PRId64, stream->cur_dts); -// pkt->dts = stream->cur_dts; - } else if (pkt->dts < next_dts[stream->index]) { - Debug(1, "non increasing dts, fixing. our dts %" PRId64 " stream next_dts %" PRId64, pkt->dts, next_dts[stream->index]); - pkt->dts = next_dts[stream->index]; + if (last_dts[stream->index] == AV_NOPTS_VALUE) { + last_dts[stream->index] = 0; + } + pkt->dts = last_dts[stream->index]; + } else if (pkt->dts < last_dts[stream->index]) { + Warning("non increasing dts, fixing. our dts %" PRId64 " stream %d next_dts %" PRId64, + pkt->dts, stream->index, next_dts[stream->index]); + pkt->dts = last_dts[stream->index]; } - if (pkt->dts > pkt->pts) { + if (pkt->pts == AV_NOPTS_VALUE) { + pkt->pts = pkt->dts; + } else if (pkt->dts > pkt->pts) { Warning("pkt.dts(%" PRId64 ") must be <= pkt.pts(%" PRId64 ")." "Decompression must happen before presentation.", pkt->dts, pkt->pts); @@ -1334,8 +1341,9 @@ int VideoStore::write_packet(AVPacket *pkt, AVStream *stream) { ZM_DUMP_STREAM_PACKET(stream, (*pkt), "finished pkt"); next_dts[stream->index] = pkt->dts + pkt->duration; - Debug(3, "next_dts for stream %d has become %" PRId64, - stream->index, next_dts[stream->index]); + last_dts[stream->index] = pkt->dts; + Debug(3, "next_dts for stream %d has become %" PRId64 " last_dts %" PRId64, + stream->index, next_dts[stream->index], last_dts[stream->index]); int ret = av_interleaved_write_frame(oc, pkt); if (ret != 0) { diff --git a/src/zm_videostore.h b/src/zm_videostore.h index b67d1d0f4..a73018293 100644 --- a/src/zm_videostore.h +++ b/src/zm_videostore.h @@ -7,6 +7,7 @@ #include "zm_swscale.h" #include +#include extern "C" { #ifdef HAVE_LIBSWRESAMPLE @@ -100,6 +101,7 @@ class VideoStore { // These are for out, should start at zero. We assume they do not wrap because we just aren't going to save files that big. int64_t *next_dts; + std::map last_dts; int64_t audio_next_pts; int max_stream_index;