blob: 995d3a402683573ee02d79120c683f6b787fd148 [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
21#include <string>
22
23#include <C2Config.h>
24#include <media/stagefright/foundation/AMessage.h>
25#include <media/MediaCodecBuffer.h>
26
27#include "Codec2Buffer.h"
Wonsik Kim469c8342019-04-11 16:46:09 -070028
29namespace android {
30
Wonsik Kim41d83432020-04-27 16:40:49 -070031struct ICrypto;
32class MemoryDealer;
Wonsik Kim155d5cb2019-10-09 12:49:49 -070033class SkipCutBuffer;
34
Wonsik Kim469c8342019-04-11 16:46:09 -070035constexpr size_t kLinearBufferSize = 1048576;
Sungtak Lee56925782020-11-08 00:07:27 -080036// This can fit an 8K frame.
37constexpr size_t kMaxLinearBufferSize = 7680 * 4320 * 2;
Wonsik Kim469c8342019-04-11 16:46:09 -070038
39/**
40 * Base class for representation of buffers at one port.
41 */
42class CCodecBuffers {
43public:
44 CCodecBuffers(const char *componentName, const char *name = "Buffers")
45 : mComponentName(componentName),
46 mChannelName(std::string(componentName) + ":" + name),
47 mName(mChannelName.c_str()) {
48 }
49 virtual ~CCodecBuffers() = default;
50
51 /**
52 * Set format for MediaCodec-facing buffers.
53 */
54 void setFormat(const sp<AMessage> &format);
55
56 /**
57 * Return a copy of current format.
58 */
59 sp<AMessage> dupFormat();
60
61 /**
62 * Returns true if the buffers are operating under array mode.
63 */
64 virtual bool isArrayMode() const { return false; }
65
66 /**
67 * Fills the vector with MediaCodecBuffer's if in array mode; otherwise,
68 * no-op.
69 */
70 virtual void getArray(Vector<sp<MediaCodecBuffer>> *) const {}
71
72 /**
73 * Return number of buffers the client owns.
74 */
Wonsik Kim0487b782020-10-28 11:45:50 -070075 virtual size_t numActiveSlots() const = 0;
Wonsik Kim469c8342019-04-11 16:46:09 -070076
77 /**
78 * Examine image data from the buffer and update the format if necessary.
79 */
80 void handleImageData(const sp<Codec2Buffer> &buffer);
81
82protected:
83 std::string mComponentName; ///< name of component for debugging
84 std::string mChannelName; ///< name of channel for debugging
85 const char *mName; ///< C-string version of channel name
86 // Format to be used for creating MediaCodec-facing buffers.
87 sp<AMessage> mFormat;
88
Wonsik Kim4a3c0462021-03-09 15:45:05 -080089 sp<ABuffer> mLastImageData;
90 sp<AMessage> mFormatWithImageData;
91
Wonsik Kim469c8342019-04-11 16:46:09 -070092private:
93 DISALLOW_EVIL_CONSTRUCTORS(CCodecBuffers);
94};
95
96class InputBuffers : public CCodecBuffers {
97public:
98 InputBuffers(const char *componentName, const char *name = "Input[]")
99 : CCodecBuffers(componentName, name) { }
100 virtual ~InputBuffers() = default;
101
102 /**
103 * Set a block pool to obtain input memory blocks.
104 */
105 void setPool(const std::shared_ptr<C2BlockPool> &pool) { mPool = pool; }
106
107 /**
108 * Get a new MediaCodecBuffer for input and its corresponding index.
109 * Returns false if no new buffer can be obtained at the moment.
110 */
111 virtual bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) = 0;
112
113 /**
114 * Release the buffer obtained from requestNewBuffer() and get the
115 * associated C2Buffer object back. Returns true if the buffer was on file
116 * and released successfully.
117 */
118 virtual bool releaseBuffer(
119 const sp<MediaCodecBuffer> &buffer,
120 std::shared_ptr<C2Buffer> *c2buffer,
121 bool release) = 0;
122
123 /**
124 * Release the buffer that is no longer used by the codec process. Return
125 * true if and only if the buffer was on file and released successfully.
126 */
127 virtual bool expireComponentBuffer(
128 const std::shared_ptr<C2Buffer> &c2buffer) = 0;
129
130 /**
131 * Flush internal state. After this call, no index or buffer previously
132 * returned from requestNewBuffer() is valid.
133 */
134 virtual void flush() = 0;
135
136 /**
137 * Return array-backed version of input buffers. The returned object
138 * shall retain the internal state so that it will honor index and
139 * buffer from previous calls of requestNewBuffer().
140 */
141 virtual std::unique_ptr<InputBuffers> toArrayMode(size_t size) = 0;
142
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700143 /**
144 * Release the buffer obtained from requestNewBuffer(), and create a deep
145 * copy clone of the buffer.
146 *
147 * \return the deep copy clone of the buffer; nullptr if cloning is not
148 * possible.
149 */
150 sp<Codec2Buffer> cloneAndReleaseBuffer(const sp<MediaCodecBuffer> &buffer);
151
Wonsik Kim469c8342019-04-11 16:46:09 -0700152protected:
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700153 virtual sp<Codec2Buffer> createNewBuffer() = 0;
154
Wonsik Kim469c8342019-04-11 16:46:09 -0700155 // Pool to obtain blocks for input buffers.
156 std::shared_ptr<C2BlockPool> mPool;
157
158private:
159 DISALLOW_EVIL_CONSTRUCTORS(InputBuffers);
160};
161
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700162class OutputBuffersArray;
163
Wonsik Kim469c8342019-04-11 16:46:09 -0700164class OutputBuffers : public CCodecBuffers {
165public:
Wonsik Kim41d83432020-04-27 16:40:49 -0700166 OutputBuffers(const char *componentName, const char *name = "Output");
167 virtual ~OutputBuffers();
Wonsik Kim469c8342019-04-11 16:46:09 -0700168
169 /**
170 * Register output C2Buffer from the component and obtain corresponding
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700171 * index and MediaCodecBuffer object.
172 *
173 * Returns:
174 * OK if registration succeeds.
175 * NO_MEMORY if all buffers are available but not compatible.
176 * WOULD_BLOCK if there are compatible buffers, but they are all in use.
Wonsik Kim469c8342019-04-11 16:46:09 -0700177 */
178 virtual status_t registerBuffer(
179 const std::shared_ptr<C2Buffer> &buffer,
180 size_t *index,
181 sp<MediaCodecBuffer> *clientBuffer) = 0;
182
183 /**
184 * Register codec specific data as a buffer to be consistent with
185 * MediaCodec behavior.
186 */
187 virtual status_t registerCsd(
188 const C2StreamInitDataInfo::output * /* csd */,
189 size_t * /* index */,
190 sp<MediaCodecBuffer> * /* clientBuffer */) = 0;
191
192 /**
193 * Release the buffer obtained from registerBuffer() and get the
194 * associated C2Buffer object back. Returns true if the buffer was on file
195 * and released successfully.
196 */
197 virtual bool releaseBuffer(
198 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) = 0;
199
200 /**
201 * Flush internal state. After this call, no index or buffer previously
202 * returned from registerBuffer() is valid.
203 */
204 virtual void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) = 0;
205
206 /**
207 * Return array-backed version of output buffers. The returned object
208 * shall retain the internal state so that it will honor index and
209 * buffer from previous calls of registerBuffer().
210 */
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700211 virtual std::unique_ptr<OutputBuffersArray> toArrayMode(size_t size) = 0;
Wonsik Kim469c8342019-04-11 16:46:09 -0700212
213 /**
214 * Initialize SkipCutBuffer object.
215 */
216 void initSkipCutBuffer(
217 int32_t delay, int32_t padding, int32_t sampleRate, int32_t channelCount);
218
219 /**
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700220 * Update SkipCutBuffer from format. The @p format must not be null.
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700221 */
Wonsik Kim970bf0b2020-11-10 11:54:15 -0800222 void updateSkipCutBuffer(const sp<AMessage> &format);
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700223
224 /**
225 * Output Stash
226 * ============
227 *
228 * The output stash is a place to hold output buffers temporarily before
229 * they are registered to output slots. It has 2 main functions:
230 * 1. Allow reordering of output frames as the codec may produce frames in a
231 * different order.
232 * 2. Act as a "buffer" between the codec and the client because the codec
233 * may produce more buffers than available slots. This excess of codec's
234 * output buffers should be registered to slots later, after the client
235 * has released some slots.
236 *
237 * The stash consists of 2 lists of buffers: mPending and mReorderStash.
238 * mPending is a normal FIFO queue with not size limit, while mReorderStash
239 * is a sorted list with size limit mDepth.
240 *
241 * The normal flow of a non-csd output buffer is as follows:
242 *
243 * |----------------OutputBuffers---------------|
244 * |----------Output stash----------| |
245 * Codec --|-> mReorderStash --> mPending --|-> slots --|-> client
246 * | | |
247 * pushToStash() popFromStashAndRegister()
248 *
249 * The buffer that comes from the codec first enters mReorderStash. The
250 * first buffer in mReorderStash gets moved to mPending when mReorderStash
251 * overflows. Buffers in mPending are registered to slots and given to the
252 * client as soon as slots are available.
253 *
254 * Every output buffer that is not a csd buffer should be put on the stash
255 * by calling pushToStash(), then later registered to a slot by calling
256 * popFromStashAndRegister() before notifying the client with
257 * onOutputBufferAvailable().
258 *
259 * Reordering
260 * ==========
261 *
262 * mReorderStash is a sorted list with a specified size limit. The size
263 * limit can be set by calling setReorderDepth().
264 *
265 * Every buffer in mReorderStash has a C2WorkOrdinalStruct, which contains 3
266 * members, all of which are comparable. Which member of C2WorkOrdinalStruct
267 * should be used for reordering can be chosen by calling setReorderKey().
268 */
269
270 /**
271 * Return the reorder depth---the size of mReorderStash.
272 */
273 uint32_t getReorderDepth() const;
274
275 /**
276 * Set the reorder depth.
277 */
278 void setReorderDepth(uint32_t depth);
279
280 /**
281 * Set the type of "key" to use in comparisons.
282 */
283 void setReorderKey(C2Config::ordinal_key_t key);
284
285 /**
286 * Return whether the output stash has any pending buffers.
287 */
288 bool hasPending() const;
289
290 /**
291 * Flush the stash and reset the depth and the key to their default values.
292 */
293 void clearStash();
294
295 /**
296 * Flush the stash.
297 */
298 void flushStash();
299
300 /**
301 * Push a buffer to the reorder stash.
302 *
303 * @param buffer C2Buffer object from the returned work.
304 * @param notify Whether the returned work contains a buffer that should
305 * be reported to the client. This may be false if the
306 * caller wants to process the buffer without notifying the
307 * client.
308 * @param timestamp Buffer timestamp to report to the client.
309 * @param flags Buffer flags to report to the client.
310 * @param format Buffer format to report to the client.
311 * @param ordinal Ordinal used in reordering. This determines when the
312 * buffer will be popped from the output stash by
313 * `popFromStashAndRegister()`.
314 */
315 void pushToStash(
316 const std::shared_ptr<C2Buffer>& buffer,
317 bool notify,
318 int64_t timestamp,
319 int32_t flags,
320 const sp<AMessage>& format,
321 const C2WorkOrdinalStruct& ordinal);
322
323 enum BufferAction : int {
324 SKIP,
325 DISCARD,
326 NOTIFY_CLIENT,
327 REALLOCATE,
328 RETRY,
329 };
330
331 /**
332 * Try to atomically pop the first buffer from the reorder stash and
333 * register it to an output slot. The function returns a value that
334 * indicates a recommended course of action for the caller.
335 *
336 * If the stash is empty, the function will return `SKIP`.
337 *
338 * If the stash is not empty, the function will peek at the first (oldest)
339 * entry in mPending process the buffer in the entry as follows:
340 * - If the buffer should not be sent to the client, the function will
341 * return `DISCARD`. The stash entry will be removed.
342 * - If the buffer should be sent to the client, the function will attempt
343 * to register the buffer to a slot. The registration may have 3 outcomes
344 * corresponding to the following return values:
345 * - `NOTIFY_CLIENT`: The buffer is successfully registered to a slot. The
346 * output arguments @p index and @p outBuffer will contain valid values
347 * that the caller can use to call onOutputBufferAvailable(). The stash
348 * entry will be removed.
349 * - `REALLOCATE`: The buffer is not registered because it is not
350 * compatible with the current slots (which are available). The caller
351 * should reallocate the OutputBuffers with slots that can fit the
352 * returned @p c2Buffer. The stash entry will not be removed
353 * - `RETRY`: All slots are currently occupied by the client. The caller
354 * should try to call this function again after the client has released
355 * some slots.
356 *
357 * @return What the caller should do afterwards.
358 *
359 * @param[out] c2Buffer Underlying C2Buffer associated to the first buffer
360 * on the stash. This value is guaranteed to be valid
361 * unless the return value is `SKIP`.
362 * @param[out] index Slot index. This value is valid only if the return
363 * value is `NOTIFY_CLIENT`.
364 * @param[out] outBuffer Registered buffer. This value is valid only if the
365 * return valu is `NOTIFY_CLIENT`.
366 */
367 BufferAction popFromStashAndRegister(
368 std::shared_ptr<C2Buffer>* c2Buffer,
369 size_t* index,
370 sp<MediaCodecBuffer>* outBuffer);
371
372protected:
373 sp<SkipCutBuffer> mSkipCutBuffer;
374
375 /**
Wonsik Kim469c8342019-04-11 16:46:09 -0700376 * Update the SkipCutBuffer object. No-op if it's never initialized.
377 */
378 void updateSkipCutBuffer(int32_t sampleRate, int32_t channelCount);
379
380 /**
381 * Submit buffer to SkipCutBuffer object, if initialized.
382 */
383 void submit(const sp<MediaCodecBuffer> &buffer);
384
Wonsik Kim469c8342019-04-11 16:46:09 -0700385private:
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700386 // SkipCutBuffer
Wonsik Kim469c8342019-04-11 16:46:09 -0700387 int32_t mDelay;
388 int32_t mPadding;
389 int32_t mSampleRate;
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700390 int32_t mChannelCount;
Wonsik Kim469c8342019-04-11 16:46:09 -0700391
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700392 void setSkipCutBuffer(int32_t skip, int32_t cut);
Wonsik Kim469c8342019-04-11 16:46:09 -0700393
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700394 // Output stash
395
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700396 // Struct for an entry in the output stash (mPending and mReorderStash)
397 struct StashEntry {
398 inline StashEntry()
399 : buffer(nullptr),
400 notify(false),
401 timestamp(0),
402 flags(0),
403 format(),
404 ordinal({0, 0, 0}) {}
405 inline StashEntry(
406 const std::shared_ptr<C2Buffer> &b,
407 bool n,
408 int64_t t,
409 int32_t f,
410 const sp<AMessage> &fmt,
411 const C2WorkOrdinalStruct &o)
412 : buffer(b),
413 notify(n),
414 timestamp(t),
415 flags(f),
416 format(fmt),
417 ordinal(o) {}
418 std::shared_ptr<C2Buffer> buffer;
419 bool notify;
420 int64_t timestamp;
421 int32_t flags;
422 sp<AMessage> format;
423 C2WorkOrdinalStruct ordinal;
424 };
425
426 /**
427 * FIFO queue of stash entries.
428 */
429 std::list<StashEntry> mPending;
430 /**
431 * Sorted list of stash entries.
432 */
433 std::list<StashEntry> mReorderStash;
434 /**
435 * Size limit of mReorderStash.
436 */
437 uint32_t mDepth{0};
438 /**
439 * Choice of key to use in ordering of stash entries in mReorderStash.
440 */
441 C2Config::ordinal_key_t mKey{C2Config::ORDINAL};
442
443 /**
444 * Return false if mPending is empty; otherwise, pop the first entry from
445 * mPending and return true.
446 */
447 bool popPending(StashEntry *entry);
448
449 /**
450 * Push an entry as the first entry of mPending.
451 */
452 void deferPending(const StashEntry &entry);
453
454 /**
455 * Comparison of C2WorkOrdinalStruct based on mKey.
456 */
457 bool less(const C2WorkOrdinalStruct &o1,
458 const C2WorkOrdinalStruct &o2) const;
459
Wonsik Kim469c8342019-04-11 16:46:09 -0700460 DISALLOW_EVIL_CONSTRUCTORS(OutputBuffers);
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700461
462 friend OutputBuffersArray;
Wonsik Kim469c8342019-04-11 16:46:09 -0700463};
464
465/**
466 * Simple local buffer pool backed by std::vector.
467 */
468class LocalBufferPool : public std::enable_shared_from_this<LocalBufferPool> {
469public:
470 /**
471 * Create a new LocalBufferPool object.
472 *
Wonsik Kim469c8342019-04-11 16:46:09 -0700473 * \return a newly created pool object.
474 */
Wonsik Kim41d83432020-04-27 16:40:49 -0700475 static std::shared_ptr<LocalBufferPool> Create();
Wonsik Kim469c8342019-04-11 16:46:09 -0700476
477 /**
478 * Return an ABuffer object whose size is at least |capacity|.
479 *
480 * \param capacity requested capacity
481 * \return nullptr if the pool capacity is reached
482 * an ABuffer object otherwise.
483 */
484 sp<ABuffer> newBuffer(size_t capacity);
485
486private:
487 /**
488 * ABuffer backed by std::vector.
489 */
490 class VectorBuffer : public ::android::ABuffer {
491 public:
492 /**
493 * Construct a VectorBuffer by taking the ownership of supplied vector.
494 *
495 * \param vec backing vector of the buffer. this object takes
496 * ownership at construction.
497 * \param pool a LocalBufferPool object to return the vector at
498 * destruction.
499 */
500 VectorBuffer(std::vector<uint8_t> &&vec, const std::shared_ptr<LocalBufferPool> &pool);
501
502 ~VectorBuffer() override;
503
504 private:
505 std::vector<uint8_t> mVec;
506 std::weak_ptr<LocalBufferPool> mPool;
507 };
508
509 Mutex mMutex;
510 size_t mPoolCapacity;
511 size_t mUsedSize;
512 std::list<std::vector<uint8_t>> mPool;
513
514 /**
515 * Private constructor to prevent constructing non-managed LocalBufferPool.
516 */
517 explicit LocalBufferPool(size_t poolCapacity)
518 : mPoolCapacity(poolCapacity), mUsedSize(0) {
519 }
520
521 /**
522 * Take back the ownership of vec from the destructed VectorBuffer and put
523 * it in front of the pool.
524 */
525 void returnVector(std::vector<uint8_t> &&vec);
526
527 DISALLOW_EVIL_CONSTRUCTORS(LocalBufferPool);
528};
529
530class BuffersArrayImpl;
531
532/**
533 * Flexible buffer slots implementation.
534 */
535class FlexBuffersImpl {
536public:
537 FlexBuffersImpl(const char *name)
538 : mImplName(std::string(name) + ".Impl"),
539 mName(mImplName.c_str()) { }
540
541 /**
542 * Assign an empty slot for a buffer and return the index. If there's no
543 * empty slot, just add one at the end and return it.
544 *
545 * \param buffer[in] a new buffer to assign a slot.
546 * \return index of the assigned slot.
547 */
548 size_t assignSlot(const sp<Codec2Buffer> &buffer);
549
550 /**
551 * Release the slot from the client, and get the C2Buffer object back from
552 * the previously assigned buffer. Note that the slot is not completely free
553 * until the returned C2Buffer object is freed.
554 *
555 * \param buffer[in] the buffer previously assigned a slot.
556 * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored
557 * if null.
558 * \return true if the buffer is successfully released from a slot
559 * false otherwise
560 */
561 bool releaseSlot(
562 const sp<MediaCodecBuffer> &buffer,
563 std::shared_ptr<C2Buffer> *c2buffer,
564 bool release);
565
566 /**
567 * Expire the C2Buffer object in the slot.
568 *
569 * \param c2buffer[in] C2Buffer object which the component released.
570 * \return true if the buffer is found in one of the slots and
571 * successfully released
572 * false otherwise
573 */
574 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer);
575
576 /**
577 * The client abandoned all known buffers, so reclaim the ownership.
578 */
579 void flush();
580
581 /**
582 * Return the number of buffers that are sent to the client but not released
583 * yet.
584 */
Wonsik Kim0487b782020-10-28 11:45:50 -0700585 size_t numActiveSlots() const;
Wonsik Kim469c8342019-04-11 16:46:09 -0700586
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700587 /**
588 * Return the number of buffers that are sent to the component but not
589 * returned back yet.
590 */
591 size_t numComponentBuffers() const;
592
Wonsik Kim469c8342019-04-11 16:46:09 -0700593private:
594 friend class BuffersArrayImpl;
595
596 std::string mImplName; ///< name for debugging
597 const char *mName; ///< C-string version of name
598
599 struct Entry {
600 sp<Codec2Buffer> clientBuffer;
601 std::weak_ptr<C2Buffer> compBuffer;
602 };
603 std::vector<Entry> mBuffers;
604};
605
606/**
607 * Static buffer slots implementation based on a fixed-size array.
608 */
609class BuffersArrayImpl {
610public:
611 BuffersArrayImpl()
612 : mImplName("BuffersArrayImpl"),
613 mName(mImplName.c_str()) { }
614
615 /**
616 * Initialize buffer array from the original |impl|. The buffers known by
617 * the client is preserved, and the empty slots are populated so that the
618 * array size is at least |minSize|.
619 *
620 * \param impl[in] FlexBuffersImpl object used so far.
621 * \param minSize[in] minimum size of the buffer array.
622 * \param allocate[in] function to allocate a client buffer for an empty slot.
623 */
624 void initialize(
625 const FlexBuffersImpl &impl,
626 size_t minSize,
627 std::function<sp<Codec2Buffer>()> allocate);
628
629 /**
630 * Grab a buffer from the underlying array which matches the criteria.
631 *
632 * \param index[out] index of the slot.
633 * \param buffer[out] the matching buffer.
634 * \param match[in] a function to test whether the buffer matches the
635 * criteria or not.
636 * \return OK if successful,
637 * WOULD_BLOCK if slots are being used,
638 * NO_MEMORY if no slot matches the criteria, even though it's
639 * available
640 */
641 status_t grabBuffer(
642 size_t *index,
643 sp<Codec2Buffer> *buffer,
644 std::function<bool(const sp<Codec2Buffer> &)> match =
Wonsik Kim936a89c2020-05-08 16:07:50 -0700645 [](const sp<Codec2Buffer> &buffer) { return (buffer != nullptr); });
Wonsik Kim469c8342019-04-11 16:46:09 -0700646
647 /**
648 * Return the buffer from the client, and get the C2Buffer object back from
649 * the buffer. Note that the slot is not completely free until the returned
650 * C2Buffer object is freed.
651 *
652 * \param buffer[in] the buffer previously grabbed.
653 * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored
654 * if null.
655 * \return true if the buffer is successfully returned
656 * false otherwise
657 */
658 bool returnBuffer(
659 const sp<MediaCodecBuffer> &buffer,
660 std::shared_ptr<C2Buffer> *c2buffer,
661 bool release);
662
663 /**
664 * Expire the C2Buffer object in the slot.
665 *
666 * \param c2buffer[in] C2Buffer object which the component released.
667 * \return true if the buffer is found in one of the slots and
668 * successfully released
669 * false otherwise
670 */
671 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer);
672
673 /**
674 * Populate |array| with the underlying buffer array.
675 *
676 * \param array[out] an array to be filled with the underlying buffer array.
677 */
678 void getArray(Vector<sp<MediaCodecBuffer>> *array) const;
679
680 /**
681 * The client abandoned all known buffers, so reclaim the ownership.
682 */
683 void flush();
684
685 /**
686 * Reallocate the array with the given allocation function.
687 *
688 * \param alloc[in] the allocation function for client buffers.
689 */
690 void realloc(std::function<sp<Codec2Buffer>()> alloc);
691
692 /**
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700693 * Grow the array to the new size. It is a programming error to supply
694 * smaller size as the new size.
695 *
696 * \param newSize[in] new size of the array.
697 * \param alloc[in] the alllocation function for client buffers to fill
698 * the new empty slots.
699 */
700 void grow(size_t newSize, std::function<sp<Codec2Buffer>()> alloc);
701
702 /**
Wonsik Kim469c8342019-04-11 16:46:09 -0700703 * Return the number of buffers that are sent to the client but not released
704 * yet.
705 */
Wonsik Kim0487b782020-10-28 11:45:50 -0700706 size_t numActiveSlots() const;
Wonsik Kim469c8342019-04-11 16:46:09 -0700707
Wonsik Kima39882b2019-06-20 16:13:56 -0700708 /**
709 * Return the size of the array.
710 */
711 size_t arraySize() const;
712
Wonsik Kim469c8342019-04-11 16:46:09 -0700713private:
714 std::string mImplName; ///< name for debugging
715 const char *mName; ///< C-string version of name
716
717 struct Entry {
718 const sp<Codec2Buffer> clientBuffer;
719 std::weak_ptr<C2Buffer> compBuffer;
720 bool ownedByClient;
721 };
722 std::vector<Entry> mBuffers;
723};
724
725class InputBuffersArray : public InputBuffers {
726public:
727 InputBuffersArray(const char *componentName, const char *name = "Input[N]")
728 : InputBuffers(componentName, name) { }
729 ~InputBuffersArray() override = default;
730
731 /**
732 * Initialize this object from the non-array state. We keep existing slots
733 * at the same index, and for empty slots we allocate client buffers with
734 * the given allocate function. If the number of slots is less than minSize,
735 * we fill the array to the minimum size.
736 *
737 * \param impl[in] existing non-array state
738 * \param minSize[in] minimum size of the array
739 * \param allocate[in] allocate function to fill empty slots
740 */
741 void initialize(
742 const FlexBuffersImpl &impl,
743 size_t minSize,
744 std::function<sp<Codec2Buffer>()> allocate);
745
746 bool isArrayMode() const final { return true; }
747
748 std::unique_ptr<InputBuffers> toArrayMode(size_t) final {
749 return nullptr;
750 }
751
752 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final;
753
754 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override;
755
756 bool releaseBuffer(
757 const sp<MediaCodecBuffer> &buffer,
758 std::shared_ptr<C2Buffer> *c2buffer,
759 bool release) override;
760
761 bool expireComponentBuffer(
762 const std::shared_ptr<C2Buffer> &c2buffer) override;
763
764 void flush() override;
765
Wonsik Kim0487b782020-10-28 11:45:50 -0700766 size_t numActiveSlots() const final;
Wonsik Kim469c8342019-04-11 16:46:09 -0700767
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700768protected:
769 sp<Codec2Buffer> createNewBuffer() override;
770
Wonsik Kim469c8342019-04-11 16:46:09 -0700771private:
772 BuffersArrayImpl mImpl;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700773 std::function<sp<Codec2Buffer>()> mAllocate;
Wonsik Kim469c8342019-04-11 16:46:09 -0700774};
775
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800776class SlotInputBuffers : public InputBuffers {
777public:
778 SlotInputBuffers(const char *componentName, const char *name = "Slot-Input")
779 : InputBuffers(componentName, name),
780 mImpl(mName) { }
781 ~SlotInputBuffers() override = default;
782
783 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) final;
784
785 bool releaseBuffer(
786 const sp<MediaCodecBuffer> &buffer,
787 std::shared_ptr<C2Buffer> *c2buffer,
788 bool release) final;
789
790 bool expireComponentBuffer(
791 const std::shared_ptr<C2Buffer> &c2buffer) final;
792
793 void flush() final;
794
795 std::unique_ptr<InputBuffers> toArrayMode(size_t size) final;
796
Wonsik Kim0487b782020-10-28 11:45:50 -0700797 size_t numActiveSlots() const final;
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800798
799protected:
800 sp<Codec2Buffer> createNewBuffer() final;
801
802private:
803 FlexBuffersImpl mImpl;
804};
805
Wonsik Kim469c8342019-04-11 16:46:09 -0700806class LinearInputBuffers : public InputBuffers {
807public:
808 LinearInputBuffers(const char *componentName, const char *name = "1D-Input")
809 : InputBuffers(componentName, name),
810 mImpl(mName) { }
811 ~LinearInputBuffers() override = default;
812
813 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override;
814
815 bool releaseBuffer(
816 const sp<MediaCodecBuffer> &buffer,
817 std::shared_ptr<C2Buffer> *c2buffer,
818 bool release) override;
819
820 bool expireComponentBuffer(
821 const std::shared_ptr<C2Buffer> &c2buffer) override;
822
823 void flush() override;
824
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700825 std::unique_ptr<InputBuffers> toArrayMode(size_t size) override;
Wonsik Kim469c8342019-04-11 16:46:09 -0700826
Wonsik Kim0487b782020-10-28 11:45:50 -0700827 size_t numActiveSlots() const final;
Wonsik Kim469c8342019-04-11 16:46:09 -0700828
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700829protected:
830 sp<Codec2Buffer> createNewBuffer() override;
831
832 FlexBuffersImpl mImpl;
Wonsik Kim469c8342019-04-11 16:46:09 -0700833
834private:
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700835 static sp<Codec2Buffer> Alloc(
836 const std::shared_ptr<C2BlockPool> &pool, const sp<AMessage> &format);
Wonsik Kim469c8342019-04-11 16:46:09 -0700837};
838
839class EncryptedLinearInputBuffers : public LinearInputBuffers {
840public:
841 EncryptedLinearInputBuffers(
842 bool secure,
843 const sp<MemoryDealer> &dealer,
844 const sp<ICrypto> &crypto,
845 int32_t heapSeqNum,
846 size_t capacity,
847 size_t numInputSlots,
848 const char *componentName, const char *name = "EncryptedInput");
849
850 ~EncryptedLinearInputBuffers() override = default;
851
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700852 std::unique_ptr<InputBuffers> toArrayMode(size_t size) override;
853
854protected:
855 sp<Codec2Buffer> createNewBuffer() override;
Wonsik Kim469c8342019-04-11 16:46:09 -0700856
857private:
Wonsik Kim469c8342019-04-11 16:46:09 -0700858 struct Entry {
859 std::weak_ptr<C2LinearBlock> block;
860 sp<IMemory> memory;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700861 int32_t heapSeqNum;
Wonsik Kim469c8342019-04-11 16:46:09 -0700862 };
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700863
864 static sp<Codec2Buffer> Alloc(
865 const std::shared_ptr<C2BlockPool> &pool,
866 const sp<AMessage> &format,
867 C2MemoryUsage usage,
868 const std::shared_ptr<std::vector<Entry>> &memoryVector);
869
870 C2MemoryUsage mUsage;
871 sp<MemoryDealer> mDealer;
872 sp<ICrypto> mCrypto;
873 std::shared_ptr<std::vector<Entry>> mMemoryVector;
Wonsik Kim469c8342019-04-11 16:46:09 -0700874};
875
876class GraphicMetadataInputBuffers : public InputBuffers {
877public:
878 GraphicMetadataInputBuffers(const char *componentName, const char *name = "2D-MetaInput");
879 ~GraphicMetadataInputBuffers() override = default;
880
881 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override;
882
883 bool releaseBuffer(
884 const sp<MediaCodecBuffer> &buffer,
885 std::shared_ptr<C2Buffer> *c2buffer,
886 bool release) override;
887
888 bool expireComponentBuffer(
889 const std::shared_ptr<C2Buffer> &c2buffer) override;
890
891 void flush() override;
892
893 std::unique_ptr<InputBuffers> toArrayMode(size_t size) final;
894
Wonsik Kim0487b782020-10-28 11:45:50 -0700895 size_t numActiveSlots() const final;
Wonsik Kim469c8342019-04-11 16:46:09 -0700896
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700897protected:
898 sp<Codec2Buffer> createNewBuffer() override;
899
Wonsik Kim469c8342019-04-11 16:46:09 -0700900private:
901 FlexBuffersImpl mImpl;
902 std::shared_ptr<C2AllocatorStore> mStore;
903};
904
905class GraphicInputBuffers : public InputBuffers {
906public:
Wonsik Kim41d83432020-04-27 16:40:49 -0700907 GraphicInputBuffers(const char *componentName, const char *name = "2D-BB-Input");
Wonsik Kim469c8342019-04-11 16:46:09 -0700908 ~GraphicInputBuffers() override = default;
909
910 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override;
911
912 bool releaseBuffer(
913 const sp<MediaCodecBuffer> &buffer,
914 std::shared_ptr<C2Buffer> *c2buffer,
915 bool release) override;
916
917 bool expireComponentBuffer(
918 const std::shared_ptr<C2Buffer> &c2buffer) override;
919
920 void flush() override;
921
922 std::unique_ptr<InputBuffers> toArrayMode(
923 size_t size) final;
924
Wonsik Kim0487b782020-10-28 11:45:50 -0700925 size_t numActiveSlots() const final;
Wonsik Kim469c8342019-04-11 16:46:09 -0700926
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700927protected:
928 sp<Codec2Buffer> createNewBuffer() override;
929
Wonsik Kim469c8342019-04-11 16:46:09 -0700930private:
931 FlexBuffersImpl mImpl;
932 std::shared_ptr<LocalBufferPool> mLocalBufferPool;
933};
934
935class DummyInputBuffers : public InputBuffers {
936public:
937 DummyInputBuffers(const char *componentName, const char *name = "2D-Input")
938 : InputBuffers(componentName, name) { }
939 ~DummyInputBuffers() override = default;
940
941 bool requestNewBuffer(size_t *, sp<MediaCodecBuffer> *) override {
942 return false;
943 }
944
945 bool releaseBuffer(
946 const sp<MediaCodecBuffer> &, std::shared_ptr<C2Buffer> *, bool) override {
947 return false;
948 }
949
950 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &) override {
951 return false;
952 }
953 void flush() override {
954 }
955
956 std::unique_ptr<InputBuffers> toArrayMode(size_t) final {
957 return nullptr;
958 }
959
960 bool isArrayMode() const final { return true; }
961
962 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final {
963 array->clear();
964 }
965
Wonsik Kim0487b782020-10-28 11:45:50 -0700966 size_t numActiveSlots() const final {
Wonsik Kim469c8342019-04-11 16:46:09 -0700967 return 0u;
968 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700969
970protected:
971 sp<Codec2Buffer> createNewBuffer() override {
972 return nullptr;
973 }
Wonsik Kim469c8342019-04-11 16:46:09 -0700974};
975
976class OutputBuffersArray : public OutputBuffers {
977public:
978 OutputBuffersArray(const char *componentName, const char *name = "Output[N]")
979 : OutputBuffers(componentName, name) { }
980 ~OutputBuffersArray() override = default;
981
982 /**
983 * Initialize this object from the non-array state. We keep existing slots
984 * at the same index, and for empty slots we allocate client buffers with
985 * the given allocate function. If the number of slots is less than minSize,
986 * we fill the array to the minimum size.
987 *
988 * \param impl[in] existing non-array state
989 * \param minSize[in] minimum size of the array
990 * \param allocate[in] allocate function to fill empty slots
991 */
992 void initialize(
993 const FlexBuffersImpl &impl,
994 size_t minSize,
995 std::function<sp<Codec2Buffer>()> allocate);
996
997 bool isArrayMode() const final { return true; }
998
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700999 std::unique_ptr<OutputBuffersArray> toArrayMode(size_t) final {
Wonsik Kim469c8342019-04-11 16:46:09 -07001000 return nullptr;
1001 }
1002
1003 status_t registerBuffer(
1004 const std::shared_ptr<C2Buffer> &buffer,
1005 size_t *index,
1006 sp<MediaCodecBuffer> *clientBuffer) final;
1007
1008 status_t registerCsd(
1009 const C2StreamInitDataInfo::output *csd,
1010 size_t *index,
1011 sp<MediaCodecBuffer> *clientBuffer) final;
1012
1013 bool releaseBuffer(
1014 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) override;
1015
1016 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) override;
1017
1018 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final;
1019
Wonsik Kim0487b782020-10-28 11:45:50 -07001020 size_t numActiveSlots() const final;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001021
Wonsik Kim469c8342019-04-11 16:46:09 -07001022 /**
1023 * Reallocate the array, filled with buffers with the same size as given
1024 * buffer.
1025 *
1026 * \param c2buffer[in] the reference buffer
1027 */
1028 void realloc(const std::shared_ptr<C2Buffer> &c2buffer);
1029
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001030 /**
1031 * Grow the array to the new size. It is a programming error to supply
1032 * smaller size as the new size.
1033 *
1034 * \param newSize[in] new size of the array.
1035 */
1036 void grow(size_t newSize);
Wonsik Kim469c8342019-04-11 16:46:09 -07001037
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001038 /**
1039 * Transfer the SkipCutBuffer and the output stash from another
1040 * OutputBuffers.
1041 */
1042 void transferFrom(OutputBuffers* source);
1043
Wonsik Kim469c8342019-04-11 16:46:09 -07001044private:
1045 BuffersArrayImpl mImpl;
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001046 std::function<sp<Codec2Buffer>()> mAlloc;
Wonsik Kim469c8342019-04-11 16:46:09 -07001047};
1048
1049class FlexOutputBuffers : public OutputBuffers {
1050public:
1051 FlexOutputBuffers(const char *componentName, const char *name = "Output[]")
1052 : OutputBuffers(componentName, name),
1053 mImpl(mName) { }
1054
1055 status_t registerBuffer(
1056 const std::shared_ptr<C2Buffer> &buffer,
1057 size_t *index,
1058 sp<MediaCodecBuffer> *clientBuffer) override;
1059
1060 status_t registerCsd(
1061 const C2StreamInitDataInfo::output *csd,
1062 size_t *index,
1063 sp<MediaCodecBuffer> *clientBuffer) final;
1064
1065 bool releaseBuffer(
1066 const sp<MediaCodecBuffer> &buffer,
1067 std::shared_ptr<C2Buffer> *c2buffer) override;
1068
1069 void flush(
1070 const std::list<std::unique_ptr<C2Work>> &flushedWork) override;
1071
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001072 std::unique_ptr<OutputBuffersArray> toArrayMode(size_t size) override;
Wonsik Kim469c8342019-04-11 16:46:09 -07001073
Wonsik Kim0487b782020-10-28 11:45:50 -07001074 size_t numActiveSlots() const final;
Wonsik Kim469c8342019-04-11 16:46:09 -07001075
1076 /**
1077 * Return an appropriate Codec2Buffer object for the type of buffers.
1078 *
1079 * \param buffer C2Buffer object to wrap.
1080 *
1081 * \return appropriate Codec2Buffer object to wrap |buffer|.
1082 */
1083 virtual sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) = 0;
1084
1085 /**
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001086 * Return a function that allocates an appropriate Codec2Buffer object for
1087 * the type of buffers, to be used as an empty array buffer. The function
1088 * must not refer to this pointer, since it may be used after this object
1089 * destructs.
Wonsik Kim469c8342019-04-11 16:46:09 -07001090 *
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001091 * \return a function that allocates appropriate Codec2Buffer object,
1092 * which can copy() from C2Buffers.
Wonsik Kim469c8342019-04-11 16:46:09 -07001093 */
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001094 virtual std::function<sp<Codec2Buffer>()> getAlloc() = 0;
Wonsik Kim469c8342019-04-11 16:46:09 -07001095
1096private:
1097 FlexBuffersImpl mImpl;
1098};
1099
1100class LinearOutputBuffers : public FlexOutputBuffers {
1101public:
1102 LinearOutputBuffers(const char *componentName, const char *name = "1D-Output")
1103 : FlexOutputBuffers(componentName, name) { }
1104
1105 void flush(
1106 const std::list<std::unique_ptr<C2Work>> &flushedWork) override;
1107
1108 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override;
1109
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001110 std::function<sp<Codec2Buffer>()> getAlloc() override;
Wonsik Kim469c8342019-04-11 16:46:09 -07001111};
1112
1113class GraphicOutputBuffers : public FlexOutputBuffers {
1114public:
1115 GraphicOutputBuffers(const char *componentName, const char *name = "2D-Output")
1116 : FlexOutputBuffers(componentName, name) { }
1117
1118 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override;
1119
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001120 std::function<sp<Codec2Buffer>()> getAlloc() override;
Wonsik Kim469c8342019-04-11 16:46:09 -07001121};
1122
1123class RawGraphicOutputBuffers : public FlexOutputBuffers {
1124public:
Wonsik Kim41d83432020-04-27 16:40:49 -07001125 RawGraphicOutputBuffers(const char *componentName, const char *name = "2D-BB-Output");
Wonsik Kim469c8342019-04-11 16:46:09 -07001126 ~RawGraphicOutputBuffers() override = default;
1127
1128 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override;
1129
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001130 std::function<sp<Codec2Buffer>()> getAlloc() override;
Wonsik Kim469c8342019-04-11 16:46:09 -07001131
1132private:
1133 std::shared_ptr<LocalBufferPool> mLocalBufferPool;
1134};
1135
1136} // namespace android
1137
1138#endif // CCODEC_BUFFERS_H_