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(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(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(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) {
198 name = vdr->configLoad("AudioOMX", "Mp2DecodingMode");
201 if (STRCASECMP(name, "PCM") == 0) {
203 } else if (STRCASECMP(name, "Passthrough") == 0) {
205 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
210 name = vdr->configLoad("AudioOMX", "AACDecodingMode");
213 if (STRCASECMP(name, "PCM") == 0) {
215 } else if (STRCASECMP(name, "Passthrough") == 0) {
217 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
222 name = vdr->configLoad("AudioOMX", "Mp3DecodingMode");
225 if (STRCASECMP(name, "PCM") == 0) {
227 } else if (STRCASECMP(name, "Passthrough") == 0) {
229 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
234 name = vdr->configLoad("AudioOMX", "AudioOutput");
237 if (STRCASECMP(name, "analog") == 0) {
239 } else if (STRCASECMP(name, "HDMI") == 0) {
249 bool AudioOMX::handleOptionChanges(Option* option)
251 if (Audio::handleOptionChanges(option))
253 switch (option->id) {
255 if (STRCASECMP(option->options[option->userSetChoice], "analog") == 0) {
257 } else if (STRCASECMP(option->options[option->userSetChoice], "HDMI")
265 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
267 } else if (STRCASECMP(option->options[option->userSetChoice],
268 "Passthrough") == 0) {
270 } else if (STRCASECMP(option->options[option->userSetChoice],
271 "PCMMultichannel") == 0) {
277 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
279 } else if (STRCASECMP(option->options[option->userSetChoice],
280 "Passthrough") == 0) {
282 } else if (STRCASECMP(option->options[option->userSetChoice],
283 "PCMMultichannel") == 0) {
289 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
291 } else if (STRCASECMP(option->options[option->userSetChoice],
292 "Passthrough") == 0) {
294 } else if (STRCASECMP(option->options[option->userSetChoice],
295 "PCMMultichannel") == 0) {
301 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
303 } else if (STRCASECMP(option->options[option->userSetChoice],
304 "Passthrough") == 0) {
306 } else if (STRCASECMP(option->options[option->userSetChoice],
307 "PCMMultichannel") == 0) {
317 bool AudioOMX::saveOptionstoServer()
320 switch (prefered_ac3) {
322 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode", "PCM");
325 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
329 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
334 switch (prefered_aac) {
336 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode", "PCM");
339 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
343 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
348 switch (prefered_mp2) {
350 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode", "PCM");
353 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
357 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
362 switch (prefered_mp3) {
364 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode", "PCM");
367 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
371 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
377 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "analog");
379 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "HDMI");
385 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
386 UINT numChoices, UINT defaultChoice, UINT startInt,
387 const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
389 bool AudioOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
391 if (!Audio::addOptionsToPanes(panenumber,options,pane)) return false;
398 static const char* audioopts[]={"analog","HDMI"};
399 option = new Option(4,tr("Audio Output"), "AudioOMX","AudioOutput",Option::TYPE_TEXT,2,0,0,audioopts,NULL,false,this);
400 options->push_back(option);
401 pane->addOptionLine(option);
404 char **ac3opts=new char *[3];
406 ac3opts[i]=new char[strlen("PCM")+1];
407 strcpy(ac3opts[i],"PCM");
410 ac3opts[i]=new char[strlen("Passthrough")+1];
411 strcpy(ac3opts[i],"PassThrough");
414 if (canpass_pcm_mch) {
415 ac3opts[i]=new char[strlen("PCMMultichannel")+1];
416 strcpy(ac3opts[i],"PCMMultichannel");
419 option = new Option(1 ,tr("AC3 HDMI Mode"), "AudioOMX", "AC3DecodingMode", Option::TYPE_TEXT, i, 0, 0, ac3opts,NULL,true, this);
420 options->push_back(option);
421 pane->addOptionLine(option);
423 /* char **aacopts = new char *[3];
425 aacopts[i] = new char[strlen("PCM") + 1];
426 strcpy(mp2opts[i], "PCM");
429 aacopts[i] = new char[strlen("Passthrough") + 1];
430 strcpy(aacopts[i], "PassThrough");
433 if (canpass_pcm_mch) {
434 aacopts[i] = new char[strlen("PCMMultichannel") + 1];
435 strcpy(aacopts[i], "PCMMultichannel");
438 option = new Option(5, tr("Mp2 HDMI Mode"), "AudioOMX",
439 "AACDecodingMode", Option::TYPE_TEXT, i, 0, 0,
440 aacopts, NULL, true, this);
441 options->push_back(option);
442 pane->addOptionLine(option);
445 char **mp2opts = new char *[3];
447 mp2opts[i] = new char[strlen("PCM") + 1];
448 strcpy(mp2opts[i], "PCM");
451 mp2opts[i] = new char[strlen("Passthrough") + 1];
452 strcpy(mp2opts[i], "PassThrough");
455 if (canpass_pcm_mch) {
456 mp2opts[i] = new char[strlen("PCMMultichannel") + 1];
457 strcpy(mp2opts[i], "PCMMultichannel");
460 option = new Option(2, tr("Mp2 HDMI Mode"), "AudioOMX",
461 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0,
462 mp2opts, NULL, true, this);
463 options->push_back(option);
464 pane->addOptionLine(option);
466 char **mp3opts = new char *[3];
468 mp3opts[i] = new char[strlen("PCM") + 1];
469 strcpy(mp3opts[i], "PCM");
472 mp3opts[i] = new char[strlen("Passthrough") + 1];
473 strcpy(mp3opts[i], "PassThrough");
476 if (canpass_pcm_mch) {
477 mp3opts[i] = new char[strlen("PCMMultichannel") + 1];
478 strcpy(mp3opts[i], "PCMMultichannel");
481 option = new Option(3, tr("Mp3 HDMI Mode"), "AudioOMX",
482 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0, mp3opts,
484 options->push_back(option);
485 pane->addOptionLine(option);*/
486 // Comment unsupported modes out
499 OMX_ERRORTYPE AudioOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
501 //Log::getInstance()->log("Audio", Log::NOTICE, "EmptyBufferDone");
502 AudioOMX *audio=(AudioOMX *)getInstance();
503 audio->ReturnEmptyOMXBuffer(buffer);
504 return OMX_ErrorNone;
508 void AudioOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
509 input_bufs_omx_mutex.Lock();
510 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
511 input_bufs_omx_free.push_back(buffer);
512 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
513 input_bufs_omx_mutex.Unlock();
514 VideoOMX *video=(VideoOMX*)Video::getInstance();
518 OMX_ERRORTYPE AudioOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
519 Log::getInstance()->log("Audio", Log::NOTICE, "FillBufferDone");
520 VideoOMX *video=(VideoOMX*)Video::getInstance();
522 return OMX_ErrorNone;
527 int AudioOMX::setStreamType(UCHAR type)
529 if (!initted) return 0;
531 // if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
535 int AudioOMX::setChannel()
537 if (!initted) return 0;
539 // if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
543 int AudioOMX::setSource()
545 if (!initted) return 0;
547 // if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
553 if (!initted) return 0;
555 // if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
559 int AudioOMX::play() {
562 lastAType=MPTYPE_MPEG_AUDIO;
563 Log::getInstance()->log("Audio", Log::DEBUG, "enter play");
565 ((VideoOMX*)Video::getInstance())->interlaceSwitch4Demux(); // switch resolution if necessary
567 if (!AllocateCodecsOMX()) {
574 int AudioOMX::ChangeAudioDestination() //clock aka omx mutex needs to be locked
577 const char * destinations[]={"local","hdmi"};
582 OMX_CONFIG_BRCMAUDIODESTINATIONTYPE auddest;
583 memset(&auddest,0,sizeof(auddest));
584 auddest.nSize=sizeof(auddest);
585 auddest.nVersion.nVersion=OMX_VERSION;
586 strcpy((char *)auddest.sName, destinations[dest]);
588 Log::getInstance()->log("Audio", Log::DEBUG, "setting destination to: %s",auddest.sName);
589 error=OMX_SetConfig(omx_aud_rend,OMX_IndexConfigBrcmAudioDestination,&auddest);
590 if (error!=OMX_ErrorNone){
591 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX_IndexConfigBrcmAudioDestination failed %x %x %s", error,omx_aud_rend,auddest.sName);
592 DeAllocateCodecsOMX();
600 int AudioOMX::ChangeAudioPortConfig(bool disport) //clock aka omx mutex needs to be locked
603 //Ok first fidle a working configuration
604 Log::getInstance()->log("Audio", Log::DEBUG,
605 "ChangeAudioPortConfig");
607 OMX_AUDIO_CODINGTYPE encoding;
610 case MPTYPE_MPEG_AUDIO: {
611 if (prefered_mp2 == 2 && false) { //not supported yet
614 if (prefered_mp2 == 1 && canpass_mp2) {
616 encoding=OMX_AUDIO_CodingMP3;
619 encoding=OMX_AUDIO_CodingPCM;
624 case MPTYPE_AAC_LATM: {
625 if (prefered_aac == 2 && false) { //not supported yet
628 Log::getInstance()->log("Audio", Log::DEBUG,
629 "ChangeAudioPortConfig debug %d %d",prefered_aac,canpass_aac);
630 if (prefered_aac == 1 && canpass_aac) {
632 encoding=OMX_AUDIO_CodingAAC;
635 encoding=OMX_AUDIO_CodingPCM;
640 case MPTYPE_AC3_PRE13:
642 if (prefered_ac3 == 2 && false) { //not supported yet
645 Log::getInstance()->log("Audio", Log::DEBUG,
646 "ChangeAudioPortConfig debug %d %d",prefered_ac3,canpass_ac3);
647 if (prefered_ac3 == 1 && canpass_ac3) {
649 encoding=OMX_AUDIO_CodingDDP;
652 encoding=OMX_AUDIO_CodingPCM;
657 case MPTYPE_MPEG_AUDIO_LAYER3: {
658 if (prefered_mp3 == 2 && false) { //not supported yet
661 if (prefered_mp3 == 1 && canpass_mp2) {
663 encoding=OMX_AUDIO_CodingMP3;
666 encoding=OMX_AUDIO_CodingPCM;
674 encoding=OMX_AUDIO_CodingPCM;
675 //mch=false; // multichannel also false
680 /*OMX_CONFIG_BOOLEANTYPE booly;
681 memset(&booly, 0, sizeof(booly));
682 booly.nSize = sizeof(booly);
683 booly.nVersion.nVersion = OMX_VERSION;
685 booly.bEnabled = OMX_TRUE;
687 booly.bEnabled = OMX_FALSE;
689 error = OMX_SetParameter(omx_aud_dec, OMX_IndexParamBrcmDecoderPassThrough,
691 if (error != OMX_ErrorNone) {
692 Log::getInstance()->log("Audio", Log::DEBUG,
693 "Init OMX_IndexParamBrcmDecoderPassThrough failed %x", error);
694 DeAllocateCodecsOMX();
697 VideoOMX* video=(VideoOMX*)Video::getInstance();
699 video->DisablePort(omx_aud_rend,omx_rend_input_port,false);
700 //DestroyInputBufsOMXwhilePlaying();
701 //video->CommandFinished(omx_aud_rend,OMX_CommandPortDisable,omx_rend_input_port);
706 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
707 memset(&format, 0, sizeof(format));
708 format.nSize = sizeof(format);
709 format.nVersion.nVersion = OMX_VERSION;
710 format.nPortIndex = omx_rend_input_port;
711 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
713 if (error != OMX_ErrorNone) {
714 Log::getInstance()->log("Audio", Log::DEBUG,
715 "Get OMX_IndexParamAudioPortFormat failed %x %d", error,
716 omx_rend_input_port);
721 Log::getInstance()->log("Audio", Log::DEBUG,
722 "Get OMX_IndexParamAudioPortFormat returned %d",format.eEncoding );
723 format.eEncoding = encoding;
725 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
727 if (error != OMX_ErrorNone) {
728 Log::getInstance()->log("Audio", Log::DEBUG,
729 "Set OMX_IndexParamAudioPortFormat failed %x %d %d", error,
730 omx_rend_input_port,format.eEncoding );
735 case OMX_AUDIO_CodingPCM: {
736 OMX_AUDIO_PARAM_PCMMODETYPE audio_pcm;
737 memset(&audio_pcm, 0, sizeof(audio_pcm));
738 audio_pcm.nSize = sizeof(audio_pcm);
739 audio_pcm.nVersion.nVersion = OMX_VERSION;
740 audio_pcm.nChannels = 2;
741 audio_pcm.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
742 audio_pcm.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
743 //audio_pcm.eChannelMapping[2]=OMX_AUDIO_ChannelMax;
744 audio_pcm.eNumData = OMX_NumericalDataSigned;
745 audio_pcm.eEndian = OMX_EndianLittle;
746 audio_pcm.bInterleaved = OMX_TRUE;
747 audio_pcm.nBitPerSample = 16;
748 audio_pcm.ePCMMode = OMX_AUDIO_PCMModeLinear;
749 audio_pcm.nChannels = 2;
750 audio_pcm.nSamplingRate = 48000;
751 audio_pcm.nPortIndex = omx_rend_input_port;
752 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPcm,
754 if (error != OMX_ErrorNone) {
755 Log::getInstance()->log("Audio", Log::DEBUG,
756 "Init OMX_IndexParamAudioPcm failed %x %d", error,
757 omx_rend_input_port);
761 case OMX_AUDIO_CodingDDP: {
762 OMX_AUDIO_PARAM_DDPTYPE audio_ddp;
763 memset(&audio_ddp, 0, sizeof(audio_ddp));
764 audio_ddp.nSize = sizeof(audio_ddp);
765 audio_ddp.nVersion.nVersion = OMX_VERSION;
766 audio_ddp.nPortIndex = omx_rend_input_port;
767 audio_ddp.nChannels = 8; //unknown
768 audio_ddp.nBitRate=0;
769 audio_ddp.nSampleRate=48000;
770 audio_ddp.eChannelMapping[0] =OMX_AUDIO_ChannelLF;
771 audio_ddp.eChannelMapping[1] =OMX_AUDIO_ChannelRF;
772 audio_ddp.eChannelMapping[2] =OMX_AUDIO_ChannelCF;
773 audio_ddp.eChannelMapping[3] =OMX_AUDIO_ChannelLFE;
774 audio_ddp.eChannelMapping[4] =OMX_AUDIO_ChannelLR;
775 audio_ddp.eChannelMapping[5] =OMX_AUDIO_ChannelRR;
776 audio_ddp.eChannelMapping[6] =OMX_AUDIO_ChannelLS;
777 audio_ddp.eChannelMapping[7] =OMX_AUDIO_ChannelRS;
778 audio_ddp.eChannelMapping[8] =OMX_AUDIO_ChannelCS;
779 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioDdp,
781 if (error != OMX_ErrorNone) {
782 Log::getInstance()->log("Audio", Log::DEBUG,
783 "Init OMX_IndexParamAudioDdp failed %x %d", error,
784 omx_rend_input_port);
789 default: break; //Make compiler happy
796 //PrepareInputBufsOMX(false);
797 video->EnablePort(omx_aud_rend,omx_rend_input_port,false);
807 int AudioOMX::InitDecoderLibAV()
810 ac3codec_context_libav = avcodec_alloc_context3(ac3codec_libav);
811 if (!ac3codec_context_libav) {
812 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for ac3 decoding context failed!");
816 ac3codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
817 ac3codec_context_libav->request_channels=2;
819 int avc_ret = avcodec_open2(ac3codec_context_libav, ac3codec_libav, NULL);
821 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
822 libav_mutex.Unlock();
826 aaclatmcodec_context_libav = avcodec_alloc_context3(aaclatmcodec_libav);
827 if (!aaclatmcodec_context_libav) {
828 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for aac decoding context failed!");
832 aaclatmcodec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
833 aaclatmcodec_context_libav->request_channels=2;
835 avc_ret = avcodec_open2(aaclatmcodec_context_libav, aaclatmcodec_libav, NULL);
837 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
838 libav_mutex.Unlock();
843 mp23codec_context_libav = avcodec_alloc_context3(mp23codec_libav);
844 if (!ac3codec_context_libav) {
845 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for mp23 decoding context failed!");
846 libav_mutex.Unlock();
850 mp23codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
851 mp23codec_context_libav->request_channels=2;
853 avc_ret = avcodec_open2(mp23codec_context_libav, mp23codec_libav, NULL);
855 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
856 libav_mutex.Unlock();
860 resam_con_libav = avresample_alloc_context();
861 if (resam_con_libav == NULL) {
862 Log::getInstance()->log("Audio", Log::DEBUG,
863 "Alloc resample context failed");
867 av_opt_set_int(resam_con_libav, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0); // our standard format
868 av_opt_set_int(resam_con_libav, "out_sample_rate",48000,0);
869 av_opt_set_int(resam_con_libav, "out_sample_fmt",AV_SAMPLE_FMT_S16,0);
870 av_opt_set_int(resam_con_libav, "matrix_encoding",AV_MATRIX_ENCODING_DPLII,0);
872 av_opt_set_int(resam_con_libav, "in_sample_rate",48000,0);
873 av_opt_set_int(resam_con_libav, "in_sample_fmt",AV_SAMPLE_FMT_S16,0);
874 av_opt_set_int(resam_con_libav, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0); //just an example
877 av_init_packet(&incoming_paket_libav);
878 decode_frame_libav=avcodec_alloc_frame();
879 libav_mutex.Unlock();
880 decompress_buffer_filled=0;
887 void AudioOMX::DeinitDecoderLibAV() {
891 if (ac3codec_context_libav) {
892 avcodec_close(ac3codec_context_libav);
893 av_free(ac3codec_context_libav);
894 ac3codec_context_libav = NULL;
896 avcodec_close(aaclatmcodec_context_libav);
897 av_free(aaclatmcodec_context_libav);
898 aaclatmcodec_context_libav = NULL;
900 av_free(decode_frame_libav);
902 avcodec_close(mp23codec_context_libav);
903 av_free(mp23codec_context_libav);
904 mp23codec_context_libav = NULL;
906 avresample_free(&resam_con_libav);
907 resam_con_libav=NULL;
911 libav_mutex.Unlock();
916 int AudioOMX::AllocateCodecsOMX()
919 static OMX_CALLBACKTYPE callbacks= {&VideoOMX::EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
921 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX");
922 //Clock, move later to audio
923 VideoOMX *video=(VideoOMX*)Video::getInstance();
925 if (!InitDecoderLibAV()) return 0;;
928 OMX_PORT_PARAM_TYPE p_param;
929 memset(&p_param,0,sizeof(p_param));
930 p_param.nSize=sizeof(p_param);
931 p_param.nVersion.nVersion=OMX_VERSION;
934 if (!video->getClockAudioandInit(&omx_clock,&omx_clock_output_port)){
935 return 0;// get the clock and init it if necessary
939 if (!video->idleClock()) {
944 error = OMX_GetHandle(&omx_aud_rend, VPE_OMX_AUDIO_REND, NULL, &callbacks);
945 if (error != OMX_ErrorNone) {
946 Log::getInstance()->log("Audio", Log::DEBUG,
947 "Init OMX audio rend failed %x", error);
948 video->UnlockClock();
949 DeAllocateCodecsOMX();
953 if (!ChangeAudioDestination()) {
954 video->UnlockClock();
955 DeAllocateCodecsOMX();
959 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioInit, &p_param);
960 if (error != OMX_ErrorNone) {
961 Log::getInstance()->log("Audio", Log::DEBUG,
962 "Init OMX audio rend OMX_GetParameter failed %x", error);
963 video->UnlockClock();
964 DeAllocateCodecsOMX();
967 omx_rend_input_port = p_param.nStartPortNumber;
973 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamOtherInit, &p_param);
974 if (error != OMX_ErrorNone) {
975 Log::getInstance()->log("Audio", Log::DEBUG,
976 "Init OMX aud rend OMX_GetParameter failed %x", error);
977 video->UnlockClock();
978 DeAllocateCodecsOMX();
981 // buggy return value
982 omx_rend_clock_port = p_param.nStartPortNumber;
987 /* error=OMX_GetHandle(&omx_aud_dec,VPE_OMX_AUDIO_DECODER,NULL,&callbacks);
989 if (error!=OMX_ErrorNone){
990 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder failed %x", error);
991 video->UnlockClock();
992 DeAllocateCodecsOMX();
996 error=OMX_GetParameter(omx_aud_dec,OMX_IndexParamAudioInit,&p_param);
997 if (error!=OMX_ErrorNone){
998 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder OMX_GetParameter failed %x", error);
999 video->UnlockClock();
1000 DeAllocateCodecsOMX();
1003 omx_codec_input_port=p_param.nStartPortNumber;
1004 omx_codec_output_port=p_param.nStartPortNumber+1;
1006 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port) || !video->DisablePort(omx_aud_dec,omx_codec_output_port)) {
1007 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio decoder failed");
1008 video->UnlockClock();
1009 DeAllocateCodecsOMX();
1016 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true) ) {
1017 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio rend failed %d",omx_rend_input_port);
1018 video->UnlockClock();
1019 DeAllocateCodecsOMX();
1023 if ( !video->DisablePort(omx_aud_rend, omx_rend_clock_port, true)) {
1024 Log::getInstance()->log("Audio", Log::DEBUG,
1025 "Disable Ports OMX rend clock port failed %d",omx_rend_clock_port);
1026 video->UnlockClock();
1027 DeAllocateCodecsOMX();
1037 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_aud_rend,omx_rend_clock_port);
1038 if (error!=OMX_ErrorNone){
1039 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);
1040 video->UnlockClock();
1041 DeAllocateCodecsOMX();
1045 if (!video->EnablePort(omx_clock,omx_clock_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_clock_port,false)
1047 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX clock rend failed");
1048 video->UnlockClock();
1049 DeAllocateCodecsOMX();
1053 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1054 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend idle ChangeComponentState");
1055 video->UnlockClock();
1056 DeAllocateCodecsOMX();
1063 if ( !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_clock_port)) {
1064 video->UnlockClock();
1065 DeAllocateCodecsOMX();
1069 if ( !video->CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1070 video->UnlockClock();
1071 DeAllocateCodecsOMX();
1077 if (!ChangeAudioPortConfig(false)){
1078 Log::getInstance()->log("Audio", Log::NOTICE, "Change AudioPortConfig failed");
1079 video->UnlockClock();
1080 DeAllocateCodecsOMX();
1084 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1085 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1086 DeAllocateCodecsOMX();
1092 if (!PrepareInputBufsOMX(true)) {
1093 video->UnlockClock();
1094 DeAllocateCodecsOMX();
1100 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,omx_aud_rend,omx_rend_input_port);
1101 if (error!=OMX_ErrorNone){
1102 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel dec to render failed %x", error);
1103 video->UnlockClock();
1104 DeAllocateCodecsOMX();
1110 /* if (!video->EnablePort(omx_aud_dec,omx_codec_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_input_port,false)
1112 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX codec rend failed");
1113 video->UnlockClock();
1114 DeAllocateCodecsOMX();
1118 /* if ( !video->CommandFinished(omx_aud_dec,OMX_CommandPortEnable,omx_codec_output_port)
1119 || !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1120 video->UnlockClock();
1121 DeAllocateCodecsOMX();
1125 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateExecuting)) {
1126 Log::getInstance()->log("Audio", Log::DEBUG, "omx_aud_rend ChangeComponentState Execute");
1127 video->UnlockClock();
1128 DeAllocateCodecsOMX();
1133 video->UnlockClock();
1137 video->clockUnpause();
1140 if (!video->setClockExecutingandRunning()) return 0;
1142 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX finished");
1150 int AudioOMX::PrepareInputBufsOMX(bool setportdef) //needs to be called with locvke omx clock mutex
1152 VideoOMX *video=(VideoOMX*)Video::getInstance();
1153 OMX_ERRORTYPE error;
1154 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1155 memset(&port_def_type,0,sizeof(port_def_type));
1156 port_def_type.nSize=sizeof(port_def_type);
1157 port_def_type.nVersion.nVersion=OMX_VERSION;
1158 port_def_type.nPortIndex=omx_rend_input_port;//omx_codec_input_port;
1160 error=OMX_GetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1162 if (error!=OMX_ErrorNone){
1163 Log::getInstance()->log("Audio", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1168 port_def_type.nBufferCountActual=2;
1169 port_def_type.nBufferSize=max(port_def_type.nBufferSize,50000); // for transcoder important
1171 error=OMX_SetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1173 if (error!=OMX_ErrorNone){
1174 Log::getInstance()->log("Audio", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1179 error=OMX_SendCommand(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port/*codec*/,0);
1180 if (error!=OMX_ErrorNone){
1181 Log::getInstance()->log("Audio", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1185 input_bufs_omx_mutex.Lock();
1186 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1187 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1188 error=OMX_AllocateBuffer(omx_aud_rend/*dec*/,&buf_head,omx_rend_input_port/*codec*/,NULL,port_def_type.nBufferSize);
1189 if (error!=OMX_ErrorNone){
1190 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1191 input_bufs_omx_mutex.Unlock();
1194 input_bufs_omx_all.push_back(buf_head);
1195 input_bufs_omx_free.push_back(buf_head);
1197 omx_first_frame=true;
1200 cur_input_buf_omx=NULL;
1201 input_bufs_omx_mutex.Unlock();
1203 if (!video->CommandFinished(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port /*codec*/)) {
1210 int AudioOMX::DestroyInputBufsOMX() //call with clock mutex locked
1212 OMX_ERRORTYPE error;
1214 cur_input_buf_omx=NULL;
1215 input_bufs_omx_mutex.Lock();
1216 for (int i=0; i< input_bufs_omx_all.size();i++) {
1217 error=OMX_FreeBuffer(omx_aud_rend/*dec*/,omx_rend_input_port/*codec*/,input_bufs_omx_all[i]);
1218 if (error!=OMX_ErrorNone){
1219 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1220 input_bufs_omx_mutex.Unlock();
1225 input_bufs_omx_all.clear();
1226 input_bufs_omx_free.clear();
1227 input_bufs_omx_mutex.Unlock();
1231 int AudioOMX::DestroyInputBufsOMXwhilePlaying() //call with clock mutex locked
1233 OMX_ERRORTYPE error;
1235 cur_input_buf_omx=NULL;
1236 input_bufs_omx_mutex.Lock();
1237 while (input_bufs_omx_all.size()>0) {
1238 if (input_bufs_omx_free.size()>0) {
1239 // Destroy one buffer
1240 vector<OMX_BUFFERHEADERTYPE*>::iterator itty=input_bufs_omx_all.begin();
1241 OMX_BUFFERHEADERTYPE* cur_buf=input_bufs_omx_free.front();
1242 for (; itty!= input_bufs_omx_all.end();itty++) {
1243 if ((*itty)==cur_buf) {
1244 input_bufs_omx_all.erase(itty);
1245 input_bufs_omx_free.pop_front();
1250 input_bufs_omx_mutex.Unlock();
1252 input_bufs_omx_mutex.Lock();
1256 Log::getInstance()->log("Audio", Log::DEBUG, "DestroyInputBufsOMXwhilePlaying %d %d", input_bufs_omx_all.size(),input_bufs_omx_free.size());
1257 input_bufs_omx_mutex.Unlock();
1262 int AudioOMX::DeAllocateCodecsOMX()
1264 OMX_ERRORTYPE error;
1266 VideoOMX *video=(VideoOMX*)Video::getInstance();
1267 Log::getInstance()->log("Audio", Log::DEBUG, "enter deallocatecodecsomx");
1271 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 1");
1272 if (cur_input_buf_omx) {
1273 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1274 OMX_ERRORTYPE error=video->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,cur_input_buf_omx);
1275 if (error!=OMX_ErrorNone) {
1276 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_EmptyThisBuffer 6 failed %x", error);
1279 cur_input_buf_omx=NULL;//write out old data
1281 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 2");
1284 if (omx_aud_rend/*dec*/) {
1285 // first stop the omx elements
1286 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1287 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1289 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 3");
1291 video->UnlockClock();
1292 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 4");
1294 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 5");
1297 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1298 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend ChangeComponentState");
1301 // TODO proper deinit sequence
1302 // first flush all buffers
1304 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
1305 if (error!=OMX_ErrorNone) {
1306 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend in failed %x", error);
1310 /* error=OMX_SendCommand(omx_aud_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
1311 if (error!=OMX_ErrorNone){
1312 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1317 /* if (!video->CommandFinished(omx_aud_dec,OMX_CommandFlush,omx_codec_input_port)) {
1318 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd codec input failed");
1323 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1324 if (error!=OMX_ErrorNone){
1325 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1329 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_clock_port, NULL);
1330 if (error!=OMX_ErrorNone){
1331 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend clock failed %x", error);
1334 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6");
1336 if (!video->CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1337 !video->CommandFinished(omx_aud_rend,OMX_CommandFlush,omx_rend_clock_port)) {
1338 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd clock shed failed");
1341 DestroyInputBufsOMX(); //We have to make sure that no buffers are in use
1342 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6a");
1343 DeinitDecoderLibAV();
1344 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 7");
1347 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true)) {
1348 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 1");
1351 /* if (!video->DisablePort(omx_aud_dec,omx_codec_output_port,true)) {
1352 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 6");
1355 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port,true)) {
1356 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 7");
1360 if (!video->DisablePort(omx_aud_rend,omx_rend_clock_port,true)) {
1361 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 4");
1364 if (!video->DisablePort(omx_clock,omx_clock_output_port,true)) {
1365 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 5");
1370 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,NULL,NULL);
1371 if (error!=OMX_ErrorNone) {
1372 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1378 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_input_port,NULL,NULL);
1379 if (error!=OMX_ErrorNone) {
1380 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1384 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1385 if (error!=OMX_ErrorNone) {
1386 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1390 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_clock_port,NULL,NULL);
1391 if (error!=OMX_ErrorNone) {
1392 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1395 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 8");
1398 //error=OMX_FreeHandle(omx_aud_dec);
1399 error=OMX_FreeHandle(omx_aud_rend);
1400 video->UnlockClock();
1401 video->destroyClock();
1402 omx_aud_rend/*dec*/=NULL;
1403 if (error!=OMX_ErrorNone) {
1404 Log::getInstance()->log("Audio", Log::DEBUG, "FreeHandle failed %d", error);
1408 video->UnlockClock();
1409 DeinitDecoderLibAV();
1411 Log::getInstance()->log("Audio", Log::DEBUG, "leave deallocate codecs OMX");
1418 int AudioOMX::stop()
1420 if (!initted) return 0;
1422 Log::getInstance()->log("Audio", Log::DEBUG, "Audio stop called");
1423 DeAllocateCodecsOMX();
1424 //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1428 int AudioOMX::mute() {
1431 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
1432 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1436 OMX_AUDIO_CONFIG_MUTETYPE amute;
1437 memset(&amute, 0, sizeof(amute));
1438 amute.nSize = sizeof(amute);
1439 amute.nVersion.nVersion = OMX_VERSION;
1440 amute.nPortIndex = omx_rend_input_port;
1441 amute.bMute = OMX_TRUE;
1442 OMX_ERRORTYPE error= OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1444 if (error != OMX_ErrorNone) {
1445 Log::getInstance()->log("Audio", Log::DEBUG,
1446 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1447 omx_rend_input_port);
1459 int AudioOMX::unMute()
1461 if (!initted) return 0;
1463 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
1464 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1468 OMX_AUDIO_CONFIG_MUTETYPE amute;
1469 memset(&amute, 0, sizeof(amute));
1470 amute.nSize = sizeof(amute);
1471 amute.nVersion.nVersion = OMX_VERSION;
1472 amute.nPortIndex = omx_rend_input_port;
1473 amute.bMute = OMX_FALSE;
1474 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1476 if (error != OMX_ErrorNone) {
1477 Log::getInstance()->log("Audio", Log::DEBUG,
1478 "Set OMX_IndexConfigAudioMute failed %x %d", error,
1479 omx_rend_input_port);
1491 int AudioOMX::pause() {
1495 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1501 int AudioOMX::unPause()
1503 if (!initted) return 0;
1505 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1512 int AudioOMX::reset()
1514 if (!initted) return 0;
1516 Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
1517 DeAllocateCodecsOMX();
1519 // if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1520 // Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
1521 // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
1527 int AudioOMX::setVolume(int tvolume)
1529 // parameter: 0 for silence, 20 for full
1530 if ((tvolume < 0) || (tvolume > 20)) return 0;
1532 // volume = 2 * (20 - volume);
1533 // Right, that one was rubbish... 0-10 were almost
1534 // inaudible, 11-20 did what should have been done
1535 // over the whole 0-20 range
1539 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1543 OMX_AUDIO_CONFIG_VOLUMETYPE avol;
1544 memset(&avol, 0, sizeof(avol));
1545 avol.nSize = sizeof(avol);
1546 avol.nVersion.nVersion = OMX_VERSION;
1547 avol.nPortIndex = omx_rend_input_port;
1548 avol.bLinear=OMX_FALSE;
1549 avol.sVolume.nValue =(volume-20)*200;
1550 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioVolume,
1552 if (error != OMX_ErrorNone) {
1553 Log::getInstance()->log("Audio", Log::DEBUG,
1554 "Set OMX_IndexConfigAudioVolume failed %x %d", error,
1555 omx_rend_input_port);
1567 int AudioOMX::test()
1570 // return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
1572 /* aud_sync_parms_t a;
1576 // int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
1579 /*OK*/ //printf("Audio sync disable = %i\n", b);
1587 unsigned int AudioOMX::AdvanceMpAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1589 if (size<=2) return size; // silly;
1590 unsigned int test=0;
1592 //inspired by libav parsing code
1593 while (test+1<size) {
1594 if (data[test]==0xFF && (data[test+1] &0xe0)==0xe0) {
1595 if ((data[test+1] & 0x18) == 0x08) {test++;continue;} //sanity check: mpeg version ID 01 -> reserved
1596 //sanity check inspired by libav
1599 const int sample_rates[4]={44100,48000,32000,0};
1600 const short bitrate_tab[2][3][15] = { { { 0, 32, 64, 96, 128, 160,
1601 192, 224, 256, 288, 320, 352, 384, 416, 448 }, { 0, 32, 48,
1602 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 }, {
1603 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
1604 256, 320 } }, { { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1605 160, 176, 192, 224, 256 }, { 0, 8, 16, 24, 32, 40, 48, 56,
1606 64, 80, 96, 112, 128, 144, 160 }, { 0, 8, 16, 24, 32, 40,
1607 48, 56, 64, 80, 96, 112, 128, 144, 160 } } };
1610 int layer=4-((data[test+1]&0x06)>>1);
1611 if (layer==4) {test++;continue;} //sanity check
1613 int bitrate_index=(data[test+2]&0xf0)>>4;
1614 if (bitrate_index==0x0f || bitrate_index==0x0) {test++;continue;} //sanity check
1616 int samplerate_index=(data[test+2]&0x0C)>>2;
1617 if (samplerate_index==0x03) {test++;continue;} //sanity check
1619 int padding=(data[test+2]&2)>>1;
1620 if (0x10 & data[test+1]) {
1621 lsf=((data[test+1]) &0x8)?0:1;
1627 int sample_rate=sample_rates[ samplerate_index]>>(lsf+mpeg2);
1629 int temp_frame_size=bitrate_tab[lsf][layer - 1][bitrate_index];
1631 frame_size=(temp_frame_size*12000)/sample_rate;
1632 frame_size=(frame_size+padding)*4;
1633 } else if (layer==2) {
1634 frame_size=(temp_frame_size*144000)/sample_rate;
1635 frame_size=frame_size+padding;
1637 frame_size=(temp_frame_size*144000)/(sample_rate<<lsf);
1638 frame_size=frame_size+padding;
1640 unsigned int sameheadertest=(data[test]<<24)|(data[test+1]<<16) |(data[test+2]<<8);
1641 const unsigned mask=(0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)); // from libav
1642 if (!mp3sameheader) {
1643 mp3sameheader=sameheadertest;
1644 mp3sameheadercount=2;
1646 if ((mp3sameheader& mask)== (sameheadertest &mask)) mp3sameheadercount++;
1647 else mp3sameheadercount=0;
1648 mp3sameheader=sameheadertest;
1650 //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);
1651 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME DIAG: %x %x %x %d",data[test],data[test+1],data[test+2],mp3sameheadercount);
1653 if (mp3sameheadercount>4) {
1654 *framesize=frame_size;
1655 return test; // probably FrameSync
1656 } //skip it if the header changes too frequently
1663 unsigned int AudioOMX::AdvanceAc3AudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1665 if (size<=4) return size; // silly;
1666 const int frm_size_tab[] = { 64, 64, 80, 80, 96, 96, 112, 112, 128, 128,
1667 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384, 384, 448,
1668 448, 512, 512, 640, 640, 768, 768, 896, 896, 1024, 1024, 1152,
1669 1152, 1280, 1280, };
1670 unsigned int test=0;
1671 *framesize=20000; //if we do not find a start code do not decompress
1672 while (test+4<size) {
1673 if (data[test]==0x0B && data[test+1]==0x77) {
1674 // now figure out the length of the frame
1675 unsigned char code=data[test+4];
1676 unsigned char fscod=(code& 0xC0)>>6;
1677 unsigned char frmsize=(code &0x3f);
1678 if (fscod!=0) Log::getInstance()->log("Audio", Log::DEBUG, "warning we only support 48 KHz sampling rate");
1679 *framesize=frm_size_tab[frmsize]*2;
1680 return test; // probably FrameSync
1687 unsigned int AudioOMX::AdvanceAacLatmAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1689 if (size<=4) return size; // silly;
1690 unsigned int test=0;
1691 *framesize=20000; //if we do not find a start code do not decompress
1692 while (test+4<size) {
1693 if (data[test] ==0x56 && (data[test+1]& 0xe0)==0xe0) {
1694 // now figure out the length of the frame
1695 unsigned int length= ((0x1f & data[test+1])<<8) | data[test+2];
1696 *framesize=length+3;
1697 return test; // probably FrameSync
1705 void AudioOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1707 packet = mplist.front();
1710 UINT AudioOMX::DeliverMediaSample(UCHAR* buffer, UINT* samplepos) {
1711 DeliverMediaPacket(packet, buffer, samplepos);
1712 if (*samplepos == packet.length) {
1720 long long AudioOMX::correctAudioLatency(long long pts,int addsamples,int srate) {
1722 VideoOMX *video = (VideoOMX*) Video::getInstance();
1724 OMX_PARAM_U32TYPE audio_lat;
1725 OMX_ERRORTYPE error;
1726 memset(&audio_lat, 0, sizeof(audio_lat));
1727 audio_lat.nSize = sizeof(audio_lat);
1728 audio_lat.nVersion.nVersion = OMX_VERSION;
1729 audio_lat.nPortIndex = omx_rend_input_port;
1731 error = OMX_GetConfig(omx_aud_rend, OMX_IndexConfigAudioRenderingLatency,
1733 video->UnlockClock();
1734 if (error != OMX_ErrorNone) {
1735 Log::getInstance()->log("Audio", Log::DEBUG,
1736 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
1737 omx_rend_input_port);
1738 return pts; // no correction in case of error
1740 /*Log::getInstance()->log("Audio", Log::DEBUG, "Current audio latency %d",
1743 long long workpts=0;
1744 workpts+=addsamples;
1745 workpts-=audio_lat.nU32;
1746 workpts*=10LL*1000LL*1000LL;
1747 workpts=workpts/((long long)srate); //one second /samplerate
1753 bool AudioOMX::DrainTargetBufferFull()
1755 //Check, if we have OMX output buffers
1757 input_bufs_omx_mutex.Lock();
1758 full=(input_bufs_omx_free.size()==0);
1759 input_bufs_omx_mutex.Unlock();
1765 UINT AudioOMX::DeliverMediaPacket(MediaPacket packet, const UCHAR* buffer,
1767 /*First Check, if we have an audio sample*/
1768 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1770 OMX_ERRORTYPE error;
1771 Log *logger=Log::getInstance();
1772 if (vw->InIframemode()) {
1775 return 0; //Not in iframe mode!
1778 if (!omx_running) return 0; // if we are not runnig do not do this
1779 if (vw->isClockPaused()) return 0; //Block if we pause
1780 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 1");
1782 //Log::getInstance()->log("Audio", Log::DEBUG, "DeliverMediaPacketOMX time %lld",packet.presentation_time);
1784 /* if (packet.synched && packet.presentation_time <= 0) {
1785 *samplepos = packet.length;
1786 firstsynched = false;
1788 Log::getInstance()->log("Audio", Log::DEBUG,
1789 "DeliverMediaPacketOMX Frameskip");
1790 return packet.length;
1793 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 2");
1795 UINT headerstrip = 0;
1796 if (packet.disconti) {
1797 firstsynched = false;
1798 decompress_buffer_filled=0;
1799 if (cur_input_buf_omx) {
1800 OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1802 if (error != OMX_ErrorNone) {
1803 Log::getInstance()->log("Audio", Log::DEBUG,
1804 "OMX_EmptyThisBuffer 1 failed %x", error);
1806 cur_input_buf_omx = NULL;
1810 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 3");
1811 if (packet.type != lastAType) {//Format Change //Push data out !
1812 firstsynched = false;
1815 Log::getInstance()->log("Audio", Log::DEBUG,"Notice audio type change %d %d", packet.type,lastAType);
1816 lastAType = packet.type;
1817 decompress_buffer_filled=0;
1819 if (cur_input_buf_omx) {
1820 OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1822 if (error != OMX_ErrorNone) {
1823 Log::getInstance()->log("Audio", Log::DEBUG,
1824 "OMX_EmptyThisBuffer 2 failed %x", error);
1826 cur_input_buf_omx = NULL;
1831 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
1832 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
1834 if (!ChangeAudioPortConfig(true)) {
1835 Log::getInstance()->log("Audio", Log::DEBUG,
1836 "Changing audio port config failed", error);
1839 pthread_setcancelstate(oldcancelstate, NULL);
1840 pthread_setcanceltype(oldcanceltype, NULL);
1844 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 4");
1846 /*Inspect PES-Header */
1847 if (*samplepos == 0 && packet.type != MPTYPE_MPEG_AUDIO_LAYER3) {//stripheader
1848 headerstrip = buffer[packet.pos_buffer + 8] + 9;
1849 if (packet.type == MPTYPE_AC3)
1850 headerstrip += 4; //skip ac3 bytes
1851 *samplepos += headerstrip;
1852 if (packet.synched) {
1853 if (cur_input_buf_omx) {
1854 //cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1855 OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1857 if (error != OMX_ErrorNone) {
1858 Log::getInstance()->log("Audio", Log::DEBUG,
1859 "OMX_EmptyThisBuffer 3 failed %x", error);
1861 //vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1863 cur_input_buf_omx = NULL;//write out old data
1865 firstsynched = true;
1866 //decompress_buffer_filled=0;
1868 if (!firstsynched) {//
1869 *samplepos = packet.length;//if we have not processed at least one
1870 decompress_buffer_filled=0;
1871 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 5");
1872 return packet.length;//synched packet ignore it!
1876 if (!cur_input_buf_omx) {
1877 input_bufs_omx_mutex.Lock();
1878 if (input_bufs_omx_free.size()==0) {
1879 input_bufs_omx_mutex.Unlock();
1880 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 6");
1881 //Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample");
1882 return 0; // we do not have a free media sample
1885 cur_input_buf_omx=input_bufs_omx_free.front();
1886 cur_input_buf_omx->nFilledLen=0;
1887 cur_input_buf_omx->nOffset=0;
1888 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
1889 input_bufs_omx_free.pop_front();
1890 input_bufs_omx_mutex.Unlock();
1892 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 7");
1894 if (cur_input_buf_omx->nFilledLen == 0) {//will only be changed on first packet
1895 if (packet.synched) {
1896 //Log::getInstance()->log("Audio", Log::DEBUG,
1897 // "packet synched marker");
1899 //lastreftimePTS=packet.pts;
1900 if (omx_first_frame) { // TODO time
1901 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
1902 Log::getInstance()->log("Audio", Log::DEBUG, "Starttime");
1903 omx_first_frame = false;
1905 cur_input_buf_omx->nFlags = 0;
1906 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
1908 lastreftimeOMX = packet.presentation_time;
1909 //Log::getInstance()->log("Audio", Log::DEBUG,
1910 // "Time code %lld pts %lld dts %lld", lastreftimeOMX, packet.pts,packet.dts);
1911 lastreftimePTS = packet.pts;
1912 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
1914 // Log::getInstance()->log("Audio", Log::DEBUG,
1915 // "packet NOT synched marker");
1916 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
1917 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
1920 if (packet.disconti || achange) {
1921 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_DISCONTINUITY;
1922 //mp23codec_context_libav->frame_size=-1;
1923 //ac3codec_context_libav->frame_size=-1;
1927 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 8");
1929 if (*samplepos>packet.length) *samplepos=0; //propably the thread got interrupted and sample is not valid any more!
1930 unsigned int haveToCopy=packet.length-*samplepos;
1933 while (haveToCopy>0) {
1934 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 9");
1936 unsigned int gotframesize=0;
1938 switch (packet.type) {
1939 case MPTYPE_MPEG_AUDIO:
1940 case MPTYPE_MPEG_AUDIO_LAYER3: {
1941 adv = AdvanceMpAudioSync(buffer+packet.pos_buffer+*samplepos,
1942 haveToCopy,&gotframesize);
1946 case MPTYPE_AC3_PRE13: {
1947 adv = AdvanceAc3AudioSync(buffer+packet.pos_buffer+*samplepos,
1948 haveToCopy,&gotframesize);
1952 case MPTYPE_AAC_LATM: {
1953 adv = AdvanceAacLatmAudioSync(buffer+packet.pos_buffer+*samplepos,
1954 haveToCopy,&gotframesize);
1958 if (adv!=haveToCopy) {
1963 *samplepos=packet.length; //no ac3 sync byte
1964 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 10");
1965 return packet.length;
1968 // so everything is fine now do a memcpy
1969 int cancopy=min(haveToCopy,cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen);
1970 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
1971 haveToCopy-=cancopy;
1972 cur_input_buf_omx->nFilledLen+=cancopy;
1973 *samplepos+=cancopy;
1974 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 11");
1976 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
1977 if (error != OMX_ErrorNone) {
1978 Log::getInstance()->log("Audio", Log::DEBUG,
1979 "OMX_EmptyThisBuffer 5 failed %x", error);
1981 cur_input_buf_omx=NULL;
1984 input_bufs_omx_mutex.Lock();
1985 if (input_bufs_omx_free.size()==0) {
1986 input_bufs_omx_mutex.Unlock();
1987 // Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample2");
1988 return *samplepos; // we do not have a free media sample
1990 cur_input_buf_omx=input_bufs_omx_free.front();
1991 cur_input_buf_omx->nFilledLen=0;
1992 cur_input_buf_omx->nOffset=0;
1993 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
1994 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
1995 input_bufs_omx_free.pop_front();
1996 input_bufs_omx_mutex.Unlock();
1998 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 12");
2001 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 13");
2009 AVCodecContext *current_context;
2010 switch (packet.type) {
2011 case MPTYPE_MPEG_AUDIO:
2012 case MPTYPE_MPEG_AUDIO_LAYER3: {
2013 current_context = mp23codec_context_libav;
2014 if (current_context->frame_size<0) framesize=1152; //Maximum framesize
2015 else framesize=current_context->frame_size;
2017 case MPTYPE_AAC_LATM: {
2018 current_context = aaclatmcodec_context_libav;
2021 case MPTYPE_AC3_PRE13: {
2022 current_context = ac3codec_context_libav;
2026 if (decompress_buffer_filled) { // have a remaining paket
2027 incoming_paket_libav.data =(uint8_t*) decompress_buffer;
2028 memcpy(decompress_buffer+decompress_buffer_filled,
2029 buffer+packet.pos_buffer+*samplepos,
2030 min(haveToCopy,decompress_buffer_size-decompress_buffer_filled));
2031 incoming_paket_libav.size = decompress_buffer_filled
2032 +min(haveToCopy,decompress_buffer_size-decompress_buffer_filled);
2033 //Log::getInstance()->log("Audio", Log::DEBUG,"Use saved audio buffer %d %d %d",packet.type,decompress_buffer_filled,packet.synched);
2035 incoming_paket_libav.data =(uint8_t*) buffer+packet.pos_buffer+*samplepos;
2036 incoming_paket_libav.size = haveToCopy;
2038 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 18");
2040 while (haveToCopy> 0 && errcount<3) {
2042 //Log::getInstance()->log("Audio", Log::DEBUG,"libav in %d %d",framesize,current_context->frame_size);
2043 //Log::getInstance()->log("Audio", Log::DEBUG, "libav in %d %d",
2044 // framesize, current_context->frame_size);
2046 bool donotdecompress=false;
2047 unsigned int gotframesize=0;
2048 // if (!decompress_buffer_filled) { // only do this if no old data is present
2050 switch (packet.type) {
2051 case MPTYPE_MPEG_AUDIO:
2052 case MPTYPE_MPEG_AUDIO_LAYER3: {
2053 adv = AdvanceMpAudioSync(incoming_paket_libav.data,
2054 incoming_paket_libav.size,&gotframesize);
2058 case MPTYPE_AC3_PRE13: {
2059 adv = AdvanceAc3AudioSync(incoming_paket_libav.data,
2060 incoming_paket_libav.size,&gotframesize);
2063 case MPTYPE_AAC_LATM: {
2064 adv = AdvanceAacLatmAudioSync(incoming_paket_libav.data,
2065 incoming_paket_libav.size,&gotframesize);
2069 if (adv > 0 /*&& !decompress_buffer_filled*/) {
2070 incoming_paket_libav.data += adv;
2071 incoming_paket_libav.size-=adv;
2074 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2075 else*/ framesize=haveToCopy;
2076 //Log::getInstance()->log("Audio", Log::DEBUG,"Advance by %d %d from %d",adv,packet.type,*samplepos );
2077 if (haveToCopy <= 0) {
2078 // Log::getInstance()->log("Audio", Log::DEBUG,"No sync code in packet remove %d",packet.type);
2079 *samplepos=packet.length;
2080 return packet.length;
2086 if (gotframesize>0 && gotframesize>haveToCopy) {
2087 donotdecompress=true;
2088 errcount=100; // exit loop
2090 // else Log::getInstance()->log("Audio", Log::DEBUG,"Loop run" );
2092 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 19");
2093 if (!donotdecompress) {
2096 pthread_testcancel();
2097 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2098 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2100 if (!omx_running || !mp23codec_context_libav
2101 || !ac3codec_context_libav) {
2102 libav_mutex.Unlock();
2105 libav_mutex.Unlock();
2106 // Log::getInstance()->log("Audio", Log::DEBUG,"libav out");
2107 int save_size=incoming_paket_libav.size;
2109 if (gotframesize<=incoming_paket_libav.size) {
2110 if (gotframesize>0) incoming_paket_libav.size=gotframesize;
2111 len = avcodec_decode_audio4(current_context, decode_frame_libav,
2112 &gotta, &incoming_paket_libav);
2114 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:E %d %d",gotframesize,incoming_paket_libav.size);
2118 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:T %d",len);
2119 incoming_paket_libav.size=save_size;
2120 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out1");
2121 pthread_setcancelstate(oldcancelstate, NULL);
2122 pthread_setcanceltype(oldcanceltype, NULL);
2123 pthread_testcancel();
2129 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out2");
2130 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 20");
2132 libav_mutex.Unlock();
2136 if (decompress_buffer_filled) { // reset to normal decoding
2138 //Log::getInstance()->log("Audio", Log::DEBUG,"saved audio: %d",len);
2139 haveToCopy -= min(len-decompress_buffer_filled,0);
2140 *samplepos += min(len-decompress_buffer_filled,0);
2141 //if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2142 /*else*/ framesize=haveToCopy;
2144 framesize=haveToCopy;
2146 incoming_paket_libav.data =(uint8_t*) buffer+packet.pos_buffer+*samplepos;
2148 decompress_buffer_filled=0;
2152 incoming_paket_libav.data += len;
2156 /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2157 else*/framesize=haveToCopy;
2160 framesize=haveToCopy;
2163 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 21");
2165 incoming_paket_libav.size =framesize;
2167 //Log::getInstance()->log("Audio", Log::DEBUG,
2170 int dsize = av_samples_get_buffer_size(NULL,
2171 /*current_context->channels*/2, decode_frame_libav->nb_samples,
2172 AV_SAMPLE_FMT_S16, 1);
2173 int dsize_in = av_samples_get_buffer_size(NULL,
2174 current_context->channels, decode_frame_libav->nb_samples,
2175 current_context->sample_fmt, 1);
2176 //if (current_context->channels==1) dsize*=2; // we convert mono to stereo
2177 if ((cur_input_buf_omx->nFilledLen + dsize)
2178 > cur_input_buf_omx->nAllocLen ) {
2179 // I doubt that this will ever happen
2180 // Log::getInstance()->log("Audio", Log::DEBUG,
2181 // "P 2 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
2182 OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
2184 if (error != OMX_ErrorNone) {
2185 Log::getInstance()->log("Audio", Log::DEBUG,
2186 "OMX_EmptyThisBuffer 4 failed %x", error);
2188 cur_input_buf_omx = NULL;
2190 if (!cur_input_buf_omx) {
2192 while (count < 10 && omx_running) {
2194 input_bufs_omx_mutex.Lock();
2195 if (input_bufs_omx_free.size() == 0) {
2196 input_bufs_omx_mutex.Unlock();
2197 // Log::getInstance()->log("Audio", Log::DEBUG,
2198 // "Deliver MediaPacket no free sample");
2200 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark22");
2201 if (!omx_running) return *samplepos;
2202 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 23");
2205 cur_input_buf_omx = input_bufs_omx_free.front();
2206 cur_input_buf_omx->nFilledLen = 0;
2207 cur_input_buf_omx->nOffset = 0;
2208 cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
2209 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2210 input_bufs_omx_free.pop_front();
2211 input_bufs_omx_mutex.Unlock();
2214 if (!cur_input_buf_omx) return *samplepos;
2219 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy in %d %d %d" ,dsize,current_context->sample_rate,cur_input_buf_omx->nFilledLen);
2222 av_opt_set_int(resam_con_libav, "in_sample_rate",decode_frame_libav->sample_rate,0);
2223 av_opt_set_int(resam_con_libav, "in_sample_fmt",decode_frame_libav->format,0);
2224 av_opt_set_int(resam_con_libav, "in_channel_layout",decode_frame_libav->channel_layout, 0);
2225 //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);
2226 //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);
2228 int ret=avresample_open(resam_con_libav);
2230 Log::getInstance()->log("Audio", Log::ERR,"Opening AV resample failed %d",ret);
2232 uint8_t *output=cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen;
2234 avresample_convert(resam_con_libav, &output,
2235 dsize, decode_frame_libav->nb_samples,
2236 decode_frame_libav->extended_data, decode_frame_libav->linesize[0], decode_frame_libav->nb_samples);
2237 avresample_close(resam_con_libav);
2240 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy out");
2241 cur_input_buf_omx->nFilledLen += dsize;
2243 //Log::getInstance()->log("Audio", Log::DEBUG,"Incomplete mpeg frames in pes packet %d %d",incoming_paket_libav.size,packet.length);
2244 /* uint8_t a1=incoming_paket_libav.data[0];
2245 uint8_t a2=incoming_paket_libav.data[1];
2246 uint8_t a3=incoming_paket_libav.data[2];
2247 uint8_t a4=incoming_paket_libav.data[3];*/
2248 // Log::getInstance()->log("Audio", Log::DEBUG,"Header %x %x %x %x",a1,a2,
2253 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 24");
2254 decompress_buffer_filled=0;
2256 //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);
2257 memcpy(decompress_buffer,incoming_paket_libav.data,min(haveToCopy,decompress_buffer_size));
2259 decompress_buffer_filled=min(haveToCopy,decompress_buffer_size);
2263 if (cur_input_buf_omx->nFilledLen) {
2264 //Log::getInstance()->log("Audio", Log::DEBUG,
2265 // "P 3 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
2266 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2267 if (error != OMX_ErrorNone) {
2268 Log::getInstance()->log("Audio", Log::DEBUG,
2269 "OMX_EmptyThisBuffer 5 failed %x", error);
2271 //if (packet.synched) vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
2272 cur_input_buf_omx = NULL;
2277 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 25");
2279 *samplepos=packet.length;
2280 return packet.length;
2286 long long AudioOMX::SetStartOffset(long long curreftime, bool *rsync){
2287 VideoOMX *vw=(VideoOMX*)Video::getInstance();
2288 return vw->SetStartAudioOffset(curreftime,rsync);
2291 void AudioOMX::ResetTimeOffsets() {
2292 VideoOMX *vw=(VideoOMX*)Video::getInstance();
2293 vw->ResetTimeOffsets();