]> git.sesse.net Git - vlc/blob - modules/codec/gstdecode.c
mediacodec: fix warning
[vlc] / modules / codec / gstdecode.c
1 /*****************************************************************************
2  * gstdecode.c: Decoder module making use of gstreamer
3  *****************************************************************************
4  * Copyright (C) 2014 VLC authors and VideoLAN
5  * $Id:
6  *
7  * Author: Vikram Fugro <vikram.fugro@gmail.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
34
35 #include <gst/gst.h>
36 #include <gst/video/video.h>
37 #include <gst/video/gstvideometa.h>
38 #include <gst/app/gstappsrc.h>
39 #include <gst/gstatomicqueue.h>
40
41 struct decoder_sys_t
42 {
43     GstElement *p_decoder;
44     GstElement *p_decode_src;
45     GstElement *p_decode_in;
46     GstElement *p_decode_out;
47
48     GstBus *p_bus;
49
50     GstVideoInfo vinfo;
51     GstAtomicQueue *p_que;
52     bool b_prerolled;
53     bool b_running;
54 };
55
56 typedef struct
57 {
58     GstCaps *p_sinkcaps;
59     GstCaps *p_srccaps;
60 } sink_src_caps_t;
61
62 /*****************************************************************************
63  * Local prototypes
64  *****************************************************************************/
65 static int  OpenDecoder( vlc_object_t* );
66 static void CloseDecoder( vlc_object_t* );
67 static picture_t *DecodeBlock( decoder_t*, block_t** );
68
69 #define MODULE_DESCRIPTION N_( "Uses GStreamer framework's plugins " \
70         "to decode the media codecs" )
71
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." )
79
80 vlc_module_begin( )
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 )
93 vlc_module_end( )
94
95 /* gst_init( ) is not thread-safe, hence a thread-safe wrapper */
96 static void vlc_gst_init( void )
97 {
98     static vlc_mutex_t init_lock = VLC_STATIC_MUTEX;
99
100     vlc_mutex_lock( &init_lock );
101     gst_init( NULL, NULL );
102     vlc_mutex_unlock( &init_lock );
103 }
104
105 static GstStructure* vlc_to_gst_fmt( const es_format_t *p_fmt )
106 {
107     const video_format_t *p_vfmt = &p_fmt->video;
108     GstStructure *p_str = NULL;
109
110     switch( p_fmt->i_codec ){
111     case VLC_CODEC_H264:
112         p_str = gst_structure_new_empty( "video/x-h264" );
113         gst_structure_set( p_str, "alignment", G_TYPE_STRING, "au", NULL );
114         break;
115     case VLC_CODEC_MP4V:
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 );
119         break;
120     case VLC_CODEC_VP8:
121         p_str = gst_structure_new_empty( "video/x-vp8" );
122         break;
123     case VLC_CODEC_MPGV:
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 );
127         break;
128     case VLC_CODEC_FLV1:
129         p_str = gst_structure_new_empty( "video/x-flash-video" );
130         gst_structure_set( p_str, "flvversion", G_TYPE_INT, 1, NULL );
131         break;
132     case VLC_CODEC_WMV1:
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 );
136         break;
137     case VLC_CODEC_WMV2:
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 );
141         break;
142     case VLC_CODEC_WMV3:
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 );
146         break;
147     case VLC_CODEC_VC1:
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 );
151         break;
152     default:
153         /* unsupported codec */
154         return NULL;
155     }
156
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 );
161
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 );
166
167     if( p_vfmt->i_sar_num && p_vfmt->i_sar_den )
168         gst_structure_set( p_str, "pixel-aspect-ratio", GST_TYPE_FRACTION,
169                 p_vfmt->i_sar_num,
170                 p_vfmt->i_sar_den, NULL );
171
172     if( p_fmt->i_extra )
173     {
174         GstBuffer *p_buf;
175
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 );
179         if( p_buf == NULL )
180         {
181             gst_structure_free( p_str );
182             return NULL;
183         }
184
185         gst_structure_set( p_str, "codec_data", GST_TYPE_BUFFER, p_buf, NULL );
186         gst_buffer_unref( p_buf );
187     }
188
189     return p_str;
190 }
191
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,
197         gpointer p_data )
198 {
199     VLC_UNUSED( p_src );
200     decoder_t *p_dec = p_data;
201     msg_Dbg( p_dec, "appsrc seeking to %"G_GUINT64_FORMAT, l_offset );
202     return TRUE;
203 }
204
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 )
212 {
213     VLC_UNUSED( p_ele );
214     decoder_t *p_dec = p_data;
215     decoder_sys_t *p_sys = p_dec->p_sys;
216     GstPad *p_pad;
217
218     msg_Dbg( p_dec, "no more pads" );
219
220     p_pad = gst_element_get_static_pad( p_sys->p_decode_out,
221             "sink" );
222     if( !gst_pad_is_linked( p_pad ) )
223     {
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 );
227     }
228
229     gst_object_unref( p_pad );
230 }
231
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 )
235 {
236     video_format_t *p_voutfmt = &p_outfmt->video;
237     const video_format_t *p_vinfmt = &p_infmt->video;
238     gboolean b_ret;
239
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(
244             VIDEO_ES,
245             gst_structure_get_string( p_str, "format" ) );
246     if( !p_outfmt->i_codec )
247         return false;
248
249     gst_structure_get_int( p_str, "width", &p_voutfmt->i_width );
250     gst_structure_get_int( p_str, "height", &p_voutfmt->i_height );
251
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 );
256
257     if( !b_ret || !p_voutfmt->i_sar_num ||
258             !p_voutfmt->i_sar_den )
259     {
260         p_voutfmt->i_sar_num = 1;
261         p_voutfmt->i_sar_den = 1;
262     }
263
264     b_ret = gst_structure_get_fraction( p_str, "framerate",
265             &p_voutfmt->i_frame_rate,
266             &p_voutfmt->i_frame_rate_base );
267
268     if( !b_ret || !p_voutfmt->i_frame_rate ||
269             !p_voutfmt->i_frame_rate_base )
270     {
271         p_voutfmt->i_frame_rate = p_vinfmt->i_frame_rate;
272         p_voutfmt->i_frame_rate_base = p_vinfmt->i_frame_rate_base;
273     }
274
275     return true;
276 }
277
278 static bool set_out_fmt( decoder_t *p_dec, GstPad *p_pad )
279 {
280     GstCaps *p_caps = gst_pad_get_current_caps( p_pad );
281     decoder_sys_t *p_sys = p_dec->p_sys;
282     GstStructure *p_str;
283
284     if( !gst_video_info_from_caps( &p_sys->vinfo,
285                 p_caps ) )
286     {
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 );
291         return false;
292     }
293
294     p_str = gst_caps_get_structure( p_caps, 0 );
295
296     if( !set_vout_format( p_str, &p_dec->fmt_in, &p_dec->fmt_out ) )
297     {
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 );
302         return false;
303     }
304
305     gst_caps_unref( p_caps );
306     return true;
307 }
308
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 )
313 {
314     VLC_UNUSED( p_ele );
315     decoder_t *p_dec = p_data;
316     decoder_sys_t *p_sys = p_dec->p_sys;
317
318     if( likely( gst_pad_has_current_caps( p_pad ) ) )
319     {
320         GstPad *p_sinkpad;
321
322         if( !set_out_fmt( p_dec, p_pad ) )
323             return;
324
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 );
329     }
330     else
331     {
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 );
335     }
336 }
337
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 )
342 {
343     VLC_UNUSED( p_ele );
344     decoder_t *p_dec = p_data;
345     decoder_sys_t *p_sys = p_dec->p_sys;
346
347     if( unlikely( p_dec->fmt_out.i_codec == 0 ) )
348     {
349         if( !gst_pad_has_current_caps( p_pad ) )
350         {
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 );
354             return;
355         }
356
357         if( !set_out_fmt( p_dec, p_pad ) )
358             return;
359     }
360
361     /* Push the buffer to the queue */
362     gst_atomic_queue_push( p_sys->p_que, gst_buffer_ref( p_buf ) );
363 }
364
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 )
370 {
371     int i_plane, i_planes, i_line, i_dst_stride, i_src_stride;
372     uint8_t *p_dst, *p_src;
373     int i_w, i_h;
374
375     i_planes = p_pic->i_planes;
376     for( i_plane = 0; i_plane < i_planes; i_plane++ )
377     {
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 );
382
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 );
386
387         for( i_line = 0;
388                 i_line < __MIN( p_pic->p[i_plane].i_lines, i_h );
389                 i_line++ )
390         {
391             memcpy( p_dst, p_src, i_w );
392             p_src += i_src_stride;
393             p_dst += i_dst_stride;
394         }
395     }
396 }
397
398 /* Check if the element can use this caps */
399 static gint find_decoder_func( gconstpointer p_p1, gconstpointer p_p2 )
400 {
401     GstElementFactory *p_factory;
402     sink_src_caps_t *p_caps;
403
404     p_factory = ( GstElementFactory* )p_p1;
405     p_caps = ( sink_src_caps_t* )p_p2;
406
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 ) );
411 }
412
413 static bool default_msg_handler( decoder_t *p_dec, GstMessage *p_msg )
414 {
415     bool err = false;
416
417     switch( GST_MESSAGE_TYPE( p_msg ) ){
418     case GST_MESSAGE_ERROR:
419         {
420             gchar  *psz_debug;
421             GError *p_error;
422
423             gst_message_parse_error( p_msg, &p_error, &psz_debug );
424             g_free( psz_debug );
425
426             msg_Err( p_dec, "Error from %s: %s",
427                     GST_ELEMENT_NAME( GST_MESSAGE_SRC( p_msg ) ),
428                     p_error->message );
429             g_error_free( p_error );
430             err = true;
431         }
432         break;
433     case GST_MESSAGE_WARNING:
434         {
435             gchar  *psz_debug;
436             GError *p_error;
437
438             gst_message_parse_warning( p_msg, &p_error, &psz_debug );
439             g_free( psz_debug );
440
441             msg_Warn( p_dec, "Warning from %s: %s",
442                     GST_ELEMENT_NAME( GST_MESSAGE_SRC( p_msg ) ),
443                     p_error->message );
444             g_error_free( p_error );
445         }
446         break;
447     case GST_MESSAGE_INFO:
448         {
449             gchar  *psz_debug;
450             GError *p_error;
451
452             gst_message_parse_info( p_msg, &p_error, &psz_debug );
453             g_free( psz_debug );
454
455             msg_Info( p_dec, "Info from %s: %s",
456                     GST_ELEMENT_NAME( GST_MESSAGE_SRC( p_msg ) ),
457                     p_error->message );
458             g_error_free( p_error );
459         }
460         break;
461     default:
462         break;
463     }
464
465     return err;
466 }
467
468 /*****************************************************************************
469  * OpenDecoder: probe the decoder and return score
470  *****************************************************************************/
471 static int OpenDecoder( vlc_object_t *p_this )
472 {
473     decoder_t *p_dec = ( decoder_t* )p_this;
474     decoder_sys_t *p_sys;
475     GstStateChangeReturn i_ret;
476     gboolean b_ret;
477     sink_src_caps_t caps = { NULL, NULL };
478     GstStructure *p_str;
479     GstAppSrcCallbacks cb;
480     int i_rval = VLC_SUCCESS;
481     GList *p_list;
482     bool dbin;
483
484 #define VLC_GST_CHECK( r, v, s, t ) \
485     { if( r == v ){ msg_Err( p_dec, s ); i_rval = t; goto fail; } }
486
487     vlc_gst_init( );
488
489     p_str = vlc_to_gst_fmt( &p_dec->fmt_in );
490     if( !p_str )
491         return VLC_EGENERIC;
492
493     /* Allocate the memory needed to store the decoder's structure */
494     p_sys = p_dec->p_sys = calloc( 1, sizeof( *p_sys ) );
495     if( p_sys == NULL )
496     {
497         gst_structure_free( p_str );
498         return VLC_ENOMEM;
499     }
500
501     dbin = var_CreateGetBool( p_dec, "use-decodebin" );
502     msg_Dbg( p_dec, "Using decodebin? %s", dbin ? "yes ":"no" );
503
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" );
508
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 );
513     if( !dbin )
514     {
515         GList *p_l;
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",
519                 VLC_ENOMOD );
520         p_l = g_list_find_custom( p_list, &caps, find_decoder_func );
521         VLC_GST_CHECK( p_l, NULL, "no suitable decoder found",
522                 VLC_ENOMOD );
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 );
528     }
529     else
530     {
531         GList *p_l;
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",
536                 VLC_ENOMOD );
537     }
538     gst_plugin_feature_list_free( p_list );
539     p_list = NULL;
540     gst_caps_unref( caps.p_srccaps );
541     caps.p_srccaps = NULL;
542
543     p_sys->b_prerolled = false;
544     p_sys->b_running = false;
545
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",
550             VLC_ENOMEM );
551
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",
554             VLC_ENOMOD );
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 ),
569             &cb, p_dec, NULL );
570
571     if( dbin )
572     {
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",
575                 VLC_ENOMOD );
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 );
582     }
583
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",
587             VLC_ENOMOD );
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 );
593
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",
598             VLC_ENOMOD );
599     gst_element_set_bus( p_sys->p_decoder, p_sys->p_bus );
600
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 );
607
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",
610             VLC_EGENERIC );
611
612     if( !dbin )
613     {
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",
616                 VLC_EGENERIC );
617     }
618
619     p_dec->fmt_out.i_cat = p_dec->fmt_in.i_cat;
620
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;
626
627     /* Force packetized for now */
628     p_dec->b_need_packetized = true;
629     /* Set callbacks */
630     p_dec->pf_decode_video = DecodeBlock;
631
632     return VLC_SUCCESS;
633
634 fail:
635     if( caps.p_sinkcaps )
636         gst_caps_unref( caps.p_sinkcaps );
637     if( caps.p_srccaps )
638         gst_caps_unref( caps.p_srccaps );
639     if( p_list )
640         gst_plugin_feature_list_free( p_list );
641     CloseDecoder( ( vlc_object_t* )p_dec );
642     return i_rval;
643 }
644
645 /* Decode */
646 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
647 {
648     block_t *p_block;
649     picture_t *p_pic = NULL;
650     decoder_sys_t *p_sys = p_dec->p_sys;
651     GstMessage *p_msg;
652     gboolean b_ret;
653     GstBuffer *p_buf;
654
655     if( !pp_block )
656         return NULL;
657
658     p_block = *pp_block;
659
660     if( !p_block )
661         goto check_messages;
662
663     if( unlikely( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY |
664                     BLOCK_FLAG_CORRUPTED ) ) )
665     {
666         if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
667         {
668             GstBuffer *p_buffer;
669             /* Send a new segment event. Seeking position is
670              * irrelevant in this case, as the main motive for a
671              * seek here, is to tell the elements to start flushing
672              * and start accepting buffers from a new time segment */
673             b_ret = gst_element_seek_simple( p_sys->p_decoder,
674                     GST_FORMAT_BYTES, GST_SEEK_FLAG_FLUSH, 0 );
675             msg_Dbg( p_dec, "new segment event : %d", b_ret );
676
677             /* flush the output buffers from the queue */
678             while( ( p_buffer = gst_atomic_queue_pop( p_sys->p_que ) ) )
679                 gst_buffer_unref( p_buffer );
680
681             p_sys->b_prerolled = false;
682         }
683
684         block_Release( p_block );
685         goto done;
686     }
687
688     if( likely( p_block->i_buffer ) )
689     {
690         p_buf = gst_buffer_new_wrapped_full( GST_MEMORY_FLAG_READONLY,
691                 p_block->p_start, p_block->i_size,
692                 p_block->p_buffer - p_block->p_start, p_block->i_buffer,
693                 p_block, ( GDestroyNotify )block_Release );
694         if( unlikely( p_buf == NULL ) )
695         {
696             msg_Err( p_dec, "failed to create input gstbuffer" );
697             p_dec->b_error = true;
698             block_Release( p_block );
699             goto done;
700         }
701
702         if( p_block->i_dts > VLC_TS_INVALID )
703             GST_BUFFER_DTS( p_buf ) = gst_util_uint64_scale( p_block->i_dts,
704                     GST_SECOND, GST_MSECOND );
705
706         if( p_block->i_pts <= VLC_TS_INVALID )
707             GST_BUFFER_PTS( p_buf ) = GST_BUFFER_DTS( p_buf );
708         else
709             GST_BUFFER_PTS( p_buf ) = gst_util_uint64_scale( p_block->i_pts,
710                     GST_SECOND, GST_MSECOND );
711
712         if( p_block->i_length > VLC_TS_INVALID )
713             GST_BUFFER_DURATION( p_buf ) = gst_util_uint64_scale(
714                     p_block->i_length, GST_SECOND, GST_MSECOND );
715
716         if( p_dec->fmt_in.video.i_frame_rate  &&
717                 p_dec->fmt_in.video.i_frame_rate_base )
718             GST_BUFFER_DURATION( p_buf ) = gst_util_uint64_scale( GST_SECOND,
719                     p_dec->fmt_in.video.i_frame_rate_base,
720                     p_dec->fmt_in.video.i_frame_rate );
721
722         /* Give the input buffer to GStreamer Bin.
723          *
724          *  libvlc                      libvlc
725          *    \ (i/p)              (o/p) ^
726          *     \                        /
727          *   ___v____GSTREAMER BIN_____/____
728          *  |                               |
729          *  |   appsrc-->decode-->fakesink  |
730          *  |_______________________________|
731          *
732          * * * * * * * * * * * * * * * * * * * * */
733         if( unlikely( gst_app_src_push_buffer(
734                         GST_APP_SRC_CAST( p_sys->p_decode_src ), p_buf )
735                     != GST_FLOW_OK ) )
736         {
737             /* block will be released internally,
738              * when gst_buffer_unref() is called */
739             p_dec->b_error = true;
740             msg_Err( p_dec, "failed to push buffer" );
741             goto done;
742         }
743     }
744     else
745         block_Release( p_block );
746
747 check_messages:
748     /* Poll for any messages, errors */
749     p_msg = gst_bus_pop_filtered( p_sys->p_bus,
750             GST_MESSAGE_ASYNC_DONE | GST_MESSAGE_ERROR |
751             GST_MESSAGE_EOS | GST_MESSAGE_WARNING |
752             GST_MESSAGE_INFO );
753     if( p_msg )
754     {
755         switch( GST_MESSAGE_TYPE( p_msg ) ){
756         case GST_MESSAGE_EOS:
757             /* for debugging purpose */
758             msg_Warn( p_dec, "got unexpected eos" );
759             break;
760         /* First buffer received */
761         case GST_MESSAGE_ASYNC_DONE:
762             /* for debugging purpose */
763             p_sys->b_prerolled = true;
764             msg_Dbg( p_dec, "Pipeline is prerolled" );
765             break;
766         default:
767             p_dec->b_error = default_msg_handler( p_dec, p_msg );
768             if( p_dec->b_error )
769             {
770                 gst_message_unref( p_msg );
771                 goto done;
772             }
773             break;
774         }
775         gst_message_unref( p_msg );
776     }
777
778     /* Look for any output buffers in the queue */
779     if( gst_atomic_queue_peek( p_sys->p_que ) )
780     {
781         GstVideoFrame frame;
782
783         /* Get a new picture */
784         p_pic = decoder_NewPicture( p_dec );
785         if( !p_pic )
786             goto done;
787
788         p_buf = GST_BUFFER_CAST(
789                 gst_atomic_queue_pop( p_sys->p_que ) );
790
791         if( likely( GST_BUFFER_PTS_IS_VALID( p_buf ) ) )
792             p_pic->date = gst_util_uint64_scale(
793                     GST_BUFFER_PTS( p_buf ), GST_MSECOND, GST_SECOND );
794         else
795             msg_Warn( p_dec, "Gst Buffer has no timestamp" );
796
797         if( unlikely( !gst_video_frame_map( &frame,
798                         &p_sys->vinfo, p_buf, GST_MAP_READ ) ) )
799         {
800             msg_Err( p_dec, "failed to map gst video frame" );
801             gst_buffer_unref( p_buf );
802             p_dec->b_error = true;
803             goto done;
804         }
805
806         gst_CopyPicture( p_pic, &frame );
807         gst_video_frame_unmap( &frame );
808         gst_buffer_unref( p_buf );
809     }
810
811 done:
812     *pp_block = NULL;
813     return p_pic;
814 }
815
816 /* Close the decoder instance */
817 static void CloseDecoder( vlc_object_t *p_this )
818 {
819     decoder_t *p_dec = ( decoder_t* )p_this;
820     decoder_sys_t *p_sys = p_dec->p_sys;
821
822     if( p_sys->b_running )
823     {
824         GstMessage *p_msg;
825         GstFlowReturn i_ret;
826
827         /* Send EOS to the pipeline */
828         i_ret = gst_app_src_end_of_stream(
829                 GST_APP_SRC_CAST( p_sys->p_decode_src ) );
830         msg_Dbg( p_dec, "app src eos: %s", gst_flow_get_name( i_ret ) );
831
832         /* and catch it on the bus with a timeout */
833         p_msg = gst_bus_timed_pop_filtered( p_sys->p_bus,
834                 2000000000ULL, GST_MESSAGE_EOS | GST_MESSAGE_ERROR );
835
836         if( p_msg )
837         {
838             switch( GST_MESSAGE_TYPE( p_msg ) ){
839             case GST_MESSAGE_EOS:
840                 msg_Dbg( p_dec, "got eos" );
841                 break;
842             default:
843                 p_dec->b_error = default_msg_handler( p_dec, p_msg );
844                 if( p_dec->b_error )
845                     msg_Warn( p_dec, "pipeline may not close gracefully" );
846                 break;
847             }
848
849             gst_message_unref( p_msg );
850         }
851         else
852             msg_Warn( p_dec,
853                     "no message, pipeline may not close gracefully" );
854     }
855
856     /* Remove any left-over buffers from the queue */
857     if( p_sys->p_que )
858     {
859         GstBuffer *p_buf;
860         while( ( p_buf = gst_atomic_queue_pop( p_sys->p_que ) ) )
861             gst_buffer_unref( p_buf );
862         gst_atomic_queue_unref( p_sys->p_que );
863     }
864
865     if( p_sys->b_running &&
866             gst_element_set_state( p_sys->p_decoder, GST_STATE_NULL )
867             != GST_STATE_CHANGE_SUCCESS )
868         msg_Warn( p_dec,
869                 "failed to change the state to NULL," \
870                 "pipeline may not close gracefully" );
871
872     if( p_sys->p_bus )
873         gst_object_unref( p_sys->p_bus );
874     if( p_sys->p_decode_src )
875         gst_object_unref( p_sys->p_decode_src );
876     if( p_sys->p_decode_in )
877         gst_object_unref( p_sys->p_decode_in );
878     if( p_sys->p_decode_out )
879         gst_object_unref( p_sys->p_decode_out );
880     if( p_sys->p_decoder )
881         gst_object_unref( p_sys->p_decoder );
882
883     free( p_sys );
884 }