blob: 432b8c00ee05284085e95730f3fdd343cf48421e [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright 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 "CCodecBufferChannel"
19#include <utils/Log.h>
20
21#include <numeric>
22
23#include <C2AllocatorGralloc.h>
24#include <C2PlatformSupport.h>
25#include <C2BlockInternal.h>
26#include <C2Config.h>
27#include <C2Debug.h>
28
29#include <android/hardware/cas/native/1.0/IDescrambler.h>
30#include <android-base/stringprintf.h>
31#include <binder/MemoryDealer.h>
32#include <gui/Surface.h>
33#include <media/openmax/OMX_Core.h>
34#include <media/stagefright/foundation/ABuffer.h>
35#include <media/stagefright/foundation/ALookup.h>
36#include <media/stagefright/foundation/AMessage.h>
37#include <media/stagefright/foundation/AUtils.h>
38#include <media/stagefright/foundation/hexdump.h>
39#include <media/stagefright/MediaCodec.h>
40#include <media/stagefright/MediaCodecConstants.h>
41#include <media/MediaCodecBuffer.h>
42#include <system/window.h>
43
44#include "CCodecBufferChannel.h"
45#include "Codec2Buffer.h"
46#include "SkipCutBuffer.h"
47
48namespace android {
49
50using android::base::StringPrintf;
51using hardware::hidl_handle;
52using hardware::hidl_string;
53using hardware::hidl_vec;
54using namespace hardware::cas::V1_0;
55using namespace hardware::cas::native::V1_0;
56
57using CasStatus = hardware::cas::V1_0::Status;
58
59/**
60 * Base class for representation of buffers at one port.
61 */
62class CCodecBufferChannel::Buffers {
63public:
64 Buffers(const char *componentName, const char *name = "Buffers")
65 : mComponentName(componentName),
66 mChannelName(std::string(componentName) + ":" + name),
67 mName(mChannelName.c_str()) {
68 }
69 virtual ~Buffers() = default;
70
71 /**
72 * Set format for MediaCodec-facing buffers.
73 */
74 void setFormat(const sp<AMessage> &format) {
75 CHECK(format != nullptr);
76 mFormat = format;
77 }
78
79 /**
80 * Return a copy of current format.
81 */
82 sp<AMessage> dupFormat() {
83 return mFormat != nullptr ? mFormat->dup() : nullptr;
84 }
85
86 /**
87 * Returns true if the buffers are operating under array mode.
88 */
89 virtual bool isArrayMode() const { return false; }
90
91 /**
92 * Fills the vector with MediaCodecBuffer's if in array mode; otherwise,
93 * no-op.
94 */
95 virtual void getArray(Vector<sp<MediaCodecBuffer>> *) const {}
96
97protected:
98 std::string mComponentName; ///< name of component for debugging
99 std::string mChannelName; ///< name of channel for debugging
100 const char *mName; ///< C-string version of channel name
101 // Format to be used for creating MediaCodec-facing buffers.
102 sp<AMessage> mFormat;
103
104private:
105 DISALLOW_EVIL_CONSTRUCTORS(Buffers);
106};
107
108class CCodecBufferChannel::InputBuffers : public CCodecBufferChannel::Buffers {
109public:
110 InputBuffers(const char *componentName, const char *name = "Input[]")
111 : Buffers(componentName, name) { }
112 virtual ~InputBuffers() = default;
113
114 /**
115 * Set a block pool to obtain input memory blocks.
116 */
117 void setPool(const std::shared_ptr<C2BlockPool> &pool) { mPool = pool; }
118
119 /**
120 * Get a new MediaCodecBuffer for input and its corresponding index.
121 * Returns false if no new buffer can be obtained at the moment.
122 */
123 virtual bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) = 0;
124
125 /**
126 * Release the buffer obtained from requestNewBuffer() and get the
127 * associated C2Buffer object back. Returns true if the buffer was on file
128 * and released successfully.
129 */
130 virtual bool releaseBuffer(
131 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) = 0;
132
133 /**
134 * Release the buffer that is no longer used by the codec process. Return
135 * true if and only if the buffer was on file and released successfully.
136 */
137 virtual bool expireComponentBuffer(
138 const std::shared_ptr<C2Buffer> &c2buffer) = 0;
139
140 /**
141 * Flush internal state. After this call, no index or buffer previously
142 * returned from requestNewBuffer() is valid.
143 */
144 virtual void flush() = 0;
145
146 /**
147 * Return array-backed version of input buffers. The returned object
148 * shall retain the internal state so that it will honor index and
149 * buffer from previous calls of requestNewBuffer().
150 */
151 virtual std::unique_ptr<InputBuffers> toArrayMode(size_t size) = 0;
152
153protected:
154 // Pool to obtain blocks for input buffers.
155 std::shared_ptr<C2BlockPool> mPool;
156
157private:
158 DISALLOW_EVIL_CONSTRUCTORS(InputBuffers);
159};
160
161class CCodecBufferChannel::OutputBuffers : public CCodecBufferChannel::Buffers {
162public:
163 OutputBuffers(const char *componentName, const char *name = "Output")
164 : Buffers(componentName, name) { }
165 virtual ~OutputBuffers() = default;
166
167 /**
168 * Register output C2Buffer from the component and obtain corresponding
169 * index and MediaCodecBuffer object. Returns false if registration
170 * fails.
171 */
172 virtual status_t registerBuffer(
173 const std::shared_ptr<C2Buffer> &buffer,
174 size_t *index,
175 sp<MediaCodecBuffer> *clientBuffer) = 0;
176
177 /**
178 * Register codec specific data as a buffer to be consistent with
179 * MediaCodec behavior.
180 */
181 virtual status_t registerCsd(
182 const C2StreamCsdInfo::output * /* csd */,
183 size_t * /* index */,
184 sp<MediaCodecBuffer> * /* clientBuffer */) = 0;
185
186 /**
187 * Release the buffer obtained from registerBuffer() and get the
188 * associated C2Buffer object back. Returns true if the buffer was on file
189 * and released successfully.
190 */
191 virtual bool releaseBuffer(
192 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) = 0;
193
194 /**
195 * Flush internal state. After this call, no index or buffer previously
196 * returned from registerBuffer() is valid.
197 */
198 virtual void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) = 0;
199
200 /**
201 * Return array-backed version of output buffers. The returned object
202 * shall retain the internal state so that it will honor index and
203 * buffer from previous calls of registerBuffer().
204 */
205 virtual std::unique_ptr<OutputBuffers> toArrayMode(size_t size) = 0;
206
207 /**
208 * Initialize SkipCutBuffer object.
209 */
210 void initSkipCutBuffer(
211 int32_t delay, int32_t padding, int32_t sampleRate, int32_t channelCount) {
212 CHECK(mSkipCutBuffer == nullptr);
213 mDelay = delay;
214 mPadding = padding;
215 mSampleRate = sampleRate;
216 setSkipCutBuffer(delay, padding, channelCount);
217 }
218
219 /**
220 * Update the SkipCutBuffer object. No-op if it's never initialized.
221 */
222 void updateSkipCutBuffer(int32_t sampleRate, int32_t channelCount) {
223 if (mSkipCutBuffer == nullptr) {
224 return;
225 }
226 int32_t delay = mDelay;
227 int32_t padding = mPadding;
228 if (sampleRate != mSampleRate) {
229 delay = ((int64_t)delay * sampleRate) / mSampleRate;
230 padding = ((int64_t)padding * sampleRate) / mSampleRate;
231 }
232 setSkipCutBuffer(delay, padding, channelCount);
233 }
234
235 /**
236 * Submit buffer to SkipCutBuffer object, if initialized.
237 */
238 void submit(const sp<MediaCodecBuffer> &buffer) {
239 if (mSkipCutBuffer != nullptr) {
240 mSkipCutBuffer->submit(buffer);
241 }
242 }
243
244 /**
245 * Transfer SkipCutBuffer object to the other Buffers object.
246 */
247 void transferSkipCutBuffer(const sp<SkipCutBuffer> &scb) {
248 mSkipCutBuffer = scb;
249 }
250
251protected:
252 sp<SkipCutBuffer> mSkipCutBuffer;
253
254private:
255 int32_t mDelay;
256 int32_t mPadding;
257 int32_t mSampleRate;
258
259 void setSkipCutBuffer(int32_t skip, int32_t cut, int32_t channelCount) {
260 if (mSkipCutBuffer != nullptr) {
261 size_t prevSize = mSkipCutBuffer->size();
262 if (prevSize != 0u) {
263 ALOGD("[%s] Replacing SkipCutBuffer holding %zu bytes", mName, prevSize);
264 }
265 }
266 mSkipCutBuffer = new SkipCutBuffer(skip, cut, channelCount);
267 }
268
269 DISALLOW_EVIL_CONSTRUCTORS(OutputBuffers);
270};
271
272namespace {
273
274// TODO: get this info from component
275const static size_t kMinInputBufferArraySize = 4;
276const static size_t kMaxPipelineCapacity = 18;
277const static size_t kChannelOutputDelay = 0;
278const static size_t kMinOutputBufferArraySize = kMaxPipelineCapacity +
279 kChannelOutputDelay;
280const static size_t kLinearBufferSize = 1048576;
281// This can fit 4K RGBA frame, and most likely client won't need more than this.
282const static size_t kMaxLinearBufferSize = 3840 * 2160 * 4;
283
284/**
285 * Simple local buffer pool backed by std::vector.
286 */
287class LocalBufferPool : public std::enable_shared_from_this<LocalBufferPool> {
288public:
289 /**
290 * Create a new LocalBufferPool object.
291 *
292 * \param poolCapacity max total size of buffers managed by this pool.
293 *
294 * \return a newly created pool object.
295 */
296 static std::shared_ptr<LocalBufferPool> Create(size_t poolCapacity) {
297 return std::shared_ptr<LocalBufferPool>(new LocalBufferPool(poolCapacity));
298 }
299
300 /**
301 * Return an ABuffer object whose size is at least |capacity|.
302 *
303 * \param capacity requested capacity
304 * \return nullptr if the pool capacity is reached
305 * an ABuffer object otherwise.
306 */
307 sp<ABuffer> newBuffer(size_t capacity) {
308 Mutex::Autolock lock(mMutex);
309 auto it = std::find_if(
310 mPool.begin(), mPool.end(),
311 [capacity](const std::vector<uint8_t> &vec) {
312 return vec.capacity() >= capacity;
313 });
314 if (it != mPool.end()) {
315 sp<ABuffer> buffer = new VectorBuffer(std::move(*it), shared_from_this());
316 mPool.erase(it);
317 return buffer;
318 }
319 if (mUsedSize + capacity > mPoolCapacity) {
320 while (!mPool.empty()) {
321 mUsedSize -= mPool.back().capacity();
322 mPool.pop_back();
323 }
324 if (mUsedSize + capacity > mPoolCapacity) {
325 ALOGD("mUsedSize = %zu, capacity = %zu, mPoolCapacity = %zu",
326 mUsedSize, capacity, mPoolCapacity);
327 return nullptr;
328 }
329 }
330 std::vector<uint8_t> vec(capacity);
331 mUsedSize += vec.capacity();
332 return new VectorBuffer(std::move(vec), shared_from_this());
333 }
334
335private:
336 /**
337 * ABuffer backed by std::vector.
338 */
339 class VectorBuffer : public ::android::ABuffer {
340 public:
341 /**
342 * Construct a VectorBuffer by taking the ownership of supplied vector.
343 *
344 * \param vec backing vector of the buffer. this object takes
345 * ownership at construction.
346 * \param pool a LocalBufferPool object to return the vector at
347 * destruction.
348 */
349 VectorBuffer(std::vector<uint8_t> &&vec, const std::shared_ptr<LocalBufferPool> &pool)
350 : ABuffer(vec.data(), vec.capacity()),
351 mVec(std::move(vec)),
352 mPool(pool) {
353 }
354
355 ~VectorBuffer() override {
356 std::shared_ptr<LocalBufferPool> pool = mPool.lock();
357 if (pool) {
358 // If pool is alive, return the vector back to the pool so that
359 // it can be recycled.
360 pool->returnVector(std::move(mVec));
361 }
362 }
363
364 private:
365 std::vector<uint8_t> mVec;
366 std::weak_ptr<LocalBufferPool> mPool;
367 };
368
369 Mutex mMutex;
370 size_t mPoolCapacity;
371 size_t mUsedSize;
372 std::list<std::vector<uint8_t>> mPool;
373
374 /**
375 * Private constructor to prevent constructing non-managed LocalBufferPool.
376 */
377 explicit LocalBufferPool(size_t poolCapacity)
378 : mPoolCapacity(poolCapacity), mUsedSize(0) {
379 }
380
381 /**
382 * Take back the ownership of vec from the destructed VectorBuffer and put
383 * it in front of the pool.
384 */
385 void returnVector(std::vector<uint8_t> &&vec) {
386 Mutex::Autolock lock(mMutex);
387 mPool.push_front(std::move(vec));
388 }
389
390 DISALLOW_EVIL_CONSTRUCTORS(LocalBufferPool);
391};
392
393sp<GraphicBlockBuffer> AllocateGraphicBuffer(
394 const std::shared_ptr<C2BlockPool> &pool,
395 const sp<AMessage> &format,
396 uint32_t pixelFormat,
397 const C2MemoryUsage &usage,
398 const std::shared_ptr<LocalBufferPool> &localBufferPool) {
399 int32_t width, height;
400 if (!format->findInt32("width", &width) || !format->findInt32("height", &height)) {
401 ALOGD("format lacks width or height");
402 return nullptr;
403 }
404
405 std::shared_ptr<C2GraphicBlock> block;
406 c2_status_t err = pool->fetchGraphicBlock(
407 width, height, pixelFormat, usage, &block);
408 if (err != C2_OK) {
409 ALOGD("fetch graphic block failed: %d", err);
410 return nullptr;
411 }
412
413 return GraphicBlockBuffer::Allocate(
414 format,
415 block,
416 [localBufferPool](size_t capacity) {
417 return localBufferPool->newBuffer(capacity);
418 });
419}
420
421class BuffersArrayImpl;
422
423/**
424 * Flexible buffer slots implementation.
425 */
426class FlexBuffersImpl {
427public:
428 FlexBuffersImpl(const char *name)
429 : mImplName(std::string(name) + ".Impl"),
430 mName(mImplName.c_str()) { }
431
432 /**
433 * Assign an empty slot for a buffer and return the index. If there's no
434 * empty slot, just add one at the end and return it.
435 *
436 * \param buffer[in] a new buffer to assign a slot.
437 * \return index of the assigned slot.
438 */
439 size_t assignSlot(const sp<Codec2Buffer> &buffer) {
440 for (size_t i = 0; i < mBuffers.size(); ++i) {
441 if (mBuffers[i].clientBuffer == nullptr
442 && mBuffers[i].compBuffer.expired()) {
443 mBuffers[i].clientBuffer = buffer;
444 return i;
445 }
446 }
447 mBuffers.push_back({ buffer, std::weak_ptr<C2Buffer>() });
448 return mBuffers.size() - 1;
449 }
450
451 /**
452 * Release the slot from the client, and get the C2Buffer object back from
453 * the previously assigned buffer. Note that the slot is not completely free
454 * until the returned C2Buffer object is freed.
455 *
456 * \param buffer[in] the buffer previously assigned a slot.
457 * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored
458 * if null.
459 * \return true if the buffer is successfully released from a slot
460 * false otherwise
461 */
462 bool releaseSlot(const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) {
463 sp<Codec2Buffer> clientBuffer;
464 size_t index = mBuffers.size();
465 for (size_t i = 0; i < mBuffers.size(); ++i) {
466 if (mBuffers[i].clientBuffer == buffer) {
467 clientBuffer = mBuffers[i].clientBuffer;
468 mBuffers[i].clientBuffer.clear();
469 index = i;
470 break;
471 }
472 }
473 if (clientBuffer == nullptr) {
474 ALOGV("[%s] %s: No matching buffer found", mName, __func__);
475 return false;
476 }
477 std::shared_ptr<C2Buffer> result = clientBuffer->asC2Buffer();
478 mBuffers[index].compBuffer = result;
479 if (c2buffer) {
480 *c2buffer = result;
481 }
482 return true;
483 }
484
485 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer) {
486 for (size_t i = 0; i < mBuffers.size(); ++i) {
487 std::shared_ptr<C2Buffer> compBuffer =
488 mBuffers[i].compBuffer.lock();
489 if (!compBuffer || compBuffer != c2buffer) {
490 continue;
491 }
492 mBuffers[i].clientBuffer = nullptr;
493 mBuffers[i].compBuffer.reset();
494 return true;
495 }
496 ALOGV("[%s] codec released an unknown buffer", mName);
497 return false;
498 }
499
500 void flush() {
501 ALOGV("[%s] buffers are flushed %zu", mName, mBuffers.size());
502 mBuffers.clear();
503 }
504
505private:
506 friend class BuffersArrayImpl;
507
508 std::string mImplName; ///< name for debugging
509 const char *mName; ///< C-string version of name
510
511 struct Entry {
512 sp<Codec2Buffer> clientBuffer;
513 std::weak_ptr<C2Buffer> compBuffer;
514 };
515 std::vector<Entry> mBuffers;
516};
517
518/**
519 * Static buffer slots implementation based on a fixed-size array.
520 */
521class BuffersArrayImpl {
522public:
523 BuffersArrayImpl()
524 : mImplName("BuffersArrayImpl"),
525 mName(mImplName.c_str()) { }
526
527 /**
528 * Initialize buffer array from the original |impl|. The buffers known by
529 * the client is preserved, and the empty slots are populated so that the
530 * array size is at least |minSize|.
531 *
532 * \param impl[in] FlexBuffersImpl object used so far.
533 * \param minSize[in] minimum size of the buffer array.
534 * \param allocate[in] function to allocate a client buffer for an empty slot.
535 */
536 void initialize(
537 const FlexBuffersImpl &impl,
538 size_t minSize,
539 std::function<sp<Codec2Buffer>()> allocate) {
540 mImplName = impl.mImplName + "[N]";
541 mName = mImplName.c_str();
542 for (size_t i = 0; i < impl.mBuffers.size(); ++i) {
543 sp<Codec2Buffer> clientBuffer = impl.mBuffers[i].clientBuffer;
544 bool ownedByClient = (clientBuffer != nullptr);
545 if (!ownedByClient) {
546 clientBuffer = allocate();
547 }
548 mBuffers.push_back({ clientBuffer, impl.mBuffers[i].compBuffer, ownedByClient });
549 }
550 ALOGV("[%s] converted %zu buffers to array mode of %zu", mName, mBuffers.size(), minSize);
551 for (size_t i = impl.mBuffers.size(); i < minSize; ++i) {
552 mBuffers.push_back({ allocate(), std::weak_ptr<C2Buffer>(), false });
553 }
554 }
555
556 /**
557 * Grab a buffer from the underlying array which matches the criteria.
558 *
559 * \param index[out] index of the slot.
560 * \param buffer[out] the matching buffer.
561 * \param match[in] a function to test whether the buffer matches the
562 * criteria or not.
563 * \return OK if successful,
564 * WOULD_BLOCK if slots are being used,
565 * NO_MEMORY if no slot matches the criteria, even though it's
566 * available
567 */
568 status_t grabBuffer(
569 size_t *index,
570 sp<Codec2Buffer> *buffer,
571 std::function<bool(const sp<Codec2Buffer> &)> match =
572 [](const sp<Codec2Buffer> &) { return true; }) {
573 // allBuffersDontMatch remains true if all buffers are available but
574 // match() returns false for every buffer.
575 bool allBuffersDontMatch = true;
576 for (size_t i = 0; i < mBuffers.size(); ++i) {
577 if (!mBuffers[i].ownedByClient && mBuffers[i].compBuffer.expired()) {
578 if (match(mBuffers[i].clientBuffer)) {
579 mBuffers[i].ownedByClient = true;
580 *buffer = mBuffers[i].clientBuffer;
581 (*buffer)->meta()->clear();
582 (*buffer)->setRange(0, (*buffer)->capacity());
583 *index = i;
584 return OK;
585 }
586 } else {
587 allBuffersDontMatch = false;
588 }
589 }
590 return allBuffersDontMatch ? NO_MEMORY : WOULD_BLOCK;
591 }
592
593 /**
594 * Return the buffer from the client, and get the C2Buffer object back from
595 * the buffer. Note that the slot is not completely free until the returned
596 * C2Buffer object is freed.
597 *
598 * \param buffer[in] the buffer previously grabbed.
599 * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored
600 * if null.
601 * \return true if the buffer is successfully returned
602 * false otherwise
603 */
604 bool returnBuffer(const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) {
605 sp<Codec2Buffer> clientBuffer;
606 size_t index = mBuffers.size();
607 for (size_t i = 0; i < mBuffers.size(); ++i) {
608 if (mBuffers[i].clientBuffer == buffer) {
609 if (!mBuffers[i].ownedByClient) {
610 ALOGD("[%s] Client returned a buffer it does not own according to our record: %zu", mName, i);
611 }
612 clientBuffer = mBuffers[i].clientBuffer;
613 mBuffers[i].ownedByClient = false;
614 index = i;
615 break;
616 }
617 }
618 if (clientBuffer == nullptr) {
619 ALOGV("[%s] %s: No matching buffer found", mName, __func__);
620 return false;
621 }
622 ALOGV("[%s] %s: matching buffer found (index=%zu)", mName, __func__, index);
623 std::shared_ptr<C2Buffer> result = clientBuffer->asC2Buffer();
624 mBuffers[index].compBuffer = result;
625 if (c2buffer) {
626 *c2buffer = result;
627 }
628 return true;
629 }
630
631 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer) {
632 for (size_t i = 0; i < mBuffers.size(); ++i) {
633 std::shared_ptr<C2Buffer> compBuffer =
634 mBuffers[i].compBuffer.lock();
635 if (!compBuffer) {
636 continue;
637 }
638 if (c2buffer == compBuffer) {
639 if (mBuffers[i].ownedByClient) {
640 // This should not happen.
641 ALOGD("[%s] codec released a buffer owned by client "
642 "(index %zu)", mName, i);
643 mBuffers[i].ownedByClient = false;
644 }
645 mBuffers[i].compBuffer.reset();
646 return true;
647 }
648 }
649 ALOGV("[%s] codec released an unknown buffer (array mode)", mName);
650 return false;
651 }
652
653 /**
654 * Populate |array| with the underlying buffer array.
655 *
656 * \param array[out] an array to be filled with the underlying buffer array.
657 */
658 void getArray(Vector<sp<MediaCodecBuffer>> *array) const {
659 array->clear();
660 for (const Entry &entry : mBuffers) {
661 array->push(entry.clientBuffer);
662 }
663 }
664
665 /**
666 * The client abandoned all known buffers, so reclaim the ownership.
667 */
668 void flush() {
669 for (Entry &entry : mBuffers) {
670 entry.ownedByClient = false;
671 }
672 }
673
674 void realloc(std::function<sp<Codec2Buffer>()> alloc) {
675 size_t size = mBuffers.size();
676 mBuffers.clear();
677 for (size_t i = 0; i < size; ++i) {
678 mBuffers.push_back({ alloc(), std::weak_ptr<C2Buffer>(), false });
679 }
680 }
681
682private:
683 std::string mImplName; ///< name for debugging
684 const char *mName; ///< C-string version of name
685
686 struct Entry {
687 const sp<Codec2Buffer> clientBuffer;
688 std::weak_ptr<C2Buffer> compBuffer;
689 bool ownedByClient;
690 };
691 std::vector<Entry> mBuffers;
692};
693
694class InputBuffersArray : public CCodecBufferChannel::InputBuffers {
695public:
696 InputBuffersArray(const char *componentName, const char *name = "Input[N]")
697 : InputBuffers(componentName, name) { }
698 ~InputBuffersArray() override = default;
699
700 void initialize(
701 const FlexBuffersImpl &impl,
702 size_t minSize,
703 std::function<sp<Codec2Buffer>()> allocate) {
704 mImpl.initialize(impl, minSize, allocate);
705 }
706
707 bool isArrayMode() const final { return true; }
708
709 std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
710 size_t) final {
711 return nullptr;
712 }
713
714 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final {
715 mImpl.getArray(array);
716 }
717
718 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override {
719 sp<Codec2Buffer> c2Buffer;
720 status_t err = mImpl.grabBuffer(index, &c2Buffer);
721 if (err == OK) {
722 c2Buffer->setFormat(mFormat);
723 *buffer = c2Buffer;
724 return true;
725 }
726 return false;
727 }
728
729 bool releaseBuffer(
730 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) override {
731 return mImpl.returnBuffer(buffer, c2buffer);
732 }
733
734 bool expireComponentBuffer(
735 const std::shared_ptr<C2Buffer> &c2buffer) override {
736 return mImpl.expireComponentBuffer(c2buffer);
737 }
738
739 void flush() override {
740 mImpl.flush();
741 }
742
743private:
744 BuffersArrayImpl mImpl;
745};
746
747class LinearInputBuffers : public CCodecBufferChannel::InputBuffers {
748public:
749 LinearInputBuffers(const char *componentName, const char *name = "1D-Input")
750 : InputBuffers(componentName, name),
751 mImpl(mName) { }
752
753 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override {
754 int32_t capacity = kLinearBufferSize;
755 (void)mFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
756 if ((size_t)capacity > kMaxLinearBufferSize) {
757 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
758 capacity = kMaxLinearBufferSize;
759 }
760 // TODO: proper max input size
761 // TODO: read usage from intf
762 sp<Codec2Buffer> newBuffer = alloc((size_t)capacity);
763 if (newBuffer == nullptr) {
764 return false;
765 }
766 *index = mImpl.assignSlot(newBuffer);
767 *buffer = newBuffer;
768 return true;
769 }
770
771 bool releaseBuffer(
772 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) override {
773 return mImpl.releaseSlot(buffer, c2buffer);
774 }
775
776 bool expireComponentBuffer(
777 const std::shared_ptr<C2Buffer> &c2buffer) override {
778 return mImpl.expireComponentBuffer(c2buffer);
779 }
780
781 void flush() override {
782 // This is no-op by default unless we're in array mode where we need to keep
783 // track of the flushed work.
784 mImpl.flush();
785 }
786
787 std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
788 size_t size) final {
789 int32_t capacity = kLinearBufferSize;
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800790 (void)mFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
791 if ((size_t)capacity > kMaxLinearBufferSize) {
792 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
793 capacity = kMaxLinearBufferSize;
794 }
795 // TODO: proper max input size
796 // TODO: read usage from intf
Pawin Vongmasa36653902018-11-15 00:10:25 -0800797 std::unique_ptr<InputBuffersArray> array(
798 new InputBuffersArray(mComponentName.c_str(), "1D-Input[N]"));
799 array->setPool(mPool);
800 array->setFormat(mFormat);
801 array->initialize(
802 mImpl,
803 size,
804 [this, capacity] () -> sp<Codec2Buffer> { return alloc(capacity); });
805 return std::move(array);
806 }
807
808 virtual sp<Codec2Buffer> alloc(size_t size) const {
809 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
810 std::shared_ptr<C2LinearBlock> block;
811
812 c2_status_t err = mPool->fetchLinearBlock(size, usage, &block);
813 if (err != C2_OK) {
814 return nullptr;
815 }
816
817 return LinearBlockBuffer::Allocate(mFormat, block);
818 }
819
820private:
821 FlexBuffersImpl mImpl;
822};
823
824class EncryptedLinearInputBuffers : public LinearInputBuffers {
825public:
826 EncryptedLinearInputBuffers(
827 bool secure,
828 const sp<MemoryDealer> &dealer,
829 const sp<ICrypto> &crypto,
830 int32_t heapSeqNum,
831 size_t capacity,
832 const char *componentName, const char *name = "EncryptedInput")
833 : LinearInputBuffers(componentName, name),
834 mUsage({0, 0}),
835 mDealer(dealer),
836 mCrypto(crypto),
837 mHeapSeqNum(heapSeqNum) {
838 if (secure) {
839 mUsage = { C2MemoryUsage::READ_PROTECTED, 0 };
840 } else {
841 mUsage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
842 }
843 for (size_t i = 0; i < kMinInputBufferArraySize; ++i) {
844 sp<IMemory> memory = mDealer->allocate(capacity);
845 if (memory == nullptr) {
846 ALOGD("[%s] Failed to allocate memory from dealer: only %zu slots allocated", mName, i);
847 break;
848 }
849 mMemoryVector.push_back({std::weak_ptr<C2LinearBlock>(), memory});
850 }
851 }
852
853 ~EncryptedLinearInputBuffers() override {
854 }
855
856 sp<Codec2Buffer> alloc(size_t size) const override {
857 sp<IMemory> memory;
858 for (const Entry &entry : mMemoryVector) {
859 if (entry.block.expired()) {
860 memory = entry.memory;
861 break;
862 }
863 }
864 if (memory == nullptr) {
865 return nullptr;
866 }
867
868 std::shared_ptr<C2LinearBlock> block;
869 c2_status_t err = mPool->fetchLinearBlock(size, mUsage, &block);
870 if (err != C2_OK) {
871 return nullptr;
872 }
873
874 return new EncryptedLinearBlockBuffer(mFormat, block, memory, mHeapSeqNum);
875 }
876
877private:
878 C2MemoryUsage mUsage;
879 sp<MemoryDealer> mDealer;
880 sp<ICrypto> mCrypto;
881 int32_t mHeapSeqNum;
882 struct Entry {
883 std::weak_ptr<C2LinearBlock> block;
884 sp<IMemory> memory;
885 };
886 std::vector<Entry> mMemoryVector;
887};
888
889class GraphicMetadataInputBuffers : public CCodecBufferChannel::InputBuffers {
890public:
891 GraphicMetadataInputBuffers(const char *componentName, const char *name = "2D-MetaInput")
892 : InputBuffers(componentName, name),
893 mImpl(mName),
894 mStore(GetCodec2PlatformAllocatorStore()) { }
895 ~GraphicMetadataInputBuffers() override = default;
896
897 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override {
898 std::shared_ptr<C2Allocator> alloc;
899 c2_status_t err = mStore->fetchAllocator(mPool->getAllocatorId(), &alloc);
900 if (err != C2_OK) {
901 return false;
902 }
903 sp<GraphicMetadataBuffer> newBuffer = new GraphicMetadataBuffer(mFormat, alloc);
904 if (newBuffer == nullptr) {
905 return false;
906 }
907 *index = mImpl.assignSlot(newBuffer);
908 *buffer = newBuffer;
909 return true;
910 }
911
912 bool releaseBuffer(
913 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) override {
914 return mImpl.releaseSlot(buffer, c2buffer);
915 }
916
917 bool expireComponentBuffer(
918 const std::shared_ptr<C2Buffer> &c2buffer) override {
919 return mImpl.expireComponentBuffer(c2buffer);
920 }
921
922 void flush() override {
923 // This is no-op by default unless we're in array mode where we need to keep
924 // track of the flushed work.
925 }
926
927 std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
928 size_t size) final {
929 std::shared_ptr<C2Allocator> alloc;
930 c2_status_t err = mStore->fetchAllocator(mPool->getAllocatorId(), &alloc);
931 if (err != C2_OK) {
932 return nullptr;
933 }
934 std::unique_ptr<InputBuffersArray> array(
935 new InputBuffersArray(mComponentName.c_str(), "2D-MetaInput[N]"));
936 array->setPool(mPool);
937 array->setFormat(mFormat);
938 array->initialize(
939 mImpl,
940 size,
941 [format = mFormat, alloc]() -> sp<Codec2Buffer> {
942 return new GraphicMetadataBuffer(format, alloc);
943 });
944 return std::move(array);
945 }
946
947private:
948 FlexBuffersImpl mImpl;
949 std::shared_ptr<C2AllocatorStore> mStore;
950};
951
952class GraphicInputBuffers : public CCodecBufferChannel::InputBuffers {
953public:
954 GraphicInputBuffers(const char *componentName, const char *name = "2D-BB-Input")
955 : InputBuffers(componentName, name),
956 mImpl(mName),
957 mLocalBufferPool(LocalBufferPool::Create(
958 kMaxLinearBufferSize * kMinInputBufferArraySize)) { }
959 ~GraphicInputBuffers() override = default;
960
961 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override {
962 // TODO: proper max input size
963 // TODO: read usage from intf
964 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
965 sp<GraphicBlockBuffer> newBuffer = AllocateGraphicBuffer(
966 mPool, mFormat, HAL_PIXEL_FORMAT_YV12, usage, mLocalBufferPool);
967 if (newBuffer == nullptr) {
968 return false;
969 }
970 *index = mImpl.assignSlot(newBuffer);
971 *buffer = newBuffer;
972 return true;
973 }
974
975 bool releaseBuffer(
976 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) override {
977 return mImpl.releaseSlot(buffer, c2buffer);
978 }
979
980 bool expireComponentBuffer(
981 const std::shared_ptr<C2Buffer> &c2buffer) override {
982 return mImpl.expireComponentBuffer(c2buffer);
983 }
984 void flush() override {
985 // This is no-op by default unless we're in array mode where we need to keep
986 // track of the flushed work.
987 }
988
989 std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
990 size_t size) final {
991 std::unique_ptr<InputBuffersArray> array(
992 new InputBuffersArray(mComponentName.c_str(), "2D-BB-Input[N]"));
993 array->setPool(mPool);
994 array->setFormat(mFormat);
995 array->initialize(
996 mImpl,
997 size,
998 [pool = mPool, format = mFormat, lbp = mLocalBufferPool]() -> sp<Codec2Buffer> {
999 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
1000 return AllocateGraphicBuffer(
1001 pool, format, HAL_PIXEL_FORMAT_YV12, usage, lbp);
1002 });
1003 return std::move(array);
1004 }
1005
1006private:
1007 FlexBuffersImpl mImpl;
1008 std::shared_ptr<LocalBufferPool> mLocalBufferPool;
1009};
1010
1011class DummyInputBuffers : public CCodecBufferChannel::InputBuffers {
1012public:
1013 DummyInputBuffers(const char *componentName, const char *name = "2D-Input")
1014 : InputBuffers(componentName, name) { }
1015
1016 bool requestNewBuffer(size_t *, sp<MediaCodecBuffer> *) override {
1017 return false;
1018 }
1019
1020 bool releaseBuffer(
1021 const sp<MediaCodecBuffer> &, std::shared_ptr<C2Buffer> *) override {
1022 return false;
1023 }
1024
1025 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &) override {
1026 return false;
1027 }
1028 void flush() override {
1029 }
1030
1031 std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
1032 size_t) final {
1033 return nullptr;
1034 }
1035
1036 bool isArrayMode() const final { return true; }
1037
1038 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final {
1039 array->clear();
1040 }
1041};
1042
1043class OutputBuffersArray : public CCodecBufferChannel::OutputBuffers {
1044public:
1045 OutputBuffersArray(const char *componentName, const char *name = "Output[N]")
1046 : OutputBuffers(componentName, name) { }
1047 ~OutputBuffersArray() override = default;
1048
1049 void initialize(
1050 const FlexBuffersImpl &impl,
1051 size_t minSize,
1052 std::function<sp<Codec2Buffer>()> allocate) {
1053 mImpl.initialize(impl, minSize, allocate);
1054 }
1055
1056 bool isArrayMode() const final { return true; }
1057
1058 std::unique_ptr<CCodecBufferChannel::OutputBuffers> toArrayMode(
1059 size_t) final {
1060 return nullptr;
1061 }
1062
1063 status_t registerBuffer(
1064 const std::shared_ptr<C2Buffer> &buffer,
1065 size_t *index,
1066 sp<MediaCodecBuffer> *clientBuffer) final {
1067 sp<Codec2Buffer> c2Buffer;
1068 status_t err = mImpl.grabBuffer(
1069 index,
1070 &c2Buffer,
1071 [buffer](const sp<Codec2Buffer> &clientBuffer) {
1072 return clientBuffer->canCopy(buffer);
1073 });
1074 if (err == WOULD_BLOCK) {
1075 ALOGV("[%s] buffers temporarily not available", mName);
1076 return err;
1077 } else if (err != OK) {
1078 ALOGD("[%s] grabBuffer failed: %d", mName, err);
1079 return err;
1080 }
1081 c2Buffer->setFormat(mFormat);
1082 if (!c2Buffer->copy(buffer)) {
1083 ALOGD("[%s] copy buffer failed", mName);
1084 return WOULD_BLOCK;
1085 }
1086 submit(c2Buffer);
1087 *clientBuffer = c2Buffer;
1088 ALOGV("[%s] grabbed buffer %zu", mName, *index);
1089 return OK;
1090 }
1091
1092 status_t registerCsd(
1093 const C2StreamCsdInfo::output *csd,
1094 size_t *index,
1095 sp<MediaCodecBuffer> *clientBuffer) final {
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
1114 bool releaseBuffer(
1115 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) override {
1116 return mImpl.returnBuffer(buffer, c2buffer);
1117 }
1118
1119 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) override {
1120 (void)flushedWork;
1121 mImpl.flush();
1122 if (mSkipCutBuffer != nullptr) {
1123 mSkipCutBuffer->clear();
1124 }
1125 }
1126
1127 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final {
1128 mImpl.getArray(array);
1129 }
1130
1131 void realloc(const std::shared_ptr<C2Buffer> &c2buffer) {
1132 std::function<sp<Codec2Buffer>()> alloc;
1133 switch (c2buffer->data().type()) {
1134 case C2BufferData::LINEAR: {
1135 uint32_t size = kLinearBufferSize;
1136 const C2ConstLinearBlock &block = c2buffer->data().linearBlocks().front();
1137 if (block.size() < kMaxLinearBufferSize / 2) {
1138 size = block.size() * 2;
1139 } else {
1140 size = kMaxLinearBufferSize;
1141 }
1142 alloc = [format = mFormat, size] {
1143 return new LocalLinearBuffer(format, new ABuffer(size));
1144 };
1145 break;
1146 }
1147
1148 // TODO: add support
1149 case C2BufferData::GRAPHIC: FALLTHROUGH_INTENDED;
1150
1151 case C2BufferData::INVALID: FALLTHROUGH_INTENDED;
1152 case C2BufferData::LINEAR_CHUNKS: FALLTHROUGH_INTENDED;
1153 case C2BufferData::GRAPHIC_CHUNKS: FALLTHROUGH_INTENDED;
1154 default:
1155 ALOGD("Unsupported type: %d", (int)c2buffer->data().type());
1156 return;
1157 }
1158 mImpl.realloc(alloc);
1159 }
1160
1161private:
1162 BuffersArrayImpl mImpl;
1163};
1164
1165class FlexOutputBuffers : public CCodecBufferChannel::OutputBuffers {
1166public:
1167 FlexOutputBuffers(const char *componentName, const char *name = "Output[]")
1168 : OutputBuffers(componentName, name),
1169 mImpl(mName) { }
1170
1171 status_t registerBuffer(
1172 const std::shared_ptr<C2Buffer> &buffer,
1173 size_t *index,
1174 sp<MediaCodecBuffer> *clientBuffer) override {
1175 sp<Codec2Buffer> newBuffer = wrap(buffer);
1176 newBuffer->setFormat(mFormat);
1177 *index = mImpl.assignSlot(newBuffer);
1178 *clientBuffer = newBuffer;
1179 ALOGV("[%s] registered buffer %zu", mName, *index);
1180 return OK;
1181 }
1182
1183 status_t registerCsd(
1184 const C2StreamCsdInfo::output *csd,
1185 size_t *index,
1186 sp<MediaCodecBuffer> *clientBuffer) final {
1187 sp<Codec2Buffer> newBuffer = new LocalLinearBuffer(
1188 mFormat, ABuffer::CreateAsCopy(csd->m.value, csd->flexCount()));
1189 *index = mImpl.assignSlot(newBuffer);
1190 *clientBuffer = newBuffer;
1191 return OK;
1192 }
1193
1194 bool releaseBuffer(
1195 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) override {
1196 return mImpl.releaseSlot(buffer, c2buffer);
1197 }
1198
1199 void flush(
1200 const std::list<std::unique_ptr<C2Work>> &flushedWork) override {
1201 (void) flushedWork;
1202 // This is no-op by default unless we're in array mode where we need to keep
1203 // track of the flushed work.
1204 }
1205
1206 std::unique_ptr<CCodecBufferChannel::OutputBuffers> toArrayMode(
1207 size_t size) override {
1208 std::unique_ptr<OutputBuffersArray> array(new OutputBuffersArray(mComponentName.c_str()));
1209 array->setFormat(mFormat);
1210 array->transferSkipCutBuffer(mSkipCutBuffer);
1211 array->initialize(
1212 mImpl,
1213 size,
1214 [this]() { return allocateArrayBuffer(); });
1215 return std::move(array);
1216 }
1217
1218 /**
1219 * Return an appropriate Codec2Buffer object for the type of buffers.
1220 *
1221 * \param buffer C2Buffer object to wrap.
1222 *
1223 * \return appropriate Codec2Buffer object to wrap |buffer|.
1224 */
1225 virtual sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) = 0;
1226
1227 /**
1228 * Return an appropriate Codec2Buffer object for the type of buffers, to be
1229 * used as an empty array buffer.
1230 *
1231 * \return appropriate Codec2Buffer object which can copy() from C2Buffers.
1232 */
1233 virtual sp<Codec2Buffer> allocateArrayBuffer() = 0;
1234
1235private:
1236 FlexBuffersImpl mImpl;
1237};
1238
1239class LinearOutputBuffers : public FlexOutputBuffers {
1240public:
1241 LinearOutputBuffers(const char *componentName, const char *name = "1D-Output")
1242 : FlexOutputBuffers(componentName, name) { }
1243
1244 void flush(
1245 const std::list<std::unique_ptr<C2Work>> &flushedWork) override {
1246 if (mSkipCutBuffer != nullptr) {
1247 mSkipCutBuffer->clear();
1248 }
1249 FlexOutputBuffers::flush(flushedWork);
1250 }
1251
1252 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override {
1253 if (buffer == nullptr) {
1254 ALOGV("[%s] using a dummy buffer", mName);
1255 return new LocalLinearBuffer(mFormat, new ABuffer(0));
1256 }
1257 if (buffer->data().type() != C2BufferData::LINEAR) {
1258 ALOGV("[%s] non-linear buffer %d", mName, buffer->data().type());
1259 // We expect linear output buffers from the component.
1260 return nullptr;
1261 }
1262 if (buffer->data().linearBlocks().size() != 1u) {
1263 ALOGV("[%s] no linear buffers", mName);
1264 // We expect one and only one linear block from the component.
1265 return nullptr;
1266 }
1267 sp<Codec2Buffer> clientBuffer = ConstLinearBlockBuffer::Allocate(mFormat, buffer);
1268 submit(clientBuffer);
1269 return clientBuffer;
1270 }
1271
1272 sp<Codec2Buffer> allocateArrayBuffer() override {
1273 // TODO: proper max output size
1274 return new LocalLinearBuffer(mFormat, new ABuffer(kLinearBufferSize));
1275 }
1276};
1277
1278class GraphicOutputBuffers : public FlexOutputBuffers {
1279public:
1280 GraphicOutputBuffers(const char *componentName, const char *name = "2D-Output")
1281 : FlexOutputBuffers(componentName, name) { }
1282
1283 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override {
1284 return new DummyContainerBuffer(mFormat, buffer);
1285 }
1286
1287 sp<Codec2Buffer> allocateArrayBuffer() override {
1288 return new DummyContainerBuffer(mFormat);
1289 }
1290};
1291
1292class RawGraphicOutputBuffers : public FlexOutputBuffers {
1293public:
1294 RawGraphicOutputBuffers(const char *componentName, const char *name = "2D-BB-Output")
1295 : FlexOutputBuffers(componentName, name),
1296 mLocalBufferPool(LocalBufferPool::Create(
1297 kMaxLinearBufferSize * kMinOutputBufferArraySize)) { }
1298 ~RawGraphicOutputBuffers() override = default;
1299
1300 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override {
1301 if (buffer == nullptr) {
1302 sp<Codec2Buffer> c2buffer = ConstGraphicBlockBuffer::AllocateEmpty(
1303 mFormat,
1304 [lbp = mLocalBufferPool](size_t capacity) {
1305 return lbp->newBuffer(capacity);
1306 });
1307 c2buffer->setRange(0, 0);
1308 return c2buffer;
1309 } else {
1310 return ConstGraphicBlockBuffer::Allocate(
1311 mFormat,
1312 buffer,
1313 [lbp = mLocalBufferPool](size_t capacity) {
1314 return lbp->newBuffer(capacity);
1315 });
1316 }
1317 }
1318
1319 sp<Codec2Buffer> allocateArrayBuffer() override {
1320 return ConstGraphicBlockBuffer::AllocateEmpty(
1321 mFormat,
1322 [lbp = mLocalBufferPool](size_t capacity) {
1323 return lbp->newBuffer(capacity);
1324 });
1325 }
1326
1327private:
1328 std::shared_ptr<LocalBufferPool> mLocalBufferPool;
1329};
1330
1331} // namespace
1332
1333CCodecBufferChannel::QueueGuard::QueueGuard(
1334 CCodecBufferChannel::QueueSync &sync) : mSync(sync) {
1335 Mutex::Autolock l(mSync.mGuardLock);
1336 // At this point it's guaranteed that mSync is not under state transition,
1337 // as we are holding its mutex.
1338
1339 Mutexed<CCodecBufferChannel::QueueSync::Counter>::Locked count(mSync.mCount);
1340 if (count->value == -1) {
1341 mRunning = false;
1342 } else {
1343 ++count->value;
1344 mRunning = true;
1345 }
1346}
1347
1348CCodecBufferChannel::QueueGuard::~QueueGuard() {
1349 if (mRunning) {
1350 // We are not holding mGuardLock at this point so that QueueSync::stop() can
1351 // keep holding the lock until mCount reaches zero.
1352 Mutexed<CCodecBufferChannel::QueueSync::Counter>::Locked count(mSync.mCount);
1353 --count->value;
1354 count->cond.broadcast();
1355 }
1356}
1357
1358void CCodecBufferChannel::QueueSync::start() {
1359 Mutex::Autolock l(mGuardLock);
1360 // If stopped, it goes to running state; otherwise no-op.
1361 Mutexed<Counter>::Locked count(mCount);
1362 if (count->value == -1) {
1363 count->value = 0;
1364 }
1365}
1366
1367void CCodecBufferChannel::QueueSync::stop() {
1368 Mutex::Autolock l(mGuardLock);
1369 Mutexed<Counter>::Locked count(mCount);
1370 if (count->value == -1) {
1371 // no-op
1372 return;
1373 }
1374 // Holding mGuardLock here blocks creation of additional QueueGuard objects, so
1375 // mCount can only decrement. In other words, threads that acquired the lock
1376 // are allowed to finish execution but additional threads trying to acquire
1377 // the lock at this point will block, and then get QueueGuard at STOPPED
1378 // state.
1379 while (count->value != 0) {
1380 count.waitForCondition(count->cond);
1381 }
1382 count->value = -1;
1383}
1384
1385// CCodecBufferChannel::PipelineCapacity
1386
1387CCodecBufferChannel::PipelineCapacity::PipelineCapacity()
1388 : input(0), component(0),
1389 mName("<UNKNOWN COMPONENT>") {
1390}
1391
1392void CCodecBufferChannel::PipelineCapacity::initialize(
1393 int newInput,
1394 int newComponent,
1395 const char* newName,
1396 const char* callerTag) {
1397 input.store(newInput, std::memory_order_relaxed);
1398 component.store(newComponent, std::memory_order_relaxed);
1399 mName = newName;
1400 ALOGV("[%s] %s -- PipelineCapacity::initialize(): "
1401 "pipeline availability initialized ==> "
1402 "input = %d, component = %d",
1403 mName, callerTag ? callerTag : "*",
1404 newInput, newComponent);
1405}
1406
1407bool CCodecBufferChannel::PipelineCapacity::allocate(const char* callerTag) {
1408 int prevInput = input.fetch_sub(1, std::memory_order_relaxed);
1409 int prevComponent = component.fetch_sub(1, std::memory_order_relaxed);
1410 if (prevInput > 0 && prevComponent > 0) {
1411 ALOGV("[%s] %s -- PipelineCapacity::allocate() returns true: "
1412 "pipeline availability -1 all ==> "
1413 "input = %d, component = %d",
1414 mName, callerTag ? callerTag : "*",
1415 prevInput - 1,
1416 prevComponent - 1);
1417 return true;
1418 }
1419 input.fetch_add(1, std::memory_order_relaxed);
1420 component.fetch_add(1, std::memory_order_relaxed);
1421 ALOGV("[%s] %s -- PipelineCapacity::allocate() returns false: "
1422 "pipeline availability unchanged ==> "
1423 "input = %d, component = %d",
1424 mName, callerTag ? callerTag : "*",
1425 prevInput,
1426 prevComponent);
1427 return false;
1428}
1429
1430void CCodecBufferChannel::PipelineCapacity::free(const char* callerTag) {
1431 int prevInput = input.fetch_add(1, std::memory_order_relaxed);
1432 int prevComponent = component.fetch_add(1, std::memory_order_relaxed);
1433 ALOGV("[%s] %s -- PipelineCapacity::free(): "
1434 "pipeline availability +1 all ==> "
1435 "input = %d, component = %d",
1436 mName, callerTag ? callerTag : "*",
1437 prevInput + 1,
1438 prevComponent + 1);
1439}
1440
1441int CCodecBufferChannel::PipelineCapacity::freeInputSlots(
1442 size_t numDiscardedInputBuffers,
1443 const char* callerTag) {
1444 int prevInput = input.fetch_add(numDiscardedInputBuffers,
1445 std::memory_order_relaxed);
1446 ALOGV("[%s] %s -- PipelineCapacity::freeInputSlots(%zu): "
1447 "pipeline availability +%zu input ==> "
1448 "input = %d, component = %d",
1449 mName, callerTag ? callerTag : "*",
1450 numDiscardedInputBuffers,
1451 numDiscardedInputBuffers,
1452 prevInput + static_cast<int>(numDiscardedInputBuffers),
1453 component.load(std::memory_order_relaxed));
1454 return prevInput + static_cast<int>(numDiscardedInputBuffers);
1455}
1456
1457int CCodecBufferChannel::PipelineCapacity::freeComponentSlot(
1458 const char* callerTag) {
1459 int prevComponent = component.fetch_add(1, std::memory_order_relaxed);
1460 ALOGV("[%s] %s -- PipelineCapacity::freeComponentSlot(): "
1461 "pipeline availability +1 component ==> "
1462 "input = %d, component = %d",
1463 mName, callerTag ? callerTag : "*",
1464 input.load(std::memory_order_relaxed),
1465 prevComponent + 1);
1466 return prevComponent + 1;
1467}
1468
1469// CCodecBufferChannel::ReorderStash
1470
1471CCodecBufferChannel::ReorderStash::ReorderStash() {
1472 clear();
1473}
1474
1475void CCodecBufferChannel::ReorderStash::clear() {
1476 mPending.clear();
1477 mStash.clear();
1478 mDepth = 0;
1479 mKey = C2Config::ORDINAL;
1480}
1481
1482void CCodecBufferChannel::ReorderStash::setDepth(uint32_t depth) {
1483 mPending.splice(mPending.end(), mStash);
1484 mDepth = depth;
1485}
1486void CCodecBufferChannel::ReorderStash::setKey(C2Config::ordinal_key_t key) {
1487 mPending.splice(mPending.end(), mStash);
1488 mKey = key;
1489}
1490
1491bool CCodecBufferChannel::ReorderStash::pop(Entry *entry) {
1492 if (mPending.empty()) {
1493 return false;
1494 }
1495 entry->buffer = mPending.front().buffer;
1496 entry->timestamp = mPending.front().timestamp;
1497 entry->flags = mPending.front().flags;
1498 entry->ordinal = mPending.front().ordinal;
1499 mPending.pop_front();
1500 return true;
1501}
1502
1503void CCodecBufferChannel::ReorderStash::emplace(
1504 const std::shared_ptr<C2Buffer> &buffer,
1505 int64_t timestamp,
1506 int32_t flags,
1507 const C2WorkOrdinalStruct &ordinal) {
1508 for (auto it = mStash.begin(); it != mStash.end(); ++it) {
1509 if (less(ordinal, it->ordinal)) {
1510 mStash.emplace(it, buffer, timestamp, flags, ordinal);
1511 return;
1512 }
1513 }
1514 mStash.emplace_back(buffer, timestamp, flags, ordinal);
1515 while (!mStash.empty() && mStash.size() > mDepth) {
1516 mPending.push_back(mStash.front());
1517 mStash.pop_front();
1518 }
1519}
1520
1521void CCodecBufferChannel::ReorderStash::defer(
1522 const CCodecBufferChannel::ReorderStash::Entry &entry) {
1523 mPending.push_front(entry);
1524}
1525
1526bool CCodecBufferChannel::ReorderStash::hasPending() const {
1527 return !mPending.empty();
1528}
1529
1530bool CCodecBufferChannel::ReorderStash::less(
1531 const C2WorkOrdinalStruct &o1, const C2WorkOrdinalStruct &o2) {
1532 switch (mKey) {
1533 case C2Config::ORDINAL: return o1.frameIndex < o2.frameIndex;
1534 case C2Config::TIMESTAMP: return o1.timestamp < o2.timestamp;
1535 case C2Config::CUSTOM: return o1.customOrdinal < o2.customOrdinal;
1536 default:
1537 ALOGD("Unrecognized key; default to timestamp");
1538 return o1.frameIndex < o2.frameIndex;
1539 }
1540}
1541
1542// CCodecBufferChannel
1543
1544CCodecBufferChannel::CCodecBufferChannel(
1545 const std::shared_ptr<CCodecCallback> &callback)
1546 : mHeapSeqNum(-1),
1547 mCCodecCallback(callback),
1548 mFrameIndex(0u),
1549 mFirstValidFrameIndex(0u),
1550 mMetaMode(MODE_NONE),
1551 mAvailablePipelineCapacity(),
1552 mInputMetEos(false) {
1553 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
1554 buffers->reset(new DummyInputBuffers(""));
1555}
1556
1557CCodecBufferChannel::~CCodecBufferChannel() {
1558 if (mCrypto != nullptr && mDealer != nullptr && mHeapSeqNum >= 0) {
1559 mCrypto->unsetHeap(mHeapSeqNum);
1560 }
1561}
1562
1563void CCodecBufferChannel::setComponent(
1564 const std::shared_ptr<Codec2Client::Component> &component) {
1565 mComponent = component;
1566 mComponentName = component->getName() + StringPrintf("#%d", int(uintptr_t(component.get()) % 997));
1567 mName = mComponentName.c_str();
1568}
1569
1570status_t CCodecBufferChannel::setInputSurface(
1571 const std::shared_ptr<InputSurfaceWrapper> &surface) {
1572 ALOGV("[%s] setInputSurface", mName);
1573 mInputSurface = surface;
1574 return mInputSurface->connect(mComponent);
1575}
1576
1577status_t CCodecBufferChannel::signalEndOfInputStream() {
1578 if (mInputSurface == nullptr) {
1579 return INVALID_OPERATION;
1580 }
1581 return mInputSurface->signalEndOfInputStream();
1582}
1583
1584status_t CCodecBufferChannel::queueInputBufferInternal(const sp<MediaCodecBuffer> &buffer) {
1585 int64_t timeUs;
1586 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1587
1588 if (mInputMetEos) {
1589 ALOGD("[%s] buffers after EOS ignored (%lld us)", mName, (long long)timeUs);
1590 return OK;
1591 }
1592
1593 int32_t flags = 0;
1594 int32_t tmp = 0;
1595 bool eos = false;
1596 if (buffer->meta()->findInt32("eos", &tmp) && tmp) {
1597 eos = true;
1598 mInputMetEos = true;
1599 ALOGV("[%s] input EOS", mName);
1600 }
1601 if (buffer->meta()->findInt32("csd", &tmp) && tmp) {
1602 flags |= C2FrameData::FLAG_CODEC_CONFIG;
1603 }
1604 ALOGV("[%s] queueInputBuffer: buffer->size() = %zu", mName, buffer->size());
1605 std::unique_ptr<C2Work> work(new C2Work);
1606 work->input.ordinal.timestamp = timeUs;
1607 work->input.ordinal.frameIndex = mFrameIndex++;
1608 // WORKAROUND: until codecs support handling work after EOS and max output sizing, use timestamp
1609 // manipulation to achieve image encoding via video codec, and to constrain encoded output.
1610 // Keep client timestamp in customOrdinal
1611 work->input.ordinal.customOrdinal = timeUs;
1612 work->input.buffers.clear();
1613
1614 if (buffer->size() > 0u) {
1615 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
1616 std::shared_ptr<C2Buffer> c2buffer;
1617 if (!(*buffers)->releaseBuffer(buffer, &c2buffer)) {
1618 return -ENOENT;
1619 }
1620 work->input.buffers.push_back(c2buffer);
1621 } else {
1622 mAvailablePipelineCapacity.freeInputSlots(1, "queueInputBufferInternal");
1623 if (eos) {
1624 flags |= C2FrameData::FLAG_END_OF_STREAM;
1625 }
1626 }
1627 work->input.flags = (C2FrameData::flags_t)flags;
1628 // TODO: fill info's
1629
1630 work->input.configUpdate = std::move(mParamsToBeSet);
1631 work->worklets.clear();
1632 work->worklets.emplace_back(new C2Worklet);
1633
1634 std::list<std::unique_ptr<C2Work>> items;
1635 items.push_back(std::move(work));
1636 c2_status_t err = mComponent->queue(&items);
1637
1638 if (err == C2_OK && eos && buffer->size() > 0u) {
1639 mCCodecCallback->onWorkQueued(false);
1640 work.reset(new C2Work);
1641 work->input.ordinal.timestamp = timeUs;
1642 work->input.ordinal.frameIndex = mFrameIndex++;
1643 // WORKAROUND: keep client timestamp in customOrdinal
1644 work->input.ordinal.customOrdinal = timeUs;
1645 work->input.buffers.clear();
1646 work->input.flags = C2FrameData::FLAG_END_OF_STREAM;
1647
1648 items.clear();
1649 items.push_back(std::move(work));
1650 err = mComponent->queue(&items);
1651 }
1652 if (err == C2_OK) {
1653 mCCodecCallback->onWorkQueued(eos);
1654 }
1655
1656 feedInputBufferIfAvailableInternal();
1657 return err;
1658}
1659
1660status_t CCodecBufferChannel::setParameters(std::vector<std::unique_ptr<C2Param>> &params) {
1661 QueueGuard guard(mSync);
1662 if (!guard.isRunning()) {
1663 ALOGD("[%s] setParameters is only supported in the running state.", mName);
1664 return -ENOSYS;
1665 }
1666 mParamsToBeSet.insert(mParamsToBeSet.end(),
1667 std::make_move_iterator(params.begin()),
1668 std::make_move_iterator(params.end()));
1669 params.clear();
1670 return OK;
1671}
1672
1673status_t CCodecBufferChannel::queueInputBuffer(const sp<MediaCodecBuffer> &buffer) {
1674 QueueGuard guard(mSync);
1675 if (!guard.isRunning()) {
1676 ALOGD("[%s] No more buffers should be queued at current state.", mName);
1677 return -ENOSYS;
1678 }
1679 return queueInputBufferInternal(buffer);
1680}
1681
1682status_t CCodecBufferChannel::queueSecureInputBuffer(
1683 const sp<MediaCodecBuffer> &buffer, bool secure, const uint8_t *key,
1684 const uint8_t *iv, CryptoPlugin::Mode mode, CryptoPlugin::Pattern pattern,
1685 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
1686 AString *errorDetailMsg) {
1687 QueueGuard guard(mSync);
1688 if (!guard.isRunning()) {
1689 ALOGD("[%s] No more buffers should be queued at current state.", mName);
1690 return -ENOSYS;
1691 }
1692
1693 if (!hasCryptoOrDescrambler()) {
1694 return -ENOSYS;
1695 }
1696 sp<EncryptedLinearBlockBuffer> encryptedBuffer((EncryptedLinearBlockBuffer *)buffer.get());
1697
1698 ssize_t result = -1;
1699 ssize_t codecDataOffset = 0;
1700 if (mCrypto != nullptr) {
1701 ICrypto::DestinationBuffer destination;
1702 if (secure) {
1703 destination.mType = ICrypto::kDestinationTypeNativeHandle;
1704 destination.mHandle = encryptedBuffer->handle();
1705 } else {
1706 destination.mType = ICrypto::kDestinationTypeSharedMemory;
1707 destination.mSharedMemory = mDecryptDestination;
1708 }
1709 ICrypto::SourceBuffer source;
1710 encryptedBuffer->fillSourceBuffer(&source);
1711 result = mCrypto->decrypt(
1712 key, iv, mode, pattern, source, buffer->offset(),
1713 subSamples, numSubSamples, destination, errorDetailMsg);
1714 if (result < 0) {
1715 return result;
1716 }
1717 if (destination.mType == ICrypto::kDestinationTypeSharedMemory) {
1718 encryptedBuffer->copyDecryptedContent(mDecryptDestination, result);
1719 }
1720 } else {
1721 // Here we cast CryptoPlugin::SubSample to hardware::cas::native::V1_0::SubSample
1722 // directly, the structure definitions should match as checked in DescramblerImpl.cpp.
1723 hidl_vec<SubSample> hidlSubSamples;
1724 hidlSubSamples.setToExternal((SubSample *)subSamples, numSubSamples, false /*own*/);
1725
1726 hardware::cas::native::V1_0::SharedBuffer srcBuffer;
1727 encryptedBuffer->fillSourceBuffer(&srcBuffer);
1728
1729 DestinationBuffer dstBuffer;
1730 if (secure) {
1731 dstBuffer.type = BufferType::NATIVE_HANDLE;
1732 dstBuffer.secureMemory = hidl_handle(encryptedBuffer->handle());
1733 } else {
1734 dstBuffer.type = BufferType::SHARED_MEMORY;
1735 dstBuffer.nonsecureMemory = srcBuffer;
1736 }
1737
1738 CasStatus status = CasStatus::OK;
1739 hidl_string detailedError;
1740 ScramblingControl sctrl = ScramblingControl::UNSCRAMBLED;
1741
1742 if (key != nullptr) {
1743 sctrl = (ScramblingControl)key[0];
1744 // Adjust for the PES offset
1745 codecDataOffset = key[2] | (key[3] << 8);
1746 }
1747
1748 auto returnVoid = mDescrambler->descramble(
1749 sctrl,
1750 hidlSubSamples,
1751 srcBuffer,
1752 0,
1753 dstBuffer,
1754 0,
1755 [&status, &result, &detailedError] (
1756 CasStatus _status, uint32_t _bytesWritten,
1757 const hidl_string& _detailedError) {
1758 status = _status;
1759 result = (ssize_t)_bytesWritten;
1760 detailedError = _detailedError;
1761 });
1762
1763 if (!returnVoid.isOk() || status != CasStatus::OK || result < 0) {
1764 ALOGI("[%s] descramble failed, trans=%s, status=%d, result=%zd",
1765 mName, returnVoid.description().c_str(), status, result);
1766 return UNKNOWN_ERROR;
1767 }
1768
1769 if (result < codecDataOffset) {
1770 ALOGD("invalid codec data offset: %zd, result %zd", codecDataOffset, result);
1771 return BAD_VALUE;
1772 }
1773
1774 ALOGV("[%s] descramble succeeded, %zd bytes", mName, result);
1775
1776 if (dstBuffer.type == BufferType::SHARED_MEMORY) {
1777 encryptedBuffer->copyDecryptedContentFromMemory(result);
1778 }
1779 }
1780
1781 buffer->setRange(codecDataOffset, result - codecDataOffset);
1782 return queueInputBufferInternal(buffer);
1783}
1784
1785void CCodecBufferChannel::feedInputBufferIfAvailable() {
1786 QueueGuard guard(mSync);
1787 if (!guard.isRunning()) {
1788 ALOGV("[%s] We're not running --- no input buffer reported", mName);
1789 return;
1790 }
1791 feedInputBufferIfAvailableInternal();
1792}
1793
1794void CCodecBufferChannel::feedInputBufferIfAvailableInternal() {
1795 while (!mInputMetEos &&
1796 !mReorderStash.lock()->hasPending() &&
1797 mAvailablePipelineCapacity.allocate("feedInputBufferIfAvailable")) {
1798 sp<MediaCodecBuffer> inBuffer;
1799 size_t index;
1800 {
1801 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
1802 if (!(*buffers)->requestNewBuffer(&index, &inBuffer)) {
1803 ALOGV("[%s] no new buffer available", mName);
1804 mAvailablePipelineCapacity.free("feedInputBufferIfAvailable");
1805 break;
1806 }
1807 }
1808 ALOGV("[%s] new input index = %zu [%p]", mName, index, inBuffer.get());
1809 mCallback->onInputBufferAvailable(index, inBuffer);
1810 }
1811}
1812
1813status_t CCodecBufferChannel::renderOutputBuffer(
1814 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001815 ALOGV("[%s] renderOutputBuffer: %p", mName, buffer.get());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001816 std::shared_ptr<C2Buffer> c2Buffer;
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001817 bool released = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001818 {
1819 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
1820 if (*buffers) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001821 released = (*buffers)->releaseBuffer(buffer, &c2Buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001822 }
1823 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001824 // NOTE: some apps try to releaseOutputBuffer() with timestamp and/or render
1825 // set to true.
1826 sendOutputBuffers();
1827 // input buffer feeding may have been gated by pending output buffers
1828 feedInputBufferIfAvailable();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001829 if (!c2Buffer) {
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001830 if (released) {
1831 ALOGD("[%s] The app is calling releaseOutputBuffer() with "
1832 "timestamp or render=true with non-video buffers. Apps should "
1833 "call releaseOutputBuffer() with render=false for those.",
1834 mName);
1835 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001836 return INVALID_OPERATION;
1837 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001838
1839#if 0
1840 const std::vector<std::shared_ptr<const C2Info>> infoParams = c2Buffer->info();
1841 ALOGV("[%s] queuing gfx buffer with %zu infos", mName, infoParams.size());
1842 for (const std::shared_ptr<const C2Info> &info : infoParams) {
1843 AString res;
1844 for (size_t ix = 0; ix + 3 < info->size(); ix += 4) {
1845 if (ix) res.append(", ");
1846 res.append(*((int32_t*)info.get() + (ix / 4)));
1847 }
1848 ALOGV(" [%s]", res.c_str());
1849 }
1850#endif
1851 std::shared_ptr<const C2StreamRotationInfo::output> rotation =
1852 std::static_pointer_cast<const C2StreamRotationInfo::output>(
1853 c2Buffer->getInfo(C2StreamRotationInfo::output::PARAM_TYPE));
1854 bool flip = rotation && (rotation->flip & 1);
1855 uint32_t quarters = ((rotation ? rotation->value : 0) / 90) & 3;
1856 uint32_t transform = 0;
1857 switch (quarters) {
1858 case 0: // no rotation
1859 transform = flip ? HAL_TRANSFORM_FLIP_H : 0;
1860 break;
1861 case 1: // 90 degrees counter-clockwise
1862 transform = flip ? (HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90)
1863 : HAL_TRANSFORM_ROT_270;
1864 break;
1865 case 2: // 180 degrees
1866 transform = flip ? HAL_TRANSFORM_FLIP_V : HAL_TRANSFORM_ROT_180;
1867 break;
1868 case 3: // 90 degrees clockwise
1869 transform = flip ? (HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90)
1870 : HAL_TRANSFORM_ROT_90;
1871 break;
1872 }
1873
1874 std::shared_ptr<const C2StreamSurfaceScalingInfo::output> surfaceScaling =
1875 std::static_pointer_cast<const C2StreamSurfaceScalingInfo::output>(
1876 c2Buffer->getInfo(C2StreamSurfaceScalingInfo::output::PARAM_TYPE));
1877 uint32_t videoScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
1878 if (surfaceScaling) {
1879 videoScalingMode = surfaceScaling->value;
1880 }
1881
1882 // Use dataspace from format as it has the default aspects already applied
1883 android_dataspace_t dataSpace = HAL_DATASPACE_UNKNOWN; // this is 0
1884 (void)buffer->format()->findInt32("android._dataspace", (int32_t *)&dataSpace);
1885
1886 // HDR static info
1887 std::shared_ptr<const C2StreamHdrStaticInfo::output> hdrStaticInfo =
1888 std::static_pointer_cast<const C2StreamHdrStaticInfo::output>(
1889 c2Buffer->getInfo(C2StreamHdrStaticInfo::output::PARAM_TYPE));
1890
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001891 // HDR10 plus info
1892 std::shared_ptr<const C2StreamHdr10PlusInfo::output> hdr10PlusInfo =
1893 std::static_pointer_cast<const C2StreamHdr10PlusInfo::output>(
1894 c2Buffer->getInfo(C2StreamHdr10PlusInfo::output::PARAM_TYPE));
1895
Pawin Vongmasa36653902018-11-15 00:10:25 -08001896 {
1897 Mutexed<OutputSurface>::Locked output(mOutputSurface);
1898 if (output->surface == nullptr) {
1899 ALOGI("[%s] cannot render buffer without surface", mName);
1900 return OK;
1901 }
1902 }
1903
1904 std::vector<C2ConstGraphicBlock> blocks = c2Buffer->data().graphicBlocks();
1905 if (blocks.size() != 1u) {
1906 ALOGD("[%s] expected 1 graphic block, but got %zu", mName, blocks.size());
1907 return UNKNOWN_ERROR;
1908 }
1909 const C2ConstGraphicBlock &block = blocks.front();
1910
1911 // TODO: revisit this after C2Fence implementation.
1912 android::IGraphicBufferProducer::QueueBufferInput qbi(
1913 timestampNs,
1914 false, // droppable
1915 dataSpace,
1916 Rect(blocks.front().crop().left,
1917 blocks.front().crop().top,
1918 blocks.front().crop().right(),
1919 blocks.front().crop().bottom()),
1920 videoScalingMode,
1921 transform,
1922 Fence::NO_FENCE, 0);
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001923 if (hdrStaticInfo || hdr10PlusInfo) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001924 HdrMetadata hdr;
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001925 if (hdrStaticInfo) {
1926 struct android_smpte2086_metadata smpte2086_meta = {
1927 .displayPrimaryRed = {
1928 hdrStaticInfo->mastering.red.x, hdrStaticInfo->mastering.red.y
1929 },
1930 .displayPrimaryGreen = {
1931 hdrStaticInfo->mastering.green.x, hdrStaticInfo->mastering.green.y
1932 },
1933 .displayPrimaryBlue = {
1934 hdrStaticInfo->mastering.blue.x, hdrStaticInfo->mastering.blue.y
1935 },
1936 .whitePoint = {
1937 hdrStaticInfo->mastering.white.x, hdrStaticInfo->mastering.white.y
1938 },
1939 .maxLuminance = hdrStaticInfo->mastering.maxLuminance,
1940 .minLuminance = hdrStaticInfo->mastering.minLuminance,
1941 };
1942
1943 struct android_cta861_3_metadata cta861_meta = {
1944 .maxContentLightLevel = hdrStaticInfo->maxCll,
1945 .maxFrameAverageLightLevel = hdrStaticInfo->maxFall,
1946 };
1947
1948 hdr.validTypes = HdrMetadata::SMPTE2086 | HdrMetadata::CTA861_3;
1949 hdr.smpte2086 = smpte2086_meta;
1950 hdr.cta8613 = cta861_meta;
1951 }
1952 if (hdr10PlusInfo) {
1953 hdr.validTypes |= HdrMetadata::HDR10PLUS;
1954 hdr.hdr10plus.assign(
1955 hdr10PlusInfo->m.value,
1956 hdr10PlusInfo->m.value + hdr10PlusInfo->flexCount());
1957 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001958 qbi.setHdrMetadata(hdr);
1959 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001960 // we don't have dirty regions
1961 qbi.setSurfaceDamage(Region::INVALID_REGION);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001962 android::IGraphicBufferProducer::QueueBufferOutput qbo;
1963 status_t result = mComponent->queueToOutputSurface(block, qbi, &qbo);
1964 if (result != OK) {
1965 ALOGI("[%s] queueBuffer failed: %d", mName, result);
1966 return result;
1967 }
1968 ALOGV("[%s] queue buffer successful", mName);
1969
1970 int64_t mediaTimeUs = 0;
1971 (void)buffer->meta()->findInt64("timeUs", &mediaTimeUs);
1972 mCCodecCallback->onOutputFramesRendered(mediaTimeUs, timestampNs);
1973
1974 return OK;
1975}
1976
1977status_t CCodecBufferChannel::discardBuffer(const sp<MediaCodecBuffer> &buffer) {
1978 ALOGV("[%s] discardBuffer: %p", mName, buffer.get());
1979 bool released = false;
1980 {
1981 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
1982 if (*buffers && (*buffers)->releaseBuffer(buffer, nullptr)) {
1983 buffers.unlock();
1984 released = true;
1985 mAvailablePipelineCapacity.freeInputSlots(1, "discardBuffer");
1986 }
1987 }
1988 {
1989 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
1990 if (*buffers && (*buffers)->releaseBuffer(buffer, nullptr)) {
1991 buffers.unlock();
1992 released = true;
1993 }
1994 }
1995 if (released) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001996 sendOutputBuffers();
Pawin Vongmasa8be93112018-12-11 14:01:42 -08001997 feedInputBufferIfAvailable();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001998 } else {
1999 ALOGD("[%s] MediaCodec discarded an unknown buffer", mName);
2000 }
2001 return OK;
2002}
2003
2004void CCodecBufferChannel::getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
2005 array->clear();
2006 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2007
2008 if (!(*buffers)->isArrayMode()) {
2009 *buffers = (*buffers)->toArrayMode(kMinInputBufferArraySize);
2010 }
2011
2012 (*buffers)->getArray(array);
2013}
2014
2015void CCodecBufferChannel::getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
2016 array->clear();
2017 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2018
2019 if (!(*buffers)->isArrayMode()) {
2020 *buffers = (*buffers)->toArrayMode(kMinOutputBufferArraySize);
2021 }
2022
2023 (*buffers)->getArray(array);
2024}
2025
2026status_t CCodecBufferChannel::start(
2027 const sp<AMessage> &inputFormat, const sp<AMessage> &outputFormat) {
2028 C2StreamBufferTypeSetting::input iStreamFormat(0u);
2029 C2StreamBufferTypeSetting::output oStreamFormat(0u);
2030 C2PortReorderBufferDepthTuning::output reorderDepth;
2031 C2PortReorderKeySetting::output reorderKey;
2032 c2_status_t err = mComponent->query(
2033 {
2034 &iStreamFormat,
2035 &oStreamFormat,
2036 &reorderDepth,
2037 &reorderKey,
2038 },
2039 {},
2040 C2_DONT_BLOCK,
2041 nullptr);
2042 if (err == C2_BAD_INDEX) {
2043 if (!iStreamFormat || !oStreamFormat) {
2044 return UNKNOWN_ERROR;
2045 }
2046 } else if (err != C2_OK) {
2047 return UNKNOWN_ERROR;
2048 }
2049
2050 {
2051 Mutexed<ReorderStash>::Locked reorder(mReorderStash);
2052 reorder->clear();
2053 if (reorderDepth) {
2054 reorder->setDepth(reorderDepth.value);
2055 }
2056 if (reorderKey) {
2057 reorder->setKey(reorderKey.value);
2058 }
2059 }
2060 // TODO: get this from input format
2061 bool secure = mComponent->getName().find(".secure") != std::string::npos;
2062
2063 std::shared_ptr<C2AllocatorStore> allocatorStore = GetCodec2PlatformAllocatorStore();
2064 int poolMask = property_get_int32(
2065 "debug.stagefright.c2-poolmask",
2066 1 << C2PlatformAllocatorStore::ION |
2067 1 << C2PlatformAllocatorStore::BUFFERQUEUE);
2068
2069 if (inputFormat != nullptr) {
2070 bool graphic = (iStreamFormat.value == C2FormatVideo);
2071 std::shared_ptr<C2BlockPool> pool;
2072 {
2073 Mutexed<BlockPools>::Locked pools(mBlockPools);
2074
2075 // set default allocator ID.
2076 pools->inputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
2077 : C2PlatformAllocatorStore::ION;
2078
2079 // query C2PortAllocatorsTuning::input from component. If an allocator ID is obtained
2080 // from component, create the input block pool with given ID. Otherwise, use default IDs.
2081 std::vector<std::unique_ptr<C2Param>> params;
2082 err = mComponent->query({ },
2083 { C2PortAllocatorsTuning::input::PARAM_TYPE },
2084 C2_DONT_BLOCK,
2085 &params);
2086 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
2087 ALOGD("[%s] Query input allocators returned %zu params => %s (%u)",
2088 mName, params.size(), asString(err), err);
2089 } else if (err == C2_OK && params.size() == 1) {
2090 C2PortAllocatorsTuning::input *inputAllocators =
2091 C2PortAllocatorsTuning::input::From(params[0].get());
2092 if (inputAllocators && inputAllocators->flexCount() > 0) {
2093 std::shared_ptr<C2Allocator> allocator;
2094 // verify allocator IDs and resolve default allocator
2095 allocatorStore->fetchAllocator(inputAllocators->m.values[0], &allocator);
2096 if (allocator) {
2097 pools->inputAllocatorId = allocator->getId();
2098 } else {
2099 ALOGD("[%s] component requested invalid input allocator ID %u",
2100 mName, inputAllocators->m.values[0]);
2101 }
2102 }
2103 }
2104
2105 // TODO: use C2Component wrapper to associate this pool with ourselves
2106 if ((poolMask >> pools->inputAllocatorId) & 1) {
2107 err = CreateCodec2BlockPool(pools->inputAllocatorId, nullptr, &pool);
2108 ALOGD("[%s] Created input block pool with allocatorID %u => poolID %llu - %s (%d)",
2109 mName, pools->inputAllocatorId,
2110 (unsigned long long)(pool ? pool->getLocalId() : 111000111),
2111 asString(err), err);
2112 } else {
2113 err = C2_NOT_FOUND;
2114 }
2115 if (err != C2_OK) {
2116 C2BlockPool::local_id_t inputPoolId =
2117 graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
2118 err = GetCodec2BlockPool(inputPoolId, nullptr, &pool);
2119 ALOGD("[%s] Using basic input block pool with poolID %llu => got %llu - %s (%d)",
2120 mName, (unsigned long long)inputPoolId,
2121 (unsigned long long)(pool ? pool->getLocalId() : 111000111),
2122 asString(err), err);
2123 if (err != C2_OK) {
2124 return NO_MEMORY;
2125 }
2126 }
2127 pools->inputPool = pool;
2128 }
2129
Wonsik Kim51051262018-11-28 13:59:05 -08002130 bool forceArrayMode = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002131 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2132 if (graphic) {
2133 if (mInputSurface) {
2134 buffers->reset(new DummyInputBuffers(mName));
2135 } else if (mMetaMode == MODE_ANW) {
2136 buffers->reset(new GraphicMetadataInputBuffers(mName));
2137 } else {
2138 buffers->reset(new GraphicInputBuffers(mName));
2139 }
2140 } else {
2141 if (hasCryptoOrDescrambler()) {
2142 int32_t capacity = kLinearBufferSize;
2143 (void)inputFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
2144 if ((size_t)capacity > kMaxLinearBufferSize) {
2145 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
2146 capacity = kMaxLinearBufferSize;
2147 }
2148 if (mDealer == nullptr) {
2149 mDealer = new MemoryDealer(
2150 align(capacity, MemoryDealer::getAllocationAlignment())
2151 * (kMinInputBufferArraySize + 1),
2152 "EncryptedLinearInputBuffers");
2153 mDecryptDestination = mDealer->allocate((size_t)capacity);
2154 }
2155 if (mCrypto != nullptr && mHeapSeqNum < 0) {
2156 mHeapSeqNum = mCrypto->setHeap(mDealer->getMemoryHeap());
2157 } else {
2158 mHeapSeqNum = -1;
2159 }
2160 buffers->reset(new EncryptedLinearInputBuffers(
2161 secure, mDealer, mCrypto, mHeapSeqNum, (size_t)capacity, mName));
Wonsik Kim51051262018-11-28 13:59:05 -08002162 forceArrayMode = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -08002163 } else {
2164 buffers->reset(new LinearInputBuffers(mName));
2165 }
2166 }
2167 (*buffers)->setFormat(inputFormat);
2168
2169 if (err == C2_OK) {
2170 (*buffers)->setPool(pool);
2171 } else {
2172 // TODO: error
2173 }
Wonsik Kim51051262018-11-28 13:59:05 -08002174
2175 if (forceArrayMode) {
2176 *buffers = (*buffers)->toArrayMode(kMinInputBufferArraySize);
2177 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08002178 }
2179
2180 if (outputFormat != nullptr) {
2181 sp<IGraphicBufferProducer> outputSurface;
2182 uint32_t outputGeneration;
2183 {
2184 Mutexed<OutputSurface>::Locked output(mOutputSurface);
2185 outputSurface = output->surface ?
2186 output->surface->getIGraphicBufferProducer() : nullptr;
2187 outputGeneration = output->generation;
2188 }
2189
2190 bool graphic = (oStreamFormat.value == C2FormatVideo);
2191 C2BlockPool::local_id_t outputPoolId_;
2192
2193 {
2194 Mutexed<BlockPools>::Locked pools(mBlockPools);
2195
2196 // set default allocator ID.
2197 pools->outputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
2198 : C2PlatformAllocatorStore::ION;
2199
2200 // query C2PortAllocatorsTuning::output from component, or use default allocator if
2201 // unsuccessful.
2202 std::vector<std::unique_ptr<C2Param>> params;
2203 err = mComponent->query({ },
2204 { C2PortAllocatorsTuning::output::PARAM_TYPE },
2205 C2_DONT_BLOCK,
2206 &params);
2207 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
2208 ALOGD("[%s] Query output allocators returned %zu params => %s (%u)",
2209 mName, params.size(), asString(err), err);
2210 } else if (err == C2_OK && params.size() == 1) {
2211 C2PortAllocatorsTuning::output *outputAllocators =
2212 C2PortAllocatorsTuning::output::From(params[0].get());
2213 if (outputAllocators && outputAllocators->flexCount() > 0) {
2214 std::shared_ptr<C2Allocator> allocator;
2215 // verify allocator IDs and resolve default allocator
2216 allocatorStore->fetchAllocator(outputAllocators->m.values[0], &allocator);
2217 if (allocator) {
2218 pools->outputAllocatorId = allocator->getId();
2219 } else {
2220 ALOGD("[%s] component requested invalid output allocator ID %u",
2221 mName, outputAllocators->m.values[0]);
2222 }
2223 }
2224 }
2225
2226 // use bufferqueue if outputting to a surface.
2227 // query C2PortSurfaceAllocatorTuning::output from component, or use default allocator
2228 // if unsuccessful.
2229 if (outputSurface) {
2230 params.clear();
2231 err = mComponent->query({ },
2232 { C2PortSurfaceAllocatorTuning::output::PARAM_TYPE },
2233 C2_DONT_BLOCK,
2234 &params);
2235 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
2236 ALOGD("[%s] Query output surface allocator returned %zu params => %s (%u)",
2237 mName, params.size(), asString(err), err);
2238 } else if (err == C2_OK && params.size() == 1) {
2239 C2PortSurfaceAllocatorTuning::output *surfaceAllocator =
2240 C2PortSurfaceAllocatorTuning::output::From(params[0].get());
2241 if (surfaceAllocator) {
2242 std::shared_ptr<C2Allocator> allocator;
2243 // verify allocator IDs and resolve default allocator
2244 allocatorStore->fetchAllocator(surfaceAllocator->value, &allocator);
2245 if (allocator) {
2246 pools->outputAllocatorId = allocator->getId();
2247 } else {
2248 ALOGD("[%s] component requested invalid surface output allocator ID %u",
2249 mName, surfaceAllocator->value);
2250 err = C2_BAD_VALUE;
2251 }
2252 }
2253 }
2254 if (pools->outputAllocatorId == C2PlatformAllocatorStore::GRALLOC
2255 && err != C2_OK
2256 && ((poolMask >> C2PlatformAllocatorStore::BUFFERQUEUE) & 1)) {
2257 pools->outputAllocatorId = C2PlatformAllocatorStore::BUFFERQUEUE;
2258 }
2259 }
2260
2261 if ((poolMask >> pools->outputAllocatorId) & 1) {
2262 err = mComponent->createBlockPool(
2263 pools->outputAllocatorId, &pools->outputPoolId, &pools->outputPoolIntf);
2264 ALOGI("[%s] Created output block pool with allocatorID %u => poolID %llu - %s",
2265 mName, pools->outputAllocatorId,
2266 (unsigned long long)pools->outputPoolId,
2267 asString(err));
2268 } else {
2269 err = C2_NOT_FOUND;
2270 }
2271 if (err != C2_OK) {
2272 // use basic pool instead
2273 pools->outputPoolId =
2274 graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
2275 }
2276
2277 // Configure output block pool ID as parameter C2PortBlockPoolsTuning::output to
2278 // component.
2279 std::unique_ptr<C2PortBlockPoolsTuning::output> poolIdsTuning =
2280 C2PortBlockPoolsTuning::output::AllocUnique({ pools->outputPoolId });
2281
2282 std::vector<std::unique_ptr<C2SettingResult>> failures;
2283 err = mComponent->config({ poolIdsTuning.get() }, C2_MAY_BLOCK, &failures);
2284 ALOGD("[%s] Configured output block pool ids %llu => %s",
2285 mName, (unsigned long long)poolIdsTuning->m.values[0], asString(err));
2286 outputPoolId_ = pools->outputPoolId;
2287 }
2288
2289 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2290
2291 if (graphic) {
2292 if (outputSurface) {
2293 buffers->reset(new GraphicOutputBuffers(mName));
2294 } else {
2295 buffers->reset(new RawGraphicOutputBuffers(mName));
2296 }
2297 } else {
2298 buffers->reset(new LinearOutputBuffers(mName));
2299 }
2300 (*buffers)->setFormat(outputFormat->dup());
2301
2302
2303 // Try to set output surface to created block pool if given.
2304 if (outputSurface) {
2305 mComponent->setOutputSurface(
2306 outputPoolId_,
2307 outputSurface,
2308 outputGeneration);
2309 }
2310
2311 if (oStreamFormat.value == C2BufferData::LINEAR
2312 && mComponentName.find("c2.qti.") == std::string::npos) {
2313 // WORKAROUND: if we're using early CSD workaround we convert to
2314 // array mode, to appease apps assuming the output
2315 // buffers to be of the same size.
2316 (*buffers) = (*buffers)->toArrayMode(kMinOutputBufferArraySize);
2317
2318 int32_t channelCount;
2319 int32_t sampleRate;
2320 if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount)
2321 && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
2322 int32_t delay = 0;
2323 int32_t padding = 0;;
2324 if (!outputFormat->findInt32("encoder-delay", &delay)) {
2325 delay = 0;
2326 }
2327 if (!outputFormat->findInt32("encoder-padding", &padding)) {
2328 padding = 0;
2329 }
2330 if (delay || padding) {
2331 // We need write access to the buffers, and we're already in
2332 // array mode.
2333 (*buffers)->initSkipCutBuffer(delay, padding, sampleRate, channelCount);
2334 }
2335 }
2336 }
2337 }
2338
2339 // Set up pipeline control. This has to be done after mInputBuffers and
2340 // mOutputBuffers are initialized to make sure that lingering callbacks
2341 // about buffers from the previous generation do not interfere with the
2342 // newly initialized pipeline capacity.
2343
2344 // Query delays
2345 C2PortRequestedDelayTuning::input inputDelay;
2346 C2PortRequestedDelayTuning::output outputDelay;
2347 C2RequestedPipelineDelayTuning pipelineDelay;
2348#if 0
2349 err = mComponent->query(
2350 { &inputDelay, &pipelineDelay, &outputDelay },
2351 {},
2352 C2_DONT_BLOCK,
2353 nullptr);
2354 mAvailablePipelineCapacity.initialize(
2355 inputDelay,
2356 inputDelay + pipelineDelay,
2357 inputDelay + pipelineDelay + outputDelay,
2358 mName);
2359#else
2360 mAvailablePipelineCapacity.initialize(
2361 kMinInputBufferArraySize,
2362 kMaxPipelineCapacity,
2363 mName);
2364#endif
2365
2366 mInputMetEos = false;
2367 mSync.start();
2368 return OK;
2369}
2370
2371status_t CCodecBufferChannel::requestInitialInputBuffers() {
2372 if (mInputSurface) {
2373 return OK;
2374 }
2375
2376 C2StreamFormatConfig::output oStreamFormat(0u);
2377 c2_status_t err = mComponent->query({ &oStreamFormat }, {}, C2_DONT_BLOCK, nullptr);
2378 if (err != C2_OK) {
2379 return UNKNOWN_ERROR;
2380 }
2381 std::vector<sp<MediaCodecBuffer>> toBeQueued;
2382 // TODO: use proper buffer depth instead of this random value
2383 for (size_t i = 0; i < kMinInputBufferArraySize; ++i) {
2384 size_t index;
2385 sp<MediaCodecBuffer> buffer;
2386 {
2387 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2388 if (!(*buffers)->requestNewBuffer(&index, &buffer)) {
2389 if (i == 0) {
2390 ALOGW("[%s] start: cannot allocate memory at all", mName);
2391 return NO_MEMORY;
2392 } else {
2393 ALOGV("[%s] start: cannot allocate memory, only %zu buffers allocated",
2394 mName, i);
2395 }
2396 break;
2397 }
2398 }
2399 if (buffer) {
2400 Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs);
2401 ALOGV("[%s] input buffer %zu available", mName, index);
2402 bool post = true;
2403 if (!configs->empty()) {
2404 sp<ABuffer> config = configs->front();
2405 if (buffer->capacity() >= config->size()) {
2406 memcpy(buffer->base(), config->data(), config->size());
2407 buffer->setRange(0, config->size());
2408 buffer->meta()->clear();
2409 buffer->meta()->setInt64("timeUs", 0);
2410 buffer->meta()->setInt32("csd", 1);
2411 post = false;
2412 } else {
2413 ALOGD("[%s] buffer capacity too small for the config (%zu < %zu)",
2414 mName, buffer->capacity(), config->size());
2415 }
2416 } else if (oStreamFormat.value == C2BufferData::LINEAR && i == 0
2417 && mComponentName.find("c2.qti.") == std::string::npos) {
2418 // WORKAROUND: Some apps expect CSD available without queueing
2419 // any input. Queue an empty buffer to get the CSD.
2420 buffer->setRange(0, 0);
2421 buffer->meta()->clear();
2422 buffer->meta()->setInt64("timeUs", 0);
2423 post = false;
2424 }
2425 if (mAvailablePipelineCapacity.allocate("requestInitialInputBuffers")) {
2426 if (post) {
2427 mCallback->onInputBufferAvailable(index, buffer);
2428 } else {
2429 toBeQueued.emplace_back(buffer);
2430 }
2431 } else {
2432 ALOGD("[%s] pipeline is full while requesting %zu-th input buffer",
2433 mName, i);
2434 }
2435 }
2436 }
2437 for (const sp<MediaCodecBuffer> &buffer : toBeQueued) {
2438 if (queueInputBufferInternal(buffer) != OK) {
2439 mAvailablePipelineCapacity.freeComponentSlot("requestInitialInputBuffers");
2440 }
2441 }
2442 return OK;
2443}
2444
2445void CCodecBufferChannel::stop() {
2446 mSync.stop();
2447 mFirstValidFrameIndex = mFrameIndex.load(std::memory_order_relaxed);
2448 if (mInputSurface != nullptr) {
2449 mInputSurface->disconnect();
2450 mInputSurface.reset();
2451 }
2452}
2453
2454void CCodecBufferChannel::flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) {
2455 ALOGV("[%s] flush", mName);
2456 {
2457 Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs);
2458 for (const std::unique_ptr<C2Work> &work : flushedWork) {
2459 if (!(work->input.flags & C2FrameData::FLAG_CODEC_CONFIG)) {
2460 continue;
2461 }
2462 if (work->input.buffers.empty()
2463 || work->input.buffers.front()->data().linearBlocks().empty()) {
2464 ALOGD("[%s] no linear codec config data found", mName);
2465 continue;
2466 }
2467 C2ReadView view =
2468 work->input.buffers.front()->data().linearBlocks().front().map().get();
2469 if (view.error() != C2_OK) {
2470 ALOGD("[%s] failed to map flushed codec config data: %d", mName, view.error());
2471 continue;
2472 }
2473 configs->push_back(ABuffer::CreateAsCopy(view.data(), view.capacity()));
2474 ALOGV("[%s] stashed flushed codec config data (size=%u)", mName, view.capacity());
2475 }
2476 }
2477 {
2478 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2479 (*buffers)->flush();
2480 }
2481 {
2482 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2483 (*buffers)->flush(flushedWork);
2484 }
2485}
2486
2487void CCodecBufferChannel::onWorkDone(
2488 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
2489 const C2StreamInitDataInfo::output *initData,
2490 size_t numDiscardedInputBuffers) {
2491 if (handleWork(std::move(work), outputFormat, initData)) {
2492 mAvailablePipelineCapacity.freeInputSlots(numDiscardedInputBuffers,
2493 "onWorkDone");
2494 feedInputBufferIfAvailable();
2495 }
2496}
2497
2498void CCodecBufferChannel::onInputBufferDone(
2499 const std::shared_ptr<C2Buffer>& buffer) {
2500 bool newInputSlotAvailable;
2501 {
2502 Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2503 newInputSlotAvailable = (*buffers)->expireComponentBuffer(buffer);
2504 if (newInputSlotAvailable) {
2505 mAvailablePipelineCapacity.freeInputSlots(1, "onInputBufferDone");
2506 }
2507 }
2508 if (newInputSlotAvailable) {
2509 feedInputBufferIfAvailable();
2510 }
2511}
2512
2513bool CCodecBufferChannel::handleWork(
2514 std::unique_ptr<C2Work> work,
2515 const sp<AMessage> &outputFormat,
2516 const C2StreamInitDataInfo::output *initData) {
2517 if ((work->input.ordinal.frameIndex - mFirstValidFrameIndex.load()).peek() < 0) {
2518 // Discard frames from previous generation.
2519 ALOGD("[%s] Discard frames from previous generation.", mName);
2520 return false;
2521 }
2522
2523 if (work->worklets.size() != 1u
2524 || !work->worklets.front()
2525 || !(work->worklets.front()->output.flags & C2FrameData::FLAG_INCOMPLETE)) {
2526 mAvailablePipelineCapacity.freeComponentSlot("handleWork");
2527 }
2528
2529 if (work->result == C2_NOT_FOUND) {
2530 ALOGD("[%s] flushed work; ignored.", mName);
2531 return true;
2532 }
2533
2534 if (work->result != C2_OK) {
2535 ALOGD("[%s] work failed to complete: %d", mName, work->result);
2536 mCCodecCallback->onError(work->result, ACTION_CODE_FATAL);
2537 return false;
2538 }
2539
2540 // NOTE: MediaCodec usage supposedly have only one worklet
2541 if (work->worklets.size() != 1u) {
2542 ALOGI("[%s] onWorkDone: incorrect number of worklets: %zu",
2543 mName, work->worklets.size());
2544 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2545 return false;
2546 }
2547
2548 const std::unique_ptr<C2Worklet> &worklet = work->worklets.front();
2549
2550 std::shared_ptr<C2Buffer> buffer;
2551 // NOTE: MediaCodec usage supposedly have only one output stream.
2552 if (worklet->output.buffers.size() > 1u) {
2553 ALOGI("[%s] onWorkDone: incorrect number of output buffers: %zu",
2554 mName, worklet->output.buffers.size());
2555 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2556 return false;
2557 } else if (worklet->output.buffers.size() == 1u) {
2558 buffer = worklet->output.buffers[0];
2559 if (!buffer) {
2560 ALOGD("[%s] onWorkDone: nullptr found in buffers; ignored.", mName);
2561 }
2562 }
2563
2564 while (!worklet->output.configUpdate.empty()) {
2565 std::unique_ptr<C2Param> param;
2566 worklet->output.configUpdate.back().swap(param);
2567 worklet->output.configUpdate.pop_back();
2568 switch (param->coreIndex().coreIndex()) {
2569 case C2PortReorderBufferDepthTuning::CORE_INDEX: {
2570 C2PortReorderBufferDepthTuning::output reorderDepth;
2571 if (reorderDepth.updateFrom(*param)) {
2572 mReorderStash.lock()->setDepth(reorderDepth.value);
2573 ALOGV("[%s] onWorkDone: updated reorder depth to %u",
2574 mName, reorderDepth.value);
2575 } else {
2576 ALOGD("[%s] onWorkDone: failed to read reorder depth", mName);
2577 }
2578 break;
2579 }
2580 case C2PortReorderKeySetting::CORE_INDEX: {
2581 C2PortReorderKeySetting::output reorderKey;
2582 if (reorderKey.updateFrom(*param)) {
2583 mReorderStash.lock()->setKey(reorderKey.value);
2584 ALOGV("[%s] onWorkDone: updated reorder key to %u",
2585 mName, reorderKey.value);
2586 } else {
2587 ALOGD("[%s] onWorkDone: failed to read reorder key", mName);
2588 }
2589 break;
2590 }
2591 default:
2592 ALOGV("[%s] onWorkDone: unrecognized config update (%08X)",
2593 mName, param->index());
2594 break;
2595 }
2596 }
2597
2598 if (outputFormat != nullptr) {
2599 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2600 ALOGD("[%s] onWorkDone: output format changed to %s",
2601 mName, outputFormat->debugString().c_str());
2602 (*buffers)->setFormat(outputFormat);
2603
2604 AString mediaType;
2605 if (outputFormat->findString(KEY_MIME, &mediaType)
2606 && mediaType == MIMETYPE_AUDIO_RAW) {
2607 int32_t channelCount;
2608 int32_t sampleRate;
2609 if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount)
2610 && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
2611 (*buffers)->updateSkipCutBuffer(sampleRate, channelCount);
2612 }
2613 }
2614 }
2615
2616 int32_t flags = 0;
2617 if (worklet->output.flags & C2FrameData::FLAG_END_OF_STREAM) {
2618 flags |= MediaCodec::BUFFER_FLAG_EOS;
2619 ALOGV("[%s] onWorkDone: output EOS", mName);
2620 }
2621
2622 sp<MediaCodecBuffer> outBuffer;
2623 size_t index;
2624
2625 // WORKAROUND: adjust output timestamp based on client input timestamp and codec
2626 // input timestamp. Codec output timestamp (in the timestamp field) shall correspond to
2627 // the codec input timestamp, but client output timestamp should (reported in timeUs)
2628 // shall correspond to the client input timesamp (in customOrdinal). By using the
2629 // delta between the two, this allows for some timestamp deviation - e.g. if one input
2630 // produces multiple output.
2631 c2_cntr64_t timestamp =
2632 worklet->output.ordinal.timestamp + work->input.ordinal.customOrdinal
2633 - work->input.ordinal.timestamp;
2634 ALOGV("[%s] onWorkDone: input %lld, codec %lld => output %lld => %lld",
2635 mName,
2636 work->input.ordinal.customOrdinal.peekll(),
2637 work->input.ordinal.timestamp.peekll(),
2638 worklet->output.ordinal.timestamp.peekll(),
2639 timestamp.peekll());
2640
2641 if (initData != nullptr) {
2642 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2643 if ((*buffers)->registerCsd(initData, &index, &outBuffer) == OK) {
2644 outBuffer->meta()->setInt64("timeUs", timestamp.peek());
2645 outBuffer->meta()->setInt32("flags", MediaCodec::BUFFER_FLAG_CODECCONFIG);
2646 ALOGV("[%s] onWorkDone: csd index = %zu [%p]", mName, index, outBuffer.get());
2647
2648 buffers.unlock();
2649 mCallback->onOutputBufferAvailable(index, outBuffer);
2650 buffers.lock();
2651 } else {
2652 ALOGD("[%s] onWorkDone: unable to register csd", mName);
2653 buffers.unlock();
2654 mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2655 buffers.lock();
2656 return false;
2657 }
2658 }
2659
2660 if (!buffer && !flags) {
2661 ALOGV("[%s] onWorkDone: Not reporting output buffer (%lld)",
2662 mName, work->input.ordinal.frameIndex.peekull());
2663 return true;
2664 }
2665
2666 if (buffer) {
2667 for (const std::shared_ptr<const C2Info> &info : buffer->info()) {
2668 // TODO: properly translate these to metadata
2669 switch (info->coreIndex().coreIndex()) {
2670 case C2StreamPictureTypeMaskInfo::CORE_INDEX:
2671 if (((C2StreamPictureTypeMaskInfo *)info.get())->value & C2PictureTypeKeyFrame) {
2672 flags |= MediaCodec::BUFFER_FLAG_SYNCFRAME;
2673 }
2674 break;
2675 default:
2676 break;
2677 }
2678 }
2679 }
2680
2681 {
2682 Mutexed<ReorderStash>::Locked reorder(mReorderStash);
2683 reorder->emplace(buffer, timestamp.peek(), flags, worklet->output.ordinal);
2684 if (flags & MediaCodec::BUFFER_FLAG_EOS) {
2685 // Flush reorder stash
2686 reorder->setDepth(0);
2687 }
2688 }
2689 sendOutputBuffers();
2690 return true;
2691}
2692
2693void CCodecBufferChannel::sendOutputBuffers() {
2694 ReorderStash::Entry entry;
2695 sp<MediaCodecBuffer> outBuffer;
2696 size_t index;
2697
2698 while (true) {
2699 {
2700 Mutexed<ReorderStash>::Locked reorder(mReorderStash);
2701 if (!reorder->hasPending()) {
2702 break;
2703 }
2704 if (!reorder->pop(&entry)) {
2705 break;
2706 }
2707 }
2708 Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2709 status_t err = (*buffers)->registerBuffer(entry.buffer, &index, &outBuffer);
2710 if (err != OK) {
2711 if (err != WOULD_BLOCK) {
2712 OutputBuffersArray *array = (OutputBuffersArray *)buffers->get();
2713 array->realloc(entry.buffer);
2714 mCCodecCallback->onOutputBuffersChanged();
2715 }
2716 buffers.unlock();
2717 ALOGV("[%s] sendOutputBuffers: unable to register output buffer", mName);
2718 mReorderStash.lock()->defer(entry);
2719 return;
2720 }
2721 buffers.unlock();
2722
2723 outBuffer->meta()->setInt64("timeUs", entry.timestamp);
2724 outBuffer->meta()->setInt32("flags", entry.flags);
2725 ALOGV("[%s] sendOutputBuffers: out buffer index = %zu [%p] => %p + %zu",
2726 mName, index, outBuffer.get(), outBuffer->data(), outBuffer->size());
2727 mCallback->onOutputBufferAvailable(index, outBuffer);
2728 }
2729}
2730
2731status_t CCodecBufferChannel::setSurface(const sp<Surface> &newSurface) {
2732 static std::atomic_uint32_t surfaceGeneration{0};
2733 uint32_t generation = (getpid() << 10) |
2734 ((surfaceGeneration.fetch_add(1, std::memory_order_relaxed) + 1)
2735 & ((1 << 10) - 1));
2736
2737 sp<IGraphicBufferProducer> producer;
2738 if (newSurface) {
2739 newSurface->setScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2740 newSurface->setMaxDequeuedBufferCount(kMinOutputBufferArraySize);
2741 producer = newSurface->getIGraphicBufferProducer();
2742 producer->setGenerationNumber(generation);
2743 } else {
2744 ALOGE("[%s] setting output surface to null", mName);
2745 return INVALID_OPERATION;
2746 }
2747
2748 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
2749 C2BlockPool::local_id_t outputPoolId;
2750 {
2751 Mutexed<BlockPools>::Locked pools(mBlockPools);
2752 outputPoolId = pools->outputPoolId;
2753 outputPoolIntf = pools->outputPoolIntf;
2754 }
2755
2756 if (outputPoolIntf) {
2757 if (mComponent->setOutputSurface(
2758 outputPoolId,
2759 producer,
2760 generation) != C2_OK) {
2761 ALOGI("[%s] setSurface: component setOutputSurface failed", mName);
2762 return INVALID_OPERATION;
2763 }
2764 }
2765
2766 {
2767 Mutexed<OutputSurface>::Locked output(mOutputSurface);
2768 output->surface = newSurface;
2769 output->generation = generation;
2770 }
2771
2772 return OK;
2773}
2774
2775void CCodecBufferChannel::setMetaMode(MetaMode mode) {
2776 mMetaMode = mode;
2777}
2778
2779status_t toStatusT(c2_status_t c2s, c2_operation_t c2op) {
2780 // C2_OK is always translated to OK.
2781 if (c2s == C2_OK) {
2782 return OK;
2783 }
2784
2785 // Operation-dependent translation
2786 // TODO: Add as necessary
2787 switch (c2op) {
2788 case C2_OPERATION_Component_start:
2789 switch (c2s) {
2790 case C2_NO_MEMORY:
2791 return NO_MEMORY;
2792 default:
2793 return UNKNOWN_ERROR;
2794 }
2795 default:
2796 break;
2797 }
2798
2799 // Backup operation-agnostic translation
2800 switch (c2s) {
2801 case C2_BAD_INDEX:
2802 return BAD_INDEX;
2803 case C2_BAD_VALUE:
2804 return BAD_VALUE;
2805 case C2_BLOCKING:
2806 return WOULD_BLOCK;
2807 case C2_DUPLICATE:
2808 return ALREADY_EXISTS;
2809 case C2_NO_INIT:
2810 return NO_INIT;
2811 case C2_NO_MEMORY:
2812 return NO_MEMORY;
2813 case C2_NOT_FOUND:
2814 return NAME_NOT_FOUND;
2815 case C2_TIMED_OUT:
2816 return TIMED_OUT;
2817 case C2_BAD_STATE:
2818 case C2_CANCELED:
2819 case C2_CANNOT_DO:
2820 case C2_CORRUPTED:
2821 case C2_OMITTED:
2822 case C2_REFUSED:
2823 return UNKNOWN_ERROR;
2824 default:
2825 return -static_cast<status_t>(c2s);
2826 }
2827}
2828
2829} // namespace android