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 "C2SoftFlacDec" |
| 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 "C2SoftFlacDec.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.flac.decoder"; |
| 33 | |
Rakesh Kumar | ed13464 | 2019-03-12 14:18:42 +0530 | [diff] [blame] | 34 | } // namespace |
| 35 | |
| 36 | class C2SoftFlacDec::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_FLAC) { |
| 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)) |
| 61 | .withFields({C2F(mSampleRate, value).inRange(1, 655350)}) |
| 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, 1)) |
| 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, 768000)) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 75 | .withFields({C2F(mBitrate, value).inRange(1, 21000000)}) |
| 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, 32768)) |
| 82 | .build()); |
Andy Hung | 9ed79de | 2018-12-28 15:53:55 -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()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Andy Hung | 9ed79de | 2018-12-28 15:53:55 -0800 | [diff] [blame] | 96 | int32_t getPcmEncodingInfo() const { return mPcmEncodingInfo->value; } |
| 97 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 98 | private: |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 99 | std::shared_ptr<C2StreamSampleRateInfo::output> mSampleRate; |
| 100 | std::shared_ptr<C2StreamChannelCountInfo::output> mChannelCount; |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 101 | std::shared_ptr<C2StreamBitrateInfo::input> mBitrate; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 102 | std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize; |
Andy Hung | 9ed79de | 2018-12-28 15:53:55 -0800 | [diff] [blame] | 103 | std::shared_ptr<C2StreamPcmEncodingInfo::output> mPcmEncodingInfo; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | C2SoftFlacDec::C2SoftFlacDec( |
| 107 | const char *name, |
| 108 | c2_node_id_t id, |
| 109 | const std::shared_ptr<IntfImpl> &intfImpl) |
| 110 | : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)), |
| 111 | mIntf(intfImpl), |
| 112 | mFLACDecoder(nullptr) { |
| 113 | } |
| 114 | |
| 115 | C2SoftFlacDec::~C2SoftFlacDec() { |
| 116 | onRelease(); |
| 117 | } |
| 118 | |
| 119 | c2_status_t C2SoftFlacDec::onInit() { |
| 120 | status_t err = initDecoder(); |
| 121 | return err == OK ? C2_OK : C2_NO_MEMORY; |
| 122 | } |
| 123 | |
| 124 | c2_status_t C2SoftFlacDec::onStop() { |
| 125 | if (mFLACDecoder) mFLACDecoder->flush(); |
| 126 | memset(&mStreamInfo, 0, sizeof(mStreamInfo)); |
| 127 | mHasStreamInfo = false; |
| 128 | mSignalledError = false; |
| 129 | mSignalledOutputEos = false; |
| 130 | return C2_OK; |
| 131 | } |
| 132 | |
| 133 | void C2SoftFlacDec::onReset() { |
| 134 | mInputBufferCount = 0; |
| 135 | (void)onStop(); |
| 136 | } |
| 137 | |
| 138 | void C2SoftFlacDec::onRelease() { |
| 139 | mInputBufferCount = 0; |
| 140 | if (mFLACDecoder) delete mFLACDecoder; |
| 141 | mFLACDecoder = nullptr; |
| 142 | } |
| 143 | |
| 144 | c2_status_t C2SoftFlacDec::onFlush_sm() { |
| 145 | return onStop(); |
| 146 | } |
| 147 | |
| 148 | status_t C2SoftFlacDec::initDecoder() { |
| 149 | if (mFLACDecoder) { |
| 150 | delete mFLACDecoder; |
| 151 | } |
| 152 | mFLACDecoder = FLACDecoder::Create(); |
| 153 | if (!mFLACDecoder) { |
| 154 | ALOGE("initDecoder: failed to create FLACDecoder"); |
| 155 | mSignalledError = true; |
| 156 | return NO_MEMORY; |
| 157 | } |
| 158 | |
| 159 | memset(&mStreamInfo, 0, sizeof(mStreamInfo)); |
| 160 | mHasStreamInfo = false; |
| 161 | mSignalledError = false; |
| 162 | mSignalledOutputEos = false; |
| 163 | mInputBufferCount = 0; |
| 164 | |
| 165 | return OK; |
| 166 | } |
| 167 | |
| 168 | static void fillEmptyWork(const std::unique_ptr<C2Work> &work) { |
| 169 | work->worklets.front()->output.flags = work->input.flags; |
| 170 | work->worklets.front()->output.buffers.clear(); |
| 171 | work->worklets.front()->output.ordinal = work->input.ordinal; |
| 172 | work->workletsProcessed = 1u; |
| 173 | } |
| 174 | |
| 175 | // (TODO) add multiframe support, in plugin and FLACDecoder.cpp |
| 176 | void C2SoftFlacDec::process( |
| 177 | const std::unique_ptr<C2Work> &work, |
| 178 | const std::shared_ptr<C2BlockPool> &pool) { |
| 179 | // Initialize output work |
| 180 | work->result = C2_OK; |
| 181 | work->workletsProcessed = 1u; |
| 182 | work->worklets.front()->output.configUpdate.clear(); |
| 183 | work->worklets.front()->output.flags = work->input.flags; |
| 184 | |
| 185 | if (mSignalledError || mSignalledOutputEos) { |
| 186 | work->result = C2_BAD_VALUE; |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | C2ReadView rView = mDummyReadView; |
| 191 | size_t inOffset = 0u; |
| 192 | size_t inSize = 0u; |
| 193 | if (!work->input.buffers.empty()) { |
| 194 | rView = work->input.buffers[0]->data().linearBlocks().front().map().get(); |
| 195 | inSize = rView.capacity(); |
| 196 | if (inSize && rView.error()) { |
| 197 | ALOGE("read view map failed %d", rView.error()); |
| 198 | work->result = C2_CORRUPTED; |
| 199 | return; |
| 200 | } |
| 201 | } |
| 202 | bool eos = (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0; |
| 203 | bool codecConfig = (work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) != 0; |
| 204 | |
| 205 | ALOGV("in buffer attr. size %zu timestamp %d frameindex %d", inSize, |
| 206 | (int)work->input.ordinal.timestamp.peeku(), (int)work->input.ordinal.frameIndex.peeku()); |
| 207 | |
| 208 | if (inSize == 0) { |
| 209 | fillEmptyWork(work); |
| 210 | if (eos) { |
| 211 | mSignalledOutputEos = true; |
| 212 | ALOGV("signalled EOS"); |
| 213 | } |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | if (mInputBufferCount == 0 && !codecConfig) { |
| 218 | ALOGV("First frame has to include configuration, forcing config"); |
| 219 | codecConfig = true; |
| 220 | } |
| 221 | |
| 222 | uint8_t *input = const_cast<uint8_t *>(rView.data() + inOffset); |
| 223 | if (codecConfig) { |
| 224 | status_t decoderErr = mFLACDecoder->parseMetadata(input, inSize); |
| 225 | if (decoderErr != OK && decoderErr != WOULD_BLOCK) { |
| 226 | ALOGE("process: FLACDecoder parseMetaData returns error %d", decoderErr); |
| 227 | mSignalledError = true; |
| 228 | work->result = C2_CORRUPTED; |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | mInputBufferCount++; |
| 233 | fillEmptyWork(work); |
| 234 | if (eos) { |
| 235 | mSignalledOutputEos = true; |
| 236 | ALOGV("signalled EOS"); |
| 237 | } |
| 238 | |
| 239 | if (decoderErr == WOULD_BLOCK) { |
| 240 | ALOGV("process: parseMetadata is Blocking, Continue %d", decoderErr); |
| 241 | } else { |
| 242 | mStreamInfo = mFLACDecoder->getStreamInfo(); |
| 243 | if (mStreamInfo.sample_rate && mStreamInfo.max_blocksize && |
| 244 | mStreamInfo.channels) { |
| 245 | mHasStreamInfo = true; |
| 246 | C2StreamSampleRateInfo::output sampleRateInfo( |
| 247 | 0u, mStreamInfo.sample_rate); |
| 248 | C2StreamChannelCountInfo::output channelCountInfo( |
| 249 | 0u, mStreamInfo.channels); |
| 250 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
| 251 | c2_status_t err = |
| 252 | mIntf->config({&sampleRateInfo, &channelCountInfo}, |
| 253 | C2_MAY_BLOCK, &failures); |
| 254 | if (err == OK) { |
| 255 | work->worklets.front()->output.configUpdate.push_back( |
| 256 | C2Param::Copy(sampleRateInfo)); |
| 257 | work->worklets.front()->output.configUpdate.push_back( |
| 258 | C2Param::Copy(channelCountInfo)); |
| 259 | } else { |
| 260 | ALOGE("Config Update failed"); |
| 261 | mSignalledError = true; |
| 262 | work->result = C2_CORRUPTED; |
| 263 | return; |
| 264 | } |
| 265 | } |
| 266 | ALOGD("process: decoder configuration : %d Hz, %d channels, %d samples," |
| 267 | " %d block size", mStreamInfo.sample_rate, mStreamInfo.channels, |
| 268 | (int)mStreamInfo.total_samples, mStreamInfo.max_blocksize); |
| 269 | } |
| 270 | return; |
| 271 | } |
| 272 | |
Andy Hung | 9ed79de | 2018-12-28 15:53:55 -0800 | [diff] [blame] | 273 | const bool outputFloat = mIntf->getPcmEncodingInfo() == C2Config::PCM_FLOAT; |
| 274 | const size_t sampleSize = outputFloat ? sizeof(float) : sizeof(short); |
| 275 | size_t outSize = mHasStreamInfo ? |
| 276 | mStreamInfo.max_blocksize * mStreamInfo.channels * sampleSize |
| 277 | : kMaxBlockSize * FLACDecoder::kMaxChannels * sampleSize; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 278 | |
| 279 | std::shared_ptr<C2LinearBlock> block; |
| 280 | C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE }; |
| 281 | c2_status_t err = pool->fetchLinearBlock(outSize, usage, &block); |
| 282 | if (err != C2_OK) { |
| 283 | ALOGE("fetchLinearBlock for Output failed with status %d", err); |
| 284 | work->result = C2_NO_MEMORY; |
| 285 | return; |
| 286 | } |
| 287 | C2WriteView wView = block->map().get(); |
| 288 | if (wView.error()) { |
| 289 | ALOGE("write view map failed %d", wView.error()); |
| 290 | work->result = C2_CORRUPTED; |
| 291 | return; |
| 292 | } |
| 293 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 294 | status_t decoderErr = mFLACDecoder->decodeOneFrame( |
Andy Hung | 9ed79de | 2018-12-28 15:53:55 -0800 | [diff] [blame] | 295 | input, inSize, wView.data(), &outSize, outputFloat); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 296 | if (decoderErr != OK) { |
| 297 | ALOGE("process: FLACDecoder decodeOneFrame returns error %d", decoderErr); |
| 298 | mSignalledError = true; |
| 299 | work->result = C2_CORRUPTED; |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | mInputBufferCount++; |
| 304 | ALOGV("out buffer attr. size %zu", outSize); |
| 305 | work->worklets.front()->output.flags = work->input.flags; |
| 306 | work->worklets.front()->output.buffers.clear(); |
| 307 | work->worklets.front()->output.buffers.push_back(createLinearBuffer(block, 0, outSize)); |
| 308 | work->worklets.front()->output.ordinal = work->input.ordinal; |
| 309 | if (eos) { |
| 310 | mSignalledOutputEos = true; |
| 311 | ALOGV("signalled EOS"); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | c2_status_t C2SoftFlacDec::drain( |
| 316 | uint32_t drainMode, |
| 317 | const std::shared_ptr<C2BlockPool> &pool) { |
| 318 | (void) pool; |
| 319 | if (drainMode == NO_DRAIN) { |
| 320 | ALOGW("drain with NO_DRAIN: no-op"); |
| 321 | return C2_OK; |
| 322 | } |
| 323 | if (drainMode == DRAIN_CHAIN) { |
| 324 | ALOGW("DRAIN_CHAIN not supported"); |
| 325 | return C2_OMITTED; |
| 326 | } |
| 327 | |
| 328 | if (mFLACDecoder) mFLACDecoder->flush(); |
| 329 | |
| 330 | return C2_OK; |
| 331 | } |
| 332 | |
| 333 | class C2SoftFlacDecFactory : public C2ComponentFactory { |
| 334 | public: |
| 335 | C2SoftFlacDecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>( |
| 336 | GetCodec2PlatformComponentStore()->getParamReflector())) { |
| 337 | } |
| 338 | |
| 339 | virtual c2_status_t createComponent( |
| 340 | c2_node_id_t id, |
| 341 | std::shared_ptr<C2Component>* const component, |
| 342 | std::function<void(C2Component*)> deleter) override { |
| 343 | *component = std::shared_ptr<C2Component>( |
| 344 | new C2SoftFlacDec(COMPONENT_NAME, |
| 345 | id, |
| 346 | std::make_shared<C2SoftFlacDec::IntfImpl>(mHelper)), |
| 347 | deleter); |
| 348 | return C2_OK; |
| 349 | } |
| 350 | |
| 351 | virtual c2_status_t createInterface( |
| 352 | c2_node_id_t id, |
| 353 | std::shared_ptr<C2ComponentInterface>* const interface, |
| 354 | std::function<void(C2ComponentInterface*)> deleter) override { |
| 355 | *interface = std::shared_ptr<C2ComponentInterface>( |
| 356 | new SimpleInterface<C2SoftFlacDec::IntfImpl>( |
| 357 | COMPONENT_NAME, id, std::make_shared<C2SoftFlacDec::IntfImpl>(mHelper)), |
| 358 | deleter); |
| 359 | return C2_OK; |
| 360 | } |
| 361 | |
| 362 | virtual ~C2SoftFlacDecFactory() override = default; |
| 363 | |
| 364 | private: |
| 365 | std::shared_ptr<C2ReflectorHelper> mHelper; |
| 366 | }; |
| 367 | |
| 368 | } // namespace android |
| 369 | |
| 370 | extern "C" ::C2ComponentFactory* CreateCodec2Factory() { |
| 371 | ALOGV("in %s", __func__); |
| 372 | return new ::android::C2SoftFlacDecFactory(); |
| 373 | } |
| 374 | |
| 375 | extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) { |
| 376 | ALOGV("in %s", __func__); |
| 377 | delete factory; |
| 378 | } |