blob: 73b4468d4657ea5c58af8067435931f66e9a3351 [file] [log] [blame]
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +00001/*
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.orgc1c92912014-12-16 13:41:36 +000015#include <vector>
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000016
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000017#include "webrtc/typedefs.h"
18
19namespace webrtc {
20
21// This is the interface class for encoders in AudioCoding module. Each codec
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000022// type must have an implementation of this class.
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000023class AudioEncoder {
24 public:
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000025 struct EncodedInfoLeaf {
kwiberg12cfc9b2015-09-08 05:57:53 -070026 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.org1db20a42014-12-01 14:44:50 +000031 };
32
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000033 // 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
kwiberg12cfc9b2015-09-08 05:57:53 -070050 virtual ~AudioEncoder() = default;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000051
kwiberg12cfc9b2015-09-08 05:57:53 -070052 // Returns the maximum number of bytes that can be produced by the encoder
jmarusic@webrtc.org51ccf372015-03-10 15:41:26 +000053 // 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
kwiberg12cfc9b2015-09-08 05:57:53 -070060 // 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.org05211272015-02-18 12:00:32 +000067 virtual int RtpTimestampRateHz() const;
henrik.lundin@webrtc.org478cedc2015-01-27 18:24:45 +000068
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000069 // 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 Kastingdce40cf2015-08-24 14:52:23 -070074 virtual size_t Num10MsFramesInNextPacket() const = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000075
henrik.lundin@webrtc.org8911bc52014-12-08 21:15:55 +000076 // Returns the maximum value that can be returned by
77 // Num10MsFramesInNextPacket().
Peter Kastingdce40cf2015-08-24 14:52:23 -070078 virtual size_t Max10MsFramesInAPacket() const = 0;
henrik.lundin@webrtc.org8911bc52014-12-08 21:15:55 +000079
Henrik Lundin3e89dbf2015-06-18 14:58:34 +020080 // 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
kwiberg12cfc9b2015-09-08 05:57:53 -070085 // 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.org478cedc2015-01-27 18:24:45 +000098
jmarusic@webrtc.org9afaee72015-03-19 08:50:26 +000099 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.org9ea6f8a2014-10-16 11:26:24 +0000103
kwiberg12cfc9b2015-09-08 05:57:53 -0700104 // 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 Wibergdcccab32015-05-07 12:35:12 +0200106 virtual void Reset() = 0;
107
kwiberg12cfc9b2015-09-08 05:57:53 -0700108 // 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 Wibergdcccab32015-05-07 12:35:12 +0200113
kwiberg12cfc9b2015-09-08 05:57:53 -0700114 // 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 Wibergdcccab32015-05-07 12:35:12 +0200119
kwiberg12cfc9b2015-09-08 05:57:53 -0700120 // 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 Wibergdcccab32015-05-07 12:35:12 +0200124
kwiberg12cfc9b2015-09-08 05:57:53 -0700125 // 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 Wibergdcccab32015-05-07 12:35:12 +0200132
kwiberg12cfc9b2015-09-08 05:57:53 -0700133 // 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 Wibergdcccab32015-05-07 12:35:12 +0200138
kwiberg12cfc9b2015-09-08 05:57:53 -0700139 // 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 Wibergdcccab32015-05-07 12:35:12 +0200153};
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +0000154} // namespace webrtc
155#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_