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 "C2SoftFlacEnc" |
| 19 | #include <log/log.h> |
| 20 | |
Andy Hung | 1188a05 | 2019-01-02 13:09:52 -0800 | [diff] [blame] | 21 | #include <audio_utils/primitives.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 22 | #include <media/stagefright/foundation/MediaDefs.h> |
| 23 | |
Harish Mahendrakar | 155481c | 2024-04-12 16:19:18 +0530 | [diff] [blame] | 24 | #include <C2Debug.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 25 | #include <C2PlatformSupport.h> |
| 26 | #include <SimpleC2Interface.h> |
| 27 | |
| 28 | #include "C2SoftFlacEnc.h" |
| 29 | |
| 30 | namespace android { |
| 31 | |
Rakesh Kumar | 66d9d06 | 2019-03-12 17:46:17 +0530 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | constexpr char COMPONENT_NAME[] = "c2.android.flac.encoder"; |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | class C2SoftFlacEnc::IntfImpl : public SimpleInterface<void>::BaseParams { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 39 | public: |
| 40 | explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper) |
Rakesh Kumar | 66d9d06 | 2019-03-12 17:46:17 +0530 | [diff] [blame] | 41 | : SimpleInterface<void>::BaseParams( |
| 42 | helper, |
| 43 | COMPONENT_NAME, |
| 44 | C2Component::KIND_ENCODER, |
| 45 | C2Component::DOMAIN_AUDIO, |
| 46 | MEDIA_MIMETYPE_AUDIO_FLAC) { |
| 47 | noPrivateBuffers(); |
| 48 | noInputReferences(); |
| 49 | noOutputReferences(); |
| 50 | noInputLatency(); |
| 51 | noTimeStretch(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 52 | setDerivedInstance(this); |
Rakesh Kumar | 66d9d06 | 2019-03-12 17:46:17 +0530 | [diff] [blame] | 53 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 54 | addParameter( |
Rakesh Kumar | 66d9d06 | 2019-03-12 17:46:17 +0530 | [diff] [blame] | 55 | DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES) |
| 56 | .withConstValue(new C2ComponentAttributesSetting( |
| 57 | C2Component::ATTRIB_IS_TEMPORAL)) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 58 | .build()); |
| 59 | addParameter( |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 60 | DefineParam(mSampleRate, C2_PARAMKEY_SAMPLE_RATE) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 61 | .withDefault(new C2StreamSampleRateInfo::input(0u, 44100)) |
| 62 | .withFields({C2F(mSampleRate, value).inRange(1, 655350)}) |
| 63 | .withSetter((Setter<decltype(*mSampleRate)>::StrictValueWithNoDeps)) |
| 64 | .build()); |
| 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::input(0u, 1)) |
| 68 | .withFields({C2F(mChannelCount, value).inRange(1, 2)}) |
| 69 | .withSetter(Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps) |
| 70 | .build()); |
| 71 | addParameter( |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 72 | DefineParam(mBitrate, C2_PARAMKEY_BITRATE) |
| 73 | .withDefault(new C2StreamBitrateInfo::output(0u, 768000)) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 74 | .withFields({C2F(mBitrate, value).inRange(1, 21000000)}) |
| 75 | .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps) |
| 76 | .build()); |
| 77 | addParameter( |
Harish Mahendrakar | b99d91f | 2019-05-18 18:33:16 -0700 | [diff] [blame] | 78 | DefineParam(mComplexity, C2_PARAMKEY_COMPLEXITY) |
| 79 | .withDefault(new C2StreamComplexityTuning::output(0u, |
| 80 | FLAC_COMPRESSION_LEVEL_DEFAULT)) |
| 81 | .withFields({C2F(mComplexity, value).inRange( |
| 82 | FLAC_COMPRESSION_LEVEL_MIN, FLAC_COMPRESSION_LEVEL_MAX)}) |
| 83 | .withSetter(Setter<decltype(*mComplexity)>::NonStrictValueWithNoDeps) |
| 84 | .build()); |
Andy Hung | 1188a05 | 2019-01-02 13:09:52 -0800 | [diff] [blame] | 85 | |
| 86 | addParameter( |
| 87 | DefineParam(mPcmEncodingInfo, C2_PARAMKEY_PCM_ENCODING) |
| 88 | .withDefault(new C2StreamPcmEncodingInfo::input(0u, C2Config::PCM_16)) |
| 89 | .withFields({C2F(mPcmEncodingInfo, value).oneOf({ |
| 90 | C2Config::PCM_16, |
| 91 | // C2Config::PCM_8, |
| 92 | C2Config::PCM_FLOAT}) |
| 93 | }) |
| 94 | .withSetter((Setter<decltype(*mPcmEncodingInfo)>::StrictValueWithNoDeps)) |
| 95 | .build()); |
Harish Mahendrakar | 155481c | 2024-04-12 16:19:18 +0530 | [diff] [blame] | 96 | |
| 97 | addParameter( |
| 98 | DefineParam(mInputMaxBufSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE) |
| 99 | .withDefault(new C2StreamMaxBufferSizeInfo::input(0u, kMaxBlockSize)) |
| 100 | .withFields({ |
| 101 | C2F(mInputMaxBufSize, value).any(), |
| 102 | }) |
| 103 | .withSetter(MaxInputSizeSetter, mChannelCount, mPcmEncodingInfo) |
| 104 | .build()); |
| 105 | } |
| 106 | |
| 107 | static C2R MaxInputSizeSetter(bool mayBlock, |
| 108 | C2P<C2StreamMaxBufferSizeInfo::input> &me, |
| 109 | const C2P<C2StreamChannelCountInfo::input> &channelCount, |
| 110 | const C2P<C2StreamPcmEncodingInfo::input> &pcmEncoding) { |
| 111 | (void)mayBlock; |
| 112 | C2R res = C2R::Ok(); |
| 113 | int bytesPerSample = pcmEncoding.v.value == C2Config::PCM_FLOAT ? 4 : 2; |
| 114 | me.set().value = kMaxBlockSize * bytesPerSample * channelCount.v.value; |
| 115 | return res; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | uint32_t getSampleRate() const { return mSampleRate->value; } |
| 119 | uint32_t getChannelCount() const { return mChannelCount->value; } |
| 120 | uint32_t getBitrate() const { return mBitrate->value; } |
Harish Mahendrakar | b99d91f | 2019-05-18 18:33:16 -0700 | [diff] [blame] | 121 | uint32_t getComplexity() const { return mComplexity->value; } |
Andy Hung | 1188a05 | 2019-01-02 13:09:52 -0800 | [diff] [blame] | 122 | int32_t getPcmEncodingInfo() const { return mPcmEncodingInfo->value; } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 123 | |
| 124 | private: |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 125 | std::shared_ptr<C2StreamSampleRateInfo::input> mSampleRate; |
| 126 | std::shared_ptr<C2StreamChannelCountInfo::input> mChannelCount; |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 127 | std::shared_ptr<C2StreamBitrateInfo::output> mBitrate; |
Harish Mahendrakar | b99d91f | 2019-05-18 18:33:16 -0700 | [diff] [blame] | 128 | std::shared_ptr<C2StreamComplexityTuning::output> mComplexity; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 129 | std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize; |
Andy Hung | 1188a05 | 2019-01-02 13:09:52 -0800 | [diff] [blame] | 130 | std::shared_ptr<C2StreamPcmEncodingInfo::input> mPcmEncodingInfo; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 131 | }; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 132 | |
| 133 | C2SoftFlacEnc::C2SoftFlacEnc( |
| 134 | const char *name, |
| 135 | c2_node_id_t id, |
| 136 | const std::shared_ptr<IntfImpl> &intfImpl) |
| 137 | : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)), |
| 138 | mIntf(intfImpl), |
| 139 | mFlacStreamEncoder(nullptr), |
| 140 | mInputBufferPcm32(nullptr) { |
| 141 | } |
| 142 | |
| 143 | C2SoftFlacEnc::~C2SoftFlacEnc() { |
| 144 | onRelease(); |
| 145 | } |
| 146 | |
| 147 | c2_status_t C2SoftFlacEnc::onInit() { |
| 148 | mFlacStreamEncoder = FLAC__stream_encoder_new(); |
| 149 | if (!mFlacStreamEncoder) return C2_CORRUPTED; |
| 150 | |
| 151 | mInputBufferPcm32 = (FLAC__int32*) malloc( |
| 152 | kInBlockSize * kMaxNumChannels * sizeof(FLAC__int32)); |
| 153 | if (!mInputBufferPcm32) return C2_NO_MEMORY; |
| 154 | |
| 155 | mSignalledError = false; |
| 156 | mSignalledOutputEos = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 157 | mIsFirstFrame = true; |
Aayush Soni | f98dd67 | 2023-07-25 12:01:53 +0530 | [diff] [blame] | 158 | mAnchorTimeStamp = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 159 | mProcessedSamples = 0u; |
| 160 | mEncoderWriteData = false; |
| 161 | mEncoderReturnedNbBytes = 0; |
| 162 | mHeaderOffset = 0; |
| 163 | mWroteHeader = false; |
| 164 | |
| 165 | status_t err = configureEncoder(); |
| 166 | return err == OK ? C2_OK : C2_CORRUPTED; |
| 167 | } |
| 168 | |
| 169 | void C2SoftFlacEnc::onRelease() { |
| 170 | if (mFlacStreamEncoder) { |
| 171 | FLAC__stream_encoder_delete(mFlacStreamEncoder); |
| 172 | mFlacStreamEncoder = nullptr; |
| 173 | } |
| 174 | |
| 175 | if (mInputBufferPcm32) { |
| 176 | free(mInputBufferPcm32); |
| 177 | mInputBufferPcm32 = nullptr; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void C2SoftFlacEnc::onReset() { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 182 | (void) onStop(); |
| 183 | } |
| 184 | |
| 185 | c2_status_t C2SoftFlacEnc::onStop() { |
| 186 | mSignalledError = false; |
| 187 | mSignalledOutputEos = false; |
| 188 | mIsFirstFrame = true; |
Aayush Soni | f98dd67 | 2023-07-25 12:01:53 +0530 | [diff] [blame] | 189 | mAnchorTimeStamp = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 190 | mProcessedSamples = 0u; |
| 191 | mEncoderWriteData = false; |
| 192 | mEncoderReturnedNbBytes = 0; |
| 193 | mHeaderOffset = 0; |
| 194 | mWroteHeader = false; |
| 195 | |
| 196 | c2_status_t status = drain(DRAIN_COMPONENT_NO_EOS, nullptr); |
| 197 | if (C2_OK != status) return status; |
| 198 | |
| 199 | status_t err = configureEncoder(); |
| 200 | if (err != OK) mSignalledError = true; |
| 201 | return C2_OK; |
| 202 | } |
| 203 | |
| 204 | c2_status_t C2SoftFlacEnc::onFlush_sm() { |
| 205 | return onStop(); |
| 206 | } |
| 207 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 208 | void C2SoftFlacEnc::process( |
| 209 | const std::unique_ptr<C2Work> &work, |
| 210 | const std::shared_ptr<C2BlockPool> &pool) { |
| 211 | // Initialize output work |
| 212 | work->result = C2_OK; |
| 213 | work->workletsProcessed = 1u; |
| 214 | work->worklets.front()->output.flags = work->input.flags; |
| 215 | |
| 216 | if (mSignalledError || mSignalledOutputEos) { |
| 217 | work->result = C2_BAD_VALUE; |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0); |
| 222 | C2ReadView rView = mDummyReadView; |
| 223 | size_t inOffset = 0u; |
| 224 | size_t inSize = 0u; |
| 225 | if (!work->input.buffers.empty()) { |
| 226 | rView = work->input.buffers[0]->data().linearBlocks().front().map().get(); |
| 227 | inSize = rView.capacity(); |
| 228 | if (inSize && rView.error()) { |
| 229 | ALOGE("read view map failed %d", rView.error()); |
| 230 | work->result = C2_CORRUPTED; |
| 231 | return; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x", |
| 236 | inSize, (int)work->input.ordinal.timestamp.peeku(), |
| 237 | (int)work->input.ordinal.frameIndex.peeku(), work->input.flags); |
| 238 | if (mIsFirstFrame && inSize) { |
Aayush Soni | f98dd67 | 2023-07-25 12:01:53 +0530 | [diff] [blame] | 239 | mAnchorTimeStamp = work->input.ordinal.timestamp.peekll(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 240 | mIsFirstFrame = false; |
| 241 | } |
| 242 | |
| 243 | if (!mWroteHeader) { |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 244 | std::unique_ptr<C2StreamInitDataInfo::output> csd = |
| 245 | C2StreamInitDataInfo::output::AllocUnique(mHeaderOffset, 0u); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 246 | if (!csd) { |
| 247 | ALOGE("CSD allocation failed"); |
| 248 | mSignalledError = true; |
| 249 | work->result = C2_NO_MEMORY; |
| 250 | return; |
| 251 | } |
| 252 | memcpy(csd->m.value, mHeader, mHeaderOffset); |
| 253 | ALOGV("put csd, %d bytes", mHeaderOffset); |
| 254 | |
| 255 | work->worklets.front()->output.configUpdate.push_back(std::move(csd)); |
| 256 | mWroteHeader = true; |
| 257 | } |
| 258 | |
Andy Hung | 1188a05 | 2019-01-02 13:09:52 -0800 | [diff] [blame] | 259 | const uint32_t channelCount = mIntf->getChannelCount(); |
| 260 | const bool inputFloat = mIntf->getPcmEncodingInfo() == C2Config::PCM_FLOAT; |
| 261 | const unsigned sampleSize = inputFloat ? sizeof(float) : sizeof(int16_t); |
| 262 | const unsigned frameSize = channelCount * sampleSize; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 263 | |
| 264 | size_t outCapacity = inSize; |
Andy Hung | 1188a05 | 2019-01-02 13:09:52 -0800 | [diff] [blame] | 265 | outCapacity += mBlockSize * frameSize; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 266 | |
| 267 | C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE }; |
| 268 | c2_status_t err = pool->fetchLinearBlock(outCapacity, usage, &mOutputBlock); |
| 269 | if (err != C2_OK) { |
| 270 | ALOGE("fetchLinearBlock for Output failed with status %d", err); |
| 271 | work->result = C2_NO_MEMORY; |
| 272 | return; |
| 273 | } |
Chih-Yu Huang | 3d78091 | 2020-10-14 12:09:36 +0900 | [diff] [blame] | 274 | |
| 275 | err = mOutputBlock->map().get().error(); |
| 276 | if (err) { |
| 277 | ALOGE("write view map failed %d", err); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 278 | work->result = C2_CORRUPTED; |
| 279 | return; |
| 280 | } |
| 281 | |
Haripriya Deshmukh | 28f6450 | 2023-04-30 13:05:07 +0530 | [diff] [blame] | 282 | class FillWork { |
| 283 | public: |
| 284 | FillWork(uint32_t flags, C2WorkOrdinalStruct ordinal, |
| 285 | const std::shared_ptr<C2Buffer> &buffer) |
| 286 | : mFlags(flags), mOrdinal(ordinal), mBuffer(buffer) {} |
| 287 | ~FillWork() = default; |
| 288 | |
| 289 | void operator()(const std::unique_ptr<C2Work> &work) { |
| 290 | work->worklets.front()->output.flags = (C2FrameData::flags_t)mFlags; |
| 291 | work->worklets.front()->output.buffers.clear(); |
| 292 | work->worklets.front()->output.ordinal = mOrdinal; |
| 293 | work->workletsProcessed = 1u; |
| 294 | work->result = C2_OK; |
| 295 | if (mBuffer) { |
| 296 | work->worklets.front()->output.buffers.push_back(mBuffer); |
| 297 | } |
| 298 | ALOGV("timestamp = %lld, index = %lld, w/%s buffer", |
| 299 | mOrdinal.timestamp.peekll(), mOrdinal.frameIndex.peekll(), |
| 300 | mBuffer ? "" : "o"); |
| 301 | } |
| 302 | |
| 303 | private: |
| 304 | const uint32_t mFlags; |
| 305 | const C2WorkOrdinalStruct mOrdinal; |
| 306 | const std::shared_ptr<C2Buffer> mBuffer; |
| 307 | }; |
| 308 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 309 | mEncoderWriteData = true; |
| 310 | mEncoderReturnedNbBytes = 0; |
| 311 | size_t inPos = 0; |
| 312 | while (inPos < inSize) { |
| 313 | const uint8_t *inPtr = rView.data() + inOffset; |
Andy Hung | 1188a05 | 2019-01-02 13:09:52 -0800 | [diff] [blame] | 314 | const size_t processSize = MIN(kInBlockSize * frameSize, (inSize - inPos)); |
| 315 | const unsigned nbInputFrames = processSize / frameSize; |
| 316 | const unsigned nbInputSamples = processSize / sampleSize; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 317 | |
Andy Hung | 1188a05 | 2019-01-02 13:09:52 -0800 | [diff] [blame] | 318 | ALOGV("about to encode %zu bytes", processSize); |
| 319 | if (inputFloat) { |
| 320 | const float * const pcmFloat = reinterpret_cast<const float *>(inPtr + inPos); |
| 321 | memcpy_to_q8_23_from_float_with_clamp(mInputBufferPcm32, pcmFloat, nbInputSamples); |
| 322 | } else { |
| 323 | const int16_t * const pcm16 = reinterpret_cast<const int16_t *>(inPtr + inPos); |
| 324 | for (unsigned i = 0; i < nbInputSamples; i++) { |
| 325 | mInputBufferPcm32[i] = (FLAC__int32) pcm16[i]; |
| 326 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | FLAC__bool ok = FLAC__stream_encoder_process_interleaved( |
| 330 | mFlacStreamEncoder, mInputBufferPcm32, nbInputFrames); |
| 331 | if (!ok) { |
| 332 | ALOGE("error encountered during encoding"); |
| 333 | mSignalledError = true; |
| 334 | work->result = C2_CORRUPTED; |
| 335 | mOutputBlock.reset(); |
| 336 | return; |
| 337 | } |
| 338 | inPos += processSize; |
| 339 | } |
| 340 | if (eos && (C2_OK != drain(DRAIN_COMPONENT_WITH_EOS, pool))) { |
| 341 | ALOGE("error encountered during encoding"); |
| 342 | mSignalledError = true; |
| 343 | work->result = C2_CORRUPTED; |
| 344 | mOutputBlock.reset(); |
| 345 | return; |
| 346 | } |
Haripriya Deshmukh | 28f6450 | 2023-04-30 13:05:07 +0530 | [diff] [blame] | 347 | |
| 348 | // cloneAndSend will create clone of work when more than one encoded frame is produced |
| 349 | while (mOutputBuffers.size() > 1) { |
| 350 | const OutputBuffer& front = mOutputBuffers.front(); |
| 351 | C2WorkOrdinalStruct ordinal = work->input.ordinal; |
| 352 | ordinal.frameIndex = front.frameIndex; |
| 353 | ordinal.timestamp = front.timestampUs; |
| 354 | cloneAndSend(work->input.ordinal.frameIndex.peeku(), work, |
| 355 | FillWork(C2FrameData::FLAG_INCOMPLETE, ordinal, front.buffer)); |
| 356 | mOutputBuffers.pop_front(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 357 | } |
Haripriya Deshmukh | 28f6450 | 2023-04-30 13:05:07 +0530 | [diff] [blame] | 358 | |
| 359 | std::shared_ptr<C2Buffer> buffer; |
| 360 | C2WorkOrdinalStruct ordinal = work->input.ordinal; |
| 361 | if (mOutputBuffers.size() == 1) { |
| 362 | const OutputBuffer& front = mOutputBuffers.front(); |
| 363 | ordinal.frameIndex = front.frameIndex; |
| 364 | ordinal.timestamp = front.timestampUs; |
| 365 | buffer = front.buffer; |
| 366 | mOutputBuffers.pop_front(); |
| 367 | } |
| 368 | // finish the response for the overall transaction. |
| 369 | // this includes any final frame that the encoder produced during this request |
| 370 | // this response is required even if no data was encoded. |
| 371 | FillWork((C2FrameData::flags_t)(eos ? C2FrameData::FLAG_END_OF_STREAM : 0), |
| 372 | ordinal, buffer)(work); |
| 373 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 374 | mOutputBlock = nullptr; |
| 375 | if (eos) { |
| 376 | mSignalledOutputEos = true; |
| 377 | ALOGV("signalled EOS"); |
| 378 | } |
| 379 | mEncoderWriteData = false; |
| 380 | mEncoderReturnedNbBytes = 0; |
| 381 | } |
| 382 | |
| 383 | FLAC__StreamEncoderWriteStatus C2SoftFlacEnc::onEncodedFlacAvailable( |
| 384 | const FLAC__byte buffer[], size_t bytes, unsigned samples, |
| 385 | unsigned current_frame) { |
| 386 | (void) current_frame; |
| 387 | ALOGV("%s (bytes=%zu, samples=%u, curr_frame=%u)", __func__, bytes, samples, |
| 388 | current_frame); |
| 389 | |
| 390 | if (samples == 0) { |
| 391 | ALOGI("saving %zu bytes of header", bytes); |
| 392 | memcpy(mHeader + mHeaderOffset, buffer, bytes); |
| 393 | mHeaderOffset += bytes;// will contain header size when finished receiving header |
| 394 | return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; |
| 395 | } |
| 396 | |
| 397 | if ((samples == 0) || !mEncoderWriteData) { |
| 398 | // called by the encoder because there's header data to save, but it's not the role |
| 399 | // of this component (unless WRITE_FLAC_HEADER_IN_FIRST_BUFFER is defined) |
| 400 | ALOGV("ignoring %zu bytes of header data (samples=%d)", bytes, samples); |
| 401 | return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; |
| 402 | } |
| 403 | |
| 404 | // write encoded data |
| 405 | C2WriteView wView = mOutputBlock->map().get(); |
| 406 | uint8_t* outData = wView.data(); |
Haripriya Deshmukh | 28f6450 | 2023-04-30 13:05:07 +0530 | [diff] [blame] | 407 | const uint32_t sampleRate = mIntf->getSampleRate(); |
Aayush Soni | f98dd67 | 2023-07-25 12:01:53 +0530 | [diff] [blame] | 408 | const int64_t outTimeStamp = mProcessedSamples * 1000000ll / sampleRate; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 409 | ALOGV("writing %zu bytes of encoded data on output", bytes); |
| 410 | // increment mProcessedSamples to maintain audio synchronization during |
| 411 | // play back |
| 412 | mProcessedSamples += samples; |
| 413 | if (bytes + mEncoderReturnedNbBytes > mOutputBlock->capacity()) { |
| 414 | ALOGE("not enough space left to write encoded data, dropping %zu bytes", bytes); |
| 415 | // a fatal error would stop the encoding |
| 416 | return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; |
| 417 | } |
| 418 | memcpy(outData + mEncoderReturnedNbBytes, buffer, bytes); |
Haripriya Deshmukh | 28f6450 | 2023-04-30 13:05:07 +0530 | [diff] [blame] | 419 | |
| 420 | std::shared_ptr<C2Buffer> c2Buffer = |
| 421 | createLinearBuffer(mOutputBlock, mEncoderReturnedNbBytes, bytes); |
| 422 | mOutputBuffers.push_back({c2Buffer, mAnchorTimeStamp + outTimeStamp, current_frame}); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 423 | mEncoderReturnedNbBytes += bytes; |
Haripriya Deshmukh | 28f6450 | 2023-04-30 13:05:07 +0530 | [diff] [blame] | 424 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 425 | return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | status_t C2SoftFlacEnc::configureEncoder() { |
| 430 | ALOGV("%s numChannel=%d, sampleRate=%d", __func__, mIntf->getChannelCount(), mIntf->getSampleRate()); |
| 431 | |
| 432 | if (mSignalledError || !mFlacStreamEncoder) { |
| 433 | ALOGE("can't configure encoder: no encoder or invalid state"); |
| 434 | return UNKNOWN_ERROR; |
| 435 | } |
| 436 | |
Andy Hung | 1188a05 | 2019-01-02 13:09:52 -0800 | [diff] [blame] | 437 | const bool inputFloat = mIntf->getPcmEncodingInfo() == C2Config::PCM_FLOAT; |
| 438 | const int bitsPerSample = inputFloat ? 24 : 16; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 439 | FLAC__bool ok = true; |
| 440 | ok = ok && FLAC__stream_encoder_set_channels(mFlacStreamEncoder, mIntf->getChannelCount()); |
| 441 | ok = ok && FLAC__stream_encoder_set_sample_rate(mFlacStreamEncoder, mIntf->getSampleRate()); |
Andy Hung | 1188a05 | 2019-01-02 13:09:52 -0800 | [diff] [blame] | 442 | ok = ok && FLAC__stream_encoder_set_bits_per_sample(mFlacStreamEncoder, bitsPerSample); |
Harish Mahendrakar | b99d91f | 2019-05-18 18:33:16 -0700 | [diff] [blame] | 443 | ok = ok && FLAC__stream_encoder_set_compression_level(mFlacStreamEncoder, |
| 444 | mIntf->getComplexity()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 445 | ok = ok && FLAC__stream_encoder_set_verify(mFlacStreamEncoder, false); |
| 446 | if (!ok) { |
| 447 | ALOGE("unknown error when configuring encoder"); |
| 448 | return UNKNOWN_ERROR; |
| 449 | } |
| 450 | |
| 451 | ok &= FLAC__STREAM_ENCODER_INIT_STATUS_OK == |
| 452 | FLAC__stream_encoder_init_stream(mFlacStreamEncoder, |
| 453 | flacEncoderWriteCallback /*write_callback*/, |
| 454 | nullptr /*seek_callback*/, |
| 455 | nullptr /*tell_callback*/, |
| 456 | nullptr /*metadata_callback*/, |
| 457 | (void *) this /*client_data*/); |
| 458 | |
| 459 | if (!ok) { |
| 460 | ALOGE("unknown error when configuring encoder"); |
| 461 | return UNKNOWN_ERROR; |
| 462 | } |
| 463 | |
| 464 | mBlockSize = FLAC__stream_encoder_get_blocksize(mFlacStreamEncoder); |
| 465 | |
Harish Mahendrakar | 155481c | 2024-04-12 16:19:18 +0530 | [diff] [blame] | 466 | // Update kMaxBlockSize to match maximum size used by the encoder |
| 467 | CHECK(mBlockSize <= kMaxBlockSize); |
| 468 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 469 | ALOGV("encoder successfully configured"); |
| 470 | return OK; |
| 471 | } |
| 472 | |
| 473 | FLAC__StreamEncoderWriteStatus C2SoftFlacEnc::flacEncoderWriteCallback( |
| 474 | const FLAC__StreamEncoder *, |
| 475 | const FLAC__byte buffer[], |
| 476 | size_t bytes, |
| 477 | unsigned samples, |
| 478 | unsigned current_frame, |
| 479 | void *client_data) { |
| 480 | return ((C2SoftFlacEnc*) client_data)->onEncodedFlacAvailable( |
| 481 | buffer, bytes, samples, current_frame); |
| 482 | } |
| 483 | |
| 484 | c2_status_t C2SoftFlacEnc::drain( |
| 485 | uint32_t drainMode, |
| 486 | const std::shared_ptr<C2BlockPool> &pool) { |
| 487 | (void) pool; |
| 488 | switch (drainMode) { |
| 489 | case NO_DRAIN: |
| 490 | ALOGW("drain with NO_DRAIN: no-op"); |
| 491 | return C2_OK; |
| 492 | case DRAIN_CHAIN: |
| 493 | ALOGW("DRAIN_CHAIN not supported"); |
| 494 | return C2_OMITTED; |
| 495 | case DRAIN_COMPONENT_WITH_EOS: |
| 496 | // TODO: This flag is not being sent back to the client |
| 497 | // because there are no items in PendingWork queue as all the |
| 498 | // inputs are being sent back with emptywork or valid encoded data |
| 499 | // mSignalledOutputEos = true; |
| 500 | case DRAIN_COMPONENT_NO_EOS: |
| 501 | break; |
| 502 | default: |
| 503 | return C2_BAD_VALUE; |
| 504 | } |
| 505 | FLAC__bool ok = FLAC__stream_encoder_finish(mFlacStreamEncoder); |
| 506 | if (!ok) return C2_CORRUPTED; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 507 | |
| 508 | return C2_OK; |
| 509 | } |
| 510 | |
| 511 | class C2SoftFlacEncFactory : public C2ComponentFactory { |
| 512 | public: |
| 513 | C2SoftFlacEncFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>( |
| 514 | GetCodec2PlatformComponentStore()->getParamReflector())) { |
| 515 | } |
| 516 | |
| 517 | virtual c2_status_t createComponent( |
| 518 | c2_node_id_t id, |
| 519 | std::shared_ptr<C2Component>* const component, |
| 520 | std::function<void(C2Component*)> deleter) override { |
| 521 | *component = std::shared_ptr<C2Component>( |
| 522 | new C2SoftFlacEnc(COMPONENT_NAME, |
| 523 | id, |
| 524 | std::make_shared<C2SoftFlacEnc::IntfImpl>(mHelper)), |
| 525 | deleter); |
| 526 | return C2_OK; |
| 527 | } |
| 528 | |
| 529 | virtual c2_status_t createInterface( |
| 530 | c2_node_id_t id, |
| 531 | std::shared_ptr<C2ComponentInterface>* const interface, |
| 532 | std::function<void(C2ComponentInterface*)> deleter) override { |
| 533 | *interface = std::shared_ptr<C2ComponentInterface>( |
| 534 | new SimpleInterface<C2SoftFlacEnc::IntfImpl>( |
| 535 | COMPONENT_NAME, id, std::make_shared<C2SoftFlacEnc::IntfImpl>(mHelper)), |
| 536 | deleter); |
| 537 | return C2_OK; |
| 538 | } |
| 539 | |
| 540 | virtual ~C2SoftFlacEncFactory() override = default; |
| 541 | private: |
| 542 | std::shared_ptr<C2ReflectorHelper> mHelper; |
| 543 | }; |
| 544 | |
| 545 | } // namespace android |
| 546 | |
Cindy Zhou | f6c0c3c | 2020-12-02 10:53:40 -0800 | [diff] [blame] | 547 | __attribute__((cfi_canonical_jump_table)) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 548 | extern "C" ::C2ComponentFactory* CreateCodec2Factory() { |
| 549 | ALOGV("in %s", __func__); |
| 550 | return new ::android::C2SoftFlacEncFactory(); |
| 551 | } |
| 552 | |
Cindy Zhou | f6c0c3c | 2020-12-02 10:53:40 -0800 | [diff] [blame] | 553 | __attribute__((cfi_canonical_jump_table)) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 554 | extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) { |
| 555 | ALOGV("in %s", __func__); |
| 556 | delete factory; |
| 557 | } |