blob: 5b81509b26e6e72e1ef83c2c6955bf3c10494936 [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
17#include "webrtc/base/checks.h"
18#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 {
27 EncodedInfoLeaf()
28 : encoded_bytes(0), encoded_timestamp(0), payload_type(0) {}
henrik.lundin@webrtc.org3b79daf2014-12-12 13:31:24 +000029
30 size_t encoded_bytes;
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000031 uint32_t encoded_timestamp;
henrik.lundin@webrtc.org7f1dfa52014-12-02 12:08:39 +000032 int payload_type;
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000033 };
34
henrik.lundin@webrtc.orgc1c92912014-12-16 13:41:36 +000035 // This is the main struct for auxiliary encoding information. Each encoded
36 // packet should be accompanied by one EncodedInfo struct, containing the
37 // total number of |encoded_bytes|, the |encoded_timestamp| and the
38 // |payload_type|. If the packet contains redundant encodings, the |redundant|
39 // vector will be populated with EncodedInfoLeaf structs. Each struct in the
40 // vector represents one encoding; the order of structs in the vector is the
41 // same as the order in which the actual payloads are written to the byte
42 // stream. When EncoderInfoLeaf structs are present in the vector, the main
43 // struct's |encoded_bytes| will be the sum of all the |encoded_bytes| in the
44 // vector.
45 struct EncodedInfo : public EncodedInfoLeaf {
46 EncodedInfo();
47 ~EncodedInfo();
48
49 std::vector<EncodedInfoLeaf> redundant;
50 };
51
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000052 virtual ~AudioEncoder() {}
53
54 // Accepts one 10 ms block of input audio (i.e., sample_rate_hz() / 100 *
55 // num_channels() samples). Multi-channel audio must be sample-interleaved.
56 // If successful, the encoder produces zero or more bytes of output in
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000057 // |encoded|, and provides the number of encoded bytes in |encoded_bytes|.
58 // In case of error, false is returned, otherwise true. It is an error for the
59 // encoder to attempt to produce more than |max_encoded_bytes| bytes of
60 // output.
61 bool Encode(uint32_t timestamp,
62 const int16_t* audio,
kwiberg@webrtc.org663fdd02014-10-29 07:28:36 +000063 size_t num_samples_per_channel,
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000064 size_t max_encoded_bytes,
65 uint8_t* encoded,
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000066 EncodedInfo* info) {
kwiberg@webrtc.org663fdd02014-10-29 07:28:36 +000067 CHECK_EQ(num_samples_per_channel,
68 static_cast<size_t>(sample_rate_hz() / 100));
henrik.lundin@webrtc.org8dc21dc2014-12-03 20:36:03 +000069 bool ret = EncodeInternal(timestamp,
70 audio,
71 max_encoded_bytes,
72 encoded,
henrik.lundin@webrtc.org8dc21dc2014-12-03 20:36:03 +000073 info);
henrik.lundin@webrtc.org3b79daf2014-12-12 13:31:24 +000074 CHECK_LE(info->encoded_bytes, max_encoded_bytes);
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000075 return ret;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000076 }
77
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000078 // Return the input sample rate in Hz and the number of input channels.
79 // These are constants set at instantiation time.
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000080 virtual int sample_rate_hz() const = 0;
81 virtual int num_channels() const = 0;
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000082
83 // Returns the number of 10 ms frames the encoder will put in the next
84 // packet. This value may only change when Encode() outputs a packet; i.e.,
85 // the encoder may vary the number of 10 ms frames from packet to packet, but
86 // it must decide the length of the next packet no later than when outputting
87 // the preceding packet.
88 virtual int Num10MsFramesInNextPacket() const = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000089
henrik.lundin@webrtc.org8911bc52014-12-08 21:15:55 +000090 // Returns the maximum value that can be returned by
91 // Num10MsFramesInNextPacket().
92 virtual int Max10MsFramesInAPacket() const = 0;
93
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000094 protected:
henrik.lundin@webrtc.org8dc21dc2014-12-03 20:36:03 +000095 virtual bool EncodeInternal(uint32_t timestamp,
96 const int16_t* audio,
97 size_t max_encoded_bytes,
98 uint8_t* encoded,
henrik.lundin@webrtc.org8dc21dc2014-12-03 20:36:03 +000099 EncodedInfo* info) = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +0000100};
101
102} // namespace webrtc
103#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_