|
| 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 | +#include "src/torchcodec/_core/AVIOTensorContext.h" |
| 8 | +#include <torch/types.h> |
| 9 | + |
| 10 | +namespace facebook::torchcodec { |
| 11 | + |
| 12 | +namespace { |
| 13 | + |
| 14 | +constexpr int64_t INITIAL_TENSOR_SIZE = 10'000'000; // 10 MB |
| 15 | +constexpr int64_t MAX_TENSOR_SIZE = 320'000'000; // 320 MB |
| 16 | + |
| 17 | +// The signature of this function is defined by FFMPEG. |
| 18 | +int read(void* opaque, uint8_t* buf, int buf_size) { |
| 19 | + auto tensorContext = static_cast<detail::TensorContext*>(opaque); |
| 20 | + TORCH_CHECK( |
| 21 | + tensorContext->current <= tensorContext->data.numel(), |
| 22 | + "Tried to read outside of the buffer: current=", |
| 23 | + tensorContext->current, |
| 24 | + ", size=", |
| 25 | + tensorContext->data.numel()); |
| 26 | + |
| 27 | + int64_t numBytesRead = std::min( |
| 28 | + static_cast<int64_t>(buf_size), |
| 29 | + tensorContext->data.numel() - tensorContext->current); |
| 30 | + |
| 31 | + TORCH_CHECK( |
| 32 | + numBytesRead >= 0, |
| 33 | + "Tried to read negative bytes: numBytesRead=", |
| 34 | + numBytesRead, |
| 35 | + ", size=", |
| 36 | + tensorContext->data.numel(), |
| 37 | + ", current=", |
| 38 | + tensorContext->current); |
| 39 | + |
| 40 | + if (numBytesRead == 0) { |
| 41 | + return AVERROR_EOF; |
| 42 | + } |
| 43 | + |
| 44 | + std::memcpy( |
| 45 | + buf, |
| 46 | + tensorContext->data.data_ptr<uint8_t>() + tensorContext->current, |
| 47 | + numBytesRead); |
| 48 | + tensorContext->current += numBytesRead; |
| 49 | + return numBytesRead; |
| 50 | +} |
| 51 | + |
| 52 | +// The signature of this function is defined by FFMPEG. |
| 53 | +int write(void* opaque, const uint8_t* buf, int buf_size) { |
| 54 | + auto tensorContext = static_cast<detail::TensorContext*>(opaque); |
| 55 | + |
| 56 | + int64_t bufSize = static_cast<int64_t>(buf_size); |
| 57 | + if (tensorContext->current + bufSize > tensorContext->data.numel()) { |
| 58 | + TORCH_CHECK( |
| 59 | + tensorContext->data.numel() * 2 <= MAX_TENSOR_SIZE, |
| 60 | + "We tried to allocate an output encoded tensor larger than ", |
| 61 | + MAX_TENSOR_SIZE, |
| 62 | + " bytes. If you think this should be supported, please report."); |
| 63 | + |
| 64 | + // We double the size of the outpout tensor. Calling cat() may not be the |
| 65 | + // most efficient, but it's simple. |
| 66 | + tensorContext->data = |
| 67 | + torch::cat({tensorContext->data, tensorContext->data}); |
| 68 | + } |
| 69 | + |
| 70 | + TORCH_CHECK( |
| 71 | + tensorContext->current + bufSize <= tensorContext->data.numel(), |
| 72 | + "Re-allocation of the output tensor didn't work. ", |
| 73 | + "This should not happen, please report on TorchCodec bug tracker"); |
| 74 | + |
| 75 | + uint8_t* outputTensorData = tensorContext->data.data_ptr<uint8_t>(); |
| 76 | + std::memcpy(outputTensorData + tensorContext->current, buf, bufSize); |
| 77 | + tensorContext->current += bufSize; |
| 78 | + return buf_size; |
| 79 | +} |
| 80 | + |
| 81 | +// The signature of this function is defined by FFMPEG. |
| 82 | +int64_t seek(void* opaque, int64_t offset, int whence) { |
| 83 | + auto tensorContext = static_cast<detail::TensorContext*>(opaque); |
| 84 | + int64_t ret = -1; |
| 85 | + |
| 86 | + switch (whence) { |
| 87 | + case AVSEEK_SIZE: |
| 88 | + ret = tensorContext->data.numel(); |
| 89 | + break; |
| 90 | + case SEEK_SET: |
| 91 | + tensorContext->current = offset; |
| 92 | + ret = offset; |
| 93 | + break; |
| 94 | + default: |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + return ret; |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace |
| 102 | + |
| 103 | +AVIOFromTensorContext::AVIOFromTensorContext(torch::Tensor data) |
| 104 | + : tensorContext_{data, 0} { |
| 105 | + TORCH_CHECK(data.numel() > 0, "data must not be empty"); |
| 106 | + TORCH_CHECK(data.is_contiguous(), "data must be contiguous"); |
| 107 | + TORCH_CHECK(data.scalar_type() == torch::kUInt8, "data must be kUInt8"); |
| 108 | + createAVIOContext(&read, nullptr, &seek, &tensorContext_); |
| 109 | +} |
| 110 | + |
| 111 | +AVIOToTensorContext::AVIOToTensorContext() |
| 112 | + : tensorContext_{torch::empty({INITIAL_TENSOR_SIZE}, {torch::kUInt8}), 0} { |
| 113 | + createAVIOContext(nullptr, &write, &seek, &tensorContext_); |
| 114 | +} |
| 115 | + |
| 116 | +torch::Tensor AVIOToTensorContext::getOutputTensor() { |
| 117 | + return tensorContext_.data.narrow( |
| 118 | + /*dim=*/0, /*start=*/0, /*length=*/tensorContext_.current); |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace facebook::torchcodec |
0 commit comments