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 Kjellander | 7464089 | 2015-10-29 11:31:02 +0100 | [diff] [blame^] | 11 | #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_INCLUDE_AUDIO_DECODER_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_CODING_NETEQ_INCLUDE_AUDIO_DECODER_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
| 14 | #include <stdlib.h> // NULL |
| 15 | |
henrike@webrtc.org | 88fbb2d | 2014-05-21 21:18:46 +0000 | [diff] [blame] | 16 | #include "webrtc/base/constructormagic.h" |
kwiberg@webrtc.org | 8b2058e | 2014-11-06 07:54:31 +0000 | [diff] [blame] | 17 | #include "webrtc/modules/audio_coding/codecs/cng/include/webrtc_cng.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 18 | #include "webrtc/typedefs.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 22 | // This is the interface class for decoders in NetEQ. Each codec type will have |
| 23 | // and implementation of this class. |
| 24 | class AudioDecoder { |
| 25 | public: |
| 26 | enum SpeechType { |
| 27 | kSpeech = 1, |
| 28 | kComfortNoise = 2 |
| 29 | }; |
| 30 | |
| 31 | // Used by PacketDuration below. Save the value -1 for errors. |
| 32 | enum { kNotImplemented = -2 }; |
| 33 | |
henrik.lundin@webrtc.org | 6dba1eb | 2015-03-18 09:47:08 +0000 | [diff] [blame] | 34 | AudioDecoder() = default; |
| 35 | virtual ~AudioDecoder() = default; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 36 | |
| 37 | // Decodes |encode_len| bytes from |encoded| and writes the result in |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 38 | // |decoded|. The maximum bytes allowed to be written into |decoded| is |
Minyue | 323b132 | 2015-05-25 13:49:37 +0200 | [diff] [blame] | 39 | // |max_decoded_bytes|. Returns the total number of samples across all |
| 40 | // channels. If the decoder produced comfort noise, |speech_type| |
henrik.lundin@webrtc.org | 1eda4e3 | 2015-02-25 10:02:29 +0000 | [diff] [blame] | 41 | // is set to kComfortNoise, otherwise it is kSpeech. The desired output |
| 42 | // sample rate is provided in |sample_rate_hz|, which must be valid for the |
| 43 | // codec at hand. |
| 44 | virtual int Decode(const uint8_t* encoded, |
| 45 | size_t encoded_len, |
| 46 | int sample_rate_hz, |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 47 | size_t max_decoded_bytes, |
henrik.lundin@webrtc.org | 1eda4e3 | 2015-02-25 10:02:29 +0000 | [diff] [blame] | 48 | int16_t* decoded, |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 49 | SpeechType* speech_type); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 50 | |
| 51 | // Same as Decode(), but interfaces to the decoders redundant decode function. |
| 52 | // The default implementation simply calls the regular Decode() method. |
henrik.lundin@webrtc.org | 1eda4e3 | 2015-02-25 10:02:29 +0000 | [diff] [blame] | 53 | virtual int DecodeRedundant(const uint8_t* encoded, |
| 54 | size_t encoded_len, |
| 55 | int sample_rate_hz, |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 56 | size_t max_decoded_bytes, |
henrik.lundin@webrtc.org | 1eda4e3 | 2015-02-25 10:02:29 +0000 | [diff] [blame] | 57 | int16_t* decoded, |
| 58 | SpeechType* speech_type); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 59 | |
| 60 | // Indicates if the decoder implements the DecodePlc method. |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 61 | virtual bool HasDecodePlc() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 62 | |
| 63 | // Calls the packet-loss concealment of the decoder to update the state after |
minyuel | 6d92bf5 | 2015-09-23 15:20:39 +0200 | [diff] [blame] | 64 | // one or several lost packets. The caller has to make sure that the |
| 65 | // memory allocated in |decoded| should accommodate |num_frames| frames. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 66 | virtual size_t DecodePlc(size_t num_frames, int16_t* decoded); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 67 | |
Karl Wiberg | 4376648 | 2015-08-27 15:22:11 +0200 | [diff] [blame] | 68 | // Resets the decoder state (empty buffers etc.). |
| 69 | virtual void Reset() = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 70 | |
| 71 | // Notifies the decoder of an incoming packet to NetEQ. |
| 72 | virtual int IncomingPacket(const uint8_t* payload, |
| 73 | size_t payload_len, |
| 74 | uint16_t rtp_sequence_number, |
| 75 | uint32_t rtp_timestamp, |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 76 | uint32_t arrival_timestamp); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 77 | |
| 78 | // Returns the last error code from the decoder. |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 79 | virtual int ErrorCode(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 80 | |
Minyue | 323b132 | 2015-05-25 13:49:37 +0200 | [diff] [blame] | 81 | // Returns the duration in samples-per-channel of the payload in |encoded| |
| 82 | // which is |encoded_len| bytes long. Returns kNotImplemented if no duration |
| 83 | // estimate is available, or -1 in case of an error. |
minyue@webrtc.org | a8cc344 | 2015-02-13 14:01:54 +0000 | [diff] [blame] | 84 | virtual int PacketDuration(const uint8_t* encoded, size_t encoded_len) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 85 | |
Minyue | 323b132 | 2015-05-25 13:49:37 +0200 | [diff] [blame] | 86 | // Returns the duration in samples-per-channel of the redandant payload in |
| 87 | // |encoded| which is |encoded_len| bytes long. Returns kNotImplemented if no |
| 88 | // duration estimate is available, or -1 in case of an error. |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 89 | virtual int PacketDurationRedundant(const uint8_t* encoded, |
| 90 | size_t encoded_len) const; |
| 91 | |
| 92 | // Detects whether a packet has forward error correction. The packet is |
| 93 | // comprised of the samples in |encoded| which is |encoded_len| bytes long. |
| 94 | // Returns true if the packet has FEC and false otherwise. |
| 95 | virtual bool PacketHasFec(const uint8_t* encoded, size_t encoded_len) const; |
| 96 | |
kwiberg@webrtc.org | 8b2058e | 2014-11-06 07:54:31 +0000 | [diff] [blame] | 97 | // If this is a CNG decoder, return the underlying CNG_dec_inst*. If this |
| 98 | // isn't a CNG decoder, don't call this method. |
| 99 | virtual CNG_dec_inst* CngDecoderInstance(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 100 | |
henrik.lundin@webrtc.org | 6dba1eb | 2015-03-18 09:47:08 +0000 | [diff] [blame] | 101 | virtual size_t Channels() const = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 102 | |
| 103 | protected: |
| 104 | static SpeechType ConvertSpeechType(int16_t type); |
| 105 | |
minyue@webrtc.org | 7f7d7e3 | 2015-03-16 12:30:37 +0000 | [diff] [blame] | 106 | virtual int DecodeInternal(const uint8_t* encoded, |
| 107 | size_t encoded_len, |
| 108 | int sample_rate_hz, |
| 109 | int16_t* decoded, |
| 110 | SpeechType* speech_type); |
| 111 | |
| 112 | virtual int DecodeRedundantInternal(const uint8_t* encoded, |
| 113 | size_t encoded_len, |
| 114 | int sample_rate_hz, |
| 115 | int16_t* decoded, |
| 116 | SpeechType* speech_type); |
| 117 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 118 | private: |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 119 | RTC_DISALLOW_COPY_AND_ASSIGN(AudioDecoder); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace webrtc |
Henrik Kjellander | 7464089 | 2015-10-29 11:31:02 +0100 | [diff] [blame^] | 123 | #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_INCLUDE_AUDIO_DECODER_H_ |