blob: 333a2ca8f80d0923d80f1843608b3c4070152bdf [file] [log] [blame]
Wonsik Kim469c8342019-04-11 16:46:09 -07001/*
2 * Copyright 2019, 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 "CCodecBuffers"
19#include <utils/Log.h>
20
21#include <C2PlatformSupport.h>
22
23#include <media/stagefright/foundation/ADebug.h>
Pawin Vongmasa9b906982020-04-11 05:07:15 -070024#include <media/stagefright/MediaCodec.h>
Wonsik Kim469c8342019-04-11 16:46:09 -070025#include <media/stagefright/MediaCodecConstants.h>
Wonsik Kim155d5cb2019-10-09 12:49:49 -070026#include <media/stagefright/SkipCutBuffer.h>
Wonsik Kim41d83432020-04-27 16:40:49 -070027#include <mediadrm/ICrypto.h>
Wonsik Kim469c8342019-04-11 16:46:09 -070028
29#include "CCodecBuffers.h"
Wonsik Kimd79ee1f2020-08-27 17:41:56 -070030#include "Codec2Mapper.h"
Wonsik Kim469c8342019-04-11 16:46:09 -070031
32namespace android {
33
34namespace {
35
36sp<GraphicBlockBuffer> AllocateGraphicBuffer(
37 const std::shared_ptr<C2BlockPool> &pool,
38 const sp<AMessage> &format,
39 uint32_t pixelFormat,
40 const C2MemoryUsage &usage,
41 const std::shared_ptr<LocalBufferPool> &localBufferPool) {
42 int32_t width, height;
43 if (!format->findInt32("width", &width) || !format->findInt32("height", &height)) {
44 ALOGD("format lacks width or height");
45 return nullptr;
46 }
47
48 std::shared_ptr<C2GraphicBlock> block;
49 c2_status_t err = pool->fetchGraphicBlock(
50 width, height, pixelFormat, usage, &block);
51 if (err != C2_OK) {
52 ALOGD("fetch graphic block failed: %d", err);
53 return nullptr;
54 }
55
56 return GraphicBlockBuffer::Allocate(
57 format,
58 block,
59 [localBufferPool](size_t capacity) {
60 return localBufferPool->newBuffer(capacity);
61 });
62}
63
64} // namespace
65
66// CCodecBuffers
67
68void CCodecBuffers::setFormat(const sp<AMessage> &format) {
69 CHECK(format != nullptr);
70 mFormat = format;
71}
72
73sp<AMessage> CCodecBuffers::dupFormat() {
74 return mFormat != nullptr ? mFormat->dup() : nullptr;
75}
76
77void CCodecBuffers::handleImageData(const sp<Codec2Buffer> &buffer) {
78 sp<ABuffer> imageDataCandidate = buffer->getImageData();
79 if (imageDataCandidate == nullptr) {
Wonsik Kim4a3c0462021-03-09 15:45:05 -080080 if (mFormatWithImageData) {
81 // We previously sent the format with image data, so use the same format.
82 buffer->setFormat(mFormatWithImageData);
83 }
Wonsik Kim469c8342019-04-11 16:46:09 -070084 return;
85 }
Wonsik Kim4a3c0462021-03-09 15:45:05 -080086 if (!mLastImageData
87 || imageDataCandidate->size() != mLastImageData->size()
88 || memcmp(imageDataCandidate->data(),
89 mLastImageData->data(),
90 mLastImageData->size()) != 0) {
Wonsik Kim469c8342019-04-11 16:46:09 -070091 ALOGD("[%s] updating image-data", mName);
Wonsik Kim4a3c0462021-03-09 15:45:05 -080092 mFormatWithImageData = dupFormat();
93 mLastImageData = imageDataCandidate;
94 mFormatWithImageData->setBuffer("image-data", imageDataCandidate);
Wonsik Kim469c8342019-04-11 16:46:09 -070095 MediaImage2 *img = (MediaImage2*)imageDataCandidate->data();
96 if (img->mNumPlanes > 0 && img->mType != img->MEDIA_IMAGE_TYPE_UNKNOWN) {
97 int32_t stride = img->mPlane[0].mRowInc;
Wonsik Kim4a3c0462021-03-09 15:45:05 -080098 mFormatWithImageData->setInt32(KEY_STRIDE, stride);
Vinay Kaliaef5fc712021-09-15 23:59:50 +000099 mFormatWithImageData->setInt32(KEY_WIDTH, img->mWidth);
100 mFormatWithImageData->setInt32(KEY_HEIGHT, img->mHeight);
101 ALOGD("[%s] updating stride = %d, width: %d, height: %d",
102 mName, stride, img->mWidth, img->mHeight);
Wonsik Kim469c8342019-04-11 16:46:09 -0700103 if (img->mNumPlanes > 1 && stride > 0) {
Taehwan Kimfd9b8092020-09-17 12:26:40 +0900104 int64_t offsetDelta =
105 (int64_t)img->mPlane[1].mOffset - (int64_t)img->mPlane[0].mOffset;
106 int32_t vstride = int32_t(offsetDelta / stride);
Wonsik Kim4a3c0462021-03-09 15:45:05 -0800107 mFormatWithImageData->setInt32(KEY_SLICE_HEIGHT, vstride);
Wonsik Kim469c8342019-04-11 16:46:09 -0700108 ALOGD("[%s] updating vstride = %d", mName, vstride);
Wonsik Kim2eb06312020-12-03 11:07:58 -0800109 buffer->setRange(
110 img->mPlane[0].mOffset,
111 buffer->size() - img->mPlane[0].mOffset);
Wonsik Kim469c8342019-04-11 16:46:09 -0700112 }
113 }
Wonsik Kim469c8342019-04-11 16:46:09 -0700114 }
Wonsik Kim4a3c0462021-03-09 15:45:05 -0800115 buffer->setFormat(mFormatWithImageData);
Wonsik Kim469c8342019-04-11 16:46:09 -0700116}
117
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700118// InputBuffers
119
120sp<Codec2Buffer> InputBuffers::cloneAndReleaseBuffer(const sp<MediaCodecBuffer> &buffer) {
121 sp<Codec2Buffer> copy = createNewBuffer();
122 if (copy == nullptr) {
123 return nullptr;
124 }
125 std::shared_ptr<C2Buffer> c2buffer;
126 if (!releaseBuffer(buffer, &c2buffer, true)) {
127 return nullptr;
128 }
129 if (!copy->canCopy(c2buffer)) {
130 return nullptr;
131 }
132 if (!copy->copy(c2buffer)) {
133 return nullptr;
134 }
135 return copy;
136}
137
Wonsik Kim469c8342019-04-11 16:46:09 -0700138// OutputBuffers
139
Wonsik Kim41d83432020-04-27 16:40:49 -0700140OutputBuffers::OutputBuffers(const char *componentName, const char *name)
141 : CCodecBuffers(componentName, name) { }
142
143OutputBuffers::~OutputBuffers() = default;
144
Wonsik Kim469c8342019-04-11 16:46:09 -0700145void OutputBuffers::initSkipCutBuffer(
146 int32_t delay, int32_t padding, int32_t sampleRate, int32_t channelCount) {
147 CHECK(mSkipCutBuffer == nullptr);
148 mDelay = delay;
149 mPadding = padding;
150 mSampleRate = sampleRate;
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700151 mChannelCount = channelCount;
152 setSkipCutBuffer(delay, padding);
Wonsik Kim469c8342019-04-11 16:46:09 -0700153}
154
155void OutputBuffers::updateSkipCutBuffer(int32_t sampleRate, int32_t channelCount) {
156 if (mSkipCutBuffer == nullptr) {
157 return;
158 }
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700159 if (mSampleRate == sampleRate && mChannelCount == channelCount) {
160 return;
161 }
Wonsik Kim469c8342019-04-11 16:46:09 -0700162 int32_t delay = mDelay;
163 int32_t padding = mPadding;
164 if (sampleRate != mSampleRate) {
165 delay = ((int64_t)delay * sampleRate) / mSampleRate;
166 padding = ((int64_t)padding * sampleRate) / mSampleRate;
167 }
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700168 mSampleRate = sampleRate;
169 mChannelCount = channelCount;
170 setSkipCutBuffer(delay, padding);
Wonsik Kim469c8342019-04-11 16:46:09 -0700171}
172
Wonsik Kim3b4349a2020-11-10 11:54:15 -0800173void OutputBuffers::updateSkipCutBuffer(const sp<AMessage> &format) {
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700174 AString mediaType;
175 if (format->findString(KEY_MIME, &mediaType)
176 && mediaType == MIMETYPE_AUDIO_RAW) {
177 int32_t channelCount;
178 int32_t sampleRate;
179 if (format->findInt32(KEY_CHANNEL_COUNT, &channelCount)
180 && format->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
181 updateSkipCutBuffer(sampleRate, channelCount);
182 }
183 }
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700184}
185
Wonsik Kim469c8342019-04-11 16:46:09 -0700186void OutputBuffers::submit(const sp<MediaCodecBuffer> &buffer) {
187 if (mSkipCutBuffer != nullptr) {
188 mSkipCutBuffer->submit(buffer);
189 }
190}
191
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700192void OutputBuffers::setSkipCutBuffer(int32_t skip, int32_t cut) {
Wonsik Kim469c8342019-04-11 16:46:09 -0700193 if (mSkipCutBuffer != nullptr) {
194 size_t prevSize = mSkipCutBuffer->size();
195 if (prevSize != 0u) {
196 ALOGD("[%s] Replacing SkipCutBuffer holding %zu bytes", mName, prevSize);
197 }
198 }
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700199 mSkipCutBuffer = new SkipCutBuffer(skip, cut, mChannelCount);
Wonsik Kim469c8342019-04-11 16:46:09 -0700200}
201
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700202void OutputBuffers::clearStash() {
203 mPending.clear();
204 mReorderStash.clear();
205 mDepth = 0;
206 mKey = C2Config::ORDINAL;
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700207}
208
209void OutputBuffers::flushStash() {
210 for (StashEntry& e : mPending) {
211 e.notify = false;
212 }
213 for (StashEntry& e : mReorderStash) {
214 e.notify = false;
215 }
216}
217
218uint32_t OutputBuffers::getReorderDepth() const {
219 return mDepth;
220}
221
222void OutputBuffers::setReorderDepth(uint32_t depth) {
223 mPending.splice(mPending.end(), mReorderStash);
224 mDepth = depth;
225}
226
227void OutputBuffers::setReorderKey(C2Config::ordinal_key_t key) {
228 mPending.splice(mPending.end(), mReorderStash);
229 mKey = key;
230}
231
232void OutputBuffers::pushToStash(
233 const std::shared_ptr<C2Buffer>& buffer,
234 bool notify,
235 int64_t timestamp,
236 int32_t flags,
237 const sp<AMessage>& format,
238 const C2WorkOrdinalStruct& ordinal) {
239 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
240 if (!buffer && eos) {
241 // TRICKY: we may be violating ordering of the stash here. Because we
242 // don't expect any more emplace() calls after this, the ordering should
243 // not matter.
244 mReorderStash.emplace_back(
245 buffer, notify, timestamp, flags, format, ordinal);
246 } else {
247 flags = flags & ~MediaCodec::BUFFER_FLAG_EOS;
248 auto it = mReorderStash.begin();
249 for (; it != mReorderStash.end(); ++it) {
250 if (less(ordinal, it->ordinal)) {
251 break;
252 }
253 }
254 mReorderStash.emplace(it,
255 buffer, notify, timestamp, flags, format, ordinal);
256 if (eos) {
257 mReorderStash.back().flags =
258 mReorderStash.back().flags | MediaCodec::BUFFER_FLAG_EOS;
259 }
260 }
261 while (!mReorderStash.empty() && mReorderStash.size() > mDepth) {
262 mPending.push_back(mReorderStash.front());
263 mReorderStash.pop_front();
264 }
265 ALOGV("[%s] %s: pushToStash -- pending size = %zu", mName, __func__, mPending.size());
266}
267
268OutputBuffers::BufferAction OutputBuffers::popFromStashAndRegister(
269 std::shared_ptr<C2Buffer>* c2Buffer,
270 size_t* index,
271 sp<MediaCodecBuffer>* outBuffer) {
272 if (mPending.empty()) {
273 return SKIP;
274 }
275
276 // Retrieve the first entry.
277 StashEntry &entry = mPending.front();
278
279 *c2Buffer = entry.buffer;
280 sp<AMessage> outputFormat = entry.format;
281
Wonsik Kim3b4349a2020-11-10 11:54:15 -0800282 if (entry.notify && mFormat != outputFormat) {
283 updateSkipCutBuffer(outputFormat);
Wonsik Kim4a3c0462021-03-09 15:45:05 -0800284 // Trigger image data processing to the new format
285 mLastImageData.clear();
Wonsik Kim3b4349a2020-11-10 11:54:15 -0800286 ALOGV("[%s] popFromStashAndRegister: output format reference changed: %p -> %p",
287 mName, mFormat.get(), outputFormat.get());
Wonsik Kim4a3c0462021-03-09 15:45:05 -0800288 ALOGD("[%s] popFromStashAndRegister: at %lldus, output format changed to %s",
289 mName, (long long)entry.timestamp, outputFormat->debugString().c_str());
Wonsik Kim3b4349a2020-11-10 11:54:15 -0800290 setFormat(outputFormat);
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700291 }
292
293 // Flushing mReorderStash because no other buffers should come after output
294 // EOS.
295 if (entry.flags & MediaCodec::BUFFER_FLAG_EOS) {
296 // Flush reorder stash
297 setReorderDepth(0);
298 }
299
300 if (!entry.notify) {
301 mPending.pop_front();
302 return DISCARD;
303 }
304
305 // Try to register the buffer.
306 status_t err = registerBuffer(*c2Buffer, index, outBuffer);
307 if (err != OK) {
308 if (err != WOULD_BLOCK) {
309 return REALLOCATE;
310 }
311 return RETRY;
312 }
313
314 // Append information from the front stash entry to outBuffer.
315 (*outBuffer)->meta()->setInt64("timeUs", entry.timestamp);
316 (*outBuffer)->meta()->setInt32("flags", entry.flags);
Byeongjo Park2eef13e2020-06-12 17:24:21 +0900317 (*outBuffer)->meta()->setInt64("frameIndex", entry.ordinal.frameIndex.peekll());
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700318 ALOGV("[%s] popFromStashAndRegister: "
319 "out buffer index = %zu [%p] => %p + %zu (%lld)",
320 mName, *index, outBuffer->get(),
321 (*outBuffer)->data(), (*outBuffer)->size(),
322 (long long)entry.timestamp);
323
324 // The front entry of mPending will be removed now that the registration
325 // succeeded.
326 mPending.pop_front();
327 return NOTIFY_CLIENT;
328}
329
330bool OutputBuffers::popPending(StashEntry *entry) {
331 if (mPending.empty()) {
332 return false;
333 }
334 *entry = mPending.front();
335 mPending.pop_front();
336 return true;
337}
338
339void OutputBuffers::deferPending(const OutputBuffers::StashEntry &entry) {
340 mPending.push_front(entry);
341}
342
343bool OutputBuffers::hasPending() const {
344 return !mPending.empty();
345}
346
347bool OutputBuffers::less(
348 const C2WorkOrdinalStruct &o1, const C2WorkOrdinalStruct &o2) const {
349 switch (mKey) {
350 case C2Config::ORDINAL: return o1.frameIndex < o2.frameIndex;
351 case C2Config::TIMESTAMP: return o1.timestamp < o2.timestamp;
352 case C2Config::CUSTOM: return o1.customOrdinal < o2.customOrdinal;
353 default:
354 ALOGD("Unrecognized key; default to timestamp");
355 return o1.frameIndex < o2.frameIndex;
356 }
357}
358
Wonsik Kim469c8342019-04-11 16:46:09 -0700359// LocalBufferPool
360
Wonsik Kim41d83432020-04-27 16:40:49 -0700361constexpr size_t kInitialPoolCapacity = kMaxLinearBufferSize;
362constexpr size_t kMaxPoolCapacity = kMaxLinearBufferSize * 32;
363
364std::shared_ptr<LocalBufferPool> LocalBufferPool::Create() {
365 return std::shared_ptr<LocalBufferPool>(new LocalBufferPool(kInitialPoolCapacity));
Wonsik Kim469c8342019-04-11 16:46:09 -0700366}
367
368sp<ABuffer> LocalBufferPool::newBuffer(size_t capacity) {
369 Mutex::Autolock lock(mMutex);
370 auto it = std::find_if(
371 mPool.begin(), mPool.end(),
372 [capacity](const std::vector<uint8_t> &vec) {
373 return vec.capacity() >= capacity;
374 });
375 if (it != mPool.end()) {
376 sp<ABuffer> buffer = new VectorBuffer(std::move(*it), shared_from_this());
377 mPool.erase(it);
378 return buffer;
379 }
380 if (mUsedSize + capacity > mPoolCapacity) {
381 while (!mPool.empty()) {
382 mUsedSize -= mPool.back().capacity();
383 mPool.pop_back();
384 }
Wonsik Kim41d83432020-04-27 16:40:49 -0700385 while (mUsedSize + capacity > mPoolCapacity && mPoolCapacity * 2 <= kMaxPoolCapacity) {
386 ALOGD("Increasing local buffer pool capacity from %zu to %zu",
387 mPoolCapacity, mPoolCapacity * 2);
388 mPoolCapacity *= 2;
389 }
Wonsik Kim469c8342019-04-11 16:46:09 -0700390 if (mUsedSize + capacity > mPoolCapacity) {
391 ALOGD("mUsedSize = %zu, capacity = %zu, mPoolCapacity = %zu",
392 mUsedSize, capacity, mPoolCapacity);
393 return nullptr;
394 }
395 }
396 std::vector<uint8_t> vec(capacity);
397 mUsedSize += vec.capacity();
398 return new VectorBuffer(std::move(vec), shared_from_this());
399}
400
401LocalBufferPool::VectorBuffer::VectorBuffer(
402 std::vector<uint8_t> &&vec, const std::shared_ptr<LocalBufferPool> &pool)
403 : ABuffer(vec.data(), vec.capacity()),
404 mVec(std::move(vec)),
405 mPool(pool) {
406}
407
408LocalBufferPool::VectorBuffer::~VectorBuffer() {
409 std::shared_ptr<LocalBufferPool> pool = mPool.lock();
410 if (pool) {
411 // If pool is alive, return the vector back to the pool so that
412 // it can be recycled.
413 pool->returnVector(std::move(mVec));
414 }
415}
416
417void LocalBufferPool::returnVector(std::vector<uint8_t> &&vec) {
418 Mutex::Autolock lock(mMutex);
419 mPool.push_front(std::move(vec));
420}
421
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700422// FlexBuffersImpl
423
Wonsik Kim469c8342019-04-11 16:46:09 -0700424size_t FlexBuffersImpl::assignSlot(const sp<Codec2Buffer> &buffer) {
425 for (size_t i = 0; i < mBuffers.size(); ++i) {
426 if (mBuffers[i].clientBuffer == nullptr
427 && mBuffers[i].compBuffer.expired()) {
428 mBuffers[i].clientBuffer = buffer;
429 return i;
430 }
431 }
432 mBuffers.push_back({ buffer, std::weak_ptr<C2Buffer>() });
433 return mBuffers.size() - 1;
434}
435
Wonsik Kim469c8342019-04-11 16:46:09 -0700436bool FlexBuffersImpl::releaseSlot(
437 const sp<MediaCodecBuffer> &buffer,
438 std::shared_ptr<C2Buffer> *c2buffer,
439 bool release) {
440 sp<Codec2Buffer> clientBuffer;
441 size_t index = mBuffers.size();
442 for (size_t i = 0; i < mBuffers.size(); ++i) {
443 if (mBuffers[i].clientBuffer == buffer) {
444 clientBuffer = mBuffers[i].clientBuffer;
445 if (release) {
446 mBuffers[i].clientBuffer.clear();
447 }
448 index = i;
449 break;
450 }
451 }
452 if (clientBuffer == nullptr) {
453 ALOGV("[%s] %s: No matching buffer found", mName, __func__);
454 return false;
455 }
456 std::shared_ptr<C2Buffer> result = mBuffers[index].compBuffer.lock();
457 if (!result) {
458 result = clientBuffer->asC2Buffer();
Wonsik Kimf9b32122020-04-02 11:30:17 -0700459 clientBuffer->clearC2BufferRefs();
Wonsik Kim469c8342019-04-11 16:46:09 -0700460 mBuffers[index].compBuffer = result;
461 }
462 if (c2buffer) {
463 *c2buffer = result;
464 }
465 return true;
466}
467
468bool FlexBuffersImpl::expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer) {
469 for (size_t i = 0; i < mBuffers.size(); ++i) {
470 std::shared_ptr<C2Buffer> compBuffer =
471 mBuffers[i].compBuffer.lock();
472 if (!compBuffer || compBuffer != c2buffer) {
473 continue;
474 }
475 mBuffers[i].compBuffer.reset();
476 ALOGV("[%s] codec released buffer #%zu", mName, i);
477 return true;
478 }
479 ALOGV("[%s] codec released an unknown buffer", mName);
480 return false;
481}
482
483void FlexBuffersImpl::flush() {
484 ALOGV("[%s] buffers are flushed %zu", mName, mBuffers.size());
485 mBuffers.clear();
486}
487
Wonsik Kim0487b782020-10-28 11:45:50 -0700488size_t FlexBuffersImpl::numActiveSlots() const {
Wonsik Kim469c8342019-04-11 16:46:09 -0700489 return std::count_if(
490 mBuffers.begin(), mBuffers.end(),
491 [](const Entry &entry) {
Wonsik Kim0487b782020-10-28 11:45:50 -0700492 return (entry.clientBuffer != nullptr
493 || !entry.compBuffer.expired());
Wonsik Kim469c8342019-04-11 16:46:09 -0700494 });
495}
496
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700497size_t FlexBuffersImpl::numComponentBuffers() const {
498 return std::count_if(
499 mBuffers.begin(), mBuffers.end(),
500 [](const Entry &entry) {
501 return !entry.compBuffer.expired();
502 });
503}
504
Wonsik Kim469c8342019-04-11 16:46:09 -0700505// BuffersArrayImpl
506
507void BuffersArrayImpl::initialize(
508 const FlexBuffersImpl &impl,
509 size_t minSize,
510 std::function<sp<Codec2Buffer>()> allocate) {
511 mImplName = impl.mImplName + "[N]";
512 mName = mImplName.c_str();
513 for (size_t i = 0; i < impl.mBuffers.size(); ++i) {
514 sp<Codec2Buffer> clientBuffer = impl.mBuffers[i].clientBuffer;
515 bool ownedByClient = (clientBuffer != nullptr);
516 if (!ownedByClient) {
517 clientBuffer = allocate();
518 }
519 mBuffers.push_back({ clientBuffer, impl.mBuffers[i].compBuffer, ownedByClient });
520 }
521 ALOGV("[%s] converted %zu buffers to array mode of %zu", mName, mBuffers.size(), minSize);
522 for (size_t i = impl.mBuffers.size(); i < minSize; ++i) {
523 mBuffers.push_back({ allocate(), std::weak_ptr<C2Buffer>(), false });
524 }
525}
526
527status_t BuffersArrayImpl::grabBuffer(
528 size_t *index,
529 sp<Codec2Buffer> *buffer,
530 std::function<bool(const sp<Codec2Buffer> &)> match) {
531 // allBuffersDontMatch remains true if all buffers are available but
532 // match() returns false for every buffer.
533 bool allBuffersDontMatch = true;
534 for (size_t i = 0; i < mBuffers.size(); ++i) {
535 if (!mBuffers[i].ownedByClient && mBuffers[i].compBuffer.expired()) {
536 if (match(mBuffers[i].clientBuffer)) {
537 mBuffers[i].ownedByClient = true;
538 *buffer = mBuffers[i].clientBuffer;
539 (*buffer)->meta()->clear();
540 (*buffer)->setRange(0, (*buffer)->capacity());
541 *index = i;
542 return OK;
543 }
544 } else {
545 allBuffersDontMatch = false;
546 }
547 }
548 return allBuffersDontMatch ? NO_MEMORY : WOULD_BLOCK;
549}
550
551bool BuffersArrayImpl::returnBuffer(
552 const sp<MediaCodecBuffer> &buffer,
553 std::shared_ptr<C2Buffer> *c2buffer,
554 bool release) {
555 sp<Codec2Buffer> clientBuffer;
556 size_t index = mBuffers.size();
557 for (size_t i = 0; i < mBuffers.size(); ++i) {
558 if (mBuffers[i].clientBuffer == buffer) {
559 if (!mBuffers[i].ownedByClient) {
560 ALOGD("[%s] Client returned a buffer it does not own according to our record: %zu",
561 mName, i);
562 }
563 clientBuffer = mBuffers[i].clientBuffer;
564 if (release) {
565 mBuffers[i].ownedByClient = false;
566 }
567 index = i;
568 break;
569 }
570 }
571 if (clientBuffer == nullptr) {
572 ALOGV("[%s] %s: No matching buffer found", mName, __func__);
573 return false;
574 }
575 ALOGV("[%s] %s: matching buffer found (index=%zu)", mName, __func__, index);
576 std::shared_ptr<C2Buffer> result = mBuffers[index].compBuffer.lock();
577 if (!result) {
578 result = clientBuffer->asC2Buffer();
Wonsik Kimf9b32122020-04-02 11:30:17 -0700579 clientBuffer->clearC2BufferRefs();
Wonsik Kim469c8342019-04-11 16:46:09 -0700580 mBuffers[index].compBuffer = result;
581 }
582 if (c2buffer) {
583 *c2buffer = result;
584 }
585 return true;
586}
587
588bool BuffersArrayImpl::expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer) {
589 for (size_t i = 0; i < mBuffers.size(); ++i) {
590 std::shared_ptr<C2Buffer> compBuffer =
591 mBuffers[i].compBuffer.lock();
592 if (!compBuffer) {
593 continue;
594 }
595 if (c2buffer == compBuffer) {
596 if (mBuffers[i].ownedByClient) {
597 // This should not happen.
598 ALOGD("[%s] codec released a buffer owned by client "
599 "(index %zu)", mName, i);
600 }
601 mBuffers[i].compBuffer.reset();
602 ALOGV("[%s] codec released buffer #%zu(array mode)", mName, i);
603 return true;
604 }
605 }
606 ALOGV("[%s] codec released an unknown buffer (array mode)", mName);
607 return false;
608}
609
610void BuffersArrayImpl::getArray(Vector<sp<MediaCodecBuffer>> *array) const {
611 array->clear();
612 for (const Entry &entry : mBuffers) {
613 array->push(entry.clientBuffer);
614 }
615}
616
617void BuffersArrayImpl::flush() {
618 for (Entry &entry : mBuffers) {
619 entry.ownedByClient = false;
620 }
621}
622
623void BuffersArrayImpl::realloc(std::function<sp<Codec2Buffer>()> alloc) {
624 size_t size = mBuffers.size();
625 mBuffers.clear();
626 for (size_t i = 0; i < size; ++i) {
627 mBuffers.push_back({ alloc(), std::weak_ptr<C2Buffer>(), false });
628 }
629}
630
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700631void BuffersArrayImpl::grow(
632 size_t newSize, std::function<sp<Codec2Buffer>()> alloc) {
633 CHECK_LT(mBuffers.size(), newSize);
634 while (mBuffers.size() < newSize) {
635 mBuffers.push_back({ alloc(), std::weak_ptr<C2Buffer>(), false });
636 }
637}
638
Wonsik Kim0487b782020-10-28 11:45:50 -0700639size_t BuffersArrayImpl::numActiveSlots() const {
Wonsik Kim469c8342019-04-11 16:46:09 -0700640 return std::count_if(
641 mBuffers.begin(), mBuffers.end(),
642 [](const Entry &entry) {
Wonsik Kim0487b782020-10-28 11:45:50 -0700643 return entry.ownedByClient || !entry.compBuffer.expired();
Wonsik Kim469c8342019-04-11 16:46:09 -0700644 });
645}
646
Wonsik Kima39882b2019-06-20 16:13:56 -0700647size_t BuffersArrayImpl::arraySize() const {
648 return mBuffers.size();
649}
650
Wonsik Kim469c8342019-04-11 16:46:09 -0700651// InputBuffersArray
652
653void InputBuffersArray::initialize(
654 const FlexBuffersImpl &impl,
655 size_t minSize,
656 std::function<sp<Codec2Buffer>()> allocate) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700657 mAllocate = allocate;
Wonsik Kim469c8342019-04-11 16:46:09 -0700658 mImpl.initialize(impl, minSize, allocate);
659}
660
661void InputBuffersArray::getArray(Vector<sp<MediaCodecBuffer>> *array) const {
662 mImpl.getArray(array);
663}
664
665bool InputBuffersArray::requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) {
666 sp<Codec2Buffer> c2Buffer;
667 status_t err = mImpl.grabBuffer(index, &c2Buffer);
668 if (err == OK) {
669 c2Buffer->setFormat(mFormat);
670 handleImageData(c2Buffer);
671 *buffer = c2Buffer;
672 return true;
673 }
674 return false;
675}
676
677bool InputBuffersArray::releaseBuffer(
678 const sp<MediaCodecBuffer> &buffer,
679 std::shared_ptr<C2Buffer> *c2buffer,
680 bool release) {
681 return mImpl.returnBuffer(buffer, c2buffer, release);
682}
683
684bool InputBuffersArray::expireComponentBuffer(
685 const std::shared_ptr<C2Buffer> &c2buffer) {
686 return mImpl.expireComponentBuffer(c2buffer);
687}
688
689void InputBuffersArray::flush() {
690 mImpl.flush();
691}
692
Wonsik Kim0487b782020-10-28 11:45:50 -0700693size_t InputBuffersArray::numActiveSlots() const {
694 return mImpl.numActiveSlots();
Wonsik Kim469c8342019-04-11 16:46:09 -0700695}
696
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700697sp<Codec2Buffer> InputBuffersArray::createNewBuffer() {
698 return mAllocate();
699}
700
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800701// SlotInputBuffers
702
703bool SlotInputBuffers::requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) {
704 sp<Codec2Buffer> newBuffer = createNewBuffer();
705 *index = mImpl.assignSlot(newBuffer);
706 *buffer = newBuffer;
707 return true;
708}
709
710bool SlotInputBuffers::releaseBuffer(
711 const sp<MediaCodecBuffer> &buffer,
712 std::shared_ptr<C2Buffer> *c2buffer,
713 bool release) {
714 return mImpl.releaseSlot(buffer, c2buffer, release);
715}
716
717bool SlotInputBuffers::expireComponentBuffer(
718 const std::shared_ptr<C2Buffer> &c2buffer) {
719 return mImpl.expireComponentBuffer(c2buffer);
720}
721
722void SlotInputBuffers::flush() {
723 mImpl.flush();
724}
725
726std::unique_ptr<InputBuffers> SlotInputBuffers::toArrayMode(size_t) {
727 TRESPASS("Array mode should not be called at non-legacy mode");
728 return nullptr;
729}
730
Wonsik Kim0487b782020-10-28 11:45:50 -0700731size_t SlotInputBuffers::numActiveSlots() const {
732 return mImpl.numActiveSlots();
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800733}
734
735sp<Codec2Buffer> SlotInputBuffers::createNewBuffer() {
736 return new DummyContainerBuffer{mFormat, nullptr};
737}
738
Wonsik Kim469c8342019-04-11 16:46:09 -0700739// LinearInputBuffers
740
741bool LinearInputBuffers::requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700742 sp<Codec2Buffer> newBuffer = createNewBuffer();
Wonsik Kim469c8342019-04-11 16:46:09 -0700743 if (newBuffer == nullptr) {
744 return false;
745 }
746 *index = mImpl.assignSlot(newBuffer);
747 *buffer = newBuffer;
748 return true;
749}
750
751bool LinearInputBuffers::releaseBuffer(
752 const sp<MediaCodecBuffer> &buffer,
753 std::shared_ptr<C2Buffer> *c2buffer,
754 bool release) {
755 return mImpl.releaseSlot(buffer, c2buffer, release);
756}
757
758bool LinearInputBuffers::expireComponentBuffer(
759 const std::shared_ptr<C2Buffer> &c2buffer) {
760 return mImpl.expireComponentBuffer(c2buffer);
761}
762
763void LinearInputBuffers::flush() {
764 // This is no-op by default unless we're in array mode where we need to keep
765 // track of the flushed work.
766 mImpl.flush();
767}
768
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700769std::unique_ptr<InputBuffers> LinearInputBuffers::toArrayMode(size_t size) {
Wonsik Kim469c8342019-04-11 16:46:09 -0700770 std::unique_ptr<InputBuffersArray> array(
771 new InputBuffersArray(mComponentName.c_str(), "1D-Input[N]"));
772 array->setPool(mPool);
773 array->setFormat(mFormat);
774 array->initialize(
775 mImpl,
776 size,
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700777 [pool = mPool, format = mFormat] () -> sp<Codec2Buffer> {
778 return Alloc(pool, format);
779 });
Wonsik Kim469c8342019-04-11 16:46:09 -0700780 return std::move(array);
781}
782
Wonsik Kim0487b782020-10-28 11:45:50 -0700783size_t LinearInputBuffers::numActiveSlots() const {
784 return mImpl.numActiveSlots();
Wonsik Kim469c8342019-04-11 16:46:09 -0700785}
786
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700787// static
788sp<Codec2Buffer> LinearInputBuffers::Alloc(
789 const std::shared_ptr<C2BlockPool> &pool, const sp<AMessage> &format) {
790 int32_t capacity = kLinearBufferSize;
791 (void)format->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
792 if ((size_t)capacity > kMaxLinearBufferSize) {
793 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
794 capacity = kMaxLinearBufferSize;
795 }
796
Wonsik Kim666604a2020-05-14 16:57:49 -0700797 int64_t usageValue = 0;
798 (void)format->findInt64("android._C2MemoryUsage", &usageValue);
799 C2MemoryUsage usage{usageValue | C2MemoryUsage::CPU_READ | C2MemoryUsage::CPU_WRITE};
Wonsik Kim469c8342019-04-11 16:46:09 -0700800 std::shared_ptr<C2LinearBlock> block;
801
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700802 c2_status_t err = pool->fetchLinearBlock(capacity, usage, &block);
Wonsik Kim469c8342019-04-11 16:46:09 -0700803 if (err != C2_OK) {
804 return nullptr;
805 }
806
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700807 return LinearBlockBuffer::Allocate(format, block);
808}
809
810sp<Codec2Buffer> LinearInputBuffers::createNewBuffer() {
811 return Alloc(mPool, mFormat);
Wonsik Kim469c8342019-04-11 16:46:09 -0700812}
813
814// EncryptedLinearInputBuffers
815
816EncryptedLinearInputBuffers::EncryptedLinearInputBuffers(
817 bool secure,
818 const sp<MemoryDealer> &dealer,
819 const sp<ICrypto> &crypto,
820 int32_t heapSeqNum,
821 size_t capacity,
822 size_t numInputSlots,
823 const char *componentName, const char *name)
824 : LinearInputBuffers(componentName, name),
825 mUsage({0, 0}),
826 mDealer(dealer),
827 mCrypto(crypto),
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700828 mMemoryVector(new std::vector<Entry>){
Wonsik Kim469c8342019-04-11 16:46:09 -0700829 if (secure) {
830 mUsage = { C2MemoryUsage::READ_PROTECTED, 0 };
831 } else {
832 mUsage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
833 }
834 for (size_t i = 0; i < numInputSlots; ++i) {
835 sp<IMemory> memory = mDealer->allocate(capacity);
836 if (memory == nullptr) {
837 ALOGD("[%s] Failed to allocate memory from dealer: only %zu slots allocated",
838 mName, i);
839 break;
840 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700841 mMemoryVector->push_back({std::weak_ptr<C2LinearBlock>(), memory, heapSeqNum});
Wonsik Kim469c8342019-04-11 16:46:09 -0700842 }
843}
844
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700845std::unique_ptr<InputBuffers> EncryptedLinearInputBuffers::toArrayMode(size_t size) {
846 std::unique_ptr<InputBuffersArray> array(
847 new InputBuffersArray(mComponentName.c_str(), "1D-EncryptedInput[N]"));
848 array->setPool(mPool);
849 array->setFormat(mFormat);
850 array->initialize(
851 mImpl,
852 size,
853 [pool = mPool,
854 format = mFormat,
855 usage = mUsage,
856 memoryVector = mMemoryVector] () -> sp<Codec2Buffer> {
857 return Alloc(pool, format, usage, memoryVector);
858 });
859 return std::move(array);
860}
861
862
863// static
864sp<Codec2Buffer> EncryptedLinearInputBuffers::Alloc(
865 const std::shared_ptr<C2BlockPool> &pool,
866 const sp<AMessage> &format,
867 C2MemoryUsage usage,
868 const std::shared_ptr<std::vector<EncryptedLinearInputBuffers::Entry>> &memoryVector) {
869 int32_t capacity = kLinearBufferSize;
870 (void)format->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
871 if ((size_t)capacity > kMaxLinearBufferSize) {
872 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
873 capacity = kMaxLinearBufferSize;
874 }
875
Wonsik Kim469c8342019-04-11 16:46:09 -0700876 sp<IMemory> memory;
877 size_t slot = 0;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700878 int32_t heapSeqNum = -1;
879 for (; slot < memoryVector->size(); ++slot) {
880 if (memoryVector->at(slot).block.expired()) {
881 memory = memoryVector->at(slot).memory;
882 heapSeqNum = memoryVector->at(slot).heapSeqNum;
Wonsik Kim469c8342019-04-11 16:46:09 -0700883 break;
884 }
885 }
886 if (memory == nullptr) {
887 return nullptr;
888 }
889
890 std::shared_ptr<C2LinearBlock> block;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700891 c2_status_t err = pool->fetchLinearBlock(capacity, usage, &block);
Wonsik Kim469c8342019-04-11 16:46:09 -0700892 if (err != C2_OK || block == nullptr) {
893 return nullptr;
894 }
895
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700896 memoryVector->at(slot).block = block;
897 return new EncryptedLinearBlockBuffer(format, block, memory, heapSeqNum);
898}
899
900sp<Codec2Buffer> EncryptedLinearInputBuffers::createNewBuffer() {
901 // TODO: android_2020
902 return nullptr;
Wonsik Kim469c8342019-04-11 16:46:09 -0700903}
904
905// GraphicMetadataInputBuffers
906
907GraphicMetadataInputBuffers::GraphicMetadataInputBuffers(
908 const char *componentName, const char *name)
909 : InputBuffers(componentName, name),
910 mImpl(mName),
911 mStore(GetCodec2PlatformAllocatorStore()) { }
912
913bool GraphicMetadataInputBuffers::requestNewBuffer(
914 size_t *index, sp<MediaCodecBuffer> *buffer) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700915 sp<Codec2Buffer> newBuffer = createNewBuffer();
Wonsik Kim469c8342019-04-11 16:46:09 -0700916 if (newBuffer == nullptr) {
917 return false;
918 }
919 *index = mImpl.assignSlot(newBuffer);
920 *buffer = newBuffer;
921 return true;
922}
923
924bool GraphicMetadataInputBuffers::releaseBuffer(
925 const sp<MediaCodecBuffer> &buffer,
926 std::shared_ptr<C2Buffer> *c2buffer,
927 bool release) {
928 return mImpl.releaseSlot(buffer, c2buffer, release);
929}
930
931bool GraphicMetadataInputBuffers::expireComponentBuffer(
932 const std::shared_ptr<C2Buffer> &c2buffer) {
933 return mImpl.expireComponentBuffer(c2buffer);
934}
935
936void GraphicMetadataInputBuffers::flush() {
937 // This is no-op by default unless we're in array mode where we need to keep
938 // track of the flushed work.
939}
940
941std::unique_ptr<InputBuffers> GraphicMetadataInputBuffers::toArrayMode(
942 size_t size) {
943 std::shared_ptr<C2Allocator> alloc;
944 c2_status_t err = mStore->fetchAllocator(mPool->getAllocatorId(), &alloc);
945 if (err != C2_OK) {
946 return nullptr;
947 }
948 std::unique_ptr<InputBuffersArray> array(
949 new InputBuffersArray(mComponentName.c_str(), "2D-MetaInput[N]"));
950 array->setPool(mPool);
951 array->setFormat(mFormat);
952 array->initialize(
953 mImpl,
954 size,
955 [format = mFormat, alloc]() -> sp<Codec2Buffer> {
956 return new GraphicMetadataBuffer(format, alloc);
957 });
958 return std::move(array);
959}
960
Wonsik Kim0487b782020-10-28 11:45:50 -0700961size_t GraphicMetadataInputBuffers::numActiveSlots() const {
962 return mImpl.numActiveSlots();
Wonsik Kim469c8342019-04-11 16:46:09 -0700963}
964
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700965sp<Codec2Buffer> GraphicMetadataInputBuffers::createNewBuffer() {
966 std::shared_ptr<C2Allocator> alloc;
967 c2_status_t err = mStore->fetchAllocator(mPool->getAllocatorId(), &alloc);
968 if (err != C2_OK) {
969 return nullptr;
970 }
971 return new GraphicMetadataBuffer(mFormat, alloc);
972}
973
Wonsik Kim469c8342019-04-11 16:46:09 -0700974// GraphicInputBuffers
975
976GraphicInputBuffers::GraphicInputBuffers(
Wonsik Kim41d83432020-04-27 16:40:49 -0700977 const char *componentName, const char *name)
Wonsik Kim469c8342019-04-11 16:46:09 -0700978 : InputBuffers(componentName, name),
979 mImpl(mName),
Wonsik Kim41d83432020-04-27 16:40:49 -0700980 mLocalBufferPool(LocalBufferPool::Create()) { }
Wonsik Kim469c8342019-04-11 16:46:09 -0700981
982bool GraphicInputBuffers::requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700983 sp<Codec2Buffer> newBuffer = createNewBuffer();
Wonsik Kim469c8342019-04-11 16:46:09 -0700984 if (newBuffer == nullptr) {
985 return false;
986 }
987 *index = mImpl.assignSlot(newBuffer);
988 handleImageData(newBuffer);
989 *buffer = newBuffer;
990 return true;
991}
992
993bool GraphicInputBuffers::releaseBuffer(
994 const sp<MediaCodecBuffer> &buffer,
995 std::shared_ptr<C2Buffer> *c2buffer,
996 bool release) {
997 return mImpl.releaseSlot(buffer, c2buffer, release);
998}
999
1000bool GraphicInputBuffers::expireComponentBuffer(
1001 const std::shared_ptr<C2Buffer> &c2buffer) {
1002 return mImpl.expireComponentBuffer(c2buffer);
1003}
1004
1005void GraphicInputBuffers::flush() {
1006 // This is no-op by default unless we're in array mode where we need to keep
1007 // track of the flushed work.
1008}
1009
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001010static uint32_t extractPixelFormat(const sp<AMessage> &format) {
1011 int32_t frameworkColorFormat = 0;
1012 if (!format->findInt32("android._color-format", &frameworkColorFormat)) {
1013 return PIXEL_FORMAT_UNKNOWN;
1014 }
1015 uint32_t pixelFormat = PIXEL_FORMAT_UNKNOWN;
1016 if (C2Mapper::mapPixelFormatFrameworkToCodec(frameworkColorFormat, &pixelFormat)) {
1017 return pixelFormat;
1018 }
1019 return PIXEL_FORMAT_UNKNOWN;
1020}
1021
Wonsik Kim469c8342019-04-11 16:46:09 -07001022std::unique_ptr<InputBuffers> GraphicInputBuffers::toArrayMode(size_t size) {
1023 std::unique_ptr<InputBuffersArray> array(
1024 new InputBuffersArray(mComponentName.c_str(), "2D-BB-Input[N]"));
1025 array->setPool(mPool);
1026 array->setFormat(mFormat);
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001027 uint32_t pixelFormat = extractPixelFormat(mFormat);
Wonsik Kim469c8342019-04-11 16:46:09 -07001028 array->initialize(
1029 mImpl,
1030 size,
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001031 [pool = mPool, format = mFormat, lbp = mLocalBufferPool, pixelFormat]()
1032 -> sp<Codec2Buffer> {
Wonsik Kim469c8342019-04-11 16:46:09 -07001033 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
1034 return AllocateGraphicBuffer(
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001035 pool, format, pixelFormat, usage, lbp);
Wonsik Kim469c8342019-04-11 16:46:09 -07001036 });
1037 return std::move(array);
1038}
1039
Wonsik Kim0487b782020-10-28 11:45:50 -07001040size_t GraphicInputBuffers::numActiveSlots() const {
1041 return mImpl.numActiveSlots();
Wonsik Kim469c8342019-04-11 16:46:09 -07001042}
1043
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001044sp<Codec2Buffer> GraphicInputBuffers::createNewBuffer() {
Wonsik Kim666604a2020-05-14 16:57:49 -07001045 int64_t usageValue = 0;
1046 (void)mFormat->findInt64("android._C2MemoryUsage", &usageValue);
1047 C2MemoryUsage usage{usageValue | C2MemoryUsage::CPU_READ | C2MemoryUsage::CPU_WRITE};
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001048 return AllocateGraphicBuffer(
Wonsik Kimd79ee1f2020-08-27 17:41:56 -07001049 mPool, mFormat, extractPixelFormat(mFormat), usage, mLocalBufferPool);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001050}
1051
Wonsik Kim469c8342019-04-11 16:46:09 -07001052// OutputBuffersArray
1053
1054void OutputBuffersArray::initialize(
1055 const FlexBuffersImpl &impl,
1056 size_t minSize,
1057 std::function<sp<Codec2Buffer>()> allocate) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001058 mAlloc = allocate;
Wonsik Kim469c8342019-04-11 16:46:09 -07001059 mImpl.initialize(impl, minSize, allocate);
1060}
1061
1062status_t OutputBuffersArray::registerBuffer(
1063 const std::shared_ptr<C2Buffer> &buffer,
1064 size_t *index,
1065 sp<MediaCodecBuffer> *clientBuffer) {
1066 sp<Codec2Buffer> c2Buffer;
1067 status_t err = mImpl.grabBuffer(
1068 index,
1069 &c2Buffer,
1070 [buffer](const sp<Codec2Buffer> &clientBuffer) {
1071 return clientBuffer->canCopy(buffer);
1072 });
1073 if (err == WOULD_BLOCK) {
1074 ALOGV("[%s] buffers temporarily not available", mName);
1075 return err;
1076 } else if (err != OK) {
1077 ALOGD("[%s] grabBuffer failed: %d", mName, err);
1078 return err;
1079 }
1080 c2Buffer->setFormat(mFormat);
1081 if (!c2Buffer->copy(buffer)) {
1082 ALOGD("[%s] copy buffer failed", mName);
1083 return WOULD_BLOCK;
1084 }
1085 submit(c2Buffer);
1086 handleImageData(c2Buffer);
1087 *clientBuffer = c2Buffer;
1088 ALOGV("[%s] grabbed buffer %zu", mName, *index);
1089 return OK;
1090}
1091
1092status_t OutputBuffersArray::registerCsd(
1093 const C2StreamInitDataInfo::output *csd,
1094 size_t *index,
1095 sp<MediaCodecBuffer> *clientBuffer) {
1096 sp<Codec2Buffer> c2Buffer;
1097 status_t err = mImpl.grabBuffer(
1098 index,
1099 &c2Buffer,
1100 [csd](const sp<Codec2Buffer> &clientBuffer) {
1101 return clientBuffer->base() != nullptr
1102 && clientBuffer->capacity() >= csd->flexCount();
1103 });
1104 if (err != OK) {
1105 return err;
1106 }
1107 memcpy(c2Buffer->base(), csd->m.value, csd->flexCount());
1108 c2Buffer->setRange(0, csd->flexCount());
1109 c2Buffer->setFormat(mFormat);
1110 *clientBuffer = c2Buffer;
1111 return OK;
1112}
1113
1114bool OutputBuffersArray::releaseBuffer(
1115 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) {
1116 return mImpl.returnBuffer(buffer, c2buffer, true);
1117}
1118
1119void OutputBuffersArray::flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) {
1120 (void)flushedWork;
1121 mImpl.flush();
1122 if (mSkipCutBuffer != nullptr) {
1123 mSkipCutBuffer->clear();
1124 }
1125}
1126
1127void OutputBuffersArray::getArray(Vector<sp<MediaCodecBuffer>> *array) const {
1128 mImpl.getArray(array);
1129}
1130
Wonsik Kim0487b782020-10-28 11:45:50 -07001131size_t OutputBuffersArray::numActiveSlots() const {
1132 return mImpl.numActiveSlots();
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001133}
1134
Wonsik Kim469c8342019-04-11 16:46:09 -07001135void OutputBuffersArray::realloc(const std::shared_ptr<C2Buffer> &c2buffer) {
Wonsik Kim469c8342019-04-11 16:46:09 -07001136 switch (c2buffer->data().type()) {
1137 case C2BufferData::LINEAR: {
1138 uint32_t size = kLinearBufferSize;
Nick Desaulniersd09eaea2019-10-07 20:19:39 -07001139 const std::vector<C2ConstLinearBlock> &linear_blocks = c2buffer->data().linearBlocks();
1140 const uint32_t block_size = linear_blocks.front().size();
1141 if (block_size < kMaxLinearBufferSize / 2) {
1142 size = block_size * 2;
Wonsik Kim469c8342019-04-11 16:46:09 -07001143 } else {
1144 size = kMaxLinearBufferSize;
1145 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001146 mAlloc = [format = mFormat, size] {
Wonsik Kim469c8342019-04-11 16:46:09 -07001147 return new LocalLinearBuffer(format, new ABuffer(size));
1148 };
Wonsik Kima39882b2019-06-20 16:13:56 -07001149 ALOGD("[%s] reallocating with linear buffer of size %u", mName, size);
Wonsik Kim469c8342019-04-11 16:46:09 -07001150 break;
1151 }
1152
Wonsik Kima39882b2019-06-20 16:13:56 -07001153 case C2BufferData::GRAPHIC: {
1154 // This is only called for RawGraphicOutputBuffers.
1155 mAlloc = [format = mFormat,
Wonsik Kim41d83432020-04-27 16:40:49 -07001156 lbp = LocalBufferPool::Create()] {
Wonsik Kima39882b2019-06-20 16:13:56 -07001157 return ConstGraphicBlockBuffer::AllocateEmpty(
1158 format,
1159 [lbp](size_t capacity) {
1160 return lbp->newBuffer(capacity);
1161 });
1162 };
1163 ALOGD("[%s] reallocating with graphic buffer: format = %s",
1164 mName, mFormat->debugString().c_str());
1165 break;
1166 }
Wonsik Kim469c8342019-04-11 16:46:09 -07001167
1168 case C2BufferData::INVALID: [[fallthrough]];
1169 case C2BufferData::LINEAR_CHUNKS: [[fallthrough]];
1170 case C2BufferData::GRAPHIC_CHUNKS: [[fallthrough]];
1171 default:
1172 ALOGD("Unsupported type: %d", (int)c2buffer->data().type());
1173 return;
1174 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001175 mImpl.realloc(mAlloc);
Wonsik Kim469c8342019-04-11 16:46:09 -07001176}
1177
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001178void OutputBuffersArray::grow(size_t newSize) {
1179 mImpl.grow(newSize, mAlloc);
Wonsik Kim469c8342019-04-11 16:46:09 -07001180}
1181
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001182void OutputBuffersArray::transferFrom(OutputBuffers* source) {
1183 mFormat = source->mFormat;
1184 mSkipCutBuffer = source->mSkipCutBuffer;
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001185 mPending = std::move(source->mPending);
1186 mReorderStash = std::move(source->mReorderStash);
1187 mDepth = source->mDepth;
1188 mKey = source->mKey;
1189}
1190
Wonsik Kim469c8342019-04-11 16:46:09 -07001191// FlexOutputBuffers
1192
1193status_t FlexOutputBuffers::registerBuffer(
1194 const std::shared_ptr<C2Buffer> &buffer,
1195 size_t *index,
1196 sp<MediaCodecBuffer> *clientBuffer) {
1197 sp<Codec2Buffer> newBuffer = wrap(buffer);
1198 if (newBuffer == nullptr) {
1199 return NO_MEMORY;
1200 }
1201 newBuffer->setFormat(mFormat);
1202 *index = mImpl.assignSlot(newBuffer);
1203 handleImageData(newBuffer);
1204 *clientBuffer = newBuffer;
1205 ALOGV("[%s] registered buffer %zu", mName, *index);
1206 return OK;
1207}
1208
1209status_t FlexOutputBuffers::registerCsd(
1210 const C2StreamInitDataInfo::output *csd,
1211 size_t *index,
1212 sp<MediaCodecBuffer> *clientBuffer) {
1213 sp<Codec2Buffer> newBuffer = new LocalLinearBuffer(
1214 mFormat, ABuffer::CreateAsCopy(csd->m.value, csd->flexCount()));
1215 *index = mImpl.assignSlot(newBuffer);
1216 *clientBuffer = newBuffer;
1217 return OK;
1218}
1219
1220bool FlexOutputBuffers::releaseBuffer(
1221 const sp<MediaCodecBuffer> &buffer,
1222 std::shared_ptr<C2Buffer> *c2buffer) {
1223 return mImpl.releaseSlot(buffer, c2buffer, true);
1224}
1225
1226void FlexOutputBuffers::flush(
1227 const std::list<std::unique_ptr<C2Work>> &flushedWork) {
1228 (void) flushedWork;
1229 // This is no-op by default unless we're in array mode where we need to keep
1230 // track of the flushed work.
1231}
1232
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001233std::unique_ptr<OutputBuffersArray> FlexOutputBuffers::toArrayMode(size_t size) {
Wonsik Kim469c8342019-04-11 16:46:09 -07001234 std::unique_ptr<OutputBuffersArray> array(new OutputBuffersArray(mComponentName.c_str()));
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001235 array->transferFrom(this);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001236 std::function<sp<Codec2Buffer>()> alloc = getAlloc();
1237 array->initialize(mImpl, size, alloc);
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001238 return array;
Wonsik Kim469c8342019-04-11 16:46:09 -07001239}
1240
Wonsik Kim0487b782020-10-28 11:45:50 -07001241size_t FlexOutputBuffers::numActiveSlots() const {
1242 return mImpl.numActiveSlots();
Wonsik Kim469c8342019-04-11 16:46:09 -07001243}
1244
1245// LinearOutputBuffers
1246
1247void LinearOutputBuffers::flush(
1248 const std::list<std::unique_ptr<C2Work>> &flushedWork) {
1249 if (mSkipCutBuffer != nullptr) {
1250 mSkipCutBuffer->clear();
1251 }
1252 FlexOutputBuffers::flush(flushedWork);
1253}
1254
1255sp<Codec2Buffer> LinearOutputBuffers::wrap(const std::shared_ptr<C2Buffer> &buffer) {
1256 if (buffer == nullptr) {
1257 ALOGV("[%s] using a dummy buffer", mName);
1258 return new LocalLinearBuffer(mFormat, new ABuffer(0));
1259 }
1260 if (buffer->data().type() != C2BufferData::LINEAR) {
1261 ALOGV("[%s] non-linear buffer %d", mName, buffer->data().type());
1262 // We expect linear output buffers from the component.
1263 return nullptr;
1264 }
1265 if (buffer->data().linearBlocks().size() != 1u) {
1266 ALOGV("[%s] no linear buffers", mName);
1267 // We expect one and only one linear block from the component.
1268 return nullptr;
1269 }
1270 sp<Codec2Buffer> clientBuffer = ConstLinearBlockBuffer::Allocate(mFormat, buffer);
1271 if (clientBuffer == nullptr) {
1272 ALOGD("[%s] ConstLinearBlockBuffer::Allocate failed", mName);
1273 return nullptr;
1274 }
1275 submit(clientBuffer);
1276 return clientBuffer;
1277}
1278
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001279std::function<sp<Codec2Buffer>()> LinearOutputBuffers::getAlloc() {
1280 return [format = mFormat]{
1281 // TODO: proper max output size
1282 return new LocalLinearBuffer(format, new ABuffer(kLinearBufferSize));
1283 };
Wonsik Kim469c8342019-04-11 16:46:09 -07001284}
1285
1286// GraphicOutputBuffers
1287
1288sp<Codec2Buffer> GraphicOutputBuffers::wrap(const std::shared_ptr<C2Buffer> &buffer) {
1289 return new DummyContainerBuffer(mFormat, buffer);
1290}
1291
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001292std::function<sp<Codec2Buffer>()> GraphicOutputBuffers::getAlloc() {
1293 return [format = mFormat]{
1294 return new DummyContainerBuffer(format);
1295 };
Wonsik Kim469c8342019-04-11 16:46:09 -07001296}
1297
1298// RawGraphicOutputBuffers
1299
1300RawGraphicOutputBuffers::RawGraphicOutputBuffers(
Wonsik Kim41d83432020-04-27 16:40:49 -07001301 const char *componentName, const char *name)
Wonsik Kim469c8342019-04-11 16:46:09 -07001302 : FlexOutputBuffers(componentName, name),
Wonsik Kim41d83432020-04-27 16:40:49 -07001303 mLocalBufferPool(LocalBufferPool::Create()) { }
Wonsik Kim469c8342019-04-11 16:46:09 -07001304
1305sp<Codec2Buffer> RawGraphicOutputBuffers::wrap(const std::shared_ptr<C2Buffer> &buffer) {
1306 if (buffer == nullptr) {
Wonsik Kim6f116902021-07-14 08:58:07 -07001307 return new Codec2Buffer(mFormat, new ABuffer(nullptr, 0));
Wonsik Kim469c8342019-04-11 16:46:09 -07001308 } else {
1309 return ConstGraphicBlockBuffer::Allocate(
1310 mFormat,
1311 buffer,
1312 [lbp = mLocalBufferPool](size_t capacity) {
1313 return lbp->newBuffer(capacity);
1314 });
1315 }
1316}
1317
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001318std::function<sp<Codec2Buffer>()> RawGraphicOutputBuffers::getAlloc() {
1319 return [format = mFormat, lbp = mLocalBufferPool]{
1320 return ConstGraphicBlockBuffer::AllocateEmpty(
1321 format,
1322 [lbp](size_t capacity) {
1323 return lbp->newBuffer(capacity);
1324 });
1325 };
Wonsik Kim469c8342019-04-11 16:46:09 -07001326}
1327
1328} // namespace android