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