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;
82 int AudioOMX::init(UCHAR tstreamType) {
87 streamType = tstreamType;
89 if (!initAllParams()) {
96 decompress_buffer_size=20000;
97 decompress_buffer=(UCHAR*)malloc(decompress_buffer_size);
98 decompress_buffer_filled=0;
103 av_log_set_flags(AV_LOG_SKIP_REPEATED);
105 ac3codec_libav = avcodec_find_decoder(AV_CODEC_ID_AC3);
106 if (ac3codec_libav == NULL) {
107 Log::getInstance()->log("Audio", Log::DEBUG,
108 "Find libav ac3 decoder failed");
112 mp23codec_libav = avcodec_find_decoder(AV_CODEC_ID_MP3);
113 if (mp23codec_libav == NULL) {
114 Log::getInstance()->log("Audio", Log::DEBUG,
115 "Find libav mpeg audio decoder failed");
119 aaclatmcodec_libav = avcodec_find_decoder(AV_CODEC_ID_AAC_LATM);
120 if (aaclatmcodec_libav == NULL) {
121 Log::getInstance()->log("Audio", Log::DEBUG,
122 "Find libav aac latm decoder failed");
128 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eMPEG1,2,EDID_AudioSampleRate_e48KHz,0);
132 Log::getInstance()->log("Audio", Log::NOTICE,
133 "TV hdmi supports mpeg1 layer 1 and 2");
135 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eMP3,2,EDID_AudioSampleRate_e48KHz,0);
139 Log::getInstance()->log("Audio", Log::NOTICE,
140 "TV hdmi supports mpeg1 layer 3");
143 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eAC3,6,EDID_AudioSampleRate_e48KHz,0);
146 Log::getInstance()->log("Audio", Log::NOTICE,
147 "TV hdmi supports AC3");
149 ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eAAC,6,EDID_AudioSampleRate_e48KHz,0);
153 Log::getInstance()->log("Audio", Log::NOTICE,
154 "TV hdmi supports AAC");
157 canpass_pcm_mch=false;
162 int AudioOMX::initAllParams()
164 return (setStreamType(streamType) && setChannel() && setSource());
167 int AudioOMX::shutdown()
169 if (!initted) return 0;
172 Log::getInstance()->log("Audio", Log::DEBUG, "audio shutdown called");
173 DeAllocateCodecsOMX();
175 free(decompress_buffer);
176 decompress_buffer=NULL;
177 decompress_buffer_size=0;
178 decompress_buffer_filled=0;
183 bool AudioOMX::loadOptionsfromServer(VDR* vdr)
185 Log::getInstance()->log("Audio", Log::DEBUG, "AudioOMX config load");
186 char *name=vdr->configLoad("AudioOMX","AC3DecodingMode");
189 if (STRCASECMP(name, "PCM") == 0) {
191 } else if (STRCASECMP(name, "Passthrough") == 0) {
193 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
199 name = vdr->configLoad("AudioOMX", "Mp2DecodingMode");
202 if (STRCASECMP(name, "PCM") == 0) {
204 } else if (STRCASECMP(name, "Passthrough") == 0) {
206 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
212 name = vdr->configLoad("AudioOMX", "AACDecodingMode");
215 if (STRCASECMP(name, "PCM") == 0) {
217 } else if (STRCASECMP(name, "Passthrough") == 0) {
219 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
225 name = vdr->configLoad("AudioOMX", "Mp3DecodingMode");
228 if (STRCASECMP(name, "PCM") == 0) {
230 } else if (STRCASECMP(name, "Passthrough") == 0) {
232 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
238 name = vdr->configLoad("AudioOMX", "AudioOutput");
241 if (STRCASECMP(name, "analog") == 0) {
243 } else if (STRCASECMP(name, "HDMI") == 0) {
254 bool AudioOMX::handleOptionChanges(Option* option)
256 if (Audio::handleOptionChanges(option))
258 switch (option->id) {
260 if (STRCASECMP(option->options[option->userSetChoice], "analog") == 0) {
262 } else if (STRCASECMP(option->options[option->userSetChoice], "HDMI")
270 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
272 } else if (STRCASECMP(option->options[option->userSetChoice],
273 "Passthrough") == 0) {
275 } else if (STRCASECMP(option->options[option->userSetChoice],
276 "PCMMultichannel") == 0) {
282 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
284 } else if (STRCASECMP(option->options[option->userSetChoice],
285 "Passthrough") == 0) {
287 } else if (STRCASECMP(option->options[option->userSetChoice],
288 "PCMMultichannel") == 0) {
294 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
296 } else if (STRCASECMP(option->options[option->userSetChoice],
297 "Passthrough") == 0) {
299 } else if (STRCASECMP(option->options[option->userSetChoice],
300 "PCMMultichannel") == 0) {
306 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
308 } else if (STRCASECMP(option->options[option->userSetChoice],
309 "Passthrough") == 0) {
311 } else if (STRCASECMP(option->options[option->userSetChoice],
312 "PCMMultichannel") == 0) {
322 bool AudioOMX::saveOptionstoServer()
325 switch (prefered_ac3) {
327 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode", "PCM");
330 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
334 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
339 switch (prefered_aac) {
341 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode", "PCM");
344 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
348 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
353 switch (prefered_mp2) {
355 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode", "PCM");
358 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
362 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
367 switch (prefered_mp3) {
369 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode", "PCM");
372 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
376 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
382 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "analog");
384 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "HDMI");
390 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
391 UINT numChoices, UINT defaultChoice, UINT startInt,
392 const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
394 bool AudioOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
396 if (!Audio::addOptionsToPanes(panenumber,options,pane)) return false;
403 static const char* audioopts[]={"analog","HDMI"};
404 option = new Option(4,tr("Audio Output"), "AudioOMX","AudioOutput",Option::TYPE_TEXT,2,0,0,audioopts,NULL,false,this);
405 options->push_back(option);
406 pane->addOptionLine(option);
409 char **ac3opts=new char *[3];
411 ac3opts[i]=new char[strlen("PCM")+1];
412 strcpy(ac3opts[i],"PCM");
415 ac3opts[i]=new char[strlen("Passthrough")+1];
416 strcpy(ac3opts[i],"PassThrough");
419 if (canpass_pcm_mch) {
420 ac3opts[i]=new char[strlen("PCMMultichannel")+1];
421 strcpy(ac3opts[i],"PCMMultichannel");
424 option = new Option(1 ,tr("AC3 HDMI Mode"), "AudioOMX", "AC3DecodingMode", Option::TYPE_TEXT, i, 0, 0, ac3opts,NULL,true, this);
425 options->push_back(option);
426 pane->addOptionLine(option);
428 /* char **aacopts = new char *[3];
430 aacopts[i] = new char[strlen("PCM") + 1];
431 strcpy(mp2opts[i], "PCM");
434 aacopts[i] = new char[strlen("Passthrough") + 1];
435 strcpy(aacopts[i], "PassThrough");
438 if (canpass_pcm_mch) {
439 aacopts[i] = new char[strlen("PCMMultichannel") + 1];
440 strcpy(aacopts[i], "PCMMultichannel");
443 option = new Option(5, tr("Mp2 HDMI Mode"), "AudioOMX",
444 "AACDecodingMode", Option::TYPE_TEXT, i, 0, 0,
445 aacopts, NULL, true, this);
446 options->push_back(option);
447 pane->addOptionLine(option);
450 char **mp2opts = new char *[3];
452 mp2opts[i] = new char[strlen("PCM") + 1];
453 strcpy(mp2opts[i], "PCM");
456 mp2opts[i] = new char[strlen("Passthrough") + 1];
457 strcpy(mp2opts[i], "PassThrough");
460 if (canpass_pcm_mch) {
461 mp2opts[i] = new char[strlen("PCMMultichannel") + 1];
462 strcpy(mp2opts[i], "PCMMultichannel");
465 option = new Option(2, tr("Mp2 HDMI Mode"), "AudioOMX",
466 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0,
467 mp2opts, NULL, true, this);
468 options->push_back(option);
469 pane->addOptionLine(option);
471 char **mp3opts = new char *[3];
473 mp3opts[i] = new char[strlen("PCM") + 1];
474 strcpy(mp3opts[i], "PCM");
477 mp3opts[i] = new char[strlen("Passthrough") + 1];
478 strcpy(mp3opts[i], "PassThrough");
481 if (canpass_pcm_mch) {
482 mp3opts[i] = new char[strlen("PCMMultichannel") + 1];
483 strcpy(mp3opts[i], "PCMMultichannel");
486 option = new Option(3, tr("Mp3 HDMI Mode"), "AudioOMX",
487 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0, mp3opts,
489 options->push_back(option);
490 pane->addOptionLine(option);*/
491 // Comment unsupported modes out
504 OMX_ERRORTYPE AudioOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
506 //Log::getInstance()->log("Audio", Log::NOTICE, "EmptyBufferDone");
507 AudioOMX *audio=(AudioOMX *)getInstance();
508 audio->ReturnEmptyOMXBuffer(buffer);
509 return OMX_ErrorNone;
513 void AudioOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
514 input_bufs_omx_mutex.Lock();
515 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
516 input_bufs_omx_free.push_back(buffer);
517 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
518 input_bufs_omx_mutex.Unlock();
519 VideoOMX *video=(VideoOMX*)Video::getInstance();
523 OMX_ERRORTYPE AudioOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
524 Log::getInstance()->log("Audio", Log::NOTICE, "FillBufferDone");
525 VideoOMX *video=(VideoOMX*)Video::getInstance();
527 return OMX_ErrorNone;
532 int AudioOMX::setStreamType(UCHAR type)
534 if (!initted) return 0;
536 // if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
540 int AudioOMX::setChannel()
542 if (!initted) return 0;
544 // if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
548 int AudioOMX::setSource()
550 if (!initted) return 0;
552 // if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
558 if (!initted) return 0;
560 // if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
564 int AudioOMX::play() {
567 lastAType=MPTYPE_MPEG_AUDIO;
568 Log::getInstance()->log("Audio", Log::DEBUG, "enter play");
570 ((VideoOMX*)Video::getInstance())->interlaceSwitch4Demux(); // switch resolution if necessary
572 if (!AllocateCodecsOMX()) {
579 int AudioOMX::ChangeAudioDestination() //clock aka omx mutex needs to be locked
582 const char * destinations[]={"local","hdmi"};
587 OMX_CONFIG_BRCMAUDIODESTINATIONTYPE auddest;
588 memset(&auddest,0,sizeof(auddest));
589 auddest.nSize=sizeof(auddest);
590 auddest.nVersion.nVersion=OMX_VERSION;
591 strcpy((char *)auddest.sName, destinations[dest]);
593 Log::getInstance()->log("Audio", Log::DEBUG, "setting destination to: %s",auddest.sName);
594 error=OMX_SetConfig(omx_aud_rend,OMX_IndexConfigBrcmAudioDestination,&auddest);
595 if (error!=OMX_ErrorNone){
596 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX_IndexConfigBrcmAudioDestination failed %x %x %s", error,omx_aud_rend,auddest.sName);
597 DeAllocateCodecsOMX();
605 int AudioOMX::ChangeAudioPortConfig(bool disport) //clock aka omx mutex needs to be locked
608 //Ok first fidle a working configuration
609 Log::getInstance()->log("Audio", Log::DEBUG,
610 "ChangeAudioPortConfig");
612 OMX_AUDIO_CODINGTYPE encoding;
615 case MPTYPE_MPEG_AUDIO: {
616 if (prefered_mp2 == 2 && false) { //not supported yet
619 if (prefered_mp2 == 1 && canpass_mp2) {
621 encoding=OMX_AUDIO_CodingMP3;
624 encoding=OMX_AUDIO_CodingPCM;
629 case MPTYPE_AAC_LATM: {
630 if (prefered_aac == 2 && false) { //not supported yet
633 Log::getInstance()->log("Audio", Log::DEBUG,
634 "ChangeAudioPortConfig debug %d %d",prefered_aac,canpass_aac);
635 if (prefered_aac == 1 && canpass_aac) {
637 encoding=OMX_AUDIO_CodingAAC;
640 encoding=OMX_AUDIO_CodingPCM;
645 case MPTYPE_AC3_PRE13:
647 if (prefered_ac3 == 2 && false) { //not supported yet
650 Log::getInstance()->log("Audio", Log::DEBUG,
651 "ChangeAudioPortConfig debug %d %d",prefered_ac3,canpass_ac3);
652 if (prefered_ac3 == 1 && canpass_ac3) {
654 encoding=OMX_AUDIO_CodingDDP;
657 encoding=OMX_AUDIO_CodingPCM;
662 case MPTYPE_MPEG_AUDIO_LAYER3: {
663 if (prefered_mp3 == 2 && false) { //not supported yet
666 if (prefered_mp3 == 1 && canpass_mp2) {
668 encoding=OMX_AUDIO_CodingMP3;
671 encoding=OMX_AUDIO_CodingPCM;
679 encoding=OMX_AUDIO_CodingPCM;
680 //mch=false; // multichannel also false
685 /*OMX_CONFIG_BOOLEANTYPE booly;
686 memset(&booly, 0, sizeof(booly));
687 booly.nSize = sizeof(booly);
688 booly.nVersion.nVersion = OMX_VERSION;
690 booly.bEnabled = OMX_TRUE;
692 booly.bEnabled = OMX_FALSE;
694 error = OMX_SetParameter(omx_aud_dec, OMX_IndexParamBrcmDecoderPassThrough,
696 if (error != OMX_ErrorNone) {
697 Log::getInstance()->log("Audio", Log::DEBUG,
698 "Init OMX_IndexParamBrcmDecoderPassThrough failed %x", error);
699 DeAllocateCodecsOMX();
702 VideoOMX* video=(VideoOMX*)Video::getInstance();
704 video->DisablePort(omx_aud_rend,omx_rend_input_port,false);
705 //DestroyInputBufsOMXwhilePlaying();
706 //video->CommandFinished(omx_aud_rend,OMX_CommandPortDisable,omx_rend_input_port);
711 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
712 memset(&format, 0, sizeof(format));
713 format.nSize = sizeof(format);
714 format.nVersion.nVersion = OMX_VERSION;
715 format.nPortIndex = omx_rend_input_port;
716 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
718 if (error != OMX_ErrorNone) {
719 Log::getInstance()->log("Audio", Log::DEBUG,
720 "Get OMX_IndexParamAudioPortFormat failed %x %d", error,
721 omx_rend_input_port);
726 Log::getInstance()->log("Audio", Log::DEBUG,
727 "Get OMX_IndexParamAudioPortFormat returned %d",format.eEncoding );
728 format.eEncoding = encoding;
730 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
732 if (error != OMX_ErrorNone) {
733 Log::getInstance()->log("Audio", Log::DEBUG,
734 "Set OMX_IndexParamAudioPortFormat failed %x %d %d", error,
735 omx_rend_input_port,format.eEncoding );
740 case OMX_AUDIO_CodingPCM: {
741 OMX_AUDIO_PARAM_PCMMODETYPE audio_pcm;
742 memset(&audio_pcm, 0, sizeof(audio_pcm));
743 audio_pcm.nSize = sizeof(audio_pcm);
744 audio_pcm.nVersion.nVersion = OMX_VERSION;
745 audio_pcm.nChannels = 2;
746 audio_pcm.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
747 audio_pcm.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
748 //audio_pcm.eChannelMapping[2]=OMX_AUDIO_ChannelMax;
749 audio_pcm.eNumData = OMX_NumericalDataSigned;
750 audio_pcm.eEndian = OMX_EndianLittle;
751 audio_pcm.bInterleaved = OMX_TRUE;
752 audio_pcm.nBitPerSample = 16;
753 audio_pcm.ePCMMode = OMX_AUDIO_PCMModeLinear;
754 audio_pcm.nChannels = 2;
755 audio_pcm.nSamplingRate = 48000;
756 audio_pcm.nPortIndex = omx_rend_input_port;
757 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPcm,
759 if (error != OMX_ErrorNone) {
760 Log::getInstance()->log("Audio", Log::DEBUG,
761 "Init OMX_IndexParamAudioPcm failed %x %d", error,
762 omx_rend_input_port);
766 case OMX_AUDIO_CodingDDP: {
767 OMX_AUDIO_PARAM_DDPTYPE audio_ddp;
768 memset(&audio_ddp, 0, sizeof(audio_ddp));
769 audio_ddp.nSize = sizeof(audio_ddp);
770 audio_ddp.nVersion.nVersion = OMX_VERSION;
771 audio_ddp.nPortIndex = omx_rend_input_port;
772 audio_ddp.nChannels = 8; //unknown
773 audio_ddp.nBitRate=0;
774 audio_ddp.nSampleRate=48000;
775 audio_ddp.eChannelMapping[0] =OMX_AUDIO_ChannelLF;
776 audio_ddp.eChannelMapping[1] =OMX_AUDIO_ChannelRF;
777 audio_ddp.eChannelMapping[2] =OMX_AUDIO_ChannelCF;
778 audio_ddp.eChannelMapping[3] =OMX_AUDIO_ChannelLFE;
779 audio_ddp.eChannelMapping[4] =OMX_AUDIO_ChannelLR;
780 audio_ddp.eChannelMapping[5] =OMX_AUDIO_ChannelRR;
781 audio_ddp.eChannelMapping[6] =OMX_AUDIO_ChannelLS;
782 audio_ddp.eChannelMapping[7] =OMX_AUDIO_ChannelRS;
783 audio_ddp.eChannelMapping[8] =OMX_AUDIO_ChannelCS;
784 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioDdp,
786 if (error != OMX_ErrorNone) {
787 Log::getInstance()->log("Audio", Log::DEBUG,
788 "Init OMX_IndexParamAudioDdp failed %x %d", error,
789 omx_rend_input_port);
794 default: break; //Make compiler happy
801 //PrepareInputBufsOMX(false);
802 video->EnablePort(omx_aud_rend,omx_rend_input_port,false);
812 int AudioOMX::InitDecoderLibAV()
815 ac3codec_context_libav = avcodec_alloc_context3(ac3codec_libav);
816 if (!ac3codec_context_libav) {
817 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for ac3 decoding context failed!");
821 ac3codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
822 ac3codec_context_libav->request_channels=2;
824 int avc_ret = avcodec_open2(ac3codec_context_libav, ac3codec_libav, NULL);
826 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
827 libav_mutex.Unlock();
831 aaclatmcodec_context_libav = avcodec_alloc_context3(aaclatmcodec_libav);
832 if (!aaclatmcodec_context_libav) {
833 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for aac decoding context failed!");
837 aaclatmcodec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
838 aaclatmcodec_context_libav->request_channels=2;
840 avc_ret = avcodec_open2(aaclatmcodec_context_libav, aaclatmcodec_libav, NULL);
842 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
843 libav_mutex.Unlock();
848 mp23codec_context_libav = avcodec_alloc_context3(mp23codec_libav);
849 if (!ac3codec_context_libav) {
850 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for mp23 decoding context failed!");
851 libav_mutex.Unlock();
855 mp23codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
856 mp23codec_context_libav->request_channels=2;
858 avc_ret = avcodec_open2(mp23codec_context_libav, mp23codec_libav, NULL);
860 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
861 libav_mutex.Unlock();
865 resam_con_libav = avresample_alloc_context();
866 if (resam_con_libav == NULL) {
867 Log::getInstance()->log("Audio", Log::DEBUG,
868 "Alloc resample context failed");
872 av_opt_set_int(resam_con_libav, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0); // our standard format
873 av_opt_set_int(resam_con_libav, "out_sample_rate",48000,0);
874 av_opt_set_int(resam_con_libav, "out_sample_fmt",AV_SAMPLE_FMT_S16,0);
875 av_opt_set_int(resam_con_libav, "matrix_encoding",AV_MATRIX_ENCODING_DPLII,0);
877 av_opt_set_int(resam_con_libav, "in_sample_rate",48000,0);
878 av_opt_set_int(resam_con_libav, "in_sample_fmt",AV_SAMPLE_FMT_S16,0);
879 av_opt_set_int(resam_con_libav, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0); //just an example
882 av_init_packet(&incoming_paket_libav);
883 decode_frame_libav=avcodec_alloc_frame();
884 libav_mutex.Unlock();
885 decompress_buffer_filled=0;
892 void AudioOMX::DeinitDecoderLibAV() {
896 if (ac3codec_context_libav) {
897 avcodec_close(ac3codec_context_libav);
898 av_free(ac3codec_context_libav);
899 ac3codec_context_libav = NULL;
901 avcodec_close(aaclatmcodec_context_libav);
902 av_free(aaclatmcodec_context_libav);
903 aaclatmcodec_context_libav = NULL;
905 av_free(decode_frame_libav);
907 avcodec_close(mp23codec_context_libav);
908 av_free(mp23codec_context_libav);
909 mp23codec_context_libav = NULL;
911 avresample_free(&resam_con_libav);
912 resam_con_libav=NULL;
916 libav_mutex.Unlock();
921 int AudioOMX::AllocateCodecsOMX()
924 static OMX_CALLBACKTYPE callbacks= {&VideoOMX::EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
926 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX");
927 //Clock, move later to audio
928 VideoOMX *video=(VideoOMX*)Video::getInstance();
930 if (!InitDecoderLibAV()) return 0;;
933 OMX_PORT_PARAM_TYPE p_param;
934 memset(&p_param,0,sizeof(p_param));
935 p_param.nSize=sizeof(p_param);
936 p_param.nVersion.nVersion=OMX_VERSION;
939 if (!video->getClockAudioandInit(&omx_clock,&omx_clock_output_port)){
940 return 0;// get the clock and init it if necessary
944 if (!video->idleClock()) {
949 error = OMX_GetHandle(&omx_aud_rend, VPE_OMX_AUDIO_REND, NULL, &callbacks);
950 if (error != OMX_ErrorNone) {
951 Log::getInstance()->log("Audio", Log::DEBUG,
952 "Init OMX audio rend failed %x", error);
953 video->UnlockClock();
954 DeAllocateCodecsOMX();
958 if (!ChangeAudioDestination()) {
959 video->UnlockClock();
960 DeAllocateCodecsOMX();
964 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioInit, &p_param);
965 if (error != OMX_ErrorNone) {
966 Log::getInstance()->log("Audio", Log::DEBUG,
967 "Init OMX audio rend OMX_GetParameter failed %x", error);
968 video->UnlockClock();
969 DeAllocateCodecsOMX();
972 omx_rend_input_port = p_param.nStartPortNumber;
978 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamOtherInit, &p_param);
979 if (error != OMX_ErrorNone) {
980 Log::getInstance()->log("Audio", Log::DEBUG,
981 "Init OMX aud rend OMX_GetParameter failed %x", error);
982 video->UnlockClock();
983 DeAllocateCodecsOMX();
986 // buggy return value
987 omx_rend_clock_port = p_param.nStartPortNumber;
992 /* error=OMX_GetHandle(&omx_aud_dec,VPE_OMX_AUDIO_DECODER,NULL,&callbacks);
994 if (error!=OMX_ErrorNone){
995 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder failed %x", error);
996 video->UnlockClock();
997 DeAllocateCodecsOMX();
1001 error=OMX_GetParameter(omx_aud_dec,OMX_IndexParamAudioInit,&p_param);
1002 if (error!=OMX_ErrorNone){
1003 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder OMX_GetParameter failed %x", error);
1004 video->UnlockClock();
1005 DeAllocateCodecsOMX();
1008 omx_codec_input_port=p_param.nStartPortNumber;
1009 omx_codec_output_port=p_param.nStartPortNumber+1;
1011 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port) || !video->DisablePort(omx_aud_dec,omx_codec_output_port)) {
1012 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio decoder failed");
1013 video->UnlockClock();
1014 DeAllocateCodecsOMX();
1021 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true) ) {
1022 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio rend failed %d",omx_rend_input_port);
1023 video->UnlockClock();
1024 DeAllocateCodecsOMX();
1028 if ( !video->DisablePort(omx_aud_rend, omx_rend_clock_port, true)) {
1029 Log::getInstance()->log("Audio", Log::DEBUG,
1030 "Disable Ports OMX rend clock port failed %d",omx_rend_clock_port);
1031 video->UnlockClock();
1032 DeAllocateCodecsOMX();
1042 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_aud_rend,omx_rend_clock_port);
1043 if (error!=OMX_ErrorNone){
1044 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);
1045 video->UnlockClock();
1046 DeAllocateCodecsOMX();
1050 if (!video->EnablePort(omx_clock,omx_clock_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_clock_port,false)
1052 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX clock rend failed");
1053 video->UnlockClock();
1054 DeAllocateCodecsOMX();
1058 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1059 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend idle ChangeComponentState");
1060 video->UnlockClock();
1061 DeAllocateCodecsOMX();
1068 if ( !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_clock_port)) {
1069 video->UnlockClock();
1070 DeAllocateCodecsOMX();
1074 if ( !video->CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1075 video->UnlockClock();
1076 DeAllocateCodecsOMX();
1082 if (!ChangeAudioPortConfig(false)){
1083 Log::getInstance()->log("Audio", Log::NOTICE, "Change AudioPortConfig failed");
1084 video->UnlockClock();
1085 DeAllocateCodecsOMX();
1089 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1090 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1091 DeAllocateCodecsOMX();
1097 if (!PrepareInputBufsOMX(true)) {
1098 video->UnlockClock();
1099 DeAllocateCodecsOMX();
1105 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,omx_aud_rend,omx_rend_input_port);
1106 if (error!=OMX_ErrorNone){
1107 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel dec to render failed %x", error);
1108 video->UnlockClock();
1109 DeAllocateCodecsOMX();
1115 /* if (!video->EnablePort(omx_aud_dec,omx_codec_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_input_port,false)
1117 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX codec rend failed");
1118 video->UnlockClock();
1119 DeAllocateCodecsOMX();
1123 /* if ( !video->CommandFinished(omx_aud_dec,OMX_CommandPortEnable,omx_codec_output_port)
1124 || !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1125 video->UnlockClock();
1126 DeAllocateCodecsOMX();
1130 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateExecuting)) {
1131 Log::getInstance()->log("Audio", Log::DEBUG, "omx_aud_rend ChangeComponentState Execute");
1132 video->UnlockClock();
1133 DeAllocateCodecsOMX();
1138 video->UnlockClock();
1142 video->clockUnpause();
1145 if (!video->setClockExecutingandRunning()) return 0;
1147 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX finished");
1155 int AudioOMX::PrepareInputBufsOMX(bool setportdef) //needs to be called with locvke omx clock mutex
1157 VideoOMX *video=(VideoOMX*)Video::getInstance();
1158 OMX_ERRORTYPE error;
1159 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1160 memset(&port_def_type,0,sizeof(port_def_type));
1161 port_def_type.nSize=sizeof(port_def_type);
1162 port_def_type.nVersion.nVersion=OMX_VERSION;
1163 port_def_type.nPortIndex=omx_rend_input_port;//omx_codec_input_port;
1165 error=OMX_GetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1167 if (error!=OMX_ErrorNone){
1168 Log::getInstance()->log("Audio", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1173 port_def_type.nBufferCountActual=2;
1174 port_def_type.nBufferSize=max(port_def_type.nBufferSize,50000); // for transcoder important
1176 error=OMX_SetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1178 if (error!=OMX_ErrorNone){
1179 Log::getInstance()->log("Audio", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1184 error=OMX_SendCommand(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port/*codec*/,0);
1185 if (error!=OMX_ErrorNone){
1186 Log::getInstance()->log("Audio", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1190 input_bufs_omx_mutex.Lock();
1191 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1192 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1193 error=OMX_AllocateBuffer(omx_aud_rend/*dec*/,&buf_head,omx_rend_input_port/*codec*/,NULL,port_def_type.nBufferSize);
1194 if (error!=OMX_ErrorNone){
1195 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1196 input_bufs_omx_mutex.Unlock();
1199 input_bufs_omx_all.push_back(buf_head);
1200 input_bufs_omx_free.push_back(buf_head);
1202 omx_first_frame=true;
1205 cur_input_buf_omx=NULL;
1206 input_bufs_omx_mutex.Unlock();
1208 if (!video->CommandFinished(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port /*codec*/)) {
1215 int AudioOMX::DestroyInputBufsOMX() //call with clock mutex locked
1217 OMX_ERRORTYPE error;
1219 cur_input_buf_omx=NULL;
1220 input_bufs_omx_mutex.Lock();
1221 for (int i=0; i< input_bufs_omx_all.size();i++) {
1222 error=OMX_FreeBuffer(omx_aud_rend/*dec*/,omx_rend_input_port/*codec*/,input_bufs_omx_all[i]);
1223 if (error!=OMX_ErrorNone){
1224 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1225 input_bufs_omx_mutex.Unlock();
1230 input_bufs_omx_all.clear();
1231 input_bufs_omx_free.clear();
1232 input_bufs_omx_mutex.Unlock();
1236 int AudioOMX::DestroyInputBufsOMXwhilePlaying() //call with clock mutex locked
1238 OMX_ERRORTYPE error;
1240 cur_input_buf_omx=NULL;
1241 input_bufs_omx_mutex.Lock();
1242 while (input_bufs_omx_all.size()>0) {
1243 if (input_bufs_omx_free.size()>0) {
1244 // Destroy one buffer
1245 vector<OMX_BUFFERHEADERTYPE*>::iterator itty=input_bufs_omx_all.begin();
1246 OMX_BUFFERHEADERTYPE* cur_buf=input_bufs_omx_free.front();
1247 for (; itty!= input_bufs_omx_all.end();itty++) {
1248 if ((*itty)==cur_buf) {
1249 input_bufs_omx_all.erase(itty);
1250 input_bufs_omx_free.pop_front();
1255 input_bufs_omx_mutex.Unlock();
1257 input_bufs_omx_mutex.Lock();
1261 Log::getInstance()->log("Audio", Log::DEBUG, "DestroyInputBufsOMXwhilePlaying %d %d", input_bufs_omx_all.size(),input_bufs_omx_free.size());
1262 input_bufs_omx_mutex.Unlock();
1267 int AudioOMX::DeAllocateCodecsOMX()
1269 OMX_ERRORTYPE error;
1271 VideoOMX *video=(VideoOMX*)Video::getInstance();
1272 Log::getInstance()->log("Audio", Log::DEBUG, "enter deallocatecodecsomx");
1276 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 1");
1277 if (cur_input_buf_omx) {
1278 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1279 OMX_ERRORTYPE error=video->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,cur_input_buf_omx);
1280 if (error!=OMX_ErrorNone) {
1281 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_EmptyThisBuffer 6 failed %x", error);
1284 cur_input_buf_omx=NULL;//write out old data
1286 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 2");
1289 if (omx_aud_rend/*dec*/) {
1290 // first stop the omx elements
1291 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1292 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1294 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 3");
1296 video->UnlockClock();
1297 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 4");
1299 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 5");
1302 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1303 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend ChangeComponentState");
1306 // TODO proper deinit sequence
1307 // first flush all buffers
1309 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
1310 if (error!=OMX_ErrorNone) {
1311 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend in failed %x", error);
1315 /* error=OMX_SendCommand(omx_aud_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
1316 if (error!=OMX_ErrorNone){
1317 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1322 /* if (!video->CommandFinished(omx_aud_dec,OMX_CommandFlush,omx_codec_input_port)) {
1323 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd codec input failed");
1328 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1329 if (error!=OMX_ErrorNone){
1330 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1334 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_clock_port, NULL);
1335 if (error!=OMX_ErrorNone){
1336 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend clock failed %x", error);
1339 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6");
1341 if (!video->CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1342 !video->CommandFinished(omx_aud_rend,OMX_CommandFlush,omx_rend_clock_port)) {
1343 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd clock shed failed");
1346 DestroyInputBufsOMX(); //We have to make sure that no buffers are in use
1347 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6a");
1348 DeinitDecoderLibAV();
1349 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 7");
1352 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true)) {
1353 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 1");
1356 /* if (!video->DisablePort(omx_aud_dec,omx_codec_output_port,true)) {
1357 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 6");
1360 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port,true)) {
1361 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 7");
1365 if (!video->DisablePort(omx_aud_rend,omx_rend_clock_port,true)) {
1366 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 4");
1369 if (!video->DisablePort(omx_clock,omx_clock_output_port,true)) {
1370 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 5");
1375 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,NULL,NULL);
1376 if (error!=OMX_ErrorNone) {
1377 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1383 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_input_port,NULL,NULL);
1384 if (error!=OMX_ErrorNone) {
1385 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1389 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1390 if (error!=OMX_ErrorNone) {
1391 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1395 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_clock_port,NULL,NULL);
1396 if (error!=OMX_ErrorNone) {
1397 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1400 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 8");
1403 //error=OMX_FreeHandle(omx_aud_dec);
1404 error=OMX_FreeHandle(omx_aud_rend);
1405 video->UnlockClock();
1406 video->destroyClock();
1407 omx_aud_rend/*dec*/=NULL;
1408 if (error!=OMX_ErrorNone) {
1409 Log::getInstance()->log("Audio", Log::DEBUG, "FreeHandle failed %d", error);
1413 video->UnlockClock();
1414 DeinitDecoderLibAV();
1416 Log::getInstance()->log("Audio", Log::DEBUG, "leave deallocate codecs OMX");
1423 int AudioOMX::stop()
1425 if (!initted) return 0;
1427 Log::getInstance()->log("Audio", Log::DEBUG, "Audio stop called");
1428 DeAllocateCodecsOMX();
1429 //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1433 int AudioOMX::mute() {
1436 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
1437 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1441 OMX_AUDIO_CONFIG_MUTETYPE amute;
1442 memset(&amute, 0, sizeof(amute));
1443 amute.nSize = sizeof(amute);
1444 amute.nVersion.nVersion = OMX_VERSION;
1445 amute.nPortIndex = omx_rend_input_port;
1446 amute.bMute = OMX_TRUE;
1447 OMX_ERRORTYPE error= OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1449 if (error != OMX_ErrorNone) {
1450 Log::getInstance()->log("Audio", Log::DEBUG,
1451 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1452 omx_rend_input_port);
1464 int AudioOMX::unMute()
1466 if (!initted) return 0;
1468 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
1469 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1473 OMX_AUDIO_CONFIG_MUTETYPE amute;
1474 memset(&amute, 0, sizeof(amute));
1475 amute.nSize = sizeof(amute);
1476 amute.nVersion.nVersion = OMX_VERSION;
1477 amute.nPortIndex = omx_rend_input_port;
1478 amute.bMute = OMX_FALSE;
1479 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1481 if (error != OMX_ErrorNone) {
1482 Log::getInstance()->log("Audio", Log::DEBUG,
1483 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1484 omx_rend_input_port);
1496 int AudioOMX::pause() {
1500 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1506 int AudioOMX::unPause()
1508 if (!initted) return 0;
1510 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1517 int AudioOMX::reset()
1519 if (!initted) return 0;
1521 Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
1522 DeAllocateCodecsOMX();
1524 // if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1525 // Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
1526 // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
1532 int AudioOMX::setVolume(int tvolume)
1534 // parameter: 0 for silence, 20 for full
1535 if ((tvolume < 0) || (tvolume > 20)) return 0;
1537 // volume = 2 * (20 - volume);
1538 // Right, that one was rubbish... 0-10 were almost
1539 // inaudible, 11-20 did what should have been done
1540 // over the whole 0-20 range
1544 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1548 OMX_AUDIO_CONFIG_VOLUMETYPE avol;
1549 memset(&avol, 0, sizeof(avol));
1550 avol.nSize = sizeof(avol);
1551 avol.nVersion.nVersion = OMX_VERSION;
1552 avol.nPortIndex = omx_rend_input_port;
1553 avol.bLinear=OMX_FALSE;
1554 avol.sVolume.nValue =(volume-20)*200;
1555 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioVolume,
1557 if (error != OMX_ErrorNone) {
1558 Log::getInstance()->log("Audio", Log::DEBUG,
1559 "Set OMX_IndexConfigAudioVolume failed %x %d", error,
1560 omx_rend_input_port);
1572 int AudioOMX::test()
1575 // return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
1577 /* aud_sync_parms_t a;
1581 // int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
1584 /*OK*/ //printf("Audio sync disable = %i\n", b);
1592 unsigned int AudioOMX::AdvanceMpAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1594 if (size<=2) return size; // silly;
1595 unsigned int test=0;
1597 //inspired by libav parsing code
1598 while (test+1<size) {
1599 if (data[test]==0xFF && (data[test+1] &0xe0)==0xe0) {
1600 if ((data[test+1] & 0x18) == 0x08) {test++;continue;} //sanity check: mpeg version ID 01 -> reserved
1601 //sanity check inspired by libav
1604 const int sample_rates[4]={44100,48000,32000,0};
1605 const short bitrate_tab[2][3][15] = { { { 0, 32, 64, 96, 128, 160,
1606 192, 224, 256, 288, 320, 352, 384, 416, 448 }, { 0, 32, 48,
1607 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 }, {
1608 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
1609 256, 320 } }, { { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1610 160, 176, 192, 224, 256 }, { 0, 8, 16, 24, 32, 40, 48, 56,
1611 64, 80, 96, 112, 128, 144, 160 }, { 0, 8, 16, 24, 32, 40,
1612 48, 56, 64, 80, 96, 112, 128, 144, 160 } } };
1615 int layer=4-((data[test+1]&0x06)>>1);
1616 if (layer==4) {test++;continue;} //sanity check
1618 int bitrate_index=(data[test+2]&0xf0)>>4;
1619 if (bitrate_index==0x0f || bitrate_index==0x0) {test++;continue;} //sanity check
1621 int samplerate_index=(data[test+2]&0x0C)>>2;
1622 if (samplerate_index==0x03) {test++;continue;} //sanity check
1624 int padding=(data[test+2]&2)>>1;
1625 if (0x10 & data[test+1]) {
1626 lsf=((data[test+1]) &0x8)?0:1;
1632 int sample_rate=sample_rates[ samplerate_index]>>(lsf+mpeg2);
1634 int temp_frame_size=bitrate_tab[lsf][layer - 1][bitrate_index];
1636 frame_size=(temp_frame_size*12000)/sample_rate;
1637 frame_size=(frame_size+padding)*4;
1638 } else if (layer==2) {
1639 frame_size=(temp_frame_size*144000)/sample_rate;
1640 frame_size=frame_size+padding;
1642 frame_size=(temp_frame_size*144000)/(sample_rate<<lsf);
1643 frame_size=frame_size+padding;
1645 unsigned int sameheadertest=(data[test]<<24)|(data[test+1]<<16) |(data[test+2]<<8);
1646 const unsigned mask=(0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)); // from libav
1647 if (!mp3sameheader) {
1648 mp3sameheader=sameheadertest;
1649 mp3sameheadercount=2;
1651 if ((mp3sameheader& mask)== (sameheadertest &mask)) mp3sameheadercount++;
1652 else mp3sameheadercount=0;
1653 mp3sameheader=sameheadertest;
1655 //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);
1656 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME DIAG: %x %x %x %d",data[test],data[test+1],data[test+2],mp3sameheadercount);
1658 if (mp3sameheadercount>4) {
1659 *framesize=frame_size;
1660 return test; // probably FrameSync
1661 } //skip it if the header changes too frequently
1668 unsigned int AudioOMX::AdvanceAc3AudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1670 if (size<=4) return size; // silly;
1671 const int frm_size_tab[] = { 64, 64, 80, 80, 96, 96, 112, 112, 128, 128,
1672 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384, 384, 448,
1673 448, 512, 512, 640, 640, 768, 768, 896, 896, 1024, 1024, 1152,
1674 1152, 1280, 1280, };
1675 unsigned int test=0;
1676 *framesize=20000; //if we do not find a start code do not decompress
1677 while (test+4<size) {
1678 if (data[test]==0x0B && data[test+1]==0x77) {
1679 // now figure out the length of the frame
1680 unsigned char code=data[test+4];
1681 unsigned char fscod=(code& 0xC0)>>6;
1682 unsigned char frmsize=(code &0x3f);
1683 if (fscod!=0) Log::getInstance()->log("Audio", Log::DEBUG, "warning we only support 48 KHz sampling rate");
1684 *framesize=frm_size_tab[frmsize]*2;
1685 return test; // probably FrameSync
1692 unsigned int AudioOMX::AdvanceAacLatmAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1694 if (size<=4) return size; // silly;
1695 unsigned int test=0;
1696 *framesize=20000; //if we do not find a start code do not decompress
1697 while (test+4<size) {
1698 if (data[test] ==0x56 && (data[test+1]& 0xe0)==0xe0) {
1699 // now figure out the length of the frame
1700 unsigned int length= ((0x1f & data[test+1])<<8) | data[test+2];
1701 *framesize=length+3;
1702 return test; // probably FrameSync
1710 void AudioOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1712 packet = mplist.front();
1715 UINT AudioOMX::DeliverMediaSample(UCHAR* buffer, UINT* samplepos) {
1716 DeliverMediaPacket(packet, buffer, samplepos);
1717 if (*samplepos == packet.length) {
1725 long long AudioOMX::correctAudioLatency(long long pts,int addsamples,int srate) {
1727 VideoOMX *video = (VideoOMX*) Video::getInstance();
1729 OMX_PARAM_U32TYPE audio_lat;
1730 OMX_ERRORTYPE error;
1731 memset(&audio_lat, 0, sizeof(audio_lat));
1732 audio_lat.nSize = sizeof(audio_lat);
1733 audio_lat.nVersion.nVersion = OMX_VERSION;
1734 audio_lat.nPortIndex = omx_rend_input_port;
1736 error = OMX_GetConfig(omx_aud_rend, OMX_IndexConfigAudioRenderingLatency,
1738 video->UnlockClock();
1739 if (error != OMX_ErrorNone) {
1740 Log::getInstance()->log("Audio", Log::DEBUG,
1741 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
1742 omx_rend_input_port);
1743 return pts; // no correction in case of error
1745 /*Log::getInstance()->log("Audio", Log::DEBUG, "Current audio latency %d",
1748 long long workpts=0;
1749 workpts+=addsamples;
1750 workpts-=audio_lat.nU32;
1751 workpts*=10LL*1000LL*1000LL;
1752 workpts=workpts/((long long)srate); //one second /samplerate
1758 bool AudioOMX::DrainTargetBufferFull()
1760 //Check, if we have OMX output buffers
1762 input_bufs_omx_mutex.Lock();
1763 full=(input_bufs_omx_free.size()==0);
1764 input_bufs_omx_mutex.Unlock();
1770 UINT AudioOMX::DeliverMediaPacket(MediaPacket packet, const UCHAR* buffer,
1772 /*First Check, if we have an audio sample*/
1773 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1775 OMX_ERRORTYPE error;
1776 Log *logger=Log::getInstance();
1777 if (vw->InIframemode()) {
1780 return 0; //Not in iframe mode!
1783 if (!omx_running) return 0; // if we are not runnig do not do this
1784 if (vw->isClockPaused()) return 0; //Block if we pause
1785 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 1");
1787 //Log::getInstance()->log("Audio", Log::DEBUG, "DeliverMediaPacketOMX time %lld",packet.presentation_time);
1789 /* if (packet.synched && packet.presentation_time <= 0) {
1790 *samplepos = packet.length;
1791 firstsynched = false;
1793 Log::getInstance()->log("Audio", Log::DEBUG,
1794 "DeliverMediaPacketOMX Frameskip");
1795 return packet.length;
1798 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 2");
1800 UINT headerstrip = 0;
1801 if (packet.disconti) {
1802 firstsynched = false;
1803 decompress_buffer_filled=0;
1804 if (cur_input_buf_omx) {
1805 OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1807 if (error != OMX_ErrorNone) {
1808 Log::getInstance()->log("Audio", Log::DEBUG,
1809 "OMX_EmptyThisBuffer 1 failed %x", error);
1811 cur_input_buf_omx = NULL;
1815 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 3");
1816 if (packet.type != lastAType) {//Format Change //Push data out !
1817 firstsynched = false;
1820 Log::getInstance()->log("Audio", Log::DEBUG,"Notice audio type change %d %d", packet.type,lastAType);
1821 lastAType = packet.type;
1822 decompress_buffer_filled=0;
1824 if (cur_input_buf_omx) {
1825 OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1827 if (error != OMX_ErrorNone) {
1828 Log::getInstance()->log("Audio", Log::DEBUG,
1829 "OMX_EmptyThisBuffer 2 failed %x", error);
1831 cur_input_buf_omx = NULL;
1836 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
1837 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
1839 if (!ChangeAudioPortConfig(true)) {
1840 Log::getInstance()->log("Audio", Log::DEBUG,
1841 "Changing audio port config failed", error);
1844 pthread_setcancelstate(oldcancelstate, NULL);
1845 pthread_setcanceltype(oldcanceltype, NULL);
1849 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 4");
1851 /*Inspect PES-Header */
1852 if (*samplepos == 0 && packet.type != MPTYPE_MPEG_AUDIO_LAYER3) {//stripheader
1853 headerstrip = buffer[packet.pos_buffer + 8] + 9;
1854 if (packet.type == MPTYPE_AC3)
1855 headerstrip += 4; //skip ac3 bytes
1856 *samplepos += headerstrip;
1857 if (packet.synched) {
1858 if (cur_input_buf_omx) {
1859 //cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1860 OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1862 if (error != OMX_ErrorNone) {
1863 Log::getInstance()->log("Audio", Log::DEBUG,
1864 "OMX_EmptyThisBuffer 3 failed %x", error);
1866 //vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1868 cur_input_buf_omx = NULL;//write out old data
1870 firstsynched = true;
1871 //decompress_buffer_filled=0;
1873 if (!firstsynched) {//
1874 *samplepos = packet.length;//if we have not processed at least one
1875 decompress_buffer_filled=0;
1876 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 5");
1877 return packet.length;//synched packet ignore it!
1881 if (!cur_input_buf_omx) {
1882 input_bufs_omx_mutex.Lock();
1883 if (input_bufs_omx_free.size()==0) {
1884 input_bufs_omx_mutex.Unlock();
1885 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 6");
1886 //Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample");
1887 return 0; // we do not have a free media sample
1890 cur_input_buf_omx=input_bufs_omx_free.front();
1891 cur_input_buf_omx->nFilledLen=0;
1892 cur_input_buf_omx->nOffset=0;
1893 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
1894 input_bufs_omx_free.pop_front();
1895 input_bufs_omx_mutex.Unlock();
1897 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 7");
1899 if (cur_input_buf_omx->nFilledLen == 0) {//will only be changed on first packet
1900 if (packet.synched) {
1901 //Log::getInstance()->log("Audio", Log::DEBUG,
1902 // "packet synched marker");
1904 //lastreftimePTS=packet.pts;
1905 if (omx_first_frame) { // TODO time
1906 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
1907 Log::getInstance()->log("Audio", Log::DEBUG, "Starttime");
1908 omx_first_frame = false;
1910 cur_input_buf_omx->nFlags = 0;
1911 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
1913 lastreftimeOMX = packet.presentation_time;
1914 //Log::getInstance()->log("Audio", Log::DEBUG,
1915 // "Time code %lld pts %lld dts %lld", lastreftimeOMX, packet.pts,packet.dts);
1916 lastreftimePTS = packet.pts;
1917 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
1919 // Log::getInstance()->log("Audio", Log::DEBUG,
1920 // "packet NOT synched marker");
1921 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
1922 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
1925 if (packet.disconti || achange) {
1926 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_DISCONTINUITY;
1927 //mp23codec_context_libav->frame_size=-1;
1928 //ac3codec_context_libav->frame_size=-1;
1932 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 8");
1934 if (*samplepos>packet.length) *samplepos=0; //propably the thread got interrupted and sample is not valid any more!
1935 unsigned int haveToCopy=packet.length-*samplepos;
1938 while (haveToCopy>0) {
1939 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 9");
1941 unsigned int gotframesize=0;
1943 switch (packet.type) {
1944 case MPTYPE_MPEG_AUDIO:
1945 case MPTYPE_MPEG_AUDIO_LAYER3: {
1946 adv = AdvanceMpAudioSync(buffer+packet.pos_buffer+*samplepos,
1947 haveToCopy,&gotframesize);
1951 case MPTYPE_AC3_PRE13: {
1952 adv = AdvanceAc3AudioSync(buffer+packet.pos_buffer+*samplepos,
1953 haveToCopy,&gotframesize);
1957 case MPTYPE_AAC_LATM: {
1958 adv = AdvanceAacLatmAudioSync(buffer+packet.pos_buffer+*samplepos,
1959 haveToCopy,&gotframesize);
1963 if (adv!=haveToCopy) {
1968 *samplepos=packet.length; //no ac3 sync byte
1969 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 10");
1970 return packet.length;
1973 // so everything is fine now do a memcpy
1974 int cancopy=min(haveToCopy,cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen);
1975 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
1976 haveToCopy-=cancopy;
1977 cur_input_buf_omx->nFilledLen+=cancopy;
1978 *samplepos+=cancopy;
1979 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 11");
1981 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
1982 if (error != OMX_ErrorNone) {
1983 Log::getInstance()->log("Audio", Log::DEBUG,
1984 "OMX_EmptyThisBuffer 5 failed %x", error);
1986 cur_input_buf_omx=NULL;
1989 input_bufs_omx_mutex.Lock();
1990 if (input_bufs_omx_free.size()==0) {
1991 input_bufs_omx_mutex.Unlock();
1992 // Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample2");
1993 return *samplepos; // we do not have a free media sample
1995 cur_input_buf_omx=input_bufs_omx_free.front();
1996 cur_input_buf_omx->nFilledLen=0;
1997 cur_input_buf_omx->nOffset=0;
1998 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
1999 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
2000 input_bufs_omx_free.pop_front();
2001 input_bufs_omx_mutex.Unlock();
2003 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 12");
2006 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 13");
2014 AVCodecContext *current_context;
2015 switch (packet.type) {
2016 case MPTYPE_MPEG_AUDIO:
2017 case MPTYPE_MPEG_AUDIO_LAYER3: {
2018 current_context = mp23codec_context_libav;
2019 if (current_context->frame_size<0) framesize=1152; //Maximum framesize
2020 else framesize=current_context->frame_size;
2022 case MPTYPE_AAC_LATM: {
2023 current_context = aaclatmcodec_context_libav;
2026 case MPTYPE_AC3_PRE13: {
2027 current_context = ac3codec_context_libav;
2031 if (decompress_buffer_filled) { // have a remaining paket
2032 incoming_paket_libav.data =(uint8_t*) decompress_buffer;
2033 memcpy(decompress_buffer+decompress_buffer_filled,
2034 buffer+packet.pos_buffer+*samplepos,
2035 min(haveToCopy,decompress_buffer_size-decompress_buffer_filled));
2036 incoming_paket_libav.size = decompress_buffer_filled
2037 +min(haveToCopy,decompress_buffer_size-decompress_buffer_filled);
2038 //Log::getInstance()->log("Audio", Log::DEBUG,"Use saved audio buffer %d %d %d",packet.type,decompress_buffer_filled,packet.synched);
2040 incoming_paket_libav.data =(uint8_t*) buffer+packet.pos_buffer+*samplepos;
2041 incoming_paket_libav.size = haveToCopy;
2043 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 18");
2045 while (haveToCopy> 0 && errcount<3) {
2047 //Log::getInstance()->log("Audio", Log::DEBUG,"libav in %d %d",framesize,current_context->frame_size);
2048 //Log::getInstance()->log("Audio", Log::DEBUG, "libav in %d %d",
2049 // framesize, current_context->frame_size);
2051 bool donotdecompress=false;
2052 unsigned int gotframesize=0;
2053 // if (!decompress_buffer_filled) { // only do this if no old data is present
2055 switch (packet.type) {
2056 case MPTYPE_MPEG_AUDIO:
2057 case MPTYPE_MPEG_AUDIO_LAYER3: {
2058 adv = AdvanceMpAudioSync(incoming_paket_libav.data,
2059 incoming_paket_libav.size,&gotframesize);
2063 case MPTYPE_AC3_PRE13: {
2064 adv = AdvanceAc3AudioSync(incoming_paket_libav.data,
2065 incoming_paket_libav.size,&gotframesize);
2068 case MPTYPE_AAC_LATM: {
2069 adv = AdvanceAacLatmAudioSync(incoming_paket_libav.data,
2070 incoming_paket_libav.size,&gotframesize);
2074 if (adv > 0 /*&& !decompress_buffer_filled*/) {
2075 incoming_paket_libav.data += adv;
2076 incoming_paket_libav.size-=adv;
2079 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2080 else*/ framesize=haveToCopy;
2081 //Log::getInstance()->log("Audio", Log::DEBUG,"Advance by %d %d from %d",adv,packet.type,*samplepos );
2082 if (haveToCopy <= 0) {
2083 // Log::getInstance()->log("Audio", Log::DEBUG,"No sync code in packet remove %d",packet.type);
2084 *samplepos=packet.length;
2085 return packet.length;
2091 if (gotframesize>0 && gotframesize>haveToCopy) {
2092 donotdecompress=true;
2093 errcount=100; // exit loop
2095 // else Log::getInstance()->log("Audio", Log::DEBUG,"Loop run" );
2097 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 19");
2098 if (!donotdecompress) {
2101 pthread_testcancel();
2102 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2103 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2105 if (!omx_running || !mp23codec_context_libav
2106 || !ac3codec_context_libav) {
2107 libav_mutex.Unlock();
2110 libav_mutex.Unlock();
2111 // Log::getInstance()->log("Audio", Log::DEBUG,"libav out");
2112 int save_size=incoming_paket_libav.size;
2114 if (gotframesize<=incoming_paket_libav.size) {
2115 if (gotframesize>0) incoming_paket_libav.size=gotframesize;
2116 len = avcodec_decode_audio4(current_context, decode_frame_libav,
2117 &gotta, &incoming_paket_libav);
2119 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:E %d %d",gotframesize,incoming_paket_libav.size);
2123 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:T %d",len);
2124 incoming_paket_libav.size=save_size;
2125 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out1");
2126 pthread_setcancelstate(oldcancelstate, NULL);
2127 pthread_setcanceltype(oldcanceltype, NULL);
2128 pthread_testcancel();
2134 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out2");
2135 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 20");
2137 libav_mutex.Unlock();
2141 if (decompress_buffer_filled) { // reset to normal decoding
2143 //Log::getInstance()->log("Audio", Log::DEBUG,"saved audio: %d",len);
2144 haveToCopy -= min(len-decompress_buffer_filled,0);
2145 *samplepos += min(len-decompress_buffer_filled,0);
2146 //if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2147 /*else*/ framesize=haveToCopy;
2149 framesize=haveToCopy;
2151 incoming_paket_libav.data =(uint8_t*) buffer+packet.pos_buffer+*samplepos;
2153 decompress_buffer_filled=0;
2157 incoming_paket_libav.data += len;
2161 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2162 else*/framesize=haveToCopy;
2165 framesize=haveToCopy;
2168 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 21");
2170 incoming_paket_libav.size =framesize;
2172 //Log::getInstance()->log("Audio", Log::DEBUG,
2175 int dsize = av_samples_get_buffer_size(NULL,
2176 /*current_context->channels*/2, decode_frame_libav->nb_samples,
2177 AV_SAMPLE_FMT_S16, 1);
2178 int dsize_in = av_samples_get_buffer_size(NULL,
2179 current_context->channels, decode_frame_libav->nb_samples,
2180 current_context->sample_fmt, 1);
2181 //if (current_context->channels==1) dsize*=2; // we convert mono to stereo
2182 if ((cur_input_buf_omx->nFilledLen + dsize)
2183 > cur_input_buf_omx->nAllocLen ) {
2184 // I doubt that this will ever happen
2185 // Log::getInstance()->log("Audio", Log::DEBUG,
2186 // "P 2 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
2187 OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
2189 if (error != OMX_ErrorNone) {
2190 Log::getInstance()->log("Audio", Log::DEBUG,
2191 "OMX_EmptyThisBuffer 4 failed %x", error);
2193 cur_input_buf_omx = NULL;
2195 if (!cur_input_buf_omx) {
2197 while (count < 10 && omx_running) {
2199 input_bufs_omx_mutex.Lock();
2200 if (input_bufs_omx_free.size() == 0) {
2201 input_bufs_omx_mutex.Unlock();
2202 // Log::getInstance()->log("Audio", Log::DEBUG,
2203 // "Deliver MediaPacket no free sample");
2205 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark22");
2206 if (!omx_running) return *samplepos;
2207 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 23");
2210 cur_input_buf_omx = input_bufs_omx_free.front();
2211 cur_input_buf_omx->nFilledLen = 0;
2212 cur_input_buf_omx->nOffset = 0;
2213 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
2214 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2215 input_bufs_omx_free.pop_front();
2216 input_bufs_omx_mutex.Unlock();
2219 if (!cur_input_buf_omx) return *samplepos;
2224 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy in %d %d %d" ,dsize,current_context->sample_rate,cur_input_buf_omx->nFilledLen);
2227 av_opt_set_int(resam_con_libav, "in_sample_rate",decode_frame_libav->sample_rate,0);
2228 av_opt_set_int(resam_con_libav, "in_sample_fmt",decode_frame_libav->format,0);
2229 av_opt_set_int(resam_con_libav, "in_channel_layout",decode_frame_libav->channel_layout, 0);
2230 //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);
2231 //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);
2233 int ret=avresample_open(resam_con_libav);
2235 Log::getInstance()->log("Audio", Log::ERR,"Opening AV resample failed %d",ret);
2237 uint8_t *output=cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen;
2239 avresample_convert(resam_con_libav, &output,
2240 dsize, decode_frame_libav->nb_samples,
2241 decode_frame_libav->extended_data, decode_frame_libav->linesize[0], decode_frame_libav->nb_samples);
2242 avresample_close(resam_con_libav);
2245 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy out");
2246 cur_input_buf_omx->nFilledLen += dsize;
2248 //Log::getInstance()->log("Audio", Log::DEBUG,"Incomplete mpeg frames in pes packet %d %d",incoming_paket_libav.size,packet.length);
2249 /* uint8_t a1=incoming_paket_libav.data[0];
2250 uint8_t a2=incoming_paket_libav.data[1];
2251 uint8_t a3=incoming_paket_libav.data[2];
2252 uint8_t a4=incoming_paket_libav.data[3];*/
2253 // Log::getInstance()->log("Audio", Log::DEBUG,"Header %x %x %x %x",a1,a2,
2258 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 24");
2259 decompress_buffer_filled=0;
2261 //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);
2262 memcpy(decompress_buffer,incoming_paket_libav.data,min(haveToCopy,decompress_buffer_size));
2264 decompress_buffer_filled=min(haveToCopy,decompress_buffer_size);
2268 if (cur_input_buf_omx->nFilledLen) {
2269 //Log::getInstance()->log("Audio", Log::DEBUG,
2270 // "P 3 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
2271 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2272 if (error != OMX_ErrorNone) {
2273 Log::getInstance()->log("Audio", Log::DEBUG,
2274 "OMX_EmptyThisBuffer 5 failed %x", error);
2276 //if (packet.synched) vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
2277 cur_input_buf_omx = NULL;
2282 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 25");
2284 *samplepos=packet.length;
2285 return packet.length;
2291 long long AudioOMX::SetStartOffset(long long curreftime, bool *rsync){
2292 VideoOMX *vw=(VideoOMX*)Video::getInstance();
2293 return vw->SetStartAudioOffset(curreftime,rsync);
2296 void AudioOMX::ResetTimeOffsets() {
2297 VideoOMX *vw=(VideoOMX*)Video::getInstance();
2298 vw->ResetTimeOffsets();