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
21 #include <audio_utils/primitives.h>
22 #include <media/stagefright/foundation/MediaDefs.h>
23
24 #include <C2PlatformSupport.h>
25 #include <SimpleC2Interface.h>
26
27 #include "C2SoftFlacEnc.h"
28
29 namespace android {
30
31 namespace {
32
33 constexpr char COMPONENT_NAME[] = "c2.android.flac.encoder";
34
35 } // namespace
36
37 class C2SoftFlacEnc::IntfImpl : public SimpleInterface<void>::BaseParams {
38 public:
IntfImpl(const std::shared_ptr<C2ReflectorHelper> & helper)39 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
40 : 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();
51 setDerivedInstance(this);
52
53 addParameter(
54 DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
55 .withConstValue(new C2ComponentAttributesSetting(
56 C2Component::ATTRIB_IS_TEMPORAL))
57 .build());
58 addParameter(
59 DefineParam(mSampleRate, C2_PARAMKEY_SAMPLE_RATE)
60 .withDefault(new C2StreamSampleRateInfo::input(0u, 44100))
61 .withFields({C2F(mSampleRate, value).inRange(1, 655350)})
62 .withSetter((Setter<decltype(*mSampleRate)>::StrictValueWithNoDeps))
63 .build());
64 addParameter(
65 DefineParam(mChannelCount, C2_PARAMKEY_CHANNEL_COUNT)
66 .withDefault(new C2StreamChannelCountInfo::input(0u, 1))
67 .withFields({C2F(mChannelCount, value).inRange(1, 2)})
68 .withSetter(Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps)
69 .build());
70 addParameter(
71 DefineParam(mBitrate, C2_PARAMKEY_BITRATE)
72 .withDefault(new C2StreamBitrateInfo::output(0u, 768000))
73 .withFields({C2F(mBitrate, value).inRange(1, 21000000)})
74 .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps)
75 .build());
76 addParameter(
77 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(
85 DefineParam(mInputMaxBufSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
86 .withConstValue(new C2StreamMaxBufferSizeInfo::input(0u, 4608))
87 .build());
88
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());
99 }
100
getSampleRate() const101 uint32_t getSampleRate() const { return mSampleRate->value; }
getChannelCount() const102 uint32_t getChannelCount() const { return mChannelCount->value; }
getBitrate() const103 uint32_t getBitrate() const { return mBitrate->value; }
getComplexity() const104 uint32_t getComplexity() const { return mComplexity->value; }
getPcmEncodingInfo() const105 int32_t getPcmEncodingInfo() const { return mPcmEncodingInfo->value; }
106
107 private:
108 std::shared_ptr<C2StreamSampleRateInfo::input> mSampleRate;
109 std::shared_ptr<C2StreamChannelCountInfo::input> mChannelCount;
110 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
111 std::shared_ptr<C2StreamComplexityTuning::output> mComplexity;
112 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
113 std::shared_ptr<C2StreamPcmEncodingInfo::input> mPcmEncodingInfo;
114 };
115
C2SoftFlacEnc(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)116 C2SoftFlacEnc::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
~C2SoftFlacEnc()126 C2SoftFlacEnc::~C2SoftFlacEnc() {
127 onRelease();
128 }
129
onInit()130 c2_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;
140 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
onRelease()152 void 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
onReset()164 void C2SoftFlacEnc::onReset() {
165 (void) onStop();
166 }
167
onStop()168 c2_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
onFlush_sm()187 c2_status_t C2SoftFlacEnc::onFlush_sm() {
188 return onStop();
189 }
190
fillEmptyWork(const std::unique_ptr<C2Work> & work)191 static void fillEmptyWork(const std::unique_ptr<C2Work> &work) {
192 work->worklets.front()->output.flags = work->input.flags;
193 work->worklets.front()->output.buffers.clear();
194 work->worklets.front()->output.ordinal = work->input.ordinal;
195 }
196
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)197 void C2SoftFlacEnc::process(
198 const std::unique_ptr<C2Work> &work,
199 const std::shared_ptr<C2BlockPool> &pool) {
200 // Initialize output work
201 work->result = C2_OK;
202 work->workletsProcessed = 1u;
203 work->worklets.front()->output.flags = work->input.flags;
204
205 if (mSignalledError || mSignalledOutputEos) {
206 work->result = C2_BAD_VALUE;
207 return;
208 }
209
210 bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
211 C2ReadView rView = mDummyReadView;
212 size_t inOffset = 0u;
213 size_t inSize = 0u;
214 if (!work->input.buffers.empty()) {
215 rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
216 inSize = rView.capacity();
217 if (inSize && rView.error()) {
218 ALOGE("read view map failed %d", rView.error());
219 work->result = C2_CORRUPTED;
220 return;
221 }
222 }
223
224 ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x",
225 inSize, (int)work->input.ordinal.timestamp.peeku(),
226 (int)work->input.ordinal.frameIndex.peeku(), work->input.flags);
227 if (mIsFirstFrame && inSize) {
228 mAnchorTimeStamp = work->input.ordinal.timestamp.peekull();
229 mIsFirstFrame = false;
230 }
231
232 if (!mWroteHeader) {
233 std::unique_ptr<C2StreamInitDataInfo::output> csd =
234 C2StreamInitDataInfo::output::AllocUnique(mHeaderOffset, 0u);
235 if (!csd) {
236 ALOGE("CSD allocation failed");
237 mSignalledError = true;
238 work->result = C2_NO_MEMORY;
239 return;
240 }
241 memcpy(csd->m.value, mHeader, mHeaderOffset);
242 ALOGV("put csd, %d bytes", mHeaderOffset);
243
244 work->worklets.front()->output.configUpdate.push_back(std::move(csd));
245 mWroteHeader = true;
246 }
247
248 const uint32_t sampleRate = mIntf->getSampleRate();
249 const uint32_t channelCount = mIntf->getChannelCount();
250 const bool inputFloat = mIntf->getPcmEncodingInfo() == C2Config::PCM_FLOAT;
251 const unsigned sampleSize = inputFloat ? sizeof(float) : sizeof(int16_t);
252 const unsigned frameSize = channelCount * sampleSize;
253 const uint64_t outTimeStamp = mProcessedSamples * 1000000ll / sampleRate;
254
255 size_t outCapacity = inSize;
256 outCapacity += mBlockSize * frameSize;
257
258 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
259 c2_status_t err = pool->fetchLinearBlock(outCapacity, usage, &mOutputBlock);
260 if (err != C2_OK) {
261 ALOGE("fetchLinearBlock for Output failed with status %d", err);
262 work->result = C2_NO_MEMORY;
263 return;
264 }
265
266 err = mOutputBlock->map().get().error();
267 if (err) {
268 ALOGE("write view map failed %d", err);
269 work->result = C2_CORRUPTED;
270 return;
271 }
272
273 mEncoderWriteData = true;
274 mEncoderReturnedNbBytes = 0;
275 size_t inPos = 0;
276 while (inPos < inSize) {
277 const uint8_t *inPtr = rView.data() + inOffset;
278 const size_t processSize = MIN(kInBlockSize * frameSize, (inSize - inPos));
279 const unsigned nbInputFrames = processSize / frameSize;
280 const unsigned nbInputSamples = processSize / sampleSize;
281
282 ALOGV("about to encode %zu bytes", processSize);
283 if (inputFloat) {
284 const float * const pcmFloat = reinterpret_cast<const float *>(inPtr + inPos);
285 memcpy_to_q8_23_from_float_with_clamp(mInputBufferPcm32, pcmFloat, nbInputSamples);
286 } else {
287 const int16_t * const pcm16 = reinterpret_cast<const int16_t *>(inPtr + inPos);
288 for (unsigned i = 0; i < nbInputSamples; i++) {
289 mInputBufferPcm32[i] = (FLAC__int32) pcm16[i];
290 }
291 }
292
293 FLAC__bool ok = FLAC__stream_encoder_process_interleaved(
294 mFlacStreamEncoder, mInputBufferPcm32, nbInputFrames);
295 if (!ok) {
296 ALOGE("error encountered during encoding");
297 mSignalledError = true;
298 work->result = C2_CORRUPTED;
299 mOutputBlock.reset();
300 return;
301 }
302 inPos += processSize;
303 }
304 if (eos && (C2_OK != drain(DRAIN_COMPONENT_WITH_EOS, pool))) {
305 ALOGE("error encountered during encoding");
306 mSignalledError = true;
307 work->result = C2_CORRUPTED;
308 mOutputBlock.reset();
309 return;
310 }
311 fillEmptyWork(work);
312 if (mEncoderReturnedNbBytes != 0) {
313 std::shared_ptr<C2Buffer> buffer = createLinearBuffer(std::move(mOutputBlock), 0, mEncoderReturnedNbBytes);
314 work->worklets.front()->output.buffers.push_back(buffer);
315 work->worklets.front()->output.ordinal.timestamp = mAnchorTimeStamp + outTimeStamp;
316 } else {
317 ALOGV("encoder process_interleaved returned without data to write");
318 }
319 mOutputBlock = nullptr;
320 if (eos) {
321 mSignalledOutputEos = true;
322 ALOGV("signalled EOS");
323 }
324 mEncoderWriteData = false;
325 mEncoderReturnedNbBytes = 0;
326 }
327
onEncodedFlacAvailable(const FLAC__byte buffer[],size_t bytes,unsigned samples,unsigned current_frame)328 FLAC__StreamEncoderWriteStatus C2SoftFlacEnc::onEncodedFlacAvailable(
329 const FLAC__byte buffer[], size_t bytes, unsigned samples,
330 unsigned current_frame) {
331 (void) current_frame;
332 ALOGV("%s (bytes=%zu, samples=%u, curr_frame=%u)", __func__, bytes, samples,
333 current_frame);
334
335 if (samples == 0) {
336 ALOGI("saving %zu bytes of header", bytes);
337 memcpy(mHeader + mHeaderOffset, buffer, bytes);
338 mHeaderOffset += bytes;// will contain header size when finished receiving header
339 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
340 }
341
342 if ((samples == 0) || !mEncoderWriteData) {
343 // called by the encoder because there's header data to save, but it's not the role
344 // of this component (unless WRITE_FLAC_HEADER_IN_FIRST_BUFFER is defined)
345 ALOGV("ignoring %zu bytes of header data (samples=%d)", bytes, samples);
346 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
347 }
348
349 // write encoded data
350 C2WriteView wView = mOutputBlock->map().get();
351 uint8_t* outData = wView.data();
352 ALOGV("writing %zu bytes of encoded data on output", bytes);
353 // increment mProcessedSamples to maintain audio synchronization during
354 // play back
355 mProcessedSamples += samples;
356 if (bytes + mEncoderReturnedNbBytes > mOutputBlock->capacity()) {
357 ALOGE("not enough space left to write encoded data, dropping %zu bytes", bytes);
358 // a fatal error would stop the encoding
359 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
360 }
361 memcpy(outData + mEncoderReturnedNbBytes, buffer, bytes);
362 mEncoderReturnedNbBytes += bytes;
363 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
364 }
365
366
configureEncoder()367 status_t C2SoftFlacEnc::configureEncoder() {
368 ALOGV("%s numChannel=%d, sampleRate=%d", __func__, mIntf->getChannelCount(), mIntf->getSampleRate());
369
370 if (mSignalledError || !mFlacStreamEncoder) {
371 ALOGE("can't configure encoder: no encoder or invalid state");
372 return UNKNOWN_ERROR;
373 }
374
375 const bool inputFloat = mIntf->getPcmEncodingInfo() == C2Config::PCM_FLOAT;
376 const int bitsPerSample = inputFloat ? 24 : 16;
377 FLAC__bool ok = true;
378 ok = ok && FLAC__stream_encoder_set_channels(mFlacStreamEncoder, mIntf->getChannelCount());
379 ok = ok && FLAC__stream_encoder_set_sample_rate(mFlacStreamEncoder, mIntf->getSampleRate());
380 ok = ok && FLAC__stream_encoder_set_bits_per_sample(mFlacStreamEncoder, bitsPerSample);
381 ok = ok && FLAC__stream_encoder_set_compression_level(mFlacStreamEncoder,
382 mIntf->getComplexity());
383 ok = ok && FLAC__stream_encoder_set_verify(mFlacStreamEncoder, false);
384 if (!ok) {
385 ALOGE("unknown error when configuring encoder");
386 return UNKNOWN_ERROR;
387 }
388
389 ok &= FLAC__STREAM_ENCODER_INIT_STATUS_OK ==
390 FLAC__stream_encoder_init_stream(mFlacStreamEncoder,
391 flacEncoderWriteCallback /*write_callback*/,
392 nullptr /*seek_callback*/,
393 nullptr /*tell_callback*/,
394 nullptr /*metadata_callback*/,
395 (void *) this /*client_data*/);
396
397 if (!ok) {
398 ALOGE("unknown error when configuring encoder");
399 return UNKNOWN_ERROR;
400 }
401
402 mBlockSize = FLAC__stream_encoder_get_blocksize(mFlacStreamEncoder);
403
404 ALOGV("encoder successfully configured");
405 return OK;
406 }
407
flacEncoderWriteCallback(const FLAC__StreamEncoder *,const FLAC__byte buffer[],size_t bytes,unsigned samples,unsigned current_frame,void * client_data)408 FLAC__StreamEncoderWriteStatus C2SoftFlacEnc::flacEncoderWriteCallback(
409 const FLAC__StreamEncoder *,
410 const FLAC__byte buffer[],
411 size_t bytes,
412 unsigned samples,
413 unsigned current_frame,
414 void *client_data) {
415 return ((C2SoftFlacEnc*) client_data)->onEncodedFlacAvailable(
416 buffer, bytes, samples, current_frame);
417 }
418
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)419 c2_status_t C2SoftFlacEnc::drain(
420 uint32_t drainMode,
421 const std::shared_ptr<C2BlockPool> &pool) {
422 (void) pool;
423 switch (drainMode) {
424 case NO_DRAIN:
425 ALOGW("drain with NO_DRAIN: no-op");
426 return C2_OK;
427 case DRAIN_CHAIN:
428 ALOGW("DRAIN_CHAIN not supported");
429 return C2_OMITTED;
430 case DRAIN_COMPONENT_WITH_EOS:
431 // TODO: This flag is not being sent back to the client
432 // because there are no items in PendingWork queue as all the
433 // inputs are being sent back with emptywork or valid encoded data
434 // mSignalledOutputEos = true;
435 case DRAIN_COMPONENT_NO_EOS:
436 break;
437 default:
438 return C2_BAD_VALUE;
439 }
440 FLAC__bool ok = FLAC__stream_encoder_finish(mFlacStreamEncoder);
441 if (!ok) return C2_CORRUPTED;
442 mIsFirstFrame = true;
443 mAnchorTimeStamp = 0ull;
444 mProcessedSamples = 0u;
445
446 return C2_OK;
447 }
448
449 class C2SoftFlacEncFactory : public C2ComponentFactory {
450 public:
C2SoftFlacEncFactory()451 C2SoftFlacEncFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
452 GetCodec2PlatformComponentStore()->getParamReflector())) {
453 }
454
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)455 virtual c2_status_t createComponent(
456 c2_node_id_t id,
457 std::shared_ptr<C2Component>* const component,
458 std::function<void(C2Component*)> deleter) override {
459 *component = std::shared_ptr<C2Component>(
460 new C2SoftFlacEnc(COMPONENT_NAME,
461 id,
462 std::make_shared<C2SoftFlacEnc::IntfImpl>(mHelper)),
463 deleter);
464 return C2_OK;
465 }
466
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)467 virtual c2_status_t createInterface(
468 c2_node_id_t id,
469 std::shared_ptr<C2ComponentInterface>* const interface,
470 std::function<void(C2ComponentInterface*)> deleter) override {
471 *interface = std::shared_ptr<C2ComponentInterface>(
472 new SimpleInterface<C2SoftFlacEnc::IntfImpl>(
473 COMPONENT_NAME, id, std::make_shared<C2SoftFlacEnc::IntfImpl>(mHelper)),
474 deleter);
475 return C2_OK;
476 }
477
478 virtual ~C2SoftFlacEncFactory() override = default;
479 private:
480 std::shared_ptr<C2ReflectorHelper> mHelper;
481 };
482
483 } // namespace android
484
485 __attribute__((cfi_canonical_jump_table))
CreateCodec2Factory()486 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
487 ALOGV("in %s", __func__);
488 return new ::android::C2SoftFlacEncFactory();
489 }
490
491 __attribute__((cfi_canonical_jump_table))
DestroyCodec2Factory(::C2ComponentFactory * factory)492 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
493 ALOGV("in %s", __func__);
494 delete factory;
495 }
496