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 "CCodec"
19 #include <utils/Log.h>
20
21 #include <sstream>
22 #include <thread>
23
24 #include <C2Config.h>
25 #include <C2Debug.h>
26 #include <C2ParamInternal.h>
27 #include <C2PlatformSupport.h>
28
29 #include <android/IOMXBufferSource.h>
30 #include <android/hardware/media/c2/1.0/IInputSurface.h>
31 #include <android/hardware/media/omx/1.0/IGraphicBufferSource.h>
32 #include <android/hardware/media/omx/1.0/IOmx.h>
33 #include <android-base/properties.h>
34 #include <android-base/stringprintf.h>
35 #include <cutils/properties.h>
36 #include <gui/IGraphicBufferProducer.h>
37 #include <gui/Surface.h>
38 #include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h>
39 #include <media/omx/1.0/WOmxNode.h>
40 #include <media/openmax/OMX_Core.h>
41 #include <media/openmax/OMX_IndexExt.h>
42 #include <media/stagefright/foundation/avc_utils.h>
43 #include <media/stagefright/omx/1.0/WGraphicBufferSource.h>
44 #include <media/stagefright/omx/OmxGraphicBufferSource.h>
45 #include <media/stagefright/CCodec.h>
46 #include <media/stagefright/BufferProducerWrapper.h>
47 #include <media/stagefright/MediaCodecConstants.h>
48 #include <media/stagefright/PersistentSurface.h>
49 #include <utils/NativeHandle.h>
50
51 #include "C2OMXNode.h"
52 #include "CCodecBufferChannel.h"
53 #include "CCodecConfig.h"
54 #include "Codec2Mapper.h"
55 #include "InputSurfaceWrapper.h"
56
57 extern "C" android::PersistentSurface *CreateInputSurface();
58
59 namespace android {
60
61 using namespace std::chrono_literals;
62 using ::android::hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer;
63 using android::base::StringPrintf;
64 using ::android::hardware::media::c2::V1_0::IInputSurface;
65
66 typedef hardware::media::omx::V1_0::IGraphicBufferSource HGraphicBufferSource;
67 typedef CCodecConfig Config;
68
69 namespace {
70
71 class CCodecWatchdog : public AHandler {
72 private:
73 enum {
74 kWhatWatch,
75 };
76 constexpr static int64_t kWatchIntervalUs = 3300000; // 3.3 secs
77
78 public:
getInstance()79 static sp<CCodecWatchdog> getInstance() {
80 static sp<CCodecWatchdog> instance(new CCodecWatchdog);
81 static std::once_flag flag;
82 // Call Init() only once.
83 std::call_once(flag, Init, instance);
84 return instance;
85 }
86
87 ~CCodecWatchdog() = default;
88
watch(sp<CCodec> codec)89 void watch(sp<CCodec> codec) {
90 bool shouldPost = false;
91 {
92 Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch);
93 // If a watch message is in flight, piggy-back this instance as well.
94 // Otherwise, post a new watch message.
95 shouldPost = codecs->empty();
96 codecs->emplace(codec);
97 }
98 if (shouldPost) {
99 ALOGV("posting watch message");
100 (new AMessage(kWhatWatch, this))->post(kWatchIntervalUs);
101 }
102 }
103
104 protected:
onMessageReceived(const sp<AMessage> & msg)105 void onMessageReceived(const sp<AMessage> &msg) {
106 switch (msg->what()) {
107 case kWhatWatch: {
108 Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch);
109 ALOGV("watch for %zu codecs", codecs->size());
110 for (auto it = codecs->begin(); it != codecs->end(); ++it) {
111 sp<CCodec> codec = it->promote();
112 if (codec == nullptr) {
113 continue;
114 }
115 codec->initiateReleaseIfStuck();
116 }
117 codecs->clear();
118 break;
119 }
120
121 default: {
122 TRESPASS("CCodecWatchdog: unrecognized message");
123 }
124 }
125 }
126
127 private:
CCodecWatchdog()128 CCodecWatchdog() : mLooper(new ALooper) {}
129
Init(const sp<CCodecWatchdog> & thiz)130 static void Init(const sp<CCodecWatchdog> &thiz) {
131 ALOGV("Init");
132 thiz->mLooper->setName("CCodecWatchdog");
133 thiz->mLooper->registerHandler(thiz);
134 thiz->mLooper->start();
135 }
136
137 sp<ALooper> mLooper;
138
139 Mutexed<std::set<wp<CCodec>>> mCodecsToWatch;
140 };
141
142 class C2InputSurfaceWrapper : public InputSurfaceWrapper {
143 public:
C2InputSurfaceWrapper(const std::shared_ptr<Codec2Client::InputSurface> & surface)144 explicit C2InputSurfaceWrapper(
145 const std::shared_ptr<Codec2Client::InputSurface> &surface) :
146 mSurface(surface) {
147 }
148
149 ~C2InputSurfaceWrapper() override = default;
150
connect(const std::shared_ptr<Codec2Client::Component> & comp)151 status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override {
152 if (mConnection != nullptr) {
153 return ALREADY_EXISTS;
154 }
155 return toStatusT(comp->connectToInputSurface(mSurface, &mConnection));
156 }
157
disconnect()158 void disconnect() override {
159 if (mConnection != nullptr) {
160 mConnection->disconnect();
161 mConnection = nullptr;
162 }
163 }
164
start()165 status_t start() override {
166 // InputSurface does not distinguish started state
167 return OK;
168 }
169
signalEndOfInputStream()170 status_t signalEndOfInputStream() override {
171 C2InputSurfaceEosTuning eos(true);
172 std::vector<std::unique_ptr<C2SettingResult>> failures;
173 c2_status_t err = mSurface->config({&eos}, C2_MAY_BLOCK, &failures);
174 if (err != C2_OK) {
175 return UNKNOWN_ERROR;
176 }
177 return OK;
178 }
179
configure(Config & config __unused)180 status_t configure(Config &config __unused) {
181 // TODO
182 return OK;
183 }
184
185 private:
186 std::shared_ptr<Codec2Client::InputSurface> mSurface;
187 std::shared_ptr<Codec2Client::InputSurfaceConnection> mConnection;
188 };
189
190 class GraphicBufferSourceWrapper : public InputSurfaceWrapper {
191 public:
192 typedef hardware::media::omx::V1_0::Status OmxStatus;
193
GraphicBufferSourceWrapper(const sp<HGraphicBufferSource> & source,uint32_t width,uint32_t height,uint64_t usage)194 GraphicBufferSourceWrapper(
195 const sp<HGraphicBufferSource> &source,
196 uint32_t width,
197 uint32_t height,
198 uint64_t usage)
199 : mSource(source), mWidth(width), mHeight(height) {
200 mDataSpace = HAL_DATASPACE_BT709;
201 mConfig.mUsage = usage;
202 }
203 ~GraphicBufferSourceWrapper() override = default;
204
connect(const std::shared_ptr<Codec2Client::Component> & comp)205 status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override {
206 mNode = new C2OMXNode(comp);
207 mOmxNode = new hardware::media::omx::V1_0::utils::TWOmxNode(mNode);
208 mNode->setFrameSize(mWidth, mHeight);
209
210 // Usage is queried during configure(), so setting it beforehand.
211 OMX_U32 usage = mConfig.mUsage & 0xFFFFFFFF;
212 (void)mNode->setParameter(
213 (OMX_INDEXTYPE)OMX_IndexParamConsumerUsageBits,
214 &usage, sizeof(usage));
215
216 return GetStatus(mSource->configure(
217 mOmxNode, static_cast<hardware::graphics::common::V1_0::Dataspace>(mDataSpace)));
218 }
219
disconnect()220 void disconnect() override {
221 if (mNode == nullptr) {
222 return;
223 }
224 sp<IOMXBufferSource> source = mNode->getSource();
225 if (source == nullptr) {
226 ALOGD("GBSWrapper::disconnect: node is not configured with OMXBufferSource.");
227 return;
228 }
229 source->onOmxIdle();
230 source->onOmxLoaded();
231 mNode.clear();
232 mOmxNode.clear();
233 }
234
GetStatus(hardware::Return<OmxStatus> && status)235 status_t GetStatus(hardware::Return<OmxStatus> &&status) {
236 if (status.isOk()) {
237 return static_cast<status_t>(status.withDefault(OmxStatus::UNKNOWN_ERROR));
238 } else if (status.isDeadObject()) {
239 return DEAD_OBJECT;
240 }
241 return UNKNOWN_ERROR;
242 }
243
start()244 status_t start() override {
245 sp<IOMXBufferSource> source = mNode->getSource();
246 if (source == nullptr) {
247 return NO_INIT;
248 }
249
250 size_t numSlots = 16;
251 constexpr OMX_U32 kPortIndexInput = 0;
252
253 OMX_PARAM_PORTDEFINITIONTYPE param;
254 param.nPortIndex = kPortIndexInput;
255 status_t err = mNode->getParameter(OMX_IndexParamPortDefinition,
256 ¶m, sizeof(param));
257 if (err == OK) {
258 numSlots = param.nBufferCountActual;
259 }
260
261 for (size_t i = 0; i < numSlots; ++i) {
262 source->onInputBufferAdded(i);
263 }
264
265 source->onOmxExecuting();
266 return OK;
267 }
268
signalEndOfInputStream()269 status_t signalEndOfInputStream() override {
270 return GetStatus(mSource->signalEndOfInputStream());
271 }
272
configure(Config & config)273 status_t configure(Config &config) {
274 std::stringstream status;
275 status_t err = OK;
276
277 // handle each configuration granually, in case we need to handle part of the configuration
278 // elsewhere
279
280 // TRICKY: we do not unset frame delay repeating
281 if (config.mMinFps > 0 && config.mMinFps != mConfig.mMinFps) {
282 int64_t us = 1e6 / config.mMinFps + 0.5;
283 status_t res = GetStatus(mSource->setRepeatPreviousFrameDelayUs(us));
284 status << " minFps=" << config.mMinFps << " => repeatDelayUs=" << us;
285 if (res != OK) {
286 status << " (=> " << asString(res) << ")";
287 err = res;
288 }
289 mConfig.mMinFps = config.mMinFps;
290 }
291
292 // pts gap
293 if (config.mMinAdjustedFps > 0 || config.mFixedAdjustedFps > 0) {
294 if (mNode != nullptr) {
295 OMX_PARAM_U32TYPE ptrGapParam = {};
296 ptrGapParam.nSize = sizeof(OMX_PARAM_U32TYPE);
297 float gap = (config.mMinAdjustedFps > 0)
298 ? c2_min(INT32_MAX + 0., 1e6 / config.mMinAdjustedFps + 0.5)
299 : c2_max(0. - INT32_MAX, -1e6 / config.mFixedAdjustedFps - 0.5);
300 // float -> uint32_t is undefined if the value is negative.
301 // First convert to int32_t to ensure the expected behavior.
302 ptrGapParam.nU32 = int32_t(gap);
303 (void)mNode->setParameter(
304 (OMX_INDEXTYPE)OMX_IndexParamMaxFrameDurationForBitrateControl,
305 &ptrGapParam, sizeof(ptrGapParam));
306 }
307 }
308
309 // max fps
310 // TRICKY: we do not unset max fps to 0 unless using fixed fps
311 if ((config.mMaxFps > 0 || (config.mFixedAdjustedFps > 0 && config.mMaxFps == -1))
312 && config.mMaxFps != mConfig.mMaxFps) {
313 status_t res = GetStatus(mSource->setMaxFps(config.mMaxFps));
314 status << " maxFps=" << config.mMaxFps;
315 if (res != OK) {
316 status << " (=> " << asString(res) << ")";
317 err = res;
318 }
319 mConfig.mMaxFps = config.mMaxFps;
320 }
321
322 if (config.mTimeOffsetUs != mConfig.mTimeOffsetUs) {
323 status_t res = GetStatus(mSource->setTimeOffsetUs(config.mTimeOffsetUs));
324 status << " timeOffset " << config.mTimeOffsetUs << "us";
325 if (res != OK) {
326 status << " (=> " << asString(res) << ")";
327 err = res;
328 }
329 mConfig.mTimeOffsetUs = config.mTimeOffsetUs;
330 }
331
332 if (config.mCaptureFps != mConfig.mCaptureFps || config.mCodedFps != mConfig.mCodedFps) {
333 status_t res =
334 GetStatus(mSource->setTimeLapseConfig(config.mCodedFps, config.mCaptureFps));
335 status << " timeLapse " << config.mCaptureFps << "fps as " << config.mCodedFps << "fps";
336 if (res != OK) {
337 status << " (=> " << asString(res) << ")";
338 err = res;
339 }
340 mConfig.mCaptureFps = config.mCaptureFps;
341 mConfig.mCodedFps = config.mCodedFps;
342 }
343
344 if (config.mStartAtUs != mConfig.mStartAtUs
345 || (config.mStopped != mConfig.mStopped && !config.mStopped)) {
346 status_t res = GetStatus(mSource->setStartTimeUs(config.mStartAtUs));
347 status << " start at " << config.mStartAtUs << "us";
348 if (res != OK) {
349 status << " (=> " << asString(res) << ")";
350 err = res;
351 }
352 mConfig.mStartAtUs = config.mStartAtUs;
353 mConfig.mStopped = config.mStopped;
354 }
355
356 // suspend-resume
357 if (config.mSuspended != mConfig.mSuspended) {
358 status_t res = GetStatus(mSource->setSuspend(config.mSuspended, config.mSuspendAtUs));
359 status << " " << (config.mSuspended ? "suspend" : "resume")
360 << " at " << config.mSuspendAtUs << "us";
361 if (res != OK) {
362 status << " (=> " << asString(res) << ")";
363 err = res;
364 }
365 mConfig.mSuspended = config.mSuspended;
366 mConfig.mSuspendAtUs = config.mSuspendAtUs;
367 }
368
369 if (config.mStopped != mConfig.mStopped && config.mStopped) {
370 status_t res = GetStatus(mSource->setStopTimeUs(config.mStopAtUs));
371 status << " stop at " << config.mStopAtUs << "us";
372 if (res != OK) {
373 status << " (=> " << asString(res) << ")";
374 err = res;
375 } else {
376 status << " delayUs";
377 hardware::Return<void> trans = mSource->getStopTimeOffsetUs(
378 [&res, &delayUs = config.mInputDelayUs](
379 auto status, auto stopTimeOffsetUs) {
380 res = static_cast<status_t>(status);
381 delayUs = stopTimeOffsetUs;
382 });
383 if (!trans.isOk()) {
384 res = trans.isDeadObject() ? DEAD_OBJECT : UNKNOWN_ERROR;
385 }
386 if (res != OK) {
387 status << " (=> " << asString(res) << ")";
388 } else {
389 status << "=" << config.mInputDelayUs << "us";
390 }
391 mConfig.mInputDelayUs = config.mInputDelayUs;
392 }
393 mConfig.mStopAtUs = config.mStopAtUs;
394 mConfig.mStopped = config.mStopped;
395 }
396
397 // color aspects (android._color-aspects)
398
399 // consumer usage is queried earlier.
400
401 // priority
402 if (mConfig.mPriority != config.mPriority) {
403 if (config.mPriority != INT_MAX) {
404 mNode->setPriority(config.mPriority);
405 }
406 mConfig.mPriority = config.mPriority;
407 }
408
409 if (status.str().empty()) {
410 ALOGD("ISConfig not changed");
411 } else {
412 ALOGD("ISConfig%s", status.str().c_str());
413 }
414 return err;
415 }
416
onInputBufferDone(c2_cntr64_t index)417 void onInputBufferDone(c2_cntr64_t index) override {
418 mNode->onInputBufferDone(index);
419 }
420
getDataspace()421 android_dataspace getDataspace() override {
422 return mNode->getDataspace();
423 }
424
425 private:
426 sp<HGraphicBufferSource> mSource;
427 sp<C2OMXNode> mNode;
428 sp<hardware::media::omx::V1_0::IOmxNode> mOmxNode;
429 uint32_t mWidth;
430 uint32_t mHeight;
431 Config mConfig;
432 };
433
434 class Codec2ClientInterfaceWrapper : public C2ComponentStore {
435 std::shared_ptr<Codec2Client> mClient;
436
437 public:
Codec2ClientInterfaceWrapper(std::shared_ptr<Codec2Client> client)438 Codec2ClientInterfaceWrapper(std::shared_ptr<Codec2Client> client)
439 : mClient(client) { }
440
441 virtual ~Codec2ClientInterfaceWrapper() = default;
442
config_sm(const std::vector<C2Param * > & params,std::vector<std::unique_ptr<C2SettingResult>> * const failures)443 virtual c2_status_t config_sm(
444 const std::vector<C2Param *> ¶ms,
445 std::vector<std::unique_ptr<C2SettingResult>> *const failures) {
446 return mClient->config(params, C2_MAY_BLOCK, failures);
447 };
448
copyBuffer(std::shared_ptr<C2GraphicBuffer>,std::shared_ptr<C2GraphicBuffer>)449 virtual c2_status_t copyBuffer(
450 std::shared_ptr<C2GraphicBuffer>,
451 std::shared_ptr<C2GraphicBuffer>) {
452 return C2_OMITTED;
453 }
454
createComponent(C2String,std::shared_ptr<C2Component> * const component)455 virtual c2_status_t createComponent(
456 C2String, std::shared_ptr<C2Component> *const component) {
457 component->reset();
458 return C2_OMITTED;
459 }
460
createInterface(C2String,std::shared_ptr<C2ComponentInterface> * const interface)461 virtual c2_status_t createInterface(
462 C2String, std::shared_ptr<C2ComponentInterface> *const interface) {
463 interface->reset();
464 return C2_OMITTED;
465 }
466
query_sm(const std::vector<C2Param * > & stackParams,const std::vector<C2Param::Index> & heapParamIndices,std::vector<std::unique_ptr<C2Param>> * const heapParams) const467 virtual c2_status_t query_sm(
468 const std::vector<C2Param *> &stackParams,
469 const std::vector<C2Param::Index> &heapParamIndices,
470 std::vector<std::unique_ptr<C2Param>> *const heapParams) const {
471 return mClient->query(stackParams, heapParamIndices, C2_MAY_BLOCK, heapParams);
472 }
473
querySupportedParams_nb(std::vector<std::shared_ptr<C2ParamDescriptor>> * const params) const474 virtual c2_status_t querySupportedParams_nb(
475 std::vector<std::shared_ptr<C2ParamDescriptor>> *const params) const {
476 return mClient->querySupportedParams(params);
477 }
478
querySupportedValues_sm(std::vector<C2FieldSupportedValuesQuery> & fields) const479 virtual c2_status_t querySupportedValues_sm(
480 std::vector<C2FieldSupportedValuesQuery> &fields) const {
481 return mClient->querySupportedValues(fields, C2_MAY_BLOCK);
482 }
483
getName() const484 virtual C2String getName() const {
485 return mClient->getName();
486 }
487
getParamReflector() const488 virtual std::shared_ptr<C2ParamReflector> getParamReflector() const {
489 return mClient->getParamReflector();
490 }
491
listComponents()492 virtual std::vector<std::shared_ptr<const C2Component::Traits>> listComponents() {
493 return std::vector<std::shared_ptr<const C2Component::Traits>>();
494 }
495 };
496
RevertOutputFormatIfNeeded(const sp<AMessage> & oldFormat,sp<AMessage> & currentFormat)497 void RevertOutputFormatIfNeeded(
498 const sp<AMessage> &oldFormat, sp<AMessage> ¤tFormat) {
499 // We used to not report changes to these keys to the client.
500 const static std::set<std::string> sIgnoredKeys({
501 KEY_BIT_RATE,
502 KEY_FRAME_RATE,
503 KEY_MAX_BIT_RATE,
504 KEY_MAX_WIDTH,
505 KEY_MAX_HEIGHT,
506 "csd-0",
507 "csd-1",
508 "csd-2",
509 });
510 if (currentFormat == oldFormat) {
511 return;
512 }
513 sp<AMessage> diff = currentFormat->changesFrom(oldFormat);
514 AMessage::Type type;
515 for (size_t i = diff->countEntries(); i > 0; --i) {
516 if (sIgnoredKeys.count(diff->getEntryNameAt(i - 1, &type)) > 0) {
517 diff->removeEntryAt(i - 1);
518 }
519 }
520 if (diff->countEntries() == 0) {
521 currentFormat = oldFormat;
522 }
523 }
524
AmendOutputFormatWithCodecSpecificData(const uint8_t * data,size_t size,const std::string & mediaType,const sp<AMessage> & outputFormat)525 void AmendOutputFormatWithCodecSpecificData(
526 const uint8_t *data, size_t size, const std::string &mediaType,
527 const sp<AMessage> &outputFormat) {
528 if (mediaType == MIMETYPE_VIDEO_AVC) {
529 // Codec specific data should be SPS and PPS in a single buffer,
530 // each prefixed by a startcode (0x00 0x00 0x00 0x01).
531 // We separate the two and put them into the output format
532 // under the keys "csd-0" and "csd-1".
533
534 unsigned csdIndex = 0;
535
536 const uint8_t *nalStart;
537 size_t nalSize;
538 while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) {
539 sp<ABuffer> csd = new ABuffer(nalSize + 4);
540 memcpy(csd->data(), "\x00\x00\x00\x01", 4);
541 memcpy(csd->data() + 4, nalStart, nalSize);
542
543 outputFormat->setBuffer(
544 AStringPrintf("csd-%u", csdIndex).c_str(), csd);
545
546 ++csdIndex;
547 }
548
549 if (csdIndex != 2) {
550 ALOGW("Expected two NAL units from AVC codec config, but %u found",
551 csdIndex);
552 }
553 } else {
554 // For everything else we just stash the codec specific data into
555 // the output format as a single piece of csd under "csd-0".
556 sp<ABuffer> csd = new ABuffer(size);
557 memcpy(csd->data(), data, size);
558 csd->setRange(0, size);
559 outputFormat->setBuffer("csd-0", csd);
560 }
561 }
562
563 } // namespace
564
565 // CCodec::ClientListener
566
567 struct CCodec::ClientListener : public Codec2Client::Listener {
568
ClientListenerandroid::CCodec::ClientListener569 explicit ClientListener(const wp<CCodec> &codec) : mCodec(codec) {}
570
onWorkDoneandroid::CCodec::ClientListener571 virtual void onWorkDone(
572 const std::weak_ptr<Codec2Client::Component>& component,
573 std::list<std::unique_ptr<C2Work>>& workItems) override {
574 (void)component;
575 sp<CCodec> codec(mCodec.promote());
576 if (!codec) {
577 return;
578 }
579 codec->onWorkDone(workItems);
580 }
581
onTrippedandroid::CCodec::ClientListener582 virtual void onTripped(
583 const std::weak_ptr<Codec2Client::Component>& component,
584 const std::vector<std::shared_ptr<C2SettingResult>>& settingResult
585 ) override {
586 // TODO
587 (void)component;
588 (void)settingResult;
589 }
590
onErrorandroid::CCodec::ClientListener591 virtual void onError(
592 const std::weak_ptr<Codec2Client::Component>& component,
593 uint32_t errorCode) override {
594 {
595 // Component is only used for reporting as we use a separate listener for each instance
596 std::shared_ptr<Codec2Client::Component> comp = component.lock();
597 if (!comp) {
598 ALOGD("Component died with error: 0x%x", errorCode);
599 } else {
600 ALOGD("Component \"%s\" returned error: 0x%x", comp->getName().c_str(), errorCode);
601 }
602 }
603
604 // Report to MediaCodec
605 // Note: for now we do not propagate the error code to MediaCodec
606 // except for C2_NO_MEMORY, as we would need to translate to a MediaCodec error.
607 sp<CCodec> codec(mCodec.promote());
608 if (!codec || !codec->mCallback) {
609 return;
610 }
611 codec->mCallback->onError(
612 errorCode == C2_NO_MEMORY ? NO_MEMORY : UNKNOWN_ERROR,
613 ACTION_CODE_FATAL);
614 }
615
onDeathandroid::CCodec::ClientListener616 virtual void onDeath(
617 const std::weak_ptr<Codec2Client::Component>& component) override {
618 { // Log the death of the component.
619 std::shared_ptr<Codec2Client::Component> comp = component.lock();
620 if (!comp) {
621 ALOGE("Codec2 component died.");
622 } else {
623 ALOGE("Codec2 component \"%s\" died.", comp->getName().c_str());
624 }
625 }
626
627 // Report to MediaCodec.
628 sp<CCodec> codec(mCodec.promote());
629 if (!codec || !codec->mCallback) {
630 return;
631 }
632 codec->mCallback->onError(DEAD_OBJECT, ACTION_CODE_FATAL);
633 }
634
onFrameRenderedandroid::CCodec::ClientListener635 virtual void onFrameRendered(uint64_t bufferQueueId,
636 int32_t slotId,
637 int64_t timestampNs) override {
638 // TODO: implement
639 (void)bufferQueueId;
640 (void)slotId;
641 (void)timestampNs;
642 }
643
onInputBufferDoneandroid::CCodec::ClientListener644 virtual void onInputBufferDone(
645 uint64_t frameIndex, size_t arrayIndex) override {
646 sp<CCodec> codec(mCodec.promote());
647 if (codec) {
648 codec->onInputBufferDone(frameIndex, arrayIndex);
649 }
650 }
651
652 private:
653 wp<CCodec> mCodec;
654 };
655
656 // CCodecCallbackImpl
657
658 class CCodecCallbackImpl : public CCodecCallback {
659 public:
CCodecCallbackImpl(CCodec * codec)660 explicit CCodecCallbackImpl(CCodec *codec) : mCodec(codec) {}
661 ~CCodecCallbackImpl() override = default;
662
onError(status_t err,enum ActionCode actionCode)663 void onError(status_t err, enum ActionCode actionCode) override {
664 mCodec->mCallback->onError(err, actionCode);
665 }
666
onOutputFramesRendered(int64_t mediaTimeUs,nsecs_t renderTimeNs)667 void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) override {
668 mCodec->mCallback->onOutputFramesRendered(
669 {RenderedFrameInfo(mediaTimeUs, renderTimeNs)});
670 }
671
onOutputBuffersChanged()672 void onOutputBuffersChanged() override {
673 mCodec->mCallback->onOutputBuffersChanged();
674 }
675
onFirstTunnelFrameReady()676 void onFirstTunnelFrameReady() override {
677 mCodec->mCallback->onFirstTunnelFrameReady();
678 }
679
680 private:
681 CCodec *mCodec;
682 };
683
684 // CCodec
685
CCodec()686 CCodec::CCodec()
687 : mChannel(new CCodecBufferChannel(std::make_shared<CCodecCallbackImpl>(this))),
688 mConfig(new CCodecConfig) {
689 }
690
~CCodec()691 CCodec::~CCodec() {
692 }
693
getBufferChannel()694 std::shared_ptr<BufferChannelBase> CCodec::getBufferChannel() {
695 return mChannel;
696 }
697
tryAndReportOnError(std::function<status_t ()> job)698 status_t CCodec::tryAndReportOnError(std::function<status_t()> job) {
699 status_t err = job();
700 if (err != C2_OK) {
701 mCallback->onError(err, ACTION_CODE_FATAL);
702 }
703 return err;
704 }
705
initiateAllocateComponent(const sp<AMessage> & msg)706 void CCodec::initiateAllocateComponent(const sp<AMessage> &msg) {
707 auto setAllocating = [this] {
708 Mutexed<State>::Locked state(mState);
709 if (state->get() != RELEASED) {
710 return INVALID_OPERATION;
711 }
712 state->set(ALLOCATING);
713 return OK;
714 };
715 if (tryAndReportOnError(setAllocating) != OK) {
716 return;
717 }
718
719 sp<RefBase> codecInfo;
720 CHECK(msg->findObject("codecInfo", &codecInfo));
721 // For Codec 2.0 components, componentName == codecInfo->getCodecName().
722
723 sp<AMessage> allocMsg(new AMessage(kWhatAllocate, this));
724 allocMsg->setObject("codecInfo", codecInfo);
725 allocMsg->post();
726 }
727
allocate(const sp<MediaCodecInfo> & codecInfo)728 void CCodec::allocate(const sp<MediaCodecInfo> &codecInfo) {
729 if (codecInfo == nullptr) {
730 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
731 return;
732 }
733 ALOGD("allocate(%s)", codecInfo->getCodecName());
734 mClientListener.reset(new ClientListener(this));
735
736 AString componentName = codecInfo->getCodecName();
737 std::shared_ptr<Codec2Client> client;
738
739 // set up preferred component store to access vendor store parameters
740 client = Codec2Client::CreateFromService("default");
741 if (client) {
742 ALOGI("setting up '%s' as default (vendor) store", client->getServiceName().c_str());
743 SetPreferredCodec2ComponentStore(
744 std::make_shared<Codec2ClientInterfaceWrapper>(client));
745 }
746
747 std::shared_ptr<Codec2Client::Component> comp;
748 c2_status_t status = Codec2Client::CreateComponentByName(
749 componentName.c_str(),
750 mClientListener,
751 &comp,
752 &client);
753 if (status != C2_OK) {
754 ALOGE("Failed Create component: %s, error=%d", componentName.c_str(), status);
755 Mutexed<State>::Locked state(mState);
756 state->set(RELEASED);
757 state.unlock();
758 mCallback->onError((status == C2_NO_MEMORY ? NO_MEMORY : UNKNOWN_ERROR), ACTION_CODE_FATAL);
759 state.lock();
760 return;
761 }
762 ALOGI("Created component [%s]", componentName.c_str());
763 mChannel->setComponent(comp);
764 auto setAllocated = [this, comp, client] {
765 Mutexed<State>::Locked state(mState);
766 if (state->get() != ALLOCATING) {
767 state->set(RELEASED);
768 return UNKNOWN_ERROR;
769 }
770 state->set(ALLOCATED);
771 state->comp = comp;
772 mClient = client;
773 return OK;
774 };
775 if (tryAndReportOnError(setAllocated) != OK) {
776 return;
777 }
778
779 // initialize config here in case setParameters is called prior to configure
780 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
781 const std::unique_ptr<Config> &config = *configLocked;
782 status_t err = config->initialize(mClient->getParamReflector(), comp);
783 if (err != OK) {
784 ALOGW("Failed to initialize configuration support");
785 // TODO: report error once we complete implementation.
786 }
787 config->queryConfiguration(comp);
788
789 mCallback->onComponentAllocated(componentName.c_str());
790 }
791
initiateConfigureComponent(const sp<AMessage> & format)792 void CCodec::initiateConfigureComponent(const sp<AMessage> &format) {
793 auto checkAllocated = [this] {
794 Mutexed<State>::Locked state(mState);
795 return (state->get() != ALLOCATED) ? UNKNOWN_ERROR : OK;
796 };
797 if (tryAndReportOnError(checkAllocated) != OK) {
798 return;
799 }
800
801 sp<AMessage> msg(new AMessage(kWhatConfigure, this));
802 msg->setMessage("format", format);
803 msg->post();
804 }
805
configure(const sp<AMessage> & msg)806 void CCodec::configure(const sp<AMessage> &msg) {
807 std::shared_ptr<Codec2Client::Component> comp;
808 auto checkAllocated = [this, &comp] {
809 Mutexed<State>::Locked state(mState);
810 if (state->get() != ALLOCATED) {
811 state->set(RELEASED);
812 return UNKNOWN_ERROR;
813 }
814 comp = state->comp;
815 return OK;
816 };
817 if (tryAndReportOnError(checkAllocated) != OK) {
818 return;
819 }
820
821 auto doConfig = [msg, comp, this]() -> status_t {
822 AString mime;
823 if (!msg->findString("mime", &mime)) {
824 return BAD_VALUE;
825 }
826
827 int32_t encoder;
828 if (!msg->findInt32("encoder", &encoder)) {
829 encoder = false;
830 }
831
832 int32_t flags;
833 if (!msg->findInt32("flags", &flags)) {
834 return BAD_VALUE;
835 }
836
837 // TODO: read from intf()
838 if ((!encoder) != (comp->getName().find("encoder") == std::string::npos)) {
839 return UNKNOWN_ERROR;
840 }
841
842 int32_t storeMeta;
843 if (encoder
844 && msg->findInt32("android._input-metadata-buffer-type", &storeMeta)
845 && storeMeta != kMetadataBufferTypeInvalid) {
846 if (storeMeta != kMetadataBufferTypeANWBuffer) {
847 ALOGD("Only ANW buffers are supported for legacy metadata mode");
848 return BAD_VALUE;
849 }
850 mChannel->setMetaMode(CCodecBufferChannel::MODE_ANW);
851 }
852
853 status_t err = OK;
854 sp<RefBase> obj;
855 sp<Surface> surface;
856 if (msg->findObject("native-window", &obj)) {
857 surface = static_cast<Surface *>(obj.get());
858 // setup tunneled playback
859 if (surface != nullptr) {
860 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
861 const std::unique_ptr<Config> &config = *configLocked;
862 if ((config->mDomain & Config::IS_DECODER)
863 && (config->mDomain & Config::IS_VIDEO)) {
864 int32_t tunneled;
865 if (msg->findInt32("feature-tunneled-playback", &tunneled) && tunneled != 0) {
866 ALOGI("Configuring TUNNELED video playback.");
867
868 err = configureTunneledVideoPlayback(comp, &config->mSidebandHandle, msg);
869 if (err != OK) {
870 ALOGE("configureTunneledVideoPlayback failed!");
871 return err;
872 }
873 config->mTunneled = true;
874 }
875
876 int32_t pushBlankBuffersOnStop = 0;
877 if (msg->findInt32(KEY_PUSH_BLANK_BUFFERS_ON_STOP, &pushBlankBuffersOnStop)) {
878 config->mPushBlankBuffersOnStop = pushBlankBuffersOnStop == 1;
879 }
880 // secure compoment or protected content default with
881 // "push-blank-buffers-on-shutdown" flag
882 if (!config->mPushBlankBuffersOnStop) {
883 int32_t usageProtected;
884 if (comp->getName().find(".secure") != std::string::npos) {
885 config->mPushBlankBuffersOnStop = true;
886 } else if (msg->findInt32("protected", &usageProtected) && usageProtected) {
887 config->mPushBlankBuffersOnStop = true;
888 }
889 }
890 }
891 }
892 setSurface(surface);
893 }
894
895 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
896 const std::unique_ptr<Config> &config = *configLocked;
897 config->mUsingSurface = surface != nullptr;
898 config->mBuffersBoundToCodec = ((flags & CONFIGURE_FLAG_USE_BLOCK_MODEL) == 0);
899 ALOGD("[%s] buffers are %sbound to CCodec for this session",
900 comp->getName().c_str(), config->mBuffersBoundToCodec ? "" : "not ");
901
902 // Enforce required parameters
903 int32_t i32;
904 float flt;
905 if (config->mDomain & Config::IS_AUDIO) {
906 if (!msg->findInt32(KEY_SAMPLE_RATE, &i32)) {
907 ALOGD("sample rate is missing, which is required for audio components.");
908 return BAD_VALUE;
909 }
910 if (!msg->findInt32(KEY_CHANNEL_COUNT, &i32)) {
911 ALOGD("channel count is missing, which is required for audio components.");
912 return BAD_VALUE;
913 }
914 if ((config->mDomain & Config::IS_ENCODER)
915 && !mime.equalsIgnoreCase(MEDIA_MIMETYPE_AUDIO_FLAC)
916 && !msg->findInt32(KEY_BIT_RATE, &i32)
917 && !msg->findFloat(KEY_BIT_RATE, &flt)) {
918 ALOGD("bitrate is missing, which is required for audio encoders.");
919 return BAD_VALUE;
920 }
921 }
922 int32_t width = 0;
923 int32_t height = 0;
924 if (config->mDomain & (Config::IS_IMAGE | Config::IS_VIDEO)) {
925 if (!msg->findInt32(KEY_WIDTH, &width)) {
926 ALOGD("width is missing, which is required for image/video components.");
927 return BAD_VALUE;
928 }
929 if (!msg->findInt32(KEY_HEIGHT, &height)) {
930 ALOGD("height is missing, which is required for image/video components.");
931 return BAD_VALUE;
932 }
933 if ((config->mDomain & Config::IS_ENCODER) && (config->mDomain & Config::IS_VIDEO)) {
934 int32_t mode = BITRATE_MODE_VBR;
935 if (msg->findInt32(KEY_BITRATE_MODE, &mode) && mode == BITRATE_MODE_CQ) {
936 if (!msg->findInt32(KEY_QUALITY, &i32)) {
937 ALOGD("quality is missing, which is required for video encoders in CQ.");
938 return BAD_VALUE;
939 }
940 } else {
941 if (!msg->findInt32(KEY_BIT_RATE, &i32)
942 && !msg->findFloat(KEY_BIT_RATE, &flt)) {
943 ALOGD("bitrate is missing, which is required for video encoders.");
944 return BAD_VALUE;
945 }
946 }
947 if (!msg->findInt32(KEY_I_FRAME_INTERVAL, &i32)
948 && !msg->findFloat(KEY_I_FRAME_INTERVAL, &flt)) {
949 ALOGD("I frame interval is missing, which is required for video encoders.");
950 return BAD_VALUE;
951 }
952 if (!msg->findInt32(KEY_FRAME_RATE, &i32)
953 && !msg->findFloat(KEY_FRAME_RATE, &flt)) {
954 ALOGD("frame rate is missing, which is required for video encoders.");
955 return BAD_VALUE;
956 }
957 }
958 }
959
960 /*
961 * Handle input surface configuration
962 */
963 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))
964 && (config->mDomain & Config::IS_ENCODER)) {
965 config->mISConfig.reset(new InputSurfaceWrapper::Config{});
966 {
967 config->mISConfig->mMinFps = 0;
968 int64_t value;
969 if (msg->findInt64(KEY_REPEAT_PREVIOUS_FRAME_AFTER, &value) && value > 0) {
970 config->mISConfig->mMinFps = 1e6 / value;
971 }
972 if (!msg->findFloat(
973 KEY_MAX_FPS_TO_ENCODER, &config->mISConfig->mMaxFps)) {
974 config->mISConfig->mMaxFps = -1;
975 }
976 config->mISConfig->mMinAdjustedFps = 0;
977 config->mISConfig->mFixedAdjustedFps = 0;
978 if (msg->findInt64(KEY_MAX_PTS_GAP_TO_ENCODER, &value)) {
979 if (value < 0 && value >= INT32_MIN) {
980 config->mISConfig->mFixedAdjustedFps = -1e6 / value;
981 config->mISConfig->mMaxFps = -1;
982 } else if (value > 0 && value <= INT32_MAX) {
983 config->mISConfig->mMinAdjustedFps = 1e6 / value;
984 }
985 }
986 }
987
988 {
989 bool captureFpsFound = false;
990 double timeLapseFps;
991 float captureRate;
992 if (msg->findDouble("time-lapse-fps", &timeLapseFps)) {
993 config->mISConfig->mCaptureFps = timeLapseFps;
994 captureFpsFound = true;
995 } else if (msg->findAsFloat(KEY_CAPTURE_RATE, &captureRate)) {
996 config->mISConfig->mCaptureFps = captureRate;
997 captureFpsFound = true;
998 }
999 if (captureFpsFound) {
1000 (void)msg->findAsFloat(KEY_FRAME_RATE, &config->mISConfig->mCodedFps);
1001 }
1002 }
1003
1004 {
1005 config->mISConfig->mSuspended = false;
1006 config->mISConfig->mSuspendAtUs = -1;
1007 int32_t value;
1008 if (msg->findInt32(KEY_CREATE_INPUT_SURFACE_SUSPENDED, &value) && value) {
1009 config->mISConfig->mSuspended = true;
1010 }
1011 }
1012 config->mISConfig->mUsage = 0;
1013 config->mISConfig->mPriority = INT_MAX;
1014 }
1015
1016 /*
1017 * Handle desired color format.
1018 */
1019 int32_t defaultColorFormat = COLOR_FormatYUV420Flexible;
1020 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) {
1021 int32_t format = 0;
1022 // Query vendor format for Flexible YUV
1023 std::vector<std::unique_ptr<C2Param>> heapParams;
1024 C2StoreFlexiblePixelFormatDescriptorsInfo *pixelFormatInfo = nullptr;
1025 int vendorSdkVersion = base::GetIntProperty(
1026 "ro.vendor.build.version.sdk", android_get_device_api_level());
1027 if (vendorSdkVersion >= __ANDROID_API_S__ && mClient->query(
1028 {},
1029 {C2StoreFlexiblePixelFormatDescriptorsInfo::PARAM_TYPE},
1030 C2_MAY_BLOCK,
1031 &heapParams) == C2_OK
1032 && heapParams.size() == 1u) {
1033 pixelFormatInfo = C2StoreFlexiblePixelFormatDescriptorsInfo::From(
1034 heapParams[0].get());
1035 } else {
1036 pixelFormatInfo = nullptr;
1037 }
1038 // bit depth -> format
1039 std::map<uint32_t, uint32_t> flexPixelFormat;
1040 std::map<uint32_t, uint32_t> flexPlanarPixelFormat;
1041 std::map<uint32_t, uint32_t> flexSemiPlanarPixelFormat;
1042 if (pixelFormatInfo && *pixelFormatInfo) {
1043 for (size_t i = 0; i < pixelFormatInfo->flexCount(); ++i) {
1044 const C2FlexiblePixelFormatDescriptorStruct &desc =
1045 pixelFormatInfo->m.values[i];
1046 if (desc.subsampling != C2Color::YUV_420
1047 // TODO(b/180076105): some device report wrong layout
1048 // || desc.layout == C2Color::INTERLEAVED_PACKED
1049 // || desc.layout == C2Color::INTERLEAVED_ALIGNED
1050 || desc.layout == C2Color::UNKNOWN_LAYOUT) {
1051 continue;
1052 }
1053 if (flexPixelFormat.count(desc.bitDepth) == 0) {
1054 flexPixelFormat.emplace(desc.bitDepth, desc.pixelFormat);
1055 }
1056 if (desc.layout == C2Color::PLANAR_PACKED
1057 && flexPlanarPixelFormat.count(desc.bitDepth) == 0) {
1058 flexPlanarPixelFormat.emplace(desc.bitDepth, desc.pixelFormat);
1059 }
1060 if (desc.layout == C2Color::SEMIPLANAR_PACKED
1061 && flexSemiPlanarPixelFormat.count(desc.bitDepth) == 0) {
1062 flexSemiPlanarPixelFormat.emplace(desc.bitDepth, desc.pixelFormat);
1063 }
1064 }
1065 }
1066 if (!msg->findInt32(KEY_COLOR_FORMAT, &format)) {
1067 // Also handle default color format (encoders require color format, so this is only
1068 // needed for decoders.
1069 if (!(config->mDomain & Config::IS_ENCODER)) {
1070 if (surface == nullptr) {
1071 const char *prefix = "";
1072 if (flexSemiPlanarPixelFormat.count(8) != 0) {
1073 format = COLOR_FormatYUV420SemiPlanar;
1074 prefix = "semi-";
1075 } else {
1076 format = COLOR_FormatYUV420Planar;
1077 }
1078 ALOGD("Client requested ByteBuffer mode decoder w/o color format set: "
1079 "using default %splanar color format", prefix);
1080 } else {
1081 format = COLOR_FormatSurface;
1082 }
1083 defaultColorFormat = format;
1084 }
1085 } else {
1086 if ((config->mDomain & Config::IS_ENCODER) || !surface) {
1087 if (vendorSdkVersion < __ANDROID_API_S__ &&
1088 (format == COLOR_FormatYUV420Flexible ||
1089 format == COLOR_FormatYUV420Planar ||
1090 format == COLOR_FormatYUV420PackedPlanar ||
1091 format == COLOR_FormatYUV420SemiPlanar ||
1092 format == COLOR_FormatYUV420PackedSemiPlanar)) {
1093 // pre-S framework used to map these color formats into YV12.
1094 // Codecs from older vendor partition may be relying on
1095 // this assumption.
1096 format = HAL_PIXEL_FORMAT_YV12;
1097 }
1098 switch (format) {
1099 case COLOR_FormatYUV420Flexible:
1100 format = COLOR_FormatYUV420Planar;
1101 if (flexPixelFormat.count(8) != 0) {
1102 format = flexPixelFormat[8];
1103 }
1104 break;
1105 case COLOR_FormatYUV420Planar:
1106 case COLOR_FormatYUV420PackedPlanar:
1107 if (flexPlanarPixelFormat.count(8) != 0) {
1108 format = flexPlanarPixelFormat[8];
1109 } else if (flexPixelFormat.count(8) != 0) {
1110 format = flexPixelFormat[8];
1111 }
1112 break;
1113 case COLOR_FormatYUV420SemiPlanar:
1114 case COLOR_FormatYUV420PackedSemiPlanar:
1115 if (flexSemiPlanarPixelFormat.count(8) != 0) {
1116 format = flexSemiPlanarPixelFormat[8];
1117 } else if (flexPixelFormat.count(8) != 0) {
1118 format = flexPixelFormat[8];
1119 }
1120 break;
1121 case COLOR_FormatYUVP010:
1122 format = COLOR_FormatYUVP010;
1123 if (flexSemiPlanarPixelFormat.count(10) != 0) {
1124 format = flexSemiPlanarPixelFormat[10];
1125 } else if (flexPixelFormat.count(10) != 0) {
1126 format = flexPixelFormat[10];
1127 }
1128 break;
1129 default:
1130 // No-op
1131 break;
1132 }
1133 }
1134 }
1135
1136 if (format != 0) {
1137 msg->setInt32("android._color-format", format);
1138 }
1139 }
1140
1141 /*
1142 * Handle dataspace
1143 */
1144 int32_t usingRecorder;
1145 if (msg->findInt32("android._using-recorder", &usingRecorder) && usingRecorder) {
1146 android_dataspace dataSpace = HAL_DATASPACE_BT709;
1147 int32_t width, height;
1148 if (msg->findInt32("width", &width)
1149 && msg->findInt32("height", &height)) {
1150 ColorAspects aspects;
1151 getColorAspectsFromFormat(msg, aspects);
1152 setDefaultCodecColorAspectsIfNeeded(aspects, width, height);
1153 // TODO: read dataspace / color aspect from the component
1154 setColorAspectsIntoFormat(aspects, const_cast<sp<AMessage> &>(msg));
1155 dataSpace = getDataSpaceForColorAspects(aspects, true /* mayexpand */);
1156 }
1157 msg->setInt32("android._dataspace", (int32_t)dataSpace);
1158 ALOGD("setting dataspace to %x", dataSpace);
1159 }
1160
1161 int32_t subscribeToAllVendorParams;
1162 if (msg->findInt32("x-*", &subscribeToAllVendorParams) && subscribeToAllVendorParams) {
1163 if (config->subscribeToAllVendorParams(comp, C2_MAY_BLOCK) != OK) {
1164 ALOGD("[%s] Failed to subscribe to all vendor params", comp->getName().c_str());
1165 }
1166 }
1167
1168 std::vector<std::unique_ptr<C2Param>> configUpdate;
1169 // NOTE: We used to ignore "video-bitrate" at configure; replicate
1170 // the behavior here.
1171 sp<AMessage> sdkParams = msg;
1172 int32_t videoBitrate;
1173 if (sdkParams->findInt32(PARAMETER_KEY_VIDEO_BITRATE, &videoBitrate)) {
1174 sdkParams = msg->dup();
1175 sdkParams->removeEntryAt(sdkParams->findEntryByName(PARAMETER_KEY_VIDEO_BITRATE));
1176 }
1177 err = config->getConfigUpdateFromSdkParams(
1178 comp, sdkParams, Config::IS_CONFIG, C2_DONT_BLOCK, &configUpdate);
1179 if (err != OK) {
1180 ALOGW("failed to convert configuration to c2 params");
1181 }
1182
1183 int32_t maxBframes = 0;
1184 if ((config->mDomain & Config::IS_ENCODER)
1185 && (config->mDomain & Config::IS_VIDEO)
1186 && sdkParams->findInt32(KEY_MAX_B_FRAMES, &maxBframes)
1187 && maxBframes > 0) {
1188 std::unique_ptr<C2StreamGopTuning::output> gop =
1189 C2StreamGopTuning::output::AllocUnique(2 /* flexCount */, 0u /* stream */);
1190 gop->m.values[0] = { P_FRAME, UINT32_MAX };
1191 gop->m.values[1] = {
1192 C2Config::picture_type_t(P_FRAME | B_FRAME),
1193 uint32_t(maxBframes)
1194 };
1195 configUpdate.push_back(std::move(gop));
1196 }
1197
1198 if ((config->mDomain & Config::IS_ENCODER)
1199 && (config->mDomain & Config::IS_VIDEO)) {
1200 // we may not use all 3 of these entries
1201 std::unique_ptr<C2StreamPictureQuantizationTuning::output> qp =
1202 C2StreamPictureQuantizationTuning::output::AllocUnique(3 /* flexCount */,
1203 0u /* stream */);
1204
1205 int ix = 0;
1206
1207 int32_t iMax = INT32_MAX;
1208 int32_t iMin = INT32_MIN;
1209 (void) sdkParams->findInt32(KEY_VIDEO_QP_I_MAX, &iMax);
1210 (void) sdkParams->findInt32(KEY_VIDEO_QP_I_MIN, &iMin);
1211 if (iMax != INT32_MAX || iMin != INT32_MIN) {
1212 qp->m.values[ix++] = {I_FRAME, iMin, iMax};
1213 }
1214
1215 int32_t pMax = INT32_MAX;
1216 int32_t pMin = INT32_MIN;
1217 (void) sdkParams->findInt32(KEY_VIDEO_QP_P_MAX, &pMax);
1218 (void) sdkParams->findInt32(KEY_VIDEO_QP_P_MIN, &pMin);
1219 if (pMax != INT32_MAX || pMin != INT32_MIN) {
1220 qp->m.values[ix++] = {P_FRAME, pMin, pMax};
1221 }
1222
1223 int32_t bMax = INT32_MAX;
1224 int32_t bMin = INT32_MIN;
1225 (void) sdkParams->findInt32(KEY_VIDEO_QP_B_MAX, &bMax);
1226 (void) sdkParams->findInt32(KEY_VIDEO_QP_B_MIN, &bMin);
1227 if (bMax != INT32_MAX || bMin != INT32_MIN) {
1228 qp->m.values[ix++] = {B_FRAME, bMin, bMax};
1229 }
1230
1231 // adjust to reflect actual use.
1232 qp->setFlexCount(ix);
1233
1234 configUpdate.push_back(std::move(qp));
1235 }
1236
1237 int32_t background = 0;
1238 if ((config->mDomain & Config::IS_VIDEO)
1239 && msg->findInt32("android._background-mode", &background)
1240 && background) {
1241 androidSetThreadPriority(gettid(), ANDROID_PRIORITY_BACKGROUND);
1242 if (config->mISConfig) {
1243 config->mISConfig->mPriority = ANDROID_PRIORITY_BACKGROUND;
1244 }
1245 }
1246
1247 err = config->setParameters(comp, configUpdate, C2_DONT_BLOCK);
1248 if (err != OK) {
1249 ALOGW("failed to configure c2 params");
1250 return err;
1251 }
1252
1253 std::vector<std::unique_ptr<C2Param>> params;
1254 C2StreamUsageTuning::input usage(0u, 0u);
1255 C2StreamMaxBufferSizeInfo::input maxInputSize(0u, 0u);
1256 C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE);
1257
1258 C2Param::Index colorAspectsRequestIndex =
1259 C2StreamColorAspectsInfo::output::PARAM_TYPE | C2Param::CoreIndex::IS_REQUEST_FLAG;
1260 std::initializer_list<C2Param::Index> indices {
1261 colorAspectsRequestIndex.withStream(0u),
1262 };
1263 int32_t colorTransferRequest = 0;
1264 if (config->mDomain & (Config::IS_IMAGE | Config::IS_VIDEO)
1265 && !sdkParams->findInt32("color-transfer-request", &colorTransferRequest)) {
1266 colorTransferRequest = 0;
1267 }
1268 c2_status_t c2err = C2_OK;
1269 if (colorTransferRequest != 0) {
1270 c2err = comp->query(
1271 { &usage, &maxInputSize, &prepend },
1272 indices,
1273 C2_DONT_BLOCK,
1274 ¶ms);
1275 } else {
1276 c2err = comp->query(
1277 { &usage, &maxInputSize, &prepend },
1278 {},
1279 C2_DONT_BLOCK,
1280 ¶ms);
1281 }
1282 if (c2err != C2_OK && c2err != C2_BAD_INDEX) {
1283 ALOGE("Failed to query component interface: %d", c2err);
1284 return UNKNOWN_ERROR;
1285 }
1286 if (usage) {
1287 if (usage.value & C2MemoryUsage::CPU_READ) {
1288 config->mInputFormat->setInt32("using-sw-read-often", true);
1289 }
1290 if (config->mISConfig) {
1291 C2AndroidMemoryUsage androidUsage(C2MemoryUsage(usage.value));
1292 config->mISConfig->mUsage = androidUsage.asGrallocUsage();
1293 }
1294 config->mInputFormat->setInt64("android._C2MemoryUsage", usage.value);
1295 }
1296
1297 // NOTE: we don't blindly use client specified input size if specified as clients
1298 // at times specify too small size. Instead, mimic the behavior from OMX, where the
1299 // client specified size is only used to ask for bigger buffers than component suggested
1300 // size.
1301 int32_t clientInputSize = 0;
1302 bool clientSpecifiedInputSize =
1303 msg->findInt32(KEY_MAX_INPUT_SIZE, &clientInputSize) && clientInputSize > 0;
1304 // TEMP: enforce minimum buffer size of 1MB for video decoders
1305 // and 16K / 4K for audio encoders/decoders
1306 if (maxInputSize.value == 0) {
1307 if (config->mDomain & Config::IS_AUDIO) {
1308 maxInputSize.value = encoder ? 16384 : 4096;
1309 } else if (!encoder) {
1310 maxInputSize.value = 1048576u;
1311 }
1312 }
1313
1314 // verify that CSD fits into this size (if defined)
1315 if ((config->mDomain & Config::IS_DECODER) && maxInputSize.value > 0) {
1316 sp<ABuffer> csd;
1317 for (size_t ix = 0; msg->findBuffer(StringPrintf("csd-%zu", ix).c_str(), &csd); ++ix) {
1318 if (csd && csd->size() > maxInputSize.value) {
1319 maxInputSize.value = csd->size();
1320 }
1321 }
1322 }
1323
1324 // TODO: do this based on component requiring linear allocator for input
1325 if ((config->mDomain & Config::IS_DECODER) || (config->mDomain & Config::IS_AUDIO)) {
1326 if (clientSpecifiedInputSize) {
1327 // Warn that we're overriding client's max input size if necessary.
1328 if ((uint32_t)clientInputSize < maxInputSize.value) {
1329 ALOGD("client requested max input size %d, which is smaller than "
1330 "what component recommended (%u); overriding with component "
1331 "recommendation.", clientInputSize, maxInputSize.value);
1332 ALOGW("This behavior is subject to change. It is recommended that "
1333 "app developers double check whether the requested "
1334 "max input size is in reasonable range.");
1335 } else {
1336 maxInputSize.value = clientInputSize;
1337 }
1338 }
1339 // Pass max input size on input format to the buffer channel (if supplied by the
1340 // component or by a default)
1341 if (maxInputSize.value) {
1342 config->mInputFormat->setInt32(
1343 KEY_MAX_INPUT_SIZE,
1344 (int32_t)(c2_min(maxInputSize.value, uint32_t(INT32_MAX))));
1345 }
1346 }
1347
1348 int32_t clientPrepend;
1349 if ((config->mDomain & Config::IS_VIDEO)
1350 && (config->mDomain & Config::IS_ENCODER)
1351 && msg->findInt32(KEY_PREPEND_HEADER_TO_SYNC_FRAMES, &clientPrepend)
1352 && clientPrepend
1353 && (!prepend || prepend.value != PREPEND_HEADER_TO_ALL_SYNC)) {
1354 ALOGE("Failed to set KEY_PREPEND_HEADER_TO_SYNC_FRAMES");
1355 return BAD_VALUE;
1356 }
1357
1358 int32_t componentColorFormat = 0;
1359 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) {
1360 // propagate HDR static info to output format for both encoders and decoders
1361 // if component supports this info, we will update from component, but only the raw port,
1362 // so don't propagate if component already filled it in.
1363 sp<ABuffer> hdrInfo;
1364 if (msg->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo)
1365 && !config->mOutputFormat->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo)) {
1366 config->mOutputFormat->setBuffer(KEY_HDR_STATIC_INFO, hdrInfo);
1367 }
1368
1369 // Set desired color format from configuration parameter
1370 int32_t format;
1371 if (!msg->findInt32(KEY_COLOR_FORMAT, &format)) {
1372 format = defaultColorFormat;
1373 }
1374 if (config->mDomain & Config::IS_ENCODER) {
1375 config->mInputFormat->setInt32(KEY_COLOR_FORMAT, format);
1376 if (msg->findInt32("android._color-format", &componentColorFormat)) {
1377 config->mInputFormat->setInt32("android._color-format", componentColorFormat);
1378 }
1379 } else {
1380 config->mOutputFormat->setInt32(KEY_COLOR_FORMAT, format);
1381 }
1382 }
1383
1384 // propagate encoder delay and padding to output format
1385 if ((config->mDomain & Config::IS_DECODER) && (config->mDomain & Config::IS_AUDIO)) {
1386 int delay = 0;
1387 if (msg->findInt32("encoder-delay", &delay)) {
1388 config->mOutputFormat->setInt32("encoder-delay", delay);
1389 }
1390 int padding = 0;
1391 if (msg->findInt32("encoder-padding", &padding)) {
1392 config->mOutputFormat->setInt32("encoder-padding", padding);
1393 }
1394 }
1395
1396 if (config->mDomain & Config::IS_AUDIO) {
1397 // set channel-mask
1398 int32_t mask;
1399 if (msg->findInt32(KEY_CHANNEL_MASK, &mask)) {
1400 if (config->mDomain & Config::IS_ENCODER) {
1401 config->mInputFormat->setInt32(KEY_CHANNEL_MASK, mask);
1402 } else {
1403 config->mOutputFormat->setInt32(KEY_CHANNEL_MASK, mask);
1404 }
1405 }
1406
1407 // set PCM encoding
1408 int32_t pcmEncoding = kAudioEncodingPcm16bit;
1409 msg->findInt32(KEY_PCM_ENCODING, &pcmEncoding);
1410 if (encoder) {
1411 config->mInputFormat->setInt32("android._config-pcm-encoding", pcmEncoding);
1412 } else {
1413 config->mOutputFormat->setInt32("android._config-pcm-encoding", pcmEncoding);
1414 }
1415 }
1416
1417 std::unique_ptr<C2Param> colorTransferRequestParam;
1418 for (std::unique_ptr<C2Param> ¶m : params) {
1419 if (param->index() == colorAspectsRequestIndex.withStream(0u)) {
1420 ALOGI("found color transfer request param");
1421 colorTransferRequestParam = std::move(param);
1422 }
1423 }
1424
1425 if (colorTransferRequest != 0) {
1426 if (colorTransferRequestParam && *colorTransferRequestParam) {
1427 C2StreamColorAspectsInfo::output *info =
1428 static_cast<C2StreamColorAspectsInfo::output *>(
1429 colorTransferRequestParam.get());
1430 if (!C2Mapper::map(info->transfer, &colorTransferRequest)) {
1431 colorTransferRequest = 0;
1432 }
1433 } else {
1434 colorTransferRequest = 0;
1435 }
1436 config->mInputFormat->setInt32("color-transfer-request", colorTransferRequest);
1437 }
1438
1439 if (componentColorFormat != 0 && componentColorFormat != COLOR_FormatSurface) {
1440 // Need to get stride/vstride
1441 uint32_t pixelFormat = PIXEL_FORMAT_UNKNOWN;
1442 if (C2Mapper::mapPixelFormatFrameworkToCodec(componentColorFormat, &pixelFormat)) {
1443 // TODO: retrieve these values without allocating a buffer.
1444 // Currently allocating a buffer is necessary to retrieve the layout.
1445 int64_t blockUsage =
1446 usage.value | C2MemoryUsage::CPU_READ | C2MemoryUsage::CPU_WRITE;
1447 std::shared_ptr<C2GraphicBlock> block = FetchGraphicBlock(
1448 width, height, componentColorFormat, blockUsage, {comp->getName()});
1449 sp<GraphicBlockBuffer> buffer;
1450 if (block) {
1451 buffer = GraphicBlockBuffer::Allocate(
1452 config->mInputFormat,
1453 block,
1454 [](size_t size) -> sp<ABuffer> { return new ABuffer(size); });
1455 } else {
1456 ALOGD("Failed to allocate a graphic block "
1457 "(width=%d height=%d pixelFormat=%u usage=%llx)",
1458 width, height, pixelFormat, (long long)blockUsage);
1459 // This means that byte buffer mode is not supported in this configuration
1460 // anyway. Skip setting stride/vstride to input format.
1461 }
1462 if (buffer) {
1463 sp<ABuffer> imageData = buffer->getImageData();
1464 MediaImage2 *img = nullptr;
1465 if (imageData && imageData->data()
1466 && imageData->size() >= sizeof(MediaImage2)) {
1467 img = (MediaImage2*)imageData->data();
1468 }
1469 if (img && img->mNumPlanes > 0 && img->mType != img->MEDIA_IMAGE_TYPE_UNKNOWN) {
1470 int32_t stride = img->mPlane[0].mRowInc;
1471 config->mInputFormat->setInt32(KEY_STRIDE, stride);
1472 if (img->mNumPlanes > 1 && stride > 0) {
1473 int64_t offsetDelta =
1474 (int64_t)img->mPlane[1].mOffset - (int64_t)img->mPlane[0].mOffset;
1475 if (offsetDelta % stride == 0) {
1476 int32_t vstride = int32_t(offsetDelta / stride);
1477 config->mInputFormat->setInt32(KEY_SLICE_HEIGHT, vstride);
1478 } else {
1479 ALOGD("Cannot report accurate slice height: "
1480 "offsetDelta = %lld stride = %d",
1481 (long long)offsetDelta, stride);
1482 }
1483 }
1484 }
1485 }
1486 }
1487 }
1488
1489 if (config->mTunneled) {
1490 config->mOutputFormat->setInt32("android._tunneled", 1);
1491 }
1492
1493 // Convert an encoding statistics level to corresponding encoding statistics
1494 // kinds
1495 int32_t encodingStatisticsLevel = VIDEO_ENCODING_STATISTICS_LEVEL_NONE;
1496 if ((config->mDomain & Config::IS_ENCODER)
1497 && (config->mDomain & Config::IS_VIDEO)
1498 && msg->findInt32(KEY_VIDEO_ENCODING_STATISTICS_LEVEL, &encodingStatisticsLevel)) {
1499 // Higher level include all the enc stats belong to lower level.
1500 switch (encodingStatisticsLevel) {
1501 // case VIDEO_ENCODING_STATISTICS_LEVEL_2: // reserved for the future level 2
1502 // with more enc stat kinds
1503 // Future extended encoding statistics for the level 2 should be added here
1504 case VIDEO_ENCODING_STATISTICS_LEVEL_1:
1505 config->subscribeToConfigUpdate(
1506 comp,
1507 {
1508 C2AndroidStreamAverageBlockQuantizationInfo::output::PARAM_TYPE,
1509 C2StreamPictureTypeInfo::output::PARAM_TYPE,
1510 });
1511 break;
1512 case VIDEO_ENCODING_STATISTICS_LEVEL_NONE:
1513 break;
1514 }
1515 }
1516 ALOGD("encoding statistics level = %d", encodingStatisticsLevel);
1517
1518 ALOGD("setup formats input: %s",
1519 config->mInputFormat->debugString().c_str());
1520 ALOGD("setup formats output: %s",
1521 config->mOutputFormat->debugString().c_str());
1522 return OK;
1523 };
1524 if (tryAndReportOnError(doConfig) != OK) {
1525 return;
1526 }
1527
1528 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1529 const std::unique_ptr<Config> &config = *configLocked;
1530
1531 config->queryConfiguration(comp);
1532
1533 mCallback->onComponentConfigured(config->mInputFormat, config->mOutputFormat);
1534 }
1535
initiateCreateInputSurface()1536 void CCodec::initiateCreateInputSurface() {
1537 status_t err = [this] {
1538 Mutexed<State>::Locked state(mState);
1539 if (state->get() != ALLOCATED) {
1540 return UNKNOWN_ERROR;
1541 }
1542 // TODO: read it from intf() properly.
1543 if (state->comp->getName().find("encoder") == std::string::npos) {
1544 return INVALID_OPERATION;
1545 }
1546 return OK;
1547 }();
1548 if (err != OK) {
1549 mCallback->onInputSurfaceCreationFailed(err);
1550 return;
1551 }
1552
1553 (new AMessage(kWhatCreateInputSurface, this))->post();
1554 }
1555
CreateOmxInputSurface()1556 sp<PersistentSurface> CCodec::CreateOmxInputSurface() {
1557 using namespace android::hardware::media::omx::V1_0;
1558 using namespace android::hardware::media::omx::V1_0::utils;
1559 using namespace android::hardware::graphics::bufferqueue::V1_0::utils;
1560 typedef android::hardware::media::omx::V1_0::Status OmxStatus;
1561 android::sp<IOmx> omx = IOmx::getService();
1562 if (omx == nullptr) {
1563 return nullptr;
1564 }
1565 typedef android::hardware::graphics::bufferqueue::V1_0::
1566 IGraphicBufferProducer HGraphicBufferProducer;
1567 typedef android::hardware::media::omx::V1_0::
1568 IGraphicBufferSource HGraphicBufferSource;
1569 OmxStatus s;
1570 android::sp<HGraphicBufferProducer> gbp;
1571 android::sp<HGraphicBufferSource> gbs;
1572
1573 using ::android::hardware::Return;
1574 Return<void> transStatus = omx->createInputSurface(
1575 [&s, &gbp, &gbs](
1576 OmxStatus status,
1577 const android::sp<HGraphicBufferProducer>& producer,
1578 const android::sp<HGraphicBufferSource>& source) {
1579 s = status;
1580 gbp = producer;
1581 gbs = source;
1582 });
1583 if (transStatus.isOk() && s == OmxStatus::OK) {
1584 return new PersistentSurface(new H2BGraphicBufferProducer(gbp), gbs);
1585 }
1586
1587 return nullptr;
1588 }
1589
CreateCompatibleInputSurface()1590 sp<PersistentSurface> CCodec::CreateCompatibleInputSurface() {
1591 sp<PersistentSurface> surface(CreateInputSurface());
1592
1593 if (surface == nullptr) {
1594 surface = CreateOmxInputSurface();
1595 }
1596
1597 return surface;
1598 }
1599
createInputSurface()1600 void CCodec::createInputSurface() {
1601 status_t err;
1602 sp<IGraphicBufferProducer> bufferProducer;
1603
1604 sp<AMessage> outputFormat;
1605 uint64_t usage = 0;
1606 {
1607 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1608 const std::unique_ptr<Config> &config = *configLocked;
1609 outputFormat = config->mOutputFormat;
1610 usage = config->mISConfig ? config->mISConfig->mUsage : 0;
1611 }
1612
1613 sp<PersistentSurface> persistentSurface = CreateCompatibleInputSurface();
1614 sp<hidl::base::V1_0::IBase> hidlTarget = persistentSurface->getHidlTarget();
1615 sp<IInputSurface> hidlInputSurface = IInputSurface::castFrom(hidlTarget);
1616 sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget);
1617
1618 if (hidlInputSurface) {
1619 std::shared_ptr<Codec2Client::InputSurface> inputSurface =
1620 std::make_shared<Codec2Client::InputSurface>(hidlInputSurface);
1621 err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>(
1622 inputSurface));
1623 bufferProducer = inputSurface->getGraphicBufferProducer();
1624 } else if (gbs) {
1625 int32_t width = 0;
1626 (void)outputFormat->findInt32("width", &width);
1627 int32_t height = 0;
1628 (void)outputFormat->findInt32("height", &height);
1629 err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>(
1630 gbs, width, height, usage));
1631 bufferProducer = persistentSurface->getBufferProducer();
1632 } else {
1633 ALOGE("Corrupted input surface");
1634 mCallback->onInputSurfaceCreationFailed(UNKNOWN_ERROR);
1635 return;
1636 }
1637
1638 if (err != OK) {
1639 ALOGE("Failed to set up input surface: %d", err);
1640 mCallback->onInputSurfaceCreationFailed(err);
1641 return;
1642 }
1643
1644 // Formats can change after setupInputSurface
1645 sp<AMessage> inputFormat;
1646 {
1647 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1648 const std::unique_ptr<Config> &config = *configLocked;
1649 inputFormat = config->mInputFormat;
1650 outputFormat = config->mOutputFormat;
1651 }
1652 mCallback->onInputSurfaceCreated(
1653 inputFormat,
1654 outputFormat,
1655 new BufferProducerWrapper(bufferProducer));
1656 }
1657
setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> & surface)1658 status_t CCodec::setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface) {
1659 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1660 const std::unique_ptr<Config> &config = *configLocked;
1661 config->mUsingSurface = true;
1662
1663 // we are now using surface - apply default color aspects to input format - as well as
1664 // get dataspace
1665 bool inputFormatChanged = config->updateFormats(Config::IS_INPUT);
1666
1667 // configure dataspace
1668 static_assert(sizeof(int32_t) == sizeof(android_dataspace), "dataspace size mismatch");
1669
1670 // The output format contains app-configured color aspects, and the input format
1671 // has the default color aspects. Use the default for the unspecified params.
1672 ColorAspects inputColorAspects, colorAspects;
1673 getColorAspectsFromFormat(config->mOutputFormat, colorAspects);
1674 getColorAspectsFromFormat(config->mInputFormat, inputColorAspects);
1675 if (colorAspects.mRange == ColorAspects::RangeUnspecified) {
1676 colorAspects.mRange = inputColorAspects.mRange;
1677 }
1678 if (colorAspects.mPrimaries == ColorAspects::PrimariesUnspecified) {
1679 colorAspects.mPrimaries = inputColorAspects.mPrimaries;
1680 }
1681 if (colorAspects.mTransfer == ColorAspects::TransferUnspecified) {
1682 colorAspects.mTransfer = inputColorAspects.mTransfer;
1683 }
1684 if (colorAspects.mMatrixCoeffs == ColorAspects::MatrixUnspecified) {
1685 colorAspects.mMatrixCoeffs = inputColorAspects.mMatrixCoeffs;
1686 }
1687 android_dataspace dataSpace = getDataSpaceForColorAspects(
1688 colorAspects, /* mayExtend = */ false);
1689 surface->setDataSpace(dataSpace);
1690 setColorAspectsIntoFormat(colorAspects, config->mInputFormat, /* force = */ true);
1691 config->mInputFormat->setInt32("android._dataspace", int32_t(dataSpace));
1692
1693 ALOGD("input format %s to %s",
1694 inputFormatChanged ? "changed" : "unchanged",
1695 config->mInputFormat->debugString().c_str());
1696
1697 status_t err = mChannel->setInputSurface(surface);
1698 if (err != OK) {
1699 // undo input format update
1700 config->mUsingSurface = false;
1701 (void)config->updateFormats(Config::IS_INPUT);
1702 return err;
1703 }
1704 config->mInputSurface = surface;
1705
1706 if (config->mISConfig) {
1707 surface->configure(*config->mISConfig);
1708 } else {
1709 ALOGD("ISConfig: no configuration");
1710 }
1711
1712 return OK;
1713 }
1714
initiateSetInputSurface(const sp<PersistentSurface> & surface)1715 void CCodec::initiateSetInputSurface(const sp<PersistentSurface> &surface) {
1716 sp<AMessage> msg = new AMessage(kWhatSetInputSurface, this);
1717 msg->setObject("surface", surface);
1718 msg->post();
1719 }
1720
setInputSurface(const sp<PersistentSurface> & surface)1721 void CCodec::setInputSurface(const sp<PersistentSurface> &surface) {
1722 sp<AMessage> outputFormat;
1723 uint64_t usage = 0;
1724 {
1725 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1726 const std::unique_ptr<Config> &config = *configLocked;
1727 outputFormat = config->mOutputFormat;
1728 usage = config->mISConfig ? config->mISConfig->mUsage : 0;
1729 }
1730 sp<hidl::base::V1_0::IBase> hidlTarget = surface->getHidlTarget();
1731 sp<IInputSurface> inputSurface = IInputSurface::castFrom(hidlTarget);
1732 sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget);
1733 if (inputSurface) {
1734 status_t err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>(
1735 std::make_shared<Codec2Client::InputSurface>(inputSurface)));
1736 if (err != OK) {
1737 ALOGE("Failed to set up input surface: %d", err);
1738 mCallback->onInputSurfaceDeclined(err);
1739 return;
1740 }
1741 } else if (gbs) {
1742 int32_t width = 0;
1743 (void)outputFormat->findInt32("width", &width);
1744 int32_t height = 0;
1745 (void)outputFormat->findInt32("height", &height);
1746 status_t err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>(
1747 gbs, width, height, usage));
1748 if (err != OK) {
1749 ALOGE("Failed to set up input surface: %d", err);
1750 mCallback->onInputSurfaceDeclined(err);
1751 return;
1752 }
1753 } else {
1754 ALOGE("Failed to set input surface: Corrupted surface.");
1755 mCallback->onInputSurfaceDeclined(UNKNOWN_ERROR);
1756 return;
1757 }
1758 // Formats can change after setupInputSurface
1759 sp<AMessage> inputFormat;
1760 {
1761 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1762 const std::unique_ptr<Config> &config = *configLocked;
1763 inputFormat = config->mInputFormat;
1764 outputFormat = config->mOutputFormat;
1765 }
1766 mCallback->onInputSurfaceAccepted(inputFormat, outputFormat);
1767 }
1768
initiateStart()1769 void CCodec::initiateStart() {
1770 auto setStarting = [this] {
1771 Mutexed<State>::Locked state(mState);
1772 if (state->get() != ALLOCATED) {
1773 return UNKNOWN_ERROR;
1774 }
1775 state->set(STARTING);
1776 return OK;
1777 };
1778 if (tryAndReportOnError(setStarting) != OK) {
1779 return;
1780 }
1781
1782 (new AMessage(kWhatStart, this))->post();
1783 }
1784
start()1785 void CCodec::start() {
1786 std::shared_ptr<Codec2Client::Component> comp;
1787 auto checkStarting = [this, &comp] {
1788 Mutexed<State>::Locked state(mState);
1789 if (state->get() != STARTING) {
1790 return UNKNOWN_ERROR;
1791 }
1792 comp = state->comp;
1793 return OK;
1794 };
1795 if (tryAndReportOnError(checkStarting) != OK) {
1796 return;
1797 }
1798
1799 c2_status_t err = comp->start();
1800 if (err != C2_OK) {
1801 mCallback->onError(toStatusT(err, C2_OPERATION_Component_start),
1802 ACTION_CODE_FATAL);
1803 return;
1804 }
1805 sp<AMessage> inputFormat;
1806 sp<AMessage> outputFormat;
1807 status_t err2 = OK;
1808 bool buffersBoundToCodec = false;
1809 {
1810 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1811 const std::unique_ptr<Config> &config = *configLocked;
1812 inputFormat = config->mInputFormat;
1813 // start triggers format dup
1814 outputFormat = config->mOutputFormat = config->mOutputFormat->dup();
1815 if (config->mInputSurface) {
1816 err2 = config->mInputSurface->start();
1817 config->mInputSurfaceDataspace = config->mInputSurface->getDataspace();
1818 }
1819 buffersBoundToCodec = config->mBuffersBoundToCodec;
1820 }
1821 if (err2 != OK) {
1822 mCallback->onError(err2, ACTION_CODE_FATAL);
1823 return;
1824 }
1825 err2 = mChannel->start(inputFormat, outputFormat, buffersBoundToCodec);
1826 if (err2 != OK) {
1827 mCallback->onError(err2, ACTION_CODE_FATAL);
1828 return;
1829 }
1830
1831 auto setRunning = [this] {
1832 Mutexed<State>::Locked state(mState);
1833 if (state->get() != STARTING) {
1834 return UNKNOWN_ERROR;
1835 }
1836 state->set(RUNNING);
1837 return OK;
1838 };
1839 if (tryAndReportOnError(setRunning) != OK) {
1840 return;
1841 }
1842
1843 // preparation of input buffers may not succeed due to the lack of
1844 // memory; returning correct error code (NO_MEMORY) as an error allows
1845 // MediaCodec to try reclaim and restart codec gracefully.
1846 std::map<size_t, sp<MediaCodecBuffer>> clientInputBuffers;
1847 err2 = mChannel->prepareInitialInputBuffers(&clientInputBuffers);
1848 if (err2 != OK) {
1849 ALOGE("Initial preparation for Input Buffers failed");
1850 mCallback->onError(err2, ACTION_CODE_FATAL);
1851 return;
1852 }
1853
1854 mCallback->onStartCompleted();
1855
1856 mChannel->requestInitialInputBuffers(std::move(clientInputBuffers));
1857 }
1858
initiateShutdown(bool keepComponentAllocated)1859 void CCodec::initiateShutdown(bool keepComponentAllocated) {
1860 if (keepComponentAllocated) {
1861 initiateStop();
1862 } else {
1863 initiateRelease();
1864 }
1865 }
1866
initiateStop()1867 void CCodec::initiateStop() {
1868 {
1869 Mutexed<State>::Locked state(mState);
1870 if (state->get() == ALLOCATED
1871 || state->get() == RELEASED
1872 || state->get() == STOPPING
1873 || state->get() == RELEASING) {
1874 // We're already stopped, released, or doing it right now.
1875 state.unlock();
1876 mCallback->onStopCompleted();
1877 state.lock();
1878 return;
1879 }
1880 state->set(STOPPING);
1881 }
1882 mChannel->reset();
1883 bool pushBlankBuffer = mConfig.lock().get()->mPushBlankBuffersOnStop;
1884 sp<AMessage> stopMessage(new AMessage(kWhatStop, this));
1885 stopMessage->setInt32("pushBlankBuffer", pushBlankBuffer);
1886 stopMessage->post();
1887 }
1888
stop(bool pushBlankBuffer)1889 void CCodec::stop(bool pushBlankBuffer) {
1890 std::shared_ptr<Codec2Client::Component> comp;
1891 {
1892 Mutexed<State>::Locked state(mState);
1893 if (state->get() == RELEASING) {
1894 state.unlock();
1895 // We're already stopped or release is in progress.
1896 mCallback->onStopCompleted();
1897 state.lock();
1898 return;
1899 } else if (state->get() != STOPPING) {
1900 state.unlock();
1901 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1902 state.lock();
1903 return;
1904 }
1905 comp = state->comp;
1906 }
1907 status_t err = comp->stop();
1908 mChannel->stopUseOutputSurface(pushBlankBuffer);
1909 if (err != C2_OK) {
1910 // TODO: convert err into status_t
1911 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1912 }
1913
1914 {
1915 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1916 const std::unique_ptr<Config> &config = *configLocked;
1917 if (config->mInputSurface) {
1918 config->mInputSurface->disconnect();
1919 config->mInputSurface = nullptr;
1920 config->mInputSurfaceDataspace = HAL_DATASPACE_UNKNOWN;
1921 }
1922 }
1923 {
1924 Mutexed<State>::Locked state(mState);
1925 if (state->get() == STOPPING) {
1926 state->set(ALLOCATED);
1927 }
1928 }
1929 mCallback->onStopCompleted();
1930 }
1931
initiateRelease(bool sendCallback)1932 void CCodec::initiateRelease(bool sendCallback /* = true */) {
1933 bool clearInputSurfaceIfNeeded = false;
1934 {
1935 Mutexed<State>::Locked state(mState);
1936 if (state->get() == RELEASED || state->get() == RELEASING) {
1937 // We're already released or doing it right now.
1938 if (sendCallback) {
1939 state.unlock();
1940 mCallback->onReleaseCompleted();
1941 state.lock();
1942 }
1943 return;
1944 }
1945 if (state->get() == ALLOCATING) {
1946 state->set(RELEASING);
1947 // With the altered state allocate() would fail and clean up.
1948 if (sendCallback) {
1949 state.unlock();
1950 mCallback->onReleaseCompleted();
1951 state.lock();
1952 }
1953 return;
1954 }
1955 if (state->get() == STARTING
1956 || state->get() == RUNNING
1957 || state->get() == STOPPING) {
1958 // Input surface may have been started, so clean up is needed.
1959 clearInputSurfaceIfNeeded = true;
1960 }
1961 state->set(RELEASING);
1962 }
1963
1964 if (clearInputSurfaceIfNeeded) {
1965 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1966 const std::unique_ptr<Config> &config = *configLocked;
1967 if (config->mInputSurface) {
1968 config->mInputSurface->disconnect();
1969 config->mInputSurface = nullptr;
1970 config->mInputSurfaceDataspace = HAL_DATASPACE_UNKNOWN;
1971 }
1972 }
1973
1974 mChannel->reset();
1975 bool pushBlankBuffer = mConfig.lock().get()->mPushBlankBuffersOnStop;
1976 // thiz holds strong ref to this while the thread is running.
1977 sp<CCodec> thiz(this);
1978 std::thread([thiz, sendCallback, pushBlankBuffer]
1979 { thiz->release(sendCallback, pushBlankBuffer); }).detach();
1980 }
1981
release(bool sendCallback,bool pushBlankBuffer)1982 void CCodec::release(bool sendCallback, bool pushBlankBuffer) {
1983 std::shared_ptr<Codec2Client::Component> comp;
1984 {
1985 Mutexed<State>::Locked state(mState);
1986 if (state->get() == RELEASED) {
1987 if (sendCallback) {
1988 state.unlock();
1989 mCallback->onReleaseCompleted();
1990 state.lock();
1991 }
1992 return;
1993 }
1994 comp = state->comp;
1995 }
1996 comp->release();
1997 mChannel->stopUseOutputSurface(pushBlankBuffer);
1998
1999 {
2000 Mutexed<State>::Locked state(mState);
2001 state->set(RELEASED);
2002 state->comp.reset();
2003 }
2004 (new AMessage(kWhatRelease, this))->post();
2005 if (sendCallback) {
2006 mCallback->onReleaseCompleted();
2007 }
2008 }
2009
setSurface(const sp<Surface> & surface)2010 status_t CCodec::setSurface(const sp<Surface> &surface) {
2011 bool pushBlankBuffer = false;
2012 {
2013 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2014 const std::unique_ptr<Config> &config = *configLocked;
2015 sp<ANativeWindow> nativeWindow = static_cast<ANativeWindow *>(surface.get());
2016 status_t err = OK;
2017
2018 if (config->mTunneled && config->mSidebandHandle != nullptr) {
2019 err = native_window_set_sideband_stream(
2020 nativeWindow.get(),
2021 const_cast<native_handle_t *>(config->mSidebandHandle->handle()));
2022 if (err != OK) {
2023 ALOGE("NativeWindow(%p) native_window_set_sideband_stream(%p) failed! (err %d).",
2024 nativeWindow.get(), config->mSidebandHandle->handle(), err);
2025 return err;
2026 }
2027 } else {
2028 // Explicitly reset the sideband handle of the window for
2029 // non-tunneled video in case the window was previously used
2030 // for a tunneled video playback.
2031 err = native_window_set_sideband_stream(nativeWindow.get(), nullptr);
2032 if (err != OK) {
2033 ALOGE("native_window_set_sideband_stream(nullptr) failed! (err %d).", err);
2034 return err;
2035 }
2036 }
2037 pushBlankBuffer = config->mPushBlankBuffersOnStop;
2038 }
2039 return mChannel->setSurface(surface, pushBlankBuffer);
2040 }
2041
signalFlush()2042 void CCodec::signalFlush() {
2043 status_t err = [this] {
2044 Mutexed<State>::Locked state(mState);
2045 if (state->get() == FLUSHED) {
2046 return ALREADY_EXISTS;
2047 }
2048 if (state->get() != RUNNING) {
2049 return UNKNOWN_ERROR;
2050 }
2051 state->set(FLUSHING);
2052 return OK;
2053 }();
2054 switch (err) {
2055 case ALREADY_EXISTS:
2056 mCallback->onFlushCompleted();
2057 return;
2058 case OK:
2059 break;
2060 default:
2061 mCallback->onError(err, ACTION_CODE_FATAL);
2062 return;
2063 }
2064
2065 mChannel->stop();
2066 (new AMessage(kWhatFlush, this))->post();
2067 }
2068
flush()2069 void CCodec::flush() {
2070 std::shared_ptr<Codec2Client::Component> comp;
2071 auto checkFlushing = [this, &comp] {
2072 Mutexed<State>::Locked state(mState);
2073 if (state->get() != FLUSHING) {
2074 return UNKNOWN_ERROR;
2075 }
2076 comp = state->comp;
2077 return OK;
2078 };
2079 if (tryAndReportOnError(checkFlushing) != OK) {
2080 return;
2081 }
2082
2083 std::list<std::unique_ptr<C2Work>> flushedWork;
2084 c2_status_t err = comp->flush(C2Component::FLUSH_COMPONENT, &flushedWork);
2085 {
2086 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
2087 flushedWork.splice(flushedWork.end(), *queue);
2088 }
2089 if (err != C2_OK) {
2090 // TODO: convert err into status_t
2091 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2092 }
2093
2094 mChannel->flush(flushedWork);
2095
2096 {
2097 Mutexed<State>::Locked state(mState);
2098 if (state->get() == FLUSHING) {
2099 state->set(FLUSHED);
2100 }
2101 }
2102 mCallback->onFlushCompleted();
2103 }
2104
signalResume()2105 void CCodec::signalResume() {
2106 std::shared_ptr<Codec2Client::Component> comp;
2107 auto setResuming = [this, &comp] {
2108 Mutexed<State>::Locked state(mState);
2109 if (state->get() != FLUSHED) {
2110 return UNKNOWN_ERROR;
2111 }
2112 state->set(RESUMING);
2113 comp = state->comp;
2114 return OK;
2115 };
2116 if (tryAndReportOnError(setResuming) != OK) {
2117 return;
2118 }
2119
2120 {
2121 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2122 const std::unique_ptr<Config> &config = *configLocked;
2123 sp<AMessage> outputFormat = config->mOutputFormat;
2124 config->queryConfiguration(comp);
2125 RevertOutputFormatIfNeeded(outputFormat, config->mOutputFormat);
2126 }
2127
2128 (void)mChannel->start(nullptr, nullptr, [&]{
2129 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2130 const std::unique_ptr<Config> &config = *configLocked;
2131 return config->mBuffersBoundToCodec;
2132 }());
2133
2134 {
2135 Mutexed<State>::Locked state(mState);
2136 if (state->get() != RESUMING) {
2137 state.unlock();
2138 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2139 state.lock();
2140 return;
2141 }
2142 state->set(RUNNING);
2143 }
2144
2145 std::map<size_t, sp<MediaCodecBuffer>> clientInputBuffers;
2146 status_t err = mChannel->prepareInitialInputBuffers(&clientInputBuffers);
2147 // FIXME(b/237656746)
2148 if (err != OK && err != NO_MEMORY) {
2149 ALOGE("Resume request for Input Buffers failed");
2150 mCallback->onError(err, ACTION_CODE_FATAL);
2151 return;
2152 }
2153 mChannel->requestInitialInputBuffers(std::move(clientInputBuffers));
2154 }
2155
signalSetParameters(const sp<AMessage> & msg)2156 void CCodec::signalSetParameters(const sp<AMessage> &msg) {
2157 std::shared_ptr<Codec2Client::Component> comp;
2158 auto checkState = [this, &comp] {
2159 Mutexed<State>::Locked state(mState);
2160 if (state->get() == RELEASED) {
2161 return INVALID_OPERATION;
2162 }
2163 comp = state->comp;
2164 return OK;
2165 };
2166 if (tryAndReportOnError(checkState) != OK) {
2167 return;
2168 }
2169
2170 // NOTE: We used to ignore "bitrate" at setParameters; replicate
2171 // the behavior here.
2172 sp<AMessage> params = msg;
2173 int32_t bitrate;
2174 if (params->findInt32(KEY_BIT_RATE, &bitrate)) {
2175 params = msg->dup();
2176 params->removeEntryAt(params->findEntryByName(KEY_BIT_RATE));
2177 }
2178
2179 int32_t syncId = 0;
2180 if (params->findInt32("audio-hw-sync", &syncId)
2181 || params->findInt32("hw-av-sync-id", &syncId)) {
2182 configureTunneledVideoPlayback(comp, nullptr, params);
2183 }
2184
2185 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2186 const std::unique_ptr<Config> &config = *configLocked;
2187
2188 /**
2189 * Handle input surface parameters
2190 */
2191 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))
2192 && (config->mDomain & Config::IS_ENCODER)
2193 && config->mInputSurface && config->mISConfig) {
2194 (void)params->findInt64(PARAMETER_KEY_OFFSET_TIME, &config->mISConfig->mTimeOffsetUs);
2195
2196 if (params->findInt64("skip-frames-before", &config->mISConfig->mStartAtUs)) {
2197 config->mISConfig->mStopped = false;
2198 } else if (params->findInt64("stop-time-us", &config->mISConfig->mStopAtUs)) {
2199 config->mISConfig->mStopped = true;
2200 }
2201
2202 int32_t value;
2203 if (params->findInt32(PARAMETER_KEY_SUSPEND, &value)) {
2204 config->mISConfig->mSuspended = value;
2205 config->mISConfig->mSuspendAtUs = -1;
2206 (void)params->findInt64(PARAMETER_KEY_SUSPEND_TIME, &config->mISConfig->mSuspendAtUs);
2207 }
2208
2209 (void)config->mInputSurface->configure(*config->mISConfig);
2210 if (config->mISConfig->mStopped) {
2211 config->mInputFormat->setInt64(
2212 "android._stop-time-offset-us", config->mISConfig->mInputDelayUs);
2213 }
2214 }
2215
2216 std::vector<std::unique_ptr<C2Param>> configUpdate;
2217 (void)config->getConfigUpdateFromSdkParams(
2218 comp, params, Config::IS_PARAM, C2_MAY_BLOCK, &configUpdate);
2219 // Prefer to pass parameters to the buffer channel, so they can be synchronized with the frames.
2220 // Parameter synchronization is not defined when using input surface. For now, route
2221 // these directly to the component.
2222 if (config->mInputSurface == nullptr
2223 && (property_get_bool("debug.stagefright.ccodec_delayed_params", false)
2224 || comp->getName().find("c2.android.") == 0)) {
2225 mChannel->setParameters(configUpdate);
2226 } else {
2227 sp<AMessage> outputFormat = config->mOutputFormat;
2228 (void)config->setParameters(comp, configUpdate, C2_MAY_BLOCK);
2229 RevertOutputFormatIfNeeded(outputFormat, config->mOutputFormat);
2230 }
2231 }
2232
signalEndOfInputStream()2233 void CCodec::signalEndOfInputStream() {
2234 mCallback->onSignaledInputEOS(mChannel->signalEndOfInputStream());
2235 }
2236
signalRequestIDRFrame()2237 void CCodec::signalRequestIDRFrame() {
2238 std::shared_ptr<Codec2Client::Component> comp;
2239 {
2240 Mutexed<State>::Locked state(mState);
2241 if (state->get() == RELEASED) {
2242 ALOGD("no IDR request sent since component is released");
2243 return;
2244 }
2245 comp = state->comp;
2246 }
2247 ALOGV("request IDR");
2248 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2249 const std::unique_ptr<Config> &config = *configLocked;
2250 std::vector<std::unique_ptr<C2Param>> params;
2251 params.push_back(
2252 std::make_unique<C2StreamRequestSyncFrameTuning::output>(0u, true));
2253 config->setParameters(comp, params, C2_MAY_BLOCK);
2254 }
2255
querySupportedParameters(std::vector<std::string> * names)2256 status_t CCodec::querySupportedParameters(std::vector<std::string> *names) {
2257 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2258 const std::unique_ptr<Config> &config = *configLocked;
2259 return config->querySupportedParameters(names);
2260 }
2261
describeParameter(const std::string & name,CodecParameterDescriptor * desc)2262 status_t CCodec::describeParameter(
2263 const std::string &name, CodecParameterDescriptor *desc) {
2264 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2265 const std::unique_ptr<Config> &config = *configLocked;
2266 return config->describe(name, desc);
2267 }
2268
subscribeToParameters(const std::vector<std::string> & names)2269 status_t CCodec::subscribeToParameters(const std::vector<std::string> &names) {
2270 std::shared_ptr<Codec2Client::Component> comp = mState.lock()->comp;
2271 if (!comp) {
2272 return INVALID_OPERATION;
2273 }
2274 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2275 const std::unique_ptr<Config> &config = *configLocked;
2276 return config->subscribeToVendorConfigUpdate(comp, names);
2277 }
2278
unsubscribeFromParameters(const std::vector<std::string> & names)2279 status_t CCodec::unsubscribeFromParameters(const std::vector<std::string> &names) {
2280 std::shared_ptr<Codec2Client::Component> comp = mState.lock()->comp;
2281 if (!comp) {
2282 return INVALID_OPERATION;
2283 }
2284 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2285 const std::unique_ptr<Config> &config = *configLocked;
2286 return config->unsubscribeFromVendorConfigUpdate(comp, names);
2287 }
2288
onWorkDone(std::list<std::unique_ptr<C2Work>> & workItems)2289 void CCodec::onWorkDone(std::list<std::unique_ptr<C2Work>> &workItems) {
2290 if (!workItems.empty()) {
2291 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
2292 queue->splice(queue->end(), workItems);
2293 }
2294 (new AMessage(kWhatWorkDone, this))->post();
2295 }
2296
onInputBufferDone(uint64_t frameIndex,size_t arrayIndex)2297 void CCodec::onInputBufferDone(uint64_t frameIndex, size_t arrayIndex) {
2298 mChannel->onInputBufferDone(frameIndex, arrayIndex);
2299 if (arrayIndex == 0) {
2300 // We always put no more than one buffer per work, if we use an input surface.
2301 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2302 const std::unique_ptr<Config> &config = *configLocked;
2303 if (config->mInputSurface) {
2304 config->mInputSurface->onInputBufferDone(frameIndex);
2305 }
2306 }
2307 }
2308
onMessageReceived(const sp<AMessage> & msg)2309 void CCodec::onMessageReceived(const sp<AMessage> &msg) {
2310 TimePoint now = std::chrono::steady_clock::now();
2311 CCodecWatchdog::getInstance()->watch(this);
2312 switch (msg->what()) {
2313 case kWhatAllocate: {
2314 // C2ComponentStore::createComponent() should return within 100ms.
2315 setDeadline(now, 1500ms, "allocate");
2316 sp<RefBase> obj;
2317 CHECK(msg->findObject("codecInfo", &obj));
2318 allocate((MediaCodecInfo *)obj.get());
2319 break;
2320 }
2321 case kWhatConfigure: {
2322 // C2Component::commit_sm() should return within 5ms.
2323 setDeadline(now, 1500ms, "configure");
2324 sp<AMessage> format;
2325 CHECK(msg->findMessage("format", &format));
2326 configure(format);
2327 break;
2328 }
2329 case kWhatStart: {
2330 // C2Component::start() should return within 500ms.
2331 setDeadline(now, 1500ms, "start");
2332 start();
2333 break;
2334 }
2335 case kWhatStop: {
2336 // C2Component::stop() should return within 500ms.
2337 setDeadline(now, 1500ms, "stop");
2338 int32_t pushBlankBuffer;
2339 if (!msg->findInt32("pushBlankBuffer", &pushBlankBuffer)) {
2340 pushBlankBuffer = 0;
2341 }
2342 stop(static_cast<bool>(pushBlankBuffer));
2343 break;
2344 }
2345 case kWhatFlush: {
2346 // C2Component::flush_sm() should return within 5ms.
2347 setDeadline(now, 1500ms, "flush");
2348 flush();
2349 break;
2350 }
2351 case kWhatRelease: {
2352 mChannel->release();
2353 mClient.reset();
2354 mClientListener.reset();
2355 break;
2356 }
2357 case kWhatCreateInputSurface: {
2358 // Surface operations may be briefly blocking.
2359 setDeadline(now, 1500ms, "createInputSurface");
2360 createInputSurface();
2361 break;
2362 }
2363 case kWhatSetInputSurface: {
2364 // Surface operations may be briefly blocking.
2365 setDeadline(now, 1500ms, "setInputSurface");
2366 sp<RefBase> obj;
2367 CHECK(msg->findObject("surface", &obj));
2368 sp<PersistentSurface> surface(static_cast<PersistentSurface *>(obj.get()));
2369 setInputSurface(surface);
2370 break;
2371 }
2372 case kWhatWorkDone: {
2373 std::unique_ptr<C2Work> work;
2374 bool shouldPost = false;
2375 {
2376 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
2377 if (queue->empty()) {
2378 break;
2379 }
2380 work.swap(queue->front());
2381 queue->pop_front();
2382 shouldPost = !queue->empty();
2383 }
2384 if (shouldPost) {
2385 (new AMessage(kWhatWorkDone, this))->post();
2386 }
2387
2388 // handle configuration changes in work done
2389 std::shared_ptr<const C2StreamInitDataInfo::output> initData;
2390 sp<AMessage> outputFormat = nullptr;
2391 {
2392 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2393 const std::unique_ptr<Config> &config = *configLocked;
2394 Config::Watcher<C2StreamInitDataInfo::output> initDataWatcher =
2395 config->watch<C2StreamInitDataInfo::output>();
2396 if (!work->worklets.empty()
2397 && (work->worklets.front()->output.flags
2398 & C2FrameData::FLAG_DISCARD_FRAME) == 0) {
2399
2400 // copy buffer info to config
2401 std::vector<std::unique_ptr<C2Param>> updates;
2402 for (const std::unique_ptr<C2Param> ¶m
2403 : work->worklets.front()->output.configUpdate) {
2404 updates.push_back(C2Param::Copy(*param));
2405 }
2406 unsigned stream = 0;
2407 std::vector<std::shared_ptr<C2Buffer>> &outputBuffers =
2408 work->worklets.front()->output.buffers;
2409 for (const std::shared_ptr<C2Buffer> &buf : outputBuffers) {
2410 for (const std::shared_ptr<const C2Info> &info : buf->info()) {
2411 // move all info into output-stream #0 domain
2412 updates.emplace_back(
2413 C2Param::CopyAsStream(*info, true /* output */, stream));
2414 }
2415
2416 const std::vector<C2ConstGraphicBlock> blocks = buf->data().graphicBlocks();
2417 // for now only do the first block
2418 if (!blocks.empty()) {
2419 // ALOGV("got output buffer with crop %u,%u+%u,%u and size %u,%u",
2420 // block.crop().left, block.crop().top,
2421 // block.crop().width, block.crop().height,
2422 // block.width(), block.height());
2423 const C2ConstGraphicBlock &block = blocks[0];
2424 updates.emplace_back(new C2StreamCropRectInfo::output(
2425 stream, block.crop()));
2426 }
2427 ++stream;
2428 }
2429
2430 sp<AMessage> oldFormat = config->mOutputFormat;
2431 config->updateConfiguration(updates, config->mOutputDomain);
2432 RevertOutputFormatIfNeeded(oldFormat, config->mOutputFormat);
2433
2434 // copy standard infos to graphic buffers if not already present (otherwise, we
2435 // may overwrite the actual intermediate value with a final value)
2436 stream = 0;
2437 const static C2Param::Index stdGfxInfos[] = {
2438 C2StreamRotationInfo::output::PARAM_TYPE,
2439 C2StreamColorAspectsInfo::output::PARAM_TYPE,
2440 C2StreamDataSpaceInfo::output::PARAM_TYPE,
2441 C2StreamHdrStaticInfo::output::PARAM_TYPE,
2442 C2StreamHdr10PlusInfo::output::PARAM_TYPE, // will be deprecated
2443 C2StreamHdrDynamicMetadataInfo::output::PARAM_TYPE,
2444 C2StreamPixelAspectRatioInfo::output::PARAM_TYPE,
2445 C2StreamSurfaceScalingInfo::output::PARAM_TYPE
2446 };
2447 for (const std::shared_ptr<C2Buffer> &buf : outputBuffers) {
2448 if (buf->data().graphicBlocks().size()) {
2449 for (C2Param::Index ix : stdGfxInfos) {
2450 if (!buf->hasInfo(ix)) {
2451 const C2Param *param =
2452 config->getConfigParameterValue(ix.withStream(stream));
2453 if (param) {
2454 std::shared_ptr<C2Param> info(C2Param::Copy(*param));
2455 buf->setInfo(std::static_pointer_cast<C2Info>(info));
2456 }
2457 }
2458 }
2459 }
2460 ++stream;
2461 }
2462 }
2463 if (config->mInputSurface) {
2464 if (work->worklets.empty()
2465 || !work->worklets.back()
2466 || (work->worklets.back()->output.flags
2467 & C2FrameData::FLAG_INCOMPLETE) == 0) {
2468 config->mInputSurface->onInputBufferDone(work->input.ordinal.frameIndex);
2469 }
2470 }
2471 if (initDataWatcher.hasChanged()) {
2472 initData = initDataWatcher.update();
2473 AmendOutputFormatWithCodecSpecificData(
2474 initData->m.value, initData->flexCount(), config->mCodingMediaType,
2475 config->mOutputFormat);
2476 }
2477 outputFormat = config->mOutputFormat;
2478 }
2479 mChannel->onWorkDone(
2480 std::move(work), outputFormat, initData ? initData.get() : nullptr);
2481 break;
2482 }
2483 case kWhatWatch: {
2484 // watch message already posted; no-op.
2485 break;
2486 }
2487 default: {
2488 ALOGE("unrecognized message");
2489 break;
2490 }
2491 }
2492 setDeadline(TimePoint::max(), 0ms, "none");
2493 }
2494
setDeadline(const TimePoint & now,const std::chrono::milliseconds & timeout,const char * name)2495 void CCodec::setDeadline(
2496 const TimePoint &now,
2497 const std::chrono::milliseconds &timeout,
2498 const char *name) {
2499 int32_t mult = std::max(1, property_get_int32("debug.stagefright.ccodec_timeout_mult", 1));
2500 Mutexed<NamedTimePoint>::Locked deadline(mDeadline);
2501 deadline->set(now + (timeout * mult), name);
2502 }
2503
configureTunneledVideoPlayback(std::shared_ptr<Codec2Client::Component> comp,sp<NativeHandle> * sidebandHandle,const sp<AMessage> & msg)2504 status_t CCodec::configureTunneledVideoPlayback(
2505 std::shared_ptr<Codec2Client::Component> comp,
2506 sp<NativeHandle> *sidebandHandle,
2507 const sp<AMessage> &msg) {
2508 std::vector<std::unique_ptr<C2SettingResult>> failures;
2509
2510 std::unique_ptr<C2PortTunneledModeTuning::output> tunneledPlayback =
2511 C2PortTunneledModeTuning::output::AllocUnique(
2512 1,
2513 C2PortTunneledModeTuning::Struct::SIDEBAND,
2514 C2PortTunneledModeTuning::Struct::REALTIME,
2515 0);
2516 // TODO: use KEY_AUDIO_HW_SYNC, KEY_HARDWARE_AV_SYNC_ID when they are in MediaCodecConstants.h
2517 if (msg->findInt32("audio-hw-sync", &tunneledPlayback->m.syncId[0])) {
2518 tunneledPlayback->m.syncType = C2PortTunneledModeTuning::Struct::sync_type_t::AUDIO_HW_SYNC;
2519 } else if (msg->findInt32("hw-av-sync-id", &tunneledPlayback->m.syncId[0])) {
2520 tunneledPlayback->m.syncType = C2PortTunneledModeTuning::Struct::sync_type_t::HW_AV_SYNC;
2521 } else {
2522 tunneledPlayback->m.syncType = C2PortTunneledModeTuning::Struct::sync_type_t::REALTIME;
2523 tunneledPlayback->setFlexCount(0);
2524 }
2525 c2_status_t c2err = comp->config({ tunneledPlayback.get() }, C2_MAY_BLOCK, &failures);
2526 if (c2err != C2_OK) {
2527 return UNKNOWN_ERROR;
2528 }
2529
2530 if (sidebandHandle == nullptr) {
2531 return OK;
2532 }
2533
2534 std::vector<std::unique_ptr<C2Param>> params;
2535 c2err = comp->query({}, {C2PortTunnelHandleTuning::output::PARAM_TYPE}, C2_DONT_BLOCK, ¶ms);
2536 if (c2err == C2_OK && params.size() == 1u) {
2537 C2PortTunnelHandleTuning::output *videoTunnelSideband =
2538 C2PortTunnelHandleTuning::output::From(params[0].get());
2539 // Currently, Codec2 only supports non-fd case for sideband native_handle.
2540 native_handle_t *handle = native_handle_create(0, videoTunnelSideband->flexCount());
2541 *sidebandHandle = NativeHandle::create(handle, true /* ownsHandle */);
2542 if (handle != nullptr && videoTunnelSideband->flexCount()) {
2543 memcpy(handle->data, videoTunnelSideband->m.values,
2544 sizeof(int32_t) * videoTunnelSideband->flexCount());
2545 return OK;
2546 } else {
2547 return NO_MEMORY;
2548 }
2549 }
2550 return UNKNOWN_ERROR;
2551 }
2552
initiateReleaseIfStuck()2553 void CCodec::initiateReleaseIfStuck() {
2554 std::string name;
2555 bool pendingDeadline = false;
2556 {
2557 Mutexed<NamedTimePoint>::Locked deadline(mDeadline);
2558 if (deadline->get() < std::chrono::steady_clock::now()) {
2559 name = deadline->getName();
2560 }
2561 if (deadline->get() != TimePoint::max()) {
2562 pendingDeadline = true;
2563 }
2564 }
2565 bool tunneled = false;
2566 bool isMediaTypeKnown = false;
2567 {
2568 static const std::set<std::string> kKnownMediaTypes{
2569 MIMETYPE_VIDEO_VP8,
2570 MIMETYPE_VIDEO_VP9,
2571 MIMETYPE_VIDEO_AV1,
2572 MIMETYPE_VIDEO_AVC,
2573 MIMETYPE_VIDEO_HEVC,
2574 MIMETYPE_VIDEO_MPEG4,
2575 MIMETYPE_VIDEO_H263,
2576 MIMETYPE_VIDEO_MPEG2,
2577 MIMETYPE_VIDEO_RAW,
2578 MIMETYPE_VIDEO_DOLBY_VISION,
2579
2580 MIMETYPE_AUDIO_AMR_NB,
2581 MIMETYPE_AUDIO_AMR_WB,
2582 MIMETYPE_AUDIO_MPEG,
2583 MIMETYPE_AUDIO_AAC,
2584 MIMETYPE_AUDIO_QCELP,
2585 MIMETYPE_AUDIO_VORBIS,
2586 MIMETYPE_AUDIO_OPUS,
2587 MIMETYPE_AUDIO_G711_ALAW,
2588 MIMETYPE_AUDIO_G711_MLAW,
2589 MIMETYPE_AUDIO_RAW,
2590 MIMETYPE_AUDIO_FLAC,
2591 MIMETYPE_AUDIO_MSGSM,
2592 MIMETYPE_AUDIO_AC3,
2593 MIMETYPE_AUDIO_EAC3,
2594
2595 MIMETYPE_IMAGE_ANDROID_HEIC,
2596 };
2597 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2598 const std::unique_ptr<Config> &config = *configLocked;
2599 tunneled = config->mTunneled;
2600 isMediaTypeKnown = (kKnownMediaTypes.count(config->mCodingMediaType) != 0);
2601 }
2602 if (!tunneled && isMediaTypeKnown && name.empty()) {
2603 constexpr std::chrono::steady_clock::duration kWorkDurationThreshold = 3s;
2604 std::chrono::steady_clock::duration elapsed = mChannel->elapsed();
2605 if (elapsed >= kWorkDurationThreshold) {
2606 name = "queue";
2607 }
2608 if (elapsed > 0s) {
2609 pendingDeadline = true;
2610 }
2611 }
2612 if (name.empty()) {
2613 // We're not stuck.
2614 if (pendingDeadline) {
2615 // If we are not stuck yet but still has deadline coming up,
2616 // post watch message to check back later.
2617 (new AMessage(kWhatWatch, this))->post();
2618 }
2619 return;
2620 }
2621
2622 C2String compName;
2623 {
2624 Mutexed<State>::Locked state(mState);
2625 if (!state->comp) {
2626 ALOGD("previous call to %s exceeded timeout "
2627 "and the component is already released", name.c_str());
2628 return;
2629 }
2630 compName = state->comp->getName();
2631 }
2632 ALOGW("[%s] previous call to %s exceeded timeout", compName.c_str(), name.c_str());
2633
2634 initiateRelease(false);
2635 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2636 }
2637
2638 // static
CreateInputSurface()2639 PersistentSurface *CCodec::CreateInputSurface() {
2640 using namespace android;
2641 using ::android::hardware::media::omx::V1_0::implementation::TWGraphicBufferSource;
2642 // Attempt to create a Codec2's input surface.
2643 std::shared_ptr<Codec2Client::InputSurface> inputSurface =
2644 Codec2Client::CreateInputSurface();
2645 if (!inputSurface) {
2646 if (property_get_int32("debug.stagefright.c2inputsurface", 0) == -1) {
2647 sp<IGraphicBufferProducer> gbp;
2648 sp<OmxGraphicBufferSource> gbs = new OmxGraphicBufferSource();
2649 status_t err = gbs->initCheck();
2650 if (err != OK) {
2651 ALOGE("Failed to create persistent input surface: error %d", err);
2652 return nullptr;
2653 }
2654 return new PersistentSurface(
2655 gbs->getIGraphicBufferProducer(), new TWGraphicBufferSource(gbs));
2656 } else {
2657 return nullptr;
2658 }
2659 }
2660 return new PersistentSurface(
2661 inputSurface->getGraphicBufferProducer(),
2662 static_cast<sp<android::hidl::base::V1_0::IBase>>(
2663 inputSurface->getHalInterface()));
2664 }
2665
2666 class IntfCache {
2667 public:
2668 IntfCache() = default;
2669
init(const std::string & name)2670 status_t init(const std::string &name) {
2671 std::shared_ptr<Codec2Client::Interface> intf{
2672 Codec2Client::CreateInterfaceByName(name.c_str())};
2673 if (!intf) {
2674 ALOGW("IntfCache [%s]: Unrecognized interface name", name.c_str());
2675 mInitStatus = NO_INIT;
2676 return NO_INIT;
2677 }
2678 const static C2StreamUsageTuning::input sUsage{0u /* stream id */};
2679 mFields.push_back(C2FieldSupportedValuesQuery::Possible(
2680 C2ParamField{&sUsage, &sUsage.value}));
2681 c2_status_t err = intf->querySupportedValues(mFields, C2_MAY_BLOCK);
2682 if (err != C2_OK) {
2683 ALOGW("IntfCache [%s]: failed to query usage supported value (err=%d)",
2684 name.c_str(), err);
2685 mFields[0].status = err;
2686 }
2687 std::vector<std::unique_ptr<C2Param>> params;
2688 err = intf->query(
2689 {&mApiFeatures},
2690 {
2691 C2StreamBufferTypeSetting::input::PARAM_TYPE,
2692 C2PortAllocatorsTuning::input::PARAM_TYPE
2693 },
2694 C2_MAY_BLOCK,
2695 ¶ms);
2696 if (err != C2_OK && err != C2_BAD_INDEX) {
2697 ALOGW("IntfCache [%s]: failed to query api features (err=%d)",
2698 name.c_str(), err);
2699 }
2700 while (!params.empty()) {
2701 C2Param *param = params.back().release();
2702 params.pop_back();
2703 if (!param) {
2704 continue;
2705 }
2706 if (param->type() == C2StreamBufferTypeSetting::input::PARAM_TYPE) {
2707 mInputStreamFormat.reset(
2708 C2StreamBufferTypeSetting::input::From(param));
2709 } else if (param->type() == C2PortAllocatorsTuning::input::PARAM_TYPE) {
2710 mInputAllocators.reset(
2711 C2PortAllocatorsTuning::input::From(param));
2712 }
2713 }
2714 mInitStatus = OK;
2715 return OK;
2716 }
2717
initCheck() const2718 status_t initCheck() const { return mInitStatus; }
2719
getUsageSupportedValues() const2720 const C2FieldSupportedValuesQuery &getUsageSupportedValues() const {
2721 CHECK_EQ(1u, mFields.size());
2722 return mFields[0];
2723 }
2724
getApiFeatures() const2725 const C2ApiFeaturesSetting &getApiFeatures() const {
2726 return mApiFeatures;
2727 }
2728
getInputStreamFormat() const2729 const C2StreamBufferTypeSetting::input &getInputStreamFormat() const {
2730 static std::unique_ptr<C2StreamBufferTypeSetting::input> sInvalidated = []{
2731 std::unique_ptr<C2StreamBufferTypeSetting::input> param;
2732 param.reset(new C2StreamBufferTypeSetting::input(0u, C2BufferData::INVALID));
2733 param->invalidate();
2734 return param;
2735 }();
2736 return mInputStreamFormat ? *mInputStreamFormat : *sInvalidated;
2737 }
2738
getInputAllocators() const2739 const C2PortAllocatorsTuning::input &getInputAllocators() const {
2740 static std::unique_ptr<C2PortAllocatorsTuning::input> sInvalidated = []{
2741 std::unique_ptr<C2PortAllocatorsTuning::input> param =
2742 C2PortAllocatorsTuning::input::AllocUnique(0);
2743 param->invalidate();
2744 return param;
2745 }();
2746 return mInputAllocators ? *mInputAllocators : *sInvalidated;
2747 }
2748
2749 private:
2750 status_t mInitStatus{NO_INIT};
2751
2752 std::vector<C2FieldSupportedValuesQuery> mFields;
2753 C2ApiFeaturesSetting mApiFeatures;
2754 std::unique_ptr<C2StreamBufferTypeSetting::input> mInputStreamFormat;
2755 std::unique_ptr<C2PortAllocatorsTuning::input> mInputAllocators;
2756 };
2757
GetIntfCache(const std::string & name)2758 static const IntfCache &GetIntfCache(const std::string &name) {
2759 static IntfCache sNullIntfCache;
2760 static std::mutex sMutex;
2761 static std::map<std::string, IntfCache> sCache;
2762 std::unique_lock<std::mutex> lock{sMutex};
2763 auto it = sCache.find(name);
2764 if (it == sCache.end()) {
2765 lock.unlock();
2766 IntfCache intfCache;
2767 status_t err = intfCache.init(name);
2768 if (err != OK) {
2769 return sNullIntfCache;
2770 }
2771 lock.lock();
2772 it = sCache.insert({name, std::move(intfCache)}).first;
2773 }
2774 return it->second;
2775 }
2776
GetCommonAllocatorIds(const std::vector<std::string> & names,C2Allocator::type_t type,std::set<C2Allocator::id_t> * ids)2777 static status_t GetCommonAllocatorIds(
2778 const std::vector<std::string> &names,
2779 C2Allocator::type_t type,
2780 std::set<C2Allocator::id_t> *ids) {
2781 int poolMask = GetCodec2PoolMask();
2782 C2PlatformAllocatorStore::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask);
2783 C2Allocator::id_t defaultAllocatorId =
2784 (type == C2Allocator::LINEAR) ? preferredLinearId : C2PlatformAllocatorStore::GRALLOC;
2785
2786 ids->clear();
2787 if (names.empty()) {
2788 return OK;
2789 }
2790 bool firstIteration = true;
2791 for (const std::string &name : names) {
2792 const IntfCache &intfCache = GetIntfCache(name);
2793 if (intfCache.initCheck() != OK) {
2794 continue;
2795 }
2796 const C2StreamBufferTypeSetting::input &streamFormat = intfCache.getInputStreamFormat();
2797 if (streamFormat) {
2798 C2Allocator::type_t allocatorType = C2Allocator::LINEAR;
2799 if (streamFormat.value == C2BufferData::GRAPHIC
2800 || streamFormat.value == C2BufferData::GRAPHIC_CHUNKS) {
2801 allocatorType = C2Allocator::GRAPHIC;
2802 }
2803
2804 if (type != allocatorType) {
2805 // requested type is not supported at input allocators
2806 ids->clear();
2807 ids->insert(defaultAllocatorId);
2808 ALOGV("name(%s) does not support a type(0x%x) as input allocator."
2809 " uses default allocator id(%d)", name.c_str(), type, defaultAllocatorId);
2810 break;
2811 }
2812 }
2813
2814 const C2PortAllocatorsTuning::input &allocators = intfCache.getInputAllocators();
2815 if (firstIteration) {
2816 firstIteration = false;
2817 if (allocators && allocators.flexCount() > 0) {
2818 ids->insert(allocators.m.values,
2819 allocators.m.values + allocators.flexCount());
2820 }
2821 if (ids->empty()) {
2822 // The component does not advertise allocators. Use default.
2823 ids->insert(defaultAllocatorId);
2824 }
2825 continue;
2826 }
2827 bool filtered = false;
2828 if (allocators && allocators.flexCount() > 0) {
2829 filtered = true;
2830 for (auto it = ids->begin(); it != ids->end(); ) {
2831 bool found = false;
2832 for (size_t j = 0; j < allocators.flexCount(); ++j) {
2833 if (allocators.m.values[j] == *it) {
2834 found = true;
2835 break;
2836 }
2837 }
2838 if (found) {
2839 ++it;
2840 } else {
2841 it = ids->erase(it);
2842 }
2843 }
2844 }
2845 if (!filtered) {
2846 // The component does not advertise supported allocators. Use default.
2847 bool containsDefault = (ids->count(defaultAllocatorId) > 0u);
2848 if (ids->size() != (containsDefault ? 1 : 0)) {
2849 ids->clear();
2850 if (containsDefault) {
2851 ids->insert(defaultAllocatorId);
2852 }
2853 }
2854 }
2855 }
2856 // Finally, filter with pool masks
2857 for (auto it = ids->begin(); it != ids->end(); ) {
2858 if ((poolMask >> *it) & 1) {
2859 ++it;
2860 } else {
2861 it = ids->erase(it);
2862 }
2863 }
2864 return OK;
2865 }
2866
CalculateMinMaxUsage(const std::vector<std::string> & names,uint64_t * minUsage,uint64_t * maxUsage)2867 static status_t CalculateMinMaxUsage(
2868 const std::vector<std::string> &names, uint64_t *minUsage, uint64_t *maxUsage) {
2869 static C2StreamUsageTuning::input sUsage{0u /* stream id */};
2870 *minUsage = 0;
2871 *maxUsage = ~0ull;
2872 for (const std::string &name : names) {
2873 const IntfCache &intfCache = GetIntfCache(name);
2874 if (intfCache.initCheck() != OK) {
2875 continue;
2876 }
2877 const C2FieldSupportedValuesQuery &usageSupportedValues =
2878 intfCache.getUsageSupportedValues();
2879 if (usageSupportedValues.status != C2_OK) {
2880 continue;
2881 }
2882 const C2FieldSupportedValues &supported = usageSupportedValues.values;
2883 if (supported.type != C2FieldSupportedValues::FLAGS) {
2884 continue;
2885 }
2886 if (supported.values.empty()) {
2887 *maxUsage = 0;
2888 continue;
2889 }
2890 if (supported.values.size() > 1) {
2891 *minUsage |= supported.values[1].u64;
2892 } else {
2893 *minUsage |= supported.values[0].u64;
2894 }
2895 int64_t currentMaxUsage = 0;
2896 for (const C2Value::Primitive &flags : supported.values) {
2897 currentMaxUsage |= flags.u64;
2898 }
2899 *maxUsage &= currentMaxUsage;
2900 }
2901 return OK;
2902 }
2903
2904 // static
CanFetchLinearBlock(const std::vector<std::string> & names,const C2MemoryUsage & usage,bool * isCompatible)2905 status_t CCodec::CanFetchLinearBlock(
2906 const std::vector<std::string> &names, const C2MemoryUsage &usage, bool *isCompatible) {
2907 for (const std::string &name : names) {
2908 const IntfCache &intfCache = GetIntfCache(name);
2909 if (intfCache.initCheck() != OK) {
2910 continue;
2911 }
2912 const C2ApiFeaturesSetting &features = intfCache.getApiFeatures();
2913 if (features && !(features.value & API_SAME_INPUT_BUFFER)) {
2914 *isCompatible = false;
2915 return OK;
2916 }
2917 }
2918 std::set<C2Allocator::id_t> allocators;
2919 GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators);
2920 if (allocators.empty()) {
2921 *isCompatible = false;
2922 return OK;
2923 }
2924
2925 uint64_t minUsage = 0;
2926 uint64_t maxUsage = ~0ull;
2927 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
2928 minUsage |= usage.expected;
2929 *isCompatible = ((maxUsage & minUsage) == minUsage);
2930 return OK;
2931 }
2932
GetPool(C2Allocator::id_t allocId)2933 static std::shared_ptr<C2BlockPool> GetPool(C2Allocator::id_t allocId) {
2934 static std::mutex sMutex{};
2935 static std::map<C2Allocator::id_t, std::shared_ptr<C2BlockPool>> sPools;
2936 std::unique_lock<std::mutex> lock{sMutex};
2937 std::shared_ptr<C2BlockPool> pool;
2938 auto it = sPools.find(allocId);
2939 if (it == sPools.end()) {
2940 c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool);
2941 if (err == OK) {
2942 sPools.emplace(allocId, pool);
2943 } else {
2944 pool.reset();
2945 }
2946 } else {
2947 pool = it->second;
2948 }
2949 return pool;
2950 }
2951
2952 // static
FetchLinearBlock(size_t capacity,const C2MemoryUsage & usage,const std::vector<std::string> & names)2953 std::shared_ptr<C2LinearBlock> CCodec::FetchLinearBlock(
2954 size_t capacity, const C2MemoryUsage &usage, const std::vector<std::string> &names) {
2955 std::set<C2Allocator::id_t> allocators;
2956 GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators);
2957 if (allocators.empty()) {
2958 allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR);
2959 }
2960
2961 uint64_t minUsage = 0;
2962 uint64_t maxUsage = ~0ull;
2963 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
2964 minUsage |= usage.expected;
2965 if ((maxUsage & minUsage) != minUsage) {
2966 allocators.clear();
2967 allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR);
2968 }
2969 std::shared_ptr<C2LinearBlock> block;
2970 for (C2Allocator::id_t allocId : allocators) {
2971 std::shared_ptr<C2BlockPool> pool = GetPool(allocId);
2972 if (!pool) {
2973 continue;
2974 }
2975 c2_status_t err = pool->fetchLinearBlock(capacity, C2MemoryUsage{minUsage}, &block);
2976 if (err != C2_OK || !block) {
2977 block.reset();
2978 continue;
2979 }
2980 break;
2981 }
2982 return block;
2983 }
2984
2985 // static
CanFetchGraphicBlock(const std::vector<std::string> & names,bool * isCompatible)2986 status_t CCodec::CanFetchGraphicBlock(
2987 const std::vector<std::string> &names, bool *isCompatible) {
2988 uint64_t minUsage = 0;
2989 uint64_t maxUsage = ~0ull;
2990 std::set<C2Allocator::id_t> allocators;
2991 GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators);
2992 if (allocators.empty()) {
2993 *isCompatible = false;
2994 return OK;
2995 }
2996 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
2997 *isCompatible = ((maxUsage & minUsage) == minUsage);
2998 return OK;
2999 }
3000
3001 // static
FetchGraphicBlock(int32_t width,int32_t height,int32_t format,uint64_t usage,const std::vector<std::string> & names)3002 std::shared_ptr<C2GraphicBlock> CCodec::FetchGraphicBlock(
3003 int32_t width,
3004 int32_t height,
3005 int32_t format,
3006 uint64_t usage,
3007 const std::vector<std::string> &names) {
3008 uint32_t halPixelFormat = HAL_PIXEL_FORMAT_YCBCR_420_888;
3009 if (!C2Mapper::mapPixelFormatFrameworkToCodec(format, &halPixelFormat)) {
3010 ALOGD("Unrecognized pixel format: %d", format);
3011 return nullptr;
3012 }
3013 uint64_t minUsage = 0;
3014 uint64_t maxUsage = ~0ull;
3015 std::set<C2Allocator::id_t> allocators;
3016 GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators);
3017 if (allocators.empty()) {
3018 allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC);
3019 }
3020 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
3021 minUsage |= usage;
3022 if ((maxUsage & minUsage) != minUsage) {
3023 allocators.clear();
3024 allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC);
3025 }
3026 std::shared_ptr<C2GraphicBlock> block;
3027 for (C2Allocator::id_t allocId : allocators) {
3028 std::shared_ptr<C2BlockPool> pool;
3029 c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool);
3030 if (err != C2_OK || !pool) {
3031 continue;
3032 }
3033 err = pool->fetchGraphicBlock(
3034 width, height, halPixelFormat, C2MemoryUsage{minUsage}, &block);
3035 if (err != C2_OK || !block) {
3036 block.reset();
3037 continue;
3038 }
3039 break;
3040 }
3041 return block;
3042 }
3043
3044 } // namespace android
3045