1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/modules/audio_coding/neteq/audio_decoder_impl.h"
12 
13 #include <assert.h>
14 
15 #include "webrtc/base/checks.h"
16 #include "webrtc/modules/audio_coding/codecs/cng/webrtc_cng.h"
17 #include "webrtc/modules/audio_coding/codecs/g711/audio_decoder_pcm.h"
18 #ifdef WEBRTC_CODEC_G722
19 #include "webrtc/modules/audio_coding/codecs/g722/audio_decoder_g722.h"
20 #endif
21 #ifdef WEBRTC_CODEC_ILBC
22 #include "webrtc/modules/audio_coding/codecs/ilbc/audio_decoder_ilbc.h"
23 #endif
24 #ifdef WEBRTC_CODEC_ISACFX
25 #include "webrtc/modules/audio_coding/codecs/isac/fix/include/audio_decoder_isacfix.h"
26 #include "webrtc/modules/audio_coding/codecs/isac/fix/include/audio_encoder_isacfix.h"
27 #endif
28 #ifdef WEBRTC_CODEC_ISAC
29 #include "webrtc/modules/audio_coding/codecs/isac/main/include/audio_decoder_isac.h"
30 #include "webrtc/modules/audio_coding/codecs/isac/main/include/audio_encoder_isac.h"
31 #endif
32 #ifdef WEBRTC_CODEC_OPUS
33 #include "webrtc/modules/audio_coding/codecs/opus/audio_decoder_opus.h"
34 #endif
35 #include "webrtc/modules/audio_coding/codecs/pcm16b/audio_decoder_pcm16b.h"
36 
37 namespace webrtc {
38 
AudioDecoderCng()39 AudioDecoderCng::AudioDecoderCng() {
40   RTC_CHECK_EQ(0, WebRtcCng_CreateDec(&dec_state_));
41   WebRtcCng_InitDec(dec_state_);
42 }
43 
~AudioDecoderCng()44 AudioDecoderCng::~AudioDecoderCng() {
45   WebRtcCng_FreeDec(dec_state_);
46 }
47 
Reset()48 void AudioDecoderCng::Reset() {
49   WebRtcCng_InitDec(dec_state_);
50 }
51 
IncomingPacket(const uint8_t * payload,size_t payload_len,uint16_t rtp_sequence_number,uint32_t rtp_timestamp,uint32_t arrival_timestamp)52 int AudioDecoderCng::IncomingPacket(const uint8_t* payload,
53                                     size_t payload_len,
54                                     uint16_t rtp_sequence_number,
55                                     uint32_t rtp_timestamp,
56                                     uint32_t arrival_timestamp) {
57   return -1;
58 }
59 
CngDecoderInstance()60 CNG_dec_inst* AudioDecoderCng::CngDecoderInstance() {
61   return dec_state_;
62 }
63 
Channels() const64 size_t AudioDecoderCng::Channels() const {
65   return 1;
66 }
67 
DecodeInternal(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,int16_t * decoded,SpeechType * speech_type)68 int AudioDecoderCng::DecodeInternal(const uint8_t* encoded,
69                                     size_t encoded_len,
70                                     int sample_rate_hz,
71                                     int16_t* decoded,
72                                     SpeechType* speech_type) {
73   return -1;
74 }
75 
CodecSupported(NetEqDecoder codec_type)76 bool CodecSupported(NetEqDecoder codec_type) {
77   switch (codec_type) {
78     case NetEqDecoder::kDecoderPCMu:
79     case NetEqDecoder::kDecoderPCMa:
80     case NetEqDecoder::kDecoderPCMu_2ch:
81     case NetEqDecoder::kDecoderPCMa_2ch:
82 #ifdef WEBRTC_CODEC_ILBC
83     case NetEqDecoder::kDecoderILBC:
84 #endif
85 #if defined(WEBRTC_CODEC_ISACFX) || defined(WEBRTC_CODEC_ISAC)
86     case NetEqDecoder::kDecoderISAC:
87 #endif
88 #ifdef WEBRTC_CODEC_ISAC
89     case NetEqDecoder::kDecoderISACswb:
90 #endif
91     case NetEqDecoder::kDecoderPCM16B:
92     case NetEqDecoder::kDecoderPCM16Bwb:
93     case NetEqDecoder::kDecoderPCM16Bswb32kHz:
94     case NetEqDecoder::kDecoderPCM16Bswb48kHz:
95     case NetEqDecoder::kDecoderPCM16B_2ch:
96     case NetEqDecoder::kDecoderPCM16Bwb_2ch:
97     case NetEqDecoder::kDecoderPCM16Bswb32kHz_2ch:
98     case NetEqDecoder::kDecoderPCM16Bswb48kHz_2ch:
99     case NetEqDecoder::kDecoderPCM16B_5ch:
100 #ifdef WEBRTC_CODEC_G722
101     case NetEqDecoder::kDecoderG722:
102     case NetEqDecoder::kDecoderG722_2ch:
103 #endif
104 #ifdef WEBRTC_CODEC_OPUS
105     case NetEqDecoder::kDecoderOpus:
106     case NetEqDecoder::kDecoderOpus_2ch:
107 #endif
108     case NetEqDecoder::kDecoderRED:
109     case NetEqDecoder::kDecoderAVT:
110     case NetEqDecoder::kDecoderCNGnb:
111     case NetEqDecoder::kDecoderCNGwb:
112     case NetEqDecoder::kDecoderCNGswb32kHz:
113     case NetEqDecoder::kDecoderCNGswb48kHz:
114     case NetEqDecoder::kDecoderArbitrary: {
115       return true;
116     }
117     default: {
118       return false;
119     }
120   }
121 }
122 
CodecSampleRateHz(NetEqDecoder codec_type)123 int CodecSampleRateHz(NetEqDecoder codec_type) {
124   switch (codec_type) {
125     case NetEqDecoder::kDecoderPCMu:
126     case NetEqDecoder::kDecoderPCMa:
127     case NetEqDecoder::kDecoderPCMu_2ch:
128     case NetEqDecoder::kDecoderPCMa_2ch:
129 #ifdef WEBRTC_CODEC_ILBC
130     case NetEqDecoder::kDecoderILBC:
131 #endif
132     case NetEqDecoder::kDecoderPCM16B:
133     case NetEqDecoder::kDecoderPCM16B_2ch:
134     case NetEqDecoder::kDecoderPCM16B_5ch:
135     case NetEqDecoder::kDecoderCNGnb: {
136       return 8000;
137     }
138 #if defined(WEBRTC_CODEC_ISACFX) || defined(WEBRTC_CODEC_ISAC)
139     case NetEqDecoder::kDecoderISAC:
140 #endif
141     case NetEqDecoder::kDecoderPCM16Bwb:
142     case NetEqDecoder::kDecoderPCM16Bwb_2ch:
143 #ifdef WEBRTC_CODEC_G722
144     case NetEqDecoder::kDecoderG722:
145     case NetEqDecoder::kDecoderG722_2ch:
146 #endif
147     case NetEqDecoder::kDecoderCNGwb: {
148       return 16000;
149     }
150 #ifdef WEBRTC_CODEC_ISAC
151     case NetEqDecoder::kDecoderISACswb:
152 #endif
153     case NetEqDecoder::kDecoderPCM16Bswb32kHz:
154     case NetEqDecoder::kDecoderPCM16Bswb32kHz_2ch:
155     case NetEqDecoder::kDecoderCNGswb32kHz: {
156       return 32000;
157     }
158     case NetEqDecoder::kDecoderPCM16Bswb48kHz:
159     case NetEqDecoder::kDecoderPCM16Bswb48kHz_2ch: {
160       return 48000;
161     }
162 #ifdef WEBRTC_CODEC_OPUS
163     case NetEqDecoder::kDecoderOpus:
164     case NetEqDecoder::kDecoderOpus_2ch: {
165       return 48000;
166     }
167 #endif
168     case NetEqDecoder::kDecoderCNGswb48kHz: {
169       // TODO(tlegrand): Remove limitation once ACM has full 48 kHz support.
170       return 32000;
171     }
172     default: {
173       return -1;  // Undefined sample rate.
174     }
175   }
176 }
177 
CreateAudioDecoder(NetEqDecoder codec_type)178 AudioDecoder* CreateAudioDecoder(NetEqDecoder codec_type) {
179   if (!CodecSupported(codec_type)) {
180     return NULL;
181   }
182   switch (codec_type) {
183     case NetEqDecoder::kDecoderPCMu:
184       return new AudioDecoderPcmU(1);
185     case NetEqDecoder::kDecoderPCMa:
186       return new AudioDecoderPcmA(1);
187     case NetEqDecoder::kDecoderPCMu_2ch:
188       return new AudioDecoderPcmU(2);
189     case NetEqDecoder::kDecoderPCMa_2ch:
190       return new AudioDecoderPcmA(2);
191 #ifdef WEBRTC_CODEC_ILBC
192     case NetEqDecoder::kDecoderILBC:
193       return new AudioDecoderIlbc;
194 #endif
195 #if defined(WEBRTC_CODEC_ISACFX)
196     case NetEqDecoder::kDecoderISAC:
197       return new AudioDecoderIsacFix();
198 #elif defined(WEBRTC_CODEC_ISAC)
199     case NetEqDecoder::kDecoderISAC:
200     case NetEqDecoder::kDecoderISACswb:
201       return new AudioDecoderIsac();
202 #endif
203     case NetEqDecoder::kDecoderPCM16B:
204     case NetEqDecoder::kDecoderPCM16Bwb:
205     case NetEqDecoder::kDecoderPCM16Bswb32kHz:
206     case NetEqDecoder::kDecoderPCM16Bswb48kHz:
207       return new AudioDecoderPcm16B(1);
208     case NetEqDecoder::kDecoderPCM16B_2ch:
209     case NetEqDecoder::kDecoderPCM16Bwb_2ch:
210     case NetEqDecoder::kDecoderPCM16Bswb32kHz_2ch:
211     case NetEqDecoder::kDecoderPCM16Bswb48kHz_2ch:
212       return new AudioDecoderPcm16B(2);
213     case NetEqDecoder::kDecoderPCM16B_5ch:
214       return new AudioDecoderPcm16B(5);
215 #ifdef WEBRTC_CODEC_G722
216     case NetEqDecoder::kDecoderG722:
217       return new AudioDecoderG722;
218     case NetEqDecoder::kDecoderG722_2ch:
219       return new AudioDecoderG722Stereo;
220 #endif
221 #ifdef WEBRTC_CODEC_OPUS
222     case NetEqDecoder::kDecoderOpus:
223       return new AudioDecoderOpus(1);
224     case NetEqDecoder::kDecoderOpus_2ch:
225       return new AudioDecoderOpus(2);
226 #endif
227     case NetEqDecoder::kDecoderCNGnb:
228     case NetEqDecoder::kDecoderCNGwb:
229     case NetEqDecoder::kDecoderCNGswb32kHz:
230     case NetEqDecoder::kDecoderCNGswb48kHz:
231       return new AudioDecoderCng;
232     case NetEqDecoder::kDecoderRED:
233     case NetEqDecoder::kDecoderAVT:
234     case NetEqDecoder::kDecoderArbitrary:
235     default: {
236       return NULL;
237     }
238   }
239 }
240 
241 }  // namespace webrtc
242