remove copyright field from plugins
[platform/upstream/gst-plugins-good.git] / ext / dv / gstdvdec.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  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <string.h>
24
25 /* First, include the header file for the plugin, to bring in the
26  * object definition and other useful things.
27  */
28 #include "gstdvdec.h"
29
30 #define NTSC_HEIGHT 480
31 #define NTSC_BUFFER 120000
32 #define PAL_HEIGHT 576
33 #define PAL_BUFFER 144000
34
35 /* The ElementDetails structure gives a human-readable description
36  * of the plugin, as well as author and version data.
37  */
38 static GstElementDetails dvdec_details = GST_ELEMENT_DETAILS (
39   "DV (smpte314) decoder plugin",
40   "Codec/Decoder/Video",
41   "Uses libdv to decode DV video (libdv.sourceforge.net)",
42   "Erik Walthinsen <omega@cse.ogi.edu>\n"
43   "Wim Taymans <wim.taymans@tvd.be>"
44 );
45
46
47 /* These are the signals that this element can fire.  They are zero-
48  * based because the numbers themselves are private to the object.
49  * LAST_SIGNAL is used for initialization of the signal array.
50  */
51 enum {
52   /* FILL ME */
53   LAST_SIGNAL
54 };
55
56 /* Arguments are identified the same way, but cannot be zero, so you
57  * must leave the ARG_0 entry in as a placeholder.
58  */
59 enum {
60   ARG_0,
61   ARG_CLAMP_LUMA,
62   ARG_CLAMP_CHROMA,
63   ARG_QUALITY,
64   /* FILL ME */
65 };
66
67 /* The PadFactory structures describe what pads the element has or
68  * can have.  They can be quite complex, but for this dvdec plugin
69  * they are rather simple.
70  */
71 GST_PAD_TEMPLATE_FACTORY (sink_temp,
72   "sink",
73   GST_PAD_SINK,
74   GST_PAD_ALWAYS,
75   GST_CAPS_NEW (
76     "dv_dec_sink",
77     "video/x-dv",
78     "systemstream", GST_PROPS_BOOLEAN (TRUE)
79   )
80 )
81
82
83 GST_PAD_TEMPLATE_FACTORY (video_src_temp,
84   "video",
85   GST_PAD_SRC,
86   GST_PAD_ALWAYS,
87   GST_CAPS_NEW (
88     "dv_dec_src",
89     "video/x-raw-yuv",
90     "format",           GST_PROPS_FOURCC (GST_MAKE_FOURCC ('Y','U','Y','2')),
91     "width",            GST_PROPS_INT (720),
92     "height",           GST_PROPS_LIST (
93                           GST_PROPS_INT (NTSC_HEIGHT),
94                           GST_PROPS_INT (PAL_HEIGHT)
95                         ),
96     "framerate",        GST_PROPS_LIST (
97                           GST_PROPS_FLOAT (25.),
98                           GST_PROPS_FLOAT (30/1.001)
99                         )
100   ),
101   GST_CAPS_NEW (
102     "dv_dec_src",
103     "video/x-raw-rgb",
104     "bpp",              GST_PROPS_INT(32),
105     "depth",            GST_PROPS_INT(32),
106     "endianness",       GST_PROPS_INT (G_BIG_ENDIAN),
107     "red_mask",         GST_PROPS_INT(0x000000ff),
108     "green_mask",       GST_PROPS_INT(0x0000ff00),
109     "blue_mask",        GST_PROPS_INT(0x00ff0000),
110     "width",            GST_PROPS_INT (720),
111     "height",           GST_PROPS_LIST (
112                           GST_PROPS_INT (NTSC_HEIGHT),
113                           GST_PROPS_INT (PAL_HEIGHT)
114                         ),
115     "framerate",        GST_PROPS_LIST (
116                           GST_PROPS_FLOAT (25.),
117                           GST_PROPS_FLOAT (30/1.001)
118                         )
119   ),
120   GST_CAPS_NEW (
121     "dv_dec_src",
122     "video/x-raw-rgb",
123     "bpp",              GST_PROPS_INT(24),
124     "depth",            GST_PROPS_INT(24),
125     "endianness",       GST_PROPS_INT (G_BIG_ENDIAN),
126     "red_mask",         GST_PROPS_INT(0x0000ff),
127     "green_mask",       GST_PROPS_INT(0x00ff00),
128     "blue_mask",        GST_PROPS_INT(0xff0000),
129     "width",            GST_PROPS_INT (720),
130     "height",           GST_PROPS_LIST (
131                           GST_PROPS_INT (NTSC_HEIGHT),
132                           GST_PROPS_INT (PAL_HEIGHT)
133                         ),
134     "framerate",        GST_PROPS_LIST (
135                           GST_PROPS_FLOAT (25.),
136                           GST_PROPS_FLOAT (30/1.001)
137                         )
138   )
139 )
140
141 GST_PAD_TEMPLATE_FACTORY ( audio_src_temp,
142   "audio",
143   GST_PAD_SRC,
144   GST_PAD_ALWAYS,
145   GST_CAPS_NEW (
146     "arts_sample",
147     "audio/x-raw-int",
148     "depth",            GST_PROPS_INT (16),
149     "width",            GST_PROPS_INT (16),
150     "signed",           GST_PROPS_BOOLEAN (TRUE),
151     "channels",         GST_PROPS_INT (2),
152     "endianness",       GST_PROPS_INT (G_LITTLE_ENDIAN),
153     "rate",             GST_PROPS_INT_RANGE (4000, 48000)
154   )
155 )
156
157 #define GST_TYPE_DVDEC_QUALITY (gst_dvdec_quality_get_type())
158 GType
159 gst_dvdec_quality_get_type (void)
160 {
161   static GType qtype = 0;
162   if (qtype == 0) {
163     static const GFlagsValue values[] = {
164       { DV_QUALITY_COLOR, "DV_QUALITY_COLOR", "Color or monochrome decoding" },
165       { DV_QUALITY_AC_1,  "DV_QUALITY_AC_1",  "AC 1 something" },
166       { DV_QUALITY_AC_2,  "DV_QUALITY_AC_2",  "AC 2 something" },
167       { 0, NULL, NULL }
168     };
169     qtype = g_flags_register_static ("GstDVDecQualityFlags", values);
170   }
171   return qtype;
172 }
173
174 /* A number of functon prototypes are given so we can refer to them later. */
175 static void             gst_dvdec_base_init             (gpointer g_class);
176 static void             gst_dvdec_class_init            (GstDVDecClass *klass);
177 static void             gst_dvdec_init                  (GstDVDec *dvdec);
178
179 static const GstQueryType* 
180                         gst_dvdec_get_src_query_types   (GstPad *pad);
181 static gboolean         gst_dvdec_src_query             (GstPad *pad, GstQueryType type,
182                                                          GstFormat *format, gint64 *value);
183 static const GstFormat* gst_dvdec_get_formats           (GstPad *pad);
184 static gboolean         gst_dvdec_sink_convert          (GstPad *pad, GstFormat src_format, gint64 src_value,
185                                                          GstFormat *dest_format, gint64 *dest_value);
186 static gboolean         gst_dvdec_src_convert           (GstPad *pad, GstFormat src_format, gint64 src_value,
187                                                          GstFormat *dest_format, gint64 *dest_value);
188
189 static const GstEventMask*
190                         gst_dvdec_get_event_masks       (GstPad *pad);
191 static gboolean         gst_dvdec_handle_src_event      (GstPad *pad, GstEvent *event);
192
193 static void             gst_dvdec_loop                  (GstElement *element);
194
195 static GstElementStateReturn    
196                         gst_dvdec_change_state          (GstElement *element);
197
198 static void             gst_dvdec_set_property          (GObject *object, guint prop_id, 
199                                                          const GValue *value, GParamSpec *pspec);
200 static void             gst_dvdec_get_property          (GObject *object, guint prop_id, 
201                                                          GValue *value, GParamSpec *pspec);
202
203 /* The parent class pointer needs to be kept around for some object
204  * operations.
205  */
206 static GstElementClass *parent_class = NULL;
207
208 /* This array holds the ids of the signals registered for this object.
209  * The array indexes are based on the enum up above.
210  */
211 /*static guint gst_dvdec_signals[LAST_SIGNAL] = { 0 }; */
212
213 /* This function is used to register and subsequently return the type
214  * identifier for this object class.  On first invocation, it will
215  * register the type, providing the name of the class, struct sizes,
216  * and pointers to the various functions that define the class.
217  */
218 GType
219 gst_dvdec_get_type (void)
220 {
221   static GType dvdec_type = 0;
222
223   if (!dvdec_type) {
224     static const GTypeInfo dvdec_info = {
225       sizeof (GstDVDecClass),      
226       gst_dvdec_base_init,      
227       NULL,
228       (GClassInitFunc) gst_dvdec_class_init,
229       NULL,
230       NULL,
231       sizeof (GstDVDec),
232       0,
233       (GInstanceInitFunc) gst_dvdec_init,
234     };
235     dvdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstDVDec", &dvdec_info, 0);
236   }
237   return dvdec_type;
238 }
239
240 static void
241 gst_dvdec_base_init (gpointer g_class)
242 {
243   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
244
245   /* The pad templates can be easily generated from the factories above,
246    * and then added to the list of padtemplates for the elementfactory.
247    * Note that the generated padtemplates are stored in static global
248    * variables, for the gst_dvdec_init function to use later on.
249    */
250   gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET(sink_temp));
251   gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET(video_src_temp));
252   gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET(audio_src_temp));
253
254   gst_element_class_set_details (element_class, &dvdec_details);
255 }
256
257 /* In order to create an instance of an object, the class must be
258  * initialized by this function.  GObject will take care of running
259  * it, based on the pointer to the function provided above.
260  */
261 static void
262 gst_dvdec_class_init (GstDVDecClass *klass)
263 {
264   /* Class pointers are needed to supply pointers to the private
265    * implementations of parent class methods.
266    */
267   GObjectClass *gobject_class;
268   GstElementClass *gstelement_class;
269
270   /* Since the dvdec class contains the parent classes, you can simply
271    * cast the pointer to get access to the parent classes.
272    */
273   gobject_class = (GObjectClass*) klass;
274   gstelement_class = (GstElementClass*) klass;
275
276   /* The parent class is needed for class method overrides. */
277   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
278
279   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_LUMA,
280     g_param_spec_boolean ("clamp_luma", "Clamp luma", "Clamp luma",
281                           FALSE, G_PARAM_READWRITE));
282   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_CHROMA,
283     g_param_spec_boolean ("clamp_chroma", "Clamp chroma", "Clamp chroma",
284                           FALSE, G_PARAM_READWRITE));
285   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
286     g_param_spec_flags ("quality", "Quality", "Decoding quality",
287                         GST_TYPE_DVDEC_QUALITY, DV_QUALITY_BEST, G_PARAM_READWRITE));
288
289   gobject_class->set_property = gst_dvdec_set_property;
290   gobject_class->get_property = gst_dvdec_get_property;
291   
292   gstelement_class->change_state = gst_dvdec_change_state;
293
294   /* table initialization, only do once */
295   dv_init(0, 0);
296 }
297
298 /* This function is responsible for initializing a specific instance of
299  * the plugin.
300  */
301 static void
302 gst_dvdec_init(GstDVDec *dvdec)
303 {
304   gint i;
305
306   dvdec->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_temp), "sink");
307   gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->sinkpad);
308   gst_pad_set_query_function (dvdec->sinkpad, NULL);
309   gst_pad_set_convert_function (dvdec->sinkpad, GST_DEBUG_FUNCPTR (gst_dvdec_sink_convert));
310   gst_pad_set_formats_function (dvdec->sinkpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
311
312   dvdec->videosrcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (video_src_temp), "video");
313   gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->videosrcpad);
314   gst_pad_set_query_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
315   gst_pad_set_query_type_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
316   gst_pad_set_event_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
317   gst_pad_set_event_mask_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
318   gst_pad_set_convert_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
319   gst_pad_set_formats_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
320
321   dvdec->audiosrcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET(audio_src_temp), "audio");
322   gst_element_add_pad(GST_ELEMENT(dvdec),dvdec->audiosrcpad);
323   gst_pad_set_query_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
324   gst_pad_set_query_type_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
325   gst_pad_set_event_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
326   gst_pad_set_event_mask_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
327   gst_pad_set_convert_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
328   gst_pad_set_formats_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
329
330   gst_element_set_loop_function (GST_ELEMENT (dvdec), gst_dvdec_loop);
331
332   dvdec->pool = NULL;
333   dvdec->length = 0;
334   dvdec->next_ts = 0LL;
335   dvdec->end_position = -1LL;
336   dvdec->need_discont = FALSE;
337   dvdec->framerate = 0;
338   dvdec->height = 0;
339   dvdec->clamp_luma = FALSE;
340   dvdec->clamp_chroma = FALSE;
341   dvdec->quality = DV_QUALITY_BEST;
342   dvdec->loop = FALSE;
343
344   for (i = 0; i <4; i++) {
345     dvdec->audio_buffers[i] = (gint16 *)g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
346   }
347 }
348
349 static const GstFormat*
350 gst_dvdec_get_formats (GstPad *pad)
351 {
352   static const GstFormat src_formats[] = {
353     GST_FORMAT_BYTES,
354     GST_FORMAT_DEFAULT,
355     GST_FORMAT_TIME,
356     0
357   };
358   static const GstFormat sink_formats[] = {
359     GST_FORMAT_BYTES,
360     GST_FORMAT_TIME,
361     0
362   };
363              
364   return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
365
366
367 static gboolean
368 gst_dvdec_src_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
369                        GstFormat *dest_format, gint64 *dest_value)
370 {
371   gboolean res = TRUE;
372   GstDVDec *dvdec;
373   gint scale = 1;
374         
375   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
376
377   if (dvdec->length == 0)
378     return FALSE;
379
380   if (dvdec->decoder == NULL)
381     return FALSE;
382
383   switch (src_format) {
384     case GST_FORMAT_BYTES:
385       switch (*dest_format) {
386         case GST_FORMAT_TIME:
387         default:
388           res = FALSE;
389       }
390       break;
391     case GST_FORMAT_TIME:
392       switch (*dest_format) {
393         case GST_FORMAT_BYTES:
394           if (pad == dvdec->videosrcpad)
395             scale = 720 * dvdec->height * dvdec->bpp;
396           else if (pad == dvdec->audiosrcpad)
397             scale = dvdec->decoder->audio->num_channels * 2;
398           /* fallthrough */
399         case GST_FORMAT_DEFAULT:
400           if (pad == dvdec->videosrcpad)
401             *dest_value = src_value * dvdec->framerate * scale / (GST_SECOND*100);
402           else if (pad == dvdec->audiosrcpad)
403             *dest_value = src_value * dvdec->decoder->audio->frequency * scale / GST_SECOND;
404           break;
405         default:
406           res = FALSE;
407       }
408       break;
409     default:
410       res = FALSE;
411   }
412   return res;
413 }
414
415 static gboolean
416 gst_dvdec_sink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
417                         GstFormat *dest_format, gint64 *dest_value)
418 {
419   gboolean res = TRUE;
420   GstDVDec *dvdec;
421         
422   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
423
424   if (dvdec->length == 0)
425     return FALSE;
426
427   switch (src_format) {
428     case GST_FORMAT_BYTES:
429       switch (*dest_format) {
430         case GST_FORMAT_TIME:
431         {
432           guint64 frame;
433           /* get frame number */
434           frame = src_value / dvdec->length;
435
436           *dest_value = (frame * GST_SECOND * 100) / dvdec->framerate;
437           break;
438         }
439         default:
440           res = FALSE;
441       }
442       break;
443     case GST_FORMAT_TIME:
444       switch (*dest_format) {
445         case GST_FORMAT_BYTES:
446         {
447           guint64 frame;
448           /* calculate the frame */
449           frame = src_value * dvdec->framerate / (GST_SECOND*100);
450           /* calculate the offset */
451           *dest_value = frame * dvdec->length;
452           break;
453         }
454         default:
455           res = FALSE;
456       }
457       break;
458     default:
459       res = FALSE;
460   }
461   return res;
462 }
463
464 static const GstQueryType*
465 gst_dvdec_get_src_query_types (GstPad *pad)
466 {   
467   static const GstQueryType src_query_types[] = {
468     GST_QUERY_TOTAL,
469     GST_QUERY_POSITION,
470     0
471   };
472   return src_query_types;
473 }
474
475 static gboolean
476 gst_dvdec_src_query (GstPad *pad, GstQueryType type,
477                      GstFormat *format, gint64 *value)
478 {
479   gboolean res = TRUE;
480   GstDVDec *dvdec;
481
482   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
483
484   switch (type) {
485     case GST_QUERY_TOTAL:
486       switch (*format) {
487         default:
488         {
489           guint64 len;
490           GstFormat tmp_format;
491
492           len = gst_bytestream_length (dvdec->bs);
493           tmp_format = GST_FORMAT_TIME;
494           if (len == -1 || !gst_pad_convert (dvdec->sinkpad, 
495                                              GST_FORMAT_BYTES, 
496                                              len, 
497                                              &tmp_format, value)) 
498           {
499             return FALSE;
500           }
501           if (!gst_pad_convert (pad, GST_FORMAT_TIME, *value, format, value)) {
502             return FALSE;
503           }
504           break;
505         }
506       }
507       break;
508     case GST_QUERY_POSITION:
509       switch (*format) {
510         default:
511           res = gst_pad_convert (pad, GST_FORMAT_TIME, dvdec->next_ts, format, value);
512           break;
513       }
514       break;
515     default:
516       res = FALSE;
517       break;
518   }
519   return res;
520
521
522 static const GstEventMask*
523 gst_dvdec_get_event_masks (GstPad *pad)
524
525   static const GstEventMask src_event_masks[] = {
526     { GST_EVENT_SEEK, GST_SEEK_METHOD_SET |
527                       GST_SEEK_FLAG_FLUSH },
528     { 0, }
529   };
530   static const GstEventMask sink_event_masks[] = {
531     { GST_EVENT_EOS, 0 },
532     { GST_EVENT_DISCONTINUOUS, 0 },
533     { GST_EVENT_FLUSH, 0 },
534     { 0, }
535   };
536
537   return (GST_PAD_IS_SRC (pad) ? src_event_masks : sink_event_masks);
538
539
540 static gboolean
541 gst_dvdec_handle_sink_event (GstDVDec *dvdec)
542 {
543   guint32 remaining;
544   GstEvent *event;
545   GstEventType type;
546                 
547   gst_bytestream_get_status (dvdec->bs, &remaining, &event);
548                   
549   type = event? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
550                       
551   switch (type) {
552     case GST_EVENT_EOS:
553       gst_pad_event_default (dvdec->sinkpad, event);
554       return TRUE;
555     case GST_EVENT_FLUSH:
556       break;
557     case GST_EVENT_DISCONTINUOUS:
558     {
559       gint i;
560       gboolean found = FALSE;
561       GstFormat format;
562
563       format = GST_FORMAT_TIME;
564       /* try to get a timestamp from the discont formats */
565       for (i = 0; i < GST_EVENT_DISCONT_OFFSET_LEN(event); i++) {
566         if (gst_pad_convert (dvdec->sinkpad, 
567                              GST_EVENT_DISCONT_OFFSET(event,i).format, 
568                              GST_EVENT_DISCONT_OFFSET(event,i).value,
569                              &format, &dvdec->next_ts)) 
570         {
571           found = TRUE;
572           break;
573         }
574       }
575       /* assume 0 then */
576       if (!found) {
577         dvdec->next_ts = 0LL;
578       }
579       dvdec->need_discont = TRUE;
580       break;
581     }
582     default:
583       g_warning ("unhandled event %d\n", type);
584       break;
585   }
586   gst_event_unref (event);
587   return TRUE;
588 }
589
590 static gboolean
591 gst_dvdec_handle_src_event (GstPad *pad, GstEvent *event)
592 {
593   gboolean res = TRUE;
594   GstDVDec *dvdec;
595
596   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
597  
598   switch (GST_EVENT_TYPE (event)) {
599     case GST_EVENT_SEEK_SEGMENT:
600     {
601       gint64 position;
602       GstFormat format;
603
604       /* first bring the format to time */
605       format = GST_FORMAT_TIME;
606       if (!gst_pad_convert (pad, 
607                             GST_EVENT_SEEK_FORMAT (event), 
608                             GST_EVENT_SEEK_ENDOFFSET (event),
609                             &format, &position)) 
610       {
611         /* could not convert seek format to time offset */
612         res = FALSE;
613         break;
614       }
615
616       dvdec->end_position = position;
617       dvdec->loop = GST_EVENT_SEEK_TYPE (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
618     }
619     case GST_EVENT_SEEK:
620     {
621       gint64 position;
622       GstFormat format;
623
624       /* first bring the format to time */
625       format = GST_FORMAT_TIME;
626       if (!gst_pad_convert (pad, 
627                             GST_EVENT_SEEK_FORMAT (event), 
628                             GST_EVENT_SEEK_OFFSET (event),
629                             &format, &position)) 
630       {
631         /* could not convert seek format to time offset */
632         res = FALSE;
633         break;
634       }
635       dvdec->next_ts = position;
636       /* then try to figure out the byteoffset for this time */
637       format = GST_FORMAT_BYTES;
638       if (!gst_pad_convert (dvdec->sinkpad, GST_FORMAT_TIME, position,
639                         &format, &position)) 
640       {
641         /* could not convert seek format to byte offset */
642         res = FALSE;
643         break;
644       }
645       /* seek to offset */
646       if (!gst_bytestream_seek (dvdec->bs, position, GST_SEEK_METHOD_SET)) {
647         res = FALSE;
648       }
649       if (GST_EVENT_TYPE (event) != GST_EVENT_SEEK_SEGMENT)
650         dvdec->end_position = -1;
651       break;
652     }
653     default:
654       res = FALSE;
655       break;
656   }
657   gst_event_unref (event);
658   return res;
659 }
660
661 static void
662 gst_dvdec_push (GstDVDec *dvdec, GstBuffer *outbuf, GstPad *pad, GstClockTime ts)
663 {   
664   GST_BUFFER_TIMESTAMP (outbuf) = ts;
665
666   if (dvdec->need_discont) {
667     GstEvent *discont;
668
669     discont = gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, ts, NULL);
670     gst_pad_push (pad, GST_DATA (discont));
671   }
672
673   gst_pad_push (pad, GST_DATA (outbuf));
674
675   if ((dvdec->end_position != -1) &&
676       (dvdec->next_ts >= dvdec->end_position)) {
677     if (dvdec->loop) 
678       gst_pad_push (pad, GST_DATA(gst_event_new (GST_EVENT_SEGMENT_DONE)));
679     else
680       gst_pad_push (pad, GST_DATA(gst_event_new (GST_EVENT_EOS)));
681   }
682 }
683
684 static void
685 gst_dvdec_loop (GstElement *element)
686 {   
687   GstDVDec *dvdec;
688   GstBuffer *buf, *outbuf;
689   guint8 *inframe;
690   gint height;
691   guint32 length, got_bytes;
692   GstFormat format;
693   guint64 ts;
694   gfloat fps;
695
696   dvdec = GST_DVDEC (element);
697
698   /* first read enough bytes to parse the header */
699   got_bytes = gst_bytestream_peek_bytes (dvdec->bs, &inframe, header_size);
700   if (got_bytes < header_size) {
701     gst_dvdec_handle_sink_event (dvdec);
702     return;
703   }
704   dv_parse_header (dvdec->decoder, inframe);
705   /* after parsing the header we know the size of the data */
706   dvdec->PAL = dv_system_50_fields (dvdec->decoder);
707
708   dvdec->framerate = (dvdec->PAL ? 2500 : 2997);
709   fps = (dvdec->PAL ? 25. : 30/1.001);
710   dvdec->height = height = (dvdec->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
711   length = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
712
713   if (length != dvdec->length) {
714     dvdec->length = length;
715     gst_bytestream_size_hint (dvdec->bs, length);
716   }
717
718   /* then read the read data */
719   got_bytes = gst_bytestream_read (dvdec->bs, &buf, length);
720   if (got_bytes < length) {
721     gst_dvdec_handle_sink_event (dvdec);
722     return;
723   }
724
725   /* if we did not negotiate yet, do it now */
726   if (!GST_PAD_CAPS (dvdec->videosrcpad)) {
727     GstCaps *allowed;
728     GstCaps *trylist;
729     
730     /* we what we are allowed to do */
731     allowed = gst_pad_get_allowed_caps (dvdec->videosrcpad);
732
733     /* try to fix our height */
734     trylist = gst_caps_intersect (allowed, gst_caps_append (
735             GST_CAPS_NEW (
736               "dvdec_negotiate",
737               "video/x-raw-yuv",
738                 "height",       GST_PROPS_INT (height),
739                 "framerate",    GST_PROPS_FLOAT (fps)
740             ), GST_CAPS_NEW (
741               "dvdec_negotiate",
742               "video/x-raw-rgb",
743                 "height",       GST_PROPS_INT (height),
744                 "framerate",    GST_PROPS_FLOAT (fps)
745             )));
746     
747     /* prepare for looping */
748     trylist = gst_caps_normalize (trylist);
749
750     while (trylist) {
751       GstCaps *to_try = gst_caps_copy_1 (trylist);
752
753       /* try each format */
754       if (gst_pad_try_set_caps (dvdec->videosrcpad, to_try) > 0) {
755         guint32 fourcc;
756
757         /* it worked, try to find what it was again */
758         gst_caps_get_fourcc_int (to_try, "format", &fourcc);
759
760         if (fourcc == GST_STR_FOURCC ("RGB ")) {
761           gint bpp;
762
763           gst_caps_get_int (to_try, "bpp", &bpp);
764           if (bpp == 24) {
765             dvdec->space = e_dv_color_rgb;
766             dvdec->bpp = 3;
767           }
768           else {
769             dvdec->space = e_dv_color_bgr0;
770             dvdec->bpp = 4;
771           }
772         }
773         else {
774           dvdec->space = e_dv_color_yuv;
775           dvdec->bpp = 2;
776         }
777         break;
778       }
779       trylist = trylist->next;
780     }
781     /* oops list exhausted an nothing was found... */
782     if (!trylist) {
783       gst_element_error (element, "could not negotiate");
784       return;
785     }
786   }
787
788   format = GST_FORMAT_TIME;
789   gst_pad_query (dvdec->videosrcpad, GST_QUERY_POSITION, &format, &ts);
790
791   dvdec->next_ts += (GST_SECOND*100) / dvdec->framerate;
792
793   if (GST_PAD_IS_LINKED (dvdec->audiosrcpad)) {
794     gint16 *a_ptr;
795     gint i, j;
796
797     dv_decode_full_audio (dvdec->decoder, GST_BUFFER_DATA (buf), dvdec->audio_buffers);
798
799     /* if we did not negotiate yet, do it now */
800     if (!GST_PAD_CAPS (dvdec->audiosrcpad)) {
801       gst_pad_try_set_caps (dvdec->audiosrcpad,
802         GST_CAPS_NEW (
803           "dvdec_audio_caps",
804           "audio/x-raw-int",
805             "rate",             GST_PROPS_INT (dvdec->decoder->audio->frequency),
806             "depth",            GST_PROPS_INT (16),
807             "width",            GST_PROPS_INT (16),
808             "signed",           GST_PROPS_BOOLEAN (TRUE),
809             "channels",         GST_PROPS_INT (dvdec->decoder->audio->num_channels),
810             "endianness",       GST_PROPS_INT (G_LITTLE_ENDIAN)
811           ));
812     }
813
814     outbuf = gst_buffer_new ();
815     GST_BUFFER_SIZE (outbuf) = dvdec->decoder->audio->samples_this_frame * 
816                                sizeof (gint16) * dvdec->decoder->audio->num_channels;
817     GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
818
819     a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);
820
821     for (i = 0; i < dvdec->decoder->audio->samples_this_frame; i++) {
822       for (j = 0; j < dvdec->decoder->audio->num_channels; j++) {
823         *(a_ptr++) = dvdec->audio_buffers[j][i];
824       }
825     }
826     gst_dvdec_push (dvdec, outbuf, dvdec->audiosrcpad, ts);
827   }
828
829   if (GST_PAD_IS_LINKED (dvdec->videosrcpad)) {
830     guint8 *outframe;
831     guint8 *outframe_ptrs[3];
832     gint outframe_pitches[3];
833
834     /* try to grab a pool */
835     if (!dvdec->pool) {
836       dvdec->pool = gst_pad_get_bufferpool (dvdec->videosrcpad);
837     }
838
839     outbuf = NULL;
840     /* try to get a buffer from the pool if we have one */
841     if (dvdec->pool) {
842       outbuf = gst_buffer_new_from_pool (dvdec->pool, 0, 0);
843     }
844     /* no buffer from pool, allocate one ourselves */
845     if (!outbuf) {
846       outbuf = gst_buffer_new ();
847     
848       GST_BUFFER_SIZE (outbuf) = (720 * height) * dvdec->bpp;
849       GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
850     }
851     
852     outframe = GST_BUFFER_DATA (outbuf);
853
854     outframe_ptrs[0] = outframe;
855     outframe_pitches[0] = 720 * dvdec->bpp;
856
857     /* the rest only matters for YUY2 */
858     if (dvdec->bpp < 3) {
859       outframe_ptrs[1] = outframe_ptrs[0] + 720 * height;
860       outframe_ptrs[2] = outframe_ptrs[1] + 360 * height;
861   
862       outframe_pitches[1] = height / 2;
863       outframe_pitches[2] = outframe_pitches[1];
864     }
865
866     dv_decode_full_frame (dvdec->decoder, GST_BUFFER_DATA (buf), 
867                           dvdec->space, outframe_ptrs, outframe_pitches);
868
869     gst_dvdec_push (dvdec, outbuf, dvdec->videosrcpad, ts);
870   }
871
872   if ((dvdec->end_position != -1) && 
873       (dvdec->next_ts >= dvdec->end_position) && 
874       !dvdec->loop) {
875     gst_element_set_eos (GST_ELEMENT (dvdec));
876   }
877
878   if (dvdec->need_discont) {
879     dvdec->need_discont = FALSE;
880   }
881
882   gst_buffer_unref (buf);
883 }
884
885 static GstElementStateReturn
886 gst_dvdec_change_state (GstElement *element)
887 {
888   GstDVDec *dvdec = GST_DVDEC (element);
889             
890   switch (GST_STATE_TRANSITION (element)) {
891     case GST_STATE_NULL_TO_READY:
892       break;
893     case GST_STATE_READY_TO_PAUSED:
894       dvdec->bs = gst_bytestream_new (dvdec->sinkpad);
895       dvdec->decoder = dv_decoder_new (0, dvdec->clamp_luma, dvdec->clamp_chroma);
896       dvdec->decoder->quality = dvdec->quality;
897       break;
898     case GST_STATE_PAUSED_TO_PLAYING:
899       break;
900     case GST_STATE_PLAYING_TO_PAUSED:
901       if (dvdec->pool)
902         gst_buffer_pool_unref (dvdec->pool);
903       dvdec->pool = NULL;
904       break;
905     case GST_STATE_PAUSED_TO_READY:
906       dv_decoder_free (dvdec->decoder);
907       dvdec->decoder = NULL;
908       gst_bytestream_destroy (dvdec->bs);
909       break;
910     case GST_STATE_READY_TO_NULL:
911       break;
912     default:
913       break;
914   }
915               
916   parent_class->change_state (element);
917                 
918   return GST_STATE_SUCCESS;
919 }
920
921 /* Arguments are part of the Gtk+ object system, and these functions
922  * enable the element to respond to various arguments.
923  */
924 static void
925 gst_dvdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
926 {
927   GstDVDec *dvdec;
928
929   /* It's not null if we got it, but it might not be ours */
930   g_return_if_fail(GST_IS_DVDEC(object));
931
932   /* Get a pointer of the right type. */
933   dvdec = GST_DVDEC(object);
934
935   /* Check the argument id to see which argument we're setting. */
936   switch (prop_id) {
937     case ARG_CLAMP_LUMA:
938       dvdec->clamp_luma = g_value_get_boolean (value);
939       break;
940     case ARG_CLAMP_CHROMA:
941       dvdec->clamp_chroma = g_value_get_boolean (value);
942       break;
943     case ARG_QUALITY:
944       dvdec->quality = g_value_get_flags (value);
945       break;
946     default:
947       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
948       break;
949   }
950 }
951
952 /* The set function is simply the inverse of the get fuction. */
953 static void
954 gst_dvdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
955 {
956   GstDVDec *dvdec;
957
958   /* It's not null if we got it, but it might not be ours */
959   g_return_if_fail(GST_IS_DVDEC(object));
960   dvdec = GST_DVDEC(object);
961
962   switch (prop_id) {
963     case ARG_CLAMP_LUMA:
964       g_value_set_boolean (value, dvdec->clamp_luma);
965       break;
966     case ARG_CLAMP_CHROMA:
967       g_value_set_boolean (value, dvdec->clamp_chroma);
968       break;
969     case ARG_QUALITY:
970       g_value_set_flags (value, dvdec->quality);
971       break;
972     default:
973       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
974       break;
975   }
976 }
977
978 /* This is the entry into the plugin itself.  When the plugin loads,
979  * this function is called to register everything that the plugin provides.
980  */
981 static gboolean
982 plugin_init (GstPlugin *plugin)
983 {
984   if (!gst_library_load ("gstbytestream"))
985     return FALSE;
986
987   if (!gst_element_register (plugin, "dvdec", GST_RANK_PRIMARY, gst_dvdec_get_type()))
988     return FALSE;
989
990   return TRUE;
991 }
992
993 GST_PLUGIN_DEFINE (
994   GST_VERSION_MAJOR,
995   GST_VERSION_MINOR,
996   "dvdec",
997   "Uses libdv to decode DV video (libdv.sourceforge.net)",
998   plugin_init,
999   VERSION,
1000   "LGPL",
1001   GST_PACKAGE,
1002   GST_ORIGIN
1003 );