]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gst-plugins-ugly0-10.git/blob - ext/mpeg2dec/gstmpeg2dec.c
f8133792d6e624e8baa95a5b1bedaa43e3d65d34
[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 /* mpeg2dec changed a struct name after 0.3.1, here's a workaround */
30 /* mpeg2dec also only defined MPEG2_RELEASE after 0.3.1
31    #if MPEG2_RELEASE < MPEG2_VERSION(0,3,2)
32 */
33 #ifndef MPEG2_RELEASE
34 #define MPEG2_VERSION(a,b,c) ((((a)&0xff)<<16)|(((b)&0xff)<<8)|((c)&0xff))
35 #define MPEG2_RELEASE MPEG2_VERSION(0,3,1)
36 typedef picture_t mpeg2_picture_t;
37 typedef gint mpeg2_state_t;
39 #define STATE_BUFFER 0
40 #endif
42 GST_DEBUG_CATEGORY_STATIC (mpeg2dec_debug);
43 #define GST_CAT_DEFAULT (mpeg2dec_debug)
45 /* table with framerates expressed as fractions */
46 static gint fpss[][2] = { {24000, 1001},
47 {24, 1}, {25, 1}, {30000, 1001},
48 {30, 1}, {50, 1}, {60000, 1001},
49 {60, 1}, {0, 1}
50 };
52 /* frame periods */
53 static guint frame_periods[] = {
54   1126125, 1125000, 1080000, 900900, 900000, 540000, 450450, 450000, 0
55 };
57 /* elementfactory information */
58 static GstElementDetails gst_mpeg2dec_details = {
59   "mpeg1 and mpeg2 video decoder",
60   "Codec/Decoder/Video",
61   "Uses libmpeg2 to decode MPEG video streams",
62   "Wim Taymans <wim.taymans@chello.be>",
63 };
65 /* Mpeg2dec signals and args */
66 enum
67 {
68   /* FILL ME */
69   LAST_SIGNAL
70 };
72 enum
73 {
74   ARG_0
75       /* FILL ME */
76 };
78 /* error out after receiving MAX_ERROR_COUNT STATE_INVALID return value
79  * from mpeg2_parse. -1 means never error out
80  */
81 #define MAX_ERROR_COUNT (5)
83 /*
84  * We can't use fractions in static pad templates, so
85  * we do something manual...
86  * FIXME: This is no longer true. We could use a normal pad template
87  * now
88  */
89 static GstPadTemplate *
90 src_templ (void)
91 {
92   static GstPadTemplate *templ = NULL;
94   if (!templ) {
95     GstCaps *caps;
96     GstStructure *structure;
97     GValue list = { 0 }
98     , fps = {
99     0}
100     , fmt = {
101     0};
102     char *fmts[] = { "YV12", "I420", "Y42B", NULL };
103     guint n;
105     caps = gst_caps_new_simple ("video/x-raw-yuv",
106         "format", GST_TYPE_FOURCC,
107         GST_MAKE_FOURCC ('I', '4', '2', '0'),
108         "width", GST_TYPE_INT_RANGE, 16, 4096,
109         "height", GST_TYPE_INT_RANGE, 16, 4096, NULL);
111     structure = gst_caps_get_structure (caps, 0);
113     g_value_init (&list, GST_TYPE_LIST);
114     g_value_init (&fps, GST_TYPE_FRACTION);
115     for (n = 0; fpss[n][0] != 0; n++) {
116       gst_value_set_fraction (&fps, fpss[n][0], fpss[n][1]);
117       gst_value_list_append_value (&list, &fps);
118     }
119     gst_structure_set_value (structure, "framerate", &list);
120     g_value_unset (&list);
121     g_value_unset (&fps);
123     g_value_init (&list, GST_TYPE_LIST);
124     g_value_init (&fmt, GST_TYPE_FOURCC);
125     for (n = 0; fmts[n] != NULL; n++) {
126       gst_value_set_fourcc (&fmt, GST_STR_FOURCC (fmts[n]));
127       gst_value_list_append_value (&list, &fmt);
128     }
129     gst_structure_set_value (structure, "format", &list);
130     g_value_unset (&list);
131     g_value_unset (&fmt);
133     templ = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps);
134   }
135   return templ;
138 #ifdef enable_user_data
139 static GstStaticPadTemplate user_data_template_factory =
140 GST_STATIC_PAD_TEMPLATE ("user_data",
141     GST_PAD_SRC,
142     GST_PAD_ALWAYS,
143     GST_STATIC_CAPS_ANY);
144 #endif
146 static GstStaticPadTemplate sink_template_factory =
147 GST_STATIC_PAD_TEMPLATE ("sink",
148     GST_PAD_SINK,
149     GST_PAD_ALWAYS,
150     GST_STATIC_CAPS ("video/mpeg, "
151         "mpegversion = (int) [ 1, 2 ], " "systemstream = (boolean) false")
152     );
154 static void gst_mpeg2dec_base_init (gpointer g_class);
155 static void gst_mpeg2dec_class_init (GstMpeg2decClass * klass);
156 static void gst_mpeg2dec_init (GstMpeg2dec * mpeg2dec);
158 static void gst_mpeg2dec_dispose (GObject * object);
159 static void gst_mpeg2dec_reset (GstMpeg2dec * mpeg2dec);
161 static void gst_mpeg2dec_set_property (GObject * object, guint prop_id,
162     const GValue * value, GParamSpec * pspec);
163 static void gst_mpeg2dec_get_property (GObject * object, guint prop_id,
164     GValue * value, GParamSpec * pspec);
165 static void gst_mpeg2dec_set_index (GstElement * element, GstIndex * index);
166 static GstIndex *gst_mpeg2dec_get_index (GstElement * element);
168 static gboolean gst_mpeg2dec_src_event (GstPad * pad, GstEvent * event);
169 static const GstQueryType *gst_mpeg2dec_get_src_query_types (GstPad * pad);
171 static gboolean gst_mpeg2dec_src_query (GstPad * pad, GstQuery * query);
173 static gboolean gst_mpeg2dec_sink_convert (GstPad * pad, GstFormat src_format,
174     gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
175 static gboolean gst_mpeg2dec_src_convert (GstPad * pad, GstFormat src_format,
176     gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
178 static GstStateChangeReturn gst_mpeg2dec_change_state (GstElement * element,
179     GstStateChange transition);
181 static gboolean gst_mpeg2dec_sink_event (GstPad * pad, GstEvent * event);
182 static GstFlowReturn gst_mpeg2dec_chain (GstPad * pad, GstBuffer * buf);
184 //static gboolean gst_mpeg2dec_sink_query (GstPad * pad, GstQuery * query);
185 static GstCaps *gst_mpeg2dec_src_getcaps (GstPad * pad);
187 #if 0
188 static const GstFormat *gst_mpeg2dec_get_formats (GstPad * pad);
189 #endif
191 #if 0
192 static const GstEventMask *gst_mpeg2dec_get_event_masks (GstPad * pad);
193 #endif
195 static GstElementClass *parent_class = NULL;
197 static gboolean crop_buffer (GstMpeg2dec * mpeg2dec, GstBuffer ** buf);
199 /*static guint gst_mpeg2dec_signals[LAST_SIGNAL] = { 0 };*/
201 GType
202 gst_mpeg2dec_get_type (void)
204   static GType mpeg2dec_type = 0;
206   if (!mpeg2dec_type) {
207     static const GTypeInfo mpeg2dec_info = {
208       sizeof (GstMpeg2decClass),
209       gst_mpeg2dec_base_init,
210       NULL,
211       (GClassInitFunc) gst_mpeg2dec_class_init,
212       NULL,
213       NULL,
214       sizeof (GstMpeg2dec),
215       0,
216       (GInstanceInitFunc) gst_mpeg2dec_init,
217     };
219     mpeg2dec_type =
220         g_type_register_static (GST_TYPE_ELEMENT, "GstMpeg2dec", &mpeg2dec_info,
221         0);
222   }
224   GST_DEBUG_CATEGORY_INIT (mpeg2dec_debug, "mpeg2dec", 0,
225       "MPEG2 decoder element");
227   return mpeg2dec_type;
230 static void
231 gst_mpeg2dec_base_init (gpointer g_class)
233   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
235   gst_element_class_add_pad_template (element_class, src_templ ());
236   gst_element_class_add_pad_template (element_class,
237       gst_static_pad_template_get (&sink_template_factory));
238 #ifdef enable_user_data
239   gst_element_class_add_pad_template (element_class,
240       gst_static_pad_template_get (&user_data_template_factory));
241 #endif
242   gst_element_class_set_details (element_class, &gst_mpeg2dec_details);
245 static void
246 gst_mpeg2dec_class_init (GstMpeg2decClass * klass)
248   GObjectClass *gobject_class;
249   GstElementClass *gstelement_class;
251   gobject_class = (GObjectClass *) klass;
252   gstelement_class = (GstElementClass *) klass;
254   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
256   gobject_class->set_property = gst_mpeg2dec_set_property;
257   gobject_class->get_property = gst_mpeg2dec_get_property;
258   gobject_class->dispose = gst_mpeg2dec_dispose;
260   gstelement_class->change_state = gst_mpeg2dec_change_state;
261   gstelement_class->set_index = gst_mpeg2dec_set_index;
262   gstelement_class->get_index = gst_mpeg2dec_get_index;
265 static void
266 gst_mpeg2dec_init (GstMpeg2dec * mpeg2dec)
268   /* create the sink and src pads */
269   mpeg2dec->sinkpad =
270       gst_pad_new_from_template (gst_static_pad_template_get
271       (&sink_template_factory), "sink");
272   gst_element_add_pad (GST_ELEMENT (mpeg2dec), mpeg2dec->sinkpad);
273   gst_pad_set_chain_function (mpeg2dec->sinkpad,
274       GST_DEBUG_FUNCPTR (gst_mpeg2dec_chain));
275   //gst_pad_set_query_function (mpeg2dec->sinkpad,
276   //                            GST_DEBUG_FUNCPTR (gst_mpeg2dec_get_sink_query));
277   gst_pad_set_event_function (mpeg2dec->sinkpad,
278       GST_DEBUG_FUNCPTR (gst_mpeg2dec_sink_event));
280   mpeg2dec->srcpad = gst_pad_new_from_template (src_templ (), "src");
281   gst_element_add_pad (GST_ELEMENT (mpeg2dec), mpeg2dec->srcpad);
282   gst_pad_set_getcaps_function (mpeg2dec->srcpad,
283       GST_DEBUG_FUNCPTR (gst_mpeg2dec_src_getcaps));
284   gst_pad_set_event_function (mpeg2dec->srcpad,
285       GST_DEBUG_FUNCPTR (gst_mpeg2dec_src_event));
286   gst_pad_set_query_type_function (mpeg2dec->srcpad,
287       GST_DEBUG_FUNCPTR (gst_mpeg2dec_get_src_query_types));
288   gst_pad_set_query_function (mpeg2dec->srcpad,
289       GST_DEBUG_FUNCPTR (gst_mpeg2dec_src_query));
291 #ifdef enable_user_data
292   mpeg2dec->userdatapad =
293       gst_pad_new_from_template (gst_static_pad_template_get
294       (&user_data_template_factory), "user_data");
295   gst_element_add_pad (GST_ELEMENT (mpeg2dec), mpeg2dec->userdatapad);
296 #endif
298   mpeg2dec->error_count = 0;
300   /* initialize the mpeg2dec acceleration */
303 static void
304 gst_mpeg2dec_dispose (GObject * object)
306   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (object);
308   if (mpeg2dec->decoder) {
309     GST_DEBUG_OBJECT (mpeg2dec, "closing decoder");
310     mpeg2_close (mpeg2dec->decoder);
311     mpeg2dec->decoder = NULL;
312   }
314   G_OBJECT_CLASS (parent_class)->dispose (object);
317 static void
318 gst_mpeg2dec_reset (GstMpeg2dec * mpeg2dec)
320   /* reset the initial video state */
321   mpeg2dec->format = MPEG2DEC_FORMAT_NONE;
322   mpeg2dec->width = -1;
323   mpeg2dec->height = -1;
324   mpeg2dec->segment_start = 0;
325   mpeg2dec->segment_end = -1;
326   mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
327   mpeg2dec->frame_period = 0;
328   mpeg2dec->need_sequence = TRUE;
329   mpeg2dec->next_time = 0;
330   mpeg2dec->offset = 0;
331   mpeg2dec->error_count = 0;
332   mpeg2_reset (mpeg2dec->decoder, 1);
335 static void
336 gst_mpeg2dec_set_index (GstElement * element, GstIndex * index)
338   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (element);
340   mpeg2dec->index = index;
342   gst_index_get_writer_id (index, GST_OBJECT (element), &mpeg2dec->index_id);
345 static GstIndex *
346 gst_mpeg2dec_get_index (GstElement * element)
348   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (element);
350   return mpeg2dec->index;
353 /* see gst-plugins/gst/games/gstvideoimage.c, paint_setup_I420() */
354 #define I420_Y_ROWSTRIDE(width) (GST_ROUND_UP_4(width))
355 #define I420_U_ROWSTRIDE(width) (GST_ROUND_UP_8(width)/2)
356 #define I420_V_ROWSTRIDE(width) ((GST_ROUND_UP_8(I420_Y_ROWSTRIDE(width)))/2)
358 #define I420_Y_OFFSET(w,h) (0)
359 #define I420_U_OFFSET(w,h) (I420_Y_OFFSET(w,h)+(I420_Y_ROWSTRIDE(w)*GST_ROUND_UP_2(h)))
360 #define I420_V_OFFSET(w,h) (I420_U_OFFSET(w,h)+(I420_U_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
362 #define I420_SIZE(w,h)     (I420_V_OFFSET(w,h)+(I420_V_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
364 static GstBuffer *
365 crop_copy_i420_buffer (GstMpeg2dec * mpeg2dec, GstBuffer * input)
367   GstBuffer *outbuf;
368   guint8 *dest, *src;
369   guint outsize, line;
371   outsize = I420_SIZE (mpeg2dec->width, mpeg2dec->height);
372   GST_LOG_OBJECT (mpeg2dec, "Copying input buffer %ux%u (%u) to output buffer "
373       "%ux%u (%u)", mpeg2dec->decoded_width, mpeg2dec->decoded_height,
374       GST_BUFFER_SIZE (input), mpeg2dec->width, mpeg2dec->height, outsize);
375   outbuf = gst_buffer_new_and_alloc (outsize);
377   /* Copy Y first */
378   src = GST_BUFFER_DATA (input);
379   dest = GST_BUFFER_DATA (outbuf);
380   for (line = 0; line < mpeg2dec->height; line++) {
381     memcpy (dest, src, mpeg2dec->width);
382     dest += I420_Y_ROWSTRIDE (mpeg2dec->width);
383     src += I420_Y_ROWSTRIDE (mpeg2dec->decoded_width);
384   }
386   /* U */
387   src = GST_BUFFER_DATA (input)
388       + I420_U_OFFSET (mpeg2dec->decoded_width, mpeg2dec->decoded_height);
389   dest = GST_BUFFER_DATA (outbuf)
390       + I420_U_OFFSET (mpeg2dec->width, mpeg2dec->height);
391   for (line = 0; line < mpeg2dec->height / 2; line++) {
392     memcpy (dest, src, mpeg2dec->width / 2);
393     dest += I420_U_ROWSTRIDE (mpeg2dec->width);
394     src += I420_U_ROWSTRIDE (mpeg2dec->decoded_width);
395   }
397   /* V */
398   src = GST_BUFFER_DATA (input)
399       + I420_V_OFFSET (mpeg2dec->decoded_width, mpeg2dec->decoded_height);
400   dest = GST_BUFFER_DATA (outbuf)
401       + I420_V_OFFSET (mpeg2dec->width, mpeg2dec->height);
402   for (line = 0; line < mpeg2dec->height / 2; line++) {
403     memcpy (dest, src, mpeg2dec->width / 2);
404     dest += I420_V_ROWSTRIDE (mpeg2dec->width);
405     src += I420_V_ROWSTRIDE (mpeg2dec->decoded_width);
406   }
408   return outbuf;
411   /* FIXME: this is unlikely to be right stride-wise and offset-wise */
412 static GstBuffer *
413 crop_copy_i422_buffer (GstMpeg2dec * mpeg2dec, GstBuffer * input)
415   GstBuffer *outbuf;
416   guint8 *in_data, *out_data;
417   guint line;
419   outbuf = gst_buffer_new_and_alloc (mpeg2dec->width * mpeg2dec->height * 2);
421   /* Copy Y first */
422   in_data = GST_BUFFER_DATA (input);
423   out_data = GST_BUFFER_DATA (outbuf);
424   for (line = 0; line < mpeg2dec->height; line++) {
425     memcpy (out_data, in_data, mpeg2dec->width);
426     out_data += mpeg2dec->width;
427     in_data += mpeg2dec->decoded_width;
428   }
430   /* Now copy U & V */
431   in_data = GST_BUFFER_DATA (input)
432       + mpeg2dec->decoded_width * mpeg2dec->decoded_height;
433   for (line = 0; line < mpeg2dec->height; line++) {
434     memcpy (out_data, in_data, mpeg2dec->width / 2);
435     memcpy (out_data + mpeg2dec->width * mpeg2dec->height / 2,
436         in_data + mpeg2dec->decoded_width * mpeg2dec->decoded_height / 2,
437         mpeg2dec->width / 2);
438     out_data += mpeg2dec->width / 2;
439     in_data += mpeg2dec->decoded_width / 2;
440   }
442   return outbuf;
445 static gboolean
446 crop_buffer (GstMpeg2dec * mpeg2dec, GstBuffer ** buf)
448   gboolean result = FALSE;
449   GstBuffer *input = *buf;
450   GstBuffer *outbuf = input;
452   /*We crop only if the target region is smaller than the input one */
453   if ((mpeg2dec->decoded_width > mpeg2dec->width) ||
454       (mpeg2dec->decoded_height > mpeg2dec->height)) {
455     /* If we don't know about the format, we just return the original
456      * buffer.
457      */
458     if (mpeg2dec->format == MPEG2DEC_FORMAT_I422 ||
459         mpeg2dec->format == MPEG2DEC_FORMAT_I420 ||
460         mpeg2dec->format == MPEG2DEC_FORMAT_YV12) {
461       /*FIXME:  I have tried to use gst_buffer_copy_on_write, but it
462        *        still have some artifact, so I'me allocating new buffer
463        *        for each frame decoded...
464        */
465       if (mpeg2dec->format == MPEG2DEC_FORMAT_I422) {
466         outbuf = crop_copy_i422_buffer (mpeg2dec, input);
467       } else {
468         outbuf = crop_copy_i420_buffer (mpeg2dec, input);
469       }
471       gst_buffer_set_caps (outbuf, GST_PAD_CAPS (mpeg2dec->srcpad));
472       gst_buffer_stamp (outbuf, input);
474       *buf = outbuf;
475       result = TRUE;
476     }
477   }
479   return result;
482 static GstFlowReturn
483 gst_mpeg2dec_alloc_buffer (GstMpeg2dec * mpeg2dec, gint64 offset,
484     GstBuffer ** obuf)
486   GstBuffer *outbuf = NULL;
487   gint size = mpeg2dec->decoded_width * mpeg2dec->decoded_height;
488   guint8 *buf[3], *out = NULL;
489   GstFlowReturn ret = GST_FLOW_OK;
491   if (mpeg2dec->format == MPEG2DEC_FORMAT_I422) {
492     ret =
493         gst_pad_alloc_buffer_and_set_caps (mpeg2dec->srcpad,
494         GST_BUFFER_OFFSET_NONE, size * 2, GST_PAD_CAPS (mpeg2dec->srcpad),
495         &outbuf);
496     if (ret != GST_FLOW_OK)
497       goto no_buffer;
499     out = GST_BUFFER_DATA (outbuf);
501     buf[0] = out;
502     buf[1] = buf[0] + size;
503     buf[2] = buf[1] + size / 2;
505   } else {
506     ret =
507         gst_pad_alloc_buffer_and_set_caps (mpeg2dec->srcpad,
508         GST_BUFFER_OFFSET_NONE, (size * 3) / 2, GST_PAD_CAPS (mpeg2dec->srcpad),
509         &outbuf);
510     if (ret != GST_FLOW_OK)
511       goto no_buffer;
513     out = GST_BUFFER_DATA (outbuf);
515     buf[0] = out;
516     if (mpeg2dec->format == MPEG2DEC_FORMAT_I420) {
517       buf[0] = out;
518       buf[1] = buf[0] + size;
519       buf[2] = buf[1] + size / 4;
520     } else {
521       buf[0] = out;
522       buf[2] = buf[0] + size;
523       buf[1] = buf[2] + size / 4;
524     }
525   }
527   mpeg2_set_buf (mpeg2dec->decoder, buf, outbuf);
529   /* we store the original byteoffset of this picture in the stream here
530    * because we need it for indexing */
531   GST_BUFFER_OFFSET (outbuf) = offset;
533 done:
534   if (ret != GST_FLOW_OK) {
535     outbuf = NULL;              /* just to asure NULL return, looking the path
536                                    above it happens only when gst_pad_alloc_buffer_and_set_caps
537                                    fails to alloc outbf */
538   }
539   *obuf = outbuf;
541   return ret;
543   /* ERRORS */
544 no_buffer:
545   {
546     if (GST_FLOW_IS_FATAL (ret)) {
547       GST_ELEMENT_ERROR (mpeg2dec, RESOURCE, FAILED, (NULL),
548           ("Failed to allocate memory for buffer, reason %s",
549               gst_flow_get_name (ret)));
550     }
551     goto done;
552   }
555 static gboolean
556 gst_mpeg2dec_negotiate_format (GstMpeg2dec * mpeg2dec)
558   GstCaps *caps;
559   guint32 fourcc;
560   const mpeg2_info_t *info;
561   const mpeg2_sequence_t *sequence;
563   info = mpeg2_info (mpeg2dec->decoder);
564   sequence = info->sequence;
566   if (sequence->width != sequence->chroma_width &&
567       sequence->height != sequence->chroma_height) {
568     fourcc = GST_STR_FOURCC ("I420");
569     mpeg2dec->format = MPEG2DEC_FORMAT_I420;
570   } else if (sequence->width == sequence->chroma_width ||
571       sequence->height == sequence->chroma_height) {
572     fourcc = GST_STR_FOURCC ("Y42B");
573     mpeg2dec->format = MPEG2DEC_FORMAT_I422;
574   } else {
575     g_warning ("mpeg2dec: 4:4:4 format not yet supported");
576     return (FALSE);
577   }
579   caps = gst_caps_new_simple ("video/x-raw-yuv",
580       "format", GST_TYPE_FOURCC, fourcc,
581       "width", G_TYPE_INT, mpeg2dec->width,
582       "height", G_TYPE_INT, mpeg2dec->height,
583       "pixel-aspect-ratio", GST_TYPE_FRACTION, mpeg2dec->pixel_width,
584       mpeg2dec->pixel_height,
585       "framerate", GST_TYPE_FRACTION, mpeg2dec->fps_n, mpeg2dec->fps_d, NULL);
587   gst_pad_set_caps (mpeg2dec->srcpad, caps);
588   gst_caps_unref (caps);
590   return TRUE;
593 static GstFlowReturn
594 handle_sequence (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info)
596   gint i;
597   GstBuffer *buf;
598   GstFlowReturn ret;
599   guint8 *dummybuf[3] = { NULL, NULL, NULL };
601   mpeg2dec->width = info->sequence->picture_width;
602   mpeg2dec->height = info->sequence->picture_height;
603   mpeg2dec->pixel_width = info->sequence->pixel_width;
604   mpeg2dec->pixel_height = info->sequence->pixel_height;
605   mpeg2dec->decoded_width = info->sequence->width;
606   mpeg2dec->decoded_height = info->sequence->height;
607   mpeg2dec->total_frames = 0;
609   /* find framerate */
610   for (i = 0; i < 9; i++) {
611     if (info->sequence->frame_period == frame_periods[i]) {
612       mpeg2dec->fps_n = fpss[i][0];
613       mpeg2dec->fps_d = fpss[i][1];
614     }
615   }
616   mpeg2dec->frame_period = info->sequence->frame_period * GST_USECOND / 27;
618   GST_DEBUG_OBJECT (mpeg2dec,
619       "sequence flags: %d, frame period: %d (%g), frame rate: %d/%d",
620       info->sequence->flags, info->sequence->frame_period,
621       (double) (mpeg2dec->frame_period) / GST_SECOND, mpeg2dec->fps_n,
622       mpeg2dec->fps_d);
623   GST_DEBUG_OBJECT (mpeg2dec, "profile: %02x, colour_primaries: %d",
624       info->sequence->profile_level_id, info->sequence->colour_primaries);
625   GST_DEBUG_OBJECT (mpeg2dec, "transfer chars: %d, matrix coef: %d",
626       info->sequence->transfer_characteristics,
627       info->sequence->matrix_coefficients);
629   if (!gst_mpeg2dec_negotiate_format (mpeg2dec))
630     goto negotiate_failed;
632   mpeg2_custom_fbuf (mpeg2dec->decoder, 1);
633   /* Pump in some null buffers, because otherwise libmpeg2 doesn't 
634    * initialise the discard_fbuf->id */
635   mpeg2_set_buf (mpeg2dec->decoder, dummybuf, NULL);
636   mpeg2_set_buf (mpeg2dec->decoder, dummybuf, NULL);
637   mpeg2_set_buf (mpeg2dec->decoder, dummybuf, NULL);
639   ret = gst_mpeg2dec_alloc_buffer (mpeg2dec, mpeg2dec->offset, &buf);
640   if (ret != GST_FLOW_OK)
641     goto done;
643   /* libmpeg2 discards first buffer twice for some reason. */
644   gst_buffer_ref (buf);
646   mpeg2dec->need_sequence = FALSE;
648 done:
649   return ret;
651 negotiate_failed:
652   {
653     GST_ELEMENT_ERROR (mpeg2dec, CORE, NEGOTIATION, (NULL), (NULL));
654     ret = GST_FLOW_NOT_NEGOTIATED;
655     goto done;
656   }
659 static GstFlowReturn
660 handle_picture (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info)
662   gboolean key_frame = FALSE;
663   GstBuffer *outbuf;
664   GstFlowReturn ret;
666   GST_DEBUG_OBJECT (mpeg2dec, "handle picture");
668   if (info->current_picture) {
669     key_frame =
670         (info->current_picture->flags & PIC_MASK_CODING_TYPE) ==
671         PIC_FLAG_CODING_TYPE_I;
672   }
673   ret = gst_mpeg2dec_alloc_buffer (mpeg2dec, mpeg2dec->offset, &outbuf);
674   if (ret != GST_FLOW_OK)
675     goto done;
677   GST_DEBUG_OBJECT (mpeg2dec, "picture %s, outbuf %p, offset %"
678       G_GINT64_FORMAT,
679       key_frame ? ", kf," : "    ", outbuf, GST_BUFFER_OFFSET (outbuf)
680       );
682   if (mpeg2dec->discont_state == MPEG2DEC_DISC_NEW_PICTURE && key_frame)
683     mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_KEYFRAME;
685   mpeg2_skip (mpeg2dec->decoder, 0);
687 done:
688   return ret;
691 static GstFlowReturn
692 handle_slice (GstMpeg2dec * mpeg2dec, const mpeg2_info_t * info)
694   GstBuffer *outbuf = NULL;
695   GstFlowReturn ret = GST_FLOW_OK;
697   GST_DEBUG_OBJECT (mpeg2dec, "picture slice/end %p %p %p %p",
698       info->display_fbuf,
699       info->display_picture, info->current_picture,
700       (info->display_fbuf ? info->display_fbuf->id : NULL));
702   if (info->display_fbuf && info->display_fbuf->id) {
703     const mpeg2_picture_t *picture;
704     gboolean key_frame = FALSE;
705     GstClockTime time;
707     outbuf = GST_BUFFER (info->display_fbuf->id);
709     picture = info->display_picture;
711     key_frame =
712         (picture->flags & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_I;
714     GST_DEBUG_OBJECT (mpeg2dec, "picture keyframe %d", key_frame);
716     if (!key_frame)
717       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
718     else
719       GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
721     if (mpeg2dec->discont_state == MPEG2DEC_DISC_NEW_KEYFRAME && key_frame)
722       mpeg2dec->discont_state = MPEG2DEC_DISC_NONE;
724     time = GST_CLOCK_TIME_NONE;
726 #if MPEG2_RELEASE < MPEG2_VERSION(0,4,0)
727     if (picture->flags & PIC_FLAG_PTS)
728       time = MPEG_TIME_TO_GST_TIME (picture->pts);
729 #else
730     if (picture->flags & PIC_FLAG_TAGS)
731       time = MPEG_TIME_TO_GST_TIME ((GstClockTime) (picture->
732               tag2) << 32 | picture->tag);
733 #endif
735     if (time == GST_CLOCK_TIME_NONE) {
736       time = mpeg2dec->next_time;
737       GST_DEBUG_OBJECT (mpeg2dec, "picture didn't have pts");
738     } else {
739       GST_DEBUG_OBJECT (mpeg2dec,
740           "picture had pts %" GST_TIME_FORMAT ", we had %"
741           GST_TIME_FORMAT, GST_TIME_ARGS (time),
742           GST_TIME_ARGS (mpeg2dec->next_time));
743       mpeg2dec->next_time = time;
744     }
745     GST_BUFFER_TIMESTAMP (outbuf) = time;
747     /* TODO set correct offset here based on frame number */
748     if (info->display_picture_2nd) {
749       GST_BUFFER_DURATION (outbuf) = (picture->nb_fields +
750           info->display_picture_2nd->nb_fields) * mpeg2dec->frame_period / 2;
751     } else {
752       GST_BUFFER_DURATION (outbuf) =
753           picture->nb_fields * mpeg2dec->frame_period / 2;
754     }
755     mpeg2dec->next_time += GST_BUFFER_DURATION (outbuf);
757     GST_DEBUG_OBJECT (mpeg2dec,
758         "picture: %s %s fields:%d off:%" G_GINT64_FORMAT " ts:%"
759         GST_TIME_FORMAT,
760         (picture->flags & PIC_FLAG_TOP_FIELD_FIRST ? "tff " : "    "),
761         (picture->flags & PIC_FLAG_PROGRESSIVE_FRAME ? "prog" : "    "),
762         picture->nb_fields, GST_BUFFER_OFFSET (outbuf),
763         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
765     if (mpeg2dec->index) {
766       gst_index_add_association (mpeg2dec->index, mpeg2dec->index_id,
767           (key_frame ? GST_ASSOCIATION_FLAG_KEY_UNIT : 0),
768           GST_FORMAT_BYTES, GST_BUFFER_OFFSET (outbuf),
769           GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (outbuf), 0);
770     }
772     if (picture->flags & PIC_FLAG_SKIP) {
773       GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer because of skip flag");
774     } else if (mpeg2dec->discont_state != MPEG2DEC_DISC_NONE) {
775       GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer, discont state %d",
776           mpeg2dec->discont_state);
777     } else if (mpeg2dec->next_time < mpeg2dec->segment_start) {
778       GST_DEBUG_OBJECT (mpeg2dec, "dropping buffer, next_time %"
779           GST_TIME_FORMAT " <  segment_start %" GST_TIME_FORMAT,
780           GST_TIME_ARGS (mpeg2dec->next_time),
781           GST_TIME_ARGS (mpeg2dec->segment_start));
782     } else {
783       gboolean cropped_outbuf_different_from_outbuf = FALSE;
785       GST_LOG_OBJECT (mpeg2dec, "pushing buffer, timestamp %"
786           GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
787           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
788           GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
790       if ((mpeg2dec->decoded_height > mpeg2dec->height) ||
791           (mpeg2dec->decoded_width > mpeg2dec->width)) {
792         cropped_outbuf_different_from_outbuf = crop_buffer (mpeg2dec, &outbuf);
793       }
795       gst_buffer_ref (outbuf);
796       ret = gst_pad_push (mpeg2dec->srcpad, outbuf);
797       if (cropped_outbuf_different_from_outbuf)
798         gst_buffer_unref (outbuf);
799     }
800   }
802   if (info->discard_fbuf && info->discard_fbuf->id) {
803     GstBuffer *discard = GST_BUFFER (info->discard_fbuf->id);
805     gst_buffer_unref (discard);
806     GST_DEBUG_OBJECT (mpeg2dec, "Discarded buffer %p", discard);
807   }
808   return ret;
811 #if 0
812 static void
813 update_streaminfo (GstMpeg2dec * mpeg2dec)
815   GstCaps *caps;
816   GstProps *props;
817   GstPropsEntry *entry;
818   const mpeg2_info_t *info;
820   info = mpeg2_info (mpeg2dec->decoder);
822   props = gst_props_empty_new ();
824   entry =
825       gst_props_entry_new ("framerate",
826       G_TYPE_DOUBLE (GST_SECOND / (float) mpeg2dec->frame_period));
827   gst_props_add_entry (props, entry);
828   entry =
829       gst_props_entry_new ("bitrate",
830       G_TYPE_INT (info->sequence->byte_rate * 8));
831   gst_props_add_entry (props, entry);
833   caps = gst_caps_new ("mpeg2dec_streaminfo",
834       "application/x-gst-streaminfo", props);
836   gst_caps_replace_sink (&mpeg2dec->streaminfo, caps);
837   g_object_notify (G_OBJECT (mpeg2dec), "streaminfo");
839 #endif
841 static GstFlowReturn
842 gst_mpeg2dec_chain (GstPad * pad, GstBuffer * buf)
844   GstMpeg2dec *mpeg2dec;
845   guint32 size;
846   guint8 *data, *end;
847   GstClockTime pts;
848   const mpeg2_info_t *info;
849   mpeg2_state_t state;
850   gboolean done = FALSE;
851   GstFlowReturn ret = GST_FLOW_OK;
853   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
855   size = GST_BUFFER_SIZE (buf);
856   data = GST_BUFFER_DATA (buf);
857   pts = GST_BUFFER_TIMESTAMP (buf);
859   GST_LOG_OBJECT (mpeg2dec, "received buffer, timestamp %"
860       GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
861       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
862       GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
864   info = mpeg2dec->info;
865   end = data + size;
867   mpeg2dec->offset = GST_BUFFER_OFFSET (buf);
869   while (!done) {
870     GST_LOG_OBJECT (mpeg2dec, "calling parse");
871     state = mpeg2_parse (mpeg2dec->decoder);
872     GST_DEBUG_OBJECT (mpeg2dec, "parse state %d", state);
874     switch (state) {
875       case STATE_SEQUENCE:
876         ret = handle_sequence (mpeg2dec, info);
877         break;
878       case STATE_SEQUENCE_REPEATED:
879         GST_DEBUG_OBJECT (mpeg2dec, "sequence repeated");
880         break;
881       case STATE_GOP:
882         break;
883       case STATE_PICTURE:
884         ret = handle_picture (mpeg2dec, info);
885         break;
886       case STATE_SLICE_1ST:
887         GST_LOG_OBJECT (mpeg2dec, "1st slice of frame encountered");
888         break;
889       case STATE_PICTURE_2ND:
890         GST_LOG_OBJECT (mpeg2dec,
891             "Second picture header encountered. Decoding 2nd field");
892         break;
893 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 4, 0)
894       case STATE_INVALID_END:
895 #endif
896       case STATE_END:
897         mpeg2dec->need_sequence = TRUE;
898       case STATE_SLICE:
899         ret = handle_slice (mpeg2dec, info);
900         break;
901       case STATE_BUFFER:
902         if (data == NULL) {
903           done = TRUE;
904         } else {
905           if (pts != GST_CLOCK_TIME_NONE) {
906             gint64 mpeg_pts = GST_TIME_TO_MPEG_TIME (pts);
908             GST_DEBUG_OBJECT (mpeg2dec,
909                 "have pts: %" G_GINT64_FORMAT " (%" GST_TIME_FORMAT ")",
910                 mpeg_pts, GST_TIME_ARGS (MPEG_TIME_TO_GST_TIME (mpeg_pts)));
912 #if MPEG2_RELEASE >= MPEG2_VERSION(0,4,0)
913             mpeg2_tag_picture (mpeg2dec->decoder, mpeg_pts & 0xffffffff,
914                 mpeg_pts >> 32);
915 #else
916             mpeg2_pts (mpeg2dec->decoder, mpeg_pts);
917 #endif
918           } else {
919             GST_LOG ("no pts");
920           }
922           GST_LOG_OBJECT (mpeg2dec, "calling mpeg2_buffer");
923           mpeg2_buffer (mpeg2dec->decoder, data, end);
924           GST_LOG_OBJECT (mpeg2dec, "calling mpeg2_buffer done");
926           data = NULL;
927         }
928         break;
929         /* error */
930       case STATE_INVALID:
931         mpeg2dec->error_count++;
932         GST_WARNING_OBJECT (mpeg2dec, "Decoding error #%d",
933             mpeg2dec->error_count);
934         if (mpeg2dec->error_count >= MAX_ERROR_COUNT && MAX_ERROR_COUNT > 0) {
935           GST_WARNING_OBJECT (mpeg2dec, "Too many decoding errors");
936           goto exit_error;
937         }
938         goto exit;
939       default:
940         GST_ERROR_OBJECT (mpeg2dec, "Unknown libmpeg2 state %d, FIXME", state);
941         goto exit;
942     }
944     mpeg2dec->error_count = 0;
946     /*
947      * FIXME: should pass more information such as state the user data is from
948      */
949 #ifdef enable_user_data
950     if (info->user_data_len > 0) {
951       GstBuffer *udbuf = gst_buffer_new_and_alloc (info->user_data_len);
953       memcpy (GST_BUFFER_DATA (udbuf), info->user_data, info->user_data_len);
955       gst_pad_push (mpeg2dec->userdatapad, udbuf);
956     }
957 #endif
959     if (ret != GST_FLOW_OK) {
960       mpeg2_reset (mpeg2dec->decoder, 0);
961       break;
962     }
963   }
964   gst_buffer_unref (buf);
965   return ret;
967 exit:
968   gst_buffer_unref (buf);
969   return GST_FLOW_OK;
971 exit_error:
972   GST_ELEMENT_ERROR (mpeg2dec, STREAM, DECODE, (NULL), (NULL));
973   gst_buffer_unref (buf);
974   return GST_FLOW_ERROR;
977 static gboolean
978 gst_mpeg2dec_sink_event (GstPad * pad, GstEvent * event)
981   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
982   gboolean ret = TRUE;
984   GST_DEBUG_OBJECT (mpeg2dec, "Got %s event on sink pad",
985       GST_EVENT_TYPE_NAME (event));
987   switch (GST_EVENT_TYPE (event)) {
988     case GST_EVENT_NEWSEGMENT:
989     {
990       mpeg2dec->next_time = -1;;
991       ret = gst_pad_event_default (pad, event);
992       break;
993     }
994     case GST_EVENT_FLUSH_START:
995       ret = gst_pad_event_default (pad, event);
996       break;
997     case GST_EVENT_FLUSH_STOP:
998       mpeg2dec->discont_state = MPEG2DEC_DISC_NEW_PICTURE;
999       mpeg2dec->next_time = -1;;
1000       mpeg2_reset (mpeg2dec->decoder, 0);
1001       ret = gst_pad_event_default (pad, event);
1002       break;
1003     case GST_EVENT_EOS:
1004       if (mpeg2dec->index && mpeg2dec->closed) {
1005         gst_index_commit (mpeg2dec->index, mpeg2dec->index_id);
1006       }
1007       ret = gst_pad_event_default (pad, event);
1008       break;
1009     default:
1010       ret = gst_pad_event_default (pad, event);
1011       break;
1012   }
1014   return ret;
1018 static GstCaps *
1019 gst_mpeg2dec_src_getcaps (GstPad * pad)
1021   GstCaps *caps;
1023   GST_OBJECT_LOCK (pad);
1024   if (!(caps = GST_PAD_CAPS (pad)))
1025     caps = (GstCaps *) gst_pad_get_pad_template_caps (pad);
1026   caps = gst_caps_ref (caps);
1027   GST_OBJECT_UNLOCK (pad);
1029   return caps;
1032 static gboolean
1033 gst_mpeg2dec_sink_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
1034     GstFormat * dest_format, gint64 * dest_value)
1036   gboolean res = TRUE;
1037   GstMpeg2dec *mpeg2dec;
1038   const mpeg2_info_t *info;
1040   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1042   if (mpeg2dec->decoder == NULL)
1043     return FALSE;
1045   if (src_format == *dest_format) {
1046     *dest_value = src_value;
1047     return TRUE;
1048   }
1050   info = mpeg2_info (mpeg2dec->decoder);
1052   switch (src_format) {
1053     case GST_FORMAT_BYTES:
1054       switch (*dest_format) {
1055         case GST_FORMAT_TIME:
1056           if (info->sequence && info->sequence->byte_rate) {
1057             *dest_value = GST_SECOND * src_value / info->sequence->byte_rate;
1058             break;
1059           }
1060         default:
1061           res = FALSE;
1062       }
1063       break;
1064     case GST_FORMAT_TIME:
1065       switch (*dest_format) {
1066         case GST_FORMAT_BYTES:
1067           if (info->sequence && info->sequence->byte_rate) {
1068             *dest_value = src_value * info->sequence->byte_rate / GST_SECOND;
1069             break;
1070           }
1071         default:
1072           res = FALSE;
1073       }
1074       break;
1075     default:
1076       res = FALSE;
1077   }
1078   return res;
1082 static gboolean
1083 gst_mpeg2dec_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
1084     GstFormat * dest_format, gint64 * dest_value)
1086   gboolean res = TRUE;
1087   GstMpeg2dec *mpeg2dec;
1088   const mpeg2_info_t *info;
1089   guint64 scale = 1;
1091   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1093   if (mpeg2dec->decoder == NULL)
1094     return FALSE;
1096   if (src_format == *dest_format) {
1097     *dest_value = src_value;
1098     return TRUE;
1099   }
1101   info = mpeg2_info (mpeg2dec->decoder);
1103   switch (src_format) {
1104     case GST_FORMAT_BYTES:
1105       switch (*dest_format) {
1106         case GST_FORMAT_TIME:
1107         default:
1108           res = FALSE;
1109       }
1110       break;
1111     case GST_FORMAT_TIME:
1112       switch (*dest_format) {
1113         case GST_FORMAT_BYTES:
1114           scale = 6 * (mpeg2dec->width * mpeg2dec->height >> 2);
1115         case GST_FORMAT_DEFAULT:
1116           if (info->sequence && mpeg2dec->frame_period) {
1117             *dest_value = src_value * scale / mpeg2dec->frame_period;
1118             break;
1119           }
1120         default:
1121           res = FALSE;
1122       }
1123       break;
1124     case GST_FORMAT_DEFAULT:
1125       switch (*dest_format) {
1126         case GST_FORMAT_TIME:
1127           *dest_value = src_value * mpeg2dec->frame_period;
1128           break;
1129         case GST_FORMAT_BYTES:
1130           *dest_value =
1131               src_value * 6 * ((mpeg2dec->width * mpeg2dec->height) >> 2);
1132           break;
1133         default:
1134           res = FALSE;
1135       }
1136       break;
1137     default:
1138       res = FALSE;
1139   }
1140   return res;
1143 static const GstQueryType *
1144 gst_mpeg2dec_get_src_query_types (GstPad * pad)
1146   static const GstQueryType types[] = {
1147     GST_QUERY_POSITION,
1148     GST_QUERY_DURATION,
1149     0
1150   };
1152   return types;
1155 static gboolean
1156 gst_mpeg2dec_src_query (GstPad * pad, GstQuery * query)
1158   gboolean res = TRUE;
1159   GstMpeg2dec *mpeg2dec;
1161   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1163   switch (GST_QUERY_TYPE (query)) {
1164     case GST_QUERY_POSITION:
1165     {
1166       GstFormat format;
1167       gint64 cur;
1169       /* save requested format */
1170       gst_query_parse_position (query, &format, NULL);
1172       /* and convert to the requested format */
1173       if (!gst_mpeg2dec_src_convert (pad, GST_FORMAT_TIME,
1174               mpeg2dec->next_time, &format, &cur))
1175         goto error;
1177       gst_query_set_position (query, format, cur);
1179       GST_LOG_OBJECT (mpeg2dec,
1180           "position query: we return %llu (format %u)", cur, format);
1181       break;
1182     }
1183     case GST_QUERY_DURATION:
1184     {
1185       GstFormat format;
1186       GstFormat rformat;
1187       gint64 total, total_bytes;
1188       GstPad *peer;
1190       if ((peer = gst_pad_get_peer (mpeg2dec->sinkpad)) == NULL)
1191         goto error;
1193       /* send to peer */
1194       if ((res = gst_pad_query (peer, query))) {
1195         gst_object_unref (peer);
1196         goto done;
1197       } else {
1198         GST_LOG_OBJECT (mpeg2dec, "query on peer pad failed, trying bytes");
1199       }
1201       /* save requested format */
1202       gst_query_parse_duration (query, &format, NULL);
1204       /* query peer for total length in bytes */
1205       gst_query_set_duration (query, GST_FORMAT_BYTES, -1);
1207       if (!(res = gst_pad_query (peer, query))) {
1208         GST_LOG_OBJECT (mpeg2dec, "query on peer pad failed");
1209         gst_object_unref (peer);
1210         goto error;
1211       }
1212       gst_object_unref (peer);
1214       /* get the returned format */
1215       gst_query_parse_duration (query, &rformat, &total_bytes);
1216       GST_LOG_OBJECT (mpeg2dec, "peer pad returned total=%lld bytes",
1217           total_bytes);
1219       if (total_bytes != -1) {
1220         if (!gst_mpeg2dec_sink_convert (pad, GST_FORMAT_BYTES, total_bytes,
1221                 &format, &total))
1222           goto error;
1223       } else {
1224         total = -1;
1225       }
1227       gst_query_set_duration (query, format, total);
1229       GST_LOG_OBJECT (mpeg2dec,
1230           "position query: we return %llu (format %u)", total, format);
1231       break;
1232     }
1233     default:
1234       res = gst_pad_query_default (pad, query);
1235       break;
1236   }
1237 done:
1238   return res;
1240 error:
1242   GST_DEBUG ("error handling query");
1243   return FALSE;
1247 #if 0
1248 static const GstEventMask *
1249 gst_mpeg2dec_get_event_masks (GstPad * pad)
1251   static const GstEventMask masks[] = {
1252     {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH},
1253     {GST_EVENT_NAVIGATION, GST_EVENT_FLAG_NONE},
1254     {0,}
1255   };
1257   return masks;
1259 #endif
1261 static gboolean
1262 index_seek (GstPad * pad, GstEvent * event)
1264   GstIndexEntry *entry;
1265   GstMpeg2dec *mpeg2dec;
1266   gdouble rate;
1267   GstFormat format;
1268   GstSeekFlags flags;
1269   GstSeekType cur_type, stop_type;
1270   gint64 cur, stop;
1272   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1274   gst_event_parse_seek (event, &rate, &format, &flags,
1275       &cur_type, &cur, &stop_type, &stop);
1277   entry = gst_index_get_assoc_entry (mpeg2dec->index, mpeg2dec->index_id,
1278       GST_INDEX_LOOKUP_BEFORE, GST_ASSOCIATION_FLAG_KEY_UNIT, format, cur);
1280   if ((entry) && gst_pad_is_linked (mpeg2dec->sinkpad)) {
1281     const GstFormat *peer_formats, *try_formats;
1283     /* since we know the exact byteoffset of the frame, make sure to seek on bytes first */
1284     const GstFormat try_all_formats[] = {
1285       GST_FORMAT_BYTES,
1286       GST_FORMAT_TIME,
1287       0
1288     };
1290     try_formats = try_all_formats;
1292 #if 0
1293     peer_formats = gst_pad_get_formats (GST_PAD_PEER (mpeg2dec->sinkpad));
1294 #else
1295     peer_formats = try_all_formats;     /* FIXE ME */
1296 #endif
1298     while (gst_formats_contains (peer_formats, *try_formats)) {
1299       gint64 value;
1301       if (gst_index_entry_assoc_map (entry, *try_formats, &value)) {
1302         GstEvent *seek_event;
1304         GST_DEBUG_OBJECT (mpeg2dec, "index %s %" G_GINT64_FORMAT
1305             " -> %s %" G_GINT64_FORMAT,
1306             gst_format_get_details (format)->nick,
1307             cur, gst_format_get_details (*try_formats)->nick, value);
1309         /* lookup succeeded, create the seek */
1310         seek_event =
1311             gst_event_new_seek (rate, *try_formats, flags, cur_type, value,
1312             stop_type, stop);
1313         /* do the seek */
1314         if (gst_pad_push_event (mpeg2dec->sinkpad, seek_event)) {
1315           /* seek worked, we're done, loop will exit */
1316 #if 0
1317           mpeg2dec->segment_start = GST_EVENT_SEEK_OFFSET (event);
1318 #endif
1319           return TRUE;
1320         }
1321       }
1322       try_formats++;
1323     }
1324   }
1325   return FALSE;
1329 static gboolean
1330 normal_seek (GstPad * pad, GstEvent * event)
1332   gdouble rate;
1333   GstFormat format, conv;
1334   GstSeekFlags flags;
1335   GstSeekType cur_type, stop_type;
1336   gint64 cur, stop;
1337   gint64 time_cur, bytes_cur;
1338   gint64 time_stop, bytes_stop;
1339   gboolean res;
1340   GstMpeg2dec *mpeg2dec;
1341   GstEvent *peer_event;
1343   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1345   GST_DEBUG ("normal seek");
1347   gst_event_parse_seek (event, &rate, &format, &flags,
1348       &cur_type, &cur, &stop_type, &stop);
1350   conv = GST_FORMAT_TIME;
1351   if (!gst_mpeg2dec_src_convert (pad, format, cur, &conv, &time_cur))
1352     goto convert_failed;
1353   if (!gst_mpeg2dec_src_convert (pad, format, stop, &conv, &time_stop))
1354     goto convert_failed;
1356   GST_DEBUG ("seek to time %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1357       GST_TIME_ARGS (time_cur), GST_TIME_ARGS (time_stop));
1359   peer_event = gst_event_new_seek (rate, GST_FORMAT_TIME, flags,
1360       cur_type, time_cur, stop_type, time_stop);
1362   /* try seek on time then */
1363   if ((res = gst_pad_push_event (mpeg2dec->sinkpad, peer_event)))
1364     goto done;
1366   /* else we try to seek on bytes */
1367   conv = GST_FORMAT_BYTES;
1368   if (!gst_mpeg2dec_sink_convert (pad, GST_FORMAT_TIME, time_cur,
1369           &conv, &bytes_cur))
1370     goto convert_failed;
1371   if (!gst_mpeg2dec_sink_convert (pad, GST_FORMAT_TIME, time_stop,
1372           &conv, &bytes_stop))
1373     goto convert_failed;
1375   /* conversion succeeded, create the seek */
1376   peer_event =
1377       gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
1378       cur_type, bytes_cur, stop_type, bytes_stop);
1380   /* do the seek */
1381   res = gst_pad_push_event (mpeg2dec->sinkpad, peer_event);
1383 done:
1384   return res;
1386   /* ERRORS */
1387 convert_failed:
1388   {
1389     /* probably unsupported seek format */
1390     GST_DEBUG_OBJECT (mpeg2dec,
1391         "failed to convert format %u into GST_FORMAT_TIME", format);
1392     return FALSE;
1393   }
1397 static gboolean
1398 gst_mpeg2dec_src_event (GstPad * pad, GstEvent * event)
1400   gboolean res;
1401   GstMpeg2dec *mpeg2dec;
1403   mpeg2dec = GST_MPEG2DEC (GST_PAD_PARENT (pad));
1405   if (mpeg2dec->decoder == NULL)
1406     goto no_decoder;
1408   switch (GST_EVENT_TYPE (event)) {
1409       /* the all-formats seek logic */
1410     case GST_EVENT_SEEK:{
1411       gst_event_ref (event);
1412       if (!(res = gst_pad_event_default (pad, event))) {
1413         if (mpeg2dec->index)
1414           res = index_seek (pad, event);
1415         else
1416           res = normal_seek (pad, event);
1417       }
1418       gst_event_unref (event);
1419       break;
1420     }
1421     case GST_EVENT_NAVIGATION:
1422       /* Forward a navigation event unchanged */
1423     default:
1424       res = gst_pad_push_event (mpeg2dec->sinkpad, event);
1425       break;
1426   }
1427   return res;
1429 no_decoder:
1430   {
1431     GST_DEBUG_OBJECT (mpeg2dec, "no decoder, cannot handle event");
1432     gst_event_unref (event);
1433     return FALSE;
1434   }
1437 static GstStateChangeReturn
1438 gst_mpeg2dec_change_state (GstElement * element, GstStateChange transition)
1440   GstStateChangeReturn ret;
1441   GstMpeg2dec *mpeg2dec = GST_MPEG2DEC (element);
1443   switch (transition) {
1444     case GST_STATE_CHANGE_NULL_TO_READY:
1445       mpeg2_accel (MPEG2_ACCEL_DETECT);
1446       if ((mpeg2dec->decoder = mpeg2_init ()) == NULL)
1447         goto init_failed;
1448       mpeg2dec->info = mpeg2_info (mpeg2dec->decoder);
1449       break;
1450     case GST_STATE_CHANGE_READY_TO_PAUSED:
1451       gst_mpeg2dec_reset (mpeg2dec);
1452       break;
1453     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1454     default:
1455       break;
1456   }
1458   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1460   switch (transition) {
1461     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1462       break;
1463     case GST_STATE_CHANGE_PAUSED_TO_READY:
1464       break;
1465     case GST_STATE_CHANGE_READY_TO_NULL:
1466       if (mpeg2dec->decoder) {
1467         mpeg2_close (mpeg2dec->decoder);
1468         mpeg2dec->decoder = NULL;
1469         mpeg2dec->info = NULL;
1470       }
1471       break;
1472     default:
1473       break;
1474   }
1475   return ret;
1477   /* ERRORS */
1478 init_failed:
1479   {
1480     GST_ELEMENT_ERROR (mpeg2dec, LIBRARY, INIT,
1481         (NULL), ("Failed to initialize libmpeg2 library"));
1482     return GST_STATE_CHANGE_FAILURE;
1483   }
1486 static void
1487 gst_mpeg2dec_set_property (GObject * object, guint prop_id,
1488     const GValue * value, GParamSpec * pspec)
1490   GstMpeg2dec *src;
1492   g_return_if_fail (GST_IS_MPEG2DEC (object));
1493   src = GST_MPEG2DEC (object);
1495   switch (prop_id) {
1496     default:
1497       break;
1498   }
1501 static void
1502 gst_mpeg2dec_get_property (GObject * object, guint prop_id, GValue * value,
1503     GParamSpec * pspec)
1505   GstMpeg2dec *mpeg2dec;
1507   g_return_if_fail (GST_IS_MPEG2DEC (object));
1508   mpeg2dec = GST_MPEG2DEC (object);
1510   switch (prop_id) {
1511     default:
1512       break;
1513   }
1516 static gboolean
1517 plugin_init (GstPlugin * plugin)
1519   if (!gst_element_register (plugin, "mpeg2dec", GST_RANK_SECONDARY,
1520           GST_TYPE_MPEG2DEC))
1521     return FALSE;
1523   return TRUE;
1526 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1527     GST_VERSION_MINOR,
1528     "mpeg2dec",
1529     "LibMpeg2 decoder", plugin_init, VERSION, "GPL", GST_PACKAGE, GST_ORIGIN);