Implement AV1 Encoder via Codec2 interface

This implementes AV1 Encoder via Codec2 interface. The encoder supports
8 and 10 bit input, Dynamic BR change, Forced key frame. No B-Frame
support at the moment.

Bug:246859000
Test: atest VideoCodecTest

Change-Id: I2c2d5dc2804f996e6c2fb1b8469a63072dec64ea
diff --git a/media/codec2/components/aom/C2SoftAomEnc.h b/media/codec2/components/aom/C2SoftAomEnc.h
new file mode 100644
index 0000000..4b820ea
--- /dev/null
+++ b/media/codec2/components/aom/C2SoftAomEnc.h
@@ -0,0 +1,160 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_C2_SOFT_AV1_ENC_H_
+#define ANDROID_C2_SOFT_AV1_ENC_H_
+
+#include <inttypes.h>
+
+#include <C2PlatformSupport.h>
+#include <Codec2BufferUtils.h>
+#include <SimpleC2Component.h>
+#include <SimpleC2Interface.h>
+#include <util/C2InterfaceHelper.h>
+
+#include "aom/aom_encoder.h"
+#include "aom/aomcx.h"
+#include "common/av1_config.h"
+
+namespace android {
+struct C2SoftAomEnc : public SimpleC2Component {
+    class IntfImpl;
+
+    C2SoftAomEnc(const char* name, c2_node_id_t id, const std::shared_ptr<IntfImpl>& intfImpl);
+
+    // From SimpleC2Component
+    c2_status_t onInit() override final;
+    c2_status_t onStop() override final;
+    void onReset() override final;
+    void onRelease() override final;
+    c2_status_t onFlush_sm() override final;
+
+    void process(const std::unique_ptr<C2Work>& work,
+                 const std::shared_ptr<C2BlockPool>& pool) override final;
+    c2_status_t drain(uint32_t drainMode, const std::shared_ptr<C2BlockPool>& pool) override final;
+
+protected:
+    virtual ~C2SoftAomEnc();
+
+private:
+  std::shared_ptr<IntfImpl> mIntf;
+
+  // Initializes aom encoder with available settings.
+  status_t initEncoder();
+
+  // aom specific opaque data structure that
+  // stores encoder state
+  aom_codec_ctx_t* mCodecContext;
+
+  // aom specific data structure that
+  // stores encoder configuration
+  aom_codec_enc_cfg_t* mCodecConfiguration;
+
+  // aom specific read-only data structure
+  // that specifies algorithm interface
+  aom_codec_iface_t* mCodecInterface;
+
+  // align stride to the power of 2
+  int32_t mStrideAlign;
+
+
+  aom_rc_mode mBitrateControlMode;
+
+  // Minimum (best quality) quantizer
+  uint32_t mMinQuantizer;
+
+  // Maximum (worst quality) quantizer
+  uint32_t mMaxQuantizer;
+
+  // Last input buffer timestamp
+  uint64_t mLastTimestamp;
+
+  // Number of input frames
+  int64_t mNumInputFrames;
+
+  // Conversion buffer is needed to input to
+  // yuv420 planar format.
+  MemoryBlock mConversionBuffer;
+
+  // Signalled End Of Stream
+  bool mSignalledOutputEos;
+
+  // Signalled Error
+  bool mSignalledError;
+
+  bool mHeadersReceived;
+
+  std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
+  std::shared_ptr<C2StreamIntraRefreshTuning::output> mIntraRefresh;
+  std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
+  std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
+  std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode;
+  std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
+
+  aom_codec_err_t setupCodecParameters();
+};
+
+class C2SoftAomEnc::IntfImpl : public SimpleInterface<void>::BaseParams {
+public:
+    explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper);
+
+    static C2R BitrateSetter(bool mayBlock, C2P<C2StreamBitrateInfo::output> &me);
+
+    static C2R SizeSetter(bool mayBlock, const C2P<C2StreamPictureSizeInfo::input> &oldMe,
+                          C2P<C2StreamPictureSizeInfo::input> &me);
+
+    static C2R ProfileLevelSetter(
+            bool mayBlock,
+            C2P<C2StreamProfileLevelInfo::output> &me);
+
+
+    // unsafe getters
+    std::shared_ptr<C2StreamPictureSizeInfo::input> getSize_l() const { return mSize; }
+    std::shared_ptr<C2StreamIntraRefreshTuning::output> getIntraRefresh_l() const {
+        return mIntraRefresh;
+    }
+    std::shared_ptr<C2StreamFrameRateInfo::output> getFrameRate_l() const { return mFrameRate; }
+    std::shared_ptr<C2StreamBitrateInfo::output> getBitrate_l() const { return mBitrate; }
+    std::shared_ptr<C2StreamBitrateModeTuning::output> getBitrateMode_l() const {
+        return mBitrateMode;
+    }
+    std::shared_ptr<C2StreamRequestSyncFrameTuning::output> getRequestSync_l() const {
+        return mRequestSync;
+    }
+    std::shared_ptr<C2StreamColorAspectsInfo::output> getCodedColorAspects_l() const {
+        return mCodedColorAspects;
+    }
+    uint32_t getSyncFramePeriod() const;
+    static C2R ColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::input> &me);
+    static C2R CodedColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::output> &me,
+                                       const C2P<C2StreamColorAspectsInfo::input> &coded);
+
+private:
+  std::shared_ptr<C2StreamUsageTuning::input> mUsage;
+  std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
+  std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
+  std::shared_ptr<C2StreamIntraRefreshTuning::output> mIntraRefresh;
+  std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
+  std::shared_ptr<C2StreamSyncFrameIntervalTuning::output> mSyncFramePeriod;
+  std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
+  std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode;
+  std::shared_ptr<C2StreamProfileLevelInfo::output> mProfileLevel;
+  std::shared_ptr<C2StreamColorAspectsInfo::input> mColorAspects;
+  std::shared_ptr<C2StreamColorAspectsInfo::output> mCodedColorAspects;
+};
+
+}  // namespace android
+#endif  // ANDROID_C2_SOFT_AV1_ENC_H_