-
Notifications
You must be signed in to change notification settings - Fork 7
/
utest-imr.c
1493 lines (1167 loc) · 45.7 KB
/
utest-imr.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
/*******************************************************************************
* utest-imr.c
*
* ADAS unit-test. IMR module interface
*
* Copyright (c) 2015 Cogent Embedded Inc. ALL RIGHTS RESERVED.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*******************************************************************************/
#define MODULE_TAG IMR
/*******************************************************************************
* Includes
******************************************************************************/
#include "utest-imr.h"
#include "utest-vsink.h"
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/epoll.h>
#include <linux/version.h>
#include <linux/videodev2.h>
#include "imr-v4l2-api.h"
#include <math.h>
/*******************************************************************************
* Tracing configuration
******************************************************************************/
TRACE_TAG(INIT, 1);
TRACE_TAG(INFO, 1);
TRACE_TAG(DEBUG, 1);
/*******************************************************************************
* Local types definitions
******************************************************************************/
/* ...IMR device data */
typedef struct imr_device
{
/* ...V4L2 file decriptor */
int vfd;
/* ...input/output buffers pool length */
int size;
/* ...input/output buffers dimensions */
int w, h, W, H;
/* ...output buffers pool */
imr_buffer_t *pool;
/* ...pending input buffers */
GQueue input;
/* ...streaming status */
int active;
/* ...number of submitted/busy buffer-pairs */
int submitted, busy;
/* ...index of next buffer-pair to submit to device */
int index;
/* ...frame sequence number */
u32 sequence;
/* ...length of input/output buffers */
u32 input_length, output_length;
/* ...processing time estimation */
u32 ts_acc;
} imr_device_t;
/* ...distortion correction engine data */
typedef struct imr_data
{
/* ...number of devices */
int num;
/* ...device-specific data */
imr_device_t *dev;
/* ...epoll file descriptor */
int efd;
/* ...engine activity flag */
int active;
/* ...module status */
u32 flags;
/* ...internal data access lock */
pthread_mutex_t lock;
/* ...processing thread */
pthread_t thread;
/* ...user-provided callbacks */
camera_callback_t *cb;
/* ...application callback data */
void *cdata;
} imr_data_t;
/*******************************************************************************
* Custom buffer metadata implementation
******************************************************************************/
/* ...metadata type registration */
GType imr_meta_api_get_type(void)
{
static volatile GType type;
static const gchar *tags[] = { GST_META_TAG_MEMORY_STR, NULL };
if (g_once_init_enter(&type))
{
GType _type = gst_meta_api_type_register("IMRMetaAPI", tags);
g_once_init_leave(&type, _type);
}
return type;
}
/* ...low-level interface */
static gboolean imr_meta_init(GstMeta *meta, gpointer params, GstBuffer *buffer)
{
imr_meta_t *_meta = (imr_meta_t *) meta;
/* ...reset fields */
memset(&_meta->meta + 1, 0, sizeof(imr_meta_t) - sizeof(GstMeta));
return TRUE;
}
/* ...metadata transformation */
static gboolean imr_meta_transform(GstBuffer *transbuf, GstMeta *meta,
GstBuffer *buffer, GQuark type, gpointer data)
{
imr_meta_t *_meta = (imr_meta_t *) meta, *_tmeta;
/* ...add JPU metadata for a transformed buffer */
_tmeta = gst_buffer_add_imr_meta(transbuf);
/* ...just copy data regardless of transform type? */
memcpy(&_tmeta->meta + 1, &_meta->meta + 1, sizeof(imr_meta_t) - sizeof(GstMeta));
return TRUE;
}
/* ...metadata release */
static void imr_meta_free(GstMeta *meta, GstBuffer *buffer)
{
imr_meta_t *_meta = (imr_meta_t *) meta;
/* ...anything to destroy? - tbd */
TRACE(DEBUG, _b("free metadata %p"), _meta);
}
/* ...register metadata implementation */
const GstMetaInfo * imr_meta_get_info(void)
{
static const GstMetaInfo *meta_info = NULL;
if (g_once_init_enter(&meta_info))
{
const GstMetaInfo *mi = gst_meta_register(
IMR_META_API_TYPE,
"IMRMeta",
sizeof(imr_meta_t),
imr_meta_init,
imr_meta_free,
imr_meta_transform);
g_once_init_leave (&meta_info, mi);
}
return meta_info;
}
/*******************************************************************************
* Processing time estimation
******************************************************************************/
/* ...reset FPS calculator */
static inline void imr_avg_time_reset(imr_device_t *dev)
{
/* ...reset processing time accumulator */
dev->ts_acc = 0;
}
/* ...update FPS calculator */
static inline u32 imr_avg_time_update(imr_device_t *dev, u32 delta)
{
u32 acc;
/* ...check if accumulator is initialized */
if ((acc = dev->ts_acc) == 0)
{
/* ...initialize accumulator on first invocation */
acc = delta << 4;
}
else
{
/* ...accumulator is setup already; do exponential averaging */
acc += delta - ((acc + 8) >> 4);
}
/* ...calculate current frame-rate */
TRACE(DEBUG, _b("delta: %u, acc: %u, fps: %f"), delta, acc, (acc ? 1e+6 / ((acc + 8) >> 4) : 0));
/* ...update timestamp and accumulator values */
dev->ts_acc = acc;
return (acc + 8) >> 4;
}
/* ...get current average processing time */
static inline u32 imr_avg_time(imr_device_t *dev)
{
return (dev->ts_acc + 8) >> 4;
}
/*******************************************************************************
* V4L2 IMR interface helpers
******************************************************************************/
/* ...check video device capabilities */
static inline int __imr_check_caps(int vfd)
{
struct v4l2_capability cap;
u32 caps;
/* ...query device capabilities */
CHK_API(ioctl(vfd, VIDIOC_QUERYCAP, &cap));
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
caps = cap.device_caps;
#else
caps = cap.capabilities;
#endif
/* ...check for a required capabilities */
if (!(caps & V4L2_CAP_VIDEO_OUTPUT))
{
TRACE(ERROR, _x("single-planar input expected: %X"), caps);
return -1;
}
else if (!(caps & V4L2_CAP_VIDEO_CAPTURE))
{
TRACE(ERROR, _x("single-planar output expected: %X"), caps);
return -1;
}
else if (!(caps & V4L2_CAP_STREAMING))
{
TRACE(ERROR, _x("streaming I/O is expected: %X"), caps);
return -1;
}
return 0;
}
/* ...mapping between Gstreamer and V4L2 pixel-formats */
static inline u32 __pixfmt_gst_to_v4l2(int format)
{
switch (format)
{
case GST_VIDEO_FORMAT_RGB16: return V4L2_PIX_FMT_RGB565;
case GST_VIDEO_FORMAT_RGB15: return V4L2_PIX_FMT_RGB555;
case GST_VIDEO_FORMAT_NV16: return V4L2_PIX_FMT_NV16;
case GST_VIDEO_FORMAT_NV12: return V4L2_PIX_FMT_NV12;
case GST_VIDEO_FORMAT_UYVY: return V4L2_PIX_FMT_UYVY;
case GST_VIDEO_FORMAT_YVYU: return V4L2_PIX_FMT_YVYU;
case GST_VIDEO_FORMAT_YUY2: return V4L2_PIX_FMT_YUYV;
case GST_VIDEO_FORMAT_GRAY8: return V4L2_PIX_FMT_GREY;
case GST_VIDEO_FORMAT_GRAY16_BE: return V4L2_PIX_FMT_Y10;
default: return 0;
}
}
static inline u32 __pixfmt_image_size(u32 w, u32 h, GstVideoFormat format)
{
switch (format)
{
case GST_VIDEO_FORMAT_ARGB: return w * h * 4;
case GST_VIDEO_FORMAT_RGB16: return w * h * 2;
case GST_VIDEO_FORMAT_NV16: return w * h * 2;
case GST_VIDEO_FORMAT_UYVY: return w * h * 2;
case GST_VIDEO_FORMAT_YUY2: return w * h * 2;
case GST_VIDEO_FORMAT_YVYU: return w * h * 2;
case GST_VIDEO_FORMAT_NV12: return w * h * 3 / 2;
case GST_VIDEO_FORMAT_GRAY8: return w * h;
case GST_VIDEO_FORMAT_GRAY16_BE: return w * h * 2;
default: return 0;
}
}
/* ...prepare IMR module for operation */
static inline int imr_set_formats(int vfd, u32 w, u32 h, u32 W, u32 H, u32 ifmt, u32 ofmt)
{
struct v4l2_format fmt;
/* ...set input format */
memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
fmt.fmt.pix.pixelformat = ifmt;
fmt.fmt.pix.field = V4L2_FIELD_ANY;
fmt.fmt.pix.width = w;
fmt.fmt.pix.height = h;
CHK_API(ioctl(vfd, VIDIOC_S_FMT, &fmt));
TRACE(INFO, _b("requested format: %u * %u, adjusted: %u * %u"), w, h, fmt.fmt.pix.width, fmt.fmt.pix.height);
/* ...verify actual width/height haven't been changed */
CHK_ERR(fmt.fmt.pix.width == w && fmt.fmt.pix.height == h, -(errno = ERANGE));
/* ...set output format */
memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.pixelformat = ofmt;
fmt.fmt.pix.field = V4L2_FIELD_ANY;
fmt.fmt.pix.width = W;
fmt.fmt.pix.height = H;
CHK_API(ioctl(vfd, VIDIOC_S_FMT, &fmt));
TRACE(INFO, _b("requested output format: %u * %u, adjusted: %u * %u"), W, H, fmt.fmt.pix.width, fmt.fmt.pix.height);
/* ...verify actual width/height haven't been changed */
CHK_ERR(fmt.fmt.pix.width == W && fmt.fmt.pix.height == H, -(errno = ERANGE));
return 0;
}
/* ...start/stop streaming on specific V4L2 device */
static inline int imr_streaming_enable(int vfd, int enable)
{
int opcode = (enable ? VIDIOC_STREAMON : VIDIOC_STREAMOFF);
int type;
CHK_API(ioctl(vfd, opcode, (type = V4L2_BUF_TYPE_VIDEO_OUTPUT, &type)));
CHK_API(ioctl(vfd, opcode, (type = V4L2_BUF_TYPE_VIDEO_CAPTURE, &type)));
return 0;
}
/* ...allocate buffer pool */
static inline int imr_allocate_buffers(int vfd, int num)
{
struct v4l2_requestbuffers reqbuf;
/* ...allocate input buffers (user-provided memory) */
memset(&reqbuf, 0, sizeof(reqbuf));
reqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
reqbuf.memory = V4L2_MEMORY_USERPTR;
reqbuf.count = num;
CHK_API(ioctl(vfd, VIDIOC_REQBUFS, &reqbuf));
CHK_ERR(reqbuf.count == (u32)num, -(errno = ENOMEM));
/* ...allocate output buffers */
memset(&reqbuf, 0, sizeof(reqbuf));
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbuf.memory = V4L2_MEMORY_USERPTR;
reqbuf.count = num;
CHK_API(ioctl(vfd, VIDIOC_REQBUFS, &reqbuf));
CHK_ERR(reqbuf.count == (u32)num, -(errno = ENOMEM));
TRACE(INFO, _b("buffer-pool allocated (%u buffers)"), num);
/* ...enable streaming as soon as we are done */
//CHK_API(imr_streaming_enable(vfd, 1));
return 0;
}
/* ...destroy output/capture buffer pool */
static inline int imr_destroy_buffers(int vfd)
{
struct v4l2_requestbuffers reqbuf;
/* ...disable streaming before releasing buffers */
CHK_API(imr_streaming_enable(vfd, 0));
/* ...release kernel-allocated input buffers */
memset(&reqbuf, 0, sizeof(reqbuf));
reqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
reqbuf.memory = V4L2_MEMORY_USERPTR;
reqbuf.count = 0;
CHK_API(ioctl(vfd, VIDIOC_REQBUFS, &reqbuf));
/* ...release kernel-allocated output buffers */
memset(&reqbuf, 0, sizeof(reqbuf));
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbuf.memory = V4L2_MEMORY_USERPTR;
reqbuf.count = 0;
CHK_API(ioctl(vfd, VIDIOC_REQBUFS, &reqbuf));
TRACE(INFO, _b("buffer-pool destroyed"));
return 0;
}
/* ...submit intput/output buffer pair */
static inline int imr_buffers_enqueue(int vfd, int j, void *input, u32 ilen, void *output, u32 olen)
{
struct v4l2_buffer buf;
/* ...prepare input buffer */
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
buf.memory = V4L2_MEMORY_USERPTR;
buf.index = j;
buf.m.userptr = (unsigned long)(uintptr_t)input;
buf.length = buf.bytesused = ilen;
CHK_API(ioctl(vfd, VIDIOC_QBUF, &buf));
/* ...set buffer parameters */
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_USERPTR;
buf.index = j;
buf.m.userptr = (unsigned long)(uintptr_t)output;
buf.length = olen;
CHK_API(ioctl(vfd, VIDIOC_QBUF, &buf));
return 0;
}
/* ...dequeue buffer pair */
static inline int imr_buffers_dequeue(int vfd, int *error, u32 *duration)
{
struct v4l2_buffer buf;
int j, k;
u64 t0, t1;
/* ...dequeue input buffer */
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
buf.memory = V4L2_MEMORY_USERPTR;
CHK_API(ioctl(vfd, VIDIOC_DQBUF, &buf));
j = buf.index;
t0 = buf.timestamp.tv_sec * 1000000ULL + buf.timestamp.tv_usec;
/* ...dequeue output buffer */
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_USERPTR;
CHK_API(ioctl(vfd, VIDIOC_DQBUF, &buf));
k = buf.index;
t1 = buf.timestamp.tv_sec * 1000000ULL + buf.timestamp.tv_usec;
/* ...check buffer-pair is correctly dequeued */
CHK_ERR(j == k, -(errno = EBADFD));
/* ...put buffer procesing status */
(error ? *error = !!(buf.flags & V4L2_BUF_FLAG_ERROR) : 0);
/* ...save processing duration as needed */
(duration ? *duration = (u32)(t1 - t0) : 0);
return k;
}
/*******************************************************************************
* V4L2 decoder thread
******************************************************************************/
/* ...add device to the poll sources */
static inline int __register_poll(imr_data_t *imr, int i, int active)
{
imr_device_t *dev = &imr->dev[i];
struct epoll_event event;
/* ...specify waiting flags */
event.events = EPOLLIN, event.data.u32 = (u32)i;
BUG(!dev->active || (active && !dev->submitted), _x("invalid poll op: active=%d, streaming=%d, submitted=%d"), active, dev->active, dev->submitted);
/* ...add/remove source */
CHK_API(epoll_ctl(imr->efd, (active ? EPOLL_CTL_ADD : EPOLL_CTL_DEL), dev->vfd, &event));
TRACE(DEBUG, _b("#%d: poll source %s"), i, (active ? "added" : "removed"));
return 0;
}
/* ...submit buffer to the device (called with a decoder lock held) */
static inline int __submit_buffer(imr_data_t *imr, int i)
{
imr_device_t *dev = &imr->dev[i];
GstBuffer *buffer;
imr_buffer_t *buf;
vsink_meta_t *vmeta;
int j;
TRACE(DEBUG, _b("#%d: input: %d, submitted: %d, busy: %d"), i, g_queue_get_length(&dev->input), dev->submitted, dev->busy);
/* ...check if we have a pending input buffer */
if (g_queue_is_empty(&dev->input)) return 0;
/* ...check if we have free buffer-pair */
if (dev->submitted + dev->busy == dev->size) return 0;
/* ...get head of the queue */
buffer = g_queue_pop_head(&dev->input);
/* ...take vsink meta-data */
vmeta = gst_buffer_get_vsink_meta(buffer);
/* ...get free buffer-pair index */
buf = &dev->pool[j = dev->index];
/* ...save associated input buffer (takes buffer ownership) */
buf->input = buffer;
TRACE(DEBUG, _b("enqueue buffer #<%d,%d>"), i, j);
/* ...prepare output buffer if needed */
(imr->cb->prepare ? imr->cb->prepare(imr->cdata, i, buf->output) : 0);
/* ...submit buffer-pair to the V4L2 */
CHK_API(imr_buffers_enqueue(dev->vfd, j, vmeta->plane[0], dev->input_length, buf->data, dev->output_length));
/* ...advance writing index */
dev->index = (++j == dev->size ? 0 : j);
/* ...advance buffer sequence number */
dev->sequence++;
/* ...add poll source as required */
CHK_API(dev->submitted++ == 0 && dev->active ? __register_poll(imr, i, 1) : 0);
return 0;
}
/* ...buffer processing function (called with a lock held) */
static inline int __process_buffer(imr_data_t *imr, int i)
{
imr_device_t *dev = &imr->dev[i];
GstBuffer *buffer;
imr_buffer_t *buf;
int error;
u32 duration;
int j;
/* ...if streaming is disabled already, bail out */
if (!dev->active || !dev->submitted) return 0;
/* ...get buffer from a device */
CHK_API(j = imr_buffers_dequeue(dev->vfd, &error, &duration));
/* ...remove poll-source if last buffer is dequeued */
(--dev->submitted == 0 ? __register_poll(imr, i, 0) : 0);
/* ...estimate buffer processing time */
imr_avg_time_update(dev, duration);
TRACE(DEBUG, _b("dequeued buffer-pair #<%d,%d>, result: %d, duration: %u, submitted: %d"), i, j, error, duration, dev->submitted);
/* ...get buffer descriptor */
buf = &dev->pool[j];
/* ...return input buffer to caller */
gst_buffer_unref(buf->input);
/* ...get output buffer handle */
buffer = buf->output;
/* ...advance number of busy buffers */
dev->busy++;
/* ...release lock before passing buffer to the application */
pthread_mutex_unlock(&imr->lock);
/* ...pass output buffer to application */
if (imr->cb->process(imr->cdata, i, buffer) != 0)
{
TRACE(ERROR, _x("failed to submit buffer to the application: %m"));
}
/* ...drop the reference (buffer is now owned by application) */
gst_buffer_unref(buffer);
/* ...reaqcuire data access lock */
pthread_mutex_lock(&imr->lock);
return 0;
}
/* ...purge buffers */
static inline int __purge_buffer(imr_data_t *imr, int i)
{
imr_device_t *dev = &imr->dev[i];
int n = dev->submitted;
int N = dev->size;
int j = dev->index;
imr_buffer_t *buf;
/* ...do nothing if no submitted buffers */
if (!n) return 0;
/* ...get index if oldest submitted buffer */
dev->index = ((j -= n) < 0 ? j += N : j);
/* ...purge output queue */
while (n--)
{
TRACE(DEBUG, _b("buffer <%d:%d> purge (index=%d)"), i, j, dev->index);
/* ...get buffer descriptor */
buf = &dev->pool[j];
/* ...release input buffers only (output buffers still belong to the pool) */
gst_buffer_unref(buf->input);
/* ...advance pool position */
(++j == N ? j = 0 : 0);
}
/* ...mark we have no submitted buffers anymore */
dev->submitted = 0;
return 0;
}
/* ...V4L2 processing thread */
static void * imr_thread(void *arg)
{
imr_data_t *imr = arg;
struct epoll_event event[imr->num];
/* ...lock internal data access */
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
pthread_mutex_lock(&imr->lock);
/* ...start processing loop */
while (1)
{
int r, k;
/* ...release the lock before going to waiting state */
pthread_mutex_unlock(&imr->lock);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
TRACE(0, _b("start waiting..."));
/* ...wait for event (infinite timeout) */
r = epoll_wait(imr->efd, event, imr->num, -1);
TRACE(0, _b("waiting complete: %d"), r);
/* ...reacquire the lock */
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
pthread_mutex_lock(&imr->lock);
/* ...check operation result */
if (r < 0)
{
/* ...ignore soft interruptions (e.g. from debugger) */
if (errno == EINTR) continue;
TRACE(ERROR, _x("poll failed: %m"));
goto out;
}
/* ...process all signalled descriptors */
for (k = 0; k < r; k++)
{
int i = (int)event[k].data.u32;
/* ...process output buffers */
if (event[k].events & EPOLLIN)
{
if (__process_buffer(imr, i) < 0)
{
TRACE(ERROR, _x("processing failed: %m"));
goto out;
}
}
else
{
BUG(1, _x("invalid poll events: i=%d, event=%X"), i, event[k].events);
}
}
}
out:
/* ...release access lock */
pthread_mutex_unlock(&imr->lock);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
TRACE(INIT, _b("thread exits: %m"));
return (void *)(intptr_t)-errno;
}
/*******************************************************************************
* Output buffer pool handling
******************************************************************************/
/* ...output buffer dispose function */
static gboolean __imr_buffer_dispose(GstMiniObject *obj)
{
GstBuffer *buffer = GST_BUFFER(obj);
imr_meta_t *meta = gst_buffer_get_imr_meta(buffer);
imr_data_t *imr = (imr_data_t *)buffer->pool;
int i = meta->id, j = meta->index;
imr_device_t *dev = &imr->dev[i];
imr_buffer_t *buf = meta->buf;
gboolean destroy;
/* ...check buffer validity */
BUG((u32)i >= (u32)imr->num || (u32)j >= (u32)dev->size, _x("invalid buffer: <%d,%d>"), i, j);
/* ...lock internal data access */
pthread_mutex_lock(&imr->lock);
/* ...decrement number of busy buffers */
dev->busy--;
TRACE(DEBUG, _b("output buffer #<%d:%d> (%p) returned to pool (busy: %d)"), i, j, buffer, dev->busy);
/* ...check if buffer needs to be requeued into the pool */
if (imr->active)
{
/* ...increment buffer reference */
gst_buffer_ref(buffer);
/* ...force processing if we have a pending input queue */
if (dev->active)
{
__submit_buffer(imr, i);
}
else
{
/* ...buffer returned while streaming is off; decrement index of the buffer */
(--dev->index < 0 ? dev->index += dev->size : 0);
TRACE(DEBUG, _b("revert index: %d"), dev->index);
}
/* ...indicate the miniobject should not be freed */
destroy = FALSE;
}
else
{
TRACE(DEBUG, _b("output buffer #<%d:%d> (%p) is freed"), i, j, buffer);
/* ...mark the buffer is freed */
buf->output = NULL;
/* ...force destruction of the buffer miniobject */
destroy = TRUE;
}
/* ...release decoder access lock */
pthread_mutex_unlock(&imr->lock);
return destroy;
}
/*******************************************************************************
* Engine setup
******************************************************************************/
/* ...resume/suspend streaming */
int imr_enable(imr_data_t *imr, int enable)
{
imr_device_t *dev;
int i;
/* ...make sure engine is active */
CHK_ERR(imr->active, -EINVAL);
/* ...aqcuire the engine lock */
pthread_mutex_lock(&imr->lock);
if (enable)
{
/* ...enable streaming */
for (i = 0; i < imr->num; i++)
{
dev = &imr->dev[i];
if (dev->active) continue;
/* ...enable input/output buffers streaming */
CHK_API(imr_streaming_enable(dev->vfd, dev->active = 1));
/* ...register poll-source as required */
CHK_API(dev->submitted ? __register_poll(imr, i, 1) : 0);
/* ...submit pending input buffers as required */
CHK_API(__submit_buffer(imr, i));
}
}
else
{
/* ...disable streaming */
for (i = 0; i < imr->num; i++)
{
dev = &imr->dev[i];
if (!dev->active) continue;
/* ...unregister poll-source as required */
CHK_API(dev->submitted ? __register_poll(imr, i, 0) : 0);
/* ...disable input/output buffers streaming */
CHK_API(imr_streaming_enable(dev->vfd, dev->active = 0));
/* ...purge all submitted buffers */
CHK_API(__purge_buffer(imr, i));
}
}
/* ...release engine lock */
pthread_mutex_unlock(&imr->lock);
TRACE(INFO, _b("streaming is %sabled"), (enable ? "en" : "dis"));
return 0;
}
/* ...start module operation */
int imr_start(imr_data_t *imr)
{
pthread_attr_t attr;
/* ...set decoder active flag */
imr->active = 1;
/* ...initialize thread attributes (joinable, 128KB stack) */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_attr_setstacksize(&attr, 128 << 10);
/* ...create V4L2 decoding thread to asynchronously process buffers */
CHK_API(pthread_create(&imr->thread, &attr, imr_thread, imr));
pthread_attr_destroy(&attr);
/* ...enable streaming */
CHK_API(imr_enable(imr, 1));
return 0;
}
/*******************************************************************************
* Module initialization
******************************************************************************/
/* ...IMR engine initialization */
imr_data_t * imr_init(char **devname, int num, camera_callback_t *cb, void *cdata)
{
imr_data_t *imr;
pthread_mutexattr_t attr;
int i;
/* ...allocate IMR processor data */
CHK_ERR(imr = calloc(1, sizeof(*imr)), (errno = ENOMEM, NULL));
/* ...save application callback data */
imr->cb = cb, imr->cdata = cdata;
/* ...allocate engine-specific data */
if ((imr->dev = calloc(imr->num = num, sizeof(imr_device_t))) == NULL)
{
TRACE(ERROR, _x("failed to allocate %zu bytes"), num * sizeof(imr_device_t));
goto error;
}
/* ...create epoll descriptor */
if ((imr->efd = epoll_create(imr->num)) < 0)
{
TRACE(ERROR, _x("failed to create epoll: %m"));
goto error;
}
/* ...open V4L2 image renderer devices */
for (i = 0; i < num; i++)
{
imr_device_t *dev = &imr->dev[i];
/* ...open separate instance for an input camera */
if ((dev->vfd = open(devname[i], O_RDWR | O_NONBLOCK)) < 0)
{
TRACE(ERROR, _x("failed to open device '%s': %m"), devname[i]);
goto error_dev;
}
/* ...check device capabilities */
if (__imr_check_caps(dev->vfd))
{
TRACE(ERROR, _x("capabilities check failed"));
errno = EINVAL;
goto error_dev;
}
TRACE(DEBUG, _b("V4L2 IMR engine #%d initialized (%s)"), i, devname[i]);
}
/* ...initialize internal access lock */
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&imr->lock, &attr);
pthread_mutexattr_destroy(&attr);
TRACE(INIT, _b("distortion correction module initialized"));
return imr;
error_dev:
/* ...close all devices */
do
{
(imr->dev[i].vfd >= 0 ? close(imr->dev[i].vfd) : 0);
}
while (i--);
/* ...destroy devices memory */
free(imr->dev);
error:
/* ...close epoll file descriptor */
close(imr->efd);
/* ...release module memory */
free(imr);
return NULL;
}
/* ...distortion correction engine runtime initialization */
int imr_setup(imr_data_t *imr, int i, int w, int h, int W, int H, int ifmt, int ofmt, int size)
{
imr_device_t *dev = &imr->dev[i];
int j;
/* ...calculate input buffer length */
CHK_ERR(dev->input_length = __pixfmt_image_size(w, h, ifmt), -(errno = EINVAL));
CHK_ERR(dev->output_length = __pixfmt_image_size(W, H, ofmt), -(errno = EINVAL));
/* ...set buffers dimensions */
dev->w = w, dev->h = h, dev->W = W, dev->H = H;
/* ...set IMR format */
CHK_API(imr_set_formats(dev->vfd, w, h, W, H, __pixfmt_gst_to_v4l2(ifmt), __pixfmt_gst_to_v4l2(ofmt)));
/* ...allocate buffers pool */
CHK_ERR(dev->pool = calloc(dev->size = size, sizeof(imr_buffer_t)), -(errno = ENOMEM));
/* ...allocate V4L2 buffers */
CHK_API(imr_allocate_buffers(dev->vfd, size));
/* ...create output buffers */
for (j = 0; j < size; j++)
{
imr_buffer_t *buf = &dev->pool[j];
GstBuffer *buffer;
imr_meta_t *meta;
/* ...create output buffer (add some memory? - tbd) */
CHK_ERR(buf->output = buffer = gst_buffer_new(), -(errno = ENOMEM));
/* ...allocate metadata */
CHK_ERR(meta = gst_buffer_add_imr_meta(buffer), -(errno = ENOMEM));
meta->id = i;
meta->index = j;
meta->buf = buf;
meta->format = ofmt;
meta->width = W;
meta->height = H;
GST_META_FLAG_SET(meta, GST_META_FLAG_POOLED);
/* ...modify buffer release callback */
GST_MINI_OBJECT_CAST(buffer)->dispose = __imr_buffer_dispose;
/* ...use "pool" pointer as a custom data */
buffer->pool = (void *)imr;
/* ...notify user about buffer allocation */
CHK_API(imr->cb->allocate(imr->cdata, i, buffer));
}