-
Notifications
You must be signed in to change notification settings - Fork 17
/
avc2ts.cpp
executable file
·4514 lines (3857 loc) · 160 KB
/
avc2ts.cpp
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
//Original credits to Artem Zuikov
//clone from https://github.com/4ertus2/rpi-cctv
// Adding other functionnalities F5OEO Evariste - [email protected]
#ifndef OMX_SKIP64BIT
#define OMX_SKIP64BIT
#endif
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <iostream>
#include <inttypes.h>
#include <math.h>
#include "bcm_host.h"
#include <interface/vcos/vcos_semaphore.h>
#include <interface/vmcs_host/vchost.h>
#include <IL/OMX_Core.h>
#include <IL/OMX_Component.h>
#include <IL/OMX_Video.h>
#include <IL/OMX_Broadcom.h>
extern "C"
{
#include "libmpegts/libmpegts.h"
#include <fdk-aac/aacenc_lib.h>
}
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h> /* low-level i/o */
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include "webcam.h"
#include "grabdisplay.h"
#include "vncclient.h"
#include "ffmpegsrc.h"
#include "b101.h"
//#include <linux/videodev2.h>
#define PROGRAM_VERSION "1.0.0"
#define AUDIO_SAMPLERATE 24000
// Problem with delay increasing : https://www.raspberrypi.org/forums/viewtopic.php?f=43&t=133446
// Introductio to IL Component : http://fr.slideshare.net/pchethan/understanding-open-max-il-18376762
// Camera modes : http://picamera.readthedocs.io/en/latest/fov.html
// Understand Low latency : http://www.design-reuse.com/articles/33005/understanding-latency-in-video-compression-systems.html
namespace
{
static const char *format2str(OMX_VIDEO_CODINGTYPE c)
{
switch (c)
{
case OMX_VIDEO_CodingUnused:
return "not used";
case OMX_VIDEO_CodingAutoDetect:
return "autodetect";
case OMX_VIDEO_CodingMPEG2:
return "MPEG2";
case OMX_VIDEO_CodingH263:
return "H.263";
case OMX_VIDEO_CodingMPEG4:
return "MPEG4";
case OMX_VIDEO_CodingWMV:
return "Windows Media Video";
case OMX_VIDEO_CodingRV:
return "RealVideo";
case OMX_VIDEO_CodingAVC:
return "H.264/AVC";
case OMX_VIDEO_CodingMJPEG:
return "Motion JPEG";
case OMX_VIDEO_CodingVP6:
return "VP6";
case OMX_VIDEO_CodingVP7:
return "VP7";
case OMX_VIDEO_CodingVP8:
return "VP8";
case OMX_VIDEO_CodingYUV:
return "Raw YUV video";
case OMX_VIDEO_CodingSorenson:
return "Sorenson";
case OMX_VIDEO_CodingTheora:
return "OGG Theora";
case OMX_VIDEO_CodingMVC:
return "H.264/MVC";
default:
std::cerr << "unknown OMX_VIDEO_CODINGTYPE: " << c << std::endl;
return "unknown";
}
}
static const char *format2str(OMX_COLOR_FORMATTYPE c)
{
switch (c)
{
case OMX_COLOR_FormatUnused:
return "OMX_COLOR_FormatUnused: not used";
case OMX_COLOR_FormatMonochrome:
return "OMX_COLOR_FormatMonochrome";
case OMX_COLOR_Format8bitRGB332:
return "OMX_COLOR_Format8bitRGB332";
case OMX_COLOR_Format12bitRGB444:
return "OMX_COLOR_Format12bitRGB444";
case OMX_COLOR_Format16bitARGB4444:
return "OMX_COLOR_Format16bitARGB4444";
case OMX_COLOR_Format16bitARGB1555:
return "OMX_COLOR_Format16bitARGB1555";
case OMX_COLOR_Format16bitRGB565:
return "OMX_COLOR_Format16bitRGB565";
case OMX_COLOR_Format16bitBGR565:
return "OMX_COLOR_Format16bitBGR565";
case OMX_COLOR_Format18bitRGB666:
return "OMX_COLOR_Format18bitRGB666";
case OMX_COLOR_Format18bitARGB1665:
return "OMX_COLOR_Format18bitARGB1665";
case OMX_COLOR_Format19bitARGB1666:
return "OMX_COLOR_Format19bitARGB1666";
case OMX_COLOR_Format24bitRGB888:
return "OMX_COLOR_Format24bitRGB888";
case OMX_COLOR_Format24bitBGR888:
return "OMX_COLOR_Format24bitBGR888";
case OMX_COLOR_Format24bitARGB1887:
return "OMX_COLOR_Format24bitARGB1887";
case OMX_COLOR_Format25bitARGB1888:
return "OMX_COLOR_Format25bitARGB1888";
case OMX_COLOR_Format32bitBGRA8888:
return "OMX_COLOR_Format32bitBGRA8888";
case OMX_COLOR_Format32bitARGB8888:
return "OMX_COLOR_Format32bitARGB8888";
case OMX_COLOR_FormatYUV411Planar:
return "OMX_COLOR_FormatYUV411Planar";
case OMX_COLOR_FormatYUV411PackedPlanar:
return "OMX_COLOR_FormatYUV411PackedPlanar: Planes fragmented when a frame is split in multiple buffers";
case OMX_COLOR_FormatYUV420Planar:
return "OMX_COLOR_FormatYUV420Planar: Planar YUV, 4:2:0 (I420)";
case OMX_COLOR_FormatYUV420PackedPlanar:
return "OMX_COLOR_FormatYUV420PackedPlanar: Planar YUV, 4:2:0 (I420), planes fragmented when a frame is split in multiple buffers";
case OMX_COLOR_FormatYUV420SemiPlanar:
return "OMX_COLOR_FormatYUV420SemiPlanar, Planar YUV, 4:2:0 (NV12), U and V planes interleaved with first U value";
case OMX_COLOR_FormatYUV422Planar:
return "OMX_COLOR_FormatYUV422Planar";
case OMX_COLOR_FormatYUV422PackedPlanar:
return "OMX_COLOR_FormatYUV422PackedPlanar: Planes fragmented when a frame is split in multiple buffers";
case OMX_COLOR_FormatYUV422SemiPlanar:
return "OMX_COLOR_FormatYUV422SemiPlanar";
case OMX_COLOR_FormatYCbYCr:
return "OMX_COLOR_FormatYCbYCr";
case OMX_COLOR_FormatYCrYCb:
return "OMX_COLOR_FormatYCrYCb";
case OMX_COLOR_FormatCbYCrY:
return "OMX_COLOR_FormatCbYCrY";
case OMX_COLOR_FormatCrYCbY:
return "OMX_COLOR_FormatCrYCbY";
case OMX_COLOR_FormatYUV444Interleaved:
return "OMX_COLOR_FormatYUV444Interleaved";
case OMX_COLOR_FormatRawBayer8bit:
return "OMX_COLOR_FormatRawBayer8bit";
case OMX_COLOR_FormatRawBayer10bit:
return "OMX_COLOR_FormatRawBayer10bit";
case OMX_COLOR_FormatRawBayer8bitcompressed:
return "OMX_COLOR_FormatRawBayer8bitcompressed";
case OMX_COLOR_FormatL2:
return "OMX_COLOR_FormatL2";
case OMX_COLOR_FormatL4:
return "OMX_COLOR_FormatL4";
case OMX_COLOR_FormatL8:
return "OMX_COLOR_FormatL8";
case OMX_COLOR_FormatL16:
return "OMX_COLOR_FormatL16";
case OMX_COLOR_FormatL24:
return "OMX_COLOR_FormatL24";
case OMX_COLOR_FormatL32:
return "OMX_COLOR_FormatL32";
case OMX_COLOR_FormatYUV420PackedSemiPlanar:
return "OMX_COLOR_FormatYUV420PackedSemiPlanar: Planar YUV, 4:2:0 (NV12), planes fragmented when a frame is split in multiple buffers, U and V planes interleaved with first U value";
case OMX_COLOR_FormatYUV422PackedSemiPlanar:
return "OMX_COLOR_FormatYUV422PackedSemiPlanar: Planes fragmented when a frame is split in multiple buffers";
case OMX_COLOR_Format18BitBGR666:
return "OMX_COLOR_Format18BitBGR666";
case OMX_COLOR_Format24BitARGB6666:
return "OMX_COLOR_Format24BitARGB6666";
case OMX_COLOR_Format24BitABGR6666:
return "OMX_COLOR_Format24BitABGR6666";
case OMX_COLOR_Format32bitABGR8888:
return "OMX_COLOR_Format32bitABGR8888";
case OMX_COLOR_Format8bitPalette:
return "OMX_COLOR_Format8bitPalette";
case OMX_COLOR_FormatYUVUV128:
return "OMX_COLOR_FormatYUVUV128";
case OMX_COLOR_FormatRawBayer12bit:
return "OMX_COLOR_FormatRawBayer12bit";
case OMX_COLOR_FormatBRCMEGL:
return "OMX_COLOR_FormatBRCMEGL";
case OMX_COLOR_FormatBRCMOpaque:
return "OMX_COLOR_FormatBRCMOpaque";
case OMX_COLOR_FormatYVU420PackedPlanar:
return "OMX_COLOR_FormatYVU420PackedPlanar";
case OMX_COLOR_FormatYVU420PackedSemiPlanar:
return "OMX_COLOR_FormatYVU420PackedSemiPlanar";
default:
std::cerr << "unknown OMX_COLOR_FORMATTYPE: " << c << std::endl;
return "unknown";
}
}
static void dump_portdef(OMX_PARAM_PORTDEFINITIONTYPE *portdef)
{
fprintf(stderr, "Port %d is %s, %s, buffers wants:%d needs:%d, size:%d, pop:%d, aligned:%d\n",
portdef->nPortIndex,
(portdef->eDir == OMX_DirInput ? "input" : "output"),
(portdef->bEnabled == OMX_TRUE ? "enabled" : "disabled"),
portdef->nBufferCountActual,
portdef->nBufferCountMin,
portdef->nBufferSize,
portdef->bPopulated,
portdef->nBufferAlignment);
OMX_VIDEO_PORTDEFINITIONTYPE *viddef = &portdef->format.video;
OMX_IMAGE_PORTDEFINITIONTYPE *imgdef = &portdef->format.image;
switch (portdef->eDomain)
{
case OMX_PortDomainVideo:
fprintf(stderr, "Video type:\n"
"\tWidth:\t\t%d\n"
"\tHeight:\t\t%d\n"
"\tStride:\t\t%d\n"
"\tSliceHeight:\t%d\n"
"\tBitrate:\t%d\n"
"\tFramerate:\t%.02f\n"
"\tError hiding:\t%s\n"
"\tCodec:\t\t%s\n"
"\tColor:\t\t%s\n",
viddef->nFrameWidth,
viddef->nFrameHeight,
viddef->nStride,
viddef->nSliceHeight,
viddef->nBitrate,
((float)viddef->xFramerate / (float)65536),
(viddef->bFlagErrorConcealment == OMX_TRUE ? "yes" : "no"),
format2str(viddef->eCompressionFormat),
format2str(viddef->eColorFormat));
break;
case OMX_PortDomainImage:
fprintf(stderr, "Image type:\n"
"\tWidth:\t\t%d\n"
"\tHeight:\t\t%d\n"
"\tStride:\t\t%d\n"
"\tSliceHeight:\t%d\n"
"\tError hiding:\t%s\n"
"\tCodec:\t\t%s\n"
"\tColor:\t\t%s\n",
imgdef->nFrameWidth,
imgdef->nFrameHeight,
imgdef->nStride,
imgdef->nSliceHeight,
(imgdef->bFlagErrorConcealment == OMX_TRUE ? "yes" : "no"),
format2str((OMX_VIDEO_CODINGTYPE)imgdef->eCompressionFormat),
format2str(imgdef->eColorFormat));
break;
default:
break;
}
}
const char *eventType2Str(OMX_EVENTTYPE eEvent)
{
switch (eEvent)
{
case OMX_EventCmdComplete:
return "OMX_EventCmdComplete";
case OMX_EventError:
return "OMX_EventError";
case OMX_EventMark:
return "OMX_EventMark";
case OMX_EventPortSettingsChanged:
return "OMX_EventPortSettingsChanged";
case OMX_EventBufferFlag:
return "OMX_EventBufferFlag";
case OMX_EventResourcesAcquired:
return "OMX_EventResourcesAcquired";
case OMX_EventComponentResumed:
return "OMX_EventComponentResumed";
case OMX_EventDynamicResourcesAvailable:
return "OMX_EventDynamicResourcesAvailable";
case OMX_EventPortFormatDetected:
return "OMX_EventPortFormatDetected";
case OMX_EventKhronosExtensions:
return "OMX_EventKhronosExtensions";
case OMX_EventVendorStartUnused:
return "OMX_EventVendorStartUnused";
case OMX_EventParamOrConfigChanged:
return "OMX_EventParamOrConfigChanged";
default:
break;
};
return nullptr;
}
static void printEvent(const char *compName, OMX_HANDLETYPE hComponent, OMX_EVENTTYPE eEvent, OMX_U32 nData1, OMX_U32 nData2)
{
const char *strEvent = eventType2Str(eEvent);
if (strEvent)
fprintf(stderr, "%s (%p) %s, data: %d, %d\n", compName, hComponent, strEvent, nData1, nData2);
else
fprintf(stderr, "%s (%p) 0x%08x, data: %d, %d\n", compName, hComponent, eEvent, nData1, nData2);
}
} // namespace
namespace broadcom
{
// TODO: add all
typedef enum
{
VIDEO_SCHEDULER = 10,
SOURCE = 20,
RESIZER = 60,
CAMERA = 70,
CLOCK = 80,
VIDEO_RENDER = 90,
VIDEO_DECODER = 130,
VIDEO_ENCODER = 200,
EGL_RENDER = 220,
NULL_SINK = 240,
VIDEO_SPLITTER = 250,
IMAGE_ENCODE = 340
} ComponentType;
static const char *componentType2name(ComponentType type)
{
switch (type)
{
case VIDEO_SCHEDULER:
return "OMX.broadcom.video_scheduler";
case SOURCE:
return "OMX.broadcom.source";
case RESIZER:
return "OMX.broadcom.resize";
case CAMERA:
return "OMX.broadcom.camera";
case CLOCK:
return "OMX.broadcom.clock";
case VIDEO_RENDER:
return "OMX.broadcom.video_render";
case VIDEO_DECODER:
return "OMX.broadcom.video_decode";
case VIDEO_ENCODER:
return "OMX.broadcom.video_encode";
case EGL_RENDER:
return "OMX.broadcom.egl_render";
case NULL_SINK:
return "OMX.broadcom.null_sink";
case VIDEO_SPLITTER:
return "OMX.broadcom.video_splitter";
case IMAGE_ENCODE:
return "OMX.broadcom.image_encode";
}
return nullptr;
}
static unsigned componentPortsCount(ComponentType type)
{
switch (type)
{
case VIDEO_SCHEDULER:
return 3;
case SOURCE:
return 1;
case RESIZER:
return 2;
case CAMERA:
return 4;
case CLOCK:
return 6;
case VIDEO_RENDER:
return 1;
case VIDEO_DECODER:
return 2;
case VIDEO_ENCODER:
return 2;
case EGL_RENDER:
return 2;
case NULL_SINK:
return 3;
case VIDEO_SPLITTER:
return 5;
case IMAGE_ENCODE:
return 2;
}
return 0;
}
struct VcosSemaphore
{
VcosSemaphore(const char *name)
{
if (vcos_semaphore_create(&sem_, name, 1) != VCOS_SUCCESS)
throw "Failed to create handler lock semaphore";
}
~VcosSemaphore()
{
vcos_semaphore_delete(&sem_);
}
VCOS_STATUS_T wait() { return vcos_semaphore_wait(&sem_); }
VCOS_STATUS_T post() { return vcos_semaphore_post(&sem_); }
private:
VCOS_SEMAPHORE_T sem_;
};
class VcosLock
{
public:
VcosLock(VcosSemaphore *sem)
: sem_(sem)
{
sem_->wait();
}
~VcosLock()
{
sem_->post();
}
private:
VcosSemaphore *sem_;
};
} // namespace broadcom
namespace rpi_omx
{
typedef broadcom::ComponentType ComponentType;
using broadcom::componentPortsCount;
using broadcom::componentType2name;
using broadcom::VcosSemaphore;
using Lock = broadcom::VcosLock;
VcosSemaphore *pSemaphore;
//
static OMX_ERRORTYPE callback_EventHandler(
OMX_HANDLETYPE hComponent,
OMX_PTR pAppData,
OMX_EVENTTYPE eEvent,
OMX_U32 nData1,
OMX_U32 nData2,
OMX_PTR pEventData);
static OMX_ERRORTYPE callback_EmptyBufferDone(
OMX_HANDLETYPE hComponent,
OMX_PTR pAppData,
OMX_BUFFERHEADERTYPE *pBuffer);
static OMX_ERRORTYPE callback_FillBufferDone(
OMX_HANDLETYPE hComponent,
OMX_PTR pAppData,
OMX_BUFFERHEADERTYPE *pBuffer);
OMX_CALLBACKTYPE cbsEvents = {
.EventHandler = callback_EventHandler,
.EmptyBufferDone = callback_EmptyBufferDone,
.FillBufferDone = callback_FillBufferDone};
//
///
class OMXExeption
{
public:
static const unsigned MAX_LEN = 512;
OMXExeption(OMX_ERRORTYPE errCode, const char *file, unsigned line, const char *msg = nullptr)
: errCode_(errCode)
{
if (msg && msg[0])
snprintf(msg_, MAX_LEN, "%s:%d OpenMAX IL error: 0x%08x. %s", file, line, errCode, msg);
else
snprintf(msg_, MAX_LEN, "%s:%d OpenMAX IL error: 0x%08x", file, line, errCode);
}
OMX_ERRORTYPE code() const { return errCode_; }
const char *what() const { return msg_; }
static void die(OMX_ERRORTYPE error, const char *str)
{
const char *errStr = omxErr2str(error);
fprintf(stderr, "OMX error: %s: 0x%08x %s\n", str, error, errStr);
exit(1);
}
private:
OMX_ERRORTYPE errCode_;
char msg_[MAX_LEN];
static const char *omxErr2str(OMX_ERRORTYPE error)
{
switch (error)
{
case OMX_ErrorNone:
return "OMX_ErrorNone";
case OMX_ErrorBadParameter:
return "OMX_ErrorBadParameter";
case OMX_ErrorIncorrectStateOperation:
return "OMX_ErrorIncorrectStateOperation";
case OMX_ErrorIncorrectStateTransition:
return "OMX_ErrorIncorrectStateTransition";
case OMX_ErrorInsufficientResources:
return "OMX_ErrorInsufficientResources";
case OMX_ErrorBadPortIndex:
return "OMX_ErrorBadPortIndex";
case OMX_ErrorHardware:
return "OMX_ErrorHardware";
// ...
default:
break;
}
return "";
}
};
#define ERR_OMX(err, msg) \
if ((err) != OMX_ErrorNone) \
throw OMXExeption(err, __FILE__, __LINE__, msg)
///
struct VideoFromat
{
typedef enum
{
RATIO_4x3,
RATIO_16x9
} Ratio;
unsigned width;
unsigned height;
unsigned framerate;
Ratio ratio;
bool fov; //Field of view
};
// CAMERA NATIVE MODE V1
//# Resolution Aspect Ratio Framerates Video Image FoV Binning
//1 1920x1080 16:9 1-30fps x Partial None
//2 2592x1944 4:3 1-15fps x x Full None
//3 2592x1944 4:3 0.1666-1fps x x Full None
//4 1296x972 4:3 1-42fps x Full 2x2
//5 1296x730 16:9 1-49fps x Full 2x2
//6 640x480 4:3 42.1-60fps x Full 4x4
//7 640x480 4:3 60.1-90fps x Full 4x4
// CAMERA NATIVE MODE V2
//# Resolution Aspect Ratio Framerates Video Image FoV Binning
//1 1920x1080 16:9 0.1-30fps x Partial None
//2 3280x2464 4:3 0.1-15fps x x Full None
//3 3280x2464 4:3 0.1-15fps x x Full None
//4 1640x1232 4:3 0.1-40fps x Full 2x2
//5 1640x922 16:9 0.1-40fps x Full 2x2
//6 1280x720 16:9 40-90fps x Partial 2x2
//7 640x480 4:3 40-90fps x Partial 2x2
static const VideoFromat VF_1920x1080 = {1920, 1080, 25, VideoFromat::RATIO_16x9, false};
//static const VideoFromat VF_2560x1920 = { 2560, 1920, 0, VideoFromat::RATIO_4x3, true };
static const VideoFromat VF_1280x960 = {1280, 960, 25, VideoFromat::RATIO_4x3, true};
static const VideoFromat VF_1280x720 = {1280, 720, 25, VideoFromat::RATIO_16x9, true};
static const VideoFromat VF_640x480 = {640, 480, 25, VideoFromat::RATIO_4x3, true};
static const VideoFromat VF_RESIZED_352x288 = {352, 288, 25, VideoFromat::RATIO_4x3, true};
static const VideoFromat VF_RESIZED_640x480 = VF_640x480;
static const VideoFromat VF_RESIZED_320x240 = {320, 240, 15, VideoFromat::RATIO_4x3, true};
static const VideoFromat VF_RESIZED_256x192 = {256, 192, 25, VideoFromat::RATIO_4x3, true};
static const VideoFromat VF_RESIZED_160x120 = {160, 120, 25, VideoFromat::RATIO_4x3, true};
static const VideoFromat VF_RESIZED_128x96 = {128, 96, 25, VideoFromat::RATIO_4x3, true};
static const VideoFromat VF_RESIZED_960x540 = {960, 540, 25, VideoFromat::RATIO_16x9, false};
static const VideoFromat VF_RESIZED_640x360 = {640, 360, 25, VideoFromat::RATIO_16x9, false};
static const VideoFromat VF_RESIZED_480x270 = {480, 270, 25, VideoFromat::RATIO_16x9, false};
static const VideoFromat VF_RESIZED_384x216 = {384, 216, 25, VideoFromat::RATIO_16x9, false};
static const VideoFromat VF_RESIZED_320x180 = {320, 180, 25, VideoFromat::RATIO_16x9, false};
static const VideoFromat VF_RESIZED_240x135 = {240, 135, 25, VideoFromat::RATIO_16x9, false};
///
template <typename T>
class Parameter
{
public:
Parameter()
{
init();
}
void init()
{
memset(¶m_, 0, sizeof(param_));
param_.nSize = sizeof(param_);
param_.nVersion.nVersion = OMX_VERSION;
param_.nVersion.s.nVersionMajor = OMX_VERSION_MAJOR;
param_.nVersion.s.nVersionMinor = OMX_VERSION_MINOR;
param_.nVersion.s.nRevision = OMX_VERSION_REVISION;
param_.nVersion.s.nStep = OMX_VERSION_STEP;
}
T &operator*() { return param_; }
T *operator&() { return ¶m_; }
T *operator->() { return ¶m_; }
const T &operator*() const { return param_; }
const T *operator&() const { return ¶m_; }
const T *operator->() const { return ¶m_; }
private:
T param_;
};
///
class OMXInit
{
public:
OMXInit()
{
ERR_OMX(OMX_Init(), "OMX initalization failed");
}
~OMXInit()
{
try
{
ERR_OMX(OMX_Deinit(), "OMX de-initalization failed");
}
catch (const OMXExeption &)
{
// TODO
}
}
};
///
class Buffer
{
public:
Buffer()
: ppBuffer_(nullptr),
fillDone_(false)
{
}
bool filled() const { return fillDone_; }
void setFilled(bool val = true)
{
Lock lock(pSemaphore); // LOCK
fillDone_ = val;
}
bool setDatasize(OMX_U32 Datasize)
{
if (Datasize <= allocSize())
{
ppBuffer_->nOffset = 0;
ppBuffer_->nFilledLen = Datasize;
return true;
}
return false;
}
OMX_BUFFERHEADERTYPE **pHeader() { return &ppBuffer_; }
OMX_BUFFERHEADERTYPE *header() { return ppBuffer_; }
OMX_U32 flags() const { return ppBuffer_->nFlags; }
OMX_U32 &flags() { return ppBuffer_->nFlags; }
OMX_U8 *data() { return ppBuffer_->pBuffer + ppBuffer_->nOffset; }
OMX_U32 dataSize() const { return ppBuffer_->nFilledLen; }
OMX_U32 allocSize() const { return ppBuffer_->nAllocLen; }
OMX_U32 TimeStamp() { return ppBuffer_->nTickCount; }
private:
OMX_BUFFERHEADERTYPE *ppBuffer_;
bool fillDone_;
};
///
class Component
{
public:
OMX_HANDLETYPE &component() { return component_; }
ComponentType type() const { return type_; }
const char *name() const { return componentType2name(type_); }
unsigned numPorts() const { return componentPortsCount(type_); }
void dumpPort(OMX_U32 nPortIndex, OMX_BOOL dumpFormats = OMX_FALSE)
{
Parameter<OMX_PARAM_PORTDEFINITIONTYPE> portdef;
getPortDefinition(nPortIndex, portdef);
dump_portdef(&portdef);
if (dumpFormats)
{
Parameter<OMX_VIDEO_PARAM_PORTFORMATTYPE> portformat;
portformat->nPortIndex = nPortIndex;
portformat->nIndex = 0;
std::cerr << "Port " << nPortIndex << " supports these video formats:" << std::endl;
for (;; portformat->nIndex++)
{
OMX_ERRORTYPE err = OMX_GetParameter(component_, OMX_IndexParamVideoPortFormat, &portformat);
if (err != OMX_ErrorNone)
break;
std::cerr << "\t" << format2str(portformat->eColorFormat)
<< ", compression: " << format2str(portformat->eCompressionFormat) << std::endl;
}
}
}
OMX_STATETYPE state()
{
OMX_STATETYPE state;
ERR_OMX(OMX_GetState(component_, &state), "OMX_GetState");
return state;
}
void switchState(OMX_STATETYPE newState)
{
unsigned value = eventState_;
ERR_OMX(OMX_SendCommand(component_, OMX_CommandStateSet, newState, NULL), "switch state");
if (!waitValue(&eventState_, value + 1))
std::cerr << name() << " lost state changed event" << std::endl;
#if 0
if (! waitStateChanged(newState))
std::cerr << name() << " state wanted: " << newState << " observed: " << state() << std::endl;
#endif
}
unsigned waitCount(OMX_U32 nPortIndex) const { return (nPortIndex == OMX_ALL) ? numPorts() : 1; }
void enablePort(OMX_U32 nPortIndex = OMX_ALL)
{
unsigned value = eventEnabled_;
ERR_OMX(OMX_SendCommand(component_, OMX_CommandPortEnable, nPortIndex, NULL), "enable port");
if (!waitValue(&eventEnabled_, value + waitCount(nPortIndex)))
std::cerr << name() << " port " << nPortIndex << " lost enable port event(s)" << std::endl;
}
void disablePort(OMX_U32 nPortIndex = OMX_ALL)
{
unsigned value = eventDisabled_;
ERR_OMX(OMX_SendCommand(component_, OMX_CommandPortDisable, nPortIndex, NULL), "disable port");
if (!waitValue(&eventDisabled_, value + waitCount(nPortIndex)))
std::cerr << name() << " port " << nPortIndex << " lost disable port event(s)" << std::endl;
}
void flushPort(OMX_U32 nPortIndex = OMX_ALL)
{
unsigned value = eventFlushed_;
ERR_OMX(OMX_SendCommand(component_, OMX_CommandFlush, nPortIndex, NULL), "flush buffers");
if (!waitValue(&eventFlushed_, value + waitCount(nPortIndex)))
std::cerr << name() << " port " << nPortIndex << " lost flush port event(s)" << std::endl;
}
void getPortDefinition(OMX_U32 nPortIndex, Parameter<OMX_PARAM_PORTDEFINITIONTYPE> &portDef)
{
portDef->nPortIndex = nPortIndex;
ERR_OMX(OMX_GetParameter(component_, OMX_IndexParamPortDefinition, &portDef), "get port definition");
}
void setPortDefinition(OMX_U32 nPortIndex, Parameter<OMX_PARAM_PORTDEFINITIONTYPE> &portDef)
{
portDef->nPortIndex = nPortIndex;
ERR_OMX(OMX_SetParameter(component_, OMX_IndexParamPortDefinition, &portDef), "set port definition");
}
void allocBuffers(OMX_U32 nPortIndex, Buffer &buffer)
{
Parameter<OMX_PARAM_PORTDEFINITIONTYPE> portDef;
getPortDefinition(nPortIndex, portDef);
//printf("Alloc Buffer with size %d\n",portDef->nBufferSize);
ERR_OMX(OMX_AllocateBuffer(component_, buffer.pHeader(), nPortIndex, NULL, portDef->nBufferSize), "OMX_AllocateBuffer");
}
void freeBuffers(OMX_U32 nPortIndex, Buffer &buffer)
{
ERR_OMX(OMX_FreeBuffer(component_, nPortIndex, buffer.header()), "OMX_FreeBuffer");
}
void callFillThisBuffer(Buffer &buffer)
{
ERR_OMX(OMX_FillThisBuffer(component_, buffer.header()), "OMX_FillThisBuffer");
}
void callEmptyThisBuffer(Buffer &buffer)
{
ERR_OMX(OMX_EmptyThisBuffer(component_, buffer.header()), "OMX_EmptyThisBuffer");
}
void eventCmdComplete(OMX_U32 cmd, OMX_U32 /*nPortIndex*/)
{
Lock lock(pSemaphore); // LOCK
switch (cmd)
{
case OMX_CommandStateSet:
++eventState_;
break;
case OMX_CommandFlush:
++eventFlushed_;
break;
case OMX_CommandPortDisable:
++eventDisabled_;
break;
case OMX_CommandPortEnable:
++eventEnabled_;
break;
case OMX_CommandMarkBuffer:
default:
break;
}
}
void eventPortSettingsChanged(OMX_U32 nPortIndex)
{
Lock lock(pSemaphore); // LOCK
++changedPorts_[n2idx(nPortIndex)];
}
protected:
OMX_HANDLETYPE component_;
ComponentType type_;
Component(ComponentType type, OMX_PTR pAppData, OMX_CALLBACKTYPE *callbacks)
: type_(type),
eventState_(0),
eventFlushed_(0),
eventDisabled_(0),
eventEnabled_(0)
{
changedPorts_.resize(numPorts());
OMX_STRING xName = const_cast<OMX_STRING>(name());
ERR_OMX(OMX_GetHandle(&component_, xName, pAppData, callbacks), "OMX_GetHandle");
disablePort();
}
~Component()
{
try
{
ERR_OMX(OMX_FreeHandle(component_), "OMX_FreeHandle");
}
catch (const OMXExeption &)
{
// TODO
}
}
// type_ equals to first port number
unsigned n2idx(OMX_U32 nPortIndex) const { return nPortIndex - type_; }
unsigned idx2n(unsigned idx) const { return type_ + idx; }
private:
static const unsigned WAIT_CHANGES_US = 1000;
static const unsigned MAX_WAIT_COUNT = 200;
unsigned eventState_;
unsigned eventFlushed_;
unsigned eventDisabled_;
unsigned eventEnabled_;
std::vector<unsigned> changedPorts_;
// TODO: wait for specific port changes
bool waitValue(unsigned *pValue, unsigned wantedValue)
{
for (unsigned i = 0; i < MAX_WAIT_COUNT; ++i)
{
if (*pValue == wantedValue)
return true;
usleep(WAIT_CHANGES_US);
}
return false;
}
#if 0
bool waitStateChanged(OMX_STATETYPE wantedState)
{
for (unsigned i=0; i < MAX_WAIT_COUNT; ++i)
{
if (state() == wantedState)
return true;
usleep(WAIT_CHANGES_US);
}
return false;
}
#endif
};
// H264 Decoder
class Decoder : public Component
{
public:
static const ComponentType cType = broadcom::VIDEO_DECODER;
static const unsigned IPORT = 130;
static const unsigned OPRT = 131;
static int32_t align(unsigned x, unsigned y)
{
return (x + y - 1) & (~(y - 1));
}
Decoder()
: Component(cType, (OMX_PTR)this, &cbsEvents),
ready_(false)
{
requestCallback();
}
void SetCodec()
{
Parameter<OMX_PARAM_PORTDEFINITIONTYPE> portDef;
getPortDefinition(IPORT, portDef);
portDef->format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
setPortDefinition(IPORT, portDef);
}
void requestCallback()
{
Parameter<OMX_CONFIG_REQUESTCALLBACKTYPE> cbtype;
cbtype->nPortIndex = OMX_ALL;
cbtype->nIndex = OMX_IndexParamCameraDeviceNumber;
cbtype->bEnable = OMX_TRUE;
ERR_OMX(OMX_SetConfig(component_, OMX_IndexConfigRequestCallback, &cbtype), "request callbacks");
}
void allocBuffers()
{
Component::allocBuffers(IPORT, bufferIn_);
}
void freeBuffers()
{
Component::freeBuffers(IPORT, bufferIn_);
}
bool ready() const { return ready_; }
void eventReady()
{
Lock lock(pSemaphore); // LOCK
ready_ = true;
}
private:
Buffer bufferIn_;
bool ready_;
};
/// Raspberry Pi Camera Module
class Camera : public Component