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