Skip to content

Commit d906231

Browse files
authored
Merge pull request #972 from OpenShot/better-ffmpeg-support
Improving Support for Newer FFMPEG versions
2 parents 044bb22 + 5be0f4a commit d906231

File tree

3 files changed

+60
-79
lines changed

3 files changed

+60
-79
lines changed

src/FFmpegReader.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,16 @@ void FFmpegReader::UpdateAudioInfo() {
695695
info.channels = AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channels;
696696
info.channel_layout = (ChannelLayout) AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channel_layout;
697697
#endif
698+
699+
// If channel layout is not set, guess based on the number of channels
700+
if (info.channel_layout == 0) {
701+
if (info.channels == 1) {
702+
info.channel_layout = openshot::LAYOUT_MONO;
703+
} else if (info.channels == 2) {
704+
info.channel_layout = openshot::LAYOUT_STEREO;
705+
}
706+
}
707+
698708
info.sample_rate = AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->sample_rate;
699709
info.audio_bit_rate = AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->bit_rate;
700710
if (info.audio_bit_rate <= 0) {

src/FFmpegWriter.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -2233,7 +2233,6 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr<Frame> frame, AVFrame *fra
22332233
ret = avcodec_receive_packet(video_codec_ctx, pkt);
22342234

22352235
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
2236-
avcodec_flush_buffers(video_codec_ctx);
22372236
got_packet_ptr = 0;
22382237
break;
22392238
}

tests/Caption.cpp

+50-78
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,36 @@
2121
#include "Timeline.h"
2222

2323

24-
TEST_CASE( "caption effect", "[libopenshot][caption]" )
25-
{
24+
// Function to check for non-black pixels in a given region of the frame
25+
bool HasNonBlackPixelsInRegion(const std::shared_ptr<openshot::Frame>& frame, int start_row, int end_row, int start_col, int end_col) {
26+
int frame_width = frame->GetWidth();
27+
int frame_height = frame->GetHeight();
28+
29+
// Ensure the search region is within the frame bounds
30+
if (start_row < 0 || end_row >= frame_height || start_col < 0 || end_col >= frame_width) {
31+
throw std::out_of_range("Search region is out of frame bounds");
32+
}
33+
34+
for (int row = start_row; row <= end_row; ++row) {
35+
const unsigned char* pixels = frame->GetPixels(row);
36+
if (!pixels) {
37+
throw std::runtime_error("Failed to get pixels for the row");
38+
}
39+
40+
for (int col = start_col; col <= end_col; ++col) {
41+
int index = col * 4;
42+
int R = pixels[index];
43+
int G = pixels[index + 1];
44+
int B = pixels[index + 2];
45+
if (!(R == 0 && G == 0 && B == 0)) {
46+
return true; // Non-black pixel found
47+
}
48+
}
49+
}
50+
return false; // No non-black pixel found
51+
}
52+
53+
TEST_CASE("caption effect", "[libopenshot][caption]") {
2654
// Check for QT Platform Environment variable - and ignore these tests if it's set to offscreen
2755
if (std::getenv("QT_QPA_PLATFORM") != nullptr) {
2856
std::string qt_platform_env = std::getenv("QT_QPA_PLATFORM");
@@ -32,14 +60,12 @@ TEST_CASE( "caption effect", "[libopenshot][caption]" )
3260
}
3361
}
3462

35-
int argc;
36-
char* argv[2];
63+
int argc = 1;
64+
char* argv[1] = {(char*)""};
3765
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
3866
QApplication app(argc, argv);
39-
QApplication::processEvents();
4067

41-
int check_row = 0;
42-
int check_col = 0;
68+
QApplication::processEvents();
4369

4470
SECTION("default constructor") {
4571

@@ -76,23 +102,12 @@ TEST_CASE( "caption effect", "[libopenshot][caption]" )
76102
// Get frame
77103
std::shared_ptr<openshot::Frame> f = clip1.GetFrame(10);
78104

79-
#ifdef _WIN32
80-
// Windows pixel location
81-
check_col = 625;
82-
check_row = 600;
83-
#else
84-
// Linux/Mac pixel location
85-
check_col = 251;
86-
check_row = 572;
87-
#endif
88-
89105
// Verify pixel values (black background pixels)
90-
const unsigned char *pixels = f->GetPixels(1);
91-
CHECK((int) pixels[0 * 4] == 0);
106+
const unsigned char* pixels = f->GetPixels(1);
107+
CHECK((int)pixels[0 * 4] == 0);
92108

93-
// Verify pixel values (white text pixels)
94-
pixels = f->GetPixels(check_row);
95-
CHECK((int) pixels[check_col * 4] == 255);
109+
// Check for non-black pixels in the region for white text
110+
CHECK(HasNonBlackPixelsInRegion(f, 560, 700, 200, 600));
96111

97112
// Create Timeline
98113
openshot::Timeline t(1280, 720, openshot::Fraction(24, 1), 44100, 2, openshot::LAYOUT_STEREO);
@@ -101,23 +116,12 @@ TEST_CASE( "caption effect", "[libopenshot][caption]" )
101116
// Get timeline frame
102117
f = t.GetFrame(10);
103118

104-
#ifdef _WIN32
105-
// Windows pixel location
106-
check_col = 625;
107-
check_row = 600;
108-
#else
109-
// Linux/Mac pixel location
110-
check_col = 251;
111-
check_row = 572;
112-
#endif
113-
114119
// Verify pixel values (black background pixels)
115120
pixels = f->GetPixels(1);
116-
CHECK((int) pixels[0 * 4] == 0);
121+
CHECK((int)pixels[0 * 4] == 0);
117122

118-
// Verify pixel values (white text pixels)
119-
pixels = f->GetPixels(check_row);
120-
CHECK((int) pixels[check_col * 4] == 255);
123+
// Check for non-black pixels in the region for white text
124+
CHECK(HasNonBlackPixelsInRegion(f, 560, 700, 200, 600));
121125

122126
// Close objects
123127
t.Close();
@@ -139,24 +143,14 @@ TEST_CASE( "caption effect", "[libopenshot][caption]" )
139143

140144
// Get frame
141145
std::shared_ptr<openshot::Frame> f = clip1.GetFrame(10);
142-
143-
#ifdef _WIN32
144-
// Windows pixel location
145-
check_col = 351;
146-
check_row = 391;
147-
#else
148-
// Linux/Mac pixel location
149-
check_col = 141;
150-
check_row = 378;
151-
#endif
146+
f->Save("/home/jonathan/test.png", 1.0, "PNG", 100);
152147

153148
// Verify pixel values (black background pixels)
154-
const unsigned char *pixels = f->GetPixels(1);
155-
CHECK((int) pixels[0 * 4] == 0);
149+
const unsigned char* pixels = f->GetPixels(1);
150+
CHECK((int)pixels[0 * 4] == 0);
156151

157-
// Verify pixel values (white text pixels)
158-
pixels = f->GetPixels(check_row);
159-
CHECK((int) pixels[check_col * 4] == 255);
152+
// Check for non-black pixels in the region for white text
153+
CHECK(HasNonBlackPixelsInRegion(f, 350, 479, 150, 500));
160154

161155
// Create Timeline
162156
openshot::Timeline t(720, 480, openshot::Fraction(24, 1), 44100, 2, openshot::LAYOUT_STEREO);
@@ -165,23 +159,12 @@ TEST_CASE( "caption effect", "[libopenshot][caption]" )
165159
// Get timeline frame
166160
f = t.GetFrame(10);
167161

168-
#ifdef _WIN32
169-
// Windows pixel location
170-
check_col = 351;
171-
check_row = 391;
172-
#else
173-
// Linux/Mac pixel location
174-
check_col = 141;
175-
check_row = 377;
176-
#endif
177-
178162
// Verify pixel values (black background pixels)
179163
pixels = f->GetPixels(1);
180-
CHECK((int) pixels[0 * 4] == 0);
164+
CHECK((int)pixels[0 * 4] == 0);
181165

182-
// Verify pixel values (white text pixels)
183-
pixels = f->GetPixels(check_row);
184-
CHECK((int) pixels[check_col * 4] == 255);
166+
// Check for non-black pixels in the region for white text
167+
CHECK(HasNonBlackPixelsInRegion(f, 200, 479, 200, 600));
185168

186169
// Close objects
187170
t.Close();
@@ -205,23 +188,12 @@ TEST_CASE( "caption effect", "[libopenshot][caption]" )
205188
// Get frame
206189
std::shared_ptr<openshot::Frame> f = clip1.GetFrame(11);
207190

208-
#ifdef _WIN32
209-
// Windows pixel location
210-
check_col = 633;
211-
check_row = 580;
212-
#else
213-
// Linux/Mac pixel location
214-
check_col = 284;
215-
check_row = 569;
216-
#endif
217-
218191
// Verify pixel values (black background pixels)
219192
const unsigned char *pixels = f->GetPixels(1);
220193
CHECK((int) pixels[0 * 4] == 0);
221194

222-
// Verify pixel values (white text pixels)
223-
pixels = f->GetPixels(check_row);
224-
CHECK((int) pixels[check_col * 4] == 255);
195+
// Check for non-black pixels in the region for white text
196+
CHECK(HasNonBlackPixelsInRegion(f, 560, 700, 200, 600));
225197

226198
// Close objects
227199
clip1.Close();

0 commit comments

Comments
 (0)