henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_ |
| 13 | |
| 14 | #include <algorithm> |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 15 | #include <vector> |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 16 | |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 17 | #include "webrtc/base/array_view.h" |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 18 | #include "webrtc/base/buffer.h" |
| 19 | #include "webrtc/base/deprecation.h" |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 20 | #include "webrtc/typedefs.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | // This is the interface class for encoders in AudioCoding module. Each codec |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 25 | // type must have an implementation of this class. |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 26 | class AudioEncoder { |
| 27 | public: |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 28 | struct EncodedInfoLeaf { |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 29 | size_t encoded_bytes = 0; |
| 30 | uint32_t encoded_timestamp = 0; |
| 31 | int payload_type = 0; |
| 32 | bool send_even_if_empty = false; |
| 33 | bool speech = true; |
henrik.lundin@webrtc.org | 1db20a4 | 2014-12-01 14:44:50 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 36 | // This is the main struct for auxiliary encoding information. Each encoded |
| 37 | // packet should be accompanied by one EncodedInfo struct, containing the |
| 38 | // total number of |encoded_bytes|, the |encoded_timestamp| and the |
| 39 | // |payload_type|. If the packet contains redundant encodings, the |redundant| |
| 40 | // vector will be populated with EncodedInfoLeaf structs. Each struct in the |
| 41 | // vector represents one encoding; the order of structs in the vector is the |
| 42 | // same as the order in which the actual payloads are written to the byte |
| 43 | // stream. When EncoderInfoLeaf structs are present in the vector, the main |
| 44 | // struct's |encoded_bytes| will be the sum of all the |encoded_bytes| in the |
| 45 | // vector. |
| 46 | struct EncodedInfo : public EncodedInfoLeaf { |
| 47 | EncodedInfo(); |
| 48 | ~EncodedInfo(); |
| 49 | |
| 50 | std::vector<EncodedInfoLeaf> redundant; |
| 51 | }; |
| 52 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 53 | virtual ~AudioEncoder() = default; |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 54 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 55 | // Returns the maximum number of bytes that can be produced by the encoder |
jmarusic@webrtc.org | 51ccf37 | 2015-03-10 15:41:26 +0000 | [diff] [blame] | 56 | // at each Encode() call. The caller can use the return value to determine |
| 57 | // the size of the buffer that needs to be allocated. This value is allowed |
| 58 | // to depend on encoder parameters like bitrate, frame size etc., so if |
| 59 | // any of these change, the caller of Encode() is responsible for checking |
| 60 | // that the buffer is large enough by calling MaxEncodedBytes() again. |
| 61 | virtual size_t MaxEncodedBytes() const = 0; |
| 62 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 63 | // Returns the input sample rate in Hz and the number of input channels. |
| 64 | // These are constants set at instantiation time. |
| 65 | virtual int SampleRateHz() const = 0; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 66 | virtual size_t NumChannels() const = 0; |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 67 | |
| 68 | // Returns the rate at which the RTP timestamps are updated. The default |
| 69 | // implementation returns SampleRateHz(). |
kwiberg@webrtc.org | 0521127 | 2015-02-18 12:00:32 +0000 | [diff] [blame] | 70 | virtual int RtpTimestampRateHz() const; |
henrik.lundin@webrtc.org | 478cedc | 2015-01-27 18:24:45 +0000 | [diff] [blame] | 71 | |
kwiberg@webrtc.org | decd930 | 2014-10-29 08:38:50 +0000 | [diff] [blame] | 72 | // Returns the number of 10 ms frames the encoder will put in the next |
| 73 | // packet. This value may only change when Encode() outputs a packet; i.e., |
| 74 | // the encoder may vary the number of 10 ms frames from packet to packet, but |
| 75 | // it must decide the length of the next packet no later than when outputting |
| 76 | // the preceding packet. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 77 | virtual size_t Num10MsFramesInNextPacket() const = 0; |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 78 | |
henrik.lundin@webrtc.org | 8911bc5 | 2014-12-08 21:15:55 +0000 | [diff] [blame] | 79 | // Returns the maximum value that can be returned by |
| 80 | // Num10MsFramesInNextPacket(). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 81 | virtual size_t Max10MsFramesInAPacket() const = 0; |
henrik.lundin@webrtc.org | 8911bc5 | 2014-12-08 21:15:55 +0000 | [diff] [blame] | 82 | |
Henrik Lundin | 3e89dbf | 2015-06-18 14:58:34 +0200 | [diff] [blame] | 83 | // Returns the current target bitrate in bits/s. The value -1 means that the |
| 84 | // codec adapts the target automatically, and a current target cannot be |
| 85 | // provided. |
| 86 | virtual int GetTargetBitrate() const = 0; |
| 87 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 88 | // Accepts one 10 ms block of input audio (i.e., SampleRateHz() / 100 * |
| 89 | // NumChannels() samples). Multi-channel audio must be sample-interleaved. |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 90 | // The encoder appends zero or more bytes of output to |encoded| and returns |
| 91 | // additional encoding information. Encode() checks some preconditions, calls |
ossu | 4f43fcf | 2016-03-04 00:54:32 -0800 | [diff] [blame^] | 92 | // EncodeImpl() which does the actual work, and then checks some |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 93 | // postconditions. |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 94 | EncodedInfo Encode(uint32_t rtp_timestamp, |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 95 | rtc::ArrayView<const int16_t> audio, |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 96 | rtc::Buffer* encoded); |
henrik.lundin@webrtc.org | 478cedc | 2015-01-27 18:24:45 +0000 | [diff] [blame] | 97 | |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 98 | // Deprecated interface to Encode (remove eventually, bug 5591). May incur a |
| 99 | // copy. The encoder produces zero or more bytes of output in |encoded| and |
| 100 | // returns additional encoding information. The caller is responsible for |
| 101 | // making sure that |max_encoded_bytes| is not smaller than the number of |
| 102 | // bytes actually produced by the encoder. |
| 103 | RTC_DEPRECATED EncodedInfo Encode(uint32_t rtp_timestamp, |
| 104 | rtc::ArrayView<const int16_t> audio, |
| 105 | size_t max_encoded_bytes, |
| 106 | uint8_t* encoded); |
| 107 | |
| 108 | EncodedInfo DEPRECATED_Encode(uint32_t rtp_timestamp, |
| 109 | rtc::ArrayView<const int16_t> audio, |
| 110 | size_t max_encoded_bytes, |
| 111 | uint8_t* encoded); |
| 112 | |
ossu | 4f43fcf | 2016-03-04 00:54:32 -0800 | [diff] [blame^] | 113 | // Deprecated interface EncodeInternal (see bug 5591). May incur a copy. |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 114 | // Subclasses implement this to perform the actual encoding. Called by |
| 115 | // Encode(). By default, this is implemented as a call to the newer |
ossu | 4f43fcf | 2016-03-04 00:54:32 -0800 | [diff] [blame^] | 116 | // EncodeImpl() that accepts an rtc::Buffer instead of a raw pointer. |
| 117 | // That version is protected, so see below. At least one of EncodeInternal |
| 118 | // or EncodeImpl _must_ be implemented by a subclass. |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 119 | virtual EncodedInfo EncodeInternal( |
| 120 | uint32_t rtp_timestamp, |
| 121 | rtc::ArrayView<const int16_t> audio, |
| 122 | size_t max_encoded_bytes, |
| 123 | uint8_t* encoded); |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 124 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 125 | // Resets the encoder to its starting state, discarding any input that has |
| 126 | // been fed to the encoder but not yet emitted in a packet. |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 127 | virtual void Reset() = 0; |
| 128 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 129 | // Enables or disables codec-internal FEC (forward error correction). Returns |
| 130 | // true if the codec was able to comply. The default implementation returns |
| 131 | // true when asked to disable FEC and false when asked to enable it (meaning |
| 132 | // that FEC isn't supported). |
| 133 | virtual bool SetFec(bool enable); |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 134 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 135 | // Enables or disables codec-internal VAD/DTX. Returns true if the codec was |
| 136 | // able to comply. The default implementation returns true when asked to |
| 137 | // disable DTX and false when asked to enable it (meaning that DTX isn't |
| 138 | // supported). |
| 139 | virtual bool SetDtx(bool enable); |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 140 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 141 | // Sets the application mode. Returns true if the codec was able to comply. |
| 142 | // The default implementation just returns false. |
| 143 | enum class Application { kSpeech, kAudio }; |
| 144 | virtual bool SetApplication(Application application); |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 145 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 146 | // Tells the encoder about the highest sample rate the decoder is expected to |
| 147 | // use when decoding the bitstream. The encoder would typically use this |
| 148 | // information to adjust the quality of the encoding. The default |
kwiberg | 7eb914d | 2015-12-15 14:20:24 -0800 | [diff] [blame] | 149 | // implementation does nothing. |
kwiberg | 3f5f1c2 | 2015-09-08 23:15:33 -0700 | [diff] [blame] | 150 | virtual void SetMaxPlaybackRate(int frequency_hz); |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 151 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 152 | // Tells the encoder what the projected packet loss rate is. The rate is in |
| 153 | // the range [0.0, 1.0]. The encoder would typically use this information to |
| 154 | // adjust channel coding efforts, such as FEC. The default implementation |
| 155 | // does nothing. |
| 156 | virtual void SetProjectedPacketLossRate(double fraction); |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 157 | |
kwiberg | 12cfc9b | 2015-09-08 05:57:53 -0700 | [diff] [blame] | 158 | // Tells the encoder what average bitrate we'd like it to produce. The |
| 159 | // encoder is free to adjust or disregard the given bitrate (the default |
| 160 | // implementation does the latter). |
| 161 | virtual void SetTargetBitrate(int target_bps); |
ossu | 10a029e | 2016-03-01 00:41:31 -0800 | [diff] [blame] | 162 | |
| 163 | protected: |
| 164 | // Subclasses implement this to perform the actual encoding. Called by |
| 165 | // Encode(). For compatibility reasons, this is implemented by default as a |
ossu | 4f43fcf | 2016-03-04 00:54:32 -0800 | [diff] [blame^] | 166 | // call to the older interface EncodeInternal(). At least one of |
| 167 | // EncodeInternal or EncodeImpl _must_ be implemented by a |
| 168 | // subclass. Preferably this one. |
| 169 | virtual EncodedInfo EncodeImpl(uint32_t rtp_timestamp, |
| 170 | rtc::ArrayView<const int16_t> audio, |
| 171 | rtc::Buffer* encoded); |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 172 | }; |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 173 | } // namespace webrtc |
| 174 | #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_ |