1 /*****************************************************************************
2 * gstdecode.c: Decoder module making use of gstreamer
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
7 * Author: Vikram Fugro <vikram.fugro@gmail.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
36 #include <gst/video/video.h>
37 #include <gst/video/gstvideometa.h>
38 #include <gst/app/gstappsrc.h>
39 #include <gst/gstatomicqueue.h>
43 GstElement
*p_decoder
;
44 GstElement
*p_decode_src
;
45 GstElement
*p_decode_in
;
46 GstElement
*p_decode_out
;
51 GstAtomicQueue
*p_que
;
62 /*****************************************************************************
64 *****************************************************************************/
65 static int OpenDecoder( vlc_object_t
* );
66 static void CloseDecoder( vlc_object_t
* );
67 static picture_t
*DecodeBlock( decoder_t
*, block_t
** );
69 #define MODULE_DESCRIPTION N_( "Uses GStreamer framework's plugins " \
70 "to decode the media codecs" )
72 #define USEDECODEBIN_TEXT N_( "Use DecodeBin" )
73 #define USEDECODEBIN_LONGTEXT N_( \
74 "DecodeBin is a container element, that can add and " \
75 "manage multiple elements. Apart from adding the decoders, " \
76 "decodebin also adds elementary stream parsers which can provide " \
77 "more info such as codec profile, level and other attributes, " \
78 "in the form of GstCaps (Stream Capabilities) to decoder." )
81 set_shortname( "GstDecode" )
82 add_shortcut( "gstdecode" )
83 set_category( CAT_INPUT
)
84 set_subcategory( SUBCAT_INPUT_VCODEC
)
85 /* decoder main module */
86 set_description( N_( "GStreamer Based Decoder" ) )
87 set_help( MODULE_DESCRIPTION
)
88 set_capability( "decoder", 50 )
89 set_section( N_( "Decoding" ) , NULL
)
90 set_callbacks( OpenDecoder
, CloseDecoder
)
91 add_bool( "use-decodebin", false, USEDECODEBIN_TEXT
,
92 USEDECODEBIN_LONGTEXT
, false )
95 /* gst_init( ) is not thread-safe, hence a thread-safe wrapper */
96 static void vlc_gst_init( void )
98 static vlc_mutex_t init_lock
= VLC_STATIC_MUTEX
;
100 vlc_mutex_lock( &init_lock
);
101 gst_init( NULL
, NULL
);
102 vlc_mutex_unlock( &init_lock
);
105 static GstStructure
* vlc_to_gst_fmt( const es_format_t
*p_fmt
)
107 const video_format_t
*p_vfmt
= &p_fmt
->video
;
108 GstStructure
*p_str
= NULL
;
110 switch( p_fmt
->i_codec
){
112 p_str
= gst_structure_new_empty( "video/x-h264" );
113 gst_structure_set( p_str
, "alignment", G_TYPE_STRING
, "au", NULL
);
116 p_str
= gst_structure_new_empty( "video/mpeg" );
117 gst_structure_set( p_str
, "mpegversion", G_TYPE_INT
, 4,
118 "systemstream", G_TYPE_BOOLEAN
, FALSE
, NULL
);
121 p_str
= gst_structure_new_empty( "video/x-vp8" );
124 p_str
= gst_structure_new_empty( "video/mpeg" );
125 gst_structure_set( p_str
, "mpegversion", G_TYPE_INT
, 2,
126 "systemstream", G_TYPE_BOOLEAN
, FALSE
, NULL
);
129 p_str
= gst_structure_new_empty( "video/x-flash-video" );
130 gst_structure_set( p_str
, "flvversion", G_TYPE_INT
, 1, NULL
);
133 p_str
= gst_structure_new_empty( "video/x-wmv" );
134 gst_structure_set( p_str
, "wmvversion", G_TYPE_INT
, 1,
135 "format", G_TYPE_STRING
, "WMV1", NULL
);
138 p_str
= gst_structure_new_empty( "video/x-wmv" );
139 gst_structure_set( p_str
, "wmvversion", G_TYPE_INT
, 2,
140 "format", G_TYPE_STRING
, "WMV2", NULL
);
143 p_str
= gst_structure_new_empty( "video/x-wmv" );
144 gst_structure_set( p_str
, "wmvversion", G_TYPE_INT
, 3,
145 "format", G_TYPE_STRING
, "WMV3", NULL
);
148 p_str
= gst_structure_new_empty( "video/x-wmv" );
149 gst_structure_set( p_str
, "wmvversion", G_TYPE_INT
, 3,
150 "format", G_TYPE_STRING
, "WVC1", NULL
);
153 /* unsupported codec */
157 if( p_vfmt
->i_width
&& p_vfmt
->i_height
)
158 gst_structure_set( p_str
,
159 "width", G_TYPE_INT
, p_vfmt
->i_width
,
160 "height", G_TYPE_INT
, p_vfmt
->i_height
, NULL
);
162 if( p_vfmt
->i_frame_rate
&& p_vfmt
->i_frame_rate_base
)
163 gst_structure_set( p_str
, "framerate", GST_TYPE_FRACTION
,
164 p_vfmt
->i_frame_rate
,
165 p_vfmt
->i_frame_rate_base
, NULL
);
167 if( p_vfmt
->i_sar_num
&& p_vfmt
->i_sar_den
)
168 gst_structure_set( p_str
, "pixel-aspect-ratio", GST_TYPE_FRACTION
,
170 p_vfmt
->i_sar_den
, NULL
);
176 p_buf
= gst_buffer_new_wrapped_full( GST_MEMORY_FLAG_READONLY
,
177 p_fmt
->p_extra
, p_fmt
->i_extra
, 0,
178 p_fmt
->i_extra
, NULL
, NULL
);
181 gst_structure_free( p_str
);
185 gst_structure_set( p_str
, "codec_data", GST_TYPE_BUFFER
, p_buf
, NULL
);
186 gst_buffer_unref( p_buf
);
192 /* Emitted by appsrc when serving a seek request.
193 * Seek over here is only used for flushing the buffers.
194 * Returns TRUE always, as the 'real' seek will be
195 * done by VLC framework */
196 static gboolean
seek_data_cb( GstAppSrc
*p_src
, guint64 l_offset
,
200 decoder_t
*p_dec
= p_data
;
201 msg_Dbg( p_dec
, "appsrc seeking to %"G_GUINT64_FORMAT
, l_offset
);
205 /* Emitted by decodebin when there are no more
206 * outputs.This signal is not really necessary
207 * to be connected. It is connected here for sanity
208 * check only, just in-case something unexpected
209 * happens inside decodebin in finding the appropriate
210 * decoder, and it fails to emit PAD_ADDED signal */
211 static void no_more_pads_cb( GstElement
*p_ele
, gpointer p_data
)
214 decoder_t
*p_dec
= p_data
;
215 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
218 msg_Dbg( p_dec
, "no more pads" );
220 p_pad
= gst_element_get_static_pad( p_sys
->p_decode_out
,
222 if( !gst_pad_is_linked( p_pad
) )
224 msg_Err( p_dec
, "failed to link decode out pad" );
225 GST_ELEMENT_ERROR( p_sys
->p_decoder
, STREAM
, FAILED
,
226 ( "vlc stream error" ), NULL
);
229 gst_object_unref( p_pad
);
232 /* Sets the video output format */
233 static bool set_vout_format( GstStructure
* p_str
,
234 const es_format_t
*restrict p_infmt
, es_format_t
*restrict p_outfmt
)
236 video_format_t
*p_voutfmt
= &p_outfmt
->video
;
237 const video_format_t
*p_vinfmt
= &p_infmt
->video
;
240 /* We are interested in system memory raw buffers for now,
241 * but support for opaque data formats can also be added.
242 * For eg. when using HW decoders for zero-copy */
243 p_outfmt
->i_codec
= vlc_fourcc_GetCodecFromString(
245 gst_structure_get_string( p_str
, "format" ) );
246 if( !p_outfmt
->i_codec
)
249 gst_structure_get_int( p_str
, "width", &p_voutfmt
->i_width
);
250 gst_structure_get_int( p_str
, "height", &p_voutfmt
->i_height
);
252 b_ret
= gst_structure_get_fraction( p_str
,
253 "pixel-aspect-ratio",
254 &p_voutfmt
->i_sar_num
,
255 &p_voutfmt
->i_sar_den
);
257 if( !b_ret
|| !p_voutfmt
->i_sar_num
||
258 !p_voutfmt
->i_sar_den
)
260 p_voutfmt
->i_sar_num
= 1;
261 p_voutfmt
->i_sar_den
= 1;
264 b_ret
= gst_structure_get_fraction( p_str
, "framerate",
265 &p_voutfmt
->i_frame_rate
,
266 &p_voutfmt
->i_frame_rate_base
);
268 if( !b_ret
|| !p_voutfmt
->i_frame_rate
||
269 !p_voutfmt
->i_frame_rate_base
)
271 p_voutfmt
->i_frame_rate
= p_vinfmt
->i_frame_rate
;
272 p_voutfmt
->i_frame_rate_base
= p_vinfmt
->i_frame_rate_base
;
278 static bool set_out_fmt( decoder_t
*p_dec
, GstPad
*p_pad
)
280 GstCaps
*p_caps
= gst_pad_get_current_caps( p_pad
);
281 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
284 if( !gst_video_info_from_caps( &p_sys
->vinfo
,
287 msg_Err( p_dec
, "failed to get video info from caps" );
288 gst_caps_unref( p_caps
);
289 GST_ELEMENT_ERROR( p_sys
->p_decoder
, STREAM
, FAILED
,
290 ( "vlc stream error" ), NULL
);
294 p_str
= gst_caps_get_structure( p_caps
, 0 );
296 if( !set_vout_format( p_str
, &p_dec
->fmt_in
, &p_dec
->fmt_out
) )
298 msg_Err( p_dec
, "failed to set out format" );
299 gst_caps_unref( p_caps
);
300 GST_ELEMENT_ERROR( p_sys
->p_decoder
, STREAM
, FAILED
,
301 ( "vlc stream error" ), NULL
);
305 gst_caps_unref( p_caps
);
309 /* Emitted by decodebin and links decodebin to fakesink.
310 * Since only one elementary codec stream is fed to decodebin,
311 * this signal cannot be emitted more than once. */
312 static void pad_added_cb( GstElement
*p_ele
, GstPad
*p_pad
, gpointer p_data
)
315 decoder_t
*p_dec
= p_data
;
316 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
318 if( likely( gst_pad_has_current_caps( p_pad
) ) )
322 if( !set_out_fmt( p_dec
, p_pad
) )
325 p_sinkpad
= gst_element_get_static_pad(
326 p_sys
->p_decode_out
, "sink" );
327 gst_pad_link( p_pad
, p_sinkpad
);
328 gst_object_unref( p_sinkpad
);
332 msg_Err( p_dec
, "decodebin src pad has no caps" );
333 GST_ELEMENT_ERROR( p_sys
->p_decoder
, STREAM
, FAILED
,
334 ( "vlc stream error" ), NULL
);
338 /* Emitted by fakesink for every buffer and sets the
339 * output format (if not set). Adds the buffer to the queue */
340 static void frame_handoff_cb( GstElement
*p_ele
, GstBuffer
*p_buf
,
341 GstPad
*p_pad
, gpointer p_data
)
344 decoder_t
*p_dec
= p_data
;
345 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
347 if( unlikely( p_dec
->fmt_out
.i_codec
== 0 ) )
349 if( !gst_pad_has_current_caps( p_pad
) )
351 msg_Err( p_dec
, "fakesink pad has no caps" );
352 GST_ELEMENT_ERROR( p_sys
->p_decoder
, STREAM
, FAILED
,
353 ( "vlc stream error" ), NULL
);
357 if( !set_out_fmt( p_dec
, p_pad
) )
361 /* Push the buffer to the queue */
362 gst_atomic_queue_push( p_sys
->p_que
, gst_buffer_ref( p_buf
) );
365 /* Copy the frame data from the GstBuffer (from decoder)
366 * to the picture obtained from downstream in VLC.
367 * TODO(Zero-Copy): This function should be avoided as much
368 * as possible, since it involves a complete frame copy. */
369 static void gst_CopyPicture( picture_t
*p_pic
, GstVideoFrame
*p_frame
)
371 int i_plane
, i_planes
, i_line
, i_dst_stride
, i_src_stride
;
372 uint8_t *p_dst
, *p_src
;
375 i_planes
= p_pic
->i_planes
;
376 for( i_plane
= 0; i_plane
< i_planes
; i_plane
++ )
378 p_dst
= p_pic
->p
[i_plane
].p_pixels
;
379 p_src
= GST_VIDEO_FRAME_PLANE_DATA( p_frame
, i_plane
);
380 i_dst_stride
= p_pic
->p
[i_plane
].i_pitch
;
381 i_src_stride
= GST_VIDEO_FRAME_PLANE_STRIDE( p_frame
, i_plane
);
383 i_w
= GST_VIDEO_FRAME_COMP_WIDTH( p_frame
,
384 i_plane
) * GST_VIDEO_FRAME_COMP_PSTRIDE( p_frame
, i_plane
);
385 i_h
= GST_VIDEO_FRAME_COMP_HEIGHT( p_frame
, i_plane
);
388 i_line
< __MIN( p_pic
->p
[i_plane
].i_lines
, i_h
);
391 memcpy( p_dst
, p_src
, i_w
);
392 p_src
+= i_src_stride
;
393 p_dst
+= i_dst_stride
;
398 /* Check if the element can use this caps */
399 static gint
find_decoder_func( gconstpointer p_p1
, gconstpointer p_p2
)
401 GstElementFactory
*p_factory
;
402 sink_src_caps_t
*p_caps
;
404 p_factory
= ( GstElementFactory
* )p_p1
;
405 p_caps
= ( sink_src_caps_t
* )p_p2
;
407 return !( gst_element_factory_can_sink_any_caps( p_factory
,
408 p_caps
->p_sinkcaps
) &&
409 gst_element_factory_can_src_any_caps( p_factory
,
410 p_caps
->p_srccaps
) );
413 static bool default_msg_handler( decoder_t
*p_dec
, GstMessage
*p_msg
)
417 switch( GST_MESSAGE_TYPE( p_msg
) ){
418 case GST_MESSAGE_ERROR
:
423 gst_message_parse_error( p_msg
, &p_error
, &psz_debug
);
426 msg_Err( p_dec
, "Error from %s: %s",
427 GST_ELEMENT_NAME( GST_MESSAGE_SRC( p_msg
) ),
429 g_error_free( p_error
);
433 case GST_MESSAGE_WARNING
:
438 gst_message_parse_warning( p_msg
, &p_error
, &psz_debug
);
441 msg_Warn( p_dec
, "Warning from %s: %s",
442 GST_ELEMENT_NAME( GST_MESSAGE_SRC( p_msg
) ),
444 g_error_free( p_error
);
447 case GST_MESSAGE_INFO
:
452 gst_message_parse_info( p_msg
, &p_error
, &psz_debug
);
455 msg_Info( p_dec
, "Info from %s: %s",
456 GST_ELEMENT_NAME( GST_MESSAGE_SRC( p_msg
) ),
458 g_error_free( p_error
);
468 /*****************************************************************************
469 * OpenDecoder: probe the decoder and return score
470 *****************************************************************************/
471 static int OpenDecoder( vlc_object_t
*p_this
)
473 decoder_t
*p_dec
= ( decoder_t
* )p_this
;
474 decoder_sys_t
*p_sys
;
475 GstStateChangeReturn i_ret
;
477 sink_src_caps_t caps
= { NULL
, NULL
};
479 GstAppSrcCallbacks cb
;
480 int i_rval
= VLC_SUCCESS
;
484 #define VLC_GST_CHECK( r, v, s, t ) \
485 { if( r == v ){ msg_Err( p_dec, s ); i_rval = t; goto fail; } }
489 p_str
= vlc_to_gst_fmt( &p_dec
->fmt_in
);
493 /* Allocate the memory needed to store the decoder's structure */
494 p_sys
= p_dec
->p_sys
= calloc( 1, sizeof( *p_sys
) );
497 gst_structure_free( p_str
);
501 dbin
= var_CreateGetBool( p_dec
, "use-decodebin" );
502 msg_Dbg( p_dec
, "Using decodebin? %s", dbin
? "yes ":"no" );
504 caps
.p_sinkcaps
= gst_caps_new_empty( );
505 gst_caps_append_structure( caps
.p_sinkcaps
, p_str
);
506 /* Currently supports only system memory raw output format */
507 caps
.p_srccaps
= gst_caps_new_empty_simple( "video/x-raw" );
509 /* Get the list of all the available gstreamer decoders */
510 p_list
= gst_element_factory_list_get_elements(
511 GST_ELEMENT_FACTORY_TYPE_DECODER
, GST_RANK_MARGINAL
);
512 VLC_GST_CHECK( p_list
, NULL
, "no decoder list found", VLC_ENOMOD
);
516 /* Sort them as per ranks */
517 p_list
= g_list_sort( p_list
, gst_plugin_feature_rank_compare_func
);
518 VLC_GST_CHECK( p_list
, NULL
, "failed to sort decoders list",
520 p_l
= g_list_find_custom( p_list
, &caps
, find_decoder_func
);
521 VLC_GST_CHECK( p_l
, NULL
, "no suitable decoder found",
523 /* create the decoder with highest rank */
524 p_sys
->p_decode_in
= gst_element_factory_create(
525 ( GstElementFactory
* )p_l
->data
, NULL
);
526 VLC_GST_CHECK( p_sys
->p_decode_in
, NULL
,
527 "failed to create decoder", VLC_ENOMOD
);
532 /* Just check if any suitable decoder exists, rest will be
533 * handled by decodebin */
534 p_l
= g_list_find_custom( p_list
, &caps
, find_decoder_func
);
535 VLC_GST_CHECK( p_l
, NULL
, "no suitable decoder found",
538 gst_plugin_feature_list_free( p_list
);
540 gst_caps_unref( caps
.p_srccaps
);
541 caps
.p_srccaps
= NULL
;
543 p_sys
->b_prerolled
= false;
544 p_sys
->b_running
= false;
546 /* Queue: GStreamer thread will dump buffers into this queue,
547 * DecodeBlock() will pop out the buffers from the queue */
548 p_sys
->p_que
= gst_atomic_queue_new( 0 );
549 VLC_GST_CHECK( p_sys
->p_que
, NULL
, "failed to create queue",
552 p_sys
->p_decode_src
= gst_element_factory_make( "appsrc", NULL
);
553 VLC_GST_CHECK( p_sys
->p_decode_src
, NULL
, "appsrc not found",
555 g_object_set( G_OBJECT( p_sys
->p_decode_src
), "caps", caps
.p_sinkcaps
,
556 "block", FALSE
, "emit-signals", TRUE
, "format", GST_FORMAT_BYTES
,
557 "stream-type", GST_APP_STREAM_TYPE_SEEKABLE
,
558 /* Making DecodeBlock() to block on appsrc with max queue size of 1 byte.
559 * This will make the push_buffer() tightly coupled with the buffer
560 * flow from appsrc -> decoder. push_buffer() will only return when
561 * the same buffer it just fed to appsrc has also been fed to the
562 * decoder element as well */
563 "block", TRUE
, "max-bytes", ( guint64
)1, NULL
);
564 gst_caps_unref( caps
.p_sinkcaps
);
565 caps
.p_sinkcaps
= NULL
;
566 cb
.enough_data
= cb
.need_data
= NULL
;
567 cb
.seek_data
= seek_data_cb
;
568 gst_app_src_set_callbacks( GST_APP_SRC( p_sys
->p_decode_src
),
573 p_sys
->p_decode_in
= gst_element_factory_make( "decodebin", NULL
);
574 VLC_GST_CHECK( p_sys
->p_decode_in
, NULL
, "decodebin not found",
576 //g_object_set( G_OBJECT( p_sys->p_decode_in ),
577 //"max-size-buffers", 2, NULL );
578 g_signal_connect( G_OBJECT( p_sys
->p_decode_in
), "pad-added",
579 G_CALLBACK( pad_added_cb
), p_dec
);
580 g_signal_connect( G_OBJECT( p_sys
->p_decode_in
), "no-more-pads",
581 G_CALLBACK( no_more_pads_cb
), p_dec
);
584 /* fakesink: will emit signal for every available buffer */
585 p_sys
->p_decode_out
= gst_element_factory_make( "fakesink", NULL
);
586 VLC_GST_CHECK( p_sys
->p_decode_out
, NULL
, "fakesink not found",
588 /* connect to the signal with the callback */
589 g_object_set( G_OBJECT( p_sys
->p_decode_out
), "sync", FALSE
,
590 "enable-last-sample", FALSE
, "signal-handoffs", TRUE
, NULL
);
591 g_signal_connect( G_OBJECT( p_sys
->p_decode_out
), "handoff",
592 G_CALLBACK( frame_handoff_cb
), p_dec
);
594 p_sys
->p_decoder
= GST_ELEMENT( gst_bin_new( "decoder" ) );
595 VLC_GST_CHECK( p_sys
->p_decoder
, NULL
, "bin not found", VLC_ENOMOD
);
596 p_sys
->p_bus
= gst_bus_new( );
597 VLC_GST_CHECK( p_sys
->p_bus
, NULL
, "failed to create bus",
599 gst_element_set_bus( p_sys
->p_decoder
, p_sys
->p_bus
);
601 gst_bin_add_many( GST_BIN( p_sys
->p_decoder
),
602 p_sys
->p_decode_src
, p_sys
->p_decode_in
,
603 p_sys
->p_decode_out
, NULL
);
604 gst_object_ref( p_sys
->p_decode_src
);
605 gst_object_ref( p_sys
->p_decode_in
);
606 gst_object_ref( p_sys
->p_decode_out
);
608 b_ret
= gst_element_link( p_sys
->p_decode_src
, p_sys
->p_decode_in
);
609 VLC_GST_CHECK( b_ret
, FALSE
, "failed to link src <-> in",
614 b_ret
= gst_element_link( p_sys
->p_decode_in
, p_sys
->p_decode_out
);
615 VLC_GST_CHECK( b_ret
, FALSE
, "failed to link in <-> out",
619 p_dec
->fmt_out
.i_cat
= p_dec
->fmt_in
.i_cat
;
621 /* set the pipeline to playing */
622 i_ret
= gst_element_set_state( p_sys
->p_decoder
, GST_STATE_PLAYING
);
623 VLC_GST_CHECK( i_ret
, GST_STATE_CHANGE_FAILURE
,
624 "set state failure", VLC_EGENERIC
);
625 p_sys
->b_running
= true;
628 p_dec
->pf_decode_video
= DecodeBlock
;
633 if( caps
.p_sinkcaps
)
634 gst_caps_unref( caps
.p_sinkcaps
);
636 gst_caps_unref( caps
.p_srccaps
);
638 gst_plugin_feature_list_free( p_list
);
639 CloseDecoder( ( vlc_object_t
* )p_dec
);
644 static picture_t
*DecodeBlock( decoder_t
*p_dec
, block_t
**pp_block
)
647 picture_t
*p_pic
= NULL
;
648 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
661 if( unlikely( p_block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
|
662 BLOCK_FLAG_CORRUPTED
) ) )
664 if( p_block
->i_flags
& BLOCK_FLAG_DISCONTINUITY
)
667 /* Send a new segment event. Seeking position is
668 * irrelevant in this case, as the main motive for a
669 * seek here, is to tell the elements to start flushing
670 * and start accepting buffers from a new time segment */
671 b_ret
= gst_element_seek_simple( p_sys
->p_decoder
,
672 GST_FORMAT_BYTES
, GST_SEEK_FLAG_FLUSH
, 0 );
673 msg_Dbg( p_dec
, "new segment event : %d", b_ret
);
675 /* flush the output buffers from the queue */
676 while( ( p_buffer
= gst_atomic_queue_pop( p_sys
->p_que
) ) )
677 gst_buffer_unref( p_buffer
);
679 p_sys
->b_prerolled
= false;
682 block_Release( p_block
);
686 if( likely( p_block
->i_buffer
) )
688 p_buf
= gst_buffer_new_wrapped_full( GST_MEMORY_FLAG_READONLY
,
689 p_block
->p_start
, p_block
->i_size
,
690 p_block
->p_buffer
- p_block
->p_start
, p_block
->i_buffer
,
691 p_block
, ( GDestroyNotify
)block_Release
);
692 if( unlikely( p_buf
== NULL
) )
694 msg_Err( p_dec
, "failed to create input gstbuffer" );
695 p_dec
->b_error
= true;
696 block_Release( p_block
);
700 if( p_block
->i_dts
> VLC_TS_INVALID
)
701 GST_BUFFER_DTS( p_buf
) = gst_util_uint64_scale( p_block
->i_dts
,
702 GST_SECOND
, GST_MSECOND
);
704 if( p_block
->i_pts
<= VLC_TS_INVALID
)
705 GST_BUFFER_PTS( p_buf
) = GST_BUFFER_DTS( p_buf
);
707 GST_BUFFER_PTS( p_buf
) = gst_util_uint64_scale( p_block
->i_pts
,
708 GST_SECOND
, GST_MSECOND
);
710 if( p_block
->i_length
> VLC_TS_INVALID
)
711 GST_BUFFER_DURATION( p_buf
) = gst_util_uint64_scale(
712 p_block
->i_length
, GST_SECOND
, GST_MSECOND
);
714 if( p_dec
->fmt_in
.video
.i_frame_rate
&&
715 p_dec
->fmt_in
.video
.i_frame_rate_base
)
716 GST_BUFFER_DURATION( p_buf
) = gst_util_uint64_scale( GST_SECOND
,
717 p_dec
->fmt_in
.video
.i_frame_rate_base
,
718 p_dec
->fmt_in
.video
.i_frame_rate
);
720 /* Give the input buffer to GStreamer Bin.
725 * ___v____GSTREAMER BIN_____/____
727 * | appsrc-->decode-->fakesink |
728 * |_______________________________|
730 * * * * * * * * * * * * * * * * * * * * */
731 if( unlikely( gst_app_src_push_buffer(
732 GST_APP_SRC_CAST( p_sys
->p_decode_src
), p_buf
)
735 /* block will be released internally,
736 * when gst_buffer_unref() is called */
737 p_dec
->b_error
= true;
738 msg_Err( p_dec
, "failed to push buffer" );
743 block_Release( p_block
);
746 /* Poll for any messages, errors */
747 p_msg
= gst_bus_pop_filtered( p_sys
->p_bus
,
748 GST_MESSAGE_ASYNC_DONE
| GST_MESSAGE_ERROR
|
749 GST_MESSAGE_EOS
| GST_MESSAGE_WARNING
|
753 switch( GST_MESSAGE_TYPE( p_msg
) ){
754 case GST_MESSAGE_EOS
:
755 /* for debugging purpose */
756 msg_Warn( p_dec
, "got unexpected eos" );
758 /* First buffer received */
759 case GST_MESSAGE_ASYNC_DONE
:
760 /* for debugging purpose */
761 p_sys
->b_prerolled
= true;
762 msg_Dbg( p_dec
, "Pipeline is prerolled" );
765 p_dec
->b_error
= default_msg_handler( p_dec
, p_msg
);
768 gst_message_unref( p_msg
);
773 gst_message_unref( p_msg
);
776 /* Look for any output buffers in the queue */
777 if( gst_atomic_queue_peek( p_sys
->p_que
) )
781 /* Get a new picture */
782 p_pic
= decoder_NewPicture( p_dec
);
786 p_buf
= GST_BUFFER_CAST(
787 gst_atomic_queue_pop( p_sys
->p_que
) );
789 if( likely( GST_BUFFER_PTS_IS_VALID( p_buf
) ) )
790 p_pic
->date
= gst_util_uint64_scale(
791 GST_BUFFER_PTS( p_buf
), GST_MSECOND
, GST_SECOND
);
793 msg_Warn( p_dec
, "Gst Buffer has no timestamp" );
795 if( unlikely( !gst_video_frame_map( &frame
,
796 &p_sys
->vinfo
, p_buf
, GST_MAP_READ
) ) )
798 msg_Err( p_dec
, "failed to map gst video frame" );
799 gst_buffer_unref( p_buf
);
800 p_dec
->b_error
= true;
804 gst_CopyPicture( p_pic
, &frame
);
805 gst_video_frame_unmap( &frame
);
806 gst_buffer_unref( p_buf
);
814 /* Close the decoder instance */
815 static void CloseDecoder( vlc_object_t
*p_this
)
817 decoder_t
*p_dec
= ( decoder_t
* )p_this
;
818 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
820 if( p_sys
->b_running
)
825 /* Send EOS to the pipeline */
826 i_ret
= gst_app_src_end_of_stream(
827 GST_APP_SRC_CAST( p_sys
->p_decode_src
) );
828 msg_Dbg( p_dec
, "app src eos: %s", gst_flow_get_name( i_ret
) );
830 /* and catch it on the bus with a timeout */
831 p_msg
= gst_bus_timed_pop_filtered( p_sys
->p_bus
,
832 2000000000ULL, GST_MESSAGE_EOS
| GST_MESSAGE_ERROR
);
836 switch( GST_MESSAGE_TYPE( p_msg
) ){
837 case GST_MESSAGE_EOS
:
838 msg_Dbg( p_dec
, "got eos" );
841 p_dec
->b_error
= default_msg_handler( p_dec
, p_msg
);
843 msg_Warn( p_dec
, "pipeline may not close gracefully" );
847 gst_message_unref( p_msg
);
851 "no message, pipeline may not close gracefully" );
854 /* Remove any left-over buffers from the queue */
858 while( ( p_buf
= gst_atomic_queue_pop( p_sys
->p_que
) ) )
859 gst_buffer_unref( p_buf
);
860 gst_atomic_queue_unref( p_sys
->p_que
);
863 if( p_sys
->b_running
&&
864 gst_element_set_state( p_sys
->p_decoder
, GST_STATE_NULL
)
865 != GST_STATE_CHANGE_SUCCESS
)
867 "failed to change the state to NULL," \
868 "pipeline may not close gracefully" );
871 gst_object_unref( p_sys
->p_bus
);
872 if( p_sys
->p_decode_src
)
873 gst_object_unref( p_sys
->p_decode_src
);
874 if( p_sys
->p_decode_in
)
875 gst_object_unref( p_sys
->p_decode_in
);
876 if( p_sys
->p_decode_out
)
877 gst_object_unref( p_sys
->p_decode_out
);
878 if( p_sys
->p_decoder
)
879 gst_object_unref( p_sys
->p_decoder
);