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