2013-01-15 16:33:18 +00:00
|
|
|
/*
|
|
|
|
* simple-decoder.c - Simple Decoder Application
|
|
|
|
*
|
2014-01-22 17:54:14 +00:00
|
|
|
* Copyright (C) 2013-2014 Intel Corporation
|
2013-11-22 04:57:18 +00:00
|
|
|
* Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
|
2013-01-15 16:33:18 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2.1
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a really simple decoder application that only accepts raw
|
|
|
|
* bitstreams. So, it may be needed to suggest what codec to use to
|
|
|
|
* the application.
|
|
|
|
*/
|
|
|
|
|
2013-01-31 10:32:24 +00:00
|
|
|
#include "gst/vaapi/sysdeps.h"
|
2013-01-15 16:33:18 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <gst/vaapi/gstvaapidecoder.h>
|
|
|
|
#include <gst/vaapi/gstvaapidecoder_h264.h>
|
|
|
|
#include <gst/vaapi/gstvaapidecoder_jpeg.h>
|
|
|
|
#include <gst/vaapi/gstvaapidecoder_mpeg2.h>
|
|
|
|
#include <gst/vaapi/gstvaapidecoder_mpeg4.h>
|
|
|
|
#include <gst/vaapi/gstvaapidecoder_vc1.h>
|
|
|
|
#include <gst/vaapi/gstvaapiwindow.h>
|
|
|
|
#include "codec.h"
|
|
|
|
#include "output.h"
|
|
|
|
|
|
|
|
static gchar *g_codec_str;
|
2013-01-16 12:53:43 +00:00
|
|
|
static gboolean g_benchmark;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
|
|
|
static GOptionEntry g_options[] = {
|
2016-04-29 10:48:44 +00:00
|
|
|
{"codec", 'c',
|
|
|
|
0,
|
|
|
|
G_OPTION_ARG_STRING, &g_codec_str,
|
|
|
|
"suggested codec", NULL},
|
|
|
|
{"benchmark", 0,
|
|
|
|
0,
|
|
|
|
G_OPTION_ARG_NONE, &g_benchmark,
|
|
|
|
"benchmark mode", NULL},
|
|
|
|
{NULL,}
|
2013-01-15 16:33:18 +00:00
|
|
|
};
|
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
APP_RUNNING,
|
|
|
|
APP_GOT_EOS,
|
|
|
|
APP_GOT_ERROR,
|
2013-01-15 16:33:18 +00:00
|
|
|
} AppEvent;
|
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
APP_ERROR_NONE,
|
|
|
|
APP_ERROR_DECODER,
|
|
|
|
APP_ERROR_RENDERER,
|
2013-01-15 16:33:18 +00:00
|
|
|
} AppError;
|
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstVaapiSurfaceProxy *proxy;
|
|
|
|
GstClockTime pts;
|
|
|
|
GstClockTime duration;
|
2013-01-18 09:25:14 +00:00
|
|
|
} RenderFrame;
|
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GMutex mutex;
|
|
|
|
GMappedFile *file;
|
|
|
|
gchar *file_name;
|
|
|
|
guint file_offset;
|
|
|
|
guint file_size;
|
|
|
|
guchar *file_data;
|
|
|
|
GstVaapiDisplay *display;
|
|
|
|
GstVaapiDecoder *decoder;
|
|
|
|
GThread *decoder_thread;
|
2021-03-19 06:42:36 +00:00
|
|
|
gboolean decoder_thread_cancel;
|
2016-04-29 10:48:44 +00:00
|
|
|
GAsyncQueue *decoder_queue;
|
|
|
|
GstVaapiCodec codec;
|
|
|
|
guint fps_n;
|
|
|
|
guint fps_d;
|
|
|
|
guint32 frame_duration;
|
|
|
|
guint surface_width;
|
|
|
|
guint surface_height;
|
|
|
|
GstVaapiWindow *window;
|
|
|
|
guint window_width;
|
|
|
|
guint window_height;
|
|
|
|
GThread *render_thread;
|
2021-03-19 06:42:36 +00:00
|
|
|
gboolean render_thread_cancel;
|
2016-04-29 10:48:44 +00:00
|
|
|
GCond render_ready;
|
|
|
|
RenderFrame *last_frame;
|
|
|
|
GError *error;
|
|
|
|
AppEvent event;
|
|
|
|
GCond event_cond;
|
|
|
|
GTimer *timer;
|
|
|
|
guint32 num_frames;
|
2013-01-15 16:33:18 +00:00
|
|
|
} App;
|
|
|
|
|
2013-01-18 09:25:14 +00:00
|
|
|
static inline RenderFrame *
|
2016-04-29 10:48:44 +00:00
|
|
|
render_frame_new (void)
|
2013-01-18 09:25:14 +00:00
|
|
|
{
|
2023-01-24 19:16:47 +00:00
|
|
|
return g_new (RenderFrame, 1);
|
2013-01-18 09:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-04-29 10:48:44 +00:00
|
|
|
render_frame_free (RenderFrame * rfp)
|
2013-01-18 09:25:14 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
if (G_UNLIKELY (!rfp))
|
|
|
|
return;
|
|
|
|
gst_vaapi_surface_proxy_replace (&rfp->proxy, NULL);
|
2023-01-24 19:16:47 +00:00
|
|
|
g_free (rfp);
|
2013-01-18 09:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2016-04-29 10:48:44 +00:00
|
|
|
render_frame_replace (RenderFrame ** rfp_ptr, RenderFrame * new_rfp)
|
2013-01-18 09:25:14 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
if (*rfp_ptr)
|
|
|
|
render_frame_free (*rfp_ptr);
|
|
|
|
*rfp_ptr = new_rfp;
|
2013-01-18 09:25:14 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 16:33:18 +00:00
|
|
|
#define APP_ERROR app_error_quark()
|
|
|
|
static GQuark
|
2016-04-29 10:48:44 +00:00
|
|
|
app_error_quark (void)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
static gsize g_quark;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
if (g_once_init_enter (&g_quark)) {
|
|
|
|
gsize quark = (gsize) g_quark_from_static_string ("AppError");
|
|
|
|
g_once_init_leave (&g_quark, quark);
|
|
|
|
}
|
|
|
|
return g_quark;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-04-29 10:48:44 +00:00
|
|
|
app_send_error (App * app, GError * error)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
g_mutex_lock (&app->mutex);
|
|
|
|
app->error = error;
|
|
|
|
app->event = APP_GOT_ERROR;
|
|
|
|
g_cond_signal (&app->event_cond);
|
|
|
|
g_mutex_unlock (&app->mutex);
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-04-29 10:48:44 +00:00
|
|
|
app_send_eos (App * app)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
g_mutex_lock (&app->mutex);
|
|
|
|
app->event = APP_GOT_EOS;
|
|
|
|
g_cond_signal (&app->event_cond);
|
|
|
|
g_mutex_unlock (&app->mutex);
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const gchar *
|
2016-04-29 10:48:44 +00:00
|
|
|
get_decoder_status_string (GstVaapiDecoderStatus status)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
const gchar *str;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
|
|
|
#define DEFINE_STATUS(status, status_string) \
|
|
|
|
case GST_VAAPI_DECODER_STATUS_##status: \
|
|
|
|
str = status_string; \
|
|
|
|
break
|
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
switch (status) {
|
|
|
|
DEFINE_STATUS (SUCCESS, "<success>");
|
|
|
|
DEFINE_STATUS (END_OF_STREAM, "<EOS>");
|
|
|
|
DEFINE_STATUS (ERROR_ALLOCATION_FAILED, "allocation failed");
|
|
|
|
DEFINE_STATUS (ERROR_INIT_FAILED, "initialization failed");
|
|
|
|
DEFINE_STATUS (ERROR_UNSUPPORTED_CODEC, "unsupported codec");
|
|
|
|
DEFINE_STATUS (ERROR_NO_DATA, "not enough data");
|
|
|
|
DEFINE_STATUS (ERROR_NO_SURFACE, "no surface vailable");
|
|
|
|
DEFINE_STATUS (ERROR_INVALID_SURFACE, "invalid surface");
|
|
|
|
DEFINE_STATUS (ERROR_BITSTREAM_PARSER, "bitstream parser error");
|
|
|
|
DEFINE_STATUS (ERROR_UNSUPPORTED_PROFILE, "unsupported profile");
|
|
|
|
DEFINE_STATUS (ERROR_UNSUPPORTED_CHROMA_FORMAT,
|
|
|
|
"unsupported chroma-format");
|
|
|
|
DEFINE_STATUS (ERROR_INVALID_PARAMETER, "invalid parameter");
|
2013-01-15 16:33:18 +00:00
|
|
|
default:
|
2016-04-29 10:48:44 +00:00
|
|
|
str = "<unknown>";
|
|
|
|
break;
|
|
|
|
}
|
2013-01-15 16:33:18 +00:00
|
|
|
#undef DEFINE_STATUS
|
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
return str;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const gchar *
|
2016-04-29 10:48:44 +00:00
|
|
|
get_error_string (AppError error)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
const gchar *str;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
|
|
|
#define DEFINE_ERROR(error, error_string) \
|
|
|
|
case APP_ERROR_##error: \
|
|
|
|
str = error_string; \
|
|
|
|
break
|
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
switch (error) {
|
|
|
|
DEFINE_ERROR (NONE, "<none>");
|
|
|
|
DEFINE_ERROR (DECODER, "decoder");
|
|
|
|
DEFINE_ERROR (RENDERER, "renderer");
|
2013-01-15 16:33:18 +00:00
|
|
|
default:
|
2016-04-29 10:48:44 +00:00
|
|
|
str = "unknown";
|
|
|
|
break;
|
|
|
|
}
|
2013-01-15 16:33:18 +00:00
|
|
|
#undef DEFINE_ERROR
|
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
return str;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
2016-04-29 10:48:44 +00:00
|
|
|
decoder_thread (gpointer data)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
App *const app = data;
|
|
|
|
GError *error = NULL;
|
|
|
|
GstVaapiDecoderStatus status;
|
|
|
|
GstVaapiSurfaceProxy *proxy;
|
|
|
|
RenderFrame *rfp;
|
|
|
|
GstBuffer *buffer;
|
|
|
|
GstClockTime pts;
|
2020-07-08 15:33:32 +00:00
|
|
|
gboolean got_eos = FALSE;
|
2016-04-29 10:48:44 +00:00
|
|
|
guint ofs;
|
|
|
|
|
|
|
|
g_print ("Decoder thread started\n");
|
2013-01-15 16:33:18 +00:00
|
|
|
|
|
|
|
#define SEND_ERROR(...) \
|
|
|
|
do { \
|
|
|
|
error = g_error_new(APP_ERROR, APP_ERROR_DECODER, __VA_ARGS__); \
|
|
|
|
goto send_error; \
|
|
|
|
} while (0)
|
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
pts = g_get_monotonic_time ();
|
|
|
|
ofs = 0;
|
2021-03-19 06:42:36 +00:00
|
|
|
while (!g_atomic_int_get (&app->decoder_thread_cancel)) {
|
2016-04-29 10:48:44 +00:00
|
|
|
if (G_UNLIKELY (ofs == app->file_size))
|
|
|
|
buffer = NULL;
|
|
|
|
else {
|
|
|
|
const gsize size = MIN (4096, app->file_size - ofs);
|
|
|
|
buffer = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY,
|
|
|
|
app->file_data, app->file_size, ofs, size, NULL, NULL);
|
|
|
|
if (!buffer)
|
|
|
|
SEND_ERROR ("failed to allocate new buffer");
|
|
|
|
ofs += size;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
2016-04-29 10:48:44 +00:00
|
|
|
if (!gst_vaapi_decoder_put_buffer (app->decoder, buffer))
|
|
|
|
SEND_ERROR ("failed to push buffer to decoder");
|
|
|
|
gst_buffer_replace (&buffer, NULL);
|
|
|
|
|
|
|
|
status = gst_vaapi_decoder_get_surface (app->decoder, &proxy);
|
|
|
|
switch (status) {
|
|
|
|
case GST_VAAPI_DECODER_STATUS_SUCCESS:
|
|
|
|
rfp = render_frame_new ();
|
|
|
|
if (!rfp)
|
|
|
|
SEND_ERROR ("failed to allocate render frame");
|
|
|
|
rfp->proxy = proxy;
|
|
|
|
rfp->pts = pts;
|
|
|
|
rfp->duration = app->frame_duration;
|
|
|
|
pts += app->frame_duration;
|
|
|
|
g_async_queue_push (app->decoder_queue, rfp);
|
|
|
|
break;
|
|
|
|
case GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA:
|
|
|
|
/* nothing to do, just continue to the next iteration */
|
|
|
|
break;
|
|
|
|
case GST_VAAPI_DECODER_STATUS_END_OF_STREAM:
|
|
|
|
gst_vaapi_decoder_flush (app->decoder);
|
|
|
|
if (got_eos)
|
|
|
|
goto send_eos;
|
|
|
|
got_eos = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SEND_ERROR ("%s", get_decoder_status_string (status));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
|
|
|
#undef SEND_ERROR
|
|
|
|
|
|
|
|
send_eos:
|
2016-04-29 10:48:44 +00:00
|
|
|
app_send_eos (app);
|
|
|
|
return NULL;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
|
|
|
send_error:
|
2016-04-29 10:48:44 +00:00
|
|
|
app_send_error (app, error);
|
|
|
|
return NULL;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
2013-01-16 12:29:06 +00:00
|
|
|
static void
|
2016-04-29 10:48:44 +00:00
|
|
|
app_set_framerate (App * app, guint fps_n, guint fps_d)
|
2013-01-16 12:29:06 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
if (!fps_n || !fps_d)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_mutex_lock (&app->mutex);
|
|
|
|
if (fps_n != app->fps_n || fps_d != app->fps_d) {
|
|
|
|
app->fps_n = fps_n;
|
|
|
|
app->fps_d = fps_d;
|
|
|
|
app->frame_duration =
|
|
|
|
gst_util_uint64_scale (GST_TIME_AS_USECONDS (GST_SECOND), fps_d, fps_n);
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&app->mutex);
|
2013-01-16 12:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-04-29 10:48:44 +00:00
|
|
|
handle_decoder_state_changes (GstVaapiDecoder * decoder,
|
|
|
|
const GstVideoCodecState * codec_state, gpointer user_data)
|
2013-01-16 12:29:06 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
App *const app = user_data;
|
2013-01-16 12:29:06 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
g_assert (app->decoder == decoder);
|
|
|
|
app_set_framerate (app, codec_state->info.fps_n, codec_state->info.fps_d);
|
2013-01-16 12:29:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 16:33:18 +00:00
|
|
|
static gboolean
|
2016-04-29 10:48:44 +00:00
|
|
|
start_decoder (App * app)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
GstCaps *caps;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
app->file = g_mapped_file_new (app->file_name, FALSE, NULL);
|
|
|
|
if (!app->file)
|
|
|
|
return FALSE;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
app->file_size = g_mapped_file_get_length (app->file);
|
|
|
|
app->file_data = (guint8 *) g_mapped_file_get_contents (app->file);
|
|
|
|
if (!app->file_data)
|
|
|
|
return FALSE;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
caps = caps_from_codec (app->codec);
|
|
|
|
switch (app->codec) {
|
2013-01-15 16:33:18 +00:00
|
|
|
case GST_VAAPI_CODEC_H264:
|
2016-04-29 10:48:44 +00:00
|
|
|
app->decoder = gst_vaapi_decoder_h264_new (app->display, caps);
|
|
|
|
break;
|
2013-01-15 16:33:18 +00:00
|
|
|
case GST_VAAPI_CODEC_JPEG:
|
2016-04-29 10:48:44 +00:00
|
|
|
app->decoder = gst_vaapi_decoder_jpeg_new (app->display, caps);
|
|
|
|
break;
|
2013-01-15 16:33:18 +00:00
|
|
|
case GST_VAAPI_CODEC_MPEG2:
|
2016-04-29 10:48:44 +00:00
|
|
|
app->decoder = gst_vaapi_decoder_mpeg2_new (app->display, caps);
|
|
|
|
break;
|
2013-01-15 16:33:18 +00:00
|
|
|
case GST_VAAPI_CODEC_MPEG4:
|
2016-04-29 10:48:44 +00:00
|
|
|
app->decoder = gst_vaapi_decoder_mpeg4_new (app->display, caps);
|
|
|
|
break;
|
2013-01-15 16:33:18 +00:00
|
|
|
case GST_VAAPI_CODEC_VC1:
|
2016-04-29 10:48:44 +00:00
|
|
|
app->decoder = gst_vaapi_decoder_vc1_new (app->display, caps);
|
|
|
|
break;
|
2013-01-15 16:33:18 +00:00
|
|
|
default:
|
2016-04-29 10:48:44 +00:00
|
|
|
app->decoder = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!app->decoder)
|
|
|
|
return FALSE;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
gst_vaapi_decoder_set_codec_state_changed_func (app->decoder,
|
|
|
|
handle_decoder_state_changes, app);
|
2013-01-16 12:29:06 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
g_timer_start (app->timer);
|
2013-01-16 12:53:43 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
app->decoder_thread = g_thread_try_new ("Decoder Thread", decoder_thread,
|
|
|
|
app, NULL);
|
|
|
|
if (!app->decoder_thread)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-04-29 10:48:44 +00:00
|
|
|
stop_decoder (App * app)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
g_timer_stop (app->timer);
|
2013-01-16 12:53:43 +00:00
|
|
|
|
2021-03-19 06:42:36 +00:00
|
|
|
g_atomic_int_set (&app->decoder_thread_cancel, TRUE);
|
2016-04-29 10:48:44 +00:00
|
|
|
g_thread_join (app->decoder_thread);
|
|
|
|
g_print ("Decoder thread stopped\n");
|
|
|
|
return TRUE;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 17:49:28 +00:00
|
|
|
static void
|
2016-04-29 10:48:44 +00:00
|
|
|
ensure_window_size (App * app, GstVaapiSurface * surface)
|
2013-01-15 17:49:28 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
guint width, height;
|
2013-01-15 17:49:28 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
if (gst_vaapi_window_get_fullscreen (app->window))
|
|
|
|
return;
|
2013-01-15 17:49:28 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
gst_vaapi_surface_get_size (surface, &width, &height);
|
|
|
|
if (app->surface_width == width && app->surface_height == height)
|
|
|
|
return;
|
|
|
|
app->surface_width = width;
|
|
|
|
app->surface_height = height;
|
2013-01-15 17:49:28 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
gst_vaapi_window_set_size (app->window, width, height);
|
|
|
|
gst_vaapi_window_get_size (app->window,
|
|
|
|
&app->window_width, &app->window_height);
|
2013-01-15 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2013-01-16 12:29:06 +00:00
|
|
|
static inline void
|
2016-04-29 10:48:44 +00:00
|
|
|
renderer_wait_until (App * app, GstClockTime pts)
|
2013-01-16 12:29:06 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
g_mutex_lock (&app->mutex);
|
|
|
|
do {
|
|
|
|
} while (g_cond_wait_until (&app->render_ready, &app->mutex, pts));
|
|
|
|
g_mutex_unlock (&app->mutex);
|
2013-01-16 12:29:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 16:33:18 +00:00
|
|
|
static gboolean
|
2016-04-29 10:48:44 +00:00
|
|
|
renderer_process (App * app, RenderFrame * rfp)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
GstVaapiSurface *surface;
|
|
|
|
const GstVaapiRectangle *crop_rect;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
|
|
|
#define SEND_ERROR(...) \
|
|
|
|
do { \
|
|
|
|
error = g_error_new(APP_ERROR, APP_ERROR_RENDERER, __VA_ARGS__); \
|
|
|
|
goto send_error; \
|
|
|
|
} while (0)
|
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
surface = gst_vaapi_surface_proxy_get_surface (rfp->proxy);
|
|
|
|
if (!surface)
|
|
|
|
SEND_ERROR ("failed to get decoded surface from render frame");
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
ensure_window_size (app, surface);
|
2013-01-15 17:49:28 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
crop_rect = gst_vaapi_surface_proxy_get_crop_rect (rfp->proxy);
|
2013-07-22 07:36:08 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
if (!gst_vaapi_surface_sync (surface))
|
|
|
|
SEND_ERROR ("failed to sync decoded surface");
|
2013-01-16 12:53:43 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
if (G_LIKELY (!g_benchmark))
|
|
|
|
renderer_wait_until (app, rfp->pts);
|
2013-01-16 12:29:06 +00:00
|
|
|
|
2020-03-13 20:49:15 +00:00
|
|
|
if (!gst_vaapi_window_put_surface (app->window, surface,
|
2016-04-29 10:48:44 +00:00
|
|
|
crop_rect, NULL, GST_VAAPI_PICTURE_STRUCTURE_FRAME))
|
|
|
|
SEND_ERROR ("failed to render surface %" GST_VAAPI_ID_FORMAT,
|
|
|
|
GST_VAAPI_ID_ARGS (gst_vaapi_surface_get_id (surface)));
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
app->num_frames++;
|
2013-01-16 12:53:43 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
render_frame_replace (&app->last_frame, rfp);
|
|
|
|
return TRUE;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
|
|
|
#undef SEND_ERROR
|
|
|
|
|
|
|
|
send_error:
|
2016-04-29 10:48:44 +00:00
|
|
|
app_send_error (app, error);
|
|
|
|
return FALSE;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
2016-04-29 10:48:44 +00:00
|
|
|
renderer_thread (gpointer data)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
App *const app = data;
|
|
|
|
RenderFrame *rfp;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
g_print ("Render thread started\n");
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2021-03-19 06:42:36 +00:00
|
|
|
while (!g_atomic_int_get (&app->render_thread_cancel)) {
|
2016-04-29 10:48:44 +00:00
|
|
|
rfp = g_async_queue_timeout_pop (app->decoder_queue, 1000000);
|
|
|
|
if (rfp && !renderer_process (app, rfp))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-04-29 10:48:44 +00:00
|
|
|
flush_decoder_queue (App * app)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
RenderFrame *rfp;
|
|
|
|
|
|
|
|
/* Flush pending surfaces */
|
|
|
|
do {
|
|
|
|
rfp = g_async_queue_try_pop (app->decoder_queue);
|
|
|
|
if (!rfp)
|
|
|
|
return TRUE;
|
|
|
|
} while (renderer_process (app, rfp));
|
|
|
|
return FALSE;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-04-29 10:48:44 +00:00
|
|
|
start_renderer (App * app)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
app->render_thread = g_thread_try_new ("Renderer Thread", renderer_thread,
|
|
|
|
app, NULL);
|
|
|
|
if (!app->render_thread)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-04-29 10:48:44 +00:00
|
|
|
stop_renderer (App * app)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2021-03-19 06:42:36 +00:00
|
|
|
g_atomic_int_set (&app->render_thread_cancel, TRUE);
|
2016-04-29 10:48:44 +00:00
|
|
|
g_thread_join (app->render_thread);
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
g_print ("Render thread stopped\n");
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
flush_decoder_queue (app);
|
|
|
|
render_frame_replace (&app->last_frame, NULL);
|
|
|
|
return TRUE;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-04-29 10:48:44 +00:00
|
|
|
app_free (App * app)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
if (!app)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (app->file) {
|
|
|
|
g_mapped_file_unref (app->file);
|
|
|
|
app->file = NULL;
|
|
|
|
}
|
|
|
|
g_free (app->file_name);
|
|
|
|
|
|
|
|
gst_vaapi_decoder_replace (&app->decoder, NULL);
|
|
|
|
gst_vaapi_window_replace (&app->window, NULL);
|
|
|
|
gst_vaapi_display_replace (&app->display, NULL);
|
|
|
|
|
|
|
|
if (app->decoder_queue) {
|
|
|
|
g_async_queue_unref (app->decoder_queue);
|
|
|
|
app->decoder_queue = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (app->timer) {
|
|
|
|
g_timer_destroy (app->timer);
|
|
|
|
app->timer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_cond_clear (&app->render_ready);
|
|
|
|
g_cond_clear (&app->event_cond);
|
|
|
|
g_mutex_clear (&app->mutex);
|
2023-01-24 19:16:47 +00:00
|
|
|
g_free (app);
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static App *
|
2016-04-29 10:48:44 +00:00
|
|
|
app_new (void)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
App *app;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2023-01-24 19:16:47 +00:00
|
|
|
app = g_new0 (App, 1);
|
2016-04-29 10:48:44 +00:00
|
|
|
if (!app)
|
|
|
|
return NULL;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
g_mutex_init (&app->mutex);
|
|
|
|
g_cond_init (&app->event_cond);
|
|
|
|
g_cond_init (&app->render_ready);
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
app_set_framerate (app, 60, 1);
|
|
|
|
app->window_width = 640;
|
|
|
|
app->window_height = 480;
|
2013-01-15 17:49:28 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
app->decoder_queue = g_async_queue_new_full (
|
|
|
|
(GDestroyNotify) render_frame_free);
|
|
|
|
if (!app->decoder_queue)
|
|
|
|
goto error;
|
2013-01-16 12:53:43 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
app->timer = g_timer_new ();
|
|
|
|
if (!app->timer)
|
|
|
|
goto error;
|
|
|
|
return app;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
|
|
|
error:
|
2016-04-29 10:48:44 +00:00
|
|
|
app_free (app);
|
|
|
|
return NULL;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-04-29 10:48:44 +00:00
|
|
|
app_check_events (App * app)
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
gboolean stop = FALSE;
|
|
|
|
|
|
|
|
do {
|
|
|
|
g_mutex_lock (&app->mutex);
|
|
|
|
while (app->event == APP_RUNNING)
|
|
|
|
g_cond_wait (&app->event_cond, &app->mutex);
|
|
|
|
|
|
|
|
switch (app->event) {
|
|
|
|
case APP_GOT_ERROR:
|
|
|
|
error = app->error;
|
|
|
|
app->error = NULL;
|
|
|
|
/* fall-through */
|
|
|
|
case APP_GOT_EOS:
|
|
|
|
stop = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&app->mutex);
|
|
|
|
} while (!stop);
|
|
|
|
|
|
|
|
if (!error)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
g_message ("%s error: %s", get_error_string (error->code), error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
return FALSE;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-04-29 10:48:44 +00:00
|
|
|
app_run (App * app, int argc, char *argv[])
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
if (argc < 2) {
|
|
|
|
g_message ("no bitstream file specified");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
app->file_name = g_strdup (argv[1]);
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
if (!g_file_test (app->file_name, G_FILE_TEST_IS_REGULAR)) {
|
|
|
|
g_message ("failed to find file '%s'", app->file_name);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
app->codec = identify_codec (app->file_name);
|
|
|
|
if (!app->codec) {
|
|
|
|
app->codec = identify_codec_from_string (g_codec_str);
|
2013-01-15 16:33:18 +00:00
|
|
|
if (!app->codec) {
|
2016-04-29 10:48:44 +00:00
|
|
|
g_message ("failed to identify codec for '%s'", app->file_name);
|
|
|
|
return FALSE;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
2016-04-29 10:48:44 +00:00
|
|
|
}
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
g_print ("Simple decoder (%s bitstream)\n", string_from_codec (app->codec));
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
app->display = video_output_create_display (NULL);
|
|
|
|
if (!app->display) {
|
|
|
|
g_message ("failed to create VA display");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
app->window = video_output_create_window (app->display,
|
|
|
|
app->window_width, app->window_height);
|
|
|
|
if (!app->window) {
|
|
|
|
g_message ("failed to create window");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
gst_vaapi_window_show (app->window);
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
if (!start_decoder (app)) {
|
|
|
|
g_message ("failed to start decoder thread");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
if (!start_renderer (app)) {
|
|
|
|
g_message ("failed to start renderer thread");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
app_check_events (app);
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
stop_renderer (app);
|
|
|
|
stop_decoder (app);
|
2013-01-16 12:53:43 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
g_print ("Decoded %u frames", app->num_frames);
|
|
|
|
if (g_benchmark) {
|
|
|
|
const gdouble elapsed = g_timer_elapsed (app->timer, NULL);
|
|
|
|
g_print (" in %.2f sec (%.1f fps)\n",
|
|
|
|
elapsed, (gdouble) app->num_frames / elapsed);
|
|
|
|
}
|
|
|
|
g_print ("\n");
|
|
|
|
return TRUE;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-04-29 10:48:44 +00:00
|
|
|
main (int argc, char *argv[])
|
2013-01-15 16:33:18 +00:00
|
|
|
{
|
2016-04-29 10:48:44 +00:00
|
|
|
App *app;
|
|
|
|
gint ret;
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
if (!video_output_init (&argc, argv, g_options))
|
|
|
|
g_error ("failed to initialize video output subsystem");
|
2013-01-31 10:32:24 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
app = app_new ();
|
|
|
|
if (!app)
|
|
|
|
g_error ("failed to create application context");
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
ret = !app_run (app, argc, argv);
|
2013-01-15 16:33:18 +00:00
|
|
|
|
2016-04-29 10:48:44 +00:00
|
|
|
app_free (app);
|
|
|
|
g_free (g_codec_str);
|
|
|
|
video_output_exit ();
|
|
|
|
return ret;
|
2013-01-15 16:33:18 +00:00
|
|
|
}
|