1 // Copyright 2018 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // https://github.com/google/opuscpp 16 17 #ifndef OPUSCPP_OPUS_WRAPPER_H_ 18 #define OPUSCPP_OPUS_WRAPPER_H_ 19 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "opus.h" 25 26 namespace opus { 27 28 std::string ErrorToString(int error); 29 30 namespace internal { 31 // Deleter for OpusEncoders and OpusDecoders 32 struct OpusDestroyer { 33 void operator()(OpusEncoder* encoder) const noexcept; 34 void operator()(OpusDecoder* decoder) const noexcept; 35 }; 36 template <typename T> 37 using opus_uptr = std::unique_ptr<T, OpusDestroyer>; 38 } // namespace internal 39 40 class Encoder { 41 public: 42 // see documentation at: 43 // https://mf4.xiph.org/jenkins/view/opus/job/opus/ws/doc/html/group__opus__encoder.html#gaa89264fd93c9da70362a0c9b96b9ca88 44 // Fs corresponds to sample_rate 45 // 46 // If expected_loss_percent is positive, FEC will be enabled 47 Encoder(opus_int32 sample_rate, int num_channels, int application, 48 int expected_loss_percent = 0); 49 50 // Resets internal state of encoder. This should be called between encoding 51 // different streams so that back-to-back decoding and one-at-a-time decoding 52 // give the same result. Returns true on success. 53 bool ResetState(); 54 55 // Sets the desired bitrate. Rates from 500 to 512000 are meaningful as well 56 // as the special values OPUS_AUTO and OPUS_BITRATE_MAX. If this method 57 // is not called, the default value of OPUS_AUTO is used. 58 // Returns true on success. 59 bool SetBitrate(int bitrate); 60 61 // Enables or disables variable bitrate in the encoder. By default, variable 62 // bitrate is enabled. Returns true on success. 63 bool SetVariableBitrate(int vbr); 64 65 // Sets the computational complexity of the encoder, in the range of 0 to 10, 66 // inclusive, with 10 being the highest complexity. Returns true on success. 67 bool SetComplexity(int complexity); 68 69 // Gets the total samples of delay added by the entire codec. This value 70 // is the minimum amount of 'preskip' that has to be specified in an 71 // ogg-stream that encapsulates the encoded audio. 72 int GetLookahead(); 73 74 // Takes audio data and encodes it. Returns a sequence of encoded packets. 75 // pcm.size() must be divisible by frame_size * (number of channels); 76 // pcm must not contain any incomplete packets. 77 // see documentation for pcm and frame_size at: 78 // https://mf4.xiph.org/jenkins/view/opus/job/opus/ws/doc/html/group__opus__encoder.html#gad2d6bf6a9ffb6674879d7605ed073e25 79 std::vector<std::vector<unsigned char>> Encode( 80 const std::vector<opus_int16>& pcm, int frame_size); 81 valid()82 int valid() const { return valid_; } 83 84 private: 85 std::vector<unsigned char> EncodeFrame( 86 const std::vector<opus_int16>::const_iterator& frame_start, 87 int frame_size); 88 89 template <typename... Ts> Ctl(int request,Ts...args)90 int Ctl(int request, Ts... args) const { 91 return opus_encoder_ctl(encoder_.get(), request, args...); 92 } 93 94 int num_channels_{}; 95 bool valid_{}; 96 internal::opus_uptr<OpusEncoder> encoder_; 97 }; 98 99 class Decoder { 100 public: 101 // see documentation at: 102 // https://mf4.xiph.org/jenkins/view/opus/job/opus/ws/doc/html/group__opus__decoder.html#ga753f6fe0b699c81cfd47d70c8e15a0bd 103 // Fs corresponds to sample_rate 104 Decoder(opus_uint32 sample_rate, int num_channels); 105 106 // Takes a sequence of encoded packets and decodes them. Returns the decoded 107 // audio. 108 // see documentation at: 109 // https://mf4.xiph.org/jenkins/view/opus/job/opus/ws/doc/html/group__opus__decoder.html#ga7d1111f64c36027ddcb81799df9b3fc9 110 std::vector<opus_int16> Decode( 111 const std::vector<std::vector<unsigned char>>& packets, int frame_size, 112 bool decode_fec); 113 valid()114 int valid() const { return valid_; } 115 116 // Takes an encoded packet and decodes it. Returns the decoded audio 117 // see documentation at: 118 // https://mf4.xiph.org/jenkins/view/opus/job/opus/ws/doc/html/group__opus__decoder.html#ga7d1111f64c36027ddcb81799df9b3fc9 119 std::vector<opus_int16> Decode(const std::vector<unsigned char>& packet, 120 int frame_size, bool decode_fec); 121 122 // Generates a dummy frame by passing nullptr to the underlying opus decode. 123 std::vector<opus_int16> DecodeDummy(int frame_size); 124 125 private: 126 int num_channels_{}; 127 bool valid_{}; 128 internal::opus_uptr<OpusDecoder> decoder_; 129 }; 130 131 } // namespace opus 132 133 #endif 134