UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
video.cpp File Reference

This file defines some common video functions. More...

#include "debug.h"
#include "rtp/rtp_types.h"
#include "utils/macros.h"
#include "video.h"
#include <iomanip>
#include <sstream>
#include <map>
Include dependency graph for video.cpp:

Go to the source code of this file.

Classes

struct  video_mode_info_t
 

Macros

#define MOD_NAME   "[video] "
 

Functions

struct video_desc get_video_desc_from_string (const char *string)
 
const char * get_video_mode_description (enum video_mode video_mode)
 
enum video_mode get_video_mode_from_str (const char *requested_mode)
 
int get_video_mode_tiles_x (enum video_mode video_mode)
 
int get_video_mode_tiles_y (enum video_mode video_mode)
 
enum video_mode guess_video_mode (int num_substreams)
 Tries to guess video mode from number of substreams.
 
std::ostream & operator<< (std::ostream &os, const codec_t &color_spec)
 
std::ostream & operator<< (std::ostream &os, const video_desc &desc)
 
std::istream & operator>> (std::istream &is, video_desc &desc)
 
static double parse_fps (const char *string, bool interlaced)
 
const char * video_desc_to_string (struct video_desc d)
 

Variables

static const map< enum video_mode, video_mode_info_t > video_mode_info
 

Detailed Description

This file defines some common video functions.

Author
Martin Pulec pulec.nosp@m.@ces.nosp@m.net.c.nosp@m.z

These function are neither video frame nor video codec related.

Definition in file video.cpp.

Macro Definition Documentation

◆ MOD_NAME

#define MOD_NAME   "[video] "

Definition at line 57 of file video.cpp.

Function Documentation

◆ get_video_desc_from_string()

struct video_desc get_video_desc_from_string ( const char * string)
Returns
video description according to the configuration string, allowing BMD-like FourCC (pal, ntsc, Hi59 etc.)

Definition at line 291 of file video.cpp.

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;
347 ret.interlacing = INTERLACED_MERGED;
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
int color_printf(const char *format,...)
#define TBOLD(x)
Definition color_out.h:70
#define MSG(l, fmt,...)
Definition debug.h:96
#define STARTS_WITH(str, token)
Definition macros.h:102
const char * name
Definition settings.cpp:193
codec_t color_spec
Definition types.h:182
@ PROGRESSIVE
progressive frame
Definition types.h:164
@ INTERLACED_MERGED
Columngs of both fields are interlaced together.
Definition types.h:167
@ UYVY
YCbCr 422 8-bit - Cb Y0 Cr Y1.
Definition types.h:71
static double parse_fps(const char *string, bool interlaced)
Definition video.cpp:253

References color_printf(), video_desc::color_spec, desc, video_desc::fps, video_desc::height, INTERLACED_MERGED, video_desc::interlacing, MSG, name, parse_fps(), PROGRESSIVE, STARTS_WITH, TBOLD, video_desc::tile_count, UYVY, and video_desc::width.

Referenced by vidcap_testcard2_init(), and vidcap_testcard_init().

Here is the call graph for this function:

◆ get_video_mode_description()

const char * get_video_mode_description ( enum video_mode video_mode )

Definition at line 118 of file video.cpp.

119{
120 return video_mode_info.at(video_mode).name;
121}
video_mode
Definition types.h:354
static const map< enum video_mode, video_mode_info_t > video_mode_info
Definition video.cpp:67

References video_mode_info.

Referenced by decode_video_frame().

◆ get_video_mode_from_str()

enum video_mode get_video_mode_from_str ( const char * requested_mode)

This function matches string representation of video mode with its respective enumeration value.

Parameters
reuqested_modetextual representation of video mode
Returns
valid member of enum video mode

Definition at line 83 of file video.cpp.

83 {
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}
@ VIDEO_UNKNOWN
unspecified video mode
Definition types.h:355

References video_mode_info, and VIDEO_UNKNOWN.

Referenced by parse_options_internal().

◆ get_video_mode_tiles_x()

int get_video_mode_tiles_x ( enum video_mode video_mode )

◆ get_video_mode_tiles_y()

int get_video_mode_tiles_y ( enum video_mode video_mode )

Definition at line 113 of file video.cpp.

114{
115 return video_mode_info.at(video_mode).y;
116}

References video_mode_info.

Referenced by decoder_set_video_mode(), decompress_thread(), display_reconfigure(), fec_thread(), and reconfigure_decoder().

◆ guess_video_mode()

