Skip to content

Commit 62d7fb5

Browse files
committed
Changing some libopenshot code based on Codacy feedback. Small refactoring, scope limiing.
1 parent 2834e77 commit 62d7fb5

File tree

2 files changed

+13
-31
lines changed

2 files changed

+13
-31
lines changed

include/FFmpegWriter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ namespace openshot {
253253

254254
/// @brief Constructor for FFmpegWriter. Throws one of the following exceptions.
255255
/// @param path The file path of the video file you want to open and read
256-
FFmpegWriter(std::string path);
256+
FFmpegWriter(const std::string path);
257257

258258
/// Close the writer
259259
void Close();

src/FFmpegWriter.cpp

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx, int6
8383
}
8484
#endif // HAVE_HW_ACCEL
8585

86-
FFmpegWriter::FFmpegWriter(std::string path) :
86+
FFmpegWriter::FFmpegWriter(const std::string path) :
8787
path(path), fmt(NULL), oc(NULL), audio_st(NULL), video_st(NULL), samples(NULL),
8888
audio_outbuf(NULL), audio_outbuf_size(0), audio_input_frame_size(0), audio_input_position(0),
8989
initial_audio_input_frame_size(0), img_convert_ctx(NULL), cache_size(8), num_of_rescalers(32),
@@ -864,9 +864,6 @@ void FFmpegWriter::flush_encoders() {
864864
return;
865865
#endif
866866

867-
int error_code = 0;
868-
int stop_encoding = 1;
869-
870867
// FLUSH VIDEO ENCODER
871868
if (info.has_video)
872869
for (;;) {
@@ -940,13 +937,9 @@ void FFmpegWriter::flush_encoders() {
940937
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + (std::string) av_err2str(error_code) + "]", "error_code", error_code);
941938
}
942939
if (!got_packet) {
943-
stop_encoding = 1;
944940
break;
945941
}
946942

947-
// Override PTS (in frames and scaled to the codec's timebase)
948-
//pkt.pts = write_video_count;
949-
950943
// set the timestamp
951944
if (pkt.pts != AV_NOPTS_VALUE)
952945
pkt.pts = av_rescale_q(pkt.pts, video_codec->time_base, video_st->time_base);
@@ -961,10 +954,6 @@ void FFmpegWriter::flush_encoders() {
961954
if (error_code < 0) {
962955
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + (std::string)av_err2str(error_code) + "]", "error_code", error_code);
963956
}
964-
965-
// Deallocate memory (if needed)
966-
if (video_outbuf)
967-
av_freep(&video_outbuf);
968957
}
969958

970959
// FLUSH AUDIO ENCODER
@@ -986,20 +975,15 @@ void FFmpegWriter::flush_encoders() {
986975
pkt.pts = pkt.dts = write_audio_count;
987976

988977
/* encode the image */
989-
int got_packet = 0;
978+
int error_code = 0;
990979
#if IS_FFMPEG_3_2
991-
avcodec_send_frame(audio_codec, NULL);
992-
got_packet = 0;
980+
error_code = avcodec_send_frame(audio_codec, NULL);
993981
#else
994982
error_code = avcodec_encode_audio2(audio_codec, &pkt, NULL, &got_packet);
995983
#endif
996984
if (error_code < 0) {
997985
ZmqLogger::Instance()->AppendDebugMethod("FFmpegWriter::flush_encoders ERROR [" + (std::string)av_err2str(error_code) + "]", "error_code", error_code);
998986
}
999-
if (!got_packet) {
1000-
stop_encoding = 1;
1001-
break;
1002-
}
1003987

1004988
// Since the PTS can change during encoding, set the value again. This seems like a huge hack,
1005989
// but it fixes lots of PTS related issues when I do this.
@@ -1583,29 +1567,27 @@ void FFmpegWriter::write_audio_packets(bool is_final) {
15831567
channels_in_frame = frame->GetAudioChannelsCount();
15841568
channel_layout_in_frame = frame->ChannelsLayout();
15851569

1586-
15871570
// Get audio sample array
15881571
float *frame_samples_float = NULL;
15891572
// Get samples interleaved together (c1 c2 c1 c2 c1 c2)
15901573
frame_samples_float = frame->GetInterleavedAudioSamples(sample_rate_in_frame, NULL, &samples_in_frame);
15911574

1592-
15931575
// Calculate total samples
15941576
total_frame_samples = samples_in_frame * channels_in_frame;
15951577

15961578
// Translate audio sample values back to 16 bit integers with saturation
1597-
float valF;
1598-
int16_t conv;
15991579
const int16_t max16 = 32767;
16001580
const int16_t min16 = -32768;
16011581
for (int s = 0; s < total_frame_samples; s++, frame_position++) {
1602-
valF = frame_samples_float[s] * (1 << 15);
1603-
if (valF > max16)
1582+
float valF = frame_samples_float[s] * (1 << 15);
1583+
int16_t conv;
1584+
if (valF > max16) {
16041585
conv = max16;
1605-
else if (valF < min16)
1586+
} else if (valF < min16) {
16061587
conv = min16;
1607-
else
1588+
} else {
16081589
conv = int(valF + 32768.5) - 32768; // +0.5 is for rounding
1590+
}
16091591

16101592
// Copy into buffer
16111593
all_queued_samples[frame_position] = conv;
@@ -1731,10 +1713,11 @@ void FFmpegWriter::write_audio_packets(bool is_final) {
17311713

17321714
// Determine how many samples we need
17331715
int diff = 0;
1734-
if (remaining_frame_samples >= remaining_packet_samples)
1716+
if (remaining_frame_samples >= remaining_packet_samples) {
17351717
diff = remaining_packet_samples;
1736-
else if (remaining_frame_samples < remaining_packet_samples)
1718+
} else {
17371719
diff = remaining_frame_samples;
1720+
}
17381721

17391722
// Copy frame samples into the packet samples array
17401723
if (!is_final)
@@ -1746,7 +1729,6 @@ void FFmpegWriter::write_audio_packets(bool is_final) {
17461729
audio_input_position += diff;
17471730
samples_position += diff * (av_get_bytes_per_sample(output_sample_fmt) / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16));
17481731
remaining_frame_samples -= diff;
1749-
remaining_packet_samples -= diff;
17501732

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

0 commit comments

Comments
 (0)