/* GStreamer
 *
 * Copyright (C) 2014 Matthew Waters <matthew@centricular.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <gst/check/gstcheck.h>

#include <gst/gl/gl.h>
#include <gst/gl/gstglfuncs.h>

#ifdef __APPLE__
#include <TargetConditionals.h>
#endif

#if defined(__APPLE__) && TARGET_OS_OSX
#include <gst/gstmacos.h>
#endif

#include <stdio.h>

static GstGLDisplay *display;
static GstGLContext *context;
static GstGLWindow *window;
static GstGLColorConvert *convert;

typedef struct _TestFrame
{
  gint width;
  gint height;
  GstVideoFormat v_format;
  gchar *data[GST_VIDEO_MAX_PLANES];
} TestFrame;

#define IGNORE_MAGIC 0x05

static const gchar rgba_reorder_data[] = { 0x49, 0x24, 0x72, 0xff };
static const gchar rgbx_reorder_data[] = { 0x49, 0x24, 0x72, IGNORE_MAGIC };
static const gchar argb_reorder_data[] = { 0xff, 0x49, 0x24, 0x72 };
static const gchar xrgb_reorder_data[] = { IGNORE_MAGIC, 0x49, 0x24, 0x72 };
static const gchar rgb_reorder_data[] = { 0x49, 0x24, 0x72, IGNORE_MAGIC };
static const gchar bgr_reorder_data[] = { 0x72, 0x24, 0x49, IGNORE_MAGIC };
static const gchar bgra_reorder_data[] = { 0x72, 0x24, 0x49, 0xff };
static const gchar bgrx_reorder_data[] = { 0x72, 0x24, 0x49, IGNORE_MAGIC };
static const gchar abgr_reorder_data[] = { 0xff, 0x72, 0x24, 0x49 };
static const gchar xbgr_reorder_data[] = { IGNORE_MAGIC, 0x72, 0x24, 0x49 };
static const gchar planar_r_data[] =
    { 0x49, IGNORE_MAGIC, IGNORE_MAGIC, IGNORE_MAGIC };
static const gchar planar_g_data[] =
    { 0x24, IGNORE_MAGIC, IGNORE_MAGIC, IGNORE_MAGIC };
static const gchar planar_b_data[] =
    { 0x72, IGNORE_MAGIC, IGNORE_MAGIC, IGNORE_MAGIC };
static const gchar planar_a_data[] =
    { 0xff, IGNORE_MAGIC, IGNORE_MAGIC, IGNORE_MAGIC };

static TestFrame test_rgba_reorder[] = {
  {1, 1, GST_VIDEO_FORMAT_RGBA, {(gchar *) & rgba_reorder_data}},
  {1, 1, GST_VIDEO_FORMAT_RGBx, {(gchar *) & rgbx_reorder_data}},
  {1, 1, GST_VIDEO_FORMAT_ARGB, {(gchar *) & argb_reorder_data}},
  {1, 1, GST_VIDEO_FORMAT_xRGB, {(gchar *) & xrgb_reorder_data}},
  {1, 1, GST_VIDEO_FORMAT_BGRA, {(gchar *) & bgra_reorder_data}},
  {1, 1, GST_VIDEO_FORMAT_BGRx, {(gchar *) & bgrx_reorder_data}},
  {1, 1, GST_VIDEO_FORMAT_ABGR, {(gchar *) & abgr_reorder_data}},
  {1, 1, GST_VIDEO_FORMAT_xBGR, {(gchar *) & xbgr_reorder_data}},
  {1, 1, GST_VIDEO_FORMAT_RGB, {(gchar *) & rgb_reorder_data}},
  {1, 1, GST_VIDEO_FORMAT_BGR, {(gchar *) & bgr_reorder_data}},
};

static TestFrame test_planar_rgb_reorder[] = {
  {1, 1, GST_VIDEO_FORMAT_RGBA, {(gchar *) & rgba_reorder_data}},
  {1, 1, GST_VIDEO_FORMAT_GBR,
        {(gchar *) planar_g_data, (gchar *) planar_b_data,
          (gchar *) planar_r_data}},
  {1, 1, GST_VIDEO_FORMAT_GBRA,
        {(gchar *) planar_g_data, (gchar *) planar_b_data,
          (gchar *) planar_r_data, (gchar *) planar_a_data}},
  {1, 1, GST_VIDEO_FORMAT_RGBP,
        {(gchar *) planar_r_data, (gchar *) planar_g_data,
          (gchar *) planar_b_data}},
  {1, 1, GST_VIDEO_FORMAT_BGRP,
        {(gchar *) planar_b_data, (gchar *) planar_g_data,
          (gchar *) planar_r_data}},
};

#ifndef GST_CAPS_FEATURE_MEMORY_DMABUF
#define GST_CAPS_FEATURE_MEMORY_DMABUF "memory:DMABuf"
#endif

static GstVideoFormat test_passthrough_formats[] = {
  GST_VIDEO_FORMAT_DMA_DRM,
};

typedef struct
{
  const gchar *features[3];     /* NULL-terminated array of feature strings */
} PassthroughFeatureSet;

