UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
delay.c
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2024 CESNET z.s.p.o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <assert.h>
39#include <math.h>
40#include <stdlib.h>
41#include <string.h>
42
43#include "debug.h"
44#include "lib_common.h"
45#include "types.h"
46#include "utils/color_out.h"
47#include "utils/list.h"
48#include "utils/macros.h"
49#include "video_codec.h"
50#include "video_display.h"
51#include "video_frame.h"
52#include "vo_postprocess.h"
53
54#define MOD_NAME "[vpp/delay] "
55
56struct state_delay {
57 double delay_sec;
61};
62
63static bool
64delay_get_property(void *state, int property, void *val, size_t *len)
65{
66 (void) state;
68 return false;
69 }
70 const size_t codecs_len = (VC_END - VC_FIRST) * sizeof(codec_t);
71 assert(codecs_len <= *len);
72 *len = codecs_len;
74 for (int i = VC_FIRST; i < VC_END; ++i) {
75 codecs[i] = i;
76 }
77 return true;
78}
79
80static void
82{
83 color_printf(TBOLD("delay") " postprocessor settings:\n");
85 TBOLD(TRED("\t-p delay:[seconds=<s>|frames=<f>]")) " | " TBOLD(
86 "-p delay:help") "\n");
87 color_printf("\nSeconds can be given as a decimal number.\n");
88 color_printf("\n");
89}
90
91static void *
92delay_init(const char *config)
93{
94 enum {
95 MAX_VAL_FRM = 2000,
96 MAX_VAL_SEC = 60,
97 };
98 if (!IS_KEY_PREFIX(config, "seconds") &&
99 !IS_KEY_PREFIX(config, "frames")) {
100 usage();
101 return NULL;
102 }
103
104 const char *val_s = strchr(config, '=') + 1;
105 double val = strtod(val_s, NULL);
106 if (val <= 0 || val > MAX_VAL_FRM ||
107 (IS_KEY_PREFIX(config, "seconds") && val > MAX_VAL_SEC)) {
108 MSG(ERROR, "Wrong delay value: %s\n", val_s);
109 return NULL;
110 }
111 if (IS_KEY_PREFIX(config, "frames") && fabs(round(val) - val) > 0.0) {
112 MSG(ERROR, "Number of frames should be an integer, given: %s\n",
113 val_s);
114 return NULL;
115 }
116
117 struct state_delay *s = calloc(1, sizeof(struct state_delay));
118 assert(s != NULL);
119 if (IS_KEY_PREFIX(config, "seconds")) {
120 s->delay_sec = val;
121 } else {
122 s->delay_frames = (int) val;
123 }
124 MSG(INFO, "Delay set to %g %s.\n", val,
125 s->delay_sec ? "seconds" : "frames");
127
128 return s;
129}
130
131static bool
133{
134 struct state_delay *s = (struct state_delay *) state;
135 s->desc = desc;
136
137 struct video_frame *f = NULL;
138 while ((f = simple_linked_list_pop(s->cached_frames)) != NULL) {
139 vf_free(f);
140 }
141
142 return true;
143}
144
145static struct video_frame *
147{
148 struct state_delay *s = state;
149
150 return vf_alloc_desc_data(s->desc);
151}
152
153static bool
154delay_postprocess(void *state, struct video_frame *in, struct video_frame *out,
155 int req_pitch)
156{
157 struct state_delay *s = state;
158
160
161 const int list_size = simple_linked_list_size(s->cached_frames);
162 if (s->delay_frames >= 0 && list_size < s->delay_frames) {
163 return false;
164 }
165
166 if (list_size / s->desc.fps < s->delay_sec) {
167 return false;
168 }
169
171 const size_t linesize =
173 for (size_t i = 0; i < s->desc.height; ++i) {
174 memcpy(out->tiles[0].data + i * req_pitch,
175 f->tiles[0].data + i * linesize, linesize);
176 }
177 vf_free(f);
178
179 return true;
180}
181
182static void
184{
185 struct state_delay *s = state;
186
187 struct video_frame *f = NULL;
188 while ((f = simple_linked_list_pop(s->cached_frames)) != NULL) {
189 vf_free(f);
190 }
192 free(s);
193}
194
195static void
196delay_get_out_desc(void *state, struct video_desc *out, int *in_display_mode,
197 int *out_frames)
198{
199 struct state_delay *s = state;
200
201 *out = s->desc;
202 *in_display_mode = DISPLAY_PROPERTY_VIDEO_MERGED;
203 *out_frames = 1;
204}
205
210
void ** state
int void * val
struct video_desc desc
int void size_t * len
int property
void vf_free(struct video_frame *buf)
Frees video_frame structure.
int vc_get_linesize(unsigned int width, codec_t codec)
struct video_frame * vf_alloc_desc_data(struct video_desc desc)
int color_printf(const char *format,...)
#define TRED(x)
Definition color_out.h:75
#define TBOLD(x)
Definition color_out.h:70
static __device__ vec3 round(const vec3 &v)
Definition cuda_dxt.cu:122
#define MSG(l, fmt,...)
Definition debug.h:96
static void delay_get_out_desc(void *state, struct video_desc *out, int *in_display_mode, int *out_frames)
Definition delay.c:196
static void delay_done(void *state)
Definition delay.c:183
static bool delay_get_property(void *state, int property, void *val, size_t *len)
Definition delay.c:64
static const struct vo_postprocess_info vo_pp_delay_info
Definition delay.c:206
static void * delay_init(const char *config)
Definition delay.c:92
static bool delay_postprocess(void *state, struct video_frame *in, struct video_frame *out, int req_pitch)
Definition delay.c:154
static bool delay_reconfigure(void *state, struct video_desc desc)
Definition delay.c:132
static void usage()
Definition delay.c:81
static struct video_frame * delay_getf(void *state)
Definition delay.c:146
@ DISPLAY_PROPERTY_VIDEO_MERGED
monolithic framebuffer
@ LIBRARY_CLASS_VIDEO_POSTPROCESS
Definition lib_common.h:83
#define REGISTER_MODULE(name, info, lclass, abi)
Registers module to global modules' registry.
Definition lib_common.h:144
void simple_linked_list_append(struct simple_linked_list *l, void *data)
Definition list.cpp:68
int simple_linked_list_size(struct simple_linked_list *l)
Definition list.cpp:93
struct simple_linked_list * simple_linked_list_init(void)
Definition list.cpp:53
void simple_linked_list_destroy(struct simple_linked_list *l)
Definition list.cpp:58
void * simple_linked_list_pop(struct simple_linked_list *l)
Definition list.cpp:82
#define IS_KEY_PREFIX(tok, key)
expands to true value if <k> from tok in format <k>=<v> is prefix of key
Definition macros.h:138
struct simple_linked_list * cached_frames
Definition delay.c:60
struct video_desc desc
Definition delay.c:59
int delay_frames
Definition delay.c:58
double delay_sec
Definition delay.c:57
unsigned int width
Definition types.h:280
char * data
Raw tile data. Pointer must be at least 4B aligned.
Definition types.h:287
double fps
Definition types.h:183
unsigned int height
height of every tile (!)
Definition types.h:180
Struct video_frame represents a video frame and contains video description.
Definition types.h:305
struct tile tiles[FLEXIBLE_ARRAY_MEMBER]
Definition types.h:345
codec_t color_spec
Definition types.h:306
codec_t
Definition types.h:65
@ VC_FIRST
Definition types.h:69
@ VC_END
Definition types.h:110
static struct @76 codecs[]
#define VO_PP_PROPERTY_CODECS
#define VO_PP_ABI_VERSION