/**
 * @file src/cbs.h
 * @brief Declarations for FFmpeg Coded Bitstream API.
 */
#pragma once

// local includes
#include "utility.h"

struct AVPacket;
struct AVCodecContext;

namespace cbs {

  /**
   * @brief Original and rewritten bytes for one codec NAL unit.
   */
  struct nal_t {
    util::buffer_t<std::uint8_t> _new;  ///< Replacement bytes generated by Sunshine.
    util::buffer_t<std::uint8_t> old;  ///< Original bytes copied from the source packet.
  };

  /**
   * @brief HEVC parameter sets before and after VUI rewriting.
   */
  struct hevc_t {
    nal_t vps;  ///< Video parameter set bytes.
    nal_t sps;  ///< Sequence parameter set bytes.
  };

  /**
   * @brief H.264 parameter set before and after VUI rewriting.
   */
  struct h264_t {
    nal_t sps;  ///< Sequence parameter set bytes.
  };

  /**
   * @brief Build HEVC replacement parameter sets for a packet.
   *
   * @param ctx FFmpeg codec context that provides colorimetry and reference-frame metadata.
   * @param packet Encoded packet containing the original VPS and SPS.
   * @return Original and rewritten VPS/SPS bytes.
   */
  hevc_t make_sps_hevc(const AVCodecContext *ctx, const AVPacket *packet);
  /**
   * @brief Build an H.264 replacement SPS for a packet.
   *
   * @param ctx FFmpeg codec context that provides colorimetry and reference-frame metadata.
   * @param packet Encoded packet containing the original SPS.
   * @return Original and rewritten SPS bytes.
   */
  h264_t make_sps_h264(const AVCodecContext *ctx, const AVPacket *packet);

  /**
   * @brief Check whether an encoded H.264 or HEVC packet contains active SPS VUI metadata.
   *
   * @param packet Encoded packet to parse with FFmpeg's coded bitstream reader.
   * @param codec_id FFmpeg codec identifier; expected to be AV_CODEC_ID_H264 or AV_CODEC_ID_H265.
   * @return `true` when the packet's active SPS advertises VUI parameters.
   */
  bool validate_sps(const AVPacket *packet, int codec_id);
}  // namespace cbs
