-
Notifications
You must be signed in to change notification settings - Fork 42
Turn BytesContext into FromTensorContext #721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fe61d91
Add test for bytes constructor and ownership
scotts 709686e
Turn BytesContext into FromTensorContext
scotts 8217a8b
Refactor file names
scotts 087b3be
Fix C++ test
scotts 2635230
Lint
scotts 10a5ddc
Actually fix C++ test
scotts 9581ae5
Note about other test failures
scotts e8c7439
Remove stray comment
scotts dba46e5
Apply review changes
scotts e1516b5
Merge branch 'main' of github.com:pytorch/torchcodec into own_bytes
scotts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#include "src/torchcodec/_core/AVIOTensorContext.h" | ||
#include <torch/types.h> | ||
|
||
namespace facebook::torchcodec { | ||
|
||
namespace { | ||
|
||
constexpr int64_t INITIAL_TENSOR_SIZE = 10'000'000; // 10 MB | ||
constexpr int64_t MAX_TENSOR_SIZE = 320'000'000; // 320 MB | ||
|
||
// The signature of this function is defined by FFMPEG. | ||
int read(void* opaque, uint8_t* buf, int buf_size) { | ||
auto tensorContext = static_cast<detail::TensorContext*>(opaque); | ||
TORCH_CHECK( | ||
tensorContext->current <= tensorContext->data.numel(), | ||
"Tried to read outside of the buffer: current=", | ||
tensorContext->current, | ||
", size=", | ||
tensorContext->data.numel()); | ||
|
||
int64_t numBytesRead = std::min( | ||
static_cast<int64_t>(buf_size), | ||
tensorContext->data.numel() - tensorContext->current); | ||
|
||
TORCH_CHECK( | ||
numBytesRead >= 0, | ||
"Tried to read negative bytes: numBytesRead=", | ||
numBytesRead, | ||
", size=", | ||
tensorContext->data.numel(), | ||
", current=", | ||
tensorContext->current); | ||
|
||
if (numBytesRead == 0) { | ||
return AVERROR_EOF; | ||
} | ||
|
||
std::memcpy( | ||
buf, | ||
tensorContext->data.data_ptr<uint8_t>() + tensorContext->current, | ||
numBytesRead); | ||
tensorContext->current += numBytesRead; | ||
return numBytesRead; | ||
} | ||
|
||
// The signature of this function is defined by FFMPEG. | ||
int write(void* opaque, const uint8_t* buf, int buf_size) { | ||
auto tensorContext = static_cast<detail::TensorContext*>(opaque); | ||
|
||
int64_t bufSize = static_cast<int64_t>(buf_size); | ||
if (tensorContext->current + bufSize > tensorContext->data.numel()) { | ||
TORCH_CHECK( | ||
tensorContext->data.numel() * 2 <= MAX_TENSOR_SIZE, | ||
"We tried to allocate an output encoded tensor larger than ", | ||
MAX_TENSOR_SIZE, | ||
" bytes. If you think this should be supported, please report."); | ||
|
||
// We double the size of the outpout tensor. Calling cat() may not be the | ||
// most efficient, but it's simple. | ||
tensorContext->data = | ||
torch::cat({tensorContext->data, tensorContext->data}); | ||
} | ||
|
||
TORCH_CHECK( | ||
tensorContext->current + bufSize <= tensorContext->data.numel(), | ||
"Re-allocation of the output tensor didn't work. ", | ||
"This should not happen, please report on TorchCodec bug tracker"); | ||
|
||
uint8_t* outputTensorData = tensorContext->data.data_ptr<uint8_t>(); | ||
std::memcpy(outputTensorData + tensorContext->current, buf, bufSize); | ||
tensorContext->current += bufSize; | ||
return buf_size; | ||
} | ||
|
||
// The signature of this function is defined by FFMPEG. | ||
int64_t seek(void* opaque, int64_t offset, int whence) { | ||
auto tensorContext = static_cast<detail::TensorContext*>(opaque); | ||
int64_t ret = -1; | ||
|
||
switch (whence) { | ||
case AVSEEK_SIZE: | ||
ret = tensorContext->data.numel(); | ||
break; | ||
case SEEK_SET: | ||
tensorContext->current = offset; | ||
ret = offset; | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
} // namespace | ||
|
||
AVIOFromTensorContext::AVIOFromTensorContext(torch::Tensor data) | ||
: tensorContext_{data, 0} { | ||
TORCH_CHECK(data.numel() > 0, "data must not be empty"); | ||
TORCH_CHECK(data.is_contiguous(), "data must be contiguous"); | ||
TORCH_CHECK(data.scalar_type() == torch::kUInt8, "data must be kUInt8"); | ||
createAVIOContext(&read, nullptr, &seek, &tensorContext_); | ||
} | ||
|
||
AVIOToTensorContext::AVIOToTensorContext() | ||
: tensorContext_{torch::empty({INITIAL_TENSOR_SIZE}, {torch::kUInt8}), 0} { | ||
createAVIOContext(nullptr, &write, &seek, &tensorContext_); | ||
} | ||
|
||
torch::Tensor AVIOToTensorContext::getOutputTensor() { | ||
return tensorContext_.data.narrow( | ||
/*dim=*/0, /*start=*/0, /*length=*/tensorContext_.current); | ||
} | ||
|
||
} // namespace facebook::torchcodec |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#include <torch/types.h> | ||
#include "src/torchcodec/_core/AVIOContextHolder.h" | ||
|
||
namespace facebook::torchcodec { | ||
|
||
namespace detail { | ||
|
||
struct TensorContext { | ||
torch::Tensor data; | ||
int64_t current; | ||
}; | ||
|
||
scotts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} // namespace detail | ||
|
||
// For Decoding: enables users to pass in the entire video or audio as bytes. | ||
// Our read and seek functions then traverse the bytes in memory. | ||
class AVIOFromTensorContext : public AVIOContextHolder { | ||
public: | ||
explicit AVIOFromTensorContext(torch::Tensor data); | ||
|
||
private: | ||
detail::TensorContext tensorContext_; | ||
}; | ||
|
||
// For Encoding: used to encode into an output uint8 (bytes) tensor. | ||
class AVIOToTensorContext : public AVIOContextHolder { | ||
public: | ||
explicit AVIOToTensorContext(); | ||
torch::Tensor getOutputTensor(); | ||
|
||
private: | ||
detail::TensorContext tensorContext_; | ||
}; | ||
|
||
} // namespace facebook::torchcodec |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.