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