blob: ca0aabb7d08574237c0e6bdd6d1b2d5c013265db [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
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
Ram Mohan16511072023-12-11 19:41:31 +053024#include <android_media_codec.h>
25
Pawin Vongmasa36653902018-11-15 00:10:25 -080026#include <C2Config.h>
27#include <C2Debug.h>
28#include <C2ParamInternal.h>
29#include <C2PlatformSupport.h>
30
Sungtak Lee92bca442024-03-12 09:10:31 +000031#include <aidl/android/hardware/graphics/common/Dataspace.h>
32#include <aidl/android/media/IAidlGraphicBufferSource.h>
33#include <aidl/android/media/IAidlBufferSource.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080034#include <android/IOMXBufferSource.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070035#include <android/hardware/media/c2/1.0/IInputSurface.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080036#include <android/hardware/media/omx/1.0/IGraphicBufferSource.h>
37#include <android/hardware/media/omx/1.0/IOmx.h>
Wonsik Kim50811882022-04-28 15:57:27 -070038#include <android-base/properties.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080039#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>
Wonsik Kim9917d4a2019-10-24 12:56:38 -070044#include <media/omx/1.0/WOmxNode.h>
45#include <media/openmax/OMX_Core.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080046#include <media/openmax/OMX_IndexExt.h>
Wonsik Kim1f5063d2021-05-03 15:41:17 -070047#include <media/stagefright/foundation/avc_utils.h>
Harish Mahendrakarf6b3e5e2024-03-21 21:04:28 +000048#include <media/stagefright/foundation/AUtils.h>
Sungtak Lee92bca442024-03-12 09:10:31 +000049#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>
Wonsik Kim9917d4a2019-10-24 12:56:38 -070053#include <media/stagefright/omx/1.0/WGraphicBufferSource.h>
54#include <media/stagefright/omx/OmxGraphicBufferSource.h>
Wonsik Kim155d5cb2019-10-09 12:49:49 -070055#include <media/stagefright/CCodec.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080056#include <media/stagefright/BufferProducerWrapper.h>
57#include <media/stagefright/MediaCodecConstants.h>
Songyue Han1e6769b2023-08-30 18:09:27 +000058#include <media/stagefright/MediaCodecMetricsConstants.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080059#include <media/stagefright/PersistentSurface.h>
Brian Lindahlff74e9d2023-07-20 14:44:04 -060060#include <media/stagefright/RenderedFrameInfo.h>
ted.sun765db4d2020-06-23 14:03:41 +080061#include <utils/NativeHandle.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080062
Sungtak Lee92bca442024-03-12 09:10:31 +000063#include "C2AidlNode.h"
Sungtak Lee64c9d932024-03-26 03:46:53 +000064#include "C2OMXNode.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080065#include "CCodecBufferChannel.h"
Wonsik Kim155d5cb2019-10-09 12:49:49 -070066#include "CCodecConfig.h"
Wonsik Kimfb7a7672019-12-27 17:13:33 -080067#include "Codec2Mapper.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080068#include "InputSurfaceWrapper.h"
69
70extern "C" android::PersistentSurface *CreateInputSurface();
71
72namespace android {
73
74using namespace std::chrono_literals;
75using ::android::hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer;
76using android::base::StringPrintf;
Pawin Vongmasad0f0e142018-11-15 03:36:28 -080077using ::android::hardware::media::c2::V1_0::IInputSurface;
Sungtak Lee92bca442024-03-12 09:10:31 +000078using ::aidl::android::media::IAidlBufferSource;
79using ::aidl::android::media::IAidlNode;
80using ::android::media::AidlGraphicBufferSource;
81using ::android::media::WAidlGraphicBufferSource;
82using ::android::media::aidl_conversion::fromAidlStatus;
Pawin Vongmasa36653902018-11-15 00:10:25 -080083
Wonsik Kim9917d4a2019-10-24 12:56:38 -070084typedef hardware::media::omx::V1_0::IGraphicBufferSource HGraphicBufferSource;
Sungtak Lee92bca442024-03-12 09:10:31 +000085typedef aidl::android::media::IAidlGraphicBufferSource AGraphicBufferSource;
Wonsik Kim155d5cb2019-10-09 12:49:49 -070086typedef CCodecConfig Config;
Wonsik Kim9917d4a2019-10-24 12:56:38 -070087
Pawin Vongmasa36653902018-11-15 00:10:25 -080088namespace {
89
90class CCodecWatchdog : public AHandler {
91private:
92 enum {
93 kWhatWatch,
94 };
95 constexpr static int64_t kWatchIntervalUs = 3300000; // 3.3 secs
96
97public:
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
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
123protected:
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
146private:
147 CCodecWatchdog() : mLooper(new ALooper) {}
148
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
161class C2InputSurfaceWrapper : public InputSurfaceWrapper {
162public:
163 explicit C2InputSurfaceWrapper(
164 const std::shared_ptr<Codec2Client::InputSurface> &surface) :
165 mSurface(surface) {
166 }
167
168 ~C2InputSurfaceWrapper() override = default;
169
170 status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override {
171 if (mConnection != nullptr) {
172 return ALREADY_EXISTS;
173 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800174 return toStatusT(comp->connectToInputSurface(mSurface, &mConnection));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800175 }
176
177 void disconnect() override {
178 if (mConnection != nullptr) {
179 mConnection->disconnect();
180 mConnection = nullptr;
181 }
182 }
183
184 status_t start() override {
185 // InputSurface does not distinguish started state
186 return OK;
187 }
188
189 status_t signalEndOfInputStream() override {
190 C2InputSurfaceEosTuning eos(true);
191 std::vector<std::unique_ptr<C2SettingResult>> failures;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800192 c2_status_t err = mSurface->config({&eos}, C2_MAY_BLOCK, &failures);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800193 if (err != C2_OK) {
194 return UNKNOWN_ERROR;
195 }
196 return OK;
197 }
198
199 status_t configure(Config &config __unused) {
200 // TODO
201 return OK;
202 }
203
204private:
205 std::shared_ptr<Codec2Client::InputSurface> mSurface;
206 std::shared_ptr<Codec2Client::InputSurfaceConnection> mConnection;
207};
208
Sungtak Lee92bca442024-03-12 09:10:31 +0000209class HGraphicBufferSourceWrapper : public InputSurfaceWrapper {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800210public:
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700211 typedef hardware::media::omx::V1_0::Status OmxStatus;
212
Sungtak Lee92bca442024-03-12 09:10:31 +0000213 HGraphicBufferSourceWrapper(
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700214 const sp<HGraphicBufferSource> &source,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800215 uint32_t width,
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700216 uint32_t height,
217 uint64_t usage)
Pawin Vongmasa36653902018-11-15 00:10:25 -0800218 : mSource(source), mWidth(width), mHeight(height) {
219 mDataSpace = HAL_DATASPACE_BT709;
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700220 mConfig.mUsage = usage;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800221 }
Sungtak Lee92bca442024-03-12 09:10:31 +0000222 ~HGraphicBufferSourceWrapper() override = default;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800223
224 status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override {
225 mNode = new C2OMXNode(comp);
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700226 mOmxNode = new hardware::media::omx::V1_0::utils::TWOmxNode(mNode);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800227 mNode->setFrameSize(mWidth, mHeight);
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700228 // Usage is queried during configure(), so setting it beforehand.
Sungtak Lee0cd4fbc2023-02-02 00:59:01 +0000229 // 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 }
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700241
Yanqiang Fanc56f3e62021-09-28 16:54:07 +0800242 return GetStatus(mSource->configure(
243 mOmxNode, static_cast<hardware::graphics::common::V1_0::Dataspace>(mDataSpace)));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800244 }
245
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();
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700258 mOmxNode.clear();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800259 }
260
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700261 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;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800266 }
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700267 return UNKNOWN_ERROR;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800268 }
269
270 status_t start() override {
271 sp<IOMXBufferSource> source = mNode->getSource();
272 if (source == nullptr) {
273 return NO_INIT;
274 }
Taehwan Kim8b3bcdd2020-11-26 22:40:40 +0900275
Wonsik Kim0f6b61d2021-01-05 18:55:22 -0800276 size_t numSlots = 16;
Wonsik Kim34d66012021-03-01 16:40:33 -0800277 constexpr OMX_U32 kPortIndexInput = 0;
Taehwan Kim8b3bcdd2020-11-26 22:40:40 +0900278
Wonsik Kim34d66012021-03-01 16:40:33 -0800279 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;
Taehwan Kim8b3bcdd2020-11-26 22:40:40 +0900285 }
286
287 for (size_t i = 0; i < numSlots; ++i) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800288 source->onInputBufferAdded(i);
289 }
290
291 source->onOmxExecuting();
292 return OK;
293 }
294
295 status_t signalEndOfInputStream() override {
296 return GetStatus(mSource->signalEndOfInputStream());
297 }
298
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);
Wonsik Kim95ba0162019-03-19 15:51:54 -0700323 float gap = (config.mMinAdjustedFps > 0)
Pawin Vongmasa36653902018-11-15 00:10:25 -0800324 ? c2_min(INT32_MAX + 0., 1e6 / config.mMinAdjustedFps + 0.5)
325 : c2_max(0. - INT32_MAX, -1e6 / config.mFixedAdjustedFps - 0.5);
Wonsik Kim95ba0162019-03-19 15:51:54 -0700326 // 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);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800329 (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
Wonsik Kim95ba0162019-03-19 15:51:54 -0700337 if ((config.mMaxFps > 0 || (config.mFixedAdjustedFps > 0 && config.mMaxFps == -1))
Pawin Vongmasa36653902018-11-15 00:10:25 -0800338 && 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";
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700403 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 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800412 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
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700425 // consumer usage is queried earlier.
426
Wonsik Kima1335e12021-04-22 16:28:29 -0700427 // 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
Wonsik Kimbd557932019-07-02 15:51:20 -0700435 if (status.str().empty()) {
436 ALOGD("ISConfig not changed");
437 } else {
438 ALOGD("ISConfig%s", status.str().c_str());
439 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800440 return err;
441 }
442
Wonsik Kim4f3314d2019-03-26 17:00:34 -0700443 void onInputBufferDone(c2_cntr64_t index) override {
444 mNode->onInputBufferDone(index);
445 }
446
Wonsik Kim1951d932024-05-23 22:59:00 +0000447 void onInputBufferEmptied() override {
448 mNode->onInputBufferEmptied();
449 }
450
Wonsik Kim673dd192021-01-29 14:58:12 -0800451 android_dataspace getDataspace() override {
452 return mNode->getDataspace();
453 }
454
Songyue Hanad01f6a2023-08-17 05:45:35 +0000455 uint32_t getPixelFormat() override {
456 return mNode->getPixelFormat();
457 }
458
Pawin Vongmasa36653902018-11-15 00:10:25 -0800459private:
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700460 sp<HGraphicBufferSource> mSource;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800461 sp<C2OMXNode> mNode;
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700462 sp<hardware::media::omx::V1_0::IOmxNode> mOmxNode;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800463 uint32_t mWidth;
464 uint32_t mHeight;
465 Config mConfig;
466};
467
Sungtak Lee92bca442024-03-12 09:10:31 +0000468class AGraphicBufferSourceWrapper : public InputSurfaceWrapper {
469public:
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
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
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
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
529 status_t signalEndOfInputStream() override {
530 return fromAidlStatus(mSource->signalEndOfInputStream());
531 }
532
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
666 void onInputBufferDone(c2_cntr64_t index) override {
667 mNode->onInputBufferDone(index);
668 }
669
Wonsik Kim1951d932024-05-23 22:59:00 +0000670 void onInputBufferEmptied() override {
671 mNode->onInputBufferEmptied();
672 }
673
Sungtak Lee92bca442024-03-12 09:10:31 +0000674 android_dataspace getDataspace() override {
675 return mNode->getDataspace();
676 }
677
678 uint32_t getPixelFormat() override {
679 return mNode->getPixelFormat();
680 }
681
682private:
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
Pawin Vongmasa36653902018-11-15 00:10:25 -0800690class Codec2ClientInterfaceWrapper : public C2ComponentStore {
691 std::shared_ptr<Codec2Client> mClient;
692
693public:
694 Codec2ClientInterfaceWrapper(std::shared_ptr<Codec2Client> client)
695 : mClient(client) { }
696
697 virtual ~Codec2ClientInterfaceWrapper() = default;
698
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
705 virtual c2_status_t copyBuffer(
706 std::shared_ptr<C2GraphicBuffer>,
707 std::shared_ptr<C2GraphicBuffer>) {
708 return C2_OMITTED;
709 }
710
711 virtual c2_status_t createComponent(
712 C2String, std::shared_ptr<C2Component> *const component) {
713 component->reset();
714 return C2_OMITTED;
715 }
716
717 virtual c2_status_t createInterface(
718 C2String, std::shared_ptr<C2ComponentInterface> *const interface) {
719 interface->reset();
720 return C2_OMITTED;
721 }
722
723 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
730 virtual c2_status_t querySupportedParams_nb(
731 std::vector<std::shared_ptr<C2ParamDescriptor>> *const params) const {
732 return mClient->querySupportedParams(params);
733 }
734
735 virtual c2_status_t querySupportedValues_sm(
736 std::vector<C2FieldSupportedValuesQuery> &fields) const {
737 return mClient->querySupportedValues(fields, C2_MAY_BLOCK);
738 }
739
740 virtual C2String getName() const {
741 return mClient->getName();
742 }
743
744 virtual std::shared_ptr<C2ParamReflector> getParamReflector() const {
745 return mClient->getParamReflector();
746 }
747
748 virtual std::vector<std::shared_ptr<const C2Component::Traits>> listComponents() {
749 return std::vector<std::shared_ptr<const C2Component::Traits>>();
750 }
751};
752
Wonsik Kim3b4349a2020-11-10 11:54:15 -0800753void 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,
Harish Mahendrakar8c537502021-02-23 21:20:22 -0800758 KEY_FRAME_RATE,
Wonsik Kim3b4349a2020-11-10 11:54:15 -0800759 KEY_MAX_BIT_RATE,
Harish Mahendrakar8c537502021-02-23 21:20:22 -0800760 KEY_MAX_WIDTH,
761 KEY_MAX_HEIGHT,
Wonsik Kim3b4349a2020-11-10 11:54:15 -0800762 "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
Wonsik Kim1f5063d2021-05-03 15:41:17 -0700781void AmendOutputFormatWithCodecSpecificData(
Greg Kaiserf2572aa2021-05-10 12:50:27 -0700782 const uint8_t *data, size_t size, const std::string &mediaType,
Wonsik Kim1f5063d2021-05-03 15:41:17 -0700783 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
Pawin Vongmasa36653902018-11-15 00:10:25 -0800819} // namespace
820
821// CCodec::ClientListener
822
823struct CCodec::ClientListener : public Codec2Client::Listener {
824
825 explicit ClientListener(const wp<CCodec> &codec) : mCodec(codec) {}
826
827 virtual void onWorkDone(
828 const std::weak_ptr<Codec2Client::Component>& component,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800829 std::list<std::unique_ptr<C2Work>>& workItems) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800830 (void)component;
831 sp<CCodec> codec(mCodec.promote());
832 if (!codec) {
833 return;
834 }
Wonsik Kimab34ed62019-01-31 15:28:46 -0800835 codec->onWorkDone(workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800836 }
837
838 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
847 virtual void onError(
848 const std::weak_ptr<Codec2Client::Component>& component,
849 uint32_t errorCode) override {
Praveen Chavan72eff012020-11-20 23:20:28 -0800850 {
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
Wonsik Kim10f33c02021-03-04 15:04:14 -0800861 // 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.
Praveen Chavan72eff012020-11-20 23:20:28 -0800863 sp<CCodec> codec(mCodec.promote());
864 if (!codec || !codec->mCallback) {
865 return;
866 }
Wonsik Kim10f33c02021-03-04 15:04:14 -0800867 codec->mCallback->onError(
868 errorCode == C2_NO_MEMORY ? NO_MEMORY : UNKNOWN_ERROR,
869 ACTION_CODE_FATAL);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800870 }
871
872 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
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800891 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;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800898 }
899
900 virtual void onInputBufferDone(
Wonsik Kimab34ed62019-01-31 15:28:46 -0800901 uint64_t frameIndex, size_t arrayIndex) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800902 sp<CCodec> codec(mCodec.promote());
903 if (codec) {
Wonsik Kimab34ed62019-01-31 15:28:46 -0800904 codec->onInputBufferDone(frameIndex, arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800905 }
906 }
907
908private:
909 wp<CCodec> mCodec;
910};
911
912// CCodecCallbackImpl
913
914class CCodecCallbackImpl : public CCodecCallback {
915public:
916 explicit CCodecCallbackImpl(CCodec *codec) : mCodec(codec) {}
917 ~CCodecCallbackImpl() override = default;
918
919 void onError(status_t err, enum ActionCode actionCode) override {
920 mCodec->mCallback->onError(err, actionCode);
921 }
922
923 void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) override {
Brian Lindahlff74e9d2023-07-20 14:44:04 -0600924 mCodec->mCallback->onOutputFramesRendered({RenderedFrameInfo(mediaTimeUs, renderTimeNs)});
Pawin Vongmasa36653902018-11-15 00:10:25 -0800925 }
926
Pawin Vongmasa36653902018-11-15 00:10:25 -0800927 void onOutputBuffersChanged() override {
928 mCodec->mCallback->onOutputBuffersChanged();
929 }
930
Guillaume Chelfi5ffbcb32021-04-12 14:23:43 +0200931 void onFirstTunnelFrameReady() override {
932 mCodec->mCallback->onFirstTunnelFrameReady();
933 }
934
Pawin Vongmasa36653902018-11-15 00:10:25 -0800935private:
936 CCodec *mCodec;
937};
938
939// CCodec
940
941CCodec::CCodec()
Wonsik Kim155d5cb2019-10-09 12:49:49 -0700942 : mChannel(new CCodecBufferChannel(std::make_shared<CCodecCallbackImpl>(this))),
943 mConfig(new CCodecConfig) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800944}
945
946CCodec::~CCodec() {
947}
948
949std::shared_ptr<BufferChannelBase> CCodec::getBufferChannel() {
950 return mChannel;
951}
952
953status_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
961void 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
983void 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
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700995 client = Codec2Client::CreateFromService("default");
Pawin Vongmasa36653902018-11-15 00:10:25 -0800996 if (client) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800997 ALOGI("setting up '%s' as default (vendor) store", client->getServiceName().c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800998 SetPreferredCodec2ComponentStore(
999 std::make_shared<Codec2ClientInterfaceWrapper>(client));
1000 }
1001
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001002 std::shared_ptr<Codec2Client::Component> comp;
1003 c2_status_t status = Codec2Client::CreateComponentByName(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001004 componentName.c_str(),
1005 mClientListener,
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001006 &comp,
Pawin Vongmasa36653902018-11-15 00:10:25 -08001007 &client);
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001008 if (status != C2_OK) {
1009 ALOGE("Failed Create component: %s, error=%d", componentName.c_str(), status);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001010 Mutexed<State>::Locked state(mState);
1011 state->set(RELEASED);
1012 state.unlock();
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001013 mCallback->onError((status == C2_NO_MEMORY ? NO_MEMORY : UNKNOWN_ERROR), ACTION_CODE_FATAL);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001014 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
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001035 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1036 const std::unique_ptr<Config> &config = *configLocked;
Wonsik Kim8a6ed372019-12-03 16:05:51 -08001037 status_t err = config->initialize(mClient->getParamReflector(), comp);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001038 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
1047void 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
1061void 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
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001087 int32_t flags;
1088 if (!msg->findInt32("flags", &flags)) {
1089 return BAD_VALUE;
1090 }
1091
Pawin Vongmasa36653902018-11-15 00:10:25 -08001092 // 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
ted.sun765db4d2020-06-23 14:03:41 +08001108 status_t err = OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001109 sp<RefBase> obj;
1110 sp<Surface> surface;
1111 if (msg->findObject("native-window", &obj)) {
1112 surface = static_cast<Surface *>(obj.get());
Sungtak Lee214ce612023-11-01 10:01:13 +00001113 int32_t generation;
1114 (void)msg->findInt32("native-window-generation", &generation);
ted.sun765db4d2020-06-23 14:03:41 +08001115 // 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 }
Guillaume Chelfi2d4c9db2022-03-18 13:43:49 +01001132
1133 int32_t pushBlankBuffersOnStop = 0;
1134 if (msg->findInt32(KEY_PUSH_BLANK_BUFFERS_ON_STOP, &pushBlankBuffersOnStop)) {
1135 config->mPushBlankBuffersOnStop = pushBlankBuffersOnStop == 1;
1136 }
shuanglong.wang480a8362023-02-17 20:55:51 +08001137 // 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 }
ted.sun765db4d2020-06-23 14:03:41 +08001147 }
1148 }
Sungtak Lee214ce612023-11-01 10:01:13 +00001149 setSurface(surface, (uint32_t)generation);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001150 }
1151
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001152 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1153 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001154 config->mUsingSurface = surface != nullptr;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001155 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 ");
Pawin Vongmasa36653902018-11-15 00:10:25 -08001158
Wonsik Kim1114eea2019-02-25 14:35:24 -08001159 // 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 }
Wonsik Kimd91f3fb2021-02-24 12:35:31 -08001179 int32_t width = 0;
1180 int32_t height = 0;
Wonsik Kim1114eea2019-02-25 14:35:24 -08001181 if (config->mDomain & (Config::IS_IMAGE | Config::IS_VIDEO)) {
Wonsik Kimd91f3fb2021-02-24 12:35:31 -08001182 if (!msg->findInt32(KEY_WIDTH, &width)) {
Wonsik Kim1114eea2019-02-25 14:35:24 -08001183 ALOGD("width is missing, which is required for image/video components.");
1184 return BAD_VALUE;
1185 }
Wonsik Kimd91f3fb2021-02-24 12:35:31 -08001186 if (!msg->findInt32(KEY_HEIGHT, &height)) {
Wonsik Kim1114eea2019-02-25 14:35:24 -08001187 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)) {
Harish Mahendrakar71cbb9d2019-05-21 11:21:27 -07001191 int32_t mode = BITRATE_MODE_VBR;
1192 if (msg->findInt32(KEY_BITRATE_MODE, &mode) && mode == BITRATE_MODE_CQ) {
Harish Mahendrakar817d3182019-03-11 16:37:47 -07001193 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 }
Wonsik Kim1114eea2019-02-25 14:35:24 -08001203 }
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 }
Wonsik Kimaab2eea2019-05-22 10:37:58 -07001209 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 }
Wonsik Kim1114eea2019-02-25 14:35:24 -08001214 }
1215 }
1216
Pawin Vongmasa36653902018-11-15 00:10:25 -08001217 /*
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;
Chong Zhang038e8f82019-02-06 19:05:14 -08001226 if (msg->findInt64(KEY_REPEAT_PREVIOUS_FRAME_AFTER, &value) && value > 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001227 config->mISConfig->mMinFps = 1e6 / value;
1228 }
Wonsik Kim95ba0162019-03-19 15:51:54 -07001229 if (!msg->findFloat(
1230 KEY_MAX_FPS_TO_ENCODER, &config->mISConfig->mMaxFps)) {
1231 config->mISConfig->mMaxFps = -1;
1232 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001233 config->mISConfig->mMinAdjustedFps = 0;
1234 config->mISConfig->mFixedAdjustedFps = 0;
Chong Zhang038e8f82019-02-06 19:05:14 -08001235 if (msg->findInt64(KEY_MAX_PTS_GAP_TO_ENCODER, &value)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001236 if (value < 0 && value >= INT32_MIN) {
1237 config->mISConfig->mFixedAdjustedFps = -1e6 / value;
Wonsik Kim95ba0162019-03-19 15:51:54 -07001238 config->mISConfig->mMaxFps = -1;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001239 } else if (value > 0 && value <= INT32_MAX) {
1240 config->mISConfig->mMinAdjustedFps = 1e6 / value;
1241 }
1242 }
1243 }
1244
1245 {
Wonsik Kim8e55f3a2019-09-03 14:10:37 -07001246 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) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001257 (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;
Chong Zhang038e8f82019-02-06 19:05:14 -08001265 if (msg->findInt32(KEY_CREATE_INPUT_SURFACE_SUSPENDED, &value) && value) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001266 config->mISConfig->mSuspended = true;
1267 }
1268 }
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001269 config->mISConfig->mUsage = 0;
Wonsik Kima1335e12021-04-22 16:28:29 -07001270 config->mISConfig->mPriority = INT_MAX;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001271 }
1272
1273 /*
1274 * Handle desired color format.
1275 */
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001276 int32_t defaultColorFormat = COLOR_FormatYUV420Flexible;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001277 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) {
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001278 int32_t format = 0;
1279 // Query vendor format for Flexible YUV
1280 std::vector<std::unique_ptr<C2Param>> heapParams;
1281 C2StoreFlexiblePixelFormatDescriptorsInfo *pixelFormatInfo = nullptr;
Wonsik Kim50811882022-04-28 15:57:27 -07001282 int vendorSdkVersion = base::GetIntProperty(
1283 "ro.vendor.build.version.sdk", android_get_device_api_level());
guochuang709b48b2022-10-25 20:40:42 +08001284 if (mClient->query(
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001285 {},
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 }
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001295 // 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;
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001299 if (pixelFormatInfo && *pixelFormatInfo) {
1300 for (size_t i = 0; i < pixelFormatInfo->flexCount(); ++i) {
1301 const C2FlexiblePixelFormatDescriptorStruct &desc =
1302 pixelFormatInfo->m.values[i];
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001303 if (desc.subsampling != C2Color::YUV_420
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001304 // 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 }
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001310 if (flexPixelFormat.count(desc.bitDepth) == 0) {
1311 flexPixelFormat.emplace(desc.bitDepth, desc.pixelFormat);
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001312 }
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001313 if (desc.layout == C2Color::PLANAR_PACKED
1314 && flexPlanarPixelFormat.count(desc.bitDepth) == 0) {
1315 flexPlanarPixelFormat.emplace(desc.bitDepth, desc.pixelFormat);
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001316 }
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001317 if (desc.layout == C2Color::SEMIPLANAR_PACKED
1318 && flexSemiPlanarPixelFormat.count(desc.bitDepth) == 0) {
1319 flexSemiPlanarPixelFormat.emplace(desc.bitDepth, desc.pixelFormat);
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001320 }
1321 }
1322 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001323 if (!msg->findInt32(KEY_COLOR_FORMAT, &format)) {
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001324 // Also handle default color format (encoders require color format, so this is only
1325 // needed for decoders.
Pawin Vongmasa36653902018-11-15 00:10:25 -08001326 if (!(config->mDomain & Config::IS_ENCODER)) {
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001327 if (surface == nullptr) {
Wonsik Kim1eb88a92021-03-29 20:44:04 -07001328 const char *prefix = "";
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001329 if (flexSemiPlanarPixelFormat.count(8) != 0) {
Wonsik Kim1eb88a92021-03-29 20:44:04 -07001330 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);
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001337 } else {
1338 format = COLOR_FormatSurface;
1339 }
1340 defaultColorFormat = format;
1341 }
1342 } else {
1343 if ((config->mDomain & Config::IS_ENCODER) || !surface) {
Wonsik Kim2b8579f2022-05-04 13:30:33 -07001344 if (vendorSdkVersion < __ANDROID_API_S__ &&
Taehwan Kim43e715d2022-09-22 12:04:59 +09001345 (format == COLOR_FormatYUV420Planar ||
Wonsik Kim2b8579f2022-05-04 13:30:33 -07001346 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 }
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001354 switch (format) {
1355 case COLOR_FormatYUV420Flexible:
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001356 format = COLOR_FormatYUV420Planar;
1357 if (flexPixelFormat.count(8) != 0) {
1358 format = flexPixelFormat[8];
1359 }
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001360 break;
1361 case COLOR_FormatYUV420Planar:
1362 case COLOR_FormatYUV420PackedPlanar:
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001363 if (flexPlanarPixelFormat.count(8) != 0) {
1364 format = flexPlanarPixelFormat[8];
1365 } else if (flexPixelFormat.count(8) != 0) {
1366 format = flexPixelFormat[8];
1367 }
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001368 break;
1369 case COLOR_FormatYUV420SemiPlanar:
1370 case COLOR_FormatYUV420PackedSemiPlanar:
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001371 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 }
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001384 break;
1385 default:
1386 // No-op
1387 break;
1388 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001389 }
1390 }
1391
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001392 if (format != 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001393 msg->setInt32("android._color-format", format);
1394 }
1395 }
1396
Wonsik Kim77e97c72021-01-20 10:33:22 -08001397 /*
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)) {
Wonsik Kim4f13d112021-03-17 04:37:46 +00001406 ColorAspects aspects;
1407 getColorAspectsFromFormat(msg, aspects);
1408 setDefaultCodecColorAspectsIfNeeded(aspects, width, height);
Wonsik Kim77e97c72021-01-20 10:33:22 -08001409 // TODO: read dataspace / color aspect from the component
Wonsik Kim4f13d112021-03-17 04:37:46 +00001410 setColorAspectsIntoFormat(aspects, const_cast<sp<AMessage> &>(msg));
1411 dataSpace = getDataSpaceForColorAspects(aspects, true /* mayexpand */);
Wonsik Kim77e97c72021-01-20 10:33:22 -08001412 }
1413 msg->setInt32("android._dataspace", (int32_t)dataSpace);
1414 ALOGD("setting dataspace to %x", dataSpace);
1415 }
1416
Wonsik Kim8a6ed372019-12-03 16:05:51 -08001417 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
Ram Mohan16511072023-12-11 19:41:31 +05301424 /*
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,
Harish Mahendrakar38a99c22024-02-26 20:28:05 +05301435 AStringPrintf("%d,%d-%d,%d=%d;", 0, 0, height, width, 0));
Ram Mohan16511072023-12-11 19:41:31 +05301436 }
1437 }
1438 }
1439 }
1440
Pawin Vongmasa36653902018-11-15 00:10:25 -08001441 std::vector<std::unique_ptr<C2Param>> configUpdate;
Wonsik Kimaa484ac2019-02-13 16:54:02 -08001442 // 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 }
ted.sun765db4d2020-06-23 14:03:41 +08001450 err = config->getConfigUpdateFromSdkParams(
Wonsik Kimaa484ac2019-02-13 16:54:02 -08001451 comp, sdkParams, Config::IS_CONFIG, C2_DONT_BLOCK, &configUpdate);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001452 if (err != OK) {
1453 ALOGW("failed to convert configuration to c2 params");
1454 }
Wonsik Kimaab2eea2019-05-22 10:37:58 -07001455
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
Ray Essicka0ae6972021-03-10 19:40:01 -08001471 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
Wonsik Kima1335e12021-04-22 16:28:29 -07001510 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
Pawin Vongmasa36653902018-11-15 00:10:25 -08001520 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);
Wonsik Kim9ca01d32019-04-01 14:45:47 -07001529 C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001530
Wonsik Kim3baecda2021-02-07 22:19:56 -08001531 C2Param::Index colorAspectsRequestIndex =
1532 C2StreamColorAspectsInfo::output::PARAM_TYPE | C2Param::CoreIndex::IS_REQUEST_FLAG;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001533 std::initializer_list<C2Param::Index> indices {
Wonsik Kim3baecda2021-02-07 22:19:56 -08001534 colorAspectsRequestIndex.withStream(0u),
Pawin Vongmasa36653902018-11-15 00:10:25 -08001535 };
Chaejung Lim86c22dc2021-12-23 00:41:05 -08001536 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 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001555 if (c2err != C2_OK && c2err != C2_BAD_INDEX) {
1556 ALOGE("Failed to query component interface: %d", c2err);
1557 return UNKNOWN_ERROR;
1558 }
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001559 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 }
Wonsik Kim666604a2020-05-14 16:57:49 -07001567 config->mInputFormat->setInt64("android._C2MemoryUsage", usage.value);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001568 }
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
Wonsik Kim9ca01d32019-04-01 14:45:47 -07001621 int32_t clientPrepend;
1622 if ((config->mDomain & Config::IS_VIDEO)
1623 && (config->mDomain & Config::IS_ENCODER)
Lajos Molnara2b5f5a2020-10-14 16:36:18 -07001624 && msg->findInt32(KEY_PREPEND_HEADER_TO_SYNC_FRAMES, &clientPrepend)
Wonsik Kim9ca01d32019-04-01 14:45:47 -07001625 && clientPrepend
1626 && (!prepend || prepend.value != PREPEND_HEADER_TO_ALL_SYNC)) {
Lajos Molnara2b5f5a2020-10-14 16:36:18 -07001627 ALOGE("Failed to set KEY_PREPEND_HEADER_TO_SYNC_FRAMES");
Wonsik Kim9ca01d32019-04-01 14:45:47 -07001628 return BAD_VALUE;
1629 }
1630
Wonsik Kimd91f3fb2021-02-24 12:35:31 -08001631 int32_t componentColorFormat = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001632 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;
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001644 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);
Wonsik Kimd91f3fb2021-02-24 12:35:31 -08001649 if (msg->findInt32("android._color-format", &componentColorFormat)) {
1650 config->mInputFormat->setInt32("android._color-format", componentColorFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001651 }
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001652 } else {
1653 config->mOutputFormat->setInt32(KEY_COLOR_FORMAT, format);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001654 }
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
Pawin Vongmasa36653902018-11-15 00:10:25 -08001669 if (config->mDomain & Config::IS_AUDIO) {
Wonsik Kim6f23cfc2021-09-24 05:45:52 -07001670 // set channel-mask
Pawin Vongmasa36653902018-11-15 00:10:25 -08001671 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 }
Wonsik Kim6f23cfc2021-09-24 05:45:52 -07001679
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 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001688 }
1689
Wonsik Kim3baecda2021-02-07 22:19:56 -08001690 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 }
Wonsik Kim3baecda2021-02-07 22:19:56 -08001697
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
Wonsik Kimd91f3fb2021-02-24 12:35:31 -08001712 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(
Harish Mahendrakarf6b3e5e2024-03-21 21:04:28 +00001721 align(width, 2), align(height, 2), componentColorFormat, blockUsage,
1722 {comp->getName()});
Wonsik Kimd91f3fb2021-02-24 12:35:31 -08001723 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
Wonsik Kimec585c32021-10-01 01:11:00 -07001763 if (config->mTunneled) {
1764 config->mOutputFormat->setInt32("android._tunneled", 1);
1765 }
1766
Yushin Cho91873b52021-12-21 04:08:35 -08001767 // 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:
Wonsik Kimeebab652022-06-02 13:01:55 -07001779 config->subscribeToConfigUpdate(
1780 comp,
1781 {
1782 C2AndroidStreamAverageBlockQuantizationInfo::output::PARAM_TYPE,
1783 C2StreamPictureTypeInfo::output::PARAM_TYPE,
1784 });
Yushin Cho91873b52021-12-21 04:08:35 -08001785 break;
1786 case VIDEO_ENCODING_STATISTICS_LEVEL_NONE:
1787 break;
1788 }
1789 }
1790 ALOGD("encoding statistics level = %d", encodingStatisticsLevel);
1791
Wonsik Kimd91f3fb2021-02-24 12:35:31 -08001792 ALOGD("setup formats input: %s",
1793 config->mInputFormat->debugString().c_str());
1794 ALOGD("setup formats output: %s",
Pawin Vongmasa36653902018-11-15 00:10:25 -08001795 config->mOutputFormat->debugString().c_str());
1796 return OK;
1797 };
1798 if (tryAndReportOnError(doConfig) != OK) {
1799 return;
1800 }
1801
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001802 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1803 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001804
Houxiang Dai183ca3d2021-02-04 14:20:11 +08001805 config->queryConfiguration(comp);
1806
Songyue Han1e6769b2023-08-30 18:09:27 +00001807 mMetrics = new AMessage;
1808 mChannel->resetBuffersPixelFormat((config->mDomain & Config::IS_ENCODER) ? true : false);
1809
Pawin Vongmasa36653902018-11-15 00:10:25 -08001810 mCallback->onComponentConfigured(config->mInputFormat, config->mOutputFormat);
1811}
1812
1813void 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
Lajos Molnar47118272019-01-31 16:28:04 -08001833sp<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();
Sungtak Lee47dcb482022-04-15 10:47:08 -07001839 if (omx == nullptr) {
1840 return nullptr;
1841 }
Lajos Molnar47118272019-01-31 16:28:04 -08001842 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;
Pawin Vongmasa18588322019-05-18 01:52:13 -07001849
Chong Zhangc8ce1d82019-03-27 10:18:38 -07001850 using ::android::hardware::Return;
1851 Return<void> transStatus = omx->createInputSurface(
Lajos Molnar47118272019-01-31 16:28:04 -08001852 [&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) {
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001861 return new PersistentSurface(new H2BGraphicBufferProducer(gbp), gbs);
Lajos Molnar47118272019-01-31 16:28:04 -08001862 }
1863
1864 return nullptr;
1865}
1866
1867sp<PersistentSurface> CCodec::CreateCompatibleInputSurface() {
1868 sp<PersistentSurface> surface(CreateInputSurface());
1869
1870 if (surface == nullptr) {
1871 surface = CreateOmxInputSurface();
1872 }
1873
1874 return surface;
1875}
1876
Pawin Vongmasa36653902018-11-15 00:10:25 -08001877void CCodec::createInputSurface() {
1878 status_t err;
1879 sp<IGraphicBufferProducer> bufferProducer;
1880
Pawin Vongmasa36653902018-11-15 00:10:25 -08001881 sp<AMessage> outputFormat;
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001882 uint64_t usage = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001883 {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001884 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1885 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001886 outputFormat = config->mOutputFormat;
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001887 usage = config->mISConfig ? config->mISConfig->mUsage : 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001888 }
1889
Lajos Molnar47118272019-01-31 16:28:04 -08001890 sp<PersistentSurface> persistentSurface = CreateCompatibleInputSurface();
Sungtak Lee92bca442024-03-12 09:10:31 +00001891 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 }
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001907 } else {
Sungtak Lee92bca442024-03-12 09:10:31 +00001908 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 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001931 }
1932
1933 if (err != OK) {
1934 ALOGE("Failed to set up input surface: %d", err);
1935 mCallback->onInputSurfaceCreationFailed(err);
1936 return;
1937 }
1938
Wonsik Kim7b9e6db2021-05-10 13:31:40 -07001939 // 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 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001947 mCallback->onInputSurfaceCreated(
1948 inputFormat,
1949 outputFormat,
1950 new BufferProducerWrapper(bufferProducer));
1951}
1952
1953status_t CCodec::setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface) {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001954 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1955 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001956 config->mUsingSurface = true;
1957
1958 // we are now using surface - apply default color aspects to input format - as well as
1959 // get dataspace
Wonsik Kim8a6ed372019-12-03 16:05:51 -08001960 bool inputFormatChanged = config->updateFormats(Config::IS_INPUT);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001961
1962 // configure dataspace
1963 static_assert(sizeof(int32_t) == sizeof(android_dataspace), "dataspace size mismatch");
Wonsik Kim66b19552021-08-02 16:07:49 -07001964
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);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001984 surface->setDataSpace(dataSpace);
Wonsik Kim66b19552021-08-02 16:07:49 -07001985 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());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001991
1992 status_t err = mChannel->setInputSurface(surface);
1993 if (err != OK) {
1994 // undo input format update
1995 config->mUsingSurface = false;
Wonsik Kim8a6ed372019-12-03 16:05:51 -08001996 (void)config->updateFormats(Config::IS_INPUT);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001997 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
Pawin Vongmasa1f213362019-01-24 06:59:16 -08002007 return OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002008}
2009
2010void CCodec::initiateSetInputSurface(const sp<PersistentSurface> &surface) {
2011 sp<AMessage> msg = new AMessage(kWhatSetInputSurface, this);
2012 msg->setObject("surface", surface);
2013 msg->post();
2014}
2015
2016void CCodec::setInputSurface(const sp<PersistentSurface> &surface) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002017 sp<AMessage> outputFormat;
Wonsik Kim9eac4d12019-05-23 12:58:48 -07002018 uint64_t usage = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002019 {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07002020 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2021 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002022 outputFormat = config->mOutputFormat;
Wonsik Kim9eac4d12019-05-23 12:58:48 -07002023 usage = config->mISConfig ? config->mISConfig->mUsage : 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002024 }
Sungtak Lee92bca442024-03-12 09:10:31 +00002025 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);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002044 return;
2045 }
Wonsik Kim9917d4a2019-10-24 12:56:38 -07002046 } else {
Sungtak Lee92bca442024-03-12 09:10:31 +00002047 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 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002075 }
Wonsik Kim7b9e6db2021-05-10 13:31:40 -07002076 // 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 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002084 mCallback->onInputSurfaceAccepted(inputFormat, outputFormat);
2085}
2086
2087void 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
2103void 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 }
Wonsik Kimd55ed3b2023-06-22 14:42:17 -07002123
2124 // clear the deadline after the component starts
2125 setDeadline(TimePoint::max(), 0ms, "none");
2126
Pawin Vongmasa36653902018-11-15 00:10:25 -08002127 sp<AMessage> inputFormat;
2128 sp<AMessage> outputFormat;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08002129 status_t err2 = OK;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002130 bool buffersBoundToCodec = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002131 {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07002132 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2133 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002134 inputFormat = config->mInputFormat;
Wonsik Kim274c8322020-02-28 10:42:21 -08002135 // start triggers format dup
2136 outputFormat = config->mOutputFormat = config->mOutputFormat->dup();
Pawin Vongmasa1f213362019-01-24 06:59:16 -08002137 if (config->mInputSurface) {
2138 err2 = config->mInputSurface->start();
Wonsik Kim673dd192021-01-29 14:58:12 -08002139 config->mInputSurfaceDataspace = config->mInputSurface->getDataspace();
Pawin Vongmasa1f213362019-01-24 06:59:16 -08002140 }
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002141 buffersBoundToCodec = config->mBuffersBoundToCodec;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002142 }
Pawin Vongmasa1f213362019-01-24 06:59:16 -08002143 if (err2 != OK) {
2144 mCallback->onError(err2, ACTION_CODE_FATAL);
2145 return;
2146 }
Arun Johnson106fe7a2023-04-26 17:49:43 +00002147
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002148 err2 = mChannel->start(inputFormat, outputFormat, buffersBoundToCodec);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002149 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 }
Arun Johnson5997bb02022-04-01 19:35:44 +00002165
Wonsik Kim34b28b42022-05-20 15:49:32 -07002166 // 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
Pawin Vongmasa36653902018-11-15 00:10:25 -08002177 mCallback->onStartCompleted();
2178
Wonsik Kim34b28b42022-05-20 15:49:32 -07002179 mChannel->requestInitialInputBuffers(std::move(clientInputBuffers));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002180}
2181
2182void CCodec::initiateShutdown(bool keepComponentAllocated) {
2183 if (keepComponentAllocated) {
2184 initiateStop();
2185 } else {
2186 initiateRelease();
2187 }
2188}
2189
2190void 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 }
Wonsik Kim936a89c2020-05-08 16:07:50 -07002205 mChannel->reset();
Sungtak Lee99144332023-01-26 11:03:14 +00002206 bool pushBlankBuffer = mConfig.lock().get()->mPushBlankBuffersOnStop;
2207 sp<AMessage> stopMessage(new AMessage(kWhatStop, this));
2208 stopMessage->setInt32("pushBlankBuffer", pushBlankBuffer);
2209 stopMessage->post();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002210}
2211
Sungtak Lee99144332023-01-26 11:03:14 +00002212void CCodec::stop(bool pushBlankBuffer) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002213 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 }
Sungtak Leec0c05962023-10-25 08:14:13 +00002230
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.
Sungtak Lee34402ae2024-05-07 19:51:28 +00002238 //
2239 // The workaround is no longer needed with fetchGraphicBlock & C2Fence changes.
Sungtak Leeb7b1cf42024-05-14 22:45:35 +00002240 // 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);
Sungtak Lee34402ae2024-05-07 19:51:28 +00002247 status_t err = C2_OK;
Sungtak Leeb7b1cf42024-05-14 22:45:35 +00002248 if (stopHalBeforeSurface && android::media::codec::provider_->stop_hal_before_surface()) {
Sungtak Lee34402ae2024-05-07 19:51:28 +00002249 err = comp->stop();
2250 mChannel->stopUseOutputSurface(pushBlankBuffer);
2251 } else {
2252 mChannel->stopUseOutputSurface(pushBlankBuffer);
2253 err = comp->stop();
2254 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002255 if (err != C2_OK) {
2256 // TODO: convert err into status_t
2257 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2258 }
2259
2260 {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07002261 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2262 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08002263 if (config->mInputSurface) {
2264 config->mInputSurface->disconnect();
2265 config->mInputSurface = nullptr;
Wonsik Kim673dd192021-01-29 14:58:12 -08002266 config->mInputSurfaceDataspace = HAL_DATASPACE_UNKNOWN;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08002267 }
2268 }
2269 {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002270 Mutexed<State>::Locked state(mState);
2271 if (state->get() == STOPPING) {
2272 state->set(ALLOCATED);
2273 }
2274 }
2275 mCallback->onStopCompleted();
2276}
2277
2278void CCodec::initiateRelease(bool sendCallback /* = true */) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -08002279 bool clearInputSurfaceIfNeeded = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002280 {
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 }
Pawin Vongmasa1f213362019-01-24 06:59:16 -08002301 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 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002307 state->set(RELEASING);
2308 }
2309
Pawin Vongmasa1f213362019-01-24 06:59:16 -08002310 if (clearInputSurfaceIfNeeded) {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07002311 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2312 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08002313 if (config->mInputSurface) {
2314 config->mInputSurface->disconnect();
2315 config->mInputSurface = nullptr;
Wonsik Kim673dd192021-01-29 14:58:12 -08002316 config->mInputSurfaceDataspace = HAL_DATASPACE_UNKNOWN;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08002317 }
2318 }
2319
Wonsik Kim936a89c2020-05-08 16:07:50 -07002320 mChannel->reset();
Sungtak Lee99144332023-01-26 11:03:14 +00002321 bool pushBlankBuffer = mConfig.lock().get()->mPushBlankBuffersOnStop;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002322 // thiz holds strong ref to this while the thread is running.
2323 sp<CCodec> thiz(this);
Sungtak Lee99144332023-01-26 11:03:14 +00002324 std::thread([thiz, sendCallback, pushBlankBuffer]
2325 { thiz->release(sendCallback, pushBlankBuffer); }).detach();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002326}
2327
Sungtak Lee99144332023-01-26 11:03:14 +00002328void CCodec::release(bool sendCallback, bool pushBlankBuffer) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002329 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 }
Sungtak Leec0c05962023-10-25 08:14:13 +00002342 // 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.
Sungtak Lee34402ae2024-05-07 19:51:28 +00002349 //
2350 // The workaround is no longer needed with fetchGraphicBlock & C2Fence changes.
Sungtak Leeb7b1cf42024-05-14 22:45:35 +00002351 // 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()) {
Sungtak Lee34402ae2024-05-07 19:51:28 +00002359 comp->release();
2360 mChannel->stopUseOutputSurface(pushBlankBuffer);
2361 } else {
2362 mChannel->stopUseOutputSurface(pushBlankBuffer);
2363 comp->release();
2364 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002365
2366 {
2367 Mutexed<State>::Locked state(mState);
2368 state->set(RELEASED);
2369 state->comp.reset();
2370 }
Wonsik Kim936a89c2020-05-08 16:07:50 -07002371 (new AMessage(kWhatRelease, this))->post();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002372 if (sendCallback) {
2373 mCallback->onReleaseCompleted();
2374 }
2375}
2376
Sungtak Lee214ce612023-11-01 10:01:13 +00002377status_t CCodec::setSurface(const sp<Surface> &surface, uint32_t generation) {
Sungtak Lee99144332023-01-26 11:03:14 +00002378 bool pushBlankBuffer = false;
Wonsik Kim75e22f42021-04-14 23:34:51 -07002379 {
2380 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2381 const std::unique_ptr<Config> &config = *configLocked;
Houxiang Dai7ab7ee62021-09-30 16:08:51 +08002382 sp<ANativeWindow> nativeWindow = static_cast<ANativeWindow *>(surface.get());
2383 status_t err = OK;
2384
Wonsik Kim75e22f42021-04-14 23:34:51 -07002385 if (config->mTunneled && config->mSidebandHandle != nullptr) {
Houxiang Dai7ab7ee62021-09-30 16:08:51 +08002386 err = native_window_set_sideband_stream(
Wonsik Kim75e22f42021-04-14 23:34:51 -07002387 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 }
Houxiang Dai7ab7ee62021-09-30 16:08:51 +08002394 } 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 }
ted.sun765db4d2020-06-23 14:03:41 +08002403 }
Sungtak Lee99144332023-01-26 11:03:14 +00002404 pushBlankBuffer = config->mPushBlankBuffersOnStop;
ted.sun765db4d2020-06-23 14:03:41 +08002405 }
Sungtak Lee214ce612023-11-01 10:01:13 +00002406 return mChannel->setSurface(surface, generation, pushBlankBuffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002407}
2408
2409void 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
2436void 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);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002462
2463 {
2464 Mutexed<State>::Locked state(mState);
Iris Changce521ee2019-07-05 16:18:54 +08002465 if (state->get() == FLUSHING) {
2466 state->set(FLUSHED);
2467 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002468 }
2469 mCallback->onFlushCompleted();
2470}
2471
2472void CCodec::signalResume() {
Wonsik Kime75a5da2020-02-14 17:29:03 -08002473 std::shared_ptr<Codec2Client::Component> comp;
2474 auto setResuming = [this, &comp] {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002475 Mutexed<State>::Locked state(mState);
2476 if (state->get() != FLUSHED) {
2477 return UNKNOWN_ERROR;
2478 }
2479 state->set(RESUMING);
Wonsik Kime75a5da2020-02-14 17:29:03 -08002480 comp = state->comp;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002481 return OK;
2482 };
2483 if (tryAndReportOnError(setResuming) != OK) {
2484 return;
2485 }
2486
Wonsik Kime75a5da2020-02-14 17:29:03 -08002487 {
2488 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2489 const std::unique_ptr<Config> &config = *configLocked;
Harish Mahendrakar8c537502021-02-23 21:20:22 -08002490 sp<AMessage> outputFormat = config->mOutputFormat;
Wonsik Kime75a5da2020-02-14 17:29:03 -08002491 config->queryConfiguration(comp);
Harish Mahendrakar8c537502021-02-23 21:20:22 -08002492 RevertOutputFormatIfNeeded(outputFormat, config->mOutputFormat);
Wonsik Kime75a5da2020-02-14 17:29:03 -08002493 }
2494
Arun Johnson106fe7a2023-04-26 17:49:43 +00002495 std::map<size_t, sp<MediaCodecBuffer>> clientInputBuffers;
Arun Johnson326166e2023-07-28 19:11:52 +00002496 status_t err = mChannel->prepareInitialInputBuffers(&clientInputBuffers, true);
Arun Johnson106fe7a2023-04-26 17:49:43 +00002497 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
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002514 (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 }());
Pawin Vongmasa36653902018-11-15 00:10:25 -08002519 {
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
Wonsik Kim34b28b42022-05-20 15:49:32 -07002530 mChannel->requestInitialInputBuffers(std::move(clientInputBuffers));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002531}
2532
Wonsik Kimaa484ac2019-02-13 16:54:02 -08002533void CCodec::signalSetParameters(const sp<AMessage> &msg) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002534 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
Wonsik Kimaa484ac2019-02-13 16:54:02 -08002547 // 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
Houxiang Dai5a97b472021-03-22 17:56:04 +08002556 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
Wonsik Kim155d5cb2019-10-09 12:49:49 -07002562 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2563 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002564
2565 /**
2566 * Handle input surface parameters
2567 */
2568 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))
Wonsik Kim8a6ed372019-12-03 16:05:51 -08002569 && (config->mDomain & Config::IS_ENCODER)
2570 && config->mInputSurface && config->mISConfig) {
Chong Zhang038e8f82019-02-06 19:05:14 -08002571 (void)params->findInt64(PARAMETER_KEY_OFFSET_TIME, &config->mISConfig->mTimeOffsetUs);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002572
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;
Chong Zhang038e8f82019-02-06 19:05:14 -08002580 if (params->findInt32(PARAMETER_KEY_SUSPEND, &value)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002581 config->mISConfig->mSuspended = value;
2582 config->mISConfig->mSuspendAtUs = -1;
Chong Zhang038e8f82019-02-06 19:05:14 -08002583 (void)params->findInt64(PARAMETER_KEY_SUSPEND_TIME, &config->mISConfig->mSuspendAtUs);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002584 }
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
Harish Mahendrakar38a99c22024-02-26 20:28:05 +05302593 /**
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
Pawin Vongmasa36653902018-11-15 00:10:25 -08002627 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 mChannel->setParameters(configUpdate);
2637 } else {
Wonsik Kim3b4349a2020-11-10 11:54:15 -08002638 sp<AMessage> outputFormat = config->mOutputFormat;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002639 (void)config->setParameters(comp, configUpdate, C2_MAY_BLOCK);
Wonsik Kim3b4349a2020-11-10 11:54:15 -08002640 RevertOutputFormatIfNeeded(outputFormat, config->mOutputFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002641 }
2642}
2643
2644void CCodec::signalEndOfInputStream() {
2645 mCallback->onSignaledInputEOS(mChannel->signalEndOfInputStream());
2646}
2647
2648void CCodec::signalRequestIDRFrame() {
2649 std::shared_ptr<Codec2Client::Component> comp;
2650 {
2651 Mutexed<State>::Locked state(mState);
2652 if (state->get() == RELEASED) {
2653 ALOGD("no IDR request sent since component is released");
2654 return;
2655 }
2656 comp = state->comp;
2657 }
2658 ALOGV("request IDR");
Wonsik Kim155d5cb2019-10-09 12:49:49 -07002659 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2660 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002661 std::vector<std::unique_ptr<C2Param>> params;
2662 params.push_back(
2663 std::make_unique<C2StreamRequestSyncFrameTuning::output>(0u, true));
2664 config->setParameters(comp, params, C2_MAY_BLOCK);
2665}
2666
Wonsik Kim874ad382021-03-12 09:59:36 -08002667status_t CCodec::querySupportedParameters(std::vector<std::string> *names) {
2668 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2669 const std::unique_ptr<Config> &config = *configLocked;
2670 return config->querySupportedParameters(names);
2671}
2672
2673status_t CCodec::describeParameter(
2674 const std::string &name, CodecParameterDescriptor *desc) {
2675 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2676 const std::unique_ptr<Config> &config = *configLocked;
2677 return config->describe(name, desc);
2678}
2679
2680status_t CCodec::subscribeToParameters(const std::vector<std::string> &names) {
2681 std::shared_ptr<Codec2Client::Component> comp = mState.lock()->comp;
2682 if (!comp) {
2683 return INVALID_OPERATION;
2684 }
2685 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2686 const std::unique_ptr<Config> &config = *configLocked;
2687 return config->subscribeToVendorConfigUpdate(comp, names);
2688}
2689
2690status_t CCodec::unsubscribeFromParameters(const std::vector<std::string> &names) {
2691 std::shared_ptr<Codec2Client::Component> comp = mState.lock()->comp;
2692 if (!comp) {
2693 return INVALID_OPERATION;
2694 }
2695 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2696 const std::unique_ptr<Config> &config = *configLocked;
2697 return config->unsubscribeFromVendorConfigUpdate(comp, names);
2698}
2699
Wonsik Kimab34ed62019-01-31 15:28:46 -08002700void CCodec::onWorkDone(std::list<std::unique_ptr<C2Work>> &workItems) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002701 if (!workItems.empty()) {
Wonsik Kimab34ed62019-01-31 15:28:46 -08002702 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
Houxiang Dai21e571f2022-05-09 21:35:39 +08002703 bool shouldPost = queue->empty();
Wonsik Kimab34ed62019-01-31 15:28:46 -08002704 queue->splice(queue->end(), workItems);
Houxiang Dai21e571f2022-05-09 21:35:39 +08002705 if (shouldPost) {
2706 (new AMessage(kWhatWorkDone, this))->post();
2707 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002708 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002709}
2710
Wonsik Kimab34ed62019-01-31 15:28:46 -08002711void CCodec::onInputBufferDone(uint64_t frameIndex, size_t arrayIndex) {
2712 mChannel->onInputBufferDone(frameIndex, arrayIndex);
Wonsik Kim4f3314d2019-03-26 17:00:34 -07002713 if (arrayIndex == 0) {
2714 // We always put no more than one buffer per work, if we use an input surface.
Wonsik Kim155d5cb2019-10-09 12:49:49 -07002715 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2716 const std::unique_ptr<Config> &config = *configLocked;
Wonsik Kim4f3314d2019-03-26 17:00:34 -07002717 if (config->mInputSurface) {
2718 config->mInputSurface->onInputBufferDone(frameIndex);
2719 }
2720 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002721}
2722
2723void CCodec::onMessageReceived(const sp<AMessage> &msg) {
2724 TimePoint now = std::chrono::steady_clock::now();
2725 CCodecWatchdog::getInstance()->watch(this);
2726 switch (msg->what()) {
2727 case kWhatAllocate: {
2728 // C2ComponentStore::createComponent() should return within 100ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002729 setDeadline(now, 1500ms, "allocate");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002730 sp<RefBase> obj;
2731 CHECK(msg->findObject("codecInfo", &obj));
2732 allocate((MediaCodecInfo *)obj.get());
2733 break;
2734 }
2735 case kWhatConfigure: {
2736 // C2Component::commit_sm() should return within 5ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002737 setDeadline(now, 1500ms, "configure");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002738 sp<AMessage> format;
2739 CHECK(msg->findMessage("format", &format));
2740 configure(format);
2741 break;
2742 }
2743 case kWhatStart: {
2744 // C2Component::start() should return within 500ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002745 setDeadline(now, 1500ms, "start");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002746 start();
2747 break;
2748 }
2749 case kWhatStop: {
2750 // C2Component::stop() should return within 500ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002751 setDeadline(now, 1500ms, "stop");
Sungtak Lee99144332023-01-26 11:03:14 +00002752 int32_t pushBlankBuffer;
2753 if (!msg->findInt32("pushBlankBuffer", &pushBlankBuffer)) {
2754 pushBlankBuffer = 0;
2755 }
2756 stop(static_cast<bool>(pushBlankBuffer));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002757 break;
2758 }
2759 case kWhatFlush: {
2760 // C2Component::flush_sm() should return within 5ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002761 setDeadline(now, 1500ms, "flush");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002762 flush();
2763 break;
2764 }
Wonsik Kim936a89c2020-05-08 16:07:50 -07002765 case kWhatRelease: {
2766 mChannel->release();
2767 mClient.reset();
2768 mClientListener.reset();
2769 break;
2770 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002771 case kWhatCreateInputSurface: {
2772 // Surface operations may be briefly blocking.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002773 setDeadline(now, 1500ms, "createInputSurface");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002774 createInputSurface();
2775 break;
2776 }
2777 case kWhatSetInputSurface: {
2778 // Surface operations may be briefly blocking.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002779 setDeadline(now, 1500ms, "setInputSurface");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002780 sp<RefBase> obj;
2781 CHECK(msg->findObject("surface", &obj));
2782 sp<PersistentSurface> surface(static_cast<PersistentSurface *>(obj.get()));
2783 setInputSurface(surface);
2784 break;
2785 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002786 case kWhatWorkDone: {
2787 std::unique_ptr<C2Work> work;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002788 bool shouldPost = false;
2789 {
2790 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
2791 if (queue->empty()) {
2792 break;
2793 }
2794 work.swap(queue->front());
2795 queue->pop_front();
2796 shouldPost = !queue->empty();
2797 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002798 if (shouldPost) {
2799 (new AMessage(kWhatWorkDone, this))->post();
2800 }
2801
Pawin Vongmasa36653902018-11-15 00:10:25 -08002802 // handle configuration changes in work done
Wonsik Kim1f5063d2021-05-03 15:41:17 -07002803 std::shared_ptr<const C2StreamInitDataInfo::output> initData;
Wonsik Kim75e22f42021-04-14 23:34:51 -07002804 sp<AMessage> outputFormat = nullptr;
2805 {
2806 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2807 const std::unique_ptr<Config> &config = *configLocked;
2808 Config::Watcher<C2StreamInitDataInfo::output> initDataWatcher =
2809 config->watch<C2StreamInitDataInfo::output>();
2810 if (!work->worklets.empty()
2811 && (work->worklets.front()->output.flags
2812 & C2FrameData::FLAG_DISCARD_FRAME) == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002813
Wonsik Kim75e22f42021-04-14 23:34:51 -07002814 // copy buffer info to config
2815 std::vector<std::unique_ptr<C2Param>> updates;
2816 for (const std::unique_ptr<C2Param> &param
2817 : work->worklets.front()->output.configUpdate) {
2818 updates.push_back(C2Param::Copy(*param));
2819 }
2820 unsigned stream = 0;
2821 std::vector<std::shared_ptr<C2Buffer>> &outputBuffers =
2822 work->worklets.front()->output.buffers;
2823 for (const std::shared_ptr<C2Buffer> &buf : outputBuffers) {
2824 for (const std::shared_ptr<const C2Info> &info : buf->info()) {
2825 // move all info into output-stream #0 domain
2826 updates.emplace_back(
2827 C2Param::CopyAsStream(*info, true /* output */, stream));
2828 }
2829
2830 const std::vector<C2ConstGraphicBlock> blocks = buf->data().graphicBlocks();
2831 // for now only do the first block
2832 if (!blocks.empty()) {
2833 // ALOGV("got output buffer with crop %u,%u+%u,%u and size %u,%u",
2834 // block.crop().left, block.crop().top,
2835 // block.crop().width, block.crop().height,
2836 // block.width(), block.height());
2837 const C2ConstGraphicBlock &block = blocks[0];
2838 updates.emplace_back(new C2StreamCropRectInfo::output(
2839 stream, block.crop()));
Wonsik Kim75e22f42021-04-14 23:34:51 -07002840 }
2841 ++stream;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002842 }
George Burgess IVc813a592020-02-22 22:54:44 -08002843
Wonsik Kim75e22f42021-04-14 23:34:51 -07002844 sp<AMessage> oldFormat = config->mOutputFormat;
2845 config->updateConfiguration(updates, config->mOutputDomain);
2846 RevertOutputFormatIfNeeded(oldFormat, config->mOutputFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002847
Wonsik Kim75e22f42021-04-14 23:34:51 -07002848 // copy standard infos to graphic buffers if not already present (otherwise, we
2849 // may overwrite the actual intermediate value with a final value)
2850 stream = 0;
2851 const static C2Param::Index stdGfxInfos[] = {
2852 C2StreamRotationInfo::output::PARAM_TYPE,
2853 C2StreamColorAspectsInfo::output::PARAM_TYPE,
2854 C2StreamDataSpaceInfo::output::PARAM_TYPE,
2855 C2StreamHdrStaticInfo::output::PARAM_TYPE,
Taehwan Kim2d222b82022-05-12 14:19:26 +09002856 C2StreamHdr10PlusInfo::output::PARAM_TYPE, // will be deprecated
2857 C2StreamHdrDynamicMetadataInfo::output::PARAM_TYPE,
Wonsik Kim75e22f42021-04-14 23:34:51 -07002858 C2StreamPixelAspectRatioInfo::output::PARAM_TYPE,
2859 C2StreamSurfaceScalingInfo::output::PARAM_TYPE
2860 };
2861 for (const std::shared_ptr<C2Buffer> &buf : outputBuffers) {
2862 if (buf->data().graphicBlocks().size()) {
2863 for (C2Param::Index ix : stdGfxInfos) {
2864 if (!buf->hasInfo(ix)) {
2865 const C2Param *param =
2866 config->getConfigParameterValue(ix.withStream(stream));
2867 if (param) {
2868 std::shared_ptr<C2Param> info(C2Param::Copy(*param));
2869 buf->setInfo(std::static_pointer_cast<C2Info>(info));
2870 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002871 }
2872 }
2873 }
Wonsik Kim75e22f42021-04-14 23:34:51 -07002874 ++stream;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002875 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002876 }
Wonsik Kim75e22f42021-04-14 23:34:51 -07002877 if (config->mInputSurface) {
Brijesh Patelab463672020-11-25 15:38:28 +05302878 if (work->worklets.empty()
2879 || !work->worklets.back()
2880 || (work->worklets.back()->output.flags
2881 & C2FrameData::FLAG_INCOMPLETE) == 0) {
2882 config->mInputSurface->onInputBufferDone(work->input.ordinal.frameIndex);
2883 }
Wonsik Kim75e22f42021-04-14 23:34:51 -07002884 }
2885 if (initDataWatcher.hasChanged()) {
Wonsik Kim1f5063d2021-05-03 15:41:17 -07002886 initData = initDataWatcher.update();
2887 AmendOutputFormatWithCodecSpecificData(
2888 initData->m.value, initData->flexCount(), config->mCodingMediaType,
2889 config->mOutputFormat);
Wonsik Kim75e22f42021-04-14 23:34:51 -07002890 }
2891 outputFormat = config->mOutputFormat;
Wonsik Kim9c387412021-04-19 21:03:53 +00002892 }
2893 mChannel->onWorkDone(
Wonsik Kim1f5063d2021-05-03 15:41:17 -07002894 std::move(work), outputFormat, initData ? initData.get() : nullptr);
Songyue Han1e6769b2023-08-30 18:09:27 +00002895 // log metrics to MediaCodec
2896 if (mMetrics->countEntries() == 0) {
2897 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2898 const std::unique_ptr<Config> &config = *configLocked;
2899 uint32_t pf = PIXEL_FORMAT_UNKNOWN;
2900 if (!config->mInputSurface) {
2901 pf = mChannel->getBuffersPixelFormat(config->mDomain & Config::IS_ENCODER);
Songyue Hanad01f6a2023-08-17 05:45:35 +00002902 } else {
2903 pf = config->mInputSurface->getPixelFormat();
Songyue Han1e6769b2023-08-30 18:09:27 +00002904 }
2905 if (pf != PIXEL_FORMAT_UNKNOWN) {
2906 mMetrics->setInt64(kCodecPixelFormat, pf);
2907 mCallback->onMetricsUpdated(mMetrics);
2908 }
2909 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002910 break;
2911 }
2912 case kWhatWatch: {
2913 // watch message already posted; no-op.
2914 break;
2915 }
2916 default: {
2917 ALOGE("unrecognized message");
2918 break;
2919 }
2920 }
2921 setDeadline(TimePoint::max(), 0ms, "none");
2922}
2923
2924void CCodec::setDeadline(
2925 const TimePoint &now,
2926 const std::chrono::milliseconds &timeout,
2927 const char *name) {
2928 int32_t mult = std::max(1, property_get_int32("debug.stagefright.ccodec_timeout_mult", 1));
2929 Mutexed<NamedTimePoint>::Locked deadline(mDeadline);
2930 deadline->set(now + (timeout * mult), name);
2931}
2932
ted.sun765db4d2020-06-23 14:03:41 +08002933status_t CCodec::configureTunneledVideoPlayback(
2934 std::shared_ptr<Codec2Client::Component> comp,
2935 sp<NativeHandle> *sidebandHandle,
2936 const sp<AMessage> &msg) {
2937 std::vector<std::unique_ptr<C2SettingResult>> failures;
2938
2939 std::unique_ptr<C2PortTunneledModeTuning::output> tunneledPlayback =
2940 C2PortTunneledModeTuning::output::AllocUnique(
2941 1,
2942 C2PortTunneledModeTuning::Struct::SIDEBAND,
2943 C2PortTunneledModeTuning::Struct::REALTIME,
2944 0);
2945 // TODO: use KEY_AUDIO_HW_SYNC, KEY_HARDWARE_AV_SYNC_ID when they are in MediaCodecConstants.h
2946 if (msg->findInt32("audio-hw-sync", &tunneledPlayback->m.syncId[0])) {
2947 tunneledPlayback->m.syncType = C2PortTunneledModeTuning::Struct::sync_type_t::AUDIO_HW_SYNC;
2948 } else if (msg->findInt32("hw-av-sync-id", &tunneledPlayback->m.syncId[0])) {
2949 tunneledPlayback->m.syncType = C2PortTunneledModeTuning::Struct::sync_type_t::HW_AV_SYNC;
2950 } else {
2951 tunneledPlayback->m.syncType = C2PortTunneledModeTuning::Struct::sync_type_t::REALTIME;
2952 tunneledPlayback->setFlexCount(0);
2953 }
2954 c2_status_t c2err = comp->config({ tunneledPlayback.get() }, C2_MAY_BLOCK, &failures);
2955 if (c2err != C2_OK) {
2956 return UNKNOWN_ERROR;
2957 }
2958
Houxiang Dai5a97b472021-03-22 17:56:04 +08002959 if (sidebandHandle == nullptr) {
2960 return OK;
2961 }
2962
ted.sun765db4d2020-06-23 14:03:41 +08002963 std::vector<std::unique_ptr<C2Param>> params;
2964 c2err = comp->query({}, {C2PortTunnelHandleTuning::output::PARAM_TYPE}, C2_DONT_BLOCK, &params);
2965 if (c2err == C2_OK && params.size() == 1u) {
2966 C2PortTunnelHandleTuning::output *videoTunnelSideband =
2967 C2PortTunnelHandleTuning::output::From(params[0].get());
2968 // Currently, Codec2 only supports non-fd case for sideband native_handle.
2969 native_handle_t *handle = native_handle_create(0, videoTunnelSideband->flexCount());
2970 *sidebandHandle = NativeHandle::create(handle, true /* ownsHandle */);
2971 if (handle != nullptr && videoTunnelSideband->flexCount()) {
2972 memcpy(handle->data, videoTunnelSideband->m.values,
2973 sizeof(int32_t) * videoTunnelSideband->flexCount());
2974 return OK;
2975 } else {
2976 return NO_MEMORY;
2977 }
2978 }
2979 return UNKNOWN_ERROR;
2980}
2981
Pawin Vongmasa36653902018-11-15 00:10:25 -08002982void CCodec::initiateReleaseIfStuck() {
Shrikara B3b87a532022-08-26 14:18:14 +05302983 std::string name;
2984 bool pendingDeadline = false;
2985 {
2986 Mutexed<NamedTimePoint>::Locked deadline(mDeadline);
2987 if (deadline->get() < std::chrono::steady_clock::now()) {
2988 name = deadline->getName();
2989 }
2990 if (deadline->get() != TimePoint::max()) {
2991 pendingDeadline = true;
2992 }
2993 }
Wonsik Kimab34ed62019-01-31 15:28:46 -08002994 if (name.empty()) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002995 // We're not stuck.
2996 if (pendingDeadline) {
2997 // If we are not stuck yet but still has deadline coming up,
2998 // post watch message to check back later.
2999 (new AMessage(kWhatWatch, this))->post();
3000 }
3001 return;
3002 }
3003
Chih-Yu Huang82e5ab32021-02-17 16:27:08 +09003004 C2String compName;
3005 {
3006 Mutexed<State>::Locked state(mState);
Wonsik Kim12380072021-05-11 09:59:20 -07003007 if (!state->comp) {
3008 ALOGD("previous call to %s exceeded timeout "
3009 "and the component is already released", name.c_str());
3010 return;
3011 }
Chih-Yu Huang82e5ab32021-02-17 16:27:08 +09003012 compName = state->comp->getName();
3013 }
3014 ALOGW("[%s] previous call to %s exceeded timeout", compName.c_str(), name.c_str());
3015
Pawin Vongmasa36653902018-11-15 00:10:25 -08003016 initiateRelease(false);
3017 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
3018}
3019
Wonsik Kim155d5cb2019-10-09 12:49:49 -07003020// static
3021PersistentSurface *CCodec::CreateInputSurface() {
Pawin Vongmasa18588322019-05-18 01:52:13 -07003022 using namespace android;
Wonsik Kim9917d4a2019-10-24 12:56:38 -07003023 using ::android::hardware::media::omx::V1_0::implementation::TWGraphicBufferSource;
Pawin Vongmasa36653902018-11-15 00:10:25 -08003024 // Attempt to create a Codec2's input surface.
Pawin Vongmasa18588322019-05-18 01:52:13 -07003025 std::shared_ptr<Codec2Client::InputSurface> inputSurface =
3026 Codec2Client::CreateInputSurface();
Lajos Molnar47118272019-01-31 16:28:04 -08003027 if (!inputSurface) {
Pawin Vongmasa18588322019-05-18 01:52:13 -07003028 if (property_get_int32("debug.stagefright.c2inputsurface", 0) == -1) {
Sungtak Lee92bca442024-03-12 09:10:31 +00003029 if (Codec2Client::IsAidlSelected()) {
3030 sp<IGraphicBufferProducer> gbp;
3031 sp<AidlGraphicBufferSource> gbs = new AidlGraphicBufferSource();
3032 status_t err = gbs->initCheck();
3033 if (err != OK) {
3034 ALOGE("Failed to create persistent input surface: error %d", err);
3035 return nullptr;
3036 }
3037 ALOGD("aidl based PersistentSurface created");
3038 std::shared_ptr<WAidlGraphicBufferSource> wrapper =
3039 ::ndk::SharedRefBase::make<WAidlGraphicBufferSource>(gbs);
3040
3041 return new PersistentSurface(
3042 gbs->getIGraphicBufferProducer(), wrapper->asBinder());
3043 } else {
3044 sp<IGraphicBufferProducer> gbp;
3045 sp<OmxGraphicBufferSource> gbs = new OmxGraphicBufferSource();
3046 status_t err = gbs->initCheck();
3047 if (err != OK) {
3048 ALOGE("Failed to create persistent input surface: error %d", err);
3049 return nullptr;
3050 }
3051 ALOGD("hidl based PersistentSurface created");
3052 return new PersistentSurface(
3053 gbs->getIGraphicBufferProducer(), new TWGraphicBufferSource(gbs));
Pawin Vongmasa18588322019-05-18 01:52:13 -07003054 }
Pawin Vongmasa18588322019-05-18 01:52:13 -07003055 } else {
3056 return nullptr;
3057 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08003058 }
Pawin Vongmasa18588322019-05-18 01:52:13 -07003059 return new PersistentSurface(
Lajos Molnar47118272019-01-31 16:28:04 -08003060 inputSurface->getGraphicBufferProducer(),
Pawin Vongmasa18588322019-05-18 01:52:13 -07003061 static_cast<sp<android::hidl::base::V1_0::IBase>>(
Lajos Molnar47118272019-01-31 16:28:04 -08003062 inputSurface->getHalInterface()));
Pawin Vongmasa36653902018-11-15 00:10:25 -08003063}
3064
Wonsik Kimffb889a2020-05-28 11:32:25 -07003065class IntfCache {
3066public:
3067 IntfCache() = default;
3068
3069 status_t init(const std::string &name) {
3070 std::shared_ptr<Codec2Client::Interface> intf{
3071 Codec2Client::CreateInterfaceByName(name.c_str())};
3072 if (!intf) {
3073 ALOGW("IntfCache [%s]: Unrecognized interface name", name.c_str());
3074 mInitStatus = NO_INIT;
3075 return NO_INIT;
3076 }
3077 const static C2StreamUsageTuning::input sUsage{0u /* stream id */};
3078 mFields.push_back(C2FieldSupportedValuesQuery::Possible(
3079 C2ParamField{&sUsage, &sUsage.value}));
3080 c2_status_t err = intf->querySupportedValues(mFields, C2_MAY_BLOCK);
3081 if (err != C2_OK) {
3082 ALOGW("IntfCache [%s]: failed to query usage supported value (err=%d)",
3083 name.c_str(), err);
3084 mFields[0].status = err;
3085 }
3086 std::vector<std::unique_ptr<C2Param>> params;
3087 err = intf->query(
3088 {&mApiFeatures},
Taehwan Kim900b49c2021-12-13 11:16:22 +09003089 {
3090 C2StreamBufferTypeSetting::input::PARAM_TYPE,
3091 C2PortAllocatorsTuning::input::PARAM_TYPE
3092 },
Wonsik Kimffb889a2020-05-28 11:32:25 -07003093 C2_MAY_BLOCK,
3094 &params);
3095 if (err != C2_OK && err != C2_BAD_INDEX) {
3096 ALOGW("IntfCache [%s]: failed to query api features (err=%d)",
3097 name.c_str(), err);
3098 }
3099 while (!params.empty()) {
3100 C2Param *param = params.back().release();
3101 params.pop_back();
3102 if (!param) {
3103 continue;
3104 }
Taehwan Kim900b49c2021-12-13 11:16:22 +09003105 if (param->type() == C2StreamBufferTypeSetting::input::PARAM_TYPE) {
3106 mInputStreamFormat.reset(
3107 C2StreamBufferTypeSetting::input::From(param));
3108 } else if (param->type() == C2PortAllocatorsTuning::input::PARAM_TYPE) {
Wonsik Kimffb889a2020-05-28 11:32:25 -07003109 mInputAllocators.reset(
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07003110 C2PortAllocatorsTuning::input::From(param));
Wonsik Kimffb889a2020-05-28 11:32:25 -07003111 }
3112 }
3113 mInitStatus = OK;
3114 return OK;
Wonsik Kimfcf46c32020-04-22 13:50:45 -07003115 }
Wonsik Kimffb889a2020-05-28 11:32:25 -07003116
3117 status_t initCheck() const { return mInitStatus; }
3118
3119 const C2FieldSupportedValuesQuery &getUsageSupportedValues() const {
3120 CHECK_EQ(1u, mFields.size());
3121 return mFields[0];
3122 }
3123
3124 const C2ApiFeaturesSetting &getApiFeatures() const {
3125 return mApiFeatures;
3126 }
3127
Taehwan Kim900b49c2021-12-13 11:16:22 +09003128 const C2StreamBufferTypeSetting::input &getInputStreamFormat() const {
3129 static std::unique_ptr<C2StreamBufferTypeSetting::input> sInvalidated = []{
3130 std::unique_ptr<C2StreamBufferTypeSetting::input> param;
3131 param.reset(new C2StreamBufferTypeSetting::input(0u, C2BufferData::INVALID));
3132 param->invalidate();
3133 return param;
3134 }();
3135 return mInputStreamFormat ? *mInputStreamFormat : *sInvalidated;
3136 }
3137
Wonsik Kimffb889a2020-05-28 11:32:25 -07003138 const C2PortAllocatorsTuning::input &getInputAllocators() const {
3139 static std::unique_ptr<C2PortAllocatorsTuning::input> sInvalidated = []{
3140 std::unique_ptr<C2PortAllocatorsTuning::input> param =
3141 C2PortAllocatorsTuning::input::AllocUnique(0);
3142 param->invalidate();
3143 return param;
3144 }();
3145 return mInputAllocators ? *mInputAllocators : *sInvalidated;
3146 }
3147
3148private:
3149 status_t mInitStatus{NO_INIT};
3150
3151 std::vector<C2FieldSupportedValuesQuery> mFields;
3152 C2ApiFeaturesSetting mApiFeatures;
Taehwan Kim900b49c2021-12-13 11:16:22 +09003153 std::unique_ptr<C2StreamBufferTypeSetting::input> mInputStreamFormat;
Wonsik Kimffb889a2020-05-28 11:32:25 -07003154 std::unique_ptr<C2PortAllocatorsTuning::input> mInputAllocators;
3155};
3156
3157static const IntfCache &GetIntfCache(const std::string &name) {
3158 static IntfCache sNullIntfCache;
3159 static std::mutex sMutex;
3160 static std::map<std::string, IntfCache> sCache;
3161 std::unique_lock<std::mutex> lock{sMutex};
3162 auto it = sCache.find(name);
3163 if (it == sCache.end()) {
3164 lock.unlock();
3165 IntfCache intfCache;
3166 status_t err = intfCache.init(name);
3167 if (err != OK) {
3168 return sNullIntfCache;
3169 }
3170 lock.lock();
3171 it = sCache.insert({name, std::move(intfCache)}).first;
3172 }
3173 return it->second;
Wonsik Kimfcf46c32020-04-22 13:50:45 -07003174}
3175
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003176static status_t GetCommonAllocatorIds(
3177 const std::vector<std::string> &names,
3178 C2Allocator::type_t type,
3179 std::set<C2Allocator::id_t> *ids) {
3180 int poolMask = GetCodec2PoolMask();
3181 C2PlatformAllocatorStore::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask);
3182 C2Allocator::id_t defaultAllocatorId =
3183 (type == C2Allocator::LINEAR) ? preferredLinearId : C2PlatformAllocatorStore::GRALLOC;
3184
3185 ids->clear();
3186 if (names.empty()) {
3187 return OK;
3188 }
Wonsik Kimfcf46c32020-04-22 13:50:45 -07003189 bool firstIteration = true;
3190 for (const std::string &name : names) {
Wonsik Kimffb889a2020-05-28 11:32:25 -07003191 const IntfCache &intfCache = GetIntfCache(name);
3192 if (intfCache.initCheck() != OK) {
Wonsik Kimfcf46c32020-04-22 13:50:45 -07003193 continue;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003194 }
Taehwan Kim900b49c2021-12-13 11:16:22 +09003195 const C2StreamBufferTypeSetting::input &streamFormat = intfCache.getInputStreamFormat();
3196 if (streamFormat) {
3197 C2Allocator::type_t allocatorType = C2Allocator::LINEAR;
3198 if (streamFormat.value == C2BufferData::GRAPHIC
3199 || streamFormat.value == C2BufferData::GRAPHIC_CHUNKS) {
3200 allocatorType = C2Allocator::GRAPHIC;
3201 }
3202
3203 if (type != allocatorType) {
3204 // requested type is not supported at input allocators
3205 ids->clear();
3206 ids->insert(defaultAllocatorId);
3207 ALOGV("name(%s) does not support a type(0x%x) as input allocator."
3208 " uses default allocator id(%d)", name.c_str(), type, defaultAllocatorId);
3209 break;
3210 }
3211 }
3212
Wonsik Kimffb889a2020-05-28 11:32:25 -07003213 const C2PortAllocatorsTuning::input &allocators = intfCache.getInputAllocators();
Wonsik Kimfcf46c32020-04-22 13:50:45 -07003214 if (firstIteration) {
3215 firstIteration = false;
Wonsik Kimffb889a2020-05-28 11:32:25 -07003216 if (allocators && allocators.flexCount() > 0) {
3217 ids->insert(allocators.m.values,
3218 allocators.m.values + allocators.flexCount());
Wonsik Kimfcf46c32020-04-22 13:50:45 -07003219 }
3220 if (ids->empty()) {
3221 // The component does not advertise allocators. Use default.
3222 ids->insert(defaultAllocatorId);
3223 }
3224 continue;
3225 }
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003226 bool filtered = false;
Wonsik Kimffb889a2020-05-28 11:32:25 -07003227 if (allocators && allocators.flexCount() > 0) {
3228 filtered = true;
3229 for (auto it = ids->begin(); it != ids->end(); ) {
3230 bool found = false;
3231 for (size_t j = 0; j < allocators.flexCount(); ++j) {
3232 if (allocators.m.values[j] == *it) {
3233 found = true;
3234 break;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003235 }
Wonsik Kimffb889a2020-05-28 11:32:25 -07003236 }
3237 if (found) {
3238 ++it;
3239 } else {
3240 it = ids->erase(it);
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003241 }
3242 }
3243 }
3244 if (!filtered) {
3245 // The component does not advertise supported allocators. Use default.
3246 bool containsDefault = (ids->count(defaultAllocatorId) > 0u);
3247 if (ids->size() != (containsDefault ? 1 : 0)) {
3248 ids->clear();
3249 if (containsDefault) {
3250 ids->insert(defaultAllocatorId);
3251 }
3252 }
3253 }
3254 }
3255 // Finally, filter with pool masks
3256 for (auto it = ids->begin(); it != ids->end(); ) {
3257 if ((poolMask >> *it) & 1) {
3258 ++it;
3259 } else {
3260 it = ids->erase(it);
3261 }
3262 }
3263 return OK;
3264}
3265
3266static status_t CalculateMinMaxUsage(
3267 const std::vector<std::string> &names, uint64_t *minUsage, uint64_t *maxUsage) {
3268 static C2StreamUsageTuning::input sUsage{0u /* stream id */};
3269 *minUsage = 0;
3270 *maxUsage = ~0ull;
3271 for (const std::string &name : names) {
Wonsik Kimffb889a2020-05-28 11:32:25 -07003272 const IntfCache &intfCache = GetIntfCache(name);
3273 if (intfCache.initCheck() != OK) {
Wonsik Kimfcf46c32020-04-22 13:50:45 -07003274 continue;
3275 }
Wonsik Kimffb889a2020-05-28 11:32:25 -07003276 const C2FieldSupportedValuesQuery &usageSupportedValues =
3277 intfCache.getUsageSupportedValues();
3278 if (usageSupportedValues.status != C2_OK) {
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003279 continue;
3280 }
Wonsik Kimffb889a2020-05-28 11:32:25 -07003281 const C2FieldSupportedValues &supported = usageSupportedValues.values;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003282 if (supported.type != C2FieldSupportedValues::FLAGS) {
3283 continue;
3284 }
3285 if (supported.values.empty()) {
3286 *maxUsage = 0;
3287 continue;
3288 }
Houxiang Daibfb8a722021-04-13 17:34:40 +08003289 if (supported.values.size() > 1) {
3290 *minUsage |= supported.values[1].u64;
3291 } else {
3292 *minUsage |= supported.values[0].u64;
3293 }
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003294 int64_t currentMaxUsage = 0;
3295 for (const C2Value::Primitive &flags : supported.values) {
3296 currentMaxUsage |= flags.u64;
3297 }
3298 *maxUsage &= currentMaxUsage;
3299 }
3300 return OK;
3301}
3302
3303// static
3304status_t CCodec::CanFetchLinearBlock(
3305 const std::vector<std::string> &names, const C2MemoryUsage &usage, bool *isCompatible) {
Wonsik Kimffb889a2020-05-28 11:32:25 -07003306 for (const std::string &name : names) {
3307 const IntfCache &intfCache = GetIntfCache(name);
3308 if (intfCache.initCheck() != OK) {
3309 continue;
3310 }
3311 const C2ApiFeaturesSetting &features = intfCache.getApiFeatures();
3312 if (features && !(features.value & API_SAME_INPUT_BUFFER)) {
3313 *isCompatible = false;
3314 return OK;
3315 }
3316 }
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003317 std::set<C2Allocator::id_t> allocators;
3318 GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators);
3319 if (allocators.empty()) {
3320 *isCompatible = false;
3321 return OK;
3322 }
Chih-Yu Huang990b5622020-10-13 14:53:37 +09003323
3324 uint64_t minUsage = 0;
3325 uint64_t maxUsage = ~0ull;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003326 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
Chih-Yu Huang990b5622020-10-13 14:53:37 +09003327 minUsage |= usage.expected;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003328 *isCompatible = ((maxUsage & minUsage) == minUsage);
3329 return OK;
3330}
3331
3332static std::shared_ptr<C2BlockPool> GetPool(C2Allocator::id_t allocId) {
3333 static std::mutex sMutex{};
3334 static std::map<C2Allocator::id_t, std::shared_ptr<C2BlockPool>> sPools;
3335 std::unique_lock<std::mutex> lock{sMutex};
3336 std::shared_ptr<C2BlockPool> pool;
3337 auto it = sPools.find(allocId);
3338 if (it == sPools.end()) {
3339 c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool);
3340 if (err == OK) {
3341 sPools.emplace(allocId, pool);
3342 } else {
3343 pool.reset();
3344 }
3345 } else {
3346 pool = it->second;
3347 }
3348 return pool;
3349}
3350
3351// static
3352std::shared_ptr<C2LinearBlock> CCodec::FetchLinearBlock(
3353 size_t capacity, const C2MemoryUsage &usage, const std::vector<std::string> &names) {
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003354 std::set<C2Allocator::id_t> allocators;
3355 GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators);
3356 if (allocators.empty()) {
3357 allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR);
3358 }
Chih-Yu Huang990b5622020-10-13 14:53:37 +09003359
3360 uint64_t minUsage = 0;
3361 uint64_t maxUsage = ~0ull;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003362 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
Chih-Yu Huang990b5622020-10-13 14:53:37 +09003363 minUsage |= usage.expected;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08003364 if ((maxUsage & minUsage) != minUsage) {
3365 allocators.clear();
3366 allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR);
3367 }
3368 std::shared_ptr<C2LinearBlock> block;
3369 for (C2Allocator::id_t allocId : allocators) {
3370 std::shared_ptr<C2BlockPool> pool = GetPool(allocId);
3371 if (!pool) {
3372 continue;
3373 }
3374 c2_status_t err = pool->fetchLinearBlock(capacity, C2MemoryUsage{minUsage}, &block);
3375 if (err != C2_OK || !block) {
3376 block.reset();
3377 continue;
3378 }
3379 break;
3380 }
3381 return block;
3382}
3383
3384// static
3385status_t CCodec::CanFetchGraphicBlock(
3386 const std::vector<std::string> &names, bool *isCompatible) {
3387 uint64_t minUsage = 0;
3388 uint64_t maxUsage = ~0ull;
3389 std::set<C2Allocator::id_t> allocators;
3390 GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators);
3391 if (allocators.empty()) {
3392 *isCompatible = false;
3393 return OK;
3394 }
3395 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
3396 *isCompatible = ((maxUsage & minUsage) == minUsage);
3397 return OK;
3398}
3399
3400// static
3401std::shared_ptr<C2GraphicBlock> CCodec::FetchGraphicBlock(
3402 int32_t width,
3403 int32_t height,
3404 int32_t format,
3405 uint64_t usage,
3406 const std::vector<std::string> &names) {
3407 uint32_t halPixelFormat = HAL_PIXEL_FORMAT_YCBCR_420_888;
3408 if (!C2Mapper::mapPixelFormatFrameworkToCodec(format, &halPixelFormat)) {
3409 ALOGD("Unrecognized pixel format: %d", format);
3410 return nullptr;
3411 }
3412 uint64_t minUsage = 0;
3413 uint64_t maxUsage = ~0ull;
3414 std::set<C2Allocator::id_t> allocators;
3415 GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators);
3416 if (allocators.empty()) {
3417 allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC);
3418 }
3419 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
3420 minUsage |= usage;
3421 if ((maxUsage & minUsage) != minUsage) {
3422 allocators.clear();
3423 allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC);
3424 }
3425 std::shared_ptr<C2GraphicBlock> block;
3426 for (C2Allocator::id_t allocId : allocators) {
3427 std::shared_ptr<C2BlockPool> pool;
3428 c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool);
3429 if (err != C2_OK || !pool) {
3430 continue;
3431 }
3432 err = pool->fetchGraphicBlock(
3433 width, height, halPixelFormat, C2MemoryUsage{minUsage}, &block);
3434 if (err != C2_OK || !block) {
3435 block.reset();
3436 continue;
3437 }
3438 break;
3439 }
3440 return block;
3441}
3442
Wonsik Kim155d5cb2019-10-09 12:49:49 -07003443} // namespace android