henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_coding/neteq/interface/audio_decoder.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | |
kwiberg@webrtc.org | 8b2058e | 2014-11-06 07:54:31 +0000 | [diff] [blame] | 15 | #include "webrtc/base/checks.h" |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 16 | #include "webrtc/modules/audio_coding/neteq/audio_decoder_impl.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 20 | int AudioDecoder::DecodeRedundant(const uint8_t* encoded, |
| 21 | size_t encoded_len, |
| 22 | int16_t* decoded, |
| 23 | SpeechType* speech_type) { |
| 24 | return Decode(encoded, encoded_len, decoded, speech_type); |
| 25 | } |
| 26 | |
| 27 | bool AudioDecoder::HasDecodePlc() const { return false; } |
| 28 | |
| 29 | int AudioDecoder::DecodePlc(int num_frames, int16_t* decoded) { return -1; } |
| 30 | |
| 31 | int AudioDecoder::IncomingPacket(const uint8_t* payload, |
| 32 | size_t payload_len, |
| 33 | uint16_t rtp_sequence_number, |
| 34 | uint32_t rtp_timestamp, |
| 35 | uint32_t arrival_timestamp) { |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | int AudioDecoder::ErrorCode() { return 0; } |
| 40 | |
| 41 | int AudioDecoder::PacketDuration(const uint8_t* encoded, size_t encoded_len) { |
| 42 | return kNotImplemented; |
| 43 | } |
| 44 | |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 45 | int AudioDecoder::PacketDurationRedundant(const uint8_t* encoded, |
| 46 | size_t encoded_len) const { |
| 47 | return kNotImplemented; |
| 48 | } |
| 49 | |
| 50 | bool AudioDecoder::PacketHasFec(const uint8_t* encoded, |
| 51 | size_t encoded_len) const { |
| 52 | return false; |
| 53 | } |
| 54 | |
kwiberg@webrtc.org | 8b2058e | 2014-11-06 07:54:31 +0000 | [diff] [blame] | 55 | CNG_dec_inst* AudioDecoder::CngDecoderInstance() { |
| 56 | FATAL() << "Not a CNG decoder"; |
| 57 | return NULL; |
| 58 | } |
| 59 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 60 | bool AudioDecoder::CodecSupported(NetEqDecoder codec_type) { |
| 61 | switch (codec_type) { |
| 62 | case kDecoderPCMu: |
| 63 | case kDecoderPCMa: |
| 64 | case kDecoderPCMu_2ch: |
| 65 | case kDecoderPCMa_2ch: |
| 66 | #ifdef WEBRTC_CODEC_ILBC |
| 67 | case kDecoderILBC: |
| 68 | #endif |
| 69 | #if defined(WEBRTC_CODEC_ISACFX) || defined(WEBRTC_CODEC_ISAC) |
| 70 | case kDecoderISAC: |
| 71 | #endif |
| 72 | #ifdef WEBRTC_CODEC_ISAC |
| 73 | case kDecoderISACswb: |
henrik.lundin@webrtc.org | ac59dba | 2013-01-31 09:55:24 +0000 | [diff] [blame] | 74 | case kDecoderISACfb: |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 75 | #endif |
| 76 | #ifdef WEBRTC_CODEC_PCM16 |
| 77 | case kDecoderPCM16B: |
| 78 | case kDecoderPCM16Bwb: |
| 79 | case kDecoderPCM16Bswb32kHz: |
| 80 | case kDecoderPCM16Bswb48kHz: |
| 81 | case kDecoderPCM16B_2ch: |
| 82 | case kDecoderPCM16Bwb_2ch: |
| 83 | case kDecoderPCM16Bswb32kHz_2ch: |
| 84 | case kDecoderPCM16Bswb48kHz_2ch: |
| 85 | case kDecoderPCM16B_5ch: |
| 86 | #endif |
| 87 | #ifdef WEBRTC_CODEC_G722 |
| 88 | case kDecoderG722: |
henrik.lundin@webrtc.org | aaad613 | 2013-02-01 11:49:28 +0000 | [diff] [blame] | 89 | case kDecoderG722_2ch: |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 90 | #endif |
turaj@webrtc.org | 6ad6a07 | 2013-09-30 20:07:39 +0000 | [diff] [blame] | 91 | #ifdef WEBRTC_CODEC_CELT |
| 92 | case kDecoderCELT_32: |
| 93 | case kDecoderCELT_32_2ch: |
| 94 | #endif |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 95 | #ifdef WEBRTC_CODEC_OPUS |
| 96 | case kDecoderOpus: |
| 97 | case kDecoderOpus_2ch: |
| 98 | #endif |
| 99 | case kDecoderRED: |
| 100 | case kDecoderAVT: |
| 101 | case kDecoderCNGnb: |
| 102 | case kDecoderCNGwb: |
| 103 | case kDecoderCNGswb32kHz: |
| 104 | case kDecoderCNGswb48kHz: |
| 105 | case kDecoderArbitrary: { |
| 106 | return true; |
| 107 | } |
| 108 | default: { |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | int AudioDecoder::CodecSampleRateHz(NetEqDecoder codec_type) { |
| 115 | switch (codec_type) { |
| 116 | case kDecoderPCMu: |
| 117 | case kDecoderPCMa: |
| 118 | case kDecoderPCMu_2ch: |
| 119 | case kDecoderPCMa_2ch: |
| 120 | #ifdef WEBRTC_CODEC_ILBC |
| 121 | case kDecoderILBC: |
| 122 | #endif |
| 123 | #ifdef WEBRTC_CODEC_PCM16 |
| 124 | case kDecoderPCM16B: |
| 125 | case kDecoderPCM16B_2ch: |
| 126 | case kDecoderPCM16B_5ch: |
| 127 | #endif |
| 128 | case kDecoderCNGnb: { |
| 129 | return 8000; |
| 130 | } |
| 131 | #if defined(WEBRTC_CODEC_ISACFX) || defined(WEBRTC_CODEC_ISAC) |
| 132 | case kDecoderISAC: |
| 133 | #endif |
| 134 | #ifdef WEBRTC_CODEC_PCM16 |
| 135 | case kDecoderPCM16Bwb: |
| 136 | case kDecoderPCM16Bwb_2ch: |
| 137 | #endif |
| 138 | #ifdef WEBRTC_CODEC_G722 |
| 139 | case kDecoderG722: |
henrik.lundin@webrtc.org | aaad613 | 2013-02-01 11:49:28 +0000 | [diff] [blame] | 140 | case kDecoderG722_2ch: |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 141 | #endif |
| 142 | case kDecoderCNGwb: { |
| 143 | return 16000; |
| 144 | } |
| 145 | #ifdef WEBRTC_CODEC_ISAC |
| 146 | case kDecoderISACswb: |
henrik.lundin@webrtc.org | ac59dba | 2013-01-31 09:55:24 +0000 | [diff] [blame] | 147 | case kDecoderISACfb: |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 148 | #endif |
| 149 | #ifdef WEBRTC_CODEC_PCM16 |
| 150 | case kDecoderPCM16Bswb32kHz: |
| 151 | case kDecoderPCM16Bswb32kHz_2ch: |
| 152 | #endif |
turaj@webrtc.org | 6ad6a07 | 2013-09-30 20:07:39 +0000 | [diff] [blame] | 153 | #ifdef WEBRTC_CODEC_CELT |
| 154 | case kDecoderCELT_32: |
| 155 | case kDecoderCELT_32_2ch: |
| 156 | #endif |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 157 | case kDecoderCNGswb32kHz: { |
| 158 | return 32000; |
| 159 | } |
| 160 | #ifdef WEBRTC_CODEC_PCM16 |
| 161 | case kDecoderPCM16Bswb48kHz: |
| 162 | case kDecoderPCM16Bswb48kHz_2ch: { |
| 163 | return 48000; |
| 164 | } |
| 165 | #endif |
| 166 | #ifdef WEBRTC_CODEC_OPUS |
| 167 | case kDecoderOpus: |
| 168 | case kDecoderOpus_2ch: { |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 169 | return 48000; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 170 | } |
| 171 | #endif |
| 172 | case kDecoderCNGswb48kHz: { |
| 173 | // TODO(tlegrand): Remove limitation once ACM has full 48 kHz support. |
| 174 | return 32000; |
| 175 | } |
| 176 | default: { |
| 177 | return -1; // Undefined sample rate. |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | AudioDecoder* AudioDecoder::CreateAudioDecoder(NetEqDecoder codec_type) { |
| 183 | if (!CodecSupported(codec_type)) { |
| 184 | return NULL; |
| 185 | } |
| 186 | switch (codec_type) { |
| 187 | case kDecoderPCMu: |
| 188 | return new AudioDecoderPcmU; |
| 189 | case kDecoderPCMa: |
| 190 | return new AudioDecoderPcmA; |
| 191 | case kDecoderPCMu_2ch: |
| 192 | return new AudioDecoderPcmUMultiCh(2); |
| 193 | case kDecoderPCMa_2ch: |
| 194 | return new AudioDecoderPcmAMultiCh(2); |
| 195 | #ifdef WEBRTC_CODEC_ILBC |
| 196 | case kDecoderILBC: |
| 197 | return new AudioDecoderIlbc; |
| 198 | #endif |
| 199 | #if defined(WEBRTC_CODEC_ISACFX) |
| 200 | case kDecoderISAC: |
| 201 | return new AudioDecoderIsacFix; |
| 202 | #elif defined(WEBRTC_CODEC_ISAC) |
turaj@webrtc.org | 1431e4d | 2014-11-11 01:44:13 +0000 | [diff] [blame] | 203 | case kDecoderISAC: |
| 204 | return new AudioDecoderIsac(16000); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 205 | case kDecoderISACswb: |
turaj@webrtc.org | 1431e4d | 2014-11-11 01:44:13 +0000 | [diff] [blame] | 206 | case kDecoderISACfb: |
| 207 | return new AudioDecoderIsac(32000); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 208 | #endif |
| 209 | #ifdef WEBRTC_CODEC_PCM16 |
| 210 | case kDecoderPCM16B: |
| 211 | case kDecoderPCM16Bwb: |
| 212 | case kDecoderPCM16Bswb32kHz: |
| 213 | case kDecoderPCM16Bswb48kHz: |
kwiberg@webrtc.org | 721ef63 | 2014-11-04 11:51:46 +0000 | [diff] [blame] | 214 | return new AudioDecoderPcm16B; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 215 | case kDecoderPCM16B_2ch: |
| 216 | case kDecoderPCM16Bwb_2ch: |
| 217 | case kDecoderPCM16Bswb32kHz_2ch: |
| 218 | case kDecoderPCM16Bswb48kHz_2ch: |
kwiberg@webrtc.org | 721ef63 | 2014-11-04 11:51:46 +0000 | [diff] [blame] | 219 | return new AudioDecoderPcm16BMultiCh(2); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 220 | case kDecoderPCM16B_5ch: |
kwiberg@webrtc.org | 721ef63 | 2014-11-04 11:51:46 +0000 | [diff] [blame] | 221 | return new AudioDecoderPcm16BMultiCh(5); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 222 | #endif |
| 223 | #ifdef WEBRTC_CODEC_G722 |
| 224 | case kDecoderG722: |
| 225 | return new AudioDecoderG722; |
turaj@webrtc.org | 4b1cd5c | 2013-03-27 20:42:48 +0000 | [diff] [blame] | 226 | case kDecoderG722_2ch: |
| 227 | return new AudioDecoderG722Stereo; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 228 | #endif |
turaj@webrtc.org | 6ad6a07 | 2013-09-30 20:07:39 +0000 | [diff] [blame] | 229 | #ifdef WEBRTC_CODEC_CELT |
| 230 | case kDecoderCELT_32: |
kwiberg@webrtc.org | 721ef63 | 2014-11-04 11:51:46 +0000 | [diff] [blame] | 231 | return new AudioDecoderCelt(1); |
turaj@webrtc.org | 6ad6a07 | 2013-09-30 20:07:39 +0000 | [diff] [blame] | 232 | case kDecoderCELT_32_2ch: |
kwiberg@webrtc.org | 721ef63 | 2014-11-04 11:51:46 +0000 | [diff] [blame] | 233 | return new AudioDecoderCelt(2); |
turaj@webrtc.org | 6ad6a07 | 2013-09-30 20:07:39 +0000 | [diff] [blame] | 234 | #endif |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 235 | #ifdef WEBRTC_CODEC_OPUS |
| 236 | case kDecoderOpus: |
kwiberg@webrtc.org | 721ef63 | 2014-11-04 11:51:46 +0000 | [diff] [blame] | 237 | return new AudioDecoderOpus(1); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 238 | case kDecoderOpus_2ch: |
kwiberg@webrtc.org | 721ef63 | 2014-11-04 11:51:46 +0000 | [diff] [blame] | 239 | return new AudioDecoderOpus(2); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 240 | #endif |
| 241 | case kDecoderCNGnb: |
| 242 | case kDecoderCNGwb: |
| 243 | case kDecoderCNGswb32kHz: |
| 244 | case kDecoderCNGswb48kHz: |
kwiberg@webrtc.org | 721ef63 | 2014-11-04 11:51:46 +0000 | [diff] [blame] | 245 | return new AudioDecoderCng; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 246 | case kDecoderRED: |
| 247 | case kDecoderAVT: |
| 248 | case kDecoderArbitrary: |
| 249 | default: { |
| 250 | return NULL; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | AudioDecoder::SpeechType AudioDecoder::ConvertSpeechType(int16_t type) { |
| 256 | switch (type) { |
| 257 | case 0: // TODO(hlundin): Both iSAC and Opus return 0 for speech. |
| 258 | case 1: |
| 259 | return kSpeech; |
| 260 | case 2: |
| 261 | return kComfortNoise; |
| 262 | default: |
| 263 | assert(false); |
| 264 | return kSpeech; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | } // namespace webrtc |