-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgstvspmfilter.c
1492 lines (1272 loc) · 48.6 KB
/
gstvspmfilter.c
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
/* GStreamer
* Copyright (C) <1999> Erik Walthinsen <[email protected]>
* This file:
* Copyright (C) 2003 Ronald Bultje <[email protected]>
* Copyright (C) 2010 David Schleef <[email protected]>
* Copyright (C) 2014 - 2023 Renesas Electronics Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/**
* SECTION:element-vspmfilter
*
* Convert video frames between a great variety of video formats.
*
* <refsect2>
* <title>Example launch line</title>
* |[
* gst-launch -v videotestsrc ! video/x-raw,format=\(string\)YUY2 ! vspmfilter ! ximagesink
* ]|
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "gstvspmfilter.h"
#include <gst/video/video.h>
#include <gst/video/gstvideometa.h>
#include <gst/video/gstvideopool.h>
#include <gst/gstquery.h>
#include <string.h>
#include <stdio.h>
#include "vspm_public.h"
#include "mmngr_user_public.h"
#include "mmngr_buf_user_public.h"
GST_DEBUG_CATEGORY (vspmfilter_debug);
#define GST_CAT_DEFAULT vspmfilter_debug
GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
#define VSP_FORMAT_PIXEL_MASK (0x0f00)
#define VSP_FORMAT_PIXEL_BIT (8)
/* number buffers of buffer pool */
#define MIN_BUFFERS (5)
#define MAX_BUFFERS (5)
GType gst_vspm_filter_get_type (void);
static GQuark _colorspace_quark;
#define gst_vspm_filter_parent_class parent_class
G_DEFINE_TYPE (GstVspmFilter, gst_vspm_filter, GST_TYPE_VIDEO_FILTER);
G_DEFINE_TYPE (GstVspmFilterBufferPool, gst_vspmfilter_buffer_pool, GST_TYPE_BUFFER_POOL);
#define CLEAR(x) memset (&(x), 0, sizeof (x))
static void gst_vspm_filter_set_property (GObject * object,
guint property_id, const GValue * value, GParamSpec * pspec);
static void gst_vspm_filter_get_property (GObject * object,
guint property_id, GValue * value, GParamSpec * pspec);
static GstFlowReturn
gst_vspm_filter_transform_buffer (GstVideoFilter * filter,
GstBuffer * inbuf,
GstBuffer * outbuf);
static GstFlowReturn gst_vspm_filter_allocate_buffer (GstVspmFilter * space);
static void gst_vspm_filter_free_buffer (GstVspmFilter * space);
static void gst_vspm_filter_set_buffer_info (GstVspmFilter * space,
GstVideoInfo * info, GstVideoAlignment * align);
static gboolean gst_vspm_filter_set_info (GstVideoFilter * filter,
GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
GstVideoInfo * out_info);
static GstFlowReturn gst_vspm_filter_transform_frame (GstVideoFilter * filter,
GstVideoFrame * in_frame, GstVideoFrame * out_frame);
static void gst_vspm_filter_finalize (GObject * obj);
static void gst_vspm_filter_import_fd (GstMemory *mem, gpointer *out, GQueue *import_list);
static void gst_vspm_filter_release_fd (GQueue *import_list);
struct _GstBaseTransformPrivate
{
/* Set by sub-class */
gboolean passthrough;
gboolean always_in_place;
GstCaps *cache_caps1;
gsize cache_caps1_size;
GstCaps *cache_caps2;
gsize cache_caps2_size;
gboolean have_same_caps;
gboolean negotiated;
/* QoS *//* with LOCK */
gboolean qos_enabled;
gdouble proportion;
GstClockTime earliest_time;
/* previous buffer had a discont */
gboolean discont;
GstPadMode pad_mode;
gboolean gap_aware;
gboolean prefer_passthrough;
/* QoS stats */
guint64 processed;
guint64 dropped;
GstClockTime position_out;
GstBufferPool *pool;
gboolean pool_active;
GstAllocator *allocator;
GstAllocationParams params;
GstQuery *query;
};
/* Properties */
enum
{
PROP_0,
PROP_VSPM_OUTBUF,
PROP_VSPM_DMABUF
};
static void
gst_vspmfilter_buffer_pool_free_buffer (GstBufferPool * bpool, GstBuffer * buffer)
{
/* No processing */
}
static GstFlowReturn
gst_vspmfilter_buffer_pool_alloc_buffer (GstBufferPool * bpool,
GstBuffer ** buffer, GstBufferPoolAcquireParams * params)
{
GstVspmFilterBufferPool *vspmfltpool = GST_VSPMFILTER_BUFFER_POOL_CAST (bpool);
GstVspmFilter * vspmfilter = vspmfltpool->vspmfilter;
GstBuffer *tmp;
GstFlowReturn result = GST_FLOW_OK;
VspmbufArray *vspm_outbuf;
vspm_outbuf = vspmfilter->vspm_outbuf;
tmp = g_ptr_array_index (vspm_outbuf->buf_array, vspm_outbuf->current_buffer_index);
*buffer = tmp;
vspm_outbuf->current_buffer_index ++;
if(vspm_outbuf->current_buffer_index >= vspm_outbuf->buf_array->len)
vspm_outbuf->current_buffer_index = 0;
return result;
}
static GstBufferPool *
gst_vspmfilter_buffer_pool_new (GstVspmFilter * vspmfilter)
{
GstVspmFilterBufferPool *pool;
g_return_val_if_fail (GST_IS_VIDEO_CONVERT(vspmfilter), NULL);
pool = g_object_new (GST_TYPE_VSPMFILTER_BUFFER_POOL, NULL);
pool->vspmfilter = gst_object_ref (vspmfilter);
GST_LOG_OBJECT (pool, "new vspmfilter buffer pool %p", pool);
return GST_BUFFER_POOL_CAST (pool);
}
static void
gst_vspmfilter_buffer_pool_finalize (GObject * object)
{
GstVspmFilterBufferPool *pool = GST_VSPMFILTER_BUFFER_POOL_CAST (object);
if (pool->caps)
gst_caps_unref (pool->caps);
gst_object_unref (pool->vspmfilter);
G_OBJECT_CLASS (gst_vspmfilter_buffer_pool_parent_class)->finalize (object);
}
static void
gst_vspmfilter_buffer_pool_init (GstVspmFilterBufferPool * pool)
{
/* No processing */
}
static void
gst_vspmfilter_buffer_pool_class_init (GstVspmFilterBufferPoolClass * klass)
{
GObjectClass *gobject_class = (GObjectClass *) klass;
GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
gobject_class->finalize = gst_vspmfilter_buffer_pool_finalize;
gstbufferpool_class->alloc_buffer = gst_vspmfilter_buffer_pool_alloc_buffer;
gstbufferpool_class->free_buffer = gst_vspmfilter_buffer_pool_free_buffer;
}
/* copies the given caps */
static GstCaps *
gst_vspm_filter_caps_remove_format_info (GstCaps * caps)
{
GstStructure *st;
gint i, n;
GstCaps *res;
res = gst_caps_new_empty ();
n = gst_caps_get_size (caps);
for (i = 0; i < n; i++) {
st = gst_caps_get_structure (caps, i);
/* If this is already expressed by the existing caps
* skip this structure */
if (i > 0 && gst_caps_is_subset_structure (res, st))
continue;
st = gst_structure_copy (st);
gst_structure_remove_fields (st, "format",
"colorimetry", "chroma-site", NULL);
gst_caps_append_structure (res, st);
}
return res;
}
static GstCaps *
gst_vspm_filter_fixate_caps (GstBaseTransform * trans,
GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
{
GstCaps *result;
gint from_w, from_h;
gint w = 0, h = 0;
GstStructure *ins, *outs;
GST_DEBUG_OBJECT (trans, "caps %" GST_PTR_FORMAT, caps);
GST_DEBUG_OBJECT (trans, "othercaps %" GST_PTR_FORMAT, othercaps);
othercaps = gst_caps_truncate (othercaps);
othercaps = gst_caps_make_writable (othercaps);
ins = gst_caps_get_structure (caps, 0);
outs = gst_caps_get_structure (othercaps, 0);
gst_structure_get_int (ins, "width", &from_w);
gst_structure_get_int (ins, "height", &from_h);
gst_structure_get_int (outs, "width", &w);
gst_structure_get_int (outs, "height", &h);
if (!w || !h) {
gst_structure_fixate_field_nearest_int (outs, "height", from_h);
gst_structure_fixate_field_nearest_int (outs, "width", from_w);
}
result = gst_caps_intersect (othercaps, caps);
if (gst_caps_is_empty (result)) {
gst_caps_unref (result);
result = othercaps;
} else {
gst_caps_unref (othercaps);
}
/* fixate remaining fields */
result = gst_caps_fixate (result);
GST_DEBUG_OBJECT (trans, "result caps %" GST_PTR_FORMAT, result);
return result;
}
static gboolean
gst_vspm_filter_filter_meta (GstBaseTransform * trans, GstQuery * query,
GType api, const GstStructure * params)
{
/* propose all metadata upstream */
return TRUE;
}
/* The caps can be transformed into any other caps with format info removed.
* However, we should prefer passthrough, so if passthrough is possible,
* put it first in the list. */
static GstCaps *
gst_vspm_filter_transform_caps (GstBaseTransform * btrans,
GstPadDirection direction, GstCaps * caps, GstCaps * filter)
{
GstCaps *tmp, *tmp2;
GstCaps *result;
GstCaps *caps_full_range_sizes;
GstStructure *structure;
gint i, n;
/* Get all possible caps that we can transform to */
tmp = gst_vspm_filter_caps_remove_format_info (caps);
caps_full_range_sizes = gst_caps_new_empty ();
n = gst_caps_get_size (tmp);
for (i = 0; i < n; i++) {
structure = gst_caps_get_structure (tmp, i);
/* If this is already expressed by the existing caps
* skip this structure */
if (i > 0 && gst_caps_is_subset_structure (caps_full_range_sizes,
structure))
continue;
/* make copy */
structure = gst_structure_copy (structure);
gst_structure_set (structure,
"width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
"height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
gst_caps_append_structure (caps_full_range_sizes, structure);
}
gst_caps_unref (tmp);
if (filter) {
tmp2 = gst_caps_intersect_full (filter, caps_full_range_sizes,
GST_CAPS_INTERSECT_FIRST);
gst_caps_unref (caps_full_range_sizes);
tmp = tmp2;
} else
tmp = caps_full_range_sizes;
result = tmp;
GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
GST_PTR_FORMAT, caps, result);
return result;
}
static gboolean
gst_vspm_filter_transform_meta (GstBaseTransform * trans, GstBuffer * outbuf,
GstMeta * meta, GstBuffer * inbuf)
{
const GstMetaInfo *info = meta->info;
gboolean ret;
if (gst_meta_api_type_has_tag (info->api, _colorspace_quark)) {
/* don't copy colorspace specific metadata, FIXME, we need a MetaTransform
* for the colorspace metadata. */
ret = FALSE;
} else {
/* copy other metadata */
ret = TRUE;
}
return ret;
}
struct extensions_t
{
GstVideoFormat gst_format;
guint vsp_format;
guint vsp_swap;
};
/* Note that below swap information will be REVERSED later (in function
* set_colorspace) because current system use Little Endian */
static const struct extensions_t exts[] = {
{GST_VIDEO_FORMAT_NV12, VSP_IN_YUV420_SEMI_NV12, VSP_SWAP_NO}, /* NV12 format is highest priority as most modules support this */
{GST_VIDEO_FORMAT_I420, VSP_IN_YUV420_PLANAR, VSP_SWAP_NO}, /* I420 is second priority */
{GST_VIDEO_FORMAT_YUY2, VSP_IN_YUV422_INT0_YUY2, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_UYVY, VSP_IN_YUV422_INT0_UYVY, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_RGBx, VSP_IN_RGBA8888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_BGRx, VSP_IN_ARGB8888, VSP_SWAP_B | VSP_SWAP_W}, /* Not supported in VSP. Use ARGB8888, and swap ARGB -> RABG -> BGRA */
{GST_VIDEO_FORMAT_xRGB, VSP_IN_ARGB8888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_xBGR, VSP_IN_ABGR8888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_RGBA, VSP_IN_RGBA8888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_BGRA, VSP_IN_ARGB8888, VSP_SWAP_B | VSP_SWAP_W}, /* Same as BGRA */
{GST_VIDEO_FORMAT_ARGB, VSP_IN_ARGB8888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_ABGR, VSP_IN_ABGR8888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_RGB , VSP_IN_RGB888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_BGR , VSP_IN_BGR888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_YVYU, VSP_IN_YUV422_INT0_YVYU, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_Y444, VSP_IN_YUV444_PLANAR, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_NV21, VSP_IN_YUV420_SEMI_NV21, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_v308, VSP_IN_YUV444_INTERLEAVED,VSP_SWAP_NO},
{GST_VIDEO_FORMAT_RGB16, VSP_IN_RGB565, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_NV16, VSP_IN_YUV422_SEMI_NV16, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_NV24, VSP_IN_YUV444_SEMI_PLANAR,VSP_SWAP_NO},
};
static const struct extensions_t exts_out[] = {
{GST_VIDEO_FORMAT_NV12, VSP_OUT_YUV420_SEMI_NV12, VSP_SWAP_NO}, /* NV12 format is highest priority as most modules support this */
{GST_VIDEO_FORMAT_I420, VSP_OUT_YUV420_PLANAR, VSP_SWAP_NO}, /* I420 is second priority */
{GST_VIDEO_FORMAT_YUY2, VSP_OUT_YUV422_INT0_YUY2, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_UYVY, VSP_OUT_YUV422_INT0_UYVY, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_RGBx, VSP_OUT_RGBP8888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_BGRx, VSP_OUT_PRGB8888, VSP_SWAP_B | VSP_SWAP_W}, /* Not supported in VSP. Use ARGB8888, and swap ARGB -> RABG -> BGRA */
{GST_VIDEO_FORMAT_xRGB, VSP_OUT_PRGB8888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_xBGR, VSP_OUT_PRGB8888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_RGBA, VSP_OUT_RGBP8888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_BGRA, VSP_OUT_PRGB8888, VSP_SWAP_B | VSP_SWAP_W}, /* Same as BGRA */
{GST_VIDEO_FORMAT_ARGB, VSP_OUT_PRGB8888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_ABGR, VSP_OUT_PBGR8888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_RGB , VSP_OUT_RGB888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_BGR , VSP_OUT_BGR888, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_YVYU, VSP_OUT_YUV422_INT0_YVYU, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_Y444, VSP_OUT_YUV444_PLANAR, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_NV21, VSP_OUT_YUV420_SEMI_NV21, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_v308, VSP_OUT_YUV444_INTERLEAVED,VSP_SWAP_NO},
{GST_VIDEO_FORMAT_RGB16, VSP_OUT_RGB565, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_NV16, VSP_OUT_YUV422_SEMI_NV16, VSP_SWAP_NO},
{GST_VIDEO_FORMAT_NV24, VSP_OUT_YUV444_SEMI_PLANAR,VSP_SWAP_NO},
};
static gint
set_colorspace (GstVideoFormat vid_fmt, guint * format, guint * fswap)
{
int nr_exts = sizeof (exts) / sizeof (exts[0]);
int i;
for (i = 0; i < nr_exts; i++) {
if (vid_fmt == exts[i].gst_format) {
*format = exts[i].vsp_format;
/* Need to reverse swap information for Little Endian */
*fswap = (VSP_SWAP_B | VSP_SWAP_W | VSP_SWAP_L | VSP_SWAP_LL) ^ exts[i].vsp_swap;
return 0;
}
}
return -1;
}
static gint
set_colorspace_output (GstVideoFormat vid_fmt, guint * format, guint * fswap)
{
int nr_exts = sizeof (exts_out) / sizeof (exts_out[0]);
int i;
for (i = 0; i < nr_exts; i++) {
if (vid_fmt == exts_out[i].gst_format) {
*format = exts_out[i].vsp_format;
/* Need to reverse swap information for Little Endian */
*fswap = (VSP_SWAP_B | VSP_SWAP_W | VSP_SWAP_L | VSP_SWAP_LL) ^ exts_out[i].vsp_swap;
return 0;
}
}
return -1;
}
static void
gst_vspm_filter_set_buffer_info (GstVspmFilter * space,
GstVideoInfo * info, GstVideoAlignment * align)
{
VspmBufferInfo *buf_info = &space->buf_info;
gint i;
if (info != NULL) {
buf_info->width = GST_VIDEO_INFO_WIDTH (info);
buf_info->height = GST_VIDEO_INFO_HEIGHT (info);
buf_info->format = GST_VIDEO_FORMAT_INFO_FORMAT (info->finfo);
buf_info->n_planes = GST_VIDEO_FORMAT_INFO_N_PLANES (info->finfo);
for (i = 0; i < buf_info->n_planes; i++) {
buf_info->plane_width[i] =
GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (info->finfo, i, info->width);
buf_info->plane_height[i] =
GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (info->finfo, i, info->height);
buf_info->plane_pixel_stride[i] =
GST_VIDEO_FORMAT_INFO_PSTRIDE (info->finfo, i);
}
}
buf_info->outbuf_size = 0;
memset (buf_info->plane_stride, 0, sizeof (buf_info->plane_stride));
memset (buf_info->plane_size , 0, sizeof (buf_info->plane_size));
memset (buf_info->plane_offset, 0, sizeof (buf_info->plane_offset));
for (i = 0; i < buf_info->n_planes; i++) {
gint stride = buf_info->plane_width[i] * buf_info->plane_pixel_stride[i];
gint sliceheight = buf_info->plane_height[i];
buf_info->plane_offset[i] = buf_info->outbuf_size;
/* If we have alignment requirement from downstream */
if (align) {
/* FIXME: Currently, we ignore padding and only update stride */
stride = GST_ROUND_UP_N(stride, align->stride_align[i]);
} else {
GST_DEBUG_OBJECT (space, "No stride alignment requirement from downstream");
}
buf_info->plane_stride[i] = stride;
buf_info->plane_size[i] = stride * sliceheight;
buf_info->outbuf_size += buf_info->plane_size[i];
}
return;
}
static GstFlowReturn
gst_vspm_filter_allocate_buffer (GstVspmFilter * space)
{
VspmBufferInfo *buf_info;
Vspm_mmng_ar *vspm_out;
VspmbufArray *vspm_outbuf;
gint vspm_used;
guint i, j;
gint page_size;
gint dmabuf_fd[GST_VIDEO_MAX_PLANES] = { 0, };
gint dmabuf_page_offset[GST_VIDEO_MAX_PLANES];
gint dmabuf_plane_size_ext[GST_VIDEO_MAX_PLANES];;
buf_info = &space->buf_info;
vspm_out = space->vspm_out;
vspm_outbuf = space->vspm_outbuf;
page_size = getpagesize();
for (i = 0; i < MAX_BUFFERS; i++) {
GstBuffer *buf;
vspm_used = vspm_out->used;
if (R_MM_OK == mmngr_alloc_in_user(&vspm_out->vspm[vspm_used].mmng_pid,
buf_info->outbuf_size,
&vspm_out->vspm[vspm_used].pphy_addr,
&vspm_out->vspm[vspm_used].phard_addr,
&vspm_out->vspm[vspm_used].puser_virt_addr,
MMNGR_VA_SUPPORT_CACHED)) {
vspm_out->used++;
if (space->use_dmabuf) {
buf = gst_buffer_new ();
for (j = 0; j < buf_info->n_planes; j++) {
gint res;
guint phys_addr;
GstMemory *mem;
phys_addr = (guint)vspm_out->vspm[vspm_used].phard_addr +
buf_info->plane_offset[j];
/* Calculate offset between physical address and page boundary */
dmabuf_page_offset[j] = phys_addr & (page_size - 1);
/* When downstream plugins do mapping from dmabuf fd it requires
* mapping from boundary page and size align for page size so
* memory for plane must increase to handle for this case */
dmabuf_plane_size_ext[j] = GST_ROUND_UP_N(
buf_info->plane_size[j] + dmabuf_page_offset[j], page_size);
res = mmngr_export_start_in_user (&vspm_out->vspm[vspm_used].dmabuf_pid[j],
dmabuf_plane_size_ext[j],
(unsigned long) GST_ROUND_DOWN_N(phys_addr, page_size),
&dmabuf_fd[j]);
if (res != R_MM_OK) {
GST_ERROR_OBJECT (space,
"mmngr_export_start_in_user failed (phys_addr:0x%08x)",
phys_addr);
return GST_FLOW_ERROR;
}
/* Set offset's information */
mem = gst_dmabuf_allocator_alloc (space->allocator, dmabuf_fd[j],
dmabuf_plane_size_ext[j]);
mem->offset = dmabuf_page_offset[j];
/* Only allow to access plane size */
mem->size = buf_info->plane_size[j];
gst_buffer_append_memory (buf, mem);
}
} else {
buf = gst_buffer_new_wrapped (
(gpointer)vspm_out->vspm[vspm_used].puser_virt_addr,
(gsize)buf_info->outbuf_size);
}
} else {
GST_ERROR_OBJECT (space,
"mmngr_alloc_in_user failed to allocate memory (%d)",
buf_info->outbuf_size);
return GST_FLOW_ERROR;
}
g_ptr_array_add (vspm_outbuf->buf_array, buf);
gst_buffer_add_video_meta_full(buf, GST_VIDEO_FRAME_FLAG_NONE,
buf_info->format,
buf_info->width, buf_info->height,
buf_info->n_planes,
buf_info->plane_offset, buf_info->plane_stride);
}
return GST_FLOW_OK;
}
static void
gst_vspm_filter_free_buffer (GstVspmFilter * space)
{
VspmBufferInfo *buf_info;
Vspm_mmng_ar *vspm_in, *vspm_out;
gint i, vspm_used;
VspmbufArray *vspm_outbuf;
vspm_in = space->vspm_in;
vspm_out = space->vspm_out;
vspm_outbuf = space->vspm_outbuf;
if (vspm_outbuf->buf_array->len > 0) {
g_ptr_array_remove_index(vspm_outbuf->buf_array,
vspm_outbuf->buf_array->len - 1);
}
/* Release the importing to avoid leak FD */
gst_vspm_filter_release_fd (space->mmngr_import_list);
while (vspm_in->used) {
vspm_used = vspm_in->used - 1;
for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
if(vspm_in->vspm[vspm_used].dmabuf_pid[i] >= 0) {
mmngr_import_end_in_user(vspm_in->vspm[vspm_used].dmabuf_pid[i]);
}
}
vspm_in->used--;
}
while (vspm_out->used) {
vspm_used = vspm_out->used - 1;
for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
if (vspm_out->vspm[vspm_used].dmabuf_pid[i] >= 0) {
mmngr_export_end_in_user(vspm_out->vspm[vspm_used].dmabuf_pid[i]);
}
}
if (vspm_out->vspm[vspm_used].mmng_pid >= 0) {
mmngr_free_in_user(vspm_out->vspm[vspm_used].mmng_pid);
}
vspm_out->used--;
}
}
static gboolean
gst_vspm_filter_set_info (GstVideoFilter * filter,
GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
GstVideoInfo * out_info)
{
GstVspmFilter *space;
GstStructure *structure;
gint i;
space = GST_VIDEO_CONVERT_CAST (filter);
/* these must match */
if (in_info->fps_n != out_info->fps_n || in_info->fps_d != out_info->fps_d)
goto format_mismatch;
/* if present, these must match too */
if (in_info->interlace_mode != out_info->interlace_mode)
goto format_mismatch;
GST_DEBUG ("reconfigured %d %d", GST_VIDEO_INFO_FORMAT (in_info),
GST_VIDEO_INFO_FORMAT (out_info));
if(space->outbuf_allocate) {
gst_vspm_filter_set_buffer_info (space, out_info, NULL);
if (space->out_port_pool) {
if (gst_buffer_pool_is_active (space->out_port_pool))
gst_buffer_pool_set_active (space->out_port_pool, FALSE);
gst_object_unref (space->out_port_pool);
}
/* create a new buffer pool*/
space->out_port_pool = gst_vspmfilter_buffer_pool_new (space);
structure = gst_buffer_pool_get_config (space->out_port_pool);
gst_buffer_pool_config_set_params(structure, outcaps,
space->buf_info.outbuf_size,
MIN_BUFFERS, MAX_BUFFERS);
if (!gst_buffer_pool_set_config (space->out_port_pool, structure)) {
GST_WARNING_OBJECT (space, "failed to set buffer pool configuration");
}
}
return TRUE;
/* ERRORS */
format_mismatch:
{
GST_ERROR_OBJECT (space, "input and output formats do not match");
return FALSE;
}
}
static gboolean
gst_vspm_filter_decide_allocation (GstBaseTransform * trans, GstQuery * query)
{
GstVspmFilter *space = GST_VIDEO_CONVERT_CAST(trans);
gboolean update = FALSE;
if (space->outbuf_allocate) {
GstBufferPool *pool = NULL;
GstStructure *config;
GstVideoAlignment align;
guint i;
if (gst_query_get_n_allocation_pools (query)) {
gst_query_parse_nth_allocation_pool(query, 0, &pool, NULL, NULL, NULL);
if (pool) {
config = gst_buffer_pool_get_config(pool);
/* vspmfilter always use its own buffer pool. If downstream requires video
* alignment, we consider to update it */
if (gst_buffer_pool_has_option(pool,
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT)) {
/* Get alignment */
gst_video_alignment_reset(&align);
gst_buffer_pool_config_get_video_alignment(config, &align);
/* FIXME: Currently, we ignore padding and only check stride */
GST_DEBUG_OBJECT(space, "got a stride alignment requirement from "
"downstream %d:%d:%d:%d",
align.stride_align[0], align.stride_align[1],
align.stride_align[2], align.stride_align[3]);
for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
if ((align.stride_align[i] != 0) &&
(space->buf_info.plane_stride[i] % align.stride_align[i] != 0)) {
update = TRUE;
break;
}
}
}
}
}
if (update) {
GstStructure *structure;
GstCaps *caps = NULL;
GST_DEBUG_OBJECT(space, "update buffer info and buffer pool");
if (space->out_port_pool) {
if (gst_buffer_pool_is_active (space->out_port_pool)) {
gst_buffer_pool_set_active (space->out_port_pool, FALSE);
}
}
gst_vspm_filter_set_buffer_info (space, NULL, &align);
structure = gst_buffer_pool_get_config (space->out_port_pool);
gst_buffer_pool_config_get_params(structure, &caps, NULL, NULL, NULL);
gst_buffer_pool_config_set_params(structure, caps,
space->buf_info.outbuf_size,
MIN_BUFFERS, MAX_BUFFERS);
if (!gst_buffer_pool_set_config (space->out_port_pool, structure)) {
GST_WARNING_OBJECT (space, "failed to set buffer pool configuration");
}
}
}
return TRUE;
}
static GstFlowReturn gst_vspm_filter_prepare_output_buffer (GstBaseTransform * trans,
GstBuffer *inbuf, GstBuffer **outbuf)
{
GstVspmFilter *space = GST_VIDEO_CONVERT_CAST (trans);
GstBuffer *buf;
guint n_mem,i;
GstFlowReturn ret = GST_FLOW_OK;
Vspm_mmng_ar *vspm_out;
VspmbufArray *vspm_outbuf;
vspm_out = space->vspm_out;
vspm_outbuf = space->vspm_outbuf;
if(space->outbuf_allocate) {
trans->priv->passthrough = 0; //disable pass-through mode
/* Allocate buffer and buffer pool */
if (space->vspm_out->used == 0) {
ret = gst_vspm_filter_allocate_buffer(space);
if (ret != GST_FLOW_OK) {
return ret;
} else {
if (!gst_buffer_pool_is_active(space->out_port_pool)) {
if (!gst_buffer_pool_set_active(space->out_port_pool, TRUE))
GST_WARNING_OBJECT(space, "failed to activate buffer pool");
}
}
}
ret = gst_buffer_pool_acquire_buffer(space->out_port_pool, outbuf, NULL);
if(gst_buffer_is_writable(*outbuf)) {
if (!GST_BASE_TRANSFORM_CLASS(parent_class)->copy_metadata (trans,
inbuf, *outbuf)) {
/* something failed, post a warning */
GST_ELEMENT_WARNING (trans, STREAM, NOT_IMPLEMENTED,
("could not copy metadata"), (NULL));
}
}
} else {
ret = GST_BASE_TRANSFORM_CLASS(parent_class)->prepare_output_buffer(trans,
inbuf,outbuf);
}
return ret;
}
static GstStateChangeReturn
gst_vspmfilter_change_state (GstElement * element, GstStateChange transition)
{
GstVspmFilter *space = GST_VIDEO_CONVERT_CAST (element);
switch (transition) {
case GST_STATE_CHANGE_PAUSED_TO_READY:
if (space->out_port_pool)
gst_buffer_pool_set_active (space->out_port_pool, FALSE);
break;
case GST_STATE_CHANGE_READY_TO_NULL:
if (space->out_port_pool) {
gst_object_unref (space->out_port_pool);
space->out_port_pool = NULL;
}
/* Release the importing to avoid leak FD */
gst_vspm_filter_release_fd (space->mmngr_import_list);
break;
default:
break;
}
return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
}
static void
gst_vspm_filter_class_init (GstVspmFilterClass * klass)
{
int nr_exts;
int i;
GstCaps* incaps;
GstCaps* outcaps;
GstCaps* tmpcaps;
GstPadTemplate* gst_vspm_filter_src_template;
GstPadTemplate* gst_vspm_filter_sink_template;
GObjectClass *gobject_class = (GObjectClass *) klass;
GstElementClass *gstelement_class = (GstElementClass *) klass;
GstBaseTransformClass *gstbasetransform_class =
(GstBaseTransformClass *) klass;
GstVideoFilterClass *gstvideofilter_class = (GstVideoFilterClass *) klass;
gobject_class->set_property = gst_vspm_filter_set_property;
gobject_class->get_property = gst_vspm_filter_get_property;
gobject_class->finalize = gst_vspm_filter_finalize;
incaps = gst_caps_new_empty();
outcaps = gst_caps_new_empty();
nr_exts = sizeof (exts) / sizeof (exts[0]);
for (i = 0; i < nr_exts; i++) {
tmpcaps = gst_caps_new_simple ("video/x-raw",
"format", G_TYPE_STRING, gst_video_format_to_string (exts[i].gst_format),
"width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
"height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
"framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
gst_caps_append (incaps, tmpcaps);
}
nr_exts = sizeof (exts_out) / sizeof (exts_out[0]);
for (i = 0; i < nr_exts; i++) {
tmpcaps = gst_caps_new_simple ("video/x-raw",
"format", G_TYPE_STRING, gst_video_format_to_string (exts_out[i].gst_format),
"width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
"height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
"framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
gst_caps_append (outcaps, tmpcaps);
}
gst_vspm_filter_src_template = gst_pad_template_new ("src",
GST_PAD_SRC, GST_PAD_ALWAYS, incaps);
gst_vspm_filter_sink_template = gst_pad_template_new ("sink",
GST_PAD_SINK, GST_PAD_ALWAYS, outcaps);
gst_element_class_add_pad_template (gstelement_class,
gst_vspm_filter_src_template);
gst_element_class_add_pad_template (gstelement_class,
gst_vspm_filter_sink_template);
gst_caps_unref (incaps);
gst_caps_unref (outcaps);
gst_element_class_set_static_metadata (gstelement_class,
"Colorspace and Video Size Converter with VSPM",
"Filter/Converter/Video",
"Converts colorspace and video size from one to another",
"Renesas Corporation");
g_object_class_install_property (gobject_class, PROP_VSPM_OUTBUF,
g_param_spec_boolean ("outbuf-alloc", "Use outbuf-alloc mode",
"Whether or not to self-allocate output buffer",
FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_VSPM_DMABUF,
g_param_spec_boolean ("dmabuf-use", "Use DMABUF mode",
"Whether or not to use dmabuf for output buffer",
FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
gstelement_class->change_state = gst_vspmfilter_change_state;
gstbasetransform_class->transform_caps =
GST_DEBUG_FUNCPTR (gst_vspm_filter_transform_caps);
gstbasetransform_class->fixate_caps =
GST_DEBUG_FUNCPTR (gst_vspm_filter_fixate_caps);
gstbasetransform_class->filter_meta =
GST_DEBUG_FUNCPTR (gst_vspm_filter_filter_meta);
gstbasetransform_class->transform_meta =
GST_DEBUG_FUNCPTR (gst_vspm_filter_transform_meta);
gstbasetransform_class->passthrough_on_same_caps = TRUE;
gstbasetransform_class->prepare_output_buffer =
GST_DEBUG_FUNCPTR (gst_vspm_filter_prepare_output_buffer);
gstbasetransform_class->decide_allocation =
GST_DEBUG_FUNCPTR (gst_vspm_filter_decide_allocation);
gstvideofilter_class->set_info =
GST_DEBUG_FUNCPTR (gst_vspm_filter_set_info);
gstvideofilter_class->transform_frame =
GST_DEBUG_FUNCPTR (gst_vspm_filter_transform_frame);
}
static void
gst_vspm_filter_finalize (GObject * obj)
{
GstVspmFilter *space = GST_VIDEO_CONVERT (obj);
GstVspmFilterVspInfo *vsp_info;
Vspm_mmng_ar *vspm_in;
Vspm_mmng_ar *vspm_out;
vsp_info = space->vsp_info;
vspm_in = space->vspm_in;
vspm_out = space->vspm_out;
if (vsp_info->mmngr_fd != -1) {
/* mmngr dev close */
close (vsp_info->mmngr_fd);
vsp_info->mmngr_fd = -1;
}
if (vsp_info->is_init_vspm) {
VSPM_lib_DriverQuit(vsp_info->vspm_handle);
}
if (vspm_in->used || vspm_out->used)
gst_vspm_filter_free_buffer (space);
if (space->vsp_info)
g_free (space->vsp_info);
if (space->mmngr_import_list)
g_queue_free (space->mmngr_import_list);
if (space->vspm_in)
g_free (space->vspm_in);
/* free buf_array when finalize */
if (space->vspm_outbuf->buf_array)
g_ptr_array_free(space->vspm_outbuf->buf_array, TRUE);
if (space->vspm_out)
g_free (space->vspm_out);
if (space->vspm_outbuf)
g_free (space->vspm_outbuf);
/* free space->allocator when finalize */
if (space->allocator)
gst_object_unref(space->allocator);
sem_destroy (&space->smp_wait);
G_OBJECT_CLASS (parent_class)->finalize (obj);
}
static void
gst_vspm_filter_init (GstVspmFilter * space)
{
GstVspmFilterVspInfo *vsp_info;
Vspm_mmng_ar *vspm_in;
Vspm_mmng_ar *vspm_out;
VspmbufArray *vspm_outbuf;
guint i, j;
space->vsp_info = g_malloc0 (sizeof (GstVspmFilterVspInfo));
space->vspm_in = g_malloc0 (sizeof (Vspm_mmng_ar));
space->vspm_out = g_malloc0 (sizeof (Vspm_mmng_ar));
space->vspm_outbuf = g_malloc0 (sizeof (VspmbufArray));
if (!space->vsp_info
|| !space->vspm_in
|| !space->vspm_out
|| !space->vspm_outbuf) {
GST_ELEMENT_ERROR (space, RESOURCE, NO_SPACE_LEFT,
("Could not allocate vsp info"), ("Could not allocate vsp info"));
return;