Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Changing some libopenshot code based on Codacy feedback #526

Merged
merged 4 commits into from
Jun 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 11 additions & 25 deletions src/FFmpegWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,9 +864,6 @@ void FFmpegWriter::flush_encoders() {
return;
#endif

int error_code = 0;
int stop_encoding = 1;

// FLUSH VIDEO ENCODER
if (info.has_video)
for (;;) {
Expand Down Expand Up @@ -940,13 +937,9 @@ void FFmpegWriter::flush_encoders() {
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code);
}
if (!got_packet) {
stop_encoding = 1;
break;
}

// Override PTS (in frames and scaled to the codec's timebase)
//pkt.pts = write_video_count;

// set the timestamp
if (pkt.pts != AV_NOPTS_VALUE)
pkt.pts = av_rescale_q(pkt.pts, video_codec->time_base, video_st->time_base);
Expand All @@ -961,10 +954,6 @@ void FFmpegWriter::flush_encoders() {
if (error_code < 0) {
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + (std::string)av_err2str(error_code) + "]", "error_code", error_code);
}

// Deallocate memory (if needed)
if (video_outbuf)
av_freep(&video_outbuf);
}

// FLUSH AUDIO ENCODER
Expand All @@ -986,18 +975,17 @@ void FFmpegWriter::flush_encoders() {
pkt.pts = pkt.dts = write_audio_count;

/* encode the image */
int error_code = 0;
int got_packet = 0;
#if IS_FFMPEG_3_2
avcodec_send_frame(audio_codec, NULL);
got_packet = 0;
error_code = avcodec_send_frame(audio_codec, NULL);
#else
error_code = avcodec_encode_audio2(audio_codec, &pkt, NULL, &got_packet);
#endif
if (error_code < 0) {
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + (std::string)av_err2str(error_code) + "]", "error_code", error_code);
}
if (!got_packet) {
stop_encoding = 1;
break;
}

Expand Down Expand Up @@ -1583,29 +1571,27 @@ void FFmpegWriter::write_audio_packets(bool is_final) {
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(sample_rate_in_frame, NULL, &samples_in_frame);


// Calculate total samples
total_frame_samples = samples_in_frame * channels_in_frame;

// Translate audio sample values back to 16 bit integers with saturation
float valF;
int16_t conv;
const int16_t max16 = 32767;
const int16_t min16 = -32768;
for (int s = 0; s < total_frame_samples; s++, frame_position++) {
valF = frame_samples_float[s] * (1 << 15);
if (valF > max16)
float valF = frame_samples_float[s] * (1 << 15);
int16_t conv;
if (valF > max16) {
conv = max16;
else if (valF < min16)
} else if (valF < min16) {
conv = min16;
else
} else {
conv = int(valF + 32768.5) - 32768; // +0.5 is for rounding
}

// Copy into buffer
all_queued_samples[frame_position] = conv;
Expand Down Expand Up @@ -1731,10 +1717,11 @@ void FFmpegWriter::write_audio_packets(bool is_final) {

// Determine how many samples we need
int diff = 0;
if (remaining_frame_samples >= remaining_packet_samples)
if (remaining_frame_samples >= remaining_packet_samples) {
diff = remaining_packet_samples;
else if (remaining_frame_samples < remaining_packet_samples)
} else {
diff = remaining_frame_samples;
}

// Copy frame samples into the packet samples array
if (!is_final)
Expand All @@ -1746,7 +1733,6 @@ void FFmpegWriter::write_audio_packets(bool is_final) {
audio_input_position += diff;
samples_position += diff * (av_get_bytes_per_sample(output_sample_fmt) / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16));
remaining_frame_samples -= diff;
remaining_packet_samples -= diff;

// Do we have enough samples to proceed?
if (audio_input_position < (audio_input_frame_size * info.channels) && !is_final)
Expand Down