Way, way, way too many files: Remove crack comment from the 2000 era.
[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 #include <math.h>
25
26 /* First, include the header file for the plugin, to bring in the
27  * object definition and other useful things.
28  */
29 #include "gstdvdec.h"
30
31 /* DV output has two modes, normal and wide. The resolution is the same in both
32  * cases: 720 pixels wide by 576 pixels tall in PAL format, and 720x480 for
33  * NTSC.
34  *
35  * Each of the modes has its own pixel aspect ratio, which is fixed in practice
36  * by ITU-R BT.601 (also known as "CCIR-601" or "Rec.601"). Or so claims a
37  * reference that I culled from the reliable "internet",
38  * http://www.mir.com/DMG/aspect.html. Normal PAL is 59/54 and normal NTSC is
39  * 10/11. Because the pixel resolution is the same for both cases, we can get
40  * the pixel aspect ratio for wide recordings by multiplying by the ratio of
41  * display aspect ratios, 16/9 (for wide) divided by 4/3 (for normal):
42  *
43  * Wide NTSC: 10/11 * (16/9)/(4/3) = 40/33
44  * Wide PAL: 59/54 * (16/9)/(4/3) = 118/81
45  *
46  * However, the pixel resolution coming out of a DV source does not combine with
47  * the standard pixel aspect ratios to give a proper display aspect ratio. An
48  * image 480 pixels tall, with a 4:3 display aspect ratio, will be 768 pixels
49  * wide. But, if we take the normal PAL aspect ratio of 59/54, and multiply it
50  * with the width of the DV image (720 pixels), we get 786.666..., which is
51  * nonintegral and too wide. The camera is not outputting a 4:3 image.
52  * 
53  * If the video sink for this stream has fixed dimensions (such as for
54  * fullscreen playback, or for a java applet in a web page), you then have two
55  * choices. Either you show the whole image, but pad the image with black
56  * borders on the top and bottom (like watching a widescreen video on a 4:3
57  * device), or you crop the video to the proper ratio. Apparently the latter is
58  * the standard practice.
59  *
60  * For its part, GStreamer is concerned with accuracy and preservation of
61  * information. This element outputs the 720x576 or 720x480 video that it
62  * recieves, noting the proper aspect ratio. This should not be a problem for
63  * windowed applications, which can change size to fit the video. Applications
64  * with fixed size requirements should decide whether to crop or pad. If you
65  * decide to crop, the videocrop element might be useful to you.
66  */
67
68 #define NTSC_HEIGHT 480
69 #define NTSC_BUFFER 120000
70 #define NTSC_FRAMERATE 30000/1001.
71
72 #define PAL_HEIGHT 576
73 #define PAL_BUFFER 144000
74 #define PAL_FRAMERATE 25.0
75
76 #define PAL_NORMAL_PAR_X        59
77 #define PAL_NORMAL_PAR_Y        54
78 #define PAL_WIDE_PAR_X          118
79 #define PAL_WIDE_PAR_Y          81
80
81 #define NTSC_NORMAL_PAR_X       10
82 #define NTSC_NORMAL_PAR_Y       11
83 #define NTSC_WIDE_PAR_X         40
84 #define NTSC_WIDE_PAR_Y         33
85
86 /* The ElementDetails structure gives a human-readable description
87  * of the plugin, as well as author and version data.
88  */
89 static GstElementDetails dvdec_details =
90 GST_ELEMENT_DETAILS ("DV (smpte314) decoder plugin",
91     "Codec/Decoder/Video",
92     "Uses libdv to decode DV video (libdv.sourceforge.net)",
93     "Erik Walthinsen <omega@cse.ogi.edu>\n" "Wim Taymans <wim.taymans@tvd.be>");
94
95
96 /* These are the signals that this element can fire.  They are zero-
97  * based because the numbers themselves are private to the object.
98  * LAST_SIGNAL is used for initialization of the signal array.
99  */
100 enum
101 {
102   /* FILL ME */
103   LAST_SIGNAL
104 };
105
106 #define DV_DEFAULT_QUALITY DV_QUALITY_BEST
107 #define DV_DEFAULT_DECODE_NTH 1
108
109 /* Arguments are identified the same way, but cannot be zero, so you
110  * must leave the ARG_0 entry in as a placeholder.
111  */
112 enum
113 {
114   ARG_0,
115   ARG_CLAMP_LUMA,
116   ARG_CLAMP_CHROMA,
117   ARG_QUALITY,
118   ARG_DECODE_NTH
119       /* FILL ME */
120 };
121
122 const gint qualities[] = {
123   DV_QUALITY_DC,
124   DV_QUALITY_AC_1,
125   DV_QUALITY_AC_2,
126   DV_QUALITY_DC | DV_QUALITY_COLOR,
127   DV_QUALITY_AC_1 | DV_QUALITY_COLOR,
128   DV_QUALITY_AC_2 | DV_QUALITY_COLOR
129 };
130
131 /* The PadFactory structures describe what pads the element has or
132  * can have.  They can be quite complex, but for this dvdec plugin
133  * they are rather simple.
134  */
135 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
136     GST_PAD_SINK,
137     GST_PAD_ALWAYS,
138     GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) true")
139     );
140
141 static GstStaticPadTemplate video_src_temp = GST_STATIC_PAD_TEMPLATE ("video",
142     GST_PAD_SRC,
143     GST_PAD_ALWAYS,
144     GST_STATIC_CAPS ("video/x-raw-yuv, "
145         "format = (fourcc) YUY2, "
146         "width = (int) 720, "
147         "height = (int) { "
148         G_STRINGIFY (NTSC_HEIGHT) ", " G_STRINGIFY (PAL_HEIGHT)
149         " }, "
150         "pixel-aspect-ratio=(fraction) { "
151         G_STRINGIFY (PAL_NORMAL_PAR_X) "/" G_STRINGIFY (PAL_NORMAL_PAR_Y) ","
152         G_STRINGIFY (PAL_WIDE_PAR_X) "/" G_STRINGIFY (PAL_WIDE_PAR_Y) ","
153         G_STRINGIFY (NTSC_NORMAL_PAR_X) "/" G_STRINGIFY (NTSC_NORMAL_PAR_Y) ","
154         G_STRINGIFY (NTSC_WIDE_PAR_X) "/" G_STRINGIFY (NTSC_WIDE_PAR_Y) "},"
155         "framerate = (double) [ 1.0, 60.0 ];"
156         //"framerate = (double) { "
157         //G_STRINGIFY (PAL_FRAMERATE) ", " G_STRINGIFY (NTSC_FRAMERATE)
158         //" }; "
159         "video/x-raw-rgb, "
160         "bpp = (int) 32, "
161         "depth = (int) 24, "
162         "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
163         "red_mask =   (int) 0x0000ff00, "
164         "green_mask = (int) 0x00ff0000, "
165         "blue_mask =  (int) 0xff000000, "
166         "width = (int) 720, "
167         "height = (int) { "
168         G_STRINGIFY (NTSC_HEIGHT) ", " G_STRINGIFY (PAL_HEIGHT)
169         " }, "
170         "pixel-aspect-ratio=(fraction) { "
171         G_STRINGIFY (PAL_NORMAL_PAR_X) "/" G_STRINGIFY (PAL_NORMAL_PAR_Y) ","
172         G_STRINGIFY (PAL_WIDE_PAR_X) "/" G_STRINGIFY (PAL_WIDE_PAR_Y) ","
173         G_STRINGIFY (NTSC_NORMAL_PAR_X) "/" G_STRINGIFY (NTSC_NORMAL_PAR_Y) ","
174         G_STRINGIFY (NTSC_WIDE_PAR_X) "/" G_STRINGIFY (NTSC_WIDE_PAR_Y) "},"
175         "framerate = (double) [ 1.0, 60.0 ];"
176         //"framerate = (double) { "
177         //G_STRINGIFY (PAL_FRAMERATE) ", " G_STRINGIFY (NTSC_FRAMERATE)
178         //" }; "
179         "video/x-raw-rgb, "
180         "bpp = (int) 24, "
181         "depth = (int) 24, "
182         "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
183         "red_mask =   (int) 0x00ff0000, "
184         "green_mask = (int) 0x0000ff00, "
185         "blue_mask =  (int) 0x000000ff, "
186         "width = (int) 720, "
187         "height = (int) { "
188         G_STRINGIFY (NTSC_HEIGHT) ", " G_STRINGIFY (PAL_HEIGHT)
189         " }, "
190         "pixel-aspect-ratio=(fraction) { "
191         G_STRINGIFY (PAL_NORMAL_PAR_X) "/" G_STRINGIFY (PAL_NORMAL_PAR_Y) ","
192         G_STRINGIFY (PAL_WIDE_PAR_X) "/" G_STRINGIFY (PAL_WIDE_PAR_Y) ","
193         G_STRINGIFY (NTSC_NORMAL_PAR_X) "/" G_STRINGIFY (NTSC_NORMAL_PAR_Y) ","
194         G_STRINGIFY (NTSC_WIDE_PAR_X) "/" G_STRINGIFY (NTSC_WIDE_PAR_Y) "},"
195         "framerate = (double) [ 1.0, 60.0 ]"
196         //"framerate = (double) { "
197         //G_STRINGIFY (PAL_FRAMERATE) ", " G_STRINGIFY (NTSC_FRAMERATE)
198         //" }"
199     )
200     );
201
202 static GstStaticPadTemplate audio_src_temp = GST_STATIC_PAD_TEMPLATE ("audio",
203     GST_PAD_SRC,
204     GST_PAD_ALWAYS,
205     GST_STATIC_CAPS ("audio/x-raw-int, "
206         "depth = (int) 16, "
207         "width = (int) 16, "
208         "signed = (boolean) TRUE, "
209         "channels = (int) {2, 4}, "
210         "endianness = (int) " G_STRINGIFY (G_LITTLE_ENDIAN) ", "
211         "rate = (int) { 32000, 44100, 48000 }")
212     );
213
214 #define GST_TYPE_DVDEC_QUALITY (gst_dvdec_quality_get_type())
215 GType
216 gst_dvdec_quality_get_type (void)
217 {
218   static GType qtype = 0;
219
220   if (qtype == 0) {
221     static const GEnumValue values[] = {
222       {0, "DV_QUALITY_FASTEST", "Fastest decoding, low-quality mono"},
223       {1, "DV_QUALITY_AC_1", "Mono decoding using the first AC coefficient"},
224       {2, "DV_QUALITY_AC_2", "Highest quality mono decoding"},
225       {3, "DV_QUALITY_DC|DV_QUALITY_COLOUR", "Fastest colour decoding"},
226       {4, "DV_QUALITY_AC_1|DV_QUALITY_COLOUR",
227           "Colour, using only the first AC coefficient"},
228       {5, "DV_QUALITY_BEST", "Highest quality colour decoding"},
229       {0, NULL, NULL},
230     };
231
232     qtype = g_enum_register_static ("GstDVDecQualityEnum", values);
233   }
234   return qtype;
235 }
236
237 /* A number of functon prototypes are given so we can refer to them later. */
238 static void gst_dvdec_base_init (gpointer g_class);
239 static void gst_dvdec_class_init (GstDVDecClass * klass);
240 static void gst_dvdec_init (GstDVDec * dvdec);
241
242 static const GstQueryType *gst_dvdec_get_src_query_types (GstPad * pad);
243 static gboolean gst_dvdec_src_query (GstPad * pad, GstQueryType type,
244     GstFormat * format, gint64 * value);
245 static const GstFormat *gst_dvdec_get_formats (GstPad * pad);
246 static gboolean gst_dvdec_sink_convert (GstPad * pad, GstFormat src_format,
247     gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
248 static gboolean gst_dvdec_src_convert (GstPad * pad, GstFormat src_format,
249     gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
250
251 static GstPadLinkReturn gst_dvdec_video_link (GstPad * pad,
252     const GstCaps * caps);
253 static GstCaps *gst_dvdec_video_getcaps (GstPad * pad);
254
255 static const GstEventMask *gst_dvdec_get_event_masks (GstPad * pad);
256 static gboolean gst_dvdec_handle_src_event (GstPad * pad, GstEvent * event);
257
258 static void gst_dvdec_loop (GstElement * element);
259
260 static GstElementStateReturn gst_dvdec_change_state (GstElement * element);
261
262 static void gst_dvdec_set_property (GObject * object, guint prop_id,
263     const GValue * value, GParamSpec * pspec);
264 static void gst_dvdec_get_property (GObject * object, guint prop_id,
265     GValue * value, GParamSpec * pspec);
266
267 /* The parent class pointer needs to be kept around for some object
268  * operations.
269  */
270 static GstElementClass *parent_class = NULL;
271
272 /* This array holds the ids of the signals registered for this object.
273  * The array indexes are based on the enum up above.
274  */
275 /*static guint gst_dvdec_signals[LAST_SIGNAL] = { 0 }; */
276
277 /* This function is used to register and subsequently return the type
278  * identifier for this object class.  On first invocation, it will
279  * register the type, providing the name of the class, struct sizes,
280  * and pointers to the various functions that define the class.
281  */
282 GType
283 gst_dvdec_get_type (void)
284 {
285   static GType dvdec_type = 0;
286
287   if (!dvdec_type) {
288     static const GTypeInfo dvdec_info = {
289       sizeof (GstDVDecClass),
290       gst_dvdec_base_init,
291       NULL,
292       (GClassInitFunc) gst_dvdec_class_init,
293       NULL,
294       NULL,
295       sizeof (GstDVDec),
296       0,
297       (GInstanceInitFunc) gst_dvdec_init,
298     };
299
300     dvdec_type =
301         g_type_register_static (GST_TYPE_ELEMENT, "GstDVDec", &dvdec_info, 0);
302   }
303   return dvdec_type;
304 }
305
306 static void
307 gst_dvdec_base_init (gpointer g_class)
308 {
309   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
310
311   /* The pad templates can be easily generated from the factories above,
312    * and then added to the list of padtemplates for the elementfactory.
313    * Note that the generated padtemplates are stored in static global
314    * variables, for the gst_dvdec_init function to use later on.
315    */
316   gst_element_class_add_pad_template (element_class,
317       gst_static_pad_template_get (&sink_temp));
318   gst_element_class_add_pad_template (element_class,
319       gst_static_pad_template_get (&video_src_temp));
320   gst_element_class_add_pad_template (element_class,
321       gst_static_pad_template_get (&audio_src_temp));
322
323   gst_element_class_set_details (element_class, &dvdec_details);
324 }
325
326 /* In order to create an instance of an object, the class must be
327  * initialized by this function.  GObject will take care of running
328  * it, based on the pointer to the function provided above.
329  */
330 static void
331 gst_dvdec_class_init (GstDVDecClass * klass)
332 {
333   /* Class pointers are needed to supply pointers to the private
334    * implementations of parent class methods.
335    */
336   GObjectClass *gobject_class;
337   GstElementClass *gstelement_class;
338
339   /* Since the dvdec class contains the parent classes, you can simply
340    * cast the pointer to get access to the parent classes.
341    */
342   gobject_class = (GObjectClass *) klass;
343   gstelement_class = (GstElementClass *) klass;
344
345   /* The parent class is needed for class method overrides. */
346   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
347
348   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_LUMA,
349       g_param_spec_boolean ("clamp_luma", "Clamp luma", "Clamp luma",
350           FALSE, G_PARAM_READWRITE));
351   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_CHROMA,
352       g_param_spec_boolean ("clamp_chroma", "Clamp chroma", "Clamp chroma",
353           FALSE, G_PARAM_READWRITE));
354   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
355       g_param_spec_enum ("quality", "Quality", "Decoding quality",
356           GST_TYPE_DVDEC_QUALITY, DV_DEFAULT_QUALITY, G_PARAM_READWRITE));
357   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DECODE_NTH,
358       g_param_spec_int ("drop-factor", "Drop Factor", "Only decode Nth frame",
359           1, G_MAXINT, DV_DEFAULT_DECODE_NTH, G_PARAM_READWRITE));
360
361   gobject_class->set_property = gst_dvdec_set_property;
362   gobject_class->get_property = gst_dvdec_get_property;
363
364   gstelement_class->change_state = gst_dvdec_change_state;
365
366   /* table initialization, only do once */
367   dv_init (0, 0);
368 }
369
370 /* This function is responsible for initializing a specific instance of
371  * the plugin.
372  */
373 static void
374 gst_dvdec_init (GstDVDec * dvdec)
375 {
376   gint i;
377
378   dvdec->found_header = FALSE;
379
380   dvdec->sinkpad =
381       gst_pad_new_from_template (gst_static_pad_template_get (&sink_temp),
382       "sink");
383   gst_pad_set_query_function (dvdec->sinkpad, NULL);
384   gst_pad_set_convert_function (dvdec->sinkpad,
385       GST_DEBUG_FUNCPTR (gst_dvdec_sink_convert));
386   gst_pad_set_formats_function (dvdec->sinkpad,
387       GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
388   gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->sinkpad);
389
390   dvdec->videosrcpad =
391       gst_pad_new_from_template (gst_static_pad_template_get (&video_src_temp),
392       "video");
393   gst_pad_set_query_function (dvdec->videosrcpad,
394       GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
395   gst_pad_set_query_type_function (dvdec->videosrcpad,
396       GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
397   gst_pad_set_event_function (dvdec->videosrcpad,
398       GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
399   gst_pad_set_event_mask_function (dvdec->videosrcpad,
400       GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
401   gst_pad_set_convert_function (dvdec->videosrcpad,
402       GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
403   gst_pad_set_formats_function (dvdec->videosrcpad,
404       GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
405   gst_pad_set_getcaps_function (dvdec->videosrcpad,
406       GST_DEBUG_FUNCPTR (gst_dvdec_video_getcaps));
407   gst_pad_set_link_function (dvdec->videosrcpad,
408       GST_DEBUG_FUNCPTR (gst_dvdec_video_link));
409   gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->videosrcpad);
410
411   dvdec->audiosrcpad =
412       gst_pad_new_from_template (gst_static_pad_template_get (&audio_src_temp),
413       "audio");
414   gst_pad_set_query_function (dvdec->audiosrcpad,
415       GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
416   gst_pad_set_query_type_function (dvdec->audiosrcpad,
417       GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
418   gst_pad_set_event_function (dvdec->audiosrcpad,
419       GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
420   gst_pad_set_event_mask_function (dvdec->audiosrcpad,
421       GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
422   gst_pad_set_convert_function (dvdec->audiosrcpad,
423       GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
424   gst_pad_set_formats_function (dvdec->audiosrcpad,
425       GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
426   gst_pad_use_explicit_caps (dvdec->audiosrcpad);
427   gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->audiosrcpad);
428
429   gst_element_set_loop_function (GST_ELEMENT (dvdec), gst_dvdec_loop);
430
431   dvdec->bs = NULL;
432   dvdec->length = 0;
433   dvdec->next_ts = 0LL;
434   dvdec->end_position = -1LL;
435   dvdec->need_discont = FALSE;
436   dvdec->new_media = FALSE;
437   dvdec->framerate = 0;
438   dvdec->height = 0;
439   dvdec->frequency = 0;
440   dvdec->channels = 0;
441   dvdec->wide = FALSE;
442   dvdec->drop_factor = 1;
443
444   dvdec->clamp_luma = FALSE;
445   dvdec->clamp_chroma = FALSE;
446   dvdec->quality = DV_DEFAULT_QUALITY;
447   dvdec->loop = FALSE;
448
449   for (i = 0; i < 4; i++) {
450     dvdec->audio_buffers[i] =
451         (gint16 *) g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
452   }
453 }
454
455 static const GstFormat *
456 gst_dvdec_get_formats (GstPad * pad)
457 {
458   static const GstFormat src_formats[] = {
459     GST_FORMAT_BYTES,
460     GST_FORMAT_DEFAULT,
461     GST_FORMAT_TIME,
462     0
463   };
464   static const GstFormat sink_formats[] = {
465     GST_FORMAT_BYTES,
466     GST_FORMAT_TIME,
467     GST_FORMAT_DEFAULT,
468     0
469   };
470
471   return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
472 }
473
474 static gboolean
475 gst_dvdec_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
476     GstFormat * dest_format, gint64 * dest_value)
477 {
478   gboolean res = TRUE;
479   GstDVDec *dvdec;
480
481   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
482   if (dvdec->length == 0)
483     return FALSE;
484
485   if (dvdec->decoder == NULL)
486     return FALSE;
487
488   switch (src_format) {
489     case GST_FORMAT_BYTES:
490       switch (*dest_format) {
491         case GST_FORMAT_BYTES:
492           *dest_value = src_value;
493           break;
494         case GST_FORMAT_DEFAULT:
495         case GST_FORMAT_TIME:
496           *dest_format = GST_FORMAT_TIME;
497           if (pad == dvdec->videosrcpad)
498             *dest_value = src_value * GST_SECOND /
499                 (720 * dvdec->height * dvdec->bpp * dvdec->framerate /
500                 GST_SECOND);
501           else if (pad == dvdec->audiosrcpad)
502             *dest_value = src_value * GST_SECOND /
503                 (2 * dvdec->frequency * dvdec->channels);
504           break;
505         default:
506           res = FALSE;
507       }
508       break;
509     case GST_FORMAT_TIME:
510       switch (*dest_format) {
511         case GST_FORMAT_BYTES:
512           if (pad == dvdec->videosrcpad)
513             *dest_value = src_value * 720 * dvdec->height * dvdec->bpp *
514                 dvdec->framerate / GST_SECOND;
515           else if (pad == dvdec->audiosrcpad)
516             *dest_value = 2 * src_value * dvdec->frequency *
517                 dvdec->channels / GST_SECOND;
518           break;
519         case GST_FORMAT_TIME:
520         case GST_FORMAT_DEFAULT:
521           *dest_format = GST_FORMAT_TIME;
522           *dest_value = src_value;
523           break;
524         default:
525           res = FALSE;
526       }
527       break;
528     default:
529       res = FALSE;
530   }
531   return res;
532 }
533
534 static gboolean
535 gst_dvdec_sink_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
536     GstFormat * dest_format, gint64 * dest_value)
537 {
538   gboolean res = TRUE;
539   GstDVDec *dvdec;
540
541   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
542
543   if (dvdec->length == 0)
544     return FALSE;
545
546   if (*dest_format == GST_FORMAT_DEFAULT)
547     *dest_format = GST_FORMAT_TIME;
548
549   switch (src_format) {
550     case GST_FORMAT_BYTES:
551       switch (*dest_format) {
552         case GST_FORMAT_TIME:
553         {
554           guint64 frame;
555
556           /* get frame number */
557           frame = src_value / dvdec->length;
558
559           *dest_value = (frame * GST_SECOND) / dvdec->framerate;
560           break;
561         }
562         case GST_FORMAT_BYTES:
563           *dest_value = src_value;
564           break;
565         default:
566           res = FALSE;
567       }
568       break;
569     case GST_FORMAT_TIME:
570       switch (*dest_format) {
571         case GST_FORMAT_BYTES:
572         {
573           guint64 frame;
574
575           /* calculate the frame */
576           frame = src_value * dvdec->framerate / GST_SECOND;
577           /* calculate the offset */
578           *dest_value = frame * dvdec->length;
579           break;
580         }
581         case GST_FORMAT_TIME:
582           *dest_value = src_value;
583           break;
584         default:
585           res = FALSE;
586       }
587       break;
588     default:
589       res = FALSE;
590   }
591   return res;
592 }
593
594 static const GstQueryType *
595 gst_dvdec_get_src_query_types (GstPad * pad)
596 {
597   static const GstQueryType src_query_types[] = {
598     GST_QUERY_TOTAL,
599     GST_QUERY_POSITION,
600     0
601   };
602
603   return src_query_types;
604 }
605
606 static gboolean
607 gst_dvdec_src_query (GstPad * pad, GstQueryType type,
608     GstFormat * format, gint64 * value)
609 {
610   gboolean res = TRUE;
611   GstDVDec *dvdec;
612
613   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
614
615   switch (type) {
616     case GST_QUERY_TOTAL:
617       switch (*format) {
618         default:
619         {
620           guint64 len;
621           GstFormat tmp_format;
622
623           if (!dvdec->bs)
624             return FALSE;
625
626           len = gst_bytestream_length (dvdec->bs);
627           tmp_format = GST_FORMAT_TIME;
628           if (len == -1 || !gst_pad_convert (dvdec->sinkpad,
629                   GST_FORMAT_BYTES, len, &tmp_format, value)) {
630             return FALSE;
631           }
632           if (!gst_pad_convert (pad, GST_FORMAT_TIME, *value, format, value)) {
633             return FALSE;
634           }
635           break;
636         }
637       }
638       break;
639     case GST_QUERY_POSITION:
640       switch (*format) {
641         default:
642           res =
643               gst_pad_convert (pad, GST_FORMAT_TIME, dvdec->next_ts, format,
644               value);
645           break;
646       }
647       break;
648     default:
649       res = FALSE;
650       break;
651   }
652   return res;
653 }
654
655 static const GstEventMask *
656 gst_dvdec_get_event_masks (GstPad * pad)
657 {
658   static const GstEventMask src_event_masks[] = {
659     {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH},
660     {0,}
661   };
662   static const GstEventMask sink_event_masks[] = {
663     {GST_EVENT_EOS, 0},
664     {GST_EVENT_DISCONTINUOUS, 0},
665     {GST_EVENT_FLUSH, 0},
666     {0,}
667   };
668
669   return (GST_PAD_IS_SRC (pad) ? src_event_masks : sink_event_masks);
670 }
671
672 static gboolean
673 gst_dvdec_handle_sink_event (GstDVDec * dvdec)
674 {
675   guint32 remaining;
676   GstEvent *event;
677   GstEventType type;
678
679   gst_bytestream_get_status (dvdec->bs, &remaining, &event);
680
681   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
682
683   switch (type) {
684     case GST_EVENT_FLUSH:
685     case GST_EVENT_EOS:
686     case GST_EVENT_FILLER:
687     {
688       /* Forward the event to output sinks */
689       if (GST_PAD_IS_LINKED (dvdec->videosrcpad)) {
690         gst_event_ref (event);
691         gst_pad_push (dvdec->videosrcpad, GST_DATA (event));
692       }
693       if (GST_PAD_IS_LINKED (dvdec->audiosrcpad)) {
694         gst_event_ref (event);
695         gst_pad_push (dvdec->audiosrcpad, GST_DATA (event));
696       }
697       if (type == GST_EVENT_EOS) {
698         gst_element_set_eos (GST_ELEMENT (dvdec));
699       }
700       break;
701     }
702     case GST_EVENT_DISCONTINUOUS:
703     {
704       gint i;
705       gboolean found = FALSE;
706       GstFormat format;
707
708       format = GST_FORMAT_TIME;
709       /* try to get a timestamp from the discont formats */
710       for (i = 0; i < GST_EVENT_DISCONT_OFFSET_LEN (event); i++) {
711         if (gst_pad_convert (dvdec->sinkpad,
712                 GST_EVENT_DISCONT_OFFSET (event, i).format,
713                 GST_EVENT_DISCONT_OFFSET (event, i).value,
714                 &format, &dvdec->next_ts)) {
715           found = TRUE;
716           break;
717         }
718       }
719       /* assume 0 then */
720       if (!found) {
721         dvdec->next_ts = 0LL;
722       }
723       dvdec->need_discont = TRUE;
724       break;
725     }
726     default:
727       return gst_pad_event_default (dvdec->sinkpad, event);
728       break;
729   }
730   gst_event_unref (event);
731   return TRUE;
732 }
733
734 static gboolean
735 gst_dvdec_handle_src_event (GstPad * pad, GstEvent * event)
736 {
737   gboolean res = TRUE;
738   GstDVDec *dvdec;
739
740   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
741
742   switch (GST_EVENT_TYPE (event)) {
743     case GST_EVENT_SEEK_SEGMENT:
744     {
745       gint64 position;
746       GstFormat format;
747
748       /* first bring the format to time */
749       format = GST_FORMAT_TIME;
750       if (!gst_pad_convert (pad,
751               GST_EVENT_SEEK_FORMAT (event),
752               GST_EVENT_SEEK_ENDOFFSET (event), &format, &position)) {
753         /* could not convert seek format to time offset */
754         res = FALSE;
755         break;
756       }
757
758       dvdec->end_position = position;
759       dvdec->loop = GST_EVENT_SEEK_TYPE (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
760     }
761     case GST_EVENT_SEEK:
762     {
763       gint64 position;
764       GstFormat format;
765
766       /* first bring the format to time */
767       format = GST_FORMAT_TIME;
768       if (!gst_pad_convert (pad,
769               GST_EVENT_SEEK_FORMAT (event),
770               GST_EVENT_SEEK_OFFSET (event), &format, &position)) {
771         /* could not convert seek format to time offset */
772         res = FALSE;
773         break;
774       }
775       dvdec->next_ts = position;
776       /* then try to figure out the byteoffset for this time */
777       format = GST_FORMAT_BYTES;
778       if (!gst_pad_convert (dvdec->sinkpad, GST_FORMAT_TIME, position,
779               &format, &position)) {
780         /* could not convert seek format to byte offset */
781         res = FALSE;
782         break;
783       }
784       /* seek to offset */
785       if (!gst_bytestream_seek (dvdec->bs, position, GST_SEEK_METHOD_SET)) {
786         res = FALSE;
787       }
788       if (GST_EVENT_TYPE (event) != GST_EVENT_SEEK_SEGMENT)
789         dvdec->end_position = -1;
790       break;
791     }
792     default:
793       res = FALSE;
794       break;
795   }
796   gst_event_unref (event);
797   return res;
798 }
799
800 static GstCaps *
801 gst_dvdec_video_getcaps (GstPad * pad)
802 {
803   GstDVDec *dvdec;
804   GstCaps *caps;
805   GstPadTemplate *src_pad_template;
806
807   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
808   src_pad_template = gst_static_pad_template_get (&video_src_temp);
809   caps = gst_caps_copy (gst_pad_template_get_caps (src_pad_template));
810
811   if (dvdec->found_header) {
812     int i;
813     gint par_x, par_y;
814
815     if (dvdec->PAL) {
816       if (dvdec->wide) {
817         par_x = PAL_WIDE_PAR_X;
818         par_y = PAL_WIDE_PAR_Y;
819       } else {
820         par_x = PAL_NORMAL_PAR_X;
821         par_y = PAL_NORMAL_PAR_Y;
822       }
823     } else {
824       if (dvdec->wide) {
825         par_x = NTSC_WIDE_PAR_X;
826         par_y = NTSC_WIDE_PAR_Y;
827       } else {
828         par_x = NTSC_NORMAL_PAR_X;
829         par_y = NTSC_NORMAL_PAR_Y;
830       }
831     }
832     /* set the height */
833     for (i = 0; i < gst_caps_get_size (caps); i++) {
834       GstStructure *structure = gst_caps_get_structure (caps, i);
835
836       gst_structure_set (structure,
837           "height", G_TYPE_INT, dvdec->height,
838           "framerate", G_TYPE_DOUBLE, dvdec->framerate / dvdec->drop_factor,
839           "pixel-aspect-ratio", GST_TYPE_FRACTION, par_x, par_y, NULL);
840     }
841   }
842
843   return caps;
844 }
845
846 static GstPadLinkReturn
847 gst_dvdec_video_link (GstPad * pad, const GstCaps * caps)
848 {
849   GstDVDec *dvdec;
850   GstStructure *structure;
851   guint32 fourcc;
852   gint height;
853   gdouble framerate;
854
855   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
856
857   /* if we did not find a header yet, return delayed */
858   if (!dvdec->found_header) {
859     return GST_PAD_LINK_DELAYED;
860   }
861
862   structure = gst_caps_get_structure (caps, 0);
863
864   if (!gst_structure_get_int (structure, "height", &height) ||
865       !gst_structure_get_double (structure, "framerate", &framerate))
866     return GST_PAD_LINK_REFUSED;
867
868   /* allow a margin of error for the framerate caused by float rounding errors */
869   if ((height != dvdec->height) ||
870       (fabs (framerate - (dvdec->framerate / dvdec->drop_factor)) > 0.00000001))
871     return GST_PAD_LINK_REFUSED;
872
873   if (strcmp (gst_structure_get_name (structure), "video/x-raw-rgb") == 0) {
874     gint bpp;
875
876     gst_structure_get_int (structure, "bpp", &bpp);
877     if (bpp == 24) {
878       dvdec->space = e_dv_color_rgb;
879       dvdec->bpp = 3;
880     } else {
881       dvdec->space = e_dv_color_bgr0;
882       dvdec->bpp = 4;
883     }
884   } else {
885     if (!gst_structure_get_fourcc (structure, "format", &fourcc))
886       return GST_PAD_LINK_REFUSED;
887
888     dvdec->space = e_dv_color_yuv;
889     dvdec->bpp = 2;
890   }
891
892   return GST_PAD_LINK_OK;
893 }
894
895 static void
896 gst_dvdec_push (GstDVDec * dvdec, GstBuffer * outbuf, GstPad * pad,
897     GstClockTime ts)
898 {
899   if ((dvdec->need_discont) || (dvdec->new_media)) {
900     GstEvent *discont;
901
902     discont = gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, ts, NULL);
903     GST_EVENT_DISCONT_NEW_MEDIA (discont) = dvdec->new_media;
904
905     gst_pad_push (pad, GST_DATA (discont));
906   }
907
908   gst_pad_push (pad, GST_DATA (outbuf));
909
910   if ((dvdec->end_position != -1) && (dvdec->next_ts >= dvdec->end_position)) {
911     if (dvdec->loop)
912       gst_pad_push (pad, GST_DATA (gst_event_new (GST_EVENT_SEGMENT_DONE)));
913     else
914       gst_pad_push (pad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
915   }
916 }
917
918 static void
919 gst_dvdec_loop (GstElement * element)
920 {
921   GstDVDec *dvdec;
922   GstBuffer *buf, *outbuf;
923   guint8 *inframe;
924   gint height;
925   guint32 length, got_bytes;
926   GstClockTime ts, duration;
927   gdouble fps;
928   gboolean wide;
929
930   dvdec = GST_DVDEC (element);
931
932   /*
933    * Apparently dv_parse_header can read from the body of the frame
934    * too, so it needs more than header_size bytes. Wacky!
935    */
936   if (dvdec->found_header)
937     length = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
938   else
939     length = NTSC_BUFFER;
940
941   /* first read enough bytes to parse the header */
942   got_bytes = gst_bytestream_peek_bytes (dvdec->bs, &inframe, length);
943   if (got_bytes < length) {
944     gst_dvdec_handle_sink_event (dvdec);
945     return;
946   }
947   if (dv_parse_header (dvdec->decoder, inframe) < 0) {
948     GST_ELEMENT_ERROR (dvdec, STREAM, DECODE, (NULL), (NULL));
949     return;
950   }
951
952   /* after parsing the header we know the length of the data */
953   dvdec->PAL = dv_system_50_fields (dvdec->decoder);
954   dvdec->found_header = TRUE;
955
956   fps = (dvdec->PAL ? PAL_FRAMERATE : NTSC_FRAMERATE);
957   height = (dvdec->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
958   length = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
959   wide = dv_format_wide (dvdec->decoder);
960
961   if (length != dvdec->length) {
962     dvdec->length = length;
963     gst_bytestream_size_hint (dvdec->bs, length);
964   }
965
966   /* then read the read data */
967   got_bytes = gst_bytestream_read (dvdec->bs, &buf, length);
968   if (got_bytes < length) {
969     gst_dvdec_handle_sink_event (dvdec);
970     return;
971   }
972
973   ts = dvdec->next_ts;
974   dvdec->next_ts += GST_SECOND / fps;
975   duration = dvdec->next_ts - ts;
976
977   dv_parse_packs (dvdec->decoder, GST_BUFFER_DATA (buf));
978   if (dv_is_new_recording (dvdec->decoder, GST_BUFFER_DATA (buf)))
979     dvdec->new_media = TRUE;
980   if (GST_PAD_IS_LINKED (dvdec->audiosrcpad)) {
981     gint num_samples;
982
983     dv_decode_full_audio (dvdec->decoder, GST_BUFFER_DATA (buf),
984         dvdec->audio_buffers);
985
986     if ((dv_get_frequency (dvdec->decoder) != dvdec->frequency) ||
987         (dv_get_num_channels (dvdec->decoder) != dvdec->channels)) {
988       if (!gst_pad_set_explicit_caps (dvdec->audiosrcpad,
989               gst_caps_new_simple ("audio/x-raw-int",
990                   "rate", G_TYPE_INT, dv_get_frequency (dvdec->decoder),
991                   "depth", G_TYPE_INT, 16,
992                   "width", G_TYPE_INT, 16,
993                   "signed", G_TYPE_BOOLEAN, TRUE,
994                   "channels", G_TYPE_INT, dv_get_num_channels (dvdec->decoder),
995                   "endianness", G_TYPE_INT, G_LITTLE_ENDIAN, NULL))) {
996         gst_buffer_unref (buf);
997         GST_ELEMENT_ERROR (dvdec, CORE, NEGOTIATION, (NULL),
998             ("Failed to negotiate audio parameters for the DV audio stream"));
999         return;
1000       }
1001
1002       dvdec->frequency = dv_get_frequency (dvdec->decoder);
1003       dvdec->channels = dv_get_num_channels (dvdec->decoder);
1004     }
1005
1006     num_samples = dv_get_num_samples (dvdec->decoder);
1007
1008     if (num_samples) {
1009       gint16 *a_ptr;
1010       gint i, j;
1011
1012       outbuf = gst_buffer_new ();
1013       GST_BUFFER_SIZE (outbuf) = dv_get_num_samples (dvdec->decoder) *
1014           sizeof (gint16) * dv_get_num_channels (dvdec->decoder);
1015       GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
1016
1017       a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);
1018
1019       for (i = 0; i < num_samples; i++) {
1020         for (j = 0; j < dv_get_num_channels (dvdec->decoder); j++) {
1021           *(a_ptr++) = dvdec->audio_buffers[j][i];
1022         }
1023       }
1024
1025       GST_BUFFER_TIMESTAMP (outbuf) = ts;
1026       GST_BUFFER_DURATION (outbuf) = duration;
1027       GST_BUFFER_OFFSET (outbuf) = dvdec->audio_offset;
1028       dvdec->audio_offset += num_samples;
1029       GST_BUFFER_OFFSET_END (outbuf) = dvdec->audio_offset;
1030
1031       gst_dvdec_push (dvdec, outbuf, dvdec->audiosrcpad, ts);
1032     }
1033   } else {
1034     dvdec->frequency = dv_get_frequency (dvdec->decoder);
1035     dvdec->channels = dv_get_num_channels (dvdec->decoder);
1036   }
1037
1038   if (GST_PAD_IS_LINKED (dvdec->videosrcpad)) {
1039     guint8 *outframe;
1040     guint8 *outframe_ptrs[3];
1041     gint outframe_pitches[3];
1042
1043     dvdec->framecount++;
1044     if (dvdec->framecount < dvdec->drop_factor) {
1045       /* don't decode */
1046       goto end;
1047     }
1048     dvdec->framecount = 0;
1049
1050     if ((dvdec->framerate != fps) || (dvdec->height != height)
1051         || dvdec->wide != wide) {
1052       dvdec->height = height;
1053       dvdec->framerate = fps;
1054       dvdec->wide = wide;
1055
1056       if (GST_PAD_LINK_FAILED (gst_pad_renegotiate (dvdec->videosrcpad))) {
1057         GST_ELEMENT_ERROR (dvdec, CORE, NEGOTIATION, (NULL), (NULL));
1058         return;
1059       }
1060     }
1061
1062     outbuf = gst_buffer_new_and_alloc ((720 * height) * dvdec->bpp);
1063
1064     outframe = GST_BUFFER_DATA (outbuf);
1065
1066     outframe_ptrs[0] = outframe;
1067     outframe_pitches[0] = 720 * dvdec->bpp;
1068
1069     /* the rest only matters for YUY2 */
1070     if (dvdec->bpp < 3) {
1071       outframe_ptrs[1] = outframe_ptrs[0] + 720 * height;
1072       outframe_ptrs[2] = outframe_ptrs[1] + 360 * height;
1073
1074       outframe_pitches[1] = height / 2;
1075       outframe_pitches[2] = outframe_pitches[1];
1076     }
1077
1078     dv_decode_full_frame (dvdec->decoder, GST_BUFFER_DATA (buf),
1079         dvdec->space, outframe_ptrs, outframe_pitches);
1080
1081     GST_BUFFER_TIMESTAMP (outbuf) = ts;
1082     GST_BUFFER_DURATION (outbuf) = duration * dvdec->drop_factor;
1083
1084     gst_dvdec_push (dvdec, outbuf, dvdec->videosrcpad, ts);
1085   } else {
1086     dvdec->height = height;
1087     dvdec->framerate = fps;
1088     dvdec->wide = wide;
1089   }
1090
1091 end:
1092   if ((dvdec->end_position != -1) &&
1093       (dvdec->next_ts >= dvdec->end_position) && !dvdec->loop) {
1094     gst_element_set_eos (GST_ELEMENT (dvdec));
1095   }
1096
1097   dvdec->need_discont = FALSE;
1098   dvdec->new_media = FALSE;
1099
1100   gst_buffer_unref (buf);
1101 }
1102
1103 static GstElementStateReturn
1104 gst_dvdec_change_state (GstElement * element)
1105 {
1106   GstDVDec *dvdec = GST_DVDEC (element);
1107
1108   switch (GST_STATE_TRANSITION (element)) {
1109     case GST_STATE_NULL_TO_READY:
1110       break;
1111     case GST_STATE_READY_TO_PAUSED:
1112       dvdec->bs = gst_bytestream_new (dvdec->sinkpad);
1113       dvdec->decoder =
1114           dv_decoder_new (0, dvdec->clamp_luma, dvdec->clamp_chroma);
1115       dvdec->decoder->quality = qualities[dvdec->quality];
1116       dvdec->audio_offset = 0;
1117       dvdec->framecount = 0;
1118       /* 
1119        * Enable this function call when libdv2 0.100 or higher is more
1120        * common
1121        */
1122       /* dv_set_quality (dvdec->decoder, qualities [dvdec->quality]); */
1123       break;
1124     case GST_STATE_PAUSED_TO_PLAYING:
1125       break;
1126     case GST_STATE_PLAYING_TO_PAUSED:
1127       break;
1128     case GST_STATE_PAUSED_TO_READY:
1129       dv_decoder_free (dvdec->decoder);
1130       dvdec->decoder = NULL;
1131       dvdec->found_header = FALSE;
1132       gst_bytestream_destroy (dvdec->bs);
1133       dvdec->bs = NULL;
1134       break;
1135     case GST_STATE_READY_TO_NULL:
1136       break;
1137     default:
1138       break;
1139   }
1140
1141   return parent_class->change_state (element);
1142 }
1143
1144 /* Arguments are part of the Gtk+ object system, and these functions
1145  * enable the element to respond to various arguments.
1146  */
1147 static void
1148 gst_dvdec_set_property (GObject * object, guint prop_id, const GValue * value,
1149     GParamSpec * pspec)
1150 {
1151   GstDVDec *dvdec;
1152
1153   g_return_if_fail (GST_IS_DVDEC (object));
1154
1155   /* Get a pointer of the right type. */
1156   dvdec = GST_DVDEC (object);
1157
1158   /* Check the argument id to see which argument we're setting. */
1159   switch (prop_id) {
1160     case ARG_CLAMP_LUMA:
1161       dvdec->clamp_luma = g_value_get_boolean (value);
1162       break;
1163     case ARG_CLAMP_CHROMA:
1164       dvdec->clamp_chroma = g_value_get_boolean (value);
1165       break;
1166     case ARG_QUALITY:
1167       dvdec->quality = g_value_get_enum (value);
1168       if ((dvdec->quality < 0) || (dvdec->quality > 5))
1169         dvdec->quality = 0;
1170       break;
1171     case ARG_DECODE_NTH:
1172       dvdec->drop_factor = g_value_get_int (value);
1173       break;
1174     default:
1175       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1176       break;
1177   }
1178 }
1179
1180 /* The set function is simply the inverse of the get fuction. */
1181 static void
1182 gst_dvdec_get_property (GObject * object, guint prop_id, GValue * value,
1183     GParamSpec * pspec)
1184 {
1185   GstDVDec *dvdec;
1186
1187   g_return_if_fail (GST_IS_DVDEC (object));
1188   dvdec = GST_DVDEC (object);
1189
1190   switch (prop_id) {
1191     case ARG_CLAMP_LUMA:
1192       g_value_set_boolean (value, dvdec->clamp_luma);
1193       break;
1194     case ARG_CLAMP_CHROMA:
1195       g_value_set_boolean (value, dvdec->clamp_chroma);
1196       break;
1197     case ARG_QUALITY:
1198       g_value_set_enum (value, dvdec->quality);
1199       break;
1200     case ARG_DECODE_NTH:
1201       g_value_set_int (value, dvdec->drop_factor);
1202       break;
1203     default:
1204       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1205       break;
1206   }
1207 }
1208
1209 /* This is the entry into the plugin itself.  When the plugin loads,
1210  * this function is called to register everything that the plugin provides.
1211  */
1212 static gboolean
1213 plugin_init (GstPlugin * plugin)
1214 {
1215   if (!gst_library_load ("gstbytestream"))
1216     return FALSE;
1217
1218   if (!gst_element_register (plugin, "dvdec", GST_RANK_PRIMARY,
1219           gst_dvdec_get_type ()))
1220     return FALSE;
1221
1222   return TRUE;
1223 }
1224
1225 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1226     GST_VERSION_MINOR,
1227     "dvdec",
1228     "Uses libdv to decode DV video (libdv.sourceforge.net)",
1229     plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN);