2 * gstvaapidecode.c - VA-API video decoder
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Copyright (C) 2011-2013 Intel Corporation
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1
10 * of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA
24 * SECTION:gstvaapidecode
25 * @short_description: A VA-API based video decoder
27 * vaapidecode decodes from raw bitstreams to surfaces suitable for
28 * the vaapisink element.
31 #include "gst/vaapi/sysdeps.h"
32 #include <gst/vaapi/gstvaapidisplay.h>
34 #include "gstvaapidecode.h"
35 #include "gstvaapipluginutil.h"
36 #include "gstvaapivideocontext.h"
37 #include "gstvaapivideobuffer.h"
38 #if GST_CHECK_VERSION(1,1,0)
39 #include "gstvaapivideometa_texture.h"
41 #if GST_CHECK_VERSION(1,0,0)
42 #include "gstvaapivideobufferpool.h"
43 #include "gstvaapivideomemory.h"
46 #include <gst/vaapi/gstvaapidecoder_h264.h>
47 #include <gst/vaapi/gstvaapidecoder_jpeg.h>
48 #include <gst/vaapi/gstvaapidecoder_mpeg2.h>
49 #include <gst/vaapi/gstvaapidecoder_mpeg4.h>
50 #include <gst/vaapi/gstvaapidecoder_vc1.h>
52 #define GST_PLUGIN_NAME "vaapidecode"
53 #define GST_PLUGIN_DESC "A VA-API based video decoder"
55 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapidecode);
56 #define GST_CAT_DEFAULT gst_debug_vaapidecode
58 /* Default templates */
59 #define GST_CAPS_CODEC(CODEC) CODEC "; "
61 static const char gst_vaapidecode_sink_caps_str[] =
62 GST_CAPS_CODEC("video/mpeg, mpegversion=2, systemstream=(boolean)false")
63 GST_CAPS_CODEC("video/mpeg, mpegversion=4")
64 GST_CAPS_CODEC("video/x-divx")
65 GST_CAPS_CODEC("video/x-xvid")
66 GST_CAPS_CODEC("video/x-h263")
67 GST_CAPS_CODEC("video/x-h264")
68 GST_CAPS_CODEC("video/x-wmv")
69 GST_CAPS_CODEC("image/jpeg")
72 static const char gst_vaapidecode_src_caps_str[] =
73 #if GST_CHECK_VERSION(1,1,0)
74 GST_VIDEO_CAPS_MAKE_WITH_FEATURES(
75 GST_CAPS_FEATURE_MEMORY_VAAPI_SURFACE, "{ ENCODED, NV12, I420, YV12 }") ";"
76 GST_VIDEO_CAPS_MAKE_WITH_FEATURES(
77 GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META, "RGBA");
79 GST_VAAPI_SURFACE_CAPS;
82 static GstStaticPadTemplate gst_vaapidecode_sink_factory =
83 GST_STATIC_PAD_TEMPLATE(
87 GST_STATIC_CAPS(gst_vaapidecode_sink_caps_str));
89 static GstStaticPadTemplate gst_vaapidecode_src_factory =
90 GST_STATIC_PAD_TEMPLATE(
94 GST_STATIC_CAPS(gst_vaapidecode_src_caps_str));
96 /* GstImplementsInterface interface */
97 #if !GST_CHECK_VERSION(1,0,0)
99 gst_vaapidecode_implements_interface_supported(
100 GstImplementsInterface *iface,
104 return (type == GST_TYPE_VIDEO_CONTEXT);
108 gst_vaapidecode_implements_iface_init(GstImplementsInterfaceClass *iface)
110 iface->supported = gst_vaapidecode_implements_interface_supported;
114 /* GstVideoContext interface */
115 #if !GST_CHECK_VERSION(1,1,0)
117 gst_vaapidecode_set_video_context(GstVideoContext *context, const gchar *type,
120 GstVaapiDecode *decode = GST_VAAPIDECODE (context);
121 gst_vaapi_set_display (type, value, &decode->display);
125 gst_video_context_interface_init(GstVideoContextInterface *iface)
127 iface->set_context = gst_vaapidecode_set_video_context;
130 #define GstVideoContextClass GstVideoContextInterface
133 G_DEFINE_TYPE_WITH_CODE(
136 GST_TYPE_VIDEO_DECODER,
137 #if !GST_CHECK_VERSION(1,0,0)
138 G_IMPLEMENT_INTERFACE(GST_TYPE_IMPLEMENTS_INTERFACE,
139 gst_vaapidecode_implements_iface_init);
141 #if !GST_CHECK_VERSION(1,1,0)
142 G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_CONTEXT,
143 gst_video_context_interface_init)
148 gst_vaapidecode_update_src_caps(GstVaapiDecode *decode,
149 const GstVideoCodecState *ref_state);
152 gst_vaapi_decoder_state_changed(GstVaapiDecoder *decoder,
153 const GstVideoCodecState *codec_state, gpointer user_data)
155 GstVaapiDecode * const decode = GST_VAAPIDECODE(user_data);
157 g_assert(decode->decoder == decoder);
159 gst_vaapidecode_update_src_caps(decode, codec_state);
162 static inline gboolean
163 gst_vaapidecode_update_sink_caps(GstVaapiDecode *decode, GstCaps *caps)
165 gst_caps_replace(&decode->sinkpad_caps, caps);
170 gst_vaapidecode_update_src_caps(GstVaapiDecode *decode,
171 const GstVideoCodecState *ref_state)
173 GstVideoDecoder * const vdec = GST_VIDEO_DECODER(decode);
174 GstVideoCodecState *state;
175 GstVideoInfo *vi, vis;
177 state = gst_video_decoder_set_output_state(vdec,
178 GST_VIDEO_INFO_FORMAT(&ref_state->info),
179 ref_state->info.width, ref_state->info.height,
180 (GstVideoCodecState *)ref_state);
185 if (GST_VIDEO_INFO_FORMAT(vi) == GST_VIDEO_FORMAT_ENCODED) {
186 gst_video_info_init(&vis);
187 gst_video_info_set_format(&vis, GST_VIDEO_FORMAT_NV12,
188 GST_VIDEO_INFO_WIDTH(vi), GST_VIDEO_INFO_HEIGHT(vi));
191 gst_video_codec_state_unref(state);
193 #if GST_CHECK_VERSION(1,1,0)
194 state->caps = gst_video_info_to_caps(&vis);
196 /* XXX: gst_video_info_to_caps() from GStreamer 0.10 does not
197 reconstruct suitable caps for "encoded" video formats */
198 state->caps = gst_caps_from_string(GST_VAAPI_SURFACE_CAPS_NAME);
202 gst_caps_set_simple(state->caps,
203 "type", G_TYPE_STRING, "vaapi",
204 "opengl", G_TYPE_BOOLEAN, USE_GLX,
205 "width", G_TYPE_INT, vi->width,
206 "height", G_TYPE_INT, vi->height,
207 "framerate", GST_TYPE_FRACTION, vi->fps_n, vi->fps_d,
208 "pixel-aspect-ratio", GST_TYPE_FRACTION, vi->par_n, vi->par_d,
211 gst_caps_set_interlaced(state->caps, vi);
213 gst_caps_replace(&decode->srcpad_caps, state->caps);
218 gst_vaapidecode_release(GstVaapiDecode *decode)
220 g_mutex_lock(&decode->decoder_mutex);
221 g_cond_signal(&decode->decoder_ready);
222 g_mutex_unlock(&decode->decoder_mutex);
226 gst_vaapidecode_decode_frame(GstVideoDecoder *vdec, GstVideoCodecFrame *frame)
228 GstVaapiDecode * const decode = GST_VAAPIDECODE(vdec);
229 GstVaapiDecoderStatus status;
233 if (decode->render_time_base)
234 end_time = decode->render_time_base;
236 end_time = g_get_monotonic_time();
237 end_time += GST_TIME_AS_USECONDS(decode->last_buffer_time);
238 end_time += G_TIME_SPAN_SECOND;
240 /* Decode current frame */
242 status = gst_vaapi_decoder_decode(decode->decoder, frame);
243 if (status == GST_VAAPI_DECODER_STATUS_ERROR_NO_SURFACE) {
244 gboolean was_signalled;
245 GST_VIDEO_DECODER_STREAM_UNLOCK(vdec);
246 g_mutex_lock(&decode->decoder_mutex);
247 was_signalled = g_cond_wait_until(
248 &decode->decoder_ready,
249 &decode->decoder_mutex,
252 g_mutex_unlock(&decode->decoder_mutex);
253 GST_VIDEO_DECODER_STREAM_LOCK(vdec);
256 goto error_decode_timeout;
258 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
265 error_decode_timeout:
267 GST_WARNING("decode timeout. Decoder required a VA surface but none "
268 "got available within one second");
273 GST_ERROR("decode error %d", status);
275 case GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CODEC:
276 case GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE:
277 case GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CHROMA_FORMAT:
278 ret = GST_FLOW_NOT_SUPPORTED;
284 gst_video_decoder_drop_frame(vdec, frame);
290 gst_vaapidecode_push_decoded_frame(GstVideoDecoder *vdec)
292 GstVaapiDecode * const decode = GST_VAAPIDECODE(vdec);
293 GstVaapiSurfaceProxy *proxy;
294 GstVaapiDecoderStatus status;
295 GstVideoCodecFrame *out_frame;
297 #if GST_CHECK_VERSION(1,0,0)
298 const GstVaapiRectangle *crop_rect;
299 GstVaapiVideoMeta *meta;
303 status = gst_vaapi_decoder_get_frame_with_timeout(decode->decoder,
305 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
306 return GST_VIDEO_DECODER_FLOW_NEED_DATA;
308 if (!GST_VIDEO_CODEC_FRAME_IS_DECODE_ONLY(out_frame)) {
309 proxy = gst_video_codec_frame_get_user_data(out_frame);
311 gst_vaapi_surface_proxy_set_destroy_notify(proxy,
312 (GDestroyNotify)gst_vaapidecode_release, decode);
314 #if GST_CHECK_VERSION(1,0,0)
315 ret = gst_video_decoder_allocate_output_frame(vdec, out_frame);
316 if (ret != GST_FLOW_OK)
317 goto error_create_buffer;
319 meta = gst_buffer_get_vaapi_video_meta(out_frame->output_buffer);
322 gst_vaapi_video_meta_set_surface_proxy(meta, proxy);
324 flags = gst_vaapi_surface_proxy_get_flags(proxy);
325 if (flags & GST_VAAPI_SURFACE_PROXY_FLAG_INTERLACED) {
326 guint out_flags = GST_VIDEO_BUFFER_FLAG_INTERLACED;
327 if (flags & GST_VAAPI_SURFACE_PROXY_FLAG_TFF)
328 out_flags |= GST_VIDEO_BUFFER_FLAG_TFF;
329 if (flags & GST_VAAPI_SURFACE_PROXY_FLAG_RFF)
330 out_flags |= GST_VIDEO_BUFFER_FLAG_RFF;
331 if (flags & GST_VAAPI_SURFACE_PROXY_FLAG_ONEFIELD)
332 out_flags |= GST_VIDEO_BUFFER_FLAG_ONEFIELD;
333 GST_BUFFER_FLAG_SET(out_frame->output_buffer, out_flags);
336 crop_rect = gst_vaapi_surface_proxy_get_crop_rect(proxy);
338 GstVideoCropMeta * const crop_meta =
339 gst_buffer_add_video_crop_meta(out_frame->output_buffer);
341 crop_meta->x = crop_rect->x;
342 crop_meta->y = crop_rect->y;
343 crop_meta->width = crop_rect->width;
344 crop_meta->height = crop_rect->height;
348 #if GST_CHECK_VERSION(1,1,0)
349 if (decode->has_texture_upload_meta)
350 gst_buffer_add_texture_upload_meta(out_frame->output_buffer);
353 out_frame->output_buffer =
354 gst_vaapi_video_buffer_new_with_surface_proxy(proxy);
355 if (!out_frame->output_buffer)
356 goto error_create_buffer;
360 ret = gst_video_decoder_finish_frame(vdec, out_frame);
361 if (ret != GST_FLOW_OK)
362 goto error_commit_buffer;
364 /* Estimate when this frame would no longer be needed for rendering */
365 if (GST_CLOCK_TIME_IS_VALID(out_frame->pts)) {
366 if (!decode->render_time_base)
367 decode->render_time_base = g_get_monotonic_time() -
368 GST_TIME_AS_USECONDS(out_frame->pts);
369 decode->last_buffer_time = out_frame->pts;
370 if (GST_CLOCK_TIME_IS_VALID(out_frame->duration))
371 decode->last_buffer_time += out_frame->duration;
373 decode->last_buffer_time += GST_SECOND;
376 decode->render_time_base = 0;
377 decode->last_buffer_time = 0;
380 gst_video_codec_frame_unref(out_frame);
386 const GstVaapiID surface_id =
387 gst_vaapi_surface_get_id(GST_VAAPI_SURFACE_PROXY_SURFACE(proxy));
389 GST_ERROR("video sink failed to create video buffer for proxy'ed "
390 "surface %" GST_VAAPI_ID_FORMAT,
391 GST_VAAPI_ID_ARGS(surface_id));
392 gst_video_decoder_drop_frame(vdec, out_frame);
393 gst_video_codec_frame_unref(out_frame);
396 #if GST_CHECK_VERSION(1,0,0)
399 GST_ERROR("failed to get vaapi video meta attached to video buffer");
400 gst_video_decoder_drop_frame(vdec, out_frame);
401 gst_video_codec_frame_unref(out_frame);
407 if (ret != GST_FLOW_FLUSHING)
408 GST_ERROR("video sink rejected the video buffer (error %d)", ret);
409 gst_video_codec_frame_unref(out_frame);
415 gst_vaapidecode_handle_frame(GstVideoDecoder *vdec, GstVideoCodecFrame *frame)
419 /* Make sure to release the base class stream lock so that decode
420 loop can call gst_video_decoder_finish_frame() without blocking */
421 GST_VIDEO_DECODER_STREAM_UNLOCK(vdec);
422 ret = gst_vaapidecode_decode_frame(vdec, frame);
423 GST_VIDEO_DECODER_STREAM_LOCK(vdec);
428 gst_vaapidecode_decode_loop(GstVaapiDecode *decode)
430 GstVideoDecoder * const vdec = GST_VIDEO_DECODER(decode);
433 ret = gst_vaapidecode_push_decoded_frame(vdec);
434 if (ret == GST_FLOW_OK)
437 /* If invoked from gst_vaapidecode_finish(), then return right
438 away no matter the errors, or the GstVaapiDecoder needs further
439 data to complete decoding (there no more data to feed in) */
440 if (decode->decoder_finish) {
441 g_mutex_lock(&decode->decoder_mutex);
442 g_cond_signal(&decode->decoder_finish_done);
443 g_mutex_unlock(&decode->decoder_mutex);
447 /* Suspend the task if an error occurred */
448 if (ret != GST_VIDEO_DECODER_FLOW_NEED_DATA)
449 gst_pad_pause_task(decode->srcpad);
453 gst_vaapidecode_finish(GstVideoDecoder *vdec)
455 GstVaapiDecode * const decode = GST_VAAPIDECODE(vdec);
456 GstVaapiDecoderStatus status;
458 /* If there is something in GstVideoDecoder's output adapter, then
459 submit the frame for decoding */
460 if (decode->current_frame_size)
461 gst_video_decoder_have_frame(vdec);
463 status = gst_vaapi_decoder_flush(decode->decoder);
464 if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
467 /* Make sure the decode loop function has a chance to return, thus
468 possibly unlocking gst_video_decoder_finish_frame() */
469 GST_VIDEO_DECODER_STREAM_UNLOCK(vdec);
470 g_mutex_lock(&decode->decoder_mutex);
471 decode->decoder_finish = TRUE;
472 g_cond_wait(&decode->decoder_finish_done, &decode->decoder_mutex);
473 g_mutex_unlock(&decode->decoder_mutex);
474 gst_pad_stop_task(decode->srcpad);
475 GST_VIDEO_DECODER_STREAM_LOCK(vdec);
481 GST_ERROR("failed to flush decoder (status %d)", status);
486 #if GST_CHECK_VERSION(1,0,0)
488 gst_vaapidecode_decide_allocation(GstVideoDecoder *vdec, GstQuery *query)
490 GstVaapiDecode * const decode = GST_VAAPIDECODE(vdec);
491 GstCaps *caps = NULL;
493 GstStructure *config;
495 guint size, min, max;
496 gboolean need_pool, update_pool;
498 gst_query_parse_allocation(query, &caps, &need_pool);
503 gst_video_info_init(&vi);
504 gst_video_info_from_caps(&vi, caps);
505 if (GST_VIDEO_INFO_FORMAT(&vi) == GST_VIDEO_FORMAT_ENCODED)
506 gst_video_info_set_format(&vi, GST_VIDEO_FORMAT_NV12,
507 GST_VIDEO_INFO_WIDTH(&vi), GST_VIDEO_INFO_HEIGHT(&vi));
509 g_return_val_if_fail(decode->display != NULL, FALSE);
511 if (gst_query_get_n_allocation_pools(query) > 0) {
512 gst_query_parse_nth_allocation_pool(query, 0, &pool, &size, &min, &max);
513 size = MAX(size, vi.size);
523 if (!pool || !gst_buffer_pool_has_option(pool,
524 GST_BUFFER_POOL_OPTION_VAAPI_VIDEO_META)) {
525 GST_INFO("no pool or doesn't support GstVaapiVideoMeta, "
527 pool = gst_vaapi_video_buffer_pool_new(decode->display);
529 goto error_create_pool;
531 config = gst_buffer_pool_get_config(pool);
532 gst_buffer_pool_config_set_params(config, caps, size, min, max);
533 gst_buffer_pool_config_add_option(config,
534 GST_BUFFER_POOL_OPTION_VAAPI_VIDEO_META);
535 gst_buffer_pool_set_config(pool, config);
538 decode->has_texture_upload_meta = FALSE;
539 if (gst_query_find_allocation_meta(query, GST_VIDEO_META_API_TYPE, NULL)) {
540 config = gst_buffer_pool_get_config(pool);
541 gst_buffer_pool_config_add_option(config,
542 GST_BUFFER_POOL_OPTION_VIDEO_META);
543 gst_buffer_pool_set_config(pool, config);
544 #if GST_CHECK_VERSION(1,1,0)
545 decode->has_texture_upload_meta = gst_query_find_allocation_meta(query,
546 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, NULL);
551 gst_query_set_nth_allocation_pool(query, 0, pool, size, min, max);
553 gst_query_add_allocation_pool(query, pool, size, min, max);
555 gst_object_unref(pool);
561 GST_ERROR("no caps specified");
566 GST_ERROR("failed to create buffer pool");
572 #if GST_CHECK_VERSION(1,1,0)
574 gst_vaapidecode_set_context(GstElement *element, GstContext *context)
576 GstVaapiDecode * const decode = GST_VAAPIDECODE(element);
577 GstVaapiDisplay *display = NULL;
579 if (gst_vaapi_video_context_get_display(context, &display)) {
580 GST_INFO_OBJECT(element, "set display %p", display);
581 gst_vaapi_display_replace(&decode->display, display);
586 static inline gboolean
587 gst_vaapidecode_ensure_display(GstVaapiDecode *decode)
589 return gst_vaapi_ensure_display(decode, GST_VAAPI_DISPLAY_TYPE_ANY,
594 gst_vaapi_codec_from_caps(GstCaps *caps)
596 return gst_vaapi_profile_get_codec(gst_vaapi_profile_from_caps(caps));
600 gst_vaapidecode_create(GstVaapiDecode *decode, GstCaps *caps)
602 GstVaapiDisplay *dpy;
604 if (!gst_vaapidecode_ensure_display(decode))
606 dpy = decode->display;
608 switch (gst_vaapi_codec_from_caps(caps)) {
609 case GST_VAAPI_CODEC_MPEG2:
610 decode->decoder = gst_vaapi_decoder_mpeg2_new(dpy, caps);
612 case GST_VAAPI_CODEC_MPEG4:
613 case GST_VAAPI_CODEC_H263:
614 decode->decoder = gst_vaapi_decoder_mpeg4_new(dpy, caps);
616 case GST_VAAPI_CODEC_H264:
617 decode->decoder = gst_vaapi_decoder_h264_new(dpy, caps);
619 case GST_VAAPI_CODEC_WMV3:
620 case GST_VAAPI_CODEC_VC1:
621 decode->decoder = gst_vaapi_decoder_vc1_new(dpy, caps);
624 case GST_VAAPI_CODEC_JPEG:
625 decode->decoder = gst_vaapi_decoder_jpeg_new(dpy, caps);
629 decode->decoder = NULL;
632 if (!decode->decoder)
635 gst_vaapi_decoder_set_codec_state_changed_func(decode->decoder,
636 gst_vaapi_decoder_state_changed, decode);
638 decode->decoder_caps = gst_caps_ref(caps);
639 return gst_pad_start_task(decode->srcpad,
640 (GstTaskFunction)gst_vaapidecode_decode_loop, decode, NULL);
644 gst_vaapidecode_destroy(GstVaapiDecode *decode)
646 gst_pad_stop_task(decode->srcpad);
647 gst_vaapi_decoder_replace(&decode->decoder, NULL);
648 gst_caps_replace(&decode->decoder_caps, NULL);
649 gst_vaapidecode_release(decode);
653 gst_vaapidecode_reset_full(GstVaapiDecode *decode, GstCaps *caps, gboolean hard)
657 decode->has_texture_upload_meta = FALSE;
659 /* Reset timers if hard reset was requested (e.g. seek) */
661 decode->render_time_base = 0;
662 decode->last_buffer_time = 0;
665 /* Only reset decoder if codec type changed */
666 else if (decode->decoder && decode->decoder_caps) {
667 if (gst_caps_is_always_compatible(caps, decode->decoder_caps))
669 codec = gst_vaapi_codec_from_caps(caps);
670 if (codec == gst_vaapi_decoder_get_codec(decode->decoder))
674 /* Reset tracked frame size */
675 decode->current_frame_size = 0;
677 gst_vaapidecode_destroy(decode);
678 return gst_vaapidecode_create(decode, caps);
682 gst_vaapidecode_finalize(GObject *object)
684 GstVaapiDecode * const decode = GST_VAAPIDECODE(object);
686 gst_caps_replace(&decode->sinkpad_caps, NULL);
687 gst_caps_replace(&decode->srcpad_caps, NULL);
688 gst_caps_replace(&decode->allowed_caps, NULL);
690 gst_vaapi_display_replace(&decode->display, NULL);
692 g_cond_clear(&decode->decoder_finish_done);
693 g_cond_clear(&decode->decoder_ready);
694 g_mutex_clear(&decode->decoder_mutex);
696 G_OBJECT_CLASS(gst_vaapidecode_parent_class)->finalize(object);
700 gst_vaapidecode_open(GstVideoDecoder *vdec)
702 GstVaapiDecode * const decode = GST_VAAPIDECODE(vdec);
703 GstVaapiDisplay * const old_display = decode->display;
706 /* Let GstVideoContext ask for a proper display to its neighbours */
707 /* Note: steal old display that may be allocated from get_caps()
708 so that to retain a reference to it, thus avoiding extra
709 initialization steps if we turn out to simply re-use the
710 existing (cached) VA display */
711 decode->display = NULL;
712 success = gst_vaapidecode_ensure_display(decode);
714 gst_vaapi_display_unref(old_display);
719 gst_vaapidecode_close(GstVideoDecoder *vdec)
721 GstVaapiDecode * const decode = GST_VAAPIDECODE(vdec);
723 gst_vaapidecode_destroy(decode);
724 gst_vaapi_display_replace(&decode->display, NULL);
729 gst_vaapidecode_reset(GstVideoDecoder *vdec, gboolean hard)
731 GstVaapiDecode * const decode = GST_VAAPIDECODE(vdec);
733 return gst_vaapidecode_reset_full(decode, decode->sinkpad_caps, hard);
737 gst_vaapidecode_set_format(GstVideoDecoder *vdec, GstVideoCodecState *state)
739 GstVaapiDecode * const decode = GST_VAAPIDECODE(vdec);
741 if (!gst_vaapidecode_update_sink_caps(decode, state->caps))
743 if (!gst_vaapidecode_update_src_caps(decode, state))
745 if (!gst_vaapidecode_reset_full(decode, decode->sinkpad_caps, FALSE))
751 gst_vaapidecode_parse(GstVideoDecoder *vdec,
752 GstVideoCodecFrame *frame, GstAdapter *adapter, gboolean at_eos)
754 GstVaapiDecode * const decode = GST_VAAPIDECODE(vdec);
755 GstVaapiDecoderStatus status;
760 status = gst_vaapi_decoder_parse(decode->decoder, frame,
761 adapter, at_eos, &got_unit_size, &got_frame);
764 case GST_VAAPI_DECODER_STATUS_SUCCESS:
765 if (got_unit_size > 0) {
766 gst_video_decoder_add_to_frame(vdec, got_unit_size);
767 decode->current_frame_size += got_unit_size;
770 ret = gst_video_decoder_have_frame(vdec);
771 decode->current_frame_size = 0;
776 case GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA:
777 ret = GST_VIDEO_DECODER_FLOW_NEED_DATA;
779 case GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CODEC:
780 case GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE:
781 case GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CHROMA_FORMAT:
782 GST_WARNING("parse error %d", status);
783 ret = GST_FLOW_NOT_SUPPORTED;
784 decode->current_frame_size = 0;
787 GST_ERROR("parse error %d", status);
789 decode->current_frame_size = 0;
796 gst_vaapidecode_class_init(GstVaapiDecodeClass *klass)
798 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
799 GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
800 GstVideoDecoderClass * const vdec_class = GST_VIDEO_DECODER_CLASS(klass);
801 GstPadTemplate *pad_template;
803 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapidecode,
804 GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
806 object_class->finalize = gst_vaapidecode_finalize;
808 vdec_class->open = GST_DEBUG_FUNCPTR(gst_vaapidecode_open);
809 vdec_class->close = GST_DEBUG_FUNCPTR(gst_vaapidecode_close);
810 vdec_class->set_format = GST_DEBUG_FUNCPTR(gst_vaapidecode_set_format);
811 vdec_class->reset = GST_DEBUG_FUNCPTR(gst_vaapidecode_reset);
812 vdec_class->parse = GST_DEBUG_FUNCPTR(gst_vaapidecode_parse);
813 vdec_class->handle_frame = GST_DEBUG_FUNCPTR(gst_vaapidecode_handle_frame);
814 vdec_class->finish = GST_DEBUG_FUNCPTR(gst_vaapidecode_finish);
816 #if GST_CHECK_VERSION(1,0,0)
817 vdec_class->decide_allocation =
818 GST_DEBUG_FUNCPTR(gst_vaapidecode_decide_allocation);
821 #if GST_CHECK_VERSION(1,1,0)
822 element_class->set_context = GST_DEBUG_FUNCPTR(gst_vaapidecode_set_context);
825 gst_element_class_set_static_metadata(element_class,
827 "Codec/Decoder/Video",
829 "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");
832 pad_template = gst_static_pad_template_get(&gst_vaapidecode_sink_factory);
833 gst_element_class_add_pad_template(element_class, pad_template);
836 pad_template = gst_static_pad_template_get(&gst_vaapidecode_src_factory);
837 gst_element_class_add_pad_template(element_class, pad_template);
841 gst_vaapidecode_ensure_allowed_caps(GstVaapiDecode *decode)
843 GstCaps *decode_caps;
844 guint i, n_decode_caps;
846 if (decode->allowed_caps)
849 if (!gst_vaapidecode_ensure_display(decode))
850 goto error_no_display;
852 decode_caps = gst_vaapi_display_get_decode_caps(decode->display);
854 goto error_no_decode_caps;
855 n_decode_caps = gst_caps_get_size(decode_caps);
857 decode->allowed_caps = gst_caps_new_empty();
858 if (!decode->allowed_caps)
859 goto error_no_memory;
861 for (i = 0; i < n_decode_caps; i++) {
862 GstStructure *structure;
863 structure = gst_caps_get_structure(decode_caps, i);
866 structure = gst_structure_copy(structure);
869 gst_structure_remove_field(structure, "profile");
870 decode->allowed_caps =
871 gst_caps_merge_structure(decode->allowed_caps, structure);
874 gst_caps_unref(decode_caps);
880 GST_ERROR("failed to retrieve VA display");
883 error_no_decode_caps:
885 GST_ERROR("failed to retrieve VA decode caps");
890 GST_ERROR("failed to allocate allowed-caps set");
891 gst_caps_unref(decode_caps);
897 gst_vaapidecode_get_caps(GstPad *pad)
899 GstVaapiDecode * const decode = GST_VAAPIDECODE(GST_OBJECT_PARENT(pad));
901 if (!gst_vaapidecode_ensure_allowed_caps(decode))
902 return gst_caps_new_empty();
904 return gst_caps_ref(decode->allowed_caps);
908 gst_vaapidecode_query(GST_PAD_QUERY_FUNCTION_ARGS)
910 GstVaapiDecode * const decode =
911 GST_VAAPIDECODE(gst_pad_get_parent_element(pad));
914 GST_INFO_OBJECT(decode, "query type %s", GST_QUERY_TYPE_NAME(query));
916 if (gst_vaapi_reply_to_query(query, decode->display)) {
917 GST_DEBUG("sharing display %p", decode->display);
920 else if (GST_PAD_IS_SINK(pad)) {
921 switch (GST_QUERY_TYPE(query)) {
922 #if GST_CHECK_VERSION(1,0,0)
923 case GST_QUERY_CAPS: {
924 GstCaps * const caps = gst_vaapidecode_get_caps(pad);
925 gst_query_set_caps_result(query, caps);
926 gst_caps_unref(caps);
932 res = GST_PAD_QUERY_FUNCTION_CALL(decode->sinkpad_query,
933 decode->sinkpad, parent, query);
938 res = GST_PAD_QUERY_FUNCTION_CALL(decode->srcpad_query,
939 decode->srcpad, parent, query);
941 gst_object_unref(decode);
946 gst_vaapidecode_init(GstVaapiDecode *decode)
948 GstVideoDecoder * const vdec = GST_VIDEO_DECODER(decode);
950 decode->display = NULL;
951 decode->decoder = NULL;
952 decode->decoder_caps = NULL;
953 decode->allowed_caps = NULL;
954 decode->render_time_base = 0;
955 decode->last_buffer_time = 0;
957 g_mutex_init(&decode->decoder_mutex);
958 g_cond_init(&decode->decoder_ready);
959 g_cond_init(&decode->decoder_finish_done);
961 gst_video_decoder_set_packetized(vdec, FALSE);
963 /* Pad through which data comes in to the element */
964 decode->sinkpad = GST_VIDEO_DECODER_SINK_PAD(vdec);
965 decode->sinkpad_query = GST_PAD_QUERYFUNC(decode->sinkpad);
966 gst_pad_set_query_function(decode->sinkpad, gst_vaapidecode_query);
967 #if !GST_CHECK_VERSION(1,0,0)
968 gst_pad_set_getcaps_function(decode->sinkpad, gst_vaapidecode_get_caps);
971 /* Pad through which data goes out of the element */
972 decode->srcpad = GST_VIDEO_DECODER_SRC_PAD(vdec);
973 decode->srcpad_query = GST_PAD_QUERYFUNC(decode->srcpad);
974 gst_pad_set_query_function(decode->srcpad, gst_vaapidecode_query);