static PassthroughFeatureSet test_passthrough_feature_sets[] = {
  /* Single feature: DMABuf only */
  {{GST_CAPS_FEATURE_MEMORY_DMABUF, NULL}},
  /* Multiple features: DMABuf + overlay composition */
  {{GST_CAPS_FEATURE_MEMORY_DMABUF,
          GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION, NULL}},
};

static void
setup (void)
{
  GError *error = NULL;

  display = gst_gl_display_new ();
  context = gst_gl_context_new (display);

  gst_gl_context_create (context, 0, &error);
  window = gst_gl_context_get_window (context);

  fail_if (error != NULL, "Error creating context: %s\n",
      error ? error->message : "Unknown Error");

  convert = gst_gl_color_convert_new (context);
}

static void
_check_gl_error (GstGLContext * context, gpointer data)
{
  GLuint error = context->gl_vtable->GetError ();
  fail_if (error != GL_NONE, "GL error 0x%x encountered during processing\n",
      error);
}

static void
teardown (void)
{
  gst_object_unref (convert);
  gst_object_unref (window);

  gst_gl_context_thread_add (context, (GstGLContextThreadFunc) _check_gl_error,
      NULL);
  gst_object_unref (context);
  gst_object_unref (display);
}

static gsize
_video_info_plane_size (GstVideoInfo * info, guint plane)
{
  if (GST_VIDEO_INFO_N_PLANES (info) == plane + 1)
    return info->offset[0] + info->size - info->offset[plane];

  return info->offset[plane + 1] - info->offset[plane];
}

static void
_frame_unref (gpointer user_data)
{
  gint *ref_count = user_data;
  g_atomic_int_add (ref_count, -1);
}

