blob: e2512dc3605a9aff23c6a8513585835d82301e4a [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
24#include <C2Config.h>
25#include <C2Debug.h>
26#include <C2ParamInternal.h>
27#include <C2PlatformSupport.h>
28
Pawin Vongmasa36653902018-11-15 00:10:25 -080029#include <android/IOMXBufferSource.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070030#include <android/hardware/media/c2/1.0/IInputSurface.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080031#include <android/hardware/media/omx/1.0/IGraphicBufferSource.h>
32#include <android/hardware/media/omx/1.0/IOmx.h>
Wonsik Kim50811882022-04-28 15:57:27 -070033#include <android-base/properties.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080034#include <android-base/stringprintf.h>
35#include <cutils/properties.h>
36#include <gui/IGraphicBufferProducer.h>
37#include <gui/Surface.h>
38#include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h>
Wonsik Kim9917d4a2019-10-24 12:56:38 -070039#include <media/omx/1.0/WOmxNode.h>
40#include <media/openmax/OMX_Core.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080041#include <media/openmax/OMX_IndexExt.h>
Wonsik Kim1f5063d2021-05-03 15:41:17 -070042#include <media/stagefright/foundation/avc_utils.h>
Wonsik Kim9917d4a2019-10-24 12:56:38 -070043#include <media/stagefright/omx/1.0/WGraphicBufferSource.h>
44#include <media/stagefright/omx/OmxGraphicBufferSource.h>
Wonsik Kim155d5cb2019-10-09 12:49:49 -070045#include <media/stagefright/CCodec.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080046#include <media/stagefright/BufferProducerWrapper.h>
47#include <media/stagefright/MediaCodecConstants.h>
Songyue Han1e6769b2023-08-30 18:09:27 +000048#include <media/stagefright/MediaCodecMetricsConstants.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080049#include <media/stagefright/PersistentSurface.h>
Brian Lindahlff74e9d2023-07-20 14:44:04 -060050#include <media/stagefright/RenderedFrameInfo.h>
ted.sun765db4d2020-06-23 14:03:41 +080051#include <utils/NativeHandle.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080052
53#include "C2OMXNode.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080054#include "CCodecBufferChannel.h"
Wonsik Kim155d5cb2019-10-09 12:49:49 -070055#include "CCodecConfig.h"
Wonsik Kimfb7a7672019-12-27 17:13:33 -080056#include "Codec2Mapper.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080057#include "InputSurfaceWrapper.h"
58
59extern "C" android::PersistentSurface *CreateInputSurface();
60
61namespace android {
62
63using namespace std::chrono_literals;
64using ::android::hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer;
65using android::base::StringPrintf;
Pawin Vongmasad0f0e142018-11-15 03:36:28 -080066using ::android::hardware::media::c2::V1_0::IInputSurface;
Pawin Vongmasa36653902018-11-15 00:10:25 -080067
Wonsik Kim9917d4a2019-10-24 12:56:38 -070068typedef hardware::media::omx::V1_0::IGraphicBufferSource HGraphicBufferSource;
Wonsik Kim155d5cb2019-10-09 12:49:49 -070069typedef CCodecConfig Config;
Wonsik Kim9917d4a2019-10-24 12:56:38 -070070
Pawin Vongmasa36653902018-11-15 00:10:25 -080071namespace {
72
73class CCodecWatchdog : public AHandler {
74private:
75 enum {
76 kWhatWatch,
77 };
78 constexpr static int64_t kWatchIntervalUs = 3300000; // 3.3 secs
79
80public:
81 static sp<CCodecWatchdog> getInstance() {
82 static sp<CCodecWatchdog> instance(new CCodecWatchdog);
83 static std::once_flag flag;
84 // Call Init() only once.
85 std::call_once(flag, Init, instance);
86 return instance;
87 }
88
89 ~CCodecWatchdog() = default;
90
91 void watch(sp<CCodec> codec) {
92 bool shouldPost = false;
93 {
94 Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch);
95 // If a watch message is in flight, piggy-back this instance as well.
96 // Otherwise, post a new watch message.
97 shouldPost = codecs->empty();
98 codecs->emplace(codec);
99 }
100 if (shouldPost) {
101 ALOGV("posting watch message");
102 (new AMessage(kWhatWatch, this))->post(kWatchIntervalUs);
103 }
104 }
105
106protected:
107 void onMessageReceived(const sp<AMessage> &msg) {
108 switch (msg->what()) {
109 case kWhatWatch: {
110 Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch);
111 ALOGV("watch for %zu codecs", codecs->size());
112 for (auto it = codecs->begin(); it != codecs->end(); ++it) {
113 sp<CCodec> codec = it->promote();
114 if (codec == nullptr) {
115 continue;
116 }
117 codec->initiateReleaseIfStuck();
118 }
119 codecs->clear();
120 break;
121 }
122
123 default: {
124 TRESPASS("CCodecWatchdog: unrecognized message");
125 }
126 }
127 }
128
129private:
130 CCodecWatchdog() : mLooper(new ALooper) {}
131
132 static void Init(const sp<CCodecWatchdog> &thiz) {
133 ALOGV("Init");
134 thiz->mLooper->setName("CCodecWatchdog");
135 thiz->mLooper->registerHandler(thiz);
136 thiz->mLooper->start();
137 }
138
139 sp<ALooper> mLooper;
140
141 Mutexed<std::set<wp<CCodec>>> mCodecsToWatch;
142};
143
144class C2InputSurfaceWrapper : public InputSurfaceWrapper {
145public:
146 explicit C2InputSurfaceWrapper(
147 const std::shared_ptr<Codec2Client::InputSurface> &surface) :
148 mSurface(surface) {
149 }
150
151 ~C2InputSurfaceWrapper() override = default;
152
153 status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override {
154 if (mConnection != nullptr) {
155 return ALREADY_EXISTS;
156 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800157 return toStatusT(comp->connectToInputSurface(mSurface, &mConnection));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800158 }
159
160 void disconnect() override {
161 if (mConnection != nullptr) {
162 mConnection->disconnect();
163 mConnection = nullptr;
164 }
165 }
166
167 status_t start() override {
168 // InputSurface does not distinguish started state
169 return OK;
170 }
171
172 status_t signalEndOfInputStream() override {
173 C2InputSurfaceEosTuning eos(true);
174 std::vector<std::unique_ptr<C2SettingResult>> failures;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800175 c2_status_t err = mSurface->config({&eos}, C2_MAY_BLOCK, &failures);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800176 if (err != C2_OK) {
177 return UNKNOWN_ERROR;
178 }
179 return OK;
180 }
181
182 status_t configure(Config &config __unused) {
183 // TODO
184 return OK;
185 }
186
187private:
188 std::shared_ptr<Codec2Client::InputSurface> mSurface;
189 std::shared_ptr<Codec2Client::InputSurfaceConnection> mConnection;
190};
191
192class GraphicBufferSourceWrapper : public InputSurfaceWrapper {
193public:
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700194 typedef hardware::media::omx::V1_0::Status OmxStatus;
195
Pawin Vongmasa36653902018-11-15 00:10:25 -0800196 GraphicBufferSourceWrapper(
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700197 const sp<HGraphicBufferSource> &source,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800198 uint32_t width,
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700199 uint32_t height,
200 uint64_t usage)
Pawin Vongmasa36653902018-11-15 00:10:25 -0800201 : mSource(source), mWidth(width), mHeight(height) {
202 mDataSpace = HAL_DATASPACE_BT709;
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700203 mConfig.mUsage = usage;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800204 }
205 ~GraphicBufferSourceWrapper() override = default;
206
207 status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override {
208 mNode = new C2OMXNode(comp);
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700209 mOmxNode = new hardware::media::omx::V1_0::utils::TWOmxNode(mNode);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800210 mNode->setFrameSize(mWidth, mHeight);
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700211 // Usage is queried during configure(), so setting it beforehand.
Sungtak Lee0cd4fbc2023-02-02 00:59:01 +0000212 // 64 bit set parameter is existing only in C2OMXNode.
213 OMX_U64 usage64 = mConfig.mUsage;
214 status_t res = mNode->setParameter(
215 (OMX_INDEXTYPE)OMX_IndexParamConsumerUsageBits64,
216 &usage64, sizeof(usage64));
217
218 if (res != OK) {
219 OMX_U32 usage = mConfig.mUsage & 0xFFFFFFFF;
220 (void)mNode->setParameter(
221 (OMX_INDEXTYPE)OMX_IndexParamConsumerUsageBits,
222 &usage, sizeof(usage));
223 }
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700224
Yanqiang Fanc56f3e62021-09-28 16:54:07 +0800225 return GetStatus(mSource->configure(
226 mOmxNode, static_cast<hardware::graphics::common::V1_0::Dataspace>(mDataSpace)));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800227 }
228
229 void disconnect() override {
230 if (mNode == nullptr) {
231 return;
232 }
233 sp<IOMXBufferSource> source = mNode->getSource();
234 if (source == nullptr) {
235 ALOGD("GBSWrapper::disconnect: node is not configured with OMXBufferSource.");
236 return;
237 }
238 source->onOmxIdle();
239 source->onOmxLoaded();
240 mNode.clear();
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700241 mOmxNode.clear();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800242 }
243
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700244 status_t GetStatus(hardware::Return<OmxStatus> &&status) {
245 if (status.isOk()) {
246 return static_cast<status_t>(status.withDefault(OmxStatus::UNKNOWN_ERROR));
247 } else if (status.isDeadObject()) {
248 return DEAD_OBJECT;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800249 }
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700250 return UNKNOWN_ERROR;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800251 }
252
253 status_t start() override {
254 sp<IOMXBufferSource> source = mNode->getSource();
255 if (source == nullptr) {
256 return NO_INIT;
257 }
Taehwan Kim8b3bcdd2020-11-26 22:40:40 +0900258
Wonsik Kim0f6b61d2021-01-05 18:55:22 -0800259 size_t numSlots = 16;
Wonsik Kim34d66012021-03-01 16:40:33 -0800260 constexpr OMX_U32 kPortIndexInput = 0;
Taehwan Kim8b3bcdd2020-11-26 22:40:40 +0900261
Wonsik Kim34d66012021-03-01 16:40:33 -0800262 OMX_PARAM_PORTDEFINITIONTYPE param;
263 param.nPortIndex = kPortIndexInput;
264 status_t err = mNode->getParameter(OMX_IndexParamPortDefinition,
265 &param, sizeof(param));
266 if (err == OK) {
267 numSlots = param.nBufferCountActual;
Taehwan Kim8b3bcdd2020-11-26 22:40:40 +0900268 }
269
270 for (size_t i = 0; i < numSlots; ++i) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800271 source->onInputBufferAdded(i);
272 }
273
274 source->onOmxExecuting();
275 return OK;
276 }
277
278 status_t signalEndOfInputStream() override {
279 return GetStatus(mSource->signalEndOfInputStream());
280 }
281
282 status_t configure(Config &config) {
283 std::stringstream status;
284 status_t err = OK;
285
286 // handle each configuration granually, in case we need to handle part of the configuration
287 // elsewhere
288
289 // TRICKY: we do not unset frame delay repeating
290 if (config.mMinFps > 0 && config.mMinFps != mConfig.mMinFps) {
291 int64_t us = 1e6 / config.mMinFps + 0.5;
292 status_t res = GetStatus(mSource->setRepeatPreviousFrameDelayUs(us));
293 status << " minFps=" << config.mMinFps << " => repeatDelayUs=" << us;
294 if (res != OK) {
295 status << " (=> " << asString(res) << ")";
296 err = res;
297 }
298 mConfig.mMinFps = config.mMinFps;
299 }
300
301 // pts gap
302 if (config.mMinAdjustedFps > 0 || config.mFixedAdjustedFps > 0) {
303 if (mNode != nullptr) {
304 OMX_PARAM_U32TYPE ptrGapParam = {};
305 ptrGapParam.nSize = sizeof(OMX_PARAM_U32TYPE);
Wonsik Kim95ba0162019-03-19 15:51:54 -0700306 float gap = (config.mMinAdjustedFps > 0)
Pawin Vongmasa36653902018-11-15 00:10:25 -0800307 ? c2_min(INT32_MAX + 0., 1e6 / config.mMinAdjustedFps + 0.5)
308 : c2_max(0. - INT32_MAX, -1e6 / config.mFixedAdjustedFps - 0.5);
Wonsik Kim95ba0162019-03-19 15:51:54 -0700309 // float -> uint32_t is undefined if the value is negative.
310 // First convert to int32_t to ensure the expected behavior.
311 ptrGapParam.nU32 = int32_t(gap);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800312 (void)mNode->setParameter(
313 (OMX_INDEXTYPE)OMX_IndexParamMaxFrameDurationForBitrateControl,
314 &ptrGapParam, sizeof(ptrGapParam));
315 }
316 }
317
318 // max fps
319 // TRICKY: we do not unset max fps to 0 unless using fixed fps
Wonsik Kim95ba0162019-03-19 15:51:54 -0700320 if ((config.mMaxFps > 0 || (config.mFixedAdjustedFps > 0 && config.mMaxFps == -1))
Pawin Vongmasa36653902018-11-15 00:10:25 -0800321 && config.mMaxFps != mConfig.mMaxFps) {
322 status_t res = GetStatus(mSource->setMaxFps(config.mMaxFps));
323 status << " maxFps=" << config.mMaxFps;
324 if (res != OK) {
325 status << " (=> " << asString(res) << ")";
326 err = res;
327 }
328 mConfig.mMaxFps = config.mMaxFps;
329 }
330
331 if (config.mTimeOffsetUs != mConfig.mTimeOffsetUs) {
332 status_t res = GetStatus(mSource->setTimeOffsetUs(config.mTimeOffsetUs));
333 status << " timeOffset " << config.mTimeOffsetUs << "us";
334 if (res != OK) {
335 status << " (=> " << asString(res) << ")";
336 err = res;
337 }
338 mConfig.mTimeOffsetUs = config.mTimeOffsetUs;
339 }
340
341 if (config.mCaptureFps != mConfig.mCaptureFps || config.mCodedFps != mConfig.mCodedFps) {
342 status_t res =
343 GetStatus(mSource->setTimeLapseConfig(config.mCodedFps, config.mCaptureFps));
344 status << " timeLapse " << config.mCaptureFps << "fps as " << config.mCodedFps << "fps";
345 if (res != OK) {
346 status << " (=> " << asString(res) << ")";
347 err = res;
348 }
349 mConfig.mCaptureFps = config.mCaptureFps;
350 mConfig.mCodedFps = config.mCodedFps;
351 }
352
353 if (config.mStartAtUs != mConfig.mStartAtUs
354 || (config.mStopped != mConfig.mStopped && !config.mStopped)) {
355 status_t res = GetStatus(mSource->setStartTimeUs(config.mStartAtUs));
356 status << " start at " << config.mStartAtUs << "us";
357 if (res != OK) {
358 status << " (=> " << asString(res) << ")";
359 err = res;
360 }
361 mConfig.mStartAtUs = config.mStartAtUs;
362 mConfig.mStopped = config.mStopped;
363 }
364
365 // suspend-resume
366 if (config.mSuspended != mConfig.mSuspended) {
367 status_t res = GetStatus(mSource->setSuspend(config.mSuspended, config.mSuspendAtUs));
368 status << " " << (config.mSuspended ? "suspend" : "resume")
369 << " at " << config.mSuspendAtUs << "us";
370 if (res != OK) {
371 status << " (=> " << asString(res) << ")";
372 err = res;
373 }
374 mConfig.mSuspended = config.mSuspended;
375 mConfig.mSuspendAtUs = config.mSuspendAtUs;
376 }
377
378 if (config.mStopped != mConfig.mStopped && config.mStopped) {
379 status_t res = GetStatus(mSource->setStopTimeUs(config.mStopAtUs));
380 status << " stop at " << config.mStopAtUs << "us";
381 if (res != OK) {
382 status << " (=> " << asString(res) << ")";
383 err = res;
384 } else {
385 status << " delayUs";
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700386 hardware::Return<void> trans = mSource->getStopTimeOffsetUs(
387 [&res, &delayUs = config.mInputDelayUs](
388 auto status, auto stopTimeOffsetUs) {
389 res = static_cast<status_t>(status);
390 delayUs = stopTimeOffsetUs;
391 });
392 if (!trans.isOk()) {
393 res = trans.isDeadObject() ? DEAD_OBJECT : UNKNOWN_ERROR;
394 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800395 if (res != OK) {
396 status << " (=> " << asString(res) << ")";
397 } else {
398 status << "=" << config.mInputDelayUs << "us";
399 }
400 mConfig.mInputDelayUs = config.mInputDelayUs;
401 }
402 mConfig.mStopAtUs = config.mStopAtUs;
403 mConfig.mStopped = config.mStopped;
404 }
405
406 // color aspects (android._color-aspects)
407
Wonsik Kim9eac4d12019-05-23 12:58:48 -0700408 // consumer usage is queried earlier.
409
Wonsik Kima1335e12021-04-22 16:28:29 -0700410 // priority
411 if (mConfig.mPriority != config.mPriority) {
412 if (config.mPriority != INT_MAX) {
413 mNode->setPriority(config.mPriority);
414 }
415 mConfig.mPriority = config.mPriority;
416 }
417
Wonsik Kimbd557932019-07-02 15:51:20 -0700418 if (status.str().empty()) {
419 ALOGD("ISConfig not changed");
420 } else {
421 ALOGD("ISConfig%s", status.str().c_str());
422 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800423 return err;
424 }
425
Wonsik Kim4f3314d2019-03-26 17:00:34 -0700426 void onInputBufferDone(c2_cntr64_t index) override {
427 mNode->onInputBufferDone(index);
428 }
429
Wonsik Kim673dd192021-01-29 14:58:12 -0800430 android_dataspace getDataspace() override {
431 return mNode->getDataspace();
432 }
433
Songyue Hanad01f6a2023-08-17 05:45:35 +0000434 uint32_t getPixelFormat() override {
435 return mNode->getPixelFormat();
436 }
437
Pawin Vongmasa36653902018-11-15 00:10:25 -0800438private:
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700439 sp<HGraphicBufferSource> mSource;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800440 sp<C2OMXNode> mNode;
Wonsik Kim9917d4a2019-10-24 12:56:38 -0700441 sp<hardware::media::omx::V1_0::IOmxNode> mOmxNode;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800442 uint32_t mWidth;
443 uint32_t mHeight;
444 Config mConfig;
445};
446
447class Codec2ClientInterfaceWrapper : public C2ComponentStore {
448 std::shared_ptr<Codec2Client> mClient;
449
450public:
451 Codec2ClientInterfaceWrapper(std::shared_ptr<Codec2Client> client)
452 : mClient(client) { }
453
454 virtual ~Codec2ClientInterfaceWrapper() = default;
455
456 virtual c2_status_t config_sm(
457 const std::vector<C2Param *> &params,
458 std::vector<std::unique_ptr<C2SettingResult>> *const failures) {
459 return mClient->config(params, C2_MAY_BLOCK, failures);
460 };
461
462 virtual c2_status_t copyBuffer(
463 std::shared_ptr<C2GraphicBuffer>,
464 std::shared_ptr<C2GraphicBuffer>) {
465 return C2_OMITTED;
466 }
467
468 virtual c2_status_t createComponent(
469 C2String, std::shared_ptr<C2Component> *const component) {
470 component->reset();
471 return C2_OMITTED;
472 }
473
474 virtual c2_status_t createInterface(
475 C2String, std::shared_ptr<C2ComponentInterface> *const interface) {
476 interface->reset();
477 return C2_OMITTED;
478 }
479
480 virtual c2_status_t query_sm(
481 const std::vector<C2Param *> &stackParams,
482 const std::vector<C2Param::Index> &heapParamIndices,
483 std::vector<std::unique_ptr<C2Param>> *const heapParams) const {
484 return mClient->query(stackParams, heapParamIndices, C2_MAY_BLOCK, heapParams);
485 }
486
487 virtual c2_status_t querySupportedParams_nb(
488 std::vector<std::shared_ptr<C2ParamDescriptor>> *const params) const {
489 return mClient->querySupportedParams(params);
490 }
491
492 virtual c2_status_t querySupportedValues_sm(
493 std::vector<C2FieldSupportedValuesQuery> &fields) const {
494 return mClient->querySupportedValues(fields, C2_MAY_BLOCK);
495 }
496
497 virtual C2String getName() const {
498 return mClient->getName();
499 }
500
501 virtual std::shared_ptr<C2ParamReflector> getParamReflector() const {
502 return mClient->getParamReflector();
503 }
504
505 virtual std::vector<std::shared_ptr<const C2Component::Traits>> listComponents() {
506 return std::vector<std::shared_ptr<const C2Component::Traits>>();
507 }
508};
509
Wonsik Kim3b4349a2020-11-10 11:54:15 -0800510void RevertOutputFormatIfNeeded(
511 const sp<AMessage> &oldFormat, sp<AMessage> &currentFormat) {
512 // We used to not report changes to these keys to the client.
513 const static std::set<std::string> sIgnoredKeys({
514 KEY_BIT_RATE,
Harish Mahendrakar8c537502021-02-23 21:20:22 -0800515 KEY_FRAME_RATE,
Wonsik Kim3b4349a2020-11-10 11:54:15 -0800516 KEY_MAX_BIT_RATE,
Harish Mahendrakar8c537502021-02-23 21:20:22 -0800517 KEY_MAX_WIDTH,
518 KEY_MAX_HEIGHT,
Wonsik Kim3b4349a2020-11-10 11:54:15 -0800519 "csd-0",
520 "csd-1",
521 "csd-2",
522 });
523 if (currentFormat == oldFormat) {
524 return;
525 }
526 sp<AMessage> diff = currentFormat->changesFrom(oldFormat);
527 AMessage::Type type;
528 for (size_t i = diff->countEntries(); i > 0; --i) {
529 if (sIgnoredKeys.count(diff->getEntryNameAt(i - 1, &type)) > 0) {
530 diff->removeEntryAt(i - 1);
531 }
532 }
533 if (diff->countEntries() == 0) {
534 currentFormat = oldFormat;
535 }
536}
537
Wonsik Kim1f5063d2021-05-03 15:41:17 -0700538void AmendOutputFormatWithCodecSpecificData(
Greg Kaiserf2572aa2021-05-10 12:50:27 -0700539 const uint8_t *data, size_t size, const std::string &mediaType,
Wonsik Kim1f5063d2021-05-03 15:41:17 -0700540 const sp<AMessage> &outputFormat) {
541 if (mediaType == MIMETYPE_VIDEO_AVC) {
542 // Codec specific data should be SPS and PPS in a single buffer,
543 // each prefixed by a startcode (0x00 0x00 0x00 0x01).
544 // We separate the two and put them into the output format
545 // under the keys "csd-0" and "csd-1".
546
547 unsigned csdIndex = 0;
548
549 const uint8_t *nalStart;
550 size_t nalSize;
551 while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) {
552 sp<ABuffer> csd = new ABuffer(nalSize + 4);
553 memcpy(csd->data(), "\x00\x00\x00\x01", 4);
554 memcpy(csd->data() + 4, nalStart, nalSize);
555
556 outputFormat->setBuffer(
557 AStringPrintf("csd-%u", csdIndex).c_str(), csd);
558
559 ++csdIndex;
560 }
561
562 if (csdIndex != 2) {
563 ALOGW("Expected two NAL units from AVC codec config, but %u found",
564 csdIndex);
565 }
566 } else {
567 // For everything else we just stash the codec specific data into
568 // the output format as a single piece of csd under "csd-0".
569 sp<ABuffer> csd = new ABuffer(size);
570 memcpy(csd->data(), data, size);
571 csd->setRange(0, size);
572 outputFormat->setBuffer("csd-0", csd);
573 }
574}
575
Pawin Vongmasa36653902018-11-15 00:10:25 -0800576} // namespace
577
578// CCodec::ClientListener
579
580struct CCodec::ClientListener : public Codec2Client::Listener {
581
582 explicit ClientListener(const wp<CCodec> &codec) : mCodec(codec) {}
583
584 virtual void onWorkDone(
585 const std::weak_ptr<Codec2Client::Component>& component,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800586 std::list<std::unique_ptr<C2Work>>& workItems) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800587 (void)component;
588 sp<CCodec> codec(mCodec.promote());
589 if (!codec) {
590 return;
591 }
Wonsik Kimab34ed62019-01-31 15:28:46 -0800592 codec->onWorkDone(workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800593 }
594
595 virtual void onTripped(
596 const std::weak_ptr<Codec2Client::Component>& component,
597 const std::vector<std::shared_ptr<C2SettingResult>>& settingResult
598 ) override {
599 // TODO
600 (void)component;
601 (void)settingResult;
602 }
603
604 virtual void onError(
605 const std::weak_ptr<Codec2Client::Component>& component,
606 uint32_t errorCode) override {
Praveen Chavan72eff012020-11-20 23:20:28 -0800607 {
608 // Component is only used for reporting as we use a separate listener for each instance
609 std::shared_ptr<Codec2Client::Component> comp = component.lock();
610 if (!comp) {
611 ALOGD("Component died with error: 0x%x", errorCode);
612 } else {
613 ALOGD("Component \"%s\" returned error: 0x%x", comp->getName().c_str(), errorCode);
614 }
615 }
616
617 // Report to MediaCodec
Wonsik Kim10f33c02021-03-04 15:04:14 -0800618 // Note: for now we do not propagate the error code to MediaCodec
619 // except for C2_NO_MEMORY, as we would need to translate to a MediaCodec error.
Praveen Chavan72eff012020-11-20 23:20:28 -0800620 sp<CCodec> codec(mCodec.promote());
621 if (!codec || !codec->mCallback) {
622 return;
623 }
Wonsik Kim10f33c02021-03-04 15:04:14 -0800624 codec->mCallback->onError(
625 errorCode == C2_NO_MEMORY ? NO_MEMORY : UNKNOWN_ERROR,
626 ACTION_CODE_FATAL);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800627 }
628
629 virtual void onDeath(
630 const std::weak_ptr<Codec2Client::Component>& component) override {
631 { // Log the death of the component.
632 std::shared_ptr<Codec2Client::Component> comp = component.lock();
633 if (!comp) {
634 ALOGE("Codec2 component died.");
635 } else {
636 ALOGE("Codec2 component \"%s\" died.", comp->getName().c_str());
637 }
638 }
639
640 // Report to MediaCodec.
641 sp<CCodec> codec(mCodec.promote());
642 if (!codec || !codec->mCallback) {
643 return;
644 }
645 codec->mCallback->onError(DEAD_OBJECT, ACTION_CODE_FATAL);
646 }
647
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800648 virtual void onFrameRendered(uint64_t bufferQueueId,
649 int32_t slotId,
650 int64_t timestampNs) override {
651 // TODO: implement
652 (void)bufferQueueId;
653 (void)slotId;
654 (void)timestampNs;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800655 }
656
657 virtual void onInputBufferDone(
Wonsik Kimab34ed62019-01-31 15:28:46 -0800658 uint64_t frameIndex, size_t arrayIndex) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800659 sp<CCodec> codec(mCodec.promote());
660 if (codec) {
Wonsik Kimab34ed62019-01-31 15:28:46 -0800661 codec->onInputBufferDone(frameIndex, arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800662 }
663 }
664
665private:
666 wp<CCodec> mCodec;
667};
668
669// CCodecCallbackImpl
670
671class CCodecCallbackImpl : public CCodecCallback {
672public:
673 explicit CCodecCallbackImpl(CCodec *codec) : mCodec(codec) {}
674 ~CCodecCallbackImpl() override = default;
675
676 void onError(status_t err, enum ActionCode actionCode) override {
677 mCodec->mCallback->onError(err, actionCode);
678 }
679
680 void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) override {
Brian Lindahlff74e9d2023-07-20 14:44:04 -0600681 mCodec->mCallback->onOutputFramesRendered({RenderedFrameInfo(mediaTimeUs, renderTimeNs)});
Pawin Vongmasa36653902018-11-15 00:10:25 -0800682 }
683
Pawin Vongmasa36653902018-11-15 00:10:25 -0800684 void onOutputBuffersChanged() override {
685 mCodec->mCallback->onOutputBuffersChanged();
686 }
687
Guillaume Chelfi5ffbcb32021-04-12 14:23:43 +0200688 void onFirstTunnelFrameReady() override {
689 mCodec->mCallback->onFirstTunnelFrameReady();
690 }
691
Pawin Vongmasa36653902018-11-15 00:10:25 -0800692private:
693 CCodec *mCodec;
694};
695
696// CCodec
697
698CCodec::CCodec()
Wonsik Kim155d5cb2019-10-09 12:49:49 -0700699 : mChannel(new CCodecBufferChannel(std::make_shared<CCodecCallbackImpl>(this))),
700 mConfig(new CCodecConfig) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800701}
702
703CCodec::~CCodec() {
704}
705
706std::shared_ptr<BufferChannelBase> CCodec::getBufferChannel() {
707 return mChannel;
708}
709
710status_t CCodec::tryAndReportOnError(std::function<status_t()> job) {
711 status_t err = job();
712 if (err != C2_OK) {
713 mCallback->onError(err, ACTION_CODE_FATAL);
714 }
715 return err;
716}
717
718void CCodec::initiateAllocateComponent(const sp<AMessage> &msg) {
719 auto setAllocating = [this] {
720 Mutexed<State>::Locked state(mState);
721 if (state->get() != RELEASED) {
722 return INVALID_OPERATION;
723 }
724 state->set(ALLOCATING);
725 return OK;
726 };
727 if (tryAndReportOnError(setAllocating) != OK) {
728 return;
729 }
730
731 sp<RefBase> codecInfo;
732 CHECK(msg->findObject("codecInfo", &codecInfo));
733 // For Codec 2.0 components, componentName == codecInfo->getCodecName().
734
735 sp<AMessage> allocMsg(new AMessage(kWhatAllocate, this));
736 allocMsg->setObject("codecInfo", codecInfo);
737 allocMsg->post();
738}
739
740void CCodec::allocate(const sp<MediaCodecInfo> &codecInfo) {
741 if (codecInfo == nullptr) {
742 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
743 return;
744 }
745 ALOGD("allocate(%s)", codecInfo->getCodecName());
746 mClientListener.reset(new ClientListener(this));
747
748 AString componentName = codecInfo->getCodecName();
749 std::shared_ptr<Codec2Client> client;
750
751 // set up preferred component store to access vendor store parameters
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700752 client = Codec2Client::CreateFromService("default");
Pawin Vongmasa36653902018-11-15 00:10:25 -0800753 if (client) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800754 ALOGI("setting up '%s' as default (vendor) store", client->getServiceName().c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800755 SetPreferredCodec2ComponentStore(
756 std::make_shared<Codec2ClientInterfaceWrapper>(client));
757 }
758
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +0900759 std::shared_ptr<Codec2Client::Component> comp;
760 c2_status_t status = Codec2Client::CreateComponentByName(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800761 componentName.c_str(),
762 mClientListener,
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +0900763 &comp,
Pawin Vongmasa36653902018-11-15 00:10:25 -0800764 &client);
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +0900765 if (status != C2_OK) {
766 ALOGE("Failed Create component: %s, error=%d", componentName.c_str(), status);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800767 Mutexed<State>::Locked state(mState);
768 state->set(RELEASED);
769 state.unlock();
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +0900770 mCallback->onError((status == C2_NO_MEMORY ? NO_MEMORY : UNKNOWN_ERROR), ACTION_CODE_FATAL);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800771 state.lock();
772 return;
773 }
774 ALOGI("Created component [%s]", componentName.c_str());
775 mChannel->setComponent(comp);
776 auto setAllocated = [this, comp, client] {
777 Mutexed<State>::Locked state(mState);
778 if (state->get() != ALLOCATING) {
779 state->set(RELEASED);
780 return UNKNOWN_ERROR;
781 }
782 state->set(ALLOCATED);
783 state->comp = comp;
784 mClient = client;
785 return OK;
786 };
787 if (tryAndReportOnError(setAllocated) != OK) {
788 return;
789 }
790
791 // initialize config here in case setParameters is called prior to configure
Wonsik Kim155d5cb2019-10-09 12:49:49 -0700792 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
793 const std::unique_ptr<Config> &config = *configLocked;
Wonsik Kim8a6ed372019-12-03 16:05:51 -0800794 status_t err = config->initialize(mClient->getParamReflector(), comp);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800795 if (err != OK) {
796 ALOGW("Failed to initialize configuration support");
797 // TODO: report error once we complete implementation.
798 }
799 config->queryConfiguration(comp);
800
801 mCallback->onComponentAllocated(componentName.c_str());
802}
803
804void CCodec::initiateConfigureComponent(const sp<AMessage> &format) {
805 auto checkAllocated = [this] {
806 Mutexed<State>::Locked state(mState);
807 return (state->get() != ALLOCATED) ? UNKNOWN_ERROR : OK;
808 };
809 if (tryAndReportOnError(checkAllocated) != OK) {
810 return;
811 }
812
813 sp<AMessage> msg(new AMessage(kWhatConfigure, this));
814 msg->setMessage("format", format);
815 msg->post();
816}
817
818void CCodec::configure(const sp<AMessage> &msg) {
819 std::shared_ptr<Codec2Client::Component> comp;
820 auto checkAllocated = [this, &comp] {
821 Mutexed<State>::Locked state(mState);
822 if (state->get() != ALLOCATED) {
823 state->set(RELEASED);
824 return UNKNOWN_ERROR;
825 }
826 comp = state->comp;
827 return OK;
828 };
829 if (tryAndReportOnError(checkAllocated) != OK) {
830 return;
831 }
832
833 auto doConfig = [msg, comp, this]() -> status_t {
834 AString mime;
835 if (!msg->findString("mime", &mime)) {
836 return BAD_VALUE;
837 }
838
839 int32_t encoder;
840 if (!msg->findInt32("encoder", &encoder)) {
841 encoder = false;
842 }
843
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800844 int32_t flags;
845 if (!msg->findInt32("flags", &flags)) {
846 return BAD_VALUE;
847 }
848
Pawin Vongmasa36653902018-11-15 00:10:25 -0800849 // TODO: read from intf()
850 if ((!encoder) != (comp->getName().find("encoder") == std::string::npos)) {
851 return UNKNOWN_ERROR;
852 }
853
854 int32_t storeMeta;
855 if (encoder
856 && msg->findInt32("android._input-metadata-buffer-type", &storeMeta)
857 && storeMeta != kMetadataBufferTypeInvalid) {
858 if (storeMeta != kMetadataBufferTypeANWBuffer) {
859 ALOGD("Only ANW buffers are supported for legacy metadata mode");
860 return BAD_VALUE;
861 }
862 mChannel->setMetaMode(CCodecBufferChannel::MODE_ANW);
863 }
864
ted.sun765db4d2020-06-23 14:03:41 +0800865 status_t err = OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800866 sp<RefBase> obj;
867 sp<Surface> surface;
868 if (msg->findObject("native-window", &obj)) {
869 surface = static_cast<Surface *>(obj.get());
ted.sun765db4d2020-06-23 14:03:41 +0800870 // setup tunneled playback
871 if (surface != nullptr) {
872 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
873 const std::unique_ptr<Config> &config = *configLocked;
874 if ((config->mDomain & Config::IS_DECODER)
875 && (config->mDomain & Config::IS_VIDEO)) {
876 int32_t tunneled;
877 if (msg->findInt32("feature-tunneled-playback", &tunneled) && tunneled != 0) {
878 ALOGI("Configuring TUNNELED video playback.");
879
880 err = configureTunneledVideoPlayback(comp, &config->mSidebandHandle, msg);
881 if (err != OK) {
882 ALOGE("configureTunneledVideoPlayback failed!");
883 return err;
884 }
885 config->mTunneled = true;
886 }
Guillaume Chelfi2d4c9db2022-03-18 13:43:49 +0100887
888 int32_t pushBlankBuffersOnStop = 0;
889 if (msg->findInt32(KEY_PUSH_BLANK_BUFFERS_ON_STOP, &pushBlankBuffersOnStop)) {
890 config->mPushBlankBuffersOnStop = pushBlankBuffersOnStop == 1;
891 }
shuanglong.wang480a8362023-02-17 20:55:51 +0800892 // secure compoment or protected content default with
893 // "push-blank-buffers-on-shutdown" flag
894 if (!config->mPushBlankBuffersOnStop) {
895 int32_t usageProtected;
896 if (comp->getName().find(".secure") != std::string::npos) {
897 config->mPushBlankBuffersOnStop = true;
898 } else if (msg->findInt32("protected", &usageProtected) && usageProtected) {
899 config->mPushBlankBuffersOnStop = true;
900 }
901 }
ted.sun765db4d2020-06-23 14:03:41 +0800902 }
903 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800904 setSurface(surface);
905 }
906
Wonsik Kim155d5cb2019-10-09 12:49:49 -0700907 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
908 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800909 config->mUsingSurface = surface != nullptr;
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800910 config->mBuffersBoundToCodec = ((flags & CONFIGURE_FLAG_USE_BLOCK_MODEL) == 0);
911 ALOGD("[%s] buffers are %sbound to CCodec for this session",
912 comp->getName().c_str(), config->mBuffersBoundToCodec ? "" : "not ");
Pawin Vongmasa36653902018-11-15 00:10:25 -0800913
Wonsik Kim1114eea2019-02-25 14:35:24 -0800914 // Enforce required parameters
915 int32_t i32;
916 float flt;
917 if (config->mDomain & Config::IS_AUDIO) {
918 if (!msg->findInt32(KEY_SAMPLE_RATE, &i32)) {
919 ALOGD("sample rate is missing, which is required for audio components.");
920 return BAD_VALUE;
921 }
922 if (!msg->findInt32(KEY_CHANNEL_COUNT, &i32)) {
923 ALOGD("channel count is missing, which is required for audio components.");
924 return BAD_VALUE;
925 }
926 if ((config->mDomain & Config::IS_ENCODER)
927 && !mime.equalsIgnoreCase(MEDIA_MIMETYPE_AUDIO_FLAC)
928 && !msg->findInt32(KEY_BIT_RATE, &i32)
929 && !msg->findFloat(KEY_BIT_RATE, &flt)) {
930 ALOGD("bitrate is missing, which is required for audio encoders.");
931 return BAD_VALUE;
932 }
933 }
Wonsik Kimd91f3fb2021-02-24 12:35:31 -0800934 int32_t width = 0;
935 int32_t height = 0;
Wonsik Kim1114eea2019-02-25 14:35:24 -0800936 if (config->mDomain & (Config::IS_IMAGE | Config::IS_VIDEO)) {
Wonsik Kimd91f3fb2021-02-24 12:35:31 -0800937 if (!msg->findInt32(KEY_WIDTH, &width)) {
Wonsik Kim1114eea2019-02-25 14:35:24 -0800938 ALOGD("width is missing, which is required for image/video components.");
939 return BAD_VALUE;
940 }
Wonsik Kimd91f3fb2021-02-24 12:35:31 -0800941 if (!msg->findInt32(KEY_HEIGHT, &height)) {
Wonsik Kim1114eea2019-02-25 14:35:24 -0800942 ALOGD("height is missing, which is required for image/video components.");
943 return BAD_VALUE;
944 }
945 if ((config->mDomain & Config::IS_ENCODER) && (config->mDomain & Config::IS_VIDEO)) {
Harish Mahendrakar71cbb9d2019-05-21 11:21:27 -0700946 int32_t mode = BITRATE_MODE_VBR;
947 if (msg->findInt32(KEY_BITRATE_MODE, &mode) && mode == BITRATE_MODE_CQ) {
Harish Mahendrakar817d3182019-03-11 16:37:47 -0700948 if (!msg->findInt32(KEY_QUALITY, &i32)) {
949 ALOGD("quality is missing, which is required for video encoders in CQ.");
950 return BAD_VALUE;
951 }
952 } else {
953 if (!msg->findInt32(KEY_BIT_RATE, &i32)
954 && !msg->findFloat(KEY_BIT_RATE, &flt)) {
955 ALOGD("bitrate is missing, which is required for video encoders.");
956 return BAD_VALUE;
957 }
Wonsik Kim1114eea2019-02-25 14:35:24 -0800958 }
959 if (!msg->findInt32(KEY_I_FRAME_INTERVAL, &i32)
960 && !msg->findFloat(KEY_I_FRAME_INTERVAL, &flt)) {
961 ALOGD("I frame interval is missing, which is required for video encoders.");
962 return BAD_VALUE;
963 }
Wonsik Kimaab2eea2019-05-22 10:37:58 -0700964 if (!msg->findInt32(KEY_FRAME_RATE, &i32)
965 && !msg->findFloat(KEY_FRAME_RATE, &flt)) {
966 ALOGD("frame rate is missing, which is required for video encoders.");
967 return BAD_VALUE;
968 }
Wonsik Kim1114eea2019-02-25 14:35:24 -0800969 }
970 }
971
Pawin Vongmasa36653902018-11-15 00:10:25 -0800972 /*
973 * Handle input surface configuration
974 */
975 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))
976 && (config->mDomain & Config::IS_ENCODER)) {
977 config->mISConfig.reset(new InputSurfaceWrapper::Config{});
978 {
979 config->mISConfig->mMinFps = 0;
980 int64_t value;
Chong Zhang038e8f82019-02-06 19:05:14 -0800981 if (msg->findInt64(KEY_REPEAT_PREVIOUS_FRAME_AFTER, &value) && value > 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800982 config->mISConfig->mMinFps = 1e6 / value;
983 }
Wonsik Kim95ba0162019-03-19 15:51:54 -0700984 if (!msg->findFloat(
985 KEY_MAX_FPS_TO_ENCODER, &config->mISConfig->mMaxFps)) {
986 config->mISConfig->mMaxFps = -1;
987 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800988 config->mISConfig->mMinAdjustedFps = 0;
989 config->mISConfig->mFixedAdjustedFps = 0;
Chong Zhang038e8f82019-02-06 19:05:14 -0800990 if (msg->findInt64(KEY_MAX_PTS_GAP_TO_ENCODER, &value)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800991 if (value < 0 && value >= INT32_MIN) {
992 config->mISConfig->mFixedAdjustedFps = -1e6 / value;
Wonsik Kim95ba0162019-03-19 15:51:54 -0700993 config->mISConfig->mMaxFps = -1;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800994 } else if (value > 0 && value <= INT32_MAX) {
995 config->mISConfig->mMinAdjustedFps = 1e6 / value;
996 }
997 }
998 }
999
1000 {
Wonsik Kim8e55f3a2019-09-03 14:10:37 -07001001 bool captureFpsFound = false;
1002 double timeLapseFps;
1003 float captureRate;
1004 if (msg->findDouble("time-lapse-fps", &timeLapseFps)) {
1005 config->mISConfig->mCaptureFps = timeLapseFps;
1006 captureFpsFound = true;
1007 } else if (msg->findAsFloat(KEY_CAPTURE_RATE, &captureRate)) {
1008 config->mISConfig->mCaptureFps = captureRate;
1009 captureFpsFound = true;
1010 }
1011 if (captureFpsFound) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001012 (void)msg->findAsFloat(KEY_FRAME_RATE, &config->mISConfig->mCodedFps);
1013 }
1014 }
1015
1016 {
1017 config->mISConfig->mSuspended = false;
1018 config->mISConfig->mSuspendAtUs = -1;
1019 int32_t value;
Chong Zhang038e8f82019-02-06 19:05:14 -08001020 if (msg->findInt32(KEY_CREATE_INPUT_SURFACE_SUSPENDED, &value) && value) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001021 config->mISConfig->mSuspended = true;
1022 }
1023 }
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001024 config->mISConfig->mUsage = 0;
Wonsik Kima1335e12021-04-22 16:28:29 -07001025 config->mISConfig->mPriority = INT_MAX;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001026 }
1027
1028 /*
1029 * Handle desired color format.
1030 */
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001031 int32_t defaultColorFormat = COLOR_FormatYUV420Flexible;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001032 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) {
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001033 int32_t format = 0;
1034 // Query vendor format for Flexible YUV
1035 std::vector<std::unique_ptr<C2Param>> heapParams;
1036 C2StoreFlexiblePixelFormatDescriptorsInfo *pixelFormatInfo = nullptr;
Wonsik Kim50811882022-04-28 15:57:27 -07001037 int vendorSdkVersion = base::GetIntProperty(
1038 "ro.vendor.build.version.sdk", android_get_device_api_level());
guochuang709b48b2022-10-25 20:40:42 +08001039 if (mClient->query(
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001040 {},
1041 {C2StoreFlexiblePixelFormatDescriptorsInfo::PARAM_TYPE},
1042 C2_MAY_BLOCK,
1043 &heapParams) == C2_OK
1044 && heapParams.size() == 1u) {
1045 pixelFormatInfo = C2StoreFlexiblePixelFormatDescriptorsInfo::From(
1046 heapParams[0].get());
1047 } else {
1048 pixelFormatInfo = nullptr;
1049 }
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001050 // bit depth -> format
1051 std::map<uint32_t, uint32_t> flexPixelFormat;
1052 std::map<uint32_t, uint32_t> flexPlanarPixelFormat;
1053 std::map<uint32_t, uint32_t> flexSemiPlanarPixelFormat;
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001054 if (pixelFormatInfo && *pixelFormatInfo) {
1055 for (size_t i = 0; i < pixelFormatInfo->flexCount(); ++i) {
1056 const C2FlexiblePixelFormatDescriptorStruct &desc =
1057 pixelFormatInfo->m.values[i];
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001058 if (desc.subsampling != C2Color::YUV_420
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001059 // TODO(b/180076105): some device report wrong layout
1060 // || desc.layout == C2Color::INTERLEAVED_PACKED
1061 // || desc.layout == C2Color::INTERLEAVED_ALIGNED
1062 || desc.layout == C2Color::UNKNOWN_LAYOUT) {
1063 continue;
1064 }
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001065 if (flexPixelFormat.count(desc.bitDepth) == 0) {
1066 flexPixelFormat.emplace(desc.bitDepth, desc.pixelFormat);
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001067 }
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001068 if (desc.layout == C2Color::PLANAR_PACKED
1069 && flexPlanarPixelFormat.count(desc.bitDepth) == 0) {
1070 flexPlanarPixelFormat.emplace(desc.bitDepth, desc.pixelFormat);
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001071 }
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001072 if (desc.layout == C2Color::SEMIPLANAR_PACKED
1073 && flexSemiPlanarPixelFormat.count(desc.bitDepth) == 0) {
1074 flexSemiPlanarPixelFormat.emplace(desc.bitDepth, desc.pixelFormat);
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001075 }
1076 }
1077 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001078 if (!msg->findInt32(KEY_COLOR_FORMAT, &format)) {
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001079 // Also handle default color format (encoders require color format, so this is only
1080 // needed for decoders.
Pawin Vongmasa36653902018-11-15 00:10:25 -08001081 if (!(config->mDomain & Config::IS_ENCODER)) {
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001082 if (surface == nullptr) {
Wonsik Kim1eb88a92021-03-29 20:44:04 -07001083 const char *prefix = "";
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001084 if (flexSemiPlanarPixelFormat.count(8) != 0) {
Wonsik Kim1eb88a92021-03-29 20:44:04 -07001085 format = COLOR_FormatYUV420SemiPlanar;
1086 prefix = "semi-";
1087 } else {
1088 format = COLOR_FormatYUV420Planar;
1089 }
1090 ALOGD("Client requested ByteBuffer mode decoder w/o color format set: "
1091 "using default %splanar color format", prefix);
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001092 } else {
1093 format = COLOR_FormatSurface;
1094 }
1095 defaultColorFormat = format;
1096 }
1097 } else {
1098 if ((config->mDomain & Config::IS_ENCODER) || !surface) {
Wonsik Kim2b8579f2022-05-04 13:30:33 -07001099 if (vendorSdkVersion < __ANDROID_API_S__ &&
Taehwan Kim43e715d2022-09-22 12:04:59 +09001100 (format == COLOR_FormatYUV420Planar ||
Wonsik Kim2b8579f2022-05-04 13:30:33 -07001101 format == COLOR_FormatYUV420PackedPlanar ||
1102 format == COLOR_FormatYUV420SemiPlanar ||
1103 format == COLOR_FormatYUV420PackedSemiPlanar)) {
1104 // pre-S framework used to map these color formats into YV12.
1105 // Codecs from older vendor partition may be relying on
1106 // this assumption.
1107 format = HAL_PIXEL_FORMAT_YV12;
1108 }
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001109 switch (format) {
1110 case COLOR_FormatYUV420Flexible:
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001111 format = COLOR_FormatYUV420Planar;
1112 if (flexPixelFormat.count(8) != 0) {
1113 format = flexPixelFormat[8];
1114 }
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001115 break;
1116 case COLOR_FormatYUV420Planar:
1117 case COLOR_FormatYUV420PackedPlanar:
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001118 if (flexPlanarPixelFormat.count(8) != 0) {
1119 format = flexPlanarPixelFormat[8];
1120 } else if (flexPixelFormat.count(8) != 0) {
1121 format = flexPixelFormat[8];
1122 }
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001123 break;
1124 case COLOR_FormatYUV420SemiPlanar:
1125 case COLOR_FormatYUV420PackedSemiPlanar:
Wonsik Kim08a8a2b2021-05-10 19:03:47 -07001126 if (flexSemiPlanarPixelFormat.count(8) != 0) {
1127 format = flexSemiPlanarPixelFormat[8];
1128 } else if (flexPixelFormat.count(8) != 0) {
1129 format = flexPixelFormat[8];
1130 }
1131 break;
1132 case COLOR_FormatYUVP010:
1133 format = COLOR_FormatYUVP010;
1134 if (flexSemiPlanarPixelFormat.count(10) != 0) {
1135 format = flexSemiPlanarPixelFormat[10];
1136 } else if (flexPixelFormat.count(10) != 0) {
1137 format = flexPixelFormat[10];
1138 }
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001139 break;
1140 default:
1141 // No-op
1142 break;
1143 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001144 }
1145 }
1146
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001147 if (format != 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001148 msg->setInt32("android._color-format", format);
1149 }
1150 }
1151
Wonsik Kim77e97c72021-01-20 10:33:22 -08001152 /*
1153 * Handle dataspace
1154 */
1155 int32_t usingRecorder;
1156 if (msg->findInt32("android._using-recorder", &usingRecorder) && usingRecorder) {
1157 android_dataspace dataSpace = HAL_DATASPACE_BT709;
1158 int32_t width, height;
1159 if (msg->findInt32("width", &width)
1160 && msg->findInt32("height", &height)) {
Wonsik Kim4f13d112021-03-17 04:37:46 +00001161 ColorAspects aspects;
1162 getColorAspectsFromFormat(msg, aspects);
1163 setDefaultCodecColorAspectsIfNeeded(aspects, width, height);
Wonsik Kim77e97c72021-01-20 10:33:22 -08001164 // TODO: read dataspace / color aspect from the component
Wonsik Kim4f13d112021-03-17 04:37:46 +00001165 setColorAspectsIntoFormat(aspects, const_cast<sp<AMessage> &>(msg));
1166 dataSpace = getDataSpaceForColorAspects(aspects, true /* mayexpand */);
Wonsik Kim77e97c72021-01-20 10:33:22 -08001167 }
1168 msg->setInt32("android._dataspace", (int32_t)dataSpace);
1169 ALOGD("setting dataspace to %x", dataSpace);
1170 }
1171
Wonsik Kim8a6ed372019-12-03 16:05:51 -08001172 int32_t subscribeToAllVendorParams;
1173 if (msg->findInt32("x-*", &subscribeToAllVendorParams) && subscribeToAllVendorParams) {
1174 if (config->subscribeToAllVendorParams(comp, C2_MAY_BLOCK) != OK) {
1175 ALOGD("[%s] Failed to subscribe to all vendor params", comp->getName().c_str());
1176 }
1177 }
1178
Pawin Vongmasa36653902018-11-15 00:10:25 -08001179 std::vector<std::unique_ptr<C2Param>> configUpdate;
Wonsik Kimaa484ac2019-02-13 16:54:02 -08001180 // NOTE: We used to ignore "video-bitrate" at configure; replicate
1181 // the behavior here.
1182 sp<AMessage> sdkParams = msg;
1183 int32_t videoBitrate;
1184 if (sdkParams->findInt32(PARAMETER_KEY_VIDEO_BITRATE, &videoBitrate)) {
1185 sdkParams = msg->dup();
1186 sdkParams->removeEntryAt(sdkParams->findEntryByName(PARAMETER_KEY_VIDEO_BITRATE));
1187 }
ted.sun765db4d2020-06-23 14:03:41 +08001188 err = config->getConfigUpdateFromSdkParams(
Wonsik Kimaa484ac2019-02-13 16:54:02 -08001189 comp, sdkParams, Config::IS_CONFIG, C2_DONT_BLOCK, &configUpdate);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001190 if (err != OK) {
1191 ALOGW("failed to convert configuration to c2 params");
1192 }
Wonsik Kimaab2eea2019-05-22 10:37:58 -07001193
1194 int32_t maxBframes = 0;
1195 if ((config->mDomain & Config::IS_ENCODER)
1196 && (config->mDomain & Config::IS_VIDEO)
1197 && sdkParams->findInt32(KEY_MAX_B_FRAMES, &maxBframes)
1198 && maxBframes > 0) {
1199 std::unique_ptr<C2StreamGopTuning::output> gop =
1200 C2StreamGopTuning::output::AllocUnique(2 /* flexCount */, 0u /* stream */);
1201 gop->m.values[0] = { P_FRAME, UINT32_MAX };
1202 gop->m.values[1] = {
1203 C2Config::picture_type_t(P_FRAME | B_FRAME),
1204 uint32_t(maxBframes)
1205 };
1206 configUpdate.push_back(std::move(gop));
1207 }
1208
Ray Essicka0ae6972021-03-10 19:40:01 -08001209 if ((config->mDomain & Config::IS_ENCODER)
1210 && (config->mDomain & Config::IS_VIDEO)) {
1211 // we may not use all 3 of these entries
1212 std::unique_ptr<C2StreamPictureQuantizationTuning::output> qp =
1213 C2StreamPictureQuantizationTuning::output::AllocUnique(3 /* flexCount */,
1214 0u /* stream */);
1215
1216 int ix = 0;
1217
1218 int32_t iMax = INT32_MAX;
1219 int32_t iMin = INT32_MIN;
1220 (void) sdkParams->findInt32(KEY_VIDEO_QP_I_MAX, &iMax);
1221 (void) sdkParams->findInt32(KEY_VIDEO_QP_I_MIN, &iMin);
1222 if (iMax != INT32_MAX || iMin != INT32_MIN) {
1223 qp->m.values[ix++] = {I_FRAME, iMin, iMax};
1224 }
1225
1226 int32_t pMax = INT32_MAX;
1227 int32_t pMin = INT32_MIN;
1228 (void) sdkParams->findInt32(KEY_VIDEO_QP_P_MAX, &pMax);
1229 (void) sdkParams->findInt32(KEY_VIDEO_QP_P_MIN, &pMin);
1230 if (pMax != INT32_MAX || pMin != INT32_MIN) {
1231 qp->m.values[ix++] = {P_FRAME, pMin, pMax};
1232 }
1233
1234 int32_t bMax = INT32_MAX;
1235 int32_t bMin = INT32_MIN;
1236 (void) sdkParams->findInt32(KEY_VIDEO_QP_B_MAX, &bMax);
1237 (void) sdkParams->findInt32(KEY_VIDEO_QP_B_MIN, &bMin);
1238 if (bMax != INT32_MAX || bMin != INT32_MIN) {
1239 qp->m.values[ix++] = {B_FRAME, bMin, bMax};
1240 }
1241
1242 // adjust to reflect actual use.
1243 qp->setFlexCount(ix);
1244
1245 configUpdate.push_back(std::move(qp));
1246 }
1247
Wonsik Kima1335e12021-04-22 16:28:29 -07001248 int32_t background = 0;
1249 if ((config->mDomain & Config::IS_VIDEO)
1250 && msg->findInt32("android._background-mode", &background)
1251 && background) {
1252 androidSetThreadPriority(gettid(), ANDROID_PRIORITY_BACKGROUND);
1253 if (config->mISConfig) {
1254 config->mISConfig->mPriority = ANDROID_PRIORITY_BACKGROUND;
1255 }
1256 }
1257
Pawin Vongmasa36653902018-11-15 00:10:25 -08001258 err = config->setParameters(comp, configUpdate, C2_DONT_BLOCK);
1259 if (err != OK) {
1260 ALOGW("failed to configure c2 params");
1261 return err;
1262 }
1263
1264 std::vector<std::unique_ptr<C2Param>> params;
1265 C2StreamUsageTuning::input usage(0u, 0u);
1266 C2StreamMaxBufferSizeInfo::input maxInputSize(0u, 0u);
Wonsik Kim9ca01d32019-04-01 14:45:47 -07001267 C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001268
Wonsik Kim3baecda2021-02-07 22:19:56 -08001269 C2Param::Index colorAspectsRequestIndex =
1270 C2StreamColorAspectsInfo::output::PARAM_TYPE | C2Param::CoreIndex::IS_REQUEST_FLAG;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001271 std::initializer_list<C2Param::Index> indices {
Wonsik Kim3baecda2021-02-07 22:19:56 -08001272 colorAspectsRequestIndex.withStream(0u),
Pawin Vongmasa36653902018-11-15 00:10:25 -08001273 };
Chaejung Lim86c22dc2021-12-23 00:41:05 -08001274 int32_t colorTransferRequest = 0;
1275 if (config->mDomain & (Config::IS_IMAGE | Config::IS_VIDEO)
1276 && !sdkParams->findInt32("color-transfer-request", &colorTransferRequest)) {
1277 colorTransferRequest = 0;
1278 }
1279 c2_status_t c2err = C2_OK;
1280 if (colorTransferRequest != 0) {
1281 c2err = comp->query(
1282 { &usage, &maxInputSize, &prepend },
1283 indices,
1284 C2_DONT_BLOCK,
1285 &params);
1286 } else {
1287 c2err = comp->query(
1288 { &usage, &maxInputSize, &prepend },
1289 {},
1290 C2_DONT_BLOCK,
1291 &params);
1292 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001293 if (c2err != C2_OK && c2err != C2_BAD_INDEX) {
1294 ALOGE("Failed to query component interface: %d", c2err);
1295 return UNKNOWN_ERROR;
1296 }
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001297 if (usage) {
1298 if (usage.value & C2MemoryUsage::CPU_READ) {
1299 config->mInputFormat->setInt32("using-sw-read-often", true);
1300 }
1301 if (config->mISConfig) {
1302 C2AndroidMemoryUsage androidUsage(C2MemoryUsage(usage.value));
1303 config->mISConfig->mUsage = androidUsage.asGrallocUsage();
1304 }
Wonsik Kim666604a2020-05-14 16:57:49 -07001305 config->mInputFormat->setInt64("android._C2MemoryUsage", usage.value);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001306 }
1307
1308 // NOTE: we don't blindly use client specified input size if specified as clients
1309 // at times specify too small size. Instead, mimic the behavior from OMX, where the
1310 // client specified size is only used to ask for bigger buffers than component suggested
1311 // size.
1312 int32_t clientInputSize = 0;
1313 bool clientSpecifiedInputSize =
1314 msg->findInt32(KEY_MAX_INPUT_SIZE, &clientInputSize) && clientInputSize > 0;
1315 // TEMP: enforce minimum buffer size of 1MB for video decoders
1316 // and 16K / 4K for audio encoders/decoders
1317 if (maxInputSize.value == 0) {
1318 if (config->mDomain & Config::IS_AUDIO) {
1319 maxInputSize.value = encoder ? 16384 : 4096;
1320 } else if (!encoder) {
1321 maxInputSize.value = 1048576u;
1322 }
1323 }
1324
1325 // verify that CSD fits into this size (if defined)
1326 if ((config->mDomain & Config::IS_DECODER) && maxInputSize.value > 0) {
1327 sp<ABuffer> csd;
1328 for (size_t ix = 0; msg->findBuffer(StringPrintf("csd-%zu", ix).c_str(), &csd); ++ix) {
1329 if (csd && csd->size() > maxInputSize.value) {
1330 maxInputSize.value = csd->size();
1331 }
1332 }
1333 }
1334
1335 // TODO: do this based on component requiring linear allocator for input
1336 if ((config->mDomain & Config::IS_DECODER) || (config->mDomain & Config::IS_AUDIO)) {
1337 if (clientSpecifiedInputSize) {
1338 // Warn that we're overriding client's max input size if necessary.
1339 if ((uint32_t)clientInputSize < maxInputSize.value) {
1340 ALOGD("client requested max input size %d, which is smaller than "
1341 "what component recommended (%u); overriding with component "
1342 "recommendation.", clientInputSize, maxInputSize.value);
1343 ALOGW("This behavior is subject to change. It is recommended that "
1344 "app developers double check whether the requested "
1345 "max input size is in reasonable range.");
1346 } else {
1347 maxInputSize.value = clientInputSize;
1348 }
1349 }
1350 // Pass max input size on input format to the buffer channel (if supplied by the
1351 // component or by a default)
1352 if (maxInputSize.value) {
1353 config->mInputFormat->setInt32(
1354 KEY_MAX_INPUT_SIZE,
1355 (int32_t)(c2_min(maxInputSize.value, uint32_t(INT32_MAX))));
1356 }
1357 }
1358
Wonsik Kim9ca01d32019-04-01 14:45:47 -07001359 int32_t clientPrepend;
1360 if ((config->mDomain & Config::IS_VIDEO)
1361 && (config->mDomain & Config::IS_ENCODER)
Lajos Molnara2b5f5a2020-10-14 16:36:18 -07001362 && msg->findInt32(KEY_PREPEND_HEADER_TO_SYNC_FRAMES, &clientPrepend)
Wonsik Kim9ca01d32019-04-01 14:45:47 -07001363 && clientPrepend
1364 && (!prepend || prepend.value != PREPEND_HEADER_TO_ALL_SYNC)) {
Lajos Molnara2b5f5a2020-10-14 16:36:18 -07001365 ALOGE("Failed to set KEY_PREPEND_HEADER_TO_SYNC_FRAMES");
Wonsik Kim9ca01d32019-04-01 14:45:47 -07001366 return BAD_VALUE;
1367 }
1368
Wonsik Kimd91f3fb2021-02-24 12:35:31 -08001369 int32_t componentColorFormat = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001370 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) {
1371 // propagate HDR static info to output format for both encoders and decoders
1372 // if component supports this info, we will update from component, but only the raw port,
1373 // so don't propagate if component already filled it in.
1374 sp<ABuffer> hdrInfo;
1375 if (msg->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo)
1376 && !config->mOutputFormat->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo)) {
1377 config->mOutputFormat->setBuffer(KEY_HDR_STATIC_INFO, hdrInfo);
1378 }
1379
1380 // Set desired color format from configuration parameter
1381 int32_t format;
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001382 if (!msg->findInt32(KEY_COLOR_FORMAT, &format)) {
1383 format = defaultColorFormat;
1384 }
1385 if (config->mDomain & Config::IS_ENCODER) {
1386 config->mInputFormat->setInt32(KEY_COLOR_FORMAT, format);
Wonsik Kimd91f3fb2021-02-24 12:35:31 -08001387 if (msg->findInt32("android._color-format", &componentColorFormat)) {
1388 config->mInputFormat->setInt32("android._color-format", componentColorFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001389 }
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001390 } else {
1391 config->mOutputFormat->setInt32(KEY_COLOR_FORMAT, format);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001392 }
1393 }
1394
1395 // propagate encoder delay and padding to output format
1396 if ((config->mDomain & Config::IS_DECODER) && (config->mDomain & Config::IS_AUDIO)) {
1397 int delay = 0;
1398 if (msg->findInt32("encoder-delay", &delay)) {
1399 config->mOutputFormat->setInt32("encoder-delay", delay);
1400 }
1401 int padding = 0;
1402 if (msg->findInt32("encoder-padding", &padding)) {
1403 config->mOutputFormat->setInt32("encoder-padding", padding);
1404 }
1405 }
1406
Pawin Vongmasa36653902018-11-15 00:10:25 -08001407 if (config->mDomain & Config::IS_AUDIO) {
Wonsik Kim6f23cfc2021-09-24 05:45:52 -07001408 // set channel-mask
Pawin Vongmasa36653902018-11-15 00:10:25 -08001409 int32_t mask;
1410 if (msg->findInt32(KEY_CHANNEL_MASK, &mask)) {
1411 if (config->mDomain & Config::IS_ENCODER) {
1412 config->mInputFormat->setInt32(KEY_CHANNEL_MASK, mask);
1413 } else {
1414 config->mOutputFormat->setInt32(KEY_CHANNEL_MASK, mask);
1415 }
1416 }
Wonsik Kim6f23cfc2021-09-24 05:45:52 -07001417
1418 // set PCM encoding
1419 int32_t pcmEncoding = kAudioEncodingPcm16bit;
1420 msg->findInt32(KEY_PCM_ENCODING, &pcmEncoding);
1421 if (encoder) {
1422 config->mInputFormat->setInt32("android._config-pcm-encoding", pcmEncoding);
1423 } else {
1424 config->mOutputFormat->setInt32("android._config-pcm-encoding", pcmEncoding);
1425 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001426 }
1427
Wonsik Kim3baecda2021-02-07 22:19:56 -08001428 std::unique_ptr<C2Param> colorTransferRequestParam;
1429 for (std::unique_ptr<C2Param> &param : params) {
1430 if (param->index() == colorAspectsRequestIndex.withStream(0u)) {
1431 ALOGI("found color transfer request param");
1432 colorTransferRequestParam = std::move(param);
1433 }
1434 }
Wonsik Kim3baecda2021-02-07 22:19:56 -08001435
1436 if (colorTransferRequest != 0) {
1437 if (colorTransferRequestParam && *colorTransferRequestParam) {
1438 C2StreamColorAspectsInfo::output *info =
1439 static_cast<C2StreamColorAspectsInfo::output *>(
1440 colorTransferRequestParam.get());
1441 if (!C2Mapper::map(info->transfer, &colorTransferRequest)) {
1442 colorTransferRequest = 0;
1443 }
1444 } else {
1445 colorTransferRequest = 0;
1446 }
1447 config->mInputFormat->setInt32("color-transfer-request", colorTransferRequest);
1448 }
1449
Wonsik Kimd91f3fb2021-02-24 12:35:31 -08001450 if (componentColorFormat != 0 && componentColorFormat != COLOR_FormatSurface) {
1451 // Need to get stride/vstride
1452 uint32_t pixelFormat = PIXEL_FORMAT_UNKNOWN;
1453 if (C2Mapper::mapPixelFormatFrameworkToCodec(componentColorFormat, &pixelFormat)) {
1454 // TODO: retrieve these values without allocating a buffer.
1455 // Currently allocating a buffer is necessary to retrieve the layout.
1456 int64_t blockUsage =
1457 usage.value | C2MemoryUsage::CPU_READ | C2MemoryUsage::CPU_WRITE;
1458 std::shared_ptr<C2GraphicBlock> block = FetchGraphicBlock(
Taehwan Kim2772e1c2022-03-31 17:15:08 +09001459 width, height, componentColorFormat, blockUsage, {comp->getName()});
Wonsik Kimd91f3fb2021-02-24 12:35:31 -08001460 sp<GraphicBlockBuffer> buffer;
1461 if (block) {
1462 buffer = GraphicBlockBuffer::Allocate(
1463 config->mInputFormat,
1464 block,
1465 [](size_t size) -> sp<ABuffer> { return new ABuffer(size); });
1466 } else {
1467 ALOGD("Failed to allocate a graphic block "
1468 "(width=%d height=%d pixelFormat=%u usage=%llx)",
1469 width, height, pixelFormat, (long long)blockUsage);
1470 // This means that byte buffer mode is not supported in this configuration
1471 // anyway. Skip setting stride/vstride to input format.
1472 }
1473 if (buffer) {
1474 sp<ABuffer> imageData = buffer->getImageData();
1475 MediaImage2 *img = nullptr;
1476 if (imageData && imageData->data()
1477 && imageData->size() >= sizeof(MediaImage2)) {
1478 img = (MediaImage2*)imageData->data();
1479 }
1480 if (img && img->mNumPlanes > 0 && img->mType != img->MEDIA_IMAGE_TYPE_UNKNOWN) {
1481 int32_t stride = img->mPlane[0].mRowInc;
1482 config->mInputFormat->setInt32(KEY_STRIDE, stride);
1483 if (img->mNumPlanes > 1 && stride > 0) {
1484 int64_t offsetDelta =
1485 (int64_t)img->mPlane[1].mOffset - (int64_t)img->mPlane[0].mOffset;
1486 if (offsetDelta % stride == 0) {
1487 int32_t vstride = int32_t(offsetDelta / stride);
1488 config->mInputFormat->setInt32(KEY_SLICE_HEIGHT, vstride);
1489 } else {
1490 ALOGD("Cannot report accurate slice height: "
1491 "offsetDelta = %lld stride = %d",
1492 (long long)offsetDelta, stride);
1493 }
1494 }
1495 }
1496 }
1497 }
1498 }
1499
Wonsik Kimec585c32021-10-01 01:11:00 -07001500 if (config->mTunneled) {
1501 config->mOutputFormat->setInt32("android._tunneled", 1);
1502 }
1503
Yushin Cho91873b52021-12-21 04:08:35 -08001504 // Convert an encoding statistics level to corresponding encoding statistics
1505 // kinds
1506 int32_t encodingStatisticsLevel = VIDEO_ENCODING_STATISTICS_LEVEL_NONE;
1507 if ((config->mDomain & Config::IS_ENCODER)
1508 && (config->mDomain & Config::IS_VIDEO)
1509 && msg->findInt32(KEY_VIDEO_ENCODING_STATISTICS_LEVEL, &encodingStatisticsLevel)) {
1510 // Higher level include all the enc stats belong to lower level.
1511 switch (encodingStatisticsLevel) {
1512 // case VIDEO_ENCODING_STATISTICS_LEVEL_2: // reserved for the future level 2
1513 // with more enc stat kinds
1514 // Future extended encoding statistics for the level 2 should be added here
1515 case VIDEO_ENCODING_STATISTICS_LEVEL_1:
Wonsik Kimeebab652022-06-02 13:01:55 -07001516 config->subscribeToConfigUpdate(
1517 comp,
1518 {
1519 C2AndroidStreamAverageBlockQuantizationInfo::output::PARAM_TYPE,
1520 C2StreamPictureTypeInfo::output::PARAM_TYPE,
1521 });
Yushin Cho91873b52021-12-21 04:08:35 -08001522 break;
1523 case VIDEO_ENCODING_STATISTICS_LEVEL_NONE:
1524 break;
1525 }
1526 }
1527 ALOGD("encoding statistics level = %d", encodingStatisticsLevel);
1528
Wonsik Kimd91f3fb2021-02-24 12:35:31 -08001529 ALOGD("setup formats input: %s",
1530 config->mInputFormat->debugString().c_str());
1531 ALOGD("setup formats output: %s",
Pawin Vongmasa36653902018-11-15 00:10:25 -08001532 config->mOutputFormat->debugString().c_str());
1533 return OK;
1534 };
1535 if (tryAndReportOnError(doConfig) != OK) {
1536 return;
1537 }
1538
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001539 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1540 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001541
Houxiang Dai183ca3d2021-02-04 14:20:11 +08001542 config->queryConfiguration(comp);
1543
Songyue Han1e6769b2023-08-30 18:09:27 +00001544 mMetrics = new AMessage;
1545 mChannel->resetBuffersPixelFormat((config->mDomain & Config::IS_ENCODER) ? true : false);
1546
Pawin Vongmasa36653902018-11-15 00:10:25 -08001547 mCallback->onComponentConfigured(config->mInputFormat, config->mOutputFormat);
1548}
1549
1550void CCodec::initiateCreateInputSurface() {
1551 status_t err = [this] {
1552 Mutexed<State>::Locked state(mState);
1553 if (state->get() != ALLOCATED) {
1554 return UNKNOWN_ERROR;
1555 }
1556 // TODO: read it from intf() properly.
1557 if (state->comp->getName().find("encoder") == std::string::npos) {
1558 return INVALID_OPERATION;
1559 }
1560 return OK;
1561 }();
1562 if (err != OK) {
1563 mCallback->onInputSurfaceCreationFailed(err);
1564 return;
1565 }
1566
1567 (new AMessage(kWhatCreateInputSurface, this))->post();
1568}
1569
Lajos Molnar47118272019-01-31 16:28:04 -08001570sp<PersistentSurface> CCodec::CreateOmxInputSurface() {
1571 using namespace android::hardware::media::omx::V1_0;
1572 using namespace android::hardware::media::omx::V1_0::utils;
1573 using namespace android::hardware::graphics::bufferqueue::V1_0::utils;
1574 typedef android::hardware::media::omx::V1_0::Status OmxStatus;
1575 android::sp<IOmx> omx = IOmx::getService();
Sungtak Lee47dcb482022-04-15 10:47:08 -07001576 if (omx == nullptr) {
1577 return nullptr;
1578 }
Lajos Molnar47118272019-01-31 16:28:04 -08001579 typedef android::hardware::graphics::bufferqueue::V1_0::
1580 IGraphicBufferProducer HGraphicBufferProducer;
1581 typedef android::hardware::media::omx::V1_0::
1582 IGraphicBufferSource HGraphicBufferSource;
1583 OmxStatus s;
1584 android::sp<HGraphicBufferProducer> gbp;
1585 android::sp<HGraphicBufferSource> gbs;
Pawin Vongmasa18588322019-05-18 01:52:13 -07001586
Chong Zhangc8ce1d82019-03-27 10:18:38 -07001587 using ::android::hardware::Return;
1588 Return<void> transStatus = omx->createInputSurface(
Lajos Molnar47118272019-01-31 16:28:04 -08001589 [&s, &gbp, &gbs](
1590 OmxStatus status,
1591 const android::sp<HGraphicBufferProducer>& producer,
1592 const android::sp<HGraphicBufferSource>& source) {
1593 s = status;
1594 gbp = producer;
1595 gbs = source;
1596 });
1597 if (transStatus.isOk() && s == OmxStatus::OK) {
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001598 return new PersistentSurface(new H2BGraphicBufferProducer(gbp), gbs);
Lajos Molnar47118272019-01-31 16:28:04 -08001599 }
1600
1601 return nullptr;
1602}
1603
1604sp<PersistentSurface> CCodec::CreateCompatibleInputSurface() {
1605 sp<PersistentSurface> surface(CreateInputSurface());
1606
1607 if (surface == nullptr) {
1608 surface = CreateOmxInputSurface();
1609 }
1610
1611 return surface;
1612}
1613
Pawin Vongmasa36653902018-11-15 00:10:25 -08001614void CCodec::createInputSurface() {
1615 status_t err;
1616 sp<IGraphicBufferProducer> bufferProducer;
1617
Pawin Vongmasa36653902018-11-15 00:10:25 -08001618 sp<AMessage> outputFormat;
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001619 uint64_t usage = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001620 {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001621 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1622 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001623 outputFormat = config->mOutputFormat;
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001624 usage = config->mISConfig ? config->mISConfig->mUsage : 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001625 }
1626
Lajos Molnar47118272019-01-31 16:28:04 -08001627 sp<PersistentSurface> persistentSurface = CreateCompatibleInputSurface();
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001628 sp<hidl::base::V1_0::IBase> hidlTarget = persistentSurface->getHidlTarget();
1629 sp<IInputSurface> hidlInputSurface = IInputSurface::castFrom(hidlTarget);
1630 sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001631
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001632 if (hidlInputSurface) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001633 std::shared_ptr<Codec2Client::InputSurface> inputSurface =
1634 std::make_shared<Codec2Client::InputSurface>(hidlInputSurface);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001635 err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001636 inputSurface));
1637 bufferProducer = inputSurface->getGraphicBufferProducer();
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001638 } else if (gbs) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001639 int32_t width = 0;
1640 (void)outputFormat->findInt32("width", &width);
1641 int32_t height = 0;
1642 (void)outputFormat->findInt32("height", &height);
1643 err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>(
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001644 gbs, width, height, usage));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001645 bufferProducer = persistentSurface->getBufferProducer();
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001646 } else {
1647 ALOGE("Corrupted input surface");
1648 mCallback->onInputSurfaceCreationFailed(UNKNOWN_ERROR);
1649 return;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001650 }
1651
1652 if (err != OK) {
1653 ALOGE("Failed to set up input surface: %d", err);
1654 mCallback->onInputSurfaceCreationFailed(err);
1655 return;
1656 }
1657
Wonsik Kim7b9e6db2021-05-10 13:31:40 -07001658 // Formats can change after setupInputSurface
1659 sp<AMessage> inputFormat;
1660 {
1661 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1662 const std::unique_ptr<Config> &config = *configLocked;
1663 inputFormat = config->mInputFormat;
1664 outputFormat = config->mOutputFormat;
1665 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001666 mCallback->onInputSurfaceCreated(
1667 inputFormat,
1668 outputFormat,
1669 new BufferProducerWrapper(bufferProducer));
1670}
1671
1672status_t CCodec::setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface) {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001673 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1674 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001675 config->mUsingSurface = true;
1676
1677 // we are now using surface - apply default color aspects to input format - as well as
1678 // get dataspace
Wonsik Kim8a6ed372019-12-03 16:05:51 -08001679 bool inputFormatChanged = config->updateFormats(Config::IS_INPUT);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001680
1681 // configure dataspace
1682 static_assert(sizeof(int32_t) == sizeof(android_dataspace), "dataspace size mismatch");
Wonsik Kim66b19552021-08-02 16:07:49 -07001683
1684 // The output format contains app-configured color aspects, and the input format
1685 // has the default color aspects. Use the default for the unspecified params.
1686 ColorAspects inputColorAspects, colorAspects;
1687 getColorAspectsFromFormat(config->mOutputFormat, colorAspects);
1688 getColorAspectsFromFormat(config->mInputFormat, inputColorAspects);
1689 if (colorAspects.mRange == ColorAspects::RangeUnspecified) {
1690 colorAspects.mRange = inputColorAspects.mRange;
1691 }
1692 if (colorAspects.mPrimaries == ColorAspects::PrimariesUnspecified) {
1693 colorAspects.mPrimaries = inputColorAspects.mPrimaries;
1694 }
1695 if (colorAspects.mTransfer == ColorAspects::TransferUnspecified) {
1696 colorAspects.mTransfer = inputColorAspects.mTransfer;
1697 }
1698 if (colorAspects.mMatrixCoeffs == ColorAspects::MatrixUnspecified) {
1699 colorAspects.mMatrixCoeffs = inputColorAspects.mMatrixCoeffs;
1700 }
1701 android_dataspace dataSpace = getDataSpaceForColorAspects(
1702 colorAspects, /* mayExtend = */ false);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001703 surface->setDataSpace(dataSpace);
Wonsik Kim66b19552021-08-02 16:07:49 -07001704 setColorAspectsIntoFormat(colorAspects, config->mInputFormat, /* force = */ true);
1705 config->mInputFormat->setInt32("android._dataspace", int32_t(dataSpace));
1706
1707 ALOGD("input format %s to %s",
1708 inputFormatChanged ? "changed" : "unchanged",
1709 config->mInputFormat->debugString().c_str());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001710
1711 status_t err = mChannel->setInputSurface(surface);
1712 if (err != OK) {
1713 // undo input format update
1714 config->mUsingSurface = false;
Wonsik Kim8a6ed372019-12-03 16:05:51 -08001715 (void)config->updateFormats(Config::IS_INPUT);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001716 return err;
1717 }
1718 config->mInputSurface = surface;
1719
1720 if (config->mISConfig) {
1721 surface->configure(*config->mISConfig);
1722 } else {
1723 ALOGD("ISConfig: no configuration");
1724 }
1725
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001726 return OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001727}
1728
1729void CCodec::initiateSetInputSurface(const sp<PersistentSurface> &surface) {
1730 sp<AMessage> msg = new AMessage(kWhatSetInputSurface, this);
1731 msg->setObject("surface", surface);
1732 msg->post();
1733}
1734
1735void CCodec::setInputSurface(const sp<PersistentSurface> &surface) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001736 sp<AMessage> outputFormat;
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001737 uint64_t usage = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001738 {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001739 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1740 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001741 outputFormat = config->mOutputFormat;
Wonsik Kim9eac4d12019-05-23 12:58:48 -07001742 usage = config->mISConfig ? config->mISConfig->mUsage : 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001743 }
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001744 sp<hidl::base::V1_0::IBase> hidlTarget = surface->getHidlTarget();
1745 sp<IInputSurface> inputSurface = IInputSurface::castFrom(hidlTarget);
1746 sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget);
1747 if (inputSurface) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001748 status_t err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>(
1749 std::make_shared<Codec2Client::InputSurface>(inputSurface)));
1750 if (err != OK) {
1751 ALOGE("Failed to set up input surface: %d", err);
1752 mCallback->onInputSurfaceDeclined(err);
1753 return;
1754 }
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001755 } else if (gbs) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001756 int32_t width = 0;
1757 (void)outputFormat->findInt32("width", &width);
1758 int32_t height = 0;
1759 (void)outputFormat->findInt32("height", &height);
1760 status_t err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>(
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001761 gbs, width, height, usage));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001762 if (err != OK) {
1763 ALOGE("Failed to set up input surface: %d", err);
1764 mCallback->onInputSurfaceDeclined(err);
1765 return;
1766 }
Wonsik Kim9917d4a2019-10-24 12:56:38 -07001767 } else {
1768 ALOGE("Failed to set input surface: Corrupted surface.");
1769 mCallback->onInputSurfaceDeclined(UNKNOWN_ERROR);
1770 return;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001771 }
Wonsik Kim7b9e6db2021-05-10 13:31:40 -07001772 // Formats can change after setupInputSurface
1773 sp<AMessage> inputFormat;
1774 {
1775 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1776 const std::unique_ptr<Config> &config = *configLocked;
1777 inputFormat = config->mInputFormat;
1778 outputFormat = config->mOutputFormat;
1779 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001780 mCallback->onInputSurfaceAccepted(inputFormat, outputFormat);
1781}
1782
1783void CCodec::initiateStart() {
1784 auto setStarting = [this] {
1785 Mutexed<State>::Locked state(mState);
1786 if (state->get() != ALLOCATED) {
1787 return UNKNOWN_ERROR;
1788 }
1789 state->set(STARTING);
1790 return OK;
1791 };
1792 if (tryAndReportOnError(setStarting) != OK) {
1793 return;
1794 }
1795
1796 (new AMessage(kWhatStart, this))->post();
1797}
1798
1799void CCodec::start() {
1800 std::shared_ptr<Codec2Client::Component> comp;
1801 auto checkStarting = [this, &comp] {
1802 Mutexed<State>::Locked state(mState);
1803 if (state->get() != STARTING) {
1804 return UNKNOWN_ERROR;
1805 }
1806 comp = state->comp;
1807 return OK;
1808 };
1809 if (tryAndReportOnError(checkStarting) != OK) {
1810 return;
1811 }
1812
1813 c2_status_t err = comp->start();
1814 if (err != C2_OK) {
1815 mCallback->onError(toStatusT(err, C2_OPERATION_Component_start),
1816 ACTION_CODE_FATAL);
1817 return;
1818 }
Wonsik Kimd55ed3b2023-06-22 14:42:17 -07001819
1820 // clear the deadline after the component starts
1821 setDeadline(TimePoint::max(), 0ms, "none");
1822
Pawin Vongmasa36653902018-11-15 00:10:25 -08001823 sp<AMessage> inputFormat;
1824 sp<AMessage> outputFormat;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001825 status_t err2 = OK;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001826 bool buffersBoundToCodec = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001827 {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001828 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1829 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001830 inputFormat = config->mInputFormat;
Wonsik Kim274c8322020-02-28 10:42:21 -08001831 // start triggers format dup
1832 outputFormat = config->mOutputFormat = config->mOutputFormat->dup();
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001833 if (config->mInputSurface) {
1834 err2 = config->mInputSurface->start();
Wonsik Kim673dd192021-01-29 14:58:12 -08001835 config->mInputSurfaceDataspace = config->mInputSurface->getDataspace();
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001836 }
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001837 buffersBoundToCodec = config->mBuffersBoundToCodec;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001838 }
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001839 if (err2 != OK) {
1840 mCallback->onError(err2, ACTION_CODE_FATAL);
1841 return;
1842 }
Arun Johnson106fe7a2023-04-26 17:49:43 +00001843
Wonsik Kimfb7a7672019-12-27 17:13:33 -08001844 err2 = mChannel->start(inputFormat, outputFormat, buffersBoundToCodec);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001845 if (err2 != OK) {
1846 mCallback->onError(err2, ACTION_CODE_FATAL);
1847 return;
1848 }
1849
1850 auto setRunning = [this] {
1851 Mutexed<State>::Locked state(mState);
1852 if (state->get() != STARTING) {
1853 return UNKNOWN_ERROR;
1854 }
1855 state->set(RUNNING);
1856 return OK;
1857 };
1858 if (tryAndReportOnError(setRunning) != OK) {
1859 return;
1860 }
Arun Johnson5997bb02022-04-01 19:35:44 +00001861
Wonsik Kim34b28b42022-05-20 15:49:32 -07001862 // preparation of input buffers may not succeed due to the lack of
1863 // memory; returning correct error code (NO_MEMORY) as an error allows
1864 // MediaCodec to try reclaim and restart codec gracefully.
1865 std::map<size_t, sp<MediaCodecBuffer>> clientInputBuffers;
1866 err2 = mChannel->prepareInitialInputBuffers(&clientInputBuffers);
1867 if (err2 != OK) {
1868 ALOGE("Initial preparation for Input Buffers failed");
1869 mCallback->onError(err2, ACTION_CODE_FATAL);
1870 return;
1871 }
1872
Pawin Vongmasa36653902018-11-15 00:10:25 -08001873 mCallback->onStartCompleted();
1874
Wonsik Kim34b28b42022-05-20 15:49:32 -07001875 mChannel->requestInitialInputBuffers(std::move(clientInputBuffers));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001876}
1877
1878void CCodec::initiateShutdown(bool keepComponentAllocated) {
1879 if (keepComponentAllocated) {
1880 initiateStop();
1881 } else {
1882 initiateRelease();
1883 }
1884}
1885
1886void CCodec::initiateStop() {
1887 {
1888 Mutexed<State>::Locked state(mState);
1889 if (state->get() == ALLOCATED
1890 || state->get() == RELEASED
1891 || state->get() == STOPPING
1892 || state->get() == RELEASING) {
1893 // We're already stopped, released, or doing it right now.
1894 state.unlock();
1895 mCallback->onStopCompleted();
1896 state.lock();
1897 return;
1898 }
1899 state->set(STOPPING);
1900 }
Wonsik Kim936a89c2020-05-08 16:07:50 -07001901 mChannel->reset();
Sungtak Lee99144332023-01-26 11:03:14 +00001902 bool pushBlankBuffer = mConfig.lock().get()->mPushBlankBuffersOnStop;
1903 sp<AMessage> stopMessage(new AMessage(kWhatStop, this));
1904 stopMessage->setInt32("pushBlankBuffer", pushBlankBuffer);
1905 stopMessage->post();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001906}
1907
Sungtak Lee99144332023-01-26 11:03:14 +00001908void CCodec::stop(bool pushBlankBuffer) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001909 std::shared_ptr<Codec2Client::Component> comp;
1910 {
1911 Mutexed<State>::Locked state(mState);
1912 if (state->get() == RELEASING) {
1913 state.unlock();
1914 // We're already stopped or release is in progress.
1915 mCallback->onStopCompleted();
1916 state.lock();
1917 return;
1918 } else if (state->get() != STOPPING) {
1919 state.unlock();
1920 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1921 state.lock();
1922 return;
1923 }
1924 comp = state->comp;
1925 }
1926 status_t err = comp->stop();
Sungtak Lee99144332023-01-26 11:03:14 +00001927 mChannel->stopUseOutputSurface(pushBlankBuffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001928 if (err != C2_OK) {
1929 // TODO: convert err into status_t
1930 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
1931 }
1932
1933 {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001934 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1935 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001936 if (config->mInputSurface) {
1937 config->mInputSurface->disconnect();
1938 config->mInputSurface = nullptr;
Wonsik Kim673dd192021-01-29 14:58:12 -08001939 config->mInputSurfaceDataspace = HAL_DATASPACE_UNKNOWN;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001940 }
1941 }
1942 {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001943 Mutexed<State>::Locked state(mState);
1944 if (state->get() == STOPPING) {
1945 state->set(ALLOCATED);
1946 }
1947 }
1948 mCallback->onStopCompleted();
1949}
1950
1951void CCodec::initiateRelease(bool sendCallback /* = true */) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001952 bool clearInputSurfaceIfNeeded = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001953 {
1954 Mutexed<State>::Locked state(mState);
1955 if (state->get() == RELEASED || state->get() == RELEASING) {
1956 // We're already released or doing it right now.
1957 if (sendCallback) {
1958 state.unlock();
1959 mCallback->onReleaseCompleted();
1960 state.lock();
1961 }
1962 return;
1963 }
1964 if (state->get() == ALLOCATING) {
1965 state->set(RELEASING);
1966 // With the altered state allocate() would fail and clean up.
1967 if (sendCallback) {
1968 state.unlock();
1969 mCallback->onReleaseCompleted();
1970 state.lock();
1971 }
1972 return;
1973 }
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001974 if (state->get() == STARTING
1975 || state->get() == RUNNING
1976 || state->get() == STOPPING) {
1977 // Input surface may have been started, so clean up is needed.
1978 clearInputSurfaceIfNeeded = true;
1979 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001980 state->set(RELEASING);
1981 }
1982
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001983 if (clearInputSurfaceIfNeeded) {
Wonsik Kim155d5cb2019-10-09 12:49:49 -07001984 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
1985 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001986 if (config->mInputSurface) {
1987 config->mInputSurface->disconnect();
1988 config->mInputSurface = nullptr;
Wonsik Kim673dd192021-01-29 14:58:12 -08001989 config->mInputSurfaceDataspace = HAL_DATASPACE_UNKNOWN;
Pawin Vongmasa1f213362019-01-24 06:59:16 -08001990 }
1991 }
1992
Wonsik Kim936a89c2020-05-08 16:07:50 -07001993 mChannel->reset();
Sungtak Lee99144332023-01-26 11:03:14 +00001994 bool pushBlankBuffer = mConfig.lock().get()->mPushBlankBuffersOnStop;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001995 // thiz holds strong ref to this while the thread is running.
1996 sp<CCodec> thiz(this);
Sungtak Lee99144332023-01-26 11:03:14 +00001997 std::thread([thiz, sendCallback, pushBlankBuffer]
1998 { thiz->release(sendCallback, pushBlankBuffer); }).detach();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001999}
2000
Sungtak Lee99144332023-01-26 11:03:14 +00002001void CCodec::release(bool sendCallback, bool pushBlankBuffer) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002002 std::shared_ptr<Codec2Client::Component> comp;
2003 {
2004 Mutexed<State>::Locked state(mState);
2005 if (state->get() == RELEASED) {
2006 if (sendCallback) {
2007 state.unlock();
2008 mCallback->onReleaseCompleted();
2009 state.lock();
2010 }
2011 return;
2012 }
2013 comp = state->comp;
2014 }
2015 comp->release();
Sungtak Lee99144332023-01-26 11:03:14 +00002016 mChannel->stopUseOutputSurface(pushBlankBuffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002017
2018 {
2019 Mutexed<State>::Locked state(mState);
2020 state->set(RELEASED);
2021 state->comp.reset();
2022 }
Wonsik Kim936a89c2020-05-08 16:07:50 -07002023 (new AMessage(kWhatRelease, this))->post();
Pawin Vongmasa36653902018-11-15 00:10:25 -08002024 if (sendCallback) {
2025 mCallback->onReleaseCompleted();
2026 }
2027}
2028
2029status_t CCodec::setSurface(const sp<Surface> &surface) {
Sungtak Lee99144332023-01-26 11:03:14 +00002030 bool pushBlankBuffer = false;
Wonsik Kim75e22f42021-04-14 23:34:51 -07002031 {
2032 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2033 const std::unique_ptr<Config> &config = *configLocked;
Houxiang Dai7ab7ee62021-09-30 16:08:51 +08002034 sp<ANativeWindow> nativeWindow = static_cast<ANativeWindow *>(surface.get());
2035 status_t err = OK;
2036
Wonsik Kim75e22f42021-04-14 23:34:51 -07002037 if (config->mTunneled && config->mSidebandHandle != nullptr) {
Houxiang Dai7ab7ee62021-09-30 16:08:51 +08002038 err = native_window_set_sideband_stream(
Wonsik Kim75e22f42021-04-14 23:34:51 -07002039 nativeWindow.get(),
2040 const_cast<native_handle_t *>(config->mSidebandHandle->handle()));
2041 if (err != OK) {
2042 ALOGE("NativeWindow(%p) native_window_set_sideband_stream(%p) failed! (err %d).",
2043 nativeWindow.get(), config->mSidebandHandle->handle(), err);
2044 return err;
2045 }
Houxiang Dai7ab7ee62021-09-30 16:08:51 +08002046 } else {
2047 // Explicitly reset the sideband handle of the window for
2048 // non-tunneled video in case the window was previously used
2049 // for a tunneled video playback.
2050 err = native_window_set_sideband_stream(nativeWindow.get(), nullptr);
2051 if (err != OK) {
2052 ALOGE("native_window_set_sideband_stream(nullptr) failed! (err %d).", err);
2053 return err;
2054 }
ted.sun765db4d2020-06-23 14:03:41 +08002055 }
Sungtak Lee99144332023-01-26 11:03:14 +00002056 pushBlankBuffer = config->mPushBlankBuffersOnStop;
ted.sun765db4d2020-06-23 14:03:41 +08002057 }
Sungtak Lee99144332023-01-26 11:03:14 +00002058 return mChannel->setSurface(surface, pushBlankBuffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002059}
2060
2061void CCodec::signalFlush() {
2062 status_t err = [this] {
2063 Mutexed<State>::Locked state(mState);
2064 if (state->get() == FLUSHED) {
2065 return ALREADY_EXISTS;
2066 }
2067 if (state->get() != RUNNING) {
2068 return UNKNOWN_ERROR;
2069 }
2070 state->set(FLUSHING);
2071 return OK;
2072 }();
2073 switch (err) {
2074 case ALREADY_EXISTS:
2075 mCallback->onFlushCompleted();
2076 return;
2077 case OK:
2078 break;
2079 default:
2080 mCallback->onError(err, ACTION_CODE_FATAL);
2081 return;
2082 }
2083
2084 mChannel->stop();
2085 (new AMessage(kWhatFlush, this))->post();
2086}
2087
2088void CCodec::flush() {
2089 std::shared_ptr<Codec2Client::Component> comp;
2090 auto checkFlushing = [this, &comp] {
2091 Mutexed<State>::Locked state(mState);
2092 if (state->get() != FLUSHING) {
2093 return UNKNOWN_ERROR;
2094 }
2095 comp = state->comp;
2096 return OK;
2097 };
2098 if (tryAndReportOnError(checkFlushing) != OK) {
2099 return;
2100 }
2101
2102 std::list<std::unique_ptr<C2Work>> flushedWork;
2103 c2_status_t err = comp->flush(C2Component::FLUSH_COMPONENT, &flushedWork);
2104 {
2105 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
2106 flushedWork.splice(flushedWork.end(), *queue);
2107 }
2108 if (err != C2_OK) {
2109 // TODO: convert err into status_t
2110 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2111 }
2112
2113 mChannel->flush(flushedWork);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002114
2115 {
2116 Mutexed<State>::Locked state(mState);
Iris Changce521ee2019-07-05 16:18:54 +08002117 if (state->get() == FLUSHING) {
2118 state->set(FLUSHED);
2119 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002120 }
2121 mCallback->onFlushCompleted();
2122}
2123
2124void CCodec::signalResume() {
Wonsik Kime75a5da2020-02-14 17:29:03 -08002125 std::shared_ptr<Codec2Client::Component> comp;
2126 auto setResuming = [this, &comp] {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002127 Mutexed<State>::Locked state(mState);
2128 if (state->get() != FLUSHED) {
2129 return UNKNOWN_ERROR;
2130 }
2131 state->set(RESUMING);
Wonsik Kime75a5da2020-02-14 17:29:03 -08002132 comp = state->comp;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002133 return OK;
2134 };
2135 if (tryAndReportOnError(setResuming) != OK) {
2136 return;
2137 }
2138
Wonsik Kime75a5da2020-02-14 17:29:03 -08002139 {
2140 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2141 const std::unique_ptr<Config> &config = *configLocked;
Harish Mahendrakar8c537502021-02-23 21:20:22 -08002142 sp<AMessage> outputFormat = config->mOutputFormat;
Wonsik Kime75a5da2020-02-14 17:29:03 -08002143 config->queryConfiguration(comp);
Harish Mahendrakar8c537502021-02-23 21:20:22 -08002144 RevertOutputFormatIfNeeded(outputFormat, config->mOutputFormat);
Wonsik Kime75a5da2020-02-14 17:29:03 -08002145 }
2146
Arun Johnson106fe7a2023-04-26 17:49:43 +00002147 std::map<size_t, sp<MediaCodecBuffer>> clientInputBuffers;
Arun Johnson326166e2023-07-28 19:11:52 +00002148 status_t err = mChannel->prepareInitialInputBuffers(&clientInputBuffers, true);
Arun Johnson106fe7a2023-04-26 17:49:43 +00002149 if (err != OK) {
2150 if (err == NO_MEMORY) {
2151 // NO_MEMORY happens here when all the buffers are still
2152 // with the codec. That is not an error as it is momentarily
2153 // and the buffers are send to the client as soon as the codec
2154 // releases them
2155 ALOGI("Resuming with all input buffers still with codec");
2156 } else {
2157 ALOGE("Resume request for Input Buffers failed");
2158 mCallback->onError(err, ACTION_CODE_FATAL);
2159 return;
2160 }
2161 }
2162
2163 // channel start should be called after prepareInitialBuffers
2164 // Calling before can cause a failure during prepare when
2165 // buffers are sent to the client before preparation from onWorkDone
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002166 (void)mChannel->start(nullptr, nullptr, [&]{
2167 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2168 const std::unique_ptr<Config> &config = *configLocked;
2169 return config->mBuffersBoundToCodec;
2170 }());
Pawin Vongmasa36653902018-11-15 00:10:25 -08002171 {
2172 Mutexed<State>::Locked state(mState);
2173 if (state->get() != RESUMING) {
2174 state.unlock();
2175 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2176 state.lock();
2177 return;
2178 }
2179 state->set(RUNNING);
2180 }
2181
Wonsik Kim34b28b42022-05-20 15:49:32 -07002182 mChannel->requestInitialInputBuffers(std::move(clientInputBuffers));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002183}
2184
Wonsik Kimaa484ac2019-02-13 16:54:02 -08002185void CCodec::signalSetParameters(const sp<AMessage> &msg) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002186 std::shared_ptr<Codec2Client::Component> comp;
2187 auto checkState = [this, &comp] {
2188 Mutexed<State>::Locked state(mState);
2189 if (state->get() == RELEASED) {
2190 return INVALID_OPERATION;
2191 }
2192 comp = state->comp;
2193 return OK;
2194 };
2195 if (tryAndReportOnError(checkState) != OK) {
2196 return;
2197 }
2198
Wonsik Kimaa484ac2019-02-13 16:54:02 -08002199 // NOTE: We used to ignore "bitrate" at setParameters; replicate
2200 // the behavior here.
2201 sp<AMessage> params = msg;
2202 int32_t bitrate;
2203 if (params->findInt32(KEY_BIT_RATE, &bitrate)) {
2204 params = msg->dup();
2205 params->removeEntryAt(params->findEntryByName(KEY_BIT_RATE));
2206 }
2207
Houxiang Dai5a97b472021-03-22 17:56:04 +08002208 int32_t syncId = 0;
2209 if (params->findInt32("audio-hw-sync", &syncId)
2210 || params->findInt32("hw-av-sync-id", &syncId)) {
2211 configureTunneledVideoPlayback(comp, nullptr, params);
2212 }
2213
Wonsik Kim155d5cb2019-10-09 12:49:49 -07002214 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2215 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002216
2217 /**
2218 * Handle input surface parameters
2219 */
2220 if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))
Wonsik Kim8a6ed372019-12-03 16:05:51 -08002221 && (config->mDomain & Config::IS_ENCODER)
2222 && config->mInputSurface && config->mISConfig) {
Chong Zhang038e8f82019-02-06 19:05:14 -08002223 (void)params->findInt64(PARAMETER_KEY_OFFSET_TIME, &config->mISConfig->mTimeOffsetUs);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002224
2225 if (params->findInt64("skip-frames-before", &config->mISConfig->mStartAtUs)) {
2226 config->mISConfig->mStopped = false;
2227 } else if (params->findInt64("stop-time-us", &config->mISConfig->mStopAtUs)) {
2228 config->mISConfig->mStopped = true;
2229 }
2230
2231 int32_t value;
Chong Zhang038e8f82019-02-06 19:05:14 -08002232 if (params->findInt32(PARAMETER_KEY_SUSPEND, &value)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002233 config->mISConfig->mSuspended = value;
2234 config->mISConfig->mSuspendAtUs = -1;
Chong Zhang038e8f82019-02-06 19:05:14 -08002235 (void)params->findInt64(PARAMETER_KEY_SUSPEND_TIME, &config->mISConfig->mSuspendAtUs);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002236 }
2237
2238 (void)config->mInputSurface->configure(*config->mISConfig);
2239 if (config->mISConfig->mStopped) {
2240 config->mInputFormat->setInt64(
2241 "android._stop-time-offset-us", config->mISConfig->mInputDelayUs);
2242 }
2243 }
2244
2245 std::vector<std::unique_ptr<C2Param>> configUpdate;
2246 (void)config->getConfigUpdateFromSdkParams(
2247 comp, params, Config::IS_PARAM, C2_MAY_BLOCK, &configUpdate);
2248 // Prefer to pass parameters to the buffer channel, so they can be synchronized with the frames.
2249 // Parameter synchronization is not defined when using input surface. For now, route
2250 // these directly to the component.
2251 if (config->mInputSurface == nullptr
2252 && (property_get_bool("debug.stagefright.ccodec_delayed_params", false)
2253 || comp->getName().find("c2.android.") == 0)) {
2254 mChannel->setParameters(configUpdate);
2255 } else {
Wonsik Kim3b4349a2020-11-10 11:54:15 -08002256 sp<AMessage> outputFormat = config->mOutputFormat;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002257 (void)config->setParameters(comp, configUpdate, C2_MAY_BLOCK);
Wonsik Kim3b4349a2020-11-10 11:54:15 -08002258 RevertOutputFormatIfNeeded(outputFormat, config->mOutputFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002259 }
2260}
2261
2262void CCodec::signalEndOfInputStream() {
2263 mCallback->onSignaledInputEOS(mChannel->signalEndOfInputStream());
2264}
2265
2266void CCodec::signalRequestIDRFrame() {
2267 std::shared_ptr<Codec2Client::Component> comp;
2268 {
2269 Mutexed<State>::Locked state(mState);
2270 if (state->get() == RELEASED) {
2271 ALOGD("no IDR request sent since component is released");
2272 return;
2273 }
2274 comp = state->comp;
2275 }
2276 ALOGV("request IDR");
Wonsik Kim155d5cb2019-10-09 12:49:49 -07002277 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2278 const std::unique_ptr<Config> &config = *configLocked;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002279 std::vector<std::unique_ptr<C2Param>> params;
2280 params.push_back(
2281 std::make_unique<C2StreamRequestSyncFrameTuning::output>(0u, true));
2282 config->setParameters(comp, params, C2_MAY_BLOCK);
2283}
2284
Wonsik Kim874ad382021-03-12 09:59:36 -08002285status_t CCodec::querySupportedParameters(std::vector<std::string> *names) {
2286 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2287 const std::unique_ptr<Config> &config = *configLocked;
2288 return config->querySupportedParameters(names);
2289}
2290
2291status_t CCodec::describeParameter(
2292 const std::string &name, CodecParameterDescriptor *desc) {
2293 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2294 const std::unique_ptr<Config> &config = *configLocked;
2295 return config->describe(name, desc);
2296}
2297
2298status_t CCodec::subscribeToParameters(const std::vector<std::string> &names) {
2299 std::shared_ptr<Codec2Client::Component> comp = mState.lock()->comp;
2300 if (!comp) {
2301 return INVALID_OPERATION;
2302 }
2303 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2304 const std::unique_ptr<Config> &config = *configLocked;
2305 return config->subscribeToVendorConfigUpdate(comp, names);
2306}
2307
2308status_t CCodec::unsubscribeFromParameters(const std::vector<std::string> &names) {
2309 std::shared_ptr<Codec2Client::Component> comp = mState.lock()->comp;
2310 if (!comp) {
2311 return INVALID_OPERATION;
2312 }
2313 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2314 const std::unique_ptr<Config> &config = *configLocked;
2315 return config->unsubscribeFromVendorConfigUpdate(comp, names);
2316}
2317
Wonsik Kimab34ed62019-01-31 15:28:46 -08002318void CCodec::onWorkDone(std::list<std::unique_ptr<C2Work>> &workItems) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002319 if (!workItems.empty()) {
Wonsik Kimab34ed62019-01-31 15:28:46 -08002320 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
2321 queue->splice(queue->end(), workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002322 }
2323 (new AMessage(kWhatWorkDone, this))->post();
2324}
2325
Wonsik Kimab34ed62019-01-31 15:28:46 -08002326void CCodec::onInputBufferDone(uint64_t frameIndex, size_t arrayIndex) {
2327 mChannel->onInputBufferDone(frameIndex, arrayIndex);
Wonsik Kim4f3314d2019-03-26 17:00:34 -07002328 if (arrayIndex == 0) {
2329 // We always put no more than one buffer per work, if we use an input surface.
Wonsik Kim155d5cb2019-10-09 12:49:49 -07002330 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2331 const std::unique_ptr<Config> &config = *configLocked;
Wonsik Kim4f3314d2019-03-26 17:00:34 -07002332 if (config->mInputSurface) {
2333 config->mInputSurface->onInputBufferDone(frameIndex);
2334 }
2335 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002336}
2337
2338void CCodec::onMessageReceived(const sp<AMessage> &msg) {
2339 TimePoint now = std::chrono::steady_clock::now();
2340 CCodecWatchdog::getInstance()->watch(this);
2341 switch (msg->what()) {
2342 case kWhatAllocate: {
2343 // C2ComponentStore::createComponent() should return within 100ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002344 setDeadline(now, 1500ms, "allocate");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002345 sp<RefBase> obj;
2346 CHECK(msg->findObject("codecInfo", &obj));
2347 allocate((MediaCodecInfo *)obj.get());
2348 break;
2349 }
2350 case kWhatConfigure: {
2351 // C2Component::commit_sm() should return within 5ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002352 setDeadline(now, 1500ms, "configure");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002353 sp<AMessage> format;
2354 CHECK(msg->findMessage("format", &format));
2355 configure(format);
2356 break;
2357 }
2358 case kWhatStart: {
2359 // C2Component::start() should return within 500ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002360 setDeadline(now, 1500ms, "start");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002361 start();
2362 break;
2363 }
2364 case kWhatStop: {
2365 // C2Component::stop() should return within 500ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002366 setDeadline(now, 1500ms, "stop");
Sungtak Lee99144332023-01-26 11:03:14 +00002367 int32_t pushBlankBuffer;
2368 if (!msg->findInt32("pushBlankBuffer", &pushBlankBuffer)) {
2369 pushBlankBuffer = 0;
2370 }
2371 stop(static_cast<bool>(pushBlankBuffer));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002372 break;
2373 }
2374 case kWhatFlush: {
2375 // C2Component::flush_sm() should return within 5ms.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002376 setDeadline(now, 1500ms, "flush");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002377 flush();
2378 break;
2379 }
Wonsik Kim936a89c2020-05-08 16:07:50 -07002380 case kWhatRelease: {
2381 mChannel->release();
2382 mClient.reset();
2383 mClientListener.reset();
2384 break;
2385 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002386 case kWhatCreateInputSurface: {
2387 // Surface operations may be briefly blocking.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002388 setDeadline(now, 1500ms, "createInputSurface");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002389 createInputSurface();
2390 break;
2391 }
2392 case kWhatSetInputSurface: {
2393 // Surface operations may be briefly blocking.
Wonsik Kim9ee5a7c2019-06-17 11:33:24 -07002394 setDeadline(now, 1500ms, "setInputSurface");
Pawin Vongmasa36653902018-11-15 00:10:25 -08002395 sp<RefBase> obj;
2396 CHECK(msg->findObject("surface", &obj));
2397 sp<PersistentSurface> surface(static_cast<PersistentSurface *>(obj.get()));
2398 setInputSurface(surface);
2399 break;
2400 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002401 case kWhatWorkDone: {
2402 std::unique_ptr<C2Work> work;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002403 bool shouldPost = false;
2404 {
2405 Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue);
2406 if (queue->empty()) {
2407 break;
2408 }
2409 work.swap(queue->front());
2410 queue->pop_front();
2411 shouldPost = !queue->empty();
2412 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002413 if (shouldPost) {
2414 (new AMessage(kWhatWorkDone, this))->post();
2415 }
2416
Pawin Vongmasa36653902018-11-15 00:10:25 -08002417 // handle configuration changes in work done
Wonsik Kim1f5063d2021-05-03 15:41:17 -07002418 std::shared_ptr<const C2StreamInitDataInfo::output> initData;
Wonsik Kim75e22f42021-04-14 23:34:51 -07002419 sp<AMessage> outputFormat = nullptr;
2420 {
2421 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2422 const std::unique_ptr<Config> &config = *configLocked;
2423 Config::Watcher<C2StreamInitDataInfo::output> initDataWatcher =
2424 config->watch<C2StreamInitDataInfo::output>();
2425 if (!work->worklets.empty()
2426 && (work->worklets.front()->output.flags
2427 & C2FrameData::FLAG_DISCARD_FRAME) == 0) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002428
Wonsik Kim75e22f42021-04-14 23:34:51 -07002429 // copy buffer info to config
2430 std::vector<std::unique_ptr<C2Param>> updates;
2431 for (const std::unique_ptr<C2Param> &param
2432 : work->worklets.front()->output.configUpdate) {
2433 updates.push_back(C2Param::Copy(*param));
2434 }
2435 unsigned stream = 0;
2436 std::vector<std::shared_ptr<C2Buffer>> &outputBuffers =
2437 work->worklets.front()->output.buffers;
2438 for (const std::shared_ptr<C2Buffer> &buf : outputBuffers) {
2439 for (const std::shared_ptr<const C2Info> &info : buf->info()) {
2440 // move all info into output-stream #0 domain
2441 updates.emplace_back(
2442 C2Param::CopyAsStream(*info, true /* output */, stream));
2443 }
2444
2445 const std::vector<C2ConstGraphicBlock> blocks = buf->data().graphicBlocks();
2446 // for now only do the first block
2447 if (!blocks.empty()) {
2448 // ALOGV("got output buffer with crop %u,%u+%u,%u and size %u,%u",
2449 // block.crop().left, block.crop().top,
2450 // block.crop().width, block.crop().height,
2451 // block.width(), block.height());
2452 const C2ConstGraphicBlock &block = blocks[0];
2453 updates.emplace_back(new C2StreamCropRectInfo::output(
2454 stream, block.crop()));
Wonsik Kim75e22f42021-04-14 23:34:51 -07002455 }
2456 ++stream;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002457 }
George Burgess IVc813a592020-02-22 22:54:44 -08002458
Wonsik Kim75e22f42021-04-14 23:34:51 -07002459 sp<AMessage> oldFormat = config->mOutputFormat;
2460 config->updateConfiguration(updates, config->mOutputDomain);
2461 RevertOutputFormatIfNeeded(oldFormat, config->mOutputFormat);
Pawin Vongmasa36653902018-11-15 00:10:25 -08002462
Wonsik Kim75e22f42021-04-14 23:34:51 -07002463 // copy standard infos to graphic buffers if not already present (otherwise, we
2464 // may overwrite the actual intermediate value with a final value)
2465 stream = 0;
2466 const static C2Param::Index stdGfxInfos[] = {
2467 C2StreamRotationInfo::output::PARAM_TYPE,
2468 C2StreamColorAspectsInfo::output::PARAM_TYPE,
2469 C2StreamDataSpaceInfo::output::PARAM_TYPE,
2470 C2StreamHdrStaticInfo::output::PARAM_TYPE,
Taehwan Kim2d222b82022-05-12 14:19:26 +09002471 C2StreamHdr10PlusInfo::output::PARAM_TYPE, // will be deprecated
2472 C2StreamHdrDynamicMetadataInfo::output::PARAM_TYPE,
Wonsik Kim75e22f42021-04-14 23:34:51 -07002473 C2StreamPixelAspectRatioInfo::output::PARAM_TYPE,
2474 C2StreamSurfaceScalingInfo::output::PARAM_TYPE
2475 };
2476 for (const std::shared_ptr<C2Buffer> &buf : outputBuffers) {
2477 if (buf->data().graphicBlocks().size()) {
2478 for (C2Param::Index ix : stdGfxInfos) {
2479 if (!buf->hasInfo(ix)) {
2480 const C2Param *param =
2481 config->getConfigParameterValue(ix.withStream(stream));
2482 if (param) {
2483 std::shared_ptr<C2Param> info(C2Param::Copy(*param));
2484 buf->setInfo(std::static_pointer_cast<C2Info>(info));
2485 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002486 }
2487 }
2488 }
Wonsik Kim75e22f42021-04-14 23:34:51 -07002489 ++stream;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002490 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002491 }
Wonsik Kim75e22f42021-04-14 23:34:51 -07002492 if (config->mInputSurface) {
Brijesh Patelab463672020-11-25 15:38:28 +05302493 if (work->worklets.empty()
2494 || !work->worklets.back()
2495 || (work->worklets.back()->output.flags
2496 & C2FrameData::FLAG_INCOMPLETE) == 0) {
2497 config->mInputSurface->onInputBufferDone(work->input.ordinal.frameIndex);
2498 }
Wonsik Kim75e22f42021-04-14 23:34:51 -07002499 }
2500 if (initDataWatcher.hasChanged()) {
Wonsik Kim1f5063d2021-05-03 15:41:17 -07002501 initData = initDataWatcher.update();
2502 AmendOutputFormatWithCodecSpecificData(
2503 initData->m.value, initData->flexCount(), config->mCodingMediaType,
2504 config->mOutputFormat);
Wonsik Kim75e22f42021-04-14 23:34:51 -07002505 }
2506 outputFormat = config->mOutputFormat;
Wonsik Kim9c387412021-04-19 21:03:53 +00002507 }
2508 mChannel->onWorkDone(
Wonsik Kim1f5063d2021-05-03 15:41:17 -07002509 std::move(work), outputFormat, initData ? initData.get() : nullptr);
Songyue Han1e6769b2023-08-30 18:09:27 +00002510 // log metrics to MediaCodec
2511 if (mMetrics->countEntries() == 0) {
2512 Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig);
2513 const std::unique_ptr<Config> &config = *configLocked;
2514 uint32_t pf = PIXEL_FORMAT_UNKNOWN;
2515 if (!config->mInputSurface) {
2516 pf = mChannel->getBuffersPixelFormat(config->mDomain & Config::IS_ENCODER);
Songyue Hanad01f6a2023-08-17 05:45:35 +00002517 } else {
2518 pf = config->mInputSurface->getPixelFormat();
Songyue Han1e6769b2023-08-30 18:09:27 +00002519 }
2520 if (pf != PIXEL_FORMAT_UNKNOWN) {
2521 mMetrics->setInt64(kCodecPixelFormat, pf);
2522 mCallback->onMetricsUpdated(mMetrics);
2523 }
2524 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002525 break;
2526 }
2527 case kWhatWatch: {
2528 // watch message already posted; no-op.
2529 break;
2530 }
2531 default: {
2532 ALOGE("unrecognized message");
2533 break;
2534 }
2535 }
2536 setDeadline(TimePoint::max(), 0ms, "none");
2537}
2538
2539void CCodec::setDeadline(
2540 const TimePoint &now,
2541 const std::chrono::milliseconds &timeout,
2542 const char *name) {
2543 int32_t mult = std::max(1, property_get_int32("debug.stagefright.ccodec_timeout_mult", 1));
2544 Mutexed<NamedTimePoint>::Locked deadline(mDeadline);
2545 deadline->set(now + (timeout * mult), name);
2546}
2547
ted.sun765db4d2020-06-23 14:03:41 +08002548status_t CCodec::configureTunneledVideoPlayback(
2549 std::shared_ptr<Codec2Client::Component> comp,
2550 sp<NativeHandle> *sidebandHandle,
2551 const sp<AMessage> &msg) {
2552 std::vector<std::unique_ptr<C2SettingResult>> failures;
2553
2554 std::unique_ptr<C2PortTunneledModeTuning::output> tunneledPlayback =
2555 C2PortTunneledModeTuning::output::AllocUnique(
2556 1,
2557 C2PortTunneledModeTuning::Struct::SIDEBAND,
2558 C2PortTunneledModeTuning::Struct::REALTIME,
2559 0);
2560 // TODO: use KEY_AUDIO_HW_SYNC, KEY_HARDWARE_AV_SYNC_ID when they are in MediaCodecConstants.h
2561 if (msg->findInt32("audio-hw-sync", &tunneledPlayback->m.syncId[0])) {
2562 tunneledPlayback->m.syncType = C2PortTunneledModeTuning::Struct::sync_type_t::AUDIO_HW_SYNC;
2563 } else if (msg->findInt32("hw-av-sync-id", &tunneledPlayback->m.syncId[0])) {
2564 tunneledPlayback->m.syncType = C2PortTunneledModeTuning::Struct::sync_type_t::HW_AV_SYNC;
2565 } else {
2566 tunneledPlayback->m.syncType = C2PortTunneledModeTuning::Struct::sync_type_t::REALTIME;
2567 tunneledPlayback->setFlexCount(0);
2568 }
2569 c2_status_t c2err = comp->config({ tunneledPlayback.get() }, C2_MAY_BLOCK, &failures);
2570 if (c2err != C2_OK) {
2571 return UNKNOWN_ERROR;
2572 }
2573
Houxiang Dai5a97b472021-03-22 17:56:04 +08002574 if (sidebandHandle == nullptr) {
2575 return OK;
2576 }
2577
ted.sun765db4d2020-06-23 14:03:41 +08002578 std::vector<std::unique_ptr<C2Param>> params;
2579 c2err = comp->query({}, {C2PortTunnelHandleTuning::output::PARAM_TYPE}, C2_DONT_BLOCK, &params);
2580 if (c2err == C2_OK && params.size() == 1u) {
2581 C2PortTunnelHandleTuning::output *videoTunnelSideband =
2582 C2PortTunnelHandleTuning::output::From(params[0].get());
2583 // Currently, Codec2 only supports non-fd case for sideband native_handle.
2584 native_handle_t *handle = native_handle_create(0, videoTunnelSideband->flexCount());
2585 *sidebandHandle = NativeHandle::create(handle, true /* ownsHandle */);
2586 if (handle != nullptr && videoTunnelSideband->flexCount()) {
2587 memcpy(handle->data, videoTunnelSideband->m.values,
2588 sizeof(int32_t) * videoTunnelSideband->flexCount());
2589 return OK;
2590 } else {
2591 return NO_MEMORY;
2592 }
2593 }
2594 return UNKNOWN_ERROR;
2595}
2596
Pawin Vongmasa36653902018-11-15 00:10:25 -08002597void CCodec::initiateReleaseIfStuck() {
Shrikara B3b87a532022-08-26 14:18:14 +05302598 std::string name;
2599 bool pendingDeadline = false;
2600 {
2601 Mutexed<NamedTimePoint>::Locked deadline(mDeadline);
2602 if (deadline->get() < std::chrono::steady_clock::now()) {
2603 name = deadline->getName();
2604 }
2605 if (deadline->get() != TimePoint::max()) {
2606 pendingDeadline = true;
2607 }
2608 }
Wonsik Kimab34ed62019-01-31 15:28:46 -08002609 if (name.empty()) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08002610 // We're not stuck.
2611 if (pendingDeadline) {
2612 // If we are not stuck yet but still has deadline coming up,
2613 // post watch message to check back later.
2614 (new AMessage(kWhatWatch, this))->post();
2615 }
2616 return;
2617 }
2618
Chih-Yu Huang82e5ab32021-02-17 16:27:08 +09002619 C2String compName;
2620 {
2621 Mutexed<State>::Locked state(mState);
Wonsik Kim12380072021-05-11 09:59:20 -07002622 if (!state->comp) {
2623 ALOGD("previous call to %s exceeded timeout "
2624 "and the component is already released", name.c_str());
2625 return;
2626 }
Chih-Yu Huang82e5ab32021-02-17 16:27:08 +09002627 compName = state->comp->getName();
2628 }
2629 ALOGW("[%s] previous call to %s exceeded timeout", compName.c_str(), name.c_str());
2630
Pawin Vongmasa36653902018-11-15 00:10:25 -08002631 initiateRelease(false);
2632 mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2633}
2634
Wonsik Kim155d5cb2019-10-09 12:49:49 -07002635// static
2636PersistentSurface *CCodec::CreateInputSurface() {
Pawin Vongmasa18588322019-05-18 01:52:13 -07002637 using namespace android;
Wonsik Kim9917d4a2019-10-24 12:56:38 -07002638 using ::android::hardware::media::omx::V1_0::implementation::TWGraphicBufferSource;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002639 // Attempt to create a Codec2's input surface.
Pawin Vongmasa18588322019-05-18 01:52:13 -07002640 std::shared_ptr<Codec2Client::InputSurface> inputSurface =
2641 Codec2Client::CreateInputSurface();
Lajos Molnar47118272019-01-31 16:28:04 -08002642 if (!inputSurface) {
Pawin Vongmasa18588322019-05-18 01:52:13 -07002643 if (property_get_int32("debug.stagefright.c2inputsurface", 0) == -1) {
2644 sp<IGraphicBufferProducer> gbp;
2645 sp<OmxGraphicBufferSource> gbs = new OmxGraphicBufferSource();
2646 status_t err = gbs->initCheck();
2647 if (err != OK) {
2648 ALOGE("Failed to create persistent input surface: error %d", err);
2649 return nullptr;
2650 }
2651 return new PersistentSurface(
Wonsik Kim9917d4a2019-10-24 12:56:38 -07002652 gbs->getIGraphicBufferProducer(), new TWGraphicBufferSource(gbs));
Pawin Vongmasa18588322019-05-18 01:52:13 -07002653 } else {
2654 return nullptr;
2655 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002656 }
Pawin Vongmasa18588322019-05-18 01:52:13 -07002657 return new PersistentSurface(
Lajos Molnar47118272019-01-31 16:28:04 -08002658 inputSurface->getGraphicBufferProducer(),
Pawin Vongmasa18588322019-05-18 01:52:13 -07002659 static_cast<sp<android::hidl::base::V1_0::IBase>>(
Lajos Molnar47118272019-01-31 16:28:04 -08002660 inputSurface->getHalInterface()));
Pawin Vongmasa36653902018-11-15 00:10:25 -08002661}
2662
Wonsik Kimffb889a2020-05-28 11:32:25 -07002663class IntfCache {
2664public:
2665 IntfCache() = default;
2666
2667 status_t init(const std::string &name) {
2668 std::shared_ptr<Codec2Client::Interface> intf{
2669 Codec2Client::CreateInterfaceByName(name.c_str())};
2670 if (!intf) {
2671 ALOGW("IntfCache [%s]: Unrecognized interface name", name.c_str());
2672 mInitStatus = NO_INIT;
2673 return NO_INIT;
2674 }
2675 const static C2StreamUsageTuning::input sUsage{0u /* stream id */};
2676 mFields.push_back(C2FieldSupportedValuesQuery::Possible(
2677 C2ParamField{&sUsage, &sUsage.value}));
2678 c2_status_t err = intf->querySupportedValues(mFields, C2_MAY_BLOCK);
2679 if (err != C2_OK) {
2680 ALOGW("IntfCache [%s]: failed to query usage supported value (err=%d)",
2681 name.c_str(), err);
2682 mFields[0].status = err;
2683 }
2684 std::vector<std::unique_ptr<C2Param>> params;
2685 err = intf->query(
2686 {&mApiFeatures},
Taehwan Kim900b49c2021-12-13 11:16:22 +09002687 {
2688 C2StreamBufferTypeSetting::input::PARAM_TYPE,
2689 C2PortAllocatorsTuning::input::PARAM_TYPE
2690 },
Wonsik Kimffb889a2020-05-28 11:32:25 -07002691 C2_MAY_BLOCK,
2692 &params);
2693 if (err != C2_OK && err != C2_BAD_INDEX) {
2694 ALOGW("IntfCache [%s]: failed to query api features (err=%d)",
2695 name.c_str(), err);
2696 }
2697 while (!params.empty()) {
2698 C2Param *param = params.back().release();
2699 params.pop_back();
2700 if (!param) {
2701 continue;
2702 }
Taehwan Kim900b49c2021-12-13 11:16:22 +09002703 if (param->type() == C2StreamBufferTypeSetting::input::PARAM_TYPE) {
2704 mInputStreamFormat.reset(
2705 C2StreamBufferTypeSetting::input::From(param));
2706 } else if (param->type() == C2PortAllocatorsTuning::input::PARAM_TYPE) {
Wonsik Kimffb889a2020-05-28 11:32:25 -07002707 mInputAllocators.reset(
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07002708 C2PortAllocatorsTuning::input::From(param));
Wonsik Kimffb889a2020-05-28 11:32:25 -07002709 }
2710 }
2711 mInitStatus = OK;
2712 return OK;
Wonsik Kimfcf46c32020-04-22 13:50:45 -07002713 }
Wonsik Kimffb889a2020-05-28 11:32:25 -07002714
2715 status_t initCheck() const { return mInitStatus; }
2716
2717 const C2FieldSupportedValuesQuery &getUsageSupportedValues() const {
2718 CHECK_EQ(1u, mFields.size());
2719 return mFields[0];
2720 }
2721
2722 const C2ApiFeaturesSetting &getApiFeatures() const {
2723 return mApiFeatures;
2724 }
2725
Taehwan Kim900b49c2021-12-13 11:16:22 +09002726 const C2StreamBufferTypeSetting::input &getInputStreamFormat() const {
2727 static std::unique_ptr<C2StreamBufferTypeSetting::input> sInvalidated = []{
2728 std::unique_ptr<C2StreamBufferTypeSetting::input> param;
2729 param.reset(new C2StreamBufferTypeSetting::input(0u, C2BufferData::INVALID));
2730 param->invalidate();
2731 return param;
2732 }();
2733 return mInputStreamFormat ? *mInputStreamFormat : *sInvalidated;
2734 }
2735
Wonsik Kimffb889a2020-05-28 11:32:25 -07002736 const C2PortAllocatorsTuning::input &getInputAllocators() const {
2737 static std::unique_ptr<C2PortAllocatorsTuning::input> sInvalidated = []{
2738 std::unique_ptr<C2PortAllocatorsTuning::input> param =
2739 C2PortAllocatorsTuning::input::AllocUnique(0);
2740 param->invalidate();
2741 return param;
2742 }();
2743 return mInputAllocators ? *mInputAllocators : *sInvalidated;
2744 }
2745
2746private:
2747 status_t mInitStatus{NO_INIT};
2748
2749 std::vector<C2FieldSupportedValuesQuery> mFields;
2750 C2ApiFeaturesSetting mApiFeatures;
Taehwan Kim900b49c2021-12-13 11:16:22 +09002751 std::unique_ptr<C2StreamBufferTypeSetting::input> mInputStreamFormat;
Wonsik Kimffb889a2020-05-28 11:32:25 -07002752 std::unique_ptr<C2PortAllocatorsTuning::input> mInputAllocators;
2753};
2754
2755static const IntfCache &GetIntfCache(const std::string &name) {
2756 static IntfCache sNullIntfCache;
2757 static std::mutex sMutex;
2758 static std::map<std::string, IntfCache> sCache;
2759 std::unique_lock<std::mutex> lock{sMutex};
2760 auto it = sCache.find(name);
2761 if (it == sCache.end()) {
2762 lock.unlock();
2763 IntfCache intfCache;
2764 status_t err = intfCache.init(name);
2765 if (err != OK) {
2766 return sNullIntfCache;
2767 }
2768 lock.lock();
2769 it = sCache.insert({name, std::move(intfCache)}).first;
2770 }
2771 return it->second;
Wonsik Kimfcf46c32020-04-22 13:50:45 -07002772}
2773
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002774static status_t GetCommonAllocatorIds(
2775 const std::vector<std::string> &names,
2776 C2Allocator::type_t type,
2777 std::set<C2Allocator::id_t> *ids) {
2778 int poolMask = GetCodec2PoolMask();
2779 C2PlatformAllocatorStore::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask);
2780 C2Allocator::id_t defaultAllocatorId =
2781 (type == C2Allocator::LINEAR) ? preferredLinearId : C2PlatformAllocatorStore::GRALLOC;
2782
2783 ids->clear();
2784 if (names.empty()) {
2785 return OK;
2786 }
Wonsik Kimfcf46c32020-04-22 13:50:45 -07002787 bool firstIteration = true;
2788 for (const std::string &name : names) {
Wonsik Kimffb889a2020-05-28 11:32:25 -07002789 const IntfCache &intfCache = GetIntfCache(name);
2790 if (intfCache.initCheck() != OK) {
Wonsik Kimfcf46c32020-04-22 13:50:45 -07002791 continue;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002792 }
Taehwan Kim900b49c2021-12-13 11:16:22 +09002793 const C2StreamBufferTypeSetting::input &streamFormat = intfCache.getInputStreamFormat();
2794 if (streamFormat) {
2795 C2Allocator::type_t allocatorType = C2Allocator::LINEAR;
2796 if (streamFormat.value == C2BufferData::GRAPHIC
2797 || streamFormat.value == C2BufferData::GRAPHIC_CHUNKS) {
2798 allocatorType = C2Allocator::GRAPHIC;
2799 }
2800
2801 if (type != allocatorType) {
2802 // requested type is not supported at input allocators
2803 ids->clear();
2804 ids->insert(defaultAllocatorId);
2805 ALOGV("name(%s) does not support a type(0x%x) as input allocator."
2806 " uses default allocator id(%d)", name.c_str(), type, defaultAllocatorId);
2807 break;
2808 }
2809 }
2810
Wonsik Kimffb889a2020-05-28 11:32:25 -07002811 const C2PortAllocatorsTuning::input &allocators = intfCache.getInputAllocators();
Wonsik Kimfcf46c32020-04-22 13:50:45 -07002812 if (firstIteration) {
2813 firstIteration = false;
Wonsik Kimffb889a2020-05-28 11:32:25 -07002814 if (allocators && allocators.flexCount() > 0) {
2815 ids->insert(allocators.m.values,
2816 allocators.m.values + allocators.flexCount());
Wonsik Kimfcf46c32020-04-22 13:50:45 -07002817 }
2818 if (ids->empty()) {
2819 // The component does not advertise allocators. Use default.
2820 ids->insert(defaultAllocatorId);
2821 }
2822 continue;
2823 }
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002824 bool filtered = false;
Wonsik Kimffb889a2020-05-28 11:32:25 -07002825 if (allocators && allocators.flexCount() > 0) {
2826 filtered = true;
2827 for (auto it = ids->begin(); it != ids->end(); ) {
2828 bool found = false;
2829 for (size_t j = 0; j < allocators.flexCount(); ++j) {
2830 if (allocators.m.values[j] == *it) {
2831 found = true;
2832 break;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002833 }
Wonsik Kimffb889a2020-05-28 11:32:25 -07002834 }
2835 if (found) {
2836 ++it;
2837 } else {
2838 it = ids->erase(it);
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002839 }
2840 }
2841 }
2842 if (!filtered) {
2843 // The component does not advertise supported allocators. Use default.
2844 bool containsDefault = (ids->count(defaultAllocatorId) > 0u);
2845 if (ids->size() != (containsDefault ? 1 : 0)) {
2846 ids->clear();
2847 if (containsDefault) {
2848 ids->insert(defaultAllocatorId);
2849 }
2850 }
2851 }
2852 }
2853 // Finally, filter with pool masks
2854 for (auto it = ids->begin(); it != ids->end(); ) {
2855 if ((poolMask >> *it) & 1) {
2856 ++it;
2857 } else {
2858 it = ids->erase(it);
2859 }
2860 }
2861 return OK;
2862}
2863
2864static status_t CalculateMinMaxUsage(
2865 const std::vector<std::string> &names, uint64_t *minUsage, uint64_t *maxUsage) {
2866 static C2StreamUsageTuning::input sUsage{0u /* stream id */};
2867 *minUsage = 0;
2868 *maxUsage = ~0ull;
2869 for (const std::string &name : names) {
Wonsik Kimffb889a2020-05-28 11:32:25 -07002870 const IntfCache &intfCache = GetIntfCache(name);
2871 if (intfCache.initCheck() != OK) {
Wonsik Kimfcf46c32020-04-22 13:50:45 -07002872 continue;
2873 }
Wonsik Kimffb889a2020-05-28 11:32:25 -07002874 const C2FieldSupportedValuesQuery &usageSupportedValues =
2875 intfCache.getUsageSupportedValues();
2876 if (usageSupportedValues.status != C2_OK) {
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002877 continue;
2878 }
Wonsik Kimffb889a2020-05-28 11:32:25 -07002879 const C2FieldSupportedValues &supported = usageSupportedValues.values;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002880 if (supported.type != C2FieldSupportedValues::FLAGS) {
2881 continue;
2882 }
2883 if (supported.values.empty()) {
2884 *maxUsage = 0;
2885 continue;
2886 }
Houxiang Daibfb8a722021-04-13 17:34:40 +08002887 if (supported.values.size() > 1) {
2888 *minUsage |= supported.values[1].u64;
2889 } else {
2890 *minUsage |= supported.values[0].u64;
2891 }
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002892 int64_t currentMaxUsage = 0;
2893 for (const C2Value::Primitive &flags : supported.values) {
2894 currentMaxUsage |= flags.u64;
2895 }
2896 *maxUsage &= currentMaxUsage;
2897 }
2898 return OK;
2899}
2900
2901// static
2902status_t CCodec::CanFetchLinearBlock(
2903 const std::vector<std::string> &names, const C2MemoryUsage &usage, bool *isCompatible) {
Wonsik Kimffb889a2020-05-28 11:32:25 -07002904 for (const std::string &name : names) {
2905 const IntfCache &intfCache = GetIntfCache(name);
2906 if (intfCache.initCheck() != OK) {
2907 continue;
2908 }
2909 const C2ApiFeaturesSetting &features = intfCache.getApiFeatures();
2910 if (features && !(features.value & API_SAME_INPUT_BUFFER)) {
2911 *isCompatible = false;
2912 return OK;
2913 }
2914 }
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002915 std::set<C2Allocator::id_t> allocators;
2916 GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators);
2917 if (allocators.empty()) {
2918 *isCompatible = false;
2919 return OK;
2920 }
Chih-Yu Huang990b5622020-10-13 14:53:37 +09002921
2922 uint64_t minUsage = 0;
2923 uint64_t maxUsage = ~0ull;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002924 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
Chih-Yu Huang990b5622020-10-13 14:53:37 +09002925 minUsage |= usage.expected;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002926 *isCompatible = ((maxUsage & minUsage) == minUsage);
2927 return OK;
2928}
2929
2930static std::shared_ptr<C2BlockPool> GetPool(C2Allocator::id_t allocId) {
2931 static std::mutex sMutex{};
2932 static std::map<C2Allocator::id_t, std::shared_ptr<C2BlockPool>> sPools;
2933 std::unique_lock<std::mutex> lock{sMutex};
2934 std::shared_ptr<C2BlockPool> pool;
2935 auto it = sPools.find(allocId);
2936 if (it == sPools.end()) {
2937 c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool);
2938 if (err == OK) {
2939 sPools.emplace(allocId, pool);
2940 } else {
2941 pool.reset();
2942 }
2943 } else {
2944 pool = it->second;
2945 }
2946 return pool;
2947}
2948
2949// static
2950std::shared_ptr<C2LinearBlock> CCodec::FetchLinearBlock(
2951 size_t capacity, const C2MemoryUsage &usage, const std::vector<std::string> &names) {
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002952 std::set<C2Allocator::id_t> allocators;
2953 GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators);
2954 if (allocators.empty()) {
2955 allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR);
2956 }
Chih-Yu Huang990b5622020-10-13 14:53:37 +09002957
2958 uint64_t minUsage = 0;
2959 uint64_t maxUsage = ~0ull;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002960 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
Chih-Yu Huang990b5622020-10-13 14:53:37 +09002961 minUsage |= usage.expected;
Wonsik Kimfb7a7672019-12-27 17:13:33 -08002962 if ((maxUsage & minUsage) != minUsage) {
2963 allocators.clear();
2964 allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR);
2965 }
2966 std::shared_ptr<C2LinearBlock> block;
2967 for (C2Allocator::id_t allocId : allocators) {
2968 std::shared_ptr<C2BlockPool> pool = GetPool(allocId);
2969 if (!pool) {
2970 continue;
2971 }
2972 c2_status_t err = pool->fetchLinearBlock(capacity, C2MemoryUsage{minUsage}, &block);
2973 if (err != C2_OK || !block) {
2974 block.reset();
2975 continue;
2976 }
2977 break;
2978 }
2979 return block;
2980}
2981
2982// static
2983status_t CCodec::CanFetchGraphicBlock(
2984 const std::vector<std::string> &names, bool *isCompatible) {
2985 uint64_t minUsage = 0;
2986 uint64_t maxUsage = ~0ull;
2987 std::set<C2Allocator::id_t> allocators;
2988 GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators);
2989 if (allocators.empty()) {
2990 *isCompatible = false;
2991 return OK;
2992 }
2993 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
2994 *isCompatible = ((maxUsage & minUsage) == minUsage);
2995 return OK;
2996}
2997
2998// static
2999std::shared_ptr<C2GraphicBlock> CCodec::FetchGraphicBlock(
3000 int32_t width,
3001 int32_t height,
3002 int32_t format,
3003 uint64_t usage,
3004 const std::vector<std::string> &names) {
3005 uint32_t halPixelFormat = HAL_PIXEL_FORMAT_YCBCR_420_888;
3006 if (!C2Mapper::mapPixelFormatFrameworkToCodec(format, &halPixelFormat)) {
3007 ALOGD("Unrecognized pixel format: %d", format);
3008 return nullptr;
3009 }
3010 uint64_t minUsage = 0;
3011 uint64_t maxUsage = ~0ull;
3012 std::set<C2Allocator::id_t> allocators;
3013 GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators);
3014 if (allocators.empty()) {
3015 allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC);
3016 }
3017 CalculateMinMaxUsage(names, &minUsage, &maxUsage);
3018 minUsage |= usage;
3019 if ((maxUsage & minUsage) != minUsage) {
3020 allocators.clear();
3021 allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC);
3022 }
3023 std::shared_ptr<C2GraphicBlock> block;
3024 for (C2Allocator::id_t allocId : allocators) {
3025 std::shared_ptr<C2BlockPool> pool;
3026 c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool);
3027 if (err != C2_OK || !pool) {
3028 continue;
3029 }
3030 err = pool->fetchGraphicBlock(
3031 width, height, halPixelFormat, C2MemoryUsage{minUsage}, &block);
3032 if (err != C2_OK || !block) {
3033 block.reset();
3034 continue;
3035 }
3036 break;
3037 }
3038 return block;
3039}
3040
Wonsik Kim155d5cb2019-10-09 12:49:49 -07003041} // namespace android