Repository navigation
Expand file tree
/
Copy pathgstpbpip.c
More file actions
2447 lines (2294 loc) · 89.6 KB
/
Copy pathgstpbpip.c
File metadata and controls
2447 lines (2294 loc) · 89.6 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <glib/gstdio.h>
#include <gst/gst.h>
#include <gst/base/gstbasesink.h>
#include <gst/video/video.h>
#include <gst/video/gstvideofilter.h>
GST_DEBUG_CATEGORY_STATIC(pbpip_debug);
#define GST_CAT_DEFAULT pbpip_debug
#define RING_CAP 12
#define PREBUFFER 3
#define NSLOTS 4
#define STALE_US (2 * G_USEC_PER_SEC)
static GMutex ring_lock;
typedef struct {
guint8 *slot[RING_CAP];
gsize slot_size;
guint rd, count;
gint w, h;
gboolean started;
gint64 last_push;
guint8 *cur;
gboolean have_cur;
guint64 pushed, dropped, shown, repeated;
} Ring;
static Ring rings[NSLOTS];
static void ring_reset_locked(Ring *ring) {
ring->rd = ring->count = 0;
ring->started = FALSE;
ring->have_cur = FALSE;
}
static void ring_resize_locked(Ring *ring, gint w, gint h) {
gsize need = (gsize) w * h * 3 / 2;
if (need != ring->slot_size) {
for (guint i = 0; i < RING_CAP; i++) {
g_free(ring->slot[i]);
ring->slot[i] = g_malloc(need);
}
g_free(ring->cur);
ring->cur = g_malloc(need);
ring->slot_size = need;
}
ring->w = w;
ring->h = h;
ring_reset_locked(ring);
}
typedef struct { GstBaseSink parent; GstVideoInfo info; gboolean have_info; guint slot; } PbPipSink;
typedef struct { GstBaseSinkClass parent_class; } PbPipSinkClass;
G_DEFINE_TYPE(PbPipSink, pb_pip_sink, GST_TYPE_BASE_SINK)
static GstStaticPadTemplate sink_tmpl = GST_STATIC_PAD_TEMPLATE(
"sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS("video/x-raw, format=(string)NV12"));
static gboolean pb_sink_set_caps(GstBaseSink *bs, GstCaps *caps) {
PbPipSink *self = (PbPipSink *) bs;
if (!gst_video_info_from_caps(&self->info, caps))
return FALSE;
Ring *ring = &rings[self->slot];
g_mutex_lock(&ring_lock);
ring_resize_locked(ring, GST_VIDEO_INFO_WIDTH(&self->info), GST_VIDEO_INFO_HEIGHT(&self->info));
g_mutex_unlock(&ring_lock);
self->have_info = TRUE;
GST_INFO_OBJECT(self, "kleines Bild %dx%d", ring->w, ring->h);
return TRUE;
}
static GstFlowReturn pb_sink_render(GstBaseSink *bs, GstBuffer *buf) {
PbPipSink *self = (PbPipSink *) bs;
GstVideoFrame f;
if (!self->have_info || !gst_video_frame_map(&f, &self->info, buf, GST_MAP_READ))
return GST_FLOW_OK;
Ring *ring = &rings[self->slot];
const gint w = ring->w, h = ring->h;
g_mutex_lock(&ring_lock);
if (ring->count == RING_CAP) {
ring->rd = (ring->rd + 1) % RING_CAP;
ring->count--;
ring->dropped++;
}
guint8 *dst = ring->slot[(ring->rd + ring->count) % RING_CAP];
const guint8 *y = GST_VIDEO_FRAME_PLANE_DATA(&f, 0), *uv = GST_VIDEO_FRAME_PLANE_DATA(&f, 1);
const gint sy = GST_VIDEO_FRAME_PLANE_STRIDE(&f, 0), suv = GST_VIDEO_FRAME_PLANE_STRIDE(&f, 1);
for (gint r = 0; r < h; r++)
memcpy(dst + (gsize) r * w, y + (gsize) r * sy, w);
for (gint r = 0; r < h / 2; r++)
memcpy(dst + (gsize) w * h + (gsize) r * w, uv + (gsize) r * suv, w);
ring->count++;
ring->pushed++;
ring->last_push = g_get_monotonic_time();
g_mutex_unlock(&ring_lock);
gst_video_frame_unmap(&f);
return GST_FLOW_OK;
}
static gboolean pb_sink_stop(GstBaseSink *bs) {
PbPipSink *self = (PbPipSink *) bs;
g_mutex_lock(&ring_lock);
ring_reset_locked(&rings[self->slot]);
g_mutex_unlock(&ring_lock);
return TRUE;
}
static void pb_sink_set_property(GObject *o, guint id, const GValue *v, GParamSpec *ps) {
PbPipSink *self = (PbPipSink *) o;
if (id == 1) self->slot = g_value_get_uint(v) % NSLOTS;
else G_OBJECT_WARN_INVALID_PROPERTY_ID(o, id, ps);
}
static void pb_sink_get_property(GObject *o, guint id, GValue *v, GParamSpec *ps) {
PbPipSink *self = (PbPipSink *) o;
if (id == 1) g_value_set_uint(v, self->slot);
else G_OBJECT_WARN_INVALID_PROPERTY_ID(o, id, ps);
}
static void pb_pip_sink_class_init(PbPipSinkClass *klass) {
GObjectClass *oc = G_OBJECT_CLASS(klass);
oc->set_property = pb_sink_set_property;
oc->get_property = pb_sink_get_property;
g_object_class_install_property(oc, 1,
g_param_spec_uint("slot", "Platz", "welches kleine Bild (0 bis 2)", 0, NSLOTS - 1, 0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
GstElementClass *ec = GST_ELEMENT_CLASS(klass);
GstBaseSinkClass *bc = GST_BASE_SINK_CLASS(klass);
gst_element_class_set_static_metadata(ec, "IRL4YOU PiP Eingang", "Sink/Video",
"Nimmt das kleine Bild für Bild-in-Bild entgegen", "IRL4YOU");
gst_element_class_add_static_pad_template(ec, &sink_tmpl);
bc->set_caps = pb_sink_set_caps;
bc->render = pb_sink_render;
bc->stop = pb_sink_stop;
}
static void pb_pip_sink_init(PbPipSink *self) {
gst_base_sink_set_sync(GST_BASE_SINK(self), FALSE);
gst_base_sink_set_async_enabled(GST_BASE_SINK(self), FALSE);
self->have_info = FALSE;
}
static void pb_pos(guint corner, gint mw, gint mh, gint pw, gint ph, gint margin, gint fx, gint fy, gint *x, gint *y) {
if (corner == 5u) {
if (fx < 0) fx = 0;
if (fx > 1000) fx = 1000;
if (fy < 0) fy = 0;
if (fy > 1000) fy = 1000;
*x = (gint) (((gint64) (mw - pw) * fx) / 1000);
*y = (gint) (((gint64) (mh - ph) * fy) / 1000);
if (*x < 0) *x = 0;
if (*y < 0) *y = 0;
} else if (corner == 4u) {
*x = (mw - pw) / 2;
*y = mh - ph - margin;
} else {
*x = (corner & 1u) ? mw - pw - margin : margin;
*y = (corner & 2u) ? mh - ph - margin : margin;
}
if (corner != 5u) {
if (*x > mw - pw) *x = mw - pw;
if (*y > mh - ph) *y = mh - ph;
if (*x < 0) *x = 0;
if (*y < 0) *y = 0;
}
}
#define PB_SHIFT 10
#define PB_ONE (1 << PB_SHIFT)
#define PB_HALF (1 << (2 * PB_SHIFT - 1))
#define PB_CHUNK 128
typedef struct {
gint op;
gint cl, cr, ct, cb;
gint bw;
guint bc;
gint bo;
gint br;
} PbStyle;
typedef struct { gint cx, cy, cw, ch, x, y; } PbPlace;
static void pb_style_default(PbStyle *st) {
st->op = 100;
st->cl = st->cr = st->ct = st->cb = 0;
st->bw = 0;
st->bc = 0xffffffu;
st->bo = 100;
st->br = 0;
}
static gboolean pb_tok_is(const char *s, const char *e, const char *lit) {
while (s < e && *lit && *s == *lit) {
s++;
lit++;
}
return s == e && *lit == 0;
}
static void pb_tok_trim(const char **s, const char **e) {
while (*s < *e && (**s == ' ' || **s == '\t'))
(*s)++;
while (*e > *s && ((*e)[-1] == ' ' || (*e)[-1] == '\t'))
(*e)--;
}
static gboolean pb_tok_num(const char *s, const char *e, gint *out) {
gboolean neg = FALSE;
gint v = 0;
if (s < e && (*s == '-' || *s == '+')) {
neg = (*s == '-');
s++;
}
if (s >= e)
return FALSE;
for (; s < e; s++) {
if (*s < '0' || *s > '9')
return FALSE;
if (v < 1000000)
v = v * 10 + (*s - '0');
}
*out = neg ? -v : v;
return TRUE;
}
static gboolean pb_tok_hex6(const char *s, const char *e, guint *out) {
guint v = 0;
if (e - s != 6)
return FALSE;
for (; s < e; s++) {
guint d;
if (*s >= '0' && *s <= '9') d = (guint) (*s - '0');
else if (*s >= 'a' && *s <= 'f') d = (guint) (*s - 'a' + 10);
else if (*s >= 'A' && *s <= 'F') d = (guint) (*s - 'A' + 10);
else return FALSE;
v = (v << 4) | d;
}
*out = v;
return TRUE;
}
#define PB_CLAMP(v, lo, hi) MIN(MAX((v), (lo)), (hi))
static void pb_style_parse(const char *text, PbStyle *st) {
pb_style_default(st);
if (!text)
return;
const char *p = text;
while (*p) {
const char *ts = p;
while (*p && *p != ',')
p++;
const char *te = p;
if (*p == ',')
p++;
const char *eq = ts;
while (eq < te && *eq != '=')
eq++;
if (eq == te)
continue;
const char *ks = ts, *ke = eq, *vs = eq + 1, *ve = te;
pb_tok_trim(&ks, &ke);
pb_tok_trim(&vs, &ve);
gint n = 0;
guint rgb = 0;
if (pb_tok_is(ks, ke, "bc")) {
if (pb_tok_hex6(vs, ve, &rgb))
st->bc = rgb;
} else if (pb_tok_num(vs, ve, &n)) {
if (pb_tok_is(ks, ke, "op")) st->op = PB_CLAMP(n, 0, 100);
else if (pb_tok_is(ks, ke, "cl")) st->cl = PB_CLAMP(n, 0, 1900);
else if (pb_tok_is(ks, ke, "cr")) st->cr = PB_CLAMP(n, 0, 1900);
else if (pb_tok_is(ks, ke, "ct")) st->ct = PB_CLAMP(n, 0, 1900);
else if (pb_tok_is(ks, ke, "cb")) st->cb = PB_CLAMP(n, 0, 1900);
else if (pb_tok_is(ks, ke, "bw")) st->bw = PB_CLAMP(n, 0, 64);
else if (pb_tok_is(ks, ke, "bo")) st->bo = PB_CLAMP(n, 0, 100);
else if (pb_tok_is(ks, ke, "br")) st->br = PB_CLAMP(n, 0, 200);
}
}
}
static gboolean pb_style_crop(const PbStyle *st, gint sw, gint sh, gint *cx, gint *cy, gint *cw, gint *ch) {
*cx = 0;
*cy = 0;
*cw = sw;
*ch = sh;
if (st->cl <= 0 && st->cr <= 0 && st->ct <= 0 && st->cb <= 0)
return FALSE;
const gint l = ((st->cl * sw + 960) / 1920) & ~1, r = ((st->cr * sw + 960) / 1920) & ~1;
const gint t = ((st->ct * sh + 540) / 1080) & ~1, b = ((st->cb * sh + 540) / 1080) & ~1;
if (l == 0 && r == 0 && t == 0 && b == 0)
return FALSE;
const gint w = (sw - l - r) & ~1, h = (sh - t - b) & ~1;
if (w < 16 || h < 16)
return FALSE;
*cx = l;
*cy = t;
*cw = w;
*ch = h;
return TRUE;
}
static gboolean pb_place(const PbStyle *st, gint mw, gint mh, gint pw, gint ph, guint corner, gint fx, gint fy, PbPlace *pl) {
if (st->op <= 0)
return FALSE;
pb_style_crop(st, pw, ph, &pl->cx, &pl->cy, &pl->cw, &pl->ch);
if (pl->cw > mw || pl->ch > mh)
return FALSE;
const gint margin = (mw / 60) & ~1;
pb_pos(corner, mw, mh, pl->cw, pl->ch, margin, fx, fy, &pl->x, &pl->y);
pl->x &= ~1;
pl->y &= ~1;
return pl->x >= 0 && pl->y >= 0 && pl->x + pl->cw <= mw && pl->y + pl->ch <= mh;
}
static void pb_rgb_to_nv12(guint rgb, gint *y, gint *u, gint *v) {
const gint r = (gint) ((rgb >> 16) & 0xffu), g = (gint) ((rgb >> 8) & 0xffu), b = (gint) (rgb & 0xffu);
const gint den = 255 * 10000;
*y = 16 + (219 * (2126 * r + 7152 * g + 722 * b) + den / 2) / den;
*u = (128 * den + 224 * (-1146 * r - 3854 * g + 5000 * b) + den / 2) / den;
*v = (128 * den + 224 * (5000 * r - 4542 * g - 458 * b) + den / 2) / den;
*y = PB_CLAMP(*y, 16, 235);
*u = PB_CLAMP(*u, 16, 240);
*v = PB_CLAMP(*v, 16, 240);
}
typedef struct {
gint cw, ch;
gint bw;
gint ro, ri;
gint op, bo;
} PbShape;
static void pb_shape_init(PbShape *s, const PbStyle *st, gint mw, gint cw, gint ch) {
const gint lim = MIN(cw, ch) / 2;
s->cw = cw;
s->ch = ch;
s->bw = s->ro = s->ri = 0;
if (st->bw > 0)
s->bw = MIN(MAX((st->bw * mw + 960) / 1920, 1), lim);
if (st->br > 0) {
s->ro = MIN((st->br * mw + 960) / 1920, lim);
s->ri = MAX(s->ro - s->bw, 0);
}
s->op = (PB_CLAMP(st->op, 0, 100) * PB_ONE + 50) / 100;
s->bo = (PB_CLAMP(st->bo, 0, 100) * PB_ONE + 50) / 100;
}
static gint64 pb_isqrt(gint64 v) {
gint64 r = 0, bit = (gint64) 1 << 62;
while (bit > v)
bit >>= 2;
while (bit != 0) {
if (v >= r + bit) {
10BC8
v -= r + bit;
r = (r >> 1) + bit;
} else {
r >>= 1;
}
bit >>= 2;
}
return r;
}
static gint pb_corner_cov(gint fx, gint fy, gint r) {
const gint64 ux = 2 * (gint64) (r - fx) - 1, uy = 2 * (gint64) (r - fy) - 1;
const gint64 d2 = ux * ux + uy * uy;
const gint64 in = 2 * (gint64) r - 1, out = 2 * (gint64) r + 1;
if (d2 <= in * in)
return PB_ONE;
if (d2 >= out * out)
return 0;
const gint64 s = pb_isqrt(d2 << (2 * PB_SHIFT));
const gint64 c = (out * PB_ONE - s + 1) / 2;
return (gint) PB_CLAMP(c, 0, PB_ONE);
}
static void pb_shape_px(const PbShape *s, gint x, gint y, gint *wb, gint *a) {
const gint fx = MIN(x, s->cw - 1 - x), fy = MIN(y, s->ch - 1 - y);
gint ao = PB_ONE, ai = PB_ONE;
if (s->ro > 0 && fx < s->ro && fy < s->ro)
ao = pb_corner_cov(fx, fy, s->ro);
if (s->bw > 0) {
const gint ix = fx - s->bw, iy = fy - s->bw;
if (ix < 0 || iy < 0)
ai = 0;
else if (s->ri > 0 && ix < s->ri && iy < s->ri)
ai = pb_corner_cov(ix, iy, s->ri);
}
*a = (s->op * ao + PB_ONE / 2) >> PB_SHIFT;
*wb = (s->bo * (PB_ONE - ai) + PB_ONE / 2) >> PB_SHIFT;
}
static gint pb_row_zone(const PbShape *s, gint y, gint *wb, gint *a) {
const gint fy = MIN(y, s->ch - 1 - y);
gint zone = 0;
*a = s->op;
*wb = 0;
if (s->bw <= 0) {
if (fy < s->ro)
zone = s->ro;
return zone;
}
if (fy < s->bw) {
*wb = s->bo;
if (fy < s->ro)
zone = s->ro;
} else {
zone = s->bw;
if (fy < s->ro)
zone = MAX(zone, s->ro);
if (s->ri > 0 && fy - s->bw < s->ri)
zone = MAX(zone, s->bw + s->ri);
}
return zone;
}
static gint pb_mix(gint d, gint p, gint b, gint wb, gint a) {
return (d * (PB_ONE - a) * PB_ONE + (p * (PB_ONE - wb) + b * wb) * a + PB_HALF) >> (2 * PB_SHIFT);
}
static void pb_y_vec(guint8 *dst, const guint8 *src, gint n, gint b, const gint *wbv, const gint *av, gint wc, gint ac) {
gint i = 0;
while (i < n) {
while (i < n && (av ? av[i] : ac) <= 0)
i++;
gint j = i;
gboolean rd = FALSE;
while (j < n && (av ? av[j] : ac) > 0) {
if ((av ? av[j] : ac) < PB_ONE)
rd = TRUE;
j++;
}
if (j > i) {
guint8 buf[PB_CHUNK];
if (rd)
memcpy(buf, dst + i, (gsize) (j - i));
for (gint k = i; k < j; k++) {
const gint a = av ? av[k] : ac, wb = wbv ? wbv[k] : wc;
buf[k - i] = (guint8) pb_mix(rd ? buf[k - i] : 0, src[k], b, wb, a);
}
memcpy(dst + i, buf, (gsize) (j - i));
}
i = j;
}
}
static void pb_uv_vec(guint8 *dst, const guint8 *src, gint n, gint bu, gint bv, const gint *wbv, const gint *av, gint wc, gint ac) {
gint i = 0;
while (i < n) {
while (i < n && (av ? av[i] : ac) <= 0)
i++;
gint j = i;
gboolean rd = FALSE;
while (j < n && (av ? av[j] : ac) > 0) {
if ((av ? av[j] : ac) < PB_ONE)
rd = TRUE;
j++;
}
if (j > i) {
guint8 buf[2 * PB_CHUNK];
if (rd)
memcpy(buf, dst + 2 * i, (gsize) (j - i) * 2);
for (gint k = i; k < j; k++) {
const gint a = av ? av[k] : ac, wb = wbv ? wbv[k] : wc, o = 2 * (k - i);
buf[o] = (guint8) pb_mix(rd ? buf[o] : 0, src[2 * k], bu, wb, a);
buf[o + 1] = (guint8) pb_mix(rd ? buf[o + 1] : 0, src[2 * k + 1], bv, wb, a);
}
memcpy(dst + 2 * i, buf, (gsize) (j - i) * 2);
}
i = j;
}
}
static void pb_y_run(guint8 *dst, const guint8 *src, gint n, gint b, gint wb, gint a) {
if (n <= 0 || a <= 0)
return;
if (a >= PB_ONE && wb <= 0) {
memcpy(dst, src, (gsize) n);
return;
}
if (a >= PB_ONE && wb >= PB_ONE) {
memset(dst, b, (gsize) n);
return;
}
for (gint i = 0; i < n; i += PB_CHUNK)
pb_y_vec(dst + i, src + i, MIN(PB_CHUNK, n - i), b, NULL, NULL, wb, a);
}
static void pb_uv_run(guint8 *dst, const guint8 *src, gint n, gint bu, gint bv, gint wb, gint a) {
if (n <= 0 || a <= 0)
return;
if (a >= PB_ONE && wb <= 0) {
memcpy(dst, src, (gsize) n * 2);
return;
}
for (gint i = 0; i < n; i += PB_CHUNK)
pb_uv_vec(dst + 2 * i, src + 2 * i, MIN(PB_CHUNK, n - i), bu, bv, NULL, NULL, wb, a);
}
static void pb_y_edge(const PbShape *s, gint y, gint x0, gint x1, guint8 *d, const guint8 *p, gint b) {
gint wbv[PB_CHUNK], av[PB_CHUNK];
for (gint x = x0; x < x1; x += PB_CHUNK) {
const gint n = MIN(PB_CHUNK, x1 - x);
for (gint i = 0; i < n; i++)
pb_shape_px(s, x + i, y, &wbv[i], &av[i]);
pb_y_vec(d + x, p + x, n, b, wbv, av, 0, 0);
}
}
static void pb_uv_edge(const PbShape *s, gint y, gint k0, gint k1, guint8 *d, const guint8 *p, gint bu, gint bv) {
gint wbv[PB_CHUNK], av[PB_CHUNK];
for (gint k = k0; k < k1; k += PB_CHUNK) {
const gint n = MIN(PB_CHUNK, k1 - k);
for (gint i = 0; i < n; i++) {
gint w4 = 0, a4 = 0;
for (gint q = 0; q < 4; q++) {
gint w, a;
pb_shape_px(s, 2 * (k + i) + (q & 1), y + (q >> 1), &w, &a);
w4 += w;
a4 += a;
}
wbv[i] = (w4 + 2) >> 2;
av[i] = (a4 + 2) >> 2;
}
pb_uv_vec(d + 2 * k, p + 2 * k, n, bu, bv, wbv, av, 0, 0);
}
}
static void pb_draw_picture(guint8 *dy, gint ystride, guint8 *duv, gint uvstride, gint mw,
const guint8 *src, gint pw, gint ph, const PbPlace *pl, const PbStyle *st) {
if (st->op <= 0)
return;
const gint cx = pl->cx, cy = pl->cy, x = pl->x, y = pl->y;
const guint8 *suv = src + (gsize) pw * ph;
PbShape s;
pb_shape_init(&s, st, mw, pl->cw & ~1, pl->ch & ~1);
if (st->op >= 100 && s.bw <= 0 && s.ro <= 0) {
for (gint r = 0; r < pl->ch; r++)
memcpy(dy + (gsize) (y + r) * ystride + x, src + (gsize) (cy + r) * pw + cx, (gsize) pl->cw);
for (gint r = 0; r < pl->ch / 2; r++)
memcpy(duv + (gsize) (y / 2 + r) * uvstride + x, suv + (gsize) (cy / 2 + r) * pw + cx, (gsize) pl->cw);
return;
}
gint yb, ub, vb;
pb_rgb_to_nv12(st->bc, &yb, &ub, &vb);
for (gint r = 0; r < s.ch; r++) {
gint wb, a;
const gint zone = pb_row_zone(&s, r, &wb, &a);
guint8 *d = dy + (gsize) (y + r) * ystride + x;
const guint8 *p = src + (gsize) (cy + r) * pw + cx;
const gint le = MIN(zone, s.cw), rs = MAX(s.cw - zone, le);
pb_y_edge(&s, r, 0, le, d, p, yb);
pb_y_run(d + le, p + le, rs - le, yb, wb, a);
pb_y_edge(&s, r, rs, s.cw, d, p, yb);
}
const gint np = s.cw / 2;
for (gint m = 0; m < s.ch / 2; m++) {
gint wb0, a0, wb1, a1;
const gint z0 = pb_row_zone(&s, 2 * m, &wb0, &a0), z1 = pb_row_zone(&s, 2 * m + 1, &wb1, &a1);
const gint zone = MAX(z0, z1);
guint8 *d = duv + (gsize) (y / 2 + m) * uvstride + x;
const guint8 *p = suv + (gsize) (cy / 2 + m) * pw + cx;
const gint kz = (zone + 1) / 2, le = MIN(kz, np), rs = MAX(np - kz, le);
pb_uv_edge(&s, 2 * m, 0, le, d, p, ub, vb);
pb_uv_run(d + 2 * le, p + 2 * le, rs - le, ub, vb, (wb0 + wb1 + 1) >> 1, (a0 + a1 + 1) >> 1);
pb_uv_edge(&s, 2 * m, rs, np, d, p, ub, vb);
}
}
typedef struct { GstVideoFilter parent; guint corner; guint slot; gint slot2; guint corner2; gint slot3; guint corner3; guint fx, fy, fx2, fy2, fx3, fy3; gint follow_tag; PbStyle style[3]; gchar *style_txt[3]; } PbPipMix;
typedef struct { GstVideoFilterClass parent_class; } PbPipMixClass;
G_DEFINE_TYPE(PbPipMix, pb_pip_mix, GST_TYPE_VIDEO_FILTER)
enum { PROP_0, PROP_CORNER, PROP_WIDTH_PCT, PROP_SLOT, PROP_SLOT2, PROP_CORNER2, PROP_SLOT3, PROP_CORNER3, PROP_X, PROP_Y, PROP_X2, PROP_Y2, PROP_X3, PROP_Y3, PROP_FOLLOW,
PROP_STYLE1, PROP_STYLE2, PROP_STYLE3 };
static GstStaticPadTemplate mix_sink = GST_STATIC_PAD_TEMPLATE(
"sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS("video/x-raw, format=(string)NV12"));
static GstStaticPadTemplate mix_src = GST_STATIC_PAD_TEMPLATE(
"src", GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS("video/x-raw, format=(string)NV12"));
static void pb_mix_set_property(GObject *o, guint id, const GValue *v, GParamSpec *ps) {
PbPipMix *self = (PbPipMix *) o;
switch (id) {
case PROP_CORNER: g_atomic_int_set(&self->corner, MIN(g_value_get_uint(v), 5u)); break;
case PROP_WIDTH_PCT: break;
case PROP_SLOT: self->slot = g_value_get_uint(v) % NSLOTS; break;
case PROP_SLOT2: self->slot2 = g_value_get_int(v) < 0 ? -1 : g_value_get_int(v) % NSLOTS; break;
case PROP_CORNER2: g_atomic_int_set(&self->corner2, MIN(g_value_get_uint(v), 5u)); break;
case PROP_X: g_atomic_int_set(&self->fx, MIN(g_value_get_uint(v), 1000u)); break;
case PROP_Y: g_atomic_int_set(&self->fy, MIN(g_value_get_uint(v), 1000u)); break;
case PROP_X2: g_atomic_int_set(&self->fx2, MIN(g_value_get_uint(v), 1000u)); break;
case PROP_Y2: g_atomic_int_set(&self->fy2, MIN(g_value_get_uint(v), 1000u)); break;
case PROP_X3: g_atomic_int_set(&self->fx3, MIN(g_value_get_uint(v), 1000u)); break;
case PROP_Y3: g_atomic_int_set(&self->fy3, MIN(g_value_get_uint(v), 1000u)); break;
case PROP_SLOT3: self->slot3 = g_value_get_int(v) < 0 ? -1 : g_value_get_int(v) % NSLOTS; break;
case PROP_CORNER3: g_atomic_int_set(&self->corner3, MIN(g_value_get_uint(v), 5u)); break;
case PROP_FOLLOW: g_atomic_int_set(&self->follow_tag, g_value_get_boolean(v) ? 1 : 0); break;
case PROP_STYLE1: case PROP_STYLE2: case PROP_STYLE3: {
const guint i = id - PROP_STYLE1;
g_mutex_lock(&ring_lock);
g_free(self->style_txt[i]);
self->style_txt[i] = g_value_dup_string(v);
pb_style_parse(self->style_txt[i], &self->style[i]);
g_mutex_unlock(&ring_lock);
break;
}
default: G_OBJECT_WARN_INVALID_PROPERTY_ID(o, id, ps);
}
}
static void pb_mix_get_property(GObject *o, guint id, GValue *v, GParamSpec *ps) {
PbPipMix *self = (PbPipMix *) o;
switch (id) {
case PROP_CORNER: g_value_set_uint(v, g_atomic_int_get(&self->corner)); break;
case PROP_WIDTH_PCT: g_value_set_uint(v, 0); break;
case PROP_SLOT: g_value_set_uint(v, self->slot); break;
case PROP_SLOT2: g_value_set_int(v, self->slot2); break;
case PROP_CORNER2: g_value_set_uint(v, g_atomic_int_get(&self->corner2)); break;
case PROP_X: g_value_set_uint(v, g_atomic_int_get(&self->fx)); break;
case PROP_Y: g_value_set_uint(v, g_atomic_int_get(&self->fy)); break;
case PROP_X2: g_value_set_uint(v, g_atomic_int_get(&self->fx2)); break;
case PROP_Y2: g_value_set_uint(v, g_atomic_int_get(&self->fy2)); break;
case PROP_X3: g_value_set_uint(v, g_atomic_int_get(&self->fx3)); break;
case PROP_Y3: g_value_set_uint(v, g_atomic_int_get(&self->fy3)); break;
case PROP_SLOT3: g_value_set_int(v, self->slot3); break;
case PROP_CORNER3: g_value_set_uint(v, g_atomic_int_get(&self->corner3)); break;
case PROP_FOLLOW: g_value_set_boolean(v, g_atomic_int_get(&self->follow_tag)); break;
case PROP_STYLE1: case PROP_STYLE2: case PROP_STYLE3: {
const guint i = id - PROP_STYLE1;
g_mutex_lock(&ring_lock);
g_value_set_string(v, self->style_txt[i] ? self->style_txt[i] : "");
g_mutex_unlock(&ring_lock);
break;
}
default: G_OBJECT_WARN_INVALID_PROPERTY_ID(o, id, ps);
}
}
static void pb_mix_finalize(GObject *o) {
PbPipMix *self = (PbPipMix *) o;
for (guint i = 0; i < G_N_ELEMENTS(self->style_txt); i++)
g_free(self->style_txt[i]);
G_OBJECT_CLASS(pb_pip_mix_parent_class)->finalize(o);
}
static void pb_mix_draw(GstVideoFrame *frame, guint slot, guint corner, gint fx, gint fy, const PbStyle *st) {
Ring *ring = &rings[slot];
if (ring->w <= 0 || ring->slot_size == 0)
return;
if (!ring->started && ring->count >= PREBUFFER)
ring->started = TRUE;
while (ring->count > PREBUFFER + 3) {
ring->rd = (ring->rd + 1) % RING_CAP;
ring->count--;
ring->dropped++;
}
if (ring->started && ring->count > 0) {
memcpy(ring->cur, ring->slot[ring->rd], ring->slot_size);
ring->rd = (ring->rd + 1) % RING_CAP;
ring->count--;
ring->have_cur = TRUE;
ring->shown++;
} else if (ring->have_cur) {
ring->repeated++;
}
const gint64 age = g_get_monotonic_time() - ring->last_push;
const gboolean draw = ring->have_cur && age < STALE_US;
const gint pw = ring->w, ph = ring->h;
const gint mw = GST_VIDEO_FRAME_WIDTH(frame), mh = GST_VIDEO_FRAME_HEIGHT(frame);
PbPlace pl = { 0, 0, 0, 0, 0, 0 };
if (draw && pb_place(st, mw, mh, pw, ph, corner, fx, fy, &pl)) {
guint8 *dy = GST_VIDEO_FRAME_PLANE_DATA(frame, 0), *duv = GST_VIDEO_FRAME_PLANE_DATA(frame, 1);
const gint sy = GST_VIDEO_FRAME_PLANE_STRIDE(frame, 0), suv = GST_VIDEO_FRAME_PLANE_STRIDE(frame, 1);
pb_draw_picture(dy, sy, duv, suv, mw, ring->cur, pw, ph, &pl, st);
}
}
static void pb_mix_trim(guint slot) {
Ring *ring = &rings[slot];
while (ring->count > PREBUFFER) {
ring->rd = (ring->rd + 1) % RING_CAP;
ring->count--;
ring->dropped++;
}
}
static GstFlowReturn pb_mix_transform_ip(GstVideoFilter *filter, GstVideoFrame *frame) {
PbPipMix *self = (PbPipMix *) filter;
const guint64 tag = GST_BUFFER_OFFSET(frame->buffer);
const gboolean follow = g_atomic_int_get(&self->follow_tag) && (tag >> 60) == 9;
g_mutex_lock(&ring_lock);
if (follow) {
const guint cn[3] = { g_atomic_int_get(&self->corner), g_atomic_int_get(&self->corner2), g_atomic_int_get(&self->corner3) };
const gint fx[3] = { g_atomic_int_get(&self->fx), g_atomic_int_get(&self->fx2), g_atomic_int_get(&self->fx3) };
const gint fy[3] = { g_atomic_int_get(&self->fy), g_atomic_int_get(&self->fy2), g_atomic_int_get(&self->fy3) };
guint shown = 0;
for (guint p = 0; p < 3; p++) {
const guint cam = (guint) ((tag >> (4 * (p + 1))) & 0xF);
if (cam > 3)
continue;
const guint ring = (cam + 3) % NSLOTS;
pb_mix_draw(frame, ring, cn[p], fx[p], fy[p], &self->style[p]);
shown |= 1u << ring;
}
for (guint r = 0; r < NSLOTS; r++)
if (!(shown & (1u << r)))
pb_mix_trim(r);
} else {
pb_mix_draw(frame, self->slot, g_atomic_int_get(&self->corner), g_atomic_int_get(&self->fx), g_atomic_int_get(&self->fy), &self->style[0]);
if (self->slot2 >= 0)
pb_mix_draw(frame, (guint) self->slot2, g_atomic_int_get(&self->corner2), g_atomic_int_get(&self->fx2), g_atomic_int_get(&self->fy2), &self->style[1]);
if (self->slot3 >= 0)
pb_mix_draw(frame, (guint) self->slot3, g_atomic_int_get(&self->corner3), g_atomic_int_get(&self->fx3), g_atomic_int_get(&self->fy3), &self->style[2]);
}
g_mutex_unlock(&ring_lock);
return GST_FLOW_OK;
}
static void pb_pip_mix_class_init(PbPipMixClass *klass) {
GObjectClass *oc = G_OBJECT_CLASS(klass);
GstElementClass *ec = GST_ELEMENT_CLASS(klass);
GstVideoFilterClass *vc = GST_VIDEO_FILTER_CLASS(klass);
oc->set_property = pb_mix_set_property;
oc->get_property = pb_mix_get_property;
oc->finalize = pb_mix_finalize;
g_object_class_install_property(oc, PROP_CORNER,
g_param_spec_uint("corner", "Ecke", "0 oben links, 1 oben rechts, 2 unten links, 3 unten rechts, 4 unten Mitte, 5 frei (x/y)",
0, 5, 3, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_PLAYING));
g_object_class_install_property(oc, PROP_WIDTH_PCT,
g_param_spec_uint("width-pct", "Breite in Prozent", "nur zur Information (Größe liefert der Decoder)",
0, 100, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property(oc, PROP_SLOT,
g_param_spec_uint("slot", "Platz", "welches kleine Bild (0 bis 2)", 0, NSLOTS - 1, 0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
g_object_class_install_property(oc, PROP_SLOT2,
g_param_spec_int("slot2", "Zweiter Platz", "zweites kleines Bild im selben Durchgang (-1 = keins)", -1, NSLOTS - 1, -1,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
g_object_class_install_property(oc, PROP_CORNER2,
g_param_spec_uint("corner2", "Ecke 2", "Ecke des zweiten kleinen Bildes", 0, 5, 2,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_PLAYING));
{
static const struct { guint id; const gchar *name; const gchar *nick; } fp[] = {
{PROP_X, "x", "X"}, {PROP_Y, "y", "Y"}, {PROP_X2, "x2", "X2"}, {PROP_Y2, "y2", "Y2"}, {PROP_X3, "x3", "X3"}, {PROP_Y3, "y3", "Y3"}};
for (guint i = 0; i < G_N_ELEMENTS(fp); i++)
g_object_class_install_property(oc, fp[i].id,
g_param_spec_uint(fp[i].name, fp[i].nick, "freie Position in Promille des Verschiebewegs (nur bei Ecke 5)", 0, 1000, 0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_PLAYING));
}
g_object_class_install_property(oc, PROP_SLOT3,
g_param_spec_int("slot3", "Dritter Platz", "drittes kleines Bild im selben Durchgang (-1 = keins)", -1, NSLOTS - 1, -1,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
g_object_class_install_property(oc, PROP_CORNER3,
g_param_spec_uint("corner3", "Ecke 3", "Ecke des dritten kleinen Bildes", 0, 5, 0,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_PLAYING));
g_object_class_install_property(oc, PROP_FOLLOW,
g_param_spec_boolean("follow-tag", "Zustand aus dem Bild", "kleine Bilder nach dem Zustand zeigen, den pbpipsel ins Bild schreibt (Tausch ohne Unterbrechung)",
FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_PLAYING));
{
static const struct { guint id; const gchar *name; const gchar *nick; } sp[] = {
{PROP_STYLE1, "style1", "Stil 1"}, {PROP_STYLE2, "style2", "Stil 2"}, {PROP_STYLE3, "style3", "Stil 3"}};
for (guint i = 0; i < G_N_ELEMENTS(sp); i++)
g_object_class_install_property(oc, sp[i].id,
g_param_spec_string(sp[i].name, sp[i].nick,
"Gestaltung des kleinen Bildes, kommagetrennt: op (Deckkraft %), cl/cr/ct/cb (Beschnitt, Bezug 1920x1080), bw (Rahmenbreite), bc (Rahmenfarbe rrggbb), bo (Rahmendeckkraft %), br (Eckenradius); leer = unverändert",
"", G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_PLAYING));
}
gst_element_class_set_static_metadata(ec, "IRL4YOU PiP Einblendung", "Filter/Effect/Video",
"Schreibt das kleine Bild in das Hauptbild, ohne dieses zu lesen", "IRL4YOU");
gst_element_class_add_static_pad_template(ec, &mix_sink);
gst_element_class_add_static_pad_template(ec, &mix_src);
vc->transform_frame_ip = pb_mix_transform_ip;
}
static void pb_pip_mix_init(PbPipMix *self) {
self->corner = 3;
self->slot2 = -1;
self->corner2 = 2;
self->slot3 = -1;
self->corner3 = 0;
for (guint i = 0; i < G_N_ELEMENTS(self->style); i++)
pb_style_default(&self->style[i]);
gst_base_transform_set_in_place(GST_BASE_TRANSFORM(self), TRUE);
}
#define RD_PIPS 3
#define RD_HIST 6
#define RD_MAXR 200
#define RD_STALE_NS G_GUINT64_CONSTANT(1000000000)
typedef struct {
gboolean on;
gint x, y, w, h;
PbStyle st;
} RdPip;
typedef struct {
gboolean ok;
gint x, y, w, h, r;
guint8 ys[4][RD_MAXR * RD_MAXR];
guint8 uvs[4][(RD_MAXR / 2) * RD_MAXR];
} RdCopy;
typedef struct {
guint64 rt;
guint64 seq;
gboolean valid;
RdCopy c[RD_PIPS];
} RdEntry;
static GMutex rd_lock;
static RdPip rd_pip[RD_PIPS];
static RdEntry rd_hist[RD_HIST];
static gint rd_wr = 0;
static guint64 rd_seq = 0;
static gint rd_active = 0;
static gint rd_radius(const RdPip *p) {
PbShape s;
pb_shape_init(&s, &p->st, 1920, p->w, p->h);
return (s.ro + 1) & ~1;
}
typedef struct { GstBaseTransform parent; GstVideoInfo info; gboolean have_info, active; } PbTap;
typedef struct { GstBaseTransformClass parent_class; } PbTapClass;
G_DEFINE_TYPE(PbTap, pb_tap, GST_TYPE_BASE_TRANSFORM)
enum { PROP_TAP0, PROP_TAP_ACTIVE };
static GstStaticPadTemplate tap_sink = GST_STATIC_PAD_TEMPLATE("sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS("video/x-raw, format=(string)NV12"));
static GstStaticPadTemplate tap_src = GST_STATIC_PAD_TEMPLATE("src", GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS("video/x-raw, format=(string)NV12"));
static void pb_tap_set_property(GObject *o, guint id, const GValue *v, GParamSpec *ps) {
PbTap *self = (PbTap *) o;
if (id != PROP_TAP_ACTIVE) {
G_OBJECT_WARN_INVALID_PROPERTY_ID(o, id, ps);
return;
}
const gboolean now = g_value_get_boolean(v);
g_mutex_lock(&rd_lock);
if (now && !self->active)
rd_active++;
else if (!now && self->active)
rd_active--;
self->active = now;
g_mutex_unlock(&rd_lock);
}
static void pb_tap_get_property(GObject *o, guint id, GValue *v, GParamSpec *ps) {
PbTap *self = (PbTap *) o;
if (id == PROP_TAP_ACTIVE)
g_value_set_boolean(v, self->active);
else
G_OBJECT_WARN_INVALID_PROPERTY_ID(o, id, ps);
}
static void pb_tap_finalize(GObject *o) {
PbTap *self = (PbTap *) o;
if (self->active) {
g_mutex_lock(&rd_lock);
rd_active--;
self->active = FALSE;
g_mutex_unlock(&rd_lock);
}
G_OBJECT_CLASS(pb_tap_parent_class)->finalize(o);
}
static gboolean pb_tap_set_caps(GstBaseTransform *t, GstCaps *in, GstCaps *out) {
PbTap *self = (PbTap *) t;
self->have_info = gst_video_info_from_caps(&self->info, in);
return TRUE;
}
static GstFlowReturn pb_tap_transform_ip(GstBaseTransform *t, GstBuffer *buf) {
PbTap *self = (PbTap *) t;
if (!self->active || !self->have_info)
return GST_FLOW_OK;
RdPip pips[RD_PIPS];
gint slot = -1;
g_mutex_lock(&rd_lock);
memcpy(pips, rd_pip, sizeof pips);
slot = rd_wr;
rd_wr = (rd_wr + 1) % RD_HIST;
rd_hist[slot].valid = FALSE;
g_mutex_unlock(&rd_lock);
GstVideoFrame fr;
if (!gst_video_frame_map(&fr, &self->info, buf, GST_MAP_READ))
return GST_FLOW_OK;
const gint fw = GST_VIDEO_FRAME_WIDTH(&fr), fh = GST_VIDEO_FRAME_HEIGHT(&fr);
const guint8 *py = GST_VIDEO_FRAME_PLANE_DATA(&fr, 0), *puv = GST_VIDEO_FRAME_PLANE_DATA(&fr, 1);
const gint sy = GST_VIDEO_FRAME_PLANE_STRIDE(&fr, 0), suv = GST_VIDEO_FRAME_PLANE_STRIDE(&fr, 1);
RdEntry *e = &rd_hist[slot];
for (gint i = 0; i < RD_PIPS; i++) {
RdCopy *c = &e->c[i];
const RdPip *p = &pips[i];
c->ok = FALSE;
if (!p->on || p->w < 16 || p->h < 16 || p->x < 0 || p->y < 0 || p->x + p->w > fw || p->y + p->h > fh)
continue;
const gint r = rd_radius(p);
if (r <= 0 || r > RD_MAXR || r > p->w || r > p->h)
continue;
for (gint q = 0; q < 4; q++) {
const gint x0 = p->x + ((q & 1) ? p->w - r : 0), y0 = p->y + ((q & 2) ? p->h - r : 0);
for (gint k = 0; k < r; k++)
memcpy(c->ys[q] + (gsize) k * r, py + (gsize) (y0 + k) * sy + x0, (gsize) r);
for (gint k = 0; k < r / 2; k++)
memcpy(c->uvs[q] + (gsize) k * r, puv + (gsize) (y0 / 2 + k) * suv + x0, (gsize) r);
}
c->x = p->x; c->y = p->y; c->w = p->w; c->h = p->h; c->r = r;
c->ok = TRUE;
}
gst_video_frame_unmap(&fr);
g_mutex_lock(&rd_lock);
e->rt = GST_BUFFER_PTS_IS_VALID(buf) ? gst_segment_to_running_time(&t->segment, GST_FORMAT_TIME, GST_BUFFER_PTS(buf)) : GST_CLOCK_TIME_NONE;
e->seq = ++rd_seq;
e->valid = TRUE;
g_mutex_unlock(&rd_lock);
return GST_FLOW_OK;
}
static void pb_tap_class_init(PbTapClass *klass) {
GObjectClass *oc = G_OBJECT_CLASS(klass);
GstElementClass *ec = GST_ELEMENT_CLASS(klass);
GstBaseTransformClass *bc = GST_BASE_TRANSFORM_CLASS(klass);
oc->set_property = pb_tap_set_property;
oc->get_property = pb_tap_get_property;
oc->finalize = pb_tap_finalize;
g_object_class_install_property(oc, PROP_TAP_ACTIVE,
g_param_spec_boolean("active", "Aktiv", "dieser Zweig liefert gerade das Hauptbild: Ecken der gerundeten kleinen Bilder sichern",
FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_PLAYING));
gst_element_class_set_static_metadata(ec, "IRL4YOU Ecken sichern", "Filter/Video", "Sichert die Ecken unter gerundeten kleinen Bildern", "IRL4YOU");
gst_element_class_add_static_pad_template(ec, &tap_sink);
gst_element_class_add_static_pad_template(ec, &tap_src);
bc->set_caps = pb_tap_set_caps;
bc->transform_ip = pb_tap_transform_ip;
bc->transform_ip_on_passthrough = TRUE;
}
static void pb_tap_init(PbTap *self) {
self->have_info = FALSE;
self->active = FALSE;
gst_base_transform_set_in_place(GST_BASE_TRANSFORM(self), TRUE);
gst_base_transform_set_passthrough(GST_BASE_TRANSFORM(self), TRUE);
}
typedef struct { GstVideoFilter parent; } PbRound;
typedef struct { GstVideoFilterClass parent_class; } PbRoundClass;
G_DEFINE_TYPE(PbRound, pb_round, GST_TYPE_VIDEO_FILTER)
enum { PROP_R0, PROP_RPIP1, PROP_RPIP2, PROP_RPIP3 };
static GstStaticPadTemplate rnd_sink = GST_STATIC_PAD_TEMPLATE("sink", GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS("video/x-raw, format=(string)NV12"));
static GstStaticPadTemplate rnd_src = GST_STATIC_PAD_TEMPLATE("src", GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS("video/x-raw, format=(string)NV12"));
static void pb_round_set_property(GObject *o, guint id, const GValue *v, GParamSpec *ps) {
if (id < PROP_RPIP1 || id > PROP_RPIP3) {
G_OBJECT_WARN_INVALID_PROPERTY_ID(o, id, ps);
return;
}
RdPip np;
memset(&np, 0, sizeof np);
pb_style_default(&np.st);
const gchar *txt = g_value_get_string(v);
gint x, y, w, h, bw, bo, br, op;
guint bc;
if (txt && sscanf(txt, "%d,%d,%d,%d,%d,%x,%d,%d,%d", &x, &y, &w, &h, &bw, &bc, &bo, &br, &op) == 9 && w >= 16 && h >= 16) {
np.on = TRUE;
np.x = x & ~1; np.y = y & ~1; np.w = w & ~1; np.h = h & ~1;
np.st.bw = PB_CLAMP(bw, 0, 64);
np.st.bc = bc & 0xffffffu;
np.st.bo = PB_CLAMP(bo, 0, 100);
np.st.br = PB_CLAMP(br, 0, 200);
np.st.op = PB_CLAMP(op, 0, 100);
}
g_mutex_lock(&rd_lock);
rd_pip[id - PROP_RPIP1] = np;
g_mutex_unlock(&rd_lock);
}
static void pb_round_get_property(GObject *o, guint id, GValue *v, GParamSpec *ps) {
if (id < PROP_RPIP1 || id > PROP_RPIP3) {
G_OBJECT_WARN_INVALID_PROPERTY_ID(o, id, ps);
return;
}
gchar buf[128] = "off";
g_mutex_lock(&rd_lock);
const RdPip *p = &rd_pip[id - PROP_RPIP1];