Blame tests/check/libs/gstglcolorconvert.c

Packit 971217
/* GStreamer
Packit 971217
 *
Packit 971217
 * Copyright (C) 2014 Matthew Waters <matthew@centricular.com>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#  include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include <gst/check/gstcheck.h>
Packit 971217
Packit 971217
#include <gst/gl/gl.h>
Packit 971217
#include <gst/gl/gstglfuncs.h>
Packit 971217
Packit 971217
#include <stdio.h>
Packit 971217
Packit 971217
static GstGLDisplay *display;
Packit 971217
static GstGLContext *context;
Packit 971217
static GstGLWindow *window;
Packit 971217
static GstGLColorConvert *convert;
Packit 971217
Packit 971217
typedef struct _TestFrame
Packit 971217
{
Packit 971217
  gint width;
Packit 971217
  gint height;
Packit 971217
  GstVideoFormat v_format;
Packit 971217
  gchar *data[GST_VIDEO_MAX_PLANES];
Packit 971217
} TestFrame;
Packit 971217
Packit 971217
#define IGNORE_MAGIC 0x05
Packit 971217
Packit 971217
static const gchar rgba_reorder_data[] = { 0x49, 0x24, 0x72, 0xff };
Packit 971217
static const gchar rgbx_reorder_data[] = { 0x49, 0x24, 0x72, IGNORE_MAGIC };
Packit 971217
static const gchar argb_reorder_data[] = { 0xff, 0x49, 0x24, 0x72 };
Packit 971217
static const gchar xrgb_reorder_data[] = { IGNORE_MAGIC, 0x49, 0x24, 0x72 };
Packit 971217
static const gchar rgb_reorder_data[] = { 0x49, 0x24, 0x72, IGNORE_MAGIC };
Packit 971217
static const gchar bgr_reorder_data[] = { 0x72, 0x24, 0x49, IGNORE_MAGIC };
Packit 971217
static const gchar bgra_reorder_data[] = { 0x72, 0x24, 0x49, 0xff };
Packit 971217
static const gchar bgrx_reorder_data[] = { 0x72, 0x24, 0x49, IGNORE_MAGIC };
Packit 971217
static const gchar abgr_reorder_data[] = { 0xff, 0x72, 0x24, 0x49 };
Packit 971217
static const gchar xbgr_reorder_data[] = { IGNORE_MAGIC, 0x72, 0x24, 0x49 };
Packit 971217
Packit 971217
static TestFrame test_rgba_reorder[] = {
Packit 971217
  {1, 1, GST_VIDEO_FORMAT_RGBA, {(gchar *) & rgba_reorder_data}},
Packit 971217
  {1, 1, GST_VIDEO_FORMAT_RGBx, {(gchar *) & rgbx_reorder_data}},
Packit 971217
  {1, 1, GST_VIDEO_FORMAT_ARGB, {(gchar *) & argb_reorder_data}},
Packit 971217
  {1, 1, GST_VIDEO_FORMAT_xRGB, {(gchar *) & xrgb_reorder_data}},
Packit 971217
  {1, 1, GST_VIDEO_FORMAT_BGRA, {(gchar *) & bgra_reorder_data}},
Packit 971217
  {1, 1, GST_VIDEO_FORMAT_BGRx, {(gchar *) & bgrx_reorder_data}},
Packit 971217
  {1, 1, GST_VIDEO_FORMAT_ABGR, {(gchar *) & abgr_reorder_data}},
Packit 971217
  {1, 1, GST_VIDEO_FORMAT_xBGR, {(gchar *) & xbgr_reorder_data}},
Packit 971217
  {1, 1, GST_VIDEO_FORMAT_RGB, {(gchar *) & rgb_reorder_data}},
Packit 971217
  {1, 1, GST_VIDEO_FORMAT_BGR, {(gchar *) & bgr_reorder_data}},
Packit 971217
};
Packit 971217
Packit 971217
static void
Packit 971217
setup (void)
Packit 971217
{
Packit 971217
  GError *error = NULL;
Packit 971217
Packit 971217
  display = gst_gl_display_new ();
Packit 971217
  context = gst_gl_context_new (display);
Packit 971217
Packit 971217
  gst_gl_context_create (context, 0, &error);
Packit 971217
  window = gst_gl_context_get_window (context);
Packit 971217
Packit 971217
  fail_if (error != NULL, "Error creating context: %s\n",
Packit 971217
      error ? error->message : "Unknown Error");
Packit 971217
Packit 971217
  convert = gst_gl_color_convert_new (context);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
_check_gl_error (GstGLContext * context, gpointer data)
Packit 971217
{
Packit 971217
  GLuint error = context->gl_vtable->GetError ();
Packit 971217
  fail_if (error != GL_NONE, "GL error 0x%x encountered during processing\n",
Packit 971217
      error);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
teardown (void)
Packit 971217
{
Packit 971217
  gst_object_unref (convert);
Packit 971217
  gst_object_unref (window);
Packit 971217
Packit 971217
  gst_gl_context_thread_add (context, (GstGLContextThreadFunc) _check_gl_error,
Packit 971217
      NULL);
Packit 971217
  gst_object_unref (context);
Packit 971217
  gst_object_unref (display);
Packit 971217
}
Packit 971217
Packit 971217
static gsize
Packit 971217
_video_info_plane_size (GstVideoInfo * info, guint plane)
Packit 971217
{
Packit 971217
  if (GST_VIDEO_INFO_N_PLANES (info) == plane + 1)
Packit 971217
    return info->offset[0] + info->size - info->offset[plane];
Packit 971217
Packit 971217
  return info->offset[plane + 1] - info->offset[plane];
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
_frame_unref (gpointer user_data)
Packit 971217
{
Packit 971217
  gint *ref_count = user_data;
Packit 971217
  g_atomic_int_add (ref_count, -1);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
check_conversion (TestFrame * frames, guint size)
Packit 971217
{
Packit 971217
  GstGLBaseMemoryAllocator *base_mem_alloc;
Packit 971217
  gint i, j, k, l;
Packit 971217
  gint ref_count = 0;
Packit 971217
Packit 971217
  base_mem_alloc =
Packit 971217
      GST_GL_BASE_MEMORY_ALLOCATOR (gst_allocator_find
Packit 971217
      (GST_GL_MEMORY_ALLOCATOR_NAME));
Packit 971217
Packit 971217
  for (i = 0; i < size; i++) {
Packit 971217
    GstBuffer *inbuf;
Packit 971217
    GstVideoInfo in_info;
Packit 971217
    gint in_width = frames[i].width;
Packit 971217
    gint in_height = frames[i].height;
Packit 971217
    GstVideoFormat in_v_format = frames[i].v_format;
Packit 971217
    GstVideoFrame in_frame;
Packit 971217
    GstCaps *in_caps;
Packit 971217
Packit 971217
    gst_video_info_set_format (&in_info, in_v_format, in_width, in_height);
Packit 971217
    in_caps = gst_video_info_to_caps (&in_info);
Packit 971217
    gst_caps_set_features (in_caps, 0,
Packit 971217
        gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));
Packit 971217
Packit 971217
    /* create GL buffer */
