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 { |
| 26 | EncodedInfoLeaf() |
henrik.lundin@webrtc.org | bb1219e | 2015-02-12 15:53:25 +0000 | [diff] [blame] | 27 | : encoded_bytes(0), |
| 28 | encoded_timestamp(0), |
| 29 | payload_type(0), |
henrik.lundin@webrtc.org | c86bbba | 2015-03-04 16:02:42 +0000 | [diff] [blame] | 30 | send_even_if_empty(false), |
| 31 | speech(true) {} |
henrik.lundin@webrtc.org | 3b79daf | 2014-12-12 13:31:24 +0000 | [diff] [blame] | 32 | |
| 33 | size_t encoded_bytes; |
henrik.lundin@webrtc.org | 1db20a4 | 2014-12-01 14:44:50 +0000 | [diff] [blame] | 34 | uint32_t encoded_timestamp; |
henrik.lundin@webrtc.org | 7f1dfa5 | 2014-12-02 12:08:39 +0000 | [diff] [blame] | 35 | int payload_type; |
henrik.lundin@webrtc.org | bb1219e | 2015-02-12 15:53:25 +0000 | [diff] [blame] | 36 | bool send_even_if_empty; |
henrik.lundin@webrtc.org | c86bbba | 2015-03-04 16:02:42 +0000 | [diff] [blame] | 37 | bool speech; |
henrik.lundin@webrtc.org | 1db20a4 | 2014-12-01 14:44:50 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
henrik.lundin@webrtc.org | c1c9291 | 2014-12-16 13:41:36 +0000 | [diff] [blame] | 40 | // This is the main struct for auxiliary encoding information. Each encoded |
| 41 | // packet should be accompanied by one EncodedInfo struct, containing the |
| 42 | // total number of |encoded_bytes|, the |encoded_timestamp| and the |
| 43 | // |payload_type|. If the packet contains redundant encodings, the |redundant| |
| 44 | // vector will be populated with EncodedInfoLeaf structs. Each struct in the |
| 45 | // vector represents one encoding; the order of structs in the vector is the |
| 46 | // same as the order in which the actual payloads are written to the byte |
| 47 | // stream. When EncoderInfoLeaf structs are present in the vector, the main |
| 48 | // struct's |encoded_bytes| will be the sum of all the |encoded_bytes| in the |
| 49 | // vector. |
| 50 | struct EncodedInfo : public EncodedInfoLeaf { |
| 51 | EncodedInfo(); |
| 52 | ~EncodedInfo(); |
| 53 | |
| 54 | std::vector<EncodedInfoLeaf> redundant; |
| 55 | }; |
| 56 | |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 57 | virtual ~AudioEncoder() {} |
| 58 | |
| 59 | // Accepts one 10 ms block of input audio (i.e., sample_rate_hz() / 100 * |
| 60 | // num_channels() samples). Multi-channel audio must be sample-interleaved. |
jmarusic@webrtc.org | 9afaee7 | 2015-03-19 08:50:26 +0000 | [diff] [blame] | 61 | // The encoder produces zero or more bytes of output in |encoded| and |
| 62 | // returns additional encoding information. |
jmarusic@webrtc.org | b1f0de3 | 2015-02-26 15:38:10 +0000 | [diff] [blame] | 63 | // The caller is responsible for making sure that |max_encoded_bytes| is |
| 64 | // not smaller than the number of bytes actually produced by the encoder. |
jmarusic@webrtc.org | 9afaee7 | 2015-03-19 08:50:26 +0000 | [diff] [blame] | 65 | EncodedInfo Encode(uint32_t rtp_timestamp, |
| 66 | const int16_t* audio, |
| 67 | size_t num_samples_per_channel, |
| 68 | size_t max_encoded_bytes, |
| 69 | uint8_t* encoded); |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 70 | |
kwiberg@webrtc.org | decd930 | 2014-10-29 08:38:50 +0000 | [diff] [blame] | 71 | // Return the input sample rate in Hz and the number of input channels. |
| 72 | // These are constants set at instantiation time. |
kwiberg@webrtc.org | 0521127 | 2015-02-18 12:00:32 +0000 | [diff] [blame] | 73 | virtual int SampleRateHz() const = 0; |
| 74 | virtual int NumChannels() const = 0; |
kwiberg@webrtc.org | decd930 | 2014-10-29 08:38:50 +0000 | [diff] [blame] | 75 | |
jmarusic@webrtc.org | 51ccf37 | 2015-03-10 15:41:26 +0000 | [diff] [blame] | 76 | // Return the maximum number of bytes that can be produced by the encoder |
| 77 | // at each Encode() call. The caller can use the return value to determine |
| 78 | // the size of the buffer that needs to be allocated. This value is allowed |
| 79 | // to depend on encoder parameters like bitrate, frame size etc., so if |
| 80 | // any of these change, the caller of Encode() is responsible for checking |
| 81 | // that the buffer is large enough by calling MaxEncodedBytes() again. |
| 82 | virtual size_t MaxEncodedBytes() const = 0; |
| 83 | |
henrik.lundin@webrtc.org | 478cedc | 2015-01-27 18:24:45 +0000 | [diff] [blame] | 84 | // Returns the rate with which the RTP timestamps are updated. By default, |
| 85 | // this is the same as sample_rate_hz(). |
kwiberg@webrtc.org | 0521127 | 2015-02-18 12:00:32 +0000 | [diff] [blame] | 86 | virtual int RtpTimestampRateHz() const; |
henrik.lundin@webrtc.org | 478cedc | 2015-01-27 18:24:45 +0000 | [diff] [blame] | 87 | |
kwiberg@webrtc.org | decd930 | 2014-10-29 08:38:50 +0000 | [diff] [blame] | 88 | // Returns the number of 10 ms frames the encoder will put in the next |
| 89 | // packet. This value may only change when Encode() outputs a packet; i.e., |
| 90 | // the encoder may vary the number of 10 ms frames from packet to packet, but |
| 91 | // it must decide the length of the next packet no later than when outputting |
| 92 | // the preceding packet. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame^] | 93 | virtual size_t Num10MsFramesInNextPacket() const = 0; |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 94 | |
henrik.lundin@webrtc.org | 8911bc5 | 2014-12-08 21:15:55 +0000 | [diff] [blame] | 95 | // Returns the maximum value that can be returned by |
| 96 | // Num10MsFramesInNextPacket(). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame^] | 97 | virtual size_t Max10MsFramesInAPacket() const = 0; |
henrik.lundin@webrtc.org | 8911bc5 | 2014-12-08 21:15:55 +0000 | [diff] [blame] | 98 | |
Henrik Lundin | 3e89dbf | 2015-06-18 14:58:34 +0200 | [diff] [blame] | 99 | // Returns the current target bitrate in bits/s. The value -1 means that the |
| 100 | // codec adapts the target automatically, and a current target cannot be |
| 101 | // provided. |
| 102 | virtual int GetTargetBitrate() const = 0; |
| 103 | |
henrik.lundin@webrtc.org | 478cedc | 2015-01-27 18:24:45 +0000 | [diff] [blame] | 104 | // Changes the target bitrate. The implementation is free to alter this value, |
| 105 | // e.g., if the desired value is outside the valid range. |
| 106 | virtual void SetTargetBitrate(int bits_per_second) {} |
| 107 | |
| 108 | // Tells the implementation what the projected packet loss rate is. The rate |
| 109 | // is in the range [0.0, 1.0]. This rate is typically used to adjust channel |
| 110 | // coding efforts, such as FEC. |
| 111 | virtual void SetProjectedPacketLossRate(double fraction) {} |
| 112 | |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 113 | // This is the encode function that the inherited classes must implement. It |
| 114 | // is called from Encode in the base class. |
jmarusic@webrtc.org | 9afaee7 | 2015-03-19 08:50:26 +0000 | [diff] [blame] | 115 | virtual EncodedInfo EncodeInternal(uint32_t rtp_timestamp, |
| 116 | const int16_t* audio, |
| 117 | size_t max_encoded_bytes, |
| 118 | uint8_t* encoded) = 0; |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 121 | class AudioEncoderMutable : public AudioEncoder { |
| 122 | public: |
| 123 | enum Application { kApplicationSpeech, kApplicationAudio }; |
| 124 | |
| 125 | // Discards unprocessed audio data. |
| 126 | virtual void Reset() = 0; |
| 127 | |
| 128 | // Enables codec-internal FEC, if the implementation supports it. |
| 129 | virtual bool SetFec(bool enable) = 0; |
| 130 | |
| 131 | // Enables or disables codec-internal VAD/DTX, if the implementation supports |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 132 | // it. |
| 133 | virtual bool SetDtx(bool enable) = 0; |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 134 | |
| 135 | // Sets the application mode. The implementation is free to disregard this |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 136 | // setting. |
| 137 | virtual bool SetApplication(Application application) = 0; |
Karl Wiberg | dcccab3 | 2015-05-07 12:35:12 +0200 | [diff] [blame] | 138 | |
| 139 | // Sets an upper limit on the payload size produced by the encoder. The |
| 140 | // implementation is free to disregard this setting. |
| 141 | virtual void SetMaxPayloadSize(int max_payload_size_bytes) = 0; |
| 142 | |
| 143 | // Sets the maximum rate which the codec may not exceed for any packet. |
| 144 | virtual void SetMaxRate(int max_rate_bps) = 0; |
| 145 | |
| 146 | // Informs the encoder about the maximum sample rate which the decoder will |
| 147 | // use when decoding the bitstream. The implementation is free to disregard |
| 148 | // this hint. |
| 149 | virtual bool SetMaxPlaybackRate(int frequency_hz) = 0; |
| 150 | }; |
henrik.lundin@webrtc.org | 9ea6f8a | 2014-10-16 11:26:24 +0000 | [diff] [blame] | 151 | } // namespace webrtc |
| 152 | #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_ |