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