Skip to content
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

Review of frame tensor creation / allocation #269

Open
NicolasHug opened this issue Oct 16, 2024 · 0 comments
Open

Review of frame tensor creation / allocation #269

NicolasHug opened this issue Oct 16, 2024 · 0 comments

Comments

@NicolasHug
Copy link
Member

NicolasHug commented Oct 16, 2024

We create/allocate output frame tensors in different places. In particular, we determine the height and width of the output tensor from different sources:

  • For batch APIs (CPU and GPU): from the stream metadata, which itself comes from the CodecContext
  • For single frame APIs:
    • CPU: swscale and filtergraph: from the AVFrame
    • GPU: from the CodecContext

The info from the metadata / CodecContext are available as soon as we add a stream, e.g. right after we instantiate a Python VideoDecoder. The AVFrame is only available once we have decoded the frame with ffmpeg (this is the "raw output").

The source of truth really is the AVFrame. CondecContext may be wrong, and in particular we now know that some streams may have variable height and width #312.


Details:

  • For batch APIs:

VideoDecoder::BatchDecodedOutput::BatchDecodedOutput(
int64_t numFrames,
const VideoStreamDecoderOptions& options,
const StreamMetadata& metadata)
: ptsSeconds(torch::empty({numFrames}, {torch::kFloat64})),
durationSeconds(torch::empty({numFrames}, {torch::kFloat64})) {
if (options.dimensionOrder == "NHWC") {
frames = torch::empty(
{numFrames,
options.height.value_or(*metadata.height),
options.width.value_or(*metadata.width),
3},
{torch::kUInt8});
} else if (options.dimensionOrder == "NCHW") {
frames = torch::empty(
{numFrames,
3,
options.height.value_or(*metadata.height),
options.width.value_or(*metadata.width)},
torch::TensorOptions()
.memory_format(torch::MemoryFormat::ChannelsLast)
.dtype({torch::kUInt8}));
} else {

  • For single frames APIs:

    int width = streamInfo.options.width.value_or(frame->width);
    int height = streamInfo.options.height.value_or(frame->height);
    torch::Tensor tensor = torch::empty(
    {height, width, 3}, torch::TensorOptions().dtype({torch::kUInt8}));

    and
    std::vector<int64_t> shape = {filteredFrame->height, filteredFrame->width, 3};
    std::vector<int64_t> strides = {filteredFrame->linesize[0], 3, 1};
    AVFrame* filteredFramePtr = filteredFrame.release();
    auto deleter = [filteredFramePtr](void*) {
    UniqueAVFrame frameToDelete(filteredFramePtr);
    };
    torch::Tensor tensor = torch::from_blob(
    filteredFramePtr->data[0], shape, strides, deleter, {torch::kUInt8});

  • For CUDA APIs:

    int width = options.width.value_or(codecContext->width);
    int height = options.height.value_or(codecContext->height);
    NppiSize oSizeROI = {width, height};
    Npp8u* input[2] = {src->data[0], src->data[1]};
    torch::Tensor& dst = output.frame;
    dst = allocateDeviceTensor({height, width, 3}, options.device);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant