1 /*
2 * Copyright (C) 2017 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 "SimpleC2Interface"
19 #include <utils/Log.h>
20
21 // use MediaDefs here vs. MediaCodecConstants as this is not MediaCodec specific/dependent
22 #include <media/stagefright/foundation/MediaDefs.h>
23
24 #include <C2PlatformSupport.h>
25 #include <SimpleC2Interface.h>
26
27 namespace android {
28
29 /* SimpleInterface */
30
SubscribedParamIndicesSetter(bool mayBlock,C2InterfaceHelper::C2P<C2SubscribedParamIndicesTuning> & me)31 static C2R SubscribedParamIndicesSetter(
32 bool mayBlock, C2InterfaceHelper::C2P<C2SubscribedParamIndicesTuning> &me) {
33 (void)mayBlock;
34 (void)me;
35
36 return C2R::Ok();
37 }
38
BaseParams(const std::shared_ptr<C2ReflectorHelper> & reflector,C2String name,C2Component::kind_t kind,C2Component::domain_t domain,C2String mediaType,std::vector<C2String> aliases)39 SimpleInterface<void>::BaseParams::BaseParams(
40 const std::shared_ptr<C2ReflectorHelper> &reflector,
41 C2String name,
42 C2Component::kind_t kind,
43 C2Component::domain_t domain,
44 C2String mediaType,
45 std::vector<C2String> aliases)
46 : C2InterfaceHelper(reflector) {
47 setDerivedInstance(this);
48
49 addParameter(
50 DefineParam(mApiFeatures, C2_PARAMKEY_API_FEATURES)
51 .withConstValue(new C2ApiFeaturesSetting(C2Config::api_feature_t(
52 API_REFLECTION |
53 API_VALUES |
54 API_CURRENT_VALUES |
55 API_DEPENDENCY |
56 API_SAME_INPUT_BUFFER)))
57 .build());
58
59 addParameter(
60 DefineParam(mName, C2_PARAMKEY_COMPONENT_NAME)
61 .withConstValue(AllocSharedString<C2ComponentNameSetting>(name.c_str()))
62 .build());
63
64 if (aliases.size()) {
65 C2String joined;
66 for (const C2String &alias : aliases) {
67 if (joined.length()) {
68 joined += ",";
69 }
70 joined += alias;
71 }
72 addParameter(
73 DefineParam(mAliases, C2_PARAMKEY_COMPONENT_ALIASES)
74 .withConstValue(AllocSharedString<C2ComponentAliasesSetting>(joined.c_str()))
75 .build());
76 }
77
78 addParameter(
79 DefineParam(mKind, C2_PARAMKEY_COMPONENT_KIND)
80 .withConstValue(new C2ComponentKindSetting(kind))
81 .build());
82
83 addParameter(
84 DefineParam(mDomain, C2_PARAMKEY_COMPONENT_DOMAIN)
85 .withConstValue(new C2ComponentDomainSetting(domain))
86 .build());
87
88 // simple interfaces have single streams
89 addParameter(
90 DefineParam(mInputStreamCount, C2_PARAMKEY_INPUT_STREAM_COUNT)
91 .withConstValue(new C2PortStreamCountTuning::input(1))
92 .build());
93
94 addParameter(
95 DefineParam(mOutputStreamCount, C2_PARAMKEY_OUTPUT_STREAM_COUNT)
96 .withConstValue(new C2PortStreamCountTuning::output(1))
97 .build());
98
99 // set up buffer formats and allocators
100
101 // default to linear buffers and no media type
102 C2BufferData::type_t rawBufferType = C2BufferData::LINEAR;
103 C2String rawMediaType;
104 C2Allocator::id_t rawAllocator = C2AllocatorStore::DEFAULT_LINEAR;
105 C2BlockPool::local_id_t rawPoolId = C2BlockPool::BASIC_LINEAR;
106 C2BufferData::type_t codedBufferType = C2BufferData::LINEAR;
107 int poolMask = GetCodec2PoolMask();
108 C2Allocator::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask);
109 C2Allocator::id_t codedAllocator = preferredLinearId;
110 C2BlockPool::local_id_t codedPoolId = C2BlockPool::BASIC_LINEAR;
111
112 switch (domain) {
113 case C2Component::DOMAIN_IMAGE: [[fallthrough]];
114 case C2Component::DOMAIN_VIDEO:
115 // TODO: should we define raw image? The only difference is timestamp handling
116 rawBufferType = C2BufferData::GRAPHIC;
117 rawMediaType = MEDIA_MIMETYPE_VIDEO_RAW;
118 rawAllocator = C2PlatformAllocatorStore::GRALLOC;
119 rawPoolId = C2BlockPool::BASIC_GRAPHIC;
120 break;
121 case C2Component::DOMAIN_AUDIO:
122 rawBufferType = C2BufferData::LINEAR;
123 rawMediaType = MEDIA_MIMETYPE_AUDIO_RAW;
124 rawAllocator = preferredLinearId;
125 rawPoolId = C2BlockPool::BASIC_LINEAR;
126 break;
127 default:
128 break;
129 }
130 bool isEncoder = kind == C2Component::KIND_ENCODER;
131
132 // handle raw decoders
133 if (mediaType == rawMediaType) {
134 codedBufferType = rawBufferType;
135 codedAllocator = rawAllocator;
136 codedPoolId = rawPoolId;
137 }
138
139 addParameter(
140 DefineParam(mInputFormat, C2_PARAMKEY_INPUT_STREAM_BUFFER_TYPE)
141 .withConstValue(new C2StreamBufferTypeSetting::input(
142 0u, isEncoder ? rawBufferType : codedBufferType))
143 .build());
144
145 addParameter(
146 DefineParam(mInputMediaType, C2_PARAMKEY_INPUT_MEDIA_TYPE)
147 .withConstValue(AllocSharedString<C2PortMediaTypeSetting::input>(
148 isEncoder ? rawMediaType : mediaType))
149 .build());
150
151 addParameter(
152 DefineParam(mOutputFormat, C2_PARAMKEY_OUTPUT_STREAM_BUFFER_TYPE)
153 .withConstValue(new C2StreamBufferTypeSetting::output(
154 0u, isEncoder ? codedBufferType : rawBufferType))
155 .build());
156
157 addParameter(
158 DefineParam(mOutputMediaType, C2_PARAMKEY_OUTPUT_MEDIA_TYPE)
159 .withConstValue(AllocSharedString<C2PortMediaTypeSetting::output>(
160 isEncoder ? mediaType : rawMediaType))
161 .build());
162
163 C2Allocator::id_t inputAllocators[1] = { isEncoder ? rawAllocator : codedAllocator };
164 C2Allocator::id_t outputAllocators[1] = { isEncoder ? codedAllocator : rawAllocator };
165 C2BlockPool::local_id_t outputPoolIds[1] = { isEncoder ? codedPoolId : rawPoolId };
166
167 addParameter(
168 DefineParam(mInputAllocators, C2_PARAMKEY_INPUT_ALLOCATORS)
169 .withDefault(C2PortAllocatorsTuning::input::AllocShared(inputAllocators))
170 .withFields({ C2F(mInputAllocators, m.values[0]).any(),
171 C2F(mInputAllocators, m.values).inRange(0, 1) })
172 .withSetter(Setter<C2PortAllocatorsTuning::input>::NonStrictValuesWithNoDeps)
173 .build());
174
175 addParameter(
176 DefineParam(mOutputAllocators, C2_PARAMKEY_OUTPUT_ALLOCATORS)
177 .withDefault(C2PortAllocatorsTuning::output::AllocShared(outputAllocators))
178 .withFields({ C2F(mOutputAllocators, m.values[0]).any(),
179 C2F(mOutputAllocators, m.values).inRange(0, 1) })
180 .withSetter(Setter<C2PortAllocatorsTuning::output>::NonStrictValuesWithNoDeps)
181 .build());
182
183 addParameter(
184 DefineParam(mOutputPoolIds, C2_PARAMKEY_OUTPUT_BLOCK_POOLS)
185 .withDefault(C2PortBlockPoolsTuning::output::AllocShared(outputPoolIds))
186 .withFields({ C2F(mOutputPoolIds, m.values[0]).any(),
187 C2F(mOutputPoolIds, m.values).inRange(0, 1) })
188 .withSetter(Setter<C2PortBlockPoolsTuning::output>::NonStrictValuesWithNoDeps)
189 .build());
190
191 // add stateless params
192 addParameter(
193 DefineParam(mSubscribedParamIndices, C2_PARAMKEY_SUBSCRIBED_PARAM_INDICES)
194 .withDefault(C2SubscribedParamIndicesTuning::AllocShared(0u))
195 .withFields({ C2F(mSubscribedParamIndices, m.values[0]).any(),
196 C2F(mSubscribedParamIndices, m.values).any() })
197 .withSetter(SubscribedParamIndicesSetter)
198 .build());
199
200 /* TODO
201
202 addParameter(
203 DefineParam(mCurrentWorkOrdinal, C2_PARAMKEY_CURRENT_WORK)
204 .withDefault(new C2CurrentWorkTuning())
205 .withFields({ C2F(mCurrentWorkOrdinal, m.timeStamp).any(),
206 C2F(mCurrentWorkOrdinal, m.frameIndex).any(),
207 C2F(mCurrentWorkOrdinal, m.customOrdinal).any() })
208 .withSetter(Setter<C2CurrentWorkTuning>::NonStrictValuesWithNoDeps)
209 .build());
210
211 addParameter(
212 DefineParam(mLastInputQueuedWorkOrdinal, C2_PARAMKEY_LAST_INPUT_QUEUED)
213 .withDefault(new C2LastWorkQueuedTuning::input())
214 .withFields({ C2F(mLastInputQueuedWorkOrdinal, m.timeStamp).any(),
215 C2F(mLastInputQueuedWorkOrdinal, m.frameIndex).any(),
216 C2F(mLastInputQueuedWorkOrdinal, m.customOrdinal).any() })
217 .withSetter(Setter<C2LastWorkQueuedTuning::input>::NonStrictValuesWithNoDeps)
218 .build());
219
220 addParameter(
221 DefineParam(mLastOutputQueuedWorkOrdinal, C2_PARAMKEY_LAST_OUTPUT_QUEUED)
222 .withDefault(new C2LastWorkQueuedTuning::output())
223 .withFields({ C2F(mLastOutputQueuedWorkOrdinal, m.timeStamp).any(),
224 C2F(mLastOutputQueuedWorkOrdinal, m.frameIndex).any(),
225 C2F(mLastOutputQueuedWorkOrdinal, m.customOrdinal).any() })
226 .withSetter(Setter<C2LastWorkQueuedTuning::output>::NonStrictValuesWithNoDeps)
227 .build());
228
229 std::shared_ptr<C2OutOfMemoryTuning> mOutOfMemory;
230
231 std::shared_ptr<C2PortConfigCounterTuning::input> mInputConfigCounter;
232 std::shared_ptr<C2PortConfigCounterTuning::output> mOutputConfigCounter;
233 std::shared_ptr<C2ConfigCounterTuning> mDirectConfigCounter;
234
235 */
236 }
237
noInputLatency()238 void SimpleInterface<void>::BaseParams::noInputLatency() {
239 addParameter(
240 DefineParam(mRequestedInputDelay, C2_PARAMKEY_INPUT_DELAY_REQUEST)
241 .withConstValue(new C2PortRequestedDelayTuning::input(0u))
242 .build());
243
244 addParameter(
245 DefineParam(mActualInputDelay, C2_PARAMKEY_INPUT_DELAY)
246 .withConstValue(new C2PortActualDelayTuning::input(0u))
247 .build());
248 }
249
noOutputLatency()250 void SimpleInterface<void>::BaseParams::noOutputLatency() {
251 addParameter(
252 DefineParam(mRequestedOutputDelay, C2_PARAMKEY_OUTPUT_DELAY_REQUEST)
253 .withConstValue(new C2PortRequestedDelayTuning::output(0u))
254 .build());
255
256 addParameter(
257 DefineParam(mActualOutputDelay, C2_PARAMKEY_OUTPUT_DELAY)
258 .withConstValue(new C2PortActualDelayTuning::output(0u))
259 .build());
260 }
261
noPipelineLatency()262 void SimpleInterface<void>::BaseParams::noPipelineLatency() {
263 addParameter(
264 DefineParam(mRequestedPipelineDelay, C2_PARAMKEY_PIPELINE_DELAY_REQUEST)
265 .withConstValue(new C2RequestedPipelineDelayTuning(0u))
266 .build());
267
268 addParameter(
269 DefineParam(mActualPipelineDelay, C2_PARAMKEY_PIPELINE_DELAY)
270 .withConstValue(new C2ActualPipelineDelayTuning(0u))
271 .build());
272 }
273
noPrivateBuffers()274 void SimpleInterface<void>::BaseParams::noPrivateBuffers() {
275 addParameter(
276 DefineParam(mPrivateAllocators, C2_PARAMKEY_PRIVATE_ALLOCATORS)
277 .withConstValue(C2PrivateAllocatorsTuning::AllocShared(0u))
278 .build());
279
280 addParameter(
281 DefineParam(mMaxPrivateBufferCount, C2_PARAMKEY_MAX_PRIVATE_BUFFER_COUNT)
282 .withConstValue(C2MaxPrivateBufferCountTuning::AllocShared(0u))
283 .build());
284
285 addParameter(
286 DefineParam(mPrivatePoolIds, C2_PARAMKEY_PRIVATE_BLOCK_POOLS)
287 .withConstValue(C2PrivateBlockPoolsTuning::AllocShared(0u))
288 .build());
289 }
290
noInputReferences()291 void SimpleInterface<void>::BaseParams::noInputReferences() {
292 addParameter(
293 DefineParam(mMaxInputReferenceAge, C2_PARAMKEY_INPUT_MAX_REFERENCE_AGE)
294 .withConstValue(new C2StreamMaxReferenceAgeTuning::input(0u))
295 .build());
296
297 addParameter(
298 DefineParam(mMaxInputReferenceCount, C2_PARAMKEY_INPUT_MAX_REFERENCE_COUNT)
299 .withConstValue(new C2StreamMaxReferenceCountTuning::input(0u))
300 .build());
301 }
302
noOutputReferences()303 void SimpleInterface<void>::BaseParams::noOutputReferences() {
304 addParameter(
305 DefineParam(mMaxOutputReferenceAge, C2_PARAMKEY_OUTPUT_MAX_REFERENCE_AGE)
306 .withConstValue(new C2StreamMaxReferenceAgeTuning::output(0u))
307 .build());
308
309 addParameter(
310 DefineParam(mMaxOutputReferenceCount, C2_PARAMKEY_OUTPUT_MAX_REFERENCE_COUNT)
311 .withConstValue(new C2StreamMaxReferenceCountTuning::output(0u))
312 .build());
313 }
314
noTimeStretch()315 void SimpleInterface<void>::BaseParams::noTimeStretch() {
316 addParameter(
317 DefineParam(mTimeStretch, C2_PARAMKEY_TIME_STRETCH)
318 .withConstValue(new C2ComponentTimeStretchTuning(1.f))
319 .build());
320 }
321
322 /*
323 Clients need to handle the following base params due to custom dependency.
324
325 std::shared_ptr<C2ApiLevelSetting> mApiLevel;
326 std::shared_ptr<C2ComponentAttributesSetting> mAttrib;
327
328 std::shared_ptr<C2PortSuggestedBufferCountTuning::input> mSuggestedInputBufferCount;
329 std::shared_ptr<C2PortSuggestedBufferCountTuning::output> mSuggestedOutputBufferCount;
330
331 std::shared_ptr<C2TrippedTuning> mTripped;
332
333 */
334
335 } // namespace android
336