UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
video.cpp
Go to the documentation of this file.
1
9/*
10 * Copyright (c) 2013 CESNET, z. s. p. o.
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, is permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * 3. Neither the name of CESNET nor the names of its contributors may be
25 * used to endorse or promote products derived from this software without
26 * specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
30 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
31 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
32 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
38 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
39 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#ifdef HAVE_CONFIG_H
43#include "config.h"
44#include "config_unix.h"
45#include "config_win32.h"
46#endif // HAVE_CONFIG_H
47
48#include "debug.h"
49#include "rtp/rtp_types.h" // FPS_MAX
50#include "utils/macros.h"
51#include "video.h"
52
53#include <iomanip>
54#include <sstream>
55#include <map>
56
57#define MOD_NAME "[video] "
58
59using namespace std;
60
61typedef struct {
62 const char *name;
63 int x;
64 int y;
66
67const static map<enum video_mode, video_mode_info_t> video_mode_info = {
68 { VIDEO_UNKNOWN, { "(unknown)", 0, 0 }},
69 { VIDEO_NORMAL, { "normal", 1, 1 }},
70 { VIDEO_DUAL, { "dual-link", 1, 2 }},
71 { VIDEO_STEREO, { "3D", 2, 1 }},
72 { VIDEO_4K, { "tiled-2x2", 2, 2 }},
73 { VIDEO_3X1, { "3x1", 3, 1 }},
74};
75
83enum video_mode get_video_mode_from_str(const char *requested_mode) {
84 if(strcasecmp(requested_mode, "help") == 0) {
85 printf("Video mode options:\n");
86 auto it = video_mode_info.begin();
87 while (it != video_mode_info.end()) {
88 if (it == video_mode_info.begin()) { // omit unknown
89 ++it;
90 continue;
91 }
92 printf("\t%s\n", it->second.name);
93 if (++it != video_mode_info.end()) {
94 }
95 }
96 return VIDEO_UNKNOWN;
97 } else {
98 for (auto const &it : video_mode_info) {
99 if (strcasecmp(requested_mode, it.second.name) == 0) {
100 return it.first;
101 }
102 }
103 fprintf(stderr, "Unknown video mode (see -M help)\n");
104 return VIDEO_UNKNOWN;
105 }
106}
107
112
117
119{
120 return video_mode_info.at(video_mode).name;
121}
122
123enum video_mode guess_video_mode(int num_substreams)
124{
125 assert(num_substreams > 0);
126
127 switch (num_substreams) {
128 case 1:
129 return VIDEO_NORMAL;
130 case 2:
131 return VIDEO_STEREO;
132 case 3:
133 return VIDEO_3X1;
134 case 4:
135 return VIDEO_4K;
136 default:
137 return VIDEO_UNKNOWN;
138 }
139}
140
141std::ostream& operator<<(std::ostream& os, const video_desc& desc)
142{
143 std::streamsize p = os.precision();
144 ios_base::fmtflags f = os.flags();
145 if (desc.tile_count > 1) {
146 os << desc.tile_count << "*";
147 }
148 os << desc.width << "x" << desc.height << " @" << setprecision(2) << setiosflags(ios_base::fixed)
150 << get_interlacing_suffix(desc.interlacing) << ", codec "
152 os.precision(p);
153 os.flags(f);
154 return os;
155}
156
157std::ostream& operator<<(std::ostream& os, const codec_t& color_spec)
158{
159 os << get_codec_name(color_spec);
160 return os;
161}
162
163std::istream& operator>>(std::istream& is, video_desc& desc)
164{
165 video_desc out;
166 char buf[1024];
167 char *line = buf;
168
169 char interl_suffix[4] = "";
170 char codec_name[11] = "";
171
172 out.tile_count = 1;
173
174 unsigned int i = 0;
175 int num_spaces = 0;
176 while (is.good() && i < sizeof buf - 1) {
177 char c;
178 is.get(c);
179 if (!is.good()) {
180 break;
181 }
182 if (num_spaces == 3 &&
183 !(isalnum(c) || c == '.')) // allowed symbols in codec name
184 {
185 is.unget();
186 break;
187 } else {
188 buf[i++] = c;
189 }
190 if (c == ' ') {
191 num_spaces += 1;
192 }
193 }
194 buf[i] = '\0';
195
197 if (strchr(line, '*')) {
198 out.tile_count = atoi(line);
199 line = strchr(line, '*') + 1;
200 }
201 int ret = sscanf(line, "%dx%d @%lf%3[a-z], codec %10s", &out.width, &out.height,
202 &out.fps, interl_suffix, codec_name);
203
204 if (ret != 5) {
205 is.setstate(std::ios::failbit);
206 log_msg(LOG_LEVEL_ERROR, "Malformed video_desc representation!\n");
207 return is;
208 }
209
210 out.color_spec = get_codec_from_name(codec_name);
211 out.interlacing = get_interlacing_from_suffix(interl_suffix);
212 out.fps = out.fps / (out.interlacing == PROGRESSIVE || out.interlacing == SEGMENTED_FRAME ? 1 : 2);
213 desc = out;
214 return is;
215}
216
217bool video_desc::operator==(video_desc const & other) const
218{
219 return video_desc_eq(*this, other);
220
221}
222
223bool video_desc::operator!=(video_desc const & other) const
224{
225 return !video_desc_eq(*this, other);
226
227}
228
230{
232}
233
234video_desc::operator string() const
235{
236 ostringstream oss;
237 oss << *this;
238 return oss.str();
239}
240
241const char *video_desc_to_string(struct video_desc d)
242{
243 thread_local string desc_str;
244 desc_str = d;
245 return desc_str.c_str();
246}
247
252static double
253parse_fps(const char *string, bool interlaced)
254{
255 const char *fps_str = strpbrk(string, "DKdikp");
256 assert(fps_str != nullptr);
257 fps_str += 1;
258 if (strlen(fps_str) == 0) {
259 return 0.0;
260 }
261 char *endptr = nullptr;
262 const long fps_l = strtol(fps_str, &endptr, 10);
263 if (fps_l <= 0 || fps_l > FPS_MAX || *endptr != '\0') {
264 MSG(ERROR, "Wrong mode FPS specification: %s\n", string);
265 return -1;
266 }
267 double fps = 0;
268 // adjust FPS (Hi29 has FPS 29.97)
269 switch (fps_l) {
270 case 23:
271 fps = 23.98;
272 break;
273 case 29:
274 fps = 29.97;
275 break;
276 case 59:
277 fps = 59.94;
278 break;
279 default:
280 fps = (double) fps_l;
281 break;
282 }
283 // convert field per sec to FPS if needed (interlaced)
284 return interlaced ? fps / 2.0 : fps;
285}
286
292{
293 const struct {
294 const char *name;
295 struct video_desc desc;
296 } map[] = {
297 {"microvision", { 16, 16, UYVY, 10, PROGRESSIVE, 1 }},
298 {"sqcif", { 126, 96, UYVY, 29.97, PROGRESSIVE, 1 }},
299 {"qcif", { 176, 144, UYVY, 29.97, PROGRESSIVE, 1 }},
300 {"cif", { 352, 288, UYVY, 29.97, PROGRESSIVE, 1 }},
301 {"4cif", { 704, 576, UYVY, 29.97, PROGRESSIVE, 1 }},
302 {"ntsc", { 720, 480, UYVY, 29.97, INTERLACED_MERGED, 1 }},
303 {"pal", { 720, 576, UYVY, 25, INTERLACED_MERGED, 1 }},
304 {"cga", { 320, 200, UYVY, 60, PROGRESSIVE, 1 }},
305 {"vga", { 640, 480, UYVY, 60, PROGRESSIVE, 1 }},
306 {"svga", { 800, 600, UYVY, 60, PROGRESSIVE, 1 }},
307 {"xga", { 1024, 768, UYVY, 60, PROGRESSIVE, 1 }},
308 {"wxga", { 1280, 768, UYVY, 60, PROGRESSIVE, 1 }},
309 {"sxga", { 1280, 1024, UYVY, 60, PROGRESSIVE, 1 }},
310 {"uxga", { 1600, 1200, UYVY, 60, PROGRESSIVE, 1 }},
311 {"wuxga", { 1920, 1200, UYVY, 60, PROGRESSIVE, 1 }},
312 {"23ps", { 1920, 1080, UYVY, 23.98, PROGRESSIVE, 1 }},
313 {"24ps", { 1920, 1080, UYVY, 24, PROGRESSIVE, 1 }},
314 {"hd", { 1280, 720, UYVY, 29.97, PROGRESSIVE, 1 }},
315 {"720p", { 1280, 720, UYVY, 29.97, PROGRESSIVE, 1 }},
316 {"fhd", { 1920, 1080, UYVY, 29.97, PROGRESSIVE, 1 }},
317 {"1080p", { 1920, 1080, UYVY, 29.97, PROGRESSIVE, 1 }},
318 {"uhd", { 3840, 2160, UYVY, 29.97, PROGRESSIVE, 1 }},
319 {"dci", { 2048, 1080, UYVY, 23.98, PROGRESSIVE, 1 }},
320 {"dci4", { 4096, 2160, UYVY, 23.98, PROGRESSIVE, 1 }},
321 };
322 if (strcasecmp(string, "help") == 0) {
323 printf("Recognized video formats: ");
324 for (unsigned i = 0; i < sizeof map / sizeof map[0]; ++i) {
325 color_printf(TBOLD("%s") ", ", map[i].name);
326 }
327 color_printf(TBOLD("{Hp,Hi,hp,2d,4k,4d,1080i,1080p,720p,2160p}{23,24,25,29,30,59,60}") "\n");
328 return {};
329
330 }
331 for (unsigned i = 0; i < sizeof map / sizeof map[0]; ++i) {
332 if (strcasecmp(map[i].name, string) == 0 || (strlen(string) == 4 && string[3] == ' ' && strncasecmp(map[i].name, string, 3) == 0)) {
333 return map[i].desc;
334 }
335 }
336 // BMD-like syntax (+ line-counted syntax like 1080p<FPS>)
337 struct video_desc ret{};
338 ret.color_spec = UYVY;
339 ret.tile_count = 1;
340 if (strncmp(string, "Hp", 2) == 0 || STARTS_WITH(string, "1080p")) {
341 ret.width = 1920;
342 ret.height = 1080;
343 } else if (strncmp(string, "Hi", 2) == 0 ||
344 STARTS_WITH(string, "1080i")) {
345 ret.width = 1920;
346 ret.height = 1080;
348 } else if (strncmp(string, "hp", 2) == 0 ||
349 STARTS_WITH(string, "720p")) {
350 ret.width = 1280;
351 ret.height = 720;
352 } else if (strncasecmp(string, "2d", 2) == 0) {
353 ret.width = 2048;
354 ret.height = 1080;
355 } else if (strncasecmp(string, "4k", 2) == 0 ||
356 STARTS_WITH(string, "2160p")) {
357 ret.width = 3860;
358 ret.height = 2160;
359 } else if (strncasecmp(string, "4d", 2) == 0) {
360 ret.width = 4096;
361 ret.height = 2160;
362 }
363 if (ret.width == 0) {
364 MSG(ERROR, "Unrecognized video mode: %s\n", string);
365 return {};
366 }
367 ret.fps = parse_fps(string, ret.interlacing == INTERLACED_MERGED);
368 if (ret.fps < .0) {
369 return {};
370 }
371
372 return ret;
373}
struct video_desc desc
const char * get_codec_name(codec_t codec)
codec_t get_codec_from_name(const char *name)
int color_printf(const char *format,...)
#define TBOLD(x)
Definition color_out.h:70
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define MSG(l, fmt,...)
Definition debug.h:96
#define STARTS_WITH(str, token)
Definition macros.h:102
@ FPS_MAX
given by UG RTP payload header
Definition rtp_types.h:174
const char * name
Definition settings.cpp:193
bool operator!=(video_desc const &) const
Definition video.cpp:223
unsigned int tile_count
Definition types.h:185
codec_t color_spec
Definition types.h:182
enum interlacing_t interlacing
Definition types.h:184
double fps
Definition types.h:183
bool operator==(video_desc const &) const
Definition video.cpp:217
unsigned int width
width of every tile (!)
Definition types.h:179
unsigned int height
height of every tile (!)
Definition types.h:180
operator std::string() const
bool operator!() const
Definition video.cpp:229
const char * name
Definition video.cpp:62
video_mode
Definition types.h:354
@ VIDEO_STEREO
stereoscopic 3D video (full left and right eye)
Definition types.h:358
@ VIDEO_UNKNOWN
unspecified video mode
Definition types.h:355
@ VIDEO_NORMAL
normal video (one tile)
Definition types.h:356
@ VIDEO_4K
tiled 4K video
Definition types.h:359
@ VIDEO_DUAL
1x2 video grid (is this ever used?)
Definition types.h:357
@ VIDEO_3X1
3x1 video
Definition types.h:360
@ PROGRESSIVE
progressive frame
Definition types.h:164
@ SEGMENTED_FRAME
Segmented frame. Contains the same data as progressive frame.
Definition types.h:168
@ INTERLACED_MERGED
Columngs of both fields are interlaced together.
Definition types.h:167
codec_t
Definition types.h:65
@ UYVY
YCbCr 422 8-bit - Cb Y0 Cr Y1.
Definition types.h:71
@ VIDEO_CODEC_NONE
dummy color spec
Definition types.h:66
const char * video_desc_to_string(struct video_desc d)
Definition video.cpp:241
static const map< enum video_mode, video_mode_info_t > video_mode_info
Definition video.cpp:67
enum video_mode get_video_mode_from_str(const char *requested_mode)
Definition video.cpp:83
static double parse_fps(const char *string, bool interlaced)
Definition video.cpp:253
std::ostream & operator<<(std::ostream &os, const video_desc &desc)
Definition video.cpp:141
enum video_mode guess_video_mode(int num_substreams)
Tries to guess video mode from number of substreams.
Definition video.cpp:123
std::istream & operator>>(std::istream &is, video_desc &desc)
Definition video.cpp:163
struct video_desc get_video_desc_from_string(const char *string)
Definition video.cpp:291
int get_video_mode_tiles_x(enum video_mode video_mode)
Definition video.cpp:108
const char * get_video_mode_description(enum video_mode video_mode)
Definition video.cpp:118
int get_video_mode_tiles_y(enum video_mode video_mode)
Definition video.cpp:113
This is an umbrella header for video functions.
enum interlacing_t get_interlacing_from_suffix(const char *suffix)
const char * get_interlacing_suffix(enum interlacing_t interlacing)
Returns suffix describing interlacing Eg. p, i or psf.
bool video_desc_eq(struct video_desc a, struct video_desc b)
Compares two video descriptions.