From 0ac26277e3b48dfa3134913867e3aba2ce2e18f5 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 2 Jun 2017 16:52:52 -0400 Subject: [PATCH 1/2] debug, fix deprecations, fix audio --- src/zm_ffmpeg_camera.cpp | 23 ++-- src/zm_videostore.cpp | 227 +++++++++++++++++++++++---------------- 2 files changed, 144 insertions(+), 106 deletions(-) diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index d11ea936e..7ebbeae07 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -23,7 +23,7 @@ #include "zm_ffmpeg_camera.h" -extern "C"{ +extern "C" { #include "libavutil/time.h" } #ifndef AV_ERROR_MAX_STRING_SIZE @@ -125,8 +125,7 @@ int FfmpegCamera::PreCapture() return( 0 ); } -int FfmpegCamera::Capture( Image &image ) -{ +int FfmpegCamera::Capture( Image &image ) { if (!mCanCapture){ return -1; } @@ -459,8 +458,7 @@ int FfmpegCamera::CloseFfmpeg(){ av_frame_free( &mRawFrame ); #if HAVE_LIBSWSCALE - if ( mConvertContext ) - { + if ( mConvertContext ) { sws_freeContext( mConvertContext ); mConvertContext = NULL; } @@ -487,8 +485,7 @@ int FfmpegCamera::CloseFfmpeg(){ return 0; } -int FfmpegCamera::FfmpegInterruptCallback(void *ctx) -{ +int FfmpegCamera::FfmpegInterruptCallback(void *ctx) { FfmpegCamera* camera = reinterpret_cast(ctx); if (camera->mIsOpening){ int now = time(NULL); @@ -526,14 +523,14 @@ void *FfmpegCamera::ReopenFfmpegThreadCallback(void *ctx){ //Function to handle capture and store int FfmpegCamera::CaptureAndRecord( Image &image, timeval recording, char* event_file ) { - if (!mCanCapture){ + if ( ! mCanCapture ) { return -1; } int ret; static char errbuf[AV_ERROR_MAX_STRING_SIZE]; // If the reopen thread has a value, but mCanCapture != 0, then we have just reopened the connection to the ffmpeg device, and we can clean up the thread. - if (mReopenThread != 0) { + if ( mReopenThread != 0 ) { void *retval = 0; ret = pthread_join(mReopenThread, &retval); @@ -545,7 +542,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, timeval recording, char* event mReopenThread = 0; } - if (mVideoCodecContext->codec_id != AV_CODEC_ID_H264) { + if ( mVideoCodecContext->codec_id != AV_CODEC_ID_H264 ) { Error( "Input stream is not h264. The stored event file may not be viewable in browser." ); } @@ -689,7 +686,7 @@ else if ( packet.pts && video_last_pts > packet.pts ) { // The following lines should ensure that the queue always begins with a video keyframe if ( packet.stream_index == mAudioStreamId ) { -Debug(2, "Have audio packet, reocrd_audio is (%d) and packetqueue.size is (%d)", record_audio, packetqueue.size() ); +//Debug(2, "Have audio packet, reocrd_audio is (%d) and packetqueue.size is (%d)", record_audio, packetqueue.size() ); if ( record_audio && packetqueue.size() ) { // if it's audio, and we are doing audio, and there is already something in the queue packetqueue.queuePacket( &packet ); @@ -781,8 +778,10 @@ Debug(2, "Have audio packet, reocrd_audio is (%d) and packetqueue.size is (%d)", return 0; } } else { - Debug(4, "Not recording audio packet" ); + Debug(4, "Not doing recording of audio packet" ); } + } else { + Debug(4, "Have audio packet, but not recording atm" ); } } else { #if LIBAVUTIL_VERSION_CHECK(56, 23, 0, 23, 0) diff --git a/src/zm_videostore.cpp b/src/zm_videostore.cpp index 0f7892f80..826f62d35 100644 --- a/src/zm_videostore.cpp +++ b/src/zm_videostore.cpp @@ -27,7 +27,7 @@ #include "zm.h" #include "zm_videostore.h" -extern "C"{ +extern "C" { #include "libavutil/time.h" } @@ -37,23 +37,23 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in, int64_t nStartTime, Monitor * monitor ) { + video_input_stream = p_video_input_stream; audio_input_stream = p_audio_input_stream; +#if LIBAVCODEC_VERSION_CHECK(57, 0, 0, 0, 0) + video_input_context = avcodec_alloc_context3( NULL ); + avcodec_parameters_to_context( video_input_context, video_input_stream->codecpar ); +#else video_input_context = video_input_stream->codec; +#endif //store inputs in variables local to class filename = filename_in; format = format_in; - keyframeMessage = false; - keyframeSkipNumber = 0; - Info("Opening video storage stream %s format: %s\n", filename, format); - //Init everything we need, shouldn't have to do this, ffmpeg_camera or something else will call it. - //av_register_all(); - ret = avformat_alloc_output_context2(&oc, NULL, NULL, filename); if ( ret < 0 ) { Warning("Could not create video storage stream %s as no output context" @@ -62,11 +62,11 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in, av_make_error_string(ret).c_str() ); } else { - Debug(2, "Success alocateing output context"); + Debug(2, "Success allocating output context"); } //Couldn't deduce format from filename, trying from format name - if (!oc) { + if ( ! oc ) { avformat_alloc_output_context2(&oc, NULL, format, filename); if (!oc) { Fatal("Could not create video storage stream %s as no output context" @@ -83,36 +83,46 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in, oc->metadata = pmetadata; output_format = oc->oformat; - Debug(2, "setting parameters"); -#if LIBAVCODEC_VERSION_CHECK(58, 0, 0, 0, 0) - AVCodec *codec = avcodec_find_decoder( video_input_stream->codecpar->codec_id ); - video_output_context = avcodec_alloc_context3( codec ); +#if LIBAVCODEC_VERSION_CHECK(57, 0, 0, 0, 0) + // Since we are not re-encoding, all we have to do is copy the parameters + video_output_context = avcodec_alloc_context3( NULL ); + + // Copy params from inputstream to context ret = avcodec_parameters_to_context( video_output_context, video_input_stream->codecpar ); + if ( ret < 0 ) { + Error( "Could not initialize context parameteres"); + return; + } else { + Debug( 2, "Success getting parameters"); + } + + video_output_stream = avformat_new_stream( oc, NULL ); + if ( ! video_output_stream ) { + Fatal("Unable to create video out stream\n"); + } else { + Debug(2, "Success creating video out stream" ); + } + + // Now copy them to the output stream + ret = avcodec_parameters_from_context( video_output_stream->codecpar, video_output_context ); if ( ret < 0 ) { Error( "Could not initialize stream parameteres"); return; } else { Debug(2, "Success setting parameters"); } - if ( avcodec_open2( video_output_context, codec, NULL ) < 0 ) { - Fatal("Unable to open video out codec\n"); - } - video_output_stream = avformat_new_stream( oc, codec ); - if (!video_output_stream) { - Fatal("Unable to create video out stream\n"); - } else { - Debug(2, "Success creating video out stream" ); - } + + zm_dump_stream_format( oc, 0, 0, 1 ); #else - video_output_stream = avformat_new_stream(oc, (AVCodec*)video_input_context->codec); - if (!video_output_stream) { + video_output_stream = avformat_new_stream(oc, (AVCodec*)video_input_context->codec ); + if ( ! video_output_stream ) { Fatal("Unable to create video out stream\n"); } else { Debug(2, "Success creating video out stream" ); } video_output_context = video_output_stream->codec; - ret = avcodec_copy_context(video_output_context, video_input_context ); + ret = avcodec_copy_context( video_output_context, video_input_context ); if (ret < 0) { Fatal("Unable to copy input video context to output video context %s\n", av_make_error_string(ret).c_str()); @@ -138,7 +148,7 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in, // WHY? //video_output_context->codec_tag = 0; - if (!video_output_context->codec_tag) { + if ( ! video_output_context->codec_tag ) { Debug(2, "No codec_tag"); if (! oc->oformat->codec_tag || av_codec_get_id (oc->oformat->codec_tag, video_input_context->codec_tag) == video_output_context->codec_id @@ -178,14 +188,22 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in, resample_context = NULL; #endif - if (audio_input_stream) { + if ( audio_input_stream ) { Debug(3, "Have audio stream" ); +#if LIBAVCODEC_VERSION_CHECK(57, 0, 0, 0, 0) + + audio_input_context = avcodec_alloc_context3( NULL ); + ret = avcodec_parameters_to_context( audio_input_context, audio_input_stream->codecpar ); +#else audio_input_context = audio_input_stream->codec; +#endif if ( audio_input_context->codec_id != AV_CODEC_ID_AAC ) { static char error_buffer[256]; avcodec_string(error_buffer, sizeof(error_buffer), audio_input_context, 0 ); - Debug(3, "Got something other than AAC (%s)", error_buffer ); + Debug(2, "Got something other than AAC (%s)", error_buffer ); + + if ( ! setup_resampler() ) { return; } @@ -198,10 +216,13 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in, audio_output_stream = NULL; } else { Debug(2, "setting parameters"); - audio_output_context = audio_output_stream->codec; + #if LIBAVCODEC_VERSION_CHECK(57, 0, 0, 0, 0) + audio_output_context = avcodec_alloc_context3( NULL ); + // Copy params from inputstream to context ret = avcodec_parameters_to_context( audio_output_context, audio_input_stream->codecpar ); #else + audio_output_context = audio_output_stream->codec; ret = avcodec_copy_context(audio_output_context, audio_input_context); #endif if (ret < 0) { @@ -267,8 +288,10 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in, video_last_dts = 0; audio_last_pts = 0; audio_last_dts = 0; - previous_pts = 0; - previous_dts = 0; + video_previous_pts = 0; + video_previous_dts = 0; + audio_previous_pts = 0; + audio_previous_dts = 0; } // VideoStore::VideoStore @@ -356,6 +379,14 @@ bool VideoStore::setup_resampler() { #ifdef HAVE_LIBAVRESAMPLE static char error_buffer[256]; + // Newer ffmpeg wants to keep everything separate... so have to lookup our own decoder, can't reuse the one from the camera. + AVCodec *audio_input_codec = avcodec_find_decoder(audio_input_stream->codecpar->codec_id); + ret = avcodec_open2( audio_input_context, audio_input_codec, NULL ); + if ( ret < 0 ) { + Error("Can't open input codec!"); + return false; + } + audio_output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC); if ( ! audio_output_codec ) { Error("Could not find codec for AAC"); @@ -363,8 +394,8 @@ bool VideoStore::setup_resampler() { } Debug(2, "Have audio output codec"); - audio_output_stream = avformat_new_stream( oc, audio_output_codec ); - audio_output_context = audio_output_stream->codec; + //audio_output_context = audio_output_stream->codec; + audio_output_context = avcodec_alloc_context3( audio_output_codec ); if ( ! audio_output_context ) { Error( "could not allocate codec context for AAC\n"); @@ -374,18 +405,15 @@ bool VideoStore::setup_resampler() { Debug(2, "Have audio_output_context"); - AVDictionary *opts = NULL; - av_dict_set(&opts, "strict", "experimental", 0); - /* put sample parameters */ audio_output_context->bit_rate = audio_input_context->bit_rate; audio_output_context->sample_rate = audio_input_context->sample_rate; audio_output_context->channels = audio_input_context->channels; audio_output_context->channel_layout = audio_input_context->channel_layout; audio_output_context->sample_fmt = audio_input_context->sample_fmt; - //audio_output_context->refcounted_frames = 1; + audio_output_context->refcounted_frames = 1; - if (audio_output_codec->supported_samplerates) { + if ( audio_output_codec->supported_samplerates ) { int found = 0; for ( unsigned int i = 0; audio_output_codec->supported_samplerates[i]; i++) { if ( audio_output_context->sample_rate == audio_output_codec->supported_samplerates[i] ) { @@ -402,36 +430,14 @@ bool VideoStore::setup_resampler() { } /* check that the encoder supports s16 pcm input */ - if (!check_sample_fmt( audio_output_codec, audio_output_context->sample_fmt)) { + if ( ! check_sample_fmt( audio_output_codec, audio_output_context->sample_fmt ) ) { Debug( 3, "Encoder does not support sample format %s, setting to FLTP", av_get_sample_fmt_name( audio_output_context->sample_fmt)); audio_output_context->sample_fmt = AV_SAMPLE_FMT_FLTP; } - //audio_output_stream->time_base = audio_input_stream->time_base; audio_output_context->time_base = (AVRational){ 1, audio_output_context->sample_rate }; - Debug(3, "Audio Time bases input stream (%d/%d) input codec: (%d/%d) output_stream (%d/%d) output codec (%d/%d)", - audio_input_stream->time_base.num, - audio_input_stream->time_base.den, - audio_input_context->time_base.num, - audio_input_context->time_base.den, - audio_output_stream->time_base.num, - audio_output_stream->time_base.den, - audio_output_context->time_base.num, - audio_output_context->time_base.den - ); - - ret = avcodec_open2(audio_output_context, audio_output_codec, &opts ); - av_dict_free(&opts); - if ( ret < 0 ) { - av_strerror(ret, error_buffer, sizeof(error_buffer)); - Fatal( "could not open codec (%d) (%s)\n", ret, error_buffer ); - audio_output_codec = NULL; - audio_output_context = NULL; - audio_output_stream = NULL; - return false; - } Debug(1, "Audio output bit_rate (%d) sample_rate(%d) channels(%d) fmt(%d) layout(%d) frame_size(%d)", audio_output_context->bit_rate, @@ -442,7 +448,28 @@ bool VideoStore::setup_resampler() { audio_output_context->frame_size ); - output_frame_size = audio_output_context->frame_size; + // Now copy them to the output stream + audio_output_stream = avformat_new_stream( oc, audio_output_codec ); + + ret = avcodec_parameters_from_context( audio_output_stream->codecpar, audio_output_context ); + if ( ret < 0 ) { + Error( "Could not initialize stream parameteres"); + return false; + } + + AVDictionary *opts = NULL; + av_dict_set( &opts, "strict", "experimental", 0); + ret = avcodec_open2( audio_output_context, audio_output_codec, &opts ); + av_dict_free(&opts); + if ( ret < 0 ) { + av_strerror(ret, error_buffer, sizeof(error_buffer)); + Fatal( "could not open codec (%d) (%s)\n", ret, error_buffer ); + audio_output_codec = NULL; + audio_output_context = NULL; + audio_output_stream = NULL; + return false; + } + /** Create a new frame to store the audio samples. */ if (!(input_frame = zm_av_frame_alloc())) { Error("Could not allocate input frame"); @@ -577,9 +604,9 @@ int VideoStore::writeVideoFramePacket( AVPacket *ipkt ) { if ( ipkt->pts < video_last_pts ) { Debug(1, "Resetting video_last_pts from (%d) to (%d)", video_last_pts, ipkt->pts ); // wrap around, need to figure out the distance FIXME having this wrong should cause a jump, but then play ok? - opkt.pts = previous_pts + av_rescale_q( ipkt->pts, video_input_stream->time_base, video_output_stream->time_base); + opkt.pts = video_previous_pts + av_rescale_q( ipkt->pts, video_input_stream->time_base, video_output_stream->time_base); } else { - opkt.pts = previous_pts + av_rescale_q( ipkt->pts - video_last_pts, video_input_stream->time_base, video_output_stream->time_base); + opkt.pts = video_previous_pts + av_rescale_q( ipkt->pts - video_last_pts, video_input_stream->time_base, video_output_stream->time_base); } } Debug(3, "opkt.pts = %d from ipkt->pts(%d) - last_pts(%d)", opkt.pts, ipkt->pts, video_last_pts ); @@ -603,24 +630,20 @@ int VideoStore::writeVideoFramePacket( AVPacket *ipkt ) { // why are we using cur_dts instead of packet.dts? I think cur_dts is in AV_TIME_BASE_Q, but ipkt.dts is in video_input_stream->time_base if ( video_input_stream->cur_dts < video_last_dts ) { Debug(1, "Resetting video_last_dts from (%d) to (%d) p.dts was (%d)", video_last_dts, video_input_stream->cur_dts, ipkt->dts ); - opkt.dts = previous_dts + av_rescale_q(video_input_stream->cur_dts, AV_TIME_BASE_Q, video_output_stream->time_base); + opkt.dts = video_previous_dts + av_rescale_q(video_input_stream->cur_dts, AV_TIME_BASE_Q, video_output_stream->time_base); } else { - opkt.dts = previous_dts + av_rescale_q(video_input_stream->cur_dts - video_last_dts, AV_TIME_BASE_Q, video_output_stream->time_base); + opkt.dts = video_previous_dts + av_rescale_q(video_input_stream->cur_dts - video_last_dts, AV_TIME_BASE_Q, video_output_stream->time_base); } - Debug(3, "opkt.dts = %d from video_input_stream->cur_dts(%d) - previus_dts(%d)", - opkt.dts, video_input_stream->cur_dts, video_last_dts - ); + Debug(3, "opkt.dts = %d from video_input_stream->cur_dts(%d) - previus_dts(%d)", opkt.dts, video_input_stream->cur_dts, video_last_dts ); video_last_dts = video_input_stream->cur_dts; } else { if ( ipkt->dts < video_last_dts ) { Debug(1, "Resetting video_last_dts from (%d) to (%d)", video_last_dts, ipkt->dts ); - opkt.dts = previous_dts + av_rescale_q( ipkt->dts, video_input_stream->time_base, video_output_stream->time_base); + opkt.dts = video_previous_dts + av_rescale_q( ipkt->dts, video_input_stream->time_base, video_output_stream->time_base); } else { - opkt.dts = previous_dts + av_rescale_q( ipkt->dts - video_last_dts, video_input_stream->time_base, video_output_stream->time_base); + opkt.dts = video_previous_dts + av_rescale_q( ipkt->dts - video_last_dts, video_input_stream->time_base, video_output_stream->time_base); } - Debug(3, "opkt.dts = %d from ipkt.dts(%d) - previus_dts(%d)", - opkt.dts, ipkt->dts, video_last_dts - ); + Debug(3, "opkt.dts = %d from ipkt.dts(%d) - previus_dts(%d)", opkt.dts, ipkt->dts, video_last_dts ); video_last_dts = ipkt->dts; } } @@ -657,15 +680,15 @@ Debug(1, "writing video packet pts(%d) dts(%d) duration(%d)", opkt.pts, opkt.dts dumpPacket( ipkt); dumpPacket(&opkt); - } else if ((previous_dts > 0) && (previous_dts > opkt.dts)) { - Warning("%s:%d: DTS out of order: %lld \u226E %lld; discarding frame", __FILE__, __LINE__, previous_dts, opkt.dts); - previous_dts = opkt.dts; + } else if ((video_previous_dts > 0) && (video_previous_dts > opkt.dts)) { + Warning("%s:%d: DTS out of order: %lld \u226E %lld; discarding frame", __FILE__, __LINE__, video_previous_dts, opkt.dts); + video_previous_dts = opkt.dts; dumpPacket(&opkt); } else { - previous_dts = opkt.dts; // Unsure if av_interleaved_write_frame() clobbers opkt.dts when out of order, so storing in advance - previous_pts = opkt.pts; + video_previous_dts = opkt.dts; // Unsure if av_interleaved_write_frame() clobbers opkt.dts when out of order, so storing in advance + video_previous_pts = opkt.pts; ret = av_interleaved_write_frame(oc, &opkt); if(ret<0){ // There's nothing we can really do if the frame is rejected, just drop it and get on with the next @@ -678,17 +701,16 @@ Debug(1, "writing video packet pts(%d) dts(%d) duration(%d)", opkt.pts, opkt.dts return 0; -} +} // end int VideoStore::writeVideoFramePacket( AVPacket *ipkt ) int VideoStore::writeAudioFramePacket( AVPacket *ipkt ) { Debug(4, "writeAudioFrame"); - if(!audio_output_stream) { + if ( ! audio_output_stream ) { Debug(1, "Called writeAudioFramePacket when no audio_output_stream"); return 0;//FIXME -ve return codes do not free packet in ffmpeg_camera at the moment } - if ( audio_output_codec ) { #ifdef HAVE_LIBAVRESAMPLE @@ -724,12 +746,10 @@ int VideoStore::writeAudioFramePacket( AVPacket *ipkt ) { av_make_error_string(ret).c_str()); dumpPacket( ipkt ); av_frame_free( &input_frame ); - zm_av_packet_unref( &opkt ); return 0; } if ( ! data_present ) { Debug(2, "Not ready to transcode a frame yet."); - zm_av_packet_unref(&opkt); return 0; } #endif @@ -775,10 +795,21 @@ int VideoStore::writeAudioFramePacket( AVPacket *ipkt ) { * The output audio stream encoder is used to do this. */ #if LIBAVCODEC_VERSION_CHECK(57, 0, 0, 0, 0) + if (( ret = avcodec_send_frame( audio_output_context, output_frame ) ) < 0 ) { + Error( "Could not send frame (error '%s')", + av_make_error_string(ret).c_str()); + zm_av_packet_unref(&opkt); + return 0; + } + if (( ret = avcodec_receive_packet( audio_output_context, &opkt )) < 0 ) { + Error( "Could not recieve packet (error '%s')", + av_make_error_string(ret).c_str()); + zm_av_packet_unref(&opkt); + return 0; + } #else if (( ret = avcodec_encode_audio2( audio_output_context, &opkt, output_frame, &data_present )) < 0) { -#endif Error( "Could not encode frame (error '%s')", av_make_error_string(ret).c_str()); zm_av_packet_unref(&opkt); @@ -789,6 +820,7 @@ int VideoStore::writeAudioFramePacket( AVPacket *ipkt ) { zm_av_packet_unref(&opkt); return 0; } +#endif #endif } else { @@ -802,14 +834,17 @@ int VideoStore::writeAudioFramePacket( AVPacket *ipkt ) { //Scale the PTS of the outgoing packet to be the correct time base if ( ipkt->pts != AV_NOPTS_VALUE ) { - if ( !audio_last_pts ) { + if ( ! audio_last_pts ) { opkt.pts = 0; + Debug(1, "No audio_last_pts"); } else { if ( audio_last_pts > ipkt->pts ) { Debug(1, "Resetting audeo_start_pts from (%d) to (%d)", audio_last_pts, ipkt->pts ); + opkt.pts = audio_previous_pts + av_rescale_q(ipkt->pts, audio_input_stream->time_base, audio_output_stream->time_base); + } else { + opkt.pts = audio_previous_pts + av_rescale_q(ipkt->pts - audio_last_pts, audio_input_stream->time_base, audio_output_stream->time_base); } - opkt.pts = previous_pts + av_rescale_q(ipkt->pts - audio_last_pts, audio_input_stream->time_base, audio_output_stream->time_base); - Debug(2, "opkt.pts = %d from ipkt->pts(%d) - last_pts(%d)", opkt.pts, ipkt->pts, audio_last_pts ); + Debug(2, "audio opkt.pts = %d from ipkt->pts(%d) - last_pts(%d)", opkt.pts, ipkt->pts, audio_last_pts ); } audio_last_pts = ipkt->pts; } else { @@ -820,28 +855,30 @@ int VideoStore::writeAudioFramePacket( AVPacket *ipkt ) { //Scale the DTS of the outgoing packet to be the correct time base if ( ! audio_last_dts ) { opkt.dts = 0; + } else { if( ipkt->dts == AV_NOPTS_VALUE ) { // So if the input has no dts assigned... still need an output dts... so we use cur_dts? if ( audio_last_dts > audio_input_stream->cur_dts ) { - Debug(1, "Resetting audio_last_pts from (%d) to cur_dts (%d)", audio_last_dts, audio_input_stream->cur_dts ); - opkt.dts = previous_dts + av_rescale_q( audio_input_stream->cur_dts, AV_TIME_BASE_Q, audio_output_stream->time_base); + Debug(1, "Resetting audio_last_dts from (%d) to cur_dts (%d)", audio_last_dts, audio_input_stream->cur_dts ); + opkt.dts = audio_previous_dts + av_rescale_q( audio_input_stream->cur_dts, AV_TIME_BASE_Q, audio_output_stream->time_base); } else { - opkt.dts = previous_dts + av_rescale_q( audio_input_stream->cur_dts - audio_last_dts, AV_TIME_BASE_Q, audio_output_stream->time_base); + opkt.dts = audio_previous_dts + av_rescale_q( audio_input_stream->cur_dts - audio_last_dts, AV_TIME_BASE_Q, audio_output_stream->time_base); } audio_last_dts = audio_input_stream->cur_dts; Debug(2, "opkt.dts = %d from video_input_stream->cur_dts(%d) - last_dts(%d)", opkt.dts, audio_input_stream->cur_dts, audio_last_dts ); } else { if ( audio_last_dts > ipkt->dts ) { Debug(1, "Resetting audio_last_dts from (%d) to (%d)", audio_last_dts, ipkt->dts ); - opkt.dts = previous_dts + av_rescale_q(ipkt->dts, audio_input_stream->time_base, audio_output_stream->time_base); + opkt.dts = audio_previous_dts + av_rescale_q(ipkt->dts, audio_input_stream->time_base, audio_output_stream->time_base); } else { - opkt.dts = previous_dts + av_rescale_q(ipkt->dts - audio_last_dts, audio_input_stream->time_base, audio_output_stream->time_base); + opkt.dts = audio_previous_dts + av_rescale_q(ipkt->dts - audio_last_dts, audio_input_stream->time_base, audio_output_stream->time_base); } Debug(2, "opkt.dts = %d from ipkt->dts(%d) - last_dts(%d)", opkt.dts, ipkt->dts, audio_last_dts ); } } + audio_last_dts = ipkt->dts; if ( opkt.dts > opkt.pts ) { Debug(1,"opkt.dts(%d) must be <= opkt.pts(%d). Decompression must happen before presentation.", opkt.dts, opkt.pts ); opkt.dts = opkt.pts; @@ -849,15 +886,17 @@ int VideoStore::writeAudioFramePacket( AVPacket *ipkt ) { // I wonder if we could just use duration instead of all the hoop jumping above? opkt.duration = av_rescale_q(ipkt->duration, audio_input_stream->time_base, audio_output_stream->time_base); + Debug( 2, "opkt.pts (%d), opkt.dts(%d) opkt.duration = (%d)", opkt.pts, opkt.dts, opkt.duration ); // pkt.pos: byte position in stream, -1 if unknown opkt.pos = -1; - opkt.flags = ipkt->flags; opkt.stream_index = ipkt->stream_index; Debug(2, "Stream index is %d", opkt.stream_index ); AVPacket safepkt; memcpy(&safepkt, &opkt, sizeof(AVPacket)); + audio_previous_dts = opkt.dts; // Unsure if av_interleaved_write_frame() clobbers opkt.dts when out of order, so storing in advance + audio_previous_pts = opkt.pts; ret = av_interleaved_write_frame(oc, &opkt); if(ret!=0){ Error("Error writing audio frame packet: %s\n", av_make_error_string(ret).c_str()); From eeefb2c8e2e3be7c86189020d812949b5ef8b91f Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 2 Jun 2017 16:53:14 -0400 Subject: [PATCH 2/2] need previous_pts and dts for both video and audio. I don't think they can share --- src/zm_videostore.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/zm_videostore.h b/src/zm_videostore.h index b76153cb8..e4d337df0 100644 --- a/src/zm_videostore.h +++ b/src/zm_videostore.h @@ -61,8 +61,10 @@ AVAudioResampleContext* resample_context; int64_t audio_last_dts; // These are for output, should start at zero. We assume they do not wrap because we just aren't going to save files that big. - int64_t previous_pts; - int64_t previous_dts; + int64_t video_previous_pts; + int64_t video_previous_dts; + int64_t audio_previous_pts; + int64_t audio_previous_dts; int64_t filter_in_rescale_delta_last;