1 /*
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_ISAC_AUDIO_ENCODER_ISAC_T_H_
12 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_AUDIO_ENCODER_ISAC_T_H_
13 
14 #include <vector>
15 
16 #include "webrtc/modules/audio_coding/codecs/audio_encoder.h"
17 #include "webrtc/modules/audio_coding/codecs/isac/locked_bandwidth_info.h"
18 
19 namespace webrtc {
20 
21 struct CodecInst;
22 
23 template <typename T>
24 class AudioEncoderIsacT final : public AudioEncoder {
25  public:
26   // Allowed combinations of sample rate, frame size, and bit rate are
27   //  - 16000 Hz, 30 ms, 10000-32000 bps
28   //  - 16000 Hz, 60 ms, 10000-32000 bps
29   //  - 32000 Hz, 30 ms, 10000-56000 bps (if T has super-wideband support)
30   struct Config {
31     bool IsOk() const;
32 
33     LockedIsacBandwidthInfo* bwinfo = nullptr;
34 
35     int payload_type = 103;
36     int sample_rate_hz = 16000;
37     int frame_size_ms = 30;
38     int bit_rate = kDefaultBitRate;  // Limit on the short-term average bit
39                                      // rate, in bits/s.
40     int max_payload_size_bytes = -1;
41     int max_bit_rate = -1;
42 
43     // If true, the encoder will dynamically adjust frame size and bit rate;
44     // the configured values are then merely the starting point.
45     bool adaptive_mode = false;
46 
47     // In adaptive mode, prevent adaptive changes to the frame size. (Not used
48     // in nonadaptive mode.)
49     bool enforce_frame_size = false;
50   };
51 
52   explicit AudioEncoderIsacT(const Config& config);
53   explicit AudioEncoderIsacT(const CodecInst& codec_inst,
54                              LockedIsacBandwidthInfo* bwinfo);
55   ~AudioEncoderIsacT() override;
56 
57   size_t MaxEncodedBytes() const override;
58   int SampleRateHz() const override;
59   size_t NumChannels() const override;
60   size_t Num10MsFramesInNextPacket() const override;
61   size_t Max10MsFramesInAPacket() const override;
62   int GetTargetBitrate() const override;
63   EncodedInfo EncodeInternal(uint32_t rtp_timestamp,
64                              rtc::ArrayView<const int16_t> audio,
65                              size_t max_encoded_bytes,
66                              uint8_t* encoded) override;
67   void Reset() override;
68 
69  private:
70   // This value is taken from STREAM_SIZE_MAX_60 for iSAC float (60 ms) and
71   // STREAM_MAXW16_60MS for iSAC fix (60 ms).
72   static const size_t kSufficientEncodeBufferSizeBytes = 400;
73 
74   static const int kDefaultBitRate = 32000;
75 
76   // Recreate the iSAC encoder instance with the given settings, and save them.
77   void RecreateEncoderInstance(const Config& config);
78 
79   Config config_;
80   typename T::instance_type* isac_state_ = nullptr;
81   LockedIsacBandwidthInfo* bwinfo_ = nullptr;
82 
83   // Have we accepted input but not yet emitted it in a packet?
84   bool packet_in_progress_ = false;
85 
86   // Timestamp of the first input of the currently in-progress packet.
87   uint32_t packet_timestamp_;
88 
89   // Timestamp of the previously encoded packet.
90   uint32_t last_encoded_timestamp_;
91 
92   RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderIsacT);
93 };
94 
95 }  // namespace webrtc
96 
97 #endif  // WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_AUDIO_ENCODER_ISAC_T_H_
98