Vignesh Venkatasubramanian | 46e0639 | 2019-06-10 15:11:58 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "C2SoftGav1Dec" |
| 19 | #include "C2SoftGav1Dec.h" |
| 20 | |
| 21 | #include <C2Debug.h> |
| 22 | #include <C2PlatformSupport.h> |
| 23 | #include <SimpleC2Interface.h> |
| 24 | #include <log/log.h> |
| 25 | #include <media/stagefright/foundation/AUtils.h> |
| 26 | #include <media/stagefright/foundation/MediaDefs.h> |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | // TODO(vigneshv): This will be changed to c2.android.av1.decoder once this |
| 31 | // component is fully functional. |
| 32 | constexpr char COMPONENT_NAME[] = "c2.android.gav1.decoder"; |
| 33 | |
| 34 | class C2SoftGav1Dec::IntfImpl : public SimpleInterface<void>::BaseParams { |
| 35 | public: |
| 36 | explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper) |
| 37 | : SimpleInterface<void>::BaseParams( |
| 38 | helper, COMPONENT_NAME, C2Component::KIND_DECODER, |
| 39 | C2Component::DOMAIN_VIDEO, MEDIA_MIMETYPE_VIDEO_AV1) { |
| 40 | noPrivateBuffers(); // TODO: account for our buffers here. |
| 41 | noInputReferences(); |
| 42 | noOutputReferences(); |
| 43 | noInputLatency(); |
| 44 | noTimeStretch(); |
| 45 | |
| 46 | addParameter(DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES) |
| 47 | .withConstValue(new C2ComponentAttributesSetting( |
| 48 | C2Component::ATTRIB_IS_TEMPORAL)) |
| 49 | .build()); |
| 50 | |
| 51 | addParameter( |
| 52 | DefineParam(mSize, C2_PARAMKEY_PICTURE_SIZE) |
| 53 | .withDefault(new C2StreamPictureSizeInfo::output(0u, 320, 240)) |
| 54 | .withFields({ |
| 55 | C2F(mSize, width).inRange(2, 2048, 2), |
| 56 | C2F(mSize, height).inRange(2, 2048, 2), |
| 57 | }) |
| 58 | .withSetter(SizeSetter) |
| 59 | .build()); |
| 60 | |
| 61 | addParameter(DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL) |
| 62 | .withDefault(new C2StreamProfileLevelInfo::input( |
| 63 | 0u, C2Config::PROFILE_AV1_0, C2Config::LEVEL_AV1_2_1)) |
| 64 | .withFields({C2F(mProfileLevel, profile) |
| 65 | .oneOf({C2Config::PROFILE_AV1_0, |
| 66 | C2Config::PROFILE_AV1_1}), |
| 67 | C2F(mProfileLevel, level) |
| 68 | .oneOf({ |
| 69 | C2Config::LEVEL_AV1_2, |
| 70 | C2Config::LEVEL_AV1_2_1, |
| 71 | C2Config::LEVEL_AV1_2_2, |
| 72 | C2Config::LEVEL_AV1_3, |
| 73 | C2Config::LEVEL_AV1_3_1, |
| 74 | C2Config::LEVEL_AV1_3_2, |
| 75 | })}) |
| 76 | .withSetter(ProfileLevelSetter, mSize) |
| 77 | .build()); |
| 78 | |
| 79 | mHdr10PlusInfoInput = C2StreamHdr10PlusInfo::input::AllocShared(0); |
| 80 | addParameter( |
| 81 | DefineParam(mHdr10PlusInfoInput, C2_PARAMKEY_INPUT_HDR10_PLUS_INFO) |
| 82 | .withDefault(mHdr10PlusInfoInput) |
| 83 | .withFields({ |
| 84 | C2F(mHdr10PlusInfoInput, m.value).any(), |
| 85 | }) |
| 86 | .withSetter(Hdr10PlusInfoInputSetter) |
| 87 | .build()); |
| 88 | |
| 89 | mHdr10PlusInfoOutput = C2StreamHdr10PlusInfo::output::AllocShared(0); |
| 90 | addParameter( |
| 91 | DefineParam(mHdr10PlusInfoOutput, C2_PARAMKEY_OUTPUT_HDR10_PLUS_INFO) |
| 92 | .withDefault(mHdr10PlusInfoOutput) |
| 93 | .withFields({ |
| 94 | C2F(mHdr10PlusInfoOutput, m.value).any(), |
| 95 | }) |
| 96 | .withSetter(Hdr10PlusInfoOutputSetter) |
| 97 | .build()); |
| 98 | |
| 99 | addParameter( |
| 100 | DefineParam(mMaxSize, C2_PARAMKEY_MAX_PICTURE_SIZE) |
| 101 | .withDefault(new C2StreamMaxPictureSizeTuning::output(0u, 320, 240)) |
| 102 | .withFields({ |
| 103 | C2F(mSize, width).inRange(2, 2048, 2), |
| 104 | C2F(mSize, height).inRange(2, 2048, 2), |
| 105 | }) |
| 106 | .withSetter(MaxPictureSizeSetter, mSize) |
| 107 | .build()); |
| 108 | |
| 109 | addParameter(DefineParam(mMaxInputSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE) |
| 110 | .withDefault(new C2StreamMaxBufferSizeInfo::input( |
| 111 | 0u, 320 * 240 * 3 / 4)) |
| 112 | .withFields({ |
| 113 | C2F(mMaxInputSize, value).any(), |
| 114 | }) |
| 115 | .calculatedAs(MaxInputSizeSetter, mMaxSize) |
| 116 | .build()); |
| 117 | |
| 118 | C2ChromaOffsetStruct locations[1] = {C2ChromaOffsetStruct::ITU_YUV_420_0()}; |
| 119 | std::shared_ptr<C2StreamColorInfo::output> defaultColorInfo = |
| 120 | C2StreamColorInfo::output::AllocShared(1u, 0u, 8u /* bitDepth */, |
| 121 | C2Color::YUV_420); |
| 122 | memcpy(defaultColorInfo->m.locations, locations, sizeof(locations)); |
| 123 | |
| 124 | defaultColorInfo = C2StreamColorInfo::output::AllocShared( |
| 125 | {C2ChromaOffsetStruct::ITU_YUV_420_0()}, 0u, 8u /* bitDepth */, |
| 126 | C2Color::YUV_420); |
| 127 | helper->addStructDescriptors<C2ChromaOffsetStruct>(); |
| 128 | |
| 129 | addParameter(DefineParam(mColorInfo, C2_PARAMKEY_CODED_COLOR_INFO) |
| 130 | .withConstValue(defaultColorInfo) |
| 131 | .build()); |
| 132 | |
| 133 | addParameter( |
| 134 | DefineParam(mDefaultColorAspects, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS) |
| 135 | .withDefault(new C2StreamColorAspectsTuning::output( |
| 136 | 0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED, |
| 137 | C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED)) |
| 138 | .withFields( |
| 139 | {C2F(mDefaultColorAspects, range) |
| 140 | .inRange(C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER), |
| 141 | C2F(mDefaultColorAspects, primaries) |
| 142 | .inRange(C2Color::PRIMARIES_UNSPECIFIED, |
| 143 | C2Color::PRIMARIES_OTHER), |
| 144 | C2F(mDefaultColorAspects, transfer) |
| 145 | .inRange(C2Color::TRANSFER_UNSPECIFIED, |
| 146 | C2Color::TRANSFER_OTHER), |
| 147 | C2F(mDefaultColorAspects, matrix) |
| 148 | .inRange(C2Color::MATRIX_UNSPECIFIED, |
| 149 | C2Color::MATRIX_OTHER)}) |
| 150 | .withSetter(DefaultColorAspectsSetter) |
| 151 | .build()); |
| 152 | |
| 153 | // TODO: support more formats? |
| 154 | addParameter(DefineParam(mPixelFormat, C2_PARAMKEY_PIXEL_FORMAT) |
| 155 | .withConstValue(new C2StreamPixelFormatInfo::output( |
| 156 | 0u, HAL_PIXEL_FORMAT_YCBCR_420_888)) |
| 157 | .build()); |
| 158 | } |
| 159 | |
| 160 | static C2R SizeSetter(bool mayBlock, |
| 161 | const C2P<C2StreamPictureSizeInfo::output> &oldMe, |
| 162 | C2P<C2StreamPictureSizeInfo::output> &me) { |
| 163 | (void)mayBlock; |
| 164 | C2R res = C2R::Ok(); |
| 165 | if (!me.F(me.v.width).supportsAtAll(me.v.width)) { |
| 166 | res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width))); |
| 167 | me.set().width = oldMe.v.width; |
| 168 | } |
| 169 | if (!me.F(me.v.height).supportsAtAll(me.v.height)) { |
| 170 | res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height))); |
| 171 | me.set().height = oldMe.v.height; |
| 172 | } |
| 173 | return res; |
| 174 | } |
| 175 | |
| 176 | static C2R MaxPictureSizeSetter( |
| 177 | bool mayBlock, C2P<C2StreamMaxPictureSizeTuning::output> &me, |
| 178 | const C2P<C2StreamPictureSizeInfo::output> &size) { |
| 179 | (void)mayBlock; |
| 180 | // TODO: get max width/height from the size's field helpers vs. |
| 181 | // hardcoding |
| 182 | me.set().width = c2_min(c2_max(me.v.width, size.v.width), 4096u); |
| 183 | me.set().height = c2_min(c2_max(me.v.height, size.v.height), 4096u); |
| 184 | return C2R::Ok(); |
| 185 | } |
| 186 | |
| 187 | static C2R MaxInputSizeSetter( |
| 188 | bool mayBlock, C2P<C2StreamMaxBufferSizeInfo::input> &me, |
| 189 | const C2P<C2StreamMaxPictureSizeTuning::output> &maxSize) { |
| 190 | (void)mayBlock; |
| 191 | // assume compression ratio of 2 |
| 192 | me.set().value = |
| 193 | (((maxSize.v.width + 63) / 64) * ((maxSize.v.height + 63) / 64) * 3072); |
| 194 | return C2R::Ok(); |
| 195 | } |
| 196 | |
| 197 | static C2R DefaultColorAspectsSetter( |
| 198 | bool mayBlock, C2P<C2StreamColorAspectsTuning::output> &me) { |
| 199 | (void)mayBlock; |
| 200 | if (me.v.range > C2Color::RANGE_OTHER) { |
| 201 | me.set().range = C2Color::RANGE_OTHER; |
| 202 | } |
| 203 | if (me.v.primaries > C2Color::PRIMARIES_OTHER) { |
| 204 | me.set().primaries = C2Color::PRIMARIES_OTHER; |
| 205 | } |
| 206 | if (me.v.transfer > C2Color::TRANSFER_OTHER) { |
| 207 | me.set().transfer = C2Color::TRANSFER_OTHER; |
| 208 | } |
| 209 | if (me.v.matrix > C2Color::MATRIX_OTHER) { |
| 210 | me.set().matrix = C2Color::MATRIX_OTHER; |
| 211 | } |
| 212 | return C2R::Ok(); |
| 213 | } |
| 214 | |
| 215 | static C2R ProfileLevelSetter( |
| 216 | bool mayBlock, C2P<C2StreamProfileLevelInfo::input> &me, |
| 217 | const C2P<C2StreamPictureSizeInfo::output> &size) { |
| 218 | (void)mayBlock; |
| 219 | (void)size; |
| 220 | (void)me; // TODO: validate |
| 221 | return C2R::Ok(); |
| 222 | } |
| 223 | |
| 224 | std::shared_ptr<C2StreamColorAspectsTuning::output> |
| 225 | getDefaultColorAspects_l() { |
| 226 | return mDefaultColorAspects; |
| 227 | } |
| 228 | |
| 229 | static C2R Hdr10PlusInfoInputSetter(bool mayBlock, |
| 230 | C2P<C2StreamHdr10PlusInfo::input> &me) { |
| 231 | (void)mayBlock; |
| 232 | (void)me; // TODO: validate |
| 233 | return C2R::Ok(); |
| 234 | } |
| 235 | |
| 236 | static C2R Hdr10PlusInfoOutputSetter(bool mayBlock, |
| 237 | C2P<C2StreamHdr10PlusInfo::output> &me) { |
| 238 | (void)mayBlock; |
| 239 | (void)me; // TODO: validate |
| 240 | return C2R::Ok(); |
| 241 | } |
| 242 | |
| 243 | private: |
| 244 | std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel; |
| 245 | std::shared_ptr<C2StreamPictureSizeInfo::output> mSize; |
| 246 | std::shared_ptr<C2StreamMaxPictureSizeTuning::output> mMaxSize; |
| 247 | std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mMaxInputSize; |
| 248 | std::shared_ptr<C2StreamColorInfo::output> mColorInfo; |
| 249 | std::shared_ptr<C2StreamPixelFormatInfo::output> mPixelFormat; |
| 250 | std::shared_ptr<C2StreamColorAspectsTuning::output> mDefaultColorAspects; |
| 251 | std::shared_ptr<C2StreamHdr10PlusInfo::input> mHdr10PlusInfoInput; |
| 252 | std::shared_ptr<C2StreamHdr10PlusInfo::output> mHdr10PlusInfoOutput; |
| 253 | }; |
| 254 | |
| 255 | C2SoftGav1Dec::C2SoftGav1Dec(const char *name, c2_node_id_t id, |
| 256 | const std::shared_ptr<IntfImpl> &intfImpl) |
| 257 | : SimpleC2Component( |
| 258 | std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)), |
| 259 | mIntf(intfImpl) {} |
| 260 | |
| 261 | c2_status_t C2SoftGav1Dec::onInit() { return C2_OK; } |
| 262 | c2_status_t C2SoftGav1Dec::onStop() { return C2_OK; } |
| 263 | void C2SoftGav1Dec::onReset() {} |
| 264 | void C2SoftGav1Dec::onRelease(){}; |
| 265 | c2_status_t C2SoftGav1Dec::onFlush_sm() { return C2_OK; } |
| 266 | void C2SoftGav1Dec::process(const std::unique_ptr<C2Work> & /*work*/, |
| 267 | const std::shared_ptr<C2BlockPool> & /*pool*/) {} |
| 268 | c2_status_t C2SoftGav1Dec::drain( |
| 269 | uint32_t /*drainMode*/, const std::shared_ptr<C2BlockPool> & /*pool*/) { |
| 270 | return C2_OK; |
| 271 | } |
| 272 | |
| 273 | class C2SoftGav1Factory : public C2ComponentFactory { |
| 274 | public: |
| 275 | C2SoftGav1Factory() |
| 276 | : mHelper(std::static_pointer_cast<C2ReflectorHelper>( |
| 277 | GetCodec2PlatformComponentStore()->getParamReflector())) {} |
| 278 | |
| 279 | virtual c2_status_t createComponent( |
| 280 | c2_node_id_t id, std::shared_ptr<C2Component> *const component, |
| 281 | std::function<void(C2Component *)> deleter) override { |
| 282 | *component = std::shared_ptr<C2Component>( |
| 283 | new C2SoftGav1Dec(COMPONENT_NAME, id, |
| 284 | std::make_shared<C2SoftGav1Dec::IntfImpl>(mHelper)), |
| 285 | deleter); |
| 286 | return C2_OK; |
| 287 | } |
| 288 | |
| 289 | virtual c2_status_t createInterface( |
| 290 | c2_node_id_t id, std::shared_ptr<C2ComponentInterface> *const interface, |
| 291 | std::function<void(C2ComponentInterface *)> deleter) override { |
| 292 | *interface = std::shared_ptr<C2ComponentInterface>( |
| 293 | new SimpleInterface<C2SoftGav1Dec::IntfImpl>( |
| 294 | COMPONENT_NAME, id, |
| 295 | std::make_shared<C2SoftGav1Dec::IntfImpl>(mHelper)), |
| 296 | deleter); |
| 297 | return C2_OK; |
| 298 | } |
| 299 | |
| 300 | virtual ~C2SoftGav1Factory() override = default; |
| 301 | |
| 302 | private: |
| 303 | std::shared_ptr<C2ReflectorHelper> mHelper; |
| 304 | }; |
| 305 | |
| 306 | } // namespace android |
| 307 | |
| 308 | extern "C" ::C2ComponentFactory *CreateCodec2Factory() { |
| 309 | ALOGV("in %s", __func__); |
| 310 | return new ::android::C2SoftGav1Factory(); |
| 311 | } |
| 312 | |
| 313 | extern "C" void DestroyCodec2Factory(::C2ComponentFactory *factory) { |
| 314 | ALOGV("in %s", __func__); |
| 315 | delete factory; |
| 316 | } |