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

Improving Support for Newer FFMPEG versions #972

Merged
merged 3 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/FFmpegReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,16 @@
info.channels = AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channels;
info.channel_layout = (ChannelLayout) AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channel_layout;
#endif

// If channel layout is not set, guess based on the number of channels
if (info.channel_layout == 0) {
if (info.channels == 1) {
info.channel_layout = openshot::LAYOUT_MONO;
} else if (info.channels == 2) {
info.channel_layout = openshot::LAYOUT_STEREO;

Check warning on line 704 in src/FFmpegReader.cpp

View check run for this annotation

Codecov / codecov/patch

src/FFmpegReader.cpp#L701-L704

Added lines #L701 - L704 were not covered by tests
}
}

info.sample_rate = AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->sample_rate;
info.audio_bit_rate = AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->bit_rate;
if (info.audio_bit_rate <= 0) {
Expand Down
1 change: 0 additions & 1 deletion src/FFmpegWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2233,7 +2233,6 @@ bool FFmpegWriter::write_video_packet(std::shared_ptr<Frame> frame, AVFrame *fra
ret = avcodec_receive_packet(video_codec_ctx, pkt);

if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
avcodec_flush_buffers(video_codec_ctx);
got_packet_ptr = 0;
break;
}
Expand Down
128 changes: 50 additions & 78 deletions tests/Caption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,36 @@
#include "Timeline.h"


TEST_CASE( "caption effect", "[libopenshot][caption]" )
{
// Function to check for non-black pixels in a given region of the frame
bool HasNonBlackPixelsInRegion(const std::shared_ptr<openshot::Frame>& frame, int start_row, int end_row, int start_col, int end_col) {
int frame_width = frame->GetWidth();
int frame_height = frame->GetHeight();

Check warning on line 27 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L25-L27

Added lines #L25 - L27 were not covered by tests

// Ensure the search region is within the frame bounds
if (start_row < 0 || end_row >= frame_height || start_col < 0 || end_col >= frame_width) {
throw std::out_of_range("Search region is out of frame bounds");

Check warning on line 31 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L30-L31

Added lines #L30 - L31 were not covered by tests
}

for (int row = start_row; row <= end_row; ++row) {
const unsigned char* pixels = frame->GetPixels(row);
if (!pixels) {
throw std::runtime_error("Failed to get pixels for the row");

Check warning on line 37 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L34-L37

Added lines #L34 - L37 were not covered by tests
}

for (int col = start_col; col <= end_col; ++col) {
int index = col * 4;
int R = pixels[index];
int G = pixels[index + 1];
int B = pixels[index + 2];
if (!(R == 0 && G == 0 && B == 0)) {
return true; // Non-black pixel found

Check warning on line 46 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L40-L46

Added lines #L40 - L46 were not covered by tests
}
}
}
return false; // No non-black pixel found

Check warning on line 50 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L50

Added line #L50 was not covered by tests
}

TEST_CASE("caption effect", "[libopenshot][caption]") {
// Check for QT Platform Environment variable - and ignore these tests if it's set to offscreen
if (std::getenv("QT_QPA_PLATFORM") != nullptr) {
std::string qt_platform_env = std::getenv("QT_QPA_PLATFORM");
Expand All @@ -32,14 +60,12 @@
}
}

int argc;
char* argv[2];
int argc = 1;
char* argv[1] = {(char*)""};

Check warning on line 64 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L63-L64

Added lines #L63 - L64 were not covered by tests
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QApplication::processEvents();

int check_row = 0;
int check_col = 0;
QApplication::processEvents();

Check warning on line 68 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L68

Added line #L68 was not covered by tests

SECTION("default constructor") {

Expand Down Expand Up @@ -76,23 +102,12 @@
// Get frame
std::shared_ptr<openshot::Frame> f = clip1.GetFrame(10);

#ifdef _WIN32
// Windows pixel location
check_col = 625;
check_row = 600;
#else
// Linux/Mac pixel location
check_col = 251;
check_row = 572;
#endif

// Verify pixel values (black background pixels)
const unsigned char *pixels = f->GetPixels(1);
CHECK((int) pixels[0 * 4] == 0);
const unsigned char* pixels = f->GetPixels(1);
CHECK((int)pixels[0 * 4] == 0);

Check warning on line 107 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L106-L107

Added lines #L106 - L107 were not covered by tests

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

Check warning on line 110 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L110

Added line #L110 was not covered by tests

// Create Timeline
openshot::Timeline t(1280, 720, openshot::Fraction(24, 1), 44100, 2, openshot::LAYOUT_STEREO);
Expand All @@ -101,23 +116,12 @@
// Get timeline frame
f = t.GetFrame(10);

#ifdef _WIN32
// Windows pixel location
check_col = 625;
check_row = 600;
#else
// Linux/Mac pixel location
check_col = 251;
check_row = 572;
#endif

// Verify pixel values (black background pixels)
pixels = f->GetPixels(1);
CHECK((int) pixels[0 * 4] == 0);
CHECK((int)pixels[0 * 4] == 0);

Check warning on line 121 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L121

Added line #L121 was not covered by tests

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

Check warning on line 124 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L124

Added line #L124 was not covered by tests

// Close objects
t.Close();
Expand All @@ -139,24 +143,14 @@

// Get frame
std::shared_ptr<openshot::Frame> f = clip1.GetFrame(10);

#ifdef _WIN32
// Windows pixel location
check_col = 351;
check_row = 391;
#else
// Linux/Mac pixel location
check_col = 141;
check_row = 378;
#endif
f->Save("/home/jonathan/test.png", 1.0, "PNG", 100);

Check warning on line 146 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L146

Added line #L146 was not covered by tests

// Verify pixel values (black background pixels)
const unsigned char *pixels = f->GetPixels(1);
CHECK((int) pixels[0 * 4] == 0);
const unsigned char* pixels = f->GetPixels(1);
CHECK((int)pixels[0 * 4] == 0);

Check warning on line 150 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L149-L150

Added lines #L149 - L150 were not covered by tests

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

Check warning on line 153 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L153

Added line #L153 was not covered by tests

// Create Timeline
openshot::Timeline t(720, 480, openshot::Fraction(24, 1), 44100, 2, openshot::LAYOUT_STEREO);
Expand All @@ -165,23 +159,12 @@
// Get timeline frame
f = t.GetFrame(10);

#ifdef _WIN32
// Windows pixel location
check_col = 351;
check_row = 391;
#else
// Linux/Mac pixel location
check_col = 141;
check_row = 377;
#endif

// Verify pixel values (black background pixels)
pixels = f->GetPixels(1);
CHECK((int) pixels[0 * 4] == 0);
CHECK((int)pixels[0 * 4] == 0);

Check warning on line 164 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L164

Added line #L164 was not covered by tests

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

Check warning on line 167 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L167

Added line #L167 was not covered by tests

// Close objects
t.Close();
Expand All @@ -205,23 +188,12 @@
// Get frame
std::shared_ptr<openshot::Frame> f = clip1.GetFrame(11);

#ifdef _WIN32
// Windows pixel location
check_col = 633;
check_row = 580;
#else
// Linux/Mac pixel location
check_col = 284;
check_row = 569;
#endif

// Verify pixel values (black background pixels)
const unsigned char *pixels = f->GetPixels(1);
CHECK((int) pixels[0 * 4] == 0);

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

Check warning on line 196 in tests/Caption.cpp

View check run for this annotation

Codecov / codecov/patch

tests/Caption.cpp#L196

Added line #L196 was not covered by tests

// Close objects
clip1.Close();
Expand Down
Loading