/*****************************************************************************
 * va.c: Video Acceleration API for avcodec
 *****************************************************************************
 * Copyright (C) 2011, M2X BV
 * $Id: f4526092eee62ec539a8997f50a331234eb8a61e $
 *
 * Authors: Jean-Paul Saman
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

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

#include <vlc_common.h>
#include <vlc_fourcc.h>
#include <assert.h>

#ifdef HAVE_LIBAVCODEC_AVCODEC_H
#   include <libavcodec/avcodec.h>
#else
#   include <avcodec.h>
#endif

#include "avcodec.h"
#ifdef HAVE_AVCODEC_VAAPI
#  include <vlc_xlib.h>
#  include <libavcodec/vaapi.h>
#  include <va/va.h>
#  include <va/va_x11.h>
#endif
#include "va.h"
#include "vaapi.h"

#ifdef HAVE_AVCODEC_VAAPI

static vlc_mutex_t vlc_va_conn_lock = VLC_STATIC_MUTEX;

static void vlc_va_lock(void)
{
    vlc_mutex_lock(&vlc_va_conn_lock);
}

static void vlc_va_unlock(void)
{
    vlc_mutex_unlock(&vlc_va_conn_lock);
}

static VASurfaceID *vlc_va_create_surfaces( vlc_va_conn_t *conn,
    const int width, const int height, const int fourcc, const int num )
{
    assert(conn);

    if( (width <= 0) || (height <= 0) || (num <= 0) )
        return NULL;

    /* FIXME: The size does not necessary match */
    if( conn->i_ref_count > 1 )
    {
        assert( num == conn->i_surface_count );
        return conn->p_surface_ids;
    }

    assert( conn->p_surface_ids == NULL );
    assert( conn->i_surface_count == 0 );

    VASurfaceID *p_surfaces = (VASurfaceID *)calloc(num, sizeof(VASurfaceID));
    if( p_surfaces == NULL )
        return NULL;

    int i_surface_count = num;
    VAStatus status = vaCreateSurfaces( conn->p_display, width, height, fourcc,
                                        num, p_surfaces );
    if( status != VA_STATUS_SUCCESS )
    {
        free( p_surfaces );
        return NULL;
    }

    assert( i_surface_count == num );

    conn->p_surface_ids = p_surfaces;
    conn->i_surface_count = i_surface_count;
    return conn->p_surface_ids;
}

static void vlc_va_destroy_surfaces( vlc_va_conn_t *conn )
{
    assert( conn );
    assert( conn->i_surface_count > 0 );
    assert( conn->p_surface_ids );

    if( conn->i_ref_count > 1 )
        return;

    if( conn->p_surface_ids != NULL )
        vaDestroySurfaces( conn->p_display, conn->p_surface_ids,
                           conn->i_surface_count );

    conn->i_surface_count = 0;
    conn->p_surface_ids = NULL;
}

/* Global VAAPI connection state */
static vlc_va_conn_t vlc_va_conn = {
    .p_display = 0,
    /* libva version */
    .i_version_major = 0,
    .i_version_minor = 0,
    /* ref counting */
    .i_ref_count = 0,
    /* locking functions */
    .lock = vlc_va_lock,
    .unlock = vlc_va_unlock,
    /* surfaces - NOTE: must be called with lock held */
    .i_surface_count = 0,
    .p_surface_ids = NULL,
    .create_surfaces = vlc_va_create_surfaces,
    .destroy_surfaces = vlc_va_destroy_surfaces,
};

/* */
vlc_va_conn_t *vlc_va_get_conn( void )
{
    return (vlc_va_conn_t *) &vlc_va_conn;
}

vlc_va_conn_t *vlc_va_Initialize( Display *display )
{
    vlc_mutex_lock( &vlc_va_conn_lock );

    /* connect global to caller */
    vlc_va_conn_t *conn = vlc_va_get_conn();
    assert( conn );
    if( conn->i_ref_count > 0 )
    {
        conn->i_ref_count++;
        vlc_mutex_unlock( &vlc_va_conn_lock );
        return conn;
    }

    /* Create a VA display */
    conn->p_display = vaGetDisplay(display);
    if( !conn->p_display )
        goto error;

    if( vaInitialize( conn->p_display, &conn->i_version_major, &conn->i_version_minor ) )
        goto error;

    conn->i_ref_count++;
    vlc_mutex_unlock( &vlc_va_conn_lock );
    return conn;

error:
    vlc_mutex_unlock( &vlc_va_conn_lock );
    return NULL;
}

void vlc_va_Terminate( vlc_va_conn_t *conn )
{
    assert( conn );

    conn->i_ref_count--;
    if( conn->i_ref_count > 0 )
        return;

    assert( conn->i_ref_count == 0 );
    assert( conn->i_surface_count == 0 );
    assert( conn->p_surface_ids == NULL );

    if( conn->p_display )
        vaTerminate( conn->p_display );

    /* Reset values */
    conn->p_display = 0;
    conn->i_version_major = conn->i_version_minor = 0;
    conn->i_ref_count = 0;
    conn = NULL;
}

#endif
