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