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