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, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "woptionpane.h"
27 #include "osdopenvg.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/opt.h"
41 lastAType = MPTYPE_MPEG_AUDIO;
47 canpass_pcm_mch=false;
49 prefered_ac3=0; //0 stereo PCM, 1 passthrough 2 Multichannel PCM
57 omx_aud_rend/*dec*/=0;
58 cur_input_buf_omx=NULL;
61 ac3codec_context_libav=NULL;
64 mp23codec_context_libav=NULL;
70 decompress_buffer=NULL;
71 decompress_buffer_size=0;
72 decompress_buffer_filled=0;
76 strcpy(L_VPE_OMX_AUDIO_REND, VPE_OMX_AUDIO_REND);
83 int AudioOMX::init(UCHAR tstreamType) {
88 streamType = tstreamType;
90 if (!initAllParams()) {
97 decompress_buffer_size=20000;
98 decompress_buffer=(UCHAR*)malloc(decompress_buffer_size);
99 decompress_buffer_filled=0;
101 #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100)
105 av_log_set_flags(AV_LOG_SKIP_REPEATED);
107 ac3codec_libav = avcodec_find_decoder(AV_CODEC_ID_AC3);
108 if (ac3codec_libav == NULL) {
109 Log::getInstance()->log("Audio", Log::DEBUG,
110 "Find libav ac3 decoder failed");
114 mp23codec_libav = avcodec_find_decoder(AV_CODEC_ID_MP3);
115 if (mp23codec_libav == NULL) {
116 Log::getInstance()->log("Audio", Log::DEBUG,
117 "Find libav mpeg audio decoder failed");
121 aaclatmcodec_libav = avcodec_find_decoder(AV_CODEC_ID_AAC_LATM);
122 if (aaclatmcodec_libav == NULL) {
123 Log::getInstance()->log("Audio", Log::DEBUG,
124 "Find libav aac latm decoder failed");
130 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eMPEG1,2,EDID_AudioSampleRate_e48KHz,0);
134 Log::getInstance()->log("Audio", Log::NOTICE,
135 "TV hdmi supports mpeg1 layer 1 and 2");
137 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eMP3,2,EDID_AudioSampleRate_e48KHz,0);
141 Log::getInstance()->log("Audio", Log::NOTICE,
142 "TV hdmi supports mpeg1 layer 3");
145 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eAC3,6,EDID_AudioSampleRate_e48KHz,0);
148 Log::getInstance()->log("Audio", Log::NOTICE,
149 "TV hdmi supports AC3");
151 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eAAC,6,EDID_AudioSampleRate_e48KHz,0);
155 Log::getInstance()->log("Audio", Log::NOTICE,
156 "TV hdmi supports AAC");
159 canpass_pcm_mch=false;
164 int AudioOMX::initAllParams()
166 return (setStreamType(streamType) && setChannel() && setSource());
169 int AudioOMX::shutdown()
171 if (!initted) return 0;
174 Log::getInstance()->log("Audio", Log::DEBUG, "audio shutdown called");
175 DeAllocateCodecsOMX();
177 free(decompress_buffer);
178 decompress_buffer=NULL;
179 decompress_buffer_size=0;
180 decompress_buffer_filled=0;
185 bool AudioOMX::loadOptionsfromServer(VDR* vdr)
187 Log::getInstance()->log("Audio", Log::DEBUG, "AudioOMX config load");
188 char *name=vdr->configLoad("AudioOMX","AC3DecodingMode");
191 if (STRCASECMP(name, "PCM") == 0) {
193 } else if (STRCASECMP(name, "Passthrough") == 0) {
195 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
201 name = vdr->configLoad("AudioOMX", "Mp2DecodingMode");
204 if (STRCASECMP(name, "PCM") == 0) {
206 } else if (STRCASECMP(name, "Passthrough") == 0) {
208 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
214 name = vdr->configLoad("AudioOMX", "AACDecodingMode");
217 if (STRCASECMP(name, "PCM") == 0) {
219 } else if (STRCASECMP(name, "Passthrough") == 0) {
221 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
227 name = vdr->configLoad("AudioOMX", "Mp3DecodingMode");
230 if (STRCASECMP(name, "PCM") == 0) {
232 } else if (STRCASECMP(name, "Passthrough") == 0) {
234 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
240 name = vdr->configLoad("AudioOMX", "AudioOutput");
243 if (STRCASECMP(name, "analog") == 0) {
245 } else if (STRCASECMP(name, "HDMI") == 0) {
256 bool AudioOMX::handleOptionChanges(Option* option)
258 if (Audio::handleOptionChanges(option))
260 switch (option->id) {
262 if (STRCASECMP(option->options[option->userSetChoice], "analog") == 0) {
264 } else if (STRCASECMP(option->options[option->userSetChoice], "HDMI")
272 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
274 } else if (STRCASECMP(option->options[option->userSetChoice],
275 "Passthrough") == 0) {
277 } else if (STRCASECMP(option->options[option->userSetChoice],
278 "PCMMultichannel") == 0) {
284 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
286 } else if (STRCASECMP(option->options[option->userSetChoice],
287 "Passthrough") == 0) {
289 } else if (STRCASECMP(option->options[option->userSetChoice],
290 "PCMMultichannel") == 0) {
296 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
298 } else if (STRCASECMP(option->options[option->userSetChoice],
299 "Passthrough") == 0) {
301 } else if (STRCASECMP(option->options[option->userSetChoice],
302 "PCMMultichannel") == 0) {
308 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
310 } else if (STRCASECMP(option->options[option->userSetChoice],
311 "Passthrough") == 0) {
313 } else if (STRCASECMP(option->options[option->userSetChoice],
314 "PCMMultichannel") == 0) {
324 bool AudioOMX::saveOptionstoServer()
327 switch (prefered_ac3) {
329 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode", "PCM");
332 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
336 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
341 switch (prefered_aac) {
343 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode", "PCM");
346 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
350 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
355 switch (prefered_mp2) {
357 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode", "PCM");
360 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
364 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
369 switch (prefered_mp3) {
371 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode", "PCM");
374 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
378 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
384 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "analog");
386 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "HDMI");
392 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
393 UINT numChoices, UINT defaultChoice, UINT startInt,
394 const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
396 bool AudioOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
398 if (!Audio::addOptionsToPanes(panenumber,options,pane)) return false;
405 static const char* audioopts[]={"analog","HDMI"};
406 option = new Option(4,tr("Audio Output"), "AudioOMX","AudioOutput",Option::TYPE_TEXT,2,0,0,audioopts,NULL,false,this);
407 options->push_back(option);
408 pane->addOptionLine(option);
411 char **ac3opts=new char *[3];
413 ac3opts[i]=new char[strlen("PCM")+1];
414 strcpy(ac3opts[i],"PCM");
417 ac3opts[i]=new char[strlen("Passthrough")+1];
418 strcpy(ac3opts[i],"PassThrough");
421 if (canpass_pcm_mch) {
422 ac3opts[i]=new char[strlen("PCMMultichannel")+1];
423 strcpy(ac3opts[i],"PCMMultichannel");
426 option = new Option(1 ,tr("AC3 HDMI Mode"), "AudioOMX", "AC3DecodingMode", Option::TYPE_TEXT, i, 0, 0, ac3opts,NULL,true, this);
427 options->push_back(option);
428 pane->addOptionLine(option);
430 /* char **aacopts = new char *[3];
432 aacopts[i] = new char[strlen("PCM") + 1];
433 strcpy(mp2opts[i], "PCM");
436 aacopts[i] = new char[strlen("Passthrough") + 1];
437 strcpy(aacopts[i], "PassThrough");
440 if (canpass_pcm_mch) {
441 aacopts[i] = new char[strlen("PCMMultichannel") + 1];
442 strcpy(aacopts[i], "PCMMultichannel");
445 option = new Option(5, tr("Mp2 HDMI Mode"), "AudioOMX",
446 "AACDecodingMode", Option::TYPE_TEXT, i, 0, 0,
447 aacopts, NULL, true, this);
448 options->push_back(option);
449 pane->addOptionLine(option);
452 char **mp2opts = new char *[3];
454 mp2opts[i] = new char[strlen("PCM") + 1];
455 strcpy(mp2opts[i], "PCM");
458 mp2opts[i] = new char[strlen("Passthrough") + 1];
459 strcpy(mp2opts[i], "PassThrough");
462 if (canpass_pcm_mch) {
463 mp2opts[i] = new char[strlen("PCMMultichannel") + 1];
464 strcpy(mp2opts[i], "PCMMultichannel");
467 option = new Option(2, tr("Mp2 HDMI Mode"), "AudioOMX",
468 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0,
469 mp2opts, NULL, true, this);
470 options->push_back(option);
471 pane->addOptionLine(option);
473 char **mp3opts = new char *[3];
475 mp3opts[i] = new char[strlen("PCM") + 1];
476 strcpy(mp3opts[i], "PCM");
479 mp3opts[i] = new char[strlen("Passthrough") + 1];
480 strcpy(mp3opts[i], "PassThrough");
483 if (canpass_pcm_mch) {
484 mp3opts[i] = new char[strlen("PCMMultichannel") + 1];
485 strcpy(mp3opts[i], "PCMMultichannel");
488 option = new Option(3, tr("Mp3 HDMI Mode"), "AudioOMX",
489 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0, mp3opts,
491 options->push_back(option);
492 pane->addOptionLine(option);*/
493 // Comment unsupported modes out
506 OMX_ERRORTYPE AudioOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
508 //Log::getInstance()->log("Audio", Log::NOTICE, "EmptyBufferDone");
509 AudioOMX *audio=(AudioOMX *)getInstance();
510 audio->ReturnEmptyOMXBuffer(buffer);
511 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=(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=(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 ((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=(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=(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");
1188 int AudioOMX::PrepareInputBufsOMX(bool setportdef) //needs to be called with locvke omx clock mutex
1190 VideoOMX *video=(VideoOMX*)Video::getInstance();
1191 OMX_ERRORTYPE error;
1192 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1193 memset(&port_def_type,0,sizeof(port_def_type));
1194 port_def_type.nSize=sizeof(port_def_type);
1195 port_def_type.nVersion.nVersion=OMX_VERSION;
1196 port_def_type.nPortIndex=omx_rend_input_port;//omx_codec_input_port;
1198 error=OMX_GetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1200 if (error!=OMX_ErrorNone){
1201 Log::getInstance()->log("Audio", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1206 port_def_type.nBufferCountActual=2;
1207 port_def_type.nBufferSize=max(port_def_type.nBufferSize,50000); // for transcoder important
1209 error=OMX_SetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1211 if (error!=OMX_ErrorNone){
1212 Log::getInstance()->log("Audio", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1217 error=OMX_SendCommand(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port/*codec*/,0);
1218 if (error!=OMX_ErrorNone){
1219 Log::getInstance()->log("Audio", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1223 input_bufs_omx_mutex.Lock();
1224 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1225 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1226 error=OMX_AllocateBuffer(omx_aud_rend/*dec*/,&buf_head,omx_rend_input_port/*codec*/,NULL,port_def_type.nBufferSize);
1227 if (error!=OMX_ErrorNone){
1228 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1229 input_bufs_omx_mutex.Unlock();
1232 input_bufs_omx_all.push_back(buf_head);
1233 input_bufs_omx_free.push_back(buf_head);
1235 omx_first_frame=true;
1238 cur_input_buf_omx=NULL;
1239 input_bufs_omx_mutex.Unlock();
1241 if (!video->CommandFinished(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port /*codec*/)) {
1248 int AudioOMX::DestroyInputBufsOMX() //call with clock mutex locked
1250 OMX_ERRORTYPE error;
1252 cur_input_buf_omx=NULL;
1253 input_bufs_omx_mutex.Lock();
1254 for (UINT i=0; i< input_bufs_omx_all.size();i++) {
1255 error=OMX_FreeBuffer(omx_aud_rend/*dec*/,omx_rend_input_port/*codec*/,input_bufs_omx_all[i]);
1256 if (error!=OMX_ErrorNone){
1257 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1258 input_bufs_omx_mutex.Unlock();
1263 input_bufs_omx_all.clear();
1264 input_bufs_omx_free.clear();
1265 input_bufs_omx_mutex.Unlock();
1270 int AudioOMX::DestroyInputBufsOMXwhilePlaying() //call with clock mutex locked
1272 //OMX_ERRORTYPE error;
1274 cur_input_buf_omx=NULL;
1275 input_bufs_omx_mutex.Lock();
1276 while (input_bufs_omx_all.size()>0) {
1277 if (input_bufs_omx_free.size()>0) {
1278 // Destroy one buffer
1279 vector<OMX_BUFFERHEADERTYPE*>::iterator itty=input_bufs_omx_all.begin();
1280 OMX_BUFFERHEADERTYPE* cur_buf=input_bufs_omx_free.front();
1281 for (; itty!= input_bufs_omx_all.end();itty++) {
1282 if ((*itty)==cur_buf) {
1283 input_bufs_omx_all.erase(itty);
1284 input_bufs_omx_free.pop_front();
1289 input_bufs_omx_mutex.Unlock();
1291 input_bufs_omx_mutex.Lock();
1295 Log::getInstance()->log("Audio", Log::DEBUG, "DestroyInputBufsOMXwhilePlaying %d %d", input_bufs_omx_all.size(),input_bufs_omx_free.size());
1296 input_bufs_omx_mutex.Unlock();
1301 int AudioOMX::DeAllocateCodecsOMX()
1303 OMX_ERRORTYPE error;
1305 VideoOMX *video=(VideoOMX*)Video::getInstance();
1306 Log::getInstance()->log("Audio", Log::DEBUG, "enter deallocatecodecsomx");
1310 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 1");
1311 if (cur_input_buf_omx) {
1312 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1313 error=video->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,cur_input_buf_omx);
1314 if (error!=OMX_ErrorNone) {
1315 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_EmptyThisBuffer 6 failed %x", error);
1318 cur_input_buf_omx=NULL;//write out old data
1320 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 2");
1323 if (omx_aud_rend/*dec*/) {
1324 // first stop the omx elements
1325 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1326 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1328 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 3");
1330 video->UnlockClock();
1331 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 4");
1333 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 5");
1336 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1337 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend ChangeComponentState");
1340 // TODO proper deinit sequence
1341 // first flush all buffers
1343 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
1344 if (error!=OMX_ErrorNone) {
1345 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend in failed %x", error);
1349 /* error=OMX_SendCommand(omx_aud_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
1350 if (error!=OMX_ErrorNone){
1351 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1356 /* if (!video->CommandFinished(omx_aud_dec,OMX_CommandFlush,omx_codec_input_port)) {
1357 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd codec input failed");
1362 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1363 if (error!=OMX_ErrorNone){
1364 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1368 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_clock_port, NULL);
1369 if (error!=OMX_ErrorNone){
1370 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend clock failed %x", error);
1373 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6");
1375 if (!video->CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1376 !video->CommandFinished(omx_aud_rend,OMX_CommandFlush,omx_rend_clock_port)) {
1377 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd clock shed failed");
1380 DestroyInputBufsOMX(); //We have to make sure that no buffers are in use
1381 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6a");
1382 DeinitDecoderLibAV();
1383 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 7");
1386 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true)) {
1387 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 1");
1390 /* if (!video->DisablePort(omx_aud_dec,omx_codec_output_port,true)) {
1391 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 6");
1394 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port,true)) {
1395 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 7");
1399 if (!video->DisablePort(omx_aud_rend,omx_rend_clock_port,true)) {
1400 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 4");
1403 if (!video->DisablePort(omx_clock,omx_clock_output_port,true)) {
1404 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 5");
1409 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,NULL,NULL);
1410 if (error!=OMX_ErrorNone) {
1411 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1417 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_input_port,NULL,0);
1418 if (error!=OMX_ErrorNone) {
1419 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1423 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,0);
1424 if (error!=OMX_ErrorNone) {
1425 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1429 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_clock_port,NULL,0);
1430 if (error!=OMX_ErrorNone) {
1431 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1434 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 8");
1437 //error=OMX_FreeHandle(omx_aud_dec);
1438 error=OMX_FreeHandle(omx_aud_rend);
1439 video->UnlockClock();
1440 video->destroyClock();
1441 omx_aud_rend/*dec*/=NULL;
1442 if (error!=OMX_ErrorNone) {
1443 Log::getInstance()->log("Audio", Log::DEBUG, "FreeHandle failed %d", error);
1447 video->UnlockClock();
1448 DeinitDecoderLibAV();
1450 Log::getInstance()->log("Audio", Log::DEBUG, "leave deallocate codecs OMX");
1457 int AudioOMX::stop()
1459 if (!initted) return 0;
1461 Log::getInstance()->log("Audio", Log::DEBUG, "Audio stop called");
1462 DeAllocateCodecsOMX();
1463 //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1467 int AudioOMX::mute() {
1470 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
1471 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1475 OMX_AUDIO_CONFIG_MUTETYPE amute;
1476 memset(&amute, 0, sizeof(amute));
1477 amute.nSize = sizeof(amute);
1478 amute.nVersion.nVersion = OMX_VERSION;
1479 amute.nPortIndex = omx_rend_input_port;
1480 amute.bMute = OMX_TRUE;
1481 OMX_ERRORTYPE error= OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1483 if (error != OMX_ErrorNone) {
1484 Log::getInstance()->log("Audio", Log::DEBUG,
1485 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1486 omx_rend_input_port);
1498 int AudioOMX::unMute()
1500 if (!initted) return 0;
1502 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
1503 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1507 OMX_AUDIO_CONFIG_MUTETYPE amute;
1508 memset(&amute, 0, sizeof(amute));
1509 amute.nSize = sizeof(amute);
1510 amute.nVersion.nVersion = OMX_VERSION;
1511 amute.nPortIndex = omx_rend_input_port;
1512 amute.bMute = OMX_FALSE;
1513 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1515 if (error != OMX_ErrorNone) {
1516 Log::getInstance()->log("Audio", Log::DEBUG,
1517 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1518 omx_rend_input_port);
1530 int AudioOMX::pause() {
1534 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1540 int AudioOMX::unPause()
1542 if (!initted) return 0;
1544 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1551 int AudioOMX::reset()
1553 if (!initted) return 0;
1555 Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
1556 DeAllocateCodecsOMX();
1558 // if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1559 // Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
1560 // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
1566 int AudioOMX::setVolume(int tvolume)
1568 // parameter: 0 for silence, 20 for full
1569 if ((tvolume < 0) || (tvolume > 20)) return 0;
1571 // volume = 2 * (20 - volume);
1572 // Right, that one was rubbish... 0-10 were almost
1573 // inaudible, 11-20 did what should have been done
1574 // over the whole 0-20 range
1578 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1582 OMX_AUDIO_CONFIG_VOLUMETYPE avol;
1583 memset(&avol, 0, sizeof(avol));
1584 avol.nSize = sizeof(avol);
1585 avol.nVersion.nVersion = OMX_VERSION;
1586 avol.nPortIndex = omx_rend_input_port;
1587 avol.bLinear=OMX_FALSE;
1588 avol.sVolume.nValue =(volume-20)*200;
1589 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioVolume,
1591 if (error != OMX_ErrorNone) {
1592 Log::getInstance()->log("Audio", Log::DEBUG,
1593 "Set OMX_IndexConfigAudioVolume failed %x %d", error,
1594 omx_rend_input_port);
1606 int AudioOMX::test()
1609 // return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
1611 /* aud_sync_parms_t a;
1615 // int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
1618 /*OK*/ //printf("Audio sync disable = %i\n", b);
1626 unsigned int AudioOMX::AdvanceMpAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1628 if (size<=2) return size; // silly;
1629 unsigned int atest=0;
1631 //inspired by libav parsing code
1632 while (atest+1<size) {
1633 if (data[atest]==0xFF && (data[atest+1] &0xe0)==0xe0) {
1634 if ((data[atest+1] & 0x18) == 0x08) {atest++;continue;} //sanity check: mpeg version ID 01 -> reserved
1635 //sanity check inspired by libav
1638 const int sample_rates[4]={44100,48000,32000,0};
1639 const short bitrate_tab[2][3][15] = { { { 0, 32, 64, 96, 128, 160,
1640 192, 224, 256, 288, 320, 352, 384, 416, 448 }, { 0, 32, 48,
1641 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 }, {
1642 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
1643 256, 320 } }, { { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1644 160, 176, 192, 224, 256 }, { 0, 8, 16, 24, 32, 40, 48, 56,
1645 64, 80, 96, 112, 128, 144, 160 }, { 0, 8, 16, 24, 32, 40,
1646 48, 56, 64, 80, 96, 112, 128, 144, 160 } } };
1649 int layer=4-((data[atest+1]&0x06)>>1);
1650 if (layer==4) {atest++;continue;} //sanity check
1652 int bitrate_index=(data[atest+2]&0xf0)>>4;
1653 if (bitrate_index==0x0f || bitrate_index==0x0) {atest++;continue;} //sanity check
1655 int samplerate_index=(data[atest+2]&0x0C)>>2;
1656 if (samplerate_index==0x03) {atest++;continue;} //sanity check
1658 int padding=(data[atest+2]&2)>>1;
1659 if (0x10 & data[atest+1]) {
1660 lsf=((data[atest+1]) &0x8)?0:1;
1666 int sample_rate=sample_rates[ samplerate_index]>>(lsf+mpeg2);
1668 int temp_frame_size=bitrate_tab[lsf][layer - 1][bitrate_index];
1670 frame_size=(temp_frame_size*12000)/sample_rate;
1671 frame_size=(frame_size+padding)*4;
1672 } else if (layer==2) {
1673 frame_size=(temp_frame_size*144000)/sample_rate;
1674 frame_size=frame_size+padding;
1676 frame_size=(temp_frame_size*144000)/(sample_rate<<lsf);
1677 frame_size=frame_size+padding;
1679 unsigned int sameheadertest=(data[atest]<<24)|(data[atest+1]<<16) |(data[atest+2]<<8);
1680 const unsigned mask=(0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)); // from libav
1681 if (!mp3sameheader) {
1682 mp3sameheader=sameheadertest;
1683 mp3sameheadercount=2;
1685 if ((mp3sameheader& mask)== (sameheadertest &mask)) mp3sameheadercount++;
1686 else mp3sameheadercount=0;
1687 mp3sameheader=sameheadertest;
1689 //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);
1690 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME DIAG: %x %x %x %d",data[atest],data[atest+1],data[atest+2],mp3sameheadercount);
1692 if (mp3sameheadercount>4) {
1693 *framesize=frame_size;
1694 return atest; // probably FrameSync
1695 } //skip it if the header changes too frequently
1702 unsigned int AudioOMX::AdvanceAc3AudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1704 if (size<=4) return size; // silly;
1705 const int frm_size_tab[] = { 64, 64, 80, 80, 96, 96, 112, 112, 128, 128,
1706 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384, 384, 448,
1707 448, 512, 512, 640, 640, 768, 768, 896, 896, 1024, 1024, 1152,
1708 1152, 1280, 1280, };
1709 unsigned int atest=0;
1710 *framesize=20000; //if we do not find a start code do not decompress
1711 while (atest+4<size) {
1712 if (data[atest]==0x0B && data[atest+1]==0x77) {
1713 // now figure out the length of the frame
1714 unsigned char code=data[atest+4];
1715 unsigned char fscod=(code& 0xC0)>>6;
1716 unsigned char frmsize=(code &0x3f);
1717 if (fscod!=0) Log::getInstance()->log("Audio", Log::DEBUG, "warning we only support 48 KHz sampling rate");
1718 *framesize=frm_size_tab[frmsize]*2;
1719 return atest; // probably FrameSync
1726 unsigned int AudioOMX::AdvanceAacLatmAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1728 if (size<=4) return size; // silly;
1729 unsigned int atest=0;
1730 *framesize=20000; //if we do not find a start code do not decompress
1731 while (atest+4<size) {
1732 if (data[atest] ==0x56 && (data[atest+1]& 0xe0)==0xe0) {
1733 // now figure out the length of the frame
1734 unsigned int length= ((0x1f & data[atest+1])<<8) | data[atest+2];
1735 *framesize=length+3;
1736 return atest; // probably FrameSync
1744 void AudioOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1746 packet = mplist.front();
1749 UINT AudioOMX::DeliverMediaSample(UCHAR* buffer, UINT* samplepos) {
1750 DeliverMediaPacket(packet, buffer, samplepos);
1751 if (*samplepos == packet.length) {
1759 long long AudioOMX::correctAudioLatency(long long pts,int addsamples,int srate) {
1761 VideoOMX *video = (VideoOMX*) Video::getInstance();
1763 OMX_PARAM_U32TYPE audio_lat;
1764 OMX_ERRORTYPE error;
1765 memset(&audio_lat, 0, sizeof(audio_lat));
1766 audio_lat.nSize = sizeof(audio_lat);
1767 audio_lat.nVersion.nVersion = OMX_VERSION;
1768 audio_lat.nPortIndex = omx_rend_input_port;
1770 error = OMX_GetConfig(omx_aud_rend, OMX_IndexConfigAudioRenderingLatency,
1772 video->UnlockClock();
1773 if (error != OMX_ErrorNone) {
1774 Log::getInstance()->log("Audio", Log::DEBUG,
1775 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
1776 omx_rend_input_port);
1777 return pts; // no correction in case of error
1779 /*Log::getInstance()->log("Audio", Log::DEBUG, "Current audio latency %d",
1782 long long workpts=0;
1783 workpts+=addsamples;
1784 workpts-=audio_lat.nU32;
1785 workpts*=10LL*1000LL*1000LL;
1786 workpts=workpts/((long long)srate); //one second /samplerate
1792 bool AudioOMX::DrainTargetBufferFull()
1794 //Check, if we have OMX output buffers
1796 input_bufs_omx_mutex.Lock();
1797 full=(input_bufs_omx_free.size()==0);
1798 input_bufs_omx_mutex.Unlock();
1804 UINT AudioOMX::DeliverMediaPacket(MediaPacket mpacket, const UCHAR* buffer,
1806 /*First Check, if we have an audio sample*/
1807 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1809 OMX_ERRORTYPE error;
1810 //Log *logger=Log::getInstance();
1811 if (vw->InIframemode()) {
1814 return 0; //Not in iframe mode!
1817 if (!omx_running) return 0; // if we are not runnig do not do this
1818 if (vw->isClockPaused()) return 0; //Block if we pause
1819 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 1");
1821 //Log::getInstance()->log("Audio", Log::DEBUG, "DeliverMediaPacketOMX time %lld",mpacket.presentation_time);
1823 /* if (mpacket.synched && mpacket.presentation_time <= 0) {
1824 *samplepos = mpacket.length;
1825 firstsynched = false;
1827 Log::getInstance()->log("Audio", Log::DEBUG,
1828 "DeliverMediaPacketOMX Frameskip");
1829 return mpacket.length;
1832 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 2");
1834 UINT headerstrip = 0;
1835 if (mpacket.disconti) {
1836 firstsynched = false;
1837 decompress_buffer_filled=0;
1838 if (cur_input_buf_omx) {
1839 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1841 if (error != OMX_ErrorNone) {
1842 Log::getInstance()->log("Audio", Log::DEBUG,
1843 "OMX_EmptyThisBuffer 1 failed %x", error);
1845 cur_input_buf_omx = NULL;
1849 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 3");
1850 if (mpacket.type != lastAType) {//Format Change //Push data out !
1851 firstsynched = false;
1854 Log::getInstance()->log("Audio", Log::DEBUG,"Notice audio type change %d %d", mpacket.type,lastAType);
1855 lastAType = mpacket.type;
1856 decompress_buffer_filled=0;
1858 if (cur_input_buf_omx) {
1859 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1861 if (error != OMX_ErrorNone) {
1862 Log::getInstance()->log("Audio", Log::DEBUG,
1863 "OMX_EmptyThisBuffer 2 failed %x", error);
1865 cur_input_buf_omx = NULL;
1870 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
1871 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
1873 if (!ChangeAudioPortConfig(true)) {
1874 Log::getInstance()->log("Audio", Log::DEBUG,
1875 "Changing audio port config failed", error);
1878 pthread_setcancelstate(oldcancelstate, NULL);
1879 pthread_setcanceltype(oldcanceltype, NULL);
1883 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 4");
1885 /*Inspect PES-Header */
1886 if (*samplepos == 0 && mpacket.type != MPTYPE_MPEG_AUDIO_LAYER3) {//stripheader
1887 headerstrip = buffer[mpacket.pos_buffer + 8] + 9;
1888 if (mpacket.type == MPTYPE_AC3)
1889 headerstrip += 4; //skip ac3 bytes
1890 *samplepos += headerstrip;
1891 if (mpacket.synched) {
1892 if (cur_input_buf_omx) {
1893 //cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1894 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1896 if (error != OMX_ErrorNone) {
1897 Log::getInstance()->log("Audio", Log::DEBUG,
1898 "OMX_EmptyThisBuffer 3 failed %x", error);
1900 //vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1902 cur_input_buf_omx = NULL;//write out old data
1904 firstsynched = true;
1905 //decompress_buffer_filled=0;
1907 if (!firstsynched) {//
1908 *samplepos = mpacket.length;//if we have not processed at least one
1909 decompress_buffer_filled=0;
1910 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 5");
1911 return mpacket.length;//synched packet ignore it!
1915 if (!cur_input_buf_omx) {
1916 input_bufs_omx_mutex.Lock();
1917 if (input_bufs_omx_free.size()==0) {
1918 input_bufs_omx_mutex.Unlock();
1919 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 6");
1920 //Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample");
1921 return 0; // we do not have a free media sample
1924 cur_input_buf_omx=input_bufs_omx_free.front();
1925 cur_input_buf_omx->nFilledLen=0;
1926 cur_input_buf_omx->nOffset=0;
1927 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
1928 input_bufs_omx_free.pop_front();
1929 input_bufs_omx_mutex.Unlock();
1931 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 7");
1933 if (cur_input_buf_omx->nFilledLen == 0) {//will only be changed on first packet
1934 if (mpacket.synched) {
1935 //Log::getInstance()->log("Audio", Log::DEBUG,
1936 // "packet synched marker");
1938 //lastreftimePTS=mpacket.pts;
1939 if (omx_first_frame) { // TODO time
1940 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
1941 Log::getInstance()->log("Audio", Log::DEBUG, "Starttime");
1942 omx_first_frame = false;
1944 cur_input_buf_omx->nFlags = 0;
1945 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
1947 lastreftimeOMX = mpacket.presentation_time;
1948 //Log::getInstance()->log("Audio", Log::DEBUG,
1949 // "Time code %lld pts %lld dts %lld", lastreftimeOMX, mpacket.pts,mpacket.dts);
1950 lastreftimePTS = mpacket.pts;
1951 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
1953 // Log::getInstance()->log("Audio", Log::DEBUG,
1954 // "packet NOT synched marker");
1955 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
1956 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
1959 if (mpacket.disconti || achange) {
1960 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_DISCONTINUITY;
1961 //mp23codec_context_libav->frame_size=-1;
1962 //ac3codec_context_libav->frame_size=-1;
1966 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 8");
1968 if (*samplepos>mpacket.length) *samplepos=0; //propably the thread got interrupted and sample is not valid any more!
1969 unsigned int haveToCopy=mpacket.length-*samplepos;
1972 while (haveToCopy>0) {
1973 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 9");
1975 unsigned int gotframesize=0;
1977 switch (mpacket.type) {
1978 case MPTYPE_MPEG_AUDIO:
1979 case MPTYPE_MPEG_AUDIO_LAYER3: {
1980 adv = AdvanceMpAudioSync(buffer+mpacket.pos_buffer+*samplepos,
1981 haveToCopy,&gotframesize);
1985 case MPTYPE_AC3_PRE13: {
1986 adv = AdvanceAc3AudioSync(buffer+mpacket.pos_buffer+*samplepos,
1987 haveToCopy,&gotframesize);
1991 case MPTYPE_AAC_LATM: {
1992 adv = AdvanceAacLatmAudioSync(buffer+mpacket.pos_buffer+*samplepos,
1993 haveToCopy,&gotframesize);
1997 if (adv != (int)haveToCopy) {
2002 *samplepos=mpacket.length; //no ac3 sync byte
2003 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 10");
2004 return mpacket.length;
2007 // so everything is fine now do a memcpy
2008 int cancopy=min(haveToCopy,cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen);
2009 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+mpacket.pos_buffer+*samplepos,cancopy);
2010 haveToCopy-=cancopy;
2011 cur_input_buf_omx->nFilledLen+=cancopy;
2012 *samplepos+=cancopy;
2013 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 11");
2015 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2016 if (error != OMX_ErrorNone) {
2017 Log::getInstance()->log("Audio", Log::DEBUG,
2018 "OMX_EmptyThisBuffer 5 failed %x", error);
2020 cur_input_buf_omx=NULL;
2023 input_bufs_omx_mutex.Lock();
2024 if (input_bufs_omx_free.size()==0) {
2025 input_bufs_omx_mutex.Unlock();
2026 // Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample2");
2027 return *samplepos; // we do not have a free media sample
2029 cur_input_buf_omx=input_bufs_omx_free.front();
2030 cur_input_buf_omx->nFilledLen=0;
2031 cur_input_buf_omx->nOffset=0;
2032 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2033 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
2034 input_bufs_omx_free.pop_front();
2035 input_bufs_omx_mutex.Unlock();
2037 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 12");
2040 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 13");
2048 AVCodecContext *current_context;
2049 switch (mpacket.type) {
2050 case MPTYPE_MPEG_AUDIO:
2051 case MPTYPE_MPEG_AUDIO_LAYER3: {
2052 current_context = mp23codec_context_libav;
2053 if (current_context->frame_size<0) framesize=1152; //Maximum framesize
2054 else framesize=current_context->frame_size;
2056 case MPTYPE_AAC_LATM: {
2057 current_context = aaclatmcodec_context_libav;
2060 case MPTYPE_AC3_PRE13: {
2061 current_context = ac3codec_context_libav;
2065 if (decompress_buffer_filled) { // have a remaining paket
2066 incoming_paket_libav.data =(uint8_t*) decompress_buffer;
2067 memcpy(decompress_buffer+decompress_buffer_filled,
2068 buffer+mpacket.pos_buffer+*samplepos,
2069 min(haveToCopy,decompress_buffer_size-decompress_buffer_filled));
2070 incoming_paket_libav.size = decompress_buffer_filled
2071 +min(haveToCopy,decompress_buffer_size-decompress_buffer_filled);
2072 //Log::getInstance()->log("Audio", Log::DEBUG,"Use saved audio buffer %d %d %d",mpacket.type,decompress_buffer_filled,mpacket.synched);
2074 incoming_paket_libav.data =(uint8_t*) buffer+mpacket.pos_buffer+*samplepos;
2075 incoming_paket_libav.size = haveToCopy;
2077 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 18");
2079 while (haveToCopy> 0 && errcount<3) {
2081 //Log::getInstance()->log("Audio", Log::DEBUG,"libav in %d %d",framesize,current_context->frame_size);
2082 //Log::getInstance()->log("Audio", Log::DEBUG, "libav in %d %d",
2083 // framesize, current_context->frame_size);
2085 bool donotdecompress=false;
2086 unsigned int gotframesize=0;
2087 // if (!decompress_buffer_filled) { // only do this if no old data is present
2089 switch (mpacket.type) {
2090 case MPTYPE_MPEG_AUDIO:
2091 case MPTYPE_MPEG_AUDIO_LAYER3: {
2092 adv = AdvanceMpAudioSync(incoming_paket_libav.data,
2093 incoming_paket_libav.size,&gotframesize);
2097 case MPTYPE_AC3_PRE13: {
2098 adv = AdvanceAc3AudioSync(incoming_paket_libav.data,
2099 incoming_paket_libav.size,&gotframesize);
2102 case MPTYPE_AAC_LATM: {
2103 adv = AdvanceAacLatmAudioSync(incoming_paket_libav.data,
2104 incoming_paket_libav.size,&gotframesize);
2108 if (adv > 0 /*&& !decompress_buffer_filled*/) {
2109 incoming_paket_libav.data += adv;
2110 incoming_paket_libav.size-=adv;
2113 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2114 else*/ framesize=haveToCopy;
2115 //Log::getInstance()->log("Audio", Log::DEBUG,"Advance by %d %d from %d",adv,mpacket.type,*samplepos );
2116 if (haveToCopy <= 0) {
2117 // Log::getInstance()->log("Audio", Log::DEBUG,"No sync code in packet remove %d",mpacket.type);
2118 *samplepos=mpacket.length;
2119 return mpacket.length;
2125 if (gotframesize>0 && gotframesize>haveToCopy) {
2126 donotdecompress=true;
2127 errcount=100; // exit loop
2129 // else Log::getInstance()->log("Audio", Log::DEBUG,"Loop run" );
2131 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 19");
2132 if (!donotdecompress) {
2135 pthread_testcancel();
2136 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2137 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2139 if (!omx_running || !mp23codec_context_libav
2140 || !ac3codec_context_libav) {
2141 libav_mutex.Unlock();
2144 libav_mutex.Unlock();
2145 // Log::getInstance()->log("Audio", Log::DEBUG,"libav out");
2146 int save_size=incoming_paket_libav.size;
2148 if ((int)gotframesize <= incoming_paket_libav.size) {
2149 if (gotframesize>0) incoming_paket_libav.size=gotframesize;
2150 len = avcodec_decode_audio4(current_context, decode_frame_libav,
2151 &gotta, &incoming_paket_libav);
2153 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:E %d %d",gotframesize,incoming_paket_libav.size);
2157 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:T %d",len);
2158 incoming_paket_libav.size=save_size;
2159 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out1");
2160 pthread_setcancelstate(oldcancelstate, NULL);
2161 pthread_setcanceltype(oldcanceltype, NULL);
2162 pthread_testcancel();
2168 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out2");
2169 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 20");
2171 libav_mutex.Unlock();
2175 if (decompress_buffer_filled) { // reset to normal decoding
2177 //Log::getInstance()->log("Audio", Log::DEBUG,"saved audio: %d",len);
2178 haveToCopy -= min(len-decompress_buffer_filled,0);
2179 *samplepos += min(len-decompress_buffer_filled,0);
2180 //if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2181 /*else*/ framesize=haveToCopy;
2183 framesize=haveToCopy;
2185 incoming_paket_libav.data =(uint8_t*) buffer+mpacket.pos_buffer+*samplepos;
2187 decompress_buffer_filled=0;
2191 incoming_paket_libav.data += len;
2195 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2196 else*/framesize=haveToCopy;
2199 framesize=haveToCopy;
2202 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 21");
2204 incoming_paket_libav.size =framesize;
2206 //Log::getInstance()->log("Audio", Log::DEBUG,
2209 int dsize = av_samples_get_buffer_size(NULL,
2210 /*current_context->channels*/2, decode_frame_libav->nb_samples,
2211 AV_SAMPLE_FMT_S16, 1);
2212 /* int dsize_in = av_samples_get_buffer_size(NULL,
2213 current_context->channels, decode_frame_libav->nb_samples,
2214 current_context->sample_fmt, 1); -- unused */
2215 //if (current_context->channels==1) dsize*=2; // we convert mono to stereo
2216 if ((cur_input_buf_omx->nFilledLen + dsize)
2217 > cur_input_buf_omx->nAllocLen ) {
2218 // I doubt that this will ever happen
2219 // Log::getInstance()->log("Audio", Log::DEBUG,
2220 // "P 2 Time code %lld pts %lld", lastreftimeOMX, mpacket.pts);
2221 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
2223 if (error != OMX_ErrorNone) {
2224 Log::getInstance()->log("Audio", Log::DEBUG,
2225 "OMX_EmptyThisBuffer 4 failed %x", error);
2227 cur_input_buf_omx = NULL;
2229 if (!cur_input_buf_omx) {
2231 while (count < 10 && omx_running) {
2233 input_bufs_omx_mutex.Lock();
2234 if (input_bufs_omx_free.size() == 0) {
2235 input_bufs_omx_mutex.Unlock();
2236 // Log::getInstance()->log("Audio", Log::DEBUG,
2237 // "Deliver MediaPacket no free sample");
2239 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark22");
2240 if (!omx_running) return *samplepos;
2241 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 23");
2244 cur_input_buf_omx = input_bufs_omx_free.front();
2245 cur_input_buf_omx->nFilledLen = 0;
2246 cur_input_buf_omx->nOffset = 0;
2247 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
2248 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2249 input_bufs_omx_free.pop_front();
2250 input_bufs_omx_mutex.Unlock();
2253 if (!cur_input_buf_omx) return *samplepos;
2258 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy in %d %d %d" ,dsize,current_context->sample_rate,cur_input_buf_omx->nFilledLen);
2261 av_opt_set_int(resam_con_libav, "in_sample_rate",decode_frame_libav->sample_rate,0);
2262 av_opt_set_int(resam_con_libav, "in_sample_fmt",decode_frame_libav->format,0);
2263 av_opt_set_int(resam_con_libav, "in_channel_layout",decode_frame_libav->channel_layout, 0);
2264 //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);
2265 //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);
2267 int ret=avresample_open(resam_con_libav);
2269 Log::getInstance()->log("Audio", Log::ERR,"Opening AV resample failed %d",ret);
2271 uint8_t *output=cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen;
2273 avresample_convert(resam_con_libav, &output,
2274 dsize, decode_frame_libav->nb_samples,
2275 decode_frame_libav->extended_data, decode_frame_libav->linesize[0], decode_frame_libav->nb_samples);
2276 avresample_close(resam_con_libav);
2279 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy out");
2280 cur_input_buf_omx->nFilledLen += dsize;
2282 //Log::getInstance()->log("Audio", Log::DEBUG,"Incomplete mpeg frames in pes packet %d %d",incoming_paket_libav.size,mpacket.length);
2283 /* uint8_t a1=incoming_paket_libav.data[0];
2284 uint8_t a2=incoming_paket_libav.data[1];
2285 uint8_t a3=incoming_paket_libav.data[2];
2286 uint8_t a4=incoming_paket_libav.data[3];*/
2287 // Log::getInstance()->log("Audio", Log::DEBUG,"Header %x %x %x %x",a1,a2,
2292 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 24");
2293 decompress_buffer_filled=0;
2295 //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);
2296 memcpy(decompress_buffer,incoming_paket_libav.data,min(haveToCopy,decompress_buffer_size));
2298 decompress_buffer_filled=min(haveToCopy,decompress_buffer_size);
2302 if (cur_input_buf_omx->nFilledLen) {
2303 //Log::getInstance()->log("Audio", Log::DEBUG,
2304 // "P 3 Time code %lld pts %lld", lastreftimeOMX, mpacket.pts);
2305 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2306 if (error != OMX_ErrorNone) {
2307 Log::getInstance()->log("Audio", Log::DEBUG,
2308 "OMX_EmptyThisBuffer 5 failed %x", error);
2310 //if (mpacket.synched) vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
2311 cur_input_buf_omx = NULL;
2316 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 25");
2318 *samplepos=mpacket.length;
2319 return mpacket.length;
2325 long long AudioOMX::SetStartOffset(long long curreftime, bool *rsync){
2326 VideoOMX *vw=(VideoOMX*)Video::getInstance();
2327 return vw->SetStartAudioOffset(curreftime,rsync);
2330 void AudioOMX::ResetTimeOffsets() {
2331 VideoOMX *vw=(VideoOMX*)Video::getInstance();
2332 vw->ResetTimeOffsets();