static void
check_conversion (TestFrame * frames, guint size)
{
  GstGLBaseMemoryAllocator *base_mem_alloc;
  gint i, j, k, l;
  gint ref_count = 0;

  base_mem_alloc =
      GST_GL_BASE_MEMORY_ALLOCATOR (gst_allocator_find
      (GST_GL_MEMORY_ALLOCATOR_NAME));

  for (i = 0; i < size; i++) {
    GstBuffer *inbuf;
    GstVideoInfo in_info;
    gint in_width = frames[i].width;
    gint in_height = frames[i].height;
    GstVideoFormat in_v_format = frames[i].v_format;
    GstCaps *in_caps;

    gst_video_info_set_format (&in_info, in_v_format, in_width, in_height);
    in_caps = gst_video_info_to_caps (&in_info);
    gst_caps_set_features (in_caps, 0,
        gst_caps_features_new_single_static_str
        (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));

    /* create GL buffer */
    inbuf = gst_buffer_new ();
    for (j = 0; j < GST_VIDEO_INFO_N_PLANES (&in_info); j++) {
      GstGLFormat tex_format = gst_gl_format_from_video_info (context,
          &in_info, j);
      GstGLVideoAllocationParams *params;
      GstGLBaseMemory *mem;

      ref_count++;
      params = gst_gl_video_allocation_params_new_wrapped_data (context, NULL,
          &in_info, j, NULL, GST_GL_TEXTURE_TARGET_2D, tex_format,
          frames[i].data[j], &ref_count, _frame_unref);

      mem = gst_gl_base_memory_alloc (base_mem_alloc,
          (GstGLAllocationParams *) params);
      fail_unless (mem != NULL && gst_is_gl_memory (GST_MEMORY_CAST (mem)),
          "failed to create GstGLMemory for %s plane %d with GL format 0x%x",
          gst_video_format_to_string (in_v_format), j, tex_format);
      gst_buffer_append_memory (inbuf, GST_MEMORY_CAST (mem));

      gst_gl_allocation_params_free ((GstGLAllocationParams *) params);
    }

    fail_unless_equals_int (gst_buffer_n_memory (inbuf),
        GST_VIDEO_INFO_N_PLANES (&in_info));

    /* Avoid gst_video_frame_map() here: without GstVideoMeta it maps the
     * whole writable buffer and can replace per-plane memories with one merged
     * memory, changing the GL input layout this test is exercising. */
    for (j = 0; j < GST_VIDEO_INFO_N_PLANES (&in_info); j++) {
      GstMemory *mem = gst_buffer_peek_memory (inbuf, j);
      GstMapInfo map;
      int plane_size = _video_info_plane_size (&in_info, j);

      fail_unless (gst_memory_map (mem, &map, GST_MAP_READ),
          "failed to map %s input plane %d",
          gst_video_format_to_string (in_v_format), j);
      for (k = 0; k < plane_size; k++) {
        if (frames[i].data[j][k] != IGNORE_MAGIC)
          fail_unless (((gchar *) map.data)[k] == frames[i].data[j][k],
              "%s input mismatch at plane %d offset %d: expected 0x%02x, got 0x%02x",
              gst_video_format_to_string (in_v_format), j, k,
              (guint8) frames[i].data[j][k], (guint8) ((gchar *) map.data)[k]);
      }
      gst_memory_unmap (mem, &map);
    }

    fail_unless_equals_int (gst_buffer_n_memory (inbuf),
        GST_VIDEO_INFO_N_PLANES (&in_info));

    for (j = 0; j < size; j++) {
      GstBuffer *outbuf;
      GstVideoInfo out_info;
      gint out_width = frames[j].width;
      gint out_height = frames[j].height;
      GstVideoFormat out_v_format = frames[j].v_format;
      gchar *out_data[GST_VIDEO_MAX_PLANES] = { 0 };
      GstVideoFrame out_frame;
      GstCaps *out_caps;

      gst_video_info_set_format (&out_info, out_v_format, out_width,
          out_height);
      out_caps = gst_video_info_to_caps (&out_info);
      gst_caps_set_features (out_caps, 0,
          gst_caps_features_new_single_static_str
          (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));

      for (k = 0; k < GST_VIDEO_INFO_N_PLANES (&out_info); k++) {
        out_data[k] = frames[j].data[k];
      }

      gst_gl_color_convert_set_caps (convert, in_caps, out_caps);

      /* convert the data */
      outbuf = gst_gl_color_convert_perform (convert, inbuf);
      if (outbuf == NULL) {
        const gchar *in_str = gst_video_format_to_string (in_v_format);
        const gchar *out_str = gst_video_format_to_string (out_v_format);
        GST_WARNING ("failed to convert from %s to %s", in_str, out_str);
      }
      fail_unless (outbuf != NULL, "failed to convert from %s to %s",
          gst_video_format_to_string (in_v_format),
          gst_video_format_to_string (out_v_format));

      fail_unless (gst_video_frame_map (&out_frame, &out_info, outbuf,
              GST_MAP_READ));

      /* check that the converted values are correct */
      for (k = 0; k < GST_VIDEO_INFO_N_PLANES (&out_info); k++) {
        int plane_size = _video_info_plane_size (&out_info, k);
        GST_MEMDUMP ("expected plane", (guint8 *) out_data[k], plane_size);
        GST_MEMDUMP ("produced plane", out_frame.data[k], plane_size);
        for (l = 0; l < plane_size; l++) {
          gchar out_pixel = ((gchar *) out_frame.data[k])[l];
          if (out_data[k][l] != IGNORE_MAGIC && out_pixel != IGNORE_MAGIC)
            fail_unless (out_pixel == out_data[k][l],
                "%s to %s mismatch at plane %d offset %d: expected 0x%02x, got 0x%02x",
                gst_video_format_to_string (in_v_format),
                gst_video_format_to_string (out_v_format), k, l,
                (guint8) out_data[k][l], (guint8) out_pixel);
          /* FIXME: check alpha clobbering */
        }
      }

      gst_caps_unref (out_caps);
      gst_video_frame_unmap (&out_frame);
      gst_buffer_unref (outbuf);
    }

    gst_caps_unref (in_caps);
    gst_buffer_unref (inbuf);

    fail_unless_equals_int (ref_count, 0);
  }

  gst_object_unref (base_mem_alloc);
}

