From 043e44d273a3a65722ee5324841fc012ee96b7a3 Mon Sep 17 00:00:00 2001 From: APHW2 MFGENG Date: Mon, 13 Nov 2017 14:24:28 -0800 Subject: [PATCH] fix, must clear the frame --- src/zm_analysis_thread.cpp | 3 --- src/zm_camera.h | 4 ++-- src/zm_image.cpp | 2 +- src/zm_image.h | 6 +++++- src/zm_monitor.cpp | 2 ++ src/zm_packet.cpp | 1 + src/zm_remote_camera_nvsocket.cpp | 24 ++++++++++++++++++++++++ src/zm_remote_camera_nvsocket.h | 1 + src/zm_videostore.cpp | 14 +++++++++----- 9 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/zm_analysis_thread.cpp b/src/zm_analysis_thread.cpp index b08632ac9..a6eec451f 100644 --- a/src/zm_analysis_thread.cpp +++ b/src/zm_analysis_thread.cpp @@ -29,13 +29,10 @@ int AnalysisThread::run() { if ( analysis_update_delay ) { cur_time = time( 0 ); if ( (unsigned int)( cur_time - last_analysis_update_time ) > analysis_update_delay ) { -Debug(4, "Updating " ); analysis_rate = monitor->GetAnalysisRate(); monitor->UpdateAdaptiveSkip(); last_analysis_update_time = cur_time; } - } else { -Debug(4, "Not Updating " ); } if ( !monitor->Analyse() ) { diff --git a/src/zm_camera.h b/src/zm_camera.h index 9d3447fe4..3cc93f5cf 100644 --- a/src/zm_camera.h +++ b/src/zm_camera.h @@ -107,8 +107,8 @@ public: virtual int PreCapture()=0; virtual int Capture(ZMPacket &p)=0; virtual int PostCapture()=0; - AVStream *get_VideoStream() { return NULL; }; - AVStream *get_AudioStream() { return NULL; }; + virtual AVStream *get_VideoStream() { return NULL; }; + virtual AVStream *get_AudioStream() { return NULL; }; int get_VideoStreamId() { return mVideoStreamId; }; int get_AudioStreamId() { return mAudioStreamId; }; }; diff --git a/src/zm_image.cpp b/src/zm_image.cpp index e8293b3fb..11cb193c0 100644 --- a/src/zm_image.cpp +++ b/src/zm_image.cpp @@ -610,7 +610,7 @@ void Image::Assign(const unsigned int p_width, const unsigned int p_height, cons size = new_size; } - if(new_buffer != buffer) + if ( new_buffer != buffer ) (*fptr_imgbufcpy)(buffer, new_buffer, size); } diff --git a/src/zm_image.h b/src/zm_image.h index e75a5e357..e837ed1b0 100644 --- a/src/zm_image.h +++ b/src/zm_image.h @@ -173,7 +173,11 @@ public: if ( colours == ZM_COLOUR_RGB32 ) { return AV_PIX_FMT_RGBA; } else if ( colours == ZM_COLOUR_RGB24 ) { - return AV_PIX_FMT_RGB24; + if ( subpixelorder == ZM_SUBPIX_ORDER_BGR){ + return AV_PIX_FMT_BGR24; + } else { + return AV_PIX_FMT_RGB24; + } } else if ( colours == ZM_COLOUR_GRAY8 ) { return AV_PIX_FMT_GRAY8; } else { diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index eba8234a4..b5be3a9e9 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -2795,6 +2795,8 @@ int Monitor::Capture() { unsigned int index = image_count % image_buffer_count; Image* capture_image = image_buffer[index].image; ZMPacket *packet = &image_buffer[index]; +// clears frame +packet->reset(); int captureResult = 0; unsigned int deinterlacing_value = deinterlacing & 0xff; diff --git a/src/zm_packet.cpp b/src/zm_packet.cpp index f60a19390..df7a776cb 100644 --- a/src/zm_packet.cpp +++ b/src/zm_packet.cpp @@ -63,6 +63,7 @@ ZMPacket::ZMPacket( AVPacket *p, AVFrame *f, Image *i ) { ZMPacket::~ZMPacket() { zm_av_packet_unref( &packet ); if ( frame ) { + //av_free(frame->data); av_frame_free( &frame ); } // We assume the image was allocated elsewhere, so we just unref it. diff --git a/src/zm_remote_camera_nvsocket.cpp b/src/zm_remote_camera_nvsocket.cpp index 88227735b..e9fe1ac60 100644 --- a/src/zm_remote_camera_nvsocket.cpp +++ b/src/zm_remote_camera_nvsocket.cpp @@ -68,6 +68,7 @@ RemoteCameraNVSocket::RemoteCameraNVSocket( timeout.tv_sec = 0; timeout.tv_usec = 0; subpixelorder = ZM_SUBPIX_ORDER_BGR; + video_stream = NULL; if ( capture ) { Initialise(); @@ -212,3 +213,26 @@ int RemoteCameraNVSocket::Capture( ZMPacket &zm_packet ) { int RemoteCameraNVSocket::PostCapture() { return( 0 ); } +AVStream *RemoteCameraNVSocket::get_VideoStream() { + if ( ! video_stream ) { + AVFormatContext *oc = avformat_alloc_context(); + video_stream = avformat_new_stream( oc, NULL ); + if ( video_stream ) { +#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) + video_stream->codecpar->width = width; + video_stream->codecpar->height = height; + video_stream->codecpar->format = GetFFMPEGPixelFormat(colours,subpixelorder); +#else + video_stream->codec->width = width; + video_stream->codec->height = height; + video_stream->codec->pix_fmt = GetFFMPEGPixelFormat(colours,subpixelorder); +#endif + } else { + Error("Can't create video stream"); + } +} else { +Debug(2,"Have videostream"); + } +Debug(2,"Get videoStream"); + return video_stream; +} diff --git a/src/zm_remote_camera_nvsocket.h b/src/zm_remote_camera_nvsocket.h index 990d34aec..45f1e5cf8 100644 --- a/src/zm_remote_camera_nvsocket.h +++ b/src/zm_remote_camera_nvsocket.h @@ -69,6 +69,7 @@ bool p_record_audio ); int PrimeCapture(); int Capture( ZMPacket &p ); int PostCapture(); +AVStream* get_VideoStream(); }; #endif // ZM_REMOTE_CAMERA_NVSOCKET_H diff --git a/src/zm_videostore.cpp b/src/zm_videostore.cpp index b47a2acac..9f1a67dd9 100644 --- a/src/zm_videostore.cpp +++ b/src/zm_videostore.cpp @@ -87,9 +87,12 @@ VideoStore::VideoStore( video_in_stream_index = video_in_stream->index; #if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) video_in_ctx = avcodec_alloc_context3(NULL); +Debug(2,"About to copy aparames"); avcodec_parameters_to_context(video_in_ctx, video_in_stream->codecpar); zm_dump_codecpar( video_in_stream->codecpar ); +Debug(2,"About to copy aparames"); +//video_in_ctx.codec_id = video_in_stream->codecpar.codec_id; #else video_in_ctx = video_in_stream->codec; #endif @@ -100,6 +103,7 @@ VideoStore::VideoStore( } video_out_ctx = NULL; + video_out_ctx = avcodec_alloc_context3(NULL); // Copy params from instream to ctx if ( video_in_stream && ( video_in_ctx->codec_id == AV_CODEC_ID_H264 ) ) { @@ -114,13 +118,10 @@ VideoStore::VideoStore( zm_dump_codec(video_out_ctx); } #else - video_out_ctx = avcodec_alloc_context3(NULL); avcodec_copy_context( video_out_ctx, video_in_ctx ); #endif // Same codec, just copy the packets, otherwise we have to decode/encode video_out_codec = (AVCodec *)video_in_ctx->codec; - video_out_ctx->time_base = video_in_ctx->time_base; - video_out_stream->time_base = video_in_stream->time_base; } else { /** Create a new frame to store the */ @@ -168,8 +169,11 @@ VideoStore::VideoStore( Debug( 3, "Encoder Option %s=%s", e->key, e->value ); } } - if ( ! av_dict_get( opts, "preset", NULL, 0 ) ) - av_dict_set( &opts, "preset", "superfast", 0 ); + + if ( ! av_dict_get( opts, "preset", NULL, 0 ) ) { + Debug(2,"Setting preset to superfast"); + av_dict_set( &opts, "preset", "superfast", 0 ); + } if ( (ret = avcodec_open2(video_out_ctx, video_out_codec, &opts)) < 0 ) { Warning("Can't open video codec (%s)! %s, trying h264",