<commit_before>/*
 *  gstvaapiutils.c - VA-API utilities
 *
 *  Copyright (C) 2010-2011 Splitted-Desktop Systems
 *    Author: <NAME> <<EMAIL>>
 *  Copyright (C) 2011-2014 Intel Corporation
 *    Author: <NAME> <<EMAIL>>
 *
 *  This library 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 2.1
 *  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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
 */

#include "sysdeps.h"
#include "gstvaapicompat.h"
#include "gstvaapiutils.h"
#include "gstvaapisurface.h"
#include "gstvaapisubpicture.h"
#include "gstvaapifilter.h"
#include <stdio.h>
#include <stdarg.h>

#if USE_VA_VPP
# include <va/va_vpp.h>
#endif

#define DEBUG 1
#include "gstvaapidebug.h"

#define CONCAT(a, b)    CONCAT_(a, b)
#define CONCAT_(a, b)   a##b
#define STRINGIFY(x)    STRINGIFY_(x)
#define STRINGIFY_(x)   #x
#define STRCASEP(p, x)  STRCASE(CONCAT(p, x))
#define STRCASE(x)      case x: return STRINGIFY(x)

#if VA_CHECK_VERSION (0,40,0)
static void
gst_vaapi_log (const char *message)
{
  gchar *msg;

  msg = g_strdup (message);
  if (!msg)
    return;
  g_strchomp (msg);
  GST_INFO ("%s", msg);
  g_free (msg);
}
#endif

gboolean
vaapi_initialize (VADisplay dpy)
{
  gint major_version, minor_version;
  VAStatus status;

#if VA_CHECK_VERSION (0,40,0)
  vaSetInfoCallback (gst_vaapi_log);
#endif

  status = vaInitialize (dpy, &major_version, &minor_version);
  if (!vaapi_check_status (status, "vaInitialize()"))
    return FALSE;

  GST_INFO ("VA-API version %d.%d", major_version, minor_version);
  return TRUE;
}

/* Check VA status for success or print out an error */
gboolean
vaapi_check_status (VAStatus status, const gchar * msg)
{
  if (status != VA_STATUS_SUCCESS) {
    GST_DEBUG ("%s: %s", msg, vaErrorStr (status));
    return FALSE;
  }
  return TRUE;
}

/* Maps VA buffer */
gpointer
vaapi_map_buffer (VADisplay dpy, VABufferID buf_id)
{
  VAStatus status;
  gpointer data = NULL;

  status = vaMapBuffer (dpy, buf_id, &data);
  if (!vaapi_check_status (status, "vaMapBuffer()"))
    return NULL;
  return data;
}

/* Unmaps VA buffer */
void
vaapi_unmap_buffer (VADisplay dpy, VABufferID buf_id, gpointer * pbuf)
{
  VAStatus status;

  if (pbuf)
    *pbuf = NULL;

  status = vaUnmapBuffer (dpy, buf_id);
  if (!vaapi_check_status (status, "vaUnmapBuffer()"))
    return;
}

/* Creates and maps VA buffer */
gboolean
vaapi_create_buffer (VADisplay dpy, VAContextID ctx, int type, guint size,
    gconstpointer buf, VABufferID * buf_id_ptr, gpointer * mapped_data)
{
  VABufferID buf_id;
  VAStatus status;
  gpointer data = (gpointer) buf;

  status = vaCreateBuffer (dpy, ctx, type, size, 1, data, &buf_id);
  if (!vaapi_check_status (status, "vaCreateBuffer()"))
    return FALSE;

  if (mapped_data) {
    data = vaapi_map_buffer (dpy, buf_id);
    if (!data)
      goto error;
    *mapped_data = data;
  }

  *buf_id_ptr = buf_id;
  return TRUE;

  /* ERRORS */
error:
  {
    vaapi_destroy_buffer (dpy, &buf_id);
    return FALSE;
  }
}

/* Destroy VA buffer */
void
vaapi_destroy_buffer (VADisplay dpy, VABufferID * buf_id_ptr)
{
  if (!buf_id_ptr || *buf_id_ptr == VA_INVALID_ID)
    return;

  vaDestroyBuffer (dpy, *buf_id_ptr);
  *buf_id_ptr = VA_INVALID_ID;
}

