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