blob: 27b9f2ffdd424d01722759e287e685596cb702aa [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.org9ea6f8a2014-10-16 11:26:24 +000015
16#include "webrtc/base/checks.h"
17#include "webrtc/typedefs.h"
18
19namespace webrtc {
20
21// This is the interface class for encoders in AudioCoding module. Each codec
22// codec type must have an implementation of this class.
23class AudioEncoder {
24 public:
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000025 struct EncodedInfo {
henrik.lundin@webrtc.org3b79daf2014-12-12 13:31:24 +000026 EncodedInfo() : encoded_bytes(0), encoded_timestamp(0), payload_type(0) {}
27
28 size_t encoded_bytes;
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000029 uint32_t encoded_timestamp;
henrik.lundin@webrtc.org7f1dfa52014-12-02 12:08:39 +000030 int payload_type;
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000031 };
32
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000033 virtual ~AudioEncoder() {}
34
35 // Accepts one 10 ms block of input audio (i.e., sample_rate_hz() / 100 *
36 // num_channels() samples). Multi-channel audio must be sample-interleaved.
37 // If successful, the encoder produces zero or more bytes of output in
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000038 // |encoded|, and provides the number of encoded bytes in |encoded_bytes|.
39 // In case of error, false is returned, otherwise true. It is an error for the
40 // encoder to attempt to produce more than |max_encoded_bytes| bytes of
41 // output.
42 bool Encode(uint32_t timestamp,
43 const int16_t* audio,
kwiberg@webrtc.org663fdd02014-10-29 07:28:36 +000044 size_t num_samples_per_channel,
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000045 size_t max_encoded_bytes,
46 uint8_t* encoded,
henrik.lundin@webrtc.org1db20a42014-12-01 14:44:50 +000047 EncodedInfo* info) {
kwiberg@webrtc.org663fdd02014-10-29 07:28:36 +000048 CHECK_EQ(num_samples_per_channel,
49 static_cast<size_t>(sample_rate_hz() / 100));
henrik.lundin@webrtc.org8dc21dc2014-12-03 20:36:03 +000050 bool ret = EncodeInternal(timestamp,
51 audio,
52 max_encoded_bytes,
53 encoded,
henrik.lundin@webrtc.org8dc21dc2014-12-03 20:36:03 +000054 info);
henrik.lundin@webrtc.org3b79daf2014-12-12 13:31:24 +000055 CHECK_LE(info->encoded_bytes, max_encoded_bytes);
henrik.lundin@webrtc.orgdef1e972014-10-21 12:48:29 +000056 return ret;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000057 }
58
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000059 // Return the input sample rate in Hz and the number of input channels.
60 // These are constants set at instantiation time.
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000061 virtual int sample_rate_hz() const = 0;
62 virtual int num_channels() const = 0;
kwiberg@webrtc.orgdecd9302014-10-29 08:38:50 +000063
64 // Returns the number of 10 ms frames the encoder will put in the next
65 // packet. This value may only change when Encode() outputs a packet; i.e.,
66 // the encoder may vary the number of 10 ms frames from packet to packet, but
67 // it must decide the length of the next packet no later than when outputting
68 // the preceding packet.
69 virtual int Num10MsFramesInNextPacket() const = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000070
henrik.lundin@webrtc.org8911bc52014-12-08 21:15:55 +000071 // Returns the maximum value that can be returned by
72 // Num10MsFramesInNextPacket().
73 virtual int Max10MsFramesInAPacket() const = 0;
74
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000075 protected:
henrik.lundin@webrtc.org8dc21dc2014-12-03 20:36:03 +000076 virtual bool EncodeInternal(uint32_t timestamp,
77 const int16_t* audio,
78 size_t max_encoded_bytes,
79 uint8_t* encoded,
henrik.lundin@webrtc.org8dc21dc2014-12-03 20:36:03 +000080 EncodedInfo* info) = 0;
henrik.lundin@webrtc.org9ea6f8a2014-10-16 11:26:24 +000081};
82
83} // namespace webrtc
84#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_H_