blob: 342d7718ea45ef90f62f6cb91ad6bbb65f73dd5b [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright (C) 2017 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 "C2SoftAacDec"
19#include <log/log.h>
20
21#include <inttypes.h>
22#include <math.h>
23#include <numeric>
24
25#include <cutils/properties.h>
26#include <media/stagefright/foundation/MediaDefs.h>
27#include <media/stagefright/foundation/hexdump.h>
28#include <media/stagefright/MediaErrors.h>
29#include <utils/misc.h>
30
31#include <C2PlatformSupport.h>
32#include <SimpleC2Interface.h>
33
34#include "C2SoftAacDec.h"
35
36#define FILEREAD_MAX_LAYERS 2
37
38#define DRC_DEFAULT_MOBILE_REF_LEVEL -16.0 /* 64*-0.25dB = -16 dB below full scale for mobile conf */
39#define DRC_DEFAULT_MOBILE_DRC_CUT 1.0 /* maximum compression of dynamic range for mobile conf */
40#define DRC_DEFAULT_MOBILE_DRC_BOOST 1.0 /* maximum compression of dynamic range for mobile conf */
41#define DRC_DEFAULT_MOBILE_DRC_HEAVY C2Config::DRC_COMPRESSION_HEAVY /* switch for heavy compression for mobile conf */
42#define DRC_DEFAULT_MOBILE_DRC_EFFECT 3 /* MPEG-D DRC effect type; 3 => Limited playback range */
Jean-Michel Trivi670b8fb2020-02-18 07:54:05 -080043#define DRC_DEFAULT_MOBILE_DRC_ALBUM 0 /* MPEG-D DRC album mode; 0 => album mode is disabled, 1 => album mode is enabled */
Jean-Michel Trivi4b936cc2020-02-17 16:29:47 -080044#define DRC_DEFAULT_MOBILE_OUTPUT_LOUDNESS (0.25) /* decoder output loudness; -1 => the value is unknown, otherwise dB step value (e.g. 64 for -16 dB) */
Pawin Vongmasa36653902018-11-15 00:10:25 -080045#define DRC_DEFAULT_MOBILE_ENC_LEVEL (0.25) /* encoder target level; -1 => the value is unknown, otherwise dB step value (e.g. 64 for -16 dB) */
46#define MAX_CHANNEL_COUNT 8 /* maximum number of audio channels that can be decoded */
47// names of properties that can be used to override the default DRC settings
48#define PROP_DRC_OVERRIDE_REF_LEVEL "aac_drc_reference_level"
49#define PROP_DRC_OVERRIDE_CUT "aac_drc_cut"
50#define PROP_DRC_OVERRIDE_BOOST "aac_drc_boost"
51#define PROP_DRC_OVERRIDE_HEAVY "aac_drc_heavy"
52#define PROP_DRC_OVERRIDE_ENC_LEVEL "aac_drc_enc_target_level"
53#define PROP_DRC_OVERRIDE_EFFECT "ro.aac_drc_effect_type"
54
55namespace android {
56
Wonsik Kimab34ed62019-01-31 15:28:46 -080057constexpr char COMPONENT_NAME[] = "c2.android.aac.decoder";
Harish Mahendrakar3c415ec2021-03-22 13:56:52 -070058constexpr size_t kDefaultOutputPortDelay = 2;
59constexpr size_t kMaxOutputPortDelay = 16;
Wonsik Kimab34ed62019-01-31 15:28:46 -080060
61class C2SoftAacDec::IntfImpl : public SimpleInterface<void>::BaseParams {
Pawin Vongmasa36653902018-11-15 00:10:25 -080062public:
63 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
Wonsik Kimab34ed62019-01-31 15:28:46 -080064 : SimpleInterface<void>::BaseParams(
65 helper,
66 COMPONENT_NAME,
67 C2Component::KIND_DECODER,
68 C2Component::DOMAIN_AUDIO,
69 MEDIA_MIMETYPE_AUDIO_AAC) {
70 noPrivateBuffers();
71 noInputReferences();
72 noOutputReferences();
73 noInputLatency();
74 noTimeStretch();
Pawin Vongmasa36653902018-11-15 00:10:25 -080075
76 addParameter(
Wonsik Kimab34ed62019-01-31 15:28:46 -080077 DefineParam(mActualOutputDelay, C2_PARAMKEY_OUTPUT_DELAY)
Harish Mahendrakar3c415ec2021-03-22 13:56:52 -070078 .withDefault(new C2PortActualDelayTuning::output(kDefaultOutputPortDelay))
79 .withFields({C2F(mActualOutputDelay, value).inRange(0, kMaxOutputPortDelay)})
80 .withSetter(Setter<decltype(*mActualOutputDelay)>::StrictValueWithNoDeps)
Pawin Vongmasa36653902018-11-15 00:10:25 -080081 .build());
82
83 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080084 DefineParam(mSampleRate, C2_PARAMKEY_SAMPLE_RATE)
Pawin Vongmasa36653902018-11-15 00:10:25 -080085 .withDefault(new C2StreamSampleRateInfo::output(0u, 44100))
86 .withFields({C2F(mSampleRate, value).oneOf({
Sungtak Leed7f88ee2019-11-14 16:04:25 -080087 7350, 8000, 11025, 12000, 16000, 22050, 24000, 32000,
88 44100, 48000, 64000, 88200, 96000
Pawin Vongmasa36653902018-11-15 00:10:25 -080089 })})
90 .withSetter(Setter<decltype(*mSampleRate)>::NonStrictValueWithNoDeps)
91 .build());
92
93 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080094 DefineParam(mChannelCount, C2_PARAMKEY_CHANNEL_COUNT)
Pawin Vongmasa36653902018-11-15 00:10:25 -080095 .withDefault(new C2StreamChannelCountInfo::output(0u, 1))
Jean-Michel Trivieba54dc2020-06-10 18:21:56 -070096 .withFields({C2F(mChannelCount, value).inRange(1, MAX_CHANNEL_COUNT)})
Pawin Vongmasa36653902018-11-15 00:10:25 -080097 .withSetter(Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps)
98 .build());
99
100 addParameter(
Jean-Michel Trivieba54dc2020-06-10 18:21:56 -0700101 DefineParam(mMaxChannelCount, C2_PARAMKEY_MAX_CHANNEL_COUNT)
102 .withDefault(new C2StreamMaxChannelCountInfo::input(0u, MAX_CHANNEL_COUNT))
103 .withFields({C2F(mMaxChannelCount, value).inRange(1, MAX_CHANNEL_COUNT)})
104 .withSetter(Setter<decltype(*mMaxChannelCount)>::StrictValueWithNoDeps)
105 .build());
106
107 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800108 DefineParam(mBitrate, C2_PARAMKEY_BITRATE)
109 .withDefault(new C2StreamBitrateInfo::input(0u, 64000))
Pawin Vongmasa36653902018-11-15 00:10:25 -0800110 .withFields({C2F(mBitrate, value).inRange(8000, 960000)})
111 .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps)
112 .build());
113
114 addParameter(
115 DefineParam(mInputMaxBufSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
116 .withConstValue(new C2StreamMaxBufferSizeInfo::input(0u, 8192))
117 .build());
118
119 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800120 DefineParam(mAacFormat, C2_PARAMKEY_AAC_PACKAGING)
121 .withDefault(new C2StreamAacFormatInfo::input(0u, C2Config::AAC_PACKAGING_RAW))
Pawin Vongmasa36653902018-11-15 00:10:25 -0800122 .withFields({C2F(mAacFormat, value).oneOf({
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800123 C2Config::AAC_PACKAGING_RAW, C2Config::AAC_PACKAGING_ADTS
Pawin Vongmasa36653902018-11-15 00:10:25 -0800124 })})
125 .withSetter(Setter<decltype(*mAacFormat)>::StrictValueWithNoDeps)
126 .build());
127
128 addParameter(
129 DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
130 .withDefault(new C2StreamProfileLevelInfo::input(0u,
131 C2Config::PROFILE_AAC_LC, C2Config::LEVEL_UNUSED))
132 .withFields({
133 C2F(mProfileLevel, profile).oneOf({
134 C2Config::PROFILE_AAC_LC,
135 C2Config::PROFILE_AAC_HE,
136 C2Config::PROFILE_AAC_HE_PS,
137 C2Config::PROFILE_AAC_LD,
138 C2Config::PROFILE_AAC_ELD,
139 C2Config::PROFILE_AAC_ER_SCALABLE,
140 C2Config::PROFILE_AAC_XHE}),
141 C2F(mProfileLevel, level).oneOf({
142 C2Config::LEVEL_UNUSED
143 })
144 })
145 .withSetter(ProfileLevelSetter)
146 .build());
147
148 addParameter(
149 DefineParam(mDrcCompressMode, C2_PARAMKEY_DRC_COMPRESSION_MODE)
150 .withDefault(new C2StreamDrcCompressionModeTuning::input(0u, C2Config::DRC_COMPRESSION_HEAVY))
151 .withFields({
152 C2F(mDrcCompressMode, value).oneOf({
153 C2Config::DRC_COMPRESSION_ODM_DEFAULT,
154 C2Config::DRC_COMPRESSION_NONE,
155 C2Config::DRC_COMPRESSION_LIGHT,
156 C2Config::DRC_COMPRESSION_HEAVY})
157 })
158 .withSetter(Setter<decltype(*mDrcCompressMode)>::StrictValueWithNoDeps)
159 .build());
160
161 addParameter(
162 DefineParam(mDrcTargetRefLevel, C2_PARAMKEY_DRC_TARGET_REFERENCE_LEVEL)
163 .withDefault(new C2StreamDrcTargetReferenceLevelTuning::input(0u, DRC_DEFAULT_MOBILE_REF_LEVEL))
164 .withFields({C2F(mDrcTargetRefLevel, value).inRange(-31.75, 0.25)})
165 .withSetter(Setter<decltype(*mDrcTargetRefLevel)>::StrictValueWithNoDeps)
166 .build());
167
168 addParameter(
169 DefineParam(mDrcEncTargetLevel, C2_PARAMKEY_DRC_ENCODED_TARGET_LEVEL)
170 .withDefault(new C2StreamDrcEncodedTargetLevelTuning::input(0u, DRC_DEFAULT_MOBILE_ENC_LEVEL))
171 .withFields({C2F(mDrcEncTargetLevel, value).inRange(-31.75, 0.25)})
172 .withSetter(Setter<decltype(*mDrcEncTargetLevel)>::StrictValueWithNoDeps)
173 .build());
174
175 addParameter(
176 DefineParam(mDrcBoostFactor, C2_PARAMKEY_DRC_BOOST_FACTOR)
177 .withDefault(new C2StreamDrcBoostFactorTuning::input(0u, DRC_DEFAULT_MOBILE_DRC_BOOST))
178 .withFields({C2F(mDrcBoostFactor, value).inRange(0, 1.)})
179 .withSetter(Setter<decltype(*mDrcBoostFactor)>::StrictValueWithNoDeps)
180 .build());
181
182 addParameter(
183 DefineParam(mDrcAttenuationFactor, C2_PARAMKEY_DRC_ATTENUATION_FACTOR)
184 .withDefault(new C2StreamDrcAttenuationFactorTuning::input(0u, DRC_DEFAULT_MOBILE_DRC_CUT))
185 .withFields({C2F(mDrcAttenuationFactor, value).inRange(0, 1.)})
186 .withSetter(Setter<decltype(*mDrcAttenuationFactor)>::StrictValueWithNoDeps)
187 .build());
188
189 addParameter(
190 DefineParam(mDrcEffectType, C2_PARAMKEY_DRC_EFFECT_TYPE)
191 .withDefault(new C2StreamDrcEffectTypeTuning::input(0u, C2Config::DRC_EFFECT_LIMITED_PLAYBACK_RANGE))
192 .withFields({
193 C2F(mDrcEffectType, value).oneOf({
194 C2Config::DRC_EFFECT_ODM_DEFAULT,
195 C2Config::DRC_EFFECT_OFF,
196 C2Config::DRC_EFFECT_NONE,
197 C2Config::DRC_EFFECT_LATE_NIGHT,
198 C2Config::DRC_EFFECT_NOISY_ENVIRONMENT,
199 C2Config::DRC_EFFECT_LIMITED_PLAYBACK_RANGE,
200 C2Config::DRC_EFFECT_LOW_PLAYBACK_LEVEL,
201 C2Config::DRC_EFFECT_DIALOG_ENHANCEMENT,
202 C2Config::DRC_EFFECT_GENERAL_COMPRESSION})
203 })
204 .withSetter(Setter<decltype(*mDrcEffectType)>::StrictValueWithNoDeps)
205 .build());
Jean-Michel Trivi4b936cc2020-02-17 16:29:47 -0800206
207 addParameter(
Jean-Michel Trivi670b8fb2020-02-18 07:54:05 -0800208 DefineParam(mDrcAlbumMode, C2_PARAMKEY_DRC_ALBUM_MODE)
209 .withDefault(new C2StreamDrcAlbumModeTuning::input(0u, C2Config::DRC_ALBUM_MODE_OFF))
210 .withFields({
211 C2F(mDrcAlbumMode, value).oneOf({
212 C2Config::DRC_ALBUM_MODE_OFF,
213 C2Config::DRC_ALBUM_MODE_ON})
214 })
215 .withSetter(Setter<decltype(*mDrcAlbumMode)>::StrictValueWithNoDeps)
216 .build());
217
218 addParameter(
Jean-Michel Trivi4b936cc2020-02-17 16:29:47 -0800219 DefineParam(mDrcOutputLoudness, C2_PARAMKEY_DRC_OUTPUT_LOUDNESS)
220 .withDefault(new C2StreamDrcOutputLoudnessTuning::output(0u, DRC_DEFAULT_MOBILE_OUTPUT_LOUDNESS))
221 .withFields({C2F(mDrcOutputLoudness, value).inRange(-57.75, 0.25)})
222 .withSetter(Setter<decltype(*mDrcOutputLoudness)>::StrictValueWithNoDeps)
223 .build());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800224 }
225
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800226 bool isAdts() const { return mAacFormat->value == C2Config::AAC_PACKAGING_ADTS; }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800227 static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::input> &me) {
228 (void)mayBlock;
229 (void)me; // TODO: validate
230 return C2R::Ok();
231 }
232 int32_t getDrcCompressMode() const { return mDrcCompressMode->value == C2Config::DRC_COMPRESSION_HEAVY ? 1 : 0; }
233 int32_t getDrcTargetRefLevel() const { return (mDrcTargetRefLevel->value <= 0 ? -mDrcTargetRefLevel->value * 4. + 0.5 : -1); }
234 int32_t getDrcEncTargetLevel() const { return (mDrcEncTargetLevel->value <= 0 ? -mDrcEncTargetLevel->value * 4. + 0.5 : -1); }
235 int32_t getDrcBoostFactor() const { return mDrcBoostFactor->value * 127. + 0.5; }
236 int32_t getDrcAttenuationFactor() const { return mDrcAttenuationFactor->value * 127. + 0.5; }
237 int32_t getDrcEffectType() const { return mDrcEffectType->value; }
Jean-Michel Trivi670b8fb2020-02-18 07:54:05 -0800238 int32_t getDrcAlbumMode() const { return mDrcAlbumMode->value; }
Jean-Michel Trivieba54dc2020-06-10 18:21:56 -0700239 u_int32_t getMaxChannelCount() const { return mMaxChannelCount->value; }
Jean-Michel Trivi4b936cc2020-02-17 16:29:47 -0800240 int32_t getDrcOutputLoudness() const { return (mDrcOutputLoudness->value <= 0 ? -mDrcOutputLoudness->value * 4. + 0.5 : -1); }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800241
242private:
Pawin Vongmasa36653902018-11-15 00:10:25 -0800243 std::shared_ptr<C2StreamSampleRateInfo::output> mSampleRate;
244 std::shared_ptr<C2StreamChannelCountInfo::output> mChannelCount;
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800245 std::shared_ptr<C2StreamBitrateInfo::input> mBitrate;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800246 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
247 std::shared_ptr<C2StreamAacFormatInfo::input> mAacFormat;
248 std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel;
249 std::shared_ptr<C2StreamDrcCompressionModeTuning::input> mDrcCompressMode;
250 std::shared_ptr<C2StreamDrcTargetReferenceLevelTuning::input> mDrcTargetRefLevel;
251 std::shared_ptr<C2StreamDrcEncodedTargetLevelTuning::input> mDrcEncTargetLevel;
252 std::shared_ptr<C2StreamDrcBoostFactorTuning::input> mDrcBoostFactor;
253 std::shared_ptr<C2StreamDrcAttenuationFactorTuning::input> mDrcAttenuationFactor;
254 std::shared_ptr<C2StreamDrcEffectTypeTuning::input> mDrcEffectType;
Jean-Michel Trivi670b8fb2020-02-18 07:54:05 -0800255 std::shared_ptr<C2StreamDrcAlbumModeTuning::input> mDrcAlbumMode;
Jean-Michel Trivieba54dc2020-06-10 18:21:56 -0700256 std::shared_ptr<C2StreamMaxChannelCountInfo::input> mMaxChannelCount;
Jean-Michel Trivi4b936cc2020-02-17 16:29:47 -0800257 std::shared_ptr<C2StreamDrcOutputLoudnessTuning::output> mDrcOutputLoudness;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800258 // TODO Add : C2StreamAacSbrModeTuning
259};
260
Pawin Vongmasa36653902018-11-15 00:10:25 -0800261C2SoftAacDec::C2SoftAacDec(
262 const char *name,
263 c2_node_id_t id,
264 const std::shared_ptr<IntfImpl> &intfImpl)
265 : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
266 mIntf(intfImpl),
267 mAACDecoder(nullptr),
268 mStreamInfo(nullptr),
269 mSignalledError(false),
Harish Mahendrakar3c415ec2021-03-22 13:56:52 -0700270 mOutputPortDelay(kDefaultOutputPortDelay),
Pawin Vongmasa36653902018-11-15 00:10:25 -0800271 mOutputDelayRingBuffer(nullptr) {
272}
273
274C2SoftAacDec::~C2SoftAacDec() {
275 onRelease();
276}
277
278c2_status_t C2SoftAacDec::onInit() {
279 status_t err = initDecoder();
280 return err == OK ? C2_OK : C2_CORRUPTED;
281}
282
283c2_status_t C2SoftAacDec::onStop() {
284 drainDecoder();
285 // reset the "configured" state
286 mOutputDelayCompensated = 0;
287 mOutputDelayRingBufferWritePos = 0;
288 mOutputDelayRingBufferReadPos = 0;
289 mOutputDelayRingBufferFilled = 0;
290 mBuffersInfo.clear();
291
Rakesh Kumar87604ef2021-05-06 18:28:37 +0530292 status_t status = UNKNOWN_ERROR;
293 if (mAACDecoder) {
294 aacDecoder_Close(mAACDecoder);
295 status = initDecoder();
296 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800297 mSignalledError = false;
298
Rakesh Kumar87604ef2021-05-06 18:28:37 +0530299 return status == OK ? C2_OK : C2_CORRUPTED;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800300}
301
302void C2SoftAacDec::onReset() {
303 (void)onStop();
304}
305
306void C2SoftAacDec::onRelease() {
307 if (mAACDecoder) {
308 aacDecoder_Close(mAACDecoder);
309 mAACDecoder = nullptr;
310 }
311 if (mOutputDelayRingBuffer) {
312 delete[] mOutputDelayRingBuffer;
313 mOutputDelayRingBuffer = nullptr;
314 }
315}
316
317status_t C2SoftAacDec::initDecoder() {
318 ALOGV("initDecoder()");
319 status_t status = UNKNOWN_ERROR;
320 mAACDecoder = aacDecoder_Open(TT_MP4_ADIF, /* num layers */ 1);
321 if (mAACDecoder != nullptr) {
322 mStreamInfo = aacDecoder_GetStreamInfo(mAACDecoder);
323 if (mStreamInfo != nullptr) {
324 status = OK;
325 }
326 }
327
328 mOutputDelayCompensated = 0;
329 mOutputDelayRingBufferSize = 2048 * MAX_CHANNEL_COUNT * kNumDelayBlocksMax;
330 mOutputDelayRingBuffer = new short[mOutputDelayRingBufferSize];
331 mOutputDelayRingBufferWritePos = 0;
332 mOutputDelayRingBufferReadPos = 0;
333 mOutputDelayRingBufferFilled = 0;
334
335 if (mAACDecoder == nullptr) {
336 ALOGE("AAC decoder is null. TODO: Can not call aacDecoder_SetParam in the following code");
337 }
338
339 //aacDecoder_SetParam(mAACDecoder, AAC_PCM_LIMITER_ENABLE, 0);
340
341 //init DRC wrapper
342 mDrcWrap.setDecoderHandle(mAACDecoder);
343 mDrcWrap.submitStreamData(mStreamInfo);
344
345 // for streams that contain metadata, use the mobile profile DRC settings unless overridden by platform properties
346 // TODO: change the DRC settings depending on audio output device type (HDMI, loadspeaker, headphone)
347
348 // DRC_PRES_MODE_WRAP_DESIRED_TARGET
349 int32_t targetRefLevel = mIntf->getDrcTargetRefLevel();
350 ALOGV("AAC decoder using desired DRC target reference level of %d", targetRefLevel);
351 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_TARGET, (unsigned)targetRefLevel);
352
353 // DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR
354
355 int32_t attenuationFactor = mIntf->getDrcAttenuationFactor();
356 ALOGV("AAC decoder using desired DRC attenuation factor of %d", attenuationFactor);
357 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR, (unsigned)attenuationFactor);
358
359 // DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR
360 int32_t boostFactor = mIntf->getDrcBoostFactor();
361 ALOGV("AAC decoder using desired DRC boost factor of %d", boostFactor);
362 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR, (unsigned)boostFactor);
363
364 // DRC_PRES_MODE_WRAP_DESIRED_HEAVY
365 int32_t compressMode = mIntf->getDrcCompressMode();
Jean-Michel Triviedf942b2020-01-29 09:59:44 -0800366 ALOGV("AAC decoder using desired DRC heavy compression switch of %d", compressMode);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800367 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_HEAVY, (unsigned)compressMode);
368
369 // DRC_PRES_MODE_WRAP_ENCODER_TARGET
370 int32_t encTargetLevel = mIntf->getDrcEncTargetLevel();
371 ALOGV("AAC decoder using encoder-side DRC reference level of %d", encTargetLevel);
372 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_ENCODER_TARGET, (unsigned)encTargetLevel);
373
374 // AAC_UNIDRC_SET_EFFECT
375 int32_t effectType = mIntf->getDrcEffectType();
376 ALOGV("AAC decoder using MPEG-D DRC effect type %d", effectType);
377 aacDecoder_SetParam(mAACDecoder, AAC_UNIDRC_SET_EFFECT, effectType);
378
Jean-Michel Trivi670b8fb2020-02-18 07:54:05 -0800379 // AAC_UNIDRC_ALBUM_MODE
380 int32_t albumMode = mIntf->getDrcAlbumMode();
381 ALOGV("AAC decoder using MPEG-D DRC album mode %d", albumMode);
382 aacDecoder_SetParam(mAACDecoder, AAC_UNIDRC_ALBUM_MODE, albumMode);
383
Jean-Michel Trivieba54dc2020-06-10 18:21:56 -0700384 // AAC_PCM_MAX_OUTPUT_CHANNELS
385 u_int32_t maxChannelCount = mIntf->getMaxChannelCount();
386 ALOGV("AAC decoder using maximum output channel count %d", maxChannelCount);
387 aacDecoder_SetParam(mAACDecoder, AAC_PCM_MAX_OUTPUT_CHANNELS, maxChannelCount);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800388
389 return status;
390}
391
392bool C2SoftAacDec::outputDelayRingBufferPutSamples(INT_PCM *samples, int32_t numSamples) {
393 if (numSamples == 0) {
394 return true;
395 }
396 if (outputDelayRingBufferSpaceLeft() < numSamples) {
397 ALOGE("RING BUFFER WOULD OVERFLOW");
398 return false;
399 }
400 if (mOutputDelayRingBufferWritePos + numSamples <= mOutputDelayRingBufferSize
401 && (mOutputDelayRingBufferReadPos <= mOutputDelayRingBufferWritePos
402 || mOutputDelayRingBufferReadPos > mOutputDelayRingBufferWritePos + numSamples)) {
403 // faster memcopy loop without checks, if the preconditions allow this
404 for (int32_t i = 0; i < numSamples; i++) {
405 mOutputDelayRingBuffer[mOutputDelayRingBufferWritePos++] = samples[i];
406 }
407
408 if (mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferSize) {
409 mOutputDelayRingBufferWritePos -= mOutputDelayRingBufferSize;
410 }
411 } else {
412 ALOGV("slow C2SoftAacDec::outputDelayRingBufferPutSamples()");
413
414 for (int32_t i = 0; i < numSamples; i++) {
415 mOutputDelayRingBuffer[mOutputDelayRingBufferWritePos] = samples[i];
416 mOutputDelayRingBufferWritePos++;
417 if (mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferSize) {
418 mOutputDelayRingBufferWritePos -= mOutputDelayRingBufferSize;
419 }
420 }
421 }
422 mOutputDelayRingBufferFilled += numSamples;
423 return true;
424}
425
426int32_t C2SoftAacDec::outputDelayRingBufferGetSamples(INT_PCM *samples, int32_t numSamples) {
427
428 if (numSamples > mOutputDelayRingBufferFilled) {
429 ALOGE("RING BUFFER WOULD UNDERRUN");
430 return -1;
431 }
432
433 if (mOutputDelayRingBufferReadPos + numSamples <= mOutputDelayRingBufferSize
434 && (mOutputDelayRingBufferWritePos < mOutputDelayRingBufferReadPos
435 || mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferReadPos + numSamples)) {
436 // faster memcopy loop without checks, if the preconditions allow this
437 if (samples != nullptr) {
438 for (int32_t i = 0; i < numSamples; i++) {
439 samples[i] = mOutputDelayRingBuffer[mOutputDelayRingBufferReadPos++];
440 }
441 } else {
442 mOutputDelayRingBufferReadPos += numSamples;
443 }
444 if (mOutputDelayRingBufferReadPos >= mOutputDelayRingBufferSize) {
445 mOutputDelayRingBufferReadPos -= mOutputDelayRingBufferSize;
446 }
447 } else {
448 ALOGV("slow C2SoftAacDec::outputDelayRingBufferGetSamples()");
449
450 for (int32_t i = 0; i < numSamples; i++) {
451 if (samples != nullptr) {
452 samples[i] = mOutputDelayRingBuffer[mOutputDelayRingBufferReadPos];
453 }
454 mOutputDelayRingBufferReadPos++;
455 if (mOutputDelayRingBufferReadPos >= mOutputDelayRingBufferSize) {
456 mOutputDelayRingBufferReadPos -= mOutputDelayRingBufferSize;
457 }
458 }
459 }
460 mOutputDelayRingBufferFilled -= numSamples;
461 return numSamples;
462}
463
464int32_t C2SoftAacDec::outputDelayRingBufferSamplesAvailable() {
465 return mOutputDelayRingBufferFilled;
466}
467
468int32_t C2SoftAacDec::outputDelayRingBufferSpaceLeft() {
469 return mOutputDelayRingBufferSize - outputDelayRingBufferSamplesAvailable();
470}
471
472void C2SoftAacDec::drainRingBuffer(
473 const std::unique_ptr<C2Work> &work,
474 const std::shared_ptr<C2BlockPool> &pool,
475 bool eos) {
476 while (!mBuffersInfo.empty() && outputDelayRingBufferSamplesAvailable()
477 >= mStreamInfo->frameSize * mStreamInfo->numChannels) {
478 Info &outInfo = mBuffersInfo.front();
479 ALOGV("outInfo.frameIndex = %" PRIu64, outInfo.frameIndex);
480 int samplesize __unused = mStreamInfo->numChannels * sizeof(int16_t);
481
482 int available = outputDelayRingBufferSamplesAvailable();
483 int numFrames = outInfo.decodedSizes.size();
484 int numSamples = numFrames * (mStreamInfo->frameSize * mStreamInfo->numChannels);
485 if (available < numSamples) {
486 if (eos) {
487 numSamples = available;
488 } else {
489 break;
490 }
491 }
492 ALOGV("%d samples available (%d), or %d frames",
493 numSamples, available, numFrames);
494 ALOGV("getting %d from ringbuffer", numSamples);
495
496 std::shared_ptr<C2LinearBlock> block;
497 std::function<void(const std::unique_ptr<C2Work>&)> fillWork =
498 [&block, numSamples, pool, this]()
499 -> std::function<void(const std::unique_ptr<C2Work>&)> {
500 auto fillEmptyWork = [](
501 const std::unique_ptr<C2Work> &work, c2_status_t err) {
502 work->result = err;
503 C2FrameData &output = work->worklets.front()->output;
504 output.flags = work->input.flags;
505 output.buffers.clear();
506 output.ordinal = work->input.ordinal;
507
508 work->workletsProcessed = 1u;
509 };
510
511 using namespace std::placeholders;
512 if (numSamples == 0) {
513 return std::bind(fillEmptyWork, _1, C2_OK);
514 }
515
516 // TODO: error handling, proper usage, etc.
517 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
Lajos Molnar268dac82021-06-09 16:25:07 -0700518 size_t bufferSize = numSamples * sizeof(int16_t);
519 c2_status_t err = pool->fetchLinearBlock(bufferSize, usage, &block);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800520 if (err != C2_OK) {
521 ALOGD("failed to fetch a linear block (%d)", err);
522 return std::bind(fillEmptyWork, _1, C2_NO_MEMORY);
523 }
524 C2WriteView wView = block->map().get();
525 // TODO
526 INT_PCM *outBuffer = reinterpret_cast<INT_PCM *>(wView.data());
527 int32_t ns = outputDelayRingBufferGetSamples(outBuffer, numSamples);
528 if (ns != numSamples) {
529 ALOGE("not a complete frame of samples available");
530 mSignalledError = true;
531 return std::bind(fillEmptyWork, _1, C2_CORRUPTED);
532 }
Lajos Molnar268dac82021-06-09 16:25:07 -0700533 return [buffer = createLinearBuffer(block, 0, bufferSize)](
Pawin Vongmasa36653902018-11-15 00:10:25 -0800534 const std::unique_ptr<C2Work> &work) {
535 work->result = C2_OK;
536 C2FrameData &output = work->worklets.front()->output;
537 output.flags = work->input.flags;
538 output.buffers.clear();
539 output.buffers.push_back(buffer);
540 output.ordinal = work->input.ordinal;
541 work->workletsProcessed = 1u;
542 };
543 }();
544
545 if (work && work->input.ordinal.frameIndex == c2_cntr64_t(outInfo.frameIndex)) {
546 fillWork(work);
547 } else {
548 finish(outInfo.frameIndex, fillWork);
549 }
550
551 ALOGV("out timestamp %" PRIu64 " / %u", outInfo.timestamp, block ? block->capacity() : 0);
552 mBuffersInfo.pop_front();
553 }
554}
555
556void C2SoftAacDec::process(
557 const std::unique_ptr<C2Work> &work,
558 const std::shared_ptr<C2BlockPool> &pool) {
559 // Initialize output work
560 work->result = C2_OK;
561 work->workletsProcessed = 1u;
562 work->worklets.front()->output.configUpdate.clear();
563 work->worklets.front()->output.flags = work->input.flags;
564
565 if (mSignalledError) {
566 return;
567 }
568
569 UCHAR* inBuffer[FILEREAD_MAX_LAYERS];
570 UINT inBufferLength[FILEREAD_MAX_LAYERS] = {0};
571 UINT bytesValid[FILEREAD_MAX_LAYERS] = {0};
572
573 INT_PCM tmpOutBuffer[2048 * MAX_CHANNEL_COUNT];
574 C2ReadView view = mDummyReadView;
575 size_t offset = 0u;
576 size_t size = 0u;
577 if (!work->input.buffers.empty()) {
578 view = work->input.buffers[0]->data().linearBlocks().front().map().get();
579 size = view.capacity();
580 }
581
582 bool eos = (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0;
583 bool codecConfig = (work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) != 0;
584
585 //TODO
586#if 0
587 if (mInputBufferCount == 0 && !codecConfig) {
588 ALOGW("first buffer should have FLAG_CODEC_CONFIG set");
589 codecConfig = true;
590 }
591#endif
592 if (codecConfig && size > 0u) {
593 // const_cast because of libAACdec method signature.
594 inBuffer[0] = const_cast<UCHAR *>(view.data() + offset);
595 inBufferLength[0] = size;
596
597 AAC_DECODER_ERROR decoderErr =
598 aacDecoder_ConfigRaw(mAACDecoder,
599 inBuffer,
600 inBufferLength);
601
602 if (decoderErr != AAC_DEC_OK) {
603 ALOGE("aacDecoder_ConfigRaw decoderErr = 0x%4.4x", decoderErr);
604 mSignalledError = true;
605 work->result = C2_CORRUPTED;
606 return;
607 }
608 work->worklets.front()->output.flags = work->input.flags;
609 work->worklets.front()->output.ordinal = work->input.ordinal;
610 work->worklets.front()->output.buffers.clear();
611 return;
612 }
613
614 Info inInfo;
615 inInfo.frameIndex = work->input.ordinal.frameIndex.peeku();
616 inInfo.timestamp = work->input.ordinal.timestamp.peeku();
617 inInfo.bufferSize = size;
618 inInfo.decodedSizes.clear();
619 while (size > 0u) {
620 ALOGV("size = %zu", size);
621 if (mIntf->isAdts()) {
622 size_t adtsHeaderSize = 0;
623 // skip 30 bits, aac_frame_length follows.
624 // ssssssss ssssiiip ppffffPc ccohCCll llllllll lll?????
625
626 const uint8_t *adtsHeader = view.data() + offset;
627
628 bool signalError = false;
629 if (size < 7) {
630 ALOGE("Audio data too short to contain even the ADTS header. "
631 "Got %zu bytes.", size);
632 hexdump(adtsHeader, size);
633 signalError = true;
634 } else {
635 bool protectionAbsent = (adtsHeader[1] & 1);
636
637 unsigned aac_frame_length =
638 ((adtsHeader[3] & 3) << 11)
639 | (adtsHeader[4] << 3)
640 | (adtsHeader[5] >> 5);
641
642 if (size < aac_frame_length) {
643 ALOGE("Not enough audio data for the complete frame. "
644 "Got %zu bytes, frame size according to the ADTS "
645 "header is %u bytes.",
646 size, aac_frame_length);
647 hexdump(adtsHeader, size);
648 signalError = true;
649 } else {
650 adtsHeaderSize = (protectionAbsent ? 7 : 9);
651 if (aac_frame_length < adtsHeaderSize) {
652 signalError = true;
653 } else {
654 // const_cast because of libAACdec method signature.
655 inBuffer[0] = const_cast<UCHAR *>(adtsHeader + adtsHeaderSize);
656 inBufferLength[0] = aac_frame_length - adtsHeaderSize;
657
658 offset += adtsHeaderSize;
659 size -= adtsHeaderSize;
660 }
661 }
662 }
663
664 if (signalError) {
665 mSignalledError = true;
666 work->result = C2_CORRUPTED;
667 return;
668 }
669 } else {
670 // const_cast because of libAACdec method signature.
671 inBuffer[0] = const_cast<UCHAR *>(view.data() + offset);
672 inBufferLength[0] = size;
673 }
674
675 // Fill and decode
676 bytesValid[0] = inBufferLength[0];
677
678 INT prevSampleRate = mStreamInfo->sampleRate;
679 INT prevNumChannels = mStreamInfo->numChannels;
Jean-Michel Trivi4b936cc2020-02-17 16:29:47 -0800680 INT prevOutLoudness = mStreamInfo->outputLoudness;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800681
682 aacDecoder_Fill(mAACDecoder,
683 inBuffer,
684 inBufferLength,
685 bytesValid);
686
687 // run DRC check
688 mDrcWrap.submitStreamData(mStreamInfo);
Jean-Michel Triviedf942b2020-01-29 09:59:44 -0800689
690 // apply runtime updates
691 // DRC_PRES_MODE_WRAP_DESIRED_TARGET
692 int32_t targetRefLevel = mIntf->getDrcTargetRefLevel();
693 ALOGV("AAC decoder using desired DRC target reference level of %d", targetRefLevel);
694 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_TARGET, (unsigned)targetRefLevel);
695
696 // DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR
697 int32_t attenuationFactor = mIntf->getDrcAttenuationFactor();
698 ALOGV("AAC decoder using desired DRC attenuation factor of %d", attenuationFactor);
699 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR, (unsigned)attenuationFactor);
700
701 // DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR
702 int32_t boostFactor = mIntf->getDrcBoostFactor();
703 ALOGV("AAC decoder using desired DRC boost factor of %d", boostFactor);
704 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR, (unsigned)boostFactor);
705
706 // DRC_PRES_MODE_WRAP_DESIRED_HEAVY
707 int32_t compressMode = mIntf->getDrcCompressMode();
708 ALOGV("AAC decoder using desried DRC heavy compression switch of %d", compressMode);
709 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_HEAVY, (unsigned)compressMode);
710
711 // DRC_PRES_MODE_WRAP_ENCODER_TARGET
712 int32_t encTargetLevel = mIntf->getDrcEncTargetLevel();
713 ALOGV("AAC decoder using encoder-side DRC reference level of %d", encTargetLevel);
714 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_ENCODER_TARGET, (unsigned)encTargetLevel);
715
716 // AAC_UNIDRC_SET_EFFECT
717 int32_t effectType = mIntf->getDrcEffectType();
718 ALOGV("AAC decoder using MPEG-D DRC effect type %d", effectType);
719 aacDecoder_SetParam(mAACDecoder, AAC_UNIDRC_SET_EFFECT, effectType);
720
Jean-Michel Trivi670b8fb2020-02-18 07:54:05 -0800721 // AAC_UNIDRC_ALBUM_MODE
722 int32_t albumMode = mIntf->getDrcAlbumMode();
723 ALOGV("AAC decoder using MPEG-D DRC album mode %d", albumMode);
724 aacDecoder_SetParam(mAACDecoder, AAC_UNIDRC_ALBUM_MODE, albumMode);
725
Jean-Michel Trivieba54dc2020-06-10 18:21:56 -0700726 // AAC_PCM_MAX_OUTPUT_CHANNELS
727 int32_t maxChannelCount = mIntf->getMaxChannelCount();
728 ALOGV("AAC decoder using maximum output channel count %d", maxChannelCount);
729 aacDecoder_SetParam(mAACDecoder, AAC_PCM_MAX_OUTPUT_CHANNELS, maxChannelCount);
730
Pawin Vongmasa36653902018-11-15 00:10:25 -0800731 mDrcWrap.update();
732
733 UINT inBufferUsedLength = inBufferLength[0] - bytesValid[0];
734 size -= inBufferUsedLength;
735 offset += inBufferUsedLength;
736
737 AAC_DECODER_ERROR decoderErr;
738 do {
739 if (outputDelayRingBufferSpaceLeft() <
740 (mStreamInfo->frameSize * mStreamInfo->numChannels)) {
741 ALOGV("skipping decode: not enough space left in ringbuffer");
742 // discard buffer
743 size = 0;
744 break;
745 }
746
747 int numConsumed = mStreamInfo->numTotalBytes;
748 decoderErr = aacDecoder_DecodeFrame(mAACDecoder,
749 tmpOutBuffer,
750 2048 * MAX_CHANNEL_COUNT,
751 0 /* flags */);
752
753 numConsumed = mStreamInfo->numTotalBytes - numConsumed;
754
755 if (decoderErr == AAC_DEC_NOT_ENOUGH_BITS) {
756 break;
757 }
758 inInfo.decodedSizes.push_back(numConsumed);
759
760 if (decoderErr != AAC_DEC_OK) {
761 ALOGW("aacDecoder_DecodeFrame decoderErr = 0x%4.4x", decoderErr);
762 }
763
764 if (bytesValid[0] != 0) {
765 ALOGE("bytesValid[0] != 0 should never happen");
766 mSignalledError = true;
767 work->result = C2_CORRUPTED;
768 return;
769 }
770
771 size_t numOutBytes =
772 mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels;
773
774 if (decoderErr == AAC_DEC_OK) {
775 if (!outputDelayRingBufferPutSamples(tmpOutBuffer,
776 mStreamInfo->frameSize * mStreamInfo->numChannels)) {
777 mSignalledError = true;
778 work->result = C2_CORRUPTED;
779 return;
780 }
781 } else {
782 ALOGW("AAC decoder returned error 0x%4.4x, substituting silence", decoderErr);
783
784 memset(tmpOutBuffer, 0, numOutBytes); // TODO: check for overflow
785
786 if (!outputDelayRingBufferPutSamples(tmpOutBuffer,
787 mStreamInfo->frameSize * mStreamInfo->numChannels)) {
788 mSignalledError = true;
789 work->result = C2_CORRUPTED;
790 return;
791 }
792
793 // Discard input buffer.
794 size = 0;
795
796 aacDecoder_SetParam(mAACDecoder, AAC_TPDEC_CLEAR_BUFFER, 1);
797
798 // After an error, replace bufferSize with the sum of the
799 // decodedSizes to resynchronize the in/out lists.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800800 inInfo.bufferSize = std::accumulate(
801 inInfo.decodedSizes.begin(), inInfo.decodedSizes.end(), 0);
802
803 // fall through
804 }
805
806 /*
807 * AAC+/eAAC+ streams can be signalled in two ways: either explicitly
808 * or implicitly, according to MPEG4 spec. AAC+/eAAC+ is a dual
809 * rate system and the sampling rate in the final output is actually
810 * doubled compared with the core AAC decoder sampling rate.
811 *
812 * Explicit signalling is done by explicitly defining SBR audio object
813 * type in the bitstream. Implicit signalling is done by embedding
814 * SBR content in AAC extension payload specific to SBR, and hence
815 * requires an AAC decoder to perform pre-checks on actual audio frames.
816 *
817 * Thus, we could not say for sure whether a stream is
818 * AAC+/eAAC+ until the first data frame is decoded.
819 */
820 if (!mStreamInfo->sampleRate || !mStreamInfo->numChannels) {
821 // if ((mInputBufferCount > 2) && (mOutputBufferCount <= 1)) {
822 ALOGD("Invalid AAC stream");
823 // TODO: notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
824 // mSignalledError = true;
825 // }
826 } else if ((mStreamInfo->sampleRate != prevSampleRate) ||
827 (mStreamInfo->numChannels != prevNumChannels)) {
828 ALOGI("Reconfiguring decoder: %d->%d Hz, %d->%d channels",
829 prevSampleRate, mStreamInfo->sampleRate,
830 prevNumChannels, mStreamInfo->numChannels);
831
832 C2StreamSampleRateInfo::output sampleRateInfo(0u, mStreamInfo->sampleRate);
833 C2StreamChannelCountInfo::output channelCountInfo(0u, mStreamInfo->numChannels);
834 std::vector<std::unique_ptr<C2SettingResult>> failures;
835 c2_status_t err = mIntf->config(
836 { &sampleRateInfo, &channelCountInfo },
837 C2_MAY_BLOCK,
838 &failures);
839 if (err == OK) {
840 // TODO: this does not handle the case where the values are
841 // altered during config.
842 C2FrameData &output = work->worklets.front()->output;
843 output.configUpdate.push_back(C2Param::Copy(sampleRateInfo));
844 output.configUpdate.push_back(C2Param::Copy(channelCountInfo));
845 } else {
846 ALOGE("Config Update failed");
847 mSignalledError = true;
848 work->result = C2_CORRUPTED;
849 return;
850 }
851 }
852 ALOGV("size = %zu", size);
Jean-Michel Trivi4b936cc2020-02-17 16:29:47 -0800853
854 if (mStreamInfo->outputLoudness != prevOutLoudness) {
855 C2StreamDrcOutputLoudnessTuning::output
856 drcOutLoudness(0u, (float) (mStreamInfo->outputLoudness*-0.25));
857
858 std::vector<std::unique_ptr<C2SettingResult>> failures;
859 c2_status_t err = mIntf->config(
860 { &drcOutLoudness },
861 C2_MAY_BLOCK,
862 &failures);
863 if (err == OK) {
864 work->worklets.front()->output.configUpdate.push_back(
865 C2Param::Copy(drcOutLoudness));
866 } else {
867 ALOGE("Getting output loudness failed");
868 }
869 }
Jean-Michel Trivi52ea7282020-06-05 09:54:38 -0700870
871 // update config with values used for decoding:
872 // Album mode, target reference level, DRC effect type, DRC attenuation and boost
873 // factor, DRC compression mode, encoder target level and max channel count
874 // with input values as they were not modified by decoder
875
876 C2StreamDrcAttenuationFactorTuning::input currentAttenuationFactor(0u,
877 (C2FloatValue) (attenuationFactor/127.));
878 work->worklets.front()->output.configUpdate.push_back(
879 C2Param::Copy(currentAttenuationFactor));
880
881 C2StreamDrcBoostFactorTuning::input currentBoostFactor(0u,
882 (C2FloatValue) (boostFactor/127.));
883 work->worklets.front()->output.configUpdate.push_back(
884 C2Param::Copy(currentBoostFactor));
885
Wonsik Kim597435f2020-10-27 17:02:51 -0700886 if (android_get_device_api_level() < __ANDROID_API_S__) {
887 // We used to report DRC compression mode in the output format
888 // in Q and R, but stopped doing that in S
889 C2StreamDrcCompressionModeTuning::input currentCompressMode(0u,
890 (C2Config::drc_compression_mode_t) compressMode);
891 work->worklets.front()->output.configUpdate.push_back(
892 C2Param::Copy(currentCompressMode));
893 }
Wonsik Kim8ec93ab2020-11-13 16:17:04 -0800894
Jean-Michel Trivi52ea7282020-06-05 09:54:38 -0700895 C2StreamDrcEncodedTargetLevelTuning::input currentEncodedTargetLevel(0u,
896 (C2FloatValue) (encTargetLevel*-0.25));
897 work->worklets.front()->output.configUpdate.push_back(
898 C2Param::Copy(currentEncodedTargetLevel));
899
900 C2StreamDrcAlbumModeTuning::input currentAlbumMode(0u,
901 (C2Config::drc_album_mode_t) albumMode);
902 work->worklets.front()->output.configUpdate.push_back(
903 C2Param::Copy(currentAlbumMode));
904
905 C2StreamDrcTargetReferenceLevelTuning::input currentTargetRefLevel(0u,
906 (float) (targetRefLevel*-0.25));
907 work->worklets.front()->output.configUpdate.push_back(
908 C2Param::Copy(currentTargetRefLevel));
909
910 C2StreamDrcEffectTypeTuning::input currentEffectype(0u,
911 (C2Config::drc_effect_type_t) effectType);
912 work->worklets.front()->output.configUpdate.push_back(
913 C2Param::Copy(currentEffectype));
914
915 C2StreamMaxChannelCountInfo::input currentMaxChannelCnt(0u, maxChannelCount);
916 work->worklets.front()->output.configUpdate.push_back(
917 C2Param::Copy(currentMaxChannelCnt));
918
Pawin Vongmasa36653902018-11-15 00:10:25 -0800919 } while (decoderErr == AAC_DEC_OK);
920 }
921
922 int32_t outputDelay = mStreamInfo->outputDelay * mStreamInfo->numChannels;
923
Harish Mahendrakar3c415ec2021-03-22 13:56:52 -0700924 size_t numSamplesInOutput = mStreamInfo->frameSize * mStreamInfo->numChannels;
925 if (numSamplesInOutput > 0) {
926 size_t actualOutputPortDelay = (outputDelay + numSamplesInOutput - 1) / numSamplesInOutput;
927 if (actualOutputPortDelay > mOutputPortDelay) {
928 mOutputPortDelay = actualOutputPortDelay;
929 ALOGV("New Output port delay %zu ", mOutputPortDelay);
930
931 C2PortActualDelayTuning::output outputPortDelay(mOutputPortDelay);
932 std::vector<std::unique_ptr<C2SettingResult>> failures;
933 c2_status_t err =
934 mIntf->config({&outputPortDelay}, C2_MAY_BLOCK, &failures);
935 if (err == OK) {
936 work->worklets.front()->output.configUpdate.push_back(
937 C2Param::Copy(outputPortDelay));
938 } else {
939 ALOGE("Cannot set output delay");
940 mSignalledError = true;
941 work->workletsProcessed = 1u;
942 work->result = C2_CORRUPTED;
943 return;
944 }
945 }
946 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800947 mBuffersInfo.push_back(std::move(inInfo));
948 work->workletsProcessed = 0u;
949 if (!eos && mOutputDelayCompensated < outputDelay) {
950 // discard outputDelay at the beginning
951 int32_t toCompensate = outputDelay - mOutputDelayCompensated;
952 int32_t discard = outputDelayRingBufferSamplesAvailable();
953 if (discard > toCompensate) {
954 discard = toCompensate;
955 }
956 int32_t discarded = outputDelayRingBufferGetSamples(nullptr, discard);
957 mOutputDelayCompensated += discarded;
958 return;
959 }
960
961 if (eos) {
962 drainInternal(DRAIN_COMPONENT_WITH_EOS, pool, work);
963 } else {
964 drainRingBuffer(work, pool, false /* not EOS */);
965 }
966}
967
968c2_status_t C2SoftAacDec::drainInternal(
969 uint32_t drainMode,
970 const std::shared_ptr<C2BlockPool> &pool,
971 const std::unique_ptr<C2Work> &work) {
972 if (drainMode == NO_DRAIN) {
973 ALOGW("drain with NO_DRAIN: no-op");
974 return C2_OK;
975 }
976 if (drainMode == DRAIN_CHAIN) {
977 ALOGW("DRAIN_CHAIN not supported");
978 return C2_OMITTED;
979 }
980
981 bool eos = (drainMode == DRAIN_COMPONENT_WITH_EOS);
982
983 drainDecoder();
984 drainRingBuffer(work, pool, eos);
985
986 if (eos) {
987 auto fillEmptyWork = [](const std::unique_ptr<C2Work> &work) {
988 work->worklets.front()->output.flags = work->input.flags;
989 work->worklets.front()->output.buffers.clear();
990 work->worklets.front()->output.ordinal = work->input.ordinal;
991 work->workletsProcessed = 1u;
992 };
993 while (mBuffersInfo.size() > 1u) {
994 finish(mBuffersInfo.front().frameIndex, fillEmptyWork);
995 mBuffersInfo.pop_front();
996 }
997 if (work && work->workletsProcessed == 0u) {
998 fillEmptyWork(work);
999 }
1000 mBuffersInfo.clear();
1001 }
1002
1003 return C2_OK;
1004}
1005
1006c2_status_t C2SoftAacDec::drain(
1007 uint32_t drainMode,
1008 const std::shared_ptr<C2BlockPool> &pool) {
1009 return drainInternal(drainMode, pool, nullptr);
1010}
1011
1012c2_status_t C2SoftAacDec::onFlush_sm() {
1013 drainDecoder();
1014 mBuffersInfo.clear();
1015
1016 int avail;
1017 while ((avail = outputDelayRingBufferSamplesAvailable()) > 0) {
1018 if (avail > mStreamInfo->frameSize * mStreamInfo->numChannels) {
1019 avail = mStreamInfo->frameSize * mStreamInfo->numChannels;
1020 }
1021 int32_t ns = outputDelayRingBufferGetSamples(nullptr, avail);
1022 if (ns != avail) {
1023 ALOGW("not a complete frame of samples available");
1024 break;
1025 }
1026 }
1027 mOutputDelayRingBufferReadPos = mOutputDelayRingBufferWritePos;
1028
1029 return C2_OK;
1030}
1031
1032void C2SoftAacDec::drainDecoder() {
1033 // flush decoder until outputDelay is compensated
1034 while (mOutputDelayCompensated > 0) {
1035 // a buffer big enough for MAX_CHANNEL_COUNT channels of decoded HE-AAC
1036 INT_PCM tmpOutBuffer[2048 * MAX_CHANNEL_COUNT];
1037
1038 // run DRC check
1039 mDrcWrap.submitStreamData(mStreamInfo);
1040 mDrcWrap.update();
1041
1042 AAC_DECODER_ERROR decoderErr =
1043 aacDecoder_DecodeFrame(mAACDecoder,
1044 tmpOutBuffer,
1045 2048 * MAX_CHANNEL_COUNT,
1046 AACDEC_FLUSH);
1047 if (decoderErr != AAC_DEC_OK) {
1048 ALOGW("aacDecoder_DecodeFrame decoderErr = 0x%4.4x", decoderErr);
1049 }
1050
1051 int32_t tmpOutBufferSamples = mStreamInfo->frameSize * mStreamInfo->numChannels;
1052 if (tmpOutBufferSamples > mOutputDelayCompensated) {
1053 tmpOutBufferSamples = mOutputDelayCompensated;
1054 }
1055 outputDelayRingBufferPutSamples(tmpOutBuffer, tmpOutBufferSamples);
1056
1057 mOutputDelayCompensated -= tmpOutBufferSamples;
1058 }
1059}
1060
1061class C2SoftAacDecFactory : public C2ComponentFactory {
1062public:
1063 C2SoftAacDecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
1064 GetCodec2PlatformComponentStore()->getParamReflector())) {
1065 }
1066
1067 virtual c2_status_t createComponent(
1068 c2_node_id_t id,
1069 std::shared_ptr<C2Component>* const component,
1070 std::function<void(C2Component*)> deleter) override {
1071 *component = std::shared_ptr<C2Component>(
1072 new C2SoftAacDec(COMPONENT_NAME,
1073 id,
1074 std::make_shared<C2SoftAacDec::IntfImpl>(mHelper)),
1075 deleter);
1076 return C2_OK;
1077 }
1078
1079 virtual c2_status_t createInterface(
1080 c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface,
1081 std::function<void(C2ComponentInterface*)> deleter) override {
1082 *interface = std::shared_ptr<C2ComponentInterface>(
1083 new SimpleInterface<C2SoftAacDec::IntfImpl>(
1084 COMPONENT_NAME, id, std::make_shared<C2SoftAacDec::IntfImpl>(mHelper)),
1085 deleter);
1086 return C2_OK;
1087 }
1088
1089 virtual ~C2SoftAacDecFactory() override = default;
1090
1091private:
1092 std::shared_ptr<C2ReflectorHelper> mHelper;
1093};
1094
1095} // namespace android
1096
Cindy Zhouf6c0c3c2020-12-02 10:53:40 -08001097__attribute__((cfi_canonical_jump_table))
Pawin Vongmasa36653902018-11-15 00:10:25 -08001098extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
1099 ALOGV("in %s", __func__);
1100 return new ::android::C2SoftAacDecFactory();
1101}
1102
Cindy Zhouf6c0c3c2020-12-02 10:53:40 -08001103__attribute__((cfi_canonical_jump_table))
Pawin Vongmasa36653902018-11-15 00:10:25 -08001104extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
1105 ALOGV("in %s", __func__);
1106 delete factory;
1107}