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; |
| 231 | |
| 232 | void setSkipCutBuffer(int32_t skip, int32_t cut, int32_t channelCount); |
| 233 | |
| 234 | DISALLOW_EVIL_CONSTRUCTORS(OutputBuffers); |
| 235 | }; |
| 236 | |
| 237 | /** |
| 238 | * Simple local buffer pool backed by std::vector. |
| 239 | */ |
| 240 | class LocalBufferPool : public std::enable_shared_from_this<LocalBufferPool> { |
| 241 | public: |
| 242 | /** |
| 243 | * Create a new LocalBufferPool object. |
| 244 | * |
| 245 | * \param poolCapacity max total size of buffers managed by this pool. |
| 246 | * |
| 247 | * \return a newly created pool object. |
| 248 | */ |
| 249 | static std::shared_ptr<LocalBufferPool> Create(size_t poolCapacity); |
| 250 | |
| 251 | /** |
| 252 | * Return an ABuffer object whose size is at least |capacity|. |
| 253 | * |
| 254 | * \param capacity requested capacity |
| 255 | * \return nullptr if the pool capacity is reached |
| 256 | * an ABuffer object otherwise. |
| 257 | */ |
| 258 | sp<ABuffer> newBuffer(size_t capacity); |
| 259 | |
| 260 | private: |
| 261 | /** |
| 262 | * ABuffer backed by std::vector. |
| 263 | */ |
| 264 | class VectorBuffer : public ::android::ABuffer { |
| 265 | public: |
| 266 | /** |
| 267 | * Construct a VectorBuffer by taking the ownership of supplied vector. |
| 268 | * |
| 269 | * \param vec backing vector of the buffer. this object takes |
| 270 | * ownership at construction. |
| 271 | * \param pool a LocalBufferPool object to return the vector at |
| 272 | * destruction. |
| 273 | */ |
| 274 | VectorBuffer(std::vector<uint8_t> &&vec, const std::shared_ptr<LocalBufferPool> &pool); |
| 275 | |
| 276 | ~VectorBuffer() override; |
| 277 | |
| 278 | private: |
| 279 | std::vector<uint8_t> mVec; |
| 280 | std::weak_ptr<LocalBufferPool> mPool; |
| 281 | }; |
| 282 | |
| 283 | Mutex mMutex; |
| 284 | size_t mPoolCapacity; |
| 285 | size_t mUsedSize; |
| 286 | std::list<std::vector<uint8_t>> mPool; |
| 287 | |
| 288 | /** |
| 289 | * Private constructor to prevent constructing non-managed LocalBufferPool. |
| 290 | */ |
| 291 | explicit LocalBufferPool(size_t poolCapacity) |
| 292 | : mPoolCapacity(poolCapacity), mUsedSize(0) { |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Take back the ownership of vec from the destructed VectorBuffer and put |
| 297 | * it in front of the pool. |
| 298 | */ |
| 299 | void returnVector(std::vector<uint8_t> &&vec); |
| 300 | |
| 301 | DISALLOW_EVIL_CONSTRUCTORS(LocalBufferPool); |
| 302 | }; |
| 303 | |
| 304 | class BuffersArrayImpl; |
| 305 | |
| 306 | /** |
| 307 | * Flexible buffer slots implementation. |
| 308 | */ |
| 309 | class FlexBuffersImpl { |
| 310 | public: |
| 311 | FlexBuffersImpl(const char *name) |
| 312 | : mImplName(std::string(name) + ".Impl"), |
| 313 | mName(mImplName.c_str()) { } |
| 314 | |
| 315 | /** |
| 316 | * Assign an empty slot for a buffer and return the index. If there's no |
| 317 | * empty slot, just add one at the end and return it. |
| 318 | * |
| 319 | * \param buffer[in] a new buffer to assign a slot. |
| 320 | * \return index of the assigned slot. |
| 321 | */ |
| 322 | size_t assignSlot(const sp<Codec2Buffer> &buffer); |
| 323 | |
| 324 | /** |
| 325 | * Release the slot from the client, and get the C2Buffer object back from |
| 326 | * the previously assigned buffer. Note that the slot is not completely free |
| 327 | * until the returned C2Buffer object is freed. |
| 328 | * |
| 329 | * \param buffer[in] the buffer previously assigned a slot. |
| 330 | * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored |
| 331 | * if null. |
| 332 | * \return true if the buffer is successfully released from a slot |
| 333 | * false otherwise |
| 334 | */ |
| 335 | bool releaseSlot( |
| 336 | const sp<MediaCodecBuffer> &buffer, |
| 337 | std::shared_ptr<C2Buffer> *c2buffer, |
| 338 | bool release); |
| 339 | |
| 340 | /** |
| 341 | * Expire the C2Buffer object in the slot. |
| 342 | * |
| 343 | * \param c2buffer[in] C2Buffer object which the component released. |
| 344 | * \return true if the buffer is found in one of the slots and |
| 345 | * successfully released |
| 346 | * false otherwise |
| 347 | */ |
| 348 | bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer); |
| 349 | |
| 350 | /** |
| 351 | * The client abandoned all known buffers, so reclaim the ownership. |
| 352 | */ |
| 353 | void flush(); |
| 354 | |
| 355 | /** |
| 356 | * Return the number of buffers that are sent to the client but not released |
| 357 | * yet. |
| 358 | */ |
| 359 | size_t numClientBuffers() const; |
| 360 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 361 | /** |
| 362 | * Return the number of buffers that are sent to the component but not |
| 363 | * returned back yet. |
| 364 | */ |
| 365 | size_t numComponentBuffers() const; |
| 366 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 367 | private: |
| 368 | friend class BuffersArrayImpl; |
| 369 | |
| 370 | std::string mImplName; ///< name for debugging |
| 371 | const char *mName; ///< C-string version of name |
| 372 | |
| 373 | struct Entry { |
| 374 | sp<Codec2Buffer> clientBuffer; |
| 375 | std::weak_ptr<C2Buffer> compBuffer; |
| 376 | }; |
| 377 | std::vector<Entry> mBuffers; |
| 378 | }; |
| 379 | |
| 380 | /** |
| 381 | * Static buffer slots implementation based on a fixed-size array. |
| 382 | */ |
| 383 | class BuffersArrayImpl { |
| 384 | public: |
| 385 | BuffersArrayImpl() |
| 386 | : mImplName("BuffersArrayImpl"), |
| 387 | mName(mImplName.c_str()) { } |
| 388 | |
| 389 | /** |
| 390 | * Initialize buffer array from the original |impl|. The buffers known by |
| 391 | * the client is preserved, and the empty slots are populated so that the |
| 392 | * array size is at least |minSize|. |
| 393 | * |
| 394 | * \param impl[in] FlexBuffersImpl object used so far. |
| 395 | * \param minSize[in] minimum size of the buffer array. |
| 396 | * \param allocate[in] function to allocate a client buffer for an empty slot. |
| 397 | */ |
| 398 | void initialize( |
| 399 | const FlexBuffersImpl &impl, |
| 400 | size_t minSize, |
| 401 | std::function<sp<Codec2Buffer>()> allocate); |
| 402 | |
| 403 | /** |
| 404 | * Grab a buffer from the underlying array which matches the criteria. |
| 405 | * |
| 406 | * \param index[out] index of the slot. |
| 407 | * \param buffer[out] the matching buffer. |
| 408 | * \param match[in] a function to test whether the buffer matches the |
| 409 | * criteria or not. |
| 410 | * \return OK if successful, |
| 411 | * WOULD_BLOCK if slots are being used, |
| 412 | * NO_MEMORY if no slot matches the criteria, even though it's |
| 413 | * available |
| 414 | */ |
| 415 | status_t grabBuffer( |
| 416 | size_t *index, |
| 417 | sp<Codec2Buffer> *buffer, |
| 418 | std::function<bool(const sp<Codec2Buffer> &)> match = |
| 419 | [](const sp<Codec2Buffer> &) { return true; }); |
| 420 | |
| 421 | /** |
| 422 | * Return the buffer from the client, and get the C2Buffer object back from |
| 423 | * the buffer. Note that the slot is not completely free until the returned |
| 424 | * C2Buffer object is freed. |
| 425 | * |
| 426 | * \param buffer[in] the buffer previously grabbed. |
| 427 | * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored |
| 428 | * if null. |
| 429 | * \return true if the buffer is successfully returned |
| 430 | * false otherwise |
| 431 | */ |
| 432 | bool returnBuffer( |
| 433 | const sp<MediaCodecBuffer> &buffer, |
| 434 | std::shared_ptr<C2Buffer> *c2buffer, |
| 435 | bool release); |
| 436 | |
| 437 | /** |
| 438 | * Expire the C2Buffer object in the slot. |
| 439 | * |
| 440 | * \param c2buffer[in] C2Buffer object which the component released. |
| 441 | * \return true if the buffer is found in one of the slots and |
| 442 | * successfully released |
| 443 | * false otherwise |
| 444 | */ |
| 445 | bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer); |
| 446 | |
| 447 | /** |
| 448 | * Populate |array| with the underlying buffer array. |
| 449 | * |
| 450 | * \param array[out] an array to be filled with the underlying buffer array. |
| 451 | */ |
| 452 | void getArray(Vector<sp<MediaCodecBuffer>> *array) const; |
| 453 | |
| 454 | /** |
| 455 | * The client abandoned all known buffers, so reclaim the ownership. |
| 456 | */ |
| 457 | void flush(); |
| 458 | |
| 459 | /** |
| 460 | * Reallocate the array with the given allocation function. |
| 461 | * |
| 462 | * \param alloc[in] the allocation function for client buffers. |
| 463 | */ |
| 464 | void realloc(std::function<sp<Codec2Buffer>()> alloc); |
| 465 | |
| 466 | /** |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 467 | * Grow the array to the new size. It is a programming error to supply |
| 468 | * smaller size as the new size. |
| 469 | * |
| 470 | * \param newSize[in] new size of the array. |
| 471 | * \param alloc[in] the alllocation function for client buffers to fill |
| 472 | * the new empty slots. |
| 473 | */ |
| 474 | void grow(size_t newSize, std::function<sp<Codec2Buffer>()> alloc); |
| 475 | |
| 476 | /** |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 477 | * Return the number of buffers that are sent to the client but not released |
| 478 | * yet. |
| 479 | */ |
| 480 | size_t numClientBuffers() const; |
| 481 | |
Wonsik Kim | a39882b | 2019-06-20 16:13:56 -0700 | [diff] [blame] | 482 | /** |
| 483 | * Return the size of the array. |
| 484 | */ |
| 485 | size_t arraySize() const; |
| 486 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 487 | private: |
| 488 | std::string mImplName; ///< name for debugging |
| 489 | const char *mName; ///< C-string version of name |
| 490 | |
| 491 | struct Entry { |
| 492 | const sp<Codec2Buffer> clientBuffer; |
| 493 | std::weak_ptr<C2Buffer> compBuffer; |
| 494 | bool ownedByClient; |
| 495 | }; |
| 496 | std::vector<Entry> mBuffers; |
| 497 | }; |
| 498 | |
| 499 | class InputBuffersArray : public InputBuffers { |
| 500 | public: |
| 501 | InputBuffersArray(const char *componentName, const char *name = "Input[N]") |
| 502 | : InputBuffers(componentName, name) { } |
| 503 | ~InputBuffersArray() override = default; |
| 504 | |
| 505 | /** |
| 506 | * Initialize this object from the non-array state. We keep existing slots |
| 507 | * at the same index, and for empty slots we allocate client buffers with |
| 508 | * the given allocate function. If the number of slots is less than minSize, |
| 509 | * we fill the array to the minimum size. |
| 510 | * |
| 511 | * \param impl[in] existing non-array state |
| 512 | * \param minSize[in] minimum size of the array |
| 513 | * \param allocate[in] allocate function to fill empty slots |
| 514 | */ |
| 515 | void initialize( |
| 516 | const FlexBuffersImpl &impl, |
| 517 | size_t minSize, |
| 518 | std::function<sp<Codec2Buffer>()> allocate); |
| 519 | |
| 520 | bool isArrayMode() const final { return true; } |
| 521 | |
| 522 | std::unique_ptr<InputBuffers> toArrayMode(size_t) final { |
| 523 | return nullptr; |
| 524 | } |
| 525 | |
| 526 | void getArray(Vector<sp<MediaCodecBuffer>> *array) const final; |
| 527 | |
| 528 | bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override; |
| 529 | |
| 530 | bool releaseBuffer( |
| 531 | const sp<MediaCodecBuffer> &buffer, |
| 532 | std::shared_ptr<C2Buffer> *c2buffer, |
| 533 | bool release) override; |
| 534 | |
| 535 | bool expireComponentBuffer( |
| 536 | const std::shared_ptr<C2Buffer> &c2buffer) override; |
| 537 | |
| 538 | void flush() override; |
| 539 | |
| 540 | size_t numClientBuffers() const final; |
| 541 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 542 | protected: |
| 543 | sp<Codec2Buffer> createNewBuffer() override; |
| 544 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 545 | private: |
| 546 | BuffersArrayImpl mImpl; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 547 | std::function<sp<Codec2Buffer>()> mAllocate; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 548 | }; |
| 549 | |
| 550 | class LinearInputBuffers : public InputBuffers { |
| 551 | public: |
| 552 | LinearInputBuffers(const char *componentName, const char *name = "1D-Input") |
| 553 | : InputBuffers(componentName, name), |
| 554 | mImpl(mName) { } |
| 555 | ~LinearInputBuffers() override = default; |
| 556 | |
| 557 | bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override; |
| 558 | |
| 559 | bool releaseBuffer( |
| 560 | const sp<MediaCodecBuffer> &buffer, |
| 561 | std::shared_ptr<C2Buffer> *c2buffer, |
| 562 | bool release) override; |
| 563 | |
| 564 | bool expireComponentBuffer( |
| 565 | const std::shared_ptr<C2Buffer> &c2buffer) override; |
| 566 | |
| 567 | void flush() override; |
| 568 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 569 | std::unique_ptr<InputBuffers> toArrayMode(size_t size) override; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 570 | |
| 571 | size_t numClientBuffers() const final; |
| 572 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 573 | protected: |
| 574 | sp<Codec2Buffer> createNewBuffer() override; |
| 575 | |
| 576 | FlexBuffersImpl mImpl; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 577 | |
| 578 | private: |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 579 | static sp<Codec2Buffer> Alloc( |
| 580 | const std::shared_ptr<C2BlockPool> &pool, const sp<AMessage> &format); |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 581 | }; |
| 582 | |
| 583 | class EncryptedLinearInputBuffers : public LinearInputBuffers { |
| 584 | public: |
| 585 | EncryptedLinearInputBuffers( |
| 586 | bool secure, |
| 587 | const sp<MemoryDealer> &dealer, |
| 588 | const sp<ICrypto> &crypto, |
| 589 | int32_t heapSeqNum, |
| 590 | size_t capacity, |
| 591 | size_t numInputSlots, |
| 592 | const char *componentName, const char *name = "EncryptedInput"); |
| 593 | |
| 594 | ~EncryptedLinearInputBuffers() override = default; |
| 595 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 596 | std::unique_ptr<InputBuffers> toArrayMode(size_t size) override; |
| 597 | |
| 598 | protected: |
| 599 | sp<Codec2Buffer> createNewBuffer() override; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 600 | |
| 601 | private: |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 602 | struct Entry { |
| 603 | std::weak_ptr<C2LinearBlock> block; |
| 604 | sp<IMemory> memory; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 605 | int32_t heapSeqNum; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 606 | }; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 607 | |
| 608 | static sp<Codec2Buffer> Alloc( |
| 609 | const std::shared_ptr<C2BlockPool> &pool, |
| 610 | const sp<AMessage> &format, |
| 611 | C2MemoryUsage usage, |
| 612 | const std::shared_ptr<std::vector<Entry>> &memoryVector); |
| 613 | |
| 614 | C2MemoryUsage mUsage; |
| 615 | sp<MemoryDealer> mDealer; |
| 616 | sp<ICrypto> mCrypto; |
| 617 | std::shared_ptr<std::vector<Entry>> mMemoryVector; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 618 | }; |
| 619 | |
| 620 | class GraphicMetadataInputBuffers : public InputBuffers { |
| 621 | public: |
| 622 | GraphicMetadataInputBuffers(const char *componentName, const char *name = "2D-MetaInput"); |
| 623 | ~GraphicMetadataInputBuffers() override = default; |
| 624 | |
| 625 | bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override; |
| 626 | |
| 627 | bool releaseBuffer( |
| 628 | const sp<MediaCodecBuffer> &buffer, |
| 629 | std::shared_ptr<C2Buffer> *c2buffer, |
| 630 | bool release) override; |
| 631 | |
| 632 | bool expireComponentBuffer( |
| 633 | const std::shared_ptr<C2Buffer> &c2buffer) override; |
| 634 | |
| 635 | void flush() override; |
| 636 | |
| 637 | std::unique_ptr<InputBuffers> toArrayMode(size_t size) final; |
| 638 | |
| 639 | size_t numClientBuffers() const final; |
| 640 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 641 | protected: |
| 642 | sp<Codec2Buffer> createNewBuffer() override; |
| 643 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 644 | private: |
| 645 | FlexBuffersImpl mImpl; |
| 646 | std::shared_ptr<C2AllocatorStore> mStore; |
| 647 | }; |
| 648 | |
| 649 | class GraphicInputBuffers : public InputBuffers { |
| 650 | public: |
| 651 | GraphicInputBuffers( |
| 652 | size_t numInputSlots, const char *componentName, const char *name = "2D-BB-Input"); |
| 653 | ~GraphicInputBuffers() override = default; |
| 654 | |
| 655 | bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override; |
| 656 | |
| 657 | bool releaseBuffer( |
| 658 | const sp<MediaCodecBuffer> &buffer, |
| 659 | std::shared_ptr<C2Buffer> *c2buffer, |
| 660 | bool release) override; |
| 661 | |
| 662 | bool expireComponentBuffer( |
| 663 | const std::shared_ptr<C2Buffer> &c2buffer) override; |
| 664 | |
| 665 | void flush() override; |
| 666 | |
| 667 | std::unique_ptr<InputBuffers> toArrayMode( |
| 668 | 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<LocalBufferPool> mLocalBufferPool; |
| 678 | }; |
| 679 | |
| 680 | class DummyInputBuffers : public InputBuffers { |
| 681 | public: |
| 682 | DummyInputBuffers(const char *componentName, const char *name = "2D-Input") |
| 683 | : InputBuffers(componentName, name) { } |
| 684 | ~DummyInputBuffers() override = default; |
| 685 | |
| 686 | bool requestNewBuffer(size_t *, sp<MediaCodecBuffer> *) override { |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | bool releaseBuffer( |
| 691 | const sp<MediaCodecBuffer> &, std::shared_ptr<C2Buffer> *, bool) override { |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &) override { |
| 696 | return false; |
| 697 | } |
| 698 | void flush() override { |
| 699 | } |
| 700 | |
| 701 | std::unique_ptr<InputBuffers> toArrayMode(size_t) final { |
| 702 | return nullptr; |
| 703 | } |
| 704 | |
| 705 | bool isArrayMode() const final { return true; } |
| 706 | |
| 707 | void getArray(Vector<sp<MediaCodecBuffer>> *array) const final { |
| 708 | array->clear(); |
| 709 | } |
| 710 | |
| 711 | size_t numClientBuffers() const final { |
| 712 | return 0u; |
| 713 | } |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 714 | |
| 715 | protected: |
| 716 | sp<Codec2Buffer> createNewBuffer() override { |
| 717 | return nullptr; |
| 718 | } |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 719 | }; |
| 720 | |
| 721 | class OutputBuffersArray : public OutputBuffers { |
| 722 | public: |
| 723 | OutputBuffersArray(const char *componentName, const char *name = "Output[N]") |
| 724 | : OutputBuffers(componentName, name) { } |
| 725 | ~OutputBuffersArray() override = default; |
| 726 | |
| 727 | /** |
| 728 | * Initialize this object from the non-array state. We keep existing slots |
| 729 | * at the same index, and for empty slots we allocate client buffers with |
| 730 | * the given allocate function. If the number of slots is less than minSize, |
| 731 | * we fill the array to the minimum size. |
| 732 | * |
| 733 | * \param impl[in] existing non-array state |
| 734 | * \param minSize[in] minimum size of the array |
| 735 | * \param allocate[in] allocate function to fill empty slots |
| 736 | */ |
| 737 | void initialize( |
| 738 | const FlexBuffersImpl &impl, |
| 739 | size_t minSize, |
| 740 | std::function<sp<Codec2Buffer>()> allocate); |
| 741 | |
| 742 | bool isArrayMode() const final { return true; } |
| 743 | |
| 744 | std::unique_ptr<OutputBuffers> toArrayMode(size_t) final { |
| 745 | return nullptr; |
| 746 | } |
| 747 | |
| 748 | status_t registerBuffer( |
| 749 | const std::shared_ptr<C2Buffer> &buffer, |
| 750 | size_t *index, |
| 751 | sp<MediaCodecBuffer> *clientBuffer) final; |
| 752 | |
| 753 | status_t registerCsd( |
| 754 | const C2StreamInitDataInfo::output *csd, |
| 755 | size_t *index, |
| 756 | sp<MediaCodecBuffer> *clientBuffer) final; |
| 757 | |
| 758 | bool releaseBuffer( |
| 759 | const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) override; |
| 760 | |
| 761 | void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) override; |
| 762 | |
| 763 | void getArray(Vector<sp<MediaCodecBuffer>> *array) const final; |
| 764 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 765 | size_t numClientBuffers() const final; |
| 766 | |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 767 | /** |
| 768 | * Reallocate the array, filled with buffers with the same size as given |
| 769 | * buffer. |
| 770 | * |
| 771 | * \param c2buffer[in] the reference buffer |
| 772 | */ |
| 773 | void realloc(const std::shared_ptr<C2Buffer> &c2buffer); |
| 774 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 775 | /** |
| 776 | * Grow the array to the new size. It is a programming error to supply |
| 777 | * smaller size as the new size. |
| 778 | * |
| 779 | * \param newSize[in] new size of the array. |
| 780 | */ |
| 781 | void grow(size_t newSize); |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 782 | |
| 783 | private: |
| 784 | BuffersArrayImpl mImpl; |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 785 | std::function<sp<Codec2Buffer>()> mAlloc; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 786 | }; |
| 787 | |
| 788 | class FlexOutputBuffers : public OutputBuffers { |
| 789 | public: |
| 790 | FlexOutputBuffers(const char *componentName, const char *name = "Output[]") |
| 791 | : OutputBuffers(componentName, name), |
| 792 | mImpl(mName) { } |
| 793 | |
| 794 | status_t registerBuffer( |
| 795 | const std::shared_ptr<C2Buffer> &buffer, |
| 796 | size_t *index, |
| 797 | sp<MediaCodecBuffer> *clientBuffer) override; |
| 798 | |
| 799 | status_t registerCsd( |
| 800 | const C2StreamInitDataInfo::output *csd, |
| 801 | size_t *index, |
| 802 | sp<MediaCodecBuffer> *clientBuffer) final; |
| 803 | |
| 804 | bool releaseBuffer( |
| 805 | const sp<MediaCodecBuffer> &buffer, |
| 806 | std::shared_ptr<C2Buffer> *c2buffer) override; |
| 807 | |
| 808 | void flush( |
| 809 | const std::list<std::unique_ptr<C2Work>> &flushedWork) override; |
| 810 | |
| 811 | std::unique_ptr<OutputBuffers> toArrayMode(size_t size) override; |
| 812 | |
| 813 | size_t numClientBuffers() const final; |
| 814 | |
| 815 | /** |
| 816 | * Return an appropriate Codec2Buffer object for the type of buffers. |
| 817 | * |
| 818 | * \param buffer C2Buffer object to wrap. |
| 819 | * |
| 820 | * \return appropriate Codec2Buffer object to wrap |buffer|. |
| 821 | */ |
| 822 | virtual sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) = 0; |
| 823 | |
| 824 | /** |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 825 | * Return a function that allocates an appropriate Codec2Buffer object for |
| 826 | * the type of buffers, to be used as an empty array buffer. The function |
| 827 | * must not refer to this pointer, since it may be used after this object |
| 828 | * destructs. |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 829 | * |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 830 | * \return a function that allocates appropriate Codec2Buffer object, |
| 831 | * which can copy() from C2Buffers. |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 832 | */ |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 833 | virtual std::function<sp<Codec2Buffer>()> getAlloc() = 0; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 834 | |
| 835 | private: |
| 836 | FlexBuffersImpl mImpl; |
| 837 | }; |
| 838 | |
| 839 | class LinearOutputBuffers : public FlexOutputBuffers { |
| 840 | public: |
| 841 | LinearOutputBuffers(const char *componentName, const char *name = "1D-Output") |
| 842 | : FlexOutputBuffers(componentName, name) { } |
| 843 | |
| 844 | void flush( |
| 845 | const std::list<std::unique_ptr<C2Work>> &flushedWork) override; |
| 846 | |
| 847 | sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override; |
| 848 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 849 | std::function<sp<Codec2Buffer>()> getAlloc() override; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 850 | }; |
| 851 | |
| 852 | class GraphicOutputBuffers : public FlexOutputBuffers { |
| 853 | public: |
| 854 | GraphicOutputBuffers(const char *componentName, const char *name = "2D-Output") |
| 855 | : FlexOutputBuffers(componentName, name) { } |
| 856 | |
| 857 | sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override; |
| 858 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 859 | std::function<sp<Codec2Buffer>()> getAlloc() override; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 860 | }; |
| 861 | |
| 862 | class RawGraphicOutputBuffers : public FlexOutputBuffers { |
| 863 | public: |
| 864 | RawGraphicOutputBuffers( |
| 865 | size_t numOutputSlots, const char *componentName, const char *name = "2D-BB-Output"); |
| 866 | ~RawGraphicOutputBuffers() override = default; |
| 867 | |
| 868 | sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override; |
| 869 | |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 870 | std::function<sp<Codec2Buffer>()> getAlloc() override; |
Wonsik Kim | 469c834 | 2019-04-11 16:46:09 -0700 | [diff] [blame] | 871 | |
| 872 | private: |
| 873 | std::shared_ptr<LocalBufferPool> mLocalBufferPool; |
| 874 | }; |
| 875 | |
| 876 | } // namespace android |
| 877 | |
| 878 | #endif // CCODEC_BUFFERS_H_ |