blob: 6335f1345ac822a54233b64adc53fb66bd3cbcf1 [file] [log] [blame]
Wonsik Kim469c8342019-04-11 16:46:09 -07001/*
2 * Copyright 2019, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef CCODEC_BUFFERS_H_
18
19#define CCODEC_BUFFERS_H_
20
Wonsik Kim6f23cfc2021-09-24 05:45:52 -070021#include <optional>
Wonsik Kim469c8342019-04-11 16:46:09 -070022#include <string>
23
24#include <C2Config.h>
Wonsik Kim6f23cfc2021-09-24 05:45:52 -070025#include <DataConverter.h>
Wonsik Kim469c8342019-04-11 16:46:09 -070026#include <media/stagefright/foundation/AMessage.h>
27#include <media/MediaCodecBuffer.h>
28
29#include "Codec2Buffer.h"
Wonsik Kim469c8342019-04-11 16:46:09 -070030
31namespace android {
32
Wonsik Kim41d83432020-04-27 16:40:49 -070033struct ICrypto;
34class MemoryDealer;
Wonsik Kim155d5cb2019-10-09 12:49:49 -070035class SkipCutBuffer;
36
Wonsik Kim469c8342019-04-11 16:46:09 -070037constexpr size_t kLinearBufferSize = 1048576;
Sungtak Lee56925782020-11-08 00:07:27 -080038// This can fit an 8K frame.
39constexpr size_t kMaxLinearBufferSize = 7680 * 4320 * 2;
Wonsik Kim469c8342019-04-11 16:46:09 -070040
41/**
42 * Base class for representation of buffers at one port.
43 */
44class CCodecBuffers {
45public:
46 CCodecBuffers(const char *componentName, const char *name = "Buffers")
47 : mComponentName(componentName),
48 mChannelName(std::string(componentName) + ":" + name),
49 mName(mChannelName.c_str()) {
50 }
51 virtual ~CCodecBuffers() = default;
52
53 /**
54 * Set format for MediaCodec-facing buffers.
55 */
56 void setFormat(const sp<AMessage> &format);
57
58 /**
59 * Return a copy of current format.
60 */
61 sp<AMessage> dupFormat();
62
63 /**
64 * Returns true if the buffers are operating under array mode.
65 */
66 virtual bool isArrayMode() const { return false; }
67
68 /**
69 * Fills the vector with MediaCodecBuffer's if in array mode; otherwise,
70 * no-op.
71 */
72 virtual void getArray(Vector<sp<MediaCodecBuffer>> *) const {}
73
74 /**
Xiao Huang5ff11fa2022-09-20 12:04:27 +000075 * Return number of buffers owned by the client or the component.
Wonsik Kim469c8342019-04-11 16:46:09 -070076 */
Wonsik Kim0487b782020-10-28 11:45:50 -070077 virtual size_t numActiveSlots() const = 0;
Wonsik Kim469c8342019-04-11 16:46:09 -070078
79 /**
80 * Examine image data from the buffer and update the format if necessary.
81 */
82 void handleImageData(const sp<Codec2Buffer> &buffer);
83
84protected:
85 std::string mComponentName; ///< name of component for debugging
86 std::string mChannelName; ///< name of channel for debugging
87 const char *mName; ///< C-string version of channel name
88 // Format to be used for creating MediaCodec-facing buffers.
89 sp<AMessage> mFormat;
90
Wonsik Kim4a3c0462021-03-09 15:45:05 -080091 sp<ABuffer> mLastImageData;
92 sp<AMessage> mFormatWithImageData;
93
Wonsik Kim469c8342019-04-11 16:46:09 -070094private:
95 DISALLOW_EVIL_CONSTRUCTORS(CCodecBuffers);
96};
97
98class InputBuffers : public CCodecBuffers {
99public:
100 InputBuffers(const char *componentName, const char *name = "Input[]")
101 : CCodecBuffers(componentName, name) { }
102 virtual ~InputBuffers() = default;
103
104 /**
105 * Set a block pool to obtain input memory blocks.
106 */
107 void setPool(const std::shared_ptr<C2BlockPool> &pool) { mPool = pool; }
108
109 /**
110 * Get a new MediaCodecBuffer for input and its corresponding index.
111 * Returns false if no new buffer can be obtained at the moment.
112 */
113 virtual bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) = 0;
114
115 /**
116 * Release the buffer obtained from requestNewBuffer() and get the
117 * associated C2Buffer object back. Returns true if the buffer was on file
118 * and released successfully.
119 */
120 virtual bool releaseBuffer(
121 const sp<MediaCodecBuffer> &buffer,
122 std::shared_ptr<C2Buffer> *c2buffer,
123 bool release) = 0;
124
125 /**
126 * Release the buffer that is no longer used by the codec process. Return
127 * true if and only if the buffer was on file and released successfully.
128 */
129 virtual bool expireComponentBuffer(
130 const std::shared_ptr<C2Buffer> &c2buffer) = 0;
131
132 /**
133 * Flush internal state. After this call, no index or buffer previously
134 * returned from requestNewBuffer() is valid.
135 */
136 virtual void flush() = 0;
137
138 /**
139 * Return array-backed version of input buffers. The returned object
140 * shall retain the internal state so that it will honor index and
141 * buffer from previous calls of requestNewBuffer().
142 */
143 virtual std::unique_ptr<InputBuffers> toArrayMode(size_t size) = 0;
144
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700145 /**
146 * Release the buffer obtained from requestNewBuffer(), and create a deep
147 * copy clone of the buffer.
148 *
149 * \return the deep copy clone of the buffer; nullptr if cloning is not
150 * possible.
151 */
152 sp<Codec2Buffer> cloneAndReleaseBuffer(const sp<MediaCodecBuffer> &buffer);
153
Wonsik Kim469c8342019-04-11 16:46:09 -0700154protected:
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700155 virtual sp<Codec2Buffer> createNewBuffer() = 0;
156
Wonsik Kim469c8342019-04-11 16:46:09 -0700157 // Pool to obtain blocks for input buffers.
158 std::shared_ptr<C2BlockPool> mPool;
159
160private:
161 DISALLOW_EVIL_CONSTRUCTORS(InputBuffers);
162};
163
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700164class OutputBuffersArray;
165
Wonsik Kim469c8342019-04-11 16:46:09 -0700166class OutputBuffers : public CCodecBuffers {
167public:
Wonsik Kim41d83432020-04-27 16:40:49 -0700168 OutputBuffers(const char *componentName, const char *name = "Output");
169 virtual ~OutputBuffers();
Wonsik Kim469c8342019-04-11 16:46:09 -0700170
171 /**
172 * Register output C2Buffer from the component and obtain corresponding
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700173 * index and MediaCodecBuffer object.
174 *
175 * Returns:
176 * OK if registration succeeds.
177 * NO_MEMORY if all buffers are available but not compatible.
178 * WOULD_BLOCK if there are compatible buffers, but they are all in use.
Wonsik Kim469c8342019-04-11 16:46:09 -0700179 */
180 virtual status_t registerBuffer(
181 const std::shared_ptr<C2Buffer> &buffer,
182 size_t *index,
183 sp<MediaCodecBuffer> *clientBuffer) = 0;
184
185 /**
186 * Register codec specific data as a buffer to be consistent with
187 * MediaCodec behavior.
188 */
189 virtual status_t registerCsd(
190 const C2StreamInitDataInfo::output * /* csd */,
191 size_t * /* index */,
192 sp<MediaCodecBuffer> * /* clientBuffer */) = 0;
193
194 /**
195 * Release the buffer obtained from registerBuffer() and get the
196 * associated C2Buffer object back. Returns true if the buffer was on file
197 * and released successfully.
198 */
199 virtual bool releaseBuffer(
200 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) = 0;
201
202 /**
203 * Flush internal state. After this call, no index or buffer previously
204 * returned from registerBuffer() is valid.
205 */
206 virtual void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) = 0;
207
208 /**
209 * Return array-backed version of output buffers. The returned object
210 * shall retain the internal state so that it will honor index and
211 * buffer from previous calls of registerBuffer().
212 */
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700213 virtual std::unique_ptr<OutputBuffersArray> toArrayMode(size_t size) = 0;
Wonsik Kim469c8342019-04-11 16:46:09 -0700214
215 /**
216 * Initialize SkipCutBuffer object.
217 */
218 void initSkipCutBuffer(
219 int32_t delay, int32_t padding, int32_t sampleRate, int32_t channelCount);
220
221 /**
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700222 * Update SkipCutBuffer from format. The @p format must not be null.
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700223 */
Wonsik Kim970bf0b2020-11-10 11:54:15 -0800224 void updateSkipCutBuffer(const sp<AMessage> &format);
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700225
226 /**
227 * Output Stash
228 * ============
229 *
230 * The output stash is a place to hold output buffers temporarily before
231 * they are registered to output slots. It has 2 main functions:
232 * 1. Allow reordering of output frames as the codec may produce frames in a
233 * different order.
234 * 2. Act as a "buffer" between the codec and the client because the codec
235 * may produce more buffers than available slots. This excess of codec's
236 * output buffers should be registered to slots later, after the client
237 * has released some slots.
238 *
239 * The stash consists of 2 lists of buffers: mPending and mReorderStash.
240 * mPending is a normal FIFO queue with not size limit, while mReorderStash
241 * is a sorted list with size limit mDepth.
242 *
243 * The normal flow of a non-csd output buffer is as follows:
244 *
245 * |----------------OutputBuffers---------------|
246 * |----------Output stash----------| |
247 * Codec --|-> mReorderStash --> mPending --|-> slots --|-> client
248 * | | |
249 * pushToStash() popFromStashAndRegister()
250 *
251 * The buffer that comes from the codec first enters mReorderStash. The
252 * first buffer in mReorderStash gets moved to mPending when mReorderStash
253 * overflows. Buffers in mPending are registered to slots and given to the
254 * client as soon as slots are available.
255 *
256 * Every output buffer that is not a csd buffer should be put on the stash
257 * by calling pushToStash(), then later registered to a slot by calling
258 * popFromStashAndRegister() before notifying the client with
259 * onOutputBufferAvailable().
260 *
261 * Reordering
262 * ==========
263 *
264 * mReorderStash is a sorted list with a specified size limit. The size
265 * limit can be set by calling setReorderDepth().
266 *
267 * Every buffer in mReorderStash has a C2WorkOrdinalStruct, which contains 3
268 * members, all of which are comparable. Which member of C2WorkOrdinalStruct
269 * should be used for reordering can be chosen by calling setReorderKey().
270 */
271
272 /**
273 * Return the reorder depth---the size of mReorderStash.
274 */
275 uint32_t getReorderDepth() const;
276
277 /**
278 * Set the reorder depth.
279 */
280 void setReorderDepth(uint32_t depth);
281
282 /**
283 * Set the type of "key" to use in comparisons.
284 */
285 void setReorderKey(C2Config::ordinal_key_t key);
286
287 /**
288 * Return whether the output stash has any pending buffers.
289 */
290 bool hasPending() const;
291
292 /**
293 * Flush the stash and reset the depth and the key to their default values.
294 */
295 void clearStash();
296
297 /**
298 * Flush the stash.
299 */
300 void flushStash();
301
302 /**
303 * Push a buffer to the reorder stash.
304 *
305 * @param buffer C2Buffer object from the returned work.
306 * @param notify Whether the returned work contains a buffer that should
307 * be reported to the client. This may be false if the
308 * caller wants to process the buffer without notifying the
309 * client.
310 * @param timestamp Buffer timestamp to report to the client.
311 * @param flags Buffer flags to report to the client.
312 * @param format Buffer format to report to the client.
313 * @param ordinal Ordinal used in reordering. This determines when the
314 * buffer will be popped from the output stash by
315 * `popFromStashAndRegister()`.
316 */
317 void pushToStash(
318 const std::shared_ptr<C2Buffer>& buffer,
319 bool notify,
320 int64_t timestamp,
321 int32_t flags,
322 const sp<AMessage>& format,
323 const C2WorkOrdinalStruct& ordinal);
324
325 enum BufferAction : int {
326 SKIP,
327 DISCARD,
328 NOTIFY_CLIENT,
329 REALLOCATE,
330 RETRY,
331 };
332
333 /**
334 * Try to atomically pop the first buffer from the reorder stash and
335 * register it to an output slot. The function returns a value that
336 * indicates a recommended course of action for the caller.
337 *
338 * If the stash is empty, the function will return `SKIP`.
339 *
340 * If the stash is not empty, the function will peek at the first (oldest)
341 * entry in mPending process the buffer in the entry as follows:
342 * - If the buffer should not be sent to the client, the function will
343 * return `DISCARD`. The stash entry will be removed.
344 * - If the buffer should be sent to the client, the function will attempt
345 * to register the buffer to a slot. The registration may have 3 outcomes
346 * corresponding to the following return values:
347 * - `NOTIFY_CLIENT`: The buffer is successfully registered to a slot. The
348 * output arguments @p index and @p outBuffer will contain valid values
349 * that the caller can use to call onOutputBufferAvailable(). The stash
350 * entry will be removed.
351 * - `REALLOCATE`: The buffer is not registered because it is not
352 * compatible with the current slots (which are available). The caller
353 * should reallocate the OutputBuffers with slots that can fit the
354 * returned @p c2Buffer. The stash entry will not be removed
355 * - `RETRY`: All slots are currently occupied by the client. The caller
356 * should try to call this function again after the client has released
357 * some slots.
358 *
359 * @return What the caller should do afterwards.
360 *
361 * @param[out] c2Buffer Underlying C2Buffer associated to the first buffer
362 * on the stash. This value is guaranteed to be valid
363 * unless the return value is `SKIP`.
364 * @param[out] index Slot index. This value is valid only if the return
365 * value is `NOTIFY_CLIENT`.
366 * @param[out] outBuffer Registered buffer. This value is valid only if the
367 * return valu is `NOTIFY_CLIENT`.
368 */
369 BufferAction popFromStashAndRegister(
370 std::shared_ptr<C2Buffer>* c2Buffer,
371 size_t* index,
372 sp<MediaCodecBuffer>* outBuffer);
373
374protected:
375 sp<SkipCutBuffer> mSkipCutBuffer;
376
377 /**
Wonsik Kim469c8342019-04-11 16:46:09 -0700378 * Update the SkipCutBuffer object. No-op if it's never initialized.
379 */
380 void updateSkipCutBuffer(int32_t sampleRate, int32_t channelCount);
381
382 /**
383 * Submit buffer to SkipCutBuffer object, if initialized.
384 */
385 void submit(const sp<MediaCodecBuffer> &buffer);
386
Wonsik Kim6f23cfc2021-09-24 05:45:52 -0700387 /**
Greg Kaiser92dc4542021-10-08 06:58:19 -0700388 * Apply DataConverter from |src| to |*dst| if needed. If |*dst| is nullptr,
Wonsik Kim6f23cfc2021-09-24 05:45:52 -0700389 * a new buffer is allocated.
390 *
391 * Returns true if conversion was needed and executed; false otherwise.
392 */
393 bool convert(const std::shared_ptr<C2Buffer> &src, sp<Codec2Buffer> *dst);
394
Wonsik Kim469c8342019-04-11 16:46:09 -0700395private:
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700396 // SkipCutBuffer
Wonsik Kim469c8342019-04-11 16:46:09 -0700397 int32_t mDelay;
398 int32_t mPadding;
399 int32_t mSampleRate;
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700400 int32_t mChannelCount;
Wonsik Kim469c8342019-04-11 16:46:09 -0700401
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700402 void setSkipCutBuffer(int32_t skip, int32_t cut);
Wonsik Kim469c8342019-04-11 16:46:09 -0700403
Wonsik Kim6f23cfc2021-09-24 05:45:52 -0700404 // DataConverter
405 sp<DataConverter> mDataConverter;
406 sp<AMessage> mFormatWithConverter;
407 std::optional<int32_t> mSrcEncoding;
408 std::optional<int32_t> mDstEncoding;
409
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700410 // Output stash
411
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700412 // Struct for an entry in the output stash (mPending and mReorderStash)
413 struct StashEntry {
414 inline StashEntry()
415 : buffer(nullptr),
416 notify(false),
417 timestamp(0),
418 flags(0),
419 format(),
420 ordinal({0, 0, 0}) {}
421 inline StashEntry(
422 const std::shared_ptr<C2Buffer> &b,
423 bool n,
424 int64_t t,
425 int32_t f,
426 const sp<AMessage> &fmt,
427 const C2WorkOrdinalStruct &o)
428 : buffer(b),
429 notify(n),
430 timestamp(t),
431 flags(f),
432 format(fmt),
433 ordinal(o) {}
434 std::shared_ptr<C2Buffer> buffer;
435 bool notify;
436 int64_t timestamp;
437 int32_t flags;
438 sp<AMessage> format;
439 C2WorkOrdinalStruct ordinal;
440 };
441
442 /**
443 * FIFO queue of stash entries.
444 */
445 std::list<StashEntry> mPending;
446 /**
447 * Sorted list of stash entries.
448 */
449 std::list<StashEntry> mReorderStash;
450 /**
451 * Size limit of mReorderStash.
452 */
453 uint32_t mDepth{0};
454 /**
455 * Choice of key to use in ordering of stash entries in mReorderStash.
456 */
457 C2Config::ordinal_key_t mKey{C2Config::ORDINAL};
458
459 /**
460 * Return false if mPending is empty; otherwise, pop the first entry from
461 * mPending and return true.
462 */
463 bool popPending(StashEntry *entry);
464
465 /**
466 * Push an entry as the first entry of mPending.
467 */
468 void deferPending(const StashEntry &entry);
469
470 /**
471 * Comparison of C2WorkOrdinalStruct based on mKey.
472 */
473 bool less(const C2WorkOrdinalStruct &o1,
474 const C2WorkOrdinalStruct &o2) const;
475
Wonsik Kim469c8342019-04-11 16:46:09 -0700476 DISALLOW_EVIL_CONSTRUCTORS(OutputBuffers);
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700477
478 friend OutputBuffersArray;
Wonsik Kim469c8342019-04-11 16:46:09 -0700479};
480
481/**
482 * Simple local buffer pool backed by std::vector.
483 */
484class LocalBufferPool : public std::enable_shared_from_this<LocalBufferPool> {
485public:
486 /**
487 * Create a new LocalBufferPool object.
488 *
Wonsik Kim469c8342019-04-11 16:46:09 -0700489 * \return a newly created pool object.
490 */
Wonsik Kim41d83432020-04-27 16:40:49 -0700491 static std::shared_ptr<LocalBufferPool> Create();
Wonsik Kim469c8342019-04-11 16:46:09 -0700492
493 /**
494 * Return an ABuffer object whose size is at least |capacity|.
495 *
496 * \param capacity requested capacity
497 * \return nullptr if the pool capacity is reached
498 * an ABuffer object otherwise.
499 */
500 sp<ABuffer> newBuffer(size_t capacity);
501
502private:
503 /**
504 * ABuffer backed by std::vector.
505 */
506 class VectorBuffer : public ::android::ABuffer {
507 public:
508 /**
509 * Construct a VectorBuffer by taking the ownership of supplied vector.
510 *
511 * \param vec backing vector of the buffer. this object takes
512 * ownership at construction.
513 * \param pool a LocalBufferPool object to return the vector at
514 * destruction.
515 */
516 VectorBuffer(std::vector<uint8_t> &&vec, const std::shared_ptr<LocalBufferPool> &pool);
517
518 ~VectorBuffer() override;
519
520 private:
521 std::vector<uint8_t> mVec;
522 std::weak_ptr<LocalBufferPool> mPool;
523 };
524
525 Mutex mMutex;
526 size_t mPoolCapacity;
527 size_t mUsedSize;
528 std::list<std::vector<uint8_t>> mPool;
529
530 /**
531 * Private constructor to prevent constructing non-managed LocalBufferPool.
532 */
533 explicit LocalBufferPool(size_t poolCapacity)
534 : mPoolCapacity(poolCapacity), mUsedSize(0) {
535 }
536
537 /**
538 * Take back the ownership of vec from the destructed VectorBuffer and put
539 * it in front of the pool.
540 */
541 void returnVector(std::vector<uint8_t> &&vec);
542
543 DISALLOW_EVIL_CONSTRUCTORS(LocalBufferPool);
544};
545
546class BuffersArrayImpl;
547
548/**
549 * Flexible buffer slots implementation.
550 */
551class FlexBuffersImpl {
552public:
553 FlexBuffersImpl(const char *name)
554 : mImplName(std::string(name) + ".Impl"),
555 mName(mImplName.c_str()) { }
556
557 /**
558 * Assign an empty slot for a buffer and return the index. If there's no
559 * empty slot, just add one at the end and return it.
560 *
561 * \param buffer[in] a new buffer to assign a slot.
562 * \return index of the assigned slot.
563 */
564 size_t assignSlot(const sp<Codec2Buffer> &buffer);
565
566 /**
567 * Release the slot from the client, and get the C2Buffer object back from
568 * the previously assigned buffer. Note that the slot is not completely free
569 * until the returned C2Buffer object is freed.
570 *
571 * \param buffer[in] the buffer previously assigned a slot.
572 * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored
573 * if null.
574 * \return true if the buffer is successfully released from a slot
575 * false otherwise
576 */
577 bool releaseSlot(
578 const sp<MediaCodecBuffer> &buffer,
579 std::shared_ptr<C2Buffer> *c2buffer,
580 bool release);
581
582 /**
583 * Expire the C2Buffer object in the slot.
584 *
585 * \param c2buffer[in] C2Buffer object which the component released.
586 * \return true if the buffer is found in one of the slots and
587 * successfully released
588 * false otherwise
589 */
590 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer);
591
592 /**
593 * The client abandoned all known buffers, so reclaim the ownership.
594 */
595 void flush();
596
597 /**
Xiao Huang5ff11fa2022-09-20 12:04:27 +0000598 * Return the number of buffers that are sent to the client or the component.
Wonsik Kim469c8342019-04-11 16:46:09 -0700599 */
Wonsik Kim0487b782020-10-28 11:45:50 -0700600 size_t numActiveSlots() const;
Wonsik Kim469c8342019-04-11 16:46:09 -0700601
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700602 /**
603 * Return the number of buffers that are sent to the component but not
604 * returned back yet.
605 */
606 size_t numComponentBuffers() const;
607
Wonsik Kim469c8342019-04-11 16:46:09 -0700608private:
609 friend class BuffersArrayImpl;
610
611 std::string mImplName; ///< name for debugging
612 const char *mName; ///< C-string version of name
613
614 struct Entry {
615 sp<Codec2Buffer> clientBuffer;
616 std::weak_ptr<C2Buffer> compBuffer;
617 };
618 std::vector<Entry> mBuffers;
619};
620
621/**
622 * Static buffer slots implementation based on a fixed-size array.
623 */
624class BuffersArrayImpl {
625public:
626 BuffersArrayImpl()
627 : mImplName("BuffersArrayImpl"),
628 mName(mImplName.c_str()) { }
629
630 /**
631 * Initialize buffer array from the original |impl|. The buffers known by
632 * the client is preserved, and the empty slots are populated so that the
633 * array size is at least |minSize|.
634 *
635 * \param impl[in] FlexBuffersImpl object used so far.
636 * \param minSize[in] minimum size of the buffer array.
637 * \param allocate[in] function to allocate a client buffer for an empty slot.
638 */
639 void initialize(
640 const FlexBuffersImpl &impl,
641 size_t minSize,
642 std::function<sp<Codec2Buffer>()> allocate);
643
644 /**
645 * Grab a buffer from the underlying array which matches the criteria.
646 *
647 * \param index[out] index of the slot.
648 * \param buffer[out] the matching buffer.
649 * \param match[in] a function to test whether the buffer matches the
650 * criteria or not.
651 * \return OK if successful,
652 * WOULD_BLOCK if slots are being used,
653 * NO_MEMORY if no slot matches the criteria, even though it's
654 * available
655 */
656 status_t grabBuffer(
657 size_t *index,
658 sp<Codec2Buffer> *buffer,
659 std::function<bool(const sp<Codec2Buffer> &)> match =
Wonsik Kim936a89c2020-05-08 16:07:50 -0700660 [](const sp<Codec2Buffer> &buffer) { return (buffer != nullptr); });
Wonsik Kim469c8342019-04-11 16:46:09 -0700661
662 /**
663 * Return the buffer from the client, and get the C2Buffer object back from
664 * the buffer. Note that the slot is not completely free until the returned
665 * C2Buffer object is freed.
666 *
667 * \param buffer[in] the buffer previously grabbed.
668 * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored
669 * if null.
670 * \return true if the buffer is successfully returned
671 * false otherwise
672 */
673 bool returnBuffer(
674 const sp<MediaCodecBuffer> &buffer,
675 std::shared_ptr<C2Buffer> *c2buffer,
676 bool release);
677
678 /**
679 * Expire the C2Buffer object in the slot.
680 *
681 * \param c2buffer[in] C2Buffer object which the component released.
682 * \return true if the buffer is found in one of the slots and
683 * successfully released
684 * false otherwise
685 */
686 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer);
687
688 /**
689 * Populate |array| with the underlying buffer array.
690 *
691 * \param array[out] an array to be filled with the underlying buffer array.
692 */
693 void getArray(Vector<sp<MediaCodecBuffer>> *array) const;
694
695 /**
696 * The client abandoned all known buffers, so reclaim the ownership.
697 */
698 void flush();
699
700 /**
701 * Reallocate the array with the given allocation function.
702 *
703 * \param alloc[in] the allocation function for client buffers.
704 */
705 void realloc(std::function<sp<Codec2Buffer>()> alloc);
706
707 /**
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700708 * Grow the array to the new size. It is a programming error to supply
709 * smaller size as the new size.
710 *
711 * \param newSize[in] new size of the array.
712 * \param alloc[in] the alllocation function for client buffers to fill
713 * the new empty slots.
714 */
715 void grow(size_t newSize, std::function<sp<Codec2Buffer>()> alloc);
716
717 /**
Xiao Huang5ff11fa2022-09-20 12:04:27 +0000718 * Return the number of buffers that are sent to the client or the component.
Wonsik Kim469c8342019-04-11 16:46:09 -0700719 */
Wonsik Kim0487b782020-10-28 11:45:50 -0700720 size_t numActiveSlots() const;
Wonsik Kim469c8342019-04-11 16:46:09 -0700721
Wonsik Kima39882b2019-06-20 16:13:56 -0700722 /**
723 * Return the size of the array.
724 */
725 size_t arraySize() const;
726
Wonsik Kim469c8342019-04-11 16:46:09 -0700727private:
728 std::string mImplName; ///< name for debugging
729 const char *mName; ///< C-string version of name
730
731 struct Entry {
732 const sp<Codec2Buffer> clientBuffer;
733 std::weak_ptr<C2Buffer> compBuffer;
734 bool ownedByClient;
735 };
736 std::vector<Entry> mBuffers;
737};
738
739class InputBuffersArray : public InputBuffers {
740public:
741 InputBuffersArray(const char *componentName, const char *name = "Input[N]")
742 : InputBuffers(componentName, name) { }
743 ~InputBuffersArray() override = default;
744
745 /**
746 * Initialize this object from the non-array state. We keep existing slots
747 * at the same index, and for empty slots we allocate client buffers with
748 * the given allocate function. If the number of slots is less than minSize,
749 * we fill the array to the minimum size.
750 *
751 * \param impl[in] existing non-array state
752 * \param minSize[in] minimum size of the array
753 * \param allocate[in] allocate function to fill empty slots
754 */
755 void initialize(
756 const FlexBuffersImpl &impl,
757 size_t minSize,
758 std::function<sp<Codec2Buffer>()> allocate);
759
760 bool isArrayMode() const final { return true; }
761
762 std::unique_ptr<InputBuffers> toArrayMode(size_t) final {
763 return nullptr;
764 }
765
766 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final;
767
768 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override;
769
770 bool releaseBuffer(
771 const sp<MediaCodecBuffer> &buffer,
772 std::shared_ptr<C2Buffer> *c2buffer,
773 bool release) override;
774
775 bool expireComponentBuffer(
776 const std::shared_ptr<C2Buffer> &c2buffer) override;
777
778 void flush() override;
779
Wonsik Kim0487b782020-10-28 11:45:50 -0700780 size_t numActiveSlots() const final;
Wonsik Kim469c8342019-04-11 16:46:09 -0700781
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700782protected:
783 sp<Codec2Buffer> createNewBuffer() override;
784
Wonsik Kim469c8342019-04-11 16:46:09 -0700785private:
786 BuffersArrayImpl mImpl;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700787 std::function<sp<Codec2Buffer>()> mAllocate;
Wonsik Kim469c8342019-04-11 16:46:09 -0700788};
789
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800790class SlotInputBuffers : public InputBuffers {
791public:
792 SlotInputBuffers(const char *componentName, const char *name = "Slot-Input")
793 : InputBuffers(componentName, name),
794 mImpl(mName) { }
795 ~SlotInputBuffers() override = default;
796
797 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) final;
798
799 bool releaseBuffer(
800 const sp<MediaCodecBuffer> &buffer,
801 std::shared_ptr<C2Buffer> *c2buffer,
802 bool release) final;
803
804 bool expireComponentBuffer(
805 const std::shared_ptr<C2Buffer> &c2buffer) final;
806
807 void flush() final;
808
809 std::unique_ptr<InputBuffers> toArrayMode(size_t size) final;
810
Wonsik Kim0487b782020-10-28 11:45:50 -0700811 size_t numActiveSlots() const final;
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800812
813protected:
814 sp<Codec2Buffer> createNewBuffer() final;
815
816private:
817 FlexBuffersImpl mImpl;
818};
819
Wonsik Kim469c8342019-04-11 16:46:09 -0700820class LinearInputBuffers : public InputBuffers {
821public:
822 LinearInputBuffers(const char *componentName, const char *name = "1D-Input")
823 : InputBuffers(componentName, name),
824 mImpl(mName) { }
825 ~LinearInputBuffers() override = default;
826
827 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override;
828
829 bool releaseBuffer(
830 const sp<MediaCodecBuffer> &buffer,
831 std::shared_ptr<C2Buffer> *c2buffer,
832 bool release) override;
833
834 bool expireComponentBuffer(
835 const std::shared_ptr<C2Buffer> &c2buffer) override;
836
837 void flush() override;
838
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700839 std::unique_ptr<InputBuffers> toArrayMode(size_t size) override;
Wonsik Kim469c8342019-04-11 16:46:09 -0700840
Wonsik Kim0487b782020-10-28 11:45:50 -0700841 size_t numActiveSlots() const final;
Wonsik Kim469c8342019-04-11 16:46:09 -0700842
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700843protected:
844 sp<Codec2Buffer> createNewBuffer() override;
845
846 FlexBuffersImpl mImpl;
Wonsik Kim469c8342019-04-11 16:46:09 -0700847
848private:
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700849 static sp<Codec2Buffer> Alloc(
850 const std::shared_ptr<C2BlockPool> &pool, const sp<AMessage> &format);
Wonsik Kim469c8342019-04-11 16:46:09 -0700851};
852
853class EncryptedLinearInputBuffers : public LinearInputBuffers {
854public:
855 EncryptedLinearInputBuffers(
856 bool secure,
857 const sp<MemoryDealer> &dealer,
858 const sp<ICrypto> &crypto,
859 int32_t heapSeqNum,
860 size_t capacity,
861 size_t numInputSlots,
862 const char *componentName, const char *name = "EncryptedInput");
863
864 ~EncryptedLinearInputBuffers() override = default;
865
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700866 std::unique_ptr<InputBuffers> toArrayMode(size_t size) override;
867
868protected:
869 sp<Codec2Buffer> createNewBuffer() override;
Wonsik Kim469c8342019-04-11 16:46:09 -0700870
871private:
Wonsik Kim469c8342019-04-11 16:46:09 -0700872 struct Entry {
873 std::weak_ptr<C2LinearBlock> block;
874 sp<IMemory> memory;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700875 int32_t heapSeqNum;
Wonsik Kim469c8342019-04-11 16:46:09 -0700876 };
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700877
878 static sp<Codec2Buffer> Alloc(
879 const std::shared_ptr<C2BlockPool> &pool,
880 const sp<AMessage> &format,
881 C2MemoryUsage usage,
882 const std::shared_ptr<std::vector<Entry>> &memoryVector);
883
884 C2MemoryUsage mUsage;
885 sp<MemoryDealer> mDealer;
886 sp<ICrypto> mCrypto;
887 std::shared_ptr<std::vector<Entry>> mMemoryVector;
Wonsik Kim469c8342019-04-11 16:46:09 -0700888};
889
890class GraphicMetadataInputBuffers : public InputBuffers {
891public:
892 GraphicMetadataInputBuffers(const char *componentName, const char *name = "2D-MetaInput");
893 ~GraphicMetadataInputBuffers() override = default;
894
895 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override;
896
897 bool releaseBuffer(
898 const sp<MediaCodecBuffer> &buffer,
899 std::shared_ptr<C2Buffer> *c2buffer,
900 bool release) override;
901
902 bool expireComponentBuffer(
903 const std::shared_ptr<C2Buffer> &c2buffer) override;
904
905 void flush() override;
906
907 std::unique_ptr<InputBuffers> toArrayMode(size_t size) final;
908
Wonsik Kim0487b782020-10-28 11:45:50 -0700909 size_t numActiveSlots() const final;
Wonsik Kim469c8342019-04-11 16:46:09 -0700910
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700911protected:
912 sp<Codec2Buffer> createNewBuffer() override;
913
Wonsik Kim469c8342019-04-11 16:46:09 -0700914private:
915 FlexBuffersImpl mImpl;
916 std::shared_ptr<C2AllocatorStore> mStore;
917};
918
919class GraphicInputBuffers : public InputBuffers {
920public:
Wonsik Kim41d83432020-04-27 16:40:49 -0700921 GraphicInputBuffers(const char *componentName, const char *name = "2D-BB-Input");
Wonsik Kim469c8342019-04-11 16:46:09 -0700922 ~GraphicInputBuffers() override = default;
923
924 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override;
925
926 bool releaseBuffer(
927 const sp<MediaCodecBuffer> &buffer,
928 std::shared_ptr<C2Buffer> *c2buffer,
929 bool release) override;
930
931 bool expireComponentBuffer(
932 const std::shared_ptr<C2Buffer> &c2buffer) override;
933
934 void flush() override;
935
936 std::unique_ptr<InputBuffers> toArrayMode(
937 size_t size) final;
938
Wonsik Kim0487b782020-10-28 11:45:50 -0700939 size_t numActiveSlots() const final;
Wonsik Kim469c8342019-04-11 16:46:09 -0700940
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700941protected:
942 sp<Codec2Buffer> createNewBuffer() override;
943
Wonsik Kim469c8342019-04-11 16:46:09 -0700944private:
945 FlexBuffersImpl mImpl;
946 std::shared_ptr<LocalBufferPool> mLocalBufferPool;
947};
948
949class DummyInputBuffers : public InputBuffers {
950public:
951 DummyInputBuffers(const char *componentName, const char *name = "2D-Input")
952 : InputBuffers(componentName, name) { }
953 ~DummyInputBuffers() override = default;
954
955 bool requestNewBuffer(size_t *, sp<MediaCodecBuffer> *) override {
956 return false;
957 }
958
959 bool releaseBuffer(
960 const sp<MediaCodecBuffer> &, std::shared_ptr<C2Buffer> *, bool) override {
961 return false;
962 }
963
964 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &) override {
965 return false;
966 }
967 void flush() override {
968 }
969
970 std::unique_ptr<InputBuffers> toArrayMode(size_t) final {
971 return nullptr;
972 }
973
974 bool isArrayMode() const final { return true; }
975
976 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final {
977 array->clear();
978 }
979
Wonsik Kim0487b782020-10-28 11:45:50 -0700980 size_t numActiveSlots() const final {
Wonsik Kim469c8342019-04-11 16:46:09 -0700981 return 0u;
982 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700983
984protected:
985 sp<Codec2Buffer> createNewBuffer() override {
986 return nullptr;
987 }
Wonsik Kim469c8342019-04-11 16:46:09 -0700988};
989
990class OutputBuffersArray : public OutputBuffers {
991public:
992 OutputBuffersArray(const char *componentName, const char *name = "Output[N]")
993 : OutputBuffers(componentName, name) { }
994 ~OutputBuffersArray() override = default;
995
996 /**
997 * Initialize this object from the non-array state. We keep existing slots
998 * at the same index, and for empty slots we allocate client buffers with
999 * the given allocate function. If the number of slots is less than minSize,
1000 * we fill the array to the minimum size.
1001 *
1002 * \param impl[in] existing non-array state
1003 * \param minSize[in] minimum size of the array
1004 * \param allocate[in] allocate function to fill empty slots
1005 */
1006 void initialize(
1007 const FlexBuffersImpl &impl,
1008 size_t minSize,
1009 std::function<sp<Codec2Buffer>()> allocate);
1010
1011 bool isArrayMode() const final { return true; }
1012
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001013 std::unique_ptr<OutputBuffersArray> toArrayMode(size_t) final {
Wonsik Kim469c8342019-04-11 16:46:09 -07001014 return nullptr;
1015 }
1016
1017 status_t registerBuffer(
1018 const std::shared_ptr<C2Buffer> &buffer,
1019 size_t *index,
1020 sp<MediaCodecBuffer> *clientBuffer) final;
1021
1022 status_t registerCsd(
1023 const C2StreamInitDataInfo::output *csd,
1024 size_t *index,
1025 sp<MediaCodecBuffer> *clientBuffer) final;
1026
1027 bool releaseBuffer(
1028 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) override;
1029
1030 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) override;
1031
1032 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final;
1033
Wonsik Kim0487b782020-10-28 11:45:50 -07001034 size_t numActiveSlots() const final;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001035
Wonsik Kim469c8342019-04-11 16:46:09 -07001036 /**
1037 * Reallocate the array, filled with buffers with the same size as given
1038 * buffer.
1039 *
1040 * \param c2buffer[in] the reference buffer
1041 */
1042 void realloc(const std::shared_ptr<C2Buffer> &c2buffer);
1043
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001044 /**
1045 * Grow the array to the new size. It is a programming error to supply
1046 * smaller size as the new size.
1047 *
1048 * \param newSize[in] new size of the array.
1049 */
1050 void grow(size_t newSize);
Wonsik Kim469c8342019-04-11 16:46:09 -07001051
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001052 /**
1053 * Transfer the SkipCutBuffer and the output stash from another
1054 * OutputBuffers.
1055 */
1056 void transferFrom(OutputBuffers* source);
1057
Wonsik Kim469c8342019-04-11 16:46:09 -07001058private:
1059 BuffersArrayImpl mImpl;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001060 std::function<sp<Codec2Buffer>()> mAlloc;
Wonsik Kim469c8342019-04-11 16:46:09 -07001061};
1062
1063class FlexOutputBuffers : public OutputBuffers {
1064public:
1065 FlexOutputBuffers(const char *componentName, const char *name = "Output[]")
1066 : OutputBuffers(componentName, name),
1067 mImpl(mName) { }
1068
1069 status_t registerBuffer(
1070 const std::shared_ptr<C2Buffer> &buffer,
1071 size_t *index,
1072 sp<MediaCodecBuffer> *clientBuffer) override;
1073
1074 status_t registerCsd(
1075 const C2StreamInitDataInfo::output *csd,
1076 size_t *index,
1077 sp<MediaCodecBuffer> *clientBuffer) final;
1078
1079 bool releaseBuffer(
1080 const sp<MediaCodecBuffer> &buffer,
1081 std::shared_ptr<C2Buffer> *c2buffer) override;
1082
1083 void flush(
1084 const std::list<std::unique_ptr<C2Work>> &flushedWork) override;
1085
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001086 std::unique_ptr<OutputBuffersArray> toArrayMode(size_t size) override;
Wonsik Kim469c8342019-04-11 16:46:09 -07001087
Wonsik Kim0487b782020-10-28 11:45:50 -07001088 size_t numActiveSlots() const final;
Wonsik Kim469c8342019-04-11 16:46:09 -07001089
1090 /**
1091 * Return an appropriate Codec2Buffer object for the type of buffers.
1092 *
1093 * \param buffer C2Buffer object to wrap.
1094 *
1095 * \return appropriate Codec2Buffer object to wrap |buffer|.
1096 */
1097 virtual sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) = 0;
1098
1099 /**
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001100 * Return a function that allocates an appropriate Codec2Buffer object for
1101 * the type of buffers, to be used as an empty array buffer. The function
1102 * must not refer to this pointer, since it may be used after this object
1103 * destructs.
Wonsik Kim469c8342019-04-11 16:46:09 -07001104 *
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001105 * \return a function that allocates appropriate Codec2Buffer object,
1106 * which can copy() from C2Buffers.
Wonsik Kim469c8342019-04-11 16:46:09 -07001107 */
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001108 virtual std::function<sp<Codec2Buffer>()> getAlloc() = 0;
Wonsik Kim469c8342019-04-11 16:46:09 -07001109
1110private:
1111 FlexBuffersImpl mImpl;
1112};
1113
1114class LinearOutputBuffers : public FlexOutputBuffers {
1115public:
1116 LinearOutputBuffers(const char *componentName, const char *name = "1D-Output")
1117 : FlexOutputBuffers(componentName, name) { }
1118
1119 void flush(
1120 const std::list<std::unique_ptr<C2Work>> &flushedWork) override;
1121
1122 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override;
1123
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001124 std::function<sp<Codec2Buffer>()> getAlloc() override;
Wonsik Kim469c8342019-04-11 16:46:09 -07001125};
1126
1127class GraphicOutputBuffers : public FlexOutputBuffers {
1128public:
1129 GraphicOutputBuffers(const char *componentName, const char *name = "2D-Output")
1130 : FlexOutputBuffers(componentName, name) { }
1131
1132 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override;
1133
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001134 std::function<sp<Codec2Buffer>()> getAlloc() override;
Wonsik Kim469c8342019-04-11 16:46:09 -07001135};
1136
1137class RawGraphicOutputBuffers : public FlexOutputBuffers {
1138public:
Wonsik Kim41d83432020-04-27 16:40:49 -07001139 RawGraphicOutputBuffers(const char *componentName, const char *name = "2D-BB-Output");
Wonsik Kim469c8342019-04-11 16:46:09 -07001140 ~RawGraphicOutputBuffers() override = default;
1141
1142 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override;
1143
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001144 std::function<sp<Codec2Buffer>()> getAlloc() override;
Wonsik Kim469c8342019-04-11 16:46:09 -07001145
1146private:
1147 std::shared_ptr<LocalBufferPool> mLocalBufferPool;
1148};
1149
1150} // namespace android
1151
1152#endif // CCODEC_BUFFERS_H_