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     /* OPUS_FRAMESIZE_20_MS */
49     const int kFrameSize = 960;
50     const int kMaxSampleRate = 48000;
51     const int kMinSampleRate = 8000;
52     const int kMaxPayload = (4000 * kMaxSampleRate) / kMinSampleRate;
53     const int kMaxNumChannels = 8;
54 
55     std::shared_ptr<IntfImpl> mIntf;
56     std::shared_ptr<C2LinearBlock> mOutputBlock;
57 
58     OpusMSEncoder* mEncoder;
59     int16_t* mInputBufferPcm16;
60 
61     bool mHeaderGenerated;
62     bool mIsFirstFrame;
63     bool mEncoderFlushed;
64     bool mBufferAvailable;
65     bool mSignalledEos;
66     bool mSignalledError;
67     uint32_t mSampleRate;
68     uint32_t mChannelCount;
69     uint32_t mFrameDurationMs;
70     uint64_t mAnchorTimeStamp;
71     uint64_t mProcessedSamples;
72     // Codec delay in ns
73     uint64_t mCodecDelay;
74     // Seek pre-roll in ns
75     uint64_t mSeekPreRoll;
76     int mNumSamplesPerFrame;
77     int mBytesEncoded;
78     int32_t mFilledLen;
79     size_t mNumPcmBytesPerInputFrame;
80     std::atomic_uint64_t mOutIndex;
81     c2_status_t initEncoder();
82     c2_status_t configureEncoder();
83     int drainEncoder(uint8_t* outPtr);
84     c2_status_t drainInternal(const std::shared_ptr<C2BlockPool>& pool,
85                               const std::unique_ptr<C2Work>& work);
86 
87     C2_DO_NOT_COPY(C2SoftOpusEnc);
88 };
89 
90 }  // namespace android
91 
92 #endif  // ANDROID_C2_SOFT_OPUS_ENC_H_
93