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