/* Return a string representation of a VAProfile */
const gchar *
string_of_VAProfile (VAProfile profile)
{
  switch (profile) {
#define MAP(profile) \
        STRCASEP(VAProfile, profile)
      MAP (MPEG2Simple);
      MAP (MPEG2Main);
      MAP (MPEG4Simple);
      MAP (MPEG4AdvancedSimple);
      MAP (MPEG4Main);
#if VA_CHECK_VERSION(0,32,0)
      MAP (JPEGBaseline);
      MAP (H263Baseline);
      MAP (H264ConstrainedBaseline);
#endif
      MAP (H264Baseline);
      MAP (H264Main);
      MAP (H264High);
#if VA_CHECK_VERSION(0,35,2)
      MAP (H264MultiviewHigh);
      MAP (H264StereoHigh);
#endif
      MAP (VC1Simple);
      MAP (VC1Main);
      MAP (VC1Advanced);<commit_msg>libs: utils: add HEVC profiles representation<commit_after>/*
 *  gstvaapiutils.c - VA-API utilities
 *
 *  Copyright (C) 2010-2011 Splitted-Desktop Systems
 *    Author: <NAME> <<EMAIL>>
 *  Copyright (C) 2011-2014 Intel Corporation
 *    Author: <NAME> <<EMAIL>>
 *
 *  This library 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 2.1
 *  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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
 */

#include "sysdeps.h"
#include "gstvaapicompat.h"
#include "gstvaapiutils.h"
#include "gstvaapisurface.h"
#include "gstvaapisubpicture.h"
#include "gstvaapifilter.h"
#include <stdio.h>
#include <stdarg.h>

#if USE_VA_VPP
# include <va/va_vpp.h>
#endif

#define DEBUG 1
#include "gstvaapidebug.h"

#define CONCAT(a, b)    CONCAT_(a, b)
#define CONCAT_(a, b)   a##b
#define STRINGIFY(x)    STRINGIFY_(x)
#define STRINGIFY_(x)   #x
#define STRCASEP(p, x)  STRCASE(CONCAT(p, x))
#define STRCASE(x)      case x: return STRINGIFY(x)

#if VA_CHECK_VERSION (0,40,0)
static void
gst_vaapi_log (const char *message)
{
  gchar *msg;

  msg = g_strdup (message);
  if (!msg)
    return;
  g_strchomp (msg);
  GST_INFO ("%s", msg);
  g_free (msg);
}
#endif

gboolean
vaapi_initialize (VADisplay dpy)
{
  gint major_version, minor_version;
  VAStatus status;

#if VA_CHECK_VERSION (0,40,0)
  vaSetInfoCallback (gst_vaapi_log);
#endif

  status = vaInitialize (dpy, &major_version, &minor_version);
  if (!vaapi_check_status (status, "vaInitialize()"))
    return FALSE;

  GST_INFO ("VA-API version %d.%d", major_version, minor_version);
  return TRUE;
}

/* Check VA status for success or print out an error */
gboolean
vaapi_check_status (VAStatus status, const gchar * msg)
{
  if (status != VA_STATUS_SUCCESS) {
    GST_DEBUG ("%s: %s", msg, vaErrorStr (status));
    return FALSE;
  }
  return TRUE;
}

/* Maps VA buffer */
gpointer
vaapi_map_buffer (VADisplay dpy, VABufferID buf_id)
{
  VAStatus status;
  gpointer data = NULL;

  status = vaMapBuffer (dpy, buf_id, &data);
  if (!vaapi_check_status (status, "vaMapBuffer()"))
    return NULL;
  return data;
}

/* Unmaps VA buffer */
void
vaapi_unmap_buffer (VADisplay dpy, VABufferID buf_id, gpointer * pbuf)
{
  VAStatus status;

  if (pbuf)
    *pbuf = NULL;

  status = vaUnmapBuffer (dpy, buf_id);
  if (!vaapi_check_status (status, "vaUnmapBuffer()"))
    return;
}

/* Creates and maps VA buffer */
gboolean
vaapi_create_buffer (VADisplay dpy, VAContextID ctx, int type, guint size,
    gconstpointer buf, VABufferID * buf_id_ptr, gpointer * mapped_data)
{
  VABufferID buf_id;
  VAStatus status;
  gpointer data = (gpointer) buf;

  status = vaCreateBuffer (dpy, ctx, type, size, 1, data, &buf_id);
  if (!vaapi_check_status (status, "vaCreateBuffer()"))
    return FALSE;

  if (mapped_data) {
    data = vaapi_map_buffer (dpy, buf_id);
    if (!data)
      goto error;
    *mapped_data = data;
  }

  *buf_id_ptr = buf_id;
  return TRUE;

  /* ERRORS */
error:
  {
    vaapi_destroy_buffer (dpy, &buf_id);
    return FALSE;
  }
}

/* Destroy VA buffer */
void
vaapi_destroy_buffer (VADisplay dpy, VABufferID * buf_id_ptr)
{
  if (!buf_id_ptr || *buf_id_ptr == VA_INVALID_ID)
    return;

  vaDestroyBuffer (dpy, *buf_id_ptr);
  *buf_id_ptr = VA_INVALID_ID;
}

/* Return a string representation of a VAProfile */
const gchar *
string_of_VAProfile (VAProfile profile)
{
  switch (profile) {
#define MAP(profile) \
        STRCASEP(VAProfile, profile)
      MAP (MPEG2Simple);
      MAP (MPEG2Main);
      MAP (MPEG4Simple);
      MAP (MPEG4AdvancedSimple);
      MAP (MPEG4Main);
#if VA_CHECK_VERSION(0,32,0)
      MAP (JPEGBaseline);
      MAP (H263Baseline);
      MAP (H264ConstrainedBaseline);
#endif
      MAP (H264Baseline);
      MAP (H264Main);
      MAP (H264High);
#if VA_CHECK_VERSION(0,35,2)
      MAP (H264MultiviewHigh);
      MAP (H264StereoHigh);
#endif
#if VA_CHECK_VERSION(0,37,1)
      MAP (HEVCMain);
      MAP (HEVCMain10);
#endif
      MAP (VC1Simple);
      MAP (VC1Main);
      MAP (VC1Advanced);
{'fraction_non_alphanumeric': 0.11407179035454745, 'fraction_numerical': 0.017397049108125964, 'mean_word_length': 3.808364213869772, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7300653', 'n_tokens_mistral': 3474, 'n_tokens_neox': 3299, 'n_words': 955}