blob: 4b820eaa6f9dfe53ef5da59d938069c797be5daa [file] [log] [blame]
Fyodor Kyslovadc71142022-09-15 16:47:03 +00001/*
2 * Copyright (C) 2022 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_AV1_ENC_H_
18#define ANDROID_C2_SOFT_AV1_ENC_H_
19
20#include <inttypes.h>
21
22#include <C2PlatformSupport.h>
23#include <Codec2BufferUtils.h>
24#include <SimpleC2Component.h>
25#include <SimpleC2Interface.h>
26#include <util/C2InterfaceHelper.h>
27
28#include "aom/aom_encoder.h"
29#include "aom/aomcx.h"
30#include "common/av1_config.h"
31
32namespace android {
33struct C2SoftAomEnc : public SimpleC2Component {
34 class IntfImpl;
35
36 C2SoftAomEnc(const char* name, c2_node_id_t id, const std::shared_ptr<IntfImpl>& intfImpl);
37
38 // From SimpleC2Component
39 c2_status_t onInit() override final;
40 c2_status_t onStop() override final;
41 void onReset() override final;
42 void onRelease() override final;
43 c2_status_t onFlush_sm() override final;
44
45 void process(const std::unique_ptr<C2Work>& work,
46 const std::shared_ptr<C2BlockPool>& pool) override final;
47 c2_status_t drain(uint32_t drainMode, const std::shared_ptr<C2BlockPool>& pool) override final;
48
49protected:
50 virtual ~C2SoftAomEnc();
51
52private:
53 std::shared_ptr<IntfImpl> mIntf;
54
55 // Initializes aom encoder with available settings.
56 status_t initEncoder();
57
58 // aom specific opaque data structure that
59 // stores encoder state
60 aom_codec_ctx_t* mCodecContext;
61
62 // aom specific data structure that
63 // stores encoder configuration
64 aom_codec_enc_cfg_t* mCodecConfiguration;
65
66 // aom specific read-only data structure
67 // that specifies algorithm interface
68 aom_codec_iface_t* mCodecInterface;
69
70 // align stride to the power of 2
71 int32_t mStrideAlign;
72
73
74 aom_rc_mode mBitrateControlMode;
75
76 // Minimum (best quality) quantizer
77 uint32_t mMinQuantizer;
78
79 // Maximum (worst quality) quantizer
80 uint32_t mMaxQuantizer;
81
82 // Last input buffer timestamp
83 uint64_t mLastTimestamp;
84
85 // Number of input frames
86 int64_t mNumInputFrames;
87
88 // Conversion buffer is needed to input to
89 // yuv420 planar format.
90 MemoryBlock mConversionBuffer;
91
92 // Signalled End Of Stream
93 bool mSignalledOutputEos;
94
95 // Signalled Error
96 bool mSignalledError;
97
98 bool mHeadersReceived;
99
100 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
101 std::shared_ptr<C2StreamIntraRefreshTuning::output> mIntraRefresh;
102 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
103 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
104 std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode;
105 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
106
107 aom_codec_err_t setupCodecParameters();
108};
109
110class C2SoftAomEnc::IntfImpl : public SimpleInterface<void>::BaseParams {
111public:
112 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper);
113
114 static C2R BitrateSetter(bool mayBlock, C2P<C2StreamBitrateInfo::output> &me);
115
116 static C2R SizeSetter(bool mayBlock, const C2P<C2StreamPictureSizeInfo::input> &oldMe,
117 C2P<C2StreamPictureSizeInfo::input> &me);
118
119 static C2R ProfileLevelSetter(
120 bool mayBlock,
121 C2P<C2StreamProfileLevelInfo::output> &me);
122
123
124 // unsafe getters
125 std::shared_ptr<C2StreamPictureSizeInfo::input> getSize_l() const { return mSize; }
126 std::shared_ptr<C2StreamIntraRefreshTuning::output> getIntraRefresh_l() const {
127 return mIntraRefresh;
128 }
129 std::shared_ptr<C2StreamFrameRateInfo::output> getFrameRate_l() const { return mFrameRate; }
130 std::shared_ptr<C2StreamBitrateInfo::output> getBitrate_l() const { return mBitrate; }
131 std::shared_ptr<C2StreamBitrateModeTuning::output> getBitrateMode_l() const {
132 return mBitrateMode;
133 }
134 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> getRequestSync_l() const {
135 return mRequestSync;
136 }
137 std::shared_ptr<C2StreamColorAspectsInfo::output> getCodedColorAspects_l() const {
138 return mCodedColorAspects;
139 }
140 uint32_t getSyncFramePeriod() const;
141 static C2R ColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::input> &me);
142 static C2R CodedColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::output> &me,
143 const C2P<C2StreamColorAspectsInfo::input> &coded);
144
145private:
146 std::shared_ptr<C2StreamUsageTuning::input> mUsage;
147 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
148 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
149 std::shared_ptr<C2StreamIntraRefreshTuning::output> mIntraRefresh;
150 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
151 std::shared_ptr<C2StreamSyncFrameIntervalTuning::output> mSyncFramePeriod;
152 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
153 std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode;
154 std::shared_ptr<C2StreamProfileLevelInfo::output> mProfileLevel;
155 std::shared_ptr<C2StreamColorAspectsInfo::input> mColorAspects;
156 std::shared_ptr<C2StreamColorAspectsInfo::output> mCodedColorAspects;
157};
158
159} // namespace android
160#endif // ANDROID_C2_SOFT_AV1_ENC_H_