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 = GST_ELEMENT_DETAILS (
42 "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"
46 "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 static GstStaticPadTemplate sink_temp =
75 GST_STATIC_PAD_TEMPLATE
81 "video/x-dv, systemstream = (boolean) true"
85 static GstStaticPadTemplate video_src_temp =
86 GST_STATIC_PAD_TEMPLATE
93 "format = (fourcc) YUY2, "
96 G_STRINGIFY(NTSC_HEIGHT) ", "
97 G_STRINGIFY(PAL_HEIGHT)
99 "framerate = (double) { "
100 G_STRINGIFY(PAL_FRAMERATE) ", "
101 G_STRINGIFY(NTSC_FRAMERATE)
107 "endianness = (int) " G_STRINGIFY(G_BIG_ENDIAN) ", "
108 "red_mask = (int) 0x000000ff, "
109 "green_mask = (int) 0x0000ff00, "
110 "blue_mask = (int) 0x00ff0000, "
111 "width = (int) 720, "
113 G_STRINGIFY(NTSC_HEIGHT) ", "
114 G_STRINGIFY(PAL_HEIGHT)
116 "framerate = (double) { "
117 G_STRINGIFY(PAL_FRAMERATE) ", "
118 G_STRINGIFY(NTSC_FRAMERATE)
124 "endianness = (int) " G_STRINGIFY(G_BIG_ENDIAN) ", "
125 "red_mask = (int) 0x000000ff, "
126 "green_mask = (int) 0x0000ff00, "
127 "blue_mask = (int) 0x00ff0000, "
128 "width = (int) 720, "
130 G_STRINGIFY(NTSC_HEIGHT) ", "
131 G_STRINGIFY(PAL_HEIGHT)
133 "framerate = (double) { "
134 G_STRINGIFY(PAL_FRAMERATE) ", "
135 G_STRINGIFY(NTSC_FRAMERATE)
140 static GstStaticPadTemplate audio_src_temp =
141 GST_STATIC_PAD_TEMPLATE
150 "signed = (boolean) TRUE, "
151 "channels = (int) 2, "
152 "endianness = (int) " G_STRINGIFY(G_LITTLE_ENDIAN) ", "
153 "rate = (int) [ 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 GstPadLinkReturn gst_dvdec_video_link (GstPad *pad,
190 const GstCaps *caps);
191 static GstCaps* gst_dvdec_video_getcaps (GstPad *pad);
193 static const GstEventMask*
194 gst_dvdec_get_event_masks (GstPad *pad);
195 static gboolean gst_dvdec_handle_src_event (GstPad *pad, GstEvent *event);
197 static void gst_dvdec_loop (GstElement *element);
199 static GstElementStateReturn
200 gst_dvdec_change_state (GstElement *element);
202 static void gst_dvdec_set_property (GObject *object, guint prop_id,
203 const GValue *value, GParamSpec *pspec);
204 static void gst_dvdec_get_property (GObject *object, guint prop_id,
205 GValue *value, GParamSpec *pspec);
207 /* The parent class pointer needs to be kept around for some object
210 static GstElementClass *parent_class = NULL;
212 /* This array holds the ids of the signals registered for this object.
213 * The array indexes are based on the enum up above.
215 /*static guint gst_dvdec_signals[LAST_SIGNAL] = { 0 }; */
217 /* This function is used to register and subsequently return the type
218 * identifier for this object class. On first invocation, it will
219 * register the type, providing the name of the class, struct sizes,
220 * and pointers to the various functions that define the class.
223 gst_dvdec_get_type (void)
225 static GType dvdec_type = 0;
228 static const GTypeInfo dvdec_info = {
229 sizeof (GstDVDecClass),
232 (GClassInitFunc) gst_dvdec_class_init,
237 (GInstanceInitFunc) gst_dvdec_init,
239 dvdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstDVDec", &dvdec_info, 0);
245 gst_dvdec_base_init (gpointer g_class)
247 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
249 /* The pad templates can be easily generated from the factories above,
250 * and then added to the list of padtemplates for the elementfactory.
251 * Note that the generated padtemplates are stored in static global
252 * variables, for the gst_dvdec_init function to use later on.
254 gst_element_class_add_pad_template (element_class, gst_static_pad_template_get(&sink_temp));
255 gst_element_class_add_pad_template (element_class, gst_static_pad_template_get(&video_src_temp));
256 gst_element_class_add_pad_template (element_class, gst_static_pad_template_get(&audio_src_temp));
258 gst_element_class_set_details (element_class, &dvdec_details);
261 /* In order to create an instance of an object, the class must be
262 * initialized by this function. GObject will take care of running
263 * it, based on the pointer to the function provided above.
266 gst_dvdec_class_init (GstDVDecClass *klass)
268 /* Class pointers are needed to supply pointers to the private
269 * implementations of parent class methods.
271 GObjectClass *gobject_class;
272 GstElementClass *gstelement_class;
274 /* Since the dvdec class contains the parent classes, you can simply
275 * cast the pointer to get access to the parent classes.
277 gobject_class = (GObjectClass*) klass;
278 gstelement_class = (GstElementClass*) klass;
280 /* The parent class is needed for class method overrides. */
281 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
283 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_LUMA,
284 g_param_spec_boolean ("clamp_luma", "Clamp luma", "Clamp luma",
285 FALSE, G_PARAM_READWRITE));
286 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_CHROMA,
287 g_param_spec_boolean ("clamp_chroma", "Clamp chroma", "Clamp chroma",
288 FALSE, G_PARAM_READWRITE));
289 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
290 g_param_spec_flags ("quality", "Quality", "Decoding quality",
291 GST_TYPE_DVDEC_QUALITY, DV_QUALITY_BEST, G_PARAM_READWRITE));
293 gobject_class->set_property = gst_dvdec_set_property;
294 gobject_class->get_property = gst_dvdec_get_property;
296 gstelement_class->change_state = gst_dvdec_change_state;
298 /* table initialization, only do once */
302 /* This function is responsible for initializing a specific instance of
306 gst_dvdec_init(GstDVDec *dvdec)
309 dvdec->found_header = FALSE;
311 dvdec->sinkpad = gst_pad_new_from_template (gst_static_pad_template_get (&sink_temp), "sink");
312 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->sinkpad);
313 gst_pad_set_query_function (dvdec->sinkpad, NULL);
314 gst_pad_set_convert_function (dvdec->sinkpad, GST_DEBUG_FUNCPTR (gst_dvdec_sink_convert));
315 gst_pad_set_formats_function (dvdec->sinkpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
317 dvdec->videosrcpad = gst_pad_new_from_template (gst_static_pad_template_get (&video_src_temp), "video");
318 gst_pad_set_query_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
319 gst_pad_set_query_type_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
320 gst_pad_set_event_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
321 gst_pad_set_event_mask_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
322 gst_pad_set_convert_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
323 gst_pad_set_formats_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
324 gst_pad_set_getcaps_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_video_getcaps));
325 gst_pad_set_link_function (dvdec->videosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_video_link));
326 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->videosrcpad);
328 dvdec->audiosrcpad = gst_pad_new_from_template (gst_static_pad_template_get (&audio_src_temp), "audio");
329 gst_pad_set_query_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
330 gst_pad_set_query_type_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
331 gst_pad_set_event_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
332 gst_pad_set_event_mask_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
333 gst_pad_set_convert_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_src_convert));
334 gst_pad_set_formats_function (dvdec->audiosrcpad, GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
335 gst_pad_use_explicit_caps (dvdec->audiosrcpad);
336 gst_element_add_pad (GST_ELEMENT(dvdec), dvdec->audiosrcpad);
338 gst_element_set_loop_function (GST_ELEMENT (dvdec), gst_dvdec_loop);
341 dvdec->next_ts = 0LL;
342 dvdec->end_position = -1LL;
343 dvdec->need_discont = FALSE;
344 dvdec->framerate = 0;
346 dvdec->frequency = 0;
349 dvdec->clamp_luma = FALSE;
350 dvdec->clamp_chroma = FALSE;
351 dvdec->quality = DV_QUALITY_BEST;
354 for (i = 0; i <4; i++) {
355 dvdec->audio_buffers[i] = (gint16 *)g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
359 static const GstFormat*
360 gst_dvdec_get_formats (GstPad *pad)
362 static const GstFormat src_formats[] = {
368 static const GstFormat sink_formats[] = {
374 return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
378 gst_dvdec_src_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
379 GstFormat *dest_format, gint64 *dest_value)
385 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
387 if (dvdec->length == 0)
390 if (dvdec->decoder == NULL)
393 switch (src_format) {
394 case GST_FORMAT_BYTES:
395 switch (*dest_format) {
396 case GST_FORMAT_TIME:
401 case GST_FORMAT_TIME:
402 switch (*dest_format) {
403 case GST_FORMAT_BYTES:
404 if (pad == dvdec->videosrcpad)
405 scale = 720 * dvdec->height * dvdec->bpp;
406 else if (pad == dvdec->audiosrcpad)
407 scale = dvdec->decoder->audio->num_channels * 2;
409 case GST_FORMAT_DEFAULT:
410 if (pad == dvdec->videosrcpad)
411 *dest_value = src_value * dvdec->framerate * scale / GST_SECOND;
412 else if (pad == dvdec->audiosrcpad)
413 *dest_value = src_value * dvdec->decoder->audio->frequency * scale / GST_SECOND;
426 gst_dvdec_sink_convert (GstPad *pad, GstFormat src_format, gint64 src_value,
427 GstFormat *dest_format, gint64 *dest_value)
432 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
434 if (dvdec->length == 0)
437 switch (src_format) {
438 case GST_FORMAT_BYTES:
439 switch (*dest_format) {
440 case GST_FORMAT_TIME:
443 /* get frame number */
444 frame = src_value / dvdec->length;
446 *dest_value = (frame * GST_SECOND) / dvdec->framerate;
453 case GST_FORMAT_TIME:
454 switch (*dest_format) {
455 case GST_FORMAT_BYTES:
458 /* calculate the frame */
459 frame = src_value * dvdec->framerate / GST_SECOND;
460 /* calculate the offset */
461 *dest_value = frame * dvdec->length;
474 static const GstQueryType*
475 gst_dvdec_get_src_query_types (GstPad *pad)
477 static const GstQueryType src_query_types[] = {
482 return src_query_types;
486 gst_dvdec_src_query (GstPad *pad, GstQueryType type,
487 GstFormat *format, gint64 *value)
492 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
495 case GST_QUERY_TOTAL:
500 GstFormat tmp_format;
502 len = gst_bytestream_length (dvdec->bs);
503 tmp_format = GST_FORMAT_TIME;
504 if (len == -1 || !gst_pad_convert (dvdec->sinkpad,
511 if (!gst_pad_convert (pad, GST_FORMAT_TIME, *value, format, value)) {
518 case GST_QUERY_POSITION:
521 res = gst_pad_convert (pad, GST_FORMAT_TIME, dvdec->next_ts, format, value);
532 static const GstEventMask*
533 gst_dvdec_get_event_masks (GstPad *pad)
535 static const GstEventMask src_event_masks[] = {
536 { GST_EVENT_SEEK, GST_SEEK_METHOD_SET |
537 GST_SEEK_FLAG_FLUSH },
540 static const GstEventMask sink_event_masks[] = {
541 { GST_EVENT_EOS, 0 },
542 { GST_EVENT_DISCONTINUOUS, 0 },
543 { GST_EVENT_FLUSH, 0 },
547 return (GST_PAD_IS_SRC (pad) ? src_event_masks : sink_event_masks);
551 gst_dvdec_handle_sink_event (GstDVDec *dvdec)
557 gst_bytestream_get_status (dvdec->bs, &remaining, &event);
559 type = event? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
562 case GST_EVENT_FLUSH:
564 case GST_EVENT_DISCONTINUOUS:
567 gboolean found = FALSE;
570 format = GST_FORMAT_TIME;
571 /* try to get a timestamp from the discont formats */
572 for (i = 0; i < GST_EVENT_DISCONT_OFFSET_LEN(event); i++) {
573 if (gst_pad_convert (dvdec->sinkpad,
574 GST_EVENT_DISCONT_OFFSET(event,i).format,
575 GST_EVENT_DISCONT_OFFSET(event,i).value,
576 &format, &dvdec->next_ts))
584 dvdec->next_ts = 0LL;
586 dvdec->need_discont = TRUE;
590 return gst_pad_event_default (dvdec->sinkpad, event);
593 gst_event_unref (event);
598 gst_dvdec_handle_src_event (GstPad *pad, GstEvent *event)
603 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
605 switch (GST_EVENT_TYPE (event)) {
606 case GST_EVENT_SEEK_SEGMENT:
611 /* first bring the format to time */
612 format = GST_FORMAT_TIME;
613 if (!gst_pad_convert (pad,
614 GST_EVENT_SEEK_FORMAT (event),
615 GST_EVENT_SEEK_ENDOFFSET (event),
618 /* could not convert seek format to time offset */
623 dvdec->end_position = position;
624 dvdec->loop = GST_EVENT_SEEK_TYPE (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
631 /* first bring the format to time */
632 format = GST_FORMAT_TIME;
633 if (!gst_pad_convert (pad,
634 GST_EVENT_SEEK_FORMAT (event),
635 GST_EVENT_SEEK_OFFSET (event),
638 /* could not convert seek format to time offset */
642 dvdec->next_ts = position;
643 /* then try to figure out the byteoffset for this time */
644 format = GST_FORMAT_BYTES;
645 if (!gst_pad_convert (dvdec->sinkpad, GST_FORMAT_TIME, position,
648 /* could not convert seek format to byte offset */
653 if (!gst_bytestream_seek (dvdec->bs, position, GST_SEEK_METHOD_SET)) {
656 if (GST_EVENT_TYPE (event) != GST_EVENT_SEEK_SEGMENT)
657 dvdec->end_position = -1;
664 gst_event_unref (event);
669 gst_dvdec_video_getcaps (GstPad *pad)
673 GstPadTemplate *src_pad_template;
675 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
676 src_pad_template = gst_static_pad_template_get (&video_src_temp);
677 caps = gst_caps_copy(gst_pad_template_get_caps (src_pad_template));
679 if (dvdec->found_header)
684 for (i = 0; i < gst_caps_get_size (caps); i++)
686 GstStructure *structure = gst_caps_get_structure (caps, i);
687 gst_structure_set(structure,
688 "height", G_TYPE_INT, dvdec->height,
689 "framerate", G_TYPE_DOUBLE, dvdec->framerate, NULL
697 static GstPadLinkReturn
698 gst_dvdec_video_link (GstPad *pad, const GstCaps *caps)
701 GstStructure *structure;
706 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
708 /* if we did not find a header yet, return delayed */
709 if (!dvdec->found_header)
711 return GST_PAD_LINK_DELAYED;
714 structure = gst_caps_get_structure (caps, 0);
716 /* it worked, try to find what it was again */
717 if (!gst_structure_get_fourcc (structure, "format", &fourcc) ||
718 !gst_structure_get_int (structure, "height", &height) ||
719 !gst_structure_get_double (structure, "framerate", &framerate))
720 return GST_PAD_LINK_REFUSED;
722 if ((height != dvdec->height) || (framerate != dvdec->framerate))
723 return GST_PAD_LINK_REFUSED;
725 if (fourcc == GST_STR_FOURCC ("RGB ")) {
728 gst_structure_get_int (structure, "bpp", &bpp);
730 dvdec->space = e_dv_color_rgb;
734 dvdec->space = e_dv_color_bgr0;
739 dvdec->space = e_dv_color_yuv;
743 return GST_PAD_LINK_OK;
747 gst_dvdec_push (GstDVDec *dvdec, GstBuffer *outbuf, GstPad *pad, GstClockTime ts)
749 GST_BUFFER_TIMESTAMP (outbuf) = ts;
751 if (dvdec->need_discont) {
754 discont = gst_event_new_discontinuous (FALSE, GST_FORMAT_TIME, ts, NULL);
755 gst_pad_push (pad, GST_DATA (discont));
758 gst_pad_push (pad, GST_DATA (outbuf));
760 if ((dvdec->end_position != -1) &&
761 (dvdec->next_ts >= dvdec->end_position)) {
763 gst_pad_push (pad, GST_DATA(gst_event_new (GST_EVENT_SEGMENT_DONE)));
765 gst_pad_push (pad, GST_DATA(gst_event_new (GST_EVENT_EOS)));
770 gst_dvdec_loop (GstElement *element)
773 GstBuffer *buf, *outbuf;
776 guint32 length, got_bytes;
781 dvdec = GST_DVDEC (element);
783 /* first read enough bytes to parse the header */
784 got_bytes = gst_bytestream_peek_bytes (dvdec->bs, &inframe, header_size);
785 if (got_bytes < header_size) {
786 gst_dvdec_handle_sink_event (dvdec);
789 if (dv_parse_header (dvdec->decoder, inframe) < 0)
791 GST_ELEMENT_ERROR (dvdec, STREAM, DECODE, (NULL), (NULL));
795 /* after parsing the header we know the length of the data */
796 dvdec->PAL = dv_system_50_fields (dvdec->decoder);
797 dvdec->found_header = TRUE;
799 fps = (dvdec->PAL ? PAL_FRAMERATE : NTSC_FRAMERATE);
800 height = (dvdec->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
801 length = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
803 if ((dvdec->framerate != fps) ||
804 (dvdec->height != height))
806 dvdec->height = height;
807 dvdec->framerate = fps;
809 if (GST_PAD_LINK_FAILED (gst_pad_renegotiate (dvdec->videosrcpad)))
811 GST_ELEMENT_ERROR (dvdec, CORE, NEGOTIATION, (NULL), (NULL));
816 if (length != dvdec->length) {
817 dvdec->length = length;
818 gst_bytestream_size_hint (dvdec->bs, length);
821 /* then read the read data */
822 got_bytes = gst_bytestream_read (dvdec->bs, &buf, length);
823 if (got_bytes < length) {
824 gst_dvdec_handle_sink_event (dvdec);
828 format = GST_FORMAT_TIME;
829 gst_pad_query (dvdec->videosrcpad, GST_QUERY_POSITION, &format, &ts);
831 dvdec->next_ts += GST_SECOND / dvdec->framerate;
833 dv_parse_packs (dvdec->decoder, GST_BUFFER_DATA (buf));
835 if (GST_PAD_IS_LINKED (dvdec->audiosrcpad)) {
839 dv_decode_full_audio (dvdec->decoder, GST_BUFFER_DATA (buf), dvdec->audio_buffers);
841 if ((dvdec->decoder->audio->frequency != dvdec->frequency) ||
842 (dvdec->decoder->audio->num_channels != dvdec->channels))
844 if (!gst_pad_set_explicit_caps (dvdec->audiosrcpad,
845 gst_caps_new_simple (
847 "rate", G_TYPE_INT, dvdec->decoder->audio->frequency,
848 "depth", G_TYPE_INT, 16,
849 "width", G_TYPE_INT, 16,
850 "signed", G_TYPE_BOOLEAN, TRUE,
851 "channels", G_TYPE_INT, dvdec->decoder->audio->num_channels,
852 "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
856 gst_buffer_unref (buf);
857 GST_ELEMENT_ERROR (dvdec, CORE, NEGOTIATION, (NULL), (NULL));
861 dvdec->frequency = dvdec->decoder->audio->frequency;
862 dvdec->channels = dvdec->decoder->audio->num_channels;
865 outbuf = gst_buffer_new ();
866 GST_BUFFER_SIZE (outbuf) = dvdec->decoder->audio->samples_this_frame *
867 sizeof (gint16) * dvdec->decoder->audio->num_channels;
868 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
870 a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);
872 for (i = 0; i < dvdec->decoder->audio->samples_this_frame; i++) {
873 for (j = 0; j < dvdec->decoder->audio->num_channels; j++) {
874 *(a_ptr++) = dvdec->audio_buffers[j][i];
877 gst_dvdec_push (dvdec, outbuf, dvdec->audiosrcpad, ts);
880 if (GST_PAD_IS_LINKED (dvdec->videosrcpad)) {
882 guint8 *outframe_ptrs[3];
883 gint outframe_pitches[3];
885 outbuf = gst_buffer_new_and_alloc ((720 * height) * dvdec->bpp);
887 outframe = GST_BUFFER_DATA (outbuf);
889 outframe_ptrs[0] = outframe;
890 outframe_pitches[0] = 720 * dvdec->bpp;
892 /* the rest only matters for YUY2 */
893 if (dvdec->bpp < 3) {
894 outframe_ptrs[1] = outframe_ptrs[0] + 720 * height;
895 outframe_ptrs[2] = outframe_ptrs[1] + 360 * height;
897 outframe_pitches[1] = height / 2;
898 outframe_pitches[2] = outframe_pitches[1];
901 dv_decode_full_frame (dvdec->decoder, GST_BUFFER_DATA (buf),
902 dvdec->space, outframe_ptrs, outframe_pitches);
904 gst_dvdec_push (dvdec, outbuf, dvdec->videosrcpad, ts);
907 if ((dvdec->end_position != -1) &&
908 (dvdec->next_ts >= dvdec->end_position) &&
910 gst_element_set_eos (GST_ELEMENT (dvdec));
913 if (dvdec->need_discont) {
914 dvdec->need_discont = FALSE;
917 gst_buffer_unref (buf);
920 static GstElementStateReturn
921 gst_dvdec_change_state (GstElement *element)
923 GstDVDec *dvdec = GST_DVDEC (element);
925 switch (GST_STATE_TRANSITION (element)) {
926 case GST_STATE_NULL_TO_READY:
928 case GST_STATE_READY_TO_PAUSED:
929 dvdec->bs = gst_bytestream_new (dvdec->sinkpad);
930 dvdec->decoder = dv_decoder_new (0, dvdec->clamp_luma, dvdec->clamp_chroma);
931 dvdec->decoder->quality = dvdec->quality;
933 case GST_STATE_PAUSED_TO_PLAYING:
935 case GST_STATE_PLAYING_TO_PAUSED:
937 case GST_STATE_PAUSED_TO_READY:
938 dv_decoder_free (dvdec->decoder);
939 dvdec->decoder = NULL;
940 dvdec->found_header = FALSE;
941 gst_bytestream_destroy (dvdec->bs);
943 case GST_STATE_READY_TO_NULL:
949 parent_class->change_state (element);
951 return GST_STATE_SUCCESS;
954 /* Arguments are part of the Gtk+ object system, and these functions
955 * enable the element to respond to various arguments.
958 gst_dvdec_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
962 /* It's not null if we got it, but it might not be ours */
963 g_return_if_fail(GST_IS_DVDEC(object));
965 /* Get a pointer of the right type. */
966 dvdec = GST_DVDEC(object);
968 /* Check the argument id to see which argument we're setting. */
971 dvdec->clamp_luma = g_value_get_boolean (value);
973 case ARG_CLAMP_CHROMA:
974 dvdec->clamp_chroma = g_value_get_boolean (value);
977 dvdec->quality = g_value_get_flags (value);
980 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
985 /* The set function is simply the inverse of the get fuction. */
987 gst_dvdec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
991 /* It's not null if we got it, but it might not be ours */
992 g_return_if_fail(GST_IS_DVDEC(object));
993 dvdec = GST_DVDEC(object);
997 g_value_set_boolean (value, dvdec->clamp_luma);
999 case ARG_CLAMP_CHROMA:
1000 g_value_set_boolean (value, dvdec->clamp_chroma);
1003 g_value_set_flags (value, dvdec->quality);
1006 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1011 /* This is the entry into the plugin itself. When the plugin loads,
1012 * this function is called to register everything that the plugin provides.
1015 plugin_init (GstPlugin *plugin)
1017 if (!gst_library_load ("gstbytestream"))
1020 if (!gst_element_register (plugin, "dvdec", GST_RANK_PRIMARY, gst_dvdec_get_type()))
1030 "Uses libdv to decode DV video (libdv.sourceforge.net)",