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 "ReflectedParamUpdater"
19 #include <utils/Log.h>
20
21 #include <iostream>
22 #include <set>
23 #include <sstream>
24
25 #include <C2Debug.h>
26 #include <C2ParamInternal.h>
27
28 #include <media/stagefright/foundation/ABuffer.h>
29 #include <media/stagefright/foundation/ADebug.h>
30 #include <media/stagefright/foundation/AString.h>
31 #include <media/stagefright/foundation/hexdump.h>
32
33 #include "ReflectedParamUpdater.h"
34
35 namespace android {
36
debugString(size_t indent_) const37 std::string ReflectedParamUpdater::Dict::debugString(size_t indent_) const {
38 std::string indent(indent_, ' ');
39 std::stringstream s;
40 s << "Dict {" << std::endl;
41
42 for (const auto &it : *this) {
43 s << indent << " ";
44
45 C2Value c2Value;
46 int32_t int32Value;
47 uint32_t uint32Value;
48 int64_t int64Value;
49 uint64_t uint64Value;
50 float floatValue;
51 sp<ABuffer> bufValue;
52 AString strValue;
53 if (it.second.find(&c2Value)) {
54 switch (c2Value.type()) {
55 case C2Value::INT32:
56 (void)c2Value.get(&int32Value);
57 s << "c2::i32 " << it.first << " = " << int32Value;
58 break;
59 case C2Value::UINT32:
60 (void)c2Value.get(&uint32Value);
61 s << "c2::u32 " << it.first << " = " << uint32Value;
62 break;
63 case C2Value::CNTR32:
64 // dump counter value as unsigned
65 (void)c2Value.get((c2_cntr32_t*)&uint32Value);
66 s << "c2::c32 " << it.first << " = " << uint32Value;
67 break;
68 case C2Value::INT64:
69 (void)c2Value.get(&int64Value);
70 s << "c2::i64 " << it.first << " = " << int64Value;
71 break;
72 case C2Value::UINT64:
73 (void)c2Value.get(&uint64Value);
74 s << "c2::u64 " << it.first << " = " << uint64Value;
75 break;
76 case C2Value::CNTR64:
77 // dump counter value as unsigned
78 (void)c2Value.get((c2_cntr64_t*)&uint64Value);
79 s << "c2::c64 " << it.first << " = " << uint64Value;
80 break;
81 case C2Value::FLOAT:
82 (void)c2Value.get(&floatValue);
83 s << "c2::float " << it.first << " = " << floatValue;
84 break;
85 default:
86 // dump unsupported values for debugging, these should not be used
87 s << "c2::unsupported " << it.first;
88 }
89 } else if (it.second.find(&int32Value)) {
90 s << "int32_t " << it.first << " = " << int32Value;
91 } else if (it.second.find(&int64Value)) {
92 s << "int64_t " << it.first << " = " << int64Value;
93 } else if (it.second.find(&strValue)) {
94 s << "string " << it.first << " = \"" << strValue.c_str() << "\"";
95 } else if (it.second.find(&bufValue)) {
96 s << "Buffer " << it.first << " = ";
97 if (bufValue != nullptr && bufValue->data() != nullptr && bufValue->size() <= 64) {
98 s << "{" << std::endl;
99 AString tmp;
100 hexdump(bufValue->data(), bufValue->size(), indent_ + 4, &tmp);
101 s << tmp.c_str() << indent << " }";
102 } else {
103 s << (void*)bufValue.get();
104 }
105 } else {
106 // dump unsupported values for debugging, this should never happen.
107 s << "unsupported " << it.first;
108 }
109 s << std::endl;
110 }
111 s << indent << "}";
112
113 return s.str();
114 }
115
addParamDesc(const std::shared_ptr<C2ParamReflector> & reflector,const std::vector<std::shared_ptr<C2ParamDescriptor>> & paramDescs)116 void ReflectedParamUpdater::addParamDesc(
117 const std::shared_ptr<C2ParamReflector> &reflector,
118 const std::vector<std::shared_ptr<C2ParamDescriptor>> ¶mDescs) {
119 for (const std::shared_ptr<C2ParamDescriptor> &desc : paramDescs) {
120 std::unique_ptr<C2StructDescriptor> structDesc = reflector->describe(
121 desc->index().coreIndex());
122 if (structDesc == nullptr) {
123 ALOGD("Could not describe %s", desc->name().c_str());
124 continue;
125 }
126 addParamDesc(desc, *structDesc, reflector, true /* markVendor */);
127 }
128 }
129
addParamStructDesc(std::shared_ptr<C2ParamDescriptor> desc,C2String path,size_t offset,const C2StructDescriptor & structDesc,const std::shared_ptr<C2ParamReflector> & reflector)130 void ReflectedParamUpdater::addParamStructDesc(
131 std::shared_ptr<C2ParamDescriptor> desc,
132 C2String path,
133 size_t offset,
134 const C2StructDescriptor &structDesc,
135 const std::shared_ptr<C2ParamReflector> &reflector) {
136 for (auto it = structDesc.begin(); it != structDesc.end(); ++it) {
137 C2String fieldName = path + "." + it->name();
138 if (it->type() & C2FieldDescriptor::STRUCT_FLAG) {
139 if (reflector == nullptr || it->extent() != 1) {
140 ALOGD("ignored struct field %s", fieldName.c_str());
141 continue;
142 }
143 std::unique_ptr<C2StructDescriptor> structDesc_ = reflector->describe(
144 C2Param::CoreIndex(it->type()).coreIndex());
145 if (structDesc_ == nullptr) {
146 ALOGD("Could not describe structure of %s", fieldName.c_str());
147 continue;
148 }
149 addParamStructDesc(desc, fieldName, offset + _C2ParamInspector::GetOffset(*it),
150 *structDesc_, reflector);
151 continue;
152 }
153
154 // verify extent and type
155 switch (it->type()) {
156 case C2FieldDescriptor::INT32:
157 case C2FieldDescriptor::UINT32:
158 case C2FieldDescriptor::CNTR32:
159 case C2FieldDescriptor::INT64:
160 case C2FieldDescriptor::UINT64:
161 case C2FieldDescriptor::CNTR64:
162 case C2FieldDescriptor::FLOAT:
163 if (it->extent() != 1) {
164 ALOGD("extent() != 1 for single value type: %s", fieldName.c_str());
165 continue;
166 }
167 break;
168 case C2FieldDescriptor::STRING:
169 case C2FieldDescriptor::BLOB:
170 break;
171
172 default:
173 ALOGD("Unrecognized type: %s", fieldName.c_str());
174 continue;
175 }
176
177 ALOGV("%s registered", fieldName.c_str());
178 // TODO: get the proper size by iterating through the fields.
179 // only insert fields the very first time
180 mMap.emplace(fieldName, FieldDesc {
181 desc,
182 std::make_unique<C2FieldDescriptor>(
183 it->type(), it->extent(), it->name(),
184 _C2ParamInspector::GetOffset(*it),
185 _C2ParamInspector::GetSize(*it)),
186 offset,
187 });
188 }
189 }
190
addParamDesc(std::shared_ptr<C2ParamDescriptor> desc,const C2StructDescriptor & structDesc,const std::shared_ptr<C2ParamReflector> & reflector,bool markVendor)191 void ReflectedParamUpdater::addParamDesc(
192 std::shared_ptr<C2ParamDescriptor> desc, const C2StructDescriptor &structDesc,
193 const std::shared_ptr<C2ParamReflector> &reflector, bool markVendor) {
194 C2String paramName = desc->name();
195
196 // Do not reflect requested parameters
197 // TODO: split these once aliases are introduced into '.actual' and '.requested' and alias
198 // the name to '.actual'.
199 if (desc->index() & C2Param::CoreIndex::IS_REQUEST_FLAG) {
200 return;
201 }
202
203 // prefix vendor parameters
204 if (desc->index().isVendor() && markVendor) {
205 paramName = "vendor." + paramName;
206 }
207 mParamNames.emplace(desc->index(), paramName);
208
209 // also allow setting whole parameters in a binary fashion via ByteBuffer
210 // this is opt-in for now
211 auto it = mWholeParams.find(paramName);
212 if (it != mWholeParams.end() && it->second.coreIndex() == desc->index().coreIndex()) {
213 mMap.emplace(paramName, FieldDesc{ desc, nullptr, 0 /* offset */ });
214 // don't add fields of whole parameters.
215 return;
216 }
217
218 addParamStructDesc(desc, paramName, 0 /* offset */, structDesc, reflector);
219 }
220
supportWholeParam(std::string name,C2Param::CoreIndex index)221 void ReflectedParamUpdater::supportWholeParam(std::string name, C2Param::CoreIndex index) {
222 mWholeParams.emplace(name, index);
223 }
224
getParamName(C2Param::Index index) const225 std::string ReflectedParamUpdater::getParamName(C2Param::Index index) const {
226 auto it = mParamNames.find(index);
227 if (it != mParamNames.end()) {
228 return it->second;
229 }
230
231 std::stringstream ret;
232 ret << "<unknown " << index << ">";
233 return ret.str();
234 }
235
getParamIndicesFromMessage(const Dict & params,std::vector<C2Param::Index> * vec) const236 void ReflectedParamUpdater::getParamIndicesFromMessage(
237 const Dict ¶ms,
238 std::vector<C2Param::Index> *vec /* nonnull */) const {
239 CHECK(vec != nullptr);
240 vec->clear();
241 std::set<C2Param::Index> indices;
242 parseMessageAndDoWork(
243 params,
244 [&indices](const std::string &, const FieldDesc &desc, const void *, size_t) {
245 indices.insert(desc.paramDesc->index());
246 });
247 for (const C2Param::Index &index : indices) {
248 vec->push_back(index);
249 }
250 }
251
getParamIndicesForKeys(const std::vector<std::string> & keys,std::vector<C2Param::Index> * vec) const252 void ReflectedParamUpdater::getParamIndicesForKeys(
253 const std::vector<std::string> &keys,
254 std::vector<C2Param::Index> *vec /* nonnull */) const {
255 CHECK(vec != nullptr);
256 vec->clear();
257 std::set<C2Param::Index> indices;
258
259 std::set<std::string> keyMap(keys.begin(), keys.end());
260
261 ALOGV("in getParamIndicesForKeys with %zu keys and map of %zu entries",
262 keyMap.size(), mMap.size());
263 for (const std::pair<const std::string, FieldDesc> &kv : mMap) {
264 const std::string &name = kv.first;
265 const FieldDesc &desc = kv.second;
266 ALOGV("count of %s is %zu", name.c_str(), keyMap.count(name));
267 if (keyMap.count(name) > 0) {
268 indices.insert(desc.paramDesc->index());
269 }
270 }
271
272 for (const C2Param::Index &index : indices) {
273 vec->push_back(index);
274 }
275 }
276
getKeysForParamIndex(const C2Param::Index & index,std::vector<std::string> * keys) const277 void ReflectedParamUpdater::getKeysForParamIndex(
278 const C2Param::Index &index,
279 std::vector<std::string> *keys /* nonnull */) const {
280 CHECK(keys != nullptr);
281 keys->clear();
282 for (const std::pair<const std::string, FieldDesc> &kv : mMap) {
283 const std::string &name = kv.first;
284 const FieldDesc &desc = kv.second;
285 if (desc.paramDesc->index() == index) {
286 keys->push_back(name);
287 }
288 }
289 }
290
updateParamsFromMessage(const Dict & params,std::vector<std::unique_ptr<C2Param>> * vec) const291 void ReflectedParamUpdater::updateParamsFromMessage(
292 const Dict ¶ms,
293 std::vector<std::unique_ptr<C2Param>> *vec /* nonnull */) const {
294 CHECK(vec != nullptr);
295
296 std::map<C2Param::Index, std::unique_ptr<C2Param>*> paramsMap;
297 for (std::unique_ptr<C2Param> ¶m : *vec) {
298 if (param && *param) {
299 paramsMap[param->index()] = ¶m;
300 }
301 }
302
303 parseMessageAndDoWork(
304 params,
305 [¶msMap](const std::string &name, const FieldDesc &desc, const void *ptr, size_t size) {
306 std::unique_ptr<C2Param> *param = nullptr;
307 auto paramIt = paramsMap.find(desc.paramDesc->index());
308 if (paramIt == paramsMap.end()) {
309 ALOGD("%s found, but param #%d isn't present to update",
310 name.c_str(), (int32_t)desc.paramDesc->index());
311 return;
312 }
313 param = paramIt->second;
314
315 struct _C2Param : public C2Param {
316 using C2Param::C2Param;
317 _C2Param(uint32_t size, uint32_t index) : C2Param(size, index) { }
318 };
319
320 // we will handle whole param updates as part of a flexible param update using
321 // a zero offset.
322 size_t offset = 0;
323 size_t minOffset = 0;
324
325 // if this descriptor has a field, use the offset and size and ensure that offset
326 // is not part of the header
327 if (desc.fieldDesc) {
328 minOffset = sizeof(C2Param);
329 offset = sizeof(C2Param) + desc.offset
330 + _C2ParamInspector::GetOffset(*desc.fieldDesc);
331 }
332
333 // reallocate or trim flexible param (or whole param) as necessary
334 if (!desc.fieldDesc /* whole param */ || desc.fieldDesc->extent() == 0) {
335 // reallocate param if more space is needed
336 if (param->get()->size() < offset + size) {
337 if (size > INT32_MAX - offset || offset < minOffset) {
338 // size too long or offset too early - abandon
339 return;
340 }
341 C2Param *newParam = (C2Param *)::operator new(offset + size);
342 new (newParam) _C2Param(offset + size, param->get()->index());
343 if (offset > sizeof(C2Param)) {
344 memcpy(newParam + 1, param->get() + 1, offset - sizeof(C2Param));
345 }
346 param->reset(newParam);
347 } else if (param->get()->size() > offset + size) {
348 // trim parameter size
349 _C2ParamInspector::TrimParam(param->get(), offset + size);
350 }
351 } else if (desc.fieldDesc->type() == C2FieldDescriptor::BLOB) {
352 // zero fill blobs if updating with smaller blob
353 if (desc.fieldDesc->extent() > size) {
354 memset((uint8_t *)(param->get()) + offset + size, 0,
355 desc.fieldDesc->extent() - size);
356 }
357 }
358
359 memcpy((uint8_t *)(param->get()) + offset, ptr, size);
360 });
361 }
362
parseMessageAndDoWork(const Dict & params,std::function<void (const std::string &,const FieldDesc &,const void *,size_t)> work) const363 void ReflectedParamUpdater::parseMessageAndDoWork(
364 const Dict ¶ms,
365 std::function<void(const std::string &, const FieldDesc &, const void *, size_t)> work) const {
366 for (const std::pair<const std::string, FieldDesc> &kv : mMap) {
367 const std::string &name = kv.first;
368 const FieldDesc &desc = kv.second;
369 auto param = params.find(name);
370 if (param == params.end()) {
371 continue;
372 }
373
374 // handle whole parameters
375 if (!desc.fieldDesc) {
376 sp<ABuffer> tmp;
377 if (param->second.find(&tmp) && tmp != nullptr) {
378 C2Param *tmpAsParam = C2Param::From(tmp->data(), tmp->size());
379 if (tmpAsParam && tmpAsParam->type().type() == desc.paramDesc->index().type()) {
380 work(name, desc, tmp->data(), tmp->size());
381 } else {
382 ALOGD("Param blob does not match param for '%s' (%p, %x vs %x)",
383 name.c_str(), tmpAsParam, tmpAsParam ? tmpAsParam->type().type() : 0xDEADu,
384 desc.paramDesc->index().type());
385 }
386 }
387 continue;
388 }
389
390 int32_t int32Value;
391 int64_t int64Value;
392 C2Value c2Value;
393
394 C2FieldDescriptor::type_t fieldType = desc.fieldDesc->type();
395 size_t fieldExtent = desc.fieldDesc->extent();
396 switch (fieldType) {
397 case C2FieldDescriptor::INT32:
398 if ((param->second.find(&c2Value) && c2Value.get(&int32Value))
399 || param->second.find(&int32Value)) {
400 work(name, desc, &int32Value, sizeof(int32Value));
401 }
402 break;
403 case C2FieldDescriptor::UINT32:
404 if ((param->second.find(&c2Value) && c2Value.get((uint32_t*)&int32Value))
405 || param->second.find(&int32Value)) {
406 work(name, desc, &int32Value, sizeof(int32Value));
407 }
408 break;
409 case C2FieldDescriptor::CNTR32:
410 if ((param->second.find(&c2Value) && c2Value.get((c2_cntr32_t*)&int32Value))
411 || param->second.find(&int32Value)) {
412 work(name, desc, &int32Value, sizeof(int32Value));
413 }
414 break;
415 case C2FieldDescriptor::INT64:
416 if ((param->second.find(&c2Value) && c2Value.get(&int64Value))
417 || param->second.find(&int64Value)) {
418 work(name, desc, &int64Value, sizeof(int64Value));
419 }
420 break;
421 case C2FieldDescriptor::UINT64:
422 if ((param->second.find(&c2Value) && c2Value.get((uint64_t*)&int64Value))
423 || param->second.find(&int64Value)) {
424 work(name, desc, &int64Value, sizeof(int64Value));
425 }
426 break;
427 case C2FieldDescriptor::CNTR64:
428 if ((param->second.find(&c2Value) && c2Value.get((c2_cntr64_t*)&int64Value))
429 || param->second.find(&int64Value)) {
430 work(name, desc, &int64Value, sizeof(int64Value));
431 }
432 break;
433 case C2FieldDescriptor::FLOAT: {
434 float tmp;
435 if (param->second.find(&c2Value) && c2Value.get(&tmp)) {
436 work(name, desc, &tmp, sizeof(tmp));
437 }
438 break;
439 }
440 case C2FieldDescriptor::STRING: {
441 AString tmp;
442 if (!param->second.find(&tmp)) {
443 break;
444 }
445 if (fieldExtent > 0 && tmp.size() >= fieldExtent) {
446 AString truncated(tmp, 0, fieldExtent - 1);
447 ALOGD("String value too long to fit: original \"%s\" truncated to \"%s\"",
448 tmp.c_str(), truncated.c_str());
449 tmp = truncated;
450 }
451 work(name, desc, tmp.c_str(), tmp.size() + 1);
452 break;
453 }
454
455 case C2FieldDescriptor::BLOB: {
456 sp<ABuffer> tmp;
457 if (!param->second.find(&tmp) || tmp == nullptr) {
458 break;
459 }
460
461 if (fieldExtent > 0 && tmp->size() > fieldExtent) {
462 ALOGD("Blob value too long to fit. Truncating.");
463 tmp->setRange(tmp->offset(), fieldExtent);
464 }
465 work(name, desc, tmp->data(), tmp->size());
466 break;
467 }
468
469 default:
470 ALOGD("Unsupported data type for %s", name.c_str());
471 break;
472 }
473 }
474 }
475
476 ReflectedParamUpdater::Dict
getParams(const std::vector<std::unique_ptr<C2Param>> & params_) const477 ReflectedParamUpdater::getParams(const std::vector<std::unique_ptr<C2Param>> ¶ms_) const {
478 std::vector<C2Param*> params;
479 params.resize(params_.size());
480 std::transform(params_.begin(), params_.end(), params.begin(),
481 [](const std::unique_ptr<C2Param>& p) -> C2Param* { return p.get(); });
482 return getParams(params);
483 }
484
485 ReflectedParamUpdater::Dict
getParams(const std::vector<C2Param * > & params) const486 ReflectedParamUpdater::getParams(const std::vector<C2Param*> ¶ms) const {
487 Dict ret;
488
489 // convert vector to map
490 std::map<C2Param::Index, C2Param *> paramsMap;
491 for (C2Param *param : params) {
492 if (param != nullptr && *param) {
493 paramsMap[param->index()] = param;
494 }
495 }
496
497 for (const std::pair<const std::string, FieldDesc> &kv : mMap) {
498 const std::string &name = kv.first;
499 const FieldDesc &desc = kv.second;
500 if (paramsMap.count(desc.paramDesc->index()) == 0) {
501 continue;
502 }
503 C2Param *param = paramsMap[desc.paramDesc->index()];
504 Value value;
505
506 // handle whole params first
507 if (!desc.fieldDesc) {
508 sp<ABuffer> buf = ABuffer::CreateAsCopy(param, param->size());
509 value.set(buf);
510 ret.emplace(name, value);
511 continue;
512 }
513
514 size_t offset = sizeof(C2Param) + desc.offset
515 + _C2ParamInspector::GetOffset(*desc.fieldDesc);
516 uint8_t *data = (uint8_t *)param + offset;
517 C2FieldDescriptor::type_t fieldType = desc.fieldDesc->type();
518 switch (fieldType) {
519 case C2FieldDescriptor::STRING: {
520 size_t length = desc.fieldDesc->extent();
521 if (length == 0) {
522 length = param->size() - offset;
523 }
524
525 if (param->size() < length || param->size() - length < offset) {
526 ALOGD("param too small for string: length %zu size %zu offset %zu",
527 length, param->size(), offset);
528 break;
529 }
530 value.set(AString((char *)data, strnlen((char *)data, length)));
531 break;
532 }
533
534 case C2FieldDescriptor::BLOB: {
535 size_t length = desc.fieldDesc->extent();
536 if (length == 0) {
537 length = param->size() - offset;
538 }
539
540 if (param->size() < length || param->size() - length < offset) {
541 ALOGD("param too small for blob: length %zu size %zu offset %zu",
542 length, param->size(), offset);
543 break;
544 }
545
546 sp<ABuffer> buf = ABuffer::CreateAsCopy(data, length);
547 value.set(buf);
548 break;
549 }
550
551 default: {
552 size_t valueSize = C2Value::SizeFor((C2Value::type_t)fieldType);
553 if (param->size() < valueSize || param->size() - valueSize < offset) {
554 ALOGD("param too small for c2value: size %zu offset %zu",
555 param->size(), offset);
556 break;
557 }
558
559 C2Value c2Value;
560 switch (fieldType) {
561 case C2FieldDescriptor::INT32: c2Value = *((int32_t *)data); break;
562 case C2FieldDescriptor::UINT32: c2Value = *((uint32_t *)data); break;
563 case C2FieldDescriptor::CNTR32: c2Value = *((c2_cntr32_t *)data); break;
564 case C2FieldDescriptor::INT64: c2Value = *((int64_t *)data); break;
565 case C2FieldDescriptor::UINT64: c2Value = *((uint64_t *)data); break;
566 case C2FieldDescriptor::CNTR64: c2Value = *((c2_cntr64_t *)data); break;
567 case C2FieldDescriptor::FLOAT: c2Value = *((float *)data); break;
568 default:
569 ALOGD("Unsupported data type for %s", name.c_str());
570 continue;
571 }
572 value.set(c2Value);
573 }
574 }
575 ret.emplace(name, value);
576 }
577 return ret;
578 }
579
clear()580 void ReflectedParamUpdater::clear() {
581 mMap.clear();
582 }
583
584 } // namespace android
585