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/>.
25 #include "woptionpane.h"
26 #include "osdopenvg.h"
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/opt.h"
42 lastAType = MPTYPE_MPEG_AUDIO;
48 canpass_pcm_mch=false;
50 prefered_ac3=0; //0 stereo PCM, 1 passthrough 2 Multichannel PCM
58 omx_aud_rend/*dec*/=0;
59 cur_input_buf_omx=NULL;
62 ac3codec_context_libav=NULL;
65 mp23codec_context_libav=NULL;
71 decompress_buffer=NULL;
72 decompress_buffer_size=0;
73 decompress_buffer_filled=0;
77 strcpy(L_VPE_OMX_AUDIO_REND, VPE_OMX_AUDIO_REND);
84 int AudioOMX::init(UCHAR tstreamType) {
89 streamType = tstreamType;
91 if (!initAllParams()) {
98 decompress_buffer_size=20000;
99 decompress_buffer=static_cast<UCHAR*>(malloc(decompress_buffer_size));
100 decompress_buffer_filled=0;
102 #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100)
106 av_log_set_flags(AV_LOG_SKIP_REPEATED);
108 ac3codec_libav = avcodec_find_decoder(AV_CODEC_ID_AC3);
109 if (ac3codec_libav == NULL) {
110 Log::getInstance()->log("Audio", Log::DEBUG,
111 "Find libav ac3 decoder failed");
115 mp23codec_libav = avcodec_find_decoder(AV_CODEC_ID_MP3);
116 if (mp23codec_libav == NULL) {
117 Log::getInstance()->log("Audio", Log::DEBUG,
118 "Find libav mpeg audio decoder failed");
122 aaclatmcodec_libav = avcodec_find_decoder(AV_CODEC_ID_AAC_LATM);
123 if (aaclatmcodec_libav == NULL) {
124 Log::getInstance()->log("Audio", Log::DEBUG,
125 "Find libav aac latm decoder failed");
131 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eMPEG1,2,EDID_AudioSampleRate_e48KHz,0);
135 Log::getInstance()->log("Audio", Log::NOTICE,
136 "TV hdmi supports mpeg1 layer 1 and 2");
138 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eMP3,2,EDID_AudioSampleRate_e48KHz,0);
142 Log::getInstance()->log("Audio", Log::NOTICE,
143 "TV hdmi supports mpeg1 layer 3");
146 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eAC3,6,EDID_AudioSampleRate_e48KHz,0);
149 Log::getInstance()->log("Audio", Log::NOTICE,
150 "TV hdmi supports AC3");
152 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eAAC,6,EDID_AudioSampleRate_e48KHz,0);
156 Log::getInstance()->log("Audio", Log::NOTICE,
157 "TV hdmi supports AAC");
160 canpass_pcm_mch=false;
165 int AudioOMX::initAllParams()
167 return (setStreamType(streamType) && setChannel() && setSource());
170 int AudioOMX::shutdown()
172 if (!initted) return 0;
175 Log::getInstance()->log("Audio", Log::DEBUG, "audio shutdown called");
176 DeAllocateCodecsOMX();
178 free(decompress_buffer);
179 decompress_buffer=NULL;
180 decompress_buffer_size=0;
181 decompress_buffer_filled=0;
186 bool AudioOMX::loadOptionsFromServer(VDR* vdr)
188 Log::getInstance()->log("Audio", Log::DEBUG, "AudioOMX config load");
189 char *name=vdr->configLoad("AudioOMX","AC3DecodingMode");
192 if (STRCASECMP(name, "PCM") == 0) {
194 } else if (STRCASECMP(name, "Passthrough") == 0) {
196 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
202 name = vdr->configLoad("AudioOMX", "Mp2DecodingMode");
205 if (STRCASECMP(name, "PCM") == 0) {
207 } else if (STRCASECMP(name, "Passthrough") == 0) {
209 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
215 name = vdr->configLoad("AudioOMX", "AACDecodingMode");
218 if (STRCASECMP(name, "PCM") == 0) {
220 } else if (STRCASECMP(name, "Passthrough") == 0) {
222 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
228 name = vdr->configLoad("AudioOMX", "Mp3DecodingMode");
231 if (STRCASECMP(name, "PCM") == 0) {
233 } else if (STRCASECMP(name, "Passthrough") == 0) {
235 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
241 name = vdr->configLoad("AudioOMX", "AudioOutput");
244 if (STRCASECMP(name, "analog") == 0) {
246 } else if (STRCASECMP(name, "HDMI") == 0) {
257 bool AudioOMX::handleOptionChanges(Option* option)
259 if (Audio::handleOptionChanges(option))
261 switch (option->id) {
263 if (STRCASECMP(option->options[option->userSetChoice], "analog") == 0) {
265 } else if (STRCASECMP(option->options[option->userSetChoice], "HDMI")
273 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
275 } else if (STRCASECMP(option->options[option->userSetChoice],
276 "Passthrough") == 0) {
278 } else if (STRCASECMP(option->options[option->userSetChoice],
279 "PCMMultichannel") == 0) {
285 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
287 } else if (STRCASECMP(option->options[option->userSetChoice],
288 "Passthrough") == 0) {
290 } else if (STRCASECMP(option->options[option->userSetChoice],
291 "PCMMultichannel") == 0) {
297 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
299 } else if (STRCASECMP(option->options[option->userSetChoice],
300 "Passthrough") == 0) {
302 } else if (STRCASECMP(option->options[option->userSetChoice],
303 "PCMMultichannel") == 0) {
309 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
311 } else if (STRCASECMP(option->options[option->userSetChoice],
312 "Passthrough") == 0) {
314 } else if (STRCASECMP(option->options[option->userSetChoice],
315 "PCMMultichannel") == 0) {
325 bool AudioOMX::saveOptionstoServer()
328 switch (prefered_ac3) {
330 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode", "PCM");
333 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
337 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
342 switch (prefered_aac) {
344 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode", "PCM");
347 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
351 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
356 switch (prefered_mp2) {
358 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode", "PCM");
361 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
365 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
370 switch (prefered_mp3) {
372 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode", "PCM");
375 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
379 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
385 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "analog");
387 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "HDMI");
393 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
394 UINT numChoices, UINT defaultChoice, UINT startInt,
395 const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
397 bool AudioOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
399 if (!Audio::addOptionsToPanes(panenumber,options,pane)) return false;
406 static const char* audioopts[]={"analog","HDMI"};
407 option = new Option(4,tr("Audio Output"), "AudioOMX","AudioOutput",Option::TYPE_TEXT,2,0,0,audioopts,NULL,false,this);
408 options->push_back(option);
409 pane->addOptionLine(option);
412 char **ac3opts=new char *[3];
414 ac3opts[i]=new char[strlen("PCM")+1];
415 strcpy(ac3opts[i],"PCM");
418 ac3opts[i]=new char[strlen("Passthrough")+1];
419 strcpy(ac3opts[i],"PassThrough");
422 if (canpass_pcm_mch) {
423 ac3opts[i]=new char[strlen("PCMMultichannel")+1];
424 strcpy(ac3opts[i],"PCMMultichannel");
427 option = new Option(1 ,tr("AC3 HDMI Mode"), "AudioOMX", "AC3DecodingMode", Option::TYPE_TEXT, i, 0, 0, ac3opts,NULL,true, this);
428 options->push_back(option);
429 pane->addOptionLine(option);
431 /* char **aacopts = new char *[3];
433 aacopts[i] = new char[strlen("PCM") + 1];
434 strcpy(mp2opts[i], "PCM");
437 aacopts[i] = new char[strlen("Passthrough") + 1];
438 strcpy(aacopts[i], "PassThrough");
441 if (canpass_pcm_mch) {
442 aacopts[i] = new char[strlen("PCMMultichannel") + 1];
443 strcpy(aacopts[i], "PCMMultichannel");
446 option = new Option(5, tr("Mp2 HDMI Mode"), "AudioOMX",
447 "AACDecodingMode", Option::TYPE_TEXT, i, 0, 0,
448 aacopts, NULL, true, this);
449 options->push_back(option);
450 pane->addOptionLine(option);
453 char **mp2opts = new char *[3];
455 mp2opts[i] = new char[strlen("PCM") + 1];
456 strcpy(mp2opts[i], "PCM");
459 mp2opts[i] = new char[strlen("Passthrough") + 1];
460 strcpy(mp2opts[i], "PassThrough");
463 if (canpass_pcm_mch) {
464 mp2opts[i] = new char[strlen("PCMMultichannel") + 1];
465 strcpy(mp2opts[i], "PCMMultichannel");
468 option = new Option(2, tr("Mp2 HDMI Mode"), "AudioOMX",
469 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0,
470 mp2opts, NULL, true, this);
471 options->push_back(option);
472 pane->addOptionLine(option);
474 char **mp3opts = new char *[3];
476 mp3opts[i] = new char[strlen("PCM") + 1];
477 strcpy(mp3opts[i], "PCM");
480 mp3opts[i] = new char[strlen("Passthrough") + 1];
481 strcpy(mp3opts[i], "PassThrough");
484 if (canpass_pcm_mch) {
485 mp3opts[i] = new char[strlen("PCMMultichannel") + 1];
486 strcpy(mp3opts[i], "PCMMultichannel");
489 option = new Option(3, tr("Mp3 HDMI Mode"), "AudioOMX",
490 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0, mp3opts,
492 options->push_back(option);
493 pane->addOptionLine(option);*/
494 // Comment unsupported modes out
503 OMX_ERRORTYPE AudioOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE /* hcomp */, OMX_IN OMX_PTR /* appdata */, OMX_IN OMX_BUFFERHEADERTYPE* buffer)
505 //Log::getInstance()->log("Audio", Log::NOTICE, "EmptyBufferDone");
506 AudioOMX* audio = static_cast<AudioOMX*>(getInstance());
507 audio->ReturnEmptyOMXBuffer(buffer);
508 return OMX_ErrorNone;
511 void AudioOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer)
513 input_bufs_omx_mutex.lock();
514 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
515 input_bufs_omx_free.push_back(buffer);
516 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
517 input_bufs_omx_mutex.unlock();
518 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
522 OMX_ERRORTYPE AudioOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE /* hcomp */, OMX_IN OMX_PTR /* appdata */, OMX_IN OMX_BUFFERHEADERTYPE* /* buffer */)
524 Log::getInstance()->log("Audio", Log::NOTICE, "FillBufferDone");
525 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
527 return OMX_ErrorNone;
530 int AudioOMX::setStreamType(UCHAR /* type */)
532 if (!initted) return 0;
534 // if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
538 int AudioOMX::setChannel()
540 if (!initted) return 0;
542 // if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
546 int AudioOMX::setSource()
548 if (!initted) return 0;
550 // if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
556 if (!initted) return 0;
558 // if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
562 int AudioOMX::play() {
565 lastAType=MPTYPE_MPEG_AUDIO;
566 Log::getInstance()->log("Audio", Log::DEBUG, "enter play");
568 static_cast<VideoOMX*>(Video::getInstance())->interlaceSwitch4Demux(); // switch resolution if necessary
570 if (!AllocateCodecsOMX()) {
577 int AudioOMX::ChangeAudioDestination() //clock aka omx mutex needs to be locked
580 const char * destinations[]={"local","hdmi"};
585 OMX_CONFIG_BRCMAUDIODESTINATIONTYPE auddest;
586 memset(&auddest,0,sizeof(auddest));
587 auddest.nSize=sizeof(auddest);
588 auddest.nVersion.nVersion=OMX_VERSION;
589 strcpy((char *)auddest.sName, destinations[dest]);
591 Log::getInstance()->log("Audio", Log::DEBUG, "setting destination to: %s",auddest.sName);
592 error=OMX_SetConfig(omx_aud_rend,OMX_IndexConfigBrcmAudioDestination,&auddest);
593 if (error!=OMX_ErrorNone){
594 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX_IndexConfigBrcmAudioDestination failed %x %x %s", error,omx_aud_rend,auddest.sName);
595 DeAllocateCodecsOMX();
603 int AudioOMX::ChangeAudioPortConfig(bool disport) //clock aka omx mutex needs to be locked
606 //Ok first fidle a working configuration
607 Log::getInstance()->log("Audio", Log::DEBUG,
608 "ChangeAudioPortConfig");
610 OMX_AUDIO_CODINGTYPE encoding;
613 case MPTYPE_MPEG_AUDIO: {
614 if (prefered_mp2 == 2 && false) { //not supported yet
617 if (prefered_mp2 == 1 && canpass_mp2) {
619 encoding=OMX_AUDIO_CodingMP3;
622 encoding=OMX_AUDIO_CodingPCM;
627 case MPTYPE_AAC_LATM: {
628 if (prefered_aac == 2 && false) { //not supported yet
631 Log::getInstance()->log("Audio", Log::DEBUG,
632 "ChangeAudioPortConfig debug %d %d",prefered_aac,canpass_aac);
633 if (prefered_aac == 1 && canpass_aac) {
635 encoding=OMX_AUDIO_CodingAAC;
638 encoding=OMX_AUDIO_CodingPCM;
643 case MPTYPE_AC3_PRE13:
645 if (prefered_ac3 == 2 && false) { //not supported yet
648 Log::getInstance()->log("Audio", Log::DEBUG,
649 "ChangeAudioPortConfig debug %d %d",prefered_ac3,canpass_ac3);
650 if (prefered_ac3 == 1 && canpass_ac3) {
652 encoding=OMX_AUDIO_CodingDDP;
655 encoding=OMX_AUDIO_CodingPCM;
660 case MPTYPE_MPEG_AUDIO_LAYER3: {
661 if (prefered_mp3 == 2 && false) { //not supported yet
664 if (prefered_mp3 == 1 && canpass_mp2) {
666 encoding=OMX_AUDIO_CodingMP3;
669 encoding=OMX_AUDIO_CodingPCM;
677 encoding=OMX_AUDIO_CodingPCM;
678 //mch=false; // multichannel also false
683 /*OMX_CONFIG_BOOLEANTYPE booly;
684 memset(&booly, 0, sizeof(booly));
685 booly.nSize = sizeof(booly);
686 booly.nVersion.nVersion = OMX_VERSION;
688 booly.bEnabled = OMX_TRUE;
690 booly.bEnabled = OMX_FALSE;
692 error = OMX_SetParameter(omx_aud_dec, OMX_IndexParamBrcmDecoderPassThrough,
694 if (error != OMX_ErrorNone) {
695 Log::getInstance()->log("Audio", Log::DEBUG,
696 "Init OMX_IndexParamBrcmDecoderPassThrough failed %x", error);
697 DeAllocateCodecsOMX();
700 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
702 video->DisablePort(omx_aud_rend,omx_rend_input_port,false);
703 //DestroyInputBufsOMXwhilePlaying();
704 //video->CommandFinished(omx_aud_rend,OMX_CommandPortDisable,omx_rend_input_port);
709 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
710 memset(&format, 0, sizeof(format));
711 format.nSize = sizeof(format);
712 format.nVersion.nVersion = OMX_VERSION;
713 format.nPortIndex = omx_rend_input_port;
714 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
716 if (error != OMX_ErrorNone) {
717 Log::getInstance()->log("Audio", Log::DEBUG,
718 "Get OMX_IndexParamAudioPortFormat failed %x %d", error,
719 omx_rend_input_port);
724 Log::getInstance()->log("Audio", Log::DEBUG,
725 "Get OMX_IndexParamAudioPortFormat returned %d",format.eEncoding );
726 format.eEncoding = encoding;
728 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
730 if (error != OMX_ErrorNone) {
731 Log::getInstance()->log("Audio", Log::DEBUG,
732 "Set OMX_IndexParamAudioPortFormat failed %x %d %d", error,
733 omx_rend_input_port,format.eEncoding );
738 case OMX_AUDIO_CodingPCM: {
739 OMX_AUDIO_PARAM_PCMMODETYPE audio_pcm;
740 memset(&audio_pcm, 0, sizeof(audio_pcm));
741 audio_pcm.nSize = sizeof(audio_pcm);
742 audio_pcm.nVersion.nVersion = OMX_VERSION;
743 audio_pcm.nChannels = 2;
744 audio_pcm.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
745 audio_pcm.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
746 //audio_pcm.eChannelMapping[2]=OMX_AUDIO_ChannelMax;
747 audio_pcm.eNumData = OMX_NumericalDataSigned;
748 audio_pcm.eEndian = OMX_EndianLittle;
749 audio_pcm.bInterleaved = OMX_TRUE;
750 audio_pcm.nBitPerSample = 16;
751 audio_pcm.ePCMMode = OMX_AUDIO_PCMModeLinear;
752 audio_pcm.nChannels = 2;
753 audio_pcm.nSamplingRate = 48000;
754 audio_pcm.nPortIndex = omx_rend_input_port;
755 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPcm,
757 if (error != OMX_ErrorNone) {
758 Log::getInstance()->log("Audio", Log::DEBUG,
759 "Init OMX_IndexParamAudioPcm failed %x %d", error,
760 omx_rend_input_port);
764 case OMX_AUDIO_CodingDDP: {
765 OMX_AUDIO_PARAM_DDPTYPE audio_ddp;
766 memset(&audio_ddp, 0, sizeof(audio_ddp));
767 audio_ddp.nSize = sizeof(audio_ddp);
768 audio_ddp.nVersion.nVersion = OMX_VERSION;
769 audio_ddp.nPortIndex = omx_rend_input_port;
770 audio_ddp.nChannels = 8; //unknown
771 audio_ddp.nBitRate=0;
772 audio_ddp.nSampleRate=48000;
773 audio_ddp.eChannelMapping[0] =OMX_AUDIO_ChannelLF;
774 audio_ddp.eChannelMapping[1] =OMX_AUDIO_ChannelRF;
775 audio_ddp.eChannelMapping[2] =OMX_AUDIO_ChannelCF;
776 audio_ddp.eChannelMapping[3] =OMX_AUDIO_ChannelLFE;
777 audio_ddp.eChannelMapping[4] =OMX_AUDIO_ChannelLR;
778 audio_ddp.eChannelMapping[5] =OMX_AUDIO_ChannelRR;
779 audio_ddp.eChannelMapping[6] =OMX_AUDIO_ChannelLS;
780 audio_ddp.eChannelMapping[7] =OMX_AUDIO_ChannelRS;
781 audio_ddp.eChannelMapping[8] =OMX_AUDIO_ChannelCS;
782 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioDdp,
784 if (error != OMX_ErrorNone) {
785 Log::getInstance()->log("Audio", Log::DEBUG,
786 "Init OMX_IndexParamAudioDdp failed %x %d", error,
787 omx_rend_input_port);
792 default: break; //Make compiler happy
799 //PrepareInputBufsOMX(false);
800 video->EnablePort(omx_aud_rend,omx_rend_input_port,false);
810 int AudioOMX::InitDecoderLibAV()
813 ac3codec_context_libav = avcodec_alloc_context3(ac3codec_libav);
814 if (!ac3codec_context_libav) {
815 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for ac3 decoding context failed!");
819 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 34, 1)
820 ac3codec_context_libav->flags |= AV_CODEC_FLAG_TRUNCATED;
822 ac3codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
825 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
826 ac3codec_context_libav->request_channel_layout=2;
828 ac3codec_context_libav->request_channels=2;
831 int avc_ret = avcodec_open2(ac3codec_context_libav, ac3codec_libav, NULL);
833 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
834 libav_mutex.unlock();
838 aaclatmcodec_context_libav = avcodec_alloc_context3(aaclatmcodec_libav);
839 if (!aaclatmcodec_context_libav) {
840 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for aac decoding context failed!");
844 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 34, 1)
845 aaclatmcodec_context_libav->flags |= AV_CODEC_FLAG_TRUNCATED;
847 aaclatmcodec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
850 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
851 aaclatmcodec_context_libav->request_channel_layout=2;
853 aaclatmcodec_context_libav->request_channels=2;
856 avc_ret = avcodec_open2(aaclatmcodec_context_libav, aaclatmcodec_libav, NULL);
858 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
859 libav_mutex.unlock();
864 mp23codec_context_libav = avcodec_alloc_context3(mp23codec_libav);
865 if (!ac3codec_context_libav) {
866 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for mp23 decoding context failed!");
867 libav_mutex.unlock();
871 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 34, 1)
872 mp23codec_context_libav->flags |= AV_CODEC_FLAG_TRUNCATED;
874 mp23codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
877 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
878 mp23codec_context_libav->request_channel_layout=2;
880 mp23codec_context_libav->request_channels=2;
883 avc_ret = avcodec_open2(mp23codec_context_libav, mp23codec_libav, NULL);
885 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
886 libav_mutex.unlock();
890 resam_con_libav = swr_alloc();
891 if (resam_con_libav == NULL) {
892 Log::getInstance()->log("Audio", Log::DEBUG,
893 "Alloc resample context failed");
897 av_opt_set_int(resam_con_libav, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0); // our standard format
898 av_opt_set_int(resam_con_libav, "out_sample_rate",48000,0);
899 av_opt_set_int(resam_con_libav, "out_sample_fmt",AV_SAMPLE_FMT_S16,0);
900 av_opt_set_int(resam_con_libav, "matrix_encoding",AV_MATRIX_ENCODING_DPLII,0);
902 av_opt_set_int(resam_con_libav, "in_sample_rate",48000,0);
903 av_opt_set_int(resam_con_libav, "in_sample_fmt",AV_SAMPLE_FMT_S16,0);
904 av_opt_set_int(resam_con_libav, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0); //just an example
907 av_init_packet(&incoming_paket_libav);
908 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
909 decode_frame_libav=av_frame_alloc();
911 decode_frame_libav=avcodec_alloc_frame();
913 libav_mutex.unlock();
914 decompress_buffer_filled=0;
921 void AudioOMX::DeinitDecoderLibAV() {
925 if (ac3codec_context_libav) {
926 avcodec_close(ac3codec_context_libav);
927 av_free(ac3codec_context_libav);
928 ac3codec_context_libav = NULL;
930 avcodec_close(aaclatmcodec_context_libav);
931 av_free(aaclatmcodec_context_libav);
932 aaclatmcodec_context_libav = NULL;
934 av_free(decode_frame_libav);
936 avcodec_close(mp23codec_context_libav);
937 av_free(mp23codec_context_libav);
938 mp23codec_context_libav = NULL;
940 swr_free(&resam_con_libav);
941 resam_con_libav=NULL;
945 libav_mutex.unlock();
950 int AudioOMX::AllocateCodecsOMX()
953 static OMX_CALLBACKTYPE callbacks= {&VideoOMX::EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
955 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX");
956 //Clock, move later to audio
957 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
959 if (!InitDecoderLibAV()) return 0;;
962 OMX_PORT_PARAM_TYPE p_param;
963 memset(&p_param,0,sizeof(p_param));
964 p_param.nSize=sizeof(p_param);
965 p_param.nVersion.nVersion=OMX_VERSION;
968 if (!video->getClockAudioandInit(&omx_clock,&omx_clock_output_port)){
969 return 0;// get the clock and init it if necessary
973 if (!video->idleClock()) {
978 error = OMX_GetHandle(&omx_aud_rend, L_VPE_OMX_AUDIO_REND, NULL, &callbacks);
979 if (error != OMX_ErrorNone) {
980 Log::getInstance()->log("Audio", Log::DEBUG,
981 "Init OMX audio rend failed %x", error);
982 video->UnlockClock();
983 DeAllocateCodecsOMX();
987 if (!ChangeAudioDestination()) {
988 video->UnlockClock();
989 DeAllocateCodecsOMX();
993 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioInit, &p_param);
994 if (error != OMX_ErrorNone) {
995 Log::getInstance()->log("Audio", Log::DEBUG,
996 "Init OMX audio rend OMX_GetParameter failed %x", error);
997 video->UnlockClock();
998 DeAllocateCodecsOMX();
1001 omx_rend_input_port = p_param.nStartPortNumber;
1007 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamOtherInit, &p_param);
1008 if (error != OMX_ErrorNone) {
1009 Log::getInstance()->log("Audio", Log::DEBUG,
1010 "Init OMX aud rend OMX_GetParameter failed %x", error);
1011 video->UnlockClock();
1012 DeAllocateCodecsOMX();
1015 // buggy return value
1016 omx_rend_clock_port = p_param.nStartPortNumber;
1021 /* error=OMX_GetHandle(&omx_aud_dec,VPE_OMX_AUDIO_DECODER,NULL,&callbacks);
1023 if (error!=OMX_ErrorNone){
1024 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder failed %x", error);
1025 video->UnlockClock();
1026 DeAllocateCodecsOMX();
1030 error=OMX_GetParameter(omx_aud_dec,OMX_IndexParamAudioInit,&p_param);
1031 if (error!=OMX_ErrorNone){
1032 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder OMX_GetParameter failed %x", error);
1033 video->UnlockClock();
1034 DeAllocateCodecsOMX();
1037 omx_codec_input_port=p_param.nStartPortNumber;
1038 omx_codec_output_port=p_param.nStartPortNumber+1;
1040 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port) || !video->DisablePort(omx_aud_dec,omx_codec_output_port)) {
1041 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio decoder failed");
1042 video->UnlockClock();
1043 DeAllocateCodecsOMX();
1050 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true) ) {
1051 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio rend failed %d",omx_rend_input_port);
1052 video->UnlockClock();
1053 DeAllocateCodecsOMX();
1057 if ( !video->DisablePort(omx_aud_rend, omx_rend_clock_port, true)) {
1058 Log::getInstance()->log("Audio", Log::DEBUG,
1059 "Disable Ports OMX rend clock port failed %d",omx_rend_clock_port);
1060 video->UnlockClock();
1061 DeAllocateCodecsOMX();
1071 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_aud_rend,omx_rend_clock_port);
1072 if (error!=OMX_ErrorNone){
1073 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);
1074 video->UnlockClock();
1075 DeAllocateCodecsOMX();
1079 if (!video->EnablePort(omx_clock,omx_clock_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_clock_port,false)
1081 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX clock rend failed");
1082 video->UnlockClock();
1083 DeAllocateCodecsOMX();
1087 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1088 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend idle ChangeComponentState");
1089 video->UnlockClock();
1090 DeAllocateCodecsOMX();
1097 if ( !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_clock_port)) {
1098 video->UnlockClock();
1099 DeAllocateCodecsOMX();
1103 if ( !video->CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1104 video->UnlockClock();
1105 DeAllocateCodecsOMX();
1111 if (!ChangeAudioPortConfig(false)){
1112 Log::getInstance()->log("Audio", Log::NOTICE, "Change AudioPortConfig failed");
1113 video->UnlockClock();
1114 DeAllocateCodecsOMX();
1118 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1119 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1120 DeAllocateCodecsOMX();
1126 if (!PrepareInputBufsOMX(true)) {
1127 video->UnlockClock();
1128 DeAllocateCodecsOMX();
1134 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,omx_aud_rend,omx_rend_input_port);
1135 if (error!=OMX_ErrorNone){
1136 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel dec to render failed %x", error);
1137 video->UnlockClock();
1138 DeAllocateCodecsOMX();
1144 /* if (!video->EnablePort(omx_aud_dec,omx_codec_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_input_port,false)
1146 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX codec rend failed");
1147 video->UnlockClock();
1148 DeAllocateCodecsOMX();
1152 /* if ( !video->CommandFinished(omx_aud_dec,OMX_CommandPortEnable,omx_codec_output_port)
1153 || !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1154 video->UnlockClock();
1155 DeAllocateCodecsOMX();
1159 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateExecuting)) {
1160 Log::getInstance()->log("Audio", Log::DEBUG, "omx_aud_rend ChangeComponentState Execute");
1161 video->UnlockClock();
1162 DeAllocateCodecsOMX();
1167 video->UnlockClock();
1171 video->clockUnpause();
1174 if (!video->setClockExecutingandRunning()) return 0;
1176 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX finished");
1181 int AudioOMX::PrepareInputBufsOMX(bool setportdef) //needs to be called with locvke omx clock mutex
1183 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
1185 OMX_ERRORTYPE error;
1186 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1187 memset(&port_def_type,0,sizeof(port_def_type));
1188 port_def_type.nSize=sizeof(port_def_type);
1189 port_def_type.nVersion.nVersion=OMX_VERSION;
1190 port_def_type.nPortIndex=omx_rend_input_port;//omx_codec_input_port;
1192 error=OMX_GetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1194 if (error!=OMX_ErrorNone){
1195 Log::getInstance()->log("Audio", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1200 port_def_type.nBufferCountActual=2;
1201 port_def_type.nBufferSize=max(port_def_type.nBufferSize,50000); // for transcoder important
1203 error=OMX_SetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1205 if (error!=OMX_ErrorNone){
1206 Log::getInstance()->log("Audio", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1211 error=OMX_SendCommand(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port/*codec*/,0);
1212 if (error!=OMX_ErrorNone){
1213 Log::getInstance()->log("Audio", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1217 input_bufs_omx_mutex.lock();
1218 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1219 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1220 error=OMX_AllocateBuffer(omx_aud_rend/*dec*/,&buf_head,omx_rend_input_port/*codec*/,NULL,port_def_type.nBufferSize);
1221 if (error!=OMX_ErrorNone){
1222 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1223 input_bufs_omx_mutex.unlock();
1226 input_bufs_omx_all.push_back(buf_head);
1227 input_bufs_omx_free.push_back(buf_head);
1229 omx_first_frame=true;
1232 cur_input_buf_omx=NULL;
1233 input_bufs_omx_mutex.unlock();
1235 if (!video->CommandFinished(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port /*codec*/)) {
1242 int AudioOMX::DestroyInputBufsOMX() //call with clock mutex locked
1244 OMX_ERRORTYPE error;
1246 cur_input_buf_omx=NULL;
1247 input_bufs_omx_mutex.lock();
1248 for (UINT i=0; i< input_bufs_omx_all.size();i++) {
1249 error=OMX_FreeBuffer(omx_aud_rend/*dec*/,omx_rend_input_port/*codec*/,input_bufs_omx_all[i]);
1250 if (error!=OMX_ErrorNone){
1251 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1252 input_bufs_omx_mutex.unlock();
1257 input_bufs_omx_all.clear();
1258 input_bufs_omx_free.clear();
1259 input_bufs_omx_mutex.unlock();
1264 int AudioOMX::DestroyInputBufsOMXwhilePlaying() //call with clock mutex locked
1266 //OMX_ERRORTYPE error;
1268 cur_input_buf_omx=NULL;
1269 input_bufs_omx_mutex.lock();
1270 while (input_bufs_omx_all.size()>0) {
1271 if (input_bufs_omx_free.size()>0) {
1272 // Destroy one buffer
1273 std::vector<OMX_BUFFERHEADERTYPE*>::iterator itty=input_bufs_omx_all.begin();
1274 OMX_BUFFERHEADERTYPE* cur_buf=input_bufs_omx_free.front();
1275 for (; itty!= input_bufs_omx_all.end();itty++) {
1276 if ((*itty)==cur_buf) {
1277 input_bufs_omx_all.erase(itty);
1278 input_bufs_omx_free.pop_front();
1283 input_bufs_omx_mutex.unlock();
1285 input_bufs_omx_mutex.lock();
1289 Log::getInstance()->log("Audio", Log::DEBUG, "DestroyInputBufsOMXwhilePlaying %d %d", input_bufs_omx_all.size(),input_bufs_omx_free.size());
1290 input_bufs_omx_mutex.unlock();
1295 int AudioOMX::DeAllocateCodecsOMX()
1297 OMX_ERRORTYPE error;
1299 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
1300 Log::getInstance()->log("Audio", Log::DEBUG, "enter deallocatecodecsomx");
1303 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 1");
1304 if (cur_input_buf_omx) {
1305 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1306 error=video->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,cur_input_buf_omx);
1307 if (error!=OMX_ErrorNone) {
1308 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_EmptyThisBuffer 6 failed %x", error);
1311 cur_input_buf_omx=NULL;//write out old data
1313 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 2");
1316 if (omx_aud_rend/*dec*/) {
1317 // first stop the omx elements
1318 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1319 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1321 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 3");
1323 video->UnlockClock();
1324 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 4");
1326 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 5");
1329 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1330 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend ChangeComponentState");
1333 // TODO proper deinit sequence
1334 // first flush all buffers
1336 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
1337 if (error!=OMX_ErrorNone) {
1338 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend in failed %x", error);
1342 /* error=OMX_SendCommand(omx_aud_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
1343 if (error!=OMX_ErrorNone){
1344 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1349 /* if (!video->CommandFinished(omx_aud_dec,OMX_CommandFlush,omx_codec_input_port)) {
1350 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd codec input failed");
1355 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1356 if (error!=OMX_ErrorNone){
1357 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1361 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_clock_port, NULL);
1362 if (error!=OMX_ErrorNone){
1363 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend clock failed %x", error);
1366 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6");
1368 if (!video->CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1369 !video->CommandFinished(omx_aud_rend,OMX_CommandFlush,omx_rend_clock_port)) {
1370 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd clock shed failed");
1373 DestroyInputBufsOMX(); //We have to make sure that no buffers are in use
1374 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6a");
1375 DeinitDecoderLibAV();
1376 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 7");
1379 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true)) {
1380 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 1");
1383 /* if (!video->DisablePort(omx_aud_dec,omx_codec_output_port,true)) {
1384 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 6");
1387 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port,true)) {
1388 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 7");
1392 if (!video->DisablePort(omx_aud_rend,omx_rend_clock_port,true)) {
1393 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 4");
1396 if (!video->DisablePort(omx_clock,omx_clock_output_port,true)) {
1397 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 5");
1402 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,NULL,NULL);
1403 if (error!=OMX_ErrorNone) {
1404 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1410 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_input_port,NULL,0);
1411 if (error!=OMX_ErrorNone) {
1412 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1416 error=OMX_SetupTunnel(omx_clock,omx_clock_output_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_aud_rend,omx_rend_clock_port,NULL,0);
1423 if (error!=OMX_ErrorNone) {
1424 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1427 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 8");
1430 //error=OMX_FreeHandle(omx_aud_dec);
1431 error=OMX_FreeHandle(omx_aud_rend);
1432 video->UnlockClock();
1433 video->destroyClock();
1434 omx_aud_rend/*dec*/=NULL;
1435 if (error!=OMX_ErrorNone) {
1436 Log::getInstance()->log("Audio", Log::DEBUG, "FreeHandle failed %d", error);
1440 video->UnlockClock();
1441 DeinitDecoderLibAV();
1443 Log::getInstance()->log("Audio", Log::DEBUG, "leave deallocate codecs OMX");
1450 int AudioOMX::stop()
1452 if (!initted) return 0;
1454 Log::getInstance()->log("Audio", Log::DEBUG, "Audio stop called");
1455 DeAllocateCodecsOMX();
1456 //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1460 int AudioOMX::mute() {
1463 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
1464 VideoOMX* vw = static_cast<VideoOMX*>(Video::getInstance());
1468 OMX_AUDIO_CONFIG_MUTETYPE amute;
1469 memset(&amute, 0, sizeof(amute));
1470 amute.nSize = sizeof(amute);
1471 amute.nVersion.nVersion = OMX_VERSION;
1472 amute.nPortIndex = omx_rend_input_port;
1473 amute.bMute = OMX_TRUE;
1474 OMX_ERRORTYPE error= OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1476 if (error != OMX_ErrorNone) {
1477 Log::getInstance()->log("Audio", Log::DEBUG,
1478 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1479 omx_rend_input_port);
1491 int AudioOMX::unMute()
1493 if (!initted) return 0;
1495 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
1496 VideoOMX* vw = static_cast<VideoOMX*>(Video::getInstance());
1500 OMX_AUDIO_CONFIG_MUTETYPE amute;
1501 memset(&amute, 0, sizeof(amute));
1502 amute.nSize = sizeof(amute);
1503 amute.nVersion.nVersion = OMX_VERSION;
1504 amute.nPortIndex = omx_rend_input_port;
1505 amute.bMute = OMX_FALSE;
1506 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1508 if (error != OMX_ErrorNone) {
1509 Log::getInstance()->log("Audio", Log::DEBUG,
1510 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1511 omx_rend_input_port);
1523 int AudioOMX::pause()
1525 if (!initted) return 0;
1526 VideoOMX* vw = static_cast<VideoOMX*>(Video::getInstance());
1531 int AudioOMX::unPause()
1533 if (!initted) return 0;
1534 VideoOMX* vw = static_cast<VideoOMX*>(Video::getInstance());
1539 int AudioOMX::reset()
1541 if (!initted) return 0;
1543 Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
1544 DeAllocateCodecsOMX();
1546 // if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1547 // Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
1548 // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
1554 int AudioOMX::setVolume(int tvolume)
1556 // parameter: 0 for silence, 20 for full
1557 if ((tvolume < 0) || (tvolume > 20)) return 0;
1559 // volume = 2 * (20 - volume);
1560 // Right, that one was rubbish... 0-10 were almost
1561 // inaudible, 11-20 did what should have been done
1562 // over the whole 0-20 range
1565 VideoOMX* vw = static_cast<VideoOMX*>(Video::getInstance());
1570 OMX_AUDIO_CONFIG_VOLUMETYPE avol;
1571 memset(&avol, 0, sizeof(avol));
1572 avol.nSize = sizeof(avol);
1573 avol.nVersion.nVersion = OMX_VERSION;
1574 avol.nPortIndex = omx_rend_input_port;
1575 avol.bLinear=OMX_FALSE;
1576 avol.sVolume.nValue =(volume-20)*200;
1577 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioVolume,
1579 if (error != OMX_ErrorNone) {
1580 Log::getInstance()->log("Audio", Log::DEBUG,
1581 "Set OMX_IndexConfigAudioVolume failed %x %d", error,
1582 omx_rend_input_port);
1594 int AudioOMX::test()
1597 // return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
1599 /* aud_sync_parms_t a;
1603 // int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
1606 /*OK*/ //printf("Audio sync disable = %i\n", b);
1614 unsigned int AudioOMX::AdvanceMpAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1616 if (size<=2) return size; // silly;
1617 unsigned int atest=0;
1619 //inspired by libav parsing code
1620 while (atest+1<size) {
1621 if (data[atest]==0xFF && (data[atest+1] &0xe0)==0xe0) {
1622 if ((data[atest+1] & 0x18) == 0x08) {atest++;continue;} //sanity check: mpeg version ID 01 -> reserved
1623 //sanity check inspired by libav
1626 const int sample_rates[4]={44100,48000,32000,0};
1627 const short bitrate_tab[2][3][15] = { { { 0, 32, 64, 96, 128, 160,
1628 192, 224, 256, 288, 320, 352, 384, 416, 448 }, { 0, 32, 48,
1629 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 }, {
1630 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
1631 256, 320 } }, { { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1632 160, 176, 192, 224, 256 }, { 0, 8, 16, 24, 32, 40, 48, 56,
1633 64, 80, 96, 112, 128, 144, 160 }, { 0, 8, 16, 24, 32, 40,
1634 48, 56, 64, 80, 96, 112, 128, 144, 160 } } };
1637 int layer=4-((data[atest+1]&0x06)>>1);
1638 if (layer==4) {atest++;continue;} //sanity check
1640 int bitrate_index=(data[atest+2]&0xf0)>>4;
1641 if (bitrate_index==0x0f || bitrate_index==0x0) {atest++;continue;} //sanity check
1643 int samplerate_index=(data[atest+2]&0x0C)>>2;
1644 if (samplerate_index==0x03) {atest++;continue;} //sanity check
1646 int padding=(data[atest+2]&2)>>1;
1647 if (0x10 & data[atest+1]) {
1648 lsf=((data[atest+1]) &0x8)?0:1;
1654 int sample_rate=sample_rates[ samplerate_index]>>(lsf+mpeg2);
1656 int temp_frame_size=bitrate_tab[lsf][layer - 1][bitrate_index];
1658 frame_size=(temp_frame_size*12000)/sample_rate;
1659 frame_size=(frame_size+padding)*4;
1660 } else if (layer==2) {
1661 frame_size=(temp_frame_size*144000)/sample_rate;
1662 frame_size=frame_size+padding;
1664 frame_size=(temp_frame_size*144000)/(sample_rate<<lsf);
1665 frame_size=frame_size+padding;
1667 unsigned int sameheadertest=(data[atest]<<24)|(data[atest+1]<<16) |(data[atest+2]<<8);
1668 const unsigned mask=(0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)); // from libav
1669 if (!mp3sameheader) {
1670 mp3sameheader=sameheadertest;
1671 mp3sameheadercount=2;
1673 if ((mp3sameheader& mask)== (sameheadertest &mask)) mp3sameheadercount++;
1674 else mp3sameheadercount=0;
1675 mp3sameheader=sameheadertest;
1677 //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);
1678 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME DIAG: %x %x %x %d",data[atest],data[atest+1],data[atest+2],mp3sameheadercount);
1680 if (mp3sameheadercount>4) {
1681 *framesize=frame_size;
1682 return atest; // probably FrameSync
1683 } //skip it if the header changes too frequently
1690 unsigned int AudioOMX::AdvanceAc3AudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1692 if (size<=4) return size; // silly;
1693 const int frm_size_tab[] = { 64, 64, 80, 80, 96, 96, 112, 112, 128, 128,
1694 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384, 384, 448,
1695 448, 512, 512, 640, 640, 768, 768, 896, 896, 1024, 1024, 1152,
1696 1152, 1280, 1280, };
1697 unsigned int atest=0;
1698 *framesize=20000; //if we do not find a start code do not decompress
1699 while (atest+4<size) {
1700 if (data[atest]==0x0B && data[atest+1]==0x77) {
1701 // now figure out the length of the frame
1702 unsigned char code=data[atest+4];
1703 unsigned char fscod=(code& 0xC0)>>6;
1704 unsigned char frmsize=(code &0x3f);
1705 if (fscod!=0) Log::getInstance()->log("Audio", Log::DEBUG, "warning we only support 48 KHz sampling rate");
1706 *framesize=frm_size_tab[frmsize]*2;
1707 return atest; // probably FrameSync
1714 unsigned int AudioOMX::AdvanceAacLatmAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1716 if (size<=4) return size; // silly;
1717 unsigned int atest=0;
1718 *framesize=20000; //if we do not find a start code do not decompress
1719 while (atest+4<size) {
1720 if (data[atest] ==0x56 && (data[atest+1]& 0xe0)==0xe0) {
1721 // now figure out the length of the frame
1722 unsigned int length= ((0x1f & data[atest+1])<<8) | data[atest+2];
1723 *framesize=length+3;
1724 return atest; // probably FrameSync
1732 void AudioOMX::PrepareMediaSample(const MediaPacketList& mplist, UINT /* samplepos */)
1734 packet = mplist.front();
1737 UINT AudioOMX::DeliverMediaSample(UCHAR* buffer, UINT* samplepos) {
1738 DeliverMediaPacket(packet, buffer, samplepos);
1739 if (*samplepos == packet.length) {
1747 long long AudioOMX::correctAudioLatency(long long pts,int addsamples,int srate)
1749 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
1751 OMX_PARAM_U32TYPE audio_lat;
1752 OMX_ERRORTYPE error;
1753 memset(&audio_lat, 0, sizeof(audio_lat));
1754 audio_lat.nSize = sizeof(audio_lat);
1755 audio_lat.nVersion.nVersion = OMX_VERSION;
1756 audio_lat.nPortIndex = omx_rend_input_port;
1758 error = OMX_GetConfig(omx_aud_rend, OMX_IndexConfigAudioRenderingLatency,
1760 video->UnlockClock();
1761 if (error != OMX_ErrorNone) {
1762 Log::getInstance()->log("Audio", Log::DEBUG,
1763 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
1764 omx_rend_input_port);
1765 return pts; // no correction in case of error
1767 /*Log::getInstance()->log("Audio", Log::DEBUG, "Current audio latency %d",
1770 long long workpts=0;
1771 workpts+=addsamples;
1772 workpts-=audio_lat.nU32;
1773 workpts*=10LL*1000LL*1000LL;
1774 workpts=workpts/((long long)srate); //one second /samplerate
1780 bool AudioOMX::DrainTargetBufferFull()
1782 //Check, if we have OMX output buffers
1784 input_bufs_omx_mutex.lock();
1785 full=(input_bufs_omx_free.size()==0);
1786 input_bufs_omx_mutex.unlock();
1792 UINT AudioOMX::DeliverMediaPacket(MediaPacket mpacket, const UCHAR* buffer,
1794 /*First Check, if we have an audio sample*/
1795 VideoOMX* vw = static_cast<VideoOMX*>(Video::getInstance());
1797 OMX_ERRORTYPE error;
1798 //Log *logger=Log::getInstance();
1799 if (vw->InIframemode()) {
1802 return 0; //Not in iframe mode!
1805 if (!omx_running) return 0; // if we are not runnig do not do this
1806 if (vw->isClockPaused()) return 0; //Block if we pause
1807 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 1");
1809 //Log::getInstance()->log("Audio", Log::DEBUG, "DeliverMediaPacketOMX time %lld",mpacket.presentation_time);
1811 /* if (mpacket.synched && mpacket.presentation_time <= 0) {
1812 *samplepos = mpacket.length;
1813 firstsynched = false;
1815 Log::getInstance()->log("Audio", Log::DEBUG,
1816 "DeliverMediaPacketOMX Frameskip");
1817 return mpacket.length;
1820 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 2");
1822 UINT headerstrip = 0;
1823 if (mpacket.disconti) {
1824 firstsynched = false;
1825 decompress_buffer_filled=0;
1826 if (cur_input_buf_omx) {
1827 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1829 if (error != OMX_ErrorNone) {
1830 Log::getInstance()->log("Audio", Log::DEBUG,
1831 "OMX_EmptyThisBuffer 1 failed %x", error);
1833 cur_input_buf_omx = NULL;
1837 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 3");
1838 if (mpacket.type != lastAType) {//Format Change //Push data out !
1839 firstsynched = false;
1842 Log::getInstance()->log("Audio", Log::DEBUG,"Notice audio type change %d %d", mpacket.type,lastAType);
1843 lastAType = mpacket.type;
1844 decompress_buffer_filled=0;
1846 if (cur_input_buf_omx) {
1847 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1849 if (error != OMX_ErrorNone) {
1850 Log::getInstance()->log("Audio", Log::DEBUG,
1851 "OMX_EmptyThisBuffer 2 failed %x", error);
1853 cur_input_buf_omx = NULL;
1858 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
1859 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
1861 if (!ChangeAudioPortConfig(true)) {
1862 Log::getInstance()->log("Audio", Log::DEBUG,
1863 "Changing audio port config failed", error);
1866 pthread_setcancelstate(oldcancelstate, NULL);
1867 pthread_setcanceltype(oldcanceltype, NULL);
1871 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 4");
1873 /*Inspect PES-Header */
1874 if (*samplepos == 0 && mpacket.type != MPTYPE_MPEG_AUDIO_LAYER3) {//stripheader
1875 headerstrip = buffer[mpacket.pos_buffer + 8] + 9;
1876 if (mpacket.type == MPTYPE_AC3)
1877 headerstrip += 4; //skip ac3 bytes
1878 *samplepos += headerstrip;
1879 if (mpacket.synched) {
1880 if (cur_input_buf_omx) {
1881 //cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1882 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1884 if (error != OMX_ErrorNone) {
1885 Log::getInstance()->log("Audio", Log::DEBUG,
1886 "OMX_EmptyThisBuffer 3 failed %x", error);
1888 //vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1890 cur_input_buf_omx = NULL;//write out old data
1892 firstsynched = true;
1893 //decompress_buffer_filled=0;
1895 if (!firstsynched) {//
1896 *samplepos = mpacket.length;//if we have not processed at least one
1897 decompress_buffer_filled=0;
1898 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 5");
1899 return mpacket.length;//synched packet ignore it!
1903 if (!cur_input_buf_omx) {
1904 input_bufs_omx_mutex.lock();
1905 if (input_bufs_omx_free.size()==0) {
1906 input_bufs_omx_mutex.unlock();
1907 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 6");
1908 //Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample");
1909 return 0; // we do not have a free media sample
1912 cur_input_buf_omx=input_bufs_omx_free.front();
1913 cur_input_buf_omx->nFilledLen=0;
1914 cur_input_buf_omx->nOffset=0;
1915 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
1916 input_bufs_omx_free.pop_front();
1917 input_bufs_omx_mutex.unlock();
1919 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 7");
1921 if (cur_input_buf_omx->nFilledLen == 0) {//will only be changed on first packet
1922 if (mpacket.synched) {
1923 //Log::getInstance()->log("Audio", Log::DEBUG,
1924 // "packet synched marker");
1926 //lastreftimePTS=mpacket.pts;
1927 if (omx_first_frame) { // TODO time
1928 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
1929 Log::getInstance()->log("Audio", Log::DEBUG, "Starttime");
1930 omx_first_frame = false;
1932 cur_input_buf_omx->nFlags = 0;
1933 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
1935 lastreftimeOMX = mpacket.presentation_time;
1936 //Log::getInstance()->log("Audio", Log::DEBUG,
1937 // "Time code %lld pts %lld dts %lld", lastreftimeOMX, mpacket.pts,mpacket.dts);
1938 lastreftimePTS = mpacket.pts;
1939 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
1941 // Log::getInstance()->log("Audio", Log::DEBUG,
1942 // "packet NOT synched marker");
1943 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
1944 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
1947 if (mpacket.disconti || achange) {
1948 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_DISCONTINUITY;
1949 //mp23codec_context_libav->frame_size=-1;
1950 //ac3codec_context_libav->frame_size=-1;
1954 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 8");
1956 if (*samplepos>mpacket.length) *samplepos=0; //propably the thread got interrupted and sample is not valid any more!
1957 unsigned int haveToCopy=mpacket.length-*samplepos;
1960 while (haveToCopy>0) {
1961 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 9");
1963 unsigned int gotframesize=0;
1965 switch (mpacket.type) {
1966 case MPTYPE_MPEG_AUDIO:
1967 case MPTYPE_MPEG_AUDIO_LAYER3: {
1968 adv = AdvanceMpAudioSync(buffer+mpacket.pos_buffer+*samplepos,
1969 haveToCopy,&gotframesize);
1973 case MPTYPE_AC3_PRE13: {
1974 adv = AdvanceAc3AudioSync(buffer+mpacket.pos_buffer+*samplepos,
1975 haveToCopy,&gotframesize);
1979 case MPTYPE_AAC_LATM: {
1980 adv = AdvanceAacLatmAudioSync(buffer+mpacket.pos_buffer+*samplepos,
1981 haveToCopy,&gotframesize);
1985 if (adv != (int)haveToCopy) {
1990 *samplepos=mpacket.length; //no ac3 sync byte
1991 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 10");
1992 return mpacket.length;
1995 // so everything is fine now do a memcpy
1996 int cancopy=min(haveToCopy,cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen);
1997 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+mpacket.pos_buffer+*samplepos,cancopy);
1998 haveToCopy-=cancopy;
1999 cur_input_buf_omx->nFilledLen+=cancopy;
2000 *samplepos+=cancopy;
2001 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 11");
2003 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2004 if (error != OMX_ErrorNone) {
2005 Log::getInstance()->log("Audio", Log::DEBUG,
2006 "OMX_EmptyThisBuffer 5 failed %x", error);
2008 cur_input_buf_omx=NULL;
2011 input_bufs_omx_mutex.lock();
2012 if (input_bufs_omx_free.size()==0) {
2013 input_bufs_omx_mutex.unlock();
2014 // Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample2");
2015 return *samplepos; // we do not have a free media sample
2017 cur_input_buf_omx=input_bufs_omx_free.front();
2018 cur_input_buf_omx->nFilledLen=0;
2019 cur_input_buf_omx->nOffset=0;
2020 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2021 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
2022 input_bufs_omx_free.pop_front();
2023 input_bufs_omx_mutex.unlock();
2025 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 12");
2028 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 13");
2036 AVCodecContext *current_context;
2037 switch (mpacket.type) {
2038 case MPTYPE_MPEG_AUDIO:
2039 case MPTYPE_MPEG_AUDIO_LAYER3: {
2040 current_context = mp23codec_context_libav;
2041 if (current_context->frame_size<0) framesize=1152; //Maximum framesize
2042 else framesize=current_context->frame_size;
2044 case MPTYPE_AAC_LATM: {
2045 current_context = aaclatmcodec_context_libav;
2048 case MPTYPE_AC3_PRE13: {
2049 current_context = ac3codec_context_libav;
2053 if (decompress_buffer_filled) { // have a remaining paket
2054 incoming_paket_libav.data =(uint8_t*) decompress_buffer;
2055 memcpy(decompress_buffer+decompress_buffer_filled,
2056 buffer+mpacket.pos_buffer+*samplepos,
2057 min(haveToCopy,decompress_buffer_size-decompress_buffer_filled));
2058 incoming_paket_libav.size = decompress_buffer_filled
2059 +min(haveToCopy,decompress_buffer_size-decompress_buffer_filled);
2060 //Log::getInstance()->log("Audio", Log::DEBUG,"Use saved audio buffer %d %d %d",mpacket.type,decompress_buffer_filled,mpacket.synched);
2062 incoming_paket_libav.data =(uint8_t*) buffer+mpacket.pos_buffer+*samplepos;
2063 incoming_paket_libav.size = haveToCopy;
2065 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 18");
2067 while (haveToCopy> 0 && errcount<3) {
2069 //Log::getInstance()->log("Audio", Log::DEBUG,"libav in %d %d",framesize,current_context->frame_size);
2070 //Log::getInstance()->log("Audio", Log::DEBUG, "libav in %d %d",
2071 // framesize, current_context->frame_size);
2073 bool donotdecompress=false;
2074 unsigned int gotframesize=0;
2075 // if (!decompress_buffer_filled) { // only do this if no old data is present
2077 switch (mpacket.type) {
2078 case MPTYPE_MPEG_AUDIO:
2079 case MPTYPE_MPEG_AUDIO_LAYER3: {
2080 adv = AdvanceMpAudioSync(incoming_paket_libav.data,
2081 incoming_paket_libav.size,&gotframesize);
2085 case MPTYPE_AC3_PRE13: {
2086 adv = AdvanceAc3AudioSync(incoming_paket_libav.data,
2087 incoming_paket_libav.size,&gotframesize);
2090 case MPTYPE_AAC_LATM: {
2091 adv = AdvanceAacLatmAudioSync(incoming_paket_libav.data,
2092 incoming_paket_libav.size,&gotframesize);
2096 if (adv > 0 /*&& !decompress_buffer_filled*/) {
2097 incoming_paket_libav.data += adv;
2098 incoming_paket_libav.size-=adv;
2101 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2102 else*/ framesize=haveToCopy;
2103 //Log::getInstance()->log("Audio", Log::DEBUG,"Advance by %d %d from %d",adv,mpacket.type,*samplepos );
2104 if (haveToCopy <= 0) {
2105 // Log::getInstance()->log("Audio", Log::DEBUG,"No sync code in packet remove %d",mpacket.type);
2106 *samplepos=mpacket.length;
2107 return mpacket.length;
2113 if (gotframesize>0 && gotframesize>haveToCopy) {
2114 donotdecompress=true;
2115 errcount=100; // exit loop
2117 // else Log::getInstance()->log("Audio", Log::DEBUG,"Loop run" );
2119 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 19");
2120 if (!donotdecompress) {
2123 pthread_testcancel();
2124 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2125 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2127 if (!omx_running || !mp23codec_context_libav
2128 || !ac3codec_context_libav) {
2129 libav_mutex.unlock();
2132 libav_mutex.unlock();
2133 // Log::getInstance()->log("Audio", Log::DEBUG,"libav out");
2134 int save_size=incoming_paket_libav.size;
2136 if ((int)gotframesize <= incoming_paket_libav.size) {
2137 if (gotframesize>0) incoming_paket_libav.size=gotframesize;
2138 len = avcodec_decode_audio4(current_context, decode_frame_libav,
2139 &gotta, &incoming_paket_libav);
2141 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:E %d %d",gotframesize,incoming_paket_libav.size);
2145 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:T %d",len);
2146 incoming_paket_libav.size=save_size;
2147 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out1");
2148 pthread_setcancelstate(oldcancelstate, NULL);
2149 pthread_setcanceltype(oldcanceltype, NULL);
2150 pthread_testcancel();
2156 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out2");
2157 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 20");
2159 libav_mutex.unlock();
2163 if (decompress_buffer_filled) { // reset to normal decoding
2165 //Log::getInstance()->log("Audio", Log::DEBUG,"saved audio: %d",len);
2166 haveToCopy -= min(len-decompress_buffer_filled,0);
2167 *samplepos += min(len-decompress_buffer_filled,0);
2168 //if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2169 /*else*/ framesize=haveToCopy;
2171 framesize=haveToCopy;
2173 incoming_paket_libav.data =(uint8_t*) buffer+mpacket.pos_buffer+*samplepos;
2175 decompress_buffer_filled=0;
2179 incoming_paket_libav.data += len;
2183 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2184 else*/framesize=haveToCopy;
2187 framesize=haveToCopy;
2190 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 21");
2192 incoming_paket_libav.size =framesize;
2194 //Log::getInstance()->log("Audio", Log::DEBUG,
2197 int dsize = av_samples_get_buffer_size(NULL,
2198 /*current_context->channels*/2, decode_frame_libav->nb_samples,
2199 AV_SAMPLE_FMT_S16, 1);
2200 /* int dsize_in = av_samples_get_buffer_size(NULL,
2201 current_context->channels, decode_frame_libav->nb_samples,
2202 current_context->sample_fmt, 1); -- unused */
2203 //if (current_context->channels==1) dsize*=2; // we convert mono to stereo
2204 if ((cur_input_buf_omx->nFilledLen + dsize)
2205 > cur_input_buf_omx->nAllocLen ) {
2206 // I doubt that this will ever happen
2207 // Log::getInstance()->log("Audio", Log::DEBUG,
2208 // "P 2 Time code %lld pts %lld", lastreftimeOMX, mpacket.pts);
2209 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
2211 if (error != OMX_ErrorNone) {
2212 Log::getInstance()->log("Audio", Log::DEBUG,
2213 "OMX_EmptyThisBuffer 4 failed %x", error);
2215 cur_input_buf_omx = NULL;
2217 if (!cur_input_buf_omx) {
2219 while (count < 10 && omx_running) {
2221 input_bufs_omx_mutex.lock();
2222 if (input_bufs_omx_free.size() == 0) {
2223 input_bufs_omx_mutex.unlock();
2224 // Log::getInstance()->log("Audio", Log::DEBUG,
2225 // "Deliver MediaPacket no free sample");
2227 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark22");
2228 if (!omx_running) return *samplepos;
2229 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 23");
2232 cur_input_buf_omx = input_bufs_omx_free.front();
2233 cur_input_buf_omx->nFilledLen = 0;
2234 cur_input_buf_omx->nOffset = 0;
2235 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
2236 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2237 input_bufs_omx_free.pop_front();
2238 input_bufs_omx_mutex.unlock();
2241 if (!cur_input_buf_omx) return *samplepos;
2246 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy in %d %d %d" ,dsize,current_context->sample_rate,cur_input_buf_omx->nFilledLen);
2249 av_opt_set_int(resam_con_libav, "in_sample_rate",decode_frame_libav->sample_rate,0);
2250 av_opt_set_int(resam_con_libav, "in_sample_fmt",decode_frame_libav->format,0);
2251 av_opt_set_int(resam_con_libav, "in_channel_layout",decode_frame_libav->channel_layout, 0);
2252 //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);
2253 //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);
2255 int ret = swr_init(resam_con_libav);
2257 Log::getInstance()->log("Audio", Log::ERR,"Opening AV resample failed %d",ret);
2259 uint8_t *output=cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen;
2262 avresample_convert ( AVAudioResampleContext * avr,
2266 uint8_t *const * input,
2272 avresample_convert(resam_con_libav,
2273 &output, dsize, decode_frame_libav->nb_samples,
2274 decode_frame_libav->extended_data, decode_frame_libav->linesize[0], decode_frame_libav->nb_samples);
2277 swr_convert ( struct SwrContext * s,
2280 const uint8_t ** in,
2285 //Log::getInstance()->log("AudioOMX", Log::DEBUG, "Calling swr_convert");
2287 swr_convert(resam_con_libav, &output, decode_frame_libav->nb_samples, (const uint8_t**)decode_frame_libav->extended_data, decode_frame_libav->nb_samples);
2289 swr_close(resam_con_libav);
2292 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy out");
2293 cur_input_buf_omx->nFilledLen += dsize;
2295 //Log::getInstance()->log("Audio", Log::DEBUG,"Incomplete mpeg frames in pes packet %d %d",incoming_paket_libav.size,mpacket.length);
2296 /* uint8_t a1=incoming_paket_libav.data[0];
2297 uint8_t a2=incoming_paket_libav.data[1];
2298 uint8_t a3=incoming_paket_libav.data[2];
2299 uint8_t a4=incoming_paket_libav.data[3];*/
2300 // Log::getInstance()->log("Audio", Log::DEBUG,"Header %x %x %x %x",a1,a2,
2305 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 24");
2306 decompress_buffer_filled=0;
2308 //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);
2309 memcpy(decompress_buffer,incoming_paket_libav.data,min(haveToCopy,decompress_buffer_size));
2311 decompress_buffer_filled=min(haveToCopy,decompress_buffer_size);
2315 if (cur_input_buf_omx->nFilledLen) {
2316 //Log::getInstance()->log("Audio", Log::DEBUG,
2317 // "P 3 Time code %lld pts %lld", lastreftimeOMX, mpacket.pts);
2318 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2319 if (error != OMX_ErrorNone) {
2320 Log::getInstance()->log("Audio", Log::DEBUG,
2321 "OMX_EmptyThisBuffer 5 failed %x", error);
2323 //if (mpacket.synched) vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
2324 cur_input_buf_omx = NULL;
2329 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 25");
2331 *samplepos=mpacket.length;
2332 return mpacket.length;
2336 long long AudioOMX::SetStartOffset(long long curreftime, bool *rsync)
2338 VideoOMX* vw = dynamic_cast<VideoOMX*>(Video::getInstance());
2339 return vw->SetStartAudioOffset(curreftime,rsync);
2342 void AudioOMX::ResetTimeOffsets()
2344 VideoOMX* vw = dynamic_cast<VideoOMX*>(Video::getInstance());
2345 vw->ResetTimeOffsets();