xref: /aosp_15_r20/external/v4l2_codec2/components/include/v4l2_codec2/components/EncodeInterface.h (revision 0ec5a0ec62797f775085659156625e7f1bdb369f)
1 // Copyright 2023 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef ANDROID_V4L2_CODEC2_COMPONENTS_ENCODE_INTERFACE_H
6 #define ANDROID_V4L2_CODEC2_COMPONENTS_ENCODE_INTERFACE_H
7 
8 #include <optional>
9 #include <vector>
10 
11 #include <C2.h>
12 #include <C2Buffer.h>
13 #include <C2Config.h>
14 #include <ui/Size.h>
15 #include <util/C2InterfaceHelper.h>
16 
17 #include <v4l2_codec2/common/Common.h>
18 #include <v4l2_codec2/common/EncodeHelpers.h>
19 
20 namespace media {
21 class V4L2Device;
22 };
23 
24 namespace android {
25 
26 // Codec 2.0 interface describing the V4L2EncodeComponent. This interface is used by the codec 2.0
27 // framework to query the component's capabilities and request configuration changes.
28 class EncodeInterface : public C2InterfaceHelper {
29 public:
30     EncodeInterface(const C2String& name, std::shared_ptr<C2ReflectorHelper> helper,
31                     const SupportedCapabilities& caps);
32 
33     // Interfaces for the V4L2EncodeInterface
34     // Note: these getters are not thread-safe. For dynamic parameters, component should use
35     // formal query API for C2ComponentInterface instead.
status()36     c2_status_t status() const { return mInitStatus; }
getOutputMediaType()37     const char* getOutputMediaType() const { return mOutputMediaType->m.value; }
getOutputProfile()38     C2Config::profile_t getOutputProfile() const { return mProfileLevel->profile; }
getOutputLevel()39     C2Config::level_t getOutputLevel() const { return mProfileLevel->level; }
getInputVisibleSize()40     const ui::Size getInputVisibleSize() const {
41         return ui::Size(mInputVisibleSize->width, mInputVisibleSize->height);
42     }
getBlockPoolId()43     C2BlockPool::local_id_t getBlockPoolId() const { return mOutputBlockPoolIds->m.values[0]; }
44 
45     // Get sync key-frame period in frames.
46     uint32_t getKeyFramePeriod() const;
47     // Get the requested bitrate mode.
getBitrateMode()48     C2Config::bitrate_mode_t getBitrateMode() const { return mBitrateMode->value; }
49     // Get the requested bitrate.
getBitrate()50     uint32_t getBitrate() const { return mBitrate->value; }
51     // Get the requested framerate.
getFramerate()52     float getFramerate() const { return mFrameRate->value; }
53 
54     // Request changing the framerate to the specified value.
setFramerate(uint32_t framerate)55     void setFramerate(uint32_t framerate) { mFrameRate->value = framerate; }
56 
57 protected:
58     void Initialize(const C2String& name, const SupportedCapabilities& caps);
59 
60     // Configurable parameter setters.
61     static C2R H264ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::output>& info,
62                                       const C2P<C2StreamPictureSizeInfo::input>& videosize,
63                                       const C2P<C2StreamFrameRateInfo::output>& frameRate,
64                                       const C2P<C2StreamBitrateInfo::output>& bitrate);
65     static C2R VP9ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::output>& info,
66                                      const C2P<C2StreamPictureSizeInfo::input>& videosize,
67                                      const C2P<C2StreamFrameRateInfo::output>& frameRate,
68                                      const C2P<C2StreamBitrateInfo::output>& bitrate);
69 
70     static C2R SizeSetter(bool mayBlock, C2P<C2StreamPictureSizeInfo::input>& videoSize);
71 
72     static C2R IntraRefreshPeriodSetter(bool mayBlock,
73                                         C2P<C2StreamIntraRefreshTuning::output>& period);
74 
75     // Recorded lowest configured level
76     // Is static for the need to use H264ProfileLevelSetter as a setter
77     static C2Config::level_t lowestConfigLevel;
78 
79     // Constant parameters
80 
81     // The kind of the component; should be C2Component::KIND_ENCODER.
82     std::shared_ptr<C2ComponentKindSetting> mKind;
83     // The input format kind; should be C2FormatVideo.
84     std::shared_ptr<C2StreamBufferTypeSetting::input> mInputFormat;
85     // The memory usage flag of input buffer; should be BufferUsage::VIDEO_ENCODER.
86     std::shared_ptr<C2StreamUsageTuning::input> mInputMemoryUsage;
87     // The output format kind; should be C2FormatCompressed.
88     std::shared_ptr<C2StreamBufferTypeSetting::output> mOutputFormat;
89     // The MIME type of input port; should be MEDIA_MIMETYPE_VIDEO_RAW.
90     std::shared_ptr<C2PortMediaTypeSetting::input> mInputMediaType;
91     // The MIME type of output port.
92     std::shared_ptr<C2PortMediaTypeSetting::output> mOutputMediaType;
93 
94     // The suggested usage of input buffer allocator ID.
95     std::shared_ptr<C2PortAllocatorsTuning::input> mInputAllocatorIds;
96     // The suggested usage of output buffer allocator ID.
97     std::shared_ptr<C2PortAllocatorsTuning::output> mOutputAllocatorIds;
98 
99     // Initialization parameters
100 
101     // The visible size for input raw video.
102     std::shared_ptr<C2StreamPictureSizeInfo::input> mInputVisibleSize;
103     // The output codec profile and level.
104     std::shared_ptr<C2StreamProfileLevelInfo::output> mProfileLevel;
105     // The expected period for key frames in microseconds.
106     std::shared_ptr<C2StreamSyncFrameIntervalTuning::output> mKeyFramePeriodUs;
107     // Component uses this ID to fetch corresponding output block pool from platform.
108     std::shared_ptr<C2PortBlockPoolsTuning::output> mOutputBlockPoolIds;
109 
110     // Dynamic parameters
111 
112     // The requested bitrate of the encoded output stream, in bits per second.
113     std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
114     // The requested bitrate mode.
115     std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode;
116     // The requested framerate, in frames per second.
117     std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
118     // The switch-type parameter that will be set to true while client requests keyframe. It
119     // will be reset once encoder gets the request.
120     std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestKeyFrame;
121     // The intra-frame refresh period. This is unused for the component now.
122     // TODO: adapt intra refresh period to encoder.
123     std::shared_ptr<C2StreamIntraRefreshTuning::output> mIntraRefreshPeriod;
124 
125     c2_status_t mInitStatus = C2_NO_INIT;
126 };
127 
128 }  // namespace android
129 
130 #endif  // ANDROID_V4L2_CODEC2_COMPONENTS_ENCODE_INTERFACE_H
131