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