blob: ebb5d36bfd5c1b8f55d8d07d1e618e4c33c84754 [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
kwiberg288886b2015-11-06 01:21:35 -080017#include "webrtc/base/array_view.h"
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000018#include "webrtc/typedefs.h"
19
20namespace webrtc {
21
22// This is the interface class for encoders in AudioCoding module. Each codec
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000023// type must have an implementation of this class.
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000024class AudioEncoder {
25 public:
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000026 struct EncodedInfoLeaf {
kwiberg12cfc9b2015-09-08 05:57:53 -070027 size_t encoded_bytes = 0;
28 uint32_t encoded_timestamp = 0;
29 int payload_type = 0;
30 bool send_even_if_empty = false;
31 bool speech = true;
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000032 };
33
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000034 // This is the main struct for auxiliary encoding information. Each encoded
35 // packet should be accompanied by one EncodedInfo struct, containing the
36 // total number of |encoded_bytes|, the |encoded_timestamp| and the
37 // |payload_type|. If the packet contains redundant encodings, the |redundant|
38 // vector will be populated with EncodedInfoLeaf structs. Each struct in the
39 // vector represents one encoding; the order of structs in the vector is the
40 // same as the order in which the actual payloads are written to the byte
41 // stream. When EncoderInfoLeaf structs are present in the vector, the main
42 // struct's |encoded_bytes| will be the sum of all the |encoded_bytes| in the
43 // vector.
44 struct EncodedInfo : public EncodedInfoLeaf {
45 EncodedInfo();
46 ~EncodedInfo();
47
48 std::vector<EncodedInfoLeaf> redundant;
49 };
50
kwiberg12cfc9b2015-09-08 05:57:53 -070051 virtual ~AudioEncoder() = default;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000052
kwiberg12cfc9b2015-09-08 05:57:53 -070053 // Returns the maximum number of bytes that can be produced by the encoder
jmarusic@webrtc.org51ccf372015-03-10 15:41:26 +000054 // at each Encode() call. The caller can use the return value to determine
55 // the size of the buffer that needs to be allocated. This value is allowed
56 // to depend on encoder parameters like bitrate, frame size etc., so if
57 // any of these change, the caller of Encode() is responsible for checking
58 // that the buffer is large enough by calling MaxEncodedBytes() again.
59 virtual size_t MaxEncodedBytes() const = 0;
60
kwiberg12cfc9b2015-09-08 05:57:53 -070061 // Returns the input sample rate in Hz and the number of input channels.
62 // These are constants set at instantiation time.
63 virtual int SampleRateHz() const = 0;
64 virtual int NumChannels() const = 0;
65
66 // Returns the rate at which the RTP timestamps are updated. The default
67 // implementation returns SampleRateHz().
kwiberg@webrtc.org05211272015-02-18 12:00:32 +000068 virtual int RtpTimestampRateHz() const;
henrik.lundin@webrtc.org478cedc2015-01-27 18:24:45 +000069
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000070 // Returns the number of 10 ms frames the encoder will put in the next
71 // packet. This value may only change when Encode() outputs a packet; i.e.,
72 // the encoder may vary the number of 10 ms frames from packet to packet, but
73 // it must decide the length of the next packet no later than when outputting
74 // the preceding packet.
Peter Kastingdce40cf2015-08-24 14:52:23 -070075 virtual size_t Num10MsFramesInNextPacket() const = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000076
henrik.lundin@webrtc.org8911bc52014-12-08 21:15:55 +000077 // Returns the maximum value that can be returned by
78 // Num10MsFramesInNextPacket().
Peter Kastingdce40cf2015-08-24 14:52:23 -070079 virtual size_t Max10MsFramesInAPacket() const = 0;
henrik.lundin@webrtc.org8911bc52014-12-08 21:15:55 +000080
Henrik Lundin3e89dbf2015-06-18 14:58:34 +020081 // Returns the current target bitrate in bits/s. The value -1 means that the
82 // codec adapts the target automatically, and a current target cannot be
83 // provided.
84 virtual int GetTargetBitrate() const = 0;
85
kwiberg12cfc9b2015-09-08 05:57:53 -070086 // Accepts one 10 ms block of input audio (i.e., SampleRateHz() / 100 *
87 // NumChannels() samples). Multi-channel audio must be sample-interleaved.
88 // The encoder produces zero or more bytes of output in |encoded| and
89 // returns additional encoding information.
90 // The caller is responsible for making sure that |max_encoded_bytes| is
91 // not smaller than the number of bytes actually produced by the encoder.
92 // Encode() checks some preconditions, calls EncodeInternal() which does the
93 // actual work, and then checks some postconditions.
94 EncodedInfo Encode(uint32_t rtp_timestamp,
kwiberg288886b2015-11-06 01:21:35 -080095 rtc::ArrayView<const int16_t> audio,
kwiberg12cfc9b2015-09-08 05:57:53 -070096 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,
kwiberg288886b2015-11-06 01:21:35 -0800100 rtc::ArrayView<const int16_t> audio,
jmarusic@webrtc.org9afaee72015-03-19 08:50:26 +0000101 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
kwiberg7eb914d2015-12-15 14:20:24 -0800128 // implementation does nothing.
kwiberg3f5f1c22015-09-08 23:15:33 -0700129 virtual void SetMaxPlaybackRate(int frequency_hz);
Karl Wibergdcccab32015-05-07 12:35:12 +0200130
kwiberg12cfc9b2015-09-08 05:57:53 -0700131 // Tells the encoder what the projected packet loss rate is. The rate is in
132 // the range [0.0, 1.0]. The encoder would typically use this information to
133 // adjust channel coding efforts, such as FEC. The default implementation
134 // does nothing.
135 virtual void SetProjectedPacketLossRate(double fraction);
Karl Wibergdcccab32015-05-07 12:35:12 +0200136
kwiberg12cfc9b2015-09-08 05:57:53 -0700137 // Tells the encoder what average bitrate we'd like it to produce. The
138 // encoder is free to adjust or disregard the given bitrate (the default
139 // implementation does the latter).
140 virtual void SetTargetBitrate(int target_bps);
Karl Wibergdcccab32015-05-07 12:35:12 +0200141};
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +0000142} // namespace webrtc
143#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_