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 "C2SoftRawDec"
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 "C2SoftRawDec.h"
27 
28 namespace android {
29 
30 namespace {
31 
32 constexpr char COMPONENT_NAME[] = "c2.android.raw.decoder";
33 
34 }  // namespace
35 
36 class C2SoftRawDec::IntfImpl : public SimpleInterface<void>::BaseParams {
37 public:
IntfImpl(const std::shared_ptr<C2ReflectorHelper> & helper)38     explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
39         : SimpleInterface<void>::BaseParams(
40                 helper,
41                 COMPONENT_NAME,
42                 C2Component::KIND_DECODER,
43                 C2Component::DOMAIN_AUDIO,
44                 MEDIA_MIMETYPE_AUDIO_RAW) {
45         noPrivateBuffers();
46         noInputReferences();
47         noOutputReferences();
48         noInputLatency();
49         noTimeStretch();
50         setDerivedInstance(this);
51 
52         addParameter(
53                 DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
54                 .withConstValue(new C2ComponentAttributesSetting(
55                     C2Component::ATTRIB_IS_TEMPORAL))
56                 .build());
57 
58         addParameter(
59                 DefineParam(mSampleRate, C2_PARAMKEY_SAMPLE_RATE)
60                 .withDefault(new C2StreamSampleRateInfo::output(0u, 44100))
61                 .withFields({C2F(mSampleRate, value).greaterThan(0)})
62                 .withSetter((Setter<decltype(*mSampleRate)>::StrictValueWithNoDeps))
63                 .build());
64 
65         addParameter(
66                 DefineParam(mChannelCount, C2_PARAMKEY_CHANNEL_COUNT)
67                 .withDefault(new C2StreamChannelCountInfo::output(0u, 2))
68                 .withFields({C2F(mChannelCount, value).inRange(1, 8)})
69                 .withSetter(Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps)
70                 .build());
71 
72         addParameter(
73                 DefineParam(mBitrate, C2_PARAMKEY_BITRATE)
74                 .withDefault(new C2StreamBitrateInfo::input(0u, 64000))
75                 .withFields({C2F(mBitrate, value).inRange(1, 98304000)})
76                 .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps)
77                 .build());
78 
79         addParameter(
80                 DefineParam(mInputMaxBufSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
81                 .withConstValue(new C2StreamMaxBufferSizeInfo::input(0u, 64 * 1024))
82                 .build());
83 
84         addParameter(
85                 DefineParam(mPcmEncodingInfo, C2_PARAMKEY_PCM_ENCODING)
86                 .withDefault(new C2StreamPcmEncodingInfo::output(0u, C2Config::PCM_16))
87                 .withFields({C2F(mPcmEncodingInfo, value).oneOf({
88                      C2Config::PCM_16,
89                      C2Config::PCM_8,
90                      C2Config::PCM_FLOAT})
91                 })
92                 .withSetter((Setter<decltype(*mPcmEncodingInfo)>::StrictValueWithNoDeps))
93                 .build());
94 
95     }
96 
97 private:
98     std::shared_ptr<C2StreamSampleRateInfo::output> mSampleRate;
99     std::shared_ptr<C2StreamChannelCountInfo::output> mChannelCount;
100     std::shared_ptr<C2StreamBitrateInfo::input> mBitrate;
101     std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
102     std::shared_ptr<C2StreamPcmEncodingInfo::output> mPcmEncodingInfo;
103 };
104 
C2SoftRawDec(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)105 C2SoftRawDec::C2SoftRawDec(
106         const char *name,
107         c2_node_id_t id,
108         const std::shared_ptr<IntfImpl> &intfImpl)
109     : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
110       mIntf(intfImpl) {
111 }
112 
~C2SoftRawDec()113 C2SoftRawDec::~C2SoftRawDec() {
114     onRelease();
115 }
116 
onInit()117 c2_status_t C2SoftRawDec::onInit() {
118     mSignalledEos = false;
119     return C2_OK;
120 }
121 
onStop()122 c2_status_t C2SoftRawDec::onStop() {
123     mSignalledEos = false;
124     return C2_OK;
125 }
126 
onReset()127 void C2SoftRawDec::onReset() {
128     (void)onStop();
129 }
130 
onRelease()131 void C2SoftRawDec::onRelease() {
132 }
133 
onFlush_sm()134 c2_status_t C2SoftRawDec::onFlush_sm() {
135     return onStop();
136 }
137 
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)138 void C2SoftRawDec::process(
139         const std::unique_ptr<C2Work> &work,
140         const std::shared_ptr<C2BlockPool> &pool) {
141     (void)pool;
142     work->result = C2_OK;
143     work->workletsProcessed = 1u;
144 
145     if (mSignalledEos) {
146         work->result = C2_BAD_VALUE;
147         return;
148     }
149 
150     ALOGV("in buffer attr. timestamp %d frameindex %d",
151           (int)work->input.ordinal.timestamp.peeku(), (int)work->input.ordinal.frameIndex.peeku());
152 
153     work->worklets.front()->output.flags = work->input.flags;
154     work->worklets.front()->output.buffers.clear();
155     work->worklets.front()->output.ordinal = work->input.ordinal;
156     if (!work->input.buffers.empty()) {
157         work->worklets.front()->output.buffers.push_back(work->input.buffers[0]);
158     }
159     if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) {
160         mSignalledEos = true;
161         ALOGV("signalled EOS");
162     }
163 }
164 
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)165 c2_status_t C2SoftRawDec::drain(
166         uint32_t drainMode,
167         const std::shared_ptr<C2BlockPool> &pool) {
168     (void) pool;
169     if (drainMode == NO_DRAIN) {
170         ALOGW("drain with NO_DRAIN: no-op");
171         return C2_OK;
172     }
173     if (drainMode == DRAIN_CHAIN) {
174         ALOGW("DRAIN_CHAIN not supported");
175         return C2_OMITTED;
176     }
177 
178     return C2_OK;
179 }
180 
181 class C2SoftRawDecFactory : public C2ComponentFactory {
182 public:
C2SoftRawDecFactory()183     C2SoftRawDecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
184             GetCodec2PlatformComponentStore()->getParamReflector())) {
185     }
186 
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)187     virtual c2_status_t createComponent(
188             c2_node_id_t id,
189             std::shared_ptr<C2Component>* const component,
190             std::function<void(C2Component*)> deleter) override {
191         *component = std::shared_ptr<C2Component>(
192                 new C2SoftRawDec(COMPONENT_NAME,
193                               id,
194                               std::make_shared<C2SoftRawDec::IntfImpl>(mHelper)),
195                 deleter);
196         return C2_OK;
197     }
198 
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)199     virtual c2_status_t createInterface(
200             c2_node_id_t id,
201             std::shared_ptr<C2ComponentInterface>* const interface,
202             std::function<void(C2ComponentInterface*)> deleter) override {
203         *interface = std::shared_ptr<C2ComponentInterface>(
204                 new SimpleInterface<C2SoftRawDec::IntfImpl>(
205                         COMPONENT_NAME, id, std::make_shared<C2SoftRawDec::IntfImpl>(mHelper)),
206                 deleter);
207         return C2_OK;
208     }
209 
210     virtual ~C2SoftRawDecFactory() override = default;
211 
212 private:
213     std::shared_ptr<C2ReflectorHelper> mHelper;
214 };
215 
216 }  // namespace android
217 
CreateCodec2Factory()218 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
219     ALOGV("in %s", __func__);
220     return new ::android::C2SoftRawDecFactory();
221 }
222 
DestroyCodec2Factory(::C2ComponentFactory * factory)223 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
224     ALOGV("in %s", __func__);
225     delete factory;
226 }
227