1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
|
/*
* simple-decoder.c - Simple Decoder Application
*
* Copyright (C) 2013 Intel Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/*
* 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.
*/
#include "gst/vaapi/sysdeps.h"
#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;
static gboolean g_benchmark;
static GOptionEntry g_options[] = {
{ "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, }
};
typedef enum {
APP_RUNNING,
APP_GOT_EOS,
APP_GOT_ERROR,
} AppEvent;
typedef enum {
APP_ERROR_NONE,
APP_ERROR_DECODER,
APP_ERROR_RENDERER,
} AppError;
typedef struct {
GstVaapiSurfaceProxy *proxy;
GstClockTime pts;
GstClockTime duration;
} RenderFrame;
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;
volatile gboolean decoder_thread_cancel;
GCond decoder_ready;
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;
volatile gboolean render_thread_cancel;
GCond render_ready;
RenderFrame *last_frame;
GError *error;
AppEvent event;
GCond event_cond;
GTimer *timer;
guint32 num_frames;
} App;
static inline RenderFrame *
render_frame_new(void)
{
return g_slice_new(RenderFrame);
}
static void
render_frame_free(RenderFrame *rfp)
{
if (G_UNLIKELY(!rfp))
return;
gst_vaapi_surface_proxy_replace(&rfp->proxy, NULL);
g_slice_free(RenderFrame, rfp);
}
static inline void
render_frame_replace(RenderFrame **rfp_ptr, RenderFrame *new_rfp)
{
if (*rfp_ptr)
render_frame_free(*rfp_ptr);
*rfp_ptr = new_rfp;
}
#define APP_ERROR app_error_quark()
static GQuark
app_error_quark(void)
{
static gsize g_quark;
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;
}
static void
app_send_error(App *app, GError *error)
{
g_mutex_lock(&app->mutex);
app->error = error;
app->event = APP_GOT_ERROR;
g_cond_signal(&app->event_cond);
g_mutex_unlock(&app->mutex);
}
static void
app_send_eos(App *app)
{
g_mutex_lock(&app->mutex);
app->event = APP_GOT_EOS;
g_cond_signal(&app->event_cond);
g_mutex_unlock(&app->mutex);
}
static const gchar *
get_decoder_status_string(GstVaapiDecoderStatus status)
{
const gchar *str;
#define DEFINE_STATUS(status, status_string) \
case GST_VAAPI_DECODER_STATUS_##status: \
str = status_string; \
break
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");
default:
str = "<unknown>";
break;
}
#undef DEFINE_STATUS
return str;
}
static const gchar *
get_error_string(AppError error)
{
const gchar *str;
#define DEFINE_ERROR(error, error_string) \
case APP_ERROR_##error: \
str = error_string; \
break
switch (error) {
DEFINE_ERROR(NONE, "<none>");
DEFINE_ERROR(DECODER, "decoder");
DEFINE_ERROR(RENDERER, "renderer");
default:
str = "unknown";
break;
}
#undef DEFINE_ERROR
return str;
}
static void
decoder_release(App *app)
{
g_mutex_lock(&app->mutex);
g_cond_signal(&app->decoder_ready);
g_mutex_unlock(&app->mutex);
}
static gpointer
decoder_thread(gpointer data)
{
App * const app = data;
GError *error = NULL;
GstVaapiDecoderStatus status;
GstVaapiSurfaceProxy *proxy;
RenderFrame *rfp;
GstBuffer *buffer;
GstClockTime pts;
gboolean got_surface, got_eos = FALSE;
gint64 end_time;
guint ofs;
g_print("Decoder thread started\n");
#define SEND_ERROR(...) \
do { \
error = g_error_new(APP_ERROR, APP_ERROR_DECODER, __VA_ARGS__); \
goto send_error; \
} while (0)
pts = g_get_monotonic_time();
ofs = 0;
while (!app->decoder_thread_cancel) {
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;
}
if (!gst_vaapi_decoder_put_buffer(app->decoder, buffer))
SEND_ERROR("failed to push buffer to decoder");
gst_buffer_replace(&buffer, NULL);
get_surface:
status = gst_vaapi_decoder_get_surface(app->decoder, &proxy);
switch (status) {
case GST_VAAPI_DECODER_STATUS_SUCCESS:
gst_vaapi_surface_proxy_set_user_data(proxy,
app, (GDestroyNotify)decoder_release);
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;
case GST_VAAPI_DECODER_STATUS_ERROR_NO_SURFACE:
end_time = g_get_monotonic_time() + G_TIME_SPAN_SECOND;
g_mutex_lock(&app->mutex);
got_surface = g_cond_wait_until(&app->decoder_ready, &app->mutex,
end_time);
g_mutex_unlock(&app->mutex);
if (got_surface)
goto get_surface;
SEND_ERROR("failed to acquire a surface within one second");
break;
default:
SEND_ERROR("%s", get_decoder_status_string(status));
break;
}
}
return NULL;
#undef SEND_ERROR
send_eos:
app_send_eos(app);
return NULL;
send_error:
app_send_error(app, error);
return NULL;
}
static void
app_set_framerate(App *app, guint fps_n, guint fps_d)
{
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);
}
static void
handle_decoder_caps(GObject *obj, GParamSpec *pspec, void *user_data)
{
App * const app = user_data;
GstVideoCodecState *codec_state;
g_assert(app->decoder == GST_VAAPI_DECODER(obj));
codec_state = gst_vaapi_decoder_get_codec_state(app->decoder);
app_set_framerate(app, codec_state->info.fps_n, codec_state->info.fps_d);
gst_video_codec_state_unref(codec_state);
}
static gboolean
start_decoder(App *app)
{
GstCaps *caps;
app->file = g_mapped_file_new(app->file_name, FALSE, NULL);
if (!app->file)
return FALSE;
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;
caps = caps_from_codec(app->codec);
switch (app->codec) {
case GST_VAAPI_CODEC_H264:
app->decoder = gst_vaapi_decoder_h264_new(app->display, caps);
break;
#if USE_JPEG_DECODER
case GST_VAAPI_CODEC_JPEG:
app->decoder = gst_vaapi_decoder_jpeg_new(app->display, caps);
break;
#endif
case GST_VAAPI_CODEC_MPEG2:
app->decoder = gst_vaapi_decoder_mpeg2_new(app->display, caps);
break;
case GST_VAAPI_CODEC_MPEG4:
app->decoder = gst_vaapi_decoder_mpeg4_new(app->display, caps);
break;
case GST_VAAPI_CODEC_VC1:
app->decoder = gst_vaapi_decoder_vc1_new(app->display, caps);
break;
default:
app->decoder = NULL;
break;
}
if (!app->decoder)
return FALSE;
g_signal_connect(G_OBJECT(app->decoder), "notify::caps",
G_CALLBACK(handle_decoder_caps), app);
g_timer_start(app->timer);
app->decoder_thread = g_thread_create(decoder_thread, app, TRUE, NULL);
if (!app->decoder_thread)
return FALSE;
return TRUE;
}
static gboolean
stop_decoder(App *app)
{
g_timer_stop(app->timer);
app->decoder_thread_cancel = TRUE;
g_thread_join(app->decoder_thread);
g_print("Decoder thread stopped\n");
return TRUE;
}
static void
ensure_window_size(App *app, GstVaapiSurface *surface)
{
guint width, height;
if (gst_vaapi_window_get_fullscreen(app->window))
return;
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;
gst_vaapi_window_set_size(app->window, width, height);
gst_vaapi_window_get_size(app->window,
&app->window_width, &app->window_height);
}
static inline void
renderer_wait_until(App *app, GstClockTime pts)
{
g_mutex_lock(&app->mutex);
do {
} while (g_cond_wait_until(&app->render_ready, &app->mutex, pts));
g_mutex_unlock(&app->mutex);
}
static gboolean
renderer_process(App *app, RenderFrame *rfp)
{
GError *error = NULL;
GstVaapiSurface *surface;
#define SEND_ERROR(...) \
do { \
error = g_error_new(APP_ERROR, APP_ERROR_RENDERER, __VA_ARGS__); \
goto send_error; \
} while (0)
surface = gst_vaapi_surface_proxy_get_surface(rfp->proxy);
if (!surface)
SEND_ERROR("failed to get decoded surface from render frame");
ensure_window_size(app, surface);
if (!gst_vaapi_surface_sync(surface))
SEND_ERROR("failed to sync decoded surface");
if (G_LIKELY(!g_benchmark))
renderer_wait_until(app, rfp->pts);
if (!gst_vaapi_window_put_surface(app->window, surface, NULL, 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)));
app->num_frames++;
render_frame_replace(&app->last_frame, rfp);
return TRUE;
#undef SEND_ERROR
send_error:
app_send_error(app, error);
return FALSE;
}
static gpointer
renderer_thread(gpointer data)
{
App * const app = data;
RenderFrame *rfp;
g_print("Render thread started\n");
while (!app->render_thread_cancel) {
rfp = g_async_queue_timeout_pop(app->decoder_queue, 1000000);
if (rfp && !renderer_process(app, rfp))
break;
}
return NULL;
}
static gboolean
flush_decoder_queue(App *app)
{
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;
}
static gboolean
start_renderer(App *app)
{
app->render_thread = g_thread_create(renderer_thread, app, TRUE, NULL);
if (!app->render_thread)
return FALSE;
return TRUE;
}
static gboolean
stop_renderer(App *app)
{
app->render_thread_cancel = TRUE;
g_thread_join(app->render_thread);
g_print("Render thread stopped\n");
flush_decoder_queue(app);
render_frame_replace(&app->last_frame, NULL);
return TRUE;
}
static void
app_free(App *app)
{
if (!app)
return;
if (app->file) {
g_mapped_file_unref(app->file);
app->file = NULL;
}
g_free(app->file_name);
g_clear_object(&app->decoder);
g_clear_object(&app->window);
g_clear_object(&app->display);
if (app->decoder_queue) {
g_async_queue_unref(app->decoder_queue);
app->decoder_queue = NULL;
}
g_cond_clear(&app->decoder_ready);
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);
g_slice_free(App, app);
}
static App *
app_new(void)
{
App *app;
app = g_slice_new0(App);
if (!app)
return NULL;
g_mutex_init(&app->mutex);
g_cond_init(&app->event_cond);
g_cond_init(&app->decoder_ready);
g_cond_init(&app->render_ready);
app_set_framerate(app, 60, 1);
app->window_width = 640;
app->window_height = 480;
app->decoder_queue = g_async_queue_new_full(
(GDestroyNotify)render_frame_free);
if (!app->decoder_queue)
goto error;
app->timer = g_timer_new();
if (!app->timer)
goto error;
return app;
error:
app_free(app);
return NULL;
}
static gboolean
app_check_events(App *app)
{
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;
}
static gboolean
app_run(App *app, int argc, char *argv[])
{
if (argc < 2) {
g_message("no bitstream file specified");
return FALSE;
}
app->file_name = g_strdup(argv[1]);
if (!g_file_test(app->file_name, G_FILE_TEST_IS_REGULAR)) {
g_message("failed to find file '%s'", app->file_name);
return FALSE;
}
app->codec = identify_codec(app->file_name);
if (!app->codec) {
app->codec = identify_codec_from_string(g_codec_str);
if (!app->codec) {
g_message("failed to identify codec for '%s'", app->file_name);
return FALSE;
}
}
g_print("Simple decoder (%s bitstream)\n", string_from_codec(app->codec));
app->display = video_output_create_display(NULL);
if (!app->display) {
g_message("failed to create VA display");
return FALSE;
}
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;
}
gst_vaapi_window_show(app->window);
if (!start_decoder(app)) {
g_message("failed to start decoder thread");
return FALSE;
}
if (!start_renderer(app)) {
g_message("failed to start renderer thread");
return FALSE;
}
app_check_events(app);
stop_renderer(app);
stop_decoder(app);
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;
}
int
main(int argc, char *argv[])
{
App *app;
gint ret;
if (!video_output_init(&argc, argv, g_options))
g_error("failed to initialize video output subsystem");
app = app_new();
if (!app)
g_error("failed to create application context");
ret = !app_run(app, argc, argv);
app_free(app);
g_free(g_codec_str);
video_output_exit();
return ret;
}
|