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 "C2SoftOpusDec"
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 "C2SoftOpusDec.h"
27
28 extern "C" {
29 #include <opus.h>
30 #include <opus_multistream.h>
31 }
32
33 namespace android {
34
35 constexpr char COMPONENT_NAME[] = "c2.android.opus.decoder";
36
37 class C2SoftOpusDec::IntfImpl : public C2InterfaceHelper {
38 public:
IntfImpl(const std::shared_ptr<C2ReflectorHelper> & helper)39 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper>& helper)
40 : C2InterfaceHelper(helper) {
41 setDerivedInstance(this);
42
43 addParameter(
44 DefineParam(mInputFormat, C2_NAME_INPUT_STREAM_FORMAT_SETTING)
45 .withConstValue(new C2StreamFormatConfig::input(0u, C2FormatCompressed))
46 .build());
47
48 addParameter(
49 DefineParam(mOutputFormat, C2_NAME_OUTPUT_STREAM_FORMAT_SETTING)
50 .withConstValue(new C2StreamFormatConfig::output(0u, C2FormatAudio))
51 .build());
52
53 addParameter(
54 DefineParam(mInputMediaType, C2_NAME_INPUT_PORT_MIME_SETTING)
55 .withConstValue(AllocSharedString<C2PortMimeConfig::input>(
56 MEDIA_MIMETYPE_AUDIO_OPUS))
57 .build());
58
59 addParameter(
60 DefineParam(mOutputMediaType, C2_NAME_OUTPUT_PORT_MIME_SETTING)
61 .withConstValue(AllocSharedString<C2PortMimeConfig::output>(
62 MEDIA_MIMETYPE_AUDIO_RAW))
63 .build());
64
65 addParameter(
66 DefineParam(mSampleRate, C2_NAME_STREAM_SAMPLE_RATE_SETTING)
67 .withDefault(new C2StreamSampleRateInfo::output(0u, 48000))
68 .withFields({C2F(mSampleRate, value).equalTo(48000)})
69 .withSetter((Setter<decltype(*mSampleRate)>::StrictValueWithNoDeps))
70 .build());
71
72 addParameter(
73 DefineParam(mChannelCount, C2_NAME_STREAM_CHANNEL_COUNT_SETTING)
74 .withDefault(new C2StreamChannelCountInfo::output(0u, 1))
75 .withFields({C2F(mChannelCount, value).inRange(1, 8)})
76 .withSetter(Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps)
77 .build());
78
79 addParameter(
80 DefineParam(mBitrate, C2_NAME_STREAM_BITRATE_SETTING)
81 .withDefault(new C2BitrateTuning::input(0u, 6000))
82 .withFields({C2F(mBitrate, value).inRange(6000, 510000)})
83 .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps)
84 .build());
85
86 addParameter(
87 DefineParam(mInputMaxBufSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
88 .withConstValue(new C2StreamMaxBufferSizeInfo::input(0u, 960 * 6))
89 .build());
90 }
91
92 private:
93 std::shared_ptr<C2StreamFormatConfig::input> mInputFormat;
94 std::shared_ptr<C2StreamFormatConfig::output> mOutputFormat;
95 std::shared_ptr<C2PortMimeConfig::input> mInputMediaType;
96 std::shared_ptr<C2PortMimeConfig::output> mOutputMediaType;
97 std::shared_ptr<C2StreamSampleRateInfo::output> mSampleRate;
98 std::shared_ptr<C2StreamChannelCountInfo::output> mChannelCount;
99 std::shared_ptr<C2BitrateTuning::input> mBitrate;
100 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
101 };
102
C2SoftOpusDec(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)103 C2SoftOpusDec::C2SoftOpusDec(const char *name, c2_node_id_t id,
104 const std::shared_ptr<IntfImpl>& intfImpl)
105 : SimpleC2Component(
106 std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
107 mIntf(intfImpl),
108 mDecoder(nullptr) {
109 }
110
~C2SoftOpusDec()111 C2SoftOpusDec::~C2SoftOpusDec() {
112 onRelease();
113 }
114
onInit()115 c2_status_t C2SoftOpusDec::onInit() {
116 status_t err = initDecoder();
117 return err == OK ? C2_OK : C2_NO_MEMORY;
118 }
119
onStop()120 c2_status_t C2SoftOpusDec::onStop() {
121 if (mDecoder) {
122 opus_multistream_decoder_destroy(mDecoder);
123 mDecoder = nullptr;
124 }
125 memset(&mHeader, 0, sizeof(mHeader));
126 mCodecDelay = 0;
127 mSeekPreRoll = 0;
128 mSamplesToDiscard = 0;
129 mInputBufferCount = 0;
130 mSignalledError = false;
131 mSignalledOutputEos = false;
132
133 return C2_OK;
134 }
135
onReset()136 void C2SoftOpusDec::onReset() {
137 (void)onStop();
138 }
139
onRelease()140 void C2SoftOpusDec::onRelease() {
141 if (mDecoder) {
142 opus_multistream_decoder_destroy(mDecoder);
143 mDecoder = nullptr;
144 }
145 }
146
initDecoder()147 status_t C2SoftOpusDec::initDecoder() {
148 memset(&mHeader, 0, sizeof(mHeader));
149 mCodecDelay = 0;
150 mSeekPreRoll = 0;
151 mSamplesToDiscard = 0;
152 mInputBufferCount = 0;
153 mSignalledError = false;
154 mSignalledOutputEos = false;
155
156 return OK;
157 }
158
onFlush_sm()159 c2_status_t C2SoftOpusDec::onFlush_sm() {
160 if (mDecoder) {
161 opus_multistream_decoder_ctl(mDecoder, OPUS_RESET_STATE);
162 mSamplesToDiscard = mSeekPreRoll;
163 mSignalledOutputEos = false;
164 }
165 return C2_OK;
166 }
167
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)168 c2_status_t C2SoftOpusDec::drain(
169 uint32_t drainMode,
170 const std::shared_ptr<C2BlockPool> &pool) {
171 (void) pool;
172 if (drainMode == NO_DRAIN) {
173 ALOGW("drain with NO_DRAIN: no-op");
174 return C2_OK;
175 }
176 if (drainMode == DRAIN_CHAIN) {
177 ALOGW("DRAIN_CHAIN not supported");
178 return C2_OMITTED;
179 }
180
181 return C2_OK;
182 }
183
fillEmptyWork(const std::unique_ptr<C2Work> & work)184 static void fillEmptyWork(const std::unique_ptr<C2Work> &work) {
185 work->worklets.front()->output.flags = work->input.flags;
186 work->worklets.front()->output.buffers.clear();
187 work->worklets.front()->output.ordinal = work->input.ordinal;
188 work->workletsProcessed = 1u;
189 }
190
ReadLE16(const uint8_t * data,size_t data_size,uint32_t read_offset)191 static uint16_t ReadLE16(const uint8_t *data, size_t data_size,
192 uint32_t read_offset) {
193 if (read_offset + 1 > data_size)
194 return 0;
195 uint16_t val;
196 val = data[read_offset];
197 val |= data[read_offset + 1] << 8;
198 return val;
199 }
200
201 static const int kRate = 48000;
202
203 // Opus uses Vorbis channel mapping, and Vorbis channel mapping specifies
204 // mappings for up to 8 channels. This information is part of the Vorbis I
205 // Specification:
206 // http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html
207 static const int kMaxChannels = 8;
208
209 // Maximum packet size used in Xiph's opusdec.
210 static const int kMaxOpusOutputPacketSizeSamples = 960 * 6;
211
212 // Default audio output channel layout. Used to initialize |stream_map| in
213 // OpusHeader, and passed to opus_multistream_decoder_create() when the header
214 // does not contain mapping information. The values are valid only for mono and
215 // stereo output: Opus streams with more than 2 channels require a stream map.
216 static const int kMaxChannelsWithDefaultLayout = 2;
217 static const uint8_t kDefaultOpusChannelLayout[kMaxChannelsWithDefaultLayout] = { 0, 1 };
218
219 // Parses Opus Header. Header spec: http://wiki.xiph.org/OggOpus#ID_Header
ParseOpusHeader(const uint8_t * data,size_t data_size,OpusHeader * header)220 static bool ParseOpusHeader(const uint8_t *data, size_t data_size,
221 OpusHeader* header) {
222 // Size of the Opus header excluding optional mapping information.
223 const size_t kOpusHeaderSize = 19;
224
225 // Offset to the channel count byte in the Opus header.
226 const size_t kOpusHeaderChannelsOffset = 9;
227
228 // Offset to the pre-skip value in the Opus header.
229 const size_t kOpusHeaderSkipSamplesOffset = 10;
230
231 // Offset to the gain value in the Opus header.
232 const size_t kOpusHeaderGainOffset = 16;
233
234 // Offset to the channel mapping byte in the Opus header.
235 const size_t kOpusHeaderChannelMappingOffset = 18;
236
237 // Opus Header contains a stream map. The mapping values are in the header
238 // beyond the always present |kOpusHeaderSize| bytes of data. The mapping
239 // data contains stream count, coupling information, and per channel mapping
240 // values:
241 // - Byte 0: Number of streams.
242 // - Byte 1: Number coupled.
243 // - Byte 2: Starting at byte 2 are |header->channels| uint8 mapping
244 // values.
245 const size_t kOpusHeaderNumStreamsOffset = kOpusHeaderSize;
246 const size_t kOpusHeaderNumCoupledOffset = kOpusHeaderNumStreamsOffset + 1;
247 const size_t kOpusHeaderStreamMapOffset = kOpusHeaderNumStreamsOffset + 2;
248
249 if (data_size < kOpusHeaderSize) {
250 ALOGE("Header size is too small.");
251 return false;
252 }
253 header->channels = *(data + kOpusHeaderChannelsOffset);
254 if (header->channels <= 0 || header->channels > kMaxChannels) {
255 ALOGE("Invalid Header, wrong channel count: %d", header->channels);
256 return false;
257 }
258
259 header->skip_samples = ReadLE16(data,
260 data_size,
261 kOpusHeaderSkipSamplesOffset);
262
263 header->gain_db = static_cast<int16_t>(ReadLE16(data,
264 data_size,
265 kOpusHeaderGainOffset));
266
267 header->channel_mapping = *(data + kOpusHeaderChannelMappingOffset);
268 if (!header->channel_mapping) {
269 if (header->channels > kMaxChannelsWithDefaultLayout) {
270 ALOGE("Invalid Header, missing stream map.");
271 return false;
272 }
273 header->num_streams = 1;
274 header->num_coupled = header->channels > 1;
275 header->stream_map[0] = 0;
276 header->stream_map[1] = 1;
277 return true;
278 }
279 if (data_size < kOpusHeaderStreamMapOffset + header->channels) {
280 ALOGE("Invalid stream map; insufficient data for current channel "
281 "count: %d", header->channels);
282 return false;
283 }
284 header->num_streams = *(data + kOpusHeaderNumStreamsOffset);
285 header->num_coupled = *(data + kOpusHeaderNumCoupledOffset);
286 if (header->num_streams + header->num_coupled != header->channels) {
287 ALOGE("Inconsistent channel mapping.");
288 return false;
289 }
290 for (int i = 0; i < header->channels; ++i)
291 header->stream_map[i] = *(data + kOpusHeaderStreamMapOffset + i);
292 return true;
293 }
294
295 // Convert nanoseconds to number of samples.
ns_to_samples(uint64_t ns,int rate)296 static uint64_t ns_to_samples(uint64_t ns, int rate) {
297 return static_cast<double>(ns) * rate / 1000000000;
298 }
299
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)300 void C2SoftOpusDec::process(
301 const std::unique_ptr<C2Work> &work,
302 const std::shared_ptr<C2BlockPool> &pool) {
303 // Initialize output work
304 work->result = C2_OK;
305 work->workletsProcessed = 1u;
306 work->worklets.front()->output.configUpdate.clear();
307 work->worklets.front()->output.flags = work->input.flags;
308
309 if (mSignalledError || mSignalledOutputEos) {
310 work->result = C2_BAD_VALUE;
311 return;
312 }
313
314 bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
315 size_t inOffset = 0u;
316 size_t inSize = 0u;
317 C2ReadView rView = mDummyReadView;
318 if (!work->input.buffers.empty()) {
319 rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
320 inSize = rView.capacity();
321 if (inSize && rView.error()) {
322 ALOGE("read view map failed %d", rView.error());
323 work->result = C2_CORRUPTED;
324 return;
325 }
326 }
327 if (inSize == 0) {
328 fillEmptyWork(work);
329 if (eos) {
330 mSignalledOutputEos = true;
331 ALOGV("signalled EOS");
332 }
333 return;
334 }
335
336 ALOGV("in buffer attr. size %zu timestamp %d frameindex %d", inSize,
337 (int)work->input.ordinal.timestamp.peeku(), (int)work->input.ordinal.frameIndex.peeku());
338 const uint8_t *data = rView.data() + inOffset;
339 if (mInputBufferCount < 3) {
340 if (mInputBufferCount == 0) {
341 if (!ParseOpusHeader(data, inSize, &mHeader)) {
342 ALOGE("Encountered error while Parsing Opus Header.");
343 mSignalledError = true;
344 work->result = C2_CORRUPTED;
345 return;
346 }
347 uint8_t channel_mapping[kMaxChannels] = {0};
348 if (mHeader.channels <= kMaxChannelsWithDefaultLayout) {
349 memcpy(&channel_mapping,
350 kDefaultOpusChannelLayout,
351 kMaxChannelsWithDefaultLayout);
352 } else {
353 memcpy(&channel_mapping,
354 mHeader.stream_map,
355 mHeader.channels);
356 }
357 int status = OPUS_INVALID_STATE;
358 mDecoder = opus_multistream_decoder_create(kRate,
359 mHeader.channels,
360 mHeader.num_streams,
361 mHeader.num_coupled,
362 channel_mapping,
363 &status);
364 if (!mDecoder || status != OPUS_OK) {
365 ALOGE("opus_multistream_decoder_create failed status = %s",
366 opus_strerror(status));
367 mSignalledError = true;
368 work->result = C2_CORRUPTED;
369 return;
370 }
371 status = opus_multistream_decoder_ctl(mDecoder,
372 OPUS_SET_GAIN(mHeader.gain_db));
373 if (status != OPUS_OK) {
374 ALOGE("Failed to set OPUS header gain; status = %s",
375 opus_strerror(status));
376 mSignalledError = true;
377 work->result = C2_CORRUPTED;
378 return;
379 }
380 } else {
381 if (inSize < 8) {
382 ALOGE("Input sample size is too small.");
383 mSignalledError = true;
384 work->result = C2_CORRUPTED;
385 return;
386 }
387 int64_t samples = ns_to_samples( *(reinterpret_cast<int64_t*>
388 (const_cast<uint8_t *> (data))), kRate);
389 if (mInputBufferCount == 1) {
390 mCodecDelay = samples;
391 mSamplesToDiscard = mCodecDelay;
392 }
393 else {
394 mSeekPreRoll = samples;
395
396 ALOGI("Configuring decoder: %d Hz, %d channels",
397 kRate, mHeader.channels);
398 C2StreamSampleRateInfo::output sampleRateInfo(0u, kRate);
399 C2StreamChannelCountInfo::output channelCountInfo(0u, mHeader.channels);
400 std::vector<std::unique_ptr<C2SettingResult>> failures;
401 c2_status_t err = mIntf->config(
402 { &sampleRateInfo, &channelCountInfo },
403 C2_MAY_BLOCK,
404 &failures);
405 if (err == OK) {
406 work->worklets.front()->output.configUpdate.push_back(C2Param::Copy(sampleRateInfo));
407 work->worklets.front()->output.configUpdate.push_back(C2Param::Copy(channelCountInfo));
408 } else {
409 ALOGE("Config Update failed");
410 mSignalledError = true;
411 work->result = C2_CORRUPTED;
412 return;
413 }
414 }
415 }
416
417 ++mInputBufferCount;
418 fillEmptyWork(work);
419 if (eos) {
420 mSignalledOutputEos = true;
421 ALOGV("signalled EOS");
422 }
423 return;
424 }
425
426 // Ignore CSD re-submissions.
427 if ((work->input.flags & C2FrameData::FLAG_CODEC_CONFIG)) {
428 fillEmptyWork(work);
429 return;
430 }
431
432 // When seeking to zero, |mCodecDelay| samples has to be discarded
433 // instead of |mSeekPreRoll| samples (as we would when seeking to any
434 // other timestamp).
435 if (work->input.ordinal.timestamp.peeku() == 0) mSamplesToDiscard = mCodecDelay;
436
437 std::shared_ptr<C2LinearBlock> block;
438 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
439 c2_status_t err = pool->fetchLinearBlock(
440 kMaxNumSamplesPerBuffer * kMaxChannels * sizeof(int16_t),
441 usage, &block);
442 if (err != C2_OK) {
443 ALOGE("fetchLinearBlock for Output failed with status %d", err);
444 work->result = C2_NO_MEMORY;
445 return;
446 }
447 C2WriteView wView = block->map().get();
448 if (wView.error()) {
449 ALOGE("write view map failed %d", wView.error());
450 work->result = C2_CORRUPTED;
451 return;
452 }
453
454 int numSamples = opus_multistream_decode(mDecoder,
455 data,
456 inSize,
457 reinterpret_cast<int16_t *> (wView.data()),
458 kMaxOpusOutputPacketSizeSamples,
459 0);
460 if (numSamples < 0) {
461 ALOGE("opus_multistream_decode returned numSamples %d", numSamples);
462 numSamples = 0;
463 mSignalledError = true;
464 work->result = C2_CORRUPTED;
465 return;
466 }
467
468 int outOffset = 0;
469 if (mSamplesToDiscard > 0) {
470 if (mSamplesToDiscard > numSamples) {
471 mSamplesToDiscard -= numSamples;
472 numSamples = 0;
473 } else {
474 numSamples -= mSamplesToDiscard;
475 outOffset = mSamplesToDiscard * sizeof(int16_t) * mHeader.channels;
476 mSamplesToDiscard = 0;
477 }
478 }
479
480 if (numSamples) {
481 int outSize = numSamples * sizeof(int16_t) * mHeader.channels;
482 ALOGV("out buffer attr. offset %d size %d ", outOffset, outSize);
483
484 work->worklets.front()->output.flags = work->input.flags;
485 work->worklets.front()->output.buffers.clear();
486 work->worklets.front()->output.buffers.push_back(createLinearBuffer(block, outOffset, outSize));
487 work->worklets.front()->output.ordinal = work->input.ordinal;
488 work->workletsProcessed = 1u;
489 } else {
490 fillEmptyWork(work);
491 block.reset();
492 }
493 if (eos) {
494 mSignalledOutputEos = true;
495 ALOGV("signalled EOS");
496 }
497 }
498
499 class C2SoftOpusDecFactory : public C2ComponentFactory {
500 public:
C2SoftOpusDecFactory()501 C2SoftOpusDecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
502 GetCodec2PlatformComponentStore()->getParamReflector())) {
503 }
504
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)505 virtual c2_status_t createComponent(
506 c2_node_id_t id,
507 std::shared_ptr<C2Component>* const component,
508 std::function<void(C2Component*)> deleter) override {
509 *component = std::shared_ptr<C2Component>(
510 new C2SoftOpusDec(COMPONENT_NAME,
511 id,
512 std::make_shared<C2SoftOpusDec::IntfImpl>(mHelper)),
513 deleter);
514 return C2_OK;
515 }
516
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)517 virtual c2_status_t createInterface(
518 c2_node_id_t id,
519 std::shared_ptr<C2ComponentInterface>* const interface,
520 std::function<void(C2ComponentInterface*)> deleter) override {
521 *interface = std::shared_ptr<C2ComponentInterface>(
522 new SimpleInterface<C2SoftOpusDec::IntfImpl>(
523 COMPONENT_NAME, id, std::make_shared<C2SoftOpusDec::IntfImpl>(mHelper)),
524 deleter);
525 return C2_OK;
526 }
527
528 virtual ~C2SoftOpusDecFactory() override = default;
529
530 private:
531 std::shared_ptr<C2ReflectorHelper> mHelper;
532 };
533
534 } // namespace android
535
CreateCodec2Factory()536 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
537 ALOGV("in %s", __func__);
538 return new ::android::C2SoftOpusDecFactory();
539 }
540
DestroyCodec2Factory(::C2ComponentFactory * factory)541 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
542 ALOGV("in %s", __func__);
543 delete factory;
544 }
545