Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "C2SoftRawDec" |
| 19 | #include <log/log.h> |
| 20 | |
| 21 | #include <media/stagefright/foundation/MediaDefs.h> |
| 22 | |
| 23 | #include <C2PlatformSupport.h> |
| 24 | #include <SimpleC2Interface.h> |
| 25 | |
| 26 | #include "C2SoftRawDec.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
Rakesh Kumar | ed13464 | 2019-03-12 14:18:42 +0530 | [diff] [blame] | 30 | namespace { |
| 31 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 32 | constexpr char COMPONENT_NAME[] = "c2.android.raw.decoder"; |
| 33 | |
Rakesh Kumar | ed13464 | 2019-03-12 14:18:42 +0530 | [diff] [blame] | 34 | } // namespace |
| 35 | |
| 36 | class C2SoftRawDec::IntfImpl : public SimpleInterface<void>::BaseParams { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 37 | public: |
| 38 | explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper) |
Rakesh Kumar | ed13464 | 2019-03-12 14:18:42 +0530 | [diff] [blame] | 39 | : SimpleInterface<void>::BaseParams( |
| 40 | helper, |
| 41 | COMPONENT_NAME, |
| 42 | C2Component::KIND_DECODER, |
| 43 | C2Component::DOMAIN_AUDIO, |
| 44 | MEDIA_MIMETYPE_AUDIO_RAW) { |
| 45 | noPrivateBuffers(); |
| 46 | noInputReferences(); |
| 47 | noOutputReferences(); |
| 48 | noInputLatency(); |
| 49 | noTimeStretch(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 50 | setDerivedInstance(this); |
| 51 | |
| 52 | addParameter( |
Rakesh Kumar | ed13464 | 2019-03-12 14:18:42 +0530 | [diff] [blame] | 53 | DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES) |
| 54 | .withConstValue(new C2ComponentAttributesSetting( |
| 55 | C2Component::ATTRIB_IS_TEMPORAL)) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 56 | .build()); |
| 57 | |
| 58 | addParameter( |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 59 | DefineParam(mSampleRate, C2_PARAMKEY_SAMPLE_RATE) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 60 | .withDefault(new C2StreamSampleRateInfo::output(0u, 44100)) |
Wonsik Kim | 1a15001 | 2020-01-21 09:48:44 -0800 | [diff] [blame] | 61 | .withFields({C2F(mSampleRate, value).greaterThan(0)}) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 62 | .withSetter((Setter<decltype(*mSampleRate)>::StrictValueWithNoDeps)) |
| 63 | .build()); |
| 64 | |
| 65 | addParameter( |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 66 | DefineParam(mChannelCount, C2_PARAMKEY_CHANNEL_COUNT) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 67 | .withDefault(new C2StreamChannelCountInfo::output(0u, 2)) |
| 68 | .withFields({C2F(mChannelCount, value).inRange(1, 8)}) |
| 69 | .withSetter(Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps) |
| 70 | .build()); |
| 71 | |
| 72 | addParameter( |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 73 | DefineParam(mBitrate, C2_PARAMKEY_BITRATE) |
| 74 | .withDefault(new C2StreamBitrateInfo::input(0u, 64000)) |
Marco Nelissen | 4054c97 | 2019-05-28 11:28:51 -0700 | [diff] [blame] | 75 | .withFields({C2F(mBitrate, value).inRange(1, 98304000)}) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 76 | .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps) |
| 77 | .build()); |
| 78 | |
| 79 | addParameter( |
| 80 | DefineParam(mInputMaxBufSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE) |
| 81 | .withConstValue(new C2StreamMaxBufferSizeInfo::input(0u, 64 * 1024)) |
| 82 | .build()); |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 83 | |
| 84 | addParameter( |
| 85 | DefineParam(mPcmEncodingInfo, C2_PARAMKEY_PCM_ENCODING) |
| 86 | .withDefault(new C2StreamPcmEncodingInfo::output(0u, C2Config::PCM_16)) |
| 87 | .withFields({C2F(mPcmEncodingInfo, value).oneOf({ |
| 88 | C2Config::PCM_16, |
| 89 | C2Config::PCM_8, |
| 90 | C2Config::PCM_FLOAT}) |
| 91 | }) |
| 92 | .withSetter((Setter<decltype(*mPcmEncodingInfo)>::StrictValueWithNoDeps)) |
| 93 | .build()); |
| 94 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | private: |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 98 | std::shared_ptr<C2StreamSampleRateInfo::output> mSampleRate; |
| 99 | std::shared_ptr<C2StreamChannelCountInfo::output> mChannelCount; |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 100 | std::shared_ptr<C2StreamBitrateInfo::input> mBitrate; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 101 | std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize; |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 102 | std::shared_ptr<C2StreamPcmEncodingInfo::output> mPcmEncodingInfo; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | C2SoftRawDec::C2SoftRawDec( |
| 106 | const char *name, |
| 107 | c2_node_id_t id, |
| 108 | const std::shared_ptr<IntfImpl> &intfImpl) |
| 109 | : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)), |
| 110 | mIntf(intfImpl) { |
| 111 | } |
| 112 | |
| 113 | C2SoftRawDec::~C2SoftRawDec() { |
| 114 | onRelease(); |
| 115 | } |
| 116 | |
| 117 | c2_status_t C2SoftRawDec::onInit() { |
| 118 | mSignalledEos = false; |
| 119 | return C2_OK; |
| 120 | } |
| 121 | |
| 122 | c2_status_t C2SoftRawDec::onStop() { |
| 123 | mSignalledEos = false; |
| 124 | return C2_OK; |
| 125 | } |
| 126 | |
| 127 | void C2SoftRawDec::onReset() { |
| 128 | (void)onStop(); |
| 129 | } |
| 130 | |
| 131 | void C2SoftRawDec::onRelease() { |
| 132 | } |
| 133 | |
| 134 | c2_status_t C2SoftRawDec::onFlush_sm() { |
| 135 | return onStop(); |
| 136 | } |
| 137 | |
| 138 | void C2SoftRawDec::process( |
| 139 | const std::unique_ptr<C2Work> &work, |
| 140 | const std::shared_ptr<C2BlockPool> &pool) { |
| 141 | (void)pool; |
| 142 | work->result = C2_OK; |
| 143 | work->workletsProcessed = 1u; |
| 144 | |
| 145 | if (mSignalledEos) { |
| 146 | work->result = C2_BAD_VALUE; |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | ALOGV("in buffer attr. timestamp %d frameindex %d", |
| 151 | (int)work->input.ordinal.timestamp.peeku(), (int)work->input.ordinal.frameIndex.peeku()); |
| 152 | |
| 153 | work->worklets.front()->output.flags = work->input.flags; |
| 154 | work->worklets.front()->output.buffers.clear(); |
| 155 | work->worklets.front()->output.ordinal = work->input.ordinal; |
| 156 | if (!work->input.buffers.empty()) { |
| 157 | work->worklets.front()->output.buffers.push_back(work->input.buffers[0]); |
| 158 | } |
| 159 | if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) { |
| 160 | mSignalledEos = true; |
| 161 | ALOGV("signalled EOS"); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | c2_status_t C2SoftRawDec::drain( |
| 166 | uint32_t drainMode, |
| 167 | const std::shared_ptr<C2BlockPool> &pool) { |
| 168 | (void) pool; |
| 169 | if (drainMode == NO_DRAIN) { |
| 170 | ALOGW("drain with NO_DRAIN: no-op"); |
| 171 | return C2_OK; |
| 172 | } |
| 173 | if (drainMode == DRAIN_CHAIN) { |
| 174 | ALOGW("DRAIN_CHAIN not supported"); |
| 175 | return C2_OMITTED; |
| 176 | } |
| 177 | |
| 178 | return C2_OK; |
| 179 | } |
| 180 | |
| 181 | class C2SoftRawDecFactory : public C2ComponentFactory { |
| 182 | public: |
| 183 | C2SoftRawDecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>( |
| 184 | GetCodec2PlatformComponentStore()->getParamReflector())) { |
| 185 | } |
| 186 | |
| 187 | virtual c2_status_t createComponent( |
| 188 | c2_node_id_t id, |
| 189 | std::shared_ptr<C2Component>* const component, |
| 190 | std::function<void(C2Component*)> deleter) override { |
| 191 | *component = std::shared_ptr<C2Component>( |
| 192 | new C2SoftRawDec(COMPONENT_NAME, |
| 193 | id, |
| 194 | std::make_shared<C2SoftRawDec::IntfImpl>(mHelper)), |
| 195 | deleter); |
| 196 | return C2_OK; |
| 197 | } |
| 198 | |
| 199 | virtual c2_status_t createInterface( |
| 200 | c2_node_id_t id, |
| 201 | std::shared_ptr<C2ComponentInterface>* const interface, |
| 202 | std::function<void(C2ComponentInterface*)> deleter) override { |
| 203 | *interface = std::shared_ptr<C2ComponentInterface>( |
| 204 | new SimpleInterface<C2SoftRawDec::IntfImpl>( |
| 205 | COMPONENT_NAME, id, std::make_shared<C2SoftRawDec::IntfImpl>(mHelper)), |
| 206 | deleter); |
| 207 | return C2_OK; |
| 208 | } |
| 209 | |
| 210 | virtual ~C2SoftRawDecFactory() override = default; |
| 211 | |
| 212 | private: |
| 213 | std::shared_ptr<C2ReflectorHelper> mHelper; |
| 214 | }; |
| 215 | |
| 216 | } // namespace android |
| 217 | |
| 218 | extern "C" ::C2ComponentFactory* CreateCodec2Factory() { |
| 219 | ALOGV("in %s", __func__); |
| 220 | return new ::android::C2SoftRawDecFactory(); |
| 221 | } |
| 222 | |
| 223 | extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) { |
| 224 | ALOGV("in %s", __func__); |
| 225 | delete factory; |
| 226 | } |