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