forked from popcornmix/omxplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OMXAudio.cpp
1476 lines (1281 loc) · 52.5 KB
/
OMXAudio.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
/*
* XBMC Media Center
* Copyright (c) 2002 d7o3g4q and RUNTiME
* Portions Copyright (c) by the authors of ffmpeg and xvid
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#if (defined HAVE_CONFIG_H) && (!defined WIN32)
#include "config.h"
#elif defined(_WIN32)
#include "system.h"
#endif
#include "OMXAudio.h"
#include "utils/log.h"
#define CLASSNAME "COMXAudio"
#include "linux/XMemUtils.h"
#ifndef VOLUME_MINIMUM
#define VOLUME_MINIMUM 0
#endif
#include <algorithm>
using namespace std;
// the size of the audio_render output port buffers
#define AUDIO_DECODE_OUTPUT_BUFFER (32*1024)
static const char rounded_up_channels_shift[] = {0,0,1,2,2,3,3,3,3};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//***********************************************************************************************
COMXAudio::COMXAudio() :
m_Initialized (false ),
m_CurrentVolume (0 ),
m_Mute (false ),
m_drc (0 ),
m_BytesPerSec (0 ),
m_InputBytesPerSec(0 ),
m_BufferLen (0 ),
m_ChunkLen (0 ),
m_InputChannels (0 ),
m_OutputChannels (0 ),
m_BitsPerSample (0 ),
m_maxLevel (0.0f ),
m_amplification (1.0f ),
m_attenuation (1.0f ),
m_submitted (0.0f ),
m_omx_clock (NULL ),
m_av_clock (NULL ),
m_settings_changed(false ),
m_setStartTime (false ),
m_eEncoding (OMX_AUDIO_CodingPCM),
m_last_pts (DVD_NOPTS_VALUE),
m_submitted_eos (false ),
m_failed_eos (false )
{
}
COMXAudio::~COMXAudio()
{
Deinitialize();
}
bool COMXAudio::PortSettingsChanged()
{
CSingleLock lock (m_critSection);
OMX_ERRORTYPE omx_err = OMX_ErrorNone;
if (m_settings_changed)
{
m_omx_decoder.DisablePort(m_omx_decoder.GetOutputPort(), true);
m_omx_decoder.EnablePort(m_omx_decoder.GetOutputPort(), true);
return true;
}
if(!m_config.passthrough)
{
if(!m_omx_mixer.Initialize("OMX.broadcom.audio_mixer", OMX_IndexParamAudioInit))
return false;
}
if(m_config.device == "omx:both")
{
if(!m_omx_splitter.Initialize("OMX.broadcom.audio_splitter", OMX_IndexParamAudioInit))
return false;
}
if (m_config.device == "omx:both" || m_config.device == "omx:local")
{
if(!m_omx_render_analog.Initialize("OMX.broadcom.audio_render", OMX_IndexParamAudioInit))
return false;
}
if (m_config.device == "omx:both" || m_config.device == "omx:hdmi")
{
if(!m_omx_render_hdmi.Initialize("OMX.broadcom.audio_render", OMX_IndexParamAudioInit))
return false;
}
if (m_config.device == "omx:alsa")
{
if(!m_omx_render_analog.Initialize("OMX.alsa.audio_render", OMX_IndexParamAudioInit))
return false;
}
UpdateAttenuation();
if( m_omx_mixer.IsInitialized() )
{
/* setup mixer output */
OMX_INIT_STRUCTURE(m_pcm_output);
m_pcm_output.nPortIndex = m_omx_decoder.GetOutputPort();
omx_err = m_omx_decoder.GetParameter(OMX_IndexParamAudioPcm, &m_pcm_output);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - error m_omx_decoder GetParameter omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
memcpy(m_pcm_output.eChannelMapping, m_output_channels, sizeof(m_output_channels));
// round up to power of 2
m_pcm_output.nChannels = m_OutputChannels > 4 ? 8 : m_OutputChannels > 2 ? 4 : m_OutputChannels;
/* limit samplerate (through resampling) if requested */
m_pcm_output.nSamplingRate = std::min(std::max((int)m_pcm_output.nSamplingRate, 8000), 192000);
m_pcm_output.nPortIndex = m_omx_mixer.GetOutputPort();
omx_err = m_omx_mixer.SetParameter(OMX_IndexParamAudioPcm, &m_pcm_output);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - error m_omx_mixer SetParameter omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
CLog::Log(LOGDEBUG, "%s::%s - Output bps %d samplerate %d channels %d buffer size %d bytes per second %d",
CLASSNAME, __func__, (int)m_pcm_output.nBitPerSample, (int)m_pcm_output.nSamplingRate, (int)m_pcm_output.nChannels, m_BufferLen, m_BytesPerSec);
PrintPCM(&m_pcm_output, std::string("output"));
if( m_omx_splitter.IsInitialized() )
{
m_pcm_output.nPortIndex = m_omx_splitter.GetInputPort();
omx_err = m_omx_splitter.SetParameter(OMX_IndexParamAudioPcm, &m_pcm_output);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - error m_omx_splitter SetParameter omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
m_pcm_output.nPortIndex = m_omx_splitter.GetOutputPort();
omx_err = m_omx_splitter.SetParameter(OMX_IndexParamAudioPcm, &m_pcm_output);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - error m_omx_splitter SetParameter omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
m_pcm_output.nPortIndex = m_omx_splitter.GetOutputPort() + 1;
omx_err = m_omx_splitter.SetParameter(OMX_IndexParamAudioPcm, &m_pcm_output);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - error m_omx_splitter SetParameter omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
}
if( m_omx_render_analog.IsInitialized() )
{
m_pcm_output.nPortIndex = m_omx_render_analog.GetInputPort();
omx_err = m_omx_render_analog.SetParameter(OMX_IndexParamAudioPcm, &m_pcm_output);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - error m_omx_render_analog SetParameter omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
}
if( m_omx_render_hdmi.IsInitialized() )
{
m_pcm_output.nPortIndex = m_omx_render_hdmi.GetInputPort();
omx_err = m_omx_render_hdmi.SetParameter(OMX_IndexParamAudioPcm, &m_pcm_output);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - error m_omx_render_hdmi SetParameter omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
}
}
if( m_omx_render_analog.IsInitialized() )
{
m_omx_tunnel_clock_analog.Initialize(m_omx_clock, m_omx_clock->GetInputPort(),
&m_omx_render_analog, m_omx_render_analog.GetInputPort()+1);
omx_err = m_omx_tunnel_clock_analog.Establish();
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - m_omx_tunnel_clock_analog.Establish omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
m_omx_render_analog.ResetEos();
}
if( m_omx_render_hdmi.IsInitialized() )
{
m_omx_tunnel_clock_hdmi.Initialize(m_omx_clock, m_omx_clock->GetInputPort() + (m_omx_render_analog.IsInitialized() ? 2 : 0),
&m_omx_render_hdmi, m_omx_render_hdmi.GetInputPort()+1);
omx_err = m_omx_tunnel_clock_hdmi.Establish();
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - m_omx_tunnel_clock_hdmi.Establish omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
m_omx_render_hdmi.ResetEos();
}
if( m_omx_render_analog.IsInitialized() )
{
// By default audio_render is the clock master, and if output samples don't fit the timestamps, it will speed up/slow down the clock.
// This tends to be better for maintaining audio sync and avoiding audio glitches, but can affect video/display sync
// when in dual audio mode, make analogue the slave
OMX_CONFIG_BOOLEANTYPE configBool;
OMX_INIT_STRUCTURE(configBool);
configBool.bEnabled = m_config.is_live || m_config.device == "omx:both" ? OMX_FALSE:OMX_TRUE;
omx_err = m_omx_render_analog.SetConfig(OMX_IndexConfigBrcmClockReferenceSource, &configBool);
if (omx_err != OMX_ErrorNone)
return false;
OMX_CONFIG_BRCMAUDIODESTINATIONTYPE audioDest;
OMX_INIT_STRUCTURE(audioDest);
strncpy((char *)audioDest.sName, m_config.device == "omx:alsa" ? m_config.subdevice.c_str() : "local", sizeof(audioDest.sName));
omx_err = m_omx_render_analog.SetConfig(OMX_IndexConfigBrcmAudioDestination, &audioDest);
if (omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - m_omx_render_analog.SetConfig omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
}
if( m_omx_render_hdmi.IsInitialized() )
{
// By default audio_render is the clock master, and if output samples don't fit the timestamps, it will speed up/slow down the clock.
// This tends to be better for maintaining audio sync and avoiding audio glitches, but can affect video/display sync
OMX_CONFIG_BOOLEANTYPE configBool;
OMX_INIT_STRUCTURE(configBool);
configBool.bEnabled = m_config.is_live ? OMX_FALSE:OMX_TRUE;
omx_err = m_omx_render_hdmi.SetConfig(OMX_IndexConfigBrcmClockReferenceSource, &configBool);
if (omx_err != OMX_ErrorNone)
return false;
OMX_CONFIG_BRCMAUDIODESTINATIONTYPE audioDest;
OMX_INIT_STRUCTURE(audioDest);
strncpy((char *)audioDest.sName, "hdmi", strlen("hdmi"));
omx_err = m_omx_render_hdmi.SetConfig(OMX_IndexConfigBrcmAudioDestination, &audioDest);
if (omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - m_omx_render_hdmi.SetConfig omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
}
if( m_omx_splitter.IsInitialized() )
{
m_omx_tunnel_splitter_analog.Initialize(&m_omx_splitter, m_omx_splitter.GetOutputPort(), &m_omx_render_analog, m_omx_render_analog.GetInputPort());
omx_err = m_omx_tunnel_splitter_analog.Establish();
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize - Error m_omx_tunnel_splitter_analog.Establish 0x%08x", omx_err);
return false;
}
m_omx_tunnel_splitter_hdmi.Initialize(&m_omx_splitter, m_omx_splitter.GetOutputPort() + 1, &m_omx_render_hdmi, m_omx_render_hdmi.GetInputPort());
omx_err = m_omx_tunnel_splitter_hdmi.Establish();
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize - Error m_omx_tunnel_splitter_hdmi.Establish 0x%08x", omx_err);
return false;
}
}
if( m_omx_mixer.IsInitialized() )
{
m_omx_tunnel_decoder.Initialize(&m_omx_decoder, m_omx_decoder.GetOutputPort(), &m_omx_mixer, m_omx_mixer.GetInputPort());
if( m_omx_splitter.IsInitialized() )
{
m_omx_tunnel_mixer.Initialize(&m_omx_mixer, m_omx_mixer.GetOutputPort(), &m_omx_splitter, m_omx_splitter.GetInputPort());
}
else
{
if( m_omx_render_analog.IsInitialized() )
{
m_omx_tunnel_mixer.Initialize(&m_omx_mixer, m_omx_mixer.GetOutputPort(), &m_omx_render_analog, m_omx_render_analog.GetInputPort());
}
if( m_omx_render_hdmi.IsInitialized() )
{
m_omx_tunnel_mixer.Initialize(&m_omx_mixer, m_omx_mixer.GetOutputPort(), &m_omx_render_hdmi, m_omx_render_hdmi.GetInputPort());
}
}
CLog::Log(LOGDEBUG, "%s::%s - bits:%d mode:%d channels:%d srate:%d nopassthrough", CLASSNAME, __func__,
(int)m_pcm_input.nBitPerSample, m_pcm_input.ePCMMode, (int)m_pcm_input.nChannels, (int)m_pcm_input.nSamplingRate);
}
else
{
if( m_omx_render_analog.IsInitialized() )
{
m_omx_tunnel_decoder.Initialize(&m_omx_decoder, m_omx_decoder.GetOutputPort(), &m_omx_render_analog, m_omx_render_analog.GetInputPort());
}
else if( m_omx_render_hdmi.IsInitialized() )
{
m_omx_tunnel_decoder.Initialize(&m_omx_decoder, m_omx_decoder.GetOutputPort(), &m_omx_render_hdmi, m_omx_render_hdmi.GetInputPort());
}
CLog::Log(LOGDEBUG, "%s::%s - bits:%d mode:%d channels:%d srate:%d passthrough", CLASSNAME, __func__,
0, 0, 0, 0);
}
omx_err = m_omx_tunnel_decoder.Establish();
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - m_omx_tunnel_decoder.Establish omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
if( m_omx_mixer.IsInitialized() )
{
omx_err = m_omx_mixer.SetStateForComponent(OMX_StateExecuting);
if(omx_err != OMX_ErrorNone) {
CLog::Log(LOGERROR, "%s::%s - m_omx_mixer OMX_StateExecuting omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
}
if( m_omx_mixer.IsInitialized() )
{
omx_err = m_omx_tunnel_mixer.Establish();
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - m_omx_tunnel_decoder.Establish omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
}
if( m_omx_splitter.IsInitialized() )
{
omx_err = m_omx_splitter.SetStateForComponent(OMX_StateExecuting);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - m_omx_splitter OMX_StateExecuting 0x%08x", CLASSNAME, __func__, omx_err);
return false;
}
}
if( m_omx_render_analog.IsInitialized() )
{
omx_err = m_omx_render_analog.SetStateForComponent(OMX_StateExecuting);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - m_omx_render_analog OMX_StateExecuting omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
}
if( m_omx_render_hdmi.IsInitialized() )
{
omx_err = m_omx_render_hdmi.SetStateForComponent(OMX_StateExecuting);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - m_omx_render_hdmi OMX_StateExecuting omx_err(0x%08x)", CLASSNAME, __func__, omx_err);
return false;
}
}
m_settings_changed = true;
return true;
}
static unsigned count_bits(uint64_t value)
{
unsigned bits = 0;
for(;value;++bits)
value &= value - 1;
return bits;
}
bool COMXAudio::Initialize(OMXClock *clock, const OMXAudioConfig &config, uint64_t channelMap, unsigned int uiBitsPerSample)
{
CSingleLock lock (m_critSection);
OMX_ERRORTYPE omx_err;
Deinitialize();
if(!m_dllAvUtil.Load())
return false;
m_config = config;
m_InputChannels = count_bits(channelMap);
if(m_InputChannels == 0)
return false;
if(m_config.hints.samplerate == 0)
return false;
m_av_clock = clock;
if(!m_av_clock)
return false;
/* passthrough overwrites hw decode */
if(m_config.passthrough)
{
m_config.hwdecode = false;
}
else if(m_config.hwdecode)
{
/* check again if we are capable to hw decode the format */
m_config.hwdecode = CanHWDecode(m_config.hints.codec);
}
if(m_config.passthrough || m_config.hwdecode)
SetCodingType(m_config.hints.codec);
else
SetCodingType(AV_CODEC_ID_PCM_S16LE);
m_omx_clock = m_av_clock->GetOMXClock();
m_drc = 0;
memset(m_input_channels, 0x0, sizeof(m_input_channels));
memset(m_output_channels, 0x0, sizeof(m_output_channels));
memset(&m_wave_header, 0x0, sizeof(m_wave_header));
m_wave_header.Format.nChannels = 2;
m_wave_header.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
// set the input format, and get the channel layout so we know what we need to open
if (!m_config.passthrough && channelMap)
{
enum PCMChannels inLayout[OMX_AUDIO_MAXCHANNELS];
enum PCMChannels outLayout[OMX_AUDIO_MAXCHANNELS];
// force out layout to stereo if input is not multichannel - it gives the receiver a chance to upmix
if (channelMap == (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT) || channelMap == AV_CH_FRONT_CENTER)
m_config.layout = PCM_LAYOUT_2_0;
BuildChannelMap(inLayout, channelMap);
m_OutputChannels = BuildChannelMapCEA(outLayout, GetChannelLayout(m_config.layout));
CPCMRemap m_remap;
m_remap.Reset();
/*outLayout = */m_remap.SetInputFormat (m_InputChannels, inLayout, uiBitsPerSample / 8, m_config.hints.samplerate, m_config.layout, m_config.boostOnDownmix);
m_remap.SetOutputFormat(m_OutputChannels, outLayout);
m_remap.GetDownmixMatrix(m_downmix_matrix);
m_wave_header.dwChannelMask = channelMap;
BuildChannelMapOMX(m_input_channels, channelMap);
BuildChannelMapOMX(m_output_channels, GetChannelLayout(m_config.layout));
}
m_BitsPerSample = uiBitsPerSample;
m_BytesPerSec = m_config.hints.samplerate * 2 << rounded_up_channels_shift[m_InputChannels];
m_BufferLen = m_BytesPerSec * AUDIO_BUFFER_SECONDS;
m_InputBytesPerSec = m_config.hints.samplerate * m_BitsPerSample * m_InputChannels >> 3;
// should be big enough that common formats (e.g. 6 channel DTS) fit in a single packet.
// we don't mind less common formats being split (e.g. ape/wma output large frames)
// 6 channel 32bpp float to 8 channel 16bpp in, so a full 48K input buffer will fit the output buffer
m_ChunkLen = AUDIO_DECODE_OUTPUT_BUFFER * (m_InputChannels * m_BitsPerSample) >> (rounded_up_channels_shift[m_InputChannels] + 4);
m_wave_header.Samples.wSamplesPerBlock = 0;
m_wave_header.Format.nChannels = m_InputChannels;
m_wave_header.Format.nBlockAlign = m_InputChannels *
(m_BitsPerSample >> 3);
// 0x8000 is custom format interpreted by GPU as WAVE_FORMAT_IEEE_FLOAT_PLANAR
m_wave_header.Format.wFormatTag = m_BitsPerSample == 32 ? 0x8000 : WAVE_FORMAT_PCM;
m_wave_header.Format.nSamplesPerSec = m_config.hints.samplerate;
m_wave_header.Format.nAvgBytesPerSec = m_BytesPerSec;
m_wave_header.Format.wBitsPerSample = m_BitsPerSample;
m_wave_header.Samples.wValidBitsPerSample = m_BitsPerSample;
m_wave_header.Format.cbSize = 0;
m_wave_header.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
if(!m_omx_decoder.Initialize("OMX.broadcom.audio_decode", OMX_IndexParamAudioInit))
return false;
OMX_CONFIG_BOOLEANTYPE boolType;
OMX_INIT_STRUCTURE(boolType);
if(m_config.passthrough)
boolType.bEnabled = OMX_TRUE;
else
boolType.bEnabled = OMX_FALSE;
omx_err = m_omx_decoder.SetParameter(OMX_IndexParamBrcmDecoderPassThrough, &boolType);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize - Error OMX_IndexParamBrcmDecoderPassThrough 0x%08x", omx_err);
printf("OMX_IndexParamBrcmDecoderPassThrough omx_err(0x%08x)\n", omx_err);
return false;
}
// set up the number/size of buffers for decoder input
OMX_PARAM_PORTDEFINITIONTYPE port_param;
OMX_INIT_STRUCTURE(port_param);
port_param.nPortIndex = m_omx_decoder.GetInputPort();
omx_err = m_omx_decoder.GetParameter(OMX_IndexParamPortDefinition, &port_param);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize error get OMX_IndexParamPortDefinition (input) omx_err(0x%08x)\n", omx_err);
return false;
}
port_param.format.audio.eEncoding = m_eEncoding;
port_param.nBufferSize = m_ChunkLen;
port_param.nBufferCountActual = std::max(port_param.nBufferCountMin, 16U);
omx_err = m_omx_decoder.SetParameter(OMX_IndexParamPortDefinition, &port_param);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize error set OMX_IndexParamPortDefinition (intput) omx_err(0x%08x)\n", omx_err);
return false;
}
// set up the number/size of buffers for decoder output
OMX_INIT_STRUCTURE(port_param);
port_param.nPortIndex = m_omx_decoder.GetOutputPort();
omx_err = m_omx_decoder.GetParameter(OMX_IndexParamPortDefinition, &port_param);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize error get OMX_IndexParamPortDefinition (output) omx_err(0x%08x)\n", omx_err);
return false;
}
port_param.nBufferCountActual = std::max((unsigned int)port_param.nBufferCountMin, m_BufferLen / port_param.nBufferSize);
omx_err = m_omx_decoder.SetParameter(OMX_IndexParamPortDefinition, &port_param);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize error set OMX_IndexParamPortDefinition (output) omx_err(0x%08x)\n", omx_err);
return false;
}
{
OMX_AUDIO_PARAM_PORTFORMATTYPE formatType;
OMX_INIT_STRUCTURE(formatType);
formatType.nPortIndex = m_omx_decoder.GetInputPort();
formatType.eEncoding = m_eEncoding;
omx_err = m_omx_decoder.SetParameter(OMX_IndexParamAudioPortFormat, &formatType);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize error OMX_IndexParamAudioPortFormat omx_err(0x%08x)\n", omx_err);
return false;
}
}
omx_err = m_omx_decoder.AllocInputBuffers();
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize - Error alloc buffers 0x%08x", omx_err);
return false;
}
omx_err = m_omx_decoder.SetStateForComponent(OMX_StateExecuting);
if(omx_err != OMX_ErrorNone) {
CLog::Log(LOGERROR, "COMXAudio::Initialize - Error setting OMX_StateExecuting 0x%08x", omx_err);
return false;
}
if(m_eEncoding == OMX_AUDIO_CodingPCM)
{
OMX_BUFFERHEADERTYPE *omx_buffer = m_omx_decoder.GetInputBuffer();
if(omx_buffer == NULL)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize - buffer error 0x%08x", omx_err);
return false;
}
omx_buffer->nOffset = 0;
omx_buffer->nFilledLen = std::min(sizeof(m_wave_header), omx_buffer->nAllocLen);
memset((unsigned char *)omx_buffer->pBuffer, 0x0, omx_buffer->nAllocLen);
memcpy((unsigned char *)omx_buffer->pBuffer, &m_wave_header, omx_buffer->nFilledLen);
omx_buffer->nFlags = OMX_BUFFERFLAG_CODECCONFIG | OMX_BUFFERFLAG_ENDOFFRAME;
omx_err = m_omx_decoder.EmptyThisBuffer(omx_buffer);
if (omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - OMX_EmptyThisBuffer() failed with result(0x%x)\n", CLASSNAME, __func__, omx_err);
m_omx_decoder.DecoderEmptyBufferDone(m_omx_decoder.GetComponent(), omx_buffer);
return false;
}
}
else if(m_config.hwdecode)
{
// send decoder config
if(m_config.hints.extrasize > 0 && m_config.hints.extradata != NULL)
{
OMX_BUFFERHEADERTYPE *omx_buffer = m_omx_decoder.GetInputBuffer();
if(omx_buffer == NULL)
{
CLog::Log(LOGERROR, "%s::%s - buffer error 0x%08x", CLASSNAME, __func__, omx_err);
return false;
}
omx_buffer->nOffset = 0;
omx_buffer->nFilledLen = std::min((OMX_U32)m_config.hints.extrasize, omx_buffer->nAllocLen);
memset((unsigned char *)omx_buffer->pBuffer, 0x0, omx_buffer->nAllocLen);
memcpy((unsigned char *)omx_buffer->pBuffer, m_config.hints.extradata, omx_buffer->nFilledLen);
omx_buffer->nFlags = OMX_BUFFERFLAG_CODECCONFIG | OMX_BUFFERFLAG_ENDOFFRAME;
omx_err = m_omx_decoder.EmptyThisBuffer(omx_buffer);
if (omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - OMX_EmptyThisBuffer() failed with result(0x%x)\n", CLASSNAME, __func__, omx_err);
m_omx_decoder.DecoderEmptyBufferDone(m_omx_decoder.GetComponent(), omx_buffer);
return false;
}
}
}
/* return on decoder error so m_Initialized stays false */
if(m_omx_decoder.BadState())
return false;
OMX_INIT_STRUCTURE(m_pcm_input);
m_pcm_input.nPortIndex = m_omx_decoder.GetInputPort();
memcpy(m_pcm_input.eChannelMapping, m_input_channels, sizeof(m_input_channels));
m_pcm_input.eNumData = OMX_NumericalDataSigned;
m_pcm_input.eEndian = OMX_EndianLittle;
m_pcm_input.bInterleaved = OMX_TRUE;
m_pcm_input.nBitPerSample = m_BitsPerSample;
m_pcm_input.ePCMMode = OMX_AUDIO_PCMModeLinear;
m_pcm_input.nChannels = m_InputChannels;
m_pcm_input.nSamplingRate = m_config.hints.samplerate;
m_Initialized = true;
m_settings_changed = false;
m_setStartTime = true;
m_submitted_eos = false;
m_failed_eos = false;
m_last_pts = DVD_NOPTS_VALUE;
m_submitted = 0.0f;
m_maxLevel = 0.0f;
CLog::Log(LOGDEBUG, "COMXAudio::Initialize Input bps %d samplerate %d channels %d buffer size %d bytes per second %d",
(int)m_pcm_input.nBitPerSample, (int)m_pcm_input.nSamplingRate, (int)m_pcm_input.nChannels, m_BufferLen, m_InputBytesPerSec);
PrintPCM(&m_pcm_input, std::string("input"));
CLog::Log(LOGDEBUG, "COMXAudio::Initialize device %s passthrough %d hwdecode %d",
m_config.device.c_str(), m_config.passthrough, m_config.hwdecode);
return true;
}
//***********************************************************************************************
bool COMXAudio::Deinitialize()
{
CSingleLock lock (m_critSection);
if ( m_omx_tunnel_clock_analog.IsInitialized() )
m_omx_tunnel_clock_analog.Deestablish();
if ( m_omx_tunnel_clock_hdmi.IsInitialized() )
m_omx_tunnel_clock_hdmi.Deestablish();
// ignore expected errors on teardown
if ( m_omx_mixer.IsInitialized() )
m_omx_mixer.IgnoreNextError(OMX_ErrorPortUnpopulated);
else
{
if ( m_omx_render_hdmi.IsInitialized() )
m_omx_render_hdmi.IgnoreNextError(OMX_ErrorPortUnpopulated);
if ( m_omx_render_analog.IsInitialized() )
m_omx_render_analog.IgnoreNextError(OMX_ErrorPortUnpopulated);
}
m_omx_tunnel_decoder.Deestablish();
if ( m_omx_tunnel_mixer.IsInitialized() )
m_omx_tunnel_mixer.Deestablish();
if ( m_omx_tunnel_splitter_hdmi.IsInitialized() )
m_omx_tunnel_splitter_hdmi.Deestablish();
if ( m_omx_tunnel_splitter_analog.IsInitialized() )
m_omx_tunnel_splitter_analog.Deestablish();
m_omx_decoder.FlushInput();
m_omx_decoder.Deinitialize();
if ( m_omx_mixer.IsInitialized() )
m_omx_mixer.Deinitialize();
if ( m_omx_splitter.IsInitialized() )
m_omx_splitter.Deinitialize();
if ( m_omx_render_hdmi.IsInitialized() )
m_omx_render_hdmi.Deinitialize();
if ( m_omx_render_analog.IsInitialized() )
m_omx_render_analog.Deinitialize();
m_BytesPerSec = 0;
m_BufferLen = 0;
m_omx_clock = NULL;
m_av_clock = NULL;
m_Initialized = false;
m_dllAvUtil.Unload();
while(!m_ampqueue.empty())
m_ampqueue.pop_front();
m_last_pts = DVD_NOPTS_VALUE;
m_submitted = 0.0f;
m_maxLevel = 0.0f;
return true;
}
void COMXAudio::Flush()
{
CSingleLock lock (m_critSection);
if(!m_Initialized)
return;
m_omx_decoder.FlushAll();
if ( m_omx_mixer.IsInitialized() )
m_omx_mixer.FlushAll();
if ( m_omx_splitter.IsInitialized() )
m_omx_splitter.FlushAll();
if ( m_omx_render_analog.IsInitialized() )
m_omx_render_analog.FlushAll();
if ( m_omx_render_hdmi.IsInitialized() )
m_omx_render_hdmi.FlushAll();
while(!m_ampqueue.empty())
m_ampqueue.pop_front();
if( m_omx_render_analog.IsInitialized() )
m_omx_render_analog.ResetEos();
if( m_omx_render_hdmi.IsInitialized() )
m_omx_render_hdmi.ResetEos();
m_last_pts = DVD_NOPTS_VALUE;
m_submitted = 0.0f;
m_maxLevel = 0.0f;
m_setStartTime = true;
}
//***********************************************************************************************
void COMXAudio::SetDynamicRangeCompression(long drc)
{
CSingleLock lock (m_critSection);
m_amplification = powf(10.0f, (float)drc / 2000.0f);
if (m_settings_changed)
UpdateAttenuation();
}
//***********************************************************************************************
void COMXAudio::SetMute(bool bMute)
{
CSingleLock lock (m_critSection);
m_Mute = bMute;
if (m_settings_changed)
UpdateAttenuation();
}
//***********************************************************************************************
void COMXAudio::SetVolume(float fVolume)
{
CSingleLock lock (m_critSection);
m_CurrentVolume = fVolume;
if (m_settings_changed)
UpdateAttenuation();
}
float COMXAudio::GetVolume()
{
return m_Mute ? VOLUME_MINIMUM : m_CurrentVolume;
}
//***********************************************************************************************
bool COMXAudio::ApplyVolume(void)
{
float m_ac3Gain = 12.0f;
CSingleLock lock (m_critSection);
if(!m_Initialized || m_config.passthrough)
return false;
float fVolume = m_Mute ? VOLUME_MINIMUM : m_CurrentVolume;
// the analogue volume is too quiet for some. Allow use of an advancedsetting to boost this (at risk of distortion) (deprecated)
double gain = pow(10, (m_ac3Gain - 12.0f) / 20.0);
const float* coeff = m_downmix_matrix;
OMX_CONFIG_BRCMAUDIODOWNMIXCOEFFICIENTS8x8 mix;
OMX_INIT_STRUCTURE(mix);
OMX_ERRORTYPE omx_err;
assert(sizeof(mix.coeff)/sizeof(mix.coeff[0]) == 64);
if (m_amplification != 1.0)
{
// reduce scaling so overflow can be seen
for(size_t i = 0; i < 8*8; ++i)
mix.coeff[i] = static_cast<unsigned int>(0x10000 * (coeff[i] * gain * 0.01f));
mix.nPortIndex = m_omx_decoder.GetInputPort();
omx_err = m_omx_decoder.SetConfig(OMX_IndexConfigBrcmAudioDownmixCoefficients8x8, &mix);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - error setting decoder OMX_IndexConfigBrcmAudioDownmixCoefficients, error 0x%08x\n",
CLASSNAME, __func__, omx_err);
return false;
}
}
for(size_t i = 0; i < 8*8; ++i)
mix.coeff[i] = static_cast<unsigned int>(0x10000 * (coeff[i] * gain * fVolume * m_amplification * m_attenuation));
mix.nPortIndex = m_omx_mixer.GetInputPort();
omx_err = m_omx_mixer.SetConfig(OMX_IndexConfigBrcmAudioDownmixCoefficients8x8, &mix);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - error setting mixer OMX_IndexConfigBrcmAudioDownmixCoefficients, error 0x%08x\n",
CLASSNAME, __func__, omx_err);
return false;
}
CLog::Log(LOGINFO, "%s::%s - Volume=%.2f (* %.2f * %.2f)\n", CLASSNAME, __func__, fVolume, m_amplification, m_attenuation);
return true;
}
//***********************************************************************************************
unsigned int COMXAudio::AddPackets(const void* data, unsigned int len)
{
return AddPackets(data, len, 0, 0, 0);
}
//***********************************************************************************************
unsigned int COMXAudio::AddPackets(const void* data, unsigned int len, double dts, double pts, unsigned int frame_size)
{
CSingleLock lock (m_critSection);
if(!m_Initialized)
{
CLog::Log(LOGERROR,"COMXAudio::AddPackets - sanity failed. no valid play handle!");
return len;
}
unsigned pitch = (m_config.passthrough || m_config.hwdecode) ? 1:(m_BitsPerSample >> 3) * m_InputChannels;
unsigned int demuxer_samples = len / pitch;
unsigned int demuxer_samples_sent = 0;
uint8_t *demuxer_content = (uint8_t *)data;
OMX_ERRORTYPE omx_err;
OMX_BUFFERHEADERTYPE *omx_buffer = NULL;
while(demuxer_samples_sent < demuxer_samples)
{
// 200ms timeout
omx_buffer = m_omx_decoder.GetInputBuffer(200);
if(omx_buffer == NULL)
{
CLog::Log(LOGERROR, "COMXAudio::Decode timeout\n");
printf("COMXAudio::Decode timeout\n");
return len;
}
omx_buffer->nOffset = 0;
omx_buffer->nFlags = 0;
// we want audio_decode output buffer size to be no more than AUDIO_DECODE_OUTPUT_BUFFER.
// it will be 16-bit and rounded up to next power of 2 in channels
unsigned int max_buffer = AUDIO_DECODE_OUTPUT_BUFFER * (m_InputChannels * m_BitsPerSample) >> (rounded_up_channels_shift[m_InputChannels] + 4);
unsigned int remaining = demuxer_samples-demuxer_samples_sent;
unsigned int samples_space = std::min(max_buffer, omx_buffer->nAllocLen)/pitch;
unsigned int samples = std::min(remaining, samples_space);
omx_buffer->nFilledLen = samples * pitch;
unsigned int frames = frame_size ? len/frame_size:0;
if ((samples < demuxer_samples || frames > 1) && m_BitsPerSample==32 && !(m_config.passthrough || m_config.hwdecode))
{
const unsigned int sample_pitch = m_BitsPerSample >> 3;
const unsigned int frame_samples = frame_size / pitch;
const unsigned int plane_size = frame_samples * sample_pitch;
const unsigned int out_plane_size = samples * sample_pitch;
//CLog::Log(LOGDEBUG, "%s::%s samples:%d/%d ps:%d ops:%d fs:%d pitch:%d filled:%d frames=%d", CLASSNAME, __func__, samples, demuxer_samples, plane_size, out_plane_size, frame_size, pitch, omx_buffer
for (unsigned int sample = 0; sample < samples; )
{
unsigned int frame = (demuxer_samples_sent + sample) / frame_samples;
unsigned int sample_in_frame = (demuxer_samples_sent + sample) - frame * frame_samples;
int out_remaining = std::min(std::min(frame_samples - sample_in_frame, samples), samples-sample);
uint8_t *src = demuxer_content + frame*frame_size + sample_in_frame * sample_pitch;
uint8_t *dst = (uint8_t *)omx_buffer->pBuffer + sample * sample_pitch;
for (unsigned int channel = 0; channel < m_InputChannels; channel++)
{
//CLog::Log(LOGDEBUG, "%s::%s copy(%d,%d,%d) (s:%d f:%d sin:%d c:%d)", CLASSNAME, __func__, dst-(uint8_t *)omx_buffer->pBuffer, src-demuxer_content, out_remaining, sample, frame, sample_in_frame
memcpy(dst, src, out_remaining * sample_pitch);
src += plane_size;
dst += out_plane_size;
}
sample += out_remaining;
}
}
else
{
uint8_t *dst = omx_buffer->pBuffer;
uint8_t *src = demuxer_content + demuxer_samples_sent * pitch;
memcpy(dst, src, omx_buffer->nFilledLen);
}
uint64_t val = (uint64_t)(pts == DVD_NOPTS_VALUE) ? 0 : pts;
if(m_setStartTime)
{
omx_buffer->nFlags = OMX_BUFFERFLAG_STARTTIME;
m_last_pts = pts;
CLog::Log(LOGDEBUG, "COMXAudio::Decode ADec : setStartTime %f\n", (float)val / DVD_TIME_BASE);
m_setStartTime = false;
}
else
{
if(pts == DVD_NOPTS_VALUE)
{
omx_buffer->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
m_last_pts = pts;
}
else if (m_last_pts != pts)
{
if(pts > m_last_pts)
m_last_pts = pts;
else
omx_buffer->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
}
else if (m_last_pts == pts)
{
omx_buffer->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
}
}
omx_buffer->nTimeStamp = ToOMXTime(val);
demuxer_samples_sent += samples;
if(demuxer_samples_sent == demuxer_samples)
omx_buffer->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
omx_err = m_omx_decoder.EmptyThisBuffer(omx_buffer);
if (omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - OMX_EmptyThisBuffer() failed with result(0x%x)\n", CLASSNAME, __func__, omx_err);
printf("%s::%s - OMX_EmptyThisBuffer() failed with result(0x%x)\n", CLASSNAME, __func__, omx_err);
m_omx_decoder.DecoderEmptyBufferDone(m_omx_decoder.GetComponent(), omx_buffer);
return 0;
}
//CLog::Log(LOGINFO, "AudiD: dts:%.0f pts:%.0f size:%d\n", dts, pts, len);
omx_err = m_omx_decoder.WaitForEvent(OMX_EventPortSettingsChanged, 0);
if (omx_err == OMX_ErrorNone)
{
if(!PortSettingsChanged())
{
CLog::Log(LOGERROR, "%s::%s - error PortSettingsChanged omx_err(0x%08x)\n", CLASSNAME, __func__, omx_err);
}
}
}
m_submitted += (float)demuxer_samples / m_config.hints.samplerate;
UpdateAttenuation();
return len;
}
void COMXAudio::UpdateAttenuation()
{
if (m_amplification == 1.0)
{
ApplyVolume();
return;
}
double level_pts = 0.0;
float level = GetMaxLevel(level_pts);
if (level_pts != 0.0)
{
amplitudes_t v;
v.level = level;