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