1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_C2_SOFT_OPUS_ENC_H_ 18 #define ANDROID_C2_SOFT_OPUS_ENC_H_ 19 20 #include <atomic> 21 #include <SimpleC2Component.h> 22 #include <util/C2InterfaceHelper.h> 23 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 24 25 struct OpusMSEncoder; 26 27 namespace android { 28 29 struct C2SoftOpusEnc : public SimpleC2Component { 30 class IntfImpl; 31 32 C2SoftOpusEnc(const char *name, c2_node_id_t id, 33 const std::shared_ptr<IntfImpl> &intfImpl); 34 C2SoftOpusEnc(const char *name, c2_node_id_t id, 35 const std::shared_ptr<C2ReflectorHelper> &helper); 36 virtual ~C2SoftOpusEnc(); 37 38 // From SimpleC2Component 39 c2_status_t onInit() override; 40 c2_status_t onStop() override; 41 void onReset() override; 42 void onRelease() override; 43 c2_status_t onFlush_sm() override; 44 void process( 45 const std::unique_ptr<C2Work> &work, 46 const std::shared_ptr<C2BlockPool> &pool) override; 47 c2_status_t drain( 48 uint32_t drainMode, 49 const std::shared_ptr<C2BlockPool> &pool) override; 50 private: 51 static const int kMaxNumChannelsSupported = 2; 52 static const int kMaxSampleRateSupported = 48000; 53 static const int kDefaultFrameDurationMs = 20; 54 // For a frame duration of 20ms, payload recommended size is 1276 as per 55 // https://www.opus-codec.org/docs/html_api/group__opusencoder.html. 56 // For 40ms, 60ms, .. payload size will change proportionately, 1276 x 2, 1276 x 3, .. 57 static const int kMaxPayload = 1500; // from tests/test_opus_encode.c 58 59 std::shared_ptr<IntfImpl> mIntf; 60 std::shared_ptr<C2LinearBlock> mOutputBlock; 61 62 OpusMSEncoder* mEncoder; 63 int16_t* mInputBufferPcm16; 64 65 bool mHeaderGenerated; 66 bool mIsFirstFrame; 67 bool mEncoderFlushed; 68 bool mBufferAvailable; 69 bool mSignalledEos; 70 bool mSignalledError; 71 uint32_t mSampleRate; 72 uint32_t mChannelCount; 73 uint32_t mFrameDurationMs; 74 int64_t mAnchorTimeStamp; 75 uint64_t mProcessedSamples; 76 // Codec delay in ns 77 uint64_t mCodecDelay; 78 // Seek pre-roll in ns 79 uint64_t mSeekPreRoll; 80 int mNumSamplesPerFrame; 81 int mBytesEncoded; 82 int32_t mFilledLen; 83 size_t mNumPcmBytesPerInputFrame; 84 std::atomic_uint64_t mOutIndex; 85 c2_status_t initEncoder(); 86 c2_status_t configureEncoder(); 87 int drainEncoder(uint8_t* outPtr); 88 c2_status_t drainInternal(const std::shared_ptr<C2BlockPool>& pool, 89 const std::unique_ptr<C2Work>& work); 90 91 C2_DO_NOT_COPY(C2SoftOpusEnc); 92 }; 93 94 } // namespace android 95 96 #endif // ANDROID_C2_SOFT_OPUS_ENC_H_ 97