|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This source code is licensed under the BSD-style license found in the |
| 5 | +// LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include "src/torchcodec/decoders/_core/FFMPEGCommon.h" |
| 10 | + |
| 11 | +namespace facebook::torchcodec { |
| 12 | + |
| 13 | +// These signatures are defined by FFmpeg. |
| 14 | +using AVIOReadFunction = int (*)(void*, uint8_t*, int); |
| 15 | +using AVIOSeekFunction = int64_t (*)(void*, int64_t, int); |
| 16 | + |
| 17 | +// The AVIOContextHolder serves several purposes: |
| 18 | +// |
| 19 | +// 1. It is a smart pointer for the AVIOContext. It has the logic to create |
| 20 | +// a new AVIOContext and will appropriately free the AVIOContext when it |
| 21 | +// goes out of scope. Note that this requires more than just the having a |
| 22 | +// UniqueAVIOContext, as the AVIOContext points to a buffer which must be |
| 23 | +// freed. |
| 24 | +// 2. It is a base class for AVIOContext specializations. When specializing a |
| 25 | +// AVIOContext, we need to provide four things: |
| 26 | +// 1. A read callback function. |
| 27 | +// 2. A seek callback function. |
| 28 | +// 3. A write callback function. (Not supported yet; it's for encoding.) |
| 29 | +// 4. A pointer to some context object that has the same lifetime as the |
| 30 | +// AVIOContext itself. This context object holds the custom state that |
| 31 | +// tracks the custom behavior of reading, seeking and writing. It is |
| 32 | +// provided upon AVIOContext creation and to the read, seek and |
| 33 | +// write callback functions. |
| 34 | +// While it's not required, it is natural for the derived classes to make |
| 35 | +// all of the above members. Base classes need to call |
| 36 | +// createAVIOContext(), ideally in there constructor. |
| 37 | +// 3. A generic handle for those that just need to manage having access to an |
| 38 | +// AVIOContext, but aren't necessarily concerned with how it was customized. |
| 39 | +class AVIOContextHolder { |
| 40 | + public: |
| 41 | + virtual ~AVIOContextHolder(); |
| 42 | + AVIOContext* getAVIOContext(); |
| 43 | + |
| 44 | + protected: |
| 45 | + // Make constructor protected to prevent anyone from constructing |
| 46 | + // an AVIOContextHolder without deriving it. (Ordinarily this would be |
| 47 | + // enforced by having a pure virtual methods, but we don't have any.) |
| 48 | + AVIOContextHolder() = default; |
| 49 | + |
| 50 | + // Deriving classes should call this function in their constructor. |
| 51 | + void createAVIOContext( |
| 52 | + AVIOReadFunction read, |
| 53 | + AVIOSeekFunction seek, |
| 54 | + void* heldData, |
| 55 | + int bufferSize = defaultBufferSize); |
| 56 | + |
| 57 | + private: |
| 58 | + UniqueAVIOContext avioContext_; |
| 59 | + |
| 60 | + // Defaults to 64 KB |
| 61 | + static const int defaultBufferSize = 64 * 1024; |
| 62 | +}; |
| 63 | + |
| 64 | +} // namespace facebook::torchcodec |
0 commit comments