From 938676b1299abd92430d5e679eb231862982babe Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sat, 6 Feb 2021 14:46:28 +0100 Subject: [PATCH] ffmpeg: exit dumpPacket early if debug logging is not enabled --- src/zm_event.cpp | 2 +- src/zm_ffmpeg.cpp | 54 ------------------------ src/zm_ffmpeg.h | 61 +++++++++++++++++++++++----- src/zm_ffmpeg_camera.cpp | 2 +- src/zm_ffmpeg_input.cpp | 2 +- src/zm_packet.cpp | 2 +- src/zm_packetqueue.cpp | 13 +++--- src/zm_rtsp_server_device_source.cpp | 2 +- src/zm_videostore.cpp | 16 ++++---- 9 files changed, 69 insertions(+), 85 deletions(-) diff --git a/src/zm_event.cpp b/src/zm_event.cpp index b6ad57b8f..3dc169987 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -553,7 +553,7 @@ void Event::AddPacket(ZMPacket *packet) { have_video_keyframe = have_video_keyframe || ( ( packet->codec_type == AVMEDIA_TYPE_VIDEO ) && packet->keyframe ); Debug(2, "have_video_keyframe %d codec_type %d == video? %d packet keyframe %d", have_video_keyframe, packet->codec_type, (packet->codec_type == AVMEDIA_TYPE_VIDEO), packet->keyframe); - dumpPacket(&packet->packet, "Adding to event"); + ZM_DUMP_PACKET(packet->packet, "Adding to event"); if ( videoStore ) { if ( have_video_keyframe ) { videoStore->writePacket(packet); diff --git a/src/zm_ffmpeg.cpp b/src/zm_ffmpeg.cpp index bd47a7fc3..4871be77c 100644 --- a/src/zm_ffmpeg.cpp +++ b/src/zm_ffmpeg.cpp @@ -649,36 +649,6 @@ int zm_send_frame_receive_packet(AVCodecContext *ctx, AVFrame *frame, AVPacket & return 1; } // end int zm_send_frame_receive_packet -void dumpPacket(AVStream *stream, AVPacket *pkt, const char *text) { - char b[10240]; - - double pts_time = (double)av_rescale_q(pkt->pts, - stream->time_base, - AV_TIME_BASE_Q - ) / AV_TIME_BASE; - - snprintf(b, sizeof(b), - " pts: %" PRId64 "=%f, dts: %" PRId64 - ", size: %d, stream_index: %d, flags: %04x, keyframe(%d) pos: %" PRId64 - ", duration: %" -#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) - PRIu64 -#else - "d" -#endif - "\n", - pkt->pts, - pts_time, - pkt->dts, - pkt->size, - pkt->stream_index, - pkt->flags, - pkt->flags & AV_PKT_FLAG_KEY, - pkt->pos, - pkt->duration); - Debug(2, "%s:%d:%s: %s", __FILE__, __LINE__, text, b); -} - void zm_free_codec( AVCodecContext **ctx ) { if ( *ctx ) { avcodec_close(*ctx); @@ -690,30 +660,6 @@ void zm_free_codec( AVCodecContext **ctx ) { } // end if } -void dumpPacket(AVPacket *pkt, const char *text) { - char b[10240]; - - snprintf(b, sizeof(b), - " pts: %" PRId64 ", dts: %" PRId64 - ", size: %d, stream_index: %d, flags: %04x, keyframe(%d) pos: %" PRId64 - ", duration: %" -#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) - PRId64 -#else - "d" -#endif - "\n", - pkt->pts, - pkt->dts, - pkt->size, - pkt->stream_index, - pkt->flags, - pkt->flags & AV_PKT_FLAG_KEY, - pkt->pos, - pkt->duration); - Debug(2, "%s:%d:%s: %s", __FILE__, __LINE__, text, b); -} - void zm_packet_copy_rescale_ts(const AVPacket *ipkt, AVPacket *opkt, const AVRational src_tb, const AVRational dst_tb) { opkt->pts = ipkt->pts; opkt->dts = ipkt->dts; diff --git a/src/zm_ffmpeg.h b/src/zm_ffmpeg.h index aae8710da..f57396a6f 100644 --- a/src/zm_ffmpeg.h +++ b/src/zm_ffmpeg.h @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -*/ +*/ #ifndef ZM_FFMPEG_H #define ZM_FFMPEG_H @@ -60,7 +60,7 @@ extern "C" { #else #include #endif - + #if LIBAVUTIL_VERSION_CHECK(54, 6, 0, 6, 0) #include #endif @@ -70,8 +70,8 @@ extern "C" { #include #include #endif /* HAVE_LIBAVUTIL_AVUTIL_H */ - -#if defined(HAVE_LIBAVUTIL_AVUTIL_H) + +#if defined(HAVE_LIBAVUTIL_AVUTIL_H) #if LIBAVUTIL_VERSION_CHECK(51, 42, 0, 74, 100) #define _AVPIXELFORMAT AVPixelFormat #else @@ -211,7 +211,7 @@ extern "C" { #endif #endif -/* A single function to initialize ffmpeg, to avoid multiple initializations */ +/* A single function to initialize ffmpeg, to avoid multiple initializations */ void FFMPEGInit(); void FFMPEGDeInit(); @@ -350,6 +350,49 @@ void zm_dump_codecpar(const AVCodecParameters *par); ); #endif +#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) +# define AV_PACKET_DURATION_FMT PRId64 +#else +# define AV_PACKET_DURATION_FMT "d" +#endif + +#ifndef DBG_OFF +# define ZM_DUMP_PACKET(pkt, text) \ + Debug(2, "%s: pts: %" PRId64 ", dts: %" PRId64 \ + ", size: %d, stream_index: %d, flags: %04x, keyframe(%d) pos: %" PRId64 ", duration: %" AV_PACKET_DURATION_FMT, \ + text,\ + pkt.pts,\ + pkt.dts,\ + pkt.size,\ + pkt.stream_index,\ + pkt.flags,\ + pkt.flags & AV_PKT_FLAG_KEY,\ + pkt.pos,\ + pkt.duration) + +# define ZM_DUMP_STREAM_PACKET(stream, pkt, text) \ + if (logDebugging()) { \ + double pts_time = static_cast(av_rescale_q(pkt.pts, stream->time_base, AV_TIME_BASE_Q)) / AV_TIME_BASE; \ + \ + Debug(2, "%s: pts: %" PRId64 "=%f, dts: %" PRId64 \ + ", size: %d, stream_index: %d, flags: %04x, keyframe(%d) pos: %" PRId64", duration: %" AV_PACKET_DURATION_FMT, \ + text, \ + pkt.pts, \ + pts_time, \ + pkt.dts, \ + pkt.size, \ + pkt.stream_index, \ + pkt.flags, \ + pkt.flags & AV_PKT_FLAG_KEY, \ + pkt.pos, \ + pkt.duration) \ + } + +#else +# define ZM_DUMP_PACKET(pkt, text) +# define ZM_DUMP_STREAM_PACKET(stream, pkt, text) +#endif + #if LIBAVCODEC_VERSION_CHECK(56, 8, 0, 60, 100) #define zm_av_packet_unref(packet) av_packet_unref(packet) #define zm_av_packet_ref(dst, src) av_packet_ref(dst, src) @@ -373,7 +416,7 @@ void zm_dump_codecpar(const AVCodecParameters *par); avcodec_decode_video(context, rawFrame, frameComplete, packet->data, packet->size) #endif #endif - + #if LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101) #define zm_av_frame_alloc() av_frame_alloc() #else @@ -382,7 +425,7 @@ void zm_dump_codecpar(const AVCodecParameters *par); #if ! LIBAVCODEC_VERSION_CHECK(55, 28, 1, 45, 101) #define av_frame_free( input_avframe ) av_freep( input_avframe ) -#endif +#endif int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt); void fix_deprecated_pix_fmt(AVCodecContext *); @@ -397,8 +440,6 @@ int zm_receive_packet(AVCodecContext *context, AVPacket &packet); int zm_send_packet_receive_frame(AVCodecContext *context, AVFrame *frame, AVPacket &packet); int zm_send_frame_receive_packet(AVCodecContext *context, AVFrame *frame, AVPacket &packet); -void dumpPacket(AVStream *, AVPacket *,const char *text=""); -void dumpPacket(AVPacket *,const char *text=""); void zm_packet_copy_rescale_ts(const AVPacket *ipkt, AVPacket *opkt, const AVRational src_tb, const AVRational dst_tb); #if defined(HAVE_LIBSWRESAMPLE) || defined(HAVE_LIBAVRESAMPLE) @@ -429,6 +470,4 @@ int zm_resample_get_delay( int zm_add_samples_to_fifo(AVAudioFifo *fifo, AVFrame *frame); int zm_get_samples_from_fifo(AVAudioFifo *fifo, AVFrame *frame); - - #endif // ZM_FFMPEG_H diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index 4b2f6f6a6..ab7555426 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -204,7 +204,7 @@ int FfmpegCamera::Capture(ZMPacket &zm_packet) { } return -1; } - dumpPacket(mFormatContext->streams[packet.stream_index], &packet, "ffmpeg_camera in"); + ZM_DUMP_STREAM_PACKET(mFormatContext->streams[packet.stream_index], packet, "ffmpeg_camera in"); #if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) zm_packet.codec_type = mFormatContext->streams[packet.stream_index]->codecpar->codec_type; diff --git a/src/zm_ffmpeg_input.cpp b/src/zm_ffmpeg_input.cpp index ed2f703dc..2b3bcfd74 100644 --- a/src/zm_ffmpeg_input.cpp +++ b/src/zm_ffmpeg_input.cpp @@ -170,7 +170,7 @@ AVFrame *FFmpeg_Input::get_frame(int stream_id) { packet.stream_index, ret, av_make_error_string(ret).c_str()); return nullptr; } - dumpPacket(input_format_context->streams[packet.stream_index], &packet, "Received packet"); + ZM_DUMP_STREAM_PACKET(input_format_context->streams[packet.stream_index], packet, "Received packet"); if ( (stream_id >= 0) && (packet.stream_index != stream_id) ) { Debug(1,"Packet is not for our stream (%d)", packet.stream_index ); diff --git a/src/zm_packet.cpp b/src/zm_packet.cpp index d3abba13a..2126c295c 100644 --- a/src/zm_packet.cpp +++ b/src/zm_packet.cpp @@ -271,7 +271,7 @@ AVPacket *ZMPacket::set_packet(AVPacket *p) { if ( zm_av_packet_ref(&packet, p) < 0 ) { Error("error refing packet"); } - //dumpPacket(&packet, "zmpacket:"); + //ZM_DUMP_PACKET(packet, "zmpacket:"); gettimeofday(timestamp, nullptr); keyframe = p->flags & AV_PKT_FLAG_KEY; return &packet; diff --git a/src/zm_packetqueue.cpp b/src/zm_packetqueue.cpp index 0c9ca1b43..f019a8af8 100644 --- a/src/zm_packetqueue.cpp +++ b/src/zm_packetqueue.cpp @@ -492,14 +492,14 @@ packetqueue_iterator *PacketQueue::get_event_start_packet_it( iterators.push_back(it); *it = snapshot_it; - dumpPacket(&((*(*it))->packet)); + ZM_DUMP_PACKET((*(*it))->packet, ""); // Step one count back pre_event_count frames as the minimum // Do not assume that snapshot_it is video // snapshot it might already point to the beginning while ( ( *it != pktQueue.begin() ) and pre_event_count ) { Debug(1, "Previous packet pre_event_count %d stream_index %d keyframe %d", pre_event_count, (*(*it))->packet.stream_index, (*(*it))->keyframe); - dumpPacket(&((*(*it))->packet)); + ZM_DUMP_PACKET((*(*it))->packet, ""); if ( (*(*it))->packet.stream_index == video_stream_id ) { pre_event_count --; if ( ! pre_event_count ) @@ -520,19 +520,19 @@ packetqueue_iterator *PacketQueue::get_event_start_packet_it( } else { Warning("Hit end of packetqueue before satisfying pre_event_count. Needed %d more video frames", pre_event_count); } - dumpPacket(&((*(*it))->packet)); + ZM_DUMP_PACKET((*(*it))->packet, ""); } return it; } // Not at beginning, so must be pointing at a video keyframe or maybe pre_event_count == 0 if ( (*(*it))->keyframe ) { - dumpPacket(&((*(*it))->packet), "Found video keyframe, Returning"); + ZM_DUMP_PACKET((*(*it))->packet, "Found video keyframe, Returning"); return it; } while ( (*it)-- != pktQueue.begin() ) { - dumpPacket(&((*(*it))->packet), "No keyframe"); + ZM_DUMP_PACKET((*(*it))->packet, "No keyframe"); if ( (*(*it))->packet.stream_index == video_stream_id and (*(*it))->keyframe ) return it; // Success } @@ -546,8 +546,7 @@ void PacketQueue::dumpQueue() { std::list::reverse_iterator it; for ( it = pktQueue.rbegin(); it != pktQueue.rend(); ++ it ) { ZMPacket *zm_packet = *it; - AVPacket *av_packet = &(zm_packet->packet); - dumpPacket(av_packet); + ZM_DUMP_PACKET(zm_packet->packet, ""); } } diff --git a/src/zm_rtsp_server_device_source.cpp b/src/zm_rtsp_server_device_source.cpp index 0616f8410..00fccc683 100644 --- a/src/zm_rtsp_server_device_source.cpp +++ b/src/zm_rtsp_server_device_source.cpp @@ -159,7 +159,7 @@ int ZoneMinderDeviceSource::getNextFrame() { // Convert pts to timeval int64_t pts = av_rescale_q(pkt->dts, m_stream->time_base, AV_TIME_BASE_Q); timeval tv = { pts/1000000, pts%1000000 }; - dumpPacket(m_stream, pkt, "rtspServer"); + ZM_DUMP_STREAM_PACKET(m_stream, (*pkt), "rtspServer"); Debug(2, "pts %" PRId64 " pkt.pts %" PRId64 " tv %d.%d", pts, pkt->pts, tv.tv_sec, tv.tv_usec); std::list< std::pair > framesList = this->splitFrames(pkt->data, pkt->size); diff --git a/src/zm_videostore.cpp b/src/zm_videostore.cpp index 281c22b91..0f9813148 100644 --- a/src/zm_videostore.cpp +++ b/src/zm_videostore.cpp @@ -559,9 +559,9 @@ void VideoStore::flush_codecs() { break; } - dumpPacket(&pkt, "raw from encoder"); + ZM_DUMP_PACKET(pkt, "raw from encoder"); av_packet_rescale_ts(&pkt, audio_out_ctx->time_base, audio_out_stream->time_base); - dumpPacket(audio_out_stream, &pkt, "writing flushed packet"); + ZM_DUMP_STREAM_PACKET(audio_out_stream, pkt, "writing flushed packet"); write_packet(&pkt, audio_out_stream); zm_av_packet_unref(&pkt); } // while have buffered frames @@ -1039,7 +1039,7 @@ int VideoStore::writeVideoFramePacket(ZMPacket *zm_packet) { } return ret; } - dumpPacket(&opkt, "packet returned by codec"); + ZM_DUMP_PACKET(opkt, "packet returned by codec"); // Need to adjust pts/dts values from codec time to stream time if ( opkt.pts != AV_NOPTS_VALUE ) @@ -1119,7 +1119,7 @@ int VideoStore::writeVideoFramePacket(ZMPacket *zm_packet) { av_packet_rescale_ts(&opkt, video_in_stream->time_base, video_out_stream->time_base); - dumpPacket(video_out_stream, &opkt, "after pts adjustment"); + ZM_DUMP_STREAM_PACKET(video_out_stream, opkt, "after pts adjustment"); } // end if codec matches write_packet(&opkt, video_out_stream); @@ -1138,7 +1138,7 @@ int VideoStore::writeAudioFramePacket(ZMPacket *zm_packet) { return 0; // FIXME -ve return codes do not free packet in ffmpeg_camera at the moment } - dumpPacket(audio_in_stream, ipkt, "input packet"); + ZM_DUMP_STREAM_PACKET(audio_in_stream, (*ipkt), "input packet"); if ( !audio_first_dts ) { audio_first_dts = ipkt->dts; @@ -1148,7 +1148,7 @@ int VideoStore::writeAudioFramePacket(ZMPacket *zm_packet) { // Need to adjust pts before feeding to decoder.... should really copy the pkt instead of modifying it ipkt->pts -= audio_first_dts; ipkt->dts -= audio_first_dts; - dumpPacket(audio_in_stream, ipkt, "after pts adjustment"); + ZM_DUMP_STREAM_PACKET(audio_in_stream, (*ipkt), "after pts adjustment"); if ( audio_out_codec ) { // I wonder if we can get multiple frames per packet? Probably @@ -1205,7 +1205,7 @@ int VideoStore::writeAudioFramePacket(ZMPacket *zm_packet) { opkt.pts = ipkt->pts; opkt.dts = ipkt->dts; av_packet_rescale_ts(&opkt, audio_in_stream->time_base, audio_out_stream->time_base); - dumpPacket(audio_out_stream, &opkt, "after stream pts adjustment"); + ZM_DUMP_STREAM_PACKET(audio_out_stream, opkt, "after stream pts adjustment"); write_packet(&opkt, audio_out_stream); zm_av_packet_unref(&opkt); @@ -1257,7 +1257,7 @@ int VideoStore::write_packet(AVPacket *pkt, AVStream *stream) { pkt->pts = pkt->dts; } - dumpPacket(stream, pkt, "finished pkt"); + ZM_DUMP_STREAM_PACKET(stream, (*pkt), "finished pkt"); next_dts[stream->index] = opkt.dts + opkt.duration; Debug(3, "video_next_dts has become %" PRId64, next_dts[stream->index]);