]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gst-plugins-ugly0-10.git/blob - ext/mpeg2dec/gstmpeg2dec.c
51c47e66d6522f0eac92148cf39875c26aa0b4ec
[glsdk/gst-plugins-ugly0-10.git] / ext / mpeg2dec / gstmpeg2dec.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <string.h>
25 #include <inttypes.h>
27 #include "gstmpeg2dec.h"
29 /* 16byte-aligns a buffer for libmpeg2 */
30 #define ALIGN_16(p) ((void *)(((uintptr_t)(p) + 15) & ~((uintptr_t)15)))
32 /* mpeg2dec changed a struct name after 0.3.1, here's a workaround */
33 /* mpeg2dec also only defined MPEG2_RELEASE after 0.3.1
34    #if MPEG2_RELEASE < MPEG2_VERSION(0,3,2)
35 */
36 #ifndef MPEG2_RELEASE
37 #define MPEG2_VERSION(a,b,c) ((((a)&0xff)<<16)|(((b)&0xff)<<8)|((c)&0xff))
38 #define MPEG2_RELEASE MPEG2_VERSION(0,3,1)
39 typedef picture_t mpeg2_picture_t;
40 typedef gint mpeg2_state_t;
42 #define STATE_BUFFER 0
43 #endif
45 GST_DEBUG_CATEGORY_STATIC (mpeg2dec_debug);
46 #define GST_CAT_DEFAULT (mpeg2dec_debug)
48 /* Send a warning message about decoding errors after receiving this many
49  * STATE_INVALID return values from mpeg2_parse. -1 means never.
50  */
51 #define WARN_THRESHOLD (5)
53 //#define enable_user_data
54 #ifdef enable_user_data
55 static GstStaticPadTemplate user_data_template_factory =
56 GST_STATIC_PAD_TEMPLATE ("user_data",
57     GST_PAD_SRC,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS_ANY);
60 #endif
62 static GstStaticPadTemplate sink_template_factory =
63 GST_STATIC_PAD_TEMPLATE ("sink",
64     GST_PAD_SINK,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("video/mpeg, "
67         "mpegversion = (int) [ 1, 2 ], " "systemstream = (boolean) false")
68     );
70 static GstStaticPadTemplate src_template_factory =
71 GST_STATIC_PAD_TEMPLATE ("src",
72     GST_PAD_SRC,
73     GST_PAD_ALWAYS,
74     GST_STATIC_CAPS ("video/x-raw-yuv, "
75         "format = (fourcc) { YV12, I420, Y42B, Y444 }, "
76         "width = (int) [ 16, 4096 ], "
77         "height = (int) [ 16, 4096 ], "
78         "framerate = (fraction) [ 0/1, 2147483647/1 ]")
79     );
81 static void gst_mpeg2dec_base_init (gpointer g_class);
82 static void gst_mpeg2dec_class_init (GstMpeg2decClass * klass);
83 static void gst_mpeg2dec_init (GstMpeg2dec * mpeg2dec);
85 static void gst_mpeg2dec_finalize (GObject * object);
86 static void gst_mpeg2dec_reset (GstMpeg2dec * mpeg2dec);
88 static void gst_mpeg2dec_set_index (GstElement * element, GstIndex * index);
89 static GstIndex *gst_mpeg2dec_get_index (GstElement * element);
91 static gboolean gst_mpeg2dec_src_event (GstPad * pad, GstEvent * event);
92 static const GstQueryType *gst_mpeg2dec_get_src_query_types (GstPad * pad);
94 static gboolean gst_mpeg2dec_src_query (GstPad * pad, GstQuery * query);
96 static gboolean gst_mpeg2dec_sink_convert (GstPad * pad, GstFormat src_format,
97     gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
98 static gboolean gst_mpeg2dec_src_convert (GstPad * pad, GstFormat src_format,
99     gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
101 static GstStateChangeReturn gst_mpeg2dec_change_state (GstElement * element,
102     GstStateChange transition);
104 static gboolean gst_mpeg2dec_sink_event (GstPad * pad, GstEvent * event);
105 static gboolean gst_mpeg2dec_setcaps (GstPad * pad, GstCaps * caps);
106 static GstFlowReturn gst_mpeg2dec_chain (GstPad * pad, GstBuffer * buf);
108 static void clear_buffers (GstMpeg2dec * mpeg2dec);
110 //static gboolean gst_mpeg2dec_sink_query (GstPad * pad, GstQuery * query);
112 #if 0
113 static const GstFormat *gst_mpeg2dec_get_formats (GstPad * pad);
114 #endif
116 #if 0
117 static const GstEventMask *gst_mpeg2dec_get_event_masks (GstPad * pad);
118 #endif
120 static GstElementClass *parent_class = NULL;
122 static gboolean gst_mpeg2dec_crop_buffer (GstMpeg2dec * dec, GstBuffer ** buf);
124 /*static guint gst_mpeg2dec_signals[LAST_SIGNAL] = { 0 };*/
126 GType
127 gst_mpeg2dec_get_type (void)
129   static GType mpeg2dec_type = 0;
131   if (!mpeg2dec_type) {
132     static const GTypeInfo mpeg2dec_info = {
133       sizeof (GstMpeg2decClass),
134       gst_mpeg2dec_base_init,
135       NULL,
136       (GClassInitFunc) gst_mpeg2dec_class_init,
137       NULL,
138       NULL,
139       sizeof (GstMpeg2dec),
140       0,
141       (GInstanceInitFunc) gst_mpeg2dec_init,
142     };
144     mpeg2dec_type =
145         g_type_register_static (GST_TYPE_ELEMENT, "GstMpeg2dec", &mpeg2dec_info,
146         0);
147   }
149   GST_DEBUG_CATEGORY_INIT (mpeg2dec_debug, "mpeg2dec", 0,
150       "MPEG2 decoder element");
152   return mpeg2dec_type;
155 static void
156 gst_mpeg2dec_base_init (gpointer g_class)
158   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
160   gst_element_class_add_static_pad_template (element_class,
161       &src_template_factory);
162   gst_element_class_add_static_pad_template (element_class,
163       &sink_template_factory);
164 #ifdef enable_user_data
165   gst_element_class_add_static_pad_template (element_class,
166       &user_data_template_factory);
167 #endif
168   gst_element_class_set_details_simple (element_class,
169       "mpeg1 and mpeg2 video decoder", "Codec/Decoder/Video",
170       "Uses libmpeg2 to decode MPEG video streams",
171       "Wim Taymans <wim.taymans@chello.be>");
174 static void
175 gst_mpeg2dec_class_init (GstMpeg2decClass * klass)
177   GObjectClass *gobject_class;
178   GstElementClass *gstelement_class;
180   gobject_class = (GObjectClass *) klass;
181   gstelement_class = (GstElementClass *) klass;
183   parent_class = g_type_class_peek_parent (klass);
185   gobject_class->finalize = gst_mpeg2dec_finalize;
187   gstelement_class->change_state = gst_mpeg2dec_change_state;
188   gstelement_class->set_index = gst_mpeg2dec_set_index;
189   gstelement_class->get_index = gst_mpeg2dec_get_index;
192 static void
193 gst_mpeg2dec_init (GstMpeg2dec * mpeg2dec)
195   /* create the sink and src pads */
196   mpeg2dec->sinkpad =
197       gst_pad_new_from_static_template (&sink_template_factory, "sink");
198   gst_pad_set_chain_function (mpeg2dec->sinkpad,
199       GST_DEBUG_FUNCPTR (gst_mpeg2dec_chain));
200 #if 0
201   gst_pad_set_query_function (mpeg2dec->sinkpad,
202       GST_DEBUG_FUNCPTR (gst_mpeg2dec_get_sink_query));
203 #endif
204   gst_pad_set_event_function (mpeg2dec->sinkpad,
205       GST_DEBUG_FUNCPTR (gst_mpeg2dec_sink_event));
206   gst_pad_set_setcaps_function (mpeg2dec->sinkpad,
207       GST_DEBUG_FUNCPTR (gst_mpeg2dec_setcaps));
208   gst_element_add_pad (GST_ELEMENT (mpeg2dec), mpeg2dec->sinkpad);
210   mpeg2dec->srcpad =
211       gst_pad_new_from_static_template (&src_template_factory, "src");
212   gst_pad_set_event_function (mpeg2dec->srcpad,
213       GST_DEBUG_FUNCPTR (gst_mpeg2dec_src_event));
214   gst_pad_set_query_type_function (mpeg2dec->srcpad,
215       GST_DEBUG_FUNCPTR (gst_mpeg2dec_get_src_query_types));
216   gst_pad_set_query_function (mpeg2dec->srcpad,
217       GST_DEBUG_FUNCPTR (gst_mpeg2dec_src_query));
218   gst_pad_use_fixed_caps (mpeg2dec->srcpad);
219   gst_element_add_pad (GST_ELEMENT (mpeg2dec), mpeg2dec->srcpad);
221 #ifdef enable_user_data
222   mpeg2dec->userdatapad =
223       gst_pad_new_from_static_template (&user_data_template_factory,
224       "user_data");
225   gst_element_add_pad (GST_ELEMENT (mpeg2dec), mpeg2dec->userdatapad);
226 #endif
228   mpeg2dec->error_count = 0;
229   mpeg2dec->can_allocate_aligned = TRUE;
231   /* initialize the mpeg2dec acceleration */
234 static void
235 gst_mpeg2dec_finalize (GObject * object)
237   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (object);
239   if (mpeg2dec->index) {
240     gst_object_unref (mpeg2dec->index);
241     mpeg2dec->index = NULL;
242     mpeg2dec->index_id = 0;
243   }
245   if (mpeg2dec->decoder) {
246     GST_DEBUG_OBJECT (mpeg2dec, "closing decoder");
247     mpeg2_close (mpeg2dec->decoder);
248     mpeg2dec->decoder = NULL;
249   }
250   clear_buffers (mpeg2dec);
251   g_free (mpeg2dec->dummybuf[3]);
252   mpeg2dec->dummybuf[3] = NULL;
254   G_OBJECT_CLASS (parent_class)->finalize (object);
257 static void
258 gst_mpeg2dec_reset (GstMpeg2dec * mpeg2dec)
260   if (mpeg2dec->index) {
261     gst_object_unref (mpeg2dec->index);
262     mpeg2dec->index = NULL;
263     mpeg2dec->index_id = 0;
264   }
266   /* reset the initial video state */
267   mpeg2dec->format = GST_VIDEO_FORMAT_UNKNOWN;
268   mpeg2dec->width = -1;
269   mpeg2dec->height = -1;
270   gst_segment_init (&mpeg2dec->segment, GST_FORMAT_UNDEFINED);
271   mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
272   mpeg2dec->frame_period = 0;
273   mpeg2dec->need_sequence = TRUE;
274   mpeg2dec->next_time = -1;
275   mpeg2dec->offset = 0;
276   mpeg2dec->error_count = 0;
277   mpeg2dec->can_allocate_aligned = TRUE;
278   mpeg2_reset (mpeg2dec->decoder, 1);
281 static void
282 gst_mpeg2dec_qos_reset (GstMpeg2dec * mpeg2dec)
284   GST_OBJECT_LOCK (mpeg2dec);
285   mpeg2dec->proportion = 1.0;
286   mpeg2dec->earliest_time = -1;
287   mpeg2dec->dropped = 0;
288   mpeg2dec->processed = 0;
289   GST_OBJECT_UNLOCK (mpeg2dec);
292 static void
293 gst_mpeg2dec_set_index (GstElement * element, GstIndex * index)
295   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (element);
297   GST_OBJECT_LOCK (mpeg2dec);
298   if (mpeg2dec->index)
299     gst_object_unref (mpeg2dec->index);
300   mpeg2dec->index = NULL;
301   mpeg2dec->index_id = 0;
302   if (index) {
303     mpeg2dec->index = gst_object_ref (index);
304   }
305   GST_OBJECT_UNLOCK (mpeg2dec);
306   /* object lock might be taken again */
307   if (index)
308     gst_index_get_writer_id (index, GST_OBJECT (element), &mpeg2dec->index_id);
311 static GstIndex *
312 gst_mpeg2dec_get_index (GstElement * element)
314   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (element);
316   return (mpeg2dec->index) ? gst_object_ref (mpeg2dec->index) : NULL;
319 static GstFlowReturn
320 gst_mpeg2dec_crop_buffer (GstMpeg2dec * dec, GstBuffer ** buf)
322   GstFlowReturn flow_ret;
323   GstBuffer *inbuf = *buf;
324   GstBuffer *outbuf;
325   guint outsize, c;
327   outsize = gst_video_format_get_size (dec->format, dec->width, dec->height);
329   GST_LOG_OBJECT (dec, "Copying input buffer %ux%u (%u) to output buffer "
330       "%ux%u (%u)", dec->decoded_width, dec->decoded_height,
331       GST_BUFFER_SIZE (inbuf), dec->width, dec->height, outsize);
333   flow_ret = gst_pad_alloc_buffer_and_set_caps (dec->srcpad,
334       GST_BUFFER_OFFSET_NONE, outsize, GST_PAD_CAPS (dec->srcpad), &outbuf);
336   if (G_UNLIKELY (flow_ret != GST_FLOW_OK))
337     return flow_ret;
339   for (c = 0; c < 3; c++) {
340     const guint8 *src;
341     guint8 *dest;
342     guint stride_in, stride_out;
343     guint c_height, c_width, line;
345     src =
346         GST_BUFFER_DATA (inbuf) +
347         gst_video_format_get_component_offset (dec->format, c,
348         dec->decoded_width, dec->decoded_height);
349     dest =
350         GST_BUFFER_DATA (outbuf) +
351         gst_video_format_get_component_offset (dec->format, c, dec->width,
352         dec->height);
353     stride_out = gst_video_format_get_row_stride (dec->format, c, dec->width);
354     stride_in =
355         gst_video_format_get_row_stride (dec->format, c, dec->decoded_width);
356     c_height =
357         gst_video_format_get_component_height (dec->format, c, dec->height);
358     c_width = gst_video_format_get_component_width (dec->format, c, dec->width);
360     for (line = 0; line < c_height; line++) {
361       memcpy (dest, src, c_width);
362       dest += stride_out;
363       src += stride_in;
364     }
365   }
367   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dec->srcpad));
368   gst_buffer_copy_metadata (outbuf, inbuf,
369       GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_FLAGS);
371   gst_buffer_unref (*buf);
372   *buf = outbuf;
374   return GST_FLOW_OK;
377 static GstFlowReturn
378 gst_mpeg2dec_alloc_sized_buf (GstMpeg2dec * mpeg2dec, guint size,
379     GstBuffer ** obuf)
381   if (mpeg2dec->can_allocate_aligned
382       && mpeg2dec->decoded_width == mpeg2dec->width
383       && mpeg2dec->decoded_height == mpeg2dec->height) {
384     GstFlowReturn ret;
386     ret = gst_pad_alloc_buffer_and_set_caps (mpeg2dec->srcpad,
387         GST_BUFFER_OFFSET_NONE, size, GST_PAD_CAPS (mpeg2dec->srcpad), obuf);
388     if (ret != GST_FLOW_OK) {
389       return ret;
390     }
392     /* libmpeg2 needs 16 byte aligned buffers... test for this here
393      * and if it fails only a single time create our own buffers from
394      * there on below that are correctly aligned */
395     if (((uintptr_t) GST_BUFFER_DATA (*obuf)) % 16 == 0) {
396       GST_LOG_OBJECT (mpeg2dec, "return 16 byte aligned buffer");
397       return ret;
398     }
400     GST_DEBUG_OBJECT (mpeg2dec,
401         "can't get 16 byte aligned buffers, creating our own ones");
402     gst_buffer_unref (*obuf);
403     mpeg2dec->can_allocate_aligned = FALSE;
404   }
406   /* can't use gst_pad_alloc_buffer() here because the output buffer will
407    * either be cropped later or be bigger than expected (for the alignment),
408    * and basetransform-based elements will complain about the wrong unit size
409    * when not operating in passthrough mode */
410   *obuf = gst_buffer_new_and_alloc (size + 15);
411   GST_BUFFER_DATA (*obuf) = (guint8 *) ALIGN_16 (GST_BUFFER_DATA (*obuf));
412   GST_BUFFER_SIZE (*obuf) = size;
413   gst_buffer_set_caps (*obuf, GST_PAD_CAPS (mpeg2dec->srcpad));
415   return GST_FLOW_OK;
418 static GstFlowReturn
419 gst_mpeg2dec_alloc_buffer (GstMpeg2dec * mpeg2dec, gint64 offset,
420     GstBuffer ** obuf)
422   GstBuffer *outbuf = NULL;
423   guint8 *buf[3];
424   GstFlowReturn ret = GST_FLOW_OK;
426   ret = gst_mpeg2dec_alloc_sized_buf (mpeg2dec, mpeg2dec->size, &outbuf);
427   if (ret != GST_FLOW_OK)
428     goto no_buffer;
430   buf[0] = GST_BUFFER_DATA (outbuf);
431   buf[1] = buf[0] + mpeg2dec->u_offs;
432   buf[2] = buf[0] + mpeg2dec->v_offs;
434   GST_DEBUG_OBJECT (mpeg2dec, "set_buf: %p %p %p, outbuf %p",
435       buf[0], buf[1], buf[2], outbuf);
437   mpeg2_set_buf (mpeg2dec->decoder, buf, outbuf);
439   /* we store the original byteoffset of this picture in the stream here
440    * because we need it for indexing */
441   GST_BUFFER_OFFSET (outbuf) = offset;
443   *obuf = outbuf;
444   return ret;
446   /* ERRORS */
447 no_buffer:
448   {
449     if (ret != GST_FLOW_WRONG_STATE && ret != GST_FLOW_UNEXPECTED &&
450         ret != GST_FLOW_NOT_LINKED) {
451       GST_ELEMENT_ERROR (mpeg2dec, RESOURCE, FAILED, (NULL),
452           ("Failed to allocate memory for buffer, reason %s",
453               gst_flow_get_name (ret)));
454     }
455     GST_DEBUG_OBJECT (mpeg2dec, "no output buffer, reason %s",
456         gst_flow_get_name (ret));
457     mpeg2_set_buf (mpeg2dec->decoder, mpeg2dec->dummybuf, NULL);
458     *obuf = NULL;
459     return ret;
460   }
463 static gboolean
464 gst_mpeg2dec_negotiate_format (GstMpeg2dec * mpeg2dec)
466   GstCaps *caps;
467   guint32 fourcc;
468   const mpeg2_info_t *info;
469   const mpeg2_sequence_t *sequence;
471   info = mpeg2_info (mpeg2dec->decoder);
472   sequence = info->sequence;
474   if (sequence->width != sequence->chroma_width &&
475       sequence->height != sequence->chroma_height) {
476     mpeg2dec->format = GST_VIDEO_FORMAT_I420;
477   } else if ((sequence->width == sequence->chroma_width &&
478           sequence->height != sequence->chroma_height) ||
479       (sequence->width != sequence->chroma_width &&
480           sequence->height == sequence->chroma_height)) {
481     mpeg2dec->format = GST_VIDEO_FORMAT_Y42B;
482   } else {
483     mpeg2dec->format = GST_VIDEO_FORMAT_Y444;
484   }
486   fourcc = gst_video_format_to_fourcc (mpeg2dec->format);
487   mpeg2dec->size = gst_video_format_get_size (mpeg2dec->format,
488       mpeg2dec->decoded_width, mpeg2dec->decoded_height);
489   mpeg2dec->u_offs = gst_video_format_get_component_offset (mpeg2dec->format, 1,
490       mpeg2dec->decoded_width, mpeg2dec->decoded_height);
491   mpeg2dec->v_offs = gst_video_format_get_component_offset (mpeg2dec->format, 2,
492       mpeg2dec->decoded_width, mpeg2dec->decoded_height);
494   if (mpeg2dec->pixel_width == 0 || mpeg2dec->pixel_height == 0) {
495     GValue par = { 0, };
496     GValue dar = { 0, };
497     GValue dimensions = { 0, };
499     /* assume display aspect ratio (DAR) of 4:3 */
500     g_value_init (&dar, GST_TYPE_FRACTION);
501     gst_value_set_fraction (&dar, 4, 3);
502     g_value_init (&dimensions, GST_TYPE_FRACTION);
503     gst_value_set_fraction (&dimensions, mpeg2dec->height, mpeg2dec->width);
505     g_value_init (&par, GST_TYPE_FRACTION);
506     if (!gst_value_fraction_multiply (&par, &dar, &dimensions)) {
507       gst_value_set_fraction (&dimensions, 1, 1);
508     }
510     mpeg2dec->pixel_width = gst_value_get_fraction_numerator (&par);
511     mpeg2dec->pixel_height = gst_value_get_fraction_denominator (&par);
513     GST_WARNING_OBJECT (mpeg2dec, "Unknown pixel-aspect-ratio, assuming %d:%d",
514         mpeg2dec->pixel_width, mpeg2dec->pixel_height);
516     g_value_unset (&par);
517     g_value_unset (&dar);
518     g_value_unset (&dimensions);
519   }
521   caps = gst_caps_new_simple ("video/x-raw-yuv",
522       "format", GST_TYPE_FOURCC, fourcc,
523       "width", G_TYPE_INT, mpeg2dec->width,
524       "height", G_TYPE_INT, mpeg2dec->height,
525       "pixel-aspect-ratio", GST_TYPE_FRACTION, mpeg2dec->pixel_width,
526       mpeg2dec->pixel_height,
527       "framerate", GST_TYPE_FRACTION, mpeg2dec->fps_n, mpeg2dec->fps_d,
528       "interlaced", G_TYPE_BOOLEAN, mpeg2dec->interlaced, NULL);
530   gst_pad_set_caps (mpeg2dec->srcpad, caps);
531   gst_caps_unref (caps);
533   return TRUE;
536 static void
537 init_dummybuf (GstMpeg2dec * mpeg2dec)
539   g_free (mpeg2dec->dummybuf[3]);
541   /* libmpeg2 needs 16 byte aligned buffers... care for this here */
542   mpeg2dec->dummybuf[3] = g_malloc0 (mpeg2dec->size + 15);
543   mpeg2dec->dummybuf[0] = ALIGN_16 (mpeg2dec->dummybuf[3]);
544   mpeg2dec->dummybuf[1] = mpeg2dec->dummybuf[0] + mpeg2dec->u_offs;
545   mpeg2dec->dummybuf[2] = mpeg2dec->dummybuf[0] + mpeg2dec->v_offs;
548 static GstFlowReturn
549 handle_sequence (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info)
551   GstFlowReturn ret = GST_FLOW_OK;
553   if (info->sequence->frame_period == 0) {
554     GST_WARNING_OBJECT (mpeg2dec, "Frame period is 0!");
555     ret = GST_FLOW_ERROR;
556     goto done;
557   }
559   mpeg2dec->width = info->sequence->picture_width;
560   mpeg2dec->height = info->sequence->picture_height;
561   mpeg2dec->decoded_width = info->sequence->width;
562   mpeg2dec->decoded_height = info->sequence->height;
563   mpeg2dec->total_frames = 0;
565   /* don't take the sequence PAR if we already have one from the sink caps */
566   if (!mpeg2dec->have_par) {
567     mpeg2dec->pixel_width = info->sequence->pixel_width;
568     mpeg2dec->pixel_height = info->sequence->pixel_height;
569   }
571   /* mpeg2 video can only be from 16x16 to 4096x4096. Everything
572    * else is a corrupted files */
573   if (mpeg2dec->width > 4096 || mpeg2dec->width < 16 ||
574       mpeg2dec->height > 4096 || mpeg2dec->height < 16) {
575     GST_ERROR_OBJECT (mpeg2dec, "Invalid frame dimensions: %d x %d",
576         mpeg2dec->width, mpeg2dec->height);
577     return GST_FLOW_ERROR;
578   }
580   /* set framerate */
581   mpeg2dec->fps_n = 27000000;
582   mpeg2dec->fps_d = info->sequence->frame_period;
583   mpeg2dec->frame_period = info->sequence->frame_period * GST_USECOND / 27;
585   mpeg2dec->interlaced =
586       !(info->sequence->flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE);
588   GST_DEBUG_OBJECT (mpeg2dec,
589       "sequence flags: %d, frame period: %d (%g), frame rate: %d/%d",
590       info->sequence->flags, info->sequence->frame_period,
591       (double) (mpeg2dec->frame_period) / GST_SECOND, mpeg2dec->fps_n,
592       mpeg2dec->fps_d);
593   GST_DEBUG_OBJECT (mpeg2dec, "profile: %02x, colour_primaries: %d",
594       info->sequence->profile_level_id, info->sequence->colour_primaries);
595   GST_DEBUG_OBJECT (mpeg2dec, "transfer chars: %d, matrix coef: %d",
596       info->sequence->transfer_characteristics,
597       info->sequence->matrix_coefficients);
598   GST_DEBUG_OBJECT (mpeg2dec,
599       "FLAGS: CONSTRAINED_PARAMETERS:%d, PROGRESSIVE_SEQUENCE:%d",
600       info->sequence->flags & SEQ_FLAG_CONSTRAINED_PARAMETERS,
601       info->sequence->flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE);
602   GST_DEBUG_OBJECT (mpeg2dec, "FLAGS: LOW_DELAY:%d, COLOUR_DESCRIPTION:%d",
603       info->sequence->flags & SEQ_FLAG_LOW_DELAY,
604       info->sequence->flags & SEQ_FLAG_COLOUR_DESCRIPTION);
606   if (!gst_mpeg2dec_negotiate_format (mpeg2dec))
607     goto negotiate_failed;
609   mpeg2_custom_fbuf (mpeg2dec->decoder, 1);
611   init_dummybuf (mpeg2dec);
613   /* Pump in some null buffers, because otherwise libmpeg2 doesn't
614    * initialise the discard_fbuf->id */
615   mpeg2_set_buf (mpeg2dec->decoder, mpeg2dec->dummybuf, NULL);
616   mpeg2_set_buf (mpeg2dec->decoder, mpeg2dec->dummybuf, NULL);
617   mpeg2_set_buf (mpeg2dec->decoder, mpeg2dec->dummybuf, NULL);
619   mpeg2dec->need_sequence = FALSE;
621 done:
622   return ret;
624 negotiate_failed:
625   {
626     GST_ELEMENT_ERROR (mpeg2dec, CORE, NEGOTIATION, (NULL), (NULL));
627     ret = GST_FLOW_NOT_NEGOTIATED;
628     goto done;
629   }
632 static void
633 clear_buffers (GstMpeg2dec * mpeg2dec)
635   gint i;
636   GstBuffer **bufpen;
638   for (i = 0; i < 4; i++) {
639     bufpen = &mpeg2dec->ip_buffers[i];
640     if (*bufpen)
641       gst_buffer_unref (*bufpen);
642     *bufpen = NULL;
643   }
644   bufpen = &mpeg2dec->b_buffer;
645   if (*bufpen)
646     gst_buffer_unref (*bufpen);
647   *bufpen = NULL;
650 static void
651 clear_queued (GstMpeg2dec * mpeg2dec)
653   g_list_foreach (mpeg2dec->queued, (GFunc) gst_mini_object_unref, NULL);
654   g_list_free (mpeg2dec->queued);
655   mpeg2dec->queued = NULL;
658 static GstFlowReturn
659 flush_queued (GstMpeg2dec * mpeg2dec)
661   GstFlowReturn res = GST_FLOW_OK;
663   while (mpeg2dec->queued) {
664     GstBuffer *buf = GST_BUFFER_CAST (mpeg2dec->queued->data);
666     GST_LOG_OBJECT (mpeg2dec, "pushing buffer %p, timestamp %"
667         GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT, buf,
668         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
669         GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
671     /* iterate ouput queue an push downstream */
672     res = gst_pad_push (mpeg2dec->srcpad, buf);
674     mpeg2dec->queued = g_list_delete_link (mpeg2dec->queued, mpeg2dec->queued);
675   }
676   return res;
679 static GstFlowReturn
680 handle_picture (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info)
682   gboolean key_frame = FALSE;
683   GstBuffer *outbuf, **bufpen;
684   GstFlowReturn ret;
685   gint type;
687   ret = gst_mpeg2dec_alloc_buffer (mpeg2dec, mpeg2dec->offset, &outbuf);
688   if (ret != GST_FLOW_OK)
689     goto no_buffer;
691   if (info->current_picture) {
692     type = info->current_picture->flags & PIC_MASK_CODING_TYPE;
693   } else {
694     type = 0;
695   }
697   GST_DEBUG_OBJECT (mpeg2dec, "handle picture type %d", type);
699   key_frame = type == PIC_FLAG_CODING_TYPE_I;
701   switch (type) {
702     case PIC_FLAG_CODING_TYPE_I:
703       mpeg2_skip (mpeg2dec->decoder, 0);
704       if (mpeg2dec->segment.rate < 0.0) {
705         /* negative rate, flush the queued pictures in reverse */
706         GST_DEBUG_OBJECT (mpeg2dec, "flushing queued buffers");
707         flush_queued (mpeg2dec);
708       }
709     case PIC_FLAG_CODING_TYPE_P:
710       bufpen = &mpeg2dec->ip_buffers[mpeg2dec->ip_bufpos];
711       GST_DEBUG_OBJECT (mpeg2dec, "I/P unref %p, ref %p", *bufpen, outbuf);
712       if (*bufpen)
713         gst_buffer_unref (*bufpen);
714       *bufpen = outbuf;
715       mpeg2dec->ip_bufpos = (mpeg2dec->ip_bufpos + 1) & 3;
716       break;
717     case PIC_FLAG_CODING_TYPE_B:
718       bufpen = &mpeg2dec->b_buffer;
719       GST_DEBUG_OBJECT (mpeg2dec, "B unref %p, ref %p", *bufpen, outbuf);
720       if (*bufpen)
721         gst_buffer_unref (*bufpen);
722       *bufpen = outbuf;
723       break;
724     default:
725       break;
726   }
728   GST_DEBUG_OBJECT (mpeg2dec, "picture %s, outbuf %p, offset %"
729       G_GINT64_FORMAT,
730       key_frame ? ", kf," : "    ", outbuf, GST_BUFFER_OFFSET (outbuf)
731       );
733   if (mpeg2dec->discont_state == MPEG2DEC_DISC_NEW_PICTURE && key_frame) {
734     mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_KEYFRAME;
735   }
737   return ret;
739 no_buffer:
740   {
741     return ret;
742   }
745 /* try to clip the buffer to the segment boundaries */
746 static gboolean
747 clip_buffer (GstMpeg2dec * dec, GstBuffer * buf)
749   gboolean res = TRUE;
750   GstClockTime in_ts, in_dur, stop;
751   gint64 cstart, cstop;
753   in_ts = GST_BUFFER_TIMESTAMP (buf);
754   in_dur = GST_BUFFER_DURATION (buf);
756   GST_LOG_OBJECT (dec,
757       "timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT,
758       GST_TIME_ARGS (in_ts), GST_TIME_ARGS (in_dur));
760   /* can't clip without TIME segment */
761   if (dec->segment.format != GST_FORMAT_TIME)
762     goto beach;
764   /* we need a start time */
765   if (!GST_CLOCK_TIME_IS_VALID (in_ts))
766     goto beach;
768   /* generate valid stop, if duration unknown, we have unknown stop */
769   stop =
770       GST_CLOCK_TIME_IS_VALID (in_dur) ? (in_ts + in_dur) : GST_CLOCK_TIME_NONE;
772   /* now clip */
773   if (!(res = gst_segment_clip (&dec->segment, GST_FORMAT_TIME,
774               in_ts, stop, &cstart, &cstop)))
775     goto beach;
777   /* update timestamp and possibly duration if the clipped stop time is
778    * valid */
779   GST_BUFFER_TIMESTAMP (buf) = cstart;
780   if (GST_CLOCK_TIME_IS_VALID (cstop))
781     GST_BUFFER_DURATION (buf) = cstop - cstart;
783 beach:
784   GST_LOG_OBJECT (dec, "%sdropping", (res ? "not " : ""));
785   return res;
788 static GstFlowReturn
789 handle_slice (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info)
791   GstBuffer *outbuf = NULL;
792   GstFlowReturn ret = GST_FLOW_OK;
793   const mpeg2_picture_t *picture;
794   gboolean key_frame = FALSE;
795   GstClockTime time;
797   GST_DEBUG_OBJECT (mpeg2dec, "picture slice/end %p %p %p %p",
798       info->display_fbuf,
799       info->display_picture, info->current_picture,
800       (info->display_fbuf ? info->display_fbuf->id : NULL));
802   if (!info->display_fbuf || !info->display_fbuf->id)
803     goto no_display;
805   outbuf = GST_BUFFER (info->display_fbuf->id);
807   picture = info->display_picture;
809   key_frame = (picture->flags & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_I;
811   GST_DEBUG_OBJECT (mpeg2dec, "picture flags: %d, type: %d, keyframe: %d",
812       picture->flags, picture->flags & PIC_MASK_CODING_TYPE, key_frame);
814   if (key_frame) {
815     GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
816     mpeg2_skip (mpeg2dec->decoder, 0);
817   } else {
818     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
819   }
821   if (mpeg2dec->discont_state == MPEG2DEC_DISC_NEW_KEYFRAME && key_frame)
822     mpeg2dec->discont_state = MPEG2DEC_DISC_NONE;
824   time = GST_CLOCK_TIME_NONE;
826 #if MPEG2_RELEASE < MPEG2_VERSION(0,4,0)
827   if (picture->flags & PIC_FLAG_PTS) {
828     time = MPEG_TIME_TO_GST_TIME (picture->pts);
829     GST_DEBUG_OBJECT (mpeg2dec, "picture pts %" G_GUINT64_FORMAT
830         ", time %" GST_TIME_FORMAT, picture->pts, GST_TIME_ARGS (time));
831   }
832 #else
833   if (picture->flags & PIC_FLAG_TAGS) {
834     guint64 pts = (((guint64) picture->tag2) << 32) | picture->tag;
836     time = MPEG_TIME_TO_GST_TIME (pts);
837     GST_DEBUG_OBJECT (mpeg2dec, "picture tags %" G_GUINT64_FORMAT
838         ", time %" GST_TIME_FORMAT, pts, GST_TIME_ARGS (time));
839   }
840 #endif
842   if (time == GST_CLOCK_TIME_NONE) {
843     time = mpeg2dec->next_time;
844     GST_DEBUG_OBJECT (mpeg2dec, "picture didn't have pts");
845   } else {
846     GST_DEBUG_OBJECT (mpeg2dec,
847         "picture had pts %" GST_TIME_FORMAT ", we had %"
848         GST_TIME_FORMAT, GST_TIME_ARGS (time),
849         GST_TIME_ARGS (mpeg2dec->next_time));
850     mpeg2dec->next_time = time;
851   }
852   GST_BUFFER_TIMESTAMP (outbuf) = time;
854   /* TODO set correct offset here based on frame number */
855   if (info->display_picture_2nd) {
856     GST_BUFFER_DURATION (outbuf) = (picture->nb_fields +
857         info->display_picture_2nd->nb_fields) * mpeg2dec->frame_period / 2;
858   } else {
859     GST_BUFFER_DURATION (outbuf) =
860         picture->nb_fields * mpeg2dec->frame_period / 2;
861   }
862   mpeg2dec->next_time += GST_BUFFER_DURATION (outbuf);
864   if (picture->flags & PIC_FLAG_TOP_FIELD_FIRST)
865     GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_TFF);
867 #if MPEG2_RELEASE >= MPEG2_VERSION(0,5,0)
868   /* repeat field introduced in 0.5.0 */
869   if (picture->flags & PIC_FLAG_REPEAT_FIRST_FIELD)
870     GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_RFF);
871 #endif
873   GST_DEBUG_OBJECT (mpeg2dec,
874       "picture: %s %s %s %s %s fields:%d off:%" G_GINT64_FORMAT " ts:%"
875       GST_TIME_FORMAT,
876       (picture->flags & PIC_FLAG_PROGRESSIVE_FRAME ? "prog" : "    "),
877       (picture->flags & PIC_FLAG_TOP_FIELD_FIRST ? "tff" : "   "),
878 #if MPEG2_RELEASE >= MPEG2_VERSION(0,5,0)
879       (picture->flags & PIC_FLAG_REPEAT_FIRST_FIELD ? "rff" : "   "),
880 #else
881       "unknown rff",
882 #endif
883       (picture->flags & PIC_FLAG_SKIP ? "skip" : "    "),
884       (picture->flags & PIC_FLAG_COMPOSITE_DISPLAY ? "composite" : "         "),
885       picture->nb_fields, GST_BUFFER_OFFSET (outbuf),
886       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
888   if (mpeg2dec->index) {
889     gst_index_add_association (mpeg2dec->index, mpeg2dec->index_id,
890         (key_frame ? GST_ASSOCIATION_FLAG_KEY_UNIT :
891             GST_ASSOCIATION_FLAG_DELTA_UNIT),
892         GST_FORMAT_BYTES, GST_BUFFER_OFFSET (outbuf),
893         GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (outbuf), 0);
894   }
896   if (picture->flags & PIC_FLAG_SKIP)
897     goto skip;
899   if (mpeg2dec->discont_state != MPEG2DEC_DISC_NONE)
900     goto drop;
902   /* check for clipping */
903   if (!clip_buffer (mpeg2dec, outbuf))
904     goto clipped;
906   if (GST_CLOCK_TIME_IS_VALID (time)) {
907     gboolean need_skip;
908     GstClockTime qostime;
910     /* qos needs to be done on running time */
911     qostime = gst_segment_to_running_time (&mpeg2dec->segment, GST_FORMAT_TIME,
912         time);
914     GST_OBJECT_LOCK (mpeg2dec);
915     /* check for QoS, don't perform the last steps of getting and
916      * pushing the buffers that are known to be late. */
917     /* FIXME, we can also entirely skip decoding if the next valid buffer is
918      * known to be after a keyframe (using the granule_shift) */
919     need_skip = mpeg2dec->earliest_time != -1
920         && qostime <= mpeg2dec->earliest_time;
921     GST_OBJECT_UNLOCK (mpeg2dec);
923     if (need_skip) {
924       GstMessage *qos_msg;
925       guint64 stream_time;
926       gint64 jitter;
928       mpeg2dec->dropped++;
930       stream_time =
931           gst_segment_to_stream_time (&mpeg2dec->segment, GST_FORMAT_TIME,
932           time);
933       jitter = GST_CLOCK_DIFF (qostime, mpeg2dec->earliest_time);
935       qos_msg =
936           gst_message_new_qos (GST_OBJECT_CAST (mpeg2dec), FALSE, qostime,
937           stream_time, time, GST_BUFFER_DURATION (outbuf));
938       gst_message_set_qos_values (qos_msg, jitter, mpeg2dec->proportion,
939           1000000);
940       gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS,
941           mpeg2dec->processed, mpeg2dec->dropped);
942       gst_element_post_message (GST_ELEMENT_CAST (mpeg2dec), qos_msg);
944       goto dropping_qos;
945     }
946   }
948   mpeg2dec->processed++;
950   /* ref before pushing it out, so we still have the ref in our
951    * array of buffers */
952   gst_buffer_ref (outbuf);
954   /* do cropping if the target region is smaller than the input one */
955   if (mpeg2dec->decoded_width != mpeg2dec->width ||
956       mpeg2dec->decoded_height != mpeg2dec->height) {
957     GST_DEBUG_OBJECT (mpeg2dec, "cropping buffer");
958     ret = gst_mpeg2dec_crop_buffer (mpeg2dec, &outbuf);
959     if (ret != GST_FLOW_OK)
960       goto done;
961   }
963   if (mpeg2dec->segment.rate >= 0.0) {
964     /* forward: push right away */
965     GST_LOG_OBJECT (mpeg2dec, "pushing buffer %p, timestamp %"
966         GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
967         outbuf,
968         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
969         GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
970     GST_LOG_OBJECT (mpeg2dec, "... with flags %x", GST_BUFFER_FLAGS (outbuf));
972     ret = gst_pad_push (mpeg2dec->srcpad, outbuf);
973     GST_DEBUG_OBJECT (mpeg2dec, "pushed with result %s",
974         gst_flow_get_name (ret));
975   } else {
976     /* reverse: queue, we'll push in reverse when we receive the next (previous)
977      * keyframe. */
978     GST_DEBUG_OBJECT (mpeg2dec, "queued frame");
979     mpeg2dec->queued = g_list_prepend (mpeg2dec->queued, outbuf);
980     ret = GST_FLOW_OK;
981   }
983 done:
985   return ret;
987   /* special cases */
988 no_display:
989   {
990     GST_DEBUG_OBJECT (mpeg2dec, "no picture to display");
991     return GST_FLOW_OK;
992   }
993 skip:
994   {
995     GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer because of skip flag");
996     return GST_FLOW_OK;
997   }
998 drop:
999   {
1000     GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer, discont state %d",
1001         mpeg2dec->discont_state);
1002     return GST_FLOW_OK;
1003   }
1004 clipped:
1005   {
1006     GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer, clipped");
1007     return GST_FLOW_OK;
1008   }
1009 dropping_qos:
1010   {
1011     GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer because of QoS");
1012     return GST_FLOW_OK;
1013   }
1016 #if 0
1017 static void
1018 update_streaminfo (GstMpeg2dec * mpeg2dec)
1020   GstCaps *caps;
1021   GstProps *props;
1022   GstPropsEntry *entry;
1023   const mpeg2_info_t *info;
1025   info = mpeg2_info (mpeg2dec->decoder);
1027   props = gst_props_empty_new ();
1029   entry =
1030       gst_props_entry_new ("framerate",
1031       G_TYPE_DOUBLE (GST_SECOND / (float) mpeg2dec->frame_period));
1032   gst_props_add_entry (props, entry);
1033   entry =
1034       gst_props_entry_new ("bitrate",
1035       G_TYPE_INT (info->sequence->byte_rate * 8));
1036   gst_props_add_entry (props, entry);
1038   caps = gst_caps_new ("mpeg2dec_streaminfo",
1039       "application/x-gst-streaminfo", props);
1041   gst_caps_replace_sink (&mpeg2dec->streaminfo, caps);
1042   g_object_notify (G_OBJECT (mpeg2dec), "streaminfo");
1044 #endif
1046 static GstFlowReturn
1047 gst_mpeg2dec_chain (GstPad * pad, GstBuffer * buf)
1049   GstMpeg2dec *mpeg2dec;
1050   guint32 size;
1051   guint8 *data, *end;
1052   GstClockTime pts;
1053   const mpeg2_info_t *info;
1054   mpeg2_state_t state;
1055   gboolean done = FALSE;
1056   GstFlowReturn ret = GST_FLOW_OK;
1058   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1060   size = GST_BUFFER_SIZE (buf);
1061   data = GST_BUFFER_DATA (buf);
1062   pts = GST_BUFFER_TIMESTAMP (buf);
1064   if (GST_BUFFER_IS_DISCONT (buf)) {
1065     GST_LOG_OBJECT (mpeg2dec, "DISCONT, reset decoder");
1066     /* when we receive a discont, reset our state as to not create too much
1067      * distortion in the picture due to missing packets */
1068     mpeg2_reset (mpeg2dec->decoder, 0);
1069     mpeg2_skip (mpeg2dec->decoder, 1);
1070     mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
1071   }
1073   GST_LOG_OBJECT (mpeg2dec, "received buffer, timestamp %"
1074       GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
1075       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1076       GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1078   info = mpeg2dec->info;
1079   end = data + size;
1081   mpeg2dec->offset = GST_BUFFER_OFFSET (buf);
1083   if (pts != GST_CLOCK_TIME_NONE) {
1084     gint64 mpeg_pts = GST_TIME_TO_MPEG_TIME (pts);
1086     GST_DEBUG_OBJECT (mpeg2dec,
1087         "have pts: %" G_GINT64_FORMAT " (%" GST_TIME_FORMAT ")",
1088         mpeg_pts, GST_TIME_ARGS (MPEG_TIME_TO_GST_TIME (mpeg_pts)));
1090 #if MPEG2_RELEASE >= MPEG2_VERSION(0,4,0)
1091     mpeg2_tag_picture (mpeg2dec->decoder, mpeg_pts & 0xffffffff,
1092         mpeg_pts >> 32);
1093 #else
1094     mpeg2_pts (mpeg2dec->decoder, mpeg_pts);
1095 #endif
1096   } else {
1097     GST_LOG ("no pts");
1098   }
1100   GST_LOG_OBJECT (mpeg2dec, "calling mpeg2_buffer");
1101   mpeg2_buffer (mpeg2dec->decoder, data, end);
1102   GST_LOG_OBJECT (mpeg2dec, "calling mpeg2_buffer done");
1104   while (!done) {
1105     GST_LOG_OBJECT (mpeg2dec, "calling parse");
1106     state = mpeg2_parse (mpeg2dec->decoder);
1107     GST_DEBUG_OBJECT (mpeg2dec, "parse state %d", state);
1109     switch (state) {
1110 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
1111       case STATE_SEQUENCE_MODIFIED:
1112         GST_DEBUG_OBJECT (mpeg2dec, "sequence modified");
1113         /* fall through */
1114 #endif
1115       case STATE_SEQUENCE:
1116         ret = handle_sequence (mpeg2dec, info);
1117         /* if there is an error handling the sequence
1118          * reset the decoder, maybe something more elegant
1119          * could be done.
1120          */
1121         if (ret == GST_FLOW_ERROR) {
1122           mpeg2dec->error_count++;
1123           GST_WARNING_OBJECT (mpeg2dec, "Decoding error #%d",
1124               mpeg2dec->error_count);
1125           if (mpeg2dec->error_count >= WARN_THRESHOLD && WARN_THRESHOLD > 0) {
1126             GST_ELEMENT_WARNING (mpeg2dec, STREAM, DECODE,
1127                 ("%d consecutive decoding errors", mpeg2dec->error_count),
1128                 (NULL));
1129           }
1130           mpeg2_reset (mpeg2dec->decoder, 0);
1131           mpeg2_skip (mpeg2dec->decoder, 1);
1132           mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
1134           goto exit;
1135         }
1136         break;
1137       case STATE_SEQUENCE_REPEATED:
1138         GST_DEBUG_OBJECT (mpeg2dec, "sequence repeated");
1139         break;
1140       case STATE_GOP:
1141         GST_DEBUG_OBJECT (mpeg2dec, "gop");
1142         break;
1143       case STATE_PICTURE:
1144         ret = handle_picture (mpeg2dec, info);
1145         break;
1146       case STATE_SLICE_1ST:
1147         GST_LOG_OBJECT (mpeg2dec, "1st slice of frame encountered");
1148         break;
1149       case STATE_PICTURE_2ND:
1150         GST_LOG_OBJECT (mpeg2dec,
1151             "Second picture header encountered. Decoding 2nd field");
1152         break;
1153 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 4, 0)
1154       case STATE_INVALID_END:
1155         GST_DEBUG_OBJECT (mpeg2dec, "invalid end");
1156 #endif
1157       case STATE_END:
1158         GST_DEBUG_OBJECT (mpeg2dec, "end");
1159         mpeg2dec->need_sequence = TRUE;
1160       case STATE_SLICE:
1161         ret = handle_slice (mpeg2dec, info);
1162         break;
1163       case STATE_BUFFER:
1164         done = TRUE;
1165         break;
1166         /* error */
1167       case STATE_INVALID:
1168         /* FIXME: at some point we should probably send newsegment events to
1169          * let downstream know that parts of the stream are missing */
1170         mpeg2dec->error_count++;
1171         GST_WARNING_OBJECT (mpeg2dec, "Decoding error #%d",
1172             mpeg2dec->error_count);
1173         if (mpeg2dec->error_count >= WARN_THRESHOLD && WARN_THRESHOLD > 0) {
1174           GST_ELEMENT_WARNING (mpeg2dec, STREAM, DECODE,
1175               ("%d consecutive decoding errors", mpeg2dec->error_count),
1176               (NULL));
1177         }
1178         continue;
1179       default:
1180         GST_ERROR_OBJECT (mpeg2dec, "Unknown libmpeg2 state %d, FIXME", state);
1181         goto exit;
1182     }
1184     mpeg2dec->error_count = 0;
1186     /*
1187      * FIXME: should pass more information such as state the user data is from
1188      */
1189 #ifdef enable_user_data
1190     if (info->user_data_len > 0) {
1191       GstBuffer *udbuf = gst_buffer_new_and_alloc (info->user_data_len);
1193       memcpy (GST_BUFFER_DATA (udbuf), info->user_data, info->user_data_len);
1195       gst_pad_push (mpeg2dec->userdatapad, udbuf);
1196     }
1197 #endif
1199     if (ret != GST_FLOW_OK) {
1200       GST_DEBUG_OBJECT (mpeg2dec, "exit loop, reason %s",
1201           gst_flow_get_name (ret));
1202       break;
1203     }
1204   }
1205 done:
1206   gst_buffer_unref (buf);
1207   return ret;
1209   /* errors */
1210 exit:
1211   {
1212     ret = GST_FLOW_OK;
1213     goto done;
1214   }
1217 static gboolean
1218 gst_mpeg2dec_sink_event (GstPad * pad, GstEvent * event)
1220   GstMpeg2dec *mpeg2dec;
1221   gboolean ret = TRUE;
1223   mpeg2dec = GST_MPEG2DEC (gst_pad_get_parent (pad));
1225   GST_DEBUG_OBJECT (mpeg2dec, "Got %s event on sink pad",
1226       GST_EVENT_TYPE_NAME (event));
1228   switch (GST_EVENT_TYPE (event)) {
1229     case GST_EVENT_NEWSEGMENT:
1230     {
1231       gboolean update;
1232       GstFormat format;
1233       gdouble rate, arate;
1234       gint64 start, stop, time;
1236       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1237           &start, &stop, &time);
1239       /* we need TIME and a positive rate */
1240       if (format != GST_FORMAT_TIME)
1241         goto newseg_wrong_format;
1243       /* now configure the values */
1244       gst_segment_set_newsegment_full (&mpeg2dec->segment, update,
1245           rate, arate, format, start, stop, time);
1247       GST_DEBUG_OBJECT (mpeg2dec,
1248           "Pushing newseg rate %g, applied rate %g, format %d, start %"
1249           G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT ", pos %" G_GINT64_FORMAT,
1250           rate, arate, format, start, stop, time);
1252       ret = gst_pad_push_event (mpeg2dec->srcpad, event);
1253       break;
1254     }
1255     case GST_EVENT_FLUSH_START:
1256       ret = gst_pad_push_event (mpeg2dec->srcpad, event);
1257       break;
1258     case GST_EVENT_FLUSH_STOP:
1259     {
1260       mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
1261       mpeg2dec->next_time = -1;;
1262       gst_mpeg2dec_qos_reset (mpeg2dec);
1263       mpeg2_reset (mpeg2dec->decoder, 0);
1264       mpeg2_skip (mpeg2dec->decoder, 1);
1265       clear_queued (mpeg2dec);
1266       ret = gst_pad_push_event (mpeg2dec->srcpad, event);
1267       break;
1268     }
1269     case GST_EVENT_EOS:
1270       if (mpeg2dec->index && mpeg2dec->closed) {
1271         gst_index_commit (mpeg2dec->index, mpeg2dec->index_id);
1272       }
1273       ret = gst_pad_push_event (mpeg2dec->srcpad, event);
1274       break;
1275     default:
1276       ret = gst_pad_push_event (mpeg2dec->srcpad, event);
1277       break;
1278   }
1280 done:
1281   gst_object_unref (mpeg2dec);
1283   return ret;
1285   /* ERRORS */
1286 newseg_wrong_format:
1287   {
1288     GST_DEBUG_OBJECT (mpeg2dec, "received non TIME newsegment");
1289     gst_event_unref (event);
1290     goto done;
1291   }
1294 static gboolean
1295 gst_mpeg2dec_setcaps (GstPad * pad, GstCaps * caps)
1297   GstMpeg2dec *mpeg2dec;
1298   GstStructure *s;
1300   mpeg2dec = GST_MPEG2DEC (gst_pad_get_parent (pad));
1302   s = gst_caps_get_structure (caps, 0);
1304   /* parse the par, this overrides the encoded par */
1305   mpeg2dec->have_par = gst_structure_get_fraction (s, "pixel-aspect-ratio",
1306       &mpeg2dec->pixel_width, &mpeg2dec->pixel_height);
1308   gst_object_unref (mpeg2dec);
1310   return TRUE;
1313 static gboolean
1314 gst_mpeg2dec_sink_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
1315     GstFormat * dest_format, gint64 * dest_value)
1317   gboolean res = TRUE;
1318   GstMpeg2dec *mpeg2dec;
1319   const mpeg2_info_t *info;
1321   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1323   if (mpeg2dec->decoder == NULL)
1324     return FALSE;
1326   if (src_format == *dest_format) {
1327     *dest_value = src_value;
1328     return TRUE;
1329   }
1331   info = mpeg2_info (mpeg2dec->decoder);
1333   switch (src_format) {
1334     case GST_FORMAT_BYTES:
1335       switch (*dest_format) {
1336         case GST_FORMAT_TIME:
1337           if (info->sequence && info->sequence->byte_rate) {
1338             *dest_value =
1339                 gst_util_uint64_scale (GST_SECOND, src_value,
1340                 info->sequence->byte_rate);
1341             GST_WARNING_OBJECT (mpeg2dec, "dest_value:%" GST_TIME_FORMAT,
1342                 GST_TIME_ARGS (*dest_value));
1343             break;
1344           } else if (info->sequence)
1345             GST_WARNING_OBJECT (mpeg2dec,
1346                 "Cannot convert from BYTES to TIME since we don't know the bitrate at this point.");
1347         default:
1348           res = FALSE;
1349       }
1350       break;
1351     case GST_FORMAT_TIME:
1352       switch (*dest_format) {
1353         case GST_FORMAT_BYTES:
1354           if (info->sequence && info->sequence->byte_rate) {
1355             *dest_value =
1356                 gst_util_uint64_scale_int (src_value, info->sequence->byte_rate,
1357                 GST_SECOND);
1358             break;
1359           } else if (info->sequence)
1360             GST_WARNING_OBJECT (mpeg2dec,
1361                 "Cannot convert from TIME to BYTES since we don't know the bitrate at this point.");
1362         default:
1363           res = FALSE;
1364       }
1365       break;
1366     default:
1367       res = FALSE;
1368   }
1369   return res;
1373 static gboolean
1374 gst_mpeg2dec_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
1375     GstFormat * dest_format, gint64 * dest_value)
1377   gboolean res = TRUE;
1378   GstMpeg2dec *mpeg2dec;
1379   const mpeg2_info_t *info;
1380   guint64 scale = 1;
1382   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1384   if (mpeg2dec->decoder == NULL)
1385     return FALSE;
1387   if (src_format == *dest_format) {
1388     *dest_value = src_value;
1389     return TRUE;
1390   }
1392   info = mpeg2_info (mpeg2dec->decoder);
1394   switch (src_format) {
1395     case GST_FORMAT_BYTES:
1396       switch (*dest_format) {
1397         case GST_FORMAT_TIME:
1398         default:
1399           res = FALSE;
1400       }
1401       break;
1402     case GST_FORMAT_TIME:
1403       switch (*dest_format) {
1404         case GST_FORMAT_BYTES:
1405           scale = 6 * (mpeg2dec->width * mpeg2dec->height >> 2);
1406         case GST_FORMAT_DEFAULT:
1407           if (info->sequence && mpeg2dec->frame_period) {
1408             *dest_value =
1409                 gst_util_uint64_scale_int (src_value, scale,
1410                 mpeg2dec->frame_period);
1411             break;
1412           }
1413         default:
1414           res = FALSE;
1415       }
1416       break;
1417     case GST_FORMAT_DEFAULT:
1418       switch (*dest_format) {
1419         case GST_FORMAT_TIME:
1420           *dest_value = src_value * mpeg2dec->frame_period;
1421           break;
1422         case GST_FORMAT_BYTES:
1423           *dest_value =
1424               src_value * 6 * ((mpeg2dec->width * mpeg2dec->height) >> 2);
1425           break;
1426         default:
1427           res = FALSE;
1428       }
1429       break;
1430     default:
1431       res = FALSE;
1432   }
1433   return res;
1436 static const GstQueryType *
1437 gst_mpeg2dec_get_src_query_types (GstPad * pad)
1439   static const GstQueryType types[] = {
1440     GST_QUERY_POSITION,
1441     GST_QUERY_DURATION,
1442     0
1443   };
1445   return types;
1448 static gboolean
1449 gst_mpeg2dec_src_query (GstPad * pad, GstQuery * query)
1451   gboolean res = TRUE;
1452   GstMpeg2dec *mpeg2dec;
1454   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1456   switch (GST_QUERY_TYPE (query)) {
1457     case GST_QUERY_POSITION:
1458     {
1459       GstFormat format;
1460       gint64 cur;
1462       /* First, we try to ask upstream, which might know better, especially in
1463        * the case of DVDs, with multiple chapter */
1464       if ((res = gst_pad_peer_query (mpeg2dec->sinkpad, query)))
1465         break;
1467       /* save requested format */
1468       gst_query_parse_position (query, &format, NULL);
1470       /* and convert to the requested format */
1471       if (!gst_mpeg2dec_src_convert (pad, GST_FORMAT_TIME,
1472               mpeg2dec->next_time, &format, &cur))
1473         goto error;
1475       cur = gst_segment_to_stream_time (&mpeg2dec->segment, format, cur);
1476       if (cur == -1)
1477         goto error;
1479       gst_query_set_position (query, format, cur);
1481       GST_LOG_OBJECT (mpeg2dec,
1482           "position query: we return %" G_GUINT64_FORMAT " (format %u)", cur,
1483           format);
1484       break;
1485     }
1486     case GST_QUERY_DURATION:
1487     {
1488       GstFormat format;
1489       GstFormat rformat;
1490       gint64 total, total_bytes;
1491       GstPad *peer;
1493       if ((peer = gst_pad_get_peer (mpeg2dec->sinkpad)) == NULL)
1494         goto error;
1496       /* save requested format */
1497       gst_query_parse_duration (query, &format, NULL);
1499       /* send to peer */
1500       if ((res = gst_pad_query (peer, query))) {
1501         gst_object_unref (peer);
1502         goto done;
1503       } else {
1504         GST_LOG_OBJECT (mpeg2dec, "query on peer pad failed, trying bytes");
1505       }
1507       /* query peer for total length in bytes */
1508       gst_query_set_duration (query, GST_FORMAT_BYTES, -1);
1510       if (!(res = gst_pad_query (peer, query))) {
1511         GST_LOG_OBJECT (mpeg2dec, "query on peer pad failed");
1512         gst_object_unref (peer);
1513         goto error;
1514       }
1515       gst_object_unref (peer);
1517       /* get the returned format */
1518       gst_query_parse_duration (query, &rformat, &total_bytes);
1519       GST_LOG_OBJECT (mpeg2dec,
1520           "peer pad returned total=%" G_GINT64_FORMAT " bytes", total_bytes);
1522       if (total_bytes != -1) {
1523         if (!gst_mpeg2dec_sink_convert (pad, GST_FORMAT_BYTES, total_bytes,
1524                 &format, &total))
1525           goto error;
1526       } else {
1527         total = -1;
1528       }
1530       gst_query_set_duration (query, format, total);
1532       GST_LOG_OBJECT (mpeg2dec,
1533           "position query: we return %" G_GUINT64_FORMAT " (format %u)", total,
1534           format);
1535       break;
1536     }
1537     default:
1538       res = gst_pad_query_default (pad, query);
1539       break;
1540   }
1541 done:
1542   return res;
1544 error:
1546   GST_DEBUG ("error handling query");
1547   return FALSE;
1551 #if 0
1552 static const GstEventMask *
1553 gst_mpeg2dec_get_event_masks (GstPad * pad)
1555   static const GstEventMask masks[] = {
1556     {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH},
1557     {GST_EVENT_NAVIGATION, GST_EVENT_FLAG_NONE},
1558     {0,}
1559   };
1561   return masks;
1563 #endif
1565 static gboolean
1566 index_seek (GstPad * pad, GstEvent * event)
1568   GstIndexEntry *entry;
1569   GstMpeg2dec *mpeg2dec;
1570   gdouble rate;
1571   GstFormat format;
1572   GstSeekFlags flags;
1573   GstSeekType cur_type, stop_type;
1574   gint64 cur, stop;
1576   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1578   gst_event_parse_seek (event, &rate, &format, &flags,
1579       &cur_type, &cur, &stop_type, &stop);
1581   entry = gst_index_get_assoc_entry (mpeg2dec->index, mpeg2dec->index_id,
1582       GST_INDEX_LOOKUP_BEFORE, GST_ASSOCIATION_FLAG_KEY_UNIT, format, cur);
1584   if ((entry) && gst_pad_is_linked (mpeg2dec->sinkpad)) {
1585     const GstFormat *peer_formats, *try_formats;
1587     /* since we know the exact byteoffset of the frame, make sure to seek on bytes first */
1588     const GstFormat try_all_formats[] = {
1589       GST_FORMAT_BYTES,
1590       GST_FORMAT_TIME,
1591       0
1592     };
1594     try_formats = try_all_formats;
1596 #if 0
1597     peer_formats = gst_pad_get_formats (GST_PAD_PEER (mpeg2dec->sinkpad));
1598 #else
1599     peer_formats = try_all_formats;     /* FIXE ME */
1600 #endif
1602     while (gst_formats_contains (peer_formats, *try_formats)) {
1603       gint64 value;
1605       if (gst_index_entry_assoc_map (entry, *try_formats, &value)) {
1606         GstEvent *seek_event;
1608         GST_DEBUG_OBJECT (mpeg2dec, "index %s %" G_GINT64_FORMAT
1609             " -> %s %" G_GINT64_FORMAT,
1610             gst_format_get_details (format)->nick,
1611             cur, gst_format_get_details (*try_formats)->nick, value);
1613         /* lookup succeeded, create the seek */
1614         seek_event =
1615             gst_event_new_seek (rate, *try_formats, flags, cur_type, value,
1616             stop_type, stop);
1617         /* do the seek */
1618         if (gst_pad_push_event (mpeg2dec->sinkpad, seek_event)) {
1619           /* seek worked, we're done, loop will exit */
1620 #if 0
1621           mpeg2dec->segment_start = GST_EVENT_SEEK_OFFSET (event);
1622 #endif
1623           return TRUE;
1624         }
1625       }
1626       try_formats++;
1627     }
1628   }
1629   return FALSE;
1632 static gboolean
1633 normal_seek (GstPad * pad, GstEvent * event)
1635   gdouble rate;
1636   GstFormat format, conv;
1637   GstSeekFlags flags;
1638   GstSeekType cur_type, stop_type;
1639   gint64 cur, stop;
1640   gint64 time_cur, bytes_cur;
1641   gint64 time_stop, bytes_stop;
1642   gboolean res;
1643   GstMpeg2dec *mpeg2dec;
1644   GstEvent *peer_event;
1646   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1648   GST_DEBUG ("normal seek");
1650   gst_event_parse_seek (event, &rate, &format, &flags,
1651       &cur_type, &cur, &stop_type, &stop);
1653   conv = GST_FORMAT_TIME;
1654   if (!gst_mpeg2dec_src_convert (pad, format, cur, &conv, &time_cur))
1655     goto convert_failed;
1656   if (!gst_mpeg2dec_src_convert (pad, format, stop, &conv, &time_stop))
1657     goto convert_failed;
1659   GST_DEBUG ("seek to time %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1660       GST_TIME_ARGS (time_cur), GST_TIME_ARGS (time_stop));
1662   peer_event = gst_event_new_seek (rate, GST_FORMAT_TIME, flags,
1663       cur_type, time_cur, stop_type, time_stop);
1665   /* try seek on time then */
1666   if ((res = gst_pad_push_event (mpeg2dec->sinkpad, peer_event)))
1667     goto done;
1669   /* else we try to seek on bytes */
1670   conv = GST_FORMAT_BYTES;
1671   if (!gst_mpeg2dec_sink_convert (pad, GST_FORMAT_TIME, time_cur,
1672           &conv, &bytes_cur))
1673     goto convert_failed;
1674   if (!gst_mpeg2dec_sink_convert (pad, GST_FORMAT_TIME, time_stop,
1675           &conv, &bytes_stop))
1676     goto convert_failed;
1678   /* conversion succeeded, create the seek */
1679   peer_event =
1680       gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
1681       cur_type, bytes_cur, stop_type, bytes_stop);
1683   /* do the seek */
1684   res = gst_pad_push_event (mpeg2dec->sinkpad, peer_event);
1686 done:
1687   return res;
1689   /* ERRORS */
1690 convert_failed:
1691   {
1692     /* probably unsupported seek format */
1693     GST_DEBUG_OBJECT (mpeg2dec,
1694         "failed to convert format %u into GST_FORMAT_TIME", format);
1695     return FALSE;
1696   }
1700 static gboolean
1701 gst_mpeg2dec_src_event (GstPad * pad, GstEvent * event)
1703   gboolean res;
1704   GstMpeg2dec *mpeg2dec;
1706   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1708   if (mpeg2dec->decoder == NULL)
1709     goto no_decoder;
1711   switch (GST_EVENT_TYPE (event)) {
1712       /* the all-formats seek logic */
1713     case GST_EVENT_SEEK:{
1714       gst_event_ref (event);
1715       if (!(res = gst_pad_push_event (mpeg2dec->sinkpad, event))) {
1716         if (mpeg2dec->index)
1717           res = index_seek (pad, event);
1718         else
1719           res = normal_seek (pad, event);
1720       }
1721       gst_event_unref (event);
1722       break;
1723     }
1724     case GST_EVENT_QOS:
1725     {
1726       gdouble proportion;
1727       GstClockTimeDiff diff;
1728       GstClockTime timestamp;
1730       gst_event_parse_qos (event, &proportion, &diff, &timestamp);
1732       GST_OBJECT_LOCK (mpeg2dec);
1733       mpeg2dec->proportion = proportion;
1734       mpeg2dec->earliest_time = timestamp + diff;
1735       GST_OBJECT_UNLOCK (mpeg2dec);
1737       GST_DEBUG_OBJECT (mpeg2dec,
1738           "got QoS %" GST_TIME_FORMAT ", %" G_GINT64_FORMAT,
1739           GST_TIME_ARGS (timestamp), diff);
1741       res = gst_pad_push_event (mpeg2dec->sinkpad, event);
1742       break;
1743     }
1744     case GST_EVENT_NAVIGATION:
1745       /* Forward a navigation event unchanged */
1746     default:
1747       res = gst_pad_push_event (mpeg2dec->sinkpad, event);
1748       break;
1749   }
1750   return res;
1752 no_decoder:
1753   {
1754     GST_DEBUG_OBJECT (mpeg2dec, "no decoder, cannot handle event");
1755     gst_event_unref (event);
1756     return FALSE;
1757   }
1760 static GstStateChangeReturn
1761 gst_mpeg2dec_change_state (GstElement * element, GstStateChange transition)
1763   GstStateChangeReturn ret;
1764   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (element);
1766   switch (transition) {
1767     case GST_STATE_CHANGE_NULL_TO_READY:
1768       mpeg2_accel (MPEG2_ACCEL_DETECT);
1769       if ((mpeg2dec->decoder = mpeg2_init ()) == NULL)
1770         goto init_failed;
1771       mpeg2dec->info = mpeg2_info (mpeg2dec->decoder);
1772       break;
1773     case GST_STATE_CHANGE_READY_TO_PAUSED:
1774       gst_mpeg2dec_reset (mpeg2dec);
1775       gst_mpeg2dec_qos_reset (mpeg2dec);
1776       break;
1777     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1778     default:
1779       break;
1780   }
1782   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1784   switch (transition) {
1785     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1786       break;
1787     case GST_STATE_CHANGE_PAUSED_TO_READY:
1788       gst_mpeg2dec_qos_reset (mpeg2dec);
1789       clear_queued (mpeg2dec);
1790       break;
1791     case GST_STATE_CHANGE_READY_TO_NULL:
1792       if (mpeg2dec->decoder) {
1793         mpeg2_close (mpeg2dec->decoder);
1794         mpeg2dec->decoder = NULL;
1795         mpeg2dec->info = NULL;
1796       }
1797       clear_buffers (mpeg2dec);
1798       break;
1799     default:
1800       break;
1801   }
1802   return ret;
1804   /* ERRORS */
1805 init_failed:
1806   {
1807     GST_ELEMENT_ERROR (mpeg2dec, LIBRARY, INIT,
1808         (NULL), ("Failed to initialize libmpeg2 library"));
1809     return GST_STATE_CHANGE_FAILURE;
1810   }
1813 static gboolean
1814 plugin_init (GstPlugin * plugin)
1816   if (!gst_element_register (plugin, "mpeg2dec", GST_RANK_PRIMARY,
1817           GST_TYPE_MPEG2DEC))
1818     return FALSE;
1820   return TRUE;
1823 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1824     GST_VERSION_MINOR,
1825     "mpeg2dec",
1826     "LibMpeg2 decoder", plugin_init, VERSION, "GPL", GST_PACKAGE_NAME,
1827     GST_PACKAGE_ORIGIN);