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"
33 lastAType = MPTYPE_MPEG_AUDIO;
38 canpass_pcm_mch=false;
40 prefered_ac3=0; //0 stereo PCM, 1 passthrough 2 Multichannel PCM
47 omx_aud_rend/*dec*/=0;
48 cur_input_buf_omx=NULL;
51 ac3codec_context_libav=NULL;
54 mp23codec_context_libav=NULL;
62 int AudioOMX::init(UCHAR tstreamType) {
67 streamType = tstreamType;
69 if (!initAllParams()) {
79 av_log_set_flags(AV_LOG_SKIP_REPEATED);
81 ac3codec_libav = avcodec_find_decoder(CODEC_ID_AC3);
82 if (ac3codec_libav == NULL) {
83 Log::getInstance()->log("Audio", Log::DEBUG,
84 "Find libav ac3 decoder failed");
88 mp23codec_libav = avcodec_find_decoder(CODEC_ID_MP3);
89 if (mp23codec_libav == NULL) {
90 Log::getInstance()->log("Audio", Log::DEBUG,
91 "Find libav mpeg audio decoder failed");
98 int AudioOMX::initAllParams()
100 return (setStreamType(streamType) && setChannel() && setSource());
103 int AudioOMX::shutdown()
105 if (!initted) return 0;
108 Log::getInstance()->log("Audio", Log::DEBUG, "audio shutdown called");
109 DeAllocateCodecsOMX();
114 bool AudioOMX::loadOptionsfromServer(VDR* vdr)
116 Log::getInstance()->log("Audio", Log::DEBUG, "AudioOMX config load");
117 char *name=vdr->configLoad("AudioOMX","AC3DecodingMode");
120 if (STRCASECMP(name, "PCM") == 0) {
122 } else if (STRCASECMP(name, "Passthrough") == 0) {
124 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
129 name = vdr->configLoad("AudioOMX", "Mp2DecodingMode");
132 if (STRCASECMP(name, "PCM") == 0) {
134 } else if (STRCASECMP(name, "Passthrough") == 0) {
136 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
141 name = vdr->configLoad("AudioOMX", "Mp3DecodingMode");
144 if (STRCASECMP(name, "PCM") == 0) {
146 } else if (STRCASECMP(name, "Passthrough") == 0) {
148 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
153 name = vdr->configLoad("AudioOMX", "AudioOutput");
156 if (STRCASECMP(name, "analog") == 0) {
158 } else if (STRCASECMP(name, "HDMI") == 0) {
168 bool AudioOMX::handleOptionChanges(Option* option)
170 if (Audio::handleOptionChanges(option))
172 switch (option->id) {
174 if (STRCASECMP(option->options[option->userSetChoice], "analog") == 0) {
176 } else if (STRCASECMP(option->options[option->userSetChoice], "HDMI")
184 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
186 } else if (STRCASECMP(option->options[option->userSetChoice],
187 "Passthrough") == 0) {
189 } else if (STRCASECMP(option->options[option->userSetChoice],
190 "PCMMultichannel") == 0) {
196 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
198 } else if (STRCASECMP(option->options[option->userSetChoice],
199 "Passthrough") == 0) {
201 } else if (STRCASECMP(option->options[option->userSetChoice],
202 "PCMMultichannel") == 0) {
208 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
210 } else if (STRCASECMP(option->options[option->userSetChoice],
211 "Passthrough") == 0) {
213 } else if (STRCASECMP(option->options[option->userSetChoice],
214 "PCMMultichannel") == 0) {
224 bool AudioOMX::saveOptionstoServer()
227 switch (prefered_ac3) {
229 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode", "PCM");
232 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
236 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
241 switch (prefered_mp2) {
243 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode", "PCM");
246 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
250 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
255 switch (prefered_mp3) {
257 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode", "PCM");
260 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
264 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
270 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "analog");
272 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "HDMI");
278 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
279 UINT numChoices, UINT defaultChoice, UINT startInt,
280 const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
282 bool AudioOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
284 if (!Audio::addOptionsToPanes(panenumber,options,pane)) return false;
291 static const char* audioopts[]={"analog","HDMI"};
292 option = new Option(4,tr("Audio Output"), "AudioOMX","AudioOutput",Option::TYPE_TEXT,2,0,0,audioopts,NULL,false,this);
293 options->push_back(option);
294 pane->addOptionLine(option);
297 char **ac3opts=new char *[3];
299 ac3opts[i]=new char[strlen("PCM")+1];
300 strcpy(ac3opts[i],"PCM");
303 ac3opts[i]=new char[strlen("Passthrough")+1];
304 strcpy(ac3opts[i],"PassThrough");
307 if (canpass_pcm_mch) {
308 ac3opts[i]=new char[strlen("PCMMultichannel")+1];
309 strcpy(ac3opts[i],"PCMMultichannel");
312 option = new Option(1 ,tr("AC3 HDMI Mode"), "AudioOMX", "AC3DecodingMode", Option::TYPE_TEXT, i, 0, 0, ac3opts,NULL,true, this);
313 options->push_back(option);
314 pane->addOptionLine(option);
316 char **mp2opts = new char *[3];
318 mp2opts[i] = new char[strlen("PCM") + 1];
319 strcpy(mp2opts[i], "PCM");
322 mp2opts[i] = new char[strlen("Passthrough") + 1];
323 strcpy(mp2opts[i], "PassThrough");
326 if (canpass_pcm_mch) {
327 mp2opts[i] = new char[strlen("PCMMultichannel") + 1];
328 strcpy(mp2opts[i], "PCMMultichannel");
331 option = new Option(2, tr("Mp2 HDMI Mode"), "AudioOMX",
332 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0,
333 mp2opts, NULL, true, this);
334 options->push_back(option);
335 pane->addOptionLine(option);
337 char **mp3opts = new char *[3];
339 mp3opts[i] = new char[strlen("PCM") + 1];
340 strcpy(mp3opts[i], "PCM");
343 mp3opts[i] = new char[strlen("Passthrough") + 1];
344 strcpy(mp3opts[i], "PassThrough");
347 if (canpass_pcm_mch) {
348 mp3opts[i] = new char[strlen("PCMMultichannel") + 1];
349 strcpy(mp3opts[i], "PCMMultichannel");
352 option = new Option(3, tr("Mp3 HDMI Mode"), "AudioOMX",
353 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0, mp3opts,
355 options->push_back(option);
356 pane->addOptionLine(option);
369 OMX_ERRORTYPE AudioOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
371 //Log::getInstance()->log("Audio", Log::NOTICE, "EmptyBufferDone");
372 AudioOMX *audio=(AudioOMX *)getInstance();
373 audio->ReturnEmptyOMXBuffer(buffer);
374 return OMX_ErrorNone;
378 void AudioOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
379 input_bufs_omx_mutex.Lock();
380 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
381 input_bufs_omx_free.push_back(buffer);
382 //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
383 input_bufs_omx_mutex.Unlock();
386 OMX_ERRORTYPE AudioOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
387 Log::getInstance()->log("Audio", Log::NOTICE, "FillBufferDone");
388 return OMX_ErrorNone;
393 int AudioOMX::setStreamType(UCHAR type)
395 if (!initted) return 0;
397 // if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
401 int AudioOMX::setChannel()
403 if (!initted) return 0;
405 // if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
409 int AudioOMX::setSource()
411 if (!initted) return 0;
413 // if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
419 if (!initted) return 0;
421 // if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
425 int AudioOMX::play() {
428 lastAType=MPTYPE_MPEG_AUDIO;
429 Log::getInstance()->log("Audio", Log::DEBUG, "enter play");
431 if (!AllocateCodecsOMX()) {
438 int AudioOMX::ChangeAudioDestination() //clock aka omx mutex needs to be locked
441 const char * destinations[]={"local","hdmi"};
446 OMX_CONFIG_BRCMAUDIODESTINATIONTYPE auddest;
447 memset(&auddest,0,sizeof(auddest));
448 auddest.nSize=sizeof(auddest);
449 auddest.nVersion.nVersion=OMX_VERSION;
450 strcpy((char *)auddest.sName, destinations[dest]);
452 Log::getInstance()->log("Audio", Log::DEBUG, "setting destination to: %s",auddest.sName);
453 error=OMX_SetConfig(omx_aud_rend,OMX_IndexConfigBrcmAudioDestination,&auddest);
454 if (error!=OMX_ErrorNone){
455 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX_IndexConfigBrcmAudioDestination failed %x %x %s", error,omx_aud_rend,auddest.sName);
456 DeAllocateCodecsOMX();
464 int AudioOMX::ChangeAudioPortConfig() //clock aka omx mutex needs to be locked
467 //Ok first fidle a working configuration
468 Log::getInstance()->log("Audio", Log::DEBUG,
469 "ChangeAudioPortConfig");
473 case MPTYPE_MPEG_AUDIO: {
474 if (prefered_mp2 == 3 && false) { //not supported yet
477 if (prefered_mp2 == 2 && canpass_mp2) {
484 case MPTYPE_AC3_PRE13:
486 if (prefered_ac3 == 3 && false) { //not supported yet
489 if (prefered_ac3 == 2 && canpass_ac3) {
496 case MPTYPE_MPEG_AUDIO_LAYER3: {
497 if (prefered_mp3 == 3 && false) { //not supported yet
500 if (prefered_mp3 == 2 && canpass_mp2) {
510 //mch=false; // multichannel also false
515 /*OMX_CONFIG_BOOLEANTYPE booly;
516 memset(&booly, 0, sizeof(booly));
517 booly.nSize = sizeof(booly);
518 booly.nVersion.nVersion = OMX_VERSION;
520 booly.bEnabled = OMX_TRUE;
522 booly.bEnabled = OMX_FALSE;
524 error = OMX_SetParameter(omx_aud_dec, OMX_IndexParamBrcmDecoderPassThrough,
526 if (error != OMX_ErrorNone) {
527 Log::getInstance()->log("Audio", Log::DEBUG,
528 "Init OMX_IndexParamBrcmDecoderPassThrough failed %x", error);
529 DeAllocateCodecsOMX();
532 VideoOMX *video=(VideoOMX*)Video::getInstance();
540 OMX_AUDIO_PARAM_PCMMODETYPE audio_pcm;
541 memset(&audio_pcm, 0, sizeof(audio_pcm));
542 audio_pcm.nSize = sizeof(audio_pcm);
543 audio_pcm.nVersion.nVersion = OMX_VERSION;
544 audio_pcm.nChannels = 2;
545 audio_pcm.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
546 audio_pcm.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
547 //audio_pcm.eChannelMapping[2]=OMX_AUDIO_ChannelMax;
548 audio_pcm.eNumData = OMX_NumericalDataSigned;
549 audio_pcm.eEndian = OMX_EndianLittle;
550 audio_pcm.bInterleaved = OMX_TRUE;
551 audio_pcm.nBitPerSample = 16;
552 audio_pcm.ePCMMode = OMX_AUDIO_PCMModeLinear;
553 audio_pcm.nChannels = 2;
554 audio_pcm.nSamplingRate = 48000;
555 audio_pcm.nPortIndex = omx_rend_input_port;
556 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPcm,
558 if (error != OMX_ErrorNone) {
559 Log::getInstance()->log("Audio", Log::DEBUG,
560 "Init OMX_IndexParamAudioPcm failed %x %d", error,
561 omx_rend_input_port);
562 DeAllocateCodecsOMX();
576 int AudioOMX::InitDecoderLibAV()
579 ac3codec_context_libav = avcodec_alloc_context3(ac3codec_libav);
580 if (!ac3codec_context_libav) {
581 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for ac3 decoding context failed!");
585 ac3codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
586 ac3codec_context_libav->request_channels=2;
588 int avc_ret = avcodec_open2(ac3codec_context_libav, ac3codec_libav, NULL);
590 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
591 libav_mutex.Unlock();
595 mp23codec_context_libav = avcodec_alloc_context3(mp23codec_libav);
596 if (!ac3codec_context_libav) {
597 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for mp23 decoding context failed!");
598 libav_mutex.Unlock();
602 mp23codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
603 mp23codec_context_libav->request_channels=2;
605 avc_ret = avcodec_open2(mp23codec_context_libav, mp23codec_libav, NULL);
607 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec failed \n");
608 libav_mutex.Unlock();
612 av_init_packet(&incoming_paket_libav);
613 decode_frame_libav=avcodec_alloc_frame();
614 libav_mutex.Unlock();
621 void AudioOMX::DeinitDecoderLibAV() {
625 if (ac3codec_context_libav) {
626 avcodec_close(ac3codec_context_libav);
627 av_free(ac3codec_context_libav);
628 ac3codec_context_libav = NULL;
629 av_free(decode_frame_libav);
630 avcodec_close(mp23codec_context_libav);
631 av_free(mp23codec_context_libav);
632 mp23codec_context_libav = NULL;
635 libav_mutex.Unlock();
640 int AudioOMX::AllocateCodecsOMX()
643 static OMX_CALLBACKTYPE callbacks= {&VideoOMX::EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
645 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX");
646 //Clock, move later to audio
647 VideoOMX *video=(VideoOMX*)Video::getInstance();
649 if (!InitDecoderLibAV()) return 0;;
652 OMX_PORT_PARAM_TYPE p_param;
653 memset(&p_param,0,sizeof(p_param));
654 p_param.nSize=sizeof(p_param);
655 p_param.nVersion.nVersion=OMX_VERSION;
658 if (!video->getClockAudioandInit(&omx_clock,&omx_clock_output_port)){
659 return 0;// get the clock and init it if necessary
663 if (!video->idleClock()) {
668 error = OMX_GetHandle(&omx_aud_rend, VPE_OMX_AUDIO_REND, NULL, &callbacks);
669 if (error != OMX_ErrorNone) {
670 Log::getInstance()->log("Audio", Log::DEBUG,
671 "Init OMX audio rend failed %x", error);
672 video->UnlockClock();
673 DeAllocateCodecsOMX();
677 if (!ChangeAudioDestination()) {
678 video->UnlockClock();
679 DeAllocateCodecsOMX();
683 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioInit, &p_param);
684 if (error != OMX_ErrorNone) {
685 Log::getInstance()->log("Audio", Log::DEBUG,
686 "Init OMX audio rend OMX_GetParameter failed %x", error);
687 video->UnlockClock();
688 DeAllocateCodecsOMX();
691 omx_rend_input_port = p_param.nStartPortNumber;
693 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamOtherInit, &p_param);
694 if (error != OMX_ErrorNone) {
695 Log::getInstance()->log("Audio", Log::DEBUG,
696 "Init OMX aud rend OMX_GetParameter failed %x", error);
697 video->UnlockClock();
698 DeAllocateCodecsOMX();
701 // buggy return value
702 omx_rend_clock_port = p_param.nStartPortNumber;
705 /* error=OMX_GetHandle(&omx_aud_dec,VPE_OMX_AUDIO_DECODER,NULL,&callbacks);
707 if (error!=OMX_ErrorNone){
708 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder failed %x", error);
709 video->UnlockClock();
710 DeAllocateCodecsOMX();
714 error=OMX_GetParameter(omx_aud_dec,OMX_IndexParamAudioInit,&p_param);
715 if (error!=OMX_ErrorNone){
716 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder OMX_GetParameter failed %x", error);
717 video->UnlockClock();
718 DeAllocateCodecsOMX();
721 omx_codec_input_port=p_param.nStartPortNumber;
722 omx_codec_output_port=p_param.nStartPortNumber+1;
724 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port) || !video->DisablePort(omx_aud_dec,omx_codec_output_port)) {
725 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio decoder failed");
726 video->UnlockClock();
727 DeAllocateCodecsOMX();
734 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true) ) {
735 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio rend failed %d",omx_rend_input_port);
736 video->UnlockClock();
737 DeAllocateCodecsOMX();
741 if ( !video->DisablePort(omx_aud_rend, omx_rend_clock_port, true)) {
742 Log::getInstance()->log("Audio", Log::DEBUG,
743 "Disable Ports OMX rend clock port failed %d",omx_rend_clock_port);
744 video->UnlockClock();
745 DeAllocateCodecsOMX();
755 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_aud_rend,omx_rend_clock_port);
756 if (error!=OMX_ErrorNone){
757 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);
758 video->UnlockClock();
759 DeAllocateCodecsOMX();
763 if (!video->EnablePort(omx_clock,omx_clock_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_clock_port,false)
765 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX clock rend failed");
766 video->UnlockClock();
767 DeAllocateCodecsOMX();
771 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
772 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend idle ChangeComponentState");
773 video->UnlockClock();
774 DeAllocateCodecsOMX();
781 if ( !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_clock_port)) {
782 video->UnlockClock();
783 DeAllocateCodecsOMX();
787 if ( !video->CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
788 video->UnlockClock();
789 DeAllocateCodecsOMX();
795 if (!ChangeAudioPortConfig()){
796 Log::getInstance()->log("Audio", Log::NOTICE, "Change AudioPortConfig failed");
797 video->UnlockClock();
798 DeAllocateCodecsOMX();
802 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
803 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
804 DeAllocateCodecsOMX();
810 if (!PrepareInputBufsOMX()) {
811 video->UnlockClock();
812 DeAllocateCodecsOMX();
818 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,omx_aud_rend,omx_rend_input_port);
819 if (error!=OMX_ErrorNone){
820 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel dec to render failed %x", error);
821 video->UnlockClock();
822 DeAllocateCodecsOMX();
828 /* if (!video->EnablePort(omx_aud_dec,omx_codec_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_input_port,false)
830 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX codec rend failed");
831 video->UnlockClock();
832 DeAllocateCodecsOMX();
836 /* if ( !video->CommandFinished(omx_aud_dec,OMX_CommandPortEnable,omx_codec_output_port)
837 || !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
838 video->UnlockClock();
839 DeAllocateCodecsOMX();
843 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateExecuting)) {
844 Log::getInstance()->log("Audio", Log::DEBUG, "omx_aud_rend ChangeComponentState Execute");
845 video->UnlockClock();
846 DeAllocateCodecsOMX();
851 video->UnlockClock();
856 if (!video->setClockExecutingandRunning()) return 0;
858 Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX finished");
866 int AudioOMX::PrepareInputBufsOMX() //needs to be called with locvke omx clock mutex
868 VideoOMX *video=(VideoOMX*)Video::getInstance();
870 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
871 memset(&port_def_type,0,sizeof(port_def_type));
872 port_def_type.nSize=sizeof(port_def_type);
873 port_def_type.nVersion.nVersion=OMX_VERSION;
874 port_def_type.nPortIndex=omx_rend_input_port;//omx_codec_input_port;
876 error=OMX_GetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
878 if (error!=OMX_ErrorNone){
879 Log::getInstance()->log("Audio", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
883 port_def_type.nBufferCountActual=2;
884 port_def_type.nBufferSize=max(port_def_type.nBufferSize,50000); // for transcoder important
886 error=OMX_SetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
888 if (error!=OMX_ErrorNone){
889 Log::getInstance()->log("Audio", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
893 error=OMX_SendCommand(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port/*codec*/,0);
894 if (error!=OMX_ErrorNone){
895 Log::getInstance()->log("Audio", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
899 input_bufs_omx_mutex.Lock();
900 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
901 OMX_BUFFERHEADERTYPE *buf_head=NULL;
902 error=OMX_AllocateBuffer(omx_aud_rend/*dec*/,&buf_head,omx_rend_input_port/*codec*/,NULL,port_def_type.nBufferSize);
903 if (error!=OMX_ErrorNone){
904 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
905 input_bufs_omx_mutex.Unlock();
908 input_bufs_omx_all.push_back(buf_head);
909 input_bufs_omx_free.push_back(buf_head);
911 omx_first_frame=true;
914 cur_input_buf_omx=NULL;
915 input_bufs_omx_mutex.Unlock();
917 if (!video->CommandFinished(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port /*codec*/)) {
924 int AudioOMX::DestroyInputBufsOMX() //call with clock mutex locked
928 cur_input_buf_omx=NULL;
929 input_bufs_omx_mutex.Lock();
930 for (int i=0; i< input_bufs_omx_all.size();i++) {
931 error=OMX_FreeBuffer(omx_aud_rend/*dec*/,omx_rend_input_port/*codec*/,input_bufs_omx_all[i]);
932 if (error!=OMX_ErrorNone){
933 Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
934 input_bufs_omx_mutex.Unlock();
939 input_bufs_omx_all.clear();
940 input_bufs_omx_free.clear();
941 input_bufs_omx_mutex.Unlock();
946 int AudioOMX::DeAllocateCodecsOMX()
950 VideoOMX *video=(VideoOMX*)Video::getInstance();
951 Log::getInstance()->log("Audio", Log::DEBUG, "enter deallocatecodecsomx");
955 if (cur_input_buf_omx) {
956 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
957 OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_aud_rend/*dec*/,cur_input_buf_omx);
958 if (error!=OMX_ErrorNone) {
959 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
962 cur_input_buf_omx=NULL;//write out old data
965 if (omx_aud_rend/*dec*/) {
966 // first stop the omx elements
967 /* if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
968 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
971 video->UnlockClock();
975 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
976 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend ChangeComponentState");
979 // TODO proper deinit sequence
980 // first flush all buffers
982 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
983 if (error!=OMX_ErrorNone) {
984 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend in failed %x", error);
988 /* error=OMX_SendCommand(omx_aud_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
989 if (error!=OMX_ErrorNone){
990 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush codec out failed %x", error);
995 /* if (!video->CommandFinished(omx_aud_dec,OMX_CommandFlush,omx_codec_input_port)) {
996 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd codec input failed");
1001 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1002 if (error!=OMX_ErrorNone){
1003 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1007 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_clock_port, NULL);
1008 if (error!=OMX_ErrorNone){
1009 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend clock failed %x", error);
1013 if (!video->CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1014 !video->CommandFinished(omx_aud_rend,OMX_CommandFlush,omx_rend_clock_port)) {
1015 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd clock shed failed");
1018 DestroyInputBufsOMX(); //We have to make sure that no buffers are in use
1019 DeinitDecoderLibAV();
1022 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true)) {
1023 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 1");
1026 /* if (!video->DisablePort(omx_aud_dec,omx_codec_output_port,true)) {
1027 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 6");
1030 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port,true)) {
1031 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 7");
1035 if (!video->DisablePort(omx_aud_rend,omx_rend_clock_port,true)) {
1036 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 4");
1039 if (!video->DisablePort(omx_clock,omx_clock_output_port,true)) {
1040 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 5");
1045 /* error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,NULL,NULL);
1046 if (error!=OMX_ErrorNone) {
1047 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1053 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_input_port,NULL,NULL);
1054 if (error!=OMX_ErrorNone) {
1055 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1059 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1060 if (error!=OMX_ErrorNone) {
1061 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1065 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_clock_port,NULL,NULL);
1066 if (error!=OMX_ErrorNone) {
1067 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1072 //error=OMX_FreeHandle(omx_aud_dec);
1073 error=OMX_FreeHandle(omx_aud_rend);
1074 video->UnlockClock();
1075 video->destroyClock();
1076 omx_aud_rend/*dec*/=NULL;
1077 if (error!=OMX_ErrorNone) {
1078 Log::getInstance()->log("Audio", Log::DEBUG, "FreeHandle failed %d", error);
1082 video->UnlockClock();
1083 DeinitDecoderLibAV();
1085 Log::getInstance()->log("Audio", Log::DEBUG, "leave deallocate codecs OMX");
1092 int AudioOMX::stop()
1094 if (!initted) return 0;
1096 Log::getInstance()->log("Audio", Log::DEBUG, "Audio stop called");
1097 DeAllocateCodecsOMX();
1098 //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1102 int AudioOMX::mute()
1104 if (!initted) return 0;
1106 // if (ioctl(fdAudio, AV_SET_AUD_MUTE, 1) != 0) return 0;
1107 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
1113 int AudioOMX::unMute()
1115 if (!initted) return 0;
1117 // if (ioctl(fdAudio, AV_SET_AUD_MUTE, 0) != 0) return 0;
1118 Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
1124 int AudioOMX::pause() {
1129 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1131 OMX_ERRORTYPE error;
1132 error = OMX_SendCommand(omx_aud_rend, OMX_CommandFlush,
1133 omx_rend_input_port, NULL);
1134 if (error != OMX_ErrorNone) {
1135 Log::getInstance()->log("Audio", Log::DEBUG,
1136 "OMX_Flush rend in failed %x", error);
1145 int AudioOMX::unPause()
1147 if (!initted) return 0;
1149 paused=false; // may be also change omx clock
1150 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1156 int AudioOMX::reset()
1158 if (!initted) return 0;
1160 Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
1161 DeAllocateCodecsOMX();
1163 // if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1164 // Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
1165 // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
1171 int AudioOMX::setVolume(int tvolume)
1173 // parameter: 0 for silence, 20 for full
1174 if ((tvolume < 0) || (tvolume > 20)) return 0;
1176 // volume = 2 * (20 - volume);
1177 // Right, that one was rubbish... 0-10 were almost
1178 // inaudible, 11-20 did what should have been done
1179 // over the whole 0-20 range
1181 tvolume = 20 - tvolume;
1183 audio_volume Avolume;
1184 Avolume.frontleft = tvolume + Aoffset.frontleft;
1185 Avolume.frontright = tvolume + Aoffset.frontright;
1186 Avolume.rearleft = tvolume + Aoffset.rearleft;
1187 Avolume.rearright = tvolume + Aoffset.rearright;
1188 Avolume.center = tvolume + Aoffset.center;
1189 Avolume.lfe = tvolume + Aoffset.lfe;
1191 // if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &Avolume) != 0) return 0;
1193 // unsigned long vol = (tvolume << 24) | (tvolume << 16);
1195 // Log::getInstance()->log("Audio", Log::DEBUG, "%lx", vol);
1196 // Log::getInstance()->log("Audio", Log::DEBUG, "%i", tvolume);
1198 // if (ioctl(fdAudio, AV_SET_AUD_VOLUME, &vol) != 0) return 0;
1203 int AudioOMX::test()
1206 // return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
1208 /* aud_sync_parms_t a;
1212 // int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
1215 /*OK*/ //printf("Audio sync disable = %i\n", b);
1223 void AudioOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1225 packet = mplist.front();
1228 UINT AudioOMX::DeliverMediaSample(UCHAR* buffer, UINT* samplepos) {
1229 DeliverMediaPacket(packet, buffer, samplepos);
1230 if (*samplepos == packet.length) {
1238 long long AudioOMX::correctAudioLatency(long long pts,int addsamples,int srate) {
1240 VideoOMX *video = (VideoOMX*) Video::getInstance();
1242 OMX_PARAM_U32TYPE audio_lat;
1243 OMX_ERRORTYPE error;
1244 memset(&audio_lat, 0, sizeof(audio_lat));
1245 audio_lat.nSize = sizeof(audio_lat);
1246 audio_lat.nVersion.nVersion = OMX_VERSION;
1247 audio_lat.nPortIndex = omx_rend_input_port;
1249 error = OMX_GetConfig(omx_aud_rend, OMX_IndexConfigAudioRenderingLatency,
1251 video->UnlockClock();
1252 if (error != OMX_ErrorNone) {
1253 Log::getInstance()->log("Audio", Log::DEBUG,
1254 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
1255 omx_rend_input_port);
1256 return pts; // no correction in case of error
1258 /*Log::getInstance()->log("Audio", Log::DEBUG, "Current audio latency %d",
1261 long long workpts=0;
1262 workpts+=addsamples;
1263 workpts-=audio_lat.nU32;
1264 workpts*=10LL*1000LL*1000LL;
1265 workpts=workpts/((long long)srate); //one second /samplerate
1273 UINT AudioOMX::DeliverMediaPacket(MediaPacket packet, const UCHAR* buffer,
1275 /*First Check, if we have an audio sample*/
1276 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1278 OMX_ERRORTYPE error;
1279 Log *logger=Log::getInstance();
1280 if (vw->InIframemode()) {
1283 return 0; //Not in iframe mode!
1286 if (!omx_running) return 0; // if we are not runnig do not do this
1287 if (paused) return 0; //Block if we pause
1289 UINT headerstrip = 0;
1290 if (packet.disconti) {
1291 firstsynched = false;
1292 if (cur_input_buf_omx) {
1293 OMX_ERRORTYPE error = OMX_EmptyThisBuffer(omx_aud_rend/*dec*/,
1295 if (error != OMX_ErrorNone) {
1296 Log::getInstance()->log("Audio", Log::DEBUG,
1297 "OMX_EmptyThisBuffer failed %x", error);
1299 cur_input_buf_omx = NULL;
1303 if (packet.type != lastAType) {//Format Change //Push data out !
1304 firstsynched = false;
1306 lastAType = packet.type;
1308 if (cur_input_buf_omx) {
1309 OMX_ERRORTYPE error = OMX_EmptyThisBuffer(omx_aud_rend/*dec*/,
1311 if (error != OMX_ErrorNone) {
1312 Log::getInstance()->log("Audio", Log::DEBUG,
1313 "OMX_EmptyThisBuffer failed %x", error);
1315 cur_input_buf_omx = NULL;
1317 if (!ChangeAudioPortConfig()) {
1318 Log::getInstance()->log("Audio", Log::DEBUG,
1319 "Changing audio port config failed", error);
1324 /*Inspect PES-Header */
1325 if (*samplepos == 0 && packet.type != MPTYPE_MPEG_AUDIO_LAYER3) {//stripheader
1326 headerstrip = buffer[packet.pos_buffer + 8] + 9;
1327 if (packet.type == MPTYPE_AC3)
1328 headerstrip += 4; //skip ac3 bytes
1329 *samplepos += headerstrip;
1330 if (packet.synched) {
1331 if (cur_input_buf_omx) {
1332 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1333 OMX_ERRORTYPE error = OMX_EmptyThisBuffer(omx_aud_rend/*dec*/,
1335 if (error != OMX_ErrorNone) {
1336 Log::getInstance()->log("Audio", Log::DEBUG,
1337 "OMX_EmptyThisBuffer failed %x", error);
1339 //FrameWaitforDisplay(lastreftimeOMX);
1340 vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1342 cur_input_buf_omx = NULL;//write out old data
1344 firstsynched = true;
1346 if (!firstsynched) {//
1347 *samplepos = packet.length;//if we have not processed at least one
1348 return packet.length;//synched packet ignore it!
1352 if (!cur_input_buf_omx) {
1353 input_bufs_omx_mutex.Lock();
1354 if (input_bufs_omx_free.size()==0) {
1355 input_bufs_omx_mutex.Unlock();
1356 //Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample");
1357 return 0; // we do not have a free media sample
1360 cur_input_buf_omx=input_bufs_omx_free.front();
1361 cur_input_buf_omx->nFilledLen=0;
1362 cur_input_buf_omx->nOffset=0;
1363 cur_input_buf_omx->nTimeStamp=0;
1364 input_bufs_omx_free.pop_front();
1365 input_bufs_omx_mutex.Unlock();
1369 if (cur_input_buf_omx->nFilledLen == 0) {//will only be changed on first packet
1370 if (packet.synched) {
1371 //Log::getInstance()->log("Audio", Log::DEBUG,
1372 // "packet synched marker");
1374 //lastreftimePTS=packet.pts;
1375 if (omx_first_frame) { // TODO time
1376 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
1377 Log::getInstance()->log("Audio", Log::DEBUG, "Starttime");
1378 omx_first_frame = false;
1380 cur_input_buf_omx->nFlags = 0;
1381 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
1383 lastreftimeOMX = packet.presentation_time;
1384 Log::getInstance()->log("Audio", Log::DEBUG,
1385 "Time code %lld pts %lld dts %lld", lastreftimeOMX, packet.pts,packet.dts);
1386 lastreftimePTS = packet.pts;
1387 cur_input_buf_omx->nTimeStamp =0;// lastreftimeOMX; // the clock component is faulty;
1389 // Log::getInstance()->log("Audio", Log::DEBUG,
1390 // "packet NOT synched marker");
1391 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
1392 cur_input_buf_omx->nTimeStamp = 0;
1395 if (packet.disconti || achange) {
1396 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_DISCONTINUITY;
1397 //mp23codec_context_libav->frame_size=-1;
1398 //ac3codec_context_libav->frame_size=-1;
1403 unsigned int haveToCopy=packet.length-*samplepos;
1412 AVCodecContext *current_context;
1413 switch (packet.type) {
1414 case MPTYPE_MPEG_AUDIO:
1415 case MPTYPE_MPEG_AUDIO_LAYER3: {
1416 current_context = mp23codec_context_libav;
1417 if (current_context->frame_size<0) framesize=1152; //Maximum framesize
1418 else framesize=current_context->frame_size;
1421 case MPTYPE_AC3_PRE13: {
1422 current_context = ac3codec_context_libav;
1426 incoming_paket_libav.data =(uint8_t*) buffer+packet.pos_buffer+*samplepos;
1427 incoming_paket_libav.size = haveToCopy;
1429 while (haveToCopy> 0 && errcount<2) {
1431 //Log::getInstance()->log("Audio", Log::DEBUG,"libav in %d %d",framesize,current_context->frame_size);
1433 if (!omx_running || !mp23codec_context_libav || !ac3codec_context_libav) {
1434 libav_mutex.Unlock();
1437 Log::getInstance()->log("Audio", Log::DEBUG,"libav in %d %d",framesize,current_context->frame_size);
1438 len = avcodec_decode_audio4(current_context, decode_frame_libav,
1439 &gotta, &incoming_paket_libav);
1440 Log::getInstance()->log("Audio", Log::DEBUG,"libav out");
1442 libav_mutex.Unlock();
1445 libav_mutex.Unlock();
1446 // Log::getInstance()->log("Audio", Log::DEBUG,"libav out");
1448 incoming_paket_libav.data += len;
1452 if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
1453 else framesize=haveToCopy;
1456 framesize=haveToCopy;
1459 incoming_paket_libav.size =framesize;
1461 //Log::getInstance()->log("Audio", Log::DEBUG,
1463 int dsize = av_samples_get_buffer_size(NULL,
1464 current_context->channels, decode_frame_libav->nb_samples,
1465 current_context->sample_fmt, 1);
1466 if ((cur_input_buf_omx->nFilledLen + dsize)
1467 > cur_input_buf_omx->nAllocLen ) {
1468 // I doubt that this will ever happen
1469 Log::getInstance()->log("Audio", Log::DEBUG,
1470 "P 2 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
1471 OMX_ERRORTYPE error = OMX_EmptyThisBuffer(omx_aud_rend/*dec*/,
1473 if (error != OMX_ErrorNone) {
1474 Log::getInstance()->log("Audio", Log::DEBUG,
1475 "OMX_EmptyThisBuffer failed %x", error);
1477 cur_input_buf_omx = NULL;
1479 if (!cur_input_buf_omx) {
1481 while (count < 10 && omx_running) {
1483 input_bufs_omx_mutex.Lock();
1484 if (input_bufs_omx_free.size() == 0) {
1485 input_bufs_omx_mutex.Unlock();
1486 Log::getInstance()->log("Audio", Log::DEBUG,
1487 "Deliver MediaPacket no free sample");
1489 if (!omx_running) return 0;
1492 cur_input_buf_omx = input_bufs_omx_free.front();
1493 cur_input_buf_omx->nFilledLen = 0;
1494 cur_input_buf_omx->nOffset = 0;
1495 cur_input_buf_omx->nTimeStamp = 0;
1496 input_bufs_omx_free.pop_front();
1497 input_bufs_omx_mutex.Unlock();
1500 if (!cur_input_buf_omx) return *samplepos;
1505 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy in %d %d %d" ,dsize,current_context->sample_rate,cur_input_buf_omx->nFilledLen);
1506 memcpy(cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen,
1507 decode_frame_libav->data[0], dsize);
1508 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy out");
1509 cur_input_buf_omx->nFilledLen += dsize;
1511 //Log::getInstance()->log("Audio", Log::DEBUG,"Incomplete mpeg frames in pes packet %d",incoming_paket_libav.size);
1518 if (cur_input_buf_omx->nFilledLen) {
1519 Log::getInstance()->log("Audio", Log::DEBUG,
1520 "P 3 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
1521 error = OMX_EmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
1522 if (error != OMX_ErrorNone) {
1523 Log::getInstance()->log("Audio", Log::DEBUG,
1524 "OMX_EmptyThisBuffer failed %x", error);
1526 if (packet.synched) vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1527 cur_input_buf_omx = NULL;
1532 *samplepos=packet.length;
1533 return packet.length;
1539 long long AudioOMX::SetStartOffset(long long curreftime, bool *rsync){
1540 VideoOMX *vw=(VideoOMX*)Video::getInstance();
1541 return vw->SetStartAudioOffset(curreftime,rsync);
1544 void AudioOMX::ResetTimeOffsets() {
1545 VideoOMX *vw=(VideoOMX*)Video::getInstance();
1546 vw->ResetTimeOffsets();