From 0e235b0efbedd75336dd52106c0be131773d2e57 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Tue, 21 Aug 2012 02:12:35 -0500 Subject: [PATCH] Refactored some of the FFmpegReader audio class, trying to locate the pops and cracks in the audio. --- src/Cache.cpp | 6 ------ src/FFmpegReader.cpp | 23 +++++++++++++---------- src/Main.cpp | 39 ++++++++++++++++++++------------------- 3 files changed, 33 insertions(+), 35 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index ab05049b..e6d08ca4 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -25,12 +25,6 @@ void Cache::Add(int frame_number, Frame *frame) frames[frame_number] = frame; frame_numbers.push_front(frame_number); } - else - { - // Frame already exists... so just update the cache - frames.erase(frame_number); - frames[frame_number] = frame; - } } // Check for the existance of a frame in the cache diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index 6de23dc4..9f36e28b 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -642,22 +642,18 @@ void FFmpegReader::ProcessAudioPacket(int requested_frame, int target_frame, int // Init some local variables (for OpenMP) Cache *my_cache = &working_cache; AVPacket *my_packet = packets[packet]; - int16_t *audio_buf = NULL; - int16_t *converted_audio = NULL; - float *channel_buffer = NULL; - int pts = my_packet->pts; // Add audio frame to list of processing audio frames #pragma omp critical (processing_list) processing_audio_frames[target_frame] = target_frame; - #pragma omp task firstprivate(requested_frame, target_frame, my_cache, my_packet, pts, audio_buf, converted_audio, channel_buffer, starting_sample) + #pragma omp task firstprivate(requested_frame, target_frame, my_cache, my_packet, starting_sample) { // Allocate audio buffer - audio_buf = new int16_t[AVCODEC_MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; + int16_t *audio_buf = new int16_t[AVCODEC_MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; // create a new array (to hold the re-sampled audio) - converted_audio = new int16_t[AVCODEC_MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; + int16_t *converted_audio = new int16_t[AVCODEC_MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; int packet_samples = 0; while (my_packet->size > 0) { @@ -692,6 +688,7 @@ void FFmpegReader::ProcessAudioPacket(int requested_frame, int target_frame, int // Re-sample audio samples (if needed) if(aCodecCtx->sample_fmt != AV_SAMPLE_FMT_S16) { + // Audio needs to be converted // Create an audio resample context object (used to convert audio samples) ReSampleContext *resampleCtx = av_audio_resample_init( @@ -718,13 +715,19 @@ void FFmpegReader::ProcessAudioPacket(int requested_frame, int target_frame, int } } + + // DEBUG + //for (int s = 0; s < packet_samples; s++) + // audio_buf[s] = s; + + int starting_frame_number = -1; for (int channel_filter = 0; channel_filter < info.channels; channel_filter++) { // Array of floats (to hold samples for each channel) starting_frame_number = target_frame; int channel_buffer_size = (packet_samples / info.channels) + 1; - channel_buffer = new float[channel_buffer_size]; + float *channel_buffer = new float[channel_buffer_size]; // Init buffer array for (int z = 0; z < channel_buffer_size; z++) @@ -810,7 +813,7 @@ void FFmpegReader::ProcessAudioPacket(int requested_frame, int target_frame, int delete[] audio_buf; audio_buf = NULL; delete[] converted_audio; - converted_audio = 0; + converted_audio = NULL; // Add video frame to list of processing video frames #pragma omp critical (processing_list) @@ -1053,7 +1056,7 @@ Frame* FFmpegReader::CreateFrame(int requested_frame) int samples_per_frame = GetSamplesPerFrame(requested_frame); // Create a new frame on the working cache - Frame *f = new Frame(requested_frame, info.width, info.height, "#000000", samples_per_frame, info.channels); + Frame *f = new Frame(requested_frame, 1, 1, "#000000", samples_per_frame, info.channels); f->SetPixelRatio(info.pixel_ratio.num, info.pixel_ratio.den); f->SetSampleRate(info.sample_rate); diff --git a/src/Main.cpp b/src/Main.cpp index fc4b7c4a..a5bd8fe7 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -33,31 +33,31 @@ int main() r.DisplayInfo(); // Create a writer - FFmpegWriter w("/home/jonathan/output.mp3"); + FFmpegWriter w("/home/jonathan/output.webm"); w.DisplayInfo(); // Set options - w.SetAudioOptions(true, "libmp3lame", 44100, 2, 128000); - //w.SetVideoOptions(true, "libvpx", Fraction(24, 1), 640, 360, Fraction(1,1), false, false, 2000000); + w.SetAudioOptions(true, "libvorbis", 44100, 2, 128000); + w.SetVideoOptions(true, "libvpx", Fraction(24, 1), 640, 360, Fraction(1,1), false, false, 2000000); // Prepare Streams w.PrepareStreams(); // Set Options -// w.SetOption(VIDEO_STREAM, "quality", "good"); -// w.SetOption(VIDEO_STREAM, "g", "120"); -// w.SetOption(VIDEO_STREAM, "qmin", "11"); -// w.SetOption(VIDEO_STREAM, "qmax", "51"); -// w.SetOption(VIDEO_STREAM, "profile", "0"); -// w.SetOption(VIDEO_STREAM, "speed", "0"); -// w.SetOption(VIDEO_STREAM, "level", "216"); -// w.SetOption(VIDEO_STREAM, "rc_lookahead", "16"); -// w.SetOption(VIDEO_STREAM, "rc_min_rate", "100000"); -// w.SetOption(VIDEO_STREAM, "rc_max_rate", "24000000"); -// w.SetOption(VIDEO_STREAM, "slices", "4"); -// w.SetOption(VIDEO_STREAM, "arnr_max_frames", "7"); -// w.SetOption(VIDEO_STREAM, "arnr_strength", "5"); -// w.SetOption(VIDEO_STREAM, "arnr_type", "3"); + w.SetOption(VIDEO_STREAM, "quality", "good"); + w.SetOption(VIDEO_STREAM, "g", "120"); + w.SetOption(VIDEO_STREAM, "qmin", "11"); + w.SetOption(VIDEO_STREAM, "qmax", "51"); + w.SetOption(VIDEO_STREAM, "profile", "0"); + w.SetOption(VIDEO_STREAM, "speed", "0"); + w.SetOption(VIDEO_STREAM, "level", "216"); + w.SetOption(VIDEO_STREAM, "rc_lookahead", "16"); + w.SetOption(VIDEO_STREAM, "rc_min_rate", "100000"); + w.SetOption(VIDEO_STREAM, "rc_max_rate", "24000000"); + w.SetOption(VIDEO_STREAM, "slices", "4"); + w.SetOption(VIDEO_STREAM, "arnr_max_frames", "7"); + w.SetOption(VIDEO_STREAM, "arnr_strength", "5"); + w.SetOption(VIDEO_STREAM, "arnr_type", "3"); // Write header w.WriteHeader(); @@ -67,11 +67,12 @@ int main() //Frame *f = r.GetFrame(1); - for (int frame = 300; frame <= 500; frame++) + for (int frame = 1; frame <= 500; frame++) { Frame *f = r.GetFrame(frame); - f->DisplayWaveform(false); + //if (f->number == 307 || f->number == 308 || f->number == 309 || f->number == 310) + // f->DisplayWaveform(false); // Apply effect //f->AddEffect("flip");