GST_START_TEST (test_reorder_buffer)
{
  guint size = G_N_ELEMENTS (test_rgba_reorder);

  /* gles can't download rgb24 textures */
  if (gst_gl_context_get_gl_api (context) & GST_GL_API_GLES2)
    size -= 2;

  check_conversion (test_rgba_reorder, size);
}

GST_END_TEST;

GST_START_TEST (test_planar_rgb_reorder_buffer)
{
  check_conversion (test_planar_rgb_reorder,
      G_N_ELEMENTS (test_planar_rgb_reorder));
}

GST_END_TEST;

GST_START_TEST (test_passthrough)
{
  guint formats_size = G_N_ELEMENTS (test_passthrough_formats);
  guint features_size = G_N_ELEMENTS (test_passthrough_feature_sets);
  gint i, j, k, l, m;

  for (i = 0; i < formats_size; i++) {
    GstVideoFormat in_format = test_passthrough_formats[i];

    for (j = 0; j < formats_size; j++) {
      GstVideoFormat out_format = test_passthrough_formats[j];

      for (k = 0; k < features_size; k++) {
        PassthroughFeatureSet *in_feature_set =
            &test_passthrough_feature_sets[k];
        GstCaps *in_caps;
        GstCapsFeatures *in_features;

        in_caps = gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING,
            gst_video_format_to_string (in_format), NULL);

        in_features = gst_caps_features_new_empty ();
        for (m = 0; in_feature_set->features[m] != NULL; m++) {
          gst_caps_features_add (in_features, in_feature_set->features[m]);
        }
        gst_caps_set_features_simple (in_caps, in_features);

        for (l = 0; l < features_size; l++) {
          PassthroughFeatureSet *out_feature_set =
              &test_passthrough_feature_sets[l];
          GstCaps *out_caps;
          GstCapsFeatures *out_features;

          out_caps = gst_caps_new_simple ("video/x-raw", "format",
              G_TYPE_STRING, gst_video_format_to_string (out_format), NULL);

          out_features = gst_caps_features_new_empty ();
          for (m = 0; out_feature_set->features[m] != NULL; m++) {
            gst_caps_features_add (out_features, out_feature_set->features[m]);
          }
          gst_caps_set_features_simple (out_caps, out_features);

          if (gst_caps_is_equal (in_caps, out_caps)) {
            GstCaps *tmp_caps, *tmp_caps2, *tmp_caps3;

            tmp_caps = gst_gl_color_convert_transform_caps (context,
                GST_PAD_SINK, in_caps, NULL);
            tmp_caps2 = gst_gl_color_convert_transform_caps (context,
                GST_PAD_SRC, out_caps, NULL);

            tmp_caps3 = gst_caps_intersect (tmp_caps, tmp_caps2);

            fail_unless (!gst_caps_is_empty (tmp_caps3));

            gst_caps_unref (tmp_caps);
            gst_caps_unref (tmp_caps2);
            gst_caps_unref (tmp_caps3);
          }

          gst_caps_unref (out_caps);
        }

        gst_caps_unref (in_caps);
      }
    }
  }
}

GST_END_TEST;

static guint
_overlay_composition_count (GstBuffer * buffer)
{
  gpointer state = NULL;
  GstMeta *meta;
  guint n = 0;
  while ((meta =
          gst_buffer_iterate_meta_filtered (buffer, &state,
              GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE)) != NULL) {
    n++;
  }
  return n;
}

static GstPadProbeReturn
convert_sink_pad_probe (GstPad * pad, GstPadProbeInfo * info,
    gpointer user_data)
{
  GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
  fail_unless (buffer != NULL);

  /* Check that we have 2 overlays, one for each textoverlay */
  fail_unless (_overlay_composition_count (buffer) == 2);

  /* Add a custom meta to ensure glcolorconvert does copies all metas. */
  fail_unless (gst_buffer_is_writable (buffer));
  gst_buffer_add_custom_meta (buffer, "test-glcolorconvert");

  return GST_PAD_PROBE_OK;
}

