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 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 23 24 struct OpusMSEncoder; 25 26 namespace android { 27 28 struct C2SoftOpusEnc : public SimpleC2Component { 29 class IntfImpl; 30 31 C2SoftOpusEnc(const char *name, c2_node_id_t id, 32 const std::shared_ptr<IntfImpl> &intfImpl); 33 virtual ~C2SoftOpusEnc(); 34 35 // From SimpleC2Component 36 c2_status_t onInit() override; 37 c2_status_t onStop() override; 38 void onReset() override; 39 void onRelease() override; 40 c2_status_t onFlush_sm() override; 41 void process( 42 const std::unique_ptr<C2Work> &work, 43 const std::shared_ptr<C2BlockPool> &pool) override; 44 c2_status_t drain( 45 uint32_t drainMode, 46 const std::shared_ptr<C2BlockPool> &pool) override; 47 private: 48 static const int kMaxNumChannelsSupported = 2; 49 static const int kMaxSampleRateSupported = 48000; 50 static const int kDefaultFrameDurationMs = 20; 51 // For a frame duration of 20ms, payload recommended size is 1276 as per 52 // https://www.opus-codec.org/docs/html_api/group__opusencoder.html. 53 // For 40ms, 60ms, .. payload size will change proportionately, 1276 x 2, 1276 x 3, .. 54 static const int kMaxPayload = 1500; // from tests/test_opus_encode.c 55 56 std::shared_ptr<IntfImpl> mIntf; 57 std::shared_ptr<C2LinearBlock> mOutputBlock; 58 59 OpusMSEncoder* mEncoder; 60 int16_t* mInputBufferPcm16; 61 62 bool mHeaderGenerated; 63 bool mIsFirstFrame; 64 bool mEncoderFlushed; 65 bool mBufferAvailable; 66 bool mSignalledEos; 67 bool mSignalledError; 68 uint32_t mSampleRate; 69 uint32_t mChannelCount; 70 uint32_t mFrameDurationMs; 71 int64_t mAnchorTimeStamp; 72 uint64_t mProcessedSamples; 73 // Codec delay in ns 74 uint64_t mCodecDelay; 75 // Seek pre-roll in ns 76 uint64_t mSeekPreRoll; 77 int mNumSamplesPerFrame; 78 int mBytesEncoded; 79 int32_t mFilledLen; 80 size_t mNumPcmBytesPerInputFrame; 81 std::atomic_uint64_t mOutIndex; 82 c2_status_t initEncoder(); 83 c2_status_t configureEncoder(); 84 int drainEncoder(uint8_t* outPtr); 85 c2_status_t drainInternal(const std::shared_ptr<C2BlockPool>& pool, 86 const std::unique_ptr<C2Work>& work); 87 88 C2_DO_NOT_COPY(C2SoftOpusEnc); 89 }; 90 91 } // namespace android 92 93 #endif // ANDROID_C2_SOFT_OPUS_ENC_H_ 94