// 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 "FFMPEGCommon.h"
#include "StreamOptions.h"

namespace facebook::torchcodec {

struct FiltersConfig {
  int inputWidth = 0;
  int inputHeight = 0;
  AVPixelFormat inputFormat = AV_PIX_FMT_NONE;
  AVRational inputAspectRatio = {0, 0};
  int outputWidth = 0;
  int outputHeight = 0;
  AVPixelFormat outputFormat = AV_PIX_FMT_NONE;
  std::string filtergraphStr;
  AVRational timeBase = {0, 0};
  UniqueAVBufferRef hwFramesCtx;

  FiltersConfig() = default;
  FiltersConfig(FiltersConfig&&) = default;
  FiltersConfig& operator=(FiltersConfig&&) = default;
  FiltersConfig(
      int inputWidth,
      int inputHeight,
      AVPixelFormat inputFormat,
      AVRational inputAspectRatio,
      int outputWidth,
      int outputHeight,
      AVPixelFormat outputFormat,
      const std::string& filtergraphStr,
      AVRational timeBase,
      AVBufferRef* hwFramesCtx = nullptr);

  bool operator==(const FiltersConfig&) const;
  bool operator!=(const FiltersConfig&) const;
};

class FilterGraph {
 public:
  FilterGraph(
      const FiltersConfig& filtersConfig,
      const VideoStreamOptions& videoStreamOptions);

  UniqueAVFrame convert(const UniqueAVFrame& avFrame);

 private:
  UniqueAVFilterGraph filterGraph_;
  AVFilterContext* sourceContext_ = nullptr;
  AVFilterContext* sinkContext_ = nullptr;
};

} // namespace facebook::torchcodec
