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