-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathgstpispconvert.cpp
More file actions
1494 lines (1265 loc) · 46.3 KB
/
gstpispconvert.cpp
File metadata and controls
1494 lines (1265 loc) · 46.3 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
/* SPDX-License-Identifier: BSD-2-Clause */
/*
* Copyright (C) 2026 Raspberry Pi Ltd
*
* gstpispconvert.cpp - GStreamer element for PiSP hardware conversion
*/
#include "gstpispconvert.h"
#include <algorithm>
#include <cerrno>
#include <cstdint>
#include <cstring>
#include <map>
#include <optional>
#include <string>
#include <unistd.h>
#include <gst/allocators/gstdmabuf.h>
#include <gst/video/video.h>
#include "common/logging.hpp"
#include "common/utils.hpp"
#include "helpers/v4l2_device.hpp"
#include "variants/variant.hpp"
using BufferRef = libpisp::helpers::BufferRef;
using Buffer = libpisp::helpers::Buffer;
GST_DEBUG_CATEGORY_STATIC(gst_pisp_convert_debug);
#define GST_CAT_DEFAULT gst_pisp_convert_debug
/* Supported GStreamer formats */
#define PISP_FORMATS "{ RGB, RGBx, BGRx, I420, YV12, Y42B, Y444, YUY2, UYVY, NV12, NV12_128C8, NV12_10LE32_128C8 }"
/* Supported DRM fourccs */
#define PISP_DRM_FORMATS \
"{ RG24, XB24, XR24, YU12, YV12, YU16, YU24, YUYV, UYVY, NV12, NV12:0x0700000000000004, P030:0x0700000000000004 }"
#define PISP_SRC_CAPS \
"video/x-raw(memory:DMABuf), format=(string)DMA_DRM, drm-format=(string)" PISP_DRM_FORMATS \
", width=(int)[1,32768], height=(int)[1,32768], framerate=(fraction)[0/1,2147483647/1]" \
";" GST_VIDEO_CAPS_MAKE_WITH_FEATURES(GST_CAPS_FEATURE_MEMORY_DMABUF, \
PISP_FORMATS) ";" GST_VIDEO_CAPS_MAKE(PISP_FORMATS)
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE(
"sink", GST_PAD_SINK, GST_PAD_ALWAYS,
GST_STATIC_CAPS(
/* DMA-DRM format (GStreamer 1.24+) */
"video/x-raw(memory:DMABuf), format=(string)DMA_DRM, drm-format=(string)" PISP_DRM_FORMATS
", width=(int)[1,32768], height=(int)[1,32768], framerate=(fraction)[0/1,2147483647/1]"
";" /* Regular dmabuf with standard formats */
/* System memory */
GST_VIDEO_CAPS_MAKE(PISP_FORMATS)));
static GstStaticPadTemplate src0_template =
GST_STATIC_PAD_TEMPLATE("src0", GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS(PISP_SRC_CAPS));
static GstStaticPadTemplate src1_template =
GST_STATIC_PAD_TEMPLATE("src1", GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS(PISP_SRC_CAPS));
#define gst_pisp_convert_parent_class parent_class
G_DEFINE_TYPE(GstPispConvert, gst_pisp_convert, GST_TYPE_ELEMENT);
GST_ELEMENT_REGISTER_DEFINE(pispconvert, "pispconvert", GST_RANK_PRIMARY, GST_TYPE_PISP_CONVERT);
/* Bidirectional mapping between GstVideoFormat and PiSP format strings */
static const std::map<GstVideoFormat, std::string> gst_pisp_format_map = {
{ GST_VIDEO_FORMAT_RGB, "RGB888" },
{ GST_VIDEO_FORMAT_RGBx, "RGBX8888" },
{ GST_VIDEO_FORMAT_BGRx, "XRGB8888" },
{ GST_VIDEO_FORMAT_I420, "YUV420P" },
{ GST_VIDEO_FORMAT_YV12, "YVU420P" },
{ GST_VIDEO_FORMAT_Y42B, "YUV422P" },
{ GST_VIDEO_FORMAT_Y444, "YUV444P" },
{ GST_VIDEO_FORMAT_YUY2, "YUYV" },
{ GST_VIDEO_FORMAT_UYVY, "UYVY" },
{ GST_VIDEO_FORMAT_NV12, "YUV420SP" },
{ GST_VIDEO_FORMAT_NV12_128C8, "YUV420SP_COL128" },
{ GST_VIDEO_FORMAT_NV12_10LE32_128C8, "YUV420SP10_COL128" },
};
/* Bidirectional mapping between DRM fourcc and PiSP format strings */
static const std::map<std::string, std::string> drm_pisp_format_map = {
{ "RG24", "RGB888" },
{ "BG24", "RGB888" },
{ "XB24", "RGBX8888" },
{ "XR24", "XRGB8888" },
{ "YU12", "YUV420P" },
{ "YV12", "YVU420P" },
{ "YU16", "YUV422P" },
{ "YU24", "YUV444P" },
{ "YUYV", "YUYV" },
{ "UYVY", "UYVY" },
{ "NV12", "YUV420SP" },
{ "NV12:0x0700000000000004", "YUV420SP_COL128" },
{ "P030:0x0700000000000004", "YUV420SP10_COL128" },
};
static const char *gst_format_to_pisp(GstVideoFormat format)
{
auto it = gst_pisp_format_map.find(format);
return it != gst_pisp_format_map.end() ? it->second.c_str() : nullptr;
}
static GstVideoFormat pisp_to_gst_video_format(const char *pisp_format)
{
if (!pisp_format)
return GST_VIDEO_FORMAT_UNKNOWN;
for (const auto &[gst_fmt, pisp_fmt] : gst_pisp_format_map)
{
if (g_str_equal(pisp_format, pisp_fmt.c_str()))
return gst_fmt;
}
return GST_VIDEO_FORMAT_UNKNOWN;
}
static const char *drm_format_to_pisp(const gchar *drm_format)
{
if (!drm_format)
return nullptr;
auto it = drm_pisp_format_map.find(drm_format);
return it != drm_pisp_format_map.end() ? it->second.c_str() : nullptr;
}
/* Helper function to check if a PiSP format string is YUV */
static bool is_yuv_format(const char *format)
{
if (!format)
return false;
return format[0] == 'Y' || format[0] == 'U';
}
/* Map GStreamer colorimetry to PiSP colour space string.
* Returns nullptr if the colorimetry is unknown/unspecified. */
static const char *colorimetry_to_pisp(const GstVideoColorimetry *colorimetry)
{
if (!colorimetry || colorimetry->matrix == GST_VIDEO_COLOR_MATRIX_UNKNOWN)
return nullptr;
bool full_range = colorimetry->range == GST_VIDEO_COLOR_RANGE_0_255;
if (colorimetry->matrix == GST_VIDEO_COLOR_MATRIX_BT2020)
return full_range ? "bt2020_full" : "bt2020";
if (colorimetry->matrix == GST_VIDEO_COLOR_MATRIX_BT709)
return full_range ? "rec709_full" : "rec709";
if (colorimetry->matrix == GST_VIDEO_COLOR_MATRIX_BT601)
return full_range ? "jpeg" : "smpte170m";
return nullptr;
}
/* Configure colour space conversion blocks for the backend */
static uint32_t configure_colour_conversion(libpisp::BackEnd *backend, const char *in_format, const char *in_colorspace,
const char *out_format, const char *out_colorspace,
unsigned int output_index)
{
uint32_t rgb_enables = 0;
/* YUV->RGB conversion on input */
if (is_yuv_format(in_format))
{
pisp_be_ccm_config csc;
backend->InitialiseYcbcrInverse(csc, in_colorspace);
backend->SetCcm(csc);
rgb_enables |= PISP_BE_RGB_ENABLE_CCM;
}
/* RGB->YUV conversion on output */
if (is_yuv_format(out_format))
{
pisp_be_ccm_config csc;
backend->InitialiseYcbcr(csc, out_colorspace);
backend->SetCsc(output_index, csc);
rgb_enables |= PISP_BE_RGB_ENABLE_CSC(output_index);
}
else if (g_str_equal(out_format, "RGB888") || g_str_equal(out_format, "RGBX8888") ||
g_str_equal(out_format, "XRGB8888"))
{
/* R/B channel swap to match GStreamer/DRM byte ordering */
pisp_be_ccm_config csc = {};
csc.coeffs[2] = csc.coeffs[4] = csc.coeffs[6] = 1 << 10;
backend->SetCsc(output_index, csc);
rgb_enables |= PISP_BE_RGB_ENABLE_CSC(output_index);
}
return rgb_enables;
}
/* GObject vmethod implementations */
static void gst_pisp_convert_finalize(GObject *object);
/* GstElement vmethod implementations */
static GstStateChangeReturn gst_pisp_convert_change_state(GstElement *element, GstStateChange transition);
/* Pad functions */
static gboolean gst_pisp_convert_sink_event(GstPad *pad, GstObject *parent, GstEvent *event);
static GstFlowReturn gst_pisp_convert_chain(GstPad *pad, GstObject *parent, GstBuffer *buf);
static gboolean gst_pisp_convert_src_event(GstPad *pad, GstObject *parent, GstEvent *event);
static gboolean gst_pisp_convert_src_query(GstPad *pad, GstObject *parent, GstQuery *query);
static gboolean gst_pisp_convert_sink_query(GstPad *pad, GstObject *parent, GstQuery *query);
/* Internal functions */
static gboolean gst_pisp_convert_start(GstPispConvert *self);
static gboolean gst_pisp_convert_stop(GstPispConvert *self);
static gboolean gst_pisp_convert_configure(GstPispConvert *self);
static gboolean gst_pisp_convert_setup_output_pool(GstPispConvert *self, guint index);
enum
{
PROP_0,
PROP_OUTPUT_BUFFER_COUNT,
PROP_CROP,
PROP_CROP0,
PROP_CROP1,
N_PROPERTIES
};
static void gst_pisp_convert_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void gst_pisp_convert_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
/* Initialize the class */
static void gst_pisp_convert_class_init(GstPispConvertClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
GstElementClass *element_class = GST_ELEMENT_CLASS(klass);
gobject_class->finalize = gst_pisp_convert_finalize;
gobject_class->set_property = gst_pisp_convert_set_property;
gobject_class->get_property = gst_pisp_convert_get_property;
gst_element_class_set_static_metadata(element_class, "PiSP Hardware Image Converter",
"Filter/Converter/Video/Scaler",
"Hardware accelerated format conversion and scaling using libpisp",
"Raspberry Pi");
gst_element_class_add_static_pad_template(element_class, &sink_template);
gst_element_class_add_static_pad_template(element_class, &src0_template);
gst_element_class_add_static_pad_template(element_class, &src1_template);
g_object_class_install_property(
gobject_class, PROP_OUTPUT_BUFFER_COUNT,
g_param_spec_uint("output-buffer-count", "Output buffer count",
"Number of backend buffers to allocate (round-robin)", 1, 32, 4,
static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property(
gobject_class, PROP_CROP,
g_param_spec_string("crop", "Crop region for all outputs",
"Crop region as 'x,y,width,height' applied to all outputs (0,0,0,0 = full input)",
"0,0,0,0", static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property(
gobject_class, PROP_CROP0,
g_param_spec_string("crop0", "Crop region for output 0",
"Crop region as 'x,y,width,height' (0,0,0,0 = no crop / full input)", "0,0,0,0",
static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property(
gobject_class, PROP_CROP1,
g_param_spec_string("crop1", "Crop region for output 1",
"Crop region as 'x,y,width,height' (0,0,0,0 = no crop / full input)", "0,0,0,0",
static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
element_class->change_state = GST_DEBUG_FUNCPTR(gst_pisp_convert_change_state);
GST_DEBUG_CATEGORY_INIT(gst_pisp_convert_debug, "pispconvert", 0, "PiSP hardware converter");
}
static void gst_pisp_convert_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
GstPispConvert *self = GST_PISP_CONVERT(object);
switch (prop_id)
{
case PROP_OUTPUT_BUFFER_COUNT:
self->priv->output_buffer_count = g_value_get_uint(value);
break;
case PROP_CROP:
case PROP_CROP0:
case PROP_CROP1:
{
const gchar *str = g_value_get_string(value);
guint x, y, w, h;
if (str && sscanf(str, "%u,%u,%u,%u", &x, &y, &w, &h) == 4)
{
pisp_be_crop_config crop_cfg = { (uint16_t)x, (uint16_t)y, (uint16_t)w, (uint16_t)h };
if (prop_id == PROP_CROP)
{
self->priv->crop[0] = crop_cfg;
self->priv->crop[1] = crop_cfg;
}
else
{
guint idx = (prop_id == PROP_CROP0) ? 0 : 1;
self->priv->crop[idx] = crop_cfg;
}
}
else
GST_WARNING_OBJECT(self, "Invalid %s format '%s', expected 'x,y,width,height'", pspec->name, str);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void gst_pisp_convert_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
GstPispConvert *self = GST_PISP_CONVERT(object);
switch (prop_id)
{
case PROP_OUTPUT_BUFFER_COUNT:
g_value_set_uint(value, self->priv->output_buffer_count);
break;
case PROP_CROP:
case PROP_CROP0:
case PROP_CROP1:
{
guint idx = (prop_id == PROP_CROP1) ? 1 : 0;
gchar *str = g_strdup_printf("%u,%u,%u,%u", self->priv->crop[idx].offset_x, self->priv->crop[idx].offset_y,
self->priv->crop[idx].width, self->priv->crop[idx].height);
g_value_take_string(value, str);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
/* Initialize the element instance */
static void gst_pisp_convert_init(GstPispConvert *self)
{
/* Create and configure sink pad */
self->sinkpad = gst_pad_new_from_static_template(&sink_template, "sink");
gst_pad_set_chain_function(self->sinkpad, GST_DEBUG_FUNCPTR(gst_pisp_convert_chain));
gst_pad_set_event_function(self->sinkpad, GST_DEBUG_FUNCPTR(gst_pisp_convert_sink_event));
gst_pad_set_query_function(self->sinkpad, GST_DEBUG_FUNCPTR(gst_pisp_convert_sink_query));
gst_element_add_pad(GST_ELEMENT(self), self->sinkpad);
/* Create and configure src0 pad (primary output) */
self->srcpad[0] = gst_pad_new_from_static_template(&src0_template, "src0");
gst_pad_set_event_function(self->srcpad[0], GST_DEBUG_FUNCPTR(gst_pisp_convert_src_event));
gst_pad_set_query_function(self->srcpad[0], GST_DEBUG_FUNCPTR(gst_pisp_convert_src_query));
gst_element_add_pad(GST_ELEMENT(self), self->srcpad[0]);
/* Create and configure src1 pad (secondary output) */
self->srcpad[1] = gst_pad_new_from_static_template(&src1_template, "src1");
gst_pad_set_event_function(self->srcpad[1], GST_DEBUG_FUNCPTR(gst_pisp_convert_src_event));
gst_pad_set_query_function(self->srcpad[1], GST_DEBUG_FUNCPTR(gst_pisp_convert_src_query));
gst_element_add_pad(GST_ELEMENT(self), self->srcpad[1]);
/* Initialize private data */
self->priv = new GstPispConvertPrivate();
self->priv->backend_device = nullptr;
self->priv->media_dev_path = nullptr;
self->priv->configured = FALSE;
self->priv->dmabuf_allocator = gst_dmabuf_allocator_new();
self->priv->use_dmabuf_input = FALSE;
for (unsigned int i = 0; i < PISP_NUM_OUTPUTS; i++)
{
self->priv->out_width[i] = 0;
self->priv->out_height[i] = 0;
self->priv->out_stride[i] = 0;
self->priv->out_hw_stride[i] = 0;
self->priv->out_format[i] = nullptr;
self->priv->output_enabled[i] = FALSE;
self->priv->use_dmabuf_output[i] = FALSE;
self->priv->output_pool[i] = nullptr;
self->priv->src_caps[i] = nullptr;
}
self->priv->pending_segment = nullptr;
self->priv->output_buffer_count = 4;
self->priv->buffer_slice_index = 0;
}
static void gst_pisp_convert_finalize(GObject *object)
{
GstPispConvert *self = GST_PISP_CONVERT(object);
if (self->priv)
{
if (self->priv->dmabuf_allocator)
{
gst_object_unref(self->priv->dmabuf_allocator);
self->priv->dmabuf_allocator = nullptr;
}
for (unsigned int i = 0; i < PISP_NUM_OUTPUTS; i++)
{
if (self->priv->output_pool[i])
{
gst_object_unref(self->priv->output_pool[i]);
self->priv->output_pool[i] = nullptr;
}
if (self->priv->src_caps[i])
{
gst_caps_unref(self->priv->src_caps[i]);
self->priv->src_caps[i] = nullptr;
}
}
if (self->priv->pending_segment)
{
gst_event_unref(self->priv->pending_segment);
self->priv->pending_segment = nullptr;
}
g_free(self->priv->media_dev_path);
delete self->priv;
self->priv = nullptr;
}
G_OBJECT_CLASS(parent_class)->finalize(object);
}
/*
* Parse output caps for a specific output index.
* Returns TRUE if caps are valid and were parsed successfully.
*/
static gboolean parse_output_caps(GstPispConvert *self, guint index, GstCaps *caps)
{
GstVideoInfo out_info;
if (!caps || gst_caps_is_empty(caps))
return FALSE;
GstCapsFeatures *out_features = gst_caps_get_features(caps, 0);
self->priv->use_dmabuf_output[index] = out_features &&
gst_caps_features_contains(out_features, GST_CAPS_FEATURE_MEMORY_DMABUF);
GstStructure *out_structure = gst_caps_get_structure(caps, 0);
const gchar *out_format_str = gst_structure_get_string(out_structure, "format");
gboolean out_is_drm = out_format_str && g_str_equal(out_format_str, "DMA_DRM");
if (out_is_drm)
{
const gchar *drm_format = gst_structure_get_string(out_structure, "drm-format");
gst_structure_get_int(out_structure, "width", (gint *)&self->priv->out_width[index]);
gst_structure_get_int(out_structure, "height", (gint *)&self->priv->out_height[index]);
self->priv->out_format[index] = drm_format_to_pisp(drm_format);
self->priv->out_stride[index] = 0;
GstVideoColorimetry colorimetry = {};
const gchar *colorimetry_str = gst_structure_get_string(out_structure, "colorimetry");
if (colorimetry_str)
gst_video_colorimetry_from_string(&colorimetry, colorimetry_str);
self->priv->out_colorspace[index] = colorimetry_to_pisp(&colorimetry);
GST_INFO_OBJECT(self, "Output%u DMA-DRM format: drm-format=%s, pisp=%s, colorspace=%s (matrix=%d, range=%d)",
index, drm_format, self->priv->out_format[index], self->priv->out_colorspace[index],
colorimetry.matrix, colorimetry.range);
}
else
{
if (!gst_video_info_from_caps(&out_info, caps))
{
GST_ERROR_OBJECT(self, "Failed to parse output%u caps", index);
return FALSE;
}
self->priv->out_width[index] = GST_VIDEO_INFO_WIDTH(&out_info);
self->priv->out_height[index] = GST_VIDEO_INFO_HEIGHT(&out_info);
self->priv->out_stride[index] = GST_VIDEO_INFO_PLANE_STRIDE(&out_info, 0);
self->priv->out_format[index] = gst_format_to_pisp(GST_VIDEO_INFO_FORMAT(&out_info));
self->priv->out_colorspace[index] = colorimetry_to_pisp(&GST_VIDEO_INFO_COLORIMETRY(&out_info));
GST_INFO_OBJECT(self, "Output%u format: pisp=%s, colorspace=%s (matrix=%d, range=%d)", index,
self->priv->out_format[index], self->priv->out_colorspace[index],
GST_VIDEO_INFO_COLORIMETRY(&out_info).matrix, GST_VIDEO_INFO_COLORIMETRY(&out_info).range);
}
/* Default output dimensions and colorspace to match input when unspecified */
if (!self->priv->out_width[index])
self->priv->out_width[index] = self->priv->in_width;
if (!self->priv->out_height[index])
self->priv->out_height[index] = self->priv->in_height;
if (!self->priv->out_colorspace[index])
self->priv->out_colorspace[index] = self->priv->in_colorspace;
if (!self->priv->out_format[index])
{
GST_ERROR_OBJECT(self, "Unsupported output%u format", index);
return FALSE;
}
self->priv->output_enabled[index] = TRUE;
return TRUE;
}
/*
* Parse input caps from the sink pad.
*/
static gboolean parse_input_caps(GstPispConvert *self, GstCaps *caps)
{
GstVideoInfo in_info;
GST_DEBUG_OBJECT(self, "parse_input_caps: caps=%" GST_PTR_FORMAT, caps);
GstCapsFeatures *in_features = gst_caps_get_features(caps, 0);
self->priv->use_dmabuf_input = in_features &&
gst_caps_features_contains(in_features, GST_CAPS_FEATURE_MEMORY_DMABUF);
GST_DEBUG_OBJECT(self, "in_features=%p, use_dmabuf_input=%d", in_features, self->priv->use_dmabuf_input);
GstStructure *in_structure = gst_caps_get_structure(caps, 0);
const gchar *in_format_str = gst_structure_get_string(in_structure, "format");
gboolean in_is_drm = in_format_str && g_str_equal(in_format_str, "DMA_DRM");
if (in_is_drm)
{
const gchar *drm_format = gst_structure_get_string(in_structure, "drm-format");
gst_structure_get_int(in_structure, "width", (gint *)&self->priv->in_width);
gst_structure_get_int(in_structure, "height", (gint *)&self->priv->in_height);
self->priv->in_format = drm_format_to_pisp(drm_format);
self->priv->in_stride = 0;
GstVideoColorimetry colorimetry = {};
const gchar *colorimetry_str = gst_structure_get_string(in_structure, "colorimetry");
if (colorimetry_str)
gst_video_colorimetry_from_string(&colorimetry, colorimetry_str);
self->priv->in_colorspace = colorimetry_to_pisp(&colorimetry);
GST_INFO_OBJECT(self, "Input DMA-DRM format: drm-format=%s, pisp=%s, colorspace=%s (matrix=%d, range=%d)",
drm_format, self->priv->in_format, self->priv->in_colorspace, colorimetry.matrix,
colorimetry.range);
}
else
{
if (!gst_video_info_from_caps(&in_info, caps))
{
GST_ERROR_OBJECT(self, "Failed to parse input caps");
return FALSE;
}
self->priv->in_width = GST_VIDEO_INFO_WIDTH(&in_info);
self->priv->in_height = GST_VIDEO_INFO_HEIGHT(&in_info);
self->priv->in_stride = GST_VIDEO_INFO_PLANE_STRIDE(&in_info, 0);
self->priv->in_format = gst_format_to_pisp(GST_VIDEO_INFO_FORMAT(&in_info));
self->priv->in_colorspace = colorimetry_to_pisp(&GST_VIDEO_INFO_COLORIMETRY(&in_info));
GST_INFO_OBJECT(self, "Input format: pisp=%s, colorspace=%s (matrix=%d, range=%d)", self->priv->in_format,
self->priv->in_colorspace, GST_VIDEO_INFO_COLORIMETRY(&in_info).matrix,
GST_VIDEO_INFO_COLORIMETRY(&in_info).range);
}
if (!self->priv->in_colorspace)
self->priv->in_colorspace = "jpeg";
if (!self->priv->in_format)
{
GST_ERROR_OBJECT(self, "Unsupported input format");
return FALSE;
}
return TRUE;
}
/* Helper functions for dmabuf support */
static gboolean gst_buffer_is_dmabuf(GstBuffer *buffer)
{
GstMemory *mem;
if (gst_buffer_n_memory(buffer) == 0)
return FALSE;
mem = gst_buffer_peek_memory(buffer, 0);
return gst_is_dmabuf_memory(mem);
}
/* Helper function to create Buffer object from GStreamer dmabuf buffer */
static std::optional<libpisp::helpers::Buffer> gst_to_libpisp_buffer(GstBuffer *buffer)
{
std::array<int, 3> fds = { -1, -1, -1 };
std::array<size_t, 3> sizes = { 0, 0, 0 };
guint n_mem = gst_buffer_n_memory(buffer);
for (guint i = 0; i < std::min(n_mem, 3u); i++)
{
GstMemory *mem = gst_buffer_peek_memory(buffer, i);
if (!gst_is_dmabuf_memory(mem))
return std::nullopt;
int raw = gst_dmabuf_memory_get_fd(mem);
fds[i] = dup(raw);
if (fds[i] < 0)
{
for (guint j = 0; j < i; j++)
{
if (fds[j] >= 0)
close(fds[j]);
}
return std::nullopt;
}
sizes[i] = mem->size;
}
return libpisp::helpers::Buffer(fds, sizes);
}
/* Create GstBuffer (dmabuf) from libpisp backend Buffer; dups FDs so backend keeps its ref */
static GstBuffer *libpisp_to_gst_dmabuf(const Buffer &buffer, GstAllocator *dmabuf_allocator)
{
GstBuffer *gstbuf = gst_buffer_new();
if (!gstbuf)
return nullptr;
for (unsigned int p = 0; p < 3; p++)
{
int fd = buffer.Fd()[p];
size_t size = buffer.Size()[p];
if (fd < 0 || size == 0)
continue;
int dup_fd = dup(fd);
if (dup_fd < 0)
{
GST_WARNING("dup(plane %u) failed: %s", p, strerror(errno));
gst_buffer_unref(gstbuf);
return nullptr;
}
GstMemory *mem = gst_dmabuf_allocator_alloc(dmabuf_allocator, dup_fd, size);
if (!mem)
{
close(dup_fd);
gst_buffer_unref(gstbuf);
return nullptr;
}
gst_buffer_append_memory(gstbuf, mem);
}
return gstbuf;
}
/* Attach GstVideoMeta with the correct hardware stride to a dmabuf output buffer */
static void add_video_meta(GstBuffer *buffer, const char *pisp_format, guint width, guint height, guint hw_stride)
{
GstVideoFormat gst_fmt = pisp_to_gst_video_format(pisp_format);
if (gst_fmt == GST_VIDEO_FORMAT_UNKNOWN)
return;
GstVideoInfo vinfo;
gst_video_info_set_format(&vinfo, gst_fmt, width, height);
gsize offsets[GST_VIDEO_MAX_PLANES] = {};
gint strides[GST_VIDEO_MAX_PLANES] = {};
gsize offset = 0;
guint n_planes = GST_VIDEO_INFO_N_PLANES(&vinfo);
GstMemory *mem;
for (guint p = 0; p < n_planes; p++)
{
offsets[p] = offset;
strides[p] = hw_stride * GST_VIDEO_INFO_PLANE_STRIDE(&vinfo, p) / GST_VIDEO_INFO_PLANE_STRIDE(&vinfo, 0);
mem = gst_buffer_peek_memory(buffer, p);
offset += mem->size;
}
gst_buffer_add_video_meta_full(buffer, GST_VIDEO_FRAME_FLAG_NONE, gst_fmt, width, height, n_planes, offsets,
strides);
}
static void copy_planes(std::array<uint8_t *, 3> src, guint src_stride, std::array<uint8_t *, 3> dst, guint dst_stride,
guint width, guint height, const char *format)
{
GST_DEBUG("copy_planes: %ux%u, src_stride=%u, dst_stride=%u, format=%s", width, height, src_stride, dst_stride,
format);
/* YUV420SP_COL128 (NV12 column 128) - special tiled format */
if (strncmp(format, "YUV420SP_COL128", 15) == 0 || strncmp(format, "YUV420SP10_COL128", 17) == 0)
{
guint y_size = GST_VIDEO_TILE_X_TILES(src_stride) * 128 * GST_VIDEO_TILE_Y_TILES(src_stride) * 8;
memcpy(dst[0], src[0], y_size);
uint8_t *src_uv = src[1] ? src[1] : src[0] + y_size;
uint8_t *dst_uv = dst[1] ? dst[1] : dst[0] + y_size;
memcpy(dst_uv, src_uv, y_size / 2);
return;
}
/* Planar YUV formats: YUV420P, YVU420P, YUV422P, YUV444P */
if (is_yuv_format(format) && strstr(format, "P") != nullptr)
{
/* Copy Y plane line by line */
for (guint y = 0; y < height; ++y)
memcpy(dst[0] + y * dst_stride, src[0] + y * src_stride, width);
/* Determine UV subsampling */
guint uv_width, uv_height;
if (strstr(format, "420") != nullptr)
{
uv_width = width / 2;
uv_height = height / 2;
}
else if (strstr(format, "422") != nullptr)
{
uv_width = width / 2;
uv_height = height;
}
else /* 444 */
{
uv_width = width;
uv_height = height;
}
guint src_uv_stride = (uv_width == width) ? src_stride : src_stride / 2;
guint dst_uv_stride = (uv_width == width) ? dst_stride : dst_stride / 2;
/* Calculate plane pointers if not explicitly provided (single contiguous buffer) */
uint8_t *src_u = src[1] ? src[1] : src[0] + src_stride * height;
uint8_t *src_v = src[2] ? src[2] : src_u + src_uv_stride * uv_height;
uint8_t *dst_u = dst[1] ? dst[1] : dst[0] + dst_stride * height;
uint8_t *dst_v = dst[2] ? dst[2] : dst_u + dst_uv_stride * uv_height;
/* Copy U and V planes */
for (guint y = 0; y < uv_height; ++y)
{
memcpy(dst_u + y * dst_uv_stride, src_u + y * src_uv_stride, uv_width);
memcpy(dst_v + y * dst_uv_stride, src_v + y * src_uv_stride, uv_width);
}
return;
}
/* Packed formats: YUYV, UYVY (2 bpp), RGB888 (3 bpp), RGBX (4 bpp) */
guint bytes_per_pixel = is_yuv_format(format) ? 2 : (strstr(format, "RGB888") != nullptr) ? 3 : 4;
guint line_stride = width * bytes_per_pixel;
for (guint y = 0; y < height; ++y)
memcpy(dst[0] + y * dst_stride, src[0] + y * src_stride, line_stride);
}
static void copy_buffer_to_pisp(GstBuffer *gstbuf, std::array<uint8_t *, 3> &mem, guint width, guint height,
guint gst_stride, guint hw_stride, const char *format)
{
GstMapInfo map;
gst_buffer_map(gstbuf, &map, GST_MAP_READ);
/* GstBuffer is always contiguous - planes calculated from offsets */
std::array<uint8_t *, 3> src = { map.data, nullptr, nullptr };
copy_planes(src, gst_stride, mem, hw_stride, width, height, format);
gst_buffer_unmap(gstbuf, &map);
}
static void copy_pisp_to_buffer(const std::array<uint8_t *, 3> &mem, GstBuffer *gstbuf, guint width, guint height,
guint gst_stride, guint hw_stride, const char *format)
{
GstMapInfo map;
gst_buffer_map(gstbuf, &map, GST_MAP_WRITE);
/* GstBuffer is always contiguous - planes calculated from offsets */
std::array<uint8_t *, 3> dst = { map.data, nullptr, nullptr };
copy_planes(const_cast<std::array<uint8_t *, 3> &>(mem), hw_stride, dst, gst_stride, width, height, format);
gst_buffer_unmap(gstbuf, &map);
}
/*
* Configure the PiSP backend for the current input/output settings.
*/
static gboolean gst_pisp_convert_configure(GstPispConvert *self)
{
if (!self->priv->backend_device)
{
GST_ERROR_OBJECT(self, "Backend not initialized");
return FALSE;
}
libpisp::BackEnd *backend = self->priv->backend_device->Get();
/* Check which outputs are enabled */
gboolean any_output = FALSE;
for (unsigned int i = 0; i < PISP_NUM_OUTPUTS; i++)
{
self->priv->output_enabled[i] = gst_pad_is_linked(self->srcpad[i]);
if (self->priv->output_enabled[i])
any_output = TRUE;
}
if (!any_output)
{
GST_ERROR_OBJECT(self, "No outputs are linked");
return FALSE;
}
/* Get output caps from linked pads */
for (unsigned int i = 0; i < PISP_NUM_OUTPUTS; i++)
{
if (!self->priv->output_enabled[i])
continue;
GstCaps *peer_caps = gst_pad_peer_query_caps(self->srcpad[i], nullptr);
if (!peer_caps || gst_caps_is_empty(peer_caps))
{
if (peer_caps)
gst_caps_unref(peer_caps);
GST_ERROR_OBJECT(self, "Failed to get caps for output%d", i);
return FALSE;
}
GstCaps *fixed_caps = gst_caps_fixate(peer_caps);
if (!parse_output_caps(self, i, fixed_caps))
{
gst_caps_unref(fixed_caps);
return FALSE;
}
/* Store caps and send downstream */
if (self->priv->src_caps[i])
gst_caps_unref(self->priv->src_caps[i]);
self->priv->src_caps[i] = fixed_caps;
gst_pad_push_event(self->srcpad[i], gst_event_new_caps(fixed_caps));
}
/* Forward pending segment event now that caps have been sent */
if (self->priv->pending_segment)
{
GST_DEBUG_OBJECT(self, "Forwarding stored segment event");
for (unsigned int i = 0; i < PISP_NUM_OUTPUTS; i++)
{
if (self->priv->output_enabled[i])
gst_pad_push_event(self->srcpad[i], gst_event_ref(self->priv->pending_segment));
}
gst_event_unref(self->priv->pending_segment);
self->priv->pending_segment = nullptr;
}
try
{
pisp_be_global_config global;
backend->GetGlobal(global);
global.bayer_enables = 0;
global.rgb_enables = PISP_BE_RGB_ENABLE_INPUT;
/* Configure input format */
pisp_image_format_config input_cfg = {};
input_cfg.width = self->priv->in_width;
input_cfg.height = self->priv->in_height;
input_cfg.format = libpisp::get_pisp_image_format(self->priv->in_format);
if (!input_cfg.format)
{
GST_ERROR_OBJECT(self, "Failed to get input format");
return FALSE;
}
libpisp::compute_stride(input_cfg);
self->priv->in_hw_stride = input_cfg.stride;
backend->SetInputFormat(input_cfg);
GST_INFO_OBJECT(self, "Input: %ux%u %s (stride: gst=%u hw=%u) colorspace %s", self->priv->in_width,
self->priv->in_height, self->priv->in_format, self->priv->in_stride, self->priv->in_hw_stride,
self->priv->in_colorspace);
if (!self->priv->in_colorspace)
self->priv->in_colorspace = "jpeg";
/* Configure each enabled output - first pass: formats and enables */
pisp_be_output_format_config output_cfg[PISP_NUM_OUTPUTS] = {};
for (unsigned int i = 0; i < PISP_NUM_OUTPUTS; i++)
{
if (!self->priv->output_enabled[i])
continue;
global.rgb_enables |= PISP_BE_RGB_ENABLE_OUTPUT(i);
output_cfg[i].image.width = self->priv->out_width[i];
output_cfg[i].image.height = self->priv->out_height[i];
output_cfg[i].image.format = libpisp::get_pisp_image_format(self->priv->out_format[i]);
if (!output_cfg[i].image.format)
{
GST_ERROR_OBJECT(self, "Failed to get output%d format", i);
return FALSE;
}
libpisp::compute_optimal_stride(output_cfg[i].image, true);
self->priv->out_hw_stride[i] = output_cfg[i].image.stride;
backend->SetOutputFormat(i, output_cfg[i]);
if ((g_str_equal(self->priv->out_format[i], "RGBX8888") ||
g_str_equal(self->priv->out_format[i], "XRGB8888")) &&
!self->priv->backend_device->Variant().BackendRGB32Supported(0))
GST_WARNING_OBJECT(self, "pisp_be HW does not support 32-bit RGB output, the image will be corrupt.");
if (!self->priv->out_colorspace[i])
self->priv->out_colorspace[i] = self->priv->in_colorspace;
global.rgb_enables |= configure_colour_conversion(backend, self->priv->in_format, self->priv->in_colorspace,
self->priv->out_format[i], self->priv->out_colorspace[i],
i);
GST_INFO_OBJECT(self, "Output%d: %ux%u %s (stride: gst=%u hw=%u) colorspace %s", i,
self->priv->out_width[i], self->priv->out_height[i], self->priv->out_format[i],
self->priv->out_stride[i], self->priv->out_hw_stride[i], self->priv->out_colorspace[i]);
}
backend->SetGlobal(global);
for (unsigned int i = 0; i < PISP_NUM_OUTPUTS; i++)
{
if (!self->priv->output_enabled[i])
continue;
pisp_be_crop_config crop = self->priv->crop[i];
/* Default to full input if width/height not specified */
if (!crop.width)
crop.width = input_cfg.width;
if (!crop.height)
crop.height = input_cfg.height;
/* Clip crop region to fit within input */
if (crop.offset_x >= input_cfg.width)
crop.offset_x = 0;
if (crop.offset_y >= input_cfg.height)
crop.offset_y = 0;
if (crop.offset_x + crop.width > input_cfg.width)
crop.width = input_cfg.width - crop.offset_x;
if (crop.offset_y + crop.height > input_cfg.height)
crop.height = input_cfg.height - crop.offset_y;
GST_INFO_OBJECT(self, "Crop%u: offset=(%u,%u) size=%ux%u", i, crop.offset_x, crop.offset_y, crop.width,
crop.height);
/* Only call SetCrop if parameters changed */
if (memcmp(&crop, &self->priv->applied_crop[i], sizeof(crop)) != 0)
{
backend->SetCrop(i, crop);
self->priv->applied_crop[i] = crop;
}
/* Only call SetSmartResize if parameters changed */
libpisp::BackEnd::SmartResize smart_resize = { (uint16_t)output_cfg[i].image.width,
(uint16_t)output_cfg[i].image.height };
if (memcmp(&smart_resize, &self->priv->applied_smart_resize[i], sizeof(smart_resize)) != 0)
{
backend->SetSmartResize(i, smart_resize);
self->priv->applied_smart_resize[i] = smart_resize;
}
}
/* Prepare the hardware configuration */
self->priv->backend_device->Prepare(self->priv->output_buffer_count);
self->priv->pisp_buffers = self->priv->backend_device->GetBuffers();
self->priv->buffer_slice_index = 0;
self->priv->configured = TRUE;
GST_INFO_OBJECT(self, "Backend configured successfully with %u output(s), %u buffer(s)",
self->priv->output_enabled[1] ? 2 : 1, self->priv->output_buffer_count);
}
catch (const std::exception &e)
{
GST_ERROR_OBJECT(self, "Failed to configure backend: %s", e.what());
return FALSE;
}
return TRUE;
}
/*
* Setup output buffer pool for the specified output index.
*/
static gboolean gst_pisp_convert_setup_output_pool(GstPispConvert *self, guint index)
{
if (self->priv->output_pool[index])
{
gst_buffer_pool_set_active(self->priv->output_pool[index], FALSE);
gst_object_unref(self->priv->output_pool[index]);
self->priv->output_pool[index] = nullptr;
}
if (!self->priv->src_caps[index])
return FALSE;
GstBufferPool *pool = gst_video_buffer_pool_new();
GstStructure *config = gst_buffer_pool_get_config(pool);
/* Calculate buffer size */
gsize size;
GstStructure *structure = gst_caps_get_structure(self->priv->src_caps[index], 0);
const gchar *format_str = gst_structure_get_string(structure, "format");
if (format_str && g_str_equal(format_str, "DMA_DRM"))
{
size = self->priv->out_width[index] * self->priv->out_height[index] * 4;
}
else
{
GstVideoInfo vinfo;
if (!gst_video_info_from_caps(&vinfo, self->priv->src_caps[index]))
{
gst_object_unref(pool);
return FALSE;
}
size = GST_VIDEO_INFO_SIZE(&vinfo);
}