va.c 5.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*****************************************************************************
 * va.c: Video Acceleration API for avcodec
 *****************************************************************************
 * Copyright (C) 2011, M2X BV
 * $Id$
 *
 * 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"
46
#include "vaapi.h"
47 48 49

#ifdef HAVE_AVCODEC_VAAPI

50 51 52 53 54 55 56 57 58 59 60
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);
}
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
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,
86
                                        i_surface_count, p_surfaces );
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    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;
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/* 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,
};

135
/* */
136
static vlc_va_conn_t *vlc_va_get_conn( void )
137 138 139 140
{
    return (vlc_va_conn_t *) &vlc_va_conn;
}

141
vlc_va_conn_t *vlc_va_Initialize( Display *display )
142
{
143
    vlc_mutex_lock( &vlc_va_conn_lock );
144

145
    /* connect global to caller */
146
    vlc_va_conn_t *conn = vlc_va_get_conn();
147 148
    assert( conn );
    if( conn->i_ref_count > 0 )
149 150
    {
        conn->i_ref_count++;
151
        vlc_mutex_unlock( &vlc_va_conn_lock );
152
        return conn;
153
    }
154

155
    /* Create a VA display */
156
    conn->p_display = vaGetDisplay(display);
157 158 159
    if( !conn->p_display )
        goto error;

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

163
    conn->i_ref_count++;
164
    vlc_mutex_unlock( &vlc_va_conn_lock );
165
    return conn;
166 167

error:
168
    vlc_mutex_unlock( &vlc_va_conn_lock );
169
    return NULL;
170 171
}

172 173
void vlc_va_Terminate( vlc_va_conn_t *conn )
{
174
    vlc_mutex_lock( &vlc_va_conn_lock );
175
    assert( conn );
176
    assert( conn->i_ref_count > 0 );
177

178
    conn->i_ref_count--;
179 180 181 182
    if( conn->i_ref_count == 0 )
    {
        assert( conn->i_surface_count == 0 );
        assert( conn->p_surface_ids == NULL );
183

184
        assert( conn->p_display );
185
        vaTerminate( conn->p_display );
186

187 188 189 190 191 192 193 194
        /* Reset values */
        conn->p_display = 0;
        conn->i_version_major = conn->i_version_minor = 0;
        conn->i_ref_count = 0;
        conn = NULL;
    }

    vlc_mutex_unlock( &vlc_va_conn_lock );
195 196
}

197
#endif