blob: 87d24f92d19b97c56fc536b58a4e7e0e14b69915 [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright 2018 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_VPX_ENC_H__
18#define ANDROID_C2_SOFT_VPX_ENC_H__
19
20#include <media/stagefright/foundation/MediaDefs.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 "vpx/vpx_encoder.h"
29#include "vpx/vpx_codec.h"
30#include "vpx/vpx_image.h"
31#include "vpx/vp8cx.h"
32
33namespace android {
34
35// TODO: These defs taken from deprecated OMX_VideoExt.h. Move these definitions
36// to a new header file and include it.
37
38/** Maximum number of temporal layers */
39#define MAXTEMPORALLAYERS 3
40
41/** temporal layer patterns */
42typedef enum TemporalPatternType {
43 VPXTemporalLayerPatternNone = 0,
44 VPXTemporalLayerPatternWebRTC = 1,
45 VPXTemporalLayerPatternMax = 0x7FFFFFFF
46} TemporalPatternType;
47
48// Base class for a VPX Encoder Component
49//
50// Only following encoder settings are available (codec specific settings might
51// be available in the sub-classes):
52// - video resolution
53// - target bitrate
54// - rate control (constant / variable)
55// - frame rate
56// - error resilience
57// - reconstruction & loop filters (g_profile)
58//
59// Only following color formats are recognized
60// - C2PlanarLayout::TYPE_RGB
61// - C2PlanarLayout::TYPE_RGBA
62//
63// Following settings are not configurable by the client
64// - encoding deadline is realtime
65// - multithreaded encoding utilizes a number of threads equal
66// to online cpu's available
67// - the algorithm interface for encoder is decided by the sub-class in use
68// - fractional bits of frame rate is discarded
69// - timestamps are in microseconds, therefore encoder timebase is fixed
70// to 1/1000000
71
72struct C2SoftVpxEnc : public SimpleC2Component {
73 class IntfImpl;
74
75 C2SoftVpxEnc(const char* name, c2_node_id_t id,
76 const std::shared_ptr<IntfImpl>& intfImpl);
77
78 // From SimpleC2Component
79 c2_status_t onInit() override final;
80 c2_status_t onStop() override final;
81 void onReset() override final;
82 void onRelease() override final;
83 c2_status_t onFlush_sm() override final;
84
85 void process(
86 const std::unique_ptr<C2Work> &work,
87 const std::shared_ptr<C2BlockPool> &pool) override final;
88 c2_status_t drain(
89 uint32_t drainMode,
90 const std::shared_ptr<C2BlockPool> &pool) override final;
91
92 protected:
93 std::shared_ptr<IntfImpl> mIntf;
94 virtual ~C2SoftVpxEnc();
95
96 // Initializes vpx encoder with available settings.
97 status_t initEncoder();
98
99 // Populates mCodecInterface with codec specific settings.
100 virtual void setCodecSpecificInterface() = 0;
101
102 // Sets codec specific configuration.
103 virtual void setCodecSpecificConfiguration() = 0;
104
105 // Sets codec specific encoder controls.
106 virtual vpx_codec_err_t setCodecSpecificControls() = 0;
107
108 // Get current encode flags.
109 virtual vpx_enc_frame_flags_t getEncodeFlags();
110
111 enum TemporalReferences {
112 // For 1 layer case: reference all (last, golden, and alt ref), but only
113 // update last.
114 kTemporalUpdateLastRefAll = 12,
115 // First base layer frame for 3 temporal layers, which updates last and
116 // golden with alt ref dependency.
117 kTemporalUpdateLastAndGoldenRefAltRef = 11,
118 // First enhancement layer with alt ref dependency.
119 kTemporalUpdateGoldenRefAltRef = 10,
120 // First enhancement layer with alt ref dependency.
121 kTemporalUpdateGoldenWithoutDependencyRefAltRef = 9,
122 // Base layer with alt ref dependency.
123 kTemporalUpdateLastRefAltRef = 8,
124 // Highest enhacement layer without dependency on golden with alt ref
125 // dependency.
126 kTemporalUpdateNoneNoRefGoldenRefAltRef = 7,
127 // Second layer and last frame in cycle, for 2 layers.
128 kTemporalUpdateNoneNoRefAltref = 6,
129 // Highest enhancement layer.
130 kTemporalUpdateNone = 5,
131 // Second enhancement layer.
132 kTemporalUpdateAltref = 4,
133 // Second enhancement layer without dependency on previous frames in
134 // the second enhancement layer.
135 kTemporalUpdateAltrefWithoutDependency = 3,
136 // First enhancement layer.
137 kTemporalUpdateGolden = 2,
138 // First enhancement layer without dependency on previous frames in
139 // the first enhancement layer.
140 kTemporalUpdateGoldenWithoutDependency = 1,
141 // Base layer.
142 kTemporalUpdateLast = 0,
143 };
144 enum {
145 kMaxTemporalPattern = 8
146 };
147
148 // vpx specific opaque data structure that
149 // stores encoder state
150 vpx_codec_ctx_t* mCodecContext;
151
152 // vpx specific data structure that
153 // stores encoder configuration
154 vpx_codec_enc_cfg_t* mCodecConfiguration;
155
156 // vpx specific read-only data structure
157 // that specifies algorithm interface (e.g. vp8)
158 vpx_codec_iface_t* mCodecInterface;
159
160 // align stride to the power of 2
161 int32_t mStrideAlign;
162
163 // Color format for the input port
164 vpx_img_fmt_t mColorFormat;
165
166 // Bitrate control mode, either constant or variable
167 vpx_rc_mode mBitrateControlMode;
168
169 // Parameter that denotes whether error resilience
170 // is enabled in encoder
171 bool mErrorResilience;
172
173 // Minimum (best quality) quantizer
174 uint32_t mMinQuantizer;
175
176 // Maximum (worst quality) quantizer
177 uint32_t mMaxQuantizer;
178
179 // Number of coding temporal layers to be used.
180 size_t mTemporalLayers;
181
182 // Temporal layer bitrare ratio in percentage
Harish Mahendrakarda612452020-03-14 17:41:29 -0700183 float_t mTemporalLayerBitrateRatio[MAXTEMPORALLAYERS];
Pawin Vongmasa36653902018-11-15 00:10:25 -0800184
185 // Temporal pattern type
186 TemporalPatternType mTemporalPatternType;
187
188 // Temporal pattern length
189 size_t mTemporalPatternLength;
190
191 // Temporal pattern current index
192 size_t mTemporalPatternIdx;
193
194 // Frame type temporal pattern
195 TemporalReferences mTemporalPattern[kMaxTemporalPattern];
196
197 // Last input buffer timestamp
198 uint64_t mLastTimestamp;
199
200 // Number of input frames
201 int64_t mNumInputFrames;
202
203 // Conversion buffer is needed to input to
204 // yuv420 planar format.
205 MemoryBlock mConversionBuffer;
206
207 // Signalled EOS
208 bool mSignalledOutputEos;
209
Harish Mahendrakare4bf61f2024-04-30 19:51:04 -0700210 // Header generated
211 bool mHeaderGenerated;
212
Pawin Vongmasa36653902018-11-15 00:10:25 -0800213 // Signalled Error
214 bool mSignalledError;
215
216 // configurations used by component in process
217 // (TODO: keep this in intf but make them internal only)
218 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
219 std::shared_ptr<C2StreamIntraRefreshTuning::output> mIntraRefresh;
220 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
221 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
222 std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode;
223 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
Harish Mahendrakarda612452020-03-14 17:41:29 -0700224 std::shared_ptr<C2StreamTemporalLayeringTuning::output> mLayering;
Neelkamal Semwale4691832021-05-25 14:00:07 +0530225 std::shared_ptr<C2StreamPictureQuantizationTuning::output> mQpBounds;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800226
227 C2_DO_NOT_COPY(C2SoftVpxEnc);
228};
229
Manisha Jajoo9acd73d2019-03-12 14:08:05 +0530230namespace {
231
232#ifdef VP9
233constexpr char COMPONENT_NAME[] = "c2.android.vp9.encoder";
234const char *MEDIA_MIMETYPE_VIDEO = MEDIA_MIMETYPE_VIDEO_VP9;
235#else
236constexpr char COMPONENT_NAME[] = "c2.android.vp8.encoder";
237const char *MEDIA_MIMETYPE_VIDEO = MEDIA_MIMETYPE_VIDEO_VP8;
238#endif
239
240} // namepsace
241
242class C2SoftVpxEnc::IntfImpl : public SimpleInterface<void>::BaseParams {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800243 public:
Manisha Jajooc743a112021-09-06 20:46:04 +0530244 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper);
245 static C2R BitrateSetter(bool mayBlock, C2P<C2StreamBitrateInfo::output> &me);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800246
247 static C2R SizeSetter(bool mayBlock, const C2P<C2StreamPictureSizeInfo::input> &oldMe,
Manisha Jajooc743a112021-09-06 20:46:04 +0530248 C2P<C2StreamPictureSizeInfo::input> &me);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800249
Manisha Jajoodf62e0f2021-11-12 18:57:28 +0530250 static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::output>& me,
251 const C2P<C2StreamPictureSizeInfo::input>& size,
252 const C2P<C2StreamFrameRateInfo::output>& frameRate,
253 const C2P<C2StreamBitrateInfo::output>& bitrate);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800254
Manisha Jajooc743a112021-09-06 20:46:04 +0530255 static C2R LayeringSetter(bool mayBlock, C2P<C2StreamTemporalLayeringTuning::output>& me);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800256
Neelkamal Semwale4691832021-05-25 14:00:07 +0530257 static C2R PictureQuantizationSetter(bool mayBlock,
258 C2P<C2StreamPictureQuantizationTuning::output> &me);
259
Pawin Vongmasa36653902018-11-15 00:10:25 -0800260 // unsafe getters
261 std::shared_ptr<C2StreamPictureSizeInfo::input> getSize_l() const { return mSize; }
Manisha Jajooc743a112021-09-06 20:46:04 +0530262 std::shared_ptr<C2StreamIntraRefreshTuning::output> getIntraRefresh_l() const {
263 return mIntraRefresh;
264 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800265 std::shared_ptr<C2StreamFrameRateInfo::output> getFrameRate_l() const { return mFrameRate; }
266 std::shared_ptr<C2StreamBitrateInfo::output> getBitrate_l() const { return mBitrate; }
Manisha Jajooc743a112021-09-06 20:46:04 +0530267 std::shared_ptr<C2StreamBitrateModeTuning::output> getBitrateMode_l() const {
268 return mBitrateMode;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800269 }
Manisha Jajooc743a112021-09-06 20:46:04 +0530270 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> getRequestSync_l() const {
271 return mRequestSync;
Manisha Jajoo6d148b02021-03-08 19:12:19 +0530272 }
Manisha Jajooc743a112021-09-06 20:46:04 +0530273 std::shared_ptr<C2StreamTemporalLayeringTuning::output> getTemporalLayers_l() const {
274 return mLayering;
275 }
Manisha Jajood9f98442021-06-02 11:27:07 +0530276 std::shared_ptr<C2StreamColorAspectsInfo::output> getCodedColorAspects_l() const {
277 return mCodedColorAspects;
278 }
Neelkamal Semwale4691832021-05-25 14:00:07 +0530279 std::shared_ptr<C2StreamPictureQuantizationTuning::output> getPictureQuantization_l() const {
280 return mPictureQuantization;
281 }
Manisha Jajooc743a112021-09-06 20:46:04 +0530282 uint32_t getSyncFramePeriod() const;
283 static C2R ColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::input> &me);
Manisha Jajoo6d148b02021-03-08 19:12:19 +0530284 static C2R CodedColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::output> &me,
Manisha Jajooc743a112021-09-06 20:46:04 +0530285 const C2P<C2StreamColorAspectsInfo::input> &coded);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800286
287 private:
Pawin Vongmasa36653902018-11-15 00:10:25 -0800288 std::shared_ptr<C2StreamUsageTuning::input> mUsage;
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800289 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800290 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
291 std::shared_ptr<C2StreamTemporalLayeringTuning::output> mLayering;
292 std::shared_ptr<C2StreamIntraRefreshTuning::output> mIntraRefresh;
293 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
294 std::shared_ptr<C2StreamSyncFrameIntervalTuning::output> mSyncFramePeriod;
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800295 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800296 std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode;
297 std::shared_ptr<C2StreamProfileLevelInfo::output> mProfileLevel;
Manisha Jajoo6d148b02021-03-08 19:12:19 +0530298 std::shared_ptr<C2StreamColorAspectsInfo::input> mColorAspects;
299 std::shared_ptr<C2StreamColorAspectsInfo::output> mCodedColorAspects;
Neelkamal Semwale4691832021-05-25 14:00:07 +0530300 std::shared_ptr<C2StreamPictureQuantizationTuning::output> mPictureQuantization;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800301};
302
303} // namespace android
304
305#endif // ANDROID_C2_SOFT_VPX_ENC_H__