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