// SPDX-License-Identifier: GPL-2.0+ AND LGPL-2.0+ AND LGPL-2.1+

#ifndef _GST_MVB_ALLOCATOR_H_
#define _GST_MVB_ALLOCATOR_H_

#include <gst/gst.h>
#include <gst/mvb/mvb-prelude.h>

#include <gst/gstallocator.h>
#include <gst/allocators/allocators.h>
#include <gst/video/video-format.h>
#include <gst/video/video-info.h>
#include <gst/gstmemory.h>

#include "mvb_heap.h"
#include "mvb_format_utils.h"

G_BEGIN_DECLS

#define GST_TYPE_MVB_ALLOCATOR (gst_mvb_allocator_get_type())
#define GST_IS_MVB_ALLOCATOR(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_MVB_ALLOCATOR))
#define GST_IS_MVB_ALLOCATOR_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass),  GST_TYPE_MVB_ALLOCATOR))
#define GST_MVB_ALLOCATOR_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj),  GST_TYPE_MVB_ALLOCATOR, GstMvbAllocatorClass))
#define GST_MVB_ALLOCATOR(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_MVB_ALLOCATOR, GstMvbAllocator))
#define GST_MVB_ALLOCATOR_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass),  GST_TYPE_MVB_ALLOCATOR, GstMvbAllocatorClass))
#define GST_MVB_ALLOCATOR_CAST(obj)            ((GstMvbAllocator *)(obj))

#define GST_MVB_ALLOCATOR_NAME      "Mvb"
#define GST_CAPS_FEATURE_MEMORY_MVB "memory:" GST_MVB_ALLOCATOR_NAME

GQuark  gst_mvb_memory_quark (void);
#define GST_MVB_MEMORY_QUARK gst_mvb_memory_quark ()


typedef struct _GstMvbAllocator        GstMvbAllocator;
typedef struct _GstMvbAllocatorClass   GstMvbAllocatorClass;
typedef struct _GstMvbAllocatorPrivate GstMvbAllocatorPrivate;

typedef struct _GstMvbMemory           GstMvbMemory;
typedef struct _GstMvbMemoryGroup      GstMvbMemoryGroup;

struct _GstMvbMemory
{
  GstMemory mem;

  gpointer plane_data[MVB_FRAME_PLANE_NUM_MAX];
  gint     plane_fd[MVB_FRAME_PLANE_NUM_MAX];
  gint     plane_size[MVB_FRAME_PLANE_NUM_MAX];
  gint     plane_num;

  struct mvb_frame_t* mvb_frame;

  GstMvbMemoryGroup* mem_group;
};

struct _GstMvbMemoryGroup
{
  gint       mem_num;  // plane number
  GstMemory* mem[MVB_FRAME_PLANE_NUM_MAX];  // dma_memory
  gint       mem_allocated;

  GstMvbMemory *mvb_mem;
};


struct _GstMvbAllocator
{
  GstDmaBufAllocator parent;

  GstMvbAllocatorPrivate *priv;
};



struct _GstMvbAllocatorClass
{
  GstDmaBufAllocatorClass parent_class;
};


GST_MVB_API
GType gst_mvb_allocator_get_type(void);

GST_MVB_API
GstAllocator *gst_mvb_allocator_new (void);

GST_MVB_API
GstMvbMemoryGroup* create_gst_mvb_memory_group (GstMvbAllocator* allocator,
    GstVideoInfo* info, guint32 usage);

G_END_DECLS

#endif
