]> git.vomp.tv Git - vompclient-marten.git/blob - audioomx.cc
Preparations for AC3 passthrough and parser for deinterlacing
[vompclient-marten.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 AudioOMX::AudioOMX()
28 {
29   initted = 0;
30   streamType = 0;
31   volume = 20;
32   muted = 0;
33   lastAType = MPTYPE_MPEG_AUDIO;
34
35   canpass_ac3=false;
36   canpass_mp2=false;
37   canpass_mp3=false;
38   canpass_pcm_mch=false;
39
40   prefered_ac3=0; //0 stereo PCM, 1 passthrough 2 Multichannel PCM
41   prefered_mp2=0;
42   prefered_mp3=0;
43   hdmi=true;
44
45   omx_running=false;
46
47   omx_aud_rend/*dec*/=0;
48   cur_input_buf_omx=NULL;
49
50   ac3codec_libav=NULL;
51   ac3codec_context_libav=NULL;
52
53   mp23codec_libav=NULL;
54   mp23codec_context_libav=NULL;
55
56   decompress_buffer=NULL;
57   decompress_buffer_size=0;
58   decompress_buffer_filled=0;
59
60 }
61
62 AudioOMX::~AudioOMX()
63 {
64 }
65
66 int AudioOMX::init(UCHAR tstreamType) {
67         if (initted)
68                 return 0;
69         initted = 1;
70
71         streamType = tstreamType;
72
73         if (!initAllParams()) {
74                 shutdown();
75                 return 0;
76         }
77
78         unMute();
79
80         decompress_buffer_size=20000;
81         decompress_buffer=(UCHAR*)malloc(decompress_buffer_size);
82         decompress_buffer_filled=0;
83
84
85
86         av_register_all();
87         av_log_set_flags(AV_LOG_SKIP_REPEATED);
88
89         ac3codec_libav = avcodec_find_decoder(CODEC_ID_AC3);
90         if (ac3codec_libav == NULL) {
91                 Log::getInstance()->log("Audio", Log::DEBUG,
92                                 "Find libav ac3 decoder failed");
93                 return 0;
94         }
95
96         mp23codec_libav = avcodec_find_decoder(CODEC_ID_MP3);
97         if (mp23codec_libav == NULL) {
98                 Log::getInstance()->log("Audio", Log::DEBUG,
99                                 "Find libav mpeg audio decoder failed");
100                 return 0;
101         }
102
103         return 1;
104 }
105
106 int AudioOMX::initAllParams()
107 {
108   return (setStreamType(streamType) && setChannel() && setSource());
109 }
110
111 int AudioOMX::shutdown()
112 {
113   if (!initted) return 0;
114   initted = 0;
115
116   Log::getInstance()->log("Audio", Log::DEBUG, "audio shutdown called");
117   DeAllocateCodecsOMX();
118
119   free(decompress_buffer);
120   decompress_buffer=NULL;
121   decompress_buffer_size=0;
122   decompress_buffer_filled=0;
123
124   return 1;
125 }
126
127 bool AudioOMX::loadOptionsfromServer(VDR* vdr)
128 {
129           Log::getInstance()->log("Audio", Log::DEBUG, "AudioOMX config load");
130     char *name=vdr->configLoad("AudioOMX","AC3DecodingMode");
131
132     if (name != NULL) {
133                 if (STRCASECMP(name, "PCM") == 0) {
134                         prefered_ac3 = 0;
135                 } else if (STRCASECMP(name, "Passthrough") == 0) {
136                         prefered_ac3 = 1;
137                 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
138                         prefered_ac3 = 2;
139                 }
140         }
141
142     name = vdr->configLoad("AudioOMX", "Mp2DecodingMode");
143
144         if (name != NULL) {
145                 if (STRCASECMP(name, "PCM") == 0) {
146                         prefered_mp2 = 0;
147                 } else if (STRCASECMP(name, "Passthrough") == 0) {
148                         prefered_mp2 = 1;
149                 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
150                         prefered_mp2 = 2;
151                 }
152         }
153
154         name = vdr->configLoad("AudioOMX", "Mp3DecodingMode");
155
156         if (name != NULL) {
157                 if (STRCASECMP(name, "PCM") == 0) {
158                         prefered_mp3 = 0;
159                 } else if (STRCASECMP(name, "Passthrough") == 0) {
160                         prefered_mp3 = 1;
161                 } else if (STRCASECMP(name, "PCMMultichannel") == 0) {
162                         prefered_mp3 = 2;
163                 }
164         }
165
166         name = vdr->configLoad("AudioOMX", "AudioOutput");
167
168         if (name != NULL) {
169                 if (STRCASECMP(name, "analog") == 0) {
170                         hdmi = false;
171                 } else if (STRCASECMP(name, "HDMI") == 0) {
172                         hdmi = true;
173                 }
174         }
175
176
177    return true;
178
179 }
180
181 bool AudioOMX::handleOptionChanges(Option* option)
182 {
183     if (Audio::handleOptionChanges(option))
184                 return true;
185         switch (option->id) {
186         case 4: {
187                 if (STRCASECMP(option->options[option->userSetChoice], "analog") == 0) {
188                         hdmi = false;
189                 } else if (STRCASECMP(option->options[option->userSetChoice], "HDMI")
190                                 == 0) {
191                         hdmi = true;
192                 }
193                 return true;
194         }
195                 break;
196         case 1: {
197                 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
198                         prefered_ac3 = 0;
199                 } else if (STRCASECMP(option->options[option->userSetChoice],
200                                 "Passthrough") == 0) {
201                         prefered_ac3 = 1;
202                 } else if (STRCASECMP(option->options[option->userSetChoice],
203                                 "PCMMultichannel") == 0) {
204                         prefered_ac3 = 2;
205                 }
206         }
207                 break;
208         case 2: {
209                 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
210                         prefered_mp2 = 0;
211                 } else if (STRCASECMP(option->options[option->userSetChoice],
212                                 "Passthrough") == 0) {
213                         prefered_mp2 = 1;
214                 } else if (STRCASECMP(option->options[option->userSetChoice],
215                                 "PCMMultichannel") == 0) {
216                         prefered_mp2 = 2;
217                 }
218         }
219                 break;
220         case 3: {
221                 if (STRCASECMP(option->options[option->userSetChoice], "PCM") == 0) {
222                         prefered_mp3 = 0;
223                 } else if (STRCASECMP(option->options[option->userSetChoice],
224                                 "Passthrough") == 0) {
225                         prefered_mp3 = 1;
226                 } else if (STRCASECMP(option->options[option->userSetChoice],
227                                 "PCMMultichannel") == 0) {
228                         prefered_mp3 = 2;
229                 }
230         }
231                 break;
232         };
233         return false;
234
235 }
236
237 bool AudioOMX::saveOptionstoServer()
238 {
239
240     switch (prefered_ac3) {
241         case 0:
242                 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode", "PCM");
243                 break;
244         case 1:
245                 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
246                                 "Passthrough");
247                 break;
248         case 2:
249                 VDR::getInstance()->configSave("AudioOMX", "AC3DecodingMode",
250                                 "PCMMultichannel");
251                 break;
252         };
253
254         switch (prefered_mp2) {
255         case 0:
256                 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode", "PCM");
257                 break;
258         case 1:
259                 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
260                                 "Passthrough");
261                 break;
262         case 2:
263                 VDR::getInstance()->configSave("AudioOMX", "Mp2DecodingMode",
264                                 "PCMMultichannel");
265                 break;
266         };
267
268         switch (prefered_mp3) {
269         case 0:
270                 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode", "PCM");
271                 break;
272         case 1:
273                 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
274                                 "Passthrough");
275                 break;
276         case 2:
277                 VDR::getInstance()->configSave("AudioOMX", "Mp3DecodingMode",
278                                 "PCMMultichannel");
279                 break;
280         };
281
282         if (!hdmi)
283                 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "analog");
284         else
285                 VDR::getInstance()->configSave("AudioOMX", "AudioOutput", "HDMI");
286
287
288     return true;
289 }
290
291 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
292            UINT numChoices, UINT defaultChoice, UINT startInt,
293            const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
294
295 bool AudioOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
296 {
297     if (!Audio::addOptionsToPanes(panenumber,options,pane)) return false;
298
299
300     Option* option;
301     if (panenumber == 2)
302     {
303
304         static const char* audioopts[]={"analog","HDMI"};
305         option = new Option(4,tr("Audio Output"), "AudioOMX","AudioOutput",Option::TYPE_TEXT,2,0,0,audioopts,NULL,false,this);
306         options->push_back(option);
307         pane->addOptionLine(option);
308
309
310         char **ac3opts=new char *[3];
311         int i=0;
312         ac3opts[i]=new char[strlen("PCM")+1];
313         strcpy(ac3opts[i],"PCM");
314         i++;
315         if (canpass_ac3) {
316                 ac3opts[i]=new char[strlen("Passthrough")+1];
317             strcpy(ac3opts[i],"PassThrough");
318             i++;
319         }
320         if (canpass_pcm_mch) {
321                 ac3opts[i]=new char[strlen("PCMMultichannel")+1];
322             strcpy(ac3opts[i],"PCMMultichannel");
323             i++;
324         }
325         option = new Option(1 ,tr("AC3 HDMI Mode"), "AudioOMX", "AC3DecodingMode", Option::TYPE_TEXT, i, 0, 0, ac3opts,NULL,true, this);
326         options->push_back(option);
327         pane->addOptionLine(option);
328
329         char **mp2opts = new char *[3];
330                 i = 0;
331                 mp2opts[i] = new char[strlen("PCM") + 1];
332                 strcpy(mp2opts[i], "PCM");
333                 i++;
334                 if (canpass_mp2) {
335                         mp2opts[i] = new char[strlen("Passthrough") + 1];
336                         strcpy(mp2opts[i], "PassThrough");
337                         i++;
338                 }
339                 if (canpass_pcm_mch) {
340                         mp2opts[i] = new char[strlen("PCMMultichannel") + 1];
341                         strcpy(mp2opts[i], "PCMMultichannel");
342                         i++;
343                 }
344                 option = new Option(2, tr("Mp2 HDMI Mode"), "AudioOMX",
345                                 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0,
346                                 mp2opts, NULL, true, this);
347                 options->push_back(option);
348                 pane->addOptionLine(option);
349
350                 char **mp3opts = new char *[3];
351                 i = 0;
352                 mp3opts[i] = new char[strlen("PCM") + 1];
353                 strcpy(mp3opts[i], "PCM");
354                 i++;
355                 if (canpass_mp3) {
356                         mp3opts[i] = new char[strlen("Passthrough") + 1];
357                         strcpy(mp3opts[i], "PassThrough");
358                         i++;
359                 }
360                 if (canpass_pcm_mch) {
361                         mp3opts[i] = new char[strlen("PCMMultichannel") + 1];
362                         strcpy(mp3opts[i], "PCMMultichannel");
363                         i++;
364                 }
365                 option = new Option(3, tr("Mp3 HDMI Mode"), "AudioOMX",
366                                 "Mp2DecodingMode", Option::TYPE_TEXT, i, 0, 0, mp3opts,
367                                 NULL, true, this);
368                 options->push_back(option);
369                 pane->addOptionLine(option);
370
371
372     }
373
374     return true;
375 }
376
377
378
379
380
381
382 OMX_ERRORTYPE AudioOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
383
384         //Log::getInstance()->log("Audio", Log::NOTICE, "EmptyBufferDone");
385         AudioOMX *audio=(AudioOMX *)getInstance();
386         audio->ReturnEmptyOMXBuffer(buffer);
387         return OMX_ErrorNone;
388
389 }
390
391 void AudioOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
392         input_bufs_omx_mutex.Lock();
393         //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
394         input_bufs_omx_free.push_back(buffer);
395         //Log::getInstance()->log("Audio", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
396         input_bufs_omx_mutex.Unlock();
397 }
398
399  OMX_ERRORTYPE AudioOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
400          Log::getInstance()->log("Audio", Log::NOTICE, "FillBufferDone");
401         return OMX_ErrorNone;
402 }
403
404
405
406 int AudioOMX::setStreamType(UCHAR type)
407 {
408   if (!initted) return 0;
409
410  // if (ioctl(fdAudio, AV_SET_AUD_STREAMTYPE, type) != 0) return 0;
411   return 1;
412 }
413
414 int AudioOMX::setChannel()
415 {
416   if (!initted) return 0;
417
418  // if (ioctl(fdAudio, AV_SET_AUD_CHANNEL, 0) != 0) return 0;
419   return 1;
420 }
421
422 int AudioOMX::setSource()
423 {
424   if (!initted) return 0;
425
426  // if (ioctl(fdAudio, AV_SET_AUD_SRC, 1) != 0) return 0;
427   return 1;
428 }
429
430 int AudioOMX::sync()
431 {
432   if (!initted) return 0;
433
434  // if (ioctl(fdAudio, AV_SET_AUD_SYNC, 2) != 0) return 0;
435   return 1;
436 }
437
438 int AudioOMX::play() {
439         if (!initted)
440                 return 0;
441         lastAType=MPTYPE_MPEG_AUDIO;
442         Log::getInstance()->log("Audio", Log::DEBUG, "enter play");
443
444         if (!AllocateCodecsOMX()) {
445                 return 0;
446         }
447         return 1;
448 }
449
450
451 int AudioOMX::ChangeAudioDestination() //clock aka omx mutex needs to be locked
452 {
453         OMX_ERRORTYPE error;
454         const char * destinations[]={"local","hdmi"};
455         int dest=0;
456         if (hdmi) dest=1;
457         else dest=0;
458
459         OMX_CONFIG_BRCMAUDIODESTINATIONTYPE auddest;
460         memset(&auddest,0,sizeof(auddest));
461         auddest.nSize=sizeof(auddest);
462         auddest.nVersion.nVersion=OMX_VERSION;
463         strcpy((char *)auddest.sName, destinations[dest]);
464
465         Log::getInstance()->log("Audio", Log::DEBUG, "setting destination to: %s",auddest.sName);
466         error=OMX_SetConfig(omx_aud_rend,OMX_IndexConfigBrcmAudioDestination,&auddest);
467         if (error!=OMX_ErrorNone){
468                 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX_IndexConfigBrcmAudioDestination failed %x %x %s", error,omx_aud_rend,auddest.sName);
469                 DeAllocateCodecsOMX();
470                 return 0;
471         }
472         return 1;
473
474
475 }
476
477 int AudioOMX::ChangeAudioPortConfig(bool disport) //clock aka omx mutex needs to be locked
478 {
479         OMX_ERRORTYPE error;
480         //Ok first fidle a working configuration
481         Log::getInstance()->log("Audio", Log::DEBUG,
482                                                         "ChangeAudioPortConfig");
483
484         OMX_AUDIO_CODINGTYPE encoding;
485         if (hdmi) {
486                 switch (lastAType) {
487                 case MPTYPE_MPEG_AUDIO: {
488                         if (prefered_mp2 == 3 && false) { //not supported yet
489
490                         } else {
491                                 if (prefered_mp2 == 2 && canpass_mp2) {
492                                         passthrough = true;
493                                         encoding=OMX_AUDIO_CodingMP3;
494                                 } else {
495                                         passthrough = false;
496                                         encoding=OMX_AUDIO_CodingPCM;
497                                 }
498                         }
499                 }
500                         break;
501                 case MPTYPE_AC3_PRE13:
502                 case MPTYPE_AC3: {
503                         if (prefered_ac3 == 3 && false) { //not supported yet
504
505                         } else {
506                                 if (prefered_ac3 == 2 && canpass_ac3) {
507                                         passthrough = true;
508                                         encoding=OMX_AUDIO_CodingDDP;
509                                 } else {
510                                         passthrough = false;
511                                         encoding=OMX_AUDIO_CodingPCM;
512                                 }
513                         }
514                 }
515                         break;
516                 case MPTYPE_MPEG_AUDIO_LAYER3: {
517                         if (prefered_mp3 == 3 && false) { //not supported yet
518
519                         } else {
520                                 if (prefered_mp3 == 2 && canpass_mp2) {
521                                         passthrough = true;
522                                         encoding=OMX_AUDIO_CodingMP3;
523                                 } else {
524                                         passthrough = false;
525                                         encoding=OMX_AUDIO_CodingPCM;
526                                 }
527                         }
528                 }
529                         break;
530                 };
531         } else {
532                  passthrough=false;
533                  encoding=OMX_AUDIO_CodingPCM;
534                  //mch=false; // multichannel also false
535         }
536
537
538
539         /*OMX_CONFIG_BOOLEANTYPE booly;
540         memset(&booly, 0, sizeof(booly));
541         booly.nSize = sizeof(booly);
542         booly.nVersion.nVersion = OMX_VERSION;
543         if (passthrough)
544                 booly.bEnabled = OMX_TRUE;
545         else
546                 booly.bEnabled = OMX_FALSE;
547
548         error = OMX_SetParameter(omx_aud_dec, OMX_IndexParamBrcmDecoderPassThrough,
549                         &booly);
550         if (error != OMX_ErrorNone) {
551                 Log::getInstance()->log("Audio", Log::DEBUG,
552                                 "Init OMX_IndexParamBrcmDecoderPassThrough failed %x", error);
553                 DeAllocateCodecsOMX();
554                 return 0;
555         }*/
556
557
558
559         if (!omx_running) {
560                 OMX_AUDIO_PARAM_PORTFORMATTYPE format;
561                 memset(&format, 0, sizeof(format));
562                 format.nSize = sizeof(format);
563                 format.nVersion.nVersion = OMX_VERSION;
564                 format.nPortIndex = omx_rend_input_port;
565                 error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
566                                 &format);
567                 if (error != OMX_ErrorNone) {
568                         Log::getInstance()->log("Audio", Log::DEBUG,
569                                         "Get OMX_IndexParamAudioPortFormat failed %x %d", error,
570                                         omx_rend_input_port);
571                         return 0;
572                 }
573
574                 format.eEncoding = encoding;
575
576                 error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPortFormat,
577                                 &format);
578                 if (error != OMX_ErrorNone) {
579                         Log::getInstance()->log("Audio", Log::DEBUG,
580                                         "Set OMX_IndexParamAudioPortFormat failed %x %d", error,
581                                         omx_rend_input_port);
582                         return 0;
583                 }
584
585                 switch (encoding) {
586                 case OMX_AUDIO_CodingPCM: {
587                         OMX_AUDIO_PARAM_PCMMODETYPE audio_pcm;
588                         memset(&audio_pcm, 0, sizeof(audio_pcm));
589                         audio_pcm.nSize = sizeof(audio_pcm);
590                         audio_pcm.nVersion.nVersion = OMX_VERSION;
591                         audio_pcm.nChannels = 2;
592                         audio_pcm.eChannelMapping[0] = OMX_AUDIO_ChannelLF;
593                         audio_pcm.eChannelMapping[1] = OMX_AUDIO_ChannelRF;
594                         //audio_pcm.eChannelMapping[2]=OMX_AUDIO_ChannelMax;
595                         audio_pcm.eNumData = OMX_NumericalDataSigned;
596                         audio_pcm.eEndian = OMX_EndianLittle;
597                         audio_pcm.bInterleaved = OMX_TRUE;
598                         audio_pcm.nBitPerSample = 16;
599                         audio_pcm.ePCMMode = OMX_AUDIO_PCMModeLinear;
600                         audio_pcm.nChannels = 2;
601                         audio_pcm.nSamplingRate = 48000;
602                         audio_pcm.nPortIndex = omx_rend_input_port;
603                         error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioPcm,
604                                         &audio_pcm);
605                         if (error != OMX_ErrorNone) {
606                                 Log::getInstance()->log("Audio", Log::DEBUG,
607                                                 "Init OMX_IndexParamAudioPcm failed %x %d", error,
608                                                 omx_rend_input_port);
609                                 return 0;
610                         }
611                 } break;
612                 case OMX_AUDIO_CodingDDP: {
613                         OMX_AUDIO_PARAM_DDPTYPE audio_ddp;
614                         memset(&audio_ddp, 0, sizeof(audio_ddp));
615                         audio_ddp.nSize = sizeof(audio_ddp);
616                         audio_ddp.nVersion.nVersion = OMX_VERSION;
617                         audio_ddp.nPortIndex = omx_rend_input_port;
618                         audio_ddp.nChannels = 8; //unknown
619                         audio_ddp.nBitRate=0;
620                         audio_ddp.nSampleRate=48000;
621                         audio_ddp.eChannelMapping[0] =OMX_AUDIO_ChannelLF;
622                         audio_ddp.eChannelMapping[1] =OMX_AUDIO_ChannelRF;
623                         audio_ddp.eChannelMapping[2] =OMX_AUDIO_ChannelCF;
624                         audio_ddp.eChannelMapping[3] =OMX_AUDIO_ChannelLFE;
625                         audio_ddp.eChannelMapping[4] =OMX_AUDIO_ChannelLR;
626                         audio_ddp.eChannelMapping[5] =OMX_AUDIO_ChannelRR;
627                         audio_ddp.eChannelMapping[6] =OMX_AUDIO_ChannelLS;
628                         audio_ddp.eChannelMapping[7] =OMX_AUDIO_ChannelRS;
629                         audio_ddp.eChannelMapping[8] =OMX_AUDIO_ChannelCS;
630                         error = OMX_SetParameter(omx_aud_rend, OMX_IndexParamAudioDdp,
631                                         &audio_ddp);
632                         if (error != OMX_ErrorNone) {
633                                 Log::getInstance()->log("Audio", Log::DEBUG,
634                                                 "Init OMX_IndexParamAudioDdp failed %x %d", error,
635                                                 omx_rend_input_port);
636                                 return 0;
637                         }
638
639                 } break;
640                 };
641
642
643         }
644
645
646
647         return 1;
648
649
650
651 }
652 int AudioOMX::InitDecoderLibAV()
653 {
654         libav_mutex.Lock();
655         ac3codec_context_libav = avcodec_alloc_context3(ac3codec_libav);
656         if (!ac3codec_context_libav) {
657                 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for ac3 decoding context failed!");
658                 return 0;
659         }
660
661         ac3codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
662         ac3codec_context_libav->request_channels=2;
663
664         int avc_ret = avcodec_open2(ac3codec_context_libav, ac3codec_libav, NULL);
665         if (avc_ret < 0) {
666                 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec  failed \n");
667                 libav_mutex.Unlock();
668                 return 0;
669         }
670
671         mp23codec_context_libav = avcodec_alloc_context3(mp23codec_libav);
672         if (!ac3codec_context_libav) {
673                 Log::getInstance()->log("Audio", Log::DEBUG, "Alloc avcodec for mp23 decoding context failed!");
674                 libav_mutex.Unlock();
675                 return 0;
676         }
677
678         mp23codec_context_libav->flags |= CODEC_FLAG_TRUNCATED;
679         mp23codec_context_libav->request_channels=2;
680
681         avc_ret = avcodec_open2(mp23codec_context_libav, mp23codec_libav, NULL);
682         if (avc_ret < 0) {
683                 Log::getInstance()->log("Audio", Log::DEBUG, "Opening libav codec  failed \n");
684                 libav_mutex.Unlock();
685                 return 0;
686         }
687
688          av_init_packet(&incoming_paket_libav);
689          decode_frame_libav=avcodec_alloc_frame();
690          libav_mutex.Unlock();
691          decompress_buffer_filled=0;
692
693
694
695         return 1;
696 }
697
698 void AudioOMX::DeinitDecoderLibAV() {
699
700
701         libav_mutex.Lock();
702         if (ac3codec_context_libav) {
703                 avcodec_close(ac3codec_context_libav);
704                 av_free(ac3codec_context_libav);
705                 ac3codec_context_libav = NULL;
706                 av_free(decode_frame_libav);
707                 avcodec_close(mp23codec_context_libav);
708                 av_free(mp23codec_context_libav);
709                 mp23codec_context_libav = NULL;
710
711         }
712         libav_mutex.Unlock();
713
714 }
715
716
717 int AudioOMX::AllocateCodecsOMX()
718 {
719         OMX_ERRORTYPE error;
720         static OMX_CALLBACKTYPE callbacks= {&VideoOMX::EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
721
722         Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX");
723         //Clock, move later to audio
724         VideoOMX *video=(VideoOMX*)Video::getInstance();
725
726         if (!InitDecoderLibAV()) return 0;;
727
728
729         OMX_PORT_PARAM_TYPE p_param;
730         memset(&p_param,0,sizeof(p_param));
731         p_param.nSize=sizeof(p_param);
732         p_param.nVersion.nVersion=OMX_VERSION;
733
734
735         if (!video->getClockAudioandInit(&omx_clock,&omx_clock_output_port)){
736                 return 0;// get the clock and init it if necessary
737         }
738
739         /* TODO end */
740         if (!video->idleClock()) {
741                 return 0;
742         }
743         video->LockClock();
744
745         error = OMX_GetHandle(&omx_aud_rend, VPE_OMX_AUDIO_REND, NULL, &callbacks);
746         if (error != OMX_ErrorNone) {
747                 Log::getInstance()->log("Audio", Log::DEBUG,
748                                 "Init OMX audio rend failed %x", error);
749                 video->UnlockClock();
750                 DeAllocateCodecsOMX();
751                 return 0;
752         }
753
754         if (!ChangeAudioDestination()) {
755                 video->UnlockClock();
756                 DeAllocateCodecsOMX();
757                 return 0;
758         }
759
760         error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamAudioInit, &p_param);
761         if (error != OMX_ErrorNone) {
762                 Log::getInstance()->log("Audio", Log::DEBUG,
763                                 "Init OMX audio rend OMX_GetParameter failed %x", error);
764                 video->UnlockClock();
765                 DeAllocateCodecsOMX();
766                 return 0;
767         }
768         omx_rend_input_port = p_param.nStartPortNumber;
769
770         error = OMX_GetParameter(omx_aud_rend, OMX_IndexParamOtherInit, &p_param);
771         if (error != OMX_ErrorNone) {
772                 Log::getInstance()->log("Audio", Log::DEBUG,
773                                 "Init OMX aud rend OMX_GetParameter failed %x", error);
774                 video->UnlockClock();
775                 DeAllocateCodecsOMX();
776                 return 0;
777         }
778         // buggy return value
779         omx_rend_clock_port = p_param.nStartPortNumber;
780
781
782 /*      error=OMX_GetHandle(&omx_aud_dec,VPE_OMX_AUDIO_DECODER,NULL,&callbacks);
783
784         if (error!=OMX_ErrorNone){
785                 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder failed %x", error);
786                 video->UnlockClock();
787                 DeAllocateCodecsOMX();
788                 return 0;
789         }
790
791         error=OMX_GetParameter(omx_aud_dec,OMX_IndexParamAudioInit,&p_param);
792         if (error!=OMX_ErrorNone){
793                 Log::getInstance()->log("Audio", Log::DEBUG, "Init OMX audio decoder OMX_GetParameter failed %x", error);
794                 video->UnlockClock();
795                 DeAllocateCodecsOMX();
796             return 0;
797         }
798         omx_codec_input_port=p_param.nStartPortNumber;
799         omx_codec_output_port=p_param.nStartPortNumber+1;
800
801         if (!video->DisablePort(omx_aud_dec,omx_codec_input_port) || !video->DisablePort(omx_aud_dec,omx_codec_output_port)) {
802                 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio decoder failed");
803                 video->UnlockClock();
804                 DeAllocateCodecsOMX();
805                 return 0;
806         }*/
807
808
809
810
811         if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true) ) {
812                 Log::getInstance()->log("Audio", Log::DEBUG, "Disable Ports OMX audio rend failed %d",omx_rend_input_port);
813                 video->UnlockClock();
814                 DeAllocateCodecsOMX();
815                 return 0;
816         }
817
818         if ( !video->DisablePort(omx_aud_rend, omx_rend_clock_port, true)) {
819                 Log::getInstance()->log("Audio", Log::DEBUG,
820                                 "Disable Ports OMX rend clock port failed %d",omx_rend_clock_port);
821                 video->UnlockClock();
822                 DeAllocateCodecsOMX();
823                 return 0;
824         }
825
826
827
828
829         //Setuo chain
830
831
832         error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_aud_rend,omx_rend_clock_port);
833         if (error!=OMX_ErrorNone){
834                 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);
835                 video->UnlockClock();
836                 DeAllocateCodecsOMX();
837                 return 0;
838         }
839
840         if (!video->EnablePort(omx_clock,omx_clock_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_clock_port,false)
841                                         ) {
842                 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX clock rend failed");
843                 video->UnlockClock();
844                 DeAllocateCodecsOMX();
845                 return 0;
846         }
847
848         if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
849                 Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend idle ChangeComponentState");
850                 video->UnlockClock();
851                 DeAllocateCodecsOMX();
852                 return 0;
853         }
854
855
856
857
858         if ( !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_clock_port)) {
859                 video->UnlockClock();
860                 DeAllocateCodecsOMX();
861                 return 0;
862         }
863
864         if ( !video->CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
865                 video->UnlockClock();
866                 DeAllocateCodecsOMX();
867                 return 0;
868         }
869
870
871
872         if (!ChangeAudioPortConfig(false)){
873                     Log::getInstance()->log("Audio", Log::NOTICE, "Change AudioPortConfig failed");
874                     video->UnlockClock();
875                     DeAllocateCodecsOMX();
876                         return 0;
877         }
878
879 /*      if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
880                 Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
881                 DeAllocateCodecsOMX();
882                 return 0;
883         }*/
884
885
886
887         if (!PrepareInputBufsOMX()) {
888                 video->UnlockClock();
889                 DeAllocateCodecsOMX();
890                 return 0;
891         }
892
893
894
895 /*      error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,omx_aud_rend,omx_rend_input_port);
896         if (error!=OMX_ErrorNone){
897                 Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel dec to render failed %x", error);
898                  video->UnlockClock();
899                 DeAllocateCodecsOMX();
900                 return 0;
901         }*/
902
903
904
905 /*      if (!video->EnablePort(omx_aud_dec,omx_codec_output_port,false) || !video->EnablePort(omx_aud_rend,omx_rend_input_port,false)
906                                                 ) {
907                 Log::getInstance()->log("Audio", Log::DEBUG, "Enable Ports OMX codec rend failed");
908                  video->UnlockClock();
909                 DeAllocateCodecsOMX();
910                 return 0;
911         }*/
912
913 /*      if ( !video->CommandFinished(omx_aud_dec,OMX_CommandPortEnable,omx_codec_output_port)
914                         || !video->CommandFinished(omx_aud_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
915                          video->UnlockClock();
916                 DeAllocateCodecsOMX();
917                 return 0;
918         }*/
919
920         if (!video->ChangeComponentState(omx_aud_rend,OMX_StateExecuting)) {
921                 Log::getInstance()->log("Audio", Log::DEBUG, "omx_aud_rend ChangeComponentState Execute");
922                  video->UnlockClock();
923                 DeAllocateCodecsOMX();
924                 return 0;
925         }
926
927
928         video->UnlockClock();
929         paused=false;
930         omx_running=true;
931         setVolume(volume);
932
933
934         if (!video->setClockExecutingandRunning()) return 0;
935
936         Log::getInstance()->log("Audio", Log::NOTICE, "Allocate Codecs OMX finished");
937
938         return 1;
939 }
940
941
942
943
944 int AudioOMX::PrepareInputBufsOMX() //needs to be called with locvke omx clock mutex
945 {
946         VideoOMX *video=(VideoOMX*)Video::getInstance();
947         OMX_ERRORTYPE error;
948         OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
949         memset(&port_def_type,0,sizeof(port_def_type));
950         port_def_type.nSize=sizeof(port_def_type);
951         port_def_type.nVersion.nVersion=OMX_VERSION;
952         port_def_type.nPortIndex=omx_rend_input_port;//omx_codec_input_port;
953
954         error=OMX_GetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
955
956         if (error!=OMX_ErrorNone){
957                         Log::getInstance()->log("Audio", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
958         }
959
960
961         port_def_type.nBufferCountActual=2;
962         port_def_type.nBufferSize=max(port_def_type.nBufferSize,50000); // for transcoder important
963
964         error=OMX_SetParameter(omx_aud_rend/*dec*/,OMX_IndexParamPortDefinition, &port_def_type);
965
966         if (error!=OMX_ErrorNone){
967                         Log::getInstance()->log("Audio", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
968         }
969
970
971         error=OMX_SendCommand(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port/*codec*/,0);
972         if (error!=OMX_ErrorNone){
973                 Log::getInstance()->log("Audio", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
974                 return 0;
975         }
976
977         input_bufs_omx_mutex.Lock();
978         for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
979                 OMX_BUFFERHEADERTYPE *buf_head=NULL;
980                 error=OMX_AllocateBuffer(omx_aud_rend/*dec*/,&buf_head,omx_rend_input_port/*codec*/,NULL,port_def_type.nBufferSize);
981                 if (error!=OMX_ErrorNone){
982                         Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
983                                 input_bufs_omx_mutex.Unlock();
984                         return 0;
985                 }
986                 input_bufs_omx_all.push_back(buf_head);
987                 input_bufs_omx_free.push_back(buf_head);
988         }
989         omx_first_frame=true;
990
991         firstsynched=false;
992         cur_input_buf_omx=NULL;
993         input_bufs_omx_mutex.Unlock();
994
995         if (!video->CommandFinished(omx_aud_rend/*dec*/,OMX_CommandPortEnable,omx_rend_input_port /*codec*/)) {
996                 return 0;
997         }
998
999         return 1;
1000 }
1001
1002 int AudioOMX::DestroyInputBufsOMX() //call with clock mutex locked
1003 {
1004         OMX_ERRORTYPE error;
1005
1006         cur_input_buf_omx=NULL;
1007         input_bufs_omx_mutex.Lock();
1008         for (int i=0; i< input_bufs_omx_all.size();i++) {
1009                 error=OMX_FreeBuffer(omx_aud_rend/*dec*/,omx_rend_input_port/*codec*/,input_bufs_omx_all[i]);
1010                 if (error!=OMX_ErrorNone){
1011                         Log::getInstance()->log("Audio", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1012                         input_bufs_omx_mutex.Unlock();
1013                         return 0;
1014                 }
1015
1016         }
1017         input_bufs_omx_all.clear();
1018         input_bufs_omx_free.clear();
1019         input_bufs_omx_mutex.Unlock();
1020
1021 }
1022
1023
1024 int AudioOMX::DeAllocateCodecsOMX()
1025 {
1026         OMX_ERRORTYPE error;
1027         omx_running=false;
1028         VideoOMX *video=(VideoOMX*)Video::getInstance();
1029          Log::getInstance()->log("Audio", Log::DEBUG, "enter deallocatecodecsomx");
1030
1031
1032
1033    Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 1");
1034    if (cur_input_buf_omx) {
1035                 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1036                 OMX_ERRORTYPE error=video->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,cur_input_buf_omx);
1037                 if (error!=OMX_ErrorNone) {
1038                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_EmptyThisBuffer 6 failed %x", error);
1039                 }
1040
1041                 cur_input_buf_omx=NULL;//write out old data
1042         }
1043    Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 2");
1044
1045    video->LockClock();
1046         if (omx_aud_rend/*dec*/) {
1047                 // first stop the omx elements
1048         /*      if (!video->ChangeComponentState(omx_aud_dec,OMX_StateIdle)) {
1049                         Log::getInstance()->log("Audio", Log::DEBUG, "aud_dec ChangeComponentState");
1050                 }*/
1051                  Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 3");
1052
1053                 video->UnlockClock();
1054                 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 4");
1055                 video->idleClock();
1056                 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 5");
1057                  video->LockClock();
1058
1059                 if (!video->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1060                         Log::getInstance()->log("Audio", Log::DEBUG, "aud_rend ChangeComponentState");
1061                 }
1062
1063         // TODO proper deinit sequence
1064                 // first flush all buffers
1065
1066                 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
1067                 if (error!=OMX_ErrorNone) {
1068                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend in failed %x", error);
1069
1070                 }
1071
1072         /*      error=OMX_SendCommand(omx_aud_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
1073                 if (error!=OMX_ErrorNone){
1074                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1075
1076                 }*/
1077
1078
1079         /*      if (!video->CommandFinished(omx_aud_dec,OMX_CommandFlush,omx_codec_input_port)) {
1080                         Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd codec input failed");
1081                 }*/
1082
1083
1084
1085                 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1086                 if (error!=OMX_ErrorNone){
1087                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1088
1089                 }
1090
1091                 error=OMX_SendCommand(omx_aud_rend,OMX_CommandFlush, omx_rend_clock_port, NULL);
1092                 if (error!=OMX_ErrorNone){
1093                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Flush rend clock failed %x", error);
1094
1095                 }
1096                 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6");
1097
1098                 if (!video->CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1099                         !video->CommandFinished(omx_aud_rend,OMX_CommandFlush,omx_rend_clock_port)) {
1100                                 Log::getInstance()->log("Audio", Log::DEBUG, "flush cmd clock shed failed");
1101                 }
1102
1103                 DestroyInputBufsOMX(); //We have to make sure that no buffers are in use
1104                 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 6a");
1105                 DeinitDecoderLibAV();
1106                 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 7");
1107
1108                 //todo flushing
1109                 if (!video->DisablePort(omx_aud_rend,omx_rend_input_port,true)) {
1110                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 1");
1111                 }
1112
1113         /*      if (!video->DisablePort(omx_aud_dec,omx_codec_output_port,true)) {
1114                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 6");
1115                 }
1116
1117                 if (!video->DisablePort(omx_aud_dec,omx_codec_input_port,true)) {
1118                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 7");
1119                 }*/
1120
1121
1122                 if (!video->DisablePort(omx_aud_rend,omx_rend_clock_port,true)) {
1123                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 4");
1124                 }
1125
1126                 if (!video->DisablePort(omx_clock,omx_clock_output_port,true)) {
1127                         Log::getInstance()->log("Audio", Log::DEBUG, "Disable Tunnel Port failed 5");
1128                 }
1129
1130
1131
1132         /*      error=OMX_SetupTunnel(omx_aud_dec,omx_codec_output_port,NULL,NULL);
1133                 if (error!=OMX_ErrorNone) {
1134                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1135
1136                 }*/
1137
1138
1139
1140                 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_input_port,NULL,NULL);
1141                 if (error!=OMX_ErrorNone) {
1142                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1143
1144                 }
1145
1146                 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1147                 if (error!=OMX_ErrorNone) {
1148                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1149
1150                 }
1151
1152                 error=OMX_SetupTunnel(omx_aud_rend,omx_rend_clock_port,NULL,NULL);
1153                 if (error!=OMX_ErrorNone) {
1154                         Log::getInstance()->log("Audio", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1155
1156                 }
1157                 Log::getInstance()->log("Audio", Log::DEBUG, "deallocatecodecsomx mark 8");
1158
1159
1160                 //error=OMX_FreeHandle(omx_aud_dec);
1161                 error=OMX_FreeHandle(omx_aud_rend);
1162                 video->UnlockClock();
1163                 video->destroyClock();
1164                 omx_aud_rend/*dec*/=NULL;
1165                 if (error!=OMX_ErrorNone) {
1166                         Log::getInstance()->log("Audio", Log::DEBUG, "FreeHandle failed %d", error);
1167                 }
1168         } else  {
1169
1170                 video->UnlockClock();
1171                 DeinitDecoderLibAV();
1172         }
1173           Log::getInstance()->log("Audio", Log::DEBUG, "leave deallocate codecs OMX");
1174
1175         return 1;
1176 }
1177
1178
1179
1180 int AudioOMX::stop()
1181 {
1182   if (!initted) return 0;
1183
1184   Log::getInstance()->log("Audio", Log::DEBUG, "Audio stop called");
1185   DeAllocateCodecsOMX();
1186   //if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1187   return 1;
1188 }
1189
1190 int AudioOMX::mute() {
1191         if (!initted)
1192                 return 0;
1193         Log::getInstance()->log("Audio", Log::DEBUG, "MUTE MUTE MUTE");
1194         if (omx_running) {
1195                 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1196                 vw->LockClock();
1197                 OMX_AUDIO_CONFIG_MUTETYPE amute;
1198                 memset(&amute, 0, sizeof(amute));
1199                 amute.nSize = sizeof(amute);
1200                 amute.nVersion.nVersion = OMX_VERSION;
1201                 amute.nPortIndex = omx_rend_input_port;
1202                 amute.bMute = OMX_TRUE;
1203                 OMX_ERRORTYPE error= OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1204                                                 &amute);
1205                 if (error != OMX_ErrorNone) {
1206                         Log::getInstance()->log("Audio", Log::DEBUG,
1207                                         "Set OMX_IndexConfigAudioMute failed %x %d", error,
1208                                         omx_rend_input_port);
1209                         return 0;
1210                 }
1211                 vw->UnlockClock();
1212         }
1213
1214         muted = 1;
1215         return 1;
1216 }
1217
1218 int AudioOMX::unMute()
1219 {
1220   if (!initted) return 0;
1221
1222   Log::getInstance()->log("Audio", Log::DEBUG, "MUTE OFF OFF OFF");
1223   if (omx_running) {
1224                 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1225                 vw->LockClock();
1226                 OMX_AUDIO_CONFIG_MUTETYPE amute;
1227                 memset(&amute, 0, sizeof(amute));
1228                 amute.nSize = sizeof(amute);
1229                 amute.nVersion.nVersion = OMX_VERSION;
1230                 amute.nPortIndex = omx_rend_input_port;
1231                 amute.bMute = OMX_FALSE;
1232                 OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioMute,
1233                                                 &amute);
1234                 if (error != OMX_ErrorNone) {
1235                         Log::getInstance()->log("Audio", Log::DEBUG,
1236                                         "Set OMX_IndexConfigAudioMute failed %x %d", error,
1237                                         omx_rend_input_port);
1238                         return 0;
1239                 }
1240                 vw->UnlockClock();
1241         }
1242
1243   muted = 0;
1244   return 1;
1245 }
1246
1247 int AudioOMX::pause() {
1248         if (!initted)
1249                 return 0;
1250         if (!paused) {
1251                 paused = true;
1252                 VideoOMX *vw = (VideoOMX*) Video::getInstance();
1253                 vw->LockClock();
1254                 if (omx_running) {
1255                         OMX_ERRORTYPE error;
1256                         error = OMX_SendCommand(omx_aud_rend, OMX_CommandFlush,
1257                                         omx_rend_input_port, NULL);
1258                         if (error != OMX_ErrorNone) {
1259                                 Log::getInstance()->log("Audio", Log::DEBUG,
1260                                                 "OMX_Flush rend in failed %x", error);
1261
1262                         }
1263                         if (!vw->ChangeComponentState(omx_aud_rend,OMX_StateIdle)) {
1264                                 Log::getInstance()->log("Audio", Log::DEBUG, " pause aud_rend idle ChangeComponentState");
1265                         }
1266                 }
1267                 vw->UnlockClock();
1268                 vw->clockPause();
1269         }
1270         return 1;
1271 }
1272
1273 int AudioOMX::unPause()
1274 {
1275   if (!initted) return 0;
1276   if (paused) {
1277           paused=false; // may be also change omx clock
1278          VideoOMX *vw = (VideoOMX*) Video::getInstance();
1279          if (omx_running) {
1280                 if (!vw->ChangeComponentState(omx_aud_rend,OMX_StateExecuting)) {
1281                         Log::getInstance()->log("Audio", Log::DEBUG, " unpause aud_rend idle ChangeComponentState");
1282                 }
1283          }
1284           vw->clockUnpause();
1285   }
1286   return 1;
1287 }
1288
1289 int AudioOMX::reset()
1290 {
1291   if (!initted) return 0;
1292 //test();
1293   Log::getInstance()->log("Audio", Log::DEBUG, "reset called");
1294   DeAllocateCodecsOMX();
1295
1296 //  if (ioctl(fdAudio, AV_SET_AUD_RESET, 0x11) != 0) return 0;
1297 //  Log::getInstance()->log("Audio", Log::DEBUG, "reset back");
1298  // if (ioctl(fdAudio, AV_SET_AUD_PLAY, 0) != 0) return 0;
1299
1300   doMuting();
1301   return 1;
1302 }
1303
1304 int AudioOMX::setVolume(int tvolume)
1305 {
1306   // parameter: 0 for silence, 20 for full
1307   if ((tvolume < 0) || (tvolume > 20)) return 0;
1308
1309 // volume = 2 * (20 - volume);
1310 // Right, that one was rubbish... 0-10 were almost
1311 // inaudible, 11-20 did what should have been done
1312 // over the whole 0-20 range
1313   volume=tvolume;
1314
1315
1316
1317   if (omx_running) {
1318           VideoOMX *vw = (VideoOMX*) Video::getInstance();
1319           vw->LockClock();
1320           OMX_AUDIO_CONFIG_VOLUMETYPE avol;
1321           memset(&avol, 0, sizeof(avol));
1322           avol.nSize = sizeof(avol);
1323           avol.nVersion.nVersion = OMX_VERSION;
1324           avol.nPortIndex = omx_rend_input_port;
1325           avol.bLinear=OMX_FALSE;
1326           avol.sVolume.nValue =(volume-20)*200;
1327           OMX_ERRORTYPE error = OMX_SetParameter(omx_aud_rend, OMX_IndexConfigAudioVolume,
1328                           &avol);
1329           if (error != OMX_ErrorNone) {
1330                   Log::getInstance()->log("Audio", Log::DEBUG,
1331                                   "Set OMX_IndexConfigAudioVolume failed %x %d", error,
1332                                   omx_rend_input_port);
1333                   return 0;
1334           }
1335           vw->UnlockClock();
1336   }
1337
1338   return 1;
1339 }
1340
1341 #ifdef DEV
1342 int AudioOMX::test()
1343 {
1344 //  ULLONG stc = 0;
1345 //  return ioctl(fdAudio, AV_SET_AUD_STC, &stc);
1346
1347 /*  aud_sync_parms_t a;
1348   a.parm1 = 0;
1349   a.parm2 = 0;
1350 */
1351 //  int b = ioctl(fdAudio, AV_SET_AUD_DISABLE_SYNC, &a);
1352
1353
1354   /*OK*/ //printf("Audio sync disable = %i\n", b);
1355
1356   return 1;
1357
1358
1359 }
1360 #endif
1361
1362 unsigned int AudioOMX::AdvanceMpAudioSync(UCHAR *data,unsigned int size,unsigned int *framesize)
1363 {
1364         if (size<=2) return size; // silly;
1365         unsigned int test=0;
1366         *framesize=0;
1367         while (test+1<size) {
1368                 if (data[test]==0xFF && (data[test+1] &0xe0)==0xe0) return test; // probably FrameSync
1369                 test++;
1370         }
1371         return size;
1372 }
1373
1374 unsigned int AudioOMX::AdvanceAc3AudioSync(UCHAR *data,unsigned int size,unsigned int *framesize)
1375 {
1376         if (size<=4) return size; // silly;
1377         const int frm_size_tab[] = { 64, 64, 80, 80, 96, 96, 112, 112, 128, 128,
1378                         160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384, 384, 448,
1379                         448, 512, 512, 640, 640, 768, 768, 896, 896, 1024, 1024, 1152,
1380                         1152, 1280, 1280, };
1381         unsigned int test=0;
1382         *framesize=20000; //if we do not find a start code do not decompress
1383         while (test+4<size) {
1384                 if (data[test]==0x0B && data[test+1]==0x77) {
1385                         // now figure out the length of the frame
1386                         unsigned char code=data[test+4];
1387                         unsigned char fscod=(code& 0xC0)>>6;
1388                         unsigned char frmsize=(code &0x3f);
1389                         if (fscod!=0) Log::getInstance()->log("Audio", Log::DEBUG, "warning we only support 48 KHz sampling rate");
1390                         *framesize=frm_size_tab[frmsize]*2;
1391                         return test; // probably FrameSync
1392                 }
1393                 test++;
1394         }
1395         return size;
1396 }
1397
1398
1399 void AudioOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1400 {
1401   packet = mplist.front();
1402 }
1403
1404 UINT AudioOMX::DeliverMediaSample(UCHAR* buffer, UINT* samplepos) {
1405         DeliverMediaPacket(packet, buffer, samplepos);
1406         if (*samplepos == packet.length) {
1407                 *samplepos = 0;
1408                 return 1;
1409         } else
1410                 return 0;
1411 }
1412
1413
1414 long long AudioOMX::correctAudioLatency(long long pts,int addsamples,int srate) {
1415
1416         VideoOMX *video = (VideoOMX*) Video::getInstance();
1417         video->LockClock();
1418         OMX_PARAM_U32TYPE audio_lat;
1419         OMX_ERRORTYPE error;
1420         memset(&audio_lat, 0, sizeof(audio_lat));
1421         audio_lat.nSize = sizeof(audio_lat);
1422         audio_lat.nVersion.nVersion = OMX_VERSION;
1423         audio_lat.nPortIndex = omx_rend_input_port;
1424
1425         error = OMX_GetConfig(omx_aud_rend, OMX_IndexConfigAudioRenderingLatency,
1426                         &audio_lat);
1427          video->UnlockClock();
1428         if (error != OMX_ErrorNone) {
1429                 Log::getInstance()->log("Audio", Log::DEBUG,
1430                                 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
1431                                 omx_rend_input_port);
1432                 return pts; // no correction in case of error
1433         }
1434         /*Log::getInstance()->log("Audio", Log::DEBUG, "Current audio latency %d",
1435                         audio_lat.nU32);*/
1436
1437         long long workpts=0;
1438         workpts+=addsamples;
1439         workpts-=audio_lat.nU32;
1440         workpts*=10LL*1000LL*1000LL;
1441         workpts=workpts/((long long)srate); //one second /samplerate
1442         workpts+=pts;
1443
1444         return workpts;
1445 }
1446
1447
1448
1449 UINT AudioOMX::DeliverMediaPacket(MediaPacket packet, const UCHAR* buffer,
1450                 UINT *samplepos) {
1451         /*First Check, if we have an audio sample*/
1452         VideoOMX *vw = (VideoOMX*) Video::getInstance();
1453         bool achange=false;
1454         OMX_ERRORTYPE error;
1455         Log *logger=Log::getInstance();
1456         if (vw->InIframemode()) {
1457                 samplepos = 0;
1458                 MILLISLEEP(10);
1459                 return 0; //Not in iframe mode!
1460         }
1461
1462         if (!omx_running) return 0; // if we are not runnig do not do this
1463         if (paused) return 0; //Block if we pause
1464
1465         //Log::getInstance()->log("Audio", Log::DEBUG, "DeliverMediaPacketOMX time %lld",packet.presentation_time);
1466
1467         if (packet.synched && packet.presentation_time <= 0) {
1468                 *samplepos = packet.length;
1469                 firstsynched = false;
1470                 Log::getInstance()->log("Audio", Log::DEBUG,
1471                                 "DeliverMediaPacketOMX Frameskip");
1472                 return packet.length;
1473         }
1474
1475
1476
1477         UINT headerstrip = 0;
1478         if (packet.disconti) {
1479                 firstsynched = false;
1480                 decompress_buffer_filled=0;
1481                 if (cur_input_buf_omx) {
1482                         OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1483                                         cur_input_buf_omx);
1484                         if (error != OMX_ErrorNone) {
1485                                 Log::getInstance()->log("Audio", Log::DEBUG,
1486                                                 "OMX_EmptyThisBuffer 1 failed %x", error);
1487                         }
1488                         cur_input_buf_omx = NULL;
1489                 }
1490         }
1491
1492         if (packet.type != lastAType) {//Format Change //Push data out !
1493                 firstsynched = false;
1494                 achange=true;
1495                 lastAType = packet.type;
1496                 decompress_buffer_filled=0;
1497
1498                 if (cur_input_buf_omx) {
1499                         OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1500                                         cur_input_buf_omx);
1501                         if (error != OMX_ErrorNone) {
1502                                 Log::getInstance()->log("Audio", Log::DEBUG,
1503                                                 "OMX_EmptyThisBuffer 2 failed %x", error);
1504                         }
1505                         cur_input_buf_omx = NULL;
1506                 }
1507                 vw->LockClock();
1508                 if (!ChangeAudioPortConfig(true)) {
1509                         Log::getInstance()->log("Audio", Log::DEBUG,
1510                                         "Changing audio port config failed", error);
1511                 }
1512                 vw->UnlockClock();
1513
1514         }
1515
1516         /*Inspect PES-Header */
1517         if (*samplepos == 0 && packet.type != MPTYPE_MPEG_AUDIO_LAYER3) {//stripheader
1518                 headerstrip = buffer[packet.pos_buffer + 8] + 9;
1519                 if (packet.type == MPTYPE_AC3)
1520                         headerstrip += 4; //skip ac3 bytes
1521                 *samplepos += headerstrip;
1522                 if (packet.synched) {
1523                         if (cur_input_buf_omx) {
1524                                 //cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
1525                                 OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1526                                                 cur_input_buf_omx);
1527                                 if (error != OMX_ErrorNone) {
1528                                         Log::getInstance()->log("Audio", Log::DEBUG,
1529                                                         "OMX_EmptyThisBuffer 3 failed %x", error);
1530                                 }
1531                                  //vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1532
1533                                 cur_input_buf_omx = NULL;//write out old data
1534                         }
1535                         firstsynched = true;
1536                         //decompress_buffer_filled=0;
1537                 } else {
1538                         if (!firstsynched) {//
1539                                 *samplepos = packet.length;//if we have not processed at least one
1540                                 decompress_buffer_filled=0;
1541                                 return packet.length;//synched packet ignore it!
1542                         }
1543                 }
1544         }
1545         if (!cur_input_buf_omx) {
1546                 input_bufs_omx_mutex.Lock();
1547                 if (input_bufs_omx_free.size()==0) {
1548                         input_bufs_omx_mutex.Unlock();
1549                         //Log::getInstance()->log("Audio", Log::DEBUG, "Deliver MediaPacket no free sample");
1550                         return 0; // we do not have a free media sample
1551
1552                 }
1553                 cur_input_buf_omx=input_bufs_omx_free.front();
1554                 cur_input_buf_omx->nFilledLen=0;
1555                 cur_input_buf_omx->nOffset=0;
1556                 cur_input_buf_omx->nTimeStamp=VideoOMX::intToOMXTicks(0);
1557                 input_bufs_omx_free.pop_front();
1558                 input_bufs_omx_mutex.Unlock();
1559         }
1560
1561         if (cur_input_buf_omx->nFilledLen == 0) {//will only be changed on first packet
1562                 if (packet.synched) {
1563                         //Log::getInstance()->log("Audio", Log::DEBUG,
1564                         //              "packet synched marker");
1565
1566                         //lastreftimePTS=packet.pts;
1567                         if (omx_first_frame) { // TODO time
1568                                 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
1569                                 Log::getInstance()->log("Audio", Log::DEBUG, "Starttime");
1570                                 omx_first_frame = false;
1571                         } else {
1572                                 cur_input_buf_omx->nFlags = 0;
1573                                 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
1574                         }
1575                         lastreftimeOMX = packet.presentation_time;
1576                 //      Log::getInstance()->log("Audio", Log::DEBUG,
1577                 //                      "Time code %lld pts %lld dts %lld", lastreftimeOMX, packet.pts,packet.dts);
1578                         lastreftimePTS = packet.pts;
1579                         cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
1580                 } else {
1581                 //      Log::getInstance()->log("Audio", Log::DEBUG,
1582                         //                                      "packet NOT synched marker");
1583                         cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
1584                         cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
1585
1586                 }
1587                 if (packet.disconti || achange) {
1588                         cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_DISCONTINUITY;
1589                         //mp23codec_context_libav->frame_size=-1;
1590                         //ac3codec_context_libav->frame_size=-1;
1591                 }
1592
1593         }
1594
1595         if (*samplepos>packet.length) *samplepos=0; //propably the thread got interrupted and sample is not valid any more!
1596         unsigned int haveToCopy=packet.length-*samplepos;
1597         if (passthrough) {
1598                 //TODO
1599         } else {
1600                 int len;
1601                 int gotta;
1602                 int framesize=0;
1603                 int errcount=0;
1604
1605                 AVCodecContext *current_context;
1606                 switch (packet.type) {
1607                 case MPTYPE_MPEG_AUDIO:
1608                 case MPTYPE_MPEG_AUDIO_LAYER3: {
1609                         current_context = mp23codec_context_libav;
1610                         if (current_context->frame_size<0) framesize=1152; //Maximum framesize
1611                         else framesize=current_context->frame_size;
1612                 }break;
1613                 case MPTYPE_AC3:
1614                 case MPTYPE_AC3_PRE13: {
1615                         current_context = ac3codec_context_libav;
1616                 }break;
1617                 };
1618
1619                 if (decompress_buffer_filled) { // have a remaining paket
1620                         incoming_paket_libav.data =(uint8_t*) decompress_buffer;
1621                         memcpy(decompress_buffer+decompress_buffer_filled,
1622                                         buffer+packet.pos_buffer+*samplepos,
1623                                         min(haveToCopy,decompress_buffer_size-decompress_buffer_filled));
1624                         incoming_paket_libav.size = decompress_buffer_filled
1625                                         +min(haveToCopy,decompress_buffer_size-decompress_buffer_filled);
1626                         //Log::getInstance()->log("Audio", Log::DEBUG,"Use saved audio buffer %d",packet.type);
1627                 } else {
1628                         incoming_paket_libav.data =(uint8_t*) buffer+packet.pos_buffer+*samplepos;
1629                         incoming_paket_libav.size = haveToCopy;
1630                 }
1631
1632                 while (haveToCopy> 0 && errcount<3) {
1633
1634                         //Log::getInstance()->log("Audio", Log::DEBUG,"libav in %d %d",framesize,current_context->frame_size);
1635                         //Log::getInstance()->log("Audio", Log::DEBUG, "libav in %d %d",
1636                                 //      framesize, current_context->frame_size);
1637
1638                         bool donotdecompress=false;
1639                         unsigned int gotframesize=0;
1640                         if (!decompress_buffer_filled) { // only do this if no old data is present
1641                                 int adv = 0;
1642                                 switch (packet.type) {
1643                                 case MPTYPE_MPEG_AUDIO:
1644                                 case MPTYPE_MPEG_AUDIO_LAYER3: {
1645                                         adv = AdvanceMpAudioSync(incoming_paket_libav.data,
1646                                                         incoming_paket_libav.size,&gotframesize);
1647                                 }
1648                                 break;
1649                                 case MPTYPE_AC3:
1650                                 case MPTYPE_AC3_PRE13: {
1651                                         adv = AdvanceAc3AudioSync(incoming_paket_libav.data,
1652                                                         incoming_paket_libav.size,&gotframesize);
1653                                 }
1654                                 break;
1655                                 };
1656                                 if (adv > 0) {
1657                                         incoming_paket_libav.data += adv;
1658                                         incoming_paket_libav.size-=adv;
1659                                         haveToCopy -= adv;
1660                                         *samplepos += adv;
1661                                         /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
1662                                         else*/ framesize=haveToCopy;
1663                                         //Log::getInstance()->log("Audio", Log::DEBUG,"Advance by %d %d from %d",adv,packet.type,*samplepos );
1664                                         if (haveToCopy <= 0) {
1665                                                 //       Log::getInstance()->log("Audio", Log::DEBUG,"No sync code in packet remove %d",packet.type);
1666                                                 *samplepos=packet.length;
1667                                                 return packet.length;
1668                                         }
1669
1670
1671                                 } //
1672
1673                                 if (gotframesize>0 && gotframesize>haveToCopy) {
1674                                         donotdecompress=true;
1675                                         errcount=100; // exit loop
1676                                 }
1677                                 //      else Log::getInstance()->log("Audio", Log::DEBUG,"Loop run" );
1678                         }
1679
1680                         if (!donotdecompress) {
1681                                 int oldcancelstate;
1682                                 int oldcanceltype;
1683                                 pthread_testcancel();
1684                                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
1685                                 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
1686                                 libav_mutex.Lock();
1687                                 if (!omx_running || !mp23codec_context_libav
1688                                                 || !ac3codec_context_libav) {
1689                                         libav_mutex.Unlock();
1690                                         return *samplepos;
1691                                 }
1692                                 libav_mutex.Unlock();
1693                                 //      Log::getInstance()->log("Audio", Log::DEBUG,"libav out");
1694                                 len = avcodec_decode_audio4(current_context, decode_frame_libav,
1695                                                 &gotta, &incoming_paket_libav);
1696                                 //Log::getInstance()->log("Audio", Log::DEBUG, "libav out1");
1697                                 pthread_setcancelstate(oldcancelstate, NULL);
1698                                 pthread_setcanceltype(oldcanceltype, NULL);
1699                                 pthread_testcancel();
1700
1701                         } else {
1702                                 gotta=0;
1703                                 len=0;
1704                         }
1705                         //Log::getInstance()->log("Audio", Log::DEBUG, "libav out2");
1706                         if (!omx_running) {
1707                                 libav_mutex.Unlock();
1708                                 return *samplepos;
1709                         }
1710
1711                         if (decompress_buffer_filled) { // reset to normal decoding
1712                                 if (len>0) {
1713                                         Log::getInstance()->log("Audio", Log::DEBUG,"saved audio: %d",len);
1714                                         haveToCopy -= min(len-decompress_buffer_filled,0);
1715                                         *samplepos += min(len-decompress_buffer_filled,0);
1716                                         //if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
1717                                         /*else*/ framesize=haveToCopy;
1718                                 } else {
1719                                         framesize=haveToCopy;
1720                                 }
1721                                 incoming_paket_libav.data =(uint8_t*) buffer+packet.pos_buffer+*samplepos;
1722                                 errcount=0;
1723                                 decompress_buffer_filled=0;
1724                         } else {
1725
1726                                 if (len>0) {
1727                                         incoming_paket_libav.data += len;
1728                                         haveToCopy -= len;
1729                                         *samplepos += len;
1730                                         errcount=0;
1731                                         /*if (current_context->frame_size>0) framesize=min(current_context->frame_size,haveToCopy);
1732                                         else*/framesize=haveToCopy;
1733                                 } else {
1734                                         errcount++;
1735                                         framesize=haveToCopy;
1736                                 }
1737                         }
1738
1739                         incoming_paket_libav.size =framesize;
1740                         if (gotta) {
1741                                 //Log::getInstance()->log("Audio", Log::DEBUG,
1742                                 //                                      "Got a frame");
1743                                 int dsize = av_samples_get_buffer_size(NULL,
1744                                                 current_context->channels, decode_frame_libav->nb_samples,
1745                                                 current_context->sample_fmt, 1);
1746                                 if ((cur_input_buf_omx->nFilledLen + dsize)
1747                                                 > cur_input_buf_omx->nAllocLen ) {
1748                                         // I doubt that this will ever happen
1749                                 //      Log::getInstance()->log("Audio", Log::DEBUG,
1750                                         //                                      "P 2 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
1751                                         OMX_ERRORTYPE error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/,
1752                                                         cur_input_buf_omx);
1753                                         if (error != OMX_ErrorNone) {
1754                                                 Log::getInstance()->log("Audio", Log::DEBUG,
1755                                                                 "OMX_EmptyThisBuffer 4 failed %x", error);
1756                                         }
1757                                         cur_input_buf_omx = NULL;
1758
1759                                         if (!cur_input_buf_omx) {
1760                                                 int count = 0;
1761                                                 while (count < 10 && omx_running) {
1762                                                         count++;
1763                                                         input_bufs_omx_mutex.Lock();
1764                                                         if (input_bufs_omx_free.size() == 0) {
1765                                                                 input_bufs_omx_mutex.Unlock();
1766                                                         //      Log::getInstance()->log("Audio", Log::DEBUG,
1767                                                                 //              "Deliver MediaPacket no free sample");
1768                                                                 MILLISLEEP(5);
1769                                                                 if (!omx_running) return *samplepos;
1770                                                                 continue;
1771                                                         }
1772                                                         cur_input_buf_omx = input_bufs_omx_free.front();
1773                                                         cur_input_buf_omx->nFilledLen = 0;
1774                                                         cur_input_buf_omx->nOffset = 0;
1775                                                         cur_input_buf_omx->nTimeStamp = VideoOMX::intToOMXTicks(0);
1776                                                         cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
1777                                                         input_bufs_omx_free.pop_front();
1778                                                         input_bufs_omx_mutex.Unlock();
1779                                                         break;
1780                                                 }
1781                                                 if (!cur_input_buf_omx) return *samplepos;
1782                                         }
1783
1784                                 }
1785
1786                                 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy in %d %d %d" ,dsize,current_context->sample_rate,cur_input_buf_omx->nFilledLen);
1787                                 memcpy(cur_input_buf_omx->pBuffer + cur_input_buf_omx->nFilledLen,
1788                                                 decode_frame_libav->data[0], dsize);
1789                                 //Log::getInstance()->log("Audio", Log::DEBUG,"memcpy out");
1790                                 cur_input_buf_omx->nFilledLen += dsize;
1791                         } else {
1792                                 //Log::getInstance()->log("Audio", Log::DEBUG,"Incomplete mpeg frames in pes packet %d %d",incoming_paket_libav.size,packet.length);
1793                         /*      uint8_t a1=incoming_paket_libav.data[0];
1794                                 uint8_t a2=incoming_paket_libav.data[1];
1795                                 uint8_t a3=incoming_paket_libav.data[2];
1796                                 uint8_t a4=incoming_paket_libav.data[3];*/
1797                         //      Log::getInstance()->log("Audio", Log::DEBUG,"Header %x %x %x %x",a1,a2,
1798                                 //              a3,a4);
1799                         }
1800
1801                 }
1802
1803         }
1804         decompress_buffer_filled=0;
1805         if (haveToCopy ) {
1806                 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);
1807                 memcpy(decompress_buffer,incoming_paket_libav.data,min(haveToCopy,decompress_buffer_size));
1808
1809             decompress_buffer_filled=min(haveToCopy,decompress_buffer_size);
1810
1811         }
1812
1813         if (cur_input_buf_omx->nFilledLen) {
1814                 //Log::getInstance()->log("Audio", Log::DEBUG,
1815                         //                                                              "P 3 Time code %lld pts %lld", lastreftimeOMX, packet.pts);
1816                 error = vw->ProtOMXEmptyThisBuffer(omx_aud_rend/*dec*/, cur_input_buf_omx);
1817                 if (error != OMX_ErrorNone) {
1818                         Log::getInstance()->log("Audio", Log::DEBUG,
1819                                         "OMX_EmptyThisBuffer 5 failed %x", error);
1820                 }
1821                 //if (packet.synched) vw->AdjustAudioPTS(correctAudioLatency(lastreftimeOMX,cur_input_buf_omx->nFilledLen/(2*2),48000));
1822                 cur_input_buf_omx = NULL;
1823         }
1824
1825
1826
1827
1828         *samplepos=packet.length;
1829         return packet.length;
1830
1831 }
1832
1833
1834
1835 long long AudioOMX::SetStartOffset(long long curreftime, bool *rsync){
1836   VideoOMX *vw=(VideoOMX*)Video::getInstance();
1837   return vw->SetStartAudioOffset(curreftime,rsync);
1838 }
1839
1840 void AudioOMX::ResetTimeOffsets() {
1841   VideoOMX *vw=(VideoOMX*)Video::getInstance();
1842   vw->ResetTimeOffsets();
1843 }
1844
1845