Packit 971217
    inbuf = gst_buffer_new ();
Packit 971217
    for (j = 0; j < GST_VIDEO_INFO_N_PLANES (&in_info); j++) {
Packit 971217
      GstGLFormat tex_format = gst_gl_format_from_video_info (context,
Packit 971217
          &in_info, j);
Packit 971217
      GstGLVideoAllocationParams *params;
Packit 971217
      GstGLBaseMemory *mem;
Packit 971217
Packit 971217
      ref_count++;
Packit 971217
      params = gst_gl_video_allocation_params_new_wrapped_data (context, NULL,
Packit 971217
          &in_info, j, NULL, GST_GL_TEXTURE_TARGET_2D, tex_format,
Packit 971217
          frames[i].data[j], &ref_count, _frame_unref);
Packit 971217
Packit 971217
      mem = gst_gl_base_memory_alloc (base_mem_alloc,
Packit 971217
          (GstGLAllocationParams *) params);
Packit 971217
      gst_buffer_append_memory (inbuf, GST_MEMORY_CAST (mem));
Packit 971217
Packit 971217
      gst_gl_allocation_params_free ((GstGLAllocationParams *) params);
Packit 971217
    }
Packit 971217
Packit 971217
    fail_unless (gst_video_frame_map (&in_frame, &in_info, inbuf,
Packit 971217
            GST_MAP_READ));
Packit 971217
Packit 971217
    /* sanity check that the correct values were wrapped */
Packit 971217
    for (j = 0; j < GST_VIDEO_INFO_N_PLANES (&in_info); j++) {
Packit 971217
      for (k = 0; k < _video_info_plane_size (&in_info, j); k++) {
Packit 971217
        if (frames[i].data[j][k] != IGNORE_MAGIC)
Packit 971217
          fail_unless (((gchar *) in_frame.data[j])[k] == frames[i].data[j][k]);
Packit 971217
      }
Packit 971217
    }
