Report which format was used for GST_FORMAT_DEFAULT in dvdec
[platform/upstream/gstreamer.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     };
161
162     qtype = g_enum_register_static ("GstDVDecQualityEnum", values);
163   }
164   return qtype;
165 }
166
167 /* A number of functon prototypes are given so we can refer to them later. */
168 static void gst_dvdec_base_init (gpointer g_class);
169 static void gst_dvdec_class_init (GstDVDecClass * klass);
170 static void gst_dvdec_init (GstDVDec * dvdec);
171
172 static const GstQueryType *gst_dvdec_get_src_query_types (GstPad * pad);
173 static gboolean gst_dvdec_src_query (GstPad * pad, GstQueryType type,
174     GstFormat * format, gint64 * value);
175 static const GstFormat *gst_dvdec_get_formats (GstPad * pad);
176 static gboolean gst_dvdec_sink_convert (GstPad * pad, GstFormat src_format,
177     gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
178 static gboolean gst_dvdec_src_convert (GstPad * pad, GstFormat src_format,
179     gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
180
181 static GstPadLinkReturn gst_dvdec_video_link (GstPad * pad,
182     const GstCaps * caps);
183 static GstCaps *gst_dvdec_video_getcaps (GstPad * pad);
184
185 static const GstEventMask *gst_dvdec_get_event_masks (GstPad * pad);
186 static gboolean gst_dvdec_handle_src_event (GstPad * pad, GstEvent * event);
187
188 static void gst_dvdec_loop (GstElement * element);
189
190 static GstElementStateReturn gst_dvdec_change_state (GstElement * element);
191
192 static void gst_dvdec_set_property (GObject * object, guint prop_id,
193     const GValue * value, GParamSpec * pspec);
194 static void gst_dvdec_get_property (GObject * object, guint prop_id,
195     GValue * value, GParamSpec * pspec);
196
197 /* The parent class pointer needs to be kept around for some object
198  * operations.
199  */
200 static GstElementClass *parent_class = NULL;
201
202 /* This array holds the ids of the signals registered for this object.
203  * The array indexes are based on the enum up above.
204  */
205 /*static guint gst_dvdec_signals[LAST_SIGNAL] = { 0 }; */
206
207 /* This function is used to register and subsequently return the type
208  * identifier for this object class.  On first invocation, it will
209  * register the type, providing the name of the class, struct sizes,
210  * and pointers to the various functions that define the class.
211  */
212 GType
213 gst_dvdec_get_type (void)
214 {
215   static GType dvdec_type = 0;
216
217   if (!dvdec_type) {
218     static const GTypeInfo dvdec_info = {
219       sizeof (GstDVDecClass),
220       gst_dvdec_base_init,
221       NULL,
222       (GClassInitFunc) gst_dvdec_class_init,
223       NULL,
224       NULL,
225       sizeof (GstDVDec),
226       0,
227       (GInstanceInitFunc) gst_dvdec_init,
228     };
229
230     dvdec_type =
231         g_type_register_static (GST_TYPE_ELEMENT, "GstDVDec", &dvdec_info, 0);
232   }
233   return dvdec_type;
234 }
235
236 static void
237 gst_dvdec_base_init (gpointer g_class)
238 {
239   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
240
241   /* The pad templates can be easily generated from the factories above,
242    * and then added to the list of padtemplates for the elementfactory.
243    * Note that the generated padtemplates are stored in static global
244    * variables, for the gst_dvdec_init function to use later on.
245    */
246   gst_element_class_add_pad_template (element_class,
247       gst_static_pad_template_get (&sink_temp));
248   gst_element_class_add_pad_template (element_class,
249       gst_static_pad_template_get (&video_src_temp));
250   gst_element_class_add_pad_template (element_class,
251       gst_static_pad_template_get (&audio_src_temp));
252
253   gst_element_class_set_details (element_class, &dvdec_details);
254 }
255
256 /* In order to create an instance of an object, the class must be
257  * initialized by this function.  GObject will take care of running
258  * it, based on the pointer to the function provided above.
259  */
260 static void
261 gst_dvdec_class_init (GstDVDecClass * klass)
262 {
263   /* Class pointers are needed to supply pointers to the private
264    * implementations of parent class methods.
265    */
266   GObjectClass *gobject_class;
267   GstElementClass *gstelement_class;
268
269   /* Since the dvdec class contains the parent classes, you can simply
270    * cast the pointer to get access to the parent classes.
271    */
272   gobject_class = (GObjectClass *) klass;
273   gstelement_class = (GstElementClass *) klass;
274
275   /* The parent class is needed for class method overrides. */
276   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
277
278   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_LUMA,
279       g_param_spec_boolean ("clamp_luma", "Clamp luma", "Clamp luma",
280           FALSE, G_PARAM_READWRITE));
281   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_CHROMA,
282       g_param_spec_boolean ("clamp_chroma", "Clamp chroma", "Clamp chroma",
283           FALSE, G_PARAM_READWRITE));
284   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
285       g_param_spec_enum ("quality", "Quality", "Decoding quality",
286           GST_TYPE_DVDEC_QUALITY, DV_QUALITY_DEFAULT, G_PARAM_READWRITE));
287
288   gobject_class->set_property = gst_dvdec_set_property;
289   gobject_class->get_property = gst_dvdec_get_property;
290
291   gstelement_class->change_state = gst_dvdec_change_state;
292
293   /* table initialization, only do once */
294   dv_init (0, 0);
295 }
296
297 /* This function is responsible for initializing a specific instance of
298  * the plugin.
299  */
300 static void
301 gst_dvdec_init (GstDVDec * dvdec)
302 {
303   gint i;
304
305   dvdec->found_header = FALSE;
306
307   dvdec->sinkpad =
308       gst_pad_new_from_template (gst_static_pad_template_get (&sink_temp),
309       "sink");
310   gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->sinkpad);
311   gst_pad_set_query_function (dvdec->sinkpad, NULL);
312   gst_pad_set_convert_function (dvdec->sinkpad,
313       GST_DEBUG_FUNCPTR (gst_dvdec_sink_convert));
314   gst_pad_set_formats_function (dvdec->sinkpad,
315       GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
316
317   dvdec->videosrcpad =
318       gst_pad_new_from_template (gst_static_pad_template_get (&video_src_temp),
319       "video");
320   gst_pad_set_query_function (dvdec->videosrcpad,
321       GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
322   gst_pad_set_query_type_function (dvdec->videosrcpad,
323       GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
324   gst_pad_set_event_function (dvdec->videosrcpad,
325       GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
326   gst_pad_set_event_mask_function (dvdec->videosrcpad,
327       GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
328   gst_pad_set_convert_function (dvdec->videosrcpad,
329       GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
330   gst_pad_set_formats_function (dvdec->videosrcpad,
331       GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
332   gst_pad_set_getcaps_function (dvdec->videosrcpad,
333       GST_DEBUG_FUNCPTR (gst_dvdec_video_getcaps));
334   gst_pad_set_link_function (dvdec->videosrcpad,
335       GST_DEBUG_FUNCPTR (gst_dvdec_video_link));
336   gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->videosrcpad);
337
338   dvdec->audiosrcpad =
339       gst_pad_new_from_template (gst_static_pad_template_get (&audio_src_temp),
340       "audio");
341   gst_pad_set_query_function (dvdec->audiosrcpad,
342       GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
343   gst_pad_set_query_type_function (dvdec->audiosrcpad,
344       GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
345   gst_pad_set_event_function (dvdec->audiosrcpad,
346       GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
347   gst_pad_set_event_mask_function (dvdec->audiosrcpad,
348       GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
349   gst_pad_set_convert_function (dvdec->audiosrcpad,
350       GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
351   gst_pad_set_formats_function (dvdec->audiosrcpad,
352       GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
353   gst_pad_use_explicit_caps (dvdec->audiosrcpad);
354   gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->audiosrcpad);
355
356   gst_element_set_loop_function (GST_ELEMENT (dvdec), gst_dvdec_loop);
357
358   dvdec->length = 0;
359   dvdec->next_ts = 0LL;
360   dvdec->end_position = -1LL;
361   dvdec->need_discont = FALSE;
362   dvdec->framerate = 0;
363   dvdec->height = 0;
364   dvdec->frequency = 0;
365   dvdec->channels = 0;
366
367   dvdec->clamp_luma = FALSE;
368   dvdec->clamp_chroma = FALSE;
369   dvdec->quality = DV_QUALITY_BEST;
370   dvdec->loop = FALSE;
371
372   for (i = 0; i < 4; i++) {
373     dvdec->audio_buffers[i] =
374         (gint16 *) g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
375   }
376 }
377
378 static const GstFormat *
379 gst_dvdec_get_formats (GstPad * pad)
380 {
381   static const GstFormat src_formats[] = {
382     GST_FORMAT_BYTES,
383     GST_FORMAT_DEFAULT,
384     GST_FORMAT_TIME,
385     0
386   };
387   static const GstFormat sink_formats[] = {
388     GST_FORMAT_BYTES,
389     GST_FORMAT_TIME,
390     0
391   };
392
393   return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
394 }
395
396 static gboolean
397 gst_dvdec_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
398     GstFormat * dest_format, gint64 * dest_value)
399 {
400   gboolean res = TRUE;
401   GstDVDec *dvdec;
402   gint scale = 1;
403
404   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
405
406   if (dvdec->length == 0)
407     return FALSE;
408
409   if (dvdec->decoder == NULL)
410     return FALSE;
411
412   switch (src_format) {
413     case GST_FORMAT_BYTES:
414       switch (*dest_format) {
415         case GST_FORMAT_TIME:
416         default:
417           res = FALSE;
418       }
419       break;
420     case GST_FORMAT_TIME:
421       switch (*dest_format) {
422         case GST_FORMAT_BYTES:
423           if (pad == dvdec->videosrcpad)
424             scale = 720 * dvdec->height * dvdec->bpp;
425           else if (pad == dvdec->audiosrcpad)
426             scale = dvdec->decoder->audio->num_channels * 2;
427           /* fallthrough */
428         case GST_FORMAT_DEFAULT:
429           *dest_format = GST_FORMAT_TIME;
430           if (pad == dvdec->videosrcpad)
431             *dest_value = src_value * dvdec->framerate * scale / GST_SECOND;
432           else if (pad == dvdec->audiosrcpad)
433             *dest_value =
434                 src_value * dvdec->decoder->audio->frequency * scale /
435                 GST_SECOND;
436           break;
437         default:
438           res = FALSE;
439       }
440       break;
441     default:
442       res = FALSE;
443   }
444   return res;
445 }
446
447 static gboolean
448 gst_dvdec_sink_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
449     GstFormat * dest_format, gint64 * dest_value)
450 {
451   gboolean res = TRUE;
452   GstDVDec *dvdec;
453
454   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
455
456   if (dvdec->length == 0)
457     return FALSE;
458
459   switch (src_format) {
460     case GST_FORMAT_BYTES:
461       switch (*dest_format) {
462         case GST_FORMAT_TIME:
463         {
464           guint64 frame;
465
466           /* get frame number */
467           frame = src_value / dvdec->length;
468
469           *dest_value = (frame * GST_SECOND) / dvdec->framerate;
470           break;
471         }
472         default:
473           res = FALSE;
474       }
475       break;
476     case GST_FORMAT_TIME:
477       switch (*dest_format) {
478         case GST_FORMAT_BYTES:
479         {
480           guint64 frame;
481
482           /* calculate the frame */
483           frame = src_value * dvdec->framerate / GST_SECOND;
484           /* calculate the offset */
485           *dest_value = frame * dvdec->length;
486           break;
487         }
488         default:
489           res = FALSE;
490       }
491       break;
492     default:
493       res = FALSE;
494   }
495   return res;
496 }
497
498 static const GstQueryType *
499 gst_dvdec_get_src_query_types (GstPad * pad)
500 {
501   static const GstQueryType src_query_types[] = {
502     GST_QUERY_TOTAL,
503     GST_QUERY_POSITION,
504     0
505   };
506
507   return src_query_types;
508 }
509
510 static gboolean
511 gst_dvdec_src_query (GstPad * pad, GstQueryType type,
512     GstFormat * format, gint64 * value)
513 {
514   gboolean res = TRUE;
515   GstDVDec *dvdec;
516
517   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
518
519   switch (type) {
520     case GST_QUERY_TOTAL:
521       switch (*format) {
522         default:
523         {
524           guint64 len;
525           GstFormat tmp_format;
526
527           len = gst_bytestream_length (dvdec->bs);
528           tmp_format = GST_FORMAT_TIME;
529           if (len == -1 || !gst_pad_convert (dvdec->sinkpad,
530                   GST_FORMAT_BYTES, len, &tmp_format, value)) {
531             return FALSE;
532           }
533           if (!gst_pad_convert (pad, GST_FORMAT_TIME, *value, format, value)) {
534             return FALSE;
535           }
536           break;
537         }
538       }
539       break;
540     case GST_QUERY_POSITION:
541       switch (*format) {
542         default:
543           res =
544               gst_pad_convert (pad, GST_FORMAT_TIME, dvdec->next_ts, format,
545               value);
546           break;
547       }
548       break;
549     default:
550       res = FALSE;
551       break;
552   }
553   return res;
554 }
555
556 static const GstEventMask *
557 gst_dvdec_get_event_masks (GstPad * pad)
558 {
559   static const GstEventMask src_event_masks[] = {
560     {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH},
561     {0,}
562   };
563   static const GstEventMask sink_event_masks[] = {
564     {GST_EVENT_EOS, 0},
565     {GST_EVENT_DISCONTINUOUS, 0},
566     {GST_EVENT_FLUSH, 0},
567     {0,}
568   };
569
570   return (GST_PAD_IS_SRC (pad) ? src_event_masks : sink_event_masks);
571 }
572
573 static gboolean
574 gst_dvdec_handle_sink_event (GstDVDec * dvdec)
575 {
576   guint32 remaining;
577   GstEvent *event;
578   GstEventType type;
579
580   gst_bytestream_get_status (dvdec->bs, &remaining, &event);
581
582   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
583
584   switch (type) {
585     case GST_EVENT_FLUSH:
586       break;
587     case GST_EVENT_DISCONTINUOUS:
588     {
589       gint i;
590       gboolean found = FALSE;
591       GstFormat format;
592
593       format = GST_FORMAT_TIME;
594       /* try to get a timestamp from the discont formats */
595       for (i = 0; i < GST_EVENT_DISCONT_OFFSET_LEN (event); i++) {
596         if (gst_pad_convert (dvdec->sinkpad,
597                 GST_EVENT_DISCONT_OFFSET (event, i).format,
598                 GST_EVENT_DISCONT_OFFSET (event, i).value,
599                 &format, &dvdec->next_ts)) {
600           found = TRUE;
601           break;
602         }
603       }
604       /* assume 0 then */
605       if (!found) {
606         dvdec->next_ts = 0LL;
607       }
608       dvdec->need_discont = TRUE;
609       break;
610     }
611     default:
612       return gst_pad_event_default (dvdec->sinkpad, event);
613       break;
614   }
615   gst_event_unref (event);
616   return TRUE;
617 }
618
619 static gboolean
620 gst_dvdec_handle_src_event (GstPad * pad, GstEvent * event)
621 {
622   gboolean res = TRUE;
623   GstDVDec *dvdec;
624
625   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
626
627   switch (GST_EVENT_TYPE (event)) {
628     case GST_EVENT_SEEK_SEGMENT:
629     {
630       gint64 position;
631       GstFormat format;
632
633       /* first bring the format to time */
634       format = GST_FORMAT_TIME;
635       if (!gst_pad_convert (pad,
636               GST_EVENT_SEEK_FORMAT (event),
637               GST_EVENT_SEEK_ENDOFFSET (event), &format, &position)) {
638         /* could not convert seek format to time offset */
639         res = FALSE;
640         break;
641       }
642
643       dvdec->end_position = position;
644       dvdec->loop = GST_EVENT_SEEK_TYPE (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
645     }
646     case GST_EVENT_SEEK:
647     {
648       gint64 position;
649       GstFormat format;
650
651       /* first bring the format to time */
652       format = GST_FORMAT_TIME;
653       if (!gst_pad_convert (pad,
654               GST_EVENT_SEEK_FORMAT (event),
655               GST_EVENT_SEEK_OFFSET (event), &format, &position)) {
656         /* could not convert seek format to time offset */
657         res = FALSE;
658         break;
659       }
660       dvdec->next_ts = position;
661       /* then try to figure out the byteoffset for this time */
662       format = GST_FORMAT_BYTES;
663       if (!gst_pad_convert (dvdec->sinkpad, GST_FORMAT_TIME, position,
664               &format, &position)) {
665         /* could not convert seek format to byte offset */
666         res = FALSE;
667         break;
668       }
669       /* seek to offset */
670       if (!gst_bytestream_seek (dvdec->bs, position, GST_SEEK_METHOD_SET)) {
671         res = FALSE;
672       }
673       if (GST_EVENT_TYPE (event) != GST_EVENT_SEEK_SEGMENT)
674         dvdec->end_position = -1;
675       break;
676     }
677     default:
678       res = FALSE;
679       break;
680   }
681   gst_event_unref (event);
682   return res;
683 }
684
685 static GstCaps *
686 gst_dvdec_video_getcaps (GstPad * pad)
687 {
688   GstDVDec *dvdec;
689   GstCaps *caps;
690   GstPadTemplate *src_pad_template;
691
692   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
693   src_pad_template = gst_static_pad_template_get (&video_src_temp);
694   caps = gst_caps_copy (gst_pad_template_get_caps (src_pad_template));
695
696   if (dvdec->found_header) {
697     int i;
698
699     /* set the height */
700     for (i = 0; i < gst_caps_get_size (caps); i++) {
701       GstStructure *structure = gst_caps_get_structure (caps, i);
702
703       gst_structure_set (structure,
704           "height", G_TYPE_INT, dvdec->height,
705           "framerate", G_TYPE_DOUBLE, dvdec->framerate, NULL);
706     }
707   }
708
709   return caps;
710 }
711
712 static GstPadLinkReturn
713 gst_dvdec_video_link (GstPad * pad, const GstCaps * caps)
714 {
715   GstDVDec *dvdec;
716   GstStructure *structure;
717   guint32 fourcc;
718   gint height;
719   gdouble framerate;
720
721   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
722
723   /* if we did not find a header yet, return delayed */
724   if (!dvdec->found_header) {
725     return GST_PAD_LINK_DELAYED;
726   }
727
728   structure = gst_caps_get_structure (caps, 0);
729
730   if (!gst_structure_get_int (structure, "height", &height) ||
731       !gst_structure_get_double (structure, "framerate", &framerate))
732     return GST_PAD_LINK_REFUSED;
733
734   if ((height != dvdec->height) || (framerate != dvdec->framerate))
735     return GST_PAD_LINK_REFUSED;
736
737   if (strcmp (gst_structure_get_name (structure), "video/x-raw-rgb") == 0) {
738     gint bpp;
739
740     gst_structure_get_int (structure, "bpp", &bpp);
741     if (bpp == 24) {
742       dvdec->space = e_dv_color_rgb;
743       dvdec->bpp = 3;
744     } else {
745       dvdec->space = e_dv_color_bgr0;
746       dvdec->bpp = 4;
747     }
748   } else {
749     if (!gst_structure_get_fourcc (structure, "format", &fourcc))
750       return GST_PAD_LINK_REFUSED;
751
752     dvdec->space = e_dv_color_yuv;
753     dvdec->bpp = 2;
754   }
755
756   return GST_PAD_LINK_OK;
757 }
758
759 static void
760 gst_dvdec_push (GstDVDec * dvdec, GstBuffer * outbuf, GstPad * pad,
761     GstClockTime ts)
762 {
763   GST_BUFFER_TIMESTAMP (outbuf) = ts;
764
765   if (dvdec->need_discont) {
766     GstEvent *discont;
767
768     discont = gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, ts, NULL);
769     gst_pad_push (pad, GST_DATA (discont));
770   }
771
772   gst_pad_push (pad, GST_DATA (outbuf));
773
774   if ((dvdec->end_position != -1) && (dvdec->next_ts >= dvdec->end_position)) {
775     if (dvdec->loop)
776       gst_pad_push (pad, GST_DATA (gst_event_new (GST_EVENT_SEGMENT_DONE)));
777     else
778       gst_pad_push (pad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
779   }
780 }
781
782 static void
783 gst_dvdec_loop (GstElement * element)
784 {
785   GstDVDec *dvdec;
786   GstBuffer *buf, *outbuf;
787   guint8 *inframe;
788   gint height;
789   guint32 length, got_bytes;
790   guint64 ts;
791   gdouble fps;
792
793   dvdec = GST_DVDEC (element);
794
795   /*
796    * Apparently dv_parse_header can read from the body of the frame
797    * too, so it needs more than header_size bytes. Wacky!
798    */
799   if (dvdec->found_header)
800     length = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
801   else
802     length = NTSC_BUFFER;
803
804   /* first read enough bytes to parse the header */
805   got_bytes = gst_bytestream_peek_bytes (dvdec->bs, &inframe, length);
806   if (got_bytes < length) {
807     gst_dvdec_handle_sink_event (dvdec);
808     return;
809   }
810   if (dv_parse_header (dvdec->decoder, inframe) < 0) {
811     GST_ELEMENT_ERROR (dvdec, STREAM, DECODE, (NULL), (NULL));
812     return;
813   }
814
815   /* after parsing the header we know the length of the data */
816   dvdec->PAL = dv_system_50_fields (dvdec->decoder);
817   dvdec->found_header = TRUE;
818
819   fps = (dvdec->PAL ? PAL_FRAMERATE : NTSC_FRAMERATE);
820   height = (dvdec->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
821   length = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
822
823   if (length != dvdec->length) {
824     dvdec->length = length;
825     gst_bytestream_size_hint (dvdec->bs, length);
826   }
827
828   /* then read the read data */
829   got_bytes = gst_bytestream_read (dvdec->bs, &buf, length);
830   if (got_bytes < length) {
831     gst_dvdec_handle_sink_event (dvdec);
832     return;
833   }
834
835   ts = dvdec->next_ts;
836   dvdec->next_ts += GST_SECOND / dvdec->framerate;
837
838   dv_parse_packs (dvdec->decoder, GST_BUFFER_DATA (buf));
839
840   if (GST_PAD_IS_LINKED (dvdec->audiosrcpad)) {
841     gint16 *a_ptr;
842     gint i, j;
843
844     dv_decode_full_audio (dvdec->decoder, GST_BUFFER_DATA (buf),
845         dvdec->audio_buffers);
846
847     if ((dvdec->decoder->audio->frequency != dvdec->frequency) ||
848         (dvdec->decoder->audio->num_channels != dvdec->channels)) {
849       if (!gst_pad_set_explicit_caps (dvdec->audiosrcpad,
850               gst_caps_new_simple ("audio/x-raw-int",
851                   "rate", G_TYPE_INT, dvdec->decoder->audio->frequency,
852                   "depth", G_TYPE_INT, 16,
853                   "width", G_TYPE_INT, 16,
854                   "signed", G_TYPE_BOOLEAN, TRUE,
855                   "channels", G_TYPE_INT, dvdec->decoder->audio->num_channels,
856                   "endianness", G_TYPE_INT, G_LITTLE_ENDIAN, NULL))) {
857         gst_buffer_unref (buf);
858         GST_ELEMENT_ERROR (dvdec, CORE, NEGOTIATION, (NULL), (NULL));
859         return;
860       }
861
862       dvdec->frequency = dvdec->decoder->audio->frequency;
863       dvdec->channels = dvdec->decoder->audio->num_channels;
864     }
865
866     outbuf = gst_buffer_new ();
867     GST_BUFFER_SIZE (outbuf) = dvdec->decoder->audio->samples_this_frame *
868         sizeof (gint16) * dvdec->decoder->audio->num_channels;
869     GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
870
871     a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);
872
873     for (i = 0; i < dvdec->decoder->audio->samples_this_frame; i++) {
874       for (j = 0; j < dvdec->decoder->audio->num_channels; j++) {
875         *(a_ptr++) = dvdec->audio_buffers[j][i];
876       }
877     }
878     gst_dvdec_push (dvdec, outbuf, dvdec->audiosrcpad, ts);
879   }
880
881   if (GST_PAD_IS_LINKED (dvdec->videosrcpad)) {
882     guint8 *outframe;
883     guint8 *outframe_ptrs[3];
884     gint outframe_pitches[3];
885
886     if ((dvdec->framerate != fps) || (dvdec->height != height)) {
887       dvdec->height = height;
888       dvdec->framerate = fps;
889
890       if (GST_PAD_LINK_FAILED (gst_pad_renegotiate (dvdec->videosrcpad))) {
891         GST_ELEMENT_ERROR (dvdec, CORE, NEGOTIATION, (NULL), (NULL));
892         return;
893       }
894     }
895
896     outbuf = gst_buffer_new_and_alloc ((720 * height) * dvdec->bpp);
897
898     outframe = GST_BUFFER_DATA (outbuf);
899
900     outframe_ptrs[0] = outframe;
901     outframe_pitches[0] = 720 * dvdec->bpp;
902
903     /* the rest only matters for YUY2 */
904     if (dvdec->bpp < 3) {
905       outframe_ptrs[1] = outframe_ptrs[0] + 720 * height;
906       outframe_ptrs[2] = outframe_ptrs[1] + 360 * height;
907
908       outframe_pitches[1] = height / 2;
909       outframe_pitches[2] = outframe_pitches[1];
910     }
911
912     dv_decode_full_frame (dvdec->decoder, GST_BUFFER_DATA (buf),
913         dvdec->space, outframe_ptrs, outframe_pitches);
914
915     gst_dvdec_push (dvdec, outbuf, dvdec->videosrcpad, ts);
916   }
917
918   if ((dvdec->end_position != -1) &&
919       (dvdec->next_ts >= dvdec->end_position) && !dvdec->loop) {
920     gst_element_set_eos (GST_ELEMENT (dvdec));
921   }
922
923   if (dvdec->need_discont) {
924     dvdec->need_discont = FALSE;
925   }
926
927   gst_buffer_unref (buf);
928 }
929
930 static GstElementStateReturn
931 gst_dvdec_change_state (GstElement * element)
932 {
933   GstDVDec *dvdec = GST_DVDEC (element);
934
935   switch (GST_STATE_TRANSITION (element)) {
936     case GST_STATE_NULL_TO_READY:
937       break;
938     case GST_STATE_READY_TO_PAUSED:
939       dvdec->bs = gst_bytestream_new (dvdec->sinkpad);
940       dvdec->decoder =
941           dv_decoder_new (0, dvdec->clamp_luma, dvdec->clamp_chroma);
942       dvdec->decoder->quality = qualities[dvdec->quality];
943       /* 
944        * Enable this function call when libdv2 0.100 or higher is more
945        * common
946        */
947       /* dv_set_quality (dvdec->decoder, qualities [dvdec->quality]); */
948       break;
949     case GST_STATE_PAUSED_TO_PLAYING:
950       break;
951     case GST_STATE_PLAYING_TO_PAUSED:
952       break;
953     case GST_STATE_PAUSED_TO_READY:
954       dv_decoder_free (dvdec->decoder);
955       dvdec->decoder = NULL;
956       dvdec->found_header = FALSE;
957       gst_bytestream_destroy (dvdec->bs);
958       break;
959     case GST_STATE_READY_TO_NULL:
960       break;
961     default:
962       break;
963   }
964
965   return parent_class->change_state (element);
966 }
967
968 /* Arguments are part of the Gtk+ object system, and these functions
969  * enable the element to respond to various arguments.
970  */
971 static void
972 gst_dvdec_set_property (GObject * object, guint prop_id, const GValue * value,
973     GParamSpec * pspec)
974 {
975   GstDVDec *dvdec;
976
977   /* It's not null if we got it, but it might not be ours */
978   g_return_if_fail (GST_IS_DVDEC (object));
979
980   /* Get a pointer of the right type. */
981   dvdec = GST_DVDEC (object);
982
983   /* Check the argument id to see which argument we're setting. */
984   switch (prop_id) {
985     case ARG_CLAMP_LUMA:
986       dvdec->clamp_luma = g_value_get_boolean (value);
987       break;
988     case ARG_CLAMP_CHROMA:
989       dvdec->clamp_chroma = g_value_get_boolean (value);
990       break;
991     case ARG_QUALITY:
992       dvdec->quality = g_value_get_enum (value);
993       if ((dvdec->quality < 0) || (dvdec->quality > 5))
994         dvdec->quality = 0;
995       break;
996     default:
997       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
998       break;
999   }
1000 }
1001
1002 /* The set function is simply the inverse of the get fuction. */
1003 static void
1004 gst_dvdec_get_property (GObject * object, guint prop_id, GValue * value,
1005     GParamSpec * pspec)
1006 {
1007   GstDVDec *dvdec;
1008
1009   /* It's not null if we got it, but it might not be ours */
1010   g_return_if_fail (GST_IS_DVDEC (object));
1011   dvdec = GST_DVDEC (object);
1012
1013   switch (prop_id) {
1014     case ARG_CLAMP_LUMA:
1015       g_value_set_boolean (value, dvdec->clamp_luma);
1016       break;
1017     case ARG_CLAMP_CHROMA:
1018       g_value_set_boolean (value, dvdec->clamp_chroma);
1019       break;
1020     case ARG_QUALITY:
1021       g_value_set_enum (value, dvdec->quality);
1022       break;
1023     default:
1024       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1025       break;
1026   }
1027 }
1028
1029 /* This is the entry into the plugin itself.  When the plugin loads,
1030  * this function is called to register everything that the plugin provides.
1031  */
1032 static gboolean
1033 plugin_init (GstPlugin * plugin)
1034 {
1035   if (!gst_library_load ("gstbytestream"))
1036     return FALSE;
1037
1038   if (!gst_element_register (plugin, "dvdec", GST_RANK_PRIMARY,
1039           gst_dvdec_get_type ()))
1040     return FALSE;
1041
1042   return TRUE;
1043 }
1044
1045 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1046     GST_VERSION_MINOR,
1047     "dvdec",
1048     "Uses libdv to decode DV video (libdv.sourceforge.net)",
1049     plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN);