/
video_format.cpp
272 lines (246 loc) · 10.1 KB
/
video_format.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
*
* This file is part of CasparCG (www.casparcg.com).
*
* CasparCG is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CasparCG 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Robert Nagy, ronag89@gmail.com
*/
#include "StdAfx.h"
#include "video_format.h"
#include <common/log.h>
#include <boost/algorithm/string.hpp>
#include <boost/range/adaptors.hpp>
#include <utility>
namespace caspar { namespace core {
struct video_format_repository::impl
{
private:
std::map<std::wstring, video_format_desc> formats_;
public:
impl()
: formats_()
{
const std::vector<video_format_desc> default_formats = {
{video_format::pal, 2, 720, 576, 1024, 576, 25000, 1000, L"PAL", {1920 / 2}},
{video_format::ntsc,
2,
720,
486,
720,
540,
30000,
1001,
L"NTSC",
{801, 801, 801, 800, 801, 801, 801, 800, 801, 801}},
{video_format::x576p2500, 1, 720, 576, 1024, 576, 25000, 1000, L"576p2500", {1920}},
{video_format::x720p2398, 1, 1280, 720, 1280, 720, 24000, 1001, L"720p2398", {2002}},
{video_format::x720p2400, 1, 1280, 720, 1280, 720, 24000, 1000, L"720p2400", {2000}},
{video_format::x720p2500, 1, 1280, 720, 1280, 720, 25000, 1000, L"720p2500", {1920}},
{video_format::x720p2997,
1,
1280,
720,
1280,
720,
30000,
1001,
L"720p2997",
{1602, 1601, 1602, 1601, 1602}},
{video_format::x720p3000, 1, 1280, 720, 1280, 720, 30000, 1000, L"720p3000", {1600}},
{video_format::x720p5000, 1, 1280, 720, 1280, 720, 50000, 1000, L"720p5000", {960}},
{video_format::x720p5994, 1, 1280, 720, 1280, 720, 60000, 1001, L"720p5994", {801, 800, 801, 801, 801}},
{video_format::x720p6000, 1, 1280, 720, 1280, 720, 60000, 1000, L"720p6000", {800}},
{video_format::x1080i5000, 2, 1920, 1080, 1920, 1080, 25000, 1000, L"1080i5000", {1920 / 2}},
{video_format::x1080i5994,
2,
1920,
1080,
1920,
1080,
30000,
1001,
L"1080i5994",
{801, 801, 801, 800, 801, 801, 801, 800, 801, 801}},
{video_format::x1080i6000, 2, 1920, 1080, 1920, 1080, 30000, 1000, L"1080i6000", {1600 / 2}},
{video_format::x1080p2398, 1, 1920, 1080, 1920, 1080, 24000, 1001, L"1080p2398", {2002}},
{video_format::x1080p2400, 1, 1920, 1080, 1920, 1080, 24000, 1000, L"1080p2400", {2000}},
{video_format::x1080p2500, 1, 1920, 1080, 1920, 1080, 25000, 1000, L"1080p2500", {1920}},
{video_format::x1080p2997,
1,
1920,
1080,
1920,
1080,
30000,
1001,
L"1080p2997",
{1602, 1601, 1602, 1601, 1602}},
{video_format::x1080p3000, 1, 1920, 1080, 1920, 1080, 30000, 1000, L"1080p3000", {1600}},
{video_format::x1080p5000, 1, 1920, 1080, 1920, 1080, 50000, 1000, L"1080p5000", {960}},
{video_format::x1080p5994, 1, 1920, 1080, 1920, 1080, 60000, 1001, L"1080p5994", {801, 800, 801, 801, 801}},
{video_format::x1080p6000, 1, 1920, 1080, 1920, 1080, 60000, 1000, L"1080p6000", {800}},
{video_format::x1556p2398, 1, 2048, 1556, 2048, 1556, 24000, 1001, L"1556p2398", {2002}},
{video_format::x1556p2400, 1, 2048, 1556, 2048, 1556, 24000, 1000, L"1556p2400", {2000}},
{video_format::x1556p2500, 1, 2048, 1556, 2048, 1556, 25000, 1000, L"1556p2500", {1920}},
{video_format::x2160p2398, 1, 3840, 2160, 3840, 2160, 24000, 1001, L"2160p2398", {2002}},
{video_format::x2160p2400, 1, 3840, 2160, 3840, 2160, 24000, 1000, L"2160p2400", {2000}},
{video_format::x2160p2500, 1, 3840, 2160, 3840, 2160, 25000, 1000, L"2160p2500", {1920}},
{video_format::x2160p2997,
1,
3840,
2160,
3840,
2160,
30000,
1001,
L"2160p2997",
{1602, 1601, 1602, 1601, 1602}},
{video_format::x2160p3000, 1, 3840, 2160, 3840, 2160, 30000, 1000, L"2160p3000", {1600}},
{video_format::x2160p5000, 1, 3840, 2160, 3840, 2160, 50000, 1000, L"2160p5000", {960}},
{video_format::x2160p5994, 1, 3840, 2160, 3840, 2160, 60000, 1001, L"2160p5994", {801, 800, 801, 801, 801}},
{video_format::x2160p6000, 1, 3840, 2160, 3840, 2160, 60000, 1000, L"2160p6000", {800}},
{video_format::x4kDCIp2398, 1, 4096, 2160, 4096, 2160, 24000, 1001, L"4kDCIp2398", {2002}},
{video_format::x4kDCIp2400, 1, 4096, 2160, 4096, 2160, 24000, 1000, L"4kDCIp2400", {2000}},
{video_format::x4kDCIp2500, 1, 4096, 2160, 4096, 2160, 25000, 1000, L"4kDCIp2500", {1920}},
{video_format::x4kDCIp2997,
1,
4096,
2160,
4096,
2160,
30000,
1001,
L"4kDCIp2997",
{1602, 1601, 1602, 1601, 1602}},
{video_format::x4kDCIp3000, 1, 4096, 2160, 4096, 2160, 30000, 1000, L"4kDCIp3000", {1600}},
{video_format::x4kDCIp5000, 1, 4096, 2160, 4096, 2160, 50000, 1000, L"4kDCIp5000", {960}},
{video_format::x4kDCIp5994,
1,
4096,
2160,
4096,
2160,
60000,
1001,
L"4kDCIp5994",
{801, 800, 801, 801, 801}},
{video_format::x4kDCIp6000, 1, 4096, 2160, 4096, 2160, 60000, 1000, L"4kDCIp6000", {800}},
};
for (auto& f : default_formats)
formats_.insert({(boost::to_lower_copy(f.name)), f});
}
video_format_desc find(const std::wstring& name) const
{
const std::wstring lower = boost::to_lower_copy(name);
const auto res = formats_.find(lower);
if (res != formats_.end())
return res->second;
return invalid();
}
video_format_desc find_format(const video_format& id) const
{
for (auto& f : formats_) {
if (f.second.format == id)
return f.second;
}
return invalid();
}
void store(const video_format_desc& format)
{
const std::wstring lower = boost::to_lower_copy(format.name);
formats_.insert({lower, format});
}
std::size_t get_max_video_format_size() const
{
size_t max = 0;
for (auto& f : formats_) {
if (f.second.size > max)
max = f.second.size;
}
return max;
}
};
video_format_repository::video_format_repository()
: impl_(new impl())
{
}
video_format_desc video_format_repository::invalid()
{
return video_format_desc(video_format::invalid, 1, 0, 0, 0, 0, 1, 1, L"invalid", {1});
};
video_format_desc video_format_repository::find(const std::wstring& name) const { return impl_->find(name); }
video_format_desc video_format_repository::find_format(const video_format& format) const
{
return impl_->find_format(format);
}
void video_format_repository::store(const video_format_desc& format) { impl_->store(format); }
std::size_t video_format_repository::get_max_video_format_size() const { return impl_->get_max_video_format_size(); }
video_format_desc::video_format_desc(const video_format format,
const int field_count,
const int width,
const int height,
const int square_width,
const int square_height,
const int time_scale,
const int duration,
const std::wstring name,
const std::vector<int> audio_cadence)
: format(format)
, width(width)
, height(height)
, square_width(square_width)
, square_height(square_height)
, field_count(field_count)
, hz(static_cast<double>(time_scale) / static_cast<double>(duration))
, fps(hz * field_count)
, framerate(time_scale, duration)
, time_scale(time_scale)
, duration(duration)
, size(width * height * 4)
, name(std::move(name))
, audio_sample_rate(48000)
, audio_cadence(std::move(audio_cadence))
{
}
video_format_desc::video_format_desc()
: format(video_format::invalid)
{
*this = video_format_repository::invalid();
}
bool operator==(const video_format_desc& lhs, const video_format_desc& rhs)
{
if (lhs.format == video_format::custom || rhs.format == video_format::custom) {
if (lhs.format != rhs.format) {
// If one is custom, and the other isnt, then they dont match
return false;
}
// TODO - expand on this
if (lhs.width != rhs.width || lhs.height != rhs.height || lhs.framerate != rhs.framerate) {
return false;
}
return true;
} else {
// If neither are custom, look just at the format
return lhs.format == rhs.format;
}
}
bool operator!=(const video_format_desc& lhs, const video_format_desc& rhs) { return !(lhs == rhs); }
std::wostream& operator<<(std::wostream& out, const video_format_desc& format_desc)
{
out << format_desc.name.c_str();
return out;
}
}} // namespace caspar::core