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;
104 av_log_set_flags(AV_LOG_SKIP_REPEATED);
106 ac3codec_libav = avcodec_find_decoder(AV_CODEC_ID_AC3);
107 if (ac3codec_libav == NULL) {
108 Log::getInstance()->log("Audio", Log::DEBUG,
109 "Find libav ac3 decoder failed");
113 mp23codec_libav = avcodec_find_decoder(AV_CODEC_ID_MP3);
114 if (mp23codec_libav == NULL) {
115 Log::getInstance()->log("Audio", Log::DEBUG,
116 "Find libav mpeg audio decoder failed");
120 aaclatmcodec_libav = avcodec_find_decoder(AV_CODEC_ID_AAC_LATM);
121 if (aaclatmcodec_libav == NULL) {
122 Log::getInstance()->log("Audio", Log::DEBUG,
123 "Find libav aac latm decoder failed");
129 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eMPEG1,2,EDID_AudioSampleRate_e48KHz,0);
133 Log::getInstance()->log("Audio", Log::NOTICE,
134 "TV hdmi supports mpeg1 layer 1 and 2");
136 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eMP3,2,EDID_AudioSampleRate_e48KHz,0);
140 Log::getInstance()->log("Audio", Log::NOTICE,
141 "TV hdmi supports mpeg1 layer 3");
144 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eAC3,6,EDID_AudioSampleRate_e48KHz,0);
147 Log::getInstance()->log("Audio", Log::NOTICE,
148 "TV hdmi supports AC3");
150 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eAAC,6,EDID_AudioSampleRate_e48KHz,0);
154 Log::getInstance()->log("Audio", Log::NOTICE,
155 "TV hdmi supports AAC");
158 canpass_pcm_mch=false;
163 int AudioOMX::initAllParams()
165 return (setStreamType(streamType) && setChannel() && setSource());
168 int AudioOMX::shutdown()
170 if (!initted) return 0;
173 Log::getInstance()->log("Audio", Log::DEBUG, "audio shutdown called");
174 DeAllocateCodecsOMX();
176 free(decompress_buffer);
177 decompress_buffer=NULL;
178 decompress_buffer_size=0;
179 decompress_buffer_filled=0;
184 bool AudioOMX::loadOptionsfromServer(VDR* vdr)
186 Log::getInstance()->log("Audio", Log::DEBUG, "AudioOMX config load");
187 char *name=vdr->configLoad("AudioOMX","AC3DecodingMode");
190 if (STRCASECMP(name, "PCM") == 0) {
192 } else if (STRCASECMP(name, "Passthrough") == 0) {
194 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
200 name = vdr->configLoad("AudioOMX", "Mp2DecodingMode");
203 if (STRCASECMP(name, "PCM") == 0) {
205 } else if (STRCASECMP(name, "Passthrough") == 0) {
207 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
213 name = vdr->configLoad("AudioOMX", "AACDecodingMode");
216 if (STRCASECMP(name, "PCM") == 0) {
218 } else if (STRCASECMP(name, "Passthrough") == 0) {
220 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
226 name = vdr->configLoad("AudioOMX", "Mp3DecodingMode");
229 if (STRCASECMP(name, "PCM") == 0) {
231 } else if (STRCASECMP(name, "Passthrough") == 0) {
233 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
239 name = vdr->configLoad("AudioOMX", "AudioOutput");
242 if (STRCASECMP(name, "analog") == 0) {
244 } else if (STRCASECMP(name, "HDMI") == 0) {
255 bool AudioOMX::handleOptionChanges(Option* option)
257 if (Audio::handleOptionChanges(option))
259 switch (option->id) {
261 if (STRCASECMP(option->options[option->userSetChoice], "analog") == 0) {
263 } else if (STRCASECMP(option->options[option->userSetChoice], "HDMI")
271 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
273 } else if (STRCASECMP(option->options[option->userSetChoice],
274 "Passthrough") == 0) {
276 } else if (STRCASECMP(option->options[option->userSetChoice],
277 "PCMMultichannel") == 0) {
283 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
285 } else if (STRCASECMP(option->options[option->userSetChoice],
286 "Passthrough") == 0) {
288 } else if (STRCASECMP(option->options[option->userSetChoice],
289 "PCMMultichannel") == 0) {
295 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
297 } else if (STRCASECMP(option->options[option->userSetChoice],
298 "Passthrough") == 0) {
300 } else if (STRCASECMP(option->options[option->userSetChoice],
301 "PCMMultichannel") == 0) {
307 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
309 } else if (STRCASECMP(option->options[option->userSetChoice],
310 "Passthrough") == 0) {
312 } else if (STRCASECMP(option->options[option->userSetChoice],
313 "PCMMultichannel") == 0) {
323 bool AudioOMX::saveOptionstoServer()
326 switch (prefered_ac3) {
328 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode", "PCM");
331 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
335 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
340 switch (prefered_aac) {
342 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode", "PCM");
345 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
349 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
354 switch (prefered_mp2) {
356 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode", "PCM");
359 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
363 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
368 switch (prefered_mp3) {
370 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode", "PCM");
373 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
377 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
383 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "analog");
385 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "HDMI");
391 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
392 UINT numChoices, UINT defaultChoice, UINT startInt,
393 const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
395 bool AudioOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
397 if (!Audio::addOptionsToPanes(panenumber,options,pane)) return false;
404 static const char* audioopts[]={"analog","HDMI"};
405 option = new Option(4,tr("Audio Output"), "AudioOMX","AudioOutput",Option::TYPE_TEXT,2,0,0,audioopts,NULL,false,this);
406 options->push_back(option);
407 pane->addOptionLine(option);
410 char **ac3opts=new char *[3];
412 ac3opts[i]=new char[strlen("PCM")+1];
413 strcpy(ac3opts[i],"PCM");
416 ac3opts[i]=new char[strlen("Passthrough")+1];
417 strcpy(ac3opts[i],"PassThrough");
420 if (canpass_pcm_mch) {
421 ac3opts[i]=new char[strlen("PCMMultichannel")+1];
422 strcpy(ac3opts[i],"PCMMultichannel");
425 option = new Option(1 ,tr("AC3 HDMI Mode"), "AudioOMX", "AC3DecodingMode", Option::TYPE_TEXT, i, 0, 0, ac3opts,NULL,true, this);
426 options->push_back(option);
427 pane->addOptionLine(option);
429 /* char **aacopts = new char *[3];
431 aacopts[i] = new char[strlen("PCM") + 1];
432 strcpy(mp2opts[i], "PCM");
435 aacopts[i] = new char[strlen("Passthrough") + 1];
436 strcpy(aacopts[i], "PassThrough");
439 if (canpass_pcm_mch) {
440 aacopts[i] = new char[strlen("PCMMultichannel") + 1];
441 strcpy(aacopts[i], "PCMMultichannel");
444 option = new Option(5, tr("Mp2 HDMI Mode"), "AudioOMX",
445 "AACDecodingMode", Option::TYPE_TEXT, i, 0, 0,
446 aacopts, NULL, true, this);
447 options->push_back(option);
448 pane->addOptionLine(option);
451 char **mp2opts = new char *[3];
453 mp2opts[i] = new char[strlen("PCM") + 1];
454 strcpy(mp2opts[i], "PCM");
457 mp2opts[i] = new char[strlen("Passthrough") + 1];
458 strcpy(mp2opts[i], "PassThrough");
461 if (canpass_pcm_mch) {
462 mp2opts[i] = new char[strlen("PCMMultichannel") + 1];
463 strcpy(mp2opts[i], "PCMMultichannel");
466 option = new Option(2, tr("Mp2 HDMI Mode"), "AudioOMX",
467 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0,
468 mp2opts, NULL, true, this);
469 options->push_back(option);
470 pane->addOptionLine(option);
472 char **mp3opts = new char *[3];
474 mp3opts[i] = new char[strlen("PCM") + 1];
475 strcpy(mp3opts[i], "PCM");
478 mp3opts[i] = new char[strlen("Passthrough") + 1];
479 strcpy(mp3opts[i], "PassThrough");
482 if (canpass_pcm_mch) {
483 mp3opts[i] = new char[strlen("PCMMultichannel") + 1];
484 strcpy(mp3opts[i], "PCMMultichannel");
487 option = new Option(3, tr("Mp3 HDMI Mode"), "AudioOMX",
488 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0, mp3opts,
490 options->push_back(option);
491 pane->addOptionLine(option);*/
492 // Comment unsupported modes out
505 OMX_ERRORTYPE AudioOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
507 //Log::getInstance()->log("Audio", Log::NOTICE, "EmptyBufferDone");
508 AudioOMX *audio=(AudioOMX *)getInstance();
509 audio->ReturnEmptyOMXBuffer(buffer);
510 return OMX_ErrorNone;
514 void AudioOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
515 input_bufs_omx_mutex.Lock();
516 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
517 input_bufs_omx_free.push_back(buffer);
518 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
519 input_bufs_omx_mutex.Unlock();
520 VideoOMX *video=(VideoOMX*)Video::getInstance();
524 OMX_ERRORTYPE AudioOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
525 Log::getInstance()->log("Audio", Log::NOTICE, "FillBufferDone");
526 VideoOMX *video=(VideoOMX*)Video::getInstance();
528 return OMX_ErrorNone;
533 int AudioOMX::setStreamType(UCHAR type)
535 if (!initted) return 0;
537 // if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
541 int AudioOMX::setChannel()
543 if (!initted) return 0;
545 // if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
549 int AudioOMX::setSource()
551 if (!initted) return 0;
553 // if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
559 if (!initted) return 0;
561 // if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
565 int AudioOMX::play() {
568 lastAType=MPTYPE_MPEG_AUDIO;
569 Log::getInstance()->log("Audio", Log::DEBUG, "enter play");
571 ((VideoOMX*)Video::getInstance())->interlaceSwitch4Demux(); // switch resolution if necessary
573 if (!AllocateCodecsOMX()) {
580 int AudioOMX::ChangeAudioDestination() //clock aka omx mutex needs to be locked
583 const char * destinations[]={"local","hdmi"};
588 OMX_CONFIG_BRCMAUDIODESTINATIONTYPE auddest;
589 memset(&auddest,0,sizeof(auddest));
590 auddest.nSize=sizeof(auddest);
591 auddest.nVersion.nVersion=OMX_VERSION;
592 strcpy((char *)auddest.sName, destinations[dest]);
594 Log::getInstance()->log("Audio", Log::DEBUG, "setting destination to: %s",auddest.sName);
595 error=OMX_SetConfig(omx_aud_rend,OMX_IndexConfigBrcmAudioDestination,&auddest);
596 if (error!=OMX_ErrorNone){
597 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX_IndexConfigBrcmAudioDestination failed %x %x %s", error,omx_aud_rend,auddest.sName);
598 DeAllocateCodecsOMX();
606 int AudioOMX::ChangeAudioPortConfig(bool disport) //clock aka omx mutex needs to be locked
609 //Ok first fidle a working configuration
610 Log::getInstance()->log("Audio", Log::DEBUG,
611 "ChangeAudioPortConfig");
613 OMX_AUDIO_CODINGTYPE encoding;
616 case MPTYPE_MPEG_AUDIO: {
617 if (prefered_mp2 == 2 && false) { //not supported yet
620 if (prefered_mp2 == 1 && canpass_mp2) {
622 encoding=OMX_AUDIO_CodingMP3;
625 encoding=OMX_AUDIO_CodingPCM;
630 case MPTYPE_AAC_LATM: {
631 if (prefered_aac == 2 && false) { //not supported yet
634 Log::getInstance()->log("Audio", Log::DEBUG,
635 "ChangeAudioPortConfig debug %d %d",prefered_aac,canpass_aac);
636 if (prefered_aac == 1 && canpass_aac) {
638 encoding=OMX_AUDIO_CodingAAC;
641 encoding=OMX_AUDIO_CodingPCM;
646 case MPTYPE_AC3_PRE13:
648 if (prefered_ac3 == 2 && false) { //not supported yet
651 Log::getInstance()->log("Audio", Log::DEBUG,
652 "ChangeAudioPortConfig debug %d %d",prefered_ac3,canpass_ac3);
653 if (prefered_ac3 == 1 && canpass_ac3) {
655 encoding=OMX_AUDIO_CodingDDP;
658 encoding=OMX_AUDIO_CodingPCM;
663 case MPTYPE_MPEG_AUDIO_LAYER3: {
664 if (prefered_mp3 == 2 && false) { //not supported yet
667 if (prefered_mp3 == 1 && canpass_mp2) {
669 encoding=OMX_AUDIO_CodingMP3;
672 encoding=OMX_AUDIO_CodingPCM;
680 encoding=OMX_AUDIO_CodingPCM;
681 //mch=false; // multichannel also false
686 /*OMX_CONFIG_BOOLEANTYPE booly;
687 memset(&booly, 0, sizeof(booly));
688 booly.nSize = sizeof(booly);
689 booly.nVersion.nVersion = OMX_VERSION;
691 booly.bEnabled = OMX_TRUE;
693 booly.bEnabled = OMX_FALSE;
695 error = OMX_SetParameter(omx_aud_dec, OMX_IndexParamBrcmDecoderPassThrough,
697 if (error != OMX_ErrorNone) {
698 Log::getInstance()->log("Audio", Log::DEBUG,
699 "Init OMX_IndexParamBrcmDecoderPassThrough failed %x", error);
700 DeAllocateCodecsOMX();
703 VideoOMX* video=(VideoOMX*)Video::getInstance();
705 video->DisablePort(omx_aud_rend,omx_rend_input_port,false);
706 //DestroyInputBufsOMXwhilePlaying();
707 //video->CommandFinished(omx_aud_rend,OMX_CommandPortDisable,omx_rend_input_port);
712 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
713 memset(&format, 0, sizeof(format));
714 format.nSize = sizeof(format);
715 format.nVersion.nVersion = OMX_VERSION;
716 format.nPortIndex = omx_rend_input_port;
717 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
719 if (error != OMX_ErrorNone) {
720 Log::getInstance()->log("Audio", Log::DEBUG,
721 "Get OMX_IndexParamAudioPortFormat failed %x %d", error,
722 omx_rend_input_port);
727 Log::getInstance()->log("Audio", Log::DEBUG,
728 "Get OMX_IndexParamAudioPortFormat returned %d",format.eEncoding );
729 format.eEncoding = encoding;
731 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
733 if (error != OMX_ErrorNone) {
734 Log::getInstance()->log("Audio", Log::DEBUG,
735 "Set OMX_IndexParamAudioPortFormat failed %x %d %d", error,
736 omx_rend_input_port,format.eEncoding );
741 case OMX_AUDIO_CodingPCM: {
742 OMX_AUDIO_PARAM_PCMMODETYPE audio_pcm;
743 memset(&audio_pcm, 0, sizeof(audio_pcm));
744 audio_pcm.nSize = sizeof(audio_pcm);
745 audio_pcm.nVersion.nVersion = OMX_VERSION;
746 audio_pcm.nChannels = 2;
747 audio_pcm.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
748 audio_pcm.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
749 //audio_pcm.eChannelMapping[2]=OMX_AUDIO_ChannelMax;
750 audio_pcm.eNumData = OMX_NumericalDataSigned;
751 audio_pcm.eEndian = OMX_EndianLittle;
752 audio_pcm.bInterleaved = OMX_TRUE;
753 audio_pcm.nBitPerSample = 16;
754 audio_pcm.ePCMMode = OMX_AUDIO_PCMModeLinear;
755 audio_pcm.nChannels = 2;
756 audio_pcm.nSamplingRate = 48000;
757 audio_pcm.nPortIndex = omx_rend_input_port;
758 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPcm,
760 if (error != OMX_ErrorNone) {
761 Log::getInstance()->log("Audio", Log::DEBUG,
762 "Init OMX_IndexParamAudioPcm failed %x %d", error,
763 omx_rend_input_port);
767 case OMX_AUDIO_CodingDDP: {
768 OMX_AUDIO_PARAM_DDPTYPE audio_ddp;
769 memset(&audio_ddp, 0, sizeof(audio_ddp));
770 audio_ddp.nSize = sizeof(audio_ddp);
771 audio_ddp.nVersion.nVersion = OMX_VERSION;
772 audio_ddp.nPortIndex = omx_rend_input_port;
773 audio_ddp.nChannels = 8; //unknown
774 audio_ddp.nBitRate=0;
775 audio_ddp.nSampleRate=48000;
776 audio_ddp.eChannelMapping[0] =OMX_AUDIO_ChannelLF;
777 audio_ddp.eChannelMapping[1] =OMX_AUDIO_ChannelRF;
778 audio_ddp.eChannelMapping[2] =OMX_AUDIO_ChannelCF;
779 audio_ddp.eChannelMapping[3] =OMX_AUDIO_ChannelLFE;
780 audio_ddp.eChannelMapping[4] =OMX_AUDIO_ChannelLR;
781 audio_ddp.eChannelMapping[5] =OMX_AUDIO_ChannelRR;
782 audio_ddp.eChannelMapping[6] =OMX_AUDIO_ChannelLS;
783 audio_ddp.eChannelMapping[7] =OMX_AUDIO_ChannelRS;
784 audio_ddp.eChannelMapping[8] =OMX_AUDIO_ChannelCS;
785 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioDdp,
787 if (error != OMX_ErrorNone) {
788 Log::getInstance()->log("Audio", Log::DEBUG,
789 "Init OMX_IndexParamAudioDdp failed %x %d", error,
790 omx_rend_input_port);
795 default: break; //Make compiler happy
802 //PrepareInputBufsOMX(false);
803 video->EnablePort(omx_aud_rend,omx_rend_input_port,false);
813 int AudioOMX::InitDecoderLibAV()
816 ac3codec_context_libav = avcodec_alloc_context3(ac3codec_libav);
817 if (!ac3codec_context_libav) {
818 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for ac3 decoding context failed!");
822 ac3codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
824 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
825 ac3codec_context_libav->request_channel_layout=2;
827 ac3codec_context_libav->request_channels=2;
830 int avc_ret = avcodec_open2(ac3codec_context_libav, ac3codec_libav, NULL);
832 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
833 libav_mutex.Unlock();
837 aaclatmcodec_context_libav = avcodec_alloc_context3(aaclatmcodec_libav);
838 if (!aaclatmcodec_context_libav) {
839 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for aac decoding context failed!");
843 aaclatmcodec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
844 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
845 aaclatmcodec_context_libav->request_channel_layout=2;
847 aaclatmcodec_context_libav->request_channels=2;
850 avc_ret = avcodec_open2(aaclatmcodec_context_libav, aaclatmcodec_libav, NULL);
852 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
853 libav_mutex.Unlock();
858 mp23codec_context_libav = avcodec_alloc_context3(mp23codec_libav);
859 if (!ac3codec_context_libav) {
860 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for mp23 decoding context failed!");
861 libav_mutex.Unlock();
865 mp23codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
866 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
867 mp23codec_context_libav->request_channel_layout=2;
869 mp23codec_context_libav->request_channels=2;
872 avc_ret = avcodec_open2(mp23codec_context_libav, mp23codec_libav, NULL);
874 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
875 libav_mutex.Unlock();
879 resam_con_libav = avresample_alloc_context();
880 if (resam_con_libav == NULL) {
881 Log::getInstance()->log("Audio", Log::DEBUG,
882 "Alloc resample context failed");
886 av_opt_set_int(resam_con_libav, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0); // our standard format
887 av_opt_set_int(resam_con_libav, "out_sample_rate",48000,0);
888 av_opt_set_int(resam_con_libav, "out_sample_fmt",AV_SAMPLE_FMT_S16,0);
889 av_opt_set_int(resam_con_libav, "matrix_encoding",AV_MATRIX_ENCODING_DPLII,0);
891 av_opt_set_int(resam_con_libav, "in_sample_rate",48000,0);
892 av_opt_set_int(resam_con_libav, "in_sample_fmt",AV_SAMPLE_FMT_S16,0);
893 av_opt_set_int(resam_con_libav, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0); //just an example
896 av_init_packet(&incoming_paket_libav);
897 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)
898 decode_frame_libav=av_frame_alloc();
900 decode_frame_libav=avcodec_alloc_frame();
902 libav_mutex.Unlock();
903 decompress_buffer_filled=0;
910 void AudioOMX::DeinitDecoderLibAV() {
914 if (ac3codec_context_libav) {
915 avcodec_close(ac3codec_context_libav);
916 av_free(ac3codec_context_libav);
917 ac3codec_context_libav = NULL;
919 avcodec_close(aaclatmcodec_context_libav);
920 av_free(aaclatmcodec_context_libav);
921 aaclatmcodec_context_libav = NULL;
923 av_free(decode_frame_libav);
925 avcodec_close(mp23codec_context_libav);
926 av_free(mp23codec_context_libav);
927 mp23codec_context_libav = NULL;
929 avresample_free(&resam_con_libav);
930 resam_con_libav=NULL;
934 libav_mutex.Unlock();
939 int AudioOMX::AllocateCodecsOMX()
942 static OMX_CALLBACKTYPE callbacks= {&VideoOMX::EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
944 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX");
945 //Clock, move later to audio
946 VideoOMX *video=(VideoOMX*)Video::getInstance();
948 if (!InitDecoderLibAV()) return 0;;
951 OMX_PORT_PARAM_TYPE p_param;
952 memset(&p_param,0,sizeof(p_param));
953 p_param.nSize=sizeof(p_param);
954 p_param.nVersion.nVersion=OMX_VERSION;
957 if (!video->getClockAudioandInit(&omx_clock,&omx_clock_output_port)){
958 return 0;// get the clock and init it if necessary
962 if (!video->idleClock()) {
967 error = OMX_GetHandle(&omx_aud_rend, L_VPE_OMX_AUDIO_REND, NULL, &callbacks);
968 if (error != OMX_ErrorNone) {
969 Log::getInstance()->log("Audio", Log::DEBUG,
970 "Init OMX audio rend failed %x", error);
971 video->UnlockClock();
972 DeAllocateCodecsOMX();
976 if (!ChangeAudioDestination()) {
977 video->UnlockClock();
978 DeAllocateCodecsOMX();
982 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioInit, &p_param);
983 if (error != OMX_ErrorNone) {
984 Log::getInstance()->log("Audio", Log::DEBUG,
985 "Init OMX audio rend OMX_GetParameter failed %x", error);
986 video->UnlockClock();
987 DeAllocateCodecsOMX();
990 omx_rend_input_port = p_param.nStartPortNumber;
996 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamOtherInit, &p_param);
997 if (error != OMX_ErrorNone) {
998 Log::getInstance()->log("Audio", Log::DEBUG,
999 "Init OMX aud rend OMX_GetParameter failed %x", error);
1000 video->UnlockClock();
1001 DeAllocateCodecsOMX();
1004 // buggy return value
1005 omx_rend_clock_port = p_param.nStartPortNumber;
1010 /* error=OMX_GetHandle(&omx_aud_dec,VPE_OMX_AUDIO_DECODER,NULL,&callbacks);
1012 if (error!=OMX_ErrorNone){
1013 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder failed %x", error);
1014 video->UnlockClock();
1015 DeAllocateCodecsOMX();
1019 error=OMX_GetParameter(omx_aud_dec,OMX_IndexParamAudioInit,&p_param);
1020 if (error!=OMX_ErrorNone){
1021 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder OMX_GetParameter failed %x", error);
1022 video->UnlockClock();
1023 DeAllocateCodecsOMX();
1026 omx_codec_input_port=p_param.nStartPortNumber;
1027 omx_codec_output_port=p_param.nStartPortNumber+1;
1029 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port) || !video->DisablePort(omx_aud_dec,omx_codec_output_port)) {
1030 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio decoder failed");
1031 video->UnlockClock();
1032 DeAllocateCodecsOMX();
1039 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true) ) {
1040 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio rend failed %d",omx_rend_input_port);
1041 video->UnlockClock();
1042 DeAllocateCodecsOMX();
1046 if ( !video->DisablePort(omx_aud_rend, omx_rend_clock_port, true)) {
1047 Log::getInstance()->log("Audio", Log::DEBUG,
1048 "Disable Ports OMX rend clock port failed %d",omx_rend_clock_port);
1049 video->UnlockClock();
1050 DeAllocateCodecsOMX();
1060 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_aud_rend,omx_rend_clock_port);
1061 if (error!=OMX_ErrorNone){
1062 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);
1063 video->UnlockClock();
1064 DeAllocateCodecsOMX();
1068 if (!video->EnablePort(omx_clock,omx_clock_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_clock_port,false)
1070 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX clock rend failed");
1071 video->UnlockClock();
1072 DeAllocateCodecsOMX();
1076 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1077 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend idle ChangeComponentState");
1078 video->UnlockClock();
1079 DeAllocateCodecsOMX();
1086 if ( !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_clock_port)) {
1087 video->UnlockClock();
1088 DeAllocateCodecsOMX();
1092 if ( !video->CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1093 video->UnlockClock();
1094 DeAllocateCodecsOMX();
1100 if (!ChangeAudioPortConfig(false)){
1101 Log::getInstance()->log("Audio", Log::NOTICE, "Change AudioPortConfig failed");
1102 video->UnlockClock();
1103 DeAllocateCodecsOMX();
1107 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1108 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1109 DeAllocateCodecsOMX();
1115 if (!PrepareInputBufsOMX(true)) {
1116 video->UnlockClock();
1117 DeAllocateCodecsOMX();
1123 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,omx_aud_rend,omx_rend_input_port);
1124 if (error!=OMX_ErrorNone){
1125 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel dec to render failed %x", error);
1126 video->UnlockClock();
1127 DeAllocateCodecsOMX();
1133 /* if (!video->EnablePort(omx_aud_dec,omx_codec_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_input_port,false)
1135 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX codec rend failed");
1136 video->UnlockClock();
1137 DeAllocateCodecsOMX();
1141 /* if ( !video->CommandFinished(omx_aud_dec,OMX_CommandPortEnable,omx_codec_output_port)
1142 || !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1143 video->UnlockClock();
1144 DeAllocateCodecsOMX();
1148 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateExecuting)) {
1149 Log::getInstance()->log("Audio", Log::DEBUG, "omx_aud_rend ChangeComponentState Execute");
1150 video->UnlockClock();
1151 DeAllocateCodecsOMX();
1156 video->UnlockClock();
1160 video->clockUnpause();
1163 if (!video->setClockExecutingandRunning()) return 0;
1165 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX finished");
1173 int AudioOMX::PrepareInputBufsOMX(bool setportdef) //needs to be called with locvke omx clock mutex
1175 VideoOMX *video=(VideoOMX*)Video::getInstance();
1176 OMX_ERRORTYPE error;
1177 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1178 memset(&port_def_type,0,sizeof(port_def_type));
1179 port_def_type.nSize=sizeof(port_def_type);
1180 port_def_type.nVersion.nVersion=OMX_VERSION;
1181 port_def_type.nPortIndex=omx_rend_input_port;//omx_codec_input_port;
1183 error=OMX_GetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1185 if (error!=OMX_ErrorNone){
1186 Log::getInstance()->log("Audio", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1191 port_def_type.nBufferCountActual=2;
1192 port_def_type.nBufferSize=max(port_def_type.nBufferSize,50000); // for transcoder important
1194 error=OMX_SetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1196 if (error!=OMX_ErrorNone){
1197 Log::getInstance()->log("Audio", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1202 error=OMX_SendCommand(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port/*codec*/,0);
1203 if (error!=OMX_ErrorNone){
1204 Log::getInstance()->log("Audio", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1208 input_bufs_omx_mutex.Lock();
1209 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1210 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1211 error=OMX_AllocateBuffer(omx_aud_rend/*dec*/,&buf_head,omx_rend_input_port/*codec*/,NULL,port_def_type.nBufferSize);
1212 if (error!=OMX_ErrorNone){
1213 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1214 input_bufs_omx_mutex.Unlock();
1217 input_bufs_omx_all.push_back(buf_head);
1218 input_bufs_omx_free.push_back(buf_head);
1220 omx_first_frame=true;
1223 cur_input_buf_omx=NULL;
1224 input_bufs_omx_mutex.Unlock();
1226 if (!video->CommandFinished(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port /*codec*/)) {
1233 int AudioOMX::DestroyInputBufsOMX() //call with clock mutex locked
1235 OMX_ERRORTYPE error;
1237 cur_input_buf_omx=NULL;
1238 input_bufs_omx_mutex.Lock();
1239 for (UINT i=0; i< input_bufs_omx_all.size();i++) {
1240 error=OMX_FreeBuffer(omx_aud_rend/*dec*/,omx_rend_input_port/*codec*/,input_bufs_omx_all[i]);
1241 if (error!=OMX_ErrorNone){
1242 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1243 input_bufs_omx_mutex.Unlock();
1248 input_bufs_omx_all.clear();
1249 input_bufs_omx_free.clear();
1250 input_bufs_omx_mutex.Unlock();
1255 int AudioOMX::DestroyInputBufsOMXwhilePlaying() //call with clock mutex locked
1257 //OMX_ERRORTYPE error;
1259 cur_input_buf_omx=NULL;
1260 input_bufs_omx_mutex.Lock();
1261 while (input_bufs_omx_all.size()>0) {
1262 if (input_bufs_omx_free.size()>0) {
1263 // Destroy one buffer
1264 vector<OMX_BUFFERHEADERTYPE*>::iterator itty=input_bufs_omx_all.begin();
1265 OMX_BUFFERHEADERTYPE* cur_buf=input_bufs_omx_free.front();
1266 for (; itty!= input_bufs_omx_all.end();itty++) {
1267 if ((*itty)==cur_buf) {
1268 input_bufs_omx_all.erase(itty);
1269 input_bufs_omx_free.pop_front();
1274 input_bufs_omx_mutex.Unlock();
1276 input_bufs_omx_mutex.Lock();
1280 Log::getInstance()->log("Audio", Log::DEBUG, "DestroyInputBufsOMXwhilePlaying %d %d", input_bufs_omx_all.size(),input_bufs_omx_free.size());
1281 input_bufs_omx_mutex.Unlock();
1286 int AudioOMX::DeAllocateCodecsOMX()
1288 OMX_ERRORTYPE error;
1290 VideoOMX *video=(VideoOMX*)Video::getInstance();
1291 Log::getInstance()->log("Audio", Log::DEBUG, "enter deallocatecodecsomx");
1295 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 1");
1296 if (cur_input_buf_omx) {
1297 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1298 error=video->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,cur_input_buf_omx);
1299 if (error!=OMX_ErrorNone) {
1300 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_EmptyThisBuffer 6 failed %x", error);
1303 cur_input_buf_omx=NULL;//write out old data
1305 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 2");
1308 if (omx_aud_rend/*dec*/) {
1309 // first stop the omx elements
1310 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1311 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1313 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 3");
1315 video->UnlockClock();
1316 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 4");
1318 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 5");
1321 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1322 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend ChangeComponentState");
1325 // TODO proper deinit sequence
1326 // first flush all buffers
1328 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
1329 if (error!=OMX_ErrorNone) {
1330 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend in failed %x", error);
1334 /* error=OMX_SendCommand(omx_aud_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
1335 if (error!=OMX_ErrorNone){
1336 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1341 /* if (!video->CommandFinished(omx_aud_dec,OMX_CommandFlush,omx_codec_input_port)) {
1342 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd codec input failed");
1347 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1348 if (error!=OMX_ErrorNone){
1349 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1353 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_clock_port, NULL);
1354 if (error!=OMX_ErrorNone){
1355 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend clock failed %x", error);
1358 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6");
1360 if (!video->CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1361 !video->CommandFinished(omx_aud_rend,OMX_CommandFlush,omx_rend_clock_port)) {
1362 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd clock shed failed");
1365 DestroyInputBufsOMX(); //We have to make sure that no buffers are in use
1366 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6a");
1367 DeinitDecoderLibAV();
1368 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 7");
1371 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true)) {
1372 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 1");
1375 /* if (!video->DisablePort(omx_aud_dec,omx_codec_output_port,true)) {
1376 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 6");
1379 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port,true)) {
1380 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 7");
1384 if (!video->DisablePort(omx_aud_rend,omx_rend_clock_port,true)) {
1385 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 4");
1388 if (!video->DisablePort(omx_clock,omx_clock_output_port,true)) {
1389 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 5");
1394 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,NULL,NULL);
1395 if (error!=OMX_ErrorNone) {
1396 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1402 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_input_port,NULL,0);
1403 if (error!=OMX_ErrorNone) {
1404 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1408 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,0);
1409 if (error!=OMX_ErrorNone) {
1410 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1414 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_clock_port,NULL,0);
1415 if (error!=OMX_ErrorNone) {
1416 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1419 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 8");
1422 //error=OMX_FreeHandle(omx_aud_dec);
1423 error=OMX_FreeHandle(omx_aud_rend);
1424 video->UnlockClock();
1425 video->destroyClock();
1426 omx_aud_rend/*dec*/=NULL;
1427 if (error!=OMX_ErrorNone) {
1428 Log::getInstance()->log("Audio", Log::DEBUG, "FreeHandle failed %d", error);
1432 video->UnlockClock();
1433 DeinitDecoderLibAV();
1435 Log::getInstance()->log("Audio", Log::DEBUG, "leave deallocate codecs OMX");
1442 int AudioOMX::stop()
1444 if (!initted) return 0;
1446 Log::getInstance()->log("Audio", Log::DEBUG, "Audio stop called");
1447 DeAllocateCodecsOMX();
1448 //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1452 int AudioOMX::mute() {
1455 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
1456 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1460 OMX_AUDIO_CONFIG_MUTETYPE amute;
1461 memset(&amute, 0, sizeof(amute));
1462 amute.nSize = sizeof(amute);
1463 amute.nVersion.nVersion = OMX_VERSION;
1464 amute.nPortIndex = omx_rend_input_port;
1465 amute.bMute = OMX_TRUE;
1466 OMX_ERRORTYPE error= OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1468 if (error != OMX_ErrorNone) {
1469 Log::getInstance()->log("Audio", Log::DEBUG,
1470 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1471 omx_rend_input_port);
1483 int AudioOMX::unMute()
1485 if (!initted) return 0;
1487 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
1488 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1492 OMX_AUDIO_CONFIG_MUTETYPE amute;
1493 memset(&amute, 0, sizeof(amute));
1494 amute.nSize = sizeof(amute);
1495 amute.nVersion.nVersion = OMX_VERSION;
1496 amute.nPortIndex = omx_rend_input_port;
1497 amute.bMute = OMX_FALSE;
1498 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1500 if (error != OMX_ErrorNone) {
1501 Log::getInstance()->log("Audio", Log::DEBUG,
1502 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1503 omx_rend_input_port);
1515 int AudioOMX::pause() {
1519 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1525 int AudioOMX::unPause()
1527 if (!initted) return 0;
1529 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1536 int AudioOMX::reset()
1538 if (!initted) return 0;
1540 Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
1541 DeAllocateCodecsOMX();
1543 // if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1544 // Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
1545 // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
1551 int AudioOMX::setVolume(int tvolume)
1553 // parameter: 0 for silence, 20 for full
1554 if ((tvolume < 0) || (tvolume > 20)) return 0;
1556 // volume = 2 * (20 - volume);
1557 // Right, that one was rubbish... 0-10 were almost
1558 // inaudible, 11-20 did what should have been done
1559 // over the whole 0-20 range
1563 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1567 OMX_AUDIO_CONFIG_VOLUMETYPE avol;
1568 memset(&avol, 0, sizeof(avol));
1569 avol.nSize = sizeof(avol);
1570 avol.nVersion.nVersion = OMX_VERSION;
1571 avol.nPortIndex = omx_rend_input_port;
1572 avol.bLinear=OMX_FALSE;
1573 avol.sVolume.nValue =(volume-20)*200;
1574 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioVolume,
1576 if (error != OMX_ErrorNone) {
1577 Log::getInstance()->log("Audio", Log::DEBUG,
1578 "Set OMX_IndexConfigAudioVolume failed %x %d", error,
1579 omx_rend_input_port);
1591 int AudioOMX::test()
1594 // return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
1596 /* aud_sync_parms_t a;
1600 // int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
1603 /*OK*/ //printf("Audio sync disable = %i\n", b);
1611 unsigned int AudioOMX::AdvanceMpAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1613 if (size<=2) return size; // silly;
1614 unsigned int atest=0;
1616 //inspired by libav parsing code
1617 while (atest+1<size) {
1618 if (data[atest]==0xFF && (data[atest+1] &0xe0)==0xe0) {
1619 if ((data[atest+1] & 0x18) == 0x08) {atest++;continue;} //sanity check: mpeg version ID 01 -> reserved
1620 //sanity check inspired by libav
1623 const int sample_rates[4]={44100,48000,32000,0};
1624 const short bitrate_tab[2][3][15] = { { { 0, 32, 64, 96, 128, 160,
1625 192, 224, 256, 288, 320, 352, 384, 416, 448 }, { 0, 32, 48,
1626 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 }, {
1627 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
1628 256, 320 } }, { { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1629 160, 176, 192, 224, 256 }, { 0, 8, 16, 24, 32, 40, 48, 56,
1630 64, 80, 96, 112, 128, 144, 160 }, { 0, 8, 16, 24, 32, 40,
1631 48, 56, 64, 80, 96, 112, 128, 144, 160 } } };
1634 int layer=4-((data[atest+1]&0x06)>>1);
1635 if (layer==4) {atest++;continue;} //sanity check
1637 int bitrate_index=(data[atest+2]&0xf0)>>4;
1638 if (bitrate_index==0x0f || bitrate_index==0x0) {atest++;continue;} //sanity check
1640 int samplerate_index=(data[atest+2]&0x0C)>>2;
1641 if (samplerate_index==0x03) {atest++;continue;} //sanity check
1643 int padding=(data[atest+2]&2)>>1;
1644 if (0x10 & data[atest+1]) {
1645 lsf=((data[atest+1]) &0x8)?0:1;
1651 int sample_rate=sample_rates[ samplerate_index]>>(lsf+mpeg2);
1653 int temp_frame_size=bitrate_tab[lsf][layer - 1][bitrate_index];
1655 frame_size=(temp_frame_size*12000)/sample_rate;
1656 frame_size=(frame_size+padding)*4;
1657 } else if (layer==2) {
1658 frame_size=(temp_frame_size*144000)/sample_rate;
1659 frame_size=frame_size+padding;
1661 frame_size=(temp_frame_size*144000)/(sample_rate<<lsf);
1662 frame_size=frame_size+padding;
1664 unsigned int sameheadertest=(data[atest]<<24)|(data[atest+1]<<16) |(data[atest+2]<<8);
1665 const unsigned mask=(0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)); // from libav
1666 if (!mp3sameheader) {
1667 mp3sameheader=sameheadertest;
1668 mp3sameheadercount=2;
1670 if ((mp3sameheader& mask)== (sameheadertest &mask)) mp3sameheadercount++;
1671 else mp3sameheadercount=0;
1672 mp3sameheader=sameheadertest;
1674 //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);
1675 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME DIAG: %x %x %x %d",data[atest],data[atest+1],data[atest+2],mp3sameheadercount);
1677 if (mp3sameheadercount>4) {
1678 *framesize=frame_size;
1679 return atest; // probably FrameSync
1680 } //skip it if the header changes too frequently
1687 unsigned int AudioOMX::AdvanceAc3AudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1689 if (size<=4) return size; // silly;
1690 const int frm_size_tab[] = { 64, 64, 80, 80, 96, 96, 112, 112, 128, 128,
1691 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384, 384, 448,
1692 448, 512, 512, 640, 640, 768, 768, 896, 896, 1024, 1024, 1152,
1693 1152, 1280, 1280, };
1694 unsigned int atest=0;
1695 *framesize=20000; //if we do not find a start code do not decompress
1696 while (atest+4<size) {
1697 if (data[atest]==0x0B && data[atest+1]==0x77) {
1698 // now figure out the length of the frame
1699 unsigned char code=data[atest+4];
1700 unsigned char fscod=(code& 0xC0)>>6;
1701 unsigned char frmsize=(code &0x3f);
1702 if (fscod!=0) Log::getInstance()->log("Audio", Log::DEBUG, "warning we only support 48 KHz sampling rate");
1703 *framesize=frm_size_tab[frmsize]*2;
1704 return atest; // probably FrameSync
1711 unsigned int AudioOMX::AdvanceAacLatmAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1713 if (size<=4) return size; // silly;
1714 unsigned int atest=0;
1715 *framesize=20000; //if we do not find a start code do not decompress
1716 while (atest+4<size) {
1717 if (data[atest] ==0x56 && (data[atest+1]& 0xe0)==0xe0) {
1718 // now figure out the length of the frame
1719 unsigned int length= ((0x1f & data[atest+1])<<8) | data[atest+2];
1720 *framesize=length+3;
1721 return atest; // probably FrameSync
1729 void AudioOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1731 packet = mplist.front();
1734 UINT AudioOMX::DeliverMediaSample(UCHAR* buffer, UINT* samplepos) {
1735 DeliverMediaPacket(packet, buffer, samplepos);
1736 if (*samplepos == packet.length) {
1744 long long AudioOMX::correctAudioLatency(long long pts,int addsamples,int srate) {
1746 VideoOMX *video = (VideoOMX*) Video::getInstance();
1748 OMX_PARAM_U32TYPE audio_lat;
1749 OMX_ERRORTYPE error;
1750 memset(&audio_lat, 0, sizeof(audio_lat));
1751 audio_lat.nSize = sizeof(audio_lat);
1752 audio_lat.nVersion.nVersion = OMX_VERSION;
1753 audio_lat.nPortIndex = omx_rend_input_port;
1755 error = OMX_GetConfig(omx_aud_rend, OMX_IndexConfigAudioRenderingLatency,
1757 video->UnlockClock();
1758 if (error != OMX_ErrorNone) {
1759 Log::getInstance()->log("Audio", Log::DEBUG,
1760 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
1761 omx_rend_input_port);
1762 return pts; // no correction in case of error
1764 /*Log::getInstance()->log("Audio", Log::DEBUG, "Current audio latency %d",
1767 long long workpts=0;
1768 workpts+=addsamples;
1769 workpts-=audio_lat.nU32;
1770 workpts*=10LL*1000LL*1000LL;
1771 workpts=workpts/((long long)srate); //one second /samplerate
1777 bool AudioOMX::DrainTargetBufferFull()
1779 //Check, if we have OMX output buffers
1781 input_bufs_omx_mutex.Lock();
1782 full=(input_bufs_omx_free.size()==0);
1783 input_bufs_omx_mutex.Unlock();
1789 UINT AudioOMX::DeliverMediaPacket(MediaPacket mpacket, const UCHAR* buffer,
1791 /*First Check, if we have an audio sample*/
1792 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1794 OMX_ERRORTYPE error;
1795 //Log *logger=Log::getInstance();
1796 if (vw->InIframemode()) {
1799 return 0; //Not in iframe mode!
1802 if (!omx_running) return 0; // if we are not runnig do not do this
1803 if (vw->isClockPaused()) return 0; //Block if we pause
1804 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 1");
1806 //Log::getInstance()->log("Audio", Log::DEBUG, "DeliverMediaPacketOMX time %lld",mpacket.presentation_time);
1808 /* if (mpacket.synched && mpacket.presentation_time <= 0) {
1809 *samplepos = mpacket.length;
1810 firstsynched = false;
1812 Log::getInstance()->log("Audio", Log::DEBUG,
1813 "DeliverMediaPacketOMX Frameskip");
1814 return mpacket.length;
1817 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 2");
1819 UINT headerstrip = 0;
1820 if (mpacket.disconti) {
1821 firstsynched = false;
1822 decompress_buffer_filled=0;
1823 if (cur_input_buf_omx) {
1824 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1826 if (error != OMX_ErrorNone) {
1827 Log::getInstance()->log("Audio", Log::DEBUG,
1828 "OMX_EmptyThisBuffer 1 failed %x", error);
1830 cur_input_buf_omx = NULL;
1834 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 3");
1835 if (mpacket.type != lastAType) {//Format Change //Push data out !
1836 firstsynched = false;
1839 Log::getInstance()->log("Audio", Log::DEBUG,"Notice audio type change %d %d", mpacket.type,lastAType);
1840 lastAType = mpacket.type;
1841 decompress_buffer_filled=0;
1843 if (cur_input_buf_omx) {
1844 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1846 if (error != OMX_ErrorNone) {
1847 Log::getInstance()->log("Audio", Log::DEBUG,
1848 "OMX_EmptyThisBuffer 2 failed %x", error);
1850 cur_input_buf_omx = NULL;
1855 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
1856 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
1858 if (!ChangeAudioPortConfig(true)) {
1859 Log::getInstance()->log("Audio", Log::DEBUG,
1860 "Changing audio port config failed", error);
1863 pthread_setcancelstate(oldcancelstate, NULL);
1864 pthread_setcanceltype(oldcanceltype, NULL);
1868 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 4");
1870 /*Inspect PES-Header */
1871 if (*samplepos == 0 && mpacket.type != MPTYPE_MPEG_AUDIO_LAYER3) {//stripheader
1872 headerstrip = buffer[mpacket.pos_buffer + 8] + 9;
1873 if (mpacket.type == MPTYPE_AC3)
1874 headerstrip += 4; //skip ac3 bytes
1875 *samplepos += headerstrip;
1876 if (mpacket.synched) {
1877 if (cur_input_buf_omx) {
1878 //cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1879 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1881 if (error != OMX_ErrorNone) {
1882 Log::getInstance()->log("Audio", Log::DEBUG,
1883 "OMX_EmptyThisBuffer 3 failed %x", error);
1885 //vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1887 cur_input_buf_omx = NULL;//write out old data
1889 firstsynched = true;
1890 //decompress_buffer_filled=0;
1892 if (!firstsynched) {//
1893 *samplepos = mpacket.length;//if we have not processed at least one
1894 decompress_buffer_filled=0;
1895 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 5");
1896 return mpacket.length;//synched packet ignore it!
1900 if (!cur_input_buf_omx) {
1901 input_bufs_omx_mutex.Lock();
1902 if (input_bufs_omx_free.size()==0) {
1903 input_bufs_omx_mutex.Unlock();
1904 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 6");
1905 //Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample");
1906 return 0; // we do not have a free media sample
1909 cur_input_buf_omx=input_bufs_omx_free.front();
1910 cur_input_buf_omx->nFilledLen=0;
1911 cur_input_buf_omx->nOffset=0;
1912 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
1913 input_bufs_omx_free.pop_front();
1914 input_bufs_omx_mutex.Unlock();
1916 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 7");
1918 if (cur_input_buf_omx->nFilledLen == 0) {//will only be changed on first packet
1919 if (mpacket.synched) {
1920 //Log::getInstance()->log("Audio", Log::DEBUG,
1921 // "packet synched marker");
1923 //lastreftimePTS=mpacket.pts;
1924 if (omx_first_frame) { // TODO time
1925 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
1926 Log::getInstance()->log("Audio", Log::DEBUG, "Starttime");
1927 omx_first_frame = false;
1929 cur_input_buf_omx->nFlags = 0;
1930 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
1932 lastreftimeOMX = mpacket.presentation_time;
1933 //Log::getInstance()->log("Audio", Log::DEBUG,
1934 // "Time code %lld pts %lld dts %lld", lastreftimeOMX, mpacket.pts,mpacket.dts);
1935 lastreftimePTS = mpacket.pts;
1936 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
1938 // Log::getInstance()->log("Audio", Log::DEBUG,
1939 // "packet NOT synched marker");
1940 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
1941 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
1944 if (mpacket.disconti || achange) {
1945 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_DISCONTINUITY;
1946 //mp23codec_context_libav->frame_size=-1;
1947 //ac3codec_context_libav->frame_size=-1;
1951 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 8");
1953 if (*samplepos>mpacket.length) *samplepos=0; //propably the thread got interrupted and sample is not valid any more!
1954 unsigned int haveToCopy=mpacket.length-*samplepos;
1957 while (haveToCopy>0) {
1958 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 9");
1960 unsigned int gotframesize=0;
1962 switch (mpacket.type) {
1963 case MPTYPE_MPEG_AUDIO:
1964 case MPTYPE_MPEG_AUDIO_LAYER3: {
1965 adv = AdvanceMpAudioSync(buffer+mpacket.pos_buffer+*samplepos,
1966 haveToCopy,&gotframesize);
1970 case MPTYPE_AC3_PRE13: {
1971 adv = AdvanceAc3AudioSync(buffer+mpacket.pos_buffer+*samplepos,
1972 haveToCopy,&gotframesize);
1976 case MPTYPE_AAC_LATM: {
1977 adv = AdvanceAacLatmAudioSync(buffer+mpacket.pos_buffer+*samplepos,
1978 haveToCopy,&gotframesize);
1982 if (adv != (int)haveToCopy) {
1987 *samplepos=mpacket.length; //no ac3 sync byte
1988 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 10");
1989 return mpacket.length;
1992 // so everything is fine now do a memcpy
1993 int cancopy=min(haveToCopy,cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen);
1994 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+mpacket.pos_buffer+*samplepos,cancopy);
1995 haveToCopy-=cancopy;
1996 cur_input_buf_omx->nFilledLen+=cancopy;
1997 *samplepos+=cancopy;
1998 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 11");
2000 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2001 if (error != OMX_ErrorNone) {
2002 Log::getInstance()->log("Audio", Log::DEBUG,
2003 "OMX_EmptyThisBuffer 5 failed %x", error);
2005 cur_input_buf_omx=NULL;
2008 input_bufs_omx_mutex.Lock();
2009 if (input_bufs_omx_free.size()==0) {
2010 input_bufs_omx_mutex.Unlock();
2011 // Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample2");
2012 return *samplepos; // we do not have a free media sample
2014 cur_input_buf_omx=input_bufs_omx_free.front();
2015 cur_input_buf_omx->nFilledLen=0;
2016 cur_input_buf_omx->nOffset=0;
2017 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2018 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
2019 input_bufs_omx_free.pop_front();
2020 input_bufs_omx_mutex.Unlock();
2022 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 12");
2025 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 13");
2033 AVCodecContext *current_context;
2034 switch (mpacket.type) {
2035 case MPTYPE_MPEG_AUDIO:
2036 case MPTYPE_MPEG_AUDIO_LAYER3: {
2037 current_context = mp23codec_context_libav;
2038 if (current_context->frame_size<0) framesize=1152; //Maximum framesize
2039 else framesize=current_context->frame_size;
2041 case MPTYPE_AAC_LATM: {
2042 current_context = aaclatmcodec_context_libav;
2045 case MPTYPE_AC3_PRE13: {
2046 current_context = ac3codec_context_libav;
2050 if (decompress_buffer_filled) { // have a remaining paket
2051 incoming_paket_libav.data =(uint8_t*) decompress_buffer;
2052 memcpy(decompress_buffer+decompress_buffer_filled,
2053 buffer+mpacket.pos_buffer+*samplepos,
2054 min(haveToCopy,decompress_buffer_size-decompress_buffer_filled));
2055 incoming_paket_libav.size = decompress_buffer_filled
2056 +min(haveToCopy,decompress_buffer_size-decompress_buffer_filled);
2057 //Log::getInstance()->log("Audio", Log::DEBUG,"Use saved audio buffer %d %d %d",mpacket.type,decompress_buffer_filled,mpacket.synched);
2059 incoming_paket_libav.data =(uint8_t*) buffer+mpacket.pos_buffer+*samplepos;
2060 incoming_paket_libav.size = haveToCopy;
2062 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 18");
2064 while (haveToCopy> 0 && errcount<3) {
2066 //Log::getInstance()->log("Audio", Log::DEBUG,"libav in %d %d",framesize,current_context->frame_size);
2067 //Log::getInstance()->log("Audio", Log::DEBUG, "libav in %d %d",
2068 // framesize, current_context->frame_size);
2070 bool donotdecompress=false;
2071 unsigned int gotframesize=0;
2072 // if (!decompress_buffer_filled) { // only do this if no old data is present
2074 switch (mpacket.type) {
2075 case MPTYPE_MPEG_AUDIO:
2076 case MPTYPE_MPEG_AUDIO_LAYER3: {
2077 adv = AdvanceMpAudioSync(incoming_paket_libav.data,
2078 incoming_paket_libav.size,&gotframesize);
2082 case MPTYPE_AC3_PRE13: {
2083 adv = AdvanceAc3AudioSync(incoming_paket_libav.data,
2084 incoming_paket_libav.size,&gotframesize);
2087 case MPTYPE_AAC_LATM: {
2088 adv = AdvanceAacLatmAudioSync(incoming_paket_libav.data,
2089 incoming_paket_libav.size,&gotframesize);
2093 if (adv > 0 /*&& !decompress_buffer_filled*/) {
2094 incoming_paket_libav.data += adv;
2095 incoming_paket_libav.size-=adv;
2098 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2099 else*/ framesize=haveToCopy;
2100 //Log::getInstance()->log("Audio", Log::DEBUG,"Advance by %d %d from %d",adv,mpacket.type,*samplepos );
2101 if (haveToCopy <= 0) {
2102 // Log::getInstance()->log("Audio", Log::DEBUG,"No sync code in packet remove %d",mpacket.type);
2103 *samplepos=mpacket.length;
2104 return mpacket.length;
2110 if (gotframesize>0 && gotframesize>haveToCopy) {
2111 donotdecompress=true;
2112 errcount=100; // exit loop
2114 // else Log::getInstance()->log("Audio", Log::DEBUG,"Loop run" );
2116 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 19");
2117 if (!donotdecompress) {
2120 pthread_testcancel();
2121 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2122 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2124 if (!omx_running || !mp23codec_context_libav
2125 || !ac3codec_context_libav) {
2126 libav_mutex.Unlock();
2129 libav_mutex.Unlock();
2130 // Log::getInstance()->log("Audio", Log::DEBUG,"libav out");
2131 int save_size=incoming_paket_libav.size;
2133 if ((int)gotframesize <= incoming_paket_libav.size) {
2134 if (gotframesize>0) incoming_paket_libav.size=gotframesize;
2135 len = avcodec_decode_audio4(current_context, decode_frame_libav,
2136 &gotta, &incoming_paket_libav);
2138 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:E %d %d",gotframesize,incoming_paket_libav.size);
2142 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:T %d",len);
2143 incoming_paket_libav.size=save_size;
2144 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out1");
2145 pthread_setcancelstate(oldcancelstate, NULL);
2146 pthread_setcanceltype(oldcanceltype, NULL);
2147 pthread_testcancel();
2153 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out2");
2154 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 20");
2156 libav_mutex.Unlock();
2160 if (decompress_buffer_filled) { // reset to normal decoding
2162 //Log::getInstance()->log("Audio", Log::DEBUG,"saved audio: %d",len);
2163 haveToCopy -= min(len-decompress_buffer_filled,0);
2164 *samplepos += min(len-decompress_buffer_filled,0);
2165 //if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2166 /*else*/ framesize=haveToCopy;
2168 framesize=haveToCopy;
2170 incoming_paket_libav.data =(uint8_t*) buffer+mpacket.pos_buffer+*samplepos;
2172 decompress_buffer_filled=0;
2176 incoming_paket_libav.data += len;
2180 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2181 else*/framesize=haveToCopy;
2184 framesize=haveToCopy;
2187 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 21");
2189 incoming_paket_libav.size =framesize;
2191 //Log::getInstance()->log("Audio", Log::DEBUG,
2194 int dsize = av_samples_get_buffer_size(NULL,
2195 /*current_context->channels*/2, decode_frame_libav->nb_samples,
2196 AV_SAMPLE_FMT_S16, 1);
2197 /* int dsize_in = av_samples_get_buffer_size(NULL,
2198 current_context->channels, decode_frame_libav->nb_samples,
2199 current_context->sample_fmt, 1); -- unused */
2200 //if (current_context->channels==1) dsize*=2; // we convert mono to stereo
2201 if ((cur_input_buf_omx->nFilledLen + dsize)
2202 > cur_input_buf_omx->nAllocLen ) {
2203 // I doubt that this will ever happen
2204 // Log::getInstance()->log("Audio", Log::DEBUG,
2205 // "P 2 Time code %lld pts %lld", lastreftimeOMX, mpacket.pts);
2206 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
2208 if (error != OMX_ErrorNone) {
2209 Log::getInstance()->log("Audio", Log::DEBUG,
2210 "OMX_EmptyThisBuffer 4 failed %x", error);
2212 cur_input_buf_omx = NULL;
2214 if (!cur_input_buf_omx) {
2216 while (count < 10 && omx_running) {
2218 input_bufs_omx_mutex.Lock();
2219 if (input_bufs_omx_free.size() == 0) {
2220 input_bufs_omx_mutex.Unlock();
2221 // Log::getInstance()->log("Audio", Log::DEBUG,
2222 // "Deliver MediaPacket no free sample");
2224 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark22");
2225 if (!omx_running) return *samplepos;
2226 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 23");
2229 cur_input_buf_omx = input_bufs_omx_free.front();
2230 cur_input_buf_omx->nFilledLen = 0;
2231 cur_input_buf_omx->nOffset = 0;
2232 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
2233 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2234 input_bufs_omx_free.pop_front();
2235 input_bufs_omx_mutex.Unlock();
2238 if (!cur_input_buf_omx) return *samplepos;
2243 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy in %d %d %d" ,dsize,current_context->sample_rate,cur_input_buf_omx->nFilledLen);
2246 av_opt_set_int(resam_con_libav, "in_sample_rate",decode_frame_libav->sample_rate,0);
2247 av_opt_set_int(resam_con_libav, "in_sample_fmt",decode_frame_libav->format,0);
2248 av_opt_set_int(resam_con_libav, "in_channel_layout",decode_frame_libav->channel_layout, 0);
2249 //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);
2250 //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);
2252 int ret=avresample_open(resam_con_libav);
2254 Log::getInstance()->log("Audio", Log::ERR,"Opening AV resample failed %d",ret);
2256 uint8_t *output=cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen;
2258 avresample_convert(resam_con_libav, &output,
2259 dsize, decode_frame_libav->nb_samples,
2260 decode_frame_libav->extended_data, decode_frame_libav->linesize[0], decode_frame_libav->nb_samples);
2261 avresample_close(resam_con_libav);
2264 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy out");
2265 cur_input_buf_omx->nFilledLen += dsize;
2267 //Log::getInstance()->log("Audio", Log::DEBUG,"Incomplete mpeg frames in pes packet %d %d",incoming_paket_libav.size,mpacket.length);
2268 /* uint8_t a1=incoming_paket_libav.data[0];
2269 uint8_t a2=incoming_paket_libav.data[1];
2270 uint8_t a3=incoming_paket_libav.data[2];
2271 uint8_t a4=incoming_paket_libav.data[3];*/
2272 // Log::getInstance()->log("Audio", Log::DEBUG,"Header %x %x %x %x",a1,a2,
2277 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 24");
2278 decompress_buffer_filled=0;
2280 //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);
2281 memcpy(decompress_buffer,incoming_paket_libav.data,min(haveToCopy,decompress_buffer_size));
2283 decompress_buffer_filled=min(haveToCopy,decompress_buffer_size);
2287 if (cur_input_buf_omx->nFilledLen) {
2288 //Log::getInstance()->log("Audio", Log::DEBUG,
2289 // "P 3 Time code %lld pts %lld", lastreftimeOMX, mpacket.pts);
2290 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2291 if (error != OMX_ErrorNone) {
2292 Log::getInstance()->log("Audio", Log::DEBUG,
2293 "OMX_EmptyThisBuffer 5 failed %x", error);
2295 //if (mpacket.synched) vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
2296 cur_input_buf_omx = NULL;
2301 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 25");
2303 *samplepos=mpacket.length;
2304 return mpacket.length;
2310 long long AudioOMX::SetStartOffset(long long curreftime, bool *rsync){
2311 VideoOMX *vw=(VideoOMX*)Video::getInstance();
2312 return vw->SetStartAudioOffset(curreftime,rsync);
2315 void AudioOMX::ResetTimeOffsets() {
2316 VideoOMX *vw=(VideoOMX*)Video::getInstance();
2317 vw->ResetTimeOffsets();