|
| 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/_core/DeviceInterface.h" |
| 10 | + |
| 11 | +namespace facebook::torchcodec { |
| 12 | + |
| 13 | +class CpuDeviceInterface : public DeviceInterface { |
| 14 | + public: |
| 15 | + CpuDeviceInterface(const torch::Device& device, const AVRational& timeBase); |
| 16 | + |
| 17 | + virtual ~CpuDeviceInterface() {} |
| 18 | + |
| 19 | + std::optional<const AVCodec*> findCodec( |
| 20 | + [[maybe_unused]] const AVCodecID& codecId) override { |
| 21 | + return std::nullopt; |
| 22 | + } |
| 23 | + |
| 24 | + void initializeContext( |
| 25 | + [[maybe_unused]] AVCodecContext* codecContext) override {} |
| 26 | + |
| 27 | + void convertAVFrameToFrameOutput( |
| 28 | + const VideoStreamOptions& videoStreamOptions, |
| 29 | + UniqueAVFrame& avFrame, |
| 30 | + FrameOutput& frameOutput, |
| 31 | + std::optional<torch::Tensor> preAllocatedOutputTensor = |
| 32 | + std::nullopt) override; |
| 33 | + |
| 34 | + private: |
| 35 | + int convertAVFrameToTensorUsingSwsScale( |
| 36 | + const UniqueAVFrame& avFrame, |
| 37 | + torch::Tensor& outputTensor); |
| 38 | + |
| 39 | + torch::Tensor convertAVFrameToTensorUsingFilterGraph( |
| 40 | + const UniqueAVFrame& avFrame); |
| 41 | + |
| 42 | + struct FilterGraphContext { |
| 43 | + UniqueAVFilterGraph filterGraph; |
| 44 | + AVFilterContext* sourceContext = nullptr; |
| 45 | + AVFilterContext* sinkContext = nullptr; |
| 46 | + }; |
| 47 | + |
| 48 | + struct DecodedFrameContext { |
| 49 | + int decodedWidth; |
| 50 | + int decodedHeight; |
| 51 | + AVPixelFormat decodedFormat; |
| 52 | + AVRational decodedAspectRatio; |
| 53 | + int expectedWidth; |
| 54 | + int expectedHeight; |
| 55 | + bool operator==(const DecodedFrameContext&); |
| 56 | + bool operator!=(const DecodedFrameContext&); |
| 57 | + }; |
| 58 | + |
| 59 | + void createSwsContext( |
| 60 | + const DecodedFrameContext& frameContext, |
| 61 | + const enum AVColorSpace colorspace); |
| 62 | + |
| 63 | + void createFilterGraph( |
| 64 | + const DecodedFrameContext& frameContext, |
| 65 | + const VideoStreamOptions& videoStreamOptions); |
| 66 | + |
| 67 | + // color-conversion fields. Only one of FilterGraphContext and |
| 68 | + // UniqueSwsContext should be non-null. |
| 69 | + FilterGraphContext filterGraphContext_; |
| 70 | + UniqueSwsContext swsContext_; |
| 71 | + |
| 72 | + // Used to know whether a new FilterGraphContext or UniqueSwsContext should |
| 73 | + // be created before decoding a new frame. |
| 74 | + DecodedFrameContext prevFrameContext_; |
| 75 | +}; |
| 76 | + |
| 77 | +} // namespace facebook::torchcodec |
0 commit comments