static GstPadProbeReturn
convert_src_pad_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
{
  GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
  fail_unless (buffer != NULL);

  /* Check that glcolorconvert kept overlay composition metas untouched (it used to add an extra copy). */
  fail_unless (_overlay_composition_count (buffer) == 2);

  /* Ensure our custom meta is still there. */
  fail_unless (gst_buffer_get_custom_meta (buffer,
          "test-glcolorconvert") != NULL);

  return GST_PAD_PROBE_OK;
}

static GstPadProbeReturn
comp_src_pad_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
{
  GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
  fail_unless (buffer != NULL);

  /* Check that gloverlaycompositor droped overlay composition metas. */
  fail_unless (_overlay_composition_count (buffer) == 0);

  /* Ensure our custom meta is still there. */
  fail_unless (gst_buffer_get_custom_meta (buffer,
          "test-glcolorconvert") != NULL);

  return GST_PAD_PROBE_OK;
}

GST_START_TEST (test_meta)
{
  GstElementFactory *textoverlay_factory;
  GError *error = NULL;
  GstElement *pipeline;

  textoverlay_factory = gst_element_factory_find ("textoverlay");
  if (textoverlay_factory == NULL) {
    GST_INFO ("Skipping meta test: textoverlay missing");
    return;
  }
  gst_object_unref (textoverlay_factory);

  gst_meta_register_custom_simple ("test-glcolorconvert");

  pipeline = gst_parse_launch ("videotestsrc "
      "! video/x-raw,format=YUY2 "
      "! glupload "
      "! textoverlay text=Hello ! textoverlay text=World "
      "! glcolorconvert name=convert "
      "! video/x-raw(ANY),format=RGBA "
      "! gloverlaycompositor name=comp ! gldownload ! fakesink", &error);
  fail_unless (pipeline != NULL, "Failed to parse pipeline: %s",
      error ? error->message : "unknown error");
  g_clear_error (&error);

  /* Check buffer before and after glcolorconvert */
  GstElement *conv = gst_bin_get_by_name (GST_BIN (pipeline), "convert");
  fail_unless (conv != NULL);
  gst_object_unref (conv);

  GstPad *pad = gst_element_get_static_pad (conv, "sink");
  gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER,
      (GstPadProbeCallback) convert_sink_pad_probe, NULL, NULL);
  gst_object_unref (pad);

  pad = gst_element_get_static_pad (conv, "src");
  gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER,
      (GstPadProbeCallback) convert_src_pad_probe, NULL, NULL);
  gst_object_unref (pad);

  /* Check that gloverlaycomposition drop overlay composition meta */
  GstElement *comp = gst_bin_get_by_name (GST_BIN (pipeline), "comp");
  fail_unless (comp != NULL);
  gst_object_unref (comp);

  pad = gst_element_get_static_pad (comp, "src");
  gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER,
      (GstPadProbeCallback) comp_src_pad_probe, NULL, NULL);
  gst_object_unref (pad);

  /* Wait for preroll */
  GstState state;
  gst_element_set_state (pipeline, GST_STATE_PAUSED);
  fail_unless_equals_int (gst_element_get_state (pipeline, &state, NULL,
          GST_SECOND * 10), GST_STATE_CHANGE_SUCCESS);
  fail_unless (state == GST_STATE_PAUSED);

  /* Teardown */
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);
}

GST_END_TEST;
static Suite *
gst_gl_color_convert_suite (void)
{
  Suite *s = suite_create ("GstGLColorConvert");
  TCase *tc_chain = tcase_create ("upload");

  suite_add_tcase (s, tc_chain);
  tcase_add_checked_fixture (tc_chain, setup, teardown);
  tcase_add_test (tc_chain, test_reorder_buffer);
  tcase_add_test (tc_chain, test_planar_rgb_reorder_buffer);
  tcase_add_test (tc_chain, test_meta);
  tcase_add_test (tc_chain, test_passthrough);
  /* FIXME add YUV <--> RGB conversion tests */

  return s;
}

#if defined(__APPLE__) && TARGET_OS_OSX
static int
run_tests (void)
{
  Suite *s = gst_gl_color_convert_suite ();

  return gst_check_run_suite_nofork (s, "gst_gl_color_convert", __FILE__);
}

int
main (int argc, char **argv)
{
  gst_check_init (&argc, &argv);
  return gst_macos_main_simple ((GstMainFuncSimple) run_tests, NULL);
}
#else
GST_CHECK_MAIN (gst_gl_color_convert);
#endif
