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 = GST_ELEMENT_DETAILS (
39 "DV (smpte314) decoder plugin",
40 "Codec/Decoder/Video",
41 "Uses libdv to decode DV video (libdv.sourceforge.net)",
42 "Erik Walthinsen <omega@cse.ogi.edu>\n"
43 "Wim Taymans <wim.taymans@tvd.be>"
47 /* These are the signals that this element can fire. They are zero-
48 * based because the numbers themselves are private to the object.
49 * LAST_SIGNAL is used for initialization of the signal array.
56 /* Arguments are identified the same way, but cannot be zero, so you
57 * must leave the ARG_0 entry in as a placeholder.
67 /* The PadFactory structures describe what pads the element has or
68 * can have. They can be quite complex, but for this dvdec plugin
69 * they are rather simple.
71 GST_PAD_TEMPLATE_FACTORY (sink_temp,
78 "systemstream", GST_PROPS_BOOLEAN (TRUE)
83 GST_PAD_TEMPLATE_FACTORY (video_src_temp,
90 "format", GST_PROPS_FOURCC (GST_MAKE_FOURCC ('Y','U','Y','2')),
91 "width", GST_PROPS_INT (720),
92 "height", GST_PROPS_LIST (
93 GST_PROPS_INT (NTSC_HEIGHT),
94 GST_PROPS_INT (PAL_HEIGHT)
96 "framerate", GST_PROPS_LIST (
97 GST_PROPS_FLOAT (25.),
98 GST_PROPS_FLOAT (30/1.001)
104 "bpp", GST_PROPS_INT(32),
105 "depth", GST_PROPS_INT(32),
106 "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
107 "red_mask", GST_PROPS_INT(0x000000ff),
108 "green_mask", GST_PROPS_INT(0x0000ff00),
109 "blue_mask", GST_PROPS_INT(0x00ff0000),
110 "width", GST_PROPS_INT (720),
111 "height", GST_PROPS_LIST (
112 GST_PROPS_INT (NTSC_HEIGHT),
113 GST_PROPS_INT (PAL_HEIGHT)
115 "framerate", GST_PROPS_LIST (
116 GST_PROPS_FLOAT (25.),
117 GST_PROPS_FLOAT (30/1.001)
123 "bpp", GST_PROPS_INT(24),
124 "depth", GST_PROPS_INT(24),
125 "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
126 "red_mask", GST_PROPS_INT(0x0000ff),
127 "green_mask", GST_PROPS_INT(0x00ff00),
128 "blue_mask", GST_PROPS_INT(0xff0000),
129 "width", GST_PROPS_INT (720),
130 "height", GST_PROPS_LIST (
131 GST_PROPS_INT (NTSC_HEIGHT),
132 GST_PROPS_INT (PAL_HEIGHT)
134 "framerate", GST_PROPS_LIST (
135 GST_PROPS_FLOAT (25.),
136 GST_PROPS_FLOAT (30/1.001)
141 GST_PAD_TEMPLATE_FACTORY ( audio_src_temp,
148 "depth", GST_PROPS_INT (16),
149 "width", GST_PROPS_INT (16),
150 "signed", GST_PROPS_BOOLEAN (TRUE),
151 "channels", GST_PROPS_INT (2),
152 "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN),
153 "rate", GST_PROPS_INT_RANGE (4000, 48000)
157 #define GST_TYPE_DVDEC_QUALITY (gst_dvdec_quality_get_type())
159 gst_dvdec_quality_get_type (void)
161 static GType qtype = 0;
163 static const GFlagsValue values[] = {
164 { DV_QUALITY_COLOR, "DV_QUALITY_COLOR", "Color or monochrome decoding" },
165 { DV_QUALITY_AC_1, "DV_QUALITY_AC_1", "AC 1 something" },
166 { DV_QUALITY_AC_2, "DV_QUALITY_AC_2", "AC 2 something" },
169 qtype = g_flags_register_static ("GstDVDecQualityFlags", values);
174 /* A number of functon prototypes are given so we can refer to them later. */
175 static void gst_dvdec_base_init (gpointer g_class);
176 static void gst_dvdec_class_init (GstDVDecClass *klass);
177 static void gst_dvdec_init (GstDVDec *dvdec);
179 static const GstQueryType*
180 gst_dvdec_get_src_query_types (GstPad *pad);
181 static gboolean gst_dvdec_src_query (GstPad *pad, GstQueryType type,
182 GstFormat *format, gint64 *value);
183 static const GstFormat* gst_dvdec_get_formats (GstPad *pad);
184 static gboolean gst_dvdec_sink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
185 GstFormat *dest_format, gint64 *dest_value);
186 static gboolean gst_dvdec_src_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
187 GstFormat *dest_format, gint64 *dest_value);
189 static const GstEventMask*
190 gst_dvdec_get_event_masks (GstPad *pad);
191 static gboolean gst_dvdec_handle_src_event (GstPad *pad, GstEvent *event);
193 static void gst_dvdec_loop (GstElement *element);
195 static GstElementStateReturn
196 gst_dvdec_change_state (GstElement *element);
198 static void gst_dvdec_set_property (GObject *object, guint prop_id,
199 const GValue *value, GParamSpec *pspec);
200 static void gst_dvdec_get_property (GObject *object, guint prop_id,
201 GValue *value, GParamSpec *pspec);
203 /* The parent class pointer needs to be kept around for some object
206 static GstElementClass *parent_class = NULL;
208 /* This array holds the ids of the signals registered for this object.
209 * The array indexes are based on the enum up above.
211 /*static guint gst_dvdec_signals[LAST_SIGNAL] = { 0 }; */
213 /* This function is used to register and subsequently return the type
214 * identifier for this object class. On first invocation, it will
215 * register the type, providing the name of the class, struct sizes,
216 * and pointers to the various functions that define the class.
219 gst_dvdec_get_type (void)
221 static GType dvdec_type = 0;
224 static const GTypeInfo dvdec_info = {
225 sizeof (GstDVDecClass),
228 (GClassInitFunc) gst_dvdec_class_init,
233 (GInstanceInitFunc) gst_dvdec_init,
235 dvdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstDVDec", &dvdec_info, 0);
241 gst_dvdec_base_init (gpointer g_class)
243 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
245 /* The pad templates can be easily generated from the factories above,
246 * and then added to the list of padtemplates for the elementfactory.
247 * Note that the generated padtemplates are stored in static global
248 * variables, for the gst_dvdec_init function to use later on.
250 gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET(sink_temp));
251 gst_element_class_add_pad_template (element_class, GST_PAD_TEMPLATE_GET(video_src_temp));
252 gst_element_class_add_pad_template (element_class, GST_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_flags ("quality", "Quality", "Decoding quality",
287 GST_TYPE_DVDEC_QUALITY, DV_QUALITY_BEST, 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->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_temp), "sink");
307 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->sinkpad);
308 gst_pad_set_query_function (dvdec->sinkpad, NULL);
309 gst_pad_set_convert_function (dvdec->sinkpad, GST_DEBUG_FUNCPTR (gst_dvdec_sink_convert));
310 gst_pad_set_formats_function (dvdec->sinkpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
312 dvdec->videosrcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (video_src_temp), "video");
313 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->videosrcpad);
314 gst_pad_set_query_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
315 gst_pad_set_query_type_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
316 gst_pad_set_event_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
317 gst_pad_set_event_mask_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
318 gst_pad_set_convert_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
319 gst_pad_set_formats_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
321 dvdec->audiosrcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET(audio_src_temp), "audio");
322 gst_element_add_pad(GST_ELEMENT(dvdec),dvdec->audiosrcpad);
323 gst_pad_set_query_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
324 gst_pad_set_query_type_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
325 gst_pad_set_event_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
326 gst_pad_set_event_mask_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
327 gst_pad_set_convert_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
328 gst_pad_set_formats_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
330 gst_element_set_loop_function (GST_ELEMENT (dvdec), gst_dvdec_loop);
334 dvdec->next_ts = 0LL;
335 dvdec->end_position = -1LL;
336 dvdec->need_discont = FALSE;
337 dvdec->framerate = 0;
339 dvdec->clamp_luma = FALSE;
340 dvdec->clamp_chroma = FALSE;
341 dvdec->quality = DV_QUALITY_BEST;
344 for (i = 0; i <4; i++) {
345 dvdec->audio_buffers[i] = (gint16 *)g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
349 static const GstFormat*
350 gst_dvdec_get_formats (GstPad *pad)
352 static const GstFormat src_formats[] = {
358 static const GstFormat sink_formats[] = {
364 return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
368 gst_dvdec_src_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
369 GstFormat *dest_format, gint64 *dest_value)
375 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
377 if (dvdec->length == 0)
380 if (dvdec->decoder == NULL)
383 switch (src_format) {
384 case GST_FORMAT_BYTES:
385 switch (*dest_format) {
386 case GST_FORMAT_TIME:
391 case GST_FORMAT_TIME:
392 switch (*dest_format) {
393 case GST_FORMAT_BYTES:
394 if (pad == dvdec->videosrcpad)
395 scale = 720 * dvdec->height * dvdec->bpp;
396 else if (pad == dvdec->audiosrcpad)
397 scale = dvdec->decoder->audio->num_channels * 2;
399 case GST_FORMAT_DEFAULT:
400 if (pad == dvdec->videosrcpad)
401 *dest_value = src_value * dvdec->framerate * scale / (GST_SECOND*100);
402 else if (pad == dvdec->audiosrcpad)
403 *dest_value = src_value * dvdec->decoder->audio->frequency * scale / GST_SECOND;
416 gst_dvdec_sink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
417 GstFormat *dest_format, gint64 *dest_value)
422 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
424 if (dvdec->length == 0)
427 switch (src_format) {
428 case GST_FORMAT_BYTES:
429 switch (*dest_format) {
430 case GST_FORMAT_TIME:
433 /* get frame number */
434 frame = src_value / dvdec->length;
436 *dest_value = (frame * GST_SECOND * 100) / dvdec->framerate;
443 case GST_FORMAT_TIME:
444 switch (*dest_format) {
445 case GST_FORMAT_BYTES:
448 /* calculate the frame */
449 frame = src_value * dvdec->framerate / (GST_SECOND*100);
450 /* calculate the offset */
451 *dest_value = frame * dvdec->length;
464 static const GstQueryType*
465 gst_dvdec_get_src_query_types (GstPad *pad)
467 static const GstQueryType src_query_types[] = {
472 return src_query_types;
476 gst_dvdec_src_query (GstPad *pad, GstQueryType type,
477 GstFormat *format, gint64 *value)
482 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
485 case GST_QUERY_TOTAL:
490 GstFormat tmp_format;
492 len = gst_bytestream_length (dvdec->bs);
493 tmp_format = GST_FORMAT_TIME;
494 if (len == -1 || !gst_pad_convert (dvdec->sinkpad,
501 if (!gst_pad_convert (pad, GST_FORMAT_TIME, *value, format, value)) {
508 case GST_QUERY_POSITION:
511 res = gst_pad_convert (pad, GST_FORMAT_TIME, dvdec->next_ts, format, value);
522 static const GstEventMask*
523 gst_dvdec_get_event_masks (GstPad *pad)
525 static const GstEventMask src_event_masks[] = {
526 { GST_EVENT_SEEK, GST_SEEK_METHOD_SET |
527 GST_SEEK_FLAG_FLUSH },
530 static const GstEventMask sink_event_masks[] = {
531 { GST_EVENT_EOS, 0 },
532 { GST_EVENT_DISCONTINUOUS, 0 },
533 { GST_EVENT_FLUSH, 0 },
537 return (GST_PAD_IS_SRC (pad) ? src_event_masks : sink_event_masks);
541 gst_dvdec_handle_sink_event (GstDVDec *dvdec)
547 gst_bytestream_get_status (dvdec->bs, &remaining, &event);
549 type = event? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
553 gst_pad_event_default (dvdec->sinkpad, event);
555 case GST_EVENT_FLUSH:
557 case GST_EVENT_DISCONTINUOUS:
560 gboolean found = FALSE;
563 format = GST_FORMAT_TIME;
564 /* try to get a timestamp from the discont formats */
565 for (i = 0; i < GST_EVENT_DISCONT_OFFSET_LEN(event); i++) {
566 if (gst_pad_convert (dvdec->sinkpad,
567 GST_EVENT_DISCONT_OFFSET(event,i).format,
568 GST_EVENT_DISCONT_OFFSET(event,i).value,
569 &format, &dvdec->next_ts))
577 dvdec->next_ts = 0LL;
579 dvdec->need_discont = TRUE;
583 g_warning ("unhandled event %d\n", type);
586 gst_event_unref (event);
591 gst_dvdec_handle_src_event (GstPad *pad, GstEvent *event)
596 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
598 switch (GST_EVENT_TYPE (event)) {
599 case GST_EVENT_SEEK_SEGMENT:
604 /* first bring the format to time */
605 format = GST_FORMAT_TIME;
606 if (!gst_pad_convert (pad,
607 GST_EVENT_SEEK_FORMAT (event),
608 GST_EVENT_SEEK_ENDOFFSET (event),
611 /* could not convert seek format to time offset */
616 dvdec->end_position = position;
617 dvdec->loop = GST_EVENT_SEEK_TYPE (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
624 /* first bring the format to time */
625 format = GST_FORMAT_TIME;
626 if (!gst_pad_convert (pad,
627 GST_EVENT_SEEK_FORMAT (event),
628 GST_EVENT_SEEK_OFFSET (event),
631 /* could not convert seek format to time offset */
635 dvdec->next_ts = position;
636 /* then try to figure out the byteoffset for this time */
637 format = GST_FORMAT_BYTES;
638 if (!gst_pad_convert (dvdec->sinkpad, GST_FORMAT_TIME, position,
641 /* could not convert seek format to byte offset */
646 if (!gst_bytestream_seek (dvdec->bs, position, GST_SEEK_METHOD_SET)) {
649 if (GST_EVENT_TYPE (event) != GST_EVENT_SEEK_SEGMENT)
650 dvdec->end_position = -1;
657 gst_event_unref (event);
662 gst_dvdec_push (GstDVDec *dvdec, GstBuffer *outbuf, GstPad *pad, GstClockTime ts)
664 GST_BUFFER_TIMESTAMP (outbuf) = ts;
666 if (dvdec->need_discont) {
669 discont = gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, ts, NULL);
670 gst_pad_push (pad, GST_DATA (discont));
673 gst_pad_push (pad, GST_DATA (outbuf));
675 if ((dvdec->end_position != -1) &&
676 (dvdec->next_ts >= dvdec->end_position)) {
678 gst_pad_push (pad, GST_DATA(gst_event_new (GST_EVENT_SEGMENT_DONE)));
680 gst_pad_push (pad, GST_DATA(gst_event_new (GST_EVENT_EOS)));
685 gst_dvdec_loop (GstElement *element)
688 GstBuffer *buf, *outbuf;
691 guint32 length, got_bytes;
696 dvdec = GST_DVDEC (element);
698 /* first read enough bytes to parse the header */
699 got_bytes = gst_bytestream_peek_bytes (dvdec->bs, &inframe, header_size);
700 if (got_bytes < header_size) {
701 gst_dvdec_handle_sink_event (dvdec);
704 dv_parse_header (dvdec->decoder, inframe);
705 /* after parsing the header we know the size of the data */
706 dvdec->PAL = dv_system_50_fields (dvdec->decoder);
708 dvdec->framerate = (dvdec->PAL ? 2500 : 2997);
709 fps = (dvdec->PAL ? 25. : 30/1.001);
710 dvdec->height = height = (dvdec->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
711 length = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
713 if (length != dvdec->length) {
714 dvdec->length = length;
715 gst_bytestream_size_hint (dvdec->bs, length);
718 /* then read the read data */
719 got_bytes = gst_bytestream_read (dvdec->bs, &buf, length);
720 if (got_bytes < length) {
721 gst_dvdec_handle_sink_event (dvdec);
725 /* if we did not negotiate yet, do it now */
726 if (!GST_PAD_CAPS (dvdec->videosrcpad)) {
730 /* we what we are allowed to do */
731 allowed = gst_pad_get_allowed_caps (dvdec->videosrcpad);
733 /* try to fix our height */
734 trylist = gst_caps_intersect (allowed, gst_caps_append (
738 "height", GST_PROPS_INT (height),
739 "framerate", GST_PROPS_FLOAT (fps)
743 "height", GST_PROPS_INT (height),
744 "framerate", GST_PROPS_FLOAT (fps)
747 /* prepare for looping */
748 trylist = gst_caps_normalize (trylist);
751 GstCaps *to_try = gst_caps_copy_1 (trylist);
753 /* try each format */
754 if (gst_pad_try_set_caps (dvdec->videosrcpad, to_try) > 0) {
757 /* it worked, try to find what it was again */
758 gst_caps_get_fourcc_int (to_try, "format", &fourcc);
760 if (fourcc == GST_STR_FOURCC ("RGB ")) {
763 gst_caps_get_int (to_try, "bpp", &bpp);
765 dvdec->space = e_dv_color_rgb;
769 dvdec->space = e_dv_color_bgr0;
774 dvdec->space = e_dv_color_yuv;
779 trylist = trylist->next;
781 /* oops list exhausted an nothing was found... */
783 gst_element_error (element, "could not negotiate");
788 format = GST_FORMAT_TIME;
789 gst_pad_query (dvdec->videosrcpad, GST_QUERY_POSITION, &format, &ts);
791 dvdec->next_ts += (GST_SECOND*100) / dvdec->framerate;
793 if (GST_PAD_IS_LINKED (dvdec->audiosrcpad)) {
797 dv_decode_full_audio (dvdec->decoder, GST_BUFFER_DATA (buf), dvdec->audio_buffers);
799 /* if we did not negotiate yet, do it now */
800 if (!GST_PAD_CAPS (dvdec->audiosrcpad)) {
801 gst_pad_try_set_caps (dvdec->audiosrcpad,
805 "rate", GST_PROPS_INT (dvdec->decoder->audio->frequency),
806 "depth", GST_PROPS_INT (16),
807 "width", GST_PROPS_INT (16),
808 "signed", GST_PROPS_BOOLEAN (TRUE),
809 "channels", GST_PROPS_INT (dvdec->decoder->audio->num_channels),
810 "endianness", GST_PROPS_INT (G_LITTLE_ENDIAN)
814 outbuf = gst_buffer_new ();
815 GST_BUFFER_SIZE (outbuf) = dvdec->decoder->audio->samples_this_frame *
816 sizeof (gint16) * dvdec->decoder->audio->num_channels;
817 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
819 a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);
821 for (i = 0; i < dvdec->decoder->audio->samples_this_frame; i++) {
822 for (j = 0; j < dvdec->decoder->audio->num_channels; j++) {
823 *(a_ptr++) = dvdec->audio_buffers[j][i];
826 gst_dvdec_push (dvdec, outbuf, dvdec->audiosrcpad, ts);
829 if (GST_PAD_IS_LINKED (dvdec->videosrcpad)) {
831 guint8 *outframe_ptrs[3];
832 gint outframe_pitches[3];
834 /* try to grab a pool */
836 dvdec->pool = gst_pad_get_bufferpool (dvdec->videosrcpad);
840 /* try to get a buffer from the pool if we have one */
842 outbuf = gst_buffer_new_from_pool (dvdec->pool, 0, 0);
844 /* no buffer from pool, allocate one ourselves */
846 outbuf = gst_buffer_new ();
848 GST_BUFFER_SIZE (outbuf) = (720 * height) * dvdec->bpp;
849 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
852 outframe = GST_BUFFER_DATA (outbuf);
854 outframe_ptrs[0] = outframe;
855 outframe_pitches[0] = 720 * dvdec->bpp;
857 /* the rest only matters for YUY2 */
858 if (dvdec->bpp < 3) {
859 outframe_ptrs[1] = outframe_ptrs[0] + 720 * height;
860 outframe_ptrs[2] = outframe_ptrs[1] + 360 * height;
862 outframe_pitches[1] = height / 2;
863 outframe_pitches[2] = outframe_pitches[1];
866 dv_decode_full_frame (dvdec->decoder, GST_BUFFER_DATA (buf),
867 dvdec->space, outframe_ptrs, outframe_pitches);
869 gst_dvdec_push (dvdec, outbuf, dvdec->videosrcpad, ts);
872 if ((dvdec->end_position != -1) &&
873 (dvdec->next_ts >= dvdec->end_position) &&
875 gst_element_set_eos (GST_ELEMENT (dvdec));
878 if (dvdec->need_discont) {
879 dvdec->need_discont = FALSE;
882 gst_buffer_unref (buf);
885 static GstElementStateReturn
886 gst_dvdec_change_state (GstElement *element)
888 GstDVDec *dvdec = GST_DVDEC (element);
890 switch (GST_STATE_TRANSITION (element)) {
891 case GST_STATE_NULL_TO_READY:
893 case GST_STATE_READY_TO_PAUSED:
894 dvdec->bs = gst_bytestream_new (dvdec->sinkpad);
895 dvdec->decoder = dv_decoder_new (0, dvdec->clamp_luma, dvdec->clamp_chroma);
896 dvdec->decoder->quality = dvdec->quality;
898 case GST_STATE_PAUSED_TO_PLAYING:
900 case GST_STATE_PLAYING_TO_PAUSED:
902 gst_buffer_pool_unref (dvdec->pool);
905 case GST_STATE_PAUSED_TO_READY:
906 dv_decoder_free (dvdec->decoder);
907 dvdec->decoder = NULL;
908 gst_bytestream_destroy (dvdec->bs);
910 case GST_STATE_READY_TO_NULL:
916 parent_class->change_state (element);
918 return GST_STATE_SUCCESS;
921 /* Arguments are part of the Gtk+ object system, and these functions
922 * enable the element to respond to various arguments.
925 gst_dvdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
929 /* It's not null if we got it, but it might not be ours */
930 g_return_if_fail(GST_IS_DVDEC(object));
932 /* Get a pointer of the right type. */
933 dvdec = GST_DVDEC(object);
935 /* Check the argument id to see which argument we're setting. */
938 dvdec->clamp_luma = g_value_get_boolean (value);
940 case ARG_CLAMP_CHROMA:
941 dvdec->clamp_chroma = g_value_get_boolean (value);
944 dvdec->quality = g_value_get_flags (value);
947 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
952 /* The set function is simply the inverse of the get fuction. */
954 gst_dvdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
958 /* It's not null if we got it, but it might not be ours */
959 g_return_if_fail(GST_IS_DVDEC(object));
960 dvdec = GST_DVDEC(object);
964 g_value_set_boolean (value, dvdec->clamp_luma);
966 case ARG_CLAMP_CHROMA:
967 g_value_set_boolean (value, dvdec->clamp_chroma);
970 g_value_set_flags (value, dvdec->quality);
973 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
978 /* This is the entry into the plugin itself. When the plugin loads,
979 * this function is called to register everything that the plugin provides.
982 plugin_init (GstPlugin *plugin)
984 if (!gst_library_load ("gstbytestream"))
987 if (!gst_element_register (plugin, "dvdec", GST_RANK_PRIMARY, gst_dvdec_get_type()))
997 "Uses libdv to decode DV video (libdv.sourceforge.net)",