2 Copyright 2004-2005 Chris Tallon, 2009 Marten Richter
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP. If not, see <https://www.gnu.org/licenses/>.
24 #include "woptionpane.h"
26 #include "osdopenvg.h"
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/opt.h"
40 lastAType = MPTYPE_MPEG_AUDIO;
46 canpass_pcm_mch=false;
48 prefered_ac3=0; //0 stereo PCM, 1 passthrough 2 Multichannel PCM
56 omx_aud_rend/*dec*/=0;
57 cur_input_buf_omx=NULL;
60 ac3codec_context_libav=NULL;
63 mp23codec_context_libav=NULL;
69 decompress_buffer=NULL;
70 decompress_buffer_size=0;
71 decompress_buffer_filled=0;
75 strcpy(L_VPE_OMX_AUDIO_REND, VPE_OMX_AUDIO_REND);
82 int AudioOMX::init(UCHAR tstreamType) {
87 streamType = tstreamType;
89 if (!initAllParams()) {
96 decompress_buffer_size=20000;
97 decompress_buffer=(UCHAR*)malloc(decompress_buffer_size);
98 decompress_buffer_filled=0;
100 #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100)
104 av_log_set_flags(AV_LOG_SKIP_REPEATED);
106 ac3codec_libav = avcodec_find_decoder(AV_CODEC_ID_AC3);
107 if (ac3codec_libav == NULL) {
108 Log::getInstance()->log("Audio", Log::DEBUG,
109 "Find libav ac3 decoder failed");
113 mp23codec_libav = avcodec_find_decoder(AV_CODEC_ID_MP3);
114 if (mp23codec_libav == NULL) {
115 Log::getInstance()->log("Audio", Log::DEBUG,
116 "Find libav mpeg audio decoder failed");
120 aaclatmcodec_libav = avcodec_find_decoder(AV_CODEC_ID_AAC_LATM);
121 if (aaclatmcodec_libav == NULL) {
122 Log::getInstance()->log("Audio", Log::DEBUG,
123 "Find libav aac latm decoder failed");
129 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eMPEG1,2,EDID_AudioSampleRate_e48KHz,0);
133 Log::getInstance()->log("Audio", Log::NOTICE,
134 "TV hdmi supports mpeg1 layer 1 and 2");
136 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eMP3,2,EDID_AudioSampleRate_e48KHz,0);
140 Log::getInstance()->log("Audio", Log::NOTICE,
141 "TV hdmi supports mpeg1 layer 3");
144 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eAC3,6,EDID_AudioSampleRate_e48KHz,0);
147 Log::getInstance()->log("Audio", Log::NOTICE,
148 "TV hdmi supports AC3");
150 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eAAC,6,EDID_AudioSampleRate_e48KHz,0);
154 Log::getInstance()->log("Audio", Log::NOTICE,
155 "TV hdmi supports AAC");
158 canpass_pcm_mch=false;
163 int AudioOMX::initAllParams()
165 return (setStreamType(streamType) && setChannel() && setSource());
168 int AudioOMX::shutdown()
170 if (!initted) return 0;
173 Log::getInstance()->log("Audio", Log::DEBUG, "audio shutdown called");
174 DeAllocateCodecsOMX();
176 free(decompress_buffer);
177 decompress_buffer=NULL;
178 decompress_buffer_size=0;
179 decompress_buffer_filled=0;
184 bool AudioOMX::loadOptionsFromServer(VDR* vdr)
186 Log::getInstance()->log("Audio", Log::DEBUG, "AudioOMX config load");
187 char *name=vdr->configLoad("AudioOMX","AC3DecodingMode");
190 if (STRCASECMP(name, "PCM") == 0) {
192 } else if (STRCASECMP(name, "Passthrough") == 0) {
194 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
200 name = vdr->configLoad("AudioOMX", "Mp2DecodingMode");
203 if (STRCASECMP(name, "PCM") == 0) {
205 } else if (STRCASECMP(name, "Passthrough") == 0) {
207 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
213 name = vdr->configLoad("AudioOMX", "AACDecodingMode");
216 if (STRCASECMP(name, "PCM") == 0) {
218 } else if (STRCASECMP(name, "Passthrough") == 0) {
220 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
226 name = vdr->configLoad("AudioOMX", "Mp3DecodingMode");
229 if (STRCASECMP(name, "PCM") == 0) {
231 } else if (STRCASECMP(name, "Passthrough") == 0) {
233 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
239 name = vdr->configLoad("AudioOMX", "AudioOutput");
242 if (STRCASECMP(name, "analog") == 0) {
244 } else if (STRCASECMP(name, "HDMI") == 0) {
255 bool AudioOMX::handleOptionChanges(Option* option)
257 if (Audio::handleOptionChanges(option))
259 switch (option->id) {
261 if (STRCASECMP(option->options[option->userSetChoice], "analog") == 0) {
263 } else if (STRCASECMP(option->options[option->userSetChoice], "HDMI")
271 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
273 } else if (STRCASECMP(option->options[option->userSetChoice],
274 "Passthrough") == 0) {
276 } else if (STRCASECMP(option->options[option->userSetChoice],
277 "PCMMultichannel") == 0) {
283 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
285 } else if (STRCASECMP(option->options[option->userSetChoice],
286 "Passthrough") == 0) {
288 } else if (STRCASECMP(option->options[option->userSetChoice],
289 "PCMMultichannel") == 0) {
295 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
297 } else if (STRCASECMP(option->options[option->userSetChoice],
298 "Passthrough") == 0) {
300 } else if (STRCASECMP(option->options[option->userSetChoice],
301 "PCMMultichannel") == 0) {
307 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
309 } else if (STRCASECMP(option->options[option->userSetChoice],
310 "Passthrough") == 0) {
312 } else if (STRCASECMP(option->options[option->userSetChoice],
313 "PCMMultichannel") == 0) {
323 bool AudioOMX::saveOptionstoServer()
326 switch (prefered_ac3) {
328 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode", "PCM");
331 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
335 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
340 switch (prefered_aac) {
342 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode", "PCM");
345 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
349 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
354 switch (prefered_mp2) {
356 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode", "PCM");
359 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
363 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
368 switch (prefered_mp3) {
370 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode", "PCM");
373 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
377 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
383 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "analog");
385 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "HDMI");
391 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
392 UINT numChoices, UINT defaultChoice, UINT startInt,
393 const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
395 bool AudioOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
397 if (!Audio::addOptionsToPanes(panenumber,options,pane)) return false;
404 static const char* audioopts[]={"analog","HDMI"};
405 option = new Option(4,tr("Audio Output"), "AudioOMX","AudioOutput",Option::TYPE_TEXT,2,0,0,audioopts,NULL,false,this);
406 options->push_back(option);
407 pane->addOptionLine(option);
410 char **ac3opts=new char *[3];
412 ac3opts[i]=new char[strlen("PCM")+1];
413 strcpy(ac3opts[i],"PCM");
416 ac3opts[i]=new char[strlen("Passthrough")+1];
417 strcpy(ac3opts[i],"PassThrough");
420 if (canpass_pcm_mch) {
421 ac3opts[i]=new char[strlen("PCMMultichannel")+1];
422 strcpy(ac3opts[i],"PCMMultichannel");
425 option = new Option(1 ,tr("AC3 HDMI Mode"), "AudioOMX", "AC3DecodingMode", Option::TYPE_TEXT, i, 0, 0, ac3opts,NULL,true, this);
426 options->push_back(option);
427 pane->addOptionLine(option);
429 /* char **aacopts = new char *[3];
431 aacopts[i] = new char[strlen("PCM") + 1];
432 strcpy(mp2opts[i], "PCM");
435 aacopts[i] = new char[strlen("Passthrough") + 1];
436 strcpy(aacopts[i], "PassThrough");
439 if (canpass_pcm_mch) {
440 aacopts[i] = new char[strlen("PCMMultichannel") + 1];
441 strcpy(aacopts[i], "PCMMultichannel");
444 option = new Option(5, tr("Mp2 HDMI Mode"), "AudioOMX",
445 "AACDecodingMode", Option::TYPE_TEXT, i, 0, 0,
446 aacopts, NULL, true, this);
447 options->push_back(option);
448 pane->addOptionLine(option);
451 char **mp2opts = new char *[3];
453 mp2opts[i] = new char[strlen("PCM") + 1];
454 strcpy(mp2opts[i], "PCM");
457 mp2opts[i] = new char[strlen("Passthrough") + 1];
458 strcpy(mp2opts[i], "PassThrough");
461 if (canpass_pcm_mch) {
462 mp2opts[i] = new char[strlen("PCMMultichannel") + 1];
463 strcpy(mp2opts[i], "PCMMultichannel");
466 option = new Option(2, tr("Mp2 HDMI Mode"), "AudioOMX",
467 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0,
468 mp2opts, NULL, true, this);
469 options->push_back(option);
470 pane->addOptionLine(option);
472 char **mp3opts = new char *[3];
474 mp3opts[i] = new char[strlen("PCM") + 1];
475 strcpy(mp3opts[i], "PCM");
478 mp3opts[i] = new char[strlen("Passthrough") + 1];
479 strcpy(mp3opts[i], "PassThrough");
482 if (canpass_pcm_mch) {
483 mp3opts[i] = new char[strlen("PCMMultichannel") + 1];
484 strcpy(mp3opts[i], "PCMMultichannel");
487 option = new Option(3, tr("Mp3 HDMI Mode"), "AudioOMX",
488 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0, mp3opts,
490 options->push_back(option);
491 pane->addOptionLine(option);*/
492 // Comment unsupported modes out
505 OMX_ERRORTYPE AudioOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
507 //Log::getInstance()->log("Audio", Log::NOTICE, "EmptyBufferDone");
508 AudioOMX *audio=(AudioOMX *)getInstance();
509 audio->ReturnEmptyOMXBuffer(buffer);
510 return OMX_ErrorNone;
514 void AudioOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
515 input_bufs_omx_mutex.lock();
516 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
517 input_bufs_omx_free.push_back(buffer);
518 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
519 input_bufs_omx_mutex.unlock();
520 VideoOMX *video=(VideoOMX*)Video::getInstance();
524 OMX_ERRORTYPE AudioOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
525 Log::getInstance()->log("Audio", Log::NOTICE, "FillBufferDone");
526 VideoOMX *video=(VideoOMX*)Video::getInstance();
528 return OMX_ErrorNone;
533 int AudioOMX::setStreamType(UCHAR type)
535 if (!initted) return 0;
537 // if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
541 int AudioOMX::setChannel()
543 if (!initted) return 0;
545 // if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
549 int AudioOMX::setSource()
551 if (!initted) return 0;
553 // if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
559 if (!initted) return 0;
561 // if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
565 int AudioOMX::play() {
568 lastAType=MPTYPE_MPEG_AUDIO;
569 Log::getInstance()->log("Audio", Log::DEBUG, "enter play");
571 ((VideoOMX*)Video::getInstance())->interlaceSwitch4Demux(); // switch resolution if necessary
573 if (!AllocateCodecsOMX()) {
580 int AudioOMX::ChangeAudioDestination() //clock aka omx mutex needs to be locked
583 const char * destinations[]={"local","hdmi"};
588 OMX_CONFIG_BRCMAUDIODESTINATIONTYPE auddest;
589 memset(&auddest,0,sizeof(auddest));
590 auddest.nSize=sizeof(auddest);
591 auddest.nVersion.nVersion=OMX_VERSION;
592 strcpy((char *)auddest.sName, destinations[dest]);
594 Log::getInstance()->log("Audio", Log::DEBUG, "setting destination to: %s",auddest.sName);
595 error=OMX_SetConfig(omx_aud_rend,OMX_IndexConfigBrcmAudioDestination,&auddest);
596 if (error!=OMX_ErrorNone){
597 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX_IndexConfigBrcmAudioDestination failed %x %x %s", error,omx_aud_rend,auddest.sName);
598 DeAllocateCodecsOMX();
606 int AudioOMX::ChangeAudioPortConfig(bool disport) //clock aka omx mutex needs to be locked
609 //Ok first fidle a working configuration
610 Log::getInstance()->log("Audio", Log::DEBUG,
611 "ChangeAudioPortConfig");
613 OMX_AUDIO_CODINGTYPE encoding;
616 case MPTYPE_MPEG_AUDIO: {
617 if (prefered_mp2 == 2 && false) { //not supported yet
620 if (prefered_mp2 == 1 && canpass_mp2) {
622 encoding=OMX_AUDIO_CodingMP3;
625 encoding=OMX_AUDIO_CodingPCM;
630 case MPTYPE_AAC_LATM: {
631 if (prefered_aac == 2 && false) { //not supported yet
634 Log::getInstance()->log("Audio", Log::DEBUG,
635 "ChangeAudioPortConfig debug %d %d",prefered_aac,canpass_aac);
636 if (prefered_aac == 1 && canpass_aac) {
638 encoding=OMX_AUDIO_CodingAAC;
641 encoding=OMX_AUDIO_CodingPCM;
646 case MPTYPE_AC3_PRE13:
648 if (prefered_ac3 == 2 && false) { //not supported yet
651 Log::getInstance()->log("Audio", Log::DEBUG,
652 "ChangeAudioPortConfig debug %d %d",prefered_ac3,canpass_ac3);
653 if (prefered_ac3 == 1 && canpass_ac3) {
655 encoding=OMX_AUDIO_CodingDDP;
658 encoding=OMX_AUDIO_CodingPCM;
663 case MPTYPE_MPEG_AUDIO_LAYER3: {
664 if (prefered_mp3 == 2 && false) { //not supported yet
667 if (prefered_mp3 == 1 && canpass_mp2) {
669 encoding=OMX_AUDIO_CodingMP3;
672 encoding=OMX_AUDIO_CodingPCM;
680 encoding=OMX_AUDIO_CodingPCM;
681 //mch=false; // multichannel also false
686 /*OMX_CONFIG_BOOLEANTYPE booly;
687 memset(&booly, 0, sizeof(booly));
688 booly.nSize = sizeof(booly);
689 booly.nVersion.nVersion = OMX_VERSION;
691 booly.bEnabled = OMX_TRUE;
693 booly.bEnabled = OMX_FALSE;
695 error = OMX_SetParameter(omx_aud_dec, OMX_IndexParamBrcmDecoderPassThrough,
697 if (error != OMX_ErrorNone) {
698 Log::getInstance()->log("Audio", Log::DEBUG,
699 "Init OMX_IndexParamBrcmDecoderPassThrough failed %x", error);
700 DeAllocateCodecsOMX();
703 VideoOMX* video=(VideoOMX*)Video::getInstance();
705 video->DisablePort(omx_aud_rend,omx_rend_input_port,false);
706 //DestroyInputBufsOMXwhilePlaying();
707 //video->CommandFinished(omx_aud_rend,OMX_CommandPortDisable,omx_rend_input_port);
712 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
713 memset(&format, 0, sizeof(format));
714 format.nSize = sizeof(format);
715 format.nVersion.nVersion = OMX_VERSION;
716 format.nPortIndex = omx_rend_input_port;
717 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
719 if (error != OMX_ErrorNone) {
720 Log::getInstance()->log("Audio", Log::DEBUG,
721 "Get OMX_IndexParamAudioPortFormat failed %x %d", error,
722 omx_rend_input_port);
727 Log::getInstance()->log("Audio", Log::DEBUG,
728 "Get OMX_IndexParamAudioPortFormat returned %d",format.eEncoding );
729 format.eEncoding = encoding;
731 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
733 if (error != OMX_ErrorNone) {
734 Log::getInstance()->log("Audio", Log::DEBUG,
735 "Set OMX_IndexParamAudioPortFormat failed %x %d %d", error,
736 omx_rend_input_port,format.eEncoding );
741 case OMX_AUDIO_CodingPCM: {
742 OMX_AUDIO_PARAM_PCMMODETYPE audio_pcm;
743 memset(&audio_pcm, 0, sizeof(audio_pcm));
744 audio_pcm.nSize = sizeof(audio_pcm);
745 audio_pcm.nVersion.nVersion = OMX_VERSION;
746 audio_pcm.nChannels = 2;
747 audio_pcm.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
748 audio_pcm.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
749 //audio_pcm.eChannelMapping[2]=OMX_AUDIO_ChannelMax;
750 audio_pcm.eNumData = OMX_NumericalDataSigned;
751 audio_pcm.eEndian = OMX_EndianLittle;
752 audio_pcm.bInterleaved = OMX_TRUE;
753 audio_pcm.nBitPerSample = 16;
754 audio_pcm.ePCMMode = OMX_AUDIO_PCMModeLinear;
755 audio_pcm.nChannels = 2;
756 audio_pcm.nSamplingRate = 48000;
757 audio_pcm.nPortIndex = omx_rend_input_port;
758 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPcm,
760 if (error != OMX_ErrorNone) {
761 Log::getInstance()->log("Audio", Log::DEBUG,
762 "Init OMX_IndexParamAudioPcm failed %x %d", error,
763 omx_rend_input_port);
767 case OMX_AUDIO_CodingDDP: {
768 OMX_AUDIO_PARAM_DDPTYPE audio_ddp;
769 memset(&audio_ddp, 0, sizeof(audio_ddp));
770 audio_ddp.nSize = sizeof(audio_ddp);
771 audio_ddp.nVersion.nVersion = OMX_VERSION;
772 audio_ddp.nPortIndex = omx_rend_input_port;
773 audio_ddp.nChannels = 8; //unknown
774 audio_ddp.nBitRate=0;
775 audio_ddp.nSampleRate=48000;
776 audio_ddp.eChannelMapping[0] =OMX_AUDIO_ChannelLF;
777 audio_ddp.eChannelMapping[1] =OMX_AUDIO_ChannelRF;
778 audio_ddp.eChannelMapping[2] =OMX_AUDIO_ChannelCF;
779 audio_ddp.eChannelMapping[3] =OMX_AUDIO_ChannelLFE;
780 audio_ddp.eChannelMapping[4] =OMX_AUDIO_ChannelLR;
781 audio_ddp.eChannelMapping[5] =OMX_AUDIO_ChannelRR;
782 audio_ddp.eChannelMapping[6] =OMX_AUDIO_ChannelLS;
783 audio_ddp.eChannelMapping[7] =OMX_AUDIO_ChannelRS;
784 audio_ddp.eChannelMapping[8] =OMX_AUDIO_ChannelCS;
785 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioDdp,
787 if (error != OMX_ErrorNone) {
788 Log::getInstance()->log("Audio", Log::DEBUG,
789 "Init OMX_IndexParamAudioDdp failed %x %d", error,
790 omx_rend_input_port);
795 default: break; //Make compiler happy
802 //PrepareInputBufsOMX(false);
803 video->EnablePort(omx_aud_rend,omx_rend_input_port,false);
813 int AudioOMX::InitDecoderLibAV()
816 ac3codec_context_libav = avcodec_alloc_context3(ac3codec_libav);
817 if (!ac3codec_context_libav) {
818 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for ac3 decoding context failed!");
822 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 34, 1)
823 ac3codec_context_libav->flags |= AV_CODEC_FLAG_TRUNCATED;
825 ac3codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
828 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
829 ac3codec_context_libav->request_channel_layout=2;
831 ac3codec_context_libav->request_channels=2;
834 int avc_ret = avcodec_open2(ac3codec_context_libav, ac3codec_libav, NULL);
836 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
837 libav_mutex.unlock();
841 aaclatmcodec_context_libav = avcodec_alloc_context3(aaclatmcodec_libav);
842 if (!aaclatmcodec_context_libav) {
843 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for aac decoding context failed!");
847 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 34, 1)
848 aaclatmcodec_context_libav->flags |= AV_CODEC_FLAG_TRUNCATED;
850 aaclatmcodec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
853 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
854 aaclatmcodec_context_libav->request_channel_layout=2;
856 aaclatmcodec_context_libav->request_channels=2;
859 avc_ret = avcodec_open2(aaclatmcodec_context_libav, aaclatmcodec_libav, NULL);
861 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
862 libav_mutex.unlock();
867 mp23codec_context_libav = avcodec_alloc_context3(mp23codec_libav);
868 if (!ac3codec_context_libav) {
869 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for mp23 decoding context failed!");
870 libav_mutex.unlock();
874 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 34, 1)
875 mp23codec_context_libav->flags |= AV_CODEC_FLAG_TRUNCATED;
877 mp23codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
880 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
881 mp23codec_context_libav->request_channel_layout=2;
883 mp23codec_context_libav->request_channels=2;
886 avc_ret = avcodec_open2(mp23codec_context_libav, mp23codec_libav, NULL);
888 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
889 libav_mutex.unlock();
893 resam_con_libav = avresample_alloc_context();
894 if (resam_con_libav == NULL) {
895 Log::getInstance()->log("Audio", Log::DEBUG,
896 "Alloc resample context failed");
900 av_opt_set_int(resam_con_libav, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0); // our standard format
901 av_opt_set_int(resam_con_libav, "out_sample_rate",48000,0);
902 av_opt_set_int(resam_con_libav, "out_sample_fmt",AV_SAMPLE_FMT_S16,0);
903 av_opt_set_int(resam_con_libav, "matrix_encoding",AV_MATRIX_ENCODING_DPLII,0);
905 av_opt_set_int(resam_con_libav, "in_sample_rate",48000,0);
906 av_opt_set_int(resam_con_libav, "in_sample_fmt",AV_SAMPLE_FMT_S16,0);
907 av_opt_set_int(resam_con_libav, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0); //just an example
910 av_init_packet(&incoming_paket_libav);
911 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
912 decode_frame_libav=av_frame_alloc();
914 decode_frame_libav=avcodec_alloc_frame();
916 libav_mutex.unlock();
917 decompress_buffer_filled=0;
924 void AudioOMX::DeinitDecoderLibAV() {
928 if (ac3codec_context_libav) {
929 avcodec_close(ac3codec_context_libav);
930 av_free(ac3codec_context_libav);
931 ac3codec_context_libav = NULL;
933 avcodec_close(aaclatmcodec_context_libav);
934 av_free(aaclatmcodec_context_libav);
935 aaclatmcodec_context_libav = NULL;
937 av_free(decode_frame_libav);
939 avcodec_close(mp23codec_context_libav);
940 av_free(mp23codec_context_libav);
941 mp23codec_context_libav = NULL;
943 avresample_free(&resam_con_libav);
944 resam_con_libav=NULL;
948 libav_mutex.unlock();
953 int AudioOMX::AllocateCodecsOMX()
956 static OMX_CALLBACKTYPE callbacks= {&VideoOMX::EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
958 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX");
959 //Clock, move later to audio
960 VideoOMX *video=(VideoOMX*)Video::getInstance();
962 if (!InitDecoderLibAV()) return 0;;
965 OMX_PORT_PARAM_TYPE p_param;
966 memset(&p_param,0,sizeof(p_param));
967 p_param.nSize=sizeof(p_param);
968 p_param.nVersion.nVersion=OMX_VERSION;
971 if (!video->getClockAudioandInit(&omx_clock,&omx_clock_output_port)){
972 return 0;// get the clock and init it if necessary
976 if (!video->idleClock()) {
981 error = OMX_GetHandle(&omx_aud_rend, L_VPE_OMX_AUDIO_REND, NULL, &callbacks);
982 if (error != OMX_ErrorNone) {
983 Log::getInstance()->log("Audio", Log::DEBUG,
984 "Init OMX audio rend failed %x", error);
985 video->UnlockClock();
986 DeAllocateCodecsOMX();
990 if (!ChangeAudioDestination()) {
991 video->UnlockClock();
992 DeAllocateCodecsOMX();
996 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioInit, &p_param);
997 if (error != OMX_ErrorNone) {
998 Log::getInstance()->log("Audio", Log::DEBUG,
999 "Init OMX audio rend OMX_GetParameter failed %x", error);
1000 video->UnlockClock();
1001 DeAllocateCodecsOMX();
1004 omx_rend_input_port = p_param.nStartPortNumber;
1010 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamOtherInit, &p_param);
1011 if (error != OMX_ErrorNone) {
1012 Log::getInstance()->log("Audio", Log::DEBUG,
1013 "Init OMX aud rend OMX_GetParameter failed %x", error);
1014 video->UnlockClock();
1015 DeAllocateCodecsOMX();
1018 // buggy return value
1019 omx_rend_clock_port = p_param.nStartPortNumber;
1024 /* error=OMX_GetHandle(&omx_aud_dec,VPE_OMX_AUDIO_DECODER,NULL,&callbacks);
1026 if (error!=OMX_ErrorNone){
1027 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder failed %x", error);
1028 video->UnlockClock();
1029 DeAllocateCodecsOMX();
1033 error=OMX_GetParameter(omx_aud_dec,OMX_IndexParamAudioInit,&p_param);
1034 if (error!=OMX_ErrorNone){
1035 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder OMX_GetParameter failed %x", error);
1036 video->UnlockClock();
1037 DeAllocateCodecsOMX();
1040 omx_codec_input_port=p_param.nStartPortNumber;
1041 omx_codec_output_port=p_param.nStartPortNumber+1;
1043 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port) || !video->DisablePort(omx_aud_dec,omx_codec_output_port)) {
1044 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio decoder failed");
1045 video->UnlockClock();
1046 DeAllocateCodecsOMX();
1053 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true) ) {
1054 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio rend failed %d",omx_rend_input_port);
1055 video->UnlockClock();
1056 DeAllocateCodecsOMX();
1060 if ( !video->DisablePort(omx_aud_rend, omx_rend_clock_port, true)) {
1061 Log::getInstance()->log("Audio", Log::DEBUG,
1062 "Disable Ports OMX rend clock port failed %d",omx_rend_clock_port);
1063 video->UnlockClock();
1064 DeAllocateCodecsOMX();
1074 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_aud_rend,omx_rend_clock_port);
1075 if (error!=OMX_ErrorNone){
1076 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel clock to rend failed %x %d %d", error,omx_clock_output_port,omx_rend_clock_port);
1077 video->UnlockClock();
1078 DeAllocateCodecsOMX();
1082 if (!video->EnablePort(omx_clock,omx_clock_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_clock_port,false)
1084 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX clock rend failed");
1085 video->UnlockClock();
1086 DeAllocateCodecsOMX();
1090 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1091 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend idle ChangeComponentState");
1092 video->UnlockClock();
1093 DeAllocateCodecsOMX();
1100 if ( !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_clock_port)) {
1101 video->UnlockClock();
1102 DeAllocateCodecsOMX();
1106 if ( !video->CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1107 video->UnlockClock();
1108 DeAllocateCodecsOMX();
1114 if (!ChangeAudioPortConfig(false)){
1115 Log::getInstance()->log("Audio", Log::NOTICE, "Change AudioPortConfig failed");
1116 video->UnlockClock();
1117 DeAllocateCodecsOMX();
1121 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1122 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1123 DeAllocateCodecsOMX();
1129 if (!PrepareInputBufsOMX(true)) {
1130 video->UnlockClock();
1131 DeAllocateCodecsOMX();
1137 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,omx_aud_rend,omx_rend_input_port);
1138 if (error!=OMX_ErrorNone){
1139 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel dec to render failed %x", error);
1140 video->UnlockClock();
1141 DeAllocateCodecsOMX();
1147 /* if (!video->EnablePort(omx_aud_dec,omx_codec_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_input_port,false)
1149 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX codec rend failed");
1150 video->UnlockClock();
1151 DeAllocateCodecsOMX();
1155 /* if ( !video->CommandFinished(omx_aud_dec,OMX_CommandPortEnable,omx_codec_output_port)
1156 || !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1157 video->UnlockClock();
1158 DeAllocateCodecsOMX();
1162 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateExecuting)) {
1163 Log::getInstance()->log("Audio", Log::DEBUG, "omx_aud_rend ChangeComponentState Execute");
1164 video->UnlockClock();
1165 DeAllocateCodecsOMX();
1170 video->UnlockClock();
1174 video->clockUnpause();
1177 if (!video->setClockExecutingandRunning()) return 0;
1179 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX finished");
1187 int AudioOMX::PrepareInputBufsOMX(bool setportdef) //needs to be called with locvke omx clock mutex
1189 VideoOMX *video=(VideoOMX*)Video::getInstance();
1190 OMX_ERRORTYPE error;
1191 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1192 memset(&port_def_type,0,sizeof(port_def_type));
1193 port_def_type.nSize=sizeof(port_def_type);
1194 port_def_type.nVersion.nVersion=OMX_VERSION;
1195 port_def_type.nPortIndex=omx_rend_input_port;//omx_codec_input_port;
1197 error=OMX_GetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1199 if (error!=OMX_ErrorNone){
1200 Log::getInstance()->log("Audio", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1205 port_def_type.nBufferCountActual=2;
1206 port_def_type.nBufferSize=max(port_def_type.nBufferSize,50000); // for transcoder important
1208 error=OMX_SetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1210 if (error!=OMX_ErrorNone){
1211 Log::getInstance()->log("Audio", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1216 error=OMX_SendCommand(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port/*codec*/,0);
1217 if (error!=OMX_ErrorNone){
1218 Log::getInstance()->log("Audio", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1222 input_bufs_omx_mutex.lock();
1223 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1224 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1225 error=OMX_AllocateBuffer(omx_aud_rend/*dec*/,&buf_head,omx_rend_input_port/*codec*/,NULL,port_def_type.nBufferSize);
1226 if (error!=OMX_ErrorNone){
1227 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1228 input_bufs_omx_mutex.unlock();
1231 input_bufs_omx_all.push_back(buf_head);
1232 input_bufs_omx_free.push_back(buf_head);
1234 omx_first_frame=true;
1237 cur_input_buf_omx=NULL;
1238 input_bufs_omx_mutex.unlock();
1240 if (!video->CommandFinished(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port /*codec*/)) {
1247 int AudioOMX::DestroyInputBufsOMX() //call with clock mutex locked
1249 OMX_ERRORTYPE error;
1251 cur_input_buf_omx=NULL;
1252 input_bufs_omx_mutex.lock();
1253 for (UINT i=0; i< input_bufs_omx_all.size();i++) {
1254 error=OMX_FreeBuffer(omx_aud_rend/*dec*/,omx_rend_input_port/*codec*/,input_bufs_omx_all[i]);
1255 if (error!=OMX_ErrorNone){
1256 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1257 input_bufs_omx_mutex.unlock();
1262 input_bufs_omx_all.clear();
1263 input_bufs_omx_free.clear();
1264 input_bufs_omx_mutex.unlock();
1269 int AudioOMX::DestroyInputBufsOMXwhilePlaying() //call with clock mutex locked
1271 //OMX_ERRORTYPE error;
1273 cur_input_buf_omx=NULL;
1274 input_bufs_omx_mutex.lock();
1275 while (input_bufs_omx_all.size()>0) {
1276 if (input_bufs_omx_free.size()>0) {
1277 // Destroy one buffer
1278 std::vector<OMX_BUFFERHEADERTYPE*>::iterator itty=input_bufs_omx_all.begin();
1279 OMX_BUFFERHEADERTYPE* cur_buf=input_bufs_omx_free.front();
1280 for (; itty!= input_bufs_omx_all.end();itty++) {
1281 if ((*itty)==cur_buf) {
1282 input_bufs_omx_all.erase(itty);
1283 input_bufs_omx_free.pop_front();
1288 input_bufs_omx_mutex.unlock();
1290 input_bufs_omx_mutex.lock();
1294 Log::getInstance()->log("Audio", Log::DEBUG, "DestroyInputBufsOMXwhilePlaying %d %d", input_bufs_omx_all.size(),input_bufs_omx_free.size());
1295 input_bufs_omx_mutex.unlock();
1300 int AudioOMX::DeAllocateCodecsOMX()
1302 OMX_ERRORTYPE error;
1304 VideoOMX *video=(VideoOMX*)Video::getInstance();
1305 Log::getInstance()->log("Audio", Log::DEBUG, "enter deallocatecodecsomx");
1309 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 1");
1310 if (cur_input_buf_omx) {
1311 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1312 error=video->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,cur_input_buf_omx);
1313 if (error!=OMX_ErrorNone) {
1314 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_EmptyThisBuffer 6 failed %x", error);
1317 cur_input_buf_omx=NULL;//write out old data
1319 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 2");
1322 if (omx_aud_rend/*dec*/) {
1323 // first stop the omx elements
1324 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1325 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1327 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 3");
1329 video->UnlockClock();
1330 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 4");
1332 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 5");
1335 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1336 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend ChangeComponentState");
1339 // TODO proper deinit sequence
1340 // first flush all buffers
1342 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
1343 if (error!=OMX_ErrorNone) {
1344 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend in failed %x", error);
1348 /* error=OMX_SendCommand(omx_aud_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
1349 if (error!=OMX_ErrorNone){
1350 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1355 /* if (!video->CommandFinished(omx_aud_dec,OMX_CommandFlush,omx_codec_input_port)) {
1356 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd codec input failed");
1361 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1362 if (error!=OMX_ErrorNone){
1363 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1367 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_clock_port, NULL);
1368 if (error!=OMX_ErrorNone){
1369 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend clock failed %x", error);
1372 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6");
1374 if (!video->CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1375 !video->CommandFinished(omx_aud_rend,OMX_CommandFlush,omx_rend_clock_port)) {
1376 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd clock shed failed");
1379 DestroyInputBufsOMX(); //We have to make sure that no buffers are in use
1380 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6a");
1381 DeinitDecoderLibAV();
1382 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 7");
1385 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true)) {
1386 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 1");
1389 /* if (!video->DisablePort(omx_aud_dec,omx_codec_output_port,true)) {
1390 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 6");
1393 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port,true)) {
1394 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 7");
1398 if (!video->DisablePort(omx_aud_rend,omx_rend_clock_port,true)) {
1399 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 4");
1402 if (!video->DisablePort(omx_clock,omx_clock_output_port,true)) {
1403 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 5");
1408 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,NULL,NULL);
1409 if (error!=OMX_ErrorNone) {
1410 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1416 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_input_port,NULL,0);
1417 if (error!=OMX_ErrorNone) {
1418 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1422 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,0);
1423 if (error!=OMX_ErrorNone) {
1424 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1428 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_clock_port,NULL,0);
1429 if (error!=OMX_ErrorNone) {
1430 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1433 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 8");
1436 //error=OMX_FreeHandle(omx_aud_dec);
1437 error=OMX_FreeHandle(omx_aud_rend);
1438 video->UnlockClock();
1439 video->destroyClock();
1440 omx_aud_rend/*dec*/=NULL;
1441 if (error!=OMX_ErrorNone) {
1442 Log::getInstance()->log("Audio", Log::DEBUG, "FreeHandle failed %d", error);
1446 video->UnlockClock();
1447 DeinitDecoderLibAV();
1449 Log::getInstance()->log("Audio", Log::DEBUG, "leave deallocate codecs OMX");
1456 int AudioOMX::stop()
1458 if (!initted) return 0;
1460 Log::getInstance()->log("Audio", Log::DEBUG, "Audio stop called");
1461 DeAllocateCodecsOMX();
1462 //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1466 int AudioOMX::mute() {
1469 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
1470 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1474 OMX_AUDIO_CONFIG_MUTETYPE amute;
1475 memset(&amute, 0, sizeof(amute));
1476 amute.nSize = sizeof(amute);
1477 amute.nVersion.nVersion = OMX_VERSION;
1478 amute.nPortIndex = omx_rend_input_port;
1479 amute.bMute = OMX_TRUE;
1480 OMX_ERRORTYPE error= OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1482 if (error != OMX_ErrorNone) {
1483 Log::getInstance()->log("Audio", Log::DEBUG,
1484 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1485 omx_rend_input_port);
1497 int AudioOMX::unMute()
1499 if (!initted) return 0;
1501 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
1502 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1506 OMX_AUDIO_CONFIG_MUTETYPE amute;
1507 memset(&amute, 0, sizeof(amute));
1508 amute.nSize = sizeof(amute);
1509 amute.nVersion.nVersion = OMX_VERSION;
1510 amute.nPortIndex = omx_rend_input_port;
1511 amute.bMute = OMX_FALSE;
1512 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1514 if (error != OMX_ErrorNone) {
1515 Log::getInstance()->log("Audio", Log::DEBUG,
1516 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1517 omx_rend_input_port);
1529 int AudioOMX::pause() {
1533 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1539 int AudioOMX::unPause()
1541 if (!initted) return 0;
1543 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1550 int AudioOMX::reset()
1552 if (!initted) return 0;
1554 Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
1555 DeAllocateCodecsOMX();
1557 // if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1558 // Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
1559 // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
1565 int AudioOMX::setVolume(int tvolume)
1567 // parameter: 0 for silence, 20 for full
1568 if ((tvolume < 0) || (tvolume > 20)) return 0;
1570 // volume = 2 * (20 - volume);
1571 // Right, that one was rubbish... 0-10 were almost
1572 // inaudible, 11-20 did what should have been done
1573 // over the whole 0-20 range
1577 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1581 OMX_AUDIO_CONFIG_VOLUMETYPE avol;
1582 memset(&avol, 0, sizeof(avol));
1583 avol.nSize = sizeof(avol);
1584 avol.nVersion.nVersion = OMX_VERSION;
1585 avol.nPortIndex = omx_rend_input_port;
1586 avol.bLinear=OMX_FALSE;
1587 avol.sVolume.nValue =(volume-20)*200;
1588 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioVolume,
1590 if (error != OMX_ErrorNone) {
1591 Log::getInstance()->log("Audio", Log::DEBUG,
1592 "Set OMX_IndexConfigAudioVolume failed %x %d", error,
1593 omx_rend_input_port);
1605 int AudioOMX::test()
1608 // return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
1610 /* aud_sync_parms_t a;
1614 // int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
1617 /*OK*/ //printf("Audio sync disable = %i\n", b);
1625 unsigned int AudioOMX::AdvanceMpAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1627 if (size<=2) return size; // silly;
1628 unsigned int atest=0;
1630 //inspired by libav parsing code
1631 while (atest+1<size) {
1632 if (data[atest]==0xFF && (data[atest+1] &0xe0)==0xe0) {
1633 if ((data[atest+1] & 0x18) == 0x08) {atest++;continue;} //sanity check: mpeg version ID 01 -> reserved
1634 //sanity check inspired by libav
1637 const int sample_rates[4]={44100,48000,32000,0};
1638 const short bitrate_tab[2][3][15] = { { { 0, 32, 64, 96, 128, 160,
1639 192, 224, 256, 288, 320, 352, 384, 416, 448 }, { 0, 32, 48,
1640 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 }, {
1641 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
1642 256, 320 } }, { { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1643 160, 176, 192, 224, 256 }, { 0, 8, 16, 24, 32, 40, 48, 56,
1644 64, 80, 96, 112, 128, 144, 160 }, { 0, 8, 16, 24, 32, 40,
1645 48, 56, 64, 80, 96, 112, 128, 144, 160 } } };
1648 int layer=4-((data[atest+1]&0x06)>>1);
1649 if (layer==4) {atest++;continue;} //sanity check
1651 int bitrate_index=(data[atest+2]&0xf0)>>4;
1652 if (bitrate_index==0x0f || bitrate_index==0x0) {atest++;continue;} //sanity check
1654 int samplerate_index=(data[atest+2]&0x0C)>>2;
1655 if (samplerate_index==0x03) {atest++;continue;} //sanity check
1657 int padding=(data[atest+2]&2)>>1;
1658 if (0x10 & data[atest+1]) {
1659 lsf=((data[atest+1]) &0x8)?0:1;
1665 int sample_rate=sample_rates[ samplerate_index]>>(lsf+mpeg2);
1667 int temp_frame_size=bitrate_tab[lsf][layer - 1][bitrate_index];
1669 frame_size=(temp_frame_size*12000)/sample_rate;
1670 frame_size=(frame_size+padding)*4;
1671 } else if (layer==2) {
1672 frame_size=(temp_frame_size*144000)/sample_rate;
1673 frame_size=frame_size+padding;
1675 frame_size=(temp_frame_size*144000)/(sample_rate<<lsf);
1676 frame_size=frame_size+padding;
1678 unsigned int sameheadertest=(data[atest]<<24)|(data[atest+1]<<16) |(data[atest+2]<<8);
1679 const unsigned mask=(0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)); // from libav
1680 if (!mp3sameheader) {
1681 mp3sameheader=sameheadertest;
1682 mp3sameheadercount=2;
1684 if ((mp3sameheader& mask)== (sameheadertest &mask)) mp3sameheadercount++;
1685 else mp3sameheadercount=0;
1686 mp3sameheader=sameheadertest;
1688 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME: %d %d %d %d %d %d %d",lsf,layer,bitrate_index,sample_rate,padding,temp_frame_size, frame_size);
1689 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME DIAG: %x %x %x %d",data[atest],data[atest+1],data[atest+2],mp3sameheadercount);
1691 if (mp3sameheadercount>4) {
1692 *framesize=frame_size;
1693 return atest; // probably FrameSync
1694 } //skip it if the header changes too frequently
1701 unsigned int AudioOMX::AdvanceAc3AudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1703 if (size<=4) return size; // silly;
1704 const int frm_size_tab[] = { 64, 64, 80, 80, 96, 96, 112, 112, 128, 128,
1705 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384, 384, 448,
1706 448, 512, 512, 640, 640, 768, 768, 896, 896, 1024, 1024, 1152,
1707 1152, 1280, 1280, };
1708 unsigned int atest=0;
1709 *framesize=20000; //if we do not find a start code do not decompress
1710 while (atest+4<size) {
1711 if (data[atest]==0x0B && data[atest+1]==0x77) {
1712 // now figure out the length of the frame
1713 unsigned char code=data[atest+4];
1714 unsigned char fscod=(code& 0xC0)>>6;
1715 unsigned char frmsize=(code &0x3f);
1716 if (fscod!=0) Log::getInstance()->log("Audio", Log::DEBUG, "warning we only support 48 KHz sampling rate");
1717 *framesize=frm_size_tab[frmsize]*2;
1718 return atest; // probably FrameSync
1725 unsigned int AudioOMX::AdvanceAacLatmAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1727 if (size<=4) return size; // silly;
1728 unsigned int atest=0;
1729 *framesize=20000; //if we do not find a start code do not decompress
1730 while (atest+4<size) {
1731 if (data[atest] ==0x56 && (data[atest+1]& 0xe0)==0xe0) {
1732 // now figure out the length of the frame
1733 unsigned int length= ((0x1f & data[atest+1])<<8) | data[atest+2];
1734 *framesize=length+3;
1735 return atest; // probably FrameSync
1743 void AudioOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1745 packet = mplist.front();
1748 UINT AudioOMX::DeliverMediaSample(UCHAR* buffer, UINT* samplepos) {
1749 DeliverMediaPacket(packet, buffer, samplepos);
1750 if (*samplepos == packet.length) {
1758 long long AudioOMX::correctAudioLatency(long long pts,int addsamples,int srate) {
1760 VideoOMX *video = (VideoOMX*) Video::getInstance();
1762 OMX_PARAM_U32TYPE audio_lat;
1763 OMX_ERRORTYPE error;
1764 memset(&audio_lat, 0, sizeof(audio_lat));
1765 audio_lat.nSize = sizeof(audio_lat);
1766 audio_lat.nVersion.nVersion = OMX_VERSION;
1767 audio_lat.nPortIndex = omx_rend_input_port;
1769 error = OMX_GetConfig(omx_aud_rend, OMX_IndexConfigAudioRenderingLatency,
1771 video->UnlockClock();
1772 if (error != OMX_ErrorNone) {
1773 Log::getInstance()->log("Audio", Log::DEBUG,
1774 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
1775 omx_rend_input_port);
1776 return pts; // no correction in case of error
1778 /*Log::getInstance()->log("Audio", Log::DEBUG, "Current audio latency %d",
1781 long long workpts=0;
1782 workpts+=addsamples;
1783 workpts-=audio_lat.nU32;
1784 workpts*=10LL*1000LL*1000LL;
1785 workpts=workpts/((long long)srate); //one second /samplerate
1791 bool AudioOMX::DrainTargetBufferFull()
1793 //Check, if we have OMX output buffers
1795 input_bufs_omx_mutex.lock();
1796 full=(input_bufs_omx_free.size()==0);
1797 input_bufs_omx_mutex.unlock();
1803 UINT AudioOMX::DeliverMediaPacket(MediaPacket mpacket, const UCHAR* buffer,
1805 /*First Check, if we have an audio sample*/
1806 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1808 OMX_ERRORTYPE error;
1809 //Log *logger=Log::getInstance();
1810 if (vw->InIframemode()) {
1813 return 0; //Not in iframe mode!
1816 if (!omx_running) return 0; // if we are not runnig do not do this
1817 if (vw->isClockPaused()) return 0; //Block if we pause
1818 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 1");
1820 //Log::getInstance()->log("Audio", Log::DEBUG, "DeliverMediaPacketOMX time %lld",mpacket.presentation_time);
1822 /* if (mpacket.synched && mpacket.presentation_time <= 0) {
1823 *samplepos = mpacket.length;
1824 firstsynched = false;
1826 Log::getInstance()->log("Audio", Log::DEBUG,
1827 "DeliverMediaPacketOMX Frameskip");
1828 return mpacket.length;
1831 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 2");
1833 UINT headerstrip = 0;
1834 if (mpacket.disconti) {
1835 firstsynched = false;
1836 decompress_buffer_filled=0;
1837 if (cur_input_buf_omx) {
1838 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1840 if (error != OMX_ErrorNone) {
1841 Log::getInstance()->log("Audio", Log::DEBUG,
1842 "OMX_EmptyThisBuffer 1 failed %x", error);
1844 cur_input_buf_omx = NULL;
1848 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 3");
1849 if (mpacket.type != lastAType) {//Format Change //Push data out !
1850 firstsynched = false;
1853 Log::getInstance()->log("Audio", Log::DEBUG,"Notice audio type change %d %d", mpacket.type,lastAType);
1854 lastAType = mpacket.type;
1855 decompress_buffer_filled=0;
1857 if (cur_input_buf_omx) {
1858 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1860 if (error != OMX_ErrorNone) {
1861 Log::getInstance()->log("Audio", Log::DEBUG,
1862 "OMX_EmptyThisBuffer 2 failed %x", error);
1864 cur_input_buf_omx = NULL;
1869 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
1870 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
1872 if (!ChangeAudioPortConfig(true)) {
1873 Log::getInstance()->log("Audio", Log::DEBUG,
1874 "Changing audio port config failed", error);
1877 pthread_setcancelstate(oldcancelstate, NULL);
1878 pthread_setcanceltype(oldcanceltype, NULL);
1882 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 4");
1884 /*Inspect PES-Header */
1885 if (*samplepos == 0 && mpacket.type != MPTYPE_MPEG_AUDIO_LAYER3) {//stripheader
1886 headerstrip = buffer[mpacket.pos_buffer + 8] + 9;
1887 if (mpacket.type == MPTYPE_AC3)
1888 headerstrip += 4; //skip ac3 bytes
1889 *samplepos += headerstrip;
1890 if (mpacket.synched) {
1891 if (cur_input_buf_omx) {
1892 //cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1893 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1895 if (error != OMX_ErrorNone) {
1896 Log::getInstance()->log("Audio", Log::DEBUG,
1897 "OMX_EmptyThisBuffer 3 failed %x", error);
1899 //vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1901 cur_input_buf_omx = NULL;//write out old data
1903 firstsynched = true;
1904 //decompress_buffer_filled=0;
1906 if (!firstsynched) {//
1907 *samplepos = mpacket.length;//if we have not processed at least one
1908 decompress_buffer_filled=0;
1909 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 5");
1910 return mpacket.length;//synched packet ignore it!
1914 if (!cur_input_buf_omx) {
1915 input_bufs_omx_mutex.lock();
1916 if (input_bufs_omx_free.size()==0) {
1917 input_bufs_omx_mutex.unlock();
1918 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 6");
1919 //Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample");
1920 return 0; // we do not have a free media sample
1923 cur_input_buf_omx=input_bufs_omx_free.front();
1924 cur_input_buf_omx->nFilledLen=0;
1925 cur_input_buf_omx->nOffset=0;
1926 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
1927 input_bufs_omx_free.pop_front();
1928 input_bufs_omx_mutex.unlock();
1930 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 7");
1932 if (cur_input_buf_omx->nFilledLen == 0) {//will only be changed on first packet
1933 if (mpacket.synched) {
1934 //Log::getInstance()->log("Audio", Log::DEBUG,
1935 // "packet synched marker");
1937 //lastreftimePTS=mpacket.pts;
1938 if (omx_first_frame) { // TODO time
1939 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
1940 Log::getInstance()->log("Audio", Log::DEBUG, "Starttime");
1941 omx_first_frame = false;
1943 cur_input_buf_omx->nFlags = 0;
1944 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
1946 lastreftimeOMX = mpacket.presentation_time;
1947 //Log::getInstance()->log("Audio", Log::DEBUG,
1948 // "Time code %lld pts %lld dts %lld", lastreftimeOMX, mpacket.pts,mpacket.dts);
1949 lastreftimePTS = mpacket.pts;
1950 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
1952 // Log::getInstance()->log("Audio", Log::DEBUG,
1953 // "packet NOT synched marker");
1954 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
1955 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
1958 if (mpacket.disconti || achange) {
1959 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_DISCONTINUITY;
1960 //mp23codec_context_libav->frame_size=-1;
1961 //ac3codec_context_libav->frame_size=-1;
1965 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 8");
1967 if (*samplepos>mpacket.length) *samplepos=0; //propably the thread got interrupted and sample is not valid any more!
1968 unsigned int haveToCopy=mpacket.length-*samplepos;
1971 while (haveToCopy>0) {
1972 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 9");
1974 unsigned int gotframesize=0;
1976 switch (mpacket.type) {
1977 case MPTYPE_MPEG_AUDIO:
1978 case MPTYPE_MPEG_AUDIO_LAYER3: {
1979 adv = AdvanceMpAudioSync(buffer+mpacket.pos_buffer+*samplepos,
1980 haveToCopy,&gotframesize);
1984 case MPTYPE_AC3_PRE13: {
1985 adv = AdvanceAc3AudioSync(buffer+mpacket.pos_buffer+*samplepos,
1986 haveToCopy,&gotframesize);
1990 case MPTYPE_AAC_LATM: {
1991 adv = AdvanceAacLatmAudioSync(buffer+mpacket.pos_buffer+*samplepos,
1992 haveToCopy,&gotframesize);
1996 if (adv != (int)haveToCopy) {
2001 *samplepos=mpacket.length; //no ac3 sync byte
2002 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 10");
2003 return mpacket.length;
2006 // so everything is fine now do a memcpy
2007 int cancopy=min(haveToCopy,cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen);
2008 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+mpacket.pos_buffer+*samplepos,cancopy);
2009 haveToCopy-=cancopy;
2010 cur_input_buf_omx->nFilledLen+=cancopy;
2011 *samplepos+=cancopy;
2012 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 11");
2014 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2015 if (error != OMX_ErrorNone) {
2016 Log::getInstance()->log("Audio", Log::DEBUG,
2017 "OMX_EmptyThisBuffer 5 failed %x", error);
2019 cur_input_buf_omx=NULL;
2022 input_bufs_omx_mutex.lock();
2023 if (input_bufs_omx_free.size()==0) {
2024 input_bufs_omx_mutex.unlock();
2025 // Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample2");
2026 return *samplepos; // we do not have a free media sample
2028 cur_input_buf_omx=input_bufs_omx_free.front();
2029 cur_input_buf_omx->nFilledLen=0;
2030 cur_input_buf_omx->nOffset=0;
2031 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2032 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
2033 input_bufs_omx_free.pop_front();
2034 input_bufs_omx_mutex.unlock();
2036 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 12");
2039 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 13");
2047 AVCodecContext *current_context;
2048 switch (mpacket.type) {
2049 case MPTYPE_MPEG_AUDIO:
2050 case MPTYPE_MPEG_AUDIO_LAYER3: {
2051 current_context = mp23codec_context_libav;
2052 if (current_context->frame_size<0) framesize=1152; //Maximum framesize
2053 else framesize=current_context->frame_size;
2055 case MPTYPE_AAC_LATM: {
2056 current_context = aaclatmcodec_context_libav;
2059 case MPTYPE_AC3_PRE13: {
2060 current_context = ac3codec_context_libav;
2064 if (decompress_buffer_filled) { // have a remaining paket
2065 incoming_paket_libav.data =(uint8_t*) decompress_buffer;
2066 memcpy(decompress_buffer+decompress_buffer_filled,
2067 buffer+mpacket.pos_buffer+*samplepos,
2068 min(haveToCopy,decompress_buffer_size-decompress_buffer_filled));
2069 incoming_paket_libav.size = decompress_buffer_filled
2070 +min(haveToCopy,decompress_buffer_size-decompress_buffer_filled);
2071 //Log::getInstance()->log("Audio", Log::DEBUG,"Use saved audio buffer %d %d %d",mpacket.type,decompress_buffer_filled,mpacket.synched);
2073 incoming_paket_libav.data =(uint8_t*) buffer+mpacket.pos_buffer+*samplepos;
2074 incoming_paket_libav.size = haveToCopy;
2076 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 18");
2078 while (haveToCopy> 0 && errcount<3) {
2080 //Log::getInstance()->log("Audio", Log::DEBUG,"libav in %d %d",framesize,current_context->frame_size);
2081 //Log::getInstance()->log("Audio", Log::DEBUG, "libav in %d %d",
2082 // framesize, current_context->frame_size);
2084 bool donotdecompress=false;
2085 unsigned int gotframesize=0;
2086 // if (!decompress_buffer_filled) { // only do this if no old data is present
2088 switch (mpacket.type) {
2089 case MPTYPE_MPEG_AUDIO:
2090 case MPTYPE_MPEG_AUDIO_LAYER3: {
2091 adv = AdvanceMpAudioSync(incoming_paket_libav.data,
2092 incoming_paket_libav.size,&gotframesize);
2096 case MPTYPE_AC3_PRE13: {
2097 adv = AdvanceAc3AudioSync(incoming_paket_libav.data,
2098 incoming_paket_libav.size,&gotframesize);
2101 case MPTYPE_AAC_LATM: {
2102 adv = AdvanceAacLatmAudioSync(incoming_paket_libav.data,
2103 incoming_paket_libav.size,&gotframesize);
2107 if (adv > 0 /*&& !decompress_buffer_filled*/) {
2108 incoming_paket_libav.data += adv;
2109 incoming_paket_libav.size-=adv;
2112 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2113 else*/ framesize=haveToCopy;
2114 //Log::getInstance()->log("Audio", Log::DEBUG,"Advance by %d %d from %d",adv,mpacket.type,*samplepos );
2115 if (haveToCopy <= 0) {
2116 // Log::getInstance()->log("Audio", Log::DEBUG,"No sync code in packet remove %d",mpacket.type);
2117 *samplepos=mpacket.length;
2118 return mpacket.length;
2124 if (gotframesize>0 && gotframesize>haveToCopy) {
2125 donotdecompress=true;
2126 errcount=100; // exit loop
2128 // else Log::getInstance()->log("Audio", Log::DEBUG,"Loop run" );
2130 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 19");
2131 if (!donotdecompress) {
2134 pthread_testcancel();
2135 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2136 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2138 if (!omx_running || !mp23codec_context_libav
2139 || !ac3codec_context_libav) {
2140 libav_mutex.unlock();
2143 libav_mutex.unlock();
2144 // Log::getInstance()->log("Audio", Log::DEBUG,"libav out");
2145 int save_size=incoming_paket_libav.size;
2147 if ((int)gotframesize <= incoming_paket_libav.size) {
2148 if (gotframesize>0) incoming_paket_libav.size=gotframesize;
2149 len = avcodec_decode_audio4(current_context, decode_frame_libav,
2150 &gotta, &incoming_paket_libav);
2152 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:E %d %d",gotframesize,incoming_paket_libav.size);
2156 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:T %d",len);
2157 incoming_paket_libav.size=save_size;
2158 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out1");
2159 pthread_setcancelstate(oldcancelstate, NULL);
2160 pthread_setcanceltype(oldcanceltype, NULL);
2161 pthread_testcancel();
2167 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out2");
2168 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 20");
2170 libav_mutex.unlock();
2174 if (decompress_buffer_filled) { // reset to normal decoding
2176 //Log::getInstance()->log("Audio", Log::DEBUG,"saved audio: %d",len);
2177 haveToCopy -= min(len-decompress_buffer_filled,0);
2178 *samplepos += min(len-decompress_buffer_filled,0);
2179 //if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2180 /*else*/ framesize=haveToCopy;
2182 framesize=haveToCopy;
2184 incoming_paket_libav.data =(uint8_t*) buffer+mpacket.pos_buffer+*samplepos;
2186 decompress_buffer_filled=0;
2190 incoming_paket_libav.data += len;
2194 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2195 else*/framesize=haveToCopy;
2198 framesize=haveToCopy;
2201 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 21");
2203 incoming_paket_libav.size =framesize;
2205 //Log::getInstance()->log("Audio", Log::DEBUG,
2208 int dsize = av_samples_get_buffer_size(NULL,
2209 /*current_context->channels*/2, decode_frame_libav->nb_samples,
2210 AV_SAMPLE_FMT_S16, 1);
2211 /* int dsize_in = av_samples_get_buffer_size(NULL,
2212 current_context->channels, decode_frame_libav->nb_samples,
2213 current_context->sample_fmt, 1); -- unused */
2214 //if (current_context->channels==1) dsize*=2; // we convert mono to stereo
2215 if ((cur_input_buf_omx->nFilledLen + dsize)
2216 > cur_input_buf_omx->nAllocLen ) {
2217 // I doubt that this will ever happen
2218 // Log::getInstance()->log("Audio", Log::DEBUG,
2219 // "P 2 Time code %lld pts %lld", lastreftimeOMX, mpacket.pts);
2220 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
2222 if (error != OMX_ErrorNone) {
2223 Log::getInstance()->log("Audio", Log::DEBUG,
2224 "OMX_EmptyThisBuffer 4 failed %x", error);
2226 cur_input_buf_omx = NULL;
2228 if (!cur_input_buf_omx) {
2230 while (count < 10 && omx_running) {
2232 input_bufs_omx_mutex.lock();
2233 if (input_bufs_omx_free.size() == 0) {
2234 input_bufs_omx_mutex.unlock();
2235 // Log::getInstance()->log("Audio", Log::DEBUG,
2236 // "Deliver MediaPacket no free sample");
2238 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark22");
2239 if (!omx_running) return *samplepos;
2240 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 23");
2243 cur_input_buf_omx = input_bufs_omx_free.front();
2244 cur_input_buf_omx->nFilledLen = 0;
2245 cur_input_buf_omx->nOffset = 0;
2246 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
2247 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2248 input_bufs_omx_free.pop_front();
2249 input_bufs_omx_mutex.unlock();
2252 if (!cur_input_buf_omx) return *samplepos;
2257 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy in %d %d %d" ,dsize,current_context->sample_rate,cur_input_buf_omx->nFilledLen);
2260 av_opt_set_int(resam_con_libav, "in_sample_rate",decode_frame_libav->sample_rate,0);
2261 av_opt_set_int(resam_con_libav, "in_sample_fmt",decode_frame_libav->format,0);
2262 av_opt_set_int(resam_con_libav, "in_channel_layout",decode_frame_libav->channel_layout, 0);
2263 //Log::getInstance()->log("Audio", Log::ERR,"AV resampledata %d %d %d %d",current_context->channels,current_context->sample_rate,current_context->sample_fmt,current_context->channel_layout);
2264 //Log::getInstance()->log("Audio", Log::ERR,"AV resampledata2 %d %d %d",decode_frame_libav->sample_rate,decode_frame_libav->format,decode_frame_libav->channel_layout);
2266 int ret=avresample_open(resam_con_libav);
2268 Log::getInstance()->log("Audio", Log::ERR,"Opening AV resample failed %d",ret);
2270 uint8_t *output=cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen;
2272 avresample_convert(resam_con_libav, &output,
2273 dsize, decode_frame_libav->nb_samples,
2274 decode_frame_libav->extended_data, decode_frame_libav->linesize[0], decode_frame_libav->nb_samples);
2275 avresample_close(resam_con_libav);
2278 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy out");
2279 cur_input_buf_omx->nFilledLen += dsize;
2281 //Log::getInstance()->log("Audio", Log::DEBUG,"Incomplete mpeg frames in pes packet %d %d",incoming_paket_libav.size,mpacket.length);
2282 /* uint8_t a1=incoming_paket_libav.data[0];
2283 uint8_t a2=incoming_paket_libav.data[1];
2284 uint8_t a3=incoming_paket_libav.data[2];
2285 uint8_t a4=incoming_paket_libav.data[3];*/
2286 // Log::getInstance()->log("Audio", Log::DEBUG,"Header %x %x %x %x",a1,a2,
2291 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 24");
2292 decompress_buffer_filled=0;
2294 //Log::getInstance()->log("Audio", Log::DEBUG,"We can not decompress %d save for later %d %x %x",haveToCopy,mpacket.type,incoming_paket_libav.data,mpacket.pos_buffer);
2295 memcpy(decompress_buffer,incoming_paket_libav.data,min(haveToCopy,decompress_buffer_size));
2297 decompress_buffer_filled=min(haveToCopy,decompress_buffer_size);
2301 if (cur_input_buf_omx->nFilledLen) {
2302 //Log::getInstance()->log("Audio", Log::DEBUG,
2303 // "P 3 Time code %lld pts %lld", lastreftimeOMX, mpacket.pts);
2304 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2305 if (error != OMX_ErrorNone) {
2306 Log::getInstance()->log("Audio", Log::DEBUG,
2307 "OMX_EmptyThisBuffer 5 failed %x", error);
2309 //if (mpacket.synched) vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
2310 cur_input_buf_omx = NULL;
2315 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 25");
2317 *samplepos=mpacket.length;
2318 return mpacket.length;
2322 long long AudioOMX::SetStartOffset(long long curreftime, bool *rsync)
2324 VideoOMX* vw = dynamic_cast<VideoOMX*>(Video::getInstance());
2325 return vw->SetStartAudioOffset(curreftime,rsync);
2328 void AudioOMX::ResetTimeOffsets()
2330 VideoOMX* vw = dynamic_cast<VideoOMX*>(Video::getInstance());
2331 vw->ResetTimeOffsets();