This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
forked from reufer/rpihddevice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomx.c
1444 lines (1217 loc) · 43.5 KB
/
omx.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
/*
* rpihddevice - VDR HD output device for Raspberry Pi
* Copyright (C) 2014, 2015, 2016 Thomas Reufer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <queue>
#include "omx.h"
#include "display.h"
#include "setup.h"
#include <vdr/tools.h>
#include <vdr/thread.h>
extern "C" {
#include "ilclient.h"
}
#include "bcm_host.h"
// default: 20x 81920 bytes, now 128x 64k (8M)
#define OMX_VIDEO_BUFFERS 128
#define OMX_VIDEO_BUFFERSIZE KILOBYTE(64);
// default: 16x 4096 bytes, now 128x 16k (2M)
#define OMX_AUDIO_BUFFERS 128
#define OMX_AUDIO_BUFFERSIZE KILOBYTE(16);
#define OMX_INIT_STRUCT(a) \
memset(&(a), 0, sizeof(a)); \
(a).nSize = sizeof(a); \
(a).nVersion.s.nVersionMajor = OMX_VERSION_MAJOR; \
(a).nVersion.s.nVersionMinor = OMX_VERSION_MINOR; \
(a).nVersion.s.nRevision = OMX_VERSION_REVISION; \
(a).nVersion.s.nStep = OMX_VERSION_STEP
#define OMX_AUDIO_CHANNEL_MAPPING(s, c) \
switch (c) { \
case 4: \
(s).eChannelMapping[0] = OMX_AUDIO_ChannelLF; \
(s).eChannelMapping[1] = OMX_AUDIO_ChannelRF; \
(s).eChannelMapping[2] = OMX_AUDIO_ChannelLR; \
(s).eChannelMapping[3] = OMX_AUDIO_ChannelRR; \
break; \
case 1: \
(s).eChannelMapping[0] = OMX_AUDIO_ChannelCF; \
break; \
case 8: \
(s).eChannelMapping[6] = OMX_AUDIO_ChannelLS; \
(s).eChannelMapping[7] = OMX_AUDIO_ChannelRS; \
case 6: \
(s).eChannelMapping[2] = OMX_AUDIO_ChannelCF; \
(s).eChannelMapping[3] = OMX_AUDIO_ChannelLFE; \
(s).eChannelMapping[4] = OMX_AUDIO_ChannelLR; \
(s).eChannelMapping[5] = OMX_AUDIO_ChannelRR; \
case 2: \
default: \
(s).eChannelMapping[0] = OMX_AUDIO_ChannelLF; \
(s).eChannelMapping[1] = OMX_AUDIO_ChannelRF; \
break; }
class cOmxEvents
{
public:
enum eEvent {
ePortSettingsChanged,
eConfigChanged,
eEndOfStream,
eBufferEmptied
};
struct Event
{
Event(eEvent _event, int _data)
: event(_event), data(_data) { };
eEvent event;
int data;
};
~cOmxEvents()
{
while (!m_events.empty())
{
delete m_events.front();
m_events.pop();
}
}
Event* Get(void)
{
Event* event = 0;
m_mutex.Lock();
if (!m_events.empty())
{
event = m_events.front();
m_events.pop();
}
m_mutex.Unlock();
return event;
}
void Add(Event* event)
{
m_mutex.Lock();
m_events.push(event);
m_mutex.Unlock();
}
private:
cMutex m_mutex;
std::queue<Event*> m_events;
};
const char* cOmx::errStr(int err)
{
return err == OMX_ErrorNone ? "None" :
err == OMX_ErrorInsufficientResources ? "InsufficientResources" :
err == OMX_ErrorUndefined ? "Undefined" :
err == OMX_ErrorInvalidComponentName ? "InvalidComponentName" :
err == OMX_ErrorComponentNotFound ? "ComponentNotFound" :
err == OMX_ErrorInvalidComponent ? "InvalidComponent" :
err == OMX_ErrorBadParameter ? "BadParameter" :
err == OMX_ErrorNotImplemented ? "NotImplemented" :
err == OMX_ErrorUnderflow ? "Underflow" :
err == OMX_ErrorOverflow ? "Overflow" :
err == OMX_ErrorHardware ? "Hardware" :
err == OMX_ErrorInvalidState ? "InvalidState" :
err == OMX_ErrorStreamCorrupt ? "StreamCorrupt" :
err == OMX_ErrorPortsNotCompatible ? "PortsNotCompatible" :
err == OMX_ErrorResourcesLost ? "ResourcesLost" :
err == OMX_ErrorNoMore ? "NoMore" :
err == OMX_ErrorVersionMismatch ? "VersionMismatch" :
err == OMX_ErrorNotReady ? "NotReady" :
err == OMX_ErrorTimeout ? "Timeout" :
err == OMX_ErrorSameState ? "SameState" :
err == OMX_ErrorResourcesPreempted ? "ResourcesPreempted" :
err == OMX_ErrorPortUnresponsiveDuringAllocation ? "PortUnresponsiveDuringAllocation" :
err == OMX_ErrorPortUnresponsiveDuringDeallocation ? "PortUnresponsiveDuringDeallocation" :
err == OMX_ErrorPortUnresponsiveDuringStop ? "PortUnresponsiveDuringStop" :
err == OMX_ErrorIncorrectStateTransition ? "IncorrectStateTransition" :
err == OMX_ErrorIncorrectStateOperation ? "IncorrectStateOperation" :
err == OMX_ErrorUnsupportedSetting ? "UnsupportedSetting" :
err == OMX_ErrorUnsupportedIndex ? "UnsupportedIndex" :
err == OMX_ErrorBadPortIndex ? "BadPortIndex" :
err == OMX_ErrorPortUnpopulated ? "PortUnpopulated" :
err == OMX_ErrorComponentSuspended ? "ComponentSuspended" :
err == OMX_ErrorDynamicResourcesUnavailable ? "DynamicResourcesUnavailable" :
err == OMX_ErrorMbErrorsInFrame ? "MbErrorsInFrame" :
err == OMX_ErrorFormatNotDetected ? "FormatNotDetected" :
err == OMX_ErrorContentPipeOpenFailed ? "ContentPipeOpenFailed" :
err == OMX_ErrorContentPipeCreationFailed ? "ContentPipeCreationFailed" :
err == OMX_ErrorSeperateTablesUsed ? "SeperateTablesUsed" :
err == OMX_ErrorTunnelingUnsupported ? "TunnelingUnsupported" :
err == OMX_ErrorKhronosExtensions ? "KhronosExtensions" :
err == OMX_ErrorVendorStartUnused ? "VendorStartUnused" :
err == OMX_ErrorDiskFull ? "DiskFull" :
err == OMX_ErrorMaxFileSize ? "MaxFileSize" :
err == OMX_ErrorDrmUnauthorised ? "DrmUnauthorised" :
err == OMX_ErrorDrmExpired ? "DrmExpired" :
err == OMX_ErrorDrmGeneral ? "DrmGeneral" :
"unknown";
}
void cOmx::Action(void)
{
cTimeMs timer;
while (Running())
{
while (cOmxEvents::Event* event = m_portEvents->Get())
{
switch (event->event)
{
case cOmxEvents::ePortSettingsChanged:
if (m_handlePortEvents)
HandlePortSettingsChanged(event->data);
break;
case cOmxEvents::eConfigChanged:
switch (event->data)
{
case OMX_IndexParamBrcmPixelAspectRatio:
if (m_handlePortEvents)
HandlePortSettingsChanged(131);
break;
case OMX_IndexConfigBufferStall:
if (IsBufferStall() && !IsClockFreezed() && m_onBufferStall)
m_onBufferStall(m_onBufferStallData);
break;
default:
break;
}
break;
case cOmxEvents::eEndOfStream:
if (event->data == 90 && m_onEndOfStream)
m_onEndOfStream(m_onEndOfStreamData);
break;
case cOmxEvents::eBufferEmptied:
HandlePortBufferEmptied((eOmxComponent)event->data);
break;
default:
break;
}
delete event;
}
cCondWait::SleepMs(10);
if (timer.TimedOut())
{
timer.Set(100);
Lock();
for (int i = BUFFERSTAT_FILTER_SIZE - 1; i > 0; i--)
{
m_usedAudioBuffers[i] = m_usedAudioBuffers[i - 1];
m_usedVideoBuffers[i] = m_usedVideoBuffers[i - 1];
}
Unlock();
}
}
}
bool cOmx::PollVideo(void)
{
return (m_usedVideoBuffers[0] * 100 / OMX_VIDEO_BUFFERS) < 90;
}
void cOmx::GetBufferUsage(int &audio, int &video)
{
audio = 0;
video = 0;
for (int i = 0; i < BUFFERSTAT_FILTER_SIZE; i++)
{
audio += m_usedAudioBuffers[i];
video += m_usedVideoBuffers[i];
}
audio = audio / BUFFERSTAT_FILTER_SIZE / OMX_AUDIO_BUFFERS * 100;
video = video / BUFFERSTAT_FILTER_SIZE / OMX_VIDEO_BUFFERS * 100;
}
void cOmx::HandlePortBufferEmptied(eOmxComponent component)
{
Lock();
switch (component)
{
case eVideoDecoder:
m_usedVideoBuffers[0]--;
break;
case eAudioRender:
m_usedAudioBuffers[0]--;
break;
default:
ELOG("HandlePortBufferEmptied: invalid component!");
break;
}
Unlock();
}
void cOmx::HandlePortSettingsChanged(unsigned int portId)
{
Lock();
DBG("HandlePortSettingsChanged(%d)", portId);
switch (portId)
{
case 191:
if (ilclient_setup_tunnel(&m_tun[eVideoFxToVideoScheduler], 0, 0) != 0)
ELOG("failed to setup up tunnel from video fx to scheduler!");
if (ilclient_change_component_state(m_comp[eVideoScheduler], OMX_StateExecuting) != 0)
ELOG("failed to enable video scheduler!");
break;
case 131:
OMX_PARAM_PORTDEFINITIONTYPE portdef;
OMX_INIT_STRUCT(portdef);
portdef.nPortIndex = 131;
if (OMX_GetParameter(ILC_GET_HANDLE(m_comp[eVideoDecoder]), OMX_IndexParamPortDefinition,
&portdef) != OMX_ErrorNone)
ELOG("failed to get video decoder port format!");
OMX_CONFIG_POINTTYPE pixelAspect;
OMX_INIT_STRUCT(pixelAspect);
pixelAspect.nPortIndex = 131;
if (OMX_GetParameter(ILC_GET_HANDLE(m_comp[eVideoDecoder]), OMX_IndexParamBrcmPixelAspectRatio,
&pixelAspect) != OMX_ErrorNone)
ELOG("failed to get pixel aspect ratio!");
OMX_CONFIG_INTERLACETYPE interlace;
OMX_INIT_STRUCT(interlace);
interlace.nPortIndex = 131;
if (OMX_GetConfig(ILC_GET_HANDLE(m_comp[eVideoDecoder]), OMX_IndexConfigCommonInterlace,
&interlace) != OMX_ErrorNone)
ELOG("failed to get video decoder interlace config!");
m_videoFrameFormat.width = portdef.format.video.nFrameWidth;
m_videoFrameFormat.height = portdef.format.video.nFrameHeight;
m_videoFrameFormat.pixelWidth = pixelAspect.nX;
m_videoFrameFormat.pixelHeight = pixelAspect.nY;
m_videoFrameFormat.scanMode =
interlace.eMode == OMX_InterlaceProgressive ? cScanMode::eProgressive :
interlace.eMode == OMX_InterlaceFieldSingleUpperFirst ? cScanMode::eTopFieldFirst :
interlace.eMode == OMX_InterlaceFieldSingleLowerFirst ? cScanMode::eBottomFieldFirst :
interlace.eMode == OMX_InterlaceFieldsInterleavedUpperFirst ? cScanMode::eTopFieldFirst :
interlace.eMode == OMX_InterlaceFieldsInterleavedLowerFirst ? cScanMode::eBottomFieldFirst :
cScanMode::eProgressive;
// discard 4 least significant bits, since there might be some deviation
// due to jitter in time stamps
m_videoFrameFormat.frameRate = ALIGN_UP(
portdef.format.video.xFramerate & 0xfffffff0, 1 << 16) >> 16;
// workaround for progressive streams detected as interlaced video by
// the decoder due to missing SEI parsing
// see: https://github.com/raspberrypi/firmware/issues/283
// update: with FW from 2015/01/18 this is not necessary anymore
if (m_videoFrameFormat.Interlaced() && m_videoFrameFormat.frameRate >= 50)
{
DLOG("%di looks implausible, you should use a recent firmware...",
m_videoFrameFormat.frameRate * 2);
//m_videoFormat.interlaced = false;
}
if (m_videoFrameFormat.Interlaced())
m_videoFrameFormat.frameRate = m_videoFrameFormat.frameRate * 2;
if (m_onStreamStart)
m_onStreamStart(m_onStreamStartData);
OMX_CONFIG_IMAGEFILTERPARAMSTYPE filterparam;
OMX_INIT_STRUCT(filterparam);
filterparam.nPortIndex = 191;
filterparam.eImageFilter = OMX_ImageFilterNone;
OMX_PARAM_U32TYPE extraBuffers;
OMX_INIT_STRUCT(extraBuffers);
extraBuffers.nPortIndex = 130;
if (cRpiDisplay::IsProgressive() && m_videoFrameFormat.Interlaced())
{
bool fastDeinterlace = !cRpiSetup::UseAdvancedDeinterlacer(
portdef.format.video.nFrameWidth,
portdef.format.video.nFrameHeight);
DBG("using %s deinterlacer", fastDeinterlace ? "fast" : "advanced");
filterparam.nNumParams = 4;
filterparam.nParams[0] = 3;
filterparam.nParams[1] = 0; // default frame interval
filterparam.nParams[2] = 0; // half framerate
filterparam.nParams[3] = 1; // use qpus
filterparam.eImageFilter = fastDeinterlace ?
OMX_ImageFilterDeInterlaceFast :
OMX_ImageFilterDeInterlaceAdvanced;
if (fastDeinterlace)
extraBuffers.nU32 = -2;
}
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eVideoFx]),
OMX_IndexConfigCommonImageFilterParameters, &filterparam) != OMX_ErrorNone)
ELOG("failed to set deinterlacing paramaters!");
if (OMX_SetParameter(ILC_GET_HANDLE(m_comp[eVideoFx]),
OMX_IndexParamBrcmExtraBuffers, &extraBuffers) != OMX_ErrorNone)
ELOG("failed to set video fx extra buffers!");
if (ilclient_setup_tunnel(&m_tun[eVideoDecoderToVideoFx], 0, 0) != 0)
ELOG("failed to setup up tunnel from video decoder to fx!");
if (ilclient_change_component_state(m_comp[eVideoFx], OMX_StateExecuting) != 0)
ELOG("failed to enable video fx!");
break;
case 11:
if (ilclient_setup_tunnel(&m_tun[eVideoSchedulerToVideoRender], 0, 0) != 0)
ELOG("failed to setup up tunnel from scheduler to render!");
if (ilclient_change_component_state(m_comp[eVideoRender], OMX_StateExecuting) != 0)
ELOG("failed to enable video render!");
break;
}
Unlock();
}
void cOmx::OnBufferEmpty(void *instance, COMPONENT_T *comp)
{
cOmx* omx = static_cast <cOmx*> (instance);
omx->m_portEvents->Add(
new cOmxEvents::Event(cOmxEvents::eBufferEmptied,
comp == omx->m_comp[eVideoDecoder] ? eVideoDecoder :
comp == omx->m_comp[eAudioRender] ? eAudioRender :
eInvalidComponent));
}
void cOmx::OnPortSettingsChanged(void *instance, COMPONENT_T *comp, OMX_U32 data)
{
cOmx* omx = static_cast <cOmx*> (instance);
omx->m_portEvents->Add(
new cOmxEvents::Event(cOmxEvents::ePortSettingsChanged, data));
}
void cOmx::OnConfigChanged(void *instance, COMPONENT_T *comp, OMX_U32 data)
{
cOmx* omx = static_cast <cOmx*> (instance);
omx->m_portEvents->Add(
new cOmxEvents::Event(cOmxEvents::eConfigChanged, data));
}
void cOmx::OnEndOfStream(void *instance, COMPONENT_T *comp, OMX_U32 data)
{
cOmx* omx = static_cast <cOmx*> (instance);
omx->m_portEvents->Add(
new cOmxEvents::Event(cOmxEvents::eEndOfStream, data));
}
void cOmx::OnError(void *instance, COMPONENT_T *comp, OMX_U32 data)
{
if ((OMX_S32)data != OMX_ErrorSameState)
ELOG("OmxError(%s)", errStr((int)data));
}
cOmx::cOmx() :
cThread(),
m_client(NULL),
m_setAudioStartTime(false),
m_setVideoStartTime(false),
m_setVideoDiscontinuity(false),
m_spareAudioBuffers(0),
m_spareVideoBuffers(0),
m_clockReference(eClockRefNone),
m_clockScale(0),
m_portEvents(new cOmxEvents()),
m_handlePortEvents(false),
m_onBufferStall(0),
m_onBufferStallData(0),
m_onEndOfStream(0),
m_onEndOfStreamData(0),
m_onStreamStart(0),
m_onStreamStartData(0)
{
memset(m_tun, 0, sizeof(m_tun));
memset(m_comp, 0, sizeof(m_comp));
memset(m_usedAudioBuffers, 0, sizeof m_usedAudioBuffers);
memset(m_usedVideoBuffers, 0, sizeof m_usedVideoBuffers);
m_videoFrameFormat.width = 0;
m_videoFrameFormat.height = 0;
m_videoFrameFormat.frameRate = 0;
m_videoFrameFormat.scanMode = cScanMode::eProgressive;
}
cOmx::~cOmx()
{
delete m_portEvents;
}
int cOmx::Init(int display, int layer)
{
m_client = ilclient_init();
if (m_client == NULL)
ELOG("ilclient_init() failed!");
if (OMX_Init() != OMX_ErrorNone)
ELOG("OMX_Init() failed!");
ilclient_set_error_callback(m_client, OnError, this);
ilclient_set_empty_buffer_done_callback(m_client, OnBufferEmpty, this);
ilclient_set_port_settings_callback(m_client, OnPortSettingsChanged, this);
ilclient_set_eos_callback(m_client, OnEndOfStream, this);
ilclient_set_configchanged_callback(m_client, OnConfigChanged, this);
// create video_decode
if (ilclient_create_component(m_client, &m_comp[eVideoDecoder],
"video_decode", (ILCLIENT_CREATE_FLAGS_T)
(ILCLIENT_DISABLE_ALL_PORTS | ILCLIENT_ENABLE_INPUT_BUFFERS)) != 0)
ELOG("failed creating video decoder!");
// create image_fx
if (ilclient_create_component(m_client, &m_comp[eVideoFx],
"image_fx", ILCLIENT_DISABLE_ALL_PORTS) != 0)
ELOG("failed creating video fx!");
// create video_render
if (ilclient_create_component(m_client, &m_comp[eVideoRender],
"video_render", ILCLIENT_DISABLE_ALL_PORTS) != 0)
ELOG("failed creating video render!");
//create clock
if (ilclient_create_component(m_client, &m_comp[eClock],
"clock", ILCLIENT_DISABLE_ALL_PORTS) != 0)
ELOG("failed creating clock!");
// create audio_render
if (ilclient_create_component(m_client, &m_comp[eAudioRender],
"audio_render", (ILCLIENT_CREATE_FLAGS_T)
(ILCLIENT_DISABLE_ALL_PORTS | ILCLIENT_ENABLE_INPUT_BUFFERS)) != 0)
ELOG("failed creating audio render!");
//create video_scheduler
if (ilclient_create_component(m_client, &m_comp[eVideoScheduler],
"video_scheduler", ILCLIENT_DISABLE_ALL_PORTS) != 0)
ELOG("failed creating video scheduler!");
// setup tunnels
set_tunnel(&m_tun[eVideoDecoderToVideoFx],
m_comp[eVideoDecoder], 131, m_comp[eVideoFx], 190);
set_tunnel(&m_tun[eVideoFxToVideoScheduler],
m_comp[eVideoFx], 191, m_comp[eVideoScheduler], 10);
set_tunnel(&m_tun[eVideoSchedulerToVideoRender],
m_comp[eVideoScheduler], 11, m_comp[eVideoRender], 90);
set_tunnel(&m_tun[eClockToVideoScheduler],
m_comp[eClock], 80, m_comp[eVideoScheduler], 12);
set_tunnel(&m_tun[eClockToAudioRender],
m_comp[eClock], 81, m_comp[eAudioRender], 101);
// setup clock tunnels first
if (ilclient_setup_tunnel(&m_tun[eClockToVideoScheduler], 0, 0) != 0)
ELOG("failed to setup up tunnel from clock to video scheduler!");
if (ilclient_setup_tunnel(&m_tun[eClockToAudioRender], 0, 0) != 0)
ELOG("failed to setup up tunnel from clock to audio render!");
ilclient_change_component_state(m_comp[eClock], OMX_StateExecuting);
ilclient_change_component_state(m_comp[eVideoDecoder], OMX_StateIdle);
ilclient_change_component_state(m_comp[eVideoFx], OMX_StateIdle);
ilclient_change_component_state(m_comp[eAudioRender], OMX_StateIdle);
SetDisplay(display, layer);
SetClockLatencyTarget();
SetPARChangeCallback(true);
SetBufferStallThreshold(20000);
SetClockReference(cOmx::eClockRefVideo);
FlushVideo();
FlushAudio();
Start();
return 0;
}
int cOmx::DeInit(void)
{
Cancel(-1);
m_portEvents->Add(0);
while (Active())
cCondWait::SleepMs(5);
ilclient_teardown_tunnels(m_tun);
ilclient_state_transition(m_comp, OMX_StateIdle);
ilclient_state_transition(m_comp, OMX_StateLoaded);
ilclient_cleanup_components(m_comp);
OMX_Deinit();
ilclient_destroy(m_client);
return 0;
}
void cOmx::SetBufferStallCallback(void (*onBufferStall)(void*), void* data)
{
m_onBufferStall = onBufferStall;
m_onBufferStallData = data;
}
void cOmx::SetEndOfStreamCallback(void (*onEndOfStream)(void*), void* data)
{
m_onEndOfStream = onEndOfStream;
m_onEndOfStreamData = data;
}
void cOmx::SetStreamStartCallback(void (*onStreamStart)(void*), void* data)
{
m_onStreamStart = onStreamStart;
m_onStreamStartData = data;
}
OMX_TICKS cOmx::ToOmxTicks(int64_t val)
{
OMX_TICKS ticks;
ticks.nLowPart = val;
ticks.nHighPart = val >> 32;
return ticks;
}
int64_t cOmx::FromOmxTicks(OMX_TICKS &ticks)
{
int64_t ret = ticks.nLowPart | ((int64_t)(ticks.nHighPart) << 32);
return ret;
}
void cOmx::PtsToTicks(int64_t pts, OMX_TICKS &ticks)
{
// ticks = pts * OMX_TICKS_PER_SECOND / PTSTICKS
pts = pts * 100 / 9;
ticks.nLowPart = pts;
ticks.nHighPart = pts >> 32;
}
int64_t cOmx::TicksToPts(OMX_TICKS &ticks)
{
// pts = ticks * PTSTICKS / OMX_TICKS_PER_SECOND
int64_t pts = ticks.nHighPart;
pts = (pts << 32) + ticks.nLowPart;
pts = pts * 9 / 100;
return pts;
}
int64_t cOmx::GetSTC(void)
{
int64_t stc = OMX_INVALID_PTS;
OMX_TIME_CONFIG_TIMESTAMPTYPE timestamp;
OMX_INIT_STRUCT(timestamp);
timestamp.nPortIndex = OMX_ALL;
if (OMX_GetConfig(ILC_GET_HANDLE(m_comp[eClock]),
OMX_IndexConfigTimeCurrentMediaTime, ×tamp) != OMX_ErrorNone)
ELOG("failed get current clock reference!");
else
stc = TicksToPts(timestamp.nTimestamp);
return stc;
}
bool cOmx::IsClockRunning(void)
{
OMX_TIME_CONFIG_CLOCKSTATETYPE cstate;
OMX_INIT_STRUCT(cstate);
if (OMX_GetConfig(ILC_GET_HANDLE(m_comp[eClock]),
OMX_IndexConfigTimeClockState, &cstate) != OMX_ErrorNone)
ELOG("failed get clock state!");
if (cstate.eState == OMX_TIME_ClockStateRunning)
return true;
else
return false;
}
void cOmx::StartClock(bool waitForVideo, bool waitForAudio, int preRollMs)
{
DBG("StartClock(%svideo, %saudio)",
waitForVideo ? "" : "no ",
waitForAudio ? "" : "no ");
OMX_TIME_CONFIG_CLOCKSTATETYPE cstate;
OMX_INIT_STRUCT(cstate);
cstate.eState = OMX_TIME_ClockStateRunning;
cstate.nOffset = ToOmxTicks(-1000LL * preRollMs);
if (waitForVideo)
{
cstate.eState = OMX_TIME_ClockStateWaitingForStartTime;
m_setVideoStartTime = true;
cstate.nWaitMask |= OMX_CLOCKPORT0;
}
if (waitForAudio)
{
cstate.eState = OMX_TIME_ClockStateWaitingForStartTime;
m_setAudioStartTime = true;
cstate.nWaitMask |= OMX_CLOCKPORT1;
}
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eClock]),
OMX_IndexConfigTimeClockState, &cstate) != OMX_ErrorNone)
ELOG("failed to start clock!");
}
void cOmx::StopClock(void)
{
OMX_TIME_CONFIG_CLOCKSTATETYPE cstate;
OMX_INIT_STRUCT(cstate);
cstate.eState = OMX_TIME_ClockStateStopped;
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eClock]),
OMX_IndexConfigTimeClockState, &cstate) != OMX_ErrorNone)
ELOG("failed to stop clock!");
}
void cOmx::SetClockScale(OMX_S32 scale)
{
if (scale != m_clockScale)
{
OMX_TIME_CONFIG_SCALETYPE scaleType;
OMX_INIT_STRUCT(scaleType);
scaleType.xScale = scale;
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eClock]),
OMX_IndexConfigTimeScale, &scaleType) != OMX_ErrorNone)
ELOG("failed to set clock scale (%d)!", scale);
else
m_clockScale = scale;
}
}
void cOmx::ResetClock(void)
{
OMX_TIME_CONFIG_TIMESTAMPTYPE timeStamp;
OMX_INIT_STRUCT(timeStamp);
if (m_clockReference == eClockRefAudio || m_clockReference == eClockRefNone)
{
timeStamp.nPortIndex = 81;
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eClock]),
OMX_IndexConfigTimeCurrentAudioReference, &timeStamp)
!= OMX_ErrorNone)
ELOG("failed to set current audio reference time!");
}
if (m_clockReference == eClockRefVideo || m_clockReference == eClockRefNone)
{
timeStamp.nPortIndex = 80;
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eClock]),
OMX_IndexConfigTimeCurrentVideoReference, &timeStamp)
!= OMX_ErrorNone)
ELOG("failed to set current video reference time!");
}
}
unsigned int cOmx::GetAudioLatency(void)
{
unsigned int ret = 0;
OMX_PARAM_U32TYPE u32;
OMX_INIT_STRUCT(u32);
u32.nPortIndex = 100;
if (OMX_GetConfig(ILC_GET_HANDLE(m_comp[eAudioRender]),
OMX_IndexConfigAudioRenderingLatency, &u32) != OMX_ErrorNone)
ELOG("failed get audio render latency!");
else
ret = u32.nU32;
return ret;
}
void cOmx::SetClockReference(eClockReference clockReference)
{
if (m_clockReference != clockReference)
{
OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refClock;
OMX_INIT_STRUCT(refClock);
refClock.eClock =
(clockReference == eClockRefAudio) ? OMX_TIME_RefClockAudio :
(clockReference == eClockRefVideo) ? OMX_TIME_RefClockVideo :
OMX_TIME_RefClockNone;
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eClock]),
OMX_IndexConfigTimeActiveRefClock, &refClock) != OMX_ErrorNone)
ELOG("failed set active clock reference!");
else
DBG("set active clock reference to %s",
clockReference == eClockRefAudio ? "audio" :
clockReference == eClockRefVideo ? "video" : "none");
m_clockReference = clockReference;
}
}
void cOmx::SetClockLatencyTarget(void)
{
OMX_CONFIG_LATENCYTARGETTYPE latencyTarget;
OMX_INIT_STRUCT(latencyTarget);
// latency target for clock
// values set according reference implementation in omxplayer
latencyTarget.nPortIndex = OMX_ALL;
latencyTarget.bEnabled = OMX_TRUE;
latencyTarget.nFilter = 10;
latencyTarget.nTarget = 0;
latencyTarget.nShift = 3;
latencyTarget.nSpeedFactor = -60;
latencyTarget.nInterFactor = 100;
latencyTarget.nAdjCap = 100;
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eClock]),
OMX_IndexConfigLatencyTarget, &latencyTarget) != OMX_ErrorNone)
ELOG("failed set clock latency target!");
// latency target for video render
// values set according reference implementation in omxplayer
latencyTarget.nPortIndex = 90;
latencyTarget.bEnabled = OMX_TRUE;
latencyTarget.nFilter = 2;
latencyTarget.nTarget = 4000;
latencyTarget.nShift = 3;
latencyTarget.nSpeedFactor = -135;
latencyTarget.nInterFactor = 500;
latencyTarget.nAdjCap = 20;
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eVideoRender]),
OMX_IndexConfigLatencyTarget, &latencyTarget) != OMX_ErrorNone)
ELOG("failed set video render latency target!");
}
void cOmx::SetPARChangeCallback(bool enable)
{
OMX_CONFIG_REQUESTCALLBACKTYPE reqCallback;
OMX_INIT_STRUCT(reqCallback);
reqCallback.nPortIndex = 131;
reqCallback.bEnable = enable ? OMX_TRUE : OMX_FALSE;
reqCallback.nIndex = OMX_IndexParamBrcmPixelAspectRatio;
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eVideoDecoder]),
OMX_IndexConfigRequestCallback, &reqCallback) != OMX_ErrorNone)
ELOG("failed to set video aspect ratio change call back!");
}
void cOmx::SetBufferStallThreshold(int delayMs)
{
if (delayMs > 0)
{
OMX_CONFIG_BUFFERSTALLTYPE stallConf;
OMX_INIT_STRUCT(stallConf);
stallConf.nPortIndex = 131;
stallConf.nDelay = delayMs * 1000;
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eVideoDecoder]),
OMX_IndexConfigBufferStall, &stallConf) != OMX_ErrorNone)
ELOG("failed to set video decoder stall config!");
}
// set buffer stall call back
OMX_CONFIG_REQUESTCALLBACKTYPE reqCallback;
OMX_INIT_STRUCT(reqCallback);
reqCallback.nPortIndex = 131;
reqCallback.nIndex = OMX_IndexConfigBufferStall;
reqCallback.bEnable = delayMs > 0 ? OMX_TRUE : OMX_FALSE;
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eVideoDecoder]),
OMX_IndexConfigRequestCallback, &reqCallback) != OMX_ErrorNone)
ELOG("failed to set video decoder stall call back!");
}
bool cOmx::IsBufferStall(void)
{
OMX_CONFIG_BUFFERSTALLTYPE stallConf;
OMX_INIT_STRUCT(stallConf);
stallConf.nPortIndex = 131;
if (OMX_GetConfig(ILC_GET_HANDLE(m_comp[eVideoDecoder]),
OMX_IndexConfigBufferStall, &stallConf) != OMX_ErrorNone)
ELOG("failed to get video decoder stall config!");
return stallConf.bStalled == OMX_TRUE;
}
void cOmx::SetVolume(int vol)
{
OMX_AUDIO_CONFIG_VOLUMETYPE volume;
OMX_INIT_STRUCT(volume);
volume.nPortIndex = 100;
volume.bLinear = OMX_TRUE;
volume.sVolume.nValue = vol * 100 / 255;
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eAudioRender]),
OMX_IndexConfigAudioVolume, &volume) != OMX_ErrorNone)
ELOG("failed to set volume!");
}
void cOmx::SetMute(bool mute)
{
OMX_AUDIO_CONFIG_MUTETYPE amute;
OMX_INIT_STRUCT(amute);
amute.nPortIndex = 100;
amute.bMute = mute ? OMX_TRUE : OMX_FALSE;
if (OMX_SetConfig(ILC_GET_HANDLE(m_comp[eAudioRender]),
OMX_IndexConfigAudioMute, &amute) != OMX_ErrorNone)
ELOG("failed to set mute state!");
}
void cOmx::StopVideo(void)
{
Lock();
// disable port buffers and allow video decoder to reconfig
ilclient_disable_port_buffers(m_comp[eVideoDecoder], 130,
m_spareVideoBuffers, NULL, NULL);
m_spareVideoBuffers = 0;
m_handlePortEvents = false;
m_videoFrameFormat.width = 0;
m_videoFrameFormat.height = 0;
m_videoFrameFormat.frameRate = 0;
m_videoFrameFormat.scanMode = cScanMode::eProgressive;
// put video decoder into idle
ilclient_change_component_state(m_comp[eVideoDecoder], OMX_StateIdle);
// put video fx into idle
ilclient_flush_tunnels(&m_tun[eVideoDecoderToVideoFx], 1);
ilclient_disable_tunnel(&m_tun[eVideoDecoderToVideoFx]);
ilclient_change_component_state(m_comp[eVideoFx], OMX_StateIdle);
// put video scheduler into idle
ilclient_flush_tunnels(&m_tun[eVideoFxToVideoScheduler], 1);
ilclient_disable_tunnel(&m_tun[eVideoFxToVideoScheduler]);
ilclient_flush_tunnels(&m_tun[eClockToVideoScheduler], 1);
ilclient_disable_tunnel(&m_tun[eClockToVideoScheduler]);
ilclient_change_component_state(m_comp[eVideoScheduler], OMX_StateIdle);
// put video render into idle
ilclient_flush_tunnels(&m_tun[eVideoSchedulerToVideoRender], 1);
ilclient_disable_tunnel(&m_tun[eVideoSchedulerToVideoRender]);
ilclient_change_component_state(m_comp[eVideoRender], OMX_StateIdle);
Unlock();
}
void cOmx::StopAudio(void)
{
Lock();
// put audio render onto idle
ilclient_flush_tunnels(&m_tun[eClockToAudioRender], 1);
ilclient_disable_tunnel(&m_tun[eClockToAudioRender]);
ilclient_change_component_state(m_comp[eAudioRender], OMX_StateIdle);
ilclient_disable_port_buffers(m_comp[eAudioRender], 100,
m_spareAudioBuffers, NULL, NULL);
m_spareAudioBuffers = 0;
Unlock();
}
void cOmx::SetVideoErrorConcealment(bool startWithValidFrame)
{
OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE ectype;
OMX_INIT_STRUCT(ectype);
ectype.bStartWithValidFrame = startWithValidFrame ? OMX_TRUE : OMX_FALSE;
if (OMX_SetParameter(ILC_GET_HANDLE(m_comp[eVideoDecoder]),
OMX_IndexParamBrcmVideoDecodeErrorConcealment, &ectype) != OMX_ErrorNone)
ELOG("failed to set video decode error concealment failed\n");
}
void cOmx::FlushAudio(void)
{
Lock();
if (OMX_SendCommand(ILC_GET_HANDLE(m_comp[eAudioRender]), OMX_CommandFlush, 100, NULL) != OMX_ErrorNone)
ELOG("failed to flush audio render!");
ilclient_wait_for_event(m_comp[eAudioRender], OMX_EventCmdComplete,
OMX_CommandFlush, 0, 100, 0, ILCLIENT_PORT_FLUSH,
VCOS_EVENT_FLAGS_SUSPEND);
ilclient_flush_tunnels(&m_tun[eClockToAudioRender], 1);
Unlock();
}
void cOmx::FlushVideo(bool flushRender)
{
Lock();
if (OMX_SendCommand(ILC_GET_HANDLE(m_comp[eVideoDecoder]), OMX_CommandFlush, 130, NULL) != OMX_ErrorNone)
ELOG("failed to flush video decoder!");
ilclient_wait_for_event(m_comp[eVideoDecoder], OMX_EventCmdComplete,
OMX_CommandFlush, 0, 130, 0, ILCLIENT_PORT_FLUSH,
VCOS_EVENT_FLAGS_SUSPEND);
ilclient_flush_tunnels(&m_tun[eVideoDecoderToVideoFx], 1);
ilclient_flush_tunnels(&m_tun[eVideoFxToVideoScheduler], 1);
if (flushRender)
ilclient_flush_tunnels(&m_tun[eVideoSchedulerToVideoRender], 1);
ilclient_flush_tunnels(&m_tun[eClockToVideoScheduler], 1);
m_setVideoDiscontinuity = true;
Unlock();
}
int cOmx::SetVideoCodec(cVideoCodec::eCodec codec)