/*
 * HEIF VVC codec.
 * Copyright (c) 2023 Dirk Farin <dirk.farin@gmail.com>
 *
 * This file is part of libheif.
 *
 * libheif is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * libheif is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libheif.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef LIBHEIF_VVC_BOXES_H
#define LIBHEIF_VVC_BOXES_H

#include "box.h"
#include <string>
#include <vector>
#include "image-items/image_item.h"
#include <memory>
#include "sequences/seq_boxes.h"


class Box_vvcC : public FullBox
{
public:
  Box_vvcC()
  {
    set_short_type(fourcc("vvcC"));
  }

  bool is_essential() const override { return true; }

  struct VvcPTLRecord {
    uint8_t num_bytes_constraint_info = 0; // 6 bits
    uint8_t general_profile_idc = 0; // 7 bits
    uint8_t general_tier_flag = 0; // 1 bit
    uint8_t general_level_idc = 0; // 8 bits
    uint8_t ptl_frame_only_constraint_flag = 0; // 1 bit
    uint8_t ptl_multi_layer_enabled_flag = 0; // 1 bit
    std::vector<uint8_t> general_constraint_info;

    std::vector<bool> ptl_sublayer_level_present_flag; // TODO: should we save this here or can we simply derive it on the fly?

    std::vector<uint8_t> sublayer_level_idc;
    std::vector<uint32_t> general_sub_profile_idc;
  };

  struct configuration
  {
    uint8_t LengthSizeMinusOne = 3;  // 0,1,3   default: 4 bytes for NAL unit lengths
    bool ptl_present_flag = true;

    // only if PTL present
    uint16_t ols_idx = 0; // 9 bits
    uint8_t num_sublayers = 0; // 3 bits
    uint8_t constant_frame_rate = 0; // 2 bits
    uint8_t chroma_format_idc = 0; // 2 bits
    uint8_t bit_depth_minus8 = 0; // 3 bits
    struct VvcPTLRecord native_ptl{};
    uint16_t max_picture_width = 0;
    uint16_t max_picture_height = 0;
    uint16_t avg_frame_rate = 0;
  };


  std::string dump(Indent&) const override;

  bool get_headers(std::vector<uint8_t>* dest) const;

  void set_configuration(const configuration& config) { m_configuration = config; }

  const configuration& get_configuration() const { return m_configuration; }

  configuration& get_configuration() { return m_configuration; }

  void append_nal_data(const std::vector<uint8_t>& nal);
  void append_nal_data(const uint8_t* data, size_t size);

  // Returns the bytes of the first NAL unit of the requested type stored in
  // the configuration record, or nullptr if none is present.
  const std::vector<uint8_t>* get_first_nal_of_type(uint8_t nal_type) const;

  Error write(StreamWriter& writer) const override;

protected:
  Error parse(BitstreamRange& range, const heif_security_limits* limits) override;

private:
    struct NalArray
    {
      bool m_array_completeness;
      uint8_t m_NAL_unit_type;

      std::vector<std::vector<uint8_t> > m_nal_units; // only one NAL item for DCI and OPI
    };

  configuration m_configuration;
  std::vector<NalArray> m_nal_array;
};


class Box_vvc1 : public Box_VisualSampleEntry
{
public:
  Box_vvc1()
  {
    set_short_type(fourcc("vvc1"));
  }
};


struct ImageSize;

// Parses a VVC SPS NAL unit. *width / *height return the post-conformance-
// window cropping (display) dimensions. If non-null, *coded_size receives the
// pre-cropping dimensions actually allocated by the decoder.
Error parse_sps_for_vvcC_configuration(const uint8_t* sps, size_t size,
                                       Box_vvcC::configuration* inout_config,
                                       uint32_t* width, uint32_t* height,
                                       ImageSize* coded_size = nullptr);

#endif // LIBHEIF_VVC_BOXES_H
