1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "C2SoftG711Dec"
19 #include <log/log.h>
20
21 #include <media/stagefright/foundation/MediaDefs.h>
22
23 #include <C2PlatformSupport.h>
24 #include <SimpleC2Interface.h>
25
26 #include "C2SoftG711Dec.h"
27
28 namespace android {
29
30 namespace {
31
32 #ifdef ALAW
33 constexpr char COMPONENT_NAME[] = "c2.android.g711.alaw.decoder";
34 #else
35 constexpr char COMPONENT_NAME[] = "c2.android.g711.mlaw.decoder";
36 #endif
37
38 } // namespace
39
40 class C2SoftG711Dec::IntfImpl : public SimpleInterface<void>::BaseParams {
41 public:
IntfImpl(const std::shared_ptr<C2ReflectorHelper> & helper)42 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
43 : SimpleInterface<void>::BaseParams(
44 helper,
45 COMPONENT_NAME,
46 C2Component::KIND_DECODER,
47 C2Component::DOMAIN_AUDIO,
48 #ifdef ALAW
49 MEDIA_MIMETYPE_AUDIO_G711_ALAW
50 #else
51 MEDIA_MIMETYPE_AUDIO_G711_MLAW
52 #endif
53 ) {
54 noPrivateBuffers();
55 noInputReferences();
56 noOutputReferences();
57 noInputLatency();
58 noTimeStretch();
59 setDerivedInstance(this);
60
61 addParameter(
62 DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
63 .withConstValue(new C2ComponentAttributesSetting(
64 C2Component::ATTRIB_IS_TEMPORAL))
65 .build());
66
67 addParameter(
68 DefineParam(mSampleRate, C2_PARAMKEY_SAMPLE_RATE)
69 .withDefault(new C2StreamSampleRateInfo::output(0u, 8000))
70 .withFields({C2F(mSampleRate, value).inRange(8000, 48000)})
71 .withSetter((Setter<decltype(*mSampleRate)>::StrictValueWithNoDeps))
72 .build());
73
74 addParameter(
75 DefineParam(mChannelCount, C2_PARAMKEY_CHANNEL_COUNT)
76 .withDefault(new C2StreamChannelCountInfo::output(0u, 1))
77 .withFields({C2F(mChannelCount, value).equalTo(1)})
78 .withSetter(Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps)
79 .build());
80
81 addParameter(
82 DefineParam(mBitrate, C2_PARAMKEY_BITRATE)
83 .withDefault(new C2StreamBitrateInfo::input(0u, 64000))
84 .withFields({C2F(mBitrate, value).equalTo(64000)})
85 .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps)
86 .build());
87
88 addParameter(
89 DefineParam(mInputMaxBufSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
90 .withConstValue(new C2StreamMaxBufferSizeInfo::input(0u, 8192))
91 .build());
92 }
93
94 private:
95 std::shared_ptr<C2StreamSampleRateInfo::output> mSampleRate;
96 std::shared_ptr<C2StreamChannelCountInfo::output> mChannelCount;
97 std::shared_ptr<C2StreamBitrateInfo::input> mBitrate;
98 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
99 };
100
C2SoftG711Dec(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)101 C2SoftG711Dec::C2SoftG711Dec(
102 const char *name,
103 c2_node_id_t id,
104 const std::shared_ptr<IntfImpl> &intfImpl)
105 : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
106 mIntf(intfImpl) {
107 }
108
~C2SoftG711Dec()109 C2SoftG711Dec::~C2SoftG711Dec() {
110 onRelease();
111 }
112
onInit()113 c2_status_t C2SoftG711Dec::onInit() {
114 mSignalledOutputEos = false;
115 return C2_OK;
116 }
117
onStop()118 c2_status_t C2SoftG711Dec::onStop() {
119 mSignalledOutputEos = false;
120 return C2_OK;
121 }
122
onReset()123 void C2SoftG711Dec::onReset() {
124 (void)onStop();
125 }
126
onRelease()127 void C2SoftG711Dec::onRelease() {
128 }
129
onFlush_sm()130 c2_status_t C2SoftG711Dec::onFlush_sm() {
131 return onStop();
132 }
133
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)134 void C2SoftG711Dec::process(
135 const std::unique_ptr<C2Work> &work,
136 const std::shared_ptr<C2BlockPool> &pool) {
137 // Initialize output work
138 work->result = C2_OK;
139 work->workletsProcessed = 1u;
140 work->worklets.front()->output.flags = work->input.flags;
141
142 if (mSignalledOutputEos) {
143 work->result = C2_BAD_VALUE;
144 return;
145 }
146
147 C2ReadView rView = mDummyReadView;
148 size_t inOffset = 0u;
149 size_t inSize = 0u;
150 if (!work->input.buffers.empty()) {
151 rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
152 inSize = rView.capacity();
153 if (inSize && rView.error()) {
154 ALOGE("read view map failed %d", rView.error());
155 work->result = C2_CORRUPTED;
156 return;
157 }
158 }
159 bool eos = (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0;
160 int outSize = inSize * sizeof(int16_t);
161
162 ALOGV("in buffer attr. size %zu timestamp %d frameindex %d", inSize,
163 (int)work->input.ordinal.timestamp.peeku(), (int)work->input.ordinal.frameIndex.peeku());
164
165 if (inSize == 0) {
166 work->worklets.front()->output.flags = work->input.flags;
167 work->worklets.front()->output.buffers.clear();
168 work->worklets.front()->output.ordinal = work->input.ordinal;
169 if (eos) {
170 mSignalledOutputEos = true;
171 ALOGV("signalled EOS");
172 }
173 return;
174 }
175
176 uint8_t *inputptr = const_cast<uint8_t *>(rView.data() + inOffset);
177
178 std::shared_ptr<C2LinearBlock> block;
179 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
180 c2_status_t err = pool->fetchLinearBlock(outSize, usage, &block);
181 if (err != C2_OK) {
182 ALOGE("fetchLinearBlock for Output failed with status %d", err);
183 work->result = C2_NO_MEMORY;
184 return;
185 }
186 C2WriteView wView = block->map().get();
187 if (wView.error()) {
188 ALOGE("write view map failed %d", wView.error());
189 work->result = C2_CORRUPTED;
190 return;
191 }
192 int16_t *outputptr = reinterpret_cast<int16_t *>(wView.data());
193
194 #ifdef ALAW
195 DecodeALaw(outputptr, inputptr, inSize);
196 #else
197 DecodeMLaw(outputptr, inputptr, inSize);
198 #endif
199
200 work->worklets.front()->output.flags = work->input.flags;
201 work->worklets.front()->output.buffers.clear();
202 work->worklets.front()->output.buffers.push_back(createLinearBuffer(block));
203 work->worklets.front()->output.ordinal = work->input.ordinal;
204
205 if (eos) {
206 mSignalledOutputEos = true;
207 ALOGV("signalled EOS");
208 }
209 }
210
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)211 c2_status_t C2SoftG711Dec::drain(
212 uint32_t drainMode,
213 const std::shared_ptr<C2BlockPool> &pool) {
214 (void) pool;
215 if (drainMode == NO_DRAIN) {
216 ALOGW("drain with NO_DRAIN: no-op");
217 return C2_OK;
218 }
219 if (drainMode == DRAIN_CHAIN) {
220 ALOGW("DRAIN_CHAIN not supported");
221 return C2_OMITTED;
222 }
223
224 return C2_OK;
225 }
226
227 #ifdef ALAW
DecodeALaw(int16_t * out,const uint8_t * in,size_t inSize)228 void C2SoftG711Dec::DecodeALaw(
229 int16_t *out, const uint8_t *in, size_t inSize) {
230 while (inSize > 0) {
231 inSize--;
232 int32_t x = *in++;
233
234 int32_t ix = x ^ 0x55;
235 ix &= 0x7f;
236
237 int32_t iexp = ix >> 4;
238 int32_t mant = ix & 0x0f;
239
240 if (iexp > 0) {
241 mant += 16;
242 }
243
244 mant = (mant << 4) + 8;
245
246 if (iexp > 1) {
247 mant = mant << (iexp - 1);
248 }
249
250 *out++ = (x > 127) ? mant : -mant;
251 }
252 }
253 #else
DecodeMLaw(int16_t * out,const uint8_t * in,size_t inSize)254 void C2SoftG711Dec::DecodeMLaw(
255 int16_t *out, const uint8_t *in, size_t inSize) {
256 while (inSize > 0) {
257 inSize--;
258 int32_t x = *in++;
259
260 int32_t mantissa = ~x;
261 int32_t exponent = (mantissa >> 4) & 7;
262 int32_t segment = exponent + 1;
263 mantissa &= 0x0f;
264
265 int32_t step = 4 << segment;
266
267 int32_t abs = (0x80l << exponent) + step * mantissa + step / 2 - 4 * 33;
268
269 *out++ = (x < 0x80) ? -abs : abs;
270 }
271 }
272 #endif
273
274 class C2SoftG711DecFactory : public C2ComponentFactory {
275 public:
C2SoftG711DecFactory()276 C2SoftG711DecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
277 GetCodec2PlatformComponentStore()->getParamReflector())) {
278 }
279
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)280 virtual c2_status_t createComponent(
281 c2_node_id_t id,
282 std::shared_ptr<C2Component>* const component,
283 std::function<void(C2Component*)> deleter) override {
284 *component = std::shared_ptr<C2Component>(
285 new C2SoftG711Dec(COMPONENT_NAME, id,
286 std::make_shared<C2SoftG711Dec::IntfImpl>(mHelper)),
287 deleter);
288 return C2_OK;
289 }
290
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)291 virtual c2_status_t createInterface(
292 c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface,
293 std::function<void(C2ComponentInterface*)> deleter) override {
294 *interface = std::shared_ptr<C2ComponentInterface>(
295 new SimpleInterface<C2SoftG711Dec::IntfImpl>(
296 COMPONENT_NAME, id, std::make_shared<C2SoftG711Dec::IntfImpl>(mHelper)),
297 deleter);
298 return C2_OK;
299 }
300
301 virtual ~C2SoftG711DecFactory() override = default;
302
303 private:
304 std::shared_ptr<C2ReflectorHelper> mHelper;
305 };
306
307 } // namespace android
308
CreateCodec2Factory()309 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
310 ALOGV("in %s", __func__);
311 return new ::android::C2SoftG711DecFactory();
312 }
313
DestroyCodec2Factory(::C2ComponentFactory * factory)314 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
315 ALOGV("in %s", __func__);
316 delete factory;
317 }
318