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     C2WriteView wView = mOutputBlock->map().get();
266     if (wView.error()) {
267         ALOGE("write view map failed %d", wView.error());
268         work->result = C2_CORRUPTED;
269         return;
270     }
271 
272     mEncoderWriteData = true;
273     mEncoderReturnedNbBytes = 0;
274     size_t inPos = 0;
275     while (inPos < inSize) {
276         const uint8_t *inPtr = rView.data() + inOffset;
277         const size_t processSize = MIN(kInBlockSize * frameSize, (inSize - inPos));
278         const unsigned nbInputFrames = processSize / frameSize;
279         const unsigned nbInputSamples = processSize / sampleSize;
280 
281         ALOGV("about to encode %zu bytes", processSize);
282         if (inputFloat) {
283             const float * const pcmFloat = reinterpret_cast<const float *>(inPtr + inPos);
284             memcpy_to_q8_23_from_float_with_clamp(mInputBufferPcm32, pcmFloat, nbInputSamples);
285         } else {
286             const int16_t * const pcm16 = reinterpret_cast<const int16_t *>(inPtr + inPos);
287             for (unsigned i = 0; i < nbInputSamples; i++) {
288                 mInputBufferPcm32[i] = (FLAC__int32) pcm16[i];
289             }
290         }
291 
292         FLAC__bool ok = FLAC__stream_encoder_process_interleaved(
293                 mFlacStreamEncoder, mInputBufferPcm32, nbInputFrames);
294         if (!ok) {
295             ALOGE("error encountered during encoding");
296             mSignalledError = true;
297             work->result = C2_CORRUPTED;
298             mOutputBlock.reset();
299             return;
300         }
301         inPos += processSize;
302     }
303     if (eos && (C2_OK != drain(DRAIN_COMPONENT_WITH_EOS, pool))) {
304         ALOGE("error encountered during encoding");
305         mSignalledError = true;
306         work->result = C2_CORRUPTED;
307         mOutputBlock.reset();
308         return;
309     }
310     fillEmptyWork(work);
311     if (mEncoderReturnedNbBytes != 0) {
312         std::shared_ptr<C2Buffer> buffer = createLinearBuffer(std::move(mOutputBlock), 0, mEncoderReturnedNbBytes);
313         work->worklets.front()->output.buffers.push_back(buffer);
314         work->worklets.front()->output.ordinal.timestamp = mAnchorTimeStamp + outTimeStamp;
315     } else {
316         ALOGV("encoder process_interleaved returned without data to write");
317     }
318     mOutputBlock = nullptr;
319     if (eos) {
320         mSignalledOutputEos = true;
321         ALOGV("signalled EOS");
322     }
323     mEncoderWriteData = false;
324     mEncoderReturnedNbBytes = 0;
325 }
326 
onEncodedFlacAvailable(const FLAC__byte buffer[],size_t bytes,unsigned samples,unsigned current_frame)327 FLAC__StreamEncoderWriteStatus C2SoftFlacEnc::onEncodedFlacAvailable(
328         const FLAC__byte buffer[], size_t bytes, unsigned samples,
329         unsigned current_frame) {
330     (void) current_frame;
331     ALOGV("%s (bytes=%zu, samples=%u, curr_frame=%u)", __func__, bytes, samples,
332           current_frame);
333 
334     if (samples == 0) {
335         ALOGI("saving %zu bytes of header", bytes);
336         memcpy(mHeader + mHeaderOffset, buffer, bytes);
337         mHeaderOffset += bytes;// will contain header size when finished receiving header
338         return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
339     }
340 
341     if ((samples == 0) || !mEncoderWriteData) {
342         // called by the encoder because there's header data to save, but it's not the role
343         // of this component (unless WRITE_FLAC_HEADER_IN_FIRST_BUFFER is defined)
344         ALOGV("ignoring %zu bytes of header data (samples=%d)", bytes, samples);
345         return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
346     }
347 
348     // write encoded data
349     C2WriteView wView = mOutputBlock->map().get();
350     uint8_t* outData = wView.data();
351     ALOGV("writing %zu bytes of encoded data on output", bytes);
352     // increment mProcessedSamples to maintain audio synchronization during
353     // play back
354     mProcessedSamples += samples;
355     if (bytes + mEncoderReturnedNbBytes > mOutputBlock->capacity()) {
356         ALOGE("not enough space left to write encoded data, dropping %zu bytes", bytes);
357         // a fatal error would stop the encoding
358         return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
359     }
360     memcpy(outData + mEncoderReturnedNbBytes, buffer, bytes);
361     mEncoderReturnedNbBytes += bytes;
362     return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
363 }
364 
365 
configureEncoder()366 status_t C2SoftFlacEnc::configureEncoder() {
367     ALOGV("%s numChannel=%d, sampleRate=%d", __func__, mIntf->getChannelCount(), mIntf->getSampleRate());
368 
369     if (mSignalledError || !mFlacStreamEncoder) {
370         ALOGE("can't configure encoder: no encoder or invalid state");
371         return UNKNOWN_ERROR;
372     }
373 
374     const bool inputFloat = mIntf->getPcmEncodingInfo() == C2Config::PCM_FLOAT;
375     const int bitsPerSample = inputFloat ? 24 : 16;
376     FLAC__bool ok = true;
377     ok = ok && FLAC__stream_encoder_set_channels(mFlacStreamEncoder, mIntf->getChannelCount());
378     ok = ok && FLAC__stream_encoder_set_sample_rate(mFlacStreamEncoder, mIntf->getSampleRate());
379     ok = ok && FLAC__stream_encoder_set_bits_per_sample(mFlacStreamEncoder, bitsPerSample);
380     ok = ok && FLAC__stream_encoder_set_compression_level(mFlacStreamEncoder,
381                     mIntf->getComplexity());
382     ok = ok && FLAC__stream_encoder_set_verify(mFlacStreamEncoder, false);
383     if (!ok) {
384         ALOGE("unknown error when configuring encoder");
385         return UNKNOWN_ERROR;
386     }
387 
388     ok &= FLAC__STREAM_ENCODER_INIT_STATUS_OK ==
389             FLAC__stream_encoder_init_stream(mFlacStreamEncoder,
390                     flacEncoderWriteCallback    /*write_callback*/,
391                     nullptr /*seek_callback*/,
392                     nullptr /*tell_callback*/,
393                     nullptr /*metadata_callback*/,
394                     (void *) this /*client_data*/);
395 
396     if (!ok) {
397         ALOGE("unknown error when configuring encoder");
398         return UNKNOWN_ERROR;
399     }
400 
401     mBlockSize = FLAC__stream_encoder_get_blocksize(mFlacStreamEncoder);
402 
403     ALOGV("encoder successfully configured");
404     return OK;
405 }
406 
flacEncoderWriteCallback(const FLAC__StreamEncoder *,const FLAC__byte buffer[],size_t bytes,unsigned samples,unsigned current_frame,void * client_data)407 FLAC__StreamEncoderWriteStatus C2SoftFlacEnc::flacEncoderWriteCallback(
408             const FLAC__StreamEncoder *,
409             const FLAC__byte buffer[],
410             size_t bytes,
411             unsigned samples,
412             unsigned current_frame,
413             void *client_data) {
414     return ((C2SoftFlacEnc*) client_data)->onEncodedFlacAvailable(
415             buffer, bytes, samples, current_frame);
416 }
417 
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)418 c2_status_t C2SoftFlacEnc::drain(
419         uint32_t drainMode,
420         const std::shared_ptr<C2BlockPool> &pool) {
421     (void) pool;
422     switch (drainMode) {
423         case NO_DRAIN:
424             ALOGW("drain with NO_DRAIN: no-op");
425             return C2_OK;
426         case DRAIN_CHAIN:
427             ALOGW("DRAIN_CHAIN not supported");
428             return C2_OMITTED;
429         case DRAIN_COMPONENT_WITH_EOS:
430             // TODO: This flag is not being sent back to the client
431             // because there are no items in PendingWork queue as all the
432             // inputs are being sent back with emptywork or valid encoded data
433             // mSignalledOutputEos = true;
434         case DRAIN_COMPONENT_NO_EOS:
435             break;
436         default:
437             return C2_BAD_VALUE;
438     }
439     FLAC__bool ok = FLAC__stream_encoder_finish(mFlacStreamEncoder);
440     if (!ok) return C2_CORRUPTED;
441     mIsFirstFrame = true;
442     mAnchorTimeStamp = 0ull;
443     mProcessedSamples = 0u;
444 
445     return C2_OK;
446 }
447 
448 class C2SoftFlacEncFactory : public C2ComponentFactory {
449 public:
C2SoftFlacEncFactory()450     C2SoftFlacEncFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
451             GetCodec2PlatformComponentStore()->getParamReflector())) {
452     }
453 
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)454     virtual c2_status_t createComponent(
455             c2_node_id_t id,
456             std::shared_ptr<C2Component>* const component,
457             std::function<void(C2Component*)> deleter) override {
458         *component = std::shared_ptr<C2Component>(
459                 new C2SoftFlacEnc(COMPONENT_NAME,
460                                   id,
461                                   std::make_shared<C2SoftFlacEnc::IntfImpl>(mHelper)),
462                 deleter);
463         return C2_OK;
464     }
465 
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)466     virtual c2_status_t createInterface(
467             c2_node_id_t id,
468             std::shared_ptr<C2ComponentInterface>* const interface,
469             std::function<void(C2ComponentInterface*)> deleter) override {
470         *interface = std::shared_ptr<C2ComponentInterface>(
471                 new SimpleInterface<C2SoftFlacEnc::IntfImpl>(
472                         COMPONENT_NAME, id, std::make_shared<C2SoftFlacEnc::IntfImpl>(mHelper)),
473                 deleter);
474         return C2_OK;
475     }
476 
477     virtual ~C2SoftFlacEncFactory() override = default;
478 private:
479     std::shared_ptr<C2ReflectorHelper> mHelper;
480 };
481 
482 }  // namespace android
483 
CreateCodec2Factory()484 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
485     ALOGV("in %s", __func__);
486     return new ::android::C2SoftFlacEncFactory();
487 }
488 
DestroyCodec2Factory(::C2ComponentFactory * factory)489 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
490     ALOGV("in %s", __func__);
491     delete factory;
492 }
493