-
-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathframe.c
More file actions
1174 lines (992 loc) · 40.7 KB
/
Copy pathframe.c
File metadata and controls
1174 lines (992 loc) · 40.7 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
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <drm_fourcc.h>
#include <gbm.h>
#include <gst/allocators/allocators.h>
#include <gst/video/video.h>
#include "flutter-pi.h"
#include "texture_registry.h"
// This will error if we don't have EGL / OpenGL ES support.
#include "gl_renderer.h"
#include "plugins/gstreamer_video_player.h"
#include "util/logging.h"
#include "util/refcounting.h"
#define MAX_N_PLANES 4
#define GSTREAMER_VER(major, minor, patch) ((((major) &0xFF) << 16) | (((minor) &0xFF) << 8) | ((patch) &0xFF))
#define THIS_GSTREAMER_VER GSTREAMER_VER(LIBGSTREAMER_VERSION_MAJOR, LIBGSTREAMER_VERSION_MINOR, LIBGSTREAMER_VERSION_PATCH)
#define DRM_FOURCC_FORMAT "c%c%c%c"
#define DRM_FOURCC_ARGS(format) (format) & 0xFF, ((format) >> 8) & 0xFF, ((format) >> 16) & 0xFF, ((format) >> 24) & 0xFF
// Serializes access to gbm_bo_create/gbm_bo_map/gbm_bo_get_fd.
// Mesa's Gallium GBM/DRI backend is not safe against concurrent calls
// into the same gbm_device from multiple threads -- without this lock,
// concurrent video zones each running their own GStreamer pipeline thread
// can segfault inside libgallium when calling dup_gst_memory_as_dmabuf()
// simultaneously (observed via gdb, SIGSEGV in libgallium.so called from
// frame.c, thread = a GStreamer "multiqueueN:src" worker thread).
static pthread_mutex_t gbm_lock = PTHREAD_MUTEX_INITIALIZER;
struct video_frame {
GstSample *sample;
struct frame_interface *interface;
uint32_t drm_format;
int n_dmabuf_fds;
int dmabuf_fds[MAX_N_PLANES];
EGLImageKHR image;
size_t width, height;
struct gl_texture_frame gl_frame;
};
struct frame_interface {
struct gbm_device *gbm_device;
EGLDisplay display;
pthread_mutex_t context_lock;
EGLContext context;
PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
bool supports_external_target;
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
bool supports_extended_imports;
#ifdef EGL_EXT_image_dma_buf_import_modifiers
PFNEGLQUERYDMABUFFORMATSEXTPROC eglQueryDmaBufFormatsEXT;
PFNEGLQUERYDMABUFMODIFIERSEXTPROC eglQueryDmaBufModifiersEXT;
#endif
int n_formats;
struct egl_modified_format *formats;
refcount_t n_refs;
};
#ifdef EGL_EXT_image_dma_buf_import_modifiers
static bool query_formats(
EGLDisplay display,
PFNEGLQUERYDMABUFFORMATSEXTPROC egl_query_dmabuf_formats,
PFNEGLQUERYDMABUFMODIFIERSEXTPROC egl_query_dmabuf_modifiers,
int *n_formats_out,
struct egl_modified_format **formats_out
) {
// EGLuint64KHR is defined by EGL_KHR_stream
#ifndef EGL_KHR_stream
#error "EGL header definitions for extension EGL_KHR_stream are required."
#endif
struct egl_modified_format *modified_formats;
EGLBoolean *external_only;
EGLuint64KHR *modifiers;
EGLint *formats;
EGLint egl_ok, n_modifiers;
int n_formats, n_modified_formats, max_n_modifiers;
egl_ok = egl_query_dmabuf_formats(display, 0, NULL, &n_formats);
if (egl_ok != EGL_TRUE) {
LOG_ERROR("Could not query number of dmabuf formats supported by EGL.\n");
goto fail;
}
formats = malloc(n_formats * sizeof *formats);
if (formats == NULL) {
goto fail;
}
egl_ok = egl_query_dmabuf_formats(display, n_formats, formats, &n_formats);
if (egl_ok != EGL_TRUE) {
LOG_ERROR("Could not query dmabuf formats supported by EGL.\n");
goto fail_free_formats;
}
// first, count how many modifiers we have for each format.
n_modified_formats = 0;
max_n_modifiers = 0;
for (int i = 0; i < n_formats; i++) {
egl_ok = egl_query_dmabuf_modifiers(display, formats[i], 0, NULL, NULL, &n_modifiers);
if (egl_ok != EGL_TRUE) {
LOG_ERROR("Could not query dmabuf formats supported by EGL.\n");
goto fail_free_formats;
}
n_modified_formats += n_modifiers;
if (n_modifiers > max_n_modifiers) {
max_n_modifiers = n_modifiers;
}
}
modified_formats = malloc(n_modified_formats * sizeof *modified_formats);
if (modified_formats == NULL) {
goto fail_free_formats;
}
modifiers = malloc(max_n_modifiers * sizeof *modifiers);
if (modifiers == NULL) {
goto fail_free_modified_formats;
}
external_only = malloc(max_n_modifiers * sizeof *external_only);
if (external_only == NULL) {
goto fail_free_modifiers;
}
LOG_DEBUG("supported formats for EGL import: ");
for (int i = 0, j = 0; i < n_formats; i++) {
egl_ok = egl_query_dmabuf_modifiers(display, formats[i], max_n_modifiers, modifiers, external_only, &n_modifiers);
if (egl_ok != EGL_TRUE) {
LOG_ERROR("Could not query dmabuf formats supported by EGL.\n");
goto fail_free_formats;
}
LOG_DEBUG_UNPREFIXED("%" DRM_FOURCC_FORMAT ", ", DRM_FOURCC_ARGS(formats[i]));
for (int k = 0; k < n_modifiers; k++, j++) {
modified_formats[j].format = formats[i];
modified_formats[j].modifier = modifiers[k];
modified_formats[j].external_only = external_only[k];
}
}
LOG_DEBUG_UNPREFIXED("\n");
free(formats);
free(modifiers);
free(external_only);
*n_formats_out = n_modified_formats;
*formats_out = modified_formats;
return true;
fail_free_modifiers:
free(modifiers);
fail_free_modified_formats:
free(modified_formats);
fail_free_formats:
free(formats);
fail:
*n_formats_out = 0;
*formats_out = NULL;
return false;
}
#endif
struct frame_interface *frame_interface_new(struct gl_renderer *renderer) {
struct frame_interface *interface;
struct gbm_device *gbm_device;
EGLBoolean egl_ok;
EGLContext context;
EGLDisplay display;
struct egl_modified_format *formats;
bool supports_extended_imports, supports_external_target;
int n_formats;
interface = malloc(sizeof *interface);
if (interface == NULL) {
return NULL;
}
if (!gl_renderer_supports_egl_extension(renderer, "EGL_EXT_image_dma_buf_import")) {
LOG_ERROR("EGL does not support EGL_EXT_image_dma_buf_import extension. Video frames cannot be uploaded.\n");
goto fail_free;
}
if (gl_renderer_supports_egl_extension(renderer, "EGL_EXT_image_dma_buf_import_modifiers")) {
#ifndef EGL_EXT_image_dma_buf_import_modifiers
LOG_ERROR(
"EGL supports EGL_EXT_image_dma_buf_import_modifiers, "
"but EGL headers didn't contain definitions for EGL_EXT_image_dma_buf_import_modifiers."
"Extended imports and pixel format information will not be used.\n"
);
supports_extended_imports = false;
#else
supports_extended_imports = true;
#endif
} else {
supports_extended_imports = false;
}
if (gl_renderer_supports_gl_extension(renderer, "GL_OES_EGL_image_external")) {
supports_external_target = true;
} else {
supports_external_target = false;
}
display = gl_renderer_get_egl_display(renderer);
if (display == EGL_NO_DISPLAY) {
goto fail_free;
}
context = gl_renderer_create_context(renderer);
if (context == EGL_NO_CONTEXT) {
goto fail_free;
}
PFNEGLCREATEIMAGEKHRPROC create_image = (PFNEGLCREATEIMAGEKHRPROC) gl_renderer_get_proc_address(renderer, "eglCreateImageKHR");
if (create_image == NULL) {
LOG_ERROR("Could not resolve eglCreateImageKHR EGL procedure.\n");
goto fail_destroy_context;
}
PFNEGLDESTROYIMAGEKHRPROC destroy_image = (PFNEGLDESTROYIMAGEKHRPROC) gl_renderer_get_proc_address(renderer, "eglDestroyImageKHR");
if (destroy_image == NULL) {
LOG_ERROR("Could not resolve eglDestroyImageKHR EGL procedure.\n");
goto fail_destroy_context;
}
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC gl_egl_image_target_texture2d = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC
) gl_renderer_get_proc_address(renderer, "glEGLImageTargetTexture2DOES");
if (gl_egl_image_target_texture2d == NULL) {
LOG_ERROR("Could not resolve glEGLImageTargetTexture2DOES EGL procedure.\n");
goto fail_destroy_context;
}
// These two are optional.
// Might be useful in the future.
#ifdef EGL_EXT_image_dma_buf_import_modifiers
PFNEGLQUERYDMABUFFORMATSEXTPROC egl_query_dmabuf_formats = (PFNEGLQUERYDMABUFFORMATSEXTPROC
) gl_renderer_get_proc_address(renderer, "eglQueryDmaBufFormatsEXT");
if (egl_query_dmabuf_formats == NULL && supports_extended_imports) {
LOG_ERROR("Could not resolve eglQueryDmaBufFormatsEXT egl procedure, even though it is listed as supported.\n");
supports_extended_imports = false;
}
PFNEGLQUERYDMABUFMODIFIERSEXTPROC egl_query_dmabuf_modifiers = (PFNEGLQUERYDMABUFMODIFIERSEXTPROC
) gl_renderer_get_proc_address(renderer, "eglQueryDmaBufModifiersEXT");
if (egl_query_dmabuf_modifiers == NULL && supports_extended_imports) {
LOG_ERROR("Could not resolve eglQueryDmaBufModifiersEXT egl procedure, even though it is listed as supported.\n");
supports_extended_imports = false;
}
#endif
gbm_device = gl_renderer_get_gbm_device(renderer);
if (gbm_device == NULL) {
LOG_ERROR("GL Render doesn't have a GBM device associated with it, which is necessary for importing the video frames.\n");
goto fail_destroy_context;
}
if (supports_extended_imports) {
#ifdef EGL_EXT_image_dma_buf_import_modifiers
query_formats(display, egl_query_dmabuf_formats, egl_query_dmabuf_modifiers, &n_formats, &formats);
#else
UNREACHABLE();
#endif
} else {
n_formats = 0;
formats = NULL;
}
interface->gbm_device = gbm_device;
interface->display = display;
pthread_mutex_init(&interface->context_lock, NULL);
interface->context = context;
interface->eglCreateImageKHR = create_image;
interface->eglDestroyImageKHR = destroy_image;
interface->supports_external_target = supports_external_target;
interface->glEGLImageTargetTexture2DOES = gl_egl_image_target_texture2d;
interface->supports_extended_imports = supports_extended_imports;
#ifdef EGL_EXT_image_dma_buf_import_modifiers
interface->eglQueryDmaBufFormatsEXT = egl_query_dmabuf_formats;
interface->eglQueryDmaBufModifiersEXT = egl_query_dmabuf_modifiers;
#endif
interface->n_formats = n_formats;
interface->formats = formats;
interface->n_refs = REFCOUNT_INIT_1;
return interface;
fail_destroy_context:
egl_ok = eglDestroyContext(display, context);
ASSERT_EGL_TRUE(egl_ok);
(void) egl_ok;
fail_free:
free(interface);
return NULL;
}
void frame_interface_destroy(struct frame_interface *interface) {
EGLBoolean egl_ok;
pthread_mutex_destroy(&interface->context_lock);
egl_ok = eglDestroyContext(interface->display, interface->context);
ASSERT_EGL_TRUE(egl_ok);
(void) egl_ok;
if (interface->formats != NULL) {
free(interface->formats);
}
free(interface);
}
ATTR_PURE int frame_interface_get_n_formats(struct frame_interface *interface) {
return interface->n_formats;
}
ATTR_PURE const struct egl_modified_format *frame_interface_get_format(struct frame_interface *interface, int index) {
assert(index < interface->n_formats);
return interface->formats + index;
}
DEFINE_LOCK_OPS(frame_interface, context_lock)
DEFINE_REF_OPS(frame_interface, n_refs)
/**
* @brief Create a dmabuf fd from the given GstBuffer.
*
* Calls gst_buffer_map on the buffer, so buffer could have changed after the call.
*
*/
UNUSED int dup_gst_buffer_range_as_dmabuf(struct gbm_device *gbm_device, GstBuffer *buffer, unsigned int memory_index, int n_memories) {
struct gbm_bo *bo;
GstMapInfo map_info;
uint32_t stride;
gboolean gst_ok;
void *map, *map_data;
int fd;
gst_ok = gst_buffer_map_range(buffer, memory_index, n_memories, &map_info, GST_MAP_READ);
if (gst_ok == FALSE) {
LOG_ERROR("Couldn't map gstreamer video frame buffer to copy it into a dma buffer.\n");
return -1;
}
bo = gbm_bo_create(gbm_device, map_info.size, 1, GBM_FORMAT_R8, GBM_BO_USE_LINEAR);
if (bo == NULL) {
LOG_ERROR("Couldn't create GBM BO to copy video frame into.\n");
goto fail_unmap_buffer;
}
map_data = NULL;
map = gbm_bo_map(bo, 0, 0, map_info.size, 1, GBM_BO_TRANSFER_WRITE, &stride, &map_data);
if (map == NULL) {
LOG_ERROR("Couldn't mmap GBM BO to copy video frame into it.\n");
goto fail_destroy_bo;
}
memcpy(map, map_info.data, map_info.size);
gbm_bo_unmap(bo, map_data);
fd = gbm_bo_get_fd(bo);
if (fd < 0) {
LOG_ERROR("Couldn't filedescriptor of video frame GBM BO.\n");
goto fail_destroy_bo;
}
/// TODO: Should we dup the fd before we destroy the bo?
gbm_bo_destroy(bo);
gst_buffer_unmap(buffer, &map_info);
return fd;
fail_destroy_bo:
gbm_bo_destroy(bo);
fail_unmap_buffer:
gst_buffer_unmap(buffer, &map_info);
return -1;
}
/**
* @brief Create a dmabuf fd from the given GstMemory.
*
* Calls gst_memory_map on the memory.
*
*/
UNUSED int dup_gst_memory_as_dmabuf(struct gbm_device *gbm_device, GstMemory *memory) {
struct gbm_bo *bo;
GstMapInfo map_info;
uint32_t stride;
gboolean gst_ok;
void *map, *map_data;
int fd;
gst_ok = gst_memory_map(memory, &map_info, GST_MAP_READ);
if (gst_ok == FALSE) {
LOG_ERROR("Couldn't map gstreamer video frame memory to copy it into a dma buffer.\n");
return -1;
}
pthread_mutex_lock(&gbm_lock);
bo = gbm_bo_create(gbm_device, map_info.size, 1, GBM_FORMAT_R8, GBM_BO_USE_LINEAR);
if (bo == NULL) {
LOG_ERROR("Couldn't create GBM BO to copy video frame into.\n");
goto fail_unlock_gbm;
}
map_data = NULL;
map = gbm_bo_map(bo, 0, 0, map_info.size, 1, GBM_BO_TRANSFER_WRITE, &stride, &map_data);
if (map == NULL) {
LOG_ERROR("Couldn't mmap GBM BO to copy video frame into it.\n");
goto fail_destroy_bo;
}
memcpy(map, map_info.data, map_info.size);
gbm_bo_unmap(bo, map_data);
fd = gbm_bo_get_fd(bo);
if (fd < 0) {
LOG_ERROR("Couldn't filedescriptor of video frame GBM BO.\n");
goto fail_destroy_bo;
}
/// TODO: Should we dup the fd before we destroy the bo?
gbm_bo_destroy(bo);
pthread_mutex_unlock(&gbm_lock);
gst_memory_unmap(memory, &map_info);
return fd;
fail_destroy_bo:
gbm_bo_destroy(bo);
fail_unlock_gbm:
pthread_mutex_unlock(&gbm_lock);
gst_memory_unmap(memory, &map_info);
return -1;
}
struct plane_info {
int fd;
uint32_t offset;
uint32_t pitch;
bool has_modifier;
uint64_t modifier;
};
#if THIS_GSTREAMER_VER < GSTREAMER_VER(1, 14, 0)
#error "Unsupported gstreamer version."
#endif
#if THIS_GSTREAMER_VER >= GSTREAMER_VER(1, 18, 0)
static bool get_plane_sizes_from_meta(const GstVideoMeta *meta, size_t plane_sizes_out[4]) {
GstVideoMeta *meta_non_const;
gboolean gst_ok;
#ifdef DEBUG
GstVideoMeta _meta_non_const;
meta_non_const = &_meta_non_const;
memcpy(meta_non_const, meta, sizeof *meta);
#else
meta_non_const = (GstVideoMeta *) meta;
#endif
gst_ok = gst_video_meta_get_plane_size(meta_non_const, plane_sizes_out);
if (gst_ok != TRUE) {
LOG_ERROR("Could not query video frame plane size. gst_video_meta_get_plane_size\n");
return false;
}
return true;
}
static bool get_plane_sizes_from_video_info(const GstVideoInfo *info, size_t plane_sizes_out[4]) {
GstVideoAlignment alignment;
GstVideoInfo *info_non_const;
gboolean gst_ok;
gst_video_alignment_reset(&alignment);
#ifdef DEBUG
info_non_const = gst_video_info_copy(info);
#else
info_non_const = (GstVideoInfo *) info;
#endif
gst_ok = gst_video_info_align_full(info_non_const, &alignment, plane_sizes_out);
if (gst_ok != TRUE) {
LOG_ERROR("Could not query video frame plane size. gst_video_info_align_full\n");
return false;
}
#ifdef DEBUG
assert(gst_video_info_is_equal(info, info_non_const));
gst_video_info_free(info_non_const);
#endif
return true;
}
static bool calculate_plane_size(const GstVideoInfo *info, int plane_index, size_t *plane_size_out) {
// Taken from: https://github.com/GStreamer/gstreamer/blob/621604aa3e4caa8db27637f63fa55fac2f7721e5/subprojects/gst-plugins-base/gst-libs/gst/video/video-info.c#L1278-L1301
#if THIS_GSTREAMER_VER >= GSTREAMER_VER(1, 21, 3)
if (GST_VIDEO_FORMAT_INFO_IS_TILED(info->finfo)) {
guint x_tiles = GST_VIDEO_TILE_X_TILES(info->stride[plane_index]);
guint y_tiles = GST_VIDEO_TILE_Y_TILES(info->stride[plane_index]);
*plane_size_out = x_tiles * y_tiles * GST_VIDEO_FORMAT_INFO_TILE_SIZE(info->finfo, plane_index);
return true;
}
#endif
gint comp[GST_VIDEO_MAX_COMPONENTS];
guint plane_height;
/* Convert plane index to component index */
gst_video_format_info_component(info->finfo, plane_index, comp);
plane_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT(info->finfo, comp[0], GST_VIDEO_INFO_FIELD_HEIGHT(info));
*plane_size_out = plane_height * GST_VIDEO_INFO_PLANE_STRIDE(info, plane_index);
return true;
}
#else
static bool get_plane_sizes_from_meta(UNUSED const GstVideoMeta *meta, UNUSED size_t plane_sizes_out[4]) {
return false;
}
static bool get_plane_sizes_from_video_info(UNUSED const GstVideoInfo *info, UNUSED size_t plane_sizes_out[4]) {
return false;
}
static bool calculate_plane_size(UNUSED const GstVideoInfo *info, UNUSED int plane_index, UNUSED size_t *plane_size_out) {
return false;
}
#endif
static int
get_plane_infos(GstBuffer *buffer, const GstVideoInfo *info, struct gbm_device *gbm_device, struct plane_info plane_infos[MAX_N_PLANES]) {
GstVideoMeta *meta;
GstMemory *memory;
gboolean gst_ok;
size_t plane_sizes[4] = { 0 };
bool has_plane_sizes;
int n_planes;
n_planes = GST_VIDEO_INFO_N_PLANES(info);
// There's so many ways to get the plane sizes.
// 1. Preferably we should use the video meta.
// 2. If that doesn't work, we'll use gst_video_info_align_full() with the video info.
// 3. If that doesn't work, we'll calculate them ourselves.
// 4. If that doesn't work, we can't determine the plane sizes.
// In that case, we'll error if we have more than one plane.
has_plane_sizes = false;
meta = gst_buffer_get_video_meta(buffer);
if (meta != NULL) {
has_plane_sizes = get_plane_sizes_from_meta(meta, plane_sizes);
}
if (!has_plane_sizes) {
has_plane_sizes = get_plane_sizes_from_video_info(info, plane_sizes);
}
if (!has_plane_sizes) {
has_plane_sizes = true;
for (int i = 0; i < n_planes; i++) {
has_plane_sizes = has_plane_sizes && calculate_plane_size(info, i, plane_sizes + i);
}
}
if (!has_plane_sizes) {
// We couldn't determine the plane sizes.
// We can still continue if we have only one plane.
if (n_planes != 1) {
LOG_ERROR(
"Couldn't determine video frame plane sizes. Without plane sizes, only planar framebuffer formats are supported, but the "
"supplied format was not planar.\n"
);
return EINVAL;
}
// We'll just assume the first plane spans the entire buffer.
plane_sizes[0] = gst_buffer_get_size(buffer);
has_plane_sizes = true;
}
for (int i = 0; i < n_planes; i++) {
size_t offset_in_memory = 0;
size_t offset_in_buffer = 0;
unsigned memory_index = 0;
unsigned n_memories = 0;
int stride, ok;
if (meta) {
offset_in_buffer = meta->offset[i];
stride = meta->stride[i];
} else {
offset_in_buffer = GST_VIDEO_INFO_PLANE_OFFSET(info, i);
stride = GST_VIDEO_INFO_PLANE_STRIDE(info, i);
}
gst_ok = gst_buffer_find_memory(buffer, offset_in_buffer, plane_sizes[i], &memory_index, &n_memories, &offset_in_memory);
if (gst_ok != TRUE) {
LOG_ERROR("Could not find video frame memory for plane.\n");
ok = EIO;
goto fail_close_fds;
}
if (n_memories != 1) {
ok = dup_gst_buffer_range_as_dmabuf(gbm_device, buffer, memory_index, n_memories);
if (ok < 0) {
LOG_ERROR("Could not duplicate gstreamer buffer range as dmabuf.\n");
ok = EIO;
goto fail_close_fds;
}
plane_infos[i].fd = ok;
} else {
memory = gst_buffer_peek_memory(buffer, memory_index);
if (gst_is_dmabuf_memory(memory)) {
ok = gst_dmabuf_memory_get_fd(memory);
if (ok < 0) {
LOG_ERROR("Could not get gstreamer memory as dmabuf.\n");
ok = EIO;
goto fail_close_fds;
}
ok = dup(ok);
if (ok < 0) {
ok = errno;
LOG_ERROR("Could not dup fd. dup: %s\n", strerror(ok));
goto fail_close_fds;
}
plane_infos[i].fd = ok;
} else {
/// TODO: When duping, duplicate all non-dmabuf memories into one
/// gbm buffer instead.
ok = dup_gst_memory_as_dmabuf(gbm_device, memory);
if (ok < 0) {
LOG_ERROR("Could not duplicate gstreamer memory as dmabuf.\n");
ok = EIO;
goto fail_close_fds;
}
plane_infos[i].fd = ok;
}
offset_in_memory += memory->offset;
}
plane_infos[i].offset = offset_in_memory;
plane_infos[i].pitch = stride;
/// TODO: Detect modifiers here
/// Modifiers will be supported in future gstreamer, see:
/// https://gstreamer.freedesktop.org/documentation/additional/design/dmabuf.html?gi-language=c
plane_infos[i].has_modifier = false;
plane_infos[i].modifier = DRM_FORMAT_MOD_LINEAR;
continue;
fail_close_fds:
for (int j = i - 1; j > 0; j--) {
close(plane_infos[i].fd);
}
return ok;
}
return 0;
}
static uint32_t drm_format_from_gst_info(const GstVideoInfo *info) {
switch (GST_VIDEO_INFO_FORMAT(info)) {
case GST_VIDEO_FORMAT_YUY2: return DRM_FORMAT_YUYV;
case GST_VIDEO_FORMAT_YVYU: return DRM_FORMAT_YVYU;
case GST_VIDEO_FORMAT_UYVY: return DRM_FORMAT_UYVY;
case GST_VIDEO_FORMAT_VYUY: return DRM_FORMAT_VYUY;
case GST_VIDEO_FORMAT_AYUV: return DRM_FORMAT_AYUV;
#if THIS_GSTREAMER_VER >= GSTREAMER_VER(1, 16, 0)
case GST_VIDEO_FORMAT_VUYA: return DRM_FORMAT_AYUV;
#endif
case GST_VIDEO_FORMAT_NV12: return DRM_FORMAT_NV12;
case GST_VIDEO_FORMAT_NV21: return DRM_FORMAT_NV21;
case GST_VIDEO_FORMAT_NV16: return DRM_FORMAT_NV16;
case GST_VIDEO_FORMAT_NV61: return DRM_FORMAT_NV61;
case GST_VIDEO_FORMAT_NV24: return DRM_FORMAT_NV24;
case GST_VIDEO_FORMAT_YUV9: return DRM_FORMAT_YUV410;
case GST_VIDEO_FORMAT_YVU9: return DRM_FORMAT_YVU410;
case GST_VIDEO_FORMAT_Y41B: return DRM_FORMAT_YUV411;
case GST_VIDEO_FORMAT_I420: return DRM_FORMAT_YUV420;
case GST_VIDEO_FORMAT_YV12: return DRM_FORMAT_YVU420;
case GST_VIDEO_FORMAT_Y42B: return DRM_FORMAT_YUV422;
case GST_VIDEO_FORMAT_Y444: return DRM_FORMAT_YUV444;
case GST_VIDEO_FORMAT_RGB16: return DRM_FORMAT_RGB565;
case GST_VIDEO_FORMAT_BGR16: return DRM_FORMAT_BGR565;
case GST_VIDEO_FORMAT_RGBA: return DRM_FORMAT_ABGR8888;
case GST_VIDEO_FORMAT_RGBx: return DRM_FORMAT_XBGR8888;
case GST_VIDEO_FORMAT_BGRA: return DRM_FORMAT_ARGB8888;
case GST_VIDEO_FORMAT_BGRx: return DRM_FORMAT_XRGB8888;
case GST_VIDEO_FORMAT_ARGB: return DRM_FORMAT_BGRA8888;
case GST_VIDEO_FORMAT_xRGB: return DRM_FORMAT_BGRX8888;
case GST_VIDEO_FORMAT_ABGR: return DRM_FORMAT_RGBA8888;
case GST_VIDEO_FORMAT_xBGR: return DRM_FORMAT_RGBX8888;
default: return DRM_FORMAT_INVALID;
}
}
ATTR_CONST GstVideoFormat gst_video_format_from_drm_format(uint32_t drm_format) {
switch (drm_format) {
case DRM_FORMAT_YUYV: return GST_VIDEO_FORMAT_YUY2;
case DRM_FORMAT_YVYU: return GST_VIDEO_FORMAT_YVYU;
case DRM_FORMAT_UYVY: return GST_VIDEO_FORMAT_UYVY;
case DRM_FORMAT_VYUY: return GST_VIDEO_FORMAT_VYUY;
case DRM_FORMAT_AYUV: return GST_VIDEO_FORMAT_AYUV;
#if THIS_GSTREAMER_VER >= GSTREAMER_VER(1, 16, 0)
// GST_VIDEO_FORMAT_AYUV and _VUYA both map to DRM_FORMAT_AYUV
// case DRM_FORMAT_AYUV: return GST_VIDEO_FORMAT_VUYA;
#endif
case DRM_FORMAT_NV12: return GST_VIDEO_FORMAT_NV12;
case DRM_FORMAT_NV21: return GST_VIDEO_FORMAT_NV21;
case DRM_FORMAT_NV16: return GST_VIDEO_FORMAT_NV16;
case DRM_FORMAT_NV61: return GST_VIDEO_FORMAT_NV61;
case DRM_FORMAT_NV24: return GST_VIDEO_FORMAT_NV24;
case DRM_FORMAT_YUV410: return GST_VIDEO_FORMAT_YUV9;
case DRM_FORMAT_YVU410: return GST_VIDEO_FORMAT_YVU9;
case DRM_FORMAT_YUV411: return GST_VIDEO_FORMAT_Y41B;
case DRM_FORMAT_YUV420: return GST_VIDEO_FORMAT_I420;
case DRM_FORMAT_YVU420: return GST_VIDEO_FORMAT_YV12;
case DRM_FORMAT_YUV422: return GST_VIDEO_FORMAT_Y42B;
case DRM_FORMAT_YUV444: return GST_VIDEO_FORMAT_Y444;
case DRM_FORMAT_RGB565: return GST_VIDEO_FORMAT_RGB16;
case DRM_FORMAT_BGR565: return GST_VIDEO_FORMAT_BGR16;
case DRM_FORMAT_ABGR8888: return GST_VIDEO_FORMAT_RGBA;
case DRM_FORMAT_XBGR8888: return GST_VIDEO_FORMAT_RGBx;
case DRM_FORMAT_ARGB8888: return GST_VIDEO_FORMAT_BGRA;
case DRM_FORMAT_XRGB8888: return GST_VIDEO_FORMAT_BGRx;
case DRM_FORMAT_BGRA8888: return GST_VIDEO_FORMAT_ARGB;
case DRM_FORMAT_BGRX8888: return GST_VIDEO_FORMAT_xRGB;
case DRM_FORMAT_RGBA8888: return GST_VIDEO_FORMAT_ABGR;
case DRM_FORMAT_RGBX8888: return GST_VIDEO_FORMAT_xBGR;
default: return GST_VIDEO_FORMAT_UNKNOWN;
}
}
static EGLint egl_color_space_from_gst_info(const GstVideoInfo *info) {
if (gst_video_colorimetry_matches(&GST_VIDEO_INFO_COLORIMETRY(info), GST_VIDEO_COLORIMETRY_BT601)) {
return EGL_ITU_REC601_EXT;
} else if (gst_video_colorimetry_matches(&GST_VIDEO_INFO_COLORIMETRY(info), GST_VIDEO_COLORIMETRY_BT709)) {
return EGL_ITU_REC709_EXT;
} else if (gst_video_colorimetry_matches(&GST_VIDEO_INFO_COLORIMETRY(info), GST_VIDEO_COLORIMETRY_BT2020)) {
return EGL_ITU_REC2020_EXT;
} else {
LOG_DEBUG("Unsupported video colorimetry: %s\n", gst_video_colorimetry_to_string(&GST_VIDEO_INFO_COLORIMETRY(info)));
return EGL_NONE;
}
}
static EGLint egl_sample_range_hint_from_gst_info(const GstVideoInfo *info) {
if (GST_VIDEO_INFO_COLORIMETRY(info).range == GST_VIDEO_COLOR_RANGE_0_255) {
return EGL_YUV_FULL_RANGE_EXT;
} else if (GST_VIDEO_INFO_COLORIMETRY(info).range == GST_VIDEO_COLOR_RANGE_16_235) {
return EGL_YUV_NARROW_RANGE_EXT;
} else {
return EGL_NONE;
}
}
static EGLint egl_horizontal_chroma_siting_from_gst_info(const GstVideoInfo *info) {
GstVideoChromaSite chroma_site = GST_VIDEO_INFO_CHROMA_SITE(info);
if (chroma_site == GST_VIDEO_CHROMA_SITE_H_COSITED || chroma_site == GST_VIDEO_CHROMA_SITE_COSITED) {
return EGL_YUV_CHROMA_SITING_0_EXT;
} else if (chroma_site == GST_VIDEO_CHROMA_SITE_V_COSITED || chroma_site == GST_VIDEO_CHROMA_SITE_NONE) {
return EGL_YUV_CHROMA_SITING_0_5_EXT;
} else {
return EGL_NONE;
}
}
static EGLint egl_vertical_chroma_siting_from_gst_info(const GstVideoInfo *info) {
GstVideoChromaSite chroma_site = GST_VIDEO_INFO_CHROMA_SITE(info);
if (chroma_site == GST_VIDEO_CHROMA_SITE_V_COSITED || chroma_site == GST_VIDEO_CHROMA_SITE_COSITED) {
return EGL_YUV_CHROMA_SITING_0_EXT;
} else if (chroma_site == GST_VIDEO_CHROMA_SITE_H_COSITED || chroma_site == GST_VIDEO_CHROMA_SITE_NONE) {
return EGL_YUV_CHROMA_SITING_0_5_EXT;
} else {
return EGL_NONE;
}
}
struct video_frame *frame_new(struct frame_interface *interface, GstSample *sample, const GstVideoInfo *info) {
#define PUT_ATTR(_key, _value) \
do { \
assert(attr_index + 2 <= ARRAY_SIZE(attributes)); \
attributes[attr_index++] = (_key); \
attributes[attr_index++] = (_value); \
} while (false)
struct video_frame *frame;
struct plane_info planes[MAX_N_PLANES];
GstVideoInfo _info;
EGLBoolean egl_ok;
GstBuffer *buffer;
EGLImageKHR egl_image;
gboolean gst_ok;
uint32_t drm_format;
GstCaps *caps;
GLuint texture;
GLenum gl_error;
EGLint egl_error;
EGLint attributes[2 * 7 + MAX_N_PLANES * 2 * 5 + 1];
EGLint egl_color_space, egl_sample_range_hint, egl_horizontal_chroma_siting, egl_vertical_chroma_siting;
int ok, width, height, n_planes, attr_index;
buffer = gst_sample_get_buffer(sample);
if (buffer == NULL) {
LOG_ERROR("Could not get buffer from video sample.\n");
return NULL;
}
// If we don't have an explicit info given, we determine it from the sample caps.
if (info == NULL) {
caps = gst_sample_get_caps(sample);
if (caps == NULL) {
return NULL;
}
info = &_info;
gst_ok = gst_video_info_from_caps(&_info, caps);
if (gst_ok == FALSE) {
LOG_ERROR("Could not get video info from video sample caps.\n");
return NULL;
}
} else {
caps = NULL;
}
// Determine some basic frame info.
width = GST_VIDEO_INFO_WIDTH(info);
height = GST_VIDEO_INFO_HEIGHT(info);
n_planes = GST_VIDEO_INFO_N_PLANES(info);
// query the drm format for this sample
drm_format = drm_format_from_gst_info(info);
if (drm_format == DRM_FORMAT_INVALID) {
LOG_ERROR("Video format has no EGL equivalent.\n");
return NULL;
}
bool external_only;
for_each_format_in_frame_interface(i, format, interface) {
if (format->format == drm_format && format->modifier == DRM_FORMAT_MOD_LINEAR) {
external_only = format->external_only;
goto format_supported;
}
}
LOG_ERROR(
"Video format is not supported by EGL: %" DRM_FOURCC_FORMAT " (modifier: %" PRIu64 ").\n",
DRM_FOURCC_ARGS(drm_format),
(uint64_t) DRM_FORMAT_MOD_LINEAR
);
return NULL;
format_supported:
// query the color space for this sample
egl_color_space = egl_color_space_from_gst_info(info);
// check the sample range
egl_sample_range_hint = egl_sample_range_hint_from_gst_info(info);
// check the chroma siting
egl_horizontal_chroma_siting = egl_horizontal_chroma_siting_from_gst_info(info);
egl_vertical_chroma_siting = egl_vertical_chroma_siting_from_gst_info(info);
frame = malloc(sizeof *frame);
if (frame == NULL) {
return NULL;
}
ok = get_plane_infos(buffer, info, interface->gbm_device, planes);
if (ok != 0) {
goto fail_free_frame;
}
// Start putting together the EGL attributes.
attr_index = 0;
#ifndef EGL_EXT_image_dma_buf_import
#error "EGL header definitions for extension EGL_EXT_image_dma_buf_import are required."
#endif
// first, put some of our basic attributes like
// frame size and format
PUT_ATTR(EGL_WIDTH, width);
PUT_ATTR(EGL_HEIGHT, height);
PUT_ATTR(EGL_LINUX_DRM_FOURCC_EXT, drm_format);
// if we have a color space, put that too
// could be one of EGL_ITU_REC601_EXT, EGL_ITU_REC709_EXT or EGL_ITU_REC2020_EXT
if (egl_color_space != EGL_NONE) {
PUT_ATTR(EGL_YUV_COLOR_SPACE_HINT_EXT, egl_color_space);
}
// if we have information about the sample range, put that into the attributes too
if (egl_sample_range_hint != EGL_NONE) {
PUT_ATTR(EGL_SAMPLE_RANGE_HINT_EXT, egl_sample_range_hint);
}
// chroma siting
if (egl_horizontal_chroma_siting != EGL_NONE) {
PUT_ATTR(EGL_YUV_CHROMA_HORIZONTAL_SITING_HINT_EXT, egl_horizontal_chroma_siting);
}
if (egl_vertical_chroma_siting != EGL_NONE) {
PUT_ATTR(EGL_YUV_CHROMA_VERTICAL_SITING_HINT_EXT, egl_vertical_chroma_siting);
}
// add plane 1
PUT_ATTR(EGL_DMA_BUF_PLANE0_FD_EXT, planes[0].fd);
PUT_ATTR(EGL_DMA_BUF_PLANE0_OFFSET_EXT, planes[0].offset);
PUT_ATTR(EGL_DMA_BUF_PLANE0_PITCH_EXT, planes[0].pitch);
if (planes[0].has_modifier) {
if (interface->supports_extended_imports) {
#ifdef EGL_EXT_image_dma_buf_import_modifiers
PUT_ATTR(EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT, uint32_to_int32(planes[0].modifier & 0xFFFFFFFFlu));
PUT_ATTR(EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, uint32_to_int32(planes[0].modifier >> 32));
#else
UNREACHABLE();
#endif
} else {
LOG_ERROR(
"video frame buffer uses modified format but EGL doesn't support the EGL_EXT_image_dma_buf_import_modifiers extension.\n"
);
goto fail_release_planes;
}
}
// add plane 2 (if present)
if (n_planes >= 2) {
PUT_ATTR(EGL_DMA_BUF_PLANE1_FD_EXT, planes[1].fd);
PUT_ATTR(EGL_DMA_BUF_PLANE1_OFFSET_EXT, planes[1].offset);
PUT_ATTR(EGL_DMA_BUF_PLANE1_PITCH_EXT, planes[1].pitch);
if (planes[1].has_modifier) {
if (interface->supports_extended_imports) {
#ifdef EGL_EXT_image_dma_buf_import_modifiers
PUT_ATTR(EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT, uint32_to_int32(planes[1].modifier & 0xFFFFFFFFlu));
PUT_ATTR(EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT, uint32_to_int32(planes[1].modifier >> 32));
#else
UNREACHABLE();
#endif
} else {
LOG_ERROR(
"video frame buffer uses modified format but EGL doesn't support the EGL_EXT_image_dma_buf_import_modifiers "
"extension.\n"
);
goto fail_release_planes;
}
}
}
// add plane 3 (if present)
if (n_planes >= 3) {
PUT_ATTR(EGL_DMA_BUF_PLANE2_FD_EXT, planes[2].fd);
PUT_ATTR(EGL_DMA_BUF_PLANE2_OFFSET_EXT, planes[2].offset);
PUT_ATTR(EGL_DMA_BUF_PLANE2_PITCH_EXT, planes[2].pitch);
if (planes[2].has_modifier) {
if (interface->supports_extended_imports) {
#ifdef EGL_EXT_image_dma_buf_import_modifiers
PUT_ATTR(EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT, uint32_to_int32(planes[2].modifier & 0xFFFFFFFFlu));
PUT_ATTR(EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT, uint32_to_int32(planes[2].modifier >> 32));
#else