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 
fillEmptyWork(const std::unique_ptr<C2Work> & work)324 static void fillEmptyWork(const std::unique_ptr<C2Work> &work) {
325     work->worklets.front()->output.flags = work->input.flags;
326     work->worklets.front()->output.buffers.clear();
327     work->worklets.front()->output.ordinal = work->input.ordinal;
328     work->workletsProcessed = 1u;
329 }
330 
331 // TODO: Can overall error checking be improved? As in the check for validity of
332 //       work, pool ptr, work->input.buffers.size() == 1, ...
333 // TODO: Blind removal of 529 samples from the output may not work. Because
334 //       mpeg layer 1 frame size is 384 samples per frame. This should introduce
335 //       negative values and can cause SEG faults. Soft omx mp3 plugin can have
336 //       this problem (CHECK!)
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)337 void C2SoftMP3::process(
338         const std::unique_ptr<C2Work> &work,
339         const std::shared_ptr<C2BlockPool> &pool) {
340     // Initialize output work
341     work->result = C2_OK;
342     work->workletsProcessed = 1u;
343     work->worklets.front()->output.configUpdate.clear();
344     work->worklets.front()->output.flags = work->input.flags;
345 
346     if (mSignalledError || mSignalledOutputEos) {
347         work->result = C2_BAD_VALUE;
348         return;
349     }
350 
351     bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
352     size_t inSize = 0u;
353     C2ReadView rView = mDummyReadView;
354     if (!work->input.buffers.empty()) {
355         rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
356         inSize = rView.capacity();
357         if (inSize && rView.error()) {
358             ALOGE("read view map failed %d", rView.error());
359             work->result = rView.error();
360             return;
361         }
362     }
363 
364     if (inSize == 0 && (!mGaplessBytes || !eos)) {
365         work->worklets.front()->output.flags = work->input.flags;
366         work->worklets.front()->output.buffers.clear();
367         work->worklets.front()->output.ordinal = work->input.ordinal;
368         return;
369     }
370     ALOGV("in buffer attr. size %zu timestamp %d frameindex %d", inSize,
371           (int)work->input.ordinal.timestamp.peeku(), (int)work->input.ordinal.frameIndex.peeku());
372 
373     int32_t numChannels = mConfig->num_channels;
374     size_t calOutSize;
375     std::vector<size_t> decodedSizes;
376     if (inSize && OK != calculateOutSize(const_cast<uint8 *>(rView.data()),
377                                          inSize, &decodedSizes)) {
378         work->result = C2_CORRUPTED;
379         return;
380     }
381     calOutSize = std::accumulate(decodedSizes.begin(), decodedSizes.end(), 0);
382     if (eos) {
383         calOutSize += kPVMP3DecoderDelay * numChannels * sizeof(int16_t);
384     }
385 
386     std::shared_ptr<C2LinearBlock> block;
387     C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
388     c2_status_t err = pool->fetchLinearBlock(calOutSize, usage, &block);
389     if (err != C2_OK) {
390         ALOGE("fetchLinearBlock for Output failed with status %d", err);
391         work->result = C2_NO_MEMORY;
392         return;
393     }
394     C2WriteView wView = block->map().get();
395     if (wView.error()) {
396         ALOGE("write view map failed %d", wView.error());
397         work->result = wView.error();
398         return;
399     }
400 
401     int outSize = 0;
402     int outOffset = 0;
403     auto it = decodedSizes.begin();
404     size_t inPos = 0;
405     int32_t samplingRate = mConfig->samplingRate;
406     while (inPos < inSize) {
407         if (it == decodedSizes.end()) {
408             ALOGE("unexpected trailing bytes, ignoring them");
409             break;
410         }
411 
412         mConfig->pInputBuffer = const_cast<uint8 *>(rView.data() + inPos);
413         mConfig->inputBufferCurrentLength = (inSize - inPos);
414         mConfig->inputBufferMaxLength = 0;
415         mConfig->inputBufferUsedLength = 0;
416         mConfig->outputFrameSize = (calOutSize - outSize) / sizeof(int16_t);
417         mConfig->pOutputBuffer = reinterpret_cast<int16_t *> (wView.data() + outSize);
418 
419         ERROR_CODE decoderErr;
420         if ((decoderErr = pvmp3_framedecoder(mConfig, mDecoderBuf))
421                 != NO_DECODING_ERROR) {
422             ALOGE("mp3 decoder returned error %d", decoderErr);
423             if (decoderErr != NO_ENOUGH_MAIN_DATA_ERROR
424                     && decoderErr != SIDE_INFO_ERROR) {
425                 mSignalledError = true;
426                 work->result = C2_CORRUPTED;
427                 return;
428             }
429 
430             // This is recoverable, just ignore the current frame and
431             // play silence instead.
432             ALOGV("ignoring error and sending silence");
433             if (mConfig->outputFrameSize == 0) {
434                 mConfig->outputFrameSize = *it / sizeof(int16_t);
435             }
436             memset(mConfig->pOutputBuffer, 0, mConfig->outputFrameSize * sizeof(int16_t));
437         } else if (mConfig->samplingRate != samplingRate
438                 || mConfig->num_channels != numChannels) {
439             ALOGI("Reconfiguring decoder: %d->%d Hz, %d->%d channels",
440                    samplingRate, mConfig->samplingRate,
441                    numChannels, mConfig->num_channels);
442             samplingRate = mConfig->samplingRate;
443             numChannels = mConfig->num_channels;
444 
445             C2StreamSampleRateInfo::output sampleRateInfo(0u, samplingRate);
446             C2StreamChannelCountInfo::output channelCountInfo(0u, numChannels);
447             std::vector<std::unique_ptr<C2SettingResult>> failures;
448             c2_status_t err = mIntf->config(
449                     { &sampleRateInfo, &channelCountInfo },
450                     C2_MAY_BLOCK,
451                     &failures);
452             if (err == OK) {
453                 work->worklets.front()->output.configUpdate.push_back(C2Param::Copy(sampleRateInfo));
454                 work->worklets.front()->output.configUpdate.push_back(C2Param::Copy(channelCountInfo));
455             } else {
456                 ALOGE("Config Update failed");
457                 mSignalledError = true;
458                 work->result = C2_CORRUPTED;
459                 return;
460             }
461         }
462         if (*it != mConfig->outputFrameSize * sizeof(int16_t)) {
463             ALOGE("panic, parsed size does not match decoded size");
464             mSignalledError = true;
465             work->result = C2_CORRUPTED;
466             return;
467         }
468         outSize += mConfig->outputFrameSize * sizeof(int16_t);
469         inPos += mConfig->inputBufferUsedLength;
470         it++;
471     }
472     if (mIsFirst) {
473         mIsFirst = false;
474         mGaplessBytes = true;
475         // The decoder delay is 529 samples, so trim that many samples off
476         // the start of the first output buffer. This essentially makes this
477         // decoder have zero delay, which the rest of the pipeline assumes.
478         outOffset = kPVMP3DecoderDelay * numChannels * sizeof(int16_t);
479         mAnchorTimeStamp = work->input.ordinal.timestamp.peekull();
480     }
481     if (eos) {
482         if (calOutSize >=
483             outSize + kPVMP3DecoderDelay * numChannels * sizeof(int16_t)) {
484             if (!memset(reinterpret_cast<int16_t*>(wView.data() + outSize), 0,
485                         kPVMP3DecoderDelay * numChannels * sizeof(int16_t))) {
486                 mSignalledError = true;
487                 work->result = C2_CORRUPTED;
488                 return;
489              }
490             ALOGV("Adding 529 samples at end");
491             mGaplessBytes = false;
492             outSize += kPVMP3DecoderDelay * numChannels * sizeof(int16_t);
493         }
494     }
495 
496     fillEmptyWork(work);
497     if (samplingRate && numChannels) {
498         int64_t outTimeStamp = mProcessedSamples * 1000000ll / samplingRate;
499         mProcessedSamples += ((outSize - outOffset) / (numChannels * sizeof(int16_t)));
500         ALOGV("out buffer attr. offset %d size %d timestamp %" PRId64 " ", outOffset,
501                outSize - outOffset, mAnchorTimeStamp + outTimeStamp);
502         decodedSizes.clear();
503         work->worklets.front()->output.buffers.push_back(
504                 createLinearBuffer(block, outOffset, outSize - outOffset));
505         work->worklets.front()->output.ordinal.timestamp = mAnchorTimeStamp + outTimeStamp;
506     }
507     if (eos) {
508         mSignalledOutputEos = true;
509         ALOGV("signalled EOS");
510     }
511 }
512 
513 class C2SoftMp3DecFactory : public C2ComponentFactory {
514 public:
C2SoftMp3DecFactory()515     C2SoftMp3DecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
516             GetCodec2PlatformComponentStore()->getParamReflector())) {
517     }
518 
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)519     virtual c2_status_t createComponent(
520             c2_node_id_t id,
521             std::shared_ptr<C2Component>* const component,
522             std::function<void(C2Component*)> deleter) override {
523         *component = std::shared_ptr<C2Component>(
524               new C2SoftMP3(COMPONENT_NAME,
525                             id,
526                             std::make_shared<C2SoftMP3::IntfImpl>(mHelper)),
527               deleter);
528         return C2_OK;
529     }
530 
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)531     virtual c2_status_t createInterface(
532             c2_node_id_t id,
533             std::shared_ptr<C2ComponentInterface>* const interface,
534             std::function<void(C2ComponentInterface*)> deleter) override {
535         *interface = std::shared_ptr<C2ComponentInterface>(
536               new SimpleInterface<C2SoftMP3::IntfImpl>(
537                       COMPONENT_NAME, id, std::make_shared<C2SoftMP3::IntfImpl>(mHelper)),
538               deleter);
539         return C2_OK;
540     }
541 
542     virtual ~C2SoftMp3DecFactory() override = default;
543 
544 private:
545     std::shared_ptr<C2ReflectorHelper> mHelper;
546 };
547 
548 }  // namespace android
549 
550 __attribute__((cfi_canonical_jump_table))
CreateCodec2Factory()551 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
552     ALOGV("in %s", __func__);
553     return new ::android::C2SoftMp3DecFactory();
554 }
555 
556 __attribute__((cfi_canonical_jump_table))
DestroyCodec2Factory(::C2ComponentFactory * factory)557 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
558     ALOGV("in %s", __func__);
559     delete factory;
560 }
561