Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 1 | /* |
| 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 Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 31 | class SkipCutBuffer; |
| 32 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 33 | constexpr size_t kLinearBufferSize = 1048576; |
| 34 | // This can fit 4K RGBA frame, and most likely client won't need more than this. |
Wonsik Kim | 8bfa17a | 2019-05-30 22:12:30 -0700 | [diff] [blame] | 35 | constexpr size_t kMaxLinearBufferSize = 4096 * 2304 * 4; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Base class for representation of buffers at one port. |
| 39 | */ |
| 40 | class CCodecBuffers { |
| 41 | public: |
| 42 | CCodecBuffers(const char *componentName, const char *name = "Buffers") |
| 43 | : mComponentName(componentName), |
| 44 | mChannelName(std::string(componentName) + ":" + name), |
| 45 | mName(mChannelName.c_str()) { |
| 46 | } |
| 47 | virtual ~CCodecBuffers() = default; |
| 48 | |
| 49 | /** |
| 50 | * Set format for MediaCodec-facing buffers. |
| 51 | */ |
| 52 | void setFormat(const sp<AMessage> &format); |
| 53 | |
| 54 | /** |
| 55 | * Return a copy of current format. |
| 56 | */ |
| 57 | sp<AMessage> dupFormat(); |
| 58 | |
| 59 | /** |
| 60 | * Returns true if the buffers are operating under array mode. |
| 61 | */ |
| 62 | virtual bool isArrayMode() const { return false; } |
| 63 | |
| 64 | /** |
| 65 | * Fills the vector with MediaCodecBuffer's if in array mode; otherwise, |
| 66 | * no-op. |
| 67 | */ |
| 68 | virtual void getArray(Vector<sp<MediaCodecBuffer>> *) const {} |
| 69 | |
| 70 | /** |
| 71 | * Return number of buffers the client owns. |
| 72 | */ |
| 73 | virtual size_t numClientBuffers() const = 0; |
| 74 | |
| 75 | /** |
| 76 | * Examine image data from the buffer and update the format if necessary. |
| 77 | */ |
| 78 | void handleImageData(const sp<Codec2Buffer> &buffer); |
| 79 | |
| 80 | protected: |
| 81 | std::string mComponentName; ///< name of component for debugging |
| 82 | std::string mChannelName; ///< name of channel for debugging |
| 83 | const char *mName; ///< C-string version of channel name |
| 84 | // Format to be used for creating MediaCodec-facing buffers. |
| 85 | sp<AMessage> mFormat; |
| 86 | |
| 87 | private: |
| 88 | DISALLOW_EVIL_CONSTRUCTORS(CCodecBuffers); |
| 89 | }; |
| 90 | |
| 91 | class InputBuffers : public CCodecBuffers { |
| 92 | public: |
| 93 | InputBuffers(const char *componentName, const char *name = "Input[]") |
| 94 | : CCodecBuffers(componentName, name) { } |
| 95 | virtual ~InputBuffers() = default; |
| 96 | |
| 97 | /** |
| 98 | * Set a block pool to obtain input memory blocks. |
| 99 | */ |
| 100 | void setPool(const std::shared_ptr<C2BlockPool> &pool) { mPool = pool; } |
| 101 | |
| 102 | /** |
| 103 | * Get a new MediaCodecBuffer for input and its corresponding index. |
| 104 | * Returns false if no new buffer can be obtained at the moment. |
| 105 | */ |
| 106 | virtual bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) = 0; |
| 107 | |
| 108 | /** |
| 109 | * Release the buffer obtained from requestNewBuffer() and get the |
| 110 | * associated C2Buffer object back. Returns true if the buffer was on file |
| 111 | * and released successfully. |
| 112 | */ |
| 113 | virtual bool releaseBuffer( |
| 114 | const sp<MediaCodecBuffer> &buffer, |
| 115 | std::shared_ptr<C2Buffer> *c2buffer, |
| 116 | bool release) = 0; |
| 117 | |
| 118 | /** |
| 119 | * Release the buffer that is no longer used by the codec process. Return |
| 120 | * true if and only if the buffer was on file and released successfully. |
| 121 | */ |
| 122 | virtual bool expireComponentBuffer( |
| 123 | const std::shared_ptr<C2Buffer> &c2buffer) = 0; |
| 124 | |
| 125 | /** |
| 126 | * Flush internal state. After this call, no index or buffer previously |
| 127 | * returned from requestNewBuffer() is valid. |
| 128 | */ |
| 129 | virtual void flush() = 0; |
| 130 | |
| 131 | /** |
| 132 | * Return array-backed version of input buffers. The returned object |
| 133 | * shall retain the internal state so that it will honor index and |
| 134 | * buffer from previous calls of requestNewBuffer(). |
| 135 | */ |
| 136 | virtual std::unique_ptr<InputBuffers> toArrayMode(size_t size) = 0; |
| 137 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Release the buffer obtained from requestNewBuffer(), and create a deep |
| 140 | * copy clone of the buffer. |
| 141 | * |
| 142 | * \return the deep copy clone of the buffer; nullptr if cloning is not |
| 143 | * possible. |
| 144 | */ |
| 145 | sp<Codec2Buffer> cloneAndReleaseBuffer(const sp<MediaCodecBuffer> &buffer); |
| 146 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 147 | protected: |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 148 | virtual sp<Codec2Buffer> createNewBuffer() = 0; |
| 149 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 150 | // Pool to obtain blocks for input buffers. |
| 151 | std::shared_ptr<C2BlockPool> mPool; |
| 152 | |
| 153 | private: |
| 154 | DISALLOW_EVIL_CONSTRUCTORS(InputBuffers); |
| 155 | }; |
| 156 | |
| 157 | class OutputBuffers : public CCodecBuffers { |
| 158 | public: |
| 159 | OutputBuffers(const char *componentName, const char *name = "Output") |
| 160 | : CCodecBuffers(componentName, name) { } |
| 161 | virtual ~OutputBuffers() = default; |
| 162 | |
| 163 | /** |
| 164 | * Register output C2Buffer from the component and obtain corresponding |
| 165 | * index and MediaCodecBuffer object. Returns false if registration |
| 166 | * fails. |
| 167 | */ |
| 168 | virtual status_t registerBuffer( |
| 169 | const std::shared_ptr<C2Buffer> &buffer, |
| 170 | size_t *index, |
| 171 | sp<MediaCodecBuffer> *clientBuffer) = 0; |
| 172 | |
| 173 | /** |
| 174 | * Register codec specific data as a buffer to be consistent with |
| 175 | * MediaCodec behavior. |
| 176 | */ |
| 177 | virtual status_t registerCsd( |
| 178 | const C2StreamInitDataInfo::output * /* csd */, |
| 179 | size_t * /* index */, |
| 180 | sp<MediaCodecBuffer> * /* clientBuffer */) = 0; |
| 181 | |
| 182 | /** |
| 183 | * Release the buffer obtained from registerBuffer() and get the |
| 184 | * associated C2Buffer object back. Returns true if the buffer was on file |
| 185 | * and released successfully. |
| 186 | */ |
| 187 | virtual bool releaseBuffer( |
| 188 | const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) = 0; |
| 189 | |
| 190 | /** |
| 191 | * Flush internal state. After this call, no index or buffer previously |
| 192 | * returned from registerBuffer() is valid. |
| 193 | */ |
| 194 | virtual void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) = 0; |
| 195 | |
| 196 | /** |
| 197 | * Return array-backed version of output buffers. The returned object |
| 198 | * shall retain the internal state so that it will honor index and |
| 199 | * buffer from previous calls of registerBuffer(). |
| 200 | */ |
| 201 | virtual std::unique_ptr<OutputBuffers> toArrayMode(size_t size) = 0; |
| 202 | |
| 203 | /** |
| 204 | * Initialize SkipCutBuffer object. |
| 205 | */ |
| 206 | void initSkipCutBuffer( |
| 207 | int32_t delay, int32_t padding, int32_t sampleRate, int32_t channelCount); |
| 208 | |
| 209 | /** |
| 210 | * Update the SkipCutBuffer object. No-op if it's never initialized. |
| 211 | */ |
| 212 | void updateSkipCutBuffer(int32_t sampleRate, int32_t channelCount); |
| 213 | |
| 214 | /** |
| 215 | * Submit buffer to SkipCutBuffer object, if initialized. |
| 216 | */ |
| 217 | void submit(const sp<MediaCodecBuffer> &buffer); |
| 218 | |
| 219 | /** |
| 220 | * Transfer SkipCutBuffer object to the other Buffers object. |
| 221 | */ |
| 222 | void transferSkipCutBuffer(const sp<SkipCutBuffer> &scb); |
| 223 | |
| 224 | protected: |
| 225 | sp<SkipCutBuffer> mSkipCutBuffer; |
| 226 | |
| 227 | private: |
| 228 | int32_t mDelay; |
| 229 | int32_t mPadding; |
| 230 | int32_t mSampleRate; |
Wonsik Kim | 40b0b1d | 2020-03-25 15:47:35 -0700 | [diff] [blame^] | 231 | int32_t mChannelCount; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 232 | |
Wonsik Kim | 40b0b1d | 2020-03-25 15:47:35 -0700 | [diff] [blame^] | 233 | void setSkipCutBuffer(int32_t skip, int32_t cut); |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 234 | |
| 235 | DISALLOW_EVIL_CONSTRUCTORS(OutputBuffers); |
| 236 | }; |
| 237 | |
| 238 | /** |
| 239 | * Simple local buffer pool backed by std::vector. |
| 240 | */ |
| 241 | class LocalBufferPool : public std::enable_shared_from_this<LocalBufferPool> { |
| 242 | public: |
| 243 | /** |
| 244 | * Create a new LocalBufferPool object. |
| 245 | * |
| 246 | * \param poolCapacity max total size of buffers managed by this pool. |
| 247 | * |
| 248 | * \return a newly created pool object. |
| 249 | */ |
| 250 | static std::shared_ptr<LocalBufferPool> Create(size_t poolCapacity); |
| 251 | |
| 252 | /** |
| 253 | * Return an ABuffer object whose size is at least |capacity|. |
| 254 | * |
| 255 | * \param capacity requested capacity |
| 256 | * \return nullptr if the pool capacity is reached |
| 257 | * an ABuffer object otherwise. |
| 258 | */ |
| 259 | sp<ABuffer> newBuffer(size_t capacity); |
| 260 | |
| 261 | private: |
| 262 | /** |
| 263 | * ABuffer backed by std::vector. |
| 264 | */ |
| 265 | class VectorBuffer : public ::android::ABuffer { |
| 266 | public: |
| 267 | /** |
| 268 | * Construct a VectorBuffer by taking the ownership of supplied vector. |
| 269 | * |
| 270 | * \param vec backing vector of the buffer. this object takes |
| 271 | * ownership at construction. |
| 272 | * \param pool a LocalBufferPool object to return the vector at |
| 273 | * destruction. |
| 274 | */ |
| 275 | VectorBuffer(std::vector<uint8_t> &&vec, const std::shared_ptr<LocalBufferPool> &pool); |
| 276 | |
| 277 | ~VectorBuffer() override; |
| 278 | |
| 279 | private: |
| 280 | std::vector<uint8_t> mVec; |
| 281 | std::weak_ptr<LocalBufferPool> mPool; |
| 282 | }; |
| 283 | |
| 284 | Mutex mMutex; |
| 285 | size_t mPoolCapacity; |
| 286 | size_t mUsedSize; |
| 287 | std::list<std::vector<uint8_t>> mPool; |
| 288 | |
| 289 | /** |
| 290 | * Private constructor to prevent constructing non-managed LocalBufferPool. |
| 291 | */ |
| 292 | explicit LocalBufferPool(size_t poolCapacity) |
| 293 | : mPoolCapacity(poolCapacity), mUsedSize(0) { |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Take back the ownership of vec from the destructed VectorBuffer and put |
| 298 | * it in front of the pool. |
| 299 | */ |
| 300 | void returnVector(std::vector<uint8_t> &&vec); |
| 301 | |
| 302 | DISALLOW_EVIL_CONSTRUCTORS(LocalBufferPool); |
| 303 | }; |
| 304 | |
| 305 | class BuffersArrayImpl; |
| 306 | |
| 307 | /** |
| 308 | * Flexible buffer slots implementation. |
| 309 | */ |
| 310 | class FlexBuffersImpl { |
| 311 | public: |
| 312 | FlexBuffersImpl(const char *name) |
| 313 | : mImplName(std::string(name) + ".Impl"), |
| 314 | mName(mImplName.c_str()) { } |
| 315 | |
| 316 | /** |
| 317 | * Assign an empty slot for a buffer and return the index. If there's no |
| 318 | * empty slot, just add one at the end and return it. |
| 319 | * |
| 320 | * \param buffer[in] a new buffer to assign a slot. |
| 321 | * \return index of the assigned slot. |
| 322 | */ |
| 323 | size_t assignSlot(const sp<Codec2Buffer> &buffer); |
| 324 | |
| 325 | /** |
| 326 | * Release the slot from the client, and get the C2Buffer object back from |
| 327 | * the previously assigned buffer. Note that the slot is not completely free |
| 328 | * until the returned C2Buffer object is freed. |
| 329 | * |
| 330 | * \param buffer[in] the buffer previously assigned a slot. |
| 331 | * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored |
| 332 | * if null. |
| 333 | * \return true if the buffer is successfully released from a slot |
| 334 | * false otherwise |
| 335 | */ |
| 336 | bool releaseSlot( |
| 337 | const sp<MediaCodecBuffer> &buffer, |
| 338 | std::shared_ptr<C2Buffer> *c2buffer, |
| 339 | bool release); |
| 340 | |
| 341 | /** |
| 342 | * Expire the C2Buffer object in the slot. |
| 343 | * |
| 344 | * \param c2buffer[in] C2Buffer object which the component released. |
| 345 | * \return true if the buffer is found in one of the slots and |
| 346 | * successfully released |
| 347 | * false otherwise |
| 348 | */ |
| 349 | bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer); |
| 350 | |
| 351 | /** |
| 352 | * The client abandoned all known buffers, so reclaim the ownership. |
| 353 | */ |
| 354 | void flush(); |
| 355 | |
| 356 | /** |
| 357 | * Return the number of buffers that are sent to the client but not released |
| 358 | * yet. |
| 359 | */ |
| 360 | size_t numClientBuffers() const; |
| 361 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 362 | /** |
| 363 | * Return the number of buffers that are sent to the component but not |
| 364 | * returned back yet. |
| 365 | */ |
| 366 | size_t numComponentBuffers() const; |
| 367 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 368 | private: |
| 369 | friend class BuffersArrayImpl; |
| 370 | |
| 371 | std::string mImplName; ///< name for debugging |
| 372 | const char *mName; ///< C-string version of name |
| 373 | |
| 374 | struct Entry { |
| 375 | sp<Codec2Buffer> clientBuffer; |
| 376 | std::weak_ptr<C2Buffer> compBuffer; |
| 377 | }; |
| 378 | std::vector<Entry> mBuffers; |
| 379 | }; |
| 380 | |
| 381 | /** |
| 382 | * Static buffer slots implementation based on a fixed-size array. |
| 383 | */ |
| 384 | class BuffersArrayImpl { |
| 385 | public: |
| 386 | BuffersArrayImpl() |
| 387 | : mImplName("BuffersArrayImpl"), |
| 388 | mName(mImplName.c_str()) { } |
| 389 | |
| 390 | /** |
| 391 | * Initialize buffer array from the original |impl|. The buffers known by |
| 392 | * the client is preserved, and the empty slots are populated so that the |
| 393 | * array size is at least |minSize|. |
| 394 | * |
| 395 | * \param impl[in] FlexBuffersImpl object used so far. |
| 396 | * \param minSize[in] minimum size of the buffer array. |
| 397 | * \param allocate[in] function to allocate a client buffer for an empty slot. |
| 398 | */ |
| 399 | void initialize( |
| 400 | const FlexBuffersImpl &impl, |
| 401 | size_t minSize, |
| 402 | std::function<sp<Codec2Buffer>()> allocate); |
| 403 | |
| 404 | /** |
| 405 | * Grab a buffer from the underlying array which matches the criteria. |
| 406 | * |
| 407 | * \param index[out] index of the slot. |
| 408 | * \param buffer[out] the matching buffer. |
| 409 | * \param match[in] a function to test whether the buffer matches the |
| 410 | * criteria or not. |
| 411 | * \return OK if successful, |
| 412 | * WOULD_BLOCK if slots are being used, |
| 413 | * NO_MEMORY if no slot matches the criteria, even though it's |
| 414 | * available |
| 415 | */ |
| 416 | status_t grabBuffer( |
| 417 | size_t *index, |
| 418 | sp<Codec2Buffer> *buffer, |
| 419 | std::function<bool(const sp<Codec2Buffer> &)> match = |
| 420 | [](const sp<Codec2Buffer> &) { return true; }); |
| 421 | |
| 422 | /** |
| 423 | * Return the buffer from the client, and get the C2Buffer object back from |
| 424 | * the buffer. Note that the slot is not completely free until the returned |
| 425 | * C2Buffer object is freed. |
| 426 | * |
| 427 | * \param buffer[in] the buffer previously grabbed. |
| 428 | * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored |
| 429 | * if null. |
| 430 | * \return true if the buffer is successfully returned |
| 431 | * false otherwise |
| 432 | */ |
| 433 | bool returnBuffer( |
| 434 | const sp<MediaCodecBuffer> &buffer, |
| 435 | std::shared_ptr<C2Buffer> *c2buffer, |
| 436 | bool release); |
| 437 | |
| 438 | /** |
| 439 | * Expire the C2Buffer object in the slot. |
| 440 | * |
| 441 | * \param c2buffer[in] C2Buffer object which the component released. |
| 442 | * \return true if the buffer is found in one of the slots and |
| 443 | * successfully released |
| 444 | * false otherwise |
| 445 | */ |
| 446 | bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer); |
| 447 | |
| 448 | /** |
| 449 | * Populate |array| with the underlying buffer array. |
| 450 | * |
| 451 | * \param array[out] an array to be filled with the underlying buffer array. |
| 452 | */ |
| 453 | void getArray(Vector<sp<MediaCodecBuffer>> *array) const; |
| 454 | |
| 455 | /** |
| 456 | * The client abandoned all known buffers, so reclaim the ownership. |
| 457 | */ |
| 458 | void flush(); |
| 459 | |
| 460 | /** |
| 461 | * Reallocate the array with the given allocation function. |
| 462 | * |
| 463 | * \param alloc[in] the allocation function for client buffers. |
| 464 | */ |
| 465 | void realloc(std::function<sp<Codec2Buffer>()> alloc); |
| 466 | |
| 467 | /** |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 468 | * Grow the array to the new size. It is a programming error to supply |
| 469 | * smaller size as the new size. |
| 470 | * |
| 471 | * \param newSize[in] new size of the array. |
| 472 | * \param alloc[in] the alllocation function for client buffers to fill |
| 473 | * the new empty slots. |
| 474 | */ |
| 475 | void grow(size_t newSize, std::function<sp<Codec2Buffer>()> alloc); |
| 476 | |
| 477 | /** |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 478 | * Return the number of buffers that are sent to the client but not released |
| 479 | * yet. |
| 480 | */ |
| 481 | size_t numClientBuffers() const; |
| 482 | |
Wonsik Kim | a39882b | 2019-06-20 16:13:56 -0700 | [diff] [blame] | 483 | /** |
| 484 | * Return the size of the array. |
| 485 | */ |
| 486 | size_t arraySize() const; |
| 487 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 488 | private: |
| 489 | std::string mImplName; ///< name for debugging |
| 490 | const char *mName; ///< C-string version of name |
| 491 | |
| 492 | struct Entry { |
| 493 | const sp<Codec2Buffer> clientBuffer; |
| 494 | std::weak_ptr<C2Buffer> compBuffer; |
| 495 | bool ownedByClient; |
| 496 | }; |
| 497 | std::vector<Entry> mBuffers; |
| 498 | }; |
| 499 | |
| 500 | class InputBuffersArray : public InputBuffers { |
| 501 | public: |
| 502 | InputBuffersArray(const char *componentName, const char *name = "Input[N]") |
| 503 | : InputBuffers(componentName, name) { } |
| 504 | ~InputBuffersArray() override = default; |
| 505 | |
| 506 | /** |
| 507 | * Initialize this object from the non-array state. We keep existing slots |
| 508 | * at the same index, and for empty slots we allocate client buffers with |
| 509 | * the given allocate function. If the number of slots is less than minSize, |
| 510 | * we fill the array to the minimum size. |
| 511 | * |
| 512 | * \param impl[in] existing non-array state |
| 513 | * \param minSize[in] minimum size of the array |
| 514 | * \param allocate[in] allocate function to fill empty slots |
| 515 | */ |
| 516 | void initialize( |
| 517 | const FlexBuffersImpl &impl, |
| 518 | size_t minSize, |
| 519 | std::function<sp<Codec2Buffer>()> allocate); |
| 520 | |
| 521 | bool isArrayMode() const final { return true; } |
| 522 | |
| 523 | std::unique_ptr<InputBuffers> toArrayMode(size_t) final { |
| 524 | return nullptr; |
| 525 | } |
| 526 | |
| 527 | void getArray(Vector<sp<MediaCodecBuffer>> *array) const final; |
| 528 | |
| 529 | bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override; |
| 530 | |
| 531 | bool releaseBuffer( |
| 532 | const sp<MediaCodecBuffer> &buffer, |
| 533 | std::shared_ptr<C2Buffer> *c2buffer, |
| 534 | bool release) override; |
| 535 | |
| 536 | bool expireComponentBuffer( |
| 537 | const std::shared_ptr<C2Buffer> &c2buffer) override; |
| 538 | |
| 539 | void flush() override; |
| 540 | |
| 541 | size_t numClientBuffers() const final; |
| 542 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 543 | protected: |
| 544 | sp<Codec2Buffer> createNewBuffer() override; |
| 545 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 546 | private: |
| 547 | BuffersArrayImpl mImpl; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 548 | std::function<sp<Codec2Buffer>()> mAllocate; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 549 | }; |
| 550 | |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 551 | class SlotInputBuffers : public InputBuffers { |
| 552 | public: |
| 553 | SlotInputBuffers(const char *componentName, const char *name = "Slot-Input") |
| 554 | : InputBuffers(componentName, name), |
| 555 | mImpl(mName) { } |
| 556 | ~SlotInputBuffers() override = default; |
| 557 | |
| 558 | bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) final; |
| 559 | |
| 560 | bool releaseBuffer( |
| 561 | const sp<MediaCodecBuffer> &buffer, |
| 562 | std::shared_ptr<C2Buffer> *c2buffer, |
| 563 | bool release) final; |
| 564 | |
| 565 | bool expireComponentBuffer( |
| 566 | const std::shared_ptr<C2Buffer> &c2buffer) final; |
| 567 | |
| 568 | void flush() final; |
| 569 | |
| 570 | std::unique_ptr<InputBuffers> toArrayMode(size_t size) final; |
| 571 | |
| 572 | size_t numClientBuffers() const final; |
| 573 | |
| 574 | protected: |
| 575 | sp<Codec2Buffer> createNewBuffer() final; |
| 576 | |
| 577 | private: |
| 578 | FlexBuffersImpl mImpl; |
| 579 | }; |
| 580 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 581 | class LinearInputBuffers : public InputBuffers { |
| 582 | public: |
| 583 | LinearInputBuffers(const char *componentName, const char *name = "1D-Input") |
| 584 | : InputBuffers(componentName, name), |
| 585 | mImpl(mName) { } |
| 586 | ~LinearInputBuffers() override = default; |
| 587 | |
| 588 | bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override; |
| 589 | |
| 590 | bool releaseBuffer( |
| 591 | const sp<MediaCodecBuffer> &buffer, |
| 592 | std::shared_ptr<C2Buffer> *c2buffer, |
| 593 | bool release) override; |
| 594 | |
| 595 | bool expireComponentBuffer( |
| 596 | const std::shared_ptr<C2Buffer> &c2buffer) override; |
| 597 | |
| 598 | void flush() override; |
| 599 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 600 | std::unique_ptr<InputBuffers> toArrayMode(size_t size) override; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 601 | |
| 602 | size_t numClientBuffers() const final; |
| 603 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 604 | protected: |
| 605 | sp<Codec2Buffer> createNewBuffer() override; |
| 606 | |
| 607 | FlexBuffersImpl mImpl; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 608 | |
| 609 | private: |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 610 | static sp<Codec2Buffer> Alloc( |
| 611 | const std::shared_ptr<C2BlockPool> &pool, const sp<AMessage> &format); |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 612 | }; |
| 613 | |
| 614 | class EncryptedLinearInputBuffers : public LinearInputBuffers { |
| 615 | public: |
| 616 | EncryptedLinearInputBuffers( |
| 617 | bool secure, |
| 618 | const sp<MemoryDealer> &dealer, |
| 619 | const sp<ICrypto> &crypto, |
| 620 | int32_t heapSeqNum, |
| 621 | size_t capacity, |
| 622 | size_t numInputSlots, |
| 623 | const char *componentName, const char *name = "EncryptedInput"); |
| 624 | |
| 625 | ~EncryptedLinearInputBuffers() override = default; |
| 626 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 627 | std::unique_ptr<InputBuffers> toArrayMode(size_t size) override; |
| 628 | |
| 629 | protected: |
| 630 | sp<Codec2Buffer> createNewBuffer() override; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 631 | |
| 632 | private: |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 633 | struct Entry { |
| 634 | std::weak_ptr<C2LinearBlock> block; |
| 635 | sp<IMemory> memory; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 636 | int32_t heapSeqNum; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 637 | }; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 638 | |
| 639 | static sp<Codec2Buffer> Alloc( |
| 640 | const std::shared_ptr<C2BlockPool> &pool, |
| 641 | const sp<AMessage> &format, |
| 642 | C2MemoryUsage usage, |
| 643 | const std::shared_ptr<std::vector<Entry>> &memoryVector); |
| 644 | |
| 645 | C2MemoryUsage mUsage; |
| 646 | sp<MemoryDealer> mDealer; |
| 647 | sp<ICrypto> mCrypto; |
| 648 | std::shared_ptr<std::vector<Entry>> mMemoryVector; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 649 | }; |
| 650 | |
| 651 | class GraphicMetadataInputBuffers : public InputBuffers { |
| 652 | public: |
| 653 | GraphicMetadataInputBuffers(const char *componentName, const char *name = "2D-MetaInput"); |
| 654 | ~GraphicMetadataInputBuffers() override = default; |
| 655 | |
| 656 | bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override; |
| 657 | |
| 658 | bool releaseBuffer( |
| 659 | const sp<MediaCodecBuffer> &buffer, |
| 660 | std::shared_ptr<C2Buffer> *c2buffer, |
| 661 | bool release) override; |
| 662 | |
| 663 | bool expireComponentBuffer( |
| 664 | const std::shared_ptr<C2Buffer> &c2buffer) override; |
| 665 | |
| 666 | void flush() override; |
| 667 | |
| 668 | std::unique_ptr<InputBuffers> toArrayMode(size_t size) final; |
| 669 | |
| 670 | size_t numClientBuffers() const final; |
| 671 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 672 | protected: |
| 673 | sp<Codec2Buffer> createNewBuffer() override; |
| 674 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 675 | private: |
| 676 | FlexBuffersImpl mImpl; |
| 677 | std::shared_ptr<C2AllocatorStore> mStore; |
| 678 | }; |
| 679 | |
| 680 | class GraphicInputBuffers : public InputBuffers { |
| 681 | public: |
| 682 | GraphicInputBuffers( |
| 683 | size_t numInputSlots, const char *componentName, const char *name = "2D-BB-Input"); |
| 684 | ~GraphicInputBuffers() override = default; |
| 685 | |
| 686 | bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override; |
| 687 | |
| 688 | bool releaseBuffer( |
| 689 | const sp<MediaCodecBuffer> &buffer, |
| 690 | std::shared_ptr<C2Buffer> *c2buffer, |
| 691 | bool release) override; |
| 692 | |
| 693 | bool expireComponentBuffer( |
| 694 | const std::shared_ptr<C2Buffer> &c2buffer) override; |
| 695 | |
| 696 | void flush() override; |
| 697 | |
| 698 | std::unique_ptr<InputBuffers> toArrayMode( |
| 699 | size_t size) final; |
| 700 | |
| 701 | size_t numClientBuffers() const final; |
| 702 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 703 | protected: |
| 704 | sp<Codec2Buffer> createNewBuffer() override; |
| 705 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 706 | private: |
| 707 | FlexBuffersImpl mImpl; |
| 708 | std::shared_ptr<LocalBufferPool> mLocalBufferPool; |
| 709 | }; |
| 710 | |
| 711 | class DummyInputBuffers : public InputBuffers { |
| 712 | public: |
| 713 | DummyInputBuffers(const char *componentName, const char *name = "2D-Input") |
| 714 | : InputBuffers(componentName, name) { } |
| 715 | ~DummyInputBuffers() override = default; |
| 716 | |
| 717 | bool requestNewBuffer(size_t *, sp<MediaCodecBuffer> *) override { |
| 718 | return false; |
| 719 | } |
| 720 | |
| 721 | bool releaseBuffer( |
| 722 | const sp<MediaCodecBuffer> &, std::shared_ptr<C2Buffer> *, bool) override { |
| 723 | return false; |
| 724 | } |
| 725 | |
| 726 | bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &) override { |
| 727 | return false; |
| 728 | } |
| 729 | void flush() override { |
| 730 | } |
| 731 | |
| 732 | std::unique_ptr<InputBuffers> toArrayMode(size_t) final { |
| 733 | return nullptr; |
| 734 | } |
| 735 | |
| 736 | bool isArrayMode() const final { return true; } |
| 737 | |
| 738 | void getArray(Vector<sp<MediaCodecBuffer>> *array) const final { |
| 739 | array->clear(); |
| 740 | } |
| 741 | |
| 742 | size_t numClientBuffers() const final { |
| 743 | return 0u; |
| 744 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 745 | |
| 746 | protected: |
| 747 | sp<Codec2Buffer> createNewBuffer() override { |
| 748 | return nullptr; |
| 749 | } |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 750 | }; |
| 751 | |
| 752 | class OutputBuffersArray : public OutputBuffers { |
| 753 | public: |
| 754 | OutputBuffersArray(const char *componentName, const char *name = "Output[N]") |
| 755 | : OutputBuffers(componentName, name) { } |
| 756 | ~OutputBuffersArray() override = default; |
| 757 | |
| 758 | /** |
| 759 | * Initialize this object from the non-array state. We keep existing slots |
| 760 | * at the same index, and for empty slots we allocate client buffers with |
| 761 | * the given allocate function. If the number of slots is less than minSize, |
| 762 | * we fill the array to the minimum size. |
| 763 | * |
| 764 | * \param impl[in] existing non-array state |
| 765 | * \param minSize[in] minimum size of the array |
| 766 | * \param allocate[in] allocate function to fill empty slots |
| 767 | */ |
| 768 | void initialize( |
| 769 | const FlexBuffersImpl &impl, |
| 770 | size_t minSize, |
| 771 | std::function<sp<Codec2Buffer>()> allocate); |
| 772 | |
| 773 | bool isArrayMode() const final { return true; } |
| 774 | |
| 775 | std::unique_ptr<OutputBuffers> toArrayMode(size_t) final { |
| 776 | return nullptr; |
| 777 | } |
| 778 | |
| 779 | status_t registerBuffer( |
| 780 | const std::shared_ptr<C2Buffer> &buffer, |
| 781 | size_t *index, |
| 782 | sp<MediaCodecBuffer> *clientBuffer) final; |
| 783 | |
| 784 | status_t registerCsd( |
| 785 | const C2StreamInitDataInfo::output *csd, |
| 786 | size_t *index, |
| 787 | sp<MediaCodecBuffer> *clientBuffer) final; |
| 788 | |
| 789 | bool releaseBuffer( |
| 790 | const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) override; |
| 791 | |
| 792 | void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) override; |
| 793 | |
| 794 | void getArray(Vector<sp<MediaCodecBuffer>> *array) const final; |
| 795 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 796 | size_t numClientBuffers() const final; |
| 797 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 798 | /** |
| 799 | * Reallocate the array, filled with buffers with the same size as given |
| 800 | * buffer. |
| 801 | * |
| 802 | * \param c2buffer[in] the reference buffer |
| 803 | */ |
| 804 | void realloc(const std::shared_ptr<C2Buffer> &c2buffer); |
| 805 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 806 | /** |
| 807 | * Grow the array to the new size. It is a programming error to supply |
| 808 | * smaller size as the new size. |
| 809 | * |
| 810 | * \param newSize[in] new size of the array. |
| 811 | */ |
| 812 | void grow(size_t newSize); |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 813 | |
| 814 | private: |
| 815 | BuffersArrayImpl mImpl; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 816 | std::function<sp<Codec2Buffer>()> mAlloc; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 817 | }; |
| 818 | |
| 819 | class FlexOutputBuffers : public OutputBuffers { |
| 820 | public: |
| 821 | FlexOutputBuffers(const char *componentName, const char *name = "Output[]") |
| 822 | : OutputBuffers(componentName, name), |
| 823 | mImpl(mName) { } |
| 824 | |
| 825 | status_t registerBuffer( |
| 826 | const std::shared_ptr<C2Buffer> &buffer, |
| 827 | size_t *index, |
| 828 | sp<MediaCodecBuffer> *clientBuffer) override; |
| 829 | |
| 830 | status_t registerCsd( |
| 831 | const C2StreamInitDataInfo::output *csd, |
| 832 | size_t *index, |
| 833 | sp<MediaCodecBuffer> *clientBuffer) final; |
| 834 | |
| 835 | bool releaseBuffer( |
| 836 | const sp<MediaCodecBuffer> &buffer, |
| 837 | std::shared_ptr<C2Buffer> *c2buffer) override; |
| 838 | |
| 839 | void flush( |
| 840 | const std::list<std::unique_ptr<C2Work>> &flushedWork) override; |
| 841 | |
| 842 | std::unique_ptr<OutputBuffers> toArrayMode(size_t size) override; |
| 843 | |
| 844 | size_t numClientBuffers() const final; |
| 845 | |
| 846 | /** |
| 847 | * Return an appropriate Codec2Buffer object for the type of buffers. |
| 848 | * |
| 849 | * \param buffer C2Buffer object to wrap. |
| 850 | * |
| 851 | * \return appropriate Codec2Buffer object to wrap |buffer|. |
| 852 | */ |
| 853 | virtual sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) = 0; |
| 854 | |
| 855 | /** |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 856 | * Return a function that allocates an appropriate Codec2Buffer object for |
| 857 | * the type of buffers, to be used as an empty array buffer. The function |
| 858 | * must not refer to this pointer, since it may be used after this object |
| 859 | * destructs. |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 860 | * |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 861 | * \return a function that allocates appropriate Codec2Buffer object, |
| 862 | * which can copy() from C2Buffers. |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 863 | */ |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 864 | virtual std::function<sp<Codec2Buffer>()> getAlloc() = 0; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 865 | |
| 866 | private: |
| 867 | FlexBuffersImpl mImpl; |
| 868 | }; |
| 869 | |
| 870 | class LinearOutputBuffers : public FlexOutputBuffers { |
| 871 | public: |
| 872 | LinearOutputBuffers(const char *componentName, const char *name = "1D-Output") |
| 873 | : FlexOutputBuffers(componentName, name) { } |
| 874 | |
| 875 | void flush( |
| 876 | const std::list<std::unique_ptr<C2Work>> &flushedWork) override; |
| 877 | |
| 878 | sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override; |
| 879 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 880 | std::function<sp<Codec2Buffer>()> getAlloc() override; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 881 | }; |
| 882 | |
| 883 | class GraphicOutputBuffers : public FlexOutputBuffers { |
| 884 | public: |
| 885 | GraphicOutputBuffers(const char *componentName, const char *name = "2D-Output") |
| 886 | : FlexOutputBuffers(componentName, name) { } |
| 887 | |
| 888 | sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override; |
| 889 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 890 | std::function<sp<Codec2Buffer>()> getAlloc() override; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 891 | }; |
| 892 | |
| 893 | class RawGraphicOutputBuffers : public FlexOutputBuffers { |
| 894 | public: |
| 895 | RawGraphicOutputBuffers( |
| 896 | size_t numOutputSlots, const char *componentName, const char *name = "2D-BB-Output"); |
| 897 | ~RawGraphicOutputBuffers() override = default; |
| 898 | |
| 899 | sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override; |
| 900 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 901 | std::function<sp<Codec2Buffer>()> getAlloc() override; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 902 | |
| 903 | private: |
| 904 | std::shared_ptr<LocalBufferPool> mLocalBufferPool; |
| 905 | }; |
| 906 | |
| 907 | } // namespace android |
| 908 | |
| 909 | #endif // CCODEC_BUFFERS_H_ |