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 "C2SoftVorbisDec"
19 #include <log/log.h>
20 
21 #include <media/stagefright/foundation/MediaDefs.h>
22 
23 #include <C2PlatformSupport.h>
24 #include <SimpleC2Interface.h>
25 
26 #include "C2SoftVorbisDec.h"
27 
28 extern "C" {
29     #include <Tremolo/codec_internal.h>
30 
31     int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb);
32     int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb);
33     int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb);
34 }
35 
36 namespace android {
37 
38 constexpr char COMPONENT_NAME[] = "c2.android.vorbis.decoder";
39 
40 class C2SoftVorbisDec::IntfImpl : public C2InterfaceHelper {
41 public:
IntfImpl(const std::shared_ptr<C2ReflectorHelper> & helper)42     explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
43         : C2InterfaceHelper(helper) {
44 
45         setDerivedInstance(this);
46 
47         addParameter(
48                 DefineParam(mInputFormat, C2_NAME_INPUT_STREAM_FORMAT_SETTING)
49                 .withConstValue(new C2StreamFormatConfig::input(0u, C2FormatCompressed))
50                 .build());
51 
52         addParameter(
53                 DefineParam(mOutputFormat, C2_NAME_OUTPUT_STREAM_FORMAT_SETTING)
54                 .withConstValue(new C2StreamFormatConfig::output(0u, C2FormatAudio))
55                 .build());
56 
57         addParameter(
58                 DefineParam(mInputMediaType, C2_NAME_INPUT_PORT_MIME_SETTING)
59                 .withConstValue(AllocSharedString<C2PortMimeConfig::input>(
60                         MEDIA_MIMETYPE_AUDIO_VORBIS))
61                 .build());
62 
63         addParameter(
64                 DefineParam(mOutputMediaType, C2_NAME_OUTPUT_PORT_MIME_SETTING)
65                 .withConstValue(AllocSharedString<C2PortMimeConfig::output>(
66                         MEDIA_MIMETYPE_AUDIO_RAW))
67                 .build());
68 
69         addParameter(
70                 DefineParam(mSampleRate, C2_NAME_STREAM_SAMPLE_RATE_SETTING)
71                 .withDefault(new C2StreamSampleRateInfo::output(0u, 48000))
72                 .withFields({C2F(mSampleRate, value).inRange(8000, 96000)})
73                 .withSetter((Setter<decltype(*mSampleRate)>::StrictValueWithNoDeps))
74                 .build());
75 
76         addParameter(
77                 DefineParam(mChannelCount, C2_NAME_STREAM_CHANNEL_COUNT_SETTING)
78                 .withDefault(new C2StreamChannelCountInfo::output(0u, 1))
79                 .withFields({C2F(mChannelCount, value).inRange(1, 8)})
80                 .withSetter(Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps)
81                 .build());
82 
83         addParameter(
84                 DefineParam(mBitrate, C2_NAME_STREAM_BITRATE_SETTING)
85                 .withDefault(new C2BitrateTuning::input(0u, 64000))
86                 .withFields({C2F(mBitrate, value).inRange(32000, 500000)})
87                 .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps)
88                 .build());
89 
90         addParameter(
91                 DefineParam(mInputMaxBufSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
92                 .withConstValue(new C2StreamMaxBufferSizeInfo::input(0u, 8192 * 2 * sizeof(int16_t)))
93                 .build());
94     }
95 
96 private:
97     std::shared_ptr<C2StreamFormatConfig::input> mInputFormat;
98     std::shared_ptr<C2StreamFormatConfig::output> mOutputFormat;
99     std::shared_ptr<C2PortMimeConfig::input> mInputMediaType;
100     std::shared_ptr<C2PortMimeConfig::output> mOutputMediaType;
101     std::shared_ptr<C2StreamSampleRateInfo::output> mSampleRate;
102     std::shared_ptr<C2StreamChannelCountInfo::output> mChannelCount;
103     std::shared_ptr<C2BitrateTuning::input> mBitrate;
104     std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
105 };
106 
C2SoftVorbisDec(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)107 C2SoftVorbisDec::C2SoftVorbisDec(
108         const char *name,
109         c2_node_id_t id,
110         const std::shared_ptr<IntfImpl> &intfImpl)
111     : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
112       mIntf(intfImpl),
113       mState(nullptr),
114       mVi(nullptr) {
115 }
116 
~C2SoftVorbisDec()117 C2SoftVorbisDec::~C2SoftVorbisDec() {
118     onRelease();
119 }
120 
onInit()121 c2_status_t C2SoftVorbisDec::onInit() {
122     status_t err = initDecoder();
123     return err == OK ? C2_OK : C2_NO_MEMORY;
124 }
125 
onStop()126 c2_status_t C2SoftVorbisDec::onStop() {
127     if (mState) {
128         vorbis_dsp_clear(mState);
129         delete mState;
130         mState = nullptr;
131     }
132 
133     if (mVi) {
134         vorbis_info_clear(mVi);
135         delete mVi;
136         mVi = nullptr;
137     }
138     mNumFramesLeftOnPage = -1;
139     mSignalledOutputEos = false;
140     mSignalledError = false;
141 
142     return (initDecoder() == OK ? C2_OK : C2_CORRUPTED);
143 }
144 
onReset()145 void C2SoftVorbisDec::onReset() {
146     (void)onStop();
147 }
148 
onRelease()149 void C2SoftVorbisDec::onRelease() {
150     if (mState) {
151         vorbis_dsp_clear(mState);
152         delete mState;
153         mState = nullptr;
154     }
155 
156     if (mVi) {
157         vorbis_info_clear(mVi);
158         delete mVi;
159         mVi = nullptr;
160     }
161 }
162 
initDecoder()163 status_t C2SoftVorbisDec::initDecoder() {
164     mVi = new vorbis_info{};
165     if (!mVi) return NO_MEMORY;
166     vorbis_info_clear(mVi);
167 
168     mState = new vorbis_dsp_state{};
169     if (!mState) return NO_MEMORY;
170     vorbis_dsp_clear(mState);
171 
172     mNumFramesLeftOnPage = -1;
173     mSignalledError = false;
174     mSignalledOutputEos = false;
175     mInfoUnpacked = false;
176     mBooksUnpacked = false;
177     return OK;
178 }
179 
onFlush_sm()180 c2_status_t C2SoftVorbisDec::onFlush_sm() {
181     mNumFramesLeftOnPage = -1;
182     mSignalledOutputEos = false;
183     if (mState) vorbis_dsp_restart(mState);
184 
185     return C2_OK;
186 }
187 
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)188 c2_status_t C2SoftVorbisDec::drain(
189         uint32_t drainMode,
190         const std::shared_ptr<C2BlockPool> &pool) {
191     (void) pool;
192     if (drainMode == NO_DRAIN) {
193         ALOGW("drain with NO_DRAIN: no-op");
194         return C2_OK;
195     }
196     if (drainMode == DRAIN_CHAIN) {
197         ALOGW("DRAIN_CHAIN not supported");
198         return C2_OMITTED;
199     }
200 
201     return C2_OK;
202 }
203 
fillEmptyWork(const std::unique_ptr<C2Work> & work)204 static void fillEmptyWork(const std::unique_ptr<C2Work> &work) {
205     work->worklets.front()->output.flags = work->input.flags;
206     work->worklets.front()->output.buffers.clear();
207     work->worklets.front()->output.ordinal = work->input.ordinal;
208     work->workletsProcessed = 1u;
209 }
210 
makeBitReader(const void * data,size_t size,ogg_buffer * buf,ogg_reference * ref,oggpack_buffer * bits)211 static void makeBitReader(
212         const void *data, size_t size,
213         ogg_buffer *buf, ogg_reference *ref, oggpack_buffer *bits) {
214     buf->data = (uint8_t *)data;
215     buf->size = size;
216     buf->refcount = 1;
217     buf->ptr.owner = nullptr;
218 
219     ref->buffer = buf;
220     ref->begin = 0;
221     ref->length = size;
222     ref->next = nullptr;
223 
224     oggpack_readinit(bits, ref);
225 }
226 
227 // (CHECK!) multiframe is tricky. decode call doesnt return the number of bytes
228 // consumed by the component. Also it is unclear why numPageFrames is being
229 // tagged at the end of input buffers for new pages. Refer lines 297-300 in
230 // SimpleDecodingSource.cpp
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)231 void C2SoftVorbisDec::process(
232         const std::unique_ptr<C2Work> &work,
233         const std::shared_ptr<C2BlockPool> &pool) {
234     // Initialize output work
235     work->result = C2_OK;
236     work->workletsProcessed = 1u;
237     work->worklets.front()->output.configUpdate.clear();
238     work->worklets.front()->output.flags = work->input.flags;
239 
240     if (mSignalledError || mSignalledOutputEos) {
241         work->result = C2_BAD_VALUE;
242         return;
243     }
244 
245     bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
246     size_t inOffset = 0u;
247     size_t inSize = 0u;
248     C2ReadView rView = mDummyReadView;
249     if (!work->input.buffers.empty()) {
250         rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
251         inSize = rView.capacity();
252         if (inSize && rView.error()) {
253             ALOGE("read view map failed %d", rView.error());
254             work->result = rView.error();
255             return;
256         }
257     }
258 
259     if (inSize == 0) {
260         fillEmptyWork(work);
261         if (eos) {
262             mSignalledOutputEos = true;
263             ALOGV("signalled EOS");
264         }
265         return;
266     }
267 
268     ALOGV("in buffer attr. size %zu timestamp %d frameindex %d", inSize,
269           (int)work->input.ordinal.timestamp.peeku(), (int)work->input.ordinal.frameIndex.peeku());
270     const uint8_t *data = rView.data() + inOffset;
271     int32_t numChannels  = mVi->channels;
272     int32_t samplingRate = mVi->rate;
273     if (inSize > 7 && !memcmp(&data[1], "vorbis", 6)) {
274         if ((data[0] != 1) && (data[0] != 5)) {
275             ALOGE("unexpected type received %d", data[0]);
276             mSignalledError = true;
277             work->result = C2_CORRUPTED;
278             return;
279         }
280 
281         ogg_buffer buf;
282         ogg_reference ref;
283         oggpack_buffer bits;
284 
285         // skip 7 <type + "vorbis"> bytes
286         makeBitReader((const uint8_t *)data + 7, inSize - 7, &buf, &ref, &bits);
287         if (data[0] == 1) {
288             vorbis_info_init(mVi);
289             if (0 != _vorbis_unpack_info(mVi, &bits)) {
290                 ALOGE("Encountered error while unpacking info");
291                 mSignalledError = true;
292                 work->result = C2_CORRUPTED;
293                 return;
294             }
295             if (mVi->rate != samplingRate ||
296                     mVi->channels != numChannels) {
297                 ALOGV("vorbis: rate/channels changed: %ld/%d", mVi->rate, mVi->channels);
298                 samplingRate = mVi->rate;
299                 numChannels = mVi->channels;
300 
301                 C2StreamSampleRateInfo::output sampleRateInfo(0u, samplingRate);
302                 C2StreamChannelCountInfo::output channelCountInfo(0u, numChannels);
303                 std::vector<std::unique_ptr<C2SettingResult>> failures;
304                 c2_status_t err = mIntf->config(
305                         { &sampleRateInfo, &channelCountInfo },
306                         C2_MAY_BLOCK,
307                         &failures);
308                 if (err == OK) {
309                     work->worklets.front()->output.configUpdate.push_back(C2Param::Copy(sampleRateInfo));
310                     work->worklets.front()->output.configUpdate.push_back(C2Param::Copy(channelCountInfo));
311                 } else {
312                     ALOGE("Config Update failed");
313                     mSignalledError = true;
314                     work->result = C2_CORRUPTED;
315                     return;
316                 }
317             }
318             mInfoUnpacked = true;
319         } else {
320             if (!mInfoUnpacked) {
321                 ALOGE("Data with type:5 sent before sending type:1");
322                 mSignalledError = true;
323                 work->result = C2_CORRUPTED;
324                 return;
325             }
326             if (0 != _vorbis_unpack_books(mVi, &bits)) {
327                 ALOGE("Encountered error while unpacking books");
328                 mSignalledError = true;
329                 work->result = C2_CORRUPTED;
330                 return;
331             }
332             if (0 != vorbis_dsp_init(mState, mVi)) {
333                 ALOGE("Encountered error while dsp init");
334                 mSignalledError = true;
335                 work->result = C2_CORRUPTED;
336                 return;
337             }
338             mBooksUnpacked = true;
339         }
340         fillEmptyWork(work);
341         if (eos) {
342             mSignalledOutputEos = true;
343             ALOGV("signalled EOS");
344         }
345         return;
346     }
347 
348     if (!mInfoUnpacked || !mBooksUnpacked) {
349         ALOGE("Missing CODEC_CONFIG data mInfoUnpacked: %d mBooksUnpack %d", mInfoUnpacked, mBooksUnpacked);
350         mSignalledError = true;
351         work->result = C2_CORRUPTED;
352         return;
353     }
354 
355     int32_t numPageFrames = 0;
356     if (inSize < sizeof(numPageFrames)) {
357         ALOGE("input header has size %zu, expected %zu", inSize, sizeof(numPageFrames));
358         mSignalledError = true;
359         work->result = C2_CORRUPTED;
360         return;
361     }
362     memcpy(&numPageFrames, data + inSize - sizeof(numPageFrames), sizeof(numPageFrames));
363     inSize -= sizeof(numPageFrames);
364     if (numPageFrames >= 0) {
365         mNumFramesLeftOnPage = numPageFrames;
366     }
367 
368     ogg_buffer buf;
369     buf.data = const_cast<unsigned char*>(data);
370     buf.size = inSize;
371     buf.refcount = 1;
372     buf.ptr.owner = nullptr;
373 
374     ogg_reference ref;
375     ref.buffer = &buf;
376     ref.begin = 0;
377     ref.length = buf.size;
378     ref.next = nullptr;
379 
380     ogg_packet pack;
381     pack.packet = &ref;
382     pack.bytes = ref.length;
383     pack.b_o_s = 0;
384     pack.e_o_s = 0;
385     pack.granulepos = 0;
386     pack.packetno = 0;
387 
388     size_t maxSamplesInBuffer = kMaxNumSamplesPerChannel * mVi->channels;
389     size_t outCapacity =  maxSamplesInBuffer * sizeof(int16_t);
390     std::shared_ptr<C2LinearBlock> block;
391     C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
392     c2_status_t err = pool->fetchLinearBlock(outCapacity, usage, &block);
393     if (err != C2_OK) {
394         ALOGE("fetchLinearBlock for Output failed with status %d", err);
395         work->result = C2_NO_MEMORY;
396         return;
397     }
398     C2WriteView wView = block->map().get();
399     if (wView.error()) {
400         ALOGE("write view map failed %d", wView.error());
401         work->result = wView.error();
402         return;
403     }
404 
405     int numFrames = 0;
406     int ret = vorbis_dsp_synthesis(mState, &pack, 1);
407     if (0 != ret) {
408         ALOGE("vorbis_dsp_synthesis returned %d", ret);
409         mSignalledError = true;
410         work->result = C2_CORRUPTED;
411         return;
412     } else {
413         numFrames = vorbis_dsp_pcmout(
414                 mState,  reinterpret_cast<int16_t *> (wView.data()),
415                 kMaxNumSamplesPerChannel);
416         if (numFrames < 0) {
417             ALOGD("vorbis_dsp_pcmout returned %d", numFrames);
418             numFrames = 0;
419         }
420     }
421 
422     if (mNumFramesLeftOnPage >= 0) {
423         if (numFrames > mNumFramesLeftOnPage) {
424             ALOGV("discarding %d frames at end of page", numFrames - mNumFramesLeftOnPage);
425             numFrames = mNumFramesLeftOnPage;
426         }
427         mNumFramesLeftOnPage -= numFrames;
428     }
429 
430     if (numFrames) {
431         int outSize = numFrames * sizeof(int16_t) * mVi->channels;
432 
433         work->worklets.front()->output.flags = work->input.flags;
434         work->worklets.front()->output.buffers.clear();
435         work->worklets.front()->output.buffers.push_back(createLinearBuffer(block, 0, outSize));
436         work->worklets.front()->output.ordinal = work->input.ordinal;
437         work->workletsProcessed = 1u;
438     } else {
439         fillEmptyWork(work);
440         block.reset();
441     }
442     if (eos) {
443         mSignalledOutputEos = true;
444         ALOGV("signalled EOS");
445     }
446 }
447 
448 class C2SoftVorbisDecFactory : public C2ComponentFactory {
449 public:
C2SoftVorbisDecFactory()450     C2SoftVorbisDecFactory() : 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 C2SoftVorbisDec(COMPONENT_NAME,
460                               id,
461                               std::make_shared<C2SoftVorbisDec::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<C2SoftVorbisDec::IntfImpl>(
472                         COMPONENT_NAME, id, std::make_shared<C2SoftVorbisDec::IntfImpl>(mHelper)),
473                 deleter);
474         return C2_OK;
475     }
476 
477     virtual ~C2SoftVorbisDecFactory() override = default;
478 
479 private:
480     std::shared_ptr<C2ReflectorHelper> mHelper;
481 };
482 
483 }  // namespace android
484 
CreateCodec2Factory()485 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
486     ALOGV("in %s", __func__);
487     return new ::android::C2SoftVorbisDecFactory();
488 }
489 
DestroyCodec2Factory(::C2ComponentFactory * factory)490 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
491     ALOGV("in %s", __func__);
492     delete factory;
493 }
494