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.
25 /* First, include the header file for the plugin, to bring in the
26 * object definition and other useful things.
30 #define NTSC_HEIGHT 480
31 #define NTSC_BUFFER 120000
32 #define NTSC_FRAMERATE 29.997
34 #define PAL_HEIGHT 576
35 #define PAL_BUFFER 144000
36 #define PAL_FRAMERATE 25.0
38 /* The ElementDetails structure gives a human-readable description
39 * of the plugin, as well as author and version data.
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>");
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.
58 /* Arguments are identified the same way, but cannot be zero, so you
59 * must leave the ARG_0 entry in as a placeholder.
70 const gint qualities[] = {
74 DV_QUALITY_DC | DV_QUALITY_COLOR,
75 DV_QUALITY_AC_1 | DV_QUALITY_COLOR,
76 DV_QUALITY_AC_2 | DV_QUALITY_COLOR,
79 #define DV_QUALITY_DEFAULT 5
81 /* The PadFactory structures describe what pads the element has or
82 * can have. They can be quite complex, but for this dvdec plugin
83 * they are rather simple.
85 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
88 GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) true")
91 static GstStaticPadTemplate video_src_temp = GST_STATIC_PAD_TEMPLATE ("video",
94 GST_STATIC_CAPS ("video/x-raw-yuv, "
95 "format = (fourcc) YUY2, "
98 G_STRINGIFY (NTSC_HEIGHT) ", " G_STRINGIFY (PAL_HEIGHT)
100 "framerate = (double) { "
101 G_STRINGIFY (PAL_FRAMERATE) ", " G_STRINGIFY (NTSC_FRAMERATE)
106 "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
107 "red_mask = (int) 0x00ff0000, "
108 "green_mask = (int) 0x0000ff00, "
109 "blue_mask = (int) 0x000000ff, "
110 "width = (int) 720, "
112 G_STRINGIFY (NTSC_HEIGHT) ", " G_STRINGIFY (PAL_HEIGHT)
114 "framerate = (double) { "
115 G_STRINGIFY (PAL_FRAMERATE) ", " G_STRINGIFY (NTSC_FRAMERATE)
120 "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
121 "red_mask = (int) 0x00ff0000, "
122 "green_mask = (int) 0x0000ff00, "
123 "blue_mask = (int) 0x000000ff, "
124 "width = (int) 720, "
126 G_STRINGIFY (NTSC_HEIGHT) ", " G_STRINGIFY (PAL_HEIGHT)
128 "framerate = (double) { "
129 G_STRINGIFY (PAL_FRAMERATE) ", " G_STRINGIFY (NTSC_FRAMERATE)
133 static GstStaticPadTemplate audio_src_temp = GST_STATIC_PAD_TEMPLATE ("audio",
136 GST_STATIC_CAPS ("audio/x-raw-int, "
139 "signed = (boolean) TRUE, "
140 "channels = (int) 2, "
141 "endianness = (int) " G_STRINGIFY (G_LITTLE_ENDIAN) ", "
142 "rate = (int) [ 4000, 48000 ]")
145 #define GST_TYPE_DVDEC_QUALITY (gst_dvdec_quality_get_type())
147 gst_dvdec_quality_get_type (void)
149 static GType qtype = 0;
152 static const GEnumValue values[] = {
153 {0, "DV_QUALITY_FASTEST", "Fastest decoding, low-quality mono"},
154 {1, "DV_QUALITY_AC_1", "Mono decoding using the first AC coefficient"},
155 {2, "DV_QUALITY_AC_2", "Highest quality mono decoding"},
156 {3, "DV_QUALITY_DC|DV_QUALITY_COLOUR", "Fastest colour decoding"},
157 {4, "DV_QUALITY_AC_1|DV_QUALITY_COLOUR",
158 "Colour, using only the first AC coefficient"},
159 {5, "DV_QUALITY_BEST", "Highest quality colour decoding"},
163 qtype = g_enum_register_static ("GstDVDecQualityEnum", values);
168 /* A number of functon prototypes are given so we can refer to them later. */
169 static void gst_dvdec_base_init (gpointer g_class);
170 static void gst_dvdec_class_init (GstDVDecClass * klass);
171 static void gst_dvdec_init (GstDVDec * dvdec);
173 static const GstQueryType *gst_dvdec_get_src_query_types (GstPad * pad);
174 static gboolean gst_dvdec_src_query (GstPad * pad, GstQueryType type,
175 GstFormat * format, gint64 * value);
176 static const GstFormat *gst_dvdec_get_formats (GstPad * pad);
177 static gboolean gst_dvdec_sink_convert (GstPad * pad, GstFormat src_format,
178 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
179 static gboolean gst_dvdec_src_convert (GstPad * pad, GstFormat src_format,
180 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
182 static GstPadLinkReturn gst_dvdec_video_link (GstPad * pad,
183 const GstCaps * caps);
184 static GstCaps *gst_dvdec_video_getcaps (GstPad * pad);
186 static const GstEventMask *gst_dvdec_get_event_masks (GstPad * pad);
187 static gboolean gst_dvdec_handle_src_event (GstPad * pad, GstEvent * event);
189 static void gst_dvdec_loop (GstElement * element);
191 static GstElementStateReturn gst_dvdec_change_state (GstElement * element);
193 static void gst_dvdec_set_property (GObject * object, guint prop_id,
194 const GValue * value, GParamSpec * pspec);
195 static void gst_dvdec_get_property (GObject * object, guint prop_id,
196 GValue * value, GParamSpec * pspec);
198 /* The parent class pointer needs to be kept around for some object
201 static GstElementClass *parent_class = NULL;
203 /* This array holds the ids of the signals registered for this object.
204 * The array indexes are based on the enum up above.
206 /*static guint gst_dvdec_signals[LAST_SIGNAL] = { 0 }; */
208 /* This function is used to register and subsequently return the type
209 * identifier for this object class. On first invocation, it will
210 * register the type, providing the name of the class, struct sizes,
211 * and pointers to the various functions that define the class.
214 gst_dvdec_get_type (void)
216 static GType dvdec_type = 0;
219 static const GTypeInfo dvdec_info = {
220 sizeof (GstDVDecClass),
223 (GClassInitFunc) gst_dvdec_class_init,
228 (GInstanceInitFunc) gst_dvdec_init,
232 g_type_register_static (GST_TYPE_ELEMENT, "GstDVDec", &dvdec_info, 0);
238 gst_dvdec_base_init (gpointer g_class)
240 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
242 /* The pad templates can be easily generated from the factories above,
243 * and then added to the list of padtemplates for the elementfactory.
244 * Note that the generated padtemplates are stored in static global
245 * variables, for the gst_dvdec_init function to use later on.
247 gst_element_class_add_pad_template (element_class,
248 gst_static_pad_template_get (&sink_temp));
249 gst_element_class_add_pad_template (element_class,
250 gst_static_pad_template_get (&video_src_temp));
251 gst_element_class_add_pad_template (element_class,
252 gst_static_pad_template_get (&audio_src_temp));
254 gst_element_class_set_details (element_class, &dvdec_details);
257 /* In order to create an instance of an object, the class must be
258 * initialized by this function. GObject will take care of running
259 * it, based on the pointer to the function provided above.
262 gst_dvdec_class_init (GstDVDecClass * klass)
264 /* Class pointers are needed to supply pointers to the private
265 * implementations of parent class methods.
267 GObjectClass *gobject_class;
268 GstElementClass *gstelement_class;
270 /* Since the dvdec class contains the parent classes, you can simply
271 * cast the pointer to get access to the parent classes.
273 gobject_class = (GObjectClass *) klass;
274 gstelement_class = (GstElementClass *) klass;
276 /* The parent class is needed for class method overrides. */
277 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
279 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_LUMA,
280 g_param_spec_boolean ("clamp_luma", "Clamp luma", "Clamp luma",
281 FALSE, G_PARAM_READWRITE));
282 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_CHROMA,
283 g_param_spec_boolean ("clamp_chroma", "Clamp chroma", "Clamp chroma",
284 FALSE, G_PARAM_READWRITE));
285 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
286 g_param_spec_enum ("quality", "Quality", "Decoding quality",
287 GST_TYPE_DVDEC_QUALITY, DV_QUALITY_DEFAULT, G_PARAM_READWRITE));
289 gobject_class->set_property = gst_dvdec_set_property;
290 gobject_class->get_property = gst_dvdec_get_property;
292 gstelement_class->change_state = gst_dvdec_change_state;
294 /* table initialization, only do once */
298 /* This function is responsible for initializing a specific instance of
302 gst_dvdec_init (GstDVDec * dvdec)
306 dvdec->found_header = FALSE;
309 gst_pad_new_from_template (gst_static_pad_template_get (&sink_temp),
311 gst_pad_set_query_function (dvdec->sinkpad, NULL);
312 gst_pad_set_convert_function (dvdec->sinkpad,
313 GST_DEBUG_FUNCPTR (gst_dvdec_sink_convert));
314 gst_pad_set_formats_function (dvdec->sinkpad,
315 GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
316 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->sinkpad);
319 gst_pad_new_from_template (gst_static_pad_template_get (&video_src_temp),
321 gst_pad_set_query_function (dvdec->videosrcpad,
322 GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
323 gst_pad_set_query_type_function (dvdec->videosrcpad,
324 GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
325 gst_pad_set_event_function (dvdec->videosrcpad,
326 GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
327 gst_pad_set_event_mask_function (dvdec->videosrcpad,
328 GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
329 gst_pad_set_convert_function (dvdec->videosrcpad,
330 GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
331 gst_pad_set_formats_function (dvdec->videosrcpad,
332 GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
333 gst_pad_set_getcaps_function (dvdec->videosrcpad,
334 GST_DEBUG_FUNCPTR (gst_dvdec_video_getcaps));
335 gst_pad_set_link_function (dvdec->videosrcpad,
336 GST_DEBUG_FUNCPTR (gst_dvdec_video_link));
337 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->videosrcpad);
340 gst_pad_new_from_template (gst_static_pad_template_get (&audio_src_temp),
342 gst_pad_set_query_function (dvdec->audiosrcpad,
343 GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
344 gst_pad_set_query_type_function (dvdec->audiosrcpad,
345 GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
346 gst_pad_set_event_function (dvdec->audiosrcpad,
347 GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
348 gst_pad_set_event_mask_function (dvdec->audiosrcpad,
349 GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
350 gst_pad_set_convert_function (dvdec->audiosrcpad,
351 GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
352 gst_pad_set_formats_function (dvdec->audiosrcpad,
353 GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
354 gst_pad_use_explicit_caps (dvdec->audiosrcpad);
355 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->audiosrcpad);
357 gst_element_set_loop_function (GST_ELEMENT (dvdec), gst_dvdec_loop);
360 dvdec->next_ts = 0LL;
361 dvdec->end_position = -1LL;
362 dvdec->need_discont = FALSE;
363 dvdec->framerate = 0;
365 dvdec->frequency = 0;
368 dvdec->clamp_luma = FALSE;
369 dvdec->clamp_chroma = FALSE;
370 dvdec->quality = DV_QUALITY_BEST;
373 for (i = 0; i < 4; i++) {
374 dvdec->audio_buffers[i] =
375 (gint16 *) g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
379 static const GstFormat *
380 gst_dvdec_get_formats (GstPad * pad)
382 static const GstFormat src_formats[] = {
388 static const GstFormat sink_formats[] = {
395 return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
399 gst_dvdec_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
400 GstFormat * dest_format, gint64 * dest_value)
405 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
407 if (dvdec->length == 0)
410 if (dvdec->decoder == NULL)
413 switch (src_format) {
414 case GST_FORMAT_BYTES:
415 switch (*dest_format) {
416 case GST_FORMAT_BYTES:
417 *dest_value = src_value;
419 case GST_FORMAT_DEFAULT:
420 case GST_FORMAT_TIME:
421 *dest_format = GST_FORMAT_TIME;
422 if (pad == dvdec->videosrcpad)
423 *dest_value = src_value * GST_SECOND /
424 (720 * dvdec->height * dvdec->bpp * dvdec->framerate /
426 else if (pad == dvdec->audiosrcpad)
427 *dest_value = src_value * GST_SECOND /
428 (2 * dvdec->decoder->audio->frequency *
429 dvdec->decoder->audio->num_channels);
435 case GST_FORMAT_TIME:
436 switch (*dest_format) {
437 case GST_FORMAT_BYTES:
438 if (pad == dvdec->videosrcpad)
439 *dest_value = src_value * 720 * dvdec->height * dvdec->bpp *
440 dvdec->framerate / GST_SECOND;
441 else if (pad == dvdec->audiosrcpad)
442 *dest_value = 2 * src_value * dvdec->decoder->audio->frequency *
443 dvdec->decoder->audio->num_channels / GST_SECOND;
445 case GST_FORMAT_TIME:
446 case GST_FORMAT_DEFAULT:
447 *dest_format = GST_FORMAT_TIME;
448 *dest_value = src_value;
461 gst_dvdec_sink_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
462 GstFormat * dest_format, gint64 * dest_value)
467 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
469 if (dvdec->length == 0)
472 if (*dest_format == GST_FORMAT_DEFAULT)
473 *dest_format = GST_FORMAT_TIME;
475 switch (src_format) {
476 case GST_FORMAT_BYTES:
477 switch (*dest_format) {
478 case GST_FORMAT_TIME:
482 /* get frame number */
483 frame = src_value / dvdec->length;
485 *dest_value = (frame * GST_SECOND) / dvdec->framerate;
488 case GST_FORMAT_BYTES:
489 *dest_value = src_value;
495 case GST_FORMAT_TIME:
496 switch (*dest_format) {
497 case GST_FORMAT_BYTES:
501 /* calculate the frame */
502 frame = src_value * dvdec->framerate / GST_SECOND;
503 /* calculate the offset */
504 *dest_value = frame * dvdec->length;
507 case GST_FORMAT_TIME:
508 *dest_value = src_value;
520 static const GstQueryType *
521 gst_dvdec_get_src_query_types (GstPad * pad)
523 static const GstQueryType src_query_types[] = {
529 return src_query_types;
533 gst_dvdec_src_query (GstPad * pad, GstQueryType type,
534 GstFormat * format, gint64 * value)
539 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
542 case GST_QUERY_TOTAL:
547 GstFormat tmp_format;
549 len = gst_bytestream_length (dvdec->bs);
550 tmp_format = GST_FORMAT_TIME;
551 if (len == -1 || !gst_pad_convert (dvdec->sinkpad,
552 GST_FORMAT_BYTES, len, &tmp_format, value)) {
555 if (!gst_pad_convert (pad, GST_FORMAT_TIME, *value, format, value)) {
562 case GST_QUERY_POSITION:
566 gst_pad_convert (pad, GST_FORMAT_TIME, dvdec->next_ts, format,
578 static const GstEventMask *
579 gst_dvdec_get_event_masks (GstPad * pad)
581 static const GstEventMask src_event_masks[] = {
582 {GST_EVENT_SEEK, GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH},
585 static const GstEventMask sink_event_masks[] = {
587 {GST_EVENT_DISCONTINUOUS, 0},
588 {GST_EVENT_FLUSH, 0},
592 return (GST_PAD_IS_SRC (pad) ? src_event_masks : sink_event_masks);
596 gst_dvdec_handle_sink_event (GstDVDec * dvdec)
602 gst_bytestream_get_status (dvdec->bs, &remaining, &event);
604 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
607 case GST_EVENT_FLUSH:
609 case GST_EVENT_DISCONTINUOUS:
612 gboolean found = FALSE;
615 format = GST_FORMAT_TIME;
616 /* try to get a timestamp from the discont formats */
617 for (i = 0; i < GST_EVENT_DISCONT_OFFSET_LEN (event); i++) {
618 if (gst_pad_convert (dvdec->sinkpad,
619 GST_EVENT_DISCONT_OFFSET (event, i).format,
620 GST_EVENT_DISCONT_OFFSET (event, i).value,
621 &format, &dvdec->next_ts)) {
628 dvdec->next_ts = 0LL;
630 dvdec->need_discont = TRUE;
634 return gst_pad_event_default (dvdec->sinkpad, event);
637 gst_event_unref (event);
642 gst_dvdec_handle_src_event (GstPad * pad, GstEvent * event)
647 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
649 switch (GST_EVENT_TYPE (event)) {
650 case GST_EVENT_SEEK_SEGMENT:
655 /* first bring the format to time */
656 format = GST_FORMAT_TIME;
657 if (!gst_pad_convert (pad,
658 GST_EVENT_SEEK_FORMAT (event),
659 GST_EVENT_SEEK_ENDOFFSET (event), &format, &position)) {
660 /* could not convert seek format to time offset */
665 dvdec->end_position = position;
666 dvdec->loop = GST_EVENT_SEEK_TYPE (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
673 /* first bring the format to time */
674 format = GST_FORMAT_TIME;
675 if (!gst_pad_convert (pad,
676 GST_EVENT_SEEK_FORMAT (event),
677 GST_EVENT_SEEK_OFFSET (event), &format, &position)) {
678 /* could not convert seek format to time offset */
682 dvdec->next_ts = position;
683 /* then try to figure out the byteoffset for this time */
684 format = GST_FORMAT_BYTES;
685 if (!gst_pad_convert (dvdec->sinkpad, GST_FORMAT_TIME, position,
686 &format, &position)) {
687 /* could not convert seek format to byte offset */
692 if (!gst_bytestream_seek (dvdec->bs, position, GST_SEEK_METHOD_SET)) {
695 if (GST_EVENT_TYPE (event) != GST_EVENT_SEEK_SEGMENT)
696 dvdec->end_position = -1;
703 gst_event_unref (event);
708 gst_dvdec_video_getcaps (GstPad * pad)
712 GstPadTemplate *src_pad_template;
714 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
715 src_pad_template = gst_static_pad_template_get (&video_src_temp);
716 caps = gst_caps_copy (gst_pad_template_get_caps (src_pad_template));
718 if (dvdec->found_header) {
722 for (i = 0; i < gst_caps_get_size (caps); i++) {
723 GstStructure *structure = gst_caps_get_structure (caps, i);
725 gst_structure_set (structure,
726 "height", G_TYPE_INT, dvdec->height,
727 "framerate", G_TYPE_DOUBLE, dvdec->framerate, NULL);
734 static GstPadLinkReturn
735 gst_dvdec_video_link (GstPad * pad, const GstCaps * caps)
738 GstStructure *structure;
743 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
745 /* if we did not find a header yet, return delayed */
746 if (!dvdec->found_header) {
747 return GST_PAD_LINK_DELAYED;
750 structure = gst_caps_get_structure (caps, 0);
752 if (!gst_structure_get_int (structure, "height", &height) ||
753 !gst_structure_get_double (structure, "framerate", &framerate))
754 return GST_PAD_LINK_REFUSED;
756 if ((height != dvdec->height) || (framerate != dvdec->framerate))
757 return GST_PAD_LINK_REFUSED;
759 if (strcmp (gst_structure_get_name (structure), "video/x-raw-rgb") == 0) {
762 gst_structure_get_int (structure, "bpp", &bpp);
764 dvdec->space = e_dv_color_rgb;
767 dvdec->space = e_dv_color_bgr0;
771 if (!gst_structure_get_fourcc (structure, "format", &fourcc))
772 return GST_PAD_LINK_REFUSED;
774 dvdec->space = e_dv_color_yuv;
778 return GST_PAD_LINK_OK;
782 gst_dvdec_push (GstDVDec * dvdec, GstBuffer * outbuf, GstPad * pad,
785 GST_BUFFER_TIMESTAMP (outbuf) = ts;
787 if (dvdec->need_discont) {
790 discont = gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, ts, NULL);
791 gst_pad_push (pad, GST_DATA (discont));
794 gst_pad_push (pad, GST_DATA (outbuf));
796 if ((dvdec->end_position != -1) && (dvdec->next_ts >= dvdec->end_position)) {
798 gst_pad_push (pad, GST_DATA (gst_event_new (GST_EVENT_SEGMENT_DONE)));
800 gst_pad_push (pad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
805 gst_dvdec_loop (GstElement * element)
808 GstBuffer *buf, *outbuf;
811 guint32 length, got_bytes;
815 dvdec = GST_DVDEC (element);
818 * Apparently dv_parse_header can read from the body of the frame
819 * too, so it needs more than header_size bytes. Wacky!
821 if (dvdec->found_header)
822 length = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
824 length = NTSC_BUFFER;
826 /* first read enough bytes to parse the header */
827 got_bytes = gst_bytestream_peek_bytes (dvdec->bs, &inframe, length);
828 if (got_bytes < length) {
829 gst_dvdec_handle_sink_event (dvdec);
832 if (dv_parse_header (dvdec->decoder, inframe) < 0) {
833 GST_ELEMENT_ERROR (dvdec, STREAM, DECODE, (NULL), (NULL));
837 /* after parsing the header we know the length of the data */
838 dvdec->PAL = dv_system_50_fields (dvdec->decoder);
839 dvdec->found_header = TRUE;
841 fps = (dvdec->PAL ? PAL_FRAMERATE : NTSC_FRAMERATE);
842 height = (dvdec->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
843 length = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
845 if (length != dvdec->length) {
846 dvdec->length = length;
847 gst_bytestream_size_hint (dvdec->bs, length);
850 /* then read the read data */
851 got_bytes = gst_bytestream_read (dvdec->bs, &buf, length);
852 if (got_bytes < length) {
853 gst_dvdec_handle_sink_event (dvdec);
858 dvdec->next_ts += GST_SECOND / dvdec->framerate;
860 dv_parse_packs (dvdec->decoder, GST_BUFFER_DATA (buf));
862 if (GST_PAD_IS_LINKED (dvdec->audiosrcpad)) {
866 dv_decode_full_audio (dvdec->decoder, GST_BUFFER_DATA (buf),
867 dvdec->audio_buffers);
869 if ((dvdec->decoder->audio->frequency != dvdec->frequency) ||
870 (dvdec->decoder->audio->num_channels != dvdec->channels)) {
871 if (!gst_pad_set_explicit_caps (dvdec->audiosrcpad,
872 gst_caps_new_simple ("audio/x-raw-int",
873 "rate", G_TYPE_INT, dvdec->decoder->audio->frequency,
874 "depth", G_TYPE_INT, 16,
875 "width", G_TYPE_INT, 16,
876 "signed", G_TYPE_BOOLEAN, TRUE,
877 "channels", G_TYPE_INT, dvdec->decoder->audio->num_channels,
878 "endianness", G_TYPE_INT, G_LITTLE_ENDIAN, NULL))) {
879 gst_buffer_unref (buf);
880 GST_ELEMENT_ERROR (dvdec, CORE, NEGOTIATION, (NULL), (NULL));
884 dvdec->frequency = dvdec->decoder->audio->frequency;
885 dvdec->channels = dvdec->decoder->audio->num_channels;
888 outbuf = gst_buffer_new ();
889 GST_BUFFER_SIZE (outbuf) = dvdec->decoder->audio->samples_this_frame *
890 sizeof (gint16) * dvdec->decoder->audio->num_channels;
891 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
893 a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);
895 for (i = 0; i < dvdec->decoder->audio->samples_this_frame; i++) {
896 for (j = 0; j < dvdec->decoder->audio->num_channels; j++) {
897 *(a_ptr++) = dvdec->audio_buffers[j][i];
900 gst_dvdec_push (dvdec, outbuf, dvdec->audiosrcpad, ts);
903 if (GST_PAD_IS_LINKED (dvdec->videosrcpad)) {
905 guint8 *outframe_ptrs[3];
906 gint outframe_pitches[3];
908 if ((dvdec->framerate != fps) || (dvdec->height != height)) {
909 dvdec->height = height;
910 dvdec->framerate = fps;
912 if (GST_PAD_LINK_FAILED (gst_pad_renegotiate (dvdec->videosrcpad))) {
913 GST_ELEMENT_ERROR (dvdec, CORE, NEGOTIATION, (NULL), (NULL));
918 outbuf = gst_buffer_new_and_alloc ((720 * height) * dvdec->bpp);
920 outframe = GST_BUFFER_DATA (outbuf);
922 outframe_ptrs[0] = outframe;
923 outframe_pitches[0] = 720 * dvdec->bpp;
925 /* the rest only matters for YUY2 */
926 if (dvdec->bpp < 3) {
927 outframe_ptrs[1] = outframe_ptrs[0] + 720 * height;
928 outframe_ptrs[2] = outframe_ptrs[1] + 360 * height;
930 outframe_pitches[1] = height / 2;
931 outframe_pitches[2] = outframe_pitches[1];
934 dv_decode_full_frame (dvdec->decoder, GST_BUFFER_DATA (buf),
935 dvdec->space, outframe_ptrs, outframe_pitches);
937 gst_dvdec_push (dvdec, outbuf, dvdec->videosrcpad, ts);
940 if ((dvdec->end_position != -1) &&
941 (dvdec->next_ts >= dvdec->end_position) && !dvdec->loop) {
942 gst_element_set_eos (GST_ELEMENT (dvdec));
945 if (dvdec->need_discont) {
946 dvdec->need_discont = FALSE;
949 gst_buffer_unref (buf);
952 static GstElementStateReturn
953 gst_dvdec_change_state (GstElement * element)
955 GstDVDec *dvdec = GST_DVDEC (element);
957 switch (GST_STATE_TRANSITION (element)) {
958 case GST_STATE_NULL_TO_READY:
960 case GST_STATE_READY_TO_PAUSED:
961 dvdec->bs = gst_bytestream_new (dvdec->sinkpad);
963 dv_decoder_new (0, dvdec->clamp_luma, dvdec->clamp_chroma);
964 dvdec->decoder->quality = qualities[dvdec->quality];
966 * Enable this function call when libdv2 0.100 or higher is more
969 /* dv_set_quality (dvdec->decoder, qualities [dvdec->quality]); */
971 case GST_STATE_PAUSED_TO_PLAYING:
973 case GST_STATE_PLAYING_TO_PAUSED:
975 case GST_STATE_PAUSED_TO_READY:
976 dv_decoder_free (dvdec->decoder);
977 dvdec->decoder = NULL;
978 dvdec->found_header = FALSE;
979 gst_bytestream_destroy (dvdec->bs);
981 case GST_STATE_READY_TO_NULL:
987 return parent_class->change_state (element);
990 /* Arguments are part of the Gtk+ object system, and these functions
991 * enable the element to respond to various arguments.
994 gst_dvdec_set_property (GObject * object, guint prop_id, const GValue * value,
999 /* It's not null if we got it, but it might not be ours */
1000 g_return_if_fail (GST_IS_DVDEC (object));
1002 /* Get a pointer of the right type. */
1003 dvdec = GST_DVDEC (object);
1005 /* Check the argument id to see which argument we're setting. */
1007 case ARG_CLAMP_LUMA:
1008 dvdec->clamp_luma = g_value_get_boolean (value);
1010 case ARG_CLAMP_CHROMA:
1011 dvdec->clamp_chroma = g_value_get_boolean (value);
1014 dvdec->quality = g_value_get_enum (value);
1015 if ((dvdec->quality < 0) || (dvdec->quality > 5))
1019 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1024 /* The set function is simply the inverse of the get fuction. */
1026 gst_dvdec_get_property (GObject * object, guint prop_id, GValue * value,
1031 /* It's not null if we got it, but it might not be ours */
1032 g_return_if_fail (GST_IS_DVDEC (object));
1033 dvdec = GST_DVDEC (object);
1036 case ARG_CLAMP_LUMA:
1037 g_value_set_boolean (value, dvdec->clamp_luma);
1039 case ARG_CLAMP_CHROMA:
1040 g_value_set_boolean (value, dvdec->clamp_chroma);
1043 g_value_set_enum (value, dvdec->quality);
1046 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1051 /* This is the entry into the plugin itself. When the plugin loads,
1052 * this function is called to register everything that the plugin provides.
1055 plugin_init (GstPlugin * plugin)
1057 if (!gst_library_load ("gstbytestream"))
1060 if (!gst_element_register (plugin, "dvdec", GST_RANK_PRIMARY,
1061 gst_dvdec_get_type ()))
1067 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1070 "Uses libdv to decode DV video (libdv.sourceforge.net)",
1071 plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN);