blob: 20ac8b922bae916628b4d90ff393583f0e9a7682 [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 {
26 EncodedInfoLeaf()
henrik.lundin@webrtc.orgbb1219e2015-02-12 15:53:25 +000027 : encoded_bytes(0),
28 encoded_timestamp(0),
29 payload_type(0),
henrik.lundin@webrtc.orgc86bbba2015-03-04 16:02:42 +000030 send_even_if_empty(false),
31 speech(true) {}
henrik.lundin@webrtc.org3b79daf2014-12-12 13:31:24 +000032
33 size_t encoded_bytes;
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000034 uint32_t encoded_timestamp;
henrik.lundin@webrtc.org7f1dfa52014-12-02 12:08:39 +000035 int payload_type;
henrik.lundin@webrtc.orgbb1219e2015-02-12 15:53:25 +000036 bool send_even_if_empty;
henrik.lundin@webrtc.orgc86bbba2015-03-04 16:02:42 +000037 bool speech;
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000038 };
39
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000040 // 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.org9ea6f8a2014-10-16 11:26:24 +000057 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.orgb1f0de32015-02-26 15:38:10 +000061 // The encoder produces zero or more bytes of output in |encoded|,
jmarusic@webrtc.orgabbdd522015-02-27 09:20:01 +000062 // and provides additional encoding information in |info|.
jmarusic@webrtc.orgb1f0de32015-02-26 15:38:10 +000063 // 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.
65 void Encode(uint32_t rtp_timestamp,
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000066 const int16_t* audio,
kwiberg@webrtc.org663fdd02014-10-29 07:28:36 +000067 size_t num_samples_per_channel,
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000068 size_t max_encoded_bytes,
69 uint8_t* encoded,
henrik.lundin@webrtc.orgf45c8ca2015-02-05 18:29:39 +000070 EncodedInfo* info);
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000071
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000072 // Return the input sample rate in Hz and the number of input channels.
73 // These are constants set at instantiation time.
kwiberg@webrtc.org05211272015-02-18 12:00:32 +000074 virtual int SampleRateHz() const = 0;
75 virtual int NumChannels() const = 0;
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000076
jmarusic@webrtc.org51ccf372015-03-10 15:41:26 +000077 // Return the maximum number of bytes that can be produced by the encoder
78 // at each Encode() call. The caller can use the return value to determine
79 // the size of the buffer that needs to be allocated. This value is allowed
80 // to depend on encoder parameters like bitrate, frame size etc., so if
81 // any of these change, the caller of Encode() is responsible for checking
82 // that the buffer is large enough by calling MaxEncodedBytes() again.
83 virtual size_t MaxEncodedBytes() const = 0;
84
henrik.lundin@webrtc.org478cedc2015-01-27 18:24:45 +000085 // Returns the rate with which the RTP timestamps are updated. By default,
86 // this is the same as sample_rate_hz().
kwiberg@webrtc.org05211272015-02-18 12:00:32 +000087 virtual int RtpTimestampRateHz() const;
henrik.lundin@webrtc.org478cedc2015-01-27 18:24:45 +000088
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000089 // Returns the number of 10 ms frames the encoder will put in the next
90 // packet. This value may only change when Encode() outputs a packet; i.e.,
91 // the encoder may vary the number of 10 ms frames from packet to packet, but
92 // it must decide the length of the next packet no later than when outputting
93 // the preceding packet.
94 virtual int Num10MsFramesInNextPacket() const = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000095
henrik.lundin@webrtc.org8911bc52014-12-08 21:15:55 +000096 // Returns the maximum value that can be returned by
97 // Num10MsFramesInNextPacket().
98 virtual int Max10MsFramesInAPacket() const = 0;
99
henrik.lundin@webrtc.org478cedc2015-01-27 18:24:45 +0000100 // Changes the target bitrate. The implementation is free to alter this value,
101 // e.g., if the desired value is outside the valid range.
102 virtual void SetTargetBitrate(int bits_per_second) {}
103
104 // Tells the implementation what the projected packet loss rate is. The rate
105 // is in the range [0.0, 1.0]. This rate is typically used to adjust channel
106 // coding efforts, such as FEC.
107 virtual void SetProjectedPacketLossRate(double fraction) {}
108
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +0000109 protected:
jmarusic@webrtc.orgb1f0de32015-02-26 15:38:10 +0000110 virtual void EncodeInternal(uint32_t rtp_timestamp,
henrik.lundin@webrtc.org8dc21dc2014-12-03 20:36:03 +0000111 const int16_t* audio,
112 size_t max_encoded_bytes,
113 uint8_t* encoded,
henrik.lundin@webrtc.org8dc21dc2014-12-03 20:36:03 +0000114 EncodedInfo* info) = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +0000115};
116
117} // namespace webrtc
118#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_