2013-07-29 05:34:06 +00:00
|
|
|
/*
|
|
|
|
* gstvaapiencoder.c - VA encoder abstraction
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Intel Corporation
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
2013-12-04 16:55:18 +00:00
|
|
|
|
2013-07-29 05:34:06 +00:00
|
|
|
#include "sysdeps.h"
|
|
|
|
#include "gstvaapicompat.h"
|
|
|
|
#include "gstvaapiencoder.h"
|
|
|
|
#include "gstvaapiencoder_priv.h"
|
|
|
|
#include "gstvaapicontext.h"
|
|
|
|
#include "gstvaapidisplay_priv.h"
|
|
|
|
|
|
|
|
#define DEBUG 1
|
|
|
|
#include "gstvaapidebug.h"
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/**
|
|
|
|
* gst_vaapi_encoder_ref:
|
|
|
|
* @encoder: a #GstVaapiEncoder
|
|
|
|
*
|
|
|
|
* Atomically increases the reference count of the given @encoder by one.
|
|
|
|
*
|
|
|
|
* Returns: The same @encoder argument
|
|
|
|
*/
|
2013-07-29 05:34:06 +00:00
|
|
|
GstVaapiEncoder *
|
|
|
|
gst_vaapi_encoder_ref (GstVaapiEncoder * encoder)
|
|
|
|
{
|
|
|
|
return gst_vaapi_object_ref (encoder);
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/**
|
|
|
|
* gst_vaapi_encoder_unref:
|
|
|
|
* @encoder: a #GstVaapiEncoder
|
|
|
|
*
|
|
|
|
* Atomically decreases the reference count of the @encoder by one. If
|
|
|
|
* the reference count reaches zero, the encoder will be free'd.
|
|
|
|
*/
|
2013-07-29 05:34:06 +00:00
|
|
|
void
|
|
|
|
gst_vaapi_encoder_unref (GstVaapiEncoder * encoder)
|
|
|
|
{
|
|
|
|
gst_vaapi_object_unref (encoder);
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/**
|
|
|
|
* gst_vaapi_encoder_replace:
|
|
|
|
* @old_encoder_ptr: a pointer to a #GstVaapiEncoder
|
|
|
|
* @new_encoder: a #GstVaapiEncoder
|
|
|
|
*
|
|
|
|
* Atomically replaces the encoder encoder held in @old_encoder_ptr
|
|
|
|
* with @new_encoder. This means that @old_encoder_ptr shall reference
|
|
|
|
* a valid encoder. However, @new_encoder can be NULL.
|
|
|
|
*/
|
2013-07-29 05:34:06 +00:00
|
|
|
void
|
|
|
|
gst_vaapi_encoder_replace (GstVaapiEncoder ** old_encoder_ptr,
|
|
|
|
GstVaapiEncoder * new_encoder)
|
|
|
|
{
|
|
|
|
gst_vaapi_object_replace (old_encoder_ptr, new_encoder);
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/* Notifies gst_vaapi_encoder_create_coded_buffer() that a new buffer is free */
|
2013-07-29 05:34:06 +00:00
|
|
|
static void
|
2013-12-03 15:11:46 +00:00
|
|
|
_coded_buffer_proxy_released_notify (GstVaapiEncoder * encoder)
|
2013-07-29 05:34:06 +00:00
|
|
|
{
|
2013-12-04 16:05:17 +00:00
|
|
|
g_mutex_lock (&encoder->mutex);
|
|
|
|
g_cond_signal (&encoder->codedbuf_free);
|
|
|
|
g_mutex_unlock (&encoder->mutex);
|
2013-07-29 05:34:06 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/* Creates a new VA coded buffer object proxy, backed from a pool */
|
2013-12-03 15:11:46 +00:00
|
|
|
static GstVaapiCodedBufferProxy *
|
|
|
|
gst_vaapi_encoder_create_coded_buffer (GstVaapiEncoder * encoder)
|
2013-07-29 05:34:06 +00:00
|
|
|
{
|
2013-12-03 15:11:46 +00:00
|
|
|
GstVaapiCodedBufferPool *const pool =
|
|
|
|
GST_VAAPI_CODED_BUFFER_POOL (encoder->codedbuf_pool);
|
|
|
|
GstVaapiCodedBufferProxy *codedbuf_proxy;
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 16:05:17 +00:00
|
|
|
g_mutex_lock (&encoder->mutex);
|
2013-12-03 15:11:46 +00:00
|
|
|
do {
|
|
|
|
codedbuf_proxy = gst_vaapi_coded_buffer_proxy_new_from_pool (pool);
|
|
|
|
if (codedbuf_proxy)
|
|
|
|
break;
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-03 15:11:46 +00:00
|
|
|
/* Wait for a free coded buffer to become available */
|
2013-12-04 16:05:17 +00:00
|
|
|
g_cond_wait (&encoder->codedbuf_free, &encoder->mutex);
|
2013-12-03 15:11:46 +00:00
|
|
|
codedbuf_proxy = gst_vaapi_coded_buffer_proxy_new_from_pool (pool);
|
|
|
|
} while (0);
|
2013-12-04 16:05:17 +00:00
|
|
|
g_mutex_unlock (&encoder->mutex);
|
2013-12-03 15:11:46 +00:00
|
|
|
if (!codedbuf_proxy)
|
|
|
|
return NULL;
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-03 15:11:46 +00:00
|
|
|
gst_vaapi_coded_buffer_proxy_set_destroy_notify (codedbuf_proxy,
|
|
|
|
(GDestroyNotify)_coded_buffer_proxy_released_notify, encoder);
|
|
|
|
return codedbuf_proxy;
|
2013-07-29 05:34:06 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/* Notifies gst_vaapi_encoder_create_surface() that a new surface is free */
|
2013-07-29 05:34:06 +00:00
|
|
|
static void
|
|
|
|
_surface_proxy_released_notify (GstVaapiEncoder * encoder)
|
|
|
|
{
|
2013-12-04 16:05:17 +00:00
|
|
|
g_mutex_lock (&encoder->mutex);
|
|
|
|
g_cond_signal (&encoder->surface_free);
|
|
|
|
g_mutex_unlock (&encoder->mutex);
|
2013-07-29 05:34:06 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/* Creates a new VA surface object proxy, backed from a pool and
|
|
|
|
useful to allocate reconstructed surfaces */
|
2013-07-29 05:34:06 +00:00
|
|
|
GstVaapiSurfaceProxy *
|
|
|
|
gst_vaapi_encoder_create_surface (GstVaapiEncoder * encoder)
|
|
|
|
{
|
|
|
|
GstVaapiSurfaceProxy *proxy;
|
|
|
|
|
2013-12-04 16:05:17 +00:00
|
|
|
g_return_val_if_fail (encoder->context != NULL, NULL);
|
|
|
|
|
|
|
|
g_mutex_lock (&encoder->mutex);
|
|
|
|
for (;;) {
|
|
|
|
proxy = gst_vaapi_context_get_surface_proxy (encoder->context);
|
|
|
|
if (proxy)
|
|
|
|
break;
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 16:05:17 +00:00
|
|
|
/* Wait for a free surface proxy to become available */
|
|
|
|
g_cond_wait (&encoder->surface_free, &encoder->mutex);
|
2013-07-29 05:34:06 +00:00
|
|
|
}
|
2013-12-04 16:05:17 +00:00
|
|
|
g_mutex_unlock (&encoder->mutex);
|
2013-07-29 05:34:06 +00:00
|
|
|
|
|
|
|
gst_vaapi_surface_proxy_set_destroy_notify (proxy,
|
|
|
|
(GDestroyNotify) _surface_proxy_released_notify, encoder);
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/**
|
|
|
|
* gst_vaapi_encoder_put_frame:
|
|
|
|
* @encoder: a #GstVaapiEncoder
|
|
|
|
* @frame: a #GstVideoCodecFrame
|
|
|
|
*
|
|
|
|
* Queues a #GstVideoCodedFrame to the HW encoder. The encoder holds
|
|
|
|
* an extra reference to the @frame.
|
|
|
|
*
|
|
|
|
* Return value: a #GstVaapiEncoderStatus
|
|
|
|
*/
|
2013-07-29 05:34:06 +00:00
|
|
|
GstVaapiEncoderStatus
|
|
|
|
gst_vaapi_encoder_put_frame (GstVaapiEncoder * encoder,
|
|
|
|
GstVideoCodecFrame * frame)
|
|
|
|
{
|
2013-12-04 16:05:17 +00:00
|
|
|
GstVaapiEncoderClass *const klass = GST_VAAPI_ENCODER_GET_CLASS (encoder);
|
|
|
|
GstVaapiEncoderStatus status;
|
|
|
|
GstVaapiEncPicture *picture;
|
|
|
|
GstVaapiCodedBufferProxy *codedbuf_proxy;
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 16:05:17 +00:00
|
|
|
for (;;) {
|
|
|
|
picture = NULL;
|
2013-12-04 16:55:18 +00:00
|
|
|
status = klass->reordering (encoder, frame, &picture);
|
2013-12-04 16:05:17 +00:00
|
|
|
if (status == GST_VAAPI_ENCODER_STATUS_NO_SURFACE)
|
|
|
|
break;
|
|
|
|
if (status != GST_VAAPI_ENCODER_STATUS_SUCCESS)
|
|
|
|
goto error_reorder_frame;
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 16:05:17 +00:00
|
|
|
codedbuf_proxy = gst_vaapi_encoder_create_coded_buffer (encoder);
|
|
|
|
if (!codedbuf_proxy)
|
|
|
|
goto error_create_coded_buffer;
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 16:05:17 +00:00
|
|
|
status = klass->encode (encoder, picture, codedbuf_proxy);
|
|
|
|
if (status != GST_VAAPI_ENCODER_STATUS_SUCCESS)
|
|
|
|
goto error_encode;
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 16:05:17 +00:00
|
|
|
gst_vaapi_coded_buffer_proxy_set_user_data (codedbuf_proxy,
|
|
|
|
picture, (GDestroyNotify) gst_vaapi_enc_picture_unref);
|
|
|
|
g_async_queue_push (encoder->codedbuf_queue, codedbuf_proxy);
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 16:05:17 +00:00
|
|
|
/* Try again with any pending reordered frame now available for encoding */
|
|
|
|
frame = NULL;
|
|
|
|
}
|
|
|
|
return GST_VAAPI_ENCODER_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
error_reorder_frame:
|
|
|
|
{
|
|
|
|
GST_ERROR ("failed to process reordered frames");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
error_create_coded_buffer:
|
|
|
|
{
|
|
|
|
GST_ERROR ("failed to allocate coded buffer");
|
|
|
|
gst_vaapi_enc_picture_unref (picture);
|
|
|
|
return GST_VAAPI_ENCODER_STATUS_ERROR_ALLOCATION_FAILED;
|
|
|
|
}
|
|
|
|
error_encode:
|
|
|
|
{
|
|
|
|
GST_ERROR ("failed to encode frame (status = %d)", status);
|
|
|
|
gst_vaapi_enc_picture_unref (picture);
|
|
|
|
gst_vaapi_coded_buffer_proxy_unref (codedbuf_proxy);
|
|
|
|
return status;
|
|
|
|
}
|
2013-07-29 05:34:06 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/**
|
|
|
|
* gst_vaapi_encoder_get_buffer_with_timeout:
|
|
|
|
* @encoder: a #GstVaapiEncoder
|
|
|
|
* @out_codedbuf_proxy_ptr: the next coded buffer as a #GstVaapiCodedBufferProxy
|
|
|
|
* @timeout: the number of microseconds to wait for the coded buffer, at most
|
|
|
|
*
|
|
|
|
* Upon successful return, *@out_codedbuf_proxy_ptr contains the next
|
|
|
|
* coded buffer as a #GstVaapiCodedBufferProxy. The caller owns this
|
|
|
|
* object, so gst_vaapi_coded_buffer_proxy_unref() shall be called
|
|
|
|
* after usage. Otherwise, @GST_VAAPI_DECODER_STATUS_ERROR_NO_BUFFER
|
|
|
|
* is returned if no coded buffer is available so far (timeout).
|
|
|
|
*
|
|
|
|
* The parent frame is available as a #GstVideoCodecFrame attached to
|
|
|
|
* the user-data anchor of the output coded buffer. Ownership of the
|
|
|
|
* frame is transferred to the coded buffer.
|
|
|
|
*
|
|
|
|
* Return value: a #GstVaapiEncoderStatus
|
|
|
|
*/
|
2013-07-29 05:34:06 +00:00
|
|
|
GstVaapiEncoderStatus
|
2013-12-04 16:55:18 +00:00
|
|
|
gst_vaapi_encoder_get_buffer_with_timeout (GstVaapiEncoder * encoder,
|
2013-12-04 16:05:17 +00:00
|
|
|
GstVaapiCodedBufferProxy ** out_codedbuf_proxy_ptr, guint64 timeout)
|
2013-07-29 05:34:06 +00:00
|
|
|
{
|
|
|
|
GstVaapiEncPicture *picture;
|
2013-12-04 16:05:17 +00:00
|
|
|
GstVaapiCodedBufferProxy *codedbuf_proxy;
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 16:05:17 +00:00
|
|
|
codedbuf_proxy = g_async_queue_timeout_pop (encoder->codedbuf_queue, timeout);
|
|
|
|
if (!codedbuf_proxy)
|
|
|
|
return GST_VAAPI_ENCODER_STATUS_NO_BUFFER;
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 16:05:17 +00:00
|
|
|
/* Wait for completion of all operations and report any error that occurred */
|
|
|
|
picture = gst_vaapi_coded_buffer_proxy_get_user_data (codedbuf_proxy);
|
|
|
|
if (!gst_vaapi_surface_sync (picture->surface))
|
|
|
|
goto error_invalid_buffer;
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
gst_vaapi_coded_buffer_proxy_set_user_data (codedbuf_proxy,
|
|
|
|
gst_video_codec_frame_ref (picture->frame),
|
|
|
|
(GDestroyNotify) gst_video_codec_frame_unref);
|
2013-12-04 16:05:17 +00:00
|
|
|
|
|
|
|
if (out_codedbuf_proxy_ptr)
|
|
|
|
*out_codedbuf_proxy_ptr = gst_vaapi_coded_buffer_proxy_ref (codedbuf_proxy);
|
|
|
|
gst_vaapi_coded_buffer_proxy_unref (codedbuf_proxy);
|
|
|
|
return GST_VAAPI_ENCODER_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
error_invalid_buffer:
|
|
|
|
{
|
|
|
|
GST_ERROR ("failed to encode the frame");
|
|
|
|
gst_vaapi_coded_buffer_proxy_unref (codedbuf_proxy);
|
|
|
|
return GST_VAAPI_ENCODER_STATUS_ERROR_INVALID_SURFACE;
|
2013-07-29 05:34:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/**
|
|
|
|
* gst_vaapi_encoder_flush:
|
|
|
|
* @encoder: a #GstVaapiEncoder
|
|
|
|
*
|
|
|
|
* Submits any pending (reordered) frame for encoding.
|
|
|
|
*
|
|
|
|
* Return value: a #GstVaapiEncoderStatus
|
|
|
|
*/
|
2013-07-29 05:34:06 +00:00
|
|
|
GstVaapiEncoderStatus
|
|
|
|
gst_vaapi_encoder_flush (GstVaapiEncoder * encoder)
|
|
|
|
{
|
|
|
|
GstVaapiEncoderClass *const klass = GST_VAAPI_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
2013-12-04 10:54:40 +00:00
|
|
|
return klass->flush (encoder);
|
2013-07-29 05:34:06 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/**
|
|
|
|
* gst_vaapi_encoder_get_codec_data:
|
|
|
|
* @encoder: a #GstVaapiEncoder
|
|
|
|
* @out_codec_data_ptr: the pointer to the resulting codec-data (#GstBuffer)
|
|
|
|
*
|
|
|
|
* Returns a codec-data buffer that best represents the encoded
|
|
|
|
* bitstream. Upon successful return, and if the @out_codec_data_ptr
|
|
|
|
* contents is not NULL, then the caller function shall deallocates
|
|
|
|
* that buffer with gst_buffer_unref().
|
|
|
|
*
|
|
|
|
* Return value: a #GstVaapiEncoderStatus
|
|
|
|
*/
|
2013-07-29 05:34:06 +00:00
|
|
|
GstVaapiEncoderStatus
|
|
|
|
gst_vaapi_encoder_get_codec_data (GstVaapiEncoder * encoder,
|
2013-12-04 16:55:18 +00:00
|
|
|
GstBuffer ** out_codec_data_ptr)
|
2013-07-29 05:34:06 +00:00
|
|
|
{
|
|
|
|
GstVaapiEncoderStatus ret = GST_VAAPI_ENCODER_STATUS_SUCCESS;
|
|
|
|
GstVaapiEncoderClass *const klass = GST_VAAPI_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
*out_codec_data_ptr = NULL;
|
2013-07-29 05:34:06 +00:00
|
|
|
if (!klass->get_codec_data)
|
|
|
|
return GST_VAAPI_ENCODER_STATUS_SUCCESS;
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
ret = klass->get_codec_data (encoder, out_codec_data_ptr);
|
2013-07-29 05:34:06 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/* Ensures the underlying VA context for encoding is created */
|
2013-07-29 05:34:06 +00:00
|
|
|
static gboolean
|
|
|
|
gst_vaapi_encoder_ensure_context (GstVaapiEncoder * encoder)
|
|
|
|
{
|
|
|
|
GstVaapiEncoderClass *const klass = GST_VAAPI_ENCODER_GET_CLASS (encoder);
|
|
|
|
GstVaapiContextInfo info;
|
|
|
|
GstVaapiContext *context;
|
|
|
|
|
|
|
|
if (GST_VAAPI_ENCODER_CONTEXT (encoder))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
memset (&info, 0, sizeof (info));
|
2013-12-04 10:54:40 +00:00
|
|
|
if (!klass->get_context_info (encoder, &info))
|
2013-07-29 05:34:06 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
context = gst_vaapi_context_new_full (GST_VAAPI_ENCODER_DISPLAY (encoder),
|
|
|
|
&info);
|
|
|
|
if (!context)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
GST_VAAPI_ENCODER_CONTEXT (encoder) = context;
|
|
|
|
GST_VAAPI_ENCODER_VA_CONTEXT (encoder) = gst_vaapi_context_get_id (context);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/**
|
|
|
|
* gst_vaapi_encoder_set_format:
|
|
|
|
* @encoder: a #GstVaapiEncoder
|
|
|
|
* @state : a #GstVideoCodecState
|
|
|
|
* @ref_caps: the set of reference caps (from pad template)
|
|
|
|
*
|
|
|
|
* Notifies the encoder of incoming data format (video resolution),
|
|
|
|
* and additional information like framerate.
|
|
|
|
*
|
|
|
|
* Return value: the newly allocated set of caps
|
|
|
|
*/
|
2013-07-29 05:34:06 +00:00
|
|
|
GstCaps *
|
|
|
|
gst_vaapi_encoder_set_format (GstVaapiEncoder * encoder,
|
2013-12-04 16:55:18 +00:00
|
|
|
GstVideoCodecState * state, GstCaps * ref_caps)
|
2013-07-29 05:34:06 +00:00
|
|
|
{
|
|
|
|
GstVaapiEncoderClass *const klass = GST_VAAPI_ENCODER_GET_CLASS (encoder);
|
|
|
|
GstCaps *out_caps = NULL;
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
if (!GST_VIDEO_INFO_WIDTH (&state->info) ||
|
|
|
|
!GST_VIDEO_INFO_HEIGHT (&state->info)) {
|
2013-07-29 05:34:06 +00:00
|
|
|
GST_WARNING ("encoder set format failed, width or height equal to 0.");
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-12-04 16:55:18 +00:00
|
|
|
GST_VAAPI_ENCODER_VIDEO_INFO (encoder) = state->info;
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
out_caps = klass->set_format (encoder, state, ref_caps);
|
2013-07-29 05:34:06 +00:00
|
|
|
if (!out_caps)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (GST_VAAPI_ENCODER_CAPS (encoder) &&
|
|
|
|
gst_caps_is_equal (out_caps, GST_VAAPI_ENCODER_CAPS (encoder))) {
|
|
|
|
gst_caps_unref (out_caps);
|
|
|
|
return GST_VAAPI_ENCODER_CAPS (encoder);
|
|
|
|
}
|
|
|
|
gst_caps_replace (&GST_VAAPI_ENCODER_CAPS (encoder), out_caps);
|
|
|
|
g_assert (GST_VAAPI_ENCODER_CONTEXT (encoder) == NULL);
|
|
|
|
gst_vaapi_object_replace (&GST_VAAPI_ENCODER_CONTEXT (encoder), NULL);
|
|
|
|
|
|
|
|
if (!gst_vaapi_encoder_ensure_context (encoder))
|
|
|
|
goto error;
|
|
|
|
|
2013-12-03 15:11:46 +00:00
|
|
|
encoder->codedbuf_size = (GST_VAAPI_ENCODER_WIDTH (encoder) *
|
2013-07-29 05:34:06 +00:00
|
|
|
GST_VAAPI_ENCODER_HEIGHT (encoder) * 400) / (16 * 16);
|
|
|
|
|
2013-12-03 15:11:46 +00:00
|
|
|
encoder->codedbuf_pool = gst_vaapi_coded_buffer_pool_new (encoder,
|
|
|
|
encoder->codedbuf_size);
|
|
|
|
if (!encoder->codedbuf_pool) {
|
|
|
|
GST_ERROR ("failed to initialized coded buffer pool");
|
2013-07-29 05:34:06 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2013-12-03 15:11:46 +00:00
|
|
|
gst_vaapi_video_pool_set_capacity (encoder->codedbuf_pool, 5);
|
2013-07-29 05:34:06 +00:00
|
|
|
|
|
|
|
return out_caps;
|
|
|
|
|
|
|
|
error:
|
|
|
|
gst_caps_replace (&GST_VAAPI_ENCODER_CAPS (encoder), NULL);
|
|
|
|
gst_caps_replace (&out_caps, NULL);
|
|
|
|
GST_ERROR ("encoder set format failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/* Base encoder initialization (internal) */
|
2013-07-29 05:34:06 +00:00
|
|
|
static gboolean
|
|
|
|
gst_vaapi_encoder_init (GstVaapiEncoder * encoder, GstVaapiDisplay * display)
|
|
|
|
{
|
2013-12-04 10:54:40 +00:00
|
|
|
GstVaapiEncoderClass *const klass = GST_VAAPI_ENCODER_GET_CLASS (encoder);
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 10:54:40 +00:00
|
|
|
g_return_val_if_fail (display != NULL, FALSE);
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 10:54:40 +00:00
|
|
|
#define CHECK_VTABLE_HOOK(FUNC) do { \
|
|
|
|
if (!klass->FUNC) \
|
|
|
|
goto error_invalid_vtable; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
CHECK_VTABLE_HOOK (init);
|
|
|
|
CHECK_VTABLE_HOOK (finalize);
|
|
|
|
CHECK_VTABLE_HOOK (encode);
|
|
|
|
CHECK_VTABLE_HOOK (reordering);
|
|
|
|
CHECK_VTABLE_HOOK (flush);
|
|
|
|
CHECK_VTABLE_HOOK (get_context_info);
|
|
|
|
CHECK_VTABLE_HOOK (set_format);
|
|
|
|
|
|
|
|
#undef CHECK_VTABLE_HOOK
|
2013-07-29 05:34:06 +00:00
|
|
|
|
|
|
|
encoder->display = gst_vaapi_display_ref (display);
|
|
|
|
encoder->va_display = gst_vaapi_display_get_display (display);
|
|
|
|
encoder->va_context = VA_INVALID_ID;
|
|
|
|
|
|
|
|
gst_video_info_init (&encoder->video_info);
|
|
|
|
|
2013-12-04 16:05:17 +00:00
|
|
|
g_mutex_init (&encoder->mutex);
|
2013-07-29 05:34:06 +00:00
|
|
|
g_cond_init (&encoder->surface_free);
|
2013-12-04 16:05:17 +00:00
|
|
|
g_cond_init (&encoder->codedbuf_free);
|
|
|
|
|
|
|
|
encoder->codedbuf_queue = g_async_queue_new_full ((GDestroyNotify)
|
|
|
|
gst_vaapi_coded_buffer_proxy_unref);
|
|
|
|
if (!encoder->codedbuf_queue)
|
|
|
|
return FALSE;
|
2013-07-29 05:34:06 +00:00
|
|
|
|
2013-12-04 10:54:40 +00:00
|
|
|
return klass->init (encoder);
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
error_invalid_vtable:
|
|
|
|
{
|
|
|
|
GST_ERROR ("invalid subclass hook (internal error)");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-07-29 05:34:06 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/* Base encoder cleanup (internal) */
|
2013-12-04 10:54:40 +00:00
|
|
|
void
|
|
|
|
gst_vaapi_encoder_finalize (GstVaapiEncoder * encoder)
|
2013-07-29 05:34:06 +00:00
|
|
|
{
|
|
|
|
GstVaapiEncoderClass *const klass = GST_VAAPI_ENCODER_GET_CLASS (encoder);
|
|
|
|
|
2013-12-04 10:54:40 +00:00
|
|
|
klass->finalize (encoder);
|
2013-07-29 05:34:06 +00:00
|
|
|
|
|
|
|
gst_vaapi_object_replace (&encoder->context, NULL);
|
|
|
|
gst_vaapi_display_replace (&encoder->display, NULL);
|
|
|
|
encoder->va_display = NULL;
|
2013-12-04 16:05:17 +00:00
|
|
|
|
|
|
|
gst_vaapi_video_pool_replace (&encoder->codedbuf_pool, NULL);
|
|
|
|
if (encoder->codedbuf_queue) {
|
|
|
|
g_async_queue_unref (encoder->codedbuf_queue);
|
|
|
|
encoder->codedbuf_queue = NULL;
|
|
|
|
}
|
2013-07-29 05:34:06 +00:00
|
|
|
g_cond_clear (&encoder->surface_free);
|
2013-12-04 16:05:17 +00:00
|
|
|
g_cond_clear (&encoder->codedbuf_free);
|
|
|
|
g_mutex_clear (&encoder->mutex);
|
2013-07-29 05:34:06 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 16:55:18 +00:00
|
|
|
/* Helper function to create new GstVaapiEncoder instances (internal) */
|
2013-07-29 05:34:06 +00:00
|
|
|
GstVaapiEncoder *
|
|
|
|
gst_vaapi_encoder_new (const GstVaapiEncoderClass * klass,
|
|
|
|
GstVaapiDisplay * display)
|
|
|
|
{
|
|
|
|
GstVaapiEncoder *encoder;
|
|
|
|
|
|
|
|
encoder = (GstVaapiEncoder *)
|
|
|
|
gst_vaapi_mini_object_new0 (GST_VAAPI_MINI_OBJECT_CLASS (klass));
|
|
|
|
if (!encoder)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!gst_vaapi_encoder_init (encoder, display))
|
|
|
|
goto error;
|
|
|
|
return encoder;
|
|
|
|
|
|
|
|
error:
|
|
|
|
gst_vaapi_encoder_unref (encoder);
|
|
|
|
return NULL;
|
|
|
|
}
|