]> git.vomp.tv Git - vompclient-marten.git/blob - audioomx.h
Add downmixing code for codecs of libav with no downmix support
[vompclient-marten.git] / audioomx.h
1 /*
2     Copyright 2004-2005 Chris Tallon, 2009,2012 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 #ifndef AUDIOOMX_H
22 #define AUDIOOMX_H
23
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/ioctl.h>
28
29 #include "defines.h"
30 #include "audio.h"
31 #include "videoomx.h"
32
33 //#define USE_LIBRESAMPLE // untested code for future use
34
35 extern "C" {
36 #include <libavcodec/avcodec.h>
37 #include <libavformat/avformat.h>
38 #ifdef USE_LIBRESAMPLE
39 #include <libavresample/avresample.h>
40 #endif
41 }
42
43
44
45
46 class AudioOMX : public Audio
47 {
48   friend class VideoOMX;
49   public:
50     AudioOMX();
51     virtual ~AudioOMX();
52
53     int init(UCHAR streamType);
54     int shutdown();
55
56     int setStreamType(UCHAR streamType);
57     int setChannel();
58     int setSource();
59     int sync();
60     int play();
61     int stop();
62     int pause();
63     int unPause();
64     int reset();
65     int setVolume(int volume);
66     int mute();
67     int unMute();
68     bool supportsAc3() { return true; }
69     bool maysupportAc3(){return true;}
70     bool streamTypeSupported(int streamtype)
71     {
72         switch (streamtype) {
73         case 0x11: //AAC LATM packaging
74         case 0x6A://ac3
75         case 3: //mpeg 1 layer 1 and 2
76         case 4:
77                 return true;
78         default:
79                 return false;
80         };
81     }
82
83
84     //Writing Data to Audiodevice
85     virtual void PrepareMediaSample(const MediaPacketList&, UINT samplepos);
86     virtual UINT DeliverMediaSample(UCHAR* buffer, UINT *samplepos);
87     UINT DeliverMediaPacket(MediaPacket packet, const UCHAR* buffer,UINT *samplepos);
88
89     virtual long long SetStartOffset(long long curreftime, bool *rsync);
90     virtual void ResetTimeOffsets();
91
92     virtual bool DrainTargetReady() {return omx_running;};
93
94
95         UCHAR getLastAType() {return lastAType;}
96
97         bool loadOptionsfromServer(VDR* vdr);
98         bool saveOptionstoServer();
99         bool addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane);
100         bool handleOptionChanges(Option* option);
101
102
103 #ifdef DEV
104     int test();
105 #endif
106
107   private:
108     int initAllParams();
109     UCHAR streamType;
110         UCHAR lastAType;
111         bool firstsynched;
112
113     MediaPacket packet;
114     UINT packetpos;
115
116
117     bool hdmi; // use hdmi as audio output
118     bool passthrough; // use audio passthrough for the current audio type
119
120     bool canpass_aac;
121     bool canpass_ac3;
122     bool canpass_mp2;
123     bool canpass_mp3;
124     bool canpass_pcm_mch;
125
126
127     int prefered_aac;
128     int prefered_ac3; //0 stereo PCM, 1 passthrough 2 Multichannel PCM
129     int prefered_mp2;
130     int prefered_mp3;
131
132
133
134     static OMX_ERRORTYPE EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* bulibaver);
135         static OMX_ERRORTYPE FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* bulibaver);
136
137
138
139         unsigned int AdvanceAc3AudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize);
140         unsigned int AdvanceAacLatmAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize);
141         unsigned int AdvanceMpAudioSync(const UCHAR *data,unsigned int size,unsigned int *framesize);
142
143
144     void ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* bulibaver);
145
146    // OMX_HANDLETYPE omx_aud_dec;
147         OMX_HANDLETYPE omx_aud_rend;
148         OMX_HANDLETYPE omx_clock;
149
150         //OMX_U32 omx_codec_input_port;
151         //OMX_U32 omx_codec_output_port;
152         OMX_U32 omx_rend_input_port;
153         OMX_U32 omx_rend_clock_port;
154         OMX_U32 omx_clock_output_port;
155
156         long long lastreftimeOMX;
157     ULLONG lastreftimePTS;
158
159
160
161         int AllocateCodecsOMX();
162         int DeAllocateCodecsOMX();
163
164         int PrepareInputBufsOMX(bool setportdef);
165         int DestroyInputBufsOMX();
166         int DestroyInputBufsOMXwhilePlaying();
167
168         int ChangeAudioPortConfig(bool disport);
169         int ChangeAudioDestination();
170         long long correctAudioLatency(long long pts,int addsamples,int srate);
171
172         vector<OMX_BUFFERHEADERTYPE*> input_bufs_omx_all;
173         list<OMX_BUFFERHEADERTYPE*> input_bufs_omx_free;
174         Mutex input_bufs_omx_mutex;
175         OMX_BUFFERHEADERTYPE* cur_input_buf_omx;
176
177     bool omx_running;
178     bool omx_first_frame;
179     Mutex libav_mutex;
180
181     AVCodec *aaclatmcodec_libav;
182     AVCodecContext *aaclatmcodec_context_libav;
183         AVCodec *ac3codec_libav;
184         AVCodecContext *ac3codec_context_libav;
185         AVCodec *mp23codec_libav;
186     AVCodecContext *mp23codec_context_libav;
187     AVPacket incoming_paket_libav;
188     AVFrame *decode_frame_libav;
189 #ifdef USE_LIBRESAMPLE
190     AVAudioResampleContext *resam_con_libav;
191 #else
192     void getDownMixMatrix(unsigned long long channels,double *left_mat,double *right_mat);
193 #endif
194
195     UCHAR * decompress_buffer;
196     unsigned int decompress_buffer_size;
197     unsigned int decompress_buffer_filled;
198     bool lsync;
199
200
201          int InitDecoderLibAV();
202          void DeinitDecoderLibAV();
203
204 };
205
206 #endif