enum video_mode guess_video_mode ( int num_substreams)

Tries to guess video mode from number of substreams.

Note
The guessed video mode may not be correct (some modes may have the same number of substreams).
Parameters
num_substreamsnumber of received substreams
Returns
guessed video mode
Return values
VIDEO_UNKNOWNif the mode was not guessed.

Definition at line 123 of file video.cpp.

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}
@ VIDEO_STEREO
stereoscopic 3D video (full left and right eye)
Definition types.h:358
@ VIDEO_NORMAL
normal video (one tile)
Definition types.h:356
@ VIDEO_4K
tiled 4K video
Definition types.h:359
@ VIDEO_3X1
3x1 video
Definition types.h:360

References VIDEO_3X1, VIDEO_4K, VIDEO_NORMAL, VIDEO_STEREO, and VIDEO_UNKNOWN.

Referenced by decode_video_frame().

◆ operator<<() [1/2]

std::ostream & operator<< ( std::ostream & os,
const codec_t & color_spec )

Definition at line 157 of file video.cpp.

158{
159 os << get_codec_name(color_spec);
160 return os;
161}
const char * get_codec_name(codec_t codec)

References get_codec_name().

Here is the call graph for this function:

◆ operator<<() [2/2]

std::ostream & operator<< ( std::ostream & os,
const video_desc & desc )

Definition at line 141 of file video.cpp.

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}
unsigned int tile_count
Definition types.h:185
enum interlacing_t interlacing
Definition types.h:184
double fps
Definition types.h:183
unsigned int width
width of every tile (!)
Definition types.h:179
unsigned int height
height of every tile (!)
Definition types.h:180
@ SEGMENTED_FRAME
Segmented frame. Contains the same data as progressive frame.
Definition types.h:168
const char * get_interlacing_suffix(enum interlacing_t interlacing)
Returns suffix describing interlacing Eg. p, i or psf.

References video_desc::color_spec, desc, video_desc::fps, get_codec_name(), get_interlacing_suffix(), video_desc::height, video_desc::interlacing, PROGRESSIVE, SEGMENTED_FRAME, video_desc::tile_count, and video_desc::width.

Here is the call graph for this function:

◆ operator>>()

std::istream & operator>> ( std::istream & is,
video_desc & desc )
Todo
regex matching would be better

Definition at line 163 of file video.cpp.

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}
codec_t get_codec_from_name(const char *name)
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
enum interlacing_t get_interlacing_from_suffix(const char *suffix)

References video_desc::color_spec, desc, video_desc::fps, get_codec_from_name(), get_interlacing_from_suffix(), video_desc::height, video_desc::interlacing, LOG_LEVEL_ERROR, log_msg(), PROGRESSIVE, SEGMENTED_FRAME, video_desc::tile_count, and video_desc::width.

Here is the call graph for this function:

◆ parse_fps()

static double parse_fps ( const char * string,
bool interlaced )
static
Return values
0.0FPS not specified
<0.0wrong FPS specification

Definition at line 253 of file video.cpp.

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}
@ FPS_MAX
given by UG RTP payload header
Definition rtp_types.h:174

References FPS_MAX, and MSG.

Referenced by get_video_desc_from_string(), init(), parse_fmt(), parse_format(), vidcap_testcard2_init(), and vidcap_testcard_init().

◆ video_desc_to_string()

const char * video_desc_to_string ( struct video_desc d)

Definition at line 241 of file video.cpp.

242{
243 thread_local string desc_str;
244 desc_str = d;
245 return desc_str.c_str();
246}

Referenced by display_reconfigure(), show_help(), vidcap_file_init(), vidcap_testcard_init(), and worker().

Variable Documentation

◆ video_mode_info

const map<enum video_mode, video_mode_info_t> video_mode_info
static
Initial value:
= {
{ VIDEO_UNKNOWN, { "(unknown)", 0, 0 }},
{ VIDEO_NORMAL, { "normal", 1, 1 }},
{ VIDEO_DUAL, { "dual-link", 1, 2 }},
{ VIDEO_STEREO, { "3D", 2, 1 }},
{ VIDEO_4K, { "tiled-2x2", 2, 2 }},
{ VIDEO_3X1, { "3x1", 3, 1 }},
}
@ VIDEO_DUAL
1x2 video grid (is this ever used?)
Definition types.h:357

Definition at line 67 of file video.cpp.

67 {
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};

Referenced by get_video_mode_description(), get_video_mode_from_str(), get_video_mode_tiles_x(), and get_video_mode_tiles_y().