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
507 OMX_ERRORTYPE AudioOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
509 //Log::getInstance()->log("Audio", Log::NOTICE, "EmptyBufferDone");
510 AudioOMX* audio = static_cast<AudioOMX*>(getInstance());
511 audio->ReturnEmptyOMXBuffer(buffer);
512 return OMX_ErrorNone;
515 void AudioOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
516 input_bufs_omx_mutex.lock();
517 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
518 input_bufs_omx_free.push_back(buffer);
519 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
520 input_bufs_omx_mutex.unlock();
521 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
525 OMX_ERRORTYPE AudioOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
526 Log::getInstance()->log("Audio", Log::NOTICE, "FillBufferDone");
527 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
529 return OMX_ErrorNone;
534 int AudioOMX::setStreamType(UCHAR type)
536 if (!initted) return 0;
538 // if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
542 int AudioOMX::setChannel()
544 if (!initted) return 0;
546 // if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
550 int AudioOMX::setSource()
552 if (!initted) return 0;
554 // if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
560 if (!initted) return 0;
562 // if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
566 int AudioOMX::play() {
569 lastAType=MPTYPE_MPEG_AUDIO;
570 Log::getInstance()->log("Audio", Log::DEBUG, "enter play");
572 static_cast<VideoOMX*>(Video::getInstance())->interlaceSwitch4Demux(); // switch resolution if necessary
574 if (!AllocateCodecsOMX()) {
581 int AudioOMX::ChangeAudioDestination() //clock aka omx mutex needs to be locked
584 const char * destinations[]={"local","hdmi"};
589 OMX_CONFIG_BRCMAUDIODESTINATIONTYPE auddest;
590 memset(&auddest,0,sizeof(auddest));
591 auddest.nSize=sizeof(auddest);
592 auddest.nVersion.nVersion=OMX_VERSION;
593 strcpy((char *)auddest.sName, destinations[dest]);
595 Log::getInstance()->log("Audio", Log::DEBUG, "setting destination to: %s",auddest.sName);
596 error=OMX_SetConfig(omx_aud_rend,OMX_IndexConfigBrcmAudioDestination,&auddest);
597 if (error!=OMX_ErrorNone){
598 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX_IndexConfigBrcmAudioDestination failed %x %x %s", error,omx_aud_rend,auddest.sName);
599 DeAllocateCodecsOMX();
607 int AudioOMX::ChangeAudioPortConfig(bool disport) //clock aka omx mutex needs to be locked
610 //Ok first fidle a working configuration
611 Log::getInstance()->log("Audio", Log::DEBUG,
612 "ChangeAudioPortConfig");
614 OMX_AUDIO_CODINGTYPE encoding;
617 case MPTYPE_MPEG_AUDIO: {
618 if (prefered_mp2 == 2 && false) { //not supported yet
621 if (prefered_mp2 == 1 && canpass_mp2) {
623 encoding=OMX_AUDIO_CodingMP3;
626 encoding=OMX_AUDIO_CodingPCM;
631 case MPTYPE_AAC_LATM: {
632 if (prefered_aac == 2 && false) { //not supported yet
635 Log::getInstance()->log("Audio", Log::DEBUG,
636 "ChangeAudioPortConfig debug %d %d",prefered_aac,canpass_aac);
637 if (prefered_aac == 1 && canpass_aac) {
639 encoding=OMX_AUDIO_CodingAAC;
642 encoding=OMX_AUDIO_CodingPCM;
647 case MPTYPE_AC3_PRE13:
649 if (prefered_ac3 == 2 && false) { //not supported yet
652 Log::getInstance()->log("Audio", Log::DEBUG,
653 "ChangeAudioPortConfig debug %d %d",prefered_ac3,canpass_ac3);
654 if (prefered_ac3 == 1 && canpass_ac3) {
656 encoding=OMX_AUDIO_CodingDDP;
659 encoding=OMX_AUDIO_CodingPCM;
664 case MPTYPE_MPEG_AUDIO_LAYER3: {
665 if (prefered_mp3 == 2 && false) { //not supported yet
668 if (prefered_mp3 == 1 && canpass_mp2) {
670 encoding=OMX_AUDIO_CodingMP3;
673 encoding=OMX_AUDIO_CodingPCM;
681 encoding=OMX_AUDIO_CodingPCM;
682 //mch=false; // multichannel also false
687 /*OMX_CONFIG_BOOLEANTYPE booly;
688 memset(&booly, 0, sizeof(booly));
689 booly.nSize = sizeof(booly);
690 booly.nVersion.nVersion = OMX_VERSION;
692 booly.bEnabled = OMX_TRUE;
694 booly.bEnabled = OMX_FALSE;
696 error = OMX_SetParameter(omx_aud_dec, OMX_IndexParamBrcmDecoderPassThrough,
698 if (error != OMX_ErrorNone) {
699 Log::getInstance()->log("Audio", Log::DEBUG,
700 "Init OMX_IndexParamBrcmDecoderPassThrough failed %x", error);
701 DeAllocateCodecsOMX();
704 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
706 video->DisablePort(omx_aud_rend,omx_rend_input_port,false);
707 //DestroyInputBufsOMXwhilePlaying();
708 //video->CommandFinished(omx_aud_rend,OMX_CommandPortDisable,omx_rend_input_port);
713 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
714 memset(&format, 0, sizeof(format));
715 format.nSize = sizeof(format);
716 format.nVersion.nVersion = OMX_VERSION;
717 format.nPortIndex = omx_rend_input_port;
718 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
720 if (error != OMX_ErrorNone) {
721 Log::getInstance()->log("Audio", Log::DEBUG,
722 "Get OMX_IndexParamAudioPortFormat failed %x %d", error,
723 omx_rend_input_port);
728 Log::getInstance()->log("Audio", Log::DEBUG,
729 "Get OMX_IndexParamAudioPortFormat returned %d",format.eEncoding );
730 format.eEncoding = encoding;
732 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
734 if (error != OMX_ErrorNone) {
735 Log::getInstance()->log("Audio", Log::DEBUG,
736 "Set OMX_IndexParamAudioPortFormat failed %x %d %d", error,
737 omx_rend_input_port,format.eEncoding );
742 case OMX_AUDIO_CodingPCM: {
743 OMX_AUDIO_PARAM_PCMMODETYPE audio_pcm;
744 memset(&audio_pcm, 0, sizeof(audio_pcm));
745 audio_pcm.nSize = sizeof(audio_pcm);
746 audio_pcm.nVersion.nVersion = OMX_VERSION;
747 audio_pcm.nChannels = 2;
748 audio_pcm.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
749 audio_pcm.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
750 //audio_pcm.eChannelMapping[2]=OMX_AUDIO_ChannelMax;
751 audio_pcm.eNumData = OMX_NumericalDataSigned;
752 audio_pcm.eEndian = OMX_EndianLittle;
753 audio_pcm.bInterleaved = OMX_TRUE;
754 audio_pcm.nBitPerSample = 16;
755 audio_pcm.ePCMMode = OMX_AUDIO_PCMModeLinear;
756 audio_pcm.nChannels = 2;
757 audio_pcm.nSamplingRate = 48000;
758 audio_pcm.nPortIndex = omx_rend_input_port;
759 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPcm,
761 if (error != OMX_ErrorNone) {
762 Log::getInstance()->log("Audio", Log::DEBUG,
763 "Init OMX_IndexParamAudioPcm failed %x %d", error,
764 omx_rend_input_port);
768 case OMX_AUDIO_CodingDDP: {
769 OMX_AUDIO_PARAM_DDPTYPE audio_ddp;
770 memset(&audio_ddp, 0, sizeof(audio_ddp));
771 audio_ddp.nSize = sizeof(audio_ddp);
772 audio_ddp.nVersion.nVersion = OMX_VERSION;
773 audio_ddp.nPortIndex = omx_rend_input_port;
774 audio_ddp.nChannels = 8; //unknown
775 audio_ddp.nBitRate=0;
776 audio_ddp.nSampleRate=48000;
777 audio_ddp.eChannelMapping[0] =OMX_AUDIO_ChannelLF;
778 audio_ddp.eChannelMapping[1] =OMX_AUDIO_ChannelRF;
779 audio_ddp.eChannelMapping[2] =OMX_AUDIO_ChannelCF;
780 audio_ddp.eChannelMapping[3] =OMX_AUDIO_ChannelLFE;
781 audio_ddp.eChannelMapping[4] =OMX_AUDIO_ChannelLR;
782 audio_ddp.eChannelMapping[5] =OMX_AUDIO_ChannelRR;
783 audio_ddp.eChannelMapping[6] =OMX_AUDIO_ChannelLS;
784 audio_ddp.eChannelMapping[7] =OMX_AUDIO_ChannelRS;
785 audio_ddp.eChannelMapping[8] =OMX_AUDIO_ChannelCS;
786 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioDdp,
788 if (error != OMX_ErrorNone) {
789 Log::getInstance()->log("Audio", Log::DEBUG,
790 "Init OMX_IndexParamAudioDdp failed %x %d", error,
791 omx_rend_input_port);
796 default: break; //Make compiler happy
803 //PrepareInputBufsOMX(false);
804 video->EnablePort(omx_aud_rend,omx_rend_input_port,false);
814 int AudioOMX::InitDecoderLibAV()
817 ac3codec_context_libav = avcodec_alloc_context3(ac3codec_libav);
818 if (!ac3codec_context_libav) {
819 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for ac3 decoding context failed!");
823 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 34, 1)
824 ac3codec_context_libav->flags |= AV_CODEC_FLAG_TRUNCATED;
826 ac3codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
829 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
830 ac3codec_context_libav->request_channel_layout=2;
832 ac3codec_context_libav->request_channels=2;
835 int avc_ret = avcodec_open2(ac3codec_context_libav, ac3codec_libav, NULL);
837 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
838 libav_mutex.unlock();
842 aaclatmcodec_context_libav = avcodec_alloc_context3(aaclatmcodec_libav);
843 if (!aaclatmcodec_context_libav) {
844 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for aac decoding context failed!");
848 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 34, 1)
849 aaclatmcodec_context_libav->flags |= AV_CODEC_FLAG_TRUNCATED;
851 aaclatmcodec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
854 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
855 aaclatmcodec_context_libav->request_channel_layout=2;
857 aaclatmcodec_context_libav->request_channels=2;
860 avc_ret = avcodec_open2(aaclatmcodec_context_libav, aaclatmcodec_libav, NULL);
862 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
863 libav_mutex.unlock();
868 mp23codec_context_libav = avcodec_alloc_context3(mp23codec_libav);
869 if (!ac3codec_context_libav) {
870 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for mp23 decoding context failed!");
871 libav_mutex.unlock();
875 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 34, 1)
876 mp23codec_context_libav->flags |= AV_CODEC_FLAG_TRUNCATED;
878 mp23codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
881 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
882 mp23codec_context_libav->request_channel_layout=2;
884 mp23codec_context_libav->request_channels=2;
887 avc_ret = avcodec_open2(mp23codec_context_libav, mp23codec_libav, NULL);
889 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
890 libav_mutex.unlock();
894 resam_con_libav = avresample_alloc_context();
895 if (resam_con_libav == NULL) {
896 Log::getInstance()->log("Audio", Log::DEBUG,
897 "Alloc resample context failed");
901 av_opt_set_int(resam_con_libav, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0); // our standard format
902 av_opt_set_int(resam_con_libav, "out_sample_rate",48000,0);
903 av_opt_set_int(resam_con_libav, "out_sample_fmt",AV_SAMPLE_FMT_S16,0);
904 av_opt_set_int(resam_con_libav, "matrix_encoding",AV_MATRIX_ENCODING_DPLII,0);
906 av_opt_set_int(resam_con_libav, "in_sample_rate",48000,0);
907 av_opt_set_int(resam_con_libav, "in_sample_fmt",AV_SAMPLE_FMT_S16,0);
908 av_opt_set_int(resam_con_libav, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0); //just an example
911 av_init_packet(&incoming_paket_libav);
912 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
913 decode_frame_libav=av_frame_alloc();
915 decode_frame_libav=avcodec_alloc_frame();
917 libav_mutex.unlock();
918 decompress_buffer_filled=0;
925 void AudioOMX::DeinitDecoderLibAV() {
929 if (ac3codec_context_libav) {
930 avcodec_close(ac3codec_context_libav);
931 av_free(ac3codec_context_libav);
932 ac3codec_context_libav = NULL;
934 avcodec_close(aaclatmcodec_context_libav);
935 av_free(aaclatmcodec_context_libav);
936 aaclatmcodec_context_libav = NULL;
938 av_free(decode_frame_libav);
940 avcodec_close(mp23codec_context_libav);
941 av_free(mp23codec_context_libav);
942 mp23codec_context_libav = NULL;
944 avresample_free(&resam_con_libav);
945 resam_con_libav=NULL;
949 libav_mutex.unlock();
954 int AudioOMX::AllocateCodecsOMX()
957 static OMX_CALLBACKTYPE callbacks= {&VideoOMX::EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
959 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX");
960 //Clock, move later to audio
961 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
963 if (!InitDecoderLibAV()) return 0;;
966 OMX_PORT_PARAM_TYPE p_param;
967 memset(&p_param,0,sizeof(p_param));
968 p_param.nSize=sizeof(p_param);
969 p_param.nVersion.nVersion=OMX_VERSION;
972 if (!video->getClockAudioandInit(&omx_clock,&omx_clock_output_port)){
973 return 0;// get the clock and init it if necessary
977 if (!video->idleClock()) {
982 error = OMX_GetHandle(&omx_aud_rend, L_VPE_OMX_AUDIO_REND, NULL, &callbacks);
983 if (error != OMX_ErrorNone) {
984 Log::getInstance()->log("Audio", Log::DEBUG,
985 "Init OMX audio rend failed %x", error);
986 video->UnlockClock();
987 DeAllocateCodecsOMX();
991 if (!ChangeAudioDestination()) {
992 video->UnlockClock();
993 DeAllocateCodecsOMX();
997 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioInit, &p_param);
998 if (error != OMX_ErrorNone) {
999 Log::getInstance()->log("Audio", Log::DEBUG,
1000 "Init OMX audio rend OMX_GetParameter failed %x", error);
1001 video->UnlockClock();
1002 DeAllocateCodecsOMX();
1005 omx_rend_input_port = p_param.nStartPortNumber;
1011 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamOtherInit, &p_param);
1012 if (error != OMX_ErrorNone) {
1013 Log::getInstance()->log("Audio", Log::DEBUG,
1014 "Init OMX aud rend OMX_GetParameter failed %x", error);
1015 video->UnlockClock();
1016 DeAllocateCodecsOMX();
1019 // buggy return value
1020 omx_rend_clock_port = p_param.nStartPortNumber;
1025 /* error=OMX_GetHandle(&omx_aud_dec,VPE_OMX_AUDIO_DECODER,NULL,&callbacks);
1027 if (error!=OMX_ErrorNone){
1028 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder failed %x", error);
1029 video->UnlockClock();
1030 DeAllocateCodecsOMX();
1034 error=OMX_GetParameter(omx_aud_dec,OMX_IndexParamAudioInit,&p_param);
1035 if (error!=OMX_ErrorNone){
1036 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder OMX_GetParameter failed %x", error);
1037 video->UnlockClock();
1038 DeAllocateCodecsOMX();
1041 omx_codec_input_port=p_param.nStartPortNumber;
1042 omx_codec_output_port=p_param.nStartPortNumber+1;
1044 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port) || !video->DisablePort(omx_aud_dec,omx_codec_output_port)) {
1045 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio decoder failed");
1046 video->UnlockClock();
1047 DeAllocateCodecsOMX();
1054 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true) ) {
1055 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio rend failed %d",omx_rend_input_port);
1056 video->UnlockClock();
1057 DeAllocateCodecsOMX();
1061 if ( !video->DisablePort(omx_aud_rend, omx_rend_clock_port, true)) {
1062 Log::getInstance()->log("Audio", Log::DEBUG,
1063 "Disable Ports OMX rend clock port failed %d",omx_rend_clock_port);
1064 video->UnlockClock();
1065 DeAllocateCodecsOMX();
1075 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_aud_rend,omx_rend_clock_port);
1076 if (error!=OMX_ErrorNone){
1077 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);
1078 video->UnlockClock();
1079 DeAllocateCodecsOMX();
1083 if (!video->EnablePort(omx_clock,omx_clock_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_clock_port,false)
1085 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX clock rend failed");
1086 video->UnlockClock();
1087 DeAllocateCodecsOMX();
1091 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1092 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend idle ChangeComponentState");
1093 video->UnlockClock();
1094 DeAllocateCodecsOMX();
1101 if ( !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_clock_port)) {
1102 video->UnlockClock();
1103 DeAllocateCodecsOMX();
1107 if ( !video->CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1108 video->UnlockClock();
1109 DeAllocateCodecsOMX();
1115 if (!ChangeAudioPortConfig(false)){
1116 Log::getInstance()->log("Audio", Log::NOTICE, "Change AudioPortConfig failed");
1117 video->UnlockClock();
1118 DeAllocateCodecsOMX();
1122 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1123 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1124 DeAllocateCodecsOMX();
1130 if (!PrepareInputBufsOMX(true)) {
1131 video->UnlockClock();
1132 DeAllocateCodecsOMX();
1138 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,omx_aud_rend,omx_rend_input_port);
1139 if (error!=OMX_ErrorNone){
1140 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel dec to render failed %x", error);
1141 video->UnlockClock();
1142 DeAllocateCodecsOMX();
1148 /* if (!video->EnablePort(omx_aud_dec,omx_codec_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_input_port,false)
1150 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX codec rend failed");
1151 video->UnlockClock();
1152 DeAllocateCodecsOMX();
1156 /* if ( !video->CommandFinished(omx_aud_dec,OMX_CommandPortEnable,omx_codec_output_port)
1157 || !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1158 video->UnlockClock();
1159 DeAllocateCodecsOMX();
1163 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateExecuting)) {
1164 Log::getInstance()->log("Audio", Log::DEBUG, "omx_aud_rend ChangeComponentState Execute");
1165 video->UnlockClock();
1166 DeAllocateCodecsOMX();
1171 video->UnlockClock();
1175 video->clockUnpause();
1178 if (!video->setClockExecutingandRunning()) return 0;
1180 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX finished");
1185 int AudioOMX::PrepareInputBufsOMX(bool setportdef) //needs to be called with locvke omx clock mutex
1187 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
1189 OMX_ERRORTYPE error;
1190 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1191 memset(&port_def_type,0,sizeof(port_def_type));
1192 port_def_type.nSize=sizeof(port_def_type);
1193 port_def_type.nVersion.nVersion=OMX_VERSION;
1194 port_def_type.nPortIndex=omx_rend_input_port;//omx_codec_input_port;
1196 error=OMX_GetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1198 if (error!=OMX_ErrorNone){
1199 Log::getInstance()->log("Audio", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1204 port_def_type.nBufferCountActual=2;
1205 port_def_type.nBufferSize=max(port_def_type.nBufferSize,50000); // for transcoder important
1207 error=OMX_SetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1209 if (error!=OMX_ErrorNone){
1210 Log::getInstance()->log("Audio", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1215 error=OMX_SendCommand(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port/*codec*/,0);
1216 if (error!=OMX_ErrorNone){
1217 Log::getInstance()->log("Audio", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1221 input_bufs_omx_mutex.lock();
1222 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1223 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1224 error=OMX_AllocateBuffer(omx_aud_rend/*dec*/,&buf_head,omx_rend_input_port/*codec*/,NULL,port_def_type.nBufferSize);
1225 if (error!=OMX_ErrorNone){
1226 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1227 input_bufs_omx_mutex.unlock();
1230 input_bufs_omx_all.push_back(buf_head);
1231 input_bufs_omx_free.push_back(buf_head);
1233 omx_first_frame=true;
1236 cur_input_buf_omx=NULL;
1237 input_bufs_omx_mutex.unlock();
1239 if (!video->CommandFinished(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port /*codec*/)) {
1246 int AudioOMX::DestroyInputBufsOMX() //call with clock mutex locked
1248 OMX_ERRORTYPE error;
1250 cur_input_buf_omx=NULL;
1251 input_bufs_omx_mutex.lock();
1252 for (UINT i=0; i< input_bufs_omx_all.size();i++) {
1253 error=OMX_FreeBuffer(omx_aud_rend/*dec*/,omx_rend_input_port/*codec*/,input_bufs_omx_all[i]);
1254 if (error!=OMX_ErrorNone){
1255 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1256 input_bufs_omx_mutex.unlock();
1261 input_bufs_omx_all.clear();
1262 input_bufs_omx_free.clear();
1263 input_bufs_omx_mutex.unlock();
1268 int AudioOMX::DestroyInputBufsOMXwhilePlaying() //call with clock mutex locked
1270 //OMX_ERRORTYPE error;
1272 cur_input_buf_omx=NULL;
1273 input_bufs_omx_mutex.lock();
1274 while (input_bufs_omx_all.size()>0) {
1275 if (input_bufs_omx_free.size()>0) {
1276 // Destroy one buffer
1277 std::vector<OMX_BUFFERHEADERTYPE*>::iterator itty=input_bufs_omx_all.begin();
1278 OMX_BUFFERHEADERTYPE* cur_buf=input_bufs_omx_free.front();
1279 for (; itty!= input_bufs_omx_all.end();itty++) {
1280 if ((*itty)==cur_buf) {
1281 input_bufs_omx_all.erase(itty);
1282 input_bufs_omx_free.pop_front();
1287 input_bufs_omx_mutex.unlock();
1289 input_bufs_omx_mutex.lock();
1293 Log::getInstance()->log("Audio", Log::DEBUG, "DestroyInputBufsOMXwhilePlaying %d %d", input_bufs_omx_all.size(),input_bufs_omx_free.size());
1294 input_bufs_omx_mutex.unlock();
1299 int AudioOMX::DeAllocateCodecsOMX()
1301 OMX_ERRORTYPE error;
1303 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
1304 Log::getInstance()->log("Audio", Log::DEBUG, "enter deallocatecodecsomx");
1307 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 1");
1308 if (cur_input_buf_omx) {
1309 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1310 error=video->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,cur_input_buf_omx);
1311 if (error!=OMX_ErrorNone) {
1312 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_EmptyThisBuffer 6 failed %x", error);
1315 cur_input_buf_omx=NULL;//write out old data
1317 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 2");
1320 if (omx_aud_rend/*dec*/) {
1321 // first stop the omx elements
1322 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1323 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1325 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 3");
1327 video->UnlockClock();
1328 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 4");
1330 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 5");
1333 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1334 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend ChangeComponentState");
1337 // TODO proper deinit sequence
1338 // first flush all buffers
1340 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
1341 if (error!=OMX_ErrorNone) {
1342 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend in failed %x", error);
1346 /* error=OMX_SendCommand(omx_aud_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
1347 if (error!=OMX_ErrorNone){
1348 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1353 /* if (!video->CommandFinished(omx_aud_dec,OMX_CommandFlush,omx_codec_input_port)) {
1354 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd codec input failed");
1359 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1360 if (error!=OMX_ErrorNone){
1361 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1365 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_clock_port, NULL);
1366 if (error!=OMX_ErrorNone){
1367 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend clock failed %x", error);
1370 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6");
1372 if (!video->CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1373 !video->CommandFinished(omx_aud_rend,OMX_CommandFlush,omx_rend_clock_port)) {
1374 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd clock shed failed");
1377 DestroyInputBufsOMX(); //We have to make sure that no buffers are in use
1378 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6a");
1379 DeinitDecoderLibAV();
1380 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 7");
1383 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true)) {
1384 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 1");
1387 /* if (!video->DisablePort(omx_aud_dec,omx_codec_output_port,true)) {
1388 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 6");
1391 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port,true)) {
1392 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 7");
1396 if (!video->DisablePort(omx_aud_rend,omx_rend_clock_port,true)) {
1397 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 4");
1400 if (!video->DisablePort(omx_clock,omx_clock_output_port,true)) {
1401 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 5");
1406 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,NULL,NULL);
1407 if (error!=OMX_ErrorNone) {
1408 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1414 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_input_port,NULL,0);
1415 if (error!=OMX_ErrorNone) {
1416 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1420 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,0);
1421 if (error!=OMX_ErrorNone) {
1422 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1426 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_clock_port,NULL,0);
1427 if (error!=OMX_ErrorNone) {
1428 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1431 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 8");
1434 //error=OMX_FreeHandle(omx_aud_dec);
1435 error=OMX_FreeHandle(omx_aud_rend);
1436 video->UnlockClock();
1437 video->destroyClock();
1438 omx_aud_rend/*dec*/=NULL;
1439 if (error!=OMX_ErrorNone) {
1440 Log::getInstance()->log("Audio", Log::DEBUG, "FreeHandle failed %d", error);
1444 video->UnlockClock();
1445 DeinitDecoderLibAV();
1447 Log::getInstance()->log("Audio", Log::DEBUG, "leave deallocate codecs OMX");
1454 int AudioOMX::stop()
1456 if (!initted) return 0;
1458 Log::getInstance()->log("Audio", Log::DEBUG, "Audio stop called");
1459 DeAllocateCodecsOMX();
1460 //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1464 int AudioOMX::mute() {
1467 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
1468 VideoOMX* vw = static_cast<VideoOMX*>(Video::getInstance());
1472 OMX_AUDIO_CONFIG_MUTETYPE amute;
1473 memset(&amute, 0, sizeof(amute));
1474 amute.nSize = sizeof(amute);
1475 amute.nVersion.nVersion = OMX_VERSION;
1476 amute.nPortIndex = omx_rend_input_port;
1477 amute.bMute = OMX_TRUE;
1478 OMX_ERRORTYPE error= OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1480 if (error != OMX_ErrorNone) {
1481 Log::getInstance()->log("Audio", Log::DEBUG,
1482 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1483 omx_rend_input_port);
1495 int AudioOMX::unMute()
1497 if (!initted) return 0;
1499 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
1500 VideoOMX* vw = static_cast<VideoOMX*>(Video::getInstance());
1504 OMX_AUDIO_CONFIG_MUTETYPE amute;
1505 memset(&amute, 0, sizeof(amute));
1506 amute.nSize = sizeof(amute);
1507 amute.nVersion.nVersion = OMX_VERSION;
1508 amute.nPortIndex = omx_rend_input_port;
1509 amute.bMute = OMX_FALSE;
1510 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1512 if (error != OMX_ErrorNone) {
1513 Log::getInstance()->log("Audio", Log::DEBUG,
1514 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1515 omx_rend_input_port);
1527 int AudioOMX::pause()
1529 if (!initted) return 0;
1530 VideoOMX* vw = static_cast<VideoOMX*>(Video::getInstance());
1535 int AudioOMX::unPause()
1537 if (!initted) return 0;
1538 VideoOMX* vw = static_cast<VideoOMX*>(Video::getInstance());
1543 int AudioOMX::reset()
1545 if (!initted) return 0;
1547 Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
1548 DeAllocateCodecsOMX();
1550 // if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1551 // Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
1552 // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
1558 int AudioOMX::setVolume(int tvolume)
1560 // parameter: 0 for silence, 20 for full
1561 if ((tvolume < 0) || (tvolume > 20)) return 0;
1563 // volume = 2 * (20 - volume);
1564 // Right, that one was rubbish... 0-10 were almost
1565 // inaudible, 11-20 did what should have been done
1566 // over the whole 0-20 range
1569 VideoOMX* vw = static_cast<VideoOMX*>(Video::getInstance());
1574 OMX_AUDIO_CONFIG_VOLUMETYPE avol;
1575 memset(&avol, 0, sizeof(avol));
1576 avol.nSize = sizeof(avol);
1577 avol.nVersion.nVersion = OMX_VERSION;
1578 avol.nPortIndex = omx_rend_input_port;
1579 avol.bLinear=OMX_FALSE;
1580 avol.sVolume.nValue =(volume-20)*200;
1581 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioVolume,
1583 if (error != OMX_ErrorNone) {
1584 Log::getInstance()->log("Audio", Log::DEBUG,
1585 "Set OMX_IndexConfigAudioVolume failed %x %d", error,
1586 omx_rend_input_port);
1598 int AudioOMX::test()
1601 // return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
1603 /* aud_sync_parms_t a;
1607 // int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
1610 /*OK*/ //printf("Audio sync disable = %i\n", b);
1618 unsigned int AudioOMX::AdvanceMpAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1620 if (size<=2) return size; // silly;
1621 unsigned int atest=0;
1623 //inspired by libav parsing code
1624 while (atest+1<size) {
1625 if (data[atest]==0xFF && (data[atest+1] &0xe0)==0xe0) {
1626 if ((data[atest+1] & 0x18) == 0x08) {atest++;continue;} //sanity check: mpeg version ID 01 -> reserved
1627 //sanity check inspired by libav
1630 const int sample_rates[4]={44100,48000,32000,0};
1631 const short bitrate_tab[2][3][15] = { { { 0, 32, 64, 96, 128, 160,
1632 192, 224, 256, 288, 320, 352, 384, 416, 448 }, { 0, 32, 48,
1633 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 }, {
1634 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
1635 256, 320 } }, { { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1636 160, 176, 192, 224, 256 }, { 0, 8, 16, 24, 32, 40, 48, 56,
1637 64, 80, 96, 112, 128, 144, 160 }, { 0, 8, 16, 24, 32, 40,
1638 48, 56, 64, 80, 96, 112, 128, 144, 160 } } };
1641 int layer=4-((data[atest+1]&0x06)>>1);
1642 if (layer==4) {atest++;continue;} //sanity check
1644 int bitrate_index=(data[atest+2]&0xf0)>>4;
1645 if (bitrate_index==0x0f || bitrate_index==0x0) {atest++;continue;} //sanity check
1647 int samplerate_index=(data[atest+2]&0x0C)>>2;
1648 if (samplerate_index==0x03) {atest++;continue;} //sanity check
1650 int padding=(data[atest+2]&2)>>1;
1651 if (0x10 & data[atest+1]) {
1652 lsf=((data[atest+1]) &0x8)?0:1;
1658 int sample_rate=sample_rates[ samplerate_index]>>(lsf+mpeg2);
1660 int temp_frame_size=bitrate_tab[lsf][layer - 1][bitrate_index];
1662 frame_size=(temp_frame_size*12000)/sample_rate;
1663 frame_size=(frame_size+padding)*4;
1664 } else if (layer==2) {
1665 frame_size=(temp_frame_size*144000)/sample_rate;
1666 frame_size=frame_size+padding;
1668 frame_size=(temp_frame_size*144000)/(sample_rate<<lsf);
1669 frame_size=frame_size+padding;
1671 unsigned int sameheadertest=(data[atest]<<24)|(data[atest+1]<<16) |(data[atest+2]<<8);
1672 const unsigned mask=(0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)); // from libav
1673 if (!mp3sameheader) {
1674 mp3sameheader=sameheadertest;
1675 mp3sameheadercount=2;
1677 if ((mp3sameheader& mask)== (sameheadertest &mask)) mp3sameheadercount++;
1678 else mp3sameheadercount=0;
1679 mp3sameheader=sameheadertest;
1681 //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);
1682 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME DIAG: %x %x %x %d",data[atest],data[atest+1],data[atest+2],mp3sameheadercount);
1684 if (mp3sameheadercount>4) {
1685 *framesize=frame_size;
1686 return atest; // probably FrameSync
1687 } //skip it if the header changes too frequently
1694 unsigned int AudioOMX::AdvanceAc3AudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1696 if (size<=4) return size; // silly;
1697 const int frm_size_tab[] = { 64, 64, 80, 80, 96, 96, 112, 112, 128, 128,
1698 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384, 384, 448,
1699 448, 512, 512, 640, 640, 768, 768, 896, 896, 1024, 1024, 1152,
1700 1152, 1280, 1280, };
1701 unsigned int atest=0;
1702 *framesize=20000; //if we do not find a start code do not decompress
1703 while (atest+4<size) {
1704 if (data[atest]==0x0B && data[atest+1]==0x77) {
1705 // now figure out the length of the frame
1706 unsigned char code=data[atest+4];
1707 unsigned char fscod=(code& 0xC0)>>6;
1708 unsigned char frmsize=(code &0x3f);
1709 if (fscod!=0) Log::getInstance()->log("Audio", Log::DEBUG, "warning we only support 48 KHz sampling rate");
1710 *framesize=frm_size_tab[frmsize]*2;
1711 return atest; // probably FrameSync
1718 unsigned int AudioOMX::AdvanceAacLatmAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1720 if (size<=4) return size; // silly;
1721 unsigned int atest=0;
1722 *framesize=20000; //if we do not find a start code do not decompress
1723 while (atest+4<size) {
1724 if (data[atest] ==0x56 && (data[atest+1]& 0xe0)==0xe0) {
1725 // now figure out the length of the frame
1726 unsigned int length= ((0x1f & data[atest+1])<<8) | data[atest+2];
1727 *framesize=length+3;
1728 return atest; // probably FrameSync
1736 void AudioOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1738 packet = mplist.front();
1741 UINT AudioOMX::DeliverMediaSample(UCHAR* buffer, UINT* samplepos) {
1742 DeliverMediaPacket(packet, buffer, samplepos);
1743 if (*samplepos == packet.length) {
1751 long long AudioOMX::correctAudioLatency(long long pts,int addsamples,int srate)
1753 VideoOMX* video = static_cast<VideoOMX*>(Video::getInstance());
1755 OMX_PARAM_U32TYPE audio_lat;
1756 OMX_ERRORTYPE error;
1757 memset(&audio_lat, 0, sizeof(audio_lat));
1758 audio_lat.nSize = sizeof(audio_lat);
1759 audio_lat.nVersion.nVersion = OMX_VERSION;
1760 audio_lat.nPortIndex = omx_rend_input_port;
1762 error = OMX_GetConfig(omx_aud_rend, OMX_IndexConfigAudioRenderingLatency,
1764 video->UnlockClock();
1765 if (error != OMX_ErrorNone) {
1766 Log::getInstance()->log("Audio", Log::DEBUG,
1767 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
1768 omx_rend_input_port);
1769 return pts; // no correction in case of error
1771 /*Log::getInstance()->log("Audio", Log::DEBUG, "Current audio latency %d",
1774 long long workpts=0;
1775 workpts+=addsamples;
1776 workpts-=audio_lat.nU32;
1777 workpts*=10LL*1000LL*1000LL;
1778 workpts=workpts/((long long)srate); //one second /samplerate
1784 bool AudioOMX::DrainTargetBufferFull()
1786 //Check, if we have OMX output buffers
1788 input_bufs_omx_mutex.lock();
1789 full=(input_bufs_omx_free.size()==0);
1790 input_bufs_omx_mutex.unlock();
1796 UINT AudioOMX::DeliverMediaPacket(MediaPacket mpacket, const UCHAR* buffer,
1798 /*First Check, if we have an audio sample*/
1799 VideoOMX* vw = static_cast<VideoOMX*>(Video::getInstance());
1801 OMX_ERRORTYPE error;
1802 //Log *logger=Log::getInstance();
1803 if (vw->InIframemode()) {
1806 return 0; //Not in iframe mode!
1809 if (!omx_running) return 0; // if we are not runnig do not do this
1810 if (vw->isClockPaused()) return 0; //Block if we pause
1811 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 1");
1813 //Log::getInstance()->log("Audio", Log::DEBUG, "DeliverMediaPacketOMX time %lld",mpacket.presentation_time);
1815 /* if (mpacket.synched && mpacket.presentation_time <= 0) {
1816 *samplepos = mpacket.length;
1817 firstsynched = false;
1819 Log::getInstance()->log("Audio", Log::DEBUG,
1820 "DeliverMediaPacketOMX Frameskip");
1821 return mpacket.length;
1824 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 2");
1826 UINT headerstrip = 0;
1827 if (mpacket.disconti) {
1828 firstsynched = false;
1829 decompress_buffer_filled=0;
1830 if (cur_input_buf_omx) {
1831 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1833 if (error != OMX_ErrorNone) {
1834 Log::getInstance()->log("Audio", Log::DEBUG,
1835 "OMX_EmptyThisBuffer 1 failed %x", error);
1837 cur_input_buf_omx = NULL;
1841 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 3");
1842 if (mpacket.type != lastAType) {//Format Change //Push data out !
1843 firstsynched = false;
1846 Log::getInstance()->log("Audio", Log::DEBUG,"Notice audio type change %d %d", mpacket.type,lastAType);
1847 lastAType = mpacket.type;
1848 decompress_buffer_filled=0;
1850 if (cur_input_buf_omx) {
1851 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1853 if (error != OMX_ErrorNone) {
1854 Log::getInstance()->log("Audio", Log::DEBUG,
1855 "OMX_EmptyThisBuffer 2 failed %x", error);
1857 cur_input_buf_omx = NULL;
1862 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
1863 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
1865 if (!ChangeAudioPortConfig(true)) {
1866 Log::getInstance()->log("Audio", Log::DEBUG,
1867 "Changing audio port config failed", error);
1870 pthread_setcancelstate(oldcancelstate, NULL);
1871 pthread_setcanceltype(oldcanceltype, NULL);
1875 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 4");
1877 /*Inspect PES-Header */
1878 if (*samplepos == 0 && mpacket.type != MPTYPE_MPEG_AUDIO_LAYER3) {//stripheader
1879 headerstrip = buffer[mpacket.pos_buffer + 8] + 9;
1880 if (mpacket.type == MPTYPE_AC3)
1881 headerstrip += 4; //skip ac3 bytes
1882 *samplepos += headerstrip;
1883 if (mpacket.synched) {
1884 if (cur_input_buf_omx) {
1885 //cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1886 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1888 if (error != OMX_ErrorNone) {
1889 Log::getInstance()->log("Audio", Log::DEBUG,
1890 "OMX_EmptyThisBuffer 3 failed %x", error);
1892 //vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1894 cur_input_buf_omx = NULL;//write out old data
1896 firstsynched = true;
1897 //decompress_buffer_filled=0;
1899 if (!firstsynched) {//
1900 *samplepos = mpacket.length;//if we have not processed at least one
1901 decompress_buffer_filled=0;
1902 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 5");
1903 return mpacket.length;//synched packet ignore it!
1907 if (!cur_input_buf_omx) {
1908 input_bufs_omx_mutex.lock();
1909 if (input_bufs_omx_free.size()==0) {
1910 input_bufs_omx_mutex.unlock();
1911 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 6");
1912 //Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample");
1913 return 0; // we do not have a free media sample
1916 cur_input_buf_omx=input_bufs_omx_free.front();
1917 cur_input_buf_omx->nFilledLen=0;
1918 cur_input_buf_omx->nOffset=0;
1919 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
1920 input_bufs_omx_free.pop_front();
1921 input_bufs_omx_mutex.unlock();
1923 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 7");
1925 if (cur_input_buf_omx->nFilledLen == 0) {//will only be changed on first packet
1926 if (mpacket.synched) {
1927 //Log::getInstance()->log("Audio", Log::DEBUG,
1928 // "packet synched marker");
1930 //lastreftimePTS=mpacket.pts;
1931 if (omx_first_frame) { // TODO time
1932 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
1933 Log::getInstance()->log("Audio", Log::DEBUG, "Starttime");
1934 omx_first_frame = false;
1936 cur_input_buf_omx->nFlags = 0;
1937 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
1939 lastreftimeOMX = mpacket.presentation_time;
1940 //Log::getInstance()->log("Audio", Log::DEBUG,
1941 // "Time code %lld pts %lld dts %lld", lastreftimeOMX, mpacket.pts,mpacket.dts);
1942 lastreftimePTS = mpacket.pts;
1943 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
1945 // Log::getInstance()->log("Audio", Log::DEBUG,
1946 // "packet NOT synched marker");
1947 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
1948 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
1951 if (mpacket.disconti || achange) {
1952 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_DISCONTINUITY;
1953 //mp23codec_context_libav->frame_size=-1;
1954 //ac3codec_context_libav->frame_size=-1;
1958 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 8");
1960 if (*samplepos>mpacket.length) *samplepos=0; //propably the thread got interrupted and sample is not valid any more!
1961 unsigned int haveToCopy=mpacket.length-*samplepos;
1964 while (haveToCopy>0) {
1965 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 9");
1967 unsigned int gotframesize=0;
1969 switch (mpacket.type) {
1970 case MPTYPE_MPEG_AUDIO:
1971 case MPTYPE_MPEG_AUDIO_LAYER3: {
1972 adv = AdvanceMpAudioSync(buffer+mpacket.pos_buffer+*samplepos,
1973 haveToCopy,&gotframesize);
1977 case MPTYPE_AC3_PRE13: {
1978 adv = AdvanceAc3AudioSync(buffer+mpacket.pos_buffer+*samplepos,
1979 haveToCopy,&gotframesize);
1983 case MPTYPE_AAC_LATM: {
1984 adv = AdvanceAacLatmAudioSync(buffer+mpacket.pos_buffer+*samplepos,
1985 haveToCopy,&gotframesize);
1989 if (adv != (int)haveToCopy) {
1994 *samplepos=mpacket.length; //no ac3 sync byte
1995 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 10");
1996 return mpacket.length;
1999 // so everything is fine now do a memcpy
2000 int cancopy=min(haveToCopy,cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen);
2001 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+mpacket.pos_buffer+*samplepos,cancopy);
2002 haveToCopy-=cancopy;
2003 cur_input_buf_omx->nFilledLen+=cancopy;
2004 *samplepos+=cancopy;
2005 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 11");
2007 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2008 if (error != OMX_ErrorNone) {
2009 Log::getInstance()->log("Audio", Log::DEBUG,
2010 "OMX_EmptyThisBuffer 5 failed %x", error);
2012 cur_input_buf_omx=NULL;
2015 input_bufs_omx_mutex.lock();
2016 if (input_bufs_omx_free.size()==0) {
2017 input_bufs_omx_mutex.unlock();
2018 // Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample2");
2019 return *samplepos; // we do not have a free media sample
2021 cur_input_buf_omx=input_bufs_omx_free.front();
2022 cur_input_buf_omx->nFilledLen=0;
2023 cur_input_buf_omx->nOffset=0;
2024 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2025 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
2026 input_bufs_omx_free.pop_front();
2027 input_bufs_omx_mutex.unlock();
2029 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 12");
2032 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 13");
2040 AVCodecContext *current_context;
2041 switch (mpacket.type) {
2042 case MPTYPE_MPEG_AUDIO:
2043 case MPTYPE_MPEG_AUDIO_LAYER3: {
2044 current_context = mp23codec_context_libav;
2045 if (current_context->frame_size<0) framesize=1152; //Maximum framesize
2046 else framesize=current_context->frame_size;
2048 case MPTYPE_AAC_LATM: {
2049 current_context = aaclatmcodec_context_libav;
2052 case MPTYPE_AC3_PRE13: {
2053 current_context = ac3codec_context_libav;
2057 if (decompress_buffer_filled) { // have a remaining paket
2058 incoming_paket_libav.data =(uint8_t*) decompress_buffer;
2059 memcpy(decompress_buffer+decompress_buffer_filled,
2060 buffer+mpacket.pos_buffer+*samplepos,
2061 min(haveToCopy,decompress_buffer_size-decompress_buffer_filled));
2062 incoming_paket_libav.size = decompress_buffer_filled
2063 +min(haveToCopy,decompress_buffer_size-decompress_buffer_filled);
2064 //Log::getInstance()->log("Audio", Log::DEBUG,"Use saved audio buffer %d %d %d",mpacket.type,decompress_buffer_filled,mpacket.synched);
2066 incoming_paket_libav.data =(uint8_t*) buffer+mpacket.pos_buffer+*samplepos;
2067 incoming_paket_libav.size = haveToCopy;
2069 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 18");
2071 while (haveToCopy> 0 && errcount<3) {
2073 //Log::getInstance()->log("Audio", Log::DEBUG,"libav in %d %d",framesize,current_context->frame_size);
2074 //Log::getInstance()->log("Audio", Log::DEBUG, "libav in %d %d",
2075 // framesize, current_context->frame_size);
2077 bool donotdecompress=false;
2078 unsigned int gotframesize=0;
2079 // if (!decompress_buffer_filled) { // only do this if no old data is present
2081 switch (mpacket.type) {
2082 case MPTYPE_MPEG_AUDIO:
2083 case MPTYPE_MPEG_AUDIO_LAYER3: {
2084 adv = AdvanceMpAudioSync(incoming_paket_libav.data,
2085 incoming_paket_libav.size,&gotframesize);
2089 case MPTYPE_AC3_PRE13: {
2090 adv = AdvanceAc3AudioSync(incoming_paket_libav.data,
2091 incoming_paket_libav.size,&gotframesize);
2094 case MPTYPE_AAC_LATM: {
2095 adv = AdvanceAacLatmAudioSync(incoming_paket_libav.data,
2096 incoming_paket_libav.size,&gotframesize);
2100 if (adv > 0 /*&& !decompress_buffer_filled*/) {
2101 incoming_paket_libav.data += adv;
2102 incoming_paket_libav.size-=adv;
2105 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2106 else*/ framesize=haveToCopy;
2107 //Log::getInstance()->log("Audio", Log::DEBUG,"Advance by %d %d from %d",adv,mpacket.type,*samplepos );
2108 if (haveToCopy <= 0) {
2109 // Log::getInstance()->log("Audio", Log::DEBUG,"No sync code in packet remove %d",mpacket.type);
2110 *samplepos=mpacket.length;
2111 return mpacket.length;
2117 if (gotframesize>0 && gotframesize>haveToCopy) {
2118 donotdecompress=true;
2119 errcount=100; // exit loop
2121 // else Log::getInstance()->log("Audio", Log::DEBUG,"Loop run" );
2123 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 19");
2124 if (!donotdecompress) {
2127 pthread_testcancel();
2128 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2129 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2131 if (!omx_running || !mp23codec_context_libav
2132 || !ac3codec_context_libav) {
2133 libav_mutex.unlock();
2136 libav_mutex.unlock();
2137 // Log::getInstance()->log("Audio", Log::DEBUG,"libav out");
2138 int save_size=incoming_paket_libav.size;
2140 if ((int)gotframesize <= incoming_paket_libav.size) {
2141 if (gotframesize>0) incoming_paket_libav.size=gotframesize;
2142 len = avcodec_decode_audio4(current_context, decode_frame_libav,
2143 &gotta, &incoming_paket_libav);
2145 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:E %d %d",gotframesize,incoming_paket_libav.size);
2149 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:T %d",len);
2150 incoming_paket_libav.size=save_size;
2151 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out1");
2152 pthread_setcancelstate(oldcancelstate, NULL);
2153 pthread_setcanceltype(oldcanceltype, NULL);
2154 pthread_testcancel();
2160 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out2");
2161 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 20");
2163 libav_mutex.unlock();
2167 if (decompress_buffer_filled) { // reset to normal decoding
2169 //Log::getInstance()->log("Audio", Log::DEBUG,"saved audio: %d",len);
2170 haveToCopy -= min(len-decompress_buffer_filled,0);
2171 *samplepos += min(len-decompress_buffer_filled,0);
2172 //if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2173 /*else*/ framesize=haveToCopy;
2175 framesize=haveToCopy;
2177 incoming_paket_libav.data =(uint8_t*) buffer+mpacket.pos_buffer+*samplepos;
2179 decompress_buffer_filled=0;
2183 incoming_paket_libav.data += len;
2187 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2188 else*/framesize=haveToCopy;
2191 framesize=haveToCopy;
2194 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 21");
2196 incoming_paket_libav.size =framesize;
2198 //Log::getInstance()->log("Audio", Log::DEBUG,
2201 int dsize = av_samples_get_buffer_size(NULL,
2202 /*current_context->channels*/2, decode_frame_libav->nb_samples,
2203 AV_SAMPLE_FMT_S16, 1);
2204 /* int dsize_in = av_samples_get_buffer_size(NULL,
2205 current_context->channels, decode_frame_libav->nb_samples,
2206 current_context->sample_fmt, 1); -- unused */
2207 //if (current_context->channels==1) dsize*=2; // we convert mono to stereo
2208 if ((cur_input_buf_omx->nFilledLen + dsize)
2209 > cur_input_buf_omx->nAllocLen ) {
2210 // I doubt that this will ever happen
2211 // Log::getInstance()->log("Audio", Log::DEBUG,
2212 // "P 2 Time code %lld pts %lld", lastreftimeOMX, mpacket.pts);
2213 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
2215 if (error != OMX_ErrorNone) {
2216 Log::getInstance()->log("Audio", Log::DEBUG,
2217 "OMX_EmptyThisBuffer 4 failed %x", error);
2219 cur_input_buf_omx = NULL;
2221 if (!cur_input_buf_omx) {
2223 while (count < 10 && omx_running) {
2225 input_bufs_omx_mutex.lock();
2226 if (input_bufs_omx_free.size() == 0) {
2227 input_bufs_omx_mutex.unlock();
2228 // Log::getInstance()->log("Audio", Log::DEBUG,
2229 // "Deliver MediaPacket no free sample");
2231 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark22");
2232 if (!omx_running) return *samplepos;
2233 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 23");
2236 cur_input_buf_omx = input_bufs_omx_free.front();
2237 cur_input_buf_omx->nFilledLen = 0;
2238 cur_input_buf_omx->nOffset = 0;
2239 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
2240 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2241 input_bufs_omx_free.pop_front();
2242 input_bufs_omx_mutex.unlock();
2245 if (!cur_input_buf_omx) return *samplepos;
2250 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy in %d %d %d" ,dsize,current_context->sample_rate,cur_input_buf_omx->nFilledLen);
2253 av_opt_set_int(resam_con_libav, "in_sample_rate",decode_frame_libav->sample_rate,0);
2254 av_opt_set_int(resam_con_libav, "in_sample_fmt",decode_frame_libav->format,0);
2255 av_opt_set_int(resam_con_libav, "in_channel_layout",decode_frame_libav->channel_layout, 0);
2256 //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);
2257 //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);
2259 int ret=avresample_open(resam_con_libav);
2261 Log::getInstance()->log("Audio", Log::ERR,"Opening AV resample failed %d",ret);
2263 uint8_t *output=cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen;
2265 avresample_convert(resam_con_libav, &output,
2266 dsize, decode_frame_libav->nb_samples,
2267 decode_frame_libav->extended_data, decode_frame_libav->linesize[0], decode_frame_libav->nb_samples);
2268 avresample_close(resam_con_libav);
2271 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy out");
2272 cur_input_buf_omx->nFilledLen += dsize;
2274 //Log::getInstance()->log("Audio", Log::DEBUG,"Incomplete mpeg frames in pes packet %d %d",incoming_paket_libav.size,mpacket.length);
2275 /* uint8_t a1=incoming_paket_libav.data[0];
2276 uint8_t a2=incoming_paket_libav.data[1];
2277 uint8_t a3=incoming_paket_libav.data[2];
2278 uint8_t a4=incoming_paket_libav.data[3];*/
2279 // Log::getInstance()->log("Audio", Log::DEBUG,"Header %x %x %x %x",a1,a2,
2284 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 24");
2285 decompress_buffer_filled=0;
2287 //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);
2288 memcpy(decompress_buffer,incoming_paket_libav.data,min(haveToCopy,decompress_buffer_size));
2290 decompress_buffer_filled=min(haveToCopy,decompress_buffer_size);
2294 if (cur_input_buf_omx->nFilledLen) {
2295 //Log::getInstance()->log("Audio", Log::DEBUG,
2296 // "P 3 Time code %lld pts %lld", lastreftimeOMX, mpacket.pts);
2297 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2298 if (error != OMX_ErrorNone) {
2299 Log::getInstance()->log("Audio", Log::DEBUG,
2300 "OMX_EmptyThisBuffer 5 failed %x", error);
2302 //if (mpacket.synched) vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
2303 cur_input_buf_omx = NULL;
2308 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 25");
2310 *samplepos=mpacket.length;
2311 return mpacket.length;
2315 long long AudioOMX::SetStartOffset(long long curreftime, bool *rsync)
2317 VideoOMX* vw = dynamic_cast<VideoOMX*>(Video::getInstance());
2318 return vw->SetStartAudioOffset(curreftime,rsync);
2321 void AudioOMX::ResetTimeOffsets()
2323 VideoOMX* vw = dynamic_cast<VideoOMX*>(Video::getInstance());
2324 vw->ResetTimeOffsets();