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 PAL_HEIGHT 576
33 #define PAL_BUFFER 144000
35 /* The ElementDetails structure gives a human-readable description
36 * of the plugin, as well as author and version data.
38 static GstElementDetails dvdec_details = {
39 "DV (smpte314) decoder plugin",
40 "Codec/Video/Decoder",
42 "Uses libdv to decode DV video (libdv.sourceforge.net)",
44 "Erik Walthinsen <omega@cse.ogi.edu>\n"
45 "Wim Taymans <wim.taymans@tvd.be>",
50 /* These are the signals that this element can fire. They are zero-
51 * based because the numbers themselves are private to the object.
52 * LAST_SIGNAL is used for initialization of the signal array.
59 /* Arguments are identified the same way, but cannot be zero, so you
60 * must leave the ARG_0 entry in as a placeholder.
70 /* The PadFactory structures describe what pads the element has or
71 * can have. They can be quite complex, but for this dvdec plugin
72 * they are rather simple.
74 GST_PAD_TEMPLATE_FACTORY (sink_temp,
81 "systemstream", GST_PROPS_BOOLEAN (TRUE)
86 GST_PAD_TEMPLATE_FACTORY (video_src_temp,
93 "format", GST_PROPS_FOURCC (GST_MAKE_FOURCC ('Y','U','Y','2')),
94 "width", GST_PROPS_INT (720),
95 "height", GST_PROPS_LIST (
96 GST_PROPS_INT (NTSC_HEIGHT),
97 GST_PROPS_INT (PAL_HEIGHT)
99 "framerate", GST_PROPS_LIST (
100 GST_PROPS_FLOAT (25.),
101 GST_PROPS_FLOAT (30/1.001)
107 "bpp", GST_PROPS_INT(32),
108 "depth", GST_PROPS_INT(32),
109 "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
110 "red_mask", GST_PROPS_INT(0x000000ff),
111 "green_mask", GST_PROPS_INT(0x0000ff00),
112 "blue_mask", GST_PROPS_INT(0x00ff00),
113 "width", GST_PROPS_INT (720),
114 "height", GST_PROPS_LIST (
115 GST_PROPS_INT (NTSC_HEIGHT),
116 GST_PROPS_INT (PAL_HEIGHT)
118 "framerate", GST_PROPS_LIST (
119 GST_PROPS_FLOAT (25.),
120 GST_PROPS_FLOAT (30/1.001)
126 "bpp", GST_PROPS_INT(24),
127 "depth", GST_PROPS_INT(24),
128 "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
129 "red_mask", GST_PROPS_INT(0x0000ff),
130 "green_mask", GST_PROPS_INT(0x00ff00),
131 "blue_mask", GST_PROPS_INT(0xff0000),
132 "width", GST_PROPS_INT (720),
133 "height", GST_PROPS_LIST (
134 GST_PROPS_INT (NTSC_HEIGHT),
135 GST_PROPS_INT (PAL_HEIGHT)
137 "framerate", GST_PROPS_LIST (
138 GST_PROPS_FLOAT (25.),
139 GST_PROPS_FLOAT (30/1.001)
144 GST_PAD_TEMPLATE_FACTORY ( audio_src_temp,
151 "depth", GST_PROPS_INT (16),
152 "width", GST_PROPS_INT (16),
153 "signed", GST_PROPS_BOOLEAN (TRUE),
154 "channels", GST_PROPS_INT (2),
155 "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
156 "rate", GST_PROPS_INT_RANGE (4000, 48000)
162 dv_type_find (GstBuffer *buf, gpointer private)
164 guint32 head = GUINT32_FROM_BE(*((guint32 *)GST_BUFFER_DATA(buf)));
167 /* check for DIF and DV flag */
168 if ((head & 0xffffff00) == 0x1f070000 && !(GST_BUFFER_DATA(buf)[4] & 0x01)) {
171 if ((head & 0x000000ff) & 0x80)
176 new = GST_CAPS_NEW ("dv_type_find",
178 "systemstream", GST_PROPS_BOOLEAN (TRUE)
184 static GstTypeDefinition dv_definition = {
185 "dv_video/dv", "video/x-dv", ".dv", dv_type_find
188 #define GST_TYPE_DVDEC_QUALITY (gst_dvdec_quality_get_type())
190 gst_dvdec_quality_get_type (void)
192 static GType qtype = 0;
194 static const GFlagsValue values[] = {
195 { DV_QUALITY_COLOR, "DV_QUALITY_COLOR", "Color or monochrome decoding" },
196 { DV_QUALITY_AC_1, "DV_QUALITY_AC_1", "AC 1 something" },
197 { DV_QUALITY_AC_2, "DV_QUALITY_AC_2", "AC 2 something" },
200 qtype = g_flags_register_static ("GstDVDecQualityFlags", values);
205 /* A number of functon prototypes are given so we can refer to them later. */
206 static void gst_dvdec_class_init (GstDVDecClass *klass);
207 static void gst_dvdec_init (GstDVDec *dvdec);
209 static const GstQueryType*
210 gst_dvdec_get_src_query_types (GstPad *pad);
211 static gboolean gst_dvdec_src_query (GstPad *pad, GstQueryType type,
212 GstFormat *format, gint64 *value);
213 static const GstFormat* gst_dvdec_get_formats (GstPad *pad);
214 static gboolean gst_dvdec_sink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
215 GstFormat *dest_format, gint64 *dest_value);
216 static gboolean gst_dvdec_src_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
217 GstFormat *dest_format, gint64 *dest_value);
219 static const GstEventMask*
220 gst_dvdec_get_event_masks (GstPad *pad);
221 static gboolean gst_dvdec_handle_src_event (GstPad *pad, GstEvent *event);
223 static void gst_dvdec_loop (GstElement *element);
225 static GstElementStateReturn
226 gst_dvdec_change_state (GstElement *element);
228 static void gst_dvdec_set_property (GObject *object, guint prop_id,
229 const GValue *value, GParamSpec *pspec);
230 static void gst_dvdec_get_property (GObject *object, guint prop_id,
231 GValue *value, GParamSpec *pspec);
233 /* The parent class pointer needs to be kept around for some object
236 static GstElementClass *parent_class = NULL;
238 /* This array holds the ids of the signals registered for this object.
239 * The array indexes are based on the enum up above.
241 /*static guint gst_dvdec_signals[LAST_SIGNAL] = { 0 }; */
243 /* This function is used to register and subsequently return the type
244 * identifier for this object class. On first invocation, it will
245 * register the type, providing the name of the class, struct sizes,
246 * and pointers to the various functions that define the class.
249 gst_dvdec_get_type (void)
251 static GType dvdec_type = 0;
254 static const GTypeInfo dvdec_info = {
255 sizeof (GstDVDecClass),
258 (GClassInitFunc) gst_dvdec_class_init,
263 (GInstanceInitFunc) gst_dvdec_init,
265 dvdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstDVDec", &dvdec_info, 0);
270 /* In order to create an instance of an object, the class must be
271 * initialized by this function. GObject will take care of running
272 * it, based on the pointer to the function provided above.
275 gst_dvdec_class_init (GstDVDecClass *klass)
277 /* Class pointers are needed to supply pointers to the private
278 * implementations of parent class methods.
280 GObjectClass *gobject_class;
281 GstElementClass *gstelement_class;
283 /* Since the dvdec class contains the parent classes, you can simply
284 * cast the pointer to get access to the parent classes.
286 gobject_class = (GObjectClass*) klass;
287 gstelement_class = (GstElementClass*) klass;
289 /* The parent class is needed for class method overrides. */
290 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
292 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_LUMA,
293 g_param_spec_boolean ("clamp_luma", "Clamp luma", "Clamp luma",
294 FALSE, G_PARAM_READWRITE));
295 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_CHROMA,
296 g_param_spec_boolean ("clamp_chroma", "Clamp chroma", "Clamp chroma",
297 FALSE, G_PARAM_READWRITE));
298 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
299 g_param_spec_flags ("quality", "Quality", "Decoding quality",
300 GST_TYPE_DVDEC_QUALITY, DV_QUALITY_BEST, G_PARAM_READWRITE));
302 gobject_class->set_property = gst_dvdec_set_property;
303 gobject_class->get_property = gst_dvdec_get_property;
305 gstelement_class->change_state = gst_dvdec_change_state;
307 /* table initialization, only do once */
311 /* This function is responsible for initializing a specific instance of
315 gst_dvdec_init(GstDVDec *dvdec)
319 dvdec->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_temp), "sink");
320 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->sinkpad);
321 gst_pad_set_query_function (dvdec->sinkpad, NULL);
322 gst_pad_set_convert_function (dvdec->sinkpad, GST_DEBUG_FUNCPTR (gst_dvdec_sink_convert));
323 gst_pad_set_formats_function (dvdec->sinkpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
325 dvdec->videosrcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (video_src_temp), "video");
326 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->videosrcpad);
327 gst_pad_set_query_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
328 gst_pad_set_query_type_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
329 gst_pad_set_event_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
330 gst_pad_set_event_mask_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
331 gst_pad_set_convert_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
332 gst_pad_set_formats_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
334 dvdec->audiosrcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET(audio_src_temp), "audio");
335 gst_element_add_pad(GST_ELEMENT(dvdec),dvdec->audiosrcpad);
336 gst_pad_set_query_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
337 gst_pad_set_query_type_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
338 gst_pad_set_event_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
339 gst_pad_set_event_mask_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
340 gst_pad_set_convert_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
341 gst_pad_set_formats_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
343 gst_element_set_loop_function (GST_ELEMENT (dvdec), gst_dvdec_loop);
347 dvdec->next_ts = 0LL;
348 dvdec->end_position = -1LL;
349 dvdec->need_discont = FALSE;
350 dvdec->framerate = 0;
352 dvdec->clamp_luma = FALSE;
353 dvdec->clamp_chroma = FALSE;
354 dvdec->quality = DV_QUALITY_BEST;
357 for (i = 0; i <4; i++) {
358 dvdec->audio_buffers[i] = (gint16 *)g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
362 static const GstFormat*
363 gst_dvdec_get_formats (GstPad *pad)
365 static const GstFormat src_formats[] = {
371 static const GstFormat sink_formats[] = {
377 return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
381 gst_dvdec_src_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
382 GstFormat *dest_format, gint64 *dest_value)
388 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
390 if (dvdec->length == 0)
393 if (dvdec->decoder == NULL)
396 switch (src_format) {
397 case GST_FORMAT_BYTES:
398 switch (*dest_format) {
399 case GST_FORMAT_TIME:
404 case GST_FORMAT_TIME:
405 switch (*dest_format) {
406 case GST_FORMAT_BYTES:
407 if (pad == dvdec->videosrcpad)
408 scale = 720 * dvdec->height * dvdec->bpp;
409 else if (pad == dvdec->audiosrcpad)
410 scale = dvdec->decoder->audio->num_channels * 2;
412 case GST_FORMAT_DEFAULT:
413 if (pad == dvdec->videosrcpad)
414 *dest_value = src_value * dvdec->framerate * scale / (GST_SECOND*100);
415 else if (pad == dvdec->audiosrcpad)
416 *dest_value = src_value * dvdec->decoder->audio->frequency * scale / GST_SECOND;
429 gst_dvdec_sink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
430 GstFormat *dest_format, gint64 *dest_value)
435 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
437 if (dvdec->length == 0)
440 switch (src_format) {
441 case GST_FORMAT_BYTES:
442 switch (*dest_format) {
443 case GST_FORMAT_TIME:
446 /* get frame number */
447 frame = src_value / dvdec->length;
449 *dest_value = (frame * GST_SECOND * 100) / dvdec->framerate;
456 case GST_FORMAT_TIME:
457 switch (*dest_format) {
458 case GST_FORMAT_BYTES:
461 /* calculate the frame */
462 frame = src_value * dvdec->framerate / (GST_SECOND*100);
463 /* calculate the offset */
464 *dest_value = frame * dvdec->length;
477 static const GstQueryType*
478 gst_dvdec_get_src_query_types (GstPad *pad)
480 static const GstQueryType src_query_types[] = {
485 return src_query_types;
489 gst_dvdec_src_query (GstPad *pad, GstQueryType type,
490 GstFormat *format, gint64 *value)
495 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
498 case GST_QUERY_TOTAL:
503 GstFormat tmp_format;
505 len = gst_bytestream_length (dvdec->bs);
506 tmp_format = GST_FORMAT_TIME;
507 if (len == -1 || !gst_pad_convert (dvdec->sinkpad,
514 if (!gst_pad_convert (pad, GST_FORMAT_TIME, *value, format, value)) {
521 case GST_QUERY_POSITION:
524 res = gst_pad_convert (pad, GST_FORMAT_TIME, dvdec->next_ts, format, value);
535 static const GstEventMask*
536 gst_dvdec_get_event_masks (GstPad *pad)
538 static const GstEventMask src_event_masks[] = {
539 { GST_EVENT_SEEK, GST_SEEK_METHOD_SET |
540 GST_SEEK_FLAG_FLUSH },
543 static const GstEventMask sink_event_masks[] = {
544 { GST_EVENT_EOS, 0 },
545 { GST_EVENT_DISCONTINUOUS, 0 },
546 { GST_EVENT_FLUSH, 0 },
550 return (GST_PAD_IS_SRC (pad) ? src_event_masks : sink_event_masks);
554 gst_dvdec_handle_sink_event (GstDVDec *dvdec)
560 gst_bytestream_get_status (dvdec->bs, &remaining, &event);
562 type = event? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
566 gst_pad_event_default (dvdec->sinkpad, event);
568 case GST_EVENT_FLUSH:
570 case GST_EVENT_DISCONTINUOUS:
573 gboolean found = FALSE;
576 format = GST_FORMAT_TIME;
577 /* try to get a timestamp from the discont formats */
578 for (i = 0; i < GST_EVENT_DISCONT_OFFSET_LEN(event); i++) {
579 if (gst_pad_convert (dvdec->sinkpad,
580 GST_EVENT_DISCONT_OFFSET(event,i).format,
581 GST_EVENT_DISCONT_OFFSET(event,i).value,
582 &format, &dvdec->next_ts))
590 dvdec->next_ts = 0LL;
592 dvdec->need_discont = TRUE;
596 g_warning ("unhandled event %d\n", type);
599 gst_event_unref (event);
604 gst_dvdec_handle_src_event (GstPad *pad, GstEvent *event)
609 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
611 switch (GST_EVENT_TYPE (event)) {
612 case GST_EVENT_SEEK_SEGMENT:
617 /* first bring the format to time */
618 format = GST_FORMAT_TIME;
619 if (!gst_pad_convert (pad,
620 GST_EVENT_SEEK_FORMAT (event),
621 GST_EVENT_SEEK_ENDOFFSET (event),
624 /* could not convert seek format to time offset */
629 dvdec->end_position = position;
630 dvdec->loop = GST_EVENT_SEEK_TYPE (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
637 /* first bring the format to time */
638 format = GST_FORMAT_TIME;
639 if (!gst_pad_convert (pad,
640 GST_EVENT_SEEK_FORMAT (event),
641 GST_EVENT_SEEK_OFFSET (event),
644 /* could not convert seek format to time offset */
648 dvdec->next_ts = position;
649 /* then try to figure out the byteoffset for this time */
650 format = GST_FORMAT_BYTES;
651 if (!gst_pad_convert (dvdec->sinkpad, GST_FORMAT_TIME, position,
654 /* could not convert seek format to byte offset */
659 if (!gst_bytestream_seek (dvdec->bs, position, GST_SEEK_METHOD_SET)) {
662 if (GST_EVENT_TYPE (event) != GST_EVENT_SEEK_SEGMENT)
663 dvdec->end_position = -1;
670 gst_event_unref (event);
675 gst_dvdec_push (GstDVDec *dvdec, GstBuffer *outbuf, GstPad *pad, GstClockTime ts)
677 GST_BUFFER_TIMESTAMP (outbuf) = ts;
679 if (dvdec->need_discont) {
682 discont = gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, ts, NULL);
683 gst_pad_push (pad, GST_BUFFER (discont));
686 gst_pad_push (pad, outbuf);
688 if ((dvdec->end_position != -1) &&
689 (dvdec->next_ts >= dvdec->end_position)) {
691 gst_pad_push (pad, GST_BUFFER(gst_event_new (GST_EVENT_SEGMENT_DONE)));
693 gst_pad_push (pad, GST_BUFFER(gst_event_new (GST_EVENT_EOS)));
698 gst_dvdec_loop (GstElement *element)
701 GstBuffer *buf, *outbuf;
704 guint32 length, got_bytes;
709 dvdec = GST_DVDEC (element);
711 /* first read enough bytes to parse the header */
712 got_bytes = gst_bytestream_peek_bytes (dvdec->bs, &inframe, header_size);
713 if (got_bytes < header_size) {
714 gst_dvdec_handle_sink_event (dvdec);
717 dv_parse_header (dvdec->decoder, inframe);
718 /* after parsing the header we know the size of the data */
719 dvdec->PAL = dv_system_50_fields (dvdec->decoder);
721 dvdec->framerate = (dvdec->PAL ? 2500 : 2997);
722 fps = (dvdec->PAL ? 25. : 30/1.001);
723 dvdec->height = height = (dvdec->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
724 length = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
726 if (length != dvdec->length) {
727 dvdec->length = length;
728 gst_bytestream_size_hint (dvdec->bs, length);
731 /* then read the read data */
732 got_bytes = gst_bytestream_read (dvdec->bs, &buf, length);
733 if (got_bytes < length) {
734 gst_dvdec_handle_sink_event (dvdec);
738 /* if we did not negotiate yet, do it now */
739 if (!GST_PAD_CAPS (dvdec->videosrcpad)) {
743 /* we what we are allowed to do */
744 allowed = gst_pad_get_allowed_caps (dvdec->videosrcpad);
746 /* try to fix our height */
747 trylist = gst_caps_intersect (allowed, gst_caps_append (
751 "height", GST_PROPS_INT (height),
752 "framerate", GST_PROPS_FLOAT (fps)
756 "height", GST_PROPS_INT (height),
757 "framerate", GST_PROPS_FLOAT (fps)
760 /* prepare for looping */
761 trylist = gst_caps_normalize (trylist);
764 GstCaps *to_try = gst_caps_copy_1 (trylist);
766 /* try each format */
767 if (gst_pad_try_set_caps (dvdec->videosrcpad, to_try) > 0) {
770 /* it worked, try to find what it was again */
771 gst_caps_get_fourcc_int (to_try, "format", &fourcc);
773 if (fourcc == GST_STR_FOURCC ("RGB ")) {
776 gst_caps_get_int (to_try, "bpp", &bpp);
778 dvdec->space = e_dv_color_rgb;
782 dvdec->space = e_dv_color_bgr0;
787 dvdec->space = e_dv_color_yuv;
792 trylist = trylist->next;
794 /* oops list exhausted an nothing was found... */
796 gst_element_error (element, "could not negotiate");
801 format = GST_FORMAT_TIME;
802 gst_pad_query (dvdec->videosrcpad, GST_QUERY_POSITION, &format, &ts);
804 dvdec->next_ts += (GST_SECOND*100) / dvdec->framerate;
806 if (GST_PAD_IS_LINKED (dvdec->audiosrcpad)) {
810 dv_decode_full_audio (dvdec->decoder, GST_BUFFER_DATA (buf), dvdec->audio_buffers);
812 /* if we did not negotiate yet, do it now */
813 if (!GST_PAD_CAPS (dvdec->audiosrcpad)) {
814 gst_pad_try_set_caps (dvdec->audiosrcpad,
818 "rate", GST_PROPS_INT (dvdec->decoder->audio->frequency),
819 "depth", GST_PROPS_INT (16),
820 "width", GST_PROPS_INT (16),
821 "signed", GST_PROPS_BOOLEAN (TRUE),
822 "channels", GST_PROPS_INT (dvdec->decoder->audio->num_channels),
823 "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN)
827 outbuf = gst_buffer_new ();
828 GST_BUFFER_SIZE (outbuf) = dvdec->decoder->audio->samples_this_frame *
829 sizeof (gint16) * dvdec->decoder->audio->num_channels;
830 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
832 a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);
834 for (i = 0; i < dvdec->decoder->audio->samples_this_frame; i++) {
835 for (j = 0; j < dvdec->decoder->audio->num_channels; j++) {
836 *(a_ptr++) = dvdec->audio_buffers[j][i];
839 gst_dvdec_push (dvdec, outbuf, dvdec->audiosrcpad, ts);
842 if (GST_PAD_IS_LINKED (dvdec->videosrcpad)) {
844 guint8 *outframe_ptrs[3];
845 gint outframe_pitches[3];
847 /* try to grab a pool */
849 dvdec->pool = gst_pad_get_bufferpool (dvdec->videosrcpad);
853 /* try to get a buffer from the pool if we have one */
855 outbuf = gst_buffer_new_from_pool (dvdec->pool, 0, 0);
857 /* no buffer from pool, allocate one ourselves */
859 outbuf = gst_buffer_new ();
861 GST_BUFFER_SIZE (outbuf) = (720 * height) * dvdec->bpp;
862 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
865 outframe = GST_BUFFER_DATA (outbuf);
867 outframe_ptrs[0] = outframe;
868 outframe_pitches[0] = 720 * dvdec->bpp;
870 /* the rest only matters for YUY2 */
871 if (dvdec->bpp < 3) {
872 outframe_ptrs[1] = outframe_ptrs[0] + 720 * height;
873 outframe_ptrs[2] = outframe_ptrs[1] + 360 * height;
875 outframe_pitches[1] = height / 2;
876 outframe_pitches[2] = outframe_pitches[1];
879 dv_decode_full_frame (dvdec->decoder, GST_BUFFER_DATA (buf),
880 dvdec->space, outframe_ptrs, outframe_pitches);
882 gst_dvdec_push (dvdec, outbuf, dvdec->videosrcpad, ts);
885 if ((dvdec->end_position != -1) &&
886 (dvdec->next_ts >= dvdec->end_position) &&
888 gst_element_set_eos (GST_ELEMENT (dvdec));
891 if (dvdec->need_discont) {
892 dvdec->need_discont = FALSE;
895 gst_buffer_unref (buf);
898 static GstElementStateReturn
899 gst_dvdec_change_state (GstElement *element)
901 GstDVDec *dvdec = GST_DVDEC (element);
903 switch (GST_STATE_TRANSITION (element)) {
904 case GST_STATE_NULL_TO_READY:
906 case GST_STATE_READY_TO_PAUSED:
907 dvdec->bs = gst_bytestream_new (dvdec->sinkpad);
908 dvdec->decoder = dv_decoder_new (0, dvdec->clamp_luma, dvdec->clamp_chroma);
909 dvdec->decoder->quality = dvdec->quality;
911 case GST_STATE_PAUSED_TO_PLAYING:
913 case GST_STATE_PLAYING_TO_PAUSED:
915 gst_buffer_pool_unref (dvdec->pool);
918 case GST_STATE_PAUSED_TO_READY:
919 dv_decoder_free (dvdec->decoder);
920 dvdec->decoder = NULL;
921 gst_bytestream_destroy (dvdec->bs);
923 case GST_STATE_READY_TO_NULL:
929 parent_class->change_state (element);
931 return GST_STATE_SUCCESS;
934 /* Arguments are part of the Gtk+ object system, and these functions
935 * enable the element to respond to various arguments.
938 gst_dvdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
942 /* It's not null if we got it, but it might not be ours */
943 g_return_if_fail(GST_IS_DVDEC(object));
945 /* Get a pointer of the right type. */
946 dvdec = GST_DVDEC(object);
948 /* Check the argument id to see which argument we're setting. */
951 dvdec->clamp_luma = g_value_get_boolean (value);
953 case ARG_CLAMP_CHROMA:
954 dvdec->clamp_chroma = g_value_get_boolean (value);
957 dvdec->quality = g_value_get_flags (value);
960 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
965 /* The set function is simply the inverse of the get fuction. */
967 gst_dvdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
971 /* It's not null if we got it, but it might not be ours */
972 g_return_if_fail(GST_IS_DVDEC(object));
973 dvdec = GST_DVDEC(object);
977 g_value_set_boolean (value, dvdec->clamp_luma);
979 case ARG_CLAMP_CHROMA:
980 g_value_set_boolean (value, dvdec->clamp_chroma);
983 g_value_set_flags (value, dvdec->quality);
986 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
991 /* This is the entry into the plugin itself. When the plugin loads,
992 * this function is called to register everything that the plugin provides.
995 plugin_init (GModule *module, GstPlugin *plugin)
997 GstElementFactory *factory;
998 GstTypeFactory *type;
1000 if (!gst_library_load ("gstbytestream"))
1003 /* We need to create an ElementFactory for each element we provide.
1004 * This consists of the name of the element, the GType identifier,
1005 * and a pointer to the details structure at the top of the file.
1007 factory = gst_element_factory_new("dvdec", GST_TYPE_DVDEC, &dvdec_details);
1008 g_return_val_if_fail(factory != NULL, FALSE);
1010 gst_element_factory_set_rank (factory, GST_ELEMENT_RANK_PRIMARY);
1011 /* The pad templates can be easily generated from the factories above,
1012 * and then added to the list of padtemplates for the elementfactory.
1013 * Note that the generated padtemplates are stored in static global
1014 * variables, for the gst_dvdec_init function to use later on.
1016 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET(sink_temp));
1017 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET(video_src_temp));
1018 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET(audio_src_temp));
1020 /* The very last thing is to register the elementfactory with the plugin. */
1021 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
1023 type = gst_type_factory_new (&dv_definition);
1024 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));
1029 GstPluginDesc plugin_desc = {