blob: 4e4a9a1164eaa37999e99c9684d469a79bcf9c6e [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());
Jean-Michel Trivi3d476d12022-05-19 17:14:18 +0000224
225 addParameter(DefineParam(mChannelMask, C2_PARAMKEY_CHANNEL_MASK)
226 .withDefault(new C2StreamChannelMaskInfo::output(0u, 0))
227 .withFields({C2F(mChannelMask, value).inRange(0, 4294967292)})
228 .withSetter(Setter<decltype(*mChannelMask)>::StrictValueWithNoDeps)
229 .build());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800230 }
231
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800232 bool isAdts() const { return mAacFormat->value == C2Config::AAC_PACKAGING_ADTS; }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800233 static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::input> &me) {
234 (void)mayBlock;
235 (void)me; // TODO: validate
236 return C2R::Ok();
237 }
238 int32_t getDrcCompressMode() const { return mDrcCompressMode->value == C2Config::DRC_COMPRESSION_HEAVY ? 1 : 0; }
239 int32_t getDrcTargetRefLevel() const { return (mDrcTargetRefLevel->value <= 0 ? -mDrcTargetRefLevel->value * 4. + 0.5 : -1); }
240 int32_t getDrcEncTargetLevel() const { return (mDrcEncTargetLevel->value <= 0 ? -mDrcEncTargetLevel->value * 4. + 0.5 : -1); }
241 int32_t getDrcBoostFactor() const { return mDrcBoostFactor->value * 127. + 0.5; }
242 int32_t getDrcAttenuationFactor() const { return mDrcAttenuationFactor->value * 127. + 0.5; }
243 int32_t getDrcEffectType() const { return mDrcEffectType->value; }
Jean-Michel Trivi670b8fb2020-02-18 07:54:05 -0800244 int32_t getDrcAlbumMode() const { return mDrcAlbumMode->value; }
Jean-Michel Trivieba54dc2020-06-10 18:21:56 -0700245 u_int32_t getMaxChannelCount() const { return mMaxChannelCount->value; }
Jean-Michel Trivi4b936cc2020-02-17 16:29:47 -0800246 int32_t getDrcOutputLoudness() const { return (mDrcOutputLoudness->value <= 0 ? -mDrcOutputLoudness->value * 4. + 0.5 : -1); }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800247
248private:
Pawin Vongmasa36653902018-11-15 00:10:25 -0800249 std::shared_ptr<C2StreamSampleRateInfo::output> mSampleRate;
250 std::shared_ptr<C2StreamChannelCountInfo::output> mChannelCount;
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800251 std::shared_ptr<C2StreamBitrateInfo::input> mBitrate;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800252 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
253 std::shared_ptr<C2StreamAacFormatInfo::input> mAacFormat;
254 std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel;
255 std::shared_ptr<C2StreamDrcCompressionModeTuning::input> mDrcCompressMode;
256 std::shared_ptr<C2StreamDrcTargetReferenceLevelTuning::input> mDrcTargetRefLevel;
257 std::shared_ptr<C2StreamDrcEncodedTargetLevelTuning::input> mDrcEncTargetLevel;
258 std::shared_ptr<C2StreamDrcBoostFactorTuning::input> mDrcBoostFactor;
259 std::shared_ptr<C2StreamDrcAttenuationFactorTuning::input> mDrcAttenuationFactor;
260 std::shared_ptr<C2StreamDrcEffectTypeTuning::input> mDrcEffectType;
Jean-Michel Trivi670b8fb2020-02-18 07:54:05 -0800261 std::shared_ptr<C2StreamDrcAlbumModeTuning::input> mDrcAlbumMode;
Jean-Michel Trivieba54dc2020-06-10 18:21:56 -0700262 std::shared_ptr<C2StreamMaxChannelCountInfo::input> mMaxChannelCount;
Jean-Michel Trivi4b936cc2020-02-17 16:29:47 -0800263 std::shared_ptr<C2StreamDrcOutputLoudnessTuning::output> mDrcOutputLoudness;
Jean-Michel Trivi3d476d12022-05-19 17:14:18 +0000264 std::shared_ptr<C2StreamChannelMaskInfo::output> mChannelMask;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800265 // TODO Add : C2StreamAacSbrModeTuning
266};
267
Pawin Vongmasa36653902018-11-15 00:10:25 -0800268C2SoftAacDec::C2SoftAacDec(
269 const char *name,
270 c2_node_id_t id,
271 const std::shared_ptr<IntfImpl> &intfImpl)
272 : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
273 mIntf(intfImpl),
274 mAACDecoder(nullptr),
275 mStreamInfo(nullptr),
276 mSignalledError(false),
Harish Mahendrakar3c415ec2021-03-22 13:56:52 -0700277 mOutputPortDelay(kDefaultOutputPortDelay),
Pawin Vongmasa36653902018-11-15 00:10:25 -0800278 mOutputDelayRingBuffer(nullptr) {
279}
280
281C2SoftAacDec::~C2SoftAacDec() {
282 onRelease();
283}
284
285c2_status_t C2SoftAacDec::onInit() {
286 status_t err = initDecoder();
287 return err == OK ? C2_OK : C2_CORRUPTED;
288}
289
290c2_status_t C2SoftAacDec::onStop() {
291 drainDecoder();
292 // reset the "configured" state
293 mOutputDelayCompensated = 0;
294 mOutputDelayRingBufferWritePos = 0;
295 mOutputDelayRingBufferReadPos = 0;
296 mOutputDelayRingBufferFilled = 0;
Wonsik Kim70982a62021-08-26 16:32:35 -0700297 mOutputDelayRingBuffer.reset();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800298 mBuffersInfo.clear();
299
Rakesh Kumar87604ef2021-05-06 18:28:37 +0530300 status_t status = UNKNOWN_ERROR;
301 if (mAACDecoder) {
302 aacDecoder_Close(mAACDecoder);
303 status = initDecoder();
304 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800305 mSignalledError = false;
306
Rakesh Kumar87604ef2021-05-06 18:28:37 +0530307 return status == OK ? C2_OK : C2_CORRUPTED;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800308}
309
310void C2SoftAacDec::onReset() {
311 (void)onStop();
312}
313
314void C2SoftAacDec::onRelease() {
315 if (mAACDecoder) {
316 aacDecoder_Close(mAACDecoder);
317 mAACDecoder = nullptr;
318 }
Wonsik Kim70982a62021-08-26 16:32:35 -0700319 mOutputDelayRingBuffer.reset();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800320}
321
322status_t C2SoftAacDec::initDecoder() {
323 ALOGV("initDecoder()");
324 status_t status = UNKNOWN_ERROR;
325 mAACDecoder = aacDecoder_Open(TT_MP4_ADIF, /* num layers */ 1);
326 if (mAACDecoder != nullptr) {
327 mStreamInfo = aacDecoder_GetStreamInfo(mAACDecoder);
328 if (mStreamInfo != nullptr) {
329 status = OK;
330 }
331 }
332
333 mOutputDelayCompensated = 0;
334 mOutputDelayRingBufferSize = 2048 * MAX_CHANNEL_COUNT * kNumDelayBlocksMax;
Wonsik Kim70982a62021-08-26 16:32:35 -0700335 mOutputDelayRingBuffer.reset(new short[mOutputDelayRingBufferSize]);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800336 mOutputDelayRingBufferWritePos = 0;
337 mOutputDelayRingBufferReadPos = 0;
338 mOutputDelayRingBufferFilled = 0;
339
340 if (mAACDecoder == nullptr) {
341 ALOGE("AAC decoder is null. TODO: Can not call aacDecoder_SetParam in the following code");
342 }
343
344 //aacDecoder_SetParam(mAACDecoder, AAC_PCM_LIMITER_ENABLE, 0);
345
346 //init DRC wrapper
347 mDrcWrap.setDecoderHandle(mAACDecoder);
348 mDrcWrap.submitStreamData(mStreamInfo);
349
350 // for streams that contain metadata, use the mobile profile DRC settings unless overridden by platform properties
351 // TODO: change the DRC settings depending on audio output device type (HDMI, loadspeaker, headphone)
352
353 // DRC_PRES_MODE_WRAP_DESIRED_TARGET
354 int32_t targetRefLevel = mIntf->getDrcTargetRefLevel();
355 ALOGV("AAC decoder using desired DRC target reference level of %d", targetRefLevel);
356 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_TARGET, (unsigned)targetRefLevel);
357
358 // DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR
359
360 int32_t attenuationFactor = mIntf->getDrcAttenuationFactor();
361 ALOGV("AAC decoder using desired DRC attenuation factor of %d", attenuationFactor);
362 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR, (unsigned)attenuationFactor);
363
364 // DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR
365 int32_t boostFactor = mIntf->getDrcBoostFactor();
366 ALOGV("AAC decoder using desired DRC boost factor of %d", boostFactor);
367 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR, (unsigned)boostFactor);
368
369 // DRC_PRES_MODE_WRAP_DESIRED_HEAVY
370 int32_t compressMode = mIntf->getDrcCompressMode();
Jean-Michel Triviedf942b2020-01-29 09:59:44 -0800371 ALOGV("AAC decoder using desired DRC heavy compression switch of %d", compressMode);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800372 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_HEAVY, (unsigned)compressMode);
373
374 // DRC_PRES_MODE_WRAP_ENCODER_TARGET
375 int32_t encTargetLevel = mIntf->getDrcEncTargetLevel();
376 ALOGV("AAC decoder using encoder-side DRC reference level of %d", encTargetLevel);
377 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_ENCODER_TARGET, (unsigned)encTargetLevel);
378
379 // AAC_UNIDRC_SET_EFFECT
380 int32_t effectType = mIntf->getDrcEffectType();
381 ALOGV("AAC decoder using MPEG-D DRC effect type %d", effectType);
382 aacDecoder_SetParam(mAACDecoder, AAC_UNIDRC_SET_EFFECT, effectType);
383
Jean-Michel Trivi670b8fb2020-02-18 07:54:05 -0800384 // AAC_UNIDRC_ALBUM_MODE
385 int32_t albumMode = mIntf->getDrcAlbumMode();
386 ALOGV("AAC decoder using MPEG-D DRC album mode %d", albumMode);
387 aacDecoder_SetParam(mAACDecoder, AAC_UNIDRC_ALBUM_MODE, albumMode);
388
Jean-Michel Trivieba54dc2020-06-10 18:21:56 -0700389 // AAC_PCM_MAX_OUTPUT_CHANNELS
390 u_int32_t maxChannelCount = mIntf->getMaxChannelCount();
391 ALOGV("AAC decoder using maximum output channel count %d", maxChannelCount);
392 aacDecoder_SetParam(mAACDecoder, AAC_PCM_MAX_OUTPUT_CHANNELS, maxChannelCount);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800393
394 return status;
395}
396
397bool C2SoftAacDec::outputDelayRingBufferPutSamples(INT_PCM *samples, int32_t numSamples) {
398 if (numSamples == 0) {
399 return true;
400 }
401 if (outputDelayRingBufferSpaceLeft() < numSamples) {
402 ALOGE("RING BUFFER WOULD OVERFLOW");
403 return false;
404 }
405 if (mOutputDelayRingBufferWritePos + numSamples <= mOutputDelayRingBufferSize
406 && (mOutputDelayRingBufferReadPos <= mOutputDelayRingBufferWritePos
407 || mOutputDelayRingBufferReadPos > mOutputDelayRingBufferWritePos + numSamples)) {
408 // faster memcopy loop without checks, if the preconditions allow this
409 for (int32_t i = 0; i < numSamples; i++) {
410 mOutputDelayRingBuffer[mOutputDelayRingBufferWritePos++] = samples[i];
411 }
412
413 if (mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferSize) {
414 mOutputDelayRingBufferWritePos -= mOutputDelayRingBufferSize;
415 }
416 } else {
417 ALOGV("slow C2SoftAacDec::outputDelayRingBufferPutSamples()");
418
419 for (int32_t i = 0; i < numSamples; i++) {
420 mOutputDelayRingBuffer[mOutputDelayRingBufferWritePos] = samples[i];
421 mOutputDelayRingBufferWritePos++;
422 if (mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferSize) {
423 mOutputDelayRingBufferWritePos -= mOutputDelayRingBufferSize;
424 }
425 }
426 }
427 mOutputDelayRingBufferFilled += numSamples;
428 return true;
429}
430
431int32_t C2SoftAacDec::outputDelayRingBufferGetSamples(INT_PCM *samples, int32_t numSamples) {
432
433 if (numSamples > mOutputDelayRingBufferFilled) {
434 ALOGE("RING BUFFER WOULD UNDERRUN");
435 return -1;
436 }
437
438 if (mOutputDelayRingBufferReadPos + numSamples <= mOutputDelayRingBufferSize
439 && (mOutputDelayRingBufferWritePos < mOutputDelayRingBufferReadPos
440 || mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferReadPos + numSamples)) {
441 // faster memcopy loop without checks, if the preconditions allow this
442 if (samples != nullptr) {
443 for (int32_t i = 0; i < numSamples; i++) {
444 samples[i] = mOutputDelayRingBuffer[mOutputDelayRingBufferReadPos++];
445 }
446 } else {
447 mOutputDelayRingBufferReadPos += numSamples;
448 }
449 if (mOutputDelayRingBufferReadPos >= mOutputDelayRingBufferSize) {
450 mOutputDelayRingBufferReadPos -= mOutputDelayRingBufferSize;
451 }
452 } else {
453 ALOGV("slow C2SoftAacDec::outputDelayRingBufferGetSamples()");
454
455 for (int32_t i = 0; i < numSamples; i++) {
456 if (samples != nullptr) {
457 samples[i] = mOutputDelayRingBuffer[mOutputDelayRingBufferReadPos];
458 }
459 mOutputDelayRingBufferReadPos++;
460 if (mOutputDelayRingBufferReadPos >= mOutputDelayRingBufferSize) {
461 mOutputDelayRingBufferReadPos -= mOutputDelayRingBufferSize;
462 }
463 }
464 }
465 mOutputDelayRingBufferFilled -= numSamples;
466 return numSamples;
467}
468
469int32_t C2SoftAacDec::outputDelayRingBufferSamplesAvailable() {
470 return mOutputDelayRingBufferFilled;
471}
472
473int32_t C2SoftAacDec::outputDelayRingBufferSpaceLeft() {
474 return mOutputDelayRingBufferSize - outputDelayRingBufferSamplesAvailable();
475}
476
477void C2SoftAacDec::drainRingBuffer(
478 const std::unique_ptr<C2Work> &work,
479 const std::shared_ptr<C2BlockPool> &pool,
480 bool eos) {
481 while (!mBuffersInfo.empty() && outputDelayRingBufferSamplesAvailable()
482 >= mStreamInfo->frameSize * mStreamInfo->numChannels) {
483 Info &outInfo = mBuffersInfo.front();
484 ALOGV("outInfo.frameIndex = %" PRIu64, outInfo.frameIndex);
485 int samplesize __unused = mStreamInfo->numChannels * sizeof(int16_t);
486
487 int available = outputDelayRingBufferSamplesAvailable();
488 int numFrames = outInfo.decodedSizes.size();
489 int numSamples = numFrames * (mStreamInfo->frameSize * mStreamInfo->numChannels);
490 if (available < numSamples) {
491 if (eos) {
492 numSamples = available;
493 } else {
494 break;
495 }
496 }
497 ALOGV("%d samples available (%d), or %d frames",
498 numSamples, available, numFrames);
499 ALOGV("getting %d from ringbuffer", numSamples);
500
501 std::shared_ptr<C2LinearBlock> block;
502 std::function<void(const std::unique_ptr<C2Work>&)> fillWork =
503 [&block, numSamples, pool, this]()
504 -> std::function<void(const std::unique_ptr<C2Work>&)> {
505 auto fillEmptyWork = [](
506 const std::unique_ptr<C2Work> &work, c2_status_t err) {
507 work->result = err;
508 C2FrameData &output = work->worklets.front()->output;
509 output.flags = work->input.flags;
510 output.buffers.clear();
511 output.ordinal = work->input.ordinal;
512
513 work->workletsProcessed = 1u;
514 };
515
516 using namespace std::placeholders;
517 if (numSamples == 0) {
518 return std::bind(fillEmptyWork, _1, C2_OK);
519 }
520
521 // TODO: error handling, proper usage, etc.
522 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
Lajos Molnar268dac82021-06-09 16:25:07 -0700523 size_t bufferSize = numSamples * sizeof(int16_t);
524 c2_status_t err = pool->fetchLinearBlock(bufferSize, usage, &block);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800525 if (err != C2_OK) {
526 ALOGD("failed to fetch a linear block (%d)", err);
527 return std::bind(fillEmptyWork, _1, C2_NO_MEMORY);
528 }
529 C2WriteView wView = block->map().get();
530 // TODO
531 INT_PCM *outBuffer = reinterpret_cast<INT_PCM *>(wView.data());
532 int32_t ns = outputDelayRingBufferGetSamples(outBuffer, numSamples);
533 if (ns != numSamples) {
534 ALOGE("not a complete frame of samples available");
535 mSignalledError = true;
536 return std::bind(fillEmptyWork, _1, C2_CORRUPTED);
537 }
Lajos Molnar268dac82021-06-09 16:25:07 -0700538 return [buffer = createLinearBuffer(block, 0, bufferSize)](
Pawin Vongmasa36653902018-11-15 00:10:25 -0800539 const std::unique_ptr<C2Work> &work) {
540 work->result = C2_OK;
541 C2FrameData &output = work->worklets.front()->output;
542 output.flags = work->input.flags;
543 output.buffers.clear();
544 output.buffers.push_back(buffer);
545 output.ordinal = work->input.ordinal;
546 work->workletsProcessed = 1u;
547 };
548 }();
549
550 if (work && work->input.ordinal.frameIndex == c2_cntr64_t(outInfo.frameIndex)) {
551 fillWork(work);
552 } else {
553 finish(outInfo.frameIndex, fillWork);
554 }
555
556 ALOGV("out timestamp %" PRIu64 " / %u", outInfo.timestamp, block ? block->capacity() : 0);
557 mBuffersInfo.pop_front();
558 }
559}
560
561void C2SoftAacDec::process(
562 const std::unique_ptr<C2Work> &work,
563 const std::shared_ptr<C2BlockPool> &pool) {
564 // Initialize output work
565 work->result = C2_OK;
566 work->workletsProcessed = 1u;
567 work->worklets.front()->output.configUpdate.clear();
568 work->worklets.front()->output.flags = work->input.flags;
569
570 if (mSignalledError) {
571 return;
572 }
573
574 UCHAR* inBuffer[FILEREAD_MAX_LAYERS];
575 UINT inBufferLength[FILEREAD_MAX_LAYERS] = {0};
576 UINT bytesValid[FILEREAD_MAX_LAYERS] = {0};
577
578 INT_PCM tmpOutBuffer[2048 * MAX_CHANNEL_COUNT];
579 C2ReadView view = mDummyReadView;
580 size_t offset = 0u;
581 size_t size = 0u;
582 if (!work->input.buffers.empty()) {
583 view = work->input.buffers[0]->data().linearBlocks().front().map().get();
584 size = view.capacity();
585 }
586
587 bool eos = (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0;
588 bool codecConfig = (work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) != 0;
589
590 //TODO
591#if 0
592 if (mInputBufferCount == 0 && !codecConfig) {
593 ALOGW("first buffer should have FLAG_CODEC_CONFIG set");
594 codecConfig = true;
595 }
596#endif
597 if (codecConfig && size > 0u) {
598 // const_cast because of libAACdec method signature.
599 inBuffer[0] = const_cast<UCHAR *>(view.data() + offset);
600 inBufferLength[0] = size;
601
602 AAC_DECODER_ERROR decoderErr =
603 aacDecoder_ConfigRaw(mAACDecoder,
604 inBuffer,
605 inBufferLength);
606
607 if (decoderErr != AAC_DEC_OK) {
608 ALOGE("aacDecoder_ConfigRaw decoderErr = 0x%4.4x", decoderErr);
609 mSignalledError = true;
610 work->result = C2_CORRUPTED;
611 return;
612 }
613 work->worklets.front()->output.flags = work->input.flags;
614 work->worklets.front()->output.ordinal = work->input.ordinal;
615 work->worklets.front()->output.buffers.clear();
616 return;
617 }
618
619 Info inInfo;
620 inInfo.frameIndex = work->input.ordinal.frameIndex.peeku();
621 inInfo.timestamp = work->input.ordinal.timestamp.peeku();
622 inInfo.bufferSize = size;
623 inInfo.decodedSizes.clear();
624 while (size > 0u) {
625 ALOGV("size = %zu", size);
626 if (mIntf->isAdts()) {
627 size_t adtsHeaderSize = 0;
628 // skip 30 bits, aac_frame_length follows.
629 // ssssssss ssssiiip ppffffPc ccohCCll llllllll lll?????
630
631 const uint8_t *adtsHeader = view.data() + offset;
632
633 bool signalError = false;
634 if (size < 7) {
635 ALOGE("Audio data too short to contain even the ADTS header. "
636 "Got %zu bytes.", size);
637 hexdump(adtsHeader, size);
638 signalError = true;
639 } else {
640 bool protectionAbsent = (adtsHeader[1] & 1);
641
642 unsigned aac_frame_length =
643 ((adtsHeader[3] & 3) << 11)
644 | (adtsHeader[4] << 3)
645 | (adtsHeader[5] >> 5);
646
647 if (size < aac_frame_length) {
648 ALOGE("Not enough audio data for the complete frame. "
649 "Got %zu bytes, frame size according to the ADTS "
650 "header is %u bytes.",
651 size, aac_frame_length);
652 hexdump(adtsHeader, size);
653 signalError = true;
654 } else {
655 adtsHeaderSize = (protectionAbsent ? 7 : 9);
656 if (aac_frame_length < adtsHeaderSize) {
657 signalError = true;
658 } else {
659 // const_cast because of libAACdec method signature.
660 inBuffer[0] = const_cast<UCHAR *>(adtsHeader + adtsHeaderSize);
661 inBufferLength[0] = aac_frame_length - adtsHeaderSize;
662
663 offset += adtsHeaderSize;
664 size -= adtsHeaderSize;
665 }
666 }
667 }
668
669 if (signalError) {
670 mSignalledError = true;
671 work->result = C2_CORRUPTED;
672 return;
673 }
674 } else {
675 // const_cast because of libAACdec method signature.
676 inBuffer[0] = const_cast<UCHAR *>(view.data() + offset);
677 inBufferLength[0] = size;
678 }
679
680 // Fill and decode
681 bytesValid[0] = inBufferLength[0];
682
683 INT prevSampleRate = mStreamInfo->sampleRate;
684 INT prevNumChannels = mStreamInfo->numChannels;
Jean-Michel Trivi4b936cc2020-02-17 16:29:47 -0800685 INT prevOutLoudness = mStreamInfo->outputLoudness;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800686
687 aacDecoder_Fill(mAACDecoder,
688 inBuffer,
689 inBufferLength,
690 bytesValid);
691
692 // run DRC check
693 mDrcWrap.submitStreamData(mStreamInfo);
Jean-Michel Triviedf942b2020-01-29 09:59:44 -0800694
695 // apply runtime updates
696 // DRC_PRES_MODE_WRAP_DESIRED_TARGET
697 int32_t targetRefLevel = mIntf->getDrcTargetRefLevel();
698 ALOGV("AAC decoder using desired DRC target reference level of %d", targetRefLevel);
699 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_TARGET, (unsigned)targetRefLevel);
700
701 // DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR
702 int32_t attenuationFactor = mIntf->getDrcAttenuationFactor();
703 ALOGV("AAC decoder using desired DRC attenuation factor of %d", attenuationFactor);
704 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR, (unsigned)attenuationFactor);
705
706 // DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR
707 int32_t boostFactor = mIntf->getDrcBoostFactor();
708 ALOGV("AAC decoder using desired DRC boost factor of %d", boostFactor);
709 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR, (unsigned)boostFactor);
710
711 // DRC_PRES_MODE_WRAP_DESIRED_HEAVY
712 int32_t compressMode = mIntf->getDrcCompressMode();
713 ALOGV("AAC decoder using desried DRC heavy compression switch of %d", compressMode);
714 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_HEAVY, (unsigned)compressMode);
715
716 // DRC_PRES_MODE_WRAP_ENCODER_TARGET
717 int32_t encTargetLevel = mIntf->getDrcEncTargetLevel();
718 ALOGV("AAC decoder using encoder-side DRC reference level of %d", encTargetLevel);
719 mDrcWrap.setParam(DRC_PRES_MODE_WRAP_ENCODER_TARGET, (unsigned)encTargetLevel);
720
721 // AAC_UNIDRC_SET_EFFECT
722 int32_t effectType = mIntf->getDrcEffectType();
723 ALOGV("AAC decoder using MPEG-D DRC effect type %d", effectType);
724 aacDecoder_SetParam(mAACDecoder, AAC_UNIDRC_SET_EFFECT, effectType);
725
Jean-Michel Trivi670b8fb2020-02-18 07:54:05 -0800726 // AAC_UNIDRC_ALBUM_MODE
727 int32_t albumMode = mIntf->getDrcAlbumMode();
728 ALOGV("AAC decoder using MPEG-D DRC album mode %d", albumMode);
729 aacDecoder_SetParam(mAACDecoder, AAC_UNIDRC_ALBUM_MODE, albumMode);
730
Jean-Michel Trivieba54dc2020-06-10 18:21:56 -0700731 // AAC_PCM_MAX_OUTPUT_CHANNELS
732 int32_t maxChannelCount = mIntf->getMaxChannelCount();
733 ALOGV("AAC decoder using maximum output channel count %d", maxChannelCount);
734 aacDecoder_SetParam(mAACDecoder, AAC_PCM_MAX_OUTPUT_CHANNELS, maxChannelCount);
735
Pawin Vongmasa36653902018-11-15 00:10:25 -0800736 mDrcWrap.update();
737
738 UINT inBufferUsedLength = inBufferLength[0] - bytesValid[0];
739 size -= inBufferUsedLength;
740 offset += inBufferUsedLength;
741
742 AAC_DECODER_ERROR decoderErr;
743 do {
744 if (outputDelayRingBufferSpaceLeft() <
745 (mStreamInfo->frameSize * mStreamInfo->numChannels)) {
746 ALOGV("skipping decode: not enough space left in ringbuffer");
747 // discard buffer
748 size = 0;
749 break;
750 }
751
752 int numConsumed = mStreamInfo->numTotalBytes;
753 decoderErr = aacDecoder_DecodeFrame(mAACDecoder,
754 tmpOutBuffer,
755 2048 * MAX_CHANNEL_COUNT,
756 0 /* flags */);
757
758 numConsumed = mStreamInfo->numTotalBytes - numConsumed;
759
760 if (decoderErr == AAC_DEC_NOT_ENOUGH_BITS) {
761 break;
762 }
763 inInfo.decodedSizes.push_back(numConsumed);
764
765 if (decoderErr != AAC_DEC_OK) {
766 ALOGW("aacDecoder_DecodeFrame decoderErr = 0x%4.4x", decoderErr);
767 }
768
769 if (bytesValid[0] != 0) {
770 ALOGE("bytesValid[0] != 0 should never happen");
771 mSignalledError = true;
772 work->result = C2_CORRUPTED;
773 return;
774 }
775
776 size_t numOutBytes =
777 mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels;
778
779 if (decoderErr == AAC_DEC_OK) {
780 if (!outputDelayRingBufferPutSamples(tmpOutBuffer,
781 mStreamInfo->frameSize * mStreamInfo->numChannels)) {
782 mSignalledError = true;
783 work->result = C2_CORRUPTED;
784 return;
785 }
786 } else {
787 ALOGW("AAC decoder returned error 0x%4.4x, substituting silence", decoderErr);
788
789 memset(tmpOutBuffer, 0, numOutBytes); // TODO: check for overflow
790
791 if (!outputDelayRingBufferPutSamples(tmpOutBuffer,
792 mStreamInfo->frameSize * mStreamInfo->numChannels)) {
793 mSignalledError = true;
794 work->result = C2_CORRUPTED;
795 return;
796 }
797
798 // Discard input buffer.
799 size = 0;
800
801 aacDecoder_SetParam(mAACDecoder, AAC_TPDEC_CLEAR_BUFFER, 1);
802
803 // After an error, replace bufferSize with the sum of the
804 // decodedSizes to resynchronize the in/out lists.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800805 inInfo.bufferSize = std::accumulate(
806 inInfo.decodedSizes.begin(), inInfo.decodedSizes.end(), 0);
807
808 // fall through
809 }
810
811 /*
812 * AAC+/eAAC+ streams can be signalled in two ways: either explicitly
813 * or implicitly, according to MPEG4 spec. AAC+/eAAC+ is a dual
814 * rate system and the sampling rate in the final output is actually
815 * doubled compared with the core AAC decoder sampling rate.
816 *
817 * Explicit signalling is done by explicitly defining SBR audio object
818 * type in the bitstream. Implicit signalling is done by embedding
819 * SBR content in AAC extension payload specific to SBR, and hence
820 * requires an AAC decoder to perform pre-checks on actual audio frames.
821 *
822 * Thus, we could not say for sure whether a stream is
823 * AAC+/eAAC+ until the first data frame is decoded.
824 */
825 if (!mStreamInfo->sampleRate || !mStreamInfo->numChannels) {
826 // if ((mInputBufferCount > 2) && (mOutputBufferCount <= 1)) {
827 ALOGD("Invalid AAC stream");
828 // TODO: notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
829 // mSignalledError = true;
830 // }
831 } else if ((mStreamInfo->sampleRate != prevSampleRate) ||
832 (mStreamInfo->numChannels != prevNumChannels)) {
833 ALOGI("Reconfiguring decoder: %d->%d Hz, %d->%d channels",
834 prevSampleRate, mStreamInfo->sampleRate,
835 prevNumChannels, mStreamInfo->numChannels);
836
837 C2StreamSampleRateInfo::output sampleRateInfo(0u, mStreamInfo->sampleRate);
838 C2StreamChannelCountInfo::output channelCountInfo(0u, mStreamInfo->numChannels);
Jean-Michel Trivi3d476d12022-05-19 17:14:18 +0000839 C2StreamChannelMaskInfo::output channelMaskInfo(0u,
840 maskFromCount(mStreamInfo->numChannels));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800841 std::vector<std::unique_ptr<C2SettingResult>> failures;
842 c2_status_t err = mIntf->config(
Jean-Michel Trivi3d476d12022-05-19 17:14:18 +0000843 { &sampleRateInfo, &channelCountInfo, &channelMaskInfo },
Pawin Vongmasa36653902018-11-15 00:10:25 -0800844 C2_MAY_BLOCK,
845 &failures);
846 if (err == OK) {
847 // TODO: this does not handle the case where the values are
848 // altered during config.
849 C2FrameData &output = work->worklets.front()->output;
850 output.configUpdate.push_back(C2Param::Copy(sampleRateInfo));
851 output.configUpdate.push_back(C2Param::Copy(channelCountInfo));
Jean-Michel Trivi3d476d12022-05-19 17:14:18 +0000852 output.configUpdate.push_back(C2Param::Copy(channelMaskInfo));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800853 } else {
854 ALOGE("Config Update failed");
855 mSignalledError = true;
856 work->result = C2_CORRUPTED;
857 return;
858 }
859 }
860 ALOGV("size = %zu", size);
Jean-Michel Trivi4b936cc2020-02-17 16:29:47 -0800861
862 if (mStreamInfo->outputLoudness != prevOutLoudness) {
863 C2StreamDrcOutputLoudnessTuning::output
864 drcOutLoudness(0u, (float) (mStreamInfo->outputLoudness*-0.25));
865
866 std::vector<std::unique_ptr<C2SettingResult>> failures;
867 c2_status_t err = mIntf->config(
868 { &drcOutLoudness },
869 C2_MAY_BLOCK,
870 &failures);
871 if (err == OK) {
872 work->worklets.front()->output.configUpdate.push_back(
873 C2Param::Copy(drcOutLoudness));
874 } else {
875 ALOGE("Getting output loudness failed");
876 }
877 }
Jean-Michel Trivi52ea7282020-06-05 09:54:38 -0700878
879 // update config with values used for decoding:
880 // Album mode, target reference level, DRC effect type, DRC attenuation and boost
881 // factor, DRC compression mode, encoder target level and max channel count
882 // with input values as they were not modified by decoder
883
884 C2StreamDrcAttenuationFactorTuning::input currentAttenuationFactor(0u,
885 (C2FloatValue) (attenuationFactor/127.));
886 work->worklets.front()->output.configUpdate.push_back(
887 C2Param::Copy(currentAttenuationFactor));
888
889 C2StreamDrcBoostFactorTuning::input currentBoostFactor(0u,
890 (C2FloatValue) (boostFactor/127.));
891 work->worklets.front()->output.configUpdate.push_back(
892 C2Param::Copy(currentBoostFactor));
893
Wonsik Kim597435f2020-10-27 17:02:51 -0700894 if (android_get_device_api_level() < __ANDROID_API_S__) {
895 // We used to report DRC compression mode in the output format
896 // in Q and R, but stopped doing that in S
897 C2StreamDrcCompressionModeTuning::input currentCompressMode(0u,
898 (C2Config::drc_compression_mode_t) compressMode);
899 work->worklets.front()->output.configUpdate.push_back(
900 C2Param::Copy(currentCompressMode));
901 }
Wonsik Kim8ec93ab2020-11-13 16:17:04 -0800902
Jean-Michel Trivi52ea7282020-06-05 09:54:38 -0700903 C2StreamDrcEncodedTargetLevelTuning::input currentEncodedTargetLevel(0u,
904 (C2FloatValue) (encTargetLevel*-0.25));
905 work->worklets.front()->output.configUpdate.push_back(
906 C2Param::Copy(currentEncodedTargetLevel));
907
908 C2StreamDrcAlbumModeTuning::input currentAlbumMode(0u,
909 (C2Config::drc_album_mode_t) albumMode);
910 work->worklets.front()->output.configUpdate.push_back(
911 C2Param::Copy(currentAlbumMode));
912
913 C2StreamDrcTargetReferenceLevelTuning::input currentTargetRefLevel(0u,
914 (float) (targetRefLevel*-0.25));
915 work->worklets.front()->output.configUpdate.push_back(
916 C2Param::Copy(currentTargetRefLevel));
917
918 C2StreamDrcEffectTypeTuning::input currentEffectype(0u,
919 (C2Config::drc_effect_type_t) effectType);
920 work->worklets.front()->output.configUpdate.push_back(
921 C2Param::Copy(currentEffectype));
922
923 C2StreamMaxChannelCountInfo::input currentMaxChannelCnt(0u, maxChannelCount);
924 work->worklets.front()->output.configUpdate.push_back(
925 C2Param::Copy(currentMaxChannelCnt));
926
Pawin Vongmasa36653902018-11-15 00:10:25 -0800927 } while (decoderErr == AAC_DEC_OK);
928 }
929
930 int32_t outputDelay = mStreamInfo->outputDelay * mStreamInfo->numChannels;
931
Harish Mahendrakar3c415ec2021-03-22 13:56:52 -0700932 size_t numSamplesInOutput = mStreamInfo->frameSize * mStreamInfo->numChannels;
933 if (numSamplesInOutput > 0) {
934 size_t actualOutputPortDelay = (outputDelay + numSamplesInOutput - 1) / numSamplesInOutput;
935 if (actualOutputPortDelay > mOutputPortDelay) {
936 mOutputPortDelay = actualOutputPortDelay;
937 ALOGV("New Output port delay %zu ", mOutputPortDelay);
938
939 C2PortActualDelayTuning::output outputPortDelay(mOutputPortDelay);
940 std::vector<std::unique_ptr<C2SettingResult>> failures;
941 c2_status_t err =
942 mIntf->config({&outputPortDelay}, C2_MAY_BLOCK, &failures);
943 if (err == OK) {
944 work->worklets.front()->output.configUpdate.push_back(
945 C2Param::Copy(outputPortDelay));
946 } else {
947 ALOGE("Cannot set output delay");
948 mSignalledError = true;
949 work->workletsProcessed = 1u;
950 work->result = C2_CORRUPTED;
951 return;
952 }
953 }
954 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800955 mBuffersInfo.push_back(std::move(inInfo));
956 work->workletsProcessed = 0u;
957 if (!eos && mOutputDelayCompensated < outputDelay) {
958 // discard outputDelay at the beginning
959 int32_t toCompensate = outputDelay - mOutputDelayCompensated;
960 int32_t discard = outputDelayRingBufferSamplesAvailable();
961 if (discard > toCompensate) {
962 discard = toCompensate;
963 }
964 int32_t discarded = outputDelayRingBufferGetSamples(nullptr, discard);
965 mOutputDelayCompensated += discarded;
966 return;
967 }
968
969 if (eos) {
970 drainInternal(DRAIN_COMPONENT_WITH_EOS, pool, work);
971 } else {
972 drainRingBuffer(work, pool, false /* not EOS */);
973 }
974}
975
976c2_status_t C2SoftAacDec::drainInternal(
977 uint32_t drainMode,
978 const std::shared_ptr<C2BlockPool> &pool,
979 const std::unique_ptr<C2Work> &work) {
980 if (drainMode == NO_DRAIN) {
981 ALOGW("drain with NO_DRAIN: no-op");
982 return C2_OK;
983 }
984 if (drainMode == DRAIN_CHAIN) {
985 ALOGW("DRAIN_CHAIN not supported");
986 return C2_OMITTED;
987 }
988
989 bool eos = (drainMode == DRAIN_COMPONENT_WITH_EOS);
990
991 drainDecoder();
992 drainRingBuffer(work, pool, eos);
993
994 if (eos) {
995 auto fillEmptyWork = [](const std::unique_ptr<C2Work> &work) {
996 work->worklets.front()->output.flags = work->input.flags;
997 work->worklets.front()->output.buffers.clear();
998 work->worklets.front()->output.ordinal = work->input.ordinal;
999 work->workletsProcessed = 1u;
1000 };
1001 while (mBuffersInfo.size() > 1u) {
1002 finish(mBuffersInfo.front().frameIndex, fillEmptyWork);
1003 mBuffersInfo.pop_front();
1004 }
1005 if (work && work->workletsProcessed == 0u) {
1006 fillEmptyWork(work);
1007 }
1008 mBuffersInfo.clear();
1009 }
1010
1011 return C2_OK;
1012}
1013
1014c2_status_t C2SoftAacDec::drain(
1015 uint32_t drainMode,
1016 const std::shared_ptr<C2BlockPool> &pool) {
1017 return drainInternal(drainMode, pool, nullptr);
1018}
1019
1020c2_status_t C2SoftAacDec::onFlush_sm() {
1021 drainDecoder();
1022 mBuffersInfo.clear();
1023
1024 int avail;
1025 while ((avail = outputDelayRingBufferSamplesAvailable()) > 0) {
1026 if (avail > mStreamInfo->frameSize * mStreamInfo->numChannels) {
1027 avail = mStreamInfo->frameSize * mStreamInfo->numChannels;
1028 }
1029 int32_t ns = outputDelayRingBufferGetSamples(nullptr, avail);
1030 if (ns != avail) {
1031 ALOGW("not a complete frame of samples available");
1032 break;
1033 }
1034 }
1035 mOutputDelayRingBufferReadPos = mOutputDelayRingBufferWritePos;
1036
1037 return C2_OK;
1038}
1039
1040void C2SoftAacDec::drainDecoder() {
1041 // flush decoder until outputDelay is compensated
1042 while (mOutputDelayCompensated > 0) {
1043 // a buffer big enough for MAX_CHANNEL_COUNT channels of decoded HE-AAC
1044 INT_PCM tmpOutBuffer[2048 * MAX_CHANNEL_COUNT];
1045
1046 // run DRC check
1047 mDrcWrap.submitStreamData(mStreamInfo);
1048 mDrcWrap.update();
1049
1050 AAC_DECODER_ERROR decoderErr =
1051 aacDecoder_DecodeFrame(mAACDecoder,
1052 tmpOutBuffer,
1053 2048 * MAX_CHANNEL_COUNT,
1054 AACDEC_FLUSH);
1055 if (decoderErr != AAC_DEC_OK) {
1056 ALOGW("aacDecoder_DecodeFrame decoderErr = 0x%4.4x", decoderErr);
1057 }
1058
1059 int32_t tmpOutBufferSamples = mStreamInfo->frameSize * mStreamInfo->numChannels;
1060 if (tmpOutBufferSamples > mOutputDelayCompensated) {
1061 tmpOutBufferSamples = mOutputDelayCompensated;
1062 }
1063 outputDelayRingBufferPutSamples(tmpOutBuffer, tmpOutBufferSamples);
1064
1065 mOutputDelayCompensated -= tmpOutBufferSamples;
1066 }
1067}
1068
Jean-Michel Trivi3d476d12022-05-19 17:14:18 +00001069// definitions based on android.media.AudioFormat.CHANNEL_OUT_*
1070#define CHANNEL_OUT_FL 0x4
1071#define CHANNEL_OUT_FR 0x8
1072#define CHANNEL_OUT_FC 0x10
1073#define CHANNEL_OUT_LFE 0x20
1074#define CHANNEL_OUT_BL 0x40
1075#define CHANNEL_OUT_BR 0x80
1076#define CHANNEL_OUT_SL 0x800
1077#define CHANNEL_OUT_SR 0x1000
1078
1079uint32_t C2SoftAacDec::maskFromCount(uint32_t channelCount) {
1080 // KEY_CHANNEL_MASK expects masks formatted according to Java android.media.AudioFormat
1081 // where the two left-most bits are 0 for output channel mask
1082 switch (channelCount) {
1083 case 1: // mono is front left
1084 return (CHANNEL_OUT_FL);
1085 case 2: // stereo
1086 return (CHANNEL_OUT_FL | CHANNEL_OUT_FR);
1087 case 4: // 4.0 = stereo with backs
1088 return (CHANNEL_OUT_FL | CHANNEL_OUT_FC
1089 | CHANNEL_OUT_BL | CHANNEL_OUT_BR);
1090 case 5: // 5.0
1091 return (CHANNEL_OUT_FL | CHANNEL_OUT_FC | CHANNEL_OUT_FR
1092 | CHANNEL_OUT_BL | CHANNEL_OUT_BR);
1093 case 6: // 5.1 = 5.0 + LFE
1094 return (CHANNEL_OUT_FL | CHANNEL_OUT_FC | CHANNEL_OUT_FR
1095 | CHANNEL_OUT_BL | CHANNEL_OUT_BR
1096 | CHANNEL_OUT_LFE);
1097 case 7: // 7.0 = 5.0 + Sides
1098 return (CHANNEL_OUT_FL | CHANNEL_OUT_FC | CHANNEL_OUT_FR
1099 | CHANNEL_OUT_BL | CHANNEL_OUT_BR
1100 | CHANNEL_OUT_SL | CHANNEL_OUT_SR);
1101 case 8: // 7.1 = 7.0 + LFE
1102 return (CHANNEL_OUT_FL | CHANNEL_OUT_FC | CHANNEL_OUT_FR
1103 | CHANNEL_OUT_BL | CHANNEL_OUT_BR | CHANNEL_OUT_SL | CHANNEL_OUT_SR
1104 | CHANNEL_OUT_LFE);
1105 default:
1106 return 0;
1107 }
1108}
1109
Pawin Vongmasa36653902018-11-15 00:10:25 -08001110class C2SoftAacDecFactory : public C2ComponentFactory {
1111public:
1112 C2SoftAacDecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
1113 GetCodec2PlatformComponentStore()->getParamReflector())) {
1114 }
1115
1116 virtual c2_status_t createComponent(
1117 c2_node_id_t id,
1118 std::shared_ptr<C2Component>* const component,
1119 std::function<void(C2Component*)> deleter) override {
1120 *component = std::shared_ptr<C2Component>(
1121 new C2SoftAacDec(COMPONENT_NAME,
1122 id,
1123 std::make_shared<C2SoftAacDec::IntfImpl>(mHelper)),
1124 deleter);
1125 return C2_OK;
1126 }
1127
1128 virtual c2_status_t createInterface(
1129 c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface,
1130 std::function<void(C2ComponentInterface*)> deleter) override {
1131 *interface = std::shared_ptr<C2ComponentInterface>(
1132 new SimpleInterface<C2SoftAacDec::IntfImpl>(
1133 COMPONENT_NAME, id, std::make_shared<C2SoftAacDec::IntfImpl>(mHelper)),
1134 deleter);
1135 return C2_OK;
1136 }
1137
1138 virtual ~C2SoftAacDecFactory() override = default;
1139
1140private:
1141 std::shared_ptr<C2ReflectorHelper> mHelper;
1142};
1143
1144} // namespace android
1145
Cindy Zhouf6c0c3c2020-12-02 10:53:40 -08001146__attribute__((cfi_canonical_jump_table))
Pawin Vongmasa36653902018-11-15 00:10:25 -08001147extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
1148 ALOGV("in %s", __func__);
1149 return new ::android::C2SoftAacDecFactory();
1150}
1151
Cindy Zhouf6c0c3c2020-12-02 10:53:40 -08001152__attribute__((cfi_canonical_jump_table))
Pawin Vongmasa36653902018-11-15 00:10:25 -08001153extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
1154 ALOGV("in %s", __func__);
1155 delete factory;
1156}