]> git.vomp.tv Git - vompclient.git/blob - audioomx.cc
Reactivate remote reintialization in videoomx and purge power events in changepowerstate
[vompclient.git] / audioomx.cc
1 /*
2     Copyright 2004-2005 Chris Tallon, 2009 Marten Richter
3
4     This file is part of VOMP.
5
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.
10
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.
15
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.
19 */
20
21 #include "audioomx.h"
22 #include "videoomx.h"
23 #include "log.h"
24 #include "vdr.h"
25 #include "woptionpane.h"
26
27 #include "osdopenvg.h"
28 #include <bcm_host.h>
29
30 AudioOMX::AudioOMX()
31 {
32   initted = 0;
33   streamType = 0;
34   volume = 20;
35   muted = 0;
36   lastAType = MPTYPE_MPEG_AUDIO;
37
38   canpass_ac3=false;
39   canpass_mp2=false;
40   canpass_mp3=false;
41   canpass_aac=false;
42   canpass_pcm_mch=false;
43
44   prefered_ac3=0; //0 stereo PCM, 1 passthrough 2 Multichannel PCM
45   prefered_mp2=0;
46   prefered_mp3=0;
47   prefered_aac=0;
48   hdmi=true;
49
50   omx_running=false;
51
52   omx_aud_rend/*dec*/=0;
53   cur_input_buf_omx=NULL;
54
55   ac3codec_libav=NULL;
56   ac3codec_context_libav=NULL;
57
58   mp23codec_libav=NULL;
59   mp23codec_context_libav=NULL;
60 #ifdef USE_LIBRESAMPLE
61   resam_con_libav=NULL;
62 #endif
63
64   decompress_buffer=NULL;
65   decompress_buffer_size=0;
66   decompress_buffer_filled=0;
67   mp3sameheader=0;
68   mp3sameheadercount=0;
69
70 }
71
72 AudioOMX::~AudioOMX()
73 {
74 }
75
76 int AudioOMX::init(UCHAR tstreamType) {
77         if (initted)
78                 return 0;
79         initted = 1;
80
81         streamType = tstreamType;
82
83         if (!initAllParams()) {
84                 shutdown();
85                 return 0;
86         }
87
88         unMute();
89
90         decompress_buffer_size=20000;
91         decompress_buffer=(UCHAR*)malloc(decompress_buffer_size);
92         decompress_buffer_filled=0;
93
94
95
96         av_register_all();
97         av_log_set_flags(AV_LOG_SKIP_REPEATED);
98
99         ac3codec_libav = avcodec_find_decoder(CODEC_ID_AC3);
100         if (ac3codec_libav == NULL) {
101                 Log::getInstance()->log("Audio", Log::DEBUG,
102                                 "Find libav ac3 decoder failed");
103                 return 0;
104         }
105
106         mp23codec_libav = avcodec_find_decoder(CODEC_ID_MP3);
107         if (mp23codec_libav == NULL) {
108                 Log::getInstance()->log("Audio", Log::DEBUG,
109                                 "Find libav mpeg audio decoder failed");
110                 return 0;
111         }
112
113         aaclatmcodec_libav = avcodec_find_decoder(CODEC_ID_AAC_LATM);
114         if (aaclatmcodec_libav == NULL) {
115                 Log::getInstance()->log("Audio", Log::DEBUG,
116                                 "Find libav aac latm decoder failed");
117                 return 0;
118         }
119
120         int ret;
121
122         ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eMPEG1,2,EDID_AudioSampleRate_e48KHz,0);
123         if (ret==0) {
124                 //canpass_mp2=true;
125                 // not implemented
126                 Log::getInstance()->log("Audio", Log::NOTICE,
127                                                 "TV hdmi supports mpeg1 layer 1 and 2");
128         }
129         ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eMP3,2,EDID_AudioSampleRate_e48KHz,0);
130         if (ret==0) {
131                 //canpass_mp3=true;
132                 // not implemented
133                 Log::getInstance()->log("Audio", Log::NOTICE,
134                                                 "TV hdmi supports mpeg1 layer 3");
135         }
136
137         ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eAC3,6,EDID_AudioSampleRate_e48KHz,0);
138         if (ret==0) {
139                 canpass_ac3=true;
140                 Log::getInstance()->log("Audio", Log::NOTICE,
141                                                 "TV hdmi supports AC3");
142         }
143         ret=vc_tv_hdmi_audio_supported( EDID_AudioFormat_eAAC,6,EDID_AudioSampleRate_e48KHz,0);
144         if (ret==0) {
145                 canpass_aac=true;
146                 //not implemented
147                 Log::getInstance()->log("Audio", Log::NOTICE,
148                                 "TV hdmi supports AAC");
149         }
150
151         canpass_pcm_mch=false;
152
153         return 1;
154 }
155
156 int AudioOMX::initAllParams()
157 {
158   return (setStreamType(streamType) && setChannel() && setSource());
159 }
160
161 int AudioOMX::shutdown()
162 {
163   if (!initted) return 0;
164   initted = 0;
165
166   Log::getInstance()->log("Audio", Log::DEBUG, "audio shutdown called");
167   DeAllocateCodecsOMX();
168
169   free(decompress_buffer);
170   decompress_buffer=NULL;
171   decompress_buffer_size=0;
172   decompress_buffer_filled=0;
173
174   return 1;
175 }
176
177 bool AudioOMX::loadOptionsfromServer(VDR* vdr)
178 {
179           Log::getInstance()->log("Audio", Log::DEBUG, "AudioOMX config load");
180     char *name=vdr->configLoad("AudioOMX","AC3DecodingMode");
181
182     if (name != NULL) {
183                 if (STRCASECMP(name, "PCM") == 0) {
184                         prefered_ac3 = 0;
185                 } else if (STRCASECMP(name, "Passthrough") == 0) {
186                         prefered_ac3 = 1;
187                 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
188                         prefered_ac3 = 2;
189                 }
190         }
191
192     name = vdr->configLoad("AudioOMX", "Mp2DecodingMode");
193
194         if (name != NULL) {
195                 if (STRCASECMP(name, "PCM") == 0) {
196                         prefered_mp2 = 0;
197                 } else if (STRCASECMP(name, "Passthrough") == 0) {
198                         prefered_mp2 = 1;
199                 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
200                         prefered_mp2 = 2;
201                 }
202         }
203
204         name = vdr->configLoad("AudioOMX", "AACDecodingMode");
205
206         if (name != NULL) {
207                 if (STRCASECMP(name, "PCM") == 0) {
208                         prefered_aac = 0;
209                 } else if (STRCASECMP(name, "Passthrough") == 0) {
210                         prefered_aac = 1;
211                 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
212                         prefered_aac = 2;
213                 }
214         }
215
216         name = vdr->configLoad("AudioOMX", "Mp3DecodingMode");
217
218         if (name != NULL) {
219                 if (STRCASECMP(name, "PCM") == 0) {
220                         prefered_mp3 = 0;
221                 } else if (STRCASECMP(name, "Passthrough") == 0) {
222                         prefered_mp3 = 1;
223                 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
224                         prefered_mp3 = 2;
225                 }
226         }
227
228         name = vdr->configLoad("AudioOMX", "AudioOutput");
229
230         if (name != NULL) {
231                 if (STRCASECMP(name, "analog") == 0) {
232                         hdmi = false;
233                 } else if (STRCASECMP(name, "HDMI") == 0) {
234                         hdmi = true;
235                 }
236         }
237
238
239    return true;
240
241 }
242
243 bool AudioOMX::handleOptionChanges(Option* option)
244 {
245     if (Audio::handleOptionChanges(option))
246                 return true;
247         switch (option->id) {
248         case 4: {
249                 if (STRCASECMP(option->options[option->userSetChoice], "analog") == 0) {
250                         hdmi = false;
251                 } else if (STRCASECMP(option->options[option->userSetChoice], "HDMI")
252                                 == 0) {
253                         hdmi = true;
254                 }
255                 return true;
256         }
257                 break;
258         case 1: {
259                 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
260                         prefered_ac3 = 0;
261                 } else if (STRCASECMP(option->options[option->userSetChoice],
262                                 "Passthrough") == 0) {
263                         prefered_ac3 = 1;
264                 } else if (STRCASECMP(option->options[option->userSetChoice],
265                                 "PCMMultichannel") == 0) {
266                         prefered_ac3 = 2;
267                 }
268         }
269                 break;
270         case 2: {
271                 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
272                         prefered_mp2 = 0;
273                 } else if (STRCASECMP(option->options[option->userSetChoice],
274                                 "Passthrough") == 0) {
275                         prefered_mp2 = 1;
276                 } else if (STRCASECMP(option->options[option->userSetChoice],
277                                 "PCMMultichannel") == 0) {
278                         prefered_mp2 = 2;
279                 }
280         }
281                 break;
282         case 3: {
283                 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
284                         prefered_mp3 = 0;
285                 } else if (STRCASECMP(option->options[option->userSetChoice],
286                                 "Passthrough") == 0) {
287                         prefered_mp3 = 1;
288                 } else if (STRCASECMP(option->options[option->userSetChoice],
289                                 "PCMMultichannel") == 0) {
290                         prefered_mp3 = 2;
291                 }
292         }
293                 break;
294         case 5: {
295                 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
296                         prefered_aac = 0;
297                 } else if (STRCASECMP(option->options[option->userSetChoice],
298                                 "Passthrough") == 0) {
299                         prefered_aac = 1;
300                 } else if (STRCASECMP(option->options[option->userSetChoice],
301                                 "PCMMultichannel") == 0) {
302                         prefered_aac = 2;
303                 }
304         }
305         break;
306         };
307         return false;
308
309 }
310
311 bool AudioOMX::saveOptionstoServer()
312 {
313
314     switch (prefered_ac3) {
315         case 0:
316                 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode", "PCM");
317                 break;
318         case 1:
319                 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
320                                 "Passthrough");
321                 break;
322         case 2:
323                 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
324                                 "PCMMultichannel");
325                 break;
326         };
327
328     switch (prefered_aac) {
329         case 0:
330                 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode", "PCM");
331                 break;
332         case 1:
333                 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
334                                 "Passthrough");
335                 break;
336         case 2:
337                 VDR::getInstance()->configSave("AudioOMX", "AACDecodingMode",
338                                 "PCMMultichannel");
339                 break;
340         };
341
342         switch (prefered_mp2) {
343         case 0:
344                 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode", "PCM");
345                 break;
346         case 1:
347                 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
348                                 "Passthrough");
349                 break;
350         case 2:
351                 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
352                                 "PCMMultichannel");
353                 break;
354         };
355
356         switch (prefered_mp3) {
357         case 0:
358                 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode", "PCM");
359                 break;
360         case 1:
361                 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
362                                 "Passthrough");
363                 break;
364         case 2:
365                 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
366                                 "PCMMultichannel");
367                 break;
368         };
369
370         if (!hdmi)
371                 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "analog");
372         else
373                 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "HDMI");
374
375
376     return true;
377 }
378
379 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
380            UINT numChoices, UINT defaultChoice, UINT startInt,
381            const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
382
383 bool AudioOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
384 {
385     if (!Audio::addOptionsToPanes(panenumber,options,pane)) return false;
386
387
388     Option* option;
389     if (panenumber == 2)
390     {
391
392         static const char* audioopts[]={"analog","HDMI"};
393         option = new Option(4,tr("Audio Output"), "AudioOMX","AudioOutput",Option::TYPE_TEXT,2,0,0,audioopts,NULL,false,this);
394         options->push_back(option);
395         pane->addOptionLine(option);
396
397
398         char **ac3opts=new char *[3];
399         int i=0;
400         ac3opts[i]=new char[strlen("PCM")+1];
401         strcpy(ac3opts[i],"PCM");
402         i++;
403         if (canpass_ac3) {
404                 ac3opts[i]=new char[strlen("Passthrough")+1];
405             strcpy(ac3opts[i],"PassThrough");
406             i++;
407         }
408         if (canpass_pcm_mch) {
409                 ac3opts[i]=new char[strlen("PCMMultichannel")+1];
410             strcpy(ac3opts[i],"PCMMultichannel");
411             i++;
412         }
413         option = new Option(1 ,tr("AC3 HDMI Mode"), "AudioOMX", "AC3DecodingMode", Option::TYPE_TEXT, i, 0, 0, ac3opts,NULL,true, this);
414         options->push_back(option);
415         pane->addOptionLine(option);
416
417  /*       char **aacopts = new char *[3];
418         i = 0;
419         aacopts[i] = new char[strlen("PCM") + 1];
420         strcpy(mp2opts[i], "PCM");
421         i++;
422         if (canpass_aac) {
423                 aacopts[i] = new char[strlen("Passthrough") + 1];
424                 strcpy(aacopts[i], "PassThrough");
425                 i++;
426         }
427         if (canpass_pcm_mch) {
428                 aacopts[i] = new char[strlen("PCMMultichannel") + 1];
429                 strcpy(aacopts[i], "PCMMultichannel");
430                 i++;
431         }
432         option = new Option(5, tr("Mp2 HDMI Mode"), "AudioOMX",
433                         "AACDecodingMode", Option::TYPE_TEXT, i, 0, 0,
434                         aacopts, NULL, true, this);
435         options->push_back(option);
436         pane->addOptionLine(option);
437
438
439         char **mp2opts = new char *[3];
440                 i = 0;
441                 mp2opts[i] = new char[strlen("PCM") + 1];
442                 strcpy(mp2opts[i], "PCM");
443                 i++;
444                 if (canpass_mp2) {
445                         mp2opts[i] = new char[strlen("Passthrough") + 1];
446                         strcpy(mp2opts[i], "PassThrough");
447                         i++;
448                 }
449                 if (canpass_pcm_mch) {
450                         mp2opts[i] = new char[strlen("PCMMultichannel") + 1];
451                         strcpy(mp2opts[i], "PCMMultichannel");
452                         i++;
453                 }
454                 option = new Option(2, tr("Mp2 HDMI Mode"), "AudioOMX",
455                                 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0,
456                                 mp2opts, NULL, true, this);
457                 options->push_back(option);
458                 pane->addOptionLine(option);
459
460                 char **mp3opts = new char *[3];
461                 i = 0;
462                 mp3opts[i] = new char[strlen("PCM") + 1];
463                 strcpy(mp3opts[i], "PCM");
464                 i++;
465                 if (canpass_mp3) {
466                         mp3opts[i] = new char[strlen("Passthrough") + 1];
467                         strcpy(mp3opts[i], "PassThrough");
468                         i++;
469                 }
470                 if (canpass_pcm_mch) {
471                         mp3opts[i] = new char[strlen("PCMMultichannel") + 1];
472                         strcpy(mp3opts[i], "PCMMultichannel");
473                         i++;
474                 }
475                 option = new Option(3, tr("Mp3 HDMI Mode"), "AudioOMX",
476                                 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0, mp3opts,
477                                 NULL, true, this);
478                 options->push_back(option);
479                 pane->addOptionLine(option);*/
480         // Comment unsupported modes out
481
482
483     }
484
485     return true;
486 }
487
488
489
490
491
492
493 OMX_ERRORTYPE AudioOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
494
495         //Log::getInstance()->log("Audio", Log::NOTICE, "EmptyBufferDone");
496         AudioOMX *audio=(AudioOMX *)getInstance();
497         audio->ReturnEmptyOMXBuffer(buffer);
498         return OMX_ErrorNone;
499
500 }
501
502 void AudioOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
503         input_bufs_omx_mutex.Lock();
504         //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
505         input_bufs_omx_free.push_back(buffer);
506         //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
507         input_bufs_omx_mutex.Unlock();
508 }
509
510  OMX_ERRORTYPE AudioOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
511          Log::getInstance()->log("Audio", Log::NOTICE, "FillBufferDone");
512         return OMX_ErrorNone;
513 }
514
515
516
517 int AudioOMX::setStreamType(UCHAR type)
518 {
519   if (!initted) return 0;
520
521  // if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
522   return 1;
523 }
524
525 int AudioOMX::setChannel()
526 {
527   if (!initted) return 0;
528
529  // if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
530   return 1;
531 }
532
533 int AudioOMX::setSource()
534 {
535   if (!initted) return 0;
536
537  // if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
538   return 1;
539 }
540
541 int AudioOMX::sync()
542 {
543   if (!initted) return 0;
544
545  // if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
546   return 1;
547 }
548
549 int AudioOMX::play() {
550         if (!initted)
551                 return 0;
552         lastAType=MPTYPE_MPEG_AUDIO;
553         Log::getInstance()->log("Audio", Log::DEBUG, "enter play");
554
555         ((VideoOMX*)Video::getInstance())->interlaceSwitch4Demux(); // switch resolution if necessary
556
557         if (!AllocateCodecsOMX()) {
558                 return 0;
559         }
560         return 1;
561 }
562
563
564 int AudioOMX::ChangeAudioDestination() //clock aka omx mutex needs to be locked
565 {
566         OMX_ERRORTYPE error;
567         const char * destinations[]={"local","hdmi"};
568         int dest=0;
569         if (hdmi) dest=1;
570         else dest=0;
571
572         OMX_CONFIG_BRCMAUDIODESTINATIONTYPE auddest;
573         memset(&auddest,0,sizeof(auddest));
574         auddest.nSize=sizeof(auddest);
575         auddest.nVersion.nVersion=OMX_VERSION;
576         strcpy((char *)auddest.sName, destinations[dest]);
577
578         Log::getInstance()->log("Audio", Log::DEBUG, "setting destination to: %s",auddest.sName);
579         error=OMX_SetConfig(omx_aud_rend,OMX_IndexConfigBrcmAudioDestination,&auddest);
580         if (error!=OMX_ErrorNone){
581                 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX_IndexConfigBrcmAudioDestination failed %x %x %s", error,omx_aud_rend,auddest.sName);
582                 DeAllocateCodecsOMX();
583                 return 0;
584         }
585         return 1;
586
587
588 }
589
590 int AudioOMX::ChangeAudioPortConfig(bool disport) //clock aka omx mutex needs to be locked
591 {
592         OMX_ERRORTYPE error;
593         //Ok first fidle a working configuration
594         Log::getInstance()->log("Audio", Log::DEBUG,
595                                                         "ChangeAudioPortConfig");
596
597         OMX_AUDIO_CODINGTYPE encoding;
598         if (hdmi) {
599                 switch (lastAType) {
600                 case MPTYPE_MPEG_AUDIO: {
601                         if (prefered_mp2 == 2 && false) { //not supported yet
602
603                         } else {
604                                 if (prefered_mp2 == 1 && canpass_mp2) {
605                                         passthrough = true;
606                                         encoding=OMX_AUDIO_CodingMP3;
607                                 } else {
608                                         passthrough = false;
609                                         encoding=OMX_AUDIO_CodingPCM;
610                                 }
611                         }
612                 }
613                         break;
614                 case MPTYPE_AAC_LATM: {
615                         if (prefered_aac == 2 && false) { //not supported yet
616
617                         } else {
618                                 Log::getInstance()->log("Audio", Log::DEBUG,
619                                                 "ChangeAudioPortConfig debug %d %d",prefered_aac,canpass_aac);
620                                 if (prefered_aac == 1 && canpass_aac) {
621                                         passthrough = true;
622                                         encoding=OMX_AUDIO_CodingAAC;
623                                 } else {
624                                         passthrough = false;
625                                         encoding=OMX_AUDIO_CodingPCM;
626                                 }
627                         }
628                 }
629                 break;
630                 case MPTYPE_AC3_PRE13:
631                 case MPTYPE_AC3: {
632                         if (prefered_ac3 == 2 && false) { //not supported yet
633
634                         } else {
635                                 Log::getInstance()->log("Audio", Log::DEBUG,
636                                                                                         "ChangeAudioPortConfig debug %d %d",prefered_ac3,canpass_ac3);
637                                 if (prefered_ac3 == 1 && canpass_ac3) {
638                                         passthrough = true;
639                                         encoding=OMX_AUDIO_CodingDDP;
640                                 } else {
641                                         passthrough = false;
642                                         encoding=OMX_AUDIO_CodingPCM;
643                                 }
644                         }
645                 }
646                         break;
647                 case MPTYPE_MPEG_AUDIO_LAYER3: {
648                         if (prefered_mp3 == 2 && false) { //not supported yet
649
650                         } else {
651                                 if (prefered_mp3 == 1 && canpass_mp2) {
652                                         passthrough = true;
653                                         encoding=OMX_AUDIO_CodingMP3;
654                                 } else {
655                                         passthrough = false;
656                                         encoding=OMX_AUDIO_CodingPCM;
657                                 }
658                         }
659                 }
660                         break;
661                 };
662         } else {
663                  passthrough=false;
664                  encoding=OMX_AUDIO_CodingPCM;
665                  //mch=false; // multichannel also false
666         }
667
668
669
670         /*OMX_CONFIG_BOOLEANTYPE booly;
671         memset(&booly, 0, sizeof(booly));
672         booly.nSize = sizeof(booly);
673         booly.nVersion.nVersion = OMX_VERSION;
674         if (passthrough)
675                 booly.bEnabled = OMX_TRUE;
676         else
677                 booly.bEnabled = OMX_FALSE;
678
679         error = OMX_SetParameter(omx_aud_dec, OMX_IndexParamBrcmDecoderPassThrough,
680                         &booly);
681         if (error != OMX_ErrorNone) {
682                 Log::getInstance()->log("Audio", Log::DEBUG,
683                                 "Init OMX_IndexParamBrcmDecoderPassThrough failed %x", error);
684                 DeAllocateCodecsOMX();
685                 return 0;
686         }*/
687         VideoOMX* video=(VideoOMX*)Video::getInstance();
688         if (disport) {
689                 video->DisablePort(omx_aud_rend,omx_rend_input_port,false);
690                 //DestroyInputBufsOMXwhilePlaying();
691                 //video->CommandFinished(omx_aud_rend,OMX_CommandPortDisable,omx_rend_input_port);
692
693         }
694
695
696         OMX_AUDIO_PARAM_PORTFORMATTYPE format;
697         memset(&format, 0, sizeof(format));
698         format.nSize = sizeof(format);
699         format.nVersion.nVersion = OMX_VERSION;
700         format.nPortIndex = omx_rend_input_port;
701         error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
702                         &format);
703         if (error != OMX_ErrorNone) {
704                 Log::getInstance()->log("Audio", Log::DEBUG,
705                                 "Get OMX_IndexParamAudioPortFormat failed %x %d", error,
706                                 omx_rend_input_port);
707                 return 0;
708         }
709         if (disport)
710
711                 Log::getInstance()->log("Audio", Log::DEBUG,
712                                 "Get OMX_IndexParamAudioPortFormat returned %d",format.eEncoding );
713         format.eEncoding = encoding;
714
715         error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
716                         &format);
717         if (error != OMX_ErrorNone) {
718                 Log::getInstance()->log("Audio", Log::DEBUG,
719                                 "Set OMX_IndexParamAudioPortFormat failed %x %d %d", error,
720                                 omx_rend_input_port,format.eEncoding );
721                 //return 0;
722         }
723
724         switch (encoding) {
725         case OMX_AUDIO_CodingPCM: {
726                 OMX_AUDIO_PARAM_PCMMODETYPE audio_pcm;
727                 memset(&audio_pcm, 0, sizeof(audio_pcm));
728                 audio_pcm.nSize = sizeof(audio_pcm);
729                 audio_pcm.nVersion.nVersion = OMX_VERSION;
730                 audio_pcm.nChannels = 2;
731                 audio_pcm.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
732                 audio_pcm.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
733                 //audio_pcm.eChannelMapping[2]=OMX_AUDIO_ChannelMax;
734                 audio_pcm.eNumData = OMX_NumericalDataSigned;
735                 audio_pcm.eEndian = OMX_EndianLittle;
736                 audio_pcm.bInterleaved = OMX_TRUE;
737                 audio_pcm.nBitPerSample = 16;
738                 audio_pcm.ePCMMode = OMX_AUDIO_PCMModeLinear;
739                 audio_pcm.nChannels = 2;
740                 audio_pcm.nSamplingRate = 48000;
741                 audio_pcm.nPortIndex = omx_rend_input_port;
742                 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPcm,
743                                 &audio_pcm);
744                 if (error != OMX_ErrorNone) {
745                         Log::getInstance()->log("Audio", Log::DEBUG,
746                                         "Init OMX_IndexParamAudioPcm failed %x %d", error,
747                                         omx_rend_input_port);
748                         return 0;
749                 }
750         } break;
751         case OMX_AUDIO_CodingDDP: {
752                 OMX_AUDIO_PARAM_DDPTYPE audio_ddp;
753                 memset(&audio_ddp, 0, sizeof(audio_ddp));
754                 audio_ddp.nSize = sizeof(audio_ddp);
755                 audio_ddp.nVersion.nVersion = OMX_VERSION;
756                 audio_ddp.nPortIndex = omx_rend_input_port;
757                 audio_ddp.nChannels = 8; //unknown
758                 audio_ddp.nBitRate=0;
759                 audio_ddp.nSampleRate=48000;
760                 audio_ddp.eChannelMapping[0] =OMX_AUDIO_ChannelLF;
761                 audio_ddp.eChannelMapping[1] =OMX_AUDIO_ChannelRF;
762                 audio_ddp.eChannelMapping[2] =OMX_AUDIO_ChannelCF;
763                 audio_ddp.eChannelMapping[3] =OMX_AUDIO_ChannelLFE;
764                 audio_ddp.eChannelMapping[4] =OMX_AUDIO_ChannelLR;
765                 audio_ddp.eChannelMapping[5] =OMX_AUDIO_ChannelRR;
766                 audio_ddp.eChannelMapping[6] =OMX_AUDIO_ChannelLS;
767                 audio_ddp.eChannelMapping[7] =OMX_AUDIO_ChannelRS;
768                 audio_ddp.eChannelMapping[8] =OMX_AUDIO_ChannelCS;
769                 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioDdp,
770                                 &audio_ddp);
771                 if (error != OMX_ErrorNone) {
772                         Log::getInstance()->log("Audio", Log::DEBUG,
773                                         "Init OMX_IndexParamAudioDdp failed %x %d", error,
774                                         omx_rend_input_port);
775                         return 0;
776                 }
777
778         } break;
779         default: break; //Make compiler happy
780         };
781
782
783
784         if (disport) {
785
786                 //PrepareInputBufsOMX(false);
787                 video->EnablePort(omx_aud_rend,omx_rend_input_port,false);
788         }
789
790
791
792         return 1;
793
794
795
796 }
797 int AudioOMX::InitDecoderLibAV()
798 {
799         libav_mutex.Lock();
800         ac3codec_context_libav = avcodec_alloc_context3(ac3codec_libav);
801         if (!ac3codec_context_libav) {
802                 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for ac3 decoding context failed!");
803                 return 0;
804         }
805
806         ac3codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
807         ac3codec_context_libav->request_channels=2;
808
809         int avc_ret = avcodec_open2(ac3codec_context_libav, ac3codec_libav, NULL);
810         if (avc_ret < 0) {
811                 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec  failed \n");
812                 libav_mutex.Unlock();
813                 return 0;
814         }
815
816         aaclatmcodec_context_libav = avcodec_alloc_context3(aaclatmcodec_libav);
817         if (!aaclatmcodec_context_libav) {
818                 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for aac decoding context failed!");
819                 return 0;
820         }
821
822         aaclatmcodec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
823         aaclatmcodec_context_libav->request_channels=2;
824
825         avc_ret = avcodec_open2(aaclatmcodec_context_libav, aaclatmcodec_libav, NULL);
826         if (avc_ret < 0) {
827                 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec  failed \n");
828                 libav_mutex.Unlock();
829                 return 0;
830         }
831
832
833         mp23codec_context_libav = avcodec_alloc_context3(mp23codec_libav);
834         if (!ac3codec_context_libav) {
835                 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for mp23 decoding context failed!");
836                 libav_mutex.Unlock();
837                 return 0;
838         }
839
840         mp23codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
841         mp23codec_context_libav->request_channels=2;
842
843         avc_ret = avcodec_open2(mp23codec_context_libav, mp23codec_libav, NULL);
844         if (avc_ret < 0) {
845                 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec  failed \n");
846                 libav_mutex.Unlock();
847                 return 0;
848         }
849 #ifdef USE_LIBRESAMPLE
850         resam_con_libav = avresample_alloc_context();
851         if (resam_con_libav == NULL) {
852                 Log::getInstance()->log("Audio", Log::DEBUG,
853                                 "Alloc resample context failed");
854                 return 0;
855         }
856
857         av_opt_set_int(resam_con_libav, "out_channel_layout", AV_CH_LAYOUT_STEREO,  0); // our standard format
858         av_opt_set_int(resam_con_libav, "out_sample_rate",48000,0);
859         av_opt_set_int(resam_con_libav, "out_sample_fmt",AV_SAMPLE_FMT_S16,0);
860
861         av_opt_set_int(resam_con_libav, "in_sample_rate",48000,0);
862         av_opt_set_int(resam_con_libav, "in_sample_fmt",AV_SAMPLE_FMT_S16,0);
863         av_opt_set_int(resam_con_libav, "in_channel_layout",  AV_CH_LAYOUT_5POINT1, 0); //just an example
864 #endif
865
866          av_init_packet(&incoming_paket_libav);
867          decode_frame_libav=avcodec_alloc_frame();
868          libav_mutex.Unlock();
869          decompress_buffer_filled=0;
870
871
872
873         return 1;
874 }
875
876 void AudioOMX::DeinitDecoderLibAV() {
877
878
879         libav_mutex.Lock();
880         if (ac3codec_context_libav) {
881                 avcodec_close(ac3codec_context_libav);
882                 av_free(ac3codec_context_libav);
883                 ac3codec_context_libav = NULL;
884
885                 avcodec_close(aaclatmcodec_context_libav);
886                 av_free(aaclatmcodec_context_libav);
887                 aaclatmcodec_context_libav = NULL;
888
889                 av_free(decode_frame_libav);
890
891                 avcodec_close(mp23codec_context_libav);
892                 av_free(mp23codec_context_libav);
893                 mp23codec_context_libav = NULL;
894 #ifdef USE_LIBRESAMPLE
895                 avresample_free(resam_con_libav);
896                 resam_con_libav=NULL;
897 #endif
898
899         }
900         libav_mutex.Unlock();
901
902 }
903
904
905 int AudioOMX::AllocateCodecsOMX()
906 {
907         OMX_ERRORTYPE error;
908         static OMX_CALLBACKTYPE callbacks= {&VideoOMX::EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
909
910         Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX");
911         //Clock, move later to audio
912         VideoOMX *video=(VideoOMX*)Video::getInstance();
913
914         if (!InitDecoderLibAV()) return 0;;
915
916
917         OMX_PORT_PARAM_TYPE p_param;
918         memset(&p_param,0,sizeof(p_param));
919         p_param.nSize=sizeof(p_param);
920         p_param.nVersion.nVersion=OMX_VERSION;
921
922
923         if (!video->getClockAudioandInit(&omx_clock,&omx_clock_output_port)){
924                 return 0;// get the clock and init it if necessary
925         }
926
927         /* TODO end */
928         if (!video->idleClock()) {
929                 return 0;
930         }
931         video->LockClock();
932
933         error = OMX_GetHandle(&omx_aud_rend, VPE_OMX_AUDIO_REND, NULL, &callbacks);
934         if (error != OMX_ErrorNone) {
935                 Log::getInstance()->log("Audio", Log::DEBUG,
936                                 "Init OMX audio rend failed %x", error);
937                 video->UnlockClock();
938                 DeAllocateCodecsOMX();
939                 return 0;
940         }
941
942         if (!ChangeAudioDestination()) {
943                 video->UnlockClock();
944                 DeAllocateCodecsOMX();
945                 return 0;
946         }
947
948         error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioInit, &p_param);
949         if (error != OMX_ErrorNone) {
950                 Log::getInstance()->log("Audio", Log::DEBUG,
951                                 "Init OMX audio rend OMX_GetParameter failed %x", error);
952                 video->UnlockClock();
953                 DeAllocateCodecsOMX();
954                 return 0;
955         }
956         omx_rend_input_port = p_param.nStartPortNumber;
957
958
959
960
961
962         error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamOtherInit, &p_param);
963         if (error != OMX_ErrorNone) {
964                 Log::getInstance()->log("Audio", Log::DEBUG,
965                                 "Init OMX aud rend OMX_GetParameter failed %x", error);
966                 video->UnlockClock();
967                 DeAllocateCodecsOMX();
968                 return 0;
969         }
970         // buggy return value
971         omx_rend_clock_port = p_param.nStartPortNumber;
972
973
974
975
976 /*      error=OMX_GetHandle(&omx_aud_dec,VPE_OMX_AUDIO_DECODER,NULL,&callbacks);
977
978         if (error!=OMX_ErrorNone){
979                 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder failed %x", error);
980                 video->UnlockClock();
981                 DeAllocateCodecsOMX();
982                 return 0;
983         }
984
985         error=OMX_GetParameter(omx_aud_dec,OMX_IndexParamAudioInit,&p_param);
986         if (error!=OMX_ErrorNone){
987                 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder OMX_GetParameter failed %x", error);
988                 video->UnlockClock();
989                 DeAllocateCodecsOMX();
990             return 0;
991         }
992         omx_codec_input_port=p_param.nStartPortNumber;
993         omx_codec_output_port=p_param.nStartPortNumber+1;
994
995         if (!video->DisablePort(omx_aud_dec,omx_codec_input_port) || !video->DisablePort(omx_aud_dec,omx_codec_output_port)) {
996                 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio decoder failed");
997                 video->UnlockClock();
998                 DeAllocateCodecsOMX();
999                 return 0;
1000         }*/
1001
1002
1003
1004
1005         if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true) ) {
1006                 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio rend failed %d",omx_rend_input_port);
1007                 video->UnlockClock();
1008                 DeAllocateCodecsOMX();
1009                 return 0;
1010         }
1011
1012         if ( !video->DisablePort(omx_aud_rend, omx_rend_clock_port, true)) {
1013                 Log::getInstance()->log("Audio", Log::DEBUG,
1014                                 "Disable Ports OMX rend clock port failed %d",omx_rend_clock_port);
1015                 video->UnlockClock();
1016                 DeAllocateCodecsOMX();
1017                 return 0;
1018         }
1019
1020
1021
1022
1023         //Setuo chain
1024
1025
1026         error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_aud_rend,omx_rend_clock_port);
1027         if (error!=OMX_ErrorNone){
1028                 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);
1029                 video->UnlockClock();
1030                 DeAllocateCodecsOMX();
1031                 return 0;
1032         }
1033
1034         if (!video->EnablePort(omx_clock,omx_clock_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_clock_port,false)
1035                                         ) {
1036                 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX clock rend failed");
1037                 video->UnlockClock();
1038                 DeAllocateCodecsOMX();
1039                 return 0;
1040         }
1041
1042         if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1043                 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend idle ChangeComponentState");
1044                 video->UnlockClock();
1045                 DeAllocateCodecsOMX();
1046                 return 0;
1047         }
1048
1049
1050
1051
1052         if ( !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_clock_port)) {
1053                 video->UnlockClock();
1054                 DeAllocateCodecsOMX();
1055                 return 0;
1056         }
1057
1058         if ( !video->CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1059                 video->UnlockClock();
1060                 DeAllocateCodecsOMX();
1061                 return 0;
1062         }
1063
1064
1065
1066         if (!ChangeAudioPortConfig(false)){
1067                     Log::getInstance()->log("Audio", Log::NOTICE, "Change AudioPortConfig failed");
1068                     video->UnlockClock();
1069                     DeAllocateCodecsOMX();
1070                         return 0;
1071         }
1072
1073 /*      if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1074                 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1075                 DeAllocateCodecsOMX();
1076                 return 0;
1077         }*/
1078
1079
1080
1081         if (!PrepareInputBufsOMX(true)) {
1082                 video->UnlockClock();
1083                 DeAllocateCodecsOMX();
1084                 return 0;
1085         }
1086
1087
1088
1089 /*      error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,omx_aud_rend,omx_rend_input_port);
1090         if (error!=OMX_ErrorNone){
1091                 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel dec to render failed %x", error);
1092                  video->UnlockClock();
1093                 DeAllocateCodecsOMX();
1094                 return 0;
1095         }*/
1096
1097
1098
1099 /*      if (!video->EnablePort(omx_aud_dec,omx_codec_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_input_port,false)
1100                                                 ) {
1101                 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX codec rend failed");
1102                  video->UnlockClock();
1103                 DeAllocateCodecsOMX();
1104                 return 0;
1105         }*/
1106
1107 /*      if ( !video->CommandFinished(omx_aud_dec,OMX_CommandPortEnable,omx_codec_output_port)
1108                         || !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1109                          video->UnlockClock();
1110                 DeAllocateCodecsOMX();
1111                 return 0;
1112         }*/
1113
1114         if (!video->ChangeComponentState(omx_aud_rend,OMX_StateExecuting)) {
1115                 Log::getInstance()->log("Audio", Log::DEBUG, "omx_aud_rend ChangeComponentState Execute");
1116                  video->UnlockClock();
1117                 DeAllocateCodecsOMX();
1118                 return 0;
1119         }
1120
1121
1122         video->UnlockClock();
1123         omx_running=true;
1124         setVolume(volume);
1125         doMuting();
1126         video->clockUnpause();
1127
1128
1129         if (!video->setClockExecutingandRunning()) return 0;
1130
1131         Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX finished");
1132
1133         return 1;
1134 }
1135
1136
1137
1138
1139 int AudioOMX::PrepareInputBufsOMX(bool setportdef) //needs to be called with locvke omx clock mutex
1140 {
1141         VideoOMX *video=(VideoOMX*)Video::getInstance();
1142         OMX_ERRORTYPE error;
1143         OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1144         memset(&port_def_type,0,sizeof(port_def_type));
1145         port_def_type.nSize=sizeof(port_def_type);
1146         port_def_type.nVersion.nVersion=OMX_VERSION;
1147         port_def_type.nPortIndex=omx_rend_input_port;//omx_codec_input_port;
1148
1149         error=OMX_GetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1150
1151         if (error!=OMX_ErrorNone){
1152                         Log::getInstance()->log("Audio", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1153         }
1154
1155
1156         if (setportdef) {
1157                 port_def_type.nBufferCountActual=2;
1158                 port_def_type.nBufferSize=max(port_def_type.nBufferSize,50000); // for transcoder important
1159
1160                 error=OMX_SetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
1161
1162                 if (error!=OMX_ErrorNone){
1163                         Log::getInstance()->log("Audio", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1164                 }
1165         }
1166
1167
1168         error=OMX_SendCommand(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port/*codec*/,0);
1169         if (error!=OMX_ErrorNone){
1170                 Log::getInstance()->log("Audio", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1171                 return 0;
1172         }
1173
1174         input_bufs_omx_mutex.Lock();
1175         for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1176                 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1177                 error=OMX_AllocateBuffer(omx_aud_rend/*dec*/,&buf_head,omx_rend_input_port/*codec*/,NULL,port_def_type.nBufferSize);
1178                 if (error!=OMX_ErrorNone){
1179                         Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1180                                 input_bufs_omx_mutex.Unlock();
1181                         return 0;
1182                 }
1183                 input_bufs_omx_all.push_back(buf_head);
1184                 input_bufs_omx_free.push_back(buf_head);
1185         }
1186         omx_first_frame=true;
1187
1188         firstsynched=false;
1189         cur_input_buf_omx=NULL;
1190         input_bufs_omx_mutex.Unlock();
1191
1192         if (!video->CommandFinished(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port /*codec*/)) {
1193                 return 0;
1194         }
1195
1196         return 1;
1197 }
1198
1199 int AudioOMX::DestroyInputBufsOMX() //call with clock mutex locked
1200 {
1201         OMX_ERRORTYPE error;
1202
1203         cur_input_buf_omx=NULL;
1204         input_bufs_omx_mutex.Lock();
1205         for (int i=0; i< input_bufs_omx_all.size();i++) {
1206                 error=OMX_FreeBuffer(omx_aud_rend/*dec*/,omx_rend_input_port/*codec*/,input_bufs_omx_all[i]);
1207                 if (error!=OMX_ErrorNone){
1208                         Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1209                         input_bufs_omx_mutex.Unlock();
1210                         return 0;
1211                 }
1212
1213         }
1214         input_bufs_omx_all.clear();
1215         input_bufs_omx_free.clear();
1216         input_bufs_omx_mutex.Unlock();
1217
1218 }
1219
1220 int AudioOMX::DestroyInputBufsOMXwhilePlaying() //call with clock mutex locked
1221 {
1222         OMX_ERRORTYPE error;
1223
1224         cur_input_buf_omx=NULL;
1225         input_bufs_omx_mutex.Lock();
1226         while (input_bufs_omx_all.size()>0) {
1227                 if (input_bufs_omx_free.size()>0) {
1228                         // Destroy one buffer
1229                         vector<OMX_BUFFERHEADERTYPE*>::iterator itty=input_bufs_omx_all.begin();
1230                         OMX_BUFFERHEADERTYPE* cur_buf=input_bufs_omx_free.front();
1231                         for (; itty!= input_bufs_omx_all.end();itty++) {
1232                                 if ((*itty)==cur_buf) {
1233                                         input_bufs_omx_all.erase(itty);
1234                                         input_bufs_omx_free.pop_front();
1235                                         break;
1236                                 }
1237                         }
1238                 } else {
1239                         input_bufs_omx_mutex.Unlock();
1240                         MILLISLEEP(5);
1241                         input_bufs_omx_mutex.Lock();
1242                 }
1243         }
1244
1245         Log::getInstance()->log("Audio", Log::DEBUG, "DestroyInputBufsOMXwhilePlaying %d %d", input_bufs_omx_all.size(),input_bufs_omx_free.size());
1246         input_bufs_omx_mutex.Unlock();
1247         return 1;
1248 }
1249
1250
1251 int AudioOMX::DeAllocateCodecsOMX()
1252 {
1253         OMX_ERRORTYPE error;
1254         omx_running=false;
1255         VideoOMX *video=(VideoOMX*)Video::getInstance();
1256          Log::getInstance()->log("Audio", Log::DEBUG, "enter deallocatecodecsomx");
1257
1258
1259
1260    Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 1");
1261    if (cur_input_buf_omx) {
1262                 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1263                 OMX_ERRORTYPE error=video->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,cur_input_buf_omx);
1264                 if (error!=OMX_ErrorNone) {
1265                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_EmptyThisBuffer 6 failed %x", error);
1266                 }
1267
1268                 cur_input_buf_omx=NULL;//write out old data
1269         }
1270    Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 2");
1271
1272    video->LockClock();
1273         if (omx_aud_rend/*dec*/) {
1274                 // first stop the omx elements
1275         /*      if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1276                         Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1277                 }*/
1278                  Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 3");
1279
1280                 video->UnlockClock();
1281                 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 4");
1282                 video->idleClock();
1283                 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 5");
1284                  video->LockClock();
1285
1286                 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1287                         Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend ChangeComponentState");
1288                 }
1289
1290         // TODO proper deinit sequence
1291                 // first flush all buffers
1292
1293                 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
1294                 if (error!=OMX_ErrorNone) {
1295                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend in failed %x", error);
1296
1297                 }
1298
1299         /*      error=OMX_SendCommand(omx_aud_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
1300                 if (error!=OMX_ErrorNone){
1301                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1302
1303                 }*/
1304
1305
1306         /*      if (!video->CommandFinished(omx_aud_dec,OMX_CommandFlush,omx_codec_input_port)) {
1307                         Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd codec input failed");
1308                 }*/
1309
1310
1311
1312                 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1313                 if (error!=OMX_ErrorNone){
1314                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1315
1316                 }
1317
1318                 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_clock_port, NULL);
1319                 if (error!=OMX_ErrorNone){
1320                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend clock failed %x", error);
1321
1322                 }
1323                 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6");
1324
1325                 if (!video->CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1326                         !video->CommandFinished(omx_aud_rend,OMX_CommandFlush,omx_rend_clock_port)) {
1327                                 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd clock shed failed");
1328                 }
1329
1330                 DestroyInputBufsOMX(); //We have to make sure that no buffers are in use
1331                 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6a");
1332                 DeinitDecoderLibAV();
1333                 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 7");
1334
1335                 //todo flushing
1336                 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true)) {
1337                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 1");
1338                 }
1339
1340         /*      if (!video->DisablePort(omx_aud_dec,omx_codec_output_port,true)) {
1341                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 6");
1342                 }
1343
1344                 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port,true)) {
1345                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 7");
1346                 }*/
1347
1348
1349                 if (!video->DisablePort(omx_aud_rend,omx_rend_clock_port,true)) {
1350                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 4");
1351                 }
1352
1353                 if (!video->DisablePort(omx_clock,omx_clock_output_port,true)) {
1354                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 5");
1355                 }
1356
1357
1358
1359         /*      error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,NULL,NULL);
1360                 if (error!=OMX_ErrorNone) {
1361                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1362
1363                 }*/
1364
1365
1366
1367                 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_input_port,NULL,NULL);
1368                 if (error!=OMX_ErrorNone) {
1369                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1370
1371                 }
1372
1373                 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1374                 if (error!=OMX_ErrorNone) {
1375                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1376
1377                 }
1378
1379                 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_clock_port,NULL,NULL);
1380                 if (error!=OMX_ErrorNone) {
1381                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1382
1383                 }
1384                 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 8");
1385
1386
1387                 //error=OMX_FreeHandle(omx_aud_dec);
1388                 error=OMX_FreeHandle(omx_aud_rend);
1389                 video->UnlockClock();
1390                 video->destroyClock();
1391                 omx_aud_rend/*dec*/=NULL;
1392                 if (error!=OMX_ErrorNone) {
1393                         Log::getInstance()->log("Audio", Log::DEBUG, "FreeHandle failed %d", error);
1394                 }
1395         } else  {
1396
1397                 video->UnlockClock();
1398                 DeinitDecoderLibAV();
1399         }
1400           Log::getInstance()->log("Audio", Log::DEBUG, "leave deallocate codecs OMX");
1401
1402         return 1;
1403 }
1404
1405
1406
1407 int AudioOMX::stop()
1408 {
1409   if (!initted) return 0;
1410
1411   Log::getInstance()->log("Audio", Log::DEBUG, "Audio stop called");
1412   DeAllocateCodecsOMX();
1413   //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1414   return 1;
1415 }
1416
1417 int AudioOMX::mute() {
1418         if (!initted)
1419                 return 0;
1420         Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
1421         VideoOMX *vw = (VideoOMX*) Video::getInstance();
1422     vw->LockClock();
1423         if (omx_running) {
1424
1425                 OMX_AUDIO_CONFIG_MUTETYPE amute;
1426                 memset(&amute, 0, sizeof(amute));
1427                 amute.nSize = sizeof(amute);
1428                 amute.nVersion.nVersion = OMX_VERSION;
1429                 amute.nPortIndex = omx_rend_input_port;
1430                 amute.bMute = OMX_TRUE;
1431                 OMX_ERRORTYPE error= OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1432                                                 &amute);
1433                 if (error != OMX_ErrorNone) {
1434                         Log::getInstance()->log("Audio", Log::DEBUG,
1435                                         "Set OMX_IndexConfigAudioMute failed %x %d", error,
1436                                         omx_rend_input_port);
1437                         vw->UnlockClock();
1438                         return 0;
1439                 }
1440
1441         }
1442         vw->UnlockClock();
1443
1444         muted = 1;
1445         return 1;
1446 }
1447
1448 int AudioOMX::unMute()
1449 {
1450   if (!initted) return 0;
1451
1452   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
1453   VideoOMX *vw = (VideoOMX*) Video::getInstance();
1454   vw->LockClock();
1455   if (omx_running) {
1456
1457                 OMX_AUDIO_CONFIG_MUTETYPE amute;
1458                 memset(&amute, 0, sizeof(amute));
1459                 amute.nSize = sizeof(amute);
1460                 amute.nVersion.nVersion = OMX_VERSION;
1461                 amute.nPortIndex = omx_rend_input_port;
1462                 amute.bMute = OMX_FALSE;
1463                 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1464                                                 &amute);
1465                 if (error != OMX_ErrorNone) {
1466                         Log::getInstance()->log("Audio", Log::DEBUG,
1467                                         "Set OMX_IndexConfigAudioMute failed %x %d", error,
1468                                         omx_rend_input_port);
1469                         vw->UnlockClock();
1470                         return 0;
1471                 }
1472         }
1473
1474         vw->UnlockClock();
1475
1476   muted = 0;
1477   return 1;
1478 }
1479
1480 int AudioOMX::pause() {
1481         if (!initted)
1482                 return 0;
1483
1484         VideoOMX *vw = (VideoOMX*) Video::getInstance();
1485         vw->clockPause();
1486
1487         return 1;
1488 }
1489
1490 int AudioOMX::unPause()
1491 {
1492   if (!initted) return 0;
1493
1494   VideoOMX *vw = (VideoOMX*) Video::getInstance();
1495
1496   vw->clockUnpause();
1497
1498   return 1;
1499 }
1500
1501 int AudioOMX::reset()
1502 {
1503   if (!initted) return 0;
1504 //test();
1505   Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
1506   DeAllocateCodecsOMX();
1507
1508 //  if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1509 //  Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
1510  // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
1511
1512   doMuting();
1513   return 1;
1514 }
1515
1516 int AudioOMX::setVolume(int tvolume)
1517 {
1518   // parameter: 0 for silence, 20 for full
1519   if ((tvolume < 0) || (tvolume > 20)) return 0;
1520
1521 // volume = 2 * (20 - volume);
1522 // Right, that one was rubbish... 0-10 were almost
1523 // inaudible, 11-20 did what should have been done
1524 // over the whole 0-20 range
1525   volume=tvolume;
1526
1527
1528   VideoOMX *vw = (VideoOMX*) Video::getInstance();
1529   vw->LockClock();
1530   if (omx_running) {
1531
1532           OMX_AUDIO_CONFIG_VOLUMETYPE avol;
1533           memset(&avol, 0, sizeof(avol));
1534           avol.nSize = sizeof(avol);
1535           avol.nVersion.nVersion = OMX_VERSION;
1536           avol.nPortIndex = omx_rend_input_port;
1537           avol.bLinear=OMX_FALSE;
1538           avol.sVolume.nValue =(volume-20)*200;
1539           OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioVolume,
1540                           &avol);
1541           if (error != OMX_ErrorNone) {
1542                   Log::getInstance()->log("Audio", Log::DEBUG,
1543                                   "Set OMX_IndexConfigAudioVolume failed %x %d", error,
1544                                   omx_rend_input_port);
1545                   vw->UnlockClock();
1546                   return 0;
1547           }
1548
1549   }
1550   vw->UnlockClock();
1551
1552   return 1;
1553 }
1554
1555 #ifdef DEV
1556 int AudioOMX::test()
1557 {
1558 //  ULLONG stc = 0;
1559 //  return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
1560
1561 /*  aud_sync_parms_t a;
1562   a.parm1 = 0;
1563   a.parm2 = 0;
1564 */
1565 //  int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
1566
1567
1568   /*OK*/ //printf("Audio sync disable = %i\n", b);
1569
1570   return 1;
1571
1572
1573 }
1574 #endif
1575
1576 unsigned int AudioOMX::AdvanceMpAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1577 {
1578         if (size<=2) return size; // silly;
1579         unsigned int test=0;
1580         *framesize=0;
1581         //inspired by libav parsing code
1582         while (test+1<size) {
1583                 if (data[test]==0xFF && (data[test+1] &0xe0)==0xe0) {
1584                         //sanity check inspired by libav
1585
1586
1587                         const int sample_rates[4]={44100,48000,32000,0};
1588                         const short bitrate_tab[2][3][15] = { { { 0, 32, 64, 96, 128, 160,
1589                                         192, 224, 256, 288, 320, 352, 384, 416, 448 }, { 0, 32, 48,
1590                                         56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 }, {
1591                                         0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
1592                                         256, 320 } }, { { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144,
1593                                         160, 176, 192, 224, 256 }, { 0, 8, 16, 24, 32, 40, 48, 56,
1594                                         64, 80, 96, 112, 128, 144, 160 }, { 0, 8, 16, 24, 32, 40,
1595                                         48, 56, 64, 80, 96, 112, 128, 144, 160 } } };
1596                         int lsf=0;
1597                         int mpeg2=0;
1598                         int layer=4-((data[test+1]&0x06)>>1);
1599                         if (layer==4) {test++;continue;} //sanity check
1600
1601                         int bitrate_index=(data[test+2]&0xf0)>>4;
1602                         if (bitrate_index==0x0f || bitrate_index==0x0) {test++;continue;} //sanity check
1603
1604                         int samplerate_index=(data[test+2]&0x0C)>>2;
1605                         if (samplerate_index==0x03) {test++;continue;} //sanity check
1606
1607                         int padding=(data[test+2]&2)>>1;
1608                         if (0x10 &  data[test+1]) {
1609                                 lsf=((data[test+1]) &0x8)?0:1;
1610                                 mpeg2=0;
1611                         } else {
1612                                 lsf=1;
1613                                 mpeg2=1;
1614                         }
1615                         int sample_rate=sample_rates[ samplerate_index]>>(lsf+mpeg2);
1616                         int frame_size=0;
1617                         int temp_frame_size=bitrate_tab[lsf][layer - 1][bitrate_index];
1618                         if (layer==1) {
1619                                 frame_size=(temp_frame_size*12000)/sample_rate;
1620                                 frame_size=(frame_size+padding)*4;
1621                         } else if (layer==2) {
1622                                 frame_size=(temp_frame_size*144000)/sample_rate;
1623                             frame_size=frame_size+padding;
1624                         } else {
1625                                 frame_size=(temp_frame_size*144000)/(sample_rate<<lsf);
1626                                 frame_size=frame_size+padding;
1627                         }
1628                         unsigned int sameheadertest=(data[test]<<24)|(data[test+1]<<16) |(data[test+2]<<8);
1629                         const unsigned mask=(0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)); // from libav
1630                         if (!mp3sameheader) {
1631                                 mp3sameheader=sameheadertest;
1632                                 mp3sameheadercount=2;
1633                         }
1634                         if ((mp3sameheader& mask)== (sameheadertest &mask)) mp3sameheadercount++;
1635                         else mp3sameheadercount=0;
1636                         mp3sameheader=sameheadertest;
1637
1638                         //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);
1639                         //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME DIAG: %x %x %x %d",data[test],data[test+1],data[test+2],mp3sameheadercount);
1640
1641                         if (mp3sameheadercount>4) {
1642                                 *framesize=frame_size;
1643                                 return test; // probably FrameSync
1644                         } //skip it if the header changes too frequently
1645                 }
1646                 test++;
1647         }
1648         return size;
1649 }
1650
1651 unsigned int AudioOMX::AdvanceAc3AudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1652 {
1653         if (size<=4) return size; // silly;
1654         const int frm_size_tab[] = { 64, 64, 80, 80, 96, 96, 112, 112, 128, 128,
1655                         160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384, 384, 448,
1656                         448, 512, 512, 640, 640, 768, 768, 896, 896, 1024, 1024, 1152,
1657                         1152, 1280, 1280, };
1658         unsigned int test=0;
1659         *framesize=20000; //if we do not find a start code do not decompress
1660         while (test+4<size) {
1661                 if (data[test]==0x0B && data[test+1]==0x77) {
1662                         // now figure out the length of the frame
1663                         unsigned char code=data[test+4];
1664                         unsigned char fscod=(code& 0xC0)>>6;
1665                         unsigned char frmsize=(code &0x3f);
1666                         if (fscod!=0) Log::getInstance()->log("Audio", Log::DEBUG, "warning we only support 48 KHz sampling rate");
1667                         *framesize=frm_size_tab[frmsize]*2;
1668                         return test; // probably FrameSync
1669                 }
1670                 test++;
1671         }
1672         return size;
1673 }
1674
1675 unsigned int AudioOMX::AdvanceAacLatmAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize)
1676 {
1677         if (size<=4) return size; // silly;
1678         unsigned int test=0;
1679         *framesize=20000; //if we do not find a start code do not decompress
1680         while (test+4<size) {
1681                 if (data[test] ==0x56 && (data[test+1]& 0xe0)==0xe0) {
1682                         // now figure out the length of the frame
1683                         unsigned int length= ((0x1f & data[test+1])<<8) | data[test+2];
1684                         *framesize=length+3;
1685                         return test; // probably FrameSync
1686                 }
1687                 test++;
1688         }
1689         return size;
1690 }
1691 #ifndef USE_LIBRESAMPLE
1692 #define SQRT3_2      1.22474487139158904909
1693
1694 void AudioOMX::getDownMixMatrix(unsigned long long channels,
1695                 int *left_mat,int *right_mat)
1696 {
1697         int i,j;
1698         double downmix_matrix[2][64];
1699         for (i=0;i<64;i++) {
1700                 for (j=0;j<2;j++) {
1701                         downmix_matrix[j][i]=0.;
1702                 }
1703         }
1704         downmix_matrix[0/*links*/][/*FRONT_LEFT*/0]=1.;
1705         downmix_matrix[1/*rechts*/][/*FRONT_RIGHT*/1]=1.;
1706         downmix_matrix[0/*links*/]  [/*FRONT_CENTER*/2] = M_SQRT1_2;
1707         downmix_matrix[1/*rechts*/] [/*FRONT_CENTER*/2] = M_SQRT1_2;
1708
1709         if (channels & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT) ) {
1710                 downmix_matrix[0/*links*/][/*BACK_CENTER*/8]=-M_SQRT1_2*M_SQRT1_2;
1711                 downmix_matrix[1/*rechts*/][/*BACK_CENTER*/8]=M_SQRT1_2*M_SQRT1_2;
1712         } else {
1713                 downmix_matrix[0/*links*/][/*BACK_CENTER*/8]=-M_SQRT1_2;
1714                 downmix_matrix[1/*rechts*/][/*BACK_CENTER*/8]=M_SQRT1_2;
1715         }
1716         downmix_matrix[0/*links*/][/*BACK_LEFT*/4 ]=-M_SQRT1_2*SQRT3_2;
1717     downmix_matrix[1/*rechts*/][/*BACK_LEFT*/4]=M_SQRT1_2*M_SQRT1_2;
1718     downmix_matrix[0/*links*/][/*BACK_RIGHT*/5 ]=-M_SQRT1_2*M_SQRT1_2;
1719     downmix_matrix[1/*rechts*/][/*BACK_RIGHT*/5]=M_SQRT1_2*SQRT3_2;
1720     downmix_matrix[0/*links*/][/*SIDE_LEFT*/9 ]=-M_SQRT1_2*SQRT3_2;
1721     downmix_matrix[1/*rechts*/][/*SIDE_LEFT*/9]=M_SQRT1_2*M_SQRT1_2;
1722     downmix_matrix[0/*links*/][/*SIDE_RIGHT*/10 ]=-M_SQRT1_2*M_SQRT1_2;
1723     downmix_matrix[1/*rechts*/][/*SIDE_RIGHT*/10]=M_SQRT1_2*SQRT3_2;
1724     downmix_matrix[0/*links*/][/*FRONT_LEFT_OF_CENTER*/6]=1.;
1725     downmix_matrix[1/*rechts*/][/*FRONT_RIGHT_OF_CENTER*/7]=1.;
1726     downmix_matrix[0/*links*/][/*LOW_FREQUENCY*/3]=M_SQRT1_2;
1727     downmix_matrix[1/*rechts*/][/*LOW_FREQUENCY*/3]=M_SQRT1_2;
1728
1729     for (i=0,j=0;i<64;i++) {
1730         if ((1ULL << i)& channels) {
1731                 left_mat[j]=(int)(downmix_matrix[0][i]*(1<<16));
1732                 right_mat[j]=(int)(downmix_matrix[1][i]*(1<<16));
1733                 j++;
1734         }
1735     }
1736 }
1737
1738 #endif
1739
1740 void AudioOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1741 {
1742   packet = mplist.front();
1743 }
1744
1745 UINT AudioOMX::DeliverMediaSample(UCHAR* buffer, UINT* samplepos) {
1746         DeliverMediaPacket(packet, buffer, samplepos);
1747         if (*samplepos == packet.length) {
1748                 *samplepos = 0;
1749                 return 1;
1750         } else
1751                 return 0;
1752 }
1753
1754
1755 long long AudioOMX::correctAudioLatency(long long pts,int addsamples,int srate) {
1756
1757         VideoOMX *video = (VideoOMX*) Video::getInstance();
1758         video->LockClock();
1759         OMX_PARAM_U32TYPE audio_lat;
1760         OMX_ERRORTYPE error;
1761         memset(&audio_lat, 0, sizeof(audio_lat));
1762         audio_lat.nSize = sizeof(audio_lat);
1763         audio_lat.nVersion.nVersion = OMX_VERSION;
1764         audio_lat.nPortIndex = omx_rend_input_port;
1765
1766         error = OMX_GetConfig(omx_aud_rend, OMX_IndexConfigAudioRenderingLatency,
1767                         &audio_lat);
1768          video->UnlockClock();
1769         if (error != OMX_ErrorNone) {
1770                 Log::getInstance()->log("Audio", Log::DEBUG,
1771                                 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
1772                                 omx_rend_input_port);
1773                 return pts; // no correction in case of error
1774         }
1775         /*Log::getInstance()->log("Audio", Log::DEBUG, "Current audio latency %d",
1776                         audio_lat.nU32);*/
1777
1778         long long workpts=0;
1779         workpts+=addsamples;
1780         workpts-=audio_lat.nU32;
1781         workpts*=10LL*1000LL*1000LL;
1782         workpts=workpts/((long long)srate); //one second /samplerate
1783         workpts+=pts;
1784
1785         return workpts;
1786 }
1787
1788 bool AudioOMX::DrainTargetBufferFull()
1789 {
1790         //Check, if we have OMX output buffers
1791         bool full;
1792         input_bufs_omx_mutex.Lock();
1793         full=(input_bufs_omx_free.size()==0);
1794         input_bufs_omx_mutex.Unlock();
1795         return full;
1796
1797 }
1798
1799
1800 UINT AudioOMX::DeliverMediaPacket(MediaPacket packet, const UCHAR* buffer,
1801                 UINT *samplepos) {
1802         /*First Check, if we have an audio sample*/
1803         VideoOMX *vw = (VideoOMX*) Video::getInstance();
1804         bool achange=false;
1805         OMX_ERRORTYPE error;
1806         Log *logger=Log::getInstance();
1807         if (vw->InIframemode()) {
1808                 samplepos = 0;
1809                 MILLISLEEP(10);
1810                 return 0; //Not in iframe mode!
1811         }
1812
1813         if (!omx_running) return 0; // if we are not runnig do not do this
1814         if (vw->isClockPaused()) return 0; //Block if we pause
1815         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 1");
1816
1817         //Log::getInstance()->log("Audio", Log::DEBUG, "DeliverMediaPacketOMX time %lld",packet.presentation_time);
1818
1819 /*      if (packet.synched && packet.presentation_time <= 0) {
1820                 *samplepos = packet.length;
1821                 firstsynched = false;
1822                 lsync=true;
1823                 Log::getInstance()->log("Audio", Log::DEBUG,
1824                                 "DeliverMediaPacketOMX Frameskip");
1825                 return packet.length;
1826         }*/
1827
1828         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 2");
1829
1830         UINT headerstrip = 0;
1831         if (packet.disconti) {
1832                 firstsynched = false;
1833                 decompress_buffer_filled=0;
1834                 if (cur_input_buf_omx) {
1835                         OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1836                                         cur_input_buf_omx);
1837                         if (error != OMX_ErrorNone) {
1838                                 Log::getInstance()->log("Audio", Log::DEBUG,
1839                                                 "OMX_EmptyThisBuffer 1 failed %x", error);
1840                         }
1841                         cur_input_buf_omx = NULL;
1842                 }
1843                 lsync=true;
1844         }
1845         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 3");
1846         if (packet.type != lastAType) {//Format Change //Push data out !
1847                 firstsynched = false;
1848                 achange=true;
1849                 mp3sameheader=0;
1850                 Log::getInstance()->log("Audio", Log::DEBUG,"Notice audio type change %d %d", packet.type,lastAType);
1851                 lastAType = packet.type;
1852                 decompress_buffer_filled=0;
1853
1854                 if (cur_input_buf_omx) {
1855                         OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1856                                         cur_input_buf_omx);
1857                         if (error != OMX_ErrorNone) {
1858                                 Log::getInstance()->log("Audio", Log::DEBUG,
1859                                                 "OMX_EmptyThisBuffer 2 failed %x", error);
1860                         }
1861                         cur_input_buf_omx = NULL;
1862                 }
1863
1864                 int oldcancelstate;
1865                 int oldcanceltype;
1866                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
1867                 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
1868                 vw->LockClock();
1869                 if (!ChangeAudioPortConfig(true)) {
1870                         Log::getInstance()->log("Audio", Log::DEBUG,
1871                                         "Changing audio port config failed", error);
1872                 }
1873                 vw->UnlockClock();
1874                 pthread_setcancelstate(oldcancelstate, NULL);
1875                 pthread_setcanceltype(oldcanceltype, NULL);
1876                 lsync=true;
1877
1878         }
1879         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 4");
1880
1881         /*Inspect PES-Header */
1882         if (*samplepos == 0 && packet.type != MPTYPE_MPEG_AUDIO_LAYER3) {//stripheader
1883                 headerstrip = buffer[packet.pos_buffer + 8] + 9;
1884                 if (packet.type == MPTYPE_AC3)
1885                         headerstrip += 4; //skip ac3 bytes
1886                 *samplepos += headerstrip;
1887                 if (packet.synched) {
1888                         if (cur_input_buf_omx) {
1889                                 //cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1890                                 OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1891                                                 cur_input_buf_omx);
1892                                 if (error != OMX_ErrorNone) {
1893                                         Log::getInstance()->log("Audio", Log::DEBUG,
1894                                                         "OMX_EmptyThisBuffer 3 failed %x", error);
1895                                 }
1896                                  //vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1897
1898                                 cur_input_buf_omx = NULL;//write out old data
1899                         }
1900                         firstsynched = true;
1901                         //decompress_buffer_filled=0;
1902                 } else {
1903                         if (!firstsynched) {//
1904                                 *samplepos = packet.length;//if we have not processed at least one
1905                                 decompress_buffer_filled=0;
1906                                 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 5");
1907                                 return packet.length;//synched packet ignore it!
1908                         }
1909                 }
1910         }
1911         if (!cur_input_buf_omx) {
1912                 input_bufs_omx_mutex.Lock();
1913                 if (input_bufs_omx_free.size()==0) {
1914                         input_bufs_omx_mutex.Unlock();
1915                         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 6");
1916                         //Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample");
1917                         return 0; // we do not have a free media sample
1918
1919                 }
1920                 cur_input_buf_omx=input_bufs_omx_free.front();
1921                 cur_input_buf_omx->nFilledLen=0;
1922                 cur_input_buf_omx->nOffset=0;
1923                 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
1924                 input_bufs_omx_free.pop_front();
1925                 input_bufs_omx_mutex.Unlock();
1926         }
1927         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 7");
1928
1929         if (cur_input_buf_omx->nFilledLen == 0) {//will only be changed on first packet
1930                 if (packet.synched) {
1931                         //Log::getInstance()->log("Audio", Log::DEBUG,
1932                         //              "packet synched marker");
1933
1934                         //lastreftimePTS=packet.pts;
1935                         if (omx_first_frame) { // TODO time
1936                                 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
1937                                 Log::getInstance()->log("Audio", Log::DEBUG, "Starttime");
1938                                 omx_first_frame = false;
1939                         } else {
1940                                 cur_input_buf_omx->nFlags = 0;
1941                                 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
1942                         }
1943                         lastreftimeOMX = packet.presentation_time;
1944                         //Log::getInstance()->log("Audio", Log::DEBUG,
1945                                 //      "Time code %lld pts %lld dts %lld", lastreftimeOMX, packet.pts,packet.dts);
1946                         lastreftimePTS = packet.pts;
1947                         cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
1948                 } else {
1949                 //      Log::getInstance()->log("Audio", Log::DEBUG,
1950                         //                                      "packet NOT synched marker");
1951                         cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
1952                         cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
1953
1954                 }
1955                 if (packet.disconti || achange) {
1956                         cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_DISCONTINUITY;
1957                         //mp23codec_context_libav->frame_size=-1;
1958                         //ac3codec_context_libav->frame_size=-1;
1959                 }
1960
1961         }
1962         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 8");
1963
1964         if (*samplepos>packet.length) *samplepos=0; //propably the thread got interrupted and sample is not valid any more!
1965         unsigned int haveToCopy=packet.length-*samplepos;
1966
1967         if (passthrough) {
1968                 while (haveToCopy>0) {
1969                         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 9");
1970                         if (lsync) {
1971                                 unsigned int gotframesize=0;
1972                                 int adv=0;
1973                                 switch (packet.type) {
1974                                 case MPTYPE_MPEG_AUDIO:
1975                                 case MPTYPE_MPEG_AUDIO_LAYER3: {
1976                                         adv = AdvanceMpAudioSync(buffer+packet.pos_buffer+*samplepos,
1977                                                         haveToCopy,&gotframesize);
1978                                 }
1979                                 break;
1980                                 case MPTYPE_AC3:
1981                                 case MPTYPE_AC3_PRE13: {
1982                                         adv = AdvanceAc3AudioSync(buffer+packet.pos_buffer+*samplepos,
1983                                                         haveToCopy,&gotframesize);
1984                                 }
1985                                 break;
1986
1987                                 case MPTYPE_AAC_LATM: {
1988                                         adv = AdvanceAacLatmAudioSync(buffer+packet.pos_buffer+*samplepos,
1989                                                         haveToCopy,&gotframesize);
1990                                 }
1991                                 break;
1992                                 };
1993                                 if (adv!=haveToCopy) {
1994                                         lsync=false;
1995                                         haveToCopy-=adv;
1996                                         *samplepos+=adv;
1997                                 } else {
1998                                         *samplepos=packet.length; //no ac3 sync byte
1999                                         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 10");
2000                                         return packet.length;
2001                                 }
2002                         }
2003                         // so everything is fine now do a memcpy
2004                         int cancopy=min(haveToCopy,cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen);
2005                         memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
2006                         haveToCopy-=cancopy;
2007                         cur_input_buf_omx->nFilledLen+=cancopy;
2008                         *samplepos+=cancopy;
2009                         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 11");
2010
2011                         error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2012                         if (error != OMX_ErrorNone) {
2013                                 Log::getInstance()->log("Audio", Log::DEBUG,
2014                                                 "OMX_EmptyThisBuffer 5 failed %x", error);
2015                         }
2016                         cur_input_buf_omx=NULL;
2017                         if (haveToCopy>0) {
2018                                 // get5 new buffer
2019                                 input_bufs_omx_mutex.Lock();
2020                                 if (input_bufs_omx_free.size()==0) {
2021                                         input_bufs_omx_mutex.Unlock();
2022                                         //      Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample2");
2023                                         return *samplepos; // we do not have a free media sample
2024                                 }
2025                                 cur_input_buf_omx=input_bufs_omx_free.front();
2026                                 cur_input_buf_omx->nFilledLen=0;
2027                                 cur_input_buf_omx->nOffset=0;
2028                                 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2029                                 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
2030                                 input_bufs_omx_free.pop_front();
2031                                 input_bufs_omx_mutex.Unlock();
2032                         }
2033                         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 12");
2034
2035                 }
2036                 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 13");
2037
2038         } else {
2039                 int len;
2040                 int gotta;
2041                 int framesize=0;
2042                 int errcount=0;
2043
2044                 AVCodecContext *current_context;
2045                 switch (packet.type) {
2046                 case MPTYPE_MPEG_AUDIO:
2047                 case MPTYPE_MPEG_AUDIO_LAYER3: {
2048                         current_context = mp23codec_context_libav;
2049                         if (current_context->frame_size<0) framesize=1152; //Maximum framesize
2050                         else framesize=current_context->frame_size;
2051                 }break;
2052                 case MPTYPE_AAC_LATM: {
2053                         current_context = aaclatmcodec_context_libav;
2054                 } break;
2055                 case MPTYPE_AC3:
2056                 case MPTYPE_AC3_PRE13: {
2057                         current_context = ac3codec_context_libav;
2058                 }break;
2059                 };
2060
2061                 if (decompress_buffer_filled) { // have a remaining paket
2062                         incoming_paket_libav.data =(uint8_t*) decompress_buffer;
2063                         memcpy(decompress_buffer+decompress_buffer_filled,
2064                                         buffer+packet.pos_buffer+*samplepos,
2065                                         min(haveToCopy,decompress_buffer_size-decompress_buffer_filled));
2066                         incoming_paket_libav.size = decompress_buffer_filled
2067                                         +min(haveToCopy,decompress_buffer_size-decompress_buffer_filled);
2068                         //Log::getInstance()->log("Audio", Log::DEBUG,"Use saved audio buffer %d %d %d",packet.type,decompress_buffer_filled,packet.synched);
2069                 } else {
2070                         incoming_paket_libav.data =(uint8_t*) buffer+packet.pos_buffer+*samplepos;
2071                         incoming_paket_libav.size = haveToCopy;
2072                 }
2073                 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 18");
2074
2075                 while (haveToCopy> 0 && errcount<3) {
2076
2077                         //Log::getInstance()->log("Audio", Log::DEBUG,"libav in %d %d",framesize,current_context->frame_size);
2078                         //Log::getInstance()->log("Audio", Log::DEBUG, "libav in %d %d",
2079                                 //      framesize, current_context->frame_size);
2080
2081                         bool donotdecompress=false;
2082                         unsigned int gotframesize=0;
2083                 //      if (!decompress_buffer_filled) { // only do this if no old data is present
2084                                 int adv = 0;
2085                                 switch (packet.type) {
2086                                 case MPTYPE_MPEG_AUDIO:
2087                                 case MPTYPE_MPEG_AUDIO_LAYER3: {
2088                                         adv = AdvanceMpAudioSync(incoming_paket_libav.data,
2089                                                         incoming_paket_libav.size,&gotframesize);
2090                                 }
2091                                 break;
2092                                 case MPTYPE_AC3:
2093                                 case MPTYPE_AC3_PRE13: {
2094                                         adv = AdvanceAc3AudioSync(incoming_paket_libav.data,
2095                                                         incoming_paket_libav.size,&gotframesize);
2096                                 }
2097                                 break;
2098                                 case MPTYPE_AAC_LATM: {
2099                                         adv = AdvanceAacLatmAudioSync(incoming_paket_libav.data,
2100                                                         incoming_paket_libav.size,&gotframesize);
2101                                 }
2102                                 break;
2103                                 };
2104                                 if (adv > 0 /*&& !decompress_buffer_filled*/) {
2105                                         incoming_paket_libav.data += adv;
2106                                         incoming_paket_libav.size-=adv;
2107                                         haveToCopy -= adv;
2108                                         *samplepos += adv;
2109                                         /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2110                                         else*/ framesize=haveToCopy;
2111                                         //Log::getInstance()->log("Audio", Log::DEBUG,"Advance by %d %d from %d",adv,packet.type,*samplepos );
2112                                         if (haveToCopy <= 0) {
2113                                                 //       Log::getInstance()->log("Audio", Log::DEBUG,"No sync code in packet remove %d",packet.type);
2114                                                 *samplepos=packet.length;
2115                                                 return packet.length;
2116                                         }
2117
2118
2119                         //      } //
2120
2121                                 if (gotframesize>0 && gotframesize>haveToCopy) {
2122                                         donotdecompress=true;
2123                                         errcount=100; // exit loop
2124                                 }
2125                                 //      else Log::getInstance()->log("Audio", Log::DEBUG,"Loop run" );
2126                         }
2127                         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 19");
2128                         if (!donotdecompress) {
2129                                 int oldcancelstate;
2130                                 int oldcanceltype;
2131                                 pthread_testcancel();
2132                                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2133                                 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2134                                 libav_mutex.Lock();
2135                                 if (!omx_running || !mp23codec_context_libav
2136                                                 || !ac3codec_context_libav) {
2137                                         libav_mutex.Unlock();
2138                                         return *samplepos;
2139                                 }
2140                                 libav_mutex.Unlock();
2141                                 //      Log::getInstance()->log("Audio", Log::DEBUG,"libav out");
2142                                 int save_size=incoming_paket_libav.size;
2143
2144                                 if (gotframesize<=incoming_paket_libav.size) {
2145                                         if (gotframesize>0) incoming_paket_libav.size=gotframesize;
2146                                         len = avcodec_decode_audio4(current_context, decode_frame_libav,
2147                                                 &gotta, &incoming_paket_libav);
2148                                 } else {
2149                                         //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:E %d %d",gotframesize,incoming_paket_libav.size);
2150                                         gotta=0;
2151                                         len=0;
2152                                 }
2153                                 //Log::getInstance()->log("Audio", Log::DEBUG, "FRAME:T %d",len);
2154                                 incoming_paket_libav.size=save_size;
2155                                 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out1");
2156                                 pthread_setcancelstate(oldcancelstate, NULL);
2157                                 pthread_setcanceltype(oldcanceltype, NULL);
2158                                 pthread_testcancel();
2159
2160                         } else {
2161                                 gotta=0;
2162                                 len=0;
2163                         }
2164                         //Log::getInstance()->log("Audio", Log::DEBUG, "libav out2");
2165                         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 20");
2166                         if (!omx_running) {
2167                                 libav_mutex.Unlock();
2168                                 return *samplepos;
2169                         }
2170
2171                         if (decompress_buffer_filled) { // reset to normal decoding
2172                                 if (len>0) {
2173                                         //Log::getInstance()->log("Audio", Log::DEBUG,"saved audio: %d",len);
2174                                         haveToCopy -= min(len-decompress_buffer_filled,0);
2175                                         *samplepos += min(len-decompress_buffer_filled,0);
2176                                         //if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2177                                         /*else*/ framesize=haveToCopy;
2178                                 } else {
2179                                         framesize=haveToCopy;
2180                                 }
2181                                 incoming_paket_libav.data =(uint8_t*) buffer+packet.pos_buffer+*samplepos;
2182                                 errcount=0;
2183                                 decompress_buffer_filled=0;
2184                         } else {
2185
2186                                 if (len>0) {
2187                                         incoming_paket_libav.data += len;
2188                                         haveToCopy -= len;
2189                                         *samplepos += len;
2190                                         errcount=0;
2191                                         /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
2192                                         else*/framesize=haveToCopy;
2193                                 } else {
2194                                         errcount++;
2195                                         framesize=haveToCopy;
2196                                 }
2197                         }
2198                         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 21");
2199
2200                         incoming_paket_libav.size =framesize;
2201                         if (gotta) {
2202                                 //Log::getInstance()->log("Audio", Log::DEBUG,
2203                                 //                                      "Got a frame");
2204
2205                                 int dsize = av_samples_get_buffer_size(NULL,
2206                                                 /*current_context->channels*/2, decode_frame_libav->nb_samples,
2207                                                 current_context->sample_fmt, 1);
2208                                 int dsize_in = av_samples_get_buffer_size(NULL,
2209                                                                                 current_context->channels, decode_frame_libav->nb_samples,
2210                                                                                 current_context->sample_fmt, 1);
2211                                 //if (current_context->channels==1) dsize*=2; // we convert mono to stereo
2212                                 if ((cur_input_buf_omx->nFilledLen + dsize)
2213                                                 > cur_input_buf_omx->nAllocLen ) {
2214                                         // I doubt that this will ever happen
2215                                 //      Log::getInstance()->log("Audio", Log::DEBUG,
2216                                         //                                      "P 2 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
2217                                         OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
2218                                                         cur_input_buf_omx);
2219                                         if (error != OMX_ErrorNone) {
2220                                                 Log::getInstance()->log("Audio", Log::DEBUG,
2221                                                                 "OMX_EmptyThisBuffer 4 failed %x", error);
2222                                         }
2223                                         cur_input_buf_omx = NULL;
2224
2225                                         if (!cur_input_buf_omx) {
2226                                                 int count = 0;
2227                                                 while (count < 10 && omx_running) {
2228                                                         count++;
2229                                                         input_bufs_omx_mutex.Lock();
2230                                                         if (input_bufs_omx_free.size() == 0) {
2231                                                                 input_bufs_omx_mutex.Unlock();
2232                                                         //      Log::getInstance()->log("Audio", Log::DEBUG,
2233                                                                 //              "Deliver MediaPacket no free sample");
2234                                                                 MILLISLEEP(5);
2235                                                                 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark22");
2236                                                                 if (!omx_running) return *samplepos;
2237                                                                 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 23");
2238                                                                 continue;
2239                                                         }
2240                                                         cur_input_buf_omx = input_bufs_omx_free.front();
2241                                                         cur_input_buf_omx->nFilledLen = 0;
2242                                                         cur_input_buf_omx->nOffset = 0;
2243                                                         cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
2244                                                         cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2245                                                         input_bufs_omx_free.pop_front();
2246                                                         input_bufs_omx_mutex.Unlock();
2247                                                         break;
2248                                                 }
2249                                                 if (!cur_input_buf_omx) return *samplepos;
2250                                         }
2251
2252                                 }
2253
2254                                 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy in %d %d %d" ,dsize,current_context->sample_rate,cur_input_buf_omx->nFilledLen);
2255                                 if (current_context->channels==2) {
2256                                         memcpy(cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen,
2257                                                         decode_frame_libav->data[0], dsize);
2258                                 }
2259 #ifndef USE_LIBRESAMPLE
2260                                 else if (current_context->channels==1) { //convert to stereo
2261                                         short* startbuffer=(short* )decode_frame_libav->data[0];
2262                                         short* endbuffer=(short* )(decode_frame_libav->data[0]+dsize/2);
2263                                         short* destbuffer=(short* )(cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen);
2264                                         while (startbuffer!=endbuffer) {
2265                                                 unsigned short temp=*startbuffer;
2266                                                 *destbuffer=temp;
2267                                                 destbuffer++;
2268                                                 *destbuffer=temp;
2269                                                 destbuffer++;
2270                                                 startbuffer++;
2271                                         }
2272                                 } else {
2273                                         unsigned int channels=current_context->channels;
2274                                         int left_mat[channels];
2275                                         int right_mat[channels];
2276                                         getDownMixMatrix(current_context->channel_layout,left_mat,right_mat);
2277
2278                         short* startbuffer=(short* )decode_frame_libav->data[0];
2279                                         short* endbuffer=(short* )(decode_frame_libav->data[0]+dsize_in);
2280                                         short* destbuffer=(short* )(cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen);
2281                                         while (startbuffer!=endbuffer) {
2282                                                 short* cur_buf=startbuffer;
2283                                                 short* cur_buf_end=startbuffer+channels;
2284                                                 int work1=0.;
2285                                                 int work2=0.;
2286                                                 int *mat1=left_mat;
2287                                                 int *mat2=right_mat;
2288                                                 while (cur_buf!=cur_buf_end) {
2289                                                         work1+= ((*mat1)*(*cur_buf));
2290                                                         work2+= ((*mat2)*(*cur_buf));
2291                                                         cur_buf++;
2292                                                         mat1++;
2293                                                         mat2++;
2294                                                 }
2295                                                 *destbuffer=work1>>16;
2296                                                 destbuffer++;
2297                                                 *destbuffer=work2>>16;
2298                                                 destbuffer++;
2299                                                 startbuffer+=channels;
2300
2301                                         }
2302
2303                                 }
2304 #else
2305                                 else {
2306                                         //untested for future use
2307                                         av_opt_set_int(resam_con_libav, "in_sample_rate",current_context->sample_rate,0);
2308                                         av_opt_set_int(resam_con_libav, "in_sample_fmt",current_context->sample_fmt,0);
2309                                         av_opt_set_int(resam_con_libav, "in_channel_layout",current_context->channel_layout, 0);
2310                                         int ret=avresample_open(resam_con_libav);
2311                                         if (ret<0) {
2312                                                 Log::getInstance()->log("Audio", Log::ERR,"Opening AV resample failed %d",ret);
2313                                         } else {
2314                                                 avresample_convert(resam_con_libav, cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen,
2315                                                                 dsize, decode_frame_libav->nb_samples,
2316                                                                 decode_frame_libav->data[0],  dsize_in,  decode_frame_libav->nb_samples);
2317                                                 avresample_close(resam_con_libav);
2318                                         }
2319
2320                                 }
2321 #endif
2322
2323                                 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy out");
2324                                 cur_input_buf_omx->nFilledLen += dsize;
2325                         } else {
2326                                 //Log::getInstance()->log("Audio", Log::DEBUG,"Incomplete mpeg frames in pes packet %d %d",incoming_paket_libav.size,packet.length);
2327                         /*      uint8_t a1=incoming_paket_libav.data[0];
2328                                 uint8_t a2=incoming_paket_libav.data[1];
2329                                 uint8_t a3=incoming_paket_libav.data[2];
2330                                 uint8_t a4=incoming_paket_libav.data[3];*/
2331                         //      Log::getInstance()->log("Audio", Log::DEBUG,"Header %x %x %x %x",a1,a2,
2332                                 //              a3,a4);
2333                         }
2334
2335                 }
2336                 //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 24");
2337                 decompress_buffer_filled=0;
2338                 if (haveToCopy ) {
2339                         //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);
2340                         memcpy(decompress_buffer,incoming_paket_libav.data,min(haveToCopy,decompress_buffer_size));
2341
2342                         decompress_buffer_filled=min(haveToCopy,decompress_buffer_size);
2343
2344                 }
2345
2346                 if (cur_input_buf_omx->nFilledLen) {
2347                         //Log::getInstance()->log("Audio", Log::DEBUG,
2348                         //                                                              "P 3 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
2349                         error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
2350                         if (error != OMX_ErrorNone) {
2351                                 Log::getInstance()->log("Audio", Log::DEBUG,
2352                                                 "OMX_EmptyThisBuffer 5 failed %x", error);
2353                         }
2354                         //if (packet.synched) vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
2355                         cur_input_buf_omx = NULL;
2356                 }
2357         }
2358
2359
2360         //Log::getInstance()->log("Audio", Log::DEBUG, "DMP mark 25");
2361
2362         *samplepos=packet.length;
2363         return packet.length;
2364
2365 }
2366
2367
2368
2369 long long AudioOMX::SetStartOffset(long long curreftime, bool *rsync){
2370   VideoOMX *vw=(VideoOMX*)Video::getInstance();
2371   return vw->SetStartAudioOffset(curreftime,rsync);
2372 }
2373
2374 void AudioOMX::ResetTimeOffsets() {
2375   VideoOMX *vw=(VideoOMX*)Video::getInstance();
2376   vw->ResetTimeOffsets();
2377 }
2378
2379