Skip to content

Commit 13bd272

Browse files
committed
Adding new settings class to be used for changing realtime settings used by libopenshot, such as scaling mode for preview vs final render, or hardware decode, etc...
1 parent e0ec603 commit 13bd272

File tree

14 files changed

+246
-37
lines changed

14 files changed

+246
-37
lines changed

include/FFmpegReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "CacheMemory.h"
4545
#include "Exceptions.h"
4646
#include "OpenMPUtilities.h"
47+
#include "Settings.h"
4748

4849

4950
using namespace std;
@@ -105,7 +106,6 @@ namespace openshot
105106
bool check_interlace;
106107
bool check_fps;
107108
bool has_missing_frames;
108-
bool use_omp_threads;
109109

110110
CacheMemory working_cache;
111111
CacheMemory missing_frames;

include/FFmpegWriter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "Exceptions.h"
5252
#include "OpenMPUtilities.h"
5353
#include "ZmqLogger.h"
54+
#include "Settings.h"
5455

5556

5657
using namespace std;

include/OpenMPUtilities.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,6 @@
3737
#define OPEN_MP_NUM_PROCESSORS (min(omp_get_num_procs(), 6))
3838
#define FF_NUM_PROCESSORS (min(omp_get_num_procs(), 12))
3939

40-
using namespace std;
4140

42-
namespace openshot {
43-
44-
// Check if OS2_OMP_THREADS environment variable is present, and return
45-
// if multiple threads should be used with OMP
46-
static bool IsOMPEnabled() {
47-
char* OS2_OMP_THREADS = getenv("OS2_OMP_THREADS");
48-
if (OS2_OMP_THREADS != NULL && strcmp(OS2_OMP_THREADS, "0") == 0)
49-
return false;
50-
else
51-
return true;
52-
}
53-
54-
}
5541

5642
#endif

include/OpenShot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,6 @@
134134
#include "Profiles.h"
135135
#include "QtImageReader.h"
136136
#include "Timeline.h"
137+
#include "Settings.h"
137138

138139
#endif

include/Settings.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @file
3+
* @brief Header file for global Settings class
4+
* @author Jonathan Thomas <[email protected]>
5+
*
6+
* @section LICENSE
7+
*
8+
* Copyright (c) 2008-2014 OpenShot Studios, LLC
9+
* <http://www.openshotstudios.com/>. This file is part of
10+
* OpenShot Library (libopenshot), an open-source project dedicated to
11+
* delivering high quality video editing and animation solutions to the
12+
* world. For more information visit <http://www.openshot.org/>.
13+
*
14+
* OpenShot Library (libopenshot) is free software: you can redistribute it
15+
* and/or modify it under the terms of the GNU Lesser General Public License
16+
* as published by the Free Software Foundation, either version 3 of the
17+
* License, or (at your option) any later version.
18+
*
19+
* OpenShot Library (libopenshot) is distributed in the hope that it will be
20+
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
* GNU Lesser General Public License for more details.
23+
*
24+
* You should have received a copy of the GNU Lesser General Public License
25+
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26+
*/
27+
28+
#ifndef OPENSHOT_SETTINGS_H
29+
#define OPENSHOT_SETTINGS_H
30+
31+
32+
#include "JuceLibraryCode/JuceHeader.h"
33+
#include <iostream>
34+
#include <iomanip>
35+
#include <fstream>
36+
#include <stdlib.h>
37+
#include <string>
38+
#include <sstream>
39+
#include <stdio.h>
40+
#include <time.h>
41+
#include <zmq.hpp>
42+
#include <unistd.h>
43+
44+
45+
using namespace std;
46+
47+
namespace openshot {
48+
49+
/**
50+
* @brief This class is contains settings used by libopenshot (and can be safely toggled at any point)
51+
*
52+
* Settings class is used primarily to toggle scale settings between preview and rendering, and adjust
53+
* other runtime related settings.
54+
*/
55+
class Settings {
56+
private:
57+
58+
/// Default constructor
59+
Settings(){}; // Don't allow user to create an instance of this singleton
60+
61+
#if __GNUC__ >=7
62+
/// Default copy method
63+
Settings(Settings const&) = delete; // Don't allow the user to assign this instance
64+
65+
/// Default assignment operator
66+
Settings & operator=(Settings const&) = delete; // Don't allow the user to assign this instance
67+
#else
68+
/// Default copy method
69+
Settings(Settings const&) {}; // Don't allow the user to assign this instance
70+
71+
/// Default assignment operator
72+
Settings & operator=(Settings const&); // Don't allow the user to assign this instance
73+
#endif
74+
75+
/// Private variable to keep track of singleton instance
76+
static Settings * m_pInstance;
77+
78+
public:
79+
/// Use video card for faster video decoding (if supported)
80+
bool HARDWARE_DECODE = false;
81+
82+
/// Use video card for faster video encoding (if supported)
83+
bool HARDWARE_ENCODE = false;
84+
85+
/// Scale mode used in FFmpeg decoding and encoding (used as an optimization for faster previews)
86+
bool HIGH_QUALITY_SCALING = false;
87+
88+
/// Wait for OpenMP task to finish before continuing (used to limit threads on slower systems)
89+
bool WAIT_FOR_VIDEO_PROCESSING_TASK = false;
90+
91+
/// Create or get an instance of this logger singleton (invoke the class with this method)
92+
static Settings * Instance();
93+
};
94+
95+
}
96+
97+
#endif

