You've already forked libopenshot
mirror of
https://github.com/OpenShot/libopenshot.git
synced 2026-03-02 08:53:52 -08:00
Moved audio decoding outside of OMP task, and fixed all audio pops and crackles!
This commit is contained in:
@@ -647,48 +647,47 @@ void FFmpegReader::ProcessAudioPacket(int requested_frame, int target_frame, int
|
||||
#pragma omp critical (processing_list)
|
||||
processing_audio_frames[target_frame] = target_frame;
|
||||
|
||||
#pragma omp task firstprivate(requested_frame, target_frame, my_cache, my_packet, starting_sample)
|
||||
|
||||
|
||||
// Allocate audio buffer
|
||||
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)
|
||||
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) {
|
||||
// re-initialize buffer size (it gets changed in the avcodec_decode_audio2 method call)
|
||||
int buf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE;
|
||||
int used = avcodec_decode_audio3(aCodecCtx, audio_buf, &buf_size, my_packet);
|
||||
|
||||
if (used < 0) {
|
||||
// Throw exception
|
||||
my_packet->size = 0;
|
||||
throw ErrorDecodingAudio("Error decoding audio samples", target_frame);
|
||||
}
|
||||
|
||||
// Calculate total number of samples
|
||||
packet_samples += (buf_size / av_get_bytes_per_sample(aCodecCtx->sample_fmt));
|
||||
|
||||
// process samples...
|
||||
my_packet->data += used;
|
||||
my_packet->size -= used;
|
||||
}
|
||||
|
||||
#pragma omp critical (packet_cache)
|
||||
{
|
||||
// Allocate audio buffer
|
||||
int16_t *audio_buf = new int16_t[AVCODEC_MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
|
||||
// Remove packet
|
||||
av_init_packet(my_packet); // TODO: this is a hack, to prevent a bug calling av_free_packet after avcodec_decode_audio3()
|
||||
RemoveAVPacket(my_packet);
|
||||
}
|
||||
|
||||
// create a new array (to hold the re-sampled audio)
|
||||
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) {
|
||||
// re-initialize buffer size (it gets changed in the avcodec_decode_audio2 method call)
|
||||
int buf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE;
|
||||
|
||||
int used = -1;
|
||||
#pragma omp critical (audio_codec)
|
||||
used = avcodec_decode_audio3(aCodecCtx, audio_buf, &buf_size, my_packet);
|
||||
|
||||
if (used < 0) {
|
||||
// Throw exception
|
||||
throw ErrorDecodingAudio("Error decoding audio samples", target_frame);
|
||||
my_packet->size = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// Calculate total number of samples
|
||||
packet_samples += (buf_size / av_get_bytes_per_sample(aCodecCtx->sample_fmt));
|
||||
|
||||
// process samples...
|
||||
my_packet->data += used;
|
||||
my_packet->size -= used;
|
||||
}
|
||||
|
||||
#pragma omp critical (packet_cache)
|
||||
{
|
||||
// Remove packet
|
||||
av_init_packet(my_packet); // TODO: this is a hack, to prevent a bug calling av_free_packet after avcodec_decode_audio3()
|
||||
RemoveAVPacket(my_packet);
|
||||
}
|
||||
|
||||
#pragma omp task firstprivate(requested_frame, target_frame, my_cache, starting_sample, audio_buf, converted_audio)
|
||||
{
|
||||
// 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(
|
||||
@@ -715,12 +714,6 @@ 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++)
|
||||
{
|
||||
@@ -822,7 +815,8 @@ void FFmpegReader::ProcessAudioPacket(int requested_frame, int target_frame, int
|
||||
for (int f = target_frame; f < starting_frame_number; f++)
|
||||
processing_audio_frames.erase(f);
|
||||
}
|
||||
}
|
||||
|
||||
} // end task
|
||||
|
||||
}
|
||||
|
||||
|
||||
40
src/Main.cpp
40
src/Main.cpp
@@ -20,9 +20,9 @@ int main()
|
||||
// openshot::FFmpegReader r("../../src/examples/piano.wav");
|
||||
// openshot::FFmpegReader r("/home/jonathan/Videos/big-buck-bunny_trailer.webm");
|
||||
|
||||
openshot::FFmpegReader r("/home/jonathan/Videos/sintel-1024-stereo.mp4");
|
||||
// openshot::FFmpegReader r("/home/jonathan/Videos/00001.mts");
|
||||
// openshot::FFmpegReader r("/home/jonathan/Videos/sintel_trailer-720p.mp4");
|
||||
// openshot::FFmpegReader r("/home/jonathan/Videos/sintel-1024-stereo.mp4");
|
||||
// openshot::FFmpegReader r("/home/jonathan/Videos/OpenShot_Now_In_3d.mp4");
|
||||
openshot::FFmpegReader r("/home/jonathan/Videos/sintel_trailer-720p.mp4");
|
||||
// openshot::FFmpegReader r("/home/jonathan/Aptana Studio Workspace/OpenShotLibrary/src/examples/piano.wav");
|
||||
// openshot::FFmpegReader r("/home/jonathan/Music/Army of Lovers/Crucified/Army of Lovers - Crucified [Single Version].mp3");
|
||||
// openshot::FFmpegReader r("/home/jonathan/Documents/OpenShot Art/test.jpeg");
|
||||
@@ -33,31 +33,31 @@ int main()
|
||||
r.DisplayInfo();
|
||||
|
||||
// Create a writer
|
||||
FFmpegWriter w("/home/jonathan/output.webm");
|
||||
FFmpegWriter w("/home/jonathan/output.mp3");
|
||||
w.DisplayInfo();
|
||||
|
||||
// Set options
|
||||
w.SetAudioOptions(true, "libvorbis", 44100, 2, 128000);
|
||||
w.SetVideoOptions(true, "libvpx", Fraction(24, 1), 640, 360, Fraction(1,1), false, false, 2000000);
|
||||
w.SetAudioOptions(true, "libmp3lame", 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();
|
||||
|
||||
Reference in New Issue
Block a user