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 "C2SoftMp3Dec"
19 #include <inttypes.h>
20 #include <log/log.h>
21 
22 #include <numeric>
23 
24 #include <media/stagefright/foundation/MediaDefs.h>
25 
26 #include <C2PlatformSupport.h>
27 #include <SimpleC2Interface.h>
28 
29 #include "C2SoftMp3Dec.h"
30 #include "pvmp3decoder_api.h"
31 
32 namespace android {
33 
34 namespace {
35 
36 constexpr char COMPONENT_NAME[] = "c2.android.mp3.decoder";
37 
38 }  // namespace
39 
40 class C2SoftMP3::IntfImpl : public SimpleInterface<void>::BaseParams {
41 public:
IntfImpl(const std::shared_ptr<C2ReflectorHelper> & helper)42     explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
43         : SimpleInterface<void>::BaseParams(
44                 helper,
45                 COMPONENT_NAME,
46                 C2Component::KIND_DECODER,
47                 C2Component::DOMAIN_AUDIO,
48                 MEDIA_MIMETYPE_AUDIO_MPEG) {
49         noPrivateBuffers();
50         noInputReferences();
51         noOutputReferences();
52         noInputLatency();
53         noTimeStretch();
54         setDerivedInstance(this);
55 
56         addParameter(
57                 DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
58                 .withConstValue(new C2ComponentAttributesSetting(
59                     C2Component::ATTRIB_IS_TEMPORAL))
60                 .build());
61 
62         addParameter(
63                 DefineParam(mSampleRate, C2_PARAMKEY_SAMPLE_RATE)
64                 .withDefault(new C2StreamSampleRateInfo::output(0u, 44100))
65                 .withFields({C2F(mSampleRate, value).oneOf({8000, 11025, 12000, 16000,
66                     22050, 24000, 32000, 44100, 48000})})
67                 .withSetter((Setter<decltype(*mSampleRate)>::StrictValueWithNoDeps))
68                 .build());
69 
70         addParameter(
71                 DefineParam(mChannelCount, C2_PARAMKEY_CHANNEL_COUNT)
72                 .withDefault(new C2StreamChannelCountInfo::output(0u, 2))
73                 .withFields({C2F(mChannelCount, value).inRange(1, 2)})
74                 .withSetter(Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps)
75                 .build());
76 
77         addParameter(
78                 DefineParam(mBitrate, C2_PARAMKEY_BITRATE)
79                 .withDefault(new C2StreamBitrateInfo::input(0u, 64000))
80                 .withFields({C2F(mBitrate, value).inRange(8000, 320000)})
81                 .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps)
82                 .build());
83 
84         addParameter(
85                 DefineParam(mInputMaxBufSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
86                 .withConstValue(new C2StreamMaxBufferSizeInfo::input(0u, 8192))
87                 .build());
88     }
89 
90 private:
91     std::shared_ptr<C2StreamSampleRateInfo::output> mSampleRate;
92     std::shared_ptr<C2StreamChannelCountInfo::output> mChannelCount;
93     std::shared_ptr<C2StreamBitrateInfo::input> mBitrate;
94     std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
95 };
96 
C2SoftMP3(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)97 C2SoftMP3::C2SoftMP3(const char *name, c2_node_id_t id,
98                      const std::shared_ptr<IntfImpl> &intfImpl)
99     : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
100       mIntf(intfImpl),
101       mConfig(nullptr),
102       mDecoderBuf(nullptr) {
103 }
104 
~C2SoftMP3()105 C2SoftMP3::~C2SoftMP3() {
106     onRelease();
107 }
108 
onInit()109 c2_status_t C2SoftMP3::onInit() {
110     status_t err = initDecoder();
111     return err == OK ? C2_OK : C2_NO_MEMORY;
112 }
113 
onStop()114 c2_status_t C2SoftMP3::onStop() {
115     // Make sure that the next buffer output does not still
116     // depend on fragments from the last one decoded.
117     pvmp3_InitDecoder(mConfig, mDecoderBuf);
118     mSignalledError = false;
119     mIsFirst = true;
120     mSignalledOutputEos = false;
121     mAnchorTimeStamp = 0;
122     mProcessedSamples = 0;
123 
124     return C2_OK;
125 }
126 
onReset()127 void C2SoftMP3::onReset() {
128     (void)onStop();
129 }
130 
onRelease()131 void C2SoftMP3::onRelease() {
132     mGaplessBytes = false;
133     if (mDecoderBuf) {
134         free(mDecoderBuf);
135         mDecoderBuf = nullptr;
136     }
137 
138     if (mConfig) {
139         delete mConfig;
140         mConfig = nullptr;
141     }
142 }
143 
initDecoder()144 status_t C2SoftMP3::initDecoder() {
145     mConfig = new tPVMP3DecoderExternal{};
146     if (!mConfig) return NO_MEMORY;
147     mConfig->equalizerType = flat;
148     mConfig->crcEnabled = false;
149 
150     size_t memRequirements = pvmp3_decoderMemRequirements();
151     mDecoderBuf = malloc(memRequirements);
152     if (!mDecoderBuf) return NO_MEMORY;
153 
154     pvmp3_InitDecoder(mConfig, mDecoderBuf);
155 
156     mIsFirst = true;
157     mGaplessBytes = false;
158     mSignalledError = false;
159     mSignalledOutputEos = false;
160     mAnchorTimeStamp = 0;
161     mProcessedSamples = 0;
162 
163     return OK;
164 }
165 
166 /* The below code is borrowed from ./test/mp3reader.cpp */
parseMp3Header(uint32_t header,size_t * frame_size,uint32_t * out_sampling_rate=nullptr,uint32_t * out_channels=nullptr,uint32_t * out_bitrate=nullptr,uint32_t * out_num_samples=nullptr)167 static bool parseMp3Header(uint32_t header, size_t *frame_size,
168                            uint32_t *out_sampling_rate = nullptr,
169                            uint32_t *out_channels = nullptr,
170                            uint32_t *out_bitrate = nullptr,
171                            uint32_t *out_num_samples = nullptr) {
172     *frame_size = 0;
173     if (out_sampling_rate) *out_sampling_rate = 0;
174     if (out_channels) *out_channels = 0;
175     if (out_bitrate) *out_bitrate = 0;
176     if (out_num_samples) *out_num_samples = 1152;
177 
178     if ((header & 0xffe00000) != 0xffe00000) return false;
179 
180     unsigned version = (header >> 19) & 3;
181     if (version == 0x01) return false;
182 
183     unsigned layer = (header >> 17) & 3;
184     if (layer == 0x00) return false;
185 
186     unsigned bitrate_index = (header >> 12) & 0x0f;
187     if (bitrate_index == 0 || bitrate_index == 0x0f) return false;
188 
189     unsigned sampling_rate_index = (header >> 10) & 3;
190     if (sampling_rate_index == 3) return false;
191 
192     static const int kSamplingRateV1[] = { 44100, 48000, 32000 };
193     int sampling_rate = kSamplingRateV1[sampling_rate_index];
194     if (version == 2 /* V2 */) {
195         sampling_rate /= 2;
196     } else if (version == 0 /* V2.5 */) {
197         sampling_rate /= 4;
198     }
199 
200     unsigned padding = (header >> 9) & 1;
201 
202     if (layer == 3) { // layer I
203         static const int kBitrateV1[] =
204         {
205             32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448
206         };
207         static const int kBitrateV2[] =
208         {
209             32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256
210         };
211 
212         int bitrate = (version == 3 /* V1 */) ? kBitrateV1[bitrate_index - 1] :
213                 kBitrateV2[bitrate_index - 1];
214 
215         if (out_bitrate) {
216             *out_bitrate = bitrate;
217         }
218         *frame_size = (12000 * bitrate / sampling_rate + padding) * 4;
219         if (out_num_samples) {
220             *out_num_samples = 384;
221         }
222     } else { // layer II or III
223         static const int kBitrateV1L2[] =
224         {
225             32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384
226         };
227 
228         static const int kBitrateV1L3[] =
229         {
230             32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320
231         };
232 
233         static const int kBitrateV2[] =
234         {
235             8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160
236         };
237 
238         int bitrate;
239         if (version == 3 /* V1 */) {
240             bitrate = (layer == 2 /* L2 */) ? kBitrateV1L2[bitrate_index - 1] :
241                     kBitrateV1L3[bitrate_index - 1];
242 
243             if (out_num_samples) {
244                 *out_num_samples = 1152;
245             }
246         } else { // V2 (or 2.5)
247             bitrate = kBitrateV2[bitrate_index - 1];
248             if (out_num_samples) {
249                 *out_num_samples = (layer == 1 /* L3 */) ? 576 : 1152;
250             }
251         }
252 
253         if (out_bitrate) {
254             *out_bitrate = bitrate;
255         }
256 
257         if (version == 3 /* V1 */) {
258             *frame_size = 144000 * bitrate / sampling_rate + padding;
259         } else { // V2 or V2.5
260             size_t tmp = (layer == 1 /* L3 */) ? 72000 : 144000;
261             *frame_size = tmp * bitrate / sampling_rate + padding;
262         }
263     }
264 
265     if (out_sampling_rate) {
266         *out_sampling_rate = sampling_rate;
267     }
268 
269     if (out_channels) {
270         int channel_mode = (header >> 6) & 3;
271 
272         *out_channels = (channel_mode == 3) ? 1 : 2;
273     }
274 
275     return true;
276 }
277 
U32_AT(const uint8_t * ptr)278 static uint32_t U32_AT(const uint8_t *ptr) {
279     return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
280 }
281 
calculateOutSize(uint8 * header,size_t inSize,std::vector<size_t> * decodedSizes)282 static status_t calculateOutSize(uint8 *header, size_t inSize,
283                                  std::vector<size_t> *decodedSizes) {
284     uint32_t channels;
285     uint32_t numSamples;
286     size_t frameSize;
287     size_t totalInSize = 0;
288 
289     while (totalInSize + 4 < inSize) {
290         if (!parseMp3Header(U32_AT(header + totalInSize), &frameSize,
291                             nullptr, &channels, nullptr, &numSamples)) {
292             ALOGE("Error in parse mp3 header during outSize estimation");
293             return UNKNOWN_ERROR;
294         }
295         totalInSize += frameSize;
296         decodedSizes->push_back(numSamples * channels * sizeof(int16_t));
297     }
298 
299     if (decodedSizes->empty()) return UNKNOWN_ERROR;
300 
301     return OK;
302 }
303 
onFlush_sm()304 c2_status_t C2SoftMP3::onFlush_sm() {
305     return onStop();
306 }
307 
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)308 c2_status_t C2SoftMP3::drain(
309         uint32_t drainMode,
310         const std::shared_ptr<C2BlockPool> &pool) {
311     (void) pool;
312     if (drainMode == NO_DRAIN) {
313         ALOGW("drain with NO_DRAIN: no-op");
314         return C2_OK;
315     }
316     if (drainMode == DRAIN_CHAIN) {
317         ALOGW("DRAIN_CHAIN not supported");
318         return C2_OMITTED;
319     }
320 
321     return C2_OK;
322 }
323 
324 // TODO: Can overall error checking be improved? As in the check for validity of
325 //       work, pool ptr, work->input.buffers.size() == 1, ...
326 // TODO: Blind removal of 529 samples from the output may not work. Because
327 //       mpeg layer 1 frame size is 384 samples per frame. This should introduce
328 //       negative values and can cause SEG faults. Soft omx mp3 plugin can have
329 //       this problem (CHECK!)
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)330 void C2SoftMP3::process(
331         const std::unique_ptr<C2Work> &work,
332         const std::shared_ptr<C2BlockPool> &pool) {
333     // Initialize output work
334     work->result = C2_OK;
335     work->workletsProcessed = 1u;
336     work->worklets.front()->output.configUpdate.clear();
337     work->worklets.front()->output.flags = work->input.flags;
338 
339     if (mSignalledError || mSignalledOutputEos) {
340         work->result = C2_BAD_VALUE;
341         return;
342     }
343 
344     bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
345     size_t inSize = 0u;
346     C2ReadView rView = mDummyReadView;
347     if (!work->input.buffers.empty()) {
348         rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
349         inSize = rView.capacity();
350         if (inSize && rView.error()) {
351             ALOGE("read view map failed %d", rView.error());
352             work->result = rView.error();
353             return;
354         }
355     }
356 
357     if (inSize == 0 && (!mGaplessBytes || !eos)) {
358         work->worklets.front()->output.flags = work->input.flags;
359         work->worklets.front()->output.buffers.clear();
360         work->worklets.front()->output.ordinal = work->input.ordinal;
361         return;
362     }
363     ALOGV("in buffer attr. size %zu timestamp %d frameindex %d", inSize,
364           (int)work->input.ordinal.timestamp.peeku(), (int)work->input.ordinal.frameIndex.peeku());
365 
366     int32_t numChannels = mConfig->num_channels;
367     size_t calOutSize;
368     std::vector<size_t> decodedSizes;
369     if (inSize && OK != calculateOutSize(const_cast<uint8 *>(rView.data()),
370                                          inSize, &decodedSizes)) {
371         work->result = C2_CORRUPTED;
372         return;
373     }
374     calOutSize = std::accumulate(decodedSizes.begin(), decodedSizes.end(), 0);
375     if (eos) {
376         calOutSize += kPVMP3DecoderDelay * numChannels * sizeof(int16_t);
377     }
378 
379     std::shared_ptr<C2LinearBlock> block;
380     C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
381     c2_status_t err = pool->fetchLinearBlock(calOutSize, usage, &block);
382     if (err != C2_OK) {
383         ALOGE("fetchLinearBlock for Output failed with status %d", err);
384         work->result = C2_NO_MEMORY;
385         return;
386     }
387     C2WriteView wView = block->map().get();
388     if (wView.error()) {
389         ALOGE("write view map failed %d", wView.error());
390         work->result = wView.error();
391         return;
392     }
393 
394     int outSize = 0;
395     int outOffset = 0;
396     auto it = decodedSizes.begin();
397     size_t inPos = 0;
398     int32_t samplingRate = mConfig->samplingRate;
399     while (inPos < inSize) {
400         if (it == decodedSizes.end()) {
401             ALOGE("unexpected trailing bytes, ignoring them");
402             break;
403         }
404 
405         mConfig->pInputBuffer = const_cast<uint8 *>(rView.data() + inPos);
406         mConfig->inputBufferCurrentLength = (inSize - inPos);
407         mConfig->inputBufferMaxLength = 0;
408         mConfig->inputBufferUsedLength = 0;
409         mConfig->outputFrameSize = (calOutSize - outSize) / sizeof(int16_t);
410         mConfig->pOutputBuffer = reinterpret_cast<int16_t *> (wView.data() + outSize);
411 
412         ERROR_CODE decoderErr;
413         if ((decoderErr = pvmp3_framedecoder(mConfig, mDecoderBuf))
414                 != NO_DECODING_ERROR) {
415             ALOGE("mp3 decoder returned error %d", decoderErr);
416             if (decoderErr != NO_ENOUGH_MAIN_DATA_ERROR
417                     && decoderErr != SIDE_INFO_ERROR) {
418                 mSignalledError = true;
419                 work->result = C2_CORRUPTED;
420                 return;
421             }
422 
423             // This is recoverable, just ignore the current frame and
424             // play silence instead.
425             ALOGV("ignoring error and sending silence");
426             if (mConfig->outputFrameSize == 0) {
427                 mConfig->outputFrameSize = *it / sizeof(int16_t);
428             }
429             memset(mConfig->pOutputBuffer, 0, mConfig->outputFrameSize * sizeof(int16_t));
430         } else if (mConfig->samplingRate != samplingRate
431                 || mConfig->num_channels != numChannels) {
432             ALOGI("Reconfiguring decoder: %d->%d Hz, %d->%d channels",
433                    samplingRate, mConfig->samplingRate,
434                    numChannels, mConfig->num_channels);
435             samplingRate = mConfig->samplingRate;
436             numChannels = mConfig->num_channels;
437 
438             C2StreamSampleRateInfo::output sampleRateInfo(0u, samplingRate);
439             C2StreamChannelCountInfo::output channelCountInfo(0u, numChannels);
440             std::vector<std::unique_ptr<C2SettingResult>> failures;
441             c2_status_t err = mIntf->config(
442                     { &sampleRateInfo, &channelCountInfo },
443                     C2_MAY_BLOCK,
444                     &failures);
445             if (err == OK) {
446                 work->worklets.front()->output.configUpdate.push_back(C2Param::Copy(sampleRateInfo));
447                 work->worklets.front()->output.configUpdate.push_back(C2Param::Copy(channelCountInfo));
448             } else {
449                 ALOGE("Config Update failed");
450                 mSignalledError = true;
451                 work->result = C2_CORRUPTED;
452                 return;
453             }
454         }
455         if (*it != mConfig->outputFrameSize * sizeof(int16_t)) {
456             ALOGE("panic, parsed size does not match decoded size");
457             mSignalledError = true;
458             work->result = C2_CORRUPTED;
459             return;
460         }
461         outSize += mConfig->outputFrameSize * sizeof(int16_t);
462         inPos += mConfig->inputBufferUsedLength;
463         it++;
464     }
465     if (mIsFirst) {
466         mIsFirst = false;
467         mGaplessBytes = true;
468         // The decoder delay is 529 samples, so trim that many samples off
469         // the start of the first output buffer. This essentially makes this
470         // decoder have zero delay, which the rest of the pipeline assumes.
471         outOffset = kPVMP3DecoderDelay * numChannels * sizeof(int16_t);
472         mAnchorTimeStamp = work->input.ordinal.timestamp.peekull();
473     }
474     if (eos) {
475         if (calOutSize >=
476             outSize + kPVMP3DecoderDelay * numChannels * sizeof(int16_t)) {
477             if (!memset(reinterpret_cast<int16_t*>(wView.data() + outSize), 0,
478                         kPVMP3DecoderDelay * numChannels * sizeof(int16_t))) {
479                 mSignalledError = true;
480                 work->result = C2_CORRUPTED;
481                 return;
482              }
483             ALOGV("Adding 529 samples at end");
484             mGaplessBytes = false;
485             outSize += kPVMP3DecoderDelay * numChannels * sizeof(int16_t);
486         }
487     }
488 
489     int64_t outTimeStamp = mProcessedSamples * 1000000ll / samplingRate;
490     mProcessedSamples += ((outSize - outOffset) / (numChannels * sizeof(int16_t)));
491     ALOGV("out buffer attr. offset %d size %d timestamp %" PRId64 " ", outOffset,
492           outSize - outOffset, mAnchorTimeStamp + outTimeStamp);
493     decodedSizes.clear();
494     work->worklets.front()->output.flags = work->input.flags;
495     work->worklets.front()->output.buffers.clear();
496     work->worklets.front()->output.buffers.push_back(
497             createLinearBuffer(block, outOffset, outSize - outOffset));
498     work->worklets.front()->output.ordinal = work->input.ordinal;
499     work->worklets.front()->output.ordinal.timestamp = mAnchorTimeStamp + outTimeStamp;
500     if (eos) {
501         mSignalledOutputEos = true;
502         ALOGV("signalled EOS");
503     }
504 }
505 
506 class C2SoftMp3DecFactory : public C2ComponentFactory {
507 public:
C2SoftMp3DecFactory()508     C2SoftMp3DecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
509             GetCodec2PlatformComponentStore()->getParamReflector())) {
510     }
511 
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)512     virtual c2_status_t createComponent(
513             c2_node_id_t id,
514             std::shared_ptr<C2Component>* const component,
515             std::function<void(C2Component*)> deleter) override {
516         *component = std::shared_ptr<C2Component>(
517               new C2SoftMP3(COMPONENT_NAME,
518                             id,
519                             std::make_shared<C2SoftMP3::IntfImpl>(mHelper)),
520               deleter);
521         return C2_OK;
522     }
523 
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)524     virtual c2_status_t createInterface(
525             c2_node_id_t id,
526             std::shared_ptr<C2ComponentInterface>* const interface,
527             std::function<void(C2ComponentInterface*)> deleter) override {
528         *interface = std::shared_ptr<C2ComponentInterface>(
529               new SimpleInterface<C2SoftMP3::IntfImpl>(
530                       COMPONENT_NAME, id, std::make_shared<C2SoftMP3::IntfImpl>(mHelper)),
531               deleter);
532         return C2_OK;
533     }
534 
535     virtual ~C2SoftMp3DecFactory() override = default;
536 
537 private:
538     std::shared_ptr<C2ReflectorHelper> mHelper;
539 };
540 
541 }  // namespace android
542 
543 __attribute__((cfi_canonical_jump_table))
CreateCodec2Factory()544 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
545     ALOGV("in %s", __func__);
546     return new ::android::C2SoftMp3DecFactory();
547 }
548 
549 __attribute__((cfi_canonical_jump_table))
DestroyCodec2Factory(::C2ComponentFactory * factory)550 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
551     ALOGV("in %s", __func__);
552     delete factory;
553 }
554