2016-11-07 20:32:38 +00:00
|
|
|
/* GStreamer Intel MSDK plugin
|
|
|
|
* Copyright (c) 2016, Intel Corporation
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "gstmsdkdec.h"
|
2018-02-13 22:49:28 +00:00
|
|
|
#include "gstmsdkbufferpool.h"
|
|
|
|
#include "gstmsdkvideomemory.h"
|
|
|
|
#include "gstmsdksystemmemory.h"
|
2018-02-13 22:51:18 +00:00
|
|
|
#include "gstmsdkcontextutil.h"
|
2016-11-07 20:32:38 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_EXTERN (gst_msdkdec_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_msdkdec_debug
|
|
|
|
|
|
|
|
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2019-08-26 05:54:13 +00:00
|
|
|
GST_STATIC_CAPS (GST_MSDK_CAPS_STR ("NV12", "NV12"))
|
2016-11-07 20:32:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
#define PROP_HARDWARE_DEFAULT TRUE
|
2018-05-07 22:11:14 +00:00
|
|
|
#define PROP_ASYNC_DEPTH_DEFAULT 1
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
#define IS_ALIGNED(i, n) (((i) & ((n)-1)) == 0)
|
|
|
|
|
2019-11-06 12:45:09 +00:00
|
|
|
#define GST_TO_MFX_TIME(time) ((time) == GST_CLOCK_TIME_NONE ? \
|
|
|
|
MFX_TIMESTAMP_UNKNOWN : gst_util_uint64_scale_round ((time), 9, 100000))
|
|
|
|
|
|
|
|
#define MFX_TIME_IS_VALID(time) ((time) != MFX_TIMESTAMP_UNKNOWN)
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
#define gst_msdkdec_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE (GstMsdkDec, gst_msdkdec, GST_TYPE_VIDEO_DECODER);
|
|
|
|
|
|
|
|
typedef struct _MsdkSurface
|
|
|
|
{
|
2018-02-13 22:49:28 +00:00
|
|
|
mfxFrameSurface1 *surface;
|
|
|
|
GstBuffer *buf;
|
2016-11-07 20:32:38 +00:00
|
|
|
GstVideoFrame data;
|
|
|
|
GstVideoFrame copy;
|
|
|
|
} MsdkSurface;
|
|
|
|
|
2020-05-14 03:03:49 +00:00
|
|
|
struct _MsdkDecTask
|
|
|
|
{
|
|
|
|
MsdkSurface *surface;
|
|
|
|
mfxSyncPoint sync_point;
|
|
|
|
|
|
|
|
gboolean decode_only;
|
|
|
|
};
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
static gboolean gst_msdkdec_drain (GstVideoDecoder * decoder);
|
2018-03-08 20:38:52 +00:00
|
|
|
static gboolean gst_msdkdec_flush (GstVideoDecoder * decoder);
|
2018-07-03 00:17:49 +00:00
|
|
|
static gboolean gst_msdkdec_negotiate (GstMsdkDec * thiz, gboolean hard_reset);
|
2018-03-08 20:38:52 +00:00
|
|
|
|
2019-12-06 04:02:50 +00:00
|
|
|
void
|
|
|
|
gst_msdkdec_add_bs_extra_param (GstMsdkDec * thiz, mfxExtBuffer * param)
|
|
|
|
{
|
|
|
|
if (thiz->num_bs_extra_params < MAX_BS_EXTRA_PARAMS) {
|
|
|
|
thiz->bs_extra_params[thiz->num_bs_extra_params] = param;
|
|
|
|
thiz->num_bs_extra_params++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 09:05:09 +00:00
|
|
|
void
|
|
|
|
gst_msdkdec_add_video_extra_param (GstMsdkDec * thiz, mfxExtBuffer * param)
|
|
|
|
{
|
|
|
|
if (thiz->num_video_extra_params < MAX_VIDEO_EXTRA_PARAMS) {
|
|
|
|
thiz->video_extra_params[thiz->num_video_extra_params] = param;
|
|
|
|
thiz->num_video_extra_params++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 00:42:20 +00:00
|
|
|
static GstVideoCodecFrame *
|
|
|
|
gst_msdkdec_get_oldest_frame (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstVideoCodecFrame *frame = NULL, *old_frame = NULL;
|
|
|
|
GList *frames, *l;
|
|
|
|
gint count = 0;
|
|
|
|
|
|
|
|
frames = gst_video_decoder_get_frames (decoder);
|
|
|
|
|
|
|
|
for (l = frames; l != NULL; l = l->next) {
|
|
|
|
GstVideoCodecFrame *f = l->data;
|
|
|
|
|
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (f->pts)) {
|
|
|
|
GST_INFO
|
|
|
|
("Frame doesn't have a valid pts yet, Use gst_video_decoder_get_oldest_frame()"
|
|
|
|
"with out considering the PTS for selecting the frame to be finished");
|
|
|
|
old_frame = gst_video_decoder_get_oldest_frame (decoder);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!frame || frame->pts > f->pts)
|
|
|
|
frame = f;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_frame)
|
|
|
|
frame = old_frame;
|
|
|
|
|
|
|
|
if (frame) {
|
|
|
|
GST_LOG_OBJECT (decoder,
|
|
|
|
"Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
|
|
|
|
frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
|
|
|
|
gst_video_codec_frame_ref (frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_frame)
|
|
|
|
gst_video_codec_frame_unref (old_frame);
|
|
|
|
|
|
|
|
g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2020-05-14 03:03:49 +00:00
|
|
|
static inline void
|
|
|
|
free_surface (MsdkSurface * s)
|
|
|
|
{
|
|
|
|
gst_buffer_unref (s->buf);
|
|
|
|
g_slice_free (MsdkSurface, s);
|
|
|
|
}
|
|
|
|
|
2019-12-23 06:09:25 +00:00
|
|
|
static void
|
2020-05-14 03:03:49 +00:00
|
|
|
unmap_frame (GstMsdkDec * thiz, MsdkSurface * s)
|
2019-12-23 06:09:25 +00:00
|
|
|
{
|
|
|
|
if (s->copy.buffer) {
|
2020-05-14 03:03:49 +00:00
|
|
|
/* we allocate this buffer from down stream, we need ref-1 for it */
|
2019-12-23 06:09:25 +00:00
|
|
|
gst_buffer_unref (s->copy.buffer);
|
2020-05-14 03:03:49 +00:00
|
|
|
gst_video_frame_unmap (&s->copy);
|
|
|
|
s->copy.buffer = NULL;
|
2019-12-23 06:09:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 03:03:49 +00:00
|
|
|
if (s->data.buffer) {
|
2019-12-23 06:09:25 +00:00
|
|
|
gst_video_frame_unmap (&s->data);
|
2020-05-14 03:03:49 +00:00
|
|
|
s->data.buffer = NULL;
|
|
|
|
}
|
2019-12-23 06:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-05-14 03:03:49 +00:00
|
|
|
gst_msdkdec_free_unlocked_msdk_surfaces (GstMsdkDec * thiz)
|
2019-12-23 06:09:25 +00:00
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
MsdkSurface *surface;
|
|
|
|
|
2020-05-14 03:03:49 +00:00
|
|
|
for (l = thiz->locked_msdk_surfaces; l;) {
|
|
|
|
GList *next = l->next;
|
2019-12-23 06:09:25 +00:00
|
|
|
surface = l->data;
|
2020-05-14 03:03:49 +00:00
|
|
|
if (surface->surface->Data.Locked == 0) {
|
|
|
|
unmap_frame (thiz, surface);
|
|
|
|
free_surface (surface);
|
|
|
|
thiz->locked_msdk_surfaces =
|
|
|
|
g_list_delete_link (thiz->locked_msdk_surfaces, l);
|
|
|
|
}
|
|
|
|
l = next;
|
2019-12-23 06:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 19:11:50 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
allocate_output_buffer (GstMsdkDec * thiz, GstBuffer ** buffer)
|
|
|
|
{
|
|
|
|
GstFlowReturn flow;
|
|
|
|
GstVideoCodecFrame *frame;
|
|
|
|
GstVideoDecoder *decoder = GST_VIDEO_DECODER (thiz);
|
|
|
|
|
2018-07-03 00:42:20 +00:00
|
|
|
frame = gst_msdkdec_get_oldest_frame (decoder);
|
2017-01-20 19:11:50 +00:00
|
|
|
if (!frame) {
|
|
|
|
if (GST_PAD_IS_FLUSHING (decoder->srcpad))
|
|
|
|
return GST_FLOW_FLUSHING;
|
|
|
|
else
|
2019-08-21 08:44:39 +00:00
|
|
|
return GST_FLOW_CUSTOM_SUCCESS;
|
2017-01-20 19:11:50 +00:00
|
|
|
}
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2017-01-20 19:11:50 +00:00
|
|
|
if (!frame->output_buffer) {
|
2019-12-23 06:09:25 +00:00
|
|
|
/* Free un-unsed msdk surfaces firstly, hence the associated mfx
|
|
|
|
* surfaces will be moved from used list to available list */
|
2020-05-14 03:03:49 +00:00
|
|
|
gst_msdkdec_free_unlocked_msdk_surfaces (thiz);
|
2019-12-23 06:09:25 +00:00
|
|
|
|
2017-01-20 19:11:50 +00:00
|
|
|
flow = gst_video_decoder_allocate_output_frame (decoder, frame);
|
|
|
|
if (flow != GST_FLOW_OK) {
|
|
|
|
gst_video_codec_frame_unref (frame);
|
|
|
|
return flow;
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2017-01-20 19:11:50 +00:00
|
|
|
*buffer = gst_buffer_ref (frame->output_buffer);
|
|
|
|
gst_buffer_replace (&frame->output_buffer, NULL);
|
|
|
|
gst_video_codec_frame_unref (frame);
|
2019-09-26 01:52:14 +00:00
|
|
|
|
2017-01-20 19:11:50 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
static MsdkSurface *
|
2017-01-20 19:11:50 +00:00
|
|
|
get_surface (GstMsdkDec * thiz, GstBuffer * buffer)
|
2016-11-07 20:32:38 +00:00
|
|
|
{
|
|
|
|
MsdkSurface *i;
|
2019-07-22 16:52:53 +00:00
|
|
|
GstVideoCodecState *output_state = NULL;
|
|
|
|
gboolean success;
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
i = g_slice_new0 (MsdkSurface);
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
if (gst_msdk_is_msdk_buffer (buffer)) {
|
|
|
|
i->surface = gst_msdk_get_surface_from_buffer (buffer);
|
|
|
|
i->buf = buffer;
|
2016-11-07 20:32:38 +00:00
|
|
|
} else {
|
2018-02-13 22:49:28 +00:00
|
|
|
/* Confirm to activate the side pool */
|
|
|
|
if (!gst_buffer_pool_is_active (thiz->pool) &&
|
|
|
|
!gst_buffer_pool_set_active (thiz->pool, TRUE)) {
|
|
|
|
g_slice_free (MsdkSurface, i);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
if (!gst_video_frame_map (&i->copy, &thiz->non_msdk_pool_info, buffer,
|
2016-11-07 20:32:38 +00:00
|
|
|
GST_MAP_WRITE))
|
|
|
|
goto failed_unref_buffer;
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
if (gst_buffer_pool_acquire_buffer (thiz->pool, &buffer,
|
|
|
|
NULL) != GST_FLOW_OK)
|
|
|
|
goto failed_unmap_copy;
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2018-03-08 20:37:52 +00:00
|
|
|
i->surface = gst_msdk_get_surface_from_buffer (buffer);
|
|
|
|
i->buf = buffer;
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2019-07-22 16:52:53 +00:00
|
|
|
output_state =
|
|
|
|
gst_video_decoder_get_output_state (GST_VIDEO_DECODER (thiz));
|
|
|
|
success =
|
|
|
|
gst_video_frame_map (&i->data, &output_state->info, buffer,
|
|
|
|
GST_MAP_READWRITE);
|
|
|
|
gst_video_codec_state_unref (output_state);
|
|
|
|
if (!success)
|
2016-11-07 20:32:38 +00:00
|
|
|
goto failed_unref_buffer2;
|
|
|
|
}
|
|
|
|
|
2021-11-17 09:05:09 +00:00
|
|
|
if (!thiz->sfc)
|
|
|
|
gst_msdk_update_mfx_frame_info_from_mfx_video_param (&i->surface->Info,
|
|
|
|
&thiz->param);
|
2020-05-14 03:03:49 +00:00
|
|
|
|
|
|
|
thiz->locked_msdk_surfaces = g_list_append (thiz->locked_msdk_surfaces, i);
|
2016-11-07 20:32:38 +00:00
|
|
|
return i;
|
|
|
|
|
|
|
|
failed_unref_buffer2:
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
buffer = i->data.buffer;
|
|
|
|
failed_unmap_copy:
|
|
|
|
gst_video_frame_unmap (&i->copy);
|
|
|
|
failed_unref_buffer:
|
|
|
|
gst_buffer_unref (buffer);
|
2018-02-13 22:49:28 +00:00
|
|
|
g_slice_free (MsdkSurface, i);
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
GST_ERROR_OBJECT (thiz, "failed to handle buffer");
|
2016-11-07 20:32:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-07-03 00:17:49 +00:00
|
|
|
gst_msdkdec_close_decoder (GstMsdkDec * thiz, gboolean reset_param)
|
2016-11-07 20:32:38 +00:00
|
|
|
{
|
|
|
|
mfxStatus status;
|
|
|
|
|
2018-02-13 22:51:18 +00:00
|
|
|
if (!thiz->context || !thiz->initialized)
|
2016-11-07 20:32:38 +00:00
|
|
|
return;
|
|
|
|
|
2018-02-13 22:51:18 +00:00
|
|
|
GST_DEBUG_OBJECT (thiz, "Closing decoder with context %" GST_PTR_FORMAT,
|
|
|
|
thiz->context);
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
if (thiz->use_video_memory)
|
|
|
|
gst_msdk_frame_free (thiz->context, &thiz->alloc_resp);
|
|
|
|
|
2018-02-13 21:41:28 +00:00
|
|
|
status = MFXVideoDECODE_Close (gst_msdk_context_get_session (thiz->context));
|
2016-11-07 20:32:38 +00:00
|
|
|
if (status != MFX_ERR_NONE && status != MFX_ERR_NOT_INITIALIZED) {
|
|
|
|
GST_WARNING_OBJECT (thiz, "Decoder close failed (%s)",
|
|
|
|
msdk_status_to_string (status));
|
|
|
|
}
|
|
|
|
|
|
|
|
g_array_set_size (thiz->tasks, 0);
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
if (reset_param)
|
|
|
|
memset (&thiz->param, 0, sizeof (thiz->param));
|
|
|
|
|
2019-12-06 04:02:50 +00:00
|
|
|
thiz->num_bs_extra_params = 0;
|
2021-11-17 09:05:09 +00:00
|
|
|
thiz->num_video_extra_params = 0;
|
2018-02-13 22:51:18 +00:00
|
|
|
thiz->initialized = FALSE;
|
2018-02-13 23:41:20 +00:00
|
|
|
gst_adapter_clear (thiz->adapter);
|
2018-02-13 22:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_msdkdec_set_context (GstElement * element, GstContext * context)
|
|
|
|
{
|
|
|
|
GstMsdkContext *msdk_context = NULL;
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (element);
|
|
|
|
|
|
|
|
if (gst_msdk_context_get_context (context, &msdk_context)) {
|
|
|
|
gst_object_replace ((GstObject **) & thiz->context,
|
|
|
|
(GstObject *) msdk_context);
|
|
|
|
gst_object_unref (msdk_context);
|
2021-08-17 13:51:58 +00:00
|
|
|
} else if (gst_msdk_context_from_external_display (context,
|
|
|
|
thiz->hardware, 0 /* GST_MSDK_JOB_DECODER will be set later */ ,
|
|
|
|
&msdk_context)) {
|
|
|
|
gst_object_replace ((GstObject **) & thiz->context,
|
|
|
|
(GstObject *) msdk_context);
|
|
|
|
gst_object_unref (msdk_context);
|
2018-02-13 22:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
|
2016-11-07 20:32:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_init_decoder (GstMsdkDec * thiz)
|
|
|
|
{
|
2019-02-22 08:20:16 +00:00
|
|
|
GstMsdkDecClass *klass = GST_MSDKDEC_GET_CLASS (thiz);
|
2021-11-17 09:05:09 +00:00
|
|
|
GstVideoInfo *info, *output_info;
|
2016-11-07 20:32:38 +00:00
|
|
|
mfxSession session;
|
|
|
|
mfxStatus status;
|
|
|
|
mfxFrameAllocRequest request;
|
2021-11-17 09:05:09 +00:00
|
|
|
#if (MFX_VERSION >= 1022)
|
|
|
|
mfxExtDecVideoProcessing ext_dec_video_proc;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
GstVideoCodecState *output_state =
|
|
|
|
gst_video_decoder_get_output_state (GST_VIDEO_DECODER (thiz));
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2018-02-13 22:51:18 +00:00
|
|
|
if (thiz->initialized)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (!thiz->context) {
|
|
|
|
GST_WARNING_OBJECT (thiz, "No MSDK Context");
|
2016-11-07 20:32:38 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-02-13 22:51:18 +00:00
|
|
|
if (!thiz->input_state) {
|
|
|
|
GST_DEBUG_OBJECT (thiz, "Have no input state yet");
|
2016-11-07 20:32:38 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2018-02-13 22:51:18 +00:00
|
|
|
info = &thiz->input_state->info;
|
2021-11-17 09:05:09 +00:00
|
|
|
output_info = &output_state->info;
|
2016-11-07 20:32:38 +00:00
|
|
|
|
|
|
|
GST_OBJECT_LOCK (thiz);
|
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
if (thiz->use_video_memory) {
|
|
|
|
gst_msdk_set_frame_allocator (thiz->context);
|
|
|
|
thiz->param.IOPattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
|
|
|
|
} else {
|
|
|
|
thiz->param.IOPattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
|
|
|
|
}
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
GST_INFO_OBJECT (thiz, "This MSDK decoder uses %s memory",
|
|
|
|
thiz->use_video_memory ? "video" : "system");
|
|
|
|
|
|
|
|
thiz->param.AsyncDepth = thiz->async_depth;
|
2018-03-29 20:41:48 +00:00
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
/* We expect msdk to fill the width and height values */
|
|
|
|
g_return_val_if_fail (thiz->param.mfx.FrameInfo.Width
|
|
|
|
&& thiz->param.mfx.FrameInfo.Height, FALSE);
|
|
|
|
|
2019-02-22 08:20:16 +00:00
|
|
|
klass->preinit_decoder (thiz);
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* Set frame rate only if provided.
|
|
|
|
* If not, frame rate will be assumed inside the driver.
|
2018-07-03 00:17:49 +00:00
|
|
|
* Also we respect the upstream provided fps values */
|
|
|
|
if (info->fps_n > 0 && info->fps_d > 0
|
|
|
|
&& info->fps_n != thiz->param.mfx.FrameInfo.FrameRateExtN
|
|
|
|
&& info->fps_d != thiz->param.mfx.FrameInfo.FrameRateExtD) {
|
2018-03-29 20:41:48 +00:00
|
|
|
thiz->param.mfx.FrameInfo.FrameRateExtN = info->fps_n;
|
|
|
|
thiz->param.mfx.FrameInfo.FrameRateExtD = info->fps_d;
|
|
|
|
}
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
if (info->par_n && info->par_d && !thiz->param.mfx.FrameInfo.AspectRatioW
|
|
|
|
&& !thiz->param.mfx.FrameInfo.AspectRatioH) {
|
|
|
|
thiz->param.mfx.FrameInfo.AspectRatioW = info->par_n;
|
|
|
|
thiz->param.mfx.FrameInfo.AspectRatioH = info->par_d;
|
2016-11-07 20:32:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
thiz->param.mfx.FrameInfo.FourCC =
|
|
|
|
thiz->param.mfx.FrameInfo.FourCC ? thiz->param.mfx.
|
|
|
|
FrameInfo.FourCC : MFX_FOURCC_NV12;
|
|
|
|
thiz->param.mfx.FrameInfo.ChromaFormat =
|
|
|
|
thiz->param.mfx.FrameInfo.ChromaFormat ? thiz->param.mfx.
|
|
|
|
FrameInfo.ChromaFormat : MFX_CHROMAFORMAT_YUV420;
|
|
|
|
|
2021-11-17 09:05:09 +00:00
|
|
|
#if (MFX_VERSION >= 1022)
|
|
|
|
if (output_info && thiz->sfc) {
|
|
|
|
memset (&ext_dec_video_proc, 0, sizeof (ext_dec_video_proc));
|
|
|
|
ext_dec_video_proc.Header.BufferId = MFX_EXTBUFF_DEC_VIDEO_PROCESSING;
|
|
|
|
ext_dec_video_proc.Header.BufferSz = sizeof (ext_dec_video_proc);
|
|
|
|
ext_dec_video_proc.In.CropW = thiz->param.mfx.FrameInfo.CropW;
|
|
|
|
ext_dec_video_proc.In.CropH = thiz->param.mfx.FrameInfo.CropH;
|
|
|
|
ext_dec_video_proc.In.CropX = 0;
|
|
|
|
ext_dec_video_proc.In.CropY = 0;
|
|
|
|
ext_dec_video_proc.Out.FourCC =
|
|
|
|
gst_msdk_get_mfx_fourcc_from_format (output_info->finfo->format);
|
|
|
|
ext_dec_video_proc.Out.ChromaFormat =
|
|
|
|
gst_msdk_get_mfx_chroma_from_format (output_info->finfo->format);
|
|
|
|
ext_dec_video_proc.Out.Width = GST_ROUND_UP_16 (output_info->width);
|
|
|
|
ext_dec_video_proc.Out.Height = GST_ROUND_UP_32 (output_info->height);
|
|
|
|
ext_dec_video_proc.Out.CropW = output_info->width;
|
|
|
|
ext_dec_video_proc.Out.CropH = output_info->height;
|
|
|
|
ext_dec_video_proc.Out.CropX = 0;
|
|
|
|
ext_dec_video_proc.Out.CropY = 0;
|
|
|
|
gst_msdkdec_add_video_extra_param (thiz,
|
|
|
|
(mfxExtBuffer *) & ext_dec_video_proc);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (thiz->num_video_extra_params) {
|
|
|
|
thiz->param.NumExtParam = thiz->num_video_extra_params;
|
|
|
|
thiz->param.ExtParam = thiz->video_extra_params;
|
|
|
|
}
|
|
|
|
|
2018-02-13 21:41:28 +00:00
|
|
|
session = gst_msdk_context_get_session (thiz->context);
|
2021-08-24 09:59:59 +00:00
|
|
|
/* validate parameters and allow MFX to make adjustments */
|
2016-11-07 20:32:38 +00:00
|
|
|
status = MFXVideoDECODE_Query (session, &thiz->param, &thiz->param);
|
|
|
|
if (status < MFX_ERR_NONE) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "Video Decode Query failed (%s)",
|
|
|
|
msdk_status_to_string (status));
|
|
|
|
goto failed;
|
|
|
|
} else if (status > MFX_ERR_NONE) {
|
|
|
|
GST_WARNING_OBJECT (thiz, "Video Decode Query returned: %s",
|
|
|
|
msdk_status_to_string (status));
|
|
|
|
}
|
|
|
|
|
2019-03-29 05:22:55 +00:00
|
|
|
klass->postinit_decoder (thiz);
|
2019-01-21 04:25:36 +00:00
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
status = MFXVideoDECODE_QueryIOSurf (session, &thiz->param, &request);
|
|
|
|
if (status < MFX_ERR_NONE) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "Query IO surfaces failed (%s)",
|
|
|
|
msdk_status_to_string (status));
|
|
|
|
goto failed;
|
|
|
|
} else if (status > MFX_ERR_NONE) {
|
|
|
|
GST_WARNING_OBJECT (thiz, "Query IO surfaces returned: %s",
|
|
|
|
msdk_status_to_string (status));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.NumFrameSuggested < thiz->param.AsyncDepth) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "Required %d surfaces (%d suggested), async %d",
|
|
|
|
request.NumFrameMin, request.NumFrameSuggested, thiz->param.AsyncDepth);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* account for downstream requirement */
|
2018-07-03 00:17:49 +00:00
|
|
|
if (G_LIKELY (thiz->min_prealloc_buffers))
|
|
|
|
request.NumFrameSuggested += thiz->min_prealloc_buffers;
|
|
|
|
else
|
|
|
|
GST_WARNING_OBJECT (thiz,
|
|
|
|
"Allocating resources without considering the downstream requirement"
|
|
|
|
"or extra scratch surface count");
|
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
if (thiz->use_video_memory) {
|
2018-02-13 22:52:14 +00:00
|
|
|
gint shared_async_depth;
|
|
|
|
|
|
|
|
shared_async_depth =
|
|
|
|
gst_msdk_context_get_shared_async_depth (thiz->context);
|
|
|
|
request.NumFrameSuggested += shared_async_depth;
|
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
request.Type |= MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
|
2018-03-30 19:06:40 +00:00
|
|
|
if (thiz->use_dmabuf)
|
|
|
|
request.Type |= MFX_MEMTYPE_EXPORT_FRAME;
|
2022-01-21 07:32:09 +00:00
|
|
|
#if (MFX_VERSION >= 1022)
|
|
|
|
if (thiz->sfc) {
|
|
|
|
request.Info.Width = ext_dec_video_proc.Out.Width;
|
|
|
|
request.Info.Height = ext_dec_video_proc.Out.Height;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
gst_msdk_frame_alloc (thiz->context, &request, &thiz->alloc_resp);
|
2016-11-07 20:32:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* update the prealloc_buffer count, which will be used later
|
2018-07-03 00:17:49 +00:00
|
|
|
* as GstBufferPool min_buffers */
|
|
|
|
thiz->min_prealloc_buffers = request.NumFrameSuggested;
|
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
GST_DEBUG_OBJECT (thiz, "Required %d surfaces (%d suggested)",
|
|
|
|
request.NumFrameMin, request.NumFrameSuggested);
|
2016-11-07 20:32:38 +00:00
|
|
|
|
|
|
|
status = MFXVideoDECODE_Init (session, &thiz->param);
|
|
|
|
if (status < MFX_ERR_NONE) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "Init failed (%s)", msdk_status_to_string (status));
|
|
|
|
goto failed;
|
|
|
|
} else if (status > MFX_ERR_NONE) {
|
|
|
|
GST_WARNING_OBJECT (thiz, "Init returned: %s",
|
|
|
|
msdk_status_to_string (status));
|
|
|
|
}
|
|
|
|
|
|
|
|
status = MFXVideoDECODE_GetVideoParam (session, &thiz->param);
|
|
|
|
if (status < MFX_ERR_NONE) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "Get Video Parameters failed (%s)",
|
|
|
|
msdk_status_to_string (status));
|
|
|
|
goto failed;
|
|
|
|
} else if (status > MFX_ERR_NONE) {
|
|
|
|
GST_WARNING_OBJECT (thiz, "Get Video Parameters returned: %s",
|
|
|
|
msdk_status_to_string (status));
|
|
|
|
}
|
|
|
|
|
2019-01-21 15:11:59 +00:00
|
|
|
g_array_set_size (thiz->tasks, 0); /* resets array content */
|
2016-11-07 20:32:38 +00:00
|
|
|
g_array_set_size (thiz->tasks, thiz->param.AsyncDepth);
|
|
|
|
thiz->next_task = 0;
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (thiz);
|
|
|
|
|
2018-02-13 22:51:18 +00:00
|
|
|
thiz->initialized = TRUE;
|
2016-11-07 20:32:38 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
GST_OBJECT_UNLOCK (thiz);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-03-30 19:06:40 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_gst_caps_has_feature (const GstCaps * caps, const gchar * feature)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; i < gst_caps_get_size (caps); i++) {
|
|
|
|
GstCapsFeatures *const features = gst_caps_get_features (caps, i);
|
|
|
|
/* Skip ANY features, we need an exact match for correct evaluation */
|
|
|
|
if (gst_caps_features_is_any (features))
|
|
|
|
continue;
|
|
|
|
if (gst_caps_features_contains (features, feature))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2018-04-02 23:49:59 +00:00
|
|
|
srcpad_can_dmabuf (GstMsdkDec * thiz)
|
2018-03-30 19:06:40 +00:00
|
|
|
{
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
GstCaps *caps, *out_caps;
|
|
|
|
GstPad *srcpad;
|
|
|
|
|
|
|
|
srcpad = GST_VIDEO_DECODER_SRC_PAD (thiz);
|
|
|
|
caps = gst_pad_get_pad_template_caps (srcpad);
|
|
|
|
|
|
|
|
out_caps = gst_pad_peer_query_caps (srcpad, caps);
|
|
|
|
if (!out_caps)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (gst_caps_is_any (out_caps) || gst_caps_is_empty (out_caps)
|
|
|
|
|| out_caps == caps)
|
|
|
|
goto done;
|
|
|
|
|
2018-04-02 23:49:59 +00:00
|
|
|
if (_gst_caps_has_feature (out_caps, GST_CAPS_FEATURE_MEMORY_DMABUF))
|
2018-03-30 19:06:40 +00:00
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (caps)
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
if (out_caps)
|
|
|
|
gst_caps_unref (out_caps);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-21 07:32:09 +00:00
|
|
|
static GstCaps *
|
|
|
|
gst_msdkdec_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
|
|
|
|
{
|
|
|
|
GstCaps *caps, *tmp = NULL;
|
|
|
|
|
|
|
|
caps = gst_pad_get_pad_template_caps (decoder->sinkpad);
|
|
|
|
if (caps) {
|
|
|
|
if (filter) {
|
|
|
|
tmp = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
caps = tmp;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
caps = gst_video_decoder_proxy_getcaps (decoder, NULL, filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
static gboolean
|
2018-07-03 00:17:49 +00:00
|
|
|
gst_msdkdec_set_src_caps (GstMsdkDec * thiz, gboolean need_allocation)
|
2016-11-07 20:32:38 +00:00
|
|
|
{
|
|
|
|
GstVideoCodecState *output_state;
|
2018-07-03 00:17:49 +00:00
|
|
|
GstVideoInfo *vinfo;
|
2016-11-07 20:32:38 +00:00
|
|
|
GstVideoAlignment align;
|
2018-07-03 00:17:49 +00:00
|
|
|
GstCaps *allocation_caps = NULL;
|
2022-01-21 07:32:09 +00:00
|
|
|
GstCaps *allowed_caps = NULL, *temp_caps;
|
2018-11-19 07:34:38 +00:00
|
|
|
GstVideoFormat format;
|
2016-11-07 20:32:38 +00:00
|
|
|
guint width, height;
|
2019-07-23 05:16:36 +00:00
|
|
|
guint alloc_w, alloc_h;
|
2022-02-17 07:16:40 +00:00
|
|
|
int out_width = 0, out_height = 0;
|
2018-07-03 00:17:49 +00:00
|
|
|
const gchar *format_str;
|
2022-01-21 07:32:09 +00:00
|
|
|
GstStructure *outs = NULL;
|
2021-12-15 03:49:40 +00:00
|
|
|
const gchar *out_format;
|
|
|
|
GValue v_format = G_VALUE_INIT;
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* use display width and display height in output state, which
|
|
|
|
* will be used for caps negotiation */
|
2018-07-03 00:17:49 +00:00
|
|
|
width =
|
|
|
|
thiz->param.mfx.FrameInfo.CropW ? thiz->param.mfx.
|
|
|
|
FrameInfo.CropW : GST_VIDEO_INFO_WIDTH (&thiz->input_state->info);
|
|
|
|
height =
|
|
|
|
thiz->param.mfx.FrameInfo.CropH ? thiz->param.mfx.
|
|
|
|
FrameInfo.CropH : GST_VIDEO_INFO_HEIGHT (&thiz->input_state->info);
|
2018-11-19 07:34:38 +00:00
|
|
|
|
|
|
|
format =
|
|
|
|
gst_msdk_get_video_format_from_mfx_fourcc (thiz->param.mfx.
|
|
|
|
FrameInfo.FourCC);
|
|
|
|
|
|
|
|
if (format == GST_VIDEO_FORMAT_UNKNOWN) {
|
2019-12-11 09:47:20 +00:00
|
|
|
GST_WARNING_OBJECT (thiz, "Failed to find a valid video format");
|
2018-11-19 07:34:38 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2021-12-15 03:49:40 +00:00
|
|
|
#if (MFX_VERSION >= 1022)
|
|
|
|
/* SFC is triggered (for AVC and HEVC) when default output format is not
|
2022-01-21 07:32:09 +00:00
|
|
|
* accepted by downstream or when downstream requests for a smaller
|
|
|
|
* resolution (i.e. SFC supports down-scaling)
|
2021-12-15 03:49:40 +00:00
|
|
|
* For SFC csc, need to do the query twice: the first time uses default
|
|
|
|
* color format to query peer pad, empty caps means default format is
|
|
|
|
* not accepted by downstream; then we need the second query to decide
|
|
|
|
* src caps color format and let SFC work. */
|
|
|
|
if (thiz->param.mfx.CodecId == MFX_CODEC_AVC ||
|
|
|
|
thiz->param.mfx.CodecId == MFX_CODEC_HEVC) {
|
|
|
|
temp_caps = gst_pad_query_caps (GST_VIDEO_DECODER (thiz)->srcpad, NULL);
|
|
|
|
temp_caps = gst_caps_make_writable (temp_caps);
|
|
|
|
|
|
|
|
g_value_init (&v_format, G_TYPE_STRING);
|
|
|
|
g_value_set_string (&v_format, gst_video_format_to_string (format));
|
|
|
|
gst_caps_set_value (temp_caps, "format", &v_format);
|
|
|
|
|
|
|
|
if (gst_caps_is_empty (gst_pad_peer_query_caps (GST_VIDEO_DECODER
|
|
|
|
(thiz)->srcpad, temp_caps))) {
|
|
|
|
allowed_caps =
|
|
|
|
gst_pad_get_allowed_caps (GST_VIDEO_DECODER (thiz)->srcpad);
|
|
|
|
outs = gst_caps_get_structure (allowed_caps, 0);
|
|
|
|
out_format = gst_structure_get_string (outs, "format");
|
|
|
|
if (out_format) {
|
|
|
|
format = gst_video_format_from_string (out_format);
|
|
|
|
thiz->sfc = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gst_caps_unref (temp_caps);
|
2022-01-21 07:32:09 +00:00
|
|
|
|
|
|
|
/* SFC scaling, need to check if downstream asking for a different resolution */
|
|
|
|
if (!allowed_caps) {
|
|
|
|
allowed_caps =
|
|
|
|
gst_pad_get_allowed_caps (GST_VIDEO_DECODER (thiz)->srcpad);
|
|
|
|
outs = gst_caps_get_structure (allowed_caps, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_structure_get_int (outs, "width", &out_width);
|
|
|
|
gst_structure_get_int (outs, "height", &out_height);
|
|
|
|
gst_caps_unref (allowed_caps);
|
|
|
|
|
|
|
|
if (out_width && out_height && (out_width != width || out_height != height)) {
|
|
|
|
if (out_width > width || out_height > height)
|
|
|
|
GST_WARNING_OBJECT (thiz, "Decoder SFC cannot do up-scaling");
|
|
|
|
else {
|
|
|
|
GST_LOG_OBJECT (thiz, "Decoder SFC is doing down-scaling");
|
|
|
|
thiz->sfc = TRUE;
|
|
|
|
width = out_width;
|
|
|
|
height = out_height;
|
|
|
|
}
|
|
|
|
}
|
2021-12-15 03:49:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
2018-11-19 07:34:38 +00:00
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
output_state =
|
|
|
|
gst_video_decoder_set_output_state (GST_VIDEO_DECODER (thiz),
|
2018-11-19 07:34:38 +00:00
|
|
|
format, width, height, thiz->input_state);
|
2018-07-03 00:17:49 +00:00
|
|
|
if (!output_state)
|
|
|
|
return FALSE;
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2019-07-23 05:16:36 +00:00
|
|
|
/* Find allocation width and height */
|
|
|
|
alloc_w =
|
|
|
|
GST_ROUND_UP_16 (thiz->param.mfx.FrameInfo.Width ? thiz->param.mfx.
|
|
|
|
FrameInfo.Width : width);
|
|
|
|
alloc_h =
|
|
|
|
GST_ROUND_UP_32 (thiz->param.mfx.FrameInfo.Height ? thiz->param.mfx.
|
|
|
|
FrameInfo.Height : height);
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* Ensure output_state->caps and info have same width and height
|
|
|
|
* Also, mandate 32 bit alignment */
|
2018-07-03 00:17:49 +00:00
|
|
|
vinfo = &output_state->info;
|
2022-01-21 07:32:09 +00:00
|
|
|
if (width == out_width || height == out_height)
|
|
|
|
gst_msdk_set_video_alignment (vinfo, 0, 0, &align);
|
|
|
|
else
|
|
|
|
gst_msdk_set_video_alignment (vinfo, alloc_w, alloc_h, &align);
|
2018-07-03 00:17:49 +00:00
|
|
|
gst_video_info_align (vinfo, &align);
|
|
|
|
output_state->caps = gst_video_info_to_caps (vinfo);
|
2022-01-21 07:32:09 +00:00
|
|
|
|
2018-04-02 23:49:59 +00:00
|
|
|
if (srcpad_can_dmabuf (thiz))
|
2018-03-30 19:06:40 +00:00
|
|
|
gst_caps_set_features (output_state->caps, 0,
|
|
|
|
gst_caps_features_new (GST_CAPS_FEATURE_MEMORY_DMABUF, NULL));
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
if (need_allocation) {
|
|
|
|
/* Find allocation width and height */
|
|
|
|
width =
|
2018-07-03 00:50:46 +00:00
|
|
|
GST_ROUND_UP_16 (thiz->param.mfx.FrameInfo.Width ? thiz->param.mfx.
|
2018-07-03 00:17:49 +00:00
|
|
|
FrameInfo.Width : GST_VIDEO_INFO_WIDTH (&output_state->info));
|
|
|
|
height =
|
|
|
|
GST_ROUND_UP_32 (thiz->param.mfx.FrameInfo.Height ? thiz->param.mfx.
|
|
|
|
FrameInfo.Height : GST_VIDEO_INFO_HEIGHT (&output_state->info));
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* set allocation width and height in allocation_caps,
|
2018-07-03 00:17:49 +00:00
|
|
|
* which may or may not be similar to the output_state caps */
|
|
|
|
allocation_caps = gst_caps_copy (output_state->caps);
|
|
|
|
format_str =
|
2019-07-22 16:52:53 +00:00
|
|
|
gst_video_format_to_string (GST_VIDEO_INFO_FORMAT
|
|
|
|
(&output_state->info));
|
2018-07-03 00:17:49 +00:00
|
|
|
gst_caps_set_simple (allocation_caps, "width", G_TYPE_INT, width, "height",
|
|
|
|
G_TYPE_INT, height, "format", G_TYPE_STRING, format_str, NULL);
|
|
|
|
GST_INFO_OBJECT (thiz, "new alloc caps = %" GST_PTR_FORMAT,
|
|
|
|
allocation_caps);
|
2019-07-26 13:45:46 +00:00
|
|
|
gst_caps_replace (&output_state->allocation_caps, allocation_caps);
|
|
|
|
gst_caps_unref (allocation_caps);
|
2018-07-03 00:17:49 +00:00
|
|
|
} else {
|
2019-07-23 17:47:44 +00:00
|
|
|
/* We keep the allocation parameters as it is to avoid pool re-negotiation.
|
|
|
|
* For codecs like VP9, dynamic resolution change doesn't require allocation
|
2018-07-03 00:17:49 +00:00
|
|
|
* reset if the new video frame resolution is lower than the
|
|
|
|
* already configured one */
|
|
|
|
}
|
|
|
|
gst_video_codec_state_unref (output_state);
|
2019-07-26 13:45:46 +00:00
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_msdkdec_set_latency (GstMsdkDec * thiz)
|
|
|
|
{
|
|
|
|
GstVideoInfo *info = &thiz->input_state->info;
|
|
|
|
gint min_delayed_frames;
|
|
|
|
GstClockTime latency;
|
|
|
|
|
2018-07-04 02:38:04 +00:00
|
|
|
min_delayed_frames = thiz->async_depth;
|
2016-11-07 20:32:38 +00:00
|
|
|
|
|
|
|
if (info->fps_n) {
|
|
|
|
latency = gst_util_uint64_scale_ceil (GST_SECOND * info->fps_d,
|
|
|
|
min_delayed_frames, info->fps_n);
|
|
|
|
} else {
|
|
|
|
/* FIXME: Assume 25fps. This is better than reporting no latency at
|
|
|
|
* all and then later failing in live pipelines
|
|
|
|
*/
|
|
|
|
latency = gst_util_uint64_scale_ceil (GST_SECOND * 1,
|
|
|
|
min_delayed_frames, 25);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (thiz,
|
|
|
|
"Updating latency to %" GST_TIME_FORMAT " (%d frames)",
|
|
|
|
GST_TIME_ARGS (latency), min_delayed_frames);
|
|
|
|
|
|
|
|
gst_video_decoder_set_latency (GST_VIDEO_DECODER (thiz), latency, latency);
|
|
|
|
}
|
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
static gint
|
|
|
|
_find_msdk_surface (gconstpointer msdk_surface, gconstpointer comp_surface)
|
|
|
|
{
|
|
|
|
MsdkSurface *cached_surface = (MsdkSurface *) msdk_surface;
|
|
|
|
mfxFrameSurface1 *_surface = (mfxFrameSurface1 *) comp_surface;
|
|
|
|
|
|
|
|
return cached_surface ? cached_surface->surface != _surface : -1;
|
|
|
|
}
|
|
|
|
|
2020-05-14 03:03:49 +00:00
|
|
|
static void
|
|
|
|
finish_task (GstMsdkDec * thiz, MsdkDecTask * task)
|
|
|
|
{
|
|
|
|
MsdkSurface *surface = task->surface;
|
|
|
|
if (surface) {
|
|
|
|
if (G_UNLIKELY (surface->copy.buffer)) {
|
|
|
|
unmap_frame (thiz, surface);
|
|
|
|
}
|
|
|
|
thiz->locked_msdk_surfaces =
|
|
|
|
g_list_append (thiz->locked_msdk_surfaces, surface);
|
|
|
|
}
|
|
|
|
task->sync_point = NULL;
|
|
|
|
task->surface = NULL;
|
|
|
|
task->decode_only = FALSE;
|
|
|
|
}
|
|
|
|
|
2019-12-06 04:48:37 +00:00
|
|
|
static void
|
|
|
|
gst_msdkdec_frame_corruption_report (GstMsdkDec * thiz, mfxU16 corruption)
|
|
|
|
{
|
|
|
|
if (!thiz->report_error || !corruption)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (corruption & MFX_CORRUPTION_MINOR)
|
|
|
|
GST_ELEMENT_WARNING (thiz, STREAM, DECODE,
|
|
|
|
("[Corruption] Minor corruption detected!"), (NULL));
|
|
|
|
|
|
|
|
if (corruption & MFX_CORRUPTION_MAJOR)
|
|
|
|
GST_ELEMENT_WARNING (thiz, STREAM, DECODE,
|
|
|
|
("[Corruption] Major corruption detected!"), (NULL));
|
|
|
|
|
|
|
|
if (corruption & MFX_CORRUPTION_ABSENT_TOP_FIELD)
|
|
|
|
GST_ELEMENT_WARNING (thiz, STREAM, DECODE,
|
|
|
|
("[Corruption] Absent top field!"), (NULL));
|
|
|
|
|
|
|
|
if (corruption & MFX_CORRUPTION_ABSENT_BOTTOM_FIELD)
|
|
|
|
GST_ELEMENT_WARNING (thiz, STREAM, DECODE,
|
|
|
|
("[Corruption] Absent bottom field!"), (NULL));
|
|
|
|
|
|
|
|
if (corruption & MFX_CORRUPTION_REFERENCE_FRAME)
|
|
|
|
GST_ELEMENT_WARNING (thiz, STREAM, DECODE,
|
|
|
|
("[Corruption] Corrupted reference frame!"), (NULL));
|
|
|
|
|
|
|
|
if (corruption & MFX_CORRUPTION_REFERENCE_LIST)
|
|
|
|
GST_ELEMENT_WARNING (thiz, STREAM, DECODE,
|
|
|
|
("[Corruption] Corrupted reference list!"), (NULL));
|
|
|
|
}
|
|
|
|
|
2017-01-20 19:11:50 +00:00
|
|
|
static GstFlowReturn
|
2016-11-07 20:32:38 +00:00
|
|
|
gst_msdkdec_finish_task (GstMsdkDec * thiz, MsdkDecTask * task)
|
|
|
|
{
|
|
|
|
GstVideoDecoder *decoder = GST_VIDEO_DECODER (thiz);
|
2017-01-20 19:11:50 +00:00
|
|
|
GstFlowReturn flow;
|
2016-11-07 20:32:38 +00:00
|
|
|
GstVideoCodecFrame *frame;
|
|
|
|
MsdkSurface *surface;
|
|
|
|
mfxStatus status;
|
2019-11-06 12:45:09 +00:00
|
|
|
guint64 pts = MFX_TIMESTAMP_UNKNOWN;
|
2016-11-07 20:32:38 +00:00
|
|
|
|
|
|
|
if (G_LIKELY (task->sync_point)) {
|
|
|
|
status =
|
2018-02-13 21:41:28 +00:00
|
|
|
MFXVideoCORE_SyncOperation (gst_msdk_context_get_session
|
2018-02-13 22:49:28 +00:00
|
|
|
(thiz->context), task->sync_point, 300000);
|
|
|
|
if (status != MFX_ERR_NONE) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "failed to do sync operation");
|
2017-01-20 19:11:50 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2018-02-13 22:49:28 +00:00
|
|
|
}
|
2019-02-02 05:26:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 03:03:49 +00:00
|
|
|
surface = task->surface;
|
|
|
|
if (surface) {
|
2019-12-06 04:48:37 +00:00
|
|
|
gst_msdkdec_frame_corruption_report (thiz,
|
|
|
|
surface->surface->Data.Corrupted);
|
2019-11-06 12:45:09 +00:00
|
|
|
GST_DEBUG_OBJECT (thiz, "Decoded MFX TimeStamp: %" G_GUINT64_FORMAT,
|
2020-05-14 03:03:49 +00:00
|
|
|
(guint64) surface->surface->Data.TimeStamp);
|
|
|
|
pts = surface->surface->Data.TimeStamp;
|
2019-11-06 12:45:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 03:03:49 +00:00
|
|
|
if (G_LIKELY (task->sync_point || (surface && task->decode_only))) {
|
2019-02-02 05:26:32 +00:00
|
|
|
gboolean decode_only = task->decode_only;
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2018-07-03 00:42:20 +00:00
|
|
|
frame = gst_msdkdec_get_oldest_frame (decoder);
|
2019-11-06 12:45:09 +00:00
|
|
|
/* align decoder frame list with current decoded position */
|
|
|
|
while (frame && MFX_TIME_IS_VALID (pts)
|
|
|
|
&& GST_CLOCK_TIME_IS_VALID (frame->pts)
|
|
|
|
&& GST_TO_MFX_TIME (frame->pts) < pts) {
|
|
|
|
GST_INFO_OBJECT (thiz, "Discarding frame: %p PTS: %" GST_TIME_FORMAT
|
|
|
|
" MFX TimeStamp: %" G_GUINT64_FORMAT,
|
|
|
|
frame, GST_TIME_ARGS (frame->pts), GST_TO_MFX_TIME (frame->pts));
|
|
|
|
gst_video_decoder_release_frame (decoder, frame);
|
|
|
|
frame = gst_msdkdec_get_oldest_frame (decoder);
|
|
|
|
}
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2017-01-20 19:11:50 +00:00
|
|
|
if (G_LIKELY (frame)) {
|
|
|
|
if (G_LIKELY (surface->copy.buffer == NULL)) {
|
2020-05-14 03:03:49 +00:00
|
|
|
/* gst_video_decoder_finish_frame will call gst_buffer_make_writable
|
|
|
|
* we need this to avoid copy buffer */
|
|
|
|
GST_MINI_OBJECT_FLAG_SET (surface->buf, GST_MINI_OBJECT_FLAG_LOCKABLE);
|
2018-02-13 22:49:28 +00:00
|
|
|
frame->output_buffer = gst_buffer_ref (surface->buf);
|
2017-01-20 19:11:50 +00:00
|
|
|
} else {
|
2022-02-17 07:12:52 +00:00
|
|
|
if (!gst_video_frame_copy (&surface->copy, &surface->data)) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "Failed to copy surface data");
|
|
|
|
gst_video_frame_unmap (&surface->copy);
|
|
|
|
gst_video_frame_unmap (&surface->data);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2017-01-20 19:11:50 +00:00
|
|
|
frame->output_buffer = gst_buffer_ref (surface->copy.buffer);
|
2020-05-14 03:03:49 +00:00
|
|
|
unmap_frame (thiz, surface);
|
2017-01-20 19:11:50 +00:00
|
|
|
}
|
2019-11-06 12:45:09 +00:00
|
|
|
GST_DEBUG_OBJECT (thiz, "surface %p TimeStamp: %" G_GUINT64_FORMAT
|
|
|
|
" frame %p TimeStamp: %" G_GUINT64_FORMAT,
|
|
|
|
surface->surface, (guint64) surface->surface->Data.TimeStamp,
|
|
|
|
frame, GST_TO_MFX_TIME (frame->pts));
|
2016-11-07 20:32:38 +00:00
|
|
|
}
|
2017-01-20 19:11:50 +00:00
|
|
|
|
2020-05-14 03:03:49 +00:00
|
|
|
finish_task (thiz, task);
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2017-01-20 19:11:50 +00:00
|
|
|
if (!frame)
|
|
|
|
return GST_FLOW_FLUSHING;
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2019-02-02 05:26:32 +00:00
|
|
|
if (decode_only)
|
|
|
|
GST_VIDEO_CODEC_FRAME_SET_DECODE_ONLY (frame);
|
2018-02-13 22:49:28 +00:00
|
|
|
flow = gst_video_decoder_finish_frame (decoder, frame);
|
2019-12-06 16:50:05 +00:00
|
|
|
if (flow == GST_FLOW_ERROR)
|
|
|
|
GST_ERROR_OBJECT (thiz, "Failed to finish frame");
|
2017-01-20 19:11:50 +00:00
|
|
|
return flow;
|
2016-11-07 20:32:38 +00:00
|
|
|
}
|
2020-05-14 03:03:49 +00:00
|
|
|
finish_task (thiz, task);
|
2017-01-20 19:11:50 +00:00
|
|
|
return GST_FLOW_OK;
|
2016-11-07 20:32:38 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 22:51:18 +00:00
|
|
|
static gboolean
|
2020-01-17 05:13:11 +00:00
|
|
|
gst_msdkdec_context_prepare (GstMsdkDec * thiz)
|
2018-02-13 22:51:18 +00:00
|
|
|
{
|
2020-01-17 05:13:11 +00:00
|
|
|
/* Try to find an existing context from the pipeline. This may (indirectly)
|
|
|
|
* invoke gst_msdkdec_set_context, which will set thiz->context. */
|
|
|
|
if (!gst_msdk_context_find (GST_ELEMENT_CAST (thiz), &thiz->context))
|
|
|
|
return FALSE;
|
2019-12-18 00:10:53 +00:00
|
|
|
|
2020-01-17 05:37:47 +00:00
|
|
|
if (thiz->context == thiz->old_context) {
|
|
|
|
GST_INFO_OBJECT (thiz, "Found old context %" GST_PTR_FORMAT
|
|
|
|
", reusing as-is", thiz->context);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-01-17 05:13:11 +00:00
|
|
|
/* TODO: Currently d3d allocator is not implemented.
|
|
|
|
* So decoder uses system memory by default on Windows.
|
|
|
|
*/
|
2020-01-06 04:59:36 +00:00
|
|
|
#ifndef _WIN32
|
2020-01-17 05:13:11 +00:00
|
|
|
thiz->use_video_memory = TRUE;
|
2020-01-06 04:59:36 +00:00
|
|
|
#else
|
2020-01-17 05:13:11 +00:00
|
|
|
thiz->use_video_memory = FALSE;
|
2020-01-06 04:59:36 +00:00
|
|
|
#endif
|
|
|
|
|
2020-01-17 05:13:11 +00:00
|
|
|
GST_INFO_OBJECT (thiz, "Found context %" GST_PTR_FORMAT " from neighbour",
|
|
|
|
thiz->context);
|
2018-12-12 04:52:46 +00:00
|
|
|
|
2020-04-10 15:22:21 +00:00
|
|
|
if (!(gst_msdk_context_get_job_type (thiz->context) & GST_MSDK_JOB_DECODER)) {
|
2020-01-17 05:13:11 +00:00
|
|
|
gst_msdk_context_add_job_type (thiz->context, GST_MSDK_JOB_DECODER);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2018-12-12 04:52:46 +00:00
|
|
|
|
2020-01-17 05:13:11 +00:00
|
|
|
/* Found an existing context that's already being used as a decoder, clone
|
|
|
|
* the MFX session inside it to create a new one */
|
|
|
|
{
|
|
|
|
GstMsdkContext *parent_context, *msdk_context;
|
2018-02-13 22:52:14 +00:00
|
|
|
|
2020-01-17 05:13:11 +00:00
|
|
|
GST_INFO_OBJECT (thiz, "Creating new context %" GST_PTR_FORMAT " with "
|
|
|
|
"joined session", thiz->context);
|
|
|
|
parent_context = thiz->context;
|
|
|
|
msdk_context = gst_msdk_context_new_with_parent (parent_context);
|
2018-02-13 22:51:18 +00:00
|
|
|
|
2020-01-17 05:13:11 +00:00
|
|
|
if (!msdk_context) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "Failed to create a context with parent context "
|
|
|
|
"as %" GST_PTR_FORMAT, parent_context);
|
|
|
|
return FALSE;
|
2018-02-13 22:51:18 +00:00
|
|
|
}
|
2020-01-17 05:13:11 +00:00
|
|
|
|
|
|
|
thiz->context = msdk_context;
|
|
|
|
gst_msdk_context_add_shared_async_depth (thiz->context,
|
|
|
|
gst_msdk_context_get_shared_async_depth (parent_context));
|
|
|
|
gst_object_unref (parent_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_start (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (decoder);
|
|
|
|
|
|
|
|
if (!gst_msdkdec_context_prepare (thiz)) {
|
2021-08-17 13:51:58 +00:00
|
|
|
if (!gst_msdk_ensure_new_context (GST_ELEMENT_CAST (thiz),
|
|
|
|
thiz->hardware, GST_MSDK_JOB_DECODER, &thiz->context))
|
2019-04-06 19:44:35 +00:00
|
|
|
return FALSE;
|
2018-02-13 22:51:18 +00:00
|
|
|
GST_INFO_OBJECT (thiz, "Creating new context %" GST_PTR_FORMAT,
|
|
|
|
thiz->context);
|
|
|
|
}
|
|
|
|
|
2020-01-17 05:37:47 +00:00
|
|
|
/* Save the current context in a separate field so that we know whether it
|
|
|
|
* has changed between calls to _start() */
|
|
|
|
gst_object_replace ((GstObject **) & thiz->old_context,
|
|
|
|
(GstObject *) thiz->context);
|
|
|
|
|
2018-02-13 22:52:14 +00:00
|
|
|
gst_msdk_context_add_shared_async_depth (thiz->context, thiz->async_depth);
|
|
|
|
|
2018-02-13 22:51:18 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_close (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (decoder);
|
2018-02-13 21:41:28 +00:00
|
|
|
|
2020-01-17 08:15:35 +00:00
|
|
|
gst_clear_object (&thiz->context);
|
2018-02-13 21:41:28 +00:00
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_stop (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (decoder);
|
2018-03-08 20:38:52 +00:00
|
|
|
|
|
|
|
gst_msdkdec_flush (decoder);
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
if (thiz->input_state) {
|
|
|
|
gst_video_codec_state_unref (thiz->input_state);
|
|
|
|
thiz->input_state = NULL;
|
|
|
|
}
|
|
|
|
if (thiz->pool) {
|
|
|
|
gst_object_unref (thiz->pool);
|
2018-02-13 21:43:00 +00:00
|
|
|
thiz->pool = NULL;
|
2016-11-07 20:32:38 +00:00
|
|
|
}
|
2018-07-03 00:17:49 +00:00
|
|
|
gst_video_info_init (&thiz->non_msdk_pool_info);
|
2018-02-13 22:51:18 +00:00
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
gst_msdkdec_close_decoder (thiz, TRUE);
|
2016-11-07 20:32:38 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
|
|
|
|
{
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (decoder);
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
if (thiz->input_state) {
|
2019-12-26 19:24:37 +00:00
|
|
|
/* mark for re-negotiation if display resolution or any other video info
|
|
|
|
* changes like framerate. */
|
|
|
|
if (!gst_video_info_is_equal (&thiz->input_state->info, &state->info)) {
|
|
|
|
GST_INFO_OBJECT (thiz, "Schedule renegotiation as video info changed");
|
2018-07-03 00:17:49 +00:00
|
|
|
thiz->do_renego = TRUE;
|
2019-12-26 19:24:37 +00:00
|
|
|
}
|
2016-11-07 20:32:38 +00:00
|
|
|
gst_video_codec_state_unref (thiz->input_state);
|
2018-07-03 00:17:49 +00:00
|
|
|
}
|
2016-11-07 20:32:38 +00:00
|
|
|
thiz->input_state = gst_video_codec_state_ref (state);
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
/* we don't set output state here to avoid caching of mismatched
|
|
|
|
* video information if there is dynamic resolution change in the stream.
|
|
|
|
* All negotiation code is consolidated in gst_msdkdec_negotiate() and
|
|
|
|
* this will be invoked from handle_frame() */
|
2016-11-07 20:32:38 +00:00
|
|
|
|
|
|
|
gst_msdkdec_set_latency (thiz);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
static void
|
|
|
|
release_msdk_surfaces (GstMsdkDec * thiz)
|
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
MsdkSurface *surface;
|
2020-05-14 03:03:49 +00:00
|
|
|
gint locked = 0;
|
|
|
|
gst_msdkdec_free_unlocked_msdk_surfaces (thiz);
|
|
|
|
|
|
|
|
for (l = thiz->locked_msdk_surfaces; l; l = l->next) {
|
|
|
|
surface = (MsdkSurface *) l->data;
|
|
|
|
unmap_frame (thiz, surface);
|
|
|
|
free_surface (surface);
|
|
|
|
locked++;
|
2018-02-13 22:49:28 +00:00
|
|
|
}
|
2020-05-14 03:03:49 +00:00
|
|
|
if (locked)
|
|
|
|
GST_ERROR_OBJECT (thiz, "msdk still locked %d surfaces", locked);
|
|
|
|
g_list_free (thiz->locked_msdk_surfaces);
|
|
|
|
thiz->locked_msdk_surfaces = NULL;
|
2018-02-13 22:49:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
/* This will get invoked in the following situations:
|
2019-07-23 17:47:44 +00:00
|
|
|
* 1: beginning of the stream, which requires initialization (== complete reset)
|
2018-07-03 00:17:49 +00:00
|
|
|
* 2: upstream notified a resolution change and set do_renego to TRUE.
|
2019-07-23 17:47:44 +00:00
|
|
|
* new resolution may or may not requires full reset
|
|
|
|
* 3: upstream failed to notify the resolution change but
|
2018-07-03 00:17:49 +00:00
|
|
|
* msdk detected the change (eg: vp9 stream in ivf elementary form
|
|
|
|
* with varying resolution frames).
|
|
|
|
*
|
|
|
|
* for any input configuration change, we deal with notification
|
2019-07-23 17:47:44 +00:00
|
|
|
* from upstream and also use msdk APIs to handle the parameter initialization
|
2018-07-03 00:17:49 +00:00
|
|
|
* efficiently
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_negotiate (GstMsdkDec * thiz, gboolean hard_reset)
|
|
|
|
{
|
|
|
|
GstVideoDecoder *decoder = GST_VIDEO_DECODER (thiz);
|
|
|
|
GST_DEBUG_OBJECT (thiz,
|
|
|
|
"Start Negotiating caps, pool and Init the msdk decdoer subsystem");
|
|
|
|
|
|
|
|
if (hard_reset) {
|
|
|
|
/* Retrieve any pending frames and push them downstream */
|
|
|
|
if (gst_msdkdec_drain (GST_VIDEO_DECODER (thiz)) != GST_FLOW_OK)
|
|
|
|
goto error_drain;
|
|
|
|
|
|
|
|
/* This will initiate the allocation query which will help to flush
|
|
|
|
* all the pending buffers in the pipeline so that we can stop
|
|
|
|
* the active bufferpool and safely invoke gst_msdk_frame_free() */
|
|
|
|
if (thiz->initialized) {
|
|
|
|
GstCaps *caps = gst_pad_get_current_caps (decoder->srcpad);
|
|
|
|
GstQuery *query = NULL;
|
|
|
|
if (caps) {
|
|
|
|
query = gst_query_new_allocation (caps, FALSE);
|
|
|
|
gst_pad_peer_query (decoder->srcpad, query);
|
|
|
|
gst_query_unref (query);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* De-initialize the decoder if it is already active */
|
2019-07-23 17:47:44 +00:00
|
|
|
/* Do not reset the mfxVideoParam since it already
|
|
|
|
* has the required parameters for new session decode */
|
2018-07-03 00:17:49 +00:00
|
|
|
gst_msdkdec_close_decoder (thiz, FALSE);
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* request for pool re-negotiation by setting do_realloc */
|
2018-07-03 00:17:49 +00:00
|
|
|
thiz->do_realloc = TRUE;
|
|
|
|
}
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* At this point all pending frames (if there are any) are pushed downstream
|
2018-07-03 00:17:49 +00:00
|
|
|
* and we are ready to negotiate the output caps */
|
|
|
|
if (!gst_msdkdec_set_src_caps (thiz, hard_reset))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* this will initiate the allocation query, we create the
|
2019-07-23 17:47:44 +00:00
|
|
|
* bufferpool in decide_allocation in order to account
|
|
|
|
* for the downstream min_buffer requirement
|
2018-07-03 00:17:49 +00:00
|
|
|
* Required initializations for MediaSDK operations
|
2019-07-23 17:47:44 +00:00
|
|
|
* will all be initialized from decide_allocation after considering
|
2018-07-03 00:17:49 +00:00
|
|
|
* some of the downstream requirements */
|
|
|
|
if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (thiz)))
|
|
|
|
goto error_negotiate;
|
|
|
|
|
|
|
|
thiz->do_renego = FALSE;
|
|
|
|
thiz->do_realloc = FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
error_drain:
|
|
|
|
GST_ERROR_OBJECT (thiz, "Failed to Drain the queued decoded frames");
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
error_negotiate:
|
2019-07-23 17:47:44 +00:00
|
|
|
GST_ERROR_OBJECT (thiz, "Failed to re-negotiate");
|
2018-07-03 00:17:49 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-05-14 03:03:49 +00:00
|
|
|
static inline gboolean
|
|
|
|
find_msdk_surface (GstMsdkDec * thiz, MsdkDecTask * task,
|
|
|
|
mfxFrameSurface1 * out_surface)
|
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
task->surface = NULL;
|
2022-01-21 07:32:09 +00:00
|
|
|
|
2020-05-14 03:03:49 +00:00
|
|
|
if (!out_surface)
|
|
|
|
return TRUE;
|
|
|
|
l = g_list_find_custom (thiz->locked_msdk_surfaces, out_surface,
|
|
|
|
_find_msdk_surface);
|
|
|
|
if (!l) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "msdk return an invalid surface %p", out_surface);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
task->surface = (MsdkSurface *) l->data;
|
|
|
|
thiz->locked_msdk_surfaces =
|
|
|
|
g_list_delete_link (thiz->locked_msdk_surfaces, l);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-12-06 04:48:37 +00:00
|
|
|
static void
|
|
|
|
gst_msdkdec_error_report (GstMsdkDec * thiz)
|
|
|
|
{
|
|
|
|
if (!thiz->report_error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#if (MFX_VERSION >= 1025)
|
|
|
|
else {
|
|
|
|
if (thiz->error_report.ErrorTypes & MFX_ERROR_SPS)
|
|
|
|
GST_ELEMENT_WARNING (thiz, STREAM, DECODE,
|
|
|
|
("[Error] SPS Error detected!"), (NULL));
|
|
|
|
|
|
|
|
if (thiz->error_report.ErrorTypes & MFX_ERROR_PPS)
|
|
|
|
GST_ELEMENT_WARNING (thiz, STREAM, DECODE,
|
|
|
|
("[Error] PPS Error detected!"), (NULL));
|
|
|
|
|
|
|
|
if (thiz->error_report.ErrorTypes & MFX_ERROR_SLICEHEADER)
|
|
|
|
GST_ELEMENT_WARNING (thiz, STREAM, DECODE,
|
|
|
|
("[Error] SliceHeader Error detected!"), (NULL));
|
|
|
|
|
|
|
|
if (thiz->error_report.ErrorTypes & MFX_ERROR_FRAME_GAP)
|
|
|
|
GST_ELEMENT_WARNING (thiz, STREAM, DECODE,
|
|
|
|
("[Error] Frame Gap Error detected!"), (NULL));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_msdkdec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
|
|
|
|
{
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (decoder);
|
2018-07-03 00:17:49 +00:00
|
|
|
GstMsdkDecClass *klass = GST_MSDKDEC_GET_CLASS (thiz);
|
2017-01-20 19:11:50 +00:00
|
|
|
GstFlowReturn flow;
|
2019-01-11 08:18:11 +00:00
|
|
|
GstBuffer *buffer, *input_buffer = NULL;
|
2018-07-03 00:17:49 +00:00
|
|
|
GstVideoInfo alloc_info;
|
2016-11-07 20:32:38 +00:00
|
|
|
MsdkDecTask *task = NULL;
|
|
|
|
mfxBitstream bitstream;
|
2018-02-13 22:49:28 +00:00
|
|
|
MsdkSurface *surface = NULL;
|
2020-05-14 03:03:49 +00:00
|
|
|
mfxFrameSurface1 *out_surface = NULL;
|
2016-11-07 20:32:38 +00:00
|
|
|
mfxSession session;
|
|
|
|
mfxStatus status;
|
|
|
|
GstMapInfo map_info;
|
2019-03-06 05:07:53 +00:00
|
|
|
guint i, retry_err_incompatible = 0;
|
2018-02-13 23:41:20 +00:00
|
|
|
gsize data_size;
|
2018-07-03 00:17:49 +00:00
|
|
|
gboolean hard_reset = FALSE;
|
2019-11-06 12:45:09 +00:00
|
|
|
GstClockTime pts = GST_CLOCK_TIME_NONE;
|
2018-02-13 22:53:02 +00:00
|
|
|
|
2019-01-21 11:46:32 +00:00
|
|
|
/* configure the subclass in order to fill the CodecID field of
|
|
|
|
* mfxVideoParam and also to load the PluginID for some of the
|
|
|
|
* codecs which is mandatory to invoke the
|
|
|
|
* MFXVideoDECODE_DecodeHeader API.
|
2018-07-03 00:48:11 +00:00
|
|
|
*
|
|
|
|
* For non packetized formats (currently only vc1), there
|
|
|
|
* could be headers received as codec_data which are not available
|
|
|
|
* instream and in that case subclass implementation will
|
|
|
|
* push it to the internal adapter. We invoke the subclass configure
|
|
|
|
* well early to make sure the codec_data received has been correctly
|
|
|
|
* pushed to the adapter by the subclasses before doing
|
|
|
|
* the DecodeHeader() later on
|
|
|
|
*/
|
|
|
|
if (!thiz->initialized || thiz->do_renego) {
|
2019-07-23 17:47:44 +00:00
|
|
|
/* Clear the internal adapter in re-negotiation for non-packetized
|
2018-07-03 00:48:11 +00:00
|
|
|
* formats */
|
2019-10-28 12:28:28 +00:00
|
|
|
if (!gst_video_decoder_get_packetized (decoder))
|
2018-07-03 00:48:11 +00:00
|
|
|
gst_adapter_clear (thiz->adapter);
|
|
|
|
|
|
|
|
if (!klass->configure || !klass->configure (thiz)) {
|
|
|
|
flow = GST_FLOW_OK;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 16:51:35 +00:00
|
|
|
/* Current frame-codec could be pushed and released before this
|
|
|
|
* function ends -- because msdkdec pushes the oldest frame,
|
|
|
|
* according its PTS, and it could be this very same frame-codec
|
|
|
|
* among others pending frame-codecs.
|
|
|
|
*
|
|
|
|
* Instead of copying the input data into the mfxBitstream, let's
|
|
|
|
* keep an extra reference to frame-codec's input buffer */
|
2019-01-11 08:18:11 +00:00
|
|
|
input_buffer = gst_buffer_ref (frame->input_buffer);
|
|
|
|
if (!gst_buffer_map (input_buffer, &map_info, GST_MAP_READ)) {
|
|
|
|
gst_buffer_unref (input_buffer);
|
2016-11-07 20:32:38 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2019-01-11 08:18:11 +00:00
|
|
|
}
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
memset (&bitstream, 0, sizeof (bitstream));
|
2018-02-13 23:41:20 +00:00
|
|
|
|
2019-12-06 04:02:50 +00:00
|
|
|
/* Add extended buffers */
|
|
|
|
if (thiz->num_bs_extra_params) {
|
|
|
|
bitstream.NumExtParam = thiz->num_bs_extra_params;
|
|
|
|
bitstream.ExtParam = thiz->bs_extra_params;
|
|
|
|
}
|
|
|
|
|
2019-10-28 12:28:28 +00:00
|
|
|
if (gst_video_decoder_get_packetized (decoder)) {
|
2019-07-23 17:47:44 +00:00
|
|
|
/* Packetized stream: we prefer to have a parser as a connected upstream
|
2018-02-13 23:41:20 +00:00
|
|
|
* element to the decoder */
|
2019-11-06 12:45:09 +00:00
|
|
|
pts = frame->pts;
|
2018-02-13 23:41:20 +00:00
|
|
|
bitstream.Data = map_info.data;
|
|
|
|
bitstream.DataLength = map_info.size;
|
|
|
|
bitstream.MaxLength = map_info.size;
|
2019-11-06 12:45:09 +00:00
|
|
|
bitstream.TimeStamp = GST_TO_MFX_TIME (pts);
|
2019-05-24 08:48:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MFX_BITSTREAM_COMPLETE_FRAME was removed since commit df59db9, however
|
|
|
|
* some customers still use DecodedOrder (deprecated in msdk-2017 version)
|
|
|
|
* for low-latency streaming of non-b-frame encoded streams, which needs to
|
|
|
|
* output the frame at once, so add it back for this case
|
|
|
|
*/
|
|
|
|
if (thiz->param.mfx.DecodedOrder == GST_MSDKDEC_OUTPUT_ORDER_DECODE)
|
|
|
|
bitstream.DataFlag |= MFX_BITSTREAM_COMPLETE_FRAME;
|
2018-02-13 23:41:20 +00:00
|
|
|
} else {
|
|
|
|
/* Non packetized streams: eg: vc1 advanced profile with per buffer bdu */
|
2019-01-11 08:18:11 +00:00
|
|
|
gst_adapter_push (thiz->adapter, gst_buffer_ref (input_buffer));
|
2018-02-13 23:41:20 +00:00
|
|
|
data_size = gst_adapter_available (thiz->adapter);
|
|
|
|
|
|
|
|
bitstream.Data = (mfxU8 *) gst_adapter_map (thiz->adapter, data_size);
|
|
|
|
bitstream.DataLength = (mfxU32) data_size;
|
|
|
|
bitstream.MaxLength = bitstream.DataLength;
|
2019-11-06 12:45:09 +00:00
|
|
|
bitstream.TimeStamp = GST_TO_MFX_TIME (pts);
|
2018-02-13 23:41:20 +00:00
|
|
|
}
|
2019-11-06 12:45:09 +00:00
|
|
|
GST_DEBUG_OBJECT (thiz,
|
|
|
|
"mfxBitStream=> DataLength:%d DataOffset:%d MaxLength:%d "
|
|
|
|
"PTS: %" GST_TIME_FORMAT " MFX TimeStamp %" G_GUINT64_FORMAT,
|
|
|
|
bitstream.DataLength, bitstream.DataOffset, bitstream.MaxLength,
|
|
|
|
GST_TIME_ARGS (pts), (guint64) bitstream.TimeStamp);
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2018-02-13 21:41:28 +00:00
|
|
|
session = gst_msdk_context_get_session (thiz->context);
|
2018-07-03 00:17:49 +00:00
|
|
|
|
|
|
|
if (!thiz->initialized || thiz->do_renego) {
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* gstreamer caps will not provide all the necessary parameters
|
|
|
|
* required for optimal decode configuration. For example: the required number
|
2018-07-03 00:17:49 +00:00
|
|
|
* of surfaces to be allocated can be calculated based on H264 SEI header
|
|
|
|
* and this information can't be retrieved from the negotiated caps.
|
2019-07-23 17:47:44 +00:00
|
|
|
* So instead of introducing a codecparser dependency to parse the headers
|
|
|
|
* inside msdk plugin, we simply use the mfx APIs to extract header information */
|
2019-12-06 04:48:37 +00:00
|
|
|
#if (MFX_VERSION >= 1025)
|
|
|
|
if (thiz->report_error)
|
|
|
|
thiz->error_report.ErrorTypes = 0;
|
|
|
|
#endif
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
status = MFXVideoDECODE_DecodeHeader (session, &bitstream, &thiz->param);
|
2019-11-06 12:45:09 +00:00
|
|
|
GST_DEBUG_OBJECT (decoder, "DecodeHeader => %d", status);
|
2019-12-06 04:48:37 +00:00
|
|
|
gst_msdkdec_error_report (thiz);
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
if (status == MFX_ERR_MORE_DATA) {
|
|
|
|
flow = GST_FLOW_OK;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-06-22 01:12:48 +00:00
|
|
|
if (!klass->post_configure (thiz)) {
|
|
|
|
flow = GST_FLOW_ERROR;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2018-11-20 07:17:44 +00:00
|
|
|
if (!thiz->initialized)
|
|
|
|
hard_reset = TRUE;
|
2019-07-26 13:45:46 +00:00
|
|
|
else {
|
|
|
|
GstVideoCodecState *output_state =
|
|
|
|
gst_video_decoder_get_output_state (GST_VIDEO_DECODER (thiz));
|
|
|
|
if (output_state) {
|
|
|
|
if (output_state->allocation_caps) {
|
2022-02-17 07:12:52 +00:00
|
|
|
if (!gst_video_info_from_caps (&alloc_info,
|
|
|
|
output_state->allocation_caps)) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "Failed to get video info from caps");
|
|
|
|
flow = GST_FLOW_ERROR;
|
|
|
|
goto error;
|
|
|
|
}
|
2019-07-26 13:45:46 +00:00
|
|
|
|
|
|
|
/* Check whether we need complete reset for dynamic resolution change */
|
|
|
|
if (thiz->param.mfx.FrameInfo.Width >
|
|
|
|
GST_VIDEO_INFO_WIDTH (&alloc_info)
|
|
|
|
|| thiz->param.mfx.FrameInfo.Height >
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&alloc_info))
|
|
|
|
hard_reset = TRUE;
|
|
|
|
}
|
|
|
|
gst_video_codec_state_unref (output_state);
|
|
|
|
}
|
2019-07-22 16:52:53 +00:00
|
|
|
|
2018-11-20 07:17:44 +00:00
|
|
|
}
|
2018-07-03 00:17:49 +00:00
|
|
|
|
|
|
|
/* if subclass requested for the force reset */
|
|
|
|
if (thiz->force_reset_on_res_change)
|
|
|
|
hard_reset = TRUE;
|
|
|
|
|
2019-01-21 15:43:11 +00:00
|
|
|
if (!gst_msdkdec_negotiate (thiz, hard_reset)) {
|
|
|
|
GST_ELEMENT_ERROR (thiz, CORE, NEGOTIATION,
|
|
|
|
("Could not negotiate the stream"), (NULL));
|
|
|
|
flow = GST_FLOW_ERROR;
|
|
|
|
goto error;
|
|
|
|
}
|
2018-07-03 00:17:49 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 16:50:05 +00:00
|
|
|
/* gst_msdkdec_handle_frame owns one ref on input argument |frame|. At this
|
|
|
|
* point this frame is not used so just unref it right away.
|
|
|
|
* gst_msdkdec_finish_task is fetching the frames itself. */
|
|
|
|
gst_video_codec_frame_unref (frame);
|
|
|
|
frame = NULL;
|
2016-11-07 20:32:38 +00:00
|
|
|
for (;;) {
|
|
|
|
task = &g_array_index (thiz->tasks, MsdkDecTask, thiz->next_task);
|
2017-01-20 19:11:50 +00:00
|
|
|
flow = gst_msdkdec_finish_task (thiz, task);
|
2019-12-06 16:50:05 +00:00
|
|
|
if (flow != GST_FLOW_OK) {
|
|
|
|
if (flow == GST_FLOW_ERROR)
|
|
|
|
GST_ERROR_OBJECT (thiz, "Failed to finish a task");
|
2018-03-08 20:38:52 +00:00
|
|
|
goto error;
|
2019-12-06 16:50:05 +00:00
|
|
|
}
|
2016-11-07 20:32:38 +00:00
|
|
|
if (!surface) {
|
2017-01-20 19:11:50 +00:00
|
|
|
flow = allocate_output_buffer (thiz, &buffer);
|
2019-08-21 08:44:39 +00:00
|
|
|
if (flow == GST_FLOW_CUSTOM_SUCCESS) {
|
|
|
|
flow = GST_FLOW_OK;
|
|
|
|
break;
|
|
|
|
} else if (flow != GST_FLOW_OK)
|
2018-03-08 20:38:52 +00:00
|
|
|
goto error;
|
2017-01-20 19:11:50 +00:00
|
|
|
surface = get_surface (thiz, buffer);
|
2016-11-07 20:32:38 +00:00
|
|
|
if (!surface) {
|
2019-07-23 17:47:44 +00:00
|
|
|
/* Can't get a surface for some reason; finish tasks, then see if
|
2016-11-07 20:32:38 +00:00
|
|
|
a surface becomes available. */
|
|
|
|
for (i = 0; i < thiz->tasks->len - 1; i++) {
|
|
|
|
thiz->next_task = (thiz->next_task + 1) % thiz->tasks->len;
|
|
|
|
task = &g_array_index (thiz->tasks, MsdkDecTask, thiz->next_task);
|
2017-01-20 19:11:50 +00:00
|
|
|
flow = gst_msdkdec_finish_task (thiz, task);
|
|
|
|
if (flow != GST_FLOW_OK)
|
2018-03-08 20:38:52 +00:00
|
|
|
goto error;
|
2017-01-20 19:11:50 +00:00
|
|
|
surface = get_surface (thiz, buffer);
|
2016-11-07 20:32:38 +00:00
|
|
|
if (surface)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!surface) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "Couldn't get a surface");
|
2017-01-20 19:11:50 +00:00
|
|
|
flow = GST_FLOW_ERROR;
|
2018-03-08 20:38:52 +00:00
|
|
|
goto error;
|
2016-11-07 20:32:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 04:48:37 +00:00
|
|
|
#if (MFX_VERSION >= 1025)
|
|
|
|
if (thiz->report_error)
|
|
|
|
thiz->error_report.ErrorTypes = 0;
|
|
|
|
#endif
|
2016-11-07 20:32:38 +00:00
|
|
|
|
|
|
|
status =
|
2018-02-13 22:49:28 +00:00
|
|
|
MFXVideoDECODE_DecodeFrameAsync (session, &bitstream, surface->surface,
|
2020-05-14 03:03:49 +00:00
|
|
|
&out_surface, &task->sync_point);
|
|
|
|
|
|
|
|
if (!find_msdk_surface (thiz, task, out_surface)) {
|
|
|
|
flow = GST_FLOW_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-11-06 12:45:09 +00:00
|
|
|
GST_DEBUG_OBJECT (decoder, "DecodeFrameAsync => %d", status);
|
2019-12-06 04:48:37 +00:00
|
|
|
gst_msdkdec_error_report (thiz);
|
2018-07-03 00:17:49 +00:00
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* media-sdk requires complete reset since the surface is inadequate
|
|
|
|
* for further decoding */
|
2019-03-06 05:07:53 +00:00
|
|
|
if (status == MFX_ERR_INCOMPATIBLE_VIDEO_PARAM &&
|
|
|
|
retry_err_incompatible++ < 1) {
|
2019-02-27 00:07:29 +00:00
|
|
|
/* MFX_ERR_INCOMPATIBLE_VIDEO_PARAM means the current mfx surface is not
|
2019-07-23 17:47:44 +00:00
|
|
|
* suitable for the current frame. Call MFXVideoDECODE_DecodeHeader to get
|
|
|
|
* the current frame size, then do memory re-allocation, otherwise
|
|
|
|
* MFXVideoDECODE_DecodeFrameAsync will still fail on next call */
|
2019-12-06 04:48:37 +00:00
|
|
|
#if (MFX_VERSION >= 1025)
|
|
|
|
if (thiz->report_error)
|
|
|
|
thiz->error_report.ErrorTypes = 0;
|
|
|
|
#endif
|
2019-02-27 00:07:29 +00:00
|
|
|
status = MFXVideoDECODE_DecodeHeader (session, &bitstream, &thiz->param);
|
2019-11-06 12:45:09 +00:00
|
|
|
GST_DEBUG_OBJECT (decoder, "DecodeHeader => %d", status);
|
2019-12-06 04:48:37 +00:00
|
|
|
gst_msdkdec_error_report (thiz);
|
|
|
|
|
2019-02-27 00:07:29 +00:00
|
|
|
if (status == MFX_ERR_MORE_DATA) {
|
|
|
|
flow = GST_FLOW_OK;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-01-21 11:46:32 +00:00
|
|
|
/* Requires memory re-allocation, do a hard reset */
|
2018-07-03 00:17:49 +00:00
|
|
|
if (!gst_msdkdec_negotiate (thiz, TRUE))
|
|
|
|
goto error;
|
2019-02-27 00:07:29 +00:00
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* The current surface is freed when doing a hard reset; a new surface is
|
2019-02-27 00:07:29 +00:00
|
|
|
* required for the new resolution */
|
|
|
|
surface = NULL;
|
|
|
|
continue;
|
2018-07-03 00:17:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 05:07:53 +00:00
|
|
|
retry_err_incompatible = 0;
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
if (G_LIKELY (status == MFX_ERR_NONE)
|
|
|
|
|| (status == MFX_WRN_VIDEO_PARAM_CHANGED)) {
|
2016-11-07 20:32:38 +00:00
|
|
|
thiz->next_task = (thiz->next_task + 1) % thiz->tasks->len;
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2019-11-18 22:26:31 +00:00
|
|
|
if (surface->surface->Data.Locked > 0)
|
2018-02-13 22:49:28 +00:00
|
|
|
surface = NULL;
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
if (bitstream.DataLength == 0) {
|
2017-01-20 19:11:50 +00:00
|
|
|
flow = GST_FLOW_OK;
|
2019-09-17 06:42:51 +00:00
|
|
|
|
|
|
|
/* Don't release it if the current surface is in use */
|
2020-05-14 03:03:49 +00:00
|
|
|
if (surface && task->surface->surface == surface->surface)
|
2019-09-17 06:42:51 +00:00
|
|
|
surface = NULL;
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (status == MFX_ERR_MORE_DATA) {
|
2019-02-11 05:53:19 +00:00
|
|
|
if (task->surface) {
|
|
|
|
task->decode_only = TRUE;
|
|
|
|
thiz->next_task = (thiz->next_task + 1) % thiz->tasks->len;
|
|
|
|
}
|
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
if (surface->surface->Data.Locked > 0)
|
|
|
|
surface = NULL;
|
2019-02-02 05:26:32 +00:00
|
|
|
flow = GST_VIDEO_DECODER_FLOW_NEED_DATA;
|
2016-11-07 20:32:38 +00:00
|
|
|
break;
|
|
|
|
} else if (status == MFX_ERR_MORE_SURFACE) {
|
|
|
|
surface = NULL;
|
|
|
|
continue;
|
2018-02-13 22:49:28 +00:00
|
|
|
} else if (status == MFX_WRN_DEVICE_BUSY) {
|
2019-07-23 17:47:44 +00:00
|
|
|
/* If device is busy, wait 1ms and retry, as per MSDK's recommendation */
|
2016-11-07 20:32:38 +00:00
|
|
|
g_usleep (1000);
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2020-05-14 03:03:49 +00:00
|
|
|
if (surface->surface->Data.Locked > 0)
|
2019-02-11 08:49:32 +00:00
|
|
|
surface = NULL;
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* If the current surface is still busy, we should do sync operation,
|
|
|
|
* then try to decode again
|
2018-02-13 22:49:28 +00:00
|
|
|
*/
|
|
|
|
thiz->next_task = (thiz->next_task + 1) % thiz->tasks->len;
|
|
|
|
} else if (status < MFX_ERR_NONE) {
|
2016-11-07 20:32:38 +00:00
|
|
|
GST_ERROR_OBJECT (thiz, "DecodeFrameAsync failed (%s)",
|
|
|
|
msdk_status_to_string (status));
|
2017-01-20 19:11:50 +00:00
|
|
|
flow = GST_FLOW_ERROR;
|
2016-11-07 20:32:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 12:28:28 +00:00
|
|
|
if (!gst_video_decoder_get_packetized (decoder)) {
|
2019-07-23 17:47:44 +00:00
|
|
|
/* flush out the data which has already been consumed by msdk */
|
2018-02-13 23:41:20 +00:00
|
|
|
gst_adapter_flush (thiz->adapter, bitstream.DataOffset);
|
|
|
|
}
|
|
|
|
|
2019-05-24 08:48:45 +00:00
|
|
|
/*
|
|
|
|
* DecodedOrder was deprecated in msdk-2017 version, but some
|
|
|
|
* customers still using this for low-latency streaming of non-b-frame
|
|
|
|
* encoded streams, which needs to output the frame at once
|
|
|
|
*/
|
|
|
|
if (thiz->param.mfx.DecodedOrder == GST_MSDKDEC_OUTPUT_ORDER_DECODE)
|
|
|
|
gst_msdkdec_finish_task (thiz, task);
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
done:
|
2019-01-11 08:18:11 +00:00
|
|
|
gst_buffer_unmap (input_buffer, &map_info);
|
|
|
|
gst_buffer_unref (input_buffer);
|
2017-01-20 19:11:50 +00:00
|
|
|
return flow;
|
2018-03-08 20:38:52 +00:00
|
|
|
|
|
|
|
error:
|
2019-01-11 08:18:11 +00:00
|
|
|
if (input_buffer) {
|
|
|
|
gst_buffer_unmap (input_buffer, &map_info);
|
|
|
|
gst_buffer_unref (input_buffer);
|
|
|
|
}
|
2019-12-06 16:50:05 +00:00
|
|
|
if (frame)
|
|
|
|
gst_video_decoder_drop_frame (decoder, frame);
|
2018-03-08 20:38:52 +00:00
|
|
|
|
|
|
|
return flow;
|
2016-11-07 20:32:38 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 02:50:18 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_msdkdec_parse (GstVideoDecoder * decoder, GstVideoCodecFrame * frame,
|
|
|
|
GstAdapter * adapter, gboolean at_eos)
|
|
|
|
{
|
|
|
|
gsize size;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
GstBuffer *buffer;
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (decoder);
|
|
|
|
|
|
|
|
/* Don't parse the input buffer indeed, it will invoke
|
|
|
|
* gst_msdkdec_handle_frame to handle the input buffer */
|
|
|
|
size = gst_adapter_available (adapter);
|
|
|
|
gst_video_decoder_add_to_frame (decoder, size);
|
|
|
|
ret = gst_video_decoder_have_frame (decoder);
|
|
|
|
size = gst_adapter_available (thiz->adapter);
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
/* The base class will set up a new frame for parsing as
|
|
|
|
* soon as there is valid data in the buffer */
|
|
|
|
buffer = gst_adapter_get_buffer (thiz->adapter, size);
|
|
|
|
gst_adapter_flush (thiz->adapter, size);
|
|
|
|
gst_adapter_push (adapter, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-07-03 00:17:49 +00:00
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
static GstBufferPool *
|
2018-07-03 00:17:49 +00:00
|
|
|
gst_msdkdec_create_buffer_pool (GstMsdkDec * thiz, GstVideoInfo * info,
|
2018-02-13 22:49:28 +00:00
|
|
|
guint num_buffers)
|
|
|
|
{
|
|
|
|
GstBufferPool *pool = NULL;
|
|
|
|
GstStructure *config;
|
|
|
|
GstAllocator *allocator = NULL;
|
|
|
|
GstVideoAlignment align;
|
2018-07-03 00:17:49 +00:00
|
|
|
GstCaps *caps = NULL;
|
2018-02-13 22:49:28 +00:00
|
|
|
GstAllocationParams params = { 0, 31, 0, 0, };
|
|
|
|
mfxFrameAllocResponse *alloc_resp = NULL;
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
g_return_val_if_fail (info, NULL);
|
|
|
|
g_return_val_if_fail (GST_VIDEO_INFO_WIDTH (info)
|
|
|
|
&& GST_VIDEO_INFO_HEIGHT (info), NULL);
|
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
alloc_resp = &thiz->alloc_resp;
|
|
|
|
|
|
|
|
pool = gst_msdk_buffer_pool_new (thiz->context, alloc_resp);
|
|
|
|
if (!pool)
|
|
|
|
goto error_no_pool;
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
caps = gst_video_info_to_caps (info);
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
/* allocators should use the same width/height/stride/height_alignment of
|
2019-07-23 17:47:44 +00:00
|
|
|
* negotiated output caps, which is what we configure in msdk_allocator */
|
2018-03-30 19:06:40 +00:00
|
|
|
if (thiz->use_dmabuf)
|
2018-07-03 00:17:49 +00:00
|
|
|
allocator = gst_msdk_dmabuf_allocator_new (thiz->context, info, alloc_resp);
|
2018-03-30 19:06:40 +00:00
|
|
|
else if (thiz->use_video_memory)
|
2018-07-03 00:17:49 +00:00
|
|
|
allocator = gst_msdk_video_allocator_new (thiz->context, info, alloc_resp);
|
2018-02-13 22:49:28 +00:00
|
|
|
else
|
2018-07-03 00:17:49 +00:00
|
|
|
allocator = gst_msdk_system_allocator_new (info);
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2019-09-10 06:44:19 +00:00
|
|
|
if (!allocator) {
|
|
|
|
gst_caps_unref (caps);
|
2018-02-13 22:49:28 +00:00
|
|
|
goto error_no_allocator;
|
2019-09-10 06:44:19 +00:00
|
|
|
}
|
2018-02-13 22:49:28 +00:00
|
|
|
|
|
|
|
config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
|
2020-05-14 03:03:49 +00:00
|
|
|
/* we need register all bufffers when we create the msdk context, so the buffer pool is not resize able */
|
2018-07-03 00:17:49 +00:00
|
|
|
gst_buffer_pool_config_set_params (config, caps,
|
2020-05-14 03:03:49 +00:00
|
|
|
GST_VIDEO_INFO_SIZE (info), num_buffers, num_buffers);
|
2018-02-13 22:49:28 +00:00
|
|
|
gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
|
|
|
|
gst_buffer_pool_config_add_option (config,
|
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
|
2019-09-10 06:44:19 +00:00
|
|
|
gst_caps_unref (caps);
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2018-03-30 19:06:40 +00:00
|
|
|
if (thiz->use_video_memory) {
|
2018-02-13 22:49:28 +00:00
|
|
|
gst_buffer_pool_config_add_option (config,
|
|
|
|
GST_BUFFER_POOL_OPTION_MSDK_USE_VIDEO_MEMORY);
|
2018-03-30 19:06:40 +00:00
|
|
|
if (thiz->use_dmabuf)
|
|
|
|
gst_buffer_pool_config_add_option (config,
|
|
|
|
GST_BUFFER_POOL_OPTION_MSDK_USE_DMABUF);
|
|
|
|
}
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2020-05-14 03:03:49 +00:00
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
gst_buffer_pool_config_set_video_alignment (config, &align);
|
|
|
|
gst_buffer_pool_config_set_allocator (config, allocator, ¶ms);
|
|
|
|
gst_object_unref (allocator);
|
|
|
|
|
|
|
|
if (!gst_buffer_pool_set_config (pool, config))
|
|
|
|
goto error_pool_config;
|
|
|
|
|
|
|
|
return pool;
|
|
|
|
|
|
|
|
error_no_pool:
|
|
|
|
{
|
|
|
|
GST_INFO_OBJECT (thiz, "failed to create bufferpool");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
error_no_allocator:
|
|
|
|
{
|
|
|
|
GST_INFO_OBJECT (thiz, "failed to create allocator");
|
2019-01-09 00:58:00 +00:00
|
|
|
gst_object_unref (pool);
|
2018-02-13 22:49:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
error_pool_config:
|
|
|
|
{
|
|
|
|
GST_INFO_OBJECT (thiz, "failed to set config");
|
2019-01-09 00:58:00 +00:00
|
|
|
gst_object_unref (pool);
|
|
|
|
gst_object_unref (allocator);
|
2018-02-13 22:49:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
|
|
|
|
{
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (decoder);
|
|
|
|
GstBufferPool *pool = NULL;
|
|
|
|
GstStructure *pool_config = NULL;
|
2018-07-03 00:17:49 +00:00
|
|
|
GstCaps *pool_caps /*, *negotiated_caps */ ;
|
2016-11-07 20:32:38 +00:00
|
|
|
guint size, min_buffers, max_buffers;
|
|
|
|
|
|
|
|
if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
|
|
|
|
query))
|
|
|
|
return FALSE;
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* Get the buffer pool config decided on by the base class. The base
|
2016-11-07 20:32:38 +00:00
|
|
|
class ensures that there will always be at least a 0th pool in
|
|
|
|
the query. */
|
|
|
|
gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
|
|
|
|
pool_config = gst_buffer_pool_get_config (pool);
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* Get the caps of pool and increase the min and max buffers by async_depth.
|
|
|
|
* We will always have that number of decode operations in-flight */
|
2016-11-07 20:32:38 +00:00
|
|
|
gst_buffer_pool_config_get_params (pool_config, &pool_caps, &size,
|
|
|
|
&min_buffers, &max_buffers);
|
|
|
|
min_buffers += thiz->async_depth;
|
|
|
|
if (max_buffers)
|
|
|
|
max_buffers += thiz->async_depth;
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
/* increase the min_buffers by 1 for smooth display in render pipeline */
|
|
|
|
min_buffers += 1;
|
|
|
|
|
|
|
|
/* this will get updated with msdk requirement */
|
|
|
|
thiz->min_prealloc_buffers = min_buffers;
|
|
|
|
|
2018-03-30 19:06:40 +00:00
|
|
|
if (_gst_caps_has_feature (pool_caps, GST_CAPS_FEATURE_MEMORY_DMABUF)) {
|
|
|
|
GST_INFO_OBJECT (decoder, "This MSDK decoder uses DMABuf memory");
|
|
|
|
thiz->use_video_memory = thiz->use_dmabuf = TRUE;
|
2022-01-21 07:32:09 +00:00
|
|
|
} else if (thiz->sfc)
|
|
|
|
thiz->use_video_memory = TRUE;
|
2018-03-30 19:06:40 +00:00
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
/* Initialize MSDK decoder before new bufferpool tries to alloc each buffer,
|
2019-07-23 17:47:44 +00:00
|
|
|
* which requires information about frame allocation.
|
2018-07-03 00:17:49 +00:00
|
|
|
* No effect if already initialized.
|
|
|
|
*/
|
|
|
|
if (!gst_msdkdec_init_decoder (thiz))
|
|
|
|
return FALSE;
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* get the updated min_buffers, which account for the msdk requirement as well */
|
2018-07-03 00:17:49 +00:00
|
|
|
min_buffers = thiz->min_prealloc_buffers;
|
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* Decoder always use its own pool. So we create a pool if msdk APIs
|
2018-07-03 00:17:49 +00:00
|
|
|
* previously requested for allocation (do_realloc = TRUE) */
|
|
|
|
if (thiz->do_realloc || !thiz->pool) {
|
2019-07-22 16:52:53 +00:00
|
|
|
GstVideoCodecState *output_state =
|
|
|
|
gst_video_decoder_get_output_state (GST_VIDEO_DECODER (thiz));
|
2020-01-17 08:15:35 +00:00
|
|
|
gst_clear_object (&thiz->pool);
|
2018-07-03 00:17:49 +00:00
|
|
|
GST_INFO_OBJECT (decoder, "create new MSDK bufferpool");
|
|
|
|
thiz->pool =
|
2019-07-22 16:52:53 +00:00
|
|
|
gst_msdkdec_create_buffer_pool (thiz, &output_state->info, min_buffers);
|
|
|
|
gst_video_codec_state_unref (output_state);
|
|
|
|
if (!thiz->pool) {
|
|
|
|
GST_ERROR_OBJECT (decoder, "failed to create new pool");
|
2018-07-03 00:17:49 +00:00
|
|
|
goto failed_to_create_pool;
|
2019-07-22 16:52:53 +00:00
|
|
|
}
|
2018-07-03 00:17:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)
|
|
|
|
&& gst_buffer_pool_has_option (pool,
|
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT)) {
|
|
|
|
GstStructure *config;
|
|
|
|
GstAllocator *allocator;
|
|
|
|
|
|
|
|
/* If downstream supports video meta and video alignment,
|
2019-07-23 17:47:44 +00:00
|
|
|
* we can replace with our own msdk bufferpool and use it
|
2018-02-13 22:49:28 +00:00
|
|
|
*/
|
|
|
|
/* Remove downstream's pool */
|
|
|
|
gst_structure_free (pool_config);
|
|
|
|
gst_object_unref (pool);
|
|
|
|
|
|
|
|
pool = gst_object_ref (thiz->pool);
|
|
|
|
|
|
|
|
/* Set the allocator of new msdk bufferpool */
|
|
|
|
config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool));
|
|
|
|
|
|
|
|
if (gst_buffer_pool_config_get_allocator (config, &allocator, NULL))
|
|
|
|
gst_query_set_nth_allocation_param (query, 0, allocator, NULL);
|
|
|
|
gst_structure_free (config);
|
|
|
|
} else {
|
2019-07-23 17:47:44 +00:00
|
|
|
/* Unfortunately, downstream doesn't have videometa or alignment support,
|
2018-07-03 00:17:49 +00:00
|
|
|
* we keep msdk pool as a side-pool that will be decoded into and
|
|
|
|
* then copied from.
|
2018-02-13 22:49:28 +00:00
|
|
|
*/
|
2019-07-22 16:52:53 +00:00
|
|
|
GstVideoCodecState *output_state = NULL;
|
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
GST_INFO_OBJECT (decoder, "Keep MSDK bufferpool as a side-pool");
|
2018-02-13 22:49:28 +00:00
|
|
|
|
|
|
|
/* Update params to downstream's pool */
|
2016-11-07 20:32:38 +00:00
|
|
|
gst_buffer_pool_config_set_params (pool_config, pool_caps, size,
|
|
|
|
min_buffers, max_buffers);
|
2018-02-13 22:49:28 +00:00
|
|
|
if (!gst_buffer_pool_set_config (pool, pool_config))
|
|
|
|
goto error_set_config;
|
2022-02-17 07:12:52 +00:00
|
|
|
if (!gst_video_info_from_caps (&thiz->non_msdk_pool_info, pool_caps)) {
|
|
|
|
GST_ERROR_OBJECT (thiz, "Failed to get video info from caps");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2018-07-03 00:17:49 +00:00
|
|
|
/* update width and height with actual negotiated values */
|
2019-07-22 16:52:53 +00:00
|
|
|
output_state =
|
|
|
|
gst_video_decoder_get_output_state (GST_VIDEO_DECODER (thiz));
|
2018-07-03 00:17:49 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&thiz->non_msdk_pool_info) =
|
2019-07-22 16:52:53 +00:00
|
|
|
GST_VIDEO_INFO_WIDTH (&output_state->info);
|
2018-07-03 00:17:49 +00:00
|
|
|
GST_VIDEO_INFO_HEIGHT (&thiz->non_msdk_pool_info) =
|
2019-07-22 16:52:53 +00:00
|
|
|
GST_VIDEO_INFO_HEIGHT (&output_state->info);
|
|
|
|
gst_video_codec_state_unref (output_state);
|
2016-11-07 20:32:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_query_set_nth_allocation_pool (query, 0, pool, size, min_buffers,
|
|
|
|
max_buffers);
|
|
|
|
|
2017-11-22 02:33:54 +00:00
|
|
|
if (pool)
|
|
|
|
gst_object_unref (pool);
|
|
|
|
|
2018-02-13 22:53:02 +00:00
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
return TRUE;
|
2017-11-22 02:33:54 +00:00
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
failed_to_create_pool:
|
2017-11-22 02:33:54 +00:00
|
|
|
GST_ERROR_OBJECT (decoder, "failed to set buffer pool config");
|
|
|
|
if (pool)
|
|
|
|
gst_object_unref (pool);
|
|
|
|
return FALSE;
|
2017-01-20 19:11:50 +00:00
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
error_set_config:
|
|
|
|
GST_ERROR_OBJECT (decoder, "failed to set buffer pool config");
|
|
|
|
if (pool)
|
|
|
|
gst_object_unref (pool);
|
|
|
|
return FALSE;
|
2017-01-20 19:11:50 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_msdkdec_drain (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (decoder);
|
2017-01-20 19:11:50 +00:00
|
|
|
GstFlowReturn flow;
|
|
|
|
GstBuffer *buffer;
|
2016-11-07 20:32:38 +00:00
|
|
|
MsdkDecTask *task;
|
|
|
|
MsdkSurface *surface = NULL;
|
2020-05-14 03:03:49 +00:00
|
|
|
mfxFrameSurface1 *out_surface;
|
2016-11-07 20:32:38 +00:00
|
|
|
mfxSession session;
|
|
|
|
mfxStatus status;
|
|
|
|
guint i;
|
|
|
|
|
2018-02-13 22:51:18 +00:00
|
|
|
if (!thiz->initialized)
|
2016-11-07 20:32:38 +00:00
|
|
|
return GST_FLOW_OK;
|
2018-02-13 21:41:28 +00:00
|
|
|
session = gst_msdk_context_get_session (thiz->context);
|
2016-11-07 20:32:38 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
task = &g_array_index (thiz->tasks, MsdkDecTask, thiz->next_task);
|
2018-02-13 22:49:28 +00:00
|
|
|
if ((flow = gst_msdkdec_finish_task (thiz, task)) != GST_FLOW_OK) {
|
|
|
|
if (flow != GST_FLOW_FLUSHING)
|
|
|
|
GST_WARNING_OBJECT (decoder,
|
|
|
|
"failed to finish the task %p, but keep draining for the remaining frames",
|
|
|
|
task);
|
2017-11-23 03:38:11 +00:00
|
|
|
}
|
2017-11-22 02:33:54 +00:00
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
if (!surface) {
|
2017-01-20 19:11:50 +00:00
|
|
|
flow = allocate_output_buffer (thiz, &buffer);
|
|
|
|
if (flow != GST_FLOW_OK)
|
|
|
|
return flow;
|
|
|
|
surface = get_surface (thiz, buffer);
|
2016-11-07 20:32:38 +00:00
|
|
|
if (!surface)
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2019-12-06 04:48:37 +00:00
|
|
|
#if (MFX_VERSION >= 1025)
|
|
|
|
if (thiz->report_error)
|
|
|
|
thiz->error_report.ErrorTypes = 0;
|
|
|
|
#endif
|
2016-11-07 20:32:38 +00:00
|
|
|
|
|
|
|
status =
|
2018-02-13 22:49:28 +00:00
|
|
|
MFXVideoDECODE_DecodeFrameAsync (session, NULL, surface->surface,
|
2020-05-14 03:03:49 +00:00
|
|
|
&out_surface, &task->sync_point);
|
|
|
|
|
|
|
|
if (!find_msdk_surface (thiz, task, out_surface)) {
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-11-06 12:45:09 +00:00
|
|
|
GST_DEBUG_OBJECT (decoder, "DecodeFrameAsync => %d", status);
|
2019-12-06 04:48:37 +00:00
|
|
|
gst_msdkdec_error_report (thiz);
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
if (G_LIKELY (status == MFX_ERR_NONE)) {
|
|
|
|
thiz->next_task = (thiz->next_task + 1) % thiz->tasks->len;
|
|
|
|
surface = NULL;
|
2018-02-13 22:49:28 +00:00
|
|
|
} else if (status == MFX_WRN_VIDEO_PARAM_CHANGED) {
|
2016-11-07 20:32:38 +00:00
|
|
|
continue;
|
2018-02-13 22:49:28 +00:00
|
|
|
} else if (status == MFX_WRN_DEVICE_BUSY) {
|
2016-11-07 20:32:38 +00:00
|
|
|
/* If device is busy, wait 1ms and retry, as per MSDK's recomendation */
|
|
|
|
g_usleep (1000);
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2019-07-23 17:47:44 +00:00
|
|
|
/* If the current surface is still busy, we should do sync operation,
|
|
|
|
* then try to decode again
|
2018-02-13 22:49:28 +00:00
|
|
|
*/
|
|
|
|
thiz->next_task = (thiz->next_task + 1) % thiz->tasks->len;
|
|
|
|
} else if (status == MFX_ERR_MORE_DATA) {
|
2016-11-07 20:32:38 +00:00
|
|
|
break;
|
2018-02-13 22:49:28 +00:00
|
|
|
} else if (status == MFX_ERR_MORE_SURFACE) {
|
|
|
|
surface = NULL;
|
|
|
|
continue;
|
|
|
|
} else if (status < MFX_ERR_NONE)
|
2016-11-07 20:32:38 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < thiz->tasks->len; i++) {
|
|
|
|
task = &g_array_index (thiz->tasks, MsdkDecTask, thiz->next_task);
|
2018-03-08 20:38:52 +00:00
|
|
|
gst_msdkdec_finish_task (thiz, task);
|
2016-11-07 20:32:38 +00:00
|
|
|
thiz->next_task = (thiz->next_task + 1) % thiz->tasks->len;
|
|
|
|
}
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-13 22:49:28 +00:00
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_flush (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (decoder);
|
2020-01-02 03:22:07 +00:00
|
|
|
GstFlowReturn ret;
|
|
|
|
|
|
|
|
ret = gst_msdkdec_drain (GST_VIDEO_DECODER_CAST (thiz));
|
2018-02-13 22:49:28 +00:00
|
|
|
|
2020-01-02 03:22:07 +00:00
|
|
|
return ret == GST_FLOW_OK;
|
2018-02-13 22:49:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_msdkdec_finish (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
return gst_msdkdec_drain (decoder);
|
|
|
|
}
|
|
|
|
|
2021-08-17 14:34:43 +00:00
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_query (GstVideoDecoder * decoder, GstQuery * query,
|
|
|
|
GstPadDirection dir)
|
|
|
|
{
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (decoder);
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_CONTEXT:{
|
|
|
|
GstMsdkContext *msdk_context = NULL;
|
|
|
|
|
|
|
|
gst_object_replace ((GstObject **) & msdk_context,
|
|
|
|
(GstObject *) thiz->context);
|
|
|
|
ret = gst_msdk_handle_context_query (GST_ELEMENT_CAST (decoder),
|
|
|
|
query, msdk_context);
|
|
|
|
gst_clear_object (&msdk_context);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if (dir == GST_PAD_SRC) {
|
|
|
|
ret =
|
|
|
|
GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
|
|
|
|
} else {
|
|
|
|
ret =
|
|
|
|
GST_VIDEO_DECODER_CLASS (parent_class)->sink_query (decoder, query);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_src_query (GstVideoDecoder * decoder, GstQuery * query)
|
|
|
|
{
|
|
|
|
return gst_msdkdec_query (decoder, query, GST_PAD_SRC);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_sink_query (GstVideoDecoder * decoder, GstQuery * query)
|
|
|
|
{
|
|
|
|
return gst_msdkdec_query (decoder, query, GST_PAD_SINK);
|
|
|
|
}
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
static void
|
|
|
|
gst_msdkdec_set_property (GObject * object, guint prop_id, const GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (object);
|
|
|
|
GstState state;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (thiz);
|
|
|
|
|
|
|
|
state = GST_STATE (thiz);
|
|
|
|
if ((state != GST_STATE_READY && state != GST_STATE_NULL) &&
|
|
|
|
!(pspec->flags & GST_PARAM_MUTABLE_PLAYING))
|
|
|
|
goto wrong_state;
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2018-11-20 06:36:30 +00:00
|
|
|
case GST_MSDKDEC_PROP_HARDWARE:
|
2016-11-07 20:32:38 +00:00
|
|
|
thiz->hardware = g_value_get_boolean (value);
|
|
|
|
break;
|
2018-11-20 06:36:30 +00:00
|
|
|
case GST_MSDKDEC_PROP_ASYNC_DEPTH:
|
2016-11-07 20:32:38 +00:00
|
|
|
thiz->async_depth = g_value_get_uint (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
GST_OBJECT_UNLOCK (thiz);
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* ERROR */
|
|
|
|
wrong_state:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (thiz, "setting property in wrong state");
|
|
|
|
GST_OBJECT_UNLOCK (thiz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_msdkdec_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (object);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (thiz);
|
|
|
|
switch (prop_id) {
|
2018-11-20 06:36:30 +00:00
|
|
|
case GST_MSDKDEC_PROP_HARDWARE:
|
2016-11-07 20:32:38 +00:00
|
|
|
g_value_set_boolean (value, thiz->hardware);
|
|
|
|
break;
|
2018-11-20 06:36:30 +00:00
|
|
|
case GST_MSDKDEC_PROP_ASYNC_DEPTH:
|
2016-11-07 20:32:38 +00:00
|
|
|
g_value_set_uint (value, thiz->async_depth);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
GST_OBJECT_UNLOCK (thiz);
|
|
|
|
}
|
|
|
|
|
2020-01-29 05:02:09 +00:00
|
|
|
static void
|
|
|
|
gst_msdkdec_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (object);
|
|
|
|
|
|
|
|
g_clear_object (&thiz->adapter);
|
2020-01-29 05:10:06 +00:00
|
|
|
gst_clear_object (&thiz->context);
|
|
|
|
gst_clear_object (&thiz->old_context);
|
2020-01-29 05:02:09 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
static void
|
|
|
|
gst_msdkdec_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstMsdkDec *thiz = GST_MSDKDEC (object);
|
|
|
|
|
|
|
|
g_array_unref (thiz->tasks);
|
2020-01-17 08:15:35 +00:00
|
|
|
thiz->tasks = NULL;
|
2019-11-19 23:35:20 +00:00
|
|
|
|
2020-05-14 03:03:49 +00:00
|
|
|
release_msdk_surfaces (thiz);
|
2019-12-13 23:33:06 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
2016-11-07 20:32:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 01:12:48 +00:00
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_post_configure (GstMsdkDec * decoder)
|
|
|
|
{
|
|
|
|
/* Do nothing */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-02-22 08:20:16 +00:00
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_preinit_decoder (GstMsdkDec * decoder)
|
|
|
|
{
|
|
|
|
decoder->param.mfx.FrameInfo.Width =
|
|
|
|
GST_ROUND_UP_16 (decoder->param.mfx.FrameInfo.Width);
|
|
|
|
decoder->param.mfx.FrameInfo.Height =
|
|
|
|
GST_ROUND_UP_32 (decoder->param.mfx.FrameInfo.Height);
|
|
|
|
|
2019-03-29 04:53:38 +00:00
|
|
|
decoder->param.mfx.FrameInfo.PicStruct =
|
|
|
|
decoder->param.mfx.FrameInfo.PicStruct ? decoder->param.mfx.
|
|
|
|
FrameInfo.PicStruct : MFX_PICSTRUCT_PROGRESSIVE;
|
|
|
|
|
2019-02-22 08:20:16 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-03-29 05:22:55 +00:00
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_postinit_decoder (GstMsdkDec * decoder)
|
|
|
|
{
|
|
|
|
/* Do nothing */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-27 07:16:17 +00:00
|
|
|
static gboolean
|
|
|
|
gst_msdkdec_transform_meta (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecFrame * frame, GstMeta * meta)
|
|
|
|
{
|
|
|
|
const GstMetaInfo *info = meta->info;
|
|
|
|
|
|
|
|
if (GST_VIDEO_DECODER_CLASS (parent_class)->transform_meta (decoder, frame,
|
|
|
|
meta))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (!g_strcmp0 (g_type_name (info->type), "GstVideoRegionOfInterestMeta"))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
static void
|
|
|
|
gst_msdkdec_class_init (GstMsdkDecClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *element_class;
|
|
|
|
GstVideoDecoderClass *decoder_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
decoder_class = GST_VIDEO_DECODER_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->set_property = gst_msdkdec_set_property;
|
|
|
|
gobject_class->get_property = gst_msdkdec_get_property;
|
2020-01-29 05:02:09 +00:00
|
|
|
gobject_class->dispose = gst_msdkdec_dispose;
|
2016-11-07 20:32:38 +00:00
|
|
|
gobject_class->finalize = gst_msdkdec_finalize;
|
|
|
|
|
2018-02-13 22:51:18 +00:00
|
|
|
element_class->set_context = gst_msdkdec_set_context;
|
|
|
|
|
2016-11-07 20:32:38 +00:00
|
|
|
decoder_class->close = GST_DEBUG_FUNCPTR (gst_msdkdec_close);
|
2018-02-13 22:51:18 +00:00
|
|
|
decoder_class->start = GST_DEBUG_FUNCPTR (gst_msdkdec_start);
|
2016-11-07 20:32:38 +00:00
|
|
|
decoder_class->stop = GST_DEBUG_FUNCPTR (gst_msdkdec_stop);
|
|
|
|
decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_msdkdec_set_format);
|
|
|
|
decoder_class->finish = GST_DEBUG_FUNCPTR (gst_msdkdec_finish);
|
|
|
|
decoder_class->handle_frame = GST_DEBUG_FUNCPTR (gst_msdkdec_handle_frame);
|
2019-09-27 02:50:18 +00:00
|
|
|
decoder_class->parse = GST_DEBUG_FUNCPTR (gst_msdkdec_parse);
|
2016-11-07 20:32:38 +00:00
|
|
|
decoder_class->decide_allocation =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_msdkdec_decide_allocation);
|
2022-01-21 07:32:09 +00:00
|
|
|
decoder_class->getcaps = GST_DEBUG_FUNCPTR (gst_msdkdec_getcaps);
|
2017-01-20 19:11:50 +00:00
|
|
|
decoder_class->flush = GST_DEBUG_FUNCPTR (gst_msdkdec_flush);
|
2016-11-07 20:32:38 +00:00
|
|
|
decoder_class->drain = GST_DEBUG_FUNCPTR (gst_msdkdec_drain);
|
2019-11-27 07:16:17 +00:00
|
|
|
decoder_class->transform_meta =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_msdkdec_transform_meta);
|
2021-08-17 14:34:43 +00:00
|
|
|
decoder_class->src_query = GST_DEBUG_FUNCPTR (gst_msdkdec_src_query);
|
|
|
|
decoder_class->sink_query = GST_DEBUG_FUNCPTR (gst_msdkdec_sink_query);
|
2016-11-07 20:32:38 +00:00
|
|
|
|
2019-06-22 01:12:48 +00:00
|
|
|
klass->post_configure = GST_DEBUG_FUNCPTR (gst_msdkdec_post_configure);
|
2019-02-22 08:20:16 +00:00
|
|
|
klass->preinit_decoder = GST_DEBUG_FUNCPTR (gst_msdkdec_preinit_decoder);
|
2019-03-29 05:22:55 +00:00
|
|
|
klass->postinit_decoder = GST_DEBUG_FUNCPTR (gst_msdkdec_postinit_decoder);
|
2019-02-22 08:20:16 +00:00
|
|
|
|
2018-11-20 06:36:30 +00:00
|
|
|
g_object_class_install_property (gobject_class, GST_MSDKDEC_PROP_HARDWARE,
|
2016-11-07 20:32:38 +00:00
|
|
|
g_param_spec_boolean ("hardware", "Hardware", "Enable hardware decoders",
|
|
|
|
PROP_HARDWARE_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2018-11-20 06:36:30 +00:00
|
|
|
g_object_class_install_property (gobject_class, GST_MSDKDEC_PROP_ASYNC_DEPTH,
|
2016-11-07 20:32:38 +00:00
|
|
|
g_param_spec_uint ("async-depth", "Async Depth",
|
|
|
|
"Depth of asynchronous pipeline",
|
|
|
|
1, 20, PROP_ASYNC_DEPTH_DEFAULT,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
gst_element_class_add_static_pad_template (element_class, &src_factory);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_msdkdec_init (GstMsdkDec * thiz)
|
|
|
|
{
|
2018-07-03 00:17:49 +00:00
|
|
|
gst_video_info_init (&thiz->non_msdk_pool_info);
|
2016-11-07 20:32:38 +00:00
|
|
|
thiz->tasks = g_array_new (FALSE, TRUE, sizeof (MsdkDecTask));
|
|
|
|
thiz->hardware = PROP_HARDWARE_DEFAULT;
|
|
|
|
thiz->async_depth = PROP_ASYNC_DEPTH_DEFAULT;
|
2018-07-03 00:17:49 +00:00
|
|
|
thiz->do_renego = TRUE;
|
|
|
|
thiz->do_realloc = TRUE;
|
|
|
|
thiz->force_reset_on_res_change = TRUE;
|
2019-12-06 04:48:37 +00:00
|
|
|
thiz->report_error = FALSE;
|
2021-11-17 09:05:09 +00:00
|
|
|
thiz->sfc = FALSE;
|
2018-02-13 23:41:20 +00:00
|
|
|
thiz->adapter = gst_adapter_new ();
|
2019-11-19 23:35:20 +00:00
|
|
|
thiz->input_state = NULL;
|
|
|
|
thiz->pool = NULL;
|
|
|
|
thiz->context = NULL;
|
2016-11-07 20:32:38 +00:00
|
|
|
}
|