Packit 971217
Packit 971217
    for (j = 0; j < size; j++) {
Packit 971217
      GstBuffer *outbuf;
Packit 971217
      GstVideoInfo out_info;
Packit 971217
      gint out_width = frames[j].width;
Packit 971217
      gint out_height = frames[j].height;
Packit 971217
      GstVideoFormat out_v_format = frames[j].v_format;
Packit 971217
      gchar *out_data[GST_VIDEO_MAX_PLANES] = { 0 };
Packit 971217
      GstVideoFrame out_frame;
Packit 971217
      GstCaps *out_caps;
Packit 971217
Packit 971217
      gst_video_info_set_format (&out_info, out_v_format, out_width,
Packit 971217
          out_height);
Packit 971217
      out_caps = gst_video_info_to_caps (&out_info);
Packit 971217
      gst_caps_set_features (out_caps, 0,
Packit 971217
          gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));
Packit 971217
Packit 971217
      for (k = 0; k < GST_VIDEO_INFO_N_PLANES (&out_info); k++) {
Packit 971217
        out_data[k] = frames[j].data[k];
Packit 971217
      }
Packit 971217
Packit 971217
      gst_gl_color_convert_set_caps (convert, in_caps, out_caps);
Packit 971217
Packit 971217
      /* convert the data */
Packit 971217
      outbuf = gst_gl_color_convert_perform (convert, inbuf);
Packit 971217
      if (outbuf == NULL) {
Packit 971217
        const gchar *in_str = gst_video_format_to_string (in_v_format);
Packit 971217
        const gchar *out_str = gst_video_format_to_string (out_v_format);
Packit 971217
        GST_WARNING ("failed to convert from %s to %s", in_str, out_str);
Packit 971217
      }
Packit 971217
Packit 971217
      fail_unless (gst_video_frame_map (&out_frame, &out_info, outbuf,
Packit 971217
              GST_MAP_READ));
Packit 971217
Packit 971217
      /* check that the converted values are correct */
Packit 971217
      for (k = 0; k < GST_VIDEO_INFO_N_PLANES (&out_info); k++) {
Packit 971217
        for (l = 0; l < _video_info_plane_size (&out_info, k); l++) {
Packit 971217
          gchar out_pixel = ((gchar *) out_frame.data[k])[l];
Packit 971217
          if (out_data[k][l] != IGNORE_MAGIC && out_pixel != IGNORE_MAGIC)
Packit 971217
            fail_unless (out_pixel == out_data[k][l]);
Packit 971217
          /* FIXME: check alpha clobbering */
Packit 971217
        }
Packit 971217
      }
Packit 971217
Packit 971217
      gst_caps_unref (out_caps);
Packit 971217
      gst_video_frame_unmap (&out_frame);
Packit 971217
      gst_buffer_unref (outbuf);
Packit 971217
    }
Packit 971217
Packit 971217
    gst_caps_unref (in_caps);
Packit 971217
    gst_video_frame_unmap (&in_frame);
Packit 971217
    gst_buffer_unref (inbuf);
Packit 971217
Packit 971217
    fail_unless_equals_int (ref_count, 0);
Packit 971217
  }
Packit 971217
Packit 971217
  gst_object_unref (base_mem_alloc);
Packit 971217
}
Packit 971217
Packit 971217
GST_START_TEST (test_reorder_buffer)
Packit 971217
{
Packit 971217
  guint size = G_N_ELEMENTS (test_rgba_reorder);
Packit 971217
Packit 971217
  /* gles can't download rgb24 textures */
Packit 971217
  if (gst_gl_context_get_gl_api (context) & GST_GL_API_GLES2)
Packit 971217
    size -= 2;
Packit 971217
Packit 971217
  check_conversion (test_rgba_reorder, size);
Packit 971217
}
Packit 971217
Packit 971217
GST_END_TEST;
Packit 971217
Packit 971217
static Suite *
Packit 971217
gst_gl_color_convert_suite (void)
Packit 971217
{
Packit 971217
  Suite *s = suite_create ("GstGLColorConvert");
Packit 971217
  TCase *tc_chain = tcase_create ("upload");
Packit 971217
Packit 971217
  suite_add_tcase (s, tc_chain);
Packit 971217
  tcase_add_checked_fixture (tc_chain, setup, teardown);
Packit 971217
  tcase_add_test (tc_chain, test_reorder_buffer);
Packit 971217
  /* FIXME add YUV <--> RGB conversion tests */
Packit 971217
Packit 971217
  return s;
Packit 971217
}
Packit 971217
Packit 971217
GST_CHECK_MAIN (gst_gl_color_convert);