include/ZmqLogger.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ using namespace std;
4747
namespace openshot {
4848

4949
/**
50-
* @brief This abstract class is the base class, used by all readers in libopenshot.
50+
* @brief This class is used for logging and sending those logs over a ZemoMQ socket to a listener
5151
*
52-
* Readers are types of classes that read video, audio, and image files, and
53-
* return openshot::Frame objects. The only requirements for a 'reader', are to
54-
* derive from this base class, implement the GetFrame method, and call the InitFileInfo() method.
52+
* OpenShot desktop editor listens to this port, to receive libopenshot debug output. It both logs to
53+
* a file and sends the stdout over a socket.
5554
*/
5655
class ZmqLogger {
5756
private:

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ SET ( OPENSHOT_SOURCE_FILES
244244
Profiles.cpp
245245
QtImageReader.cpp
246246
QtPlayer.cpp
247+
Settings.cpp
247248
Timeline.cpp
248249

249250
# Qt Video Player

src/FFmpegReader.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ FFmpegReader::FFmpegReader(string path)
3838
check_fps(false), enable_seek(true), is_open(false), seek_audio_frame_found(0), seek_video_frame_found(0),
3939
prev_samples(0), prev_pts(0), pts_total(0), pts_counter(0), is_duration_known(false), largest_frame_processed(0),
4040
current_video_frame(0), has_missing_frames(false), num_packets_since_video_frame(0), num_checks_since_final(0),
41-
packet(NULL), use_omp_threads(true) {
41+
packet(NULL) {
4242

4343
// Initialize FFMpeg, and register all formats and codecs
4444
AV_REGISTER_ALL
@@ -60,7 +60,7 @@ FFmpegReader::FFmpegReader(string path, bool inspect_reader)
6060
check_fps(false), enable_seek(true), is_open(false), seek_audio_frame_found(0), seek_video_frame_found(0),
6161
prev_samples(0), prev_pts(0), pts_total(0), pts_counter(0), is_duration_known(false), largest_frame_processed(0),
6262
current_video_frame(0), has_missing_frames(false), num_packets_since_video_frame(0), num_checks_since_final(0),
63-
packet(NULL), use_omp_threads(true) {
63+
packet(NULL) {
6464

6565
// Initialize FFMpeg, and register all formats and codecs
6666
AV_REGISTER_ALL
@@ -229,9 +229,6 @@ void FFmpegReader::Open()
229229
missing_frames.SetMaxBytesFromInfo(OPEN_MP_NUM_PROCESSORS * 2, info.width, info.height, info.sample_rate, info.channels);
230230
final_cache.SetMaxBytesFromInfo(OPEN_MP_NUM_PROCESSORS * 2, info.width, info.height, info.sample_rate, info.channels);
231231

232-
// Initialize OMP threading support
233-
use_omp_threads = openshot::IsOMPEnabled();
234-
235232
// Mark as "open"
236233
is_open = true;
237234
}
@@ -613,7 +610,7 @@ std::shared_ptr<Frame> FFmpegReader::ReadStream(int64_t requested_frame)
613610
// Process Video Packet
614611
ProcessVideoPacket(requested_frame);
615612

616-
if (!use_omp_threads) {
613+
if (openshot::Settings::Instance()->WAIT_FOR_VIDEO_PROCESSING_TASK) {
617614
// Wait on each OMP task to complete before moving on to the next one. This slows
618615
// down processing considerably, but might be more stable on some systems.
619616
#pragma omp taskwait
@@ -628,16 +625,16 @@ std::shared_ptr<Frame> FFmpegReader::ReadStream(int64_t requested_frame)
628625
num_packets_since_video_frame++;
629626

630627
// Check the status of a seek (if any)
631-
if (is_seeking)
632-
#pragma omp critical (openshot_seek)
633-
check_seek = CheckSeek(false);
634-
else
635-
check_seek = false;
636-
637-
if (check_seek) {
638-
// Jump to the next iteration of this loop
639-
continue;
640-
}
628+
if (is_seeking)
629+
#pragma omp critical (openshot_seek)
630+
check_seek = CheckSeek(false);
631+
else
632+
check_seek = false;
633+
634+
if (check_seek) {
635+
// Jump to the next iteration of this loop
636+
continue;
637+
}
641638

642639
// Update PTS / Frame Offset (if any)
643640
UpdatePTSOffset(false);
@@ -919,8 +916,12 @@ void FFmpegReader::ProcessVideoPacket(int64_t requested_frame)
919916
// Copy picture data from one AVFrame (or AVPicture) to another one.
920917
AV_COPY_PICTURE_DATA(pFrameRGB, buffer, PIX_FMT_RGBA, width, height);
921918

919+
int scale_mode = SWS_FAST_BILINEAR;
920+
if (openshot::Settings::Instance()->HIGH_QUALITY_SCALING) {
921+
scale_mode = SWS_LANCZOS;
922+
}
922923
SwsContext *img_convert_ctx = sws_getContext(info.width, info.height, AV_GET_CODEC_PIXEL_FORMAT(pStream, pCodecCtx), width,
923-
height, PIX_FMT_RGBA, SWS_LANCZOS, NULL, NULL, NULL);
924+
height, PIX_FMT_RGBA, scale_mode, NULL, NULL, NULL);
924925

925926
// Resize / Convert to RGB
926927
sws_scale(img_convert_ctx, my_frame->data, my_frame->linesize, 0,

src/FFmpegWriter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1748,11 +1748,16 @@ void FFmpegWriter::OutputStreamInfo()
17481748
// Init a collection of software rescalers (thread safe)
17491749
void FFmpegWriter::InitScalers(int source_width, int source_height)
17501750
{
1751+
int scale_mode = SWS_FAST_BILINEAR;
1752+
if (openshot::Settings::Instance()->HIGH_QUALITY_SCALING) {
1753+
scale_mode = SWS_LANCZOS;
1754+
}
1755+
17511756
// Init software rescalers vector (many of them, one for each thread)
17521757
for (int x = 0; x < num_of_rescalers; x++)
17531758
{
17541759
// Init the software scaler from FFMpeg
1755-
img_convert_ctx = sws_getContext(source_width, source_height, PIX_FMT_RGBA, info.width, info.height, AV_GET_CODEC_PIXEL_FORMAT(video_st, video_st->codec), SWS_LANCZOS, NULL, NULL, NULL);
1760+
img_convert_ctx = sws_getContext(source_width, source_height, PIX_FMT_RGBA, info.width, info.height, AV_GET_CODEC_PIXEL_FORMAT(video_st, video_st->codec), scale_mode, NULL, NULL, NULL);
17561761

17571762
// Add rescaler to vector
17581763
image_rescalers.push_back(img_convert_ctx);

src/Settings.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @file
3+
* @brief Source file for global Settings class
4+
* @author Jonathan Thomas <[email protected]>
5+
*
6+
* @section LICENSE
7+
*
8+
* Copyright (c) 2008-2014 OpenShot Studios, LLC
9+
* <http://www.openshotstudios.com/>. This file is part of
10+
* OpenShot Library (libopenshot), an open-source project dedicated to
11+
* delivering high quality video editing and animation solutions to the
12+
* world. For more information visit <http://www.openshot.org/>.
13+
*
14+
* OpenShot Library (libopenshot) is free software: you can redistribute it
15+
* and/or modify it under the terms of the GNU Lesser General Public License
16+
* as published by the Free Software Foundation, either version 3 of the
17+
* License, or (at your option) any later version.
18+
*
19+
* OpenShot Library (libopenshot) is distributed in the hope that it will be
20+
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
* GNU Lesser General Public License for more details.
23+
*
24+
* You should have received a copy of the GNU Lesser General Public License
25+
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26+
*/
27+
28+
#include "../include/Settings.h"
29+
30+
using namespace std;
31+
using namespace openshot;
32+
33+
34+
// Global reference to logger
35+
Settings *Settings::m_pInstance = NULL;
36+
37+
// Create or Get an instance of the logger singleton
38+
Settings *Settings::Instance()
39+
{
40+
if (!m_pInstance) {
41+
// Create the actual instance of logger only once
42+
m_pInstance = new Settings;
43+
m_pInstance->HARDWARE_DECODE = false;
44+
m_pInstance->HARDWARE_ENCODE = false;
45+
m_pInstance->HIGH_QUALITY_SCALING = false;
46+
m_pInstance->WAIT_FOR_VIDEO_PROCESSING_TASK = false;
47+
}
48+
49+
return m_pInstance;
50+
}

0 commit comments

Comments
 (0)