-
Notifications
You must be signed in to change notification settings - Fork 502
/
OMXAudio.cpp
1595 lines (1345 loc) · 46.8 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 STANDALONE
#include "guilib/AudioContext.h"
#include "settings/AdvancedSettings.h"
#include "settings/GUISettings.h"
#include "settings/Settings.h"
#include "guilib/LocalizeStrings.h"
#endif
#ifndef VOLUME_MINIMUM
#define VOLUME_MINIMUM -6000 // -60dB
#endif
#include <algorithm>
using namespace std;
#define OMX_MAX_CHANNELS 9
static enum PCMChannels OMXChannelMap[OMX_MAX_CHANNELS] =
{
PCM_FRONT_LEFT , PCM_FRONT_RIGHT,
PCM_FRONT_CENTER, PCM_LOW_FREQUENCY,
PCM_BACK_LEFT , PCM_BACK_RIGHT,
PCM_SIDE_LEFT , PCM_SIDE_RIGHT,
PCM_BACK_CENTER
};
static enum OMX_AUDIO_CHANNELTYPE OMXChannels[OMX_MAX_CHANNELS] =
{
OMX_AUDIO_ChannelLF, OMX_AUDIO_ChannelRF,
OMX_AUDIO_ChannelCF, OMX_AUDIO_ChannelLFE,
OMX_AUDIO_ChannelLR, OMX_AUDIO_ChannelRR,
OMX_AUDIO_ChannelLS, OMX_AUDIO_ChannelRS,
OMX_AUDIO_ChannelCS
};
static unsigned int WAVEChannels[OMX_MAX_CHANNELS] =
{
SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
SPEAKER_TOP_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
SPEAKER_BACK_CENTER
};
static const uint16_t AC3Bitrates[] = {32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640};
static const uint16_t AC3FSCod [] = {48000, 44100, 32000, 0};
static const uint16_t DTSFSCod [] = {0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0, 12000, 24000, 48000, 0, 0};
// Dolby 5.1 downmixing coefficients
const float downmixing_coefficients_6[16] = {
// L R
/* L */ 1, 0,
/* R */ 0, 1,
/* C */ 0.7071, 0.7071,
/* LFE */ 0.7071, 0.7071,
/* Ls */ 0.7071, 0,
/* Rs */ 0, 0.7071,
/* Lr */ 0, 0,
/* Rr */ 0, 0
};
// 7.1 downmixing coefficients
const float downmixing_coefficients_8[16] = {
// L R
/* L */ 1, 0,
/* R */ 0, 1,
/* C */ 0.7071, 0.7071,
/* LFE */ 0.7071, 0.7071,
/* Ls */ 0.7071, 0,
/* Rs */ 0, 0.7071,
/* Lr */ 0.7071, 0,
/* Rr */ 0, 0.7071
};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//***********************************************************************************************
COMXAudio::COMXAudio() :
m_pCallback (NULL ),
m_Initialized (false ),
m_Pause (false ),
m_CanPause (false ),
m_CurrentVolume (0 ),
m_Passthrough (false ),
m_HWDecode (false ),
m_normalize_downmix(true ),
m_BytesPerSec (0 ),
m_BufferLen (0 ),
m_ChunkLen (0 ),
m_InputChannels (0 ),
m_OutputChannels (0 ),
m_downmix_channels(0 ),
m_BitsPerSample (0 ),
m_omx_clock (NULL ),
m_av_clock (NULL ),
m_external_clock (false ),
m_setStartTime (false ),
m_SampleSize (0 ),
m_first_frame (true ),
m_LostSync (true ),
m_SampleRate (0 ),
m_eEncoding (OMX_AUDIO_CodingPCM),
m_extradata (NULL ),
m_extrasize (0 ),
m_fifo_size (0.0 ),
m_visBufferLength (0 ),
m_last_pts (DVD_NOPTS_VALUE)
{
}
COMXAudio::~COMXAudio()
{
if(m_Initialized)
Deinitialize();
}
bool COMXAudio::Initialize(IAudioCallback* pCallback, const CStdString& device, enum PCMChannels *channelMap,
COMXStreamInfo &hints, OMXClock *clock, EEncoded bPassthrough, bool bUseHWDecode,
bool boostOnDownmix, long initialVolume, float fifo_size)
{
m_HWDecode = false;
m_Passthrough = false;
if(bPassthrough != IAudioRenderer::ENCODED_NONE)
{
m_Passthrough = true;
SetCodingType(hints.codec);
}
else if(bUseHWDecode)
{
m_HWDecode = CanHWDecode(hints.codec);
}
else
{
SetCodingType(CODEC_ID_PCM_S16LE);
}
SetClock(clock);
if(hints.extrasize > 0 && hints.extradata != NULL)
{
m_extrasize = hints.extrasize;
m_extradata = (uint8_t *)malloc(m_extrasize);
memcpy(m_extradata, hints.extradata, hints.extrasize);
}
return Initialize(pCallback, device, hints.channels, channelMap, hints.channels, hints.samplerate, hints.bitspersample,
false, boostOnDownmix, false, bPassthrough, initialVolume, fifo_size);
}
bool COMXAudio::Initialize(IAudioCallback* pCallback, const CStdString& device, int iChannels, enum PCMChannels *channelMap, unsigned int downmixChannels, unsigned int uiSamplesPerSec, unsigned int uiBitsPerSample, bool bResample, bool boostOnDownmix, bool bIsMusic, EEncoded bPassthrough, long initialVolume, float fifo_size)
{
std::string deviceuse;
if(device == "hdmi") {
deviceuse = "hdmi";
} else {
deviceuse = "local";
}
if(!m_dllAvUtil.Load())
return false;
m_Passthrough = false;
m_fifo_size = fifo_size;
if(bPassthrough != IAudioRenderer::ENCODED_NONE)
m_Passthrough =true;
m_drc = 0;
memset(&m_wave_header, 0x0, sizeof(m_wave_header));
#ifndef STANDALONE
bool bAudioOnAllSpeakers(false);
g_audioContext.SetupSpeakerConfig(iChannels, bAudioOnAllSpeakers, bIsMusic);
if(bPassthrough)
{
g_audioContext.SetActiveDevice(CAudioContext::DIRECTSOUND_DEVICE_DIGITAL);
} else {
g_audioContext.SetActiveDevice(CAudioContext::DIRECTSOUND_DEVICE);
}
m_CurrentVolume = g_settings.m_nVolumeLevel;
#else
m_CurrentVolume = initialVolume;
#endif
m_downmix_channels = downmixChannels;
m_normalize_downmix = !boostOnDownmix;
m_InputChannels = iChannels;
m_remap.Reset();
OMX_INIT_STRUCTURE(m_pcm_output);
m_OutputChannels = 2;
m_pcm_output.nChannels = m_OutputChannels;
m_pcm_output.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
m_pcm_output.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
m_pcm_output.eChannelMapping[2] = OMX_AUDIO_ChannelMax;
OMX_INIT_STRUCTURE(m_pcm_input);
m_pcm_input.nChannels = m_OutputChannels;
m_pcm_input.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
m_pcm_input.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
m_pcm_input.eChannelMapping[2] = OMX_AUDIO_ChannelMax;
m_wave_header.Format.nChannels = m_OutputChannels;
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
enum PCMChannels *outLayout = NULL;
if (!m_Passthrough && channelMap)
outLayout = m_remap.SetInputFormat (iChannels, channelMap, uiBitsPerSample / 8, uiSamplesPerSec);;
if (!m_Passthrough && channelMap && outLayout)
{
/* setup output channel map */
m_OutputChannels = 0;
int ch = 0, map;
int chan = 0;
while(outLayout[ch] != PCM_INVALID && chan < OMX_AUDIO_MAXCHANNELS)
{
for(map = 0; map < OMX_MAX_CHANNELS; ++map)
{
if (outLayout[ch] == OMXChannelMap[map])
{
m_pcm_output.eChannelMapping[chan] = OMXChannels[map];
chan++;
break;
}
}
++ch;
}
m_OutputChannels = chan;
/* setup input channel map */
for (chan=0; chan < OMX_AUDIO_MAXCHANNELS; chan++)
m_pcm_input.eChannelMapping[chan] = OMX_AUDIO_ChannelNone;
ch = 0;
map = 0;
chan = 0;
while(channelMap[ch] != PCM_INVALID && chan < iChannels)
{
for(map = 0; map < OMX_MAX_CHANNELS; ++map)
{
if (channelMap[ch] == OMXChannelMap[map])
{
m_pcm_input.eChannelMapping[chan] = OMXChannels[map];
m_wave_header.dwChannelMask |= WAVEChannels[map];
chan++;
break;
}
}
++ch;
}
}
// set the m_pcm_output parameters
m_pcm_output.eNumData = OMX_NumericalDataSigned;
m_pcm_output.eEndian = OMX_EndianLittle;
m_pcm_output.bInterleaved = OMX_TRUE;
m_pcm_output.nBitPerSample = 16;
m_pcm_output.ePCMMode = OMX_AUDIO_PCMModeLinear;
m_pcm_output.nChannels = m_OutputChannels;
m_pcm_output.nSamplingRate = uiSamplesPerSec;
m_SampleRate = uiSamplesPerSec;
m_BitsPerSample = uiBitsPerSample;
m_BufferLen = m_BytesPerSec = uiSamplesPerSec * (uiBitsPerSample >> 3) * m_InputChannels;
m_BufferLen *= m_fifo_size;
m_ChunkLen = 2048 * (uiBitsPerSample >> 3) * m_InputChannels;
m_wave_header.Samples.wValidBitsPerSample = uiBitsPerSample;
m_wave_header.Samples.wSamplesPerBlock = 0;
m_wave_header.Format.nChannels = m_InputChannels;
m_wave_header.Format.nBlockAlign = m_InputChannels * (uiBitsPerSample >> 3);
// Custom format interpreted by GPU as WAVE_FORMAT_IEEE_FLOAT_PLANAR
m_wave_header.Format.wFormatTag = 0x8000;
m_wave_header.Format.nSamplesPerSec = uiSamplesPerSec;
m_wave_header.Format.nAvgBytesPerSec = m_BytesPerSec;
m_wave_header.Format.wBitsPerSample = uiBitsPerSample;
m_wave_header.Samples.wValidBitsPerSample = uiBitsPerSample;
m_wave_header.Format.cbSize = 0;
m_wave_header.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
//m_SampleSize = (m_pcm_output.nChannels * m_pcm_output.nBitPerSample * m_pcm_output.nSamplingRate)>>3;
m_SampleSize = (m_pcm_input.nChannels * m_pcm_output.nBitPerSample * m_pcm_output.nSamplingRate)>>3;
m_pcm_input.eNumData = OMX_NumericalDataSigned;
m_pcm_input.eEndian = OMX_EndianLittle;
m_pcm_input.bInterleaved = OMX_TRUE;
m_pcm_input.nBitPerSample = uiBitsPerSample;
m_pcm_input.ePCMMode = OMX_AUDIO_PCMModeLinear;
m_pcm_input.nChannels = m_InputChannels;
m_pcm_input.nSamplingRate = uiSamplesPerSec;
PrintPCM(&m_pcm_input);
PrintPCM(&m_pcm_output);
OMX_ERRORTYPE omx_err = OMX_ErrorNone;
std::string componentName = "";
componentName = "OMX.broadcom.audio_render";
if(!m_omx_render.Initialize(componentName, OMX_IndexParamAudioInit))
return false;
OMX_CONFIG_BRCMAUDIODESTINATIONTYPE audioDest;
OMX_INIT_STRUCTURE(audioDest);
strncpy((char *)audioDest.sName, device.c_str(), strlen(device.c_str()));
omx_err = m_omx_render.SetConfig(OMX_IndexConfigBrcmAudioDestination, &audioDest);
if (omx_err != OMX_ErrorNone)
return false;
componentName = "OMX.broadcom.audio_decode";
if(!m_omx_decoder.Initialize(componentName, OMX_IndexParamAudioInit))
return false;
if(!m_Passthrough)
{
componentName = "OMX.broadcom.audio_mixer";
if(!m_omx_mixer.Initialize(componentName, OMX_IndexParamAudioInit))
return false;
}
if(m_Passthrough)
{
OMX_CONFIG_BOOLEANTYPE boolType;
OMX_INIT_STRUCTURE(boolType);
boolType.bEnabled = OMX_TRUE;
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
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 omx_err(0x%08x)\n", omx_err);
return false;
}
port_param.format.audio.eEncoding = m_eEncoding;
port_param.nBufferSize = m_ChunkLen;
port_param.nBufferCountActual = m_BufferLen / m_ChunkLen;
omx_err = m_omx_decoder.SetParameter(OMX_IndexParamPortDefinition, &port_param);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize error set OMX_IndexParamPortDefinition omx_err(0x%08x)\n", omx_err);
return false;
}
if(m_HWDecode)
{
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;
}
}
if(m_av_clock == NULL)
{
/* no external clock set. generate one */
m_external_clock = false;
m_av_clock = new OMXClock();
if(!m_av_clock->OMXInitialize(false, true))
{
delete m_av_clock;
m_av_clock = NULL;
CLog::Log(LOGERROR, "COMXAudio::Initialize error creating av clock\n");
return false;
}
}
m_omx_clock = m_av_clock->GetOMXClock();
m_omx_tunnel_clock.Initialize(m_omx_clock, m_omx_clock->GetInputPort(), &m_omx_render, m_omx_render.GetInputPort()+1);
omx_err = m_omx_tunnel_clock.Establish(false);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize m_omx_tunnel_clock.Establish\n");
return false;
}
if(!m_external_clock)
{
omx_err = m_omx_clock->SetStateForComponent(OMX_StateExecuting);
if (omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize m_omx_clock.SetStateForComponent\n");
return false;
}
}
/*
m_pcm_output.nPortIndex = m_omx_render.GetInputPort();
omx_err = m_omx_render.SetParameter(OMX_IndexParamAudioPcm, &m_pcm_output);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize OMX_IndexParamAudioPcm 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;
}
if(!m_Passthrough)
{
m_omx_tunnel_decoder.Initialize(&m_omx_decoder, m_omx_decoder.GetOutputPort(), &m_omx_mixer, m_omx_mixer.GetInputPort());
omx_err = m_omx_tunnel_decoder.Establish(false);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize - Error m_omx_tunnel_decoder.Establish 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;
}
m_omx_tunnel_mixer.Initialize(&m_omx_mixer, m_omx_mixer.GetOutputPort(), &m_omx_render, m_omx_render.GetInputPort());
omx_err = m_omx_tunnel_mixer.Establish(false);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize - Error m_omx_tunnel_decoder.Establish 0x%08x", omx_err);
return false;
}
omx_err = m_omx_mixer.SetStateForComponent(OMX_StateExecuting);
if(omx_err != OMX_ErrorNone) {
CLog::Log(LOGERROR, "COMXAudio::Initialize - Error setting OMX_StateExecuting 0x%08x", omx_err);
return false;
}
}
else
{
m_omx_tunnel_decoder.Initialize(&m_omx_decoder, m_omx_decoder.GetOutputPort(), &m_omx_render, m_omx_render.GetInputPort());
omx_err = m_omx_tunnel_decoder.Establish(false);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize - Error m_omx_tunnel_decoder.Establish 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;
}
}
omx_err = m_omx_render.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 = sizeof(m_wave_header);
if(omx_buffer->nFilledLen > omx_buffer->nAllocLen)
{
CLog::Log(LOGERROR, "COMXAudio::Initialize - omx_buffer->nFilledLen > omx_buffer->nAllocLen");
return false;
}
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);
return false;
}
}
else if(m_HWDecode)
{
// send decoder config
if(m_extrasize > 0 && m_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 = m_extrasize;
if(omx_buffer->nFilledLen > omx_buffer->nAllocLen)
{
CLog::Log(LOGERROR, "%s::%s - omx_buffer->nFilledLen > omx_buffer->nAllocLen", CLASSNAME, __func__);
return false;
}
memset((unsigned char *)omx_buffer->pBuffer, 0x0, omx_buffer->nAllocLen);
memcpy((unsigned char *)omx_buffer->pBuffer, m_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);
return false;
}
}
}
m_Initialized = true;
m_setStartTime = true;
m_first_frame = true;
m_last_pts = DVD_NOPTS_VALUE;
SetCurrentVolume(m_CurrentVolume);
CLog::Log(LOGDEBUG, "COMXAudio::Initialize Ouput bps %d samplerate %d channels %d device %s buffer size %d bytes per second %d passthrough %d hwdecode %d",
(int)m_pcm_output.nBitPerSample, (int)m_pcm_output.nSamplingRate, (int)m_pcm_output.nChannels, deviceuse.c_str(), m_BufferLen, m_BytesPerSec, m_Passthrough, m_HWDecode);
CLog::Log(LOGDEBUG, "COMXAudio::Initialize Input bps %d samplerate %d channels %d device %s buffer size %d bytes per second %d passthrough %d hwdecode %d",
(int)m_pcm_input.nBitPerSample, (int)m_pcm_input.nSamplingRate, (int)m_pcm_input.nChannels, deviceuse.c_str(), m_BufferLen, m_BytesPerSec, m_Passthrough, m_HWDecode);
return true;
}
//***********************************************************************************************
bool COMXAudio::Deinitialize()
{
if(!m_Initialized)
return true;
if(!m_external_clock && m_av_clock != NULL)
m_av_clock->OMXStop();
m_omx_tunnel_decoder.Flush();
if(!m_Passthrough)
m_omx_tunnel_mixer.Flush();
m_omx_tunnel_clock.Flush();
m_omx_tunnel_clock.Deestablish();
if(!m_Passthrough)
m_omx_tunnel_mixer.Deestablish();
m_omx_tunnel_decoder.Deestablish();
m_omx_decoder.FlushInput();
m_omx_render.Deinitialize();
if(!m_Passthrough)
m_omx_mixer.Deinitialize();
m_omx_decoder.Deinitialize();
m_Initialized = false;
m_BytesPerSec = 0;
m_BufferLen = 0;
if(!m_external_clock && m_av_clock != NULL)
{
delete m_av_clock;
m_av_clock = NULL;
m_external_clock = false;
}
m_omx_clock = NULL;
m_av_clock = NULL;
m_Initialized = false;
m_LostSync = true;
m_HWDecode = false;
if(m_extradata)
free(m_extradata);
m_extradata = NULL;
m_extrasize = 0;
m_dllAvUtil.Unload();
m_setStartTime = true;
m_first_frame = true;
m_last_pts = DVD_NOPTS_VALUE;
return true;
}
void COMXAudio::Flush()
{
if(!m_Initialized)
return;
m_omx_decoder.FlushInput();
m_omx_tunnel_decoder.Flush();
if(!m_Passthrough)
m_omx_tunnel_mixer.Flush();
//m_setStartTime = true;
m_last_pts = DVD_NOPTS_VALUE;
m_LostSync = true;
//m_first_frame = true;
}
//***********************************************************************************************
bool COMXAudio::Pause()
{
if (!m_Initialized)
return -1;
if(m_Pause) return true;
m_Pause = true;
m_omx_decoder.SetStateForComponent(OMX_StatePause);
return true;
}
//***********************************************************************************************
bool COMXAudio::Resume()
{
if (!m_Initialized)
return -1;
if(!m_Pause) return true;
m_Pause = false;
m_omx_decoder.SetStateForComponent(OMX_StateExecuting);
return true;
}
//***********************************************************************************************
bool COMXAudio::Stop()
{
if (!m_Initialized)
return -1;
Flush();
m_Pause = false;
return true;
}
//***********************************************************************************************
long COMXAudio::GetCurrentVolume() const
{
return m_CurrentVolume;
}
//***********************************************************************************************
void COMXAudio::Mute(bool bMute)
{
if(!m_Initialized)
return;
if (bMute)
SetCurrentVolume(VOLUME_MINIMUM);
else
SetCurrentVolume(m_CurrentVolume);
}
//***********************************************************************************************
bool COMXAudio::SetCurrentVolume(long nVolume)
{
if(!m_Initialized || m_Passthrough)
return false;
m_CurrentVolume = nVolume;
if((m_downmix_channels == 6 || m_downmix_channels == 8) &&
m_OutputChannels == 2)
{
// Convert from millibels to amplitude ratio
double r = pow(10, nVolume / 2000.0);
const float* coeff = NULL;
switch(m_downmix_channels)
{
case 6:
coeff = downmixing_coefficients_6;
break;
case 8:
coeff = downmixing_coefficients_8;
break;
default:
assert(0);
}
if(m_normalize_downmix)
{
double sum_L = 0;
double sum_R = 0;
for(size_t i = 0; i < 16; ++i)
{
if(i & 1)
sum_R += coeff[i];
else
sum_L += coeff[i];
}
r /= max(sum_L, sum_R);
}
OMX_CONFIG_BRCMAUDIODOWNMIXCOEFFICIENTS mix;
OMX_INIT_STRUCTURE(mix);
mix.nPortIndex = m_omx_mixer.GetInputPort();
static_assert(sizeof(mix.coeff)/sizeof(mix.coeff[0]) == 16,
"Unexpected OMX_CONFIG_BRCMAUDIODOWNMIXCOEFFICIENTS::coeff length");
for(size_t i = 0; i < 16; ++i)
mix.coeff[i] = static_cast<unsigned int>(0x10000 * (coeff[i] * r));
OMX_ERRORTYPE omx_err =
m_omx_mixer.SetConfig(OMX_IndexConfigBrcmAudioDownmixCoefficients, &mix);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - error setting OMX_IndexConfigBrcmAudioDownmixCoefficients, error 0x%08x\n",
CLASSNAME, __func__, omx_err);
return false;
}
}
else
{
OMX_AUDIO_CONFIG_VOLUMETYPE volume;
OMX_INIT_STRUCTURE(volume);
volume.nPortIndex = m_omx_render.GetInputPort();
volume.sVolume.nValue = nVolume;
OMX_ERRORTYPE omx_err =
m_omx_render.SetConfig(OMX_IndexConfigAudioVolume, &volume);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "%s::%s - error setting OMX_IndexConfigAudioVolume, error 0x%08x\n",
CLASSNAME, __func__, omx_err);
return false;
}
}
return true;
}
//***********************************************************************************************
unsigned int COMXAudio::GetSpace()
{
int free = m_omx_decoder.GetInputBufferSpace();
return free;
}
unsigned int COMXAudio::AddPackets(const void* data, unsigned int len)
{
return AddPackets(data, len, 0, 0);
}
//***********************************************************************************************
unsigned int COMXAudio::AddPackets(const void* data, unsigned int len, double dts, double pts)
{
if(!m_Initialized) {
CLog::Log(LOGERROR,"COMXAudio::AddPackets - sanity failed. no valid play handle!");
return len;
}
if (!m_Passthrough && m_pCallback)
{
unsigned int mylen = std::min(len, sizeof m_visBuffer);
memcpy(m_visBuffer, data, mylen);
m_visBufferLength = mylen;
}
if(m_eEncoding == OMX_AUDIO_CodingDTS && m_LostSync && (m_Passthrough || m_HWDecode))
{
int skip = SyncDTS((uint8_t *)data, len);
if(skip > 0)
return len;
}
if(m_eEncoding == OMX_AUDIO_CodingDDP && m_LostSync && (m_Passthrough || m_HWDecode))
{
int skip = SyncAC3((uint8_t *)data, len);
if(skip > 0)
return len;
}
unsigned int demuxer_bytes = (unsigned int)len;
uint8_t *demuxer_content = (uint8_t *)data;
OMX_ERRORTYPE omx_err;
OMX_BUFFERHEADERTYPE *omx_buffer = NULL;
while(demuxer_bytes)
{
// 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;
omx_buffer->nFilledLen = (demuxer_bytes > omx_buffer->nAllocLen) ? omx_buffer->nAllocLen : demuxer_bytes;
memcpy(omx_buffer->pBuffer, demuxer_content, omx_buffer->nFilledLen);
/*
if (m_SampleSize > 0 && pts != DVD_NOPTS_VALUE && !(omx_buffer->nFlags & OMX_BUFFERFLAG_TIME_UNKNOWN) && !m_Passthrough && !m_HWDecode)
{
pts += ((double)omx_buffer->nFilledLen * DVD_TIME_BASE) / m_SampleSize;
}
printf("ADec : pts %f omx_buffer 0x%08x buffer 0x%08x number %d\n",
(float)pts / AV_TIME_BASE, omx_buffer, omx_buffer->pBuffer, (int)omx_buffer->pAppPrivate);
*/
uint64_t val = (uint64_t)(pts == DVD_NOPTS_VALUE) ? 0 : pts;
if(m_setStartTime)
{
omx_buffer->nFlags = OMX_BUFFERFLAG_STARTTIME;
m_setStartTime = false;
m_last_pts = pts;
}
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_bytes -= omx_buffer->nFilledLen;
demuxer_content += omx_buffer->nFilledLen;
if(demuxer_bytes == 0)
omx_buffer->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
int nRetry = 0;
while(true)
{
omx_err = m_omx_decoder.EmptyThisBuffer(omx_buffer);
if (omx_err == OMX_ErrorNone)
{
break;
}
else
{
CLog::Log(LOGERROR, "%s::%s - OMX_EmptyThisBuffer() failed with result(0x%x)\n", CLASSNAME, __func__, omx_err);
nRetry++;
}
if(nRetry == 5)
{
CLog::Log(LOGERROR, "%s::%s - OMX_EmptyThisBuffer() finaly failed\n", CLASSNAME, __func__);
printf("%s::%s - OMX_EmptyThisBuffer() finaly failed\n", CLASSNAME, __func__);
return 0;
}
}
if(m_first_frame)
{
m_first_frame = false;
//m_omx_render.WaitForEvent(OMX_EventPortSettingsChanged);
m_omx_render.DisablePort(m_omx_render.GetInputPort(), false);
if(!m_Passthrough)
{
m_omx_mixer.DisablePort(m_omx_mixer.GetOutputPort(), false);
m_omx_mixer.DisablePort(m_omx_mixer.GetInputPort(), false);
}
m_omx_decoder.DisablePort(m_omx_decoder.GetOutputPort(), false);
if(!m_Passthrough)
{
OMX_INIT_STRUCTURE(m_pcm_input);
m_pcm_input.nPortIndex = m_omx_decoder.GetOutputPort();
omx_err = m_omx_decoder.GetParameter(OMX_IndexParamAudioPcm, &m_pcm_input);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::AddPackets error GetParameter 1 omx_err(0x%08x)\n", omx_err);
}
/* setup mixer input */
m_pcm_input.nPortIndex = m_omx_mixer.GetInputPort();
omx_err = m_omx_mixer.SetParameter(OMX_IndexParamAudioPcm, &m_pcm_input);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::AddPackets error SetParameter 1 omx_err(0x%08x)\n", omx_err);
}
omx_err = m_omx_mixer.GetParameter(OMX_IndexParamAudioPcm, &m_pcm_input);
if(omx_err != OMX_ErrorNone)
{
CLog::Log(LOGERROR, "COMXAudio::AddPackets error GetParameter 2 omx_err(0x%08x)\n", omx_err);
}
/* setup mixer output */
m_pcm_output.nPortIndex = m_omx_mixer.GetOutputPort();
omx_err = m_omx_mixer.SetParameter(OMX_IndexParamAudioPcm, &m_pcm_output);
if(omx_err != OMX_ErrorNone)
{