Debian Codegraph @

src/xrt/auxiliary/util/u_sink_combiner.c

raw symbols SCIP
1 // Copyright 2019-2021, Collabora, Ltd.cxx . . $ `<file>/src/xrt/auxiliary/util/u_sink_combiner.c`/
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4 * @file
5 * @brief An @ref xrt_frame_sink that combines two frames into a stereo frame.
6 * @author Jakob Bornecrantz <[email protected]>
7 * @author Moshi Turner <[email protected]>
8 * @ingroup aux_util
9 */
10
11 #include "util/u_misc.h"cxx . . $ `<file>/src/xrt/auxiliary/util/u_misc.h`/
12 #include "util/u_sink.h"cxx . . $ `<file>/src/xrt/auxiliary/util/u_sink.h`/
13 #include "util/u_frame.h"cxx . . $ `<file>/src/xrt/auxiliary/util/u_frame.h`/
14 #include "util/u_logging.h"cxx . . $ `<file>/src/xrt/auxiliary/util/u_logging.h`/
15 #include "util/u_trace_marker.h"cxx . . $ `<file>/src/xrt/auxiliary/util/u_trace_marker.h`/
16
17 #include <stdio.h>
18 #include <pthread.h>
19
20
21 /*!
22 * An @ref xrt_frame_sink combiner, frames pushed to the left and right side will be combined into one @ref xrt_frame
23 * with format XRT_STEREO_FORMAT_SBS. Will drop stale frames if the combining work takes too long.
24 *
25 * @implements xrt_frame_sink
26 * @implements xrt_frame_node
27 */
28 struct u_sink_combinercxx . . $ u_sink_combiner#
29 {
30 //! Base sink for left.
31 struct xrt_frame_sinkcxx . . $ xrt_frame_sink# leftcxx . . $ u_sink_combiner#left.;
32
33 //! Base sink for right.
34 struct xrt_frame_sinkcxx . . $ xrt_frame_sink# rightcxx . . $ u_sink_combiner#right.;
35
36 //! For tracking on the frame context.
37 struct xrt_frame_nodecxx . . $ xrt_frame_node# nodecxx . . $ u_sink_combiner#node.;
38
39 //! The consumer of the frames that are queued.
40 struct xrt_frame_sinkcxx . . $ xrt_frame_sink# *consumercxx . . $ u_sink_combiner#consumer.;
41
42 //! The current queued frame.
43 struct xrt_framecxx . . $ xrt_frame# *framescxx . . $ u_sink_combiner#frames.[2];
44
45 pthread_tcxx . . $ pthread_t# threadcxx . . $ u_sink_combiner#thread.;
46 pthread_mutex_tcxx . . $ pthread_mutex_t# mutexcxx . . $ u_sink_combiner#mutex.;
47 pthread_cond_tcxx . . $ pthread_cond_t# condcxx . . $ u_sink_combiner#cond.;
48
49 //! Should we keep running.
50 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! runningcxx . . $ u_sink_combiner#running.;
51 };
52
53 static void
54 combine_frames_l8cxx . . $ combine_frames_l8(6eb2972212c7749c).(struct xrt_framecxx . . $ xrt_frame# *l, struct xrt_framecxx . . $ xrt_frame# *r, struct xrt_framecxx . . $ xrt_frame# *f)
55 {
56 SINK_TRACE_MARKERcxx . . $ `src/xrt/auxiliary/util/u_trace_marker.h:116:9`!();
57
58
59 uint32_tcxx . . $ uint32_t# height = l->heightcxx . . $ xrt_frame#height.;
60
61 for (uint32_tcxx . . $ uint32_t# y = 0; y < height; y++) {
62 uint8_tcxx . . $ uint8_t# *dst = f->datacxx . . $ xrt_frame#data. + f->stridecxx . . $ xrt_frame#stride. * y;
63 uint8_tcxx . . $ uint8_t# *src = l->datacxx . . $ xrt_frame#data. + l->stridecxx . . $ xrt_frame#stride. * y;
64
65 for (uint32_tcxx . . $ uint32_t# x = 0; x < l->widthcxx . . $ xrt_frame#width.; x++) {
66 *dst++ = *src++;
67 }
68
69 dst = f->datacxx . . $ xrt_frame#data. + f->stridecxx . . $ xrt_frame#stride. * y + l->widthcxx . . $ xrt_frame#width.;
70 src = r->datacxx . . $ xrt_frame#data. + r->stridecxx . . $ xrt_frame#stride. * y;
71 for (uint32_tcxx . . $ uint32_t# x = 0; x < r->widthcxx . . $ xrt_frame#width.; x++) {
72 *dst++ = *src++;
73 }
74 }
75 }
76
77 static void
78 combine_frames_r8g8b8cxx . . $ combine_frames_r8g8b8(6eb2972212c7749c).(struct xrt_framecxx . . $ xrt_frame# *l, struct xrt_framecxx . . $ xrt_frame# *r, struct xrt_framecxx . . $ xrt_frame# *f)
79 {
80 SINK_TRACE_MARKERcxx . . $ `src/xrt/auxiliary/util/u_trace_marker.h:116:9`!();
81
82 uint32_tcxx . . $ uint32_t# height = l->heightcxx . . $ xrt_frame#height.;
83
84 for (uint32_tcxx . . $ uint32_t# y = 0; y < height; y++) {
85 uint8_tcxx . . $ uint8_t# *dst = f->datacxx . . $ xrt_frame#data. + f->stridecxx . . $ xrt_frame#stride. * y;
86 uint8_tcxx . . $ uint8_t# *src = l->datacxx . . $ xrt_frame#data. + l->stridecxx . . $ xrt_frame#stride. * y;
87
88 for (uint32_tcxx . . $ uint32_t# x = 0; x < l->widthcxx . . $ xrt_frame#width. * 3; x++) {
89 *dst++ = *src++;
90 }
91
92 dst = f->datacxx . . $ xrt_frame#data. + f->stridecxx . . $ xrt_frame#stride. * y + l->widthcxx . . $ xrt_frame#width. * 3;
93 src = r->datacxx . . $ xrt_frame#data. + r->stridecxx . . $ xrt_frame#stride. * y;
94 for (uint32_tcxx . . $ uint32_t# x = 0; x < r->widthcxx . . $ xrt_frame#width. * 3; x++) {
95 *dst++ = *src++;
96 }
97 }
98 }
99
100 static void
101 combine_framescxx . . $ combine_frames(d3e171790c4e540a).(struct xrt_framecxx . . $ xrt_frame# *l, struct xrt_framecxx . . $ xrt_frame# *r, struct xrt_framecxx . . $ xrt_frame# **out_frame)
102 {
103 SINK_TRACE_MARKERcxx . . $ `src/xrt/auxiliary/util/u_trace_marker.h:116:9`!();
104
105 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(l->width == r->width);
106 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(l->height == r->height);
107 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(l->format == r->format);
108 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!((l->format == XRT_FORMAT_L8) || (l->format == XRT_FORMAT_R8G8B8));
109
110 int64_tcxx . . $ int64_t# diff_ns = l->timestampcxx . . $ xrt_frame#timestamp. - r->timestampcxx . . $ xrt_frame#timestamp.;
111 uint32_tcxx . . $ uint32_t# height = l->heightcxx . . $ xrt_frame#height.;
112 uint32_tcxx . . $ uint32_t# width = l->widthcxx . . $ xrt_frame#width. + r->widthcxx . . $ xrt_frame#width.;
113 enum xrt_formatcxx . . $ xrt_format# format = l->formatcxx . . $ xrt_frame#format.;
114
115 u_frame_create_one_offcxx . . $ u_frame_create_one_off(c137bc5da3ab1e89).(format, width, height, out_frame);
116
117 struct xrt_framecxx . . $ xrt_frame# *f = *out_frame;
118 f->timestampcxx . . $ xrt_frame#timestamp. = l->timestampcxx . . $ xrt_frame#timestamp. - (diff_ns / 2); // Middle of both frames.
119 f->stereo_formatcxx . . $ xrt_frame#stereo_format. = XRT_STEREO_FORMAT_SBScxx . . $ XRT_STEREO_FORMAT_SBS.;
120 f->source_sequencecxx . . $ xrt_frame#source_sequence. = l->source_sequencecxx . . $ xrt_frame#source_sequence.;
121
122 switch (l->formatcxx . . $ xrt_frame#format.) {
123 case XRT_FORMAT_L8cxx . . $ XRT_FORMAT_L8.: {
124 combine_frames_l8cxx . . $ combine_frames_l8(6eb2972212c7749c).(l, r, f);
125 break;
126 }
127 case XRT_FORMAT_R8G8B8cxx . . $ XRT_FORMAT_R8G8B8.: {
128 combine_frames_r8g8b8cxx . . $ combine_frames_r8g8b8(6eb2972212c7749c).(l, r, f);
129 break;
130 }
131 default: assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(!"Unimplemented!");
132 }
133 #if 0
134 // So that we can test if this works on a really slow computer
135 os_nanosleep(0.1f * U_TIME_1S_IN_NS);
136 #endif
137 }
138
139 static void
140 combiner_left_framecxx . . $ combiner_left_frame(1e8c9dd18059651).(struct xrt_frame_sinkcxx . . $ xrt_frame_sink# *xfs, struct xrt_framecxx . . $ xrt_frame# *xf)
141 {
142 SINK_TRACE_MARKERcxx . . $ `src/xrt/auxiliary/util/u_trace_marker.h:116:9`!();
143
144 struct u_sink_combinercxx . . $ u_sink_combiner# *q = container_ofcxx . . $ `src/xrt/include/xrt/xrt_compiler.h:149:9`!(xfs, struct u_sink_combiner, left);
145
146 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&q->mutexcxx . . $ u_sink_combiner#mutex.);
147
148 // Only schedule new frames if we are running.
149 if (q->runningcxx . . $ u_sink_combiner#running.) {
150 xrt_frame_referencecxx . . $ xrt_frame_reference(fce10333cf069560).(&q->framescxx . . $ u_sink_combiner#frames.[0], xf);
151
152 // the right frame can be not null if the combiner is dropping frames and we've received another
153 // left-right push as it was still running.
154 xrt_frame_referencecxx . . $ xrt_frame_reference(fce10333cf069560).(&q->framescxx . . $ u_sink_combiner#frames.[1], NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
155 }
156
157
158
159 pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&q->mutexcxx . . $ u_sink_combiner#mutex.);
160 }
161
162 static void
163 combiner_right_framecxx . . $ combiner_right_frame(1e8c9dd18059651).(struct xrt_frame_sinkcxx . . $ xrt_frame_sink# *xfs, struct xrt_framecxx . . $ xrt_frame# *xf)
164 {
165 SINK_TRACE_MARKERcxx . . $ `src/xrt/auxiliary/util/u_trace_marker.h:116:9`!();
166
167 struct u_sink_combinercxx . . $ u_sink_combiner# *q = container_ofcxx . . $ `src/xrt/include/xrt/xrt_compiler.h:149:9`!(xfs, struct u_sink_combiner, right);
168
169 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&q->mutexcxx . . $ u_sink_combiner#mutex.);
170
171 // Only schedule new frames if we are running.
172 if (q->runningcxx . . $ u_sink_combiner#running.) {
173 xrt_frame_referencecxx . . $ xrt_frame_reference(fce10333cf069560).(&q->framescxx . . $ u_sink_combiner#frames.[1], xf);
174 }
175
176 // If both frames are here, do the work!
177 // (Yes, this push_frame will block, and so will combiner_left_frame as it's waiting for the work to complete.
178 // It's okay, u_sink_force_genlock does the async/frame-dropping stuff for us.)
179 if (q->framescxx . . $ u_sink_combiner#frames.[0] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && q->framescxx . . $ u_sink_combiner#frames.[1] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
180 struct xrt_framecxx . . $ xrt_frame# *frames[2] = {NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!};
181 frames[0] = q->framescxx . . $ u_sink_combiner#frames.[0];
182 frames[1] = q->framescxx . . $ u_sink_combiner#frames.[1];
183 q->framescxx . . $ u_sink_combiner#frames.[0] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
184 q->framescxx . . $ u_sink_combiner#frames.[1] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
185 /*
186 * Check timestamps.
187 */
188 XRT_MAYBE_UNUSEDcxx . . $ `src/xrt/include/xrt/xrt_compiler.h:53:9`! int64_tcxx . . $ int64_t# diff_ns = frames[0]->timestampcxx . . $ xrt_frame#timestamp. - frames[1]->timestampcxx . . $ xrt_frame#timestamp.;
189
190
191 // u_sink_force_genlock should have done this for us already
192 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(!(diff_ns < -U_TIME_1MS_IN_NScxx . . $ `src/xrt/auxiliary/util/u_time.h:54:9`! || diff_ns > U_TIME_1MS_IN_NScxx . . $ `src/xrt/auxiliary/util/u_time.h:54:9`!));
193
194 struct xrt_framecxx . . $ xrt_frame# *frame = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
195 combine_framescxx . . $ combine_frames(d3e171790c4e540a).(frames[0], frames[1], &frame);
196
197 // Send to the consumer that does the work.
198 xrt_sink_push_framecxx . . $ xrt_sink_push_frame(1e8c9dd18059651).(q->consumercxx . . $ u_sink_combiner#consumer., frame);
199
200 /*
201 * Drop our reference we don't need it anymore, or it's held by
202 * the consumer.
203 */
204 xrt_frame_referencecxx . . $ xrt_frame_reference(fce10333cf069560).(&frame, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
205 xrt_frame_referencecxx . . $ xrt_frame_reference(fce10333cf069560).(&frames[0], NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
206 xrt_frame_referencecxx . . $ xrt_frame_reference(fce10333cf069560).(&frames[1], NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
207
208 } else {
209 U_LOG_Wcxx . . $ `src/xrt/auxiliary/util/u_logging.h:301:9`!("Right frame pushed with no left frame");
210 }
211
212 pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&q->mutexcxx . . $ u_sink_combiner#mutex.);
213 }
214
215 static void
216 combiner_break_apartcxx . . $ combiner_break_apart(4381388d16accfed).(struct xrt_frame_nodecxx . . $ xrt_frame_node# *node)
217 {
218 struct u_sink_combinercxx . . $ u_sink_combiner# *q = container_ofcxx . . $ `src/xrt/include/xrt/xrt_compiler.h:149:9`!(node, struct u_sink_combiner, node);
219
220 // The fields are protected.
221 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&q->mutexcxx . . $ u_sink_combiner#mutex.);
222
223 // Stop the thread and inhibit any new frames to be added to the queue.
224 q->runningcxx . . $ u_sink_combiner#running. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
225
226 // Release any frame waiting for submission.
227 xrt_frame_referencecxx . . $ xrt_frame_reference(fce10333cf069560).(&q->framescxx . . $ u_sink_combiner#frames.[0], NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
228 xrt_frame_referencecxx . . $ xrt_frame_reference(fce10333cf069560).(&q->framescxx . . $ u_sink_combiner#frames.[1], NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
229
230 // No longer need to protect fields.
231 pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&q->mutexcxx . . $ u_sink_combiner#mutex.);
232 }
233
234 static void
235 combiner_destroycxx . . $ combiner_destroy(4381388d16accfed).(struct xrt_frame_nodecxx . . $ xrt_frame_node# *node)
236 {
237 struct u_sink_combinercxx . . $ u_sink_combiner# *q = container_ofcxx . . $ `src/xrt/include/xrt/xrt_compiler.h:149:9`!(node, struct u_sink_combiner, node);
238
239 // Destroy resources.
240 pthread_mutex_destroycxx . . $ pthread_mutex_destroy(d89ae235521888f9).(&q->mutexcxx . . $ u_sink_combiner#mutex.);
241 pthread_cond_destroycxx . . $ pthread_cond_destroy(98095c34a2b0149d).(&q->condcxx . . $ u_sink_combiner#cond.);
242 freecxx . . $ free(5d22bdacc48458e8).(q);
243 }
244
245
246 /*
247 *
248 * Exported functions.
249 *
250 */
251
252 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`!
253 u_sink_combiner_createcxx . . $ u_sink_combiner_create(59b9d4473286b266).(struct xrt_frame_contextcxx . . $ xrt_frame_context# *xfctx,
254 struct xrt_frame_sinkcxx . . $ xrt_frame_sink# *downstream,
255 struct xrt_frame_sinkcxx . . $ xrt_frame_sink# **out_left_xfs,
256 struct xrt_frame_sinkcxx . . $ xrt_frame_sink# **out_right_xfs)
257 {
258 struct u_sink_combinercxx . . $ u_sink_combiner# *q = U_TYPED_CALLOCcxx . . $ `src/xrt/auxiliary/util/u_misc.h:47:9`!(struct u_sink_combiner);
259 int ret = 0;
260
261 // If you remove this, this sink will block for some time after you push the left frame while copying the data.
262 // Only remove this if you're sure that's okay.
263 u_sink_force_genlock_createcxx . . $ u_sink_force_genlock_create(7ad4e49ea7565b31).(xfctx, &q->leftcxx . . $ u_sink_combiner#left., &q->rightcxx . . $ u_sink_combiner#right., out_left_xfs, out_right_xfs);
264
265 q->leftcxx . . $ u_sink_combiner#left..push_framecxx . . $ xrt_frame_sink#push_frame. = combiner_left_framecxx . . $ combiner_left_frame(1e8c9dd18059651).;
266 q->rightcxx . . $ u_sink_combiner#right..push_framecxx . . $ xrt_frame_sink#push_frame. = combiner_right_framecxx . . $ combiner_right_frame(1e8c9dd18059651).;
267 q->nodecxx . . $ u_sink_combiner#node..break_apartcxx . . $ xrt_frame_node#break_apart. = combiner_break_apartcxx . . $ combiner_break_apart(4381388d16accfed).;
268 q->nodecxx . . $ u_sink_combiner#node..destroycxx . . $ xrt_frame_node#destroy. = combiner_destroycxx . . $ combiner_destroy(4381388d16accfed).;
269 q->consumercxx . . $ u_sink_combiner#consumer. = downstream;
270 q->runningcxx . . $ u_sink_combiner#running. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
271
272 ret = pthread_mutex_initcxx . . $ pthread_mutex_init(db9d29dab44ea1aa).(&q->mutexcxx . . $ u_sink_combiner#mutex., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
273 if (ret != 0) {
274 freecxx . . $ free(5d22bdacc48458e8).(q);
275 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
276 }
277
278 xrt_frame_context_addcxx . . $ xrt_frame_context_add(cc43192741dca22).(xfctx, &q->nodecxx . . $ u_sink_combiner#node.);
279
280
281 return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
282 }