2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * <2005> Wim Taymans <wim@fluendo.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
27 /* First, include the header file for the plugin, to bring in the
28 * object definition and other useful things.
32 /* DV output has two modes, normal and wide. The resolution is the same in both
33 * cases: 720 pixels wide by 576 pixels tall in PAL format, and 720x480 for
36 * Each of the modes has its own pixel aspect ratio, which is fixed in practice
37 * by ITU-R BT.601 (also known as "CCIR-601" or "Rec.601"). Or so claims a
38 * reference that I culled from the reliable "internet",
39 * http://www.mir.com/DMG/aspect.html. Normal PAL is 59/54 and normal NTSC is
40 * 10/11. Because the pixel resolution is the same for both cases, we can get
41 * the pixel aspect ratio for wide recordings by multiplying by the ratio of
42 * display aspect ratios, 16/9 (for wide) divided by 4/3 (for normal):
44 * Wide NTSC: 10/11 * (16/9)/(4/3) = 40/33
45 * Wide PAL: 59/54 * (16/9)/(4/3) = 118/81
47 * However, the pixel resolution coming out of a DV source does not combine with
48 * the standard pixel aspect ratios to give a proper display aspect ratio. An
49 * image 480 pixels tall, with a 4:3 display aspect ratio, will be 768 pixels
50 * wide. But, if we take the normal PAL aspect ratio of 59/54, and multiply it
51 * with the width of the DV image (720 pixels), we get 786.666..., which is
52 * nonintegral and too wide. The camera is not outputting a 4:3 image.
54 * If the video sink for this stream has fixed dimensions (such as for
55 * fullscreen playback, or for a java applet in a web page), you then have two
56 * choices. Either you show the whole image, but pad the image with black
57 * borders on the top and bottom (like watching a widescreen video on a 4:3
58 * device), or you crop the video to the proper ratio. Apparently the latter is
59 * the standard practice.
61 * For its part, GStreamer is concerned with accuracy and preservation of
62 * information. This element outputs the 720x576 or 720x480 video that it
63 * recieves, noting the proper aspect ratio. This should not be a problem for
64 * windowed applications, which can change size to fit the video. Applications
65 * with fixed size requirements should decide whether to crop or pad which
66 * an element such as videobox can do.
69 #define NTSC_HEIGHT 480
70 #define NTSC_BUFFER 120000
71 #define NTSC_FRAMERATE 30000/1001.
73 #define PAL_HEIGHT 576
74 #define PAL_BUFFER 144000
75 #define PAL_FRAMERATE 25.0
77 #define PAL_NORMAL_PAR_X 59
78 #define PAL_NORMAL_PAR_Y 54
79 #define PAL_WIDE_PAR_X 118
80 #define PAL_WIDE_PAR_Y 81
82 #define NTSC_NORMAL_PAR_X 10
83 #define NTSC_NORMAL_PAR_Y 11
84 #define NTSC_WIDE_PAR_X 40
85 #define NTSC_WIDE_PAR_Y 33
87 /* The ElementDetails structure gives a human-readable description
88 * of the plugin, as well as author and version data.
90 static GstElementDetails dvdec_details =
91 GST_ELEMENT_DETAILS ("DV (smpte314) decoder plugin",
92 "Codec/Decoder/Video",
93 "Uses libdv to decode DV video (libdv.sourceforge.net)",
94 "Erik Walthinsen <omega@cse.ogi.edu>," "Wim Taymans <wim@fluendo.com>");
97 /* These are the signals that this element can fire. They are zero-
98 * based because the numbers themselves are private to the object.
99 * LAST_SIGNAL is used for initialization of the signal array.
107 #define DV_DEFAULT_QUALITY DV_QUALITY_BEST
108 #define DV_DEFAULT_DECODE_NTH 1
110 /* Arguments are identified the same way, but cannot be zero, so you
111 * must leave the ARG_0 entry in as a placeholder.
123 const gint qualities[] = {
127 DV_QUALITY_DC | DV_QUALITY_COLOR,
128 DV_QUALITY_AC_1 | DV_QUALITY_COLOR,
129 DV_QUALITY_AC_2 | DV_QUALITY_COLOR
132 /* The PadFactory structures describe what pads the element has or
133 * can have. They can be quite complex, but for this dvdec plugin
134 * they are rather simple.
136 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
139 GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) true")
142 static GstStaticPadTemplate video_src_temp = GST_STATIC_PAD_TEMPLATE ("video",
145 GST_STATIC_CAPS ("video/x-raw-yuv, "
146 "format = (fourcc) YUY2, "
147 "width = (int) 720, "
149 G_STRINGIFY (NTSC_HEIGHT) ", " G_STRINGIFY (PAL_HEIGHT)
151 "pixel-aspect-ratio=(fraction) { "
152 G_STRINGIFY (PAL_NORMAL_PAR_X) "/" G_STRINGIFY (PAL_NORMAL_PAR_Y) ","
153 G_STRINGIFY (PAL_WIDE_PAR_X) "/" G_STRINGIFY (PAL_WIDE_PAR_Y) ","
154 G_STRINGIFY (NTSC_NORMAL_PAR_X) "/" G_STRINGIFY (NTSC_NORMAL_PAR_Y) ","
155 G_STRINGIFY (NTSC_WIDE_PAR_X) "/" G_STRINGIFY (NTSC_WIDE_PAR_Y) "},"
156 "framerate = (double) [ 1.0, 60.0 ];"
160 "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
161 "red_mask = (int) 0x0000ff00, "
162 "green_mask = (int) 0x00ff0000, "
163 "blue_mask = (int) 0xff000000, "
164 "width = (int) 720, "
166 G_STRINGIFY (NTSC_HEIGHT) ", " G_STRINGIFY (PAL_HEIGHT)
168 "pixel-aspect-ratio=(fraction) { "
169 G_STRINGIFY (PAL_NORMAL_PAR_X) "/" G_STRINGIFY (PAL_NORMAL_PAR_Y) ","
170 G_STRINGIFY (PAL_WIDE_PAR_X) "/" G_STRINGIFY (PAL_WIDE_PAR_Y) ","
171 G_STRINGIFY (NTSC_NORMAL_PAR_X) "/" G_STRINGIFY (NTSC_NORMAL_PAR_Y) ","
172 G_STRINGIFY (NTSC_WIDE_PAR_X) "/" G_STRINGIFY (NTSC_WIDE_PAR_Y) "},"
173 "framerate = (double) [ 1.0, 60.0 ];"
177 "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
178 "red_mask = (int) 0x00ff0000, "
179 "green_mask = (int) 0x0000ff00, "
180 "blue_mask = (int) 0x000000ff, "
181 "width = (int) 720, "
183 G_STRINGIFY (NTSC_HEIGHT) ", " G_STRINGIFY (PAL_HEIGHT)
185 "pixel-aspect-ratio=(fraction) { "
186 G_STRINGIFY (PAL_NORMAL_PAR_X) "/" G_STRINGIFY (PAL_NORMAL_PAR_Y) ","
187 G_STRINGIFY (PAL_WIDE_PAR_X) "/" G_STRINGIFY (PAL_WIDE_PAR_Y) ","
188 G_STRINGIFY (NTSC_NORMAL_PAR_X) "/" G_STRINGIFY (NTSC_NORMAL_PAR_Y) ","
189 G_STRINGIFY (NTSC_WIDE_PAR_X) "/" G_STRINGIFY (NTSC_WIDE_PAR_Y) "},"
190 "framerate = (double) [ 1.0, 60.0 ]")
193 static GstStaticPadTemplate audio_src_temp = GST_STATIC_PAD_TEMPLATE ("audio",
196 GST_STATIC_CAPS ("audio/x-raw-int, "
199 "signed = (boolean) TRUE, "
200 "channels = (int) {2, 4}, "
201 "endianness = (int) " G_STRINGIFY (G_LITTLE_ENDIAN) ", "
202 "rate = (int) { 32000, 44100, 48000 }")
205 #define GST_TYPE_DVDEC_QUALITY (gst_dvdec_quality_get_type())
207 gst_dvdec_quality_get_type (void)
209 static GType qtype = 0;
212 static const GEnumValue values[] = {
213 {0, "DV_QUALITY_FASTEST", "Fastest decoding, low-quality mono"},
214 {1, "DV_QUALITY_AC_1", "Mono decoding using the first AC coefficient"},
215 {2, "DV_QUALITY_AC_2", "Highest quality mono decoding"},
216 {3, "DV_QUALITY_DC|DV_QUALITY_COLOUR", "Fastest colour decoding"},
217 {4, "DV_QUALITY_AC_1|DV_QUALITY_COLOUR",
218 "Colour, using only the first AC coefficient"},
219 {5, "DV_QUALITY_BEST", "Highest quality colour decoding"},
223 qtype = g_enum_register_static ("GstDVDecQualityEnum", values);
228 /* A number of functon prototypes are given so we can refer to them later. */
229 static void gst_dvdec_base_init (gpointer g_class);
230 static void gst_dvdec_class_init (GstDVDecClass * klass);
231 static void gst_dvdec_init (GstDVDec * dvdec);
233 static const GstQueryType *gst_dvdec_get_src_query_types (GstPad * pad);
234 static gboolean gst_dvdec_src_query (GstPad * pad, GstQuery * query);
235 static const GstQueryType *gst_dvdec_get_sink_query_types (GstPad * pad);
236 static gboolean gst_dvdec_sink_query (GstPad * pad, GstQuery * query);
239 static const GstFormat *gst_dvdec_get_formats (GstPad * pad);
241 static gboolean gst_dvdec_sink_convert (GstPad * pad, GstFormat src_format,
242 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
243 static gboolean gst_dvdec_src_convert (GstPad * pad, GstFormat src_format,
244 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
246 static gboolean gst_dvdec_video_setcaps (GstPad * pad, GstCaps * caps);
247 static GstCaps *gst_dvdec_video_getcaps (GstPad * pad);
250 static const GstEventMask *gst_dvdec_get_event_masks (GstPad * pad);
252 static gboolean gst_dvdec_handle_src_event (GstPad * pad, GstEvent * event);
253 static GstFlowReturn gst_dvdec_flush (GstDVDec * dvdec);
254 static GstFlowReturn gst_dvdec_chain (GstPad * pad, GstBuffer * buffer);
255 static gboolean gst_dvdec_handle_sink_event (GstPad * pad, GstEvent * event);
257 static GstElementStateReturn gst_dvdec_change_state (GstElement * element);
259 static void gst_dvdec_set_property (GObject * object, guint prop_id,
260 const GValue * value, GParamSpec * pspec);
261 static void gst_dvdec_get_property (GObject * object, guint prop_id,
262 GValue * value, GParamSpec * pspec);
264 /* The parent class pointer needs to be kept around for some object
267 static GstElementClass *parent_class = NULL;
269 /* This array holds the ids of the signals registered for this object.
270 * The array indexes are based on the enum up above.
272 /*static guint gst_dvdec_signals[LAST_SIGNAL] = { 0 }; */
274 /* This function is used to register and subsequently return the type
275 * identifier for this object class. On first invocation, it will
276 * register the type, providing the name of the class, struct sizes,
277 * and pointers to the various functions that define the class.
280 gst_dvdec_get_type (void)
282 static GType dvdec_type = 0;
285 static const GTypeInfo dvdec_info = {
286 sizeof (GstDVDecClass),
289 (GClassInitFunc) gst_dvdec_class_init,
294 (GInstanceInitFunc) gst_dvdec_init,
298 g_type_register_static (GST_TYPE_ELEMENT, "GstDVDec", &dvdec_info, 0);
304 gst_dvdec_base_init (gpointer g_class)
306 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
308 /* The pad templates can be easily generated from the factories above,
309 * and then added to the list of padtemplates for the elementfactory.
310 * Note that the generated padtemplates are stored in static global
311 * variables, for the gst_dvdec_init function to use later on.
313 gst_element_class_add_pad_template (element_class,
314 gst_static_pad_template_get (&sink_temp));
315 gst_element_class_add_pad_template (element_class,
316 gst_static_pad_template_get (&video_src_temp));
317 gst_element_class_add_pad_template (element_class,
318 gst_static_pad_template_get (&audio_src_temp));
320 gst_element_class_set_details (element_class, &dvdec_details);
323 /* In order to create an instance of an object, the class must be
324 * initialized by this function. GObject will take care of running
325 * it, based on the pointer to the function provided above.
328 gst_dvdec_class_init (GstDVDecClass * klass)
330 /* Class pointers are needed to supply pointers to the private
331 * implementations of parent class methods.
333 GObjectClass *gobject_class;
334 GstElementClass *gstelement_class;
336 /* Since the dvdec class contains the parent classes, you can simply
337 * cast the pointer to get access to the parent classes.
339 gobject_class = (GObjectClass *) klass;
340 gstelement_class = (GstElementClass *) klass;
342 gobject_class->set_property = gst_dvdec_set_property;
343 gobject_class->get_property = gst_dvdec_get_property;
345 /* The parent class is needed for class method overrides. */
346 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
348 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_LUMA,
349 g_param_spec_boolean ("clamp_luma", "Clamp luma", "Clamp luma",
350 FALSE, G_PARAM_READWRITE));
351 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CLAMP_CHROMA,
352 g_param_spec_boolean ("clamp_chroma", "Clamp chroma", "Clamp chroma",
353 FALSE, G_PARAM_READWRITE));
354 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
355 g_param_spec_enum ("quality", "Quality", "Decoding quality",
356 GST_TYPE_DVDEC_QUALITY, DV_DEFAULT_QUALITY, G_PARAM_READWRITE));
357 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DECODE_NTH,
358 g_param_spec_int ("drop-factor", "Drop Factor", "Only decode Nth frame",
359 1, G_MAXINT, DV_DEFAULT_DECODE_NTH, G_PARAM_READWRITE));
361 gstelement_class->change_state = gst_dvdec_change_state;
363 /* table initialization, only do once */
367 /* This function is responsible for initializing a specific instance of
371 gst_dvdec_init (GstDVDec * dvdec)
376 gst_pad_new_from_template (gst_static_pad_template_get (&sink_temp),
378 gst_pad_set_chain_function (dvdec->sinkpad, gst_dvdec_chain);
379 gst_pad_set_event_function (dvdec->sinkpad, gst_dvdec_handle_sink_event);
380 gst_pad_set_query_function (dvdec->sinkpad,
381 GST_DEBUG_FUNCPTR (gst_dvdec_sink_query));
382 gst_pad_set_query_type_function (dvdec->sinkpad,
383 GST_DEBUG_FUNCPTR (gst_dvdec_get_sink_query_types));
385 gst_pad_set_formats_function (dvdec->sinkpad,
386 GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
388 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->sinkpad);
391 gst_pad_new_from_template (gst_static_pad_template_get (&video_src_temp),
393 gst_pad_set_query_function (dvdec->videosrcpad,
394 GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
395 gst_pad_set_query_type_function (dvdec->videosrcpad,
396 GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
397 gst_pad_set_event_function (dvdec->videosrcpad,
398 GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
400 gst_pad_set_event_mask_function (dvdec->videosrcpad,
401 GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
402 gst_pad_set_formats_function (dvdec->videosrcpad,
403 GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
405 gst_pad_set_getcaps_function (dvdec->videosrcpad,
406 GST_DEBUG_FUNCPTR (gst_dvdec_video_getcaps));
407 gst_pad_set_setcaps_function (dvdec->videosrcpad,
408 GST_DEBUG_FUNCPTR (gst_dvdec_video_setcaps));
409 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->videosrcpad);
412 gst_pad_new_from_template (gst_static_pad_template_get (&audio_src_temp),
414 gst_pad_set_query_function (dvdec->audiosrcpad,
415 GST_DEBUG_FUNCPTR (gst_dvdec_src_query));
416 gst_pad_set_query_type_function (dvdec->audiosrcpad,
417 GST_DEBUG_FUNCPTR (gst_dvdec_get_src_query_types));
418 gst_pad_set_event_function (dvdec->audiosrcpad,
419 GST_DEBUG_FUNCPTR (gst_dvdec_handle_src_event));
421 gst_pad_set_event_mask_function (dvdec->audiosrcpad,
422 GST_DEBUG_FUNCPTR (gst_dvdec_get_event_masks));
423 gst_pad_set_formats_function (dvdec->audiosrcpad,
424 GST_DEBUG_FUNCPTR (gst_dvdec_get_formats));
427 /* once we set caps, this is the only thing we can do */
428 gst_pad_use_fixed_caps (dvdec->audiosrcpad);
429 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->audiosrcpad);
431 dvdec->adapter = gst_adapter_new ();
433 dvdec->timestamp = 0LL;
435 dvdec->start_timestamp = -1LL;
436 dvdec->stop_timestamp = -1LL;
437 dvdec->need_discont = FALSE;
438 dvdec->new_media = FALSE;
439 dvdec->framerate = 0;
441 dvdec->frequency = 0;
444 dvdec->drop_factor = 1;
446 dvdec->clamp_luma = FALSE;
447 dvdec->clamp_chroma = FALSE;
448 dvdec->quality = DV_DEFAULT_QUALITY;
451 for (i = 0; i < 4; i++) {
452 dvdec->audio_buffers[i] =
453 (gint16 *) g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
458 static const GstFormat *
459 gst_dvdec_get_formats (GstPad * pad)
461 static const GstFormat src_formats[] = {
467 static const GstFormat sink_formats[] = {
474 return (GST_PAD_IS_SRC (pad) ? src_formats : sink_formats);
479 gst_dvdec_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
480 GstFormat * dest_format, gint64 * dest_value)
485 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
486 if (dvdec->frame_len == -1)
489 if (dvdec->decoder == NULL)
492 if (*dest_format == src_format) {
493 *dest_value = src_value;
497 switch (src_format) {
498 case GST_FORMAT_BYTES:
499 switch (*dest_format) {
500 case GST_FORMAT_BYTES:
501 *dest_value = src_value;
503 case GST_FORMAT_DEFAULT:
504 case GST_FORMAT_TIME:
505 *dest_format = GST_FORMAT_TIME;
506 if (pad == dvdec->videosrcpad)
507 *dest_value = src_value * GST_SECOND /
508 (720 * dvdec->height * dvdec->bpp * dvdec->framerate /
510 else if (pad == dvdec->audiosrcpad)
511 *dest_value = src_value * GST_SECOND /
512 (2 * dvdec->frequency * dvdec->channels);
518 case GST_FORMAT_TIME:
519 switch (*dest_format) {
520 case GST_FORMAT_BYTES:
521 if (pad == dvdec->videosrcpad)
522 *dest_value = src_value * 720 * dvdec->height * dvdec->bpp *
523 dvdec->framerate / GST_SECOND;
524 else if (pad == dvdec->audiosrcpad)
525 *dest_value = 2 * src_value * dvdec->frequency *
526 dvdec->channels / GST_SECOND;
528 case GST_FORMAT_TIME:
529 case GST_FORMAT_DEFAULT:
530 *dest_format = GST_FORMAT_TIME;
531 *dest_value = src_value;
542 gst_object_unref (dvdec);
548 gst_object_unref (dvdec);
554 gst_dvdec_sink_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
555 GstFormat * dest_format, gint64 * dest_value)
561 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
563 if (dvdec->frame_len <= 0)
566 GST_DEBUG ("%d -> %d", src_format, *dest_format);
568 if (*dest_format == GST_FORMAT_DEFAULT)
569 *dest_format = GST_FORMAT_TIME;
571 if (*dest_format == src_format) {
572 *dest_value = src_value;
576 framerate = (dvdec->PAL ? PAL_FRAMERATE : NTSC_FRAMERATE);
578 switch (src_format) {
579 case GST_FORMAT_BYTES:
580 switch (*dest_format) {
581 case GST_FORMAT_TIME:
585 /* get frame number */
586 frame = src_value / dvdec->frame_len;
588 *dest_value = (frame * GST_SECOND) / framerate;
595 case GST_FORMAT_TIME:
596 switch (*dest_format) {
597 case GST_FORMAT_BYTES:
601 /* calculate the frame */
602 frame = src_value * framerate / GST_SECOND;
603 /* calculate the offset */
604 *dest_value = frame * dvdec->frame_len;
616 gst_object_unref (dvdec);
621 gst_object_unref (dvdec);
626 static const GstQueryType *
627 gst_dvdec_get_src_query_types (GstPad * pad)
629 static const GstQueryType src_query_types[] = {
635 return src_query_types;
639 gst_dvdec_src_query (GstPad * pad, GstQuery * query)
644 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
646 switch (GST_QUERY_TYPE (query)) {
647 case GST_QUERY_POSITION:
653 /* get target format */
654 gst_query_parse_position (query, &format, NULL, NULL);
656 /* change query to perform on peer */
657 gst_query_set_position (query, GST_FORMAT_BYTES, -1, -1);
659 if ((peer = gst_pad_get_peer (dvdec->sinkpad))) {
660 /* ask peer for total length */
661 if (!(res = gst_pad_query (peer, query))) {
662 gst_object_unref (peer);
666 /* get peer total length */
667 gst_query_parse_position (query, NULL, NULL, &end);
669 /* convert end to requested format */
671 if (!(res = gst_pad_query_convert (dvdec->sinkpad,
672 GST_FORMAT_BYTES, end, &format, &end))) {
673 gst_object_unref (peer);
677 gst_object_unref (peer);
681 /* bring the position to the requested format. */
682 if (!(res = gst_pad_query_convert (pad,
683 GST_FORMAT_TIME, dvdec->timestamp, &format, &cur)))
686 gst_query_set_position (query, format, cur, end);
689 case GST_QUERY_CONVERT:
691 GstFormat src_fmt, dest_fmt;
692 gint64 src_val, dest_val;
694 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
696 gst_dvdec_src_convert (pad, src_fmt, src_val, &dest_fmt,
699 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
706 gst_object_unref (dvdec);
712 gst_object_unref (dvdec);
713 GST_DEBUG ("error handling event");
718 static const GstQueryType *
719 gst_dvdec_get_sink_query_types (GstPad * pad)
721 static const GstQueryType sink_query_types[] = {
726 return sink_query_types;
730 gst_dvdec_sink_query (GstPad * pad, GstQuery * query)
735 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
737 switch (GST_QUERY_TYPE (query)) {
738 case GST_QUERY_CONVERT:
740 GstFormat src_fmt, dest_fmt;
741 gint64 src_val, dest_val;
743 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
745 gst_dvdec_sink_convert (pad, src_fmt, src_val, &dest_fmt,
748 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
755 gst_object_unref (dvdec);
761 gst_object_unref (dvdec);
762 GST_DEBUG ("error handling event");
768 gst_dvdec_send_event (GstDVDec * dvdec, GstEvent * event)
770 gboolean res = FALSE;
772 gst_event_ref (event);
773 res |= gst_pad_push_event (dvdec->videosrcpad, event);
774 res |= gst_pad_push_event (dvdec->audiosrcpad, event);
780 gst_dvdec_handle_sink_event (GstPad * pad, GstEvent * event)
782 GstDVDec *dvdec = GST_DVDEC (gst_pad_get_parent (pad));
785 switch (GST_EVENT_TYPE (event)) {
786 case GST_EVENT_FLUSH_START:
787 /* we are not blocking on anything exect the push() calls
788 * to the peer which will be unblocked by forwarding the
790 res = gst_dvdec_send_event (dvdec, event);
792 /* and wait till streaming stops, not strictly needed as
793 * the peer calling us will do the same. */
794 GST_STREAM_LOCK (pad);
795 GST_STREAM_UNLOCK (pad);
797 case GST_EVENT_FLUSH_STOP:
798 GST_STREAM_LOCK (pad);
799 gst_adapter_clear (dvdec->adapter);
800 GST_DEBUG ("cleared adapter");
801 res = gst_dvdec_send_event (dvdec, event);
802 GST_STREAM_UNLOCK (pad);
804 case GST_EVENT_NEWSEGMENT:
808 GST_STREAM_LOCK (pad);
810 /* parse byte start and stop positions */
811 gst_event_parse_newsegment (event, NULL, &format,
812 &dvdec->start_byte, &dvdec->stop_byte, NULL);
814 /* and queue a DISCONT before sending the next set of buffers */
815 dvdec->need_discont = TRUE;
816 gst_event_unref (event);
817 GST_STREAM_UNLOCK (pad);
822 GST_STREAM_LOCK (pad);
823 /* flush any pending data */
824 gst_dvdec_flush (dvdec);
826 res = gst_dvdec_send_event (dvdec, event);
827 /* and clear the adapter */
828 gst_adapter_clear (dvdec->adapter);
829 GST_STREAM_UNLOCK (pad);
833 gst_object_unref (dvdec);
839 gst_dvdec_handle_src_event (GstPad * pad, GstEvent * event)
844 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
846 switch (GST_EVENT_TYPE (event)) {
851 GstFormat format, conv;
854 GstSeekType cur_type, stop_type;
856 gint64 start_position, end_position;
858 gst_event_parse_seek (event, &rate, &format, &flags,
859 &cur_type, &cur, &stop_type, &stop);
861 if ((offset = cur) != -1) {
862 /* bring the format to time on srcpad. */
863 conv = GST_FORMAT_TIME;
864 if (!(res = gst_pad_query_convert (pad,
865 format, offset, &conv, &start_position))) {
866 /* could not convert seek format to time offset */
869 /* and convert to bytes on sinkpad. */
870 conv = GST_FORMAT_BYTES;
871 if (!(res = gst_pad_query_convert (dvdec->sinkpad,
872 GST_FORMAT_TIME, start_position, &conv, &start_position))) {
873 /* could not convert time format to bytes offset */
880 if ((offset = stop) != -1) {
881 /* bring the format to time on srcpad. */
882 conv = GST_FORMAT_TIME;
883 if (!(res = gst_pad_query_convert (pad,
884 format, offset, &conv, &end_position))) {
885 /* could not convert seek format to time offset */
888 conv = GST_FORMAT_BYTES;
889 /* and convert to bytes on sinkpad. */
890 if (!(res = gst_pad_query_convert (dvdec->sinkpad,
891 GST_FORMAT_TIME, end_position, &conv, &end_position))) {
892 /* could not convert seek format to bytes offset */
898 /* now this is the updated seek event on bytes */
899 newevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
900 cur_type, start_position, stop_type, end_position);
902 res = gst_pad_push_event (dvdec->sinkpad, newevent);
909 gst_event_unref (event);
911 gst_object_unref (dvdec);
917 gst_dvdec_video_getcaps (GstPad * pad)
921 GstPadTemplate *src_pad_template;
923 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
925 src_pad_template = gst_static_pad_template_get (&video_src_temp);
926 caps = gst_caps_copy (gst_pad_template_get_caps (src_pad_template));
928 if (dvdec->found_header) {
934 par_x = PAL_WIDE_PAR_X;
935 par_y = PAL_WIDE_PAR_Y;
937 par_x = PAL_NORMAL_PAR_X;
938 par_y = PAL_NORMAL_PAR_Y;
942 par_x = NTSC_WIDE_PAR_X;
943 par_y = NTSC_WIDE_PAR_Y;
945 par_x = NTSC_NORMAL_PAR_X;
946 par_y = NTSC_NORMAL_PAR_Y;
950 for (i = 0; i < gst_caps_get_size (caps); i++) {
951 GstStructure *structure = gst_caps_get_structure (caps, i);
953 gst_structure_set (structure,
954 "height", G_TYPE_INT, dvdec->height,
955 "framerate", G_TYPE_DOUBLE, dvdec->framerate / dvdec->drop_factor,
956 "pixel-aspect-ratio", GST_TYPE_FRACTION, par_x, par_y, NULL);
960 gst_object_unref (dvdec);
966 gst_dvdec_video_setcaps (GstPad * pad, GstCaps * caps)
969 GstStructure *structure;
973 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
975 structure = gst_caps_get_structure (caps, 0);
977 if (!gst_structure_get_int (structure, "height", &height) ||
978 !gst_structure_get_double (structure, "framerate", &framerate))
981 /* allow a margin of error for the framerate caused by float rounding errors */
982 if ((height != dvdec->height) ||
983 (fabs (framerate - (dvdec->framerate / dvdec->drop_factor)) > 0.00000001))
986 if (strcmp (gst_structure_get_name (structure), "video/x-raw-rgb") == 0) {
989 GST_DEBUG ("got rgb format");
991 gst_structure_get_int (structure, "bpp", &bpp);
993 dvdec->space = e_dv_color_rgb;
995 GST_DEBUG ("configured rgb24");
997 dvdec->space = e_dv_color_bgr0;
999 GST_DEBUG ("configured bgr32");
1004 if (!gst_structure_get_fourcc (structure, "format", &fourcc))
1007 if (fourcc != GST_STR_FOURCC ("YUY2"))
1010 dvdec->space = e_dv_color_yuv;
1012 GST_DEBUG ("configured YUY2");
1015 gst_object_unref (dvdec);
1021 GST_DEBUG ("unsupported format");
1022 gst_object_unref (dvdec);
1027 static GstFlowReturn
1028 gst_dvdec_decode_audio (GstDVDec * dvdec, const guint8 * data)
1031 gint frequency, channels;
1034 frequency = dv_get_frequency (dvdec->decoder);
1035 channels = dv_get_num_channels (dvdec->decoder);
1037 /* check if format changed */
1038 if ((frequency != dvdec->frequency) || (channels != dvdec->channels)) {
1041 dvdec->frequency = frequency;
1042 dvdec->channels = channels;
1044 /* and set new caps */
1045 caps = gst_caps_new_simple ("audio/x-raw-int",
1046 "rate", G_TYPE_INT, frequency,
1047 "depth", G_TYPE_INT, 16,
1048 "width", G_TYPE_INT, 16,
1049 "signed", G_TYPE_BOOLEAN, TRUE,
1050 "channels", G_TYPE_INT, channels,
1051 "endianness", G_TYPE_INT, G_LITTLE_ENDIAN, NULL);
1052 gst_pad_set_caps (dvdec->audiosrcpad, caps);
1053 gst_caps_unref (caps);
1056 dv_decode_full_audio (dvdec->decoder, data, dvdec->audio_buffers);
1058 if ((num_samples = dv_get_num_samples (dvdec->decoder)) > 0) {
1063 outbuf = gst_buffer_new_and_alloc (num_samples *
1064 sizeof (gint16) * dvdec->channels);
1066 a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);
1068 for (i = 0; i < num_samples; i++) {
1069 for (j = 0; j < dvdec->channels; j++) {
1070 *(a_ptr++) = dvdec->audio_buffers[j][i];
1074 GST_DEBUG ("pushing audio %" GST_TIME_FORMAT,
1075 GST_TIME_ARGS (dvdec->timestamp));
1077 GST_BUFFER_TIMESTAMP (outbuf) = dvdec->timestamp;
1078 GST_BUFFER_DURATION (outbuf) = dvdec->duration;
1079 GST_BUFFER_OFFSET (outbuf) = dvdec->audio_offset;
1080 dvdec->audio_offset += num_samples;
1081 GST_BUFFER_OFFSET_END (outbuf) = dvdec->audio_offset;
1083 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dvdec->audiosrcpad));
1085 ret = gst_pad_push (dvdec->audiosrcpad, outbuf);
1094 static GstFlowReturn
1095 gst_dvdec_decode_video (GstDVDec * dvdec, const guint8 * data)
1098 guint8 *outframe_ptrs[3];
1099 gint outframe_pitches[3];
1104 GstFlowReturn ret = GST_FLOW_OK;
1107 framerate = (dvdec->PAL ? PAL_FRAMERATE : NTSC_FRAMERATE);
1108 height = (dvdec->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
1109 wide = dv_format_wide (dvdec->decoder);
1111 /* see if anything changed */
1112 if ((dvdec->framerate != framerate) || (dvdec->height != height)
1113 || dvdec->wide != wide) {
1115 gboolean setcaps_ret;
1118 dvdec->framerate = framerate;
1119 dvdec->height = height;
1124 par_x = PAL_WIDE_PAR_X;
1125 par_y = PAL_WIDE_PAR_Y;
1127 par_x = PAL_NORMAL_PAR_X;
1128 par_y = PAL_NORMAL_PAR_Y;
1132 par_x = NTSC_WIDE_PAR_X;
1133 par_y = NTSC_WIDE_PAR_Y;
1135 par_x = NTSC_NORMAL_PAR_X;
1136 par_y = NTSC_NORMAL_PAR_Y;
1140 /* and set new caps, this will be used as suggestion in alloc
1142 caps = gst_caps_new_simple ("video/x-raw-yuv",
1143 "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("YUY2"),
1144 "width", G_TYPE_INT, 720,
1145 "height", G_TYPE_INT, height,
1146 "framerate", G_TYPE_DOUBLE, framerate / dvdec->drop_factor,
1147 "pixel-aspect-ratio", GST_TYPE_FRACTION, par_x, par_y, NULL);
1148 setcaps_ret = gst_pad_set_caps (dvdec->videosrcpad, caps);
1149 gst_caps_unref (caps);
1150 g_return_val_if_fail (setcaps_ret == TRUE, GST_FLOW_UNEXPECTED);
1154 /* check if we can skip this frame */
1155 dvdec->framecount++;
1156 if (dvdec->framecount < dvdec->drop_factor)
1158 dvdec->framecount = 0;
1161 gst_pad_alloc_buffer (dvdec->videosrcpad, 0, (720 * height) * dvdec->bpp,
1162 GST_PAD_CAPS (dvdec->videosrcpad), &outbuf);
1163 if (ret != GST_FLOW_OK)
1166 outframe = GST_BUFFER_DATA (outbuf);
1168 outframe_ptrs[0] = outframe;
1169 outframe_pitches[0] = 720 * dvdec->bpp;
1171 /* the rest only matters for YUY2 */
1172 if (dvdec->bpp < 3) {
1173 outframe_ptrs[1] = outframe_ptrs[0] + 720 * height;
1174 outframe_ptrs[2] = outframe_ptrs[1] + 360 * height;
1176 outframe_pitches[1] = height / 2;
1177 outframe_pitches[2] = outframe_pitches[1];
1180 dv_decode_full_frame (dvdec->decoder, data,
1181 dvdec->space, outframe_ptrs, outframe_pitches);
1183 GST_DEBUG ("pushing video %" GST_TIME_FORMAT,
1184 GST_TIME_ARGS (dvdec->timestamp));
1186 GST_BUFFER_TIMESTAMP (outbuf) = dvdec->timestamp;
1187 GST_BUFFER_OFFSET (outbuf) = dvdec->video_offset;
1188 GST_BUFFER_OFFSET_END (outbuf) = dvdec->video_offset + 1;
1189 GST_BUFFER_DURATION (outbuf) = dvdec->duration * dvdec->drop_factor;
1191 ret = gst_pad_push (dvdec->videosrcpad, outbuf);
1194 dvdec->video_offset++;
1205 static GstFlowReturn
1206 gst_dvdec_decode_frame (GstDVDec * dvdec, const guint8 * data)
1208 GstClockTime next_ts;
1210 GstFlowReturn aret, vret, ret;
1212 if (dvdec->need_discont) {
1217 /* convert to time and store as start/end_timestamp */
1218 format = GST_FORMAT_TIME;
1219 if (!(res = gst_pad_query_convert (dvdec->sinkpad,
1221 dvdec->start_byte, &format, &dvdec->start_timestamp))) {
1225 dvdec->timestamp = dvdec->start_timestamp;
1227 format = GST_FORMAT_TIME;
1228 if (!(res = gst_pad_query_convert (dvdec->sinkpad,
1230 dvdec->stop_byte, &format, &dvdec->stop_timestamp))) {
1234 event = gst_event_new_newsegment (1.0, GST_FORMAT_TIME,
1235 dvdec->start_timestamp, dvdec->stop_timestamp, 0);
1236 gst_dvdec_send_event (dvdec, event);
1238 dvdec->need_discont = FALSE;
1241 framerate = (dvdec->PAL ? PAL_FRAMERATE : NTSC_FRAMERATE);
1243 next_ts = dvdec->timestamp + GST_SECOND / framerate;
1244 dvdec->duration = next_ts - dvdec->timestamp;
1246 dv_parse_packs (dvdec->decoder, data);
1247 if (dv_is_new_recording (dvdec->decoder, data))
1248 dvdec->new_media = TRUE;
1250 aret = ret = gst_dvdec_decode_audio (dvdec, data);
1251 if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)
1254 vret = ret = gst_dvdec_decode_video (dvdec, data);
1255 if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)
1258 if (aret == GST_FLOW_NOT_LINKED && vret == GST_FLOW_NOT_LINKED) {
1259 ret = GST_FLOW_NOT_LINKED;
1264 dvdec->timestamp = next_ts;
1271 GST_DEBUG ("error generating discont event");
1272 return GST_FLOW_ERROR;
1276 /* flush any remaining data in the adapter */
1277 static GstFlowReturn
1278 gst_dvdec_flush (GstDVDec * dvdec)
1280 GstFlowReturn ret = GST_FLOW_OK;
1282 while (gst_adapter_available (dvdec->adapter) >= dvdec->frame_len) {
1286 /* get the accumulated bytes */
1287 data = gst_adapter_peek (dvdec->adapter, dvdec->frame_len);
1289 /* parse header to know the length and other params */
1290 if (dv_parse_header (dvdec->decoder, data) < 0)
1291 goto parse_header_error;
1293 dvdec->found_header = TRUE;
1295 /* after parsing the header we know the length of the data */
1296 dvdec->PAL = dv_system_50_fields (dvdec->decoder);
1297 length = dvdec->frame_len = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
1299 /* if we still have enough for a frame, start decoding */
1300 if (gst_adapter_available (dvdec->adapter) >= length) {
1302 data = gst_adapter_peek (dvdec->adapter, length);
1304 /* and decode the data */
1305 ret = gst_dvdec_decode_frame (dvdec, data);
1307 gst_adapter_flush (dvdec->adapter, length);
1309 if (ret != GST_FLOW_OK)
1319 GST_ELEMENT_ERROR (dvdec, STREAM, DECODE,
1320 ("Error parsing DV header"), ("Error parsing DV header"));
1321 gst_object_unref (dvdec);
1323 return GST_FLOW_ERROR;
1327 /* streaming operation:
1329 * accumulate data until we have a frame, then decode.
1331 static GstFlowReturn
1332 gst_dvdec_chain (GstPad * pad, GstBuffer * buffer)
1337 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
1339 gst_adapter_push (dvdec->adapter, buffer);
1341 /* Apparently dv_parse_header can read from the body of the frame
1342 * too, so it needs more than header_size bytes. Wacky!
1344 if (dvdec->frame_len == -1) {
1345 /* if we don't know the length of a frame, we assume it is
1346 * the NTSC_BUFFER length, as this is enough to figure out
1347 * if this is PAL or NTSC */
1348 dvdec->frame_len = NTSC_BUFFER;
1351 /* and try to flush pending frames */
1352 ret = gst_dvdec_flush (dvdec);
1357 static GstElementStateReturn
1358 gst_dvdec_change_state (GstElement * element)
1360 GstDVDec *dvdec = GST_DVDEC (element);
1361 GstElementStateReturn ret;
1364 transition = GST_STATE_TRANSITION (element);
1366 switch (transition) {
1367 case GST_STATE_NULL_TO_READY:
1369 case GST_STATE_READY_TO_PAUSED:
1371 dv_decoder_new (0, dvdec->clamp_luma, dvdec->clamp_chroma);
1372 dvdec->decoder->quality = qualities[dvdec->quality];
1373 dvdec->audio_offset = 0;
1374 dvdec->video_offset = 0;
1375 dvdec->framecount = 0;
1376 dvdec->found_header = FALSE;
1377 dvdec->frame_len = -1;
1379 * Enable this function call when libdv2 0.100 or higher is more
1382 /* dv_set_quality (dvdec->decoder, qualities [dvdec->quality]); */
1384 case GST_STATE_PAUSED_TO_PLAYING:
1390 ret = parent_class->change_state (element);
1392 switch (transition) {
1393 case GST_STATE_PLAYING_TO_PAUSED:
1395 case GST_STATE_PAUSED_TO_READY:
1396 gst_adapter_clear (dvdec->adapter);
1397 dv_decoder_free (dvdec->decoder);
1398 dvdec->decoder = NULL;
1400 case GST_STATE_READY_TO_NULL:
1409 gst_dvdec_set_property (GObject * object, guint prop_id, const GValue * value,
1414 /* Get a pointer of the right type. */
1415 dvdec = GST_DVDEC (object);
1417 /* Check the argument id to see which argument we're setting. */
1419 case ARG_CLAMP_LUMA:
1420 dvdec->clamp_luma = g_value_get_boolean (value);
1422 case ARG_CLAMP_CHROMA:
1423 dvdec->clamp_chroma = g_value_get_boolean (value);
1426 dvdec->quality = g_value_get_enum (value);
1427 if ((dvdec->quality < 0) || (dvdec->quality > 5))
1430 case ARG_DECODE_NTH:
1431 dvdec->drop_factor = g_value_get_int (value);
1434 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1439 /* The set function is simply the inverse of the get fuction. */
1441 gst_dvdec_get_property (GObject * object, guint prop_id, GValue * value,
1446 dvdec = GST_DVDEC (object);
1449 case ARG_CLAMP_LUMA:
1450 g_value_set_boolean (value, dvdec->clamp_luma);
1452 case ARG_CLAMP_CHROMA:
1453 g_value_set_boolean (value, dvdec->clamp_chroma);
1456 g_value_set_enum (value, dvdec->quality);
1458 case ARG_DECODE_NTH:
1459 g_value_set_int (value, dvdec->drop_factor);
1462 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1467 /* This is the entry into the plugin itself. When the plugin loads,
1468 * this function is called to register everything that the plugin provides.
1471 plugin_init (GstPlugin * plugin)
1473 if (!gst_element_register (plugin, "dvdec", GST_RANK_PRIMARY,
1474 gst_dvdec_get_type ()))
1480 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1483 "Uses libdv to decode DV video (libdv.sourceforge.net)",
1484 plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN);