2013-09-12 23:41:49 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Unit tests for openshot::FFmpegReader
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* LICENSE
|
2013-09-12 23:41:49 -05:00
|
|
|
*
|
2019-06-11 06:48:32 -04:00
|
|
|
* Copyright (c) 2008-2019 OpenShot Studios, LLC
|
2014-03-29 18:49:22 -05:00
|
|
|
* <http://www.openshotstudios.com/>. This file is part of
|
|
|
|
|
* OpenShot Library (libopenshot), an open-source project dedicated to
|
|
|
|
|
* delivering high quality video editing and animation solutions to the
|
|
|
|
|
* world. For more information visit <http://www.openshot.org/>.
|
2013-09-12 23:41:49 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is free software: you can redistribute it
|
2014-07-11 16:52:14 -05:00
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* as published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
2013-09-12 23:41:49 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is distributed in the hope that it will be
|
|
|
|
|
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2014-07-11 16:52:14 -05:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-09-12 23:41:49 -05:00
|
|
|
*
|
2014-07-11 16:52:14 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
2013-09-12 23:41:49 -05:00
|
|
|
*/
|
|
|
|
|
|
2020-12-26 21:51:24 -05:00
|
|
|
#include <sstream>
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
#include <catch2/catch.hpp>
|
|
|
|
|
|
2020-12-26 21:51:24 -05:00
|
|
|
#include "FFmpegReader.h"
|
2021-01-26 10:52:04 -05:00
|
|
|
#include "Exceptions.h"
|
2020-12-26 21:51:24 -05:00
|
|
|
#include "Frame.h"
|
|
|
|
|
#include "Timeline.h"
|
|
|
|
|
#include "Json.h"
|
2012-07-09 01:22:11 -05:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Invalid_Path", "[libopenshot][ffmpegreader]" )
|
2012-07-09 01:22:11 -05:00
|
|
|
{
|
|
|
|
|
// Check invalid path
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK_THROWS_AS(FFmpegReader(""), InvalidFile);
|
2012-10-09 01:45:34 -05:00
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "GetFrame_Before_Opening", "[libopenshot][ffmpegreader]" )
|
2012-10-09 01:45:34 -05:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
2015-09-28 22:05:50 -05:00
|
|
|
stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "piano.wav";
|
|
|
|
|
FFmpegReader r(path.str());
|
2012-10-09 01:45:34 -05:00
|
|
|
|
|
|
|
|
// Check invalid path
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK_THROWS_AS(r.GetFrame(1), ReaderClosed);
|
2012-07-09 01:22:11 -05:00
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Check_Audio_File", "[libopenshot][ffmpegreader]" )
|
2012-07-09 01:22:11 -05:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
2015-09-28 22:05:50 -05:00
|
|
|
stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "piano.wav";
|
|
|
|
|
FFmpegReader r(path.str());
|
2012-10-09 01:45:34 -05:00
|
|
|
r.Open();
|
2012-07-09 01:22:11 -05:00
|
|
|
|
|
|
|
|
// Get frame 1
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> f = r.GetFrame(1);
|
2012-07-09 01:22:11 -05:00
|
|
|
|
|
|
|
|
// Get the number of channels and samples
|
2012-08-15 17:27:14 -05:00
|
|
|
float *samples = f->GetAudioSamples(0);
|
2012-07-09 01:22:11 -05:00
|
|
|
|
|
|
|
|
// Check audio properties
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->GetAudioChannelsCount() == 2);
|
|
|
|
|
CHECK(f->GetAudioSamplesCount() == 332);
|
2012-07-09 01:22:11 -05:00
|
|
|
|
|
|
|
|
// Check actual sample values (to be sure the waveform is correct)
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(samples[0] == Approx(0.0f).margin(0.00001));
|
|
|
|
|
CHECK(samples[50] == Approx(0.0f).margin(0.00001));
|
|
|
|
|
CHECK(samples[100] == Approx(0.0f).margin(0.00001));
|
|
|
|
|
CHECK(samples[200] == Approx(0.0f).margin(0.00001));
|
|
|
|
|
CHECK(samples[230] == Approx(0.16406f).margin(0.00001));
|
|
|
|
|
CHECK(samples[300] == Approx(-0.06250f).margin(0.00001));
|
2012-07-09 01:44:36 -05:00
|
|
|
|
|
|
|
|
// Close reader
|
|
|
|
|
r.Close();
|
2012-07-09 01:22:11 -05:00
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Check_Video_File", "[libopenshot][ffmpegreader]" )
|
2012-07-09 01:44:36 -05:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
2015-09-28 22:05:50 -05:00
|
|
|
stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "test.mp4";
|
|
|
|
|
FFmpegReader r(path.str());
|
2012-10-09 01:45:34 -05:00
|
|
|
r.Open();
|
2012-07-09 01:44:36 -05:00
|
|
|
|
|
|
|
|
// Get frame 1
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> f = r.GetFrame(1);
|
2012-07-09 01:44:36 -05:00
|
|
|
|
|
|
|
|
// Get the image data
|
2015-06-01 00:20:14 -07:00
|
|
|
const unsigned char* pixels = f->GetPixels(10);
|
|
|
|
|
int pixel_index = 112 * 4; // pixel 112 (4 bytes per pixel)
|
2012-07-09 01:44:36 -05:00
|
|
|
|
|
|
|
|
// Check image properties on scanline 10, pixel 112
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK((int)pixels[pixel_index] == Approx(21).margin(5));
|
|
|
|
|
CHECK((int)pixels[pixel_index + 1] == Approx(191).margin(5));
|
|
|
|
|
CHECK((int)pixels[pixel_index + 2] == Approx(0).margin(5));
|
|
|
|
|
CHECK((int)pixels[pixel_index + 3] == Approx(255).margin(5));
|
2012-07-09 01:44:36 -05:00
|
|
|
|
2019-05-31 19:02:28 -05:00
|
|
|
// Check pixel function
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->CheckPixel(10, 112, 21, 191, 0, 255, 5) == true);
|
|
|
|
|
CHECK_FALSE(f->CheckPixel(10, 112, 0, 0, 0, 0, 5));
|
2019-05-31 19:02:28 -05:00
|
|
|
|
2012-07-09 01:44:36 -05:00
|
|
|
// Get frame 1
|
|
|
|
|
f = r.GetFrame(2);
|
|
|
|
|
|
|
|
|
|
// Get the next frame
|
2012-08-15 17:27:14 -05:00
|
|
|
pixels = f->GetPixels(10);
|
2015-06-01 00:20:14 -07:00
|
|
|
pixel_index = 112 * 4; // pixel 112 (4 bytes per pixel)
|
2012-07-09 01:44:36 -05:00
|
|
|
|
|
|
|
|
// Check image properties on scanline 10, pixel 112
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK((int)pixels[pixel_index] == Approx(0).margin(5));
|
|
|
|
|
CHECK((int)pixels[pixel_index + 1] == Approx(96).margin(5));
|
|
|
|
|
CHECK((int)pixels[pixel_index + 2] == Approx(188).margin(5));
|
|
|
|
|
CHECK((int)pixels[pixel_index + 3] == Approx(255).margin(5));
|
2012-07-09 01:44:36 -05:00
|
|
|
|
2019-05-31 19:02:28 -05:00
|
|
|
// Check pixel function
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->CheckPixel(10, 112, 0, 96, 188, 255, 5) == true);
|
|
|
|
|
CHECK_FALSE(f->CheckPixel(10, 112, 0, 0, 0, 0, 5));
|
2019-05-31 19:02:28 -05:00
|
|
|
|
2014-04-03 22:35:25 -05:00
|
|
|
// Close reader
|
|
|
|
|
r.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Seek", "[libopenshot][ffmpegreader]" )
|
2014-04-03 22:35:25 -05:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
2015-09-28 22:05:50 -05:00
|
|
|
stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
|
|
|
|
|
FFmpegReader r(path.str());
|
2014-04-03 22:35:25 -05:00
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Get frame
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> f = r.GetFrame(1);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 1);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(300);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 300);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(301);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 301);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(315);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 315);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(275);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 275);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(270);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 270);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(500);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 500);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(100);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 100);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(600);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 600);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(1);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 1);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(700);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 700);
|
2014-04-03 22:35:25 -05:00
|
|
|
|
2012-07-09 01:44:36 -05:00
|
|
|
// Close reader
|
|
|
|
|
r.Close();
|
|
|
|
|
|
|
|
|
|
}
|
2014-04-03 22:35:25 -05:00
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Frame_Rate", "[libopenshot][ffmpegreader]" )
|
2020-03-26 20:12:10 -04:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
|
|
|
|
stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
|
|
|
|
|
FFmpegReader r(path.str());
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Verify detected frame rate
|
|
|
|
|
openshot::Fraction rate = r.info.fps;
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(rate.num == 24);
|
|
|
|
|
CHECK(rate.den == 1);
|
2020-03-26 20:12:10 -04:00
|
|
|
|
|
|
|
|
r.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Multiple_Open_and_Close", "[libopenshot][ffmpegreader]" )
|
2015-02-19 01:03:22 -06:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
2015-09-28 22:05:50 -05:00
|
|
|
stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
|
|
|
|
|
FFmpegReader r(path.str());
|
2015-02-19 01:03:22 -06:00
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Get frame that requires a seek
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> f = r.GetFrame(1200);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 1200);
|
2015-02-19 01:03:22 -06:00
|
|
|
|
|
|
|
|
// Close and Re-open the reader
|
|
|
|
|
r.Close();
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(1);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 1);
|
2015-02-19 01:03:22 -06:00
|
|
|
f = r.GetFrame(250);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 250);
|
2015-02-19 01:03:22 -06:00
|
|
|
|
|
|
|
|
// Close and Re-open the reader
|
|
|
|
|
r.Close();
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Get frame
|
|
|
|
|
f = r.GetFrame(750);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 750);
|
2015-02-19 01:03:22 -06:00
|
|
|
f = r.GetFrame(1000);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(f->number == 1000);
|
2015-02-19 01:03:22 -06:00
|
|
|
|
|
|
|
|
// Close reader
|
|
|
|
|
r.Close();
|
|
|
|
|
}
|
2020-03-26 20:12:10 -04:00
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "verify parent Timeline", "[libopenshot][ffmpegreader]" )
|
2020-10-23 01:35:46 -05:00
|
|
|
{
|
|
|
|
|
// Create a reader
|
|
|
|
|
stringstream path;
|
|
|
|
|
path << TEST_MEDIA_PATH << "sintel_trailer-720p.mp4";
|
|
|
|
|
FFmpegReader r(path.str());
|
|
|
|
|
r.Open();
|
|
|
|
|
|
|
|
|
|
// Check size of frame image
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(r.GetFrame(1)->GetImage()->width() == 1280);
|
|
|
|
|
CHECK(r.GetFrame(1)->GetImage()->height() == 720);
|
2020-10-23 01:35:46 -05:00
|
|
|
r.GetFrame(1)->GetImage()->save("reader-1.png", "PNG");
|
|
|
|
|
|
|
|
|
|
// Create a Clip associated with this reader
|
|
|
|
|
Clip c1(&r);
|
|
|
|
|
c1.Open();
|
|
|
|
|
|
|
|
|
|
// Check size of frame image (should still be the same)
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(r.GetFrame(1)->GetImage()->width() == 1280);
|
|
|
|
|
CHECK(r.GetFrame(1)->GetImage()->height() == 720);
|
2020-10-23 01:35:46 -05:00
|
|
|
|
|
|
|
|
// Create Timeline
|
|
|
|
|
Timeline t1(640, 480, Fraction(30,1), 44100, 2, LAYOUT_STEREO);
|
|
|
|
|
t1.AddClip(&c1);
|
|
|
|
|
|
|
|
|
|
// Check size of frame image (it should now match the parent timeline)
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(r.GetFrame(1)->GetImage()->width() == 640);
|
|
|
|
|
CHECK(r.GetFrame(1)->GetImage()->height() == 360);
|
2020-10-23 01:35:46 -05:00
|
|
|
}
|