2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
26 /* First, include the header file for the plugin, to bring in the
27 * object definition and other useful things.
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
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):
43 * Wide NTSC: 10/11 * (16/9)/(4/3) = 40/33
44 * Wide PAL: 59/54 * (16/9)/(4/3) = 118/81
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.
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.
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.
68 #define NTSC_HEIGHT 480
69 #define NTSC_BUFFER 120000
70 #define NTSC_FRAMERATE 30000/1001.
72 #define PAL_HEIGHT 576
73 #define PAL_BUFFER 144000
74 #define PAL_FRAMERATE 25.0
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
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
86 /* The ElementDetails structure gives a human-readable description
87 * of the plugin, as well as author and version data.
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>");
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.
106 #define DV_DEFAULT_QUALITY DV_QUALITY_BEST
107 #define DV_DEFAULT_DECODE_NTH 1
109 /* Arguments are identified the same way, but cannot be zero, so you
110 * must leave the ARG_0 entry in as a placeholder.
122 const gint qualities[] = {
126 DV_QUALITY_DC | DV_QUALITY_COLOR,
127 DV_QUALITY_AC_1 | DV_QUALITY_COLOR,
128 DV_QUALITY_AC_2 | DV_QUALITY_COLOR
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.
135 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
138 GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) true")
141 static GstStaticPadTemplate video_src_temp = GST_STATIC_PAD_TEMPLATE ("video",
144 GST_STATIC_CAPS ("video/x-raw-yuv, "
145 "format = (fourcc) YUY2, "
146 "width = (int) 720, "
148 G_STRINGIFY (NTSC_HEIGHT) ", " G_STRINGIFY (PAL_HEIGHT)
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)
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, "
168 G_STRINGIFY (NTSC_HEIGHT) ", " G_STRINGIFY (PAL_HEIGHT)
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)
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, "
188 G_STRINGIFY (NTSC_HEIGHT) ", " G_STRINGIFY (PAL_HEIGHT)
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)
202 static GstStaticPadTemplate audio_src_temp = GST_STATIC_PAD_TEMPLATE ("audio",
205 GST_STATIC_CAPS ("audio/x-raw-int, "
208 "signed = (boolean) TRUE, "
209 "channels = (int) {2, 4}, "
210 "endianness = (int) " G_STRINGIFY (G_LITTLE_ENDIAN) ", "
211 "rate = (int) { 32000, 44100, 48000 }")
214 #define GST_TYPE_DVDEC_QUALITY (gst_dvdec_quality_get_type())
216 gst_dvdec_quality_get_type (void)
218 static GType 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"},
232 qtype = g_enum_register_static ("GstDVDecQualityEnum", values);
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);
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);
251 static GstPadLinkReturn gst_dvdec_video_link (GstPad * pad,
252 const GstCaps * caps);
253 static GstCaps *gst_dvdec_video_getcaps (GstPad * pad);
255 static const GstEventMask *gst_dvdec_get_event_masks (GstPad * pad);
256 static gboolean gst_dvdec_handle_src_event (GstPad * pad, GstEvent * event);
258 static void gst_dvdec_loop (GstElement * element);
260 static GstElementStateReturn gst_dvdec_change_state (GstElement * element);
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);
267 /* The parent class pointer needs to be kept around for some object
270 static GstElementClass *parent_class = NULL;
272 /* This array holds the ids of the signals registered for this object.
273 * The array indexes are based on the enum up above.
275 /*static guint gst_dvdec_signals[LAST_SIGNAL] = { 0 }; */
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.
283 gst_dvdec_get_type (void)
285 static GType dvdec_type = 0;
288 static const GTypeInfo dvdec_info = {
289 sizeof (GstDVDecClass),
292 (GClassInitFunc) gst_dvdec_class_init,
297 (GInstanceInitFunc) gst_dvdec_init,
301 g_type_register_static (GST_TYPE_ELEMENT, "GstDVDec", &dvdec_info, 0);
307 gst_dvdec_base_init (gpointer g_class)
309 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
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.
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));
323 gst_element_class_set_details (element_class, &dvdec_details);
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.
331 gst_dvdec_class_init (GstDVDecClass * klass)
333 /* Class pointers are needed to supply pointers to the private
334 * implementations of parent class methods.
336 GObjectClass *gobject_class;
337 GstElementClass *gstelement_class;
339 /* Since the dvdec class contains the parent classes, you can simply
340 * cast the pointer to get access to the parent classes.
342 gobject_class = (GObjectClass *) klass;
343 gstelement_class = (GstElementClass *) klass;
345 /* The parent class is needed for class method overrides. */
346 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
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));
361 gobject_class->set_property = gst_dvdec_set_property;
362 gobject_class->get_property = gst_dvdec_get_property;
364 gstelement_class->change_state = gst_dvdec_change_state;
366 /* table initialization, only do once */
370 /* This function is responsible for initializing a specific instance of
374 gst_dvdec_init (GstDVDec * dvdec)
378 dvdec->found_header = FALSE;
381 gst_pad_new_from_template (gst_static_pad_template_get (&sink_temp),
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);
391 gst_pad_new_from_template (gst_static_pad_template_get (&video_src_temp),
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);
412 gst_pad_new_from_template (gst_static_pad_template_get (&audio_src_temp),
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);
429 gst_element_set_loop_function (GST_ELEMENT (dvdec), gst_dvdec_loop);
433 dvdec->next_ts = 0LL;
434 dvdec->end_position = -1LL;
435 dvdec->need_discont = FALSE;
436 dvdec->new_media = FALSE;
437 dvdec->framerate = 0;
439 dvdec->frequency = 0;
442 dvdec->drop_factor = 1;
444 dvdec->clamp_luma = FALSE;
445 dvdec->clamp_chroma = FALSE;
446 dvdec->quality = DV_DEFAULT_QUALITY;
449 for (i = 0; i < 4; i++) {
450 dvdec->audio_buffers[i] =
451 (gint16 *) g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
455 static const GstFormat *
456 gst_dvdec_get_formats (GstPad * pad)
458 static const GstFormat src_formats[] = {
464 static const GstFormat sink_formats[] = {
471 return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
475 gst_dvdec_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
476 GstFormat * dest_format, gint64 * dest_value)
481 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
482 if (dvdec->length == 0)
485 if (dvdec->decoder == NULL)
488 switch (src_format) {
489 case GST_FORMAT_BYTES:
490 switch (*dest_format) {
491 case GST_FORMAT_BYTES:
492 *dest_value = src_value;
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 /
501 else if (pad == dvdec->audiosrcpad)
502 *dest_value = src_value * GST_SECOND /
503 (2 * dvdec->frequency * dvdec->channels);
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;
519 case GST_FORMAT_TIME:
520 case GST_FORMAT_DEFAULT:
521 *dest_format = GST_FORMAT_TIME;
522 *dest_value = src_value;
535 gst_dvdec_sink_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
536 GstFormat * dest_format, gint64 * dest_value)
541 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
543 if (dvdec->length == 0)
546 if (*dest_format == GST_FORMAT_DEFAULT)
547 *dest_format = GST_FORMAT_TIME;
549 switch (src_format) {
550 case GST_FORMAT_BYTES:
551 switch (*dest_format) {
552 case GST_FORMAT_TIME:
556 /* get frame number */
557 frame = src_value / dvdec->length;
559 *dest_value = (frame * GST_SECOND) / dvdec->framerate;
562 case GST_FORMAT_BYTES:
563 *dest_value = src_value;
569 case GST_FORMAT_TIME:
570 switch (*dest_format) {
571 case GST_FORMAT_BYTES:
575 /* calculate the frame */
576 frame = src_value * dvdec->framerate / GST_SECOND;
577 /* calculate the offset */
578 *dest_value = frame * dvdec->length;
581 case GST_FORMAT_TIME:
582 *dest_value = src_value;
594 static const GstQueryType *
595 gst_dvdec_get_src_query_types (GstPad * pad)
597 static const GstQueryType src_query_types[] = {
603 return src_query_types;
607 gst_dvdec_src_query (GstPad * pad, GstQueryType type,
608 GstFormat * format, gint64 * value)
613 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
616 case GST_QUERY_TOTAL:
621 GstFormat tmp_format;
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)) {
632 if (!gst_pad_convert (pad, GST_FORMAT_TIME, *value, format, value)) {
639 case GST_QUERY_POSITION:
643 gst_pad_convert (pad, GST_FORMAT_TIME, dvdec->next_ts, format,
655 static const GstEventMask *
656 gst_dvdec_get_event_masks (GstPad * pad)
658 static const GstEventMask src_event_masks[] = {
659 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH},
662 static const GstEventMask sink_event_masks[] = {
664 {GST_EVENT_DISCONTINUOUS, 0},
665 {GST_EVENT_FLUSH, 0},
669 return (GST_PAD_IS_SRC (pad) ? src_event_masks : sink_event_masks);
673 gst_dvdec_handle_sink_event (GstDVDec * dvdec)
679 gst_bytestream_get_status (dvdec->bs, &remaining, &event);
681 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
684 case GST_EVENT_FLUSH:
686 case GST_EVENT_FILLER:
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));
693 if (GST_PAD_IS_LINKED (dvdec->audiosrcpad)) {
694 gst_event_ref (event);
695 gst_pad_push (dvdec->audiosrcpad, GST_DATA (event));
697 if (type == GST_EVENT_EOS) {
698 gst_element_set_eos (GST_ELEMENT (dvdec));
702 case GST_EVENT_DISCONTINUOUS:
705 gboolean found = FALSE;
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)) {
721 dvdec->next_ts = 0LL;
723 dvdec->need_discont = TRUE;
727 return gst_pad_event_default (dvdec->sinkpad, event);
730 gst_event_unref (event);
735 gst_dvdec_handle_src_event (GstPad * pad, GstEvent * event)
740 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
742 switch (GST_EVENT_TYPE (event)) {
743 case GST_EVENT_SEEK_SEGMENT:
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 */
758 dvdec->end_position = position;
759 dvdec->loop = GST_EVENT_SEEK_TYPE (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
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 */
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 */
785 if (!gst_bytestream_seek (dvdec->bs, position, GST_SEEK_METHOD_SET)) {
788 if (GST_EVENT_TYPE (event) != GST_EVENT_SEEK_SEGMENT)
789 dvdec->end_position = -1;
796 gst_event_unref (event);
801 gst_dvdec_video_getcaps (GstPad * pad)
805 GstPadTemplate *src_pad_template;
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));
811 if (dvdec->found_header) {
817 par_x = PAL_WIDE_PAR_X;
818 par_y = PAL_WIDE_PAR_Y;
820 par_x = PAL_NORMAL_PAR_X;
821 par_y = PAL_NORMAL_PAR_Y;
825 par_x = NTSC_WIDE_PAR_X;
826 par_y = NTSC_WIDE_PAR_Y;
828 par_x = NTSC_NORMAL_PAR_X;
829 par_y = NTSC_NORMAL_PAR_Y;
833 for (i = 0; i < gst_caps_get_size (caps); i++) {
834 GstStructure *structure = gst_caps_get_structure (caps, i);
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);
846 static GstPadLinkReturn
847 gst_dvdec_video_link (GstPad * pad, const GstCaps * caps)
850 GstStructure *structure;
855 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
857 /* if we did not find a header yet, return delayed */
858 if (!dvdec->found_header) {
859 return GST_PAD_LINK_DELAYED;
862 structure = gst_caps_get_structure (caps, 0);
864 if (!gst_structure_get_int (structure, "height", &height) ||
865 !gst_structure_get_double (structure, "framerate", &framerate))
866 return GST_PAD_LINK_REFUSED;
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;
873 if (strcmp (gst_structure_get_name (structure), "video/x-raw-rgb") == 0) {
876 gst_structure_get_int (structure, "bpp", &bpp);
878 dvdec->space = e_dv_color_rgb;
881 dvdec->space = e_dv_color_bgr0;
885 if (!gst_structure_get_fourcc (structure, "format", &fourcc))
886 return GST_PAD_LINK_REFUSED;
888 dvdec->space = e_dv_color_yuv;
892 return GST_PAD_LINK_OK;
896 gst_dvdec_push (GstDVDec * dvdec, GstBuffer * outbuf, GstPad * pad,
899 if ((dvdec->need_discont) || (dvdec->new_media)) {
902 discont = gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, ts, NULL);
903 GST_EVENT_DISCONT_NEW_MEDIA (discont) = dvdec->new_media;
905 gst_pad_push (pad, GST_DATA (discont));
908 gst_pad_push (pad, GST_DATA (outbuf));
910 if ((dvdec->end_position != -1) && (dvdec->next_ts >= dvdec->end_position)) {
912 gst_pad_push (pad, GST_DATA (gst_event_new (GST_EVENT_SEGMENT_DONE)));
914 gst_pad_push (pad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
919 gst_dvdec_loop (GstElement * element)
922 GstBuffer *buf, *outbuf;
925 guint32 length, got_bytes;
926 GstClockTime ts, duration;
930 dvdec = GST_DVDEC (element);
933 * Apparently dv_parse_header can read from the body of the frame
934 * too, so it needs more than header_size bytes. Wacky!
936 if (dvdec->found_header)
937 length = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
939 length = NTSC_BUFFER;
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);
947 if (dv_parse_header (dvdec->decoder, inframe) < 0) {
948 GST_ELEMENT_ERROR (dvdec, STREAM, DECODE, (NULL), (NULL));
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;
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);
961 if (length != dvdec->length) {
962 dvdec->length = length;
963 gst_bytestream_size_hint (dvdec->bs, length);
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);
974 dvdec->next_ts += GST_SECOND / fps;
975 duration = dvdec->next_ts - ts;
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)) {
983 dv_decode_full_audio (dvdec->decoder, GST_BUFFER_DATA (buf),
984 dvdec->audio_buffers);
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"));
1002 dvdec->frequency = dv_get_frequency (dvdec->decoder);
1003 dvdec->channels = dv_get_num_channels (dvdec->decoder);
1006 num_samples = dv_get_num_samples (dvdec->decoder);
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));
1017 a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);
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];
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;
1031 gst_dvdec_push (dvdec, outbuf, dvdec->audiosrcpad, ts);
1034 dvdec->frequency = dv_get_frequency (dvdec->decoder);
1035 dvdec->channels = dv_get_num_channels (dvdec->decoder);
1038 if (GST_PAD_IS_LINKED (dvdec->videosrcpad)) {
1040 guint8 *outframe_ptrs[3];
1041 gint outframe_pitches[3];
1043 dvdec->framecount++;
1044 if (dvdec->framecount < dvdec->drop_factor) {
1048 dvdec->framecount = 0;
1050 if ((dvdec->framerate != fps) || (dvdec->height != height)
1051 || dvdec->wide != wide) {
1052 dvdec->height = height;
1053 dvdec->framerate = fps;
1056 if (GST_PAD_LINK_FAILED (gst_pad_renegotiate (dvdec->videosrcpad))) {
1057 GST_ELEMENT_ERROR (dvdec, CORE, NEGOTIATION, (NULL), (NULL));
1062 outbuf = gst_buffer_new_and_alloc ((720 * height) * dvdec->bpp);
1064 outframe = GST_BUFFER_DATA (outbuf);
1066 outframe_ptrs[0] = outframe;
1067 outframe_pitches[0] = 720 * dvdec->bpp;
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;
1074 outframe_pitches[1] = height / 2;
1075 outframe_pitches[2] = outframe_pitches[1];
1078 dv_decode_full_frame (dvdec->decoder, GST_BUFFER_DATA (buf),
1079 dvdec->space, outframe_ptrs, outframe_pitches);
1081 GST_BUFFER_TIMESTAMP (outbuf) = ts;
1082 GST_BUFFER_DURATION (outbuf) = duration * dvdec->drop_factor;
1084 gst_dvdec_push (dvdec, outbuf, dvdec->videosrcpad, ts);
1086 dvdec->height = height;
1087 dvdec->framerate = fps;
1092 if ((dvdec->end_position != -1) &&
1093 (dvdec->next_ts >= dvdec->end_position) && !dvdec->loop) {
1094 gst_element_set_eos (GST_ELEMENT (dvdec));
1097 dvdec->need_discont = FALSE;
1098 dvdec->new_media = FALSE;
1100 gst_buffer_unref (buf);
1103 static GstElementStateReturn
1104 gst_dvdec_change_state (GstElement * element)
1106 GstDVDec *dvdec = GST_DVDEC (element);
1108 switch (GST_STATE_TRANSITION (element)) {
1109 case GST_STATE_NULL_TO_READY:
1111 case GST_STATE_READY_TO_PAUSED:
1112 dvdec->bs = gst_bytestream_new (dvdec->sinkpad);
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;
1119 * Enable this function call when libdv2 0.100 or higher is more
1122 /* dv_set_quality (dvdec->decoder, qualities [dvdec->quality]); */
1124 case GST_STATE_PAUSED_TO_PLAYING:
1126 case GST_STATE_PLAYING_TO_PAUSED:
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);
1135 case GST_STATE_READY_TO_NULL:
1141 return parent_class->change_state (element);
1144 /* Arguments are part of the Gtk+ object system, and these functions
1145 * enable the element to respond to various arguments.
1148 gst_dvdec_set_property (GObject * object, guint prop_id, const GValue * value,
1153 g_return_if_fail (GST_IS_DVDEC (object));
1155 /* Get a pointer of the right type. */
1156 dvdec = GST_DVDEC (object);
1158 /* Check the argument id to see which argument we're setting. */
1160 case ARG_CLAMP_LUMA:
1161 dvdec->clamp_luma = g_value_get_boolean (value);
1163 case ARG_CLAMP_CHROMA:
1164 dvdec->clamp_chroma = g_value_get_boolean (value);
1167 dvdec->quality = g_value_get_enum (value);
1168 if ((dvdec->quality < 0) || (dvdec->quality > 5))
1171 case ARG_DECODE_NTH:
1172 dvdec->drop_factor = g_value_get_int (value);
1175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1180 /* The set function is simply the inverse of the get fuction. */
1182 gst_dvdec_get_property (GObject * object, guint prop_id, GValue * value,
1187 g_return_if_fail (GST_IS_DVDEC (object));
1188 dvdec = GST_DVDEC (object);
1191 case ARG_CLAMP_LUMA:
1192 g_value_set_boolean (value, dvdec->clamp_luma);
1194 case ARG_CLAMP_CHROMA:
1195 g_value_set_boolean (value, dvdec->clamp_chroma);
1198 g_value_set_enum (value, dvdec->quality);
1200 case ARG_DECODE_NTH:
1201 g_value_set_int (value, dvdec->drop_factor);
1204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
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.
1213 plugin_init (GstPlugin * plugin)
1215 if (!gst_library_load ("gstbytestream"))
1218 if (!gst_element_register (plugin, "dvdec", GST_RANK_PRIMARY,
1219 gst_dvdec_get_type ()))
1225 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1228 "Uses libdv to decode DV video (libdv.sourceforge.net)",
1229 plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN);