From 9a66704ec352a549016cb4da91b5a4fd427982e1 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Sun, 8 Oct 2023 19:56:02 -0500 Subject: [PATCH] Fixing a few regressions, writing audio trailer, crashes due to writing audio trailer, etc... --- src/FFmpegWriter.cpp | 50 ++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/FFmpegWriter.cpp b/src/FFmpegWriter.cpp index a18c3170..eea37274 100644 --- a/src/FFmpegWriter.cpp +++ b/src/FFmpegWriter.cpp @@ -699,11 +699,11 @@ void FFmpegWriter::write_frame(std::shared_ptr frame) { // Create blank exception bool has_error_encoding_video = false; - // Process all audio frames (in a separate thread) + // Process audio frame if (info.has_audio && audio_st) write_audio_packets(false, frame); - // Encode and add the frame to the output file + // Process video frame if (info.has_video && video_st) process_video_packet(frame); @@ -714,23 +714,17 @@ void FFmpegWriter::write_frame(std::shared_ptr frame) { AVFrame *frame_final = av_frames[frame]; // Write frame to video file - bool success = write_video_packet(frame, frame_final); - if (!success) + if (!write_video_packet(frame, frame_final)) { has_error_encoding_video = true; + } + + // Deallocate buffer and AVFrame + av_freep(&(frame_final->data[0])); + AV_FREE_FRAME(&frame_final); + av_frames.erase(frame); } } - // Does this frame's AVFrame still exist - if (av_frames.count(frame)) { - // Get AVFrame - AVFrame *av_frame = av_frames[frame]; - - // Deallocate buffer and AVFrame - av_freep(&(av_frame->data[0])); - AV_FREE_FRAME(&av_frame); - av_frames.erase(frame); - } - // Done writing is_writing = false; @@ -758,6 +752,10 @@ void FFmpegWriter::WriteFrame(ReaderBase *reader, int64_t start, int64_t length) // Write the file trailer (after all frames are written) void FFmpegWriter::WriteTrailer() { + // Process final audio frame (if any) + if (info.has_audio && audio_st) + write_audio_packets(true, NULL); + // Flush encoders (who sometimes hold on to frames) flush_encoders(); @@ -1530,7 +1528,7 @@ void FFmpegWriter::open_video(AVFormatContext *oc, AVStream *st) { // write all queued frames' audio to the video file void FFmpegWriter::write_audio_packets(bool is_final, std::shared_ptr frame) { - if (!frame) + if (!frame && !is_final) return; // Init audio buffers / variables @@ -1548,17 +1546,19 @@ void FFmpegWriter::write_audio_packets(bool is_final, std::shared_ptrSampleRate(); - samples_in_frame = frame->GetAudioSamplesCount(); - channels_in_frame = frame->GetAudioChannelsCount(); - channel_layout_in_frame = frame->ChannelsLayout(); - // Get audio sample array float *frame_samples_float = NULL; - // Get samples interleaved together (c1 c2 c1 c2 c1 c2) - frame_samples_float = frame->GetInterleavedAudioSamples(&samples_in_frame); + + // Get the audio details from this frame + if (frame) { + sample_rate_in_frame = frame->SampleRate(); + samples_in_frame = frame->GetAudioSamplesCount(); + channels_in_frame = frame->GetAudioChannelsCount(); + channel_layout_in_frame = frame->ChannelsLayout(); + + // Get samples interleaved together (c1 c2 c1 c2 c1 c2) + frame_samples_float = frame->GetInterleavedAudioSamples(&samples_in_frame); + } // Calculate total samples total_frame_samples = samples_in_frame * channels_in_frame;