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;
823 ac3codec_context_libav->request_channels=2;
825 int avc_ret = avcodec_open2(ac3codec_context_libav, ac3codec_libav, NULL);
827 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
828 libav_mutex.Unlock();
832 aaclatmcodec_context_libav = avcodec_alloc_context3(aaclatmcodec_libav);
833 if (!aaclatmcodec_context_libav) {
834 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for aac decoding context failed!");
838 aaclatmcodec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
839 aaclatmcodec_context_libav->request_channels=2;
841 avc_ret = avcodec_open2(aaclatmcodec_context_libav, aaclatmcodec_libav, NULL);
843 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
844 libav_mutex.Unlock();
849 mp23codec_context_libav = avcodec_alloc_context3(mp23codec_libav);
850 if (!ac3codec_context_libav) {
851 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for mp23 decoding context failed!");
852 libav_mutex.Unlock();
856 mp23codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
857 mp23codec_context_libav->request_channels=2;
859 avc_ret = avcodec_open2(mp23codec_context_libav, mp23codec_libav, NULL);
861 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
862 libav_mutex.Unlock();
866 resam_con_libav = avresample_alloc_context();
867 if (resam_con_libav == NULL) {
868 Log::getInstance()->log("Audio", Log::DEBUG,
869 "Alloc resample context failed");
873 av_opt_set_int(resam_con_libav, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0); // our standard format
874 av_opt_set_int(resam_con_libav, "out_sample_rate",48000,0);
875 av_opt_set_int(resam_con_libav, "out_sample_fmt",AV_SAMPLE_FMT_S16,0);
876 av_opt_set_int(resam_con_libav, "matrix_encoding",AV_MATRIX_ENCODING_DPLII,0);
878 av_opt_set_int(resam_con_libav, "in_sample_rate",48000,0);
879 av_opt_set_int(resam_con_libav, "in_sample_fmt",AV_SAMPLE_FMT_S16,0);
880 av_opt_set_int(resam_con_libav, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0); //just an example
883 av_init_packet(&incoming_paket_libav);
884 decode_frame_libav=avcodec_alloc_frame();
885 libav_mutex.Unlock();
886 decompress_buffer_filled=0;
893 void AudioOMX::DeinitDecoderLibAV() {
897 if (ac3codec_context_libav) {
898 avcodec_close(ac3codec_context_libav);
899 av_free(ac3codec_context_libav);
900 ac3codec_context_libav = NULL;
902 avcodec_close(aaclatmcodec_context_libav);
903 av_free(aaclatmcodec_context_libav);
904 aaclatmcodec_context_libav = NULL;
906 av_free(decode_frame_libav);
908 avcodec_close(mp23codec_context_libav);
909 av_free(mp23codec_context_libav);
910 mp23codec_context_libav = NULL;
912 avresample_free(&resam_con_libav);
913 resam_con_libav=NULL;
917 libav_mutex.Unlock();
922 int AudioOMX::AllocateCodecsOMX()
925 static OMX_CALLBACKTYPE callbacks= {&VideoOMX::EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
927 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX");
928 //Clock, move later to audio
929 VideoOMX *video=(VideoOMX*)Video::getInstance();
931 if (!InitDecoderLibAV()) return 0;;
934 OMX_PORT_PARAM_TYPE p_param;
935 memset(&p_param,0,sizeof(p_param));
936 p_param.nSize=sizeof(p_param);
937 p_param.nVersion.nVersion=OMX_VERSION;
940 if (!video->getClockAudioandInit(&omx_clock,&omx_clock_output_port)){
941 return 0;// get the clock and init it if necessary
945 if (!video->idleClock()) {
950 error = OMX_GetHandle(&omx_aud_rend, L_VPE_OMX_AUDIO_REND, NULL, &callbacks);
951 if (error != OMX_ErrorNone) {
952 Log::getInstance()->log("Audio", Log::DEBUG,
953 "Init OMX audio rend failed %x", error);
954 video->UnlockClock();
955 DeAllocateCodecsOMX();
959 if (!ChangeAudioDestination()) {
960 video->UnlockClock();
961 DeAllocateCodecsOMX();
965 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioInit, &p_param);
966 if (error != OMX_ErrorNone) {
967 Log::getInstance()->log("Audio", Log::DEBUG,
968 "Init OMX audio rend OMX_GetParameter failed %x", error);
969 video->UnlockClock();
970 DeAllocateCodecsOMX();
973 omx_rend_input_port = p_param.nStartPortNumber;
979 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamOtherInit, &p_param);
980 if (error != OMX_ErrorNone) {
981 Log::getInstance()->log("Audio", Log::DEBUG,
982 "Init OMX aud rend OMX_GetParameter failed %x", error);
983 video->UnlockClock();
984 DeAllocateCodecsOMX();
987 // buggy return value
988 omx_rend_clock_port = p_param.nStartPortNumber;
993 /* error=OMX_GetHandle(&omx_aud_dec,VPE_OMX_AUDIO_DECODER,NULL,&callbacks);
995 if (error!=OMX_ErrorNone){
996 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder failed %x", error);
997 video->UnlockClock();
998 DeAllocateCodecsOMX();
1002 error=OMX_GetParameter(omx_aud_dec,OMX_IndexParamAudioInit,&p_param);
1003 if (error!=OMX_ErrorNone){
1004 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder OMX_GetParameter failed %x", error);
1005 video->UnlockClock();
1006 DeAllocateCodecsOMX();
1009 omx_codec_input_port=p_param.nStartPortNumber;
1010 omx_codec_output_port=p_param.nStartPortNumber+1;
1012 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port) || !video->DisablePort(omx_aud_dec,omx_codec_output_port)) {
1013 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio decoder failed");
1014 video->UnlockClock();
1015 DeAllocateCodecsOMX();
1022 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true) ) {
1023 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio rend failed %d",omx_rend_input_port);
1024 video->UnlockClock();
1025 DeAllocateCodecsOMX();
1029 if ( !video->DisablePort(omx_aud_rend, omx_rend_clock_port, true)) {
1030 Log::getInstance()->log("Audio", Log::DEBUG,
1031 "Disable Ports OMX rend clock port failed %d",omx_rend_clock_port);
1032 video->UnlockClock();
1033 DeAllocateCodecsOMX();
1043 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_aud_rend,omx_rend_clock_port);
1044 if (error!=OMX_ErrorNone){
1045 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);
1046 video->UnlockClock();
1047 DeAllocateCodecsOMX();
1051 if (!video->EnablePort(omx_clock,omx_clock_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_clock_port,false)
1053 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX clock rend failed");
1054 video->UnlockClock();
1055 DeAllocateCodecsOMX();
1059 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1060 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend idle ChangeComponentState");
1061 video->UnlockClock();
1062 DeAllocateCodecsOMX();
1069 if ( !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_clock_port)) {
1070 video->UnlockClock();
1071 DeAllocateCodecsOMX();
1075 if ( !video->CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1076 video->UnlockClock();
1077 DeAllocateCodecsOMX();
1083 if (!ChangeAudioPortConfig(false)){
1084 Log::getInstance()->log("Audio", Log::NOTICE, "Change AudioPortConfig failed");
1085 video->UnlockClock();
1086 DeAllocateCodecsOMX();
1090 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1091 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1092 DeAllocateCodecsOMX();
1098 if (!PrepareInputBufsOMX(true)) {
1099 video->UnlockClock();
1100 DeAllocateCodecsOMX();
1106 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,omx_aud_rend,omx_rend_input_port);
1107 if (error!=OMX_ErrorNone){
1108 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel dec to render failed %x", error);
1109 video->UnlockClock();
1110 DeAllocateCodecsOMX();
1116 /* if (!video->EnablePort(omx_aud_dec,omx_codec_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_input_port,false)
1118 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX codec rend failed");
1119 video->UnlockClock();
1120 DeAllocateCodecsOMX();
1124 /* if ( !video->CommandFinished(omx_aud_dec,OMX_CommandPortEnable,omx_codec_output_port)
1125 || !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1126 video->UnlockClock();
1127 DeAllocateCodecsOMX();
1131 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateExecuting)) {
1132 Log::getInstance()->log("Audio", Log::DEBUG, "omx_aud_rend ChangeComponentState Execute");
1133 video->UnlockClock();
1134 DeAllocateCodecsOMX();
1139 video->UnlockClock();
1143 video->clockUnpause();
1146 if (!video->setClockExecutingandRunning()) return 0;
1148 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX finished");
1156 int AudioOMX::PrepareInputBufsOMX(bool setportdef) //needs to be called with locvke omx clock mutex
1158 VideoOMX *video=(VideoOMX*)Video::getInstance();
1159 OMX_ERRORTYPE error;
1160 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1161 memset(&port_def_type,0,sizeof(port_def_type));
1162 port_def_type.nSize=sizeof(port_def_type);
1163 port_def_type.nVersion.nVersion=OMX_VERSION;
1164 port_def_type.nPortIndex=omx_rend_input_port;//omx_codec_input_port;
1166 error=OMX_GetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1168 if (error!=OMX_ErrorNone){
1169 Log::getInstance()->log("Audio", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1174 port_def_type.nBufferCountActual=2;
1175 port_def_type.nBufferSize=max(port_def_type.nBufferSize,50000); // for transcoder important
1177 error=OMX_SetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1179 if (error!=OMX_ErrorNone){
1180 Log::getInstance()->log("Audio", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1185 error=OMX_SendCommand(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port/*codec*/,0);
1186 if (error!=OMX_ErrorNone){
1187 Log::getInstance()->log("Audio", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1191 input_bufs_omx_mutex.Lock();
1192 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1193 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1194 error=OMX_AllocateBuffer(omx_aud_rend/*dec*/,&buf_head,omx_rend_input_port/*codec*/,NULL,port_def_type.nBufferSize);
1195 if (error!=OMX_ErrorNone){
1196 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1197 input_bufs_omx_mutex.Unlock();
1200 input_bufs_omx_all.push_back(buf_head);
1201 input_bufs_omx_free.push_back(buf_head);
1203 omx_first_frame=true;
1206 cur_input_buf_omx=NULL;
1207 input_bufs_omx_mutex.Unlock();
1209 if (!video->CommandFinished(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port /*codec*/)) {
1216 int AudioOMX::DestroyInputBufsOMX() //call with clock mutex locked
1218 OMX_ERRORTYPE error;
1220 cur_input_buf_omx=NULL;
1221 input_bufs_omx_mutex.Lock();
1222 for (UINT i=0; i< input_bufs_omx_all.size();i++) {
1223 error=OMX_FreeBuffer(omx_aud_rend/*dec*/,omx_rend_input_port/*codec*/,input_bufs_omx_all[i]);
1224 if (error!=OMX_ErrorNone){
1225 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1226 input_bufs_omx_mutex.Unlock();
1231 input_bufs_omx_all.clear();
1232 input_bufs_omx_free.clear();
1233 input_bufs_omx_mutex.Unlock();
1238 int AudioOMX::DestroyInputBufsOMXwhilePlaying() //call with clock mutex locked
1240 //OMX_ERRORTYPE error;
1242 cur_input_buf_omx=NULL;
1243 input_bufs_omx_mutex.Lock();
1244 while (input_bufs_omx_all.size()>0) {
1245 if (input_bufs_omx_free.size()>0) {
1246 // Destroy one buffer
1247 vector<OMX_BUFFERHEADERTYPE*>::iterator itty=input_bufs_omx_all.begin();
1248 OMX_BUFFERHEADERTYPE* cur_buf=input_bufs_omx_free.front();
1249 for (; itty!= input_bufs_omx_all.end();itty++) {
1250 if ((*itty)==cur_buf) {
1251 input_bufs_omx_all.erase(itty);
1252 input_bufs_omx_free.pop_front();
1257 input_bufs_omx_mutex.Unlock();
1259 input_bufs_omx_mutex.Lock();
1263 Log::getInstance()->log("Audio", Log::DEBUG, "DestroyInputBufsOMXwhilePlaying %d %d", input_bufs_omx_all.size(),input_bufs_omx_free.size());
1264 input_bufs_omx_mutex.Unlock();
1269 int AudioOMX::DeAllocateCodecsOMX()
1271 OMX_ERRORTYPE error;
1273 VideoOMX *video=(VideoOMX*)Video::getInstance();
1274 Log::getInstance()->log("Audio", Log::DEBUG, "enter deallocatecodecsomx");
1278 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 1");
1279 if (cur_input_buf_omx) {
1280 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1281 error=video->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,cur_input_buf_omx);
1282 if (error!=OMX_ErrorNone) {
1283 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_EmptyThisBuffer 6 failed %x", error);
1286 cur_input_buf_omx=NULL;//write out old data
1288 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 2");
1291 if (omx_aud_rend/*dec*/) {
1292 // first stop the omx elements
1293 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1294 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1296 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 3");
1298 video->UnlockClock();
1299 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 4");
1301 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 5");
1304 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1305 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend ChangeComponentState");
1308 // TODO proper deinit sequence
1309 // first flush all buffers
1311 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
1312 if (error!=OMX_ErrorNone) {
1313 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend in failed %x", error);
1317 /* error=OMX_SendCommand(omx_aud_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
1318 if (error!=OMX_ErrorNone){
1319 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1324 /* if (!video->CommandFinished(omx_aud_dec,OMX_CommandFlush,omx_codec_input_port)) {
1325 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd codec input failed");
1330 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1331 if (error!=OMX_ErrorNone){
1332 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1336 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_clock_port, NULL);
1337 if (error!=OMX_ErrorNone){
1338 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend clock failed %x", error);
1341 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6");
1343 if (!video->CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1344 !video->CommandFinished(omx_aud_rend,OMX_CommandFlush,omx_rend_clock_port)) {
1345 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd clock shed failed");
1348 DestroyInputBufsOMX(); //We have to make sure that no buffers are in use
1349 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6a");
1350 DeinitDecoderLibAV();
1351 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 7");
1354 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true)) {
1355 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 1");
1358 /* if (!video->DisablePort(omx_aud_dec,omx_codec_output_port,true)) {
1359 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 6");
1362 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port,true)) {
1363 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 7");
1367 if (!video->DisablePort(omx_aud_rend,omx_rend_clock_port,true)) {
1368 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 4");
1371 if (!video->DisablePort(omx_clock,omx_clock_output_port,true)) {
1372 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 5");
1377 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,NULL,NULL);
1378 if (error!=OMX_ErrorNone) {
1379 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1385 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_input_port,NULL,0);
1386 if (error!=OMX_ErrorNone) {
1387 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1391 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,0);
1392 if (error!=OMX_ErrorNone) {
1393 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1397 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_clock_port,NULL,0);
1398 if (error!=OMX_ErrorNone) {
1399 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1402 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 8");
1405 //error=OMX_FreeHandle(omx_aud_dec);
1406 error=OMX_FreeHandle(omx_aud_rend);
1407 video->UnlockClock();
1408 video->destroyClock();
1409 omx_aud_rend/*dec*/=NULL;
1410 if (error!=OMX_ErrorNone) {
1411 Log::getInstance()->log("Audio", Log::DEBUG, "FreeHandle failed %d", error);
1415 video->UnlockClock();
1416 DeinitDecoderLibAV();
1418 Log::getInstance()->log("Audio", Log::DEBUG, "leave deallocate codecs OMX");
1425 int AudioOMX::stop()
1427 if (!initted) return 0;
1429 Log::getInstance()->log("Audio", Log::DEBUG, "Audio stop called");
1430 DeAllocateCodecsOMX();
1431 //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1435 int AudioOMX::mute() {
1438 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
1439 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1443 OMX_AUDIO_CONFIG_MUTETYPE amute;
1444 memset(&amute, 0, sizeof(amute));
1445 amute.nSize = sizeof(amute);
1446 amute.nVersion.nVersion = OMX_VERSION;
1447 amute.nPortIndex = omx_rend_input_port;
1448 amute.bMute = OMX_TRUE;
1449 OMX_ERRORTYPE error= OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1451 if (error != OMX_ErrorNone) {
1452 Log::getInstance()->log("Audio", Log::DEBUG,
1453 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1454 omx_rend_input_port);
1466 int AudioOMX::unMute()
1468 if (!initted) return 0;
1470 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
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_FALSE;
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::pause() {
1502 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1508 int AudioOMX::unPause()
1510 if (!initted) return 0;
1512 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1519 int AudioOMX::reset()
1521 if (!initted) return 0;
1523 Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
1524 DeAllocateCodecsOMX();
1526 // if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1527 // Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
1528 // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
1534 int AudioOMX::setVolume(int tvolume)
1536 // parameter: 0 for silence, 20 for full
1537 if ((tvolume < 0) || (tvolume > 20)) return 0;
1539 // volume = 2 * (20 - volume);
1540 // Right, that one was rubbish... 0-10 were almost
1541 // inaudible, 11-20 did what should have been done
1542 // over the whole 0-20 range
1546 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1550 OMX_AUDIO_CONFIG_VOLUMETYPE avol;
1551 memset(&avol, 0, sizeof(avol));
1552 avol.nSize = sizeof(avol);
1553 avol.nVersion.nVersion = OMX_VERSION;
1554 avol.nPortIndex = omx_rend_input_port;
1555 avol.bLinear=OMX_FALSE;
1556 avol.sVolume.nValue =(volume-20)*200;
1557 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioVolume,
1559 if (error != OMX_ErrorNone) {
1560 Log::getInstance()->log("Audio", Log::DEBUG,
1561 "Set OMX_IndexConfigAudioVolume failed %x %d", error,
1562 omx_rend_input_port);
1574 int AudioOMX::test()
1577 // return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
1579 /* aud_sync_parms_t a;
1583 // int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
1586 /*OK*/ //printf("Audio sync disable = %i\n", b);
1594 unsigned int AudioOMX::AdvanceMpAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1596 if (size<=2) return size; // silly;
1597 unsigned int atest=0;
1599 //inspired by libav parsing code
1600 while (atest+1<size) {
1601 if (data[atest]==0xFF && (data[atest+1] &0xe0)==0xe0) {
1602 if ((data[atest+1] & 0x18) == 0x08) {atest++;continue;} //sanity check: mpeg version ID 01 -> reserved
1603 //sanity check inspired by libav
1606 const int sample_rates[4]={44100,48000,32000,0};
1607 const short bitrate_tab[2][3][15] = { { { 0, 32, 64, 96, 128, 160,
1608 192, 224, 256, 288, 320, 352, 384, 416, 448 }, { 0, 32, 48,
1609 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 }, {
1610 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
1611 256, 320 } }, { { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1612 160, 176, 192, 224, 256 }, { 0, 8, 16, 24, 32, 40, 48, 56,
1613 64, 80, 96, 112, 128, 144, 160 }, { 0, 8, 16, 24, 32, 40,
1614 48, 56, 64, 80, 96, 112, 128, 144, 160 } } };
1617 int layer=4-((data[atest+1]&0x06)>>1);
1618 if (layer==4) {atest++;continue;} //sanity check
1620 int bitrate_index=(data[atest+2]&0xf0)>>4;
1621 if (bitrate_index==0x0f || bitrate_index==0x0) {atest++;continue;} //sanity check
1623 int samplerate_index=(data[atest+2]&0x0C)>>2;
1624 if (samplerate_index==0x03) {atest++;continue;} //sanity check
1626 int padding=(data[atest+2]&2)>>1;
1627 if (0x10 & data[atest+1]) {
1628 lsf=((data[atest+1]) &0x8)?0:1;
1634 int sample_rate=sample_rates[ samplerate_index]>>(lsf+mpeg2);
1636 int temp_frame_size=bitrate_tab[lsf][layer - 1][bitrate_index];
1638 frame_size=(temp_frame_size*12000)/sample_rate;
1639 frame_size=(frame_size+padding)*4;
1640 } else if (layer==2) {
1641 frame_size=(temp_frame_size*144000)/sample_rate;
1642 frame_size=frame_size+padding;
1644 frame_size=(temp_frame_size*144000)/(sample_rate<<lsf);
1645 frame_size=frame_size+padding;
1647 unsigned int sameheadertest=(data[atest]<<24)|(data[atest+1]<<16) |(data[atest+2]<<8);
1648 const unsigned mask=(0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)); // from libav
1649 if (!mp3sameheader) {
1650 mp3sameheader=sameheadertest;
1651 mp3sameheadercount=2;
1653 if ((mp3sameheader& mask)== (sameheadertest &mask)) mp3sameheadercount++;
1654 else mp3sameheadercount=0;
1655 mp3sameheader=sameheadertest;
1657 //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);
1658 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME DIAG: %x %x %x %d",data[atest],data[atest+1],data[atest+2],mp3sameheadercount);
1660 if (mp3sameheadercount>4) {
1661 *framesize=frame_size;
1662 return atest; // probably FrameSync
1663 } //skip it if the header changes too frequently
1670 unsigned int AudioOMX::AdvanceAc3AudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1672 if (size<=4) return size; // silly;
1673 const int frm_size_tab[] = { 64, 64, 80, 80, 96, 96, 112, 112, 128, 128,
1674 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384, 384, 448,
1675 448, 512, 512, 640, 640, 768, 768, 896, 896, 1024, 1024, 1152,
1676 1152, 1280, 1280, };
1677 unsigned int atest=0;
1678 *framesize=20000; //if we do not find a start code do not decompress
1679 while (atest+4<size) {
1680 if (data[atest]==0x0B && data[atest+1]==0x77) {
1681 // now figure out the length of the frame
1682 unsigned char code=data[atest+4];
1683 unsigned char fscod=(code& 0xC0)>>6;
1684 unsigned char frmsize=(code &0x3f);
1685 if (fscod!=0) Log::getInstance()->log("Audio", Log::DEBUG, "warning we only support 48 KHz sampling rate");
1686 *framesize=frm_size_tab[frmsize]*2;
1687 return atest; // probably FrameSync
1694 unsigned int AudioOMX::AdvanceAacLatmAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1696 if (size<=4) return size; // silly;
1697 unsigned int atest=0;
1698 *framesize=20000; //if we do not find a start code do not decompress
1699 while (atest+4<size) {
1700 if (data[atest] ==0x56 && (data[atest+1]& 0xe0)==0xe0) {
1701 // now figure out the length of the frame
1702 unsigned int length= ((0x1f & data[atest+1])<<8) | data[atest+2];
1703 *framesize=length+3;
1704 return atest; // probably FrameSync
1712 void AudioOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1714 packet = mplist.front();
1717 UINT AudioOMX::DeliverMediaSample(UCHAR* buffer, UINT* samplepos) {
1718 DeliverMediaPacket(packet, buffer, samplepos);
1719 if (*samplepos == packet.length) {
1727 long long AudioOMX::correctAudioLatency(long long pts,int addsamples,int srate) {
1729 VideoOMX *video = (VideoOMX*) Video::getInstance();
1731 OMX_PARAM_U32TYPE audio_lat;
1732 OMX_ERRORTYPE error;
1733 memset(&audio_lat, 0, sizeof(audio_lat));
1734 audio_lat.nSize = sizeof(audio_lat);
1735 audio_lat.nVersion.nVersion = OMX_VERSION;
1736 audio_lat.nPortIndex = omx_rend_input_port;
1738 error = OMX_GetConfig(omx_aud_rend, OMX_IndexConfigAudioRenderingLatency,
1740 video->UnlockClock();
1741 if (error != OMX_ErrorNone) {
1742 Log::getInstance()->log("Audio", Log::DEBUG,
1743 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
1744 omx_rend_input_port);
1745 return pts; // no correction in case of error
1747 /*Log::getInstance()->log("Audio", Log::DEBUG, "Current audio latency %d",
1750 long long workpts=0;
1751 workpts+=addsamples;
1752 workpts-=audio_lat.nU32;
1753 workpts*=10LL*1000LL*1000LL;
1754 workpts=workpts/((long long)srate); //one second /samplerate
1760 bool AudioOMX::DrainTargetBufferFull()
1762 //Check, if we have OMX output buffers
1764 input_bufs_omx_mutex.Lock();
1765 full=(input_bufs_omx_free.size()==0);
1766 input_bufs_omx_mutex.Unlock();
1772 UINT AudioOMX::DeliverMediaPacket(MediaPacket packet, const UCHAR* buffer,
1774 /*First Check, if we have an audio sample*/
1775 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1777 OMX_ERRORTYPE error;
1778 //Log *logger=Log::getInstance();
1779 if (vw->InIframemode()) {
1782 return 0; //Not in iframe mode!
1785 if (!omx_running) return 0; // if we are not runnig do not do this
1786 if (vw->isClockPaused()) return 0; //Block if we pause
1787 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 1");
1789 //Log::getInstance()->log("Audio", Log::DEBUG, "DeliverMediaPacketOMX time %lld",packet.presentation_time);
1791 /* if (packet.synched && packet.presentation_time <= 0) {
1792 *samplepos = packet.length;
1793 firstsynched = false;
1795 Log::getInstance()->log("Audio", Log::DEBUG,
1796 "DeliverMediaPacketOMX Frameskip");
1797 return packet.length;
1800 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 2");
1802 UINT headerstrip = 0;
1803 if (packet.disconti) {
1804 firstsynched = false;
1805 decompress_buffer_filled=0;
1806 if (cur_input_buf_omx) {
1807 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1809 if (error != OMX_ErrorNone) {
1810 Log::getInstance()->log("Audio", Log::DEBUG,
1811 "OMX_EmptyThisBuffer 1 failed %x", error);
1813 cur_input_buf_omx = NULL;
1817 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 3");
1818 if (packet.type != lastAType) {//Format Change //Push data out !
1819 firstsynched = false;
1822 Log::getInstance()->log("Audio", Log::DEBUG,"Notice audio type change %d %d", packet.type,lastAType);
1823 lastAType = packet.type;
1824 decompress_buffer_filled=0;
1826 if (cur_input_buf_omx) {
1827 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1829 if (error != OMX_ErrorNone) {
1830 Log::getInstance()->log("Audio", Log::DEBUG,
1831 "OMX_EmptyThisBuffer 2 failed %x", error);
1833 cur_input_buf_omx = NULL;
1838 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
1839 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
1841 if (!ChangeAudioPortConfig(true)) {
1842 Log::getInstance()->log("Audio", Log::DEBUG,
1843 "Changing audio port config failed", error);
1846 pthread_setcancelstate(oldcancelstate, NULL);
1847 pthread_setcanceltype(oldcanceltype, NULL);
1851 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 4");
1853 /*Inspect PES-Header */
1854 if (*samplepos == 0 && packet.type != MPTYPE_MPEG_AUDIO_LAYER3) {//stripheader
1855 headerstrip = buffer[packet.pos_buffer + 8] + 9;
1856 if (packet.type == MPTYPE_AC3)
1857 headerstrip += 4; //skip ac3 bytes
1858 *samplepos += headerstrip;
1859 if (packet.synched) {
1860 if (cur_input_buf_omx) {
1861 //cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1862 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1864 if (error != OMX_ErrorNone) {
1865 Log::getInstance()->log("Audio", Log::DEBUG,
1866 "OMX_EmptyThisBuffer 3 failed %x", error);
1868 //vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1870 cur_input_buf_omx = NULL;//write out old data
1872 firstsynched = true;
1873 //decompress_buffer_filled=0;
1875 if (!firstsynched) {//
1876 *samplepos = packet.length;//if we have not processed at least one
1877 decompress_buffer_filled=0;
1878 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 5");
1879 return packet.length;//synched packet ignore it!
1883 if (!cur_input_buf_omx) {
1884 input_bufs_omx_mutex.Lock();
1885 if (input_bufs_omx_free.size()==0) {
1886 input_bufs_omx_mutex.Unlock();
1887 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 6");
1888 //Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample");
1889 return 0; // we do not have a free media sample
1892 cur_input_buf_omx=input_bufs_omx_free.front();
1893 cur_input_buf_omx->nFilledLen=0;
1894 cur_input_buf_omx->nOffset=0;
1895 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
1896 input_bufs_omx_free.pop_front();
1897 input_bufs_omx_mutex.Unlock();
1899 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 7");
1901 if (cur_input_buf_omx->nFilledLen == 0) {//will only be changed on first packet
1902 if (packet.synched) {
1903 //Log::getInstance()->log("Audio", Log::DEBUG,
1904 // "packet synched marker");
1906 //lastreftimePTS=packet.pts;
1907 if (omx_first_frame) { // TODO time
1908 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
1909 Log::getInstance()->log("Audio", Log::DEBUG, "Starttime");
1910 omx_first_frame = false;
1912 cur_input_buf_omx->nFlags = 0;
1913 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
1915 lastreftimeOMX = packet.presentation_time;
1916 //Log::getInstance()->log("Audio", Log::DEBUG,
1917 // "Time code %lld pts %lld dts %lld", lastreftimeOMX, packet.pts,packet.dts);
1918 lastreftimePTS = packet.pts;
1919 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
1921 // Log::getInstance()->log("Audio", Log::DEBUG,
1922 // "packet NOT synched marker");
1923 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
1924 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
1927 if (packet.disconti || achange) {
1928 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_DISCONTINUITY;
1929 //mp23codec_context_libav->frame_size=-1;
1930 //ac3codec_context_libav->frame_size=-1;
1934 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 8");
1936 if (*samplepos>packet.length) *samplepos=0; //propably the thread got interrupted and sample is not valid any more!
1937 unsigned int haveToCopy=packet.length-*samplepos;
1940 while (haveToCopy>0) {
1941 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 9");
1943 unsigned int gotframesize=0;
1945 switch (packet.type) {
1946 case MPTYPE_MPEG_AUDIO:
1947 case MPTYPE_MPEG_AUDIO_LAYER3: {
1948 adv = AdvanceMpAudioSync(buffer+packet.pos_buffer+*samplepos,
1949 haveToCopy,&gotframesize);
1953 case MPTYPE_AC3_PRE13: {
1954 adv = AdvanceAc3AudioSync(buffer+packet.pos_buffer+*samplepos,
1955 haveToCopy,&gotframesize);
1959 case MPTYPE_AAC_LATM: {
1960 adv = AdvanceAacLatmAudioSync(buffer+packet.pos_buffer+*samplepos,
1961 haveToCopy,&gotframesize);
1965 if (adv != (int)haveToCopy) {
1970 *samplepos=packet.length; //no ac3 sync byte
1971 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 10");
1972 return packet.length;
1975 // so everything is fine now do a memcpy
1976 int cancopy=min(haveToCopy,cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen);
1977 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
1978 haveToCopy-=cancopy;
1979 cur_input_buf_omx->nFilledLen+=cancopy;
1980 *samplepos+=cancopy;
1981 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 11");
1983 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
1984 if (error != OMX_ErrorNone) {
1985 Log::getInstance()->log("Audio", Log::DEBUG,
1986 "OMX_EmptyThisBuffer 5 failed %x", error);
1988 cur_input_buf_omx=NULL;
1991 input_bufs_omx_mutex.Lock();
1992 if (input_bufs_omx_free.size()==0) {
1993 input_bufs_omx_mutex.Unlock();
1994 // Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample2");
1995 return *samplepos; // we do not have a free media sample
1997 cur_input_buf_omx=input_bufs_omx_free.front();
1998 cur_input_buf_omx->nFilledLen=0;
1999 cur_input_buf_omx->nOffset=0;
2000 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2001 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
2002 input_bufs_omx_free.pop_front();
2003 input_bufs_omx_mutex.Unlock();
2005 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 12");
2008 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 13");
2016 AVCodecContext *current_context;
2017 switch (packet.type) {
2018 case MPTYPE_MPEG_AUDIO:
2019 case MPTYPE_MPEG_AUDIO_LAYER3: {
2020 current_context = mp23codec_context_libav;
2021 if (current_context->frame_size<0) framesize=1152; //Maximum framesize
2022 else framesize=current_context->frame_size;
2024 case MPTYPE_AAC_LATM: {
2025 current_context = aaclatmcodec_context_libav;
2028 case MPTYPE_AC3_PRE13: {
2029 current_context = ac3codec_context_libav;
2033 if (decompress_buffer_filled) { // have a remaining paket
2034 incoming_paket_libav.data =(uint8_t*) decompress_buffer;
2035 memcpy(decompress_buffer+decompress_buffer_filled,
2036 buffer+packet.pos_buffer+*samplepos,
2037 min(haveToCopy,decompress_buffer_size-decompress_buffer_filled));
2038 incoming_paket_libav.size = decompress_buffer_filled
2039 +min(haveToCopy,decompress_buffer_size-decompress_buffer_filled);
2040 //Log::getInstance()->log("Audio", Log::DEBUG,"Use saved audio buffer %d %d %d",packet.type,decompress_buffer_filled,packet.synched);
2042 incoming_paket_libav.data =(uint8_t*) buffer+packet.pos_buffer+*samplepos;
2043 incoming_paket_libav.size = haveToCopy;
2045 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 18");
2047 while (haveToCopy> 0 && errcount<3) {
2049 //Log::getInstance()->log("Audio", Log::DEBUG,"libav in %d %d",framesize,current_context->frame_size);
2050 //Log::getInstance()->log("Audio", Log::DEBUG, "libav in %d %d",
2051 // framesize, current_context->frame_size);
2053 bool donotdecompress=false;
2054 unsigned int gotframesize=0;
2055 // if (!decompress_buffer_filled) { // only do this if no old data is present
2057 switch (packet.type) {
2058 case MPTYPE_MPEG_AUDIO:
2059 case MPTYPE_MPEG_AUDIO_LAYER3: {
2060 adv = AdvanceMpAudioSync(incoming_paket_libav.data,
2061 incoming_paket_libav.size,&gotframesize);
2065 case MPTYPE_AC3_PRE13: {
2066 adv = AdvanceAc3AudioSync(incoming_paket_libav.data,
2067 incoming_paket_libav.size,&gotframesize);
2070 case MPTYPE_AAC_LATM: {
2071 adv = AdvanceAacLatmAudioSync(incoming_paket_libav.data,
2072 incoming_paket_libav.size,&gotframesize);
2076 if (adv > 0 /*&& !decompress_buffer_filled*/) {
2077 incoming_paket_libav.data += adv;
2078 incoming_paket_libav.size-=adv;
2081 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2082 else*/ framesize=haveToCopy;
2083 //Log::getInstance()->log("Audio", Log::DEBUG,"Advance by %d %d from %d",adv,packet.type,*samplepos );
2084 if (haveToCopy <= 0) {
2085 // Log::getInstance()->log("Audio", Log::DEBUG,"No sync code in packet remove %d",packet.type);
2086 *samplepos=packet.length;
2087 return packet.length;
2093 if (gotframesize>0 && gotframesize>haveToCopy) {
2094 donotdecompress=true;
2095 errcount=100; // exit loop
2097 // else Log::getInstance()->log("Audio", Log::DEBUG,"Loop run" );
2099 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 19");
2100 if (!donotdecompress) {
2103 pthread_testcancel();
2104 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2105 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2107 if (!omx_running || !mp23codec_context_libav
2108 || !ac3codec_context_libav) {
2109 libav_mutex.Unlock();
2112 libav_mutex.Unlock();
2113 // Log::getInstance()->log("Audio", Log::DEBUG,"libav out");
2114 int save_size=incoming_paket_libav.size;
2116 if ((int)gotframesize <= incoming_paket_libav.size) {
2117 if (gotframesize>0) incoming_paket_libav.size=gotframesize;
2118 len = avcodec_decode_audio4(current_context, decode_frame_libav,
2119 &gotta, &incoming_paket_libav);
2121 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:E %d %d",gotframesize,incoming_paket_libav.size);
2125 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:T %d",len);
2126 incoming_paket_libav.size=save_size;
2127 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out1");
2128 pthread_setcancelstate(oldcancelstate, NULL);
2129 pthread_setcanceltype(oldcanceltype, NULL);
2130 pthread_testcancel();
2136 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out2");
2137 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 20");
2139 libav_mutex.Unlock();
2143 if (decompress_buffer_filled) { // reset to normal decoding
2145 //Log::getInstance()->log("Audio", Log::DEBUG,"saved audio: %d",len);
2146 haveToCopy -= min(len-decompress_buffer_filled,0);
2147 *samplepos += min(len-decompress_buffer_filled,0);
2148 //if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2149 /*else*/ framesize=haveToCopy;
2151 framesize=haveToCopy;
2153 incoming_paket_libav.data =(uint8_t*) buffer+packet.pos_buffer+*samplepos;
2155 decompress_buffer_filled=0;
2159 incoming_paket_libav.data += len;
2163 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2164 else*/framesize=haveToCopy;
2167 framesize=haveToCopy;
2170 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 21");
2172 incoming_paket_libav.size =framesize;
2174 //Log::getInstance()->log("Audio", Log::DEBUG,
2177 int dsize = av_samples_get_buffer_size(NULL,
2178 /*current_context->channels*/2, decode_frame_libav->nb_samples,
2179 AV_SAMPLE_FMT_S16, 1);
2180 int dsize_in = av_samples_get_buffer_size(NULL,
2181 current_context->channels, decode_frame_libav->nb_samples,
2182 current_context->sample_fmt, 1);
2183 //if (current_context->channels==1) dsize*=2; // we convert mono to stereo
2184 if ((cur_input_buf_omx->nFilledLen + dsize)
2185 > cur_input_buf_omx->nAllocLen ) {
2186 // I doubt that this will ever happen
2187 // Log::getInstance()->log("Audio", Log::DEBUG,
2188 // "P 2 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
2189 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
2191 if (error != OMX_ErrorNone) {
2192 Log::getInstance()->log("Audio", Log::DEBUG,
2193 "OMX_EmptyThisBuffer 4 failed %x", error);
2195 cur_input_buf_omx = NULL;
2197 if (!cur_input_buf_omx) {
2199 while (count < 10 && omx_running) {
2201 input_bufs_omx_mutex.Lock();
2202 if (input_bufs_omx_free.size() == 0) {
2203 input_bufs_omx_mutex.Unlock();
2204 // Log::getInstance()->log("Audio", Log::DEBUG,
2205 // "Deliver MediaPacket no free sample");
2207 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark22");
2208 if (!omx_running) return *samplepos;
2209 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 23");
2212 cur_input_buf_omx = input_bufs_omx_free.front();
2213 cur_input_buf_omx->nFilledLen = 0;
2214 cur_input_buf_omx->nOffset = 0;
2215 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
2216 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2217 input_bufs_omx_free.pop_front();
2218 input_bufs_omx_mutex.Unlock();
2221 if (!cur_input_buf_omx) return *samplepos;
2226 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy in %d %d %d" ,dsize,current_context->sample_rate,cur_input_buf_omx->nFilledLen);
2229 av_opt_set_int(resam_con_libav, "in_sample_rate",decode_frame_libav->sample_rate,0);
2230 av_opt_set_int(resam_con_libav, "in_sample_fmt",decode_frame_libav->format,0);
2231 av_opt_set_int(resam_con_libav, "in_channel_layout",decode_frame_libav->channel_layout, 0);
2232 //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);
2233 //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);
2235 int ret=avresample_open(resam_con_libav);
2237 Log::getInstance()->log("Audio", Log::ERR,"Opening AV resample failed %d",ret);
2239 uint8_t *output=cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen;
2241 avresample_convert(resam_con_libav, &output,
2242 dsize, decode_frame_libav->nb_samples,
2243 decode_frame_libav->extended_data, decode_frame_libav->linesize[0], decode_frame_libav->nb_samples);
2244 avresample_close(resam_con_libav);
2247 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy out");
2248 cur_input_buf_omx->nFilledLen += dsize;
2250 //Log::getInstance()->log("Audio", Log::DEBUG,"Incomplete mpeg frames in pes packet %d %d",incoming_paket_libav.size,packet.length);
2251 /* uint8_t a1=incoming_paket_libav.data[0];
2252 uint8_t a2=incoming_paket_libav.data[1];
2253 uint8_t a3=incoming_paket_libav.data[2];
2254 uint8_t a4=incoming_paket_libav.data[3];*/
2255 // Log::getInstance()->log("Audio", Log::DEBUG,"Header %x %x %x %x",a1,a2,
2260 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 24");
2261 decompress_buffer_filled=0;
2263 //Log::getInstance()->log("Audio", Log::DEBUG,"We can not decompress %d save for later %d %x %x",haveToCopy,packet.type,incoming_paket_libav.data,packet.pos_buffer);
2264 memcpy(decompress_buffer,incoming_paket_libav.data,min(haveToCopy,decompress_buffer_size));
2266 decompress_buffer_filled=min(haveToCopy,decompress_buffer_size);
2270 if (cur_input_buf_omx->nFilledLen) {
2271 //Log::getInstance()->log("Audio", Log::DEBUG,
2272 // "P 3 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
2273 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2274 if (error != OMX_ErrorNone) {
2275 Log::getInstance()->log("Audio", Log::DEBUG,
2276 "OMX_EmptyThisBuffer 5 failed %x", error);
2278 //if (packet.synched) vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
2279 cur_input_buf_omx = NULL;
2284 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 25");
2286 *samplepos=packet.length;
2287 return packet.length;
2293 long long AudioOMX::SetStartOffset(long long curreftime, bool *rsync){
2294 VideoOMX *vw=(VideoOMX*)Video::getInstance();
2295 return vw->SetStartAudioOffset(curreftime,rsync);
2298 void AudioOMX::ResetTimeOffsets() {
2299 VideoOMX *vw=(VideoOMX*)Video::getInstance();
2300 vw->ResetTimeOffsets();