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, 12)})
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                      C2Config::PCM_24,
92                      C2Config::PCM_32})
93                 })
94                 .withSetter((Setter<decltype(*mPcmEncodingInfo)>::StrictValueWithNoDeps))
95                 .build());
96 
97     }
98 
99 private:
100     std::shared_ptr<C2StreamSampleRateInfo::output> mSampleRate;
101     std::shared_ptr<C2StreamChannelCountInfo::output> mChannelCount;
102     std::shared_ptr<C2StreamBitrateInfo::input> mBitrate;
103     std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
104     std::shared_ptr<C2StreamPcmEncodingInfo::output> mPcmEncodingInfo;
105 };
106 
C2SoftRawDec(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)107 C2SoftRawDec::C2SoftRawDec(
108         const char *name,
109         c2_node_id_t id,
110         const std::shared_ptr<IntfImpl> &intfImpl)
111     : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
112       mIntf(intfImpl) {
113 }
114 
~C2SoftRawDec()115 C2SoftRawDec::~C2SoftRawDec() {
116     onRelease();
117 }
118 
onInit()119 c2_status_t C2SoftRawDec::onInit() {
120     mSignalledEos = false;
121     return C2_OK;
122 }
123 
onStop()124 c2_status_t C2SoftRawDec::onStop() {
125     mSignalledEos = false;
126     return C2_OK;
127 }
128 
onReset()129 void C2SoftRawDec::onReset() {
130     (void)onStop();
131 }
132 
onRelease()133 void C2SoftRawDec::onRelease() {
134 }
135 
onFlush_sm()136 c2_status_t C2SoftRawDec::onFlush_sm() {
137     return onStop();
138 }
139 
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)140 void C2SoftRawDec::process(
141         const std::unique_ptr<C2Work> &work,
142         const std::shared_ptr<C2BlockPool> &pool) {
143     (void)pool;
144     work->result = C2_OK;
145     work->workletsProcessed = 1u;
146 
147     if (mSignalledEos) {
148         work->result = C2_BAD_VALUE;
149         return;
150     }
151 
152     ALOGV("in buffer attr. timestamp %d frameindex %d",
153           (int)work->input.ordinal.timestamp.peeku(), (int)work->input.ordinal.frameIndex.peeku());
154 
155     work->worklets.front()->output.flags = work->input.flags;
156     work->worklets.front()->output.buffers.clear();
157     work->worklets.front()->output.ordinal = work->input.ordinal;
158     if (!work->input.buffers.empty()) {
159         work->worklets.front()->output.buffers.push_back(work->input.buffers[0]);
160     }
161     if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) {
162         mSignalledEos = true;
163         ALOGV("signalled EOS");
164     }
165 }
166 
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)167 c2_status_t C2SoftRawDec::drain(
168         uint32_t drainMode,
169         const std::shared_ptr<C2BlockPool> &pool) {
170     (void) pool;
171     if (drainMode == NO_DRAIN) {
172         ALOGW("drain with NO_DRAIN: no-op");
173         return C2_OK;
174     }
175     if (drainMode == DRAIN_CHAIN) {
176         ALOGW("DRAIN_CHAIN not supported");
177         return C2_OMITTED;
178     }
179 
180     return C2_OK;
181 }
182 
183 class C2SoftRawDecFactory : public C2ComponentFactory {
184 public:
C2SoftRawDecFactory()185     C2SoftRawDecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
186             GetCodec2PlatformComponentStore()->getParamReflector())) {
187     }
188 
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)189     virtual c2_status_t createComponent(
190             c2_node_id_t id,
191             std::shared_ptr<C2Component>* const component,
192             std::function<void(C2Component*)> deleter) override {
193         *component = std::shared_ptr<C2Component>(
194                 new C2SoftRawDec(COMPONENT_NAME,
195                               id,
196                               std::make_shared<C2SoftRawDec::IntfImpl>(mHelper)),
197                 deleter);
198         return C2_OK;
199     }
200 
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)201     virtual c2_status_t createInterface(
202             c2_node_id_t id,
203             std::shared_ptr<C2ComponentInterface>* const interface,
204             std::function<void(C2ComponentInterface*)> deleter) override {
205         *interface = std::shared_ptr<C2ComponentInterface>(
206                 new SimpleInterface<C2SoftRawDec::IntfImpl>(
207                         COMPONENT_NAME, id, std::make_shared<C2SoftRawDec::IntfImpl>(mHelper)),
208                 deleter);
209         return C2_OK;
210     }
211 
212     virtual ~C2SoftRawDecFactory() override = default;
213 
214 private:
215     std::shared_ptr<C2ReflectorHelper> mHelper;
216 };
217 
218 }  // namespace android
219 
220 __attribute__((cfi_canonical_jump_table))
CreateCodec2Factory()221 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
222     ALOGV("in %s", __func__);
223     return new ::android::C2SoftRawDecFactory();
224 }
225 
226 __attribute__((cfi_canonical_jump_table))
DestroyCodec2Factory(::C2ComponentFactory * factory)227 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
228     ALOGV("in %s", __func__);
229     delete factory;
230 }
231