|
11 | 11 |
|
12 | 12 | namespace facebook::torchcodec { |
13 | 13 |
|
14 | | -// The AVIOContextHolder serves several purposes: |
| 14 | +// The AVIOContextHolder is a base class for I/O backends. It serves as: |
15 | 15 | // |
16 | | -// 1. It is a smart pointer for the AVIOContext. It has the logic to create |
17 | | -// a new AVIOContext and will appropriately free the AVIOContext when it |
18 | | -// goes out of scope. Note that this requires more than just having a |
19 | | -// UniqueAVIOContext, as the AVIOContext points to a buffer which must be |
20 | | -// freed. |
21 | | -// 2. It is a base class for AVIOContext specializations. When specializing a |
22 | | -// AVIOContext, we need to provide four things: |
23 | | -// 1. A read callback function, for decoding. |
24 | | -// 2. A seek callback function, for decoding and encoding. |
25 | | -// 3. A write callback function, for encoding. |
26 | | -// 4. A pointer to some context object that has the same lifetime as the |
27 | | -// AVIOContext itself. This context object holds the custom state that |
28 | | -// tracks the custom behavior of reading, seeking and writing. It is |
29 | | -// provided upon AVIOContext creation and to the read, seek and |
30 | | -// write callback functions. |
31 | | -// The callback functions do not need to be members of the derived class, |
32 | | -// but the derived class must have access to them. The context object must |
33 | | -// be a member of the derived class. Derived classes need to call |
34 | | -// createAVIOContext(), ideally in their constructor. |
35 | | -// 3. A generic handle for those that just need to manage having access to an |
36 | | -// AVIOContext, but aren't necessarily concerned with how it was customized: |
37 | | -// typically, the SingleStreamDecoder. |
| 16 | +// 1. A generic I/O interface: derived classes override virtual methods |
| 17 | +// (read, write, seek, getSize) to implement their specific I/O. |
| 18 | +// These can be called directly by consumers like WavDecoder. |
| 19 | +// |
| 20 | +// 2. An FFmpeg AVIO adapter: calling createAVIOContext() sets up an |
| 21 | +// FFmpeg AVIOContext whose callbacks automatically delegate to the |
| 22 | +// virtual methods. This is used by SingleStreamDecoder and Encoder. |
| 23 | +// |
| 24 | +// 3. A smart pointer for the AVIOContext, freeing it and its buffer |
| 25 | +// on destruction. |
38 | 26 | class FORCE_PUBLIC_VISIBILITY AVIOContextHolder { |
39 | 27 | public: |
40 | 28 | virtual ~AVIOContextHolder(); |
41 | 29 | AVIOContext* getAVIOContext(); |
42 | 30 |
|
43 | | - // Generic I/O primitives used by consumers that don't go through |
44 | | - // FFmpeg's AVIO layer (e.g. WavDecoder). Derived classes override |
45 | | - // the ones they support. |
46 | 31 | virtual int read(uint8_t* buf, int size); |
| 32 | + virtual int write(const uint8_t* buf, int size); |
47 | 33 | virtual int64_t seek(int64_t offset, int whence); |
48 | 34 | virtual int64_t getSize(); |
49 | 35 |
|
50 | 36 | protected: |
51 | | - // Make constructor protected to prevent anyone from constructing |
52 | | - // an AVIOContextHolder without deriving it. (Ordinarily this would be |
53 | | - // enforced by having a pure virtual methods, but we don't have any.) |
54 | 37 | AVIOContextHolder() = default; |
55 | 38 |
|
56 | | - // Deriving classes should call this function in their constructor. |
57 | | - void createAVIOContext( |
58 | | - AVIOReadFunction read, |
59 | | - AVIOWriteFunction write, |
60 | | - AVIOSeekFunction seek, |
61 | | - void* heldData, |
62 | | - bool isForWriting, |
63 | | - int bufferSize = defaultBufferSize); |
| 39 | + // Sets up an FFmpeg AVIOContext whose callbacks delegate to the |
| 40 | + // virtual methods above. Derived classes that need FFmpeg AVIO |
| 41 | + // should call this in their constructor. |
| 42 | + void createAVIOContext(bool isForWriting, int bufferSize = defaultBufferSize); |
64 | 43 |
|
65 | 44 | private: |
| 45 | + static int readCallback(void* opaque, uint8_t* buf, int buf_size); |
| 46 | + static int writeCallback(void* opaque, const uint8_t* buf, int buf_size); |
| 47 | + static int64_t seekCallback(void* opaque, int64_t offset, int whence); |
| 48 | + |
66 | 49 | UniqueAVIOContext avioContext_; |
67 | 50 |
|
68 | 51 | // Defaults to 64 KB |
|
0 commit comments