cbf4b491b8f31acdf6ebc243eb7696d132128222
[dcpomatic.git] / src / lib / util.h
1 /*
2     Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 /** @file src/util.h
23  *  @brief Some utility functions and classes.
24  */
25
26
27 #ifndef DCPOMATIC_UTIL_H
28 #define DCPOMATIC_UTIL_H
29
30
31 #include "audio_mapping.h"
32 #include "dcpomatic_time.h"
33 #include "pixel_quanta.h"
34 #include "types.h"
35 #include <dcp/atmos_asset.h>
36 #include <dcp/decrypted_kdm.h>
37 #include <dcp/util.h>
38 #include <dcp/subtitle_image.h>
39 #include <boost/optional.hpp>
40 #include <boost/filesystem.hpp>
41 #include <boost/date_time/gregorian/gregorian.hpp>
42 #include <string>
43 #include <map>
44 #include <vector>
45
46 #undef check
47
48
49 namespace dcp {
50         class PictureAsset;
51         class SoundAsset;
52 }
53
54 #ifndef M_PI
55 #define M_PI (3.14159265358979323846)
56 #endif
57
58 /** The maximum number of audio channels that we can have in a DCP */
59 #define MAX_DCP_AUDIO_CHANNELS 16
60 /** Message broadcast to find possible encoding servers */
61 #define DCPOMATIC_HELLO "I mean really, Ray, it's used."
62 /** Number of films to keep in history */
63 #define HISTORY_SIZE 10
64 #define REPORT_PROBLEM _("Please report this problem by using Help -> Report a problem or via email to carl@dcpomatic.com")
65 #define TEXT_FONT_ID "font"
66 /** Largest KDM size (in bytes) that will be accepted */
67 #define MAX_KDM_SIZE (256 * 1024)
68 /** Number of lines that closed caption viewers will display */
69 #define MAX_CLOSED_CAPTION_LINES 3
70 /** Maximum line length of closed caption viewers, according to SMPTE Bv2.1 */
71 #define MAX_CLOSED_CAPTION_LENGTH 32
72 /** Maximum size of a subtitle / closed caption MXF in bytes, according to SMPTE Bv2.1 */
73 #define MAX_TEXT_MXF_SIZE (115 * 1024 * 1024)
74 #define MAX_TEXT_MXF_SIZE_TEXT "115MB"
75 /** Maximum size of a font file, in bytes */
76 #define MAX_FONT_FILE_SIZE (640 * 1024)
77 #define MAX_FONT_FILE_SIZE_TEXT "640KB"
78 /** Maximum size of the XML part of a closed caption file, according to SMPTE Bv2.1 */
79 #define MAX_CLOSED_CAPTION_XML_SIZE (256 * 1024)
80 #define MAX_CLOSED_CAPTION_XML_SIZE_TEXT "256KB"
81 #define CERTIFICATE_VALIDITY_PERIOD (10 * 365)
82
83 extern std::string program_name;
84 extern bool is_batch_converter;
85
86 struct AVSubtitle;
87 class AudioBuffers;
88 class TextDecoder;
89
90 extern std::string seconds_to_hms (int);
91 extern std::string time_to_hmsf (dcpomatic::DCPTime time, Frame rate);
92 extern std::string seconds_to_approximate_hms (int);
93 extern double seconds (struct timeval);
94 extern void dcpomatic_setup ();
95 extern void dcpomatic_setup_path_encoding ();
96 extern void dcpomatic_setup_gettext_i18n (std::string);
97 extern std::string digest_head_tail (std::vector<boost::filesystem::path>, boost::uintmax_t size);
98 extern void ensure_ui_thread ();
99 extern std::string audio_channel_name (int);
100 extern std::string short_audio_channel_name (int);
101 extern bool valid_image_file (boost::filesystem::path);
102 extern bool valid_sound_file (boost::filesystem::path);
103 extern bool valid_j2k_file (boost::filesystem::path);
104 #ifdef DCPOMATIC_WINDOWS
105 extern boost::filesystem::path mo_path ();
106 #endif
107 extern std::string tidy_for_filename (std::string);
108 extern dcp::Size fit_ratio_within (float ratio, dcp::Size);
109 extern int stride_round_up (int, int const *, int);
110 extern void* wrapped_av_malloc (size_t);
111 extern void set_backtrace_file (boost::filesystem::path);
112 extern std::map<std::string, std::string> split_get_request (std::string url);
113 extern std::string video_asset_filename (std::shared_ptr<dcp::PictureAsset> asset, int reel_index, int reel_count, boost::optional<std::string> content_summary);
114 extern std::string audio_asset_filename (std::shared_ptr<dcp::SoundAsset> asset, int reel_index, int reel_count, boost::optional<std::string> content_summary);
115 extern std::string atmos_asset_filename (std::shared_ptr<dcp::AtmosAsset> asset, int reel_index, int reel_count, boost::optional<std::string> content_summary);
116 extern float relaxed_string_to_float (std::string);
117 extern std::string careful_string_filter (std::string);
118 extern std::pair<int, int> audio_channel_types (std::list<int> mapped, int channels);
119 extern std::shared_ptr<AudioBuffers> remap (std::shared_ptr<const AudioBuffers> input, int output_channels, AudioMapping map);
120 extern Eyes increment_eyes (Eyes e);
121 extern void checked_fread (void* ptr, size_t size, FILE* stream, boost::filesystem::path path);
122 extern void checked_fwrite (void const * ptr, size_t size, FILE* stream, boost::filesystem::path path);
123 extern size_t utf8_strlen (std::string s);
124 extern std::string day_of_week_to_string (boost::gregorian::greg_weekday d);
125 extern void emit_subtitle_image (dcpomatic::ContentTimePeriod period, dcp::SubtitleImage sub, dcp::Size size, std::shared_ptr<TextDecoder> decoder);
126 extern bool show_jobs_on_console (bool progress);
127 extern void copy_in_bits (boost::filesystem::path from, boost::filesystem::path to, std::function<void (float)>);
128 extern dcp::Size scale_for_display (dcp::Size s, dcp::Size display_container, dcp::Size film_container, PixelQuanta quanta);
129 extern dcp::DecryptedKDM decrypt_kdm_with_helpful_error (dcp::EncryptedKDM kdm);
130 extern boost::filesystem::path default_font_file ();
131 extern std::string to_upper (std::string s);
132 extern void start_of_thread (std::string name);
133
134 template <class T>
135 std::list<T>
136 vector_to_list (std::vector<T> v)
137 {
138         std::list<T> l;
139         for (auto& i: v) {
140                 l.push_back (i);
141         }
142         return l;
143 }
144
145 template <class T>
146 std::vector<T>
147 list_to_vector (std::list<T> v)
148 {
149         std::vector<T> l;
150         for (auto& i: v) {
151                 l.push_back (i);
152         }
153         return l;
154 }
155
156 extern double db_to_linear (double db);
157 extern double linear_to_db (double linear);
158
159
160 template <class T>
161 T clamp (T val, T minimum, T maximum)
162 {
163         return std::max(std::min(val, maximum), minimum);
164 }
165
166
167 #endif